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};
231 HMI_NOTICE("WORK AROUND: add surface on request surface");
232 auto client = g_app_list.lookUpClient(s_appid);
233 client->addSurface(id);
236 // Set role map of (new, old)
237 this->rolenew2old[role] = string(drawing_name);
242 // Check currently registered drawing names if it is already there.
243 return Err<int>("Surface already present");
246 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
249 unsigned sid = std::stol(ivi_id);
251 HMI_DEBUG("This API(requestSurfaceXDG) is for XDG Application using runXDG");
253 * IVI-shell doesn't send surface_size event via ivi-wm protocol
254 * if the application is using XDG surface.
255 * So WM has to set surface size with original size here
257 WMError ret = this->lc->setXDGSurfaceOriginSize(sid);
260 HMI_ERROR("%s", errorDescription(ret));
261 HMI_WARNING("The main user of this API is runXDG");
265 // TODO: application requests by old role,
266 // so convert role old to new
267 const char *role = this->convertRoleOldToNew(drawing_name);
268 string s_role = role;
269 string s_appid = appid;
272 if(!g_app_list.contains(s_appid))
274 // auto lid = this->layers.get_layer_id(string(role));
275 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
279 * register drawing_name as fallback and make it displayed.
281 l_id = this->lc->getNewLayerID("fallback", &l_name);
282 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
285 return "Designated role does not match any role, fallback is disabled";
289 // TODO: remote layer size is fixed value
290 if ("Remote" == l_name)
292 this->lc->createNewRemoteLayer(l_id);
296 this->lc->createNewLayer(l_id);
299 // add client into the db
300 g_app_list.addClient(s_appid, l_id, s_role);
303 auto rname = this->id_alloc.lookup(s_role);
306 return "Surface already present";
309 // register pair drawing_name and ivi_id
310 this->id_alloc.register_name_id(role, sid);
312 auto client = g_app_list.lookUpClient(s_appid);
313 client->addSurface(sid);
315 // Set role map of (new, old)
316 this->rolenew2old[role] = string(drawing_name);
321 bool WindowManager::api_set_role(char const *appid, char const *drawing_name)
325 // TODO: application requests by old role,
326 // so convert role old to new
327 const char *role = this->convertRoleOldToNew(drawing_name);
328 string s_role = role;
329 string s_appid = appid;
333 if(!g_app_list.contains(s_appid))
335 // auto lid = this->layers.get_layer_id(string(role));
336 unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
340 * register drawing_name as fallback and make it displayed.
342 l_id = this->lc->getNewLayerID("fallback", &l_name);
343 HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
346 HMI_ERROR("Designated role does not match any role, fallback is disabled");
351 // TODO: remote layer size is fixed value
352 if ("Remote" == l_name)
354 this->lc->createNewRemoteLayer(l_id);
358 this->lc->createNewLayer(l_id);
361 // add client into the db
362 g_app_list.addClient(s_appid, l_id, s_role);
363 // Set role map of (new, old)
364 this->rolenew2old[role] = string(drawing_name);
367 // for(auto itr = this->tmp_surface2app.begin();
368 // itr != this->tmp_surface2app.end() ; ++itr)
370 for(auto& x : this->tmp_surface2app)
372 if(x.second.appid == s_appid)
374 unsigned surface = x.first;
375 auto client = g_app_list.lookUpClient(s_appid);
376 client->addSurface(surface);
377 this->tmp_surface2app.erase(surface);
378 this->id_alloc.register_name_id(s_role, surface);
384 // search floating surfaceID from pid if pid is designated.
385 wm_err = g_app_list.popFloatingSurface(pid, &surface);
388 // get floating surface with appid. If WM queries appid from pid,
389 // WM can bind surface and role with appid(not implemented yet)
390 //wm_err = g_app_list.popFloatingSurface(id);
392 if(wm_err != WMError::SUCCESS){
393 HMI_ERROR("No floating surface for app: %s", id.c_str());
394 g_app_list.addFloatingClient(id, lid, role);
395 HMI_NOTICE("%s : Waiting for surface creation", id.c_str());
400 if (g_app_list.contains(id))
402 HMI_INFO("Add role: %s with surface: %d. Client %s has multi surfaces.",
403 role.c_str(), surface, id.c_str());
404 auto client = g_app_list.lookUpClient(id);
405 client->appendRole(role);
408 HMI_INFO("Create new client: %s, surface: %d into layer: %d with role: %s",
409 id.c_str(), surface, lid, role.c_str());
410 g_app_list.addClient(id, lid, role);
413 // register pair drawing_name and ivi_id
418 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
419 char const *drawing_area, const reply_func &reply)
421 // TODO: application requests by old role,
422 // so convert role old to new
423 const char *c_role = this->convertRoleOldToNew(drawing_name);
426 string role = c_role;
427 string area = drawing_area;
429 if(!g_app_list.contains(id))
431 reply("app doesn't request 'requestSurface' or 'setRole' yet");
434 auto client = g_app_list.lookUpClient(id);
436 // unsigned srfc = client->surfaceID(role);
437 // unsigned layer = client->layerID();
439 // g_app_list.removeFloatingSurface(client->surfaceID());
440 // g_app_list.removeFloatingSurface(client);
442 Task task = Task::TASK_ALLOCATE;
443 unsigned req_num = 0;
444 WMError ret = WMError::UNKNOWN;
446 ret = this->setRequest(id, role, area, task, &req_num);
448 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
449 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
451 if(ret != WMError::SUCCESS)
453 HMI_ERROR(errorDescription(ret));
454 reply("Failed to set request");
459 if (req_num != g_app_list.currentRequestNumber())
461 // Add request, then invoked after the previous task is finished
462 HMI_SEQ_DEBUG(req_num, "request is accepted");
469 ret = this->checkPolicy(req_num);
471 if (ret != WMError::SUCCESS)
474 HMI_SEQ_ERROR(req_num, errorDescription(ret));
475 g_app_list.removeRequest(req_num);
476 this->processNextRequest();
480 void WindowManager::api_activate_surface_for_slave(
481 char const *appid, char const *drawing_name,
482 char const *drawing_area, const reply_func &reply)
484 // TODO: application requests by old role,
485 // so convert role old to new
486 const char *c_role = this->convertRoleOldToNew(drawing_name);
489 string role = c_role;
490 string area = drawing_area;
492 if(!g_app_list.contains(id))
494 // Request surface of app in slave to register app information
495 this->api_request_surface(appid, drawing_name);
497 // Set role of app in slave to register app information
498 this->api_set_role(appid, drawing_name);
500 auto client = g_app_list.lookUpClient(id);
502 // unsigned srfc = client->surfaceID(role);
503 // unsigned layer = client->layerID();
505 // g_app_list.removeFloatingSurface(client->surfaceID());
506 // g_app_list.removeFloatingSurface(client);
508 Task task = Task::TASK_ALLOCATE;
509 unsigned req_num = 0;
510 WMError ret = WMError::UNKNOWN;
512 ret = this->setRequestForSlave(id, role, area, task, &req_num);
514 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
515 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
517 if(ret != WMError::SUCCESS)
519 HMI_ERROR(errorDescription(ret));
520 reply("Failed to set request");
525 if (req_num != g_app_list.currentRequestNumber())
527 // Add request, then invoked after the previous task is finished
528 HMI_SEQ_DEBUG(req_num, "request is accepted");
535 ret = this->checkPolicyForSlave(req_num);
537 if (ret != WMError::SUCCESS)
540 HMI_SEQ_ERROR(req_num, errorDescription(ret));
541 g_app_list.removeRequest(req_num);
542 this->processNextRequest();
546 void WindowManager::api_activate_surface_to_master(
547 char const *appid, char const *drawing_name,
548 char const *drawing_area, const reply_func &reply)
550 // TODO: application requests by old role,
551 // so convert role old to new
552 const char *c_role = this->convertRoleOldToNew(drawing_name);
555 string role = c_role;
556 string area = drawing_area;
558 if(!g_app_list.contains(id))
560 reply("app doesn't request 'requestSurface' or 'setRole' yet");
563 auto client = g_app_list.lookUpClient(id);
565 // unsigned srfc = client->surfaceID(role);
566 // unsigned layer = client->layerID();
568 // g_app_list.removeFloatingSurface(client->surfaceID());
569 // g_app_list.removeFloatingSurface(client);
571 Task task = Task::TASK_ALLOCATE;
572 unsigned req_num = 0;
573 WMError ret = WMError::UNKNOWN;
575 ret = this->setRequest(id, role, area, task, &req_num);
577 //vector<WMLayerState> current_states = this->lc->getCurrentStates();
578 // ret = this->setRequest(id, role, area, task, current_states, &req_num);
580 if(ret != WMError::SUCCESS)
582 HMI_ERROR(errorDescription(ret));
583 reply("Failed to set request");
588 if (req_num != g_app_list.currentRequestNumber())
590 // Add request, then invoked after the previous task is finished
591 HMI_SEQ_DEBUG(req_num, "request is accepted");
598 int i_ret = this->wmcon.sendRequest("activateWindow", appid,
599 drawing_name, drawing_area);
603 HMI_SEQ_ERROR(req_num, errorDescription(ret));
604 g_app_list.removeRequest(req_num);
605 this->processNextRequest();
611 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
612 const reply_func &reply)
614 // TODO: application requests by old role,
615 // so convert role old to new
616 const char *c_role = this->convertRoleOldToNew(drawing_name);
622 string role = c_role;
623 string area = ""; //drawing_area;
624 Task task = Task::TASK_RELEASE;
625 unsigned req_num = 0;
626 WMError ret = WMError::UNKNOWN;
628 ret = this->setRequest(id, role, area, task, &req_num);
630 if (ret != WMError::SUCCESS)
632 HMI_ERROR(errorDescription(ret));
633 reply("Failed to set request");
638 if (req_num != g_app_list.currentRequestNumber())
640 // Add request, then invoked after the previous task is finished
641 HMI_SEQ_DEBUG(req_num, "request is accepted");
648 ret = this->checkPolicy(req_num);
650 if (ret != WMError::SUCCESS)
653 HMI_SEQ_ERROR(req_num, errorDescription(ret));
654 g_app_list.removeRequest(req_num);
655 this->processNextRequest();
659 void WindowManager::api_deactivate_surface_for_slave(char const *appid, char const *drawing_name,
660 const reply_func &reply)
662 // TODO: application requests by old role,
663 // so convert role old to new
664 const char *c_role = this->convertRoleOldToNew(drawing_name);
670 string role = c_role;
671 string area = "";//drawing_area;
672 Task task = Task::TASK_RELEASE;
673 unsigned req_num = 0;
674 WMError ret = WMError::UNKNOWN;
676 ret = this->setRequest(id, role, area, task, &req_num);
678 if (ret != WMError::SUCCESS)
680 HMI_ERROR(errorDescription(ret));
681 reply("Failed to set request");
686 if (req_num != g_app_list.currentRequestNumber())
688 // Add request, then invoked after the previous task is finished
689 HMI_SEQ_DEBUG(req_num, "request is accepted");
696 ret = this->checkPolicyForSlave(req_num);
698 if (ret != WMError::SUCCESS)
701 HMI_SEQ_ERROR(req_num, errorDescription(ret));
702 g_app_list.removeRequest(req_num);
703 this->processNextRequest();
707 void WindowManager::api_deactivate_surface_to_master(char const *appid, char const *drawing_name,
708 const reply_func &reply)
710 // TODO: application requests by old role,
711 // so convert role old to new
712 const char *c_role = this->convertRoleOldToNew(drawing_name);
718 string role = c_role;
719 string area = "";//drawing_area;
720 Task task = Task::TASK_RELEASE;
721 unsigned req_num = 0;
722 WMError ret = WMError::UNKNOWN;
724 ret = this->setRequest(id, role, area, task, &req_num);
726 if (ret != WMError::SUCCESS)
728 HMI_ERROR(errorDescription(ret));
729 reply("Failed to set request");
734 if (req_num != g_app_list.currentRequestNumber())
736 // Add request, then invoked after the previous task is finished
737 HMI_SEQ_DEBUG(req_num, "request is accepted");
744 int i_ret = this->wmcon.sendRequest("deactivateWindow", appid,
749 HMI_SEQ_ERROR(req_num, errorDescription(ret));
750 g_app_list.removeRequest(req_num);
751 this->processNextRequest();
757 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
759 // TODO: application requests by old role,
760 // so convert role old to new
761 const char *c_role = this->convertRoleOldToNew(drawing_name);
764 string role = c_role;
765 unsigned current_req = g_app_list.currentRequestNumber();
766 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
770 HMI_ERROR("%s is not in transition state", id.c_str());
774 if (g_app_list.endDrawFullfilled(current_req))
776 // do task for endDraw
778 WMError ret = this->doEndDraw(current_req);
780 if(ret != WMError::SUCCESS)
782 //this->emit_error();
784 // Undo state of PolicyManager
785 this->pmw.undoState();
786 this->lc->undoUpdate();
788 this->emitScreenUpdated(current_req);
789 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
791 g_app_list.removeRequest(current_req);
793 this->processNextRequest();
797 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
802 void WindowManager::api_enddraw_for_remote(char const *appid, char const *drawing_name)
804 int ret = this->wmcon.sendRequest("endDraw", appid, drawing_name, "");
809 this->pmw.undoState();
810 this->lc->undoUpdate();
812 unsigned current_req = g_app_list.currentRequestNumber();
813 g_app_list.removeRequest(current_req);
814 this->processNextRequest();
819 this->api_enddraw(appid, drawing_name);
822 bool WindowManager::api_client_set_render_order(char const* appid, const vector<string>& render_order)
826 auto client = g_app_list.lookUpClient(id);
829 client->setRenderOrder(render_order);
834 string WindowManager::api_client_attach_service_surface
835 (const char* appid, const char* dest, const char* service_surface)
837 string uuid, s_dest = dest;
838 auto client = g_app_list.lookUpClient(s_dest);
841 HMI_ERROR("Failed to look up destination [%s]", dest);
844 uuid = client->attachTmpServiceSurface(appid, service_surface);
845 this->tmp_services.emplace_back(TmpService{appid, dest, service_surface, uuid});
849 result<json_object *> WindowManager::api_get_display_info()
851 Screen screen = this->lc->getScreenInfo();
853 json_object *object = json_object_new_object();
854 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(screen.width()));
855 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(screen.height()));
857 json_object_object_add(object, kKeyWidthMm, json_object_new_int(0));
858 json_object_object_add(object, kKeyHeightMm, json_object_new_int(0));
859 json_object_object_add(object, kKeyScale, json_object_new_double(this->lc->scale()));
861 return Ok<json_object *>(object);
864 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
868 // TODO: application requests by old role,
869 // so convert role old to new
870 const char *role = this->convertRoleOldToNew(drawing_name);
872 // Check drawing name, surface/layer id
873 auto const &surface_id = this->id_alloc.lookup(string(role));
876 return Err<json_object *>("Surface does not exist");
879 // Set area rectangle
880 rect area_info = this->area_info[*surface_id];
881 json_object *object = json_object_new_object();
882 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
883 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
884 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
885 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
887 return Ok<json_object *>(object);
890 result<json_object *> WindowManager::api_get_car_info(char const *label)
892 json_object *j_in = nullptr;
893 json_object *j_out = nullptr;
895 if (0 == strcmp("parking_brake_status", label))
897 // Get parking brake status
898 json_bool val = (this->crr_car_info.parking_brake_stt) ? TRUE : FALSE;
899 j_in = json_object_new_boolean(val);
901 else if (0 == strcmp("accelerator.pedal.position", label))
903 // Get accelerator pedal position
904 double val = this->crr_car_info.accel_pedal_pos;
905 j_in = json_object_new_double(val);
907 else if (0 == strcmp("car_state", label))
910 const char* val = (this->crr_car_info.running_stt) ? "run" : "stop";
911 j_in = json_object_new_string(val);
913 else if (0 == strcmp("lightstatus.brake", label)) {
914 // Get lightstatus brake status
915 json_bool val = (this->crr_car_info.lightstatus_brake_stt) ? TRUE : FALSE;
916 j_in = json_object_new_boolean(val);
920 return Err<json_object *>("Car info does not exist");
923 // Create output object
924 j_out = json_object_new_object();
925 json_object_object_add(j_out, "value", j_in);
927 return Ok<json_object *>(j_out);
930 void WindowManager::send_event(char const *evname)
932 HMI_DEBUG("%s: %s", __func__, evname);
934 int ret = afb_event_push(this->map_afb_event[evname], nullptr);
937 HMI_DEBUG("afb_event_push: %m");
941 void WindowManager::send_event(char const *evname, char const *label)
943 HMI_DEBUG("%s: %s(%s)", __func__, evname, label);
945 json_object *j = json_object_new_object();
946 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
948 int ret = afb_event_push(this->map_afb_event[evname], j);
951 HMI_DEBUG("afb_event_push failed: %m");
955 void WindowManager::send_event(char const *evname, char const *label, char const *area,
956 int x, int y, int w, int h)
958 HMI_DEBUG("%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
959 __func__, evname, label, area, x, y, w, h);
961 json_object *j_rect = json_object_new_object();
962 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
963 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
964 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
965 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
967 json_object *j = json_object_new_object();
968 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
969 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
970 json_object_object_add(j, kKeyDrawingRect, j_rect);
972 int ret = afb_event_push(this->map_afb_event[evname], j);
975 HMI_DEBUG("afb_event_push failed: %m");
979 string WindowManager::searchApp(unsigned pid, unsigned ppid, unsigned surface, json_object* resp)
981 // retrieve appid from pid from application manager
983 // check appid then add it to the client
984 HMI_INFO("Runners:%s", json_object_get_string(resp));
985 int size = json_object_array_length(resp);
986 HMI_INFO("pid %d, ppid %d, surface %d",pid, ppid, surface);
987 for(int i = 0; i < size; i++)
989 json_object *j = json_object_array_get_idx(resp, i);
990 int runid = jh::getIntFromJson(j, "runid");
991 const char* id = jh::getStringFromJson(j, "id");
992 HMI_DEBUG("Appid %s, runid %d", id, runid);
993 if(id && (runid == ppid))
996 s_id.erase(s_id.find('@'));
998 HMI_INFO("App found %s", appid.c_str());
1004 HMI_WARNING("Failed to retrieve id");
1009 void WindowManager::storeSurface(const string& appid, unsigned ppid, unsigned surface)
1011 auto elem = std::find_if(this->tmp_services.begin(), this->tmp_services.end(),
1012 [&appid](TmpService& ts){
1013 return (ts.dest == appid );
1016 this->lc->setXDGSurfaceOriginSize(surface);
1017 if(elem != this->tmp_services.end())
1020 auto client = g_app_list.lookUpClient(elem->dest);
1021 if(client == nullptr)
1025 HMI_INFO("Attach surface %d (service %s) to app %s", surface, elem->service.c_str(), elem->dest.c_str());
1026 client->attachServiceSurface(elem->service, surface);
1031 auto client = g_app_list.lookUpClient(appid);
1032 if(client != nullptr)
1034 client->addSurface(surface);
1035 this->id_alloc.register_name_id(client->role(), surface);
1039 // Store tmp surface and appid for application
1040 // who requests setRole after creating shell surface
1041 this->tmp_surface2app.emplace(surface, TmpClient{appid, ppid});
1049 void WindowManager::surface_created(unsigned pid, unsigned surface_id)
1052 if(this->tmp_surface2app.count(surface_id) != 0)
1054 string appid = this->tmp_surface2app[surface_id].appid;
1055 auto client = g_app_list.lookUpClient(appid);
1056 if(client != nullptr)
1058 WMError ret = client->addSurface(surface_id);
1059 HMI_INFO("Add surface %d to \"%s\"", surface_id, appid.c_str());
1060 if(ret != WMError::SUCCESS)
1062 HMI_ERROR("Failed to add surface to client %s", client->appID().c_str());
1065 this->tmp_surface2app.erase(surface_id);
1069 HMI_NOTICE("Unknown surface %d", surface_id);
1071 std::ostringstream os;
1073 string path = "/proc/" + os.str() + "/stat";
1074 std::ifstream ifs(path.c_str());
1077 if(!ifs.fail() && std::getline(ifs, str))
1079 std::sscanf(str.data(), "%*d %*s %*c %d", &ppid);
1080 HMI_INFO("Retrieve ppid %d", ppid);
1084 HMI_ERROR("Failed to open /proc/%d/stat", pid);
1085 HMI_ERROR("File system may be different");
1088 struct AfbClosure* c = new struct AfbClosure(pid, ppid, surface_id);
1089 // search pid from surfaceID
1090 afb_service_call("afm-main", "runners", json_object_new_object(),
1091 [](void* closure, int stat, json_object* resp){
1092 HMI_DEBUG("check %s", json_object_get_string(resp));
1093 struct AfbClosure* c = static_cast<struct AfbClosure*>(closure);
1097 HMI_ERROR("Failed to call runners");
1102 json_object_object_get_ex(resp, "response", &j);
1103 string appid = g_context->searchApp(c->pid, c->ppid, c->surface, j);
1106 g_context->storeSurface(appid, c->ppid, c->surface);
1109 json_object_put(resp);
1115 void WindowManager::surface_removed(unsigned surface_id)
1117 HMI_DEBUG("Delete surface_id %u", surface_id);
1118 this->id_alloc.remove_id(surface_id);
1119 // this->layers.remove_surface(surface_id);
1120 g_app_list.removeSurface(surface_id);
1123 void WindowManager::removeClient(const string &appid)
1125 HMI_DEBUG("Remove clinet %s from list", appid.c_str());
1126 auto client = g_app_list.lookUpClient(appid);
1127 this->lc->terminateApp(client);
1128 g_app_list.removeClient(appid);
1131 void WindowManager::exceptionProcessForTransition()
1133 unsigned req_num = g_app_list.currentRequestNumber();
1134 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
1135 g_app_list.removeRequest(req_num);
1136 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
1137 this->processNextRequest();
1140 void WindowManager::analyzeReceivedEvent(const char *event, struct json_object *object)
1142 HMI_DEBUG("event:%s", event);
1144 // If receive low can signal
1145 if (strstr(event, "low-can"))
1147 // Analyze low can signal
1148 const char *signal_name = this->lcc.analyzeCanSignal(object);
1150 // Create task for car state and input it to PolicyManager
1151 Task task = this->convertCanSignalToCarStateTask(signal_name);
1152 if (Task::TASK_INVALID != task)
1154 this->inputCarStateTask(task);
1159 void WindowManager::timerHandler()
1161 unsigned req_num = g_app_list.currentRequestNumber();
1162 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
1163 g_app_list.reqDump();
1164 g_app_list.removeRequest(req_num);
1165 this->processNextRequest();
1168 void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
1170 WMError ret = WMError::UNKNOWN;
1171 // req_num is guaranteed by Window Manager
1172 unsigned req_num = g_app_list.currentRequestNumber();
1173 Task task = Task::TASK_INVALID;
1175 if (actions.empty())
1177 if (g_app_list.haveRequest())
1179 HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
1180 goto proc_remove_request;
1184 HMI_SEQ_DEBUG(req_num, "There is no request");
1189 // Check weather there is the no request task
1190 // [The no request task]
1191 // - TaskCarState::RESTRICTION_MODE_OFF
1192 // - TaskCarState::RESTRICTION_MODE_ON
1193 for (const auto &act : actions)
1195 if (TaskCarState::RESTRICTION_MODE_OFF == act.car_state)
1197 task = Task::TASK_RESTRICTION_MODE_OFF;
1200 else if (TaskCarState::RESTRICTION_MODE_ON == act.car_state)
1202 task = Task::TASK_RESTRICTION_MODE_ON;
1207 // If there is the request-less task, set request here
1208 if (Task::TASK_INVALID != task) {
1210 ret = this->setRequest(task, &req_num);
1212 if(ret != WMError::SUCCESS)
1218 for (auto &act : actions)
1223 auto const &surface_id = this->id_alloc.lookup(act.role.c_str());
1224 if(surface_id == nullopt)
1226 HMI_SEQ_DEBUG(req_num, "There is not surface id for role:%s", act.role.c_str());
1230 std::string appid = g_app_list.getAppID(*surface_id, &found);
1233 if (TaskVisible::INVISIBLE == act.visible)
1235 HMI_SEQ_DEBUG(req_num, "role:%s is killed, so do not set this action", act.role.c_str());
1240 HMI_SEQ_ERROR(req_num, "appid of role:%s which is visible is not found", act.role.c_str());
1241 ret = WMError::FAIL;
1245 auto client = g_app_list.lookUpClient(appid);
1246 act.req_num = req_num;
1247 act.client = client;
1249 // If Window Manager is master and this action is for slave,
1250 // change TaskVisible
1251 if (this->wmcon.isMasterMode())
1253 if (this->wmcon.isMasterArea(act.area.c_str()))
1255 HMI_DEBUG("Set TaskVisible::REQ_REMOTE_VISIBLE");
1256 act.visible = TaskVisible::REQ_REMOTE_VISIBLE;
1258 // TODO: Check whether role is tbtnavi to request remote invisible
1259 else if (("tbtnavi" == act.role) &&
1260 (TaskVisible::INVISIBLE == act.visible))
1262 HMI_DEBUG("Set TaskVisible::REQ_REMOTE_INVISIBLE");
1263 act.visible = TaskVisible::REQ_REMOTE_INVISIBLE;
1268 ret = g_app_list.setAction(req_num, act);
1269 if (ret != WMError::SUCCESS)
1271 HMI_SEQ_ERROR(req_num, "Setting action is failed");
1276 HMI_SEQ_DEBUG(req_num, "Start transition.");
1277 ret = this->startTransition(req_num);
1278 if (ret != WMError::SUCCESS)
1280 if (ret == WMError::NO_LAYOUT_CHANGE)
1282 goto proc_remove_request;
1286 HMI_SEQ_ERROR(req_num, "Transition state is failed");
1294 //this->emit_error()
1295 HMI_SEQ_ERROR(req_num, errorDescription(ret));
1296 this->pmw.undoState();
1298 proc_remove_request:
1299 g_app_list.removeRequest(req_num);
1300 this->processNextRequest();
1303 void WindowManager::processError(WMError error)
1305 unsigned req_num = g_app_list.currentRequestNumber();
1307 //this->emit_error()
1308 HMI_SEQ_ERROR(req_num, errorDescription(error));
1309 g_app_list.removeRequest(req_num);
1310 this->processNextRequest();
1313 void WindowManager::processForRemoteRequest(json_object *data)
1315 const char *req = jh::getStringFromJson(data, "req");
1316 const char *appid = jh::getStringFromJson(data, "appid");
1317 const char *drawing_name = jh::getStringFromJson(data, "drawing_name");
1318 const char *drawing_area = jh::getStringFromJson(data, "drawing_area");
1320 if (!req || !drawing_name)
1322 HMI_ERROR("Parse Error!!");
1326 if (this->wmcon.isMasterMode())
1330 HMI_ERROR("Parse Error!!");
1334 auto reply = [](const char *errmsg) {
1335 if (errmsg != nullptr)
1342 if ("activateWindow" == std::string(req))
1346 HMI_ERROR("Parse Error!!");
1350 this->api_activate_surface_for_slave(
1351 appid, drawing_name, drawing_area, reply);
1353 else if ("deactivateWindow" == std::string(req))
1355 this->api_deactivate_surface_for_slave(
1356 appid, drawing_name, reply);
1358 else if ("endDraw" == std::string(req))
1360 this->api_enddraw(appid, drawing_name);
1365 if ("syncDraw" == std::string(req))
1369 if (!appid || !drawing_area)
1371 HMI_ERROR("Parse Error!!");
1375 unsigned req_num = g_app_list.currentRequestNumber();
1376 auto client = g_app_list.lookUpClient(appid);
1378 // TODO: application requests by old role,
1379 // so convert role old to new
1380 const char *c_role = this->convertRoleOldToNew(drawing_name);
1383 bool end_draw_finished = false;
1389 string(drawing_area),
1390 TaskVisible::REMOTE_VISIBLE,
1392 TaskCarState::NO_TASK
1396 WMError ret = g_app_list.setAction(req_num, act);
1397 if (ret != WMError::SUCCESS)
1399 HMI_SEQ_ERROR(req_num, "Setting action is failed");
1403 this->emit_syncdraw(string(drawing_name), string(drawing_area));
1404 this->wmcon.startSyncDrawForRemote(appid);
1407 else if ("activated" == std::string(req))
1409 this->emit_visible(drawing_name);
1410 this->emit_activated(drawing_name);
1412 else if ("deactivated" == std::string(req))
1416 if (!appid || !drawing_area)
1418 HMI_ERROR("Parse Error!!");
1422 unsigned req_num = g_app_list.currentRequestNumber();
1423 auto client = g_app_list.lookUpClient(appid);
1425 // TODO: application requests by old role,
1426 // so convert role old to new
1427 const char *c_role = this->convertRoleOldToNew(drawing_name);
1430 bool end_draw_finished = true;
1437 TaskVisible::REMOTE_INVISIBLE,
1439 TaskCarState::NO_TASK
1442 this->lc->visibilityChange(act);
1443 this->lc->renderLayers();
1444 this->lc->renderLayersRemote();
1446 this->emit_invisible(drawing_name);
1447 this->emit_deactivated(drawing_name);
1448 this->emitScreenUpdated(req_num);
1450 g_app_list.removeRequest(req_num);
1451 this->processNextRequest();
1453 else if ("flushDraw" == std::string(req))
1455 this->emit_flushdraw(drawing_name);
1461 ******* Private Functions *******
1464 void WindowManager::emit_activated(char const *label)
1466 this->send_event(kListEventName[Event_Active], label);
1469 void WindowManager::emit_deactivated(char const *label)
1471 this->send_event(kListEventName[Event_Inactive], label);
1474 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1476 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1479 void WindowManager::emit_syncdraw(const string &role, const string &area)
1481 rect rect = this->lc->getAreaSize(area);
1482 this->send_event(kListEventName[Event_SyncDraw],
1483 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
1486 void WindowManager::emit_flushdraw(char const *label)
1488 this->send_event(kListEventName[Event_FlushDraw], label);
1491 void WindowManager::emit_visible(char const *label, bool is_visible)
1493 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1496 void WindowManager::emit_invisible(char const *label)
1498 return emit_visible(label, false);
1501 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
1503 void WindowManager::emitHeadlampOff()
1505 // Send HeadlampOff event for all application
1506 this->send_event(kListEventName[Event_HeadlampOff]);
1509 void WindowManager::emitHeadlampOn()
1511 // Send HeadlampOn event for all application
1512 this->send_event(kListEventName[Event_HeadlampOn]);
1515 void WindowManager::emitParkingBrakeOff()
1517 // Send ParkingBrakeOff event for all application
1518 this->send_event(kListEventName[Event_ParkingBrakeOff]);
1521 void WindowManager::emitParkingBrakeOn()
1523 // Send ParkingBrakeOn event for all application
1524 this->send_event(kListEventName[Event_ParkingBrakeOn]);
1527 void WindowManager::emitLightstatusBrakeOff()
1529 // Send LightstatusBrakeOff event for all application
1530 this->send_event(kListEventName[Event_LightstatusBrakeOff]);
1533 void WindowManager::emitLightstatusBrakeOn()
1535 // Send LightstatusBrakeOn event for all application
1536 this->send_event(kListEventName[Event_LightstatusBrakeOn]);
1539 void WindowManager::emitCarStop()
1541 // Send CarStop event for all application
1542 this->send_event(kListEventName[Event_CarStop]);
1545 void WindowManager::emitCarRun()
1547 // Send CarRun event for all application
1548 this->send_event(kListEventName[Event_CarRun]);
1551 WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
1552 Task task, unsigned* req_num)
1554 if (!g_app_list.contains(appid))
1556 return WMError::NOT_REGISTERED;
1559 auto client = g_app_list.lookUpClient(appid);
1564 unsigned current = g_app_list.currentRequestNumber();
1565 unsigned requested_num = g_app_list.getRequestNumber(appid);
1566 if (requested_num != 0)
1568 HMI_SEQ_INFO(requested_num,
1569 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
1570 return REQ_REJECTED;
1573 WMRequest req = WMRequest(appid, role, area, task);
1574 unsigned new_req = g_app_list.addRequest(req);
1576 g_app_list.reqDump();
1578 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1580 return WMError::SUCCESS;
1583 WMError WindowManager::setRequest(Task task, unsigned* req_num)
1588 unsigned current = g_app_list.currentRequestNumber();
1590 WMRequest req = WMRequest(task);
1591 unsigned new_req = g_app_list.addRequest(req);
1593 g_app_list.reqDump();
1595 HMI_SEQ_DEBUG(current, "start sequence for task:%d", task);
1597 return WMError::SUCCESS;
1600 WMError WindowManager::setRequestForSlave(const string& appid, const string &role, const string &area,
1601 Task task, unsigned* req_num)
1606 unsigned current = g_app_list.currentRequestNumber();
1607 unsigned requested_num = g_app_list.getRequestNumber(appid);
1608 if (requested_num != 0)
1610 HMI_SEQ_INFO(requested_num,
1611 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
1612 return REQ_REJECTED;
1615 WMRequest req = WMRequest(appid, role, area, task);
1616 unsigned new_req = g_app_list.addRequest(req);
1618 g_app_list.reqDump();
1620 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1622 return WMError::SUCCESS;
1625 WMError WindowManager::checkPolicy(unsigned req_num)
1630 // get current trigger
1632 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1633 auto trigger = g_app_list.getRequest(req_num, &found);
1636 ret = WMError::NO_ENTRY;
1639 string req_area = trigger.area;
1641 if (trigger.task == Task::TASK_ALLOCATE)
1643 const char *msg = this->check_surface_exist(trigger.role.c_str());
1647 HMI_SEQ_ERROR(req_num, msg);
1652 // Input event data to PolicyManager
1653 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1655 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1659 // Execute state transition of PolicyManager
1660 if (0 > this->pmw.executeStateTransition())
1662 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1666 ret = WMError::SUCCESS;
1668 g_app_list.reqDump();
1673 WMError WindowManager::checkPolicyForSlave(unsigned req_num)
1678 // get current trigger
1680 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1681 auto trigger = g_app_list.getRequest(req_num, &found);
1684 ret = WMError::NO_ENTRY;
1687 string req_area = trigger.area;
1689 // Input event data to PolicyManager
1690 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1692 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1696 // Execute state transition of PolicyManager
1697 if (0 > this->pmw.executeStateTransition())
1699 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1703 ret = WMError::SUCCESS;
1705 g_app_list.reqDump();
1710 WMError WindowManager::startTransition(unsigned req_num)
1712 bool sync_draw_happen = false;
1714 WMError ret = WMError::SUCCESS;
1715 auto actions = g_app_list.getActions(req_num, &found);
1718 ret = WMError::NO_ENTRY;
1719 HMI_SEQ_ERROR(req_num,
1720 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1724 g_app_list.reqDump();
1725 for (const auto &action : actions)
1727 // TODO: application requests by old role,
1728 // so convert role new to old for emitting event
1729 string old_role = this->rolenew2old[action.role];
1731 if (action.visible == TaskVisible::VISIBLE)
1733 sync_draw_happen = true;
1735 this->emit_syncdraw(old_role, action.area);
1736 /* TODO: emit event for app not subscriber
1737 if(g_app_list.contains(y.appid))
1738 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1740 else if(action.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1742 // If this action is for slave, send to slave
1743 this->wmcon.sendRequest("syncDraw", action.client->appID().c_str(),
1744 old_role.c_str(), action.area.c_str());
1746 else if (action.car_state != TaskCarState::NO_TASK)
1748 this->transitionCarState(action.car_state);
1752 if (sync_draw_happen)
1758 // deactivate only, no syncDraw
1759 // Make it deactivate here
1760 for (const auto &x : actions)
1762 this->lc->visibilityChange(x);
1763 string old_role = this->rolenew2old[x.role];
1765 if (x.visible == TaskVisible::INVISIBLE)
1767 emit_deactivated(old_role.c_str());
1769 else if (x.visible == TaskVisible::REQ_REMOTE_INVISIBLE)
1771 // If this action is for slave, send to slave
1772 int i_ret = this->wmcon.sendRequest("deactivated", x.client->appID().c_str(),
1773 old_role.c_str(), "");
1776 ret = WMError::FAIL;
1780 /* if (g_app_list.contains(x.appid))
1782 auto client = g_app_list.lookUpClient(x.appid);
1783 //this->deactivate(client->surfaceID(x.role));
1786 this->lc->renderLayers();
1787 this->lc->renderLayersRemote();
1788 ret = WMError::NO_LAYOUT_CHANGE;
1793 void WindowManager::transitionCarState(TaskCarState task)
1795 if (TaskCarState::PARKING_BRAKE_OFF == task)
1797 this->crr_car_info.parking_brake_stt = false;
1798 this->emitParkingBrakeOff();
1800 else if (TaskCarState::PARKING_BRAKE_ON == task)
1802 this->crr_car_info.parking_brake_stt = true;
1803 this->emitParkingBrakeOn();
1805 else if (TaskCarState::ACCEL_PEDAL_OFF == task)
1807 this->crr_car_info.accel_pedal_stt = false;
1809 else if (TaskCarState::ACCEL_PEDAL_ON == task)
1811 this->crr_car_info.accel_pedal_stt = true;
1813 else if (TaskCarState::HEDLAMP_OFF == task)
1815 this->crr_car_info.headlamp_stt = false;
1816 this->emitHeadlampOff();
1818 else if (TaskCarState::HEDLAMP_ON == task)
1820 this->crr_car_info.headlamp_stt = true;
1821 this->emitHeadlampOn();
1823 else if (TaskCarState::LIGHTSTATUS_BRAKE_OFF == task)
1825 this->crr_car_info.lightstatus_brake_stt = false;
1826 this->emitLightstatusBrakeOff();
1828 else if (TaskCarState::LIGHTSTATUS_BRAKE_ON == task)
1830 this->crr_car_info.lightstatus_brake_stt = true;
1831 this->emitLightstatusBrakeOn();
1833 else if (TaskCarState::CAR_STOP == task)
1835 this->crr_car_info.running_stt = false;
1836 this->emitCarStop();
1838 else if (TaskCarState::CAR_RUN == task)
1840 this->crr_car_info.running_stt = true;
1845 WMError WindowManager::doEndDraw(unsigned req_num)
1849 auto actions = g_app_list.getActions(req_num, &found);
1850 WMError ret = WMError::SUCCESS;
1853 ret = WMError::NO_ENTRY;
1857 HMI_SEQ_INFO(req_num, "do endDraw");
1859 // layout change and make it visible
1860 for (const auto &act : actions)
1862 if(act.visible != TaskVisible::NO_CHANGE)
1865 ret = this->lc->layoutChange(act);
1866 if(ret != WMError::SUCCESS)
1868 HMI_SEQ_WARNING(req_num,
1869 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1872 ret = this->lc->visibilityChange(act);
1874 // Emit active/deactive event
1875 string old_role = this->rolenew2old[act.role];
1876 if(act.visible == TaskVisible::VISIBLE)
1878 emit_visible(old_role.c_str());
1879 emit_activated(old_role.c_str());
1881 else if(act.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1883 // If this action is for slave, send to slave
1884 int i_ret = this->wmcon.sendRequest("activated", "", old_role.c_str(), "");
1887 ret = WMError::FAIL;
1890 else if(act.visible == TaskVisible::REQ_REMOTE_INVISIBLE)
1892 // If this action is for slave, send to slave
1893 int i_ret = this->wmcon.sendRequest("deactivated", "", old_role.c_str(), "");
1896 ret = WMError::FAIL;
1899 else if((act.visible == TaskVisible::REMOTE_VISIBLE) ||
1900 (act.visible == TaskVisible::REMOTE_INVISIBLE))
1902 // nop because emit active/deactive by event from remote
1906 emit_invisible(old_role.c_str());
1907 emit_deactivated(old_role.c_str());
1910 if (ret != WMError::SUCCESS)
1912 HMI_SEQ_WARNING(req_num,
1913 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1916 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1919 this->lc->renderLayers();
1920 this->lc->renderLayersRemote();
1922 HMI_SEQ_INFO(req_num, "emit flushDraw");
1924 for(const auto &act_flush : actions)
1926 // TODO: application requests by old role,
1927 // so convert role new to old for emitting event
1928 string old_role = this->rolenew2old[act_flush.role];
1930 if(act_flush.visible == TaskVisible::VISIBLE)
1932 this->emit_flushdraw(old_role.c_str());
1934 else if(act_flush.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1936 // If this action is for slave, send to slave
1937 this->wmcon.sendRequest("flushDraw", "", old_role.c_str(), "");
1944 void WindowManager::emitScreenUpdated(unsigned req_num)
1947 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1949 auto actions = g_app_list.getActions(req_num, &found);
1952 // create json object
1953 json_object *j = json_object_new_object();
1954 json_object *jarray = json_object_new_array();
1956 for(const auto& action: actions)
1958 if((action.visible == TaskVisible::VISIBLE) ||
1959 (action.visible == TaskVisible::REMOTE_VISIBLE))
1961 json_object_array_add(jarray, json_object_new_string(action.client->appID().c_str()));
1964 json_object_object_add(j, kKeyIds, jarray);
1965 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1967 int ret = afb_event_push(
1968 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1971 HMI_DEBUG("afb_event_push failed: %m");
1975 void WindowManager::setTimer()
1978 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
1979 HMI_ERROR("Could't set time (clock_gettime() returns with error");
1983 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1984 if (g_timer_ev_src == nullptr)
1986 // firsttime set into sd_event
1987 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1988 CLOCK_BOOTTIME, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL, 1, processTimerHandler, this);
1991 HMI_ERROR("Could't set timer");
1996 // update timer limitation after second time
1997 sd_event_source_set_time(g_timer_ev_src, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL);
1998 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
2002 void WindowManager::stopTimer()
2004 unsigned req_num = g_app_list.currentRequestNumber();
2005 HMI_SEQ_DEBUG(req_num, "Timer stop");
2006 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
2009 HMI_SEQ_ERROR(req_num, "Timer stop failed");
2013 void WindowManager::processNextRequest()
2016 g_app_list.reqDump();
2017 unsigned req_num = g_app_list.currentRequestNumber();
2018 if (g_app_list.haveRequest())
2020 HMI_SEQ_DEBUG(req_num, "Process next request");
2021 WMError rc = checkPolicy(req_num);
2022 if (rc != WMError::SUCCESS)
2024 HMI_SEQ_ERROR(req_num, errorDescription(rc));
2029 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
2033 const char* WindowManager::convertRoleOldToNew(char const *old_role)
2035 const char *new_role = nullptr;
2037 for (auto const &on : this->roleold2new)
2039 std::regex regex = std::regex(on.first);
2040 if (std::regex_match(old_role, regex))
2042 // role is old. So convert to new.
2043 new_role = on.second.c_str();
2048 if (nullptr == new_role)
2050 // role is new or fallback.
2051 new_role = old_role;
2054 HMI_DEBUG("old:%s -> new:%s", old_role, new_role);
2059 int WindowManager::loadOldRolesConfigFile()
2061 // Get afm application installed dir
2062 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
2063 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
2066 if (!afm_app_install_dir)
2068 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
2072 file_name = string(afm_app_install_dir) + string(kPathOldRolesConfigFile);
2075 // Load old_rolea config file
2076 json_object* json_obj;
2077 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
2080 HMI_ERROR("Could not open %s, so use default old_roles information", kPathOldRolesConfigFile);
2081 json_obj = json_tokener_parse(kDefaultOldRolesConfig);
2083 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
2086 json_object* json_cfg;
2087 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
2089 HMI_ERROR("Parse Error!!");
2093 int len = json_object_array_length(json_cfg);
2094 HMI_DEBUG("json_cfg len:%d", len);
2095 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
2097 for (int i=0; i<len; i++)
2099 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
2101 const char* old_role = jh::getStringFromJson(json_tmp, "name");
2102 if (nullptr == old_role)
2104 HMI_ERROR("Parse Error!!");
2108 const char* new_role = jh::getStringFromJson(json_tmp, "new");
2109 if (nullptr == new_role)
2111 HMI_ERROR("Parse Error!!");
2115 this->roleold2new[old_role] = string(new_role);
2119 for(auto itr = this->roleold2new.begin();
2120 itr != this->roleold2new.end(); ++itr)
2122 HMI_DEBUG(">>> role old:%s new:%s",
2123 itr->first.c_str(), itr->second.c_str());
2126 // Release json_object
2127 json_object_put(json_obj);
2132 Task WindowManager::convertCanSignalToCarStateTask(const char *signal_name)
2134 wm::LowCanClient *lcc = &(this->lcc);
2135 Task task = Task::TASK_INVALID;
2137 // If car info is updated, set car state change event
2138 if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoParkingBrake]))
2140 HMI_DEBUG("Parking Brake state is changed");
2142 if (lcc->getCurrentParkingBrakeState())
2144 task = wm::Task::TASK_PARKING_BRAKE_ON;
2148 task = wm::Task::TASK_PARKING_BRAKE_OFF;
2151 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoAccelPedalPos]))
2153 // Update accel pedal position
2154 this->crr_car_info.accel_pedal_pos = lcc->getCurrentAccelPedalPosition();
2156 if (lcc->isChangedAccelPedalState())
2158 HMI_DEBUG("Accelerator Pedal state is changed");
2160 if (lcc->getCurrentAccelPedalState())
2162 task = wm::Task::TASK_ACCEL_PEDAL_ON;
2166 task = wm::Task::TASK_ACCEL_PEDAL_OFF;
2170 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoHeadlame]))
2172 HMI_DEBUG("Headlamp state is changed");
2174 if (lcc->getCurrentHeadlampState())
2176 task = wm::Task::TASK_HEDLAMP_ON;
2180 task = wm::Task::TASK_HEDLAMP_OFF;
2183 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoLightstatusBrake]))
2185 HMI_DEBUG("Lightstatus Brake state is changed");
2187 if (lcc->getCurrentLightstatusBrakeState())
2189 task = wm::Task::TASK_LIGHTSTATUS_BRAKE_ON;
2193 task = wm::Task::TASK_LIGHTSTATUS_BRAKE_OFF;
2199 void WindowManager::inputCarStateTask(Task task)
2201 unsigned req_num = 0;
2202 WMError ret = WMError::UNKNOWN;
2204 ret = this->setRequest(task, &req_num);
2206 if(ret != WMError::SUCCESS)
2208 HMI_ERROR(errorDescription(ret));
2212 if (req_num != g_app_list.currentRequestNumber())
2214 // Add request, then invoked after the previous task is finished
2215 HMI_SEQ_DEBUG(req_num, "request is accepted");
2222 ret = this->checkPolicy(req_num);
2224 if (ret != WMError::SUCCESS)
2226 //this->emit_error()
2227 HMI_SEQ_ERROR(req_num, errorDescription(ret));
2228 g_app_list.removeRequest(req_num);
2229 this->processNextRequest();
2233 const char *WindowManager::check_surface_exist(const char *drawing_name)
2235 auto const &surface_id = this->id_alloc.lookup(string(drawing_name));
2238 return "Surface does not exist";
2241 /* if (!this->controller->surface_exists(*surface_id))
2243 return "Surface does not exist in controller!";
2246 /* auto layer_id = this->layers.get_layer_id(*surface_id);
2250 return "Surface is not on any layer!";
2253 HMI_DEBUG("surface %d is detected", *surface_id);
2257 const char* WindowManager::kDefaultOldRolesConfig = "{ \
2260 \"name\": \"HomeScreen\", \
2261 \"new\": \"homescreen\" \
2264 \"name\": \"Music\", \
2265 \"new\": \"music\" \
2268 \"name\": \"MediaPlayer\", \
2269 \"new\": \"music\" \
2272 \"name\": \"Video\", \
2273 \"new\": \"video\" \
2276 \"name\": \"VideoPlayer\", \
2277 \"new\": \"video\" \
2280 \"name\": \"WebBrowser\", \
2281 \"new\": \"browser\" \
2284 \"name\": \"Radio\", \
2285 \"new\": \"radio\" \
2288 \"name\": \"Phone\", \
2289 \"new\": \"phone\" \
2292 \"name\": \"Navigation\", \
2296 \"name\": \"HVAC\", \
2300 \"name\": \"Settings\", \
2301 \"new\": \"settings\" \
2304 \"name\": \"Dashboard\", \
2305 \"new\": \"dashboard\" \
2308 \"name\": \"POI\", \
2312 \"name\": \"Mixer\", \
2313 \"new\": \"mixer\" \
2316 \"name\": \"Restriction\", \
2317 \"new\": \"restriction\" \
2320 \"name\": \"^OnScreen.*\", \
2321 \"new\": \"on_screen\" \