5319b2edd226b33519ae3ca49d70686e50c2f0dc
[apps/agl-service-windowmanager-2017.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.db"
25 #define LC_LAYER_SETTING_PATH "/etc/layers_setting.json"
26 #define LC_DEFAULT_AREA "fullscreen"
27
28 using std::string;
29 using std::vector;
30 using std::shared_ptr;
31
32 namespace wm {
33
34 LayerControl* g_lc_ctxt;
35
36 static void createCallback_static(ilmObjectType object,
37                             t_ilm_uint id,
38                             t_ilm_bool created,
39                             void* data)
40 {
41     static_cast<LayerControl*>(data)->dispatchCreateEvent(object, id, created);
42 }
43
44 static void surfaceCallback_static(t_ilm_surface surface,
45             struct ilmSurfaceProperties* surface_prop,
46             t_ilm_notification_mask mask)
47 {
48     g_lc_ctxt->dispatchPropertyChangeEvent(surface, surface_prop, mask);
49 }
50
51 static void layerCallback_static(t_ilm_layer layer,
52             struct ilmLayerProperties* layer_prop,
53             t_ilm_notification_mask mask)
54 {
55     g_lc_ctxt->dispatchPropertyChangeEvent(layer, layer_prop, mask);
56 }
57
58 LayerControl::LayerControl(const std::string& root)
59 {
60     string area_path = root + LC_AREA_PATH;
61     string layer_path= root + LC_LAYER_SETTING_PATH;
62     // load layers.setting.json
63     WMError ret = this->loadLayerSetting(layer_path);
64     assert(ret == WMError::SUCCESS);
65     // load area.db
66     ret = this->loadAreaDb(area_path);
67     assert(ret == WMError::SUCCESS);
68 }
69
70 WMError LayerControl::init(const LayerControlCallbacks& cb)
71 {
72     HMI_DEBUG("Initialize of ilm library and display");
73     t_ilm_uint num = 0;
74     t_ilm_uint *ids;
75     int cnt = 0;
76     ilmErrorTypes rc = ilm_init();
77
78     while (rc != ILM_SUCCESS)
79     {
80         cnt++;
81         if (20 <= cnt)
82         {
83             HMI_ERROR("Could not connect to compositor");
84             goto lc_init_error;
85         }
86         HMI_ERROR("Wait to start weston ...");
87         sleep(1);
88         rc = ilm_init();
89     }
90     if(rc != ILM_SUCCESS) goto lc_init_error;
91
92     // Get current screen setting
93     rc = ilm_getScreenIDs(&num, &ids);
94
95     if(rc != ILM_SUCCESS) goto lc_init_error;
96
97     for(unsigned i = 0; i < num; i++)
98     {
99         HMI_INFO("get screen: %d", ids[i]);
100     }
101     // Currently, 0 is only available
102     this->screenID = ids[0];
103
104     rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);
105
106     if(rc != ILM_SUCCESS) goto lc_init_error;
107
108     // Register Callback from ILM
109     this->cb = cb;
110     ilm_registerNotification(createCallback_static, this);
111
112     return WMError::SUCCESS;
113
114 lc_init_error:
115     HMI_ERROR("Failed to initialize. Terminate WM");
116
117     return WMError::FAIL;
118 }
119
120 void LayerControl::createNewLayer(unsigned id)
121 {
122     HMI_INFO("create new ID :%d", id);
123     struct rect rct = this->area2size[LC_DEFAULT_AREA];
124     ilm_layerCreateWithDimension(&id, rct.w, rct.h);
125     //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
126     //ilm_layerSetDestinationRectangle(id, rct.x, rct.y, rct.w, rct.h);
127     ilm_layerSetOpacity(id, 1.0);
128     ilm_layerSetVisibility(id, ILM_FALSE);
129     ilm_commitChanges();
130     auto wm_layer = getWMLayer(id);
131     wm_layer->addLayer(id);
132     this->commitChange();
133 }
134
135 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
136 {
137     unsigned ret = 0;
138     for(const auto& l: this->wm_layers)
139     {
140         ret = l->getNewLayerID(role);
141         if(ret != 0)
142         {
143             *layer_name = l->layerName();
144             unsigned uid = l->getUuid();
145             this->lid2wmlid[ret] = uid;
146             break;
147         }
148     }
149     return ret;
150 }
151
152 shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
153 {
154     unsigned uuid = this->lid2wmlid[layer];
155     return this->wm_layers[uuid];
156 }
157
158 struct rect LayerControl::getAreaSize(const std::string& area)
159 {
160     return area2size[area];
161 }
162
163 void LayerControl::setupArea(const rectangle& base_rct, double scaling)
164 {
165     struct rect rct;
166     this->scaling = scaling;
167
168     rct = this->area2size["normal.full"];
169     this->area2size["normalfull"] = rct;
170     this->area2size["normal"] = rct;
171
172     for (auto &i : this->area2size)
173     {
174         i.second.x = base_rct.left() + static_cast<int>(scaling * i.second.x + 0.5);
175         i.second.y = base_rct.top() + static_cast<int>(scaling * i.second.y + 0.5);
176         i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
177         i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
178
179         HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
180             i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
181     }
182 }
183
184 Screen LayerControl::getScreenInfo()
185 {
186     return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
187 }
188
189 double LayerControl::scale()
190 {
191     return this->scaling;
192 }
193
194 WMError LayerControl::updateLayer(LayerState& layer_state)
195 {
196     return WMError::SUCCESS;
197 }
198
199 WMError LayerControl::commitChange()
200 {
201     HMI_INFO("Commit change");
202     WMError rc = WMError::SUCCESS;
203     vector<unsigned> ivi_l_ids;
204     for(auto& l : this->wm_layers)
205     {
206         auto state = l->getLayerState();
207         for(const auto& id : state.getIviIdList())
208         {
209             ivi_l_ids.push_back(id);
210         }
211     }
212     t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
213     if(id_array == nullptr)
214     {
215         HMI_WARNING("short memory");
216         this->undoUpdate();
217         return WMError::FAIL;
218     }
219     int count = 0;
220     for(const auto& i : ivi_l_ids)
221     {
222         id_array[count] = i;
223         HMI_DEBUG("check render order %d", i);
224         ++count;
225     }
226
227     ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
228     if(ret != ILM_SUCCESS)
229     {
230         this->undoUpdate();
231         rc = WMError::FAIL;
232     }
233     ilm_commitChanges();
234     delete id_array;
235     return rc;
236 }
237
238 void LayerControl::undoUpdate() {}
239
240 WMError LayerControl::loadLayerSetting(const string &path)
241 {
242     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
243
244     json_object *json_obj, *json_cfg;
245     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
246     if (0 > ret)
247     {
248         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
249         return WMError::FAIL;
250     }
251     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
252
253     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
254     {
255         HMI_ERROR("Parse Error!!");
256         return WMError::FAIL;
257     }
258
259     int len = json_object_array_length(json_cfg);
260     HMI_DEBUG("json_cfg len:%d", len);
261
262     for (int i = 0; i < len; i++)
263     {
264         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
265         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
266
267         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
268     }
269     json_object_put(json_obj);
270
271     return WMError::SUCCESS;
272 }
273
274 WMError LayerControl::loadAreaDb(const std::string& path)
275 {
276     // Load area.db
277     json_object *json_obj;
278     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
279     if (0 > ret)
280     {
281         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
282         return WMError::FAIL;
283     }
284     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
285
286     // Perse areas
287     json_object *json_cfg;
288     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
289     {
290         HMI_ERROR("Parse Error!!");
291         return WMError::FAIL;
292     }
293
294     int len = json_object_array_length(json_cfg);
295     HMI_DEBUG("json_cfg len:%d", len);
296
297     const char *area;
298     for (int i = 0; i < len; i++)
299     {
300         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
301         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
302
303         area = jh::getStringFromJson(json_tmp, "name");
304         if (nullptr == area)
305         {
306             HMI_ERROR("Parse Error!!");
307             return WMError::FAIL;
308         }
309         HMI_DEBUG("> area:%s", area);
310
311         json_object *json_rect;
312         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
313         {
314             HMI_ERROR("Parse Error!!");
315             return WMError::FAIL;
316         }
317         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
318
319         struct rect area_size;
320         area_size.x = jh::getIntFromJson(json_rect, "x");
321         area_size.y = jh::getIntFromJson(json_rect, "y");
322         area_size.w = jh::getIntFromJson(json_rect, "w");
323         area_size.h = jh::getIntFromJson(json_rect, "h");
324
325         this->area2size[area] = area_size;
326     }
327
328     // Check
329     for (const auto& itr : this->area2size)
330     {
331         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
332                   itr.first.c_str(), itr.second.x, itr.second.y,
333                   itr.second.w, itr.second.h);
334     }
335
336     // Release json_object
337     json_object_put(json_obj);
338
339     return WMError::SUCCESS;
340 }
341
342 WMError LayerControl::layoutChange(const WMAction& action)
343 {
344     WMError ret = WMError::FAIL;
345     if (action.visible == TaskVisible::INVISIBLE)
346     {
347         // Visibility is not change -> no redraw is required
348         return WMError::SUCCESS;
349     }
350     if(action.client == nullptr)
351     {
352         HMI_SEQ_ERROR(action.req_num, "client may vanish");
353         return WMError::NOT_REGISTERED;
354     }
355     unsigned layer = action.client->layerID();
356
357     // Layout Manager
358     // WMError ret = this->setLayerSize(layer, action.area);
359     auto rect = this->getAreaSize(action.area);
360     HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
361     ilmErrorTypes err = ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
362     for(auto &wm_layer: this->wm_layers)
363     {
364         if(wm_layer->hasLayerID(layer))
365         {
366             LayerState ls = wm_layer->getLayerState();
367             ls.setArea(action.client->appID(), action.area);
368         }
369     }
370     if(err == ILM_SUCCESS)
371     {
372         ret = WMError::SUCCESS;
373     }
374     return ret;
375 }
376
377 WMError LayerControl::visibilityChange(const WMAction& action)
378 {
379     WMError ret = WMError::FAIL;
380     if(action.client == nullptr)
381     {
382         HMI_SEQ_ERROR(action.req_num, "client may vanish");
383         return WMError::NOT_REGISTERED;
384     }
385
386     if (action.visible != TaskVisible::INVISIBLE)
387     {
388         ret = this->makeVisible(action.client);
389     }
390     else
391     {
392         ret = this->makeInvisible(action.client);
393     }
394     return ret;
395 }
396
397 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
398 {
399     if (ILM_SURFACE == object)
400     {
401         if (created)
402         {
403             ilmSurfaceProperties sp;
404             ilmErrorTypes rc;
405             rc = ilm_getPropertiesOfSurface(id, &sp);
406             if(rc != ILM_SUCCESS)
407                 return;
408             this->cb.surfaceCreated(sp.creatorPid, id);
409             ilm_surfaceAddNotification(id, surfaceCallback_static);
410             ilm_surfaceSetVisibility(id, ILM_TRUE);
411         }
412         else
413         {
414             // this->cb->surfaceDestroyed(id);
415         }
416     }
417     if (ILM_LAYER == object)
418     {
419         if(created)
420         {
421             ilm_layerAddNotification(id, layerCallback_static);
422             // this->cb->layerCreated(id);
423         }
424         else
425         {
426             // this->cb->layerDestroyed(id); // Means Application is dead.
427         }
428     }
429 }
430
431 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
432         struct ilmSurfaceProperties* sprop,
433         t_ilm_notification_mask mask)
434 {
435     pid_t pid = sprop->creatorPid;
436     HMI_DEBUG("pid : %d", pid);
437
438     if (ILM_NOTIFICATION_VISIBILITY & mask)
439     {
440         //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
441     }
442     if (ILM_NOTIFICATION_OPACITY & mask)
443     {
444     }
445     if (ILM_NOTIFICATION_ORIENTATION & mask)
446     {
447     }
448     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
449     {
450         // this->cb->surfaceSourceRectChanged(id, )
451     }
452     if (ILM_NOTIFICATION_DEST_RECT & mask)
453     {
454         // this->cb->surfaceSourceRectChanged(id, )
455     }
456     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
457     {
458     }
459     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
460     {
461         /* application being down */
462         // m_appLayers.remove(pid);
463     }
464     if (ILM_NOTIFICATION_CONFIGURED & mask)
465     {
466         HMI_DEBUG("surface %d available", id);
467         ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
468         ilm_surfaceSetDestinationRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
469         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
470         qDebug("  surfaceProperties %d", surface);
471         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
472         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
473
474         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
475             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
476             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
477         } else {
478             ilmErrorTypes result;
479             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
480
481             if (layer != 0) {
482                 configureAppSurface(surface,
483                                     surfaceProperties->origSourceWidth,
484                                     surfaceProperties->origSourceHeight);
485
486                 result = ilm_layerAddSurface(layer, surface);
487                 if (result != ILM_SUCCESS) {
488                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
489                 }
490                 ilm_commitChanges();
491             }
492         }
493         updateScreen(); */
494     }
495 }
496
497 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
498         struct ilmLayerProperties* lprop,
499         t_ilm_notification_mask mask)
500 {
501     if (ILM_NOTIFICATION_VISIBILITY & mask)
502     {
503         //this->cb->layerVisibilityChanged(id, sprop->visibility);
504     }
505     if (ILM_NOTIFICATION_OPACITY & mask)
506     {
507     }
508     if (ILM_NOTIFICATION_ORIENTATION & mask)
509     {
510     }
511     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
512     {
513         // this->cb->surfaceSourceRectChanged(id, )
514     }
515     if (ILM_NOTIFICATION_DEST_RECT & mask)
516     {
517         // this->cb->surfaceSourceRectChanged(id, )
518     }
519     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
520     {
521     }
522     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
523     {
524         /* application being down */
525         // m_appLayers.remove(pid);
526     }
527     if (ILM_NOTIFICATION_CONFIGURED & mask)
528     {
529         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
530         qDebug("  surfaceProperties %d", surface);
531         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
532         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
533
534         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
535             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
536             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
537         } else {
538             ilmErrorTypes result;
539             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
540
541             if (layer != 0) {
542                 configureAppSurface(surface,
543                                     surfaceProperties->origSourceWidth,
544                                     surfaceProperties->origSourceHeight);
545
546                 result = ilm_layerAddSurface(layer, surface);
547                 if (result != ILM_SUCCESS) {
548                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
549                 }
550                 ilm_commitChanges();
551             }
552         }
553         updateScreen(); */
554     }
555 }
556
557 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
558 {
559     WMError ret = WMError::SUCCESS;
560     // Don't check here the client is not nullptr
561     unsigned layer = client->layerID();
562
563     ilm_layerSetVisibility(layer, ILM_TRUE);
564
565     /* for(auto& wm_layer : this->wm_layers)
566     {
567         if(wm_layer->hasLayerID(layer))
568         {
569             LayerState ls = wm_layer->getLayerState();
570             ls.addLayer(layer);;
571         }
572     } */
573
574     // Move foreground from back ground layer
575     /* for(auto& wm_layer : this->wm_layers)
576     {
577         if(wm_layer->layerName() == "BackGroundLayer")
578         {
579             if(wm_layer->hasRole(client->role()))
580             {
581                 LayerState ls = wm_layer->getLayerState();
582                 ls.removeLayer(layer);
583             }
584             break;
585         }
586     } */
587
588     return ret;
589 }
590
591 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
592 {
593     WMError ret = WMError::SUCCESS;
594     unsigned layer = client->layerID(); // Don't check here the client is not nullptr
595
596     /* bool mv_ok = this->mvBackGround(client);
597
598     if(!mv_ok)
599     {
600         ilm_layerSetVisibility(layer, ILM_FALSE);
601     } */
602
603     ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
604
605     /* for(auto& wm_layer : this->wm_layers)
606     {
607         if(wm_layer->hasLayerID(layer))
608         {
609             LayerState ls = wm_layer->getLayerState();
610             ls.removeLayer(layer);;
611         }
612     } */
613
614
615
616     return ret;
617 }
618
619 /* bool LayerControl::mvBackGround(const shared_ptr<WMClient> client)
620 {
621     bool ret = false;
622
623     // Move background from foreground layer
624     auto bg = this->getWMLayer("BackGroundLayer");
625     if(bg != nullptr)
626     {
627         unsigned layer = client->layerID();
628         if(bg->hasRole(client->role()))
629         {
630             LayerState bg_ls = bg->getLayerState();
631             bg_ls.addLayer(layer);
632             auto wm_layer = this->getWMLayer(layer);
633             LayerState ls = wm_layer->getLayerState();
634             ls.removeLayer(layer);
635         }
636         ret = true;
637     }
638     return ret;
639 }
640
641 bool LayerControl::mvForeGround(const shared_ptr<WMClient> client)
642 {
643     bool ret = false;
644
645     // Move foreground from foreground layer
646     auto bg = this->getWMLayer("BackGroundLayer");
647     if(bg != nullptr)
648     {
649         unsigned layer = client->layerID();
650         if(bg->hasRole(client->role()))
651         {
652             LayerState bg_ls = bg->getLayerState();
653             bg_ls.removeLayer(layer);
654             auto wm_layer = this->getWMLayer(layer);
655             LayerState ls = wm_layer->getLayerState();
656             ls.addLayer(layer);
657         }
658         ret = true;
659     }
660     return ret;
661 }
662
663 */
664
665 } // namespace wm