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