1 // joystick.cxx -- joystick support
3 // Written by Curtis Olson, started October 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // (Log is kept at end of this file)
33 #include <Aircraft/aircraft.hxx>
34 #include <Debug/logstream.hxx>
36 #if defined( ENABLE_LINUX_JOYSTICK )
38 #elif defined( ENABLE_GLUT_JOYSTICK )
44 #include "joystick.hxx"
47 #if defined( ENABLE_LINUX_JOYSTICK )
50 static jsJoystick *js0;
51 static jsJoystick *js1;
53 // these will hold the values of the axes
54 static float *js_ax0, *js_ax1;
56 #elif defined( ENABLE_GLUT_JOYSTICK )
58 // Do we want these user settable ??
59 static float joy_scale = 1./1000;
61 // play with following to get your desired sensitivity
62 static int x_dead_zone = 50;
63 static int y_dead_zone = 2*x_dead_zone;
65 // Joystick support using glut -- William Riley -- riley@technologist.com
67 // Joystick fixed values for calibration and scaling
68 static float joy_x_max = joy_scale;
69 static float joy_y_max = joy_scale;
71 static int joy_z_min = 1000, /* joy_z_ctr=0, */ joy_z_max = -1000;
72 static int joy_z_dead_min = 100, joy_z_dead_max = -100;
75 # error port me: no joystick support
80 #if defined( ENABLE_GLUT_JOYSTICK )
82 // Function called by glutJoystickFunc(), adjusts read values and
83 // passes them to the necessary aircraft control functions
84 void joystick(unsigned int buttonMask, int js_x, int js_y, int js_z)
86 float joy_x, joy_y, joy_z;
87 // adjust the values to fgfs's scale and allow a 'dead zone' to
88 // reduce jitter code adapted from joystick.c by Michele
89 // F. America - nomimarketing@mail.telepac.pt
91 if( js_x > -x_dead_zone && js_x < x_dead_zone) {
94 joy_x = js_x * joy_scale;
97 if( js_y > -y_dead_zone && js_y < y_dead_zone) {
100 joy_y = js_y * joy_scale;
103 if( js_z >= joy_z_dead_min && js_z <= joy_z_dead_max ) {
106 joy_z = (float)js_z / (float)(joy_z_max - joy_z_min);
107 joy_z = (((joy_z*2.0)+1.0)/2);
109 // Pass the values to the control routines
110 controls.set_elevator( -joy_y );
111 controls.set_aileron( joy_x );
112 controls.set_throttle( FGControls::ALL_ENGINES, joy_z );
115 #endif // ENABLE_GLUT_JOYSTICK
118 // Initialize the joystick(s)
119 int fgJoystickInit( void ) {
121 FG_LOG( FG_INPUT, FG_INFO, "Initializing joystick" );
123 #if defined( ENABLE_LINUX_JOYSTICK )
125 js0 = new jsJoystick ( 0 );
126 js1 = new jsJoystick ( 1 );
128 if ( js0->notWorking () ) {
131 // allocate storage for axes values
132 js_ax0 = new float [ js0->getNumAxes() ];
135 js0->setDeadBand( 0, 0.1 );
136 js0->setDeadBand( 1, 0.1 );
138 FG_LOG ( FG_INPUT, FG_INFO,
139 " Joystick 0 detected with " << js0->getNumAxes()
143 if ( js1->notWorking () ) {
146 // allocate storage for axes values
147 js_ax1 = new float [ js1->getNumAxes() ];
150 js1->setDeadBand( 0, 0.1 );
151 js1->setDeadBand( 1, 0.1 );
153 FG_LOG ( FG_INPUT, FG_INFO,
154 " Joystick 1 detected with " << js1->getNumAxes()
158 if ( js0->notWorking() && js1->notWorking() ) {
159 FG_LOG ( FG_INPUT, FG_INFO, " No joysticks detected" );
163 #elif defined( ENABLE_GLUT_JOYSTICK )
165 glutJoystickFunc(joystick, 100);
168 # error port me: no joystick support
175 #if defined( ENABLE_LINUX_JOYSTICK )
177 // update the control parameters based on joystick intput
178 int fgJoystickRead( void ) {
181 if ( ! js0->notWorking() ) {
182 js0->read( &b, js_ax0 ) ;
183 controls.set_aileron( js_ax0[0] );
184 controls.set_elevator( -js_ax0[1] );
187 if ( ! js1->notWorking() ) {
188 js1->read( &b, js_ax1 ) ;
189 controls.set_rudder( js_ax1[0] );
190 controls.set_throttle( FGControls::ALL_ENGINES, -js_ax1[1] * 1.05 );
196 #endif // ENABLE_LINUX_JOYSTICK
200 // Revision 1.8 1999/04/03 04:20:33 curt
201 // Integration of Steve's plib conglomeration.
203 // Revision 1.7 1999/01/19 17:52:30 curt
204 // Some joystick tweaks by Norman Vine.
206 // Revision 1.6 1998/12/05 16:13:16 curt
207 // Renamed class fgCONTROLS to class FGControls.
209 // Revision 1.5 1998/11/06 21:18:04 curt
210 // Converted to new logstream debugging facility. This allows release
211 // builds with no messages at all (and no performance impact) by using
212 // the -DFG_NDEBUG flag.
214 // Revision 1.4 1998/10/27 02:14:32 curt
215 // Changes to support GLUT joystick routines as fall back.
217 // Revision 1.3 1998/10/25 14:08:44 curt
218 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
220 // Revision 1.2 1998/10/25 10:56:25 curt
221 // Completely rewritten to use Steve Baker's joystick interface class.
223 // Revision 1.1 1998/10/24 22:28:16 curt
224 // Renamed joystick.[ch] to joystick.[ch]xx
225 // Added js.hxx which is Steve's joystick interface class.
227 // Revision 1.7 1998/04/25 22:06:29 curt
228 // Edited cvs log messages in source files ... bad bad bad!
230 // Revision 1.6 1998/04/18 04:14:05 curt
231 // Moved fg_debug.c to it's own library.
233 // Revision 1.5 1998/02/12 21:59:44 curt
234 // Incorporated code changes contributed by Charlie Hotchkiss
235 // <chotchkiss@namg.us.anritsu.com>
237 // Revision 1.4 1998/02/03 23:20:20 curt
238 // Lots of little tweaks to fix various consistency problems discovered by
239 // Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
240 // passed arguments along to the real printf(). Also incorporated HUD changes
241 // by Michele America.
243 // Revision 1.3 1998/01/27 00:47:54 curt
244 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
245 // system and commandline/config file processing code.
247 // Revision 1.2 1997/12/30 20:47:40 curt
248 // Integrated new event manager with subsystem initializations.
250 // Revision 1.1 1997/08/29 18:06:54 curt