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> load_layer_map(char const *filename) {
57 logdebug("loading IDs from %s", filename);
59 auto j = file_to_json(filename);
61 return Err<layer_map>(j.unwrap_err());
63 json jids = j.unwrap();
65 return to_layer_map(jids);
71 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
72 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
73 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
74 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
76 App::App(wl::display *d)
86 pending_events(false) {
87 assert(g_app == nullptr);
92 auto l = load_layer_map(
93 this->config.get_string("layers.json").value().c_str());
95 this->layers = l.unwrap();
97 logerror("%s", l.err().value());
100 } catch (std::exception &e) {
101 logerror("Loading of configuration failed: %s", e.what());
105 App::~App() { g_app = nullptr; }
108 if (!this->display->ok()) {
112 if (this->layers.mapping.empty()) {
113 logerror("No surface -> layer mapping loaded");
117 this->display->add_global_handler(
118 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
119 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
122 this->display->add_global_handler(
123 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
125 std::make_unique<struct genivi::controller>(r, name, v);
127 // Init controller hooks
128 this->controller->chooks = &this->chooks;
130 // XXX: This protocol needs the output, so lets just add our mapping
132 this->controller->add_proxy_to_id_mapping(
133 this->outputs.back()->proxy.get(),
134 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
135 this->outputs.back()->proxy.get())));
138 // First level objects
139 this->display->roundtrip();
140 // Second level objects
141 this->display->roundtrip();
142 // Third level objects
143 this->display->roundtrip();
145 return init_layers();
148 int App::dispatch_events() {
149 if (this->dispatch_events() == 0) {
153 int ret = this->display->dispatch();
155 logerror("wl_display_dipatch() returned error %d",
156 this->display->get_error());
159 this->display->flush();
164 int App::dispatch_pending_events() {
165 if (this->pop_pending_events()) {
166 this->display->dispatch_pending();
172 bool App::pop_pending_events() {
174 return this->pending_events.compare_exchange_strong(
175 x, false, std::memory_order_consume);
178 void App::set_pending_events() {
179 this->pending_events.store(true, std::memory_order_release);
182 optional<int> App::lookup_id(char const *name) {
183 return this->id_alloc.lookup(std::string(name));
185 optional<std::string> App::lookup_name(int id) {
186 return this->id_alloc.lookup(id);
190 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
191 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
192 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
193 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
194 // |_____| |___/ \_\/_/
195 int App::init_layers() {
196 if (!this->controller) {
197 logerror("ivi_controller global not available");
201 if (this->outputs.empty()) {
202 logerror("no output was set up!");
206 auto &c = this->controller;
208 auto &o = this->outputs.front();
209 auto &s = c->screens.begin()->second;
210 auto &layers = c->layers;
212 // XXX: Write output dimensions to ivi controller...
213 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
221 // Quick and dirty setup of layers
222 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
223 // do any zorder arrangement).
224 for (auto const &i : this->layers.mapping) {
225 c->layer_create(i.layer_id, o->width, o->height);
226 auto &l = layers[i.layer_id];
227 l->set_destination_rectangle(0, 0, o->width, o->height);
228 l->set_visibility(1);
230 "Setting up layer %s (%d) for surfaces %d-%d and role match \"%s\"",
231 i.name.c_str(), i.layer_id, i.id_min, i.id_max, i.role.c_str());
232 this->layouts[l->id] = LayoutState{};
235 // Add layers to screen (XXX: are they sorted correctly?)
236 s->set_render_order(this->layers.layers);
240 this->display->flush();
247 #ifdef WE_LIKE_NOT_TO_USE_THIS
248 // This can fix the HomeScreen...
249 void redraw_fix(App *app, std::unique_ptr<genivi::surface> &s, int x, int y,
251 { // XXX: Work around weston redraw issues
252 // trigger an update by changing the source dimensions!
253 s->set_configuration(w + 1, h);
254 s->set_source_rectangle(0, 0, w + 1, h);
255 s->set_destination_rectangle(x, y, w + 1, h);
256 app->controller->commit_changes();
257 app->display->roundtrip();
259 // wait some time, for the process to do its thing...
260 using namespace std::chrono_literals;
261 std::this_thread::sleep_for(100ms);
263 // Set a different size then what we actually want.
264 s->set_configuration(w, h);
265 s->set_source_rectangle(0, 0, w, h);
266 s->set_destination_rectangle(x, y, w, h);
267 app->controller->commit_changes();
268 app->display->roundtrip();
275 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id) {
276 if (!this->controller->surface_exists(surface_id)) {
277 logerror("Surface %d does not exist", surface_id);
281 auto o_layer_id = this->layers.get_layer_id(surface_id);
284 logerror("Surface %d is not associated with any layer!", surface_id);
288 uint32_t layer_id = *o_layer_id;
290 auto const &layer = this->layers.get_layer(layer_id);
291 auto rect = layer.value().rect;
292 auto &s = this->controller->surfaces[surface_id];
299 // less-than-0 values refer to MAX + 1 - $VALUE
300 // e.g. MAX is either screen width or height
302 w = this->controller->output_size.w + 1 + w;
305 h = this->controller->output_size.h + 1 + h;
308 if (sub_surface_id) {
309 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id)) {
310 logerror("surface_set_layout: layers of surfaces (%d and %d) don't match!", surface_id, *sub_surface_id);
317 // split along major axis
326 auto &ss = this->controller->surfaces[*sub_surface_id];
328 logdebug("surface_set_layout for sub surface %u on layer %u", *sub_surface_id,
331 // configure surface to wxh dimensions
332 ss->set_configuration(w, h);
333 // set source reactangle, even if we should not need to set it.
334 ss->set_source_rectangle(0, 0, w, h);
335 // set destination to the display rectangle
336 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
339 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
342 // configure surface to wxh dimensions
343 s->set_configuration(w, h);
344 // set source reactangle, even if we should not need to set it.
345 s->set_source_rectangle(0, 0, w, h);
346 // set destination to the display rectangle
347 s->set_destination_rectangle(x, y, w, h);
349 // redraw_fix(this, s, x, y, w, h);
350 // redraw_fix(this, ss, x+x_off, y+y_off, w, h);
352 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
353 surface_id, layer_id, x, y, w, h);
356 char const *App::api_activate_surface(char const *drawing_name) {
358 auto const &surface_id = this->lookup_id(drawing_name);
361 return "Surface does not exist";
364 if (!this->controller->surface_exists(*surface_id)) {
365 return "Surface does not exist in controller!";
368 auto layer_id = this->layers.get_layer_id(*surface_id);
371 return "Surface is not on any layer!";
374 auto o_state = *this->layers.get_layout_state(*surface_id);
377 return "Could not find layer for surface";
380 struct LayoutState &state = *o_state;
382 // disable layers that are above our current layer
383 for (auto const &l : this->layers.mapping) {
384 if (l.layer_id <= *layer_id) {
389 if (l.state.main != -1) {
390 this->deactivate(l.state.main);
395 if (l.state.sub != -1) {
396 this->deactivate(l.state.sub);
401 l.state.s = LayoutState::Single;
404 this->controller->commit_changes();
405 this->display->flush();
409 if (state.main == *surface_id || state.sub == *surface_id) {
410 return "Surface already active";
413 // This should involve a policy check, but as we do not (yet) have
414 // such a thing, we will just switch to this surface.
415 // XXX: input focus missing!!1
417 if (state.main == -1) {
418 this->emit_syncdraw(drawing_name);
420 this->surface_set_layout(*surface_id);
421 this->activate(*surface_id);
422 state.main = *surface_id;
424 state.s = LayoutState::Single;
426 this->enqueue_flushdraw(state.main);
428 bool can_split = this->can_split(state, *surface_id);
430 if (state.sub == -1) {
432 if (state.main != *surface_id) {
433 std::string main = std::move(*this->lookup_name(state.main));
434 this->emit_syncdraw(drawing_name);
435 this->emit_syncdraw(main.c_str());
437 this->surface_set_layout(state.main, surface_id);
438 this->activate(*surface_id);
439 state.sub = *surface_id;
441 this->enqueue_flushdraw(state.main);
442 this->enqueue_flushdraw(state.sub);
445 this->emit_syncdraw(drawing_name);
447 this->surface_set_layout(*surface_id);
448 this->deactivate(state.main);
449 this->activate(*surface_id);
450 this->deactivate(state.sub);
451 state.main = *surface_id;
453 state.s = LayoutState::Single;
455 this->enqueue_flushdraw(state.main);
461 this->controller->commit_changes();
462 this->display->flush();
468 char const *App::api_deactivate_surface(char const *drawing_name) {
470 auto const &surface_id = this->lookup_id(drawing_name);
473 return "Surface does not exist";
476 if (*surface_id == this->layers.main_surface) {
477 return "Cannot deactivate main_surface";
480 auto o_state = *this->layers.get_layout_state(*surface_id);
483 return "Could not find layer for surface";
486 struct LayoutState &state = *o_state;
488 if (state.main == -1) {
489 return "No surface active";
492 if (*surface_id == this->layers.main_surface) {
493 logdebug("Refusing to deactivate main_surface %d", *surface_id);
497 if (state.main == *surface_id) {
498 if (state.sub != -1) {
499 std::string sub = std::move(*this->lookup_name(state.sub));
500 this->emit_syncdraw(sub.c_str());
502 this->deactivate(*surface_id);
503 this->surface_set_layout(state.sub);
504 state.main = state.sub;
506 state.s = LayoutState::Single;
508 this->enqueue_flushdraw(state.sub);
510 this->deactivate(*surface_id);
513 } else if (state.sub == *surface_id) {
514 std::string main = std::move(*this->lookup_name(state.main));
515 this->emit_syncdraw(main.c_str());
517 this->deactivate(*surface_id);
518 this->deactivate(*surface_id);
519 this->surface_set_layout(state.main);
521 state.s = LayoutState::Single;
523 this->enqueue_flushdraw(state.main);
525 return "Surface is not active";
528 this->controller->commit_changes();
529 this->display->flush();
534 void App::enqueue_flushdraw(int surface_id) {
535 this->check_flushdraw(surface_id);
536 logdebug("Enqueuing EndDraw for surface_id %d", surface_id);
537 this->pending_end_draw.push_back(surface_id);
540 void App::check_flushdraw(int surface_id) {
541 auto i = std::find(std::begin(this->pending_end_draw),
542 std::end(this->pending_end_draw), surface_id);
543 if (i != std::end(this->pending_end_draw)) {
544 auto n = this->lookup_name(surface_id);
545 logerror("Application %s (%d) has pending EndDraw call(s)!",
546 n ? n->c_str() : "unknown-name", surface_id);
547 std::swap(this->pending_end_draw[std::distance(
548 std::begin(this->pending_end_draw), i)],
549 this->pending_end_draw.back());
550 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
554 char const *App::api_enddraw(char const *drawing_name) {
555 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
556 auto n = this->lookup_name(this->pending_end_draw[i]);
557 if (n && *n == drawing_name) {
558 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
559 this->pending_end_draw.resize(iend - 1);
560 // XXX: Please tell the compositor to thaw the surface...
561 this->emit_flushdraw(drawing_name);
565 return "No EndDraw pending for surface";
568 void App::api_ping() {
569 this->dispatch_pending_events();
573 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
574 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
575 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
576 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
578 void App::surface_created(uint32_t surface_id) {
579 auto layer_id = this->layers.get_layer_id(surface_id);
581 logdebug("Newly created surfce %d is not associated with any layer!",
586 logdebug("surface_id is %u, layer_id is %u", surface_id, *layer_id);
588 this->controller->layers[*layer_id]->add_surface(
589 this->controller->surfaces[surface_id].get());
591 // activate the main_surface right away
592 if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
593 logdebug("Activating main_surface (%d)", surface_id);
595 this->api_activate_surface(
596 this->lookup_name(surface_id).value_or("unknown-name").c_str());
600 void App::surface_removed(uint32_t surface_id) {
601 logdebug("surface_id is %u", surface_id);
603 auto drawing_name = this->lookup_name(surface_id);
605 this->api_deactivate_surface(drawing_name->c_str());
608 this->id_alloc.remove_id(surface_id);
611 void App::emit_activated(char const *label) {
612 this->api.send_event("activated", label);
615 void App::emit_deactivated(char const *label) {
616 this->api.send_event("deactivated", label);
619 void App::emit_syncdraw(char const *label) {
620 this->api.send_event("syncdraw", label);
623 void App::emit_flushdraw(char const *label) {
624 this->api.send_event("flushdraw", label);
627 void App::emit_visible(char const *label, bool is_visible) {
628 this->api.send_event(is_visible ? "visible" : "invisible", label);
631 void App::emit_invisible(char const *label) {
632 return emit_visible(label, false);
635 void App::emit_visible(char const *label) { return emit_visible(label, true); }
637 result<int> App::api_request_surface(char const *drawing_name) {
638 auto lid = this->layers.get_layer_id(std::string(drawing_name));
640 // XXX: to we need to put these applications on the App layer?
641 return Err<int>("Drawing name does not match any role");
644 auto rname = this->lookup_id(drawing_name);
646 // name does not exist yet, allocate surface id...
647 auto id = int(this->id_alloc.generate_id(drawing_name));
648 this->layers.add_surface(id, *lid);
650 // XXX: we set the main_surface[_name] here and now,
651 // not sure if we want this, but it worked so far.
652 if (!this->layers.main_surface_name.empty() &&
653 this->layers.main_surface_name == drawing_name) {
654 this->layers.main_surface = id;
655 logdebug("Set main_surface id to %u", id);
661 // Check currently registered drawing names if it is already there.
662 return Err<int>("Surface already present");
665 void App::activate(int id) {
666 if (this->controller->sprops[id].visibility == 0) {
667 this->controller->surfaces[id]->set_visibility(1);
669 this->lookup_name(id).value_or("unknown-name").c_str();
670 this->emit_activated(label);
671 this->emit_visible(label);
675 void App::deactivate(int id) {
676 if (this->controller->sprops[id].visibility != 0) {
677 this->controller->surfaces[id]->set_visibility(0);
679 this->lookup_name(id).value_or("unknown-name").c_str();
680 this->emit_deactivated(label);
681 this->emit_invisible(label);
685 bool App::can_split(struct LayoutState const &state, int new_id) {
686 if (state.main != -1 && state.main != new_id) {
687 auto new_id_layer = this->layers.get_layer_id(new_id).value();
688 auto current_id_layer = this->layers.get_layer_id(state.main).value();
690 // surfaces are on separate layers, don't bother.
691 if (new_id_layer != current_id_layer) {
695 std::string const &new_id_str = this->lookup_name(new_id).value();
696 std::string const &cur_id_str = this->lookup_name(state.main).value();
698 auto const &layer = this->layers.get_layer(new_id_layer);
700 logdebug("layer info name: %s", layer->name.c_str());
702 if (layer->layouts.empty()) {
706 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
707 logdebug("%d main_match '%s'", new_id_layer, i->main_match.c_str());
708 auto rem = std::regex(i->main_match);
709 if (std::regex_match(cur_id_str, rem)) {
710 // build the second one only if the first already matched
711 logdebug("%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
712 auto res = std::regex(i->sub_match);
713 if (std::regex_match(new_id_str, res)) {
714 logdebug("layout matched!");
725 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
726 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
727 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
728 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
730 void controller_hooks::surface_created(uint32_t surface_id) {
731 this->app->surface_created(surface_id);
734 void controller_hooks::surface_removed(uint32_t surface_id) {
735 this->app->surface_removed(surface_id);