//* Accessing JoyStick in Posix *

// Received from: http://archives.seul.org/linuxgames/Aug-1999/msg00107.html /* this is the linux 2.2.x way of handling joysticks. It allows an arbitrary * number of axis and buttons. It's event driven, and has full signed int * ranges of the axis (-32768 to 32767). It also lets you pull the joysticks * name. The only place this works of that I know of is in the linux 1.x * joystick driver, which is included in the linux 2.2.x kernels */ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #include <linux/joystick.h> /* struct js_event { u32 time; // event timestamp in milliseconds s16 value; // value u8 type; // event type u8 number; // axis/button number }; EVENT TYPES: #define JS_EVENT_BUTTON 0x01 // button pressed/released #define JS_EVENT_AXIS 0x02 // joystick moved #define JS_EVENT_INIT 0x80 // initial state of device IOCTL commands: #define JSIOCGVERSION ... // get driver version #define JSIOCGAXES ... // get number of axes #define JSIOCGBUTTONS ... // get number of buttons #define JSIOCGNAME(len) ... // get identifier string #define JSIOCSCORR ... // set correction values #define JSIOCGCORR ... // get correction values #define JSIOCSAXMAP ... // set axis mapping #define JSIOCGAXMAP ... // get axis mapping #define JSIOCSBTNMAP ... // set button mapping #define JSIOCGBTNMAP ... // get button mapping */ #define JOY_DEV "/dev/input/js0" int main() { int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x; char *button=NULL, name_of_joystick[80]; struct js_event js; if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 ) { fprintf( stderr, "Couldn't open joystick\n" ); return -1; } ioctl( joy_fd, JSIOCGAXES, &num_of_axis ); ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons ); ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick ); // dynamic size arrays axis = (int *) calloc( num_of_axis, sizeof( int ) ); button = (char *) calloc( num_of_buttons, sizeof( char ) ); fprintf( stdout, "Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n" , name_of_joystick , num_of_axis , num_of_buttons ); fcntl( joy_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */ while( 1 ) /* infinite loop */ { /* read the joystick state */ read(joy_fd, &js, sizeof(struct js_event)); /* see what to do with the event */ switch (js.type & ~JS_EVENT_INIT) { case JS_EVENT_AXIS: axis [ js.number ] = js.value; break; case JS_EVENT_BUTTON: button [ js.number ] = js.value; break; } /* print the results */ fprintf(stdout, "B: "); for(x=num_of_buttons-1 ; x>=0 ; --x ) fprintf(stdout, "%d", !!button[x] ); if (num_of_axis>0) fprintf(stdout, " X: %7d", axis[0]); if (num_of_axis>1) fprintf(stdout, " Y: %7d", axis[1]); if (num_of_axis>2) fprintf(stdout, " Z: %7d", axis[2]); if (num_of_axis>3) fprintf(stdout, " R: %7d", axis[3]); if (num_of_axis>4) fprintf(stdout, " U: %7d", axis[4]); if (num_of_axis>5) fprintf(stdout, " V: %7d", axis[5]); if (num_of_axis>6) fprintf(stdout, " more skipped"); fprintf(stdout, "\n"); fflush(stdout); if (button[0] && button[1] && button[2] && button[3]) break; } close( joy_fd ); free(axis); free(button); return 0; }