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>
46 result<json> file_to_json(char const *filename) {
47 std::ifstream i(filename);
49 return Err<json>("Could not open config file");
56 struct result<layer_map>
57 load_layer_map(char const *filename) {
58 logdebug("loading IDs from %s", filename);
60 auto j = file_to_json(filename);
62 return Err<layer_map>(j.unwrap_err());
64 json jids = j.unwrap();
66 return to_layer_map(jids);
72 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
73 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
74 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
75 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
77 App::App(wl::display *d)
87 pending_events(false) {
88 assert(g_app == nullptr);
93 auto l = load_layer_map(
94 this->config.get_string("layers.json").value().c_str());
96 this->layers = l.unwrap();
98 logerror("%s", l.err().value());
101 } catch (std::exception &e) {
102 logerror("Loading of configuration failed: %s", e.what());
106 App::~App() { g_app = nullptr; }
109 if (!this->display->ok()) {
113 if (this->layers.mapping.empty()) {
114 logerror("No surface -> layer mapping loaded");
118 this->display->add_global_handler(
119 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
120 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
123 this->display->add_global_handler(
124 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
126 std::make_unique<struct genivi::controller>(r, name, v);
128 // Init controller hooks
129 this->controller->chooks = &this->chooks;
131 // XXX: This protocol needs the output, so lets just add our mapping
133 this->controller->add_proxy_to_id_mapping(
134 this->outputs.back()->proxy.get(),
135 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
136 this->outputs.back()->proxy.get())));
139 // First level objects
140 this->display->roundtrip();
141 // Second level objects
142 this->display->roundtrip();
143 // Third level objects
144 this->display->roundtrip();
146 return init_layers();
149 int App::dispatch_events() {
150 if (this->dispatch_events() == 0) {
154 int ret = this->display->dispatch();
156 logerror("wl_display_dipatch() returned error %d",
157 this->display->get_error());
160 this->display->flush();
165 int App::dispatch_pending_events() {
166 if (this->pop_pending_events()) {
167 assert(this->pending_events == false);
168 this->display->dispatch_pending();
175 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
176 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
177 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
178 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
179 // |_____| |___/ \_\/_/
180 int App::init_layers() {
181 if (!this->controller) {
182 logerror("ivi_controller global not available");
186 if (this->outputs.empty()) {
187 logerror("no output was set up!");
191 auto &c = this->controller;
193 auto &o = this->outputs.front();
194 auto &s = c->screens.begin()->second;
195 auto &layers = c->layers;
197 // XXX: Write output dimensions to ivi controller...
198 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
206 // Quick and dirty setup of layers
207 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
208 // do any zorder arrangement).
209 for (auto const &i : this->layers.mapping) {
210 c->layer_create(i.layer_id, o->width, o->height);
211 auto &l = layers[i.layer_id];
212 l->set_destination_rectangle(0, 0, o->width, o->height);
213 l->set_visibility(1);
214 logdebug("Setting up layer %s (%d) for surfaces %d-%d and role match \"%s\"", i.name.c_str(),
215 i.layer_id, i.id_min, i.id_max, i.role.c_str());
216 this->layouts[l->id] = LayoutState{};
219 // Add layers to screen (XXX: are they sorted correctly?)
220 s->set_render_order(this->layers.layers);
224 this->display->flush();
231 // This can fix the HomeScreen...
232 void redraw_fix(App *app, std::unique_ptr<genivi::surface> &s, int x, int y,
234 { // XXX: Work around weston redraw issues
235 // trigger an update by changing the source dimensions!
236 s->set_configuration(w + 1, h);
237 s->set_source_rectangle(0, 0, w + 1, h);
238 s->set_destination_rectangle(x, y, w + 1, h);
239 app->controller->commit_changes();
240 app->display->roundtrip();
242 // wait some time, for the process to do its thing...
243 using namespace std::chrono_literals;
244 std::this_thread::sleep_for(100ms);
246 // Set a different size then what we actually want.
247 s->set_configuration(w, h);
248 s->set_source_rectangle(0, 0, w, h);
249 s->set_destination_rectangle(x, y, w, h);
250 app->controller->commit_changes();
251 app->display->roundtrip();
257 void App::surface_set_layout_full(uint32_t surface_id) {
258 if (!this->controller->surface_exists(surface_id)) {
259 logerror("Surface %d does not exist", int(surface_id));
263 auto o_layer_id = this->layers.get_layer_id(surface_id);
266 logerror("Surface %d is not associated with any layer!", int(surface_id));
270 uint32_t layer_id = o_layer_id.value();
271 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
274 auto const &layer = this->layers.get_layer(layer_id);
275 auto rect = layer.value().rect;
276 auto &s = this->controller->surfaces[surface_id];
283 // less-than-0 values refer to MAX + 1 - $VALUE
284 // e.g. MAX is either screen width or height
286 w = this->controller->output_size.w + 1 + w;
289 h = this->controller->output_size.h + 1 + h;
292 // configure surface to wxh dimensions
293 s->set_configuration(w, h);
295 // set source reactangle, even if we should not need to set it.
296 s->set_source_rectangle(0, 0, w, h);
298 // set destination to the display rectangle
299 s->set_destination_rectangle(x, y, w, h);
301 s->set_visibility(0);
304 this->controller->commit_changes();
305 this->display->roundtrip();
307 //redraw_fix(this, s, x, y, w, h);
309 logdebug("Surface %u now with rect { %d, %d, %d, %d }",
310 surface_id, x, y, w, h);
313 void App::surface_set_layout_split(uint32_t surface_id, uint32_t sub_surface_id) {
314 if (!this->controller->surface_exists(surface_id)) {
315 logerror("Surface %d does not exist", int(surface_id));
319 auto o_layer_id = this->layers.get_layer_id(surface_id);
322 logerror("Surface %d is not associated with any layer!", int(surface_id));
326 uint32_t layer_id = o_layer_id.value();
327 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
330 auto const &layer = this->layers.get_layer(layer_id);
331 auto rect = layer.value().rect;
332 auto &s = this->controller->surfaces[surface_id];
333 auto &ss = this->controller->surfaces[sub_surface_id];
340 this->state.main = surface_id;
341 this->state.sub = sub_surface_id;
342 this->state.state = LayoutState::LayoutSplit;
344 // less-than-0 values refer to MAX + 1 - $VALUE
345 // e.g. MAX is either screen width or height
347 w = this->controller->output_size.w + 1 + w;
350 h = this->controller->output_size.h + 1 + h;
356 // split along major axis
365 // configure surface to wxh dimensions
366 s->set_configuration(w, h);
367 // set source reactangle, even if we should not need to set it.
368 s->set_source_rectangle(0, 0, w, h);
369 // set destination to the display rectangle
370 s->set_destination_rectangle(x, y, w, h);
372 s->set_visibility(1);
375 // configure surface to wxh dimensions
376 ss->set_configuration(w, h);
377 // set source reactangle, even if we should not need to set it.
378 ss->set_source_rectangle(0, 0, w, h);
379 // set destination to the display rectangle
380 ss->set_destination_rectangle(x+x_off, y+y_off, w, h);
382 ss->set_visibility(1);
383 ss->set_opacity(256);
385 this->controller->commit_changes();
386 this->display->roundtrip();
388 //redraw_fix(this, s, x, y, w, h);
389 //redraw_fix(this, ss, x+x_off, y+y_off, w, h);
391 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
392 surface_id, layer_id, x, y, w, h);
395 char const *App::activate_surface(char const *drawing_name) {
399 auto oid = this->lookup_id(drawing_name);
401 surface_id = oid.value();
403 return "Surface does not exist";
407 if (!this->controller->surface_exists(surface_id)) {
408 return "Surface does not exist";
411 if (this->state.main == surface_id || this->state.sub == surface_id) {
412 return "Surface already active";
415 // This should involve a policy check, but as we do not (yet) have
416 // such a thing, we will just switch to this surface.
417 // XXX: input focus missing!!1
419 if (this->state != LayoutState{}) {
420 switch (this->state.state) {
421 case LayoutState::LayoutSingle:
422 if (this->can_split(surface_id)) {
423 this->surface_set_layout_split(this->state.main, surface_id);
428 case LayoutState::LayoutSplit:
429 if (this->can_split(surface_id)) {
430 this->deactivate(this->state.sub);
431 this->surface_set_layout_split(this->state.main, surface_id);
434 case LayoutState::LayoutNone:
439 // Make it visible, no (or little effect) if already visible
440 auto &s = this->controller->surfaces[surface_id];
442 // Set all others invisible
443 for (auto &i : this->controller->surfaces) {
444 auto &si = this->controller->sprops[i.second->id];
445 if (si.id != s->id && si.visibility != 0 &&
446 int(si.id) != this->layers.main_surface) {
447 this->deactivate(si.id);
450 this->activate(s->id);
453 this->controller->commit_changes();
454 this->display->flush();
456 this->state.main = surface_id;
457 this->state.state = LayoutState::LayoutSingle;
463 char const *App::deactivate_surface(char const *drawing_name) {
467 auto oid = this->lookup_id(drawing_name);
469 surface_id = oid.value();
471 return "Surface does not exist";
475 if (surface_id == this->layers.main_surface) {
476 return "Cannot deactivate main_surface";
479 switch (this->state.state) {
480 case LayoutState::LayoutSplit:
481 if (surface_id == this->state.main) {
482 this->deactivate(surface_id);
483 this->surface_set_layout_full(this->state.main);
485 this->deactivate(this->state.sub);
486 this->surface_set_layout_full(this->state.sub);
488 this->state.main = -1;
489 this->state.sub = -1;
490 this->state.state = LayoutState::LayoutSingle;
491 } else if (surface_id == this->state.sub) {
492 this->deactivate(surface_id);
493 this->surface_set_layout_full(this->state.sub);
494 this->surface_set_layout_full(this->state.main);
496 // XXX send syndraw events....
498 this->state.sub = -1;
499 this->state.state = LayoutState::LayoutSingle;
503 case LayoutState::LayoutSingle:
504 this->deactivate(surface_id);
505 this->state.main = -1;
506 this->state.sub = -1;
509 case LayoutState::LayoutNone:
514 this->controller->commit_changes();
515 this->display->flush();
521 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
522 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
523 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
524 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
526 void App::surface_created(uint32_t surface_id) {
527 logdebug("surface_id is %u", surface_id);
529 this->surface_set_layout_full(surface_id);
531 this->controller->layers[this->layers.get_layer_id(surface_id).value()]
532 ->add_surface(this->controller->surfaces[surface_id].get());
534 // activate the main_surface right away
535 if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
536 logdebug("Activating main_surface (%d)", surface_id);
538 this->activate_surface(
539 this->lookup_name(surface_id).value_or("unknown-name").c_str());
543 void App::surface_removed(uint32_t surface_id) {
544 logdebug("surface_id is %u", surface_id);
546 this->id_alloc.remove_id(surface_id);
549 void App::emit_activated(char const *label) {
550 this->api.send_event("activated", label);
553 void App::emit_deactivated(char const *label) {
554 this->api.send_event("deactivated", label);
557 void App::emit_syncdraw(char const *label) {
558 this->api.send_event("syncdraw", label);
561 void App::emit_flushdraw(char const *label) {
562 this->api.send_event("syncdraw", label);
565 void App::emit_visible(char const *label, bool is_visible) {
566 this->api.send_event(is_visible ? "visible" : "invisible", label);
569 void App::emit_invisible(char const *label) { return emit_visible(label, 0); }
571 void App::emit_visible(char const *label) { return emit_visible(label, 1); }
573 result<int> App::request_surface(char const *drawing_name) {
574 auto lid = this->layers.get_layer_id(std::string(drawing_name));
576 // XXX: to we need to put these applications on the App layer?
577 return Err<int>("Drawing name does not match any role");
580 auto rname = this->lookup_id(drawing_name);
582 // name does not exist yet, allocate surface id...
583 auto id = int(this->id_alloc.generate_id(drawing_name));
584 this->layers.add_surface(id, lid.value());
586 // XXX: we set the main_surface[_name] here and now,
587 // not sure if we want this, but it worked so far.
588 if (!this->layers.main_surface_name.empty() &&
589 this->layers.main_surface_name == drawing_name) {
590 this->layers.main_surface = id;
591 logdebug("Set main_surface id to %u", id);
597 // Check currently registered drawing names if it is already there.
598 return Err<int>("Surface already present");
601 void App::activate(unsigned id) {
602 if (this->controller->sprops[id].visibility == 0) {
603 this->controller->surfaces[id]->set_visibility(1);
605 this->lookup_name(id).value_or("unknown-name").c_str();
606 this->emit_activated(label);
607 this->emit_visible(label);
611 void App::deactivate(unsigned id) {
612 if (this->controller->sprops[id].visibility != 0) {
613 this->controller->surfaces[id]->set_visibility(0);
615 this->lookup_name(id).value_or("unknown-name").c_str();
616 this->emit_deactivated(label);
617 this->emit_invisible(label);
621 bool App::can_split(unsigned new_id) {
622 if (this->state.state == LayoutState::LayoutSingle) {
623 auto new_id_layer = this->layers.get_layer_id(new_id).value();
624 auto current_id_layer =
625 this->layers.get_layer_id(this->state.main).value();
627 // surfaces are on separate layers, don't bother.
628 if (new_id_layer != current_id_layer) {
632 std::string const &new_id_str = this->lookup_name(new_id).value();
633 std::string const &cur_id_str =
634 this->lookup_name(this->state.main).value();
636 auto const &layer = this->layers.get_layer(new_id_layer);
638 logdebug("layer info name: %s", layer->name.c_str());
640 if (layer->layouts.empty()) {
644 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
645 logdebug("%d main_match '%s'", new_id_layer, i->main_match.c_str());
646 auto rem = std::regex(i->main_match);
647 if (std::regex_match(cur_id_str, rem)) {
648 // build the second one only if the first already matched
649 logdebug("%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
650 auto res = std::regex(i->sub_match);
651 if (std::regex_match(new_id_str, res)) {
662 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
663 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
664 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
665 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
667 void controller_hooks::surface_created(uint32_t surface_id) {
668 this->app->surface_created(surface_id);
671 void controller_hooks::surface_removed(uint32_t surface_id) {
672 this->app->surface_removed(surface_id);