Add loadLayerSetting
[apps/agl-service-windowmanager-2017.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/area.db"\r
23 #define LC_LAYER_SETTING_PATH "/etc/layer_setting.json"\r
24 \r
25 using std::string;\r
26 \r
27 namespace wm {\r
28 \r
29 static void notification_static(ilmObjectType object,\r
30                             t_ilm_uint id,\r
31                             t_ilm_bool created,\r
32                             void* data)\r
33 {\r
34     static_cast<LayerControl*>(data)->dispatchILMEvent(object, id, created);\r
35 }\r
36 \r
37 LayerControl::LayerControl(const std::string& root)\r
38 {\r
39     string area_path = root + LC_AREA_PATH;\r
40     string layer_path= root + LC_LAYER_SETTING_PATH;\r
41     // load layers.setting.json\r
42     WMError ret = this->loadLayerSetting(layer_path);\r
43     assert(ret == WMError::SUCCESS);\r
44     // load area.db\r
45     ret = this->loadAreaDb(area_path);\r
46     assert(ret == WMError::SUCCESS);\r
47 }\r
48 \r
49 WMError LayerControl::init()\r
50 {\r
51     ilmErrorTypes rc = ilm_init();\r
52     t_ilm_uint num = 0;\r
53     t_ilm_uint *ids;\r
54     int cnt = 0;\r
55 \r
56     while (rc != ILM_SUCCESS)\r
57     {\r
58         cnt++;\r
59         if (20 <= cnt)\r
60         {\r
61             HMI_ERROR("Could not connect to compositor");\r
62             goto lc_init_error;\r
63         }\r
64         HMI_ERROR("Wait to start weston ...");\r
65         sleep(1);\r
66         ilm_init();\r
67     }\r
68     if(rc != ILM_SUCCESS) goto lc_init_error;\r
69 \r
70     rc = ilm_getScreenIDs(&num, &ids);\r
71 \r
72     if(rc != ILM_SUCCESS) goto lc_init_error;\r
73 \r
74     for(unsigned i = 0; i < num; i++)\r
75     {\r
76         HMI_INFO("get screen: %d", ids[i]);\r
77     }\r
78     // Currently, 0 is only available\r
79     this->screenID = ids[0];\r
80 \r
81     rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);\r
82 \r
83     if(rc != ILM_SUCCESS) goto lc_init_error;\r
84 \r
85     // Register Callback from ILM\r
86     ilm_registerNotification(notification_static, this);\r
87 \r
88     return WMError::SUCCESS;\r
89 \r
90 lc_init_error:\r
91     HMI_ERROR("Failed to initialize. Terminate WM");\r
92 \r
93     return WMError::FAIL;\r
94 }\r
95 \r
96 unsigned LayerControl::getNewLayerID(const string& role)\r
97 {\r
98     unsigned ret = 0;\r
99     for(const auto& l: this->wm_layers)\r
100     {\r
101         ret = l->getNewLayerID(role);\r
102         if(ret != 0)\r
103         {\r
104             break;\r
105         }\r
106     }\r
107     return ret;\r
108 }\r
109 \r
110 WMError LayerControl::updateLayer(WMLayer& wm_layer)\r
111 {\r
112     return WMError::SUCCESS;\r
113 }\r
114 \r
115 void LayerControl::commitChange() {}\r
116 \r
117 void LayerControl::undoUpdate() {}\r
118 \r
119 WMError LayerControl::loadLayerSetting(const string &path)\r
120 {\r
121     HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);\r
122 \r
123     json_object *json_obj, *json_cfg;\r
124     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);\r
125     if (0 > ret)\r
126     {\r
127         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());\r
128         return WMError::FAIL;\r
129     }\r
130     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));\r
131 \r
132     if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))\r
133     {\r
134         HMI_ERROR("Parse Error!!");\r
135         return WMError::FAIL;\r
136     }\r
137 \r
138     int len = json_object_array_length(json_cfg);\r
139     HMI_DEBUG("json_cfg len:%d", len);\r
140 \r
141     for (int i = 0; i < len; i++)\r
142     {\r
143         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);\r
144         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));\r
145 \r
146         this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp));\r
147     }\r
148 \r
149     return WMError::SUCCESS;\r
150 }\r
151 \r
152 WMError LayerControl::loadAreaDb(const std::string& path)\r
153 {\r
154     // Load area.db\r
155     json_object *json_obj;\r
156     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);\r
157     if (0 > ret)\r
158     {\r
159         HMI_DEBUG("Could not open %s, so use default area information", path.c_str());\r
160         return WMError::FAIL;\r
161     }\r
162     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));\r
163 \r
164     // Perse areas\r
165     json_object *json_cfg;\r
166     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))\r
167     {\r
168         HMI_ERROR("Parse Error!!");\r
169         return WMError::FAIL;\r
170     }\r
171 \r
172     int len = json_object_array_length(json_cfg);\r
173     HMI_DEBUG("json_cfg len:%d", len);\r
174 \r
175     const char *area;\r
176     for (int i = 0; i < len; i++)\r
177     {\r
178         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);\r
179         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));\r
180 \r
181         area = jh::getStringFromJson(json_tmp, "name");\r
182         if (nullptr == area)\r
183         {\r
184             HMI_ERROR("Parse Error!!");\r
185             return WMError::FAIL;\r
186         }\r
187         HMI_DEBUG("> area:%s", area);\r
188 \r
189         json_object *json_rect;\r
190         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))\r
191         {\r
192             HMI_ERROR("Parse Error!!");\r
193             return WMError::FAIL;\r
194         }\r
195         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));\r
196 \r
197         struct rect area_size;\r
198         area_size.x = jh::getIntFromJson(json_rect, "x");\r
199         area_size.y = jh::getIntFromJson(json_rect, "y");\r
200         area_size.w = jh::getIntFromJson(json_rect, "w");\r
201         area_size.h = jh::getIntFromJson(json_rect, "h");\r
202 \r
203         this->area2size[area] = area_size;\r
204     }\r
205 \r
206     // Check\r
207     for (const auto& itr : this->area2size)\r
208     {\r
209         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",\r
210                   itr.first.c_str(), itr.second.x, itr.second.y,\r
211                   itr.second.w, itr.second.h);\r
212     }\r
213 \r
214     // Release json_object\r
215     json_object_put(json_obj);\r
216 \r
217     return WMError::SUCCESS;\r
218 }\r
219 \r
220 void LayerControl::dispatchILMEvent(ilmObjectType object, t_ilm_uint id, t_ilm_bool created)\r
221 {\r
222     ;\r
223 }\r
224 \r
225 } // namespace wm