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