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