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 saved_width;
118 unsigned int saved_height;
119 unsigned int resize_edges;
121 struct wlt_shm_buffer buffer;
122 struct wl_callback *w_frame;
124 struct shl_dlist widget_list;
128 struct shl_dlist list;
129 struct wlt_window *wnd;
131 wlt_widget_redraw_cb redraw_cb;
132 wlt_widget_destroy_cb destroy_cb;
133 wlt_widget_prepare_resize_cb prepare_resize_cb;
134 wlt_widget_resize_cb resize_cb;
135 wlt_widget_pointer_enter_cb pointer_enter_cb;
136 wlt_widget_pointer_leave_cb pointer_leave_cb;
137 wlt_widget_pointer_motion_cb pointer_motion_cb;
138 wlt_widget_pointer_button_cb pointer_button_cb;
139 wlt_widget_keyboard_cb keyboard_cb;
142 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
145 static const char template[] = "/wlterm-shared-XXXXXX";
146 struct wlt_pool *pool;
151 path = getenv("XDG_RUNTIME_DIR");
153 log_error("XDG_RUNTIME_DIR not set");
157 pool = malloc(sizeof(*pool));
160 memset(pool, 0, sizeof(*pool));
163 name = malloc(strlen(path) + sizeof(template));
169 strcat(name, template);
171 fd = mkostemp(name, O_CLOEXEC);
173 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
182 log_warning("cannot unlink temporary file %s (%d): %m",
186 ret = ftruncate(fd, size);
188 log_error("cannot truncate temporary file to length %lu (%d): %m",
194 pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
196 if (pool->data == MAP_FAILED) {
197 log_error("cannot mmap temporary file (%d): %m", errno);
202 pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
204 log_error("cannot create wayland shm pool object");
214 munmap(pool->data, size);
222 static void wlt_pool_free(struct wlt_pool *pool)
224 munmap(pool->data, pool->size);
225 wl_shm_pool_destroy(pool->w_pool);
230 * These cursor-names are based on:
231 * https://bugs.kde.org/attachment.cgi?id=67313
234 static char *(*cursors[])[] = {
235 [WLT_CURSOR_NONE] = &(char*[]) {
238 [WLT_CURSOR_TOP] = &(char*[]) {
243 [WLT_CURSOR_BOTTOM] = &(char*[]) {
248 [WLT_CURSOR_LEFT] = &(char*[]) {
253 [WLT_CURSOR_RIGHT] = &(char*[]) {
258 [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
263 [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
268 [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
269 "bottom_left_corner",
273 [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
274 "bottom_right_corner",
278 [WLT_CURSOR_DRAGGING] = &(char*[]) {
281 "208530c400c041818281048008011002",
284 [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
291 [WLT_CURSOR_IBEAM] = &(char*[]) {
299 static int load_cursors(struct wlt_display *disp)
302 size_t size = sizeof(cursors) / sizeof(*cursors);
303 struct wl_cursor *cursor;
305 disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
307 disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
308 if (!disp->cursor_theme) {
309 log_warning("cannot load curdor theme");
313 for (i = 0; i < WLT_CURSOR_NUM; ++i) {
315 if (i < size && cursors[i]) {
316 for (j = 0; (*cursors[i])[j]; ++j) {
317 cursor = wl_cursor_theme_get_cursor(
325 if (!cursor && i != WLT_CURSOR_NONE)
326 log_warning("cannot load cursor for ID %d", i);
328 disp->cursors[i] = cursor;
334 static void unload_cursors(struct wlt_display *disp)
336 if (disp->cursor_theme)
337 wl_cursor_theme_destroy(disp->cursor_theme);
338 if (disp->w_cursor_surface)
339 wl_surface_destroy(disp->w_cursor_surface);
342 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
345 struct wl_cursor *cur;
346 struct wl_cursor_image *image;
347 struct wl_buffer *buffer;
349 if (cursor >= WLT_CURSOR_NUM)
350 cursor = WLT_CURSOR_LEFT_PTR;
352 if (disp->pointer_enter_serial > disp->cursor_serial)
355 if (!force && cursor == disp->current_cursor)
358 disp->current_cursor = cursor;
359 disp->cursor_serial = disp->pointer_enter_serial;
361 cur = disp->cursors[cursor];
363 wl_pointer_set_cursor(disp->w_pointer,
364 disp->pointer_enter_serial,
369 image = cur->images[0];
370 buffer = wl_cursor_image_get_buffer(image);
372 log_error("cannot load buffer for cursor image");
376 wl_pointer_set_cursor(disp->w_pointer,
377 disp->pointer_enter_serial,
378 disp->w_cursor_surface,
379 image->hotspot_x, image->hotspot_y);
380 wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
381 wl_surface_damage(disp->w_cursor_surface, 0, 0,
382 image->width, image->height);
385 static int dp_mask_update(uint32_t mask, void *data)
387 struct wlt_display *disp = data;
395 if (mask & WL_DISPLAY_READABLE)
397 if (mask & WL_DISPLAY_WRITABLE)
398 mode |= EV_WRITEABLE;
400 ret = ev_fd_update(disp->dp_fd, mode);
402 log_warning("cannot update wayland-fd event-polling modes (%d)",
408 static void dp_event(struct ev_fd *fd, int mask, void *data)
410 struct wlt_display *disp = data;
413 if (mask & (EV_HUP | EV_ERR)) {
414 log_warning("HUP/ERR on wayland socket");
415 disp->state = STATE_HUP;
416 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
417 ev_eloop_rm_fd(disp->dp_fd);
423 if (mask & EV_READABLE)
424 mode |= WL_DISPLAY_READABLE;
425 if (mask & EV_WRITEABLE)
426 mode |= WL_DISPLAY_WRITABLE;
429 wl_display_iterate(disp->dp, mode);
432 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
433 uint32_t serial, struct wl_surface *w_surface,
434 wl_fixed_t x, wl_fixed_t y)
436 struct wlt_display *disp = data;
437 struct shl_dlist *iter;
438 struct wlt_window *wnd;
439 struct wlt_widget *widget;
444 shl_dlist_for_each(iter, &disp->window_list) {
445 wnd = shl_dlist_entry(iter, struct wlt_window, list);
446 if (wnd->w_surface == w_surface)
450 if (iter == &disp->window_list) {
451 log_debug("unknown surface");
455 disp->pointer_enter_serial = serial;
456 disp->last_serial = serial;
457 disp->pointer_focus = wnd;
458 shl_dlist_for_each(iter, &wnd->widget_list) {
459 widget = shl_dlist_entry(iter, struct wlt_widget, list);
460 if (widget->pointer_enter_cb)
461 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
466 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
467 uint32_t serial, struct wl_surface *w_surface)
469 struct wlt_display *disp = data;
470 struct shl_dlist *iter;
471 struct wlt_window *wnd;
472 struct wlt_widget *widget;
474 wnd = disp->pointer_focus;
475 disp->pointer_focus = NULL;
476 disp->last_serial = serial;
481 shl_dlist_for_each(iter, &wnd->widget_list) {
482 widget = shl_dlist_entry(iter, struct wlt_widget, list);
483 if (widget->pointer_leave_cb)
484 widget->pointer_leave_cb(widget, widget->data);
488 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
489 uint32_t time, wl_fixed_t x, wl_fixed_t y)
491 struct wlt_display *disp = data;
492 struct shl_dlist *iter;
493 struct wlt_window *wnd;
494 struct wlt_widget *widget;
496 wnd = disp->pointer_focus;
500 shl_dlist_for_each(iter, &wnd->widget_list) {
501 widget = shl_dlist_entry(iter, struct wlt_widget, list);
502 if (widget->pointer_motion_cb)
503 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
508 static void pointer_button(void *data, struct wl_pointer *w_pointer,
509 uint32_t serial, uint32_t time, uint32_t button,
512 struct wlt_display *disp = data;
513 struct shl_dlist *iter;
514 struct wlt_window *wnd;
515 struct wlt_widget *widget;
517 wnd = disp->pointer_focus;
518 disp->last_serial = serial;
523 shl_dlist_for_each(iter, &wnd->widget_list) {
524 widget = shl_dlist_entry(iter, struct wlt_widget, list);
525 if (widget->pointer_button_cb)
526 widget->pointer_button_cb(widget, button, state,
531 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
532 uint32_t time, uint32_t axis, wl_fixed_t value)
536 static const struct wl_pointer_listener pointer_listener = {
537 .enter = pointer_enter,
538 .leave = pointer_leave,
539 .motion = pointer_motion,
540 .button = pointer_button,
541 .axis = pointer_axis,
544 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
545 uint32_t format, int fd, uint32_t size)
547 struct wlt_display *disp = data;
550 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
551 log_error("invalid keyboard format");
556 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
557 if (map == MAP_FAILED) {
558 log_error("cannot mmap keyboard keymap");
563 disp->xkb_keymap = xkb_map_new_from_string(disp->xkb_ctx, map,
564 XKB_KEYMAP_FORMAT_TEXT_V1,
569 if (!disp->xkb_keymap) {
570 log_error("cannot create xkb keymap");
574 disp->xkb_state = xkb_state_new(disp->xkb_keymap);
575 if (!disp->xkb_state) {
576 log_error("cannot create XKB state object");
577 xkb_map_unref(disp->xkb_keymap);
578 disp->xkb_keymap = NULL;
583 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
584 uint32_t serial, struct wl_surface *w_surface,
585 struct wl_array *keys)
587 struct wlt_display *disp = data;
588 struct shl_dlist *iter;
589 struct wlt_window *wnd;
591 disp->last_serial = serial;
592 if (!disp->xkb_state)
598 shl_dlist_for_each(iter, &disp->window_list) {
599 wnd = shl_dlist_entry(iter, struct wlt_window, list);
600 if (wnd->w_surface == w_surface)
604 if (iter == &disp->window_list)
607 disp->keyboard_focus = wnd;
610 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
611 uint32_t serial, struct wl_surface *surface)
613 struct wlt_display *disp = data;
615 disp->last_serial = serial;
616 disp->keyboard_focus = NULL;
617 ev_timer_update(disp->repeat_timer, NULL);
620 static unsigned int get_effective_modmask(struct xkb_state *state)
622 unsigned int mods = 0;
624 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT,
625 XKB_STATE_EFFECTIVE))
626 mods |= TSM_SHIFT_MASK;
627 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
628 XKB_STATE_EFFECTIVE))
629 mods |= TSM_LOCK_MASK;
630 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
631 XKB_STATE_EFFECTIVE))
632 mods |= TSM_CONTROL_MASK;
633 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
634 XKB_STATE_EFFECTIVE))
635 mods |= TSM_MOD1_MASK;
636 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO,
637 XKB_STATE_EFFECTIVE))
638 mods |= TSM_MOD4_MASK;
643 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
644 uint32_t serial, uint32_t time, uint32_t key,
647 struct wlt_display *disp = data;
648 struct wlt_window *wnd = disp->keyboard_focus;
649 uint32_t code, num_syms;
651 enum wl_keyboard_key_state state = state_w;
652 const xkb_keysym_t *syms;
654 struct shl_dlist *iter;
655 struct wlt_widget *widget;
656 struct itimerspec spec;
658 disp->last_serial = serial;
659 if (!disp->xkb_state)
666 mask = get_effective_modmask(disp->xkb_state);
667 num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
668 sym = XKB_KEY_NoSymbol;
672 shl_dlist_for_each(iter, &wnd->widget_list) {
673 widget = shl_dlist_entry(iter, struct wlt_widget, list);
674 if (widget->keyboard_cb)
675 widget->keyboard_cb(widget, mask, sym, state,
679 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
680 sym == disp->repeat_sym) {
681 ev_timer_update(disp->repeat_timer, NULL);
682 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
683 disp->repeat_sym = sym;
684 spec.it_interval.tv_sec = 0;
685 spec.it_interval.tv_nsec = 25 * 1000000;
686 spec.it_value.tv_sec = 0;
687 spec.it_value.tv_nsec = 250 * 1000000;
688 ev_timer_update(disp->repeat_timer, &spec);
692 static void repeat_event(struct ev_timer *timer, uint64_t num, void *data)
694 struct wlt_display *disp = data;
695 struct wlt_window *wnd = disp->keyboard_focus;
696 struct wlt_widget *widget;
697 struct shl_dlist *iter;
703 mask = get_effective_modmask(disp->xkb_state);
704 shl_dlist_for_each(iter, &wnd->widget_list) {
705 widget = shl_dlist_entry(iter, struct wlt_widget, list);
706 if (widget->keyboard_cb)
707 widget->keyboard_cb(widget, mask, disp->repeat_sym,
708 WL_KEYBOARD_KEY_STATE_PRESSED,
713 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
714 uint32_t serial, uint32_t mods_depressed,
715 uint32_t mods_latched, uint32_t mods_locked,
718 struct wlt_display *disp = data;
720 disp->last_serial = serial;
721 if (!disp->xkb_state)
724 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
725 mods_locked, 0, 0, group);
728 static const struct wl_keyboard_listener keyboard_listener = {
729 .keymap = keyboard_keymap,
730 .enter = keyboard_enter,
731 .leave = keyboard_leave,
733 .modifiers = keyboard_modifiers,
736 static void check_ready(struct wlt_display *disp)
738 if (disp->state > STATE_INIT)
747 log_debug("wayland display initialized");
750 disp->state = STATE_RUNNING;
751 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
755 static void seat_capabilities(void *data, struct wl_seat *seat,
756 enum wl_seat_capability caps)
758 struct wlt_display *disp = data;
760 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
761 disp->w_pointer = wl_seat_get_pointer(seat);
762 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
766 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
767 disp->w_keyboard = wl_seat_get_keyboard(seat);
768 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
775 static const struct wl_seat_listener seat_listener = {
776 .capabilities = seat_capabilities,
779 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
780 uint32_t version, void *data)
782 struct wlt_display *disp = data;
784 if (!strcmp(iface, "wl_display")) {
785 log_debug("new wl_display global");
787 } else if (!strcmp(iface, "wl_compositor")) {
789 log_error("global wl_compositor advertised twice");
792 disp->w_comp = wl_display_bind(disp->dp,
794 &wl_compositor_interface);
796 log_error("cannot bind wl_compositor object");
799 } else if (!strcmp(iface, "wl_seat")) {
801 log_error("global wl_seat advertised twice");
804 disp->w_seat = wl_display_bind(disp->dp,
808 log_error("cannot bind wl_seat object");
812 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
813 } else if (!strcmp(iface, "wl_shell")) {
815 log_error("global wl_shell advertised twice");
818 disp->w_shell = wl_display_bind(disp->dp,
820 &wl_shell_interface);
821 if (!disp->w_shell) {
822 log_error("cannot bind wl_shell object");
825 } else if (!strcmp(iface, "wl_shm")) {
827 log_error("global wl_shm advertised twice");
830 disp->w_shm = wl_display_bind(disp->dp,
834 log_error("cannot bind wl_shm object");
838 log_debug("ignoring new unknown global %s", iface);
842 log_debug("new global %s", iface);
847 int wlt_display_new(struct wlt_display **out,
848 struct ev_eloop *eloop)
850 struct wlt_display *disp;
856 log_debug("creating new wlt-display");
858 disp = malloc(sizeof(*disp));
861 memset(disp, 0, sizeof(*disp));
864 shl_dlist_init(&disp->window_list);
866 ret = shl_hook_new(&disp->listeners);
868 log_error("cannot create listeners hook");
872 disp->dp = wl_display_connect(NULL);
874 log_error("cannot connect to wayland socket (%d): %m", errno);
879 ret = ev_eloop_new_fd(eloop,
881 wl_display_get_fd(disp->dp,
888 log_error("cannot create event-fd for wayland display (%d)",
893 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
896 log_error("cannot create repeat-timer");
900 disp->dp_listener = wl_display_add_global_listener(disp->dp,
903 if (!disp->dp_listener) {
904 log_error("cannot add wayland global listener (%d)", errno);
908 disp->xkb_ctx = xkb_context_new(0);
909 if (!disp->xkb_ctx) {
910 log_error("cannot create XKB context");
914 log_debug("wlt-display waiting for globals...");
916 ev_eloop_ref(disp->eloop);
921 ev_eloop_rm_timer(disp->repeat_timer);
923 ev_eloop_rm_fd(disp->dp_fd);
925 wl_display_disconnect(disp->dp);
927 shl_hook_free(disp->listeners);
933 void wlt_display_ref(struct wlt_display *disp)
935 if (!disp || !disp->ref)
941 void wlt_display_unref(struct wlt_display *disp)
943 if (!disp || !disp->ref || --disp->ref)
946 unload_cursors(disp);
947 wl_display_remove_global_listener(disp->dp, disp->dp_listener);
948 wl_display_flush(disp->dp);
949 wl_display_disconnect(disp->dp);
950 ev_eloop_rm_timer(disp->repeat_timer);
951 ev_eloop_rm_fd(disp->dp_fd);
952 xkb_context_unref(disp->xkb_ctx);
953 shl_hook_free(disp->listeners);
954 ev_eloop_unref(disp->eloop);
958 int wlt_display_register_cb(struct wlt_display *disp,
959 wlt_display_cb cb, void *data)
964 return shl_hook_add_cast(disp->listeners, cb, data);
967 void wlt_display_unregister_cb(struct wlt_display *disp,
968 wlt_display_cb cb, void *data)
973 shl_hook_rm_cast(disp->listeners, cb, data);
976 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
979 wl_shell_surface_pong(s, serial);
982 static void wlt_window_do_redraw(struct wlt_window *wnd,
986 struct shl_dlist *iter;
987 struct wlt_widget *widget;
989 struct wlt_rect alloc;
993 alloc.width = wnd->buffer.width;
994 alloc.height = wnd->buffer.height;
995 shl_dlist_for_each(iter, &wnd->widget_list) {
996 widget = shl_dlist_entry(iter, struct wlt_widget, list);
997 if (widget->resize_cb)
998 widget->resize_cb(widget, &alloc, widget->data);
1001 memset(wnd->buffer.data, 0,
1002 wnd->buffer.stride * wnd->buffer.height);
1004 wnd->skip_damage = true;
1005 shl_dlist_for_each(iter, &wnd->widget_list) {
1006 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1007 if (widget->redraw_cb)
1008 widget->redraw_cb(widget, widget->data);
1010 wnd->skip_damage = false;
1012 if (!wnd->buffer_attached) {
1013 wnd->buffer_attached = true;
1014 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
1015 x = (int)oldw - wnd->buffer.width;
1018 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
1019 y = (int)oldh - wnd->buffer.height;
1022 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1023 wnd->resize_edges = 0;
1026 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1027 wnd->buffer.height);
1030 static int resize_window(struct wlt_window *wnd, unsigned int width,
1031 unsigned int height, bool force_redraw)
1033 struct shl_dlist *iter;
1034 struct wlt_widget *widget;
1035 struct wl_buffer *old_buffer = NULL;
1036 struct wlt_pool *old_pool = NULL;
1039 unsigned int oldw, oldh, neww, newh;
1041 if (!wnd || !width || !height)
1046 shl_dlist_for_each(iter, &wnd->widget_list) {
1047 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1048 if (widget->prepare_resize_cb)
1049 widget->prepare_resize_cb(widget, width, height,
1059 if (width == wnd->buffer.width &&
1060 height == wnd->buffer.height) {
1062 wlt_window_do_redraw(wnd, width, height);
1066 oldw = wnd->buffer.width;
1067 oldh = wnd->buffer.height;
1069 nsize = width * height * 4;
1070 if (wnd->pool && wnd->pool->size >= nsize) {
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;
1082 old_pool = wnd->pool;
1083 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1085 log_error("cannot create memory pool");
1086 wnd->pool = old_pool;
1090 old_buffer = wnd->w_buffer;
1091 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1094 WL_SHM_FORMAT_ARGB8888);
1095 if (!wnd->w_buffer) {
1096 log_error("cannot create wayland shm buffer");
1097 wnd->w_buffer = old_buffer;
1098 wlt_pool_free(wnd->pool);
1099 wnd->pool = old_pool;
1104 wnd->buffer.data = wnd->pool->data;
1105 wnd->buffer.width = width;
1106 wnd->buffer.height = height;
1107 wnd->buffer.stride = width * 4;
1108 wnd->buffer_attached = false;
1110 wlt_window_do_redraw(wnd, oldw, oldh);
1113 wl_buffer_destroy(old_buffer);
1115 wlt_pool_free(old_pool);
1120 static void frame_callback(void *data, struct wl_callback *w_callback,
1122 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1124 static const struct wl_callback_listener frame_callback_listener = {
1125 .done = frame_callback,
1128 static void do_frame(struct wlt_window *wnd)
1132 wnd->idle_pending = false;
1133 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1135 if (wnd->need_resize) {
1136 force = wnd->need_redraw;
1137 wnd->need_frame = true;
1138 wnd->need_resize = false;
1139 wnd->need_redraw = false;
1140 resize_window(wnd, wnd->new_width, wnd->new_height, force);
1143 if (wnd->need_redraw) {
1144 wnd->need_frame = true;
1145 wnd->need_redraw = false;
1146 wlt_window_do_redraw(wnd, wnd->buffer.width,
1147 wnd->buffer.height);
1150 if (wnd->need_frame) {
1151 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1152 wl_callback_add_listener(wnd->w_frame,
1153 &frame_callback_listener, wnd);
1157 static void frame_callback(void *data, struct wl_callback *w_callback,
1160 struct wlt_window *wnd = data;
1162 wl_callback_destroy(w_callback);
1163 wnd->w_frame = NULL;
1164 wnd->need_frame = false;
1169 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1171 struct wlt_window *wnd = data;
1177 * Buffer Handling and Frame Scheduling
1178 * We use wl_shm for buffer allocation. This means, we have a single buffer
1179 * client side and the server loads it into its backbuffer for rendering. If the
1180 * server does not do this, we are screwed anyway, but that's on behalf of the
1181 * server, so we don't care.
1183 * However, this means, when we create a buffer, we need to notify the
1184 * compositor and then wait until the compositor has created its back-buffer,
1185 * before we continue using this buffer. We can use the "frame" callback to get
1186 * notified about this.
1188 * The logic we have is:
1189 * You set the boolean flags what action is needed in wlt_window and then call
1190 * "schedule_frame()". If we didn't already do any buffer operations in this
1191 * frame, then this function schedules an idle-callback which then performs
1192 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1193 * that this frame was already used and schedules a frame-callback.
1194 * If during this time another call to schedule_frame() is made, we do nothing
1195 * but wait for the frame-callback. It will then directly perform all the
1196 * requested functions and reschedule a frame-callback.
1197 * If nothing was schedule for one frame, we have no more interest in
1198 * frame-callbacks and thus we set "need_frame" to false again and don't
1199 * schedule any more frame-callbacks.
1201 static void schedule_frame(struct wlt_window *wnd)
1205 if (!wnd || wnd->w_frame)
1208 if (wnd->need_frame || wnd->idle_pending)
1211 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1213 log_error("cannot schedule idle callback: %d", ret);
1215 wnd->idle_pending = true;
1218 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1219 uint32_t edges, int32_t width,
1222 struct wlt_window *wnd = data;
1229 wnd->resize_edges = edges;
1230 wlt_window_set_size(wnd, width, height);
1233 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1237 static const struct wl_shell_surface_listener shell_surface_listener = {
1238 .ping = shell_surface_ping,
1239 .configure = shell_surface_configure,
1240 .popup_done = shell_surface_popup_done,
1243 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1245 struct wlt_window *wnd = data;
1247 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1248 wnd->close_pending = false;
1251 wnd->close_cb(wnd, wnd->data);
1254 int wlt_display_create_window(struct wlt_display *disp,
1255 struct wlt_window **out,
1257 unsigned int height,
1260 struct wlt_window *wnd;
1263 if (!disp || !out || !width || !height)
1266 if (disp->state != STATE_RUNNING) {
1267 log_error("cannot create window, display is not running but in state %u",
1272 wnd = malloc(sizeof(*wnd));
1275 memset(wnd, 0, sizeof(*wnd));
1279 shl_dlist_init(&wnd->widget_list);
1281 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1282 if (!wnd->w_surface) {
1283 log_error("cannot create wayland surface");
1288 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1290 if (!wnd->w_shell_surface) {
1291 log_error("cannot retrieve shell-surface for wayland surface");
1296 wl_shell_surface_add_listener(wnd->w_shell_surface,
1297 &shell_surface_listener, wnd);
1298 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1300 ret = resize_window(wnd, width, height, true);
1302 goto err_shell_surface;
1304 wlt_display_ref(disp);
1305 shl_dlist_link(&disp->window_list, &wnd->list);
1310 wl_shell_surface_destroy(wnd->w_shell_surface);
1312 wl_surface_destroy(wnd->w_surface);
1318 void wlt_window_ref(struct wlt_window *wnd)
1320 if (!wnd || !wnd->ref)
1326 void wlt_window_unref(struct wlt_window *wnd)
1328 struct wlt_widget *widget;
1330 if (!wnd || !wnd->ref || --wnd->ref)
1333 while (!shl_dlist_empty(&wnd->widget_list)) {
1334 widget = shl_dlist_entry(wnd->widget_list.next,
1335 struct wlt_widget, list);
1336 wlt_widget_destroy(widget);
1339 if (wnd->close_pending)
1340 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1342 if (wnd->idle_pending)
1343 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1344 shl_dlist_unlink(&wnd->list);
1346 wl_callback_destroy(wnd->w_frame);
1348 wl_buffer_destroy(wnd->w_buffer);
1350 wlt_pool_free(wnd->pool);
1351 wl_shell_surface_destroy(wnd->w_shell_surface);
1352 wl_surface_destroy(wnd->w_surface);
1353 wlt_display_unref(wnd->disp);
1357 int wlt_window_create_widget(struct wlt_window *wnd,
1358 struct wlt_widget **out,
1361 struct wlt_widget *widget;
1366 widget = malloc(sizeof(*widget));
1369 memset(widget, 0, sizeof(*widget));
1371 widget->data = data;
1373 wlt_window_schedule_redraw(wnd);
1374 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1379 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1384 wnd->need_redraw = true;
1385 schedule_frame(wnd);
1388 void wlt_window_damage(struct wlt_window *wnd,
1389 struct wlt_rect *damage)
1391 if (!wnd || !damage || wnd->skip_damage)
1394 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1395 damage->width, damage->height);
1398 void wlt_window_get_buffer(struct wlt_window *wnd,
1399 const struct wlt_rect *alloc,
1400 struct wlt_shm_buffer *buf)
1402 struct wlt_shm_buffer *rbuf;
1407 rbuf = &wnd->buffer;
1410 memcpy(buf, rbuf, sizeof(*buf));
1414 if (alloc->x >= rbuf->width ||
1415 alloc->y >= rbuf->height) {
1416 memset(buf, 0, sizeof(*buf));
1421 if (alloc->x + alloc->width > rbuf->width)
1422 buf->width = rbuf->width - alloc->x;
1424 buf->width = alloc->width;
1427 if (alloc->y + alloc->height > rbuf->height)
1428 buf->height = rbuf->height - alloc->y;
1430 buf->height = alloc->height;
1433 buf->stride = rbuf->stride;
1436 buf->data = rbuf->data;
1437 buf->data += alloc->y * rbuf->stride;
1438 buf->data += alloc->x * 4;
1441 void wlt_window_move(struct wlt_window *wnd)
1446 wl_shell_surface_move(wnd->w_shell_surface,
1448 wnd->disp->last_serial);
1451 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1456 wl_shell_surface_resize(wnd->w_shell_surface,
1458 wnd->disp->last_serial,
1462 int wlt_window_set_size(struct wlt_window *wnd,
1463 unsigned int width, unsigned int height)
1465 if (!wnd || !width || !height)
1468 wnd->new_width = width;
1469 wnd->new_height = height;
1470 wnd->need_resize = true;
1471 schedule_frame(wnd);
1476 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1481 set_cursor(wnd->disp, cursor);
1484 void wlt_window_set_close_cb(struct wlt_window *wnd,
1485 wlt_window_close_cb cb)
1493 void wlt_window_close(struct wlt_window *wnd)
1498 wnd->close_pending = true;
1499 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1502 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1507 if (wnd->maximized) {
1508 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1509 wlt_window_set_size(wnd, wnd->saved_width, wnd->saved_height);
1511 wnd->saved_width = wnd->buffer.width;
1512 wnd->saved_height = wnd->buffer.height;
1513 wl_shell_surface_set_maximized(wnd->w_shell_surface, NULL);
1516 wnd->maximized = !wnd->maximized;
1519 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1524 return wnd->disp->eloop;
1527 void wlt_widget_destroy(struct wlt_widget *widget)
1532 if (widget->destroy_cb)
1533 widget->destroy_cb(widget, widget->data);
1534 shl_dlist_unlink(&widget->list);
1538 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1546 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1547 wlt_widget_redraw_cb cb)
1552 widget->redraw_cb = cb;
1555 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1556 wlt_widget_destroy_cb cb)
1561 widget->destroy_cb = cb;
1564 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1565 wlt_widget_prepare_resize_cb prepare_cb,
1566 wlt_widget_resize_cb cb)
1571 widget->prepare_resize_cb = prepare_cb;
1572 widget->resize_cb = cb;
1575 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1576 wlt_widget_pointer_enter_cb enter_cb,
1577 wlt_widget_pointer_leave_cb leave_cb,
1578 wlt_widget_pointer_motion_cb motion_cb,
1579 wlt_widget_pointer_button_cb button_cb)
1584 widget->pointer_enter_cb = enter_cb;
1585 widget->pointer_leave_cb = leave_cb;
1586 widget->pointer_motion_cb = motion_cb;
1587 widget->pointer_button_cb = button_cb;
1590 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1591 wlt_widget_keyboard_cb cb)
1596 widget->keyboard_cb = cb;