[hackers] [dmenu] if isatty() first read from stdin and then grab the keyboard, otherwise first grab the keyboard and then read from stdin

From: Anselm R. Garbe <arg_AT_suckless.org>
Date: Mon Feb 26 11:46:53 2007

changeset: 182:8ea08a5e0735
tag: tip
user: Anselm R. Garbe <arg_AT_suckless.org>
date: Mon Feb 26 11:44:41 2007 +0100
summary: if isatty() first read from stdin and then grab the keyboard, otherwise first grab the keyboard and then read from stdin

diff -r f584f5a0427a -r 8ea08a5e0735 main.c
--- a/main.c Mon Feb 26 10:47:52 2007 +0100
+++ b/main.c Mon Feb 26 11:44:41 2007 +0100
@@ -106,6 +106,13 @@ drawmenu(void) {
         }
         XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
         XFlush(dpy);
+}
+
+static void
+grabkeyboard(void) {
+ while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
+ GrabModeAsync, CurrentTime) != GrabSuccess)
+ usleep(1000);
 }
 
 static unsigned long
@@ -418,12 +425,6 @@ main(int argc, char *argv[]) {
         XModifierKeymap *modmap;
         XSetWindowAttributes wa;
 
- if(argc == 2 && !strncmp("-v", argv[1], 3))
- eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
- else if(isatty(STDIN_FILENO)) {
- fputs("error: dmenu can't run in an interactive shell\n", stdout);
- usage();
- }
         /* command line args */
         for(i = 1; i < argc; i++)
                 if(!strncmp(argv[i], "-b", 3)) {
@@ -447,6 +448,8 @@ main(int argc, char *argv[]) {
                 else if(!strncmp(argv[i], "-sf", 4)) {
                         if(++i < argc) selfg = argv[i];
                 }
+ else if(!strncmp(argv[i], "-v", 3))
+ eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
                 else
                         usage();
         setlocale(LC_CTYPE, "");
@@ -455,10 +458,14 @@ main(int argc, char *argv[]) {
                 eprint("dmenu: cannot open display\n");
         screen = DefaultScreen(dpy);
         root = RootWindow(dpy, screen);
- while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
- GrabModeAsync, CurrentTime) != GrabSuccess)
- usleep(1000);
- maxname = readstdin();
+ if(isatty(STDIN_FILENO)) {
+ maxname = readstdin();
+ grabkeyboard();
+ }
+ else { /* prevent keypress loss */
+ grabkeyboard();
+ maxname = readstdin();
+ }
         /* init modifier map */
         modmap = XGetModifierMapping(dpy);
         for (i = 0; i < 8; i++) {
Received on Mon Feb 26 2007 - 11:46:53 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:55:57 UTC