bb3bfc2047c4b2a222b1d6c0577ec2786a61c5ec
[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 "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 areas.json
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 to Window Manager and 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, this->offset_x, this->offset_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     this->scaling = scaling;
179     this->offset_x = base_rct.left();
180     this->offset_y = base_rct.top();
181
182     for (auto &i : this->area2size)
183     {
184         i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
185         i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
186         i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
187         i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
188
189         HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
190             i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
191     }
192 }
193
194 Screen LayerControl::getScreenInfo()
195 {
196     return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
197 }
198
199 double LayerControl::scale()
200 {
201     return this->scaling;
202 }
203
204 WMError LayerControl::updateLayer(LayerState& layer_state)
205 {
206     return WMError::SUCCESS;
207 }
208
209 WMError LayerControl::renderLayers()
210 {
211     HMI_INFO("Commit change");
212     WMError rc = WMError::SUCCESS;
213
214     // Check the number of layers
215     vector<unsigned> ivi_l_ids;
216     for(auto& l : this->wm_layers)
217     {
218         auto state = l->getLayerState();
219         HMI_DEBUG("layer %s", l->layerName().c_str());
220         for(const auto& id : state.getIviIdList())
221         {
222             HMI_DEBUG("Add %d", id);
223             ivi_l_ids.push_back(id);
224         }
225     }
226
227     // Create render order
228     t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
229     if(id_array == nullptr)
230     {
231         HMI_WARNING("short memory");
232         this->undoUpdate();
233         return WMError::FAIL;
234     }
235     int count = 0;
236     for(const auto& i : ivi_l_ids)
237     {
238         id_array[count] = i;
239         ++count;
240     }
241
242     // Display
243     ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
244     if(ret != ILM_SUCCESS)
245     {
246         this->undoUpdate();
247         rc = WMError::FAIL;
248     }
249     else
250     {
251         for(auto& l : this->wm_layers)
252         {
253             l->update();
254         }
255     }
256     ilm_commitChanges();
257     delete id_array;
258     return rc;
259 }
260
261 void LayerControl::undoUpdate()
262 {
263     for(auto& l : this->wm_layers)
264     {
265         l->undo();
266     }
267 }
268
269 WMError LayerControl::loadLayerSetting(const string &path)
270 {
271     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
272
273     json_object *json_obj, *json_cfg;
274     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
275     if (0 > ret)
276     {
277         HMI_ERROR("Could not open %s", path.c_str());
278         return WMError::FAIL;
279     }
280     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
281
282     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
283     {
284         HMI_ERROR("Parse Error!!");
285         return WMError::FAIL;
286     }
287
288     int len = json_object_array_length(json_cfg);
289     HMI_DEBUG("json_cfg len:%d", len);
290
291     for (int i = 0; i < len; i++)
292     {
293         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
294         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
295
296         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
297     }
298     json_object_put(json_obj);
299
300     return WMError::SUCCESS;
301 }
302
303 WMError LayerControl::loadAreaDb(const std::string& path)
304 {
305     // Load area.db
306     json_object *json_obj;
307     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
308     if (0 > ret)
309     {
310         HMI_ERROR("Could not open %s", path.c_str());
311         return WMError::FAIL;
312     }
313     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
314
315     // Perse areas
316     json_object *json_cfg;
317     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
318     {
319         HMI_ERROR("Parse Error!!");
320         return WMError::FAIL;
321     }
322
323     int len = json_object_array_length(json_cfg);
324     HMI_DEBUG("json_cfg len:%d", len);
325
326     const char *area;
327     for (int i = 0; i < len; i++)
328     {
329         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
330         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
331
332         area = jh::getStringFromJson(json_tmp, "name");
333         if (nullptr == area)
334         {
335             HMI_ERROR("Parse Error!!");
336             return WMError::FAIL;
337         }
338         HMI_DEBUG("> area:%s", area);
339
340         json_object *json_rect;
341         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
342         {
343             HMI_ERROR("Parse Error!!");
344             return WMError::FAIL;
345         }
346         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
347
348         struct rect area_size;
349         area_size.x = jh::getIntFromJson(json_rect, "x");
350         area_size.y = jh::getIntFromJson(json_rect, "y");
351         area_size.w = jh::getIntFromJson(json_rect, "w");
352         area_size.h = jh::getIntFromJson(json_rect, "h");
353
354         this->area2size[area] = area_size;
355     }
356
357     // Check
358     for (const auto& itr : this->area2size)
359     {
360         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
361                   itr.first.c_str(), itr.second.x, itr.second.y,
362                   itr.second.w, itr.second.h);
363     }
364
365     // Release json_object
366     json_object_put(json_obj);
367
368     return WMError::SUCCESS;
369 }
370
371 WMError LayerControl::layoutChange(const WMAction& action)
372 {
373     if (action.visible == TaskVisible::INVISIBLE)
374     {
375         // Visibility is not change -> no redraw is required
376         return WMError::SUCCESS;
377     }
378     if(action.client == nullptr)
379     {
380         HMI_SEQ_ERROR(action.req_num, "client may vanish");
381         return WMError::NOT_REGISTERED;
382     }
383     unsigned layer = action.client->layerID();
384     unsigned surface = action.client->surfaceID();
385
386     auto rect = this->getAreaSize(action.area);
387     HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
388     ilm_commitChanges();
389     ilm_surfaceSetDestinationRectangle(surface, rect.x, rect.y, rect.w, rect.h);
390     ilm_commitChanges();
391     for(auto &wm_layer: this->wm_layers)
392     {
393         // Store the state who is assigned to the area
394         if(wm_layer->hasLayerID(layer))
395         {
396             wm_layer->setAreaToState(action.client->appID(), action.area);
397             /* TODO: manipulate state directly
398             LayerState ls = wm_layer->getLayerState();
399             ls.setArea(action.client->appID(), action.area);
400             wm_layer->dump(); */
401         }
402     }
403
404     return WMError::SUCCESS;
405 }
406
407 WMError LayerControl::visibilityChange(const WMAction& action)
408 {
409     WMError ret = WMError::FAIL;
410     if(action.client == nullptr)
411     {
412         HMI_SEQ_ERROR(action.req_num, "client may vanish");
413         return WMError::NOT_REGISTERED;
414     }
415
416     if (action.visible == TaskVisible::VISIBLE)
417     {
418         ret = this->makeVisible(action.client);
419     }
420     else if (action.visible == TaskVisible::INVISIBLE)
421     {
422         ret = this->makeInvisible(action.client);
423     }
424     return ret;
425 }
426
427 void LayerControl::terminateApp(const shared_ptr<WMClient> client)
428 {
429     for(auto& l : this->wm_layers)
430     {
431         l->terminateApp(client->layerID());
432     }
433 }
434
435 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
436 {
437     if (ILM_SURFACE == object)
438     {
439         if (created)
440         {
441             ilmSurfaceProperties sp;
442             ilmErrorTypes rc;
443             rc = ilm_getPropertiesOfSurface(id, &sp);
444             if(rc != ILM_SUCCESS)
445             {
446                 HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
447                 return;
448             }
449             this->cb.surfaceCreated(sp.creatorPid, id);
450             ilm_surfaceAddNotification(id, surfaceCallback_static);
451             ilm_surfaceSetVisibility(id, ILM_TRUE);
452         }
453         else
454         {
455             this->cb.surfaceDestroyed(id);
456         }
457     }
458     if (ILM_LAYER == object)
459     {
460         if(created)
461         {
462             ilm_layerAddNotification(id, layerCallback_static);
463         }
464         else
465         {
466             // Ignore here. Nothing to do currently.
467             // Process of application dead is handled by Window Manager
468             // from binder notification
469         }
470     }
471 }
472
473 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
474         struct ilmSurfaceProperties* sprop,
475         t_ilm_notification_mask mask)
476 {
477     /*
478       ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
479       are not handled here, handled in create/destroy event
480      */
481     if (ILM_NOTIFICATION_VISIBILITY & mask)
482     {
483         HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
484     }
485     if (ILM_NOTIFICATION_OPACITY & mask)
486     {
487         HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
488     }
489     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
490     {
491         HMI_DEBUG("surface %d source rect changes", id);
492     }
493     if (ILM_NOTIFICATION_DEST_RECT & mask)
494     {
495         HMI_DEBUG("surface %d dest rect changes", id);
496     }
497     if (ILM_NOTIFICATION_CONFIGURED & mask)
498     {
499         HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
500             sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
501         ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
502     }
503 }
504
505 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
506         struct ilmLayerProperties* lprop,
507         t_ilm_notification_mask mask)
508 {
509     if (ILM_NOTIFICATION_VISIBILITY & mask)
510     {
511         HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
512     }
513     if (ILM_NOTIFICATION_OPACITY & mask)
514     {
515         HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
516     }
517     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
518     {
519         HMI_DEBUG("layer %d source rect changes", id);
520     }
521     if (ILM_NOTIFICATION_DEST_RECT & mask)
522     {
523         HMI_DEBUG("layer %d dest rect changes", id);
524     }
525 }
526
527 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
528 {
529     WMError ret = WMError::SUCCESS;
530     // Don't check here the client is not nullptr
531     unsigned layer = client->layerID();
532
533     this->moveForeGround(client);
534
535     ilm_layerSetVisibility(layer, ILM_TRUE);
536
537     /* for(auto& wm_layer : this->wm_layers)
538     {
539         if(wm_layer->hasLayerID(layer))
540         {
541             LayerState ls = wm_layer->getLayerState();
542             ls.addLayer(layer);;
543         }
544     } */
545
546     // Move foreground from back ground layer
547     /* for(auto& wm_layer : this->wm_layers)
548     {
549         if(wm_layer->layerName() == "BackGroundLayer")
550         {
551             if(wm_layer->hasRole(client->role()))
552             {
553                 LayerState ls = wm_layer->getLayerState();
554                 ls.removeLayer(layer);
555             }
556             break;
557         }
558     } */
559
560     return ret;
561 }
562
563 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
564 {
565     WMError ret = WMError::SUCCESS;
566     unsigned layer = client->layerID(); // Don't check here the client is not nullptr
567
568     bool mv_ok = this->moveBackGround(client);
569
570     if(!mv_ok)
571     {
572         HMI_INFO("make invisible client %s", client->appID().c_str());
573         ilm_layerSetVisibility(layer, ILM_FALSE);
574     }
575
576     //ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
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
588
589     return ret;
590 }
591
592 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
593 {
594     bool ret = false;
595
596     // Move background from foreground layer
597     auto bg = this->getWMLayer(BACK_GROUND_LAYER);
598     if(bg != nullptr)
599     {
600         HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
601         unsigned layer = client->layerID();
602         if(bg->hasRole(client->role()))
603         {
604             HMI_INFO("%s go to background", client->appID().c_str());
605             bg->addLayerToState(layer);
606             auto wm_layer = this->getWMLayer(layer);
607             wm_layer->removeLayerFromState(layer);
608             /* TODO: manipulate state directly
609             LayerState bg_ls = bg->getLayerState();
610             bg_ls.addLayer(layer);
611             LayerState ls = wm_layer->getLayerState();
612             ls.removeLayer(layer); */
613             bg->dump();
614             wm_layer->dump();
615             ret = true;
616         }
617     }
618     return ret;
619 }
620
621 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
622 {
623     bool ret = false;
624
625     // Move foreground from foreground layer
626     auto bg = this->getWMLayer(BACK_GROUND_LAYER);
627     if(bg != nullptr)
628     {
629         if(bg->hasRole(client->role()))
630         {
631             unsigned layer = client->layerID();
632             HMI_INFO("%s go to foreground", client->appID().c_str());
633             bg->removeLayerFromState(layer);
634             auto wm_layer = this->getWMLayer(layer);
635             wm_layer->addLayerToState(layer);
636             /* TODO: manipulate state directly
637             LayerState bg_ls = bg->getLayerState();
638             bg_ls.removeLayer(layer);
639             LayerState ls = wm_layer->getLayerState();
640             ls.addLayer(layer); */
641             bg->dump();
642             wm_layer->dump();
643             ret = true;
644         }
645     }
646     return ret;
647 }
648
649 } // namespace wm