4 * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Wayland Terminal toolkit helpers
36 #include <wayland-client.h>
37 #include <wayland-cursor.h>
38 #include <xkbcommon/xkbcommon.h>
41 #include "shl_dlist.h"
45 #include "wlt_toolkit.h"
47 #define LOG_SUBSYSTEM "wlt_toolkit"
50 struct wl_shm_pool *w_pool;
63 struct ev_eloop *eloop;
64 struct wl_display *dp;
66 struct wl_global_listener *dp_listener;
67 struct shl_hook *listeners;
70 struct shl_dlist window_list;
72 struct wl_compositor *w_comp;
73 struct wl_seat *w_seat;
74 struct wl_shell *w_shell;
78 uint32_t pointer_enter_serial;
79 struct wl_pointer *w_pointer;
80 struct wlt_window *pointer_focus;
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];
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;
99 struct shl_dlist list;
101 wlt_window_close_cb close_cb;
103 struct wlt_display *disp;
104 struct wlt_pool *pool;
106 struct wl_surface *w_surface;
107 struct wl_shell_surface *w_shell_surface;
108 struct wl_buffer *w_buffer;
110 bool buffer_attached;
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;
122 struct wlt_shm_buffer buffer;
123 struct wl_callback *w_frame;
125 struct shl_dlist widget_list;
129 struct shl_dlist list;
130 struct wlt_window *wnd;
132 wlt_widget_redraw_cb redraw_cb;
133 wlt_widget_destroy_cb destroy_cb;
134 wlt_widget_prepare_resize_cb prepare_resize_cb;
135 wlt_widget_resize_cb resize_cb;
136 wlt_widget_pointer_enter_cb pointer_enter_cb;
137 wlt_widget_pointer_leave_cb pointer_leave_cb;
138 wlt_widget_pointer_motion_cb pointer_motion_cb;
139 wlt_widget_pointer_button_cb pointer_button_cb;
140 wlt_widget_keyboard_cb keyboard_cb;
143 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
146 static const char template[] = "/wlterm-shared-XXXXXX";
147 struct wlt_pool *pool;
152 path = getenv("XDG_RUNTIME_DIR");
154 log_error("XDG_RUNTIME_DIR not set");
158 pool = malloc(sizeof(*pool));
161 memset(pool, 0, sizeof(*pool));
164 name = malloc(strlen(path) + sizeof(template));
170 strcat(name, template);
172 fd = mkostemp(name, O_CLOEXEC);
174 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
183 log_warning("cannot unlink temporary file %s (%d): %m",
187 ret = ftruncate(fd, size);
189 log_error("cannot truncate temporary file to length %lu (%d): %m",
195 pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
197 if (pool->data == MAP_FAILED) {
198 log_error("cannot mmap temporary file (%d): %m", errno);
203 pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
205 log_error("cannot create wayland shm pool object");
215 munmap(pool->data, size);
223 static void wlt_pool_free(struct wlt_pool *pool)
225 munmap(pool->data, pool->size);
226 wl_shm_pool_destroy(pool->w_pool);
231 * These cursor-names are based on:
232 * https://bugs.kde.org/attachment.cgi?id=67313
235 static char *(*cursors[])[] = {
236 [WLT_CURSOR_NONE] = &(char*[]) {
239 [WLT_CURSOR_TOP] = &(char*[]) {
244 [WLT_CURSOR_BOTTOM] = &(char*[]) {
249 [WLT_CURSOR_LEFT] = &(char*[]) {
254 [WLT_CURSOR_RIGHT] = &(char*[]) {
259 [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
264 [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
269 [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
270 "bottom_left_corner",
274 [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
275 "bottom_right_corner",
279 [WLT_CURSOR_DRAGGING] = &(char*[]) {
282 "208530c400c041818281048008011002",
285 [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
292 [WLT_CURSOR_IBEAM] = &(char*[]) {
300 static int load_cursors(struct wlt_display *disp)
303 size_t size = sizeof(cursors) / sizeof(*cursors);
304 struct wl_cursor *cursor;
306 disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
308 disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
309 if (!disp->cursor_theme) {
310 log_warning("cannot load curdor theme");
314 for (i = 0; i < WLT_CURSOR_NUM; ++i) {
316 if (i < size && cursors[i]) {
317 for (j = 0; (*cursors[i])[j]; ++j) {
318 cursor = wl_cursor_theme_get_cursor(
326 if (!cursor && i != WLT_CURSOR_NONE)
327 log_warning("cannot load cursor for ID %d", i);
329 disp->cursors[i] = cursor;
335 static void unload_cursors(struct wlt_display *disp)
337 if (disp->cursor_theme)
338 wl_cursor_theme_destroy(disp->cursor_theme);
339 if (disp->w_cursor_surface)
340 wl_surface_destroy(disp->w_cursor_surface);
343 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
346 struct wl_cursor *cur;
347 struct wl_cursor_image *image;
348 struct wl_buffer *buffer;
350 if (cursor >= WLT_CURSOR_NUM)
351 cursor = WLT_CURSOR_LEFT_PTR;
353 if (disp->pointer_enter_serial > disp->cursor_serial)
356 if (!force && cursor == disp->current_cursor)
359 disp->current_cursor = cursor;
360 disp->cursor_serial = disp->pointer_enter_serial;
362 cur = disp->cursors[cursor];
364 wl_pointer_set_cursor(disp->w_pointer,
365 disp->pointer_enter_serial,
370 image = cur->images[0];
371 buffer = wl_cursor_image_get_buffer(image);
373 log_error("cannot load buffer for cursor image");
377 wl_pointer_set_cursor(disp->w_pointer,
378 disp->pointer_enter_serial,
379 disp->w_cursor_surface,
380 image->hotspot_x, image->hotspot_y);
381 wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
382 wl_surface_damage(disp->w_cursor_surface, 0, 0,
383 image->width, image->height);
386 static int dp_mask_update(uint32_t mask, void *data)
388 struct wlt_display *disp = data;
396 if (mask & WL_DISPLAY_READABLE)
398 if (mask & WL_DISPLAY_WRITABLE)
399 mode |= EV_WRITEABLE;
401 ret = ev_fd_update(disp->dp_fd, mode);
403 log_warning("cannot update wayland-fd event-polling modes (%d)",
409 static void dp_event(struct ev_fd *fd, int mask, void *data)
411 struct wlt_display *disp = data;
414 if (mask & (EV_HUP | EV_ERR)) {
415 log_warning("HUP/ERR on wayland socket");
416 disp->state = STATE_HUP;
417 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
418 ev_eloop_rm_fd(disp->dp_fd);
424 if (mask & EV_READABLE)
425 mode |= WL_DISPLAY_READABLE;
426 if (mask & EV_WRITEABLE)
427 mode |= WL_DISPLAY_WRITABLE;
430 wl_display_iterate(disp->dp, mode);
433 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
434 uint32_t serial, struct wl_surface *w_surface,
435 wl_fixed_t x, wl_fixed_t y)
437 struct wlt_display *disp = data;
438 struct shl_dlist *iter;
439 struct wlt_window *wnd;
440 struct wlt_widget *widget;
445 shl_dlist_for_each(iter, &disp->window_list) {
446 wnd = shl_dlist_entry(iter, struct wlt_window, list);
447 if (wnd->w_surface == w_surface)
451 if (iter == &disp->window_list) {
452 log_debug("unknown surface");
456 disp->pointer_enter_serial = serial;
457 disp->last_serial = serial;
458 disp->pointer_focus = wnd;
459 shl_dlist_for_each(iter, &wnd->widget_list) {
460 widget = shl_dlist_entry(iter, struct wlt_widget, list);
461 if (widget->pointer_enter_cb)
462 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
467 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
468 uint32_t serial, struct wl_surface *w_surface)
470 struct wlt_display *disp = data;
471 struct shl_dlist *iter;
472 struct wlt_window *wnd;
473 struct wlt_widget *widget;
475 wnd = disp->pointer_focus;
476 disp->pointer_focus = NULL;
477 disp->last_serial = serial;
482 shl_dlist_for_each(iter, &wnd->widget_list) {
483 widget = shl_dlist_entry(iter, struct wlt_widget, list);
484 if (widget->pointer_leave_cb)
485 widget->pointer_leave_cb(widget, widget->data);
489 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
490 uint32_t time, wl_fixed_t x, wl_fixed_t y)
492 struct wlt_display *disp = data;
493 struct shl_dlist *iter;
494 struct wlt_window *wnd;
495 struct wlt_widget *widget;
497 wnd = disp->pointer_focus;
501 shl_dlist_for_each(iter, &wnd->widget_list) {
502 widget = shl_dlist_entry(iter, struct wlt_widget, list);
503 if (widget->pointer_motion_cb)
504 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
509 static void pointer_button(void *data, struct wl_pointer *w_pointer,
510 uint32_t serial, uint32_t time, uint32_t button,
513 struct wlt_display *disp = data;
514 struct shl_dlist *iter;
515 struct wlt_window *wnd;
516 struct wlt_widget *widget;
518 wnd = disp->pointer_focus;
519 disp->last_serial = serial;
524 shl_dlist_for_each(iter, &wnd->widget_list) {
525 widget = shl_dlist_entry(iter, struct wlt_widget, list);
526 if (widget->pointer_button_cb)
527 widget->pointer_button_cb(widget, button, state,
532 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
533 uint32_t time, uint32_t axis, wl_fixed_t value)
537 static const struct wl_pointer_listener pointer_listener = {
538 .enter = pointer_enter,
539 .leave = pointer_leave,
540 .motion = pointer_motion,
541 .button = pointer_button,
542 .axis = pointer_axis,
545 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
546 uint32_t format, int fd, uint32_t size)
548 struct wlt_display *disp = data;
551 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
552 log_error("invalid keyboard format");
557 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
558 if (map == MAP_FAILED) {
559 log_error("cannot mmap keyboard keymap");
564 disp->xkb_keymap = xkb_map_new_from_string(disp->xkb_ctx, map,
565 XKB_KEYMAP_FORMAT_TEXT_V1,
570 if (!disp->xkb_keymap) {
571 log_error("cannot create xkb keymap");
575 disp->xkb_state = xkb_state_new(disp->xkb_keymap);
576 if (!disp->xkb_state) {
577 log_error("cannot create XKB state object");
578 xkb_map_unref(disp->xkb_keymap);
579 disp->xkb_keymap = NULL;
584 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
585 uint32_t serial, struct wl_surface *w_surface,
586 struct wl_array *keys)
588 struct wlt_display *disp = data;
589 struct shl_dlist *iter;
590 struct wlt_window *wnd;
592 disp->last_serial = serial;
593 if (!disp->xkb_state)
599 shl_dlist_for_each(iter, &disp->window_list) {
600 wnd = shl_dlist_entry(iter, struct wlt_window, list);
601 if (wnd->w_surface == w_surface)
605 if (iter == &disp->window_list)
608 disp->keyboard_focus = wnd;
611 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
612 uint32_t serial, struct wl_surface *surface)
614 struct wlt_display *disp = data;
616 disp->last_serial = serial;
617 disp->keyboard_focus = NULL;
618 ev_timer_update(disp->repeat_timer, NULL);
621 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
622 uint32_t serial, uint32_t time, uint32_t key,
625 struct wlt_display *disp = data;
626 struct wlt_window *wnd = disp->keyboard_focus;
627 uint32_t code, num_syms;
629 enum wl_keyboard_key_state state = state_w;
630 const xkb_keysym_t *syms;
632 struct shl_dlist *iter;
633 struct wlt_widget *widget;
634 struct itimerspec spec;
636 disp->last_serial = serial;
637 if (!disp->xkb_state)
644 mask = shl_get_xkb_mods(disp->xkb_state);
645 num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
646 sym = XKB_KEY_NoSymbol;
650 shl_dlist_for_each(iter, &wnd->widget_list) {
651 widget = shl_dlist_entry(iter, struct wlt_widget, list);
652 if (widget->keyboard_cb)
653 widget->keyboard_cb(widget, mask, sym, state,
657 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
658 sym == disp->repeat_sym) {
659 ev_timer_update(disp->repeat_timer, NULL);
660 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
661 disp->repeat_sym = sym;
662 spec.it_interval.tv_sec = 0;
663 spec.it_interval.tv_nsec = 25 * 1000000;
664 spec.it_value.tv_sec = 0;
665 spec.it_value.tv_nsec = 250 * 1000000;
666 ev_timer_update(disp->repeat_timer, &spec);
670 static void repeat_event(struct ev_timer *timer, uint64_t num, void *data)
672 struct wlt_display *disp = data;
673 struct wlt_window *wnd = disp->keyboard_focus;
674 struct wlt_widget *widget;
675 struct shl_dlist *iter;
681 mask = shl_get_xkb_mods(disp->xkb_state);
682 shl_dlist_for_each(iter, &wnd->widget_list) {
683 widget = shl_dlist_entry(iter, struct wlt_widget, list);
684 if (widget->keyboard_cb)
685 widget->keyboard_cb(widget, mask, disp->repeat_sym,
686 WL_KEYBOARD_KEY_STATE_PRESSED,
691 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
692 uint32_t serial, uint32_t mods_depressed,
693 uint32_t mods_latched, uint32_t mods_locked,
696 struct wlt_display *disp = data;
698 disp->last_serial = serial;
699 if (!disp->xkb_state)
702 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
703 mods_locked, 0, 0, group);
706 static const struct wl_keyboard_listener keyboard_listener = {
707 .keymap = keyboard_keymap,
708 .enter = keyboard_enter,
709 .leave = keyboard_leave,
711 .modifiers = keyboard_modifiers,
714 static void check_ready(struct wlt_display *disp)
716 if (disp->state > STATE_INIT)
725 log_debug("wayland display initialized");
728 disp->state = STATE_RUNNING;
729 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
733 static void seat_capabilities(void *data, struct wl_seat *seat,
734 enum wl_seat_capability caps)
736 struct wlt_display *disp = data;
738 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
739 disp->w_pointer = wl_seat_get_pointer(seat);
740 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
744 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
745 disp->w_keyboard = wl_seat_get_keyboard(seat);
746 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
753 static const struct wl_seat_listener seat_listener = {
754 .capabilities = seat_capabilities,
757 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
758 uint32_t version, void *data)
760 struct wlt_display *disp = data;
762 if (!strcmp(iface, "wl_display")) {
763 log_debug("new wl_display global");
765 } else if (!strcmp(iface, "wl_compositor")) {
767 log_error("global wl_compositor advertised twice");
770 disp->w_comp = wl_display_bind(disp->dp,
772 &wl_compositor_interface);
774 log_error("cannot bind wl_compositor object");
777 } else if (!strcmp(iface, "wl_seat")) {
779 log_error("global wl_seat advertised twice");
782 disp->w_seat = wl_display_bind(disp->dp,
786 log_error("cannot bind wl_seat object");
790 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
791 } else if (!strcmp(iface, "wl_shell")) {
793 log_error("global wl_shell advertised twice");
796 disp->w_shell = wl_display_bind(disp->dp,
798 &wl_shell_interface);
799 if (!disp->w_shell) {
800 log_error("cannot bind wl_shell object");
803 } else if (!strcmp(iface, "wl_shm")) {
805 log_error("global wl_shm advertised twice");
808 disp->w_shm = wl_display_bind(disp->dp,
812 log_error("cannot bind wl_shm object");
816 log_debug("ignoring new unknown global %s", iface);
820 log_debug("new global %s", iface);
825 int wlt_display_new(struct wlt_display **out,
826 struct ev_eloop *eloop)
828 struct wlt_display *disp;
834 log_debug("creating new wlt-display");
836 disp = malloc(sizeof(*disp));
839 memset(disp, 0, sizeof(*disp));
842 shl_dlist_init(&disp->window_list);
844 ret = shl_hook_new(&disp->listeners);
846 log_error("cannot create listeners hook");
850 disp->dp = wl_display_connect(NULL);
852 log_error("cannot connect to wayland socket (%d): %m", errno);
857 ret = ev_eloop_new_fd(eloop,
859 wl_display_get_fd(disp->dp,
866 log_error("cannot create event-fd for wayland display (%d)",
871 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
874 log_error("cannot create repeat-timer");
878 disp->dp_listener = wl_display_add_global_listener(disp->dp,
881 if (!disp->dp_listener) {
882 log_error("cannot add wayland global listener (%d)", errno);
886 disp->xkb_ctx = xkb_context_new(0);
887 if (!disp->xkb_ctx) {
888 log_error("cannot create XKB context");
892 log_debug("wlt-display waiting for globals...");
894 ev_eloop_ref(disp->eloop);
899 ev_eloop_rm_timer(disp->repeat_timer);
901 ev_eloop_rm_fd(disp->dp_fd);
903 wl_display_disconnect(disp->dp);
905 shl_hook_free(disp->listeners);
911 void wlt_display_ref(struct wlt_display *disp)
913 if (!disp || !disp->ref)
919 void wlt_display_unref(struct wlt_display *disp)
921 if (!disp || !disp->ref || --disp->ref)
924 unload_cursors(disp);
925 wl_display_remove_global_listener(disp->dp, disp->dp_listener);
926 wl_display_flush(disp->dp);
927 wl_display_disconnect(disp->dp);
928 ev_eloop_rm_timer(disp->repeat_timer);
929 ev_eloop_rm_fd(disp->dp_fd);
930 xkb_context_unref(disp->xkb_ctx);
931 shl_hook_free(disp->listeners);
932 ev_eloop_unref(disp->eloop);
936 int wlt_display_register_cb(struct wlt_display *disp,
937 wlt_display_cb cb, void *data)
942 return shl_hook_add_cast(disp->listeners, cb, data);
945 void wlt_display_unregister_cb(struct wlt_display *disp,
946 wlt_display_cb cb, void *data)
951 shl_hook_rm_cast(disp->listeners, cb, data);
954 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
957 wl_shell_surface_pong(s, serial);
960 static void wlt_window_do_redraw(struct wlt_window *wnd,
964 struct shl_dlist *iter;
965 struct wlt_widget *widget;
967 struct wlt_rect alloc;
971 alloc.width = wnd->buffer.width;
972 alloc.height = wnd->buffer.height;
973 shl_dlist_for_each(iter, &wnd->widget_list) {
974 widget = shl_dlist_entry(iter, struct wlt_widget, list);
975 if (widget->resize_cb)
976 widget->resize_cb(widget, &alloc, widget->data);
979 memset(wnd->buffer.data, 0,
980 wnd->buffer.stride * wnd->buffer.height);
982 wnd->skip_damage = true;
983 shl_dlist_for_each(iter, &wnd->widget_list) {
984 widget = shl_dlist_entry(iter, struct wlt_widget, list);
985 if (widget->redraw_cb)
986 widget->redraw_cb(widget, widget->data);
988 wnd->skip_damage = false;
990 if (!wnd->buffer_attached) {
991 wnd->buffer_attached = true;
992 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
993 x = (int)oldw - wnd->buffer.width;
996 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
997 y = (int)oldh - wnd->buffer.height;
1000 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1001 wnd->resize_edges = 0;
1004 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1005 wnd->buffer.height);
1008 static int resize_window(struct wlt_window *wnd, unsigned int width,
1009 unsigned int height, bool force_redraw)
1011 struct shl_dlist *iter;
1012 struct wlt_widget *widget;
1013 struct wl_buffer *old_buffer = NULL;
1014 struct wlt_pool *old_pool = NULL;
1017 unsigned int oldw, oldh, neww, newh, minw, minh;
1019 if (!wnd || !width || !height)
1026 shl_dlist_for_each(iter, &wnd->widget_list) {
1027 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1028 if (widget->prepare_resize_cb)
1029 widget->prepare_resize_cb(widget, width, height,
1040 if (width == wnd->buffer.width &&
1041 height == wnd->buffer.height) {
1043 wlt_window_do_redraw(wnd, width, height);
1047 oldw = wnd->buffer.width;
1048 oldh = wnd->buffer.height;
1050 nsize = width * height * 4;
1051 if (wnd->pool && wnd->pool->size >= nsize) {
1052 old_buffer = wnd->w_buffer;
1053 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1056 WL_SHM_FORMAT_ARGB8888);
1057 if (!wnd->w_buffer) {
1058 log_error("cannot create wayland shm buffer");
1059 wnd->w_buffer = old_buffer;
1063 old_pool = wnd->pool;
1064 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1066 log_error("cannot create memory pool");
1067 wnd->pool = old_pool;
1071 old_buffer = wnd->w_buffer;
1072 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1075 WL_SHM_FORMAT_ARGB8888);
1076 if (!wnd->w_buffer) {
1077 log_error("cannot create wayland shm buffer");
1078 wnd->w_buffer = old_buffer;
1079 wlt_pool_free(wnd->pool);
1080 wnd->pool = old_pool;
1085 wnd->buffer.data = wnd->pool->data;
1086 wnd->buffer.width = width;
1087 wnd->buffer.height = height;
1088 wnd->buffer.stride = width * 4;
1089 wnd->buffer_attached = false;
1091 wlt_window_do_redraw(wnd, oldw, oldh);
1094 wl_buffer_destroy(old_buffer);
1096 wlt_pool_free(old_pool);
1101 static void frame_callback(void *data, struct wl_callback *w_callback,
1103 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1105 static const struct wl_callback_listener frame_callback_listener = {
1106 .done = frame_callback,
1109 static void do_frame(struct wlt_window *wnd)
1113 wnd->idle_pending = false;
1114 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1116 if (wnd->need_resize) {
1117 force = wnd->need_redraw;
1118 wnd->need_frame = true;
1119 wnd->need_resize = false;
1120 wnd->need_redraw = false;
1121 resize_window(wnd, wnd->new_width, wnd->new_height, force);
1124 if (wnd->need_redraw) {
1125 wnd->need_frame = true;
1126 wnd->need_redraw = false;
1127 wlt_window_do_redraw(wnd, wnd->buffer.width,
1128 wnd->buffer.height);
1131 if (wnd->need_frame) {
1132 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1133 wl_callback_add_listener(wnd->w_frame,
1134 &frame_callback_listener, wnd);
1138 static void frame_callback(void *data, struct wl_callback *w_callback,
1141 struct wlt_window *wnd = data;
1143 wl_callback_destroy(w_callback);
1144 wnd->w_frame = NULL;
1145 wnd->need_frame = false;
1150 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1152 struct wlt_window *wnd = data;
1158 * Buffer Handling and Frame Scheduling
1159 * We use wl_shm for buffer allocation. This means, we have a single buffer
1160 * client side and the server loads it into its backbuffer for rendering. If the
1161 * server does not do this, we are screwed anyway, but that's on behalf of the
1162 * server, so we don't care.
1164 * However, this means, when we create a buffer, we need to notify the
1165 * compositor and then wait until the compositor has created its back-buffer,
1166 * before we continue using this buffer. We can use the "frame" callback to get
1167 * notified about this.
1169 * The logic we have is:
1170 * You set the boolean flags what action is needed in wlt_window and then call
1171 * "schedule_frame()". If we didn't already do any buffer operations in this
1172 * frame, then this function schedules an idle-callback which then performs
1173 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1174 * that this frame was already used and schedules a frame-callback.
1175 * If during this time another call to schedule_frame() is made, we do nothing
1176 * but wait for the frame-callback. It will then directly perform all the
1177 * requested functions and reschedule a frame-callback.
1178 * If nothing was schedule for one frame, we have no more interest in
1179 * frame-callbacks and thus we set "need_frame" to false again and don't
1180 * schedule any more frame-callbacks.
1182 static void schedule_frame(struct wlt_window *wnd)
1186 if (!wnd || wnd->w_frame)
1189 if (wnd->need_frame || wnd->idle_pending)
1192 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1194 log_error("cannot schedule idle callback: %d", ret);
1196 wnd->idle_pending = true;
1199 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1200 uint32_t edges, int32_t width,
1203 struct wlt_window *wnd = data;
1210 wnd->resize_edges = edges;
1211 wlt_window_set_size(wnd, width, height);
1214 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1218 static const struct wl_shell_surface_listener shell_surface_listener = {
1219 .ping = shell_surface_ping,
1220 .configure = shell_surface_configure,
1221 .popup_done = shell_surface_popup_done,
1224 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1226 struct wlt_window *wnd = data;
1228 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1229 wnd->close_pending = false;
1232 wnd->close_cb(wnd, wnd->data);
1235 int wlt_display_create_window(struct wlt_display *disp,
1236 struct wlt_window **out,
1238 unsigned int height,
1241 struct wlt_window *wnd;
1244 if (!disp || !out || !width || !height)
1247 if (disp->state != STATE_RUNNING) {
1248 log_error("cannot create window, display is not running but in state %u",
1253 wnd = malloc(sizeof(*wnd));
1256 memset(wnd, 0, sizeof(*wnd));
1260 shl_dlist_init(&wnd->widget_list);
1262 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1263 if (!wnd->w_surface) {
1264 log_error("cannot create wayland surface");
1269 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1271 if (!wnd->w_shell_surface) {
1272 log_error("cannot retrieve shell-surface for wayland surface");
1277 wl_shell_surface_add_listener(wnd->w_shell_surface,
1278 &shell_surface_listener, wnd);
1279 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1281 ret = resize_window(wnd, width, height, true);
1283 goto err_shell_surface;
1285 wlt_display_ref(disp);
1286 shl_dlist_link(&disp->window_list, &wnd->list);
1291 wl_shell_surface_destroy(wnd->w_shell_surface);
1293 wl_surface_destroy(wnd->w_surface);
1299 void wlt_window_ref(struct wlt_window *wnd)
1301 if (!wnd || !wnd->ref)
1307 void wlt_window_unref(struct wlt_window *wnd)
1309 struct wlt_widget *widget;
1311 if (!wnd || !wnd->ref || --wnd->ref)
1314 while (!shl_dlist_empty(&wnd->widget_list)) {
1315 widget = shl_dlist_entry(wnd->widget_list.next,
1316 struct wlt_widget, list);
1317 wlt_widget_destroy(widget);
1320 if (wnd->close_pending)
1321 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1323 if (wnd->idle_pending)
1324 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1325 shl_dlist_unlink(&wnd->list);
1327 wl_callback_destroy(wnd->w_frame);
1329 wl_buffer_destroy(wnd->w_buffer);
1331 wlt_pool_free(wnd->pool);
1332 wl_shell_surface_destroy(wnd->w_shell_surface);
1333 wl_surface_destroy(wnd->w_surface);
1334 wlt_display_unref(wnd->disp);
1338 int wlt_window_create_widget(struct wlt_window *wnd,
1339 struct wlt_widget **out,
1342 struct wlt_widget *widget;
1347 widget = malloc(sizeof(*widget));
1350 memset(widget, 0, sizeof(*widget));
1352 widget->data = data;
1354 wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
1355 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1360 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1365 wnd->need_redraw = true;
1366 schedule_frame(wnd);
1369 void wlt_window_damage(struct wlt_window *wnd,
1370 struct wlt_rect *damage)
1372 if (!wnd || !damage || wnd->skip_damage)
1375 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1376 damage->width, damage->height);
1379 void wlt_window_get_buffer(struct wlt_window *wnd,
1380 const struct wlt_rect *alloc,
1381 struct wlt_shm_buffer *buf)
1383 struct wlt_shm_buffer *rbuf;
1388 rbuf = &wnd->buffer;
1391 memcpy(buf, rbuf, sizeof(*buf));
1395 if (alloc->x >= rbuf->width ||
1396 alloc->y >= rbuf->height) {
1397 memset(buf, 0, sizeof(*buf));
1402 if (alloc->x + alloc->width > rbuf->width)
1403 buf->width = rbuf->width - alloc->x;
1405 buf->width = alloc->width;
1408 if (alloc->y + alloc->height > rbuf->height)
1409 buf->height = rbuf->height - alloc->y;
1411 buf->height = alloc->height;
1414 buf->stride = rbuf->stride;
1417 buf->data = rbuf->data;
1418 buf->data += alloc->y * rbuf->stride;
1419 buf->data += alloc->x * 4;
1422 void wlt_window_move(struct wlt_window *wnd)
1427 wl_shell_surface_move(wnd->w_shell_surface,
1429 wnd->disp->last_serial);
1432 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1437 wl_shell_surface_resize(wnd->w_shell_surface,
1439 wnd->disp->last_serial,
1443 int wlt_window_set_size(struct wlt_window *wnd,
1444 unsigned int width, unsigned int height)
1446 if (!wnd || !width || !height)
1449 wnd->new_width = width;
1450 wnd->new_height = height;
1451 wnd->need_resize = true;
1452 schedule_frame(wnd);
1457 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1462 set_cursor(wnd->disp, cursor);
1465 void wlt_window_set_close_cb(struct wlt_window *wnd,
1466 wlt_window_close_cb cb)
1474 void wlt_window_close(struct wlt_window *wnd)
1479 wnd->close_pending = true;
1480 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1483 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1488 if (wnd->maximized) {
1489 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1490 wlt_window_set_size(wnd, wnd->saved_width, wnd->saved_height);
1492 wnd->saved_width = wnd->buffer.width;
1493 wnd->saved_height = wnd->buffer.height;
1494 wl_shell_surface_set_maximized(wnd->w_shell_surface, NULL);
1497 wnd->maximized = !wnd->maximized;
1500 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1505 return wnd->disp->eloop;
1508 void wlt_widget_destroy(struct wlt_widget *widget)
1513 if (widget->destroy_cb)
1514 widget->destroy_cb(widget, widget->data);
1515 shl_dlist_unlink(&widget->list);
1519 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1527 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1528 wlt_widget_redraw_cb cb)
1533 widget->redraw_cb = cb;
1536 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1537 wlt_widget_destroy_cb cb)
1542 widget->destroy_cb = cb;
1545 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1546 wlt_widget_prepare_resize_cb prepare_cb,
1547 wlt_widget_resize_cb cb)
1552 widget->prepare_resize_cb = prepare_cb;
1553 widget->resize_cb = cb;
1556 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1557 wlt_widget_pointer_enter_cb enter_cb,
1558 wlt_widget_pointer_leave_cb leave_cb,
1559 wlt_widget_pointer_motion_cb motion_cb,
1560 wlt_widget_pointer_button_cb button_cb)
1565 widget->pointer_enter_cb = enter_cb;
1566 widget->pointer_leave_cb = leave_cb;
1567 widget->pointer_motion_cb = motion_cb;
1568 widget->pointer_button_cb = button_cb;
1571 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1572 wlt_widget_keyboard_cb cb)
1577 widget->keyboard_cb = cb;