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;
638 disp->last_serial = serial;
639 if (!disp->xkb_state)
646 mask = shl_get_xkb_mods(disp->xkb_state);
647 num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
648 sym = XKB_KEY_NoSymbol;
653 shl_dlist_for_each(iter, &wnd->widget_list) {
654 widget = shl_dlist_entry(iter, struct wlt_widget, list);
655 if (widget->keyboard_cb) {
656 if (widget->keyboard_cb(widget, mask, sym, state,
657 handled, widget->data))
662 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
663 sym == disp->repeat_sym) {
664 ev_timer_update(disp->repeat_timer, NULL);
665 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
666 disp->repeat_sym = sym;
667 spec.it_interval.tv_sec = 0;
668 spec.it_interval.tv_nsec = 25 * 1000000;
669 spec.it_value.tv_sec = 0;
670 spec.it_value.tv_nsec = 250 * 1000000;
671 ev_timer_update(disp->repeat_timer, &spec);
675 static void repeat_event(struct ev_timer *timer, uint64_t num, void *data)
677 struct wlt_display *disp = data;
678 struct wlt_window *wnd = disp->keyboard_focus;
679 struct wlt_widget *widget;
680 struct shl_dlist *iter;
687 mask = shl_get_xkb_mods(disp->xkb_state);
689 shl_dlist_for_each(iter, &wnd->widget_list) {
690 widget = shl_dlist_entry(iter, struct wlt_widget, list);
691 if (widget->keyboard_cb) {
692 if (widget->keyboard_cb(widget, mask, disp->repeat_sym,
693 WL_KEYBOARD_KEY_STATE_PRESSED,
694 handled, widget->data))
700 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
701 uint32_t serial, uint32_t mods_depressed,
702 uint32_t mods_latched, uint32_t mods_locked,
705 struct wlt_display *disp = data;
707 disp->last_serial = serial;
708 if (!disp->xkb_state)
711 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
712 mods_locked, 0, 0, group);
715 static const struct wl_keyboard_listener keyboard_listener = {
716 .keymap = keyboard_keymap,
717 .enter = keyboard_enter,
718 .leave = keyboard_leave,
720 .modifiers = keyboard_modifiers,
723 static void check_ready(struct wlt_display *disp)
725 if (disp->state > STATE_INIT)
734 log_debug("wayland display initialized");
737 disp->state = STATE_RUNNING;
738 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
742 static void seat_capabilities(void *data, struct wl_seat *seat,
743 enum wl_seat_capability caps)
745 struct wlt_display *disp = data;
747 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
748 disp->w_pointer = wl_seat_get_pointer(seat);
749 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
753 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
754 disp->w_keyboard = wl_seat_get_keyboard(seat);
755 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
762 static const struct wl_seat_listener seat_listener = {
763 .capabilities = seat_capabilities,
766 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
767 uint32_t version, void *data)
769 struct wlt_display *disp = data;
771 if (!strcmp(iface, "wl_display")) {
772 log_debug("new wl_display global");
774 } else if (!strcmp(iface, "wl_compositor")) {
776 log_error("global wl_compositor advertised twice");
779 disp->w_comp = wl_display_bind(disp->dp,
781 &wl_compositor_interface);
783 log_error("cannot bind wl_compositor object");
786 } else if (!strcmp(iface, "wl_seat")) {
788 log_error("global wl_seat advertised twice");
791 disp->w_seat = wl_display_bind(disp->dp,
795 log_error("cannot bind wl_seat object");
799 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
800 } else if (!strcmp(iface, "wl_shell")) {
802 log_error("global wl_shell advertised twice");
805 disp->w_shell = wl_display_bind(disp->dp,
807 &wl_shell_interface);
808 if (!disp->w_shell) {
809 log_error("cannot bind wl_shell object");
812 } else if (!strcmp(iface, "wl_shm")) {
814 log_error("global wl_shm advertised twice");
817 disp->w_shm = wl_display_bind(disp->dp,
821 log_error("cannot bind wl_shm object");
825 log_debug("ignoring new unknown global %s", iface);
829 log_debug("new global %s", iface);
834 int wlt_display_new(struct wlt_display **out,
835 struct ev_eloop *eloop)
837 struct wlt_display *disp;
843 log_debug("creating new wlt-display");
845 disp = malloc(sizeof(*disp));
848 memset(disp, 0, sizeof(*disp));
851 shl_dlist_init(&disp->window_list);
853 ret = shl_hook_new(&disp->listeners);
855 log_error("cannot create listeners hook");
859 disp->dp = wl_display_connect(NULL);
861 log_error("cannot connect to wayland socket (%d): %m", errno);
866 ret = ev_eloop_new_fd(eloop,
868 wl_display_get_fd(disp->dp,
875 log_error("cannot create event-fd for wayland display (%d)",
880 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
883 log_error("cannot create repeat-timer");
887 disp->dp_listener = wl_display_add_global_listener(disp->dp,
890 if (!disp->dp_listener) {
891 log_error("cannot add wayland global listener (%d)", errno);
895 disp->xkb_ctx = xkb_context_new(0);
896 if (!disp->xkb_ctx) {
897 log_error("cannot create XKB context");
901 log_debug("wlt-display waiting for globals...");
903 ev_eloop_ref(disp->eloop);
908 ev_eloop_rm_timer(disp->repeat_timer);
910 ev_eloop_rm_fd(disp->dp_fd);
912 wl_display_disconnect(disp->dp);
914 shl_hook_free(disp->listeners);
920 void wlt_display_ref(struct wlt_display *disp)
922 if (!disp || !disp->ref)
928 void wlt_display_unref(struct wlt_display *disp)
930 if (!disp || !disp->ref || --disp->ref)
933 unload_cursors(disp);
934 wl_display_remove_global_listener(disp->dp, disp->dp_listener);
935 wl_display_flush(disp->dp);
936 wl_display_disconnect(disp->dp);
937 ev_eloop_rm_timer(disp->repeat_timer);
938 ev_eloop_rm_fd(disp->dp_fd);
939 xkb_context_unref(disp->xkb_ctx);
940 shl_hook_free(disp->listeners);
941 ev_eloop_unref(disp->eloop);
945 int wlt_display_register_cb(struct wlt_display *disp,
946 wlt_display_cb cb, void *data)
951 return shl_hook_add_cast(disp->listeners, cb, data);
954 void wlt_display_unregister_cb(struct wlt_display *disp,
955 wlt_display_cb cb, void *data)
960 shl_hook_rm_cast(disp->listeners, cb, data);
963 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
966 wl_shell_surface_pong(s, serial);
969 static void wlt_window_do_redraw(struct wlt_window *wnd,
973 struct shl_dlist *iter;
974 struct wlt_widget *widget;
975 unsigned int x, y, flags;
976 struct wlt_rect alloc;
980 flags |= WLT_WINDOW_MAXIMIZED;
982 flags |= WLT_WINDOW_FULLSCREEN;
986 alloc.width = wnd->buffer.width;
987 alloc.height = wnd->buffer.height;
988 shl_dlist_for_each(iter, &wnd->widget_list) {
989 widget = shl_dlist_entry(iter, struct wlt_widget, list);
990 if (widget->resize_cb)
991 widget->resize_cb(widget, flags, &alloc, widget->data);
994 memset(wnd->buffer.data, 0,
995 wnd->buffer.stride * wnd->buffer.height);
997 wnd->skip_damage = true;
998 shl_dlist_for_each(iter, &wnd->widget_list) {
999 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1000 if (widget->redraw_cb)
1001 widget->redraw_cb(widget, flags, widget->data);
1003 wnd->skip_damage = false;
1005 if (!wnd->buffer_attached) {
1006 wnd->buffer_attached = true;
1007 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
1008 x = (int)oldw - wnd->buffer.width;
1011 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
1012 y = (int)oldh - wnd->buffer.height;
1015 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1016 wnd->resize_edges = 0;
1019 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1020 wnd->buffer.height);
1023 static int resize_window(struct wlt_window *wnd, unsigned int width,
1024 unsigned int height, bool force_redraw)
1026 struct shl_dlist *iter;
1027 struct wlt_widget *widget;
1028 struct wl_buffer *old_buffer = NULL;
1029 struct wlt_pool *old_pool = NULL;
1032 unsigned int oldw, oldh, neww, newh, minw, minh, flags;
1035 width = wnd->buffer.width;
1037 height = wnd->buffer.height;
1039 if (!width || !height)
1044 flags |= WLT_WINDOW_MAXIMIZED;
1045 if (wnd->fullscreen)
1046 flags |= WLT_WINDOW_FULLSCREEN;
1052 shl_dlist_for_each(iter, &wnd->widget_list) {
1053 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1054 if (widget->prepare_resize_cb)
1055 widget->prepare_resize_cb(widget,
1068 if (width == wnd->buffer.width &&
1069 height == wnd->buffer.height) {
1071 wlt_window_do_redraw(wnd, width, height);
1075 oldw = wnd->buffer.width;
1076 oldh = wnd->buffer.height;
1078 nsize = width * height * 4;
1079 if (wnd->pool && wnd->pool->size >= nsize) {
1080 old_buffer = wnd->w_buffer;
1081 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1084 WL_SHM_FORMAT_ARGB8888);
1085 if (!wnd->w_buffer) {
1086 log_error("cannot create wayland shm buffer");
1087 wnd->w_buffer = old_buffer;
1091 old_pool = wnd->pool;
1092 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1094 log_error("cannot create memory pool");
1095 wnd->pool = old_pool;
1099 old_buffer = wnd->w_buffer;
1100 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1103 WL_SHM_FORMAT_ARGB8888);
1104 if (!wnd->w_buffer) {
1105 log_error("cannot create wayland shm buffer");
1106 wnd->w_buffer = old_buffer;
1107 wlt_pool_free(wnd->pool);
1108 wnd->pool = old_pool;
1113 wnd->buffer.data = wnd->pool->data;
1114 wnd->buffer.width = width;
1115 wnd->buffer.height = height;
1116 wnd->buffer.stride = width * 4;
1117 wnd->buffer_attached = false;
1119 wlt_window_do_redraw(wnd, oldw, oldh);
1122 wl_buffer_destroy(old_buffer);
1124 wlt_pool_free(old_pool);
1129 static void frame_callback(void *data, struct wl_callback *w_callback,
1131 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1133 static const struct wl_callback_listener frame_callback_listener = {
1134 .done = frame_callback,
1137 static void do_frame(struct wlt_window *wnd)
1141 wnd->idle_pending = false;
1142 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1144 if (wnd->need_resize) {
1145 force = wnd->need_redraw;
1146 wnd->need_frame = true;
1147 wnd->need_resize = false;
1148 wnd->need_redraw = false;
1149 resize_window(wnd, wnd->new_width, wnd->new_height, force);
1152 if (wnd->need_redraw) {
1153 wnd->need_frame = true;
1154 wnd->need_redraw = false;
1155 wlt_window_do_redraw(wnd, wnd->buffer.width,
1156 wnd->buffer.height);
1159 if (wnd->need_frame) {
1160 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1161 wl_callback_add_listener(wnd->w_frame,
1162 &frame_callback_listener, wnd);
1166 static void frame_callback(void *data, struct wl_callback *w_callback,
1169 struct wlt_window *wnd = data;
1171 wl_callback_destroy(w_callback);
1172 wnd->w_frame = NULL;
1173 wnd->need_frame = false;
1178 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1180 struct wlt_window *wnd = data;
1186 * Buffer Handling and Frame Scheduling
1187 * We use wl_shm for buffer allocation. This means, we have a single buffer
1188 * client side and the server loads it into its backbuffer for rendering. If the
1189 * server does not do this, we are screwed anyway, but that's on behalf of the
1190 * server, so we don't care.
1192 * However, this means, when we create a buffer, we need to notify the
1193 * compositor and then wait until the compositor has created its back-buffer,
1194 * before we continue using this buffer. We can use the "frame" callback to get
1195 * notified about this.
1197 * The logic we have is:
1198 * You set the boolean flags what action is needed in wlt_window and then call
1199 * "schedule_frame()". If we didn't already do any buffer operations in this
1200 * frame, then this function schedules an idle-callback which then performs
1201 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1202 * that this frame was already used and schedules a frame-callback.
1203 * If during this time another call to schedule_frame() is made, we do nothing
1204 * but wait for the frame-callback. It will then directly perform all the
1205 * requested functions and reschedule a frame-callback.
1206 * If nothing was schedule for one frame, we have no more interest in
1207 * frame-callbacks and thus we set "need_frame" to false again and don't
1208 * schedule any more frame-callbacks.
1210 static void schedule_frame(struct wlt_window *wnd)
1214 if (!wnd || wnd->w_frame)
1217 if (wnd->need_frame || wnd->idle_pending)
1220 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1222 log_error("cannot schedule idle callback: %d", ret);
1224 wnd->idle_pending = true;
1227 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1228 uint32_t edges, int32_t width,
1231 struct wlt_window *wnd = data;
1238 wnd->resize_edges = edges;
1239 wlt_window_set_size(wnd, width, height);
1242 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1246 static const struct wl_shell_surface_listener shell_surface_listener = {
1247 .ping = shell_surface_ping,
1248 .configure = shell_surface_configure,
1249 .popup_done = shell_surface_popup_done,
1252 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1254 struct wlt_window *wnd = data;
1256 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1257 wnd->close_pending = false;
1260 wnd->close_cb(wnd, wnd->data);
1263 int wlt_display_create_window(struct wlt_display *disp,
1264 struct wlt_window **out,
1266 unsigned int height,
1269 struct wlt_window *wnd;
1272 if (!disp || !out || !width || !height)
1275 if (disp->state != STATE_RUNNING) {
1276 log_error("cannot create window, display is not running but in state %u",
1281 wnd = malloc(sizeof(*wnd));
1284 memset(wnd, 0, sizeof(*wnd));
1288 shl_dlist_init(&wnd->widget_list);
1290 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1291 if (!wnd->w_surface) {
1292 log_error("cannot create wayland surface");
1297 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1299 if (!wnd->w_shell_surface) {
1300 log_error("cannot retrieve shell-surface for wayland surface");
1305 wl_shell_surface_add_listener(wnd->w_shell_surface,
1306 &shell_surface_listener, wnd);
1307 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1309 ret = resize_window(wnd, width, height, true);
1311 goto err_shell_surface;
1313 wlt_display_ref(disp);
1314 shl_dlist_link(&disp->window_list, &wnd->list);
1319 wl_shell_surface_destroy(wnd->w_shell_surface);
1321 wl_surface_destroy(wnd->w_surface);
1327 void wlt_window_ref(struct wlt_window *wnd)
1329 if (!wnd || !wnd->ref)
1335 void wlt_window_unref(struct wlt_window *wnd)
1337 struct wlt_widget *widget;
1339 if (!wnd || !wnd->ref || --wnd->ref)
1342 while (!shl_dlist_empty(&wnd->widget_list)) {
1343 widget = shl_dlist_entry(wnd->widget_list.next,
1344 struct wlt_widget, list);
1345 wlt_widget_destroy(widget);
1348 if (wnd->close_pending)
1349 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1351 if (wnd->idle_pending)
1352 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1353 shl_dlist_unlink(&wnd->list);
1355 wl_callback_destroy(wnd->w_frame);
1357 wl_buffer_destroy(wnd->w_buffer);
1359 wlt_pool_free(wnd->pool);
1360 wl_shell_surface_destroy(wnd->w_shell_surface);
1361 wl_surface_destroy(wnd->w_surface);
1362 wlt_display_unref(wnd->disp);
1366 int wlt_window_create_widget(struct wlt_window *wnd,
1367 struct wlt_widget **out,
1370 struct wlt_widget *widget;
1375 widget = malloc(sizeof(*widget));
1378 memset(widget, 0, sizeof(*widget));
1380 widget->data = data;
1382 wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
1383 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1388 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1393 wnd->need_redraw = true;
1394 schedule_frame(wnd);
1397 void wlt_window_damage(struct wlt_window *wnd,
1398 struct wlt_rect *damage)
1400 if (!wnd || !damage || wnd->skip_damage)
1403 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1404 damage->width, damage->height);
1407 void wlt_window_get_buffer(struct wlt_window *wnd,
1408 const struct wlt_rect *alloc,
1409 struct wlt_shm_buffer *buf)
1411 struct wlt_shm_buffer *rbuf;
1416 rbuf = &wnd->buffer;
1419 memcpy(buf, rbuf, sizeof(*buf));
1423 if (alloc->x >= rbuf->width ||
1424 alloc->y >= rbuf->height) {
1425 memset(buf, 0, sizeof(*buf));
1430 if (alloc->x + alloc->width > rbuf->width)
1431 buf->width = rbuf->width - alloc->x;
1433 buf->width = alloc->width;
1436 if (alloc->y + alloc->height > rbuf->height)
1437 buf->height = rbuf->height - alloc->y;
1439 buf->height = alloc->height;
1442 buf->stride = rbuf->stride;
1445 buf->data = rbuf->data;
1446 buf->data += alloc->y * rbuf->stride;
1447 buf->data += alloc->x * 4;
1450 void wlt_window_move(struct wlt_window *wnd)
1455 wl_shell_surface_move(wnd->w_shell_surface,
1457 wnd->disp->last_serial);
1460 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1465 wl_shell_surface_resize(wnd->w_shell_surface,
1467 wnd->disp->last_serial,
1471 int wlt_window_set_size(struct wlt_window *wnd,
1472 unsigned int width, unsigned int height)
1477 wnd->new_width = width;
1478 wnd->new_height = height;
1479 wnd->need_resize = true;
1480 schedule_frame(wnd);
1485 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1490 set_cursor(wnd->disp, cursor);
1493 void wlt_window_set_close_cb(struct wlt_window *wnd,
1494 wlt_window_close_cb cb)
1502 void wlt_window_close(struct wlt_window *wnd)
1507 wnd->close_pending = true;
1508 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1511 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1516 if (wnd->maximized) {
1517 if (!wnd->fullscreen) {
1518 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1519 wlt_window_set_size(wnd, wnd->saved_width,
1523 if (!wnd->fullscreen) {
1524 wnd->saved_width = wnd->buffer.width;
1525 wnd->saved_height = wnd->buffer.height;
1526 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1531 wnd->maximized = !wnd->maximized;
1534 void wlt_window_toggle_fullscreen(struct wlt_window *wnd)
1539 if (wnd->fullscreen) {
1540 if (wnd->maximized) {
1541 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1544 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1545 wlt_window_set_size(wnd, wnd->saved_width,
1549 if (!wnd->maximized) {
1550 wnd->saved_width = wnd->buffer.width;
1551 wnd->saved_height = wnd->buffer.height;
1553 wl_shell_surface_set_fullscreen(wnd->w_shell_surface,
1554 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1558 wnd->fullscreen = !wnd->fullscreen;
1561 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1566 return wnd->disp->eloop;
1569 struct wlt_display *wlt_window_get_display(struct wlt_window *wnd)
1577 void wlt_widget_destroy(struct wlt_widget *widget)
1582 if (widget->destroy_cb)
1583 widget->destroy_cb(widget, widget->data);
1584 shl_dlist_unlink(&widget->list);
1588 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1596 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1597 wlt_widget_redraw_cb cb)
1602 widget->redraw_cb = cb;
1605 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1606 wlt_widget_destroy_cb cb)
1611 widget->destroy_cb = cb;
1614 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1615 wlt_widget_prepare_resize_cb prepare_cb,
1616 wlt_widget_resize_cb cb)
1621 widget->prepare_resize_cb = prepare_cb;
1622 widget->resize_cb = cb;
1625 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1626 wlt_widget_pointer_enter_cb enter_cb,
1627 wlt_widget_pointer_leave_cb leave_cb,
1628 wlt_widget_pointer_motion_cb motion_cb,
1629 wlt_widget_pointer_button_cb button_cb)
1634 widget->pointer_enter_cb = enter_cb;
1635 widget->pointer_leave_cb = leave_cb;
1636 widget->pointer_motion_cb = motion_cb;
1637 widget->pointer_button_cb = button_cb;
1640 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1641 wlt_widget_keyboard_cb cb)
1646 widget->keyboard_cb = cb;