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