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