wikeyname.c (1313B)
1 /* Copyright ©2010 Kris Maglione <maglione.k at Gmail> 2 * See LICENSE file for license details. 3 */ 4 #include <stuff/util.h> 5 #include <stuff/x.h> 6 #include <fmt.h> 7 #include <locale.h> 8 #include <unistd.h> 9 10 static const char version[] = "wikeyname-"VERSION", "COPYRIGHT"\n"; 11 12 static Handlers handlers; 13 static char* keyname; 14 static int nkeys; 15 16 static void 17 usage(void) { 18 lprint(2, "usage: wikeyname\n"); 19 exit(1); 20 } 21 22 int 23 main(int argc, char *argv[]) { 24 25 setlocale(LC_CTYPE, ""); 26 27 ARGBEGIN{ 28 case 'v': 29 lprint(2, version); 30 return 0; 31 default: usage(); 32 }ARGEND; 33 34 if(argc) 35 usage(); 36 37 fmtinstall('K', fmtkey); 38 initdisplay(); 39 40 selectinput(&scr.root, KeyPressMask|KeyReleaseMask); 41 sethandler(&scr.root, &handlers); 42 if(!grabkeyboard(&scr.root)) 43 fatal("can't grab keyboard\n"); 44 45 if(isatty(1)) 46 lprint(2, "Please press a key...\n"); 47 event_loop(); 48 lprint(1, "%s\n", keyname); 49 50 XCloseDisplay(display); 51 return 0; 52 } 53 54 static bool 55 kdown_event(Window *w, void *aux, XKeyEvent *ev) { 56 57 USED(w, aux); 58 nkeys++; 59 free(keyname); 60 keyname = smprint("%K", ev); 61 return false; 62 } 63 64 static bool 65 kup_event(Window *w, void *aux, XKeyEvent *ev) { 66 67 USED(w, aux, ev); 68 if(keyname != nil && --nkeys <= 0) 69 event_looprunning = false; 70 return false; 71 } 72 73 74 static Handlers handlers = { 75 .kup = kup_event, 76 .kdown = kdown_event, 77 }; 78