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