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