From 9dd76e3ed65499ab191de4e48ebf6c7c60de21f0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 19 Feb 2011 19:32:44 +0000 Subject: [PATCH] hostside: hidrawconv: break out dispatch() --- hostside/hidrawconv.c | 43 +++++++++++++++++++++++++------------------ hostside/hidrawconv.h | 6 ++++++ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/hostside/hidrawconv.c b/hostside/hidrawconv.c index 762133e..6151b0a 100644 --- a/hostside/hidrawconv.c +++ b/hostside/hidrawconv.c @@ -17,8 +17,7 @@ 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]; +static LastReports lasts; void reportbits(const uint8_t msg[], const uint8_t last[], int len, const KeyBit *bit) { @@ -45,30 +44,38 @@ void reportlocs(const uint8_t msg[], const uint8_t last[], printf("%s %.5f\n", loc->str, loc->sign * val); } } - + +void dispatch(LastReports *lasts, const char *message_prefix, + ProcessReport *const report_processors[MAXREPORTS], + const uint8_t *msg, int l) { + ProcessReport *pr= report_processors[msg[0]]; + Last *last= &lasts->lasts[msg[0]]; + if (!pr) { + if (!last->len) + fprintf(stderr,"%s:%s unexpected report 0x%02x\n", + progname, message_prefix, msg[0]); + last->len= l; + return; + } + 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); + memcpy(last->msg, msg, l); +} + 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\n", 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); + dispatch(&lasts,"",report_processors, msg,l); if (ferror(stdout) || fflush(stdout)) diee("failed flushing event to stdout"); - memcpy(last->msg, msg, l); } } diff --git a/hostside/hidrawconv.h b/hostside/hidrawconv.h index 5380a6c..2fdfdb2 100644 --- a/hostside/hidrawconv.h +++ b/hostside/hidrawconv.h @@ -9,6 +9,8 @@ #define MAXREPORTLEN 256 #define MAXREPORTS 256 +typedef struct { int len; uint8_t *msg; } Last; +typedef struct { Last lasts[MAXREPORTS]; } LastReports; typedef void ProcessReport(const uint8_t *msg, int msglen, const uint8_t *last); extern const char *const descriptor; @@ -32,4 +34,8 @@ void reportbits(const uint8_t msg[], const uint8_t last[], void reportlocs(const uint8_t msg[], const uint8_t last[], int len, const ValLoc locs[]); +void dispatch(LastReports *lasts, const char *message_prefix, + ProcessReport *const report_processors[MAXREPORTS], + const uint8_t *msg, int l); + #endif /*HIDRAWCONV_H*/ -- 2.30.2