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