Skip to content

Commit

Permalink
feat: use keycodes instead of keysyms
Browse files Browse the repository at this point in the history
  • Loading branch information
Frewacom committed Jul 28, 2021
1 parent 6bb5953 commit 7a66f02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
17 changes: 5 additions & 12 deletions dwl.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ typedef struct {

typedef struct {
uint32_t mod;
xkb_keysym_t keysym;
xkb_keycode_t keycode;
scm_t_bits *func;
} Key;

Expand Down Expand Up @@ -241,7 +241,7 @@ static Client *focustop(Monitor *m);
static void getxdecomode(struct wl_listener *listener, void *data);
static void incnmaster(const Arg *arg);
static void inputdevice(struct wl_listener *listener, void *data);
static int keybinding(uint32_t mods, xkb_keysym_t sym);
static int keybinding(uint32_t mods, xkb_keycode_t keycode);
static void keypress(struct wl_listener *listener, void *data);
static void keypressmod(struct wl_listener *listener, void *data);
static void killclient(const Arg *arg);
Expand Down Expand Up @@ -1244,7 +1244,7 @@ inputdevice(struct wl_listener *listener, void *data)
}

int
keybinding(uint32_t mods, xkb_keysym_t sym)
keybinding(uint32_t mods, xkb_keycode_t keycode)
{
/*
* Here we handle compositor keybindings. This is when the compositor is
Expand All @@ -1255,7 +1255,7 @@ keybinding(uint32_t mods, xkb_keysym_t sym)
const Key *k;
for (k = keys; k < (keys + (numkeys * sizeof(Key))); k++) {
if (CLEANMASK(mods) == CLEANMASK(k->mod) &&
sym == k->keysym && k->func) {
keycode == k->keycode && k->func) {
guile_call(GUILE_PROC_ACTION, k->func, NULL);
handled = 1;
}
Expand All @@ -1266,27 +1266,20 @@ keybinding(uint32_t mods, xkb_keysym_t sym)
void
keypress(struct wl_listener *listener, void *data)
{
int i;
/* This event is raised when a key is pressed or released. */
Keyboard *kb = wl_container_of(listener, kb, key);
struct wlr_event_keyboard_key *event = data;

/* Translate libinput keycode -> xkbcommon */
uint32_t keycode = event->keycode + 8;
/* Get a list of keysyms based on the keymap for this keyboard */
const xkb_keysym_t *syms;
int nsyms = xkb_state_key_get_syms(
kb->device->keyboard->xkb_state, keycode, &syms);

int handled = 0;
uint32_t mods = wlr_keyboard_get_modifiers(kb->device->keyboard);

wlr_idle_notify_activity(idle, seat);

/* On _press_, attempt to process a compositor keybinding. */
if (event->state == WLR_KEY_PRESSED)
for (i = 0; i < nsyms; i++)
handled = keybinding(mods, syms[i]) || handled;
handled = keybinding(mods, keycode) || handled;

if (!handled) {
/* Pass unhandled keycodes along to the client. */
Expand Down
12 changes: 6 additions & 6 deletions guile-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ guile_parse_rule(unsigned int index, SCM rule, void *data)
static inline void
guile_parse_key(unsigned int index, SCM key, void *data)
{
char *sym = get_value_string(key, "key");
xkb_keysym_t keysym = xkb_keysym_from_name(sym, 0);
if (keysym == XKB_KEY_NoSymbol)
BARF("error: invalid xkb key '%s'\n", sym);
xkb_keycode_t keycode = get_value_unsigned_int(key, "key", -1);
/* Should we use `xkb_keycode_is_legal_x11`? */
if (!xkb_keycode_is_legal_x11(keycode)
|| !xkb_keycode_is_legal_ext(keycode))
BARF("guile: keycode '%d' is not a legal keycode\n", keycode);
((Key*)data)[index] = (Key){
.mod = get_value_modifiers(key, "modifiers"),
.keysym = keysym,
.keycode = keycode,
.func = get_value_proc_pointer(key, "action")
};
free(sym);
}

static inline void
Expand Down

0 comments on commit 7a66f02

Please sign in to comment.