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->pending_events.load(std::memory_order_consume)) {
151 this->pending_events.store(false, std::memory_order_release);
152 return this->display->dispatch_pending();
155 int ret = this->display->dispatch();
157 logerror("wl_display_dipatch() returned error %d",
158 this->display->get_error());
161 this->display->flush();
163 // execute pending tasks, that is layout changes etc.
164 // this->execute_pending();
170 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
171 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
172 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
173 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
174 // |_____| |___/ \_\/_/
175 int App::init_layers() {
176 if (!this->controller) {
177 logerror("ivi_controller global not available");
181 if (this->outputs.empty()) {
182 logerror("no output was set up!");
186 auto &c = this->controller;
188 auto &o = this->outputs.front();
189 auto &s = c->screens.begin()->second;
190 auto &layers = c->layers;
192 // XXX: Write output dimensions to ivi controller...
193 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
201 // Quick and dirty setup of layers
202 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
203 // do any zorder arrangement).
204 for (auto const &i : this->layers.mapping) {
205 c->layer_create(i.layer_id, o->width, o->height);
206 auto &l = layers[i.layer_id];
207 l->set_destination_rectangle(0, 0, o->width, o->height);
208 l->set_visibility(1);
209 logdebug("Setting up layer %s (%d) for surfaces %d-%d and role match \"%s\"", i.name.c_str(),
210 i.layer_id, i.id_min, i.id_max, i.role.c_str());
211 this->layouts[l->id] = LayoutState{};
214 // Add layers to screen (XXX: are they sorted correctly?)
215 s->set_render_order(this->layers.layers);
219 this->display->flush();
226 // This can fix the HomeScreen...
227 void redraw_fix(App *app, std::unique_ptr<genivi::surface> &s, int x, int y,
229 { // XXX: Work around weston redraw issues
230 // trigger an update by changing the source dimensions!
231 s->set_configuration(w + 1, h);
232 s->set_source_rectangle(0, 0, w + 1, h);
233 s->set_destination_rectangle(x, y, w + 1, h);
234 app->controller->commit_changes();
235 app->display->roundtrip();
237 // wait some time, for the process to do its thing...
238 using namespace std::chrono_literals;
239 std::this_thread::sleep_for(100ms);
241 // Set a different size then what we actually want.
242 s->set_configuration(w, h);
243 s->set_source_rectangle(0, 0, w, h);
244 s->set_destination_rectangle(x, y, w, h);
245 app->controller->commit_changes();
246 app->display->roundtrip();
252 void App::surface_set_layout_full(uint32_t surface_id) {
253 if (!this->controller->surface_exists(surface_id)) {
254 logerror("Surface %d does not exist", int(surface_id));
258 auto o_layer_id = this->layers.get_layer_id(surface_id);
261 logerror("Surface %d is not associated with any layer!", int(surface_id));
265 uint32_t layer_id = o_layer_id.value();
266 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
269 auto const &layer = this->layers.get_layer(layer_id);
270 auto rect = layer.value().rect;
271 auto &s = this->controller->surfaces[surface_id];
278 // less-than-0 values refer to MAX + 1 - $VALUE
279 // e.g. MAX is either screen width or height
281 w = this->controller->output_size.w + 1 + w;
284 h = this->controller->output_size.h + 1 + h;
287 // configure surface to wxh dimensions
288 s->set_configuration(w, h);
290 // set source reactangle, even if we should not need to set it.
291 s->set_source_rectangle(0, 0, w, h);
293 // set destination to the display rectangle
294 s->set_destination_rectangle(x, y, w, h);
296 s->set_visibility(0);
299 this->controller->commit_changes();
300 this->display->roundtrip();
302 //redraw_fix(this, s, x, y, w, h);
304 logdebug("Surface %u now with rect { %d, %d, %d, %d }",
305 surface_id, x, y, w, h);
308 void App::surface_set_layout_split(uint32_t surface_id, uint32_t sub_surface_id) {
309 if (!this->controller->surface_exists(surface_id)) {
310 logerror("Surface %d does not exist", int(surface_id));
314 auto o_layer_id = this->layers.get_layer_id(surface_id);
317 logerror("Surface %d is not associated with any layer!", int(surface_id));
321 uint32_t layer_id = o_layer_id.value();
322 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
325 auto const &layer = this->layers.get_layer(layer_id);
326 auto rect = layer.value().rect;
327 auto &s = this->controller->surfaces[surface_id];
328 auto &ss = this->controller->surfaces[sub_surface_id];
335 this->state.main = surface_id;
336 this->state.sub = sub_surface_id;
337 this->state.state = LayoutState::LayoutSplit;
339 // less-than-0 values refer to MAX + 1 - $VALUE
340 // e.g. MAX is either screen width or height
342 w = this->controller->output_size.w + 1 + w;
345 h = this->controller->output_size.h + 1 + h;
351 // split along major axis
360 // configure surface to wxh dimensions
361 s->set_configuration(w, h);
362 // set source reactangle, even if we should not need to set it.
363 s->set_source_rectangle(0, 0, w, h);
364 // set destination to the display rectangle
365 s->set_destination_rectangle(x, y, w, h);
367 s->set_visibility(1);
370 // configure surface to wxh dimensions
371 ss->set_configuration(w, h);
372 // set source reactangle, even if we should not need to set it.
373 ss->set_source_rectangle(0, 0, w, h);
374 // set destination to the display rectangle
375 ss->set_destination_rectangle(x+x_off, y+y_off, w, h);
377 ss->set_visibility(1);
378 ss->set_opacity(256);
380 this->controller->commit_changes();
381 this->display->roundtrip();
383 //redraw_fix(this, s, x, y, w, h);
384 //redraw_fix(this, ss, x+x_off, y+y_off, w, h);
386 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
387 surface_id, layer_id, x, y, w, h);
390 char const *App::activate_surface(char const *drawing_name) {
394 auto oid = this->lookup_id(drawing_name);
396 surface_id = oid.value();
398 return "Surface does not exist";
402 if (!this->controller->surface_exists(surface_id)) {
403 return "Surface does not exist";
406 if (this->state.main == surface_id || this->state.sub == surface_id) {
407 return "Surface already active";
410 // This should involve a policy check, but as we do not (yet) have
411 // such a thing, we will just switch to this surface.
412 // XXX: input focus missing!!1
414 if (this->state != LayoutState{}) {
415 switch (this->state.state) {
416 case LayoutState::LayoutSingle:
417 if (this->can_split(surface_id)) {
418 this->surface_set_layout_split(this->state.main, surface_id);
423 case LayoutState::LayoutSplit:
424 if (this->can_split(surface_id)) {
425 this->deactivate(this->state.sub);
426 this->surface_set_layout_split(this->state.main, surface_id);
429 case LayoutState::LayoutNone:
434 // Make it visible, no (or little effect) if already visible
435 auto &s = this->controller->surfaces[surface_id];
437 // Set all others invisible
438 for (auto &i : this->controller->surfaces) {
439 auto &si = this->controller->sprops[i.second->id];
440 if (si.id != s->id && si.visibility != 0 &&
441 int(si.id) != this->layers.main_surface) {
442 this->deactivate(si.id);
445 this->activate(s->id);
448 this->controller->commit_changes();
449 this->display->flush();
451 this->state.main = surface_id;
452 this->state.state = LayoutState::LayoutSingle;
458 char const *App::deactivate_surface(char const *drawing_name) {
462 auto oid = this->lookup_id(drawing_name);
464 surface_id = oid.value();
466 return "Surface does not exist";
470 if (surface_id == this->layers.main_surface) {
471 return "Cannot deactivate main_surface";
474 switch (this->state.state) {
475 case LayoutState::LayoutSplit:
476 if (surface_id == this->state.main) {
477 this->deactivate(surface_id);
478 this->surface_set_layout_full(this->state.main);
480 this->deactivate(this->state.sub);
481 this->surface_set_layout_full(this->state.sub);
483 this->state.main = -1;
484 this->state.sub = -1;
485 this->state.state = LayoutState::LayoutSingle;
486 } else if (surface_id == this->state.sub) {
487 this->deactivate(surface_id);
488 this->surface_set_layout_full(this->state.sub);
489 this->surface_set_layout_full(this->state.main);
491 // XXX send syndraw events....
493 this->state.sub = -1;
494 this->state.state = LayoutState::LayoutSingle;
498 case LayoutState::LayoutSingle:
499 this->deactivate(surface_id);
500 this->state.main = -1;
501 this->state.sub = -1;
504 case LayoutState::LayoutNone:
509 this->controller->commit_changes();
510 this->display->flush();
516 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
517 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
518 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
519 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
521 void App::surface_created(uint32_t surface_id) {
522 logdebug("surface_id is %u", surface_id);
524 this->surface_set_layout_full(surface_id);
526 this->controller->layers[this->layers.get_layer_id(surface_id).value()]
527 ->add_surface(this->controller->surfaces[surface_id].get());
529 // activate the main_surface right away
530 if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
531 logdebug("Activating main_surface (%d)", surface_id);
533 this->activate_surface(
534 this->lookup_name(surface_id).value_or("unknown-name").c_str());
538 void App::surface_removed(uint32_t surface_id) {
539 logdebug("surface_id is %u", surface_id);
541 this->id_alloc.remove_id(surface_id);
544 void App::emit_activated(char const *label) {
545 this->api.send_event("activated", label);
548 void App::emit_deactivated(char const *label) {
549 this->api.send_event("deactivated", label);
552 void App::emit_syncdraw(char const *label) {
553 this->api.send_event("syncdraw", label);
556 void App::emit_flushdraw(char const *label) {
557 this->api.send_event("syncdraw", label);
560 void App::emit_visible(char const *label, bool is_visible) {
561 this->api.send_event(is_visible ? "visible" : "invisible", label);
564 void App::emit_invisible(char const *label) { return emit_visible(label, 0); }
566 void App::emit_visible(char const *label) { return emit_visible(label, 1); }
568 result<int> App::request_surface(char const *drawing_name) {
569 auto lid = this->layers.get_layer_id(std::string(drawing_name));
571 // XXX: to we need to put these applications on the App layer?
572 return Err<int>("Drawing name does not match any role");
575 auto rname = this->lookup_id(drawing_name);
577 // name does not exist yet, allocate surface id...
578 auto id = int(this->id_alloc.generate_id(drawing_name));
579 this->layers.add_surface(id, lid.value());
581 // XXX: we set the main_surface[_name] here and now,
582 // not sure if we want this, but it worked so far.
583 if (!this->layers.main_surface_name.empty() &&
584 this->layers.main_surface_name == drawing_name) {
585 this->layers.main_surface = id;
586 logdebug("Set main_surface id to %u", id);
592 // Check currently registered drawing names if it is already there.
593 return Err<int>("Surface already present");
596 void App::activate(unsigned id) {
597 if (this->controller->sprops[id].visibility == 0) {
598 this->controller->surfaces[id]->set_visibility(1);
600 this->lookup_name(id).value_or("unknown-name").c_str();
601 this->emit_activated(label);
602 this->emit_visible(label);
606 void App::deactivate(unsigned id) {
607 if (this->controller->sprops[id].visibility != 0) {
608 this->controller->surfaces[id]->set_visibility(0);
610 this->lookup_name(id).value_or("unknown-name").c_str();
611 this->emit_deactivated(label);
612 this->emit_invisible(label);
616 bool App::can_split(unsigned new_id) {
617 if (this->state.state == LayoutState::LayoutSingle) {
618 auto new_id_layer = this->layers.get_layer_id(new_id).value();
619 auto current_id_layer =
620 this->layers.get_layer_id(this->state.main).value();
622 // surfaces are on separate layers, don't bother.
623 if (new_id_layer != current_id_layer) {
627 std::string const &new_id_str = this->lookup_name(new_id).value();
628 std::string const &cur_id_str =
629 this->lookup_name(this->state.main).value();
631 auto const &layer = this->layers.get_layer(new_id_layer);
633 logdebug("layer info name: %s", layer->name.c_str());
635 if (layer->layouts.empty()) {
639 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
640 logdebug("%d main_match '%s'", new_id_layer, i->main_match.c_str());
641 auto rem = std::regex(i->main_match);
642 if (std::regex_match(cur_id_str, rem)) {
643 // build the second one only if the first already matched
644 logdebug("%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
645 auto res = std::regex(i->sub_match);
646 if (std::regex_match(new_id_str, res)) {
657 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
658 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
659 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
660 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
662 void controller_hooks::surface_created(uint32_t surface_id) {
663 this->app->surface_created(surface_id);
666 void controller_hooks::surface_removed(uint32_t surface_id) {
667 this->app->surface_removed(surface_id);