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"
27 using json = nlohmann::json;
29 layer::layer(nlohmann::json const &j)
31 this->role = j["role"];
32 this->name = j["name"];
33 this->layer_id = j["layer_id"];
35 HMI_DEBUG("layer_id:%d name:%s", this->layer_id, this->name.c_str());
38 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
43 auto m = j["mappings"];
45 std::transform(std::cbegin(m), std::cend(m),
46 std::inserter(stl.mapping, stl.mapping.end()),
47 [](nlohmann::json const &j) {
48 return std::pair<int, struct layer>(
49 j.value("layer_id", -1), layer(j));
52 // TODO: add sanity checks here?
53 // * check for double IDs
54 // * check for double names/roles
56 stl.layers.reserve(m.size());
57 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
58 std::back_inserter(stl.layers),
59 [&stl](std::pair<int, struct layer> const &k) {
60 stl.roles.emplace_back(
61 std::make_pair(k.second.role, k.second.layer_id));
62 return unsigned(k.second.layer_id);
65 std::sort(stl.layers.begin(), stl.layers.end());
67 for (auto i : stl.mapping)
69 if (i.second.name.empty())
71 return Err<struct layer_map>("Found mapping w/o name");
73 if (i.second.layer_id == -1)
75 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
79 auto msi = j.find("main_surface");
82 stl.main_surface_name = msi->value("surface_role", "");
83 stl.main_surface = -1;
88 catch (std::exception &e)
90 return Err<struct layer_map>(e.what());
95 layer_map::get_layer_id(int surface_id)
97 auto i = this->surfaces.find(surface_id);
98 if (i != this->surfaces.end())
100 return optional<int>(i->second);
105 optional<int> layer_map::get_layer_id(std::string const &role)
107 for (auto const &r : this->roles)
109 auto re = std::regex(r.first);
110 if (std::regex_match(role, re))
112 HMI_DEBUG("role %s matches layer %d", role.c_str(), r.second);
113 return optional<int>(r.second);
116 HMI_DEBUG("role %s does NOT match any layer", role.c_str());
120 json layer::to_json() const
122 auto is_full = this->rect == compositor::full_rect;
127 r = {{"type", "full"}};
131 r = {{"type", "rect"},
133 {{"x", this->rect.x},
135 {"width", this->rect.w},
136 {"height", this->rect.h}}}};
140 {"name", this->name},
141 {"role", this->role},
142 {"layer_id", this->layer_id},
147 json layer_map::to_json() const
150 for (auto const &i : this->mapping)
152 j.push_back(i.second.to_json());
157 void layer_map::setupArea(double scaling)
159 compositor::rect rct;
161 rct = this->area2size["normal.full"];
162 this->area2size["normalfull"] = rct;
163 this->area2size["normal"] = rct;
165 for (auto &i : this->area2size)
167 i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
168 i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
169 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
170 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
172 HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
173 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
177 compositor::rect layer_map::getAreaSize(const std::string &area)
179 return area2size[area];
182 int layer_map::loadAreaDb()
186 std::string file_name(get_file_path("areas.db"));
189 json_object *json_obj;
190 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
193 HMI_DEBUG("Could not open area.db, so use default area information");
194 json_obj = json_tokener_parse(kDefaultAreaDb);
196 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
199 HMI_DEBUG("Perse areas");
200 json_object *json_cfg;
201 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
203 HMI_ERROR("Parse Error!!");
207 int len = json_object_array_length(json_cfg);
208 HMI_DEBUG("json_cfg len:%d", len);
209 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
212 for (int i = 0; i < len; i++)
214 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
215 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
217 area = jh::getStringFromJson(json_tmp, "name");
220 HMI_ERROR("Parse Error!!");
223 HMI_DEBUG("> area:%s", area);
225 json_object *json_rect;
226 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
228 HMI_ERROR("Parse Error!!");
231 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
233 compositor::rect area_size;
234 area_size.x = jh::getIntFromJson(json_rect, "x");
235 area_size.y = jh::getIntFromJson(json_rect, "y");
236 area_size.w = jh::getIntFromJson(json_rect, "w");
237 area_size.h = jh::getIntFromJson(json_rect, "h");
239 this->area2size[area] = area_size;
243 for (auto itr = this->area2size.begin();
244 itr != this->area2size.end(); ++itr)
246 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
247 itr->first.c_str(), itr->second.x, itr->second.y,
248 itr->second.w, itr->second.h);
251 // Release json_object
252 json_object_put(json_obj);
257 const char* layer_map::kDefaultAreaDb = "{ \
260 \"name\": \"fullscreen\", \
269 \"name\": \"normal.full\", \
278 \"name\": \"split.main\", \
287 \"name\": \"split.sub\", \
296 \"name\": \"software_keyboard\", \
305 \"name\": \"restriction.normal\", \
314 \"name\": \"restriction.split.main\", \
323 \"name\": \"restriction.split.sub\", \
332 \"name\": \"on_screen\", \