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