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