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