Update wm_layer
[apps/agl-service-windowmanager.git] / src / wm_layer_control.cpp
1 /*\r
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 #include <assert.h>\r
17 #include <unistd.h>\r
18 #include "wm_layer_control.hpp"\r
19 #include "wm_layer.hpp"\r
20 #include "json_helper.hpp"\r
21 \r
22 #define LC_AREA_PATH "/etc/areas.db"\r
23 #define LC_LAYER_SETTING_PATH "/etc/layer_setting.json"\r
24 #define LC_DEFAULT_AREA "normal.full"\r
25 \r
26 using std::string;\r
27 using std::vector;\r
28 \r
29 namespace wm {\r
30 \r
31 LayerControl* g_lc_ctxt;\r
32 \r
33 static void createCallback_static(ilmObjectType object,\r
34                             t_ilm_uint id,\r
35                             t_ilm_bool created,\r
36                             void* data)\r
37 {\r
38     static_cast<LayerControl*>(data)->dispatchCreateEvent(object, id, created);\r
39 }\r
40 \r
41 static void surfaceCallback_static(t_ilm_surface surface,\r
42             struct ilmSurfaceProperties* surface_prop,\r
43             t_ilm_notification_mask mask)\r
44 {\r
45     g_lc_ctxt->dispatchPropertyChangeEvent(surface, surface_prop, mask);\r
46 }\r
47 \r
48 static void layerCallback_static(t_ilm_layer layer,\r
49             struct ilmLayerProperties* layer_prop,\r
50             t_ilm_notification_mask mask)\r
51 {\r
52     g_lc_ctxt->dispatchPropertyChangeEvent(layer, layer_prop, mask);\r
53 }\r
54 \r
55 LayerControl::LayerControl(const std::string& root)\r
56 {\r
57     string area_path = root + LC_AREA_PATH;\r
58     string layer_path= root + LC_LAYER_SETTING_PATH;\r
59     // load layers.setting.json\r
60     WMError ret = this->loadLayerSetting(layer_path);\r
61     assert(ret == WMError::SUCCESS);\r
62     // load area.db\r
63     ret = this->loadAreaDb(area_path);\r
64     assert(ret == WMError::SUCCESS);\r
65 }\r
66 \r
67 WMError LayerControl::init(const LayerControlCallbacks& cb)\r
68 {\r
69     ilmErrorTypes rc = ilm_init();\r
70     t_ilm_uint num = 0;\r
71     t_ilm_uint *ids;\r
72     int cnt = 0;\r
73 \r
74     while (rc != ILM_SUCCESS)\r
75     {\r
76         cnt++;\r
77         if (20 <= cnt)\r
78         {\r
79             HMI_ERROR("Could not connect to compositor");\r
80             goto lc_init_error;\r
81         }\r
82         HMI_ERROR("Wait to start weston ...");\r
83         sleep(1);\r
84         ilm_init();\r
85     }\r
86     if(rc != ILM_SUCCESS) goto lc_init_error;\r
87 \r
88     // Get current screen setting\r
89     rc = ilm_getScreenIDs(&num, &ids);\r
90 \r
91     if(rc != ILM_SUCCESS) goto lc_init_error;\r
92 \r
93     for(unsigned i = 0; i < num; i++)\r
94     {\r
95         HMI_INFO("get screen: %d", ids[i]);\r
96     }\r
97     // Currently, 0 is only available\r
98     this->screenID = ids[0];\r
99 \r
100     rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);\r
101 \r
102     if(rc != ILM_SUCCESS) goto lc_init_error;\r
103 \r
104     // Register Callback from ILM\r
105     this->cb = cb;\r
106     ilm_registerNotification(createCallback_static, this);\r
107 \r
108     return WMError::SUCCESS;\r
109 \r
110 lc_init_error:\r
111     HMI_ERROR("Failed to initialize. Terminate WM");\r
112 \r
113     return WMError::FAIL;\r
114 }\r
115 \r
116 void LayerControl::createNewLayer(unsigned id)\r
117 {\r
118     HMI_INFO("create new ID :%d", id);\r
119     struct rect rct = this->area2size[LC_DEFAULT_AREA];\r
120     ilm_layerCreateWithDimension(&id, rct.w, rct.h);\r
121     ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);\r
122     ilm_layerSetDestinationRectangle(id, rct.x, rct.y, rct.w, rct.h);\r
123     ilm_layerSetOpacity(id, 1.0);\r
124     ilm_layerSetVisibility(id, ILM_TRUE);\r
125     ilm_commitChanges();\r
126 }\r
127 \r
128 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)\r
129 {\r
130     unsigned ret = 0;\r
131     for(const auto& l: this->wm_layers)\r
132     {\r
133         ret = l->getNewLayerID(role);\r
134         if(ret != 0)\r
135         {\r
136             *layer_name = l->layerName();\r
137             break;\r
138         }\r
139     }\r
140     return ret;\r
141 }\r
142 \r
143 struct rect LayerControl::getAreaSize(const std::string& area)\r
144 {\r
145     return area2size[area];\r
146 }\r
147 \r
148 void LayerControl::setupArea(double scaling)\r
149 {\r
150     struct rect rct;\r
151     this->scaling = scaling;\r
152 \r
153     rct = this->area2size["normal.full"];\r
154     this->area2size["normalfull"] = rct;\r
155     this->area2size["normal"] = rct;\r
156 \r
157     for (auto &i : this->area2size)\r
158     {\r
159         i.second.x = static_cast<int>(scaling * i.second.x + 0.5);\r
160         i.second.y = static_cast<int>(scaling * i.second.y + 0.5);\r
161         i.second.w = static_cast<int>(scaling * i.second.w + 0.5);\r
162         i.second.h = static_cast<int>(scaling * i.second.h + 0.5);\r
163 \r
164         HMI_DEBUG("wm:lm", "area:%s size(after) : x:%d y:%d w:%d h:%d",\r
165             i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);\r
166     }\r
167 }\r
168 \r
169 Screen LayerControl::getScreenInfo()\r
170 {\r
171     return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);\r
172 }\r
173 \r
174 double LayerControl::scale()\r
175 {\r
176     return this->scaling;\r
177 }\r
178 \r
179 WMError LayerControl::updateLayer(LayerState& layer_state)\r
180 {\r
181     return WMError::SUCCESS;\r
182 }\r
183 \r
184 WMError LayerControl::commitChange()\r
185 {\r
186     WMError rc = WMError::SUCCESS;\r
187     vector<unsigned> ivi_l_ids;\r
188     for(const auto& l : this->wm_layers)\r
189     {\r
190         auto state = l->getLayerState();\r
191         for(const auto& id : state.getIviIdList())\r
192         {\r
193             ivi_l_ids.push_back(id);\r
194         }\r
195     }\r
196     t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];\r
197     if(id_array == nullptr)\r
198     {\r
199         HMI_WARNING("short memory");\r
200         this->undoUpdate();\r
201         return WMError::FAIL;\r
202     }\r
203     int count = 0;\r
204     for(const auto& i : ivi_l_ids)\r
205     {\r
206         id_array[count] = i;\r
207     }\r
208 \r
209     ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());\r
210     if(ret != ILM_SUCCESS)\r
211     {\r
212         this->undoUpdate();\r
213         rc = WMError::FAIL;\r
214     }\r
215     delete id_array;\r
216     return rc;\r
217 }\r
218 \r
219 void LayerControl::undoUpdate() {}\r
220 \r
221 WMError LayerControl::loadLayerSetting(const string &path)\r
222 {\r
223     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);\r
224 \r
225     json_object *json_obj, *json_cfg;\r
226     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);\r
227     if (0 > ret)\r
228     {\r
229         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());\r
230         return WMError::FAIL;\r
231     }\r
232     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));\r
233 \r
234     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))\r
235     {\r
236         HMI_ERROR("Parse Error!!");\r
237         return WMError::FAIL;\r
238     }\r
239 \r
240     int len = json_object_array_length(json_cfg);\r
241     HMI_DEBUG("json_cfg len:%d", len);\r
242 \r
243     for (int i = 0; i < len; i++)\r
244     {\r
245         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);\r
246         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));\r
247 \r
248         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp));\r
249     }\r
250 \r
251     return WMError::SUCCESS;\r
252 }\r
253 \r
254 WMError LayerControl::loadAreaDb(const std::string& path)\r
255 {\r
256     // Load area.db\r
257     json_object *json_obj;\r
258     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);\r
259     if (0 > ret)\r
260     {\r
261         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());\r
262         return WMError::FAIL;\r
263     }\r
264     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));\r
265 \r
266     // Perse areas\r
267     json_object *json_cfg;\r
268     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))\r
269     {\r
270         HMI_ERROR("Parse Error!!");\r
271         return WMError::FAIL;\r
272     }\r
273 \r
274     int len = json_object_array_length(json_cfg);\r
275     HMI_DEBUG("json_cfg len:%d", len);\r
276 \r
277     const char *area;\r
278     for (int i = 0; i < len; i++)\r
279     {\r
280         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);\r
281         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));\r
282 \r
283         area = jh::getStringFromJson(json_tmp, "name");\r
284         if (nullptr == area)\r
285         {\r
286             HMI_ERROR("Parse Error!!");\r
287             return WMError::FAIL;\r
288         }\r
289         HMI_DEBUG("> area:%s", area);\r
290 \r
291         json_object *json_rect;\r
292         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))\r
293         {\r
294             HMI_ERROR("Parse Error!!");\r
295             return WMError::FAIL;\r
296         }\r
297         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));\r
298 \r
299         struct rect area_size;\r
300         area_size.x = jh::getIntFromJson(json_rect, "x");\r
301         area_size.y = jh::getIntFromJson(json_rect, "y");\r
302         area_size.w = jh::getIntFromJson(json_rect, "w");\r
303         area_size.h = jh::getIntFromJson(json_rect, "h");\r
304 \r
305         this->area2size[area] = area_size;\r
306     }\r
307 \r
308     // Check\r
309     for (const auto& itr : this->area2size)\r
310     {\r
311         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",\r
312                   itr.first.c_str(), itr.second.x, itr.second.y,\r
313                   itr.second.w, itr.second.h);\r
314     }\r
315 \r
316     // Release json_object\r
317     json_object_put(json_obj);\r
318 \r
319     return WMError::SUCCESS;\r
320 }\r
321 \r
322 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)\r
323 {\r
324     this->cb.test(id);\r
325     if (ILM_SURFACE == object)\r
326     {\r
327         if (created)\r
328         {\r
329             ilmSurfaceProperties sp;\r
330             ilmErrorTypes rc;\r
331             rc = ilm_getPropertiesOfSurface(id, &sp);\r
332             if(rc != ILM_SUCCESS)\r
333                 return;\r
334             // this->cb->surfaceCreated(pid, id);\r
335             ilm_surfaceAddNotification(id, surfaceCallback_static);\r
336             ilm_surfaceSetSourceRectangle(id, 0, 0, sp.origSourceWidth, sp.origSourceHeight);\r
337         }\r
338         else\r
339         {\r
340             // this->cb->surfaceDestroyed(id);\r
341         }\r
342     }\r
343     if (ILM_LAYER == object)\r
344     {\r
345         if(created)\r
346         {\r
347             ilm_layerAddNotification(id, layerCallback_static);\r
348             // this->cb->layerCreated(id);\r
349         }\r
350         else\r
351         {\r
352             // this->cb->layerDestroyed(id); // Means Application is dead.\r
353         }\r
354     }\r
355 }\r
356 \r
357 void LayerControl::dispatchPropertyChangeEvent(unsigned id,\r
358         struct ilmSurfaceProperties* sprop,\r
359         t_ilm_notification_mask mask)\r
360 {\r
361     pid_t pid = sprop->creatorPid;\r
362     HMI_DEBUG("pid : %d", pid);\r
363 \r
364     if (ILM_NOTIFICATION_VISIBILITY & mask)\r
365     {\r
366         //this->cb->surfaceVisibilityChanged(id, sprop->visibility);\r
367     }\r
368     if (ILM_NOTIFICATION_OPACITY & mask)\r
369     {\r
370     }\r
371     if (ILM_NOTIFICATION_ORIENTATION & mask)\r
372     {\r
373     }\r
374     if (ILM_NOTIFICATION_SOURCE_RECT & mask)\r
375     {\r
376         // this->cb->surfaceSourceRectChanged(id, )\r
377     }\r
378     if (ILM_NOTIFICATION_DEST_RECT & mask)\r
379     {\r
380         // this->cb->surfaceSourceRectChanged(id, )\r
381     }\r
382     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)\r
383     {\r
384     }\r
385     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)\r
386     {\r
387         /* application being down */\r
388         // m_appLayers.remove(pid);\r
389     }\r
390     if (ILM_NOTIFICATION_CONFIGURED & mask)\r
391     {\r
392         /* qDebug("ILM_NOTIFICATION_CONFIGURED");\r
393         qDebug("  surfaceProperties %d", surface);\r
394         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);\r
395         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);\r
396 \r
397         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {\r
398             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);\r
399             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);\r
400         } else {\r
401             ilmErrorTypes result;\r
402             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);\r
403 \r
404             if (layer != 0) {\r
405                 configureAppSurface(surface,\r
406                                     surfaceProperties->origSourceWidth,\r
407                                     surfaceProperties->origSourceHeight);\r
408 \r
409                 result = ilm_layerAddSurface(layer, surface);\r
410                 if (result != ILM_SUCCESS) {\r
411                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);\r
412                 }\r
413                 ilm_commitChanges();\r
414             }\r
415         }\r
416         updateScreen(); */\r
417     }\r
418 }\r
419 \r
420 void LayerControl::dispatchPropertyChangeEvent(unsigned id,\r
421         struct ilmLayerProperties* lprop,\r
422         t_ilm_notification_mask mask)\r
423 {\r
424     if (ILM_NOTIFICATION_VISIBILITY & mask)\r
425     {\r
426         //this->cb->layerVisibilityChanged(id, sprop->visibility);\r
427     }\r
428     if (ILM_NOTIFICATION_OPACITY & mask)\r
429     {\r
430     }\r
431     if (ILM_NOTIFICATION_ORIENTATION & mask)\r
432     {\r
433     }\r
434     if (ILM_NOTIFICATION_SOURCE_RECT & mask)\r
435     {\r
436         // this->cb->surfaceSourceRectChanged(id, )\r
437     }\r
438     if (ILM_NOTIFICATION_DEST_RECT & mask)\r
439     {\r
440         // this->cb->surfaceSourceRectChanged(id, )\r
441     }\r
442     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)\r
443     {\r
444     }\r
445     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)\r
446     {\r
447         /* application being down */\r
448         // m_appLayers.remove(pid);\r
449     }\r
450     if (ILM_NOTIFICATION_CONFIGURED & mask)\r
451     {\r
452         /* qDebug("ILM_NOTIFICATION_CONFIGURED");\r
453         qDebug("  surfaceProperties %d", surface);\r
454         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);\r
455         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);\r
456 \r
457         if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {\r
458             addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);\r
459             configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);\r
460         } else {\r
461             ilmErrorTypes result;\r
462             t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);\r
463 \r
464             if (layer != 0) {\r
465                 configureAppSurface(surface,\r
466                                     surfaceProperties->origSourceWidth,\r
467                                     surfaceProperties->origSourceHeight);\r
468 \r
469                 result = ilm_layerAddSurface(layer, surface);\r
470                 if (result != ILM_SUCCESS) {\r
471                     qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);\r
472                 }\r
473                 ilm_commitChanges();\r
474             }\r
475         }\r
476         updateScreen(); */\r
477     }\r
478 }\r
479 \r
480 } // namespace wm