wayland: unique_ptr'd wayland_proxy
[staging/windowmanager.git] / src / wayland.cpp
1 #include <utility>
2
3 #include "wayland.hpp"
4
5 //                                                                  _
6 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
7 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
8 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
9 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
10 //                                 |_|
11 namespace wl {
12
13 //      _ _           _
14 //   __| (_)___ _ __ | | __ _ _   _
15 //  / _` | / __| '_ \| |/ _` | | | |
16 // | (_| | \__ \ |_) | | (_| | |_| |
17 //  \__,_|_|___/ .__/|_|\__,_|\__, |
18 //             |_|            |___/
19 display::display()
20    : d(std::unique_ptr<struct wl_display, void (*)(struct wl_display *)>(
21         wl_display_connect(nullptr),
22 #ifdef DEBUG_OUTPUT
23         [](struct wl_display *d) {
24            logdebug("wl::display ~display @ %p", d);
25            wl_display_disconnect(d);
26         })),
27 #else
28         &wl_display_disconnect)),
29 #endif
30      r(d.get()) {
31 }
32
33 bool display::ok() const { return d && wl_display_get_error(d.get()) == 0; }
34
35 void display::roundtrip() { wl_display_roundtrip(this->d.get()); }
36
37 int display::dispatch() { return wl_display_dispatch(this->d.get()); }
38
39 void display::flush() { wl_display_flush(this->d.get()); }
40
41 int display::get_fd() const { return wl_display_get_fd(this->d.get()); }
42
43 //                 _     _
44 //  _ __ ___  __ _(_)___| |_ _ __ _   _
45 // | '__/ _ \/ _` | / __| __| '__| | | |
46 // | | |  __/ (_| | \__ \ |_| |  | |_| |
47 // |_|  \___|\__, |_|___/\__|_|   \__, |
48 //           |___/                |___/
49 namespace {
50 void registry_global(void *data, struct wl_registry * /*r*/, uint32_t name,
51                      char const *iface, uint32_t v) {
52    static_cast<struct registry *>(data)->global(name, iface, v);
53 }
54
55 void registry_global_remove(void *data, struct wl_registry * /*r*/,
56                             uint32_t name) {
57    static_cast<struct registry *>(data)->global_remove(name);
58 }
59
60 constexpr struct wl_registry_listener registry_listener = {
61    registry_global, registry_global_remove};
62 }  // namespace
63
64 registry::registry(struct wl_display *d)
65    : wayland_proxy(d == nullptr ? nullptr : wl_display_get_registry(d)) {
66    if (this->proxy != nullptr) {
67       wl_registry_add_listener(this->proxy.get(), &registry_listener, this);
68    }
69 }
70
71 void registry::add_global_handler(char const *iface, binder bind) {
72    this->bindings[iface] = std::move(bind);
73 }
74
75 void registry::global(uint32_t name, char const *iface, uint32_t v) {
76    auto b = this->bindings.find(iface);
77    if (b != this->bindings.end()) {
78       b->second(this->proxy.get(), name, v);
79    }
80    logdebug("wl::registry @ %p global n %u i %s v %u", this->proxy.get(), name,
81             iface, v);
82 }
83
84 void registry::global_remove(uint32_t /*name*/) {}
85
86 //              _               _
87 //   ___  _   _| |_ _ __  _   _| |_
88 //  / _ \| | | | __| '_ \| | | | __|
89 // | (_) | |_| | |_| |_) | |_| | |_
90 //  \___/ \__,_|\__| .__/ \__,_|\__|
91 //                 |_|
92 namespace {
93 void output_geometry(void *data, struct wl_output * /*wl_output*/, int32_t x,
94                      int32_t y, int32_t physical_width, int32_t physical_height,
95                      int32_t subpixel, const char *make, const char *model,
96                      int32_t transform) {
97    static_cast<struct output *>(data)->geometry(
98       x, y, physical_width, physical_height, subpixel, make, model, transform);
99 }
100
101 void output_mode(void *data, struct wl_output * /*wl_output*/, uint32_t flags,
102                  int32_t width, int32_t height, int32_t refresh) {
103    static_cast<struct output *>(data)->mode(flags, width, height, refresh);
104 }
105
106 void output_done(void *data, struct wl_output * /*wl_output*/) {
107    static_cast<struct output *>(data)->done();
108 }
109
110 void output_scale(void *data, struct wl_output * /*wl_output*/,
111                   int32_t factor) {
112    static_cast<struct output *>(data)->scale(factor);
113 }
114
115 constexpr struct wl_output_listener output_listener = {
116    output_geometry, output_mode, output_done, output_scale};
117 }  // namespace
118
119 output::output(struct wl_registry *r, uint32_t name, uint32_t v)
120    : wayland_proxy(wl_registry_bind(r, name, &wl_output_interface, v)) {
121    wl_output_add_listener(this->proxy.get(), &output_listener, this);
122 }
123
124 void output::geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
125                       int32_t subpel, char const *make, char const *model,
126                       int32_t tx) {
127    logdebug(
128       "wl::output %s @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
129       __func__, this->proxy.get(), x, y, pw, ph, subpel, make, model, tx);
130 }
131
132 void output::mode(uint32_t flags, int32_t w, int32_t h, int32_t r) {
133    logdebug("wl::output %s @ %p f %x w %i h %i r %i", __func__,
134             this->proxy.get(), flags, w, h, r);
135    if ((flags & WL_OUTPUT_MODE_CURRENT) != 0u) {
136       this->width = w;
137       this->height = h;
138       this->refresh = r;
139    }
140 }
141
142 void output::done() {
143    logdebug("wl::output %s @ %p done", __func__, this->proxy.get());
144 }
145
146 void output::scale(int32_t factor) {
147    logdebug("wl::output %s @ %p f %i", __func__, this->proxy.get(), factor);
148 }
149 }  // namespace wl
150
151 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
152 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
153 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
154 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
155 //                                 |_|
156 //                   _       _
157 //   __ _  ___ _ __ (_)_   _(_)
158 //  / _` |/ _ \ '_ \| \ \ / / |
159 // | (_| |  __/ | | | |\ V /| |
160 //  \__, |\___|_| |_|_| \_/ |_|
161 //  |___/
162 namespace genivi {
163
164 //                  _             _ _
165 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
166 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
167 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
168 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
169 //
170 namespace {
171 void controller_screen(void *data, struct ivi_controller * /*ivi_controller*/,
172                        uint32_t id_screen,
173                        struct ivi_controller_screen *screen) {
174    static_cast<struct controller *>(data)->controller_screen(id_screen, screen);
175 }
176
177 void controller_layer(void *data, struct ivi_controller * /*ivi_controller*/,
178                       uint32_t id_layer) {
179    static_cast<struct controller *>(data)->controller_layer(id_layer);
180 }
181
182 void controller_surface(void *data, struct ivi_controller * /*ivi_controller*/,
183                         uint32_t id_surface) {
184    static_cast<struct controller *>(data)->controller_surface(id_surface);
185 }
186
187 void controller_error(void *data, struct ivi_controller * /*ivi_controller*/,
188                       int32_t object_id, int32_t object_type,
189                       int32_t error_code, const char *error_text) {
190    static_cast<struct controller *>(data)->controller_error(
191       object_id, object_type, error_code, error_text);
192 }
193
194 constexpr struct ivi_controller_listener listener = {
195    controller_screen, controller_layer, controller_surface, controller_error};
196 }  // namespace
197
198 controller::controller(struct wl_registry *r, uint32_t name, uint32_t version)
199    : wayland_proxy(
200         wl_registry_bind(r, name, &ivi_controller_interface, version)),
201      surface_proxy_to_id{},
202      layer_proxy_to_id{},
203      screen_proxy_to_id{},
204      surfaces{},
205      layers{},
206      screens{},
207      pending{},
208      output_size{} {
209    ivi_controller_add_listener(this->proxy.get(), &listener, this);
210 }
211
212 void controller::layer_create(uint32_t id, int32_t w, int32_t h) {
213    this->layers[id] = std::make_unique<layer>(id, w, h, this);
214 }
215
216 void controller::surface_create(uint32_t id) {
217    this->surfaces[id] = std::make_unique<surface>(id, this);
218 }
219
220 void controller::controller_screen(uint32_t id,
221                                    struct ivi_controller_screen *screen) {
222    logdebug("genivi::controller @ %p screen %u (%x) @ %p", this->proxy.get(),
223             id, id, screen);
224    this->screens[id] = std::make_unique<struct screen>(id, this, screen);
225 }
226
227 void controller::controller_layer(uint32_t id) {
228    logdebug("genivi::controller @ %p layer %u (%x)", this->proxy.get(), id, id);
229    auto &l = this->layers[id] = std::make_unique<struct layer>(id, this);
230    l->clear_surfaces();
231 }
232
233 void controller::controller_surface(uint32_t id) {
234    logdebug("genivi::controller @ %p surface %u (%x)", this->proxy.get(), id,
235             id);
236    this->surfaces[id] = std::make_unique<struct surface>(id, this);
237
238    add_task("fullscreen surface", [id](struct controller *c) {
239       auto &s = c->surfaces[id];
240       s->set_destination_rectangle(0, 0, c->output_size.w, c->output_size.h);
241       s->set_visibility(1);
242       uint32_t lid = id == 0x16180 ? 1000 : 100;
243       c->layers[lid]->add_surface(s.get());
244       logdebug("Surface %u now fullscreen on layer %u", id, lid);
245    });
246 }
247
248 void controller::controller_error(int32_t object_id, int32_t object_type,
249                                   int32_t error_code, const char *error_text) {
250    logdebug("genivi::controller @ %p error o %i t %i c %i text %s",
251             this->proxy.get(), object_id, object_type, error_code, error_text);
252 }
253
254 //  _
255 // | | __ _ _   _  ___ _ __
256 // | |/ _` | | | |/ _ \ '__|
257 // | | (_| | |_| |  __/ |
258 // |_|\__,_|\__, |\___|_|
259 //          |___/
260 namespace {
261 void layer_visibility(void *data,
262                       struct ivi_controller_layer * /*ivi_controller_layer*/,
263                       int32_t visibility) {
264    auto l = static_cast<struct layer *>(data);
265    l->parent->layer_visibility(l->id, visibility);
266 }
267
268 void layer_opacity(void *data,
269                    struct ivi_controller_layer * /*ivi_controller_layer*/,
270                    wl_fixed_t opacity) {
271    auto l = static_cast<struct layer *>(data);
272    l->parent->layer_opacity(l->id, float(wl_fixed_to_double(opacity)));
273 }
274
275 void layer_source_rectangle(
276    void *data, struct ivi_controller_layer * /*ivi_controller_layer*/,
277    int32_t x, int32_t y, int32_t width, int32_t height) {
278    auto l = static_cast<struct layer *>(data);
279    l->parent->layer_source_rectangle(l->id, x, y, width, height);
280 }
281
282 void layer_destination_rectangle(
283    void *data, struct ivi_controller_layer * /*ivi_controller_layer*/,
284    int32_t x, int32_t y, int32_t width, int32_t height) {
285    auto l = static_cast<struct layer *>(data);
286    l->parent->layer_destination_rectangle(l->id, x, y, width, height);
287 }
288
289 void layer_configuration(void *data,
290                          struct ivi_controller_layer * /*ivi_controller_layer*/,
291                          int32_t width, int32_t height) {
292    auto l = static_cast<struct layer *>(data);
293    l->parent->layer_configuration(l->id, width, height);
294 }
295
296 void layer_orientation(void *data,
297                        struct ivi_controller_layer * /*ivi_controller_layer*/,
298                        int32_t orientation) {
299    auto l = static_cast<struct layer *>(data);
300    l->parent->layer_orientation(l->id, orientation);
301 }
302
303 void layer_screen(void *data,
304                   struct ivi_controller_layer * /*ivi_controller_layer*/,
305                   struct wl_output *screen) {
306    auto l = static_cast<struct layer *>(data);
307    l->parent->layer_screen(l->id, screen);
308 }
309
310 void layer_destroyed(void *data,
311                      struct ivi_controller_layer * /*ivi_controller_layer*/) {
312    auto l = static_cast<struct layer *>(data);
313    l->parent->layer_destroyed(l->id);
314 }
315
316 constexpr struct ivi_controller_layer_listener layer_listener = {
317    layer_visibility,       layer_opacity,
318    layer_source_rectangle, layer_destination_rectangle,
319    layer_configuration,    layer_orientation,
320    layer_screen,           layer_destroyed,
321 };
322 }  // namespace
323
324 layer::layer(uint32_t i, struct controller *c) : layer(i, 0, 0, c) {}
325
326 layer::layer(uint32_t i, int32_t w, int32_t h, struct controller *c)
327    : wayland_proxy(ivi_controller_layer_create(c->proxy.get(), i, w, h),
328                    [c, i](ivi_controller_layer *l) {
329                       logdebug("~layer layer %i @ %p", i, l);
330                       c->remove_proxy_to_id_mapping(l);
331                       ivi_controller_layer_destroy(l, 1);
332                    }),
333      controller_child(c, i),
334      dst_rect{},
335      src_rect{},
336      size{},
337      orientation{},
338      visibility{},
339      opacity{} {
340    this->parent->add_proxy_to_id_mapping(this->proxy.get(), i);
341    ivi_controller_layer_add_listener(this->proxy.get(), &layer_listener, this);
342 }
343
344 void layer::set_visibility(uint32_t visibility) {
345    ivi_controller_layer_set_visibility(this->proxy.get(), visibility);
346 }
347
348 void layer::set_opacity(wl_fixed_t opacity) {
349    ivi_controller_layer_set_opacity(this->proxy.get(), opacity);
350 }
351
352 void layer::set_source_rectangle(int32_t x, int32_t y, int32_t width,
353                                  int32_t height) {
354    ivi_controller_layer_set_source_rectangle(this->proxy.get(), x, y, width,
355                                              height);
356 }
357
358 void layer::set_destination_rectangle(int32_t x, int32_t y, int32_t width,
359                                       int32_t height) {
360    ivi_controller_layer_set_destination_rectangle(this->proxy.get(), x, y,
361                                                   width, height);
362 }
363
364 void layer::set_configuration(int32_t width, int32_t height) {
365    ivi_controller_layer_set_configuration(this->proxy.get(), width, height);
366 }
367
368 void layer::set_orientation(int32_t orientation) {
369    ivi_controller_layer_set_orientation(this->proxy.get(), orientation);
370 }
371
372 void layer::screenshot(const char *filename) {
373    ivi_controller_layer_screenshot(this->proxy.get(), filename);
374 }
375
376 void layer::clear_surfaces() {
377    ivi_controller_layer_clear_surfaces(this->proxy.get());
378 }
379
380 void layer::add_surface(struct surface *surface) {
381    ivi_controller_layer_add_surface(this->proxy.get(), surface->proxy.get());
382 }
383
384 void layer::remove_surface(struct surface *surface) {
385    ivi_controller_layer_remove_surface(this->proxy.get(), surface->proxy.get());
386 }
387
388 void layer::set_render_order(std::vector<uint32_t> const &ro) {
389    struct wl_array wlro {
390       .size = ro.size() * sizeof(ro[0]), .alloc = ro.capacity() * sizeof(ro[0]),
391       .data = const_cast<void *>(static_cast<void const *>(ro.data()))
392    };
393    ivi_controller_layer_set_render_order(this->proxy.get(), &wlro);
394 }
395
396 void controller::layer_visibility(uint32_t id, int32_t visibility) {
397    logdebug("genivi::layer %s @ %p v %i", __func__, this->proxy.get(),
398             visibility);
399    this->layers[id]->visibility = visibility;
400 }
401
402 void controller::layer_opacity(uint32_t id, float opacity) {
403    logdebug("genivi::layer %s @ %p o %f", __func__, this->proxy.get(), opacity);
404    this->layers[id]->opacity = opacity;
405 }
406
407 void controller::layer_source_rectangle(uint32_t id, int32_t x, int32_t y,
408                                         int32_t width, int32_t height) {
409    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__,
410             this->proxy.get(), x, y, width, height);
411    this->layers[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
412 }
413
414 void controller::layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
415                                              int32_t width, int32_t height) {
416    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__,
417             this->proxy.get(), x, y, width, height);
418    this->layers[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
419 }
420
421 void controller::layer_configuration(uint32_t id, int32_t width,
422                                      int32_t height) {
423    logdebug("genivi::layer %s @ %p w %i h %i", __func__, this->proxy.get(),
424             width, height);
425    this->layers[id]->size = size{uint32_t(width), uint32_t(height)};
426 }
427
428 void controller::layer_orientation(uint32_t id, int32_t orientation) {
429    logdebug("genivi::layer %s @ %p o %i", __func__, this->proxy.get(),
430             orientation);
431    this->layers[id]->orientation = orientation;
432 }
433
434 void controller::layer_screen(uint32_t /*id*/, struct wl_output *screen) {
435    logdebug("genivi::layer %s @ %p s %p", __func__, this->proxy.get(), screen);
436 }
437
438 void controller::layer_destroyed(uint32_t id) {
439    logdebug("genivi::layer %s @ %p", __func__, this->proxy.get());
440    add_task("remove layer",
441             [id](struct controller *c) { c->layers.erase(id); });
442 }
443
444 //                  __
445 //  ___ _   _ _ __ / _| __ _  ___ ___
446 // / __| | | | '__| |_ / _` |/ __/ _ \
447 // \__ \ |_| | |  |  _| (_| | (_|  __/
448 // |___/\__,_|_|  |_|  \__,_|\___\___|
449 //
450 namespace {
451
452 void surface_visibility(
453    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
454    int32_t visibility) {
455    auto s = static_cast<struct surface *>(data);
456    s->parent->surface_visibility(s->id, visibility);
457 }
458
459 void surface_opacity(void *data,
460                      struct ivi_controller_surface * /*ivi_controller_surface*/,
461                      wl_fixed_t opacity) {
462    auto s = static_cast<struct surface *>(data);
463    s->parent->surface_opacity(s->id, float(wl_fixed_to_double(opacity)));
464 }
465
466 void surface_source_rectangle(
467    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
468    int32_t x, int32_t y, int32_t width, int32_t height) {
469    auto s = static_cast<struct surface *>(data);
470    s->parent->surface_source_rectangle(s->id, x, y, width, height);
471 }
472
473 void surface_destination_rectangle(
474    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
475    int32_t x, int32_t y, int32_t width, int32_t height) {
476    auto s = static_cast<struct surface *>(data);
477    s->parent->surface_destination_rectangle(s->id, x, y, width, height);
478 }
479
480 void surface_configuration(
481    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
482    int32_t width, int32_t height) {
483    auto s = static_cast<struct surface *>(data);
484    s->parent->surface_configuration(s->id, width, height);
485 }
486
487 void surface_orientation(
488    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
489    int32_t orientation) {
490    auto s = static_cast<struct surface *>(data);
491    s->parent->surface_orientation(s->id, orientation);
492 }
493
494 void surface_pixelformat(
495    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
496    int32_t pixelformat) {
497    auto s = static_cast<struct surface *>(data);
498    s->parent->surface_pixelformat(s->id, pixelformat);
499 }
500
501 void surface_layer(void *data,
502                    struct ivi_controller_surface * /*ivi_controller_surface*/,
503                    struct ivi_controller_layer *layer) {
504    auto s = static_cast<struct surface *>(data);
505    s->parent->surface_layer(s->id, layer);
506 }
507
508 void surface_stats(void *data,
509                    struct ivi_controller_surface * /*ivi_controller_surface*/,
510                    uint32_t redraw_count, uint32_t frame_count,
511                    uint32_t update_count, uint32_t pid,
512                    const char *process_name) {
513    auto s = static_cast<struct surface *>(data);
514    s->parent->surface_stats(s->id, redraw_count, frame_count, update_count, pid,
515                             process_name);
516 }
517
518 void surface_destroyed(
519    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/) {
520    auto s = static_cast<struct surface *>(data);
521    s->parent->surface_destroyed(s->id);
522 }
523
524 void surface_content(void *data,
525                      struct ivi_controller_surface * /*ivi_controller_surface*/,
526                      int32_t content_state) {
527    auto s = static_cast<struct surface *>(data);
528    s->parent->surface_content(s->id, content_state);
529 }
530
531 constexpr struct ivi_controller_surface_listener surface_listener = {
532    surface_visibility,
533    surface_opacity,
534    surface_source_rectangle,
535    surface_destination_rectangle,
536    surface_configuration,
537    surface_orientation,
538    surface_pixelformat,
539    surface_layer,
540    surface_stats,
541    surface_destroyed,
542    surface_content,
543 };
544 }  // namespace
545
546 surface::surface(uint32_t i, struct controller *c)
547    : wayland_proxy(ivi_controller_surface_create(c->proxy.get(), i),
548                    [c, i](ivi_controller_surface *s) {
549                       logdebug("~surface surface %i @ %p", i, s);
550                       c->remove_proxy_to_id_mapping(s);
551                       ivi_controller_surface_destroy(s, 1);
552                    }),
553      controller_child(c, i),
554      dst_rect{},
555      src_rect{},
556      size{},
557      orientation{},
558      visibility{},
559      opacity{1.f} {
560    this->parent->add_proxy_to_id_mapping(this->proxy.get(), i);
561    ivi_controller_surface_add_listener(this->proxy.get(), &surface_listener,
562                                        this);
563 }
564
565 void surface::set_visibility(uint32_t visibility) {
566    ivi_controller_surface_set_visibility(this->proxy.get(), visibility);
567 }
568
569 void surface::set_opacity(wl_fixed_t opacity) {
570    ivi_controller_surface_set_opacity(this->proxy.get(), opacity);
571 }
572
573 void surface::set_source_rectangle(int32_t x, int32_t y, int32_t width,
574                                    int32_t height) {
575    ivi_controller_surface_set_source_rectangle(this->proxy.get(), x, y, width,
576                                                height);
577 }
578
579 void surface::set_destination_rectangle(int32_t x, int32_t y, int32_t width,
580                                         int32_t height) {
581    ivi_controller_surface_set_destination_rectangle(this->proxy.get(), x, y,
582                                                     width, height);
583 }
584
585 void surface::set_configuration(int32_t width, int32_t height) {
586    ivi_controller_surface_set_configuration(this->proxy.get(), width, height);
587 }
588
589 void surface::set_orientation(int32_t orientation) {
590    ivi_controller_surface_set_orientation(this->proxy.get(), orientation);
591 }
592
593 void surface::screenshot(const char *filename) {
594    ivi_controller_surface_screenshot(this->proxy.get(), filename);
595 }
596
597 void surface::send_stats() {
598    ivi_controller_surface_send_stats(this->proxy.get());
599 }
600
601 void surface::destroy(int32_t destroy_scene_object) {
602    ivi_controller_surface_destroy(this->proxy.get(), destroy_scene_object);
603 }
604
605 void controller::surface_visibility(uint32_t id, int32_t visibility) {
606    logdebug("genivi::surface %s @ %p v %i", __func__, this->proxy.get(),
607             visibility);
608    this->surfaces[id]->visibility = visibility;
609 }
610
611 void controller::surface_opacity(uint32_t id, float opacity) {
612    logdebug("genivi::surface %s @ %p o %f", __func__, this->proxy.get(),
613             opacity);
614    this->surfaces[id]->opacity = opacity;
615 }
616
617 void controller::surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
618                                           int32_t width, int32_t height) {
619    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
620             this->proxy.get(), x, y, width, height);
621    this->surfaces[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
622 }
623
624 void controller::surface_destination_rectangle(uint32_t id, int32_t x,
625                                                int32_t y, int32_t width,
626                                                int32_t height) {
627    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
628             this->proxy.get(), x, y, width, height);
629    this->surfaces[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
630 }
631
632 void controller::surface_configuration(uint32_t id, int32_t width,
633                                        int32_t height) {
634    logdebug("genivi::surface %s @ %p w %i h %i", __func__, this->proxy.get(),
635             width, height);
636    this->surfaces[id]->size = size{uint32_t(width), uint32_t(height)};
637 }
638
639 void controller::surface_orientation(uint32_t id, int32_t orientation) {
640    logdebug("genivi::surface %s @ %p o %i", __func__, this->proxy.get(),
641             orientation);
642    this->surfaces[id]->orientation = orientation;
643 }
644
645 void controller::surface_pixelformat(uint32_t /*id*/, int32_t pixelformat) {
646    logdebug("genivi::surface %s @ %p f %i", __func__, this->proxy.get(),
647             pixelformat);
648 }
649
650 void controller::surface_layer(uint32_t /*id*/,
651                                struct ivi_controller_layer *layer) {
652    logdebug("genivi::surface %s @ %p l %u @ %p", __func__, this->proxy.get(),
653             this->layer_proxy_to_id[uintptr_t(layer)], layer);
654 }
655
656 void controller::surface_stats(uint32_t /*id*/, uint32_t redraw_count,
657                                uint32_t frame_count, uint32_t update_count,
658                                uint32_t pid, const char *process_name) {
659    logdebug("genivi::surface %s @ %p r %u f %u u %u pid %u p %s", __func__,
660             this->proxy.get(), redraw_count, frame_count, update_count, pid,
661             process_name);
662 }
663
664 void controller::surface_destroyed(uint32_t id) {
665    logdebug("genivi::surface %s @ %p", __func__, this->proxy.get());
666    this->surfaces.erase(id);
667 }
668
669 void controller::surface_content(uint32_t id, int32_t content_state) {
670    logdebug("genivi::surface %s @ %p s %i", __func__, this->proxy.get(),
671             content_state);
672    if (content_state == IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED) {
673       add_task("remove surface",
674                [id](struct controller *c) { c->surfaces.erase(id); });
675    }
676 }
677
678 void controller::add_proxy_to_id_mapping(struct ivi_controller_surface *p,
679                                          uint32_t id) {
680    this->surface_proxy_to_id[uintptr_t(p)] = id;
681    logdebug("Add surface proxy mapping for %p (%u)", p, id);
682 }
683
684 void controller::remove_proxy_to_id_mapping(struct ivi_controller_surface *p) {
685    logdebug("Remove surface proxy mapping for %p", p);
686    this->surface_proxy_to_id.erase(uintptr_t(p));
687 }
688
689 void controller::add_proxy_to_id_mapping(struct ivi_controller_layer *p,
690                                          uint32_t id) {
691    logdebug("Add layer proxy mapping for %p (%u)", p, id);
692    this->layer_proxy_to_id[uintptr_t(p)] = id;
693 }
694
695 void controller::remove_proxy_to_id_mapping(struct ivi_controller_layer *p) {
696    logdebug("Remove layer proxy mapping for %p", p);
697    this->layer_proxy_to_id.erase(uintptr_t(p));
698 }
699
700 void controller::add_proxy_to_id_mapping(struct wl_output *p, uint32_t id) {
701    logdebug("Add screen proxy mapping for %p (%u)", p, id);
702    this->screen_proxy_to_id[uintptr_t(p)] = id;
703 }
704
705 void controller::remove_proxy_to_id_mapping(struct wl_output *p) {
706    logdebug("Remove screen proxy mapping for %p", p);
707    this->screen_proxy_to_id.erase(uintptr_t(p));
708 }
709
710 void controller::add_task(char const *name,
711                           std::function<void(struct controller *)> &&f) {
712    this->pending.emplace_back(std::make_pair(name, f));
713 }
714
715 void controller::execute_pending() {
716    if (!this->pending.empty()) {
717       for (auto &t : this->pending) {
718          logdebug("executing task '%s'", t.first);
719          t.second(this);
720       }
721       this->pending.clear();
722       ivi_controller_commit_changes(this->proxy.get());
723       // XXX: No flush here...
724    }
725 }
726
727 //
728 //  ___  ___ _ __ ___  ___ _ __
729 // / __|/ __| '__/ _ \/ _ \ '_ \
730 // \__ \ (__| | |  __/  __/ | | |
731 // |___/\___|_|  \___|\___|_| |_|
732 //
733 screen::screen(uint32_t i, struct controller *c,
734                struct ivi_controller_screen *p)
735    : wayland_proxy(p), controller_child(c, i) {
736    logdebug("genivi::screen @ %p id %u", p, i);
737 }
738
739 void screen::clear() { ivi_controller_screen_clear(this->proxy.get()); }
740
741 void screen::add_layer(layer *l) {
742    ivi_controller_screen_add_layer(this->proxy.get(), l->proxy.get());
743 }
744
745 void screen::set_render_order(std::vector<uint32_t> const &ro) {
746    struct wl_array wlro {
747       .size = ro.size() * sizeof(ro[0]), .alloc = ro.capacity() * sizeof(ro[0]),
748       .data = const_cast<void *>(static_cast<void const *>(ro.data()))
749    };
750    ivi_controller_screen_set_render_order(this->proxy.get(), &wlro);
751 }
752
753 }  // namespace genivi