build: update autotools logic and add "wlterm" application
[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 "eloop.h"
39 #include "log.h"
40 #include "shl_dlist.h"
41 #include "shl_hook.h"
42 #include "wlt_toolkit.h"
43
44 #define LOG_SUBSYSTEM "wlt_toolkit"
45
46 struct wlt_pool {
47         struct wl_shm_pool *w_pool;
48         size_t size;
49         uint8_t *data;
50 };
51
52 enum {
53         STATE_INIT = 0,
54         STATE_RUNNING,
55         STATE_HUP,
56 };
57
58 struct wlt_display {
59         unsigned long ref;
60         struct ev_eloop *eloop;
61         struct wl_display *dp;
62         struct ev_fd *dp_fd;
63         struct wl_global_listener *dp_listener;
64         struct shl_hook *listeners;
65         unsigned int state;
66
67         struct shl_dlist window_list;
68
69         struct wl_compositor *w_comp;
70         struct wl_seat *w_seat;
71         struct wl_shell *w_shell;
72         struct wl_shm *w_shm;
73
74         uint32_t last_serial;
75         uint32_t pointer_enter_serial;
76         struct wl_pointer *w_pointer;
77         struct wlt_window *pointer_focus;
78
79         uint32_t cursor_serial;
80         unsigned int current_cursor;
81         struct wl_surface *w_cursor_surface;
82         struct wl_cursor_theme *cursor_theme;
83         struct wl_cursor *cursors[WLT_CURSOR_NUM];
84 };
85
86 struct wlt_window {
87         unsigned long ref;
88         struct shl_dlist list;
89         void *data;
90         wlt_window_close_cb close_cb;
91         bool close_pending;
92         struct wlt_display *disp;
93         struct wlt_pool *pool;
94
95         struct wl_surface *w_surface;
96         struct wl_shell_surface *w_shell_surface;
97         struct wl_buffer *w_buffer;
98
99         bool buffer_attached;
100         bool skip_damage;
101         bool need_resize;
102         bool need_redraw;
103         bool need_frame;
104         bool idle_pending;
105         unsigned int new_width;
106         unsigned int new_height;
107         unsigned int resize_edges;
108         struct wlt_shm_buffer buffer;
109         struct wl_callback *w_frame;
110
111         struct shl_dlist widget_list;
112 };
113
114 struct wlt_widget {
115         struct shl_dlist list;
116         struct wlt_window *wnd;
117         void *data;
118         wlt_widget_redraw_cb redraw_cb;
119         wlt_widget_destroy_cb destroy_cb;
120         wlt_widget_prepare_resize_cb prepare_resize_cb;
121         wlt_widget_resize_cb resize_cb;
122         wlt_widget_pointer_enter_cb pointer_enter_cb;
123         wlt_widget_pointer_leave_cb pointer_leave_cb;
124         wlt_widget_pointer_motion_cb pointer_motion_cb;
125         wlt_widget_pointer_button_cb pointer_button_cb;
126 };
127
128 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
129                         size_t size)
130 {
131         static const char template[] = "/wlterm-shared-XXXXXX";
132         struct wlt_pool *pool;
133         int fd, ret;
134         const char *path;
135         char *name;
136
137         path = getenv("XDG_RUNTIME_DIR");
138         if (!path) {
139                 log_error("XDG_RUNTIME_DIR not set");
140                 return -EFAULT;
141         }
142
143         pool = malloc(sizeof(*pool));
144         if (!pool)
145                 return -ENOMEM;
146         memset(pool, 0, sizeof(*pool));
147         pool->size = size;
148
149         name = malloc(strlen(path) + sizeof(template));
150         if (!name) {
151                 ret = -ENOMEM;
152                 goto err_free;
153         }
154         strcpy(name, path);
155         strcat(name, template);
156
157         fd = mkostemp(name, O_CLOEXEC);
158         if (fd < 0) {
159                 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
160                           name, errno);
161                 ret = -EFAULT;
162                 free(name);
163                 goto err_free;
164         }
165
166         ret = unlink(name);
167         if (ret)
168                 log_warning("cannot unlink temporary file %s (%d): %m",
169                             name, errno);
170         free(name);
171
172         ret = ftruncate(fd, size);
173         if (ret) {
174                 log_error("cannot truncate temporary file to length %lu (%d): %m",
175                           size, errno);
176                 ret = -EFAULT;
177                 goto err_close;
178         }
179
180         pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
181                           fd, 0);
182         if (pool->data == MAP_FAILED) {
183                 log_error("cannot mmap temporary file (%d): %m", errno);
184                 ret = -EFAULT;
185                 goto err_close;
186         }
187
188         pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
189         if (!pool->w_pool) {
190                 log_error("cannot create wayland shm pool object");
191                 ret = -EFAULT;
192                 goto err_map;
193         }
194
195         close(fd);
196         *out = pool;
197         return 0;
198
199 err_map:
200         munmap(pool->data, size);
201 err_close:
202         close(fd);
203 err_free:
204         free(pool);
205         return ret;
206 }
207
208 static void wlt_pool_free(struct wlt_pool *pool)
209 {
210         munmap(pool->data, pool->size);
211         wl_shm_pool_destroy(pool->w_pool);
212         free(pool);
213 }
214
215 /*
216  * These cursor-names are based on:
217  *   https://bugs.kde.org/attachment.cgi?id=67313
218  */
219
220 static char *(*cursors[])[] = {
221         [WLT_CURSOR_NONE] = &(char*[]) {
222                 NULL,
223         },
224         [WLT_CURSOR_TOP] = &(char*[]) {
225                 "top_side",
226                 "n-resize",
227                 NULL,
228         },
229         [WLT_CURSOR_BOTTOM] = &(char*[]) {
230                 "bottom_side",
231                 "s-resize",
232                 NULL,
233         },
234         [WLT_CURSOR_LEFT] = &(char*[]) {
235                 "left_side",
236                 "w-resize",
237                 NULL,
238         },
239         [WLT_CURSOR_RIGHT] = &(char*[]) {
240                 "right_side",
241                 "e-resize",
242                 NULL,
243         },
244         [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
245                 "top_left_corner",
246                 "nw-resize",
247                 NULL,
248         },
249         [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
250                 "top_right_corner",
251                 "ne-resize",
252                 NULL,
253         },
254         [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
255                 "bottom_left_corner",
256                 "sw-resize",
257                 NULL,
258         },
259         [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
260                 "bottom_right_corner",
261                 "se-resize",
262                 NULL,
263         },
264         [WLT_CURSOR_DRAGGING] = &(char*[]) {
265                 "grabbing",
266                 "closedhand",
267                 "208530c400c041818281048008011002",
268                 NULL,
269         },
270         [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
271                 "left_ptr",
272                 "default",
273                 "top_left_arrow",
274                 "left-arrow",
275                 NULL,
276         },
277         [WLT_CURSOR_IBEAM] = &(char*[]) {
278                 "xterm",
279                 "ibeam",
280                 "text",
281                 NULL,
282         },
283 };
284
285 static int load_cursors(struct wlt_display *disp)
286 {
287         unsigned int i, j;
288         size_t size = sizeof(cursors) / sizeof(*cursors);
289         struct wl_cursor *cursor;
290
291         disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
292
293         disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
294         if (!disp->cursor_theme) {
295                 log_warning("cannot load curdor theme");
296                 return -EFAULT;
297         }
298
299         for (i = 0; i < WLT_CURSOR_NUM; ++i) {
300                 cursor = NULL;
301                 if (i < size && cursors[i]) {
302                         for (j = 0; (*cursors[i])[j]; ++j) {
303                                 cursor = wl_cursor_theme_get_cursor(
304                                                         disp->cursor_theme,
305                                                         (*cursors[i])[j]);
306                                 if (cursor)
307                                         break;
308                         }
309                 }
310
311                 if (!cursor && i != WLT_CURSOR_NONE)
312                         log_warning("cannot load cursor for ID %d", i);
313
314                 disp->cursors[i] = cursor;
315         }
316
317         return 0;
318 }
319
320 static void unload_cursors(struct wlt_display *disp)
321 {
322         if (disp->cursor_theme)
323                 wl_cursor_theme_destroy(disp->cursor_theme);
324         if (disp->w_cursor_surface)
325                 wl_surface_destroy(disp->w_cursor_surface);
326 }
327
328 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
329 {
330         bool force = false;
331         struct wl_cursor *cur;
332         struct wl_cursor_image *image;
333         struct wl_buffer *buffer;
334
335         if (cursor >= WLT_CURSOR_NUM)
336                 cursor = WLT_CURSOR_LEFT_PTR;
337
338         if (disp->pointer_enter_serial > disp->cursor_serial)
339                 force = true;
340
341         if (!force && cursor == disp->current_cursor)
342                 return;
343
344         disp->current_cursor = cursor;
345         disp->cursor_serial = disp->pointer_enter_serial;
346
347         cur = disp->cursors[cursor];
348         if (!cur) {
349                 wl_pointer_set_cursor(disp->w_pointer,
350                                       disp->pointer_enter_serial,
351                                       NULL, 0, 0);
352                 return;
353         }
354
355         image = cur->images[0];
356         buffer = wl_cursor_image_get_buffer(image);
357         if (!buffer) {
358                 log_error("cannot load buffer for cursor image");
359                 return;
360         }
361
362         wl_pointer_set_cursor(disp->w_pointer,
363                               disp->pointer_enter_serial,
364                               disp->w_cursor_surface,
365                               image->hotspot_x, image->hotspot_y);
366         wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
367         wl_surface_damage(disp->w_cursor_surface, 0, 0,
368                           image->width, image->height);
369 }
370
371 static int dp_mask_update(uint32_t mask, void *data)
372 {
373         struct wlt_display *disp = data;
374         int ret;
375         int mode;
376
377         if (!disp->dp_fd)
378                 return 0;
379
380         mode = 0;
381         if (mask & WL_DISPLAY_READABLE)
382                 mode |= EV_READABLE;
383         if (mask & WL_DISPLAY_WRITABLE)
384                 mode |= EV_WRITEABLE;
385
386         ret = ev_fd_update(disp->dp_fd, mode);
387         if (ret)
388                 log_warning("cannot update wayland-fd event-polling modes (%d)",
389                             ret);
390
391         return 0;
392 }
393
394 static void dp_event(struct ev_fd *fd, int mask, void *data)
395 {
396         struct wlt_display *disp = data;
397         int mode;
398
399         if (mask & (EV_HUP | EV_ERR)) {
400                 log_warning("HUP/ERR on wayland socket");
401                 disp->state = STATE_HUP;
402                 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
403                 ev_eloop_rm_fd(disp->dp_fd);
404                 disp->dp_fd = NULL;
405                 return;
406         }
407
408         mode = 0;
409         if (mask & EV_READABLE)
410                 mode |= WL_DISPLAY_READABLE;
411         if (mask & EV_WRITEABLE)
412                 mode |= WL_DISPLAY_WRITABLE;
413
414         if (mode)
415                 wl_display_iterate(disp->dp, mode);
416 }
417
418 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
419                           uint32_t serial, struct wl_surface *w_surface,
420                           wl_fixed_t x, wl_fixed_t y)
421 {
422         struct wlt_display *disp = data;
423         struct shl_dlist *iter;
424         struct wlt_window *wnd;
425         struct wlt_widget *widget;
426
427         if (!w_surface)
428                 return;
429
430         shl_dlist_for_each(iter, &disp->window_list) {
431                 wnd = shl_dlist_entry(iter, struct wlt_window, list);
432                 if (wnd->w_surface == w_surface)
433                         break;
434         }
435
436         if (iter == &disp->window_list) {
437                 log_debug("unknown surface");
438                 return;
439         }
440
441         disp->pointer_enter_serial = serial;
442         disp->last_serial = serial;
443         disp->pointer_focus = wnd;
444         shl_dlist_for_each(iter, &wnd->widget_list) {
445                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
446                 if (widget->pointer_enter_cb)
447                         widget->pointer_enter_cb(widget, x >> 8, y >> 8,
448                                                  widget->data);
449         }
450 }
451
452 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
453                           uint32_t serial, struct wl_surface *w_surface)
454 {
455         struct wlt_display *disp = data;
456         struct shl_dlist *iter;
457         struct wlt_window *wnd;
458         struct wlt_widget *widget;
459
460         wnd = disp->pointer_focus;
461         disp->pointer_focus = NULL;
462         disp->last_serial = serial;
463
464         if (!wnd)
465                 return;
466
467         shl_dlist_for_each(iter, &wnd->widget_list) {
468                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
469                 if (widget->pointer_leave_cb)
470                         widget->pointer_leave_cb(widget, widget->data);
471         }
472 }
473
474 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
475                            uint32_t time, wl_fixed_t x, wl_fixed_t y)
476 {
477         struct wlt_display *disp = data;
478         struct shl_dlist *iter;
479         struct wlt_window *wnd;
480         struct wlt_widget *widget;
481
482         wnd = disp->pointer_focus;
483         if (!wnd)
484                 return;
485
486         shl_dlist_for_each(iter, &wnd->widget_list) {
487                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
488                 if (widget->pointer_motion_cb)
489                         widget->pointer_motion_cb(widget, x >> 8, y >> 8,
490                                                   widget->data);
491         }
492 }
493
494 static void pointer_button(void *data, struct wl_pointer *w_pointer,
495                            uint32_t serial, uint32_t time, uint32_t button,
496                            uint32_t state)
497 {
498         struct wlt_display *disp = data;
499         struct shl_dlist *iter;
500         struct wlt_window *wnd;
501         struct wlt_widget *widget;
502
503         wnd = disp->pointer_focus;
504         disp->last_serial = serial;
505
506         if (!wnd)
507                 return;
508
509         shl_dlist_for_each(iter, &wnd->widget_list) {
510                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
511                 if (widget->pointer_button_cb)
512                         widget->pointer_button_cb(widget, button, state,
513                                                   widget->data);
514         }
515 }
516
517 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
518                          uint32_t time, uint32_t axis, wl_fixed_t value)
519 {
520 }
521
522 static const struct wl_pointer_listener pointer_listener = {
523         .enter = pointer_enter,
524         .leave = pointer_leave,
525         .motion = pointer_motion,
526         .button = pointer_button,
527         .axis = pointer_axis,
528 };
529
530 static void check_ready(struct wlt_display *disp)
531 {
532         if (disp->state > STATE_INIT)
533                 return;
534
535         if (disp->w_comp &&
536             disp->w_seat &&
537             disp->w_shell &&
538             disp->w_shm &&
539             disp->w_pointer) {
540                 log_debug("wayland display initialized");
541                 load_cursors(disp);
542
543                 disp->state = STATE_RUNNING;
544                 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
545         }
546 }
547
548 static void seat_capabilities(void *data, struct wl_seat *seat,
549                               enum wl_seat_capability caps)
550 {
551         struct wlt_display *disp = data;
552
553         if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
554                 disp->w_pointer = wl_seat_get_pointer(seat);
555                 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
556                                         disp);
557         }
558
559         check_ready(disp);
560 }
561
562 static const struct wl_seat_listener seat_listener = {
563         .capabilities = seat_capabilities,
564 };
565
566 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
567                       uint32_t version, void *data)
568 {
569         struct wlt_display *disp = data;
570
571         if (!strcmp(iface, "wl_display")) {
572                 log_debug("new wl_display global");
573                 return;
574         } else if (!strcmp(iface, "wl_compositor")) {
575                 if (disp->w_comp) {
576                         log_error("global wl_compositor advertised twice");
577                         return;
578                 }
579                 disp->w_comp = wl_display_bind(disp->dp,
580                                                id,
581                                                &wl_compositor_interface);
582                 if (!disp->w_comp) {
583                         log_error("cannot bind wl_compositor object");
584                         return;
585                 }
586         } else if (!strcmp(iface, "wl_seat")) {
587                 if (disp->w_seat) {
588                         log_error("global wl_seat advertised twice");
589                         return;
590                 }
591                 disp->w_seat = wl_display_bind(disp->dp,
592                                                id,
593                                                &wl_seat_interface);
594                 if (!disp->w_seat) {
595                         log_error("cannot bind wl_seat object");
596                         return;
597                 }
598
599                 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
600         } else if (!strcmp(iface, "wl_shell")) {
601                 if (disp->w_shell) {
602                         log_error("global wl_shell advertised twice");
603                         return;
604                 }
605                 disp->w_shell = wl_display_bind(disp->dp,
606                                                 id,
607                                                 &wl_shell_interface);
608                 if (!disp->w_shell) {
609                         log_error("cannot bind wl_shell object");
610                         return;
611                 }
612         } else if (!strcmp(iface, "wl_shm")) {
613                 if (disp->w_shm) {
614                         log_error("global wl_shm advertised twice");
615                         return;
616                 }
617                 disp->w_shm = wl_display_bind(disp->dp,
618                                               id,
619                                               &wl_shm_interface);
620                 if (!disp->w_shm) {
621                         log_error("cannot bind wl_shm object");
622                         return;
623                 }
624         } else {
625                 log_debug("ignoring new unknown global %s", iface);
626                 return;
627         }
628
629         log_debug("new global %s", iface);
630
631         check_ready(disp);
632 }
633
634 int wlt_display_new(struct wlt_display **out,
635                     struct ev_eloop *eloop)
636 {
637         struct wlt_display *disp;
638         int ret;
639
640         if (!out || !eloop)
641                 return -EINVAL;
642
643         log_debug("creating new wlt-display");
644
645         disp = malloc(sizeof(*disp));
646         if (!disp)
647                 return -ENOMEM;
648         memset(disp, 0, sizeof(*disp));
649         disp->ref = 1;
650         disp->eloop = eloop;
651         shl_dlist_init(&disp->window_list);
652
653         ret = shl_hook_new(&disp->listeners);
654         if (ret) {
655                 log_error("cannot create listeners hook");
656                 goto err_free;
657         }
658
659         disp->dp = wl_display_connect(NULL);
660         if (!disp->dp) {
661                 log_error("cannot connect to wayland socket (%d): %m", errno);
662                 ret = -EFAULT;
663                 goto err_listener;
664         }
665
666         ret = ev_eloop_new_fd(eloop,
667                               &disp->dp_fd,
668                               wl_display_get_fd(disp->dp,
669                                                 dp_mask_update,
670                                                 disp),
671                               EV_READABLE,
672                               dp_event,
673                               disp);
674         if (ret) {
675                 log_error("cannot create event-fd for wayland display (%d)",
676                           ret);
677                 goto err_dp;
678         }
679
680         disp->dp_listener = wl_display_add_global_listener(disp->dp,
681                                                            dp_global,
682                                                            disp);
683         if (!disp->dp_listener) {
684                 log_error("cannot add wayland global listener (%d)");
685                 goto err_dp_fd;
686         }
687
688         log_debug("wlt-display waiting for globals...");
689
690         ev_eloop_ref(disp->eloop);
691         *out = disp;
692         return 0;
693
694 err_dp_fd:
695         ev_eloop_rm_fd(disp->dp_fd);
696 err_dp:
697         wl_display_disconnect(disp->dp);
698 err_listener:
699         shl_hook_free(disp->listeners);
700 err_free:
701         free(disp);
702         return ret;
703 }
704
705 void wlt_display_ref(struct wlt_display *disp)
706 {
707         if (!disp || !disp->ref)
708                 return;
709
710         ++disp->ref;
711 }
712
713 void wlt_display_unref(struct wlt_display *disp)
714 {
715         if (!disp || !disp->ref || --disp->ref)
716                 return;
717
718         unload_cursors(disp);
719         wl_display_remove_global_listener(disp->dp, disp->dp_listener);
720         ev_eloop_rm_fd(disp->dp_fd);
721         wl_display_flush(disp->dp);
722         wl_display_disconnect(disp->dp);
723         shl_hook_free(disp->listeners);
724         ev_eloop_unref(disp->eloop);
725         free(disp);
726 }
727
728 int wlt_display_register_cb(struct wlt_display *disp,
729                             wlt_display_cb cb, void *data)
730 {
731         if (!disp)
732                 return -EINVAL;
733
734         return shl_hook_add_cast(disp->listeners, cb, data);
735 }
736
737 void wlt_display_unregister_cb(struct wlt_display *disp,
738                                wlt_display_cb cb, void *data)
739 {
740         if (!disp)
741                 return;
742
743         shl_hook_rm_cast(disp->listeners, cb, data);
744 }
745
746 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
747                                uint32_t serial)
748 {
749         wl_shell_surface_pong(s, serial);
750 }
751
752 static void wlt_window_do_redraw(struct wlt_window *wnd,
753                                  unsigned int oldw,
754                                  unsigned int oldh)
755 {
756         struct shl_dlist *iter;
757         struct wlt_widget *widget;
758         unsigned int x, y;
759         struct wlt_rect alloc;
760
761         alloc.x = 0;
762         alloc.y = 0;
763         alloc.width = wnd->buffer.width;
764         alloc.height = wnd->buffer.height;
765         shl_dlist_for_each(iter, &wnd->widget_list) {
766                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
767                 if (widget->resize_cb)
768                         widget->resize_cb(widget, &alloc, widget->data);
769         }
770
771         memset(wnd->buffer.data, 0,
772                wnd->buffer.stride * wnd->buffer.height);
773
774         wnd->skip_damage = true;
775         shl_dlist_for_each(iter, &wnd->widget_list) {
776                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
777                 if (widget->redraw_cb)
778                         widget->redraw_cb(widget, widget->data);
779         }
780         wnd->skip_damage = false;
781
782         if (!wnd->buffer_attached) {
783                 wnd->buffer_attached = true;
784                 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
785                         x = (int)oldw - wnd->buffer.width;
786                 else
787                         x = 0;
788                 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
789                         y = (int)oldh - wnd->buffer.height;
790                 else
791                         y = 0;
792                 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
793                 wnd->resize_edges = 0;
794         }
795
796         wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
797                           wnd->buffer.height);
798 }
799
800 static int resize_window(struct wlt_window *wnd, unsigned int width,
801                          unsigned int height)
802 {
803         struct shl_dlist *iter;
804         struct wlt_widget *widget;
805         struct wl_buffer *old_buffer = NULL;
806         struct wlt_pool *old_pool = NULL;
807         size_t nsize;
808         int ret;
809         unsigned int oldw, oldh;
810
811         if (!wnd || !width || !height)
812                 return -EINVAL;
813
814         shl_dlist_for_each(iter, &wnd->widget_list) {
815                 widget = shl_dlist_entry(iter, struct wlt_widget, list);
816                 if (widget->prepare_resize_cb)
817                         widget->prepare_resize_cb(widget, &width, &height,
818                                                   widget->data);
819         }
820
821         if (width == wnd->buffer.width &&
822             height == wnd->buffer.height)
823                 return 0;
824
825         oldw = wnd->buffer.width;
826         oldh = wnd->buffer.height;
827
828         nsize = width * height * 4;
829         if (wnd->pool && wnd->pool->size >= nsize) {
830                 old_buffer = wnd->w_buffer;
831                 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
832                                                 0, width, height,
833                                                 width * 4,
834                                                 WL_SHM_FORMAT_ARGB8888);
835                 if (!wnd->w_buffer) {
836                         log_error("cannot create wayland shm buffer");
837                         wnd->w_buffer = old_buffer;
838                         return -ENOMEM;
839                 }
840         } else {
841                 old_pool = wnd->pool;
842                 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
843                 if (ret) {
844                         log_error("cannot create memory pool");
845                         wnd->pool = old_pool;
846                         return ret;
847                 }
848
849                 old_buffer = wnd->w_buffer;
850                 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
851                                                 0, width, height,
852                                                 width * 4,
853                                                 WL_SHM_FORMAT_ARGB8888);
854                 if (!wnd->w_buffer) {
855                         log_error("cannot create wayland shm buffer");
856                         wnd->w_buffer = old_buffer;
857                         wlt_pool_free(wnd->pool);
858                         wnd->pool = old_pool;
859                         return -ENOMEM;
860                 }
861         }
862
863         wnd->buffer.data = wnd->pool->data;
864         wnd->buffer.width = width;
865         wnd->buffer.height = height;
866         wnd->buffer.stride = width * 4;
867         wnd->buffer_attached = false;
868
869         wlt_window_do_redraw(wnd, oldw, oldh);
870
871         if (old_buffer)
872                 wl_buffer_destroy(old_buffer);
873         if (old_pool)
874                 wlt_pool_free(old_pool);
875
876         return 0;
877 }
878
879 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
880
881 static void do_frame(struct wlt_window *wnd)
882 {
883         wnd->idle_pending = false;
884         ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
885
886         if (wnd->need_resize) {
887                 wnd->need_resize = false;
888                 wnd->need_redraw = false;
889                 resize_window(wnd, wnd->new_width, wnd->new_height);
890         }
891
892         if (wnd->need_redraw) {
893                 wnd->need_redraw = false;
894                 wlt_window_do_redraw(wnd, wnd->buffer.width,
895                                      wnd->buffer.height);
896         }
897 }
898
899 static void frame_callback(void *data, struct wl_callback *w_callback,
900                            uint32_t time)
901 {
902         struct wlt_window *wnd = data;
903
904         wl_callback_destroy(w_callback);
905         wnd->w_frame = NULL;
906         wnd->need_frame = false;
907
908         do_frame(wnd);
909 }
910
911 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
912 {
913         struct wlt_window *wnd = data;
914
915         wnd->need_frame = true;
916         do_frame(wnd);
917 }
918
919 static const struct wl_callback_listener frame_callback_listener = {
920         .done = frame_callback,
921 };
922
923 static void schedule_frame(struct wlt_window *wnd)
924 {
925         if (!wnd || wnd->w_frame)
926                 return;
927
928         if (!wnd->need_frame && !wnd->idle_pending) {
929                 wnd->idle_pending = true;
930                 ev_eloop_register_idle_cb(wnd->disp->eloop,
931                                           idle_frame, wnd);
932                 wnd->w_frame = wl_surface_frame(wnd->w_surface);
933                 wl_callback_add_listener(wnd->w_frame,
934                                          &frame_callback_listener, wnd);
935         }
936 }
937
938 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
939                                     uint32_t edges, int32_t width,
940                                     int32_t height)
941 {
942         struct wlt_window *wnd = data;
943
944         if (width <= 0)
945                 width = 1;
946         if (height <= 0)
947                 height = 1;
948
949         wnd->resize_edges = edges;
950         wlt_window_set_size(wnd, width, height);
951 }
952
953 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
954 {
955 }
956
957 static const struct wl_shell_surface_listener shell_surface_listener = {
958         .ping = shell_surface_ping,
959         .configure = shell_surface_configure,
960         .popup_done = shell_surface_popup_done,
961 };
962
963 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
964 {
965         struct wlt_window *wnd = data;
966
967         ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
968         wnd->close_pending = false;
969
970         if (wnd->close_cb)
971                 wnd->close_cb(wnd, wnd->data);
972 }
973
974 int wlt_display_create_window(struct wlt_display *disp,
975                               struct wlt_window **out,
976                               unsigned int width,
977                               unsigned int height,
978                               void *data)
979 {
980         struct wlt_window *wnd;
981         int ret;
982
983         if (!disp || !out || !width || !height)
984                 return -EINVAL;
985
986         if (disp->state != STATE_RUNNING) {
987                 log_error("cannot create window, display is not running but in state %u",
988                           disp->state);
989                 return -EBUSY;
990         }
991
992         wnd = malloc(sizeof(*wnd));
993         if (!wnd)
994                 return -ENOMEM;
995         memset(wnd, 0, sizeof(*wnd));
996         wnd->ref = 1;
997         wnd->disp = disp;
998         wnd->data = data;
999         shl_dlist_init(&wnd->widget_list);
1000
1001         wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1002         if (!wnd->w_surface) {
1003                 log_error("cannot create wayland surface");
1004                 ret = -EFAULT;
1005                 goto err_free;
1006         }
1007
1008         wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1009                                                           wnd->w_surface);
1010         if (!wnd->w_shell_surface) {
1011                 log_error("cannot retrieve shell-surface for wayland surface");
1012                 ret = -EFAULT;
1013                 goto err_surface;
1014         }
1015
1016         wl_shell_surface_add_listener(wnd->w_shell_surface,
1017                                       &shell_surface_listener, wnd);
1018         wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1019
1020         ret = resize_window(wnd, width, height);
1021         if (ret)
1022                 goto err_shell_surface;
1023
1024         wlt_display_ref(disp);
1025         shl_dlist_link(&disp->window_list, &wnd->list);
1026         *out = wnd;
1027         return 0;
1028
1029 err_shell_surface:
1030         wl_shell_surface_destroy(wnd->w_shell_surface);
1031 err_surface:
1032         wl_surface_destroy(wnd->w_surface);
1033 err_free:
1034         free(wnd);
1035         return ret;
1036 }
1037
1038 void wlt_window_ref(struct wlt_window *wnd)
1039 {
1040         if (!wnd || !wnd->ref)
1041                 return;
1042
1043         ++wnd->ref;
1044 }
1045
1046 void wlt_window_unref(struct wlt_window *wnd)
1047 {
1048         struct wlt_widget *widget;
1049
1050         if (!wnd || !wnd->ref || --wnd->ref)
1051                 return;
1052
1053         while (!shl_dlist_empty(&wnd->widget_list)) {
1054                 widget = shl_dlist_entry(wnd->widget_list.next,
1055                                          struct wlt_widget, list);
1056                 wlt_widget_destroy(widget);
1057         }
1058
1059         if (wnd->close_pending)
1060                 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1061                                             wnd);
1062         if (wnd->idle_pending)
1063                 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1064         shl_dlist_unlink(&wnd->list);
1065         if (wnd->w_frame)
1066                 wl_callback_destroy(wnd->w_frame);
1067         if (wnd->w_buffer)
1068                 wl_buffer_destroy(wnd->w_buffer);
1069         if (wnd->pool)
1070                 wlt_pool_free(wnd->pool);
1071         wl_shell_surface_destroy(wnd->w_shell_surface);
1072         wl_surface_destroy(wnd->w_surface);
1073         wlt_display_unref(wnd->disp);
1074         free(wnd);
1075 }
1076
1077 int wlt_window_create_widget(struct wlt_window *wnd,
1078                              struct wlt_widget **out,
1079                              void *data)
1080 {
1081         struct wlt_widget *widget;
1082
1083         if (!wnd || !out)
1084                 return -EINVAL;
1085
1086         widget = malloc(sizeof(*widget));
1087         if (!widget)
1088                 return -ENOMEM;
1089         memset(widget, 0, sizeof(*widget));
1090         widget->wnd = wnd;
1091         widget->data = data;
1092
1093         wlt_window_schedule_redraw(wnd);
1094         shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1095         *out = widget;
1096         return 0;
1097 }
1098
1099 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1100 {
1101         if (!wnd)
1102                 return;
1103
1104         wnd->need_redraw = true;
1105         schedule_frame(wnd);
1106 }
1107
1108 void wlt_window_damage(struct wlt_window *wnd,
1109                        struct wlt_rect *damage)
1110 {
1111         if (!wnd || !damage || wnd->skip_damage)
1112                 return;
1113
1114         wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1115                           damage->width, damage->height);
1116 }
1117
1118 void wlt_window_get_buffer(struct wlt_window *wnd,
1119                            const struct wlt_rect *alloc,
1120                            struct wlt_shm_buffer *buf)
1121 {
1122         struct wlt_shm_buffer *rbuf;
1123
1124         if (!wnd || !buf)
1125                 return;
1126
1127         rbuf = &wnd->buffer;
1128
1129         if (!alloc) {
1130                 memcpy(buf, rbuf, sizeof(*buf));
1131                 return;
1132         }
1133
1134         if (alloc->x >= rbuf->width ||
1135             alloc->y >= rbuf->height) {
1136                 memset(buf, 0, sizeof(*buf));
1137                 return;
1138         }
1139
1140         /* set width */
1141         if (alloc->x + alloc->width > rbuf->width)
1142                 buf->width = rbuf->width - alloc->x;
1143         else
1144                 buf->width = alloc->width;
1145
1146         /* set height */
1147         if (alloc->y + alloc->height > rbuf->height)
1148                 buf->height = rbuf->height - alloc->y;
1149         else
1150                 buf->height = alloc->height;
1151
1152         /* set stride */
1153         buf->stride  = rbuf->stride;
1154
1155         /* set data */
1156         buf->data  = rbuf->data;
1157         buf->data += alloc->y * rbuf->stride;
1158         buf->data += alloc->x * 4;
1159 }
1160
1161 void wlt_window_move(struct wlt_window *wnd)
1162 {
1163         if (!wnd)
1164                 return;
1165
1166         wl_shell_surface_move(wnd->w_shell_surface,
1167                               wnd->disp->w_seat,
1168                               wnd->disp->last_serial);
1169 }
1170
1171 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1172 {
1173         if (!wnd)
1174                 return;
1175
1176         wl_shell_surface_resize(wnd->w_shell_surface,
1177                                 wnd->disp->w_seat,
1178                                 wnd->disp->last_serial,
1179                                 edges);
1180 }
1181
1182 int wlt_window_set_size(struct wlt_window *wnd,
1183                         unsigned int width, unsigned int height)
1184 {
1185         if (!wnd || !width || !height)
1186                 return -EINVAL;
1187
1188         wnd->new_width = width;
1189         wnd->new_height = height;
1190         wnd->need_resize = true;
1191         schedule_frame(wnd);
1192
1193         return 0;
1194 }
1195
1196 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1197 {
1198         if (!wnd)
1199                 return;
1200
1201         set_cursor(wnd->disp, cursor);
1202 }
1203
1204 void wlt_window_set_close_cb(struct wlt_window *wnd,
1205                              wlt_window_close_cb cb)
1206 {
1207         if (!wnd)
1208                 return;
1209
1210         wnd->close_cb = cb;
1211 }
1212
1213 void wlt_window_close(struct wlt_window *wnd)
1214 {
1215         if (!wnd)
1216                 return;
1217
1218         wnd->close_pending = true;
1219         ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1220 }
1221
1222 void wlt_widget_destroy(struct wlt_widget *widget)
1223 {
1224         if (!widget)
1225                 return;
1226
1227         if (widget->destroy_cb)
1228                 widget->destroy_cb(widget, widget->data);
1229         shl_dlist_unlink(&widget->list);
1230         free(widget);
1231 }
1232
1233 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1234 {
1235         if (!widget)
1236                 return NULL;
1237
1238         return widget->wnd;
1239 }
1240
1241 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1242                               wlt_widget_redraw_cb cb)
1243 {
1244         if (!widget)
1245                 return;
1246
1247         widget->redraw_cb = cb;
1248 }
1249
1250 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1251                                wlt_widget_destroy_cb cb)
1252 {
1253         if (!widget)
1254                 return;
1255
1256         widget->destroy_cb = cb;
1257 }
1258
1259 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1260                               wlt_widget_prepare_resize_cb prepare_cb,
1261                               wlt_widget_resize_cb cb)
1262 {
1263         if (!widget)
1264                 return;
1265
1266         widget->prepare_resize_cb = prepare_cb;
1267         widget->resize_cb = cb;
1268 }
1269
1270 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1271                                wlt_widget_pointer_enter_cb enter_cb,
1272                                wlt_widget_pointer_leave_cb leave_cb,
1273                                wlt_widget_pointer_motion_cb motion_cb,
1274                                wlt_widget_pointer_button_cb button_cb)
1275 {
1276         if (!widget)
1277                 return;
1278
1279         widget->pointer_enter_cb = enter_cb;
1280         widget->pointer_leave_cb = leave_cb;
1281         widget->pointer_motion_cb = motion_cb;
1282         widget->pointer_button_cb = button_cb;
1283 }