Fix source size 0 sometimes
[apps/agl-service-windowmanager-2017.git] / src / window_manager.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <fstream>
18 #include <regex>
19 #include <sstream>
20
21 #include "window_manager.hpp"
22 #include "json_helper.hpp"
23 #include "applist.hpp"
24
25 extern "C"
26 {
27 #include <systemd/sd-event.h>
28 }
29
30 using std::string;
31 using std::vector;
32
33 namespace wm
34 {
35
36 static const uint64_t kTimeOut = 3ULL; /* 3s */
37
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";
44
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";
59
60 static const char kPathOldRolesConfigFile[] = "/etc/old_roles.json";
61
62 static sd_event_source *g_timer_ev_src = nullptr;
63 static AppList g_app_list;
64 static WindowManager *g_context;
65
66 struct AfbClosure {
67 public:
68     AfbClosure(unsigned pid, unsigned ppid, unsigned surface)
69         : pid(pid), ppid(ppid), surface(surface) {}
70     ~AfbClosure() = default;
71     unsigned pid;
72     unsigned ppid;
73     unsigned surface;
74 };
75
76 namespace
77 {
78
79 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
80 {
81     HMI_NOTICE("Time out occurs because the client replys endDraw slow, so revert the request");
82     reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
83     return 0;
84 }
85
86 static void onStateTransitioned(vector<WMAction> actions)
87 {
88     g_context->startTransitionWrapper(actions);
89 }
90
91 static void onError()
92 {
93     g_context->processError(WMError::LAYOUT_CHANGE_FAIL);
94 }
95
96 static void onReceiveRemoteRequest(json_object *data)
97 {
98     g_context->processForRemoteRequest(data);
99 }
100 } // namespace
101
102 /**
103  * WindowManager Impl
104  */
105 WindowManager::WindowManager()
106     : wmcon{},
107       id_alloc{}
108 {
109     const char *path = getenv("AFM_APP_INSTALL_DIR");
110     if (!path)
111     {
112         HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
113     }
114     string root = path;
115
116     // TODO: ECU name should be decide by config file
117     // Get mode and decide ECU name
118     string ecu_name = this->wmcon.getEcuName();
119
120     this->lc = std::make_shared<LayerControl>(root, ecu_name);
121
122     HMI_DEBUG("Layer Controller initialized");
123 }
124
125 int WindowManager::init()
126 {
127     LayerControlCallbacks lmcb;
128     lmcb.surfaceCreated = [&](unsigned pid, unsigned surface){
129         this->surface_created(pid, surface);
130         };
131     lmcb.surfaceDestroyed = [&](unsigned surface){
132         this->surface_removed(surface);
133     };
134     this->lc->init(lmcb);
135
136     // TODO: application requests by old role,
137     //       so create role map (old, new)
138     // Load old_roles config file
139     this->loadOldRolesConfigFile();
140
141     // Initialize LowCanClient
142     this->lcc.initialize();
143
144     // Store my context for calling callback from PolicyManager
145     g_context = this;
146
147     // Initialize PMWrapper
148     this->pmw.initialize(this->wmcon.getEcuName());
149
150     // Register callback to PolicyManager
151     this->pmw.registerCallback(onStateTransitioned, onError);
152
153     // Initialize WMConnection
154     this->wmcon.initialize();
155
156     // Register callback to WMConnection
157     this->wmcon.registerCallback(onReceiveRemoteRequest);
158
159     // Make afb event
160     for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
161     {
162         map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
163     }
164
165     const struct rect css_bg = this->lc->getAreaSize("fullscreen");
166     Screen screen = this->lc->getScreenInfo();
167     rectangle dp_bg(screen.width(), screen.height());
168
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());
174
175     double scale = static_cast<double>(dp_bg.height()) / css_bg.h;
176     this->lc->setupArea(dp_bg, scale);
177
178     return 0; //init_layers();
179 }
180
181 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
182 {
183     // TODO: application requests by old role,
184     //       so convert role old to new
185     const char *role = this->convertRoleOldToNew(drawing_name);
186     string l_name;
187     string s_appid = appid;
188     string s_role = role;
189
190     if(!g_app_list.contains(s_appid))
191     {
192         // auto lid = this->layers.get_layer_id(string(role));
193         unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
194         if (l_id == 0)
195         {
196             /**
197              * register drawing_name as fallback and make it displayed.
198              */
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);
201             if (l_id == 0)
202             {
203                 return Err<int>("Designated role does not match any role, fallback is disabled");
204             }
205         }
206
207         // TODO: remote layer size is fixed value
208         if ("Remote" == l_name)
209         {
210             this->lc->createNewRemoteLayer(l_id);
211         }
212         else
213         {
214             this->lc->createNewLayer(l_id);
215         }
216
217         // add client into the db
218         g_app_list.addClient(s_appid, l_id, s_role);
219     }
220
221     // generate surface ID for ivi-shell application
222
223     auto rname = this->id_alloc.lookup(string(role));
224     if (!rname)
225     {
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};
229
230         // Work Around
231         HMI_NOTICE("WORK AROUND: add surface on request surface");
232         auto client = g_app_list.lookUpClient(s_appid);
233         client->addSurface(id);
234         ///////////////
235
236         // Set role map of (new, old)
237         this->rolenew2old[role] = string(drawing_name);
238
239         return Ok<int>(id);
240     }
241
242     // Check currently registered drawing names if it is already there.
243     return Err<int>("Surface already present");
244 }
245
246 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
247                                      char const *ivi_id)
248 {
249     unsigned sid = std::stol(ivi_id);
250
251     HMI_DEBUG("This API(requestSurfaceXDG) is for XDG Application using runXDG");
252     /*
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
256      */
257     WMError ret = this->lc->setXDGSurfaceOriginSize(sid);
258     if(ret != SUCCESS)
259     {
260         HMI_ERROR("%s", errorDescription(ret));
261         HMI_WARNING("The main user of this API is runXDG");
262         return "fail";
263     }
264
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;
270     string l_name;
271
272     if(!g_app_list.contains(s_appid))
273     {
274         // auto lid = this->layers.get_layer_id(string(role));
275         unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
276         if (l_id == 0)
277         {
278             /**
279              * register drawing_name as fallback and make it displayed.
280              */
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);
283             if (l_id == 0)
284             {
285                 return "Designated role does not match any role, fallback is disabled";
286             }
287         }
288
289         // TODO: remote layer size is fixed value
290         if ("Remote" == l_name)
291         {
292             this->lc->createNewRemoteLayer(l_id);
293         }
294         else
295         {
296             this->lc->createNewLayer(l_id);
297         }
298
299         // add client into the db
300         g_app_list.addClient(s_appid, l_id, s_role);
301     }
302
303     auto rname = this->id_alloc.lookup(s_role);
304     if (rname)
305     {
306         return "Surface already present";
307     }
308
309     // register pair drawing_name and ivi_id
310     this->id_alloc.register_name_id(role, sid);
311
312     auto client = g_app_list.lookUpClient(s_appid);
313     client->addSurface(sid);
314
315     // Set role map of (new, old)
316     this->rolenew2old[role] = string(drawing_name);
317
318     return nullptr;
319 }
320
321 bool WindowManager::api_set_role(char const *appid, char const *drawing_name)
322 {
323     bool ret = false;
324
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;
330     string l_name;
331
332     // Create WMClient
333     if(!g_app_list.contains(s_appid))
334     {
335         // auto lid = this->layers.get_layer_id(string(role));
336         unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
337         if (l_id == 0)
338         {
339             /**
340              * register drawing_name as fallback and make it displayed.
341              */
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);
344             if (l_id == 0)
345             {
346                 HMI_ERROR("Designated role does not match any role, fallback is disabled");
347                 return ret;
348             }
349         }
350
351         // TODO: remote layer size is fixed value
352         if ("Remote" == l_name)
353         {
354             this->lc->createNewRemoteLayer(l_id);
355         }
356         else
357         {
358             this->lc->createNewLayer(l_id);
359         }
360
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);
365     }
366
367     // for(auto itr = this->tmp_surface2app.begin();
368     //         itr != this->tmp_surface2app.end() ; ++itr)
369     // {
370     for(auto& x : this->tmp_surface2app)
371     {
372         if(x.second.appid == s_appid)
373         {
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);
379             break;
380         }
381     }
382
383 /*     if(0 != pid){
384         // search floating surfaceID from pid if pid is designated.
385         wm_err = g_app_list.popFloatingSurface(pid, &surface);
386     }
387     else{
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);
391     }
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());
396         return ret;
397     }
398
399     ret = true;
400     if (g_app_list.contains(id))
401     {
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);
406     }
407     else{
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);
411     } */
412
413     // register pair drawing_name and ivi_id
414
415     return true;
416 }
417
418 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
419                                char const *drawing_area, const reply_func &reply)
420 {
421     // TODO: application requests by old role,
422     //       so convert role old to new
423     const char *c_role = this->convertRoleOldToNew(drawing_name);
424
425     string id = appid;
426     string role = c_role;
427     string area = drawing_area;
428
429     if(!g_app_list.contains(id))
430     {
431         reply("app doesn't request 'requestSurface' or 'setRole' yet");
432         return;
433     }
434     auto client = g_app_list.lookUpClient(id);
435
436     // unsigned srfc = client->surfaceID(role);
437     // unsigned layer = client->layerID();
438
439     // g_app_list.removeFloatingSurface(client->surfaceID());
440     // g_app_list.removeFloatingSurface(client);
441
442     Task task = Task::TASK_ALLOCATE;
443     unsigned req_num = 0;
444     WMError ret = WMError::UNKNOWN;
445
446     ret = this->setRequest(id, role, area, task, &req_num);
447
448     //vector<WMLayerState> current_states = this->lc->getCurrentStates();
449     // ret = this->setRequest(id, role, area, task, current_states, &req_num);
450
451     if(ret != WMError::SUCCESS)
452     {
453         HMI_ERROR(errorDescription(ret));
454         reply("Failed to set request");
455         return;
456     }
457
458     reply(nullptr);
459     if (req_num != g_app_list.currentRequestNumber())
460     {
461         // Add request, then invoked after the previous task is finished
462         HMI_SEQ_DEBUG(req_num, "request is accepted");
463         return;
464     }
465
466     /*
467      * Do allocate tasks
468      */
469     ret = this->checkPolicy(req_num);
470
471     if (ret != WMError::SUCCESS)
472     {
473         //this->emit_error()
474         HMI_SEQ_ERROR(req_num, errorDescription(ret));
475         g_app_list.removeRequest(req_num);
476         this->processNextRequest();
477     }
478 }
479
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)
483 {
484     // TODO: application requests by old role,
485     //       so convert role old to new
486     const char *c_role = this->convertRoleOldToNew(drawing_name);
487
488     string id = appid;
489     string role = c_role;
490     string area = drawing_area;
491
492     if(!g_app_list.contains(id))
493     {
494         // Request surface of app in slave to register app information
495         this->api_request_surface(appid, drawing_name);
496
497         // Set role of app in slave to register app information
498         this->api_set_role(appid, drawing_name);
499     }
500     auto client = g_app_list.lookUpClient(id);
501
502     // unsigned srfc = client->surfaceID(role);
503     // unsigned layer = client->layerID();
504
505     // g_app_list.removeFloatingSurface(client->surfaceID());
506     // g_app_list.removeFloatingSurface(client);
507
508     Task task = Task::TASK_ALLOCATE;
509     unsigned req_num = 0;
510     WMError ret = WMError::UNKNOWN;
511
512     ret = this->setRequestForSlave(id, role, area, task, &req_num);
513
514     //vector<WMLayerState> current_states = this->lc->getCurrentStates();
515     // ret = this->setRequest(id, role, area, task, current_states, &req_num);
516
517     if(ret != WMError::SUCCESS)
518     {
519         HMI_ERROR(errorDescription(ret));
520         reply("Failed to set request");
521         return;
522     }
523
524     reply(nullptr);
525     if (req_num != g_app_list.currentRequestNumber())
526     {
527         // Add request, then invoked after the previous task is finished
528         HMI_SEQ_DEBUG(req_num, "request is accepted");
529         return;
530     }
531
532     /*
533      * Do allocate tasks
534      */
535     ret = this->checkPolicyForSlave(req_num);
536
537     if (ret != WMError::SUCCESS)
538     {
539         //this->emit_error()
540         HMI_SEQ_ERROR(req_num, errorDescription(ret));
541         g_app_list.removeRequest(req_num);
542         this->processNextRequest();
543     }
544 }
545
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)
549 {
550     // TODO: application requests by old role,
551     //       so convert role old to new
552     const char *c_role = this->convertRoleOldToNew(drawing_name);
553
554     string id = appid;
555     string role = c_role;
556     string area = drawing_area;
557
558     if(!g_app_list.contains(id))
559     {
560         reply("app doesn't request 'requestSurface' or 'setRole' yet");
561         return;
562     }
563     auto client = g_app_list.lookUpClient(id);
564
565     // unsigned srfc = client->surfaceID(role);
566     // unsigned layer = client->layerID();
567
568     // g_app_list.removeFloatingSurface(client->surfaceID());
569     // g_app_list.removeFloatingSurface(client);
570
571     Task task = Task::TASK_ALLOCATE;
572     unsigned req_num = 0;
573     WMError ret = WMError::UNKNOWN;
574
575     ret = this->setRequest(id, role, area, task, &req_num);
576
577     //vector<WMLayerState> current_states = this->lc->getCurrentStates();
578     // ret = this->setRequest(id, role, area, task, current_states, &req_num);
579
580     if(ret != WMError::SUCCESS)
581     {
582         HMI_ERROR(errorDescription(ret));
583         reply("Failed to set request");
584         return;
585     }
586
587     reply(nullptr);
588     if (req_num != g_app_list.currentRequestNumber())
589     {
590         // Add request, then invoked after the previous task is finished
591         HMI_SEQ_DEBUG(req_num, "request is accepted");
592         return;
593     }
594
595     /*
596      * Do allocate tasks
597      */
598     int i_ret = this->wmcon.sendRequest("activateWindow", appid,
599                                         drawing_name, drawing_area);
600     if (0 > i_ret)
601     {
602         //this->emit_error()
603         HMI_SEQ_ERROR(req_num, errorDescription(ret));
604         g_app_list.removeRequest(req_num);
605         this->processNextRequest();
606     }
607
608     this->setTimer();
609 }
610
611 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
612                                  const reply_func &reply)
613 {
614     // TODO: application requests by old role,
615     //       so convert role old to new
616     const char *c_role = this->convertRoleOldToNew(drawing_name);
617
618     /*
619     * Check Phase
620     */
621     string id = appid;
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;
627
628     ret = this->setRequest(id, role, area, task, &req_num);
629
630     if (ret != WMError::SUCCESS)
631     {
632         HMI_ERROR(errorDescription(ret));
633         reply("Failed to set request");
634         return;
635     }
636
637     reply(nullptr);
638     if (req_num != g_app_list.currentRequestNumber())
639     {
640         // Add request, then invoked after the previous task is finished
641         HMI_SEQ_DEBUG(req_num, "request is accepted");
642         return;
643     }
644
645     /*
646     * Do allocate tasks
647     */
648     ret = this->checkPolicy(req_num);
649
650     if (ret != WMError::SUCCESS)
651     {
652         //this->emit_error()
653         HMI_SEQ_ERROR(req_num, errorDescription(ret));
654         g_app_list.removeRequest(req_num);
655         this->processNextRequest();
656     }
657 }
658
659 void WindowManager::api_deactivate_surface_for_slave(char const *appid, char const *drawing_name,
660                                  const reply_func &reply)
661 {
662     // TODO: application requests by old role,
663     //       so convert role old to new
664     const char *c_role = this->convertRoleOldToNew(drawing_name);
665
666     /*
667     * Check Phase
668     */
669     string id = appid;
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;
675
676     ret = this->setRequest(id, role, area, task, &req_num);
677
678     if (ret != WMError::SUCCESS)
679     {
680         HMI_ERROR(errorDescription(ret));
681         reply("Failed to set request");
682         return;
683     }
684
685     reply(nullptr);
686     if (req_num != g_app_list.currentRequestNumber())
687     {
688         // Add request, then invoked after the previous task is finished
689         HMI_SEQ_DEBUG(req_num, "request is accepted");
690         return;
691     }
692
693     /*
694     * Do allocate tasks
695     */
696     ret = this->checkPolicyForSlave(req_num);
697
698     if (ret != WMError::SUCCESS)
699     {
700         //this->emit_error()
701         HMI_SEQ_ERROR(req_num, errorDescription(ret));
702         g_app_list.removeRequest(req_num);
703         this->processNextRequest();
704     }
705 }
706
707 void WindowManager::api_deactivate_surface_to_master(char const *appid, char const *drawing_name,
708                                  const reply_func &reply)
709 {
710     // TODO: application requests by old role,
711     //       so convert role old to new
712     const char *c_role = this->convertRoleOldToNew(drawing_name);
713
714     /*
715     * Check Phase
716     */
717     string id = appid;
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;
723
724     ret = this->setRequest(id, role, area, task, &req_num);
725
726     if (ret != WMError::SUCCESS)
727     {
728         HMI_ERROR(errorDescription(ret));
729         reply("Failed to set request");
730         return;
731     }
732
733     reply(nullptr);
734     if (req_num != g_app_list.currentRequestNumber())
735     {
736         // Add request, then invoked after the previous task is finished
737         HMI_SEQ_DEBUG(req_num, "request is accepted");
738         return;
739     }
740
741     /*
742     * Do allocate tasks
743     */
744     int i_ret = this->wmcon.sendRequest("deactivateWindow", appid,
745                                         drawing_name, "");
746     if (0 > i_ret)
747     {
748         //this->emit_error()
749         HMI_SEQ_ERROR(req_num, errorDescription(ret));
750         g_app_list.removeRequest(req_num);
751         this->processNextRequest();
752     }
753
754     this->setTimer();
755 }
756
757 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
758 {
759     // TODO: application requests by old role,
760     //       so convert role old to new
761     const char *c_role = this->convertRoleOldToNew(drawing_name);
762
763     string id = appid;
764     string role = c_role;
765     unsigned current_req = g_app_list.currentRequestNumber();
766     bool result = g_app_list.setEndDrawFinished(current_req, id, role);
767
768     if (!result)
769     {
770         HMI_ERROR("%s is not in transition state", id.c_str());
771         return;
772     }
773
774     if (g_app_list.endDrawFullfilled(current_req))
775     {
776         // do task for endDraw
777         this->stopTimer();
778         WMError ret = this->doEndDraw(current_req);
779
780         if(ret != WMError::SUCCESS)
781         {
782             //this->emit_error();
783
784             // Undo state of PolicyManager
785             this->pmw.undoState();
786             this->lc->undoUpdate();
787         }
788         this->emitScreenUpdated(current_req);
789         HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
790
791         g_app_list.removeRequest(current_req);
792
793         this->processNextRequest();
794     }
795     else
796     {
797         HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
798         return;
799     }
800 }
801
802 void WindowManager::api_enddraw_for_remote(char const *appid, char const *drawing_name)
803 {
804     int ret = this->wmcon.sendRequest("endDraw", appid, drawing_name, "");
805     if (0 > ret)
806     {
807         //this->emit_error()
808
809         this->pmw.undoState();
810         this->lc->undoUpdate();
811
812         unsigned current_req = g_app_list.currentRequestNumber();
813         g_app_list.removeRequest(current_req);
814         this->processNextRequest();
815
816         return;
817     }
818
819     this->api_enddraw(appid, drawing_name);
820 }
821
822 bool WindowManager::api_client_set_render_order(char const* appid, const vector<string>& render_order)
823 {
824     bool ret = false;
825     string id = appid;
826     auto client = g_app_list.lookUpClient(id);
827     if(client)
828     {
829         client->setRenderOrder(render_order);
830     }
831     return ret;
832 }
833
834 string WindowManager::api_client_attach_service_surface
835     (const char* appid, const char* dest, const char* service_surface)
836 {
837     string uuid, s_dest = dest;
838     auto client = g_app_list.lookUpClient(s_dest);
839     if(!client)
840     {
841         HMI_ERROR("Failed to look up destination [%s]", dest);
842         return uuid;
843     }
844     uuid = client->attachTmpServiceSurface(appid, service_surface);
845     this->tmp_services.emplace_back(TmpService{appid, dest, service_surface, uuid});
846     return uuid;
847 }
848
849 result<json_object *> WindowManager::api_get_display_info()
850 {
851     Screen screen = this->lc->getScreenInfo();
852
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()));
856     // TODO: set size
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()));
860
861     return Ok<json_object *>(object);
862 }
863
864 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
865 {
866     HMI_DEBUG("called");
867
868     // TODO: application requests by old role,
869     //       so convert role old to new
870     const char *role = this->convertRoleOldToNew(drawing_name);
871
872     // Check drawing name, surface/layer id
873     auto const &surface_id = this->id_alloc.lookup(string(role));
874     if (!surface_id)
875     {
876         return Err<json_object *>("Surface does not exist");
877     }
878
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));
886
887     return Ok<json_object *>(object);
888 }
889
890 result<json_object *> WindowManager::api_get_car_info(char const *label)
891 {
892     json_object *j_in  = nullptr;
893     json_object *j_out = nullptr;
894
895     if (0 == strcmp("parking_brake_status", label))
896     {
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);
900     }
901     else if (0 == strcmp("accelerator.pedal.position", label))
902     {
903         // Get accelerator pedal position
904         double val = this->crr_car_info.accel_pedal_pos;
905         j_in = json_object_new_double(val);
906     }
907     else if (0 == strcmp("car_state", label))
908     {
909         // Get running state
910         const char* val = (this->crr_car_info.running_stt) ? "run" : "stop";
911         j_in = json_object_new_string(val);
912     }
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);
917     }
918     else
919     {
920        return Err<json_object *>("Car info does not exist");
921     }
922
923     // Create output object
924     j_out = json_object_new_object();
925     json_object_object_add(j_out, "value", j_in);
926
927     return Ok<json_object *>(j_out);
928 }
929
930 void WindowManager::send_event(char const *evname)
931 {
932     HMI_DEBUG("%s: %s", __func__, evname);
933
934     int ret = afb_event_push(this->map_afb_event[evname], nullptr);
935     if (ret != 0)
936     {
937         HMI_DEBUG("afb_event_push: %m");
938     }
939 }
940
941 void WindowManager::send_event(char const *evname, char const *label)
942 {
943     HMI_DEBUG("%s: %s(%s)", __func__, evname, label);
944
945     json_object *j = json_object_new_object();
946     json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
947
948     int ret = afb_event_push(this->map_afb_event[evname], j);
949     if (ret != 0)
950     {
951         HMI_DEBUG("afb_event_push failed: %m");
952     }
953 }
954
955 void WindowManager::send_event(char const *evname, char const *label, char const *area,
956                      int x, int y, int w, int h)
957 {
958     HMI_DEBUG("%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
959               __func__, evname, label, area, x, y, w, h);
960
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));
966
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);
971
972     int ret = afb_event_push(this->map_afb_event[evname], j);
973     if (ret != 0)
974     {
975         HMI_DEBUG("afb_event_push failed: %m");
976     }
977 }
978
979 string WindowManager::searchApp(unsigned pid, unsigned ppid, unsigned surface, json_object* resp)
980 {
981     // retrieve appid from pid from application manager
982     string appid;
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++)
988     {
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))
994         {
995             string s_id = id;
996             s_id.erase(s_id.find('@'));
997             appid = s_id;
998             HMI_INFO("App found %s", appid.c_str());
999             break;
1000         }
1001     }
1002     if(appid.empty())
1003     {
1004         HMI_WARNING("Failed to retrieve id");
1005     }
1006     return appid;
1007 }
1008
1009 void WindowManager::storeSurface(const string& appid, unsigned ppid, unsigned surface)
1010 {
1011     auto elem = std::find_if(this->tmp_services.begin(), this->tmp_services.end(),
1012                 [&appid](TmpService& ts){
1013                     return (ts.dest == appid );
1014                 });
1015
1016     this->lc->setXDGSurfaceOriginSize(surface);
1017     if(elem != this->tmp_services.end())
1018     {
1019         // attachApp
1020         auto client = g_app_list.lookUpClient(elem->dest);
1021         if(client == nullptr)
1022         {
1023             return;
1024         }
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);
1027     }
1028     else
1029     {
1030         // setRole
1031         auto client = g_app_list.lookUpClient(appid);
1032         if(client != nullptr)
1033         {
1034             client->addSurface(surface);
1035             this->id_alloc.register_name_id(client->role(), surface);
1036         }
1037         else
1038         {
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});
1042         }
1043     }
1044 }
1045
1046 /**
1047  * proxied events
1048  */
1049 void WindowManager::surface_created(unsigned pid, unsigned surface_id)
1050 {
1051     // requestSurface
1052     if(this->tmp_surface2app.count(surface_id) != 0)
1053     {
1054         string appid = this->tmp_surface2app[surface_id].appid;
1055         auto client = g_app_list.lookUpClient(appid);
1056         if(client != nullptr)
1057         {
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)
1061             {
1062                 HMI_ERROR("Failed to add surface to client %s", client->appID().c_str());
1063             }
1064         }
1065         this->tmp_surface2app.erase(surface_id);
1066     }
1067     else
1068     {
1069         HMI_NOTICE("Unknown surface %d", surface_id);
1070         // retrieve ppid
1071         std::ostringstream os;
1072         os << pid ;
1073         string path = "/proc/" + os.str() + "/stat";
1074         std::ifstream ifs(path.c_str());
1075         string str;
1076         unsigned ppid = 0;
1077         if(!ifs.fail() && std::getline(ifs, str))
1078         {
1079             std::sscanf(str.data(), "%*d %*s %*c %d", &ppid);
1080             HMI_INFO("Retrieve ppid %d", ppid);
1081         }
1082         else
1083         {
1084             HMI_ERROR("Failed to open /proc/%d/stat", pid);
1085             HMI_ERROR("File system may be different");
1086             return;
1087         }
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);
1094                 HMI_DEBUG("check");
1095                 if(stat != 0)
1096                 {
1097                     HMI_ERROR("Failed to call runners");
1098                 }
1099                 else
1100                 {
1101                     json_object* j;
1102                     json_object_object_get_ex(resp, "response", &j);
1103                     string appid = g_context->searchApp(c->pid, c->ppid, c->surface, j);
1104                     if(!appid.empty())
1105                     {
1106                         g_context->storeSurface(appid, c->ppid, c->surface);
1107                     }
1108                 }
1109                 json_object_put(resp);
1110                 delete c;
1111             }, c);
1112     }
1113 }
1114
1115 void WindowManager::surface_removed(unsigned surface_id)
1116 {
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);
1121 }
1122
1123 void WindowManager::removeClient(const string &appid)
1124 {
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);
1129 }
1130
1131 void WindowManager::exceptionProcessForTransition()
1132 {
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();
1138 }
1139
1140 void WindowManager::analyzeReceivedEvent(const char *event, struct json_object *object)
1141 {
1142     HMI_DEBUG("event:%s", event);
1143
1144     // If receive low can signal
1145     if (strstr(event, "low-can"))
1146     {
1147         // Analyze low can signal
1148         const char *signal_name = this->lcc.analyzeCanSignal(object);
1149
1150         // Create task for car state and input it to PolicyManager
1151         Task task = this->convertCanSignalToCarStateTask(signal_name);
1152         if (Task::TASK_INVALID != task)
1153         {
1154             this->inputCarStateTask(task);
1155         }
1156     }
1157 }
1158
1159 void WindowManager::timerHandler()
1160 {
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();
1166 }
1167
1168 void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
1169 {
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;
1174
1175     if (actions.empty())
1176     {
1177         if (g_app_list.haveRequest())
1178         {
1179             HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
1180             goto proc_remove_request;
1181         }
1182         else
1183         {
1184             HMI_SEQ_DEBUG(req_num, "There is no request");
1185             return;
1186         }
1187     }
1188
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)
1194     {
1195         if (TaskCarState::RESTRICTION_MODE_OFF == act.car_state)
1196         {
1197             task = Task::TASK_RESTRICTION_MODE_OFF;
1198             break;
1199         }
1200         else if (TaskCarState::RESTRICTION_MODE_ON == act.car_state)
1201         {
1202             task = Task::TASK_RESTRICTION_MODE_ON;
1203             break;
1204         }
1205     }
1206
1207     // If there is the request-less task, set request here
1208     if (Task::TASK_INVALID != task) {
1209         unsigned req_num;
1210         ret = this->setRequest(task, &req_num);
1211
1212         if(ret != WMError::SUCCESS)
1213         {
1214             goto error;
1215         }
1216     }
1217
1218     for (auto &act : actions)
1219     {
1220         if ("" != act.role)
1221         {
1222             bool found;
1223             auto const &surface_id = this->id_alloc.lookup(act.role.c_str());
1224             if(surface_id == nullopt)
1225             {
1226                 HMI_SEQ_DEBUG(req_num, "There is not surface id for role:%s", act.role.c_str());
1227                 continue;
1228             }
1229
1230             std::string appid = g_app_list.getAppID(*surface_id, &found);
1231             if (!found)
1232             {
1233                 if (TaskVisible::INVISIBLE == act.visible)
1234                 {
1235                     HMI_SEQ_DEBUG(req_num, "role:%s is killed, so do not set this action", act.role.c_str());
1236                     continue;
1237                 }
1238                 else
1239                 {
1240                     HMI_SEQ_ERROR(req_num, "appid of role:%s which is visible is not found", act.role.c_str());
1241                     ret = WMError::FAIL;
1242                     goto error;
1243                 }
1244             }
1245             auto client = g_app_list.lookUpClient(appid);
1246             act.req_num = req_num;
1247             act.client = client;
1248
1249             // If Window Manager is master and this action is for slave,
1250             // change TaskVisible
1251             if (this->wmcon.isMasterMode())
1252             {
1253                 if (this->wmcon.isMasterArea(act.area.c_str()))
1254                 {
1255                     HMI_DEBUG("Set TaskVisible::REQ_REMOTE_VISIBLE");
1256                     act.visible = TaskVisible::REQ_REMOTE_VISIBLE;
1257                 }
1258                 // TODO: Check whether role is tbtnavi to request remote invisible
1259                 else if (("tbtnavi" == act.role) &&
1260                          (TaskVisible::INVISIBLE == act.visible))
1261                 {
1262                     HMI_DEBUG("Set TaskVisible::REQ_REMOTE_INVISIBLE");
1263                     act.visible = TaskVisible::REQ_REMOTE_INVISIBLE;
1264                 }
1265             }
1266         }
1267
1268         ret = g_app_list.setAction(req_num, act);
1269         if (ret != WMError::SUCCESS)
1270         {
1271             HMI_SEQ_ERROR(req_num, "Setting action is failed");
1272             goto error;
1273         }
1274     }
1275
1276     HMI_SEQ_DEBUG(req_num, "Start transition.");
1277     ret = this->startTransition(req_num);
1278     if (ret != WMError::SUCCESS)
1279     {
1280         if (ret == WMError::NO_LAYOUT_CHANGE)
1281         {
1282             goto proc_remove_request;
1283         }
1284         else
1285         {
1286             HMI_SEQ_ERROR(req_num, "Transition state is failed");
1287             goto error;
1288         }
1289     }
1290
1291     return;
1292
1293 error:
1294     //this->emit_error()
1295     HMI_SEQ_ERROR(req_num, errorDescription(ret));
1296     this->pmw.undoState();
1297
1298 proc_remove_request:
1299     g_app_list.removeRequest(req_num);
1300     this->processNextRequest();
1301 }
1302
1303 void WindowManager::processError(WMError error)
1304 {
1305     unsigned req_num = g_app_list.currentRequestNumber();
1306
1307     //this->emit_error()
1308     HMI_SEQ_ERROR(req_num, errorDescription(error));
1309     g_app_list.removeRequest(req_num);
1310     this->processNextRequest();
1311 }
1312
1313 void WindowManager::processForRemoteRequest(json_object *data)
1314 {
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");
1319
1320     if (!req || !drawing_name)
1321     {
1322         HMI_ERROR("Parse Error!!");
1323         return;
1324     }
1325
1326     if (this->wmcon.isMasterMode())
1327     {
1328         if (!appid)
1329         {
1330             HMI_ERROR("Parse Error!!");
1331             return;
1332         }
1333
1334         auto reply = [](const char *errmsg) {
1335             if (errmsg != nullptr)
1336             {
1337                 HMI_ERROR(errmsg);
1338                 return;
1339             }
1340         };
1341
1342         if ("activateWindow" == std::string(req))
1343         {
1344             if (!drawing_area)
1345             {
1346                 HMI_ERROR("Parse Error!!");
1347                 return;
1348             }
1349
1350             this->api_activate_surface_for_slave(
1351                 appid, drawing_name, drawing_area, reply);
1352         }
1353         else if ("deactivateWindow" == std::string(req))
1354         {
1355             this->api_deactivate_surface_for_slave(
1356                 appid, drawing_name, reply);
1357         }
1358         else if ("endDraw" == std::string(req))
1359         {
1360             this->api_enddraw(appid, drawing_name);
1361         }
1362     }
1363     else
1364     {
1365         if ("syncDraw" == std::string(req))
1366         {
1367             this->stopTimer();
1368
1369             if (!appid || !drawing_area)
1370             {
1371                 HMI_ERROR("Parse Error!!");
1372                 return;
1373             }
1374
1375             unsigned req_num = g_app_list.currentRequestNumber();
1376             auto client = g_app_list.lookUpClient(appid);
1377
1378             // TODO: application requests by old role,
1379             //       so convert role old to new
1380             const char *c_role = this->convertRoleOldToNew(drawing_name);
1381
1382             // Create action
1383             bool end_draw_finished = false;
1384             WMAction act
1385             {
1386                 req_num,
1387                 client,
1388                 string(c_role),
1389                 string(drawing_area),
1390                 TaskVisible::REMOTE_VISIBLE,
1391                 end_draw_finished,
1392                 TaskCarState::NO_TASK
1393             };
1394
1395             // Set action
1396             WMError ret = g_app_list.setAction(req_num, act);
1397             if (ret != WMError::SUCCESS)
1398             {
1399                 HMI_SEQ_ERROR(req_num, "Setting action is failed");
1400                 return;
1401             }
1402
1403             this->emit_syncdraw(string(drawing_name), string(drawing_area));
1404             this->wmcon.startSyncDrawForRemote(appid);
1405             this->setTimer();
1406         }
1407         else if ("activated" == std::string(req))
1408         {
1409             this->emit_visible(drawing_name);
1410             this->emit_activated(drawing_name);
1411         }
1412         else if ("deactivated" == std::string(req))
1413         {
1414             this->stopTimer();
1415
1416             if (!appid || !drawing_area)
1417             {
1418                 HMI_ERROR("Parse Error!!");
1419                 return;
1420             }
1421
1422             unsigned req_num = g_app_list.currentRequestNumber();
1423             auto client = g_app_list.lookUpClient(appid);
1424
1425             // TODO: application requests by old role,
1426             //       so convert role old to new
1427             const char *c_role = this->convertRoleOldToNew(drawing_name);
1428
1429             // Create action
1430             bool end_draw_finished = true;
1431             WMAction act
1432             {
1433                 req_num,
1434                 client,
1435                 string(c_role),
1436                 "",
1437                 TaskVisible::REMOTE_INVISIBLE,
1438                 end_draw_finished,
1439                 TaskCarState::NO_TASK
1440             };
1441
1442             this->lc->visibilityChange(act);
1443             this->lc->renderLayers();
1444             this->lc->renderLayersRemote();
1445
1446             this->emit_invisible(drawing_name);
1447             this->emit_deactivated(drawing_name);
1448             this->emitScreenUpdated(req_num);
1449
1450             g_app_list.removeRequest(req_num);
1451             this->processNextRequest();
1452         }
1453         else if ("flushDraw" == std::string(req))
1454         {
1455             this->emit_flushdraw(drawing_name);
1456         }
1457     }
1458 }
1459
1460 /*
1461  ******* Private Functions *******
1462  */
1463
1464 void WindowManager::emit_activated(char const *label)
1465 {
1466     this->send_event(kListEventName[Event_Active], label);
1467 }
1468
1469 void WindowManager::emit_deactivated(char const *label)
1470 {
1471     this->send_event(kListEventName[Event_Inactive], label);
1472 }
1473
1474 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1475 {
1476     this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1477 }
1478
1479 void WindowManager::emit_syncdraw(const string &role, const string &area)
1480 {
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);
1484 }
1485
1486 void WindowManager::emit_flushdraw(char const *label)
1487 {
1488     this->send_event(kListEventName[Event_FlushDraw], label);
1489 }
1490
1491 void WindowManager::emit_visible(char const *label, bool is_visible)
1492 {
1493     this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1494 }
1495
1496 void WindowManager::emit_invisible(char const *label)
1497 {
1498     return emit_visible(label, false);
1499 }
1500
1501 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
1502
1503 void WindowManager::emitHeadlampOff()
1504 {
1505     // Send HeadlampOff event for all application
1506     this->send_event(kListEventName[Event_HeadlampOff]);
1507 }
1508
1509 void WindowManager::emitHeadlampOn()
1510 {
1511     // Send HeadlampOn event for all application
1512     this->send_event(kListEventName[Event_HeadlampOn]);
1513 }
1514
1515 void WindowManager::emitParkingBrakeOff()
1516 {
1517     // Send ParkingBrakeOff event for all application
1518     this->send_event(kListEventName[Event_ParkingBrakeOff]);
1519 }
1520
1521 void WindowManager::emitParkingBrakeOn()
1522 {
1523     // Send ParkingBrakeOn event for all application
1524     this->send_event(kListEventName[Event_ParkingBrakeOn]);
1525 }
1526
1527 void WindowManager::emitLightstatusBrakeOff()
1528 {
1529     // Send LightstatusBrakeOff event for all application
1530     this->send_event(kListEventName[Event_LightstatusBrakeOff]);
1531 }
1532
1533 void WindowManager::emitLightstatusBrakeOn()
1534 {
1535     // Send LightstatusBrakeOn event for all application
1536     this->send_event(kListEventName[Event_LightstatusBrakeOn]);
1537 }
1538
1539 void WindowManager::emitCarStop()
1540 {
1541     // Send CarStop event for all application
1542     this->send_event(kListEventName[Event_CarStop]);
1543 }
1544
1545 void WindowManager::emitCarRun()
1546 {
1547     // Send CarRun event for all application
1548     this->send_event(kListEventName[Event_CarRun]);
1549 }
1550
1551 WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
1552                             Task task, unsigned* req_num)
1553 {
1554     if (!g_app_list.contains(appid))
1555     {
1556         return WMError::NOT_REGISTERED;
1557     }
1558
1559     auto client = g_app_list.lookUpClient(appid);
1560
1561     /*
1562      * Queueing Phase
1563      */
1564     unsigned current = g_app_list.currentRequestNumber();
1565     unsigned requested_num = g_app_list.getRequestNumber(appid);
1566     if (requested_num != 0)
1567     {
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;
1571     }
1572
1573     WMRequest req = WMRequest(appid, role, area, task);
1574     unsigned new_req = g_app_list.addRequest(req);
1575     *req_num = new_req;
1576     g_app_list.reqDump();
1577
1578     HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1579
1580     return WMError::SUCCESS;
1581 }
1582
1583 WMError WindowManager::setRequest(Task task, unsigned* req_num)
1584 {
1585     /*
1586      * Queueing Phase
1587      */
1588     unsigned current = g_app_list.currentRequestNumber();
1589
1590     WMRequest req = WMRequest(task);
1591     unsigned new_req = g_app_list.addRequest(req);
1592     *req_num = new_req;
1593     g_app_list.reqDump();
1594
1595     HMI_SEQ_DEBUG(current, "start sequence for task:%d", task);
1596
1597     return WMError::SUCCESS;
1598 }
1599
1600 WMError WindowManager::setRequestForSlave(const string& appid, const string &role, const string &area,
1601                             Task task, unsigned* req_num)
1602 {
1603     /*
1604      * Queueing Phase
1605      */
1606     unsigned current = g_app_list.currentRequestNumber();
1607     unsigned requested_num = g_app_list.getRequestNumber(appid);
1608     if (requested_num != 0)
1609     {
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;
1613     }
1614
1615     WMRequest req = WMRequest(appid, role, area, task);
1616     unsigned new_req = g_app_list.addRequest(req);
1617     *req_num = new_req;
1618     g_app_list.reqDump();
1619
1620     HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1621
1622     return WMError::SUCCESS;
1623 }
1624
1625 WMError WindowManager::checkPolicy(unsigned req_num)
1626 {
1627     /*
1628     * Check Policy
1629     */
1630     // get current trigger
1631     bool found = false;
1632     WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1633     auto trigger = g_app_list.getRequest(req_num, &found);
1634     if (!found)
1635     {
1636         ret = WMError::NO_ENTRY;
1637         return ret;
1638     }
1639     string req_area = trigger.area;
1640
1641     if (trigger.task == Task::TASK_ALLOCATE)
1642     {
1643         const char *msg = this->check_surface_exist(trigger.role.c_str());
1644
1645         if (msg)
1646         {
1647             HMI_SEQ_ERROR(req_num, msg);
1648             return ret;
1649         }
1650     }
1651
1652     // Input event data to PolicyManager
1653     if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1654     {
1655         HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1656         return ret;
1657     }
1658
1659     // Execute state transition of PolicyManager
1660     if (0 > this->pmw.executeStateTransition())
1661     {
1662         HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1663         return ret;
1664     }
1665
1666     ret = WMError::SUCCESS;
1667
1668     g_app_list.reqDump();
1669
1670     return ret;
1671 }
1672
1673 WMError WindowManager::checkPolicyForSlave(unsigned req_num)
1674 {
1675     /*
1676     * Check Policy
1677     */
1678     // get current trigger
1679     bool found = false;
1680     WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1681     auto trigger = g_app_list.getRequest(req_num, &found);
1682     if (!found)
1683     {
1684         ret = WMError::NO_ENTRY;
1685         return ret;
1686     }
1687     string req_area = trigger.area;
1688
1689     // Input event data to PolicyManager
1690     if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1691     {
1692         HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1693         return ret;
1694     }
1695
1696     // Execute state transition of PolicyManager
1697     if (0 > this->pmw.executeStateTransition())
1698     {
1699         HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1700         return ret;
1701     }
1702
1703     ret = WMError::SUCCESS;
1704
1705     g_app_list.reqDump();
1706
1707     return ret;
1708 }
1709
1710 WMError WindowManager::startTransition(unsigned req_num)
1711 {
1712     bool sync_draw_happen = false;
1713     bool found = false;
1714     WMError ret = WMError::SUCCESS;
1715     auto actions = g_app_list.getActions(req_num, &found);
1716     if (!found)
1717     {
1718         ret = WMError::NO_ENTRY;
1719         HMI_SEQ_ERROR(req_num,
1720             "Window Manager bug :%s : Action is not set", errorDescription(ret));
1721         return ret;
1722     }
1723
1724     g_app_list.reqDump();
1725     for (const auto &action : actions)
1726     {
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];
1730
1731         if (action.visible == TaskVisible::VISIBLE)
1732         {
1733             sync_draw_happen = true;
1734
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); */
1739         }
1740         else if(action.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1741         {
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());
1745         }
1746         else if (action.car_state != TaskCarState::NO_TASK)
1747         {
1748             this->transitionCarState(action.car_state);
1749         }
1750     }
1751
1752     if (sync_draw_happen)
1753     {
1754         this->setTimer();
1755     }
1756     else
1757     {
1758         // deactivate only, no syncDraw
1759         // Make it deactivate here
1760         for (const auto &x : actions)
1761         {
1762             this->lc->visibilityChange(x);
1763             string old_role = this->rolenew2old[x.role];
1764
1765             if (x.visible == TaskVisible::INVISIBLE)
1766             {
1767                 emit_deactivated(old_role.c_str());
1768             }
1769             else if (x.visible == TaskVisible::REQ_REMOTE_INVISIBLE)
1770             {
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(), "");
1774                 if (0 > i_ret)
1775                 {
1776                     ret = WMError::FAIL;
1777                 }
1778             }
1779
1780             /* if (g_app_list.contains(x.appid))
1781             {
1782                 auto client = g_app_list.lookUpClient(x.appid);
1783                 //this->deactivate(client->surfaceID(x.role));
1784             } */
1785         }
1786         this->lc->renderLayers();
1787         this->lc->renderLayersRemote();
1788         ret = WMError::NO_LAYOUT_CHANGE;
1789     }
1790     return ret;
1791 }
1792
1793 void WindowManager::transitionCarState(TaskCarState task)
1794 {
1795     if (TaskCarState::PARKING_BRAKE_OFF == task)
1796     {
1797         this->crr_car_info.parking_brake_stt = false;
1798         this->emitParkingBrakeOff();
1799     }
1800     else if (TaskCarState::PARKING_BRAKE_ON == task)
1801     {
1802         this->crr_car_info.parking_brake_stt = true;
1803         this->emitParkingBrakeOn();
1804     }
1805     else if (TaskCarState::ACCEL_PEDAL_OFF == task)
1806     {
1807         this->crr_car_info.accel_pedal_stt = false;
1808     }
1809     else if (TaskCarState::ACCEL_PEDAL_ON == task)
1810     {
1811         this->crr_car_info.accel_pedal_stt = true;
1812     }
1813     else if (TaskCarState::HEDLAMP_OFF == task)
1814     {
1815         this->crr_car_info.headlamp_stt = false;
1816         this->emitHeadlampOff();
1817     }
1818     else if (TaskCarState::HEDLAMP_ON == task)
1819     {
1820         this->crr_car_info.headlamp_stt = true;
1821         this->emitHeadlampOn();
1822     }
1823     else if (TaskCarState::LIGHTSTATUS_BRAKE_OFF == task)
1824     {
1825         this->crr_car_info.lightstatus_brake_stt = false;
1826         this->emitLightstatusBrakeOff();
1827     }
1828     else if (TaskCarState::LIGHTSTATUS_BRAKE_ON == task)
1829     {
1830         this->crr_car_info.lightstatus_brake_stt = true;
1831         this->emitLightstatusBrakeOn();
1832     }
1833     else if (TaskCarState::CAR_STOP == task)
1834     {
1835         this->crr_car_info.running_stt = false;
1836         this->emitCarStop();
1837     }
1838     else if (TaskCarState::CAR_RUN == task)
1839     {
1840         this->crr_car_info.running_stt = true;
1841         this->emitCarRun();
1842     }
1843 }
1844
1845 WMError WindowManager::doEndDraw(unsigned req_num)
1846 {
1847     // get actions
1848     bool found;
1849     auto actions = g_app_list.getActions(req_num, &found);
1850     WMError ret = WMError::SUCCESS;
1851     if (!found)
1852     {
1853         ret = WMError::NO_ENTRY;
1854         return ret;
1855     }
1856
1857     HMI_SEQ_INFO(req_num, "do endDraw");
1858
1859     // layout change and make it visible
1860     for (const auto &act : actions)
1861     {
1862         if(act.visible != TaskVisible::NO_CHANGE)
1863         {
1864             // layout change
1865             ret = this->lc->layoutChange(act);
1866             if(ret != WMError::SUCCESS)
1867             {
1868                 HMI_SEQ_WARNING(req_num,
1869                     "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1870                 return ret;
1871             }
1872             ret = this->lc->visibilityChange(act);
1873
1874             // Emit active/deactive event
1875             string old_role = this->rolenew2old[act.role];
1876             if(act.visible == TaskVisible::VISIBLE)
1877             {
1878                 emit_visible(old_role.c_str());
1879                 emit_activated(old_role.c_str());
1880             }
1881             else if(act.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1882             {
1883                 // If this action is for slave, send to slave
1884                 int i_ret = this->wmcon.sendRequest("activated", "", old_role.c_str(), "");
1885                 if (0 > i_ret)
1886                 {
1887                     ret = WMError::FAIL;
1888                 }
1889             }
1890             else if(act.visible == TaskVisible::REQ_REMOTE_INVISIBLE)
1891             {
1892                 // If this action is for slave, send to slave
1893                 int i_ret = this->wmcon.sendRequest("deactivated", "", old_role.c_str(), "");
1894                 if (0 > i_ret)
1895                 {
1896                     ret = WMError::FAIL;
1897                 }
1898             }
1899             else if((act.visible == TaskVisible::REMOTE_VISIBLE) ||
1900                     (act.visible == TaskVisible::REMOTE_INVISIBLE))
1901             {
1902                 // nop because emit active/deactive by event from remote
1903             }
1904             else
1905             {
1906                 emit_invisible(old_role.c_str());
1907                 emit_deactivated(old_role.c_str());
1908             }
1909
1910             if (ret != WMError::SUCCESS)
1911             {
1912                 HMI_SEQ_WARNING(req_num,
1913                     "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1914                 return ret;
1915             }
1916             HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1917         }
1918     }
1919     this->lc->renderLayers();
1920     this->lc->renderLayersRemote();
1921
1922     HMI_SEQ_INFO(req_num, "emit flushDraw");
1923
1924     for(const auto &act_flush : actions)
1925     {
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];
1929
1930         if(act_flush.visible == TaskVisible::VISIBLE)
1931         {
1932             this->emit_flushdraw(old_role.c_str());
1933         }
1934         else if(act_flush.visible == TaskVisible::REQ_REMOTE_VISIBLE)
1935         {
1936             // If this action is for slave, send to slave
1937             this->wmcon.sendRequest("flushDraw", "", old_role.c_str(), "");
1938         }
1939     }
1940
1941     return ret;
1942 }
1943
1944 void WindowManager::emitScreenUpdated(unsigned req_num)
1945 {
1946     // Get visible apps
1947     HMI_SEQ_DEBUG(req_num, "emit screen updated");
1948     bool found = false;
1949     auto actions = g_app_list.getActions(req_num, &found);
1950
1951     HMI_DEBUG("@@@@@");
1952     // create json object
1953     json_object *j = json_object_new_object();
1954     json_object *jarray = json_object_new_array();
1955
1956     for(const auto& action: actions)
1957     {
1958         if((action.visible == TaskVisible::VISIBLE) ||
1959            (action.visible == TaskVisible::REMOTE_VISIBLE))
1960         {
1961             json_object_array_add(jarray, json_object_new_string(action.client->appID().c_str()));
1962         }
1963     }
1964     json_object_object_add(j, kKeyIds, jarray);
1965     HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1966
1967     int ret = afb_event_push(
1968         this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1969     if (ret != 0)
1970     {
1971         HMI_DEBUG("afb_event_push failed: %m");
1972     }
1973 }
1974
1975 void WindowManager::setTimer()
1976 {
1977     struct timespec ts;
1978     if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
1979         HMI_ERROR("Could't set time (clock_gettime() returns with error");
1980         return;
1981     }
1982
1983     HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1984     if (g_timer_ev_src == nullptr)
1985     {
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);
1989         if (ret < 0)
1990         {
1991             HMI_ERROR("Could't set timer");
1992         }
1993     }
1994     else
1995     {
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);
1999     }
2000 }
2001
2002 void WindowManager::stopTimer()
2003 {
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);
2007     if (rc < 0)
2008     {
2009         HMI_SEQ_ERROR(req_num, "Timer stop failed");
2010     }
2011 }
2012
2013 void WindowManager::processNextRequest()
2014 {
2015     g_app_list.next();
2016     g_app_list.reqDump();
2017     unsigned req_num = g_app_list.currentRequestNumber();
2018     if (g_app_list.haveRequest())
2019     {
2020         HMI_SEQ_DEBUG(req_num, "Process next request");
2021         WMError rc = checkPolicy(req_num);
2022         if (rc != WMError::SUCCESS)
2023         {
2024             HMI_SEQ_ERROR(req_num, errorDescription(rc));
2025         }
2026     }
2027     else
2028     {
2029         HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
2030     }
2031 }
2032
2033 const char* WindowManager::convertRoleOldToNew(char const *old_role)
2034 {
2035     const char *new_role = nullptr;
2036
2037     for (auto const &on : this->roleold2new)
2038     {
2039         std::regex regex = std::regex(on.first);
2040         if (std::regex_match(old_role, regex))
2041         {
2042             // role is old. So convert to new.
2043             new_role = on.second.c_str();
2044             break;
2045         }
2046     }
2047
2048     if (nullptr == new_role)
2049     {
2050         // role is new or fallback.
2051         new_role = old_role;
2052     }
2053
2054     HMI_DEBUG("old:%s -> new:%s", old_role, new_role);
2055
2056     return new_role;
2057 }
2058
2059 int WindowManager::loadOldRolesConfigFile()
2060 {
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);
2064
2065     string file_name;
2066     if (!afm_app_install_dir)
2067     {
2068         HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
2069     }
2070     else
2071     {
2072         file_name = string(afm_app_install_dir) + string(kPathOldRolesConfigFile);
2073     }
2074
2075     // Load old_rolea config file
2076     json_object* json_obj;
2077     int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
2078     if (0 > ret)
2079     {
2080         HMI_ERROR("Could not open %s, so use default old_roles information", kPathOldRolesConfigFile);
2081         json_obj = json_tokener_parse(kDefaultOldRolesConfig);
2082     }
2083     HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
2084
2085     // Perse apps
2086     json_object* json_cfg;
2087     if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
2088     {
2089         HMI_ERROR("Parse Error!!");
2090         return -1;
2091     }
2092
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));
2096
2097     for (int i=0; i<len; i++)
2098     {
2099         json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
2100
2101         const char* old_role = jh::getStringFromJson(json_tmp, "name");
2102         if (nullptr == old_role)
2103         {
2104             HMI_ERROR("Parse Error!!");
2105             return -1;
2106         }
2107
2108         const char* new_role = jh::getStringFromJson(json_tmp, "new");
2109         if (nullptr == new_role)
2110         {
2111             HMI_ERROR("Parse Error!!");
2112             return -1;
2113         }
2114
2115         this->roleold2new[old_role] = string(new_role);
2116     }
2117
2118     // Check
2119     for(auto itr = this->roleold2new.begin();
2120       itr != this->roleold2new.end(); ++itr)
2121     {
2122         HMI_DEBUG(">>> role old:%s new:%s",
2123                   itr->first.c_str(), itr->second.c_str());
2124     }
2125
2126     // Release json_object
2127     json_object_put(json_obj);
2128
2129     return 0;
2130 }
2131
2132 Task WindowManager::convertCanSignalToCarStateTask(const char *signal_name)
2133 {
2134     wm::LowCanClient *lcc = &(this->lcc);
2135     Task task = Task::TASK_INVALID;
2136
2137     // If car info is updated, set car state change event
2138     if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoParkingBrake]))
2139     {
2140         HMI_DEBUG("Parking Brake state is changed");
2141
2142         if (lcc->getCurrentParkingBrakeState())
2143         {
2144             task = wm::Task::TASK_PARKING_BRAKE_ON;
2145         }
2146         else
2147         {
2148             task = wm::Task::TASK_PARKING_BRAKE_OFF;
2149         }
2150     }
2151     else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoAccelPedalPos]))
2152     {
2153         // Update accel pedal position
2154         this->crr_car_info.accel_pedal_pos = lcc->getCurrentAccelPedalPosition();
2155
2156         if (lcc->isChangedAccelPedalState())
2157         {
2158             HMI_DEBUG("Accelerator Pedal state is changed");
2159
2160             if (lcc->getCurrentAccelPedalState())
2161             {
2162                 task = wm::Task::TASK_ACCEL_PEDAL_ON;
2163             }
2164             else
2165             {
2166                 task = wm::Task::TASK_ACCEL_PEDAL_OFF;
2167             }
2168         }
2169     }
2170     else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoHeadlame]))
2171     {
2172         HMI_DEBUG("Headlamp state is changed");
2173
2174         if (lcc->getCurrentHeadlampState())
2175         {
2176             task = wm::Task::TASK_HEDLAMP_ON;
2177         }
2178         else
2179         {
2180             task = wm::Task::TASK_HEDLAMP_OFF;
2181         }
2182     }
2183     else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoLightstatusBrake]))
2184     {
2185         HMI_DEBUG("Lightstatus Brake state is changed");
2186
2187         if (lcc->getCurrentLightstatusBrakeState())
2188         {
2189             task = wm::Task::TASK_LIGHTSTATUS_BRAKE_ON;
2190         }
2191         else
2192         {
2193             task = wm::Task::TASK_LIGHTSTATUS_BRAKE_OFF;
2194         }
2195     }
2196     return task;
2197 }
2198
2199 void WindowManager::inputCarStateTask(Task task)
2200 {
2201     unsigned req_num = 0;
2202     WMError ret = WMError::UNKNOWN;
2203
2204     ret = this->setRequest(task, &req_num);
2205
2206     if(ret != WMError::SUCCESS)
2207     {
2208         HMI_ERROR(errorDescription(ret));
2209         return;
2210     }
2211
2212     if (req_num != g_app_list.currentRequestNumber())
2213     {
2214         // Add request, then invoked after the previous task is finished
2215         HMI_SEQ_DEBUG(req_num, "request is accepted");
2216         return;
2217     }
2218
2219     /*
2220      * Do allocate tasks
2221      */
2222     ret = this->checkPolicy(req_num);
2223
2224     if (ret != WMError::SUCCESS)
2225     {
2226         //this->emit_error()
2227         HMI_SEQ_ERROR(req_num, errorDescription(ret));
2228         g_app_list.removeRequest(req_num);
2229         this->processNextRequest();
2230     }
2231 }
2232
2233 const char *WindowManager::check_surface_exist(const char *drawing_name)
2234 {
2235     auto const &surface_id = this->id_alloc.lookup(string(drawing_name));
2236     if (!surface_id)
2237     {
2238         return "Surface does not exist";
2239     }
2240
2241     /* if (!this->controller->surface_exists(*surface_id))
2242     {
2243         return "Surface does not exist in controller!";
2244     } */
2245
2246     /* auto layer_id = this->layers.get_layer_id(*surface_id);
2247
2248     if (!layer_id)
2249     {
2250         return "Surface is not on any layer!";
2251     } */
2252
2253     HMI_DEBUG("surface %d is detected", *surface_id);
2254     return nullptr;
2255 }
2256
2257 const char* WindowManager::kDefaultOldRolesConfig = "{ \
2258     \"old_roles\": [ \
2259         { \
2260             \"name\": \"HomeScreen\", \
2261             \"new\": \"homescreen\" \
2262         }, \
2263         { \
2264             \"name\": \"Music\", \
2265             \"new\": \"music\" \
2266         }, \
2267         { \
2268             \"name\": \"MediaPlayer\", \
2269             \"new\": \"music\" \
2270         }, \
2271         { \
2272             \"name\": \"Video\", \
2273             \"new\": \"video\" \
2274         }, \
2275         { \
2276             \"name\": \"VideoPlayer\", \
2277             \"new\": \"video\" \
2278         }, \
2279         { \
2280             \"name\": \"WebBrowser\", \
2281             \"new\": \"browser\" \
2282         }, \
2283         { \
2284             \"name\": \"Radio\", \
2285             \"new\": \"radio\" \
2286         }, \
2287         { \
2288             \"name\": \"Phone\", \
2289             \"new\": \"phone\" \
2290         }, \
2291         { \
2292             \"name\": \"Navigation\", \
2293             \"new\": \"map\" \
2294         }, \
2295         { \
2296             \"name\": \"HVAC\", \
2297             \"new\": \"hvac\" \
2298         }, \
2299         { \
2300             \"name\": \"Settings\", \
2301             \"new\": \"settings\" \
2302         }, \
2303         { \
2304             \"name\": \"Dashboard\", \
2305             \"new\": \"dashboard\" \
2306         }, \
2307         { \
2308             \"name\": \"POI\", \
2309             \"new\": \"poi\" \
2310         }, \
2311         { \
2312             \"name\": \"Mixer\", \
2313             \"new\": \"mixer\" \
2314         }, \
2315         { \
2316             \"name\": \"Restriction\", \
2317             \"new\": \"restriction\" \
2318         }, \
2319         { \
2320             \"name\": \"^OnScreen.*\", \
2321             \"new\": \"on_screen\" \
2322         } \
2323     ] \
2324 }";
2325
2326 } // namespace wm