Add none event and category
[apps/agl-service-windowmanager.git] / src / policy_manager / policy_manager.cpp
1 /*
2  * Copyright (c) 2018 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 <fstream>
19 #include <sstream>
20 #include <istream>
21 #include <thread>
22 #include <map>
23 #include <systemd/sd-event.h>
24 #include <json-c/json.h>
25 #include "policy_manager.hpp"
26 #include "hmi-debug.h"
27
28 namespace stm {
29 extern "C" {
30 #include "dummy_stm.h"
31 }
32 } // namespace stm
33
34
35 namespace pm {
36 typedef struct AreaState {
37     std::string name;
38     std::string category;
39     std::string role;
40 } AreaState;
41
42 typedef std::vector<AreaState> AreaList;
43 typedef struct LayoutState {
44     std::string name;
45     std::map<std::string, int> category_num;
46     AreaList area_list;
47     std::map<std::string, std::vector<std::string>> role_history;
48 } LayoutState;
49
50 typedef struct LayerState {
51     std::string name;
52     LayoutState layout_state;
53     bool changed;
54 } LayerState;
55
56 typedef struct CarElement {
57     std::string state;
58     bool changed;
59 } CarElement;
60
61 struct sd_event* event_loop;
62 std::map<int, struct sd_event_source*> event_source_list;
63 std::map<int, std::string> g_req_role_list;
64 PolicyManager::CallbackTable callback;
65
66 std::unordered_map<std::string, CarElement> g_prv_car_elements;
67 std::unordered_map<std::string, CarElement> g_crr_car_elements;
68
69 std::unordered_map<std::string, LayerState> g_prv_layers;
70 std::unordered_map<std::string, LayerState> g_crr_layers;
71
72 std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
73
74 std::unordered_map<std::string, LayoutState> g_default_layouts;
75 }  // namespace pm
76
77
78 PolicyManager::PolicyManager() :
79   eventname2no(),
80   categoryname2no(),
81   areaname2no(),
82   role2category(),
83   category2role(),
84   role2defaultarea()
85 {
86     HMI_DEBUG("wm:pm", "Call");
87 }
88
89 int PolicyManager::initialize() {
90     HMI_DEBUG("wm:pm", "Call");
91
92     int ret = 0;
93
94     // Create convert map
95     for (int i = stm::gStmEventNoMin; i <= stm::gStmEventNoMax; i++) {
96         HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
97         this->eventname2no[stm::gStmEventName[i]] = stm::gStmEventNo[i];
98     }
99
100     for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
101         HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
102         this->categoryname2no[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
103     }
104
105     for (int i = stm::gStmAreaNoMin; i <= stm::gStmAreaNoMax; i++) {
106         HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
107         this->areaname2no[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
108     }
109
110     // Load role.db
111     ret = this->loadRoleDb();
112     if (0 > ret) {
113         HMI_ERROR("wm:pm", "Load role.db Error!!");
114         return ret;
115     }
116
117     // Load layout.db
118     ret = this->loadLayoutDb();
119     if (0 > ret) {
120         HMI_ERROR("wm:pm", "Load layout.db Error!!");
121         return ret;
122     }
123
124     // Initialize state which is managed by PolicyManager
125     this->initializeLocalState();
126
127     // Initialize StateTransitioner
128     stm::stmInitialize();
129
130     // Initialize sd_event loop
131     ret = this->initializeSdEventLoop();
132     if (0 > ret) {
133         HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
134         return ret;
135     }
136
137     return ret;
138 }
139
140 static void initializeLocalCarElementState() {
141     pm::CarElement init_car_ele;
142     init_car_ele.state   = "none";
143     init_car_ele.changed = false;
144
145     for (int i = stm::gStmCarElementNoMin; i <= stm::gStmCarElementNoMax; i++) {
146         const char* car_ele_name = stm::gStmCarElementName[i];
147         pm::g_crr_car_elements[car_ele_name] = init_car_ele;
148     }
149
150     pm::g_prv_car_elements = pm::g_crr_car_elements;
151 }
152
153 static void initializeLocalLayerState() {
154     pm::AreaState init_area;
155     pm::LayoutState init_layout;
156     init_area.name     = stm::gStmAreaName[stm::gStmAreaNoNone];
157     init_area.category = "";
158     init_area.role     = "";
159     init_layout.name   = stm::gStmLayoutName[stm::gStmLayoutNoNone];
160     init_layout.area_list.push_back(init_area);
161
162     for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
163         const char* layer_name = stm::gStmLayerName[i];
164         pm::g_crr_layers[layer_name].name         = layer_name;
165         pm::g_crr_layers[layer_name].layout_state = init_layout;
166         pm::g_crr_layers[layer_name].changed      = false;
167     }
168
169     pm::g_prv_layers = pm::g_crr_layers;
170 }
171
172 void PolicyManager::initializeLocalState() {
173     initializeLocalCarElementState();
174     initializeLocalLayerState();
175 }
176
177 int PolicyManager::initializeSdEventLoop() {
178     // Get default event loop object
179     int ret = sd_event_new(&(pm::event_loop));
180     if (0 > ret) {
181         HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
182         return -1;
183     }
184
185     // Create thread for sd_event and detach
186     std::thread sd_event_loop([this]() {
187         while (1) {
188             sd_event_run(pm::event_loop, 1000);
189         }
190     });
191     sd_event_loop.detach();
192
193     return 0;
194 }
195
196 static void addStateToJson(
197   const char* name, bool changed, std::string state, json_object** json_out) {
198     if ((nullptr == name) || (nullptr == json_out)) {
199         HMI_ERROR("wm:pm", "Invalid argument!!!");
200         return;
201     }
202
203     json_object_object_add(*json_out, "name", json_object_new_string(name));
204     json_object_object_add(*json_out, "state", json_object_new_string(state.c_str()));
205     json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
206 }
207
208 static void addStateToJson(const char* layer_name, bool changed,
209                            pm::AreaList area_list, json_object** json_out) {
210     if ((nullptr == layer_name) || (nullptr == json_out)) {
211         HMI_ERROR("wm:pm", "Invalid argument!!!");
212         return;
213     }
214
215     json_object* json_areas = json_object_new_array();
216     json_object* json_tmp;
217     for (pm::AreaState as : area_list) {
218         json_tmp = json_object_new_object();
219         json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
220         json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
221         json_object_array_add(json_areas, json_tmp);
222     }
223
224     json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
225     json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
226     json_object_object_add(*json_out, "areas", json_areas);
227 }
228
229 static void updateLocalCarElementState(stm::stm_state_t crr_state) {
230     int car_state_no;
231     std::string car_state;
232     bool changed;
233
234     // Store previous layers
235     pm::g_prv_car_elements = pm::g_crr_car_elements;
236
237     // Update car elements
238     HMI_DEBUG("wm:pm", ">>> CAR ELEMENT");
239     for (int car_ele_no = stm::gStmCarElementNoMin;
240          car_ele_no <= stm::gStmCarElementNoMax; car_ele_no++) {
241         const char* car_ele_name = stm::gStmCarElementName[car_ele_no];
242
243         car_state_no = crr_state.car_element[car_ele_no].state;
244         car_state = stm::gStmCarStateNameList[car_ele_no][car_state_no];
245         changed = (crr_state.car_element[car_ele_no].changed) ? true : false;
246
247         pm::g_crr_car_elements[car_ele_name].state = car_state;
248         pm::g_crr_car_elements[car_ele_name].changed = changed;
249
250         HMI_DEBUG("wm:pm", ">>> >>> NAME: %s", car_ele_name);
251         HMI_DEBUG("wm:pm", ">>> >>> >>> STATE:%s", car_state.c_str());
252         HMI_DEBUG("wm:pm", ">>> >>> >>> CHANGED:%s", (changed) ? "true" : "false");
253     }
254 }
255
256 static void updateLocalLayerState(int event_data, stm::stm_state_t crr_state) {
257     int event_no, category_no, area_no;
258
259     event_no    = (event_data & STM_MSK_EVT_NO) - 1;
260     category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
261     area_no     = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
262
263     std::string req_evt = std::string((event_no < 0)    ? "-" : stm::gStmEventName[event_no]);
264     std::string req_ctg = std::string((category_no < 0) ? "-" : stm::gStmCategoryName[category_no]);
265     std::string req_area = std::string((area_no < 0)    ? "-" : stm::gStmAreaName[area_no]);
266     std::string req_role = pm::g_req_role_list[event_data];
267     HMI_DEBUG("wm:pm", "REQ: event:%s role:%s category:%s area:%s",
268         req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
269
270     // Store previous layers
271     pm::g_prv_layers = pm::g_crr_layers;
272
273     // Update layers
274     for (int layer_no = stm::gStmLayerNoMin;
275          layer_no <= stm::gStmLayerNoMax; layer_no++) {
276         const char* layer_name = stm::gStmLayerName[layer_no];
277         HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
278
279 #if 1
280         // If restriction mode is changed off -> on,
281         // store current state for state of restriction mode off
282         if ((crr_state.car_element[stm::gStmCarElementNoRestrictionMode].changed)
283             && (stm::gStmRestrictionModeStateNoOn == crr_state.car_element[stm::gStmCarElementNoRestrictionMode].state)) {
284             HMI_DEBUG("wm:pm", "Store current state for state of restriction mode off");
285             pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
286         }
287 #else
288         // If car state is changed car_stop -> car_run,
289         // store current state for state of car stop
290         if ((crr_state.car.changed)
291             && (stm::gStmRunningNoRun == crr_state.car.state)) {
292             HMI_DEBUG("wm:pm", "Store current state for state of car stop");
293             pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
294         }
295 #endif
296
297         // This layer is changed?
298         int changed = crr_state.layer[layer_no].changed;
299         if (changed) {
300             HMI_DEBUG("wm:pm", "This layer is changed");
301
302             // Get previous layout name of this layer
303             pm::LayoutState prv_layout_state =  pm::g_prv_layers[layer_name].layout_state;
304             std::string prv_layout_name = prv_layout_state.name;
305
306             // Get current layout name of this layer
307             int crr_layout_state_no =  crr_state.layer[layer_no].state;
308             std::string crr_layout_name = std::string(stm::gStmLayoutName[crr_layout_state_no]);
309
310             pm::LayoutState crr_layout_state;
311 #if 1
312             if ((crr_state.car_element[stm::gStmCarElementNoRestrictionMode].changed)
313                 && (stm::gStmRestrictionModeStateNoOff == crr_state.car_element[stm::gStmCarElementNoRestrictionMode].state)) {
314                 // If restriction mode is changed on -> off,
315                 // restore state of restriction mode off
316                 HMI_DEBUG("wm:pm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
317 #else
318             if ((crr_state.car_element[gStmCarElementNoRunning].changed)
319                 && (stm::gStmCarElementNoStop == crr_state.car_element[gStmCarElementNoRunning].state)) {
320                 // If car state is changed car_run -> car_stop,
321                 // restore state of car stop
322                 HMI_DEBUG("wm:pm", "Car state is changed car_run -> car_stop, so restore state of car stop");
323 #endif
324                 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
325                 crr_layout_name = crr_layout_state.name;
326                 if ((prv_layout_name == crr_layout_name)
327                     && (stm::gStmAreaName[stm::gStmAreaNoNone] == crr_layout_name)) {
328                     changed  = 0;
329                 }
330             }
331             else if ((prv_layout_name == crr_layout_name)
332                      && (stm::gStmAreaName[stm::gStmAreaNoNone] == crr_layout_name)) {
333                 // Copy previous layout state for current
334                 crr_layout_state = prv_layout_state;
335                 changed  = 0;
336             }
337             else {
338                 // Copy previous layout state for current
339                 crr_layout_state = prv_layout_state;
340
341                 HMI_DEBUG("wm:pm", "layout name previous:%s current:%s",
342                           prv_layout_name.c_str(), crr_layout_name.c_str());
343                 if (prv_layout_name == crr_layout_name) {
344                     HMI_DEBUG("wm:pm", "Previous layout is same with current");
345                 }
346                 else {
347                     // If previous layout is NOT same with current,
348                     // current areas is set with default value
349                     HMI_DEBUG("wm:pm", "Previous layout is NOT same with current");
350                     crr_layout_state.name         = pm::g_default_layouts[crr_layout_name].name;
351                     crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
352                     crr_layout_state.area_list    = pm::g_default_layouts[crr_layout_name].area_list;
353                 }
354
355                 // Create candidate list
356                 std::map<std::string, pm::AreaList> cand_list;
357                 for (int ctg_no=stm::gStmCategoryNoMin;
358                      ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
359                     const char* ctg = stm::gStmCategoryName[ctg_no];
360                     HMI_DEBUG("wm:pm", "ctg:%s", ctg);
361
362                     // Create candidate list for category from the previous displayed categories
363                     pm::AreaList tmp_cand_list;
364                     for (pm::AreaState area_state : prv_layout_state.area_list) {
365                         if (std::string(ctg) == area_state.category) {
366                             // If there is the category which is same with new category in previous layout,
367                             // push it to list
368                             HMI_DEBUG("wm:pm", "Push to candidate list category:%s role:%s",
369                                       area_state.category.c_str(), area_state.role.c_str());
370                             tmp_cand_list.push_back(area_state);
371                         }
372                     }
373
374                     int candidate_num = prv_layout_state.category_num[ctg];
375                     int blank_num = crr_layout_state.category_num[ctg];
376                     HMI_DEBUG("wm:pm", "blank_num:%d candidate_num:%d", blank_num, candidate_num);
377
378                     // If requested event is "activate"
379                     // and there are requested category and area,
380                     // update area with requested role in current layout.
381                     bool request_for_this_layer = false;
382                     bool updated = false;
383                     if ((ctg == req_ctg) && ("activate" == req_evt) ) {
384                         HMI_DEBUG("wm:pm", "requested event is activate");
385                         for (pm::AreaState &as : crr_layout_state.area_list) {
386                             if (as.category == req_ctg) {
387                                 request_for_this_layer = true;
388
389                                 if (as.name == req_area) {
390                                     HMI_DEBUG("wm:pm", "Update current layout: area:%s category:%s role:%s",
391                                               as.name.c_str(), as.category.c_str(), as.role.c_str());
392                                     as.role = req_role;
393                                     blank_num--;
394                                     updated = true;
395                                     break;
396                                 }
397                             }
398                         }
399
400                         // If NOT updated: there is not requested area in new layout, 
401                         // so push requested role to candidate list
402                         if (request_for_this_layer && (!updated)) {
403                             HMI_DEBUG("wm:pm", "Push request to candidate list");
404                             pm::AreaState area_state;
405                             area_state.name = req_area;
406                             area_state.category = req_ctg;
407                             area_state.role = req_role;
408                             tmp_cand_list.push_back(area_state);
409                         }
410                     }
411
412                     // Compare number of candidate/blank,
413                     // And remove role in order of the oldest as necessary
414                     if (candidate_num < blank_num) {
415                         // Refer history stack
416                         // and add to the top of tmp_cand_list in order to the newest
417                         while (candidate_num != blank_num) {
418                             pm::AreaState area_state;
419                             area_state.name = "";
420                             area_state.category = ctg;
421                             if (0 != crr_layout_state.role_history[ctg].size()) {
422                                 HMI_DEBUG("wm:pm", "Use role in history stack:%s",
423                                           crr_layout_state.role_history[ctg].back().c_str());
424                                 area_state.role = crr_layout_state.role_history[ctg].back();
425                                 crr_layout_state.role_history[ctg].pop_back();
426                             }
427                             else {
428                                 HMI_ERROR("wm:pm", "There is no role in history stack!!");
429                                 area_state.role = "";
430                             }
431                             tmp_cand_list.push_back(area_state);
432                             candidate_num++;
433                         }
434                     }
435                     else if (candidate_num > blank_num) {
436                         HMI_DEBUG("wm:pm", "candidate_num > blank_num");
437
438                         // Remove the oldest role from candidate list
439                         while (candidate_num != blank_num) {
440                             std::string removed_role = tmp_cand_list.begin()->role;
441                             HMI_DEBUG("wm:pm", "Remove the oldest data(role:%s) from tmp_cand_list",
442                                       removed_role.c_str());
443                             tmp_cand_list.erase(tmp_cand_list.begin());
444                             candidate_num--;
445
446                             // Push removed data to history stack
447                             crr_layout_state.role_history[ctg].push_back(removed_role);
448                         }
449                     }
450                     else {  // (candidate_num == blank_num)
451                         // nop
452                     }
453
454                     cand_list[ctg] = tmp_cand_list;
455                 }
456
457                 // Update areas
458                 for (pm::AreaState &as : crr_layout_state.area_list) {
459                     HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
460                               as.name.c_str(), as.category.c_str());
461                     if ("" == as.role) {
462                         HMI_DEBUG("wm:pm", "Update this area with role:%s",
463                                   cand_list[as.category].begin()->role.c_str());
464                         as.role = cand_list[as.category].begin()->role;
465                         cand_list[as.category].erase(cand_list[as.category].begin());
466                     }
467                 }
468             }
469             // Update current layout of this layer
470             pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
471         }
472         // Update changed flag
473         pm::g_crr_layers[layer_name].changed = (changed) ? true : false;
474     }
475
476     // Erase role for the event_data from list
477     pm::g_req_role_list.erase(event_data);
478
479     // Check
480     for (auto itr : pm::g_crr_layers) {
481         pm::LayerState ls = itr.second;
482         HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
483         HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
484
485         for (pm::AreaState as : ls.layout_state.area_list) {
486             HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
487             HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
488             HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
489         }
490         HMI_DEBUG("wm:pm", ">>> >>> CHANGED:%s", (ls.changed) ? "true" : "false");
491     }
492 }
493
494 static void updateLocalState(int event_data, stm::stm_state_t crr_state) {
495     updateLocalCarElementState(crr_state);
496     updateLocalLayerState(event_data, crr_state);
497 }
498
499 static void createOutputInformation(stm::stm_state_t crr_state, json_object **json_out) {
500     json_object* json_tmp;
501
502     // Create car element information
503     // {
504     //     "car_elements": [
505     //     {
506     //         "parking_brake": {
507     //             "changed": <bool>,
508     //             "state": <const char*>
509     //         },
510     //         ...
511     //     },
512     json_object* json_car_ele = json_object_new_array();
513     const char* car_ele_name;
514     for (int car_ele_no = stm::gStmCarElementNoMin;
515          car_ele_no <= stm::gStmCarElementNoMax; car_ele_no++) {
516         car_ele_name = stm::gStmCarElementName[car_ele_no];
517         json_tmp = json_object_new_object();
518         addStateToJson(car_ele_name,
519                        pm::g_crr_car_elements[car_ele_name].changed,
520                        pm::g_crr_car_elements[car_ele_name].state,
521                        &json_tmp);
522         json_object_array_add(json_car_ele, json_tmp);
523     }
524     json_object_object_add(*json_out, "car_elements", json_car_ele);
525
526     // Create layout information
527     //
528     //     "layers": [
529     //     {
530     //         "homescreen": {
531     //             "changed": <bool>,
532     //             "areas": [
533     //             {
534     //                 "name":<const char*>,
535     //                 "role":<const char*>
536     //             }.
537     //             ...
538     //             ]
539     //         }
540     //     },
541     //     ...
542     json_object* json_layer = json_object_new_array();
543     const char* layer_name;
544     for (int layer_no = stm::gStmLayerNoMin;
545          layer_no <= stm::gStmLayerNoMax; layer_no++) {
546         layer_name = stm::gStmLayerName[layer_no];
547         json_tmp = json_object_new_object();
548         addStateToJson(layer_name,
549                        pm::g_crr_layers[layer_name].changed,
550                        pm::g_crr_layers[layer_name].layout_state.area_list,
551                        &json_tmp);
552         json_object_array_add(json_layer, json_tmp);
553     }
554     json_object_object_add(*json_out, "layers", json_layer);
555 }
556
557 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role);
558 static void controlTimerEvent(stm::stm_state_t crr_state) {
559     if (crr_state.car_element[stm::gStmCarElementNoRunning].changed) {
560         if (stm::gStmRunningNoRun == crr_state.car_element[stm::gStmCarElementNoRunning].state) {
561             // Set delay event(restriction mode on)
562             checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, "");
563         }
564         else if (stm::gStmRunningNoStop == crr_state.car_element[stm::gStmCarElementNoRunning].state) {
565             // Stop timer for restriction on event
566             if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
567               != pm::event_source_list.end()) {
568                 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
569                 sd_event_source *event_source
570                     = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
571                 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
572                 if (0 > ret) {
573                     HMI_ERROR("wm:pm", "Failed to stop timer");
574                 }
575             }
576
577             // Set event(restriction mode off)
578             checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, "");
579         }
580     }
581 }
582
583 static int checkPolicy(sd_event_source *source, void *data) {
584     HMI_DEBUG("wm:pm", "Call");
585     HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
586
587     int event_data = *((int*)data);
588
589     int event_no, category_no, area_no;
590     event_no    = (event_data & STM_MSK_EVT_NO) - 1;
591     category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
592     area_no     = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
593     HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
594               (event_no < 0)    ? "-" : stm::gStmEventName[event_no],
595               (category_no < 0) ? "-" : stm::gStmCategoryName[category_no],
596               (area_no < 0)     ? "-" : stm::gStmAreaName[area_no]);
597
598     // Transition state
599     stm::stm_state_t crr_state;
600     int ret = stm::stmTransitionState(event_data, &crr_state);
601     if (0 > ret) {
602         HMI_ERROR("wm:pm", "Failed transition state");
603         if (nullptr != pm::callback.onError) {
604             json_object* json_out = json_object_new_object();
605             json_object_object_add(json_out, "message",
606                                    json_object_new_string("Failed to transition state"));
607             json_object_object_add(json_out, "event",
608                                    json_object_new_string(stm::gStmEventName[event_no]));
609             json_object_object_add(json_out, "role",
610                                    json_object_new_string(pm::g_req_role_list[event_data].c_str()));
611             json_object_object_add(json_out, "area",
612                                    json_object_new_string(stm::gStmAreaName[area_no]));
613             pm::callback.onError(json_out);
614             json_object_put(json_out);
615         }
616         return -1;
617     }
618
619     // Update state which is managed by PolicyManager
620     updateLocalState(event_data, crr_state);
621
622     // Create output information for ResourceManager
623     json_object* json_out = json_object_new_object();
624     createOutputInformation(crr_state, &json_out);
625
626     // Notify changed state
627     if (nullptr != pm::callback.onStateTransitioned) {
628         pm::callback.onStateTransitioned(json_out);
629     }
630
631     // Start/Stop timer events
632     controlTimerEvent(crr_state);
633
634     // Release json_object
635     json_object_put(json_out);
636
637     // Release data
638     delete (int*)data;
639
640     // Destroy sd_event_source object
641     sd_event_source_unref(source);
642
643     // Remove event source from list
644     if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
645         pm::event_source_list.erase(event_data);
646     }
647
648     HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
649     return 0;
650 }
651
652 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
653     HMI_DEBUG("wm:pm", "Call");
654
655     int ret = checkPolicy(source, data);
656     return ret;
657 }
658
659 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role)
660 {
661     HMI_DEBUG("wm:pm", "Call");
662     HMI_DEBUG("wm:pm", "event:0x%x delay:%d role:%s", event, delay_ms, role.c_str());
663
664     // Store requested role
665     pm::g_req_role_list[event] = role;
666
667     if (0 == delay_ms) {
668         int ret = sd_event_add_defer(pm::event_loop, NULL,
669                                      &checkPolicy, new int(event));
670         if (0 > ret) {
671             HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
672             pm::g_req_role_list.erase(event);
673             return -1;
674         }
675     }
676     else {
677         // Get current time
678         struct timespec time_spec;
679         clock_gettime(CLOCK_MONOTONIC, &time_spec);
680
681         // Calculate timer fired time
682         uint64_t usec = (time_spec.tv_sec * 1000000)
683             + (time_spec.tv_nsec / 1000)
684             + (delay_ms * 1000);
685
686         // Set timer
687         struct sd_event_source* event_source;
688         int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
689                                     &timerEvent, new int(event));
690         if (0 > ret) {
691             HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
692             pm::g_req_role_list.erase(event);
693             return -1;
694         }
695
696         // Store event source
697         pm::event_source_list[event] = event_source;
698     }
699
700     return 0;
701 }
702
703 void PolicyManager::registerCallback(CallbackTable callback) {
704     HMI_DEBUG("wm:pm", "Call");
705
706     pm::callback.onStateTransitioned = callback.onStateTransitioned;
707     pm::callback.onError             = callback.onError;
708 }
709
710 int PolicyManager::setInputEventData(json_object* json_in) {
711     HMI_DEBUG("wm:pm", "Call");
712
713     // Check arguments
714     if (nullptr == json_in) {
715         HMI_ERROR("wm:pm", "Argument is NULL!!");
716         return -1;
717     }
718
719     // Get event from json_object
720     const char* event = this->getStringFromJson(json_in, "event");
721     int event_no = stm::gStmEventNoNone;
722     if (nullptr != event) {
723         // Convert name to number
724         auto itr = this->eventname2no.find(event);
725         if (this->eventname2no.end() != itr) {
726             event_no = this->eventname2no[event];
727             HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
728         }
729         else {
730             HMI_ERROR("wm:pm", "Invalid event name!!");
731             return -1;
732         }
733     }
734     else {
735         HMI_ERROR("wm:pm", "Event is not set!!");
736         return -1;
737     }
738
739     // Get role from json_object
740     const char* role = this->getStringFromJson(json_in, "role");
741     int category_no = stm::gStmCategoryNoNone;
742     if (nullptr != role) {
743         HMI_DEBUG("wm:pm", "role(%s)", role);
744
745         // Convert role to category
746         std::string category;
747         auto itr = this->role2category.find(role);
748         if (this->role2category.end() != itr) {
749             category = this->role2category[role];
750
751             // Convert name to number
752             category_no = categoryname2no[category];
753             HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category.c_str(), category_no);
754         }
755     }
756
757     if (stm::gStmCategoryNoNone == category_no) {
758         role = "";
759     }
760
761     // Get areat from json_object
762     const char* area = this->getStringFromJson(json_in, "area");
763     int area_no = stm::gStmAreaNoNone;
764     if (nullptr != area) {
765         auto itr = this->areaname2no.find(area);
766         if (this->areaname2no.end() != itr) {
767             area_no = this->areaname2no[area];
768             HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
769         }
770     }
771
772     // If role is set and area is not set, use default area
773     if ((stm::gStmAreaNoNone == area_no)
774         && (stm::gStmCategoryNoNone != category_no)) {
775         std::string def_area = this->role2defaultarea[role];
776         area_no = this->areaname2no[def_area];
777         HMI_DEBUG("wm:pm", "area(%s:%d)", def_area.c_str(), area_no);
778     }
779
780     // Set event info to the queue
781     EventInfo event_info;
782     event_info.event = (event_no | category_no | area_no);
783     event_info.role = std::string(role);
784     event_info.delay = 0;
785     this->event_info_queue.push(event_info);
786
787     return 0;
788 }
789
790 int PolicyManager::executeStateTransition() {
791     HMI_DEBUG("wm:pm", "Call");
792
793     int ret;
794     EventInfo event_info;
795
796     while (!this->event_info_queue.empty()) {
797         // Get event info from queue and delete
798         event_info = this->event_info_queue.front();
799         this->event_info_queue.pop();
800
801         // Set event info for checking policy
802         ret = checkPolicyEntry(event_info.event, event_info.delay, event_info.role);
803     }
804     return ret;
805 }
806
807 void PolicyManager::undoState() {
808     HMI_DEBUG("wm:pm", "Call");
809
810     // Undo state of STM
811     stm::stmUndoState();
812
813     pm::g_crr_layers = pm::g_prv_layers;
814 }
815
816 extern const char* kDefaultRoleDb;
817 int PolicyManager::loadRoleDb() {
818     HMI_DEBUG("wm:pm", "Call");
819
820     std::string file_name;
821
822     // Get afm application installed dir
823     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
824     HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
825
826     if (!afm_app_install_dir) {
827         HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
828     }
829     else {
830         file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
831     }
832
833     // Load role.db
834     json_object* json_obj;
835     int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
836     if (0 > ret) {
837         HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
838         json_obj = json_tokener_parse(kDefaultRoleDb);
839     }
840     HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
841
842     json_object* json_roles;
843     if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
844         HMI_ERROR("wm:pm", "Parse Error!!");
845         return -1;
846     }
847
848     int len = json_object_array_length(json_roles);
849     HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
850     HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
851
852     json_object* json_tmp;
853     const char* category;
854     const char* roles;
855     const char* areas;
856     for (int i=0; i<len; i++) {
857         json_tmp = json_object_array_get_idx(json_roles, i);
858
859         category = this->getStringFromJson(json_tmp, "category");
860         roles =  this->getStringFromJson(json_tmp, "role");
861         areas =  this->getStringFromJson(json_tmp, "area");
862
863         if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
864             HMI_ERROR("wm:pm", "Parse Error!!");
865             return -1;
866         }
867
868         // Parse roles by '|'
869         std::vector<std::string> vct_roles;
870         vct_roles = this->parseString(std::string(roles), '|');
871
872         // Parse areas by '|'
873         std::vector<std::string> vct_areas;
874         vct_areas = this->parseString(std::string(areas), '|');
875
876         // Set role, category, default area
877         for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
878             // Delete space from role and area name
879             std::string role = this->deleteSpace(*itr);
880             std::string area = this->deleteSpace(vct_areas[0]);
881
882             this->role2category[role] = std::string(category);
883             this->role2defaultarea[role] = area;
884         }
885
886         this->category2role[std::string(category)] = std::string(roles);
887     }
888
889     // Check
890     HMI_DEBUG("wm:pm", "Check role2category");
891     for (auto& x:this->role2category){
892         HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
893     }
894
895     HMI_DEBUG("wm:pm", "Check role2defaultarea");
896     for (auto& x:this->role2defaultarea){
897         HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
898     }
899
900     HMI_DEBUG("wm:pm", "Check category2role");
901     for (auto& x:this->category2role){
902         HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
903     }
904
905     return 0;
906 }
907
908 extern const char* kDefaultLayoutDb;
909 int PolicyManager::loadLayoutDb() {
910     HMI_DEBUG("wm:pm", "Call");
911
912     // Get afm application installed dir
913     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
914     HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
915
916     std::string file_name;
917     if (!afm_app_install_dir) {
918         HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
919     }
920     else {
921         file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
922     }
923
924     // Load layout.db
925     json_object* json_obj;
926     int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
927     if (0 > ret) {
928         HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
929         json_obj = json_tokener_parse(kDefaultLayoutDb);
930     }
931     HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
932
933     // Perse layouts
934     HMI_DEBUG("wm:pm", "Perse layouts");
935     json_object* json_cfg;
936     if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
937         HMI_ERROR("wm:pm", "Parse Error!!");
938         return -1;
939     }
940
941     int len = json_object_array_length(json_cfg);
942     HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
943     HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
944
945     const char* layout;
946     const char* role;
947     const char* category;
948     for (int i=0; i<len; i++) {
949         json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
950
951         layout = this->getStringFromJson(json_tmp, "name");
952         if (nullptr == layout) {
953             HMI_ERROR("wm:pm", "Parse Error!!");
954             return -1;
955         }
956         HMI_DEBUG("wm:pm", "> layout:%s", layout);
957
958         json_object* json_area_array;
959         if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
960           HMI_ERROR("wm:pm", "Parse Error!!");
961           return -1;
962         }
963
964         int len_area = json_object_array_length(json_area_array);
965         HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
966         HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
967
968         pm::LayoutState layout_state;
969         pm::AreaState area_state;
970         std::map<std::string, int> category_num;
971         for (int ctg_no = stm::gStmCategoryNoMin;
972              ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
973             const char* ctg_name = stm::gStmCategoryName[ctg_no];
974             category_num[ctg_name] = 0;
975         }
976
977         for (int j=0; j<len_area; j++) {
978             json_object* json_area = json_object_array_get_idx(json_area_array, j);
979
980             // Get area name
981             const char* area = this->getStringFromJson(json_area, "name");
982             if (nullptr == area) {
983               HMI_ERROR("wm:pm", "Parse Error!!");
984               return -1;
985             }
986             area_state.name = std::string(area);
987             HMI_DEBUG("wm:pm", ">> area:%s", area);
988
989             // Get app attribute of the area
990             category = this->getStringFromJson(json_area, "category");
991             if (nullptr == category) {
992                 HMI_ERROR("wm:pm", "Parse Error!!");
993                 return -1;
994             }
995             area_state.category = std::string(category);
996             category_num[category]++;
997             HMI_DEBUG("wm:pm", ">>> category:%s", category);
998
999             role = this->getStringFromJson(json_area, "role");
1000             if (nullptr != role) {
1001                 // Role is NOT essential here
1002                 area_state.role = std::string(role);
1003             }
1004             else {
1005                 area_state.role = std::string("");
1006
1007             }
1008             HMI_DEBUG("wm:pm", ">>> role:%s", role);
1009
1010             layout_state.area_list.push_back(area_state);
1011
1012         }
1013
1014         layout_state.name = layout;
1015         layout_state.category_num = category_num;
1016         pm::g_default_layouts[layout] = layout_state;
1017     }
1018
1019     // initialize for none layout
1020     pm::LayoutState none_layout_state;
1021     memset(&none_layout_state, 0, sizeof(none_layout_state));
1022     none_layout_state.name                 = "none";
1023     pm::g_default_layouts["none"] = none_layout_state;
1024
1025     // Check
1026     for(auto itr_layout = pm::g_default_layouts.begin();
1027       itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
1028         HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
1029
1030         for (auto itr_area = itr_layout->second.area_list.begin();
1031           itr_area != itr_layout->second.area_list.end(); ++itr_area) {
1032             HMI_DEBUG("wm:pm", ">>> >>> area    :%s", itr_area->name.c_str());
1033             HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
1034             HMI_DEBUG("wm:pm", ">>> >>> role    :%s", itr_area->role.c_str());
1035         }
1036     }
1037
1038     // Release json_object
1039     json_object_put(json_obj);
1040
1041     return 0;
1042 }
1043
1044 // TODO:
1045 // This function will be removed because json_helper has same function.
1046 // json_helper should be library.
1047 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
1048     if ((nullptr == obj) || (nullptr == key)) {
1049         HMI_ERROR("wm:pm", "Argument is nullptr!!!");
1050         return nullptr;
1051     }
1052
1053     json_object* tmp;
1054     if (!json_object_object_get_ex(obj, key, &tmp)) {
1055         HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1056         return nullptr;
1057     }
1058
1059     return json_object_get_string(tmp);
1060 }
1061
1062 // TODO:
1063 // This function will be removed because json_helper has same function.
1064 // json_helper should be library.
1065 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
1066     const int input_size = 128;
1067     int ret = -1;
1068
1069     if ((nullptr == file) || (nullptr == obj)) {
1070         HMI_ERROR("wm:jh", "Argument is nullptr!!!");
1071         return ret;
1072     }
1073
1074     HMI_DEBUG("wm:jh", "Input file: %s", file);
1075
1076     // Open json file
1077     FILE *fp = fopen(file, "rb");
1078     if (nullptr == fp) {
1079         HMI_ERROR("wm:jh", "Could not open file");
1080         return ret;
1081     }
1082
1083     // Parse file data
1084     struct json_tokener *tokener = json_tokener_new();
1085     enum json_tokener_error json_error;
1086     char buffer[input_size];
1087     int block_cnt = 1;
1088     while (1) {
1089         size_t len = fread(buffer, sizeof(char), input_size, fp);
1090         *obj = json_tokener_parse_ex(tokener, buffer, len);
1091         if (nullptr != *obj) {
1092             HMI_DEBUG("wm:jh", "File input is success");
1093             ret = 0;
1094             break;
1095         }
1096
1097         json_error = json_tokener_get_error(tokener);
1098         if ((json_tokener_continue != json_error)
1099             || (input_size > len)) {
1100             HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1101                       (input_size * block_cnt), json_tokener_error_desc(json_error));
1102             HMI_ERROR("wm:jh", "\n%s", buffer);
1103             *obj = nullptr;
1104             break;
1105         }
1106         block_cnt++;
1107     }
1108
1109     // Close json file
1110     fclose(fp);
1111
1112     // Free json_tokener
1113     json_tokener_free(tokener);
1114
1115     return ret;
1116 }
1117
1118 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1119     // Parse string by delimiter
1120     std::vector<std::string> vct;
1121     std::stringstream ss{str};
1122     std::string buf;
1123     while (std::getline(ss, buf, delimiter)) {
1124       if (!buf.empty()) {
1125         vct.push_back(buf);
1126       }
1127     }
1128     return vct;
1129 }
1130
1131 std::string PolicyManager::deleteSpace(std::string str) {
1132     std::string ret = str;
1133     size_t pos;
1134     while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1135       ret.erase(pos, 1);
1136     }
1137     return ret;
1138 }
1139
1140 const char* kDefaultRoleDb = "{ \
1141     \"roles\":[ \
1142     { \
1143         \"category\": \"homescreen\", \
1144         \"role\": \"homescreen\", \
1145         \"area\": \"full\", \
1146     }, \
1147     { \
1148         \"category\": \"map\", \
1149         \"role\": \"map\", \
1150         \"area\": \"full | normal | split.main\", \
1151     }, \
1152     { \
1153         \"category\": \"general\", \
1154         \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1155         \"area\": \"normal\", \
1156     }, \
1157     { \
1158         \"category\": \"phone\", \
1159         \"role\": \"phone\", \
1160         \"area\": \"normal\", \
1161     }, \
1162     { \
1163         \"category\": \"splitable\", \
1164         \"role\": \"splitable1 | splitable2\", \
1165         \"area\": \"normal | split.main | split.sub\", \
1166     }, \
1167     { \
1168         \"category\": \"popup\", \
1169         \"role\": \"popup\", \
1170         \"area\": \"on_screen\", \
1171     }, \
1172     { \
1173         \"category\": \"system_alert\", \
1174         \"role\": \"system_alert\", \
1175         \"area\": \"on_screen\", \
1176     }, \
1177     { \
1178         \"category\": \"tbt\", \
1179         \"role\": \"tbt\", \
1180         \"area\": \"hud\", \
1181     } \
1182     ] \
1183 }";
1184
1185
1186 const char* kDefaultLayoutDb = "{ \
1187     \"layouts\": [ \
1188         { \
1189             \"name\": \"pu\", \
1190             \"layer\": \"on_screen\", \
1191             \"areas\": [ \
1192                 { \
1193                     \"name\": \"pop_up\", \
1194                     \"role\": \"incomming_call\" \
1195                 } \
1196             ] \
1197         }, \
1198         { \
1199             \"name\": \"sa\", \
1200             \"layer\": \"on_screen\", \
1201             \"areas\": [ \
1202                 { \
1203                     \"name\": \"system_alert\", \
1204                     \"role\": \"system_alert\" \
1205                 } \
1206             ] \
1207         }, \
1208         { \
1209             \"name\": \"m1\", \
1210             \"layer\": \"apps\", \
1211             \"areas\": [ \
1212                 { \
1213                     \"name\": \"normal\", \
1214                     \"role\": \"map\" \
1215                 } \
1216             ] \
1217         }, \
1218         { \
1219             \"name\": \"m2\", \
1220             \"layer\": \"apps\", \
1221             \"areas\": [ \
1222                 { \
1223                     \"name\": \"split.main\", \
1224                     \"role\": \"map\" \
1225                 }, \
1226                 { \
1227                     \"name\": \"split.sub\", \
1228                     \"category\": \"hvac\" \
1229                 } \
1230             ] \
1231         }, \
1232         { \
1233             \"name\": \"mf\", \
1234             \"layer\": \"apps\", \
1235             \"areas\": [ \
1236                 { \
1237                     \"name\": \"full\", \
1238                     \"role\": \"map\" \
1239                 } \
1240             ] \
1241         }, \
1242         { \
1243             \"name\": \"s1\", \
1244             \"layer\": \"apps\", \
1245             \"areas\": [ \
1246                 { \
1247                     \"name\": \"normal\", \
1248                     \"category\": \"splitable\" \
1249                 } \
1250             ] \
1251         }, \
1252         { \
1253             \"name\": \"s2\", \
1254             \"layer\": \"apps\", \
1255             \"areas\": [ \
1256                 { \
1257                     \"name\": \"split.main\", \
1258                     \"category\": \"splitable\" \
1259                 }, \
1260                 { \
1261                     \"name\": \"split.sub\", \
1262                     \"category\": \"splitable\" \
1263                 } \
1264             ] \
1265         }, \
1266         { \
1267             \"name\": \"g\", \
1268             \"layer\": \"apps\", \
1269             \"areas\": [ \
1270                 { \
1271                     \"name\": \"normal\", \
1272                     \"category\": \"general\" \
1273                 } \
1274             ] \
1275         }, \
1276         { \
1277             \"name\": \"hs\", \
1278             \"layer\": \"homescreen\", \
1279             \"areas\": [ \
1280                 { \
1281                     \"name\": \"full\", \
1282                     \"role\": \"homescreen\" \
1283                 } \
1284             ] \
1285         } \
1286     ], \
1287     \"areas\": [ \
1288         { \
1289             \"name\": \"normal\", \
1290             \"rect\": { \
1291                 \"x\": 0, \
1292                 \"y\": 218, \
1293                 \"w\": 1080, \
1294                 \"h\": 1488 \
1295             } \
1296         }, \
1297         { \
1298             \"name\": \"split.main\", \
1299             \"rect\": { \
1300                 \"x\": 0, \
1301                 \"y\": 218, \
1302                 \"w\": 1080, \
1303                 \"h\": 744 \
1304             } \
1305         }, \
1306         { \
1307             \"name\": \"split.sub\", \
1308             \"rect\": { \
1309                 \"x\": 0, \
1310                 \"y\": 962, \
1311                 \"w\": 1080, \
1312                 \"h\": 744 \
1313             } \
1314         }, \
1315         { \
1316             \"name\": \"full\", \
1317             \"rect\": { \
1318                 \"x\": 0, \
1319                 \"y\": 0, \
1320                 \"w\": 1080, \
1321                 \"h\": 1920 \
1322             } \
1323         }, \
1324         { \
1325             \"name\": \"pop_up\", \
1326             \"rect\": { \
1327                 \"x\": 0, \
1328                 \"y\": 640, \
1329                 \"w\": 1080, \
1330                 \"h\": 640 \
1331             } \
1332         }, \
1333         { \
1334             \"name\": \"system_alert\", \
1335             \"rect\": { \
1336                 \"x\": 0, \
1337                 \"y\": 640, \
1338                 \"w\": 1080, \
1339                 \"h\": 640 \
1340             } \
1341         } \
1342     ] \
1343 }";