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