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