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