From 4e0224f32f6a9db613f73706f102c49150c0a631 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Sat, 24 Dec 2011 11:59:40 +0100 Subject: [PATCH] restructuring, guarantee permutation property --- src/main.c | 212 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 145 insertions(+), 67 deletions(-) diff --git a/src/main.c b/src/main.c index 93362df..aa79461 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,8 @@ #include "config.h" -#define MAXFN 1024 +#define BTN_CNT (KEY_MAX - BTN_MISC + 1) + #define EXIT_INVARG 1 #define EXIT_OPEN 2 #define EXIT_IOCTL 3 @@ -48,21 +49,37 @@ void printHelp(FILE* out, int argc, char** argv) } -void listAxismap(int fd) +__u8 getNumberOfAxes(int fd) { __u8 cnt=0; - __u8 map[ABS_CNT] = {-1}; - - if (ioctl(fd, JSIOCGAXES, &cnt) ) { perror(PACKAGE_STRING ": error getting number of axes"); exit(EXIT_IOCTL); } + return cnt; +} + + +__u8 getNumberOfButtons(int fd) +{ + __u8 cnt=0; + if (ioctl(fd, JSIOCGBUTTONS, &cnt) ) + { + perror(PACKAGE_STRING ": error getting number of buttons"); + exit(EXIT_IOCTL); + } + return cnt; +} + + +void listAxismap(int fd) +{ + __u8 map[ABS_CNT] = {-1}; + const __u8 cnt = getNumberOfAxes(fd); printf("Got %d axes:\n", cnt); assert( cnt <= ABS_CNT); - if( ioctl(fd, JSIOCGAXMAP, &map) < 0 ) { perror(PACKAGE_STRING ": error getting axis map"); @@ -87,17 +104,10 @@ void listAxismap(int fd) void listButtonmap(int fd) { - __u8 cnt=0; - __u16 map[KEY_MAX - BTN_MISC + 1] = {-1}; - - if (ioctl(fd, JSIOCGBUTTONS, &cnt) ) - { - perror(PACKAGE_STRING ": error getting number of buttons"); - exit(EXIT_IOCTL); - } + __u16 map[BTN_CNT] = {-1}; + const __u8 cnt = getNumberOfButtons(fd); printf("Got %d buttons:\n", cnt); - if( ioctl(fd, JSIOCGBTNMAP, &map) < 0 ) { perror(PACKAGE_STRING ": error getting button map"); @@ -120,47 +130,116 @@ void listButtonmap(int fd) } -void setAxismap(int fd, char* mapstr) +/** Fill an integer array of given size by comma-separated values in str. + * Return the number of values contained in str. */ +unsigned parseIntArray(int* array, unsigned size, char* str) { - __u8 cnt=0; - __u8 map[ABS_CNT] = {-1}; + unsigned i; + for(i=0; i=size || contains[v] ) + return 0; + contains[v] = 1; + } + + // Do it the hard way on array[cnt, max] + unsigned min=0; + for(unsigned i=cnt; i=(1<<8) ) + array[i] = min; + contains[min] = 1; + } + + return 1; +} + + +void setAxismap(int fd, char* mapstr) +{ + __u8 map[ABS_CNT] = {-1}; + int imap[ABS_CNT] = {-1}; + + + const __u8 cnt = getNumberOfAxes(fd); + printf("Setting %d axes.\n", cnt); + assert( cnt <= ABS_CNT); + + + const unsigned readcnt = parseIntArray(imap, cnt, mapstr); + if( readcnt != cnt ) + { + fprintf(stderr, "Invalid number of values given: %d\n", readcnt); + exit(EXIT_VALUE); + } + + // Nothing to do + if( cnt<=0 ) + return; + + // Fill the remaining array such that we obtain a permutation + if( !fillPermutation(imap, cnt, ABS_CNT) ) + { + fprintf(stderr, "Mapping needs to be a permutation.\n"); + exit(EXIT_VALUE); + } + + // Copy array and check bounds + for(unsigned i=0; i=ABS_CNT ) { - fprintf(stderr, "Value out of bounds: %d\n", val); + fprintf(stderr, "Value out of bounds: %d\n", imap[i]); exit(EXIT_VALUE); } - map[i] = val; - s = strstr(s, ","); - if(s) - s++; + map[i] = imap[i]; } - if( s ) - fprintf(stderr, "Too many values given for the axismap. Ignoring the rest.\n"); - printf("Setting the following map:\n"); for(int i=0; i=(1<<16) ) + // Copy array and check bounds + for(unsigned i=0; i=BTN_CNT ) { - fprintf(stderr, "Value out of bounds: %d\n", val); + fprintf(stderr, "Value out of bounds: %d\n", imap[i]); exit(EXIT_VALUE); } - map[i] = val; - s = strstr(s, ","); - if(s) - s++; + map[i] = imap[i]; } - if( s ) - fprintf(stderr, "Too many values given for the axismap. Ignoring the rest.\n"); - printf("Setting the following map:\n"); for(int i=0; i= argc ) reportMissingArgument(opt); - strncpy(dev, argv[i], MAXFN); + dev = argv[i]; } else if( !strcmp(opt, "--list-axismap") ) { @@ -286,7 +364,7 @@ int main(int argc, char** argv) } - if( strcmp("", dev)==0 ) + if( !dev ) { fprintf(stderr, "You need to specify a device.\n"); return EXIT_INVARG; -- 2.30.2