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"
21 #include "hmi-debug.h"
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("wm", "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("wm", "role %s matches layer %d", role.c_str(), r.second);
112 return optional<int>(r.second);
115 HMI_DEBUG("wm", "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("wm:lm", "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 HMI_DEBUG("wm:lm", "Call");
185 // Get afm application installed dir
186 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
187 HMI_DEBUG("wm:lm", "afm_app_install_dir:%s", afm_app_install_dir);
189 std::string file_name;
190 if (!afm_app_install_dir)
192 HMI_ERROR("wm:lm", "AFM_APP_INSTALL_DIR is not defined");
196 file_name = std::string(afm_app_install_dir) + std::string("/etc/areas.db");
200 json_object *json_obj;
201 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
204 HMI_DEBUG("wm:lm", "Could not open area.db, so use default area information");
205 json_obj = json_tokener_parse(kDefaultAreaDb);
207 HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
210 HMI_DEBUG("wm:lm", "Perse areas");
211 json_object *json_cfg;
212 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
214 HMI_ERROR("wm:lm", "Parse Error!!");
218 int len = json_object_array_length(json_cfg);
219 HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
220 HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
223 for (int i = 0; i < len; i++)
225 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
226 HMI_DEBUG("wm:lm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
228 area = jh::getStringFromJson(json_tmp, "name");
231 HMI_ERROR("wm:lm", "Parse Error!!");
234 HMI_DEBUG("wm:lm", "> area:%s", area);
236 json_object *json_rect;
237 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
239 HMI_ERROR("wm:lm", "Parse Error!!");
242 HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
244 compositor::rect area_size;
245 area_size.x = jh::getIntFromJson(json_rect, "x");
246 area_size.y = jh::getIntFromJson(json_rect, "y");
247 area_size.w = jh::getIntFromJson(json_rect, "w");
248 area_size.h = jh::getIntFromJson(json_rect, "h");
250 this->area2size[area] = area_size;
254 for (auto itr = this->area2size.begin();
255 itr != this->area2size.end(); ++itr)
257 HMI_DEBUG("wm:lm", "area:%s x:%d y:%d w:%d h:%d",
258 itr->first.c_str(), itr->second.x, itr->second.y,
259 itr->second.w, itr->second.h);
262 // Release json_object
263 json_object_put(json_obj);
268 const char* layer_map::kDefaultAreaDb = "{ \
271 \"name\": \"fullscreen\", \
280 \"name\": \"normal.full\", \
289 \"name\": \"split.main\", \
298 \"name\": \"split.sub\", \
307 \"name\": \"software_keyboard\", \
316 \"name\": \"restriction.normal\", \
325 \"name\": \"restriction.split.main\", \
334 \"name\": \"restriction.split.sub\", \
343 \"name\": \"on_screen\", \