comparison event.c @ 9:bc03b37b37ba

move command mode text to the left, next to the layout icon
author paulo@thepaulopc
date Wed, 15 Jun 2011 23:27:32 -0700
parents a54de16f8277
children
comparison
equal deleted inserted replaced
3:1282da32047b 4:5a2375a0fd8b
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com> 1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details. 2 * See LICENSE file for license details.
3 */ 3 */
4 #include "dwm.h" 4 #include "dwm.h"
5 #include <stdlib.h>
6 #include <X11/keysym.h> 5 #include <X11/keysym.h>
7 #include <X11/Xatom.h> 6 #include <X11/Xatom.h>
8 7
9 /* static */ 8 /* static */
9
10 static unsigned int cmdmod[4];
11 static unsigned int keymode = COMMANDMODE;
12 static KeySym cmdkeysym[4];
10 13
11 typedef struct { 14 typedef struct {
12 unsigned long mod; 15 unsigned long mod;
13 KeySym keysym; 16 KeySym keysym;
14 void (*func)(Arg *arg); 17 void (*func)(Arg *arg);
285 drawstatus(); 288 drawstatus();
286 } 289 }
287 } 290 }
288 291
289 static void 292 static void
290 keypress(XEvent *e) { 293 defkeypress(XEvent *e) {
291 static unsigned int len = sizeof key / sizeof key[0]; 294 static unsigned int len = sizeof key / sizeof key[0];
292 unsigned int i; 295 unsigned int i;
293 KeySym keysym; 296 KeySym keysym;
294 XKeyEvent *ev = &e->xkey; 297 XKeyEvent *ev = &e->xkey;
295 298
368 Client *c; 371 Client *c;
369 XUnmapEvent *ev = &e->xunmap; 372 XUnmapEvent *ev = &e->xunmap;
370 373
371 if((c = getclient(ev->window))) 374 if((c = getclient(ev->window)))
372 unmanage(c); 375 unmanage(c);
376 }
377
378 typedef struct {
379 unsigned int mod[4];
380 KeySym keysym[4];
381 void (*func)(Arg *arg);
382 Arg arg;
383 } Command;
384
385 CMDKEYS
386 COMMANDS
387
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;
395
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 }
429
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 }
373 } 445 }
374 446
375 /* extern */ 447 /* extern */
376 448
377 void (*handler[LASTEvent]) (XEvent *) = { 449 void (*handler[LASTEvent]) (XEvent *) = {
387 [PropertyNotify] = propertynotify, 459 [PropertyNotify] = propertynotify,
388 [UnmapNotify] = unmapnotify 460 [UnmapNotify] = unmapnotify
389 }; 461 };
390 462
391 void 463 void
392 grabkeys(void) { 464 grabdefkeys(void) {
393 static unsigned int len = sizeof key / sizeof key[0]; 465 static unsigned int len = sizeof key / sizeof key[0];
394 unsigned int i; 466 unsigned int i;
395 KeyCode code; 467 KeyCode code;
396 468
397 XUngrabKey(dpy, AnyKey, AnyModifier, root); 469 XUngrabKey(dpy, AnyKey, AnyModifier, root);
405 GrabModeAsync, GrabModeAsync); 477 GrabModeAsync, GrabModeAsync);
406 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True, 478 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
407 GrabModeAsync, GrabModeAsync); 479 GrabModeAsync, GrabModeAsync);
408 } 480 }
409 } 481 }
482
483 void
484 clearcmd(Arg *arg) {
485 unsigned int i;
486
487 for(i = 0; i < LENGTH(cmdkeysym); i++) {
488 cmdkeysym[i] = 0;
489 cmdmod[i] = 0;
490 }
491 }
492
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 }
505
506 void
507 setkeymode(Arg *arg) {
508 Arg a = {0};
509
510 if(!arg)
511 return;
512 keymode = arg->i;
513 clearcmd(&a);
514 grabkeys();
515 drawstatus();
516 }
517
518 unsigned int
519 getkeymode(void) {
520 return keymode;
521 }
522
523 void
524 func_insert(void (*argfunc)(Arg *), Arg *arg) {
525 Arg a = { .i = INSERTMODE };
526 argfunc(arg);
527 setkeymode(&a);
528 }