Applied tthierry's patch to fix compiling on FreeBSD.
[freeglut] / freeglut-1.3 / freeglut_joystick.c
index 369910c..7ac4c3d 100644 (file)
@@ -6,10 +6,6 @@
  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
  * Written by Steve Baker, <sjbaker1@airmail.net>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
@@ -44,7 +40,7 @@
 #define G_LOG_DOMAIN "freeglut-joystick"
 
 #include "../include/GL/freeglut.h"
-#include "../include/GL/freeglut_internal.h"
+#include "freeglut_internal.h"
 
 /*
  * PWO: I don't like it at all. It's a mess. Could it be cleared?
@@ -114,7 +110,7 @@ typedef struct tagSFG_Joystick SFG_Joystick;
 struct tagSFG_Joystick
 {
 #ifdef __FreeBSD__
-    gint        id;
+    int         id;
 #endif
 
 #ifdef WIN32
@@ -123,19 +119,19 @@ struct tagSFG_Joystick
 #else
 #   ifdef JS_NEW
         struct js_event js;
-        gint        tmp_buttons;
+        int         tmp_buttons;
         float       tmp_axes[ _JS_MAX_AXES ];
 #   else
-        JS_DATA_TYPE js;
+        struct JS_DATA_TYPE js;
 #   endif
 
-    gchar fname[ 128 ];
-    gint  fd;
+    char fname[ 128 ];
+    int  fd;
 #endif
 
-    gboolean  error;
-    gint      num_axes;
-    gint      num_buttons;
+    GLboolean error;
+    int       num_axes;
+    int       num_buttons;
 
     float dead_band[ _JS_MAX_AXES ];
     float saturate [ _JS_MAX_AXES ];
@@ -152,15 +148,15 @@ static SFG_Joystick* fgJoystick = NULL;
 /*
  * Read the raw joystick data
  */
