2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "json_helper.hpp"
26 using json = nlohmann::json;
28 layer::layer(nlohmann::json const &j)
30 this->role = j["role"];
31 this->name = j["name"];
32 this->layer_id = j["layer_id"];
34 HMI_DEBUG("layer_id:%d name:%s", this->layer_id, this->name.c_str());
37 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
42 auto m = j["mappings"];
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));
51 // TODO: add sanity checks here?
52 // * check for double IDs
53 // * check for double names/roles
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);
64 std::sort(stl.layers.begin(), stl.layers.end());
66 for (auto i : stl.mapping)
68 if (i.second.name.empty())
70 return Err<struct layer_map>("Found mapping w/o name");
72 if (i.second.layer_id == -1)
74 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
78 auto msi = j.find("main_surface");
81 stl.main_surface_name = msi->value("surface_role", "");
82 stl.main_surface = -1;
87 catch (std::exception &e)
89 return Err<struct layer_map>(e.what());
94 layer_map::get_layer_id(int surface_id)
96 auto i = this->surfaces.find(surface_id);
97 if (i != this->surfaces.end())
99 return optional<int>(i->second);
104 optional<int> layer_map::get_layer_id(std::string const &role)
106 for (auto const &r : this->roles)
108 auto re = std::regex(r.first);
109 if (std::regex_match(role, re))
111 HMI_DEBUG("role %s matches layer %d", role.c_str(), r.second);
112 return optional<int>(r.second);
115 HMI_DEBUG("role %s does NOT match any layer", role.c_str());
119 json layer::to_json() const
121 auto is_full = this->rect == compositor::full_rect;
126 r = {{"type", "full"}};
130 r = {{"type", "rect"},
132 {{"x", this->rect.x},
134 {"width", this->rect.w},
135 {"height", this->rect.h}}}};
139 {"name", this->name},
140 {"role", this->role},
141 {"layer_id", this->layer_id},
146 json layer_map::to_json() const
149 for (auto const &i : this->mapping)
151 j.push_back(i.second.to_json());
156 void layer_map::setupArea(double scaling)
158 compositor::rect rct;
160 rct = this->area2size["normal.full"];
161 this->area2size["normalfull"] = rct;
162 this->area2size["normal"] = rct;
164 for (auto &i : this->area2size)
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);
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);
176 compositor::rect layer_map::getAreaSize(const std::string &area)
178 return area2size[area];
181 int layer_map::loadAreaDb()
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);
187 std::string file_name;
188 if (!afm_app_install_dir)
190 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
194 file_name = std::string(afm_app_install_dir) + std::string("/etc/areas.db");
198 json_object *json_obj;
199 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
202 HMI_DEBUG("Could not open area.db, so use default area information");
203 json_obj = json_tokener_parse(kDefaultAreaDb);
205 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
208 HMI_DEBUG("Perse areas");
209 json_object *json_cfg;
210 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
212 HMI_ERROR("Parse Error!!");
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));
221 for (int i = 0; i < len; i++)
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));
226 area = jh::getStringFromJson(json_tmp, "name");
229 HMI_ERROR("Parse Error!!");
232 HMI_DEBUG("> area:%s", area);
234 json_object *json_rect;
235 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
237 HMI_ERROR("Parse Error!!");
240 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
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");
248 this->area2size[area] = area_size;
252 for (auto itr = this->area2size.begin();
253 itr != this->area2size.end(); ++itr)
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);
260 // Release json_object
261 json_object_put(json_obj);
266 const char* layer_map::kDefaultAreaDb = "{ \
269 \"name\": \"fullscreen\", \
278 \"name\": \"normal.full\", \
287 \"name\": \"split.main\", \
296 \"name\": \"split.sub\", \
305 \"name\": \"software_keyboard\", \
314 \"name\": \"restriction.normal\", \
323 \"name\": \"restriction.split.main\", \
332 \"name\": \"restriction.split.sub\", \
341 \"name\": \"on_screen\", \