Mercurial > hg > index.fcgi > dwm > dwm-3.6.1-12pba
view event.c @ 11:da0e0496f75b
add "inverse" color set, for indicating in the status bar when in command mode
author | paulo |
---|---|
date | Tue, 18 Sep 2012 00:17:32 -0700 |
parents | a54de16f8277 |
children | c65d4f8dc8fd |
line source
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <X11/keysym.h>
6 #include <X11/Xatom.h>
8 /* static */
10 static unsigned int cmdmod[4];
11 static unsigned int keymode = COMMANDMODE;
12 static KeySym cmdkeysym[4];
14 typedef struct {
15 unsigned long mod;
16 KeySym keysym;
17 void (*func)(Arg *arg);
18 Arg arg;
19 } Key;
21 KEYS
23 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
24 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
26 static Client *
27 getclient(Window w) {
28 Client *c;
30 for(c = clients; c && c->win != w; c = c->next);
31 return c;
32 }
34 static void
35 movemouse(Client *c) {
36 int x1, y1, ocx, ocy, di, nx, ny;
37 unsigned int dui;
38 Window dummy;
39 XEvent ev;
41 ocx = nx = c->x;
42 ocy = ny = c->y;
43 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
44 None, cursor[CurMove], CurrentTime) != GrabSuccess)
45 return;
46 c->ismax = False;
47 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
48 for(;;) {
49 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
50 switch (ev.type) {
51 case ButtonRelease:
52 XUngrabPointer(dpy, CurrentTime);
53 return;
54 case ConfigureRequest:
55 case Expose:
56 case MapRequest:
57 handler[ev.type](&ev);
58 break;
59 case MotionNotify:
60 XSync(dpy, False);
61 nx = ocx + (ev.xmotion.x - x1);
62 ny = ocy + (ev.xmotion.y - y1);
63 if(abs(wax + nx) < SNAP)
64 nx = wax;
65 else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
66 nx = wax + waw - c->w - 2 * c->border;
67 if(abs(way - ny) < SNAP)
68 ny = way;
69 else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
70 ny = way + wah - c->h - 2 * c->border;
71 resize(c, nx, ny, c->w, c->h, False);
72 break;
73 }
74 }
75 }
77 static void
78 resizemouse(Client *c) {
79 int ocx, ocy;
80 int nw, nh;
81 XEvent ev;
83 ocx = c->x;
84 ocy = c->y;
85 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
86 None, cursor[CurResize], CurrentTime) != GrabSuccess)
87 return;
88 c->ismax = False;
89 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
90 for(;;) {
91 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
92 switch(ev.type) {
93 case ButtonRelease:
94 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
95 c->w + c->border - 1, c->h + c->border - 1);
96 XUngrabPointer(dpy, CurrentTime);
97 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
98 return;
99 case ConfigureRequest:
100 case Expose:
101 case MapRequest:
102 handler[ev.type](&ev);
103 break;
104 case MotionNotify:
105 XSync(dpy, False);
106 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
107 nw = 1;
108 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
109 nh = 1;
110 resize(c, c->x, c->y, nw, nh, True);
111 break;
112 }
113 }
114 }
116 static void
117 buttonpress(XEvent *e) {
118 int x;
119 Arg a;
120 Client *c;
121 XButtonPressedEvent *ev = &e->xbutton;
123 if(barwin == ev->window) {
124 x = 0;
125 for(a.i = 0; a.i < ntags; a.i++) {
126 x += textw(tags[a.i]);
127 if(ev->x < x) {
128 if(ev->button == Button1) {
129 if(ev->state & MODKEY)
130 tag(&a);
131 else
132 view(&a);
133 }
134 else if(ev->button == Button3) {
135 if(ev->state & MODKEY)
136 toggletag(&a);
137 else
138 toggleview(&a);
139 }
140 return;
141 }
142 }
143 if(ev->x < x + blw)
144 switch(ev->button) {
145 case Button1:
146 a.i = -1;
147 setlayout(&a);
148 break;
149 case Button3:
150 a.i = -2;
151 setlayout(&a);
152 break;
153 case Button4:
154 a.i = 1;
155 incnmaster(&a);
156 break;
157 case Button5:
158 a.i = -1;
159 incnmaster(&a);
160 break;
161 }
162 if(ev->x > x + blw)
163 switch(ev->button) {
164 case Button1: zoom(NULL); break;
165 case Button2: toggleversatile(NULL); break;
166 case Button3: killclient(NULL); break;
167 case Button4: pushup(NULL); break;
168 case Button5: pushdown(NULL); break;
169 }
170 }
171 else if(tbarwin == ev->window) {
172 int i, w;
173 for(i=0, c = clients; c; c = c->next) {
174 if(isvisible(c))
175 i++;
176 }
177 if(!i || i >= MAX_TASKS)
178 return;
179 else
180 w = sw/i;
181 switch(ev->button) {
182 case Button4: focusprev(NULL); return; break;
183 case Button5: focusnext(NULL); return; break;
184 }
185 for(i=1, c = clients; c && i*w<=sw; c = c->next, i++) {
186 for(; c && !isvisible(c); c = c->next);
187 if(i*w > ev->x) {
188 switch(ev->button) {
189 case Button1: focus(c); restack(); break;
190 case Button3: focus(c); zoom(NULL); break;
191 }
192 break;
193 }
194 }
195 }
196 else if((c = getclient(ev->window))) {
197 focus(c);
198 if(CLEANMASK(ev->state) != MODKEY)
199 return;
200 if(ev->button == Button1 && (lt->arrange == versatile || c->isversatile)) {
201 restack();
202 movemouse(c);
203 }
204 else if(ev->button == Button2)
205 zoom(NULL);
206 else if(ev->button == Button3
207 && (lt->arrange == versatile || c->isversatile) && !c->isfixed)
208 {
209 restack();
210 resizemouse(c);
211 }
212 }
213 }
215 static void
216 configurerequest(XEvent *e) {
217 Client *c;
218 XConfigureRequestEvent *ev = &e->xconfigurerequest;
219 XWindowChanges wc;
221 if((c = getclient(ev->window))) {
222 c->ismax = False;
223 if(ev->value_mask & CWBorderWidth)
224 c->border = ev->border_width;
225 if(c->isfixed || c->isversatile || (lt->arrange == versatile)) {
226 if(ev->value_mask & CWX)
227 c->x = ev->x;
228 if(ev->value_mask & CWY)
229 c->y = ev->y;
230 if(ev->value_mask & CWWidth)
231 c->w = ev->width;
232 if(ev->value_mask & CWHeight)
233 c->h = ev->height;
234 if((ev->value_mask & (CWX | CWY))
235 && !(ev->value_mask & (CWWidth | CWHeight)))
236 configure(c);
237 if(isvisible(c))
238 /* XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); */
239 resize(c, c->x, c->y, c->w, c->h, False);
240 }
241 else
242 configure(c);
243 }
244 else {
245 wc.x = ev->x;
246 wc.y = ev->y;
247 wc.width = ev->width;
248 wc.height = ev->height;
249 wc.border_width = ev->border_width;
250 wc.sibling = ev->above;
251 wc.stack_mode = ev->detail;
252 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
253 }
254 XSync(dpy, False);
255 }
257 static void
258 destroynotify(XEvent *e) {
259 Client *c;
260 XDestroyWindowEvent *ev = &e->xdestroywindow;
262 if((c = getclient(ev->window)))
263 unmanage(c);
264 }
266 static void
267 enternotify(XEvent *e) {
268 Client *c;
269 XCrossingEvent *ev = &e->xcrossing;
271 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
272 return;
273 if(!CLICK_TO_FOCUS && (c = getclient(ev->window)) && isvisible(c))
274 focus(c);
275 else if(ev->window == root) {
276 selscreen = True;
277 for(c = stack; c && !isvisible(c); c = c->snext);
278 focus(c);
279 }
280 }
282 static void
283 expose(XEvent *e) {
284 XExposeEvent *ev = &e->xexpose;
286 if(ev->count == 0) {
287 if(barwin == ev->window || tbarwin == ev->window)
288 drawstatus();
289 }
290 }
292 static void
293 defkeypress(XEvent *e) {
294 static unsigned int len = sizeof key / sizeof key[0];
295 unsigned int i;
296 KeySym keysym;
297 XKeyEvent *ev = &e->xkey;
299 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
300 for(i = 0; i < len; i++)
301 if(keysym == key[i].keysym
302 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
303 {
304 if(key[i].func)
305 key[i].func(&key[i].arg);
306 }
307 }
309 static void
310 leavenotify(XEvent *e) {
311 XCrossingEvent *ev = &e->xcrossing;
313 if((ev->window == root) && !ev->same_screen) {
314 selscreen = False;
315 focus(NULL);
316 }
317 }
319 static void
320 mappingnotify(XEvent *e) {
321 XMappingEvent *ev = &e->xmapping;
323 XRefreshKeyboardMapping(ev);
324 if(ev->request == MappingKeyboard)
325 grabkeys();
326 }
328 static void
329 maprequest(XEvent *e) {
330 static XWindowAttributes wa;
331 XMapRequestEvent *ev = &e->xmaprequest;
333 if(!XGetWindowAttributes(dpy, ev->window, &wa))
334 return;
335 if(wa.override_redirect)
336 return;
337 if(!getclient(ev->window))
338 manage(ev->window, &wa);
339 }
341 static void
342 propertynotify(XEvent *e) {
343 Client *c;
344 Window trans;
345 XPropertyEvent *ev = &e->xproperty;
347 if(ev->state == PropertyDelete)
348 return; /* ignore */
349 if((c = getclient(ev->window))) {
350 switch (ev->atom) {
351 default: break;
352 case XA_WM_TRANSIENT_FOR:
353 XGetTransientForHint(dpy, c->win, &trans);
354 if(!c->isversatile && (c->isversatile = (getclient(trans) != NULL)))
355 lt->arrange();
356 break;
357 case XA_WM_NORMAL_HINTS:
358 updatesizehints(c);
359 break;
360 }
361 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
362 updatetitle(c);
363 if(c == sel)
364 drawstatus();
365 }
366 }
367 }
369 static void
370 unmapnotify(XEvent *e) {
371 Client *c;
372 XUnmapEvent *ev = &e->xunmap;
374 if((c = getclient(ev->window)))
375 unmanage(c);
376 }
378 typedef struct {
379 unsigned int mod[4];
380 KeySym keysym[4];
381 void (*func)(Arg *arg);
382 Arg arg;
383 } Command;
385 CMDKEYS
386 COMMANDS
388 void
389 keypress(XEvent *e) {
390 unsigned int i, j;
391 Arg a = {0};
392 Bool ismatch = False, maybematch = False;
393 KeySym keysym;
394 XKeyEvent *ev;
396 if(keymode == INSERTMODE)
397 defkeypress(e);
398 else if(keymode == COMMANDMODE) {
399 ev = &e->xkey;
400 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
401 if(keysym < XK_Shift_L || keysym > XK_Hyper_R) {
402 for(j = 0; j < LENGTH(cmdkeysym); j++)
403 if(cmdkeysym[j] == 0) {
404 cmdkeysym[j] = keysym;
405 cmdmod[j] = ev->state;
406 break;
407 }
408 for(i = 0; i < LENGTH(commands); i++) {
409 for(j = 0; j < LENGTH(cmdkeysym); j++) {
410 if(cmdkeysym[j] == commands[i].keysym[j]
411 && CLEANMASK(cmdmod[j]) == CLEANMASK(commands[i].mod[j]))
412 ismatch = True;
413 else if(cmdkeysym[j] == 0
414 && cmdmod[j] == 0) {
415 ismatch = False;
416 maybematch = True;
417 break;
418 } else {
419 ismatch = False;
420 break;
421 }
422 }
423 if(ismatch) {
424 if(commands[i].func)
425 commands[i].func(&(commands[i].arg));
426 clearcmd(&a);
427 break;
428 }
430 }
431 if(!maybematch)
432 clearcmd(&a);
433 if(!ismatch) {
434 for(i = 0; i < LENGTH(cmdkeys); i++)
435 if(keysym == cmdkeys[i].keysym
436 && CLEANMASK(cmdkeys[i].mod) == CLEANMASK(ev->state)
437 && cmdkeys[i].func) {
438 cmdkeys[i].func(&(cmdkeys[i].arg));
439 ismatch = True;
440 break;
441 }
442 }
443 }
444 }
445 }
447 /* extern */
449 void (*handler[LASTEvent]) (XEvent *) = {
450 [ButtonPress] = buttonpress,
451 [ConfigureRequest] = configurerequest,
452 [DestroyNotify] = destroynotify,
453 [EnterNotify] = enternotify,
454 [LeaveNotify] = leavenotify,
455 [Expose] = expose,
456 [KeyPress] = keypress,
457 [MappingNotify] = mappingnotify,
458 [MapRequest] = maprequest,
459 [PropertyNotify] = propertynotify,
460 [UnmapNotify] = unmapnotify
461 };
463 void
464 grabdefkeys(void) {
465 static unsigned int len = sizeof key / sizeof key[0];
466 unsigned int i;
467 KeyCode code;
469 XUngrabKey(dpy, AnyKey, AnyModifier, root);
470 for(i = 0; i < len; i++) {
471 code = XKeysymToKeycode(dpy, key[i].keysym);
472 XGrabKey(dpy, code, key[i].mod, root, True,
473 GrabModeAsync, GrabModeAsync);
474 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
475 GrabModeAsync, GrabModeAsync);
476 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
477 GrabModeAsync, GrabModeAsync);
478 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
479 GrabModeAsync, GrabModeAsync);
480 }
481 }
483 void
484 clearcmd(Arg *arg) {
485 unsigned int i;
487 for(i = 0; i < LENGTH(cmdkeysym); i++) {
488 cmdkeysym[i] = 0;
489 cmdmod[i] = 0;
490 }
491 }
493 void
494 grabkeys(void) {
495 if(keymode == INSERTMODE) {
496 XUngrabKeyboard(dpy, CurrentTime);
497 grabdefkeys();
498 } else if(keymode == COMMANDMODE) {
499 XUngrabKey(dpy, AnyKey, AnyModifier, root);
500 //XGrabKey(dpy, AnyKey, AnyModifier, root,
501 // True, GrabModeAsync, GrabModeAsync);
502 XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime);
503 }
504 }
506 void
507 setkeymode(Arg *arg) {
508 Arg a = {0};
510 if(!arg)
511 return;
512 keymode = arg->i;
513 clearcmd(&a);
514 grabkeys();
515 drawstatus();
516 }
518 unsigned int
519 getkeymode(void) {
520 return keymode;
521 }
523 void
524 func_insert(void (*argfunc)(Arg *), Arg *arg) {
525 Arg a = { .i = INSERTMODE };
526 argfunc(arg);
527 setkeymode(&a);
528 }