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 )
37 # include <Joystick/js.hxx>
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 // Joystick support using glut -- William Riley -- riley@technologist.com
60 // Joystick fixed values for calibration and scaling
61 static int joy_x_min=1000, /* joy_x_ctr=0, */ joy_x_max=-1000;
62 static int joy_y_min=1000, /* joy_y_ctr=0, */ joy_y_max=-1000;
63 static int joy_z_min=1000, /* joy_z_ctr=0, */ joy_z_max=-1000;
64 static int joy_x_dead_min=100, joy_x_dead_max=-100;
65 static int joy_y_dead_min=100, joy_y_dead_max=-100;
66 static int joy_z_dead_min=100, joy_z_dead_max=-100;
69 # error port me: no joystick support
74 #if defined( ENABLE_GLUT_JOYSTICK )
76 // Function called by glutJoystickFunc(), adjusts read values and
77 // passes them to the necessary aircraft control functions
78 void joystick(unsigned int buttonMask, int js_x, int js_y, int js_z)
80 double joy_x, joy_y, joy_z;
81 // adjust the values to fgfs's scale and allow a 'dead zone' to
82 // reduce jitter code adapted from joystick.c by Michele
83 // F. America - nomimarketing@mail.telepac.pt
84 if( js_x >= joy_x_dead_min && js_x <= joy_x_dead_max ) {
87 joy_x = (double)js_x/(double)(joy_x_max-joy_x_min);
89 if( js_y >= joy_y_dead_min && js_y <= joy_y_dead_max ) {
92 joy_y = (double)js_y/(double)(joy_y_max-joy_y_min);
94 if( js_z >= joy_z_dead_min && js_z <= joy_z_dead_max ) {
97 joy_z = (double)js_z/(double)(joy_z_max-joy_z_min);
99 // Scale the values up for full range of motion
102 joy_z = (((joy_z*2.0)+1.0)/2);
103 // Pass the values to the control routines
104 controls.set_elevator( joy_y );
105 controls.set_aileron( -joy_x );
106 controls.set_throttle( fgCONTROLS::FG_ALL_ENGINES, joy_z );
109 #endif // ENABLE_GLUT_JOYSTICK
112 // Initialize the joystick(s)
113 int fgJoystickInit( void ) {
115 FG_LOG( FG_INPUT, FG_INFO, "Initializing joystick" );
117 #if defined( ENABLE_LINUX_JOYSTICK )
119 js0 = new jsJoystick ( 0 );
120 js1 = new jsJoystick ( 1 );
122 if ( js0->notWorking () ) {
125 // allocate storage for axes values
126 js_ax0 = new float [ js0->getNumAxes() ];
129 js0->setDeadBand( 0, 0.1 );
130 js0->setDeadBand( 1, 0.1 );
132 FG_LOG ( FG_INPUT, FG_INFO,
133 " Joystick 0 detected with " << js0->getNumAxes()
137 if ( js1->notWorking () ) {
140 // allocate storage for axes values
141 js_ax1 = new float [ js1->getNumAxes() ];
144 js1->setDeadBand( 0, 0.1 );
145 js1->setDeadBand( 1, 0.1 );
147 FG_LOG ( FG_INPUT, FG_INFO,
148 " Joystick 1 detected with " << js1->getNumAxes()
152 if ( js0->notWorking() && js1->notWorking() ) {
153 FG_LOG ( FG_INPUT, FG_INFO, " No joysticks detected" );
157 #elif defined( ENABLE_GLUT_JOYSTICK )
159 glutJoystickFunc(joystick, 100);
162 # error port me: no joystick support
169 #if defined( ENABLE_LINUX_JOYSTICK )
171 // update the control parameters based on joystick intput
172 int fgJoystickRead( void ) {
175 if ( ! js0->notWorking() ) {
176 js0->read( &b, js_ax0 ) ;
177 controls.set_aileron( js_ax0[0] );
178 controls.set_elevator( -js_ax0[1] );
181 if ( ! js1->notWorking() ) {
182 js1->read( &b, js_ax1 ) ;
183 controls.set_rudder( js_ax1[0] );
184 controls.set_throttle( fgCONTROLS::FG_ALL_ENGINES, -js_ax1[1] * 1.05 );
190 #endif // ENABLE_LINUX_JOYSTICK
194 // Revision 1.5 1998/11/06 21:18:04 curt
195 // Converted to new logstream debugging facility. This allows release
196 // builds with no messages at all (and no performance impact) by using
197 // the -DFG_NDEBUG flag.
199 // Revision 1.4 1998/10/27 02:14:32 curt
200 // Changes to support GLUT joystick routines as fall back.
202 // Revision 1.3 1998/10/25 14:08:44 curt
203 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
205 // Revision 1.2 1998/10/25 10:56:25 curt
206 // Completely rewritten to use Steve Baker's joystick interface class.
208 // Revision 1.1 1998/10/24 22:28:16 curt
209 // Renamed joystick.[ch] to joystick.[ch]xx
210 // Added js.hxx which is Steve's joystick interface class.
212 // Revision 1.7 1998/04/25 22:06:29 curt
213 // Edited cvs log messages in source files ... bad bad bad!
215 // Revision 1.6 1998/04/18 04:14:05 curt
216 // Moved fg_debug.c to it's own library.
218 // Revision 1.5 1998/02/12 21:59:44 curt
219 // Incorporated code changes contributed by Charlie Hotchkiss
220 // <chotchkiss@namg.us.anritsu.com>
222 // Revision 1.4 1998/02/03 23:20:20 curt
223 // Lots of little tweaks to fix various consistency problems discovered by
224 // Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
225 // passed arguments along to the real printf(). Also incorporated HUD changes
226 // by Michele America.
228 // Revision 1.3 1998/01/27 00:47:54 curt
229 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
230 // system and commandline/config file processing code.
232 // Revision 1.2 1997/12/30 20:47:40 curt
233 // Integrated new event manager with subsystem initializations.
235 // Revision 1.1 1997/08/29 18:06:54 curt