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_ivi_wm.hpp"
29 #include <json-c/json.h>
38 #include "wm_client.hpp"
39 #include "applist.hpp"
43 #include <systemd/sd-event.h>
49 static const unsigned kTimeOut = 3000000UL; /* 3s */
51 /* DrawingArea name used by "{layout}.{area}" */
52 const char kNameLayoutNormal[] = "normal";
53 const char kNameLayoutSplit[] = "split";
54 const char kNameAreaFull[] = "full";
55 const char kNameAreaMain[] = "main";
56 const char kNameAreaSub[] = "sub";
58 /* Key for json obejct */
59 const char kKeyDrawingName[] = "drawing_name";
60 const char kKeyDrawingArea[] = "drawing_area";
61 const char kKeyDrawingRect[] = "drawing_rect";
62 const char kKeyX[] = "x";
63 const char kKeyY[] = "y";
64 const char kKeyWidth[] = "width";
65 const char kKeyHeight[] = "height";
66 const char kKeyWidthPixel[] = "width_pixel";
67 const char kKeyHeightPixel[] = "height_pixel";
68 const char kKeyWidthMm[] = "width_mm";
69 const char kKeyHeightMm[] = "height_mm";
71 static sd_event_source *g_timer_ev_src = nullptr;
73 static AppList g_app_list;
80 result<json> file_to_json(char const *filename)
83 std::ifstream i(filename);
86 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
87 j = default_layers_json;
97 struct result<layer_map> load_layer_map(char const *filename)
99 HMI_DEBUG("wm", "loading IDs from %s", filename);
101 auto j = file_to_json(filename);
104 return Err<layer_map>(j.unwrap_err());
106 json jids = j.unwrap();
108 return to_layer_map(jids);
112 processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
114 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
115 reinterpret_cast<wm::App *>(userdata)->timerHandler();
121 void App::timerHandler()
123 unsigned req_num = g_app_list.currentRequestNumber();
124 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
125 g_app_list.reqDump();
126 g_app_list.removeRequest(req_num);
127 this->processNextRequest();
130 void App::removeClient(const std::string &appid)
132 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
133 g_app_list.removeClient(appid);
136 void App::exeptionProcessForTransition()
138 unsigned req_num = g_app_list.currentRequestNumber();
139 HMI_SEQ_NOTICE(req_num, "Process exeption handling for request. Remove current request %d", req_num);
140 g_app_list.removeRequest(req_num);
141 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
142 this->processNextRequest();
145 bool App::subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname)
147 if(!g_app_list.contains(appid)){
148 HMI_DEBUG("wm", "Client %s is not registered", appid.c_str());
151 auto client = g_app_list.lookUpClient(appid);
152 return client->subscribe(req, evname);
158 App::App(wl::display *d)
166 pending_events(false),
172 auto l = load_layer_map(
173 this->config.get_string("layers.json").value().c_str());
176 this->layers = l.unwrap();
180 HMI_ERROR("wm", "%s", l.err().value());
184 catch (std::exception &e)
186 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
192 if (!this->display->ok())
197 if (this->layers.mapping.empty())
199 HMI_ERROR("wm", "No surface -> layer mapping loaded");
204 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
206 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
209 this->display->add_global_handler(
210 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
211 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
214 this->display->add_global_handler(
215 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
217 std::make_unique<struct compositor::controller>(r, name, v);
219 // Init controller hooks
220 this->controller->chooks = &this->chooks;
222 // This protocol needs the output, so lets just add our mapping here...
223 this->controller->add_proxy_to_id_mapping(
224 this->outputs.back()->proxy.get(),
225 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
226 this->outputs.back()->proxy.get())));
229 this->controller->create_screen(this->outputs.back()->proxy.get());
231 // Set display to controller
232 this->controller->display = this->display;
235 // First level objects
236 this->display->roundtrip();
237 // Second level objects
238 this->display->roundtrip();
239 // Third level objects
240 this->display->roundtrip();
242 return init_layers();
245 int App::dispatch_pending_events()
247 if (this->pop_pending_events())
249 this->display->dispatch_pending();
255 bool App::pop_pending_events()
258 return this->pending_events.compare_exchange_strong(
259 x, false, std::memory_order_consume);
262 void App::set_pending_events()
264 this->pending_events.store(true, std::memory_order_release);
267 optional<int> App::lookup_id(char const *name)
269 return this->id_alloc.lookup(std::string(name));
271 optional<std::string> App::lookup_name(int id)
273 return this->id_alloc.lookup(id);
279 int App::init_layers()
281 if (!this->controller)
283 HMI_ERROR("wm", "ivi_controller global not available");
287 if (this->outputs.empty())
289 HMI_ERROR("wm", "no output was set up!");
293 auto &c = this->controller;
295 auto &o = this->outputs.front();
296 auto &s = c->screens.begin()->second;
297 auto &layers = c->layers;
299 // Write output dimensions to ivi controller...
300 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
301 c->physical_size = compositor::size{uint32_t(o->physical_width),
302 uint32_t(o->physical_height)};
310 // Quick and dirty setup of layers
311 for (auto const &i : this->layers.mapping)
313 c->layer_create(i.second.layer_id, o->width, o->height);
314 auto &l = layers[i.second.layer_id];
315 l->set_destination_rectangle(0, 0, o->width, o->height);
316 l->set_visibility(1);
317 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
318 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
321 // Add layers to screen
322 s->set_render_order(this->layers.layers);
324 this->layout_commit();
326 this->layers.setupArea(o->width, o->height);
331 void App::surface_set_layout(int surface_id, const std::string& area)
333 if (!this->controller->surface_exists(surface_id))
335 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
339 auto o_layer_id = this->layers.get_layer_id(surface_id);
343 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
347 uint32_t layer_id = *o_layer_id;
349 auto const &layer = this->layers.get_layer(layer_id);
350 auto rect = this->layers.getAreaSize(area);
351 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
352 rect.x, rect.y, rect.w, rect.h);
353 auto &s = this->controller->surfaces[surface_id];
360 // less-than-0 values refer to MAX + 1 - $VALUE
361 // e.g. MAX is either screen width or height
364 w = this->controller->output_size.w + 1 + w;
368 h = this->controller->output_size.h + 1 + h;
371 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
374 // set destination to the display rectangle
375 s->set_destination_rectangle(x, y, w, h);
377 // update area information
378 this->area_info[surface_id].x = x;
379 this->area_info[surface_id].y = y;
380 this->area_info[surface_id].w = w;
381 this->area_info[surface_id].h = h;
383 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
384 surface_id, layer_id, x, y, w, h);
387 void App::layout_commit()
389 this->controller->commit_changes();
390 this->display->flush();
395 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
396 if (g_timer_ev_src == nullptr)
398 // firsttime set into sd_event
399 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
400 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
403 HMI_ERROR("wm", "Could't set timer");
408 // update timer limitation after second time
409 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
410 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
414 void App::stopTimer()
416 unsigned req_num = g_app_list.currentRequestNumber();
417 HMI_SEQ_DEBUG(req_num, "Timer stop");
418 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
421 HMI_SEQ_ERROR(req_num, "Timer stop failed");
425 WMError App::lm_release(const struct WMAction &action)
427 //auto const &surface_id = this->lookup_id(drawing_name);
428 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
429 unsigned req_num = g_app_list.currentRequestNumber();
430 auto const &surface_id = this->lookup_id(action.role.c_str());
433 HMI_SEQ_ERROR(req_num, "Surface does not exist");
437 if (*surface_id == this->layers.main_surface)
439 HMI_SEQ_ERROR(req_num, "Cannot deactivate main_surface");
443 auto o_state = *this->layers.get_layout_state(*surface_id);
445 if (o_state == nullptr)
447 HMI_SEQ_ERROR(req_num, "Could not find layer for surface");
451 struct LayoutState &state = *o_state;
453 if (state.main == -1)
455 HMI_SEQ_ERROR(req_num, "No surface active");
459 // Check against main_surface, main_surface_name is the configuration item.
460 if (*surface_id == this->layers.main_surface)
462 HMI_SEQ_DEBUG(req_num, "Refusing to deactivate main_surface %d", *surface_id);
464 return WMError::SUCCESS;
466 if ((state.main == *surface_id) && (state.sub == *surface_id))
468 HMI_SEQ_ERROR(req_num, "Surface is not active");
472 if (state.main == *surface_id)
477 state, LayoutState{state.sub, -1}, [&](LayoutState const &nl) {
478 std::string sub = std::move(*this->lookup_name(state.sub));
480 this->deactivate(*surface_id);
481 this->surface_set_layout(state.sub);
484 this->layout_commit();
485 std::string str_area =
486 std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
487 compositor::rect area_rect = this->area_info[state.sub];
488 this->emit_syncdraw(sub.c_str(), str_area.c_str(),
489 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
490 this->enqueue_flushdraw(state.sub);
495 this->try_layout(state, LayoutState{-1, -1}, [&](LayoutState const &nl) {
496 this->deactivate(*surface_id);
498 this->layout_commit();
502 else if (state.sub == *surface_id)
505 state, LayoutState{state.main, -1}, [&](LayoutState const &nl) {
506 std::string main = std::move(*this->lookup_name(state.main));
508 this->deactivate(*surface_id);
509 this->surface_set_layout(state.main);
512 this->layout_commit();
513 std::string str_area =
514 std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
515 compositor::rect area_rect = this->area_info[state.main];
516 this->emit_syncdraw(main.c_str(), str_area.c_str(),
517 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
518 this->enqueue_flushdraw(state.main);
521 return WMError::SUCCESS;
524 WMError App::doTransition(unsigned req_num)
526 HMI_SEQ_DEBUG(req_num, "check policy");
527 WMError ret = this->checkPolicy(req_num);
528 if (ret != WMError::SUCCESS)
532 HMI_SEQ_DEBUG(req_num, "Start transition.");
533 ret = this->startTransition(req_num);
537 WMError App::checkPolicy(unsigned req_num)
542 // get current trigger
545 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
546 auto trigger = g_app_list.getRequest(req_num, &found);
549 ret = WMError::NO_ENTRY;
552 std::string req_area = trigger.area;
554 // >>>> Compatible with current window manager until policy manager coming
555 if (trigger.task == Task::TASK_ALLOCATE)
557 HMI_SEQ_DEBUG(req_num, "Check split or not");
558 const char *msg = this->check_surface_exist(trigger.role.c_str());
562 HMI_SEQ_ERROR(req_num, msg);
563 ret = WMError::LAYOUT_CHANGE_FAIL;
567 auto const &surface_id = this->lookup_id(trigger.role.c_str());
568 auto o_state = *this->layers.get_layout_state(*surface_id);
569 struct LayoutState &state = *o_state;
571 unsigned curernt_sid = state.main;
572 split = this->can_split(state, *surface_id);
576 HMI_SEQ_DEBUG(req_num, "Split happens");
577 // Get current visible role
578 std::string add_role = this->lookup_name(state.main).value();
580 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
581 // Change request area
582 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
583 HMI_SEQ_NOTICE(req_num, "Change request area from %s to %s, because split is happen",
584 trigger.area, req_area);
585 // set another action
586 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
589 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %s", curernt_sid);
590 ret = WMError::NOT_REGISTERED;
593 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
594 add_name.c_str(), add_role.c_str(), add_area.c_str());
596 bool end_draw_finished = false;
597 WMAction split_action{
601 TaskVisible::VISIBLE,
603 WMError ret = g_app_list.setAction(req_num, split_action);
604 if (ret != WMError::SUCCESS)
606 HMI_SEQ_ERROR(req_num, "Failed to set action");
609 g_app_list.reqDump();
614 HMI_SEQ_DEBUG(req_num, "split doesn't happen");
617 // Set invisible task(Remove if policy manager finish)
618 ret = this->setInvisibleTask(trigger.role, split);
619 if(ret != WMError::SUCCESS)
621 HMI_SEQ_ERROR(req_num, "Failed to set invisible task: %s", errorDescription(ret));
625 /* get new status from Policy Manager */
626 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
627 if(trigger.role == "HomeScreen")
629 // TODO : Remove when Policy Manager completed
630 HMI_SEQ_NOTICE(req_num, "Hack. This process will be removed. Change HomeScreen code!!");
631 req_area = "fullscreen";
633 TaskVisible task_visible =
634 (trigger.task == Task::TASK_ALLOCATE) ? TaskVisible::VISIBLE : TaskVisible::INVISIBLE;
636 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, task_visible);
637 g_app_list.reqDump();
642 WMError App::startTransition(unsigned req_num)
644 bool sync_draw_happen = false;
646 WMError ret = WMError::SUCCESS;
647 auto actions = g_app_list.getActions(req_num, &found);
650 ret = WMError::NO_ENTRY;
651 HMI_SEQ_ERROR(req_num,
652 "Window Manager bug :%s : Action is not set", errorDescription(ret));
656 for (const auto &action : actions)
658 if (action.visible != TaskVisible::INVISIBLE)
660 sync_draw_happen = true;
661 this->emit_syncdraw(action.role, action.area);
662 /* TODO: emit event for app not subscriber
663 if(g_app_list.contains(y.appid))
664 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
668 if (sync_draw_happen)
674 // deactivate only, no syncDraw
675 // Make it deactivate here
676 for (const auto &x : actions)
678 if (g_app_list.contains(x.appid))
680 auto client = g_app_list.lookUpClient(x.appid);
681 this->deactivate(client->surfaceID(x.role));
684 ret = NO_LAYOUT_CHANGE;
689 WMError App::setInvisibleTask(const std::string &role, bool split)
691 unsigned req_num = g_app_list.currentRequestNumber();
692 HMI_SEQ_DEBUG(req_num, "set current visible app to invisible task");
693 // This task is copied from original actiavete surface
694 const char *drawing_name = role.c_str();
695 auto const &surface_id = this->lookup_id(drawing_name);
696 auto layer_id = this->layers.get_layer_id(*surface_id);
697 auto o_state = *this->layers.get_layout_state(*surface_id);
698 struct LayoutState &state = *o_state;
699 std::string add_name, add_role;
700 std::string add_area = "";
702 TaskVisible task_visible = TaskVisible::INVISIBLE;
703 bool end_draw_finished = true;
706 for (auto const &l : this->layers.mapping)
708 if (l.second.layer_id <= *layer_id)
712 HMI_DEBUG("wm", "debug: main %d , sub : %d", l.second.state.main, l.second.state.sub);
713 if (l.second.state.main != -1)
715 //this->deactivate(l.second.state.main);
716 surface = l.second.state.main;
717 add_role = *this->id_alloc.lookup(surface);
718 add_name = g_app_list.getAppID(surface, add_role, &found);
720 return WMError::NOT_REGISTERED;
722 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
723 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
724 g_app_list.setAction(req_num, act);
725 l.second.state.main = -1;
728 if (l.second.state.sub != -1)
730 //this->deactivate(l.second.state.sub);
731 surface = l.second.state.sub;
732 add_role = *this->id_alloc.lookup(surface);
733 add_name = g_app_list.getAppID(surface, add_role, &found);
736 return WMError::NOT_REGISTERED;
738 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
739 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
740 g_app_list.setAction(req_num, act);
741 l.second.state.sub = -1;
745 // change current state here, but this is hack
746 auto layer = this->layers.get_layer(*layer_id);
748 if (state.main == -1)
750 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
751 //state = LayoutState{*surface_id};
753 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
754 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
755 //this->surface_set_layout(*surface_id);
758 //compositor::rect area_rect = this->area_info[*surface_id];
763 if (0 != strcmp(drawing_name, "HomeScreen"))
768 std::move(*this->id_alloc.lookup(state.main));
769 if (state.sub != *surface_id)
773 //this->deactivate(state.sub);
775 deact_sub.role = main;
776 deact_sub.area = add_area;
777 deact_sub.appid = g_app_list.getAppID(state.sub, main, &found);
780 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
781 return WMError::NOT_REGISTERED;
783 deact_sub.visible = task_visible;
784 deact_sub.end_draw_finished = end_draw_finished;
785 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
786 g_app_list.setAction(req_num, deact_sub);
789 //state = LayoutState{state.main, *surface_id};
793 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
795 //this->surface_set_layout(*surface_id);
796 if (state.main != *surface_id)
798 // this->deactivate(state.main);
800 deact_main.role = std::move(*this->id_alloc.lookup(state.main));
802 deact_main.area = add_area;
803 deact_main.appid = g_app_list.getAppID(state.main, deact_main.role, &found);
806 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
807 return WMError::NOT_REGISTERED;
809 deact_main.visible = task_visible;
810 deact_main.end_draw_finished = end_draw_finished;
811 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
812 g_app_list.setAction(req_num, deact_main);
816 if (state.sub != *surface_id)
818 //this->deactivate(state.sub);
820 deact_sub.role = std::move(*this->id_alloc.lookup(state.sub));
822 deact_sub.area = add_area;
823 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
826 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
827 return WMError::NOT_REGISTERED;
829 deact_sub.visible = task_visible;
830 deact_sub.end_draw_finished = end_draw_finished;
831 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
832 g_app_list.setAction(req_num, deact_sub);
835 //state = LayoutState{*surface_id};
839 return WMError::SUCCESS;
842 const char *App::check_surface_exist(const char *drawing_name)
844 auto const &surface_id = this->lookup_id(drawing_name);
847 //reply("Surface does not exist");
848 return "Surface does not exist";
851 if (!this->controller->surface_exists(*surface_id))
853 //reply("Surface does not exist in controller!");
854 return "Surface does not exist in controller!";
857 auto layer_id = this->layers.get_layer_id(*surface_id);
861 //reply("Surface is not on any layer!");
862 return "Surface is not on any layer!";
865 auto o_state = *this->layers.get_layout_state(*surface_id);
867 if (o_state == nullptr)
869 //reply("Could not find layer for surface");
870 return "Could not find layer for surface";
873 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
878 WMError App::setRequest(const std::string& appid, const std::string &role, const std::string &area,
879 Task task, unsigned* req_num)
881 if (!g_app_list.contains(appid))
883 return WMError::NOT_REGISTERED;
886 auto client = g_app_list.lookUpClient(appid);
891 unsigned current = g_app_list.currentRequestNumber();
892 unsigned requested_num = g_app_list.getRequestNumber(appid);
893 if (requested_num != 0)
895 HMI_SEQ_INFO(requested_num,
896 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
900 WMRequest req = WMRequest(appid, role, area, task);
901 unsigned new_req = g_app_list.addRequest(req);
903 g_app_list.reqDump();
905 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
907 return WMError::SUCCESS;
910 void App::api_activate_surface(char const *appid, char const *drawing_name,
911 char const *drawing_area, const reply_func &reply)
915 std::string id = appid;
916 std::string role = drawing_name;
917 std::string area = drawing_area;
918 Task task = Task::TASK_ALLOCATE;
919 unsigned req_num = 0;
920 WMError ret = WMError::UNKNOWN;
922 ret = this->setRequest(id, role, area, task, &req_num);
924 if(ret != WMError::SUCCESS)
926 HMI_ERROR("wm", errorDescription(ret));
927 reply("Failed to set request");
932 if (req_num != g_app_list.currentRequestNumber())
934 // Add request, then invoked after the previous task is finished
935 HMI_SEQ_DEBUG(req_num, "request is accepted");
942 ret = this->doTransition(req_num);
944 if (ret != WMError::SUCCESS)
947 HMI_SEQ_ERROR(req_num, errorDescription(ret));
948 g_app_list.removeRequest(req_num);
949 this->processNextRequest();
953 void App::api_deactivate_surface(char const *appid, char const *drawing_name,
954 const reply_func &reply)
961 std::string id = appid;
962 std::string role = drawing_name;
963 std::string area = ""; //drawing_area;
964 Task task = Task::TASK_RELEASE;
965 unsigned req_num = 0;
966 WMError ret = WMError::UNKNOWN;
968 ret = this->setRequest(id, role, area, task, &req_num);
970 if (ret != WMError::SUCCESS)
972 HMI_ERROR("wm", errorDescription(ret));
973 reply("Failed to set request");
978 if (req_num != g_app_list.currentRequestNumber())
980 // Add request, then invoked after the previous task is finished
981 HMI_SEQ_DEBUG(req_num, "request is accepted");
988 ret = this->doTransition(req_num);
990 if (ret != WMError::SUCCESS)
993 HMI_SEQ_ERROR(req_num, errorDescription(ret));
994 g_app_list.removeRequest(req_num);
995 this->processNextRequest();
999 void App::enqueue_flushdraw(int surface_id)
1001 this->check_flushdraw(surface_id);
1002 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
1003 this->pending_end_draw.push_back(surface_id);
1006 void App::check_flushdraw(int surface_id)
1008 auto i = std::find(std::begin(this->pending_end_draw),
1009 std::end(this->pending_end_draw), surface_id);
1010 if (i != std::end(this->pending_end_draw))
1012 auto n = this->lookup_name(surface_id);
1013 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
1014 n ? n->c_str() : "unknown-name", surface_id);
1015 std::swap(this->pending_end_draw[std::distance(
1016 std::begin(this->pending_end_draw), i)],
1017 this->pending_end_draw.back());
1018 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
1022 WMError App::doEndDraw(unsigned req_num)
1026 auto actions = g_app_list.getActions(req_num, &found);
1027 WMError ret = WMError::SUCCESS;
1030 ret = WMError::NO_ENTRY;
1034 HMI_SEQ_INFO(req_num, "do endDraw");
1036 // layout change and make it visible
1037 for (const auto &act : actions)
1040 if(!g_app_list.contains(act.appid)){
1041 ret = WMError::NOT_REGISTERED;
1043 ret = this->layoutChange(act);
1044 if(ret != WMError::SUCCESS)
1046 HMI_SEQ_WARNING(req_num,
1047 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1050 ret = this->visibilityChange(act);
1051 if (ret != WMError::SUCCESS)
1053 HMI_SEQ_WARNING(req_num,
1054 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1057 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1058 //this->lm_enddraw(act.role.c_str());
1061 // Change current state
1062 this->changeCurrentState(req_num);
1064 HMI_SEQ_INFO(req_num, "emit flushDraw");
1066 for(const auto &act_flush : actions)
1068 if(act_flush.visible != TaskVisible::INVISIBLE)
1070 this->emit_flushdraw(act_flush.role.c_str());
1077 WMError App::setSurfaceSize(unsigned surface, const std::string &area)
1079 this->surface_set_layout(surface, area);
1081 return WMError::SUCCESS;
1084 WMError App::layoutChange(const WMAction &action)
1086 if (action.visible == TaskVisible::INVISIBLE)
1088 // Visibility is not change -> no redraw is required
1089 return WMError::SUCCESS;
1091 auto client = g_app_list.lookUpClient(action.appid);
1092 unsigned surface = client->surfaceID(action.role);
1095 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1096 "client doesn't have surface with role(%s)", action.role.c_str());
1097 return WMError::NOT_REGISTERED;
1100 WMError ret = this->setSurfaceSize(surface, action.area);
1104 WMError App::visibilityChange(const WMAction &action)
1106 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1107 if(!g_app_list.contains(action.appid)){
1108 return WMError::NOT_REGISTERED;
1110 auto client = g_app_list.lookUpClient(action.appid);
1111 unsigned surface = client->surfaceID(action.role);
1114 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1115 "client doesn't have surface with role(%s)", action.role.c_str());
1116 return WMError::NOT_REGISTERED;
1119 if (action.visible != TaskVisible::INVISIBLE)
1121 this->activate(surface); // Layout Manager task
1125 this->deactivate(surface); // Layout Manager task
1127 return WMError::SUCCESS;
1130 WMError App::changeCurrentState(unsigned req_num)
1132 HMI_SEQ_DEBUG(req_num, "Change current layout state");
1133 bool trigger_found = false, action_found = false;
1134 auto trigger = g_app_list.getRequest(req_num, &trigger_found);
1135 auto actions = g_app_list.getActions(req_num, &action_found);
1136 if (!trigger_found || !action_found)
1138 HMI_SEQ_ERROR(req_num, "Action not found");
1139 return WMError::LAYOUT_CHANGE_FAIL;
1142 // Layout state reset
1143 struct LayoutState reset_state{-1, -1};
1144 HMI_SEQ_DEBUG(req_num,"Reset layout state");
1145 for (const auto &action : actions)
1147 if(!g_app_list.contains(action.appid)){
1148 return WMError::NOT_REGISTERED;
1150 auto client = g_app_list.lookUpClient(action.appid);
1151 auto pCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1152 if(pCurState == nullptr)
1154 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1157 pCurState->main = reset_state.main;
1158 pCurState->sub = reset_state.sub;
1161 HMI_SEQ_DEBUG(req_num, "Change state");
1162 for (const auto &action : actions)
1164 auto client = g_app_list.lookUpClient(action.appid);
1165 auto pLayerCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1166 if (pLayerCurState == nullptr)
1168 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1173 if (action.visible != TaskVisible::INVISIBLE)
1175 surface = (int)client->surfaceID(action.role);
1176 HMI_SEQ_INFO(req_num, "Change %s surface : %d, state visible area : %s",
1177 action.role.c_str(), surface, action.area.c_str());
1178 // visible == true -> layout changes
1179 if(action.area == "normal.full" || action.area == "split.main")
1181 pLayerCurState->main = surface;
1183 else if (action.area == "split.sub")
1185 pLayerCurState->sub = surface;
1190 pLayerCurState->main = surface;
1195 return WMError::SUCCESS;
1198 void App::api_enddraw(char const *appid, char const *drawing_name)
1200 std::string id = appid;
1201 std::string role = drawing_name;
1202 unsigned current_req = g_app_list.currentRequestNumber();
1203 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
1207 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
1211 if (g_app_list.endDrawFullfilled(current_req))
1213 // do task for endDraw
1215 WMError ret = this->doEndDraw(current_req);
1217 if(ret != WMError::SUCCESS)
1219 //this->emit_error();
1221 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
1223 g_app_list.removeRequest(current_req);
1225 this->processNextRequest();
1229 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
1234 void App::processNextRequest()
1237 g_app_list.reqDump();
1238 unsigned req_num = g_app_list.currentRequestNumber();
1239 if (g_app_list.haveRequest())
1241 HMI_SEQ_DEBUG(req_num, "Process next request");
1242 WMError rc = doTransition(req_num);
1243 if (rc != WMError::SUCCESS)
1245 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1250 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1254 void App::api_ping() { this->dispatch_pending_events(); }
1256 void App::send_event(char const *evname, char const *label)
1258 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
1260 json_object *j = json_object_new_object();
1261 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1263 int ret = afb_event_push(this->map_afb_event[evname], j);
1266 HMI_DEBUG("wm", "afb_event_push failed: %m");
1270 void App::send_event(char const *evname, char const *label, char const *area,
1271 int x, int y, int w, int h)
1273 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
1274 __func__, evname, label, area, x, y, w, h);
1276 json_object *j_rect = json_object_new_object();
1277 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
1278 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
1279 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
1280 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
1282 json_object *j = json_object_new_object();
1283 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1284 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
1285 json_object_object_add(j, kKeyDrawingRect, j_rect);
1287 int ret = afb_event_push(this->map_afb_event[evname], j);
1290 HMI_DEBUG("wm", "afb_event_push failed: %m");
1297 void App::surface_created(uint32_t surface_id)
1299 auto layer_id = this->layers.get_layer_id(surface_id);
1302 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
1307 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
1309 this->controller->layers[*layer_id]->add_surface(surface_id);
1310 this->layout_commit();
1311 // activate the main_surface right away
1312 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
1313 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
1315 this->api_activate_surface(
1316 this->lookup_name(surface_id).value_or("unknown-name").c_str());
1319 // search pid from surfaceID
1321 // pick up appid from pid from application manager
1323 // check appid then add it to the client
1326 void App::surface_removed(uint32_t surface_id)
1328 HMI_DEBUG("wm", "surface_id is %u", surface_id);
1330 g_app_list.removeSurface(surface_id);
1333 void App::emit_activated(char const *label)
1335 this->send_event(kListEventName[Event_Active], label);
1338 void App::emit_deactivated(char const *label)
1340 this->send_event(kListEventName[Event_Inactive], label);
1343 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1345 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1348 void App::emit_syncdraw(const std::string &role, const std::string &area)
1350 compositor::rect rect = this->layers.getAreaSize(area);
1351 //this->lm_get_area_info(area, &x, &y, &w, &h);
1352 this->send_event(kListEventName[Event_SyncDraw],
1353 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
1356 void App::emit_flushdraw(char const *label)
1358 this->send_event(kListEventName[Event_FlushDraw], label);
1361 void App::emit_visible(char const *label, bool is_visible)
1363 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1366 void App::emit_invisible(char const *label)
1368 return emit_visible(label, false);
1371 void App::emit_visible(char const *label) { return emit_visible(label, true); }
1373 result<int> App::api_request_surface(char const *appid, char const *drawing_name)
1375 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1379 * register drawing_name as fallback and make it displayed.
1381 lid = this->layers.get_layer_id(std::string("Fallback"));
1382 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1385 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1389 auto rname = this->lookup_id(drawing_name);
1392 // name does not exist yet, allocate surface id...
1393 auto id = int(this->id_alloc.generate_id(drawing_name));
1394 this->layers.add_surface(id, *lid);
1396 // set the main_surface[_name] here and now
1397 if (!this->layers.main_surface_name.empty() &&
1398 this->layers.main_surface_name == drawing_name)
1400 this->layers.main_surface = id;
1401 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1404 // add client into the db
1405 std::string appid_str(appid);
1406 std::string role(drawing_name);
1407 //g_app_list.addClient(appid_str, role);
1408 g_app_list.addClient(appid_str, *lid, id, role);
1413 // Check currently registered drawing names if it is already there.
1414 return Err<int>("Surface already present");
1417 char const *App::api_request_surface(char const *appid, char const *drawing_name,
1422 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1423 unsigned sid = std::stol(ivi_id);
1428 * register drawing_name as fallback and make it displayed.
1430 lid = this->layers.get_layer_id(std::string("Fallback"));
1431 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1434 return "Drawing name does not match any role, Fallback is disabled";
1438 auto rname = this->lookup_id(drawing_name);
1442 return "Surface already present";
1445 // register pair drawing_name and ivi_id
1446 this->id_alloc.register_name_id(drawing_name, sid);
1447 this->layers.add_surface(sid, *lid);
1449 // this surface is already created
1450 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1452 this->controller->layers[*lid]->add_surface(sid);
1453 this->layout_commit();
1458 result<json_object *> App::api_get_display_info()
1461 if (!this->controller)
1463 return Err<json_object *>("ivi_controller global not available");
1467 compositor::size o_size = this->controller->output_size;
1468 compositor::size p_size = this->controller->physical_size;
1470 json_object *object = json_object_new_object();
1471 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1472 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1473 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1474 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1476 return Ok<json_object *>(object);
1479 result<json_object *> App::api_get_area_info(char const *drawing_name)
1481 HMI_DEBUG("wm", "called");
1483 // Check drawing name, surface/layer id
1484 auto const &surface_id = this->lookup_id(drawing_name);
1487 return Err<json_object *>("Surface does not exist");
1490 if (!this->controller->surface_exists(*surface_id))
1492 return Err<json_object *>("Surface does not exist in controller!");
1495 auto layer_id = this->layers.get_layer_id(*surface_id);
1498 return Err<json_object *>("Surface is not on any layer!");
1501 auto o_state = *this->layers.get_layout_state(*surface_id);
1502 if (o_state == nullptr)
1504 return Err<json_object *>("Could not find layer for surface");
1507 struct LayoutState &state = *o_state;
1508 if ((state.main != *surface_id) && (state.sub != *surface_id))
1510 return Err<json_object *>("Surface is inactive");
1513 // Set area rectangle
1514 compositor::rect area_info = this->area_info[*surface_id];
1515 json_object *object = json_object_new_object();
1516 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1517 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1518 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1519 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1521 return Ok<json_object *>(object);
1524 void App::activate(int id)
1526 auto ip = this->controller->sprops.find(id);
1527 if (ip != this->controller->sprops.end())
1529 this->controller->surfaces[id]->set_visibility(1);
1531 this->lookup_name(id).value_or("unknown-name").c_str();
1534 if ((0 == strcmp(label, "Radio")) ||
1535 (0 == strcmp(label, "MediaPlayer")) ||
1536 (0 == strcmp(label, "Music")) ||
1537 (0 == strcmp(label, "Navigation")))
1539 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1544 this->surface_bg.erase(i);
1546 // Remove from BG layer (999)
1547 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1548 this->controller->layers[999]->remove_surface(id);
1550 // Add to FG layer (1001)
1551 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1552 this->controller->layers[1001]->add_surface(id);
1554 for (int j : this->surface_bg)
1556 HMI_DEBUG("wm", "Stored id:%d", j);
1563 this->layout_commit();
1565 this->emit_visible(label);
1566 this->emit_activated(label);
1570 void App::deactivate(int id)
1572 auto ip = this->controller->sprops.find(id);
1573 if (ip != this->controller->sprops.end())
1576 this->lookup_name(id).value_or("unknown-name").c_str();
1579 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1583 this->surface_bg.push_back(id);
1585 // Remove from FG layer (1001)
1586 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1587 this->controller->layers[1001]->remove_surface(id);
1589 // Add to BG layer (999)
1590 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1591 this->controller->layers[999]->add_surface(id);
1593 for (int j : surface_bg)
1595 HMI_DEBUG("wm", "Stored id:%d", j);
1600 this->controller->surfaces[id]->set_visibility(0);
1604 this->emit_deactivated(label);
1605 this->emit_invisible(label);
1609 bool App::can_split(struct LayoutState const &state, int new_id)
1611 if (state.main != -1 && state.main != new_id)
1613 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1614 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1616 // surfaces are on separate layers, don't bother.
1617 if (new_id_layer != current_id_layer)
1622 std::string const &new_id_str = this->lookup_name(new_id).value();
1623 std::string const &cur_id_str = this->lookup_name(state.main).value();
1625 auto const &layer = this->layers.get_layer(new_id_layer);
1627 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1629 if (layer->layouts.empty())
1634 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1636 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1637 auto rem = std::regex(i->main_match);
1638 if (std::regex_match(cur_id_str, rem))
1640 // build the second one only if the first already matched
1641 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1642 auto res = std::regex(i->sub_match);
1643 if (std::regex_match(new_id_str, res))
1645 HMI_DEBUG("wm", "layout matched!");
1655 void App::try_layout(struct LayoutState & /*state*/,
1656 struct LayoutState const &new_layout,
1657 std::function<void(LayoutState const &nl)> apply)
1659 if (this->policy.layout_is_valid(new_layout))
1668 void controller_hooks::surface_created(uint32_t surface_id)
1670 this->app->surface_created(surface_id);
1673 void controller_hooks::surface_removed(uint32_t surface_id)
1675 this->app->surface_removed(surface_id);
1678 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1681 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,