2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
10 #include "wayland.hpp"
17 #include <json-c/json.h>
19 #include <bits/signum.h>
29 using json = nlohmann::json;
31 struct wm::area area_from_json(json const &j) {
34 j["name"].get<std::string>(),
36 get<int32_t>(j["width"]), get<int32_t>(j["height"]),
37 get<int32_t>(j["x"]), get<int32_t>(j["y"]),
39 get<uint32_t>(j["zorder"]),
43 result<struct layout> layout_from_json(json const &j) {
45 auto &ja = j["areas"];
47 auto l = layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
49 if (ja.size() > layout::MAX_N_AREAS) {
50 return Err<struct layout>("Invalid number of areas in layout");
53 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
56 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
62 result<json> file_to_json(char const *filename) {
63 std::ifstream i(filename);
65 return Err<json>("Could not open config file");
72 struct result<layouts_type> load_layout(char const *filename) {
73 DB("loading layout from " << filename);
75 auto j = file_to_json(filename);
77 return Err<layouts_type>(j.unwrap_err());
79 json jlayouts = j.unwrap();
81 auto layouts = layouts_type();
82 layouts.reserve(jlayouts.size());
83 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
84 std::back_inserter(layouts), layout_from_json);
89 struct result<layer_map>
90 load_layer_ids(char const *filename) {
91 DB("loading IDs from " << filename);
93 auto j = file_to_json(filename);
95 return Err<layer_map>(j.unwrap_err());
97 json jids = j.unwrap();
99 return to_layer_map(jids);
105 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
106 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
107 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
108 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
110 App::App(wl::display *d)
118 assert(g_app == nullptr);
122 auto l = load_layer_ids("../ids.json");
124 this->layers = l.unwrap();
126 logerror("%s", l.err().value());
131 auto l = load_layout("../layout.json");
133 this->layouts = l.unwrap();
135 logerror("%s", l.err().value());
140 App::~App() { g_app = nullptr; }
143 if (!this->display->ok()) {
147 if (this->layers.mapping.empty()) {
148 logerror("No surface -> layer mapping loaded");
152 this->display->add_global_handler(
153 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
154 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
157 this->display->add_global_handler(
158 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
159 this->controller = std::make_unique<genivi::controller>(r, name, v);
161 // Init controller hooks
162 this->controller->chooks = &this->chooks;
164 // XXX: This protocol needs the output, so lets just add our mapping
166 this->controller->add_proxy_to_id_mapping(
167 this->outputs.back()->proxy.get(),
168 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
169 this->outputs.back()->proxy.get())));
172 // First level objects
173 this->display->roundtrip();
174 // Second level objects
175 this->display->roundtrip();
176 // Third level objects
177 this->display->roundtrip();
179 return init_layout();
182 int App::dispatch_events() {
183 int ret = this->display->dispatch();
185 logerror("wl_display_dipatch() returned error %d",
186 this->display->get_error());
189 this->display->flush();
191 // execute pending tasks, that is layout changes etc.
192 this->controller->execute_pending();
193 this->display->roundtrip();
199 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
200 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
201 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
202 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
203 // |_____| |___/ \_\/_/
204 int App::init_layout() {
205 if (!this->controller) {
206 logerror("ivi_controller global not available");
210 if (this->outputs.empty()) {
211 logerror("no output was set up!");
215 auto &c = this->controller;
217 auto &o = this->outputs.front();
218 auto &s = c->screens.begin()->second;
219 auto &layers = c->layers;
221 // XXX: Write output dimensions to ivi controller...
222 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
230 // Quick and dirty setup of layers
231 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
232 // do any zorder arrangement).
233 for (auto const &i : this->layers.mapping) {
234 c->layer_create(i.layer_id, o->width, o->height);
235 auto &l = layers[i.layer_id];
236 l->set_destination_rectangle(0, 0, o->width, o->height);
237 l->set_visibility(1);
238 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
239 i.layer_id, i.id_min, i.id_max);
242 // Add layers to screen (XXX: are they sorted correctly?)
243 s->set_render_order(this->layers.layers);
247 this->display->flush();
253 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
254 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
255 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
256 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
258 void App::surface_created(uint32_t surface_id) {
259 DB("surface_id is " << surface_id);
260 int layer_id = this->layers.get_layer_id(surface_id).value_or(-1);
261 if (layer_id == -1) {
262 logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
265 auto rect = this->layers.get_layer_rect(surface_id).value();
266 this->controller->add_task(
267 "fullscreen surface",
268 [layer_id, surface_id, rect](struct genivi::controller *c) {
269 auto &s = c->surfaces[surface_id];
276 // less-than-0 values refer to MAX + 1 - $VALUE
277 // e.g. MAX is either screen width or height
279 w = c->output_size.w + 1 + w;
282 h = c->output_size.h + 1 + h;
284 logdebug("Computed rect={ %d, %d, %d, %d }", x, y, w, h);
286 // configure surface to wxh dimensions
287 s->set_configuration(w, h);
288 // set source rect to "entire surface"
289 s->set_source_rectangle(0, 0, w, h);
290 // set destination to the display rectangle
291 s->set_destination_rectangle(x, y, w, h);
293 s->set_visibility(1);
294 c->layers[layer_id]->add_surface(s.get());
295 logdebug("Surface %u now on layer %u", surface_id, layer_id);
300 void App::surface_removed(uint32_t surface_id) {
301 DB("surface_id is " << surface_id);
305 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
306 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
307 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
308 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
309 // |___/_____| |_| |_|
310 binding_api::result_type binding_api::register_surface(uint32_t appid,
312 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
314 return Err<json_object *>("invalid appid");
317 if (surfid > 0xffff) {
318 return Err<json_object *>("invalid surfaceid");
321 return Ok(json_object_new_int((appid << 16) + surfid));
324 binding_api::result_type binding_api::debug_layers() {
325 logdebug("%s", __func__);
326 return Ok(to_json(this->app->controller->lprops));
329 binding_api::result_type binding_api::debug_surfaces() {
330 logdebug("%s", __func__);
331 return Ok(to_json(this->app->controller->sprops));
334 binding_api::result_type binding_api::debug_status() {
335 logdebug("%s", __func__);
336 json_object *jr = json_object_new_object();
337 json_object_object_add(jr, "surfaces",
338 to_json(this->app->controller->sprops));
339 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
343 binding_api::result_type binding_api::debug_terminate() {
344 logdebug("%s", __func__);
345 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
346 // doesn't play well with perf
347 return Ok(json_object_new_object());
351 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
352 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
353 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
354 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
356 void controller_hooks::surface_created(uint32_t surface_id) {
357 this->app->surface_created(surface_id);
360 void controller_hooks::surface_removed(uint32_t surface_id) {
361 this->app->surface_removed(surface_id);