Bug Fix : 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 "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     this->cb.test(id);
384     if (ILM_SURFACE == object)
385     {
386         if (created)
387         {
388             ilmSurfaceProperties sp;
389             ilmErrorTypes rc;
390             rc = ilm_getPropertiesOfSurface(id, &sp);
391             if(rc != ILM_SUCCESS)
392                 return;
393             // this->cb->surfaceCreated(pid, id);
394             ilm_surfaceAddNotification(id, surfaceCallback_static);
395             ilm_surfaceSetSourceRectangle(id, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
396         }
397         else
398         {
399             // this->cb->surfaceDestroyed(id);
400         }
401     }
402     if (ILM_LAYER == object)
403     {
404         if(created)
405         {
406             ilm_layerAddNotification(id, layerCallback_static);
407             // this->cb->layerCreated(id);
408         }
409         else
410         {
411             // this->cb->layerDestroyed(id); // Means Application is dead.
412         }
413     }
414 }
415
416 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
417         struct ilmSurfaceProperties* sprop,
418         t_ilm_notification_mask mask)
419 {
420     pid_t pid = sprop->creatorPid;
421     HMI_DEBUG("pid : %d", pid);
422
423     if (ILM_NOTIFICATION_VISIBILITY & mask)
424     {
425         //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
426     }
427     if (ILM_NOTIFICATION_OPACITY & mask)
428     {
429     }
430     if (ILM_NOTIFICATION_ORIENTATION & mask)
431     {
432     }
433     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
434     {
435         // this->cb->surfaceSourceRectChanged(id, )
436     }
437     if (ILM_NOTIFICATION_DEST_RECT & mask)
438     {
439         // this->cb->surfaceSourceRectChanged(id, )
440     }
441     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
442     {
443     }
444     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
445     {
446         /* application being down */
447         // m_appLayers.remove(pid);
448     }
449     if (ILM_NOTIFICATION_CONFIGURED & mask)
450     {
451         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
452         qDebug("  surfaceProperties %d", surface);
453         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
454         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
455
456         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
457             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
458             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
459         } else {
460             ilmErrorTypes result;
461             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
462
463             if (layer != 0) {
464                 configureAppSurface(surface,
465                                     surfaceProperties->origSourceWidth,
466                                     surfaceProperties->origSourceHeight);
467
468                 result = ilm_layerAddSurface(layer, surface);
469                 if (result != ILM_SUCCESS) {
470                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
471                 }
472                 ilm_commitChanges();
473             }
474         }
475         updateScreen(); */
476     }
477 }
478
479 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
480         struct ilmLayerProperties* lprop,
481         t_ilm_notification_mask mask)
482 {
483     if (ILM_NOTIFICATION_VISIBILITY & mask)
484     {
485         //this->cb->layerVisibilityChanged(id, sprop->visibility);
486     }
487     if (ILM_NOTIFICATION_OPACITY & mask)
488     {
489     }
490     if (ILM_NOTIFICATION_ORIENTATION & mask)
491     {
492     }
493     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
494     {
495         // this->cb->surfaceSourceRectChanged(id, )
496     }
497     if (ILM_NOTIFICATION_DEST_RECT & mask)
498     {
499         // this->cb->surfaceSourceRectChanged(id, )
500     }
501     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
502     {
503     }
504     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
505     {
506         /* application being down */
507         // m_appLayers.remove(pid);
508     }
509     if (ILM_NOTIFICATION_CONFIGURED & mask)
510     {
511         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
512         qDebug("  surfaceProperties %d", surface);
513         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
514         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
515
516         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
517             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
518             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
519         } else {
520             ilmErrorTypes result;
521             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
522
523             if (layer != 0) {
524                 configureAppSurface(surface,
525                                     surfaceProperties->origSourceWidth,
526                                     surfaceProperties->origSourceHeight);
527
528                 result = ilm_layerAddSurface(layer, surface);
529                 if (result != ILM_SUCCESS) {
530                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
531                 }
532                 ilm_commitChanges();
533             }
534         }
535         updateScreen(); */
536     }
537 }
538
539 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
540 {
541     WMError ret = WMError::SUCCESS;
542     // Don't check here the client is not nullptr
543     unsigned layer = client->layerID();
544
545     for(auto& wm_layer : this->wm_layers)
546     {
547         if(wm_layer->hasLayerID(layer))
548         {
549             LayerState ls = wm_layer->getLayerState();
550             ls.addLayer(layer);;
551         }
552     }
553
554     // Move foreground from back ground layer
555     for(auto& wm_layer : this->wm_layers)
556     {
557         if(wm_layer->layerName() == "BackGroundLayer")
558         {
559             if(wm_layer->hasRole(client->role()))
560             {
561                 LayerState ls = wm_layer->getLayerState();
562                 ls.removeLayer(layer);
563             }
564             break;
565         }
566     }
567
568     return ret;
569 }
570
571 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
572 {
573     WMError ret = WMError::SUCCESS;
574     // Don't check here the client is not nullptr
575     unsigned layer = client->layerID();
576
577     for(auto& wm_layer : this->wm_layers)
578     {
579         if(wm_layer->hasLayerID(layer))
580         {
581             LayerState ls = wm_layer->getLayerState();
582             ls.removeLayer(layer);;
583         }
584     }
585
586     // Move foreground from back ground layer
587     for(auto& wm_layer : this->wm_layers)
588     {
589         if(wm_layer->layerName() == "BackGroundLayer")
590         {
591             if(wm_layer->hasRole(client->role()))
592             {
593                 LayerState ls = wm_layer->getLayerState();
594                 ls.addLayer(layer);
595             }
596             break;
597         }
598     }
599
600     return ret;
601 }
602 } // namespace wm