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 "json_helper.hpp"
22 #include "applist.hpp"
26 #include <systemd/sd-event.h>
35 static const uint64_t kTimeOut = 3ULL; /* 3s */
37 /* DrawingArea name used by "{layout}.{area}" */
38 const char kNameLayoutNormal[] = "normal";
39 const char kNameLayoutSplit[] = "split";
40 const char kNameAreaFull[] = "full";
41 const char kNameAreaMain[] = "main";
42 const char kNameAreaSub[] = "sub";
44 /* Key for json obejct */
45 const char kKeyDrawingName[] = "drawing_name";
46 const char kKeyDrawingArea[] = "drawing_area";
47 const char kKeyDrawingRect[] = "drawing_rect";
48 const char kKeyX[] = "x";
49 const char kKeyY[] = "y";
50 const char kKeyWidth[] = "width";
51 const char kKeyHeight[] = "height";
52 const char kKeyWidthPixel[] = "width_pixel";
53 const char kKeyHeightPixel[] = "height_pixel";
54 const char kKeyWidthMm[] = "width_mm";
55 const char kKeyHeightMm[] = "height_mm";
56 const char kKeyScale[] = "scale";
57 const char kKeyIds[] = "ids";
59 static sd_event_source *g_timer_ev_src = nullptr;
60 static AppList g_app_list;
61 static WindowManager *g_context;
66 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
68 HMI_NOTICE("Time out occurs because the client replys endDraw slow, so revert the request");
69 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
73 static void onStateTransitioned(vector<WMAction> actions)
75 g_context->startTransitionWrapper(actions);
80 g_context->processError(WMError::LAYOUT_CHANGE_FAIL);
87 WindowManager::WindowManager()
90 const char *path = getenv("AFM_APP_INSTALL_DIR");
93 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
97 this->lc = std::make_shared<LayerControl>(root);
99 HMI_DEBUG("Layer Controller initialized");
102 int WindowManager::init()
104 // TODO: application requests by old role,
105 // so create role map (old, new)
107 LayerControlCallbacks lmcb;
108 lmcb.surfaceCreated = [&](unsigned pid, unsigned surface){
109 this->surface_created(surface);
111 lmcb.surfaceDestroyed = [&](unsigned surface){
112 this->surface_removed(surface);
114 this->lc->init(lmcb);
115 this->loadOldRoleDb();
117 // Store my context for calling callback from PolicyManager
120 // Initialize PMWrapper
121 this->pmw.initialize();
123 // Register callback to PolicyManager
124 this->pmw.registerCallback(onStateTransitioned, onError);
127 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
129 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
132 const struct rect css_bg = this->lc->getAreaSize("fullscreen");
133 Screen screen = this->lc->getScreenInfo();
134 rectangle dp_bg(screen.width(), screen.height());
136 dp_bg.set_aspect(static_cast<double>(css_bg.w) / css_bg.h);
137 dp_bg.fit(screen.width(), screen.height());
138 dp_bg.center(screen.width(), screen.height());
139 HMI_DEBUG("SCALING: CSS BG(%dx%d) -> DDP %dx%d,(%dx%d)",
140 css_bg.w, css_bg.h, dp_bg.left(), dp_bg.top(), dp_bg.width(), dp_bg.height());
142 double scale = static_cast<double>(dp_bg.height()) / css_bg.h;
143 this->lc->setupArea(dp_bg, scale);
145 return 0; //init_layers();
148 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
150 // TODO: application requests by old role,
151 // so convert role old to new
152 const char *role = this->convertRoleOldToNew(drawing_name);
154 string s_appid = appid;
155 string s_role = role;
157 if(!g_app_list.contains(s_appid))
159 // auto lid = this->layers.get_layer_id(string(role));
160 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
164 * register drawing_name as fallback and make it displayed.
166 l_id = this->lc->getNewLayerID("fallback", &l_name);
167 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
170 return Err<int>("Designated role does not match any role, fallback is disabled");
173 this->lc->createNewLayer(l_id);
174 // add client into the db
175 g_app_list.addClient(s_appid, l_id, s_role);
178 // generate surface ID for ivi-shell application
180 auto rname = this->id_alloc.lookup(string(role));
183 // name does not exist yet, allocate surface id...
184 auto id = int(this->id_alloc.generate_id(role));
185 this->tmp_surface2app[id] = s_appid;
187 // Set role map of (new, old)
188 this->rolenew2old[role] = string(drawing_name);
193 // Check currently registered drawing names if it is already there.
194 return Err<int>("Surface already present");
197 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
200 // TODO: application requests by old role,
201 // so convert role old to new
202 const char *role = this->convertRoleOldToNew(drawing_name);
203 string s_role = role;
204 string s_appid = appid;
207 if(!g_app_list.contains(s_appid))
209 // auto lid = this->layers.get_layer_id(string(role));
210 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
214 * register drawing_name as fallback and make it displayed.
216 l_id = this->lc->getNewLayerID("fallback", &l_name);
217 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
220 return "Designated role does not match any role, fallback is disabled";
223 this->lc->createNewLayer(l_id);
224 // add client into the db
225 g_app_list.addClient(s_appid, l_id, s_role);
228 unsigned sid = std::stol(ivi_id);
230 auto rname = this->id_alloc.lookup(s_role);
233 return "Surface already present";
235 if(this->tmp_surface2app.count(sid) != 0)
237 this->tmp_surface2app.erase(sid);
241 // register pair drawing_name and ivi_id
242 this->id_alloc.register_name_id(role, sid);
243 // this->layers.add_surface(sid, *lid);
245 // this surface is already created
246 // HMI_DEBUG("surface_id is %u, layer_id is %u", sid, *lid);
248 // this->controller->layers[*lid]->add_surface(sid);
249 // this->layout_commit();
251 // add client into the db
252 // g_app_list.addClient(appid_str, lid, s_role);
253 auto client = g_app_list.lookUpClient(s_appid);
254 client->addSurface(sid);
256 // Set role map of (new, old)
257 this->rolenew2old[role] = string(drawing_name);
262 bool WindowManager::api_set_role(char const *appid, char const *drawing_name, unsigned pid){
264 string role = drawing_name;
266 unsigned surface = 0;
267 WMError wm_err = WMError::UNKNOWN;
270 // get layer ID which role should be in
271 // auto lid = this->layers.get_layer_id(role);
272 unsigned lid = this->lc->getNewLayerID(role, &l_name);
275 // lid = this->layers.get_layer_id(string("fallback"));
276 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role.c_str());
279 HMI_ERROR("Drawing name does not match any role, fallback is disabled");
285 // search floating surfaceID from pid if pid is designated.
286 wm_err = g_app_list.popFloatingSurface(pid, &surface);
289 // get floating surface with appid. If WM queries appid from pid,
290 // WM can bind surface and role with appid(not implemented yet)
291 //wm_err = g_app_list.popFloatingSurface(id);
293 if(wm_err != WMError::SUCCESS){
294 HMI_ERROR("No floating surface for app: %s", id.c_str());
295 g_app_list.addFloatingClient(id, lid, role);
296 HMI_NOTICE("%s : Waiting for surface creation", id.c_str());
301 if (g_app_list.contains(id))
303 HMI_INFO("Add role: %s with surface: %d. Client %s has multi surfaces.",
304 role.c_str(), surface, id.c_str());
305 auto client = g_app_list.lookUpClient(id);
306 client->appendRole(role);
309 HMI_INFO("Create new client: %s, surface: %d into layer: %d with role: %s",
310 id.c_str(), surface, lid, role.c_str());
311 g_app_list.addClient(id, lid, role);
314 // register pair drawing_name and ivi_id
315 this->id_alloc.register_name_id(role.c_str(), surface);
317 // this surface is already created
318 HMI_DEBUG("surface_id is %u, layer_id is %u", surface, lid);
323 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
324 char const *drawing_area, const reply_func &reply)
326 // TODO: application requests by old role,
327 // so convert role old to new
328 const char *c_role = this->convertRoleOldToNew(drawing_name);
331 string role = c_role;
332 string area = drawing_area;
334 if(!g_app_list.contains(id))
336 reply("app doesn't request 'requestSurface' or 'setRole' yet");
339 auto client = g_app_list.lookUpClient(id);
341 // unsigned srfc = client->surfaceID(role);
342 // unsigned layer = client->layerID();
344 // g_app_list.removeFloatingSurface(client->surfaceID());
345 // g_app_list.removeFloatingSurface(client);
347 Task task = Task::TASK_ALLOCATE;
348 unsigned req_num = 0;
349 WMError ret = WMError::UNKNOWN;
351 ret = this->setRequest(id, role, area, task, &req_num);
353 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
354 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
356 if(ret != WMError::SUCCESS)
358 HMI_ERROR(errorDescription(ret));
359 reply("Failed to set request");
364 if (req_num != g_app_list.currentRequestNumber())
366 // Add request, then invoked after the previous task is finished
367 HMI_SEQ_DEBUG(req_num, "request is accepted");
374 ret = this->checkPolicy(req_num);
376 if (ret != WMError::SUCCESS)
379 HMI_SEQ_ERROR(req_num, errorDescription(ret));
380 g_app_list.removeRequest(req_num);
381 this->processNextRequest();
385 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
386 const reply_func &reply)
388 // TODO: application requests by old role,
389 // so convert role old to new
390 const char *c_role = this->convertRoleOldToNew(drawing_name);
396 string role = c_role;
397 string area = ""; //drawing_area;
398 Task task = Task::TASK_RELEASE;
399 unsigned req_num = 0;
400 WMError ret = WMError::UNKNOWN;
402 ret = this->setRequest(id, role, area, task, &req_num);
404 if (ret != WMError::SUCCESS)
406 HMI_ERROR(errorDescription(ret));
407 reply("Failed to set request");
412 if (req_num != g_app_list.currentRequestNumber())
414 // Add request, then invoked after the previous task is finished
415 HMI_SEQ_DEBUG(req_num, "request is accepted");
422 ret = this->checkPolicy(req_num);
424 if (ret != WMError::SUCCESS)
427 HMI_SEQ_ERROR(req_num, errorDescription(ret));
428 g_app_list.removeRequest(req_num);
429 this->processNextRequest();
433 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
435 // TODO: application requests by old role,
436 // so convert role old to new
437 const char *c_role = this->convertRoleOldToNew(drawing_name);
440 string role = c_role;
441 unsigned current_req = g_app_list.currentRequestNumber();
442 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
446 HMI_ERROR("%s is not in transition state", id.c_str());
450 if (g_app_list.endDrawFullfilled(current_req))
452 // do task for endDraw
454 WMError ret = this->doEndDraw(current_req);
456 if(ret != WMError::SUCCESS)
458 //this->emit_error();
460 // Undo state of PolicyManager
461 this->pmw.undoState();
462 this->lc->undoUpdate();
464 this->emitScreenUpdated(current_req);
465 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
467 g_app_list.removeRequest(current_req);
469 this->processNextRequest();
473 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
478 result<json_object *> WindowManager::api_get_display_info()
480 Screen screen = this->lc->getScreenInfo();
482 json_object *object = json_object_new_object();
483 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(screen.width()));
484 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(screen.height()));
486 json_object_object_add(object, kKeyWidthMm, json_object_new_int(0));
487 json_object_object_add(object, kKeyHeightMm, json_object_new_int(0));
488 json_object_object_add(object, kKeyScale, json_object_new_double(this->lc->scale()));
490 return Ok<json_object *>(object);
493 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
497 // TODO: application requests by old role,
498 // so convert role old to new
499 const char *role = this->convertRoleOldToNew(drawing_name);
501 // Check drawing name, surface/layer id
502 auto const &surface_id = this->id_alloc.lookup(string(role));
505 return Err<json_object *>("Surface does not exist");
508 // Set area rectangle
509 rect area_info = this->area_info[*surface_id];
510 json_object *object = json_object_new_object();
511 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
512 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
513 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
514 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
516 return Ok<json_object *>(object);
519 void WindowManager::send_event(char const *evname, char const *label)
521 HMI_DEBUG("%s: %s(%s)", __func__, evname, label);
523 json_object *j = json_object_new_object();
524 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
526 int ret = afb_event_push(this->map_afb_event[evname], j);
529 HMI_DEBUG("afb_event_push failed: %m");
533 void WindowManager::send_event(char const *evname, char const *label, char const *area,
534 int x, int y, int w, int h)
536 HMI_DEBUG("%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
537 __func__, evname, label, area, x, y, w, h);
539 json_object *j_rect = json_object_new_object();
540 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
541 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
542 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
543 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
545 json_object *j = json_object_new_object();
546 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
547 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
548 json_object_object_add(j, kKeyDrawingRect, j_rect);
550 int ret = afb_event_push(this->map_afb_event[evname], j);
553 HMI_DEBUG("afb_event_push failed: %m");
560 void WindowManager::surface_created(uint32_t surface_id)
562 if(this->tmp_surface2app.count(surface_id) != 0)
564 string appid = this->tmp_surface2app[surface_id];
565 this->tmp_surface2app.erase(surface_id);
566 if(g_app_list.contains(appid))
568 auto client = g_app_list.lookUpClient(appid);
569 WMError ret = client->addSurface(surface_id);
570 HMI_INFO("Add surface %d to \"%s\"", surface_id, appid.c_str());
571 if(ret != WMError::SUCCESS)
573 HMI_ERROR("Failed to add surface to client %s", client->appID().c_str());
579 HMI_NOTICE("Unknown surface %d", surface_id);
580 this->tmp_surface2app[surface_id] = ""; /* Store for requestSurfaceXDG */
584 void WindowManager::surface_removed(uint32_t surface_id)
586 HMI_DEBUG("Delete surface_id %u", surface_id);
587 this->id_alloc.remove_id(surface_id);
588 // this->layers.remove_surface(surface_id);
589 g_app_list.removeSurface(surface_id);
592 void WindowManager::removeClient(const string &appid)
594 HMI_DEBUG("Remove clinet %s from list", appid.c_str());
595 auto client = g_app_list.lookUpClient(appid);
596 this->lc->terminateApp(client);
597 g_app_list.removeClient(appid);
600 void WindowManager::exceptionProcessForTransition()
602 unsigned req_num = g_app_list.currentRequestNumber();
603 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
604 g_app_list.removeRequest(req_num);
605 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
606 this->processNextRequest();
609 void WindowManager::timerHandler()
611 unsigned req_num = g_app_list.currentRequestNumber();
612 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
613 g_app_list.reqDump();
614 g_app_list.removeRequest(req_num);
615 this->processNextRequest();
618 void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
621 // req_num is guaranteed by Window Manager
622 unsigned req_num = g_app_list.currentRequestNumber();
626 if (g_app_list.haveRequest())
628 HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
629 goto proc_remove_request;
633 HMI_SEQ_DEBUG(req_num, "There is no request");
638 for (auto &act : actions)
643 auto const &surface_id = this->id_alloc.lookup(act.role.c_str());
644 if(surface_id == nullopt)
646 goto proc_remove_request;
648 std::string appid = g_app_list.getAppID(*surface_id, &found);
651 if (TaskVisible::INVISIBLE == act.visible)
653 // App is killed, so do not set this action
658 HMI_SEQ_ERROR(req_num, "appid which is visible is not found");
663 auto client = g_app_list.lookUpClient(appid);
664 act.req_num = req_num;
668 ret = g_app_list.setAction(req_num, act);
669 if (ret != WMError::SUCCESS)
671 HMI_SEQ_ERROR(req_num, "Setting action is failed");
676 HMI_SEQ_DEBUG(req_num, "Start transition.");
677 ret = this->startTransition(req_num);
678 if (ret != WMError::SUCCESS)
680 if (ret == WMError::NO_LAYOUT_CHANGE)
682 goto proc_remove_request;
686 HMI_SEQ_ERROR(req_num, "Transition state is failed");
695 HMI_SEQ_ERROR(req_num, errorDescription(ret));
696 this->pmw.undoState();
699 g_app_list.removeRequest(req_num);
700 this->processNextRequest();
703 void WindowManager::processError(WMError error)
705 unsigned req_num = g_app_list.currentRequestNumber();
708 HMI_SEQ_ERROR(req_num, errorDescription(error));
709 g_app_list.removeRequest(req_num);
710 this->processNextRequest();
714 ******* Private Functions *******
717 void WindowManager::emit_activated(char const *label)
719 this->send_event(kListEventName[Event_Active], label);
722 void WindowManager::emit_deactivated(char const *label)
724 this->send_event(kListEventName[Event_Inactive], label);
727 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
729 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
732 void WindowManager::emit_syncdraw(const string &role, const string &area)
734 rect rect = this->lc->getAreaSize(area);
735 this->send_event(kListEventName[Event_SyncDraw],
736 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
739 void WindowManager::emit_flushdraw(char const *label)
741 this->send_event(kListEventName[Event_FlushDraw], label);
744 void WindowManager::emit_visible(char const *label, bool is_visible)
746 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
749 void WindowManager::emit_invisible(char const *label)
751 return emit_visible(label, false);
754 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
756 void WindowManager::activate(int id)
758 // TODO: application requests by old role,
759 // so convert role new to old for emitting event
760 /* const char* old_role = this->rolenew2old[label].c_str();
762 this->emit_visible(old_role);
763 this->emit_activated(old_role); */
766 void WindowManager::deactivate(int id)
768 // TODO: application requests by old role,
769 // so convert role new to old for emitting event
770 /* const char* old_role = this->rolenew2old[label].c_str();
772 this->emit_deactivated(old_role);
773 this->emit_invisible(old_role); */
776 WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
777 Task task, unsigned* req_num)
779 if (!g_app_list.contains(appid))
781 return WMError::NOT_REGISTERED;
784 auto client = g_app_list.lookUpClient(appid);
789 unsigned current = g_app_list.currentRequestNumber();
790 unsigned requested_num = g_app_list.getRequestNumber(appid);
791 if (requested_num != 0)
793 HMI_SEQ_INFO(requested_num,
794 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
798 WMRequest req = WMRequest(appid, role, area, task);
799 unsigned new_req = g_app_list.addRequest(req);
801 g_app_list.reqDump();
803 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
805 return WMError::SUCCESS;
808 WMError WindowManager::checkPolicy(unsigned req_num)
813 // get current trigger
815 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
816 auto trigger = g_app_list.getRequest(req_num, &found);
819 ret = WMError::NO_ENTRY;
822 string req_area = trigger.area;
824 if (trigger.task == Task::TASK_ALLOCATE)
826 const char *msg = this->check_surface_exist(trigger.role.c_str());
830 HMI_SEQ_ERROR(req_num, msg);
835 // Input event data to PolicyManager
836 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
838 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
842 // Execute state transition of PolicyManager
843 if (0 > this->pmw.executeStateTransition())
845 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
849 ret = WMError::SUCCESS;
851 g_app_list.reqDump();
856 WMError WindowManager::startTransition(unsigned req_num)
858 bool sync_draw_happen = false;
860 WMError ret = WMError::SUCCESS;
861 auto actions = g_app_list.getActions(req_num, &found);
864 ret = WMError::NO_ENTRY;
865 HMI_SEQ_ERROR(req_num,
866 "Window Manager bug :%s : Action is not set", errorDescription(ret));
870 g_app_list.reqDump();
871 for (const auto &action : actions)
873 if (action.visible == TaskVisible::VISIBLE)
875 sync_draw_happen = true;
877 // TODO: application requests by old role,
878 // so convert role new to old for emitting event
879 string old_role = this->rolenew2old[action.role];
881 this->emit_syncdraw(old_role, action.area);
882 /* TODO: emit event for app not subscriber
883 if(g_app_list.contains(y.appid))
884 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
888 if (sync_draw_happen)
894 // deactivate only, no syncDraw
895 // Make it deactivate here
896 for (const auto &x : actions)
898 this->lc->visibilityChange(x);
899 string old_role = this->rolenew2old[x.role];
900 emit_deactivated(old_role.c_str());
902 /* if (g_app_list.contains(x.appid))
904 auto client = g_app_list.lookUpClient(x.appid);
905 //this->deactivate(client->surfaceID(x.role));
908 this->lc->renderLayers();
909 ret = WMError::NO_LAYOUT_CHANGE;
914 WMError WindowManager::doEndDraw(unsigned req_num)
918 auto actions = g_app_list.getActions(req_num, &found);
919 WMError ret = WMError::SUCCESS;
922 ret = WMError::NO_ENTRY;
926 HMI_SEQ_INFO(req_num, "do endDraw");
928 // layout change and make it visible
929 for (const auto &act : actions)
931 if(act.visible != TaskVisible::NO_CHANGE)
934 ret = this->lc->layoutChange(act);
935 if(ret != WMError::SUCCESS)
937 HMI_SEQ_WARNING(req_num,
938 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
941 ret = this->lc->visibilityChange(act);
943 // Emit active/deactive event
944 string old_role = this->rolenew2old[act.role];
945 if(act.visible == VISIBLE)
947 emit_activated(old_role.c_str());
951 emit_deactivated(old_role.c_str());
954 if (ret != WMError::SUCCESS)
956 HMI_SEQ_WARNING(req_num,
957 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
960 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
963 this->lc->renderLayers();
965 HMI_SEQ_INFO(req_num, "emit flushDraw");
967 for(const auto &act_flush : actions)
969 if(act_flush.visible == TaskVisible::VISIBLE)
971 // TODO: application requests by old role,
972 // so convert role new to old for emitting event
973 string old_role = this->rolenew2old[act_flush.role];
975 this->emit_flushdraw(old_role.c_str());
982 void WindowManager::emitScreenUpdated(unsigned req_num)
985 HMI_SEQ_DEBUG(req_num, "emit screen updated");
987 auto actions = g_app_list.getActions(req_num, &found);
989 // create json object
990 json_object *j = json_object_new_object();
991 json_object *jarray = json_object_new_array();
993 for(const auto& action: actions)
995 if(action.visible != TaskVisible::INVISIBLE)
997 json_object_array_add(jarray, json_object_new_string(action.client->appID().c_str()));
1000 json_object_object_add(j, kKeyIds, jarray);
1001 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1003 int ret = afb_event_push(
1004 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1007 HMI_DEBUG("afb_event_push failed: %m");
1011 void WindowManager::setTimer()
1014 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
1015 HMI_ERROR("Could't set time (clock_gettime() returns with error");
1019 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1020 if (g_timer_ev_src == nullptr)
1022 // firsttime set into sd_event
1023 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1024 CLOCK_BOOTTIME, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL, 1, processTimerHandler, this);
1027 HMI_ERROR("Could't set timer");
1032 // update timer limitation after second time
1033 sd_event_source_set_time(g_timer_ev_src, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL);
1034 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1038 void WindowManager::stopTimer()
1040 unsigned req_num = g_app_list.currentRequestNumber();
1041 HMI_SEQ_DEBUG(req_num, "Timer stop");
1042 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1045 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1049 void WindowManager::processNextRequest()
1052 g_app_list.reqDump();
1053 unsigned req_num = g_app_list.currentRequestNumber();
1054 if (g_app_list.haveRequest())
1056 HMI_SEQ_DEBUG(req_num, "Process next request");
1057 WMError rc = checkPolicy(req_num);
1058 if (rc != WMError::SUCCESS)
1060 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1065 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1069 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1071 const char *new_role = nullptr;
1073 for (auto const &on : this->roleold2new)
1075 std::regex regex = std::regex(on.first);
1076 if (std::regex_match(old_role, regex))
1078 // role is old. So convert to new.
1079 new_role = on.second.c_str();
1084 if (nullptr == new_role)
1086 // role is new or fallback.
1087 new_role = old_role;
1090 HMI_DEBUG("old:%s -> new:%s", old_role, new_role);
1095 int WindowManager::loadOldRoleDb()
1097 // Get afm application installed dir
1098 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1099 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
1102 if (!afm_app_install_dir)
1104 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
1108 file_name = string(afm_app_install_dir) + string("/etc/old_roles.db");
1112 json_object* json_obj;
1113 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1116 HMI_ERROR("Could not open old_role.db, so use default old_role information");
1117 json_obj = json_tokener_parse(kDefaultOldRoleDb);
1119 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
1122 json_object* json_cfg;
1123 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1125 HMI_ERROR("Parse Error!!");
1129 int len = json_object_array_length(json_cfg);
1130 HMI_DEBUG("json_cfg len:%d", len);
1131 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
1133 for (int i=0; i<len; i++)
1135 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1137 const char* old_role = jh::getStringFromJson(json_tmp, "name");
1138 if (nullptr == old_role)
1140 HMI_ERROR("Parse Error!!");
1144 const char* new_role = jh::getStringFromJson(json_tmp, "new");
1145 if (nullptr == new_role)
1147 HMI_ERROR("Parse Error!!");
1151 this->roleold2new[old_role] = string(new_role);
1155 for(auto itr = this->roleold2new.begin();
1156 itr != this->roleold2new.end(); ++itr)
1158 HMI_DEBUG(">>> role old:%s new:%s",
1159 itr->first.c_str(), itr->second.c_str());
1162 // Release json_object
1163 json_object_put(json_obj);
1168 const char *WindowManager::check_surface_exist(const char *drawing_name)
1170 auto const &surface_id = this->id_alloc.lookup(string(drawing_name));
1173 return "Surface does not exist";
1176 /* if (!this->controller->surface_exists(*surface_id))
1178 return "Surface does not exist in controller!";
1181 /* auto layer_id = this->layers.get_layer_id(*surface_id);
1185 return "Surface is not on any layer!";
1188 HMI_DEBUG("surface %d is detected", *surface_id);
1192 const char* WindowManager::kDefaultOldRoleDb = "{ \
1195 \"name\": \"HomeScreen\", \
1196 \"new\": \"homescreen\" \
1199 \"name\": \"Music\", \
1200 \"new\": \"music\" \
1203 \"name\": \"MediaPlayer\", \
1204 \"new\": \"music\" \
1207 \"name\": \"Video\", \
1208 \"new\": \"video\" \
1211 \"name\": \"VideoPlayer\", \
1212 \"new\": \"video\" \
1215 \"name\": \"WebBrowser\", \
1216 \"new\": \"browser\" \
1219 \"name\": \"Radio\", \
1220 \"new\": \"radio\" \
1223 \"name\": \"Phone\", \
1224 \"new\": \"phone\" \
1227 \"name\": \"Navigation\", \
1231 \"name\": \"HVAC\", \
1235 \"name\": \"Settings\", \
1236 \"new\": \"settings\" \
1239 \"name\": \"Dashboard\", \
1240 \"new\": \"dashboard\" \
1243 \"name\": \"POI\", \
1247 \"name\": \"Mixer\", \
1248 \"new\": \"mixer\" \
1251 \"name\": \"Restriction\", \
1252 \"new\": \"restriction\" \
1255 \"name\": \"^OnScreen.*\", \
1256 \"new\": \"on_screen\" \