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.
35 #include <simgear/misc/props.hxx>
36 #include <simgear/debug/logstream.hxx>
38 #include "joystick.hxx"
43 static const int MAX_JOYSTICKS = 2;
45 static const int MAX_JOYSTICKS = 10;
47 static const int MAX_AXES = _JS_MAX_AXES;
48 static const int MAX_BUTTONS = 10;
52 * Property names for joysticks and axes.
54 static const char * jsNames[] = {
55 "js0", "js1", "js2", "js3", "js4",
56 "js5", "js6", "js7", "js8", "js9"
58 static const char * axisNames[] = {
59 "axis0", "axis1", "axis2", "axis3", "axis4",
60 "axis5", "axis6", "axis7", "axis8", "axis9"
62 static const char * buttonNames[] = {
63 "button0", "button1", "button2", "button3", "button4",
64 "button5", "button6", "button7", "button8", "button9"
69 * Settings for a single axis.
72 axis () : value(0), offset(0.0), factor(1.0),
73 last_value(9999999), tolerance(0.002) {}
83 * Settings for a single button.
91 button () : value(0), step(0.0), action(ADJUST), isRepeatable(true),
102 * Settings for a single joystick.
105 virtual ~joystick () {
119 * Array of joystick settings.
121 static joystick joysticks[MAX_JOYSTICKS];
125 * Set up default values if properties are not specified.
130 SGPropertyList &props = current_properties;
132 // Default axis 0 to aileron
133 if (!props.getValue("/input/js0/axis0/control")) {
134 props.setStringValue("/input/js0/axis0/control", "/controls/aileron");
135 props.setFloatValue("/input/js0/axis0/dead-band", 0.1);
138 // Default axis 1 to elevator
139 if (!props.getValue("/input/js0/axis1/control")) {
140 props.setStringValue("/input/js0/axis1/control", "/controls/elevator");
141 props.setFloatValue("/input/js0/axis1/dead-band", 0.1);
142 props.setFloatValue("/input/js0/axis1/factor", -1.0);
145 // Default axis 2 to rudder
146 if (!props.getValue("/input/js0/axis2/control")) {
147 props.setStringValue("/input/js0/axis2/control", "/controls/rudder");
148 props.setFloatValue("/input/js0/axis2/dead-band", 0.1);
151 // Default axis 3 to throttle
152 // We need to fiddle with the offset
153 // and factor to make it work
154 if (!props.getValue("/input/js0/axis3/control")) {
155 props.setStringValue("/input/js0/axis3/control", "/controls/throttle");
156 props.setFloatValue("/input/js0/axis3/dead-band", 0.0);
157 props.setFloatValue("/input/js0/axis3/offset", -1.0);
158 props.setFloatValue("/input/js0/axis3/factor", -0.5);
161 // Default button 0 to all brakes
162 if (!props.getValue("/input/js0/button0/control")) {
163 props.setStringValue("/input/js0/button0/action", "switch");
164 props.setStringValue("/input/js0/button0/control", "/controls/brake");
165 props.setFloatValue("/input/js0/button0/step", 1.0);
166 props.setFloatValue("/input/js0/button0/repeatable", false);
169 // Default button 1 to left brake.
170 if (!props.getValue("/input/js0/button1/control")) {
171 props.setStringValue("/input/js0/button1/action", "switch");
172 props.setStringValue("/input/js0/button1/control", "/controls/left-brake");
173 props.setFloatValue("/input/js0/button1/step", 1.0);
174 props.setFloatValue("/input/js0/button1/repeatable", false);
177 // Default button 2 to right brake.
178 if (!props.getValue("/input/js0/button2/control")) {
179 props.setStringValue("/input/js0/button2/action", "switch");
180 props.setStringValue("/input/js0/button2/control",
181 "/controls/right-brake");
182 props.setFloatValue("/input/js0/button2/step", 1.0);
183 props.setFloatValue("/input/js0/button2/repeatable", false);
186 // Default buttons 3 and 4 to elevator trim
187 if (!props.getValue("/input/js0/button3/control")) {
188 props.setStringValue("/input/js0/button3/action", "adjust");
189 props.setStringValue("/input/js0/button3/control",
190 "/controls/elevator-trim");
191 props.setFloatValue("/input/js0/button3/step", 0.001);
192 props.setBoolValue("/input/js0/button3/repeatable", true);
194 if (!props.getValue("/input/js0/button4/control")) {
195 props.setStringValue("/input/js0/button4/action", "adjust");
196 props.setStringValue("/input/js0/button4/control",
197 "/controls/elevator-trim");
198 props.setFloatValue("/input/js0/button4/step", -0.001);
199 props.setBoolValue("/input/js0/button4/repeatable", true);
202 // Default buttons 5 and 6 to flaps
203 if (!props.getValue("/input/js0/button5/control")) {
204 props.setStringValue("/input/js0/button5/action", "adjust");
205 props.setStringValue("/input/js0/button5/control", "/controls/flaps");
206 props.setFloatValue("/input/js0/button5/step", -0.34);
207 props.setBoolValue("/input/js0/button5/repeatable", false);
209 if (!props.getValue("/input/js0/button6/control")) {
210 props.setStringValue("/input/js0/button6/action", "adjust");
211 props.setStringValue("/input/js0/button6/control", "/controls/flaps");
212 props.setFloatValue("/input/js0/button6/step", 0.34);
213 props.setBoolValue("/input/js0/button6/repeatable", false);
219 * Initialize any joysticks found.
224 bool seen_joystick = false;
226 FG_LOG(FG_INPUT, FG_INFO, "Initializing joysticks");
230 for (int i = 0; i < MAX_JOYSTICKS; i++) {
231 jsJoystick * js = new jsJoystick(i);
232 joysticks[i].js = js;
233 if (js->notWorking()) {
234 FG_LOG(FG_INPUT, FG_INFO, "Joystick " << i << " not found");
239 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
240 int nbuttons = jsCaps.wNumButtons;
242 int nbuttons = MAX_BUTTONS;
245 int naxes = js->getNumAxes();
246 joysticks[i].naxes = naxes;
247 joysticks[i].nbuttons = nbuttons;
249 FG_LOG(FG_INPUT, FG_INFO, "Initializing joystick " << i);
250 seen_joystick = true;
252 // Set up range arrays
253 float minRange[naxes];
254 float maxRange[naxes];
257 // Initialize with default values
258 js->getMinRange(minRange);
259 js->getMaxRange(maxRange);
260 js->getCenter(center);
262 // Allocate axes and buttons
263 joysticks[i].axes = new axis[naxes];
264 joysticks[i].buttons = new button[nbuttons];
268 // Initialize the axes.
270 for (int j = 0; j < naxes; j++) {
271 axis &a = joysticks[i].axes[j];
273 string base = "/input/";
276 base += axisNames[j];
277 FG_LOG(FG_INPUT, FG_INFO, " Axis " << j << ':');
282 SGValue * value = current_properties.getValue(name);
284 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
287 const string &control = value->getStringValue();
288 a.value = current_properties.getValue(control, true);
289 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
293 name += "/dead-band";
294 value = current_properties.getValue(name);
296 js->setDeadBand(j, value->getFloatValue());
297 FG_LOG(FG_INPUT, FG_INFO, " dead-band is " << js->getDeadBand(j));
302 value = current_properties.getValue(name);
304 a.offset = value->getFloatValue();
305 FG_LOG(FG_INPUT, FG_INFO, " offset is " << a.offset);
311 value = current_properties.getValue(name);
313 a.factor = value->getFloatValue();
314 FG_LOG(FG_INPUT, FG_INFO, " factor is " << a.factor);
319 name += "/tolerance";
320 value = current_properties.getValue(name);
322 a.tolerance = value->getFloatValue();
323 FG_LOG(FG_INPUT, FG_INFO, " tolerance is " << a.tolerance);
328 name += "/saturation";
329 value = current_properties.getValue(name);
331 js->setSaturation(j, value->getFloatValue());
332 FG_LOG(FG_INPUT, FG_INFO, " saturation is " << js->getSaturation(j));
336 name += "/min-range";
337 value = current_properties.getValue(name);
339 minRange[j] = value->getFloatValue();
340 FG_LOG(FG_INPUT, FG_INFO, " min-range is " << minRange[j]);
344 name += "/max-range";
345 value = current_properties.getValue(name);
347 maxRange[j] = value->getFloatValue();
348 FG_LOG(FG_INPUT, FG_INFO, " max-range is " << maxRange[j]);
353 value = current_properties.getValue(name);
355 center[j] = value->getFloatValue();
356 FG_LOG(FG_INPUT, FG_INFO, " center is " << center[j]);
361 // Initialize the buttons.
363 for (int j = 0; j < nbuttons; j++) {
364 button &b = joysticks[i].buttons[j];
366 string base = "/input/";
369 base += buttonNames[j];
370 FG_LOG(FG_INPUT, FG_INFO, " Button " << j << ':');
375 cout << "Trying name " << name << endl;
376 SGValue * value = current_properties.getValue(name);
378 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
381 const string &control = value->getStringValue();
382 b.value = current_properties.getValue(control, true);
383 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
388 value = current_properties.getValue(name);
390 b.step = value->getFloatValue();
391 FG_LOG(FG_INPUT, FG_INFO, " step is " << b.step);
396 value = current_properties.getValue(name);
397 string action = "adjust";
399 action = value->getStringValue();
400 if (action == "toggle") {
401 b.action = button::TOGGLE;
402 b.isRepeatable = false;
403 } else if (action == "switch") {
404 b.action = button::SWITCH;
405 b.isRepeatable = false;
406 } else if (action == "adjust") {
407 b.action = button::ADJUST;
408 b.isRepeatable = true;
410 FG_LOG(FG_INPUT, FG_ALERT, " unknown action " << action);
412 b.action = button::ADJUST;
413 b.isRepeatable = true;
415 FG_LOG(FG_INPUT, FG_INFO, " action is " << action);
419 name += "/repeatable";
420 value = current_properties.getValue(name);
422 b.isRepeatable = value->getBoolValue();
423 FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
424 " repeatable" : " not repeatable"));
427 js->setMinRange(minRange);
428 js->setMaxRange(maxRange);
429 js->setCenter(center);
433 FG_LOG(FG_INPUT, FG_INFO, "Done initializing joysticks");
435 FG_LOG(FG_INPUT, FG_ALERT, "No joysticks detected");
437 return seen_joystick;
442 * Update property values based on the joystick state(s).
449 for (int i = 0; i < MAX_JOYSTICKS; i++) {
450 jsJoystick * js = joysticks[i].js;
451 float axis_values[joysticks[i].naxes];
452 if (js->notWorking()) {
456 js->read(&buttons, axis_values);
461 for (int j = 0; j < joysticks[i].naxes; j++) {
463 axis &a = joysticks[i].axes[j];
465 // If the axis hasn't changed, don't
467 if (fabs(axis_values[j] - a.last_value) <= a.tolerance)
470 a.last_value = axis_values[j];
473 flag = a.value->setDoubleValue((axis_values[j] + a.offset) *
476 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
477 << i << ", axis " << j);
483 for (int j = 0; j < joysticks[i].nbuttons; j++) {
485 button &b = joysticks[i].buttons[j];
490 if ((buttons & (1 << j)) > 0) {
492 if (b.lastState == 1 && !b.isRepeatable)
498 if (b.value->getDoubleValue() == 0.0)
499 flag = b.value->setDoubleValue(b.step);
501 flag = b.value->setDoubleValue(0.0);
503 if (b.value->getBoolValue())
504 flag = b.value->setBoolValue(false);
506 flag = b.value->setBoolValue(true);
510 flag = b.value->setDoubleValue(b.step);
513 if (!b.value->setDoubleValue(b.value->getDoubleValue() + b.step))
514 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
515 << i << ", axis " << j);
526 if (b.lastState == 0 && !b.isRepeatable)
534 flag = b.value->setDoubleValue(0.0);
547 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for "
548 << jsNames[i] << ' ' << buttonNames[j]);
555 // end of joystick.cxx