4 * Joystick handling code
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Steve Baker, <sjbaker1@airmail.net>
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * PWO: this is not exactly what Steve Baker has done for PLIB, as I had to convert
29 * it from C++ to C. And I've also reformatted it a bit (that's my little
30 * personal deviation :]) I don't really know if it is still portable...
31 * Steve: could you please add some comments to the code? :)
33 * FreeBSD port - courtesy of Stephen Montgomery-Smith <stephen@math.missouri.edu>
36 #if defined(__FreeBSD__) || defined(__NetBSD__)
37 #include <sys/param.h>
44 #define G_LOG_DOMAIN "freeglut-joystick"
46 #include "../include/GL/freeglut.h"
47 #include "freeglut_internal.h"
50 * PWO: I don't like it at all. It's a mess. Could it be cleared?
54 # if defined( __CYGWIN32__ ) || defined( __CYGWIN__ )
58 # include <mmsystem.h>
63 # if defined(__FreeBSD__) || defined(__NetBSD__)
64 # if __FreeBSD_version >= 500000
65 # include <sys/joystick.h>
67 # include <machine/joystick.h>
69 # define JS_DATA_TYPE joystick
70 # define JS_RETURN (sizeof(struct JS_DATA_TYPE))
71 # elif defined(__linux__)
72 # include <sys/ioctl.h>
73 # include <linux/joystick.h>
77 * Check the joystick driver version
80 # if JS_VERSION >= 0x010000
88 * Not Windoze and no joystick driver...
90 * Well - we'll put these values in and that should
91 * allow the code to at least compile. The JS open
92 * routine should error out and shut off all the code
102 # define JS_RETURN (sizeof(struct JS_DATA_TYPE))
108 # define _JS_MAX_AXES 6
111 # define _JS_MAX_AXES 2
113 # define _JS_MAX_AXES 6
117 typedef struct tagSFG_Joystick SFG_Joystick;
118 struct tagSFG_Joystick
131 float tmp_axes[ _JS_MAX_AXES ];
133 struct JS_DATA_TYPE js;
144 float dead_band[ _JS_MAX_AXES ];
145 float saturate [ _JS_MAX_AXES ];
146 float center [ _JS_MAX_AXES ];
147 float max [ _JS_MAX_AXES ];
148 float min [ _JS_MAX_AXES ];
152 * The static joystick structure pointer
154 static SFG_Joystick* fgJoystick = NULL;
157 * Read the raw joystick data
159 static void fghJoystickRawRead ( SFG_Joystick* joy, int* buttons, float* axes )
175 for( i=0; i<joy->num_axes; i++ )
182 status = joyGetPosEx( joy->js_id, &joy->js );
184 if( status != JOYERR_NOERROR )
191 *buttons = joy->js.dwButtons;
196 * WARNING - Fall through case clauses!!
198 switch( joy->num_axes )
200 case 6: axes[5] = (float) joy->js.dwVpos;
201 case 5: axes[4] = (float) joy->js.dwUpos;
202 case 4: axes[3] = (float) joy->js.dwRpos;
203 case 3: axes[2] = (float) joy->js.dwZpos;
204 case 2: axes[1] = (float) joy->js.dwYpos;
205 case 1: axes[0] = (float) joy->js.dwXpos;
213 status = read( joy->fd, &joy->js, sizeof(struct js_event) );
215 if( status != sizeof(struct js_event) )
217 if( errno == EAGAIN )
222 if( buttons ) *buttons = joy->tmp_buttons;
223 if( axes ) memcpy( axes, joy->tmp_axes, sizeof(float) * joy->num_axes );
227 fgWarning( "%s", joy->fname );
232 switch( joy->js.type & ~JS_EVENT_INIT )
234 case JS_EVENT_BUTTON:
235 if ( joy->js.value == 0 ) /* clear the flag */
236 joy->tmp_buttons &= ~(1 << joy->js.number);
238 joy->tmp_buttons |= (1 << joy->js.number);
242 joy->tmp_axes[ joy->js.number ] = (float) joy->js.value;
245 memcpy( axes, joy->tmp_axes, sizeof(float) * joy->num_axes );
250 *buttons = joy->tmp_buttons;
254 status = read( joy->fd, &joy->js, JS_RETURN );
256 if( status != JS_RETURN )
258 fgWarning( "%s", joy->fname );
264 # if defined(__FreeBSD__) || defined(__NetBSD__)
265 *buttons = (joy->js.b1 ? 1 : 0) | (joy->js.b2 ? 2 : 0);
267 *buttons = joy->js.buttons;
272 axes[ 0 ] = (float) joy->js.x;
273 axes[ 1 ] = (float) joy->js.y;
280 * Correct the joystick axis data
282 static float fghJoystickFudgeAxis( SFG_Joystick* joy, float value, int axis )
284 if( value < joy->center[ axis ] )
286 float xx = (value - joy->center[ axis ]) / (joy->center[ axis ] - joy->min[ axis ]);
288 if( xx < -joy->saturate[ axis ] )
291 if( xx > -joy->dead_band [ axis ] )
294 xx = (xx + joy->dead_band[ axis ]) / (joy->saturate[ axis ] - joy->dead_band[ axis ]);
296 return( ( xx < -1.0f ) ? -1.0f : xx );
300 float xx = (value - joy->center [ axis ]) / (joy->max[ axis ] - joy->center[ axis ]);
302 if( xx > joy->saturate[ axis ] )
305 if( xx < joy->dead_band[ axis ] )
308 xx = (xx - joy->dead_band[ axis ]) / (joy->saturate[ axis ] - joy->dead_band[ axis ]);
310 return( ( xx > 1.0f ) ? 1.0f : xx );
315 * Read the corrected joystick data
317 static void fghJoystickRead( SFG_Joystick* joy, int* buttons, float* axes )
319 float raw_axes[ _JS_MAX_AXES ];
328 for ( i=0; i<joy->num_axes ; i++ )
332 fghJoystickRawRead( joy, buttons, raw_axes );
335 for( i=0 ; i<joy->num_axes ; i++ )
336 axes[ i ] = fghJoystickFudgeAxis( joy, raw_axes[ i ], i );
340 * Happy happy happy joy joy joy (happy new year toudi :D)
342 static void fghJoystickOpen( SFG_Joystick* joy )
348 joy->js.dwFlags = JOY_RETURNALL;
349 joy->js.dwSize = sizeof( joy->js );
351 memset( &jsCaps, 0, sizeof(jsCaps) );
353 joy->error = (joyGetDevCaps( joy->js_id, &jsCaps, sizeof(jsCaps) ) != JOYERR_NOERROR);
354 joy->num_axes = (jsCaps.wNumAxes < _JS_MAX_AXES ) ? jsCaps.wNumAxes : _JS_MAX_AXES;
357 * WARNING - Fall through case clauses!!
359 switch( joy->num_axes )
361 case 6 : joy->min[ 5 ] = (float) jsCaps.wVmin; joy->max[ 5 ] = (float) jsCaps.wVmax;
362 case 5 : joy->min[ 4 ] = (float) jsCaps.wUmin; joy->max[ 4 ] = (float) jsCaps.wUmax;
363 case 4 : joy->min[ 3 ] = (float) jsCaps.wRmin; joy->max[ 3 ] = (float) jsCaps.wRmax;
364 case 3 : joy->min[ 2 ] = (float) jsCaps.wZmin; joy->max[ 2 ] = (float) jsCaps.wZmax;
365 case 2 : joy->min[ 1 ] = (float) jsCaps.wYmin; joy->max[ 1 ] = (float) jsCaps.wYmax;
366 case 1 : joy->min[ 0 ] = (float) jsCaps.wXmin; joy->max[ 0 ] = (float) jsCaps.wXmax; break;
369 * I guess we have no axes at all
371 default: joy->error = TRUE; break;
375 * Guess all the rest judging on the axes extremals
377 for( i=0 ; i<joy->num_axes ; i++ )
379 joy->center [ i ] = (joy->max[i] + joy->min[i]) * 0.5f;
380 joy->dead_band[ i ] = 0.0f;
381 joy->saturate [ i ] = 1.0f;
386 int buttons[ _JS_MAX_AXES ];
387 float axes[ _JS_MAX_AXES ];
388 int noargs, in_no_axes;
389 char joyfname[ 1024 ];
399 * Default for older Linux systems.
402 joy->num_buttons = 32;
405 for( i=0 ; i<_JS_MAX_AXES ; i++ )
406 joy->tmp_axes[ i ] = 0.0f ;
408 joy->tmp_buttons = 0 ;
411 joy->fd = open( joy->fname, O_RDONLY );
413 joy->error = (joy->fd < 0);
419 fghJoystickRawRead(joy, buttons, axes );
420 joy->error = axes[ 0 ] < -1000000000.0f;
424 sprintf( joyfname, "%s/.joy%drc", getenv( "HOME" ), joy->id );
426 joyfile = fopen( joyfname, "r" );
427 joy->error = (joyfile == NULL);
432 joyfile, "%d%f%f%f%f%f%f",
434 &joy->min[ 0 ], &joy->center[ 0 ], &joy->max[ 0 ],
435 &joy->min[ 1 ], &joy->center[ 1 ], &joy->max[ 1 ]
438 joy->error = (noargs != 7) || (in_no_axes != _JS_MAX_AXES);
443 for( i=0 ; i<_JS_MAX_AXES ; i++ )
445 joy->dead_band[ i ] = 0.0f;
446 joy->saturate [ i ] = 1.0f;
451 * Set the correct number of axes for the linux driver
454 ioctl( joy->fd, JSIOCGAXES , &joy->num_axes );
455 ioctl( joy->fd, JSIOCGBUTTONS, &joy->num_buttons );
456 fcntl( joy->fd, F_SETFL, O_NONBLOCK );
461 * The Linux driver seems to return 512 for all axes
462 * when no stick is present - but there is a chance
463 * that could happen by accident - so it's gotta happen
464 * on both axes for at least 100 attempts.
466 * PWO: shouldn't be that done somehow wiser on the kernel level?
473 fghJoystickRawRead( joy, NULL, joy->center );
475 } while( !joy->error && counter < 100 && joy->center[ 0 ] == 512.0f && joy->center[ 1 ] == 512.0f );
481 for( i=0 ; i<_JS_MAX_AXES ; i++ )
484 joy->max [ i ] = 32767.0f;
485 joy->center[ i ] = 0.0f;
486 joy->min [ i ] = -32767.0f;
488 joy->max[ i ] = joy->center[ i ] * 2.0f;
489 joy->min[ i ] = 0.0f;
491 joy->dead_band[ i ] = 0.0f ;
492 joy->saturate [ i ] = 1.0f ;
501 void fgJoystickInit( int ident )
504 * Make sure we don't get reinitialized
506 if( fgJoystick != NULL )
507 fgError( "illegal attemp to initialize joystick device" );
510 * Have the global joystick structure created
512 fgJoystick = (SFG_Joystick *)calloc( sizeof(SFG_Joystick), 1 );
517 case 0: fgJoystick->js_id = JOYSTICKID1; fghJoystickOpen( fgJoystick ); break;
518 case 1: fgJoystick->js_id = JOYSTICKID2; fghJoystickOpen( fgJoystick ); break;
519 default: fgJoystick->num_axes = 0; fgJoystick->error = TRUE; break;
524 fgJoystick->id = ident;
525 sprintf( fgJoystick->fname, "/dev/joy%d", ident );
527 sprintf( fgJoystick->fname, "/dev/js%d", ident );
531 * Let's try opening the joystick device now:
533 fghJoystickOpen( fgJoystick );
540 void fgJoystickClose( void )
542 if( fgJoystick == NULL )
543 fgError( "illegal attempt to deinitialize joystick device" );
546 if( fgJoystick->error != TRUE )
547 close( fgJoystick->fd );
550 free ( fgJoystick ) ;
551 fgJoystick = NULL ; /* show joystick has been deinitialized */
555 * Polls the joystick and executes the joystick callback hooked to the
556 * window specified in the function's parameter:
558 void fgJoystickPollWindow( SFG_Window* window )
560 float axes[ _JS_MAX_AXES ];
564 * Make sure the joystick device is initialized, the window seems valid
565 * and that there is a joystick callback hooked to it:
567 freeglut_return_if_fail( fgJoystick != NULL && window != NULL );
568 freeglut_return_if_fail( window->Callbacks.Joystick != NULL );
571 * Poll the joystick now:
573 fghJoystickRead( fgJoystick, &buttons, axes );
576 * Execute the freeglut joystick callback now
578 fgSetWindow (window);
579 window->Callbacks.Joystick(
581 (int) (axes[ 0 ] * 1000.0f),
582 (int) (axes[ 1 ] * 1000.0f),
583 (int) (axes[ 2 ] * 1000.0f)
588 * PWO: These jsJoystick class methods have not been implemented.
589 * We might consider adding such functions to freeglut-2.0.
592 int getNumAxes () { return num_axes ; }
593 int notWorking () { return error ; }
595 float getDeadBand ( int axis ) { return dead_band [ axis ] ; }
596 void setDeadBand ( int axis, float db ) { dead_band [ axis ] = db ; }
598 float getSaturation ( int axis ) { return saturate [ axis ] ; }
599 void setSaturation ( int axis, float st ) { saturate [ axis ] = st ; }
601 void setMinRange ( float *axes ) { memcpy ( min , axes, num_axes * sizeof(float) ) ; }
602 void setMaxRange ( float *axes ) { memcpy ( max , axes, num_axes * sizeof(float) ) ; }
603 void setCenter ( float *axes ) { memcpy ( center, axes, num_axes * sizeof(float) ) ; }
605 void getMinRange ( float *axes ) { memcpy ( axes, min , num_axes * sizeof(float) ) ; }
606 void getMaxRange ( float *axes ) { memcpy ( axes, max , num_axes * sizeof(float) ) ; }
607 void getCenter ( float *axes ) { memcpy ( axes, center, num_axes * sizeof(float) ) ; }
610 /*** END OF FILE ***/