chiark / gitweb /
evdev-manip builds but does not work
authorian <ian>
Thu, 5 Jun 2008 19:22:11 +0000 (19:22 +0000)
committerian <ian>
Thu, 5 Jun 2008 19:22:11 +0000 (19:22 +0000)
hostside/.cvsignore
hostside/Makefile
hostside/common.h
hostside/evdev-manip.c
hostside/extract-input-codes
hostside/realtime.h

index 89bed14076415b0d4f7ba5563db8020eacb16845..54662ce399f594a41c68bd2c8ebd9a266cd19d8b 100644 (file)
@@ -25,3 +25,5 @@ record-y.[ch]
 +realtime.stderr
 *+dflags.h
 *.new
+evdev-manip
+input-codes.h
index 3963d7cbe44b07a45b2dac95aed4b13ebd503e9d..cd51b261021dd83bab7a6d2ab5b897df0080ed2d 100644 (file)
@@ -4,11 +4,14 @@ TRAINS=               santafe shinkansen
 
 AUTOINCS=      auproto-pic.h layoutinfo.h selectors.h retransmit-table.h \
                errorcodes.h stastate.h record-y.h record-l.h \
-               realtime+dflags.h
+               realtime+dflags.h input-codes.h
 TARGETS=       hostside-old gui-plan-bot realtime topology-dump \
+               evdev-manip \
                $(addsuffix .speeds.ps, $(TRAINS)) \
                $(addsuffix .speeds.record, $(TRAINS))
 
+INPUT_H=       /usr/include/linux/input.h
+
 include ../common.make
 include ../cprogs.make
 
@@ -73,9 +76,14 @@ realtime+dflags.h: debug-extractor $(addsuffix .c, $(REALTIME_CORE))
                ./$^ >$@.new
                cmp $@ $@.new || mv -f $@.new $@
 
+evdev-manip:   evdev-manip.o utils.o
+
 safety:                safety.o utils.o trackloc.o ../layout/ours.layout-data.o
                $(LINK)
 
+input-codes.h: extract-input-codes $(INPUT_H)
+               ./$^ $o
+
 %.speeds.ps %.speeds.record: ./analyse-speeds \
        ../layout/ours.redacted.shellvars %.manual
                ./$< $*
index 5c4208c91753cc71f4f0c3b4b93f6cc6390a1dd2..da3c8f9f500b9db5ecd58ca26ed368b96e08eea1 100644 (file)
@@ -9,8 +9,17 @@
 #define COMMON_H
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/time.h>
+#include <errno.h>
+#include <unistd.h>
+#include <assert.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <string.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <stddef.h>
+#include <ctype.h>
+#include <math.h>
 
 typedef struct ParseState ParseState;
 typedef struct CmdInfo CmdInfo;
index b2651ac5deb11a31594e2d21188fd6d37e0590af..6fa2c636c3e260cd6c60b3f5ebfe7bcf002cbe04 100644 (file)
@@ -5,29 +5,71 @@
  *     --[no-]grab     --nograb is default
  */
 
-#include <input.h>
-
 #include "common.h"
 
-typedef struct {
-  void (*onevent)();
-  void (*mainloop)();
-} ModeInfo;
+#include <poll.h>
+#include <sys/fcntl.h>
 
-typedef const ModeInfo *Mode;
+#include <linux/input.h>
+#include "input-codes.h" /* not really a header */
 
 typedef struct {
   char *path;
   int fd;
 } Device;
 
+typedef struct {
+  void (*event)(Device *d, const struct input_event *ie);
+  void (*mainloop)(void);
+} ModeInfo;
+
+typedef const ModeInfo *Mode;
+
 static int ndevices;
 static Device *devices;
 
 static Mode mode;
 static int grab;
 
