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