12 #include <sys/ioctl.h>
13 #include <linux/joystick.h>
22 #define EXIT_VERSION 4
27 void reportMissingArgument(const char* opt
)
29 fprintf(stderr
, "Missing argument for option '%s'.\n", opt
);
33 void printHelp(FILE* out
, int argc
, char** argv
)
37 fprintf(out
, "Usage:\n"
40 " %s {commands} --dev <device>\n"
45 " --set-axismap 'id, id, id...'\n"
46 " --set-buttonmap 'id, id, id...'\n",
47 argv
[0], argv
[0], argv
[0]);
51 void listAxismap(int fd
)
54 __u8 map
[ABS_CNT
] = {-1};
57 if (ioctl(fd
, JSIOCGAXES
, &cnt
) )
59 perror(PACKAGE_STRING
": error getting number of axes");
62 printf("Got %d axes:\n", cnt
);
63 assert( cnt
<= ABS_CNT
);
66 if( ioctl(fd
, JSIOCGAXMAP
, &map
) < 0 )
68 perror(PACKAGE_STRING
": error getting axis map");
72 for(int i
=0; i
<cnt
; i
++)
73 printf(" %d => %d\n", i
, map
[i
] );
76 printf("Set this mapping with:\n");
77 printf(" " PACKAGE_STRING
" --set-axismap '");
78 for(int i
=0; i
<cnt
; i
++)
82 printf("%d", map
[i
] );
88 void listButtonmap(int fd
)
91 __u16 map
[KEY_MAX
- BTN_MISC
+ 1] = {-1};
93 if (ioctl(fd
, JSIOCGBUTTONS
, &cnt
) )
95 perror(PACKAGE_STRING
": error getting number of buttons");
98 printf("Got %d buttons:\n", cnt
);
101 if( ioctl(fd
, JSIOCGBTNMAP
, &map
) < 0 )
103 perror(PACKAGE_STRING
": error getting button map");
107 for(int i
=0; i
<cnt
; i
++)
108 printf(" %d => %d\n", i
, map
[i
] );
111 printf("Set this mapping with:\n");
112 printf(" " PACKAGE_STRING
" --set-buttonmap '");
113 for(int i
=0; i
<cnt
; i
++)
117 printf("%d", map
[i
] );
123 void setAxismap(int fd
, char* mapstr
)
126 __u8 map
[ABS_CNT
] = {-1};
129 if (ioctl(fd
, JSIOCGAXES
, &cnt
) )
131 perror(PACKAGE_STRING
": error getting number of axes");
134 printf("Setting %d axes.\n", cnt
);
135 assert( cnt
<= ABS_CNT
);
139 for(unsigned i
=0; i
<cnt
; ++i
)
143 fprintf(stderr
, "Missing value for axis %d.\n", i
);
148 sscanf(s
, "%d", &val
);
149 if( val
<0 || val
>=(1<<8) )
151 fprintf(stderr
, "Value out of bounds: %d\n", val
);
162 fprintf(stderr
, "Too many values given for the axismap. Ignoring the rest.\n");
165 printf("Setting the following map:\n");
166 for(int i
=0; i
<cnt
; i
++)
167 printf(" %d => %d\n", i
, map
[i
] );
170 if( ioctl(fd
, JSIOCSAXMAP
, &map
) )
172 perror( PACKAGE_STRING
": error setting axis map");
178 void setButtonmap(int fd
, char* mapstr
)
181 __u16 map
[KEY_MAX
- BTN_MISC
+ 1] = {-1};
183 printf("foo: %d\n", 1<<8);
185 if (ioctl(fd
, JSIOCGBUTTONS
, &cnt
) )
187 perror(PACKAGE_STRING
": error getting number of buttons");
190 printf("Setting %d buttons.\n", cnt
);
194 for(unsigned i
=0; i
<cnt
; ++i
)
198 fprintf(stderr
, "Missing value for axis %d.\n", i
);
203 sscanf(s
, "%d", &val
);
204 if( val
<0 || val
>=(1<<16) )
206 fprintf(stderr
, "Value out of bounds: %d\n", val
);
217 fprintf(stderr
, "Too many values given for the axismap. Ignoring the rest.\n");
220 printf("Setting the following map:\n");
221 for(int i
=0; i
<cnt
; i
++)
222 printf(" %d => %d\n", i
, map
[i
] );
225 if( ioctl(fd
, JSIOCSBTNMAP
, &map
) )
227 perror( PACKAGE_STRING
": error setting button map");
233 int main(int argc
, char** argv
)
235 int argListAxismap
=0;
236 int argListButtonmap
=0;
237 char* argSetAxismap
=0;
238 char* argSetButtonmap
=0;
239 char dev
[MAXFN
+1] = "";
243 for(int i
=1; i
<argc
; ++i
)
245 const char* opt
= argv
[i
];
247 if( !strcmp(opt
,"--help") || !strcmp(opt
,"-h") )
249 printHelp(stdout
, argc
, argv
);
252 else if( !strcmp(opt
, "--dev") || !strcmp(opt
,"-d") )
256 reportMissingArgument(opt
);
257 strncpy(dev
, argv
[i
], MAXFN
);
259 else if( !strcmp(opt
, "--list-axismap") )
263 else if( !strcmp(opt
, "--list-buttonmap") )
265 argListButtonmap
= 1;
267 else if( !strcmp(opt
, "--set-axismap") )
271 reportMissingArgument(opt
);
272 argSetAxismap
= argv
[i
];
274 else if( !strcmp(opt
, "--set-buttonmap") )
278 reportMissingArgument(opt
);
279 argSetButtonmap
= argv
[i
];
283 fprintf(stderr
, "Invalid option '%s'.\n", opt
);
289 if( strcmp("", dev
)==0 )
291 fprintf(stderr
, "You need to specify a device.\n");
297 if( (fd
= open(dev
, O_RDONLY
)) < 0 )
299 perror("Cannot open device.");
305 if( ioctl(fd
, JSIOCGVERSION
, &version
) )
307 perror( PACKAGE_STRING
": error getting version");
311 if( version
!= JS_VERSION
)
313 fprintf(stderr
, PACKAGE_STRING
": compiled with different version %d\n", JS_VERSION
);
321 if( argListButtonmap
)
325 setAxismap(fd
, argSetAxismap
);
327 if( argSetButtonmap
)
328 setButtonmap(fd
, argSetButtonmap
);