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