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;
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);
400 wl_surface_commit(disp->w_cursor_surface);
403 static void dp_dispatch(struct wlt_display *disp, bool nonblock)
409 ret = wl_display_dispatch_pending(disp->dp);
411 ret = wl_display_dispatch(disp->dp);
414 log_error("error during wayland dispatch (%d): %m", errno);
416 } else if (errno == EAGAIN) {
417 ret = ev_fd_update(disp->dp_fd, EV_READABLE | EV_WRITEABLE);
419 log_warning("cannot update wayland-fd event-polling modes (%d)",
422 ret = ev_fd_update(disp->dp_fd, EV_READABLE);
424 log_warning("cannot update wayland-fd event-polling modes (%d)",
429 static void dp_pre_event(struct ev_eloop *eloop, void *unused, void *data)
431 struct wlt_display *disp = data;
433 dp_dispatch(disp, true);
436 static void dp_event(struct ev_fd *fd, int mask, void *data)
438 struct wlt_display *disp = data;
440 if (mask & (EV_HUP | EV_ERR)) {
441 log_warning("HUP/ERR on wayland socket");
442 disp->state = STATE_HUP;
443 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
444 ev_eloop_rm_fd(disp->dp_fd);
449 if (mask & EV_READABLE)
450 dp_dispatch(disp, false);
452 dp_dispatch(disp, true);
455 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
456 uint32_t serial, struct wl_surface *w_surface,
457 wl_fixed_t x, wl_fixed_t y)
459 struct wlt_display *disp = data;
460 struct shl_dlist *iter;
461 struct wlt_window *wnd;
462 struct wlt_widget *widget;
467 shl_dlist_for_each(iter, &disp->window_list) {
468 wnd = shl_dlist_entry(iter, struct wlt_window, list);
469 if (wnd->w_surface == w_surface)
473 if (iter == &disp->window_list) {
474 log_debug("unknown surface");
478 disp->pointer_enter_serial = serial;
479 disp->last_serial = serial;
480 disp->pointer_focus = wnd;
481 shl_dlist_for_each(iter, &wnd->widget_list) {
482 widget = shl_dlist_entry(iter, struct wlt_widget, list);
483 if (widget->pointer_enter_cb)
484 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
489 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
490 uint32_t serial, struct wl_surface *w_surface)
492 struct wlt_display *disp = data;
493 struct shl_dlist *iter;
494 struct wlt_window *wnd;
495 struct wlt_widget *widget;
497 wnd = disp->pointer_focus;
498 disp->pointer_focus = NULL;
499 disp->last_serial = serial;
504 shl_dlist_for_each(iter, &wnd->widget_list) {
505 widget = shl_dlist_entry(iter, struct wlt_widget, list);
506 if (widget->pointer_leave_cb)
507 widget->pointer_leave_cb(widget, widget->data);
511 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
512 uint32_t time, wl_fixed_t x, wl_fixed_t y)
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;
523 shl_dlist_for_each(iter, &wnd->widget_list) {
524 widget = shl_dlist_entry(iter, struct wlt_widget, list);
525 if (widget->pointer_motion_cb)
526 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
531 static void pointer_button(void *data, struct wl_pointer *w_pointer,
532 uint32_t serial, uint32_t time, uint32_t button,
535 struct wlt_display *disp = data;
536 struct shl_dlist *iter;
537 struct wlt_window *wnd;
538 struct wlt_widget *widget;
540 wnd = disp->pointer_focus;
541 disp->last_serial = serial;
546 shl_dlist_for_each(iter, &wnd->widget_list) {
547 widget = shl_dlist_entry(iter, struct wlt_widget, list);
548 if (widget->pointer_button_cb)
549 widget->pointer_button_cb(widget, button, state,
554 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
555 uint32_t time, uint32_t axis, wl_fixed_t value)
559 static const struct wl_pointer_listener pointer_listener = {
560 .enter = pointer_enter,
561 .leave = pointer_leave,
562 .motion = pointer_motion,
563 .button = pointer_button,
564 .axis = pointer_axis,
567 static void keyboard_keymap(void *data, struct wl_keyboard *keyboard,
568 uint32_t format, int fd, uint32_t size)
570 struct wlt_display *disp = data;
573 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
574 log_error("invalid keyboard format");
579 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
580 if (map == MAP_FAILED) {
581 log_error("cannot mmap keyboard keymap");
586 disp->xkb_keymap = xkb_keymap_new_from_string(disp->xkb_ctx, map,
587 XKB_KEYMAP_FORMAT_TEXT_V1,
592 if (!disp->xkb_keymap) {
593 log_error("cannot create xkb keymap");
597 disp->xkb_state = xkb_state_new(disp->xkb_keymap);
598 if (!disp->xkb_state) {
599 log_error("cannot create XKB state object");
600 xkb_keymap_unref(disp->xkb_keymap);
601 disp->xkb_keymap = NULL;
606 static void keyboard_enter(void *data, struct wl_keyboard *keyboard,
607 uint32_t serial, struct wl_surface *w_surface,
608 struct wl_array *keys)
610 struct wlt_display *disp = data;
611 struct shl_dlist *iter;
612 struct wlt_window *wnd;
614 disp->last_serial = serial;
615 if (!disp->xkb_state)
621 shl_dlist_for_each(iter, &disp->window_list) {
622 wnd = shl_dlist_entry(iter, struct wlt_window, list);
623 if (wnd->w_surface == w_surface)
627 if (iter == &disp->window_list)
630 disp->keyboard_focus = wnd;
633 static void keyboard_leave(void *data, struct wl_keyboard *keyboard,
634 uint32_t serial, struct wl_surface *surface)
636 struct wlt_display *disp = data;
638 disp->last_serial = serial;
639 disp->keyboard_focus = NULL;
640 ev_timer_update(disp->repeat_timer, NULL);
643 static void keyboard_key(void *data, struct wl_keyboard *keyboard,
644 uint32_t serial, uint32_t time, uint32_t key,
647 struct wlt_display *disp = data;
648 struct wlt_window *wnd = disp->keyboard_focus;
649 uint32_t code, num_syms, ascii;
651 enum wl_keyboard_key_state state = state_w;
652 const xkb_keysym_t *syms;
654 struct shl_dlist *iter;
655 struct wlt_widget *widget;
656 struct itimerspec spec;
659 disp->last_serial = serial;
660 if (!disp->xkb_state)
667 mask = shl_get_xkb_mods(disp->xkb_state);
668 num_syms = xkb_state_key_get_syms(disp->xkb_state, code, &syms);
669 ascii = shl_get_ascii(disp->xkb_state, code, syms, num_syms);
670 sym = XKB_KEY_NoSymbol;
675 shl_dlist_for_each(iter, &wnd->widget_list) {
676 widget = shl_dlist_entry(iter, struct wlt_widget, list);
677 if (widget->keyboard_cb) {
678 if (widget->keyboard_cb(widget, mask, sym, ascii, state,
679 handled, widget->data))
684 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
685 sym == disp->repeat_sym) {
686 ev_timer_update(disp->repeat_timer, NULL);
687 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
688 disp->repeat_sym = sym;
689 disp->repeat_ascii = ascii;
690 spec.it_interval.tv_sec = 0;
691 spec.it_interval.tv_nsec = wlt_conf.xkb_repeat_rate * 1000000;
692 spec.it_value.tv_sec = 0;
693 spec.it_value.tv_nsec = wlt_conf.xkb_repeat_delay * 1000000;
694 ev_timer_update(disp->repeat_timer, &spec);
698 static void repeat_event(struct ev_timer *timer, uint64_t num, void *data)
700 struct wlt_display *disp = data;
701 struct wlt_window *wnd = disp->keyboard_focus;
702 struct wlt_widget *widget;
703 struct shl_dlist *iter;
710 mask = shl_get_xkb_mods(disp->xkb_state);
712 shl_dlist_for_each(iter, &wnd->widget_list) {
713 widget = shl_dlist_entry(iter, struct wlt_widget, list);
714 if (widget->keyboard_cb) {
715 if (widget->keyboard_cb(widget, mask, disp->repeat_sym,
717 WL_KEYBOARD_KEY_STATE_PRESSED,
718 handled, widget->data))
724 static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
725 uint32_t serial, uint32_t mods_depressed,
726 uint32_t mods_latched, uint32_t mods_locked,
729 struct wlt_display *disp = data;
731 disp->last_serial = serial;
732 if (!disp->xkb_state)
735 xkb_state_update_mask(disp->xkb_state, mods_depressed, mods_latched,
736 mods_locked, 0, 0, group);
739 static const struct wl_keyboard_listener keyboard_listener = {
740 .keymap = keyboard_keymap,
741 .enter = keyboard_enter,
742 .leave = keyboard_leave,
744 .modifiers = keyboard_modifiers,
747 static void data_offer_unref(struct data_offer *offer)
751 if (!offer || !offer->ref || --offer->ref)
754 len = shl_array_get_length(offer->types);
755 for (i = 0; i < len; ++i)
756 free(*SHL_ARRAY_AT(offer->types, char*, i));
757 shl_array_free(offer->types);
758 wl_data_offer_destroy(offer->w_offer);
762 static void data_offer_offer(void *data, struct wl_data_offer *w_offer,
767 struct data_offer *offer = wl_data_offer_get_user_data(w_offer);
773 ret = shl_array_push(offer->types, &tmp);
780 static const struct wl_data_offer_listener data_offer_listener = {
781 .offer = data_offer_offer,
784 static void data_dev_data_offer(void *data, struct wl_data_device *w_dev,
785 struct wl_data_offer *w_offer)
787 struct data_offer *offer;
788 struct wlt_display *disp = data;
791 offer = malloc(sizeof(*offer));
793 wl_data_offer_destroy(w_offer);
796 memset(offer, 0, sizeof(*offer));
798 offer->w_offer = w_offer;
801 ret = shl_array_new(&offer->types, sizeof(char*), 4);
803 wl_data_offer_destroy(w_offer);
808 wl_data_offer_add_listener(w_offer, &data_offer_listener, offer);
809 wl_data_offer_set_user_data(w_offer, offer);
812 static void data_dev_enter(void *data, struct wl_data_device *w_dev,
813 uint32_t serial, struct wl_surface *w_surface,
814 wl_fixed_t x, wl_fixed_t y,
815 struct wl_data_offer *w_offer)
817 struct wlt_display *disp = data;
819 if (disp->drag_offer) {
820 data_offer_unref(disp->drag_offer);
821 disp->drag_offer = NULL;
827 disp->drag_offer = wl_data_offer_get_user_data(w_offer);
830 static void data_dev_leave(void *data, struct wl_data_device *w_dev)
832 struct wlt_display *disp = data;
834 if (disp->drag_offer) {
835 data_offer_unref(disp->drag_offer);
836 disp->drag_offer = NULL;
840 static void data_dev_motion(void *data, struct wl_data_device *w_dev,
841 uint32_t time, wl_fixed_t x, wl_fixed_t y)
845 static void data_dev_drop(void *data, struct wl_data_device *w_dev)
849 static void data_dev_selection(void *data, struct wl_data_device *w_dev,
850 struct wl_data_offer *w_offer)
852 struct wlt_display *disp = data;
854 if (disp->selection_offer) {
855 data_offer_unref(disp->selection_offer);
856 disp->selection_offer = NULL;
862 disp->selection_offer = wl_data_offer_get_user_data(w_offer);
865 int wlt_display_get_selection_to_fd(struct wlt_display *disp, const char *mime,
869 struct data_offer *offer;
873 if (!disp->selection_offer)
876 offer = disp->selection_offer;
877 num = shl_array_get_length(offer->types);
878 for (i = 0; i < num; ++i) {
879 if (!strcmp(mime, *SHL_ARRAY_AT(offer->types, char*, i)))
886 wl_data_offer_receive(offer->w_offer, mime, output_fd);
890 int wlt_display_get_selection_fd(struct wlt_display *disp, const char *mime)
894 if (pipe2(p, O_CLOEXEC | O_NONBLOCK))
897 ret = wlt_display_get_selection_to_fd(disp, mime, p[1]);
908 int wlt_display_new_data_source(struct wlt_display *disp,
909 struct wl_data_source **out)
911 struct wl_data_source *src;
916 src = wl_data_device_manager_create_data_source(disp->w_manager);
924 void wlt_display_set_selection(struct wlt_display *disp,
925 struct wl_data_source *selection)
930 wl_data_device_set_selection(disp->w_data_dev, selection,
934 static const struct wl_data_device_listener data_dev_listener = {
935 .data_offer = data_dev_data_offer,
936 .enter = data_dev_enter,
937 .leave = data_dev_leave,
938 .motion = data_dev_motion,
939 .drop = data_dev_drop,
940 .selection = data_dev_selection,
943 static void check_ready(struct wlt_display *disp)
945 if (disp->state > STATE_INIT)
955 log_debug("wayland display initialized");
958 disp->w_data_dev = wl_data_device_manager_get_data_device(
959 disp->w_manager, disp->w_seat);
960 wl_data_device_add_listener(disp->w_data_dev,
964 disp->state = STATE_RUNNING;
965 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
969 static void seat_capabilities(void *data, struct wl_seat *seat,
970 enum wl_seat_capability caps)
972 struct wlt_display *disp = data;
974 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
975 disp->w_pointer = wl_seat_get_pointer(seat);
976 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
980 if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !disp->w_keyboard) {
981 disp->w_keyboard = wl_seat_get_keyboard(seat);
982 wl_keyboard_add_listener(disp->w_keyboard, &keyboard_listener,
989 static const struct wl_seat_listener seat_listener = {
990 .capabilities = seat_capabilities,
993 static void dp_global(void *data, struct wl_registry *registry, uint32_t id,
994 const char *iface, uint32_t version)
996 struct wlt_display *disp = data;
998 if (!strcmp(iface, "wl_display")) {
999 log_debug("new wl_display global");
1001 } else if (!strcmp(iface, "wl_compositor")) {
1003 log_error("global wl_compositor advertised twice");
1006 disp->w_comp = wl_registry_bind(disp->w_registry,
1008 &wl_compositor_interface,
1010 if (!disp->w_comp) {
1011 log_error("cannot bind wl_compositor object");
1014 } else if (!strcmp(iface, "wl_seat")) {
1016 log_error("global wl_seat advertised twice");
1019 disp->w_seat = wl_registry_bind(disp->w_registry,
1023 if (!disp->w_seat) {
1024 log_error("cannot bind wl_seat object");
1028 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
1029 } else if (!strcmp(iface, "wl_shell")) {
1030 if (disp->w_shell) {
1031 log_error("global wl_shell advertised twice");
1034 disp->w_shell = wl_registry_bind(disp->w_registry,
1036 &wl_shell_interface,
1038 if (!disp->w_shell) {
1039 log_error("cannot bind wl_shell object");
1042 } else if (!strcmp(iface, "wl_shm")) {
1044 log_error("global wl_shm advertised twice");
1047 disp->w_shm = wl_registry_bind(disp->w_registry,
1052 log_error("cannot bind wl_shm object");
1055 } else if (!strcmp(iface, "wl_data_device_manager")) {
1056 if (disp->w_manager) {
1057 log_error("global wl_data_device_manager advertised twice");
1060 disp->w_manager = wl_registry_bind(disp->w_registry,
1062 &wl_data_device_manager_interface,
1064 if (!disp->w_manager) {
1065 log_error("cannot bind wl_data_device_manager_object");
1069 log_debug("ignoring new unknown global %s", iface);
1073 log_debug("new global %s", iface);
1078 static const struct wl_registry_listener registry_listener = {
1079 .global = dp_global,
1080 /* TODO: handle .global_remove */
1083 int wlt_display_new(struct wlt_display **out,
1084 struct ev_eloop *eloop)
1086 struct wlt_display *disp;
1092 log_debug("creating new wlt-display");
1094 disp = malloc(sizeof(*disp));
1097 memset(disp, 0, sizeof(*disp));
1099 disp->eloop = eloop;
1100 shl_dlist_init(&disp->window_list);
1102 ret = shl_hook_new(&disp->listeners);
1104 log_error("cannot create listeners hook");
1108 disp->dp = wl_display_connect(NULL);
1110 log_error("cannot connect to wayland socket (%d): %m", errno);
1115 fd = wl_display_get_fd(disp->dp);
1117 log_error("cannot retrieve wayland FD");
1122 /* TODO: nonblocking wl_display doesn't work, yet. fix that upstream */
1125 set = fcntl(fd, F_GETFL);
1127 log_error("cannot retrieve file flags for wayland FD (%d): %m",
1134 ret = fcntl(fd, F_SETFL, set);
1136 log_error("cannot set file flags for wayland FD (%d): %m",
1142 ret = ev_eloop_new_fd(eloop,
1149 log_error("cannot create event-fd for wayland display (%d)",
1154 ret = ev_eloop_register_pre_cb(eloop, dp_pre_event, disp);
1156 log_error("cannot register pre-cb (%d)", ret);
1160 ret = ev_eloop_new_timer(eloop, &disp->repeat_timer, NULL,
1161 repeat_event, disp);
1163 log_error("cannot create repeat-timer");
1167 disp->w_registry = wl_display_get_registry(disp->dp);
1168 if (!disp->w_registry) {
1169 log_error("cannot get wayland registry object");
1173 wl_registry_add_listener(disp->w_registry, ®istry_listener, disp);
1175 disp->xkb_ctx = xkb_context_new(0);
1176 if (!disp->xkb_ctx) {
1177 log_error("cannot create XKB context");
1181 log_debug("wlt-display waiting for globals...");
1183 ev_eloop_ref(disp->eloop);
1188 ev_eloop_rm_timer(disp->repeat_timer);
1190 ev_eloop_unregister_pre_cb(disp->eloop, dp_pre_event, disp);
1192 ev_eloop_rm_fd(disp->dp_fd);
1194 wl_display_disconnect(disp->dp);
1196 shl_hook_free(disp->listeners);
1202 void wlt_display_ref(struct wlt_display *disp)
1204 if (!disp || !disp->ref)
1210 void wlt_display_unref(struct wlt_display *disp)
1212 if (!disp || !disp->ref || --disp->ref)
1215 unload_cursors(disp);
1216 wl_display_flush(disp->dp);
1217 wl_display_disconnect(disp->dp);
1218 ev_eloop_rm_timer(disp->repeat_timer);
1219 ev_eloop_unregister_pre_cb(disp->eloop, dp_pre_event, disp);
1220 ev_eloop_rm_fd(disp->dp_fd);
1221 xkb_context_unref(disp->xkb_ctx);
1222 shl_hook_free(disp->listeners);
1223 ev_eloop_unref(disp->eloop);
1227 int wlt_display_register_cb(struct wlt_display *disp,
1228 wlt_display_cb cb, void *data)
1233 return shl_hook_add_cast(disp->listeners, cb, data, false);
1236 void wlt_display_unregister_cb(struct wlt_display *disp,
1237 wlt_display_cb cb, void *data)
1242 shl_hook_rm_cast(disp->listeners, cb, data);
1245 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
1248 wl_shell_surface_pong(s, serial);
1251 static void wlt_window_do_redraw(struct wlt_window *wnd,
1255 struct shl_dlist *iter;
1256 struct wlt_widget *widget;
1257 unsigned int x, y, flags;
1258 struct wlt_rect alloc;
1262 flags |= WLT_WINDOW_MAXIMIZED;
1263 if (wnd->fullscreen)
1264 flags |= WLT_WINDOW_FULLSCREEN;
1268 alloc.width = wnd->buffer.width;
1269 alloc.height = wnd->buffer.height;
1270 shl_dlist_for_each(iter, &wnd->widget_list) {
1271 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1272 if (widget->resize_cb)
1273 widget->resize_cb(widget, flags, &alloc, widget->data);
1276 memset(wnd->buffer.data, 0,
1277 wnd->buffer.stride * wnd->buffer.height);
1279 wnd->skip_damage = true;
1280 shl_dlist_for_each(iter, &wnd->widget_list) {
1281 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1282 if (widget->redraw_cb)
1283 widget->redraw_cb(widget, flags, widget->data);
1285 wnd->skip_damage = false;
1287 if (!wnd->buffer_attached) {
1288 wnd->buffer_attached = true;
1289 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
1290 x = (int)oldw - wnd->buffer.width;
1293 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
1294 y = (int)oldh - wnd->buffer.height;
1297 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
1298 wnd->resize_edges = 0;
1301 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
1302 wnd->buffer.height);
1303 wl_surface_commit(wnd->w_surface);
1306 static int resize_window(struct wlt_window *wnd, unsigned int width,
1307 unsigned int height)
1309 struct shl_dlist *iter;
1310 struct wlt_widget *widget;
1311 struct wl_buffer *old_buffer = NULL;
1312 struct wlt_pool *old_pool = NULL;
1315 unsigned int oldw, oldh, neww, newh, minw, minh, flags;
1318 width = wnd->buffer.width;
1320 height = wnd->buffer.height;
1322 if (!width || !height)
1327 flags |= WLT_WINDOW_MAXIMIZED;
1328 if (wnd->fullscreen)
1329 flags |= WLT_WINDOW_FULLSCREEN;
1335 shl_dlist_for_each(iter, &wnd->widget_list) {
1336 widget = shl_dlist_entry(iter, struct wlt_widget, list);
1337 if (widget->prepare_resize_cb)
1338 widget->prepare_resize_cb(widget,
1351 if (width == wnd->buffer.width &&
1352 height == wnd->buffer.height) {
1353 wlt_window_do_redraw(wnd, width, height);
1357 oldw = wnd->buffer.width;
1358 oldh = wnd->buffer.height;
1360 nsize = width * height * 4;
1361 if (wnd->pool && wnd->pool->size >= nsize) {
1362 old_buffer = wnd->w_buffer;
1363 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1366 WL_SHM_FORMAT_ARGB8888);
1367 if (!wnd->w_buffer) {
1368 log_error("cannot create wayland shm buffer");
1369 wnd->w_buffer = old_buffer;
1373 old_pool = wnd->pool;
1374 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
1376 log_error("cannot create memory pool");
1377 wnd->pool = old_pool;
1381 old_buffer = wnd->w_buffer;
1382 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
1385 WL_SHM_FORMAT_ARGB8888);
1386 if (!wnd->w_buffer) {
1387 log_error("cannot create wayland shm buffer");
1388 wnd->w_buffer = old_buffer;
1389 wlt_pool_free(wnd->pool);
1390 wnd->pool = old_pool;
1395 wnd->buffer.data = wnd->pool->data;
1396 wnd->buffer.width = width;
1397 wnd->buffer.height = height;
1398 wnd->buffer.stride = width * 4;
1399 wnd->buffer_attached = false;
1401 wlt_window_do_redraw(wnd, oldw, oldh);
1404 wl_buffer_destroy(old_buffer);
1406 wlt_pool_free(old_pool);
1411 static void frame_callback(void *data, struct wl_callback *w_callback,
1413 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
1415 static const struct wl_callback_listener frame_callback_listener = {
1416 .done = frame_callback,
1419 static void do_frame(struct wlt_window *wnd)
1421 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd,
1424 if (wnd->need_resize) {
1425 wnd->need_frame = true;
1426 wnd->need_resize = false;
1427 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1428 wl_callback_add_listener(wnd->w_frame,
1429 &frame_callback_listener, wnd);
1430 resize_window(wnd, wnd->new_width, wnd->new_height);
1432 wnd->need_frame = true;
1433 wnd->w_frame = wl_surface_frame(wnd->w_surface);
1434 wl_callback_add_listener(wnd->w_frame,
1435 &frame_callback_listener, wnd);
1436 wlt_window_do_redraw(wnd, wnd->buffer.width,
1437 wnd->buffer.height);
1441 static void schedule_frame(struct wlt_window *wnd);
1443 static void frame_callback(void *data, struct wl_callback *w_callback,
1446 struct wlt_window *wnd = data;
1448 wl_callback_destroy(w_callback);
1449 wnd->w_frame = NULL;
1451 wnd->idle_pending = false;
1452 if (wnd->need_frame)
1453 schedule_frame(wnd);
1456 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
1458 struct wlt_window *wnd = data;
1464 * Buffer Handling and Frame Scheduling
1465 * We use wl_shm for buffer allocation. This means, we have a single buffer
1466 * client side and the server loads it into its backbuffer for rendering. If the
1467 * server does not do this, we are screwed anyway, but that's on behalf of the
1468 * server, so we don't care.
1470 * However, this means, when we create a buffer, we need to notify the
1471 * compositor and then wait until the compositor has created its back-buffer,
1472 * before we continue using this buffer. We can use the "frame" callback to get
1473 * notified about this.
1475 * The logic we have is:
1476 * You set the boolean flags what action is needed in wlt_window and then call
1477 * "schedule_frame()". If we didn't already do any buffer operations in this
1478 * frame, then this function schedules an idle-callback which then performs
1479 * the requested functions (flags in wlt_window). Afterwards, it sets a marker
1480 * that this frame was already used and schedules a frame-callback.
1481 * If during this time another call to schedule_frame() is made, we do nothing
1482 * but wait for the frame-callback. It will then directly perform all the
1483 * requested functions and reschedule a frame-callback.
1484 * If nothing was schedule for one frame, we have no more interest in
1485 * frame-callbacks and thus we set "need_frame" to false again and don't
1486 * schedule any more frame-callbacks.
1488 static void schedule_frame(struct wlt_window *wnd)
1495 wnd->need_frame = true;
1497 if (wnd->idle_pending)
1500 ret = ev_eloop_register_idle_cb(wnd->disp->eloop, idle_frame, wnd,
1503 log_error("cannot schedule idle callback: %d", ret);
1505 wnd->idle_pending = true;
1508 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
1509 uint32_t edges, int32_t width,
1512 struct wlt_window *wnd = data;
1519 wnd->resize_edges = edges;
1520 wlt_window_set_size(wnd, width, height);
1523 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
1527 static const struct wl_shell_surface_listener shell_surface_listener = {
1528 .ping = shell_surface_ping,
1529 .configure = shell_surface_configure,
1530 .popup_done = shell_surface_popup_done,
1533 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
1535 struct wlt_window *wnd = data;
1537 ev_eloop_unregister_idle_cb(eloop, close_window, wnd, EV_NORMAL);
1538 wnd->close_pending = false;
1541 wnd->close_cb(wnd, wnd->data);
1544 int wlt_display_create_window(struct wlt_display *disp,
1545 struct wlt_window **out,
1547 unsigned int height,
1550 struct wlt_window *wnd;
1553 if (!disp || !out || !width || !height)
1556 if (disp->state != STATE_RUNNING) {
1557 log_error("cannot create window, display is not running but in state %u",
1562 wnd = malloc(sizeof(*wnd));
1565 memset(wnd, 0, sizeof(*wnd));
1569 shl_dlist_init(&wnd->widget_list);
1571 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1572 if (!wnd->w_surface) {
1573 log_error("cannot create wayland surface");
1578 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1580 if (!wnd->w_shell_surface) {
1581 log_error("cannot retrieve shell-surface for wayland surface");
1586 wl_shell_surface_add_listener(wnd->w_shell_surface,
1587 &shell_surface_listener, wnd);
1588 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1590 ret = resize_window(wnd, width, height);
1592 goto err_shell_surface;
1594 wlt_display_ref(disp);
1595 shl_dlist_link(&disp->window_list, &wnd->list);
1600 wl_shell_surface_destroy(wnd->w_shell_surface);
1602 wl_surface_destroy(wnd->w_surface);
1608 void wlt_window_ref(struct wlt_window *wnd)
1610 if (!wnd || !wnd->ref)
1616 void wlt_window_unref(struct wlt_window *wnd)
1618 struct wlt_widget *widget;
1620 if (!wnd || !wnd->ref || --wnd->ref)
1623 while (!shl_dlist_empty(&wnd->widget_list)) {
1624 widget = shl_dlist_entry(wnd->widget_list.next,
1625 struct wlt_widget, list);
1626 wlt_widget_destroy(widget);
1629 if (wnd->close_pending)
1630 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1632 if (wnd->idle_pending)
1633 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd,
1635 shl_dlist_unlink(&wnd->list);
1637 wl_callback_destroy(wnd->w_frame);
1639 wl_buffer_destroy(wnd->w_buffer);
1641 wlt_pool_free(wnd->pool);
1642 wl_shell_surface_destroy(wnd->w_shell_surface);
1643 wl_surface_destroy(wnd->w_surface);
1644 wlt_display_unref(wnd->disp);
1648 int wlt_window_create_widget(struct wlt_window *wnd,
1649 struct wlt_widget **out,
1652 struct wlt_widget *widget;
1657 widget = malloc(sizeof(*widget));
1660 memset(widget, 0, sizeof(*widget));
1662 widget->data = data;
1664 wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
1665 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1670 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1675 schedule_frame(wnd);
1678 void wlt_window_damage(struct wlt_window *wnd,
1679 struct wlt_rect *damage)
1681 if (!wnd || !damage || wnd->skip_damage)
1684 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1685 damage->width, damage->height);
1686 wl_surface_commit(wnd->w_surface);
1689 void wlt_window_get_buffer(struct wlt_window *wnd,
1690 const struct wlt_rect *alloc,
1691 struct wlt_shm_buffer *buf)
1693 struct wlt_shm_buffer *rbuf;
1698 rbuf = &wnd->buffer;
1701 memcpy(buf, rbuf, sizeof(*buf));
1705 if (alloc->x >= rbuf->width ||
1706 alloc->y >= rbuf->height) {
1707 memset(buf, 0, sizeof(*buf));
1712 if (alloc->x + alloc->width > rbuf->width)
1713 buf->width = rbuf->width - alloc->x;
1715 buf->width = alloc->width;
1718 if (alloc->y + alloc->height > rbuf->height)
1719 buf->height = rbuf->height - alloc->y;
1721 buf->height = alloc->height;
1724 buf->stride = rbuf->stride;
1727 buf->data = rbuf->data;
1728 buf->data += alloc->y * rbuf->stride;
1729 buf->data += alloc->x * 4;
1732 void wlt_window_move(struct wlt_window *wnd)
1737 wl_shell_surface_move(wnd->w_shell_surface,
1739 wnd->disp->last_serial);
1742 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1747 wl_shell_surface_resize(wnd->w_shell_surface,
1749 wnd->disp->last_serial,
1753 int wlt_window_set_size(struct wlt_window *wnd,
1754 unsigned int width, unsigned int height)
1759 wnd->new_width = width;
1760 wnd->new_height = height;
1761 wnd->need_resize = true;
1762 schedule_frame(wnd);
1767 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1772 set_cursor(wnd->disp, cursor);
1775 void wlt_window_set_close_cb(struct wlt_window *wnd,
1776 wlt_window_close_cb cb)
1784 void wlt_window_close(struct wlt_window *wnd)
1789 wnd->close_pending = true;
1790 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd,
1794 void wlt_window_toggle_maximize(struct wlt_window *wnd)
1799 if (wnd->maximized) {
1800 if (!wnd->fullscreen) {
1801 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1802 wlt_window_set_size(wnd, wnd->saved_width,
1806 if (!wnd->fullscreen) {
1807 wnd->saved_width = wnd->buffer.width;
1808 wnd->saved_height = wnd->buffer.height;
1809 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1814 wnd->maximized = !wnd->maximized;
1817 void wlt_window_toggle_fullscreen(struct wlt_window *wnd)
1822 if (wnd->fullscreen) {
1823 if (wnd->maximized) {
1824 wl_shell_surface_set_maximized(wnd->w_shell_surface,
1827 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1828 wlt_window_set_size(wnd, wnd->saved_width,
1832 if (!wnd->maximized) {
1833 wnd->saved_width = wnd->buffer.width;
1834 wnd->saved_height = wnd->buffer.height;
1836 wl_shell_surface_set_fullscreen(wnd->w_shell_surface,
1837 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1841 wnd->fullscreen = !wnd->fullscreen;
1844 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1849 return wnd->disp->eloop;
1852 struct wlt_display *wlt_window_get_display(struct wlt_window *wnd)
1860 void wlt_widget_destroy(struct wlt_widget *widget)
1865 if (widget->destroy_cb)
1866 widget->destroy_cb(widget, widget->data);
1867 shl_dlist_unlink(&widget->list);
1871 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1879 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1880 wlt_widget_redraw_cb cb)
1885 widget->redraw_cb = cb;
1888 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1889 wlt_widget_destroy_cb cb)
1894 widget->destroy_cb = cb;
1897 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1898 wlt_widget_prepare_resize_cb prepare_cb,
1899 wlt_widget_resize_cb cb)
1904 widget->prepare_resize_cb = prepare_cb;
1905 widget->resize_cb = cb;
1908 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1909 wlt_widget_pointer_enter_cb enter_cb,
1910 wlt_widget_pointer_leave_cb leave_cb,
1911 wlt_widget_pointer_motion_cb motion_cb,
1912 wlt_widget_pointer_button_cb button_cb)
1917 widget->pointer_enter_cb = enter_cb;
1918 widget->pointer_leave_cb = leave_cb;
1919 widget->pointer_motion_cb = motion_cb;
1920 widget->pointer_button_cb = button_cb;
1923 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
1924 wlt_widget_keyboard_cb cb)
1929 widget->keyboard_cb = cb;