2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3 * Copyright (c) 2018 Konsulko Group
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "json_helper.hpp"
23 #include "hmi-debug.h"
28 using json = nlohmann::json;
30 layer::layer(nlohmann::json const &j)
32 this->role = j["role"];
33 this->name = j["name"];
34 this->layer_id = j["layer_id"];
36 // Init flag of normal layout only
37 this->is_normal_layout_only = true;
39 auto split_layouts = j.find("split_layouts");
40 if (split_layouts != j.end())
43 // Clear flag of normal layout only
44 this->is_normal_layout_only = false;
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) {
52 sl["name"], sl["main_match"], sl["sub_match"]
55 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
58 l.name.c_str(), l.main_match.c_str(),
63 HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
64 this->layer_id, this->is_normal_layout_only);
67 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
72 auto m = j["mappings"];
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));
81 // TODO: add sanity checks here?
82 // * check for double IDs
83 // * check for double names/roles
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);
94 std::sort(stl.layers.begin(), stl.layers.end());
96 for (auto i : stl.mapping)
98 if (i.second.name.empty())
100 return Err<struct layer_map>("Found mapping w/o name");
102 if (i.second.layer_id == -1)
104 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
108 auto msi = j.find("main_surface");
111 stl.main_surface_name = msi->value("surface_role", "");
112 stl.main_surface = -1;
117 catch (std::exception &e)
119 return Err<struct layer_map>(e.what());
124 layer_map::get_layer_id(int surface_id)
126 auto i = this->surfaces.find(surface_id);
127 if (i != this->surfaces.end())
129 return optional<int>(i->second);
134 optional<int> layer_map::get_layer_id(std::string const &role)
136 for (auto const &r : this->roles)
138 auto re = std::regex(r.first);
139 if (std::regex_match(role, re))
141 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
142 return optional<int>(r.second);
145 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
149 json layer::to_json() const
151 auto is_full = this->rect == compositor::full_rect;
156 r = {{"type", "full"}};
160 r = {{"type", "rect"},
162 {{"x", this->rect.x},
164 {"width", this->rect.w},
165 {"height", this->rect.h}}}};
169 {"name", this->name},
170 {"role", this->role},
171 {"layer_id", this->layer_id},
176 json layer_map::to_json() const
179 for (auto const &i : this->mapping)
181 j.push_back(i.second.to_json());
186 void layer_map::setupArea(double scaling)
188 compositor::rect rct;
190 rct = this->area2size["normal.full"];
191 this->area2size["normalfull"] = rct;
192 this->area2size["normal"] = rct;
194 for (auto &i : this->area2size)
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);
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);
206 compositor::rect layer_map::getAreaSize(const std::string &area)
208 return area2size[area];
211 int layer_map::loadAreaDb()
213 HMI_DEBUG("wm:lm", "Call");
215 std::string file_name(get_file_path("areas.db"));
218 json_object *json_obj;
219 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
222 HMI_DEBUG("wm:lm", "Could not open area.db, so use default area information");
223 json_obj = json_tokener_parse(kDefaultAreaDb);
225 HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
228 HMI_DEBUG("wm:lm", "Perse areas");
229 json_object *json_cfg;
230 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
232 HMI_ERROR("wm:lm", "Parse Error!!");
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));
241 for (int i = 0; i < len; i++)
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));
246 area = jh::getStringFromJson(json_tmp, "name");
249 HMI_ERROR("wm:lm", "Parse Error!!");
252 HMI_DEBUG("wm:lm", "> area:%s", area);
254 json_object *json_rect;
255 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
257 HMI_ERROR("wm:lm", "Parse Error!!");
260 HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
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");
268 this->area2size[area] = area_size;
272 for (auto itr = this->area2size.begin();
273 itr != this->area2size.end(); ++itr)
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);
280 // Release json_object
281 json_object_put(json_obj);
286 const char* layer_map::kDefaultAreaDb = "{ \
289 \"name\": \"fullscreen\", \
298 \"name\": \"normal.full\", \
307 \"name\": \"split.main\", \
316 \"name\": \"split.sub\", \
325 \"name\": \"software_keyboard\", \
334 \"name\": \"restriction.normal\", \
343 \"name\": \"restriction.split.main\", \
352 \"name\": \"restriction.split.sub\", \
361 \"name\": \"on_screen\", \