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