main: set layer visibility in init_layout()
[staging/windowmanager.git] / src / wayland.cpp
1 #include "wayland.hpp"
2
3 //                                                                  _
4 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___  __      _| |
5 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \ \ \ /\ / / |
6 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/  \ V  V /| |
7 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|   \_/\_/ |_|
8 //                                 |_|
9 namespace wl {
10
11 //      _ _           _
12 //   __| (_)___ _ __ | | __ _ _   _
13 //  / _` | / __| '_ \| |/ _` | | | |
14 // | (_| | \__ \ |_) | | (_| | |_| |
15 //  \__,_|_|___/ .__/|_|\__,_|\__, |
16 //             |_|            |___/
17 display::display()
18    : d(std::unique_ptr<struct wl_display, void (*)(struct wl_display *)>(
19         wl_display_connect(NULL),
20 #ifdef DEBUG_OUTPUT
21         [](struct wl_display *d) {
22            logdebug("wl::display ~display @ %p", d);
23            wl_display_disconnect(d);
24         })),
25 #else
26         &wl_display_disconnect)),
27 #endif
28      r(d.get()) {
29 }
30
31 display::~display() {}
32
33 bool display::ok() const { return d && wl_display_get_error(d.get()) == 0; }
34
35 void display::roundtrip() { wl_display_roundtrip(this->d.get()); }
36
37 int display::dispatch() { return wl_display_dispatch(this->d.get()); }
38
39 void display::flush() { wl_display_flush(this->d.get()); }
40
41 int display::get_fd() const { return wl_display_get_fd(this->d.get()); }
42
43 //                 _     _
44 //  _ __ ___  __ _(_)___| |_ _ __ _   _
45 // | '__/ _ \/ _` | / __| __| '__| | | |
46 // | | |  __/ (_| | \__ \ |_| |  | |_| |
47 // |_|  \___|\__, |_|___/\__|_|   \__, |
48 //           |___/                |___/
49 namespace {
50 void registry_global(void *data, struct wl_registry *r, uint32_t name,
51                      char const *iface, uint32_t v) {
52    static_cast<struct registry *>(data)->global(name, iface, v);
53 }
54
55 void registry_global_remove(void *data, struct wl_registry *r, uint32_t name) {
56    static_cast<struct registry *>(data)->global_remove(name);
57 }
58
59 constexpr struct wl_registry_listener registry_listener = {
60    registry_global, registry_global_remove};
61 }
62
63 registry::registry(struct wl_display *d)
64    : wayland_proxy(!d ? nullptr : wl_display_get_registry(d)) {
65    if (this->proxy)
66       wl_registry_add_listener(this->proxy, &registry_listener, this);
67 }
68
69 registry::~registry() {
70    logdebug("wl::registry %s @ %p", __func__, this->proxy);
71 }
72
73 void registry::add_global_handler(char const *iface, binder bind) {
74    this->bindings[iface] = bind;
75 }
76
77 void registry::global(uint32_t name, char const *iface, uint32_t v) {
78    auto b = this->bindings.find(iface);
79    if (b != this->bindings.end())
80       b->second(this->proxy, name, v);
81    logdebug("wl::registry @ %p global n %u i %s v %u", this->proxy, name, iface,
82             v);
83 }
84
85 void registry::global_remove(uint32_t name) {}
86
87 //              _               _
88 //   ___  _   _| |_ _ __  _   _| |_
89 //  / _ \| | | | __| '_ \| | | | __|
90 // | (_) | |_| | |_| |_) | |_| | |_
91 //  \___/ \__,_|\__| .__/ \__,_|\__|
92 //                 |_|
93 namespace {
94 void output_geometry(void *data, struct wl_output *wl_output, int32_t x,
95                      int32_t y, int32_t physical_width, int32_t physical_height,
96                      int32_t subpixel, const char *make, const char *model,
97                      int32_t transform) {
98    static_cast<struct output *>(data)->geometry(
99       x, y, physical_width, physical_height, subpixel, make, model, transform);
100 }
101
102 void output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
103                  int32_t width, int32_t height, int32_t refresh) {
104    static_cast<struct output *>(data)->mode(flags, width, height, refresh);
105 }
106
107 void output_done(void *data, struct wl_output *wl_output) {
108    static_cast<struct output *>(data)->done();
109 }
110
111 void output_scale(void *data, struct wl_output *wl_output, int32_t factor) {
112    static_cast<struct output *>(data)->scale(factor);
113 }
114
115 constexpr struct wl_output_listener output_listener = {
116    output_geometry, output_mode, output_done, output_scale};
117 }
118
119 output::output(struct wl_registry *r, uint32_t name, uint32_t v)
120    : wayland_proxy(wl_registry_bind(r, name, &wl_output_interface, v)) {
121    wl_output_add_listener(this->proxy, &output_listener, this);
122 }
123
124 void output::geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
125                       int32_t subpel, char const *make, char const *model,
126                       int32_t tx) {
127    logdebug(
128       "wl::output %s @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
129       __func__, this->proxy, x, y, pw, ph, subpel, make, model, tx);
130 }
131
132 void output::mode(uint32_t flags, int32_t w, int32_t h, int32_t r) {
133    logdebug("wl::output %s @ %p f %x w %i h %i r %i", __func__, this->proxy,
134             flags, w, h, r);
135    if (flags & WL_OUTPUT_MODE_CURRENT) {
136       this->width = w;
137       this->height = h;
138       this->refresh = r;
139    }
140 }
141
142 void output::done() {
143    logdebug("wl::output %s @ %p done", __func__, this->proxy);
144 }
145
146 void output::scale(int32_t factor) {
147    logdebug("wl::output %s @ %p f %i", __func__, this->proxy, factor);
148 }
149 }
150
151 //  _ __   __ _ _ __ ___   ___  ___ _ __   __ _  ___ ___
152 // | '_ \ / _` | '_ ` _ \ / _ \/ __| '_ \ / _` |/ __/ _ \
153 // | | | | (_| | | | | | |  __/\__ \ |_) | (_| | (_|  __/
154 // |_| |_|\__,_|_| |_| |_|\___||___/ .__/ \__,_|\___\___|
155 //                                 |_|
156 //                   _       _
157 //   __ _  ___ _ __ (_)_   _(_)
158 //  / _` |/ _ \ '_ \| \ \ / / |
159 // | (_| |  __/ | | | |\ V /| |
160 //  \__, |\___|_| |_|_| \_/ |_|
161 //  |___/
162 namespace genivi {
163
164 //                  _             _ _
165 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __
166 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
167 // | (_| (_) | | | | |_| | | (_) | | |  __/ |
168 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|
169 //
170 namespace {
171 void controller_screen(void *data, struct ivi_controller *ivi_controller,
172                        uint32_t id_screen,
173                        struct ivi_controller_screen *screen) {
174    static_cast<struct controller *>(data)->controller_screen(id_screen, screen);
175 }
176
177 void controller_layer(void *data, struct ivi_controller *ivi_controller,
178                       uint32_t id_layer) {
179    static_cast<struct controller *>(data)->controller_layer(id_layer);
180 }
181
182 void controller_surface(void *data, struct ivi_controller *ivi_controller,
183                         uint32_t id_surface) {
184    static_cast<struct controller *>(data)->controller_surface(id_surface);
185 }
186
187 void controller_error(void *data, struct ivi_controller *ivi_controller,
188                       int32_t object_id, int32_t object_type,
189                       int32_t error_code, const char *error_text) {
190    static_cast<struct controller *>(data)->controller_error(
191       object_id, object_type, error_code, error_text);
192 }
193
194 constexpr struct ivi_controller_listener listener = {
195    controller_screen, controller_layer, controller_surface, controller_error};
196 }
197
198 controller::controller(struct wl_registry *r, uint32_t name, uint32_t version)
199    : wayland_proxy(
200         wl_registry_bind(r, name, &ivi_controller_interface, version)),
201      surface_proxy_to_id{},
202      surfaces{},
203      layer_proxy_to_id{},
204      layers{},
205      screen_proxy_to_id{},
206      screens{},
207      pending{},
208      output_size{} {
209    ivi_controller_add_listener(this->proxy, &listener, this);
210 }
211
212 controller::~controller() {
213 }
214
215 void controller::layer_create(uint32_t id, int32_t w, int32_t h) {
216    this->layers[id] = std::make_unique<layer>(id, w, h, this);
217 }
218
219 void controller::surface_create(uint32_t id) {
220    this->surfaces[id] = std::make_unique<surface>(id, this);
221 }
222
223 void controller::controller_screen(uint32_t id,
224                                    struct ivi_controller_screen *screen) {
225    logdebug("genivi::controller @ %p screen %u (%x) @ %p", this->proxy, id, id,
226             screen);
227    this->screens[id] = std::make_unique<struct screen>(id, this, screen);
228 }
229
230 void controller::controller_layer(uint32_t id) {
231    logdebug("genivi::controller @ %p layer %u (%x)", this->proxy, id, id);
232    this->layers[id] = std::make_unique<struct layer>(id, this);
233 }
234
235 void controller::controller_surface(uint32_t id) {
236    logdebug("genivi::controller @ %p surface %u (%x)", this->proxy, id, id);
237    this->surfaces[id] = std::make_unique<struct surface>(id, this);
238
239    add_task("fullscreen surface", [id](struct controller *c) {
240       c->surfaces[id]->set_destination_rectangle(0, 0, c->output_size.w,
241                                                  c->output_size.h);
242       c->surfaces[id]->set_visibility(1);
243       uint32_t lid = id == 0x16180 ? 1000 : 100;
244       c->layers[lid]->add_surface(s.get());
245       logdebug("Surface %u now fullscreen on layer %u", id, lid);
246    });
247 }
248
249 void controller::controller_error(int32_t object_id, int32_t object_type,
250                                   int32_t error_code, const char *error_text) {
251    logdebug("genivi::controller @ %p error o %i t %i c %i text %s", this->proxy,
252             object_id, object_type, error_code, error_text);
253 }
254
255 //  _
256 // | | __ _ _   _  ___ _ __
257 // | |/ _` | | | |/ _ \ '__|
258 // | | (_| | |_| |  __/ |
259 // |_|\__,_|\__, |\___|_|
260 //          |___/
261 namespace {
262 void layer_visibility(void *data,
263                       struct ivi_controller_layer *ivi_controller_layer,
264                       int32_t visibility) {
265    static_cast<struct layer *>(data)->parent->layer_visibility(
266       static_cast<struct layer *>(data)->id, visibility);
267 }
268
269 void layer_opacity(void *data,
270                    struct ivi_controller_layer *ivi_controller_layer,
271                    wl_fixed_t opacity) {
272    static_cast<struct layer *>(data)->parent->layer_opacity(
273       static_cast<struct layer *>(data)->id,
274       float(wl_fixed_to_double(opacity)));
275 }
276
277 void layer_source_rectangle(void *data,
278                             struct ivi_controller_layer *ivi_controller_layer,
279                             int32_t x, int32_t y, int32_t width,
280                             int32_t height) {
281    static_cast<struct layer *>(data)->parent->layer_source_rectangle(
282       static_cast<struct layer *>(data)->id, x, y, width, height);
283 }
284
285 void layer_destination_rectangle(
286    void *data, struct ivi_controller_layer *ivi_controller_layer, int32_t x,
287    int32_t y, int32_t width, int32_t height) {
288    static_cast<struct layer *>(data)->parent->layer_destination_rectangle(
289       static_cast<struct layer *>(data)->id, x, y, width, height);
290 }
291
292 void layer_configuration(void *data,
293                          struct ivi_controller_layer *ivi_controller_layer,
294                          int32_t width, int32_t height) {
295    static_cast<struct layer *>(data)->parent->layer_configuration(
296       static_cast<struct layer *>(data)->id, width, height);
297 }
298
299 void layer_orientation(void *data,
300                        struct ivi_controller_layer *ivi_controller_layer,
301                        int32_t orientation) {
302    static_cast<struct layer *>(data)->parent->layer_orientation(
303       static_cast<struct layer *>(data)->id, orientation);
304 }
305
306 void layer_screen(void *data, struct ivi_controller_layer *ivi_controller_layer,
307                   struct wl_output *screen) {
308    static_cast<struct layer *>(data)->parent->layer_screen(
309       static_cast<struct layer *>(data)->id, screen);
310 }
311
312 void layer_destroyed(void *data,
313                      struct ivi_controller_layer *ivi_controller_layer) {
314    static_cast<struct layer *>(data)->parent->layer_destroyed(
315       static_cast<struct layer *>(data)->id);
316 }
317
318 constexpr struct ivi_controller_layer_listener layer_listener = {
319    layer_visibility,       layer_opacity,
320    layer_source_rectangle, layer_destination_rectangle,
321    layer_configuration,    layer_orientation,
322    layer_screen,           layer_destroyed,
323 };
324 }
325
326 layer::layer(uint32_t i, struct controller *c) : layer(i, 0, 0, c) {}
327
328 layer::layer(uint32_t i, int32_t w, int32_t h, struct controller *c)
329    : wayland_proxy(ivi_controller_layer_create(c->proxy, i, w, h)),
330      controller_child(c, i),
331      dst_rect{},
332      src_rect{},
333      size{},
334      orientation{},
335      visibility{},
336      opacity{} {
337    this->parent->add_proxy_to_id_mapping(this->proxy, i);
338    ivi_controller_layer_add_listener(this->proxy, &layer_listener, this);
339 }
340
341 layer::~layer() {
342    logdebug("%s layer %i @ %p", __func__, this->id, this->proxy);
343    this->parent->remove_proxy_to_id_mapping(this->proxy);
344    ivi_controller_layer_destroy(this->proxy, 1);
345    this->proxy = nullptr;
346 }
347
348 void controller::layer_visibility(uint32_t id, int32_t visibility) {
349    logdebug("genivi::layer %s @ %p v %i", __func__, this->proxy, visibility);
350    this->layers[id]->visibility = visibility;
351 }
352
353 void controller::layer_opacity(uint32_t id, float opacity) {
354    logdebug("genivi::layer %s @ %p o %f", __func__, this->proxy, opacity);
355    this->layers[id]->opacity = opacity;
356 }
357
358 void controller::layer_source_rectangle(uint32_t id, int32_t x, int32_t y,
359                                         int32_t width, int32_t height) {
360    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
361             x, y, width, height);
362    this->layers[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
363 }
364
365 void controller::layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
366                                              int32_t width, int32_t height) {
367    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
368             x, y, width, height);
369    this->layers[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
370 }
371
372 void controller::layer_configuration(uint32_t id, int32_t width,
373                                      int32_t height) {
374    logdebug("genivi::layer %s @ %p w %i h %i", __func__, this->proxy, width,
375             height);
376    this->layers[id]->size = size{uint32_t(width), uint32_t(height)};
377 }
378
379 void controller::layer_orientation(uint32_t id, int32_t orientation) {
380    logdebug("genivi::layer %s @ %p o %i", __func__, this->proxy, orientation);
381    this->layers[id]->orientation = orientation;
382 }
383
384 void controller::layer_screen(uint32_t id, struct wl_output *screen) {
385    logdebug("genivi::layer %s @ %p s %p", __func__, this->proxy, screen);
386 }
387
388 void controller::layer_destroyed(uint32_t id) {
389    logdebug("genivi::layer %s @ %p", __func__, this->proxy);
390 }
391
392 //                  __
393 //  ___ _   _ _ __ / _| __ _  ___ ___
394 // / __| | | | '__| |_ / _` |/ __/ _ \
395 // \__ \ |_| | |  |  _| (_| | (_|  __/
396 // |___/\__,_|_|  |_|  \__,_|\___\___|
397 //
398 namespace {
399
400 void surface_visibility(void *data,
401                         struct ivi_controller_surface *ivi_controller_surface,
402                         int32_t visibility) {
403    static_cast<struct surface *>(data)->parent->surface_visibility(
404       static_cast<struct surface *>(data)->id, visibility);
405 }
406
407 void surface_opacity(void *data,
408                      struct ivi_controller_surface *ivi_controller_surface,
409                      wl_fixed_t opacity) {
410    static_cast<struct surface *>(data)->parent->surface_opacity(
411       static_cast<struct surface *>(data)->id,
412       float(wl_fixed_to_double(opacity)));
413 }
414
415 void surface_source_rectangle(
416    void *data, struct ivi_controller_surface *ivi_controller_surface, int32_t x,
417    int32_t y, int32_t width, int32_t height) {
418    static_cast<struct surface *>(data)->parent->surface_source_rectangle(
419       static_cast<struct surface *>(data)->id, x, y, width, height);
420 }
421
422 void surface_destination_rectangle(
423    void *data, struct ivi_controller_surface *ivi_controller_surface, int32_t x,
424    int32_t y, int32_t width, int32_t height) {
425    static_cast<struct surface *>(data)->parent->surface_destination_rectangle(
426       static_cast<struct surface *>(data)->id, x, y, width, height);
427 }
428
429 void surface_configuration(
430    void *data, struct ivi_controller_surface *ivi_controller_surface,
431    int32_t width, int32_t height) {
432    static_cast<struct surface *>(data)->parent->surface_configuration(
433       static_cast<struct surface *>(data)->id, width, height);
434 }
435
436 void surface_orientation(void *data,
437                          struct ivi_controller_surface *ivi_controller_surface,
438                          int32_t orientation) {
439    static_cast<struct surface *>(data)->parent->surface_orientation(
440       static_cast<struct surface *>(data)->id, orientation);
441 }
442
443 void surface_pixelformat(void *data,
444                          struct ivi_controller_surface *ivi_controller_surface,
445                          int32_t pixelformat) {
446    static_cast<struct surface *>(data)->parent->surface_pixelformat(
447       static_cast<struct surface *>(data)->id, pixelformat);
448 }
449
450 void surface_layer(void *data,
451                    struct ivi_controller_surface *ivi_controller_surface,
452                    struct ivi_controller_layer *layer) {
453    static_cast<struct surface *>(data)->parent->surface_layer(
454       static_cast<struct surface *>(data)->id, layer);
455 }
456
457 void surface_stats(void *data,
458                    struct ivi_controller_surface *ivi_controller_surface,
459                    uint32_t redraw_count, uint32_t frame_count,
460                    uint32_t update_count, uint32_t pid,
461                    const char *process_name) {
462    static_cast<struct surface *>(data)->parent->surface_stats(
463       static_cast<struct surface *>(data)->id, redraw_count, frame_count,
464       update_count, pid, process_name);
465 }
466
467 void surface_destroyed(void *data,
468                        struct ivi_controller_surface *ivi_controller_surface) {
469    static_cast<struct surface *>(data)->parent->surface_destroyed(
470       static_cast<struct surface *>(data)->id);
471 }
472
473 void surface_content(void *data,
474                      struct ivi_controller_surface *ivi_controller_surface,
475                      int32_t content_state) {
476    static_cast<struct surface *>(data)->parent->surface_content(
477       static_cast<struct surface *>(data)->id, content_state);
478 }
479
480 constexpr struct ivi_controller_surface_listener surface_listener = {
481    surface_visibility,
482    surface_opacity,
483    surface_source_rectangle,
484    surface_destination_rectangle,
485    surface_configuration,
486    surface_orientation,
487    surface_pixelformat,
488    surface_layer,
489    surface_stats,
490    surface_destroyed,
491    surface_content,
492 };
493 }
494
495 surface::surface(uint32_t i, struct controller *c)
496    : wayland_proxy(ivi_controller_surface_create(c->proxy, i)),
497      controller_child(c, i),
498      dst_rect{},
499      src_rect{},
500      size{},
501      orientation{},
502      visibility{},
503      opacity{1.f} {
504    this->parent->add_proxy_to_id_mapping(this->proxy, i);
505    ivi_controller_surface_add_listener(this->proxy, &surface_listener, this);
506 }
507
508 surface::~surface() {
509    logdebug("%s surface %i @ %p", __func__, this->id, this->proxy);
510    this->parent->remove_proxy_to_id_mapping(this->proxy);
511    ivi_controller_surface_destroy(this->proxy, 1);
512    this->proxy = nullptr;
513 }
514
515 void controller::surface_visibility(uint32_t id, int32_t visibility) {
516    logdebug("genivi::surface %s @ %p v %i", __func__, this->proxy, visibility);
517    this->surfaces[id]->visibility = visibility;
518 }
519
520 void controller::surface_opacity(uint32_t id, float opacity) {
521    logdebug("genivi::surface %s @ %p o %f", __func__, this->proxy, opacity);
522    this->surfaces[id]->opacity = opacity;
523 }
524
525 void controller::surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
526                                           int32_t width, int32_t height) {
527    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
528             this->proxy, x, y, width, height);
529    this->surfaces[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
530 }
531
532 void controller::surface_destination_rectangle(uint32_t id, int32_t x,
533                                                int32_t y, int32_t width,
534                                                int32_t height) {
535    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
536             this->proxy, x, y, width, height);
537    this->surfaces[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
538 }
539
540 void controller::surface_configuration(uint32_t id, int32_t width,
541                                        int32_t height) {
542    logdebug("genivi::surface %s @ %p w %i h %i", __func__, this->proxy, width,
543             height);
544    this->surfaces[id]->size = size{uint32_t(width), uint32_t(height)};
545 }
546
547 void controller::surface_orientation(uint32_t id, int32_t orientation) {
548    logdebug("genivi::surface %s @ %p o %i", __func__, this->proxy, orientation);
549    this->surfaces[id]->orientation = orientation;
550 }
551
552 void controller::surface_pixelformat(uint32_t id, int32_t pixelformat) {
553    logdebug("genivi::surface %s @ %p f %i", __func__, this->proxy, pixelformat);
554 }
555
556 void controller::surface_layer(uint32_t id,
557                                struct ivi_controller_layer *layer) {
558    logdebug("genivi::surface %s @ %p l %u @ %p", __func__, this->proxy,
559             this->layer_proxy_to_id[uintptr_t(layer)], layer);
560 }
561
562 void controller::surface_stats(uint32_t id, uint32_t redraw_count,
563                                uint32_t frame_count, uint32_t update_count,
564                                uint32_t pid, const char *process_name) {
565    logdebug("genivi::surface %s @ %p r %u f %u u %u pid %u p %s", __func__,
566             this->proxy, redraw_count, frame_count, update_count, pid,
567             process_name);
568 }
569
570 void controller::surface_destroyed(uint32_t id) {
571    logdebug("genivi::surface %s @ %p", __func__, this->proxy);
572    this->surfaces.erase(id);
573 }
574
575 void controller::surface_content(uint32_t id, int32_t content_state) {
576    logdebug("genivi::surface %s @ %p s %i", __func__, this->proxy,
577             content_state);
578    if (content_state == IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED) {
579       add_task("remove surface",
580                [id](struct controller *c) { c->surfaces.erase(id); });
581    }
582 }
583
584 //
585 //  ___  ___ _ __ ___  ___ _ __
586 // / __|/ __| '__/ _ \/ _ \ '_ \
587 // \__ \ (__| | |  __/  __/ | | |
588 // |___/\___|_|  \___|\___|_| |_|
589 //
590 screen::screen(uint32_t i, struct controller *c,
591                struct ivi_controller_screen *p)
592    : wayland_proxy(p), controller_child(c, i) {
593    logdebug("genivi::screen @ %p id %u", p, i);
594 }
595 }