wlt: toolkit: fix sending two surface.attach() reqs in one frame
[platform/upstream/kmscon.git] / src / wlt_toolkit.c
1 /*
2  * wlt - Toolkit Helper
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Wayland Terminal toolkit helpers
28  */
29
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/mman.h>
35 #include <unistd.h>
36 #include <wayland-client.h>
37 #include <wayland-cursor.h>
38 #include <xkbcommon/xkbcommon.h>
39 #include "eloop.h"
40 #include "log.h"
41 #include "shl_dlist.h"
42 #include "shl_hook.h"
43 #include "tsm_vte.h"
44 #include "wlt_toolkit.h"
45
46 #define LOG_SUBSYSTEM "wlt_toolkit"
47
48 struct wlt_pool {
49         struct wl_shm_pool *w_pool;
50         size_t size;
51         uint8_t *data;
52 };
53
54 enum {
55         STATE_INIT = 0,
56         STATE_RUNNING,
57         STATE_HUP,
58 };
59
60 struct wlt_display {
61         unsigned long ref;
62         struct ev_eloop *eloop;
63         struct wl_display *dp;
64         struct ev_fd *dp_fd;
65         struct wl_global_listener *dp_listener;
66         struct shl_hook *listeners;
67         unsigned int state;
68
69         struct shl_dlist window_list;
70
71         struct wl_compositor *w_comp;
72         struct wl_seat *w_seat;
73         struct wl_shell *w_shell;
74         struct wl_shm *w_shm;
75
76         uint32_t last_serial;
77         uint32_t pointer_enter_serial;
78         struct wl_pointer *w_pointer;
79         struct wlt_window *pointer_focus;
80
81         uint32_t cursor_serial;
82         unsigned int current_cursor;
83         struct wl_surface *w_cursor_surface;
84         struct wl_cursor_theme *cursor_theme;
85         struct wl_cursor *cursors[WLT_CURSOR_NUM];
86
87         struct wl_keyboard *w_keyboard;
88         struct xkb_context *xkb_ctx;
89         struct xkb_keymap *xkb_keymap;
90         struct xkb_state *xkb_state;
91         struct wlt_window *keyboard_focus;
92 };
93
94 struct wlt_window {
95         unsigned long ref;
96         struct shl_dlist list;
97         void *data;
98         wlt_window_close_cb close_cb;
99         bool close_pending;
100         struct wlt_display *disp;
101         struct wlt_pool *pool;
102
103         struct wl_surface *w_surface;
104         struct wl_shell_surface *w_shell_surface;
105         struct wl_buffer *w_buffer;
106
107         bool buffer_attached;
108         bool skip_damage;
109         bool need_resize;
110         bool need_redraw;
111         bool need_frame;
112         bool idle_pending;
113         unsigned int new_width;
114         unsigned int new_height;
115         unsigned int resize_edges;
116         struct wlt_shm_buffer buffer;
117         struct wl_callback *w_frame;
118
119         struct shl_dlist widget_list;
120 };
121
122 struct wlt_widget {
123         struct shl_dlist list;
124         struct wlt_window *wnd;
125         void *data;
126         wlt_widget_redraw_cb redraw_cb;
127         wlt_widget_destroy_cb destroy_cb;
128         wlt_widget_prepare_resize_cb prepare_resize_cb;
129         wlt_widget_resize_cb resize_cb;
130         wlt_widget_pointer_enter_cb pointer_enter_cb;
131         wlt_widget_pointer_leave_cb pointer_leave_cb;
132         wlt_widget_pointer_motion_cb pointer_motion_cb;
133         wlt_widget_pointer_button_cb pointer_button_cb;
134         wlt_widget_keyboard_cb keyboard_cb;
135 };
136
137 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
138                         size_t size)
139 {
140         static const char template[] = "/wlterm-shared-XXXXXX";
141         struct wlt_pool *pool;
142         int fd, ret;
143         const char *path;
144         char *name;
145
146         path = getenv("XDG_RUNTIME_DIR");
147         if (!path) {
148                 log_error("XDG_RUNTIME_DIR not set");
149                 return -EFAULT;
150         }
151
152         pool = malloc(sizeof(*pool));
153         if (!pool)
154                 return -ENOMEM;
155         memset(pool, 0, sizeof(*pool));
156         pool->size = size;
157
158         name = malloc(strlen(path) + sizeof(template));
159         if (!name) {
160                 ret = -ENOMEM;
161                 goto err_free;
162         }
163         strcpy(name, path);
164         strcat(name, template);
165
166         fd = mkostemp(name, O_CLOEXEC);
167         if (fd < 0) {
168                 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
169                           name, errno);
170                 ret = -EFAULT;
171                 free(name);
172                 goto err_free;
173         }
174
175         ret = unlink(name);
176         if (ret)
177                 log_warning("cannot unlink temporary file %s (%d): %m",
178                             name, errno);
179         free(name);
180
181         ret = ftruncate(fd, size);
182         if (ret) {
183                 log_error("cannot truncate temporary file to length %lu (%d): %m",
184                           size, errno);
185                 ret = -EFAULT;
186                 goto err_close;
187         }
188
189         pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
190                           fd, 0);
191         if (pool->data == MAP_FAILED) {
192                 log_error("cannot mmap temporary file (%d): %m", errno);
193                 ret = -EFAULT;
194                 goto err_close;
195         }
196
197         pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
198         if (!pool->w_pool) {
199                 log_error("cannot create wayland shm pool object");
200                 ret = -EFAULT;
201                 goto err_map;
202         }
203
204         close(fd);
205         *out = pool;
206         return 0;
207
208 err_map:
209         munmap(pool->data, size);
210 err_close:
211         close(fd);
212 err_free:
213         free(pool);
214         return ret;
215 }
216
217 static void wlt_pool_free(struct wlt_pool *pool)
218 {
219         munmap(pool->data, pool->size);
220         wl_shm_pool_destroy(pool->w_pool);
221         free(pool);
222 }
223
224 /*
225  * These cursor-names are based on:
226  *   https://bugs.kde.org/attachment.cgi?id=67313
227  */
228
229 static char *(*cursors[])[] = {
230         [WLT_CURSOR_NONE] = &(char*[]) {
231                 NULL,
232         },
233         [WLT_CURSOR_TOP] = &(char*[]) {
234                 "top_side",
235                 "n-resize",
236                 NULL,
237         },
238         [WLT_CURSOR_BOTTOM] = &(char*[]) {
239                 "bottom_side",
240                 "s-resize",
241                 NULL,
242         },
243         [WLT_CURSOR_LEFT] = &(char*[]) {
244                 "left_side",
245                 "w-resize",
246                 NULL,
247         },
248         [WLT_CURSOR_RIGHT] = &(char*[]) {
249                 "right_side",
250                 "e-resize",
251                 NULL,
252         },
253         [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
254                 "top_left_corner",
255                 "nw-resize",
256                 NULL,
257         },
258         [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
259                 "top_right_corner",
260                 "ne-resize",
261                 NULL,
262         },
263         [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
264                 "bottom_left_corner",
265                 "sw-resize",
266                 NULL,
267         },
268         [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
269                 "bottom_right_corner",
270                 "se-resize",
271                 NULL,
272         },
273         [WLT_CURSOR_DRAGGING] = &(char*[]) {
274                 "grabbing",
275                 "closedhand",
276                 "208530c400c041818281048008011002",
277                 NULL,
278         },
279         [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
280                 "left_ptr",
281                 "default",
282                 "top_left_arrow",
283                 "left-arrow",
284                 NULL,
285         },
286         [WLT_CURSOR_IBEAM] = &(char*[]) {
287                 "xterm",
288                 "ibeam",
289                 "text",
290                 NULL,
291         },
292 };
293
294 static int load_cursors(struct wlt_display *disp)
295 {
296         unsigned int i, j;
297         size_t size = sizeof(cursors) / sizeof(*cursors);
298         struct wl_cursor *cursor;
299
300         disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
301
302         disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
303         if (!disp->cursor_theme) {
304                 log_warning("cannot load curdor theme");
305                 return -EFAULT;
306         }
307
308         for (i = 0; i < WLT_CURSOR_NUM; ++i) {
309                 cursor = NULL;
310                 if (i < size && cursors[i]) {
311                         for (j = 0; (*cursors[i])[j]; ++j) {
312                                 cursor = wl_cursor_theme_get_cursor(
313                                                         disp->cursor_theme,
314                                                         (*cursors[i])[j]);
315                                 if (cursor)
316                                         break;
317                         }
318                 }
319
320                 if (!cursor && i != WLT_CURSOR_NONE)
321                         log_warning("cannot load cursor for ID %d", i);
322
323                 disp->cursors[i] = cursor;
324         }
325
326         return 0;
327 }
328
329 static void unload_cursors(struct wlt_display *disp)
330 {
331         if (disp->cursor_theme)
332                 wl_cursor_theme_destroy(disp->cursor_theme);
333         if (disp->w_cursor_surface)
334                 wl_surface_destroy(disp->w_cursor_surface);
335 }
336
337 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
338 {
339         bool force = false;
340         struct wl_cursor *cur;
341         struct wl_cursor_image *image;
342         struct wl_buffer *buffer;
343
344         if (cursor >= WLT_CURSOR_NUM)
345                 cursor = WLT_CURSOR_LEFT_PTR;
346
347         if (disp->pointer_enter_serial > disp->cursor_serial)
348                 force = true;
349
350         if (!force && cursor == disp->current_cursor)
351                 return;
352
353         disp->current_cursor = cursor;
354         disp->cursor_serial = disp->pointer_enter_serial;
355
356         cur = disp->cursors[cursor];
357         if (!cur) {
358                 wl_pointer_set_cursor(disp->w_pointer,
359                                       disp->pointer_enter_serial,
360                                       NULL, 0, 0);
361                 return;
362         }
363
364         image = cur->images[0];
365         buffer = wl_cursor_image_get_buffer(image);
366         if (!buffer) {
367                 log_error("cannot load buffer for cursor image");
368                 return;
369         }
370
371         wl_pointer_set_cursor(disp->w_pointer,
372                               disp->pointer_enter_serial,
373                               disp->w_cursor_surface,
374                               image->hotspot_x, image->hotspot_y);
375         wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
376         wl_surface_damage(disp->w_cursor_surface, 0, 0,
377                           image->width, image->height);
378 }
379
380 static int dp_mask_update(uint32_t mask, void *data)
381 {
382         struct wlt_display *disp = data;
383         int ret;
384         int mode;
385
386         if (!disp->dp_fd)
387                 return 0;
388
389         mode = 0;
390         if (mask & WL_DISPLAY_READABLE)
391                 mode |= EV_READABLE;
392         if (mask & WL_DISPLAY_WRITABLE)
393                 mode |= EV_WRITEABLE;
394
395         ret = ev_fd_update(disp->dp_fd, mode);
396         if (ret)
397                 log_warning("cannot update wayland-fd event-polling modes (%d)",
398                             ret);
399
400         return 0;
401 }
402
403 static void dp_event(struct ev_fd *fd, int mask, void *data)
404 {
405         struct wlt_display *disp = data;
406         int mode;
407
408         if (mask & (EV_HUP | EV_ERR)) {
409                 log_warning("HUP/ERR on wayland socket");
410                 disp->state = STATE_HUP;
411                 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
412                 ev_eloop_rm_fd(disp->dp_fd);
413                 disp->dp_fd = NULL;
414                 return;
415         }
416
417         mode = 0;
418         if (mask & EV_READABLE)
419                 mode |= WL_DISPLAY_READABLE;
420         if (mask & EV_WRITEABLE)
421                 mode |= WL_DISPLAY_WRITABLE;
422
423         if (mode)
424                 wl_display_iterate(disp->dp, mode);
425 }
426
427 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
428                           uint32_t serial, struct wl_surface *w_surface,
429                           wl_fixed_t x, wl_fixed_t y)
430 {
431         struct wlt_display *disp = data;
432         struct shl_dlist *iter;
433         struct wlt_window *wnd;
434         struct wlt_widget *widget;
435
436         if (!w_surface)
437                 return;
438
439         shl_dlist_for_each(iter, &disp->window_list) {
440                 wnd = shl_dlist_entry(iter, struct wlt_window, list);
441                 if (wnd->w_surface == w_surface)
442                         break;
443         }
444
445         if (iter == &disp->window_list) {
446                 log_debug("unknown surface");
447                 return;
448         }
449
450         disp->pointer_enter_serial = serial;
451         disp->last_serial = serial;
452         disp->pointer_focus = wnd;
453         shl_dlist_for_each(iter, &wnd->widget_list) {
454                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
455                 if (widget->pointer_enter_cb)
456                         widget->pointer_enter_cb(widget, x >> 8, y >> 8,
457                                                  widget->data);
458         }
459 }
460
461 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
462                           uint32_t serial, struct wl_surface *w_surface)
463 {
464         struct wlt_display *disp = data;
465         struct shl_dlist *iter;
466         struct wlt_window *wnd;
467         struct wlt_widget *widget;
468
469         wnd = disp->pointer_focus;
470         disp->pointer_focus = NULL;
471         disp->last_serial = serial;
472
473         if (!wnd)
474                 return;
475
476         shl_dlist_for_each(iter, &wnd->widget_list) {
477                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
478                 if (widget->pointer_leave_cb)
479                         widget->pointer_leave_cb(widget, widget->data);
480         }
481 }
482
483 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
484                            uint32_t time, wl_fixed_t x, wl_fixed_t y)
485 {
486         struct wlt_display *disp = data;
487         struct shl_dlist *iter;
488         struct wlt_window *wnd;
489         struct wlt_widget *widget;
490
491         wnd = disp->pointer_focus;
492         if (!wnd)
493                 return;
494
495         shl_dlist_for_each(iter, &wnd->widget_list) {
496                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
497                 if (widget->pointer_motion_cb)
498                         widget->pointer_motion_cb(widget, x >> 8, y >> 8,
499                                                   widget->data);
500         }
501 }
502
503 static void pointer_button(void *data, struct wl_pointer *w_pointer,
504                            uint32_t serial, uint32_t time, uint32_t button,
505                            uint32_t state)
506 {
507         struct wlt_display *disp = data;
508         struct shl_dlist *iter;
509         struct wlt_window *wnd;
510         struct wlt_widget *widget;
511
512         wnd = disp->pointer_focus;
513         disp->last_serial = serial;
514
515         if (!wnd)
516                 return;
517
518         shl_dlist_for_each(iter, &wnd->widget_list) {
519                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
520                 if (widget->pointer_button_cb)
521                         widget->pointer_button_cb(widget, button, state,
522                                                   widget->data);
523         }
524 }
525
526 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
527                          uint32_t time, uint32_t axis, wl_fixed_t value)
528 {
529 }
530
531 static const struct wl_pointer_listener pointer_listener = {
532         .enter = pointer_enter,
533         .leave = pointer_leave,
534         .motion = pointer_motion,
535         .button = pointer_button,
536         .axis = pointer_axis,
537 };
538
539 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
540                             uint32_t format, int fd, uint32_t size)
541 {
542         struct wlt_display *disp = data;
543         char *map;
544
545         if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
546                 log_error("invalid keyboard format");
547                 close(fd);
548                 return;
549         }
550
551         map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
552         if (map == MAP_FAILED) {
553                 log_error("cannot mmap keyboard keymap");
554                 close(fd);
555                 return;
556         }
557
558         disp->xkb_keymap = xkb_map_new_from_string(disp->xkb_ctx, map,
559                                                    XKB_KEYMAP_FORMAT_TEXT_V1,
560                                                    0);
561         munmap(map, size);
562         close(fd);
563
564         if (!disp->xkb_keymap) {
565                 log_error("cannot create xkb keymap");
566                 return;
567         }
568
569         disp->xkb_state = xkb_state_new(disp->xkb_keymap);
570         if (!disp->xkb_state) {
571                 log_error("cannot create XKB state object");
572                 xkb_map_unref(disp->xkb_keymap);
573                 disp->xkb_keymap = NULL;
574                 return;
575         }
576 }
577
578 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
579                            uint32_t serial, struct wl_surface *w_surface,
580                            struct wl_array *keys)
581 {
582         struct wlt_display *disp = data;
583         struct shl_dlist *iter;
584         struct wlt_window *wnd;
585
586         disp->last_serial = serial;
587         if (!disp->xkb_state)
588                 return;
589
590         if (!w_surface)
591                 return;
592
593         shl_dlist_for_each(iter, &disp->window_list) {
594                 wnd = shl_dlist_entry(iter, struct wlt_window, list);
595                 if (wnd->w_surface == w_surface)
596                         break;
597         }
598
599         if (iter == &disp->window_list)
600                 return;
601
602         disp->keyboard_focus = wnd;
603 }
604
605 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
606                            uint32_t serial, struct wl_surface *surface)
607 {
608         struct wlt_display *disp = data;
609
610         disp->last_serial = serial;
611         disp->keyboard_focus = NULL;
612 }
613
614 static unsigned int get_effective_modmask(struct xkb_state *state)
615 {
616         unsigned int mods = 0;
617
618         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT,
619                                                 XKB_STATE_EFFECTIVE))
620                 mods |= TSM_SHIFT_MASK;
621         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
622                                                 XKB_STATE_EFFECTIVE))
623                 mods |= TSM_LOCK_MASK;
624         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
625                                                 XKB_STATE_EFFECTIVE))
626                 mods |= TSM_CONTROL_MASK;
627         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
628                                                 XKB_STATE_EFFECTIVE))
629                 mods |= TSM_MOD1_MASK;
630         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO,
631                                                 XKB_STATE_EFFECTIVE))
632                 mods |= TSM_MOD4_MASK;
633
634         return mods;
635 }
636
637 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
638                          uint32_t serial, uint32_t time, uint32_t key,
639                          uint32_t state_w)
640 {
641         struct wlt_display *disp = data;
642         struct wlt_window *wnd = disp->keyboard_focus;
643         uint32_t code, num_syms;
644         unsigned int mask;
645         enum wl_keyboard_key_state state = state_w;
646         const xkb_keysym_t *syms;
647         xkb_keysym_t sym;
648         struct shl_dlist *iter;
649         struct wlt_widget *widget;
650
651         disp->last_serial = serial;
652         if (!disp->xkb_state)
653                 return;
654
655         code = key + 8;
656         if (!wnd)
657                 return;
658
659         mask = get_effective_modmask(disp->xkb_state);
660         num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
661         sym = XKB_KEY_NoSymbol;
662         if (num_syms == 1)
663                 sym = syms[0];
664
665         shl_dlist_for_each(iter, &wnd->widget_list) {
666                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
667                 if (widget->keyboard_cb)
668                         widget->keyboard_cb(widget, mask, sym, state,
669                                             widget->data);
670         }
671 }
672
673 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
674                                uint32_t serial, uint32_t mods_depressed,
675                                uint32_t mods_latched, uint32_t mods_locked,
676                                uint32_t group)
677 {
678         struct wlt_display *disp = data;
679
680         disp->last_serial = serial;
681         if (!disp->xkb_state)
682                 return;
683
684         xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
685                               mods_locked, 0, 0, group);
686 }
687
688 static const struct wl_keyboard_listener keyboard_listener = {
689         .keymap = keyboard_keymap,
690         .enter = keyboard_enter,
691         .leave = keyboard_leave,
692         .key = keyboard_key,
693         .modifiers = keyboard_modifiers,
694 };
695
696 static void check_ready(struct wlt_display *disp)
697 {
698         if (disp->state > STATE_INIT)
699                 return;
700
701         if (disp->w_comp &&
702             disp->w_seat &&
703             disp->w_shell &&
704             disp->w_shm &&
705             disp->w_pointer &&
706             disp->w_keyboard) {
707                 log_debug("wayland display initialized");
708                 load_cursors(disp);
709
710                 disp->state = STATE_RUNNING;
711                 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
712         }
713 }
714
715 static void seat_capabilities(void *data, struct wl_seat *seat,
716                               enum wl_seat_capability caps)
717 {
718         struct wlt_display *disp = data;
719
720         if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
721                 disp->w_pointer = wl_seat_get_pointer(seat);
722                 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
723                                         disp);
724         }
725
726         if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
727                 disp->w_keyboard = wl_seat_get_keyboard(seat);
728                 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
729                                          disp);
730         }
731
732         check_ready(disp);
733 }
734
735 static const struct wl_seat_listener seat_listener = {
736         .capabilities = seat_capabilities,
737 };
738
739 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
740                       uint32_t version, void *data)
741 {
742         struct wlt_display *disp = data;
743
744         if (!strcmp(iface, "wl_display")) {
745                 log_debug("new wl_display global");
746                 return;
747         } else if (!strcmp(iface, "wl_compositor")) {
748                 if (disp->w_comp) {
749                         log_error("global wl_compositor advertised twice");
750                         return;
751                 }
752                 disp->w_comp = wl_display_bind(disp->dp,
753                                                id,
754                                                &wl_compositor_interface);
755                 if (!disp->w_comp) {
756                         log_error("cannot bind wl_compositor object");
757                         return;
758                 }
759         } else if (!strcmp(iface, "wl_seat")) {
760                 if (disp->w_seat) {
761                         log_error("global wl_seat advertised twice");
762                         return;
763                 }
764                 disp->w_seat = wl_display_bind(disp->dp,
765                                                id,
766                                                &wl_seat_interface);
767                 if (!disp->w_seat) {
768                         log_error("cannot bind wl_seat object");
769                         return;
770                 }
771
772                 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
773         } else if (!strcmp(iface, "wl_shell")) {
774                 if (disp->w_shell) {
775                         log_error("global wl_shell advertised twice");
776                         return;
777                 }
778                 disp->w_shell = wl_display_bind(disp->dp,
779                                                 id,
780                                                 &wl_shell_interface);
781                 if (!disp->w_shell) {
782                         log_error("cannot bind wl_shell object");
783                         return;
784                 }
785         } else if (!strcmp(iface, "wl_shm")) {
786                 if (disp->w_shm) {
787                         log_error("global wl_shm advertised twice");
788                         return;
789                 }
790                 disp->w_shm = wl_display_bind(disp->dp,
791                                               id,
792                                               &wl_shm_interface);
793                 if (!disp->w_shm) {
794                         log_error("cannot bind wl_shm object");
795                         return;
796                 }
797         } else {
798                 log_debug("ignoring new unknown global %s", iface);
799                 return;
800         }
801
802         log_debug("new global %s", iface);
803
804         check_ready(disp);
805 }
806
807 int wlt_display_new(struct wlt_display **out,
808                     struct ev_eloop *eloop)
809 {
810         struct wlt_display *disp;
811         int ret;
812
813         if (!out || !eloop)
814                 return -EINVAL;
815
816         log_debug("creating new wlt-display");
817
818         disp = malloc(sizeof(*disp));
819         if (!disp)
820                 return -ENOMEM;
821         memset(disp, 0, sizeof(*disp));
822         disp->ref = 1;
823         disp->eloop = eloop;
824         shl_dlist_init(&disp->window_list);
825
826         ret = shl_hook_new(&disp->listeners);
827         if (ret) {
828                 log_error("cannot create listeners hook");
829                 goto err_free;
830         }
831
832         disp->dp = wl_display_connect(NULL);
833         if (!disp->dp) {
834                 log_error("cannot connect to wayland socket (%d): %m", errno);
835                 ret = -EFAULT;
836                 goto err_listener;
837         }
838
839         ret = ev_eloop_new_fd(eloop,
840                               &disp->dp_fd,
841                               wl_display_get_fd(disp->dp,
842                                                 dp_mask_update,
843                                                 disp),
844                               EV_READABLE,
845                               dp_event,
846                               disp);
847         if (ret) {
848                 log_error("cannot create event-fd for wayland display (%d)",
849                           ret);
850                 goto err_dp;
851         }
852
853         disp->dp_listener = wl_display_add_global_listener(disp->dp,
854                                                            dp_global,
855                                                            disp);
856         if (!disp->dp_listener) {
857                 log_error("cannot add wayland global listener (%d)");
858                 goto err_dp_fd;
859         }
860
861         disp->xkb_ctx = xkb_context_new(0);
862         if (!disp->xkb_ctx) {
863                 log_error("cannot create XKB context");
864                 goto err_dp_fd;
865         }
866
867         log_debug("wlt-display waiting for globals...");
868
869         ev_eloop_ref(disp->eloop);
870         *out = disp;
871         return 0;
872
873 err_dp_fd:
874         ev_eloop_rm_fd(disp->dp_fd);
875 err_dp:
876         wl_display_disconnect(disp->dp);
877 err_listener:
878         shl_hook_free(disp->listeners);
879 err_free:
880         free(disp);
881         return ret;
882 }
883
884 void wlt_display_ref(struct wlt_display *disp)
885 {
886         if (!disp || !disp->ref)
887                 return;
888
889         ++disp->ref;
890 }
891
892 void wlt_display_unref(struct wlt_display *disp)
893 {
894         if (!disp || !disp->ref || --disp->ref)
895                 return;
896
897         unload_cursors(disp);
898         wl_display_remove_global_listener(disp->dp, disp->dp_listener);
899         ev_eloop_rm_fd(disp->dp_fd);
900         wl_display_flush(disp->dp);
901         wl_display_disconnect(disp->dp);
902         xkb_context_unref(disp->xkb_ctx);
903         shl_hook_free(disp->listeners);
904         ev_eloop_unref(disp->eloop);
905         free(disp);
906 }
907
908 int wlt_display_register_cb(struct wlt_display *disp,
909                             wlt_display_cb cb, void *data)
910 {
911         if (!disp)
912                 return -EINVAL;
913
914         return shl_hook_add_cast(disp->listeners, cb, data);
915 }
916
917 void wlt_display_unregister_cb(struct wlt_display *disp,
918                                wlt_display_cb cb, void *data)
919 {
920         if (!disp)
921                 return;
922
923         shl_hook_rm_cast(disp->listeners, cb, data);
924 }
925
926 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
927                                uint32_t serial)
928 {
929         wl_shell_surface_pong(s, serial);
930 }
931
932 static void wlt_window_do_redraw(struct wlt_window *wnd,
933                                  unsigned int oldw,
934                                  unsigned int oldh)
935 {
936         struct shl_dlist *iter;
937         struct wlt_widget *widget;
938         unsigned int x, y;
939         struct wlt_rect alloc;
940
941         alloc.x = 0;
942         alloc.y = 0;
943         alloc.width = wnd->buffer.width;
944         alloc.height = wnd->buffer.height;
945         shl_dlist_for_each(iter, &wnd->widget_list) {
946                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
947                 if (widget->resize_cb)
948                         widget->resize_cb(widget, &alloc, widget->data);
949         }
950
951         memset(wnd->buffer.data, 0,
952                wnd->buffer.stride * wnd->buffer.height);
953
954         wnd->skip_damage = true;
955         shl_dlist_for_each(iter, &wnd->widget_list) {
956                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
957                 if (widget->redraw_cb)
958                         widget->redraw_cb(widget, widget->data);
959         }
960         wnd->skip_damage = false;
961
962         if (!wnd->buffer_attached) {
963                 wnd->buffer_attached = true;
964                 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
965                         x = (int)oldw - wnd->buffer.width;
966                 else
967                         x = 0;
968                 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
969                         y = (int)oldh - wnd->buffer.height;
970                 else
971                         y = 0;
972                 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
973                 wnd->resize_edges = 0;
974         }
975
976         wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
977                           wnd->buffer.height);
978 }
979
980 static int resize_window(struct wlt_window *wnd, unsigned int width,
981                          unsigned int height)
982 {
983         struct shl_dlist *iter;
984         struct wlt_widget *widget;
985         struct wl_buffer *old_buffer = NULL;
986         struct wlt_pool *old_pool = NULL;
987         size_t nsize;
988         int ret;
989         unsigned int oldw, oldh;
990
991         if (!wnd || !width || !height)
992                 return -EINVAL;
993
994         shl_dlist_for_each(iter, &wnd->widget_list) {
995                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
996                 if (widget->prepare_resize_cb)
997                         widget->prepare_resize_cb(widget, &width, &height,
998                                                   widget->data);
999         }
1000
1001         if (width == wnd->buffer.width &&
1002             height == wnd->buffer.height)
1003                 return 0;
1004
1005         oldw = wnd->buffer.width;
1006         oldh = wnd->buffer.height;
1007
1008         nsize = width * height * 4;
1009         if (wnd->pool && wnd->pool->size >= nsize) {
1010                 old_buffer = wnd->w_buffer;
1011                 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1012                                                 0, width, height,
1013                                                 width * 4,
1014                                                 WL_SHM_FORMAT_ARGB8888);
1015                 if (!wnd->w_buffer) {
1016                         log_error("cannot create wayland shm buffer");
1017                         wnd->w_buffer = old_buffer;
1018                         return -ENOMEM;
1019                 }
1020         } else {
1021                 old_pool = wnd->pool;
1022                 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1023                 if (ret) {
1024                         log_error("cannot create memory pool");
1025                         wnd->pool = old_pool;
1026                         return ret;
1027                 }
1028
1029                 old_buffer = wnd->w_buffer;
1030                 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1031                                                 0, width, height,
1032                                                 width * 4,
1033                                                 WL_SHM_FORMAT_ARGB8888);
1034                 if (!wnd->w_buffer) {
1035                         log_error("cannot create wayland shm buffer");
1036                         wnd->w_buffer = old_buffer;
1037                         wlt_pool_free(wnd->pool);
1038                         wnd->pool = old_pool;
1039                         return -ENOMEM;
1040                 }
1041         }
1042
1043         wnd->buffer.data = wnd->pool->data;
1044         wnd->buffer.width = width;
1045         wnd->buffer.height = height;
1046         wnd->buffer.stride = width * 4;
1047         wnd->buffer_attached = false;
1048
1049         wlt_window_do_redraw(wnd, oldw, oldh);
1050
1051         if (old_buffer)
1052                 wl_buffer_destroy(old_buffer);
1053         if (old_pool)
1054                 wlt_pool_free(old_pool);
1055
1056         return 0;
1057 }
1058
1059 static void frame_callback(void *data, struct wl_callback *w_callback,
1060                            uint32_t time);
1061 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1062
1063 static const struct wl_callback_listener frame_callback_listener = {
1064         .done = frame_callback,
1065 };
1066
1067 static void do_frame(struct wlt_window *wnd)
1068 {
1069         wnd->idle_pending = false;
1070         ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1071
1072         if (wnd->need_resize) {
1073                 wnd->need_frame = true;
1074                 wnd->need_resize = false;
1075                 wnd->need_redraw = false;
1076                 resize_window(wnd, wnd->new_width, wnd->new_height);
1077         }
1078
1079         if (wnd->need_redraw) {
1080                 wnd->need_frame = true;
1081                 wnd->need_redraw = false;
1082                 wlt_window_do_redraw(wnd, wnd->buffer.width,
1083                                      wnd->buffer.height);
1084         }
1085
1086         if (wnd->need_frame) {
1087                 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1088                 wl_callback_add_listener(wnd->w_frame,
1089                                          &frame_callback_listener, wnd);
1090         }
1091 }
1092
1093 static void frame_callback(void *data, struct wl_callback *w_callback,
1094                            uint32_t time)
1095 {
1096         struct wlt_window *wnd = data;
1097
1098         wl_callback_destroy(w_callback);
1099         wnd->w_frame = NULL;
1100         wnd->need_frame = false;
1101
1102         do_frame(wnd);
1103 }
1104
1105 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1106 {
1107         struct wlt_window *wnd = data;
1108
1109         do_frame(wnd);
1110 }
1111
1112 /*
1113  * Buffer Handling and Frame Scheduling
1114  * We use wl_shm for buffer allocation. This means, we have a single buffer
1115  * client side and the server loads it into its backbuffer for rendering. If the
1116  * server does not do this, we are screwed anyway, but that's on behalf of the
1117  * server, so we don't care.
1118  *
1119  * However, this means, when we create a buffer, we need to notify the
1120  * compositor and then wait until the compositor has created its back-buffer,
1121  * before we continue using this buffer. We can use the "frame" callback to get
1122  * notified about this.
1123  *
1124  * The logic we have is:
1125  * You set the boolean flags what action is needed in wlt_window and then call
1126  * "schedule_frame()". If we didn't already do any buffer operations in this
1127  * frame, then this function schedules an idle-callback which then performs
1128  * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1129  * that this frame was already used and schedules a frame-callback.
1130  * If during this time another call to schedule_frame() is made, we do nothing
1131  * but wait for the frame-callback. It will then directly perform all the
1132  * requested functions and reschedule a frame-callback.
1133  * If nothing was schedule for one frame, we have no more interest in
1134  * frame-callbacks and thus we set "need_frame" to false again and don't
1135  * schedule any more frame-callbacks.
1136  */
1137 static void schedule_frame(struct wlt_window *wnd)
1138 {
1139         int ret;
1140
1141         if (!wnd || wnd->w_frame)
1142                 return;
1143
1144         if (wnd->need_frame || wnd->idle_pending)
1145                 return;
1146
1147         ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1148         if (ret)
1149                 log_error("cannot schedule idle callback: %d", ret);
1150         else
1151                 wnd->idle_pending = true;
1152 }
1153
1154 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1155                                     uint32_t edges, int32_t width,
1156                                     int32_t height)
1157 {
1158         struct wlt_window *wnd = data;
1159
1160         if (width <= 0)
1161                 width = 1;
1162         if (height <= 0)
1163                 height = 1;
1164
1165         wnd->resize_edges = edges;
1166         wlt_window_set_size(wnd, width, height);
1167 }
1168
1169 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1170 {
1171 }
1172
1173 static const struct wl_shell_surface_listener shell_surface_listener = {
1174         .ping = shell_surface_ping,
1175         .configure = shell_surface_configure,
1176         .popup_done = shell_surface_popup_done,
1177 };
1178
1179 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1180 {
1181         struct wlt_window *wnd = data;
1182
1183         ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1184         wnd->close_pending = false;
1185
1186         if (wnd->close_cb)
1187                 wnd->close_cb(wnd, wnd->data);
1188 }
1189
1190 int wlt_display_create_window(struct wlt_display *disp,
1191                               struct wlt_window **out,
1192                               unsigned int width,
1193                               unsigned int height,
1194                               void *data)
1195 {
1196         struct wlt_window *wnd;
1197         int ret;
1198
1199         if (!disp || !out || !width || !height)
1200                 return -EINVAL;
1201
1202         if (disp->state != STATE_RUNNING) {
1203                 log_error("cannot create window, display is not running but in state %u",
1204                           disp->state);
1205                 return -EBUSY;
1206         }
1207
1208         wnd = malloc(sizeof(*wnd));
1209         if (!wnd)
1210                 return -ENOMEM;
1211         memset(wnd, 0, sizeof(*wnd));
1212         wnd->ref = 1;
1213         wnd->disp = disp;
1214         wnd->data = data;
1215         shl_dlist_init(&wnd->widget_list);
1216
1217         wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1218         if (!wnd->w_surface) {
1219                 log_error("cannot create wayland surface");
1220                 ret = -EFAULT;
1221                 goto err_free;
1222         }
1223
1224         wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1225                                                           wnd->w_surface);
1226         if (!wnd->w_shell_surface) {
1227                 log_error("cannot retrieve shell-surface for wayland surface");
1228                 ret = -EFAULT;
1229                 goto err_surface;
1230         }
1231
1232         wl_shell_surface_add_listener(wnd->w_shell_surface,
1233                                       &shell_surface_listener, wnd);
1234         wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1235
1236         ret = resize_window(wnd, width, height);
1237         if (ret)
1238                 goto err_shell_surface;
1239
1240         wlt_display_ref(disp);
1241         shl_dlist_link(&disp->window_list, &wnd->list);
1242         *out = wnd;
1243         return 0;
1244
1245 err_shell_surface:
1246         wl_shell_surface_destroy(wnd->w_shell_surface);
1247 err_surface:
1248         wl_surface_destroy(wnd->w_surface);
1249 err_free:
1250         free(wnd);
1251         return ret;
1252 }
1253
1254 void wlt_window_ref(struct wlt_window *wnd)
1255 {
1256         if (!wnd || !wnd->ref)
1257                 return;
1258
1259         ++wnd->ref;
1260 }
1261
1262 void wlt_window_unref(struct wlt_window *wnd)
1263 {
1264         struct wlt_widget *widget;
1265
1266         if (!wnd || !wnd->ref || --wnd->ref)
1267                 return;
1268
1269         while (!shl_dlist_empty(&wnd->widget_list)) {
1270                 widget = shl_dlist_entry(wnd->widget_list.next,
1271                                          struct wlt_widget, list);
1272                 wlt_widget_destroy(widget);
1273         }
1274
1275         if (wnd->close_pending)
1276                 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1277                                             wnd);
1278         if (wnd->idle_pending)
1279                 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1280         shl_dlist_unlink(&wnd->list);
1281         if (wnd->w_frame)
1282                 wl_callback_destroy(wnd->w_frame);
1283         if (wnd->w_buffer)
1284                 wl_buffer_destroy(wnd->w_buffer);
1285         if (wnd->pool)
1286                 wlt_pool_free(wnd->pool);
1287         wl_shell_surface_destroy(wnd->w_shell_surface);
1288         wl_surface_destroy(wnd->w_surface);
1289         wlt_display_unref(wnd->disp);
1290         free(wnd);
1291 }
1292
1293 int wlt_window_create_widget(struct wlt_window *wnd,
1294                              struct wlt_widget **out,
1295                              void *data)
1296 {
1297         struct wlt_widget *widget;
1298
1299         if (!wnd || !out)
1300                 return -EINVAL;
1301
1302         widget = malloc(sizeof(*widget));
1303         if (!widget)
1304                 return -ENOMEM;
1305         memset(widget, 0, sizeof(*widget));
1306         widget->wnd = wnd;
1307         widget->data = data;
1308
1309         wlt_window_schedule_redraw(wnd);
1310         shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1311         *out = widget;
1312         return 0;
1313 }
1314
1315 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1316 {
1317         if (!wnd)
1318                 return;
1319
1320         wnd->need_redraw = true;
1321         schedule_frame(wnd);
1322 }
1323
1324 void wlt_window_damage(struct wlt_window *wnd,
1325                        struct wlt_rect *damage)
1326 {
1327         if (!wnd || !damage || wnd->skip_damage)
1328                 return;
1329
1330         wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1331                           damage->width, damage->height);
1332 }
1333
1334 void wlt_window_get_buffer(struct wlt_window *wnd,
1335                            const struct wlt_rect *alloc,
1336                            struct wlt_shm_buffer *buf)
1337 {
1338         struct wlt_shm_buffer *rbuf;
1339
1340         if (!wnd || !buf)
1341                 return;
1342
1343         rbuf = &wnd->buffer;
1344
1345         if (!alloc) {
1346                 memcpy(buf, rbuf, sizeof(*buf));
1347                 return;
1348         }
1349
1350         if (alloc->x >= rbuf->width ||
1351             alloc->y >= rbuf->height) {
1352                 memset(buf, 0, sizeof(*buf));
1353                 return;
1354         }
1355
1356         /* set width */
1357         if (alloc->x + alloc->width > rbuf->width)
1358                 buf->width = rbuf->width - alloc->x;
1359         else
1360                 buf->width = alloc->width;
1361
1362         /* set height */
1363         if (alloc->y + alloc->height > rbuf->height)
1364                 buf->height = rbuf->height - alloc->y;
1365         else
1366                 buf->height = alloc->height;
1367
1368         /* set stride */
1369         buf->stride  = rbuf->stride;
1370
1371         /* set data */
1372         buf->data  = rbuf->data;
1373         buf->data += alloc->y * rbuf->stride;
1374         buf->data += alloc->x * 4;
1375 }
1376
1377 void wlt_window_move(struct wlt_window *wnd)
1378 {
1379         if (!wnd)
1380                 return;
1381
1382         wl_shell_surface_move(wnd->w_shell_surface,
1383                               wnd->disp->w_seat,
1384                               wnd->disp->last_serial);
1385 }
1386
1387 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1388 {
1389         if (!wnd)
1390                 return;
1391
1392         wl_shell_surface_resize(wnd->w_shell_surface,
1393                                 wnd->disp->w_seat,
1394                                 wnd->disp->last_serial,
1395                                 edges);
1396 }
1397
1398 int wlt_window_set_size(struct wlt_window *wnd,
1399                         unsigned int width, unsigned int height)
1400 {
1401         if (!wnd || !width || !height)
1402                 return -EINVAL;
1403
1404         wnd->new_width = width;
1405         wnd->new_height = height;
1406         wnd->need_resize = true;
1407         schedule_frame(wnd);
1408
1409         return 0;
1410 }
1411
1412 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1413 {
1414         if (!wnd)
1415                 return;
1416
1417         set_cursor(wnd->disp, cursor);
1418 }
1419
1420 void wlt_window_set_close_cb(struct wlt_window *wnd,
1421                              wlt_window_close_cb cb)
1422 {
1423         if (!wnd)
1424                 return;
1425
1426         wnd->close_cb = cb;
1427 }
1428
1429 void wlt_window_close(struct wlt_window *wnd)
1430 {
1431         if (!wnd)
1432                 return;
1433
1434         wnd->close_pending = true;
1435         ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1436 }
1437
1438 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1439 {
1440         if (!wnd)
1441                 return NULL;
1442
1443         return wnd->disp->eloop;
1444 }
1445
1446 void wlt_widget_destroy(struct wlt_widget *widget)
1447 {
1448         if (!widget)
1449                 return;
1450
1451         if (widget->destroy_cb)
1452                 widget->destroy_cb(widget, widget->data);
1453         shl_dlist_unlink(&widget->list);
1454         free(widget);
1455 }
1456
1457 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1458 {
1459         if (!widget)
1460                 return NULL;
1461
1462         return widget->wnd;
1463 }
1464
1465 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1466                               wlt_widget_redraw_cb cb)
1467 {
1468         if (!widget)
1469                 return;
1470
1471         widget->redraw_cb = cb;
1472 }
1473
1474 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1475                                wlt_widget_destroy_cb cb)
1476 {
1477         if (!widget)
1478                 return;
1479
1480         widget->destroy_cb = cb;
1481 }
1482
1483 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1484                               wlt_widget_prepare_resize_cb prepare_cb,
1485                               wlt_widget_resize_cb cb)
1486 {
1487         if (!widget)
1488                 return;
1489
1490         widget->prepare_resize_cb = prepare_cb;
1491         widget->resize_cb = cb;
1492 }
1493
1494 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1495                                wlt_widget_pointer_enter_cb enter_cb,
1496                                wlt_widget_pointer_leave_cb leave_cb,
1497                                wlt_widget_pointer_motion_cb motion_cb,
1498                                wlt_widget_pointer_button_cb button_cb)
1499 {
1500         if (!widget)
1501                 return;
1502
1503         widget->pointer_enter_cb = enter_cb;
1504         widget->pointer_leave_cb = leave_cb;
1505         widget->pointer_motion_cb = motion_cb;
1506         widget->pointer_button_cb = button_cb;
1507 }
1508
1509 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1510                                 wlt_widget_keyboard_cb cb)
1511 {
1512         if (!widget)
1513                 return;
1514
1515         widget->keyboard_cb = cb;
1516 }