Change API in wm_layer
[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 #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
217     // Check the number of layers
218     vector<unsigned> ivi_l_ids;
219     for(auto& l : this->wm_layers)
220     {
221         auto state = l->getLayerState();
222         HMI_DEBUG("layer %s", l->layerName().c_str());
223         for(const auto& id : state.getIviIdList())
224         {
225             HMI_DEBUG("Add %d", id);
226             ivi_l_ids.push_back(id);
227         }
228     }
229
230     // Create render order
231     t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
232     if(id_array == nullptr)
233     {
234         HMI_WARNING("short memory");
235         this->undoUpdate();
236         return WMError::FAIL;
237     }
238     int count = 0;
239     for(const auto& i : ivi_l_ids)
240     {
241         id_array[count] = i;
242         ++count;
243     }
244
245     // Display
246     ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
247     if(ret != ILM_SUCCESS)
248     {
249         this->undoUpdate();
250         rc = WMError::FAIL;
251     }
252     else
253     {
254         for(auto& l : this->wm_layers)
255         {
256             l->update();
257         }
258     }
259     ilm_commitChanges();
260     delete id_array;
261     return rc;
262 }
263
264 void LayerControl::undoUpdate()
265 {
266     for(auto& l : this->wm_layers)
267     {
268         l->undo();
269     }
270 }
271
272 WMError LayerControl::loadLayerSetting(const string &path)
273 {
274     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
275
276     json_object *json_obj, *json_cfg;
277     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
278     if (0 > ret)
279     {
280         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
281         return WMError::FAIL;
282     }
283     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
284
285     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
286     {
287         HMI_ERROR("Parse Error!!");
288         return WMError::FAIL;
289     }
290
291     int len = json_object_array_length(json_cfg);
292     HMI_DEBUG("json_cfg len:%d", len);
293
294     for (int i = 0; i < len; i++)
295     {
296         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
297         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
298
299         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
300     }
301     json_object_put(json_obj);
302
303     return WMError::SUCCESS;
304 }
305
306 WMError LayerControl::loadAreaDb(const std::string& path)
307 {
308     // Load area.db
309     json_object *json_obj;
310     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
311     if (0 > ret)
312     {
313         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
314         return WMError::FAIL;
315     }
316     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
317
318     // Perse areas
319     json_object *json_cfg;
320     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
321     {
322         HMI_ERROR("Parse Error!!");
323         return WMError::FAIL;
324     }
325
326     int len = json_object_array_length(json_cfg);
327     HMI_DEBUG("json_cfg len:%d", len);
328
329     const char *area;
330     for (int i = 0; i < len; i++)
331     {
332         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
333         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
334
335         area = jh::getStringFromJson(json_tmp, "name");
336         if (nullptr == area)
337         {
338             HMI_ERROR("Parse Error!!");
339             return WMError::FAIL;
340         }
341         HMI_DEBUG("> area:%s", area);
342
343         json_object *json_rect;
344         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
345         {
346             HMI_ERROR("Parse Error!!");
347             return WMError::FAIL;
348         }
349         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
350
351         struct rect area_size;
352         area_size.x = jh::getIntFromJson(json_rect, "x");
353         area_size.y = jh::getIntFromJson(json_rect, "y");
354         area_size.w = jh::getIntFromJson(json_rect, "w");
355         area_size.h = jh::getIntFromJson(json_rect, "h");
356
357         this->area2size[area] = area_size;
358     }
359
360     // Check
361     for (const auto& itr : this->area2size)
362     {
363         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
364                   itr.first.c_str(), itr.second.x, itr.second.y,
365                   itr.second.w, itr.second.h);
366     }
367
368     // Release json_object
369     json_object_put(json_obj);
370
371     return WMError::SUCCESS;
372 }
373
374 WMError LayerControl::layoutChange(const WMAction& action)
375 {
376     if (action.visible == TaskVisible::INVISIBLE)
377     {
378         // Visibility is not change -> no redraw is required
379         return WMError::SUCCESS;
380     }
381     if(action.client == nullptr)
382     {
383         HMI_SEQ_ERROR(action.req_num, "client may vanish");
384         return WMError::NOT_REGISTERED;
385     }
386     unsigned layer = action.client->layerID();
387
388     // Layout Manager
389     // WMError ret = this->setLayerSize(layer, action.area);
390     auto rect = this->getAreaSize(action.area);
391     HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
392     ilm_layerSetSourceRectangle(layer, 0, 0, rect.w, rect.h);
393     ilm_commitChanges();
394     ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
395     ilm_commitChanges();
396     for(auto &wm_layer: this->wm_layers)
397     {
398         if(wm_layer->hasLayerID(layer))
399         {
400             wm_layer->setAreaToState(action.client->appID(), action.area);
401             /* TODO: manipulate state directly
402             LayerState ls = wm_layer->getLayerState();
403             ls.setArea(action.client->appID(), action.area);
404             wm_layer->dump(); */
405         }
406     }
407
408     return WMError::SUCCESS;
409 }
410
411 WMError LayerControl::visibilityChange(const WMAction& action)
412 {
413     WMError ret = WMError::FAIL;
414     if(action.client == nullptr)
415     {
416         HMI_SEQ_ERROR(action.req_num, "client may vanish");
417         return WMError::NOT_REGISTERED;
418     }
419
420     if (action.visible == TaskVisible::VISIBLE)
421     {
422         ret = this->makeVisible(action.client);
423     }
424     else if (action.visible == TaskVisible::INVISIBLE)
425     {
426         ret = this->makeInvisible(action.client);
427     }
428     return ret;
429 }
430
431 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
432 {
433     if (ILM_SURFACE == object)
434     {
435         if (created)
436         {
437             ilmSurfaceProperties sp;
438             ilmErrorTypes rc;
439             rc = ilm_getPropertiesOfSurface(id, &sp);
440             if(rc != ILM_SUCCESS)
441                 return;
442             this->cb.surfaceCreated(sp.creatorPid, id);
443             ilm_surfaceAddNotification(id, surfaceCallback_static);
444             ilm_surfaceSetVisibility(id, ILM_TRUE);
445         }
446         else
447         {
448             this->cb.surfaceDestroyed(id);
449         }
450     }
451     if (ILM_LAYER == object)
452     {
453         if(created)
454         {
455             ilm_layerAddNotification(id, layerCallback_static);
456             // this->cb->layerCreated(id);
457         }
458         else
459         {
460             // this->cb->layerDestroyed(id); // Means Application is dead.
461         }
462     }
463 }
464
465 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
466         struct ilmSurfaceProperties* sprop,
467         t_ilm_notification_mask mask)
468 {
469     pid_t pid = sprop->creatorPid;
470     HMI_DEBUG("pid : %d", pid);
471
472     if (ILM_NOTIFICATION_VISIBILITY & mask)
473     {
474         //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
475     }
476     if (ILM_NOTIFICATION_OPACITY & mask)
477     {
478     }
479     if (ILM_NOTIFICATION_ORIENTATION & mask)
480     {
481     }
482     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
483     {
484         // this->cb->surfaceSourceRectChanged(id, )
485     }
486     if (ILM_NOTIFICATION_DEST_RECT & mask)
487     {
488         // this->cb->surfaceSourceRectChanged(id, )
489     }
490     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
491     {
492     }
493     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
494     {
495         /* application being down */
496         // m_appLayers.remove(pid);
497     }
498     if (ILM_NOTIFICATION_CONFIGURED & mask)
499     {
500         HMI_DEBUG("surface %d available", id);
501         ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
502         ilm_surfaceSetDestinationRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
503         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
504         qDebug("  surfaceProperties %d", surface);
505         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
506         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
507
508         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
509             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
510             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
511         } else {
512             ilmErrorTypes result;
513             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
514
515             if (layer != 0) {
516                 configureAppSurface(surface,
517                                     surfaceProperties->origSourceWidth,
518                                     surfaceProperties->origSourceHeight);
519
520                 result = ilm_layerAddSurface(layer, surface);
521                 if (result != ILM_SUCCESS) {
522                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
523                 }
524                 ilm_commitChanges();
525             }
526         }
527         updateScreen(); */
528     }
529 }
530
531 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
532         struct ilmLayerProperties* lprop,
533         t_ilm_notification_mask mask)
534 {
535     if (ILM_NOTIFICATION_VISIBILITY & mask)
536     {
537         //this->cb->layerVisibilityChanged(id, sprop->visibility);
538     }
539     if (ILM_NOTIFICATION_OPACITY & mask)
540     {
541     }
542     if (ILM_NOTIFICATION_ORIENTATION & mask)
543     {
544     }
545     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
546     {
547         // this->cb->surfaceSourceRectChanged(id, )
548     }
549     if (ILM_NOTIFICATION_DEST_RECT & mask)
550     {
551         // this->cb->surfaceSourceRectChanged(id, )
552     }
553     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
554     {
555     }
556     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
557     {
558         /* application being down */
559         // m_appLayers.remove(pid);
560     }
561     if (ILM_NOTIFICATION_CONFIGURED & mask)
562     {
563         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
564         qDebug("  surfaceProperties %d", surface);
565         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
566         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
567
568         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
569             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
570             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
571         } else {
572             ilmErrorTypes result;
573             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
574
575             if (layer != 0) {
576                 configureAppSurface(surface,
577                                     surfaceProperties->origSourceWidth,
578                                     surfaceProperties->origSourceHeight);
579
580                 result = ilm_layerAddSurface(layer, surface);
581                 if (result != ILM_SUCCESS) {
582                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
583                 }
584                 ilm_commitChanges();
585             }
586         }
587         updateScreen(); */
588     }
589 }
590
591 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
592 {
593     WMError ret = WMError::SUCCESS;
594     // Don't check here the client is not nullptr
595     unsigned layer = client->layerID();
596
597     this->moveForeGround(client);
598
599     ilm_layerSetVisibility(layer, ILM_TRUE);
600
601     /* for(auto& wm_layer : this->wm_layers)
602     {
603         if(wm_layer->hasLayerID(layer))
604         {
605             LayerState ls = wm_layer->getLayerState();
606             ls.addLayer(layer);;
607         }
608     } */
609
610     // Move foreground from back ground layer
611     /* for(auto& wm_layer : this->wm_layers)
612     {
613         if(wm_layer->layerName() == "BackGroundLayer")
614         {
615             if(wm_layer->hasRole(client->role()))
616             {
617                 LayerState ls = wm_layer->getLayerState();
618                 ls.removeLayer(layer);
619             }
620             break;
621         }
622     } */
623
624     return ret;
625 }
626
627 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
628 {
629     WMError ret = WMError::SUCCESS;
630     unsigned layer = client->layerID(); // Don't check here the client is not nullptr
631
632     bool mv_ok = this->moveBackGround(client);
633
634     if(!mv_ok)
635     {
636         HMI_INFO("make invisible client %s", client->appID().c_str());
637         ilm_layerSetVisibility(layer, ILM_FALSE);
638     }
639
640     //ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
641
642     /* for(auto& wm_layer : this->wm_layers)
643     {
644         if(wm_layer->hasLayerID(layer))
645         {
646             LayerState ls = wm_layer->getLayerState();
647             ls.removeLayer(layer);;
648         }
649     } */
650
651
652
653     return ret;
654 }
655
656 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
657 {
658     bool ret = false;
659
660     // Move background from foreground layer
661     auto bg = this->getWMLayer(BACK_GROUND_LAYER);
662     if(bg != nullptr)
663     {
664         HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
665         unsigned layer = client->layerID();
666         if(bg->hasRole(client->role()))
667         {
668             HMI_INFO("%s go to background", client->appID().c_str());
669             bg->addLayerToState(layer);
670             auto wm_layer = this->getWMLayer(layer);
671             wm_layer->removeLayerFromState(layer);
672             /* TODO: manipulate state directly
673             LayerState bg_ls = bg->getLayerState();
674             bg_ls.addLayer(layer);
675             LayerState ls = wm_layer->getLayerState();
676             ls.removeLayer(layer); */
677             bg->dump();
678             wm_layer->dump();
679             ret = true;
680         }
681     }
682     return ret;
683 }
684
685 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
686 {
687     bool ret = false;
688
689     // Move foreground from foreground layer
690     auto bg = this->getWMLayer(BACK_GROUND_LAYER);
691     if(bg != nullptr)
692     {
693         if(bg->hasRole(client->role()))
694         {
695             unsigned layer = client->layerID();
696             HMI_INFO("%s go to foreground", client->appID().c_str());
697             bg->removeLayerFromState(layer);
698             auto wm_layer = this->getWMLayer(layer);
699             wm_layer->addLayerToState(layer);
700             /* TODO: manipulate state directly
701             LayerState bg_ls = bg->getLayerState();
702             bg_ls.removeLayer(layer);
703             LayerState ls = wm_layer->getLayerState();
704             ls.addLayer(layer); */
705             bg->dump();
706             wm_layer->dump();
707             ret = true;
708         }
709     }
710     return ret;
711 }
712
713 } // namespace wm