1 // joystick.cxx -- joystick support
3 // Original module written by Curtis Olson, started October 1998.
4 // Completely rewritten by David Megginson, July 2000.
6 // Copyright (C) 1998 - 2000 Curtis L. Olson - curt@flightgear.org
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/compiler.h>
38 #include <Main/fg_props.hxx>
40 #include <simgear/debug/logstream.hxx>
43 #include "joystick.hxx"
49 static const int MAX_JOYSTICKS = 2;
51 static const int MAX_JOYSTICKS = 10;
53 static const int MAX_AXES = _JS_MAX_AXES;
54 static const int MAX_BUTTONS = 32;
58 * Property names for joysticks and axes.
60 static const char * jsNames[] = {
61 "js0", "js1", "js2", "js3", "js4",
62 "js5", "js6", "js7", "js8", "js9"
64 static const char * axisNames[] = {
65 "axis0", "axis1", "axis2", "axis3", "axis4",
66 "axis5", "axis6", "axis7", "axis8", "axis9"
68 static const char * buttonNames[] = {
69 "button0", "button1", "button2", "button3", "button4",
70 "button5", "button6", "button7", "button8", "button9",
71 "button10", "button11", "button12", "button13", "button14",
72 "button15", "button16", "button17", "button18", "button19",
73 "button20", "button21", "button22", "button23", "button24",
74 "button25", "button26", "button27", "button28", "button29",
75 "button30", "button31"
80 * Settings for a single axis.
83 axis () : value(0), offset(0.0), factor(1.0),
84 last_value(9999999), tolerance(0.002) {}
94 * Settings for a single button.
102 button () : value(0), step(0.0), action(ADJUST), isRepeatable(true),
113 * Settings for a single joystick.
116 virtual ~joystick () {
135 * Array of joystick settings.
137 static joystick joysticks[MAX_JOYSTICKS];
141 * Initialize any joysticks found.
146 bool seen_joystick = false;
148 FG_LOG(FG_INPUT, FG_INFO, "Initializing joysticks");
150 for (int i = 0; i < MAX_JOYSTICKS; i++) {
151 jsJoystick * js = new jsJoystick(i);
152 joysticks[i].js = js;
153 if (js->notWorking()) {
154 FG_LOG(FG_INPUT, FG_INFO, "Joystick " << i << " not found");
159 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
160 int nbuttons = jsCaps.wNumButtons;
162 int nbuttons = MAX_BUTTONS;
165 int naxes = js->getNumAxes();
166 joysticks[i].naxes = naxes;
167 joysticks[i].nbuttons = nbuttons;
169 FG_LOG(FG_INPUT, FG_INFO, "Initializing joystick " << i);
170 seen_joystick = true;
172 // Set up range arrays
173 float *minRange = new float[naxes];
174 float *maxRange = new float[naxes];
175 float *center = new float[naxes];
177 // Initialize with default values
178 js->getMinRange(minRange);
179 js->getMaxRange(maxRange);
180 js->getCenter(center);
182 // Allocate axes and buttons
183 joysticks[i].axes = new axis[naxes];
184 joysticks[i].buttons = new button[nbuttons];
188 // Initialize the axes.
191 for (j = 0; j < naxes; j++) {
192 axis &a = joysticks[i].axes[j];
194 string base = "/input/";
197 base += axisNames[j];
198 FG_LOG(FG_INPUT, FG_INFO, " Axis " << j << ':');
203 SGValue * value = fgGetValue(name);
205 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
208 const string &control = value->getStringValue();
209 a.value = fgGetValue(control, true);
210 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
214 name += "/dead-band";
215 value = fgGetValue(name);
217 js->setDeadBand(j, value->getDoubleValue());
218 FG_LOG(FG_INPUT, FG_INFO, " dead-band is " << js->getDeadBand(j));
223 value = fgGetValue(name);
225 a.offset = value->getDoubleValue();
226 FG_LOG(FG_INPUT, FG_INFO, " offset is " << a.offset);
232 value = fgGetValue(name);
234 a.factor = value->getDoubleValue();
235 FG_LOG(FG_INPUT, FG_INFO, " factor is " << a.factor);
240 name += "/tolerance";
241 value = fgGetValue(name);
243 a.tolerance = value->getDoubleValue();
244 FG_LOG(FG_INPUT, FG_INFO, " tolerance is " << a.tolerance);
249 name += "/saturation";
250 value = fgGetValue(name);
252 js->setSaturation(j, value->getDoubleValue());
253 FG_LOG(FG_INPUT, FG_INFO, " saturation is " << js->getSaturation(j));
257 name += "/min-range";
258 value = fgGetValue(name);
260 minRange[j] = value->getDoubleValue();
261 FG_LOG(FG_INPUT, FG_INFO, " min-range is " << minRange[j]);
265 name += "/max-range";
266 value = fgGetValue(name);
268 maxRange[j] = value->getDoubleValue();
269 FG_LOG(FG_INPUT, FG_INFO, " max-range is " << maxRange[j]);
274 value = fgGetValue(name);
276 center[j] = value->getDoubleValue();
277 FG_LOG(FG_INPUT, FG_INFO, " center is " << center[j]);
282 // Initialize the buttons.
284 for (j = 0; j < nbuttons; j++) {
285 button &b = joysticks[i].buttons[j];
287 string base = "/input/";
290 base += buttonNames[j];
291 FG_LOG(FG_INPUT, FG_INFO, " Button " << j << ':');
296 cout << "Trying name " << name << endl;
297 SGValue * value = fgGetValue(name);
299 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
302 const string &control = value->getStringValue();
303 b.value = fgGetValue(control, true);
304 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
309 value = fgGetValue(name);
311 b.step = value->getDoubleValue();
312 FG_LOG(FG_INPUT, FG_INFO, " step is " << b.step);
317 value = fgGetValue(name);
318 string action = "adjust";
320 action = value->getStringValue();
321 if (action == "toggle") {
322 b.action = button::TOGGLE;
323 b.isRepeatable = false;
324 } else if (action == "switch") {
325 b.action = button::SWITCH;
326 b.isRepeatable = false;
327 } else if (action == "adjust") {
328 b.action = button::ADJUST;
329 b.isRepeatable = true;
331 FG_LOG(FG_INPUT, FG_ALERT, " unknown action " << action);
333 b.action = button::ADJUST;
334 b.isRepeatable = true;
336 FG_LOG(FG_INPUT, FG_INFO, " action is " << action);
340 name += "/repeatable";
341 value = fgGetValue(name);
343 b.isRepeatable = value->getBoolValue();
344 FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
345 " repeatable" : " not repeatable"));
348 js->setMinRange(minRange);
349 js->setMaxRange(maxRange);
350 js->setCenter(center);
359 FG_LOG(FG_INPUT, FG_INFO, "Done initializing joysticks");
361 FG_LOG(FG_INPUT, FG_ALERT, "No joysticks detected");
363 return seen_joystick;
368 * Update property values based on the joystick state(s).
374 float *axis_values = new float[MAX_AXES];
376 for (int i = 0; i < MAX_JOYSTICKS; i++) {
377 jsJoystick * js = joysticks[i].js;
378 // float *axis_values = new float[joysticks[i].naxes];
379 if (js->notWorking()) {
383 js->read(&buttons, axis_values);
389 for (j = 0; j < joysticks[i].naxes; j++) {
391 axis &a = joysticks[i].axes[j];
393 // If the axis hasn't changed, don't
395 if (fabs(axis_values[j] - a.last_value) <= a.tolerance)
398 a.last_value = axis_values[j];
401 flag = a.value->setDoubleValue((axis_values[j] + a.offset) *
404 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
405 << i << ", axis " << j);
411 for (j = 0; j < joysticks[i].nbuttons; j++) {
413 button &b = joysticks[i].buttons[j];
418 if ((buttons & (1 << j)) > 0) {
420 if (b.lastState == 1 && !b.isRepeatable)
426 if (b.value->getDoubleValue() == 0.0)
427 flag = b.value->setDoubleValue(b.step);
429 flag = b.value->setDoubleValue(0.0);
431 if (b.value->getBoolValue())
432 flag = b.value->setBoolValue(false);
434 flag = b.value->setBoolValue(true);
438 flag = b.value->setDoubleValue(b.step);
441 flag = b.value->setDoubleValue(b.value->getDoubleValue() +
453 if (b.lastState == 0 && !b.isRepeatable)
463 flag = b.value->setDoubleValue(0.0);
473 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for "
474 << jsNames[i] << ' ' << buttonNames[j]);
484 // end of joystick.cxx