]> git.mxchange.org Git - flightgear.git/blobdiff - Joystick/joystick.cxx
Moved max node per tile definition to fg_constants.h
[flightgear.git] / Joystick / joystick.cxx
index 0cba4f6e5e41223c8834af3768097f005881e27f..b5bef52802ef7dfedaedd183a57145e2b6d77642 100644 (file)
 // (Log is kept at end of this file)
 
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#ifdef HAVE_WINDOWS_H
+#  include <windows.h>                     
+#endif
+
 #include <Aircraft/aircraft.hxx>
-#include <Debug/fg_debug.h>
-#include <Joystick/js.hxx>
+#include <Debug/logstream.hxx>
+
+#if defined( ENABLE_LINUX_JOYSTICK )
+#  include <js.h>
+#elif defined( ENABLE_GLUT_JOYSTICK )
+#  include <GL/glut.h>
+#  include <XGL/xgl.h>
+#endif
+
 
 #include "joystick.hxx"
 
 
+#if defined( ENABLE_LINUX_JOYSTICK )
+
 // joystick classes
 static jsJoystick *js0;
 static jsJoystick *js1;
@@ -36,11 +53,74 @@ static jsJoystick *js1;
 // these will hold the values of the axes
 static float *js_ax0, *js_ax1;
 
+#elif defined( ENABLE_GLUT_JOYSTICK )
+
+// Do we want these user settable ??
+static float joy_scale = 1./1000;
+
+// play with following to get your desired sensitivity
+static int x_dead_zone = 50;
+static int y_dead_zone = 2*x_dead_zone;
+
+// Joystick support using glut -- William Riley -- riley@technologist.com
+
+// Joystick fixed values for calibration and scaling
+static float joy_x_max = joy_scale;
+static float joy_y_max = joy_scale;
+
+static int joy_z_min = 1000, /* joy_z_ctr=0, */ joy_z_max = -1000;
+static int joy_z_dead_min = 100, joy_z_dead_max = -100;
+
+#else
+#  error port me: no joystick support
+#endif
+
+
+
+#if defined( ENABLE_GLUT_JOYSTICK )
+
+// Function called by glutJoystickFunc(), adjusts read values and
+// passes them to the necessary aircraft control functions
+void joystick(unsigned int buttonMask, int js_x, int js_y, int js_z)
+{
+    float joy_x, joy_y, joy_z;
+    // adjust the values to fgfs's scale and allow a 'dead zone' to
+    // reduce jitter code adapted from joystick.c by Michele
+    // F. America - nomimarketing@mail.telepac.pt
+
+    if( js_x > -x_dead_zone && js_x < x_dead_zone) {
+       joy_x = 0.0;
+    } else {
+       joy_x = js_x * joy_scale;
+    }
+
+    if( js_y > -y_dead_zone && js_y < y_dead_zone) {
+       joy_y = 0.0;
+    } else {
+       joy_y = js_y * joy_scale;
+    }
+
+    if( js_z >= joy_z_dead_min && js_z <= joy_z_dead_max ) {
+       joy_z = 0.0;
+    }
+    joy_z = (float)js_z / (float)(joy_z_max - joy_z_min);
+    joy_z = (((joy_z*2.0)+1.0)/2);
+
+    // Pass the values to the control routines
+    controls.set_elevator( -joy_y );
+    controls.set_aileron( joy_x );
+    controls.set_throttle( FGControls::ALL_ENGINES, joy_z );
+}
+
+#endif // ENABLE_GLUT_JOYSTICK
+
 
 // Initialize the joystick(s)
 int fgJoystickInit( void ) {
 
-    fgPrintf( FG_INPUT, FG_INFO, "Initializing joystick\n");
+    FG_LOG( FG_INPUT, FG_INFO, "Initializing joystick" );
+
+#if defined( ENABLE_LINUX_JOYSTICK )
 
     js0 = new jsJoystick ( 0 );
     js1 = new jsJoystick ( 1 );
@@ -55,9 +135,9 @@ int fgJoystickInit( void ) {
        js0->setDeadBand( 0, 0.1 );
        js0->setDeadBand( 1, 0.1 );
 
-       fgPrintf ( FG_INPUT, FG_INFO, 
-                  "  Joystick 0 detected with %d axes\n",
-                  js0->getNumAxes() );
+       FG_LOG ( FG_INPUT, FG_INFO, 
+                "  Joystick 0 detected with " << js0->getNumAxes() 
+                << " axes" );
     }
 
     if ( js1->notWorking () ) {
@@ -70,44 +150,73 @@ int fgJoystickInit( void ) {
        js1->setDeadBand( 0, 0.1 );
        js1->setDeadBand( 1, 0.1 );
 
-       fgPrintf ( FG_INPUT, FG_INFO,
-                  "  Joystick 1 detected with %d axes\n",
-                  js1->getNumAxes() );
+       FG_LOG ( FG_INPUT, FG_INFO,
+                "  Joystick 1 detected with " << js1->getNumAxes() 
+                << " axes" );
     }
 
     if ( js0->notWorking() && js1->notWorking() ) {
-       fgPrintf ( FG_INPUT, FG_INFO, "  No joysticks detected\n" );
+       FG_LOG ( FG_INPUT, FG_INFO, "  No joysticks detected" );
        return 0;
     }
 
+#elif defined( ENABLE_GLUT_JOYSTICK )
+
+    glutJoystickFunc(joystick, 100);
+
+#else
+#  error port me: no joystick support
+#endif
+
     return 1;
 }
 
 
+#if defined( ENABLE_LINUX_JOYSTICK )
+
 // update the control parameters based on joystick intput
 int fgJoystickRead( void ) {
-    fgCONTROLS *c;
     int b;
 
-    c = current_aircraft.controls;
-
     if ( ! js0->notWorking() ) {
        js0->read( &b, js_ax0 ) ;
-       fgAileronSet( js_ax0[0] );
-       fgElevSet( -js_ax0[1] );
+       controls.set_aileron( js_ax0[0] );
+       controls.set_elevator( -js_ax0[1] );
     }
 
     if ( ! js1->notWorking() ) {
        js1->read( &b, js_ax1 ) ;
-       fgRudderSet( js_ax1[0] );
-       fgThrottleSet(FG_Throttle_All, -js_ax1[1] * 1.05 );
+       controls.set_rudder( js_ax1[0] );
+       controls.set_throttle( FGControls::ALL_ENGINES, -js_ax1[1] * 1.05 );
     }
 
     return 1;
 }
 
+#endif // ENABLE_LINUX_JOYSTICK
+
 
 // $Log$
+// Revision 1.8  1999/04/03 04:20:33  curt
+// Integration of Steve's plib conglomeration.
+//
+// Revision 1.7  1999/01/19 17:52:30  curt
+// Some joystick tweaks by Norman Vine.
+//
+// Revision 1.6  1998/12/05 16:13:16  curt
+// Renamed class fgCONTROLS to class FGControls.
+//
+// Revision 1.5  1998/11/06 21:18:04  curt
+// Converted to new logstream debugging facility.  This allows release
+// builds with no messages at all (and no performance impact) by using
+// the -DFG_NDEBUG flag.
+//
+// Revision 1.4  1998/10/27 02:14:32  curt
+// Changes to support GLUT joystick routines as fall back.
+//
+// Revision 1.3  1998/10/25 14:08:44  curt
+// Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
+//
 // Revision 1.2  1998/10/25 10:56:25  curt
 // Completely rewritten to use Steve Baker's joystick interface class.
 //