app/layers: move embedded test run to layers
[staging/windowmanager.git] / src / app.cpp
1 //
2 // Created by mfritzsc on 7/11/17.
3 //
4
5 #include "app.hpp"
6 #include "json_helper.hpp"
7 #include "layers.hpp"
8 #include "layout.hpp"
9 #include "util.hpp"
10 #include "wayland.hpp"
11
12 #include <cstdio>
13 #include <memory>
14
15 #include <cassert>
16
17 #include <json-c/json.h>
18
19 #include <fstream>
20 #include <json.hpp>
21
22 namespace wm {
23
24 namespace {
25 App *g_app;
26
27 using json = nlohmann::json;
28
29 struct wm::area area_from_json(json const &j) {
30    DB(j);
31    return wm::area{
32       j["name"].get<std::string>(),
33       {
34          get<uint32_t>(j["width"]), get<uint32_t>(j["height"]),
35          get<int32_t>(j["x"]), get<int32_t>(j["y"]),
36       },
37       get<uint32_t>(j["zorder"]),
38    };
39 }
40
41 result<struct layout> layout_from_json(json const &j) {
42    DB(j);
43    auto &ja = j["areas"];
44
45    auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
46
47    if (ja.size() > layout::MAX_N_AREAS) {
48       return Err<struct layout>("Invalid number of areas in layout");
49    }
50
51    logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
52             unsigned(ja.size()));
53
54    std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
55                   area_from_json);
56
57    return Ok(l);
58 }
59
60 struct result<layouts_type> load_layout(char const *filename) {
61    DB("loading layout from " << filename);
62
63    json jlayouts;
64    std::ifstream i(filename);
65    i >> jlayouts;
66
67    auto layouts = layouts_type();
68    std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
69                   std::back_inserter(layouts), layout_from_json);
70
71    return Ok(layouts);
72 }
73
74 struct result<surface_id_to_layer_map>
75    load_layer_ids(char const *filename) {
76    DB("loading IDs from " << filename);
77
78    json jids;
79    std::ifstream i(filename);
80    i >> jids;
81
82    return to_surface_id_to_layer_map(jids);
83 }
84
85 }  // namespace
86
87 App::App(wl::display *d)
88    : api{this}, display{d}, controller{} {
89    // layouts(load_layout("../layout.json").unwrap()) {
90    assert(g_app == nullptr);
91    g_app = this;
92
93    auto a = load_layout("../layout.json");
94    auto b = a.map_err([](char const *e) -> char const * {
95       DB("Could not load layout: " << e);
96       return e;
97    });
98
99    auto c = load_layer_ids("../ids.json");
100    auto e = c.map_err([](char const *e) -> char const * {
101       DB("Could not load ids: " << e);
102       return e;
103    });
104 }
105
106 App::~App() { g_app = nullptr; }
107
108 int App::init() {
109    if (!this->display->ok()) {
110       return -1;
111    }
112
113    this->display->add_global_handler(
114       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
115          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
116       });
117
118    this->display->add_global_handler(
119       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
120          this->controller = std::make_unique<genivi::controller>(r, name, v);
121
122          // XXX: This protocol needs the output, so lets just add our mapping
123          // here...
124          this->controller->add_proxy_to_id_mapping(
125             this->outputs.back()->proxy.get(),
126             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
127                this->outputs.back()->proxy.get())));
128       });
129
130    // First level objects
131    this->display->roundtrip();
132    // Second level objects
133    this->display->roundtrip();
134    // Third level objects
135    this->display->roundtrip();
136
137    return init_layout();
138 }
139
140 int App::dispatch_events() {
141    int ret = this->display->dispatch();
142    if (ret == -1) {
143       logerror("wl_display_dipatch() returned error %d",
144                this->display->get_error());
145       return -1;
146    }
147    this->display->flush();
148
149    // execute pending tasks, that is layout changes etc.
150    this->controller->execute_pending();
151    this->display->roundtrip();
152
153    return 0;
154 }
155
156 //  _       _ _       _                         _    ____
157 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
158 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
159 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
160 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
161 //              |_____|       |___/                 \_\/_/
162 int App::init_layout() {
163    if (!this->controller) {
164       logerror("ivi_controller global not available");
165       return -1;
166    }
167
168    if (this->outputs.empty()) {
169       logerror("no output was set up!");
170       return -1;
171    }
172
173    auto &c = this->controller;
174
175    auto &o = this->outputs.front();
176    auto &s = c->screens.begin()->second;
177    auto &layers = c->layers;
178
179    // XXX: Write output dimensions to ivi controller...
180    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
181
182    // Clear scene
183    layers.clear();
184
185    // Clear screen
186    s->clear();
187
188    // Setup our dummy scene...
189    c->layer_create(100, 0, 0);   // bottom layer, anything else
190    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
191
192    auto &l100 = c->layers[100];
193    auto &l1k = c->layers[1000];
194
195    // Set layers fullscreen
196    l100->set_destination_rectangle(0, 0, o->width, o->height);
197    l1k->set_destination_rectangle(0, 0, o->width, o->height);
198    l100->set_visibility(1);
199    l1k->set_visibility(1);
200
201    // Add layers to screen
202    s->set_render_order({100, 1000});
203
204    c->commit_changes();
205
206    this->display->flush();
207
208    return 0;
209 }
210
211 binding_api::result_type binding_api::register_surface(uint32_t appid,
212                                                        uint32_t surfid) {
213    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
214    if (appid > 0xff) {
215       return Err<json_object *>("invalid appid");
216    }
217
218    if (surfid > 0xffff) {
219       return Err<json_object *>("invalid surfaceid");
220    }
221
222    return Ok(json_object_new_int((appid << 16) + surfid));
223 }
224
225 binding_api::result_type binding_api::debug_layers() {
226    logdebug("%s", __func__);
227    return Ok(to_json(this->app->controller->lprops));
228 }
229
230 binding_api::result_type binding_api::debug_surfaces() {
231    logdebug("%s", __func__);
232    return Ok(to_json(this->app->controller->sprops));
233 }
234
235 binding_api::result_type binding_api::debug_status() {
236    logdebug("%s", __func__);
237    json_object *jr = json_object_new_object();
238    json_object_object_add(jr, "surfaces",
239                           to_json(this->app->controller->sprops));
240    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
241    return Ok(jr);
242 }
243
244 }  // namespace wm