From 159eb13341e267c2c6bd06ec9fcf23a0a41c4944 Mon Sep 17 00:00:00 2001 From: Etaash Mathamsetty Date: Thu, 27 Jun 2024 16:35:03 -0400 Subject: [PATCH] wayland: Fix some issues with wayland keybinds --- src/keybinds.h | 4 +++- src/wayland_keybinds.cpp | 34 ++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/keybinds.h b/src/keybinds.h index 62d33ac..4c50570 100644 --- a/src/keybinds.h +++ b/src/keybinds.h @@ -22,8 +22,10 @@ static inline bool keys_are_pressed(const std::vector& keys) { update_wl_queue(); - if(wl_pressed_keys.size() == keys.size() && wl_pressed_keys == keys) + if(wl_pressed_keys == keys) + { return true; + } } #endif diff --git a/src/wayland_keybinds.cpp b/src/wayland_keybinds.cpp index 84372e2..72cb89f 100644 --- a/src/wayland_keybinds.cpp +++ b/src/wayland_keybinds.cpp @@ -19,11 +19,20 @@ struct xkb_state *state_xkb = nullptr; struct wl_event_queue* queue = nullptr; std::vector wl_pressed_keys {}; +static void seat_handle_capabilities(void *data, wl_seat *seat, uint32_t caps); +static void seat_handle_name(void *data, struct wl_seat *seat, const char *name) {} + +struct wl_seat_listener seat_listener { + .capabilities = seat_handle_capabilities, + .name = seat_handle_name, +}; + static void registry_handle_global(void *data, struct wl_registry* registry, uint32_t name, const char *interface, uint32_t version) { if(strcmp(interface, wl_seat_interface.name) == 0) { - seat = (struct wl_seat*)wl_registry_bind(registry, name, &wl_seat_interface, 7); + seat = (struct wl_seat*)wl_registry_bind(registry, name, &wl_seat_interface, 5); + wl_seat_add_listener(seat, &seat_listener, NULL); } } @@ -94,9 +103,21 @@ struct wl_keyboard_listener keyboard_listener { .repeat_info = wl_keyboard_repeat_info }; +static void seat_handle_capabilities(void *data, wl_seat *seat, uint32_t caps) +{ + if(caps & WL_SEAT_CAPABILITY_KEYBOARD) + { + if(!keyboard) + { + keyboard = wl_seat_get_keyboard(seat); + wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL); + } + } +} + void update_wl_queue() { - wl_display_roundtrip_queue(wl_display_ptr, queue); + wl_display_dispatch_queue_pending(wl_display_ptr, queue); } void init_wayland_data() @@ -104,15 +125,12 @@ void init_wayland_data() if (!wl_display_ptr) return; - struct wl_display *display_wrapped = (struct wl_display*)wl_proxy_create_wrapper(wl_display_ptr); queue = wl_display_create_queue(wl_display_ptr); + struct wl_display *display_wrapped = (struct wl_display*)wl_proxy_create_wrapper(wl_display_ptr); wl_proxy_set_queue((struct wl_proxy*)display_wrapped, queue); wl_registry *registry = wl_display_get_registry(display_wrapped); wl_proxy_wrapper_destroy(display_wrapped); wl_registry_add_listener(registry, ®istry_listener, NULL); - update_wl_queue(); - update_wl_queue(); - keyboard = wl_seat_get_keyboard(seat); - wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL); - update_wl_queue(); + wl_display_roundtrip_queue(wl_display_ptr, queue); + wl_display_roundtrip_queue(wl_display_ptr, queue); }