joystick init fixes (John Fay)
authorBrian Paul <brianp@vmware.com>
Wed, 6 Oct 2004 14:54:13 +0000 (14:54 +0000)
committerBrian Paul <brianp@vmware.com>
Wed, 6 Oct 2004 14:54:13 +0000 (14:54 +0000)
git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@523 7f0cb862-5218-0410-a997-914c9d46530a

src/freeglut_callbacks.c
src/freeglut_internal.h
src/freeglut_joystick.c
src/freeglut_state.c

index 94c4dbd..38adc58 100644 (file)
@@ -171,12 +171,7 @@ void FGAPIENTRY glutJoystickFunc( void (* callback)
                                   ( unsigned int, int, int, int ),
                                   int pollInterval )
 {
-    if( !fgState.JoysticksInitialised )
-    {
-        fgJoystickInit( 0 );
-        fgJoystickInit( 1 );
-        fgState.JoysticksInitialised = GL_TRUE;
-    }
+    fgInitialiseJoysticks ();
 
     SET_CALLBACK( Joystick );
     fgStructure.Window->State.JoystickPollRate = pollInterval;
index 56f8f24..a032851 100644 (file)
@@ -704,10 +704,16 @@ SFG_Menu*   fgCreateMenu( FGCBMenu menuCallback );
 void        fgDestroyMenu( SFG_Menu* menu );
 
 /* Joystick device management functions, defined in freeglut_joystick.c */
-void        fgJoystickInit( int ident );
+int         fgJoystickDetect( void );
+void        fgInitialiseJoysticks( void );
 void        fgJoystickClose( void );
 void        fgJoystickPollWindow( SFG_Window* window );
 
+/* More joystick functions.  Should these go into the API?  */
+int  glutJoystickGetNumAxes( int ident );
+int  glutJoystickGetNumButtons( int ident );
+int  glutJoystickNotWorking( int ident );
+
 /*
  * Helper function to enumerate through all registered windows
  * and one to enumerate all of a window's subwindows...
index a61a708..185ef1b 100644 (file)
@@ -450,7 +450,6 @@ static void fghJoystickAddHatElement ( SFG_Joystick* joy, CFDictionaryRef hat );
  * The static joystick structure pointer
  */
 #define MAX_NUM_JOYSTICKS  2
-static int fgNumberOfJoysticks = 0;
 static SFG_Joystick *fgJoystick [ MAX_NUM_JOYSTICKS ];
 
 
@@ -1528,7 +1527,7 @@ static void fghJoystickOpen( SFG_Joystick* joy )
 /*
  * This function replaces the constructor method in the JS library.
  */
-void fgJoystickInit( int ident )
+static void fghJoystickInit( int ident )
 {
     if( ident >= MAX_NUM_JOYSTICKS )
       fgError( "Too large a joystick number: %d", ident );
@@ -1639,6 +1638,22 @@ void fgJoystickInit( int ident )
 }
 
 /*
+ * Try initializing all the joysticks (well, both of them)
+ */
+void fgInitialiseJoysticks ( void )
+{
+  /* Initialization courtesy of OpenGLUT -- do we want it? */
+  if( !fgState.JoysticksInitialised )
+  {
+    int ident ;
+    for ( ident = 0; ident < MAX_NUM_JOYSTICKS; ident++ )
+      fghJoystickInit( ident );
+
+    fgState.JoysticksInitialised = GL_TRUE;
+  }
+}
+
+/*
  *
  */
 void fgJoystickClose( void )
@@ -1722,12 +1737,38 @@ void fgJoystickPollWindow( SFG_Window* window )
 }
 
 /*
- * PWO: These jsJoystick class methods have not been implemented.
+ * Implementation for glutDeviceGet(GLUT_HAS_JOYSTICK)
+ */
+int fgJoystickDetect( void )
+{
+  int ident;
+
+  fgInitialiseJoysticks ();
+
+  if ( !fgJoystick )
+    return 0;
+
+  if ( !fgState.JoysticksInitialised )
+    return 0;
+
+  for( ident=0; ident<MAX_NUM_JOYSTICKS; ident++ )
+    if( fgJoystick[ident] && !fgJoystick[ident]->error )
+      return 1;
+
+  return 0;
+}
+
+/*
+ * Joystick information functions
  */
 int  glutJoystickGetNumAxes( int ident )
 {
     return fgJoystick[ ident ]->num_axes;
 }
+int  glutJoystickGetNumButtons( int ident )
+{
+    return fgJoystick[ ident ]->num_buttons;
+}
 int  glutJoystickNotWorking( int ident )
 {
     return fgJoystick[ ident ]->error;
index f8b70e9..cf478a8 100644 (file)
@@ -528,13 +528,21 @@ int FGAPIENTRY glutDeviceGet( GLenum eWhat )
 
 #endif
 
-    case GLUT_JOYSTICK_POLL_RATE:
     case GLUT_HAS_JOYSTICK:
+        return fgJoystickDetect ();
+
     case GLUT_OWNS_JOYSTICK:
+        return fgState.JoysticksInitialised;
+
+    case GLUT_JOYSTICK_POLL_RATE:
+        return fgStructure.Window ? fgStructure.Window->State.JoystickPollRate : 0;
+
+    /* The following two are only for Joystick 0 but this is an improvement */
     case GLUT_JOYSTICK_BUTTONS:
+        return glutJoystickGetNumButtons ( 0 );
+
     case GLUT_JOYSTICK_AXES:
-        /* XXX WARNING: THIS IS A BIG LIE! */
-        return 0;
+        return glutJoystickGetNumAxes ( 0 );
 
     case GLUT_HAS_SPACEBALL:
     case GLUT_HAS_DIAL_AND_BUTTON_BOX: