parsekey.c (1518B)
1 /* Copyright ©2007-2010 Kris Maglione <maglione.k at Gmail> 2 * See LICENSE file for license details. 3 */ 4 #include "../x11.h" 5 6 char *modkey_names[] = { 7 "Shift", 8 "", 9 "Control", 10 "Mod1", 11 "Mod2", 12 "Mod3", 13 "Mod4", 14 "Mod5", 15 nil 16 }; 17 18 bool 19 parsekey(char *str, int *mask, char **key) { 20 static char *keys[16]; 21 int i, j, nkeys; 22 23 *mask = 0; 24 nkeys = tokenize(keys, nelem(keys), str, '-'); 25 for(i=0; i < nkeys; i++) { 26 for(j=0; modkey_names[j]; j++) 27 if(!strcasecmp(modkey_names[j], keys[i])) { 28 *mask |= 1 << j; 29 goto next; 30 } 31 break; 32 next: continue; 33 } 34 if(key) { 35 if(nkeys) 36 *key = keys[i]; 37 return i == nkeys - 1; 38 } 39 else 40 return i == nkeys; 41 } 42 43 int 44 numlockmask(void) { 45 static int masks[] = { 46 ShiftMask, LockMask, ControlMask, Mod1Mask, 47 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask 48 }; 49 XModifierKeymap *modmap; 50 KeyCode kcode; 51 int i, max, numlock; 52 53 numlock = 0; 54 modmap = XGetModifierMapping(display); 55 kcode = keycode("Num_Lock"); 56 if(kcode && modmap && modmap->max_keypermod > 0) { 57 max = nelem(masks) * modmap->max_keypermod; 58 for(i = 0; i < max && !numlock; i++) 59 if(modmap->modifiermap[i] == kcode) 60 numlock = masks[i / modmap->max_keypermod]; 61 } 62 XFreeModifiermap(modmap); 63 return numlock; 64 } 65 66 int 67 fmtkey(Fmt *f) { 68 XKeyEvent *ev; 69 char *key; 70 int nfmt; 71 72 ev = va_arg(f->args, XKeyEvent*); 73 key = XKeysymToString(XKeycodeToKeysym(display, ev->keycode, 0)); 74 75 nfmt = f->nfmt; 76 unmask(f, ev->state, modkey_names, '-'); 77 if(f->nfmt > nfmt) 78 fmtrune(f, '-'); 79 return fmtstrcpy(f, key); 80 } 81