Include WMClient into WMRequest
[apps/agl-service-windowmanager.git] / src / wm_layer.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 "wm_layer.hpp"
20 #include "json_helper.hpp"
21 #include "util.hpp"
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("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("role %s matches layer %d", role.c_str(), r.second);
112             return optional<int>(r.second);
113         }
114     }
115     HMI_DEBUG("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("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     // Get afm application installed dir
184     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
185     HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
186
187     std::string file_name;
188     if (!afm_app_install_dir)
189     {
190         HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
191     }
192     else
193     {
194         file_name = std::string(afm_app_install_dir) + std::string("/etc/areas.db");
195     }
196
197     // Load area.db
198     json_object *json_obj;
199     int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
200     if (0 > ret)
201     {
202         HMI_DEBUG("Could not open area.db, so use default area information");
203         json_obj = json_tokener_parse(kDefaultAreaDb);
204     }
205     HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
206
207     // Perse areas
208     HMI_DEBUG("Perse areas");
209     json_object *json_cfg;
210     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
211     {
212         HMI_ERROR("Parse Error!!");
213         return -1;
214     }
215
216     int len = json_object_array_length(json_cfg);
217     HMI_DEBUG("json_cfg len:%d", len);
218     HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
219
220     const char *area;
221     for (int i = 0; i < len; i++)
222     {
223         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
224         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
225
226         area = jh::getStringFromJson(json_tmp, "name");
227         if (nullptr == area)
228         {
229             HMI_ERROR("Parse Error!!");
230             return -1;
231         }
232         HMI_DEBUG("> area:%s", area);
233
234         json_object *json_rect;
235         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
236         {
237             HMI_ERROR("Parse Error!!");
238             return -1;
239         }
240         HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
241
242         compositor::rect area_size;
243         area_size.x = jh::getIntFromJson(json_rect, "x");
244         area_size.y = jh::getIntFromJson(json_rect, "y");
245         area_size.w = jh::getIntFromJson(json_rect, "w");
246         area_size.h = jh::getIntFromJson(json_rect, "h");
247
248         this->area2size[area] = area_size;
249     }
250
251     // Check
252     for (auto itr = this->area2size.begin();
253          itr != this->area2size.end(); ++itr)
254     {
255         HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
256                   itr->first.c_str(), itr->second.x, itr->second.y,
257                   itr->second.w, itr->second.h);
258     }
259
260     // Release json_object
261     json_object_put(json_obj);
262
263     return 0;
264 }
265
266 const char* layer_map::kDefaultAreaDb = "{ \
267     \"areas\": [ \
268         { \
269             \"name\": \"fullscreen\", \
270             \"rect\": { \
271                 \"x\": 0, \
272                 \"y\": 0, \
273                 \"w\": 1080, \
274                 \"h\": 1920 \
275             } \
276         }, \
277         { \
278             \"name\": \"normal.full\", \
279             \"rect\": { \
280                 \"x\": 0, \
281                 \"y\": 218, \
282                 \"w\": 1080, \
283                 \"h\": 1488 \
284             } \
285         }, \
286         { \
287             \"name\": \"split.main\", \
288             \"rect\": { \
289                 \"x\": 0, \
290                 \"y\": 218, \
291                 \"w\": 1080, \
292                 \"h\": 744 \
293             } \
294         }, \
295         { \
296             \"name\": \"split.sub\", \
297             \"rect\": { \
298                 \"x\": 0, \
299                 \"y\": 962, \
300                 \"w\": 1080, \
301                 \"h\": 744 \
302             } \
303         }, \
304         { \
305             \"name\": \"software_keyboard\", \
306             \"rect\": { \
307                 \"x\": 0, \
308                 \"y\": 962, \
309                 \"w\": 1080, \
310                 \"h\": 744 \
311             } \
312         }, \
313         { \
314             \"name\": \"restriction.normal\", \
315             \"rect\": { \
316                 \"x\": 0, \
317                 \"y\": 218, \
318                 \"w\": 1080, \
319                 \"h\": 1488 \
320             } \
321         }, \
322         { \
323             \"name\": \"restriction.split.main\", \
324             \"rect\": { \
325                 \"x\": 0, \
326                 \"y\": 218, \
327                 \"w\": 1080, \
328                 \"h\": 744 \
329             } \
330         }, \
331         { \
332             \"name\": \"restriction.split.sub\", \
333             \"rect\": { \
334                 \"x\": 0, \
335                 \"y\": 962, \
336                 \"w\": 1080, \
337                 \"h\": 744 \
338             } \
339         }, \
340         { \
341             \"name\": \"on_screen\", \
342             \"rect\": { \
343                 \"x\": 0, \
344                 \"y\": 218, \
345                 \"w\": 1080, \
346                 \"h\": 1488 \
347             } \
348         } \
349     ] \
350 }";
351
352 } // namespace wm