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