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