if isatty() first read from stdin and then grab the keyboard, otherwise first grab the keyboard and then read from stdin 8a066fab
Anselm R. Garbe · 2007-02-26 11:44 1 file(s) · +17 −10
main.c +17 −10
108 108
	XFlush(dpy);
109 109
}
110 110
111 +
static void
112 +
grabkeyboard(void) {
113 +
	while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
114 +
			 GrabModeAsync, CurrentTime) != GrabSuccess)
115 +
		usleep(1000);
116 +
}
117 +
111 118
static unsigned long
112 119
initcolor(const char *colstr) {
113 120
	Colormap cmap = DefaultColormap(dpy, screen);
418 425
	XModifierKeymap *modmap;
419 426
	XSetWindowAttributes wa;
420 427
421 -
	if(argc == 2 && !strncmp("-v", argv[1], 3))
422 -
		eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
423 -
	else if(isatty(STDIN_FILENO)) {
424 -
		fputs("error: dmenu can't run in an interactive shell\n", stdout);
425 -
		usage();
426 -
	}
427 428
	/* command line args */
428 429
	for(i = 1; i < argc; i++)
429 430
		if(!strncmp(argv[i], "-b", 3)) {
447 448
		else if(!strncmp(argv[i], "-sf", 4)) {
448 449
			if(++i < argc) selfg = argv[i];
449 450
		}
451 +
		else if(!strncmp(argv[i], "-v", 3))
452 +
			eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
450 453
		else
451 454
			usage();
452 455
	setlocale(LC_CTYPE, "");
455 458
		eprint("dmenu: cannot open display\n");
456 459
	screen = DefaultScreen(dpy);
457 460
	root = RootWindow(dpy, screen);
458 -
	while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
459 -
			 GrabModeAsync, CurrentTime) != GrabSuccess)
460 -
		usleep(1000);
461 -
	maxname = readstdin();
461 +
	if(isatty(STDIN_FILENO)) {
462 +
		maxname = readstdin();
463 +
		grabkeyboard();
464 +
	}
465 +
	else { /* prevent keypress loss */
466 +
		grabkeyboard();
467 +
		maxname = readstdin();
468 +
	}
462 469
	/* init modifier map */
463 470
	modmap = XGetModifierMapping(dpy);
464 471
	for (i = 0; i < 8; i++) {