dwm-grab-all-keycodes-6.2.diff (1859B)
1 From f64e5ddc9bc47dd3bca79a1eac214525ba005caf Mon Sep 17 00:00:00 2001 2 From: Alexander Courtis <acourtis@atlassian.com> 3 Date: Sat, 15 Feb 2020 14:23:26 +1100 4 Subject: [PATCH] Grab all keycodes that map to keys.keysym 5 6 There may be multiple keycodes that map to a keys.keysym. One such scenario is using xkb to remap a key: `caps:escape` 7 8 When grabbing keys, we now scan all X keycode mappings and look for match. 9 10 Changing keymaps via xkb or other means will not cause the keys to be "re-grabbed". This existing behaviour is desirable. 11 12 --- 13 dwm.c | 26 ++++++++++++++++++++------ 14 1 file changed, 20 insertions(+), 6 deletions(-) 15 16 diff --git a/dwm.c b/dwm.c 17 index cc4fce7..04f6220 100644 18 --- a/dwm.c 19 +++ b/dwm.c 20 @@ -1104,14 +1104,28 @@ grabkeys(void) 21 { 22 unsigned int i, j; 23 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; 24 - KeyCode code; 25 + int kc, kcmin, kcmax, kcper; 26 + KeySym keysym, *keysyms; 27 28 XUngrabKey(dpy, AnyKey, AnyModifier, root); 29 - for (i = 0; i < LENGTH(keys); i++) 30 - if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) 31 - for (j = 0; j < LENGTH(modifiers); j++) 32 - XGrabKey(dpy, code, keys[i].mod | modifiers[j], root, 33 - True, GrabModeAsync, GrabModeAsync); 34 + 35 + /* retrieve all the keycode -> keysym mappings */ 36 + XDisplayKeycodes(dpy, &kcmin, &kcmax); 37 + keysyms = XGetKeyboardMapping(dpy, kcmin, kcmax - kcmin + 1, &kcper); 38 + 39 + /* only look at the first keysym for each keycode as we handle shifted states */ 40 + for (kc = kcmin; kc <= kcmax; kc++) { 41 + keysym = keysyms[(kc - kcmin) * kcper]; 42 + for (i = 0; i < LENGTH(keys); i++) { 43 + if (keys[i].keysym == keysym) { 44 + for (j = 0; j < LENGTH(modifiers); j++) { 45 + XGrabKey(dpy, kc, keys[i].mod | modifiers[j], root, True, GrabModeAsync, GrabModeAsync); 46 + } 47 + } 48 + } 49 + } 50 + 51 + XFree(keysyms); 52 } 53 } 54 55 -- 56 2.25.0 57