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