]> git.mxchange.org Git - flightgear.git/blob - Joystick/joystick.cxx
Moved max node per tile definition to fg_constants.h
[flightgear.git] / Joystick / joystick.cxx
1 // joystick.cxx -- joystick support
2 //
3 // Written by Curtis Olson, started October 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson - curt@me.umn.edu
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>                     
31 #endif
32
33 #include <Aircraft/aircraft.hxx>
34 #include <Debug/logstream.hxx>
35
36 #if defined( ENABLE_LINUX_JOYSTICK )
37 #  include <js.h>
38 #elif defined( ENABLE_GLUT_JOYSTICK )
39 #  include <GL/glut.h>
40 #  include <XGL/xgl.h>
41 #endif
42
43
44 #include "joystick.hxx"
45
46
47 #if defined( ENABLE_LINUX_JOYSTICK )
48
49 // joystick classes
50 static jsJoystick *js0;
51 static jsJoystick *js1;
52
53 // these will hold the values of the axes
54 static float *js_ax0, *js_ax1;
55
56 #elif defined( ENABLE_GLUT_JOYSTICK )
57
58 // Do we want these user settable ??
59 static float joy_scale = 1./1000;
60
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;
64
65 // Joystick support using glut -- William Riley -- riley@technologist.com
66
67 // Joystick fixed values for calibration and scaling
68 static float joy_x_max = joy_scale;
69 static float joy_y_max = joy_scale;
70
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;
73
74 #else
75 #  error port me: no joystick support
76 #endif
77
78
79
80 #if defined( ENABLE_GLUT_JOYSTICK )
81
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)
85 {
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
90
91     if( js_x > -x_dead_zone && js_x < x_dead_zone) {
92         joy_x = 0.0;
93     } else {
94         joy_x = js_x * joy_scale;
95     }
96
97     if( js_y > -y_dead_zone && js_y < y_dead_zone) {
98         joy_y = 0.0;
99     } else {
100         joy_y = js_y * joy_scale;
101     }
102
103     if( js_z >= joy_z_dead_min && js_z <= joy_z_dead_max ) {
104         joy_z = 0.0;
105     }
106     joy_z = (float)js_z / (float)(joy_z_max - joy_z_min);
107     joy_z = (((joy_z*2.0)+1.0)/2);
108
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 );
113 }
114
115 #endif // ENABLE_GLUT_JOYSTICK
116
117
118 // Initialize the joystick(s)
119 int fgJoystickInit( void ) {
120
121     FG_LOG( FG_INPUT, FG_INFO, "Initializing joystick" );
122
123 #if defined( ENABLE_LINUX_JOYSTICK )
124
125     js0 = new jsJoystick ( 0 );
126     js1 = new jsJoystick ( 1 );
127
128     if ( js0->notWorking () ) {
129         // not working
130     } else {
131         // allocate storage for axes values
132         js_ax0 = new float [ js0->getNumAxes() ];
133
134         // configure
135         js0->setDeadBand( 0, 0.1 );
136         js0->setDeadBand( 1, 0.1 );
137
138         FG_LOG ( FG_INPUT, FG_INFO, 
139                  "  Joystick 0 detected with " << js0->getNumAxes() 
140                  << " axes" );
141     }
142
143     if ( js1->notWorking () ) {
144         // not working
145     } else {
146         // allocate storage for axes values
147         js_ax1 = new float [ js1->getNumAxes() ];
148
149         // configure
150         js1->setDeadBand( 0, 0.1 );
151         js1->setDeadBand( 1, 0.1 );
152
153         FG_LOG ( FG_INPUT, FG_INFO,
154                  "  Joystick 1 detected with " << js1->getNumAxes() 
155                  << " axes" );
156     }
157
158     if ( js0->notWorking() && js1->notWorking() ) {
159         FG_LOG ( FG_INPUT, FG_INFO, "  No joysticks detected" );
160         return 0;
161     }
162
163 #elif defined( ENABLE_GLUT_JOYSTICK )
164
165     glutJoystickFunc(joystick, 100);
166
167 #else
168 #  error port me: no joystick support
169 #endif
170
171     return 1;
172 }
173
174
175 #if defined( ENABLE_LINUX_JOYSTICK )
176
177 // update the control parameters based on joystick intput
178 int fgJoystickRead( void ) {
179     int b;
180
181     if ( ! js0->notWorking() ) {
182         js0->read( &b, js_ax0 ) ;
183         controls.set_aileron( js_ax0[0] );
184         controls.set_elevator( -js_ax0[1] );
185     }
186
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 );
191     }
192
193     return 1;
194 }
195
196 #endif // ENABLE_LINUX_JOYSTICK
197
198
199 // $Log$
200 // Revision 1.8  1999/04/03 04:20:33  curt
201 // Integration of Steve's plib conglomeration.
202 //
203 // Revision 1.7  1999/01/19 17:52:30  curt
204 // Some joystick tweaks by Norman Vine.
205 //
206 // Revision 1.6  1998/12/05 16:13:16  curt
207 // Renamed class fgCONTROLS to class FGControls.
208 //
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.
213 //
214 // Revision 1.4  1998/10/27 02:14:32  curt
215 // Changes to support GLUT joystick routines as fall back.
216 //
217 // Revision 1.3  1998/10/25 14:08:44  curt
218 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
219 //
220 // Revision 1.2  1998/10/25 10:56:25  curt
221 // Completely rewritten to use Steve Baker's joystick interface class.
222 //
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.
226 //
227 // Revision 1.7  1998/04/25 22:06:29  curt
228 // Edited cvs log messages in source files ... bad bad bad!
229 //
230 // Revision 1.6  1998/04/18 04:14:05  curt
231 // Moved fg_debug.c to it's own library.
232 //
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>
236 //
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.
242 //
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.
246 //
247 // Revision 1.2  1997/12/30 20:47:40  curt
248 // Integrated new event manager with subsystem initializations.
249 //
250 // Revision 1.1  1997/08/29 18:06:54  curt
251 // Initial revision.
252 //
253