chiark / gitweb /
hostside/hidrawconv*: tools for converting hidraw according to our own idea of how...
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 5 Feb 2011 02:15:08 +0000 (02:15 +0000)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 5 Feb 2011 02:15:08 +0000 (02:15 +0000)
hostside/.gitignore
hostside/Makefile
hostside/hidrawconv-hex.c [new file with mode: 0644]
hostside/hidrawconv-joytechneos.c [new file with mode: 0644]
hostside/hidrawconv.c [new file with mode: 0644]
hostside/hidrawconv.h [new file with mode: 0644]

index b47722d0d0f4ca5c7f731b0b3270023bdbc599ed..ee2ae0b333b4356b8ff083840462748f265256b8 100644 (file)
@@ -31,3 +31,5 @@ input-codes.h
 hiddev-ioctl
 hidraw-ioctl
 kdled-ioctl
+hidrawconv-hex
+hidrawconv-joytechneos
index ce2d65f6ed803fdac8b33af1c3ded72337695b44..b49f79209e4d9d769c925990446c7ff7539273ee 100644 (file)
@@ -6,7 +6,10 @@ AUTOINCS=      auproto-pic.h layoutinfo.h retransmit-table.h           \
                errorcodes.h stastate.h record-y.h record-l.h           \
                realtime+dflags.h input-codes.h
 
-CLIENT_TARGETS=        gui-plan-bot topology-dump evdev-manip hidraw-ioctl
+HIDRAWCONVS=   hex joytechneos
+
+CLIENT_TARGETS=        gui-plan-bot topology-dump evdev-manip hidraw-ioctl \
+               $(addprefix hidrawconv-, $(HIDRAWCONVS))
 
 TARGETS=       adhoc-test realtime hiddev-ioctl kdled-ioctl            \
                $(CLIENT_TARGETS)                                       \
@@ -43,6 +46,9 @@ hidraw-ioctl: hidraw-ioctl.o utils.o
 hiddev-ioctl:  hiddev-ioctl.o utils.o
 kdled-ioctl:   kdled-ioctl.o
 
+hidrawconv-%:  hidrawconv-%.o hidrawconv.o utils.o
+               $(LINK)
+
 gui-plan-bot: gui-plan-%:  gui-plan.o utils.o parseutils.o obc.o       \
                        ../layout/ours.dgram-%.plandata.o               \
                        $(LAYOUT_DATA).o $(LIBOOP_OBJS)
