]> git.mxchange.org Git - flightgear.git/blob - Simulator/Joystick/joystick.cxx
Initial revision.
[flightgear.git] / Simulator / 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
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
35 #if defined( ENABLE_LINUX_JOYSTICK )
36 #  include <js.h>
37 #elif defined( ENABLE_GLUT_JOYSTICK )
38 #  include <GL/glut.h>
39 #  include <XGL/xgl.h>
40 #endif
41
42
43 #include "joystick.hxx"
44
45
46 #if defined( ENABLE_LINUX_JOYSTICK )
47
48 // joystick classes
49 static jsJoystick *js0;
50 static jsJoystick *js1;
51
52 // these will hold the values of the axes
53 static float *js_ax0, *js_ax1;
54
55 #elif defined( ENABLE_GLUT_JOYSTICK )
56
57 // Do we want these user settable ??
58 static float joy_scale = 1./1000;
59
60 // play with following to get your desired sensitivity
61 static int x_dead_zone = 50;
62 static int y_dead_zone = 2*x_dead_zone;
63
64 // Joystick support using glut -- William Riley -- riley@technologist.com
65
66 // Joystick fixed values for calibration and scaling
67 static float joy_x_max = joy_scale;
68 static float joy_y_max = joy_scale;
69
70 static int joy_z_min = 1000, /* joy_z_ctr=0, */ joy_z_max = -1000;
71 static int joy_z_dead_min = 100, joy_z_dead_max = -100;
72
73 #else
74 #  error port me: no joystick support
75 #endif
76
77
78
79 #if defined( ENABLE_GLUT_JOYSTICK )
80
81 // Function called by glutJoystickFunc(), adjusts read values and
82 // passes them to the necessary aircraft control functions
83 void joystick(unsigned int buttonMask, int js_x, int js_y, int js_z)
84 {
85     float joy_x, joy_y, joy_z;
86     // adjust the values to fgfs's scale and allow a 'dead zone' to
87     // reduce jitter code adapted from joystick.c by Michele
88     // F. America - nomimarketing@mail.telepac.pt
89
90     if( js_x > -x_dead_zone && js_x < x_dead_zone) {
91         joy_x = 0.0;
92     } else {
93         joy_x = js_x * joy_scale;
94     }
95
96     if( js_y > -y_dead_zone && js_y < y_dead_zone) {
97         joy_y = 0.0;
98     } else {
99         joy_y = js_y * joy_scale;
100     }
101
102     if( js_z >= joy_z_dead_min && js_z <= joy_z_dead_max ) {
103         joy_z = 0.0;
104     }
105     joy_z = (float)js_z / (float)(joy_z_max - joy_z_min);
106     joy_z = (((joy_z*2.0)+1.0)/2);
107
108     // Pass the values to the control routines
109     controls.set_elevator( -joy_y );
110     controls.set_aileron( joy_x );
111     controls.set_throttle( FGControls::ALL_ENGINES, joy_z );
112 }
113
114 #endif // ENABLE_GLUT_JOYSTICK
115
116
117 // Initialize the joystick(s)
118 int fgJoystickInit( void ) {
119
120     FG_LOG( FG_INPUT, FG_INFO, "Initializing joystick" );
121
122 #if defined( ENABLE_LINUX_JOYSTICK )
123
124     js0 = new jsJoystick ( 0 );
125     js1 = new jsJoystick ( 1 );
126
127     if ( js0->notWorking () ) {
128         // not working
129     } else {
130         // allocate storage for axes values
131         js_ax0 = new float [ js0->getNumAxes() ];
132
133         // configure
134         js0->setDeadBand( 0, 0.1 );
135         js0->setDeadBand( 1, 0.1 );
136
137         FG_LOG ( FG_INPUT, FG_INFO, 
138                  "  Joystick 0 detected with " << js0->getNumAxes() 
139                  << " axes" );
140     }
141
142     if ( js1->notWorking () ) {
143         // not working
144     } else {
145         // allocate storage for axes values
146         js_ax1 = new float [ js1->getNumAxes() ];
147
148         // configure
149         js1->setDeadBand( 0, 0.1 );
150         js1->setDeadBand( 1, 0.1 );
151
152         FG_LOG ( FG_INPUT, FG_INFO,
153                  "  Joystick 1 detected with " << js1->getNumAxes() 
154                  << " axes" );
155     }
156
157     if ( js0->notWorking() && js1->notWorking() ) {
158         FG_LOG ( FG_INPUT, FG_INFO, "  No joysticks detected" );
159         return 0;
160     }
161
162 #elif defined( ENABLE_GLUT_JOYSTICK )
163
164     glutJoystickFunc(joystick, 100);
165
166 #else
167 #  error port me: no joystick support
168 #endif
169
170     return 1;
171 }
172
173
174 #if defined( ENABLE_LINUX_JOYSTICK )
175
176 // update the control parameters based on joystick intput
177 int fgJoystickRead( void ) {
178     int b;
179
180     if ( ! js0->notWorking() ) {
181         js0->read( &b, js_ax0 ) ;
182         controls.set_aileron( js_ax0[0] );
183         controls.set_elevator( -js_ax0[1] );
184     }
185
186     if ( ! js1->notWorking() ) {
187         js1->read( &b, js_ax1 ) ;
188         controls.set_rudder( js_ax1[0] );
189         controls.set_throttle( FGControls::ALL_ENGINES, -js_ax1[1] * 1.05 );
190     }
191
192     return 1;
193 }
194
195 #endif // ENABLE_LINUX_JOYSTICK
196
197