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