aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dmenu.14
-rw-r--r--main.c42
2 files changed, 15 insertions, 31 deletions
diff --git a/dmenu.1 b/dmenu.1
index 2afa17c..477219a 100644
--- a/dmenu.1
+++ b/dmenu.1
@@ -10,7 +10,6 @@ dmenu \- dynamic menu
10.RB [ \-p " <prompt>"] 10.RB [ \-p " <prompt>"]
11.RB [ \-sb " <color>"] 11.RB [ \-sb " <color>"]
12.RB [ \-sf " <color>"] 12.RB [ \-sf " <color>"]
13.RB [ \-t " <seconds>"]
14.RB [ \-v ] 13.RB [ \-v ]
15.SH DESCRIPTION 14.SH DESCRIPTION
16.SS Overview 15.SS Overview
@@ -41,9 +40,6 @@ defines the selected background color (#RGB, #RRGGBB, and color names are suppor
41.B \-sf <color> 40.B \-sf <color>
42defines the selected foreground color (#RGB, #RRGGBB, and color names are supported). 41defines the selected foreground color (#RGB, #RRGGBB, and color names are supported).
43.TP 42.TP
44.B \-t <seconds>
45defines the seconds to wait for standard input, before exiting (default is 3).
46.TP
47.B \-v 43.B \-v
48prints version information to standard output, then exits. 44prints version information to standard output, then exits.
49.SH USAGE 45.SH USAGE
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;