OnScreen app can be displayed for pop_up role
[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 layout.db
34     ret = this->loadLayoutDb();
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* kDefaultLayoutDb;
48 int LayoutManager::loadLayoutDb() {
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/layout.db");
61     }
62
63     // Load layout.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 layout.db, so use default layout information");
68         json_obj = json_tokener_parse(kDefaultLayoutDb);
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* kDefaultLayoutDb = "{ \
127     \"layouts\": [ \
128         { \
129             \"name\": \"pu\", \
130             \"layer\": \"on_screen\", \
131             \"areas\": [ \
132                 { \
133                     \"name\": \"pop_up\", \
134                     \"role\": \"incomming_call\" \
135                 } \
136             ] \
137         }, \
138         { \
139             \"name\": \"sa\", \
140             \"layer\": \"on_screen\", \
141             \"areas\": [ \
142                 { \
143                     \"name\": \"system_alert\", \
144                     \"role\": \"system_alert\" \
145                 } \
146             ] \
147         }, \
148         { \
149             \"name\": \"m1\", \
150             \"layer\": \"apps\", \
151             \"areas\": [ \
152                 { \
153                     \"name\": \"normal\", \
154                     \"role\": \"map\" \
155                 } \
156             ] \
157         }, \
158         { \
159             \"name\": \"m2\", \
160             \"layer\": \"apps\", \
161             \"areas\": [ \
162                 { \
163                     \"name\": \"split.main\", \
164                     \"role\": \"map\" \
165                 }, \
166                 { \
167                     \"name\": \"split.sub\", \
168                     \"category\": \"hvac\" \
169                 } \
170             ] \
171         }, \
172         { \
173             \"name\": \"mf\", \
174             \"layer\": \"apps\", \
175             \"areas\": [ \
176                 { \
177                     \"name\": \"full\", \
178                     \"role\": \"map\" \
179                 } \
180             ] \
181         }, \
182         { \
183             \"name\": \"s1\", \
184             \"layer\": \"apps\", \
185             \"areas\": [ \
186                 { \
187                     \"name\": \"normal\", \
188                     \"category\": \"splitable\" \
189                 } \
190             ] \
191         }, \
192         { \
193             \"name\": \"s2\", \
194             \"layer\": \"apps\", \
195             \"areas\": [ \
196                 { \
197                     \"name\": \"split.main\", \
198                     \"category\": \"splitable\" \
199                 }, \
200                 { \
201                     \"name\": \"split.sub\", \
202                     \"category\": \"splitable\" \
203                 } \
204             ] \
205         }, \
206         { \
207             \"name\": \"g\", \
208             \"layer\": \"apps\", \
209             \"areas\": [ \
210                 { \
211                     \"name\": \"normal\", \
212                     \"category\": \"general\" \
213                 } \
214             ] \
215         }, \
216         { \
217             \"name\": \"hs\", \
218             \"layer\": \"homescreen\", \
219             \"areas\": [ \
220                 { \
221                     \"name\": \"full\", \
222                     \"role\": \"homescreen\" \
223                 } \
224             ] \
225         } \
226     ], \
227     \"areas\": [ \
228         { \
229             \"name\": \"normal\", \
230             \"rect\": { \
231                 \"x\": 0, \
232                 \"y\": 218, \
233                 \"w\": 1080, \
234                 \"h\": 1488 \
235             } \
236         }, \
237         { \
238             \"name\": \"split.main\", \
239             \"rect\": { \
240                 \"x\": 0, \
241                 \"y\": 218, \
242                 \"w\": 1080, \
243                 \"h\": 744 \
244             } \
245         }, \
246         { \
247             \"name\": \"split.sub\", \
248             \"rect\": { \
249                 \"x\": 0, \
250                 \"y\": 962, \
251                 \"w\": 1080, \
252                 \"h\": 744 \
253             } \
254         }, \
255         { \
256             \"name\": \"full\", \
257             \"rect\": { \
258                 \"x\": 0, \
259                 \"y\": 0, \
260                 \"w\": 1080, \
261                 \"h\": 1920 \
262             } \
263         }, \
264         { \
265             \"name\": \"pop_up\", \
266             \"rect\": { \
267                 \"x\": 0, \
268                 \"y\": 640, \
269                 \"w\": 1080, \
270                 \"h\": 640 \
271             } \
272         }, \
273         { \
274             \"name\": \"system_alert\", \
275             \"rect\": { \
276                 \"x\": 0, \
277                 \"y\": 640, \
278                 \"w\": 1080, \
279                 \"h\": 640 \
280             } \
281         } \
282     ] \
283 }";