app: rename load_layer_ids to load_layer_map
[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      layouts(),
118      layers() {
119    assert(g_app == nullptr);
120    g_app = this;
121
122    {
123       auto l = load_layer_map("../ids.json");
124       if (l.is_ok()) {
125          this->layers = l.unwrap();
126       } else {
127          logerror("%s", l.err().value());
128       }
129    }
130
131    {
132       auto l = load_layout("../layout.json");
133       if (l.is_ok()) {
134          this->layouts = l.unwrap();
135       } else {
136          logerror("%s", l.err().value());
137       }
138    }
139 }
140
141 App::~App() { g_app = nullptr; }
142
143 int App::init() {
144    if (!this->display->ok()) {
145       return -1;
146    }
147
148    if (this->layers.mapping.empty()) {
149       logerror("No surface -> layer mapping loaded");
150       return -1;
151    }
152
153    this->display->add_global_handler(
154       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
155          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
156       });
157
158    this->display->add_global_handler(
159       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
160          this->controller = std::make_unique<genivi::controller>(r, name, v);
161
162          // Init controller hooks
163          this->controller->chooks = &this->chooks;
164
165          // XXX: This protocol needs the output, so lets just add our mapping
166          // here...
167          this->controller->add_proxy_to_id_mapping(
168             this->outputs.back()->proxy.get(),
169             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
170                this->outputs.back()->proxy.get())));
171       });
172
173    // First level objects
174    this->display->roundtrip();
175    // Second level objects
176    this->display->roundtrip();
177    // Third level objects
178    this->display->roundtrip();
179
180    return init_layout();
181 }
182
183 int App::dispatch_events() {
184    int ret = this->display->dispatch();
185    if (ret == -1) {
186       logerror("wl_display_dipatch() returned error %d",
187                this->display->get_error());
188       return -1;
189    }
190    this->display->flush();
191
192    // execute pending tasks, that is layout changes etc.
193    this->controller->execute_pending();
194    this->display->roundtrip();
195
196    return 0;
197 }
198
199 //  _       _ _       _                         _    ____
200 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
201 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
202 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
203 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
204 //              |_____|       |___/                 \_\/_/
205 int App::init_layout() {
206    if (!this->controller) {
207       logerror("ivi_controller global not available");
208       return -1;
209    }
210
211    if (this->outputs.empty()) {
212       logerror("no output was set up!");
213       return -1;
214    }
215
216    auto &c = this->controller;
217
218    auto &o = this->outputs.front();
219    auto &s = c->screens.begin()->second;
220    auto &layers = c->layers;
221
222    // XXX: Write output dimensions to ivi controller...
223    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
224
225    // Clear scene
226    layers.clear();
227
228    // Clear screen
229    s->clear();
230
231    // Quick and dirty setup of layers
232    // XXX: This likely needs to be sorted by order (note, we don't (yet?)
233    // do any zorder arrangement).
234    for (auto const &i : this->layers.mapping) {
235       c->layer_create(i.layer_id, o->width, o->height);
236       auto &l = layers[i.layer_id];
237       l->set_destination_rectangle(0, 0, o->width, o->height);
238       l->set_visibility(1);
239       logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
240                i.layer_id, i.id_min, i.id_max);
241    }
242
243    // Add layers to screen (XXX: are they sorted correctly?)
244    s->set_render_order(this->layers.layers);
245
246    c->commit_changes();
247
248    this->display->flush();
249
250    return 0;
251 }
252
253 //                      _          _   _____                 _
254 //  _ __  _ __ _____  _(_) ___  __| | | ____|_   _____ _ __ | |_ ___
255 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | |  _| \ \ / / _ \ '_ \| __/ __|
256 // | |_) | | | (_) >  <| |  __/ (_| | | |___ \ V /  __/ | | | |_\__ \
257 // | .__/|_|  \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
258 // |_|
259 void App::surface_created(uint32_t surface_id) {
260    DB("surface_id is " << surface_id);
261    int layer_id = this->layers.get_layer_id(surface_id).value_or(-1);
262    if (layer_id == -1) {
263       logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
264                surface_id);
265    } else {
266       auto rect = this->layers.get_layer_rect(surface_id).value();
267       this->controller->add_task(
268          "fullscreen surface",
269          [layer_id, surface_id, rect](struct genivi::controller *c) {
270             auto &s = c->surfaces[surface_id];
271
272             int x = rect.x;
273             int y = rect.y;
274             int w = rect.w;
275             int h = rect.h;
276
277             // less-than-0 values refer to MAX + 1 - $VALUE
278             // e.g. MAX is either screen width or height
279             if (w < 0) {
280                w = c->output_size.w + 1 + w;
281             }
282             if (h < 0) {
283                h = c->output_size.h + 1 + h;
284             }
285             logdebug("Computed rect={ %d, %d, %d, %d }", x, y, w, h);
286
287             // configure surface to wxh dimensions
288             s->set_configuration(w, h);
289             // set source rect to "entire surface"
290             s->set_source_rectangle(0, 0, w, h);
291             // set destination to the display rectangle
292             s->set_destination_rectangle(x, y, w, h);
293
294             s->set_visibility(1);
295             c->layers[layer_id]->add_surface(s.get());
296             logdebug("Surface %u now on layer %u", surface_id, layer_id);
297          });
298    }
299 }
300
301 void App::surface_removed(uint32_t surface_id) {
302    DB("surface_id is " << surface_id);
303 }
304
305 //  _     _           _ _                            _   _                 _
306 // | |__ (_)_ __   __| (_)_ __   __ _     __ _ _ __ (_) (_)_ __ ___  _ __ | |
307 // | '_ \| | '_ \ / _` | | '_ \ / _` |   / _` | '_ \| | | | '_ ` _ \| '_ \| |
308 // | |_) | | | | | (_| | | | | | (_| |  | (_| | |_) | | | | | | | | | |_) | |
309 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
310 //                              |___/_____|   |_|                   |_|
311 binding_api::result_type binding_api::register_surface(uint32_t appid,
312                                                        uint32_t surfid) {
313    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
314    if (appid > 0xff) {
315       return Err<json_object *>("invalid appid");
316    }
317
318    if (surfid > 0xffff) {
319       return Err<json_object *>("invalid surfaceid");
320    }
321
322    return Ok(json_object_new_int((appid << 16) + surfid));
323 }
324
325 binding_api::result_type binding_api::debug_layers() {
326    logdebug("%s", __func__);
327    return Ok(to_json(this->app->controller->lprops));
328 }
329
330 binding_api::result_type binding_api::debug_surfaces() {
331    logdebug("%s", __func__);
332    return Ok(to_json(this->app->controller->sprops));
333 }
334
335 binding_api::result_type binding_api::debug_status() {
336    logdebug("%s", __func__);
337    json_object *jr = json_object_new_object();
338    json_object_object_add(jr, "surfaces",
339                           to_json(this->app->controller->sprops));
340    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
341    return Ok(jr);
342 }
343
344 binding_api::result_type binding_api::debug_terminate() {
345    logdebug("%s", __func__);
346    raise(SIGKILL);  // XXX afb-daemon kills it's pgroup using TERM, which
347                     // doesn't play well with perf
348    return Ok(json_object_new_object());
349 }
350
351 //                  _             _ _            _                 _
352 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __ | |__   ___   ___ | | _____
353 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
354 // | (_| (_) | | | | |_| | | (_) | | |  __/ |   | | | | (_) | (_) |   <\__ \
355 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
356 //                                         |_____|
357 void controller_hooks::surface_created(uint32_t surface_id) {
358    this->app->surface_created(surface_id);
359 }
360
361 void controller_hooks::surface_removed(uint32_t surface_id) {
362    this->app->surface_removed(surface_id);
363 }
364
365 }  // namespace wm