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