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;
161 if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
163 int nbuttons = MAX_BUTTONS;
166 int naxes = js->getNumAxes();
167 if (naxes > MAX_AXES) naxes = MAX_AXES;
168 joysticks[i].naxes = naxes;
169 joysticks[i].nbuttons = nbuttons;
171 FG_LOG(FG_INPUT, FG_INFO, "Initializing joystick " << i);
172 seen_joystick = true;
174 // Set up range arrays
175 float *minRange = new float[naxes];
176 float *maxRange = new float[naxes];
177 float *center = new float[naxes];
179 // Initialize with default values
180 js->getMinRange(minRange);
181 js->getMaxRange(maxRange);
182 js->getCenter(center);
184 // Allocate axes and buttons
185 joysticks[i].axes = new axis[naxes];
186 joysticks[i].buttons = new button[nbuttons];
190 // Initialize the axes.
193 for (j = 0; j < naxes; j++) {
194 axis &a = joysticks[i].axes[j];
196 string base = "/input/";
199 base += axisNames[j];
200 FG_LOG(FG_INPUT, FG_INFO, " Axis " << j << ':');
205 SGValue * value = fgGetValue(name);
207 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
210 const string &control = value->getStringValue();
211 a.value = fgGetValue(control, true);
212 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
216 name += "/dead-band";
217 value = fgGetValue(name);
219 js->setDeadBand(j, value->getDoubleValue());
220 FG_LOG(FG_INPUT, FG_INFO, " dead-band is " << js->getDeadBand(j));
225 value = fgGetValue(name);
227 a.offset = value->getDoubleValue();
228 FG_LOG(FG_INPUT, FG_INFO, " offset is " << a.offset);
234 value = fgGetValue(name);
236 a.factor = value->getDoubleValue();
237 FG_LOG(FG_INPUT, FG_INFO, " factor is " << a.factor);
242 name += "/tolerance";
243 value = fgGetValue(name);
245 a.tolerance = value->getDoubleValue();
246 FG_LOG(FG_INPUT, FG_INFO, " tolerance is " << a.tolerance);
251 name += "/saturation";
252 value = fgGetValue(name);
254 js->setSaturation(j, value->getDoubleValue());
255 FG_LOG(FG_INPUT, FG_INFO, " saturation is " << js->getSaturation(j));
259 name += "/min-range";
260 value = fgGetValue(name);
262 minRange[j] = value->getDoubleValue();
263 FG_LOG(FG_INPUT, FG_INFO, " min-range is " << minRange[j]);
267 name += "/max-range";
268 value = fgGetValue(name);
270 maxRange[j] = value->getDoubleValue();
271 FG_LOG(FG_INPUT, FG_INFO, " max-range is " << maxRange[j]);
276 value = fgGetValue(name);
278 center[j] = value->getDoubleValue();
279 FG_LOG(FG_INPUT, FG_INFO, " center is " << center[j]);
284 // Initialize the buttons.
286 for (j = 0; j < nbuttons; j++) {
287 button &b = joysticks[i].buttons[j];
289 string base = "/input/";
292 base += buttonNames[j];
293 FG_LOG(FG_INPUT, FG_INFO, " Button " << j << ':');
298 cout << "Trying name " << name << endl;
299 SGValue * value = fgGetValue(name);
301 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
304 const string &control = value->getStringValue();
305 b.value = fgGetValue(control, true);
306 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
311 value = fgGetValue(name);
313 b.step = value->getDoubleValue();
314 FG_LOG(FG_INPUT, FG_INFO, " step is " << b.step);
319 value = fgGetValue(name);
320 string action = "adjust";
322 action = value->getStringValue();
323 if (action == "toggle") {
324 b.action = button::TOGGLE;
325 b.isRepeatable = false;
326 } else if (action == "switch") {
327 b.action = button::SWITCH;
328 b.isRepeatable = false;
329 } else if (action == "adjust") {
330 b.action = button::ADJUST;
331 b.isRepeatable = true;
333 FG_LOG(FG_INPUT, FG_ALERT, " unknown action " << action);
335 b.action = button::ADJUST;
336 b.isRepeatable = true;
338 FG_LOG(FG_INPUT, FG_INFO, " action is " << action);
342 name += "/repeatable";
343 value = fgGetValue(name);
345 b.isRepeatable = value->getBoolValue();
346 FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
347 " repeatable" : " not repeatable"));
350 js->setMinRange(minRange);
351 js->setMaxRange(maxRange);
352 js->setCenter(center);
361 FG_LOG(FG_INPUT, FG_INFO, "Done initializing joysticks");
363 FG_LOG(FG_INPUT, FG_ALERT, "No joysticks detected");
365 return seen_joystick;
370 * Update property values based on the joystick state(s).
376 float *axis_values = new float[MAX_AXES];
378 for (int i = 0; i < MAX_JOYSTICKS; i++) {
379 jsJoystick * js = joysticks[i].js;
380 // float *axis_values = new float[joysticks[i].naxes];
381 if (js->notWorking()) {
385 js->read(&buttons, axis_values);
391 for (j = 0; j < joysticks[i].naxes; j++) {
393 axis &a = joysticks[i].axes[j];
395 // If the axis hasn't changed, don't
397 if (fabs(axis_values[j] - a.last_value) <= a.tolerance)
400 a.last_value = axis_values[j];
403 flag = a.value->setDoubleValue((axis_values[j] + a.offset) *
406 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
407 << i << ", axis " << j);
413 for (j = 0; j < joysticks[i].nbuttons; j++) {
415 button &b = joysticks[i].buttons[j];
420 if ((buttons & (1 << j)) > 0) {
422 if (b.lastState == 1 && !b.isRepeatable)
428 if (b.value->getDoubleValue() == 0.0)
429 flag = b.value->setDoubleValue(b.step);
431 flag = b.value->setDoubleValue(0.0);
433 if (b.value->getBoolValue())
434 flag = b.value->setBoolValue(false);
436 flag = b.value->setBoolValue(true);
440 flag = b.value->setDoubleValue(b.step);
443 flag = b.value->setDoubleValue(b.value->getDoubleValue() +
455 if (b.lastState == 0 && !b.isRepeatable)
465 flag = b.value->setDoubleValue(0.0);
475 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for "
476 << jsNames[i] << ' ' << buttonNames[j]);
486 // end of joystick.cxx