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