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? no, since js == this - and we're in the destructor already.
70 FGJoystickInput::FGJoystickInput()
74 FGJoystickInput::~FGJoystickInput()
79 void FGJoystickInput::_remove()
81 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
82 js_nodes->removeChildren("js", false);
83 for (int i = 0; i < MAX_JOYSTICKS; i++)
86 delete bindings[i].js;
87 bindings[i].js = NULL;
91 void FGJoystickInput::init()
94 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
95 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
97 FGDeviceConfigurationMap configMap("Input/Joysticks", js_nodes, "js-named");
99 for (int i = 0; i < MAX_JOYSTICKS; i++) {
100 jsJoystick * js = new jsJoystick(i);
103 if (js->notWorking()) {
104 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
108 const char * name = js->getName();
109 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
112 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
115 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
116 SGPropertyNode_ptr named;
118 if ((named = configMap[name])) {
119 string source = named->getStringValue("source", "user defined");
120 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
122 } else if ((named = configMap["default"])) {
123 string source = named->getStringValue("source", "user defined");
124 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
125 << "\"\nUsing default: \"" << source << '"');
128 SG_LOG(SG_INPUT, SG_WARN, "No joystick configuration file with <name>" << name << "</name> entry found!");
131 js_node = js_nodes->getChild("js", i, true);
132 copyProperties(named, js_node);
133 js_node->setStringValue("id", name);
138 void FGJoystickInput::reinit() {
139 SG_LOG(SG_INPUT, SG_DEBUG, "Re-Initializing joystick bindings");
141 FGJoystickInput::init();
142 FGJoystickInput::postinit();
145 void FGJoystickInput::postinit()
147 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
148 SGPropertyNode_ptr js_nodes = fgGetNode("/input/joysticks");
150 for (int i = 0; i < MAX_JOYSTICKS; i++) {
151 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
152 jsJoystick *js = bindings[i].js;
153 if (!js_node || js->notWorking())
158 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
159 unsigned int nbuttons = jsCaps.wNumButtons;
160 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
162 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
165 int naxes = js->getNumAxes();
166 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
167 bindings[i].naxes = naxes;
168 bindings[i].nbuttons = nbuttons;
170 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
172 // Set up range arrays
173 float minRange[MAX_JOYSTICK_AXES];
174 float maxRange[MAX_JOYSTICK_AXES];
175 float center[MAX_JOYSTICK_AXES];
177 // Initialize with default values
178 js->getMinRange(minRange);
179 js->getMaxRange(maxRange);
180 js->getCenter(center);
182 // Allocate axes and buttons
183 bindings[i].axes = new axis[naxes];
184 bindings[i].buttons = new FGButton[nbuttons];
187 // Initialize nasal groups.
191 string module = str.str();
192 nasalsys->createModule(module.c_str(), module.c_str(), "", 0);
194 PropertyList nasal = js_node->getChildren("nasal");
196 for (j = 0; j < nasal.size(); j++) {
197 nasal[j]->setStringValue("module", module.c_str());
198 nasalsys->handleCommand(nasal[j]);
202 // Initialize the axes.
204 PropertyList axes = js_node->getChildren("axis");
205 size_t nb_axes = axes.size();
206 for (j = 0; j < nb_axes; j++ ) {
207 const SGPropertyNode * axis_node = axes[j];
208 const SGPropertyNode * num_node = axis_node->getChild("number");
209 int n_axis = axis_node->getIndex();
211 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
213 // Silently ignore platforms that are not specified within the
214 // <number></number> section
219 if (n_axis >= naxes) {
220 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
223 axis &a = bindings[i].axes[n_axis];
225 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
227 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
228 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
229 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
230 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
232 read_bindings(axis_node, a.bindings, KEYMOD_NONE, module );
234 // Initialize the virtual axis buttons.
235 a.low.init(axis_node->getChild("low"), "low", module );
236 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
238 a.high.init(axis_node->getChild("high"), "high", module );
239 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
240 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
245 // Initialize the buttons.
247 PropertyList buttons = js_node->getChildren("button");
249 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
250 const SGPropertyNode * button_node = buttons[j];
251 const SGPropertyNode * num_node = button_node->getChild("number");
252 size_t n_but = button_node->getIndex();
254 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
257 if (n_but >= nbuttons) {
258 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
262 sprintf(buf, "%u", (unsigned)n_but);
263 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
264 bindings[i].buttons[n_but].init(button_node, buf, module );
266 // get interval-sec property
267 FGButton &b = bindings[i].buttons[n_but];
268 if (button_node != 0) {
269 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
274 js->setMinRange(minRange);
275 js->setMaxRange(maxRange);
276 js->setCenter(center);
280 void FGJoystickInput::update( double dt )
282 float axis_values[MAX_JOYSTICK_AXES];
283 int modifiers = fgGetKeyModifiers();
286 for (int i = 0; i < MAX_JOYSTICKS; i++) {
288 jsJoystick * js = bindings[i].js;
289 if (js == 0 || js->notWorking())
292 js->read(&buttons, axis_values);
293 if (js->notWorking()) // If js is disconnected
296 // Fire bindings for the axes.
297 for (int j = 0; j < bindings[i].naxes; j++) {
298 axis &a = bindings[i].axes[j];
300 // Do nothing if the axis position
301 // is unchanged; only a change in
302 // position fires the bindings.
303 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
304 a.last_value = axis_values[j];
305 for (unsigned int k = 0; k < a.bindings[KEYMOD_NONE].size(); k++)
306 a.bindings[KEYMOD_NONE][k]->fire(axis_values[j]);
309 // do we have to emulate axis buttons?
311 if(a.last_dt >= a.interval_sec) {
312 if (a.low.bindings[modifiers].size())
313 bindings[i].axes[j].low.update( modifiers, axis_values[j] < a.low_threshold );
315 if (a.high.bindings[modifiers].size())
316 bindings[i].axes[j].high.update( modifiers, axis_values[j] > a.high_threshold );
318 a.last_dt -= a.interval_sec;
322 // Fire bindings for the buttons.
323 for (int j = 0; j < bindings[i].nbuttons; j++) {
324 FGButton &b = bindings[i].buttons[j];
326 if(b.last_dt >= b.interval_sec) {
327 bindings[i].buttons[j].update( modifiers, (buttons & (1 << j)) > 0 );
328 b.last_dt -= b.interval_sec;