aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c42
1 files changed, 15 insertions, 27 deletions
diff --git a/main.c b/main.c
index 0840643..54a3a12 100644
--- a/main.c
+++ b/main.c
@@ -10,8 +10,6 @@
10#include <stdio.h> 10#include <stdio.h>
11#include <string.h> 11#include <string.h>
12#include <unistd.h> 12#include <unistd.h>
13#include <sys/select.h>
14#include <sys/time.h>
15#include <X11/Xutil.h> 13#include <X11/Xutil.h>
16#include <X11/keysym.h> 14#include <X11/keysym.h>
17 15
@@ -457,6 +455,12 @@ readstdin(void) {
457 return maxname; 455 return maxname;
458} 456}
459 457
458static void
459usage(void) {
460 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
461 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
462}
463
460/* extern */ 464/* extern */
461 465
462int screen; 466int screen;
@@ -472,16 +476,16 @@ main(int argc, char *argv[]) {
472 char *normfg = NORMFGCOLOR; 476 char *normfg = NORMFGCOLOR;
473 char *selbg = SELBGCOLOR; 477 char *selbg = SELBGCOLOR;
474 char *selfg = SELFGCOLOR; 478 char *selfg = SELFGCOLOR;
475 fd_set rd;
476 int i, j; 479 int i, j;
477 struct timeval timeout;
478 Item *itm; 480 Item *itm;
479 XEvent ev; 481 XEvent ev;
480 XModifierKeymap *modmap; 482 XModifierKeymap *modmap;
481 XSetWindowAttributes wa; 483 XSetWindowAttributes wa;
482 484
483 timeout.tv_usec = 0; 485 if(isatty(STDIN_FILENO)) {
484 timeout.tv_sec = 3; 486 fputs("error: dmenu can't run in an interactive shell\n", stdout);
487 usage();
488 }
485 /* command line args */ 489 /* command line args */
486 for(i = 1; i < argc; i++) 490 for(i = 1; i < argc; i++)
487 if(!strncmp(argv[i], "-b", 3)) { 491 if(!strncmp(argv[i], "-b", 3)) {
@@ -505,41 +509,26 @@ main(int argc, char *argv[]) {
505 else if(!strncmp(argv[i], "-sf", 4)) { 509 else if(!strncmp(argv[i], "-sf", 4)) {
506 if(++i < argc) selfg = argv[i]; 510 if(++i < argc) selfg = argv[i];
507 } 511 }
508 else if(!strncmp(argv[i], "-t", 3)) { 512 else if(!strncmp(argv[i], "-v", 3))
509 if(++i < argc) timeout.tv_sec = atoi(argv[i]); 513 eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
510 }
511 else if(!strncmp(argv[i], "-v", 3)) {
512 fputs("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
513 exit(EXIT_SUCCESS);
514 }
515 else 514 else
516 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>] [-p <prompt>]\n" 515 usage();
517 " [-sb <color>] [-sf <color>] [-t <seconds>] [-v]\n", stdout);
518 setlocale(LC_CTYPE, ""); 516 setlocale(LC_CTYPE, "");
519 dpy = XOpenDisplay(0); 517 dpy = XOpenDisplay(0);
520 if(!dpy) 518 if(!dpy)
521 eprint("dmenu: cannot open display\n"); 519 eprint("dmenu: cannot open display\n");
522 screen = DefaultScreen(dpy); 520 screen = DefaultScreen(dpy);
523 root = RootWindow(dpy, screen); 521 root = RootWindow(dpy, screen);
524
525 /* Note, the select() construction allows to grab all keypresses as
526 * early as possible, to not loose them. But if there is no standard
527 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
528 * seconds. This is convenience behavior for rapid typers.
529 */
530 while(XGrabKeyboard(dpy, root, True, GrabModeAsync, 522 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
531 GrabModeAsync, CurrentTime) != GrabSuccess) 523 GrabModeAsync, CurrentTime) != GrabSuccess)
532 usleep(1000); 524 usleep(1000);
533 FD_ZERO(&rd);
534 FD_SET(STDIN_FILENO, &rd);
535 if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
536 goto UninitializedEnd;
537 maxname = readstdin(); 525 maxname = readstdin();
538 /* init modifier map */ 526 /* init modifier map */
539 modmap = XGetModifierMapping(dpy); 527 modmap = XGetModifierMapping(dpy);
540 for (i = 0; i < 8; i++) { 528 for (i = 0; i < 8; i++) {
541 for (j = 0; j < modmap->max_keypermod; j++) { 529 for (j = 0; j < modmap->max_keypermod; j++) {
542 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) 530 if(modmap->modifiermap[i * modmap->max_keypermod + j]
531 == XKeysymToKeycode(dpy, XK_Num_Lock))
543 numlockmask = (1 << i); 532 numlockmask = (1 << i);
544 } 533 }
545 } 534 }
@@ -607,7 +596,6 @@ main(int argc, char *argv[]) {
607 XFreePixmap(dpy, dc.drawable); 596 XFreePixmap(dpy, dc.drawable);
608 XFreeGC(dpy, dc.gc); 597 XFreeGC(dpy, dc.gc);
609 XDestroyWindow(dpy, win); 598 XDestroyWindow(dpy, win);
610UninitializedEnd:
611 XUngrabKeyboard(dpy, CurrentTime); 599 XUngrabKeyboard(dpy, CurrentTime);
612 XCloseDisplay(dpy); 600 XCloseDisplay(dpy);
613 return ret; 601 return ret;