Remove lookup_id and lookup_name
[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
20 #include "window_manager.hpp"
21 #include "json_helper.hpp"
22 #include "applist.hpp"
23
24 extern "C"
25 {
26 #include <systemd/sd-event.h>
27 }
28
29 using std::string;
30 using std::vector;
31
32 namespace wm
33 {
34
35 static const uint64_t kTimeOut = 3ULL; /* 3s */
36
37 /* DrawingArea name used by "{layout}.{area}" */
38 const char kNameLayoutNormal[] = "normal";
39 const char kNameLayoutSplit[]  = "split";
40 const char kNameAreaFull[]     = "full";
41 const char kNameAreaMain[]     = "main";
42 const char kNameAreaSub[]      = "sub";
43
44 /* Key for json obejct */
45 const char kKeyDrawingName[] = "drawing_name";
46 const char kKeyDrawingArea[] = "drawing_area";
47 const char kKeyDrawingRect[] = "drawing_rect";
48 const char kKeyX[]           = "x";
49 const char kKeyY[]           = "y";
50 const char kKeyWidth[]       = "width";
51 const char kKeyHeight[]      = "height";
52 const char kKeyWidthPixel[]  = "width_pixel";
53 const char kKeyHeightPixel[] = "height_pixel";
54 const char kKeyWidthMm[]     = "width_mm";
55 const char kKeyHeightMm[]    = "height_mm";
56 const char kKeyScale[]       = "scale";
57 const char kKeyIds[]         = "ids";
58
59 static sd_event_source *g_timer_ev_src = nullptr;
60 static AppList g_app_list;
61 static WindowManager *g_context;
62
63 namespace
64 {
65
66 // using nlohmann::json;
67
68 /* result<json> file_to_json(char const *filename)
69 {
70     json j;
71     std::ifstream i(filename);
72     if (i.fail())
73     {
74         HMI_DEBUG("Could not open config file, so use default layer information");
75         j = default_layers_json;
76     }
77     else
78     {
79         i >> j;
80     }
81
82     return Ok(j);
83 }
84
85 struct result<layer_map> load_layer_map(char const *filename)
86 {
87     HMI_DEBUG("loading IDs from %s", filename);
88
89     auto j = file_to_json(filename);
90     if (j.is_err())
91     {
92         return Err<layer_map>(j.unwrap_err());
93     }
94     json jids = j.unwrap();
95
96     return to_layer_map(jids);
97 } */
98
99 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
100 {
101     HMI_NOTICE("Time out occurs because the client replys endDraw slow, so revert the request");
102     reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
103     return 0;
104 }
105
106 static void onStateTransitioned(vector<WMAction> actions)
107 {
108     g_context->startTransitionWrapper(actions);
109 }
110
111 static void onError()
112 {
113     g_context->processError(WMError::LAYOUT_CHANGE_FAIL);
114 }
115 } // namespace
116
117 /**
118  * WindowManager Impl
119  */
120 WindowManager::WindowManager()
121     : chooks{this},
122       id_alloc{}
123 {
124     const char *path = getenv("AFM_APP_INSTALL_DIR");
125     if (!path)
126     {
127         HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
128     }
129     string root = path;
130
131     this->lc = std::make_shared<LayerControl>(root);
132
133 /*     try
134     {
135         {
136             auto l = load_layer_map(path.c_str());
137             if (l.is_ok())
138             {
139                 this->layers = l.unwrap();
140             }
141             else
142             {
143                 HMI_ERROR("%s", l.err().value());
144             }
145         }
146     }
147     catch (std::exception &e)
148     {
149         HMI_ERROR("Loading of configuration failed: %s", e.what());
150     } */
151 }
152
153 int WindowManager::init()
154 {
155     /* if (!this->display->ok())
156     {
157         return -1;
158     }
159
160     if (this->layers.mapping.empty())
161     {
162         HMI_ERROR("No surface -> layer mapping loaded");
163         return -1;
164     } */
165
166     // TODO: application requests by old role,
167     //       so create role map (old, new)
168     // Load old_role.db
169     this->loadOldRoleDb();
170
171     // Store my context for calling callback from PolicyManager
172     g_context = this;
173
174     // Initialize PMWrapper
175     this->pmw.initialize();
176
177     // Register callback to PolicyManager
178     this->pmw.registerCallback(onStateTransitioned, onError);
179
180     // Make afb event
181     for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
182     {
183         map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
184     }
185
186     const struct rect css_bg = this->lc->getAreaSize("fullscreen");
187     Screen screen = this->lc->getScreenInfo();
188     rectangle dp_bg(screen.width(), screen.height());
189
190     dp_bg.set_aspect(static_cast<double>(css_bg.w) / css_bg.h);
191     dp_bg.fit(screen.width(), screen.height());
192     dp_bg.center(screen.width(), screen.height());
193     HMI_DEBUG("SCALING: CSS BG(%dx%d) -> DDP %dx%d,(%dx%d)",
194               css_bg.w, css_bg.h, dp_bg.left(), dp_bg.top(), dp_bg.width(), dp_bg.height());
195
196     double scale = static_cast<double>(dp_bg.height()) / css_bg.h;
197     this->lc->setupArea(scale);
198
199     /* this->display->add_global_handler(
200         "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
201             this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
202         });
203
204     this->display->add_global_handler(
205         "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
206             this->controller =
207                 std::make_unique<struct compositor::controller>(r, name, v);
208
209             // Init controller hooks
210             this->controller->chooks = &this->chooks;
211
212             // This protocol needs the output, so lets just add our mapping here...
213             this->controller->add_proxy_to_id_mapping(
214                 this->outputs.front()->proxy.get(),
215                 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
216                     this->outputs.front()->proxy.get())));
217
218             // Create screen
219             this->controller->create_screen(this->outputs.front()->proxy.get());
220
221             // Set display to controller
222             this->controller->display = this->display;
223         });
224
225     // First level objects
226     this->display->roundtrip();
227     // Second level objects
228     this->display->roundtrip();
229     // Third level objects
230     this->display->roundtrip(); */
231
232     return 0; //init_layers();
233 }
234
235 /* int WindowManager::dispatch_pending_events()
236 {
237     if (this->pop_pending_events())
238     {
239         this->display->dispatch_pending();
240         return 0;
241     }
242     return -1;
243 }
244
245 void WindowManager::set_pending_events()
246 {
247     this->pending_events.store(true, std::memory_order_release);
248 } */
249
250 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
251 {
252     // TODO: application requests by old role,
253     //       so convert role old to new
254     const char *role = this->convertRoleOldToNew(drawing_name);
255     string l_name;
256
257     // auto lid = this->layers.get_layer_id(string(role));
258     unsigned l_id = this->lc->getNewLayerID(role, &l_name);
259     if (l_id == 0)
260     {
261         /**
262        * register drawing_name as fallback and make it displayed.
263        */
264         // lid = this->layers.get_layer_id(string("fallback"));
265         l_id = this->lc->getNewLayerID("fallback", &l_name);
266         HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
267         if (l_id == 0)
268         {
269             return Err<int>("Designated role does not match any role, fallback is disabled");
270         }
271     }
272
273     // generate surface ID for ivi-shell application
274
275     auto rname = this->id_alloc.lookup(string(role));
276     if (!rname)
277     {
278         // name does not exist yet, allocate surface id...
279         auto id = int(this->id_alloc.generate_id(role));
280         // this->layers.add_surface(id, *lid);
281
282         // set the main_surface[_name] here and now
283         // if (!this->layers.main_surface_name.empty() &&
284         //     this->layers.main_surface_name == drawing_name)
285         // {
286         //     this->layers.main_surface = id;
287         //     HMI_DEBUG("Set main_surface id to %u", id);
288         // }
289
290         // add client into the db
291         string appid_str(appid);
292         g_app_list.addClient(appid_str, l_id, l_name, id, string(role));
293
294         /* if(g_app_list.contains(appid_str))
295         {
296             // add surface into app
297             auto client = g_app_list.lookUpClient(appid_str);
298             // client.addSurface(id);
299         }
300         else
301         {
302             g_app_list.addClient(appid_str, l_id, id, string(role));
303         } */
304
305         // Set role map of (new, old)
306         this->rolenew2old[role] = string(drawing_name);
307
308         return Ok<int>(id);
309     }
310
311     // Check currently registered drawing names if it is already there.
312     return Err<int>("Surface already present");
313 }
314
315 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
316                                      char const *ivi_id)
317 {
318     // TODO: application requests by old role,
319     //       so convert role old to new
320     const char *role = this->convertRoleOldToNew(drawing_name);
321     string str = role;
322     string l_name;
323
324     // auto lid = this->layers.get_layer_id(string(role));
325     unsigned lid = this->lc->getNewLayerID(str, &l_name);
326     unsigned sid = std::stol(ivi_id);
327
328     if (lid == 0)
329     {
330         //register drawing_name as fallback and make it displayed.
331         lid = this->lc->getNewLayerID(str, &l_name);
332         HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
333         if (lid == 0)
334         {
335             return "Drawing name does not match any role, fallback is disabled";
336         }
337     }
338
339     auto rname = this->id_alloc.lookup(str);
340
341     if (rname)
342     {
343         return "Surface already present";
344     }
345
346     // register pair drawing_name and ivi_id
347     this->id_alloc.register_name_id(role, sid);
348     // this->layers.add_surface(sid, *lid);
349
350     // this surface is already created
351     // HMI_DEBUG("surface_id is %u, layer_id is %u", sid, *lid);
352
353     // this->controller->layers[*lid]->add_surface(sid);
354     // this->layout_commit();
355
356     // add client into the db
357     string appid_str(appid);
358     g_app_list.addClient(appid_str, lid, l_name, sid, string(role));
359
360     // Set role map of (new, old)
361     this->rolenew2old[role] = string(drawing_name);
362
363     return nullptr;
364 }
365
366 /**
367  * This function is substitute of requestSurface
368  * If surface creation is faster than application request of this function,
369  * WM will bind surfaceID with application and role.
370  * If surface creation is slower than application request of thie function,
371  * WM will put Client into pending list.
372  *
373  * Note :
374  * Application can request with pid but this is temporary solution for now.
375  * This will be removed.
376  * */
377 bool WindowManager::api_set_role(char const *appid, char const *drawing_name, unsigned pid){
378     string id = appid;
379     string role = drawing_name;
380     string l_name;
381     unsigned surface = 0;
382     WMError wm_err = WMError::UNKNOWN;
383     bool ret = false;
384
385     // get layer ID which role should be in
386     // auto lid = this->layers.get_layer_id(role);
387     unsigned lid = this->lc->getNewLayerID(role, &l_name);
388     if (lid == 0)
389     {
390         // lid = this->layers.get_layer_id(string("fallback"));
391         HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role.c_str());
392         if (lid == 0)
393         {
394             HMI_ERROR("Drawing name does not match any role, fallback is disabled");
395             return ret;
396         }
397     }
398
399     if(0 != pid){
400         // search floating surfaceID from pid if pid is designated.
401         wm_err = g_app_list.popFloatingSurface(pid, &surface);
402     }
403     else{
404         // get floating surface with appid. If WM queries appid from pid,
405         // WM can bind surface and role with appid(not implemented yet)
406         //wm_err = g_app_list.popFloatingSurface(id);
407     }
408     if(wm_err != WMError::SUCCESS){
409         HMI_ERROR("No floating surface for app: %s", id.c_str());
410         g_app_list.addFloatingClient(id, lid, role);
411         HMI_NOTICE("%s : Waiting for surface creation", id.c_str());
412         return ret;
413     }
414
415     ret = true;
416     if (g_app_list.contains(id))
417     {
418         HMI_INFO("Add role: %s with surface: %d. Client %s has multi surfaces.",
419                  role.c_str(), surface, id.c_str());
420         auto client = g_app_list.lookUpClient(id);
421         client->appendRole(role);
422     }
423     else{
424         HMI_INFO("Create new client: %s, surface: %d into layer: %d with role: %s",
425                  id.c_str(), surface, lid, role.c_str());
426         g_app_list.addClient(id, lid, l_name, surface, role);
427     }
428
429     // register pair drawing_name and ivi_id
430     this->id_alloc.register_name_id(role.c_str(), surface);
431     // this->layers.add_surface(surface, *lid);
432
433     // this surface is already created
434     HMI_DEBUG("surface_id is %u, layer_id is %u", surface, lid);
435
436     // const auto &o_layer = this->layers.get_layer(*lid);
437     /* auto rect = o_layer.value().rect;
438     if(rect.w < 0)
439     {
440         rect.w = this->controller->output_size.w + 1 + rect.w;
441     }
442     if(rect.h < 0)
443     {
444         rect.h = this->controller->output_size.h + 1 + rect.h;
445     } */
446
447     // this->controller->layers[*lid]->add_surface(surface);
448     // this->layout_commit();
449
450     return ret;
451 }
452
453 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
454                                char const *drawing_area, const reply_func &reply)
455 {
456     ;
457
458     // TODO: application requests by old role,
459     //       so convert role old to new
460     const char *c_role = this->convertRoleOldToNew(drawing_name);
461
462     string id = appid;
463     string role = c_role;
464     string area = drawing_area;
465
466     if(!g_app_list.contains(id))
467     {
468         reply("app doesn't request 'requestSurface' or 'setRole' yet");
469         return;
470     }
471     auto client = g_app_list.lookUpClient(id);
472
473     unsigned srfc = client->surfaceID(role);
474     if(srfc == 0)
475     {
476         HMI_ERROR("role sould be set with surface");
477         reply("role sould be set with surface");
478         return;
479     }
480     g_app_list.removeFloatingSurface(client->surfaceID());
481
482     Task task = Task::TASK_ALLOCATE;
483     unsigned req_num = 0;
484     WMError ret = WMError::UNKNOWN;
485
486     ret = this->setRequest(id, role, area, task, &req_num);
487
488     //vector<WMLayerState> current_states = this->lc->getCurrentStates();
489     // ret = this->setRequest(id, role, area, task, current_states, &req_num);
490
491     if(ret != WMError::SUCCESS)
492     {
493         HMI_ERROR(errorDescription(ret));
494         reply("Failed to set request");
495         return;
496     }
497
498     reply(nullptr);
499     if (req_num != g_app_list.currentRequestNumber())
500     {
501         // Add request, then invoked after the previous task is finished
502         HMI_SEQ_DEBUG(req_num, "request is accepted");
503         return;
504     }
505
506     /*
507      * Do allocate tasks
508      */
509     ret = this->doTransition(req_num);
510
511     if (ret != WMError::SUCCESS)
512     {
513         //this->emit_error()
514         HMI_SEQ_ERROR(req_num, errorDescription(ret));
515         g_app_list.removeRequest(req_num);
516         this->processNextRequest();
517     }
518 }
519
520 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
521                                  const reply_func &reply)
522 {
523     ;
524
525     // TODO: application requests by old role,
526     //       so convert role old to new
527     const char *c_role = this->convertRoleOldToNew(drawing_name);
528
529     /*
530     * Check Phase
531     */
532     string id = appid;
533     string role = c_role;
534     string area = ""; //drawing_area;
535     Task task = Task::TASK_RELEASE;
536     unsigned req_num = 0;
537     WMError ret = WMError::UNKNOWN;
538
539     ret = this->setRequest(id, role, area, task, &req_num);
540     //vector<WMLayerState> current_states = this->lc->getCurrentStates();
541     // ret = this->setRequest(id, role, area, task, current_states, &req_num);
542
543     if (ret != WMError::SUCCESS)
544     {
545         HMI_ERROR(errorDescription(ret));
546         reply("Failed to set request");
547         return;
548     }
549
550     reply(nullptr);
551     if (req_num != g_app_list.currentRequestNumber())
552     {
553         // Add request, then invoked after the previous task is finished
554         HMI_SEQ_DEBUG(req_num, "request is accepted");
555         return;
556     }
557
558     /*
559     * Do allocate tasks
560     */
561     ret = this->doTransition(req_num);
562
563     if (ret != WMError::SUCCESS)
564     {
565         //this->emit_error()
566         HMI_SEQ_ERROR(req_num, errorDescription(ret));
567         g_app_list.removeRequest(req_num);
568         this->processNextRequest();
569     }
570 }
571
572 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
573 {
574     // TODO: application requests by old role,
575     //       so convert role old to new
576     const char *c_role = this->convertRoleOldToNew(drawing_name);
577
578     string id = appid;
579     string role = c_role;
580     unsigned current_req = g_app_list.currentRequestNumber();
581     bool result = g_app_list.setEndDrawFinished(current_req, id, role);
582
583     if (!result)
584     {
585         HMI_ERROR("%s is not in transition state", id.c_str());
586         return;
587     }
588
589     if (g_app_list.endDrawFullfilled(current_req))
590     {
591         // do task for endDraw
592         this->stopTimer();
593         WMError ret = this->doEndDraw(current_req);
594
595         if(ret != WMError::SUCCESS)
596         {
597             //this->emit_error();
598
599             // Undo state of PolicyManager
600             this->pmw.undoState();
601             this->lc->undoUpdate();
602         }
603         this->emitScreenUpdated(current_req);
604         HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
605
606         g_app_list.removeRequest(current_req);
607
608         this->processNextRequest();
609     }
610     else
611     {
612         HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
613         return;
614     }
615 }
616
617 result<json_object *> WindowManager::api_get_display_info()
618 {
619     /* if (!this->display->ok())
620     {
621         return Err<json_object *>("Wayland compositor is not available");
622     } */
623
624     // Set display info
625 /*     size o_size = this->controller->output_size;
626     size p_size = this->controller->physical_size; */
627     Screen screen = this->lc->getScreenInfo();
628
629     json_object *object = json_object_new_object();
630     json_object_object_add(object, kKeyWidthPixel, json_object_new_int(screen.width()));
631     json_object_object_add(object, kKeyHeightPixel, json_object_new_int(screen.height()));
632     // TODO: set size
633     json_object_object_add(object, kKeyWidthMm, json_object_new_int(0));
634     json_object_object_add(object, kKeyHeightMm, json_object_new_int(0));
635     json_object_object_add(object, kKeyScale, json_object_new_double(this->lc->scale()));
636
637     return Ok<json_object *>(object);
638 }
639
640 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
641 {
642     HMI_DEBUG("called");
643
644     // TODO: application requests by old role,
645     //       so convert role old to new
646     const char *role = this->convertRoleOldToNew(drawing_name);
647
648     // Check drawing name, surface/layer id
649     auto const &surface_id = this->id_alloc.lookup(string(role));
650     if (!surface_id)
651     {
652         return Err<json_object *>("Surface does not exist");
653     }
654
655     /* if (!this->controller->surface_exists(*surface_id))
656     {
657         return Err<json_object *>("Surface does not exist in controller!");
658     } */
659
660     /* // auto layer_id = this->layers.get_layer_id(*surface_id);
661     if (!layer_id)
662     {
663         return Err<json_object *>("Surface is not on any layer!");
664     } */
665
666     // Set area rectangle
667     rect area_info = this->area_info[*surface_id];
668     json_object *object = json_object_new_object();
669     json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
670     json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
671     json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
672     json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
673
674     return Ok<json_object *>(object);
675 }
676
677 void WindowManager::api_ping() { /* this->dispatch_pending_events(); */ }
678
679 void WindowManager::send_event(char const *evname, char const *label)
680 {
681     HMI_DEBUG("%s: %s(%s)", __func__, evname, label);
682
683     json_object *j = json_object_new_object();
684     json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
685
686     int ret = afb_event_push(this->map_afb_event[evname], j);
687     if (ret != 0)
688     {
689         HMI_DEBUG("afb_event_push failed: %m");
690     }
691 }
692
693 void WindowManager::send_event(char const *evname, char const *label, char const *area,
694                      int x, int y, int w, int h)
695 {
696     HMI_DEBUG("%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
697               __func__, evname, label, area, x, y, w, h);
698
699     json_object *j_rect = json_object_new_object();
700     json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
701     json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
702     json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
703     json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
704
705     json_object *j = json_object_new_object();
706     json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
707     json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
708     json_object_object_add(j, kKeyDrawingRect, j_rect);
709
710     int ret = afb_event_push(this->map_afb_event[evname], j);
711     if (ret != 0)
712     {
713         HMI_DEBUG("afb_event_push failed: %m");
714     }
715 }
716
717 /**
718  * proxied events
719  */
720 void WindowManager::surface_created(uint32_t surface_id)
721 {
722     /* this->controller->get_surface_properties(surface_id, IVI_WM_PARAM_SIZE);
723
724     auto layer_id = this->layers.get_layer_id(surface_id);
725     if (!layer_id)
726     {
727         HMI_DEBUG("Newly created surfce %d is not associated with any layer!",
728                   surface_id);
729         return;
730     }
731
732     HMI_DEBUG("surface_id is %u, layer_id is %u", surface_id, *layer_id);
733
734     this->controller->layers[*layer_id]->add_surface(surface_id);
735     this->layout_commit(); */
736 }
737
738 void WindowManager::surface_removed(uint32_t surface_id)
739 {
740     HMI_DEBUG("Delete surface_id %u", surface_id);
741     this->id_alloc.remove_id(surface_id);
742     // this->layers.remove_surface(surface_id);
743     g_app_list.removeSurface(surface_id);
744 }
745
746 void WindowManager::surface_properties(unsigned surface_id, unsigned pid)
747 {
748     HMI_DEBUG("get surface properties");
749
750     // search pid from surfaceID
751     /*json_object *response;
752     afb_service_call_sync("afm-main", "runners", nullptr, &response);
753
754     // retrieve appid from pid from application manager
755     string appid = "";
756     if(response == nullptr)
757     {
758         HMI_ERROR("No runners");
759     }
760     else
761     {
762         // check appid then add it to the client
763         HMI_INFO("Runners:%s", json_object_get_string(response));
764         int size = json_object_array_length(response);
765         for(int i = 0; i < size; i++)
766         {
767             json_object *j = json_object_array_get_idx(response, i);
768             const char* id = jh::getStringFromJson(j, "id");
769             int runid      = jh::getIntFromJson(j, "runid");
770             if(id && (runid > 0))
771             {
772                 if(runid == pid)
773                 {
774                     appid = id;
775                     break;
776                 }
777             }
778         }
779     }
780     json_object_put(response);
781
782     g_app_list.addFloatingSurface(appid, surface_id, pid);*/
783 }
784
785 void WindowManager::removeClient(const string &appid)
786 {
787     HMI_DEBUG("Remove clinet %s from list", appid.c_str());
788     g_app_list.removeClient(appid);
789 }
790
791 void WindowManager::exceptionProcessForTransition()
792 {
793     unsigned req_num = g_app_list.currentRequestNumber();
794     HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
795     g_app_list.removeRequest(req_num);
796     HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
797     this->processNextRequest();
798 }
799
800 void WindowManager::timerHandler()
801 {
802     unsigned req_num = g_app_list.currentRequestNumber();
803     HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
804     g_app_list.reqDump();
805     g_app_list.removeRequest(req_num);
806     this->processNextRequest();
807 }
808
809 void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
810 {
811     WMError ret;
812     unsigned req_num = g_app_list.currentRequestNumber();
813
814     if (actions.empty())
815     {
816         if (g_app_list.haveRequest())
817         {
818             HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
819             goto proc_remove_request;
820         }
821         else
822         {
823             HMI_SEQ_DEBUG(req_num, "There is no request");
824             return;
825         }
826     }
827
828     for (auto &act : actions)
829     {
830         if ("" != act.role)
831         {
832             bool found;
833             auto const &surface_id = this->id_alloc.lookup(act.role);
834             string appid = g_app_list.getAppID(*surface_id, act.role, &found);
835             if (!found)
836             {
837                 if (TaskVisible::INVISIBLE == act.visible)
838                 {
839                     // App is killed, so do not set this action
840                     continue;
841                 }
842                 else
843                 {
844                     HMI_SEQ_ERROR(req_num, "appid which is visible is not found");
845                     ret = WMError::FAIL;
846                     goto error;
847                 }
848             }
849             act.appid = appid;
850         }
851
852         ret = g_app_list.setAction(req_num, act);
853         if (ret != WMError::SUCCESS)
854         {
855             HMI_SEQ_ERROR(req_num, "Setting action is failed");
856             goto error;
857         }
858     }
859
860     HMI_SEQ_DEBUG(req_num, "Start transition.");
861     ret = this->startTransition(req_num);
862     if (ret != WMError::SUCCESS)
863     {
864         if (ret == WMError::NO_LAYOUT_CHANGE)
865         {
866             goto proc_remove_request;
867         }
868         else
869         {
870             HMI_SEQ_ERROR(req_num, "Transition state is failed");
871             goto error;
872         }
873     }
874
875     return;
876
877 error:
878     //this->emit_error()
879     HMI_SEQ_ERROR(req_num, errorDescription(ret));
880     this->pmw.undoState();
881
882 proc_remove_request:
883     g_app_list.removeRequest(req_num);
884     this->processNextRequest();
885 }
886
887 void WindowManager::processError(WMError error)
888 {
889     unsigned req_num = g_app_list.currentRequestNumber();
890
891     //this->emit_error()
892     HMI_SEQ_ERROR(req_num, errorDescription(error));
893     g_app_list.removeRequest(req_num);
894     this->processNextRequest();
895 }
896
897 /*
898  ******* Private Functions *******
899  */
900
901 /**
902  * init_layers()
903  */
904 int WindowManager::init_layers()
905 {
906     /* if (!this->controller)
907     {
908         HMI_ERROR("ivi_controller global not available");
909         return -1;
910     }
911
912     if (this->outputs.empty())
913     {
914         HMI_ERROR("no output was set up!");
915         return -1;
916     }
917
918     auto &c = this->controller;
919
920     auto &o = this->outputs.front();
921     auto &s = c->screens.begin()->second;
922     auto &layers = c->layers;
923
924     // Write output dimensions to ivi controller...
925     c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
926     c->physical_size = compositor::size{uint32_t(o->physical_width),
927                                         uint32_t(o->physical_height)};
928
929
930     HMI_DEBUG("SCALING: screen (%dx%d), physical (%dx%d)",
931               o->width, o->height, o->physical_width, o->physical_height);
932
933     // this->layers.loadAreaDb();
934
935     const compositor::rect css_bg = this->layers.getAreaSize("fullscreen");
936     rectangle dp_bg(o->width, o->height);
937
938     dp_bg.set_aspect(static_cast<double>(css_bg.w) / css_bg.h);
939     dp_bg.fit(o->width, o->height);
940     dp_bg.center(o->width, o->height);
941     HMI_DEBUG("SCALING: CSS BG(%dx%d) -> DDP %dx%d,(%dx%d)",
942               css_bg.w, css_bg.h, dp_bg.left(), dp_bg.top(), dp_bg.width(), dp_bg.height());
943
944     // Clear scene
945     // layers.clear();
946
947     // Clear screen
948     // s->clear();
949
950     // Quick and dirty setup of layers
951     for (auto const &i : this->layers.mapping)
952     {
953         c->layer_create(i.second.layer_id, dp_bg.width(), dp_bg.height());
954         auto &l = layers[i.second.layer_id];
955         l->set_destination_rectangle(dp_bg.left(), dp_bg.top(), dp_bg.width(), dp_bg.height());
956         l->set_visibility(1);
957         HMI_DEBUG("Setting up layer %s (%d) for surface role match \"%s\"",
958                   i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
959     }
960
961     // Add layers to screen
962     s->set_render_order(this->layers.layers);
963
964     this->layout_commit();
965
966     c->scale = static_cast<double>(dp_bg.height()) / css_bg.h;
967     this->layers.setupArea(c->scale);
968  */
969     return 0;
970 }
971
972 void WindowManager::surface_set_layout(int surface_id, const string& area)
973 {
974     /* if (!this->controller->surface_exists(surface_id))
975     {
976         HMI_ERROR("Surface %d does not exist", surface_id);
977         return;
978     }
979
980     auto o_layer_id = this->layers.get_layer_id(surface_id);
981
982     if (!o_layer_id)
983     {
984         HMI_ERROR("Surface %d is not associated with any layer!", surface_id);
985         return;
986     }
987
988     uint32_t layer_id = *o_layer_id;
989
990     auto const &layer = this->layers.get_layer(layer_id);
991     auto rect = this->layers.getAreaSize(area);
992     HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
993                     rect.x, rect.y, rect.w, rect.h);
994     auto &s = this->controller->surfaces[surface_id];
995
996     int x = rect.x;
997     int y = rect.y;
998     int w = rect.w;
999     int h = rect.h;
1000
1001     HMI_DEBUG("surface_set_layout for surface %u on layer %u", surface_id,
1002               layer_id);
1003
1004     // set destination to the display rectangle
1005     s->set_destination_rectangle(x, y, w, h);
1006
1007     // update area information
1008     this->area_info[surface_id].x = x;
1009     this->area_info[surface_id].y = y;
1010     this->area_info[surface_id].w = w;
1011     this->area_info[surface_id].h = h;
1012
1013     HMI_DEBUG("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
1014               surface_id, layer_id, x, y, w, h); */
1015 }
1016
1017 void WindowManager::layout_commit()
1018 {
1019 /*     this->controller->commit_changes();
1020     this->display->flush(); */
1021 }
1022
1023 void WindowManager::emit_activated(char const *label)
1024 {
1025     this->send_event(kListEventName[Event_Active], label);
1026 }
1027
1028 void WindowManager::emit_deactivated(char const *label)
1029 {
1030     this->send_event(kListEventName[Event_Inactive], label);
1031 }
1032
1033 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1034 {
1035     this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1036 }
1037
1038 void WindowManager::emit_syncdraw(const string &role, const string &area)
1039 {
1040 /*     rect rect = this->layers.getAreaSize(area);
1041     this->send_event(kListEventName[Event_SyncDraw],
1042         role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h); */
1043 }
1044
1045 void WindowManager::emit_flushdraw(char const *label)
1046 {
1047     this->send_event(kListEventName[Event_FlushDraw], label);
1048 }
1049
1050 void WindowManager::emit_visible(char const *label, bool is_visible)
1051 {
1052     this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1053 }
1054
1055 void WindowManager::emit_invisible(char const *label)
1056 {
1057     return emit_visible(label, false);
1058 }
1059
1060 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
1061
1062 void WindowManager::activate(int id)
1063 {
1064     /* auto ip = this->controller->sprops.find(id);
1065     if (ip != this->controller->sprops.end())
1066     {
1067         this->controller->surfaces[id]->set_visibility(1);
1068         */
1069         char const *label =
1070             this->id_alloc.lookup(id).value_or("unknown-name").c_str();
1071
1072         /* // FOR CES DEMO >>>
1073         if ((0 == strcmp(label, "radio")) ||
1074             (0 == strcmp(label, "music")) ||
1075             (0 == strcmp(label, "video")) ||
1076             (0 == strcmp(label, "map")))
1077         {
1078             for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1079             {
1080                 if (id == *i)
1081                 {
1082                     // Remove id
1083                     this->surface_bg.erase(i);
1084
1085                     // Remove from BG layer (999)
1086                     HMI_DEBUG("Remove %s(%d) from BG layer", label, id);
1087                     this->controller->layers[999]->remove_surface(id);
1088
1089                     // Add to FG layer (1001)
1090                     HMI_DEBUG("Add %s(%d) to FG layer", label, id);
1091                     this->controller->layers[1001]->add_surface(id);
1092
1093                     for (int j : this->surface_bg)
1094                     {
1095                         HMI_DEBUG("Stored id:%d", j);
1096                     }
1097                     break;
1098                 }
1099             }
1100         }
1101         // <<< FOR CES DEMO
1102
1103         this->layout_commit(); */
1104
1105         // TODO: application requests by old role,
1106         //       so convert role new to old for emitting event
1107         const char* old_role = this->rolenew2old[label].c_str();
1108
1109         this->emit_visible(old_role);
1110         this->emit_activated(old_role);
1111     // }
1112 }
1113
1114 void WindowManager::deactivate(int id)
1115 {
1116     /* auto ip = this->controller->sprops.find(id);
1117     if (ip != this->controller->sprops.end())
1118     {*/
1119         char const *label =
1120             this->id_alloc.lookup(id).value_or("unknown-name").c_str();
1121
1122         /*// FOR CES DEMO >>>
1123         if ((0 == strcmp(label, "radio")) ||
1124             (0 == strcmp(label, "music")) ||
1125             (0 == strcmp(label, "video")) ||
1126             (0 == strcmp(label, "map")))
1127         {
1128
1129             // Store id
1130             this->surface_bg.push_back(id);
1131
1132             // Remove from FG layer (1001)
1133             HMI_DEBUG("Remove %s(%d) from FG layer", label, id);
1134             this->controller->layers[1001]->remove_surface(id);
1135
1136             // Add to BG layer (999)
1137             HMI_DEBUG("Add %s(%d) to BG layer", label, id);
1138             this->controller->layers[999]->add_surface(id);
1139
1140             for (int j : surface_bg)
1141             {
1142                 HMI_DEBUG("Stored id:%d", j);
1143             }
1144         }
1145         else
1146         {
1147             this->controller->surfaces[id]->set_visibility(0);
1148         }
1149         // <<< FOR CES DEMO
1150
1151         this->layout_commit(); */
1152
1153         // TODO: application requests by old role,
1154         //       so convert role new to old for emitting event
1155         const char* old_role = this->rolenew2old[label].c_str();
1156
1157         this->emit_deactivated(old_role);
1158         this->emit_invisible(old_role);
1159     // }
1160 }
1161
1162 WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
1163                             Task task, unsigned* req_num)
1164 {
1165     if (!g_app_list.contains(appid))
1166     {
1167         return WMError::NOT_REGISTERED;
1168     }
1169
1170     auto client = g_app_list.lookUpClient(appid);
1171
1172     /*
1173      * Queueing Phase
1174      */
1175     unsigned current = g_app_list.currentRequestNumber();
1176     unsigned requested_num = g_app_list.getRequestNumber(appid);
1177     if (requested_num != 0)
1178     {
1179         HMI_SEQ_INFO(requested_num,
1180             "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
1181         return REQ_REJECTED;
1182     }
1183
1184     WMRequest req = WMRequest(appid, role, area, task);
1185     unsigned new_req = g_app_list.addRequest(req);
1186     *req_num = new_req;
1187     g_app_list.reqDump();
1188
1189     HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1190
1191     return WMError::SUCCESS;
1192 }
1193
1194 WMError WindowManager::doTransition(unsigned req_num)
1195 {
1196     HMI_SEQ_DEBUG(req_num, "check policy");
1197     WMError ret = this->checkPolicy(req_num);
1198     return ret;
1199 }
1200
1201 WMError WindowManager::checkPolicy(unsigned req_num)
1202 {
1203     /*
1204     * Check Policy
1205     */
1206     // get current trigger
1207     bool found = false;
1208     WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1209     auto trigger = g_app_list.getRequest(req_num, &found);
1210     if (!found)
1211     {
1212         ret = WMError::NO_ENTRY;
1213         return ret;
1214     }
1215     string req_area = trigger.area;
1216
1217     if (trigger.task == Task::TASK_ALLOCATE)
1218     {
1219         const char *msg = this->check_surface_exist(trigger.role.c_str());
1220
1221         if (msg)
1222         {
1223             HMI_SEQ_ERROR(req_num, msg);
1224             return ret;
1225         }
1226     }
1227
1228     // Input event data to PolicyManager
1229     if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1230     {
1231         HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1232         return ret;
1233     }
1234
1235     // Execute state transition of PolicyManager
1236     if (0 > this->pmw.executeStateTransition())
1237     {
1238         HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1239         return ret;
1240     }
1241
1242     ret = WMError::SUCCESS;
1243
1244     g_app_list.reqDump();
1245
1246     return ret;
1247 }
1248
1249 WMError WindowManager::startTransition(unsigned req_num)
1250 {
1251     bool sync_draw_happen = false;
1252     bool found = false;
1253     WMError ret = WMError::SUCCESS;
1254     auto actions = g_app_list.getActions(req_num, &found);
1255     if (!found)
1256     {
1257         ret = WMError::NO_ENTRY;
1258         HMI_SEQ_ERROR(req_num,
1259             "Window Manager bug :%s : Action is not set", errorDescription(ret));
1260         return ret;
1261     }
1262
1263     for (const auto &action : actions)
1264     {
1265         if (action.visible == TaskVisible::VISIBLE)
1266         {
1267             sync_draw_happen = true;
1268
1269             // TODO: application requests by old role,
1270             //       so convert role new to old for emitting event
1271             string old_role = this->rolenew2old[action.role];
1272
1273             this->emit_syncdraw(old_role, action.area);
1274             /* TODO: emit event for app not subscriber
1275             if(g_app_list.contains(y.appid))
1276                 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1277         }
1278     }
1279
1280     if (sync_draw_happen)
1281     {
1282         this->setTimer();
1283     }
1284     else
1285     {
1286         // deactivate only, no syncDraw
1287         // Make it deactivate here
1288         for (const auto &x : actions)
1289         {
1290             if (g_app_list.contains(x.appid))
1291             {
1292                 auto client = g_app_list.lookUpClient(x.appid);
1293                 this->deactivate(client->surfaceID(x.role));
1294             }
1295         }
1296         ret = WMError::NO_LAYOUT_CHANGE;
1297     }
1298     return ret;
1299 }
1300
1301 WMError WindowManager::doEndDraw(unsigned req_num)
1302 {
1303     // get actions
1304     bool found;
1305     auto actions = g_app_list.getActions(req_num, &found);
1306     WMError ret = WMError::SUCCESS;
1307     if (!found)
1308     {
1309         ret = WMError::NO_ENTRY;
1310         return ret;
1311     }
1312
1313     HMI_SEQ_INFO(req_num, "do endDraw");
1314
1315     // layout change and make it visible
1316     for (const auto &act : actions)
1317     {
1318         if(act.visible != TaskVisible::NO_CHANGE)
1319         {
1320             // layout change
1321             if(!g_app_list.contains(act.appid)){
1322                 ret = WMError::NOT_REGISTERED;
1323             }
1324             ret = this->layoutChange(act);
1325             if(ret != WMError::SUCCESS)
1326             {
1327                 HMI_SEQ_WARNING(req_num,
1328                     "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1329                 return ret;
1330             }
1331             ret = this->visibilityChange(act);
1332             if (ret != WMError::SUCCESS)
1333             {
1334                 HMI_SEQ_WARNING(req_num,
1335                     "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1336                 return ret;
1337             }
1338             HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1339             //this->lc_enddraw(act.role.c_str());
1340         }
1341     }
1342     this->layout_commit();
1343
1344     HMI_SEQ_INFO(req_num, "emit flushDraw");
1345
1346     for(const auto &act_flush : actions)
1347     {
1348         if(act_flush.visible == TaskVisible::VISIBLE)
1349         {
1350             // TODO: application requests by old role,
1351             //       so convert role new to old for emitting event
1352             string old_role = this->rolenew2old[act_flush.role];
1353
1354             this->emit_flushdraw(old_role.c_str());
1355         }
1356     }
1357
1358     return ret;
1359 }
1360
1361 WMError WindowManager::layoutChange(const WMAction &action)
1362 {
1363     if (action.visible == TaskVisible::INVISIBLE)
1364     {
1365         // Visibility is not change -> no redraw is required
1366         return WMError::SUCCESS;
1367     }
1368     auto client = g_app_list.lookUpClient(action.appid);
1369     unsigned surface = client->surfaceID(action.role);
1370     if (surface == 0)
1371     {
1372         HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1373                       "client doesn't have surface with role(%s)", action.role.c_str());
1374         return WMError::NOT_REGISTERED;
1375     }
1376     // Layout Manager
1377     WMError ret = this->setSurfaceSize(surface, action.area);
1378     return ret;
1379 }
1380
1381 WMError WindowManager::visibilityChange(const WMAction &action)
1382 {
1383     HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1384     if(!g_app_list.contains(action.appid)){
1385         return WMError::NOT_REGISTERED;
1386     }
1387     auto client = g_app_list.lookUpClient(action.appid);
1388     unsigned surface = client->surfaceID(action.role);
1389     if(surface == 0)
1390     {
1391         HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1392                       "client doesn't have surface with role(%s)", action.role.c_str());
1393         return WMError::NOT_REGISTERED;
1394     }
1395
1396     if (action.visible != TaskVisible::INVISIBLE)
1397     {
1398         this->activate(surface); // Layout Manager task
1399     }
1400     else
1401     {
1402         this->deactivate(surface); // Layout Manager task
1403     }
1404     return WMError::SUCCESS;
1405 }
1406
1407 WMError WindowManager::setSurfaceSize(unsigned surface, const string &area)
1408 {
1409     this->surface_set_layout(surface, area);
1410
1411     return WMError::SUCCESS;
1412 }
1413
1414 void WindowManager::emitScreenUpdated(unsigned req_num)
1415 {
1416     // Get visible apps
1417     HMI_SEQ_DEBUG(req_num, "emit screen updated");
1418     bool found = false;
1419     auto actions = g_app_list.getActions(req_num, &found);
1420
1421     // create json object
1422     json_object *j = json_object_new_object();
1423     json_object *jarray = json_object_new_array();
1424
1425     for(const auto& action: actions)
1426     {
1427         if(action.visible != TaskVisible::INVISIBLE)
1428         {
1429             json_object_array_add(jarray, json_object_new_string(action.appid.c_str()));
1430         }
1431     }
1432     json_object_object_add(j, kKeyIds, jarray);
1433     HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1434
1435     int ret = afb_event_push(
1436         this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1437     if (ret != 0)
1438     {
1439         HMI_DEBUG("afb_event_push failed: %m");
1440     }
1441 }
1442
1443 void WindowManager::setTimer()
1444 {
1445     struct timespec ts;
1446     if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
1447         HMI_ERROR("Could't set time (clock_gettime() returns with error");
1448         return;
1449     }
1450
1451     HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1452     if (g_timer_ev_src == nullptr)
1453     {
1454         // firsttime set into sd_event
1455         int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1456             CLOCK_BOOTTIME, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL, 1, processTimerHandler, this);
1457         if (ret < 0)
1458         {
1459             HMI_ERROR("Could't set timer");
1460         }
1461     }
1462     else
1463     {
1464         // update timer limitation after second time
1465         sd_event_source_set_time(g_timer_ev_src, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL);
1466         sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1467     }
1468 }
1469
1470 void WindowManager::stopTimer()
1471 {
1472     unsigned req_num = g_app_list.currentRequestNumber();
1473     HMI_SEQ_DEBUG(req_num, "Timer stop");
1474     int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1475     if (rc < 0)
1476     {
1477         HMI_SEQ_ERROR(req_num, "Timer stop failed");
1478     }
1479 }
1480
1481 void WindowManager::processNextRequest()
1482 {
1483     g_app_list.next();
1484     g_app_list.reqDump();
1485     unsigned req_num = g_app_list.currentRequestNumber();
1486     if (g_app_list.haveRequest())
1487     {
1488         HMI_SEQ_DEBUG(req_num, "Process next request");
1489         WMError rc = doTransition(req_num);
1490         if (rc != WMError::SUCCESS)
1491         {
1492             HMI_SEQ_ERROR(req_num, errorDescription(rc));
1493         }
1494     }
1495     else
1496     {
1497         HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1498     }
1499 }
1500
1501 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1502 {
1503     const char *new_role = nullptr;
1504
1505     for (auto const &on : this->roleold2new)
1506     {
1507         std::regex regex = std::regex(on.first);
1508         if (std::regex_match(old_role, regex))
1509         {
1510             // role is old. So convert to new.
1511             new_role = on.second.c_str();
1512             break;
1513         }
1514     }
1515
1516     if (nullptr == new_role)
1517     {
1518         // role is new or fallback.
1519         new_role = old_role;
1520     }
1521
1522     HMI_DEBUG("old:%s -> new:%s", old_role, new_role);
1523
1524     return new_role;
1525 }
1526
1527 int WindowManager::loadOldRoleDb()
1528 {
1529     // Get afm application installed dir
1530     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1531     HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
1532
1533     string file_name;
1534     if (!afm_app_install_dir)
1535     {
1536         HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
1537     }
1538     else
1539     {
1540         file_name = string(afm_app_install_dir) + string("/etc/old_roles.db");
1541     }
1542
1543     // Load old_role.db
1544     json_object* json_obj;
1545     int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1546     if (0 > ret)
1547     {
1548         HMI_ERROR("Could not open old_role.db, so use default old_role information");
1549         json_obj = json_tokener_parse(kDefaultOldRoleDb);
1550     }
1551     HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
1552
1553     // Perse apps
1554     json_object* json_cfg;
1555     if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1556     {
1557         HMI_ERROR("Parse Error!!");
1558         return -1;
1559     }
1560
1561     int len = json_object_array_length(json_cfg);
1562     HMI_DEBUG("json_cfg len:%d", len);
1563     HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
1564
1565     for (int i=0; i<len; i++)
1566     {
1567         json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1568
1569         const char* old_role = jh::getStringFromJson(json_tmp, "name");
1570         if (nullptr == old_role)
1571         {
1572             HMI_ERROR("Parse Error!!");
1573             return -1;
1574         }
1575
1576         const char* new_role = jh::getStringFromJson(json_tmp, "new");
1577         if (nullptr == new_role)
1578         {
1579             HMI_ERROR("Parse Error!!");
1580             return -1;
1581         }
1582
1583         this->roleold2new[old_role] = string(new_role);
1584     }
1585
1586     // Check
1587     for(auto itr = this->roleold2new.begin();
1588       itr != this->roleold2new.end(); ++itr)
1589     {
1590         HMI_DEBUG(">>> role old:%s new:%s",
1591                   itr->first.c_str(), itr->second.c_str());
1592     }
1593
1594     // Release json_object
1595     json_object_put(json_obj);
1596
1597     return 0;
1598 }
1599
1600 const char *WindowManager::check_surface_exist(const char *drawing_name)
1601 {
1602     auto const &surface_id = this->id_alloc.lookup(string(drawing_name));
1603     if (!surface_id)
1604     {
1605         return "Surface does not exist";
1606     }
1607
1608     /* if (!this->controller->surface_exists(*surface_id))
1609     {
1610         return "Surface does not exist in controller!";
1611     } */
1612
1613     /* auto layer_id = this->layers.get_layer_id(*surface_id);
1614
1615     if (!layer_id)
1616     {
1617         return "Surface is not on any layer!";
1618     } */
1619
1620     HMI_DEBUG("surface %d is detected", *surface_id);
1621     return nullptr;
1622 }
1623
1624 const char* WindowManager::kDefaultOldRoleDb = "{ \
1625     \"old_roles\": [ \
1626         { \
1627             \"name\": \"HomeScreen\", \
1628             \"new\": \"homescreen\" \
1629         }, \
1630         { \
1631             \"name\": \"Music\", \
1632             \"new\": \"music\" \
1633         }, \
1634         { \
1635             \"name\": \"MediaPlayer\", \
1636             \"new\": \"music\" \
1637         }, \
1638         { \
1639             \"name\": \"Video\", \
1640             \"new\": \"video\" \
1641         }, \
1642         { \
1643             \"name\": \"VideoPlayer\", \
1644             \"new\": \"video\" \
1645         }, \
1646         { \
1647             \"name\": \"WebBrowser\", \
1648             \"new\": \"browser\" \
1649         }, \
1650         { \
1651             \"name\": \"Radio\", \
1652             \"new\": \"radio\" \
1653         }, \
1654         { \
1655             \"name\": \"Phone\", \
1656             \"new\": \"phone\" \
1657         }, \
1658         { \
1659             \"name\": \"Navigation\", \
1660             \"new\": \"map\" \
1661         }, \
1662         { \
1663             \"name\": \"HVAC\", \
1664             \"new\": \"hvac\" \
1665         }, \
1666         { \
1667             \"name\": \"Settings\", \
1668             \"new\": \"settings\" \
1669         }, \
1670         { \
1671             \"name\": \"Dashboard\", \
1672             \"new\": \"dashboard\" \
1673         }, \
1674         { \
1675             \"name\": \"POI\", \
1676             \"new\": \"poi\" \
1677         }, \
1678         { \
1679             \"name\": \"Mixer\", \
1680             \"new\": \"mixer\" \
1681         }, \
1682         { \
1683             \"name\": \"Restriction\", \
1684             \"new\": \"restriction\" \
1685         }, \
1686         { \
1687             \"name\": \"^OnScreen.*\", \
1688             \"new\": \"on_screen\" \
1689         } \
1690     ] \
1691 }";
1692
1693 /**
1694  * controller_hooks
1695  */
1696 void controller_hooks::surface_created(uint32_t surface_id)
1697 {
1698     this->wmgr->surface_created(surface_id);
1699 }
1700
1701 void controller_hooks::surface_removed(uint32_t surface_id)
1702 {
1703     this->wmgr->surface_removed(surface_id);
1704 }
1705
1706 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1707                                           uint32_t /*v*/) {}
1708
1709 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,
1710                                                      uint32_t /*x*/,
1711                                                      uint32_t /*y*/,
1712                                                      uint32_t /*w*/,
1713                                                      uint32_t /*h*/) {}
1714
1715 void controller_hooks::surface_properties(uint32_t surface_id, uint32_t pid)
1716 {
1717     this->wmgr->surface_properties(surface_id, pid);
1718 }
1719
1720 } // namespace wm