app: debug_layers returns a representation of layers.json configuration
[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 <bits/signum.h>
20 #include <csignal>
21 #include <fstream>
22 #include <json.hpp>
23
24 namespace wm {
25
26 namespace {
27 App *g_app;
28
29 using json = nlohmann::json;
30
31 struct wm::area area_from_json(json const &j) {
32    DB(j);
33    return wm::area{
34       j["name"].get<std::string>(),
35       {
36          get<int32_t>(j["width"]), get<int32_t>(j["height"]),
37          get<int32_t>(j["x"]), get<int32_t>(j["y"]),
38       },
39       get<uint32_t>(j["zorder"]),
40    };
41 }
42
43 result<struct layout> layout_from_json(json const &j) {
44    DB(j);
45    auto &ja = j["areas"];
46
47    auto l = layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
48
49    if (ja.size() > layout::MAX_N_AREAS) {
50       return Err<struct layout>("Invalid number of areas in layout");
51    }
52
53    logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
54             unsigned(ja.size()));
55
56    std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
57                   area_from_json);
58
59    return Ok(l);
60 }
61
62 result<json> file_to_json(char const *filename) {
63    std::ifstream i(filename);
64    if (i.fail()) {
65       return Err<json>("Could not open config file");
66    }
67    json j;
68    i >> j;
69    return Ok(j);
70 }
71
72 // Will throw if parsing fails
73 struct result<layouts_type> load_layout(char const *filename) {
74    DB("loading layout from " << filename);
75
76    auto j = file_to_json(filename);
77    if (j.is_err()) {
78       return Err<layouts_type>(j.unwrap_err());
79    }
80    json jlayouts = j.unwrap();
81
82    auto layouts = layouts_type();
83    layouts.reserve(jlayouts.size());
84    std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
85                   std::back_inserter(layouts), layout_from_json);
86
87    return Ok(layouts);
88 }
89
90 struct result<layer_map>
91    load_layer_map(char const *filename) {
92    DB("loading IDs from " << filename);
93
94    auto j = file_to_json(filename);
95    if (j.is_err()) {
96       return Err<layer_map>(j.unwrap_err());
97    }
98    json jids = j.unwrap();
99
100    return to_layer_map(jids);
101 }
102
103 }  // namespace
104
105 //       _                    _                  _                 _
106 //   ___| | __ _ ___ ___     / \   _ __  _ __   (_)_ __ ___  _ __ | |
107 //  / __| |/ _` / __/ __|   / _ \ | '_ \| '_ \  | | '_ ` _ \| '_ \| |
108 // | (__| | (_| \__ \__ \  / ___ \| |_) | |_) | | | | | | | | |_) | |
109 //  \___|_|\__,_|___/___/ /_/   \_\ .__/| .__/  |_|_| |_| |_| .__/|_|
110 //                                |_|   |_|                 |_|
111 App::App(wl::display *d)
112    : api{this},
113      chooks{this},
114      display{d},
115      controller{},
116      outputs(),
117      config(),
118      layouts(),
119      layers() {
120    assert(g_app == nullptr);
121    g_app = this;
122
123    try {
124       {
125          auto l = load_layer_map(
126                  this->config.get_string("layers.json").value().c_str());
127          if (l.is_ok()) {
128             this->layers = l.unwrap();
129          } else {
130             logerror("%s", l.err().value());
131          }
132       }
133
134       {
135          auto l = load_layout(
136                  this->config.get_string("layout.json").value().c_str());
137          if (l.is_ok()) {
138             this->layouts = l.unwrap();
139          } else {
140             logerror("%s", l.err().value());
141          }
142       }
143    } catch (std::exception &e) {
144       logerror("Loading of configuration failed: %s", e.what());
145    }
146 }
147
148 App::~App() { g_app = nullptr; }
149
150 int App::init() {
151    if (!this->display->ok()) {
152       return -1;
153    }
154
155    if (this->layers.mapping.empty()) {
156       logerror("No surface -> layer mapping loaded");
157       return -1;
158    }
159
160    this->display->add_global_handler(
161       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
162          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
163       });
164
165    this->display->add_global_handler(
166       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
167          this->controller = std::make_unique<genivi::controller>(r, name, v);
168
169          // Init controller hooks
170          this->controller->chooks = &this->chooks;
171
172          // XXX: This protocol needs the output, so lets just add our mapping
173          // here...
174          this->controller->add_proxy_to_id_mapping(
175             this->outputs.back()->proxy.get(),
176             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
177                this->outputs.back()->proxy.get())));
178       });
179
180    // First level objects
181    this->display->roundtrip();
182    // Second level objects
183    this->display->roundtrip();
184    // Third level objects
185    this->display->roundtrip();
186
187    return init_layout();
188 }
189
190 int App::dispatch_events() {
191    int ret = this->display->dispatch();
192    if (ret == -1) {
193       logerror("wl_display_dipatch() returned error %d",
194                this->display->get_error());
195       return -1;
196    }
197    this->display->flush();
198
199    // execute pending tasks, that is layout changes etc.
200    this->controller->execute_pending();
201    this->display->roundtrip();
202
203    return 0;
204 }
205
206 //  _       _ _       _                         _    ____
207 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
208 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
209 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
210 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
211 //              |_____|       |___/                 \_\/_/
212 int App::init_layout() {
213    if (!this->controller) {
214       logerror("ivi_controller global not available");
215       return -1;
216    }
217
218    if (this->outputs.empty()) {
219       logerror("no output was set up!");
220       return -1;
221    }
222
223    auto &c = this->controller;
224
225    auto &o = this->outputs.front();
226    auto &s = c->screens.begin()->second;
227    auto &layers = c->layers;
228
229    // XXX: Write output dimensions to ivi controller...
230    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
231
232    // Clear scene
233    layers.clear();
234
235    // Clear screen
236    s->clear();
237
238    // Quick and dirty setup of layers
239    // XXX: This likely needs to be sorted by order (note, we don't (yet?)
240    // do any zorder arrangement).
241    for (auto const &i : this->layers.mapping) {
242       c->layer_create(i.layer_id, o->width, o->height);
243       auto &l = layers[i.layer_id];
244       l->set_destination_rectangle(0, 0, o->width, o->height);
245       l->set_visibility(1);
246       logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
247                i.layer_id, i.id_min, i.id_max);
248    }
249
250    // Add layers to screen (XXX: are they sorted correctly?)
251    s->set_render_order(this->layers.layers);
252
253    c->commit_changes();
254
255    this->display->flush();
256
257    return 0;
258 }
259
260 //                      _          _   _____                 _
261 //  _ __  _ __ _____  _(_) ___  __| | | ____|_   _____ _ __ | |_ ___
262 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | |  _| \ \ / / _ \ '_ \| __/ __|
263 // | |_) | | | (_) >  <| |  __/ (_| | | |___ \ V /  __/ | | | |_\__ \
264 // | .__/|_|  \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
265 // |_|
266 void App::surface_created(uint32_t surface_id) {
267    DB("surface_id is " << surface_id);
268    int layer_id = this->layers.get_layer_id(surface_id).value_or(-1);
269    if (layer_id == -1) {
270       logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
271                surface_id);
272    } else {
273       auto rect = this->layers.get_layer_rect(surface_id).value();
274       this->controller->add_task(
275          "fullscreen surface",
276          [layer_id, surface_id, rect](struct genivi::controller *c) {
277             auto &s = c->surfaces[surface_id];
278
279             int x = rect.x;
280             int y = rect.y;
281             int w = rect.w;
282             int h = rect.h;
283
284             // less-than-0 values refer to MAX + 1 - $VALUE
285             // e.g. MAX is either screen width or height
286             if (w < 0) {
287                w = c->output_size.w + 1 + w;
288             }
289             if (h < 0) {
290                h = c->output_size.h + 1 + h;
291             }
292             logdebug("Computed rect={ %d, %d, %d, %d }", x, y, w, h);
293
294             // configure surface to wxh dimensions
295             s->set_configuration(w, h);
296             // set source rect to "entire surface"
297             s->set_source_rectangle(0, 0, w, h);
298             // set destination to the display rectangle
299             s->set_destination_rectangle(x, y, w, h);
300
301             s->set_visibility(1);
302             c->layers[layer_id]->add_surface(s.get());
303             logdebug("Surface %u now on layer %u", surface_id, layer_id);
304          });
305    }
306 }
307
308 void App::surface_removed(uint32_t surface_id) {
309    DB("surface_id is " << surface_id);
310 }
311
312 //  _     _           _ _                            _   _                 _
313 // | |__ (_)_ __   __| (_)_ __   __ _     __ _ _ __ (_) (_)_ __ ___  _ __ | |
314 // | '_ \| | '_ \ / _` | | '_ \ / _` |   / _` | '_ \| | | | '_ ` _ \| '_ \| |
315 // | |_) | | | | | (_| | | | | | (_| |  | (_| | |_) | | | | | | | | | |_) | |
316 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
317 //                              |___/_____|   |_|                   |_|
318 binding_api::result_type binding_api::register_surface(uint32_t appid,
319                                                        uint32_t surfid) {
320    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
321    if (appid > 0xff) {
322       return Err<json_object *>("invalid appid");
323    }
324
325    if (surfid > 0xffff) {
326       return Err<json_object *>("invalid surfaceid");
327    }
328
329    return Ok(json_object_new_int((appid << 16) + surfid));
330 }
331
332 binding_api::result_type binding_api::debug_layers() {
333    logdebug("%s", __func__);
334    return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
335 }
336
337 binding_api::result_type binding_api::debug_surfaces() {
338    logdebug("%s", __func__);
339    return Ok(to_json(this->app->controller->sprops));
340 }
341
342 binding_api::result_type binding_api::debug_status() {
343    logdebug("%s", __func__);
344    json_object *jr = json_object_new_object();
345    json_object_object_add(jr, "surfaces",
346                           to_json(this->app->controller->sprops));
347    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
348    return Ok(jr);
349 }
350
351 binding_api::result_type binding_api::debug_terminate() {
352    logdebug("%s", __func__);
353    raise(SIGKILL);  // XXX afb-daemon kills it's pgroup using TERM, which
354                     // doesn't play well with perf
355    return Ok(json_object_new_object());
356 }
357
358 //                  _             _ _            _                 _
359 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __ | |__   ___   ___ | | _____
360 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
361 // | (_| (_) | | | | |_| | | (_) | | |  __/ |   | | | | (_) | (_) |   <\__ \
362 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
363 //                                         |_____|
364 void controller_hooks::surface_created(uint32_t surface_id) {
365    this->app->surface_created(surface_id);
366 }
367
368 void controller_hooks::surface_removed(uint32_t surface_id) {
369    this->app->surface_removed(surface_id);
370 }
371
372 }  // namespace wm