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