diff event.c @ 6:0968b3739b8d

apply keymodes patch
author paulo@thepaulopc
date Sun, 12 Jun 2011 14:32:54 -0700
parents a54de16f8277
children c65d4f8dc8fd
line diff
     1.1 --- a/event.c	Wed Jan 27 23:28:29 2010 -0800
     1.2 +++ b/event.c	Sun Jun 12 14:32:54 2011 -0700
     1.3 @@ -2,12 +2,15 @@
     1.4   * See LICENSE file for license details.
     1.5   */
     1.6  #include "dwm.h"
     1.7 -#include <stdlib.h>
     1.8  #include <X11/keysym.h>
     1.9  #include <X11/Xatom.h>
    1.10  
    1.11  /* static */
    1.12  
    1.13 +static unsigned int cmdmod[4];
    1.14 +static unsigned int keymode = COMMANDMODE;
    1.15 +static KeySym cmdkeysym[4];
    1.16 +
    1.17  typedef struct {
    1.18  	unsigned long mod;
    1.19  	KeySym keysym;
    1.20 @@ -287,7 +290,7 @@
    1.21  }
    1.22  
    1.23  static void
    1.24 -keypress(XEvent *e) {
    1.25 +defkeypress(XEvent *e) {
    1.26  	static unsigned int len = sizeof key / sizeof key[0];
    1.27  	unsigned int i;
    1.28  	KeySym keysym;
    1.29 @@ -372,6 +375,75 @@
    1.30  		unmanage(c);
    1.31  }
    1.32  
    1.33 +typedef struct {
    1.34 +	unsigned int mod[4];
    1.35 +	KeySym keysym[4];
    1.36 +	void (*func)(Arg *arg);
    1.37 +	Arg arg;
    1.38 +} Command;
    1.39 +
    1.40 +CMDKEYS
    1.41 +COMMANDS
    1.42 +
    1.43 +void
    1.44 +keypress(XEvent *e) {
    1.45 +	unsigned int i, j;
    1.46 +	Arg a = {0};
    1.47 +	Bool ismatch = False, maybematch = False;
    1.48 +	KeySym keysym;
    1.49 +	XKeyEvent *ev;
    1.50 +
    1.51 +	if(keymode == INSERTMODE)
    1.52 +		defkeypress(e);
    1.53 +	else if(keymode == COMMANDMODE) {
    1.54 +		ev = &e->xkey;
    1.55 +		keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
    1.56 +		if(keysym < XK_Shift_L || keysym > XK_Hyper_R) {
    1.57 +			for(j = 0; j < LENGTH(cmdkeysym); j++)
    1.58 +				if(cmdkeysym[j] == 0) {
    1.59 +					cmdkeysym[j] = keysym;
    1.60 +					cmdmod[j] = ev->state;
    1.61 +					break;
    1.62 +				}
    1.63 +			for(i = 0; i < LENGTH(commands); i++) {
    1.64 +				for(j = 0; j < LENGTH(cmdkeysym); j++) {
    1.65 +					if(cmdkeysym[j] == commands[i].keysym[j] 
    1.66 +					&& CLEANMASK(cmdmod[j]) == CLEANMASK(commands[i].mod[j]))
    1.67 +						ismatch = True;
    1.68 +					else if(cmdkeysym[j] == 0 
    1.69 +					&& cmdmod[j] == 0) {
    1.70 +						ismatch = False;
    1.71 +						maybematch = True;
    1.72 +						break;
    1.73 +					} else {
    1.74 +						ismatch = False;
    1.75 +						break;
    1.76 +					}
    1.77 +				}
    1.78 +				if(ismatch) {
    1.79 +					if(commands[i].func)
    1.80 +						commands[i].func(&(commands[i].arg));
    1.81 +					clearcmd(&a);
    1.82 +					break;
    1.83 +				}
    1.84 +
    1.85 +			}
    1.86 +			if(!maybematch)
    1.87 +				clearcmd(&a);
    1.88 +			if(!ismatch) {
    1.89 +				for(i = 0; i < LENGTH(cmdkeys); i++)
    1.90 +					if(keysym == cmdkeys[i].keysym
    1.91 +					&& CLEANMASK(cmdkeys[i].mod) == CLEANMASK(ev->state)
    1.92 +					&& cmdkeys[i].func) {
    1.93 +						cmdkeys[i].func(&(cmdkeys[i].arg));
    1.94 +						ismatch = True;
    1.95 +						break;
    1.96 +					}
    1.97 +			}
    1.98 +		}
    1.99 +	}
   1.100 +}
   1.101 +
   1.102  /* extern */
   1.103  
   1.104  void (*handler[LASTEvent]) (XEvent *) = {
   1.105 @@ -389,7 +461,7 @@
   1.106  };
   1.107  
   1.108  void
   1.109 -grabkeys(void) {
   1.110 +grabdefkeys(void) {
   1.111  	static unsigned int len = sizeof key / sizeof key[0];
   1.112  	unsigned int i;
   1.113  	KeyCode code;
   1.114 @@ -407,3 +479,50 @@
   1.115  				GrabModeAsync, GrabModeAsync);
   1.116  	}
   1.117  }
   1.118 +
   1.119 +void
   1.120 +clearcmd(Arg *arg) {
   1.121 +	unsigned int i;
   1.122 +
   1.123 +	for(i = 0; i < LENGTH(cmdkeysym); i++) {
   1.124 +		cmdkeysym[i] = 0;
   1.125 +		cmdmod[i] = 0;
   1.126 +	}
   1.127 +}
   1.128 +
   1.129 +void
   1.130 +grabkeys(void) {
   1.131 +	if(keymode == INSERTMODE) {
   1.132 +		XUngrabKeyboard(dpy, CurrentTime);
   1.133 +		grabdefkeys();
   1.134 +	} else if(keymode == COMMANDMODE) {
   1.135 +		XUngrabKey(dpy, AnyKey, AnyModifier, root);
   1.136 +		//XGrabKey(dpy, AnyKey, AnyModifier, root,
   1.137 +		//	 True, GrabModeAsync, GrabModeAsync);
   1.138 +		XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime);
   1.139 +	}
   1.140 +}
   1.141 +
   1.142 +void
   1.143 +setkeymode(Arg *arg) {
   1.144 +	Arg a = {0};
   1.145 +
   1.146 +	if(!arg)
   1.147 +		return;
   1.148 +	keymode = arg->i;
   1.149 +	clearcmd(&a);
   1.150 +	grabkeys();
   1.151 +	drawstatus();
   1.152 +}
   1.153 +
   1.154 +unsigned int
   1.155 +getkeymode(void) {
   1.156 +	return keymode;
   1.157 +}
   1.158 +
   1.159 +void
   1.160 +func_insert(void (*argfunc)(Arg *), Arg *arg) {
   1.161 +	Arg a = { .i = INSERTMODE };
   1.162 +	argfunc(arg);
   1.163 +	setkeymode(&a);
   1.164 +}