Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
implement repeatable keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
sevz17 committed Dec 26, 2022
1 parent b39d270 commit 0d4c132
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions dwl.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ typedef struct {
struct wl_list link;
struct wlr_keyboard *wlr_keyboard;

int nsyms;
const xkb_keysym_t *keysyms; /* invalid if nsyms == 0 */
uint32_t mods; /* invalid if nsyms == 0 */
struct wl_event_source *key_repeat_source;

struct wl_listener modifiers;
struct wl_listener key;
struct wl_listener destroy;
Expand Down Expand Up @@ -259,6 +264,7 @@ static void inputdevice(struct wl_listener *listener, void *data);
static int keybinding(uint32_t mods, xkb_keysym_t sym);
static void keypress(struct wl_listener *listener, void *data);
static void keypressmod(struct wl_listener *listener, void *data);
static int keyrepeat(void *data);
static void killclient(const Arg *arg);
static void locksession(struct wl_listener *listener, void *data);
static void maplayersurfacenotify(struct wl_listener *listener, void *data);
Expand Down Expand Up @@ -670,6 +676,7 @@ cleanupkeyboard(struct wl_listener *listener, void *data)
{
Keyboard *kb = wl_container_of(listener, kb, destroy);

wl_event_source_remove(kb->key_repeat_source);
wl_list_remove(&kb->link);
wl_list_remove(&kb->modifiers.link);
wl_list_remove(&kb->key.link);
Expand Down Expand Up @@ -817,6 +824,9 @@ createkeyboard(struct wlr_keyboard *keyboard)

wlr_seat_set_keyboard(seat, keyboard);

kb->key_repeat_source = wl_event_loop_add_timer(
wl_display_get_event_loop(dpy), keyrepeat, kb);

/* And add the keyboard to our list of keyboards */
wl_list_insert(&keyboards, &kb->link);
}
Expand Down Expand Up @@ -1399,6 +1409,17 @@ keypress(struct wl_listener *listener, void *data)
for (i = 0; i < nsyms; i++)
handled = keybinding(mods, syms[i]) || handled;

if (handled && kb->wlr_keyboard->repeat_info.delay > 0) {
kb->mods = mods;
kb->keysyms = syms;
kb->nsyms = nsyms;
wl_event_source_timer_update(kb->key_repeat_source,
kb->wlr_keyboard->repeat_info.delay);
} else {
kb->nsyms = 0;
wl_event_source_timer_update(kb->key_repeat_source, 0);
}

if (!handled) {
/* Pass unhandled keycodes along to the client. */
wlr_seat_set_keyboard(seat, kb->wlr_keyboard);
Expand All @@ -1425,6 +1446,22 @@ keypressmod(struct wl_listener *listener, void *data)
&kb->wlr_keyboard->modifiers);
}

int
keyrepeat(void *data)
{
Keyboard *kb = data;
int i;
if (kb->nsyms && kb->wlr_keyboard->repeat_info.rate > 0) {
wl_event_source_timer_update(kb->key_repeat_source,
1000 / kb->wlr_keyboard->repeat_info.rate);

for (i = 0; i < kb->nsyms; i++)
keybinding(kb->mods, kb->keysyms[i]);
}

return 0;
}

void
killclient(const Arg *arg)
{
Expand Down

0 comments on commit 0d4c132

Please sign in to comment.