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;
123 struct wlt_shm_buffer buffer;
124 struct wl_callback *w_frame;
126 struct shl_dlist widget_list;
130 struct shl_dlist list;
131 struct wlt_window *wnd;
133 wlt_widget_redraw_cb redraw_cb;
134 wlt_widget_destroy_cb destroy_cb;
135 wlt_widget_prepare_resize_cb prepare_resize_cb;
136 wlt_widget_resize_cb resize_cb;
137 wlt_widget_pointer_enter_cb pointer_enter_cb;
138 wlt_widget_pointer_leave_cb pointer_leave_cb;
139 wlt_widget_pointer_motion_cb pointer_motion_cb;
140 wlt_widget_pointer_button_cb pointer_button_cb;
141 wlt_widget_keyboard_cb keyboard_cb;
144 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
147 static const char template[] = "/wlterm-shared-XXXXXX";
148 struct wlt_pool *pool;
153 path = getenv("XDG_RUNTIME_DIR");
155 log_error("XDG_RUNTIME_DIR not set");
159 pool = malloc(sizeof(*pool));
162 memset(pool, 0, sizeof(*pool));
165 name = malloc(strlen(path) + sizeof(template));
171 strcat(name, template);
173 fd = mkostemp(name, O_CLOEXEC);
175 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
184 log_warning("cannot unlink temporary file %s (%d): %m",
188 ret = ftruncate(fd, size);
190 log_error("cannot truncate temporary file to length %lu (%d): %m",
196 pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
198 if (pool->data == MAP_FAILED) {
199 log_error("cannot mmap temporary file (%d): %m", errno);
204 pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
206 log_error("cannot create wayland shm pool object");
216 munmap(pool->data, size);
224 static void wlt_pool_free(struct wlt_pool *pool)
226 munmap(pool->data, pool->size);
227 wl_shm_pool_destroy(pool->w_pool);
232 * These cursor-names are based on:
233 * https://bugs.kde.org/attachment.cgi?id=67313
236 static char *(*cursors[])[] = {
237 [WLT_CURSOR_NONE] = &(char*[]) {
240 [WLT_CURSOR_TOP] = &(char*[]) {
245 [WLT_CURSOR_BOTTOM] = &(char*[]) {
250 [WLT_CURSOR_LEFT] = &(char*[]) {
255 [WLT_CURSOR_RIGHT] = &(char*[]) {
260 [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
265 [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
270 [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
271 "bottom_left_corner",
275 [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
276 "bottom_right_corner",
280 [WLT_CURSOR_DRAGGING] = &(char*[]) {
283 "208530c400c041818281048008011002",
286 [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
293 [WLT_CURSOR_IBEAM] = &(char*[]) {
301 static int load_cursors(struct wlt_display *disp)
304 size_t size = sizeof(cursors) / sizeof(*cursors);
305 struct wl_cursor *cursor;
307 disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
309 disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
310 if (!disp->cursor_theme) {
311 log_warning("cannot load curdor theme");
315 for (i = 0; i < WLT_CURSOR_NUM; ++i) {
317 if (i < size && cursors[i]) {
318 for (j = 0; (*cursors[i])[j]; ++j) {
319 cursor = wl_cursor_theme_get_cursor(
327 if (!cursor && i != WLT_CURSOR_NONE)
328 log_warning("cannot load cursor for ID %d", i);
330 disp->cursors[i] = cursor;
336 static void unload_cursors(struct wlt_display *disp)
338 if (disp->cursor_theme)
339 wl_cursor_theme_destroy(disp->cursor_theme);
340 if (disp->w_cursor_surface)
341 wl_surface_destroy(disp->w_cursor_surface);
344 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
347 struct wl_cursor *cur;
348 struct wl_cursor_image *image;
349 struct wl_buffer *buffer;
351 if (cursor >= WLT_CURSOR_NUM)
352 cursor = WLT_CURSOR_LEFT_PTR;
354 if (disp->pointer_enter_serial > disp->cursor_serial)
357 if (!force && cursor == disp->current_cursor)
360 disp->current_cursor = cursor;
361 disp->cursor_serial = disp->pointer_enter_serial;
363 cur = disp->cursors[cursor];
365 wl_pointer_set_cursor(disp->w_pointer,
366 disp->pointer_enter_serial,
371 image = cur->images[0];
372 buffer = wl_cursor_image_get_buffer(image);
374 log_error("cannot load buffer for cursor image");
378 wl_pointer_set_cursor(disp->w_pointer,
379 disp->pointer_enter_serial,
380 disp->w_cursor_surface,
381 image->hotspot_x, image->hotspot_y);
382 wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
383 wl_surface_damage(disp->w_cursor_surface, 0, 0,
384 image->width, image->height);
387 static int dp_mask_update(uint32_t mask, void *data)
389 struct wlt_display *disp = data;
397 if (mask & WL_DISPLAY_READABLE)
399 if (mask & WL_DISPLAY_WRITABLE)
400 mode |= EV_WRITEABLE;
402 ret = ev_fd_update(disp->dp_fd, mode);
404 log_warning("cannot update wayland-fd event-polling modes (%d)",
410 static void dp_event(struct ev_fd *fd, int mask, void *data)
412 struct wlt_display *disp = data;
415 if (mask & (EV_HUP | EV_ERR)) {
416 log_warning("HUP/ERR on wayland socket");
417 disp->state = STATE_HUP;
418 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
419 ev_eloop_rm_fd(disp->dp_fd);
425 if (mask & EV_READABLE)
426 mode |= WL_DISPLAY_READABLE;
427 if (mask & EV_WRITEABLE)
428 mode |= WL_DISPLAY_WRITABLE;
431 wl_display_iterate(disp->dp, mode);
434 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
435 uint32_t serial, struct wl_surface *w_surface,
436 wl_fixed_t x, wl_fixed_t y)
438 struct wlt_display *disp = data;
439 struct shl_dlist *iter;
440 struct wlt_window *wnd;
441 struct wlt_widget *widget;
446 shl_dlist_for_each(iter, &disp->window_list) {
447 wnd = shl_dlist_entry(iter, struct wlt_window, list);
448 if (wnd->w_surface == w_surface)
452 if (iter == &disp->window_list) {
453 log_debug("unknown surface");
457 disp->pointer_enter_serial = serial;
458 disp->last_serial = serial;
459 disp->pointer_focus = wnd;
460 shl_dlist_for_each(iter, &wnd->widget_list) {
461 widget = shl_dlist_entry(iter, struct wlt_widget, list);
462 if (widget->pointer_enter_cb)
463 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
468 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
469 uint32_t serial, struct wl_surface *w_surface)
471 struct wlt_display *disp = data;
472 struct shl_dlist *iter;
473 struct wlt_window *wnd;
474 struct wlt_widget *widget;
476 wnd = disp->pointer_focus;
477 disp->pointer_focus = NULL;
478 disp->last_serial = serial;
483 shl_dlist_for_each(iter, &wnd->widget_list) {
484 widget = shl_dlist_entry(iter, struct wlt_widget, list);
485 if (widget->pointer_leave_cb)
486 widget->pointer_leave_cb(widget, widget->data);
490 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
491 uint32_t time, wl_fixed_t x, wl_fixed_t y)
493 struct wlt_display *disp = data;
494 struct shl_dlist *iter;
495 struct wlt_window *wnd;
496 struct wlt_widget *widget;
498 wnd = disp->pointer_focus;
502 shl_dlist_for_each(iter, &wnd->widget_list) {
503 widget = shl_dlist_entry(iter, struct wlt_widget, list);
504 if (widget->pointer_motion_cb)
505 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
510 static void pointer_button(void *data, struct wl_pointer *w_pointer,
511 uint32_t serial, uint32_t time, uint32_t button,
514 struct wlt_display *disp = data;
515 struct shl_dlist *iter;
516 struct wlt_window *wnd;
517 struct wlt_widget *widget;
519 wnd = disp->pointer_focus;
520 disp->last_serial = serial;
525 shl_dlist_for_each(iter, &wnd->widget_list) {
526 widget = shl_dlist_entry(iter, struct wlt_widget, list);
527 if (widget->pointer_button_cb)
528 widget->pointer_button_cb(widget, button, state,
533 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
534 uint32_t time, uint32_t axis, wl_fixed_t value)
538 static const struct wl_pointer_listener pointer_listener = {
539 .enter = pointer_enter,
540 .leave = pointer_leave,
541 .motion = pointer_motion,
542 .button = pointer_button,
543 .axis = pointer_axis,
546 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
547 uint32_t format, int fd, uint32_t size)
549 struct wlt_display *disp = data;
552 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
553 log_error("invalid keyboard format");
558 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
559 if (map == MAP_FAILED) {
560 log_error("cannot mmap keyboard keymap");
565 disp->xkb_keymap = xkb_map_new_from_string(disp->xkb_ctx, map,
566 XKB_KEYMAP_FORMAT_TEXT_V1,
571 if (!disp->xkb_keymap) {
572 log_error("cannot create xkb keymap");
576 disp->xkb_state = xkb_state_new(disp->xkb_keymap);
577 if (!disp->xkb_state) {
578 log_error("cannot create XKB state object");
579 xkb_map_unref(disp->xkb_keymap);
580 disp->xkb_keymap = NULL;
585 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
586 uint32_t serial, struct wl_surface *w_surface,
587 struct wl_array *keys)
589 struct wlt_display *disp = data;
590 struct shl_dlist *iter;
591 struct wlt_window *wnd;
593 disp->last_serial = serial;
594 if (!disp->xkb_state)
600 shl_dlist_for_each(iter, &disp->window_list) {
601 wnd = shl_dlist_entry(iter, struct wlt_window, list);
602 if (wnd->w_surface == w_surface)
606 if (iter == &disp->window_list)
609 disp->keyboard_focus = wnd;
612 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
613 uint32_t serial, struct wl_surface *surface)
615 struct wlt_display *disp = data;
617 disp->last_serial = serial;
618 disp->keyboard_focus = NULL;
619 ev_timer_update(disp->repeat_timer, NULL);
622 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
623 uint32_t serial, uint32_t time, uint32_t key,
626 struct wlt_display *disp = data;
627 struct wlt_window *wnd = disp->keyboard_focus;
628 uint32_t code, num_syms;
630 enum wl_keyboard_key_state state = state_w;
631 const xkb_keysym_t *syms;
633 struct shl_dlist *iter;
634 struct wlt_widget *widget;
635 struct itimerspec spec;
637 disp->last_serial = serial;
638 if (!disp->xkb_state)
645 mask = shl_get_xkb_mods(disp->xkb_state);
646 num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
647 sym = XKB_KEY_NoSymbol;
651 shl_dlist_for_each(iter, &wnd->widget_list) {
652 widget = shl_dlist_entry(iter, struct wlt_widget, list);
653 if (widget->keyboard_cb)
654 widget->keyboard_cb(widget, mask, sym, state,
658 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
659 sym == disp->repeat_sym) {
660 ev_timer_update(disp->repeat_timer, NULL);
661 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
662 disp->repeat_sym = sym;
663 spec.it_interval.tv_sec = 0;
664 spec.it_interval.tv_nsec = 25 * 1000000;
665 spec.it_value.tv_sec = 0;
666 spec.it_value.tv_nsec = 250 * 1000000;
667 ev_timer_update(disp->repeat_timer, &spec);
671 static void repeat_event(struct ev_timer *timer, uint64_t num, void *data)
673 struct wlt_display *disp = data;
674 struct wlt_window *wnd = disp->keyboard_focus;
675 struct wlt_widget *widget;
676 struct shl_dlist *iter;
682 mask = shl_get_xkb_mods(disp->xkb_state);
683 shl_dlist_for_each(iter, &wnd->widget_list) {
684 widget = shl_dlist_entry(iter, struct wlt_widget, list);
685 if (widget->keyboard_cb)
686 widget->keyboard_cb(widget, mask, disp->repeat_sym,
687 WL_KEYBOARD_KEY_STATE_PRESSED,
692 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
693 uint32_t serial, uint32_t mods_depressed,
694 uint32_t mods_latched, uint32_t mods_locked,
697 struct wlt_display *disp = data;
699 disp->last_serial = serial;
700 if (!disp->xkb_state)
703 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
704 mods_locked, 0, 0, group);
707 static const struct wl_keyboard_listener keyboard_listener = {
708 .keymap = keyboard_keymap,
709 .enter = keyboard_enter,
710 .leave = keyboard_leave,
712 .modifiers = keyboard_modifiers,
715 static void check_ready(struct wlt_display *disp)
717 if (disp->state > STATE_INIT)
726 log_debug("wayland display initialized");
729 disp->state = STATE_RUNNING;
730 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
734 static void seat_capabilities(void *data, struct wl_seat *seat,
735 enum wl_seat_capability caps)
737 struct wlt_display *disp = data;
739 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
740 disp->w_pointer = wl_seat_get_pointer(seat);
741 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
745 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
746 disp->w_keyboard = wl_seat_get_keyboard(seat);
747 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
754 static const struct wl_seat_listener seat_listener = {
755 .capabilities = seat_capabilities,
758 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
759 uint32_t version, void *data)
761 struct wlt_display *disp = data;
763 if (!strcmp(iface, "wl_display")) {
764 log_debug("new wl_display global");
766 } else if (!strcmp(iface, "wl_compositor")) {
768 log_error("global wl_compositor advertised twice");
771 disp->w_comp = wl_display_bind(disp->dp,
773 &wl_compositor_interface);
775 log_error("cannot bind wl_compositor object");
778 } else if (!strcmp(iface, "wl_seat")) {
780 log_error("global wl_seat advertised twice");
783 disp->w_seat = wl_display_bind(disp->dp,
787 log_error("cannot bind wl_seat object");
791 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
792 } else if (!strcmp(iface, "wl_shell")) {
794 log_error("global wl_shell advertised twice");
797 disp->w_shell = wl_display_bind(disp->dp,
799 &wl_shell_interface);
800 if (!disp->w_shell) {
801 log_error("cannot bind wl_shell object");
804 } else if (!strcmp(iface, "wl_shm")) {
806 log_error("global wl_shm advertised twice");
809 disp->w_shm = wl_display_bind(disp->dp,
813 log_error("cannot bind wl_shm object");
817 log_debug("ignoring new unknown global %s", iface);
821 log_debug("new global %s", iface);
826 int wlt_display_new(struct wlt_display **out,
827 struct ev_eloop *eloop)
829 struct wlt_display *disp;
835 log_debug("creating new wlt-display");
837 disp = malloc(sizeof(*disp));
840 memset(disp, 0, sizeof(*disp));
843 shl_dlist_init(&disp->window_list);
845 ret = shl_hook_new(&disp->listeners);
847 log_error("cannot create listeners hook");
851 disp->dp = wl_display_connect(NULL);
853 log_error("cannot connect to wayland socket (%d): %m", errno);
858 ret = ev_eloop_new_fd(eloop,
860 wl_display_get_fd(disp->dp,
867 log_error("cannot create event-fd for wayland display (%d)",
872 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
875 log_error("cannot create repeat-timer");
879 disp->dp_listener = wl_display_add_global_listener(disp->dp,
882 if (!disp->dp_listener) {
883 log_error("cannot add wayland global listener (%d)", errno);
887 disp->xkb_ctx = xkb_context_new(0);
888 if (!disp->xkb_ctx) {
889 log_error("cannot create XKB context");
893 log_debug("wlt-display waiting for globals...");
895 ev_eloop_ref(disp->eloop);
900 ev_eloop_rm_timer(disp->repeat_timer);
902 ev_eloop_rm_fd(disp->dp_fd);
904 wl_display_disconnect(disp->dp);
906 shl_hook_free(disp->listeners);
912 void wlt_display_ref(struct wlt_display *disp)
914 if (!disp || !disp->ref)
920 void wlt_display_unref(struct wlt_display *disp)
922 if (!disp || !disp->ref || --disp->ref)
925 unload_cursors(disp);
926 wl_display_remove_global_listener(disp->dp, disp->dp_listener);
927 wl_display_flush(disp->dp);
928 wl_display_disconnect(disp->dp);
929 ev_eloop_rm_timer(disp->repeat_timer);
930 ev_eloop_rm_fd(disp->dp_fd);
931 xkb_context_unref(disp->xkb_ctx);
932 shl_hook_free(disp->listeners);
933 ev_eloop_unref(disp->eloop);
937 int wlt_display_register_cb(struct wlt_display *disp,
938 wlt_display_cb cb, void *data)
943 return shl_hook_add_cast(disp->listeners, cb, data);
946 void wlt_display_unregister_cb(struct wlt_display *disp,
947 wlt_display_cb cb, void *data)
952 shl_hook_rm_cast(disp->listeners, cb, data);
955 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
958 wl_shell_surface_pong(s, serial);
961 static void wlt_window_do_redraw(struct wlt_window *wnd,
965 struct shl_dlist *iter;
966 struct wlt_widget *widget;
967 unsigned int x, y, flags;
968 struct wlt_rect alloc;
972 flags |= WLT_WINDOW_MAXIMIZED;
974 flags |= WLT_WINDOW_FULLSCREEN;
978 alloc.width = wnd->buffer.width;
979 alloc.height = wnd->buffer.height;
980 shl_dlist_for_each(iter, &wnd->widget_list) {
981 widget = shl_dlist_entry(iter, struct wlt_widget, list);
982 if (widget->resize_cb)
983 widget->resize_cb(widget, flags, &alloc, widget->data);
986 memset(wnd->buffer.data, 0,
987 wnd->buffer.stride * wnd->buffer.height);
989 wnd->skip_damage = true;
990 shl_dlist_for_each(iter, &wnd->widget_list) {
991 widget = shl_dlist_entry(iter, struct wlt_widget, list);
992 if (widget->redraw_cb)
993 widget->redraw_cb(widget, flags, widget->data);
995 wnd->skip_damage = false;
997 if (!wnd->buffer_attached) {
998 wnd->buffer_attached = true;
999 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
1000 x = (int)oldw - wnd->buffer.width;
1003 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
1004 y = (int)oldh - wnd->buffer.height;
1007 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1008 wnd->resize_edges = 0;
1011 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1012 wnd->buffer.height);
1015 static int resize_window(struct wlt_window *wnd, unsigned int width,
1016 unsigned int height, bool force_redraw)
1018 struct shl_dlist *iter;
1019 struct wlt_widget *widget;
1020 struct wl_buffer *old_buffer = NULL;
1021 struct wlt_pool *old_pool = NULL;
1024 unsigned int oldw, oldh, neww, newh, minw, minh, flags;
1026 if (!wnd || !width || !height)
1031 flags |= WLT_WINDOW_MAXIMIZED;
1032 if (wnd->fullscreen)
1033 flags |= WLT_WINDOW_FULLSCREEN;
1039 shl_dlist_for_each(iter, &wnd->widget_list) {
1040 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1041 if (widget->prepare_resize_cb)
1042 widget->prepare_resize_cb(widget,
1055 if (width == wnd->buffer.width &&
1056 height == wnd->buffer.height) {
1058 wlt_window_do_redraw(wnd, width, height);
1062 oldw = wnd->buffer.width;
1063 oldh = wnd->buffer.height;
1065 nsize = width * height * 4;
1066 if (wnd->pool && wnd->pool->size >= nsize) {
1067 old_buffer = wnd->w_buffer;
1068 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1071 WL_SHM_FORMAT_ARGB8888);
1072 if (!wnd->w_buffer) {
1073 log_error("cannot create wayland shm buffer");
1074 wnd->w_buffer = old_buffer;
1078 old_pool = wnd->pool;
1079 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1081 log_error("cannot create memory pool");
1082 wnd->pool = old_pool;
1086 old_buffer = wnd->w_buffer;
1087 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1090 WL_SHM_FORMAT_ARGB8888);
1091 if (!wnd->w_buffer) {
1092 log_error("cannot create wayland shm buffer");
1093 wnd->w_buffer = old_buffer;
1094 wlt_pool_free(wnd->pool);
1095 wnd->pool = old_pool;
1100 wnd->buffer.data = wnd->pool->data;
1101 wnd->buffer.width = width;
1102 wnd->buffer.height = height;
1103 wnd->buffer.stride = width * 4;
1104 wnd->buffer_attached = false;
1106 wlt_window_do_redraw(wnd, oldw, oldh);
1109 wl_buffer_destroy(old_buffer);
1111 wlt_pool_free(old_pool);
1116 static void frame_callback(void *data, struct wl_callback *w_callback,
1118 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1120 static const struct wl_callback_listener frame_callback_listener = {
1121 .done = frame_callback,
1124 static void do_frame(struct wlt_window *wnd)
1128 wnd->idle_pending = false;
1129 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1131 if (wnd->need_resize) {
1132 force = wnd->need_redraw;
1133 wnd->need_frame = true;
1134 wnd->need_resize = false;
1135 wnd->need_redraw = false;
1136 resize_window(wnd, wnd->new_width, wnd->new_height, force);
1139 if (wnd->need_redraw) {
1140 wnd->need_frame = true;
1141 wnd->need_redraw = false;
1142 wlt_window_do_redraw(wnd, wnd->buffer.width,
1143 wnd->buffer.height);
1146 if (wnd->need_frame) {
1147 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1148 wl_callback_add_listener(wnd->w_frame,
1149 &frame_callback_listener, wnd);
1153 static void frame_callback(void *data, struct wl_callback *w_callback,
1156 struct wlt_window *wnd = data;
1158 wl_callback_destroy(w_callback);
1159 wnd->w_frame = NULL;
1160 wnd->need_frame = false;
1165 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1167 struct wlt_window *wnd = data;
1173 * Buffer Handling and Frame Scheduling
1174 * We use wl_shm for buffer allocation. This means, we have a single buffer
1175 * client side and the server loads it into its backbuffer for rendering. If the
1176 * server does not do this, we are screwed anyway, but that's on behalf of the
1177 * server, so we don't care.
1179 * However, this means, when we create a buffer, we need to notify the
1180 * compositor and then wait until the compositor has created its back-buffer,
1181 * before we continue using this buffer. We can use the "frame" callback to get
1182 * notified about this.
1184 * The logic we have is:
1185 * You set the boolean flags what action is needed in wlt_window and then call
1186 * "schedule_frame()". If we didn't already do any buffer operations in this
1187 * frame, then this function schedules an idle-callback which then performs
1188 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1189 * that this frame was already used and schedules a frame-callback.
1190 * If during this time another call to schedule_frame() is made, we do nothing
1191 * but wait for the frame-callback. It will then directly perform all the
1192 * requested functions and reschedule a frame-callback.
1193 * If nothing was schedule for one frame, we have no more interest in
1194 * frame-callbacks and thus we set "need_frame" to false again and don't
1195 * schedule any more frame-callbacks.
1197 static void schedule_frame(struct wlt_window *wnd)
1201 if (!wnd || wnd->w_frame)
1204 if (wnd->need_frame || wnd->idle_pending)
1207 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1209 log_error("cannot schedule idle callback: %d", ret);
1211 wnd->idle_pending = true;
1214 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1215 uint32_t edges, int32_t width,
1218 struct wlt_window *wnd = data;
1225 wnd->resize_edges = edges;
1226 wlt_window_set_size(wnd, width, height);
1229 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1233 static const struct wl_shell_surface_listener shell_surface_listener = {
1234 .ping = shell_surface_ping,
1235 .configure = shell_surface_configure,
1236 .popup_done = shell_surface_popup_done,
1239 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1241 struct wlt_window *wnd = data;
1243 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1244 wnd->close_pending = false;
1247 wnd->close_cb(wnd, wnd->data);
1250 int wlt_display_create_window(struct wlt_display *disp,
1251 struct wlt_window **out,
1253 unsigned int height,
1256 struct wlt_window *wnd;
1259 if (!disp || !out || !width || !height)
1262 if (disp->state != STATE_RUNNING) {
1263 log_error("cannot create window, display is not running but in state %u",
1268 wnd = malloc(sizeof(*wnd));
1271 memset(wnd, 0, sizeof(*wnd));
1275 shl_dlist_init(&wnd->widget_list);
1277 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1278 if (!wnd->w_surface) {
1279 log_error("cannot create wayland surface");
1284 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1286 if (!wnd->w_shell_surface) {
1287 log_error("cannot retrieve shell-surface for wayland surface");
1292 wl_shell_surface_add_listener(wnd->w_shell_surface,
1293 &shell_surface_listener, wnd);
1294 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1296 ret = resize_window(wnd, width, height, true);
1298 goto err_shell_surface;
1300 wlt_display_ref(disp);
1301 shl_dlist_link(&disp->window_list, &wnd->list);
1306 wl_shell_surface_destroy(wnd->w_shell_surface);
1308 wl_surface_destroy(wnd->w_surface);
1314 void wlt_window_ref(struct wlt_window *wnd)
1316 if (!wnd || !wnd->ref)
1322 void wlt_window_unref(struct wlt_window *wnd)
1324 struct wlt_widget *widget;
1326 if (!wnd || !wnd->ref || --wnd->ref)
1329 while (!shl_dlist_empty(&wnd->widget_list)) {
1330 widget = shl_dlist_entry(wnd->widget_list.next,
1331 struct wlt_widget, list);
1332 wlt_widget_destroy(widget);
1335 if (wnd->close_pending)
1336 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1338 if (wnd->idle_pending)
1339 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1340 shl_dlist_unlink(&wnd->list);
1342 wl_callback_destroy(wnd->w_frame);
1344 wl_buffer_destroy(wnd->w_buffer);
1346 wlt_pool_free(wnd->pool);
1347 wl_shell_surface_destroy(wnd->w_shell_surface);
1348 wl_surface_destroy(wnd->w_surface);
1349 wlt_display_unref(wnd->disp);
1353 int wlt_window_create_widget(struct wlt_window *wnd,
1354 struct wlt_widget **out,
1357 struct wlt_widget *widget;
1362 widget = malloc(sizeof(*widget));
1365 memset(widget, 0, sizeof(*widget));
1367 widget->data = data;
1369 wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
1370 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1375 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1380 wnd->need_redraw = true;
1381 schedule_frame(wnd);
1384 void wlt_window_damage(struct wlt_window *wnd,
1385 struct wlt_rect *damage)
1387 if (!wnd || !damage || wnd->skip_damage)
1390 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1391 damage->width, damage->height);
1394 void wlt_window_get_buffer(struct wlt_window *wnd,
1395 const struct wlt_rect *alloc,
1396 struct wlt_shm_buffer *buf)
1398 struct wlt_shm_buffer *rbuf;
1403 rbuf = &wnd->buffer;
1406 memcpy(buf, rbuf, sizeof(*buf));
1410 if (alloc->x >= rbuf->width ||
1411 alloc->y >= rbuf->height) {
1412 memset(buf, 0, sizeof(*buf));
1417 if (alloc->x + alloc->width > rbuf->width)
1418 buf->width = rbuf->width - alloc->x;
1420 buf->width = alloc->width;
1423 if (alloc->y + alloc->height > rbuf->height)
1424 buf->height = rbuf->height - alloc->y;
1426 buf->height = alloc->height;
1429 buf->stride = rbuf->stride;
1432 buf->data = rbuf->data;
1433 buf->data += alloc->y * rbuf->stride;
1434 buf->data += alloc->x * 4;
1437 void wlt_window_move(struct wlt_window *wnd)
1442 wl_shell_surface_move(wnd->w_shell_surface,
1444 wnd->disp->last_serial);
1447 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1452 wl_shell_surface_resize(wnd->w_shell_surface,
1454 wnd->disp->last_serial,
1458 int wlt_window_set_size(struct wlt_window *wnd,
1459 unsigned int width, unsigned int height)
1461 if (!wnd || !width || !height)
1464 wnd->new_width = width;
1465 wnd->new_height = height;
1466 wnd->need_resize = true;
1467 schedule_frame(wnd);
1472 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1477 set_cursor(wnd->disp, cursor);
1480 void wlt_window_set_close_cb(struct wlt_window *wnd,
1481 wlt_window_close_cb cb)
1489 void wlt_window_close(struct wlt_window *wnd)
1494 wnd->close_pending = true;
1495 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1498 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1503 if (wnd->maximized) {
1504 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1505 wlt_window_set_size(wnd, wnd->saved_width, wnd->saved_height);
1507 if (!wnd->fullscreen) {
1508 wnd->saved_width = wnd->buffer.width;
1509 wnd->saved_height = wnd->buffer.height;
1511 wl_shell_surface_set_maximized(wnd->w_shell_surface, NULL);
1514 wnd->maximized = !wnd->maximized;
1517 void wlt_window_toggle_fullscreen(struct wlt_window *wnd)
1522 if (wnd->fullscreen) {
1523 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1524 wlt_window_set_size(wnd, wnd->saved_width, wnd->saved_height);
1526 if (!wnd->maximized) {
1527 wnd->saved_width = wnd->buffer.width;
1528 wnd->saved_height = wnd->buffer.height;
1530 wl_shell_surface_set_fullscreen(wnd->w_shell_surface,
1531 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1535 wnd->fullscreen = !wnd->fullscreen;
1538 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1543 return wnd->disp->eloop;
1546 void wlt_widget_destroy(struct wlt_widget *widget)
1551 if (widget->destroy_cb)
1552 widget->destroy_cb(widget, widget->data);
1553 shl_dlist_unlink(&widget->list);
1557 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1565 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1566 wlt_widget_redraw_cb cb)
1571 widget->redraw_cb = cb;
1574 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1575 wlt_widget_destroy_cb cb)
1580 widget->destroy_cb = cb;
1583 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1584 wlt_widget_prepare_resize_cb prepare_cb,
1585 wlt_widget_resize_cb cb)
1590 widget->prepare_resize_cb = prepare_cb;
1591 widget->resize_cb = cb;
1594 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1595 wlt_widget_pointer_enter_cb enter_cb,
1596 wlt_widget_pointer_leave_cb leave_cb,
1597 wlt_widget_pointer_motion_cb motion_cb,
1598 wlt_widget_pointer_button_cb button_cb)
1603 widget->pointer_enter_cb = enter_cb;
1604 widget->pointer_leave_cb = leave_cb;
1605 widget->pointer_motion_cb = motion_cb;
1606 widget->pointer_button_cb = button_cb;
1609 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1610 wlt_widget_keyboard_cb cb)
1615 widget->keyboard_cb = cb;