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"
44 #include "wlt_toolkit.h"
46 #define LOG_SUBSYSTEM "wlt_toolkit"
49 struct wl_shm_pool *w_pool;
62 struct ev_eloop *eloop;
63 struct wl_display *dp;
65 struct wl_global_listener *dp_listener;
66 struct shl_hook *listeners;
69 struct shl_dlist window_list;
71 struct wl_compositor *w_comp;
72 struct wl_seat *w_seat;
73 struct wl_shell *w_shell;
77 uint32_t pointer_enter_serial;
78 struct wl_pointer *w_pointer;
79 struct wlt_window *pointer_focus;
81 uint32_t cursor_serial;
82 unsigned int current_cursor;
83 struct wl_surface *w_cursor_surface;
84 struct wl_cursor_theme *cursor_theme;
85 struct wl_cursor *cursors[WLT_CURSOR_NUM];
87 struct wl_keyboard *w_keyboard;
88 struct xkb_context *xkb_ctx;
89 struct xkb_keymap *xkb_keymap;
90 struct xkb_state *xkb_state;
91 struct wlt_window *keyboard_focus;
92 struct ev_timer *repeat_timer;
98 struct shl_dlist list;
100 wlt_window_close_cb close_cb;
102 struct wlt_display *disp;
103 struct wlt_pool *pool;
105 struct wl_surface *w_surface;
106 struct wl_shell_surface *w_shell_surface;
107 struct wl_buffer *w_buffer;
109 bool buffer_attached;
115 unsigned int new_width;
116 unsigned int new_height;
117 unsigned int resize_edges;
118 struct wlt_shm_buffer buffer;
119 struct wl_callback *w_frame;
121 struct shl_dlist widget_list;
125 struct shl_dlist list;
126 struct wlt_window *wnd;
128 wlt_widget_redraw_cb redraw_cb;
129 wlt_widget_destroy_cb destroy_cb;
130 wlt_widget_prepare_resize_cb prepare_resize_cb;
131 wlt_widget_resize_cb resize_cb;
132 wlt_widget_pointer_enter_cb pointer_enter_cb;
133 wlt_widget_pointer_leave_cb pointer_leave_cb;
134 wlt_widget_pointer_motion_cb pointer_motion_cb;
135 wlt_widget_pointer_button_cb pointer_button_cb;
136 wlt_widget_keyboard_cb keyboard_cb;
139 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
142 static const char template[] = "/wlterm-shared-XXXXXX";
143 struct wlt_pool *pool;
148 path = getenv("XDG_RUNTIME_DIR");
150 log_error("XDG_RUNTIME_DIR not set");
154 pool = malloc(sizeof(*pool));
157 memset(pool, 0, sizeof(*pool));
160 name = malloc(strlen(path) + sizeof(template));
166 strcat(name, template);
168 fd = mkostemp(name, O_CLOEXEC);
170 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
179 log_warning("cannot unlink temporary file %s (%d): %m",
183 ret = ftruncate(fd, size);
185 log_error("cannot truncate temporary file to length %lu (%d): %m",
191 pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
193 if (pool->data == MAP_FAILED) {
194 log_error("cannot mmap temporary file (%d): %m", errno);
199 pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
201 log_error("cannot create wayland shm pool object");
211 munmap(pool->data, size);
219 static void wlt_pool_free(struct wlt_pool *pool)
221 munmap(pool->data, pool->size);
222 wl_shm_pool_destroy(pool->w_pool);
227 * These cursor-names are based on:
228 * https://bugs.kde.org/attachment.cgi?id=67313
231 static char *(*cursors[])[] = {
232 [WLT_CURSOR_NONE] = &(char*[]) {
235 [WLT_CURSOR_TOP] = &(char*[]) {
240 [WLT_CURSOR_BOTTOM] = &(char*[]) {
245 [WLT_CURSOR_LEFT] = &(char*[]) {
250 [WLT_CURSOR_RIGHT] = &(char*[]) {
255 [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
260 [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
265 [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
266 "bottom_left_corner",
270 [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
271 "bottom_right_corner",
275 [WLT_CURSOR_DRAGGING] = &(char*[]) {
278 "208530c400c041818281048008011002",
281 [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
288 [WLT_CURSOR_IBEAM] = &(char*[]) {
296 static int load_cursors(struct wlt_display *disp)
299 size_t size = sizeof(cursors) / sizeof(*cursors);
300 struct wl_cursor *cursor;
302 disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
304 disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
305 if (!disp->cursor_theme) {
306 log_warning("cannot load curdor theme");
310 for (i = 0; i < WLT_CURSOR_NUM; ++i) {
312 if (i < size && cursors[i]) {
313 for (j = 0; (*cursors[i])[j]; ++j) {
314 cursor = wl_cursor_theme_get_cursor(
322 if (!cursor && i != WLT_CURSOR_NONE)
323 log_warning("cannot load cursor for ID %d", i);
325 disp->cursors[i] = cursor;
331 static void unload_cursors(struct wlt_display *disp)
333 if (disp->cursor_theme)
334 wl_cursor_theme_destroy(disp->cursor_theme);
335 if (disp->w_cursor_surface)
336 wl_surface_destroy(disp->w_cursor_surface);
339 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
342 struct wl_cursor *cur;
343 struct wl_cursor_image *image;
344 struct wl_buffer *buffer;
346 if (cursor >= WLT_CURSOR_NUM)
347 cursor = WLT_CURSOR_LEFT_PTR;
349 if (disp->pointer_enter_serial > disp->cursor_serial)
352 if (!force && cursor == disp->current_cursor)
355 disp->current_cursor = cursor;
356 disp->cursor_serial = disp->pointer_enter_serial;
358 cur = disp->cursors[cursor];
360 wl_pointer_set_cursor(disp->w_pointer,
361 disp->pointer_enter_serial,
366 image = cur->images[0];
367 buffer = wl_cursor_image_get_buffer(image);
369 log_error("cannot load buffer for cursor image");
373 wl_pointer_set_cursor(disp->w_pointer,
374 disp->pointer_enter_serial,
375 disp->w_cursor_surface,
376 image->hotspot_x, image->hotspot_y);
377 wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
378 wl_surface_damage(disp->w_cursor_surface, 0, 0,
379 image->width, image->height);
382 static int dp_mask_update(uint32_t mask, void *data)
384 struct wlt_display *disp = data;
392 if (mask & WL_DISPLAY_READABLE)
394 if (mask & WL_DISPLAY_WRITABLE)
395 mode |= EV_WRITEABLE;
397 ret = ev_fd_update(disp->dp_fd, mode);
399 log_warning("cannot update wayland-fd event-polling modes (%d)",
405 static void dp_event(struct ev_fd *fd, int mask, void *data)
407 struct wlt_display *disp = data;
410 if (mask & (EV_HUP | EV_ERR)) {
411 log_warning("HUP/ERR on wayland socket");
412 disp->state = STATE_HUP;
413 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
414 ev_eloop_rm_fd(disp->dp_fd);
420 if (mask & EV_READABLE)
421 mode |= WL_DISPLAY_READABLE;
422 if (mask & EV_WRITEABLE)
423 mode |= WL_DISPLAY_WRITABLE;
426 wl_display_iterate(disp->dp, mode);
429 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
430 uint32_t serial, struct wl_surface *w_surface,
431 wl_fixed_t x, wl_fixed_t y)
433 struct wlt_display *disp = data;
434 struct shl_dlist *iter;
435 struct wlt_window *wnd;
436 struct wlt_widget *widget;
441 shl_dlist_for_each(iter, &disp->window_list) {
442 wnd = shl_dlist_entry(iter, struct wlt_window, list);
443 if (wnd->w_surface == w_surface)
447 if (iter == &disp->window_list) {
448 log_debug("unknown surface");
452 disp->pointer_enter_serial = serial;
453 disp->last_serial = serial;
454 disp->pointer_focus = wnd;
455 shl_dlist_for_each(iter, &wnd->widget_list) {
456 widget = shl_dlist_entry(iter, struct wlt_widget, list);
457 if (widget->pointer_enter_cb)
458 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
463 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
464 uint32_t serial, struct wl_surface *w_surface)
466 struct wlt_display *disp = data;
467 struct shl_dlist *iter;
468 struct wlt_window *wnd;
469 struct wlt_widget *widget;
471 wnd = disp->pointer_focus;
472 disp->pointer_focus = NULL;
473 disp->last_serial = serial;
478 shl_dlist_for_each(iter, &wnd->widget_list) {
479 widget = shl_dlist_entry(iter, struct wlt_widget, list);
480 if (widget->pointer_leave_cb)
481 widget->pointer_leave_cb(widget, widget->data);
485 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
486 uint32_t time, wl_fixed_t x, wl_fixed_t y)
488 struct wlt_display *disp = data;
489 struct shl_dlist *iter;
490 struct wlt_window *wnd;
491 struct wlt_widget *widget;
493 wnd = disp->pointer_focus;
497 shl_dlist_for_each(iter, &wnd->widget_list) {
498 widget = shl_dlist_entry(iter, struct wlt_widget, list);
499 if (widget->pointer_motion_cb)
500 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
505 static void pointer_button(void *data, struct wl_pointer *w_pointer,
506 uint32_t serial, uint32_t time, uint32_t button,
509 struct wlt_display *disp = data;
510 struct shl_dlist *iter;
511 struct wlt_window *wnd;
512 struct wlt_widget *widget;
514 wnd = disp->pointer_focus;
515 disp->last_serial = serial;
520 shl_dlist_for_each(iter, &wnd->widget_list) {
521 widget = shl_dlist_entry(iter, struct wlt_widget, list);
522 if (widget->pointer_button_cb)
523 widget->pointer_button_cb(widget, button, state,
528 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
529 uint32_t time, uint32_t axis, wl_fixed_t value)
533 static const struct wl_pointer_listener pointer_listener = {
534 .enter = pointer_enter,
535 .leave = pointer_leave,
536 .motion = pointer_motion,
537 .button = pointer_button,
538 .axis = pointer_axis,
541 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
542 uint32_t format, int fd, uint32_t size)
544 struct wlt_display *disp = data;
547 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
548 log_error("invalid keyboard format");
553 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
554 if (map == MAP_FAILED) {
555 log_error("cannot mmap keyboard keymap");
560 disp->xkb_keymap = xkb_map_new_from_string(disp->xkb_ctx, map,
561 XKB_KEYMAP_FORMAT_TEXT_V1,
566 if (!disp->xkb_keymap) {
567 log_error("cannot create xkb keymap");
571 disp->xkb_state = xkb_state_new(disp->xkb_keymap);
572 if (!disp->xkb_state) {
573 log_error("cannot create XKB state object");
574 xkb_map_unref(disp->xkb_keymap);
575 disp->xkb_keymap = NULL;
580 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
581 uint32_t serial, struct wl_surface *w_surface,
582 struct wl_array *keys)
584 struct wlt_display *disp = data;
585 struct shl_dlist *iter;
586 struct wlt_window *wnd;
588 disp->last_serial = serial;
589 if (!disp->xkb_state)
595 shl_dlist_for_each(iter, &disp->window_list) {
596 wnd = shl_dlist_entry(iter, struct wlt_window, list);
597 if (wnd->w_surface == w_surface)
601 if (iter == &disp->window_list)
604 disp->keyboard_focus = wnd;
607 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
608 uint32_t serial, struct wl_surface *surface)
610 struct wlt_display *disp = data;
612 disp->last_serial = serial;
613 disp->keyboard_focus = NULL;
614 ev_timer_update(disp->repeat_timer, NULL);
617 static unsigned int get_effective_modmask(struct xkb_state *state)
619 unsigned int mods = 0;
621 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT,
622 XKB_STATE_EFFECTIVE))
623 mods |= TSM_SHIFT_MASK;
624 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
625 XKB_STATE_EFFECTIVE))
626 mods |= TSM_LOCK_MASK;
627 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
628 XKB_STATE_EFFECTIVE))
629 mods |= TSM_CONTROL_MASK;
630 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
631 XKB_STATE_EFFECTIVE))
632 mods |= TSM_MOD1_MASK;
633 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO,
634 XKB_STATE_EFFECTIVE))
635 mods |= TSM_MOD4_MASK;
640 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
641 uint32_t serial, uint32_t time, uint32_t key,
644 struct wlt_display *disp = data;
645 struct wlt_window *wnd = disp->keyboard_focus;
646 uint32_t code, num_syms;
648 enum wl_keyboard_key_state state = state_w;
649 const xkb_keysym_t *syms;
651 struct shl_dlist *iter;
652 struct wlt_widget *widget;
653 struct itimerspec spec;
655 disp->last_serial = serial;
656 if (!disp->xkb_state)
663 mask = get_effective_modmask(disp->xkb_state);
664 num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
665 sym = XKB_KEY_NoSymbol;
669 shl_dlist_for_each(iter, &wnd->widget_list) {
670 widget = shl_dlist_entry(iter, struct wlt_widget, list);
671 if (widget->keyboard_cb)
672 widget->keyboard_cb(widget, mask, sym, state,
676 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
677 sym == disp->repeat_sym) {
678 ev_timer_update(disp->repeat_timer, NULL);
679 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
680 disp->repeat_sym = sym;
681 spec.it_interval.tv_sec = 0;
682 spec.it_interval.tv_nsec = 25 * 1000000;
683 spec.it_value.tv_sec = 0;
684 spec.it_value.tv_nsec = 250 * 1000000;
685 ev_timer_update(disp->repeat_timer, &spec);
689 static void repeat_event(struct ev_timer *timer, uint64_t num, void *data)
691 struct wlt_display *disp = data;
692 struct wlt_window *wnd = disp->keyboard_focus;
693 struct wlt_widget *widget;
694 struct shl_dlist *iter;
700 mask = get_effective_modmask(disp->xkb_state);
701 shl_dlist_for_each(iter, &wnd->widget_list) {
702 widget = shl_dlist_entry(iter, struct wlt_widget, list);
703 if (widget->keyboard_cb)
704 widget->keyboard_cb(widget, mask, disp->repeat_sym,
705 WL_KEYBOARD_KEY_STATE_PRESSED,
710 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
711 uint32_t serial, uint32_t mods_depressed,
712 uint32_t mods_latched, uint32_t mods_locked,
715 struct wlt_display *disp = data;
717 disp->last_serial = serial;
718 if (!disp->xkb_state)
721 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
722 mods_locked, 0, 0, group);
725 static const struct wl_keyboard_listener keyboard_listener = {
726 .keymap = keyboard_keymap,
727 .enter = keyboard_enter,
728 .leave = keyboard_leave,
730 .modifiers = keyboard_modifiers,
733 static void check_ready(struct wlt_display *disp)
735 if (disp->state > STATE_INIT)
744 log_debug("wayland display initialized");
747 disp->state = STATE_RUNNING;
748 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
752 static void seat_capabilities(void *data, struct wl_seat *seat,
753 enum wl_seat_capability caps)
755 struct wlt_display *disp = data;
757 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
758 disp->w_pointer = wl_seat_get_pointer(seat);
759 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
763 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
764 disp->w_keyboard = wl_seat_get_keyboard(seat);
765 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
772 static const struct wl_seat_listener seat_listener = {
773 .capabilities = seat_capabilities,
776 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
777 uint32_t version, void *data)
779 struct wlt_display *disp = data;
781 if (!strcmp(iface, "wl_display")) {
782 log_debug("new wl_display global");
784 } else if (!strcmp(iface, "wl_compositor")) {
786 log_error("global wl_compositor advertised twice");
789 disp->w_comp = wl_display_bind(disp->dp,
791 &wl_compositor_interface);
793 log_error("cannot bind wl_compositor object");
796 } else if (!strcmp(iface, "wl_seat")) {
798 log_error("global wl_seat advertised twice");
801 disp->w_seat = wl_display_bind(disp->dp,
805 log_error("cannot bind wl_seat object");
809 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
810 } else if (!strcmp(iface, "wl_shell")) {
812 log_error("global wl_shell advertised twice");
815 disp->w_shell = wl_display_bind(disp->dp,
817 &wl_shell_interface);
818 if (!disp->w_shell) {
819 log_error("cannot bind wl_shell object");
822 } else if (!strcmp(iface, "wl_shm")) {
824 log_error("global wl_shm advertised twice");
827 disp->w_shm = wl_display_bind(disp->dp,
831 log_error("cannot bind wl_shm object");
835 log_debug("ignoring new unknown global %s", iface);
839 log_debug("new global %s", iface);
844 int wlt_display_new(struct wlt_display **out,
845 struct ev_eloop *eloop)
847 struct wlt_display *disp;
853 log_debug("creating new wlt-display");
855 disp = malloc(sizeof(*disp));
858 memset(disp, 0, sizeof(*disp));
861 shl_dlist_init(&disp->window_list);
863 ret = shl_hook_new(&disp->listeners);
865 log_error("cannot create listeners hook");
869 disp->dp = wl_display_connect(NULL);
871 log_error("cannot connect to wayland socket (%d): %m", errno);
876 ret = ev_eloop_new_fd(eloop,
878 wl_display_get_fd(disp->dp,
885 log_error("cannot create event-fd for wayland display (%d)",
890 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
893 log_error("cannot create repeat-timer");
897 disp->dp_listener = wl_display_add_global_listener(disp->dp,
900 if (!disp->dp_listener) {
901 log_error("cannot add wayland global listener (%d)");
905 disp->xkb_ctx = xkb_context_new(0);
906 if (!disp->xkb_ctx) {
907 log_error("cannot create XKB context");
911 log_debug("wlt-display waiting for globals...");
913 ev_eloop_ref(disp->eloop);
918 ev_eloop_rm_timer(disp->repeat_timer);
920 ev_eloop_rm_fd(disp->dp_fd);
922 wl_display_disconnect(disp->dp);
924 shl_hook_free(disp->listeners);
930 void wlt_display_ref(struct wlt_display *disp)
932 if (!disp || !disp->ref)
938 void wlt_display_unref(struct wlt_display *disp)
940 if (!disp || !disp->ref || --disp->ref)
943 unload_cursors(disp);
944 wl_display_remove_global_listener(disp->dp, disp->dp_listener);
945 wl_display_flush(disp->dp);
946 wl_display_disconnect(disp->dp);
947 ev_eloop_rm_timer(disp->repeat_timer);
948 ev_eloop_rm_fd(disp->dp_fd);
949 xkb_context_unref(disp->xkb_ctx);
950 shl_hook_free(disp->listeners);
951 ev_eloop_unref(disp->eloop);
955 int wlt_display_register_cb(struct wlt_display *disp,
956 wlt_display_cb cb, void *data)
961 return shl_hook_add_cast(disp->listeners, cb, data);
964 void wlt_display_unregister_cb(struct wlt_display *disp,
965 wlt_display_cb cb, void *data)
970 shl_hook_rm_cast(disp->listeners, cb, data);
973 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
976 wl_shell_surface_pong(s, serial);
979 static void wlt_window_do_redraw(struct wlt_window *wnd,
983 struct shl_dlist *iter;
984 struct wlt_widget *widget;
986 struct wlt_rect alloc;
990 alloc.width = wnd->buffer.width;
991 alloc.height = wnd->buffer.height;
992 shl_dlist_for_each(iter, &wnd->widget_list) {
993 widget = shl_dlist_entry(iter, struct wlt_widget, list);
994 if (widget->resize_cb)
995 widget->resize_cb(widget, &alloc, widget->data);
998 memset(wnd->buffer.data, 0,
999 wnd->buffer.stride * wnd->buffer.height);
1001 wnd->skip_damage = true;
1002 shl_dlist_for_each(iter, &wnd->widget_list) {
1003 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1004 if (widget->redraw_cb)
1005 widget->redraw_cb(widget, widget->data);
1007 wnd->skip_damage = false;
1009 if (!wnd->buffer_attached) {
1010 wnd->buffer_attached = true;
1011 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
1012 x = (int)oldw - wnd->buffer.width;
1015 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
1016 y = (int)oldh - wnd->buffer.height;
1019 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1020 wnd->resize_edges = 0;
1023 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1024 wnd->buffer.height);
1027 static int resize_window(struct wlt_window *wnd, unsigned int width,
1028 unsigned int height)
1030 struct shl_dlist *iter;
1031 struct wlt_widget *widget;
1032 struct wl_buffer *old_buffer = NULL;
1033 struct wlt_pool *old_pool = NULL;
1036 unsigned int oldw, oldh;
1038 if (!wnd || !width || !height)
1041 shl_dlist_for_each(iter, &wnd->widget_list) {
1042 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1043 if (widget->prepare_resize_cb)
1044 widget->prepare_resize_cb(widget, &width, &height,
1048 if (width == wnd->buffer.width &&
1049 height == wnd->buffer.height)
1052 oldw = wnd->buffer.width;
1053 oldh = wnd->buffer.height;
1055 nsize = width * height * 4;
1056 if (wnd->pool && wnd->pool->size >= nsize) {
1057 old_buffer = wnd->w_buffer;
1058 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1061 WL_SHM_FORMAT_ARGB8888);
1062 if (!wnd->w_buffer) {
1063 log_error("cannot create wayland shm buffer");
1064 wnd->w_buffer = old_buffer;
1068 old_pool = wnd->pool;
1069 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1071 log_error("cannot create memory pool");
1072 wnd->pool = old_pool;
1076 old_buffer = wnd->w_buffer;
1077 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1080 WL_SHM_FORMAT_ARGB8888);
1081 if (!wnd->w_buffer) {
1082 log_error("cannot create wayland shm buffer");
1083 wnd->w_buffer = old_buffer;
1084 wlt_pool_free(wnd->pool);
1085 wnd->pool = old_pool;
1090 wnd->buffer.data = wnd->pool->data;
1091 wnd->buffer.width = width;
1092 wnd->buffer.height = height;
1093 wnd->buffer.stride = width * 4;
1094 wnd->buffer_attached = false;
1096 wlt_window_do_redraw(wnd, oldw, oldh);
1099 wl_buffer_destroy(old_buffer);
1101 wlt_pool_free(old_pool);
1106 static void frame_callback(void *data, struct wl_callback *w_callback,
1108 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1110 static const struct wl_callback_listener frame_callback_listener = {
1111 .done = frame_callback,
1114 static void do_frame(struct wlt_window *wnd)
1116 wnd->idle_pending = false;
1117 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1119 if (wnd->need_resize) {
1120 wnd->need_frame = true;
1121 wnd->need_resize = false;
1122 wnd->need_redraw = false;
1123 resize_window(wnd, wnd->new_width, wnd->new_height);
1126 if (wnd->need_redraw) {
1127 wnd->need_frame = true;
1128 wnd->need_redraw = false;
1129 wlt_window_do_redraw(wnd, wnd->buffer.width,
1130 wnd->buffer.height);
1133 if (wnd->need_frame) {
1134 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1135 wl_callback_add_listener(wnd->w_frame,
1136 &frame_callback_listener, wnd);
1140 static void frame_callback(void *data, struct wl_callback *w_callback,
1143 struct wlt_window *wnd = data;
1145 wl_callback_destroy(w_callback);
1146 wnd->w_frame = NULL;
1147 wnd->need_frame = false;
1152 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1154 struct wlt_window *wnd = data;
1160 * Buffer Handling and Frame Scheduling
1161 * We use wl_shm for buffer allocation. This means, we have a single buffer
1162 * client side and the server loads it into its backbuffer for rendering. If the
1163 * server does not do this, we are screwed anyway, but that's on behalf of the
1164 * server, so we don't care.
1166 * However, this means, when we create a buffer, we need to notify the
1167 * compositor and then wait until the compositor has created its back-buffer,
1168 * before we continue using this buffer. We can use the "frame" callback to get
1169 * notified about this.
1171 * The logic we have is:
1172 * You set the boolean flags what action is needed in wlt_window and then call
1173 * "schedule_frame()". If we didn't already do any buffer operations in this
1174 * frame, then this function schedules an idle-callback which then performs
1175 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1176 * that this frame was already used and schedules a frame-callback.
1177 * If during this time another call to schedule_frame() is made, we do nothing
1178 * but wait for the frame-callback. It will then directly perform all the
1179 * requested functions and reschedule a frame-callback.
1180 * If nothing was schedule for one frame, we have no more interest in
1181 * frame-callbacks and thus we set "need_frame" to false again and don't
1182 * schedule any more frame-callbacks.
1184 static void schedule_frame(struct wlt_window *wnd)
1188 if (!wnd || wnd->w_frame)
1191 if (wnd->need_frame || wnd->idle_pending)
1194 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1196 log_error("cannot schedule idle callback: %d", ret);
1198 wnd->idle_pending = true;
1201 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1202 uint32_t edges, int32_t width,
1205 struct wlt_window *wnd = data;
1212 wnd->resize_edges = edges;
1213 wlt_window_set_size(wnd, width, height);
1216 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1220 static const struct wl_shell_surface_listener shell_surface_listener = {
1221 .ping = shell_surface_ping,
1222 .configure = shell_surface_configure,
1223 .popup_done = shell_surface_popup_done,
1226 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1228 struct wlt_window *wnd = data;
1230 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1231 wnd->close_pending = false;
1234 wnd->close_cb(wnd, wnd->data);
1237 int wlt_display_create_window(struct wlt_display *disp,
1238 struct wlt_window **out,
1240 unsigned int height,
1243 struct wlt_window *wnd;
1246 if (!disp || !out || !width || !height)
1249 if (disp->state != STATE_RUNNING) {
1250 log_error("cannot create window, display is not running but in state %u",
1255 wnd = malloc(sizeof(*wnd));
1258 memset(wnd, 0, sizeof(*wnd));
1262 shl_dlist_init(&wnd->widget_list);
1264 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1265 if (!wnd->w_surface) {
1266 log_error("cannot create wayland surface");
1271 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1273 if (!wnd->w_shell_surface) {
1274 log_error("cannot retrieve shell-surface for wayland surface");
1279 wl_shell_surface_add_listener(wnd->w_shell_surface,
1280 &shell_surface_listener, wnd);
1281 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1283 ret = resize_window(wnd, width, height);
1285 goto err_shell_surface;
1287 wlt_display_ref(disp);
1288 shl_dlist_link(&disp->window_list, &wnd->list);
1293 wl_shell_surface_destroy(wnd->w_shell_surface);
1295 wl_surface_destroy(wnd->w_surface);
1301 void wlt_window_ref(struct wlt_window *wnd)
1303 if (!wnd || !wnd->ref)
1309 void wlt_window_unref(struct wlt_window *wnd)
1311 struct wlt_widget *widget;
1313 if (!wnd || !wnd->ref || --wnd->ref)
1316 while (!shl_dlist_empty(&wnd->widget_list)) {
1317 widget = shl_dlist_entry(wnd->widget_list.next,
1318 struct wlt_widget, list);
1319 wlt_widget_destroy(widget);
1322 if (wnd->close_pending)
1323 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1325 if (wnd->idle_pending)
1326 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1327 shl_dlist_unlink(&wnd->list);
1329 wl_callback_destroy(wnd->w_frame);
1331 wl_buffer_destroy(wnd->w_buffer);
1333 wlt_pool_free(wnd->pool);
1334 wl_shell_surface_destroy(wnd->w_shell_surface);
1335 wl_surface_destroy(wnd->w_surface);
1336 wlt_display_unref(wnd->disp);
1340 int wlt_window_create_widget(struct wlt_window *wnd,
1341 struct wlt_widget **out,
1344 struct wlt_widget *widget;
1349 widget = malloc(sizeof(*widget));
1352 memset(widget, 0, sizeof(*widget));
1354 widget->data = data;
1356 wlt_window_schedule_redraw(wnd);
1357 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1362 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1367 wnd->need_redraw = true;
1368 schedule_frame(wnd);
1371 void wlt_window_damage(struct wlt_window *wnd,
1372 struct wlt_rect *damage)
1374 if (!wnd || !damage || wnd->skip_damage)
1377 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1378 damage->width, damage->height);
1381 void wlt_window_get_buffer(struct wlt_window *wnd,
1382 const struct wlt_rect *alloc,
1383 struct wlt_shm_buffer *buf)
1385 struct wlt_shm_buffer *rbuf;
1390 rbuf = &wnd->buffer;
1393 memcpy(buf, rbuf, sizeof(*buf));
1397 if (alloc->x >= rbuf->width ||
1398 alloc->y >= rbuf->height) {
1399 memset(buf, 0, sizeof(*buf));
1404 if (alloc->x + alloc->width > rbuf->width)
1405 buf->width = rbuf->width - alloc->x;
1407 buf->width = alloc->width;
1410 if (alloc->y + alloc->height > rbuf->height)
1411 buf->height = rbuf->height - alloc->y;
1413 buf->height = alloc->height;
1416 buf->stride = rbuf->stride;
1419 buf->data = rbuf->data;
1420 buf->data += alloc->y * rbuf->stride;
1421 buf->data += alloc->x * 4;
1424 void wlt_window_move(struct wlt_window *wnd)
1429 wl_shell_surface_move(wnd->w_shell_surface,
1431 wnd->disp->last_serial);
1434 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1439 wl_shell_surface_resize(wnd->w_shell_surface,
1441 wnd->disp->last_serial,
1445 int wlt_window_set_size(struct wlt_window *wnd,
1446 unsigned int width, unsigned int height)
1448 if (!wnd || !width || !height)
1451 wnd->new_width = width;
1452 wnd->new_height = height;
1453 wnd->need_resize = true;
1454 schedule_frame(wnd);
1459 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1464 set_cursor(wnd->disp, cursor);
1467 void wlt_window_set_close_cb(struct wlt_window *wnd,
1468 wlt_window_close_cb cb)
1476 void wlt_window_close(struct wlt_window *wnd)
1481 wnd->close_pending = true;
1482 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1485 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1490 return wnd->disp->eloop;
1493 void wlt_widget_destroy(struct wlt_widget *widget)
1498 if (widget->destroy_cb)
1499 widget->destroy_cb(widget, widget->data);
1500 shl_dlist_unlink(&widget->list);
1504 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1512 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1513 wlt_widget_redraw_cb cb)
1518 widget->redraw_cb = cb;
1521 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1522 wlt_widget_destroy_cb cb)
1527 widget->destroy_cb = cb;
1530 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1531 wlt_widget_prepare_resize_cb prepare_cb,
1532 wlt_widget_resize_cb cb)
1537 widget->prepare_resize_cb = prepare_cb;
1538 widget->resize_cb = cb;
1541 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1542 wlt_widget_pointer_enter_cb enter_cb,
1543 wlt_widget_pointer_leave_cb leave_cb,
1544 wlt_widget_pointer_motion_cb motion_cb,
1545 wlt_widget_pointer_button_cb button_cb)
1550 widget->pointer_enter_cb = enter_cb;
1551 widget->pointer_leave_cb = leave_cb;
1552 widget->pointer_motion_cb = motion_cb;
1553 widget->pointer_button_cb = button_cb;
1556 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1557 wlt_widget_keyboard_cb cb)
1562 widget->keyboard_cb = cb;