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