9d0c7a740d1961854d194da06d6a44c28b900efa
[apps/agl-service-windowmanager-2017.git] / src / wayland_ivi_wm.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 WM_WAYLAND_HPP
18 #define WM_WAYLAND_HPP
19
20 #include "controller_hooks.hpp"
21 #include "ivi-wm-client-protocol.h"
22 #include "util.hpp"
23
24 #include <functional>
25 #include <memory>
26 #include <unordered_map>
27 #include <vector>
28
29 /**
30  * @struct wayland_proxy
31  */
32 template <typename ProxyT>
33 struct wayland_proxy
34 {
35     std::unique_ptr<ProxyT, std::function<void(ProxyT *)>> proxy;
36     wayland_proxy(wayland_proxy const &) = delete;
37     wayland_proxy &operator=(wayland_proxy const &) = delete;
38     wayland_proxy(void *p)
39         : wayland_proxy(p,
40                         reinterpret_cast<void (*)(ProxyT *)>(wl_proxy_destroy)) {}
41     wayland_proxy(void *p, std::function<void(ProxyT *)> &&p_del)
42         : proxy(std::unique_ptr<ProxyT, std::function<void(ProxyT *)>>(
43               static_cast<ProxyT *>(p), p_del)) {}
44 };
45
46 /**
47  * namespace wl
48  */
49 namespace wl
50 {
51
52 /**
53  * @struct registry
54  */
55 struct registry : public wayland_proxy<struct wl_registry>
56 {
57     typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
58     std::unordered_map<std::string, binder> bindings;
59
60     registry(registry const &) = delete;
61     registry &operator=(registry const &) = delete;
62     registry(struct wl_display *d);
63
64     void add_global_handler(char const *iface, binder bind);
65
66     // Events
67     void global_created(uint32_t name, char const *iface, uint32_t v);
68     void global_removed(uint32_t name);
69 };
70
71 /**
72  * @struct display
73  */
74 struct display
75 {
76     std::unique_ptr<struct wl_display, void (*)(struct wl_display *)> d;
77     struct registry r;
78
79     display(display const &) = delete;
80     display &operator=(display const &) = delete;
81     display();
82     bool ok() const;
83     void roundtrip();
84     int dispatch();
85     int dispatch_pending();
86     int read_events();
87     void flush();
88     int get_fd() const;
89     int get_error();
90
91     // Lets just proxy this for the registry
92     inline void add_global_handler(char const *iface, registry::binder bind)
93     {
94         this->r.add_global_handler(iface, bind);
95     }
96 };
97
98 /**
99  * @struct output
100  */
101 struct output : public wayland_proxy<struct wl_output>
102 {
103     int width{};
104     int height{};
105     int physical_width{};
106     int physical_height{};
107     int refresh{};
108     int transform{};
109
110     output(output const &) = delete;
111     output &operator=(output const &) = delete;
112     output(struct wl_registry *r, uint32_t name, uint32_t v);
113
114     // Events
115     void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
116                   char const *make, char const *model, int32_t tx);
117     void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
118     void done();
119     void scale(int32_t factor);
120 };
121 } // namespace wl
122
123 /**
124  * namespace compositor
125  */
126 namespace compositor
127 {
128
129 struct size
130 {
131     uint32_t w, h;
132 };
133
134 struct rect
135 {
136     int32_t w, h;
137     int32_t x, y;
138 };
139
140 static const constexpr rect full_rect = rect{-1, -1, 0, 0};
141
142 inline bool operator==(struct rect a, struct rect b)
143 {
144     return a.w == b.w && a.h == b.h && a.x == b.x && a.y == b.y;
145 }
146
147 struct controller;
148
149 struct controller_child
150 {
151     struct controller *parent;
152     uint32_t id;
153
154     controller_child(controller_child const &) = delete;
155     controller_child &operator=(controller_child const &) = delete;
156     controller_child(struct controller *c, uint32_t i) : parent(c), id(i) {}
157     virtual ~controller_child() {}
158 };
159
160 struct surface_properties
161 {
162     uint32_t id; // let's just save an ID here too
163     struct rect dst_rect;
164     struct rect src_rect;
165     struct size size;
166     int32_t orientation;
167     int32_t visibility;
168     float opacity;
169 };
170
171 /**
172  * @struct surface
173  */
174 struct surface : public controller_child
175 {
176     surface(surface const &) = delete;
177     surface &operator=(surface const &) = delete;
178     surface(uint32_t i, struct controller *c);
179
180     // Requests
181     void set_visibility(uint32_t visibility);
182     void set_source_rectangle(int32_t x, int32_t y,
183                               int32_t width, int32_t height);
184     void set_destination_rectangle(int32_t x, int32_t y,
185                                    int32_t width, int32_t height);
186 };
187
188 /**
189  * @struct layer
190  */
191 struct layer : public controller_child
192 {
193     layer(layer const &) = delete;
194     layer &operator=(layer const &) = delete;
195     layer(uint32_t i, struct controller *c);
196     layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
197
198     // Requests
199     void set_visibility(uint32_t visibility);
200     void set_destination_rectangle(int32_t x, int32_t y,
201                                    int32_t width, int32_t height);
202     void add_surface(uint32_t surface_id);
203     void remove_surface(uint32_t surface_id);
204 };
205
206 /**
207  * @struct screen
208  */
209 struct screen : public wayland_proxy<struct ivi_wm_screen>,
210                 public controller_child
211 {
212     screen(screen const &) = delete;
213     screen &operator=(screen const &) = delete;
214     screen(uint32_t i, struct controller *c, struct wl_output *o);
215
216     void clear();
217     void screen_created(struct screen *screen, uint32_t id);
218     void set_render_order(std::vector<uint32_t> const &ro);
219 };
220
221 /**
222  * @struct controller
223  */
224 struct controller : public wayland_proxy<struct ivi_wm>
225 {
226     // This controller is still missing ivi-input
227
228     typedef std::unordered_map<uintptr_t, uint32_t> proxy_to_id_map_type;
229     typedef std::unordered_map<uint32_t, std::unique_ptr<struct surface>>
230         surface_map_type;
231     typedef std::unordered_map<uint32_t, std::unique_ptr<struct layer>>
232         layer_map_type;
233     typedef std::unordered_map<uint32_t, struct screen *> screen_map_type;
234     typedef std::unordered_map<uint32_t, struct surface_properties> props_map;
235
236     // HACK:
237     // The order of these member is mandatory, as when objects are destroyed
238     // they will call their parent (that's us right here!) and remove their
239     // proxy-to-id mapping. I.e. the *_proxy_to_id members need to be valid
240     // when the surfaces/layers/screens maps are destroyed. This sucks, but
241     // I cannot see a better solution w/o globals or some other horrible
242     // call-our-parent construct.
243     proxy_to_id_map_type surface_proxy_to_id;
244     proxy_to_id_map_type layer_proxy_to_id;
245     proxy_to_id_map_type screen_proxy_to_id;
246
247     props_map sprops;
248     props_map lprops;
249
250     surface_map_type surfaces;
251     layer_map_type layers;
252     screen_map_type screens;
253
254     std::unique_ptr<struct screen> screen;
255
256     size output_size;   // Display size[pixel]
257     size physical_size; // Display size[mm]
258
259     wm::controller_hooks *chooks;
260
261     struct wl::display *display;
262
263     void add_proxy_to_sid_mapping(struct ivi_wm *p, uint32_t id);
264     void remove_proxy_to_sid_mapping(struct ivi_wm *p);
265
266     void add_proxy_to_lid_mapping(struct ivi_wm *p, uint32_t id);
267     void remove_proxy_to_lid_mapping(struct ivi_wm *p);
268
269     void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
270     void remove_proxy_to_id_mapping(struct wl_output *p);
271
272     bool surface_exists(uint32_t id) const
273     {
274         return this->surfaces.find(id) != this->surfaces.end();
275     }
276
277     bool layer_exists(uint32_t id) const
278     {
279         return this->layers.find(id) != this->layers.end();
280     }
281
282     controller(struct wl_registry *r, uint32_t name, uint32_t version);
283
284     // Requests
285     void commit_changes() const
286     {
287         ivi_wm_commit_changes(this->proxy.get());
288     }
289     void layer_create(uint32_t id, int32_t w, int32_t h);
290     void surface_create(uint32_t id);
291     void create_screen(struct wl_output *output);
292
293     // Events
294     void surface_visibility_changed(uint32_t id, int32_t visibility);
295     void surface_opacity_changed(uint32_t id, float opacity);
296     void surface_source_rectangle_changed(uint32_t id, int32_t x, int32_t y,
297                                           int32_t width, int32_t height);
298     void surface_destination_rectangle_changed(uint32_t id, int32_t x, int32_t y,
299                                                int32_t width, int32_t height);
300     void surface_created(uint32_t id);
301     void surface_destroyed(uint32_t surface_id);
302     void surface_error_detected(uint32_t object_id,
303                                 uint32_t error_code, char const *error_text);
304     void surface_size_changed(uint32_t id, int32_t width, int32_t height);
305     void surface_stats_received(uint32_t surface_id,
306                                 uint32_t frame_count, uint32_t pid);
307     void surface_added_to_layer(uint32_t layer_id, uint32_t surface_id);
308
309     void layer_visibility_changed(uint32_t layer_id, int32_t visibility);
310     void layer_opacity_changed(uint32_t layer_id, float opacity);
311     void layer_source_rectangle_changed(uint32_t layer_id, int32_t x, int32_t y,
312                                         int32_t width, int32_t height);
313     void layer_destination_rectangle_changed(uint32_t layer_id, int32_t x, int32_t y,
314                                              int32_t width, int32_t height);
315     void layer_created(uint32_t id);
316     void layer_destroyed(uint32_t layer_id);
317     void layer_error_detected(uint32_t object_id,
318                               uint32_t error_code, char const *error_text);
319 };
320 } // namespace compositor
321
322 #endif // !WM_WAYLAND_HPP