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