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