[hackers] [wmii] Allow bindings to work regardless of caps lock. || Kris Maglione

From: <hg_AT_suckless.org>
Date: Sat, 16 May 2009 15:14:34 +0000 (UTC)

changeset: 2454:5d035a83e42f
tag: tip
user: Kris Maglione <jg_AT_suckless.org>
date: Sat May 16 11:14:33 2009 -0400
files: cmd/menu/keys.c cmd/wmii/fns.h cmd/wmii/key.c cmd/wmii/message.c cmd/wmii/x11.c include/x11.h
description:
Allow bindings to work regardless of caps lock.

diff -r 54b5cb0c33b3 -r 5d035a83e42f cmd/menu/keys.c
--- a/cmd/menu/keys.c Sat May 16 10:43:03 2009 -0400
+++ b/cmd/menu/keys.c Sat May 16 11:14:33 2009 -0400
@@ -5,7 +5,6 @@
 #include "fns.h"
 
 typedef struct Key Key;
-typedef struct KMask KMask;
 
 struct Key {
         Key* next;
@@ -15,19 +14,6 @@
 };
 
 static Key* bindings;
-
-static struct KMask {
- int mask;
- const char* name;
-} masks[] = {
- {ControlMask, "Control"},
- {Mod1Mask, "Mod1"},
- {Mod2Mask, "Mod2"},
- {Mod3Mask, "Mod3"},
- {Mod4Mask, "Mod4"},
- {ShiftMask, "Shift"},
- {0,}
-};
 
 static void
 init_numlock(void) {
@@ -58,12 +44,10 @@
 parse_keys(char *spec) {
         static char *lines[1024];
         static char *words[16];
- static char *keys[16];
         Key *k;
- KMask *m;
         char *p, *line;
- long mask;
- int i, j, nlines, nwords, nkeys;
+ int mask;
+ int i, nlines, nwords;
 
         if(!numlock)
                 init_numlock();
@@ -74,24 +58,14 @@
                 p = strchr(line, '#');
                 if(p)
                         *p = '\0';
+
                 nwords = stokenize(words, nelem(words) - 1, line, " \t");
                 words[nwords] = nil;
                 if(!words[0])
                         continue;
- mask = 0;
- nkeys = tokenize(keys, nelem(keys), words[0], '-');
- for(j=0; j < nkeys; j++) {
- for(m=masks; m->mask; m++)
- if(!strcasecmp(m->name, keys[j])) {
- mask |= m->mask;
- goto next;
- }
- break;
- next: continue;
- }
- if(j == nkeys - 1) {
+ if(parsekey(words[0], &mask, &p)) {
                         k = emallocz(sizeof *k);
- k->key = keys[j];
+ k->key = p;
                         k->mask = mask;
                         k->action = strlistdup(words + 1);
                         k->next = bindings;
diff -r 54b5cb0c33b3 -r 5d035a83e42f cmd/wmii/fns.h
--- a/cmd/wmii/fns.h Sat May 16 10:43:03 2009 -0400
+++ b/cmd/wmii/fns.h Sat May 16 11:14:33 2009 -0400
@@ -201,7 +201,6 @@
 /* key.c */
 void init_lock_keys(void);
 void kpress(XWindow, ulong mod, KeyCode);
-ulong str2modmask(const char*);
 void update_keys(void);
 
 /* main.c */
diff -r 54b5cb0c33b3 -r 5d035a83e42f cmd/wmii/key.c
--- a/cmd/wmii/key.c Sat May 16 10:43:03 2009 -0400
+++ b/cmd/wmii/key.c Sat May 16 11:14:33 2009 -0400
@@ -8,22 +8,19 @@
 
 void
 init_lock_keys(void) {
+ static int masks[] = {
+ ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask,
+ Mod3Mask, Mod4Mask, Mod5Mask
+ };
         XModifierKeymap *modmap;
         KeyCode numlock;
- static int masks[] = {
- ShiftMask, LockMask, ControlMask,
- Mod1Mask, Mod2Mask, Mod3Mask,
- Mod4Mask, Mod5Mask
- };
- int i;
+ int i, max;
 
         numlock_mask = 0;
         modmap = XGetModifierMapping(display);
         numlock = keycode("Num_Lock");
         if(numlock)
         if(modmap && modmap->max_keypermod > 0) {
- int max;
-
                 max = nelem(masks) * modmap->max_keypermod;
                 for(i = 0; i < max; i++)
                         if(modmap->modifiermap[i] == numlock)
@@ -31,27 +28,6 @@
         }
         XFreeModifiermap(modmap);
         valid_mask = 255 & ~(numlock_mask | LockMask);
-}
-
-ulong
-str2modmask(const char *val) {
- ulong mod = 0;
-
- if (strstr(val, "Shift"))
- mod |= ShiftMask;
- if (strstr(val, "Control"))
- mod |= ControlMask;
- if (strstr(val, "Mod1"))
- mod |= Mod1Mask;
- if (strstr(val, "Mod2"))
- mod |= Mod2Mask;
- if (strstr(val, "Mod3"))
- mod |= Mod3Mask;
- if (strstr(val, "Mod4"))
- mod |= Mod4Mask;
- if (strstr(val, "Mod5"))
- mod |= Mod5Mask;
- return mod;
 }
 
 static void
@@ -73,21 +49,21 @@
 static void
 grabkey(Key *k) {
         _grab(scr.root.w, k->key, k->mod);
+ _grab(scr.root.w, k->key, k->mod | LockMask);
         if(numlock_mask) {
                 _grab(scr.root.w, k->key, k->mod | numlock_mask);
                 _grab(scr.root.w, k->key, k->mod | numlock_mask | LockMask);
         }
- /* sync(); */
 }
 
 static void
 ungrabkey(Key *k) {
         XUngrabKey(display, k->key, k->mod, scr.root.w);
+ XUngrabKey(display, k->key, k->mod | LockMask, scr.root.w);
         if(numlock_mask) {
                 XUngrabKey(display, k->key, k->mod | numlock_mask, scr.root.w);
                 XUngrabKey(display, k->key, k->mod | numlock_mask | LockMask, scr.root.w);
         }
- /* sync(); */
 }
 
 static Key *
@@ -102,12 +78,13 @@
 
 static Key*
 getkey(const char *name) {
+ Key *k, *r;
         char buf[128];
         char *seq[8];
         char *kstr;
+ int mask;
         uint i, toks;
         static ushort id = 1;
- Key *k, *r;
 
         r = nil;
 
@@ -125,15 +102,11 @@
                         k = k->next;
                 }
                 utflcpy(k->name, name, sizeof k->name);
- kstr = strrchr(seq[i], '-');
- if(kstr)
- kstr++;
- else
- kstr = seq[i];
- k->key = XKeysymToKeycode(display, XStringToKeysym(kstr));
- k->mod = str2modmask(seq[i]);
- if (k->key == NoSymbol)
- {
+ if(parsekey(seq[i], &mask, &kstr)) {
+ k->key = keycode(kstr);
+ k->mod = mask;
+ }
+ if(k->key == 0) {
                         freekey(r);
                         return nil;
                 }
diff -r 54b5cb0c33b3 -r 5d035a83e42f cmd/wmii/message.c
--- a/cmd/wmii/message.c Sat May 16 10:43:03 2009 -0400
+++ b/cmd/wmii/message.c Sat May 16 11:14:33 2009 -0400
@@ -462,6 +462,7 @@
         Font *fn;
         char *s, *ret;
         ulong n;
+ int i;
 
         USED(p);
         ret = nil;
@@ -518,13 +519,11 @@
                 break;
         case LGRABMOD:
                 s = msg_getword(m);
- n = str2modmask(s);
-
- if((n & (Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) == 0)
+ if(!parsekey(s, &i, nil) || i == 0)
                         return Ebadvalue;
 
                 utflcpy(def.grabmod, s, sizeof def.grabmod);
- def.mod = n;
+ def.mod = i;
                 break;
         case LINCMODE:
                 if(!setdef(&def.incmode, msg_getword(m), incmodetab, nelem(incmodetab)))
diff -r 54b5cb0c33b3 -r 5d035a83e42f cmd/wmii/x11.c
--- a/cmd/wmii/x11.c Sat May 16 10:43:03 2009 -0400
+++ b/cmd/wmii/x11.c Sat May 16 11:14:33 2009 -0400
@@ -8,6 +8,7 @@
 #include "dat.h"
 #include <limits.h>
 #include <math.h>
+#include <strings.h>
 #include <unistd.h>
 #include <bio.h>
 #include "fns.h"
@@ -732,6 +733,48 @@
         return XKeysymToKeycode(display, XStringToKeysym(name));
 }
 
+typedef struct KMask KMask;
+
+static struct KMask {
+ int mask;
+ const char* name;
+} masks[] = {
+ {ShiftMask, "Shift"},
+ {ControlMask, "Control"},
+ {Mod1Mask, "Mod1"},
+ {Mod2Mask, "Mod2"},
+ {Mod3Mask, "Mod3"},
+ {Mod4Mask, "Mod4"},
+ {Mod5Mask, "Mod5"},
+ {0,}
+};
+
+bool
+parsekey(char *str, int *mask, char **key) {
+ static char *keys[16];
+ KMask *m;
+ int i, nkeys;
+
+ *mask = 0;
+ nkeys = tokenize(keys, nelem(keys), str, '-');
+ for(i=0; i < nkeys; i++) {
+ for(m=masks; m->mask; m++)
+ if(!strcasecmp(m->name, keys[i])) {
+ *mask |= m->mask;
+ goto next;
+ }
+ break;
+ next: continue;
+ }
+ if(key) {
+ if(nkeys)
+ *key = keys[i];
+ return i == nkeys - 1;
+ }
+ else
+ return i == nkeys;
+}
+
 void
 sync(void) {
         XSync(display, false);
diff -r 54b5cb0c33b3 -r 5d035a83e42f include/x11.h
--- a/include/x11.h Sat May 16 10:43:03 2009 -0400
+++ b/include/x11.h Sat May 16 11:14:33 2009 -0400
@@ -226,6 +226,7 @@
 void movewin(Window*, Point);
 Point mulpt(Point p, Point q);
 bool namedcolor(char *name, ulong*);
+bool parsekey(char*, int*, char**);
 int pointerscreen(void);
 Point querypointer(Window*);
 void raisewin(Window*);
Received on Sat May 16 2009 - 15:14:34 UTC

This archive was generated by hypermail 2.2.0 : Sat May 16 2009 - 15:24:05 UTC