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.setDoubleValue("/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.setDoubleValue("/input/js0/axis1/dead-band", 0.1);
142 props.setDoubleValue("/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.setDoubleValue("/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.setDoubleValue("/input/js0/axis3/dead-band", 0.0);
157 props.setDoubleValue("/input/js0/axis3/offset", -1.0);
158 props.setDoubleValue("/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/brakes/all");
165 props.setDoubleValue("/input/js0/button0/step", 1.0);
166 props.setDoubleValue("/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",
173 "/controls/brakes/left");
174 props.setDoubleValue("/input/js0/button1/step", 1.0);
175 props.setDoubleValue("/input/js0/button1/repeatable", false);
178 // Default button 2 to right brake.
179 if (!props.getValue("/input/js0/button2/control")) {
180 props.setStringValue("/input/js0/button2/action", "switch");
181 props.setStringValue("/input/js0/button2/control",
182 "/controls/brakes/right");
183 props.setDoubleValue("/input/js0/button2/step", 1.0);
184 props.setDoubleValue("/input/js0/button2/repeatable", false);
187 // Default buttons 3 and 4 to elevator trim
188 if (!props.getValue("/input/js0/button3/control")) {
189 props.setStringValue("/input/js0/button3/action", "adjust");
190 props.setStringValue("/input/js0/button3/control",
191 "/controls/elevator-trim");
192 props.setDoubleValue("/input/js0/button3/step", 0.001);
193 props.setBoolValue("/input/js0/button3/repeatable", true);
195 if (!props.getValue("/input/js0/button4/control")) {
196 props.setStringValue("/input/js0/button4/action", "adjust");
197 props.setStringValue("/input/js0/button4/control",
198 "/controls/elevator-trim");
199 props.setDoubleValue("/input/js0/button4/step", -0.001);
200 props.setBoolValue("/input/js0/button4/repeatable", true);
203 // Default buttons 5 and 6 to flaps
204 if (!props.getValue("/input/js0/button5/control")) {
205 props.setStringValue("/input/js0/button5/action", "adjust");
206 props.setStringValue("/input/js0/button5/control", "/controls/flaps");
207 props.setDoubleValue("/input/js0/button5/step", -0.34);
208 props.setBoolValue("/input/js0/button5/repeatable", false);
210 if (!props.getValue("/input/js0/button6/control")) {
211 props.setStringValue("/input/js0/button6/action", "adjust");
212 props.setStringValue("/input/js0/button6/control", "/controls/flaps");
213 props.setDoubleValue("/input/js0/button6/step", 0.34);
214 props.setBoolValue("/input/js0/button6/repeatable", false);
220 * Initialize any joysticks found.
225 bool seen_joystick = false;
227 FG_LOG(FG_INPUT, FG_INFO, "Initializing joysticks");
231 for (int i = 0; i < MAX_JOYSTICKS; i++) {
232 jsJoystick * js = new jsJoystick(i);
233 joysticks[i].js = js;
234 if (js->notWorking()) {
235 FG_LOG(FG_INPUT, FG_INFO, "Joystick " << i << " not found");
240 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
241 int nbuttons = jsCaps.wNumButtons;
243 int nbuttons = MAX_BUTTONS;
246 int naxes = js->getNumAxes();
247 joysticks[i].naxes = naxes;
248 joysticks[i].nbuttons = nbuttons;
250 FG_LOG(FG_INPUT, FG_INFO, "Initializing joystick " << i);
251 seen_joystick = true;
253 // Set up range arrays
254 float minRange[naxes];
255 float maxRange[naxes];
258 // Initialize with default values
259 js->getMinRange(minRange);
260 js->getMaxRange(maxRange);
261 js->getCenter(center);
263 // Allocate axes and buttons
264 joysticks[i].axes = new axis[naxes];
265 joysticks[i].buttons = new button[nbuttons];
269 // Initialize the axes.
271 for (int j = 0; j < naxes; j++) {
272 axis &a = joysticks[i].axes[j];
274 string base = "/input/";
277 base += axisNames[j];
278 FG_LOG(FG_INPUT, FG_INFO, " Axis " << j << ':');
283 SGValue * value = current_properties.getValue(name);
285 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
288 const string &control = value->getStringValue();
289 a.value = current_properties.getValue(control, true);
290 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
294 name += "/dead-band";
295 value = current_properties.getValue(name);
297 js->setDeadBand(j, value->getDoubleValue());
298 FG_LOG(FG_INPUT, FG_INFO, " dead-band is " << js->getDeadBand(j));
303 value = current_properties.getValue(name);
305 a.offset = value->getDoubleValue();
306 FG_LOG(FG_INPUT, FG_INFO, " offset is " << a.offset);
312 value = current_properties.getValue(name);
314 a.factor = value->getDoubleValue();
315 FG_LOG(FG_INPUT, FG_INFO, " factor is " << a.factor);
320 name += "/tolerance";
321 value = current_properties.getValue(name);
323 a.tolerance = value->getDoubleValue();
324 FG_LOG(FG_INPUT, FG_INFO, " tolerance is " << a.tolerance);
329 name += "/saturation";
330 value = current_properties.getValue(name);
332 js->setSaturation(j, value->getDoubleValue());
333 FG_LOG(FG_INPUT, FG_INFO, " saturation is " << js->getSaturation(j));
337 name += "/min-range";
338 value = current_properties.getValue(name);
340 minRange[j] = value->getDoubleValue();
341 FG_LOG(FG_INPUT, FG_INFO, " min-range is " << minRange[j]);
345 name += "/max-range";
346 value = current_properties.getValue(name);
348 maxRange[j] = value->getDoubleValue();
349 FG_LOG(FG_INPUT, FG_INFO, " max-range is " << maxRange[j]);
354 value = current_properties.getValue(name);
356 center[j] = value->getDoubleValue();
357 FG_LOG(FG_INPUT, FG_INFO, " center is " << center[j]);
362 // Initialize the buttons.
364 for (int j = 0; j < nbuttons; j++) {
365 button &b = joysticks[i].buttons[j];
367 string base = "/input/";
370 base += buttonNames[j];
371 FG_LOG(FG_INPUT, FG_INFO, " Button " << j << ':');
376 cout << "Trying name " << name << endl;
377 SGValue * value = current_properties.getValue(name);
379 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
382 const string &control = value->getStringValue();
383 b.value = current_properties.getValue(control, true);
384 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
389 value = current_properties.getValue(name);
391 b.step = value->getDoubleValue();
392 FG_LOG(FG_INPUT, FG_INFO, " step is " << b.step);
397 value = current_properties.getValue(name);
398 string action = "adjust";
400 action = value->getStringValue();
401 if (action == "toggle") {
402 b.action = button::TOGGLE;
403 b.isRepeatable = false;
404 } else if (action == "switch") {
405 b.action = button::SWITCH;
406 b.isRepeatable = false;
407 } else if (action == "adjust") {
408 b.action = button::ADJUST;
409 b.isRepeatable = true;
411 FG_LOG(FG_INPUT, FG_ALERT, " unknown action " << action);
413 b.action = button::ADJUST;
414 b.isRepeatable = true;
416 FG_LOG(FG_INPUT, FG_INFO, " action is " << action);
420 name += "/repeatable";
421 value = current_properties.getValue(name);
423 b.isRepeatable = value->getBoolValue();
424 FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
425 " repeatable" : " not repeatable"));
428 js->setMinRange(minRange);
429 js->setMaxRange(maxRange);
430 js->setCenter(center);
434 FG_LOG(FG_INPUT, FG_INFO, "Done initializing joysticks");
436 FG_LOG(FG_INPUT, FG_ALERT, "No joysticks detected");
438 return seen_joystick;
443 * Update property values based on the joystick state(s).
450 for (int i = 0; i < MAX_JOYSTICKS; i++) {
451 jsJoystick * js = joysticks[i].js;
452 float axis_values[joysticks[i].naxes];
453 if (js->notWorking()) {
457 js->read(&buttons, axis_values);
462 for (int j = 0; j < joysticks[i].naxes; j++) {
464 axis &a = joysticks[i].axes[j];
466 // If the axis hasn't changed, don't
468 if (fabs(axis_values[j] - a.last_value) <= a.tolerance)
471 a.last_value = axis_values[j];
474 flag = a.value->setDoubleValue((axis_values[j] + a.offset) *
477 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
478 << i << ", axis " << j);
484 for (int j = 0; j < joysticks[i].nbuttons; j++) {
486 button &b = joysticks[i].buttons[j];
491 if ((buttons & (1 << j)) > 0) {
493 if (b.lastState == 1 && !b.isRepeatable)
499 if (b.value->getDoubleValue() == 0.0)
500 flag = b.value->setDoubleValue(b.step);
502 flag = b.value->setDoubleValue(0.0);
504 if (b.value->getBoolValue())
505 flag = b.value->setBoolValue(false);
507 flag = b.value->setBoolValue(true);
511 flag = b.value->setDoubleValue(b.step);
514 if (!b.value->setDoubleValue(b.value->getDoubleValue() + b.step))
515 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
516 << i << ", axis " << j);
527 if (b.lastState == 0 && !b.isRepeatable)
535 flag = b.value->setDoubleValue(0.0);
548 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for "
549 << jsNames[i] << ' ' << buttonNames[j]);
556 // end of joystick.cxx