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