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 HMI_DEBUG("wm", "layer_id:%d name:%s", this->layer_id, this->name.c_str());
39 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
44 auto m = j["mappings"];
46 std::transform(std::cbegin(m), std::cend(m),
47 std::inserter(stl.mapping, stl.mapping.end()),
48 [](nlohmann::json const &j) {
49 return std::pair<int, struct layer>(
50 j.value("layer_id", -1), layer(j));
53 // TODO: add sanity checks here?
54 // * check for double IDs
55 // * check for double names/roles
57 stl.layers.reserve(m.size());
58 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
59 std::back_inserter(stl.layers),
60 [&stl](std::pair<int, struct layer> const &k) {
61 stl.roles.emplace_back(
62 std::make_pair(k.second.role, k.second.layer_id));
63 return unsigned(k.second.layer_id);
66 std::sort(stl.layers.begin(), stl.layers.end());
68 for (auto i : stl.mapping)
70 if (i.second.name.empty())
72 return Err<struct layer_map>("Found mapping w/o name");
74 if (i.second.layer_id == -1)
76 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
80 auto msi = j.find("main_surface");
83 stl.main_surface_name = msi->value("surface_role", "");
84 stl.main_surface = -1;
89 catch (std::exception &e)
91 return Err<struct layer_map>(e.what());
96 layer_map::get_layer_id(int surface_id)
98 auto i = this->surfaces.find(surface_id);
99 if (i != this->surfaces.end())
101 return optional<int>(i->second);
106 optional<int> layer_map::get_layer_id(std::string const &role)
108 for (auto const &r : this->roles)
110 auto re = std::regex(r.first);
111 if (std::regex_match(role, re))
113 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
114 return optional<int>(r.second);
117 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
121 json layer::to_json() const
123 auto is_full = this->rect == compositor::full_rect;
128 r = {{"type", "full"}};
132 r = {{"type", "rect"},
134 {{"x", this->rect.x},
136 {"width", this->rect.w},
137 {"height", this->rect.h}}}};
141 {"name", this->name},
142 {"role", this->role},
143 {"layer_id", this->layer_id},
148 json layer_map::to_json() const
151 for (auto const &i : this->mapping)
153 j.push_back(i.second.to_json());
158 void layer_map::setupArea(double scaling)
160 compositor::rect rct;
162 rct = this->area2size["normal.full"];
163 this->area2size["normalfull"] = rct;
164 this->area2size["normal"] = rct;
166 for (auto &i : this->area2size)
168 i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
169 i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
170 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
171 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
173 HMI_DEBUG("wm:lm", "area:%s size(after) : x:%d y:%d w:%d h:%d",
174 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
178 compositor::rect layer_map::getAreaSize(const std::string &area)
180 return area2size[area];
183 int layer_map::loadAreaDb()
185 HMI_DEBUG("wm:lm", "Call");
187 std::string file_name(get_file_path("areas.db"));
190 json_object *json_obj;
191 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
194 HMI_DEBUG("wm:lm", "Could not open area.db, so use default area information");
195 json_obj = json_tokener_parse(kDefaultAreaDb);
197 HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
200 HMI_DEBUG("wm:lm", "Perse areas");
201 json_object *json_cfg;
202 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
204 HMI_ERROR("wm:lm", "Parse Error!!");
208 int len = json_object_array_length(json_cfg);
209 HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
210 HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
213 for (int i = 0; i < len; i++)
215 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
216 HMI_DEBUG("wm:lm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
218 area = jh::getStringFromJson(json_tmp, "name");
221 HMI_ERROR("wm:lm", "Parse Error!!");
224 HMI_DEBUG("wm:lm", "> area:%s", area);
226 json_object *json_rect;
227 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
229 HMI_ERROR("wm:lm", "Parse Error!!");
232 HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
234 compositor::rect area_size;
235 area_size.x = jh::getIntFromJson(json_rect, "x");
236 area_size.y = jh::getIntFromJson(json_rect, "y");
237 area_size.w = jh::getIntFromJson(json_rect, "w");
238 area_size.h = jh::getIntFromJson(json_rect, "h");
240 this->area2size[area] = area_size;
244 for (auto itr = this->area2size.begin();
245 itr != this->area2size.end(); ++itr)
247 HMI_DEBUG("wm:lm", "area:%s x:%d y:%d w:%d h:%d",
248 itr->first.c_str(), itr->second.x, itr->second.y,
249 itr->second.w, itr->second.h);
252 // Release json_object
253 json_object_put(json_obj);
258 const char* layer_map::kDefaultAreaDb = "{ \
261 \"name\": \"fullscreen\", \
270 \"name\": \"normal.full\", \
279 \"name\": \"split.main\", \
288 \"name\": \"split.sub\", \
297 \"name\": \"software_keyboard\", \
306 \"name\": \"restriction.normal\", \
315 \"name\": \"restriction.split.main\", \
324 \"name\": \"restriction.split.sub\", \
333 \"name\": \"on_screen\", \