wayland: removed unnecessary destructors
[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, &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, name, v);
79    }
80    logdebug("wl::registry @ %p global n %u i %s v %u", this->proxy, name, iface,
81             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, &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, 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__, this->proxy,
134             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);
144 }
145
146 void output::scale(int32_t factor) {
147    logdebug("wl::output %s @ %p f %i", __func__, this->proxy, 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, &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, id, id,
223             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, 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, id, id);
235    this->surfaces[id] = std::make_unique<struct surface>(id, this);
236
237    add_task("fullscreen surface", [id](struct controller *c) {
238       auto &s = c->surfaces[id];
239       s->set_destination_rectangle(0, 0, c->output_size.w, c->output_size.h);
240       s->set_visibility(1);
241       uint32_t lid = id == 0x16180 ? 1000 : 100;
242       c->layers[lid]->add_surface(s.get());
243       logdebug("Surface %u now fullscreen on layer %u", id, lid);
244    });
245 }
246
247 void controller::controller_error(int32_t object_id, int32_t object_type,
248                                   int32_t error_code, const char *error_text) {
249    logdebug("genivi::controller @ %p error o %i t %i c %i text %s", this->proxy,
250             object_id, object_type, error_code, error_text);
251 }
252
253 //  _
254 // | | __ _ _   _  ___ _ __
255 // | |/ _` | | | |/ _ \ '__|
256 // | | (_| | |_| |  __/ |
257 // |_|\__,_|\__, |\___|_|
258 //          |___/
259 namespace {
260 void layer_visibility(void *data,
261                       struct ivi_controller_layer * /*ivi_controller_layer*/,
262                       int32_t visibility) {
263    auto l = static_cast<struct layer *>(data);
264    l->parent->layer_visibility(l->id, visibility);
265 }
266
267 void layer_opacity(void *data,
268                    struct ivi_controller_layer * /*ivi_controller_layer*/,
269                    wl_fixed_t opacity) {
270    auto l = static_cast<struct layer *>(data);
271    l->parent->layer_opacity(l->id, float(wl_fixed_to_double(opacity)));
272 }
273
274 void layer_source_rectangle(
275    void *data, struct ivi_controller_layer * /*ivi_controller_layer*/,
276    int32_t x, int32_t y, int32_t width, int32_t height) {
277    auto l = static_cast<struct layer *>(data);
278    l->parent->layer_source_rectangle(l->id, x, y, width, height);
279 }
280
281 void layer_destination_rectangle(
282    void *data, struct ivi_controller_layer * /*ivi_controller_layer*/,
283    int32_t x, int32_t y, int32_t width, int32_t height) {
284    auto l = static_cast<struct layer *>(data);
285    l->parent->layer_destination_rectangle(l->id, x, y, width, height);
286 }
287
288 void layer_configuration(void *data,
289                          struct ivi_controller_layer * /*ivi_controller_layer*/,
290                          int32_t width, int32_t height) {
291    auto l = static_cast<struct layer *>(data);
292    l->parent->layer_configuration(l->id, width, height);
293 }
294
295 void layer_orientation(void *data,
296                        struct ivi_controller_layer * /*ivi_controller_layer*/,
297                        int32_t orientation) {
298    auto l = static_cast<struct layer *>(data);
299    l->parent->layer_orientation(l->id, orientation);
300 }
301
302 void layer_screen(void *data,
303                   struct ivi_controller_layer * /*ivi_controller_layer*/,
304                   struct wl_output *screen) {
305    auto l = static_cast<struct layer *>(data);
306    l->parent->layer_screen(l->id, screen);
307 }
308
309 void layer_destroyed(void *data,
310                      struct ivi_controller_layer * /*ivi_controller_layer*/) {
311    auto l = static_cast<struct layer *>(data);
312    l->parent->layer_destroyed(l->id);
313 }
314
315 constexpr struct ivi_controller_layer_listener layer_listener = {
316    layer_visibility,       layer_opacity,
317    layer_source_rectangle, layer_destination_rectangle,
318    layer_configuration,    layer_orientation,
319    layer_screen,           layer_destroyed,
320 };
321 }  // namespace
322
323 layer::layer(uint32_t i, struct controller *c) : layer(i, 0, 0, c) {}
324
325 layer::layer(uint32_t i, int32_t w, int32_t h, struct controller *c)
326    : wayland_proxy(ivi_controller_layer_create(c->proxy, i, w, h)),
327      controller_child(c, i),
328      dst_rect{},
329      src_rect{},
330      size{},
331      orientation{},
332      visibility{},
333      opacity{} {
334    this->parent->add_proxy_to_id_mapping(this->proxy, i);
335    ivi_controller_layer_add_listener(this->proxy, &layer_listener, this);
336 }
337
338 layer::~layer() {
339    logdebug("%s layer %i @ %p", __func__, this->id, this->proxy);
340    this->parent->remove_proxy_to_id_mapping(this->proxy);
341    ivi_controller_layer_destroy(this->proxy, 1);
342    this->proxy = nullptr;
343 }
344
345 void layer::set_visibility(uint32_t visibility) {
346    ivi_controller_layer_set_visibility(this->proxy, visibility);
347 }
348
349 void layer::set_opacity(wl_fixed_t opacity) {
350    ivi_controller_layer_set_opacity(this->proxy, opacity);
351 }
352
353 void layer::set_source_rectangle(int32_t x, int32_t y, int32_t width,
354                                  int32_t height) {
355    ivi_controller_layer_set_source_rectangle(this->proxy, x, y, width, 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, x, y, width,
361                                                   height);
362 }
363
364 void layer::set_configuration(int32_t width, int32_t height) {
365    ivi_controller_layer_set_configuration(this->proxy, width, height);
366 }
367
368 void layer::set_orientation(int32_t orientation) {
369    ivi_controller_layer_set_orientation(this->proxy, orientation);
370 }
371
372 void layer::screenshot(const char *filename) {
373    ivi_controller_layer_screenshot(this->proxy, filename);
374 }
375
376 void layer::clear_surfaces() {
377    ivi_controller_layer_clear_surfaces(this->proxy);
378 }
379
380 void layer::add_surface(struct surface *surface) {
381    ivi_controller_layer_add_surface(this->proxy, surface->proxy);
382 }
383
384 void layer::remove_surface(struct surface *surface) {
385    ivi_controller_layer_remove_surface(this->proxy, surface->proxy);
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, &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, visibility);
398    this->layers[id]->visibility = visibility;
399 }
400
401 void controller::layer_opacity(uint32_t id, float opacity) {
402    logdebug("genivi::layer %s @ %p o %f", __func__, this->proxy, opacity);
403    this->layers[id]->opacity = opacity;
404 }
405
406 void controller::layer_source_rectangle(uint32_t id, int32_t x, int32_t y,
407                                         int32_t width, int32_t height) {
408    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
409             x, y, width, height);
410    this->layers[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
411 }
412
413 void controller::layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
414                                              int32_t width, int32_t height) {
415    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
416             x, y, width, height);
417    this->layers[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
418 }
419
420 void controller::layer_configuration(uint32_t id, int32_t width,
421                                      int32_t height) {
422    logdebug("genivi::layer %s @ %p w %i h %i", __func__, this->proxy, width,
423             height);
424    this->layers[id]->size = size{uint32_t(width), uint32_t(height)};
425 }
426
427 void controller::layer_orientation(uint32_t id, int32_t orientation) {
428    logdebug("genivi::layer %s @ %p o %i", __func__, this->proxy, orientation);
429    this->layers[id]->orientation = orientation;
430 }
431
432 void controller::layer_screen(uint32_t /*id*/, struct wl_output *screen) {
433    logdebug("genivi::layer %s @ %p s %p", __func__, this->proxy, screen);
434 }
435
436 void controller::layer_destroyed(uint32_t id) {
437    logdebug("genivi::layer %s @ %p", __func__, this->proxy);
438    add_task("remove layer",
439             [id](struct controller *c) { c->layers.erase(id); });
440 }
441
442 //                  __
443 //  ___ _   _ _ __ / _| __ _  ___ ___
444 // / __| | | | '__| |_ / _` |/ __/ _ \
445 // \__ \ |_| | |  |  _| (_| | (_|  __/
446 // |___/\__,_|_|  |_|  \__,_|\___\___|
447 //
448 namespace {
449
450 void surface_visibility(
451    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
452    int32_t visibility) {
453    auto s = static_cast<struct surface *>(data);
454    s->parent->surface_visibility(s->id, visibility);
455 }
456
457 void surface_opacity(void *data,
458                      struct ivi_controller_surface * /*ivi_controller_surface*/,
459                      wl_fixed_t opacity) {
460    auto s = static_cast<struct surface *>(data);
461    s->parent->surface_opacity(s->id, float(wl_fixed_to_double(opacity)));
462 }
463
464 void surface_source_rectangle(
465    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
466    int32_t x, int32_t y, int32_t width, int32_t height) {
467    auto s = static_cast<struct surface *>(data);
468    s->parent->surface_source_rectangle(s->id, x, y, width, height);
469 }
470
471 void surface_destination_rectangle(
472    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
473    int32_t x, int32_t y, int32_t width, int32_t height) {
474    auto s = static_cast<struct surface *>(data);
475    s->parent->surface_destination_rectangle(s->id, x, y, width, height);
476 }
477
478 void surface_configuration(
479    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
480    int32_t width, int32_t height) {
481    auto s = static_cast<struct surface *>(data);
482    s->parent->surface_configuration(s->id, width, height);
483 }
484
485 void surface_orientation(
486    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
487    int32_t orientation) {
488    auto s = static_cast<struct surface *>(data);
489    s->parent->surface_orientation(s->id, orientation);
490 }
491
492 void surface_pixelformat(
493    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/,
494    int32_t pixelformat) {
495    auto s = static_cast<struct surface *>(data);
496    s->parent->surface_pixelformat(s->id, pixelformat);
497 }
498
499 void surface_layer(void *data,
500                    struct ivi_controller_surface * /*ivi_controller_surface*/,
501                    struct ivi_controller_layer *layer) {
502    auto s = static_cast<struct surface *>(data);
503    s->parent->surface_layer(s->id, layer);
504 }
505
506 void surface_stats(void *data,
507                    struct ivi_controller_surface * /*ivi_controller_surface*/,
508                    uint32_t redraw_count, uint32_t frame_count,
509                    uint32_t update_count, uint32_t pid,
510                    const char *process_name) {
511    auto s = static_cast<struct surface *>(data);
512    s->parent->surface_stats(s->id, redraw_count, frame_count,
513       update_count, pid, process_name);
514 }
515
516 void surface_destroyed(
517    void *data, struct ivi_controller_surface * /*ivi_controller_surface*/) {
518    auto s = static_cast<struct surface *>(data);
519    s->parent->surface_destroyed(s->id);
520 }
521
522 void surface_content(void *data,
523                      struct ivi_controller_surface * /*ivi_controller_surface*/,
524                      int32_t content_state) {
525    auto s = static_cast<struct surface *>(data);
526    s->parent->surface_content(s->id, content_state);
527 }
528
529 constexpr struct ivi_controller_surface_listener surface_listener = {
530    surface_visibility,
531    surface_opacity,
532    surface_source_rectangle,
533    surface_destination_rectangle,
534    surface_configuration,
535    surface_orientation,
536    surface_pixelformat,
537    surface_layer,
538    surface_stats,
539    surface_destroyed,
540    surface_content,
541 };
542 }  // namespace
543
544 surface::surface(uint32_t i, struct controller *c)
545    : wayland_proxy(ivi_controller_surface_create(c->proxy, i)),
546      controller_child(c, i),
547      dst_rect{},
548      src_rect{},
549      size{},
550      orientation{},
551      visibility{},
552      opacity{1.f} {
553    this->parent->add_proxy_to_id_mapping(this->proxy, i);
554    ivi_controller_surface_add_listener(this->proxy, &surface_listener, this);
555 }
556
557 surface::~surface() {
558    logdebug("%s surface %i @ %p", __func__, this->id, this->proxy);
559    this->parent->remove_proxy_to_id_mapping(this->proxy);
560    ivi_controller_surface_destroy(this->proxy, 1);
561    this->proxy = nullptr;
562 }
563
564 void surface::set_visibility(uint32_t visibility) {
565    ivi_controller_surface_set_visibility(this->proxy, visibility);
566 }
567
568 void surface::set_opacity(wl_fixed_t opacity) {
569    ivi_controller_surface_set_opacity(this->proxy, opacity);
570 }
571
572 void surface::set_source_rectangle(int32_t x, int32_t y, int32_t width,
573                                    int32_t height) {
574    ivi_controller_surface_set_source_rectangle(this->proxy, x, y, width,
575                                                height);
576 }
577
578 void surface::set_destination_rectangle(int32_t x, int32_t y, int32_t width,
579                                         int32_t height) {
580    ivi_controller_surface_set_destination_rectangle(this->proxy, x, y, width,
581                                                     height);
582 }
583
584 void surface::set_configuration(int32_t width, int32_t height) {
585    ivi_controller_surface_set_configuration(this->proxy, width, height);
586 }
587
588 void surface::set_orientation(int32_t orientation) {
589    ivi_controller_surface_set_orientation(this->proxy, orientation);
590 }
591
592 void surface::screenshot(const char *filename) {
593    ivi_controller_surface_screenshot(this->proxy, filename);
594 }
595
596 void surface::send_stats() { ivi_controller_surface_send_stats(this->proxy); }
597
598 void surface::destroy(int32_t destroy_scene_object) {
599    ivi_controller_surface_destroy(this->proxy, destroy_scene_object);
600 }
601
602 void controller::surface_visibility(uint32_t id, int32_t visibility) {
603    logdebug("genivi::surface %s @ %p v %i", __func__, this->proxy, visibility);
604    this->surfaces[id]->visibility = visibility;
605 }
606
607 void controller::surface_opacity(uint32_t id, float opacity) {
608    logdebug("genivi::surface %s @ %p o %f", __func__, this->proxy, opacity);
609    this->surfaces[id]->opacity = opacity;
610 }
611
612 void controller::surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
613                                           int32_t width, int32_t height) {
614    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
615             this->proxy, x, y, width, height);
616    this->surfaces[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
617 }
618
619 void controller::surface_destination_rectangle(uint32_t id, int32_t x,
620                                                int32_t y, int32_t width,
621                                                int32_t height) {
622    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
623             this->proxy, x, y, width, height);
624    this->surfaces[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
625 }
626
627 void controller::surface_configuration(uint32_t id, int32_t width,
628                                        int32_t height) {
629    logdebug("genivi::surface %s @ %p w %i h %i", __func__, this->proxy, width,
630             height);
631    this->surfaces[id]->size = size{uint32_t(width), uint32_t(height)};
632 }
633
634 void controller::surface_orientation(uint32_t id, int32_t orientation) {
635    logdebug("genivi::surface %s @ %p o %i", __func__, this->proxy, orientation);
636    this->surfaces[id]->orientation = orientation;
637 }
638
639 void controller::surface_pixelformat(uint32_t /*id*/, int32_t pixelformat) {
640    logdebug("genivi::surface %s @ %p f %i", __func__, this->proxy, pixelformat);
641 }
642
643 void controller::surface_layer(uint32_t /*id*/,
644                                struct ivi_controller_layer *layer) {
645    logdebug("genivi::surface %s @ %p l %u @ %p", __func__, this->proxy,
646             this->layer_proxy_to_id[uintptr_t(layer)], layer);
647 }
648
649 void controller::surface_stats(uint32_t /*id*/, uint32_t redraw_count,
650                                uint32_t frame_count, uint32_t update_count,
651                                uint32_t pid, const char *process_name) {
652    logdebug("genivi::surface %s @ %p r %u f %u u %u pid %u p %s", __func__,
653             this->proxy, redraw_count, frame_count, update_count, pid,
654             process_name);
655 }
656
657 void controller::surface_destroyed(uint32_t id) {
658    logdebug("genivi::surface %s @ %p", __func__, this->proxy);
659    this->surfaces.erase(id);
660 }
661
662 void controller::surface_content(uint32_t id, int32_t content_state) {
663    logdebug("genivi::surface %s @ %p s %i", __func__, this->proxy,
664             content_state);
665    if (content_state == IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED) {
666       add_task("remove surface",
667                [id](struct controller *c) { c->surfaces.erase(id); });
668    }
669 }
670
671 void controller::add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id) {
672    this->surface_proxy_to_id[uintptr_t(p)] = id;
673    logdebug("Add surface proxy mapping for %p (%u)", p, id);
674 }
675
676 void controller::remove_proxy_to_id_mapping(struct ivi_controller_surface *p) {
677    logdebug("Remove surface proxy mapping for %p", p);
678    this->surface_proxy_to_id.erase(uintptr_t(p));
679 }
680
681 void controller::add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id) {
682    logdebug("Add layer proxy mapping for %p (%u)", p, id);
683    this->layer_proxy_to_id[uintptr_t(p)] = id;
684 }
685
686 void controller::remove_proxy_to_id_mapping(struct ivi_controller_layer *p) {
687    logdebug("Remove layer proxy mapping for %p", p);
688    this->layer_proxy_to_id.erase(uintptr_t(p));
689 }
690
691 void controller::add_proxy_to_id_mapping(struct wl_output *p, uint32_t id) {
692    logdebug("Add screen proxy mapping for %p (%u)", p, id);
693    this->screen_proxy_to_id[uintptr_t(p)] = id;
694 }
695
696 void controller::remove_proxy_to_id_mapping(struct wl_output *p) {
697    logdebug("Remove screen proxy mapping for %p", p);
698    this->screen_proxy_to_id.erase(uintptr_t(p));
699 }
700
701 void controller::add_task(char const *name, std::function<void(struct controller *)> &&f) {
702    this->pending.emplace_back(std::make_pair(name, f));
703 }
704
705 void controller::execute_pending() {
706    if (!this->pending.empty()) {
707       for (auto &t : this->pending) {
708          logdebug("executing task '%s'", t.first);
709          t.second(this);
710       }
711       this->pending.clear();
712       ivi_controller_commit_changes(this->proxy);
713       // XXX: No flush here...
714    }
715 }
716
717 //
718 //  ___  ___ _ __ ___  ___ _ __
719 // / __|/ __| '__/ _ \/ _ \ '_ \
720 // \__ \ (__| | |  __/  __/ | | |
721 // |___/\___|_|  \___|\___|_| |_|
722 //
723 screen::screen(uint32_t i, struct controller *c,
724                struct ivi_controller_screen *p)
725    : wayland_proxy(p), controller_child(c, i) {
726    logdebug("genivi::screen @ %p id %u", p, i);
727 }
728
729 void screen::clear() { ivi_controller_screen_clear(this->proxy); }
730
731 void screen::add_layer(layer *l) {
732    ivi_controller_screen_add_layer(this->proxy, l->proxy);
733 }
734
735 void screen::set_render_order(std::vector<uint32_t> const &ro) {
736    struct wl_array wlro {
737       .size = ro.size() * sizeof(ro[0]), .alloc = ro.capacity() * sizeof(ro[0]),
738       .data = const_cast<void *>(static_cast<void const *>(ro.data()))
739    };
740    ivi_controller_screen_set_render_order(this->proxy, &wlro);
741 }
742
743 }  // namespace genivi