Fix place of connection.json
[apps/agl-service-windowmanager.git] / src / wm_layer_control.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 #include <assert.h>
17 #include <unistd.h>
18 #include "wm_layer_control.hpp"
19 #include "wm_layer.hpp"
20 #include "wm_client.hpp"
21 #include "request.hpp"
22 #include "json_helper.hpp"
23
24 #define LC_AREA_PATH "/etc/areas.json"
25 #define LC_LAYER_SETTING_PATH "/etc/layers.json"
26 #define LC_WESTON_SETTING_PATH "/etc/weston.json"
27 #define LC_DEFAULT_AREA "fullscreen"
28 #define BACK_GROUND_LAYER "BackGroundLayer"
29 #define AREA_NAME_RSE1 "rse1.normal.full"
30 #define AREA_NAME_RSE2 "rse2.normal.full"
31 #define AREA_NAME_HUD "hud.normal.full"
32 #define AREA_NAME_HUD_UL "hud.upper.left"
33 #define defaultWestonTimes 60000
34 #define defaultWestonSleep 50
35
36 using std::string;
37 using std::vector;
38 using std::shared_ptr;
39
40 namespace wm {
41
42 LayerControl* g_lc_ctxt;
43
44 static void createCallback_static(ilmObjectType object,
45                             t_ilm_uint id,
46                             t_ilm_bool created,
47                             void* data)
48 {
49     static_cast<LayerControl*>(data)->dispatchCreateEvent(object, id, created);
50 }
51
52 static void surfaceCallback_static(t_ilm_surface surface,
53             struct ilmSurfaceProperties* surface_prop,
54             t_ilm_notification_mask mask)
55 {
56     g_lc_ctxt->dispatchSurfacePropChangeEvent(surface, surface_prop, mask);
57 }
58
59 static void layerCallback_static(t_ilm_layer layer,
60             struct ilmLayerProperties* layer_prop,
61             t_ilm_notification_mask mask)
62 {
63     g_lc_ctxt->dispatchLayerPropChangeEvent(layer, layer_prop, mask);
64 }
65
66 LayerControl::LayerControl(const std::string& root, const std::string& ecu_name)
67 {
68     string area_path = root + LC_AREA_PATH;
69     string layer_path= root + LC_LAYER_SETTING_PATH;
70     string weston_path = root + LC_WESTON_SETTING_PATH;
71
72     this->loadWestonSetting(weston_path);
73
74     // load layers.setting.json
75     WMError ret = this->loadLayerSetting(layer_path);
76     assert(ret == WMError::SUCCESS);
77     // load areas.json
78     ret = this->loadAreasConfigFile(area_path, ecu_name);
79     assert(ret == WMError::SUCCESS);
80 }
81
82 WMError LayerControl::init(const LayerControlCallbacks& cb)
83 {
84     HMI_DEBUG("Initialize of ilm library and display");
85     t_ilm_uint num = 0;
86     t_ilm_uint *ids;
87     ilmErrorTypes rc;
88     struct timespec startTs, endTs;
89
90     if (clock_gettime(CLOCK_BOOTTIME, &startTs) != 0)
91     {
92         HMI_ERROR("Could't set time (clock_gettime() returns with error");
93         goto lc_init_error;
94     }
95
96     endTs = startTs;
97
98     while ((startTs.tv_sec + (this->times / 1000)) > endTs.tv_sec)
99     {
100         rc = ilm_init();
101
102         if (rc == ILM_SUCCESS)
103         {
104             HMI_DEBUG("Weston Connected");
105             break;
106         }
107
108         HMI_ERROR("Wait to start weston ...");
109         usleep(this->sleep * 1000);
110
111         if (clock_gettime(CLOCK_BOOTTIME, &endTs) != 0)
112         {
113             HMI_ERROR("Could't set time (clock_gettime() returns with error");
114             goto lc_init_error;
115         }
116     }
117
118     if(rc != ILM_SUCCESS) goto lc_init_error;
119
120     // Get current screen setting
121     rc = ilm_getScreenIDs(&num, &ids);
122
123     if(rc != ILM_SUCCESS) goto lc_init_error;
124
125     for(unsigned i = 0; i < num; i++)
126     {
127         HMI_INFO("get screen: %d", ids[i]);
128         ilmScreenProperties prop;
129         rc = ilm_getPropertiesOfScreen(ids[i], &prop);
130
131         if(rc != ILM_SUCCESS) goto lc_init_error;
132         this->wm_screens.emplace_back(Screen(ids[i], prop.screenWidth, prop.screenHeight));
133     }
134
135     // Currently, 0 is only available
136     this->screenID = ids[0];
137
138     if(rc != ILM_SUCCESS) goto lc_init_error;
139
140     // Register Callback to Window Manager and from ILM
141     this->cb = cb;
142     ilm_registerNotification(createCallback_static, this);
143
144     return WMError::SUCCESS;
145
146 lc_init_error:
147     HMI_ERROR("Failed to initialize. Terminate WM");
148
149     return WMError::FAIL;
150 }
151
152 void LayerControl::createNewLayer(unsigned id)
153 {
154     HMI_INFO("create new ID :%d", id);
155     struct rect rct = this->area2size[LC_DEFAULT_AREA];
156     ilm_layerCreateWithDimension(&id, rct.w, rct.h);
157     //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
158     ilm_layerSetDestinationRectangle(id, this->offset_x, this->offset_y, rct.w, rct.h);
159     ilm_layerSetOpacity(id, 1.0);
160     ilm_layerSetVisibility(id, ILM_FALSE);
161     ilm_commitChanges();
162     auto wm_layer = getWMLayer(id);
163     wm_layer->addLayerToState(id);
164     this->renderLayers();
165 }
166
167 void LayerControl::createNewRemoteLayer(unsigned id)
168 {
169     HMI_INFO("create new ID :%d (For remote layer)", id);
170     struct rect rct = {640, 720, 0, 0};
171     ilm_layerCreateWithDimension(&id, rct.w, rct.h);
172     //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
173     ilm_layerSetDestinationRectangle(id, this->offset_x, this->offset_y, rct.w, rct.h);
174     ilm_layerSetOpacity(id, 1.0);
175     ilm_layerSetVisibility(id, ILM_FALSE);
176     ilm_commitChanges();
177     auto wm_layer = getWMLayer(id);
178     wm_layer->addLayerToState(id);
179     this->renderLayers();
180 }
181
182 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
183 {
184     unsigned ret = 0;
185     for(const auto& l: this->wm_layers)
186     {
187         ret = l->getNewLayerID(role);
188         if(ret != 0)
189         {
190             *layer_name = l->layerName();
191             unsigned wmlid = l->getWMLayerID();
192             this->lid2wmlid[ret] = wmlid;
193             break;
194         }
195     }
196     return ret;
197 }
198
199 shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
200 {
201     unsigned wm_lid = this->lid2wmlid[layer];
202     return this->wm_layers[wm_lid];
203 }
204
205 std::shared_ptr<WMLayer> LayerControl::getWMLayer(std::string layer_name)
206 {
207     for(auto &l : this->wm_layers)
208     {
209         if(l->layerName() == layer_name)
210         {
211             return l;
212         }
213     }
214     return nullptr;
215 }
216
217 struct rect LayerControl::getAreaSize(const std::string& area)
218 {
219     return area2size[area];
220 }
221
222 void LayerControl::setupArea(const rectangle& base_rct, double scaling)
223 {
224     this->scaling = scaling;
225     this->offset_x = base_rct.left();
226     this->offset_y = base_rct.top();
227
228     // TODO: set all screen
229     this->wm_screens[0].setScale(scaling);
230     this->wm_screens[0].setOffset(offset_x, offset_y);
231
232     for (auto &i : this->area2size)
233     {
234         i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
235         i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
236         i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
237         i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
238
239         HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
240             i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
241     }
242 }
243
244 Screen LayerControl::getScreenInfo()
245 {
246     return wm_screens[0];
247 }
248
249 double LayerControl::scale()
250 {
251     return this->scaling;
252 }
253
254 WMError LayerControl::updateLayer(LayerState& layer_state)
255 {
256     return WMError::SUCCESS;
257 }
258
259 WMError LayerControl::renderLayers()
260 {
261     HMI_INFO("Commit change");
262     WMError rc = WMError::SUCCESS;
263
264     // Create render order for each screen and set render order
265     for(const auto& screen : this->wm_screens)
266     {
267         unsigned size_ids = 0;
268         for(const auto& wm_l : this->wm_layers)
269         {
270             if(wm_l->getScreenID() == screen.id())
271             {
272                 for(const auto& id : wm_l->getLayerState().getIviIdList())
273                 {
274                     ++size_ids;
275                 }
276             }
277         }
278         t_ilm_layer* id_array = new t_ilm_layer[size_ids];
279         if(id_array == nullptr)
280         {
281             HMI_WARNING("short memory");
282             this->undoUpdate();
283             return WMError::FAIL;
284         }
285         int idx = 0;
286         for(const auto& wm_l : this->wm_layers)
287         {
288             if(wm_l->getScreenID() == screen.id())
289             {
290                 for(const auto& id : wm_l->getLayerState().getIviIdList())
291                 {
292                     id_array[idx] = id;
293                     ++idx;
294                 }
295             }
296         }
297
298         // Send to display
299         ilmErrorTypes ret = ilm_displaySetRenderOrder(screen.id(), id_array, size_ids);
300         if(ret != ILM_SUCCESS)
301         {
302             this->undoUpdate();
303             return WMError::FAIL;
304         }
305         else
306         {
307             for(auto& l : this->wm_layers)
308             {
309                 if(l->getScreenID() == screen.id()) {
310                     l->update();
311                 }
312             }
313         }
314         ilm_commitChanges();
315         delete id_array;
316     }
317     return rc;
318 }
319
320 WMError LayerControl::setXDGSurfaceOriginSize(unsigned surface)
321 {
322     WMError ret = WMError::NOT_REGISTERED;
323     ilmSurfaceProperties prop;
324     ilmErrorTypes rc = ilm_getPropertiesOfSurface(surface, &prop);
325     if(rc == ILM_SUCCESS)
326     {
327         HMI_INFO("xdg surface info %d, %d", prop.origSourceWidth, prop.origSourceHeight);
328         ilm_surfaceSetSourceRectangle(surface, 0, 0, prop.origSourceWidth, prop.origSourceHeight);
329         ret = WMError::SUCCESS;
330     }
331     return ret;
332 }
333
334
335 void LayerControl::undoUpdate()
336 {
337     for(auto& l : this->wm_layers)
338     {
339         l->undo();
340     }
341 }
342
343 int LayerControl::loadWestonSetting(const string &path)
344 {
345     HMI_DEBUG("loading Weston(Conpositor Time Out) Setting from %s", path.c_str());
346
347     json_object *json_obj;
348
349     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
350     if (0 > ret)
351     {
352         HMI_ERROR("Could not open %s, so use default", path.c_str());
353         this->times = defaultWestonTimes;
354         this->sleep = defaultWestonSleep;
355         return 0;
356     }
357     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
358
359     long times = jh::getIntFromJson(json_obj, "times");
360     this->times = (0 != times) ? times : defaultWestonTimes;
361
362     long sleep = jh::getIntFromJson(json_obj, "sleep");
363     this->sleep = (0 != sleep) ? sleep : defaultWestonSleep;
364
365     return 0;
366 }
367
368 WMError LayerControl::loadLayerSetting(const string &path)
369 {
370     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
371
372     json_object *json_obj, *json_cfg;
373     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
374     if (0 > ret)
375     {
376         HMI_ERROR("Could not open %s", path.c_str());
377         return WMError::FAIL;
378     }
379     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
380
381     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
382     {
383         HMI_ERROR("Parse Error!!");
384         return WMError::FAIL;
385     }
386
387     int len = json_object_array_length(json_cfg);
388     HMI_DEBUG("json_cfg len:%d", len);
389
390     for (int i = 0; i < len; i++)
391     {
392         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
393         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
394
395         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
396
397         int screen = jh::getIntFromJson(json_tmp, "screen");
398         if (screen > 0)
399         {
400             std::string layerName = jh::getStringFromJson(json_tmp, "name");
401
402             HMI_DEBUG("Remote Layer Name:%s Screen: %d", layerName.c_str(), screen);
403             this->wm_remoteLayerName.push_back(layerName);
404         }
405
406     }
407     json_object_put(json_obj);
408
409     return WMError::SUCCESS;
410 }
411
412 WMError LayerControl::loadAreasConfigFile(const std::string& path, const std::string& ecu_name)
413 {
414     // Load areas config file
415     json_object *json_obj;
416     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
417     if (0 > ret)
418     {
419         HMI_ERROR("Could not open %s", path.c_str());
420         return WMError::FAIL;
421     }
422     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
423
424     // Parse ecus
425     json_object *json_cfg;
426     if (!json_object_object_get_ex(json_obj, "ecus", &json_cfg))
427     {
428         HMI_ERROR("Parse Error!!");
429         return WMError::FAIL;
430     }
431
432     int num_ecu = json_object_array_length(json_cfg);
433     HMI_DEBUG("json_cfg(ecus) len:%d", num_ecu);
434
435     const char* c_ecu_name;
436     json_object *json_ecu;
437     for (int i = 0; i < num_ecu; i++)
438     {
439         json_ecu= json_object_array_get_idx(json_cfg, i);
440
441         c_ecu_name = jh::getStringFromJson(json_ecu, "name");
442         if (nullptr == c_ecu_name)
443         {
444             HMI_ERROR("Parse Error!!");
445             return WMError::FAIL;
446         }
447
448         if (ecu_name == string(c_ecu_name))
449         {
450             break;
451         }
452         else
453         {
454             json_ecu = nullptr;
455         }
456     }
457
458     if (!json_ecu)
459     {
460         HMI_ERROR("Areas for ecu:%s is NOT exist!!", ecu_name.c_str());
461         return WMError::FAIL;
462     }
463
464     // Parse screens
465     if (!json_object_object_get_ex(json_ecu, "screens", &json_cfg))
466     {
467         HMI_ERROR("Parse Error!!");
468         return WMError::FAIL;
469     }
470
471     int num_screen = json_object_array_length(json_cfg);
472     HMI_DEBUG("json_cfg(screens) len:%d", num_screen);
473
474     json_object *json_screen;
475     for (int i = 0; i < num_screen; i++)
476     {
477         int screen_id;
478         json_screen = json_object_array_get_idx(json_cfg, i);
479         HMI_INFO("json_cfg dump:%s", json_object_get_string(json_cfg));
480
481         // TODO: Currently only one display is connected to a ECU.
482
483         screen_id = jh::getIntFromJson(json_screen, "id"); // screen_id is not used currently
484
485         // Parse areas
486         json_object *json_tmp;
487         if (!json_object_object_get_ex(json_screen, "areas", &json_tmp))
488         {
489             HMI_ERROR("Parse Error!!");
490             return WMError::FAIL;
491         }
492
493         int num_area = json_object_array_length(json_tmp);
494         HMI_DEBUG("json_tmp(areas) len:%d", num_area);
495
496         const char *area;
497         for (int j = 0; j < num_area; j++)
498         {
499             json_object *json_area = json_object_array_get_idx(json_tmp, j);
500
501             area = jh::getStringFromJson(json_area, "name");
502             if (nullptr == area)
503             {
504                 HMI_ERROR("Parse Error!!");
505                 return WMError::FAIL;
506             }
507             HMI_DEBUG("> area:%s", area);
508
509             json_object *json_rect;
510             if (!json_object_object_get_ex(json_area, "rect", &json_rect))
511             {
512                 HMI_ERROR("Parse Error!!");
513                 return WMError::FAIL;
514             }
515
516             struct rect area_size;
517             area_size.x = jh::getIntFromJson(json_rect, "x");
518             area_size.y = jh::getIntFromJson(json_rect, "y");
519             area_size.w = jh::getIntFromJson(json_rect, "w");
520             area_size.h = jh::getIntFromJson(json_rect, "h");
521             HMI_INFO("%d, %d, %d, %d, ", area_size.x, area_size.y, area_size.w, area_size.h);
522
523             this->area2size[area] = area_size;
524         }
525
526         // Check
527         for (const auto& itr : this->area2size)
528         {
529             HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
530                       itr.first.c_str(), itr.second.x, itr.second.y,
531                       itr.second.w, itr.second.h);
532         }
533     }
534
535     // Release json_object
536     json_object_put(json_obj);
537
538     return WMError::SUCCESS;
539 }
540
541 WMError LayerControl::layoutChange(const WMAction& action)
542 {
543     if ((action.visible == TaskVisible::INVISIBLE) ||
544         (action.visible == TaskVisible::REQ_REMOTE_VISIBLE) ||
545         (action.visible == TaskVisible::REQ_REMOTE_INVISIBLE))
546     {
547         // Visibility is not change -> no redraw is required
548         return WMError::SUCCESS;
549     }
550     if(action.client == nullptr)
551     {
552         HMI_SEQ_ERROR(action.req_num, "client may vanish");
553         return WMError::NOT_REGISTERED;
554     }
555     unsigned layer = action.client->layerID();
556     unsigned surface = action.client->surfaceID();
557
558     auto rect = this->getAreaSize(action.area);
559     HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
560
561     // TO BE FIXED:
562     // Sometimes, ivi_wm_surface_size signal doesn't reach window manager,
563     // then, Window Manager can't set source size.
564     // This fixes it but it takes about 200ns(on R-Car M3) wastefully
565     ilmSurfaceProperties sp;
566     ilm_getPropertiesOfSurface(surface, &sp);
567     if(sp.origSourceHeight != sp.sourceHeight) {
568         HMI_SEQ_NOTICE(action.req_num, "WORK AROUND: set source size w:%d h%d", sp.origSourceWidth, sp.origSourceHeight);
569         ilm_surfaceSetSourceRectangle(surface, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
570         ilm_commitChanges();
571     }
572
573     ilm_surfaceSetDestinationRectangle(surface, rect.x, rect.y, rect.w, rect.h);
574     ilm_commitChanges();
575     for(auto &wm_layer: this->wm_layers)
576     {
577         // Store the state who is assigned to the area
578         if(wm_layer->hasLayerID(layer))
579         {
580             wm_layer->attachAppToArea(action.client->appID(), action.area);
581             /* TODO: manipulate state directly
582             LayerState ls = wm_layer->getLayerState();
583             ls.seattachAppToAreatArea(action.client->appID(), action.area);
584             wm_layer->dump(); */
585         }
586     }
587
588     return WMError::SUCCESS;
589 }
590
591 WMError LayerControl::visibilityChange(const WMAction& action)
592 {
593     WMError ret = WMError::FAIL;
594     if(action.client == nullptr)
595     {
596         HMI_SEQ_ERROR(action.req_num, "client may vanish");
597         return WMError::NOT_REGISTERED;
598     }
599
600     if (action.visible == TaskVisible::VISIBLE) 
601     {
602         ret = this->makeVisible(action.client);
603     }
604     else if (action.visible == TaskVisible::INVISIBLE) 
605     {
606         ret = this->makeInvisible(action.client);
607     }
608     else if (action.visible == TaskVisible::REMOTE_VISIBLE)
609     {
610         ret = this->makeVisible(action.client);
611         this->moveRemote(action.client->layerID(), action.area);
612     }
613     else if (action.visible == TaskVisible::REMOTE_INVISIBLE)
614     {
615         ret = this->makeInvisible(action.client);
616         this->moveLocal(action.client->layerID());
617     }
618     else // TaskVisible::REQ_REMOTE_VISIBLE || TaskVisible::REQ_REMOTE_INVISIBLE
619     {
620         // Visibility is not change
621         ret = WMError::SUCCESS;
622     }
623
624     return ret;
625 }
626
627 WMError LayerControl::updateAreaList(const ChangeAreaReq& req)
628 {
629     // error check
630     for(const auto& elem : req.area_req)
631     {
632         if(this->area2size.find(elem.first) == this->area2size.end())
633         {
634             HMI_ERROR("requested area %s is not registered in area list", elem.first.c_str());
635             return WMError::NOT_REGISTERED;
636         }
637     }
638     // update list
639     for(const auto& elem : req.area_req)
640     {
641         this->area2size[elem.first] = elem.second;
642     }
643     if(req.save)
644     {
645         HMI_NOTICE("Not implemented");
646         // TODO
647     }
648     return WMError::SUCCESS;
649 }
650
651 WMError LayerControl::getUpdateAreaList(ChangeAreaReq *req)
652 {
653     for(const auto& wm_layer : this->wm_layers)
654     {
655         // get area name and compare it with elem
656         for(const auto& area : req->area_req)
657         {
658             string app = wm_layer->attachedApp(area.first);
659             if(!app.empty())
660             {
661                 HMI_INFO("app %s changes area %s", app.c_str(), area.first.c_str());
662                 req->update_app2area[app] = area.first;
663             }
664         }
665     }
666     return WMError::SUCCESS;
667 }
668
669 void LayerControl::appTerminated(const shared_ptr<WMClient> client)
670 {
671     for(auto& l : this->wm_layers)
672     {
673         if(l->hasLayerID(client->layerID()))
674         {
675             l->appTerminated(client->layerID());
676         }
677     }
678 }
679
680 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
681 {
682     if (ILM_SURFACE == object)
683     {
684         if (created)
685         {
686             ilmSurfaceProperties sp;
687             ilmErrorTypes rc;
688             rc = ilm_getPropertiesOfSurface(id, &sp);
689             if(rc != ILM_SUCCESS)
690             {
691                 HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
692                 return;
693             }
694             this->cb.surfaceCreated(sp.creatorPid, id);
695             ilm_surfaceSetSourceRectangle(id, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
696             ilm_surfaceAddNotification(id, surfaceCallback_static);
697             ilm_surfaceSetVisibility(id, ILM_TRUE);
698             ilm_surfaceSetType(id, ILM_SURFACETYPE_DESKTOP);
699         }
700         else
701         {
702             this->cb.surfaceDestroyed(id);
703         }
704     }
705     if (ILM_LAYER == object)
706     {
707         if(created)
708         {
709             ilm_layerAddNotification(id, layerCallback_static);
710         }
711         else
712         {
713             // Ignore here. Nothing to do currently.
714             // Process of application dead is handled by Window Manager
715             // from binder notification
716         }
717     }
718 }
719
720 void LayerControl::dispatchSurfacePropChangeEvent(unsigned id,
721         struct ilmSurfaceProperties* sprop,
722         t_ilm_notification_mask mask)
723 {
724     /*
725       ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
726       are not handled here, handled in create/destroy event
727      */
728     if (ILM_NOTIFICATION_VISIBILITY & mask)
729     {
730         HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
731     }
732     if (ILM_NOTIFICATION_OPACITY & mask)
733     {
734         HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
735     }
736     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
737     {
738         HMI_DEBUG("surface %d source rect changes", id);
739     }
740     if (ILM_NOTIFICATION_DEST_RECT & mask)
741     {
742         HMI_DEBUG("surface %d dest rect changes", id);
743     }
744     if (ILM_NOTIFICATION_CONFIGURED & mask)
745     {
746         HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
747             sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
748         ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
749     }
750 }
751
752 void LayerControl::dispatchLayerPropChangeEvent(unsigned id,
753         struct ilmLayerProperties* lprop,
754         t_ilm_notification_mask mask)
755 {
756     if (ILM_NOTIFICATION_VISIBILITY & mask)
757     {
758         HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
759     }
760     if (ILM_NOTIFICATION_OPACITY & mask)
761     {
762         HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
763     }
764     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
765     {
766         HMI_DEBUG("layer %d source rect changes", id);
767     }
768     if (ILM_NOTIFICATION_DEST_RECT & mask)
769     {
770         HMI_DEBUG("layer %d dest rect changes", id);
771     }
772 }
773
774 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
775 {
776     WMError ret = WMError::SUCCESS;
777     // Don't check here wheher client is nullptr or not
778     unsigned layer = client->layerID();
779
780     this->moveForeGround(client);
781
782     ilm_layerSetVisibility(layer, ILM_TRUE);
783
784     return ret;
785 }
786
787 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
788 {
789     WMError ret = WMError::SUCCESS;
790     // Don't check here the client is not nullptr
791     unsigned layer = client->layerID();
792
793     bool mv_ok = this->moveBackGround(client);
794
795     if(!mv_ok)
796     {
797         HMI_INFO("make invisible client %s", client->appID().c_str());
798         ilm_layerSetVisibility(layer, ILM_FALSE);
799     }
800
801     return ret;
802 }
803
804 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
805 {
806     bool ret = false;
807
808     // Move background from foreground layer
809     auto bg = this->getWMLayer(BACK_GROUND_LAYER);
810     if(bg != nullptr)
811     {
812         HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
813         unsigned layer = client->layerID();
814         if(bg->hasRole(client->role()))
815         {
816             HMI_INFO("%s go to background", client->appID().c_str());
817             bg->addLayerToState(layer);
818             auto wm_layer = this->getWMLayer(layer);
819             wm_layer->removeLayerFromState(layer);
820             /* TODO: manipulate state directly
821             LayerState bg_ls = bg->getLayerState();
822             bg_ls.addLayer(layer);
823             LayerState ls = wm_layer->getLayerState();
824             ls.removeLayer(layer); */
825             bg->dump();
826             wm_layer->dump();
827             ret = true;
828         }
829     }
830     return ret;
831 }
832
833 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
834 {
835     bool ret = false;
836
837     // Move foreground from foreground layer
838     auto bg = this->getWMLayer(BACK_GROUND_LAYER);
839     if(bg != nullptr)
840     {
841         if(bg->hasRole(client->role()))
842         {
843             unsigned layer = client->layerID();
844             HMI_INFO("%s go to foreground", client->appID().c_str());
845             bg->removeLayerFromState(layer);
846             auto wm_layer = this->getWMLayer(layer);
847             wm_layer->addLayerToState(layer);
848             /* TODO: manipulate state directly
849             LayerState bg_ls = bg->getLayerState();
850             bg_ls.removeLayer(layer);
851             LayerState ls = wm_layer->getLayerState();
852             ls.addLayer(layer); */
853             bg->dump();
854             wm_layer->dump();
855             ret = true;
856         }
857     }
858     return ret;
859 }
860
861 bool LayerControl::moveRemote(unsigned layer, const std::string& area)
862 {
863     bool ret = false;
864     std::string remote_layer = this->areaToRemoteLayer(area);
865     auto remote = this->getWMLayer(remote_layer);
866     
867     if(remote != nullptr)
868     {
869         ret = true;
870         if(remote->hasLayerID(layer))
871         {
872             HMI_DEBUG("Already moved layer %d", layer);
873             remote->dump();
874             return ret;
875         }
876
877         shared_ptr<WMLayer> wm_layer;
878         std::string wm_layer_name = this->hasRemoteLayer(layer);
879
880         if(wm_layer_name == "")
881         {
882             wm_layer = this->getWMLayer(layer);
883         }
884         else
885         {
886             wm_layer = this->getWMLayer(wm_layer_name);
887         }
888
889         remote->addLayerToState(layer);
890         wm_layer->removeLayerFromState(layer);
891         wm_layer->dump();
892         remote->dump();
893     }
894
895     return ret;
896 }
897
898 bool LayerControl::moveLocal(unsigned layer)
899 {
900     bool ret = false;
901     std::string remote_layer;
902
903     remote_layer = this->hasRemoteLayer(layer);
904
905     auto remote = this->getWMLayer(remote_layer);
906     if(remote != nullptr)
907     {
908         remote->removeLayerFromState(layer);
909         auto wm_layer = this->getWMLayer(layer);
910         wm_layer->addLayerToState(layer);
911         wm_layer->dump();
912         remote->dump();
913         ret = true;
914     }
915
916     return ret;
917 }
918
919 std::string LayerControl::hasRemoteLayer(unsigned layer)
920 {
921     for (auto itr = wm_remoteLayerName.begin(); itr != wm_remoteLayerName.end(); ++itr)
922     {
923         auto remote = this->getWMLayer(*itr);
924         if(remote != nullptr)
925         {
926             if(remote->hasLayerFromState(layer))
927             {
928                 return *itr;
929             }
930         }
931     }
932
933     return "";
934 }
935
936 std::string LayerControl::areaToRemoteLayer(const std::string& area)
937 {
938     std::vector<std::string> elements;
939
940     if (area.find('.') != std::string::npos)
941     {
942         elements = parseString(area, '.');
943     }
944     else
945     {
946         elements.push_back(area);
947     }
948
949     for (auto itr = wm_remoteLayerName.begin(); itr != wm_remoteLayerName.end(); ++itr)
950     {
951         unsigned int idx = 0;
952         for (auto itr_elem = elements.begin(); itr_elem != elements.end(); ++itr_elem)
953         {
954             if (std::regex_search(*itr, std::regex(*itr_elem, std::regex::icase)))
955             {
956                 if (++idx == elements.size())
957                 {
958                     return *itr;
959                 }
960             }
961             else
962             {
963                 // TODO: workaround, skip normal case
964                 if ((itr_elem->compare("normal") == 0) || (itr_elem->compare("full") == 0))
965                 {
966                     if (++idx == elements.size())
967                     {
968                         return *itr;
969                     }
970                 }
971                 else
972                 {
973                     break;
974                 }
975             }
976         }
977     }
978
979     return wm_remoteLayerName[0];
980 }
981 } // namespace wm