2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 HMI_DEBUG("wm", "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);
74 App::App(wl::display *d)
83 pending_events(false),
87 auto l = load_layer_map(
88 this->config.get_string("layers.json").value().c_str());
90 this->layers = l.unwrap();
92 HMI_ERROR("wm", "%s", l.err().value());
95 } catch (std::exception &e) {
96 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
101 if (!this->display->ok()) {
105 if (this->layers.mapping.empty()) {
106 HMI_ERROR("wm", "No surface -> layer mapping loaded");
111 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
112 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
115 this->display->add_global_handler(
116 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
117 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
120 this->display->add_global_handler(
121 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
123 std::make_unique<struct compositor::controller>(r, name, v);
125 // Init controller hooks
126 this->controller->chooks = &this->chooks;
128 // This protocol needs the output, so lets just add our mapping here...
129 this->controller->add_proxy_to_id_mapping(
130 this->outputs.back()->proxy.get(),
131 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
132 this->outputs.back()->proxy.get())));
135 // First level objects
136 this->display->roundtrip();
137 // Second level objects
138 this->display->roundtrip();
139 // Third level objects
140 this->display->roundtrip();
142 return init_layers();
145 int App::dispatch_events() {
146 if (this->dispatch_events() == 0) {
150 int ret = this->display->dispatch();
152 HMI_ERROR("wm", "wl_display_dipatch() returned error %d",
153 this->display->get_error());
156 this->display->flush();
161 int App::dispatch_pending_events() {
162 if (this->pop_pending_events()) {
163 this->display->dispatch_pending();
169 bool App::pop_pending_events() {
171 return this->pending_events.compare_exchange_strong(
172 x, false, std::memory_order_consume);
175 void App::set_pending_events() {
176 this->pending_events.store(true, std::memory_order_release);
179 optional<int> App::lookup_id(char const *name) {
180 return this->id_alloc.lookup(std::string(name));
182 optional<std::string> App::lookup_name(int id) {
183 return this->id_alloc.lookup(id);
189 int App::init_layers() {
190 if (!this->controller) {
191 HMI_ERROR("wm", "ivi_controller global not available");
195 if (this->outputs.empty()) {
196 HMI_ERROR("wm", "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 // Write output dimensions to ivi controller...
207 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
215 // Quick and dirty setup of layers
216 for (auto const &i : this->layers.mapping) {
217 c->layer_create(i.second.layer_id, o->width, o->height);
218 auto &l = layers[i.second.layer_id];
219 l->set_destination_rectangle(0, 0, o->width, o->height);
220 l->set_visibility(1);
221 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
222 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
225 // Add layers to screen
226 s->set_render_order(this->layers.layers);
228 this->layout_commit();
233 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id) {
234 if (!this->controller->surface_exists(surface_id)) {
235 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
239 auto o_layer_id = this->layers.get_layer_id(surface_id);
242 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
246 uint32_t layer_id = *o_layer_id;
248 auto const &layer = this->layers.get_layer(layer_id);
249 auto rect = layer.value().rect;
250 auto &s = this->controller->surfaces[surface_id];
257 // less-than-0 values refer to MAX + 1 - $VALUE
258 // e.g. MAX is either screen width or height
260 w = this->controller->output_size.w + 1 + w;
263 h = this->controller->output_size.h + 1 + h;
266 if (sub_surface_id) {
267 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id)) {
269 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
270 surface_id, *sub_surface_id);
277 // split along major axis
286 auto &ss = this->controller->surfaces[*sub_surface_id];
288 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
289 *sub_surface_id, layer_id);
291 // configure surface to wxh dimensions
292 ss->set_configuration(w, h);
293 // set source reactangle, even if we should not need to set it.
294 ss->set_source_rectangle(0, 0, w, h);
295 // set destination to the display rectangle
296 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
300 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
303 // configure surface to wxh dimensions
304 s->set_configuration(w, h);
305 // set source reactangle, even if we should not need to set it.
306 s->set_source_rectangle(0, 0, w, h);
308 // set destination to the display rectangle
309 s->set_destination_rectangle(x, y, w, h);
311 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
312 surface_id, layer_id, x, y, w, h);
315 void App::layout_commit() {
316 this->controller->commit_changes();
317 this->display->flush();
320 char const *App::api_activate_surface(char const *drawing_name, char const *drawing_area) {
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 auto layer = this->layers.get_layer(*layer_id);
373 if (state.main == -1) {
375 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
376 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
377 this->surface_set_layout(*surface_id);
380 // Commit for configuraton
381 this->layout_commit();
383 if (!(layer->is_normal_layout_only)) {
384 // Wait for configuration listener
385 controller->is_configured = false;
386 while (!(controller->is_configured)) {
387 dispatch_pending_events();
391 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
392 this->emit_syncdraw(drawing_name, str_area.c_str());
393 this->enqueue_flushdraw(state.main);
396 if (0 == strcmp(drawing_name, "HomeScreen")) {
398 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
399 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
400 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
401 this->emit_syncdraw(drawing_name, str_area.c_str());
402 this->enqueue_flushdraw(state.main);
405 bool can_split = this->can_split(state, *surface_id);
410 LayoutState{state.main, *surface_id},
411 [&] (LayoutState const &nl) {
412 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
414 std::move(*this->lookup_name(state.main));
416 this->surface_set_layout(state.main, surface_id);
417 if (state.sub != *surface_id) {
418 if (state.sub != -1) {
419 this->deactivate(state.sub);
424 // Commit for configuraton and visibility(0)
425 this->layout_commit();
427 // Wait for configuration listener
428 controller->is_configured = false;
429 while (!(controller->is_configured)) {
430 dispatch_pending_events();
433 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
434 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
435 this->emit_syncdraw(main.c_str(), str_area_main.c_str());
436 this->emit_syncdraw(drawing_name, str_area_sub.c_str());
437 this->enqueue_flushdraw(state.main);
438 this->enqueue_flushdraw(state.sub);
442 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
443 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
445 this->surface_set_layout(*surface_id);
446 if (state.main != *surface_id) {
447 this->deactivate(state.main);
449 if (state.sub != -1) {
450 if (state.sub != *surface_id) {
451 this->deactivate(state.sub);
456 // Commit for configuraton and visibility(0)
457 this->layout_commit();
459 if (!(layer->is_normal_layout_only)) {
460 // Wait for configuration listener
461 controller->is_configured = false;
462 while (!(controller->is_configured)) {
463 dispatch_pending_events();
467 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
468 this->emit_syncdraw(drawing_name, str_area.c_str());
469 this->enqueue_flushdraw(state.main);
479 char const *App::api_deactivate_surface(char const *drawing_name) {
481 auto const &surface_id = this->lookup_id(drawing_name);
484 return "Surface does not exist";
487 if (*surface_id == this->layers.main_surface) {
488 return "Cannot deactivate main_surface";
491 auto o_state = *this->layers.get_layout_state(*surface_id);
493 if (o_state == nullptr) {
494 return "Could not find layer for surface";
497 struct LayoutState &state = *o_state;
499 if (state.main == -1) {
500 return "No surface active";
503 // XXX: check against main_surface, main_surface_name is the configuration
505 if (*surface_id == this->layers.main_surface) {
506 HMI_DEBUG("wm", "Refusing to deactivate main_surface %d", *surface_id);
510 if (state.main == *surface_id) {
511 if (state.sub != -1) {
513 state, LayoutState{state.sub, -1}, [&] (LayoutState const &nl) {
514 std::string sub = std::move(*this->lookup_name(state.sub));
516 this->deactivate(*surface_id);
517 this->surface_set_layout(state.sub);
520 this->layout_commit();
521 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
522 this->emit_syncdraw(sub.c_str(), str_area.c_str());
523 this->enqueue_flushdraw(state.sub);
526 this->try_layout(state, LayoutState{-1, -1}, [&] (LayoutState const &nl) {
527 this->deactivate(*surface_id);
529 this->layout_commit();
532 } else if (state.sub == *surface_id) {
534 state, LayoutState{state.main, -1}, [&] (LayoutState const &nl) {
535 std::string main = std::move(*this->lookup_name(state.main));
537 this->deactivate(*surface_id);
538 this->surface_set_layout(state.main);
541 this->layout_commit();
542 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
543 this->emit_syncdraw(main.c_str(), str_area.c_str());
544 this->enqueue_flushdraw(state.main);
547 return "Surface is not active";
553 void App::enqueue_flushdraw(int surface_id) {
554 this->check_flushdraw(surface_id);
555 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
556 this->pending_end_draw.push_back(surface_id);
559 void App::check_flushdraw(int surface_id) {
560 auto i = std::find(std::begin(this->pending_end_draw),
561 std::end(this->pending_end_draw), surface_id);
562 if (i != std::end(this->pending_end_draw)) {
563 auto n = this->lookup_name(surface_id);
564 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
565 n ? n->c_str() : "unknown-name", surface_id);
566 std::swap(this->pending_end_draw[std::distance(
567 std::begin(this->pending_end_draw), i)],
568 this->pending_end_draw.back());
569 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
573 char const *App::api_enddraw(char const *drawing_name) {
574 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
575 auto n = this->lookup_name(this->pending_end_draw[i]);
576 if (n && *n == drawing_name) {
577 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
578 this->pending_end_draw.resize(iend - 1);
579 // XXX: Please tell the compositor to thaw the surface...
580 this->activate(this->pending_end_draw[i]);
581 this->layout_commit();
582 this->emit_flushdraw(drawing_name);
586 return "No EndDraw pending for surface";
589 void App::api_ping() { this->dispatch_pending_events(); }
594 void App::surface_created(uint32_t surface_id) {
595 auto layer_id = this->layers.get_layer_id(surface_id);
597 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
602 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
604 this->controller->layers[*layer_id]->add_surface(
605 this->controller->surfaces[surface_id].get());
607 // activate the main_surface right away
608 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
609 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
611 this->api_activate_surface(
612 this->lookup_name(surface_id).value_or("unknown-name").c_str());
616 void App::surface_removed(uint32_t surface_id) {
617 HMI_DEBUG("wm", "surface_id is %u", surface_id);
619 // We cannot normally deactivate the main_surface, so be explicit
621 if (int(surface_id) == this->layers.main_surface) {
622 this->deactivate_main_surface();
624 auto drawing_name = this->lookup_name(surface_id);
626 this->api_deactivate_surface(drawing_name->c_str());
630 this->id_alloc.remove_id(surface_id);
631 this->layers.remove_surface(surface_id);
634 void App::emit_activated(char const *label) {
635 this->api.send_event(kListEventName[Event_Active], label);
638 void App::emit_deactivated(char const *label) {
639 this->api.send_event(kListEventName[Event_Inactive], label);
642 void App::emit_syncdraw(char const *label, char const *area) {
643 this->api.send_event(kListEventName[Event_SyncDraw], label, area);
646 void App::emit_flushdraw(char const *label) {
647 this->api.send_event(kListEventName[Event_FlushDraw], label);
650 void App::emit_visible(char const *label, bool is_visible) {
651 this->api.send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
654 void App::emit_invisible(char const *label) {
655 return emit_visible(label, false);
658 void App::emit_visible(char const *label) { return emit_visible(label, true); }
660 result<int> App::api_request_surface(char const *drawing_name) {
661 auto lid = this->layers.get_layer_id(std::string(drawing_name));
663 // TODO: Do we need to put these applications on the App layer?
664 return Err<int>("Drawing name does not match any role");
667 auto rname = this->lookup_id(drawing_name);
669 // name does not exist yet, allocate surface id...
670 auto id = int(this->id_alloc.generate_id(drawing_name));
671 this->layers.add_surface(id, *lid);
673 // set the main_surface[_name] here and now
674 if (!this->layers.main_surface_name.empty() &&
675 this->layers.main_surface_name == drawing_name) {
676 this->layers.main_surface = id;
677 HMI_DEBUG("wm", "Set main_surface id to %u", id);
683 // Check currently registered drawing names if it is already there.
684 return Err<int>("Surface already present");
687 void App::activate(int id) {
688 auto ip = this->controller->sprops.find(id);
689 if (ip != this->controller->sprops.end()) {
690 this->controller->surfaces[id]->set_visibility(0);
691 this->layout_commit();
692 this->controller->surfaces[id]->set_visibility(1);
694 this->lookup_name(id).value_or("unknown-name").c_str();
695 this->emit_visible(label);
696 this->emit_activated(label);
700 void App::deactivate(int id) {
701 auto ip = this->controller->sprops.find(id);
702 if (ip != this->controller->sprops.end() && ip->second.visibility != 0) {
703 this->controller->surfaces[id]->set_visibility(0);
705 this->lookup_name(id).value_or("unknown-name").c_str();
706 this->emit_deactivated(label);
707 this->emit_invisible(label);
711 void App::deactivate_main_surface() {
712 this->layers.main_surface = -1;
713 this->api_deactivate_surface(this->layers.main_surface_name.c_str());
716 bool App::can_split(struct LayoutState const &state, int new_id) {
717 if (state.main != -1 && state.main != new_id) {
718 auto new_id_layer = this->layers.get_layer_id(new_id).value();
719 auto current_id_layer = this->layers.get_layer_id(state.main).value();
721 // surfaces are on separate layers, don't bother.
722 if (new_id_layer != current_id_layer) {
726 std::string const &new_id_str = this->lookup_name(new_id).value();
727 std::string const &cur_id_str = this->lookup_name(state.main).value();
729 auto const &layer = this->layers.get_layer(new_id_layer);
731 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
733 if (layer->layouts.empty()) {
737 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
738 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
739 auto rem = std::regex(i->main_match);
740 if (std::regex_match(cur_id_str, rem)) {
741 // build the second one only if the first already matched
742 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
743 auto res = std::regex(i->sub_match);
744 if (std::regex_match(new_id_str, res)) {
745 HMI_DEBUG("wm", "layout matched!");
755 void App::try_layout(struct LayoutState & /*state*/,
756 struct LayoutState const &new_layout,
757 std::function<void(LayoutState const &nl)> apply) {
758 if (this->policy.layout_is_valid(new_layout)) {
766 void controller_hooks::surface_created(uint32_t surface_id) {
767 this->app->surface_created(surface_id);
770 void controller_hooks::surface_removed(uint32_t surface_id) {
771 this->app->surface_removed(surface_id);
774 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
777 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,