diff --git a/hostside/hidrawconv-hex.c b/hostside/hidrawconv-hex.c
new file mode 100644 (file)
index 0000000..e4dbf85
--- /dev/null
@@ -0,0 +1,33 @@
+#include "hidrawconv.h"
+
+static void pr(uint8_t msg[MAXREPORTLEN], int msglen,
+              const uint8_t last[MAXREPORTLEN]) {
+  dumphex(stdout, msg, msglen);
+  putchar('\n');
+}
+
+const char *progname= "hidrawconv-hex";
+
+const char *const descriptor= "?";
+
+ProcessReport *const report_processors[MAXREPORTS]= {
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+  pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr, pr,pr,pr,pr,
+};
diff --git a/hostside/hidrawconv-joytechneos.c b/hostside/hidrawconv-joytechneos.c
new file mode 100644 (file)
index 0000000..6b39458
--- /dev/null
@@ -0,0 +1,40 @@
+#include "hidrawconv.h"
+
+static const KeyBit keybits[]= {
+  { "EV_KEY BTN_1",          6, 0x01 },
+  { "EV_KEY BTN_2",          6, 0x02 },
+  { "EV_KEY BTN_3",          6, 0x04 },
+  { "EV_KEY BTN_4",          6, 0x08 },
+  { "EV_KEY BTN_5",          6, 0x10 },
+  { "EV_KEY BTN_6",          6, 0x20 },
+  { "EV_KEY BTN_9",          7, 0x01 },
+  { "EV_KEY BTN_10",         7, 0x02 },
+  { "EV_KEY TRIG_L",         6, 0x40 },
+  { "EV_KEY TRIG_R",         6, 0x80 },
+  { "EV_KEY PUSH_THROTTLE",  7, 0x04 },
+  { "EV_KEY PUSH_XY",        7, 0x08 },
+  { 0 }
+};
+static const ValLoc vallocs[]= {
+  { "EV_ABS ABS_X",          1,0,-1, 0xff, 0x7f },
+  { "EV_ABS ABS_Y",          2,0,-1, 0xff, 0x80 },
+  { "EV_ABS ABS_RUDDER",     3,0,+1, 0xff, 0x7f },
+  { "EV_ABS ABS_THROTTLE",   4,0,-1, 0xff, 0x80 },
+  { "EV_ABS DPAD",           5,0,+1, 0x0f, 0x00 },
+  { 0 }
+};
+
+static void pr(uint8_t msg[MAXREPORTLEN], int msglen,
+              const uint8_t last[MAXREPORTLEN]) {
+  reportbits(msg, last, msglen, keybits);
+  reportlocs(msg, last, msglen, vallocs);
+}
+
+const char *progname= "hidrawconv-joytechneos";
+
+const char *const descriptor=
+  "05010904a101854a0901a1000501093009311580257f350045ff660000750895028102c0050209ba150026ff00750895018102050209bb150026ff007508950181020501093915012508360000463b016514750895018102950c7501050945013500150025011901290c810295040605ff09018102c005140924a101854d150026ff000931750895079182c005010906a101854b050719e029e71500250175019508810295017508810195057501050819012905910295017503910195057508150025680507190029688100c0";
+
+ProcessReport *const report_processors[MAXREPORTS]= {
+ [0x4a] = pr
+};
diff --git a/hostside/hidrawconv.c b/hostside/hidrawconv.c
new file mode 100644 (file)
index 0000000..738a153
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * usage:
+ *   .../hidrawconv-<controller> -d
+ *   .../hidrawconv-<controller> -e </dev/hidrawN
+ * where -a means all, and the other letters are:
+ *   -d   print expected descriptor (as for hidraw-ioctl -d)
+ *   -e   pretend to be evdev-manip
+ * exit status:
+ *   0      all ok
+ *   other  some other problem
+ */
+
+#include "hidrawconv.h"
+
+void die_vprintf_hook(const char *fmt, va_list al) { }
+void die_hook(void) { }
+
+typedef struct { int len; uint8_t *msg; } Last;
+static Last lasts[MAXREPORTS];
+
+void reportbits(const uint8_t msg[], const uint8_t last[],
+               int len, const KeyBit *bit) {
+  for (; bit->str; bit++) {
+    if (bit->pos >= len) continue;
+    uint8_t m= msg[bit->pos] & bit->mask;
+    uint8_t l= last[bit->pos] & bit->mask;
+    if (m==l) continue;
+    printf("%s %d\n", bit->str, !!m);
+  }
+}
+
+void reportlocs(const uint8_t msg[], const uint8_t last[],
+               int len, const ValLoc *loc) {
+  for (; loc->str; loc++) {
+    if (loc->pos >= len) continue;
+    uint8_t mb= msg[loc->pos] & loc->mask;
+    uint8_t lb= last[loc->pos] & loc->mask;
+    if (mb==lb) continue;
+    mb >>= loc->rshift;
+    mb -= loc->zero;
+    printf("%s %d\n", loc->str, loc->sign * (int8_t)mb);
+  }
+}
+  
+static void events(void) {
+  uint8_t msg[MAXREPORTLEN];
+  for (;;) {
+    int l= read(0, msg, sizeof(msg));
+    if (!l) break;
+    if (l<0) { perror("hidrawconv: read"); exit(-1); }
+    ProcessReport *pr= report_processors[msg[0]];
+    Last *last= &lasts[msg[0]];
+    if (!pr) {
+      if (!last->len)
+       fprintf(stderr,"%s: unexpected report 0x%02x", progname, msg[0]);
+      last->len= l;
+      continue;
+    }
+    if (last->len < l) {
+      last->msg= mrealloc(last->msg, l);
+      memset(last->msg + last->len, 0, l - last->len);
+      last->len= l;
+    }
+    pr(msg, l, last->msg);
+    if (ferror(stdout) || fflush(stdout))
+      diee("failed flushing event to stdout");
+    memcpy(last->msg, msg, l);
+  }
+}
+
+int main(int argc, char **argv) {
+  const char *how;
+
+  if (!*argv || !(how=*++argv) || *how++!='-' || !*how || how[1] || *++argv)
+    badusage("need exactly one argument, -d or -e");
+
+  switch (how[0]) {
+  case 'd':
+    puts(descriptor);
+    break;
+
+  case 'e':
+    events();
+    break;
+
+  default:
+    badusage("unknown option/mode");
+  }
+
+  if (ferror(stdout) || fflush(stdout))
+    diee("write/flush stdout");
+
+  return 0;
+}
diff --git a/hostside/hidrawconv.h b/hostside/hidrawconv.h
new file mode 100644 (file)
index 0000000..9e34f78
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef HIDRAWCONV_H
+#define HIDRAWCONV_H
+
+#include <stdio.h>
+#include <stdint.h>
+
+#include "common.h"
+
+#define MAXREPORTLEN 256
+#define MAXREPORTS 256
+
+typedef void ProcessReport(uint8_t msg[MAXREPORTLEN], int msglen,
+                          const uint8_t last[MAXREPORTLEN]);
+
+extern const char *const descriptor;
+extern ProcessReport *const report_processors[MAXREPORTS];
+
+typedef struct {
+  const char *str;
+  int pos;
+  uint8_t mask;
+} KeyBit;
+
+typedef struct {
+  const char *str;
+  int pos, rshift, sign; /* mask is applied before rshift */
+  uint8_t mask, zero;
+} ValLoc;
+
+void reportbits(const uint8_t msg[], const uint8_t last[],
+               int len, const KeyBit bits[]);
+
+void reportlocs(const uint8_t msg[], const uint8_t last[],
+               int len, const ValLoc locs[]);
+
+#endif /*HIDRAWCONV_H*/