e0800f8f85ebebb5163a4683cdfe48f7faa3ad30
[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     HMI_INFO("Commit change");
191     WMError rc = WMError::SUCCESS;
192     vector<unsigned> ivi_l_ids;
193     for(auto& l : this->wm_layers)
194     {
195         auto state = l->getLayerState();
196         for(const auto& id : state.getIviIdList())
197         {
198             ivi_l_ids.push_back(id);
199         }
200     }
201     t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
202     if(id_array == nullptr)
203     {
204         HMI_WARNING("short memory");
205         this->undoUpdate();
206         return WMError::FAIL;
207     }
208     int count = 0;
209     for(const auto& i : ivi_l_ids)
210     {
211         id_array[count] = i;
212         HMI_DEBUG("check render order %d", i);
213         ++count;
214     }
215
216     ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
217     if(ret != ILM_SUCCESS)
218     {
219         this->undoUpdate();
220         rc = WMError::FAIL;
221     }
222     delete id_array;
223     return rc;
224 }
225
226 void LayerControl::undoUpdate() {}
227
228 WMError LayerControl::loadLayerSetting(const string &path)
229 {
230     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
231
232     json_object *json_obj, *json_cfg;
233     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
234     if (0 > ret)
235     {
236         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
237         return WMError::FAIL;
238     }
239     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
240
241     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
242     {
243         HMI_ERROR("Parse Error!!");
244         return WMError::FAIL;
245     }
246
247     int len = json_object_array_length(json_cfg);
248     HMI_DEBUG("json_cfg len:%d", len);
249
250     for (int i = 0; i < len; i++)
251     {
252         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
253         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
254
255         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp));
256     }
257
258     return WMError::SUCCESS;
259 }
260
261 WMError LayerControl::loadAreaDb(const std::string& path)
262 {
263     // Load area.db
264     json_object *json_obj;
265     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
266     if (0 > ret)
267     {
268         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
269         return WMError::FAIL;
270     }
271     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
272
273     // Perse areas
274     json_object *json_cfg;
275     if (!json_object_object_get_ex(json_obj, "areas", &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     const char *area;
285     for (int i = 0; i < len; i++)
286     {
287         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
288         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
289
290         area = jh::getStringFromJson(json_tmp, "name");
291         if (nullptr == area)
292         {
293             HMI_ERROR("Parse Error!!");
294             return WMError::FAIL;
295         }
296         HMI_DEBUG("> area:%s", area);
297
298         json_object *json_rect;
299         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
300         {
301             HMI_ERROR("Parse Error!!");
302             return WMError::FAIL;
303         }
304         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
305
306         struct rect area_size;
307         area_size.x = jh::getIntFromJson(json_rect, "x");
308         area_size.y = jh::getIntFromJson(json_rect, "y");
309         area_size.w = jh::getIntFromJson(json_rect, "w");
310         area_size.h = jh::getIntFromJson(json_rect, "h");
311
312         this->area2size[area] = area_size;
313     }
314
315     // Check
316     for (const auto& itr : this->area2size)
317     {
318         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
319                   itr.first.c_str(), itr.second.x, itr.second.y,
320                   itr.second.w, itr.second.h);
321     }
322
323     // Release json_object
324     json_object_put(json_obj);
325
326     return WMError::SUCCESS;
327 }
328
329 WMError LayerControl::layoutChange(const WMAction& action)
330 {
331     WMError ret = WMError::FAIL;
332     if (action.visible == TaskVisible::INVISIBLE)
333     {
334         // Visibility is not change -> no redraw is required
335         return WMError::SUCCESS;
336     }
337     if(action.client == nullptr)
338     {
339         HMI_SEQ_ERROR(action.req_num, "client may vanish");
340         return WMError::NOT_REGISTERED;
341     }
342     unsigned layer = action.client->layerID();
343
344     // Layout Manager
345     // WMError ret = this->setLayerSize(layer, action.area);
346     auto rect = this->getAreaSize(action.area);
347     ilmErrorTypes err = ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
348     for(auto &wm_layer: this->wm_layers)
349     {
350         if(wm_layer->hasLayerID(layer))
351         {
352             LayerState ls = wm_layer->getLayerState();
353             ls.setArea(action.client->appID(), action.area);
354         }
355     }
356     if(err == ILM_SUCCESS)
357     {
358         ret = WMError::SUCCESS;
359     }
360     return ret;
361 }
362
363 WMError LayerControl::visibilityChange(const WMAction& action)
364 {
365     WMError ret = WMError::FAIL;
366     if(action.client == nullptr)
367     {
368         HMI_SEQ_ERROR(action.req_num, "client may vanish");
369         return WMError::NOT_REGISTERED;
370     }
371
372     if (action.visible != TaskVisible::INVISIBLE)
373     {
374         ret = this->makeVisible(action.client);
375     }
376     else
377     {
378         ret = this->makeInvisible(action.client);
379     }
380     return ret;
381 }
382
383 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
384 {
385     if (ILM_SURFACE == object)
386     {
387         if (created)
388         {
389             ilmSurfaceProperties sp;
390             ilmErrorTypes rc;
391             rc = ilm_getPropertiesOfSurface(id, &sp);
392             if(rc != ILM_SUCCESS)
393                 return;
394             this->cb.surfaceCreated(sp.creatorPid, id);
395             ilm_surfaceAddNotification(id, surfaceCallback_static);
396             ilm_surfaceSetSourceRectangle(id, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
397         }
398         else
399         {
400             // this->cb->surfaceDestroyed(id);
401         }
402     }
403     if (ILM_LAYER == object)
404     {
405         if(created)
406         {
407             ilm_layerAddNotification(id, layerCallback_static);
408             // this->cb->layerCreated(id);
409         }
410         else
411         {
412             // this->cb->layerDestroyed(id); // Means Application is dead.
413         }
414     }
415 }
416
417 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
418         struct ilmSurfaceProperties* sprop,
419         t_ilm_notification_mask mask)
420 {
421     pid_t pid = sprop->creatorPid;
422     HMI_DEBUG("pid : %d", pid);
423
424     if (ILM_NOTIFICATION_VISIBILITY & mask)
425     {
426         //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
427     }
428     if (ILM_NOTIFICATION_OPACITY & mask)
429     {
430     }
431     if (ILM_NOTIFICATION_ORIENTATION & mask)
432     {
433     }
434     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
435     {
436         // this->cb->surfaceSourceRectChanged(id, )
437     }
438     if (ILM_NOTIFICATION_DEST_RECT & mask)
439     {
440         // this->cb->surfaceSourceRectChanged(id, )
441     }
442     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
443     {
444     }
445     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
446     {
447         /* application being down */
448         // m_appLayers.remove(pid);
449     }
450     if (ILM_NOTIFICATION_CONFIGURED & mask)
451     {
452         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
453         qDebug("  surfaceProperties %d", surface);
454         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
455         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
456
457         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
458             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
459             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
460         } else {
461             ilmErrorTypes result;
462             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
463
464             if (layer != 0) {
465                 configureAppSurface(surface,
466                                     surfaceProperties->origSourceWidth,
467                                     surfaceProperties->origSourceHeight);
468
469                 result = ilm_layerAddSurface(layer, surface);
470                 if (result != ILM_SUCCESS) {
471                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
472                 }
473                 ilm_commitChanges();
474             }
475         }
476         updateScreen(); */
477     }
478 }
479
480 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
481         struct ilmLayerProperties* lprop,
482         t_ilm_notification_mask mask)
483 {
484     if (ILM_NOTIFICATION_VISIBILITY & mask)
485     {
486         //this->cb->layerVisibilityChanged(id, sprop->visibility);
487     }
488     if (ILM_NOTIFICATION_OPACITY & mask)
489     {
490     }
491     if (ILM_NOTIFICATION_ORIENTATION & mask)
492     {
493     }
494     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
495     {
496         // this->cb->surfaceSourceRectChanged(id, )
497     }
498     if (ILM_NOTIFICATION_DEST_RECT & mask)
499     {
500         // this->cb->surfaceSourceRectChanged(id, )
501     }
502     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
503     {
504     }
505     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
506     {
507         /* application being down */
508         // m_appLayers.remove(pid);
509     }
510     if (ILM_NOTIFICATION_CONFIGURED & mask)
511     {
512         /* qDebug("ILM_NOTIFICATION_CONFIGURED");
513         qDebug("  surfaceProperties %d", surface);
514         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
515         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
516
517         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
518             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
519             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
520         } else {
521             ilmErrorTypes result;
522             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
523
524             if (layer != 0) {
525                 configureAppSurface(surface,
526                                     surfaceProperties->origSourceWidth,
527                                     surfaceProperties->origSourceHeight);
528
529                 result = ilm_layerAddSurface(layer, surface);
530                 if (result != ILM_SUCCESS) {
531                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
532                 }
533                 ilm_commitChanges();
534             }
535         }
536         updateScreen(); */
537     }
538 }
539
540 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
541 {
542     WMError ret = WMError::SUCCESS;
543     // Don't check here the client is not nullptr
544     unsigned layer = client->layerID();
545
546     for(auto& wm_layer : this->wm_layers)
547     {
548         if(wm_layer->hasLayerID(layer))
549         {
550             LayerState ls = wm_layer->getLayerState();
551             ls.addLayer(layer);;
552         }
553     }
554
555     // Move foreground from back ground layer
556     for(auto& wm_layer : this->wm_layers)
557     {
558         if(wm_layer->layerName() == "BackGroundLayer")
559         {
560             if(wm_layer->hasRole(client->role()))
561             {
562                 LayerState ls = wm_layer->getLayerState();
563                 ls.removeLayer(layer);
564             }
565             break;
566         }
567     }
568
569     return ret;
570 }
571
572 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
573 {
574     WMError ret = WMError::SUCCESS;
575     // Don't check here the client is not nullptr
576     unsigned layer = client->layerID();
577
578     for(auto& wm_layer : this->wm_layers)
579     {
580         if(wm_layer->hasLayerID(layer))
581         {
582             LayerState ls = wm_layer->getLayerState();
583             ls.removeLayer(layer);;
584         }
585     }
586
587     // Move foreground from back ground layer
588     for(auto& wm_layer : this->wm_layers)
589     {
590         if(wm_layer->layerName() == "BackGroundLayer")
591         {
592             if(wm_layer->hasRole(client->role()))
593             {
594                 LayerState ls = wm_layer->getLayerState();
595                 ls.addLayer(layer);
596             }
597             break;
598         }
599     }
600
601     return ret;
602 }
603 } // namespace wm