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);
72 App::App(wl::display *d)
81 pending_events(false),
85 auto l = load_layer_map(
86 this->config.get_string("layers.json").value().c_str());
88 this->layers = l.unwrap();
90 logerror("%s", l.err().value());
93 } catch (std::exception &e) {
94 logerror("Loading of configuration failed: %s", e.what());
99 if (!this->display->ok()) {
103 if (this->layers.mapping.empty()) {
104 logerror("No surface -> layer mapping loaded");
108 this->display->add_global_handler(
109 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
110 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
113 this->display->add_global_handler(
114 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
116 std::make_unique<struct genivi::controller>(r, name, v);
118 // Init controller hooks
119 this->controller->chooks = &this->chooks;
121 // XXX: This protocol needs the output, so lets just add our mapping
123 this->controller->add_proxy_to_id_mapping(
124 this->outputs.back()->proxy.get(),
125 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
126 this->outputs.back()->proxy.get())));
129 // First level objects
130 this->display->roundtrip();
131 // Second level objects
132 this->display->roundtrip();
133 // Third level objects
134 this->display->roundtrip();
136 return init_layers();
139 int App::dispatch_events() {
140 if (this->dispatch_events() == 0) {
144 int ret = this->display->dispatch();
146 logerror("wl_display_dipatch() returned error %d",
147 this->display->get_error());
150 this->display->flush();
155 int App::dispatch_pending_events() {
156 if (this->pop_pending_events()) {
157 this->display->dispatch_pending();
163 bool App::pop_pending_events() {
165 return this->pending_events.compare_exchange_strong(
166 x, false, std::memory_order_consume);
169 void App::set_pending_events() {
170 this->pending_events.store(true, std::memory_order_release);
173 optional<int> App::lookup_id(char const *name) {
174 return this->id_alloc.lookup(std::string(name));
176 optional<std::string> App::lookup_name(int id) {
177 return this->id_alloc.lookup(id);
183 int App::init_layers() {
184 if (!this->controller) {
185 logerror("ivi_controller global not available");
189 if (this->outputs.empty()) {
190 logerror("no output was set up!");
194 auto &c = this->controller;
196 auto &o = this->outputs.front();
197 auto &s = c->screens.begin()->second;
198 auto &layers = c->layers;
200 // Write output dimensions to ivi controller...
201 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
209 // Quick and dirty setup of layers
210 for (auto const &i : this->layers.mapping) {
211 c->layer_create(i.second.layer_id, o->width, o->height);
212 auto &l = layers[i.second.layer_id];
213 l->set_destination_rectangle(0, 0, o->width, o->height);
214 l->set_visibility(1);
215 logdebug("Setting up layer %s (%d) for surface role match \"%s\"",
216 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
219 // Add layers to screen
220 s->set_render_order(this->layers.layers);
222 this->layout_commit();
227 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id) {
228 if (!this->controller->surface_exists(surface_id)) {
229 logerror("Surface %d does not exist", surface_id);
233 auto o_layer_id = this->layers.get_layer_id(surface_id);
236 logerror("Surface %d is not associated with any layer!", surface_id);
240 uint32_t layer_id = *o_layer_id;
242 auto const &layer = this->layers.get_layer(layer_id);
243 auto rect = layer.value().rect;
244 auto &s = this->controller->surfaces[surface_id];
251 // less-than-0 values refer to MAX + 1 - $VALUE
252 // e.g. MAX is either screen width or height
254 w = this->controller->output_size.w + 1 + w;
257 h = this->controller->output_size.h + 1 + h;
260 if (sub_surface_id) {
261 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id)) {
263 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
264 surface_id, *sub_surface_id);
271 // split along major axis
280 auto &ss = this->controller->surfaces[*sub_surface_id];
282 logdebug("surface_set_layout for sub surface %u on layer %u",
283 *sub_surface_id, layer_id);
285 // configure surface to wxh dimensions
286 ss->set_configuration(w, h);
287 // set source reactangle, even if we should not need to set it.
288 ss->set_source_rectangle(0, 0, w, h);
289 // set destination to the display rectangle
290 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
293 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
296 // configure surface to wxh dimensions
297 s->set_configuration(w, h);
298 // set source reactangle, even if we should not need to set it.
299 s->set_source_rectangle(0, 0, w, h);
301 // set destination to the display rectangle
302 s->set_destination_rectangle(x, y, w, h);
304 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
305 surface_id, layer_id, x, y, w, h);
308 void App::layout_commit() {
309 this->controller->commit_changes();
310 this->display->flush();
313 char const *App::api_activate_surface(char const *drawing_name, char const *drawing_area) {
315 auto const &surface_id = this->lookup_id(drawing_name);
318 return "Surface does not exist";
321 if (!this->controller->surface_exists(*surface_id)) {
322 return "Surface does not exist in controller!";
325 auto layer_id = this->layers.get_layer_id(*surface_id);
328 return "Surface is not on any layer!";
331 auto o_state = *this->layers.get_layout_state(*surface_id);
333 if (o_state == nullptr) {
334 return "Could not find layer for surface";
337 struct LayoutState &state = *o_state;
339 // disable layers that are above our current layer
340 for (auto const &l : this->layers.mapping) {
341 if (l.second.layer_id <= *layer_id) {
346 if (l.second.state.main != -1) {
347 this->deactivate(l.second.state.main);
348 l.second.state.main = -1;
352 if (l.second.state.sub != -1) {
353 this->deactivate(l.second.state.sub);
354 l.second.state.sub = -1;
359 this->layout_commit();
363 if (state.main == *surface_id || state.sub == *surface_id) {
364 return "Surface already active";
367 if (state.main == -1) {
369 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
370 this->surface_set_layout(*surface_id);
372 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
373 this->emit_syncdraw(drawing_name, str_area.c_str());
374 this->enqueue_flushdraw(state.main);
377 bool can_split = this->can_split(state, *surface_id);
382 LayoutState{state.main, *surface_id},
383 [&] (LayoutState const &nl) {
385 std::move(*this->lookup_name(state.main));
387 this->surface_set_layout(state.main, surface_id);
388 if (state.sub != -1) {
389 this->deactivate(state.sub);
393 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
394 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
395 this->emit_syncdraw(main.c_str(), str_area_main.c_str());
396 this->emit_syncdraw(drawing_name, str_area_sub.c_str());
397 this->enqueue_flushdraw(state.main);
398 this->enqueue_flushdraw(state.sub);
402 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
403 this->surface_set_layout(*surface_id);
404 this->deactivate(state.main);
405 if (state.sub != -1) {
406 this->deactivate(state.sub);
411 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
412 this->emit_syncdraw(drawing_name, str_area.c_str());
413 this->enqueue_flushdraw(state.main);
422 char const *App::api_deactivate_surface(char const *drawing_name) {
424 auto const &surface_id = this->lookup_id(drawing_name);
427 return "Surface does not exist";
430 if (*surface_id == this->layers.main_surface) {
431 return "Cannot deactivate main_surface";
434 auto o_state = *this->layers.get_layout_state(*surface_id);
436 if (o_state == nullptr) {
437 return "Could not find layer for surface";
440 struct LayoutState &state = *o_state;
442 if (state.main == -1) {
443 return "No surface active";
446 // XXX: check against main_surface, main_surface_name is the configuration
448 if (*surface_id == this->layers.main_surface) {
449 logdebug("Refusing to deactivate main_surface %d", *surface_id);
453 if (state.main == *surface_id) {
454 if (state.sub != -1) {
456 state, LayoutState{state.sub, -1}, [&] (LayoutState const &nl) {
457 std::string sub = std::move(*this->lookup_name(state.sub));
459 this->deactivate(*surface_id);
460 this->surface_set_layout(state.sub);
463 this->layout_commit();
464 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
465 this->emit_syncdraw(sub.c_str(), str_area.c_str());
466 this->enqueue_flushdraw(state.sub);
469 this->try_layout(state, LayoutState{-1, -1}, [&] (LayoutState const &nl) {
470 this->deactivate(*surface_id);
472 this->layout_commit();
475 } else if (state.sub == *surface_id) {
477 state, LayoutState{state.main, -1}, [&] (LayoutState const &nl) {
478 std::string main = std::move(*this->lookup_name(state.main));
480 this->deactivate(*surface_id);
481 this->surface_set_layout(state.main);
484 this->layout_commit();
485 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
486 this->emit_syncdraw(main.c_str(), str_area.c_str());
487 this->enqueue_flushdraw(state.main);
490 return "Surface is not active";
496 void App::enqueue_flushdraw(int surface_id) {
497 this->check_flushdraw(surface_id);
498 logdebug("Enqueuing EndDraw for surface_id %d", surface_id);
499 this->pending_end_draw.push_back(surface_id);
502 void App::check_flushdraw(int surface_id) {
503 auto i = std::find(std::begin(this->pending_end_draw),
504 std::end(this->pending_end_draw), surface_id);
505 if (i != std::end(this->pending_end_draw)) {
506 auto n = this->lookup_name(surface_id);
507 logerror("Application %s (%d) has pending EndDraw call(s)!",
508 n ? n->c_str() : "unknown-name", surface_id);
509 std::swap(this->pending_end_draw[std::distance(
510 std::begin(this->pending_end_draw), i)],
511 this->pending_end_draw.back());
512 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
516 char const *App::api_enddraw(char const *drawing_name) {
517 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
518 auto n = this->lookup_name(this->pending_end_draw[i]);
519 if (n && *n == drawing_name) {
520 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
521 this->pending_end_draw.resize(iend - 1);
522 // XXX: Please tell the compositor to thaw the surface...
523 this->activate(this->pending_end_draw[i]);
524 this->layout_commit();
525 this->emit_flushdraw(drawing_name);
529 return "No EndDraw pending for surface";
532 void App::api_ping() { this->dispatch_pending_events(); }
537 void App::surface_created(uint32_t surface_id) {
538 auto layer_id = this->layers.get_layer_id(surface_id);
540 logdebug("Newly created surfce %d is not associated with any layer!",
545 logdebug("surface_id is %u, layer_id is %u", surface_id, *layer_id);
547 this->controller->layers[*layer_id]->add_surface(
548 this->controller->surfaces[surface_id].get());
550 // activate the main_surface right away
551 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
552 logdebug("Activating main_surface (%d)", surface_id);
554 this->api_activate_surface(
555 this->lookup_name(surface_id).value_or("unknown-name").c_str());
559 void App::surface_removed(uint32_t surface_id) {
560 logdebug("surface_id is %u", surface_id);
562 // We cannot normally deactivate the main_surface, so be explicit
564 if (int(surface_id) == this->layers.main_surface) {
565 this->deactivate_main_surface();
567 auto drawing_name = this->lookup_name(surface_id);
569 this->api_deactivate_surface(drawing_name->c_str());
573 this->id_alloc.remove_id(surface_id);
574 this->layers.remove_surface(surface_id);
577 void App::emit_activated(char const *label) {
578 this->api.send_event("active", label);
581 void App::emit_deactivated(char const *label) {
582 this->api.send_event("inactive", label);
585 void App::emit_syncdraw(char const *label, char const *area) {
586 this->api.send_event("syncdraw", label, area);
589 void App::emit_flushdraw(char const *label) {
590 this->api.send_event("flushdraw", label);
593 void App::emit_visible(char const *label, bool is_visible) {
594 this->api.send_event(is_visible ? "visible" : "invisible", label);
597 void App::emit_invisible(char const *label) {
598 return emit_visible(label, false);
601 void App::emit_visible(char const *label) { return emit_visible(label, true); }
603 result<int> App::api_request_surface(char const *drawing_name) {
604 auto lid = this->layers.get_layer_id(std::string(drawing_name));
606 // XXX: to we need to put these applications on the App layer?
607 return Err<int>("Drawing name does not match any role");
610 auto rname = this->lookup_id(drawing_name);
612 // name does not exist yet, allocate surface id...
613 auto id = int(this->id_alloc.generate_id(drawing_name));
614 this->layers.add_surface(id, *lid);
616 // XXX: we set the main_surface[_name] here and now,
617 // not sure if we want this, but it worked so far.
618 if (!this->layers.main_surface_name.empty() &&
619 this->layers.main_surface_name == drawing_name) {
620 this->layers.main_surface = id;
621 logdebug("Set main_surface id to %u", id);
627 // Check currently registered drawing names if it is already there.
628 return Err<int>("Surface already present");
631 void App::activate(int id) {
632 auto ip = this->controller->sprops.find(id);
633 if (ip != this->controller->sprops.end() && ip->second.visibility == 0) {
634 this->controller->surfaces[id]->set_visibility(1);
636 this->lookup_name(id).value_or("unknown-name").c_str();
637 this->emit_visible(label);
638 this->emit_activated(label);
642 void App::deactivate(int id) {
643 auto ip = this->controller->sprops.find(id);
644 if (ip != this->controller->sprops.end() && ip->second.visibility != 0) {
645 this->controller->surfaces[id]->set_visibility(0);
647 this->lookup_name(id).value_or("unknown-name").c_str();
648 this->emit_deactivated(label);
649 this->emit_invisible(label);
653 void App::deactivate_main_surface() {
654 this->layers.main_surface = -1;
655 this->api_deactivate_surface(this->layers.main_surface_name.c_str());
658 bool App::can_split(struct LayoutState const &state, int new_id) {
659 if (state.main != -1 && state.main != new_id) {
660 auto new_id_layer = this->layers.get_layer_id(new_id).value();
661 auto current_id_layer = this->layers.get_layer_id(state.main).value();
663 // surfaces are on separate layers, don't bother.
664 if (new_id_layer != current_id_layer) {
668 std::string const &new_id_str = this->lookup_name(new_id).value();
669 std::string const &cur_id_str = this->lookup_name(state.main).value();
671 auto const &layer = this->layers.get_layer(new_id_layer);
673 logdebug("layer info name: %s", layer->name.c_str());
675 if (layer->layouts.empty()) {
679 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
680 logdebug("%d main_match '%s'", new_id_layer, i->main_match.c_str());
681 auto rem = std::regex(i->main_match);
682 if (std::regex_match(cur_id_str, rem)) {
683 // build the second one only if the first already matched
684 logdebug("%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
685 auto res = std::regex(i->sub_match);
686 if (std::regex_match(new_id_str, res)) {
687 logdebug("layout matched!");
697 void App::try_layout(struct LayoutState & /*state*/,
698 struct LayoutState const &new_layout,
699 std::function<void(LayoutState const &nl)> apply) {
700 if (this->policy.layout_is_valid(new_layout)) {
708 void controller_hooks::surface_created(uint32_t surface_id) {
709 this->app->surface_created(surface_id);
712 void controller_hooks::surface_removed(uint32_t surface_id) {
713 this->app->surface_removed(surface_id);
716 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
719 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,