wlt: toolkit: make fullscreen overwrite maximized mode
[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 (!wnd || !width || !height)
1035                 return -EINVAL;
1036
1037         flags = 0;
1038         if (wnd->maximized)
1039                 flags |= WLT_WINDOW_MAXIMIZED;
1040         if (wnd->fullscreen)
1041                 flags |= WLT_WINDOW_FULLSCREEN;
1042
1043         neww = 0;
1044         newh = 0;
1045         minw = 0;
1046         minh = 0;
1047         shl_dlist_for_each(iter, &wnd->widget_list) {
1048                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1049                 if (widget->prepare_resize_cb)
1050                         widget->prepare_resize_cb(widget,
1051                                                   flags,
1052                                                   width, height,
1053                                                   &minw, &minh,
1054                                                   &neww, &newh,
1055                                                   widget->data);
1056         }
1057
1058         if (neww)
1059                 width = neww;
1060         if (newh)
1061                 height = newh;
1062
1063         if (width == wnd->buffer.width &&
1064             height == wnd->buffer.height) {
1065                 if (force_redraw)
1066                         wlt_window_do_redraw(wnd, width, height);
1067                 return 0;
1068         }
1069
1070         oldw = wnd->buffer.width;
1071         oldh = wnd->buffer.height;
1072
1073         nsize = width * height * 4;
1074         if (wnd->pool && wnd->pool->size >= nsize) {
1075                 old_buffer = wnd->w_buffer;
1076                 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1077                                                 0, width, height,
1078                                                 width * 4,
1079                                                 WL_SHM_FORMAT_ARGB8888);
1080                 if (!wnd->w_buffer) {
1081                         log_error("cannot create wayland shm buffer");
1082                         wnd->w_buffer = old_buffer;
1083                         return -ENOMEM;
1084                 }
1085         } else {
1086                 old_pool = wnd->pool;
1087                 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1088                 if (ret) {
1089                         log_error("cannot create memory pool");
1090                         wnd->pool = old_pool;
1091                         return ret;
1092                 }
1093
1094                 old_buffer = wnd->w_buffer;
1095                 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1096                                                 0, width, height,
1097                                                 width * 4,
1098                                                 WL_SHM_FORMAT_ARGB8888);
1099                 if (!wnd->w_buffer) {
1100                         log_error("cannot create wayland shm buffer");
1101                         wnd->w_buffer = old_buffer;
1102                         wlt_pool_free(wnd->pool);
1103                         wnd->pool = old_pool;
1104                         return -ENOMEM;
1105                 }
1106         }
1107
1108         wnd->buffer.data = wnd->pool->data;
1109         wnd->buffer.width = width;
1110         wnd->buffer.height = height;
1111         wnd->buffer.stride = width * 4;
1112         wnd->buffer_attached = false;
1113
1114         wlt_window_do_redraw(wnd, oldw, oldh);
1115
1116         if (old_buffer)
1117                 wl_buffer_destroy(old_buffer);
1118         if (old_pool)
1119                 wlt_pool_free(old_pool);
1120
1121         return 0;
1122 }
1123
1124 static void frame_callback(void *data, struct wl_callback *w_callback,
1125                            uint32_t time);
1126 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1127
1128 static const struct wl_callback_listener frame_callback_listener = {
1129         .done = frame_callback,
1130 };
1131
1132 static void do_frame(struct wlt_window *wnd)
1133 {
1134         bool force;
1135
1136         wnd->idle_pending = false;
1137         ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1138
1139         if (wnd->need_resize) {
1140                 force = wnd->need_redraw;
1141                 wnd->need_frame = true;
1142                 wnd->need_resize = false;
1143                 wnd->need_redraw = false;
1144                 resize_window(wnd, wnd->new_width, wnd->new_height, force);
1145         }
1146
1147         if (wnd->need_redraw) {
1148                 wnd->need_frame = true;
1149                 wnd->need_redraw = false;
1150                 wlt_window_do_redraw(wnd, wnd->buffer.width,
1151                                      wnd->buffer.height);
1152         }
1153
1154         if (wnd->need_frame) {
1155                 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1156                 wl_callback_add_listener(wnd->w_frame,
1157                                          &frame_callback_listener, wnd);
1158         }
1159 }
1160
1161 static void frame_callback(void *data, struct wl_callback *w_callback,
1162                            uint32_t time)
1163 {
1164         struct wlt_window *wnd = data;
1165
1166         wl_callback_destroy(w_callback);
1167         wnd->w_frame = NULL;
1168         wnd->need_frame = false;
1169
1170         do_frame(wnd);
1171 }
1172
1173 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1174 {
1175         struct wlt_window *wnd = data;
1176
1177         do_frame(wnd);
1178 }
1179
1180 /*
1181  * Buffer Handling and Frame Scheduling
1182  * We use wl_shm for buffer allocation. This means, we have a single buffer
1183  * client side and the server loads it into its backbuffer for rendering. If the
1184  * server does not do this, we are screwed anyway, but that's on behalf of the
1185  * server, so we don't care.
1186  *
1187  * However, this means, when we create a buffer, we need to notify the
1188  * compositor and then wait until the compositor has created its back-buffer,
1189  * before we continue using this buffer. We can use the "frame" callback to get
1190  * notified about this.
1191  *
1192  * The logic we have is:
1193  * You set the boolean flags what action is needed in wlt_window and then call
1194  * "schedule_frame()". If we didn't already do any buffer operations in this
1195  * frame, then this function schedules an idle-callback which then performs
1196  * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1197  * that this frame was already used and schedules a frame-callback.
1198  * If during this time another call to schedule_frame() is made, we do nothing
1199  * but wait for the frame-callback. It will then directly perform all the
1200  * requested functions and reschedule a frame-callback.
1201  * If nothing was schedule for one frame, we have no more interest in
1202  * frame-callbacks and thus we set "need_frame" to false again and don't
1203  * schedule any more frame-callbacks.
1204  */
1205 static void schedule_frame(struct wlt_window *wnd)
1206 {
1207         int ret;
1208
1209         if (!wnd || wnd->w_frame)
1210                 return;
1211
1212         if (wnd->need_frame || wnd->idle_pending)
1213                 return;
1214
1215         ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1216         if (ret)
1217                 log_error("cannot schedule idle callback: %d", ret);
1218         else
1219                 wnd->idle_pending = true;
1220 }
1221
1222 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1223                                     uint32_t edges, int32_t width,
1224                                     int32_t height)
1225 {
1226         struct wlt_window *wnd = data;
1227
1228         if (width <= 0)
1229                 width = 1;
1230         if (height <= 0)
1231                 height = 1;
1232
1233         wnd->resize_edges = edges;
1234         wlt_window_set_size(wnd, width, height);
1235 }
1236
1237 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1238 {
1239 }
1240
1241 static const struct wl_shell_surface_listener shell_surface_listener = {
1242         .ping = shell_surface_ping,
1243         .configure = shell_surface_configure,
1244         .popup_done = shell_surface_popup_done,
1245 };
1246
1247 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1248 {
1249         struct wlt_window *wnd = data;
1250
1251         ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1252         wnd->close_pending = false;
1253
1254         if (wnd->close_cb)
1255                 wnd->close_cb(wnd, wnd->data);
1256 }
1257
1258 int wlt_display_create_window(struct wlt_display *disp,
1259                               struct wlt_window **out,
1260                               unsigned int width,
1261                               unsigned int height,
1262                               void *data)
1263 {
1264         struct wlt_window *wnd;
1265         int ret;
1266
1267         if (!disp || !out || !width || !height)
1268                 return -EINVAL;
1269
1270         if (disp->state != STATE_RUNNING) {
1271                 log_error("cannot create window, display is not running but in state %u",
1272                           disp->state);
1273                 return -EBUSY;
1274         }
1275
1276         wnd = malloc(sizeof(*wnd));
1277         if (!wnd)
1278                 return -ENOMEM;
1279         memset(wnd, 0, sizeof(*wnd));
1280         wnd->ref = 1;
1281         wnd->disp = disp;
1282         wnd->data = data;
1283         shl_dlist_init(&wnd->widget_list);
1284
1285         wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1286         if (!wnd->w_surface) {
1287                 log_error("cannot create wayland surface");
1288                 ret = -EFAULT;
1289                 goto err_free;
1290         }
1291
1292         wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1293                                                           wnd->w_surface);
1294         if (!wnd->w_shell_surface) {
1295                 log_error("cannot retrieve shell-surface for wayland surface");
1296                 ret = -EFAULT;
1297                 goto err_surface;
1298         }
1299
1300         wl_shell_surface_add_listener(wnd->w_shell_surface,
1301                                       &shell_surface_listener, wnd);
1302         wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1303
1304         ret = resize_window(wnd, width, height, true);
1305         if (ret)
1306                 goto err_shell_surface;
1307
1308         wlt_display_ref(disp);
1309         shl_dlist_link(&disp->window_list, &wnd->list);
1310         *out = wnd;
1311         return 0;
1312
1313 err_shell_surface:
1314         wl_shell_surface_destroy(wnd->w_shell_surface);
1315 err_surface:
1316         wl_surface_destroy(wnd->w_surface);
1317 err_free:
1318         free(wnd);
1319         return ret;
1320 }
1321
1322 void wlt_window_ref(struct wlt_window *wnd)
1323 {
1324         if (!wnd || !wnd->ref)
1325                 return;
1326
1327         ++wnd->ref;
1328 }
1329
1330 void wlt_window_unref(struct wlt_window *wnd)
1331 {
1332         struct wlt_widget *widget;
1333
1334         if (!wnd || !wnd->ref || --wnd->ref)
1335                 return;
1336
1337         while (!shl_dlist_empty(&wnd->widget_list)) {
1338                 widget = shl_dlist_entry(wnd->widget_list.next,
1339                                          struct wlt_widget, list);
1340                 wlt_widget_destroy(widget);
1341         }
1342
1343         if (wnd->close_pending)
1344                 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1345                                             wnd);
1346         if (wnd->idle_pending)
1347                 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1348         shl_dlist_unlink(&wnd->list);
1349         if (wnd->w_frame)
1350                 wl_callback_destroy(wnd->w_frame);
1351         if (wnd->w_buffer)
1352                 wl_buffer_destroy(wnd->w_buffer);
1353         if (wnd->pool)
1354                 wlt_pool_free(wnd->pool);
1355         wl_shell_surface_destroy(wnd->w_shell_surface);
1356         wl_surface_destroy(wnd->w_surface);
1357         wlt_display_unref(wnd->disp);
1358         free(wnd);
1359 }
1360
1361 int wlt_window_create_widget(struct wlt_window *wnd,
1362                              struct wlt_widget **out,
1363                              void *data)
1364 {
1365         struct wlt_widget *widget;
1366
1367         if (!wnd || !out)
1368                 return -EINVAL;
1369
1370         widget = malloc(sizeof(*widget));
1371         if (!widget)
1372                 return -ENOMEM;
1373         memset(widget, 0, sizeof(*widget));
1374         widget->wnd = wnd;
1375         widget->data = data;
1376
1377         wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
1378         shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1379         *out = widget;
1380         return 0;
1381 }
1382
1383 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1384 {
1385         if (!wnd)
1386                 return;
1387
1388         wnd->need_redraw = true;
1389         schedule_frame(wnd);
1390 }
1391
1392 void wlt_window_damage(struct wlt_window *wnd,
1393                        struct wlt_rect *damage)
1394 {
1395         if (!wnd || !damage || wnd->skip_damage)
1396                 return;
1397
1398         wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1399                           damage->width, damage->height);
1400 }
1401
1402 void wlt_window_get_buffer(struct wlt_window *wnd,
1403                            const struct wlt_rect *alloc,
1404                            struct wlt_shm_buffer *buf)
1405 {
1406         struct wlt_shm_buffer *rbuf;
1407
1408         if (!wnd || !buf)
1409                 return;
1410
1411         rbuf = &wnd->buffer;
1412
1413         if (!alloc) {
1414                 memcpy(buf, rbuf, sizeof(*buf));
1415                 return;
1416         }
1417
1418         if (alloc->x >= rbuf->width ||
1419             alloc->y >= rbuf->height) {
1420                 memset(buf, 0, sizeof(*buf));
1421                 return;
1422         }
1423
1424         /* set width */
1425         if (alloc->x + alloc->width > rbuf->width)
1426                 buf->width = rbuf->width - alloc->x;
1427         else
1428                 buf->width = alloc->width;
1429
1430         /* set height */
1431         if (alloc->y + alloc->height > rbuf->height)
1432                 buf->height = rbuf->height - alloc->y;
1433         else
1434                 buf->height = alloc->height;
1435
1436         /* set stride */
1437         buf->stride  = rbuf->stride;
1438
1439         /* set data */
1440         buf->data  = rbuf->data;
1441         buf->data += alloc->y * rbuf->stride;
1442         buf->data += alloc->x * 4;
1443 }
1444
1445 void wlt_window_move(struct wlt_window *wnd)
1446 {
1447         if (!wnd)
1448                 return;
1449
1450         wl_shell_surface_move(wnd->w_shell_surface,
1451                               wnd->disp->w_seat,
1452                               wnd->disp->last_serial);
1453 }
1454
1455 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1456 {
1457         if (!wnd)
1458                 return;
1459
1460         wl_shell_surface_resize(wnd->w_shell_surface,
1461                                 wnd->disp->w_seat,
1462                                 wnd->disp->last_serial,
1463                                 edges);
1464 }
1465
1466 int wlt_window_set_size(struct wlt_window *wnd,
1467                         unsigned int width, unsigned int height)
1468 {
1469         if (!wnd || !width || !height)
1470                 return -EINVAL;
1471
1472         wnd->new_width = width;
1473         wnd->new_height = height;
1474         wnd->need_resize = true;
1475         schedule_frame(wnd);
1476
1477         return 0;
1478 }
1479
1480 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1481 {
1482         if (!wnd)
1483                 return;
1484
1485         set_cursor(wnd->disp, cursor);
1486 }
1487
1488 void wlt_window_set_close_cb(struct wlt_window *wnd,
1489                              wlt_window_close_cb cb)
1490 {
1491         if (!wnd)
1492                 return;
1493
1494         wnd->close_cb = cb;
1495 }
1496
1497 void wlt_window_close(struct wlt_window *wnd)
1498 {
1499         if (!wnd)
1500                 return;
1501
1502         wnd->close_pending = true;
1503         ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1504 }
1505
1506 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1507 {
1508         if (!wnd)
1509                 return;
1510
1511         if (wnd->maximized) {
1512                 if (!wnd->fullscreen) {
1513                         wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1514                         wlt_window_set_size(wnd, wnd->saved_width,
1515                                             wnd->saved_height);
1516                 }
1517         } else {
1518                 if (!wnd->fullscreen) {
1519                         wnd->saved_width = wnd->buffer.width;
1520                         wnd->saved_height = wnd->buffer.height;
1521                         wl_shell_surface_set_maximized(wnd->w_shell_surface,
1522                                                        NULL);
1523                 }
1524         }
1525
1526         wnd->maximized = !wnd->maximized;
1527 }
1528
1529 void wlt_window_toggle_fullscreen(struct wlt_window *wnd)
1530 {
1531         if (!wnd)
1532                 return;
1533
1534         if (wnd->fullscreen) {
1535                 if (wnd->maximized) {
1536                         wl_shell_surface_set_maximized(wnd->w_shell_surface,
1537                                                        NULL);
1538                 } else {
1539                         wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1540                         wlt_window_set_size(wnd, wnd->saved_width,
1541                                             wnd->saved_height);
1542                 }
1543         } else {
1544                 if (!wnd->maximized) {
1545                         wnd->saved_width = wnd->buffer.width;
1546                         wnd->saved_height = wnd->buffer.height;
1547                 }
1548                 wl_shell_surface_set_fullscreen(wnd->w_shell_surface,
1549                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1550                                 0, NULL);
1551         }
1552
1553         wnd->fullscreen = !wnd->fullscreen;
1554 }
1555
1556 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1557 {
1558         if (!wnd)
1559                 return NULL;
1560
1561         return wnd->disp->eloop;
1562 }
1563
1564 void wlt_widget_destroy(struct wlt_widget *widget)
1565 {
1566         if (!widget)
1567                 return;
1568
1569         if (widget->destroy_cb)
1570                 widget->destroy_cb(widget, widget->data);
1571         shl_dlist_unlink(&widget->list);
1572         free(widget);
1573 }
1574
1575 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1576 {
1577         if (!widget)
1578                 return NULL;
1579
1580         return widget->wnd;
1581 }
1582
1583 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1584                               wlt_widget_redraw_cb cb)
1585 {
1586         if (!widget)
1587                 return;
1588
1589         widget->redraw_cb = cb;
1590 }
1591
1592 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1593                                wlt_widget_destroy_cb cb)
1594 {
1595         if (!widget)
1596                 return;
1597
1598         widget->destroy_cb = cb;
1599 }
1600
1601 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1602                               wlt_widget_prepare_resize_cb prepare_cb,
1603                               wlt_widget_resize_cb cb)
1604 {
1605         if (!widget)
1606                 return;
1607
1608         widget->prepare_resize_cb = prepare_cb;
1609         widget->resize_cb = cb;
1610 }
1611
1612 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1613                                wlt_widget_pointer_enter_cb enter_cb,
1614                                wlt_widget_pointer_leave_cb leave_cb,
1615                                wlt_widget_pointer_motion_cb motion_cb,
1616                                wlt_widget_pointer_button_cb button_cb)
1617 {
1618         if (!widget)
1619                 return;
1620
1621         widget->pointer_enter_cb = enter_cb;
1622         widget->pointer_leave_cb = leave_cb;
1623         widget->pointer_motion_cb = motion_cb;
1624         widget->pointer_button_cb = button_cb;
1625 }
1626
1627 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1628                                 wlt_widget_keyboard_cb cb)
1629 {
1630         if (!widget)
1631                 return;
1632
1633         widget->keyboard_cb = cb;
1634 }