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