- int page = HID_PAGE ( h->usage );\r
- int usage = HID_USAGE ( h->usage );\r
-\r
- if ( page == HUP_GENERIC_DESKTOP )\r
- {\r
- int i;\r
- for ( i = 0; i < joy->num_axes; i++ )\r
- if (joy->os->axes_usage[i] == usage)\r
- {\r
- if (usage == HUG_HAT_SWITCH)\r
- {\r
- if (d < 0 || d > 8)\r
- d = 0; /* safety */\r
- joy->os->cache_axes[i] = (float)hatmap_x[d];\r
- joy->os->cache_axes[i + 1] = (float)hatmap_y[d];\r
- }\r
- else\r
- {\r
- joy->os->cache_axes[i] = (float)d;\r
- }\r
- break;\r
- }\r
- }\r
- else if (page == HUP_BUTTON)\r
- {\r
- if (usage > 0 && usage < _JS_MAX_BUTTONS + 1)\r
- {\r
- if (d)\r
- joy->os->cache_buttons |= (1 << ( usage - 1 ));\r
- else\r
- joy->os->cache_buttons &= ~(1 << ( usage - 1 ));\r
- }\r
- }\r
- }\r
- }\r
-#ifdef HAVE_ERRNO_H\r
- if ( len < 0 && errno != EAGAIN )\r
-#else\r
- if ( len < 0 )\r
-#endif\r
- {\r
- perror( joy->os->fname );\r
- joy->error = 1;\r
- }\r
- if ( buttons != NULL ) *buttons = joy->os->cache_buttons;\r
- if ( axes != NULL )\r
- memcpy ( axes, joy->os->cache_axes, sizeof(float) * joy->num_axes );\r
-# endif\r
-# endif\r
-\r
-# ifdef JS_NEW\r
-\r
- while ( 1 )\r
- {\r
- status = read ( joy->fd, &joy->js, sizeof(struct js_event) );\r
-\r
- if ( status != sizeof( struct js_event ) )\r
- {\r
-#ifdef HAVE_ERRNO_H\r
- if ( errno == EAGAIN )\r
- {\r
- /* Use the old values */\r
- if ( buttons )\r
- *buttons = joy->tmp_buttons;\r
- if ( axes )\r
- memcpy( axes, joy->tmp_axes,\r
- sizeof( float ) * joy->num_axes );\r
- return;\r
- }\r
-#endif\r
-\r
- fgWarning ( "%s", joy->fname );\r
- joy->error = GL_TRUE;\r
- return;\r
- }\r
-\r
- switch ( joy->js.type & ~JS_EVENT_INIT )\r
- {\r
- case JS_EVENT_BUTTON:\r
- if( joy->js.value == 0 ) /* clear the flag */\r
- joy->tmp_buttons &= ~( 1 << joy->js.number );\r
- else\r
- joy->tmp_buttons |= ( 1 << joy->js.number );\r
- break;\r
-\r
- case JS_EVENT_AXIS:\r
- if ( joy->js.number < joy->num_axes )\r
- {\r
- joy->tmp_axes[ joy->js.number ] = ( float )joy->js.value;\r
-\r
- if( axes )\r
- memcpy( axes, joy->tmp_axes, sizeof(float) * joy->num_axes );\r
- }\r
- break;\r
-\r
- default:\r
- fgWarning ( "PLIB_JS: Unrecognised /dev/js return!?!" );\r
-\r
- /* use the old values */\r
-\r
- if ( buttons != NULL ) *buttons = joy->tmp_buttons;\r
- if ( axes != NULL )\r
- memcpy ( axes, joy->tmp_axes, sizeof(float) * joy->num_axes );\r
-\r
- return;\r
- }\r
-\r
- if( buttons )\r
- *buttons = joy->tmp_buttons;\r
- }\r
-# else\r
-\r
- status = read( joy->fd, &joy->js, JS_RETURN );\r
-\r
- if ( status != JS_RETURN )\r
- {\r
- fgWarning( "%s", joy->fname );\r
- joy->error = GL_TRUE;\r
- return;\r
- }\r
-\r
- if ( buttons )\r
-# if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ )\r
- *buttons = ( joy->js.b1 ? 1 : 0 ) | ( joy->js.b2 ? 2 : 0 ); /* XXX Should not be here -- BSD is handled earlier */\r
-# else\r
- *buttons = joy->js.buttons;\r
-# endif\r
-\r
- if ( axes )\r
- {\r
- axes[ 0 ] = (float) joy->js.x;\r
- axes[ 1 ] = (float) joy->js.y;\r
- }\r
-# endif\r
-#endif\r
-}\r
-\r
-/*\r
- * Correct the joystick axis data\r
- */\r
-static float fghJoystickFudgeAxis( SFG_Joystick* joy, float value, int axis )\r
-{\r
- if( value < joy->center[ axis ] )\r
- {\r
- float xx = ( value - joy->center[ axis ] ) / ( joy->center[ axis ] -\r
- joy->min[ axis ] );\r
-\r
- if( xx < -joy->saturate[ axis ] )\r
- return -1.0f;\r
-\r
- if( xx > -joy->dead_band [ axis ] )\r
- return 0.0f;\r
-\r
- xx = ( xx + joy->dead_band[ axis ] ) / ( joy->saturate[ axis ] -\r
- joy->dead_band[ axis ] );\r
-\r
- return ( xx < -1.0f ) ? -1.0f : xx;\r
- }\r
- else\r
- {\r
- float xx = ( value - joy->center [ axis ] ) / ( joy->max[ axis ] -\r
- joy->center[ axis ] );\r
-\r
- if( xx > joy->saturate[ axis ] )\r
- return 1.0f;\r
-\r
- if( xx < joy->dead_band[ axis ] )\r
- return 0.0f;\r
-\r
- xx = ( xx - joy->dead_band[ axis ] ) / ( joy->saturate[ axis ] -\r
- joy->dead_band[ axis ] );\r
-\r
- return ( xx > 1.0f ) ? 1.0f : xx;\r
- }\r
-}\r
-\r
-/*\r
- * Read the corrected joystick data\r
- */\r
-static void fghJoystickRead( SFG_Joystick* joy, int* buttons, float* axes )\r
-{\r
- float raw_axes[ _JS_MAX_AXES ];\r
- int i;\r
-\r
- if( joy->error )\r
- {\r
- if( buttons )\r
- *buttons = 0;\r
-\r
- if( axes )\r
- for ( i=0; i<joy->num_axes; i++ )\r
- axes[ i ] = 0.0f;\r
- }\r
-\r
- fghJoystickRawRead( joy, buttons, raw_axes );\r
-\r
- if( axes )\r
- for( i=0; i<joy->num_axes; i++ )\r
- axes[ i ] = fghJoystickFudgeAxis( joy, raw_axes[ i ], i );\r
-}\r
-\r
-/*\r
- * Happy happy happy joy joy joy (happy new year toudi :D)\r
- */\r
-\r
-\r
-#if TARGET_HOST_MAC_OSX\r
-/** open the IOKit connection, enumerate all the HID devices, add their\r
-interface references to the static array. We then use the array index\r
-as the device number when we come to open() the joystick. */\r
-static int fghJoystickFindDevices ( SFG_Joystick *joy, mach_port_t masterPort )\r
-{\r
- CFMutableDictionaryRef hidMatch = NULL;\r
- IOReturn rv = kIOReturnSuccess;\r
-\r
- io_iterator_t hidIterator;\r
- io_object_t ioDev;\r