]> git.mxchange.org Git - flightgear.git/blob - src/Joystick/joystick.cxx
Updates by norman vine to controls.hxx.
[flightgear.git] / src / Joystick / joystick.cxx
1 // joystick.cxx -- joystick support
2 //
3 // Written by Curtis Olson, started October 1998.
4 //
5 // Copyright (C) 1998 - 1999  Curtis L. Olson - curt@flightgear.org
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
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <Aircraft/aircraft.hxx>
33 #include <Debug/logstream.hxx>
34 #include <Main/options.hxx>
35
36 #if defined( ENABLE_PLIB_JOYSTICK )
37 #  include <js.h>               // plib include
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_PLIB_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 #elif defined( MACOS )
75 #  warning port me: no joystick support
76 #else
77 #  error port me: no joystick support
78 #endif
79
80
81
82 #if defined( ENABLE_GLUT_JOYSTICK )
83
84 // Function called by glutJoystickFunc(), adjusts read values and
85 // passes them to the necessary aircraft control functions
86 void joystick(unsigned int buttonMask, int js_x, int js_y, int js_z)
87 {
88     float joy_x, joy_y, joy_z;
89     // adjust the values to fgfs's scale and allow a 'dead zone' to
90     // reduce jitter code adapted from joystick.c by Michele
91     // F. America - nomimarketing@mail.telepac.pt
92
93     if( js_x > -x_dead_zone && js_x < x_dead_zone) {
94         joy_x = 0.0;
95     } else {
96         joy_x = js_x * joy_scale;
97     }
98
99     if( js_y > -y_dead_zone && js_y < y_dead_zone) {
100         joy_y = 0.0;
101     } else {
102         joy_y = js_y * joy_scale;
103     }
104
105     if( js_z >= joy_z_dead_min && js_z <= joy_z_dead_max ) {
106         joy_z = 0.0;
107     }
108     joy_z = (float)js_z / (float)(joy_z_max - joy_z_min);
109     joy_z = (((joy_z*2.0)+1.0)/2);
110
111     // Pass the values to the control routines
112     controls.set_elevator( -joy_y );
113     controls.set_aileron( joy_x );
114     controls.set_throttle( FGControls::ALL_ENGINES, joy_z );
115 }
116
117 #endif // ENABLE_GLUT_JOYSTICK
118
119
120 // Initialize the joystick(s)
121 int fgJoystickInit( void ) {
122
123     FG_LOG( FG_INPUT, FG_INFO, "Initializing joystick" );
124
125 #if defined( ENABLE_PLIB_JOYSTICK )
126
127     js0 = new jsJoystick ( 0 );
128     js1 = new jsJoystick ( 1 );
129
130     if ( js0->notWorking () ) {
131         // not working
132     } else {
133         // allocate storage for axes values
134         js_ax0 = new float [ js0->getNumAxes() ];
135
136         // configure
137         js0->setDeadBand( 0, 0.1 );
138         js0->setDeadBand( 1, 0.1 );
139
140         FG_LOG ( FG_INPUT, FG_INFO, 
141                  "  Joystick 0 detected with " << js0->getNumAxes() 
142                  << " axes" );
143     }
144
145     if ( js1->notWorking () ) {
146         // not working
147     } else {
148         // allocate storage for axes values
149         js_ax1 = new float [ js1->getNumAxes() ];
150
151         // configure
152         js1->setDeadBand( 0, 0.1 );
153         js1->setDeadBand( 1, 0.1 );
154
155         FG_LOG ( FG_INPUT, FG_INFO,
156                  "  Joystick 1 detected with " << js1->getNumAxes() 
157                  << " axes" );
158     }
159
160     if ( js0->notWorking() && js1->notWorking() ) {
161         FG_LOG ( FG_INPUT, FG_INFO, "  No joysticks detected" );
162         return 0;
163     }
164
165     // I hate doing this sort of thing, but it's overridable from the
166     // command line/config file.  If the user hasn't specified an
167     // autocoordination preference, and if they have a single 2 axis
168     // joystick, then automatical enable auto_coordination.
169
170     if ( (current_options.get_auto_coordination() == 
171           fgOPTIONS::FG_AUTO_COORD_NOT_SPECIFIED) &&
172          (!js0->notWorking() && js1->notWorking() && (js0->getNumAxes() < 3)
173           )
174          )
175     {
176         current_options.set_auto_coordination(fgOPTIONS::FG_AUTO_COORD_ENABLED);
177     }
178
179
180 #elif defined( ENABLE_GLUT_JOYSTICK )
181
182     glutJoystickFunc(joystick, 100);
183
184 #elif defined( MACOS )
185 #  warning port me: no joystick support
186 #else
187 #  error port me: no joystick support
188 #endif
189
190     return 1;
191 }
192
193
194 #if defined( ENABLE_PLIB_JOYSTICK )
195
196 // update the control parameters based on joystick intput
197 int fgJoystickRead( void ) {
198     int b;
199
200     if ( ! js0->notWorking() ) {
201         js0->read( &b, js_ax0 ) ;
202         controls.set_aileron( js_ax0[0] );
203         controls.set_elevator( -js_ax0[1] );
204
205         //  Added by William Riley -- riley@technologist.com
206         if ( js0->getNumAxes() >= 3 ) {
207             controls.set_throttle( FGControls::ALL_ENGINES,
208                                    ((-js_ax0[2] + 1) / 2) );
209         } 
210         if ( js0->getNumAxes() > 3 ) {
211             if ( current_options.get_auto_coordination() !=
212                   fgOPTIONS::FG_AUTO_COORD_ENABLED ) 
213             {
214                 controls.set_rudder( js_ax0[3] );
215             }
216         }
217         //  End of William's code
218
219     }
220
221     if ( ! js1->notWorking() ) {
222         js1->read( &b, js_ax1 ) ;
223         if ( current_options.get_auto_coordination() !=
224              fgOPTIONS::FG_AUTO_COORD_ENABLED ) 
225         {
226             controls.set_rudder( js_ax1[0] );
227         }
228         controls.set_throttle( FGControls::ALL_ENGINES, -js_ax1[1] * 1.05 );
229     }
230
231     return 1;
232 }
233
234 #endif // ENABLE_PLIB_JOYSTICK
235