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>
45 result<json> file_to_json(char const *filename) {
46 std::ifstream i(filename);
48 return Err<json>("Could not open config file");
55 struct result<layer_map> load_layer_map(char const *filename) {
56 logdebug("loading IDs from %s", filename);
58 auto j = file_to_json(filename);
60 return Err<layer_map>(j.unwrap_err());
62 json jids = j.unwrap();
64 return to_layer_map(jids);
70 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
71 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
72 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
73 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
75 App::App(wl::display *d)
84 pending_events(false),
88 auto l = load_layer_map(
89 this->config.get_string("layers.json").value().c_str());
91 this->layers = l.unwrap();
93 logerror("%s", l.err().value());
96 } catch (std::exception &e) {
97 logerror("Loading of configuration failed: %s", e.what());
102 if (!this->display->ok()) {
106 if (this->layers.mapping.empty()) {
107 logerror("No surface -> layer mapping loaded");
111 this->display->add_global_handler(
112 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
113 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
116 this->display->add_global_handler(
117 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
119 std::make_unique<struct genivi::controller>(r, name, v);
121 // Init controller hooks
122 this->controller->chooks = &this->chooks;
124 // XXX: This protocol needs the output, so lets just add our mapping
126 this->controller->add_proxy_to_id_mapping(
127 this->outputs.back()->proxy.get(),
128 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
129 this->outputs.back()->proxy.get())));
132 // First level objects
133 this->display->roundtrip();
134 // Second level objects
135 this->display->roundtrip();
136 // Third level objects
137 this->display->roundtrip();
139 return init_layers();
142 int App::dispatch_events() {
143 if (this->dispatch_events() == 0) {
147 int ret = this->display->dispatch();
149 logerror("wl_display_dipatch() returned error %d",
150 this->display->get_error());
153 this->display->flush();
158 int App::dispatch_pending_events() {
159 if (this->pop_pending_events()) {
160 this->display->dispatch_pending();
166 bool App::pop_pending_events() {
168 return this->pending_events.compare_exchange_strong(
169 x, false, std::memory_order_consume);
172 void App::set_pending_events() {
173 this->pending_events.store(true, std::memory_order_release);
176 optional<int> App::lookup_id(char const *name) {
177 return this->id_alloc.lookup(std::string(name));
179 optional<std::string> App::lookup_name(int id) {
180 return this->id_alloc.lookup(id);
184 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
185 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
186 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
187 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
188 // |_____| |___/ \_\/_/
189 int App::init_layers() {
190 if (!this->controller) {
191 logerror("ivi_controller global not available");
195 if (this->outputs.empty()) {
196 logerror("no output was set up!");
200 auto &c = this->controller;
202 auto &o = this->outputs.front();
203 auto &s = c->screens.begin()->second;
204 auto &layers = c->layers;
206 // XXX: Write output dimensions to ivi controller...
207 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
215 // Quick and dirty setup of layers
216 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
217 // do any zorder arrangement).
218 for (auto const &i : this->layers.mapping) {
219 c->layer_create(i.second.layer_id, o->width, o->height);
220 auto &l = layers[i.second.layer_id];
221 l->set_destination_rectangle(0, 0, o->width, o->height);
222 l->set_visibility(1);
223 logdebug("Setting up layer %s (%d) for surface role match \"%s\"",
224 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
227 // Add layers to screen (XXX: are they sorted correctly?)
228 s->set_render_order(this->layers.layers);
230 this->layout_commit();
235 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id) {
236 if (!this->controller->surface_exists(surface_id)) {
237 logerror("Surface %d does not exist", surface_id);
241 auto o_layer_id = this->layers.get_layer_id(surface_id);
244 logerror("Surface %d is not associated with any layer!", surface_id);
248 uint32_t layer_id = *o_layer_id;
250 auto const &layer = this->layers.get_layer(layer_id);
251 auto rect = layer.value().rect;
252 auto &s = this->controller->surfaces[surface_id];
259 // less-than-0 values refer to MAX + 1 - $VALUE
260 // e.g. MAX is either screen width or height
262 w = this->controller->output_size.w + 1 + w;
265 h = this->controller->output_size.h + 1 + h;
268 if (sub_surface_id) {
269 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id)) {
271 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
272 surface_id, *sub_surface_id);
279 // split along major axis
288 auto &ss = this->controller->surfaces[*sub_surface_id];
290 logdebug("surface_set_layout for sub surface %u on layer %u",
291 *sub_surface_id, layer_id);
293 // configure surface to wxh dimensions
294 ss->set_configuration(w, h);
295 // set source reactangle, even if we should not need to set it.
296 ss->set_source_rectangle(0, 0, w, h);
297 // set destination to the display rectangle
298 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
301 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
304 // configure surface to wxh dimensions
305 s->set_configuration(w, h);
306 // set source reactangle, even if we should not need to set it.
307 s->set_source_rectangle(0, 0, w, h);
309 // set destination to the display rectangle
310 s->set_destination_rectangle(x, y, w, h);
312 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
313 surface_id, layer_id, x, y, w, h);
316 void App::layout_commit() {
317 this->controller->commit_changes();
318 this->display->flush();
321 char const *App::api_activate_surface(char const *drawing_name) {
323 auto const &surface_id = this->lookup_id(drawing_name);
326 return "Surface does not exist";
329 if (!this->controller->surface_exists(*surface_id)) {
330 return "Surface does not exist in controller!";
333 auto layer_id = this->layers.get_layer_id(*surface_id);
336 return "Surface is not on any layer!";
339 auto o_state = *this->layers.get_layout_state(*surface_id);
341 if (o_state == nullptr) {
342 return "Could not find layer for surface";
345 struct LayoutState &state = *o_state;
347 // disable layers that are above our current layer
348 for (auto const &l : this->layers.mapping) {
349 if (l.second.layer_id <= *layer_id) {
354 if (l.second.state.main != -1) {
355 this->deactivate(l.second.state.main);
356 l.second.state.main = -1;
360 if (l.second.state.sub != -1) {
361 this->deactivate(l.second.state.sub);
362 l.second.state.sub = -1;
367 this->layout_commit();
371 if (state.main == *surface_id || state.sub == *surface_id) {
372 return "Surface already active";
375 if (state.main == -1) {
377 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
378 this->surface_set_layout(*surface_id);
379 // XXX do we need to activate after enddraw?
381 this->emit_syncdraw(drawing_name);
382 this->enqueue_flushdraw(state.main);
385 bool can_split = this->can_split(state, *surface_id);
390 LayoutState{state.main, *surface_id},
391 [&] (LayoutState const &nl) {
393 std::move(*this->lookup_name(state.main));
395 this->surface_set_layout(state.main, surface_id);
396 if (state.sub != -1) {
397 this->deactivate(state.sub);
401 this->emit_syncdraw(drawing_name);
402 this->emit_syncdraw(main.c_str());
403 this->enqueue_flushdraw(state.main);
404 this->enqueue_flushdraw(state.sub);
408 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
409 this->surface_set_layout(*surface_id);
410 this->deactivate(state.main);
411 if (state.sub != -1) {
412 this->deactivate(state.sub);
416 this->emit_syncdraw(drawing_name);
417 this->enqueue_flushdraw(state.main);
426 char const *App::api_deactivate_surface(char const *drawing_name) {
428 auto const &surface_id = this->lookup_id(drawing_name);
431 return "Surface does not exist";
434 if (*surface_id == this->layers.main_surface) {
435 return "Cannot deactivate main_surface";
438 auto o_state = *this->layers.get_layout_state(*surface_id);
440 if (o_state == nullptr) {
441 return "Could not find layer for surface";
444 struct LayoutState &state = *o_state;
446 if (state.main == -1) {
447 return "No surface active";
450 // XXX: check against main_surface, main_surface_name is the configuration
452 if (*surface_id == this->layers.main_surface) {
453 logdebug("Refusing to deactivate main_surface %d", *surface_id);
457 if (state.main == *surface_id) {
458 if (state.sub != -1) {
460 state, LayoutState{state.sub, -1}, [&] (LayoutState const &nl) {
461 std::string sub = std::move(*this->lookup_name(state.sub));
463 this->deactivate(*surface_id);
464 this->surface_set_layout(state.sub);
467 this->layout_commit();
468 this->emit_syncdraw(sub.c_str());
469 this->enqueue_flushdraw(state.sub);
472 this->try_layout(state, LayoutState{-1, -1}, [&] (LayoutState const &nl) {
473 this->deactivate(*surface_id);
475 this->layout_commit();
478 } else if (state.sub == *surface_id) {
480 state, LayoutState{state.main, -1}, [&] (LayoutState const &nl) {
481 std::string main = std::move(*this->lookup_name(state.main));
483 this->deactivate(*surface_id);
484 this->surface_set_layout(state.main);
487 this->layout_commit();
488 this->emit_syncdraw(main.c_str());
489 this->enqueue_flushdraw(state.main);
492 return "Surface is not active";
498 void App::enqueue_flushdraw(int surface_id) {
499 this->check_flushdraw(surface_id);
500 logdebug("Enqueuing EndDraw for surface_id %d", surface_id);
501 this->pending_end_draw.push_back(surface_id);
504 void App::check_flushdraw(int surface_id) {
505 auto i = std::find(std::begin(this->pending_end_draw),
506 std::end(this->pending_end_draw), surface_id);
507 if (i != std::end(this->pending_end_draw)) {
508 auto n = this->lookup_name(surface_id);
509 logerror("Application %s (%d) has pending EndDraw call(s)!",
510 n ? n->c_str() : "unknown-name", surface_id);
511 std::swap(this->pending_end_draw[std::distance(
512 std::begin(this->pending_end_draw), i)],
513 this->pending_end_draw.back());
514 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
518 char const *App::api_enddraw(char const *drawing_name) {
519 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
520 auto n = this->lookup_name(this->pending_end_draw[i]);
521 if (n && *n == drawing_name) {
522 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
523 this->pending_end_draw.resize(iend - 1);
524 // XXX: Please tell the compositor to thaw the surface...
525 this->activate(this->pending_end_draw[i]);
526 this->layout_commit();
527 this->emit_flushdraw(drawing_name);
531 return "No EndDraw pending for surface";
534 void App::api_ping() { this->dispatch_pending_events(); }
537 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
538 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
539 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
540 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
542 void App::surface_created(uint32_t surface_id) {
543 auto layer_id = this->layers.get_layer_id(surface_id);
545 logdebug("Newly created surfce %d is not associated with any layer!",
550 logdebug("surface_id is %u, layer_id is %u", surface_id, *layer_id);
552 this->controller->layers[*layer_id]->add_surface(
553 this->controller->surfaces[surface_id].get());
555 // activate the main_surface right away
556 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
557 logdebug("Activating main_surface (%d)", surface_id);
559 this->api_activate_surface(
560 this->lookup_name(surface_id).value_or("unknown-name").c_str());
564 void App::surface_removed(uint32_t surface_id) {
565 logdebug("surface_id is %u", surface_id);
567 // We cannot normally deactivate the main_surface, so be explicit
569 if (int(surface_id) == this->layers.main_surface) {
570 this->deactivate_main_surface();
572 auto drawing_name = this->lookup_name(surface_id);
574 this->api_deactivate_surface(drawing_name->c_str());
578 this->id_alloc.remove_id(surface_id);
579 this->layers.remove_surface(surface_id);
582 void App::emit_activated(char const *label) {
583 this->api.send_event("active", label);
586 void App::emit_deactivated(char const *label) {
587 this->api.send_event("inactive", label);
590 void App::emit_syncdraw(char const *label) {
591 this->api.send_event("syncdraw", label);
594 void App::emit_flushdraw(char const *label) {
595 this->api.send_event("flushdraw", label);
598 void App::emit_visible(char const *label, bool is_visible) {
599 this->api.send_event(is_visible ? "visible" : "invisible", label);
602 void App::emit_invisible(char const *label) {
603 return emit_visible(label, false);
606 void App::emit_visible(char const *label) { return emit_visible(label, true); }
608 result<int> App::api_request_surface(char const *drawing_name) {
609 auto lid = this->layers.get_layer_id(std::string(drawing_name));
611 // XXX: to we need to put these applications on the App layer?
612 return Err<int>("Drawing name does not match any role");
615 auto rname = this->lookup_id(drawing_name);
617 // name does not exist yet, allocate surface id...
618 auto id = int(this->id_alloc.generate_id(drawing_name));
619 this->layers.add_surface(id, *lid);
621 // XXX: we set the main_surface[_name] here and now,
622 // not sure if we want this, but it worked so far.
623 if (!this->layers.main_surface_name.empty() &&
624 this->layers.main_surface_name == drawing_name) {
625 this->layers.main_surface = id;
626 logdebug("Set main_surface id to %u", id);
632 // Check currently registered drawing names if it is already there.
633 return Err<int>("Surface already present");
636 void App::activate(int id) {
637 auto ip = this->controller->sprops.find(id);
638 if (ip != this->controller->sprops.end() && ip->second.visibility == 0) {
639 this->controller->surfaces[id]->set_visibility(1);
641 this->lookup_name(id).value_or("unknown-name").c_str();
642 this->emit_activated(label);
643 this->emit_visible(label);
647 void App::deactivate(int id) {
648 auto ip = this->controller->sprops.find(id);
649 if (ip != this->controller->sprops.end() && ip->second.visibility != 0) {
650 this->controller->surfaces[id]->set_visibility(0);
652 this->lookup_name(id).value_or("unknown-name").c_str();
653 this->emit_deactivated(label);
654 this->emit_invisible(label);
658 void App::deactivate_main_surface() {
659 this->layers.main_surface = -1;
660 this->api_deactivate_surface(this->layers.main_surface_name.c_str());
663 bool App::can_split(struct LayoutState const &state, int new_id) {
664 if (state.main != -1 && state.main != new_id) {
665 auto new_id_layer = this->layers.get_layer_id(new_id).value();
666 auto current_id_layer = this->layers.get_layer_id(state.main).value();
668 // surfaces are on separate layers, don't bother.
669 if (new_id_layer != current_id_layer) {
673 std::string const &new_id_str = this->lookup_name(new_id).value();
674 std::string const &cur_id_str = this->lookup_name(state.main).value();
676 auto const &layer = this->layers.get_layer(new_id_layer);
678 logdebug("layer info name: %s", layer->name.c_str());
680 if (layer->layouts.empty()) {
684 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
685 logdebug("%d main_match '%s'", new_id_layer, i->main_match.c_str());
686 auto rem = std::regex(i->main_match);
687 if (std::regex_match(cur_id_str, rem)) {
688 // build the second one only if the first already matched
689 logdebug("%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
690 auto res = std::regex(i->sub_match);
691 if (std::regex_match(new_id_str, res)) {
692 logdebug("layout matched!");
702 void App::try_layout(struct LayoutState & /*state*/,
703 struct LayoutState const &new_layout,
704 std::function<void(LayoutState const &nl)> apply) {
705 if (this->policy.layout_is_valid(new_layout)) {
711 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
712 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
713 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
714 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
716 void controller_hooks::surface_created(uint32_t surface_id) {
717 this->app->surface_created(surface_id);
720 void controller_hooks::surface_removed(uint32_t surface_id) {
721 this->app->surface_removed(surface_id);
724 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
727 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,