87702363b8ca0beff6734e7bac9afaba09ee9535
[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 "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     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_TRUE);
129     ilm_commitChanges();
130 }
131
132 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
133 {
134     unsigned ret = 0;
135     for(const auto& l: this->wm_layers)
136     {
137         ret = l->getNewLayerID(role);
138         if(ret != 0)
139         {
140             *layer_name = l->layerName();
141             break;
142         }
143     }
144     return ret;
145 }
146
147 struct rect LayerControl::getAreaSize(const std::string& area)
148 {
149     return area2size[area];
150 }
151
152 void LayerControl::setupArea(double scaling)
153 {
154     struct rect rct;
155     this->scaling = scaling;
156
157     rct = this->area2size["normal.full"];
158     this->area2size["normalfull"] = rct;
159     this->area2size["normal"] = rct;
160
161     for (auto &i : this->area2size)
162     {
163         i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
164         i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
165         i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
166         i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
167
168         HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
169             i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
170     }
171 }
172
173 Screen LayerControl::getScreenInfo()
174 {
175     return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
176 }
177
178 double LayerControl::scale()
179 {
180     return this->scaling;
181 }
182
183 WMError LayerControl::updateLayer(LayerState& layer_state)
184 {
185     return WMError::SUCCESS;
186 }
187
188 WMError LayerControl::commitChange()
189 {
190     WMError rc = WMError::SUCCESS;
191     vector<unsigned> ivi_l_ids;
192     for(const auto& l : this->wm_layers)
193     {
194         auto state = l->getLayerState();
195         for(const auto& id : state.getIviIdList())
196         {
197             ivi_l_ids.push_back(id);
198         }
199     }
200     t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
201     if(id_array == nullptr)
202     {
203         HMI_WARNING("short memory");
204         this->undoUpdate();
205         return WMError::FAIL;
206     }
207     int count = 0;
208     for(const auto& i : ivi_l_ids)
209     {
210         id_array[count] = i;
211         ++count;
212     }
213
214     ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
215     if(ret != ILM_SUCCESS)
216     {
217         this->undoUpdate();
218         rc = WMError::FAIL;
219     }
220     delete id_array;
221     return rc;
222 }
223
224 void LayerControl::undoUpdate() {}
225
226 WMError LayerControl::loadLayerSetting(const string &path)
227 {
228     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
229
230     json_object *json_obj, *json_cfg;
231     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
232     if (0 > ret)
233     {
234         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
235         return WMError::FAIL;
236     }
237     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
238
239     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
240     {
241         HMI_ERROR("Parse Error!!");
242         return WMError::FAIL;
243     }
244
245     int len = json_object_array_length(json_cfg);
246     HMI_DEBUG("json_cfg len:%d", len);
247
248     for (int i = 0; i < len; i++)
249     {
250         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
251         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
252
253         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp));
254     }
255
256     return WMError::SUCCESS;
257 }
258
259 WMError LayerControl::loadAreaDb(const std::string& path)
260 {
261     // Load area.db
262     json_object *json_obj;
263     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
264     if (0 > ret)
265     {
266         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
267         return WMError::FAIL;
268     }
269     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
270
271     // Perse areas
272     json_object *json_cfg;
273     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
274     {
275         HMI_ERROR("Parse Error!!");
276         return WMError::FAIL;
277     }
278
279     int len = json_object_array_length(json_cfg);
280     HMI_DEBUG("json_cfg len:%d", len);
281
282     const char *area;
283     for (int i = 0; i < len; i++)
284     {
285         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
286         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
287
288         area = jh::getStringFromJson(json_tmp, "name");
289         if (nullptr == area)
290         {
291             HMI_ERROR("Parse Error!!");
292             return WMError::FAIL;
293         }
294         HMI_DEBUG("> area:%s", area);
295
296         json_object *json_rect;
297         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
298         {
299             HMI_ERROR("Parse Error!!");
300             return WMError::FAIL;
301         }
302         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
303
304         struct rect area_size;
305         area_size.x = jh::getIntFromJson(json_rect, "x");
306         area_size.y = jh::getIntFromJson(json_rect, "y");
307         area_size.w = jh::getIntFromJson(json_rect, "w");
308         area_size.h = jh::getIntFromJson(json_rect, "h");
309
310         this->area2size[area] = area_size;
311     }
312
313     // Check
314     for (const auto& itr : this->area2size)
315     {
316         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
317                   itr.first.c_str(), itr.second.x, itr.second.y,
318                   itr.second.w, itr.second.h);
319     }
320
321     // Release json_object
322     json_object_put(json_obj);
323
324     return WMError::SUCCESS;
325 }
326
327 WMError LayerControl::layoutChange(const WMAction& action)
328 {
329     WMError ret = WMError::FAIL;
330     if (action.visible == TaskVisible::INVISIBLE)
331     {
332         // Visibility is not change -> no redraw is required
333         return WMError::SUCCESS;
334     }
335     if(action.client == nullptr)
336     {
337         HMI_SEQ_ERROR(action.req_num, "client may vanish");
338         return WMError::NOT_REGISTERED;
339     }
340     unsigned layer = action.client->layerID();
341
342     // Layout Manager
343     // WMError ret = this->setLayerSize(layer, action.area);
344     auto rect = this->getAreaSize(action.area);
345     ilmErrorTypes err = ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
346     for(auto &wm_layer: this->wm_layers)
347     {
348         if(wm_layer->hasLayerID(layer))
349         {
350             LayerState ls = wm_layer->getLayerState();
351             ls.setArea(action.client->appID(), action.area);
352         }
353     }
354     if(err == ILM_SUCCESS)
355     {
356         ret = WMError::SUCCESS;
357     }
358     return ret;
359 }
360
361 WMError LayerControl::visibilityChange(const WMAction& action)
362 {
363     WMError ret = WMError::FAIL;
364     if(action.client == nullptr)
365     {
366         HMI_SEQ_ERROR(action.req_num, "client may vanish");
367         return WMError::NOT_REGISTERED;
368     }
369
370     if (action.visible != TaskVisible::INVISIBLE)
371     {
372         ret = this->makeVisible(action.client);
373     }
374     else
375     {
376         ret = this->makeInvisible(action.client);
377     }
378     return ret;
379 }
380
381 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
382 {
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(sp.creatorPid, 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