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 FGJoystickInput::axis::axis ()
31 : last_value(9999999),
40 FGJoystickInput::axis::~axis ()
44 FGJoystickInput::joystick::joystick ()
54 FGJoystickInput::joystick::~joystick ()
56 // delete js? why not?
63 FGJoystickInput::FGJoystickInput()
67 FGJoystickInput::~FGJoystickInput()
72 void FGJoystickInput::init()
75 // TODO: zero the old bindings first.
76 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
77 SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
79 FGDeviceConfigurationMap configMap("Input/Joysticks", js_nodes, "js-named");
81 for (int i = 0; i < MAX_JOYSTICKS; i++) {
82 jsJoystick * js = new jsJoystick(i);
85 if (js->notWorking()) {
86 SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
90 const char * name = js->getName();
91 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
94 SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
97 SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
98 SGPropertyNode_ptr named;
100 if ((named = configMap[name])) {
101 string source = named->getStringValue("source", "user defined");
102 SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
104 } else if ((named = configMap["default"])) {
105 string source = named->getStringValue("source", "user defined");
106 SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
107 << "\"\nUsing default: \"" << source << '"');
110 SG_LOG(SG_INPUT, SG_WARN, "No joystick configuration file with <name>" << name << "</name> entry found!");
113 js_node = js_nodes->getChild("js", i, true);
114 copyProperties(named, js_node);
115 js_node->setStringValue("id", name);
120 void FGJoystickInput::postinit()
122 FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
123 SGPropertyNode_ptr js_nodes = fgGetNode("/input/joysticks");
125 for (int i = 0; i < MAX_JOYSTICKS; i++) {
126 SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
127 jsJoystick *js = bindings[i].js;
128 if (!js_node || js->notWorking())
133 joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
134 unsigned int nbuttons = jsCaps.wNumButtons;
135 if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
137 unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
140 int naxes = js->getNumAxes();
141 if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
142 bindings[i].naxes = naxes;
143 bindings[i].nbuttons = nbuttons;
145 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
147 // Set up range arrays
148 float minRange[MAX_JOYSTICK_AXES];
149 float maxRange[MAX_JOYSTICK_AXES];
150 float center[MAX_JOYSTICK_AXES];
152 // Initialize with default values
153 js->getMinRange(minRange);
154 js->getMaxRange(maxRange);
155 js->getCenter(center);
157 // Allocate axes and buttons
158 bindings[i].axes = new axis[naxes];
159 bindings[i].buttons = new FGButton[nbuttons];
162 // Initialize nasal groups.
166 string module = str.str();
167 nasalsys->createModule(module.c_str(), module.c_str(), "", 0);
169 vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
171 for (j = 0; j < nasal.size(); j++) {
172 nasal[j]->setStringValue("module", module.c_str());
173 nasalsys->handleCommand(nasal[j]);
177 // Initialize the axes.
179 vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
180 size_t nb_axes = axes.size();
181 for (j = 0; j < nb_axes; j++ ) {
182 const SGPropertyNode * axis_node = axes[j];
183 const SGPropertyNode * num_node = axis_node->getChild("number");
184 int n_axis = axis_node->getIndex();
186 n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
188 // Silently ignore platforms that are not specified within the
189 // <number></number> section
194 if (n_axis >= naxes) {
195 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
198 axis &a = bindings[i].axes[n_axis];
200 js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
202 a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
203 minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
204 maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
205 center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
207 read_bindings(axis_node, a.bindings, KEYMOD_NONE, module );
209 // Initialize the virtual axis buttons.
210 a.low.init(axis_node->getChild("low"), "low", module );
211 a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
213 a.high.init(axis_node->getChild("high"), "high", module );
214 a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
215 a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
220 // Initialize the buttons.
222 vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
224 for (j = 0; j < buttons.size() && j < nbuttons; j++) {
225 const SGPropertyNode * button_node = buttons[j];
226 const SGPropertyNode * num_node = button_node->getChild("number");
227 size_t n_but = button_node->getIndex();
229 n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
232 if (n_but >= nbuttons) {
233 SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
237 sprintf(buf, "%u", (unsigned)n_but);
238 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
239 bindings[i].buttons[n_but].init(button_node, buf, module );
241 // get interval-sec property
242 FGButton &b = bindings[i].buttons[n_but];
243 if (button_node != 0) {
244 b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
249 js->setMinRange(minRange);
250 js->setMaxRange(maxRange);
251 js->setCenter(center);
255 void FGJoystickInput::update( double dt )
257 float axis_values[MAX_JOYSTICK_AXES];
258 int modifiers = fgGetKeyModifiers();
261 for (int i = 0; i < MAX_JOYSTICKS; i++) {
263 jsJoystick * js = bindings[i].js;
264 if (js == 0 || js->notWorking())
267 js->read(&buttons, axis_values);
269 // Fire bindings for the axes.
270 for (int j = 0; j < bindings[i].naxes; j++) {
271 axis &a = bindings[i].axes[j];
273 // Do nothing if the axis position
274 // is unchanged; only a change in
275 // position fires the bindings.
276 if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
277 a.last_value = axis_values[j];
278 for (unsigned int k = 0; k < a.bindings[KEYMOD_NONE].size(); k++)
279 a.bindings[KEYMOD_NONE][k]->fire(axis_values[j]);
282 // do we have to emulate axis buttons?
284 if(a.last_dt >= a.interval_sec) {
285 if (a.low.bindings[modifiers].size())
286 bindings[i].axes[j].low.update( modifiers, axis_values[j] < a.low_threshold );
288 if (a.high.bindings[modifiers].size())
289 bindings[i].axes[j].high.update( modifiers, axis_values[j] > a.high_threshold );
291 a.last_dt -= a.interval_sec;
295 // Fire bindings for the buttons.
296 for (int j = 0; j < bindings[i].nbuttons; j++) {
297 FGButton &b = bindings[i].buttons[j];
299 if(b.last_dt >= b.interval_sec) {
300 bindings[i].buttons[j].update( modifiers, (buttons & (1 << j)) > 0 );
301 b.last_dt -= b.interval_sec;