]> git.mxchange.org Git - flightgear.git/blob - Joystick/joystick.cxx
63c207a6e28505fc2bff0e96373d7a5cb2413630
[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 <Joystick/js.hxx>
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     // Joystick support using glut -- William Riley -- riley@technologist.com
59
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;
67
68 #else
69 #  error port me: no joystick support
70 #endif
71
72
73
74 #if defined( ENABLE_GLUT_JOYSTICK )
75
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)
79 {
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 ) {
85         joy_x = 0.0;
86     } else {
87         joy_x = (double)js_x/(double)(joy_x_max-joy_x_min);
88     }   
89     if( js_y >= joy_y_dead_min && js_y <= joy_y_dead_max ) {
90         joy_y = 0.0;
91     } else {
92         joy_y = (double)js_y/(double)(joy_y_max-joy_y_min);
93     }
94     if( js_z >= joy_z_dead_min && js_z <= joy_z_dead_max ) {
95         joy_z = 0.0;
96     } else {
97         joy_z = (double)js_z/(double)(joy_z_max-joy_z_min);
98     }
99     // Scale the values up for full range of motion
100     joy_x *= 2.0;
101     joy_y *= 2.0;
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 );
107 }
108
109 #endif // ENABLE_GLUT_JOYSTICK
110
111
112 // Initialize the joystick(s)
113 int fgJoystickInit( void ) {
114
115     FG_LOG( FG_INPUT, FG_INFO, "Initializing joystick" );
116
117 #if defined( ENABLE_LINUX_JOYSTICK )
118
119     js0 = new jsJoystick ( 0 );
120     js1 = new jsJoystick ( 1 );
121
122     if ( js0->notWorking () ) {
123         // not working
124     } else {
125         // allocate storage for axes values
126         js_ax0 = new float [ js0->getNumAxes() ];
127
128         // configure
129         js0->setDeadBand( 0, 0.1 );
130         js0->setDeadBand( 1, 0.1 );
131
132         FG_LOG ( FG_INPUT, FG_INFO, 
133                  "  Joystick 0 detected with " << js0->getNumAxes() 
134                  << " axes" );
135     }
136
137     if ( js1->notWorking () ) {
138         // not working
139     } else {
140         // allocate storage for axes values
141         js_ax1 = new float [ js1->getNumAxes() ];
142
143         // configure
144         js1->setDeadBand( 0, 0.1 );
145         js1->setDeadBand( 1, 0.1 );
146
147         FG_LOG ( FG_INPUT, FG_INFO,
148                  "  Joystick 1 detected with " << js1->getNumAxes() 
149                  << " axes" );
150     }
151
152     if ( js0->notWorking() && js1->notWorking() ) {
153         FG_LOG ( FG_INPUT, FG_INFO, "  No joysticks detected" );
154         return 0;
155     }
156
157 #elif defined( ENABLE_GLUT_JOYSTICK )
158
159     glutJoystickFunc(joystick, 100);
160
161 #else
162 #  error port me: no joystick support
163 #endif
164
165     return 1;
166 }
167
168
169 #if defined( ENABLE_LINUX_JOYSTICK )
170
171 // update the control parameters based on joystick intput
172 int fgJoystickRead( void ) {
173     int b;
174
175     if ( ! js0->notWorking() ) {
176         js0->read( &b, js_ax0 ) ;
177         controls.set_aileron( js_ax0[0] );
178         controls.set_elevator( -js_ax0[1] );
179     }
180
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 );
185     }
186
187     return 1;
188 }
189
190 #endif // ENABLE_LINUX_JOYSTICK
191
192
193 // $Log$
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.
198 //
199 // Revision 1.4  1998/10/27 02:14:32  curt
200 // Changes to support GLUT joystick routines as fall back.
201 //
202 // Revision 1.3  1998/10/25 14:08:44  curt
203 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
204 //
205 // Revision 1.2  1998/10/25 10:56:25  curt
206 // Completely rewritten to use Steve Baker's joystick interface class.
207 //
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.
211 //
212 // Revision 1.7  1998/04/25 22:06:29  curt
213 // Edited cvs log messages in source files ... bad bad bad!
214 //
215 // Revision 1.6  1998/04/18 04:14:05  curt
216 // Moved fg_debug.c to it's own library.
217 //
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>
221 //
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.
227 //
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.
231 //
232 // Revision 1.2  1997/12/30 20:47:40  curt
233 // Integrated new event manager with subsystem initializations.
234 //
235 // Revision 1.1  1997/08/29 18:06:54  curt
236 // Initial revision.
237 //
238