app: preallocate layouts storage
[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 = wm::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}, chooks{this}, display{d}, controller{} {
96    // layouts(load_layout("../layout.json").unwrap()) {
97    assert(g_app == nullptr);
98    g_app = this;
99
100    auto a = load_layout("../layout.json");
101    auto b = a.map_err([](char const *e) -> char const * {
102       DB("Could not load layout: " << e);
103       return e;
104    });
105
106    auto c = load_layer_ids("../ids.json");
107    auto e = c.map_err([](char const *e) -> char const * {
108       DB("Could not load ids: " << e);
109       return e;
110    });
111 }
112
113 App::~App() { g_app = nullptr; }
114
115 int App::init() {
116    if (!this->display->ok()) {
117       return -1;
118    }
119
120    this->display->add_global_handler(
121       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
122          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
123       });
124
125    this->display->add_global_handler(
126       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
127          this->controller = std::make_unique<genivi::controller>(r, name, v);
128
129          // Init controller hooks
130          this->controller->chooks = &this->chooks;
131
132          // XXX: This protocol needs the output, so lets just add our mapping
133          // here...
134          this->controller->add_proxy_to_id_mapping(
135             this->outputs.back()->proxy.get(),
136             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
137                this->outputs.back()->proxy.get())));
138       });
139
140    // First level objects
141    this->display->roundtrip();
142    // Second level objects
143    this->display->roundtrip();
144    // Third level objects
145    this->display->roundtrip();
146
147    return init_layout();
148 }
149
150 int App::dispatch_events() {
151    int ret = this->display->dispatch();
152    if (ret == -1) {
153       logerror("wl_display_dipatch() returned error %d",
154                this->display->get_error());
155       return -1;
156    }
157    this->display->flush();
158
159    // execute pending tasks, that is layout changes etc.
160    this->controller->execute_pending();
161    this->display->roundtrip();
162
163    return 0;
164 }
165
166 //  _       _ _       _                         _    ____
167 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
168 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
169 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
170 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
171 //              |_____|       |___/                 \_\/_/
172 int App::init_layout() {
173    if (!this->controller) {
174       logerror("ivi_controller global not available");
175       return -1;
176    }
177
178    if (this->outputs.empty()) {
179       logerror("no output was set up!");
180       return -1;
181    }
182
183    auto &c = this->controller;
184
185    auto &o = this->outputs.front();
186    auto &s = c->screens.begin()->second;
187    auto &layers = c->layers;
188
189    // XXX: Write output dimensions to ivi controller...
190    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
191
192    // Clear scene
193    layers.clear();
194
195    // Clear screen
196    s->clear();
197
198    // Setup our dummy scene...
199    c->layer_create(100, 0, 0);   // bottom layer, anything else
200    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
201
202    auto &l100 = c->layers[100];
203    auto &l1k = c->layers[1000];
204
205    // Set layers fullscreen
206    l100->set_destination_rectangle(0, 0, o->width, o->height);
207    l1k->set_destination_rectangle(0, 0, o->width, o->height);
208    l100->set_visibility(1);
209    l1k->set_visibility(1);
210
211    // Add layers to screen
212    s->set_render_order({100, 1000});
213
214    c->commit_changes();
215
216    this->display->flush();
217
218    return 0;
219 }
220
221 void App::surface_created(uint32_t surface_id) {
222    DB("surface_id is " << surface_id);
223    this->controller->add_task("fullscreen surface", [surface_id](struct genivi::controller *c) {
224       auto &s = c->surfaces[surface_id];
225       s->set_destination_rectangle(0, 0, c->output_size.w, c->output_size.h);
226       s->set_visibility(1);
227       uint32_t lid = surface_id == 0x16180 ? 1000 : 100;
228       c->layers[lid]->add_surface(s.get());
229       logdebug("Surface %u now fullscreen on layer %u", surface_id, lid);
230    });
231 }
232
233 void App::surface_removed(uint32_t surface_id) {
234    DB("surface_id is " << surface_id);
235 }
236
237 //  _     _           _ _                            _   _                 _
238 // | |__ (_)_ __   __| (_)_ __   __ _     __ _ _ __ (_) (_)_ __ ___  _ __ | |
239 // | '_ \| | '_ \ / _` | | '_ \ / _` |   / _` | '_ \| | | | '_ ` _ \| '_ \| |
240 // | |_) | | | | | (_| | | | | | (_| |  | (_| | |_) | | | | | | | | | |_) | |
241 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
242 //                              |___/_____|   |_|                   |_|
243 binding_api::result_type binding_api::register_surface(uint32_t appid,
244                                                        uint32_t surfid) {
245    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
246    if (appid > 0xff) {
247       return Err<json_object *>("invalid appid");
248    }
249
250    if (surfid > 0xffff) {
251       return Err<json_object *>("invalid surfaceid");
252    }
253
254    return Ok(json_object_new_int((appid << 16) + surfid));
255 }
256
257 binding_api::result_type binding_api::debug_layers() {
258    logdebug("%s", __func__);
259    return Ok(to_json(this->app->controller->lprops));
260 }
261
262 binding_api::result_type binding_api::debug_surfaces() {
263    logdebug("%s", __func__);
264    return Ok(to_json(this->app->controller->sprops));
265 }
266
267 binding_api::result_type binding_api::debug_status() {
268    logdebug("%s", __func__);
269    json_object *jr = json_object_new_object();
270    json_object_object_add(jr, "surfaces",
271                           to_json(this->app->controller->sprops));
272    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
273    return Ok(jr);
274 }
275
276 //                  _             _ _            _                 _
277 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __ | |__   ___   ___ | | _____
278 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
279 // | (_| (_) | | | | |_| | | (_) | | |  __/ |   | | | | (_) | (_) |   <\__ \
280 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
281 //                                         |_____|
282 void controller_hooks::surface_created(uint32_t surface_id) {
283    this->app->surface_created(surface_id);
284 }
285
286 void controller_hooks::surface_removed(uint32_t surface_id) {
287    this->app->surface_removed(surface_id);
288 }
289
290 }  // namespace wm