54d0bd5268f8379e4ccecfedf8b88e37319cd354
[staging/windowmanager.git] / src / wayland.hpp
1 #ifndef WM_WAYLAND_HPP
2 #define WM_WAYLAND_HPP
3
4 #include "ivi-controller-client-protocol.h"
5
6 #include "util.h"
7
8 #include <functional>
9 #include <map>
10 #include <memory>
11
12 //                      _                 _
13 // __      ____ _ _   _| | __ _ _ __   __| |     _ __  _ __ _____  ___   _
14 // \ \ /\ / / _` | | | | |/ _` | '_ \ / _` |    | '_ \| '__/ _ \ \/ / | | |
15 //  \ V  V / (_| | |_| | | (_| | | | | (_| |    | |_) | | | (_) >  <| |_| |
16 //   \_/\_/ \__,_|\__, |_|\__,_|_| |_|\__,_|____| .__/|_|  \___/_/\_\\__, |
17 //                |___/                   |_____|_|                  |___/
18 template <typename ProxyT>
19 struct wayland_proxy {
20    ProxyT *proxy;
21
22    wayland_proxy(void *p) : proxy(static_cast<ProxyT *>(p)) {}
23
24    virtual ~wayland_proxy() {
25       // If this is the nullptr, then it means it already was destroyed by a
26       // custom wayland dtor
27       if (this->proxy) {
28          logdebug("%s %p @ %p", __func__, this, this->proxy);
29          wl_proxy_destroy(reinterpret_cast<wl_proxy *>(proxy));
30       }
31    }
32 };
33
34 //                                                                  _
35 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
36 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
37 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
38 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
39 //                                 |_|
40 namespace wl {
41 struct registry;
42
43 //      _ _           _
44 //   __| (_)___ _ __ | | __ _ _   _
45 //  / _` | / __| '_ \| |/ _` | | | |
46 // | (_| | \__ \ |_) | | (_| | |_| |
47 //  \__,_|_|___/ .__/|_|\__,_|\__, |
48 //             |_|            |___/
49 struct display {
50    std::unique_ptr<wl_display, void (*)(wl_display *)> d;
51    std::unique_ptr<registry> r;
52
53    display();
54    bool ok() const;
55    void roundtrip();
56 };
57
58 //                 _     _
59 //  _ __ ___  __ _(_)___| |_ _ __ _   _
60 // | '__/ _ \/ _` | / __| __| '__| | | |
61 // | | |  __/ (_| | \__ \ |_| |  | |_| |
62 // |_|  \___|\__, |_|___/\__|_|   \__, |
63 //           |___/                |___/
64 struct registry : public wayland_proxy<wl_registry> {
65    typedef std::function<void(wl_registry *, uint32_t, uint32_t)> binder;
66    std::map<std::string, binder> bindings;
67
68    registry(wl_display *d);
69    ~registry();
70
71    void add_global_handler(char const *iface, binder bind);
72
73    void event_global(uint32_t name, char const *iface, uint32_t v);
74    void event_global_remove(uint32_t name);
75
76    static void s_global(void *data, struct wl_registry *r, uint32_t name,
77                         char const *iface, uint32_t v);
78
79    static void s_global_remove(void *data, struct wl_registry *r,
80                                uint32_t name);
81
82    static constexpr wl_registry_listener listener = {s_global, s_global_remove};
83 };
84
85 //              _               _
86 //   ___  _   _| |_ _ __  _   _| |_
87 //  / _ \| | | | __| '_ \| | | | __|
88 // | (_) | |_| | |_| |_) | |_| | |_
89 //  \___/ \__,_|\__| .__/ \__,_|\__|
90 //                 |_|
91 struct output : wayland_proxy<wl_output> {
92    output(wl_registry *registry, uint32_t name, uint32_t version);
93
94    void event_geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
95                        int32_t subpel, char const *make, char const *model,
96                        int32_t tx);
97    void event_mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
98    void event_done();
99    void event_scale(int32_t factor);
100
101    static void s_geometry(void *data, struct wl_output *wl_output, int32_t x,
102                           int32_t y, int32_t physical_width,
103                           int32_t physical_height, int32_t subpixel,
104                           const char *make, const char *model,
105                           int32_t transform);
106
107    static void s_mode(void *data, struct wl_output *wl_output, uint32_t flags,
108                       int32_t width, int32_t height, int32_t refresh);
109
110    static void s_done(void *data, struct wl_output *wl_output);
111
112    static void s_scale(void *data, struct wl_output *wl_output, int32_t factor);
113
114    static constexpr wl_output_listener listener = {s_geometry, s_mode, s_done,
115                                                    s_scale};
116 };
117 }
118
119 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
120 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
121 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
122 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
123 //                                 |_|
124 //                   _       _
125 //   __ _  ___ _ __ (_)_   _(_)
126 //  / _` |/ _ \ '_ \| \ \ / / |
127 // | (_| |  __/ | | | |\ V /| |
128 //  \__, |\___|_| |_|_| \_/ |_|
129 //  |___/
130 namespace genivi {
131
132 //                  __
133 //  ___ _   _ _ __ / _| __ _  ___ ___
134 // / __| | | | '__| |_ / _` |/ __/ _ \
135 // \__ \ |_| | |  |  _| (_| | (_|  __/
136 // |___/\__,_|_|  |_|  \__,_|\___\___|
137 //
138 struct surface : public wayland_proxy<ivi_controller_surface> {
139    uint32_t id;
140
141    surface(uint32_t i, ivi_controller *c);
142    ~surface() override;
143
144    void event_visibility(int32_t visibility);
145    void event_opacity(wl_fixed_t opacity);
146    void event_source_rectangle(int32_t x, int32_t y, int32_t width,
147                                int32_t height);
148    void event_destination_rectangle(int32_t x, int32_t y, int32_t width,
149                                     int32_t height);
150    void event_configuration(int32_t width, int32_t height);
151    void event_orientation(int32_t orientation);
152    void event_pixelformat(int32_t pixelformat);
153    void event_layer(struct ivi_controller_layer *layer);
154    void event_stats(uint32_t redraw_count, uint32_t frame_count,
155                     uint32_t update_count, uint32_t pid,
156                     const char *process_name);
157    void event_destroyed();
158    void event_content(int32_t content_state);
159
160    static void s_visibility(
161       void *data, struct ivi_controller_surface *ivi_controller_surface,
162       int32_t visibility);
163    static void s_opacity(void *data,
164                          struct ivi_controller_surface *ivi_controller_surface,
165                          wl_fixed_t opacity);
166    static void s_source_rectangle(
167       void *data, struct ivi_controller_surface *ivi_controller_surface,
168       int32_t x, int32_t y, int32_t width, int32_t height);
169    static void s_destination_rectangle(
170       void *data, struct ivi_controller_surface *ivi_controller_surface,
171       int32_t x, int32_t y, int32_t width, int32_t height);
172    static void s_configuration(
173       void *data, struct ivi_controller_surface *ivi_controller_surface,
174       int32_t width, int32_t height);
175    static void s_orientation(
176       void *data, struct ivi_controller_surface *ivi_controller_surface,
177       int32_t orientation);
178    static void s_pixelformat(
179       void *data, struct ivi_controller_surface *ivi_controller_surface,
180       int32_t pixelformat);
181    static void s_layer(void *data,
182                        struct ivi_controller_surface *ivi_controller_surface,
183                        struct ivi_controller_layer *layer);
184    static void s_stats(void *data,
185                        struct ivi_controller_surface *ivi_controller_surface,
186                        uint32_t redraw_count, uint32_t frame_count,
187                        uint32_t update_count, uint32_t pid,
188                        const char *process_name);
189    static void s_destroyed(
190       void *data, struct ivi_controller_surface *ivi_controller_surface);
191    static void s_content(void *data,
192                          struct ivi_controller_surface *ivi_controller_surface,
193                          int32_t content_state);
194
195    static constexpr ivi_controller_surface_listener listener = {
196       s_visibility,
197       s_opacity,
198       s_source_rectangle,
199       s_destination_rectangle,
200       s_configuration,
201       s_orientation,
202       s_pixelformat,
203       s_layer,
204       s_stats,
205       s_destroyed,
206       s_content,
207    };
208 };
209
210 //  _
211 // | | __ _ _   _  ___ _ __
212 // | |/ _` | | | |/ _ \ '__|
213 // | | (_| | |_| |  __/ |
214 // |_|\__,_|\__, |\___|_|
215 //          |___/
216 struct layer : public wayland_proxy<ivi_controller_layer> {
217    uint32_t id;
218
219    layer(uint32_t i, ivi_controller *c);
220    ~layer() override;
221
222    void event_visibility(int32_t visibility);
223    void event_opacity(wl_fixed_t opacity);
224    void event_source_rectangle(int32_t x, int32_t y, int32_t width,
225                                int32_t height);
226    void event_destination_rectangle(int32_t x, int32_t y, int32_t width,
227                                     int32_t height);
228    void event_configuration(int32_t width, int32_t height);
229    void event_orientation(int32_t orientation);
230    void event_screen(struct wl_output *screen);
231    void event_destroyed();
232
233    static void s_visibility(void *data,
234                             struct ivi_controller_layer *ivi_controller_layer,
235                             int32_t visibility);
236    static void s_opacity(void *data,
237                          struct ivi_controller_layer *ivi_controller_layer,
238                          wl_fixed_t opacity);
239    static void s_source_rectangle(
240       void *data, struct ivi_controller_layer *ivi_controller_layer, int32_t x,
241       int32_t y, int32_t width, int32_t height);
242    static void s_destination_rectangle(
243       void *data, struct ivi_controller_layer *ivi_controller_layer, int32_t x,
244       int32_t y, int32_t width, int32_t height);
245    static void s_configuration(
246       void *data, struct ivi_controller_layer *ivi_controller_layer,
247       int32_t width, int32_t height);
248    static void s_orientation(void *data,
249                              struct ivi_controller_layer *ivi_controller_layer,
250                              int32_t orientation);
251    static void s_screen(void *data,
252                         struct ivi_controller_layer *ivi_controller_layer,
253                         struct wl_output *screen);
254    static void s_destroyed(void *data,
255                            struct ivi_controller_layer *ivi_controller_layer);
256
257    static constexpr ivi_controller_layer_listener listener = {
258       s_visibility,       s_opacity,
259       s_source_rectangle, s_destination_rectangle,
260       s_configuration,    s_orientation,
261       s_screen,           s_destroyed,
262    };
263 };
264
265 //
266 // ___  ___ _ __ ___  ___ _ __
267 /// __|/ __| '__/ _ \/ _ \ '_ \
268 //\__ \ (__| | |  __/  __/ | | |
269 //|___/\___|_|  \___|\___|_| |_|
270 //
271 struct screen : public wayland_proxy<ivi_controller_screen> {
272    uint32_t id;
273
274    screen(uint32_t i, ivi_controller_screen *p);
275 };
276
277 //                  _             _ _
278 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
279 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
280 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
281 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
282 //
283 struct controller : public wayland_proxy<ivi_controller> {
284    std::map<uint32_t, std::unique_ptr<surface>> surfaces;
285    std::map<uint32_t, std::unique_ptr<layer>> layers;
286    std::map<uint32_t, std::unique_ptr<screen>> screens;
287
288    controller(wl_registry *r, uint32_t name, uint32_t version);
289    ~controller() override;
290
291    void event_screen(uint32_t id, ivi_controller_screen *screen);
292    void event_layer(uint32_t id);
293    void event_surface(uint32_t id);
294    void event_error(int32_t oid, int32_t otype, int32_t code, char const *text);
295
296    // thunks...
297    static void s_screen(void *data, struct ivi_controller *ivi_controller,
298                         uint32_t id_screen,
299                         struct ivi_controller_screen *screen);
300
301    static void s_layer(void *data, struct ivi_controller *ivi_controller,
302                        uint32_t id_layer);
303
304    static void s_surface(void *data, struct ivi_controller *ivi_controller,
305                          uint32_t id_surface);
306
307    static void s_error(void *data, struct ivi_controller *ivi_controller,
308                        int32_t object_id, int32_t object_type,
309                        int32_t error_code, const char *error_text);
310
311    static constexpr ivi_controller_listener listener = {s_screen, s_layer,
312                                                         s_surface, s_error};
313 };
314 }
315
316 #endif  // !WM_WAYLAND_HPP