-static void fghJoystickRawRead ( SFG_Joystick* joy, gint* buttons, float* axes )
+static void fghJoystickRawRead ( SFG_Joystick* joy, int* buttons, float* axes )
 {
 #ifdef WIN32
     MMRESULT status;
 #else
-    gint status;
+    int status;
 #endif
 
-    gint i;
+    int i;
 
     if( joy->error )
     {
@@ -184,7 +180,7 @@ static void fghJoystickRawRead ( SFG_Joystick* joy, gint* buttons, float* axes )
     }
 
     if( buttons )
-        *buttons = (int) joy->js.dwButtons;
+        *buttons = joy->js.dwButtons;
 
     if( axes )
     {
@@ -206,7 +202,7 @@ static void fghJoystickRawRead ( SFG_Joystick* joy, gint* buttons, float* axes )
 
     while( 1 )
     {
-        gint status = read( joy->fd, &joy->js, sizeof(struct js_event) );
+        status = read( joy->fd, &joy->js, sizeof(struct js_event) );
 
         if( status != sizeof(struct js_event) )
         {
@@ -220,7 +216,7 @@ static void fghJoystickRawRead ( SFG_Joystick* joy, gint* buttons, float* axes )
                 return;
                }
 
-               g_warning( joy->fname );
+               fgWarning( "%s", joy->fname );
                joy->error = TRUE;
                return;
         }
@@ -251,7 +247,7 @@ static void fghJoystickRawRead ( SFG_Joystick* joy, gint* buttons, float* axes )
 
     if( status != JS_RETURN )
     {
-        g_warning( fname );
+        g_warning( joy->fname );
         joy->error = TRUE;
         return;
     }
@@ -275,7 +271,7 @@ static void fghJoystickRawRead ( SFG_Joystick* joy, gint* buttons, float* axes )
 /*
  * Correct the joystick axis data
  */
-static float fghJoystickFudgeAxis( SFG_Joystick* joy, float value, gint axis )
+static float fghJoystickFudgeAxis( SFG_Joystick* joy, float value, int axis )
 {
     if( value < joy->center[ axis ] )
     {
@@ -310,10 +306,10 @@ static float fghJoystickFudgeAxis( SFG_Joystick* joy, float value, gint axis )
 /*
  * Read the corrected joystick data
  */
-static void fghJoystickRead( SFG_Joystick* joy, gint* buttons, float* axes )
+static void fghJoystickRead( SFG_Joystick* joy, int* buttons, float* axes )
 {
     float raw_axes[ _JS_MAX_AXES ];
-    gint  i;
+    int  i;
 
     if( joy->error )
     {
@@ -339,7 +335,7 @@ static void fghJoystickOpen( SFG_Joystick* joy )
 {
 #ifdef WIN32
     JOYCAPS jsCaps;
-    gint    i;
+    int     i;
 
     joy->js.dwFlags = JOY_RETURNALL;
     joy->js.dwSize  = sizeof( joy->js );
@@ -379,14 +375,17 @@ static void fghJoystickOpen( SFG_Joystick* joy )
 
 #else
 #   ifdef __FreeBSD__
-    gint buttons[ _JS_MAX_AXES ];
+    int   buttons[ _JS_MAX_AXES ];
     float axes[ _JS_MAX_AXES ];
-    gint  noargs, in_no_axes;
-    gchar joyfname[ 1024 ];
+    int   noargs, in_no_axes;
+    char  joyfname[ 1024 ];
     FILE* joyfile;
+#   else
+#       ifndef JS_NEW
+    int counter;
+#       endif
 #   endif
-
-    gint i, counter;
+    int i;
 
     /*
      * Default for older Linux systems.
@@ -414,7 +413,7 @@ static void fghJoystickOpen( SFG_Joystick* joy )
     if( joy->error )
       return ;
 
-    sprintf( joyfname, "%s/.joy%drc", g_getenv( "HOME" ), id );
+    sprintf( joyfname, "%s/.joy%drc", g_getenv( "HOME" ), joy->id );
 
     joyfile = fopen( joyfname, "r" );
     joy->error = (joyfile == NULL);
@@ -435,8 +434,8 @@ static void fghJoystickOpen( SFG_Joystick* joy )
 
     for( i=0 ; i<_JS_MAX_AXES ; i++ )
     {
-        dead_band[ i ] = 0.0f;
-        saturate [ i ] = 1.0f;
+        joy->dead_band[ i ] = 0.0f;
+        joy->saturate [ i ] = 1.0f;
     }
 #   else
 
@@ -463,9 +462,9 @@ static void fghJoystickOpen( SFG_Joystick* joy )
 
     do
     { 
-        fghJoystickRawRead( joy, NULL, center );
+        fghJoystickRawRead( joy, NULL, joy->center )
         counter++;
-    } while( !joy->error && counter < 100 && center[ 0 ] == 512.0f && center[ 1 ] == 512.0f );
+    } while( !joy->error && counter < 100 && joy->center[ 0 ] == 512.0f && joy->center[ 1 ] == 512.0f );
    
     if( counter >= 100 )
         joy->error = TRUE;
@@ -478,7 +477,7 @@ static void fghJoystickOpen( SFG_Joystick* joy )
         joy->center[ i ] =      0.0f;
         joy->min   [ i ] = -32767.0f;
 #       else
-        joy->max[ i ] = center[ i ] * 2.0f;
+        joy->max[ i ] = joy->center[ i ] * 2.0f;
         joy->min[ i ] = 0.0f;
 #       endif
         joy->dead_band[ i ] = 0.0f ;
@@ -491,18 +490,18 @@ static void fghJoystickOpen( SFG_Joystick* joy )
 /*
  *
  */
-void fgJoystickInit( gint ident )
+void fgJoystickInit( int ident )
 {
     /*
      * Make sure we don't get reinitialized
      */
     if( fgJoystick != NULL )
-        g_error( "illegal attemp to initialize joystick device" );
+        fgError( "illegal attemp to initialize joystick device" );
 
     /*
      * Have the global joystick structure created
      */
-    fgJoystick = g_new0( SFG_Joystick, 1 );
+    fgJoystick = calloc( sizeof(SFG_Joystick), 1 );
 
 #ifdef WIN32
     switch( ident )
@@ -533,12 +532,15 @@ void fgJoystickInit( gint ident )
 void fgJoystickClose( void )
 {
     if( fgJoystick == NULL )
-        g_error( "illegal attempt to deinitialize joystick device" );
+        fgError( "illegal attempt to deinitialize joystick device" );
 
 #ifndef WIN32
     if( fgJoystick->error != TRUE )
         close( fgJoystick->fd );
 #endif
+
+    free ( fgJoystick ) ;
+    fgJoystick = NULL ;  /* show joystick has been deinitialized */
 }
 
 /*
@@ -548,14 +550,14 @@ void fgJoystickClose( void )
 void fgJoystickPollWindow( SFG_Window* window )
 {
     float axes[ _JS_MAX_AXES ];
-    gint buttons;
+    int buttons;
 
     /*
      * Make sure the joystick device is initialized, the window seems valid
      * and that there is a joystick callback hooked to it:
      */
-    freeglut_return_if_fail( fgJoystick == NULL || window == NULL );
-    freeglut_return_if_fail( window->Callbacks.Joystick == NULL );
+    freeglut_return_if_fail( fgJoystick != NULL && window != NULL );
+    freeglut_return_if_fail( window->Callbacks.Joystick != NULL );
 
     /*
      * Poll the joystick now:
@@ -567,9 +569,9 @@ void fgJoystickPollWindow( SFG_Window* window )
      */
     window->Callbacks.Joystick(
         buttons,
-        (gint) (axes[ 0 ] * 1000.0f),
-        (gint) (axes[ 1 ] * 1000.0f),
-        (gint) (axes[ 2 ] * 1000.0f)
+        (int) (axes[ 0 ] * 1000.0f),
+        (int) (axes[ 1 ] * 1000.0f),
+        (int) (axes[ 2 ] * 1000.0f)
     );
 }