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