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 const char kPathOldRolesConfigFile[] = "/etc/old_roles.json";
62 static sd_event_source *g_timer_ev_src = nullptr;
63 static AppList g_app_list;
64 static WindowManager *g_context;
68 AfbClosure(unsigned pid, unsigned ppid, unsigned surface)
69 : pid(pid), ppid(ppid), surface(surface) {}
70 ~AfbClosure() = default;
79 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
81 HMI_NOTICE("Time out occurs because the client replys endDraw slow, so revert the request");
82 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
86 static void onStateTransitioned(vector<WMAction> actions)
88 g_context->startTransitionWrapper(actions);
93 g_context->processError(WMError::LAYOUT_CHANGE_FAIL);
96 static void onReceiveRemoteRequest(json_object *data)
98 g_context->processForRemoteRequest(data);
105 WindowManager::WindowManager()
109 const char *path = getenv("AFM_APP_INSTALL_DIR");
112 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
116 // TODO: ECU name should be decide by config file
117 // Get mode and decide ECU name
118 string ecu_name = this->wmcon.getEcuName();
120 this->lc = std::make_shared<LayerControl>(root, ecu_name);
122 HMI_DEBUG("Layer Controller initialized");
125 int WindowManager::init()
127 LayerControlCallbacks lmcb;
128 lmcb.surfaceCreated = [&](unsigned pid, unsigned surface){
129 this->surface_created(pid, surface);
131 lmcb.surfaceDestroyed = [&](unsigned surface){
132 this->surface_removed(surface);
134 this->lc->init(lmcb);
136 // TODO: application requests by old role,
137 // so create role map (old, new)
138 // Load old_roles config file
139 this->loadOldRolesConfigFile();
141 // Initialize LowCanClient
142 this->lcc.initialize();
144 // Store my context for calling callback from PolicyManager
147 // Initialize PMWrapper
148 this->pmw.initialize(this->wmcon.getEcuName());
150 // Register callback to PolicyManager
151 this->pmw.registerCallback(onStateTransitioned, onError);
153 // Initialize WMConnection
154 this->wmcon.initialize();
156 // Register callback to WMConnection
157 this->wmcon.registerCallback(onReceiveRemoteRequest);
160 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
162 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
165 const struct rect css_bg = this->lc->getAreaSize("fullscreen");
166 Screen screen = this->lc->getScreenInfo();
167 rectangle dp_bg(screen.width(), screen.height());
169 dp_bg.set_aspect(static_cast<double>(css_bg.w) / css_bg.h);
170 dp_bg.fit(screen.width(), screen.height());
171 dp_bg.center(screen.width(), screen.height());
172 HMI_DEBUG("SCALING: CSS BG(%dx%d) -> DDP %dx%d,(%dx%d)",
173 css_bg.w, css_bg.h, dp_bg.left(), dp_bg.top(), dp_bg.width(), dp_bg.height());
175 double scale = static_cast<double>(dp_bg.height()) / css_bg.h;
176 this->lc->setupArea(dp_bg, scale);
178 return 0; //init_layers();
181 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
183 // TODO: application requests by old role,
184 // so convert role old to new
185 const char *role = this->convertRoleOldToNew(drawing_name);
187 string s_appid = appid;
188 string s_role = role;
190 if(!g_app_list.contains(s_appid))
192 // auto lid = this->layers.get_layer_id(string(role));
193 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
197 * register drawing_name as fallback and make it displayed.
199 l_id = this->lc->getNewLayerID("fallback", &l_name);
200 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
203 return Err<int>("Designated role does not match any role, fallback is disabled");
207 // TODO: remote layer size is fixed value
208 if ("Remote" == l_name)
210 this->lc->createNewRemoteLayer(l_id);
214 this->lc->createNewLayer(l_id);
217 // add client into the db
218 g_app_list.addClient(s_appid, l_id, s_role);
221 // generate surface ID for ivi-shell application
223 auto rname = this->id_alloc.lookup(string(role));
226 // name does not exist yet, allocate surface id...
227 auto id = int(this->id_alloc.generate_id(role));
228 this->tmp_surface2app[id] = {s_appid, 0};
230 // Set role map of (new, old)
231 this->rolenew2old[role] = string(drawing_name);
236 // Check currently registered drawing names if it is already there.
237 return Err<int>("Surface already present");
240 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
243 unsigned sid = std::stol(ivi_id);
245 HMI_DEBUG("This API(requestSurfaceXDG) is for XDG Application using runXDG");
247 * IVI-shell doesn't send surface_size event via ivi-wm protocol
248 * if the application is using XDG surface.
249 * So WM has to set surface size with original size here
251 WMError ret = this->lc->setXDGSurfaceOriginSize(sid);
254 HMI_ERROR("%s", errorDescription(ret));
255 HMI_WARNING("The main user of this API is runXDG");
259 // TODO: application requests by old role,
260 // so convert role old to new
261 const char *role = this->convertRoleOldToNew(drawing_name);
262 string s_role = role;
263 string s_appid = appid;
266 if(!g_app_list.contains(s_appid))
268 // auto lid = this->layers.get_layer_id(string(role));
269 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
273 * register drawing_name as fallback and make it displayed.
275 l_id = this->lc->getNewLayerID("fallback", &l_name);
276 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
279 return "Designated role does not match any role, fallback is disabled";
283 // TODO: remote layer size is fixed value
284 if ("Remote" == l_name)
286 this->lc->createNewRemoteLayer(l_id);
290 this->lc->createNewLayer(l_id);
293 // add client into the db
294 g_app_list.addClient(s_appid, l_id, s_role);
297 auto rname = this->id_alloc.lookup(s_role);
300 return "Surface already present";
303 // register pair drawing_name and ivi_id
304 this->id_alloc.register_name_id(role, sid);
306 auto client = g_app_list.lookUpClient(s_appid);
307 client->addSurface(sid);
309 // Set role map of (new, old)
310 this->rolenew2old[role] = string(drawing_name);
315 bool WindowManager::api_set_role(char const *appid, char const *drawing_name)
319 // TODO: application requests by old role,
320 // so convert role old to new
321 const char *role = this->convertRoleOldToNew(drawing_name);
322 string s_role = role;
323 string s_appid = appid;
327 if(!g_app_list.contains(s_appid))
329 // auto lid = this->layers.get_layer_id(string(role));
330 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
334 * register drawing_name as fallback and make it displayed.
336 l_id = this->lc->getNewLayerID("fallback", &l_name);
337 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
340 HMI_ERROR("Designated role does not match any role, fallback is disabled");
345 // TODO: remote layer size is fixed value
346 if ("Remote" == l_name)
348 this->lc->createNewRemoteLayer(l_id);
352 this->lc->createNewLayer(l_id);
355 // add client into the db
356 g_app_list.addClient(s_appid, l_id, s_role);
357 // Set role map of (new, old)
358 this->rolenew2old[role] = string(drawing_name);
361 // for(auto itr = this->tmp_surface2app.begin();
362 // itr != this->tmp_surface2app.end() ; ++itr)
364 for(auto& x : this->tmp_surface2app)
366 if(x.second.appid == s_appid)
368 unsigned surface = x.first;
369 auto client = g_app_list.lookUpClient(s_appid);
370 client->addSurface(surface);
371 this->tmp_surface2app.erase(surface);
372 this->id_alloc.register_name_id(s_role, surface);
378 // search floating surfaceID from pid if pid is designated.
379 wm_err = g_app_list.popFloatingSurface(pid, &surface);
382 // get floating surface with appid. If WM queries appid from pid,
383 // WM can bind surface and role with appid(not implemented yet)
384 //wm_err = g_app_list.popFloatingSurface(id);
386 if(wm_err != WMError::SUCCESS){
387 HMI_ERROR("No floating surface for app: %s", id.c_str());
388 g_app_list.addFloatingClient(id, lid, role);
389 HMI_NOTICE("%s : Waiting for surface creation", id.c_str());
394 if (g_app_list.contains(id))
396 HMI_INFO("Add role: %s with surface: %d. Client %s has multi surfaces.",
397 role.c_str(), surface, id.c_str());
398 auto client = g_app_list.lookUpClient(id);
399 client->appendRole(role);
402 HMI_INFO("Create new client: %s, surface: %d into layer: %d with role: %s",
403 id.c_str(), surface, lid, role.c_str());
404 g_app_list.addClient(id, lid, role);
407 // register pair drawing_name and ivi_id
412 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
413 char const *drawing_area, 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);
420 string role = c_role;
421 string area = drawing_area;
423 if(!g_app_list.contains(id))
425 reply("app doesn't request 'requestSurface' or 'setRole' yet");
428 auto client = g_app_list.lookUpClient(id);
430 // unsigned srfc = client->surfaceID(role);
431 // unsigned layer = client->layerID();
433 // g_app_list.removeFloatingSurface(client->surfaceID());
434 // g_app_list.removeFloatingSurface(client);
436 Task task = Task::TASK_ALLOCATE;
437 unsigned req_num = 0;
438 WMError ret = WMError::UNKNOWN;
440 ret = this->setRequest(id, role, area, task, &req_num);
442 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
443 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
445 if(ret != WMError::SUCCESS)
447 HMI_ERROR(errorDescription(ret));
448 reply("Failed to set request");
453 if (req_num != g_app_list.currentRequestNumber())
455 // Add request, then invoked after the previous task is finished
456 HMI_SEQ_DEBUG(req_num, "request is accepted");
463 ret = this->checkPolicy(req_num);
465 if (ret != WMError::SUCCESS)
468 HMI_SEQ_ERROR(req_num, errorDescription(ret));
469 g_app_list.removeRequest(req_num);
470 this->processNextRequest();
474 void WindowManager::api_activate_surface_for_slave(
475 char const *appid, char const *drawing_name,
476 char const *drawing_area, const reply_func &reply)
478 // TODO: application requests by old role,
479 // so convert role old to new
480 const char *c_role = this->convertRoleOldToNew(drawing_name);
483 string role = c_role;
484 string area = drawing_area;
486 if(!g_app_list.contains(id))
488 // Request surface of app in slave to register app information
489 this->api_request_surface(appid, drawing_name);
491 // Set role of app in slave to register app information
492 this->api_set_role(appid, drawing_name);
494 auto client = g_app_list.lookUpClient(id);
496 // unsigned srfc = client->surfaceID(role);
497 // unsigned layer = client->layerID();
499 // g_app_list.removeFloatingSurface(client->surfaceID());
500 // g_app_list.removeFloatingSurface(client);
502 Task task = Task::TASK_ALLOCATE;
503 unsigned req_num = 0;
504 WMError ret = WMError::UNKNOWN;
506 ret = this->setRequestForSlave(id, role, area, task, &req_num);
508 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
509 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
511 if(ret != WMError::SUCCESS)
513 HMI_ERROR(errorDescription(ret));
514 reply("Failed to set request");
519 if (req_num != g_app_list.currentRequestNumber())
521 // Add request, then invoked after the previous task is finished
522 HMI_SEQ_DEBUG(req_num, "request is accepted");
529 ret = this->checkPolicyForSlave(req_num);
531 if (ret != WMError::SUCCESS)
534 HMI_SEQ_ERROR(req_num, errorDescription(ret));
535 g_app_list.removeRequest(req_num);
536 this->processNextRequest();
540 void WindowManager::api_activate_surface_to_master(
541 char const *appid, char const *drawing_name,
542 char const *drawing_area, const reply_func &reply)
544 // TODO: application requests by old role,
545 // so convert role old to new
546 const char *c_role = this->convertRoleOldToNew(drawing_name);
549 string role = c_role;
550 string area = drawing_area;
552 if(!g_app_list.contains(id))
554 reply("app doesn't request 'requestSurface' or 'setRole' yet");
557 auto client = g_app_list.lookUpClient(id);
559 // unsigned srfc = client->surfaceID(role);
560 // unsigned layer = client->layerID();
562 // g_app_list.removeFloatingSurface(client->surfaceID());
563 // g_app_list.removeFloatingSurface(client);
565 Task task = Task::TASK_ALLOCATE;
566 unsigned req_num = 0;
567 WMError ret = WMError::UNKNOWN;
569 ret = this->setRequest(id, role, area, task, &req_num);
571 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
572 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
574 if(ret != WMError::SUCCESS)
576 HMI_ERROR(errorDescription(ret));
577 reply("Failed to set request");
582 if (req_num != g_app_list.currentRequestNumber())
584 // Add request, then invoked after the previous task is finished
585 HMI_SEQ_DEBUG(req_num, "request is accepted");
592 int i_ret = this->wmcon.sendRequest("activateWindow", appid,
593 drawing_name, drawing_area);
597 HMI_SEQ_ERROR(req_num, errorDescription(ret));
598 g_app_list.removeRequest(req_num);
599 this->processNextRequest();
605 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
606 const reply_func &reply)
608 // TODO: application requests by old role,
609 // so convert role old to new
610 const char *c_role = this->convertRoleOldToNew(drawing_name);
616 string role = c_role;
617 string area = ""; //drawing_area;
618 Task task = Task::TASK_RELEASE;
619 unsigned req_num = 0;
620 WMError ret = WMError::UNKNOWN;
622 ret = this->setRequest(id, role, area, task, &req_num);
624 if (ret != WMError::SUCCESS)
626 HMI_ERROR(errorDescription(ret));
627 reply("Failed to set request");
632 if (req_num != g_app_list.currentRequestNumber())
634 // Add request, then invoked after the previous task is finished
635 HMI_SEQ_DEBUG(req_num, "request is accepted");
642 ret = this->checkPolicy(req_num);
644 if (ret != WMError::SUCCESS)
647 HMI_SEQ_ERROR(req_num, errorDescription(ret));
648 g_app_list.removeRequest(req_num);
649 this->processNextRequest();
653 void WindowManager::api_deactivate_surface_for_slave(char const *appid, char const *drawing_name,
654 const reply_func &reply)
656 // TODO: application requests by old role,
657 // so convert role old to new
658 const char *c_role = this->convertRoleOldToNew(drawing_name);
664 string role = c_role;
665 string area = "";//drawing_area;
666 Task task = Task::TASK_RELEASE;
667 unsigned req_num = 0;
668 WMError ret = WMError::UNKNOWN;
670 ret = this->setRequest(id, role, area, task, &req_num);
672 if (ret != WMError::SUCCESS)
674 HMI_ERROR(errorDescription(ret));
675 reply("Failed to set request");
680 if (req_num != g_app_list.currentRequestNumber())
682 // Add request, then invoked after the previous task is finished
683 HMI_SEQ_DEBUG(req_num, "request is accepted");
690 ret = this->checkPolicyForSlave(req_num);
692 if (ret != WMError::SUCCESS)
695 HMI_SEQ_ERROR(req_num, errorDescription(ret));
696 g_app_list.removeRequest(req_num);
697 this->processNextRequest();
701 void WindowManager::api_deactivate_surface_to_master(char const *appid, char const *drawing_name,
702 const reply_func &reply)
704 // TODO: application requests by old role,
705 // so convert role old to new
706 const char *c_role = this->convertRoleOldToNew(drawing_name);
712 string role = c_role;
713 string area = "";//drawing_area;
714 Task task = Task::TASK_RELEASE;
715 unsigned req_num = 0;
716 WMError ret = WMError::UNKNOWN;
718 ret = this->setRequest(id, role, area, task, &req_num);
720 if (ret != WMError::SUCCESS)
722 HMI_ERROR(errorDescription(ret));
723 reply("Failed to set request");
728 if (req_num != g_app_list.currentRequestNumber())
730 // Add request, then invoked after the previous task is finished
731 HMI_SEQ_DEBUG(req_num, "request is accepted");
738 int i_ret = this->wmcon.sendRequest("deactivateWindow", appid,
743 HMI_SEQ_ERROR(req_num, errorDescription(ret));
744 g_app_list.removeRequest(req_num);
745 this->processNextRequest();
751 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
753 // TODO: application requests by old role,
754 // so convert role old to new
755 const char *c_role = this->convertRoleOldToNew(drawing_name);
758 string role = c_role;
759 unsigned current_req = g_app_list.currentRequestNumber();
760 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
764 HMI_ERROR("%s is not in transition state", id.c_str());
768 if (g_app_list.endDrawFullfilled(current_req))
770 // do task for endDraw
772 WMError ret = this->doEndDraw(current_req);
774 if(ret != WMError::SUCCESS)
776 //this->emit_error();
778 // Undo state of PolicyManager
779 this->pmw.undoState();
780 this->lc->undoUpdate();
782 this->emitScreenUpdated(current_req);
783 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
785 g_app_list.removeRequest(current_req);
787 this->processNextRequest();
791 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
796 void WindowManager::api_enddraw_for_remote(char const *appid, char const *drawing_name)
798 int ret = this->wmcon.sendRequest("endDraw", appid, drawing_name, "");
803 this->pmw.undoState();
804 this->lc->undoUpdate();
806 unsigned current_req = g_app_list.currentRequestNumber();
807 g_app_list.removeRequest(current_req);
808 this->processNextRequest();
813 this->api_enddraw(appid, drawing_name);
816 bool WindowManager::api_client_set_render_order(char const* appid, const vector<string>& render_order)
820 auto client = g_app_list.lookUpClient(id);
823 client->setRenderOrder(render_order);
828 string WindowManager::api_client_attach_service_surface
829 (const char* appid, const char* dest, const char* service_surface)
831 string uuid, s_dest = dest;
832 auto client = g_app_list.lookUpClient(s_dest);
835 HMI_ERROR("Failed to look up destination [%s]", dest);
838 uuid = client->attachTmpServiceSurface(appid, service_surface);
839 this->tmp_services.emplace_back(TmpService{appid, dest, service_surface, uuid});
843 result<json_object *> WindowManager::api_get_display_info()
845 Screen screen = this->lc->getScreenInfo();
847 json_object *object = json_object_new_object();
848 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(screen.width()));
849 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(screen.height()));
851 json_object_object_add(object, kKeyWidthMm, json_object_new_int(0));
852 json_object_object_add(object, kKeyHeightMm, json_object_new_int(0));
853 json_object_object_add(object, kKeyScale, json_object_new_double(this->lc->scale()));
855 return Ok<json_object *>(object);
858 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
862 // TODO: application requests by old role,
863 // so convert role old to new
864 const char *role = this->convertRoleOldToNew(drawing_name);
866 // Check drawing name, surface/layer id
867 auto const &surface_id = this->id_alloc.lookup(string(role));
870 return Err<json_object *>("Surface does not exist");
873 // Set area rectangle
874 rect area_info = this->area_info[*surface_id];
875 json_object *object = json_object_new_object();
876 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
877 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
878 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
879 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
881 return Ok<json_object *>(object);
884 result<json_object *> WindowManager::api_get_car_info(char const *label)
886 json_object *j_in = nullptr;
887 json_object *j_out = nullptr;
889 if (0 == strcmp("parking_brake_status", label))
891 // Get parking brake status
892 json_bool val = (this->crr_car_info.parking_brake_stt) ? TRUE : FALSE;
893 j_in = json_object_new_boolean(val);
895 else if (0 == strcmp("accelerator.pedal.position", label))
897 // Get accelerator pedal position
898 double val = this->crr_car_info.accel_pedal_pos;
899 j_in = json_object_new_double(val);
901 else if (0 == strcmp("car_state", label))
904 const char* val = (this->crr_car_info.running_stt) ? "run" : "stop";
905 j_in = json_object_new_string(val);
907 else if (0 == strcmp("lightstatus.brake", label)) {
908 // Get lightstatus brake status
909 json_bool val = (this->crr_car_info.lightstatus_brake_stt) ? TRUE : FALSE;
910 j_in = json_object_new_boolean(val);
914 return Err<json_object *>("Car info does not exist");
917 // Create output object
918 j_out = json_object_new_object();
919 json_object_object_add(j_out, "value", j_in);
921 return Ok<json_object *>(j_out);
924 void WindowManager::send_event(char const *evname)
926 HMI_DEBUG("%s: %s", __func__, evname);
928 int ret = afb_event_push(this->map_afb_event[evname], nullptr);
931 HMI_DEBUG("afb_event_push: %m");
935 void WindowManager::send_event(char const *evname, char const *label)
937 HMI_DEBUG("%s: %s(%s)", __func__, evname, label);
939 json_object *j = json_object_new_object();
940 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
942 int ret = afb_event_push(this->map_afb_event[evname], j);
945 HMI_DEBUG("afb_event_push failed: %m");
949 void WindowManager::send_event(char const *evname, char const *label, char const *area,
950 int x, int y, int w, int h)
952 HMI_DEBUG("%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
953 __func__, evname, label, area, x, y, w, h);
955 json_object *j_rect = json_object_new_object();
956 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
957 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
958 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
959 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
961 json_object *j = json_object_new_object();
962 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
963 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
964 json_object_object_add(j, kKeyDrawingRect, j_rect);
966 int ret = afb_event_push(this->map_afb_event[evname], j);
969 HMI_DEBUG("afb_event_push failed: %m");
973 string WindowManager::searchApp(unsigned pid, unsigned ppid, unsigned surface, json_object* resp)
975 // retrieve appid from pid from application manager
977 // check appid then add it to the client
978 HMI_INFO("Runners:%s", json_object_get_string(resp));
979 int size = json_object_array_length(resp);
980 HMI_INFO("pid %d, ppid %d, surface %d",pid, ppid, surface);
981 for(int i = 0; i < size; i++)
983 json_object *j = json_object_array_get_idx(resp, i);
984 int runid = jh::getIntFromJson(j, "runid");
985 const char* id = jh::getStringFromJson(j, "id");
986 HMI_DEBUG("Appid %s, runid %d", id, runid);
987 if(id && (runid == ppid))
990 s_id.erase(s_id.find('@'));
992 HMI_INFO("App found %s", appid.c_str());
998 HMI_WARNING("Failed to retrieve id");
1003 void WindowManager::storeSurface(const string& appid, unsigned ppid, unsigned surface)
1005 auto elem = std::find_if(this->tmp_services.begin(), this->tmp_services.end(),
1006 [&appid](TmpService& ts){
1007 return (ts.dest == appid );
1010 this->lc->setXDGSurfaceOriginSize(surface);
1011 if(elem != this->tmp_services.end())
1014 auto client = g_app_list.lookUpClient(elem->dest);
1015 if(client == nullptr)
1019 HMI_INFO("Attach surface %d (service %s) to app %s", surface, elem->service.c_str(), elem->dest.c_str());
1020 client->attachServiceSurface(elem->service, surface);
1025 auto client = g_app_list.lookUpClient(appid);
1026 if(client != nullptr)
1028 client->addSurface(surface);
1029 this->id_alloc.register_name_id(client->role(), surface);
1033 // Store tmp surface and appid for application
1034 // who requests setRole after creating shell surface
1035 this->tmp_surface2app.emplace(surface, TmpClient{appid, ppid});
1043 void WindowManager::surface_created(unsigned pid, unsigned surface_id)
1046 if(this->tmp_surface2app.count(surface_id) != 0)
1048 string appid = this->tmp_surface2app[surface_id].appid;
1049 auto client = g_app_list.lookUpClient(appid);
1050 if(client != nullptr)
1052 WMError ret = client->addSurface(surface_id);
1053 HMI_INFO("Add surface %d to \"%s\"", surface_id, appid.c_str());
1054 if(ret != WMError::SUCCESS)
1056 HMI_ERROR("Failed to add surface to client %s", client->appID().c_str());
1059 this->tmp_surface2app.erase(surface_id);
1063 HMI_NOTICE("Unknown surface %d", surface_id);
1065 std::ostringstream os;
1067 string path = "/proc/" + os.str() + "/stat";
1068 std::ifstream ifs(path.c_str());
1071 if(!ifs.fail() && std::getline(ifs, str))
1073 std::sscanf(str.data(), "%*d %*s %*c %d", &ppid);
1074 HMI_INFO("Retrieve ppid %d", ppid);
1078 HMI_ERROR("Failed to open /proc/%d/stat", pid);
1079 HMI_ERROR("File system may be different");
1082 struct AfbClosure* c = new struct AfbClosure(pid, ppid, surface_id);
1083 // search pid from surfaceID
1084 afb_service_call("afm-main", "runners", json_object_new_object(),
1085 [](void* closure, int stat, json_object* resp){
1086 HMI_DEBUG("check %s", json_object_get_string(resp));
1087 struct AfbClosure* c = static_cast<struct AfbClosure*>(closure);
1091 HMI_ERROR("Failed to call runners");
1096 json_object_object_get_ex(resp, "response", &j);
1097 string appid = g_context->searchApp(c->pid, c->ppid, c->surface, j);
1100 g_context->storeSurface(appid, c->ppid, c->surface);
1103 json_object_put(resp);
1109 void WindowManager::surface_removed(unsigned surface_id)
1111 HMI_DEBUG("Delete surface_id %u", surface_id);
1112 this->id_alloc.remove_id(surface_id);
1113 // this->layers.remove_surface(surface_id);
1114 g_app_list.removeSurface(surface_id);
1117 void WindowManager::removeClient(const string &appid)
1119 HMI_DEBUG("Remove clinet %s from list", appid.c_str());
1120 auto client = g_app_list.lookUpClient(appid);
1121 this->lc->terminateApp(client);
1122 g_app_list.removeClient(appid);
1125 void WindowManager::exceptionProcessForTransition()
1127 unsigned req_num = g_app_list.currentRequestNumber();
1128 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
1129 g_app_list.removeRequest(req_num);
1130 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
1131 this->processNextRequest();
1134 void WindowManager::analyzeReceivedEvent(const char *event, struct json_object *object)
1136 HMI_DEBUG("event:%s", event);
1138 // If receive low can signal
1139 if (strstr(event, "low-can"))
1141 // Analyze low can signal
1142 const char *signal_name = this->lcc.analyzeCanSignal(object);
1144 // Create task for car state and input it to PolicyManager
1145 Task task = this->convertCanSignalToCarStateTask(signal_name);
1146 if (Task::TASK_INVALID != task)
1148 this->inputCarStateTask(task);
1153 void WindowManager::timerHandler()
1155 unsigned req_num = g_app_list.currentRequestNumber();
1156 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
1157 g_app_list.reqDump();
1158 g_app_list.removeRequest(req_num);
1159 this->processNextRequest();
1162 void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
1164 WMError ret = WMError::UNKNOWN;
1165 // req_num is guaranteed by Window Manager
1166 unsigned req_num = g_app_list.currentRequestNumber();
1167 Task task = Task::TASK_INVALID;
1169 if (actions.empty())
1171 if (g_app_list.haveRequest())
1173 HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
1174 goto proc_remove_request;
1178 HMI_SEQ_DEBUG(req_num, "There is no request");
1183 // Check weather there is the no request task
1184 // [The no request task]
1185 // - TaskCarState::RESTRICTION_MODE_OFF
1186 // - TaskCarState::RESTRICTION_MODE_ON
1187 for (const auto &act : actions)
1189 if (TaskCarState::RESTRICTION_MODE_OFF == act.car_state)
1191 task = Task::TASK_RESTRICTION_MODE_OFF;
1194 else if (TaskCarState::RESTRICTION_MODE_ON == act.car_state)
1196 task = Task::TASK_RESTRICTION_MODE_ON;
1201 // If there is the request-less task, set request here
1202 if (Task::TASK_INVALID != task) {
1204 ret = this->setRequest(task, &req_num);
1206 if(ret != WMError::SUCCESS)
1212 for (auto &act : actions)
1217 auto const &surface_id = this->id_alloc.lookup(act.role.c_str());
1218 if(surface_id == nullopt)
1220 HMI_SEQ_DEBUG(req_num, "There is not surface id for role:%s", act.role.c_str());
1224 std::string appid = g_app_list.getAppID(*surface_id, &found);
1227 if (TaskVisible::INVISIBLE == act.visible)
1229 HMI_SEQ_DEBUG(req_num, "role:%s is killed, so do not set this action", act.role.c_str());
1234 HMI_SEQ_ERROR(req_num, "appid of role:%s which is visible is not found", act.role.c_str());
1235 ret = WMError::FAIL;
1239 auto client = g_app_list.lookUpClient(appid);
1240 act.req_num = req_num;
1241 act.client = client;
1243 // If Window Manager is master and this action is for slave,
1244 // change TaskVisible
1245 if (this->wmcon.isMasterMode())
1247 if (this->wmcon.isMasterArea(act.area.c_str()))
1249 HMI_DEBUG("Set TaskVisible::REQ_REMOTE_VISIBLE");
1250 act.visible = TaskVisible::REQ_REMOTE_VISIBLE;
1252 // TODO: Check whether role is tbtnavi to request remote invisible
1253 else if (("tbtnavi" == act.role) &&
1254 (TaskVisible::INVISIBLE == act.visible))
1256 HMI_DEBUG("Set TaskVisible::REQ_REMOTE_INVISIBLE");
1257 act.visible = TaskVisible::REQ_REMOTE_INVISIBLE;
1262 ret = g_app_list.setAction(req_num, act);
1263 if (ret != WMError::SUCCESS)
1265 HMI_SEQ_ERROR(req_num, "Setting action is failed");
1270 HMI_SEQ_DEBUG(req_num, "Start transition.");
1271 ret = this->startTransition(req_num);
1272 if (ret != WMError::SUCCESS)
1274 if (ret == WMError::NO_LAYOUT_CHANGE)
1276 goto proc_remove_request;
1280 HMI_SEQ_ERROR(req_num, "Transition state is failed");
1288 //this->emit_error()
1289 HMI_SEQ_ERROR(req_num, errorDescription(ret));
1290 this->pmw.undoState();
1292 proc_remove_request:
1293 g_app_list.removeRequest(req_num);
1294 this->processNextRequest();
1297 void WindowManager::processError(WMError error)
1299 unsigned req_num = g_app_list.currentRequestNumber();
1301 //this->emit_error()
1302 HMI_SEQ_ERROR(req_num, errorDescription(error));
1303 g_app_list.removeRequest(req_num);
1304 this->processNextRequest();
1307 void WindowManager::processForRemoteRequest(json_object *data)
1309 const char *req = jh::getStringFromJson(data, "req");
1310 const char *appid = jh::getStringFromJson(data, "appid");
1311 const char *drawing_name = jh::getStringFromJson(data, "drawing_name");
1312 const char *drawing_area = jh::getStringFromJson(data, "drawing_area");
1314 if (!req || !drawing_name)
1316 HMI_ERROR("Parse Error!!");
1320 if (this->wmcon.isMasterMode())
1324 HMI_ERROR("Parse Error!!");
1328 auto reply = [](const char *errmsg) {
1329 if (errmsg != nullptr)
1336 if ("activateWindow" == std::string(req))
1340 HMI_ERROR("Parse Error!!");
1344 this->api_activate_surface_for_slave(
1345 appid, drawing_name, drawing_area, reply);
1347 else if ("deactivateWindow" == std::string(req))
1349 this->api_deactivate_surface_for_slave(
1350 appid, drawing_name, reply);
1352 else if ("endDraw" == std::string(req))
1354 this->api_enddraw(appid, drawing_name);
1359 if ("syncDraw" == std::string(req))
1363 if (!appid || !drawing_area)
1365 HMI_ERROR("Parse Error!!");
1369 unsigned req_num = g_app_list.currentRequestNumber();
1370 auto client = g_app_list.lookUpClient(appid);
1372 // TODO: application requests by old role,
1373 // so convert role old to new
1374 const char *c_role = this->convertRoleOldToNew(drawing_name);
1377 bool end_draw_finished = false;
1383 string(drawing_area),
1384 TaskVisible::REMOTE_VISIBLE,
1386 TaskCarState::NO_TASK
1390 WMError ret = g_app_list.setAction(req_num, act);
1391 if (ret != WMError::SUCCESS)
1393 HMI_SEQ_ERROR(req_num, "Setting action is failed");
1397 this->emit_syncdraw(string(drawing_name), string(drawing_area));
1398 this->wmcon.startSyncDrawForRemote(appid);
1401 else if ("activated" == std::string(req))
1403 this->emit_visible(drawing_name);
1404 this->emit_activated(drawing_name);
1406 else if ("deactivated" == std::string(req))
1410 if (!appid || !drawing_area)
1412 HMI_ERROR("Parse Error!!");
1416 unsigned req_num = g_app_list.currentRequestNumber();
1417 auto client = g_app_list.lookUpClient(appid);
1419 // TODO: application requests by old role,
1420 // so convert role old to new
1421 const char *c_role = this->convertRoleOldToNew(drawing_name);
1424 bool end_draw_finished = true;
1431 TaskVisible::REMOTE_INVISIBLE,
1433 TaskCarState::NO_TASK
1436 this->lc->visibilityChange(act);
1437 this->lc->renderLayers();
1438 this->lc->renderLayersRemote();
1440 this->emit_invisible(drawing_name);
1441 this->emit_deactivated(drawing_name);
1442 this->emitScreenUpdated(req_num);
1444 g_app_list.removeRequest(req_num);
1445 this->processNextRequest();
1447 else if ("flushDraw" == std::string(req))
1449 this->emit_flushdraw(drawing_name);
1455 ******* Private Functions *******
1458 void WindowManager::emit_activated(char const *label)
1460 this->send_event(kListEventName[Event_Active], label);
1463 void WindowManager::emit_deactivated(char const *label)
1465 this->send_event(kListEventName[Event_Inactive], label);
1468 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1470 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1473 void WindowManager::emit_syncdraw(const string &role, const string &area)
1475 rect rect = this->lc->getAreaSize(area);
1476 this->send_event(kListEventName[Event_SyncDraw],
1477 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
1480 void WindowManager::emit_flushdraw(char const *label)
1482 this->send_event(kListEventName[Event_FlushDraw], label);
1485 void WindowManager::emit_visible(char const *label, bool is_visible)
1487 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1490 void WindowManager::emit_invisible(char const *label)
1492 return emit_visible(label, false);
1495 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
1497 void WindowManager::emitHeadlampOff()
1499 // Send HeadlampOff event for all application
1500 this->send_event(kListEventName[Event_HeadlampOff]);
1503 void WindowManager::emitHeadlampOn()
1505 // Send HeadlampOn event for all application
1506 this->send_event(kListEventName[Event_HeadlampOn]);
1509 void WindowManager::emitParkingBrakeOff()
1511 // Send ParkingBrakeOff event for all application
1512 this->send_event(kListEventName[Event_ParkingBrakeOff]);
1515 void WindowManager::emitParkingBrakeOn()
1517 // Send ParkingBrakeOn event for all application
1518 this->send_event(kListEventName[Event_ParkingBrakeOn]);
1521 void WindowManager::emitLightstatusBrakeOff()
1523 // Send LightstatusBrakeOff event for all application
1524 this->send_event(kListEventName[Event_LightstatusBrakeOff]);
1527 void WindowManager::emitLightstatusBrakeOn()
1529 // Send LightstatusBrakeOn event for all application
1530 this->send_event(kListEventName[Event_LightstatusBrakeOn]);
1533 void WindowManager::emitCarStop()
1535 // Send CarStop event for all application
1536 this->send_event(kListEventName[Event_CarStop]);
1539 void WindowManager::emitCarRun()
1541 // Send CarRun event for all application
1542 this->send_event(kListEventName[Event_CarRun]);
1545 WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
1546 Task task, unsigned* req_num)
1548 if (!g_app_list.contains(appid))
1550 return WMError::NOT_REGISTERED;
1553 auto client = g_app_list.lookUpClient(appid);
1558 unsigned current = g_app_list.currentRequestNumber();
1559 unsigned requested_num = g_app_list.getRequestNumber(appid);
1560 if (requested_num != 0)
1562 HMI_SEQ_INFO(requested_num,
1563 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
1564 return REQ_REJECTED;
1567 WMRequest req = WMRequest(appid, role, area, task);
1568 unsigned new_req = g_app_list.addRequest(req);
1570 g_app_list.reqDump();
1572 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1574 return WMError::SUCCESS;
1577 WMError WindowManager::setRequest(Task task, unsigned* req_num)
1582 unsigned current = g_app_list.currentRequestNumber();
1584 WMRequest req = WMRequest(task);
1585 unsigned new_req = g_app_list.addRequest(req);
1587 g_app_list.reqDump();
1589 HMI_SEQ_DEBUG(current, "start sequence for task:%d", task);
1591 return WMError::SUCCESS;
1594 WMError WindowManager::setRequestForSlave(const string& appid, const string &role, const string &area,
1595 Task task, unsigned* req_num)
1600 unsigned current = g_app_list.currentRequestNumber();
1601 unsigned requested_num = g_app_list.getRequestNumber(appid);
1602 if (requested_num != 0)
1604 HMI_SEQ_INFO(requested_num,
1605 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
1606 return REQ_REJECTED;
1609 WMRequest req = WMRequest(appid, role, area, task);
1610 unsigned new_req = g_app_list.addRequest(req);
1612 g_app_list.reqDump();
1614 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1616 return WMError::SUCCESS;
1619 WMError WindowManager::checkPolicy(unsigned req_num)
1624 // get current trigger
1626 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1627 auto trigger = g_app_list.getRequest(req_num, &found);
1630 ret = WMError::NO_ENTRY;
1633 string req_area = trigger.area;
1635 if (trigger.task == Task::TASK_ALLOCATE)
1637 const char *msg = this->check_surface_exist(trigger.role.c_str());
1641 HMI_SEQ_ERROR(req_num, msg);
1646 // Input event data to PolicyManager
1647 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1649 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1653 // Execute state transition of PolicyManager
1654 if (0 > this->pmw.executeStateTransition())
1656 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1660 ret = WMError::SUCCESS;
1662 g_app_list.reqDump();
1667 WMError WindowManager::checkPolicyForSlave(unsigned req_num)
1672 // get current trigger
1674 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1675 auto trigger = g_app_list.getRequest(req_num, &found);
1678 ret = WMError::NO_ENTRY;
1681 string req_area = trigger.area;
1683 // Input event data to PolicyManager
1684 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1686 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1690 // Execute state transition of PolicyManager
1691 if (0 > this->pmw.executeStateTransition())
1693 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1697 ret = WMError::SUCCESS;
1699 g_app_list.reqDump();
1704 WMError WindowManager::startTransition(unsigned req_num)
1706 bool sync_draw_happen = false;
1708 WMError ret = WMError::SUCCESS;
1709 auto actions = g_app_list.getActions(req_num, &found);
1712 ret = WMError::NO_ENTRY;
1713 HMI_SEQ_ERROR(req_num,
1714 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1718 g_app_list.reqDump();
1719 for (const auto &action : actions)
1721 // TODO: application requests by old role,
1722 // so convert role new to old for emitting event
1723 string old_role = this->rolenew2old[action.role];
1725 if (action.visible == TaskVisible::VISIBLE)
1727 sync_draw_happen = true;
1729 this->emit_syncdraw(old_role, action.area);
1730 /* TODO: emit event for app not subscriber
1731 if(g_app_list.contains(y.appid))
1732 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1734 else if(action.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1736 // If this action is for slave, send to slave
1737 this->wmcon.sendRequest("syncDraw", action.client->appID().c_str(),
1738 old_role.c_str(), action.area.c_str());
1740 else if (action.car_state != TaskCarState::NO_TASK)
1742 this->transitionCarState(action.car_state);
1746 if (sync_draw_happen)
1752 // deactivate only, no syncDraw
1753 // Make it deactivate here
1754 for (const auto &x : actions)
1756 this->lc->visibilityChange(x);
1757 string old_role = this->rolenew2old[x.role];
1759 if (x.visible == TaskVisible::INVISIBLE)
1761 emit_deactivated(old_role.c_str());
1763 else if (x.visible == TaskVisible::REQ_REMOTE_INVISIBLE)
1765 // If this action is for slave, send to slave
1766 int i_ret = this->wmcon.sendRequest("deactivated", x.client->appID().c_str(),
1767 old_role.c_str(), "");
1770 ret = WMError::FAIL;
1774 /* if (g_app_list.contains(x.appid))
1776 auto client = g_app_list.lookUpClient(x.appid);
1777 //this->deactivate(client->surfaceID(x.role));
1780 this->lc->renderLayers();
1781 this->lc->renderLayersRemote();
1782 ret = WMError::NO_LAYOUT_CHANGE;
1787 void WindowManager::transitionCarState(TaskCarState task)
1789 if (TaskCarState::PARKING_BRAKE_OFF == task)
1791 this->crr_car_info.parking_brake_stt = false;
1792 this->emitParkingBrakeOff();
1794 else if (TaskCarState::PARKING_BRAKE_ON == task)
1796 this->crr_car_info.parking_brake_stt = true;
1797 this->emitParkingBrakeOn();
1799 else if (TaskCarState::ACCEL_PEDAL_OFF == task)
1801 this->crr_car_info.accel_pedal_stt = false;
1803 else if (TaskCarState::ACCEL_PEDAL_ON == task)
1805 this->crr_car_info.accel_pedal_stt = true;
1807 else if (TaskCarState::HEDLAMP_OFF == task)
1809 this->crr_car_info.headlamp_stt = false;
1810 this->emitHeadlampOff();
1812 else if (TaskCarState::HEDLAMP_ON == task)
1814 this->crr_car_info.headlamp_stt = true;
1815 this->emitHeadlampOn();
1817 else if (TaskCarState::LIGHTSTATUS_BRAKE_OFF == task)
1819 this->crr_car_info.lightstatus_brake_stt = false;
1820 this->emitLightstatusBrakeOff();
1822 else if (TaskCarState::LIGHTSTATUS_BRAKE_ON == task)
1824 this->crr_car_info.lightstatus_brake_stt = true;
1825 this->emitLightstatusBrakeOn();
1827 else if (TaskCarState::CAR_STOP == task)
1829 this->crr_car_info.running_stt = false;
1830 this->emitCarStop();
1832 else if (TaskCarState::CAR_RUN == task)
1834 this->crr_car_info.running_stt = true;
1839 WMError WindowManager::doEndDraw(unsigned req_num)
1843 auto actions = g_app_list.getActions(req_num, &found);
1844 WMError ret = WMError::SUCCESS;
1847 ret = WMError::NO_ENTRY;
1851 HMI_SEQ_INFO(req_num, "do endDraw");
1853 // layout change and make it visible
1854 for (const auto &act : actions)
1856 if(act.visible != TaskVisible::NO_CHANGE)
1859 ret = this->lc->layoutChange(act);
1860 if(ret != WMError::SUCCESS)
1862 HMI_SEQ_WARNING(req_num,
1863 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1866 ret = this->lc->visibilityChange(act);
1868 // Emit active/deactive event
1869 string old_role = this->rolenew2old[act.role];
1870 if(act.visible == TaskVisible::VISIBLE)
1872 emit_visible(old_role.c_str());
1873 emit_activated(old_role.c_str());
1875 else if(act.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1877 // If this action is for slave, send to slave
1878 int i_ret = this->wmcon.sendRequest("activated", "", old_role.c_str(), "");
1881 ret = WMError::FAIL;
1884 else if(act.visible == TaskVisible::REQ_REMOTE_INVISIBLE)
1886 // If this action is for slave, send to slave
1887 int i_ret = this->wmcon.sendRequest("deactivated", "", old_role.c_str(), "");
1890 ret = WMError::FAIL;
1893 else if((act.visible == TaskVisible::REMOTE_VISIBLE) ||
1894 (act.visible == TaskVisible::REMOTE_INVISIBLE))
1896 // nop because emit active/deactive by event from remote
1900 emit_invisible(old_role.c_str());
1901 emit_deactivated(old_role.c_str());
1904 if (ret != WMError::SUCCESS)
1906 HMI_SEQ_WARNING(req_num,
1907 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1910 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1913 this->lc->renderLayers();
1914 this->lc->renderLayersRemote();
1916 HMI_SEQ_INFO(req_num, "emit flushDraw");
1918 for(const auto &act_flush : actions)
1920 // TODO: application requests by old role,
1921 // so convert role new to old for emitting event
1922 string old_role = this->rolenew2old[act_flush.role];
1924 if(act_flush.visible == TaskVisible::VISIBLE)
1926 this->emit_flushdraw(old_role.c_str());
1928 else if(act_flush.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1930 // If this action is for slave, send to slave
1931 this->wmcon.sendRequest("flushDraw", "", old_role.c_str(), "");
1938 void WindowManager::emitScreenUpdated(unsigned req_num)
1941 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1943 auto actions = g_app_list.getActions(req_num, &found);
1946 // create json object
1947 json_object *j = json_object_new_object();
1948 json_object *jarray = json_object_new_array();
1950 for(const auto& action: actions)
1952 if((action.visible == TaskVisible::VISIBLE) ||
1953 (action.visible == TaskVisible::REMOTE_VISIBLE))
1955 json_object_array_add(jarray, json_object_new_string(action.client->appID().c_str()));
1958 json_object_object_add(j, kKeyIds, jarray);
1959 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1961 int ret = afb_event_push(
1962 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1965 HMI_DEBUG("afb_event_push failed: %m");
1969 void WindowManager::setTimer()
1972 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
1973 HMI_ERROR("Could't set time (clock_gettime() returns with error");
1977 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1978 if (g_timer_ev_src == nullptr)
1980 // firsttime set into sd_event
1981 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1982 CLOCK_BOOTTIME, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL, 1, processTimerHandler, this);
1985 HMI_ERROR("Could't set timer");
1990 // update timer limitation after second time
1991 sd_event_source_set_time(g_timer_ev_src, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL);
1992 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1996 void WindowManager::stopTimer()
1998 unsigned req_num = g_app_list.currentRequestNumber();
1999 HMI_SEQ_DEBUG(req_num, "Timer stop");
2000 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
2003 HMI_SEQ_ERROR(req_num, "Timer stop failed");
2007 void WindowManager::processNextRequest()
2010 g_app_list.reqDump();
2011 unsigned req_num = g_app_list.currentRequestNumber();
2012 if (g_app_list.haveRequest())
2014 HMI_SEQ_DEBUG(req_num, "Process next request");
2015 WMError rc = checkPolicy(req_num);
2016 if (rc != WMError::SUCCESS)
2018 HMI_SEQ_ERROR(req_num, errorDescription(rc));
2023 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
2027 const char* WindowManager::convertRoleOldToNew(char const *old_role)
2029 const char *new_role = nullptr;
2031 for (auto const &on : this->roleold2new)
2033 std::regex regex = std::regex(on.first);
2034 if (std::regex_match(old_role, regex))
2036 // role is old. So convert to new.
2037 new_role = on.second.c_str();
2042 if (nullptr == new_role)
2044 // role is new or fallback.
2045 new_role = old_role;
2048 HMI_DEBUG("old:%s -> new:%s", old_role, new_role);
2053 int WindowManager::loadOldRolesConfigFile()
2055 // Get afm application installed dir
2056 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
2057 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
2060 if (!afm_app_install_dir)
2062 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
2066 file_name = string(afm_app_install_dir) + string(kPathOldRolesConfigFile);
2069 // Load old_rolea config file
2070 json_object* json_obj;
2071 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
2074 HMI_ERROR("Could not open %s, so use default old_roles information", kPathOldRolesConfigFile);
2075 json_obj = json_tokener_parse(kDefaultOldRolesConfig);
2077 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
2080 json_object* json_cfg;
2081 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
2083 HMI_ERROR("Parse Error!!");
2087 int len = json_object_array_length(json_cfg);
2088 HMI_DEBUG("json_cfg len:%d", len);
2089 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
2091 for (int i=0; i<len; i++)
2093 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
2095 const char* old_role = jh::getStringFromJson(json_tmp, "name");
2096 if (nullptr == old_role)
2098 HMI_ERROR("Parse Error!!");
2102 const char* new_role = jh::getStringFromJson(json_tmp, "new");
2103 if (nullptr == new_role)
2105 HMI_ERROR("Parse Error!!");
2109 this->roleold2new[old_role] = string(new_role);
2113 for(auto itr = this->roleold2new.begin();
2114 itr != this->roleold2new.end(); ++itr)
2116 HMI_DEBUG(">>> role old:%s new:%s",
2117 itr->first.c_str(), itr->second.c_str());
2120 // Release json_object
2121 json_object_put(json_obj);
2126 Task WindowManager::convertCanSignalToCarStateTask(const char *signal_name)
2128 wm::LowCanClient *lcc = &(this->lcc);
2129 Task task = Task::TASK_INVALID;
2131 // If car info is updated, set car state change event
2132 if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoParkingBrake]))
2134 HMI_DEBUG("Parking Brake state is changed");
2136 if (lcc->getCurrentParkingBrakeState())
2138 task = wm::Task::TASK_PARKING_BRAKE_ON;
2142 task = wm::Task::TASK_PARKING_BRAKE_OFF;
2145 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoAccelPedalPos]))
2147 // Update accel pedal position
2148 this->crr_car_info.accel_pedal_pos = lcc->getCurrentAccelPedalPosition();
2150 if (lcc->isChangedAccelPedalState())
2152 HMI_DEBUG("Accelerator Pedal state is changed");
2154 if (lcc->getCurrentAccelPedalState())
2156 task = wm::Task::TASK_ACCEL_PEDAL_ON;
2160 task = wm::Task::TASK_ACCEL_PEDAL_OFF;
2164 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoHeadlame]))
2166 HMI_DEBUG("Headlamp state is changed");
2168 if (lcc->getCurrentHeadlampState())
2170 task = wm::Task::TASK_HEDLAMP_ON;
2174 task = wm::Task::TASK_HEDLAMP_OFF;
2177 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoLightstatusBrake]))
2179 HMI_DEBUG("Lightstatus Brake state is changed");
2181 if (lcc->getCurrentLightstatusBrakeState())
2183 task = wm::Task::TASK_LIGHTSTATUS_BRAKE_ON;
2187 task = wm::Task::TASK_LIGHTSTATUS_BRAKE_OFF;
2193 void WindowManager::inputCarStateTask(Task task)
2195 unsigned req_num = 0;
2196 WMError ret = WMError::UNKNOWN;
2198 ret = this->setRequest(task, &req_num);
2200 if(ret != WMError::SUCCESS)
2202 HMI_ERROR(errorDescription(ret));
2206 if (req_num != g_app_list.currentRequestNumber())
2208 // Add request, then invoked after the previous task is finished
2209 HMI_SEQ_DEBUG(req_num, "request is accepted");
2216 ret = this->checkPolicy(req_num);
2218 if (ret != WMError::SUCCESS)
2220 //this->emit_error()
2221 HMI_SEQ_ERROR(req_num, errorDescription(ret));
2222 g_app_list.removeRequest(req_num);
2223 this->processNextRequest();
2227 const char *WindowManager::check_surface_exist(const char *drawing_name)
2229 auto const &surface_id = this->id_alloc.lookup(string(drawing_name));
2232 return "Surface does not exist";
2235 /* if (!this->controller->surface_exists(*surface_id))
2237 return "Surface does not exist in controller!";
2240 /* auto layer_id = this->layers.get_layer_id(*surface_id);
2244 return "Surface is not on any layer!";
2247 HMI_DEBUG("surface %d is detected", *surface_id);
2251 const char* WindowManager::kDefaultOldRolesConfig = "{ \
2254 \"name\": \"HomeScreen\", \
2255 \"new\": \"homescreen\" \
2258 \"name\": \"Music\", \
2259 \"new\": \"music\" \
2262 \"name\": \"MediaPlayer\", \
2263 \"new\": \"music\" \
2266 \"name\": \"Video\", \
2267 \"new\": \"video\" \
2270 \"name\": \"VideoPlayer\", \
2271 \"new\": \"video\" \
2274 \"name\": \"WebBrowser\", \
2275 \"new\": \"browser\" \
2278 \"name\": \"Radio\", \
2279 \"new\": \"radio\" \
2282 \"name\": \"Phone\", \
2283 \"new\": \"phone\" \
2286 \"name\": \"Navigation\", \
2290 \"name\": \"HVAC\", \
2294 \"name\": \"Settings\", \
2295 \"new\": \"settings\" \
2298 \"name\": \"Dashboard\", \
2299 \"new\": \"dashboard\" \
2302 \"name\": \"POI\", \
2306 \"name\": \"Mixer\", \
2307 \"new\": \"mixer\" \
2310 \"name\": \"Restriction\", \
2311 \"new\": \"restriction\" \
2314 \"name\": \"^OnScreen.*\", \
2315 \"new\": \"on_screen\" \