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