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) {
48 std::ifstream i(filename);
50 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
51 j = default_layers_json;
60 struct result<layer_map> load_layer_map(char const *filename) {
61 HMI_DEBUG("wm", "loading IDs from %s", filename);
63 auto j = file_to_json(filename);
65 return Err<layer_map>(j.unwrap_err());
67 json jids = j.unwrap();
69 return to_layer_map(jids);
78 App::App(wl::display *d)
87 pending_events(false),
91 auto l = load_layer_map(
92 this->config.get_string("layers.json").value().c_str());
94 this->layers = l.unwrap();
96 HMI_ERROR("wm", "%s", l.err().value());
99 } catch (std::exception &e) {
100 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
105 if (!this->display->ok()) {
109 if (this->layers.mapping.empty()) {
110 HMI_ERROR("wm", "No surface -> layer mapping loaded");
115 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
116 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
119 this->display->add_global_handler(
120 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
121 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
124 this->display->add_global_handler(
125 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
127 std::make_unique<struct compositor::controller>(r, name, v);
129 // Init controller hooks
130 this->controller->chooks = &this->chooks;
132 // This protocol needs the output, so lets just add our mapping here...
133 this->controller->add_proxy_to_id_mapping(
134 this->outputs.back()->proxy.get(),
135 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
136 this->outputs.back()->proxy.get())));
139 // First level objects
140 this->display->roundtrip();
141 // Second level objects
142 this->display->roundtrip();
143 // Third level objects
144 this->display->roundtrip();
146 return init_layers();
149 int App::dispatch_events() {
150 if (this->dispatch_events() == 0) {
154 int ret = this->display->dispatch();
156 HMI_ERROR("wm", "wl_display_dipatch() returned error %d",
157 this->display->get_error());
160 this->display->flush();
165 int App::dispatch_pending_events() {
166 if (this->pop_pending_events()) {
167 this->display->dispatch_pending();
173 bool App::pop_pending_events() {
175 return this->pending_events.compare_exchange_strong(
176 x, false, std::memory_order_consume);
179 void App::set_pending_events() {
180 this->pending_events.store(true, std::memory_order_release);
183 optional<int> App::lookup_id(char const *name) {
184 return this->id_alloc.lookup(std::string(name));
186 optional<std::string> App::lookup_name(int id) {
187 return this->id_alloc.lookup(id);
193 int App::init_layers() {
194 if (!this->controller) {
195 HMI_ERROR("wm", "ivi_controller global not available");
199 if (this->outputs.empty()) {
200 HMI_ERROR("wm", "no output was set up!");
204 auto &c = this->controller;
206 auto &o = this->outputs.front();
207 auto &s = c->screens.begin()->second;
208 auto &layers = c->layers;
210 // Write output dimensions to ivi controller...
211 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
219 // Quick and dirty setup of layers
220 for (auto const &i : this->layers.mapping) {
221 c->layer_create(i.second.layer_id, o->width, o->height);
222 auto &l = layers[i.second.layer_id];
223 l->set_destination_rectangle(0, 0, o->width, o->height);
224 l->set_visibility(1);
225 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
226 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
229 // Add layers to screen
230 s->set_render_order(this->layers.layers);
232 this->layout_commit();
237 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id) {
238 if (!this->controller->surface_exists(surface_id)) {
239 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
243 auto o_layer_id = this->layers.get_layer_id(surface_id);
246 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
250 uint32_t layer_id = *o_layer_id;
252 auto const &layer = this->layers.get_layer(layer_id);
253 auto rect = layer.value().rect;
254 auto &s = this->controller->surfaces[surface_id];
261 // less-than-0 values refer to MAX + 1 - $VALUE
262 // e.g. MAX is either screen width or height
264 w = this->controller->output_size.w + 1 + w;
267 h = this->controller->output_size.h + 1 + h;
270 if (sub_surface_id) {
271 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id)) {
273 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
274 surface_id, *sub_surface_id);
281 // split along major axis
290 auto &ss = this->controller->surfaces[*sub_surface_id];
292 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
293 *sub_surface_id, layer_id);
295 // configure surface to wxh dimensions
296 ss->set_configuration(w, h);
297 // set source reactangle, even if we should not need to set it.
298 ss->set_source_rectangle(0, 0, w, h);
299 // set destination to the display rectangle
300 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
304 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
307 // configure surface to wxh dimensions
308 s->set_configuration(w, h);
309 // set source reactangle, even if we should not need to set it.
310 s->set_source_rectangle(0, 0, w, h);
312 // set destination to the display rectangle
313 s->set_destination_rectangle(x, y, w, h);
315 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
316 surface_id, layer_id, x, y, w, h);
319 void App::layout_commit() {
320 this->controller->commit_changes();
321 this->display->flush();
324 char const *App::api_activate_surface(char const *drawing_name, char const *drawing_area) {
327 auto const &surface_id = this->lookup_id(drawing_name);
330 return "Surface does not exist";
333 if (!this->controller->surface_exists(*surface_id)) {
334 return "Surface does not exist in controller!";
337 auto layer_id = this->layers.get_layer_id(*surface_id);
340 return "Surface is not on any layer!";
343 auto o_state = *this->layers.get_layout_state(*surface_id);
345 if (o_state == nullptr) {
346 return "Could not find layer for surface";
349 struct LayoutState &state = *o_state;
351 // disable layers that are above our current layer
352 for (auto const &l : this->layers.mapping) {
353 if (l.second.layer_id <= *layer_id) {
358 if (l.second.state.main != -1) {
359 this->deactivate(l.second.state.main);
360 l.second.state.main = -1;
364 if (l.second.state.sub != -1) {
365 this->deactivate(l.second.state.sub);
366 l.second.state.sub = -1;
371 this->layout_commit();
375 auto layer = this->layers.get_layer(*layer_id);
377 if (state.main == -1) {
379 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
380 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
381 this->surface_set_layout(*surface_id);
384 // Commit for configuraton
385 this->layout_commit();
387 if (!(layer->is_normal_layout_only)) {
388 // Wait for configuration listener
389 controller->is_configured = false;
390 while (!(controller->is_configured)) {
391 dispatch_pending_events();
395 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
396 this->emit_syncdraw(drawing_name, str_area.c_str());
397 this->enqueue_flushdraw(state.main);
400 if (0 == strcmp(drawing_name, "HomeScreen")) {
402 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
403 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
404 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
405 this->emit_syncdraw(drawing_name, str_area.c_str());
406 this->enqueue_flushdraw(state.main);
409 bool can_split = this->can_split(state, *surface_id);
414 LayoutState{state.main, *surface_id},
415 [&] (LayoutState const &nl) {
416 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
418 std::move(*this->lookup_name(state.main));
420 this->surface_set_layout(state.main, surface_id);
421 if (state.sub != *surface_id) {
422 if (state.sub != -1) {
423 this->deactivate(state.sub);
428 // Commit for configuraton and visibility(0)
429 this->layout_commit();
431 // Wait for configuration listener
432 controller->is_configured = false;
433 while (!(controller->is_configured)) {
434 dispatch_pending_events();
437 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
438 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
439 this->emit_syncdraw(main.c_str(), str_area_main.c_str());
440 this->emit_syncdraw(drawing_name, str_area_sub.c_str());
441 this->enqueue_flushdraw(state.main);
442 this->enqueue_flushdraw(state.sub);
446 state, LayoutState{*surface_id}, [&] (LayoutState const &nl) {
447 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
449 this->surface_set_layout(*surface_id);
450 if (state.main != *surface_id) {
451 this->deactivate(state.main);
453 if (state.sub != -1) {
454 if (state.sub != *surface_id) {
455 this->deactivate(state.sub);
460 // Commit for configuraton and visibility(0)
461 this->layout_commit();
463 if (!(layer->is_normal_layout_only)) {
464 // Wait for configuration listener
465 controller->is_configured = false;
466 while (!(controller->is_configured)) {
467 dispatch_pending_events();
471 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
472 this->emit_syncdraw(drawing_name, str_area.c_str());
473 this->enqueue_flushdraw(state.main);
483 char const *App::api_deactivate_surface(char const *drawing_name) {
485 auto const &surface_id = this->lookup_id(drawing_name);
488 return "Surface does not exist";
491 if (*surface_id == this->layers.main_surface) {
492 return "Cannot deactivate main_surface";
495 auto o_state = *this->layers.get_layout_state(*surface_id);
497 if (o_state == nullptr) {
498 return "Could not find layer for surface";
501 struct LayoutState &state = *o_state;
503 if (state.main == -1) {
504 return "No surface active";
507 // Check against main_surface, main_surface_name is the configuration item.
508 if (*surface_id == this->layers.main_surface) {
509 HMI_DEBUG("wm", "Refusing to deactivate main_surface %d", *surface_id);
513 if (state.main == *surface_id) {
514 if (state.sub != -1) {
516 state, LayoutState{state.sub, -1}, [&] (LayoutState const &nl) {
517 std::string sub = std::move(*this->lookup_name(state.sub));
519 this->deactivate(*surface_id);
520 this->surface_set_layout(state.sub);
523 this->layout_commit();
524 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
525 this->emit_syncdraw(sub.c_str(), str_area.c_str());
526 this->enqueue_flushdraw(state.sub);
529 this->try_layout(state, LayoutState{-1, -1}, [&] (LayoutState const &nl) {
530 this->deactivate(*surface_id);
532 this->layout_commit();
535 } else if (state.sub == *surface_id) {
537 state, LayoutState{state.main, -1}, [&] (LayoutState const &nl) {
538 std::string main = std::move(*this->lookup_name(state.main));
540 this->deactivate(*surface_id);
541 this->surface_set_layout(state.main);
544 this->layout_commit();
545 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
546 this->emit_syncdraw(main.c_str(), str_area.c_str());
547 this->enqueue_flushdraw(state.main);
550 return "Surface is not active";
556 void App::enqueue_flushdraw(int surface_id) {
557 this->check_flushdraw(surface_id);
558 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
559 this->pending_end_draw.push_back(surface_id);
562 void App::check_flushdraw(int surface_id) {
563 auto i = std::find(std::begin(this->pending_end_draw),
564 std::end(this->pending_end_draw), surface_id);
565 if (i != std::end(this->pending_end_draw)) {
566 auto n = this->lookup_name(surface_id);
567 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
568 n ? n->c_str() : "unknown-name", surface_id);
569 std::swap(this->pending_end_draw[std::distance(
570 std::begin(this->pending_end_draw), i)],
571 this->pending_end_draw.back());
572 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
576 char const *App::api_enddraw(char const *drawing_name) {
577 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
578 auto n = this->lookup_name(this->pending_end_draw[i]);
579 if (n && *n == drawing_name) {
580 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
581 this->pending_end_draw.resize(iend - 1);
582 this->activate(this->pending_end_draw[i]);
583 this->layout_commit();
584 this->emit_flushdraw(drawing_name);
588 return "No EndDraw pending for surface";
591 void App::api_ping() { this->dispatch_pending_events(); }
596 void App::surface_created(uint32_t surface_id) {
597 auto layer_id = this->layers.get_layer_id(surface_id);
599 HMI_DEBUG("wm", "Newly created surface %d is not associated with any layer! Must be a XDG apps",
601 auto layer_id = this->layers.get_layer_id(XDG_DRAWING_NAME);
604 HMI_DEBUG("wm", "No role found for that XDG App");
608 this->layers.add_surface(surface_id, *layer_id);
609 // set the main_surface[_name] here and now
610 if (!this->layers.main_surface_name.empty() &&
611 this->layers.main_surface_name == XDG_DRAWING_NAME) {
612 this->layers.main_surface = surface_id;
613 HMI_DEBUG("wm", "Set main_surface id to %u", surface_id);
617 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
619 this->controller->layers[*layer_id]->add_surface(
620 this->controller->surfaces[surface_id].get());
622 // activate the main_surface right away
623 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
624 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
626 this->api_activate_surface(
627 this->lookup_name(surface_id).value_or("unknown-name").c_str());
631 void App::surface_removed(uint32_t surface_id) {
632 HMI_DEBUG("wm", "surface_id is %u", surface_id);
634 // We cannot normally deactivate the main_surface, so be explicit
636 if (int(surface_id) == this->layers.main_surface) {
637 this->deactivate_main_surface();
639 auto drawing_name = this->lookup_name(surface_id);
641 this->api_deactivate_surface(drawing_name->c_str());
645 this->id_alloc.remove_id(surface_id);
646 this->layers.remove_surface(surface_id);
649 void App::emit_activated(char const *label) {
650 this->api.send_event(kListEventName[Event_Active], label);
653 void App::emit_deactivated(char const *label) {
654 this->api.send_event(kListEventName[Event_Inactive], label);
657 void App::emit_syncdraw(char const *label, char const *area) {
658 this->api.send_event(kListEventName[Event_SyncDraw], label, area);
661 void App::emit_flushdraw(char const *label) {
662 this->api.send_event(kListEventName[Event_FlushDraw], label);
665 void App::emit_visible(char const *label, bool is_visible) {
666 this->api.send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
669 void App::emit_invisible(char const *label) {
670 return emit_visible(label, false);
673 void App::emit_visible(char const *label) { return emit_visible(label, true); }
675 result<int> App::api_request_surface(char const *drawing_name) {
676 auto lid = this->layers.get_layer_id(std::string(drawing_name));
678 // TODO: Do we need to put these applications on the App layer?
679 return Err<int>("Drawing name does not match any role");
682 auto rname = this->lookup_id(drawing_name);
684 // name does not exist yet, allocate surface id...
685 auto id = int(this->id_alloc.generate_id(drawing_name));
686 this->layers.add_surface(id, *lid);
688 // set the main_surface[_name] here and now
689 if (!this->layers.main_surface_name.empty() &&
690 this->layers.main_surface_name == drawing_name) {
691 this->layers.main_surface = id;
692 HMI_DEBUG("wm", "Set main_surface id to %u", id);
698 // Check currently registered drawing names if it is already there.
699 return Err<int>("Surface already present");
702 void App::activate(int id) {
703 auto ip = this->controller->sprops.find(id);
704 if (ip != this->controller->sprops.end()) {
705 this->controller->surfaces[id]->set_visibility(0);
706 this->layout_commit();
707 this->controller->surfaces[id]->set_visibility(1);
709 this->lookup_name(id).value_or("unknown-name").c_str();
710 this->emit_visible(label);
711 this->emit_activated(label);
715 void App::deactivate(int id) {
716 auto ip = this->controller->sprops.find(id);
717 if (ip != this->controller->sprops.end() && ip->second.visibility != 0) {
718 this->controller->surfaces[id]->set_visibility(0);
720 this->lookup_name(id).value_or("unknown-name").c_str();
721 this->emit_deactivated(label);
722 this->emit_invisible(label);
726 void App::deactivate_main_surface() {
727 this->layers.main_surface = -1;
728 this->api_deactivate_surface(this->layers.main_surface_name.c_str());
731 bool App::can_split(struct LayoutState const &state, int new_id) {
732 if (state.main != -1 && state.main != new_id) {
733 auto new_id_layer = this->layers.get_layer_id(new_id).value();
734 auto current_id_layer = this->layers.get_layer_id(state.main).value();
736 // surfaces are on separate layers, don't bother.
737 if (new_id_layer != current_id_layer) {
741 std::string const &new_id_str = this->lookup_name(new_id).value();
742 std::string const &cur_id_str = this->lookup_name(state.main).value();
744 auto const &layer = this->layers.get_layer(new_id_layer);
746 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
748 if (layer->layouts.empty()) {
752 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++) {
753 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
754 auto rem = std::regex(i->main_match);
755 if (std::regex_match(cur_id_str, rem)) {
756 // build the second one only if the first already matched
757 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
758 auto res = std::regex(i->sub_match);
759 if (std::regex_match(new_id_str, res)) {
760 HMI_DEBUG("wm", "layout matched!");
770 void App::try_layout(struct LayoutState & /*state*/,
771 struct LayoutState const &new_layout,
772 std::function<void(LayoutState const &nl)> apply) {
773 if (this->policy.layout_is_valid(new_layout)) {
781 void controller_hooks::surface_created(uint32_t surface_id) {
782 this->app->surface_created(surface_id);
785 void controller_hooks::surface_removed(uint32_t surface_id) {
786 this->app->surface_removed(surface_id);
789 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
792 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,