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