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