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;
51 this->prv_layers_["restriction"] = layout;
53 this->crr_layers_["on_screen"] = layout;
54 this->crr_layers_["apps"] = layout;
55 this->crr_layers_["homescreen"] = layout;
56 this->crr_layers_["restriction"] = layout;
58 this->prv_layers_car_stop_["on_screen"] = layout;
59 this->prv_layers_car_stop_["apps"] = layout;
60 this->prv_layers_car_stop_["homescreen"] = layout;
61 this->prv_layers_car_stop_["restriction"] = layout;
66 bool LayoutManager::updateLayout(json_object* obj,
67 const char* new_role, const char* category) {
68 HMI_DEBUG("wm:lm", "Call");
72 // Check car state change
73 json_object* json_car;
74 if (!json_object_object_get_ex(obj, "car", &json_car)) {
75 HMI_ERROR("wm:lm", "Parse Error!!");
79 json_bool is_car_state_changed;
80 std::string car_state = "";
81 is_car_state_changed = jh::getBoolFromJson(json_car, "is_changed");
82 if (is_car_state_changed) {
83 // If car state is changed, get car state
84 car_state = jh::getStringFromJson(json_car, "state");
87 // Update layout of all layers
88 json_object* json_layers;
89 if (!json_object_object_get_ex(obj, "layers", &json_layers)) {
90 HMI_ERROR("wm:lm", "Parse Error!!");
94 int len = json_object_array_length(json_layers);
95 HMI_DEBUG("wm:lm", "json_layers len:%d", len);
96 HMI_DEBUG("wm:lm", "json_layers dump:%s", json_object_get_string(json_layers));
98 for (int i=0; i<len; i++) {
99 json_object* json_tmp = json_object_array_get_idx(json_layers, i);
101 // Get layer name and json_object
103 json_object* json_layer;
104 json_object_object_foreach(json_tmp, key, val) {
107 HMI_DEBUG("wm:lm", "Try to update %s layer state", layer);
110 // Store previous state
111 this->prv_layers_[layer] = this->crr_layers_[layer];
112 std::string prv_layout_name = this->prv_layers_[layer].begin()->first;
114 // If car state is changed car_stop -> car_run,
115 // store current state for state of car stop
116 if ((is_car_state_changed) && ("car_run" == car_state)) {
117 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
118 this->prv_layers_car_stop_[layer] = this->crr_layers_[layer];
121 json_object* json_is_changed;
122 if (!json_object_object_get_ex(json_layer, "is_changed", &json_is_changed)) {
123 HMI_ERROR("wm:lm", "Not found key \"is_changed\"");
127 // If layer state is changed
128 if (json_object_get_boolean(json_is_changed)) {
129 // Set layout changed flag
130 this->is_layout_changed_[layer] = true;
132 json_object* json_state;
133 if (!json_object_object_get_ex(json_layer, "state", &json_state)) {
134 HMI_ERROR("wm:lm", "Not found key \"state\"");
138 const char* crr_layout_name = json_object_get_string(json_state);
139 HMI_DEBUG("wm:lm", "crr state: %s", crr_layout_name);
141 TypeLayouts crr_layout;
142 if ((is_car_state_changed) && ("car_stop" == car_state)) {
143 // If car state is changed car_run -> car_stop,
144 // restore state of car stop
145 HMI_DEBUG("wm:lm", "Restore state of car stop");
146 crr_layout = this->prv_layers_car_stop_[layer];
148 else if ("none" == std::string(crr_layout_name)) {
149 // If current layout is "none",
150 // current areas is set with "none"
153 rol_ctg["none"] = "none";
154 area["none"] = rol_ctg;
155 crr_layout["none"] = area;
158 if (std::string(crr_layout_name) == prv_layout_name) {
159 // If previous layout is same with current,
160 // previous areas are copied to current
161 crr_layout[crr_layout_name] = this->prv_layers_[layer][crr_layout_name];
164 // If previous layout is NOT same with current,
165 // current areas is set with default value
166 crr_layout[crr_layout_name] = this->layout_define_[crr_layout_name];
169 if (is_car_state_changed) {
170 // Updating role is not necessary
171 // because new_role is not specified when car state is changed
174 // Get new_area for new role
175 std::string new_area = this->getAreaName(this->layout_define_[crr_layout_name],
178 // Update role in new area
180 crr_role["role"] = std::string(new_role);
181 crr_layout[crr_layout_name][new_area] = crr_role;
185 // Update layer state
186 this->crr_layers_[layer] = crr_layout;
189 for (auto itr_layout = this->crr_layers_[layer].begin();
190 itr_layout != this->crr_layers_[layer].end(); ++itr_layout) {
191 for (auto itr_area = itr_layout->second.begin();
192 itr_area != itr_layout->second.end(); ++itr_area) {
193 for (auto itr_role = itr_area->second.begin();
194 itr_role != itr_area->second.end(); ++itr_role) {
195 HMI_DEBUG("wm:lm", "layout:%s, area:%s, rol_ctg:%s, name:%s",
196 itr_layout->first.c_str(), itr_area->first.c_str(),
197 itr_role->first.c_str(), itr_role->second.c_str());
205 // Clear layout changed flag
206 this->is_layout_changed_[layer] = false;
212 // TODO: This API is for workaround, so this will be removed
213 void LayoutManager::updateArea(const char* layer, const char* role, const char* area) {
214 this->crr_layers_[layer].begin()->second[area]["role"] = std::string(role);
217 LayoutManager::TypeLayers LayoutManager::getCurrentLayers() {
218 return this->crr_layers_;
221 LayoutManager::TypeLayers LayoutManager::getPreviousLayers() {
222 return this->prv_layers_;
225 compositor::rect LayoutManager::getAreaSize(const char* area) {
226 return this->area2size_[area];
229 std::string LayoutManager::getAreaName(TypeAreas areas, const char* role, const char* category) {
230 for (auto itr_area = areas.begin(); itr_area != areas.end(); ++itr_area) {
231 std::string area_name = itr_area->first;
232 TypeRolCtg rol_ctg = itr_area->second;
234 if ("role" == rol_ctg.begin()->first) {
235 if (std::string(role) == rol_ctg.begin()->second) {
239 else if ("category" == rol_ctg.begin()->first) {
240 if (std::string(category) == rol_ctg.begin()->second) {
245 return std::string("none");
248 return std::string("none");
252 bool LayoutManager::isLayoutChanged(const char* layer) {
253 return this->is_layout_changed_[layer];
257 extern const char* kDefaultLayoutDb;
258 int LayoutManager::loadLayoutDb() {
259 HMI_DEBUG("wm:lm", "Call");
261 // Get afm application installed dir
262 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
263 HMI_DEBUG("wm:lm", "afm_app_install_dir:%s", afm_app_install_dir);
265 std::string file_name;
266 if (!afm_app_install_dir) {
267 HMI_ERROR("wm:lm", "AFM_APP_INSTALL_DIR is not defined");
270 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
274 json_object* json_obj;
275 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
277 HMI_DEBUG("wm:lm", "Could not open layout.db, so use default layout information");
278 json_obj = json_tokener_parse(kDefaultLayoutDb);
280 HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
283 HMI_DEBUG("wm:lm", "Perse layouts");
284 json_object* json_cfg;
285 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
286 HMI_ERROR("wm:lm", "Parse Error!!");
290 int len = json_object_array_length(json_cfg);
291 HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
292 HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
296 const char* category;
297 for (int i=0; i<len; i++) {
298 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
300 layout = jh::getStringFromJson(json_tmp, "name");
301 if (nullptr == layout) {
302 HMI_ERROR("wm:lm", "Parse Error!!");
305 HMI_DEBUG("wm:lm", "> layout:%s", layout);
307 json_object* json_area_array;
308 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
309 HMI_ERROR("wm:lm", "Parse Error!!");
313 int len_area = json_object_array_length(json_area_array);
314 HMI_DEBUG("wm:lm", "json_area_array len:%d", len_area);
315 HMI_DEBUG("wm:lm", "json_area_array dump:%s", json_object_get_string(json_area_array));
318 for (int j=0; j<len_area; j++) {
319 json_object* json_area = json_object_array_get_idx(json_area_array, j);
321 const char* area = jh::getStringFromJson(json_area, "name");
322 if (nullptr == area) {
323 HMI_ERROR("wm:lm", "Parse Error!!");
326 HMI_DEBUG("wm:lm", ">> area:%s", area);
328 TypeRolCtg rol_ctg_name;
329 role = jh::getStringFromJson(json_area, "role");
330 if (nullptr == role) {
331 category = jh::getStringFromJson(json_area, "category");
332 if (nullptr == category) {
333 HMI_ERROR("wm:lm", "Parse Error!!");
336 rol_ctg_name["category"] = std::string(category);
337 HMI_DEBUG("wm:lm", ">>> category:%s", category);
340 rol_ctg_name["role"] = std::string(role);
341 HMI_DEBUG("wm:lm", ">>> role:%s", role);
344 areas[area] = rol_ctg_name;
347 this->layout_define_[layout] = areas;
351 for(auto itr_layout = this->layout_define_.begin();
352 itr_layout != this->layout_define_.end(); ++itr_layout) {
353 for (auto itr_area = itr_layout->second.begin();
354 itr_area != itr_layout->second.end(); ++itr_area) {
355 for (auto itr_role = itr_area->second.begin();
356 itr_role != itr_area->second.end(); ++itr_role) {
357 HMI_DEBUG("wm:lm", "layout:%s, area:%s, rol_ctg:%s, name:%s",
358 itr_layout->first.c_str(), itr_area->first.c_str(),
359 itr_role->first.c_str(), itr_role->second.c_str());
365 HMI_DEBUG("wm:lm", "Perse areas");
366 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg)) {
367 HMI_ERROR("wm:lm", "Parse Error!!");
371 len = json_object_array_length(json_cfg);
372 HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
373 HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
376 for (int i=0; i<len; i++) {
377 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
378 HMI_DEBUG("wm:lm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
380 area = jh::getStringFromJson(json_tmp, "name");
381 if (nullptr == area) {
382 HMI_ERROR("wm:lm", "Parse Error!!");
385 HMI_DEBUG("wm:lm", "> area:%s", area);
387 json_object* json_rect;
388 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect)) {
389 HMI_ERROR("wm:lm", "Parse Error!!");
392 HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
394 compositor::rect area_size;
395 area_size.x = jh::getIntFromJson(json_rect, "x");
396 area_size.y = jh::getIntFromJson(json_rect, "y");
397 area_size.w = jh::getIntFromJson(json_rect, "w");
398 area_size.h = jh::getIntFromJson(json_rect, "h");
400 this->area2size_[area] = area_size;
404 for(auto itr = this->area2size_.begin();
405 itr != this->area2size_.end(); ++itr) {
406 HMI_DEBUG("wm:lm", "area:%s x:%d y:%d w:%d h:%d",
407 itr->first.c_str(), itr->second.x, itr->second.y,
408 itr->second.w, itr->second.h);
411 // Release json_object
412 json_object_put(json_obj);
417 const char* kDefaultLayoutDb = "{ \
421 \"layer\": \"on_screen\", \
424 \"name\": \"pop_up\", \
425 \"role\": \"incomming_call\" \
431 \"layer\": \"on_screen\", \
434 \"name\": \"system_alert\", \
435 \"role\": \"system_alert\" \
441 \"layer\": \"apps\", \
444 \"name\": \"normal\", \
451 \"layer\": \"apps\", \
454 \"name\": \"split.main\", \
458 \"name\": \"split.sub\", \
459 \"category\": \"hvac\" \
465 \"layer\": \"apps\", \
468 \"name\": \"full\", \
475 \"layer\": \"apps\", \
478 \"name\": \"normal\", \
479 \"category\": \"splitable\" \
485 \"layer\": \"apps\", \
488 \"name\": \"split.main\", \
489 \"category\": \"splitable\" \
492 \"name\": \"split.sub\", \
493 \"category\": \"splitable\" \
499 \"layer\": \"apps\", \
502 \"name\": \"normal\", \
503 \"category\": \"general\" \
509 \"layer\": \"homescreen\", \
512 \"name\": \"full\", \
513 \"role\": \"homescreen\" \
520 \"name\": \"normal\", \
529 \"name\": \"split.main\", \
538 \"name\": \"split.sub\", \
547 \"name\": \"full\", \
556 \"name\": \"pop_up\", \
565 \"name\": \"system_alert\", \