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 // Init flag of normal layout only
35 this->is_normal_layout_only = true;
37 auto split_layouts = j.find("split_layouts");
38 if (split_layouts != j.end())
41 // Clear flag of normal layout only
42 this->is_normal_layout_only = false;
44 auto &sls = j["split_layouts"];
45 // this->layouts.reserve(sls.size());
46 std::transform(std::cbegin(sls), std::cend(sls),
47 std::back_inserter(this->layouts), [this](json const &sl) {
50 sl["name"], sl["main_match"], sl["sub_match"]
53 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
56 l.name.c_str(), l.main_match.c_str(),
61 HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
62 this->layer_id, this->is_normal_layout_only);
65 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
70 auto m = j["mappings"];
72 std::transform(std::cbegin(m), std::cend(m),
73 std::inserter(stl.mapping, stl.mapping.end()),
74 [](nlohmann::json const &j) {
75 return std::pair<int, struct layer>(
76 j.value("layer_id", -1), layer(j));
79 // TODO: add sanity checks here?
80 // * check for double IDs
81 // * check for double names/roles
83 stl.layers.reserve(m.size());
84 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
85 std::back_inserter(stl.layers),
86 [&stl](std::pair<int, struct layer> const &k) {
87 stl.roles.emplace_back(
88 std::make_pair(k.second.role, k.second.layer_id));
89 return unsigned(k.second.layer_id);
92 std::sort(stl.layers.begin(), stl.layers.end());
94 for (auto i : stl.mapping)
96 if (i.second.name.empty())
98 return Err<struct layer_map>("Found mapping w/o name");
100 if (i.second.layer_id == -1)
102 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
106 auto msi = j.find("main_surface");
109 stl.main_surface_name = msi->value("surface_role", "");
110 stl.main_surface = -1;
115 catch (std::exception &e)
117 return Err<struct layer_map>(e.what());
122 layer_map::get_layer_id(int surface_id)
124 auto i = this->surfaces.find(surface_id);
125 if (i != this->surfaces.end())
127 return optional<int>(i->second);
132 optional<int> layer_map::get_layer_id(std::string const &role)
134 for (auto const &r : this->roles)
136 auto re = std::regex(r.first);
137 if (std::regex_match(role, re))
139 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
140 return optional<int>(r.second);
143 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
147 json layer::to_json() const
149 auto is_full = this->rect == compositor::full_rect;
154 r = {{"type", "full"}};
158 r = {{"type", "rect"},
160 {{"x", this->rect.x},
162 {"width", this->rect.w},
163 {"height", this->rect.h}}}};
167 {"name", this->name},
168 {"role", this->role},
169 {"layer_id", this->layer_id},
174 json layer_map::to_json() const
177 for (auto const &i : this->mapping)
179 j.push_back(i.second.to_json());
184 void layer_map::setupArea(int output_w, int output_h)
186 compositor::rect rct;
187 for (auto &i : this->area2size)
191 // less-than-0 values refer to MAX + 1 - $VALUE
192 // e.g. MAX is either screen width or height
194 rct.w = output_w + 1 + rct.w;
196 rct.h = output_h + 1 + rct.h;
201 rct = this->area2size["normal.full"];
202 this->area2size["normalfull"] = rct;
203 this->area2size["normal"] = rct;
205 for (auto &i : this->area2size)
207 HMI_DEBUG("wm:lm", "area:%s size(after) : x:%d y:%d w:%d h:%d",
208 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
212 compositor::rect layer_map::getAreaSize(const std::string &area)
214 return area2size[area];
217 int layer_map::loadAreaDb()
219 HMI_DEBUG("wm:lm", "Call");
221 // Get afm application installed dir
222 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
223 HMI_DEBUG("wm:lm", "afm_app_install_dir:%s", afm_app_install_dir);
225 std::string file_name;
226 if (!afm_app_install_dir)
228 HMI_ERROR("wm:lm", "AFM_APP_INSTALL_DIR is not defined");
232 file_name = std::string(afm_app_install_dir) + std::string("/etc/areas.db");
236 json_object *json_obj;
237 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
240 HMI_DEBUG("wm:lm", "Could not open area.db, so use default area information");
241 json_obj = json_tokener_parse(kDefaultAreaDb);
243 HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
246 HMI_DEBUG("wm:lm", "Perse areas");
247 json_object *json_cfg;
248 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
250 HMI_ERROR("wm:lm", "Parse Error!!");
254 int len = json_object_array_length(json_cfg);
255 HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
256 HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
259 for (int i = 0; i < len; i++)
261 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
262 HMI_DEBUG("wm:lm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
264 area = jh::getStringFromJson(json_tmp, "name");
267 HMI_ERROR("wm:lm", "Parse Error!!");
270 HMI_DEBUG("wm:lm", "> area:%s", area);
272 json_object *json_rect;
273 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
275 HMI_ERROR("wm:lm", "Parse Error!!");
278 HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
280 compositor::rect area_size;
281 area_size.x = jh::getIntFromJson(json_rect, "x");
282 area_size.y = jh::getIntFromJson(json_rect, "y");
283 area_size.w = jh::getIntFromJson(json_rect, "w");
284 area_size.h = jh::getIntFromJson(json_rect, "h");
286 this->area2size[area] = area_size;
290 for (auto itr = this->area2size.begin();
291 itr != this->area2size.end(); ++itr)
293 HMI_DEBUG("wm:lm", "area:%s x:%d y:%d w:%d h:%d",
294 itr->first.c_str(), itr->second.x, itr->second.y,
295 itr->second.w, itr->second.h);
298 // Release json_object
299 json_object_put(json_obj);
304 const char* layer_map::kDefaultAreaDb = "{ \
307 \"name\": \"fullscreen\", \
316 \"name\": \"normal.full\", \
325 \"name\": \"split.main\", \
334 \"name\": \"split.sub\", \
343 \"name\": \"software_keyboard\", \
352 \"name\": \"restriction.normal\", \
361 \"name\": \"restriction.split.main\", \
370 \"name\": \"restriction.split.sub\", \
379 \"name\": \"on_screen\", \