Ensure wm_subscribe returns the correct value
[apps/agl-service-windowmanager.git] / src / window_manager.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2019 Konsulko Group
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <fstream>
19 #include <regex>
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 using std::unordered_map;
33
34 namespace wm
35 {
36
37 static const uint64_t kTimeOut = 3ULL; /* 3s */
38
39 /* DrawingArea name used by "{layout}.{area}" */
40 const char kNameLayoutNormal[] = "normal";
41 const char kNameLayoutSplit[]  = "split";
42 const char kNameAreaFull[]     = "full";
43 const char kNameAreaMain[]     = "main";
44 const char kNameAreaSub[]      = "sub";
45
46 /* Key for json obejct */
47 const char kKeyDrawingName[] = "drawing_name";
48 const char kKeyDrawingArea[] = "drawing_area";
49 const char kKeyDrawingRect[] = "drawing_rect";
50 const char kKeyX[]           = "x";
51 const char kKeyY[]           = "y";
52 const char kKeyWidth[]       = "width";
53 const char kKeyHeight[]      = "height";
54 const char kKeyWidthPixel[]  = "width_pixel";
55 const char kKeyHeightPixel[] = "height_pixel";
56 const char kKeyWidthMm[]     = "width_mm";
57 const char kKeyHeightMm[]    = "height_mm";
58 const char kKeyScale[]       = "scale";
59 const char kKeyIds[]         = "ids";
60
61 static const vector<string> kListEventName{
62         "active",
63         "inactive",
64         "visible",
65         "invisible",
66         "syncDraw",
67         "flushDraw",
68         "screenUpdated",
69         "error"};
70
71 static sd_event_source *g_timer_ev_src = nullptr;
72 static AppList g_app_list;
73 static WindowManager *g_context;
74 static vector<string> white_list_area_size_change = {
75     "homescreen", "settings"
76 };
77
78 namespace
79 {
80
81 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
82 {
83     HMI_NOTICE("Time out occurs because the client replys endDraw slow, so revert the request");
84     reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
85     return 0;
86 }
87
88 static void onStateTransitioned(vector<WMAction> actions)
89 {
90     g_context->startTransitionWrapper(actions);
91 }
92
93 static void onError()
94 {
95     g_context->processError(WMError::LAYOUT_CHANGE_FAIL);
96 }
97 } // namespace
98
99 /**
100  * WindowManager Impl
101  */
102 WindowManager::WindowManager()
103     : id_alloc{}
104 {
105     const char *path = getenv("AFM_APP_INSTALL_DIR");
106     if (!path)
107     {
108         HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
109     }
110     string root = path;
111
112     this->lc = std::make_shared<LayerControl>(root);
113
114     HMI_DEBUG("Layer Controller initialized");
115 }
116
117 int WindowManager::init()
118 {
119     LayerControlCallbacks lmcb;
120     lmcb.surfaceCreated = [&](unsigned pid, unsigned surface){
121         this->surface_created(surface);
122         };
123     lmcb.surfaceDestroyed = [&](unsigned surface){
124         this->surface_removed(surface);
125     };
126
127     if(this->lc->init(lmcb) != WMError::SUCCESS)
128     {
129         return -1;
130     }
131
132     // Store my context for calling callback from PolicyManager
133     g_context = this;
134
135     // Initialize PMWrapper
136     this->pmw.initialize();
137
138     // Register callback to PolicyManager
139     this->pmw.registerCallback(onStateTransitioned, onError);
140
141     // Make afb event for subscriber
142     for (int i = Event_ScreenUpdated; i < Event_Error; i++)
143     {
144         map_afb_event[kListEventName[i]] = afb_api_make_event(afbBindingV3root, kListEventName[i].c_str());
145     }
146
147     const struct rect css_bg = this->lc->getAreaSize("fullscreen");
148     Screen screen = this->lc->getScreenInfo();
149     rectangle dp_bg(screen.width(), screen.height());
150
151     dp_bg.set_aspect(static_cast<double>(css_bg.w) / css_bg.h);
152     dp_bg.fit(screen.width(), screen.height());
153     dp_bg.center(screen.width(), screen.height());
154     HMI_DEBUG("SCALING: CSS BG(%dx%d) -> DDP %dx%d,(%dx%d)",
155               css_bg.w, css_bg.h, dp_bg.left(), dp_bg.top(), dp_bg.width(), dp_bg.height());
156
157     double scale = static_cast<double>(dp_bg.height()) / css_bg.h;
158     this->lc->setupArea(dp_bg, scale);
159
160     return 0;
161 }
162
163 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
164 {
165     string str_id = appid;
166     string role = drawing_name;
167     unsigned lid = 0;
168
169     if(!g_app_list.contains(str_id))
170     {
171         lid = this->generateLayerForClient(role);
172         if (lid == 0)
173         {
174             return Err<int>("Designated role does not match any role, fallback is disabled");
175         }
176         // add client into the db
177         g_app_list.addClient(str_id, lid, role);
178     }
179     else
180     {
181         // This case occurs when an client calls "subscribe" before request_surface.
182         // Then application doesn't have layer and role yet.
183         auto client = g_app_list.lookUpClient(str_id);
184         if(client->layerID() == 0)
185         {
186             client->setLayerID(this->generateLayerForClient(role));
187         }
188         client->setRole(role);
189     }
190
191     // generate surface ID for ivi-shell application
192     auto rname = this->id_alloc.lookup(role);
193     if (!rname)
194     {
195         // name does not exist yet, allocate surface id...
196         auto id = int(this->id_alloc.generate_id(role));
197         this->tmp_surface2app[id] = {str_id, lid};
198
199         auto client = g_app_list.lookUpClient(str_id);
200         client->registerSurface(id);
201
202         return Ok<int>(id);
203     }
204
205     // Check currently registered drawing names if it is already there.
206     return Err<int>("Surface already present");
207 }
208
209 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
210                                      char const *ivi_id)
211 {
212     string str_id   = appid;
213     string role = drawing_name;
214
215     unsigned sid = std::stol(ivi_id);
216     HMI_DEBUG("This API(requestSurfaceXDG) is for XDG Application using runXDG");
217     /*
218      * IVI-shell doesn't send surface_size event via ivi-wm protocol
219      * if the application is using XDG surface.
220      * So WM has to set surface size with original size here
221      */
222     WMError ret = this->lc->setXDGSurfaceOriginSize(sid);
223     if(ret != SUCCESS)
224     {
225         HMI_ERROR("%s", errorDescription(ret));
226         HMI_WARNING("The main user of this API is runXDG");
227         return "fail";
228     }
229
230     if(!g_app_list.contains(str_id))
231     {
232         unsigned l_id = this->generateLayerForClient(role);
233         if (l_id == 0)
234         {
235             return "Designated role does not match any role, fallback is disabled";
236         }
237         // add client into the db
238         g_app_list.addClient(str_id, l_id, role);
239     }
240     else
241     {
242         // This case occurs when an client calls "subscribe" before request_surface.
243         // Then application doesn't have layer and role yet.
244         auto client = g_app_list.lookUpClient(str_id);
245         if(client->layerID() == 0)
246         {
247             client->setLayerID(this->generateLayerForClient(role));
248         }
249         client->setRole(role);
250     }
251
252     auto rname = this->id_alloc.lookup(role);
253
254     if (rname)
255     {
256         return "Surface already present";
257     }
258
259     // register pair drawing_name and ivi_id
260     this->id_alloc.register_name_id(role, sid);
261
262     auto client = g_app_list.lookUpClient(str_id);
263     client->addSurface(sid);
264
265     return nullptr;
266 }
267
268 void WindowManager::api_activate_window(char const *appid, char const *drawing_name,
269                                char const *drawing_area, const reply_func &reply)
270 {
271     string id = appid;
272     string role = drawing_name;
273     string area = drawing_area;
274
275     if(!g_app_list.contains(id))
276     {
277         reply("app doesn't request 'requestSurface' or 'setRole' yet");
278         return;
279     }
280     auto client = g_app_list.lookUpClient(id);
281
282     Task task = Task::TASK_ALLOCATE;
283     unsigned req_num = 0;
284     WMError ret = WMError::UNKNOWN;
285
286     ret = this->setRequest(id, role, area, task, &req_num);
287
288     if(ret != WMError::SUCCESS)
289     {
290         HMI_ERROR(errorDescription(ret));
291         reply("Failed to set request");
292         return;
293     }
294
295     reply(nullptr);
296     if (req_num != g_app_list.currentRequestNumber())
297     {
298         // Add request, then invoked after the previous task is finished
299         HMI_SEQ_DEBUG(req_num, "request is accepted");
300         return;
301     }
302
303      // Do allocate tasks
304     ret = this->checkPolicy(req_num);
305
306     if (ret != WMError::SUCCESS)
307     {
308         //this->emit_error()
309         HMI_SEQ_ERROR(req_num, errorDescription(ret));
310         g_app_list.removeRequest(req_num);
311         this->processNextRequest();
312     }
313 }
314
315 void WindowManager::api_deactivate_window(char const *appid, char const *drawing_name,
316                                  const reply_func &reply)
317 {
318     // Check Phase
319     string id = appid;
320     string role = drawing_name;
321     string area = ""; //drawing_area;
322     Task task = Task::TASK_RELEASE;
323     unsigned req_num = 0;
324     WMError ret = WMError::UNKNOWN;
325
326     ret = this->setRequest(id, role, area, task, &req_num);
327
328     if (ret != WMError::SUCCESS)
329     {
330         HMI_ERROR(errorDescription(ret));
331         reply("Failed to set request");
332         return;
333     }
334
335     reply(nullptr);
336     if (req_num != g_app_list.currentRequestNumber())
337     {
338         // Add request, then invoked after the previous task is finished
339         HMI_SEQ_DEBUG(req_num, "request is accepted");
340         return;
341     }
342
343     // Do allocate tasks
344     ret = this->checkPolicy(req_num);
345
346     if (ret != WMError::SUCCESS)
347     {
348         //this->emit_error()
349         HMI_SEQ_ERROR(req_num, errorDescription(ret));
350         g_app_list.removeRequest(req_num);
351         this->processNextRequest();
352     }
353 }
354
355 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
356 {
357     string id = appid;
358     string role = drawing_name;
359     unsigned current_req = g_app_list.currentRequestNumber();
360     bool result = g_app_list.setEndDrawFinished(current_req, id, role);
361
362     if (!result)
363     {
364         HMI_ERROR("%s is not in transition state", id.c_str());
365         return;
366     }
367
368     if (g_app_list.endDrawFullfilled(current_req))
369     {
370         // do task for endDraw
371         this->stopTimer();
372         WMError ret = this->doEndDraw(current_req);
373
374         if(ret != WMError::SUCCESS)
375         {
376             //this->emit_error();
377
378             // Undo state of PolicyManager
379             this->pmw.undoState();
380             this->lc->undoUpdate();
381         }
382         this->emitScreenUpdated(current_req);
383         HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
384
385         g_app_list.removeRequest(current_req);
386
387         this->processNextRequest();
388     }
389     else
390     {
391         HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
392         return;
393     }
394 }
395
396 json_object* WindowManager::api_get_area_list()
397 {
398     json_object* ret = json_object_new_object();
399     json_object* jarray = json_object_new_array();
400     unordered_map<string, struct rect> area2size = this->lc->getAreaList();
401     for(const auto& area : area2size)
402     {
403         json_object* j = json_object_new_object();
404         json_object_object_add(j, "name", json_object_new_string(area.first.c_str()));
405         json_object* jrect = json_object_new_object();
406         json_object_object_add(jrect, "x", json_object_new_int(area.second.x));
407         json_object_object_add(jrect, "y", json_object_new_int(area.second.y));
408         json_object_object_add(jrect, "w", json_object_new_int(area.second.w));
409         json_object_object_add(jrect, "h", json_object_new_int(area.second.h));
410         json_object_object_add(j, "rect", jrect);
411         json_object_array_add(jarray, j);
412     }
413     json_object_object_add(ret, "areas", jarray);
414     HMI_DEBUG("area_list: %s", json_object_get_string(ret));
415     return ret;
416 }
417
418 void WindowManager::api_change_area_size(ChangeAreaReq &areas)
419 {
420     // Error check
421     areas.dump();
422     auto client = g_app_list.lookUpClient(areas.appname);
423     WMError ret;
424     if(client == nullptr)
425     {
426         HMI_ERROR("Call register your role with setRole or requestSurface");
427         return;
428     }
429     if(std::find(white_list_area_size_change.begin(),
430                  white_list_area_size_change.end(), client->role()) == white_list_area_size_change.end())
431     {
432         HMI_ERROR("Application %s which has the role %s is not allowed to change area size", client->appID().c_str(), client->role().c_str());
433         return;
434     }
435
436     // Update
437     ret = this->lc->updateAreaList(areas);
438     if(ret != WMError::SUCCESS)
439     {
440         HMI_ERROR("%d : %s", ret, errorDescription(ret));
441         return;
442     }
443     ret = this->lc->getUpdateAreaList(&areas);
444     areas.dump();
445     if(ret != WMError::SUCCESS)
446     {
447         HMI_ERROR("%d : %s", ret, errorDescription(ret));
448         return;
449     }
450
451     // Create Action
452     unsigned req_num;
453     bool found = false;
454     ret = this->setRequest(client->appID(), client->role(), "-", Task::TASK_CHANGE_AREA, &req_num); // area is null
455     if(ret != WMError::SUCCESS)
456     {
457         HMI_SEQ_ERROR(req_num, "%d : %s", ret, errorDescription(ret));
458         return;
459     }
460     for(const auto &update: areas.update_app2area)
461     {
462         // create action
463         auto client = g_app_list.lookUpClient(update.first);
464         if(client == nullptr)
465         {
466             HMI_SEQ_ERROR(req_num, "%s : %s", update.first.c_str(), errorDescription(ret));
467             g_app_list.removeRequest(req_num);
468             this->processNextRequest();
469             return;
470         }
471         ret = g_app_list.setAction(req_num, client, client->role(), update.second, TaskVisible::VISIBLE);
472         if(ret != WMError::SUCCESS)
473         {
474             HMI_SEQ_ERROR(req_num, "Failed to set request");
475             return;
476         }
477     }
478     HMI_SEQ_INFO(req_num, "Area change request");
479     g_app_list.reqDump();
480
481     // Request change size to applications
482     for(const auto &action : g_app_list.getActions(req_num, &found))
483     {
484         struct rect r = this->lc->getAreaSize(action.area);
485         action.client->emitSyncDraw(action.area, r);
486     }
487 }
488
489 bool WindowManager::api_subscribe(afb_req_t req, EventType event_id)
490 {
491     bool ret = false;
492     char* appid = afb_req_get_application_id(req);
493     if(event_id < Event_Val_Min || event_id > Event_Val_Max)
494     {
495         HMI_ERROR("not defined in Window Manager", event_id);
496         free(appid);
497         return ret;
498     }
499     HMI_INFO("%s subscribe %s : %d", appid, kListEventName[event_id].c_str(), event_id);
500     if(event_id == Event_ScreenUpdated)
501     {
502         // Event_ScreenUpdated should be emitted to subscriber
503         afb_event_t event = this->map_afb_event[kListEventName[event_id]];
504         int rc = afb_req_subscribe(req, event);
505         if(rc == 0)
506         {
507             ret = true;
508         }
509     }
510     else if(appid)
511     {
512         string id = appid;
513         if(!g_app_list.contains(id))
514         {
515             g_app_list.addClient(id);
516         }
517         ret = g_app_list.lookUpClient(id)->subscribe(req, kListEventName[event_id]);
518     }
519     else
520     {
521         HMI_ERROR("appid is not set");
522     }
523     free(appid);
524     return ret;
525 }
526
527 result<json_object *> WindowManager::api_get_display_info()
528 {
529     Screen screen = this->lc->getScreenInfo();
530
531     json_object *object = json_object_new_object();
532     json_object_object_add(object, kKeyWidthPixel, json_object_new_int(screen.width()));
533     json_object_object_add(object, kKeyHeightPixel, json_object_new_int(screen.height()));
534     // TODO: set size
535     json_object_object_add(object, kKeyWidthMm, json_object_new_int(0));
536     json_object_object_add(object, kKeyHeightMm, json_object_new_int(0));
537     json_object_object_add(object, kKeyScale, json_object_new_double(this->lc->scale()));
538
539     return Ok<json_object *>(object);
540 }
541
542 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
543 {
544     HMI_DEBUG("called");
545
546     string role = drawing_name;
547
548     // Check drawing name, surface/layer id
549     auto const &surface_id = this->id_alloc.lookup(role);
550     if (!surface_id)
551     {
552         return Err<json_object *>("Surface does not exist");
553     }
554
555     // Set area rectangle
556     struct rect area_info = this->area_info[*surface_id];
557     json_object *object = json_object_new_object();
558     json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
559     json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
560     json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
561     json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
562
563     return Ok<json_object *>(object);
564 }
565
566 /**
567  * proxied events
568  */
569 void WindowManager::surface_created(unsigned surface_id)
570 {
571     // requestSurface
572     if(this->tmp_surface2app.count(surface_id) != 0)
573     {
574         string appid = this->tmp_surface2app[surface_id].appid;
575         auto client = g_app_list.lookUpClient(appid);
576         if(client != nullptr)
577         {
578             WMError ret = client->addSurface(surface_id);
579             HMI_INFO("Add surface %d to \"%s\"", surface_id, appid.c_str());
580             if(ret != WMError::SUCCESS)
581             {
582                 HMI_ERROR("Failed to add surface to client %s", client->appID().c_str());
583             }
584         }
585         this->tmp_surface2app.erase(surface_id);
586     }
587 }
588
589 void WindowManager::surface_removed(unsigned surface_id)
590 {
591     HMI_DEBUG("Delete surface_id %u", surface_id);
592     this->id_alloc.remove_id(surface_id);
593     g_app_list.removeSurface(surface_id);
594 }
595
596 void WindowManager::removeClient(const string &appid)
597 {
598     HMI_DEBUG("Remove clinet %s from list", appid.c_str());
599     auto client = g_app_list.lookUpClient(appid);
600     this->lc->appTerminated(client);
601     g_app_list.removeClient(appid);
602 }
603
604 void WindowManager::exceptionProcessForTransition()
605 {
606     unsigned req_num = g_app_list.currentRequestNumber();
607     HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
608     g_app_list.removeRequest(req_num);
609     HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
610     this->processNextRequest();
611 }
612
613 void WindowManager::timerHandler()
614 {
615     unsigned req_num = g_app_list.currentRequestNumber();
616     HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
617     g_app_list.reqDump();
618     g_app_list.removeRequest(req_num);
619     this->processNextRequest();
620 }
621
622 void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
623 {
624     WMError ret;
625     unsigned req_num = g_app_list.currentRequestNumber();
626
627     if (actions.empty())
628     {
629         if (g_app_list.haveRequest())
630         {
631             HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
632             goto proc_remove_request;
633         }
634         else
635         {
636             HMI_SEQ_DEBUG(req_num, "There is no request");
637             return;
638         }
639     }
640
641     for (auto &act : actions)
642     {
643         if ("" != act.role)
644         {
645             bool found;
646             auto const &surface_id = this->id_alloc.lookup(act.role);
647             if(surface_id == nullopt)
648             {
649                 goto proc_remove_request;
650             }
651             string appid = g_app_list.getAppID(*surface_id, &found);
652             if (!found)
653             {
654                 if (TaskVisible::INVISIBLE == act.visible)
655                 {
656                     // App is killed, so do not set this action
657                     continue;
658                 }
659                 else
660                 {
661                     HMI_SEQ_ERROR(req_num, "appid which is visible is not found");
662                     ret = WMError::FAIL;
663                     goto error;
664                 }
665             }
666             auto client = g_app_list.lookUpClient(appid);
667             act.req_num = req_num;
668             act.client = client;
669         }
670
671         ret = g_app_list.setAction(req_num, act);
672         if (ret != WMError::SUCCESS)
673         {
674             HMI_SEQ_ERROR(req_num, "Setting action is failed");
675             goto error;
676         }
677     }
678
679     HMI_SEQ_DEBUG(req_num, "Start transition.");
680     ret = this->startTransition(req_num);
681     if (ret != WMError::SUCCESS)
682     {
683         if (ret == WMError::NO_LAYOUT_CHANGE)
684         {
685             goto proc_remove_request;
686         }
687         else
688         {
689             HMI_SEQ_ERROR(req_num, "Transition state is failed");
690             goto error;
691         }
692     }
693
694     return;
695
696 error:
697     //this->emit_error()
698     HMI_SEQ_ERROR(req_num, errorDescription(ret));
699     this->pmw.undoState();
700
701 proc_remove_request:
702     g_app_list.removeRequest(req_num);
703     this->processNextRequest();
704 }
705
706 void WindowManager::processError(WMError error)
707 {
708     unsigned req_num = g_app_list.currentRequestNumber();
709
710     //this->emit_error()
711     HMI_SEQ_ERROR(req_num, errorDescription(error));
712     g_app_list.removeRequest(req_num);
713     this->processNextRequest();
714 }
715
716 unsigned WindowManager::generateLayerForClient(const string& role)
717 {
718     string l_name;
719     unsigned lid = this->lc->getNewLayerID(role, &l_name);
720     if (lid == 0)
721     {
722         // register drawing_name as fallback and make it displayed.
723         lid = this->lc->getNewLayerID(string("fallback"));
724         HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role.c_str());
725         if (lid == 0)
726         {
727             return lid;
728         }
729     }
730
731     // TODO: remote layer name is fixed
732     this->lc->createNewLayer(lid, ("Remote" == l_name));
733
734     // add client into the db
735     return lid;
736 }
737
738 WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
739                             Task task, unsigned* req_num)
740 {
741     if (!g_app_list.contains(appid))
742     {
743         return WMError::NOT_REGISTERED;
744     }
745
746     auto client = g_app_list.lookUpClient(appid);
747
748     /*
749      * Queueing Phase
750      */
751     unsigned current = g_app_list.currentRequestNumber();
752     unsigned requested_num = g_app_list.getRequestNumber(appid);
753     if (requested_num != 0)
754     {
755         HMI_SEQ_INFO(requested_num,
756             "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
757         return REQ_REJECTED;
758     }
759
760     WMRequest req = WMRequest(appid, role, area, task);
761     unsigned new_req = g_app_list.addRequest(req);
762     *req_num = new_req;
763     g_app_list.reqDump();
764
765     HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
766
767     return WMError::SUCCESS;
768 }
769
770 WMError WindowManager::checkPolicy(unsigned req_num)
771 {
772     /*
773     * Check Policy
774     */
775     // get current trigger
776     bool found = false;
777     WMError ret = WMError::LAYOUT_CHANGE_FAIL;
778     auto trigger = g_app_list.getRequest(req_num, &found);
779     if (!found)
780     {
781         ret = WMError::NO_ENTRY;
782         return ret;
783     }
784     string req_area = trigger.area;
785
786     // Input event data to PolicyManager
787     if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
788     {
789         HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
790         return ret;
791     }
792
793     // Execute state transition of PolicyManager
794     if (0 > this->pmw.executeStateTransition())
795     {
796         HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
797         return ret;
798     }
799
800     ret = WMError::SUCCESS;
801
802     g_app_list.reqDump();
803
804     return ret;
805 }
806
807 WMError WindowManager::startTransition(unsigned req_num)
808 {
809     bool sync_draw_happen = false;
810     bool found = false;
811     WMError ret = WMError::SUCCESS;
812     auto actions = g_app_list.getActions(req_num, &found);
813     if (!found)
814     {
815         ret = WMError::NO_ENTRY;
816         HMI_SEQ_ERROR(req_num,
817             "Window Manager bug :%s : Action is not set", errorDescription(ret));
818         return ret;
819     }
820
821     g_app_list.reqDump();
822     for (const auto &action : actions)
823     {
824         if (action.visible == TaskVisible::VISIBLE)
825         {
826             sync_draw_happen = true;
827             struct rect r = this->lc->getAreaSize(action.area);
828             action.client->emitSyncDraw(action.area, r);
829         }
830     }
831
832     if (sync_draw_happen)
833     {
834         this->setTimer();
835     }
836     else
837     {
838         // deactivate only, no syncDraw
839         // Make it deactivate here
840         for (const auto &x : actions)
841         {
842             this->lc->visibilityChange(x);
843             x.client->emitVisible(false);
844         }
845         this->lc->renderLayers();
846         ret = WMError::NO_LAYOUT_CHANGE;
847     }
848     return ret;
849 }
850
851 WMError WindowManager::doEndDraw(unsigned req_num)
852 {
853     // get actions
854     bool found;
855     auto actions = g_app_list.getActions(req_num, &found);
856     WMError ret = WMError::SUCCESS;
857     if (!found)
858     {
859         ret = WMError::NO_ENTRY;
860         return ret;
861     }
862
863     HMI_SEQ_INFO(req_num, "do endDraw");
864
865     // layout change and make it visible
866     for (const auto &act : actions)
867     {
868         if(act.visible != TaskVisible::NO_CHANGE)
869         {
870             // layout change
871             ret = this->lc->layoutChange(act);
872             if(ret != WMError::SUCCESS)
873             {
874                 HMI_SEQ_WARNING(req_num,
875                     "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
876                 return ret;
877             }
878             ret = this->lc->visibilityChange(act);
879             act.client->emitVisible((act.visible == VISIBLE));
880
881             if (ret != WMError::SUCCESS)
882             {
883                 HMI_SEQ_WARNING(req_num,
884                     "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
885                 return ret;
886             }
887             HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
888         }
889     }
890     this->lc->renderLayers();
891
892     HMI_SEQ_INFO(req_num, "emit flushDraw");
893
894     for(const auto &act_flush : actions)
895     {
896         if(act_flush.visible == TaskVisible::VISIBLE)
897         {
898             act_flush.client->emitFlushDraw();
899         }
900     }
901
902     return ret;
903 }
904
905 void WindowManager::emitScreenUpdated(unsigned req_num)
906 {
907     // Get visible apps
908     HMI_SEQ_DEBUG(req_num, "emit screen updated");
909     bool found = false;
910     auto actions = g_app_list.getActions(req_num, &found);
911     if (!found)
912     {
913         HMI_SEQ_ERROR(req_num,
914                       "Window Manager bug :%s : Action is not set",
915                       errorDescription(WMError::NO_ENTRY));
916         return;
917     }
918
919     // create json object
920     json_object *j = json_object_new_object();
921     json_object *jarray = json_object_new_array();
922
923     for(const auto& action: actions)
924     {
925         if(action.visible != TaskVisible::INVISIBLE)
926         {
927             json_object_array_add(jarray, json_object_new_string(action.client->appID().c_str()));
928         }
929     }
930     json_object_object_add(j, kKeyIds, jarray);
931     HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
932
933     int ret = afb_event_push(
934         this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
935     if (ret < 0)
936     {
937         HMI_DEBUG("afb_event_push failed: %m");
938     }
939 }
940
941 void WindowManager::setTimer()
942 {
943     struct timespec ts;
944     if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
945         HMI_ERROR("Could't set time (clock_gettime() returns with error");
946         return;
947     }
948
949     HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
950     if (g_timer_ev_src == nullptr)
951     {
952         // firsttime set into sd_event
953         int ret = sd_event_add_time(afb_api_get_event_loop(afbBindingV3root), &g_timer_ev_src,
954             CLOCK_BOOTTIME, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL, 1, processTimerHandler, this);
955         if (ret < 0)
956         {
957             HMI_ERROR("Could't set timer");
958         }
959     }
960     else
961     {
962         // update timer limitation after second time
963         sd_event_source_set_time(g_timer_ev_src, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL);
964         sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
965     }
966 }
967
968 void WindowManager::stopTimer()
969 {
970     unsigned req_num = g_app_list.currentRequestNumber();
971     HMI_SEQ_DEBUG(req_num, "Timer stop");
972     int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
973     if (rc < 0)
974     {
975         HMI_SEQ_ERROR(req_num, "Timer stop failed");
976     }
977 }
978
979 void WindowManager::processNextRequest()
980 {
981     g_app_list.next();
982     g_app_list.reqDump();
983     unsigned req_num = g_app_list.currentRequestNumber();
984     if (g_app_list.haveRequest())
985     {
986         HMI_SEQ_DEBUG(req_num, "Process next request");
987         WMError rc = checkPolicy(req_num);
988         if (rc != WMError::SUCCESS)
989         {
990             HMI_SEQ_ERROR(req_num, errorDescription(rc));
991         }
992     }
993     else
994     {
995         HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
996     }
997 }
998
999 } // namespace wm