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