Allow bindings to work regardless of caps lock.

This commit is contained in:
Kris Maglione 2009-05-16 11:14:33 -04:00
parent 6884033956
commit 69aec39fa1
6 changed files with 66 additions and 77 deletions

View File

@ -5,7 +5,6 @@
#include "fns.h"
typedef struct Key Key;
typedef struct KMask KMask;
struct Key {
Key* next;
@ -16,19 +15,6 @@ struct Key {
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) {
static int masks[] = {
@ -58,12 +44,10 @@ void
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 @@ parse_keys(char *spec) {
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;

View File

@ -201,7 +201,6 @@ Rectangle rect_intersection(Rectangle, Rectangle);
/* key.c */
void init_lock_keys(void);
void kpress(XWindow, ulong mod, KeyCode);
ulong str2modmask(const char*);
void update_keys(void);
/* main.c */

View File

@ -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)
@ -33,27 +30,6 @@ init_lock_keys(void) {
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
freekey(Key *k) {
Key *n;
@ -73,21 +49,21 @@ _grab(XWindow w, int keycode, uint mod) {
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 @@ name2key(const char *name) {
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 @@ getkey(const char *name) {
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;
}

View File

@ -462,6 +462,7 @@ message_root(void *p, IxpMsg *m) {
Font *fn;
char *s, *ret;
ulong n;
int i;
USED(p);
ret = nil;
@ -518,13 +519,11 @@ message_root(void *p, IxpMsg *m) {
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)))

View File

@ -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 @@ keycode(char *name) {
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);

View File

@ -226,6 +226,7 @@ int mapwin(Window*);
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*);