monitor: Drain stdin on exit

master
Raheman Vaiya 4 years ago
parent 312ab6efc0
commit 52ab139ca3
  1. 6
      src/evloop.c
  2. 1
      src/keyd.h
  3. 31
      src/monitor.c

@ -115,8 +115,10 @@ int evloop(int (*event_handler) (struct event *ev))
} }
for (i = 0; i < nr_aux_fds; i++) { for (i = 0; i < nr_aux_fds; i++) {
if (pfds[i+ndevs+1].revents) { short events = pfds[i+ndevs+1].revents;
ev.type = EV_FD_ACTIVITY;
if (events) {
ev.type = events & POLLERR ? EV_FD_ERR : EV_FD_ACTIVITY;
ev.fd = aux_fds[i]; ev.fd = aux_fds[i];
timeout = event_handler(&ev); timeout = event_handler(&ev);

@ -52,6 +52,7 @@ enum event_type {
EV_DEV_REMOVE, EV_DEV_REMOVE,
EV_DEV_EVENT, EV_DEV_EVENT,
EV_FD_ACTIVITY, EV_FD_ACTIVITY,
EV_FD_ERR,
EV_TIMEOUT, EV_TIMEOUT,
}; };

@ -1,25 +1,31 @@
#include "keyd.h" #include "keyd.h"
static void set_echo(int set) static void set_tflags(tcflag_t flags, int val)
{ {
if (!isatty(1)) if (!isatty(0))
return; return;
struct termios tinfo; struct termios tinfo;
tcgetattr(1, &tinfo); tcgetattr(0, &tinfo);
if (set) if (val)
tinfo.c_lflag |= ECHO; tinfo.c_lflag |= flags;
else else
tinfo.c_lflag &= ~ECHO; tinfo.c_lflag &= ~flags;
tcsetattr(1, TCSANOW, &tinfo); tcsetattr(0, TCSANOW, &tinfo);
} }
static void cleanup() static void cleanup()
{ {
set_echo(1); /* Drain STDIN (useful for scripting). */
set_tflags(ICANON, 0);
char buf[4096];
fcntl(0, F_SETFL, O_NONBLOCK);
while(read(0, buf, sizeof buf) > 0) {}
set_tflags(ICANON|ECHO, 1);
} }
int event_handler(struct event *ev) int event_handler(struct event *ev)
@ -47,7 +53,7 @@ int event_handler(struct event *ev)
ev->dev->product_id, name, ev->devev->pressed ? "down" : "up"); ev->dev->product_id, name, ev->devev->pressed ? "down" : "up");
} }
break; break;
case EV_FD_ACTIVITY: case EV_FD_ERR:
exit(0); exit(0);
break; break;
default: default:
@ -62,9 +68,12 @@ int event_handler(struct event *ev)
int monitor(int argc, char *argv[]) int monitor(int argc, char *argv[])
{ {
set_echo(0); if (isatty(1))
set_tflags(ECHO, 0);
evloop_add_fd(1); /* Eagerly terminate on pipe closures. */
if (!isatty(1))
evloop_add_fd(1);
setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0);

Loading…
Cancel
Save