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
29 * convert it from C++ to C. And I've also reformatted it a bit (that's
30 * my little personal deviation :]) I don't really know if it is still
32 * Steve: could you please add some comments to the code? :)
34 * FreeBSD port by Stephen Montgomery-Smith <stephen@math.missouri.edu>
37 #if defined(__FreeBSD__) || defined(__NetBSD__)
38 #include <sys/param.h>
45 #define G_LOG_DOMAIN "freeglut-joystick"
47 #include "../include/GL/freeglut.h"
48 #include "freeglut_internal.h"
51 * PWO: I don't like it at all. It's a mess. Could it be cleared?
55 # if defined( __CYGWIN32__ ) || defined( __CYGWIN__ )
59 # include <mmsystem.h>
64 # if defined(__FreeBSD__) || defined(__NetBSD__)
65 # if __FreeBSD_version >= 500000
66 # include <sys/joystick.h>
68 # include <machine/joystick.h>
70 # define JS_DATA_TYPE joystick
71 # define JS_RETURN (sizeof(struct JS_DATA_TYPE))
72 # elif defined(__linux__)
73 # include <sys/ioctl.h>
74 # include <linux/joystick.h>
78 * Check the joystick driver version
81 # if JS_VERSION >= 0x010000
89 * Not Windoze and no (known) joystick driver...
91 * Well - we'll put these values in and that should
92 * allow the code to at least compile. The JS open
93 * routine should error out and shut off all the code
103 # define JS_RETURN (sizeof(struct JS_DATA_TYPE))
109 # define _JS_MAX_AXES 6
112 # define _JS_MAX_AXES 2
114 # define _JS_MAX_AXES 6
118 typedef struct tagSFG_Joystick SFG_Joystick;
119 struct tagSFG_Joystick
132 float tmp_axes[ _JS_MAX_AXES ];
134 struct JS_DATA_TYPE js;
145 float dead_band[ _JS_MAX_AXES ];
146 float saturate [ _JS_MAX_AXES ];
147 float center [ _JS_MAX_AXES ];
148 float max [ _JS_MAX_AXES ];
149 float min [ _JS_MAX_AXES ];
153 * The static joystick structure pointer
155 static SFG_Joystick* fgJoystick = NULL;
158 * Read the raw joystick data
160 static void fghJoystickRawRead ( SFG_Joystick* joy, int* buttons, float* axes )
176 for( i=0; i<joy->num_axes; i++ )
183 status = joyGetPosEx( joy->js_id, &joy->js );
185 if( status != JOYERR_NOERROR )
192 *buttons = joy->js.dwButtons;
197 * WARNING - Fall through case clauses!!
199 switch( joy->num_axes )
201 case 6: axes[5] = (float) joy->js.dwVpos;
202 case 5: axes[4] = (float) joy->js.dwUpos;
203 case 4: axes[3] = (float) joy->js.dwRpos;
204 case 3: axes[2] = (float) joy->js.dwZpos;
205 case 2: axes[1] = (float) joy->js.dwYpos;
206 case 1: axes[0] = (float) joy->js.dwXpos;
214 status = read( joy->fd, &joy->js, sizeof(struct js_event) );
216 if( status != sizeof(struct js_event) )
218 if( errno == EAGAIN )
224 *buttons = joy->tmp_buttons;
226 memcpy( axes, joy->tmp_axes,
227 sizeof(float) * joy->num_axes );
231 fgWarning( "%s", joy->fname );
236 switch( joy->js.type & ~JS_EVENT_INIT )
238 case JS_EVENT_BUTTON:
239 if ( joy->js.value == 0 ) /* clear the flag */
240 joy->tmp_buttons &= ~(1 << joy->js.number);
242 joy->tmp_buttons |= (1 << joy->js.number);
246 joy->tmp_axes[ joy->js.number ] = (float) joy->js.value;
249 memcpy( axes, joy->tmp_axes, sizeof(float) * joy->num_axes );
254 *buttons = joy->tmp_buttons;
258 status = read( joy->fd, &joy->js, JS_RETURN );
260 if( status != JS_RETURN )
262 fgWarning( "%s", joy->fname );
268 # if defined(__FreeBSD__) || defined(__NetBSD__)
269 *buttons = (joy->js.b1 ? 1 : 0) | (joy->js.b2 ? 2 : 0);
271 *buttons = joy->js.buttons;
276 axes[ 0 ] = (float) joy->js.x;
277 axes[ 1 ] = (float) joy->js.y;
284 * Correct the joystick axis data
286 static float fghJoystickFudgeAxis( SFG_Joystick* joy, float value, int axis )
288 if( value < joy->center[ axis ] )
290 float xx = (value - joy->center[ axis ]) / (joy->center[ axis ] -
293 if( xx < -joy->saturate[ axis ] )
296 if( xx > -joy->dead_band [ axis ] )
299 xx = (xx + joy->dead_band[ axis ]) / (joy->saturate[ axis ] -
300 joy->dead_band[ axis ]);
302 return( ( xx < -1.0f ) ? -1.0f : xx );
306 float xx = (value - joy->center [ axis ]) / (joy->max[ axis ] -
307 joy->center[ axis ]);
309 if( xx > joy->saturate[ axis ] )
312 if( xx < joy->dead_band[ axis ] )
315 xx = (xx - joy->dead_band[ axis ]) / (joy->saturate[ axis ] -
316 joy->dead_band[ axis ]);
318 return( ( xx > 1.0f ) ? 1.0f : xx );
323 * Read the corrected joystick data
325 static void fghJoystickRead( SFG_Joystick* joy, int* buttons, float* axes )
327 float raw_axes[ _JS_MAX_AXES ];
336 for ( i=0; i<joy->num_axes ; i++ )
340 fghJoystickRawRead( joy, buttons, raw_axes );
343 for( i=0 ; i<joy->num_axes ; i++ )
344 axes[ i ] = fghJoystickFudgeAxis( joy, raw_axes[ i ], i );
348 * Happy happy happy joy joy joy (happy new year toudi :D)
350 static void fghJoystickOpen( SFG_Joystick* joy )
356 joy->js.dwFlags = JOY_RETURNALL;
357 joy->js.dwSize = sizeof( joy->js );
359 memset( &jsCaps, 0, sizeof(jsCaps) );
362 (joyGetDevCaps( joy->js_id, &jsCaps, sizeof(jsCaps) ) !=
365 (jsCaps.wNumAxes < _JS_MAX_AXES ) ? jsCaps.wNumAxes : _JS_MAX_AXES;
368 * WARNING - Fall through case clauses!!
370 switch( joy->num_axes )
373 joy->min[ 5 ] = (float) jsCaps.wVmin;
374 joy->max[ 5 ] = (float) jsCaps.wVmax;
376 joy->min[ 4 ] = (float) jsCaps.wUmin;
377 joy->max[ 4 ] = (float) jsCaps.wUmax;
379 joy->min[ 3 ] = (float) jsCaps.wRmin;
380 joy->max[ 3 ] = (float) jsCaps.wRmax;
382 joy->min[ 2 ] = (float) jsCaps.wZmin;
383 joy->max[ 2 ] = (float) jsCaps.wZmax;
385 joy->min[ 1 ] = (float) jsCaps.wYmin;
386 joy->max[ 1 ] = (float) jsCaps.wYmax;
388 joy->min[ 0 ] = (float) jsCaps.wXmin;
389 joy->max[ 0 ] = (float) jsCaps.wXmax;
393 * I guess we have no axes at all
401 * Guess all the rest judging on the axes extremals
403 for( i=0 ; i<joy->num_axes ; i++ )
405 joy->center [ i ] = (joy->max[i] + joy->min[i]) * 0.5f;
406 joy->dead_band[ i ] = 0.0f;
407 joy->saturate [ i ] = 1.0f;
412 int buttons[ _JS_MAX_AXES ];
413 float axes[ _JS_MAX_AXES ];
414 int noargs, in_no_axes;
415 char joyfname[ 1024 ];
425 * Default for older Linux systems.
428 joy->num_buttons = 32;
431 for( i=0 ; i<_JS_MAX_AXES ; i++ )
432 joy->tmp_axes[ i ] = 0.0f ;
434 joy->tmp_buttons = 0 ;
437 joy->fd = open( joy->fname, O_RDONLY );
439 joy->error = (joy->fd < 0);
445 fghJoystickRawRead(joy, buttons, axes );
446 joy->error = axes[ 0 ] < -1000000000.0f;
450 sprintf( joyfname, "%s/.joy%drc", getenv( "HOME" ), joy->id );
452 joyfile = fopen( joyfname, "r" );
453 joy->error = (joyfile == NULL);
458 joyfile, "%d%f%f%f%f%f%f",
460 &joy->min[ 0 ], &joy->center[ 0 ], &joy->max[ 0 ],
461 &joy->min[ 1 ], &joy->center[ 1 ], &joy->max[ 1 ]
464 joy->error = (noargs != 7) || (in_no_axes != _JS_MAX_AXES);
469 for( i=0 ; i<_JS_MAX_AXES ; i++ )
471 joy->dead_band[ i ] = 0.0f;
472 joy->saturate [ i ] = 1.0f;
477 * Set the correct number of axes for the linux driver
480 ioctl( joy->fd, JSIOCGAXES , &joy->num_axes );
481 ioctl( joy->fd, JSIOCGBUTTONS, &joy->num_buttons );
482 fcntl( joy->fd, F_SETFL, O_NONBLOCK );
487 * The Linux driver seems to return 512 for all axes
488 * when no stick is present - but there is a chance
489 * that could happen by accident - so it's gotta happen
490 * on both axes for at least 100 attempts.
492 * PWO: shouldn't be that done somehow wiser on the kernel level?
499 fghJoystickRawRead( joy, NULL, joy->center );
501 } while( !joy->error && counter < 100 && joy->center[ 0 ] == 512.0f && joy->center[ 1 ] == 512.0f );
507 for( i=0 ; i<_JS_MAX_AXES ; i++ )
510 joy->max [ i ] = 32767.0f;
511 joy->center[ i ] = 0.0f;
512 joy->min [ i ] = -32767.0f;
514 joy->max[ i ] = joy->center[ i ] * 2.0f;
515 joy->min[ i ] = 0.0f;
517 joy->dead_band[ i ] = 0.0f ;
518 joy->saturate [ i ] = 1.0f ;
527 void fgJoystickInit( int ident )
529 if( fgJoystick != NULL )
530 fgError( "illegal attemp to initialize joystick device" );
532 fgJoystick = (SFG_Joystick *)calloc( sizeof(SFG_Joystick), 1 );
537 case 0: fgJoystick->js_id = JOYSTICKID1; fghJoystickOpen( fgJoystick ); break;
538 case 1: fgJoystick->js_id = JOYSTICKID2; fghJoystickOpen( fgJoystick ); break;
539 default: fgJoystick->num_axes = 0; fgJoystick->error = TRUE; break;
544 fgJoystick->id = ident;
545 sprintf( fgJoystick->fname, "/dev/joy%d", ident );
547 sprintf( fgJoystick->fname, "/dev/js%d", ident );
550 fghJoystickOpen( fgJoystick );
557 void fgJoystickClose( void )
559 if( fgJoystick == NULL )
560 fgError( "illegal attempt to deinitialize joystick device" );
563 if( fgJoystick->error != TRUE )
564 close( fgJoystick->fd );
567 free ( fgJoystick ) ;
568 fgJoystick = NULL ; /* show joystick has been deinitialized */
572 * Polls the joystick and executes the joystick callback hooked to the
573 * window specified in the function's parameter:
575 void fgJoystickPollWindow( SFG_Window* window )
577 float axes[ _JS_MAX_AXES ];
580 freeglut_return_if_fail( fgJoystick != NULL && window != NULL );
581 freeglut_return_if_fail( window->Callbacks.Joystick != NULL );
583 fghJoystickRead( fgJoystick, &buttons, axes );
585 fgSetWindow (window);
586 window->Callbacks.Joystick(
588 (int) (axes[ 0 ] * 1000.0f),
589 (int) (axes[ 1 ] * 1000.0f),
590 (int) (axes[ 2 ] * 1000.0f)
595 * PWO: These jsJoystick class methods have not been implemented.
596 * We might consider adding such functions to freeglut-2.0.
599 int getNumAxes () { return num_axes ; }
600 int notWorking () { return error ; }
602 float getDeadBand ( int axis ) { return dead_band [ axis ] ; }
603 void setDeadBand ( int axis, float db ) { dead_band [ axis ] = db ; }
605 float getSaturation ( int axis ) { return saturate [ axis ] ; }
606 void setSaturation ( int axis, float st ) { saturate [ axis ] = st ; }
608 void setMinRange ( float *axes ) { memcpy ( min , axes, num_axes * sizeof(float) ) ; }
609 void setMaxRange ( float *axes ) { memcpy ( max , axes, num_axes * sizeof(float) ) ; }
610 void setCenter ( float *axes ) { memcpy ( center, axes, num_axes * sizeof(float) ) ; }
612 void getMinRange ( float *axes ) { memcpy ( axes, min , num_axes * sizeof(float) ) ; }
613 void getMaxRange ( float *axes ) { memcpy ( axes, max , num_axes * sizeof(float) ) ; }
614 void getCenter ( float *axes ) { memcpy ( axes, center, num_axes * sizeof(float) ) ; }
617 /*** END OF FILE ***/