1f69474847069a7ecb113a5d514c561911e46c22
[apps/agl-service-windowmanager-2017.git] / src / policy_manager / policy_manager.hpp
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 #ifndef TMCAGLWM_POLICY_MANAGER_HPP
18 #define TMCAGLWM_POLICY_MANAGER_HPP
19
20 #include <functional>
21 #include <unordered_map>
22 #include <vector>
23 #include <queue>
24
25 struct json_object;
26 struct sd_event;
27 struct sd_event_source;
28 struct StmState;
29
30 class PolicyManager
31 {
32
33   public:
34     explicit PolicyManager();
35     ~PolicyManager() = default;
36
37     using Handler = std::function<void(json_object *)>;
38
39     typedef struct
40     {
41         Handler onStateTransitioned;
42         Handler onError;
43     } CallbackTable;
44
45     int initialize();
46     void registerCallback(CallbackTable callback_table);
47     int setInputEventData(json_object *json_in);
48     int executeStateTransition();
49     void undoState();
50
51     // Do not use these functions
52     int transitionState(sd_event_source *source, void *data);
53     int timerEvent(sd_event_source *source, uint64_t usec, void *data);
54
55   private:
56     // Disable copy and move
57     PolicyManager(PolicyManager const &) = delete;
58     PolicyManager &operator=(PolicyManager const &) = delete;
59     PolicyManager(PolicyManager &&) = delete;
60     PolicyManager &operator=(PolicyManager &&) = delete;
61
62     typedef struct EventInfo
63     {
64         int event;
65         std::string role;
66         uint64_t delay;
67     } EventInfo;
68
69     typedef struct AreaState
70     {
71         std::string name;
72         std::string category;
73         std::string role;
74     } AreaState;
75
76     typedef std::vector<AreaState> AreaList;
77     typedef struct LayoutState
78     {
79         std::string name;
80         std::map<std::string, int> category_num;
81         AreaList area_list;
82     } LayoutState;
83
84     typedef struct LayerState
85     {
86         std::string name;
87         LayoutState layout_state;
88         bool changed;
89     } LayerState;
90
91     // Convert map
92     std::unordered_map<std::string, int> eventname2no;
93     std::unordered_map<std::string, int> categoryname2no;
94     std::unordered_map<std::string, int> areaname2no;
95
96     std::unordered_map<std::string, std::string> role2category;
97     std::unordered_map<std::string, std::string> category2role;
98     std::unordered_map<std::string, std::string> role2defaultarea;
99
100     struct sd_event *event_loop;
101     std::queue<EventInfo> event_info_queue;
102     std::map<int, struct sd_event_source *> event_source_list;
103     std::map<int, std::string> req_role_list;
104
105     CallbackTable callback;
106
107     std::unordered_map<std::string, LayerState> prv_layers;
108     std::unordered_map<std::string, LayerState> crr_layers;
109
110     std::unordered_map<std::string, LayoutState> default_layouts;
111
112     std::map<std::string, std::vector<std::string>> role_history;
113
114     void initializeState();
115     void initializeLayerState();
116     void updateState(int event_data, StmState crr_state);
117     void updateLayerState(int event_data, StmState crr_state);
118     void createOutputInformation(StmState crr_state, json_object **json_out);
119     int setStateTransitionProcessToSystemd(int event, uint64_t delay_ms, std::string role);
120
121     void pushRoleHistory(std::string category, std::string role);
122     std::string popRoleHistory(std::string category);
123
124     int loadRoleDb();
125     int loadLayoutDb();
126
127     void dumpLayerState(std::unordered_map<std::string, LayerState> &layers);
128     void dumpRoleHistory();
129
130     void addStateToJson(const char *name, bool changed,
131                         std::string state, json_object **json_out);
132     void addStateToJson(const char *layer_name, bool changed,
133                         AreaList area_list, json_object **json_out);
134     const char *getStringFromJson(json_object *obj, const char *key);
135     int inputJsonFilie(const char *file, json_object **obj);
136
137     std::vector<std::string> parseString(std::string str, char delimiter);
138     std::string deleteSpace(std::string str);
139
140     static const char *kDefaultRoleDb;
141     static const char *kDefaultLayoutDb;
142 };
143
144 #endif // TMCAGLWM_POLICY_MANAGER_HPP