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 <simgear/misc/props.hxx>
39 #include <simgear/debug/logstream.hxx>
41 #include "joystick.hxx"
47 static const int MAX_JOYSTICKS = 2;
49 static const int MAX_JOYSTICKS = 10;
51 static const int MAX_AXES = _JS_MAX_AXES;
52 static const int MAX_BUTTONS = 32;
56 * Property names for joysticks and axes.
58 static const char * jsNames[] = {
59 "js0", "js1", "js2", "js3", "js4",
60 "js5", "js6", "js7", "js8", "js9"
62 static const char * axisNames[] = {
63 "axis0", "axis1", "axis2", "axis3", "axis4",
64 "axis5", "axis6", "axis7", "axis8", "axis9"
66 static const char * buttonNames[] = {
67 "button0", "button1", "button2", "button3", "button4",
68 "button5", "button6", "button7", "button8", "button9",
69 "button10", "button11", "button12", "button13", "button14",
70 "button15", "button16", "button17", "button18", "button19",
71 "button20", "button21", "button22", "button23", "button24",
72 "button25", "button26", "button27", "button28", "button29",
73 "button30", "button31"
78 * Settings for a single axis.
81 axis () : value(0), offset(0.0), factor(1.0),
82 last_value(9999999), tolerance(0.002) {}
92 * Settings for a single button.
100 button () : value(0), step(0.0), action(ADJUST), isRepeatable(true),
111 * Settings for a single joystick.
114 virtual ~joystick () {
133 * Array of joystick settings.
135 static joystick joysticks[MAX_JOYSTICKS];
139 * Initialize any joysticks found.
144 bool seen_joystick = false;
146 FG_LOG(FG_INPUT, FG_INFO, "Initializing joysticks");
148 for (int i = 0; i < MAX_JOYSTICKS; i++) {
149 jsJoystick * js = new jsJoystick(i);
150 joysticks[i].js = js;
151 if (js->notWorking()) {
152 FG_LOG(FG_INPUT, FG_INFO, "Joystick " << i << " not found");
157 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
158 int nbuttons = jsCaps.wNumButtons;
160 int nbuttons = MAX_BUTTONS;
163 int naxes = js->getNumAxes();
164 joysticks[i].naxes = naxes;
165 joysticks[i].nbuttons = nbuttons;
167 FG_LOG(FG_INPUT, FG_INFO, "Initializing joystick " << i);
168 seen_joystick = true;
170 // Set up range arrays
171 float *minRange = new float[naxes];
172 float *maxRange = new float[naxes];
173 float *center = new float[naxes];
175 // Initialize with default values
176 js->getMinRange(minRange);
177 js->getMaxRange(maxRange);
178 js->getCenter(center);
180 // Allocate axes and buttons
181 joysticks[i].axes = new axis[naxes];
182 joysticks[i].buttons = new button[nbuttons];
186 // Initialize the axes.
189 for (j = 0; j < naxes; j++) {
190 axis &a = joysticks[i].axes[j];
192 string base = "/input/";
195 base += axisNames[j];
196 FG_LOG(FG_INPUT, FG_INFO, " Axis " << j << ':');
201 SGValue * value = current_properties.getValue(name);
203 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
206 const string &control = value->getStringValue();
207 a.value = current_properties.getValue(control, true);
208 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
212 name += "/dead-band";
213 value = current_properties.getValue(name);
215 js->setDeadBand(j, value->getDoubleValue());
216 FG_LOG(FG_INPUT, FG_INFO, " dead-band is " << js->getDeadBand(j));
221 value = current_properties.getValue(name);
223 a.offset = value->getDoubleValue();
224 FG_LOG(FG_INPUT, FG_INFO, " offset is " << a.offset);
230 value = current_properties.getValue(name);
232 a.factor = value->getDoubleValue();
233 FG_LOG(FG_INPUT, FG_INFO, " factor is " << a.factor);
238 name += "/tolerance";
239 value = current_properties.getValue(name);
241 a.tolerance = value->getDoubleValue();
242 FG_LOG(FG_INPUT, FG_INFO, " tolerance is " << a.tolerance);
247 name += "/saturation";
248 value = current_properties.getValue(name);
250 js->setSaturation(j, value->getDoubleValue());
251 FG_LOG(FG_INPUT, FG_INFO, " saturation is " << js->getSaturation(j));
255 name += "/min-range";
256 value = current_properties.getValue(name);
258 minRange[j] = value->getDoubleValue();
259 FG_LOG(FG_INPUT, FG_INFO, " min-range is " << minRange[j]);
263 name += "/max-range";
264 value = current_properties.getValue(name);
266 maxRange[j] = value->getDoubleValue();
267 FG_LOG(FG_INPUT, FG_INFO, " max-range is " << maxRange[j]);
272 value = current_properties.getValue(name);
274 center[j] = value->getDoubleValue();
275 FG_LOG(FG_INPUT, FG_INFO, " center is " << center[j]);
280 // Initialize the buttons.
282 for (j = 0; j < nbuttons; j++) {
283 button &b = joysticks[i].buttons[j];
285 string base = "/input/";
288 base += buttonNames[j];
289 FG_LOG(FG_INPUT, FG_INFO, " Button " << j << ':');
294 cout << "Trying name " << name << endl;
295 SGValue * value = current_properties.getValue(name);
297 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
300 const string &control = value->getStringValue();
301 b.value = current_properties.getValue(control, true);
302 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
307 value = current_properties.getValue(name);
309 b.step = value->getDoubleValue();
310 FG_LOG(FG_INPUT, FG_INFO, " step is " << b.step);
315 value = current_properties.getValue(name);
316 string action = "adjust";
318 action = value->getStringValue();
319 if (action == "toggle") {
320 b.action = button::TOGGLE;
321 b.isRepeatable = false;
322 } else if (action == "switch") {
323 b.action = button::SWITCH;
324 b.isRepeatable = false;
325 } else if (action == "adjust") {
326 b.action = button::ADJUST;
327 b.isRepeatable = true;
329 FG_LOG(FG_INPUT, FG_ALERT, " unknown action " << action);
331 b.action = button::ADJUST;
332 b.isRepeatable = true;
334 FG_LOG(FG_INPUT, FG_INFO, " action is " << action);
338 name += "/repeatable";
339 value = current_properties.getValue(name);
341 b.isRepeatable = value->getBoolValue();
342 FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
343 " repeatable" : " not repeatable"));
346 js->setMinRange(minRange);
347 js->setMaxRange(maxRange);
348 js->setCenter(center);
357 FG_LOG(FG_INPUT, FG_INFO, "Done initializing joysticks");
359 FG_LOG(FG_INPUT, FG_ALERT, "No joysticks detected");
361 return seen_joystick;
366 * Update property values based on the joystick state(s).
372 float *axis_values = new float[MAX_AXES];
374 for (int i = 0; i < MAX_JOYSTICKS; i++) {
375 jsJoystick * js = joysticks[i].js;
376 // float *axis_values = new float[joysticks[i].naxes];
377 if (js->notWorking()) {
381 js->read(&buttons, axis_values);
387 for (j = 0; j < joysticks[i].naxes; j++) {
389 axis &a = joysticks[i].axes[j];
391 // If the axis hasn't changed, don't
393 if (fabs(axis_values[j] - a.last_value) <= a.tolerance)
396 a.last_value = axis_values[j];
399 flag = a.value->setDoubleValue((axis_values[j] + a.offset) *
402 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
403 << i << ", axis " << j);
409 for (j = 0; j < joysticks[i].nbuttons; j++) {
411 button &b = joysticks[i].buttons[j];
416 if ((buttons & (1 << j)) > 0) {
418 if (b.lastState == 1 && !b.isRepeatable)
424 if (b.value->getDoubleValue() == 0.0)
425 flag = b.value->setDoubleValue(b.step);
427 flag = b.value->setDoubleValue(0.0);
429 if (b.value->getBoolValue())
430 flag = b.value->setBoolValue(false);
432 flag = b.value->setBoolValue(true);
436 flag = b.value->setDoubleValue(b.step);
439 if (!b.value->setDoubleValue(b.value->getDoubleValue() + b.step))
440 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
441 << i << ", axis " << j);
452 if (b.lastState == 0 && !b.isRepeatable)
462 flag = b.value->setDoubleValue(0.0);
472 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for "
473 << jsNames[i] << ' ' << buttonNames[j]);
483 // end of joystick.cxx