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>
40 #define G_LOG_DOMAIN "freeglut-joystick"
42 #include "../include/GL/freeglut.h"
43 #include "freeglut_internal.h"
46 * PWO: I don't like it at all. It's a mess. Could it be cleared?
50 # if defined( __CYGWIN32__ ) || defined( __CYGWIN__ )
54 # include <mmsystem.h>
60 # include <machine/joystick.h>
61 # define JS_DATA_TYPE joystick
62 # define JS_RETURN (sizeof(struct JS_DATA_TYPE))
63 # elif defined(__linux__)
64 # include <sys/ioctl.h>
65 # include <linux/joystick.h>
69 * Check the joystick driver version
72 # if JS_VERSION >= 0x010000
80 * Not Windoze and no joystick driver...
82 * Well - we'll put these values in and that should
83 * allow the code to at least compile. The JS open
84 * routine should error out and shut off all the code
94 # define JS_RETURN (sizeof(struct JS_DATA_TYPE))
100 # define _JS_MAX_AXES 6
103 # define _JS_MAX_AXES 2
105 # define _JS_MAX_AXES 6
109 typedef struct tagSFG_Joystick SFG_Joystick;
110 struct tagSFG_Joystick
123 float tmp_axes[ _JS_MAX_AXES ];
125 struct JS_DATA_TYPE js;
136 float dead_band[ _JS_MAX_AXES ];
137 float saturate [ _JS_MAX_AXES ];
138 float center [ _JS_MAX_AXES ];
139 float max [ _JS_MAX_AXES ];
140 float min [ _JS_MAX_AXES ];
144 * The static joystick structure pointer
146 static SFG_Joystick* fgJoystick = NULL;
149 * Read the raw joystick data
151 static void fghJoystickRawRead ( SFG_Joystick* joy, int* buttons, float* axes )
167 for( i=0; i<joy->num_axes; i++ )
174 status = joyGetPosEx( joy->js_id, &joy->js );
176 if( status != JOYERR_NOERROR )
183 *buttons = joy->js.dwButtons;
188 * WARNING - Fall through case clauses!!
190 switch( joy->num_axes )
192 case 6: axes[5] = (float) joy->js.dwVpos;
193 case 5: axes[4] = (float) joy->js.dwUpos;
194 case 4: axes[3] = (float) joy->js.dwRpos;
195 case 3: axes[2] = (float) joy->js.dwZpos;
196 case 2: axes[1] = (float) joy->js.dwYpos;
197 case 1: axes[0] = (float) joy->js.dwXpos;
205 status = read( joy->fd, &joy->js, sizeof(struct js_event) );
207 if( status != sizeof(struct js_event) )
209 if( errno == EAGAIN )
214 if( buttons ) *buttons = joy->tmp_buttons;
215 if( axes ) memcpy( axes, joy->tmp_axes, sizeof(float) * joy->num_axes );
219 fgWarning( "%s", joy->fname );
224 switch( joy->js.type & ~JS_EVENT_INIT )
226 case JS_EVENT_BUTTON:
227 if ( joy->js.value == 0 ) /* clear the flag */
228 joy->tmp_buttons &= ~(1 << joy->js.number);
230 joy->tmp_buttons |= (1 << joy->js.number);
234 joy->tmp_axes[ joy->js.number ] = (float) joy->js.value;
237 memcpy( axes, joy->tmp_axes, sizeof(float) * joy->num_axes );
242 *buttons = joy->tmp_buttons;
246 status = read( joy->fd, &joy->js, JS_RETURN );
248 if( status != JS_RETURN )
250 g_warning( joy->fname );
257 *buttons = (joy->js.b1 ? 1 : 0) | (joy->js.b2 ? 2 : 0);
259 *buttons = joy->js.buttons;
264 axes[ 0 ] = (float) joy->js.x;
265 axes[ 1 ] = (float) joy->js.y;
272 * Correct the joystick axis data
274 static float fghJoystickFudgeAxis( SFG_Joystick* joy, float value, int axis )
276 if( value < joy->center[ axis ] )
278 float xx = (value - joy->center[ axis ]) / (joy->center[ axis ] - joy->min[ axis ]);
280 if( xx < -joy->saturate[ axis ] )
283 if( xx > -joy->dead_band [ axis ] )
286 xx = (xx + joy->dead_band[ axis ]) / (joy->saturate[ axis ] - joy->dead_band[ axis ]);
288 return( ( xx < -1.0f ) ? -1.0f : xx );
292 float xx = (value - joy->center [ axis ]) / (joy->max[ axis ] - joy->center[ axis ]);
294 if( xx > joy->saturate[ axis ] )
297 if( xx < joy->dead_band[ axis ] )
300 xx = (xx - joy->dead_band[ axis ]) / (joy->saturate[ axis ] - joy->dead_band[ axis ]);
302 return( ( xx > 1.0f ) ? 1.0f : xx );
307 * Read the corrected joystick data
309 static void fghJoystickRead( SFG_Joystick* joy, int* buttons, float* axes )
311 float raw_axes[ _JS_MAX_AXES ];
320 for ( i=0; i<joy->num_axes ; i++ )
324 fghJoystickRawRead( joy, buttons, raw_axes );
327 for( i=0 ; i<joy->num_axes ; i++ )
328 axes[ i ] = fghJoystickFudgeAxis( joy, raw_axes[ i ], i );
332 * Happy happy happy joy joy joy (happy new year toudi :D)
334 static void fghJoystickOpen( SFG_Joystick* joy )
340 joy->js.dwFlags = JOY_RETURNALL;
341 joy->js.dwSize = sizeof( joy->js );
343 memset( &jsCaps, 0, sizeof(jsCaps) );
345 joy->error = (joyGetDevCaps( joy->js_id, &jsCaps, sizeof(jsCaps) ) != JOYERR_NOERROR);
346 joy->num_axes = (jsCaps.wNumAxes < _JS_MAX_AXES ) ? jsCaps.wNumAxes : _JS_MAX_AXES;
349 * WARNING - Fall through case clauses!!
351 switch( joy->num_axes )
353 case 6 : joy->min[ 5 ] = (float) jsCaps.wVmin; joy->max[ 5 ] = (float) jsCaps.wVmax;
354 case 5 : joy->min[ 4 ] = (float) jsCaps.wUmin; joy->max[ 4 ] = (float) jsCaps.wUmax;
355 case 4 : joy->min[ 3 ] = (float) jsCaps.wRmin; joy->max[ 3 ] = (float) jsCaps.wRmax;
356 case 3 : joy->min[ 2 ] = (float) jsCaps.wZmin; joy->max[ 2 ] = (float) jsCaps.wZmax;
357 case 2 : joy->min[ 1 ] = (float) jsCaps.wYmin; joy->max[ 1 ] = (float) jsCaps.wYmax;
358 case 1 : joy->min[ 0 ] = (float) jsCaps.wXmin; joy->max[ 0 ] = (float) jsCaps.wXmax; break;
361 * I guess we have no axes at all
363 default: joy->error = TRUE; break;
367 * Guess all the rest judging on the axes extremals
369 for( i=0 ; i<joy->num_axes ; i++ )
371 joy->center [ i ] = (joy->max[i] + joy->min[i]) * 0.5f;
372 joy->dead_band[ i ] = 0.0f;
373 joy->saturate [ i ] = 1.0f;
378 int buttons[ _JS_MAX_AXES ];
379 float axes[ _JS_MAX_AXES ];
380 int noargs, in_no_axes;
381 char joyfname[ 1024 ];
391 * Default for older Linux systems.
394 joy->num_buttons = 32;
397 for( i=0 ; i<_JS_MAX_AXES ; i++ )
398 joy->tmp_axes[ i ] = 0.0f ;
400 joy->tmp_buttons = 0 ;
403 joy->fd = open( joy->fname, O_RDONLY );
405 joy->error = (joy->fd < 0);
411 fghJoystickRawRead( buttons, axes );
412 joy->error = axes[ 0 ] < -1000000000.0f;
416 sprintf( joyfname, "%s/.joy%drc", g_getenv( "HOME" ), id );
418 joyfile = fopen( joyfname, "r" );
419 joy->error = (joyfile == NULL);
424 joyfile, "%d%f%f%f%f%f%f",
426 &joy->min[ 0 ], &joy->center[ 0 ], &joy->max[ 0 ],
427 &joy->min[ 1 ], &joy->center[ 1 ], &joy->max[ 1 ]
430 joy->error = (noargs != 7) || (in_no_axes != _JS_MAX_AXES);
435 for( i=0 ; i<_JS_MAX_AXES ; i++ )
437 dead_band[ i ] = 0.0f;
438 saturate [ i ] = 1.0f;
443 * Set the correct number of axes for the linux driver
446 ioctl( joy->fd, JSIOCGAXES , &joy->num_axes );
447 ioctl( joy->fd, JSIOCGBUTTONS, &joy->num_buttons );
448 fcntl( joy->fd, F_SETFL, O_NONBLOCK );
453 * The Linux driver seems to return 512 for all axes
454 * when no stick is present - but there is a chance
455 * that could happen by accident - so it's gotta happen
456 * on both axes for at least 100 attempts.
458 * PWO: shouldn't be that done somehow wiser on the kernel level?
465 fghJoystickRawRead( joy, NULL, joy->center )
467 } while( !joy->error && counter < 100 && joy->center[ 0 ] == 512.0f && joy->center[ 1 ] == 512.0f );
473 for( i=0 ; i<_JS_MAX_AXES ; i++ )
476 joy->max [ i ] = 32767.0f;
477 joy->center[ i ] = 0.0f;
478 joy->min [ i ] = -32767.0f;
480 joy->max[ i ] = joy->center[ i ] * 2.0f;
481 joy->min[ i ] = 0.0f;
483 joy->dead_band[ i ] = 0.0f ;
484 joy->saturate [ i ] = 1.0f ;
493 void fgJoystickInit( int ident )
496 * Make sure we don't get reinitialized
498 if( fgJoystick != NULL )
499 fgError( "illegal attemp to initialize joystick device" );
502 * Have the global joystick structure created
504 fgJoystick = calloc( sizeof(SFG_Joystick), 1 );
509 case 0: fgJoystick->js_id = JOYSTICKID1; fghJoystickOpen( fgJoystick ); break;
510 case 1: fgJoystick->js_id = JOYSTICKID2; fghJoystickOpen( fgJoystick ); break;
511 default: fgJoystick->num_axes = 0; fgJoystick->error = TRUE; break;
516 fgJoystick->id = ident;
517 sprintf( fgJoystick->fname, "/dev/joy%d", ident );
519 sprintf( fgJoystick->fname, "/dev/js%d", ident );
523 * Let's try opening the joystick device now:
525 fghJoystickOpen( fgJoystick );
532 void fgJoystickClose( void )
534 if( fgJoystick == NULL )
535 fgError( "illegal attempt to deinitialize joystick device" );
538 if( fgJoystick->error != TRUE )
539 close( fgJoystick->fd );
542 free ( fgJoystick ) ;
543 fgJoystick = NULL ; /* show joystick has been deinitialized */
547 * Polls the joystick and executes the joystick callback hooked to the
548 * window specified in the function's parameter:
550 void fgJoystickPollWindow( SFG_Window* window )
552 float axes[ _JS_MAX_AXES ];
556 * Make sure the joystick device is initialized, the window seems valid
557 * and that there is a joystick callback hooked to it:
559 freeglut_return_if_fail( fgJoystick != NULL && window != NULL );
560 freeglut_return_if_fail( window->Callbacks.Joystick != NULL );
563 * Poll the joystick now:
565 fghJoystickRead( fgJoystick, &buttons, axes );
568 * Execute the freeglut joystick callback now
570 window->Callbacks.Joystick(
572 (int) (axes[ 0 ] * 1000.0f),
573 (int) (axes[ 1 ] * 1000.0f),
574 (int) (axes[ 2 ] * 1000.0f)
579 * PWO: These jsJoystick class methods have not been implemented.
580 * We might consider adding such functions to freeglut-2.0.
583 int getNumAxes () { return num_axes ; }
584 int notWorking () { return error ; }
586 float getDeadBand ( int axis ) { return dead_band [ axis ] ; }
587 void setDeadBand ( int axis, float db ) { dead_band [ axis ] = db ; }
589 float getSaturation ( int axis ) { return saturate [ axis ] ; }
590 void setSaturation ( int axis, float st ) { saturate [ axis ] = st ; }
592 void setMinRange ( float *axes ) { memcpy ( min , axes, num_axes * sizeof(float) ) ; }
593 void setMaxRange ( float *axes ) { memcpy ( max , axes, num_axes * sizeof(float) ) ; }
594 void setCenter ( float *axes ) { memcpy ( center, axes, num_axes * sizeof(float) ) ; }
596 void getMinRange ( float *axes ) { memcpy ( axes, min , num_axes * sizeof(float) ) ; }
597 void getMaxRange ( float *axes ) { memcpy ( axes, max , num_axes * sizeof(float) ) ; }
598 void getCenter ( float *axes ) { memcpy ( axes, center, num_axes * sizeof(float) ) ; }
601 /*** END OF FILE ***/