Rename file names
[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 "util.hpp"
25 #include "controller_hooks.hpp"
26 #include "wm_layer.hpp"
27 #include "layout.hpp"
28 #include "wayland_ivi_wm.hpp"
29 #include "pm_wrapper.hpp"
30 #include "request.hpp"
31 #include "wm_error.hpp"
32 extern "C"
33 {
34 #include <afb/afb-binding.h>
35 }
36
37 struct json_object;
38
39 namespace wl
40 {
41 struct display;
42 }
43
44 namespace compositor
45 {
46 struct controller;
47 }
48
49 namespace wm
50 {
51
52 using std::experimental::optional;
53
54 /* DrawingArea name used by "{layout}.{area}" */
55 extern const char kNameLayoutNormal[];
56 extern const char kNameLayoutSplit[];
57 extern const char kNameAreaFull[];
58 extern const char kNameAreaMain[];
59 extern const char kNameAreaSub[];
60
61 /* Key for json obejct */
62 extern const char kKeyDrawingName[];
63 extern const char kKeyDrawingArea[];
64 extern const char kKeyDrawingRect[];
65 extern const char kKeyX[];
66 extern const char kKeyY[];
67 extern const char kKeyWidth[];
68 extern const char kKeyHeigh[];
69 extern const char kKeyWidthPixel[];
70 extern const char kKeyHeightPixel[];
71 extern const char kKeyWidthMm[];
72 extern const char kKeyHeightMm[];
73 extern const char kKeyScale[];
74 extern const char kKeyIds[];
75
76 struct id_allocator
77 {
78     unsigned next = 1;
79
80     // Surfaces that where requested but not yet created
81     std::unordered_map<unsigned, std::string> id2name;
82     std::unordered_map<std::string, unsigned> name2id;
83
84     id_allocator(id_allocator const &) = delete;
85     id_allocator(id_allocator &&) = delete;
86     id_allocator &operator=(id_allocator const &);
87     id_allocator &operator=(id_allocator &&) = delete;
88
89     // Insert and return a new ID
90     unsigned generate_id(std::string const &name)
91     {
92         unsigned sid = this->next++;
93         this->id2name[sid] = name;
94         this->name2id[name] = sid;
95         HMI_DEBUG("allocated new id %u with name %s", sid, name.c_str());
96         return sid;
97     }
98
99     // Insert a new ID which defined outside
100     void register_name_id(std::string const &name, unsigned sid)
101     {
102         this->id2name[sid] = name;
103         this->name2id[name] = sid;
104         HMI_DEBUG("register id %u with name %s", sid, name.c_str());
105         return;
106     }
107
108     // Lookup by ID or by name
109     optional<unsigned> lookup(std::string const &name) const
110     {
111         auto i = this->name2id.find(name);
112         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
113     }
114
115     optional<std::string> lookup(unsigned id) const
116     {
117         auto i = this->id2name.find(id);
118         return i == this->id2name.end() ? nullopt
119                                         : optional<std::string>(i->second);
120     }
121
122     // Remove a surface id and name
123     void remove_id(std::string const &name)
124     {
125         auto i = this->name2id.find(name);
126         if (i != this->name2id.end())
127         {
128             this->id2name.erase(i->second);
129             this->name2id.erase(i);
130         }
131     }
132
133     void remove_id(unsigned id)
134     {
135         auto i = this->id2name.find(id);
136         if (i != this->id2name.end())
137         {
138             this->name2id.erase(i->second);
139             this->id2name.erase(i);
140         }
141     }
142 };
143
144 class WindowManager
145 {
146   public:
147     typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
148     typedef std::function<void(const char *err_msg)> reply_func;
149
150     enum EventType
151     {
152         Event_Val_Min = 0,
153
154         Event_Active = Event_Val_Min,
155         Event_Inactive,
156
157         Event_Visible,
158         Event_Invisible,
159
160         Event_SyncDraw,
161         Event_FlushDraw,
162
163         Event_ScreenUpdated,
164
165         Event_Error,
166
167         Event_Val_Max = Event_Error,
168     };
169
170     const std::vector<const char *> kListEventName{
171         "active",
172         "inactive",
173         "visible",
174         "invisible",
175         "syncDraw",
176         "flushDraw",
177         "screenUpdated",
178         "error"};
179
180     struct controller_hooks chooks;
181
182     // This is the one thing, we do not own.
183     struct wl::display *display;
184
185     std::unique_ptr<struct compositor::controller> controller;
186     std::vector<std::unique_ptr<struct wl::output>> outputs;
187
188     // track current layouts separately
189     layer_map layers;
190
191     // ID allocation and proxy methods for lookup
192     struct id_allocator id_alloc;
193
194     // Set by AFB API when wayland events need to be dispatched
195     std::atomic<bool> pending_events;
196
197     std::map<const char *, struct afb_event> map_afb_event;
198
199     // Surface are info (x, y, w, h)
200     rect_map area_info;
201
202     // FOR CES DEMO
203     std::vector<int> surface_bg;
204
205     explicit WindowManager(wl::display *d);
206     ~WindowManager() = default;
207
208     WindowManager(WindowManager const &) = delete;
209     WindowManager &operator=(WindowManager const &) = delete;
210     WindowManager(WindowManager &&) = delete;
211     WindowManager &operator=(WindowManager &&) = delete;
212
213     int init();
214     int dispatch_pending_events();
215     void set_pending_events();
216
217     result<int> api_request_surface(char const *appid, char const *role);
218     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
219     void api_activate_surface(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
220     void api_deactivate_surface(char const *appid, char const *role, const reply_func &reply);
221     void api_enddraw(char const *appid, char const *role);
222     result<json_object *> api_get_display_info();
223     result<json_object *> api_get_area_info(char const *role);
224     void api_ping();
225     void send_event(char const *evname, char const *label);
226     void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
227
228     // Events from the compositor we are interested in
229     void surface_created(uint32_t surface_id);
230     void surface_removed(uint32_t surface_id);
231
232     void removeClient(const std::string &appid);
233     void exceptionProcessForTransition();
234     const char* convertRoleOldToNew(char const *role);
235
236     // Do not use this function
237     void timerHandler();
238     void startTransitionWrapper(std::vector<WMAction> &actions);
239     void processError(WMError error);
240
241   private:
242     bool pop_pending_events();
243     optional<int> lookup_id(char const *name);
244     optional<std::string> lookup_name(int id);
245     int init_layers();
246     void surface_set_layout(int surface_id, const std::string& area = "");
247     void layout_commit();
248
249     // WM Events to clients
250     void emit_activated(char const *label);
251     void emit_deactivated(char const *label);
252     void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
253     void emit_syncdraw(const std::string &role, const std::string &area);
254     void emit_flushdraw(char const *label);
255     void emit_visible(char const *label, bool is_visible);
256     void emit_invisible(char const *label);
257     void emit_visible(char const *label);
258
259     void activate(int id);
260     void deactivate(int id);
261     WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
262                              Task task, unsigned *req_num);
263     WMError doTransition(unsigned sequence_number);
264     WMError checkPolicy(unsigned req_num);
265     WMError startTransition(unsigned req_num);
266
267     WMError doEndDraw(unsigned req_num);
268     WMError layoutChange(const WMAction &action);
269     WMError visibilityChange(const WMAction &action);
270     WMError setSurfaceSize(unsigned surface, const std::string& area);
271     void    emitScreenUpdated(unsigned req_num);
272
273     void setTimer();
274     void stopTimer();
275     void processNextRequest();
276
277     int loadOldRoleDb();
278
279     const char *check_surface_exist(const char *role);
280
281   private:
282     std::unordered_map<std::string, struct compositor::rect> area2size;
283     std::unordered_map<std::string, std::string> roleold2new;
284     std::unordered_map<std::string, std::string> rolenew2old;
285
286     PMWrapper pmw;
287
288     static const char* kDefaultOldRoleDb;
289 };
290
291 } // namespace wm
292
293 #endif // WINDOW_MANAGER_HPP