Clean: remove useless headers
[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 "config.hpp"
26 #include "controller_hooks.hpp"
27 #include "layers.hpp"
28 #include "layout.hpp"
29 #include "policy.hpp"
30 #include "wayland_ivi_wm.hpp"
31 #include "hmi-debug.h"
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_set<unsigned> pending_surfaces;
75     std::unordered_map<std::string, unsigned> name2id;
76
77     id_allocator(id_allocator const &) = delete;
78     id_allocator(id_allocator &&) = delete;
79     id_allocator &operator=(id_allocator const &);
80     id_allocator &operator=(id_allocator &&) = delete;
81
82     // Insert and return a new ID
83     unsigned generate_id(std::string const &name)
84     {
85         unsigned sid = this->next++;
86         this->id2name[sid] = name;
87         // this->pending_surfaces.insert({sid});
88         this->name2id[name] = sid;
89         HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
90         return sid;
91     }
92
93     // Insert a new ID which defined outside
94     void register_name_id(std::string const &name, unsigned sid)
95     {
96         this->id2name[sid] = name;
97         this->name2id[name] = sid;
98         HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
99         return;
100     }
101
102     // Lookup by ID or by name
103     optional<unsigned> lookup(std::string const &name) const
104     {
105         auto i = this->name2id.find(name);
106         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
107     }
108
109     optional<std::string> lookup(unsigned id) const
110     {
111         auto i = this->id2name.find(id);
112         return i == this->id2name.end() ? nullopt
113                                         : optional<std::string>(i->second);
114     }
115
116     // Remove a surface id and name
117     void remove_id(std::string const &name)
118     {
119         auto i = this->name2id.find(name);
120         if (i != this->name2id.end())
121         {
122             this->id2name.erase(i->second);
123             this->name2id.erase(i);
124         }
125     }
126
127     void remove_id(unsigned id)
128     {
129         auto i = this->id2name.find(id);
130         if (i != this->id2name.end())
131         {
132             this->name2id.erase(i->second);
133             this->id2name.erase(i);
134         }
135     }
136 };
137
138 struct App
139 {
140
141     typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
142     typedef std::function<void(const char *err_msg)> reply_func;
143
144     enum EventType
145     {
146         Event_Val_Min = 0,
147
148         Event_Active = Event_Val_Min,
149         Event_Inactive,
150
151         Event_Visible,
152         Event_Invisible,
153
154         Event_SyncDraw,
155         Event_FlushDraw,
156
157         Event_Val_Max = Event_FlushDraw,
158     };
159
160     const std::vector<const char *> kListEventName{
161         "active",
162         "inactive",
163         "visible",
164         "invisible",
165         "syncdraw",
166         "flushdraw"};
167
168     struct controller_hooks chooks;
169
170     // This is the one thing, we do not own.
171     struct wl::display *display;
172
173     std::unique_ptr<struct compositor::controller> controller;
174     std::vector<std::unique_ptr<struct wl::output>> outputs;
175
176     struct config config;
177
178     // track current layouts separately
179     layer_map layers;
180
181     // ID allocation and proxy methods for lookup
182     struct id_allocator id_alloc;
183
184     // Set by AFB API when wayland events need to be dispatched
185     std::atomic<bool> pending_events;
186
187     std::vector<int> pending_end_draw;
188
189     Policy policy;
190
191     std::map<const char *, struct afb_event> map_afb_event;
192
193     // Surface are info (x, y, w, h)
194     rect_map area_info;
195
196     // FOR CES DEMO
197     std::vector<int> surface_bg;
198
199     explicit App(wl::display *d);
200     ~App() = default;
201
202     App(App const &) = delete;
203     App &operator=(App const &) = delete;
204     App(App &&) = delete;
205     App &operator=(App &&) = delete;
206
207     int init();
208
209     int dispatch_pending_events();
210
211     void set_pending_events();
212
213     result<int> api_request_surface(char const *drawing_name);
214     char const *api_request_surface(char const *drawing_name, char const *ivi_id);
215     void api_activate_surface(char const *drawing_name, char const *drawing_area, const reply_func &reply);
216     void api_deactivate_surface(char const *drawing_name, const reply_func &reply);
217     void api_enddraw(char const *drawing_name);
218     result<json_object *> api_get_display_info();
219     result<json_object *> api_get_area_info(char const *drawing_name);
220     void api_ping();
221     void send_event(char const *evname, char const *label);
222     void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
223
224     // Events from the compositor we are interested in
225     void surface_created(uint32_t surface_id);
226     void surface_removed(uint32_t surface_id);
227
228   private:
229     optional<int> lookup_id(char const *name);
230     optional<std::string> lookup_name(int id);
231
232     bool pop_pending_events();
233
234     void enqueue_flushdraw(int surface_id);
235     void check_flushdraw(int surface_id);
236
237     int init_layers();
238
239     void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
240     void layout_commit();
241
242     // TMC WM Events to clients
243     void emit_activated(char const *label);
244     void emit_deactivated(char const *label);
245     void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
246     void emit_flushdraw(char const *label);
247     void emit_visible(char const *label, bool is_visible);
248     void emit_invisible(char const *label);
249     void emit_visible(char const *label);
250
251     void activate(int id);
252     void deactivate(int id);
253     void deactivate_main_surface();
254
255     bool can_split(struct LayoutState const &state, int new_id);
256     void try_layout(struct LayoutState &state,
257                     struct LayoutState const &new_layout,
258                     std::function<void(LayoutState const &nl)> apply);
259 };
260
261 } // namespace wm
262
263 #endif // TMCAGLWM_APP_HPP