1 // FGJoystickInput.cxx -- handle user input from joystick devices
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "FGJoystickInput.hxx"
26 #include "FGDeviceConfigurationMap.hxx"
27 #include <Main/fg_props.hxx>
28 #include <Scripting/NasalSys.hxx>
30 using simgear::PropertyList;
32 FGJoystickInput::axis::axis ()
33 : last_value(9999999),
42 FGJoystickInput::axis::~axis ()
46 FGJoystickInput::joystick::joystick ()
56 FGJoystickInput::joystick::~joystick ()
58 // delete js? why not?
65 FGJoystickInput::FGJoystickInput()
69 FGJoystickInput::~FGJoystickInput()
74 void FGJoystickInput::init()
77 // TODO: zero the old bindings first.
78 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
79 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
81 FGDeviceConfigurationMap configMap("Input/Joysticks", js_nodes, "js-named");
83 for (int i = 0; i < MAX_JOYSTICKS; i++) {
84 jsJoystick * js = new jsJoystick(i);
87 if (js->notWorking()) {
88 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
92 const char * name = js->getName();
93 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
96 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
99 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
100 SGPropertyNode_ptr named;
102 if ((named = configMap[name])) {
103 string source = named->getStringValue("source", "user defined");
104 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
106 } else if ((named = configMap["default"])) {
107 string source = named->getStringValue("source", "user defined");
108 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
109 << "\"\nUsing default: \"" << source << '"');
112 SG_LOG(SG_INPUT, SG_WARN, "No joystick configuration file with <name>" << name << "</name> entry found!");
115 js_node = js_nodes->getChild("js", i, true);
116 copyProperties(named, js_node);
117 js_node->setStringValue("id", name);
122 void FGJoystickInput::reinit() {
123 SG_LOG(SG_INPUT, SG_DEBUG, "Re-Initializing joystick bindings");
124 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
125 js_nodes->removeChildren("js", false);
126 FGJoystickInput::init();
127 FGJoystickInput::postinit();
130 void FGJoystickInput::postinit()
132 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
133 SGPropertyNode_ptr js_nodes = fgGetNode("/input/joysticks");
135 for (int i = 0; i < MAX_JOYSTICKS; i++) {
136 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
137 jsJoystick *js = bindings[i].js;
138 if (!js_node || js->notWorking())
143 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
144 unsigned int nbuttons = jsCaps.wNumButtons;
145 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
147 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
150 int naxes = js->getNumAxes();
151 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
152 bindings[i].naxes = naxes;
153 bindings[i].nbuttons = nbuttons;
155 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
157 // Set up range arrays
158 float minRange[MAX_JOYSTICK_AXES];
159 float maxRange[MAX_JOYSTICK_AXES];
160 float center[MAX_JOYSTICK_AXES];
162 // Initialize with default values
163 js->getMinRange(minRange);
164 js->getMaxRange(maxRange);
165 js->getCenter(center);
167 // Allocate axes and buttons
168 bindings[i].axes = new axis[naxes];
169 bindings[i].buttons = new FGButton[nbuttons];
172 // Initialize nasal groups.
176 string module = str.str();
177 nasalsys->createModule(module.c_str(), module.c_str(), "", 0);
179 PropertyList nasal = js_node->getChildren("nasal");
181 for (j = 0; j < nasal.size(); j++) {
182 nasal[j]->setStringValue("module", module.c_str());
183 nasalsys->handleCommand(nasal[j]);
187 // Initialize the axes.
189 PropertyList axes = js_node->getChildren("axis");
190 size_t nb_axes = axes.size();
191 for (j = 0; j < nb_axes; j++ ) {
192 const SGPropertyNode * axis_node = axes[j];
193 const SGPropertyNode * num_node = axis_node->getChild("number");
194 int n_axis = axis_node->getIndex();
196 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
198 // Silently ignore platforms that are not specified within the
199 // <number></number> section
204 if (n_axis >= naxes) {
205 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
208 axis &a = bindings[i].axes[n_axis];
210 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
212 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
213 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
214 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
215 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
217 read_bindings(axis_node, a.bindings, KEYMOD_NONE, module );
219 // Initialize the virtual axis buttons.
220 a.low.init(axis_node->getChild("low"), "low", module );
221 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
223 a.high.init(axis_node->getChild("high"), "high", module );
224 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
225 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
230 // Initialize the buttons.
232 PropertyList buttons = js_node->getChildren("button");
234 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
235 const SGPropertyNode * button_node = buttons[j];
236 const SGPropertyNode * num_node = button_node->getChild("number");
237 size_t n_but = button_node->getIndex();
239 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
242 if (n_but >= nbuttons) {
243 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
247 sprintf(buf, "%u", (unsigned)n_but);
248 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
249 bindings[i].buttons[n_but].init(button_node, buf, module );
251 // get interval-sec property
252 FGButton &b = bindings[i].buttons[n_but];
253 if (button_node != 0) {
254 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
259 js->setMinRange(minRange);
260 js->setMaxRange(maxRange);
261 js->setCenter(center);
265 void FGJoystickInput::update( double dt )
267 float axis_values[MAX_JOYSTICK_AXES];
268 int modifiers = fgGetKeyModifiers();
271 for (int i = 0; i < MAX_JOYSTICKS; i++) {
273 jsJoystick * js = bindings[i].js;
274 if (js == 0 || js->notWorking())
277 js->read(&buttons, axis_values);
278 if (js->notWorking()) // If js is disconnected
281 // Fire bindings for the axes.
282 for (int j = 0; j < bindings[i].naxes; j++) {
283 axis &a = bindings[i].axes[j];
285 // Do nothing if the axis position
286 // is unchanged; only a change in
287 // position fires the bindings.
288 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
289 a.last_value = axis_values[j];
290 for (unsigned int k = 0; k < a.bindings[KEYMOD_NONE].size(); k++)
291 a.bindings[KEYMOD_NONE][k]->fire(axis_values[j]);
294 // do we have to emulate axis buttons?
296 if(a.last_dt >= a.interval_sec) {
297 if (a.low.bindings[modifiers].size())
298 bindings[i].axes[j].low.update( modifiers, axis_values[j] < a.low_threshold );
300 if (a.high.bindings[modifiers].size())
301 bindings[i].axes[j].high.update( modifiers, axis_values[j] > a.high_threshold );
303 a.last_dt -= a.interval_sec;
307 // Fire bindings for the buttons.
308 for (int j = 0; j < bindings[i].nbuttons; j++) {
309 FGButton &b = bindings[i].buttons[j];
311 if(b.last_dt >= b.interval_sec) {
312 bindings[i].buttons[j].update( modifiers, (buttons & (1 << j)) > 0 );
313 b.last_dt -= b.interval_sec;