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