Implement key repeat

This commit is contained in:
Martin Dørum 2020-06-19 22:02:23 +01:00 committed by GitHub
parent a6b3a0956b
commit a99afe6a70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 9 deletions

View File

@ -1,6 +1,11 @@
#ifndef _SWAYLOCK_SEAT_H #ifndef _SWAYLOCK_SEAT_H
#define _SWAYLOCK_SEAT_H #define _SWAYLOCK_SEAT_H
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include <stdint.h>
#include <stdbool.h>
struct loop;
struct loop_timer;
struct swaylock_xkb { struct swaylock_xkb {
bool caps_lock; bool caps_lock;
@ -14,6 +19,11 @@ struct swaylock_seat {
struct swaylock_state *state; struct swaylock_state *state;
struct wl_pointer *pointer; struct wl_pointer *pointer;
struct wl_keyboard *keyboard; struct wl_keyboard *keyboard;
int32_t repeat_period_ms;
int32_t repeat_delay_ms;
uint32_t repeat_sym;
uint32_t repeat_codepoint;
struct loop_timer *repeat_timer;
}; };
extern const struct wl_seat_listener seat_listener; extern const struct wl_seat_listener seat_listener;

13
loop.c
View File

@ -21,6 +21,7 @@ struct loop_timer {
void (*callback)(void *data); void (*callback)(void *data);
void *data; void *data;
struct timespec expiry; struct timespec expiry;
bool removed;
struct wl_list link; // struct loop_timer::link struct wl_list link; // struct loop_timer::link
}; };
@ -104,12 +105,19 @@ void loop_poll(struct loop *loop) {
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
struct loop_timer *timer = NULL, *tmp_timer = NULL; struct loop_timer *timer = NULL, *tmp_timer = NULL;
wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) { wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
if (timer->removed) {
wl_list_remove(&timer->link);
free(timer);
continue;
}
bool expired = timer->expiry.tv_sec < now.tv_sec || bool expired = timer->expiry.tv_sec < now.tv_sec ||
(timer->expiry.tv_sec == now.tv_sec && (timer->expiry.tv_sec == now.tv_sec &&
timer->expiry.tv_nsec < now.tv_nsec); timer->expiry.tv_nsec < now.tv_nsec);
if (expired) { if (expired) {
timer->callback(timer->data); timer->callback(timer->data);
loop_remove_timer(loop, timer); wl_list_remove(&timer->link);
free(timer);
} }
} }
} }
@ -184,8 +192,7 @@ bool loop_remove_timer(struct loop *loop, struct loop_timer *remove) {
struct loop_timer *timer = NULL, *tmp_timer = NULL; struct loop_timer *timer = NULL, *tmp_timer = NULL;
wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) { wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
if (timer == remove) { if (timer == remove) {
wl_list_remove(&timer->link); timer->removed = true;
free(timer);
return true; return true;
} }
} }

2
main.c
View File

@ -314,7 +314,7 @@ static void handle_global(void *data, struct wl_registry *registry,
&wl_shm_interface, 1); &wl_shm_interface, 1);
} else if (strcmp(interface, wl_seat_interface.name) == 0) { } else if (strcmp(interface, wl_seat_interface.name) == 0) {
struct wl_seat *seat = wl_registry_bind( struct wl_seat *seat = wl_registry_bind(
registry, name, &wl_seat_interface, 3); registry, name, &wl_seat_interface, 4);
struct swaylock_seat *swaylock_seat = struct swaylock_seat *swaylock_seat =
calloc(1, sizeof(struct swaylock_seat)); calloc(1, sizeof(struct swaylock_seat));
swaylock_seat->state = state; swaylock_seat->state = state;

41
seat.c
View File

@ -6,10 +6,12 @@
#include "log.h" #include "log.h"
#include "swaylock.h" #include "swaylock.h"
#include "seat.h" #include "seat.h"
#include "loop.h"
static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
uint32_t format, int32_t fd, uint32_t size) { uint32_t format, int32_t fd, uint32_t size) {
struct swaylock_state *state = data; struct swaylock_seat *seat = data;
struct swaylock_state *state = seat->state;
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd); close(fd);
swaylock_log(LOG_ERROR, "Unknown keymap format %d, aborting", format); swaylock_log(LOG_ERROR, "Unknown keymap format %d, aborting", format);
@ -45,9 +47,18 @@ static void keyboard_leave(void *data, struct wl_keyboard *wl_keyboard,
// Who cares // Who cares
} }
static void keyboard_repeat(void *data) {
struct swaylock_seat *seat = data;
struct swaylock_state *state = seat->state;
seat->repeat_timer = loop_add_timer(
state->eventloop, seat->repeat_period_ms, keyboard_repeat, seat);
swaylock_handle_key(state, seat->repeat_sym, seat->repeat_codepoint);
}
static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard, static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard,
uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) { uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) {
struct swaylock_state *state = data; struct swaylock_seat *seat = data;
struct swaylock_state *state = seat->state;
enum wl_keyboard_key_state key_state = _key_state; enum wl_keyboard_key_state key_state = _key_state;
xkb_keysym_t sym = xkb_state_key_get_one_sym(state->xkb.state, key + 8); xkb_keysym_t sym = xkb_state_key_get_one_sym(state->xkb.state, key + 8);
uint32_t keycode = key_state == WL_KEYBOARD_KEY_STATE_PRESSED ? uint32_t keycode = key_state == WL_KEYBOARD_KEY_STATE_PRESSED ?
@ -56,12 +67,25 @@ static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard,
if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED) { if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED) {
swaylock_handle_key(state, sym, codepoint); swaylock_handle_key(state, sym, codepoint);
} }
if (seat->repeat_timer) {
loop_remove_timer(seat->state->eventloop, seat->repeat_timer);
seat->repeat_timer = NULL;
}
if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED && seat->repeat_period_ms > 0) {
seat->repeat_sym = sym;
seat->repeat_codepoint = codepoint;
seat->repeat_timer = loop_add_timer(
seat->state->eventloop, seat->repeat_delay_ms, keyboard_repeat, seat);
}
} }
static void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, static void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard,
uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group) { uint32_t mods_locked, uint32_t group) {
struct swaylock_state *state = data; struct swaylock_seat *seat = data;
struct swaylock_state *state = seat->state;
int layout_same = xkb_state_layout_index_is_active(state->xkb.state, int layout_same = xkb_state_layout_index_is_active(state->xkb.state,
group, XKB_STATE_LAYOUT_EFFECTIVE); group, XKB_STATE_LAYOUT_EFFECTIVE);
if (!layout_same) { if (!layout_same) {
@ -82,7 +106,14 @@ static void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard,
static void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, static void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
int32_t rate, int32_t delay) { int32_t rate, int32_t delay) {
// TODO struct swaylock_seat *seat = data;
if (rate <= 0) {
seat->repeat_period_ms = -1;
} else {
// Keys per second -> milliseconds between keys
seat->repeat_period_ms = 1000 / rate;
}
seat->repeat_delay_ms = delay;
} }
static const struct wl_keyboard_listener keyboard_listener = { static const struct wl_keyboard_listener keyboard_listener = {
@ -168,7 +199,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
} }
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
seat->keyboard = wl_seat_get_keyboard(wl_seat); seat->keyboard = wl_seat_get_keyboard(wl_seat);
wl_keyboard_add_listener(seat->keyboard, &keyboard_listener, seat->state); wl_keyboard_add_listener(seat->keyboard, &keyboard_listener, seat);
} }
} }