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