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.
21 #include "window_manager.hpp"
22 #include "json_helper.hpp"
23 #include "applist.hpp"
27 #include <systemd/sd-event.h>
36 static const uint64_t kTimeOut = 3ULL; /* 3s */
38 /* DrawingArea name used by "{layout}.{area}" */
39 const char kNameLayoutNormal[] = "normal";
40 const char kNameLayoutSplit[] = "split";
41 const char kNameAreaFull[] = "full";
42 const char kNameAreaMain[] = "main";
43 const char kNameAreaSub[] = "sub";
45 /* Key for json obejct */
46 const char kKeyDrawingName[] = "drawing_name";
47 const char kKeyDrawingArea[] = "drawing_area";
48 const char kKeyDrawingRect[] = "drawing_rect";
49 const char kKeyX[] = "x";
50 const char kKeyY[] = "y";
51 const char kKeyWidth[] = "width";
52 const char kKeyHeight[] = "height";
53 const char kKeyWidthPixel[] = "width_pixel";
54 const char kKeyHeightPixel[] = "height_pixel";
55 const char kKeyWidthMm[] = "width_mm";
56 const char kKeyHeightMm[] = "height_mm";
57 const char kKeyScale[] = "scale";
58 const char kKeyIds[] = "ids";
60 static sd_event_source *g_timer_ev_src = nullptr;
61 static AppList g_app_list;
62 static WindowManager *g_context;
67 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
69 HMI_NOTICE("Time out occurs because the client replys endDraw slow, so revert the request");
70 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
74 static void onStateTransitioned(vector<WMAction> actions)
76 g_context->startTransitionWrapper(actions);
81 g_context->processError(WMError::LAYOUT_CHANGE_FAIL);
88 WindowManager::WindowManager()
91 const char *path = getenv("AFM_APP_INSTALL_DIR");
94 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
98 this->lc = std::make_shared<LayerControl>(root);
100 HMI_DEBUG("Layer Controller initialized");
103 int WindowManager::init()
105 // TODO: application requests by old role,
106 // so create role map (old, new)
108 LayerControlCallbacks lmcb;
109 lmcb.surfaceCreated = [&](unsigned pid, unsigned surface){
110 this->surface_created(pid, surface);
112 lmcb.surfaceDestroyed = [&](unsigned surface){
113 this->surface_removed(surface);
115 this->lc->init(lmcb);
116 this->loadOldRoleDb();
118 // Store my context for calling callback from PolicyManager
121 // Initialize PMWrapper
122 this->pmw.initialize();
124 // Register callback to PolicyManager
125 this->pmw.registerCallback(onStateTransitioned, onError);
128 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
130 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
133 const struct rect css_bg = this->lc->getAreaSize("fullscreen");
134 Screen screen = this->lc->getScreenInfo();
135 rectangle dp_bg(screen.width(), screen.height());
137 dp_bg.set_aspect(static_cast<double>(css_bg.w) / css_bg.h);
138 dp_bg.fit(screen.width(), screen.height());
139 dp_bg.center(screen.width(), screen.height());
140 HMI_DEBUG("SCALING: CSS BG(%dx%d) -> DDP %dx%d,(%dx%d)",
141 css_bg.w, css_bg.h, dp_bg.left(), dp_bg.top(), dp_bg.width(), dp_bg.height());
143 double scale = static_cast<double>(dp_bg.height()) / css_bg.h;
144 this->lc->setupArea(dp_bg, scale);
146 return 0; //init_layers();
149 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
151 // TODO: application requests by old role,
152 // so convert role old to new
153 const char *role = this->convertRoleOldToNew(drawing_name);
155 string s_appid = appid;
156 string s_role = role;
158 if(!g_app_list.contains(s_appid))
160 // auto lid = this->layers.get_layer_id(string(role));
161 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
165 * register drawing_name as fallback and make it displayed.
167 l_id = this->lc->getNewLayerID("fallback", &l_name);
168 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
171 return Err<int>("Designated role does not match any role, fallback is disabled");
174 this->lc->createNewLayer(l_id);
175 // add client into the db
176 g_app_list.addClient(s_appid, l_id, s_role);
179 // generate surface ID for ivi-shell application
181 auto rname = this->id_alloc.lookup(string(role));
184 // name does not exist yet, allocate surface id...
185 auto id = int(this->id_alloc.generate_id(role));
186 this->tmp_surface2app[id] = {s_appid, 0};
188 // Set role map of (new, old)
189 this->rolenew2old[role] = string(drawing_name);
194 // Check currently registered drawing names if it is already there.
195 return Err<int>("Surface already present");
198 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
201 unsigned sid = std::stol(ivi_id);
203 HMI_DEBUG("This API(requestSurfaceXDG) is for XDG Application using runXDG");
205 * IVI-shell doesn't send surface_size event via ivi-wm protocol
206 * if the application is using XDG surface.
207 * So WM has to set surface size with original size here
209 WMError ret = this->lc->setXDGSurfaceOriginSize(sid);
212 HMI_ERROR("%s", errorDescription(ret));
213 HMI_WARNING("The main user of this API is runXDG");
217 // TODO: application requests by old role,
218 // so convert role old to new
219 const char *role = this->convertRoleOldToNew(drawing_name);
220 string s_role = role;
221 string s_appid = appid;
224 if(!g_app_list.contains(s_appid))
226 // auto lid = this->layers.get_layer_id(string(role));
227 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
231 * register drawing_name as fallback and make it displayed.
233 l_id = this->lc->getNewLayerID("fallback", &l_name);
234 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
237 return "Designated role does not match any role, fallback is disabled";
240 this->lc->createNewLayer(l_id);
241 // add client into the db
242 g_app_list.addClient(s_appid, l_id, s_role);
245 auto rname = this->id_alloc.lookup(s_role);
248 return "Surface already present";
251 // register pair drawing_name and ivi_id
252 this->id_alloc.register_name_id(role, sid);
254 auto client = g_app_list.lookUpClient(s_appid);
255 client->addSurface(sid);
257 // Set role map of (new, old)
258 this->rolenew2old[role] = string(drawing_name);
263 bool WindowManager::api_set_role(char const *appid, char const *drawing_name)
267 // TODO: application requests by old role,
268 // so convert role old to new
269 const char *role = this->convertRoleOldToNew(drawing_name);
270 string s_role = role;
271 string s_appid = appid;
275 if(!g_app_list.contains(s_appid))
277 // auto lid = this->layers.get_layer_id(string(role));
278 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
282 * register drawing_name as fallback and make it displayed.
284 l_id = this->lc->getNewLayerID("fallback", &l_name);
285 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
288 HMI_ERROR("Designated role does not match any role, fallback is disabled");
292 this->lc->createNewLayer(l_id);
293 // add client into the db
294 g_app_list.addClient(s_appid, l_id, s_role);
295 // Set role map of (new, old)
296 this->rolenew2old[role] = s_role;
299 // for(auto itr = this->tmp_surface2app.begin();
300 // itr != this->tmp_surface2app.end() ; ++itr)
302 for(auto& x : this->tmp_surface2app)
304 if(x.second.appid == s_appid)
306 unsigned surface = x.first;
307 auto client = g_app_list.lookUpClient(s_appid);
308 client->addSurface(surface);
309 this->tmp_surface2app.erase(surface);
310 this->id_alloc.register_name_id(s_role, surface);
316 // search floating surfaceID from pid if pid is designated.
317 wm_err = g_app_list.popFloatingSurface(pid, &surface);
320 // get floating surface with appid. If WM queries appid from pid,
321 // WM can bind surface and role with appid(not implemented yet)
322 //wm_err = g_app_list.popFloatingSurface(id);
324 if(wm_err != WMError::SUCCESS){
325 HMI_ERROR("No floating surface for app: %s", id.c_str());
326 g_app_list.addFloatingClient(id, lid, role);
327 HMI_NOTICE("%s : Waiting for surface creation", id.c_str());
332 if (g_app_list.contains(id))
334 HMI_INFO("Add role: %s with surface: %d. Client %s has multi surfaces.",
335 role.c_str(), surface, id.c_str());
336 auto client = g_app_list.lookUpClient(id);
337 client->appendRole(role);
340 HMI_INFO("Create new client: %s, surface: %d into layer: %d with role: %s",
341 id.c_str(), surface, lid, role.c_str());
342 g_app_list.addClient(id, lid, role);
345 // register pair drawing_name and ivi_id
350 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
351 char const *drawing_area, const reply_func &reply)
353 // TODO: application requests by old role,
354 // so convert role old to new
355 const char *c_role = this->convertRoleOldToNew(drawing_name);
358 string role = c_role;
359 string area = drawing_area;
361 if(!g_app_list.contains(id))
363 reply("app doesn't request 'requestSurface' or 'setRole' yet");
366 auto client = g_app_list.lookUpClient(id);
368 // unsigned srfc = client->surfaceID(role);
369 // unsigned layer = client->layerID();
371 // g_app_list.removeFloatingSurface(client->surfaceID());
372 // g_app_list.removeFloatingSurface(client);
374 Task task = Task::TASK_ALLOCATE;
375 unsigned req_num = 0;
376 WMError ret = WMError::UNKNOWN;
378 ret = this->setRequest(id, role, area, task, &req_num);
380 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
381 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
383 if(ret != WMError::SUCCESS)
385 HMI_ERROR(errorDescription(ret));
386 reply("Failed to set request");
391 if (req_num != g_app_list.currentRequestNumber())
393 // Add request, then invoked after the previous task is finished
394 HMI_SEQ_DEBUG(req_num, "request is accepted");
401 ret = this->checkPolicy(req_num);
403 if (ret != WMError::SUCCESS)
406 HMI_SEQ_ERROR(req_num, errorDescription(ret));
407 g_app_list.removeRequest(req_num);
408 this->processNextRequest();
412 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
413 const reply_func &reply)
415 // TODO: application requests by old role,
416 // so convert role old to new
417 const char *c_role = this->convertRoleOldToNew(drawing_name);
423 string role = c_role;
424 string area = ""; //drawing_area;
425 Task task = Task::TASK_RELEASE;
426 unsigned req_num = 0;
427 WMError ret = WMError::UNKNOWN;
429 ret = this->setRequest(id, role, area, task, &req_num);
431 if (ret != WMError::SUCCESS)
433 HMI_ERROR(errorDescription(ret));
434 reply("Failed to set request");
439 if (req_num != g_app_list.currentRequestNumber())
441 // Add request, then invoked after the previous task is finished
442 HMI_SEQ_DEBUG(req_num, "request is accepted");
449 ret = this->checkPolicy(req_num);
451 if (ret != WMError::SUCCESS)
454 HMI_SEQ_ERROR(req_num, errorDescription(ret));
455 g_app_list.removeRequest(req_num);
456 this->processNextRequest();
460 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
462 // TODO: application requests by old role,
463 // so convert role old to new
464 const char *c_role = this->convertRoleOldToNew(drawing_name);
467 string role = c_role;
468 unsigned current_req = g_app_list.currentRequestNumber();
469 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
473 HMI_ERROR("%s is not in transition state", id.c_str());
477 if (g_app_list.endDrawFullfilled(current_req))
479 // do task for endDraw
481 WMError ret = this->doEndDraw(current_req);
483 if(ret != WMError::SUCCESS)
485 //this->emit_error();
487 // Undo state of PolicyManager
488 this->pmw.undoState();
489 this->lc->undoUpdate();
491 this->emitScreenUpdated(current_req);
492 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
494 g_app_list.removeRequest(current_req);
496 this->processNextRequest();
500 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
505 bool WindowManager::api_client_set_render_order(char const* appid, const vector<string>& render_order)
509 auto client = g_app_list.lookUpClient(id);
512 client->setRenderOrder(render_order);
517 string WindowManager::api_client_attach_service_surface
518 (const char* appid, const char* dest, const char* service_surface)
520 string uuid, s_dest = dest;
521 auto client = g_app_list.lookUpClient(s_dest);
524 HMI_ERROR("Failed to look up destination [%s]", dest);
527 uuid = client->attachTmpServiceSurface(appid, service_surface);
528 this->tmp_services.emplace_back(TmpService{appid, dest, service_surface, uuid});
532 result<json_object *> WindowManager::api_get_display_info()
534 Screen screen = this->lc->getScreenInfo();
536 json_object *object = json_object_new_object();
537 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(screen.width()));
538 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(screen.height()));
540 json_object_object_add(object, kKeyWidthMm, json_object_new_int(0));
541 json_object_object_add(object, kKeyHeightMm, json_object_new_int(0));
542 json_object_object_add(object, kKeyScale, json_object_new_double(this->lc->scale()));
544 return Ok<json_object *>(object);
547 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
551 // TODO: application requests by old role,
552 // so convert role old to new
553 const char *role = this->convertRoleOldToNew(drawing_name);
555 // Check drawing name, surface/layer id
556 auto const &surface_id = this->id_alloc.lookup(string(role));
559 return Err<json_object *>("Surface does not exist");
562 // Set area rectangle
563 rect area_info = this->area_info[*surface_id];
564 json_object *object = json_object_new_object();
565 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
566 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
567 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
568 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
570 return Ok<json_object *>(object);
573 void WindowManager::send_event(char const *evname, char const *label)
575 HMI_DEBUG("%s: %s(%s)", __func__, evname, label);
577 json_object *j = json_object_new_object();
578 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
580 int ret = afb_event_push(this->map_afb_event[evname], j);
583 HMI_DEBUG("afb_event_push failed: %m");
587 void WindowManager::send_event(char const *evname, char const *label, char const *area,
588 int x, int y, int w, int h)
590 HMI_DEBUG("%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
591 __func__, evname, label, area, x, y, w, h);
593 json_object *j_rect = json_object_new_object();
594 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
595 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
596 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
597 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
599 json_object *j = json_object_new_object();
600 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
601 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
602 json_object_object_add(j, kKeyDrawingRect, j_rect);
604 int ret = afb_event_push(this->map_afb_event[evname], j);
607 HMI_DEBUG("afb_event_push failed: %m");
614 void WindowManager::surface_created(unsigned pid, unsigned surface_id)
617 if(this->tmp_surface2app.count(surface_id) != 0)
619 string appid = this->tmp_surface2app[surface_id].appid;
620 auto client = g_app_list.lookUpClient(appid);
621 if(client != nullptr)
623 WMError ret = client->addSurface(surface_id);
624 HMI_INFO("Add surface %d to \"%s\"", surface_id, appid.c_str());
625 if(ret != WMError::SUCCESS)
627 HMI_ERROR("Failed to add surface to client %s", client->appID().c_str());
630 this->tmp_surface2app.erase(surface_id);
634 HMI_NOTICE("Unknown surface %d", surface_id);
636 std::ostringstream os;
638 string path = "/proc/" + os.str() + "/stat";
639 std::ifstream ifs(path.c_str());
642 if(!ifs.fail() && std::getline(ifs, str))
644 std::sscanf(str.data(), "%*d %*s %*c %d", &ppid);
645 HMI_INFO("Retrieve ppid %d", ppid);
649 HMI_ERROR("Failed to open /proc/%d/stat", pid);
650 HMI_ERROR("File system may be different");
653 // search pid from surfaceID
654 json_object *response;
655 afb_service_call_sync("afm-main", "runners", nullptr, &response);
657 // retrieve appid from pid from application manager
659 if(response == nullptr)
661 HMI_ERROR("No runners");
665 // check appid then add it to the client
666 HMI_INFO("Runners:%s", json_object_get_string(response));
667 int size = json_object_array_length(response);
668 for(int i = 0; i < size; i++)
670 json_object *j = json_object_array_get_idx(response, i);
671 const char* id = jh::getStringFromJson(j, "id");
672 int runid = jh::getIntFromJson(j, "runid");
673 if(id && (runid > 0))
684 HMI_ERROR("Not found");
687 json_object_put(response);
689 auto elem = std::find_if(this->tmp_services.begin(), this->tmp_services.end(),
690 [&appid](TmpService& ts){
691 return (ts.dest == appid );
693 if(elem == this->tmp_services.end())
696 auto client = g_app_list.lookUpClient(elem->dest);
697 if(client == nullptr)
701 HMI_INFO("Attach surface %d (service %s) to app %s", surface_id, elem->service.c_str(), elem->dest.c_str());
702 client->attachServiceSurface(elem->service, surface_id);
707 auto client = g_app_list.lookUpClient(appid);
708 if(client != nullptr)
710 client->addSurface(surface_id);
711 this->id_alloc.register_name_id(client->role(), surface_id);
716 * Store tmp surface and appid for application
717 * who requests setRole after creating shell surface
719 this->tmp_surface2app.emplace(surface_id, TmpClient{appid, ppid});
725 void WindowManager::surface_removed(unsigned surface_id)
727 HMI_DEBUG("Delete surface_id %u", surface_id);
728 this->id_alloc.remove_id(surface_id);
729 // this->layers.remove_surface(surface_id);
730 g_app_list.removeSurface(surface_id);
733 void WindowManager::removeClient(const string &appid)
735 HMI_DEBUG("Remove clinet %s from list", appid.c_str());
736 auto client = g_app_list.lookUpClient(appid);
737 this->lc->terminateApp(client);
738 g_app_list.removeClient(appid);
741 void WindowManager::exceptionProcessForTransition()
743 unsigned req_num = g_app_list.currentRequestNumber();
744 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
745 g_app_list.removeRequest(req_num);
746 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
747 this->processNextRequest();
750 void WindowManager::timerHandler()
752 unsigned req_num = g_app_list.currentRequestNumber();
753 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
754 g_app_list.reqDump();
755 g_app_list.removeRequest(req_num);
756 this->processNextRequest();
759 void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
762 // req_num is guaranteed by Window Manager
763 unsigned req_num = g_app_list.currentRequestNumber();
767 if (g_app_list.haveRequest())
769 HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
770 goto proc_remove_request;
774 HMI_SEQ_DEBUG(req_num, "There is no request");
779 for (auto &act : actions)
784 auto const &surface_id = this->id_alloc.lookup(act.role.c_str());
785 if(surface_id == nullopt)
787 goto proc_remove_request;
789 std::string appid = g_app_list.getAppID(*surface_id, &found);
792 if (TaskVisible::INVISIBLE == act.visible)
794 // App is killed, so do not set this action
799 HMI_SEQ_ERROR(req_num, "appid which is visible is not found");
804 auto client = g_app_list.lookUpClient(appid);
805 act.req_num = req_num;
809 ret = g_app_list.setAction(req_num, act);
810 if (ret != WMError::SUCCESS)
812 HMI_SEQ_ERROR(req_num, "Setting action is failed");
817 HMI_SEQ_DEBUG(req_num, "Start transition.");
818 ret = this->startTransition(req_num);
819 if (ret != WMError::SUCCESS)
821 if (ret == WMError::NO_LAYOUT_CHANGE)
823 goto proc_remove_request;
827 HMI_SEQ_ERROR(req_num, "Transition state is failed");
836 HMI_SEQ_ERROR(req_num, errorDescription(ret));
837 this->pmw.undoState();
840 g_app_list.removeRequest(req_num);
841 this->processNextRequest();
844 void WindowManager::processError(WMError error)
846 unsigned req_num = g_app_list.currentRequestNumber();
849 HMI_SEQ_ERROR(req_num, errorDescription(error));
850 g_app_list.removeRequest(req_num);
851 this->processNextRequest();
855 ******* Private Functions *******
858 void WindowManager::emit_activated(char const *label)
860 this->send_event(kListEventName[Event_Active], label);
863 void WindowManager::emit_deactivated(char const *label)
865 this->send_event(kListEventName[Event_Inactive], label);
868 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
870 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
873 void WindowManager::emit_syncdraw(const string &role, const string &area)
875 rect rect = this->lc->getAreaSize(area);
876 this->send_event(kListEventName[Event_SyncDraw],
877 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
880 void WindowManager::emit_flushdraw(char const *label)
882 this->send_event(kListEventName[Event_FlushDraw], label);
885 void WindowManager::emit_visible(char const *label, bool is_visible)
887 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
890 void WindowManager::emit_invisible(char const *label)
892 return emit_visible(label, false);
895 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
897 void WindowManager::activate(int id)
899 // TODO: application requests by old role,
900 // so convert role new to old for emitting event
901 /* const char* old_role = this->rolenew2old[label].c_str();
903 this->emit_visible(old_role);
904 this->emit_activated(old_role); */
907 void WindowManager::deactivate(int id)
909 // TODO: application requests by old role,
910 // so convert role new to old for emitting event
911 /* const char* old_role = this->rolenew2old[label].c_str();
913 this->emit_deactivated(old_role);
914 this->emit_invisible(old_role); */
917 WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
918 Task task, unsigned* req_num)
920 if (!g_app_list.contains(appid))
922 return WMError::NOT_REGISTERED;
925 auto client = g_app_list.lookUpClient(appid);
930 unsigned current = g_app_list.currentRequestNumber();
931 unsigned requested_num = g_app_list.getRequestNumber(appid);
932 if (requested_num != 0)
934 HMI_SEQ_INFO(requested_num,
935 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
939 WMRequest req = WMRequest(appid, role, area, task);
940 unsigned new_req = g_app_list.addRequest(req);
942 g_app_list.reqDump();
944 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
946 return WMError::SUCCESS;
949 WMError WindowManager::checkPolicy(unsigned req_num)
954 // get current trigger
956 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
957 auto trigger = g_app_list.getRequest(req_num, &found);
960 ret = WMError::NO_ENTRY;
963 string req_area = trigger.area;
965 if (trigger.task == Task::TASK_ALLOCATE)
967 const char *msg = this->check_surface_exist(trigger.role.c_str());
971 HMI_SEQ_ERROR(req_num, msg);
976 // Input event data to PolicyManager
977 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
979 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
983 // Execute state transition of PolicyManager
984 if (0 > this->pmw.executeStateTransition())
986 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
990 ret = WMError::SUCCESS;
992 g_app_list.reqDump();
997 WMError WindowManager::startTransition(unsigned req_num)
999 bool sync_draw_happen = false;
1001 WMError ret = WMError::SUCCESS;
1002 auto actions = g_app_list.getActions(req_num, &found);
1005 ret = WMError::NO_ENTRY;
1006 HMI_SEQ_ERROR(req_num,
1007 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1011 g_app_list.reqDump();
1012 for (const auto &action : actions)
1014 if (action.visible == TaskVisible::VISIBLE)
1016 sync_draw_happen = true;
1018 // TODO: application requests by old role,
1019 // so convert role new to old for emitting event
1020 string old_role = this->rolenew2old[action.role];
1022 this->emit_syncdraw(old_role, action.area);
1023 /* TODO: emit event for app not subscriber
1024 if(g_app_list.contains(y.appid))
1025 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1029 if (sync_draw_happen)
1035 // deactivate only, no syncDraw
1036 // Make it deactivate here
1037 for (const auto &x : actions)
1039 this->lc->visibilityChange(x);
1040 string old_role = this->rolenew2old[x.role];
1041 emit_deactivated(old_role.c_str());
1043 /* if (g_app_list.contains(x.appid))
1045 auto client = g_app_list.lookUpClient(x.appid);
1046 //this->deactivate(client->surfaceID(x.role));
1049 this->lc->renderLayers();
1050 ret = WMError::NO_LAYOUT_CHANGE;
1055 WMError WindowManager::doEndDraw(unsigned req_num)
1059 auto actions = g_app_list.getActions(req_num, &found);
1060 WMError ret = WMError::SUCCESS;
1063 ret = WMError::NO_ENTRY;
1067 HMI_SEQ_INFO(req_num, "do endDraw");
1069 // layout change and make it visible
1070 for (const auto &act : actions)
1072 if(act.visible != TaskVisible::NO_CHANGE)
1075 ret = this->lc->layoutChange(act);
1076 if(ret != WMError::SUCCESS)
1078 HMI_SEQ_WARNING(req_num,
1079 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1082 ret = this->lc->visibilityChange(act);
1084 // Emit active/deactive event
1085 string old_role = this->rolenew2old[act.role];
1086 if(act.visible == VISIBLE)
1088 emit_activated(old_role.c_str());
1092 emit_deactivated(old_role.c_str());
1095 if (ret != WMError::SUCCESS)
1097 HMI_SEQ_WARNING(req_num,
1098 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1101 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1104 this->lc->renderLayers();
1106 HMI_SEQ_INFO(req_num, "emit flushDraw");
1108 for(const auto &act_flush : actions)
1110 if(act_flush.visible == TaskVisible::VISIBLE)
1112 // TODO: application requests by old role,
1113 // so convert role new to old for emitting event
1114 string old_role = this->rolenew2old[act_flush.role];
1116 this->emit_flushdraw(old_role.c_str());
1123 void WindowManager::emitScreenUpdated(unsigned req_num)
1126 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1128 auto actions = g_app_list.getActions(req_num, &found);
1130 // create json object
1131 json_object *j = json_object_new_object();
1132 json_object *jarray = json_object_new_array();
1134 for(const auto& action: actions)
1136 if(action.visible != TaskVisible::INVISIBLE)
1138 json_object_array_add(jarray, json_object_new_string(action.client->appID().c_str()));
1141 json_object_object_add(j, kKeyIds, jarray);
1142 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1144 int ret = afb_event_push(
1145 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1148 HMI_DEBUG("afb_event_push failed: %m");
1152 void WindowManager::setTimer()
1155 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
1156 HMI_ERROR("Could't set time (clock_gettime() returns with error");
1160 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1161 if (g_timer_ev_src == nullptr)
1163 // firsttime set into sd_event
1164 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1165 CLOCK_BOOTTIME, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL, 1, processTimerHandler, this);
1168 HMI_ERROR("Could't set timer");
1173 // update timer limitation after second time
1174 sd_event_source_set_time(g_timer_ev_src, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL);
1175 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1179 void WindowManager::stopTimer()
1181 unsigned req_num = g_app_list.currentRequestNumber();
1182 HMI_SEQ_DEBUG(req_num, "Timer stop");
1183 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1186 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1190 void WindowManager::processNextRequest()
1193 g_app_list.reqDump();
1194 unsigned req_num = g_app_list.currentRequestNumber();
1195 if (g_app_list.haveRequest())
1197 HMI_SEQ_DEBUG(req_num, "Process next request");
1198 WMError rc = checkPolicy(req_num);
1199 if (rc != WMError::SUCCESS)
1201 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1206 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1210 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1212 const char *new_role = nullptr;
1214 for (auto const &on : this->roleold2new)
1216 std::regex regex = std::regex(on.first);
1217 if (std::regex_match(old_role, regex))
1219 // role is old. So convert to new.
1220 new_role = on.second.c_str();
1225 if (nullptr == new_role)
1227 // role is new or fallback.
1228 new_role = old_role;
1231 HMI_DEBUG("old:%s -> new:%s", old_role, new_role);
1236 int WindowManager::loadOldRoleDb()
1238 // Get afm application installed dir
1239 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1240 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
1243 if (!afm_app_install_dir)
1245 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
1249 file_name = string(afm_app_install_dir) + string("/etc/old_roles.db");
1253 json_object* json_obj;
1254 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1257 HMI_ERROR("Could not open old_role.db, so use default old_role information");
1258 json_obj = json_tokener_parse(kDefaultOldRoleDb);
1260 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
1263 json_object* json_cfg;
1264 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1266 HMI_ERROR("Parse Error!!");
1270 int len = json_object_array_length(json_cfg);
1271 HMI_DEBUG("json_cfg len:%d", len);
1272 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
1274 for (int i=0; i<len; i++)
1276 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1278 const char* old_role = jh::getStringFromJson(json_tmp, "name");
1279 if (nullptr == old_role)
1281 HMI_ERROR("Parse Error!!");
1285 const char* new_role = jh::getStringFromJson(json_tmp, "new");
1286 if (nullptr == new_role)
1288 HMI_ERROR("Parse Error!!");
1292 this->roleold2new[old_role] = string(new_role);
1296 for(auto itr = this->roleold2new.begin();
1297 itr != this->roleold2new.end(); ++itr)
1299 HMI_DEBUG(">>> role old:%s new:%s",
1300 itr->first.c_str(), itr->second.c_str());
1303 // Release json_object
1304 json_object_put(json_obj);
1309 const char *WindowManager::check_surface_exist(const char *drawing_name)
1311 auto const &surface_id = this->id_alloc.lookup(string(drawing_name));
1314 return "Surface does not exist";
1317 /* if (!this->controller->surface_exists(*surface_id))
1319 return "Surface does not exist in controller!";
1322 /* auto layer_id = this->layers.get_layer_id(*surface_id);
1326 return "Surface is not on any layer!";
1329 HMI_DEBUG("surface %d is detected", *surface_id);
1333 const char* WindowManager::kDefaultOldRoleDb = "{ \
1336 \"name\": \"HomeScreen\", \
1337 \"new\": \"homescreen\" \
1340 \"name\": \"Music\", \
1341 \"new\": \"music\" \
1344 \"name\": \"MediaPlayer\", \
1345 \"new\": \"music\" \
1348 \"name\": \"Video\", \
1349 \"new\": \"video\" \
1352 \"name\": \"VideoPlayer\", \
1353 \"new\": \"video\" \
1356 \"name\": \"WebBrowser\", \
1357 \"new\": \"browser\" \
1360 \"name\": \"Radio\", \
1361 \"new\": \"radio\" \
1364 \"name\": \"Phone\", \
1365 \"new\": \"phone\" \
1368 \"name\": \"Navigation\", \
1372 \"name\": \"HVAC\", \
1376 \"name\": \"Settings\", \
1377 \"new\": \"settings\" \
1380 \"name\": \"Dashboard\", \
1381 \"new\": \"dashboard\" \
1384 \"name\": \"POI\", \
1388 \"name\": \"Mixer\", \
1389 \"new\": \"mixer\" \
1392 \"name\": \"Restriction\", \
1393 \"new\": \"restriction\" \
1396 \"name\": \"^OnScreen.*\", \
1397 \"new\": \"on_screen\" \