Change names. Not use sequence number but request number
[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
37 namespace wl
38 {
39 struct display;
40 }
41
42 namespace compositor
43 {
44 struct controller;
45 }
46
47 namespace wm
48 {
49
50 using std::experimental::optional;
51
52 /* DrawingArea name used by "{layout}.{area}" */
53 extern const char kNameLayoutNormal[];
54 extern const char kNameLayoutSplit[];
55 extern const char kNameAreaFull[];
56 extern const char kNameAreaMain[];
57 extern const char kNameAreaSub[];
58
59 /* Key for json obejct */
60 extern const char kKeyDrawingName[];
61 extern const char kKeyDrawingArea[];
62 extern const char kKeyDrawingRect[];
63 extern const char kKeyX[];
64 extern const char kKeyY[];
65 extern const char kKeyWidth[];
66 extern const char kKeyHeigh[];
67 extern const char kKeyWidthPixel[];
68 extern const char kKeyHeightPixel[];
69 extern const char kKeyWidthMm[];
70 extern const char kKeyHeightMm[];
71
72 struct id_allocator
73 {
74     unsigned next = 1;
75
76     // Surfaces that where requested but not yet created
77     std::unordered_map<unsigned, std::string> id2name;
78     // std::unordered_set<unsigned> pending_surfaces;
79     std::unordered_map<std::string, unsigned> name2id;
80
81     id_allocator(id_allocator const &) = delete;
82     id_allocator(id_allocator &&) = delete;
83     id_allocator &operator=(id_allocator const &);
84     id_allocator &operator=(id_allocator &&) = delete;
85
86     // Insert and return a new ID
87     unsigned generate_id(std::string const &name)
88     {
89         unsigned sid = this->next++;
90         this->id2name[sid] = name;
91         // this->pending_surfaces.insert({sid});
92         this->name2id[name] = sid;
93         HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
94         return sid;
95     }
96
97     // Insert a new ID which defined outside
98     void register_name_id(std::string const &name, unsigned sid)
99     {
100         this->id2name[sid] = name;
101         this->name2id[name] = sid;
102         HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
103         return;
104     }
105
106     // Lookup by ID or by name
107     optional<unsigned> lookup(std::string const &name) const
108     {
109         auto i = this->name2id.find(name);
110         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
111     }
112
113     optional<std::string> lookup(unsigned id) const
114     {
115         auto i = this->id2name.find(id);
116         return i == this->id2name.end() ? nullopt
117                                         : optional<std::string>(i->second);
118     }
119
120     // Remove a surface id and name
121     void remove_id(std::string const &name)
122     {
123         auto i = this->name2id.find(name);
124         if (i != this->name2id.end())
125         {
126             this->id2name.erase(i->second);
127             this->name2id.erase(i);
128         }
129     }
130
131     void remove_id(unsigned id)
132     {
133         auto i = this->id2name.find(id);
134         if (i != this->id2name.end())
135         {
136             this->name2id.erase(i->second);
137             this->id2name.erase(i);
138         }
139     }
140 };
141
142 struct App
143 {
144
145     typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
146     typedef std::function<void(const char *err_msg)> reply_func;
147
148     enum EventType
149     {
150         Event_Val_Min = 0,
151
152         Event_Active = Event_Val_Min,
153         Event_Inactive,
154
155         Event_Visible,
156         Event_Invisible,
157
158         Event_SyncDraw,
159         Event_FlushDraw,
160
161         Event_Error,
162
163         Event_Val_Max = Event_Error,
164     };
165
166     const std::vector<const char *> kListEventName{
167         "active",
168         "inactive",
169         "visible",
170         "invisible",
171         "syncdraw",
172         "flushdraw",
173         "error"};
174
175     struct controller_hooks chooks;
176
177     // This is the one thing, we do not own.
178     struct wl::display *display;
179
180     std::unique_ptr<struct compositor::controller> controller;
181     std::vector<std::unique_ptr<struct wl::output>> outputs;
182
183     struct config config;
184
185     // track current layouts separately
186     layer_map layers;
187
188     // ID allocation and proxy methods for lookup
189     struct id_allocator id_alloc;
190
191     // Set by AFB API when wayland events need to be dispatched
192     std::atomic<bool> pending_events;
193
194     std::vector<int> pending_end_draw;
195
196     Policy policy;
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 App(wl::display *d);
207     ~App() = default;
208
209     App(App const &) = delete;
210     App &operator=(App const &) = delete;
211     App(App &&) = delete;
212     App &operator=(App &&) = delete;
213
214     int init();
215
216     int dispatch_pending_events();
217
218     void set_pending_events();
219
220     result<int> api_request_surface(char const *appid, char const *drawing_name);
221     char const *api_request_surface(char const *appid, char const *drawing_name, char const *ivi_id);
222     void api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply);
223     void api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply);
224     void api_enddraw(char const *appid, char const *drawing_name);
225     result<json_object *> api_get_display_info();
226     result<json_object *> api_get_area_info(char const *drawing_name);
227     void api_ping();
228     void send_event(char const *evname, char const *label);
229     void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
230
231     // Events from the compositor we are interested in
232     void surface_created(uint32_t surface_id);
233     void surface_removed(uint32_t surface_id);
234
235     // Do not use this function
236     //static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata);
237     void timerHandler();
238     void removeClient(const std::string &appid);
239     bool subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname);
240
241   private:
242     optional<int> lookup_id(char const *name);
243     optional<std::string> lookup_name(int id);
244
245     bool pop_pending_events();
246
247     void enqueue_flushdraw(int surface_id);
248     void check_flushdraw(int surface_id);
249
250     int init_layers();
251
252     void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
253     void layout_commit();
254
255     // TMC WM Events to clients
256     void emit_activated(char const *label);
257     void emit_deactivated(char const *label);
258     void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
259     void emit_flushdraw(char const *label);
260     void emit_visible(char const *label, bool is_visible);
261     void emit_invisible(char const *label);
262     void emit_visible(char const *label);
263
264     bool do_transition(unsigned req_num);
265
266     void do_enddraw(unsigned req_num);
267     void process_request();
268     void set_timer();
269     void stop_timer();
270     const char *check_surface_exist(const char *drawing_name);
271
272     void activate(int id);
273     void deactivate(int id);
274     void deactivate_main_surface();
275
276     bool can_split(struct LayoutState const &state, int new_id);
277     void try_layout(struct LayoutState &state,
278                     struct LayoutState const &new_layout,
279                     std::function<void(LayoutState const &nl)> apply);
280
281     // The following function is temporary.
282     // Then will be removed when layermanager is finished
283     void lm_layout_change(const char *drawing_name);
284     bool lm_layout_change(const struct WMAction &action);
285     bool lm_release(const struct WMAction &action);
286     void lm_enddraw(const char *drawing_name);
287 };
288
289 } // namespace wm
290
291 #endif // TMCAGLWM_APP_HPP