Change size (0, 0) if layer turns invisible
[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(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 = static_cast<int>(scaling * i.second.x + 0.5);
175         i.second.y = 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     ilmErrorTypes err = ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
361     for(auto &wm_layer: this->wm_layers)
362     {
363         if(wm_layer->hasLayerID(layer))
364         {
365             LayerState ls = wm_layer->getLayerState();
366             ls.setArea(action.client->appID(), action.area);
367         }
368     }
369     if(err == ILM_SUCCESS)
370     {
371         ret = WMError::SUCCESS;
372     }
373     return ret;
374 }
375
376 WMError LayerControl::visibilityChange(const WMAction& action)
377 {
378     WMError ret = WMError::FAIL;
379     if(action.client == nullptr)
380     {
381         HMI_SEQ_ERROR(action.req_num, "client may vanish");
382         return WMError::NOT_REGISTERED;
383     }
384
385     if (action.visible != TaskVisible::INVISIBLE)
386     {
387         ret = this->makeVisible(action.client);
388     }
389     else
390     {
391         ret = this->makeInvisible(action.client);
392     }
393     return ret;
394 }
395
396 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
397 {
398     if (ILM_SURFACE == object)
399     {
400         if (created)
401         {
402             ilmSurfaceProperties sp;
403             ilmErrorTypes rc;
404             rc = ilm_getPropertiesOfSurface(id, &sp);
405             if(rc != ILM_SUCCESS)
406                 return;
407             this->cb.surfaceCreated(sp.creatorPid, id);
408             ilm_surfaceAddNotification(id, surfaceCallback_static);
409             ilm_surfaceSetVisibility(id, ILM_TRUE);
410         }
411         else
412         {
413             // this->cb->surfaceDestroyed(id);
414         }
415     }
416     if (ILM_LAYER == object)
417     {
418         if(created)
419         {
420             ilm_layerAddNotification(id, layerCallback_static);
421             // this->cb->layerCreated(id);
422         }
423         else
424         {
425             // this->cb->layerDestroyed(id); // Means Application is dead.
426         }
427     }
428 }
429
430 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
431         struct ilmSurfaceProperties* sprop,
432         t_ilm_notification_mask mask)
433 {
434     pid_t pid = sprop->creatorPid;
435     HMI_DEBUG("pid : %d", pid);
436
437     if (ILM_NOTIFICATION_VISIBILITY & mask)
438     {
439         //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
440     }
441     if (ILM_NOTIFICATION_OPACITY & mask)
442     {
443     }
444     if (ILM_NOTIFICATION_ORIENTATION & mask)
445     {
446     }
447     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
448     {
449         // this->cb->surfaceSourceRectChanged(id, )
450     }
451     if (ILM_NOTIFICATION_DEST_RECT & mask)
452     {
453         // this->cb->surfaceSourceRectChanged(id, )
454     }
455     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
456     {
457     }
458     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
459     {
460         /* application being down */
461         // m_appLayers.remove(pid);
462     }
463     if (ILM_NOTIFICATION_CONFIGURED & mask)
464     {
465         HMI_DEBUG("surface %d available", id);
466         ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
467         ilm_surfaceSetDestinationRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
468         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
469         qDebug("  surfaceProperties %d", surface);
470         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
471         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
472
473         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
474             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
475             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
476         } else {
477             ilmErrorTypes result;
478             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
479
480             if (layer != 0) {
481                 configureAppSurface(surface,
482                                     surfaceProperties->origSourceWidth,
483                                     surfaceProperties->origSourceHeight);
484
485                 result = ilm_layerAddSurface(layer, surface);
486                 if (result != ILM_SUCCESS) {
487                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
488                 }
489                 ilm_commitChanges();
490             }
491         }
492         updateScreen(); */
493     }
494 }
495
496 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
497         struct ilmLayerProperties* lprop,
498         t_ilm_notification_mask mask)
499 {
500     if (ILM_NOTIFICATION_VISIBILITY & mask)
501     {
502         //this->cb->layerVisibilityChanged(id, sprop->visibility);
503     }
504     if (ILM_NOTIFICATION_OPACITY & mask)
505     {
506     }
507     if (ILM_NOTIFICATION_ORIENTATION & mask)
508     {
509     }
510     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
511     {
512         // this->cb->surfaceSourceRectChanged(id, )
513     }
514     if (ILM_NOTIFICATION_DEST_RECT & mask)
515     {
516         // this->cb->surfaceSourceRectChanged(id, )
517     }
518     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
519     {
520     }
521     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
522     {
523         /* application being down */
524         // m_appLayers.remove(pid);
525     }
526     if (ILM_NOTIFICATION_CONFIGURED & mask)
527     {
528         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
529         qDebug("  surfaceProperties %d", surface);
530         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
531         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
532
533         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
534             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
535             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
536         } else {
537             ilmErrorTypes result;
538             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
539
540             if (layer != 0) {
541                 configureAppSurface(surface,
542                                     surfaceProperties->origSourceWidth,
543                                     surfaceProperties->origSourceHeight);
544
545                 result = ilm_layerAddSurface(layer, surface);
546                 if (result != ILM_SUCCESS) {
547                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
548                 }
549                 ilm_commitChanges();
550             }
551         }
552         updateScreen(); */
553     }
554 }
555
556 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
557 {
558     WMError ret = WMError::SUCCESS;
559     // Don't check here the client is not nullptr
560     unsigned layer = client->layerID();
561
562     ilm_layerSetVisibility(layer, ILM_TRUE);
563
564     /* for(auto& wm_layer : this->wm_layers)
565     {
566         if(wm_layer->hasLayerID(layer))
567         {
568             LayerState ls = wm_layer->getLayerState();
569             ls.addLayer(layer);;
570         }
571     } */
572
573     // Move foreground from back ground layer
574     /* for(auto& wm_layer : this->wm_layers)
575     {
576         if(wm_layer->layerName() == "BackGroundLayer")
577         {
578             if(wm_layer->hasRole(client->role()))
579             {
580                 LayerState ls = wm_layer->getLayerState();
581                 ls.removeLayer(layer);
582             }
583             break;
584         }
585     } */
586
587     return ret;
588 }
589
590 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
591 {
592     WMError ret = WMError::SUCCESS;
593     unsigned layer = client->layerID(); // Don't check here the client is not nullptr
594
595     /* bool mv_ok = this->mvBackGround(client);
596
597     if(!mv_ok)
598     {
599         ilm_layerSetVisibility(layer, ILM_FALSE);
600     } */
601
602     ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
603
604     /* for(auto& wm_layer : this->wm_layers)
605     {
606         if(wm_layer->hasLayerID(layer))
607         {
608             LayerState ls = wm_layer->getLayerState();
609             ls.removeLayer(layer);;
610         }
611     } */
612
613
614
615     return ret;
616 }
617
618 /* bool LayerControl::mvBackGround(const shared_ptr<WMClient> client)
619 {
620     bool ret = false;
621
622     // Move background from foreground layer
623     auto bg = this->getWMLayer("BackGroundLayer");
624     if(bg != nullptr)
625     {
626         unsigned layer = client->layerID();
627         if(bg->hasRole(client->role()))
628         {
629             LayerState bg_ls = bg->getLayerState();
630             bg_ls.addLayer(layer);
631             auto wm_layer = this->getWMLayer(layer);
632             LayerState ls = wm_layer->getLayerState();
633             ls.removeLayer(layer);
634         }
635         ret = true;
636     }
637     return ret;
638 }
639
640 bool LayerControl::mvForeGround(const shared_ptr<WMClient> client)
641 {
642     bool ret = false;
643
644     // Move foreground from foreground layer
645     auto bg = this->getWMLayer("BackGroundLayer");
646     if(bg != nullptr)
647     {
648         unsigned layer = client->layerID();
649         if(bg->hasRole(client->role()))
650         {
651             LayerState bg_ls = bg->getLayerState();
652             bg_ls.removeLayer(layer);
653             auto wm_layer = this->getWMLayer(layer);
654             LayerState ls = wm_layer->getLayerState();
655             ls.addLayer(layer);
656         }
657         ret = true;
658     }
659     return ret;
660 }
661
662 */
663
664 } // namespace wm