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;
78 struct wl_registry *w_registry;
80 struct shl_dlist window_list;
82 struct wl_compositor *w_comp;
83 struct wl_seat *w_seat;
84 struct wl_shell *w_shell;
88 uint32_t pointer_enter_serial;
89 struct wl_pointer *w_pointer;
90 struct wlt_window *pointer_focus;
92 uint32_t cursor_serial;
93 unsigned int current_cursor;
94 struct wl_surface *w_cursor_surface;
95 struct wl_cursor_theme *cursor_theme;
96 struct wl_cursor *cursors[WLT_CURSOR_NUM];
98 struct wl_keyboard *w_keyboard;
99 struct xkb_context *xkb_ctx;
100 struct xkb_keymap *xkb_keymap;
101 struct xkb_state *xkb_state;
102 struct wlt_window *keyboard_focus;
103 struct ev_timer *repeat_timer;
105 uint32_t repeat_ascii;
107 struct wl_data_device_manager *w_manager;
108 struct wl_data_device *w_data_dev;
109 struct data_offer *drag_offer;
110 struct data_offer *selection_offer;
115 struct shl_dlist list;
117 wlt_window_close_cb close_cb;
119 struct wlt_display *disp;
120 struct wlt_pool *pool;
122 struct wl_surface *w_surface;
123 struct wl_shell_surface *w_shell_surface;
124 struct wl_buffer *w_buffer;
126 bool buffer_attached;
132 unsigned int new_width;
133 unsigned int new_height;
134 unsigned int saved_width;
135 unsigned int saved_height;
136 unsigned int resize_edges;
139 struct wlt_shm_buffer buffer;
140 struct wl_callback *w_frame;
142 struct shl_dlist widget_list;
146 struct shl_dlist list;
147 struct wlt_window *wnd;
149 wlt_widget_redraw_cb redraw_cb;
150 wlt_widget_destroy_cb destroy_cb;
151 wlt_widget_prepare_resize_cb prepare_resize_cb;
152 wlt_widget_resize_cb resize_cb;
153 wlt_widget_pointer_enter_cb pointer_enter_cb;
154 wlt_widget_pointer_leave_cb pointer_leave_cb;
155 wlt_widget_pointer_motion_cb pointer_motion_cb;
156 wlt_widget_pointer_button_cb pointer_button_cb;
157 wlt_widget_keyboard_cb keyboard_cb;
160 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
163 static const char template[] = "/wlterm-shared-XXXXXX";
164 struct wlt_pool *pool;
169 path = getenv("XDG_RUNTIME_DIR");
171 log_error("XDG_RUNTIME_DIR not set");
175 pool = malloc(sizeof(*pool));
178 memset(pool, 0, sizeof(*pool));
181 name = malloc(strlen(path) + sizeof(template));
187 strcat(name, template);
189 fd = mkostemp(name, O_CLOEXEC);
191 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
200 log_warning("cannot unlink temporary file %s (%d): %m",
204 ret = ftruncate(fd, size);
206 log_error("cannot truncate temporary file to length %lu (%d): %m",
212 pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
214 if (pool->data == MAP_FAILED) {
215 log_error("cannot mmap temporary file (%d): %m", errno);
220 pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
222 log_error("cannot create wayland shm pool object");
232 munmap(pool->data, size);
240 static void wlt_pool_free(struct wlt_pool *pool)
242 munmap(pool->data, pool->size);
243 wl_shm_pool_destroy(pool->w_pool);
248 * These cursor-names are based on:
249 * https://bugs.kde.org/attachment.cgi?id=67313
252 static char *(*cursors[])[] = {
253 [WLT_CURSOR_NONE] = &(char*[]) {
256 [WLT_CURSOR_TOP] = &(char*[]) {
261 [WLT_CURSOR_BOTTOM] = &(char*[]) {
266 [WLT_CURSOR_LEFT] = &(char*[]) {
271 [WLT_CURSOR_RIGHT] = &(char*[]) {
276 [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
281 [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
286 [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
287 "bottom_left_corner",
291 [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
292 "bottom_right_corner",
296 [WLT_CURSOR_DRAGGING] = &(char*[]) {
299 "208530c400c041818281048008011002",
302 [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
309 [WLT_CURSOR_IBEAM] = &(char*[]) {
317 static int load_cursors(struct wlt_display *disp)
320 size_t size = sizeof(cursors) / sizeof(*cursors);
321 struct wl_cursor *cursor;
323 disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
325 disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
326 if (!disp->cursor_theme) {
327 log_warning("cannot load curdor theme");
331 for (i = 0; i < WLT_CURSOR_NUM; ++i) {
333 if (i < size && cursors[i]) {
334 for (j = 0; (*cursors[i])[j]; ++j) {
335 cursor = wl_cursor_theme_get_cursor(
343 if (!cursor && i != WLT_CURSOR_NONE)
344 log_warning("cannot load cursor for ID %d", i);
346 disp->cursors[i] = cursor;
352 static void unload_cursors(struct wlt_display *disp)
354 if (disp->cursor_theme)
355 wl_cursor_theme_destroy(disp->cursor_theme);
356 if (disp->w_cursor_surface)
357 wl_surface_destroy(disp->w_cursor_surface);
360 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
363 struct wl_cursor *cur;
364 struct wl_cursor_image *image;
365 struct wl_buffer *buffer;
367 if (cursor >= WLT_CURSOR_NUM)
368 cursor = WLT_CURSOR_LEFT_PTR;
370 if (disp->pointer_enter_serial > disp->cursor_serial)
373 if (!force && cursor == disp->current_cursor)
376 disp->current_cursor = cursor;
377 disp->cursor_serial = disp->pointer_enter_serial;
379 cur = disp->cursors[cursor];
381 wl_pointer_set_cursor(disp->w_pointer,
382 disp->pointer_enter_serial,
387 image = cur->images[0];
388 buffer = wl_cursor_image_get_buffer(image);
390 log_error("cannot load buffer for cursor image");
394 wl_pointer_set_cursor(disp->w_pointer,
395 disp->pointer_enter_serial,
396 disp->w_cursor_surface,
397 image->hotspot_x, image->hotspot_y);
398 wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
399 wl_surface_damage(disp->w_cursor_surface, 0, 0,
400 image->width, image->height);
401 wl_surface_commit(disp->w_cursor_surface);
404 static void dp_dispatch(struct wlt_display *disp, bool nonblock)
410 ret = wl_display_dispatch_pending(disp->dp);
412 ret = wl_display_dispatch(disp->dp);
415 log_error("error during wayland dispatch (%d): %m", errno);
417 } else if (errno == EAGAIN) {
418 ret = ev_fd_update(disp->dp_fd, EV_READABLE | EV_WRITEABLE);
420 log_warning("cannot update wayland-fd event-polling modes (%d)",
423 ret = ev_fd_update(disp->dp_fd, EV_READABLE);
425 log_warning("cannot update wayland-fd event-polling modes (%d)",
430 static void dp_pre_event(struct ev_eloop *eloop, void *unused, void *data)
432 struct wlt_display *disp = data;
434 dp_dispatch(disp, true);
437 static void dp_event(struct ev_fd *fd, int mask, void *data)
439 struct wlt_display *disp = data;
441 if (mask & (EV_HUP | EV_ERR)) {
442 log_warning("HUP/ERR on wayland socket");
443 disp->state = STATE_HUP;
444 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
445 ev_eloop_rm_fd(disp->dp_fd);
450 if (mask & EV_READABLE)
451 dp_dispatch(disp, false);
453 dp_dispatch(disp, true);
456 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
457 uint32_t serial, struct wl_surface *w_surface,
458 wl_fixed_t x, wl_fixed_t y)
460 struct wlt_display *disp = data;
461 struct shl_dlist *iter;
462 struct wlt_window *wnd;
463 struct wlt_widget *widget;
468 shl_dlist_for_each(iter, &disp->window_list) {
469 wnd = shl_dlist_entry(iter, struct wlt_window, list);
470 if (wnd->w_surface == w_surface)
474 if (iter == &disp->window_list) {
475 log_debug("unknown surface");
479 disp->pointer_enter_serial = serial;
480 disp->last_serial = serial;
481 disp->pointer_focus = wnd;
482 shl_dlist_for_each(iter, &wnd->widget_list) {
483 widget = shl_dlist_entry(iter, struct wlt_widget, list);
484 if (widget->pointer_enter_cb)
485 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
490 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
491 uint32_t serial, struct wl_surface *w_surface)
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;
499 disp->pointer_focus = NULL;
500 disp->last_serial = serial;
505 shl_dlist_for_each(iter, &wnd->widget_list) {
506 widget = shl_dlist_entry(iter, struct wlt_widget, list);
507 if (widget->pointer_leave_cb)
508 widget->pointer_leave_cb(widget, widget->data);
512 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
513 uint32_t time, wl_fixed_t x, wl_fixed_t y)
515 struct wlt_display *disp = data;
516 struct shl_dlist *iter;
517 struct wlt_window *wnd;
518 struct wlt_widget *widget;
520 wnd = disp->pointer_focus;
524 shl_dlist_for_each(iter, &wnd->widget_list) {
525 widget = shl_dlist_entry(iter, struct wlt_widget, list);
526 if (widget->pointer_motion_cb)
527 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
532 static void pointer_button(void *data, struct wl_pointer *w_pointer,
533 uint32_t serial, uint32_t time, uint32_t button,
536 struct wlt_display *disp = data;
537 struct shl_dlist *iter;
538 struct wlt_window *wnd;
539 struct wlt_widget *widget;
541 wnd = disp->pointer_focus;
542 disp->last_serial = serial;
547 shl_dlist_for_each(iter, &wnd->widget_list) {
548 widget = shl_dlist_entry(iter, struct wlt_widget, list);
549 if (widget->pointer_button_cb)
550 widget->pointer_button_cb(widget, button, state,
555 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
556 uint32_t time, uint32_t axis, wl_fixed_t value)
560 static const struct wl_pointer_listener pointer_listener = {
561 .enter = pointer_enter,
562 .leave = pointer_leave,
563 .motion = pointer_motion,
564 .button = pointer_button,
565 .axis = pointer_axis,
568 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
569 uint32_t format, int fd, uint32_t size)
571 struct wlt_display *disp = data;
574 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
575 log_error("invalid keyboard format");
580 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
581 if (map == MAP_FAILED) {
582 log_error("cannot mmap keyboard keymap");
587 disp->xkb_keymap = xkb_keymap_new_from_string(disp->xkb_ctx, map,
588 XKB_KEYMAP_FORMAT_TEXT_V1,
593 if (!disp->xkb_keymap) {
594 log_error("cannot create xkb keymap");
598 disp->xkb_state = xkb_state_new(disp->xkb_keymap);
599 if (!disp->xkb_state) {
600 log_error("cannot create XKB state object");
601 xkb_keymap_unref(disp->xkb_keymap);
602 disp->xkb_keymap = NULL;
607 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
608 uint32_t serial, struct wl_surface *w_surface,
609 struct wl_array *keys)
611 struct wlt_display *disp = data;
612 struct shl_dlist *iter;
613 struct wlt_window *wnd;
615 disp->last_serial = serial;
616 if (!disp->xkb_state)
622 shl_dlist_for_each(iter, &disp->window_list) {
623 wnd = shl_dlist_entry(iter, struct wlt_window, list);
624 if (wnd->w_surface == w_surface)
628 if (iter == &disp->window_list)
631 disp->keyboard_focus = wnd;
634 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
635 uint32_t serial, struct wl_surface *surface)
637 struct wlt_display *disp = data;
639 disp->last_serial = serial;
640 disp->keyboard_focus = NULL;
641 ev_timer_update(disp->repeat_timer, NULL);
644 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
645 uint32_t serial, uint32_t time, uint32_t key,
648 struct wlt_display *disp = data;
649 struct wlt_window *wnd = disp->keyboard_focus;
650 uint32_t code, num_syms, ascii;
652 enum wl_keyboard_key_state state = state_w;
653 const xkb_keysym_t *syms;
655 struct shl_dlist *iter;
656 struct wlt_widget *widget;
657 struct itimerspec spec;
660 disp->last_serial = serial;
661 if (!disp->xkb_state)
668 mask = shl_get_xkb_mods(disp->xkb_state);
669 num_syms = xkb_state_key_get_syms(disp->xkb_state, code, &syms);
670 ascii = shl_get_ascii(disp->xkb_state, code, syms, num_syms);
671 sym = XKB_KEY_NoSymbol;
676 shl_dlist_for_each(iter, &wnd->widget_list) {
677 widget = shl_dlist_entry(iter, struct wlt_widget, list);
678 if (widget->keyboard_cb) {
679 if (widget->keyboard_cb(widget, mask, sym, ascii, state,
680 handled, widget->data))
685 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
686 sym == disp->repeat_sym) {
687 ev_timer_update(disp->repeat_timer, NULL);
688 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
689 disp->repeat_sym = sym;
690 disp->repeat_ascii = ascii;
691 spec.it_interval.tv_sec = 0;
692 spec.it_interval.tv_nsec = wlt_conf.xkb_repeat_rate * 1000000;
693 spec.it_value.tv_sec = 0;
694 spec.it_value.tv_nsec = wlt_conf.xkb_repeat_delay * 1000000;
695 ev_timer_update(disp->repeat_timer, &spec);
699 static void repeat_event(struct ev_timer *timer, uint64_t num, void *data)
701 struct wlt_display *disp = data;
702 struct wlt_window *wnd = disp->keyboard_focus;
703 struct wlt_widget *widget;
704 struct shl_dlist *iter;
711 mask = shl_get_xkb_mods(disp->xkb_state);
713 shl_dlist_for_each(iter, &wnd->widget_list) {
714 widget = shl_dlist_entry(iter, struct wlt_widget, list);
715 if (widget->keyboard_cb) {
716 if (widget->keyboard_cb(widget, mask, disp->repeat_sym,
718 WL_KEYBOARD_KEY_STATE_PRESSED,
719 handled, widget->data))
725 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
726 uint32_t serial, uint32_t mods_depressed,
727 uint32_t mods_latched, uint32_t mods_locked,
730 struct wlt_display *disp = data;
732 disp->last_serial = serial;
733 if (!disp->xkb_state)
736 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
737 mods_locked, 0, 0, group);
740 static const struct wl_keyboard_listener keyboard_listener = {
741 .keymap = keyboard_keymap,
742 .enter = keyboard_enter,
743 .leave = keyboard_leave,
745 .modifiers = keyboard_modifiers,
748 static void data_offer_unref(struct data_offer *offer)
752 if (!offer || !offer->ref || --offer->ref)
755 len = shl_array_get_length(offer->types);
756 for (i = 0; i < len; ++i)
757 free(*SHL_ARRAY_AT(offer->types, char*, i));
758 shl_array_free(offer->types);
759 wl_data_offer_destroy(offer->w_offer);
763 static void data_offer_offer(void *data, struct wl_data_offer *w_offer,
768 struct data_offer *offer = wl_data_offer_get_user_data(w_offer);
774 ret = shl_array_push(offer->types, &tmp);
781 static const struct wl_data_offer_listener data_offer_listener = {
782 .offer = data_offer_offer,
785 static void data_dev_data_offer(void *data, struct wl_data_device *w_dev,
786 struct wl_data_offer *w_offer)
788 struct data_offer *offer;
789 struct wlt_display *disp = data;
792 offer = malloc(sizeof(*offer));
794 wl_data_offer_destroy(w_offer);
797 memset(offer, 0, sizeof(*offer));
799 offer->w_offer = w_offer;
802 ret = shl_array_new(&offer->types, sizeof(char*), 4);
804 wl_data_offer_destroy(w_offer);
809 wl_data_offer_add_listener(w_offer, &data_offer_listener, offer);
810 wl_data_offer_set_user_data(w_offer, offer);
813 static void data_dev_enter(void *data, struct wl_data_device *w_dev,
814 uint32_t serial, struct wl_surface *w_surface,
815 wl_fixed_t x, wl_fixed_t y,
816 struct wl_data_offer *w_offer)
818 struct wlt_display *disp = data;
820 if (disp->drag_offer) {
821 data_offer_unref(disp->drag_offer);
822 disp->drag_offer = NULL;
828 disp->drag_offer = wl_data_offer_get_user_data(w_offer);
831 static void data_dev_leave(void *data, struct wl_data_device *w_dev)
833 struct wlt_display *disp = data;
835 if (disp->drag_offer) {
836 data_offer_unref(disp->drag_offer);
837 disp->drag_offer = NULL;
841 static void data_dev_motion(void *data, struct wl_data_device *w_dev,
842 uint32_t time, wl_fixed_t x, wl_fixed_t y)
846 static void data_dev_drop(void *data, struct wl_data_device *w_dev)
850 static void data_dev_selection(void *data, struct wl_data_device *w_dev,
851 struct wl_data_offer *w_offer)
853 struct wlt_display *disp = data;
855 if (disp->selection_offer) {
856 data_offer_unref(disp->selection_offer);
857 disp->selection_offer = NULL;
863 disp->selection_offer = wl_data_offer_get_user_data(w_offer);
866 int wlt_display_get_selection_to_fd(struct wlt_display *disp, const char *mime,
870 struct data_offer *offer;
874 if (!disp->selection_offer)
877 offer = disp->selection_offer;
878 num = shl_array_get_length(offer->types);
879 for (i = 0; i < num; ++i) {
880 if (!strcmp(mime, *SHL_ARRAY_AT(offer->types, char*, i)))
887 wl_data_offer_receive(offer->w_offer, mime, output_fd);
891 int wlt_display_get_selection_fd(struct wlt_display *disp, const char *mime)
895 if (pipe2(p, O_CLOEXEC | O_NONBLOCK))
898 ret = wlt_display_get_selection_to_fd(disp, mime, p[1]);
909 int wlt_display_new_data_source(struct wlt_display *disp,
910 struct wl_data_source **out)
912 struct wl_data_source *src;
917 src = wl_data_device_manager_create_data_source(disp->w_manager);
925 void wlt_display_set_selection(struct wlt_display *disp,
926 struct wl_data_source *selection)
931 wl_data_device_set_selection(disp->w_data_dev, selection,
935 static const struct wl_data_device_listener data_dev_listener = {
936 .data_offer = data_dev_data_offer,
937 .enter = data_dev_enter,
938 .leave = data_dev_leave,
939 .motion = data_dev_motion,
940 .drop = data_dev_drop,
941 .selection = data_dev_selection,
944 static void check_ready(struct wlt_display *disp)
946 if (disp->state > STATE_INIT)
956 log_debug("wayland display initialized");
959 disp->w_data_dev = wl_data_device_manager_get_data_device(
960 disp->w_manager, disp->w_seat);
961 wl_data_device_add_listener(disp->w_data_dev,
965 disp->state = STATE_RUNNING;
966 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
970 static void seat_capabilities(void *data, struct wl_seat *seat,
971 enum wl_seat_capability caps)
973 struct wlt_display *disp = data;
975 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
976 disp->w_pointer = wl_seat_get_pointer(seat);
977 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
981 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
982 disp->w_keyboard = wl_seat_get_keyboard(seat);
983 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
990 static const struct wl_seat_listener seat_listener = {
991 .capabilities = seat_capabilities,
994 static void dp_global(void *data, struct wl_registry *registry, uint32_t id,
995 const char *iface, uint32_t version)
997 struct wlt_display *disp = data;
999 if (!strcmp(iface, "wl_display")) {
1000 log_debug("new wl_display global");
1002 } else if (!strcmp(iface, "wl_compositor")) {
1004 log_error("global wl_compositor advertised twice");
1007 disp->w_comp = wl_registry_bind(disp->w_registry,
1009 &wl_compositor_interface,
1011 if (!disp->w_comp) {
1012 log_error("cannot bind wl_compositor object");
1015 } else if (!strcmp(iface, "wl_seat")) {
1017 log_error("global wl_seat advertised twice");
1020 disp->w_seat = wl_registry_bind(disp->w_registry,
1024 if (!disp->w_seat) {
1025 log_error("cannot bind wl_seat object");
1029 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
1030 } else if (!strcmp(iface, "wl_shell")) {
1031 if (disp->w_shell) {
1032 log_error("global wl_shell advertised twice");
1035 disp->w_shell = wl_registry_bind(disp->w_registry,
1037 &wl_shell_interface,
1039 if (!disp->w_shell) {
1040 log_error("cannot bind wl_shell object");
1043 } else if (!strcmp(iface, "wl_shm")) {
1045 log_error("global wl_shm advertised twice");
1048 disp->w_shm = wl_registry_bind(disp->w_registry,
1053 log_error("cannot bind wl_shm object");
1056 } else if (!strcmp(iface, "wl_data_device_manager")) {
1057 if (disp->w_manager) {
1058 log_error("global wl_data_device_manager advertised twice");
1061 disp->w_manager = wl_registry_bind(disp->w_registry,
1063 &wl_data_device_manager_interface,
1065 if (!disp->w_manager) {
1066 log_error("cannot bind wl_data_device_manager_object");
1070 log_debug("ignoring new unknown global %s", iface);
1074 log_debug("new global %s", iface);
1079 static const struct wl_registry_listener registry_listener = {
1080 .global = dp_global,
1081 /* TODO: handle .global_remove */
1084 int wlt_display_new(struct wlt_display **out,
1085 struct ev_eloop *eloop)
1087 struct wlt_display *disp;
1093 log_debug("creating new wlt-display");
1095 disp = malloc(sizeof(*disp));
1098 memset(disp, 0, sizeof(*disp));
1100 disp->eloop = eloop;
1101 shl_dlist_init(&disp->window_list);
1103 ret = shl_hook_new(&disp->listeners);
1105 log_error("cannot create listeners hook");
1109 disp->dp = wl_display_connect(NULL);
1111 log_error("cannot connect to wayland socket (%d): %m", errno);
1116 fd = wl_display_get_fd(disp->dp);
1118 log_error("cannot retrieve wayland FD");
1123 /* TODO: nonblocking wl_display doesn't work, yet. fix that upstream */
1126 set = fcntl(fd, F_GETFL);
1128 log_error("cannot retrieve file flags for wayland FD (%d): %m",
1135 ret = fcntl(fd, F_SETFL, set);
1137 log_error("cannot set file flags for wayland FD (%d): %m",
1143 ret = ev_eloop_new_fd(eloop,
1150 log_error("cannot create event-fd for wayland display (%d)",
1155 ret = ev_eloop_register_pre_cb(eloop, dp_pre_event, disp);
1157 log_error("cannot register pre-cb (%d)", ret);
1161 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
1162 repeat_event, disp);
1164 log_error("cannot create repeat-timer");
1168 disp->w_registry = wl_display_get_registry(disp->dp);
1169 if (!disp->w_registry) {
1170 log_error("cannot get wayland registry object");
1174 wl_registry_add_listener(disp->w_registry, ®istry_listener, disp);
1176 disp->xkb_ctx = xkb_context_new(0);
1177 if (!disp->xkb_ctx) {
1178 log_error("cannot create XKB context");
1182 log_debug("wlt-display waiting for globals...");
1184 ev_eloop_ref(disp->eloop);
1189 ev_eloop_rm_timer(disp->repeat_timer);
1191 ev_eloop_unregister_pre_cb(disp->eloop, dp_pre_event, disp);
1193 ev_eloop_rm_fd(disp->dp_fd);
1195 wl_display_disconnect(disp->dp);
1197 shl_hook_free(disp->listeners);
1203 void wlt_display_ref(struct wlt_display *disp)
1205 if (!disp || !disp->ref)
1211 void wlt_display_unref(struct wlt_display *disp)
1213 if (!disp || !disp->ref || --disp->ref)
1216 unload_cursors(disp);
1217 wl_display_flush(disp->dp);
1218 wl_display_disconnect(disp->dp);
1219 ev_eloop_rm_timer(disp->repeat_timer);
1220 ev_eloop_unregister_pre_cb(disp->eloop, dp_pre_event, disp);
1221 ev_eloop_rm_fd(disp->dp_fd);
1222 xkb_context_unref(disp->xkb_ctx);
1223 shl_hook_free(disp->listeners);
1224 ev_eloop_unref(disp->eloop);
1228 int wlt_display_register_cb(struct wlt_display *disp,
1229 wlt_display_cb cb, void *data)
1234 return shl_hook_add_cast(disp->listeners, cb, data);
1237 void wlt_display_unregister_cb(struct wlt_display *disp,
1238 wlt_display_cb cb, void *data)
1243 shl_hook_rm_cast(disp->listeners, cb, data);
1246 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
1249 wl_shell_surface_pong(s, serial);
1252 static void wlt_window_do_redraw(struct wlt_window *wnd,
1256 struct shl_dlist *iter;
1257 struct wlt_widget *widget;
1258 unsigned int x, y, flags;
1259 struct wlt_rect alloc;
1263 flags |= WLT_WINDOW_MAXIMIZED;
1264 if (wnd->fullscreen)
1265 flags |= WLT_WINDOW_FULLSCREEN;
1269 alloc.width = wnd->buffer.width;
1270 alloc.height = wnd->buffer.height;
1271 shl_dlist_for_each(iter, &wnd->widget_list) {
1272 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1273 if (widget->resize_cb)
1274 widget->resize_cb(widget, flags, &alloc, widget->data);
1277 memset(wnd->buffer.data, 0,
1278 wnd->buffer.stride * wnd->buffer.height);
1280 wnd->skip_damage = true;
1281 shl_dlist_for_each(iter, &wnd->widget_list) {
1282 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1283 if (widget->redraw_cb)
1284 widget->redraw_cb(widget, flags, widget->data);
1286 wnd->skip_damage = false;
1288 if (!wnd->buffer_attached) {
1289 wnd->buffer_attached = true;
1290 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
1291 x = (int)oldw - wnd->buffer.width;
1294 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
1295 y = (int)oldh - wnd->buffer.height;
1298 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1299 wnd->resize_edges = 0;
1302 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1303 wnd->buffer.height);
1304 wl_surface_commit(wnd->w_surface);
1307 static int resize_window(struct wlt_window *wnd, unsigned int width,
1308 unsigned int height, bool force_redraw)
1310 struct shl_dlist *iter;
1311 struct wlt_widget *widget;
1312 struct wl_buffer *old_buffer = NULL;
1313 struct wlt_pool *old_pool = NULL;
1316 unsigned int oldw, oldh, neww, newh, minw, minh, flags;
1319 width = wnd->buffer.width;
1321 height = wnd->buffer.height;
1323 if (!width || !height)
1328 flags |= WLT_WINDOW_MAXIMIZED;
1329 if (wnd->fullscreen)
1330 flags |= WLT_WINDOW_FULLSCREEN;
1336 shl_dlist_for_each(iter, &wnd->widget_list) {
1337 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1338 if (widget->prepare_resize_cb)
1339 widget->prepare_resize_cb(widget,
1352 if (width == wnd->buffer.width &&
1353 height == wnd->buffer.height) {
1355 wlt_window_do_redraw(wnd, width, height);
1359 oldw = wnd->buffer.width;
1360 oldh = wnd->buffer.height;
1362 nsize = width * height * 4;
1363 if (wnd->pool && wnd->pool->size >= nsize) {
1364 old_buffer = wnd->w_buffer;
1365 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1368 WL_SHM_FORMAT_ARGB8888);
1369 if (!wnd->w_buffer) {
1370 log_error("cannot create wayland shm buffer");
1371 wnd->w_buffer = old_buffer;
1375 old_pool = wnd->pool;
1376 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1378 log_error("cannot create memory pool");
1379 wnd->pool = old_pool;
1383 old_buffer = wnd->w_buffer;
1384 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1387 WL_SHM_FORMAT_ARGB8888);
1388 if (!wnd->w_buffer) {
1389 log_error("cannot create wayland shm buffer");
1390 wnd->w_buffer = old_buffer;
1391 wlt_pool_free(wnd->pool);
1392 wnd->pool = old_pool;
1397 wnd->buffer.data = wnd->pool->data;
1398 wnd->buffer.width = width;
1399 wnd->buffer.height = height;
1400 wnd->buffer.stride = width * 4;
1401 wnd->buffer_attached = false;
1403 wlt_window_do_redraw(wnd, oldw, oldh);
1406 wl_buffer_destroy(old_buffer);
1408 wlt_pool_free(old_pool);
1413 static void frame_callback(void *data, struct wl_callback *w_callback,
1415 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1417 static const struct wl_callback_listener frame_callback_listener = {
1418 .done = frame_callback,
1421 static void do_frame(struct wlt_window *wnd)
1425 wnd->idle_pending = false;
1426 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1428 if (wnd->need_resize) {
1429 force = wnd->need_redraw;
1430 wnd->need_frame = true;
1431 wnd->need_resize = false;
1432 wnd->need_redraw = false;
1433 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1434 wl_callback_add_listener(wnd->w_frame,
1435 &frame_callback_listener, wnd);
1436 resize_window(wnd, wnd->new_width, wnd->new_height, force);
1439 if (wnd->need_redraw) {
1440 wnd->need_frame = true;
1441 wnd->need_redraw = false;
1442 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1443 wl_callback_add_listener(wnd->w_frame,
1444 &frame_callback_listener, wnd);
1445 wlt_window_do_redraw(wnd, wnd->buffer.width,
1446 wnd->buffer.height);
1450 static void frame_callback(void *data, struct wl_callback *w_callback,
1453 struct wlt_window *wnd = data;
1455 wl_callback_destroy(w_callback);
1456 wnd->w_frame = NULL;
1457 wnd->need_frame = false;
1462 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1464 struct wlt_window *wnd = data;
1470 * Buffer Handling and Frame Scheduling
1471 * We use wl_shm for buffer allocation. This means, we have a single buffer
1472 * client side and the server loads it into its backbuffer for rendering. If the
1473 * server does not do this, we are screwed anyway, but that's on behalf of the
1474 * server, so we don't care.
1476 * However, this means, when we create a buffer, we need to notify the
1477 * compositor and then wait until the compositor has created its back-buffer,
1478 * before we continue using this buffer. We can use the "frame" callback to get
1479 * notified about this.
1481 * The logic we have is:
1482 * You set the boolean flags what action is needed in wlt_window and then call
1483 * "schedule_frame()". If we didn't already do any buffer operations in this
1484 * frame, then this function schedules an idle-callback which then performs
1485 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1486 * that this frame was already used and schedules a frame-callback.
1487 * If during this time another call to schedule_frame() is made, we do nothing
1488 * but wait for the frame-callback. It will then directly perform all the
1489 * requested functions and reschedule a frame-callback.
1490 * If nothing was schedule for one frame, we have no more interest in
1491 * frame-callbacks and thus we set "need_frame" to false again and don't
1492 * schedule any more frame-callbacks.
1494 static void schedule_frame(struct wlt_window *wnd)
1498 if (!wnd || wnd->w_frame)
1501 if (wnd->need_frame || wnd->idle_pending)
1504 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1506 log_error("cannot schedule idle callback: %d", ret);
1508 wnd->idle_pending = true;
1511 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1512 uint32_t edges, int32_t width,
1515 struct wlt_window *wnd = data;
1522 wnd->resize_edges = edges;
1523 wlt_window_set_size(wnd, width, height);
1526 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1530 static const struct wl_shell_surface_listener shell_surface_listener = {
1531 .ping = shell_surface_ping,
1532 .configure = shell_surface_configure,
1533 .popup_done = shell_surface_popup_done,
1536 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1538 struct wlt_window *wnd = data;
1540 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
1541 wnd->close_pending = false;
1544 wnd->close_cb(wnd, wnd->data);
1547 int wlt_display_create_window(struct wlt_display *disp,
1548 struct wlt_window **out,
1550 unsigned int height,
1553 struct wlt_window *wnd;
1556 if (!disp || !out || !width || !height)
1559 if (disp->state != STATE_RUNNING) {
1560 log_error("cannot create window, display is not running but in state %u",
1565 wnd = malloc(sizeof(*wnd));
1568 memset(wnd, 0, sizeof(*wnd));
1572 shl_dlist_init(&wnd->widget_list);
1574 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1575 if (!wnd->w_surface) {
1576 log_error("cannot create wayland surface");
1581 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1583 if (!wnd->w_shell_surface) {
1584 log_error("cannot retrieve shell-surface for wayland surface");
1589 wl_shell_surface_add_listener(wnd->w_shell_surface,
1590 &shell_surface_listener, wnd);
1591 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1593 ret = resize_window(wnd, width, height, true);
1595 goto err_shell_surface;
1597 wlt_display_ref(disp);
1598 shl_dlist_link(&disp->window_list, &wnd->list);
1603 wl_shell_surface_destroy(wnd->w_shell_surface);
1605 wl_surface_destroy(wnd->w_surface);
1611 void wlt_window_ref(struct wlt_window *wnd)
1613 if (!wnd || !wnd->ref)
1619 void wlt_window_unref(struct wlt_window *wnd)
1621 struct wlt_widget *widget;
1623 if (!wnd || !wnd->ref || --wnd->ref)
1626 while (!shl_dlist_empty(&wnd->widget_list)) {
1627 widget = shl_dlist_entry(wnd->widget_list.next,
1628 struct wlt_widget, list);
1629 wlt_widget_destroy(widget);
1632 if (wnd->close_pending)
1633 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1635 if (wnd->idle_pending)
1636 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1637 shl_dlist_unlink(&wnd->list);
1639 wl_callback_destroy(wnd->w_frame);
1641 wl_buffer_destroy(wnd->w_buffer);
1643 wlt_pool_free(wnd->pool);
1644 wl_shell_surface_destroy(wnd->w_shell_surface);
1645 wl_surface_destroy(wnd->w_surface);
1646 wlt_display_unref(wnd->disp);
1650 int wlt_window_create_widget(struct wlt_window *wnd,
1651 struct wlt_widget **out,
1654 struct wlt_widget *widget;
1659 widget = malloc(sizeof(*widget));
1662 memset(widget, 0, sizeof(*widget));
1664 widget->data = data;
1666 wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
1667 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1672 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1677 wnd->need_redraw = true;
1678 schedule_frame(wnd);
1681 void wlt_window_damage(struct wlt_window *wnd,
1682 struct wlt_rect *damage)
1684 if (!wnd || !damage || wnd->skip_damage)
1687 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1688 damage->width, damage->height);
1689 wl_surface_commit(wnd->w_surface);
1692 void wlt_window_get_buffer(struct wlt_window *wnd,
1693 const struct wlt_rect *alloc,
1694 struct wlt_shm_buffer *buf)
1696 struct wlt_shm_buffer *rbuf;
1701 rbuf = &wnd->buffer;
1704 memcpy(buf, rbuf, sizeof(*buf));
1708 if (alloc->x >= rbuf->width ||
1709 alloc->y >= rbuf->height) {
1710 memset(buf, 0, sizeof(*buf));
1715 if (alloc->x + alloc->width > rbuf->width)
1716 buf->width = rbuf->width - alloc->x;
1718 buf->width = alloc->width;
1721 if (alloc->y + alloc->height > rbuf->height)
1722 buf->height = rbuf->height - alloc->y;
1724 buf->height = alloc->height;
1727 buf->stride = rbuf->stride;
1730 buf->data = rbuf->data;
1731 buf->data += alloc->y * rbuf->stride;
1732 buf->data += alloc->x * 4;
1735 void wlt_window_move(struct wlt_window *wnd)
1740 wl_shell_surface_move(wnd->w_shell_surface,
1742 wnd->disp->last_serial);
1745 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1750 wl_shell_surface_resize(wnd->w_shell_surface,
1752 wnd->disp->last_serial,
1756 int wlt_window_set_size(struct wlt_window *wnd,
1757 unsigned int width, unsigned int height)
1762 wnd->new_width = width;
1763 wnd->new_height = height;
1764 wnd->need_resize = true;
1765 schedule_frame(wnd);
1770 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1775 set_cursor(wnd->disp, cursor);
1778 void wlt_window_set_close_cb(struct wlt_window *wnd,
1779 wlt_window_close_cb cb)
1787 void wlt_window_close(struct wlt_window *wnd)
1792 wnd->close_pending = true;
1793 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1796 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1801 if (wnd->maximized) {
1802 if (!wnd->fullscreen) {
1803 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1804 wlt_window_set_size(wnd, wnd->saved_width,
1808 if (!wnd->fullscreen) {
1809 wnd->saved_width = wnd->buffer.width;
1810 wnd->saved_height = wnd->buffer.height;
1811 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1816 wnd->maximized = !wnd->maximized;
1819 void wlt_window_toggle_fullscreen(struct wlt_window *wnd)
1824 if (wnd->fullscreen) {
1825 if (wnd->maximized) {
1826 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1829 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1830 wlt_window_set_size(wnd, wnd->saved_width,
1834 if (!wnd->maximized) {
1835 wnd->saved_width = wnd->buffer.width;
1836 wnd->saved_height = wnd->buffer.height;
1838 wl_shell_surface_set_fullscreen(wnd->w_shell_surface,
1839 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1843 wnd->fullscreen = !wnd->fullscreen;
1846 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1851 return wnd->disp->eloop;
1854 struct wlt_display *wlt_window_get_display(struct wlt_window *wnd)
1862 void wlt_widget_destroy(struct wlt_widget *widget)
1867 if (widget->destroy_cb)
1868 widget->destroy_cb(widget, widget->data);
1869 shl_dlist_unlink(&widget->list);
1873 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1881 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1882 wlt_widget_redraw_cb cb)
1887 widget->redraw_cb = cb;
1890 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1891 wlt_widget_destroy_cb cb)
1896 widget->destroy_cb = cb;
1899 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1900 wlt_widget_prepare_resize_cb prepare_cb,
1901 wlt_widget_resize_cb cb)
1906 widget->prepare_resize_cb = prepare_cb;
1907 widget->resize_cb = cb;
1910 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1911 wlt_widget_pointer_enter_cb enter_cb,
1912 wlt_widget_pointer_leave_cb leave_cb,
1913 wlt_widget_pointer_motion_cb motion_cb,
1914 wlt_widget_pointer_button_cb button_cb)
1919 widget->pointer_enter_cb = enter_cb;
1920 widget->pointer_leave_cb = leave_cb;
1921 widget->pointer_motion_cb = motion_cb;
1922 widget->pointer_button_cb = button_cb;
1925 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1926 wlt_widget_keyboard_cb cb)
1931 widget->keyboard_cb = cb;