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.
18 #include <json-c/json.h>
20 #include "json_helper.hpp"
21 #include "hmi-debug.h"
24 LayoutManager::LayoutManager() {
25 HMI_DEBUG("wm:lm", "Call");
28 int LayoutManager::initialize() {
29 HMI_DEBUG("wm:lm", "Call");
34 ret = this->loadLayoutDb();
36 HMI_ERROR("wm:lm", "Load layout.db Error!!");
44 rol_ctg["none"] = "none";
45 area["none"] = rol_ctg;
46 layout["none"] = area;
48 this->prv_layers_["on_screen"] = layout;
49 this->prv_layers_["apps"] = layout;
50 this->prv_layers_["homescreen"] = layout;
52 this->crr_layers_["on_screen"] = layout;
53 this->crr_layers_["apps"] = layout;
54 this->crr_layers_["homescreen"] = layout;
56 this->prv_layers_car_stop_["on_screen"] = layout;
57 this->prv_layers_car_stop_["apps"] = layout;
58 this->prv_layers_car_stop_["homescreen"] = layout;
63 bool LayoutManager::updateLayout(json_object* obj,
64 const char* new_role, const char* category) {
65 HMI_DEBUG("wm:lm", "Call");
69 // Check car state change
70 json_object* json_car;
71 if (!json_object_object_get_ex(obj, "car", &json_car)) {
72 HMI_ERROR("wm:lm", "Parse Error!!");
76 json_bool is_car_state_changed;
77 std::string car_state = "";
78 is_car_state_changed = jh::getBoolFromJson(json_car, "is_changed");
79 if (is_car_state_changed) {
80 // If car state is changed, get car state
81 car_state = jh::getStringFromJson(json_car, "state");
84 // Update layout of all layers
85 json_object* json_layers;
86 if (!json_object_object_get_ex(obj, "layers", &json_layers)) {
87 HMI_ERROR("wm:lm", "Parse Error!!");
91 int len = json_object_array_length(json_layers);
92 HMI_DEBUG("wm:lm", "json_layers len:%d", len);
93 HMI_DEBUG("wm:lm", "json_layers dump:%s", json_object_get_string(json_layers));
95 for (int i=0; i<len; i++) {
96 json_object* json_tmp = json_object_array_get_idx(json_layers, i);
98 // Get layer name and json_object
100 json_object* json_layer;
101 json_object_object_foreach(json_tmp, key, val) {
104 HMI_DEBUG("wm:lm", "Update %s layer state", layer);
107 // Store previous state
108 this->prv_layers_[layer] = this->crr_layers_[layer];
109 std::string prv_layout_name = this->prv_layers_[layer].begin()->first;
111 // If car state is changed car_stop -> car_run,
112 // store current state for state of car stop
113 if ((is_car_state_changed) && ("car_run" == car_state)) {
114 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
115 this->prv_layers_car_stop_[layer] = this->crr_layers_[layer];
118 json_object* json_is_changed;
119 if (!json_object_object_get_ex(json_layer, "is_changed", &json_is_changed)) {
120 HMI_ERROR("wm:lm", "Not found key \"is_changed\"");
124 // If layer state is changed
125 if (json_object_get_boolean(json_is_changed)) {
126 // Set layout changed flag
127 this->is_layout_changed_[layer] = true;
129 json_object* json_state;
130 if (!json_object_object_get_ex(json_layer, "state", &json_state)) {
131 HMI_ERROR("wm:lm", "Not found key \"state\"");
135 const char* crr_layout_name = json_object_get_string(json_state);
136 HMI_DEBUG("wm:lm", "crr state: %s", crr_layout_name);
138 TypeLayouts crr_layout;
139 if ((is_car_state_changed) && ("car_stop" == car_state)) {
140 // If car state is changed car_run -> car_stop,
141 // restore state of car stop
142 HMI_DEBUG("wm:lm", "Restore state of car stop");
143 crr_layout = this->prv_layers_car_stop_[layer];
145 else if ("none" == std::string(crr_layout_name)) {
146 // If current layout is "none",
147 // current areas is set with "none"
150 rol_ctg["none"] = "none";
151 area["none"] = rol_ctg;
152 crr_layout["none"] = area;
155 if (std::string(crr_layout_name) == prv_layout_name) {
156 // If previous layout is same with current,
157 // previous areas are copied to current
158 crr_layout[crr_layout_name] = this->prv_layers_[layer][crr_layout_name];
161 // If previous layout is NOT same with current,
162 // current areas is set with default value
163 crr_layout[crr_layout_name] = this->layout_define_[crr_layout_name];
166 if (is_car_state_changed) {
167 // Updating role is not necessary
168 // because new_role is not specified when car state is changed
171 // Get new_area for new role
172 std::string new_area = this->getAreaName(this->layout_define_[crr_layout_name],
175 // Update role in new area
177 crr_role["role"] = std::string(new_role);
178 crr_layout[crr_layout_name][new_area] = crr_role;
182 // Update layer state
183 this->crr_layers_[layer] = crr_layout;
186 for (auto itr_layout = this->crr_layers_[layer].begin();
187 itr_layout != this->crr_layers_[layer].end(); ++itr_layout) {
188 for (auto itr_area = itr_layout->second.begin();
189 itr_area != itr_layout->second.end(); ++itr_area) {
190 for (auto itr_role = itr_area->second.begin();
191 itr_role != itr_area->second.end(); ++itr_role) {
192 HMI_DEBUG("wm:lm", "layout:%s, area:%s, rol_ctg:%s, name:%s",
193 itr_layout->first.c_str(), itr_area->first.c_str(),
194 itr_role->first.c_str(), itr_role->second.c_str());
202 // Clear layout changed flag
203 this->is_layout_changed_[layer] = false;
209 // TODO: This API is for workaround, so this will be removed
210 void LayoutManager::updateArea(const char* layer, const char* role, const char* area) {
211 this->crr_layers_[layer].begin()->second[area]["role"] = std::string(role);
214 LayoutManager::TypeLayers LayoutManager::getCurrentLayers() {
215 return this->crr_layers_;
218 LayoutManager::TypeLayers LayoutManager::getPreviousLayers() {
219 return this->prv_layers_;
222 compositor::rect LayoutManager::getAreaSize(const char* area) {
223 return this->area2size_[area];
226 std::string LayoutManager::getAreaName(TypeAreas areas, const char* role, const char* category) {
227 for (auto itr_area = areas.begin(); itr_area != areas.end(); ++itr_area) {
228 std::string area_name = itr_area->first;
229 TypeRolCtg rol_ctg = itr_area->second;
231 if ("role" == rol_ctg.begin()->first) {
232 if (std::string(role) == rol_ctg.begin()->second) {
236 else if ("category" == rol_ctg.begin()->first) {
237 if (std::string(category) == rol_ctg.begin()->second) {
242 return std::string("none");
245 return std::string("none");
249 bool LayoutManager::isLayoutChanged(const char* layer) {
250 return this->is_layout_changed_[layer];
253 extern const char* kDefaultLayoutDb;
254 int LayoutManager::loadLayoutDb() {
255 HMI_DEBUG("wm:lm", "Call");
257 // Get afm application installed dir
258 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
259 HMI_DEBUG("wm:lm", "afm_app_install_dir:%s", afm_app_install_dir);
261 std::string file_name;
262 if (!afm_app_install_dir) {
263 HMI_ERROR("wm:lm", "AFM_APP_INSTALL_DIR is not defined");
266 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
270 HMI_DEBUG("wm:lm", "file_name:%s", file_name.c_str());
271 json_object* json_obj = json_object_from_file(file_name.c_str());
272 if (nullptr == json_obj) {
273 HMI_ERROR("wm:lm", "Could not open layout.db, so use default role information");
274 json_obj = json_tokener_parse(kDefaultLayoutDb);
276 HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
279 HMI_DEBUG("wm:lm", "Perse layouts");
280 json_object* json_cfg;
281 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
282 HMI_ERROR("wm:lm", "Parse Error!!");
286 int len = json_object_array_length(json_cfg);
287 HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
288 HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
292 const char* category;
293 for (int i=0; i<len; i++) {
294 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
296 layout = jh::getStringFromJson(json_tmp, "name");
297 if (nullptr == layout) {
298 HMI_ERROR("wm:lm", "Parse Error!!");
301 HMI_DEBUG("wm:lm", "> layout:%s", layout);
303 json_object* json_area_array;
304 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
305 HMI_ERROR("wm:lm", "Parse Error!!");
309 int len_area = json_object_array_length(json_area_array);
310 HMI_DEBUG("wm:lm", "json_area_array len:%d", len_area);
311 HMI_DEBUG("wm:lm", "json_area_array dump:%s", json_object_get_string(json_area_array));
314 for (int j=0; j<len_area; j++) {
315 json_object* json_area = json_object_array_get_idx(json_area_array, j);
317 const char* area = jh::getStringFromJson(json_area, "name");
318 if (nullptr == area) {
319 HMI_ERROR("wm:lm", "Parse Error!!");
322 HMI_DEBUG("wm:lm", ">> area:%s", area);
324 TypeRolCtg rol_ctg_name;
325 role = jh::getStringFromJson(json_area, "role");
326 if (nullptr == role) {
327 category = jh::getStringFromJson(json_area, "category");
328 if (nullptr == category) {
329 HMI_ERROR("wm:lm", "Parse Error!!");
332 rol_ctg_name["category"] = std::string(category);
333 HMI_DEBUG("wm:lm", ">>> category:%s", category);
336 rol_ctg_name["role"] = std::string(role);
337 HMI_DEBUG("wm:lm", ">>> role:%s", role);
340 areas[area] = rol_ctg_name;
343 this->layout_define_[layout] = areas;
347 for(auto itr_layout = this->layout_define_.begin();
348 itr_layout != this->layout_define_.end(); ++itr_layout) {
349 for (auto itr_area = itr_layout->second.begin();
350 itr_area != itr_layout->second.end(); ++itr_area) {
351 for (auto itr_role = itr_area->second.begin();
352 itr_role != itr_area->second.end(); ++itr_role) {
353 HMI_DEBUG("wm:lm", "layout:%s, area:%s, rol_ctg:%s, name:%s",
354 itr_layout->first.c_str(), itr_area->first.c_str(),
355 itr_role->first.c_str(), itr_role->second.c_str());
361 HMI_DEBUG("wm:lm", "Perse areas");
362 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg)) {
363 HMI_ERROR("wm:lm", "Parse Error!!");
367 len = json_object_array_length(json_cfg);
368 HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
369 HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
372 for (int i=0; i<len; i++) {
373 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
374 HMI_DEBUG("wm:lm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
376 area = jh::getStringFromJson(json_tmp, "name");
377 if (nullptr == area) {
378 HMI_ERROR("wm:lm", "Parse Error!!");
381 HMI_DEBUG("wm:lm", "> area:%s", area);
383 json_object* json_rect;
384 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect)) {
385 HMI_ERROR("wm:lm", "Parse Error!!");
388 HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
390 compositor::rect area_size;
391 area_size.x = jh::getIntFromJson(json_rect, "x");
392 area_size.y = jh::getIntFromJson(json_rect, "y");
393 area_size.w = jh::getIntFromJson(json_rect, "w");
394 area_size.h = jh::getIntFromJson(json_rect, "h");
396 this->area2size_[area] = area_size;
400 for(auto itr = this->area2size_.begin();
401 itr != this->area2size_.end(); ++itr) {
402 HMI_DEBUG("wm:lm", "area:%s x:%d y:%d w:%d h:%d",
403 itr->first.c_str(), itr->second.x, itr->second.y,
404 itr->second.w, itr->second.h);
407 // Release json_object
408 json_object_put(json_obj);
413 const char* kDefaultLayoutDb = "{ \
417 \"layer\": \"on_screen\", \
420 \"name\": \"pop_up\", \
421 \"role\": \"incomming_call\" \
427 \"layer\": \"on_screen\", \
430 \"name\": \"system_alert\", \
431 \"role\": \"system_alert\" \
437 \"layer\": \"apps\", \
440 \"name\": \"normal\", \
447 \"layer\": \"apps\", \
450 \"name\": \"split.main\", \
454 \"name\": \"split.sub\", \
455 \"category\": \"hvac\" \
461 \"layer\": \"apps\", \
464 \"name\": \"full\", \
471 \"layer\": \"apps\", \
474 \"name\": \"normal\", \
475 \"category\": \"splitable\" \
481 \"layer\": \"apps\", \
484 \"name\": \"split.main\", \
485 \"category\": \"splitable\" \
488 \"name\": \"split.sub\", \
489 \"category\": \"splitable\" \
495 \"layer\": \"apps\", \
498 \"name\": \"normal\", \
499 \"category\": \"general\" \
505 \"layer\": \"homescreen\", \
508 \"name\": \"full\", \
509 \"role\": \"homescreen\" \
516 \"name\": \"normal\", \
525 \"name\": \"split.main\", \
534 \"name\": \"split.sub\", \
543 \"name\": \"full\", \
552 \"name\": \"pop_up\", \
561 \"name\": \"system_alert\", \