2 * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "json_helper.hpp"
22 #include "wayland.hpp"
29 #include <json-c/json.h>
32 #include <bits/signum.h>
43 using json = nlohmann::json;
45 struct wm::area area_from_json(json const &j) {
49 j["width"], j["height"], j["x"], j["y"],
55 result<struct layout> layout_from_json(json const &j) {
56 auto &ja = j["areas"];
58 auto l = layout{j["name"], uint32_t(ja.size()), {}};
60 if (ja.size() > layout::MAX_N_AREAS) {
61 return Err<struct layout>("Invalid number of areas in layout");
64 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
67 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
73 result<json> file_to_json(char const *filename) {
74 std::ifstream i(filename);
76 return Err<json>("Could not open config file");
83 // Will throw if parsing fails
84 struct result<layouts_type> load_layout(char const *filename) {
85 logdebug("loading layout from %s", filename);
87 auto j = file_to_json(filename);
89 return Err<layouts_type>(j.unwrap_err());
91 json jlayouts = j.unwrap();
93 auto layouts = layouts_type();
94 layouts.reserve(jlayouts.size());
95 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
96 std::back_inserter(layouts), layout_from_json);
101 struct result<layer_map>
102 load_layer_map(char const *filename) {
103 logdebug("loading IDs from %s", filename);
105 auto j = file_to_json(filename);
107 return Err<layer_map>(j.unwrap_err());
109 json jids = j.unwrap();
111 return to_layer_map(jids);
117 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
118 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
119 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
120 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
122 App::App(wl::display *d)
134 assert(g_app == nullptr);
139 auto l = load_layer_map(
140 this->config.get_string("layers.json").value().c_str());
142 this->layers = l.unwrap();
144 logerror("%s", l.err().value());
150 load_layout(this->config.get_string("layout.json").value().c_str());
152 this->layouts = l.unwrap();
154 logerror("%s", l.err().value());
157 } catch (std::exception &e) {
158 logerror("Loading of configuration failed: %s", e.what());
162 App::~App() { g_app = nullptr; }
165 if (!this->display->ok()) {
169 if (this->layers.mapping.empty()) {
170 logerror("No surface -> layer mapping loaded");
174 this->display->add_global_handler(
175 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
176 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
179 this->display->add_global_handler(
180 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
181 this->controller = std::make_unique<struct genivi::controller>(r, name, v);
183 // Init controller hooks
184 this->controller->chooks = &this->chooks;
186 // XXX: This protocol needs the output, so lets just add our mapping
188 this->controller->add_proxy_to_id_mapping(
189 this->outputs.back()->proxy.get(),
190 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
191 this->outputs.back()->proxy.get())));
194 // First level objects
195 this->display->roundtrip();
196 // Second level objects
197 this->display->roundtrip();
198 // Third level objects
199 this->display->roundtrip();
201 return init_layout();
204 int App::dispatch_events() {
205 int ret = this->display->dispatch();
207 logerror("wl_display_dipatch() returned error %d",
208 this->display->get_error());
211 this->display->flush();
213 // execute pending tasks, that is layout changes etc.
214 //this->execute_pending();
220 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
221 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
222 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
223 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
224 // |_____| |___/ \_\/_/
225 int App::init_layout() {
226 if (!this->controller) {
227 logerror("ivi_controller global not available");
231 if (this->outputs.empty()) {
232 logerror("no output was set up!");
236 auto &c = this->controller;
238 auto &o = this->outputs.front();
239 auto &s = c->screens.begin()->second;
240 auto &layers = c->layers;
242 // XXX: Write output dimensions to ivi controller...
243 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
251 // Quick and dirty setup of layers
252 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
253 // do any zorder arrangement).
254 for (auto const &i : this->layers.mapping) {
255 c->layer_create(i.layer_id, o->width, o->height);
256 auto &l = layers[i.layer_id];
257 l->set_destination_rectangle(0, 0, o->width, o->height);
258 l->set_visibility(1);
259 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
260 i.layer_id, i.id_min, i.id_max);
263 // Add layers to screen (XXX: are they sorted correctly?)
264 s->set_render_order(this->layers.layers);
268 this->display->flush();
275 // This can fix the HomeScreen...
276 void redraw_fix(App *app, std::unique_ptr<genivi::surface> &s, int x, int y, int w, int h) {
277 { // XXX: Work around weston redraw issues
278 // trigger an update by changing the source dimensions!
279 s->set_configuration(w, h);
280 s->set_source_rectangle(0, 0, w, h);
281 s->set_destination_rectangle(x, y, w, h);
282 app->controller->commit_changes();
283 app->display->roundtrip();
285 // wait some time, for the process to do its thing...
286 using namespace std::chrono_literals;
287 std::this_thread::sleep_for(100ms);
289 // Set a different size then what we actually want.
290 s->set_configuration(w + 1, h);
291 s->set_source_rectangle(0, 0, w + 1, h);
292 s->set_destination_rectangle(x, y, w + 1, h);
293 app->controller->commit_changes();
294 app->display->roundtrip();
296 std::this_thread::sleep_for(100ms);
302 void App::surface_set_layout(uint32_t surface_id) {
303 if (!this->controller->surface_exists(surface_id)) {
304 logerror("Surface %d does not exist", int(surface_id));
308 auto o_layer_id = this->layers.get_layer_id(surface_id);
311 logerror("Surface %d is not associated with any layer!", int(surface_id));
315 uint32_t layer_id = o_layer_id.value();
316 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
319 auto const &layer = this->layers.get_layer(layer_id);
320 auto rect = layer.value().rect;
321 auto &s = this->controller->surfaces[surface_id];
328 // less-than-0 values refer to MAX + 1 - $VALUE
329 // e.g. MAX is either screen width or height
331 w = this->controller->output_size.w + 1 + w;
334 h = this->controller->output_size.h + 1 + h;
337 redraw_fix(this, s, x, y, w, h);
339 // configure surface to wxh dimensions
340 s->set_configuration(w, h);
342 // set source reactangle, even if we should not need to set it.
343 s->set_source_rectangle(0, 0, w, h);
345 // set destination to the display rectangle
346 s->set_destination_rectangle(x, y, w, h);
348 s->set_visibility(0);
351 this->controller->commit_changes();
352 this->display->roundtrip();
354 this->controller->layers[layer_id]->add_surface(s.get());
356 // activate the main_surface right away
357 if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
358 logdebug("Activating main_surface (%d)", surface_id);
360 this->activate_surface(surface_id);
363 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
364 surface_id, layer_id, x, y, w, h);
367 char const *App::activate_surface(uint32_t surface_id) {
368 if (!this->controller->surface_exists(surface_id)) {
369 return "Surface does not exist";
372 // This should involve a policy check, but as we do not (yet) have
373 // such a thing, we will just switch to this surface.
374 // XXX: input focus missing!!1
376 // Make it visible, no (or little effect) if already visible
377 auto &s = this->controller->surfaces[surface_id];
379 // Set all others invisible
380 for (auto &i : this->controller->surfaces) {
381 auto &si = this->controller->sprops[i.second->id];
382 if (int(si.id) != this->layers.main_surface) {
383 i.second->set_visibility(0);
384 this->controller->commit_changes();
385 this->display->flush();
388 s->set_visibility(1);
391 this->controller->commit_changes();
392 this->display->flush();
394 // Current active surface is the first in last_active
395 this->last_active.push_front(s->id);
396 /// // XXX: I am not sure I even need 5 elements...
397 /// this->last_active.resize(std::min(int(this->last_active.size()), 5));
403 char const *App::deactivate_surface(uint32_t surface_id) {
404 if (surface_id == this->layers.main_surface) {
405 return "Cannot deactivate main_surface";
408 if (this->last_active.empty()) {
409 return "Cannot deactivate surface (not active)";
412 // XXX: should an active surface not alsways be front() or
413 // front+1() of last_active?!
415 // XXX: Should I really be able to deactivate a surface that is not
416 // front() of last_active?
417 auto is = std::find(this->last_active.begin(),
418 this->last_active.end(),
421 if (is == this->last_active.end()) {
422 return "Cannot deactivate surface (not active)";
425 this->last_active.erase(is);
427 if (! this->last_active.empty()) {
428 // Should be active already, shouldn't it?
429 this->activate_surface(this->last_active.front());
431 this->activate_surface(this->layers.main_surface);
438 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
439 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
440 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
441 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
443 void App::surface_created(uint32_t surface_id) {
444 //surface_id >>= id_allocator::id_shift;
446 logdebug("surface_id is %u", surface_id);
448 this->surface_set_layout(surface_id);
451 void App::surface_removed(uint32_t surface_id) {
452 //surface_id >>= id_allocator::id_shift;
454 logdebug("surface_id is %u", surface_id);
456 this->id_alloc.remove_id(surface_id);
458 // Also remove from last_active, if found
459 auto i = std::find(this->last_active.begin(),
460 this->last_active.end(), surface_id);
461 if (i != this->last_active.end()) {
462 this->last_active.erase(i);
466 void App::emit_activated(char const *label) {
467 this->api.send_event("activated", json_object_new_string(label));
470 void App::emit_deactivated(char const *label) {
471 this->api.send_event("deactivated", json_object_new_string(label));
474 void App::emit_syncdraw(char const *label) {
475 this->api.send_event("syncdraw", json_object_new_string(label));
478 void App::emit_visible(char const *label, bool is_visible) {
479 this->api.send_event(is_visible ? "visible" : "invisible", json_object_new_string(label));
482 result<int> App::request_surface(char const *drawing_name) {
483 auto lid = this->layers.get_layer_id(std::string(drawing_name));
485 // XXX: to we need to put these applications on the App layer?
486 return Err<int>("Drawing name does not match any role");
489 auto rname = this->id_alloc.lookup(drawing_name);
491 // name does not exist yet, allocate surface id...
492 auto id = int(this->id_alloc.generate_id(drawing_name));
493 this->layers.add_surface(id, lid.value());
495 // XXX: you should fix this!
496 if (!this->layers.main_surface_name.empty() &&
497 this->layers.main_surface_name == drawing_name) {
498 this->layers.main_surface = id;
499 this->activate_surface(id);
500 logdebug("Set main_surface id to %u", id);
506 // Check currently registered drawing names if it is already there.
507 return Err<int>("Surface already present");
510 char const *App::activate_surface(char const *drawing_name) {
511 auto osid = this->id_alloc.lookup(drawing_name);
514 logdebug("ativate surface with name %s and id %u", drawing_name,
516 auto ret = this->activate_surface(osid.value());
518 this->emit_activated(drawing_name);
523 logerror("surface %s unknown", drawing_name);
524 return "Surface unknown";
527 char const *App::deactivate_surface(char const *drawing_name) {
528 auto osid = this->id_alloc.lookup(drawing_name);
531 logdebug("deativate surface with name %s and id %u", drawing_name,
533 return this->deactivate_surface(osid.value());
536 logerror("surface %s unknown", drawing_name);
537 return "Surface unknown";
541 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
542 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
543 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
544 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
545 // |___/_____| |_| |_|
546 binding_api::result_type binding_api::request_surface(
547 char const *drawing_name) {
548 auto r = this->app->request_surface(drawing_name);
550 return Err<json_object *>(r.unwrap_err());
552 return Ok(json_object_new_int(r.unwrap()));
555 binding_api::result_type binding_api::activate_surface(
556 char const *drawing_name) {
557 logdebug("%s drawing_name %s", __func__, drawing_name);
558 auto r = this->app->activate_surface(drawing_name);
560 return Err<json_object *>(r);
562 return Ok(json_object_new_object());
565 binding_api::result_type binding_api::deactivate_surface(char const* drawing_name) {
566 logdebug("%s drawing_name %s", __func__, drawing_name);
567 auto r = this->app->deactivate_surface(drawing_name);
569 return Err<json_object *>(r);
571 return Ok(json_object_new_object());
574 binding_api::result_type binding_api::enddraw(char const* drawing_name) {
575 logdebug("%s drawing_name %s", __func__, drawing_name);
576 return Err<json_object*>("not implemented");
579 binding_api::result_type binding_api::list_drawing_names() {
580 logdebug("%s", __func__);
581 json j = this->app->id_alloc.names;
582 return Ok(json_tokener_parse(j.dump().c_str()));
585 binding_api::result_type binding_api::debug_layers() {
586 logdebug("%s", __func__);
587 return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
590 binding_api::result_type binding_api::debug_surfaces() {
591 logdebug("%s", __func__);
592 return Ok(to_json(this->app->controller->sprops));
595 binding_api::result_type binding_api::debug_status() {
596 logdebug("%s", __func__);
597 json_object *jr = json_object_new_object();
598 json_object_object_add(jr, "surfaces",
599 to_json(this->app->controller->sprops));
600 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
604 binding_api::result_type binding_api::debug_terminate() {
605 logdebug("%s", __func__);
606 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
607 // doesn't play well with perf
608 return Ok(json_object_new_object());
611 binding_api::result_type binding_api::demo_activate_surface(
612 uint32_t surfaceid) {
613 char const *e = this->app->activate_surface(surfaceid);
615 return Err<json_object *>(e);
617 return Ok(json_object_new_object());
620 binding_api::result_type binding_api::demo_activate_all() {
621 for (auto &s : this->app->controller->surfaces) {
622 s.second->set_visibility(1);
624 this->app->controller->commit_changes();
625 this->app->display->flush();
626 return Ok(json_object_new_object());
630 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
631 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
632 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
633 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
635 void controller_hooks::surface_created(uint32_t surface_id) {
636 this->app->surface_created(surface_id);
639 void controller_hooks::surface_removed(uint32_t surface_id) {
640 this->app->surface_removed(surface_id);