Skip to content

Commit

Permalink
apply grab-all-keycodes patch
Browse files Browse the repository at this point in the history
  • Loading branch information
dylantjb committed Oct 6, 2021
1 parent 340aebf commit 3feb4ee
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
26 changes: 20 additions & 6 deletions dwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,14 +1120,28 @@ grabkeys(void)
{
unsigned int i, j;
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
KeyCode code;
int kc, kcmin, kcmax, kcper;
KeySym keysym, *keysyms;

XUngrabKey(dpy, AnyKey, AnyModifier, root);
for (i = 0; i < LENGTH(keys); i++)
if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
for (j = 0; j < LENGTH(modifiers); j++)
XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
True, GrabModeAsync, GrabModeAsync);

/* retrieve all the keycode -> keysym mappings */
XDisplayKeycodes(dpy, &kcmin, &kcmax);
keysyms = XGetKeyboardMapping(dpy, kcmin, kcmax - kcmin + 1, &kcper);

/* only look at the first keysym for each keycode as we handle shifted states */
for (kc = kcmin; kc <= kcmax; kc++) {
keysym = keysyms[(kc - kcmin) * kcper];
for (i = 0; i < LENGTH(keys); i++) {
if (keys[i].keysym == keysym) {
for (j = 0; j < LENGTH(modifiers); j++) {
XGrabKey(dpy, kc, keys[i].mod | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
}
}
}
}

XFree(keysyms);
}
}

Expand Down
57 changes: 57 additions & 0 deletions patches/dwm-grab-all-keycodes-6.2.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
From f64e5ddc9bc47dd3bca79a1eac214525ba005caf Mon Sep 17 00:00:00 2001
From: Alexander Courtis <[email protected]>
Date: Sat, 15 Feb 2020 14:23:26 +1100
Subject: [PATCH] Grab all keycodes that map to keys.keysym

There may be multiple keycodes that map to a keys.keysym. One such scenario is using xkb to remap a key: `caps:escape`

When grabbing keys, we now scan all X keycode mappings and look for match.

Changing keymaps via xkb or other means will not cause the keys to be "re-grabbed". This existing behaviour is desirable.

---
dwm.c | 26 ++++++++++++++++++++------
1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/dwm.c b/dwm.c
index cc4fce7..04f6220 100644
--- a/dwm.c
+++ b/dwm.c
@@ -1104,14 +1104,28 @@ grabkeys(void)
{
unsigned int i, j;
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
- KeyCode code;
+ int kc, kcmin, kcmax, kcper;
+ KeySym keysym, *keysyms;

XUngrabKey(dpy, AnyKey, AnyModifier, root);
- for (i = 0; i < LENGTH(keys); i++)
- if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
- for (j = 0; j < LENGTH(modifiers); j++)
- XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
- True, GrabModeAsync, GrabModeAsync);
+
+ /* retrieve all the keycode -> keysym mappings */
+ XDisplayKeycodes(dpy, &kcmin, &kcmax);
+ keysyms = XGetKeyboardMapping(dpy, kcmin, kcmax - kcmin + 1, &kcper);
+
+ /* only look at the first keysym for each keycode as we handle shifted states */
+ for (kc = kcmin; kc <= kcmax; kc++) {
+ keysym = keysyms[(kc - kcmin) * kcper];
+ for (i = 0; i < LENGTH(keys); i++) {
+ if (keys[i].keysym == keysym) {
+ for (j = 0; j < LENGTH(modifiers); j++) {
+ XGrabKey(dpy, kc, keys[i].mod | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
+ }
+ }
+ }
+ }
+
+ XFree(keysyms);
}
}

--
2.25.0

0 comments on commit 3feb4ee

Please sign in to comment.