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.
37 #include <simgear/misc/props.hxx>
38 #include <simgear/debug/logstream.hxx>
40 #include "joystick.hxx"
45 static const int MAX_JOYSTICKS = 2;
47 static const int MAX_JOYSTICKS = 10;
49 static const int MAX_AXES = _JS_MAX_AXES;
50 static const int MAX_BUTTONS = 32;
54 * Property names for joysticks and axes.
56 static const char * jsNames[] = {
57 "js0", "js1", "js2", "js3", "js4",
58 "js5", "js6", "js7", "js8", "js9"
60 static const char * axisNames[] = {
61 "axis0", "axis1", "axis2", "axis3", "axis4",
62 "axis5", "axis6", "axis7", "axis8", "axis9"
64 static const char * buttonNames[] = {
65 "button0", "button1", "button2", "button3", "button4",
66 "button5", "button6", "button7", "button8", "button9",
67 "button10", "button11", "button12", "button13", "button14",
68 "button15", "button16", "button17", "button18", "button19",
69 "button20", "button21", "button22", "button23", "button24",
70 "button25", "button26", "button27", "button28", "button29",
71 "button30", "button31"
76 * Settings for a single axis.
79 axis () : value(0), offset(0.0), factor(1.0),
80 last_value(9999999), tolerance(0.002) {}
90 * Settings for a single button.
98 button () : value(0), step(0.0), action(ADJUST), isRepeatable(true),
109 * Settings for a single joystick.
112 virtual ~joystick () {
126 * Array of joystick settings.
128 static joystick joysticks[MAX_JOYSTICKS];
132 * Set up default values if properties are not specified.
137 SGPropertyList &props = current_properties;
139 // Default axis 0 to aileron
140 if (!props.getValue("/input/js0/axis0/control")) {
141 props.setStringValue("/input/js0/axis0/control", "/controls/aileron");
142 props.setDoubleValue("/input/js0/axis0/dead-band", 0.1);
145 // Default axis 1 to elevator
146 if (!props.getValue("/input/js0/axis1/control")) {
147 props.setStringValue("/input/js0/axis1/control", "/controls/elevator");
148 props.setDoubleValue("/input/js0/axis1/dead-band", 0.1);
149 props.setDoubleValue("/input/js0/axis1/factor", -1.0);
152 // Default axis 2 to rudder
153 if (!props.getValue("/input/js0/axis2/control")) {
154 props.setStringValue("/input/js0/axis2/control", "/controls/rudder");
155 props.setDoubleValue("/input/js0/axis2/dead-band", 0.1);
158 // Default axis 3 to throttle
159 // We need to fiddle with the offset
160 // and factor to make it work
161 if (!props.getValue("/input/js0/axis3/control")) {
162 props.setStringValue("/input/js0/axis3/control", "/controls/throttle");
163 props.setDoubleValue("/input/js0/axis3/dead-band", 0.0);
164 props.setDoubleValue("/input/js0/axis3/offset", -1.0);
165 props.setDoubleValue("/input/js0/axis3/factor", -0.5);
168 // Default button 0 to all brakes
169 if (!props.getValue("/input/js0/button0/control")) {
170 props.setStringValue("/input/js0/button0/action", "switch");
171 props.setStringValue("/input/js0/button0/control", "/controls/brakes/all");
172 props.setDoubleValue("/input/js0/button0/step", 1.0);
173 props.setDoubleValue("/input/js0/button0/repeatable", false);
176 // Default button 1 to left brake.
177 if (!props.getValue("/input/js0/button1/control")) {
178 props.setStringValue("/input/js0/button1/action", "switch");
179 props.setStringValue("/input/js0/button1/control",
180 "/controls/brakes/left");
181 props.setDoubleValue("/input/js0/button1/step", 1.0);
182 props.setDoubleValue("/input/js0/button1/repeatable", false);
185 // Default button 2 to right brake.
186 if (!props.getValue("/input/js0/button2/control")) {
187 props.setStringValue("/input/js0/button2/action", "switch");
188 props.setStringValue("/input/js0/button2/control",
189 "/controls/brakes/right");
190 props.setDoubleValue("/input/js0/button2/step", 1.0);
191 props.setDoubleValue("/input/js0/button2/repeatable", false);
194 // Default buttons 3 and 4 to elevator trim
195 if (!props.getValue("/input/js0/button3/control")) {
196 props.setStringValue("/input/js0/button3/action", "adjust");
197 props.setStringValue("/input/js0/button3/control",
198 "/controls/elevator-trim");
199 props.setDoubleValue("/input/js0/button3/step", 0.001);
200 props.setBoolValue("/input/js0/button3/repeatable", true);
202 if (!props.getValue("/input/js0/button4/control")) {
203 props.setStringValue("/input/js0/button4/action", "adjust");
204 props.setStringValue("/input/js0/button4/control",
205 "/controls/elevator-trim");
206 props.setDoubleValue("/input/js0/button4/step", -0.001);
207 props.setBoolValue("/input/js0/button4/repeatable", true);
210 // Default buttons 5 and 6 to flaps
211 if (!props.getValue("/input/js0/button5/control")) {
212 props.setStringValue("/input/js0/button5/action", "adjust");
213 props.setStringValue("/input/js0/button5/control", "/controls/flaps");
214 props.setDoubleValue("/input/js0/button5/step", -0.34);
215 props.setBoolValue("/input/js0/button5/repeatable", false);
217 if (!props.getValue("/input/js0/button6/control")) {
218 props.setStringValue("/input/js0/button6/action", "adjust");
219 props.setStringValue("/input/js0/button6/control", "/controls/flaps");
220 props.setDoubleValue("/input/js0/button6/step", 0.34);
221 props.setBoolValue("/input/js0/button6/repeatable", false);
227 * Initialize any joysticks found.
232 bool seen_joystick = false;
234 FG_LOG(FG_INPUT, FG_INFO, "Initializing joysticks");
238 for (int i = 0; i < MAX_JOYSTICKS; i++) {
239 jsJoystick * js = new jsJoystick(i);
240 joysticks[i].js = js;
241 if (js->notWorking()) {
242 FG_LOG(FG_INPUT, FG_INFO, "Joystick " << i << " not found");
247 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
248 int nbuttons = jsCaps.wNumButtons;
250 int nbuttons = MAX_BUTTONS;
253 int naxes = js->getNumAxes();
254 joysticks[i].naxes = naxes;
255 joysticks[i].nbuttons = nbuttons;
257 FG_LOG(FG_INPUT, FG_INFO, "Initializing joystick " << i);
258 seen_joystick = true;
260 // Set up range arrays
261 float *minRange = new float[naxes];
262 float *maxRange = new float[naxes];
263 float *center = new float[naxes];
265 // Initialize with default values
266 js->getMinRange(minRange);
267 js->getMaxRange(maxRange);
268 js->getCenter(center);
270 // Allocate axes and buttons
271 joysticks[i].axes = new axis[naxes];
272 joysticks[i].buttons = new button[nbuttons];
276 // Initialize the axes.
279 for (j = 0; j < naxes; j++) {
280 axis &a = joysticks[i].axes[j];
282 string base = "/input/";
285 base += axisNames[j];
286 FG_LOG(FG_INPUT, FG_INFO, " Axis " << j << ':');
291 SGValue * value = current_properties.getValue(name);
293 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
296 const string &control = value->getStringValue();
297 a.value = current_properties.getValue(control, true);
298 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
302 name += "/dead-band";
303 value = current_properties.getValue(name);
305 js->setDeadBand(j, value->getDoubleValue());
306 FG_LOG(FG_INPUT, FG_INFO, " dead-band is " << js->getDeadBand(j));
311 value = current_properties.getValue(name);
313 a.offset = value->getDoubleValue();
314 FG_LOG(FG_INPUT, FG_INFO, " offset is " << a.offset);
320 value = current_properties.getValue(name);
322 a.factor = value->getDoubleValue();
323 FG_LOG(FG_INPUT, FG_INFO, " factor is " << a.factor);
328 name += "/tolerance";
329 value = current_properties.getValue(name);
331 a.tolerance = value->getDoubleValue();
332 FG_LOG(FG_INPUT, FG_INFO, " tolerance is " << a.tolerance);
337 name += "/saturation";
338 value = current_properties.getValue(name);
340 js->setSaturation(j, value->getDoubleValue());
341 FG_LOG(FG_INPUT, FG_INFO, " saturation is " << js->getSaturation(j));
345 name += "/min-range";
346 value = current_properties.getValue(name);
348 minRange[j] = value->getDoubleValue();
349 FG_LOG(FG_INPUT, FG_INFO, " min-range is " << minRange[j]);
353 name += "/max-range";
354 value = current_properties.getValue(name);
356 maxRange[j] = value->getDoubleValue();
357 FG_LOG(FG_INPUT, FG_INFO, " max-range is " << maxRange[j]);
362 value = current_properties.getValue(name);
364 center[j] = value->getDoubleValue();
365 FG_LOG(FG_INPUT, FG_INFO, " center is " << center[j]);
370 // Initialize the buttons.
372 for (j = 0; j < nbuttons; j++) {
373 button &b = joysticks[i].buttons[j];
375 string base = "/input/";
378 base += buttonNames[j];
379 FG_LOG(FG_INPUT, FG_INFO, " Button " << j << ':');
384 cout << "Trying name " << name << endl;
385 SGValue * value = current_properties.getValue(name);
387 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
390 const string &control = value->getStringValue();
391 b.value = current_properties.getValue(control, true);
392 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
397 value = current_properties.getValue(name);
399 b.step = value->getDoubleValue();
400 FG_LOG(FG_INPUT, FG_INFO, " step is " << b.step);
405 value = current_properties.getValue(name);
406 string action = "adjust";
408 action = value->getStringValue();
409 if (action == "toggle") {
410 b.action = button::TOGGLE;
411 b.isRepeatable = false;
412 } else if (action == "switch") {
413 b.action = button::SWITCH;
414 b.isRepeatable = false;
415 } else if (action == "adjust") {
416 b.action = button::ADJUST;
417 b.isRepeatable = true;
419 FG_LOG(FG_INPUT, FG_ALERT, " unknown action " << action);
421 b.action = button::ADJUST;
422 b.isRepeatable = true;
424 FG_LOG(FG_INPUT, FG_INFO, " action is " << action);
428 name += "/repeatable";
429 value = current_properties.getValue(name);
431 b.isRepeatable = value->getBoolValue();
432 FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
433 " repeatable" : " not repeatable"));
436 js->setMinRange(minRange);
437 js->setMaxRange(maxRange);
438 js->setCenter(center);
447 FG_LOG(FG_INPUT, FG_INFO, "Done initializing joysticks");
449 FG_LOG(FG_INPUT, FG_ALERT, "No joysticks detected");
451 return seen_joystick;
456 * Update property values based on the joystick state(s).
462 float *axis_values = new float[MAX_AXES];
464 for (int i = 0; i < MAX_JOYSTICKS; i++) {
465 jsJoystick * js = joysticks[i].js;
466 // float *axis_values = new float[joysticks[i].naxes];
467 if (js->notWorking()) {
471 js->read(&buttons, axis_values);
477 for (j = 0; j < joysticks[i].naxes; j++) {
479 axis &a = joysticks[i].axes[j];
481 // If the axis hasn't changed, don't
483 if (fabs(axis_values[j] - a.last_value) <= a.tolerance)
486 a.last_value = axis_values[j];
489 flag = a.value->setDoubleValue((axis_values[j] + a.offset) *
492 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
493 << i << ", axis " << j);
499 for (j = 0; j < joysticks[i].nbuttons; j++) {
501 button &b = joysticks[i].buttons[j];
506 if ((buttons & (1 << j)) > 0) {
508 if (b.lastState == 1 && !b.isRepeatable)
514 if (b.value->getDoubleValue() == 0.0)
515 flag = b.value->setDoubleValue(b.step);
517 flag = b.value->setDoubleValue(0.0);
519 if (b.value->getBoolValue())
520 flag = b.value->setBoolValue(false);
522 flag = b.value->setBoolValue(true);
526 flag = b.value->setDoubleValue(b.step);
529 if (!b.value->setDoubleValue(b.value->getDoubleValue() + b.step))
530 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
531 << i << ", axis " << j);
542 if (b.lastState == 0 && !b.isRepeatable)
550 flag = b.value->setDoubleValue(0.0);
563 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for "
564 << jsNames[i] << ' ' << buttonNames[j]);
574 // end of joystick.cxx