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)
132 assert(g_app == nullptr);
137 auto l = load_layer_map(
138 this->config.get_string("layers.json").value().c_str());
140 this->layers = l.unwrap();
142 logerror("%s", l.err().value());
148 load_layout(this->config.get_string("layout.json").value().c_str());
150 this->layouts = l.unwrap();
152 logerror("%s", l.err().value());
155 } catch (std::exception &e) {
156 logerror("Loading of configuration failed: %s", e.what());
160 App::~App() { g_app = nullptr; }
163 if (!this->display->ok()) {
167 if (this->layers.mapping.empty()) {
168 logerror("No surface -> layer mapping loaded");
172 this->display->add_global_handler(
173 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
174 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
177 this->display->add_global_handler(
178 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
179 this->controller = std::make_unique<struct genivi::controller>(r, name, v);
181 // Init controller hooks
182 this->controller->chooks = &this->chooks;
184 // XXX: This protocol needs the output, so lets just add our mapping
186 this->controller->add_proxy_to_id_mapping(
187 this->outputs.back()->proxy.get(),
188 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
189 this->outputs.back()->proxy.get())));
192 // First level objects
193 this->display->roundtrip();
194 // Second level objects
195 this->display->roundtrip();
196 // Third level objects
197 this->display->roundtrip();
199 return init_layers();
202 int App::dispatch_events() {
203 int ret = this->display->dispatch();
205 logerror("wl_display_dipatch() returned error %d",
206 this->display->get_error());
209 this->display->flush();
211 // execute pending tasks, that is layout changes etc.
212 //this->execute_pending();
218 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
219 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
220 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
221 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
222 // |_____| |___/ \_\/_/
223 int App::init_layers() {
224 if (!this->controller) {
225 logerror("ivi_controller global not available");
229 if (this->outputs.empty()) {
230 logerror("no output was set up!");
234 auto &c = this->controller;
236 auto &o = this->outputs.front();
237 auto &s = c->screens.begin()->second;
238 auto &layers = c->layers;
240 // XXX: Write output dimensions to ivi controller...
241 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
249 // Quick and dirty setup of layers
250 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
251 // do any zorder arrangement).
252 for (auto const &i : this->layers.mapping) {
253 c->layer_create(i.layer_id, o->width, o->height);
254 auto &l = layers[i.layer_id];
255 l->set_destination_rectangle(0, 0, o->width, o->height);
256 l->set_visibility(1);
257 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
258 i.layer_id, i.id_min, i.id_max);
261 // Add layers to screen (XXX: are they sorted correctly?)
262 s->set_render_order(this->layers.layers);
266 this->display->flush();
273 // This can fix the HomeScreen...
274 void redraw_fix(App *app, std::unique_ptr<genivi::surface> &s, int x, int y, int w, int h) {
275 { // XXX: Work around weston redraw issues
276 // trigger an update by changing the source dimensions!
277 s->set_configuration(w + 1, h);
278 s->set_source_rectangle(0, 0, w + 1, h);
279 s->set_destination_rectangle(x, y, w + 1, h);
280 app->controller->commit_changes();
281 app->display->roundtrip();
283 // wait some time, for the process to do its thing...
284 using namespace std::chrono_literals;
285 std::this_thread::sleep_for(100ms);
287 // Set a different size then what we actually want.
288 s->set_configuration(w, h);
289 s->set_source_rectangle(0, 0, w, h);
290 s->set_destination_rectangle(x, y, w, h);
291 app->controller->commit_changes();
292 app->display->roundtrip();
298 void App::surface_init_layout(uint32_t surface_id) {
299 if (!this->controller->surface_exists(surface_id)) {
300 logerror("Surface %d does not exist", int(surface_id));
304 auto o_layer_id = this->layers.get_layer_id(surface_id);
307 logerror("Surface %d is not associated with any layer!", int(surface_id));
311 uint32_t layer_id = o_layer_id.value();
312 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
315 auto const &layer = this->layers.get_layer(layer_id);
316 auto rect = layer.value().rect;
317 auto &s = this->controller->surfaces[surface_id];
324 // less-than-0 values refer to MAX + 1 - $VALUE
325 // e.g. MAX is either screen width or height
327 w = this->controller->output_size.w + 1 + w;
330 h = this->controller->output_size.h + 1 + h;
333 // configure surface to wxh dimensions
334 s->set_configuration(w, h);
336 // set source reactangle, even if we should not need to set it.
337 s->set_source_rectangle(0, 0, w, h);
339 // set destination to the display rectangle
340 s->set_destination_rectangle(x, y, w, h);
342 s->set_visibility(0);
345 this->controller->commit_changes();
346 this->display->roundtrip();
348 redraw_fix(this, s, x, y, w, h);
350 this->controller->layers[layer_id]->add_surface(s.get());
352 // activate the main_surface right away
353 if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
354 logdebug("Activating main_surface (%d)", surface_id);
356 this->activate_surface(this->lookup_name(surface_id).value_or("unknown-name").c_str());
359 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
360 surface_id, layer_id, x, y, w, h);
363 char const *App::activate_surface(char const *drawing_name) {
367 auto oid = this->lookup_id(drawing_name);
369 surface_id = oid.value();
371 return "Surface does not exist";
375 if (!this->controller->surface_exists(surface_id)) {
376 return "Surface does not exist";
379 // This should involve a policy check, but as we do not (yet) have
380 // such a thing, we will just switch to this surface.
381 // XXX: input focus missing!!1
383 // Make it visible, no (or little effect) if already visible
384 auto &s = this->controller->surfaces[surface_id];
386 // Set all others invisible
387 for (auto &i : this->controller->surfaces) {
388 auto &si = this->controller->sprops[i.second->id];
389 if (si.visibility != 0 && int(si.id) != this->layers.main_surface) {
390 this->deactivate(i.second->id);
393 this->activate(s->id);
396 this->controller->commit_changes();
397 this->display->flush();
403 char const *App::deactivate_surface(char const *drawing_name) {
407 auto oid = this->lookup_id(drawing_name);
409 surface_id = oid.value();
411 return "Surface does not exist";
415 if (surface_id == this->layers.main_surface) {
416 return "Cannot deactivate main_surface";
419 this->deactivate(surface_id);
421 this->controller->commit_changes();
422 this->display->flush();
428 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
429 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
430 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
431 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
433 void App::surface_created(uint32_t surface_id) {
434 logdebug("surface_id is %u", surface_id);
436 this->surface_init_layout(surface_id);
439 void App::surface_removed(uint32_t surface_id) {
440 logdebug("surface_id is %u", surface_id);
442 this->id_alloc.remove_id(surface_id);
445 void App::emit_activated(char const *label) {
446 this->api.send_event("activated", label);
449 void App::emit_deactivated(char const *label) {
450 this->api.send_event("deactivated", label);
453 void App::emit_syncdraw(char const *label) {
454 this->api.send_event("syncdraw", label);
457 void App::emit_flushdraw(char const *label) {
458 this->api.send_event("syncdraw", label);
461 void App::emit_visible(char const *label, bool is_visible) {
462 this->api.send_event(is_visible ? "visible" : "invisible", label);
465 result<int> App::request_surface(char const *drawing_name) {
466 auto lid = this->layers.get_layer_id(std::string(drawing_name));
468 // XXX: to we need to put these applications on the App layer?
469 return Err<int>("Drawing name does not match any role");
472 auto rname = this->lookup_id(drawing_name);
474 // name does not exist yet, allocate surface id...
475 auto id = int(this->id_alloc.generate_id(drawing_name));
476 this->layers.add_surface(id, lid.value());
478 // XXX: we set the main_surface[_name] here and now,
479 // not sure if we want this, but it worked so far.
480 if (!this->layers.main_surface_name.empty() &&
481 this->layers.main_surface_name == drawing_name) {
482 this->layers.main_surface = id;
483 logdebug("Set main_surface id to %u", id);
489 // Check currently registered drawing names if it is already there.
490 return Err<int>("Surface already present");
493 void App::activate(unsigned id) {
494 if (this->controller->sprops[id].visibility == 0) {
495 this->controller->surfaces[id]->set_visibility(1);
496 this->emit_activated(
497 this->lookup_name(id).value_or("unknown-name").c_str());
501 void App::deactivate(unsigned id) {
502 if (this->controller->sprops[id].visibility != 0) {
503 this->controller->surfaces[id]->set_visibility(0);
504 this->emit_deactivated(
505 this->lookup_name(id).value_or("unknown-name").c_str());
510 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
511 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
512 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
513 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
514 // |___/_____| |_| |_|
515 binding_api::result_type binding_api::request_surface(
516 char const *drawing_name) {
517 auto r = this->app->request_surface(drawing_name);
519 return Err<json_object *>(r.unwrap_err());
521 return Ok(json_object_new_int(r.unwrap()));
524 binding_api::result_type binding_api::activate_surface(
525 char const *drawing_name) {
526 logdebug("%s drawing_name %s", __func__, drawing_name);
527 auto r = this->app->activate_surface(drawing_name);
529 return Err<json_object *>(r);
531 return Ok(json_object_new_object());
534 binding_api::result_type binding_api::deactivate_surface(char const* drawing_name) {
535 logdebug("%s drawing_name %s", __func__, drawing_name);
536 auto r = this->app->deactivate_surface(drawing_name);
538 return Err<json_object *>(r);
540 return Ok(json_object_new_object());
543 binding_api::result_type binding_api::enddraw(char const* drawing_name) {
544 logdebug("%s drawing_name %s", __func__, drawing_name);
545 return Err<json_object*>("not implemented");
548 binding_api::result_type binding_api::list_drawing_names() {
549 logdebug("%s", __func__);
550 json j = this->app->id_alloc.name2id;
551 return Ok(json_tokener_parse(j.dump().c_str()));
554 binding_api::result_type binding_api::debug_layers() {
555 logdebug("%s", __func__);
556 return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
559 binding_api::result_type binding_api::debug_surfaces() {
560 logdebug("%s", __func__);
561 return Ok(to_json(this->app->controller->sprops));
564 binding_api::result_type binding_api::debug_status() {
565 logdebug("%s", __func__);
566 json_object *jr = json_object_new_object();
567 json_object_object_add(jr, "surfaces",
568 to_json(this->app->controller->sprops));
569 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
573 binding_api::result_type binding_api::debug_terminate() {
574 logdebug("%s", __func__);
575 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
576 // doesn't play well with perf
577 return Ok(json_object_new_object());
581 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
582 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
583 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
584 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
586 void controller_hooks::surface_created(uint32_t surface_id) {
587 this->app->surface_created(surface_id);
590 void controller_hooks::surface_removed(uint32_t surface_id) {
591 this->app->surface_removed(surface_id);