app: handle failure to load layout.json gracefully
[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 #include <bits/signum.h>
22 #include <csignal>
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 struct result<layouts_type> load_layout(char const *filename) {
63    DB("loading layout from " << filename);
64
65    json jlayouts;
66    std::ifstream i(filename);
67    i >> jlayouts;
68
69    auto layouts = layouts_type();
70    layouts.reserve(jlayouts.size());
71    std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
72                   std::back_inserter(layouts), layout_from_json);
73
74    return Ok(layouts);
75 }
76
77 struct result<surface_id_to_layer_map>
78    load_layer_ids(char const *filename) {
79    DB("loading IDs from " << filename);
80
81    json jids;
82    std::ifstream i(filename);
83    i >> jids;
84
85    return to_surface_id_to_layer_map(jids);
86 }
87
88 }  // namespace
89
90 //       _                    _                  _                 _
91 //   ___| | __ _ ___ ___     / \   _ __  _ __   (_)_ __ ___  _ __ | |
92 //  / __| |/ _` / __/ __|   / _ \ | '_ \| '_ \  | | '_ ` _ \| '_ \| |
93 // | (__| | (_| \__ \__ \  / ___ \| |_) | |_) | | | | | | | | |_) | |
94 //  \___|_|\__,_|___/___/ /_/   \_\ .__/| .__/  |_|_| |_| |_| .__/|_|
95 //                                |_|   |_|                 |_|
96 App::App(wl::display *d)
97    : api{this},
98      chooks{this},
99      display{d},
100      controller{},
101      outputs(),
102      layouts(), //load_layout("../layout.json").unwrap()),
103      surface2layer(load_layer_ids("../ids.json").unwrap()) {
104    // layouts(load_layout("../layout.json").unwrap()) {
105    assert(g_app == nullptr);
106    g_app = this;
107
108    try {
109       auto l = load_layout("../layout.json");
110       if (l.is_err()) {
111          logerror("Coult not load layout configuration: %s", l.err().value());
112       }
113    } catch (std::exception &e) {
114       logerror("Coult not load layout configuration: %s", e.what());
115    }
116 }
117
118 App::~App() { g_app = nullptr; }
119
120 int App::init() {
121    if (!this->display->ok()) {
122       return -1;
123    }
124
125    this->display->add_global_handler(
126       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
127          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
128       });
129
130    this->display->add_global_handler(
131       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
132          this->controller = std::make_unique<genivi::controller>(r, name, v);
133
134          // Init controller hooks
135          this->controller->chooks = &this->chooks;
136
137          // XXX: This protocol needs the output, so lets just add our mapping
138          // here...
139          this->controller->add_proxy_to_id_mapping(
140             this->outputs.back()->proxy.get(),
141             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
142                this->outputs.back()->proxy.get())));
143       });
144
145    // First level objects
146    this->display->roundtrip();
147    // Second level objects
148    this->display->roundtrip();
149    // Third level objects
150    this->display->roundtrip();
151
152    return init_layout();
153 }
154
155 int App::dispatch_events() {
156    int ret = this->display->dispatch();
157    if (ret == -1) {
158       logerror("wl_display_dipatch() returned error %d",
159                this->display->get_error());
160       return -1;
161    }
162    this->display->flush();
163
164    // execute pending tasks, that is layout changes etc.
165    this->controller->execute_pending();
166    this->display->roundtrip();
167
168    return 0;
169 }
170
171 //  _       _ _       _                         _    ____
172 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
173 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
174 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
175 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
176 //              |_____|       |___/                 \_\/_/
177 int App::init_layout() {
178    if (!this->controller) {
179       logerror("ivi_controller global not available");
180       return -1;
181    }
182
183    if (this->outputs.empty()) {
184       logerror("no output was set up!");
185       return -1;
186    }
187
188    auto &c = this->controller;
189
190    auto &o = this->outputs.front();
191    auto &s = c->screens.begin()->second;
192    auto &layers = c->layers;
193
194    // XXX: Write output dimensions to ivi controller...
195    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
196
197    // Clear scene
198    layers.clear();
199
200    // Clear screen
201    s->clear();
202
203    // Quick and dirty setup of layers
204    // XXX: This likely needs to be sorted by order (note, we don't (yet?)
205    // do any zorder arrangement).
206    for (auto const &i : this->surface2layer.mapping) {
207       c->layer_create(i.layer_id, o->width, o->height);
208       auto &l = layers[i.layer_id];
209       l->set_destination_rectangle(0, 0, o->width, o->height);
210       l->set_visibility(1);
211       logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
212                i.layer_id, i.id_min, i.id_max);
213    }
214
215    // Add layers to screen (XXX: are they sorted correctly?)
216    s->set_render_order(this->surface2layer.layers);
217
218    c->commit_changes();
219
220    this->display->flush();
221
222    return 0;
223 }
224
225 //                      _          _   _____                 _
226 //  _ __  _ __ _____  _(_) ___  __| | | ____|_   _____ _ __ | |_ ___
227 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | |  _| \ \ / / _ \ '_ \| __/ __|
228 // | |_) | | | (_) >  <| |  __/ (_| | | |___ \ V /  __/ | | | |_\__ \
229 // | .__/|_|  \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
230 // |_|
231 void App::surface_created(uint32_t surface_id) {
232    DB("surface_id is " << surface_id);
233    int layer_id =
234       this->surface2layer.get_layer_for_surface(surface_id).value_or(-1);
235    if (layer_id == -1) {
236       logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
237                surface_id);
238    } else {
239       auto rect = this->surface2layer.get_rect_for_surface(surface_id).value();
240       this->controller->add_task(
241          "fullscreen surface",
242          [layer_id, surface_id, rect](struct genivi::controller *c) {
243             auto &s = c->surfaces[surface_id];
244
245             int x = rect.x;
246             int y = rect.y;
247             int w = rect.w;
248             int h = rect.h;
249
250             if (w < 0) {
251                w = c->output_size.w + 1 + w;
252             }
253             if (h < 0) {
254                h = c->output_size.h + 1 + h;
255             }
256             logdebug("Computed rect={ %d, %d, %d, %d }", x,  y, w, h);
257
258             s->set_configuration(w, h);
259             s->set_source_rectangle(0, 0, w, h);
260             s->set_destination_rectangle(x, y, w, h);
261
262             s->set_visibility(1);
263             c->layers[layer_id]->add_surface(s.get());
264             logdebug("Surface %u now on layer %u", surface_id,
265                      layer_id);
266          });
267    }
268 }
269
270 void App::surface_removed(uint32_t surface_id) {
271    DB("surface_id is " << surface_id);
272 }
273
274 //  _     _           _ _                            _   _                 _
275 // | |__ (_)_ __   __| (_)_ __   __ _     __ _ _ __ (_) (_)_ __ ___  _ __ | |
276 // | '_ \| | '_ \ / _` | | '_ \ / _` |   / _` | '_ \| | | | '_ ` _ \| '_ \| |
277 // | |_) | | | | | (_| | | | | | (_| |  | (_| | |_) | | | | | | | | | |_) | |
278 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
279 //                              |___/_____|   |_|                   |_|
280 binding_api::result_type binding_api::register_surface(uint32_t appid,
281                                                        uint32_t surfid) {
282    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
283    if (appid > 0xff) {
284       return Err<json_object *>("invalid appid");
285    }
286
287    if (surfid > 0xffff) {
288       return Err<json_object *>("invalid surfaceid");
289    }
290
291    return Ok(json_object_new_int((appid << 16) + surfid));
292 }
293
294 binding_api::result_type binding_api::debug_layers() {
295    logdebug("%s", __func__);
296    return Ok(to_json(this->app->controller->lprops));
297 }
298
299 binding_api::result_type binding_api::debug_surfaces() {
300    logdebug("%s", __func__);
301    return Ok(to_json(this->app->controller->sprops));
302 }
303
304 binding_api::result_type binding_api::debug_status() {
305    logdebug("%s", __func__);
306    json_object *jr = json_object_new_object();
307    json_object_object_add(jr, "surfaces",
308                           to_json(this->app->controller->sprops));
309    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
310    return Ok(jr);
311 }
312
313 binding_api::result_type binding_api::debug_terminate() {
314    logdebug("%s", __func__);
315    raise(SIGKILL);  // XXX afb-daemon kills it's pgroup using TERM, which doesn't play well with perf
316    return Ok(json_object_new_object());
317 }
318
319 //                  _             _ _            _                 _
320 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __ | |__   ___   ___ | | _____
321 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
322 // | (_| (_) | | | | |_| | | (_) | | |  __/ |   | | | | (_) | (_) |   <\__ \
323 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
324 //                                         |_____|
325 void controller_hooks::surface_created(uint32_t surface_id) {
326    this->app->surface_created(surface_id);
327 }
328
329 void controller_hooks::surface_removed(uint32_t surface_id) {
330    this->app->surface_removed(surface_id);
331 }
332
333 }  // namespace wm