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_array.h"
42 #include "shl_dlist.h"
47 #include "wlt_toolkit.h"
49 #define LOG_SUBSYSTEM "wlt_toolkit"
52 struct wl_shm_pool *w_pool;
65 struct wl_data_offer *w_offer;
66 struct wlt_display *disp;
67 struct shl_array *types;
72 struct ev_eloop *eloop;
73 struct wl_display *dp;
75 struct wl_global_listener *dp_listener;
76 struct shl_hook *listeners;
79 struct shl_dlist window_list;
81 struct wl_compositor *w_comp;
82 struct wl_seat *w_seat;
83 struct wl_shell *w_shell;
87 uint32_t pointer_enter_serial;
88 struct wl_pointer *w_pointer;
89 struct wlt_window *pointer_focus;
91 uint32_t cursor_serial;
92 unsigned int current_cursor;
93 struct wl_surface *w_cursor_surface;
94 struct wl_cursor_theme *cursor_theme;
95 struct wl_cursor *cursors[WLT_CURSOR_NUM];
97 struct wl_keyboard *w_keyboard;
98 struct xkb_context *xkb_ctx;
99 struct xkb_keymap *xkb_keymap;
100 struct xkb_state *xkb_state;
101 struct wlt_window *keyboard_focus;
102 struct ev_timer *repeat_timer;
104 uint32_t repeat_ascii;
106 struct wl_data_device_manager *w_manager;
107 struct wl_data_device *w_data_dev;
108 struct data_offer *drag_offer;
109 struct data_offer *selection_offer;
114 struct shl_dlist list;
116 wlt_window_close_cb close_cb;
118 struct wlt_display *disp;
119 struct wlt_pool *pool;
121 struct wl_surface *w_surface;
122 struct wl_shell_surface *w_shell_surface;
123 struct wl_buffer *w_buffer;
125 bool buffer_attached;
131 unsigned int new_width;
132 unsigned int new_height;
133 unsigned int saved_width;
134 unsigned int saved_height;
135 unsigned int resize_edges;
138 struct wlt_shm_buffer buffer;
139 struct wl_callback *w_frame;
141 struct shl_dlist widget_list;
145 struct shl_dlist list;
146 struct wlt_window *wnd;
148 wlt_widget_redraw_cb redraw_cb;
149 wlt_widget_destroy_cb destroy_cb;
150 wlt_widget_prepare_resize_cb prepare_resize_cb;
151 wlt_widget_resize_cb resize_cb;
152 wlt_widget_pointer_enter_cb pointer_enter_cb;
153 wlt_widget_pointer_leave_cb pointer_leave_cb;
154 wlt_widget_pointer_motion_cb pointer_motion_cb;
155 wlt_widget_pointer_button_cb pointer_button_cb;
156 wlt_widget_keyboard_cb keyboard_cb;
159 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
162 static const char template[] = "/wlterm-shared-XXXXXX";
163 struct wlt_pool *pool;
168 path = getenv("XDG_RUNTIME_DIR");
170 log_error("XDG_RUNTIME_DIR not set");
174 pool = malloc(sizeof(*pool));
177 memset(pool, 0, sizeof(*pool));
180 name = malloc(strlen(path) + sizeof(template));
186 strcat(name, template);
188 fd = mkostemp(name, O_CLOEXEC);
190 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
199 log_warning("cannot unlink temporary file %s (%d): %m",
203 ret = ftruncate(fd, size);
205 log_error("cannot truncate temporary file to length %lu (%d): %m",
211 pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
213 if (pool->data == MAP_FAILED) {
214 log_error("cannot mmap temporary file (%d): %m", errno);
219 pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
221 log_error("cannot create wayland shm pool object");
231 munmap(pool->data, size);
239 static void wlt_pool_free(struct wlt_pool *pool)
241 munmap(pool->data, pool->size);
242 wl_shm_pool_destroy(pool->w_pool);
247 * These cursor-names are based on:
248 * https://bugs.kde.org/attachment.cgi?id=67313
251 static char *(*cursors[])[] = {
252 [WLT_CURSOR_NONE] = &(char*[]) {
255 [WLT_CURSOR_TOP] = &(char*[]) {
260 [WLT_CURSOR_BOTTOM] = &(char*[]) {
265 [WLT_CURSOR_LEFT] = &(char*[]) {
270 [WLT_CURSOR_RIGHT] = &(char*[]) {
275 [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
280 [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
285 [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
286 "bottom_left_corner",
290 [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
291 "bottom_right_corner",
295 [WLT_CURSOR_DRAGGING] = &(char*[]) {
298 "208530c400c041818281048008011002",
301 [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
308 [WLT_CURSOR_IBEAM] = &(char*[]) {
316 static int load_cursors(struct wlt_display *disp)
319 size_t size = sizeof(cursors) / sizeof(*cursors);
320 struct wl_cursor *cursor;
322 disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
324 disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
325 if (!disp->cursor_theme) {
326 log_warning("cannot load curdor theme");
330 for (i = 0; i < WLT_CURSOR_NUM; ++i) {
332 if (i < size && cursors[i]) {
333 for (j = 0; (*cursors[i])[j]; ++j) {
334 cursor = wl_cursor_theme_get_cursor(
342 if (!cursor && i != WLT_CURSOR_NONE)
343 log_warning("cannot load cursor for ID %d", i);
345 disp->cursors[i] = cursor;
351 static void unload_cursors(struct wlt_display *disp)
353 if (disp->cursor_theme)
354 wl_cursor_theme_destroy(disp->cursor_theme);
355 if (disp->w_cursor_surface)
356 wl_surface_destroy(disp->w_cursor_surface);
359 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
362 struct wl_cursor *cur;
363 struct wl_cursor_image *image;
364 struct wl_buffer *buffer;
366 if (cursor >= WLT_CURSOR_NUM)
367 cursor = WLT_CURSOR_LEFT_PTR;
369 if (disp->pointer_enter_serial > disp->cursor_serial)
372 if (!force && cursor == disp->current_cursor)
375 disp->current_cursor = cursor;
376 disp->cursor_serial = disp->pointer_enter_serial;
378 cur = disp->cursors[cursor];
380 wl_pointer_set_cursor(disp->w_pointer,
381 disp->pointer_enter_serial,
386 image = cur->images[0];
387 buffer = wl_cursor_image_get_buffer(image);
389 log_error("cannot load buffer for cursor image");
393 wl_pointer_set_cursor(disp->w_pointer,
394 disp->pointer_enter_serial,
395 disp->w_cursor_surface,
396 image->hotspot_x, image->hotspot_y);
397 wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
398 wl_surface_damage(disp->w_cursor_surface, 0, 0,
399 image->width, image->height);
402 static int dp_mask_update(uint32_t mask, void *data)
404 struct wlt_display *disp = data;
412 if (mask & WL_DISPLAY_READABLE)
414 if (mask & WL_DISPLAY_WRITABLE)
415 mode |= EV_WRITEABLE;
417 ret = ev_fd_update(disp->dp_fd, mode);
419 log_warning("cannot update wayland-fd event-polling modes (%d)",
425 static void dp_event(struct ev_fd *fd, int mask, void *data)
427 struct wlt_display *disp = data;
430 if (mask & (EV_HUP | EV_ERR)) {
431 log_warning("HUP/ERR on wayland socket");
432 disp->state = STATE_HUP;
433 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
434 ev_eloop_rm_fd(disp->dp_fd);
440 if (mask & EV_READABLE)
441 mode |= WL_DISPLAY_READABLE;
442 if (mask & EV_WRITEABLE)
443 mode |= WL_DISPLAY_WRITABLE;
446 wl_display_iterate(disp->dp, mode);
449 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
450 uint32_t serial, struct wl_surface *w_surface,
451 wl_fixed_t x, wl_fixed_t y)
453 struct wlt_display *disp = data;
454 struct shl_dlist *iter;
455 struct wlt_window *wnd;
456 struct wlt_widget *widget;
461 shl_dlist_for_each(iter, &disp->window_list) {
462 wnd = shl_dlist_entry(iter, struct wlt_window, list);
463 if (wnd->w_surface == w_surface)
467 if (iter == &disp->window_list) {
468 log_debug("unknown surface");
472 disp->pointer_enter_serial = serial;
473 disp->last_serial = serial;
474 disp->pointer_focus = wnd;
475 shl_dlist_for_each(iter, &wnd->widget_list) {
476 widget = shl_dlist_entry(iter, struct wlt_widget, list);
477 if (widget->pointer_enter_cb)
478 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
483 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
484 uint32_t serial, struct wl_surface *w_surface)
486 struct wlt_display *disp = data;
487 struct shl_dlist *iter;
488 struct wlt_window *wnd;
489 struct wlt_widget *widget;
491 wnd = disp->pointer_focus;
492 disp->pointer_focus = NULL;
493 disp->last_serial = serial;
498 shl_dlist_for_each(iter, &wnd->widget_list) {
499 widget = shl_dlist_entry(iter, struct wlt_widget, list);
500 if (widget->pointer_leave_cb)
501 widget->pointer_leave_cb(widget, widget->data);
505 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
506 uint32_t time, wl_fixed_t x, wl_fixed_t y)
508 struct wlt_display *disp = data;
509 struct shl_dlist *iter;
510 struct wlt_window *wnd;
511 struct wlt_widget *widget;
513 wnd = disp->pointer_focus;
517 shl_dlist_for_each(iter, &wnd->widget_list) {
518 widget = shl_dlist_entry(iter, struct wlt_widget, list);
519 if (widget->pointer_motion_cb)
520 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
525 static void pointer_button(void *data, struct wl_pointer *w_pointer,
526 uint32_t serial, uint32_t time, uint32_t button,
529 struct wlt_display *disp = data;
530 struct shl_dlist *iter;
531 struct wlt_window *wnd;
532 struct wlt_widget *widget;
534 wnd = disp->pointer_focus;
535 disp->last_serial = serial;
540 shl_dlist_for_each(iter, &wnd->widget_list) {
541 widget = shl_dlist_entry(iter, struct wlt_widget, list);
542 if (widget->pointer_button_cb)
543 widget->pointer_button_cb(widget, button, state,
548 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
549 uint32_t time, uint32_t axis, wl_fixed_t value)
553 static const struct wl_pointer_listener pointer_listener = {
554 .enter = pointer_enter,
555 .leave = pointer_leave,
556 .motion = pointer_motion,
557 .button = pointer_button,
558 .axis = pointer_axis,
561 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
562 uint32_t format, int fd, uint32_t size)
564 struct wlt_display *disp = data;
567 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
568 log_error("invalid keyboard format");
573 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
574 if (map == MAP_FAILED) {
575 log_error("cannot mmap keyboard keymap");
580 disp->xkb_keymap = xkb_map_new_from_string(disp->xkb_ctx, map,
581 XKB_KEYMAP_FORMAT_TEXT_V1,
586 if (!disp->xkb_keymap) {
587 log_error("cannot create xkb keymap");
591 disp->xkb_state = xkb_state_new(disp->xkb_keymap);
592 if (!disp->xkb_state) {
593 log_error("cannot create XKB state object");
594 xkb_map_unref(disp->xkb_keymap);
595 disp->xkb_keymap = NULL;
600 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
601 uint32_t serial, struct wl_surface *w_surface,
602 struct wl_array *keys)
604 struct wlt_display *disp = data;
605 struct shl_dlist *iter;
606 struct wlt_window *wnd;
608 disp->last_serial = serial;
609 if (!disp->xkb_state)
615 shl_dlist_for_each(iter, &disp->window_list) {
616 wnd = shl_dlist_entry(iter, struct wlt_window, list);
617 if (wnd->w_surface == w_surface)
621 if (iter == &disp->window_list)
624 disp->keyboard_focus = wnd;
627 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
628 uint32_t serial, struct wl_surface *surface)
630 struct wlt_display *disp = data;
632 disp->last_serial = serial;
633 disp->keyboard_focus = NULL;
634 ev_timer_update(disp->repeat_timer, NULL);
637 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
638 uint32_t serial, uint32_t time, uint32_t key,
641 struct wlt_display *disp = data;
642 struct wlt_window *wnd = disp->keyboard_focus;
643 uint32_t code, num_syms, ascii;
645 enum wl_keyboard_key_state state = state_w;
646 const xkb_keysym_t *syms;
648 struct shl_dlist *iter;
649 struct wlt_widget *widget;
650 struct itimerspec spec;
653 disp->last_serial = serial;
654 if (!disp->xkb_state)
661 mask = shl_get_xkb_mods(disp->xkb_state);
662 num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
663 ascii = shl_get_ascii(disp->xkb_state, code, syms, num_syms);
664 sym = XKB_KEY_NoSymbol;
669 shl_dlist_for_each(iter, &wnd->widget_list) {
670 widget = shl_dlist_entry(iter, struct wlt_widget, list);
671 if (widget->keyboard_cb) {
672 if (widget->keyboard_cb(widget, mask, sym, ascii, state,
673 handled, widget->data))
678 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
679 sym == disp->repeat_sym) {
680 ev_timer_update(disp->repeat_timer, NULL);
681 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
682 disp->repeat_sym = sym;
683 disp->repeat_ascii = ascii;
684 spec.it_interval.tv_sec = 0;
685 spec.it_interval.tv_nsec = wlt_conf.xkb_repeat_rate * 1000000;
686 spec.it_value.tv_sec = 0;
687 spec.it_value.tv_nsec = wlt_conf.xkb_repeat_delay * 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;
704 mask = shl_get_xkb_mods(disp->xkb_state);
706 shl_dlist_for_each(iter, &wnd->widget_list) {
707 widget = shl_dlist_entry(iter, struct wlt_widget, list);
708 if (widget->keyboard_cb) {
709 if (widget->keyboard_cb(widget, mask, disp->repeat_sym,
711 WL_KEYBOARD_KEY_STATE_PRESSED,
712 handled, widget->data))
718 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
719 uint32_t serial, uint32_t mods_depressed,
720 uint32_t mods_latched, uint32_t mods_locked,
723 struct wlt_display *disp = data;
725 disp->last_serial = serial;
726 if (!disp->xkb_state)
729 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
730 mods_locked, 0, 0, group);
733 static const struct wl_keyboard_listener keyboard_listener = {
734 .keymap = keyboard_keymap,
735 .enter = keyboard_enter,
736 .leave = keyboard_leave,
738 .modifiers = keyboard_modifiers,
741 static void data_offer_unref(struct data_offer *offer)
745 if (!offer || !offer->ref || --offer->ref)
748 len = shl_array_get_length(offer->types);
749 for (i = 0; i < len; ++i)
750 free(*SHL_ARRAY_AT(offer->types, char*, i));
751 shl_array_free(offer->types);
752 wl_data_offer_destroy(offer->w_offer);
756 static void data_offer_offer(void *data, struct wl_data_offer *w_offer,
761 struct data_offer *offer = wl_data_offer_get_user_data(w_offer);
767 ret = shl_array_push(offer->types, &tmp);
774 static const struct wl_data_offer_listener data_offer_listener = {
775 .offer = data_offer_offer,
778 static void data_dev_data_offer(void *data, struct wl_data_device *w_dev,
779 struct wl_data_offer *w_offer)
781 struct data_offer *offer;
782 struct wlt_display *disp = data;
785 offer = malloc(sizeof(*offer));
787 wl_data_offer_destroy(w_offer);
790 memset(offer, 0, sizeof(*offer));
792 offer->w_offer = w_offer;
795 ret = shl_array_new(&offer->types, sizeof(char*), 4);
797 wl_data_offer_destroy(w_offer);
802 wl_data_offer_add_listener(w_offer, &data_offer_listener, offer);
803 wl_data_offer_set_user_data(w_offer, offer);
806 static void data_dev_enter(void *data, struct wl_data_device *w_dev,
807 uint32_t serial, struct wl_surface *w_surface,
808 wl_fixed_t x, wl_fixed_t y,
809 struct wl_data_offer *w_offer)
811 struct wlt_display *disp = data;
813 if (disp->drag_offer) {
814 data_offer_unref(disp->drag_offer);
815 disp->drag_offer = NULL;
821 disp->drag_offer = wl_data_offer_get_user_data(w_offer);
824 static void data_dev_leave(void *data, struct wl_data_device *w_dev)
826 struct wlt_display *disp = data;
828 if (disp->drag_offer) {
829 data_offer_unref(disp->drag_offer);
830 disp->drag_offer = NULL;
834 static void data_dev_motion(void *data, struct wl_data_device *w_dev,
835 uint32_t time, wl_fixed_t x, wl_fixed_t y)
839 static void data_dev_drop(void *data, struct wl_data_device *w_dev)
843 static void data_dev_selection(void *data, struct wl_data_device *w_dev,
844 struct wl_data_offer *w_offer)
846 struct wlt_display *disp = data;
848 if (disp->selection_offer) {
849 data_offer_unref(disp->selection_offer);
850 disp->selection_offer = NULL;
856 disp->selection_offer = wl_data_offer_get_user_data(w_offer);
859 int wlt_display_get_selection_to_fd(struct wlt_display *disp, const char *mime,
863 struct data_offer *offer;
867 if (!disp->selection_offer)
870 offer = disp->selection_offer;
871 num = shl_array_get_length(offer->types);
872 for (i = 0; i < num; ++i) {
873 if (!strcmp(mime, *SHL_ARRAY_AT(offer->types, char*, i)))
880 wl_data_offer_receive(offer->w_offer, mime, output_fd);
884 int wlt_display_get_selection_fd(struct wlt_display *disp, const char *mime)
888 if (pipe2(p, O_CLOEXEC | O_NONBLOCK))
891 ret = wlt_display_get_selection_to_fd(disp, mime, p[1]);
902 int wlt_display_new_data_source(struct wlt_display *disp,
903 struct wl_data_source **out)
905 struct wl_data_source *src;
910 src = wl_data_device_manager_create_data_source(disp->w_manager);
918 void wlt_display_set_selection(struct wlt_display *disp,
919 struct wl_data_source *selection)
924 wl_data_device_set_selection(disp->w_data_dev, selection,
928 static const struct wl_data_device_listener data_dev_listener = {
929 .data_offer = data_dev_data_offer,
930 .enter = data_dev_enter,
931 .leave = data_dev_leave,
932 .motion = data_dev_motion,
933 .drop = data_dev_drop,
934 .selection = data_dev_selection,
937 static void check_ready(struct wlt_display *disp)
939 if (disp->state > STATE_INIT)
949 log_debug("wayland display initialized");
952 disp->w_data_dev = wl_data_device_manager_get_data_device(
953 disp->w_manager, disp->w_seat);
954 wl_data_device_add_listener(disp->w_data_dev,
958 disp->state = STATE_RUNNING;
959 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
963 static void seat_capabilities(void *data, struct wl_seat *seat,
964 enum wl_seat_capability caps)
966 struct wlt_display *disp = data;
968 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
969 disp->w_pointer = wl_seat_get_pointer(seat);
970 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
974 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
975 disp->w_keyboard = wl_seat_get_keyboard(seat);
976 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
983 static const struct wl_seat_listener seat_listener = {
984 .capabilities = seat_capabilities,
987 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
988 uint32_t version, void *data)
990 struct wlt_display *disp = data;
992 if (!strcmp(iface, "wl_display")) {
993 log_debug("new wl_display global");
995 } else if (!strcmp(iface, "wl_compositor")) {
997 log_error("global wl_compositor advertised twice");
1000 disp->w_comp = wl_display_bind(disp->dp,
1002 &wl_compositor_interface);
1003 if (!disp->w_comp) {
1004 log_error("cannot bind wl_compositor object");
1007 } else if (!strcmp(iface, "wl_seat")) {
1009 log_error("global wl_seat advertised twice");
1012 disp->w_seat = wl_display_bind(disp->dp,
1014 &wl_seat_interface);
1015 if (!disp->w_seat) {
1016 log_error("cannot bind wl_seat object");
1020 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
1021 } else if (!strcmp(iface, "wl_shell")) {
1022 if (disp->w_shell) {
1023 log_error("global wl_shell advertised twice");
1026 disp->w_shell = wl_display_bind(disp->dp,
1028 &wl_shell_interface);
1029 if (!disp->w_shell) {
1030 log_error("cannot bind wl_shell object");
1033 } else if (!strcmp(iface, "wl_shm")) {
1035 log_error("global wl_shm advertised twice");
1038 disp->w_shm = wl_display_bind(disp->dp,
1042 log_error("cannot bind wl_shm object");
1045 } else if (!strcmp(iface, "wl_data_device_manager")) {
1046 if (disp->w_manager) {
1047 log_error("global wl_data_device_manager advertised twice");
1050 disp->w_manager = wl_display_bind(disp->dp, id,
1051 &wl_data_device_manager_interface);
1052 if (!disp->w_manager) {
1053 log_error("cannot bind wl_data_device_manager_object");
1057 log_debug("ignoring new unknown global %s", iface);
1061 log_debug("new global %s", iface);
1066 int wlt_display_new(struct wlt_display **out,
1067 struct ev_eloop *eloop)
1069 struct wlt_display *disp;
1075 log_debug("creating new wlt-display");
1077 disp = malloc(sizeof(*disp));
1080 memset(disp, 0, sizeof(*disp));
1082 disp->eloop = eloop;
1083 shl_dlist_init(&disp->window_list);
1085 ret = shl_hook_new(&disp->listeners);
1087 log_error("cannot create listeners hook");
1091 disp->dp = wl_display_connect(NULL);
1093 log_error("cannot connect to wayland socket (%d): %m", errno);
1098 ret = ev_eloop_new_fd(eloop,
1100 wl_display_get_fd(disp->dp,
1107 log_error("cannot create event-fd for wayland display (%d)",
1112 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
1113 repeat_event, disp);
1115 log_error("cannot create repeat-timer");
1119 disp->dp_listener = wl_display_add_global_listener(disp->dp,
1122 if (!disp->dp_listener) {
1123 log_error("cannot add wayland global listener (%d)", errno);
1127 disp->xkb_ctx = xkb_context_new(0);
1128 if (!disp->xkb_ctx) {
1129 log_error("cannot create XKB context");
1133 log_debug("wlt-display waiting for globals...");
1135 ev_eloop_ref(disp->eloop);
1140 ev_eloop_rm_timer(disp->repeat_timer);
1142 ev_eloop_rm_fd(disp->dp_fd);
1144 wl_display_disconnect(disp->dp);
1146 shl_hook_free(disp->listeners);
1152 void wlt_display_ref(struct wlt_display *disp)
1154 if (!disp || !disp->ref)
1160 void wlt_display_unref(struct wlt_display *disp)
1162 if (!disp || !disp->ref || --disp->ref)
1165 unload_cursors(disp);
1166 wl_display_remove_global_listener(disp->dp, disp->dp_listener);
1167 wl_display_flush(disp->dp);
1168 wl_display_disconnect(disp->dp);
1169 ev_eloop_rm_timer(disp->repeat_timer);
1170 ev_eloop_rm_fd(disp->dp_fd);
1171 xkb_context_unref(disp->xkb_ctx);
1172 shl_hook_free(disp->listeners);
1173 ev_eloop_unref(disp->eloop);
1177 int wlt_display_register_cb(struct wlt_display *disp,
1178 wlt_display_cb cb, void *data)
1183 return shl_hook_add_cast(disp->listeners, cb, data);
1186 void wlt_display_unregister_cb(struct wlt_display *disp,
1187 wlt_display_cb cb, void *data)
1192 shl_hook_rm_cast(disp->listeners, cb, data);
1195 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
1198 wl_shell_surface_pong(s, serial);
1201 static void wlt_window_do_redraw(struct wlt_window *wnd,
1205 struct shl_dlist *iter;
1206 struct wlt_widget *widget;
1207 unsigned int x, y, flags;
1208 struct wlt_rect alloc;
1212 flags |= WLT_WINDOW_MAXIMIZED;
1213 if (wnd->fullscreen)
1214 flags |= WLT_WINDOW_FULLSCREEN;
1218 alloc.width = wnd->buffer.width;
1219 alloc.height = wnd->buffer.height;
1220 shl_dlist_for_each(iter, &wnd->widget_list) {
1221 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1222 if (widget->resize_cb)
1223 widget->resize_cb(widget, flags, &alloc, widget->data);
1226 memset(wnd->buffer.data, 0,
1227 wnd->buffer.stride * wnd->buffer.height);
1229 wnd->skip_damage = true;
1230 shl_dlist_for_each(iter, &wnd->widget_list) {
1231 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1232 if (widget->redraw_cb)
1233 widget->redraw_cb(widget, flags, widget->data);
1235 wnd->skip_damage = false;
1237 if (!wnd->buffer_attached) {
1238 wnd->buffer_attached = true;
1239 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
1240 x = (int)oldw - wnd->buffer.width;
1243 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
1244 y = (int)oldh - wnd->buffer.height;
1247 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1248 wnd->resize_edges = 0;
1251 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1252 wnd->buffer.height);
1255 static int resize_window(struct wlt_window *wnd, unsigned int width,
1256 unsigned int height, bool force_redraw)
1258 struct shl_dlist *iter;
1259 struct wlt_widget *widget;
1260 struct wl_buffer *old_buffer = NULL;
1261 struct wlt_pool *old_pool = NULL;
1264 unsigned int oldw, oldh, neww, newh, minw, minh, flags;
1267 width = wnd->buffer.width;
1269 height = wnd->buffer.height;
1271 if (!width || !height)
1276 flags |= WLT_WINDOW_MAXIMIZED;
1277 if (wnd->fullscreen)
1278 flags |= WLT_WINDOW_FULLSCREEN;
1284 shl_dlist_for_each(iter, &wnd->widget_list) {
1285 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1286 if (widget->prepare_resize_cb)
1287 widget->prepare_resize_cb(widget,
1300 if (width == wnd->buffer.width &&
1301 height == wnd->buffer.height) {
1303 wlt_window_do_redraw(wnd, width, height);
1307 oldw = wnd->buffer.width;
1308 oldh = wnd->buffer.height;
1310 nsize = width * height * 4;
1311 if (wnd->pool && wnd->pool->size >= nsize) {
1312 old_buffer = wnd->w_buffer;
1313 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1316 WL_SHM_FORMAT_ARGB8888);
1317 if (!wnd->w_buffer) {
1318 log_error("cannot create wayland shm buffer");
1319 wnd->w_buffer = old_buffer;
1323 old_pool = wnd->pool;
1324 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1326 log_error("cannot create memory pool");
1327 wnd->pool = old_pool;
1331 old_buffer = wnd->w_buffer;
1332 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1335 WL_SHM_FORMAT_ARGB8888);
1336 if (!wnd->w_buffer) {
1337 log_error("cannot create wayland shm buffer");
1338 wnd->w_buffer = old_buffer;
1339 wlt_pool_free(wnd->pool);
1340 wnd->pool = old_pool;
1345 wnd->buffer.data = wnd->pool->data;
1346 wnd->buffer.width = width;
1347 wnd->buffer.height = height;
1348 wnd->buffer.stride = width * 4;
1349 wnd->buffer_attached = false;
1351 wlt_window_do_redraw(wnd, oldw, oldh);
1354 wl_buffer_destroy(old_buffer);
1356 wlt_pool_free(old_pool);
1361 static void frame_callback(void *data, struct wl_callback *w_callback,
1363 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1365 static const struct wl_callback_listener frame_callback_listener = {
1366 .done = frame_callback,
1369 static void do_frame(struct wlt_window *wnd)
1373 wnd->idle_pending = false;
1374 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1376 if (wnd->need_resize) {
1377 force = wnd->need_redraw;
1378 wnd->need_frame = true;
1379 wnd->need_resize = false;
1380 wnd->need_redraw = false;
1381 resize_window(wnd, wnd->new_width, wnd->new_height, force);
1384 if (wnd->need_redraw) {
1385 wnd->need_frame = true;
1386 wnd->need_redraw = false;
1387 wlt_window_do_redraw(wnd, wnd->buffer.width,
1388 wnd->buffer.height);
1391 if (wnd->need_frame) {
1392 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1393 wl_callback_add_listener(wnd->w_frame,
1394 &frame_callback_listener, wnd);
1398 static void frame_callback(void *data, struct wl_callback *w_callback,
1401 struct wlt_window *wnd = data;
1403 wl_callback_destroy(w_callback);
1404 wnd->w_frame = NULL;
1405 wnd->need_frame = false;
1410 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1412 struct wlt_window *wnd = data;
1418 * Buffer Handling and Frame Scheduling
1419 * We use wl_shm for buffer allocation. This means, we have a single buffer
1420 * client side and the server loads it into its backbuffer for rendering. If the
1421 * server does not do this, we are screwed anyway, but that's on behalf of the
1422 * server, so we don't care.
1424 * However, this means, when we create a buffer, we need to notify the
1425 * compositor and then wait until the compositor has created its back-buffer,
1426 * before we continue using this buffer. We can use the "frame" callback to get
1427 * notified about this.
1429 * The logic we have is:
1430 * You set the boolean flags what action is needed in wlt_window and then call
1431 * "schedule_frame()". If we didn't already do any buffer operations in this
1432 * frame, then this function schedules an idle-callback which then performs
1433 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1434 * that this frame was already used and schedules a frame-callback.
1435 * If during this time another call to schedule_frame() is made, we do nothing
1436 * but wait for the frame-callback. It will then directly perform all the
1437 * requested functions and reschedule a frame-callback.
1438 * If nothing was schedule for one frame, we have no more interest in
1439 * frame-callbacks and thus we set "need_frame" to false again and don't
1440 * schedule any more frame-callbacks.
1442 static void schedule_frame(struct wlt_window *wnd)
1446 if (!wnd || wnd->w_frame)
1449 if (wnd->need_frame || wnd->idle_pending)
1452 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1454 log_error("cannot schedule idle callback: %d", ret);
1456 wnd->idle_pending = true;
1459 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1460 uint32_t edges, int32_t width,
1463 struct wlt_window *wnd = data;
1470 wnd->resize_edges = edges;
1471 wlt_window_set_size(wnd, width, height);
1474 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1478 static const struct wl_shell_surface_listener shell_surface_listener = {
1479 .ping = shell_surface_ping,
1480 .configure = shell_surface_configure,
1481 .popup_done = shell_surface_popup_done,
1484 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1486 struct wlt_window *wnd = data;
1488 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1489 wnd->close_pending = false;
1492 wnd->close_cb(wnd, wnd->data);
1495 int wlt_display_create_window(struct wlt_display *disp,
1496 struct wlt_window **out,
1498 unsigned int height,
1501 struct wlt_window *wnd;
1504 if (!disp || !out || !width || !height)
1507 if (disp->state != STATE_RUNNING) {
1508 log_error("cannot create window, display is not running but in state %u",
1513 wnd = malloc(sizeof(*wnd));
1516 memset(wnd, 0, sizeof(*wnd));
1520 shl_dlist_init(&wnd->widget_list);
1522 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1523 if (!wnd->w_surface) {
1524 log_error("cannot create wayland surface");
1529 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1531 if (!wnd->w_shell_surface) {
1532 log_error("cannot retrieve shell-surface for wayland surface");
1537 wl_shell_surface_add_listener(wnd->w_shell_surface,
1538 &shell_surface_listener, wnd);
1539 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1541 ret = resize_window(wnd, width, height, true);
1543 goto err_shell_surface;
1545 wlt_display_ref(disp);
1546 shl_dlist_link(&disp->window_list, &wnd->list);
1551 wl_shell_surface_destroy(wnd->w_shell_surface);
1553 wl_surface_destroy(wnd->w_surface);
1559 void wlt_window_ref(struct wlt_window *wnd)
1561 if (!wnd || !wnd->ref)
1567 void wlt_window_unref(struct wlt_window *wnd)
1569 struct wlt_widget *widget;
1571 if (!wnd || !wnd->ref || --wnd->ref)
1574 while (!shl_dlist_empty(&wnd->widget_list)) {
1575 widget = shl_dlist_entry(wnd->widget_list.next,
1576 struct wlt_widget, list);
1577 wlt_widget_destroy(widget);
1580 if (wnd->close_pending)
1581 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1583 if (wnd->idle_pending)
1584 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1585 shl_dlist_unlink(&wnd->list);
1587 wl_callback_destroy(wnd->w_frame);
1589 wl_buffer_destroy(wnd->w_buffer);
1591 wlt_pool_free(wnd->pool);
1592 wl_shell_surface_destroy(wnd->w_shell_surface);
1593 wl_surface_destroy(wnd->w_surface);
1594 wlt_display_unref(wnd->disp);
1598 int wlt_window_create_widget(struct wlt_window *wnd,
1599 struct wlt_widget **out,
1602 struct wlt_widget *widget;
1607 widget = malloc(sizeof(*widget));
1610 memset(widget, 0, sizeof(*widget));
1612 widget->data = data;
1614 wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
1615 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1620 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1625 wnd->need_redraw = true;
1626 schedule_frame(wnd);
1629 void wlt_window_damage(struct wlt_window *wnd,
1630 struct wlt_rect *damage)
1632 if (!wnd || !damage || wnd->skip_damage)
1635 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1636 damage->width, damage->height);
1639 void wlt_window_get_buffer(struct wlt_window *wnd,
1640 const struct wlt_rect *alloc,
1641 struct wlt_shm_buffer *buf)
1643 struct wlt_shm_buffer *rbuf;
1648 rbuf = &wnd->buffer;
1651 memcpy(buf, rbuf, sizeof(*buf));
1655 if (alloc->x >= rbuf->width ||
1656 alloc->y >= rbuf->height) {
1657 memset(buf, 0, sizeof(*buf));
1662 if (alloc->x + alloc->width > rbuf->width)
1663 buf->width = rbuf->width - alloc->x;
1665 buf->width = alloc->width;
1668 if (alloc->y + alloc->height > rbuf->height)
1669 buf->height = rbuf->height - alloc->y;
1671 buf->height = alloc->height;
1674 buf->stride = rbuf->stride;
1677 buf->data = rbuf->data;
1678 buf->data += alloc->y * rbuf->stride;
1679 buf->data += alloc->x * 4;
1682 void wlt_window_move(struct wlt_window *wnd)
1687 wl_shell_surface_move(wnd->w_shell_surface,
1689 wnd->disp->last_serial);
1692 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1697 wl_shell_surface_resize(wnd->w_shell_surface,
1699 wnd->disp->last_serial,
1703 int wlt_window_set_size(struct wlt_window *wnd,
1704 unsigned int width, unsigned int height)
1709 wnd->new_width = width;
1710 wnd->new_height = height;
1711 wnd->need_resize = true;
1712 schedule_frame(wnd);
1717 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1722 set_cursor(wnd->disp, cursor);
1725 void wlt_window_set_close_cb(struct wlt_window *wnd,
1726 wlt_window_close_cb cb)
1734 void wlt_window_close(struct wlt_window *wnd)
1739 wnd->close_pending = true;
1740 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1743 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1748 if (wnd->maximized) {
1749 if (!wnd->fullscreen) {
1750 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1751 wlt_window_set_size(wnd, wnd->saved_width,
1755 if (!wnd->fullscreen) {
1756 wnd->saved_width = wnd->buffer.width;
1757 wnd->saved_height = wnd->buffer.height;
1758 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1763 wnd->maximized = !wnd->maximized;
1766 void wlt_window_toggle_fullscreen(struct wlt_window *wnd)
1771 if (wnd->fullscreen) {
1772 if (wnd->maximized) {
1773 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1776 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1777 wlt_window_set_size(wnd, wnd->saved_width,
1781 if (!wnd->maximized) {
1782 wnd->saved_width = wnd->buffer.width;
1783 wnd->saved_height = wnd->buffer.height;
1785 wl_shell_surface_set_fullscreen(wnd->w_shell_surface,
1786 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1790 wnd->fullscreen = !wnd->fullscreen;
1793 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1798 return wnd->disp->eloop;
1801 struct wlt_display *wlt_window_get_display(struct wlt_window *wnd)
1809 void wlt_widget_destroy(struct wlt_widget *widget)
1814 if (widget->destroy_cb)
1815 widget->destroy_cb(widget, widget->data);
1816 shl_dlist_unlink(&widget->list);
1820 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1828 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1829 wlt_widget_redraw_cb cb)
1834 widget->redraw_cb = cb;
1837 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1838 wlt_widget_destroy_cb cb)
1843 widget->destroy_cb = cb;
1846 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1847 wlt_widget_prepare_resize_cb prepare_cb,
1848 wlt_widget_resize_cb cb)
1853 widget->prepare_resize_cb = prepare_cb;
1854 widget->resize_cb = cb;
1857 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1858 wlt_widget_pointer_enter_cb enter_cb,
1859 wlt_widget_pointer_leave_cb leave_cb,
1860 wlt_widget_pointer_motion_cb motion_cb,
1861 wlt_widget_pointer_button_cb button_cb)
1866 widget->pointer_enter_cb = enter_cb;
1867 widget->pointer_leave_cb = leave_cb;
1868 widget->pointer_motion_cb = motion_cb;
1869 widget->pointer_button_cb = button_cb;
1872 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1873 wlt_widget_keyboard_cb cb)
1878 widget->keyboard_cb = cb;