]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGJoystickInput.cxx
Restore named JS/input configs (overrides)
[flightgear.git] / src / Input / FGJoystickInput.cxx
1 // FGJoystickInput.cxx -- handle user input from joystick devices
2 //
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
5 //
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include "FGJoystickInput.hxx"
30
31 #include <simgear/props/props_io.hxx>
32 #include "FGDeviceConfigurationMap.hxx"
33 #include <Main/fg_props.hxx>
34 #include <Scripting/NasalSys.hxx>
35 #include <boost/foreach.hpp>
36
37 using simgear::PropertyList;
38
39 FGJoystickInput::axis::axis ()
40   : last_value(9999999),
41     tolerance(0.002),
42     low_threshold(-0.9),
43     high_threshold(0.9),
44     interval_sec(0),
45     last_dt(0)
46 {
47 }
48
49 FGJoystickInput::axis::~axis ()
50 {
51 }
52
53 FGJoystickInput::joystick::joystick ()
54   : jsnum(0),
55     js(0),
56     naxes(0),
57     nbuttons(0),
58     axes(0),
59     buttons(0),
60     predefined(true)
61 {
62 }
63
64 FGJoystickInput::joystick::~joystick ()
65 {
66   //  delete js? no, since js == this - and we're in the destructor already.
67   delete[] axes;
68   delete[] buttons;
69   jsnum = 0;
70   js = NULL;
71   naxes = 0;
72   nbuttons = 0;
73   axes = NULL;
74   buttons = NULL;
75 }
76
77
78 FGJoystickInput::FGJoystickInput()
79 {
80 }
81
82 FGJoystickInput::~FGJoystickInput()
83 {
84     _remove(true);
85 }
86
87 void FGJoystickInput::_remove(bool all)
88 {
89     SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true);
90
91     for (int i = 0; i < MAX_JOYSTICKS; i++)
92     {
93         // do not remove predefined joysticks info on reinit
94         if (all || (!bindings[i].predefined))
95             js_nodes->removeChild("js", i, false);
96         if (bindings[i].js)
97             delete bindings[i].js;
98         bindings[i].js = NULL;
99     }
100 }
101
102 void FGJoystickInput::init()
103 {
104   jsInit();
105   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
106   SGPropertyNode_ptr js_nodes = fgGetNode("/input/joysticks", true);
107   status_node = fgGetNode("/devices/status/joysticks", true);
108
109   FGDeviceConfigurationMap configMap("Input/Joysticks",js_nodes, "js-named");
110
111   for (int i = 0; i < MAX_JOYSTICKS; i++) {
112     jsJoystick * js = new jsJoystick(i);
113     bindings[i].js = js;
114
115     if (js->notWorking()) {
116       SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
117       continue;
118     }
119
120     const char * name = js->getName();
121     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
122
123     if (js_node) {
124       SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
125
126     } else {
127       bindings[i].predefined = false;
128       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
129       SGPropertyNode_ptr named;
130
131       if (configMap.hasConfiguration(name)) {
132         named = configMap.configurationForDeviceName(name);
133         string source = named->getStringValue("source", "user defined");
134         SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
135
136       } else if ((named = configMap.configurationForDeviceName("default"))) {
137         string source = named->getStringValue("source", "user defined");
138         SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
139             << "\"\nUsing default: \"" << source << '"');
140
141       } else {
142         SG_LOG(SG_INPUT, SG_WARN, "No joystick configuration file with <name>" << name << "</name> entry found!");
143       }
144
145       js_node = js_nodes->getChild("js", i, true);
146       copyProperties(named, js_node);
147       js_node->setStringValue("id", name);
148     }
149   }
150 }
151
152 void FGJoystickInput::reinit() {
153   SG_LOG(SG_INPUT, SG_DEBUG, "Re-Initializing joystick bindings");
154   _remove(false);
155   FGJoystickInput::init();
156   FGJoystickInput::postinit();
157 }
158
159 void FGJoystickInput::postinit()
160 {
161   FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
162   SGPropertyNode_ptr js_nodes = fgGetNode("/input/joysticks");
163
164   for (int i = 0; i < MAX_JOYSTICKS; i++) {
165     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
166     jsJoystick *js = bindings[i].js;
167     if (!js_node || js->notWorking())
168       continue;
169
170 #ifdef WIN32
171     JOYCAPS jsCaps ;
172     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
173     unsigned int nbuttons = jsCaps.wNumButtons;
174     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
175 #else
176     unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
177 #endif
178
179     int naxes = js->getNumAxes();
180     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
181     bindings[i].naxes = naxes;
182     bindings[i].nbuttons = nbuttons;
183
184     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
185
186                                 // Set up range arrays
187     float minRange[MAX_JOYSTICK_AXES];
188     float maxRange[MAX_JOYSTICK_AXES];
189     float center[MAX_JOYSTICK_AXES];
190
191                                 // Initialize with default values
192     js->getMinRange(minRange);
193     js->getMaxRange(maxRange);
194     js->getCenter(center);
195
196                                 // Allocate axes and buttons
197     bindings[i].axes = new axis[naxes];
198     bindings[i].buttons = new FGButton[nbuttons];
199
200     //
201     // Initialize nasal groups.
202     //
203     std::ostringstream str;
204     str << "__js" << i;
205     string module = str.str();
206     nasalsys->createModule(module.c_str(), module.c_str(), "", 0);
207
208     PropertyList nasal = js_node->getChildren("nasal");
209     unsigned int j;
210     for (j = 0; j < nasal.size(); j++) {
211       nasal[j]->setStringValue("module", module.c_str());
212       nasalsys->handleCommand(nasal[j]);
213     }
214
215     //
216     // Initialize the axes.
217     //
218     PropertyList axes = js_node->getChildren("axis");
219     size_t nb_axes = axes.size();
220     for (j = 0; j < nb_axes; j++ ) {
221       const SGPropertyNode * axis_node = axes[j];
222       const SGPropertyNode * num_node = axis_node->getChild("number");
223       int n_axis = axis_node->getIndex();
224       if (num_node != 0) {
225           n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
226
227         #ifdef SG_MAC
228           // Mac falls back to Unix by default - avoids specifying many
229           // duplicate <mac> entries in joystick config files
230           if (n_axis < 0) {
231               n_axis = num_node->getIntValue("unix", -1);
232           }
233         #endif
234           
235           // Silently ignore platforms that are not specified within the
236           // <number></number> section
237           if (n_axis < 0) {
238               continue;
239           }
240       }
241
242       if (n_axis >= naxes) {
243           SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
244           continue;
245       }
246       axis &a = bindings[i].axes[n_axis];
247
248       js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
249
250       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
251       minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
252       maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
253       center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
254
255       read_bindings(axis_node, a.bindings, KEYMOD_NONE, module );
256
257       // Initialize the virtual axis buttons.
258       a.low.init(axis_node->getChild("low"), "low", module );
259       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
260
261       a.high.init(axis_node->getChild("high"), "high", module );
262       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
263       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
264       a.last_dt = 0.0;
265     }
266
267     //
268     // Initialize the buttons.
269     //
270     PropertyList buttons = js_node->getChildren("button");
271     BOOST_FOREACH( SGPropertyNode * button_node, buttons ) {
272       size_t n_but = button_node->getIndex();
273
274       const SGPropertyNode * num_node = button_node->getChild("number");
275       if (NULL != num_node)
276           n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
277
278       if (n_but >= nbuttons) {
279           SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
280           continue;
281       }
282
283       std::ostringstream buf;
284       buf << (unsigned)n_but;
285
286       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << buf.str());
287       FGButton &b = bindings[i].buttons[n_but];
288       b.init(button_node, buf.str(), module );
289       // get interval-sec property
290       b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
291       b.last_dt = 0.0;
292     }
293
294     js->setMinRange(minRange);
295     js->setMaxRange(maxRange);
296     js->setCenter(center);
297   }
298 }
299
300 void FGJoystickInput::update( double dt )
301 {
302   float axis_values[MAX_JOYSTICK_AXES];
303   int modifiers = fgGetKeyModifiers();
304   int buttons;
305
306   for (int i = 0; i < MAX_JOYSTICKS; i++) {
307
308     jsJoystick * js = bindings[i].js;
309     if (js == 0 || js->notWorking())
310       continue;
311
312     js->read(&buttons, axis_values);
313     if (js->notWorking()) // If js is disconnected
314       continue;
315             
316     // Update device status    
317     SGPropertyNode_ptr status = status_node->getChild("joystick", i, true);
318     for (int j = 0; j < MAX_JOYSTICK_AXES; j++) {
319       status->getChild("axis", j, true)->setFloatValue(axis_values[j]);
320     }
321     
322     for (int j = 0; j < MAX_JOYSTICK_BUTTONS; j++) {
323       status->getChild("button", j, true)->setBoolValue((buttons & (1u << j)) > 0 );
324     }
325
326                                 // Fire bindings for the axes.
327     for (int j = 0; j < bindings[i].naxes; j++) {
328       axis &a = bindings[i].axes[j];
329
330                                 // Do nothing if the axis position
331                                 // is unchanged; only a change in
332                                 // position fires the bindings.
333                                 // But only if there are bindings
334       if (fabs(axis_values[j] - a.last_value) > a.tolerance
335          && a.bindings[KEYMOD_NONE].size() > 0 ) {
336         a.last_value = axis_values[j];
337         for (unsigned int k = 0; k < a.bindings[KEYMOD_NONE].size(); k++)
338           a.bindings[KEYMOD_NONE][k]->fire(axis_values[j]);
339       }
340
341                                 // do we have to emulate axis buttons?
342       a.last_dt += dt;
343       if(a.last_dt >= a.interval_sec) {
344         if (a.low.bindings[modifiers].size())
345           bindings[i].axes[j].low.update( modifiers, axis_values[j] < a.low_threshold );
346
347         if (a.high.bindings[modifiers].size())
348           bindings[i].axes[j].high.update( modifiers, axis_values[j] > a.high_threshold );
349
350         a.last_dt -= a.interval_sec;
351       }
352     }
353
354                                 // Fire bindings for the buttons.
355     for (int j = 0; j < bindings[i].nbuttons; j++) {
356       FGButton &b = bindings[i].buttons[j];
357       b.last_dt += dt;
358       if(b.last_dt >= b.interval_sec) {
359         bindings[i].buttons[j].update( modifiers, (buttons & (1u << j)) > 0 );
360         b.last_dt -= b.interval_sec;
361       }
362     }
363   }
364 }
365