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