Merge "Drop 2017 suffix"
[apps/agl-service-windowmanager.git] / src / layers.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
17 #include <regex>
18
19 #include "layers.hpp"
20 #include "json_helper.hpp"
21 #include "hmi-debug.h"
22
23 namespace wm
24 {
25
26 using json = nlohmann::json;
27
28 layer::layer(nlohmann::json const &j)
29 {
30     this->role = j["role"];
31     this->name = j["name"];
32     this->layer_id = j["layer_id"];
33
34     HMI_DEBUG("wm", "layer_id:%d name:%s", this->layer_id, this->name.c_str());
35 }
36
37 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
38 {
39     try
40     {
41         layer_map stl{};
42         auto m = j["mappings"];
43
44         std::transform(std::cbegin(m), std::cend(m),
45                        std::inserter(stl.mapping, stl.mapping.end()),
46                        [](nlohmann::json const &j) {
47                            return std::pair<int, struct layer>(
48                                j.value("layer_id", -1), layer(j));
49                        });
50
51         // TODO: add sanity checks here?
52         // * check for double IDs
53         // * check for double names/roles
54
55         stl.layers.reserve(m.size());
56         std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
57                        std::back_inserter(stl.layers),
58                        [&stl](std::pair<int, struct layer> const &k) {
59                            stl.roles.emplace_back(
60                                std::make_pair(k.second.role, k.second.layer_id));
61                            return unsigned(k.second.layer_id);
62                        });
63
64         std::sort(stl.layers.begin(), stl.layers.end());
65
66         for (auto i : stl.mapping)
67         {
68             if (i.second.name.empty())
69             {
70                 return Err<struct layer_map>("Found mapping w/o name");
71             }
72             if (i.second.layer_id == -1)
73             {
74                 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
75             }
76         }
77
78         auto msi = j.find("main_surface");
79         if (msi != j.end())
80         {
81             stl.main_surface_name = msi->value("surface_role", "");
82             stl.main_surface = -1;
83         }
84
85         return Ok(stl);
86     }
87     catch (std::exception &e)
88     {
89         return Err<struct layer_map>(e.what());
90     }
91 }
92
93 optional<int>
94 layer_map::get_layer_id(int surface_id)
95 {
96     auto i = this->surfaces.find(surface_id);
97     if (i != this->surfaces.end())
98     {
99         return optional<int>(i->second);
100     }
101     return nullopt;
102 }
103
104 optional<int> layer_map::get_layer_id(std::string const &role)
105 {
106     for (auto const &r : this->roles)
107     {
108         auto re = std::regex(r.first);
109         if (std::regex_match(role, re))
110         {
111             HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
112             return optional<int>(r.second);
113         }
114     }
115     HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
116     return nullopt;
117 }
118
119 json layer::to_json() const
120 {
121     auto is_full = this->rect == compositor::full_rect;
122
123     json r{};
124     if (is_full)
125     {
126         r = {{"type", "full"}};
127     }
128     else
129     {
130         r = {{"type", "rect"},
131              {"rect",
132               {{"x", this->rect.x},
133                {"y", this->rect.y},
134                {"width", this->rect.w},
135                {"height", this->rect.h}}}};
136     }
137
138     return {
139         {"name", this->name},
140         {"role", this->role},
141         {"layer_id", this->layer_id},
142         {"area", r},
143     };
144 }
145
146 json layer_map::to_json() const
147 {
148     json j{};
149     for (auto const &i : this->mapping)
150     {
151         j.push_back(i.second.to_json());
152     }
153     return j;
154 }
155
156 void layer_map::setupArea(double scaling)
157 {
158     compositor::rect rct;
159
160     rct = this->area2size["normal.full"];
161     this->area2size["normalfull"] = rct;
162     this->area2size["normal"] = rct;
163
164     for (auto &i : this->area2size)
165     {
166         i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
167         i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
168         i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
169         i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
170
171         HMI_DEBUG("wm:lm", "area:%s size(after) : x:%d y:%d w:%d h:%d",
172             i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
173     }
174 }
175
176 compositor::rect layer_map::getAreaSize(const std::string &area)
177 {
178     return area2size[area];
179 }
180
181 int layer_map::loadAreaDb()
182 {
183     HMI_DEBUG("wm:lm", "Call");
184
185     // Get afm application installed dir
186     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
187     HMI_DEBUG("wm:lm", "afm_app_install_dir:%s", afm_app_install_dir);
188
189     std::string file_name;
190     if (!afm_app_install_dir)
191     {
192         HMI_ERROR("wm:lm", "AFM_APP_INSTALL_DIR is not defined");
193     }
194     else
195     {
196         file_name = std::string(afm_app_install_dir) + std::string("/etc/areas.db");
197     }
198
199     // Load area.db
200     json_object *json_obj;
201     int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
202     if (0 > ret)
203     {
204         HMI_DEBUG("wm:lm", "Could not open area.db, so use default area information");
205         json_obj = json_tokener_parse(kDefaultAreaDb);
206     }
207     HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
208
209     // Perse areas
210     HMI_DEBUG("wm:lm", "Perse areas");
211     json_object *json_cfg;
212     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
213     {
214         HMI_ERROR("wm:lm", "Parse Error!!");
215         return -1;
216     }
217
218     int len = json_object_array_length(json_cfg);
219     HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
220     HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
221
222     const char *area;
223     for (int i = 0; i < len; i++)
224     {
225         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
226         HMI_DEBUG("wm:lm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
227
228         area = jh::getStringFromJson(json_tmp, "name");
229         if (nullptr == area)
230         {
231             HMI_ERROR("wm:lm", "Parse Error!!");
232             return -1;
233         }
234         HMI_DEBUG("wm:lm", "> area:%s", area);
235
236         json_object *json_rect;
237         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
238         {
239             HMI_ERROR("wm:lm", "Parse Error!!");
240             return -1;
241         }
242         HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
243
244         compositor::rect area_size;
245         area_size.x = jh::getIntFromJson(json_rect, "x");
246         area_size.y = jh::getIntFromJson(json_rect, "y");
247         area_size.w = jh::getIntFromJson(json_rect, "w");
248         area_size.h = jh::getIntFromJson(json_rect, "h");
249
250         this->area2size[area] = area_size;
251     }
252
253     // Check
254     for (auto itr = this->area2size.begin();
255          itr != this->area2size.end(); ++itr)
256     {
257         HMI_DEBUG("wm:lm", "area:%s x:%d y:%d w:%d h:%d",
258                   itr->first.c_str(), itr->second.x, itr->second.y,
259                   itr->second.w, itr->second.h);
260     }
261
262     // Release json_object
263     json_object_put(json_obj);
264
265     return 0;
266 }
267
268 const char* layer_map::kDefaultAreaDb = "{ \
269     \"areas\": [ \
270         { \
271             \"name\": \"fullscreen\", \
272             \"rect\": { \
273                 \"x\": 0, \
274                 \"y\": 0, \
275                 \"w\": 1080, \
276                 \"h\": 1920 \
277             } \
278         }, \
279         { \
280             \"name\": \"normal.full\", \
281             \"rect\": { \
282                 \"x\": 0, \
283                 \"y\": 218, \
284                 \"w\": 1080, \
285                 \"h\": 1488 \
286             } \
287         }, \
288         { \
289             \"name\": \"split.main\", \
290             \"rect\": { \
291                 \"x\": 0, \
292                 \"y\": 218, \
293                 \"w\": 1080, \
294                 \"h\": 744 \
295             } \
296         }, \
297         { \
298             \"name\": \"split.sub\", \
299             \"rect\": { \
300                 \"x\": 0, \
301                 \"y\": 962, \
302                 \"w\": 1080, \
303                 \"h\": 744 \
304             } \
305         }, \
306         { \
307             \"name\": \"software_keyboard\", \
308             \"rect\": { \
309                 \"x\": 0, \
310                 \"y\": 962, \
311                 \"w\": 1080, \
312                 \"h\": 744 \
313             } \
314         }, \
315         { \
316             \"name\": \"restriction.normal\", \
317             \"rect\": { \
318                 \"x\": 0, \
319                 \"y\": 218, \
320                 \"w\": 1080, \
321                 \"h\": 1488 \
322             } \
323         }, \
324         { \
325             \"name\": \"restriction.split.main\", \
326             \"rect\": { \
327                 \"x\": 0, \
328                 \"y\": 218, \
329                 \"w\": 1080, \
330                 \"h\": 744 \
331             } \
332         }, \
333         { \
334             \"name\": \"restriction.split.sub\", \
335             \"rect\": { \
336                 \"x\": 0, \
337                 \"y\": 962, \
338                 \"w\": 1080, \
339                 \"h\": 744 \
340             } \
341         }, \
342         { \
343             \"name\": \"on_screen\", \
344             \"rect\": { \
345                 \"x\": 0, \
346                 \"y\": 218, \
347                 \"w\": 1080, \
348                 \"h\": 1488 \
349             } \
350         } \
351     ] \
352 }";
353
354 } // namespace wm