]> git.mdlowis.com Git - proto/labwc.git/commitdiff
keyboard: Implement key repeat for keybindings
authorJohn Lindgren <john@jlindgren.net>
Wed, 2 Nov 2022 20:37:24 +0000 (16:37 -0400)
committerJohan Malm <johanmalm@users.noreply.github.com>
Wed, 2 Nov 2022 21:52:33 +0000 (21:52 +0000)
It seems that every Wayland client is expected to implement its own
key-repeat logic, rather than doing it server-side as in X11.  This
means that labwc also has to implement its own key-repeat logic for
compositor keybindings.

This is a very simplistic timer-based implementation.  It doesn't
attempt to synthesize accurate timestamps, and may lag depending
on system load, but it appears to get the job done.

v2: Use server->wl_event_loop
v3: Comments and formatting

include/labwc.h
src/keyboard.c
src/seat.c

index 950dd31adc4214192afde6d07f0943e594ce0399..0325bcf4f24dfa369cd302c403a2fe1464c4ef45 100644 (file)
@@ -88,6 +88,10 @@ struct keyboard {
        bool is_virtual;
        struct wl_listener modifier;
        struct wl_listener key;
+       /* key repeat for compositor keybinds */
+       uint32_t keybind_repeat_keycode;
+       int32_t keybind_repeat_rate;
+       struct wl_event_source *keybind_repeat;
 };
 
 struct seat {
@@ -533,6 +537,7 @@ struct view *desktop_focused_view(struct server *server);
 void desktop_focus_topmost_mapped_view(struct server *server);
 bool isfocusable(struct view *view);
 
+void keyboard_cancel_keybind_repeat(struct keyboard *keyboard);
 void keyboard_key_notify(struct wl_listener *listener, void *data);
 void keyboard_modifiers_notify(struct wl_listener *listener, void *data);
 void keyboard_init(struct seat *seat);
index e4fa16e667cb8a8ac59aee156b95b197b5df7513..63bc7016b8d4e22641886b09f73c846aefc20839 100644 (file)
@@ -1,8 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0-only
+#include <assert.h>
 #include <wlr/backend/multi.h>
 #include <wlr/backend/session.h>
 #include "action.h"
-#include "buffer.h"
 #include "key-state.h"
 #include "labwc.h"
 #include "workspaces.h"
@@ -105,10 +105,9 @@ static bool is_modifier_key(xkb_keysym_t sym)
 }
 
 static bool
-handle_compositor_keybindings(struct wl_listener *listener,
+handle_compositor_keybindings(struct keyboard *keyboard,
                struct wlr_keyboard_key_event *event)
 {
-       struct keyboard *keyboard = wl_container_of(listener, keyboard, key);
        struct seat *seat = keyboard->base.seat;
        struct server *server = seat->server;
        struct wlr_keyboard *wlr_keyboard = keyboard->wlr_keyboard;
@@ -231,6 +230,52 @@ out:
        return handled;
 }
 
+static int
+handle_keybind_repeat(void *data)
+{
+       struct keyboard *keyboard = data;
+       assert(keyboard->keybind_repeat);
+
+       /* synthesize event */
+       struct wlr_keyboard_key_event event = {
+               .keycode = keyboard->keybind_repeat_keycode,
+               .state = WL_KEYBOARD_KEY_STATE_PRESSED
+       };
+
+       handle_compositor_keybindings(keyboard, &event);
+       wl_event_source_timer_update(keyboard->keybind_repeat,
+               keyboard->keybind_repeat_rate);
+
+       return 0; /* ignored per wl_event_loop docs */
+}
+
+static void
+start_keybind_repeat(struct server *server, struct keyboard *keyboard,
+               struct wlr_keyboard_key_event *event)
+{
+       struct wlr_keyboard *wlr_keyboard = keyboard->wlr_keyboard;
+       assert(!keyboard->keybind_repeat);
+
+       if (wlr_keyboard->repeat_info.rate > 0
+                       && wlr_keyboard->repeat_info.delay > 0) {
+               keyboard->keybind_repeat_keycode = event->keycode;
+               keyboard->keybind_repeat_rate = wlr_keyboard->repeat_info.rate;
+               keyboard->keybind_repeat = wl_event_loop_add_timer(
+                       server->wl_event_loop, handle_keybind_repeat, keyboard);
+               wl_event_source_timer_update(keyboard->keybind_repeat,
+                       wlr_keyboard->repeat_info.delay);
+       }
+}
+
+void
+keyboard_cancel_keybind_repeat(struct keyboard *keyboard)
+{
+       if (keyboard->keybind_repeat) {
+               wl_event_source_remove(keyboard->keybind_repeat);
+               keyboard->keybind_repeat = NULL;
+       }
+}
+
 void
 keyboard_key_notify(struct wl_listener *listener, void *data)
 {
@@ -242,9 +287,15 @@ keyboard_key_notify(struct wl_listener *listener, void *data)
        struct wlr_keyboard *wlr_keyboard = keyboard->wlr_keyboard;
        wlr_idle_notify_activity(seat->wlr_idle, seat->seat);
 
-       bool handled = handle_compositor_keybindings(listener, event);
+       /* any new press/release cancels current keybind repeat */
+       keyboard_cancel_keybind_repeat(keyboard);
 
-       if (!handled) {
+       bool handled = handle_compositor_keybindings(keyboard, event);
+       if (handled) {
+               if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
+                       start_keybind_repeat(seat->server, keyboard, event);
+               }
+       } else {
                wlr_seat_set_keyboard(wlr_seat, wlr_keyboard);
                wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec,
                        event->keycode, event->state);
index 64ddd87bbf602fc7f5a74b87bfe27b5540e9f3c3..0a099bc2979bff653ddcc48789c7d90852deca70 100644 (file)
@@ -24,6 +24,7 @@ input_device_destroy(struct wl_listener *listener, void *data)
                struct keyboard *keyboard = (struct keyboard *)input;
                wl_list_remove(&keyboard->key.link);
                wl_list_remove(&keyboard->modifier.link);
+               keyboard_cancel_keybind_repeat(keyboard);
        }
        free(input);
 }