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 * Initialize any joysticks found.
137 bool seen_joystick = false;
139 FG_LOG(FG_INPUT, FG_INFO, "Initializing joysticks");
141 for (int i = 0; i < MAX_JOYSTICKS; i++) {
142 jsJoystick * js = new jsJoystick(i);
143 joysticks[i].js = js;
144 if (js->notWorking()) {
145 FG_LOG(FG_INPUT, FG_INFO, "Joystick " << i << " not found");
150 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
151 int nbuttons = jsCaps.wNumButtons;
153 int nbuttons = MAX_BUTTONS;
156 int naxes = js->getNumAxes();
157 joysticks[i].naxes = naxes;
158 joysticks[i].nbuttons = nbuttons;
160 FG_LOG(FG_INPUT, FG_INFO, "Initializing joystick " << i);
161 seen_joystick = true;
163 // Set up range arrays
164 float *minRange = new float[naxes];
165 float *maxRange = new float[naxes];
166 float *center = new float[naxes];
168 // Initialize with default values
169 js->getMinRange(minRange);
170 js->getMaxRange(maxRange);
171 js->getCenter(center);
173 // Allocate axes and buttons
174 joysticks[i].axes = new axis[naxes];
175 joysticks[i].buttons = new button[nbuttons];
179 // Initialize the axes.
182 for (j = 0; j < naxes; j++) {
183 axis &a = joysticks[i].axes[j];
185 string base = "/input/";
188 base += axisNames[j];
189 FG_LOG(FG_INPUT, FG_INFO, " Axis " << j << ':');
194 SGValue * value = current_properties.getValue(name);
196 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
199 const string &control = value->getStringValue();
200 a.value = current_properties.getValue(control, true);
201 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
205 name += "/dead-band";
206 value = current_properties.getValue(name);
208 js->setDeadBand(j, value->getDoubleValue());
209 FG_LOG(FG_INPUT, FG_INFO, " dead-band is " << js->getDeadBand(j));
214 value = current_properties.getValue(name);
216 a.offset = value->getDoubleValue();
217 FG_LOG(FG_INPUT, FG_INFO, " offset is " << a.offset);
223 value = current_properties.getValue(name);
225 a.factor = value->getDoubleValue();
226 FG_LOG(FG_INPUT, FG_INFO, " factor is " << a.factor);
231 name += "/tolerance";
232 value = current_properties.getValue(name);
234 a.tolerance = value->getDoubleValue();
235 FG_LOG(FG_INPUT, FG_INFO, " tolerance is " << a.tolerance);
240 name += "/saturation";
241 value = current_properties.getValue(name);
243 js->setSaturation(j, value->getDoubleValue());
244 FG_LOG(FG_INPUT, FG_INFO, " saturation is " << js->getSaturation(j));
248 name += "/min-range";
249 value = current_properties.getValue(name);
251 minRange[j] = value->getDoubleValue();
252 FG_LOG(FG_INPUT, FG_INFO, " min-range is " << minRange[j]);
256 name += "/max-range";
257 value = current_properties.getValue(name);
259 maxRange[j] = value->getDoubleValue();
260 FG_LOG(FG_INPUT, FG_INFO, " max-range is " << maxRange[j]);
265 value = current_properties.getValue(name);
267 center[j] = value->getDoubleValue();
268 FG_LOG(FG_INPUT, FG_INFO, " center is " << center[j]);
273 // Initialize the buttons.
275 for (j = 0; j < nbuttons; j++) {
276 button &b = joysticks[i].buttons[j];
278 string base = "/input/";
281 base += buttonNames[j];
282 FG_LOG(FG_INPUT, FG_INFO, " Button " << j << ':');
287 cout << "Trying name " << name << endl;
288 SGValue * value = current_properties.getValue(name);
290 FG_LOG(FG_INPUT, FG_INFO, " no control defined");
293 const string &control = value->getStringValue();
294 b.value = current_properties.getValue(control, true);
295 FG_LOG(FG_INPUT, FG_INFO, " using control " << control);
300 value = current_properties.getValue(name);
302 b.step = value->getDoubleValue();
303 FG_LOG(FG_INPUT, FG_INFO, " step is " << b.step);
308 value = current_properties.getValue(name);
309 string action = "adjust";
311 action = value->getStringValue();
312 if (action == "toggle") {
313 b.action = button::TOGGLE;
314 b.isRepeatable = false;
315 } else if (action == "switch") {
316 b.action = button::SWITCH;
317 b.isRepeatable = false;
318 } else if (action == "adjust") {
319 b.action = button::ADJUST;
320 b.isRepeatable = true;
322 FG_LOG(FG_INPUT, FG_ALERT, " unknown action " << action);
324 b.action = button::ADJUST;
325 b.isRepeatable = true;
327 FG_LOG(FG_INPUT, FG_INFO, " action is " << action);
331 name += "/repeatable";
332 value = current_properties.getValue(name);
334 b.isRepeatable = value->getBoolValue();
335 FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
336 " repeatable" : " not repeatable"));
339 js->setMinRange(minRange);
340 js->setMaxRange(maxRange);
341 js->setCenter(center);
350 FG_LOG(FG_INPUT, FG_INFO, "Done initializing joysticks");
352 FG_LOG(FG_INPUT, FG_ALERT, "No joysticks detected");
354 return seen_joystick;
359 * Update property values based on the joystick state(s).
365 float *axis_values = new float[MAX_AXES];
367 for (int i = 0; i < MAX_JOYSTICKS; i++) {
368 jsJoystick * js = joysticks[i].js;
369 // float *axis_values = new float[joysticks[i].naxes];
370 if (js->notWorking()) {
374 js->read(&buttons, axis_values);
380 for (j = 0; j < joysticks[i].naxes; j++) {
382 axis &a = joysticks[i].axes[j];
384 // If the axis hasn't changed, don't
386 if (fabs(axis_values[j] - a.last_value) <= a.tolerance)
389 a.last_value = axis_values[j];
392 flag = a.value->setDoubleValue((axis_values[j] + a.offset) *
395 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
396 << i << ", axis " << j);
402 for (j = 0; j < joysticks[i].nbuttons; j++) {
404 button &b = joysticks[i].buttons[j];
409 if ((buttons & (1 << j)) > 0) {
411 if (b.lastState == 1 && !b.isRepeatable)
417 if (b.value->getDoubleValue() == 0.0)
418 flag = b.value->setDoubleValue(b.step);
420 flag = b.value->setDoubleValue(0.0);
422 if (b.value->getBoolValue())
423 flag = b.value->setBoolValue(false);
425 flag = b.value->setBoolValue(true);
429 flag = b.value->setDoubleValue(b.step);
432 if (!b.value->setDoubleValue(b.value->getDoubleValue() + b.step))
433 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for joystick "
434 << i << ", axis " << j);
445 if (b.lastState == 0 && !b.isRepeatable)
455 flag = b.value->setDoubleValue(0.0);
465 FG_LOG(FG_INPUT, FG_ALERT, "Failed to set value for "
466 << jsNames[i] << ' ' << buttonNames[j]);
476 // end of joystick.cxx