Separate area.db from layout.db
[apps/agl-service-windowmanager.git] / src / layout_manager / layout.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <json-c/json.h>
19 #include "layout.hpp"
20 #include "json_helper.hpp"
21 #include "hmi-debug.h"
22
23
24 LayoutManager::LayoutManager() {
25     HMI_DEBUG("wm:lm", "Call");
26 }
27
28 int LayoutManager::initialize() {
29     HMI_DEBUG("wm:lm", "Call");
30
31     int ret = 0;
32
33     // Load area.db
34     ret = this->loadAreaDb();
35     if (0 > ret) {
36         HMI_ERROR("wm:lm", "Load layout.db Error!!");
37         return ret;
38     }
39
40     return ret;
41 }
42
43 compositor::rect LayoutManager::getAreaSize(const char* area) {
44     return this->area2size[area];
45 }
46
47 extern const char* kDefaultAreaDb;
48 int LayoutManager::loadAreaDb() {
49     HMI_DEBUG("wm:lm", "Call");
50
51     // Get afm application installed dir
52     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
53     HMI_DEBUG("wm:lm", "afm_app_install_dir:%s", afm_app_install_dir);
54
55     std::string file_name;
56     if (!afm_app_install_dir) {
57         HMI_ERROR("wm:lm", "AFM_APP_INSTALL_DIR is not defined");
58     }
59     else {
60         file_name = std::string(afm_app_install_dir) + std::string("/etc/area.db");
61     }
62
63     // Load area.db
64     json_object* json_obj;
65     int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
66     if (0 > ret) {
67         HMI_DEBUG("wm:lm", "Could not open area.db, so use default area information");
68         json_obj = json_tokener_parse(kDefaultAreaDb);
69     }
70     HMI_DEBUG("wm:lm", "json_obj dump:%s", json_object_get_string(json_obj));
71
72     // Perse areas
73     HMI_DEBUG("wm:lm", "Perse areas");
74     json_object* json_cfg;
75     if (!json_object_object_get_ex(json_obj, "areas", &json_cfg)) {
76         HMI_ERROR("wm:lm", "Parse Error!!");
77         return -1;
78     }
79
80     int len = json_object_array_length(json_cfg);
81     HMI_DEBUG("wm:lm", "json_cfg len:%d", len);
82     HMI_DEBUG("wm:lm", "json_cfg dump:%s", json_object_get_string(json_cfg));
83
84     const char* area;
85     for (int i=0; i<len; i++) {
86         json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
87         HMI_DEBUG("wm:lm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
88
89         area = jh::getStringFromJson(json_tmp, "name");
90         if (nullptr == area) {
91             HMI_ERROR("wm:lm", "Parse Error!!");
92             return -1;
93         }
94         HMI_DEBUG("wm:lm", "> area:%s", area);
95
96         json_object* json_rect;
97         if (!json_object_object_get_ex(json_tmp, "rect", &json_rect)) {
98           HMI_ERROR("wm:lm", "Parse Error!!");
99           return -1;
100         }
101         HMI_DEBUG("wm:lm", "> json_rect dump:%s", json_object_get_string(json_rect));
102
103         compositor::rect area_size;
104         area_size.x = jh::getIntFromJson(json_rect, "x");
105         area_size.y = jh::getIntFromJson(json_rect, "y");
106         area_size.w = jh::getIntFromJson(json_rect, "w");
107         area_size.h = jh::getIntFromJson(json_rect, "h");
108
109         this->area2size[area] = area_size;
110     }
111
112     // Check
113     for(auto itr = this->area2size.begin();
114       itr != this->area2size.end(); ++itr) {
115         HMI_DEBUG("wm:lm", "area:%s x:%d y:%d w:%d h:%d",
116                   itr->first.c_str(), itr->second.x, itr->second.y,
117                   itr->second.w, itr->second.h);
118     }
119
120     // Release json_object
121     json_object_put(json_obj);
122
123     return 0;
124 }
125
126 const char* kDefaultAreaDb = "{ \
127     \"areas\": [ \
128         { \
129             \"name\": \"fullscreen\", \
130             \"rect\": { \
131                 \"x\": 0, \
132                 \"y\": 0, \
133                 \"w\": 1080, \
134                 \"h\": 1920 \
135             } \
136         }, \
137         { \
138             \"name\": \"normal\", \
139             \"rect\": { \
140                 \"x\": 0, \
141                 \"y\": 218, \
142                 \"w\": 1080, \
143                 \"h\": 1488 \
144             } \
145         }, \
146         { \
147             \"name\": \"split.main\", \
148             \"rect\": { \
149                 \"x\": 0, \
150                 \"y\": 218, \
151                 \"w\": 1080, \
152                 \"h\": 744 \
153             } \
154         }, \
155         { \
156             \"name\": \"split.sub\", \
157             \"rect\": { \
158                 \"x\": 0, \
159                 \"y\": 962, \
160                 \"w\": 1080, \
161                 \"h\": 744 \
162             } \
163         }, \
164         { \
165             \"name\": \"software_keyboard\", \
166             \"rect\": { \
167                 \"x\": 0, \
168                 \"y\": 962, \
169                 \"w\": 1080, \
170                 \"h\": 744 \
171             } \
172         }, \
173         { \
174             \"name\": \"restriction.normal\", \
175             \"rect\": { \
176                 \"x\": 0, \
177                 \"y\": 218, \
178                 \"w\": 1080, \
179                 \"h\": 1488 \
180             } \
181         }, \
182         { \
183             \"name\": \"restriction.split.main\", \
184             \"rect\": { \
185                 \"x\": 0, \
186                 \"y\": 218, \
187                 \"w\": 1080, \
188                 \"h\": 744 \
189             } \
190         }, \
191         { \
192             \"name\": \"restriction.split.sub\", \
193             \"rect\": { \
194                 \"x\": 0, \
195                 \"y\": 962, \
196                 \"w\": 1080, \
197                 \"h\": 744 \
198             } \
199         }, \
200         { \
201             \"name\": \"pop_up\", \
202             \"rect\": { \
203                 \"x\": 0, \
204                 \"y\": 640, \
205                 \"w\": 1080, \
206                 \"h\": 640 \
207             } \
208         }, \
209         { \
210             \"name\": \"system_alert\", \
211             \"rect\": { \
212                 \"x\": 0, \
213                 \"y\": 640, \
214                 \"w\": 1080, \
215                 \"h\": 640 \
216             } \
217         } \
218     ] \
219 }";