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