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