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 this->display->dispatch_pending();
174 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
175 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
176 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
177 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
178 // |_____| |___/ \_\/_/
179 int App::init_layers() {
180 if (!this->controller) {
181 logerror("ivi_controller global not available");
185 if (this->outputs.empty()) {
186 logerror("no output was set up!");
190 auto &c = this->controller;
192 auto &o = this->outputs.front();
193 auto &s = c->screens.begin()->second;
194 auto &layers = c->layers;
196 // XXX: Write output dimensions to ivi controller...
197 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
205 // Quick and dirty setup of layers
206 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
207 // do any zorder arrangement).
208 for (auto const &i : this->layers.mapping) {
209 c->layer_create(i.layer_id, o->width, o->height);
210 auto &l = layers[i.layer_id];
211 l->set_destination_rectangle(0, 0, o->width, o->height);
212 l->set_visibility(1);
213 logdebug("Setting up layer %s (%d) for surfaces %d-%d and role match \"%s\"", i.name.c_str(),
214 i.layer_id, i.id_min, i.id_max, i.role.c_str());
215 this->layouts[l->id] = LayoutState{};
218 // Add layers to screen (XXX: are they sorted correctly?)
219 s->set_render_order(this->layers.layers);
223 this->display->flush();
230 #ifdef WE_LIKE_NOT_TO_USE_THIS
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();
258 void App::surface_set_layout_full(uint32_t surface_id) {
259 if (!this->controller->surface_exists(surface_id)) {
260 logerror("Surface %d does not exist", int(surface_id));
264 auto o_layer_id = this->layers.get_layer_id(surface_id);
267 logerror("Surface %d is not associated with any layer!", int(surface_id));
271 uint32_t layer_id = o_layer_id.value();
272 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
275 auto const &layer = this->layers.get_layer(layer_id);
276 auto rect = layer.value().rect;
277 auto &s = this->controller->surfaces[surface_id];
284 // less-than-0 values refer to MAX + 1 - $VALUE
285 // e.g. MAX is either screen width or height
287 w = this->controller->output_size.w + 1 + w;
290 h = this->controller->output_size.h + 1 + h;
293 // configure surface to wxh dimensions
294 s->set_configuration(w, h);
296 // set source reactangle, even if we should not need to set it.
297 s->set_source_rectangle(0, 0, w, h);
299 // set destination to the display rectangle
300 s->set_destination_rectangle(x, y, w, h);
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 // less-than-0 values refer to MAX + 1 - $VALUE
336 // e.g. MAX is either screen width or height
338 w = this->controller->output_size.w + 1 + w;
341 h = this->controller->output_size.h + 1 + h;
347 // split along major axis
356 // configure surface to wxh dimensions
357 s->set_configuration(w, h);
358 // set source reactangle, even if we should not need to set it.
359 s->set_source_rectangle(0, 0, w, h);
360 // set destination to the display rectangle
361 s->set_destination_rectangle(x, y, w, h);
363 // configure surface to wxh dimensions
364 ss->set_configuration(w, h);
365 // set source reactangle, even if we should not need to set it.
366 ss->set_source_rectangle(0, 0, w, h);
367 // set destination to the display rectangle
368 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
370 // redraw_fix(this, s, x, y, w, h);
371 // redraw_fix(this, ss, x+x_off, y+y_off, w, h);
373 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
374 surface_id, layer_id, x, y, w, h);
377 char const *App::api_activate_surface(char const *drawing_name) {
379 auto const &surface_id = this->lookup_id(drawing_name);
382 return "Surface does not exist";
385 if (!this->controller->surface_exists(*surface_id)) {
386 return "Surface does not exist in controller!";
389 auto layer_id = this->layers.get_layer_id(*surface_id);
392 return "Surface is not on any layer!";
395 auto o_state = *this->layers.get_layout_state(*surface_id);
398 return "Could not find layer for surface";
401 struct LayoutState &state = *o_state;
403 // disable layers that are above our current layer
404 for (auto const &l : this->layers.mapping) {
405 if (l.layer_id <= *layer_id) {
410 if (l.state.main != -1) {
411 this->deactivate(l.state.main);
416 if (l.state.sub != -1) {
417 this->deactivate(l.state.sub);
422 l.state.s = LayoutState::Single;
425 this->controller->commit_changes();
426 this->display->flush();
430 if (state.main == *surface_id || state.sub == *surface_id) {
431 return "Surface already active";
434 // This should involve a policy check, but as we do not (yet) have
435 // such a thing, we will just switch to this surface.
436 // XXX: input focus missing!!1
438 if (state.main == -1) {
439 this->emit_syncdraw(drawing_name);
441 this->surface_set_layout_full(*surface_id);
442 this->activate(*surface_id);
443 state.main = *surface_id;
445 state.s = LayoutState::Single;
447 //////this->emit_flushdraw(drawing_name);
448 this->pending_end_draw.push_back(state.main);
450 bool can_split = this->can_split(state, *surface_id);
452 if (state.sub == -1) {
454 if (state.main != *surface_id) {
455 std::string main = std::move(*this->lookup_name(state.main));
456 this->emit_syncdraw(drawing_name);
457 this->emit_syncdraw(main.c_str());
459 this->surface_set_layout_split(state.main, *surface_id);
460 this->activate(*surface_id);
461 state.sub = *surface_id;
463 this->pending_end_draw.push_back(state.main);
464 this->pending_end_draw.push_back(state.sub);
465 // Should wait for EndDraw event...
466 //////this->emit_flushdraw(drawing_name);
467 //////this->emit_flushdraw(main.c_str());
470 this->emit_syncdraw(drawing_name);
472 this->surface_set_layout_full(*surface_id);
473 this->deactivate(state.main);
474 this->activate(*surface_id);
475 this->deactivate(state.sub);
476 state.main = *surface_id;
478 state.s = LayoutState::Single;
480 //////this->emit_flushdraw(drawing_name);
481 this->pending_end_draw.push_back(state.main);
487 this->controller->commit_changes();
488 this->display->flush();
494 char const *App::api_deactivate_surface(char const *drawing_name) {
496 auto const &surface_id = this->lookup_id(drawing_name);
499 return "Surface does not exist";
502 if (*surface_id == this->layers.main_surface) {
503 return "Cannot deactivate main_surface";
506 auto o_state = *this->layers.get_layout_state(*surface_id);
509 return "Could not find layer for surface";
512 struct LayoutState &state = *o_state;
514 if (state.main == -1) {
515 return "No surface active";
518 if (*surface_id == this->layers.main_surface) {
519 logdebug("Refusing to deactivate main_surface %d", *surface_id);
523 if (state.main == *surface_id) {
524 if (state.sub != -1) {
525 std::string sub = std::move(*this->lookup_name(state.sub));
526 this->emit_syncdraw(sub.c_str());
528 this->deactivate(*surface_id);
529 this->surface_set_layout_full(state.sub);
530 state.main = state.sub;
532 state.s = LayoutState::Single;
534 //////this->emit_flushdraw(sub.c_str());
535 this->pending_end_draw.push_back(state.sub);
537 this->deactivate(*surface_id);
540 }else if (state.sub == *surface_id) {
541 std::string main = std::move(*this->lookup_name(state.main));
542 this->emit_syncdraw(main.c_str());
544 this->deactivate(*surface_id);
545 this->deactivate(*surface_id);
546 this->surface_set_layout_full(state.main);
548 state.s = LayoutState::Single;
550 //////this->emit_flushdraw(main.c_str());
551 this->pending_end_draw.push_back(state.main);
553 return "Surface is not active";
556 this->controller->commit_changes();
557 this->display->flush();
562 char const *App::api_enddraw(char const *drawing_name) {
563 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
564 auto n = this->lookup_name(this->pending_end_draw[i]);
565 if (n && *n == drawing_name) {
566 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
567 this->pending_end_draw.resize(iend - 1);
568 // XXX: Please tell the compositor to thaw the surface...
569 this->emit_flushdraw(drawing_name);
573 return "No EndDraw pending for surface";
577 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
578 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
579 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
580 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
582 void App::surface_created(uint32_t surface_id) {
583 auto layer_id = this->layers.get_layer_id(surface_id);
585 logdebug("Newly created surfce %d is not associated with any layer!", surface_id);
589 logdebug("surface_id is %u, layer_id is %u", surface_id, *layer_id);
591 this->controller->layers[*layer_id]
592 ->add_surface(this->controller->surfaces[surface_id].get());
594 // activate the main_surface right away
595 if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
596 logdebug("Activating main_surface (%d)", surface_id);
598 this->api_activate_surface(
599 this->lookup_name(surface_id).value_or("unknown-name").c_str());
603 void App::surface_removed(uint32_t surface_id) {
604 logdebug("surface_id is %u", surface_id);
606 auto drawing_name = this->lookup_name(surface_id);
608 this->api_deactivate_surface(drawing_name->c_str());
611 this->id_alloc.remove_id(surface_id);
614 void App::emit_activated(char const *label) {
615 this->api.send_event("activated", label);
618 void App::emit_deactivated(char const *label) {
619 this->api.send_event("deactivated", label);
622 void App::emit_syncdraw(char const *label) {
623 this->api.send_event("syncdraw", label);
626 void App::emit_flushdraw(char const *label) {
627 this->api.send_event("flushdraw", label);
630 void App::emit_visible(char const *label, bool is_visible) {
631 this->api.send_event(is_visible ? "visible" : "invisible", label);
634 void App::emit_invisible(char const *label) {
635 return emit_visible(label, false);
638 void App::emit_visible(char const *label) { return emit_visible(label, true); }
640 result<int> App::api_request_surface(char const *drawing_name) {
641 auto lid = this->layers.get_layer_id(std::string(drawing_name));
643 // XXX: to we need to put these applications on the App layer?
644 return Err<int>("Drawing name does not match any role");
647 auto rname = this->lookup_id(drawing_name);
649 // name does not exist yet, allocate surface id...
650 auto id = int(this->id_alloc.generate_id(drawing_name));
651 this->layers.add_surface(id, *lid);
653 // XXX: we set the main_surface[_name] here and now,
654 // not sure if we want this, but it worked so far.
655 if (!this->layers.main_surface_name.empty() &&
656 this->layers.main_surface_name == drawing_name) {
657 this->layers.main_surface = id;
658 logdebug("Set main_surface id to %u", id);
664 // Check currently registered drawing names if it is already there.
665 return Err<int>("Surface already present");
668 void App::activate(int id) {
669 if (this->controller->sprops[id].visibility == 0) {
670 this->controller->surfaces[id]->set_visibility(1);
672 this->lookup_name(id).value_or("unknown-name").c_str();
673 this->emit_activated(label);
674 this->emit_visible(label);
678 void App::deactivate(int id) {
679 if (this->controller->sprops[id].visibility != 0) {
680 this->controller->surfaces[id]->set_visibility(0);
682 this->lookup_name(id).value_or("unknown-name").c_str();
683 this->emit_deactivated(label);
684 this->emit_invisible(label);
688 bool App::can_split(struct LayoutState const &state, int new_id) {
689 if (state.main != -1 && state.main != new_id) {
690 auto new_id_layer = this->layers.get_layer_id(new_id).value();
691 auto current_id_layer =
692 this->layers.get_layer_id(state.main).value();
694 // surfaces are on separate layers, don't bother.
695 if (new_id_layer != current_id_layer) {
699 std::string const &new_id_str = this->lookup_name(new_id).value();
700 std::string const &cur_id_str =
701 this->lookup_name(state.main).value();
703 auto const &layer = this->layers.get_layer(new_id_layer);
705 logdebug("layer info name: %s", layer->name.c_str());
707 if (layer->layouts.empty()) {
711 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
712 logdebug("%d main_match '%s'", new_id_layer, i->main_match.c_str());
713 auto rem = std::regex(i->main_match);
714 if (std::regex_match(cur_id_str, rem)) {
715 // build the second one only if the first already matched
716 logdebug("%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
717 auto res = std::regex(i->sub_match);
718 if (std::regex_match(new_id_str, res)) {
719 logdebug("layout matched!");
730 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
731 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
732 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
733 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
735 void controller_hooks::surface_created(uint32_t surface_id) {
736 this->app->surface_created(surface_id);
739 void controller_hooks::surface_removed(uint32_t surface_id) {
740 this->app->surface_removed(surface_id);