moved callback thunks to impl, makes classes readable
[staging/windowmanager.git] / src / wayland.cpp
1 #include "wayland.hpp"
2
3 //                                                                  _
4 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
5 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
6 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
7 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
8 //                                 |_|
9 namespace wl {
10
11 //      _ _           _
12 //   __| (_)___ _ __ | | __ _ _   _
13 //  / _` | / __| '_ \| |/ _` | | | |
14 // | (_| | \__ \ |_) | | (_| | |_| |
15 //  \__,_|_|___/ .__/|_|\__,_|\__, |
16 //             |_|            |___/
17 display::display()
18    : d(std::unique_ptr<wl_display, void (*)(wl_display *)>(
19         wl_display_connect(NULL), wl_display_disconnect)),
20      r(std::make_unique<registry>(d.get())) {}
21
22 bool display::ok() const {
23    return d.get() != nullptr && wl_display_get_error(d.get()) == 0;
24 }
25
26 void display::roundtrip() { wl_display_roundtrip(this->d.get()); }
27
28 int display::dispatch() { return wl_display_dispatch(this->d.get()); }
29
30 //                 _     _
31 //  _ __ ___  __ _(_)___| |_ _ __ _   _
32 // | '__/ _ \/ _` | / __| __| '__| | | |
33 // | | |  __/ (_| | \__ \ |_| |  | |_| |
34 // |_|  \___|\__, |_|___/\__|_|   \__, |
35 //           |___/                |___/
36 namespace {
37 void registry_global(void *data, struct wl_registry *r, uint32_t name,
38                      char const *iface, uint32_t v) {
39    static_cast<registry *>(data)->global(name, iface, v);
40 }
41
42 void registry_global_remove(void *data, struct wl_registry *r, uint32_t name) {
43    static_cast<registry *>(data)->global_remove(name);
44 }
45
46 constexpr wl_registry_listener registry_listener = {registry_global,
47                                                     registry_global_remove};
48 }
49
50 registry::registry(wl_display *d) : wayland_proxy(wl_display_get_registry(d)) {
51    wl_registry_add_listener(this->proxy, &registry_listener, this);
52 }
53
54 registry::~registry() {
55    wl_registry_destroy(this->proxy);
56    this->proxy = nullptr;
57 }
58
59 void registry::add_global_handler(char const *iface, binder bind) {
60    this->bindings[iface] = bind;
61 }
62
63 void registry::global(uint32_t name, char const *iface, uint32_t v) {
64    auto b = this->bindings.find(iface);
65    if (b != this->bindings.end())
66       b->second(this->proxy, name, v);
67    else
68       logdebug("wl::registry @ %p global n %u i %s v %u", this->proxy, name,
69                iface, v);
70 }
71
72 void registry::global_remove(uint32_t name) {}
73
74 //              _               _
75 //   ___  _   _| |_ _ __  _   _| |_
76 //  / _ \| | | | __| '_ \| | | | __|
77 // | (_) | |_| | |_| |_) | |_| | |_
78 //  \___/ \__,_|\__| .__/ \__,_|\__|
79 //                 |_|
80 namespace {
81 void output_geometry(void *data, struct wl_output *wl_output, int32_t x,
82                      int32_t y, int32_t physical_width, int32_t physical_height,
83                      int32_t subpixel, const char *make, const char *model,
84                      int32_t transform) {
85    static_cast<output *>(data)->geometry(x, y, physical_width, physical_height,
86                                          subpixel, make, model, transform);
87 }
88
89 void output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
90                  int32_t width, int32_t height, int32_t refresh) {
91    static_cast<output *>(data)->mode(flags, width, height, refresh);
92 }
93
94 void output_done(void *data, struct wl_output *wl_output) {
95    static_cast<output *>(data)->done();
96 }
97
98 void output_scale(void *data, struct wl_output *wl_output, int32_t factor) {
99    static_cast<output *>(data)->scale(factor);
100 }
101
102 constexpr wl_output_listener output_listener = {output_geometry, output_mode,
103                                                 output_done, output_scale};
104 }
105
106 output::output(wl_registry *r, uint32_t name, uint32_t v)
107    : wayland_proxy(wl_registry_bind(r, name, &wl_output_interface, v)) {
108    wl_output_add_listener(this->proxy, &output_listener, this);
109 }
110
111 void output::geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
112                       int32_t subpel, char const *make, char const *model,
113                       int32_t tx) {
114    logdebug(
115       "wl::output %s @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
116       __func__, this->proxy, x, y, pw, ph, subpel, make, model, tx);
117 }
118
119 void output::mode(uint32_t flags, int32_t w, int32_t h, int32_t r) {
120    logdebug("wl::output %s @ %p f %x w %i h %i r %i", __func__, this->proxy,
121             flags, w, h, r);
122 }
123
124 void output::done() {
125    logdebug("wl::output %s @ %p done", __func__, this->proxy);
126 }
127
128 void output::scale(int32_t factor) {
129    logdebug("wl::output %s @ %p f %i", __func__, this->proxy, factor);
130 }
131 }
132
133 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
134 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
135 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
136 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
137 //                                 |_|
138 //                   _       _
139 //   __ _  ___ _ __ (_)_   _(_)
140 //  / _` |/ _ \ '_ \| \ \ / / |
141 // | (_| |  __/ | | | |\ V /| |
142 //  \__, |\___|_| |_|_| \_/ |_|
143 //  |___/
144 namespace genivi {
145
146 //                  _             _ _
147 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
148 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
149 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
150 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
151 //
152 namespace {
153 void controller_screen(void *data, struct ivi_controller *ivi_controller,
154                        uint32_t id_screen,
155                        struct ivi_controller_screen *screen) {
156    static_cast<controller *>(data)->screen(id_screen, screen);
157 }
158
159 void controller_layer(void *data, struct ivi_controller *ivi_controller,
160                       uint32_t id_layer) {
161    static_cast<controller *>(data)->layer(id_layer);
162 }
163
164 void controller_surface(void *data, struct ivi_controller *ivi_controller,
165                         uint32_t id_surface) {
166    static_cast<controller *>(data)->surface(id_surface);
167 }
168
169 void controller_error(void *data, struct ivi_controller *ivi_controller,
170                       int32_t object_id, int32_t object_type,
171                       int32_t error_code, const char *error_text) {
172    static_cast<controller *>(data)->error(object_id, object_type, error_code,
173                                           error_text);
174 }
175
176 constexpr ivi_controller_listener listener = {
177    controller_screen, controller_layer, controller_surface, controller_error};
178 }
179
180 controller::controller(wl_registry *r, uint32_t name, uint32_t version)
181    : wayland_proxy(
182         wl_registry_bind(r, name, &ivi_controller_interface, version)) {
183    ivi_controller_add_listener(this->proxy, &listener, this);
184 }
185
186 controller::~controller() {}
187
188 void controller::screen(uint32_t id, struct ivi_controller_screen *screen) {
189    logdebug("genivi::controller @ %p screen %u (%x) @ %p", this->proxy, id, id,
190             screen);
191    this->screens[id] = std::make_unique<struct screen>(id, screen);
192 }
193
194 void controller::layer(uint32_t id) {
195    logdebug("genivi::controller @ %p layer %u (%x)", this->proxy, id, id);
196    this->layers[id] = std::make_unique<struct layer>(id, this->proxy);
197 }
198
199 void controller::surface(uint32_t id) {
200    logdebug("genivi::controller @ %p surface %u (%x)", this->proxy, id, id);
201    this->surfaces[id] = std::make_unique<struct surface>(id, this->proxy);
202 }
203
204 void controller::error(int32_t object_id, int32_t object_type,
205                        int32_t error_code, const char *error_text) {
206    logdebug("genivi::controller @ %p error o %i t %i c %i text %s", this->proxy,
207             object_id, object_type, error_code, error_text);
208 }
209
210 //  _
211 // | | __ _ _   _  ___ _ __
212 // | |/ _` | | | |/ _ \ '__|
213 // | | (_| | |_| |  __/ |
214 // |_|\__,_|\__, |\___|_|
215 //          |___/
216 namespace {
217 void layer_visibility(void *data,
218                       struct ivi_controller_layer *ivi_controller_layer,
219                       int32_t visibility) {
220    static_cast<layer *>(data)->visibility(visibility);
221 }
222
223 void layer_opacity(void *data,
224                    struct ivi_controller_layer *ivi_controller_layer,
225                    wl_fixed_t opacity) {
226    static_cast<layer *>(data)->opacity(opacity);
227 }
228
229 void layer_source_rectangle(void *data,
230                             struct ivi_controller_layer *ivi_controller_layer,
231                             int32_t x, int32_t y, int32_t width,
232                             int32_t height) {
233    static_cast<layer *>(data)->source_rectangle(x, y, width, height);
234 }
235
236 void layer_destination_rectangle(
237    void *data, struct ivi_controller_layer *ivi_controller_layer, int32_t x,
238    int32_t y, int32_t width, int32_t height) {
239    static_cast<layer *>(data)->destination_rectangle(x, y, width, height);
240 }
241
242 void layer_configuration(void *data,
243                          struct ivi_controller_layer *ivi_controller_layer,
244                          int32_t width, int32_t height) {
245    static_cast<layer *>(data)->configuration(width, height);
246 }
247
248 void layer_orientation(void *data,
249                        struct ivi_controller_layer *ivi_controller_layer,
250                        int32_t orientation) {
251    static_cast<layer *>(data)->orientation(orientation);
252 }
253
254 void layer_screen(void *data, struct ivi_controller_layer *ivi_controller_layer,
255                   struct wl_output *screen) {
256    static_cast<layer *>(data)->screen(screen);
257 }
258
259 void layer_destroyed(void *data,
260                      struct ivi_controller_layer *ivi_controller_layer) {
261    static_cast<layer *>(data)->destroyed();
262 }
263
264 constexpr ivi_controller_layer_listener layer_listener = {
265    layer_visibility,       layer_opacity,
266    layer_source_rectangle, layer_destination_rectangle,
267    layer_configuration,    layer_orientation,
268    layer_screen,           layer_destroyed,
269 };
270 }
271
272 layer::layer(uint32_t i, ivi_controller *c)
273    : wayland_proxy(ivi_controller_layer_create(c, i, 0, 0)), id(i) {
274    ivi_controller_layer_add_listener(this->proxy, &layer_listener, this);
275 }
276
277 layer::~layer() {
278    logdebug("%s layer %i @ %p", __func__, this->id, this->proxy);
279    ivi_controller_layer_destroy(this->proxy, 1);
280    this->proxy = nullptr;
281 }
282
283 void layer::visibility(int32_t visibility) {
284    logdebug("genivi::layer %s @ %p v %i", __func__, this->proxy, visibility);
285 }
286
287 void layer::opacity(wl_fixed_t opacity) {
288    logdebug("genivi::layer %s @ %p o %i", __func__, this->proxy, opacity);
289 }
290
291 void layer::source_rectangle(int32_t x, int32_t y, int32_t width,
292                              int32_t height) {
293    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
294             x, y, width, height);
295 }
296
297 void layer::destination_rectangle(int32_t x, int32_t y, int32_t width,
298                                   int32_t height) {
299    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
300             x, y, width, height);
301 }
302
303 void layer::configuration(int32_t width, int32_t height) {
304    logdebug("genivi::layer %s @ %p w %i h %i", __func__, this->proxy, width,
305             height);
306 }
307
308 void layer::orientation(int32_t orientation) {
309    logdebug("genivi::layer %s @ %p o %i", __func__, this->proxy, orientation);
310 }
311
312 void layer::screen(struct wl_output *screen) {
313    logdebug("genivi::layer %s @ %p s %p", __func__, this->proxy, screen);
314 }
315
316 void layer::destroyed() {
317    logdebug("genivi::layer %s @ %p", __func__, this->proxy);
318 }
319
320 //                  __
321 //  ___ _   _ _ __ / _| __ _  ___ ___
322 // / __| | | | '__| |_ / _` |/ __/ _ \
323 // \__ \ |_| | |  |  _| (_| | (_|  __/
324 // |___/\__,_|_|  |_|  \__,_|\___\___|
325 //
326 namespace {
327
328 void surface_visibility(void *data,
329                         struct ivi_controller_surface *ivi_controller_surface,
330                         int32_t visibility) {
331    static_cast<surface *>(data)->visibility(visibility);
332 }
333
334 void surface_opacity(void *data,
335                      struct ivi_controller_surface *ivi_controller_surface,
336                      wl_fixed_t opacity) {
337    static_cast<surface *>(data)->opacity(opacity);
338 }
339
340 void surface_source_rectangle(
341    void *data, struct ivi_controller_surface *ivi_controller_surface, int32_t x,
342    int32_t y, int32_t width, int32_t height) {
343    static_cast<surface *>(data)->source_rectangle(x, y, width, height);
344 }
345
346 void surface_destination_rectangle(
347    void *data, struct ivi_controller_surface *ivi_controller_surface, int32_t x,
348    int32_t y, int32_t width, int32_t height) {
349    static_cast<surface *>(data)->destination_rectangle(x, y, width, height);
350 }
351
352 void surface_configuration(
353    void *data, struct ivi_controller_surface *ivi_controller_surface,
354    int32_t width, int32_t height) {
355    static_cast<surface *>(data)->configuration(width, height);
356 }
357
358 void surface_orientation(void *data,
359                          struct ivi_controller_surface *ivi_controller_surface,
360                          int32_t orientation) {
361    static_cast<surface *>(data)->orientation(orientation);
362 }
363
364 void surface_pixelformat(void *data,
365                          struct ivi_controller_surface *ivi_controller_surface,
366                          int32_t pixelformat) {
367    static_cast<surface *>(data)->pixelformat(pixelformat);
368 }
369
370 void surface_layer(void *data,
371                    struct ivi_controller_surface *ivi_controller_surface,
372                    struct ivi_controller_layer *layer) {
373    static_cast<surface *>(data)->layer(layer);
374 }
375
376 void surface_stats(void *data,
377                    struct ivi_controller_surface *ivi_controller_surface,
378                    uint32_t redraw_count, uint32_t frame_count,
379                    uint32_t update_count, uint32_t pid,
380                    const char *process_name) {
381    static_cast<surface *>(data)->stats(redraw_count, frame_count, update_count,
382                                        pid, process_name);
383 }
384
385 void surface_destroyed(void *data,
386                        struct ivi_controller_surface *ivi_controller_surface) {
387    static_cast<surface *>(data)->destroyed();
388 }
389
390 void surface_content(void *data,
391                      struct ivi_controller_surface *ivi_controller_surface,
392                      int32_t content_state) {
393    static_cast<surface *>(data)->content(content_state);
394 }
395
396 static constexpr ivi_controller_surface_listener surface_listener = {
397    surface_visibility,
398    surface_opacity,
399    surface_source_rectangle,
400    surface_destination_rectangle,
401    surface_configuration,
402    surface_orientation,
403    surface_pixelformat,
404    surface_layer,
405    surface_stats,
406    surface_destroyed,
407    surface_content,
408 };
409 }
410
411 surface::surface(uint32_t i, ivi_controller *c)
412    : wayland_proxy(ivi_controller_surface_create(c, i)), id(i) {
413    ivi_controller_surface_add_listener(this->proxy, &surface_listener, this);
414 }
415
416 surface::~surface() {
417    logdebug("%s surface %i @ %p", __func__, this->id, this->proxy);
418    ivi_controller_surface_destroy(this->proxy, 1);
419    this->proxy = nullptr;
420 }
421
422 void surface::visibility(int32_t visibility) {
423    logdebug("genivi::surface %s @ %p v %i", __func__, this->proxy, visibility);
424 }
425
426 void surface::opacity(wl_fixed_t opacity) {
427    logdebug("genivi::surface %s @ %p o %i", __func__, this->proxy, opacity);
428 }
429
430 void surface::source_rectangle(int32_t x, int32_t y, int32_t width,
431                                int32_t height) {
432    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
433             this->proxy, x, y, width, height);
434 }
435
436 void surface::destination_rectangle(int32_t x, int32_t y, int32_t width,
437                                     int32_t height) {
438    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
439             this->proxy, x, y, width, height);
440 }
441
442 void surface::configuration(int32_t width, int32_t height) {
443    logdebug("genivi::surface %s @ %p w %i h %i", __func__, this->proxy, width,
444             height);
445 }
446
447 void surface::orientation(int32_t orientation) {
448    logdebug("genivi::surface %s @ %p o %i", __func__, this->proxy, orientation);
449 }
450
451 void surface::pixelformat(int32_t pixelformat) {
452    logdebug("genivi::surface %s @ %p f %i", __func__, this->proxy, pixelformat);
453 }
454
455 void surface::layer(struct ivi_controller_layer *layer) {
456    logdebug("genivi::surface %s @ %p l @ %p", __func__, this->proxy, layer);
457 }
458
459 void surface::stats(uint32_t redraw_count, uint32_t frame_count,
460                     uint32_t update_count, uint32_t pid,
461                     const char *process_name) {
462    logdebug("genivi::surface %s @ %p r %u f %u u %u pid %u p %s", __func__,
463             this->proxy, redraw_count, frame_count, update_count, pid,
464             process_name);
465 }
466
467 void surface::destroyed() {
468    logdebug("genivi::surface %s @ %p", __func__, this->proxy);
469 }
470
471 void surface::content(int32_t content_state) {
472    logdebug("genivi::surface %s @ %p s %i", __func__, this->proxy,
473             content_state);
474 }
475
476 //
477 // ___  ___ _ __ ___  ___ _ __
478 /// __|/ __| '__/ _ \/ _ \ '_ \
479 //\__ \ (__| | |  __/  __/ | | |
480 //|___/\___|_|  \___|\___|_| |_|
481 //
482 screen::screen(uint32_t i, ivi_controller_screen *p)
483    : wayland_proxy(p), id(i) {}
484 }