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.
20 #include "window_manager.hpp"
21 #include "../include/json.hpp"
22 #include "applist.hpp"
26 #include <systemd/sd-event.h>
32 static const unsigned kTimeOut = 3000000UL; /* 3s */
34 /* DrawingArea name used by "{layout}.{area}" */
35 const char kNameLayoutNormal[] = "normal";
36 const char kNameLayoutSplit[] = "split";
37 const char kNameAreaFull[] = "full";
38 const char kNameAreaMain[] = "main";
39 const char kNameAreaSub[] = "sub";
41 /* Key for json obejct */
42 const char kKeyDrawingName[] = "drawing_name";
43 const char kKeyDrawingArea[] = "drawing_area";
44 const char kKeyDrawingRect[] = "drawing_rect";
45 const char kKeyX[] = "x";
46 const char kKeyY[] = "y";
47 const char kKeyWidth[] = "width";
48 const char kKeyHeight[] = "height";
49 const char kKeyWidthPixel[] = "width_pixel";
50 const char kKeyHeightPixel[] = "height_pixel";
51 const char kKeyWidthMm[] = "width_mm";
52 const char kKeyHeightMm[] = "height_mm";
54 static sd_event_source *g_timer_ev_src = nullptr;
56 static AppList g_app_list;
63 result<json> file_to_json(char const *filename)
66 std::ifstream i(filename);
69 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
70 j = default_layers_json;
80 struct result<layer_map> load_layer_map(char const *filename)
82 HMI_DEBUG("wm", "loading IDs from %s", filename);
84 auto j = file_to_json(filename);
87 return Err<layer_map>(j.unwrap_err());
89 json jids = j.unwrap();
91 return to_layer_map(jids);
94 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
96 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
97 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
103 void WindowManager::timerHandler()
105 unsigned req_num = g_app_list.currentRequestNumber();
106 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
107 g_app_list.reqDump();
108 g_app_list.removeRequest(req_num);
109 this->processNextRequest();
112 void WindowManager::removeClient(const std::string &appid)
114 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
115 g_app_list.removeClient(appid);
118 void WindowManager::exceptionProcessForTransition()
120 unsigned req_num = g_app_list.currentRequestNumber();
121 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
122 g_app_list.removeRequest(req_num);
123 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
124 this->processNextRequest();
127 bool WindowManager::subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname)
129 if(!g_app_list.contains(appid)){
130 HMI_DEBUG("wm", "Client %s is not registered", appid.c_str());
133 auto client = g_app_list.lookUpClient(appid);
134 return client->subscribe(req, evname);
140 WindowManager::WindowManager(wl::display *d)
147 pending_events(false)
149 char const *path_layers_json = getenv("AFM_APP_INSTALL_DIR");
151 if (!path_layers_json)
153 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
154 path = std::string(path_layers_json);
158 path = std::string(path_layers_json) + std::string("/etc/layers.json");
164 auto l = load_layer_map(path.c_str());
167 this->layers = l.unwrap();
171 HMI_ERROR("wm", "%s", l.err().value());
175 catch (std::exception &e)
177 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
181 int WindowManager::init()
183 if (!this->display->ok())
188 if (this->layers.mapping.empty())
190 HMI_ERROR("wm", "No surface -> layer mapping loaded");
195 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
197 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
200 this->display->add_global_handler(
201 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
202 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
205 this->display->add_global_handler(
206 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
208 std::make_unique<struct compositor::controller>(r, name, v);
210 // Init controller hooks
211 this->controller->chooks = &this->chooks;
213 // This protocol needs the output, so lets just add our mapping here...
214 this->controller->add_proxy_to_id_mapping(
215 this->outputs.back()->proxy.get(),
216 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
217 this->outputs.back()->proxy.get())));
220 this->controller->create_screen(this->outputs.back()->proxy.get());
222 // Set display to controller
223 this->controller->display = this->display;
226 // First level objects
227 this->display->roundtrip();
228 // Second level objects
229 this->display->roundtrip();
230 // Third level objects
231 this->display->roundtrip();
233 return init_layers();
236 int WindowManager::dispatch_pending_events()
238 if (this->pop_pending_events())
240 this->display->dispatch_pending();
246 bool WindowManager::pop_pending_events()
249 return this->pending_events.compare_exchange_strong(
250 x, false, std::memory_order_consume);
253 void WindowManager::set_pending_events()
255 this->pending_events.store(true, std::memory_order_release);
258 optional<int> WindowManager::lookup_id(char const *name)
260 return this->id_alloc.lookup(std::string(name));
262 optional<std::string> WindowManager::lookup_name(int id)
264 return this->id_alloc.lookup(id);
270 int WindowManager::init_layers()
272 if (!this->controller)
274 HMI_ERROR("wm", "ivi_controller global not available");
278 if (this->outputs.empty())
280 HMI_ERROR("wm", "no output was set up!");
284 auto &c = this->controller;
286 auto &o = this->outputs.front();
287 auto &s = c->screens.begin()->second;
288 auto &layers = c->layers;
290 // Write output dimensions to ivi controller...
291 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
292 c->physical_size = compositor::size{uint32_t(o->physical_width),
293 uint32_t(o->physical_height)};
301 // Quick and dirty setup of layers
302 for (auto const &i : this->layers.mapping)
304 c->layer_create(i.second.layer_id, o->width, o->height);
305 auto &l = layers[i.second.layer_id];
306 l->set_destination_rectangle(0, 0, o->width, o->height);
307 l->set_visibility(1);
308 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
309 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
312 // Add layers to screen
313 s->set_render_order(this->layers.layers);
315 this->layout_commit();
317 this->layers.setupArea(o->width, o->height);
322 void WindowManager::surface_set_layout(int surface_id, const std::string& area)
324 if (!this->controller->surface_exists(surface_id))
326 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
330 auto o_layer_id = this->layers.get_layer_id(surface_id);
334 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
338 uint32_t layer_id = *o_layer_id;
340 auto const &layer = this->layers.get_layer(layer_id);
341 auto rect = this->layers.getAreaSize(area);
342 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
343 rect.x, rect.y, rect.w, rect.h);
344 auto &s = this->controller->surfaces[surface_id];
351 // less-than-0 values refer to MAX + 1 - $VALUE
352 // e.g. MAX is either screen width or height
355 w = this->controller->output_size.w + 1 + w;
359 h = this->controller->output_size.h + 1 + h;
362 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
365 // set destination to the display rectangle
366 s->set_destination_rectangle(x, y, w, h);
368 // update area information
369 this->area_info[surface_id].x = x;
370 this->area_info[surface_id].y = y;
371 this->area_info[surface_id].w = w;
372 this->area_info[surface_id].h = h;
374 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
375 surface_id, layer_id, x, y, w, h);
378 void WindowManager::layout_commit()
380 this->controller->commit_changes();
381 this->display->flush();
384 void WindowManager::setTimer()
386 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
387 if (g_timer_ev_src == nullptr)
389 // firsttime set into sd_event
390 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
391 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
394 HMI_ERROR("wm", "Could't set timer");
399 // update timer limitation after second time
400 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
401 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
405 void WindowManager::stopTimer()
407 unsigned req_num = g_app_list.currentRequestNumber();
408 HMI_SEQ_DEBUG(req_num, "Timer stop");
409 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
412 HMI_SEQ_ERROR(req_num, "Timer stop failed");
416 WMError WindowManager::doTransition(unsigned req_num)
418 HMI_SEQ_DEBUG(req_num, "check policy");
419 WMError ret = this->checkPolicy(req_num);
420 if (ret != WMError::SUCCESS)
424 HMI_SEQ_DEBUG(req_num, "Start transition.");
425 ret = this->startTransition(req_num);
429 WMError WindowManager::checkPolicy(unsigned req_num)
434 // get current trigger
437 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
438 auto trigger = g_app_list.getRequest(req_num, &found);
441 ret = WMError::NO_ENTRY;
444 std::string req_area = trigger.area;
446 // >>>> Compatible with current window manager until policy manager coming
447 if (trigger.task == Task::TASK_ALLOCATE)
449 HMI_SEQ_DEBUG(req_num, "Check split or not");
450 const char *msg = this->check_surface_exist(trigger.role.c_str());
454 HMI_SEQ_ERROR(req_num, msg);
455 ret = WMError::LAYOUT_CHANGE_FAIL;
459 auto const &surface_id = this->lookup_id(trigger.role.c_str());
460 auto o_state = *this->layers.get_layout_state(*surface_id);
461 struct LayoutState &state = *o_state;
463 unsigned curernt_sid = state.main;
464 split = this->can_split(state, *surface_id);
468 HMI_SEQ_DEBUG(req_num, "Split happens");
469 // Get current visible role
470 std::string add_role = this->lookup_name(state.main).value();
472 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
473 // Change request area
474 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
475 HMI_SEQ_NOTICE(req_num, "Change request area from %s to %s, because split happens",
476 trigger.area.c_str(), req_area.c_str());
477 // set another action
478 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
481 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %d", curernt_sid);
482 ret = WMError::NOT_REGISTERED;
485 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
486 add_name.c_str(), add_role.c_str(), add_area.c_str());
488 bool end_draw_finished = false;
489 WMAction split_action{
493 TaskVisible::VISIBLE,
495 WMError ret = g_app_list.setAction(req_num, split_action);
496 if (ret != WMError::SUCCESS)
498 HMI_SEQ_ERROR(req_num, "Failed to set action");
501 g_app_list.reqDump();
506 HMI_SEQ_DEBUG(req_num, "split doesn't happen");
509 // Set invisible task(Remove if policy manager finish)
510 ret = this->setInvisibleTask(trigger.role, split);
511 if(ret != WMError::SUCCESS)
513 HMI_SEQ_ERROR(req_num, "Failed to set invisible task: %s", errorDescription(ret));
517 /* get new status from Policy Manager */
518 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
519 if(trigger.role == "HomeScreen")
521 // TODO : Remove when Policy Manager completed
522 HMI_SEQ_NOTICE(req_num, "Hack. This process will be removed. Change HomeScreen code!!");
523 req_area = "fullscreen";
525 TaskVisible task_visible =
526 (trigger.task == Task::TASK_ALLOCATE) ? TaskVisible::VISIBLE : TaskVisible::INVISIBLE;
528 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, task_visible);
529 g_app_list.reqDump();
534 WMError WindowManager::startTransition(unsigned req_num)
536 bool sync_draw_happen = false;
538 WMError ret = WMError::SUCCESS;
539 auto actions = g_app_list.getActions(req_num, &found);
542 ret = WMError::NO_ENTRY;
543 HMI_SEQ_ERROR(req_num,
544 "Window Manager bug :%s : Action is not set", errorDescription(ret));
548 for (const auto &action : actions)
550 if (action.visible != TaskVisible::INVISIBLE)
552 sync_draw_happen = true;
553 this->emit_syncdraw(action.role, action.area);
554 /* TODO: emit event for app not subscriber
555 if(g_app_list.contains(y.appid))
556 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
560 if (sync_draw_happen)
566 // deactivate only, no syncDraw
567 // Make it deactivate here
568 for (const auto &x : actions)
570 if (g_app_list.contains(x.appid))
572 auto client = g_app_list.lookUpClient(x.appid);
573 this->deactivate(client->surfaceID(x.role));
576 ret = NO_LAYOUT_CHANGE;
581 WMError WindowManager::setInvisibleTask(const std::string &role, bool split)
583 unsigned req_num = g_app_list.currentRequestNumber();
584 HMI_SEQ_DEBUG(req_num, "set current visible app to invisible task");
585 // This task is copied from original actiavete surface
586 const char *drawing_name = role.c_str();
587 auto const &surface_id = this->lookup_id(drawing_name);
588 auto layer_id = this->layers.get_layer_id(*surface_id);
589 auto o_state = *this->layers.get_layout_state(*surface_id);
590 struct LayoutState &state = *o_state;
591 std::string add_name, add_role;
592 std::string add_area = "";
594 TaskVisible task_visible = TaskVisible::INVISIBLE;
595 bool end_draw_finished = true;
598 for (auto const &l : this->layers.mapping)
600 if (l.second.layer_id <= *layer_id)
604 HMI_DEBUG("wm", "debug: main %d , sub : %d", l.second.state.main, l.second.state.sub);
605 if (l.second.state.main != -1)
607 //this->deactivate(l.second.state.main);
608 surface = l.second.state.main;
609 add_role = *this->id_alloc.lookup(surface);
610 add_name = g_app_list.getAppID(surface, add_role, &found);
612 return WMError::NOT_REGISTERED;
614 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
615 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
616 g_app_list.setAction(req_num, act);
617 l.second.state.main = -1;
620 if (l.second.state.sub != -1)
622 //this->deactivate(l.second.state.sub);
623 surface = l.second.state.sub;
624 add_role = *this->id_alloc.lookup(surface);
625 add_name = g_app_list.getAppID(surface, add_role, &found);
628 return WMError::NOT_REGISTERED;
630 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
631 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
632 g_app_list.setAction(req_num, act);
633 l.second.state.sub = -1;
637 // change current state here, but this is hack
638 auto layer = this->layers.get_layer(*layer_id);
640 if (state.main == -1)
642 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
646 if (0 != strcmp(drawing_name, "HomeScreen"))
650 if (state.sub != *surface_id)
654 //this->deactivate(state.sub);
657 std::move(*this->id_alloc.lookup(state.sub));
658 deact_sub.area = add_area;
659 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
662 HMI_SEQ_ERROR(req_num, "App doesn't exist for role : %s",
663 deact_sub.role.c_str());
664 return WMError::NOT_REGISTERED;
666 deact_sub.visible = task_visible;
667 deact_sub.end_draw_finished = end_draw_finished;
668 HMI_SEQ_DEBUG(req_num, "Set invisible task for %s", deact_sub.appid.c_str());
669 g_app_list.setAction(req_num, deact_sub);
672 //state = LayoutState{state.main, *surface_id};
676 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
678 //this->surface_set_layout(*surface_id);
679 if (state.main != *surface_id)
681 // this->deactivate(state.main);
683 deact_main.role = std::move(*this->id_alloc.lookup(state.main));
685 deact_main.area = add_area;
686 deact_main.appid = g_app_list.getAppID(state.main, deact_main.role, &found);
689 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
690 return WMError::NOT_REGISTERED;
692 deact_main.visible = task_visible;
693 deact_main.end_draw_finished = end_draw_finished;
694 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
695 g_app_list.setAction(req_num, deact_main);
699 if (state.sub != *surface_id)
701 //this->deactivate(state.sub);
703 deact_sub.role = std::move(*this->id_alloc.lookup(state.sub));
705 deact_sub.area = add_area;
706 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
709 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
710 return WMError::NOT_REGISTERED;
712 deact_sub.visible = task_visible;
713 deact_sub.end_draw_finished = end_draw_finished;
714 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
715 g_app_list.setAction(req_num, deact_sub);
718 //state = LayoutState{*surface_id};
722 return WMError::SUCCESS;
725 const char *WindowManager::check_surface_exist(const char *drawing_name)
727 auto const &surface_id = this->lookup_id(drawing_name);
730 //reply("Surface does not exist");
731 return "Surface does not exist";
734 if (!this->controller->surface_exists(*surface_id))
736 //reply("Surface does not exist in controller!");
737 return "Surface does not exist in controller!";
740 auto layer_id = this->layers.get_layer_id(*surface_id);
744 //reply("Surface is not on any layer!");
745 return "Surface is not on any layer!";
748 auto o_state = *this->layers.get_layout_state(*surface_id);
750 if (o_state == nullptr)
752 //reply("Could not find layer for surface");
753 return "Could not find layer for surface";
756 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
761 WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
762 Task task, unsigned* req_num)
764 if (!g_app_list.contains(appid))
766 return WMError::NOT_REGISTERED;
769 auto client = g_app_list.lookUpClient(appid);
774 unsigned current = g_app_list.currentRequestNumber();
775 unsigned requested_num = g_app_list.getRequestNumber(appid);
776 if (requested_num != 0)
778 HMI_SEQ_INFO(requested_num,
779 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
783 WMRequest req = WMRequest(appid, role, area, task);
784 unsigned new_req = g_app_list.addRequest(req);
786 g_app_list.reqDump();
788 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
790 return WMError::SUCCESS;
793 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
794 char const *drawing_area, const reply_func &reply)
798 std::string id = appid;
799 std::string role = drawing_name;
800 std::string area = drawing_area;
801 Task task = Task::TASK_ALLOCATE;
802 unsigned req_num = 0;
803 WMError ret = WMError::UNKNOWN;
805 ret = this->setRequest(id, role, area, task, &req_num);
807 if(ret != WMError::SUCCESS)
809 HMI_ERROR("wm", errorDescription(ret));
810 reply("Failed to set request");
815 if (req_num != g_app_list.currentRequestNumber())
817 // Add request, then invoked after the previous task is finished
818 HMI_SEQ_DEBUG(req_num, "request is accepted");
825 ret = this->doTransition(req_num);
827 if (ret != WMError::SUCCESS)
830 HMI_SEQ_ERROR(req_num, errorDescription(ret));
831 g_app_list.removeRequest(req_num);
832 this->processNextRequest();
836 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
837 const reply_func &reply)
844 std::string id = appid;
845 std::string role = drawing_name;
846 std::string area = ""; //drawing_area;
847 Task task = Task::TASK_RELEASE;
848 unsigned req_num = 0;
849 WMError ret = WMError::UNKNOWN;
851 ret = this->setRequest(id, role, area, task, &req_num);
853 if (ret != WMError::SUCCESS)
855 HMI_ERROR("wm", errorDescription(ret));
856 reply("Failed to set request");
861 if (req_num != g_app_list.currentRequestNumber())
863 // Add request, then invoked after the previous task is finished
864 HMI_SEQ_DEBUG(req_num, "request is accepted");
871 ret = this->doTransition(req_num);
873 if (ret != WMError::SUCCESS)
876 HMI_SEQ_ERROR(req_num, errorDescription(ret));
877 g_app_list.removeRequest(req_num);
878 this->processNextRequest();
882 WMError WindowManager::doEndDraw(unsigned req_num)
886 auto actions = g_app_list.getActions(req_num, &found);
887 WMError ret = WMError::SUCCESS;
890 ret = WMError::NO_ENTRY;
894 HMI_SEQ_INFO(req_num, "do endDraw");
896 // layout change and make it visible
897 for (const auto &act : actions)
900 if(!g_app_list.contains(act.appid)){
901 ret = WMError::NOT_REGISTERED;
903 ret = this->layoutChange(act);
904 if(ret != WMError::SUCCESS)
906 HMI_SEQ_WARNING(req_num,
907 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
910 ret = this->visibilityChange(act);
911 if (ret != WMError::SUCCESS)
913 HMI_SEQ_WARNING(req_num,
914 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
917 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
918 //this->lm_enddraw(act.role.c_str());
921 // Change current state
922 this->changeCurrentState(req_num);
924 HMI_SEQ_INFO(req_num, "emit flushDraw");
926 for(const auto &act_flush : actions)
928 if(act_flush.visible != TaskVisible::INVISIBLE)
930 this->emit_flushdraw(act_flush.role.c_str());
937 WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
939 this->surface_set_layout(surface, area);
941 return WMError::SUCCESS;
944 WMError WindowManager::layoutChange(const WMAction &action)
946 if (action.visible == TaskVisible::INVISIBLE)
948 // Visibility is not change -> no redraw is required
949 return WMError::SUCCESS;
951 auto client = g_app_list.lookUpClient(action.appid);
952 unsigned surface = client->surfaceID(action.role);
955 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
956 "client doesn't have surface with role(%s)", action.role.c_str());
957 return WMError::NOT_REGISTERED;
960 WMError ret = this->setSurfaceSize(surface, action.area);
964 WMError WindowManager::visibilityChange(const WMAction &action)
966 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
967 if(!g_app_list.contains(action.appid)){
968 return WMError::NOT_REGISTERED;
970 auto client = g_app_list.lookUpClient(action.appid);
971 unsigned surface = client->surfaceID(action.role);
974 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
975 "client doesn't have surface with role(%s)", action.role.c_str());
976 return WMError::NOT_REGISTERED;
979 if (action.visible != TaskVisible::INVISIBLE)
981 this->activate(surface); // Layout Manager task
985 this->deactivate(surface); // Layout Manager task
987 return WMError::SUCCESS;
990 WMError WindowManager::changeCurrentState(unsigned req_num)
992 HMI_SEQ_DEBUG(req_num, "Change current layout state");
993 bool trigger_found = false, action_found = false;
994 auto trigger = g_app_list.getRequest(req_num, &trigger_found);
995 auto actions = g_app_list.getActions(req_num, &action_found);
996 if (!trigger_found || !action_found)
998 HMI_SEQ_ERROR(req_num, "Action not found");
999 return WMError::LAYOUT_CHANGE_FAIL;
1002 // Layout state reset
1003 struct LayoutState reset_state{-1, -1};
1004 HMI_SEQ_DEBUG(req_num,"Reset layout state");
1005 for (const auto &action : actions)
1007 if(!g_app_list.contains(action.appid)){
1008 return WMError::NOT_REGISTERED;
1010 auto client = g_app_list.lookUpClient(action.appid);
1011 auto pCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1012 if(pCurState == nullptr)
1014 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1017 pCurState->main = reset_state.main;
1018 pCurState->sub = reset_state.sub;
1021 HMI_SEQ_DEBUG(req_num, "Change state");
1022 for (const auto &action : actions)
1024 auto client = g_app_list.lookUpClient(action.appid);
1025 auto pLayerCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1026 if (pLayerCurState == nullptr)
1028 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1033 if (action.visible != TaskVisible::INVISIBLE)
1035 surface = (int)client->surfaceID(action.role);
1036 HMI_SEQ_INFO(req_num, "Change %s surface : %d, state visible area : %s",
1037 action.role.c_str(), surface, action.area.c_str());
1038 // visible == true -> layout changes
1039 if(action.area == "normal.full" || action.area == "split.main")
1041 pLayerCurState->main = surface;
1043 else if (action.area == "split.sub")
1045 pLayerCurState->sub = surface;
1050 pLayerCurState->main = surface;
1055 return WMError::SUCCESS;
1058 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
1060 std::string id = appid;
1061 std::string role = drawing_name;
1062 unsigned current_req = g_app_list.currentRequestNumber();
1063 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
1067 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
1071 if (g_app_list.endDrawFullfilled(current_req))
1073 // do task for endDraw
1075 WMError ret = this->doEndDraw(current_req);
1077 if(ret != WMError::SUCCESS)
1079 //this->emit_error();
1081 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
1083 g_app_list.removeRequest(current_req);
1085 this->processNextRequest();
1089 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
1094 void WindowManager::processNextRequest()
1097 g_app_list.reqDump();
1098 unsigned req_num = g_app_list.currentRequestNumber();
1099 if (g_app_list.haveRequest())
1101 HMI_SEQ_DEBUG(req_num, "Process next request");
1102 WMError rc = doTransition(req_num);
1103 if (rc != WMError::SUCCESS)
1105 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1110 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1114 void WindowManager::api_ping() { this->dispatch_pending_events(); }
1116 void WindowManager::send_event(char const *evname, char const *label)
1118 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
1120 json_object *j = json_object_new_object();
1121 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1123 int ret = afb_event_push(this->map_afb_event[evname], j);
1126 HMI_DEBUG("wm", "afb_event_push failed: %m");
1130 void WindowManager::send_event(char const *evname, char const *label, char const *area,
1131 int x, int y, int w, int h)
1133 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
1134 __func__, evname, label, area, x, y, w, h);
1136 json_object *j_rect = json_object_new_object();
1137 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
1138 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
1139 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
1140 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
1142 json_object *j = json_object_new_object();
1143 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1144 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
1145 json_object_object_add(j, kKeyDrawingRect, j_rect);
1147 int ret = afb_event_push(this->map_afb_event[evname], j);
1150 HMI_DEBUG("wm", "afb_event_push failed: %m");
1157 void WindowManager::surface_created(uint32_t surface_id)
1159 auto layer_id = this->layers.get_layer_id(surface_id);
1162 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
1167 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
1169 this->controller->layers[*layer_id]->add_surface(surface_id);
1170 this->layout_commit();
1173 void WindowManager::surface_removed(uint32_t surface_id)
1175 HMI_DEBUG("wm", "surface_id is %u", surface_id);
1177 g_app_list.removeSurface(surface_id);
1180 void WindowManager::emit_activated(char const *label)
1182 this->send_event(kListEventName[Event_Active], label);
1185 void WindowManager::emit_deactivated(char const *label)
1187 this->send_event(kListEventName[Event_Inactive], label);
1190 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1192 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1195 void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
1197 compositor::rect rect = this->layers.getAreaSize(area);
1198 //this->lm_get_area_info(area, &x, &y, &w, &h);
1199 this->send_event(kListEventName[Event_SyncDraw],
1200 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
1203 void WindowManager::emit_flushdraw(char const *label)
1205 this->send_event(kListEventName[Event_FlushDraw], label);
1208 void WindowManager::emit_visible(char const *label, bool is_visible)
1210 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1213 void WindowManager::emit_invisible(char const *label)
1215 return emit_visible(label, false);
1218 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
1220 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
1222 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1226 * register drawing_name as fallback and make it displayed.
1228 lid = this->layers.get_layer_id(std::string("Fallback"));
1229 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1232 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1236 auto rname = this->lookup_id(drawing_name);
1239 // name does not exist yet, allocate surface id...
1240 auto id = int(this->id_alloc.generate_id(drawing_name));
1241 this->layers.add_surface(id, *lid);
1243 // set the main_surface[_name] here and now
1244 if (!this->layers.main_surface_name.empty() &&
1245 this->layers.main_surface_name == drawing_name)
1247 this->layers.main_surface = id;
1248 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1251 // add client into the db
1252 std::string appid_str(appid);
1253 std::string role(drawing_name);
1254 //g_app_list.addClient(appid_str, role);
1255 g_app_list.addClient(appid_str, *lid, id, role);
1260 // Check currently registered drawing names if it is already there.
1261 return Err<int>("Surface already present");
1264 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
1269 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1270 unsigned sid = std::stol(ivi_id);
1275 * register drawing_name as fallback and make it displayed.
1277 lid = this->layers.get_layer_id(std::string("Fallback"));
1278 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1281 return "Drawing name does not match any role, Fallback is disabled";
1285 auto rname = this->lookup_id(drawing_name);
1289 return "Surface already present";
1292 // register pair drawing_name and ivi_id
1293 this->id_alloc.register_name_id(drawing_name, sid);
1294 this->layers.add_surface(sid, *lid);
1296 // this surface is already created
1297 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1299 this->controller->layers[*lid]->add_surface(sid);
1300 this->layout_commit();
1305 result<json_object *> WindowManager::api_get_display_info()
1308 if (!this->controller)
1310 return Err<json_object *>("ivi_controller global not available");
1314 compositor::size o_size = this->controller->output_size;
1315 compositor::size p_size = this->controller->physical_size;
1317 json_object *object = json_object_new_object();
1318 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1319 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1320 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1321 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1323 return Ok<json_object *>(object);
1326 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
1328 HMI_DEBUG("wm", "called");
1330 // Check drawing name, surface/layer id
1331 auto const &surface_id = this->lookup_id(drawing_name);
1334 return Err<json_object *>("Surface does not exist");
1337 if (!this->controller->surface_exists(*surface_id))
1339 return Err<json_object *>("Surface does not exist in controller!");
1342 auto layer_id = this->layers.get_layer_id(*surface_id);
1345 return Err<json_object *>("Surface is not on any layer!");
1348 auto o_state = *this->layers.get_layout_state(*surface_id);
1349 if (o_state == nullptr)
1351 return Err<json_object *>("Could not find layer for surface");
1354 struct LayoutState &state = *o_state;
1355 if ((state.main != *surface_id) && (state.sub != *surface_id))
1357 return Err<json_object *>("Surface is inactive");
1360 // Set area rectangle
1361 compositor::rect area_info = this->area_info[*surface_id];
1362 json_object *object = json_object_new_object();
1363 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1364 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1365 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1366 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1368 return Ok<json_object *>(object);
1371 void WindowManager::activate(int id)
1373 auto ip = this->controller->sprops.find(id);
1374 if (ip != this->controller->sprops.end())
1376 this->controller->surfaces[id]->set_visibility(1);
1378 this->lookup_name(id).value_or("unknown-name").c_str();
1381 if ((0 == strcmp(label, "Radio")) ||
1382 (0 == strcmp(label, "MediaPlayer")) ||
1383 (0 == strcmp(label, "Music")) ||
1384 (0 == strcmp(label, "Navigation")))
1386 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1391 this->surface_bg.erase(i);
1393 // Remove from BG layer (999)
1394 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1395 this->controller->layers[999]->remove_surface(id);
1397 // Add to FG layer (1001)
1398 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1399 this->controller->layers[1001]->add_surface(id);
1401 for (int j : this->surface_bg)
1403 HMI_DEBUG("wm", "Stored id:%d", j);
1410 this->layout_commit();
1412 this->emit_visible(label);
1413 this->emit_activated(label);
1417 void WindowManager::deactivate(int id)
1419 auto ip = this->controller->sprops.find(id);
1420 if (ip != this->controller->sprops.end())
1423 this->lookup_name(id).value_or("unknown-name").c_str();
1426 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1430 this->surface_bg.push_back(id);
1432 // Remove from FG layer (1001)
1433 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1434 this->controller->layers[1001]->remove_surface(id);
1436 // Add to BG layer (999)
1437 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1438 this->controller->layers[999]->add_surface(id);
1440 for (int j : surface_bg)
1442 HMI_DEBUG("wm", "Stored id:%d", j);
1447 this->controller->surfaces[id]->set_visibility(0);
1451 this->emit_deactivated(label);
1452 this->emit_invisible(label);
1456 bool WindowManager::can_split(struct LayoutState const &state, int new_id)
1458 if (state.main != -1 && state.main != new_id)
1460 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1461 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1463 // surfaces are on separate layers, don't bother.
1464 if (new_id_layer != current_id_layer)
1469 std::string const &new_id_str = this->lookup_name(new_id).value();
1470 std::string const &cur_id_str = this->lookup_name(state.main).value();
1472 auto const &layer = this->layers.get_layer(new_id_layer);
1474 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1476 if (layer->layouts.empty())
1481 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1483 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1484 auto rem = std::regex(i->main_match);
1485 if (std::regex_match(cur_id_str, rem))
1487 // build the second one only if the first already matched
1488 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1489 auto res = std::regex(i->sub_match);
1490 if (std::regex_match(new_id_str, res))
1492 HMI_DEBUG("wm", "layout matched!");
1505 void controller_hooks::surface_created(uint32_t surface_id)
1507 this->app->surface_created(surface_id);
1510 void controller_hooks::surface_removed(uint32_t surface_id)
1512 this->app->surface_removed(surface_id);
1515 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1518 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,