wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

keys.c (1981B)


      1 #include "dat.h"
      2 #include <ctype.h>
      3 #include <strings.h>
      4 #include <unistd.h>
      5 #include "fns.h"
      6 
      7 typedef struct Key Key;
      8 
      9 struct Key {
     10 	Key*	next;
     11 	long	mask;
     12 	char*	key;
     13 	char**	action;
     14 };
     15 
     16 static Key*	bindings;
     17 static int	numlock;
     18 
     19 /*
     20  * To do: Find my red black tree implementation.
     21  */
     22 void
     23 parse_keys(char *spec) {
     24 	static char *lines[1024];
     25 	static char *words[16];
     26 	Key *k;
     27 	char *p, *line;
     28 	int mask;
     29 	int i, nlines, nwords;
     30 
     31 	if(!numlock)
     32 		numlock = numlockmask();
     33 
     34 	nlines = tokenize(lines, nelem(lines), spec, '\n');
     35 	for(i=0; i < nlines; i++) {
     36 		line = lines[i];
     37 		p = strchr(line, '#');
     38 		if(p)
     39 			*p = '\0';
     40 
     41 		nwords = stokenize(words, nelem(words) - 1, line, " \t");
     42 		words[nwords] = nil;
     43 		if(!words[0])
     44 			continue;
     45 		if(parsekey(words[0], &mask, &p)) {
     46 			k = emallocz(sizeof *k);
     47 			k->key    = p;
     48 			k->mask   = mask;
     49 			k->action = strlistdup(words + 1);
     50 			k->next   = bindings;
     51 			bindings = k;
     52 		}
     53 	}
     54 }
     55 
     56 char**
     57 find_key(char *key, long mask) {
     58 	Key *k;
     59 
     60 	/* Horrible hack. */
     61 	if(!strcmp(key, "ISO_Left_Tab"))
     62 		key = "Tab";
     63 
     64 	mask &= ~(numlock | LockMask) & ((1<<8) - 1);
     65 	for(k=bindings; k; k=k->next)
     66 		if(!strcasecmp(k->key, key) && k->mask == mask)
     67 			return k->action;
     68 	return nil;
     69 }
     70 
     71 	/* sed 's/"([^"]+)"/L\1/g' | tr 'a-z' 'A-Z' */
     72 	/* awk '{$1=""; print}' keys.txt | perl -e '$_=lc join "", <>; print join "\n", m/(\w+)/g;' | sort -u | sed 's:.*:	"&",:' */
     73 char *symtab[] = {
     74 	"accept",
     75 	"backward",
     76 	"char",
     77 	"complete",
     78 	"delete",
     79 	"first",
     80 	"forward",
     81 	"history",
     82 	"kill",
     83 	"last",
     84 	"line",
     85 	"literal",
     86 	"next",
     87 	"nextpage",
     88 	"paste",
     89 	"prev",
     90 	"prevpage",
     91 	"reject",
     92 	"word",
     93 };
     94 
     95 static int
     96 _bsearch(char *s, char **tab, int ntab) {
     97 	int i, n, m, cmp;
     98 
     99 	if(s == nil)
    100 		return -1;
    101 
    102 	n = ntab;
    103 	i = 0;
    104 	while(n) {
    105 		m = n/2;
    106 		cmp = strcasecmp(s, tab[i+m]);
    107 		if(cmp == 0)
    108 			return i+m;
    109 		if(cmp < 0 || m == 0)
    110 			n = m;
    111 		else {
    112 			i += m;
    113 			n = n-m;
    114 		}
    115 	}
    116 	return -1;
    117 }
    118 
    119 int
    120 getsym(char *s) {
    121 	return _bsearch(s, symtab, nelem(symtab));
    122 }
    123