chiark / gitweb /
new --stdin-monitor option
authorian <ian>
Sat, 7 Jun 2008 22:51:42 +0000 (22:51 +0000)
committerian <ian>
Sat, 7 Jun 2008 22:51:42 +0000 (22:51 +0000)
hostside/evdev-manip.c

index 84a04d0dcf2494517b139d9461395bf8e7d95e5b..34ec916509bb7a3831e9fe42d1dfe1499d214114 100644 (file)
@@ -7,6 +7,7 @@
  *                        ^^^^^^^^^^^^^^^^^^^^
  *                          this part is in /proc/bus/usb/devices
  *                           and can thus be specified by caller
+ *     --stdin-monitor
  */
 
 #include "common.h"
@@ -36,7 +37,7 @@ static int ndevices;
 static Device *devices;
 
 static Mode mode;
-static int grab;
+static int grab, stdinmonitor;
 static const char *expect_sysfs;
 
 static void pr_hex(unsigned long value) { printf("%#lx",value); }
@@ -128,25 +129,37 @@ static void dump_died(Device *d, int revents, int readr, int readc, int e) {
 
 static void mainloop(void) {
   struct pollfd *polls;
-  int i, r;
+  int i, r, npolls;
 
-  polls= mmalloc(sizeof(*polls)*ndevices);
+  npolls= ndevices + stdinmonitor;
+  polls= mmalloc(sizeof(*polls)*npolls);
   for (i=0; i<ndevices; i++) {
     polls[i].fd= devices[i].fd;
     polls[i].events= POLLIN;
   }
+  if (stdinmonitor) {
+    polls[ndevices].fd= 0;
+    polls[ndevices].events= POLLIN;
+  }
 
   for (;;) {
-    for (i=0; i<ndevices; i++)
+    for (i=0; i<npolls; i++)
       polls[i].revents= 0;
 
-    r= poll(polls,ndevices,-1);
+    r= poll(polls,npolls,-1);
     if (r==-1) {
       if (errno==EINTR) continue;
       diee("poll failed");
     }
     assert(r>0);
 
+    if (stdinmonitor) {
+      if (polls[ndevices].revents) {
+       printf("quitting-stdin-polled %#x\n", polls[ndevices].revents);
+       exit(0);
+      }
+    }
+
     for (i=0; i<ndevices; i++) {
       if (polls[i].revents & ~POLLIN) {
        unsigned char dummy;
@@ -240,6 +253,7 @@ int main(int argc, const char **argv) {
     else if (!strcmp(arg,"--dump")) { mode= &mode_dump; }
     else if (!strcmp(arg,"--grab")) { grab= 1; }
     else if (!strcmp(arg,"--no-grab")) { grab= 0; }
+    else if (!strcmp(arg,"--stdin-monitor")) { stdinmonitor= 1; }
     else badusage("unknown option");
   }
   mode->mainloop();