Rename files to use "_" instead of "-"
[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 class AppList;
74
75 struct id_allocator
76 {
77     unsigned next = 1;
78
79     // Surfaces that where requested but not yet created
80     std::unordered_map<unsigned, std::string> id2name;
81     // std::unordered_set<unsigned> pending_surfaces;
82     std::unordered_map<std::string, unsigned> name2id;
83
84     id_allocator(id_allocator const &) = delete;
85     id_allocator(id_allocator &&) = delete;
86     id_allocator &operator=(id_allocator const &);
87     id_allocator &operator=(id_allocator &&) = delete;
88
89     // Insert and return a new ID
90     unsigned generate_id(std::string const &name)
91     {
92         unsigned sid = this->next++;
93         this->id2name[sid] = name;
94         // this->pending_surfaces.insert({sid});
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 struct App
146 {
147
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_Error,
165
166         Event_Val_Max = Event_Error,
167     };
168
169     const std::vector<const char *> kListEventName{
170         "active",
171         "inactive",
172         "visible",
173         "invisible",
174         "syncdraw",
175         "flushdraw",
176         "error"};
177
178     struct controller_hooks chooks;
179
180     // This is the one thing, we do not own.
181     struct wl::display *display;
182
183     std::unique_ptr<struct compositor::controller> controller;
184     std::vector<std::unique_ptr<struct wl::output>> outputs;
185
186     struct config config;
187
188     // track current layouts separately
189     layer_map layers;
190
191     // ID allocation and proxy methods for lookup
192     struct id_allocator id_alloc;
193
194     // Set by AFB API when wayland events need to be dispatched
195     std::atomic<bool> pending_events;
196
197     std::vector<int> pending_end_draw;
198
199     Policy policy;
200
201     std::map<const char *, struct afb_event> map_afb_event;
202
203     // Surface are info (x, y, w, h)
204     rect_map area_info;
205
206     // FOR CES DEMO
207     std::vector<int> surface_bg;
208
209     explicit App(wl::display *d);
210     ~App()/*  = default */;
211
212     App(App const &) = delete;
213     App &operator=(App const &) = delete;
214     App(App &&) = delete;
215     App &operator=(App &&) = delete;
216
217     int init();
218
219     int dispatch_pending_events();
220
221     void set_pending_events();
222
223     result<int> api_request_surface(char const *appid, char const *drawing_name);
224     char const *api_request_surface(char const *appid, char const *drawing_name, char const *ivi_id);
225     bool api_set_role(char const *appid, char const *drawing_name, unsigned pid);
226     void api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply);
227     void api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply);
228     void api_enddraw(char const *appid, char const *drawing_name);
229     result<json_object *> api_get_display_info();
230     result<json_object *> api_get_area_info(char const *drawing_name);
231     void api_ping();
232     void send_event(char const *evname, char const *label);
233     void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
234
235     // Events from the compositor we are interested in
236     void surface_created(uint32_t surface_id);
237     void surface_removed(uint32_t surface_id);
238     void surface_properties(uint32_t surface_id, uint32_t pid);
239
240     // Do not use this function
241     //static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata);
242     void timerHandler();
243     void removeClient(const std::string &appid);
244     bool subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname);
245
246   private:
247     optional<int> lookup_id(char const *name);
248     optional<std::string> lookup_name(int id);
249
250     bool pop_pending_events();
251
252     void enqueue_flushdraw(int surface_id);
253     void check_flushdraw(int surface_id);
254
255     int init_layers();
256
257     void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
258     void layout_commit();
259
260     // TMC WM Events to clients
261     void emit_activated(char const *label);
262     void emit_deactivated(char const *label);
263     void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
264     void emit_flushdraw(char const *label);
265     void emit_visible(char const *label, bool is_visible);
266     void emit_invisible(char const *label);
267     void emit_visible(char const *label);
268
269     WMError do_transition(unsigned sequence_number);
270
271     void do_enddraw(unsigned req_num);
272     void process_request();
273     void set_timer();
274     void stop_timer();
275     const char *check_surface_exist(const char *drawing_name);
276
277     void activate(int id);
278     void deactivate(int id);
279     void deactivate_main_surface();
280
281     bool can_split(struct LayoutState const &state, int new_id);
282     void try_layout(struct LayoutState &state,
283                     struct LayoutState const &new_layout,
284                     std::function<void(LayoutState const &nl)> apply);
285
286     // The following function is temporary.
287     // Then will be removed when layermanager is finished
288     void lm_layout_change(const char *drawing_name);
289     WMError lm_layout_change(const struct WMAction &action);
290     WMError lm_release(const struct WMAction &action);
291     void lm_enddraw(const char *drawing_name);
292
293   private:
294     std::unique_ptr<wm::AppList> app_list;
295 };
296
297 } // namespace wm
298
299 #endif // TMCAGLWM_APP_HPP