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