-static void mode_dump(void) {
+static void pr_hexdec(unsigned long value) { printf("%#lx %ld",value,value); }
+
+#define PR_TABLE_STR(tab, val) (pr_table_str(iesis_##tab, iesin_##tab, (val)))
+static void pr_table_str(const InputEventStringInfo *strings, int nstrings,
+                        unsigned long value) {
+  const InputEventStringInfo *string;
+  if (value > nstrings) { pr_hexdec(value); return; }
+  string= &strings[value];
+  if (!string->prefix) { pr_hexdec(value); return; }
+  printf("%s %s", string->prefix, string->main);
+}
+
+static void dump_event(Device *d, const struct input_event *ie) {
+  const InputEventTypeInfo *t;
+
+  printf("event ");
+  PR_TABLE_STR(ev, ie->type);
+
+  printf(" ");
+  if (ie->type >= IETIN) {
+    t= 0;
+  } else {
+    t= &ietis[ie->type];
+    if (!t->strings) t= 0;
+  }
+  if (t) pr_table_str(t->strings, t->nstrings, ie->code);
+  else pr_hexdec(ie->code);
+
+  printf(" ");
+  switch (ie->type) {
+  case EV_ABS:
+  case EV_REL:
+    printf("%ld",(long)ie->value);
+    break;
+  default:
+    printf("%lx",(unsigned long)ie->value);
+    break;
+  }
+  printf("\n");
 }
 
 static void process_device(Device *d) {
@@ -38,7 +80,7 @@ static void process_device(Device *d) {
   printf("event-group-start device %s\n",d->path);
 
   for (;;) {
-    for (p=&ie, remain=sizeof(ie);
+    for (p=(void*)&ie, remain=sizeof(ie);
         remain;
         p+=r, remain-=r) {
       r= read(d->fd, &ie, remain);
@@ -53,14 +95,18 @@ static void process_device(Device *d) {
     if (ie.type == EV_SYN) {
       printf("synchronised %ju.%06d\n",
             (uintmax_t)ie.time.tv_sec,
-            (int)ie.time_tv_usec);
+            (int)ie.time.tv_usec);
       break;
     }
-    
+
+    mode->event(d, &ie);
+  }
+  mflushstdout();
 }
 
 static void mainloop(void) {
   struct pollfd *polls;
+  int i, r;
 
   polls= mmalloc(sizeof(*polls)*ndevices);
   for (i=0; i<ndevices; i++) {
@@ -72,7 +118,7 @@ static void mainloop(void) {
     for (i=0; i<ndevices; i++)
       polls[i].revents= 0;
 
-    r= poll(&polls,ndevices,-1);
+    r= poll(polls,ndevices,-1);
     if (r==-1) {
       if (errno==EINTR) continue;
       diee("poll failed");
@@ -80,15 +126,17 @@ static void mainloop(void) {
     assert(r>0);
 
     for (i=0; i<ndevices; i++) {
-      if (polls[i].revent & ~POLLIN)
+      if (polls[i].revents & ~POLLIN)
        die("device %s (fd %d) gave unexpected poll revent %#x",
-           devices[i].path, devices[i].fd, polls[i].revent);
-      if (polls[i].revent)
+           devices[i].path, devices[i].fd, polls[i].revents);
+      if (polls[i].revents)
        process_device(&devices[i]);
     }
   }
 }
 
+static const ModeInfo mode_dump= { dump_event, mainloop };
+
 static void getdevice(const char *path) {
   int r;
   struct input_id iid;
@@ -101,8 +149,10 @@ static void getdevice(const char *path) {
   d->fd= open(path, O_RDONLY);  if (d->fd<0) diee("%s: failed to open",path);
 
   r= ioctl(d->fd, EVIOCGID, &iid);  if (r) diee("%s: failed to get id",path);
-  printf("device %s bustype %#x vendor %#x product #%x version %#x\n"
-        path, iid.bustype, iid.vendor, iid.product, iid.version);
+  printf("device %s bustype ", path);
+  PR_TABLE_STR(bus, iid.bustype);
+  printf(" vendor %#x product #%x version %#x\n",
+        iid.vendor, iid.product, iid.version);
   mflushstdout();
 
   if (grab)
@@ -116,11 +166,15 @@ int main(int argc, const char **argv) {
     if (arg[0] != '-') {
       getdevice(arg);
     }
-    else if (!strcmp(arg,"--dump")) { mode= mode_dump; }
+    else if (!strcmp(arg,"--dump")) { mode= &mode_dump; }
     else if (!strcmp(arg,"--grab")) { grab= 1; }
     else if (!strcmp(arg,"--no-grab")) { grab= 0; }
-    else badusage("unknown option `%s'",arg);
+    else badusage("unknown option");
   }
   mode->mainloop();
   return 0;
 }
+
+const char *progname= "evdev-manip";
+void die_hook(void) { }
+void die_vprintf_hook(const char *fmt, va_list al) { }
index 7f6b645b2769eecee1f5c7653199a1393d1a796b..f95b222a96a7942778969f72da0e5dd3e392b76b 100755 (executable)
@@ -4,7 +4,7 @@ use strict 'vars';
 
 # "/usr/include/linux/input.h
 
-our (%strs, %checkunique);
+our (%strs, %checkunique, %tabaliases, %evtypes);
 
 sub strs ($) {
     my ($t) = @_;
@@ -12,6 +12,7 @@ sub strs ($) {
 }
 sub evtype ($) {
     my ($t) = @_;
+    $evtypes{$t}= 1;
     strs($t);
 }
 
@@ -22,9 +23,9 @@ foreach $t (qw(EV BUS)) {
 foreach $t (qw(KEY REL ABS MSC LED)) {
     evtype($t);
 }
-$strs{'BTN'}= $strs{'KEY'};
+$tabaliases{'BTN'}= 'KEY';
 
-our ($tab,$str,$val);
+our ($tab,$str,$val,$s);
 
 while (<>) {
     if (m/^\#define (\w+)_(\w+)\s+(0|[1-9]\d+|0x[0-9a-f]+)\s*$/) {
@@ -32,24 +33,89 @@ while (<>) {
        next if "${tab}_${str}" =~
            m/^BTN_(?:MISC|MOUSE|JOYSTICK|GAMEPAD|DIGI)$|^EV_VERSION$/;
        next if $str eq 'MAX';
-       if (exists($strs{$tab})) {
-           die "$tab $str $val" if defined $strs{$tab}[$val];
-           $strs{$tab}[$val]= $str;
+       $t= $tabaliases{$tab} || $tab;
+       $s= $strs{$t};
+       if (defined $s) {
+           die "$tab $str $val" if defined $s->[$val];
+           $s->[$val]= [ $tab, $str ];
        }
-       die "$tab $str $val" if $checkunique{$tab}{$str}++;
+       die "$tab $str $val" if $checkunique{$t}{"$tab $str"}++;
     }
 }
 
-our ($s,$i);
+our ($i,$iesis,$iesin,$n,$si);
+
+print <<END
+typedef struct {
+  const char *prefix, *main;
+} InputEventStringInfo;
+
+typedef struct {
+  int nstrings;
+  const InputEventStringInfo *strings;
+} InputEventTypeInfo;
+
+END
+    or die $!;
+
+sub print_i () {
+    printf "    /* %4d %#6x */ ", $i,$i or die $!;
+}
 
 for $t (sort keys %strs) {
-    printf "static const char *iesis_%s[]= {\n", lc $t
-       or die $!;
     $s= $strs{$t};
+    $n= @$s;
+    $iesin= "IESIN_$t";
+    $iesis= "iesis_\L$t";
+    print <<END
+#define $iesin $n
+static const int \L$iesin\E= $n;
+static const InputEventStringInfo ${iesis}[$iesin] = {
+END
+        or die $!;
     for ($i=0; $i<@$s; $i++) {
-       printf "  %s,\n", (defined $s->[$i] ? "\"$s->[$i]\"" : "0")
-           or die $!;
+       $si= $s->[$i];
+       print_i();
+       if (defined $si) {
+           printf "  { %10s %-25s },\n", "\"$si->[0]\",", "\"$si->[1]\""
+               or die $!;
+       } else {
+           print "  { 0 },\n"
+               or die $!;
+       }
     }
     printf "};\n"
        or die $!;
 }
+
+for $tab (sort keys %tabaliases) {
+    $t= $tabaliases{$tab};
+    print <<END
+#define IESIN_${tab} IESIN_${t}
+#define iesis_\L${tab} iesis_${t}
+END
+        or die $!;
+}
+
+$s= $strs{'EV'};
+print <<END
+#define IETIN IESIN_EV
+static const InputEventTypeInfo ietis[IETIN]= {
+END
+    or die $!;
+
+our (@sip);
+
+for ($i=0; $i<@$s; $i++) {
+    $si= $s->[$i];
+    $tab= defined $si ? $si->[1] : undef;
+    $tab= $tab && exists $evtypes{$tab} ? $tab : undef;
+    @sip= $tab ? ("IESIN_$tab", "iesis_\L$tab") : ("-1", "0");
+    print_i();
+    printf "  { %-21s %-20s },\n", "$sip[0],", $sip[1]
+       or die $!;
+}
+print <<END
+};
+END
+    or die $!;
index d4d3fafa6a8663f23230b1e51c7655116552e717..6b049f340a8cdad40b39a56caff54ff97f4fb322 100644 (file)
@@ -9,25 +9,16 @@
 #include "auproto-pic.h"
 #include "dliste.h"
 
-#include <stdarg.h>
-#include <string.h>
-#include <errno.h>
-#include <assert.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <limits.h>
-#include <stddef.h>
-#include <ctype.h>
-#include <math.h>
-
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#include <unistd.h>
 #include <fcntl.h>
 #include <dirent.h>
 
+#include <sys/types.h>
+#include <sys/time.h>
+
 #include "../layout/layout-data.h"
 #include "realtime+dflags.h"