clang-tidy once more
[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    std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
69                   std::back_inserter(layouts), layout_from_json);
70
71    return Ok(layouts);
72 }
73
74 struct result<surface_id_to_layer_map>
75    load_layer_ids(char const *filename) {
76    DB("loading IDs from " << filename);
77
78    json jids;
79    std::ifstream i(filename);
80    i >> jids;
81
82    auto m = to_surface_id_to_layer_map(jids["mappings"]);
83
84    if (m.is_ok()) {
85       auto i = m.unwrap();
86
87       auto jtests = jids.value("tests", json());
88
89       std::vector<std::pair<int, int>> tests;
90       std::transform(std::cbegin(jtests), std::cend(jtests),
91                      std::back_inserter(tests), [](json const &j) {
92                         return std::make_pair(get<int>(j["surface_id"]),
93                                               get<int>(j["expect_layer_id"]));
94                      });
95
96       for (auto sid : tests) {
97          if (i.get_layer_for_surface(sid.first).value_or(-1) != sid.second) {
98             return Err<surface_id_to_layer_map>("ID Map embedded test failed!");
99          }
100       }
101    }
102
103    return m;
104 }
105
106 }  // namespace
107
108 App::App(wl::display *d)
109    : api{this}, display{d}, controller{} {
110    // layouts(load_layout("../layout.json").unwrap()) {
111    assert(g_app == nullptr);
112    g_app = this;
113
114    auto a = load_layout("../layout.json");
115    auto b = a.map_err([](char const *e) -> char const * {
116       DB("Could not load layout: " << e);
117       return e;
118    });
119
120    auto c = load_layer_ids("../ids.json");
121    auto e = c.map_err([](char const *e) -> char const * {
122       DB("Could not load ids: " << e);
123       return e;
124    });
125 }
126
127 App::~App() { g_app = nullptr; }
128
129 int App::init() {
130    if (!this->display->ok()) {
131       return -1;
132    }
133
134    this->display->add_global_handler(
135       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
136          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
137       });
138
139    this->display->add_global_handler(
140       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
141          this->controller = std::make_unique<genivi::controller>(r, name, v);
142
143          // XXX: This protocol needs the output, so lets just add our mapping
144          // here...
145          this->controller->add_proxy_to_id_mapping(
146             this->outputs.back()->proxy.get(),
147             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
148                this->outputs.back()->proxy.get())));
149       });
150
151    // First level objects
152    this->display->roundtrip();
153    // Second level objects
154    this->display->roundtrip();
155    // Third level objects
156    this->display->roundtrip();
157
158    return init_layout();
159 }
160
161 int App::dispatch_events() {
162    int ret = this->display->dispatch();
163    if (ret == -1) {
164       logerror("wl_display_dipatch() returned error %d",
165                this->display->get_error());
166       return -1;
167    }
168    this->display->flush();
169
170    // execute pending tasks, that is layout changes etc.
171    this->controller->execute_pending();
172    this->display->roundtrip();
173
174    return 0;
175 }
176
177 //  _       _ _       _                         _    ____
178 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
179 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
180 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
181 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
182 //              |_____|       |___/                 \_\/_/
183 int App::init_layout() {
184    if (!this->controller) {
185       logerror("ivi_controller global not available");
186       return -1;
187    }
188
189    if (this->outputs.empty()) {
190       logerror("no output was set up!");
191       return -1;
192    }
193
194    auto &c = this->controller;
195
196    auto &o = this->outputs.front();
197    auto &s = c->screens.begin()->second;
198    auto &layers = c->layers;
199
200    // XXX: Write output dimensions to ivi controller...
201    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
202
203    // Clear scene
204    layers.clear();
205
206    // Clear screen
207    s->clear();
208
209    // Setup our dummy scene...
210    c->layer_create(100, 0, 0);   // bottom layer, anything else
211    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
212
213    auto &l100 = c->layers[100];
214    auto &l1k = c->layers[1000];
215
216    // Set layers fullscreen
217    l100->set_destination_rectangle(0, 0, o->width, o->height);
218    l1k->set_destination_rectangle(0, 0, o->width, o->height);
219    l100->set_visibility(1);
220    l1k->set_visibility(1);
221
222    // Add layers to screen
223    s->set_render_order({100, 1000});
224
225    c->commit_changes();
226
227    this->display->flush();
228
229    return 0;
230 }
231
232 binding_api::result_type binding_api::register_surface(uint32_t appid,
233                                                        uint32_t surfid) {
234    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
235    if (appid > 0xff) {
236       return Err<json_object *>("invalid appid");
237    }
238
239    if (surfid > 0xffff) {
240       return Err<json_object *>("invalid surfaceid");
241    }
242
243    return Ok(json_object_new_int((appid << 16) + surfid));
244 }
245
246 binding_api::result_type binding_api::debug_layers() {
247    logdebug("%s", __func__);
248    return Ok(to_json(this->app->controller->lprops));
249 }
250
251 binding_api::result_type binding_api::debug_surfaces() {
252    logdebug("%s", __func__);
253    return Ok(to_json(this->app->controller->sprops));
254 }
255
256 binding_api::result_type binding_api::debug_status() {
257    logdebug("%s", __func__);
258    json_object *jr = json_object_new_object();
259    json_object_object_add(jr, "surfaces",
260                           to_json(this->app->controller->sprops));
261    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
262    return Ok(jr);
263 }
264
265 }  // namespace wm