POI: AGL LifeCycle Management
[apps/agl-service-windowmanager.git] / src / window_manager.hpp
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 #ifndef WINDOW_MANAGER_HPP
18 #define WINDOW_MANAGER_HPP
19
20 #include <atomic>
21 #include <memory>
22 #include <unordered_map>
23 #include <experimental/optional>
24 #include "result.hpp"
25 #include "pm_wrapper.hpp"
26 #include "util.hpp"
27 #include "request.hpp"
28 #include "wm_error.hpp"
29 #include "wm_layer_control.hpp"
30 extern "C"
31 {
32 #include <afb/afb-binding.h>
33 }
34
35 #include "activity_manager.hpp"
36
37 struct json_object;
38
39 namespace wm
40 {
41
42 using std::experimental::optional;
43
44 /* DrawingArea name used by "{layout}.{area}" */
45 extern const char kNameLayoutNormal[];
46 extern const char kNameLayoutSplit[];
47 extern const char kNameAreaFull[];
48 extern const char kNameAreaMain[];
49 extern const char kNameAreaSub[];
50
51 /* Key for json obejct */
52 extern const char kKeyDrawingName[];
53 extern const char kKeyDrawingArea[];
54 extern const char kKeyDrawingRect[];
55 extern const char kKeyX[];
56 extern const char kKeyY[];
57 extern const char kKeyWidth[];
58 extern const char kKeyHeigh[];
59 extern const char kKeyWidthPixel[];
60 extern const char kKeyHeightPixel[];
61 extern const char kKeyWidthMm[];
62 extern const char kKeyHeightMm[];
63 extern const char kKeyScale[];
64 extern const char kKeyIds[];
65
66 struct id_allocator
67 {
68     unsigned next = 1;
69     // Surfaces that where requested but not yet created
70     std::unordered_map<unsigned, std::string> id2name;
71     std::unordered_map<std::string, unsigned> name2id;
72
73     id_allocator(id_allocator const &) = delete;
74     id_allocator(id_allocator &&) = delete;
75     id_allocator &operator=(id_allocator const &);
76     id_allocator &operator=(id_allocator &&) = delete;
77
78     // Insert and return a new ID
79     unsigned generate_id(std::string const &name)
80     {
81         unsigned sid = this->next++;
82         this->id2name[sid] = name;
83         this->name2id[name] = sid;
84         HMI_DEBUG("allocated new id %u with name %s", sid, name.c_str());
85         return sid;
86     }
87
88     // Insert a new ID which defined outside
89     void register_name_id(std::string const &name, unsigned sid)
90     {
91         this->id2name[sid] = name;
92         this->name2id[name] = sid;
93         HMI_DEBUG("register id %u with name %s", sid, name.c_str());
94         return;
95     }
96
97     // Lookup by ID or by name
98     optional<unsigned> lookup(std::string const &name) const
99     {
100         auto i = this->name2id.find(name);
101         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
102     }
103
104     optional<std::string> lookup(unsigned id) const
105     {
106         auto i = this->id2name.find(id);
107         return i == this->id2name.end() ? nullopt
108                                         : optional<std::string>(i->second);
109     }
110
111     // Remove a surface id and name
112     void remove_id(std::string const &name)
113     {
114         auto i = this->name2id.find(name);
115         if (i != this->name2id.end())
116         {
117             this->id2name.erase(i->second);
118             this->name2id.erase(i);
119         }
120     }
121
122     void remove_id(unsigned id)
123     {
124         auto i = this->id2name.find(id);
125         if (i != this->id2name.end())
126         {
127             this->name2id.erase(i->second);
128             this->id2name.erase(i);
129         }
130     }
131 };
132
133 struct TmpClient
134 {
135     std::string   appid;
136     unsigned layer;
137 };
138
139
140 class WindowManager
141 {
142   public:
143     typedef std::unordered_map<uint32_t, struct rect> rect_map;
144     typedef std::function<void(const char *err_msg)> reply_func;
145
146     enum EventType
147     {
148         Event_Val_Min = 0,
149
150         Event_Active = Event_Val_Min,
151         Event_Inactive,
152
153         Event_Visible,
154         Event_Invisible,
155
156         Event_SyncDraw,
157         Event_FlushDraw,
158
159         Event_ScreenUpdated,
160
161         Event_Error,
162
163         Event_Val_Max = Event_Error,
164     };
165
166     explicit WindowManager();
167     ~WindowManager() = default;
168
169     WindowManager(WindowManager const &) = delete;
170     WindowManager &operator=(WindowManager const &) = delete;
171     WindowManager(WindowManager &&) = delete;
172     WindowManager &operator=(WindowManager &&) = delete;
173
174     int init();
175
176     result<int> api_request_surface(char const *appid, char const *role);
177     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
178     void api_activate_window(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
179     void api_deactivate_window(char const *appid, char const *role, const reply_func &reply);
180     void api_enddraw(char const *appid, char const *role);
181     int  api_subscribe(afb_req_t req, int event_id);
182
183     result<json_object *> api_get_display_info();
184     result<json_object *> api_get_area_info(char const *role);
185
186     void send_event(const std::string& evname, const std::string& role);
187     void send_event(const std::string& evname, const std::string& role, const std::string& area, int x, int y, int w, int h);
188
189     // Events from the compositor we are interested in
190     void surface_created(unsigned surface_id);
191     void surface_removed(unsigned surface_id);
192
193     void removeClient(const std::string &appid);
194     void exceptionProcessForTransition();
195
196     // Do not use this function
197     void timerHandler();
198     void startTransitionWrapper(std::vector<WMAction> &actions);
199     void processError(WMError error);
200
201   public:
202     // AGL Lifecycle Management API
203     lcm::ActivityManager amgr;
204
205   private:
206     // WM Events to clients
207     void emit_activated(const std::string& role);
208     void emit_deactivated(const std::string& role);
209     void emit_syncdraw(const std::string& role, char const *area, int x, int y, int w, int h);
210     void emit_syncdraw(const std::string &role, const std::string &area);
211     void emit_flushdraw(const std::string& role);
212     void emit_visible(const std::string& role, bool is_visible);
213     void emit_invisible(const std::string& role);
214     void emit_visible(const std::string& role);
215
216     WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
217                              Task task, unsigned *req_num);
218     WMError checkPolicy(unsigned req_num);
219     WMError startTransition(unsigned req_num);
220
221     WMError doEndDraw(unsigned req_num);
222     void    emitScreenUpdated(unsigned req_num);
223
224     void setTimer();
225     void stopTimer();
226     void processNextRequest();
227
228   private:
229     std::map<std::string, afb_event_t> map_afb_event;
230     std::unordered_map<std::string, struct rect> area2size;
231     std::shared_ptr<LayerControl> lc;
232     PMWrapper pmw;
233     rect_map area_info;
234     struct id_allocator id_alloc;
235
236     // ID allocation and proxy methods for lookup
237     std::unordered_map<unsigned, struct TmpClient> tmp_surface2app;
238 };
239
240 } // namespace wm
241
242 #endif // WINDOW_MANAGER_HPP