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