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