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>
45 result<json> file_to_json(char const *filename) {
47 std::ifstream i(filename);
49 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
50 j = default_layers_json;
59 struct result<layer_map> load_layer_map(char const *filename) {
60 HMI_DEBUG("wm", "loading IDs from %s", filename);
62 auto j = file_to_json(filename);
64 return Err<layer_map>(j.unwrap_err());
66 json jids = j.unwrap();
68 return to_layer_map(jids);
77 App::App(wl::display *d)
85 pending_events(false),
89 auto l = load_layer_map(
90 this->config.get_string("layers.json").value().c_str());
92 this->layers = l.unwrap();
94 HMI_ERROR("wm", "%s", l.err().value());
97 } catch (std::exception &e) {
98 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
103 if (!this->display->ok()) {
107 if (this->layers.mapping.empty()) {
108 HMI_ERROR("wm", "No surface -> layer mapping loaded");
113 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
114 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
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 compositor::controller>(r, name, v);
127 // Init controller hooks
128 this->controller->chooks = &this->chooks;
130 // This protocol needs the output, so lets just add our mapping here...
131 this->controller->add_proxy_to_id_mapping(
132 this->outputs.back()->proxy.get(),
133 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
134 this->outputs.back()->proxy.get())));
137 // First level objects
138 this->display->roundtrip();
139 // Second level objects
140 this->display->roundtrip();
141 // Third level objects
142 this->display->roundtrip();
144 return init_layers();
147 int App::dispatch_events() {
148 if (this->dispatch_events() == 0) {
152 int ret = this->display->dispatch();
154 HMI_ERROR("wm", "wl_display_dipatch() returned error %d",
155 this->display->get_error());
158 this->display->flush();
163 int App::dispatch_pending_events() {
164 if (this->pop_pending_events()) {
165 this->display->dispatch_pending();
171 bool App::pop_pending_events() {
173 return this->pending_events.compare_exchange_strong(
174 x, false, std::memory_order_consume);
177 void App::set_pending_events() {
178 this->pending_events.store(true, std::memory_order_release);
181 optional<int> App::lookup_id(char const *name) {
182 return this->id_alloc.lookup(std::string(name));
184 optional<std::string> App::lookup_name(int id) {
185 return this->id_alloc.lookup(id);
191 int App::init_layers() {
192 if (!this->controller) {
193 HMI_ERROR("wm", "ivi_controller global not available");
197 if (this->outputs.empty()) {
198 HMI_ERROR("wm", "no output was set up!");
202 auto &c = this->controller;
204 auto &o = this->outputs.front();
205 auto &s = c->screens.begin()->second;
206 auto &layers = c->layers;
208 // Write output dimensions to ivi controller...
209 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
217 // Quick and dirty setup of layers
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 HMI_DEBUG("wm", "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
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 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
241 auto o_layer_id = this->layers.get_layer_id(surface_id);
244 HMI_ERROR("wm", "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 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
291 *sub_surface_id, layer_id);
293 // set destination to the display rectangle
294 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
298 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
301 // set destination to the display rectangle
302 s->set_destination_rectangle(x, y, w, h);
304 HMI_DEBUG("wm", "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) {
316 auto const &surface_id = this->lookup_id(drawing_name);
319 return "Surface does not exist";
322 if (!this->controller->surface_exists(*surface_id)) {
323 return "Surface does not exist in controller!";
326 auto layer_id = this->layers.get_layer_id(*surface_id);
329 return "Surface is not on any layer!";
332 auto o_state = *this->layers.get_layout_state(*surface_id);
334 if (o_state == nullptr) {
335 return "Could not find layer for surface";
338 struct LayoutState &state = *o_state;
340 // disable layers that are above our current layer
341 for (auto const &l : this->layers.mapping) {
342 if (l.second.layer_id <= *layer_id) {
347 if (l.second.state.main != -1) {
348 this->deactivate(l.second.state.main);
349 l.second.state.main = -1;
353 if (l.second.state.sub != -1) {
354 this->deactivate(l.second.state.sub);
355 l.second.state.sub = -1;
360 this->layout_commit();
364 auto layer = this->layers.get_layer(*layer_id);
366 if (state.main == -1) {
368 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
369 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
370 this->surface_set_layout(*surface_id);
373 // Commit for configuraton
374 this->layout_commit();
376 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
377 this->emit_syncdraw(drawing_name, str_area.c_str());
378 this->enqueue_flushdraw(state.main);
381 if (0 == strcmp(drawing_name, "HomeScreen")) {
383 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
384 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
385 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
386 this->emit_syncdraw(drawing_name, str_area.c_str());
387 this->enqueue_flushdraw(state.main);
390 bool can_split = this->can_split(state, *surface_id);
395 LayoutState{state.main, *surface_id},
396 [&] (LayoutState const &nl) {
397 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
399 std::move(*this->lookup_name(state.main));
401 this->surface_set_layout(state.main, surface_id);
402 if (state.sub != *surface_id) {
403 if (state.sub != -1) {
404 this->deactivate(state.sub);
409 // Commit for configuration and visibility(0)
410 this->layout_commit();
412 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
413 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
414 this->emit_syncdraw(main.c_str(), str_area_main.c_str());
415 this->emit_syncdraw(drawing_name, str_area_sub.c_str());
416 this->enqueue_flushdraw(state.main);
417 this->enqueue_flushdraw(state.sub);
421 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
422 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
424 this->surface_set_layout(*surface_id);
425 if (state.main != *surface_id) {
426 this->deactivate(state.main);
428 if (state.sub != -1) {
429 if (state.sub != *surface_id) {
430 this->deactivate(state.sub);
435 // Commit for configuraton and visibility(0)
436 this->layout_commit();
438 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
439 this->emit_syncdraw(drawing_name, str_area.c_str());
440 this->enqueue_flushdraw(state.main);
450 char const *App::api_deactivate_surface(char const *drawing_name) {
452 auto const &surface_id = this->lookup_id(drawing_name);
455 return "Surface does not exist";
458 if (*surface_id == this->layers.main_surface) {
459 return "Cannot deactivate main_surface";
462 auto o_state = *this->layers.get_layout_state(*surface_id);
464 if (o_state == nullptr) {
465 return "Could not find layer for surface";
468 struct LayoutState &state = *o_state;
470 if (state.main == -1) {
471 return "No surface active";
474 // Check against main_surface, main_surface_name is the configuration item.
475 if (*surface_id == this->layers.main_surface) {
476 HMI_DEBUG("wm", "Refusing to deactivate main_surface %d", *surface_id);
480 if (state.main == *surface_id) {
481 if (state.sub != -1) {
483 state, LayoutState{state.sub, -1}, [&] (LayoutState const &nl) {
484 std::string sub = std::move(*this->lookup_name(state.sub));
486 this->deactivate(*surface_id);
487 this->surface_set_layout(state.sub);
490 this->layout_commit();
491 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
492 this->emit_syncdraw(sub.c_str(), str_area.c_str());
493 this->enqueue_flushdraw(state.sub);
496 this->try_layout(state, LayoutState{-1, -1}, [&] (LayoutState const &nl) {
497 this->deactivate(*surface_id);
499 this->layout_commit();
502 } else if (state.sub == *surface_id) {
504 state, LayoutState{state.main, -1}, [&] (LayoutState const &nl) {
505 std::string main = std::move(*this->lookup_name(state.main));
507 this->deactivate(*surface_id);
508 this->surface_set_layout(state.main);
511 this->layout_commit();
512 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
513 this->emit_syncdraw(main.c_str(), str_area.c_str());
514 this->enqueue_flushdraw(state.main);
517 return "Surface is not active";
523 void App::enqueue_flushdraw(int surface_id) {
524 this->check_flushdraw(surface_id);
525 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
526 this->pending_end_draw.push_back(surface_id);
529 void App::check_flushdraw(int surface_id) {
530 auto i = std::find(std::begin(this->pending_end_draw),
531 std::end(this->pending_end_draw), surface_id);
532 if (i != std::end(this->pending_end_draw)) {
533 auto n = this->lookup_name(surface_id);
534 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
535 n ? n->c_str() : "unknown-name", surface_id);
536 std::swap(this->pending_end_draw[std::distance(
537 std::begin(this->pending_end_draw), i)],
538 this->pending_end_draw.back());
539 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
543 char const *App::api_enddraw(char const *drawing_name) {
544 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
545 auto n = this->lookup_name(this->pending_end_draw[i]);
546 if (n && *n == drawing_name) {
547 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
548 this->pending_end_draw.resize(iend - 1);
549 this->activate(this->pending_end_draw[i]);
550 this->layout_commit();
551 this->emit_flushdraw(drawing_name);
555 return "No EndDraw pending for surface";
558 void App::api_ping() { this->dispatch_pending_events(); }
560 void App::send_event(char const *evname, char const *label){
561 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
563 json_object *j = json_object_new_object();
564 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
566 int ret = afb_event_push(this->map_afb_event[evname], j);
568 HMI_DEBUG("wm", "afb_event_push failed: %m");
572 void App::send_event(char const *evname, char const *label, char const *area){
573 HMI_DEBUG("wm", "%s: %s(%s, %s)", __func__, evname, label, area);
575 json_object *j = json_object_new_object();
576 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
577 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
579 int ret = afb_event_push(this->map_afb_event[evname], j);
581 HMI_DEBUG("wm", "afb_event_push failed: %m");
588 void App::surface_created(uint32_t surface_id) {
589 auto layer_id = this->layers.get_layer_id(surface_id);
591 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
596 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
598 this->controller->layers[*layer_id]->add_surface(
599 this->controller->surfaces[surface_id].get());
600 this->layout_commit();
601 // activate the main_surface right away
602 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
603 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
605 this->api_activate_surface(
606 this->lookup_name(surface_id).value_or("unknown-name").c_str());
610 void App::surface_removed(uint32_t surface_id) {
611 HMI_DEBUG("wm", "surface_id is %u", surface_id);
613 // We cannot normally deactivate the main_surface, so be explicit
615 if (int(surface_id) == this->layers.main_surface) {
616 this->deactivate_main_surface();
618 auto drawing_name = this->lookup_name(surface_id);
620 this->api_deactivate_surface(drawing_name->c_str());
624 this->id_alloc.remove_id(surface_id);
625 this->layers.remove_surface(surface_id);
628 void App::emit_activated(char const *label) {
629 this->send_event(kListEventName[Event_Active], label);
632 void App::emit_deactivated(char const *label) {
633 this->send_event(kListEventName[Event_Inactive], label);
636 void App::emit_syncdraw(char const *label, char const *area) {
637 this->send_event(kListEventName[Event_SyncDraw], label, area);
640 void App::emit_flushdraw(char const *label) {
641 this->send_event(kListEventName[Event_FlushDraw], label);
644 void App::emit_visible(char const *label, bool is_visible) {
645 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
648 void App::emit_invisible(char const *label) {
649 return emit_visible(label, false);
652 void App::emit_visible(char const *label) { return emit_visible(label, true); }
654 result<int> App::api_request_surface(char const *drawing_name) {
655 auto lid = this->layers.get_layer_id(std::string(drawing_name));
657 // TODO: Do we need to put these applications on the App layer?
658 return Err<int>("Drawing name does not match any role");
661 auto rname = this->lookup_id(drawing_name);
663 // name does not exist yet, allocate surface id...
664 auto id = int(this->id_alloc.generate_id(drawing_name));
665 this->layers.add_surface(id, *lid);
667 // set the main_surface[_name] here and now
668 if (!this->layers.main_surface_name.empty() &&
669 this->layers.main_surface_name == drawing_name) {
670 this->layers.main_surface = id;
671 HMI_DEBUG("wm", "Set main_surface id to %u", id);
677 // Check currently registered drawing names if it is already there.
678 return Err<int>("Surface already present");
681 char const *App::api_request_surface(char const *drawing_name,
682 char const *ivi_id) {
685 auto lid = this->layers.get_layer_id(std::string(drawing_name));
686 unsigned sid = std::stol(ivi_id);
689 return "Drawing name does not match any role";
692 auto rname = this->lookup_id(drawing_name);
695 return "Surface already present";
698 // register pair drawing_name and ivi_id
699 this->id_alloc.register_name_id(drawing_name, sid);
700 this->layers.add_surface(sid, *lid);
702 // this surface is already created
703 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
705 this->controller->layers[*lid]->add_surface(
706 this->controller->surfaces[sid].get());
707 this->layout_commit();
712 void App::activate(int id) {
713 auto ip = this->controller->sprops.find(id);
714 if (ip != this->controller->sprops.end()) {
715 this->controller->surfaces[id]->set_visibility(1);
717 this->lookup_name(id).value_or("unknown-name").c_str();
720 if ((0 == strcmp(label, "Radio"))
721 || (0 == strcmp(label, "MediaPlayer"))
722 || (0 == strcmp(label, "Music"))
723 || (0 == strcmp(label, "Navigation"))) {
724 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i) {
727 this->surface_bg.erase(i);
729 // Remove from BG layer (999)
730 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
731 this->controller->layers[999]->remove_surface(
732 this->controller->surfaces[id].get());
734 // Add to FG layer (1001)
735 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
736 this->controller->layers[1001]->add_surface(
737 this->controller->surfaces[id].get());
739 for (int j : this->surface_bg) {
740 HMI_DEBUG("wm", "Stored id:%d", j);
748 this->emit_visible(label);
749 this->emit_activated(label);
753 void App::deactivate(int id) {
754 auto ip = this->controller->sprops.find(id);
755 if (ip != this->controller->sprops.end() && ip->second.visibility != 0) {
757 this->lookup_name(id).value_or("unknown-name").c_str();
760 if ((0 == strcmp(label, "Radio"))
761 || (0 == strcmp(label, "MediaPlayer"))
762 || (0 == strcmp(label, "Music"))
763 || (0 == strcmp(label, "Navigation"))) {
766 this->surface_bg.push_back(id);
768 // Remove from FG layer (1001)
769 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
770 this->controller->layers[1001]->remove_surface(
771 this->controller->surfaces[id].get());
773 // Add to BG layer (999)
774 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
775 this->controller->layers[999]->add_surface(
776 this->controller->surfaces[id].get());
778 for (int j : surface_bg) {
779 HMI_DEBUG("wm", "Stored id:%d", j);
783 this->controller->surfaces[id]->set_visibility(0);
787 this->emit_deactivated(label);
788 this->emit_invisible(label);
792 void App::deactivate_main_surface() {
793 this->layers.main_surface = -1;
794 this->api_deactivate_surface(this->layers.main_surface_name.c_str());
797 bool App::can_split(struct LayoutState const &state, int new_id) {
798 if (state.main != -1 && state.main != new_id) {
799 auto new_id_layer = this->layers.get_layer_id(new_id).value();
800 auto current_id_layer = this->layers.get_layer_id(state.main).value();
802 // surfaces are on separate layers, don't bother.
803 if (new_id_layer != current_id_layer) {
807 std::string const &new_id_str = this->lookup_name(new_id).value();
808 std::string const &cur_id_str = this->lookup_name(state.main).value();
810 auto const &layer = this->layers.get_layer(new_id_layer);
812 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
814 if (layer->layouts.empty()) {
818 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
819 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
820 auto rem = std::regex(i->main_match);
821 if (std::regex_match(cur_id_str, rem)) {
822 // build the second one only if the first already matched
823 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
824 auto res = std::regex(i->sub_match);
825 if (std::regex_match(new_id_str, res)) {
826 HMI_DEBUG("wm", "layout matched!");
836 void App::try_layout(struct LayoutState & /*state*/,
837 struct LayoutState const &new_layout,
838 std::function<void(LayoutState const &nl)> apply) {
839 if (this->policy.layout_is_valid(new_layout)) {
847 void controller_hooks::surface_created(uint32_t surface_id) {
848 this->app->surface_created(surface_id);
851 void controller_hooks::surface_removed(uint32_t surface_id) {
852 this->app->surface_removed(surface_id);
855 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
858 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,