X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2FCommon%2Ffreeglut_joystick.c;h=5cfa43022f668b935767e9f096338c92c430fc28;hb=0848ae668792714605699541b09d07476bf649e8;hp=416091ffa5a7d3c7f694841307f89c65c490d215;hpb=38011fb92295a6a40a003b3d1fabcd565694cfb1;p=freeglut diff --git a/src/Common/freeglut_joystick.c b/src/Common/freeglut_joystick.c index 416091f..5cfa430 100644 --- a/src/Common/freeglut_joystick.c +++ b/src/Common/freeglut_joystick.c @@ -42,25 +42,17 @@ * interspersed */ -/* XXX It might be better to poll the operating system for the numbers of buttons and - * XXX axes and then dynamically allocate the arrays. - */ -#define _JS_MAX_BUTTONS 32 - #if TARGET_HOST_MACINTOSH -# define _JS_MAX_AXES 9 # include #endif #if TARGET_HOST_MAC_OSX -# define _JS_MAX_AXES 16 # include # include # include #endif #if TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) -# define _JS_MAX_AXES 8 # include # include # include @@ -68,7 +60,6 @@ #endif #if TARGET_HOST_POSIX_X11 -# define _JS_MAX_AXES 16 # ifdef HAVE_SYS_IOCTL_H # include # endif @@ -376,65 +367,6 @@ static int fghJoystickInitializeHID(struct os_specific_s *os, #endif /* - * Definition of "SFG_Joystick" structure -- based on JS's "jsJoystick" object class. - * See "js.h" lines 80-178. - */ -typedef struct tagSFG_Joystick SFG_Joystick; -struct tagSFG_Joystick -{ -#if TARGET_HOST_MACINTOSH -#define ISP_NUM_AXIS 9 -#define ISP_NUM_NEEDS 41 - ISpElementReference isp_elem [ ISP_NUM_NEEDS ]; - ISpNeed isp_needs [ ISP_NUM_NEEDS ]; -#endif - -#if TARGET_HOST_MAC_OSX - IOHIDDeviceInterface ** hidDev; - IOHIDElementCookie buttonCookies[41]; - IOHIDElementCookie axisCookies[_JS_MAX_AXES]; - long minReport[_JS_MAX_AXES], - maxReport[_JS_MAX_AXES]; -#endif - -#if TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) - JOYCAPS jsCaps; - JOYINFOEX js; - UINT js_id; -#endif - - -#if TARGET_HOST_POSIX_X11 -# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) - struct os_specific_s *os; -# endif - -# ifdef JS_NEW - struct js_event js; - int tmp_buttons; - float tmp_axes [ _JS_MAX_AXES ]; -# else - struct JS_DATA_TYPE js; -# endif - - char fname [ 128 ]; - int fd; -#endif - - int id; - GLboolean error; - char name [ 128 ]; - int num_axes; - int num_buttons; - - float dead_band[ _JS_MAX_AXES ]; - float saturate [ _JS_MAX_AXES ]; - float center [ _JS_MAX_AXES ]; - float max [ _JS_MAX_AXES ]; - float min [ _JS_MAX_AXES ]; -}; - -/* * Functions associated with the "jsJoystick" class in PLIB */ #if TARGET_HOST_MAC_OSX @@ -456,6 +388,7 @@ static void fghJoystickAddHatElement ( SFG_Joystick* joy, CFDictionaryRef hat ); /* External function declarations (mostly platform-specific) */ +extern void fgPlatformJoystickInit( SFG_Joystick *fgJoystick[], int ident ); extern void fgPlatformJoystickClose ( int ident ); /* @@ -464,6 +397,131 @@ extern void fgPlatformJoystickClose ( int ident ); #define MAX_NUM_JOYSTICKS 2 static SFG_Joystick *fgJoystick [ MAX_NUM_JOYSTICKS ]; +/* + * Platform-Specific Code + */ + +#if TARGET_HOST_MACINTOSH +void fgPlatformJoystickInit( SFG_Joystick *fgJoystick[], int ident ) +{ + fgJoystick[ ident ]->id = ident; + snprintf( fgJoystick[ ident ]->fname, sizeof(fgJoystick[ ident ]->fname), "/dev/js%d", ident ); /* FIXME */ + fgJoystick[ ident ]->error = GL_FALSE; +} + + +void fgPlatformJoystickClose ( int ident ) +{ + ISpSuspend( ); + ISpStop( ); + ISpShutdown( ); +} +#endif + +#if TARGET_HOST_MAC_OSX +void fgPlatformJoystickInit( SFG_Joystick *fgJoystick[], int ident ) +{ + fgJoystick[ ident ]->id = ident; + fgJoystick[ ident ]->error = GL_FALSE; + fgJoystick[ ident ]->num_axes = 0; + fgJoystick[ ident ]->num_buttons = 0; + + if( numDevices < 0 ) + { + /* do first-time init (since we can't over-ride jsInit, hmm */ + numDevices = 0; + + mach_port_t masterPort; + IOReturn rv = IOMasterPort( bootstrap_port, &masterPort ); + if( rv != kIOReturnSuccess ) + { + fgWarning( "error getting master Mach port" ); + return; + } + fghJoystickFindDevices( masterPort ); + } + + if ( ident >= numDevices ) + { + fgJoystick[ ident ]->error = GL_TRUE; + return; + } + + /* get the name now too */ + CFDictionaryRef properties = getCFProperties( ioDevices[ ident ] ); + CFTypeRef ref = CFDictionaryGetValue( properties, + CFSTR( kIOHIDProductKey ) ); + if (!ref) + ref = CFDictionaryGetValue(properties, CFSTR( "USB Product Name" ) ); + + if( !ref || + !CFStringGetCString( ( CFStringRef )ref, name, 128, + CFStringGetSystemEncoding( ) ) ) + { + fgWarning( "error getting device name" ); + name[ 0 ] = '\0'; + } +} + + +void fgPlatformJoystickClose ( int ident ) +{ + ( *( fgJoystick[ ident ]->hidDev ) )-> + close( fgJoystick[ ident ]->hidDev ); +} +#endif + +#if TARGET_HOST_POSIX_X11 +void fgPlatformJoystickInit( SFG_Joystick *fgJoystick[], int ident ) +{ +#if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ ) + fgJoystick[ ident ]->id = ident; + fgJoystick[ ident ]->error = GL_FALSE; + + fgJoystick[ ident ]->os = calloc( 1, sizeof( struct os_specific_s ) ); + memset( fgJoystick[ ident ]->os, 0, sizeof( struct os_specific_s ) ); + if( ident < USB_IDENT_OFFSET ) + fgJoystick[ ident ]->os->is_analog = 1; + if( fgJoystick[ ident ]->os->is_analog ) + snprintf( fgJoystick[ ident ]->os->fname, sizeof(fgJoystick[ ident ]->os->fname), "%s%d", AJSDEV, ident ); + else + snprintf( fgJoystick[ ident ]->os->fname, sizeof(fgJoystick[ ident ]->os->fname), "%s%d", UHIDDEV, + ident - USB_IDENT_OFFSET ); +#elif defined( __linux__ ) + fgJoystick[ ident ]->id = ident; + fgJoystick[ ident ]->error = GL_FALSE; + + snprintf( fgJoystick[ident]->fname, sizeof(fgJoystick[ident]->fname), "/dev/input/js%d", ident ); + + if( access( fgJoystick[ ident ]->fname, F_OK ) != 0 ) + snprintf( fgJoystick[ ident ]->fname, sizeof(fgJoystick[ ident ]->fname), "/dev/js%d", ident ); +#endif +} + + +void fgPlatformJoystickClose ( int ident ) +{ +#if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ ) + if( fgJoystick[ident]->os ) + { + if( ! fgJoystick[ ident ]->error ) + close( fgJoystick[ ident ]->os->fd ); +#ifdef HAVE_USB_JS + if( fgJoystick[ ident ]->os->hids ) + free (fgJoystick[ ident ]->os->hids); + if( fgJoystick[ ident ]->os->hid_data_buf ) + free( fgJoystick[ ident ]->os->hid_data_buf ); +#endif + free( fgJoystick[ident]->os ); + } +#endif + + if( ! fgJoystick[ident]->error ) + close( fgJoystick[ ident ]->fd ); +} +#endif + + /* * Read the raw joystick data @@ -1509,97 +1567,7 @@ static void fghJoystickInit( int ident ) fgJoystick[ ident ]->num_axes = fgJoystick[ ident ]->num_buttons = 0; fgJoystick[ ident ]->error = GL_TRUE; -#if TARGET_HOST_MACINTOSH - fgJoystick[ ident ]->id = ident; - snprintf( fgJoystick[ ident ]->fname, sizeof(fgJoystick[ ident ]->fname), "/dev/js%d", ident ); /* FIXME */ - fgJoystick[ ident ]->error = GL_FALSE; -#endif - -#if TARGET_HOST_MAC_OSX - fgJoystick[ ident ]->id = ident; - fgJoystick[ ident ]->error = GL_FALSE; - fgJoystick[ ident ]->num_axes = 0; - fgJoystick[ ident ]->num_buttons = 0; - - if( numDevices < 0 ) - { - /* do first-time init (since we can't over-ride jsInit, hmm */ - numDevices = 0; - - mach_port_t masterPort; - IOReturn rv = IOMasterPort( bootstrap_port, &masterPort ); - if( rv != kIOReturnSuccess ) - { - fgWarning( "error getting master Mach port" ); - return; - } - fghJoystickFindDevices( masterPort ); - } - - if ( ident >= numDevices ) - { - fgJoystick[ ident ]->error = GL_TRUE; - return; - } - - /* get the name now too */ - CFDictionaryRef properties = getCFProperties( ioDevices[ ident ] ); - CFTypeRef ref = CFDictionaryGetValue( properties, - CFSTR( kIOHIDProductKey ) ); - if (!ref) - ref = CFDictionaryGetValue(properties, CFSTR( "USB Product Name" ) ); - - if( !ref || - !CFStringGetCString( ( CFStringRef )ref, name, 128, - CFStringGetSystemEncoding( ) ) ) - { - fgWarning( "error getting device name" ); - name[ 0 ] = '\0'; - } -#endif - -#if TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) - switch( ident ) - { - case 0: - fgJoystick[ ident ]->js_id = JOYSTICKID1; - fgJoystick[ ident ]->error = GL_FALSE; - break; - case 1: - fgJoystick[ ident ]->js_id = JOYSTICKID2; - fgJoystick[ ident ]->error = GL_FALSE; - break; - default: - fgJoystick[ ident ]->num_axes = 0; - fgJoystick[ ident ]->error = GL_TRUE; - return; - } -#endif - -#if TARGET_HOST_POSIX_X11 -# if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ ) - fgJoystick[ ident ]->id = ident; - fgJoystick[ ident ]->error = GL_FALSE; - - fgJoystick[ ident ]->os = calloc( 1, sizeof( struct os_specific_s ) ); - memset( fgJoystick[ ident ]->os, 0, sizeof( struct os_specific_s ) ); - if( ident < USB_IDENT_OFFSET ) - fgJoystick[ ident ]->os->is_analog = 1; - if( fgJoystick[ ident ]->os->is_analog ) - snprintf( fgJoystick[ ident ]->os->fname, sizeof(fgJoystick[ ident ]->os->fname), "%s%d", AJSDEV, ident ); - else - snprintf( fgJoystick[ ident ]->os->fname, sizeof(fgJoystick[ ident ]->os->fname), "%s%d", UHIDDEV, - ident - USB_IDENT_OFFSET ); -# elif defined( __linux__ ) - fgJoystick[ ident ]->id = ident; - fgJoystick[ ident ]->error = GL_FALSE; - - snprintf( fgJoystick[ident]->fname, sizeof(fgJoystick[ident]->fname), "/dev/input/js%d", ident ); - - if( access( fgJoystick[ ident ]->fname, F_OK ) != 0 ) - snprintf( fgJoystick[ ident ]->fname, sizeof(fgJoystick[ ident ]->fname), "/dev/js%d", ident ); -# endif -#endif + fgPlatformJoystickInit( fgJoystick, ident ); fghJoystickOpen( fgJoystick[ ident ] ); } @@ -1619,50 +1587,6 @@ void fgInitialiseJoysticks ( void ) } } -/* - * - */ - -#if TARGET_HOST_MACINTOSH -void fgPlatformJoystickClose ( int ident ) -{ - ISpSuspend( ); - ISpStop( ); - ISpShutdown( ); -} -#endif - -#if TARGET_HOST_MAC_OSX -void fgPlatformJoystickClose ( int ident ) -{ - ( *( fgJoystick[ ident ]->hidDev ) )-> - close( fgJoystick[ ident ]->hidDev ); -} -#endif - -#if TARGET_HOST_POSIX_X11 -void fgPlatformJoystickClose ( int ident ) -{ -#if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ ) - if( fgJoystick[ident]->os ) - { - if( ! fgJoystick[ ident ]->error ) - close( fgJoystick[ ident ]->os->fd ); -#ifdef HAVE_USB_JS - if( fgJoystick[ ident ]->os->hids ) - free (fgJoystick[ ident ]->os->hids); - if( fgJoystick[ ident ]->os->hid_data_buf ) - free( fgJoystick[ ident ]->os->hid_data_buf ); -#endif - free( fgJoystick[ident]->os ); - } -#endif - - if( ! fgJoystick[ident]->error ) - close( fgJoystick[ ident ]->fd ); -} -#endif - void fgJoystickClose( void ) {