]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGJoystickInput.cxx
PLIB net removed from FlightGear
[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 #include "FGJoystickInput.hxx"
26 #include "FGDeviceConfigurationMap.hxx"
27 #include <Main/fg_props.hxx>
28 #include <Scripting/NasalSys.hxx>
29
30 using simgear::PropertyList;
31
32 FGJoystickInput::axis::axis ()
33   : last_value(9999999),
34     tolerance(0.002),
35     low_threshold(-0.9),
36     high_threshold(0.9),
37     interval_sec(0),
38     last_dt(0)
39 {
40 }
41
42 FGJoystickInput::axis::~axis ()
43 {
44 }
45
46 FGJoystickInput::joystick::joystick ()
47   : jsnum(0),
48     js(0),
49     naxes(0),
50     nbuttons(0),
51     axes(0),
52     buttons(0)
53 {
54 }
55
56 FGJoystickInput::joystick::~joystick ()
57 {
58 //  delete js? why not?
59 //   delete js;
60   delete[] axes;
61   delete[] buttons;
62 }
63
64
65 FGJoystickInput::FGJoystickInput()
66 {
67 }
68
69 FGJoystickInput::~FGJoystickInput()
70 {
71 }
72
73
74 void FGJoystickInput::init()
75 {
76   jsInit();
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);
80
81   FGDeviceConfigurationMap configMap("Input/Joysticks", js_nodes, "js-named");
82
83   for (int i = 0; i < MAX_JOYSTICKS; i++) {
84     jsJoystick * js = new jsJoystick(i);
85     bindings[i].js = js;
86
87     if (js->notWorking()) {
88       SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
89       continue;
90     }
91
92     const char * name = js->getName();
93     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
94
95     if (js_node) {
96       SG_LOG(SG_INPUT, SG_INFO, "Using existing bindings for joystick " << i);
97
98     } else {
99       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
100       SGPropertyNode_ptr named;
101
102       if ((named = configMap[name])) {
103         string source = named->getStringValue("source", "user defined");
104         SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
105
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 << '"');
110
111       } else {
112         SG_LOG(SG_INPUT, SG_WARN, "No joystick configuration file with <name>" << name << "</name> entry found!");
113       }
114
115       js_node = js_nodes->getChild("js", i, true);
116       copyProperties(named, js_node);
117       js_node->setStringValue("id", name);
118     }
119   }
120 }
121
122 void FGJoystickInput::postinit()
123 {
124   FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
125   SGPropertyNode_ptr js_nodes = fgGetNode("/input/joysticks");
126
127   for (int i = 0; i < MAX_JOYSTICKS; i++) {
128     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
129     jsJoystick *js = bindings[i].js;
130     if (!js_node || js->notWorking())
131       continue;
132
133 #ifdef WIN32
134     JOYCAPS jsCaps ;
135     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
136     unsigned int nbuttons = jsCaps.wNumButtons;
137     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
138 #else
139     unsigned int nbuttons = MAX_JOYSTICK_BUTTONS;
140 #endif
141
142     int naxes = js->getNumAxes();
143     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
144     bindings[i].naxes = naxes;
145     bindings[i].nbuttons = nbuttons;
146
147     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
148
149                                 // Set up range arrays
150     float minRange[MAX_JOYSTICK_AXES];
151     float maxRange[MAX_JOYSTICK_AXES];
152     float center[MAX_JOYSTICK_AXES];
153
154                                 // Initialize with default values
155     js->getMinRange(minRange);
156     js->getMaxRange(maxRange);
157     js->getCenter(center);
158
159                                 // Allocate axes and buttons
160     bindings[i].axes = new axis[naxes];
161     bindings[i].buttons = new FGButton[nbuttons];
162
163     //
164     // Initialize nasal groups.
165     //
166     ostringstream str;
167     str << "__js" << i;
168     string module = str.str();
169     nasalsys->createModule(module.c_str(), module.c_str(), "", 0);
170
171     PropertyList nasal = js_node->getChildren("nasal");
172     unsigned int j;
173     for (j = 0; j < nasal.size(); j++) {
174       nasal[j]->setStringValue("module", module.c_str());
175       nasalsys->handleCommand(nasal[j]);
176     }
177
178     //
179     // Initialize the axes.
180     //
181     PropertyList axes = js_node->getChildren("axis");
182     size_t nb_axes = axes.size();
183     for (j = 0; j < nb_axes; j++ ) {
184       const SGPropertyNode * axis_node = axes[j];
185       const SGPropertyNode * num_node = axis_node->getChild("number");
186       int n_axis = axis_node->getIndex();
187       if (num_node != 0) {
188           n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
189
190           // Silently ignore platforms that are not specified within the
191           // <number></number> section
192           if (n_axis < 0)
193              continue;
194       }
195
196       if (n_axis >= naxes) {
197           SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
198           continue;
199       }
200       axis &a = bindings[i].axes[n_axis];
201
202       js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
203
204       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
205       minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
206       maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
207       center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
208
209       read_bindings(axis_node, a.bindings, KEYMOD_NONE, module );
210
211       // Initialize the virtual axis buttons.
212       a.low.init(axis_node->getChild("low"), "low", module );
213       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
214
215       a.high.init(axis_node->getChild("high"), "high", module );
216       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
217       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
218       a.last_dt = 0.0;
219     }
220
221     //
222     // Initialize the buttons.
223     //
224     PropertyList buttons = js_node->getChildren("button");
225     char buf[32];
226     for (j = 0; j < buttons.size() && j < nbuttons; j++) {
227       const SGPropertyNode * button_node = buttons[j];
228       const SGPropertyNode * num_node = button_node->getChild("number");
229       size_t n_but = button_node->getIndex();
230       if (num_node != 0) {
231           n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
232       }
233
234       if (n_but >= nbuttons) {
235           SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
236           continue;
237       }
238
239       sprintf(buf, "%u", (unsigned)n_but);
240       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
241       bindings[i].buttons[n_but].init(button_node, buf, module );
242
243       // get interval-sec property
244       FGButton &b = bindings[i].buttons[n_but];
245       if (button_node != 0) {
246         b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
247         b.last_dt = 0.0;
248       }
249     }
250
251     js->setMinRange(minRange);
252     js->setMaxRange(maxRange);
253     js->setCenter(center);
254   }
255 }
256
257 void FGJoystickInput::update( double dt )
258 {
259   float axis_values[MAX_JOYSTICK_AXES];
260   int modifiers = fgGetKeyModifiers();
261   int buttons;
262
263   for (int i = 0; i < MAX_JOYSTICKS; i++) {
264
265     jsJoystick * js = bindings[i].js;
266     if (js == 0 || js->notWorking())
267       continue;
268
269     js->read(&buttons, axis_values);
270
271                                 // Fire bindings for the axes.
272     for (int j = 0; j < bindings[i].naxes; j++) {
273       axis &a = bindings[i].axes[j];
274
275                                 // Do nothing if the axis position
276                                 // is unchanged; only a change in
277                                 // position fires the bindings.
278       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
279         a.last_value = axis_values[j];
280         for (unsigned int k = 0; k < a.bindings[KEYMOD_NONE].size(); k++)
281           a.bindings[KEYMOD_NONE][k]->fire(axis_values[j]);
282       }
283
284                                 // do we have to emulate axis buttons?
285       a.last_dt += dt;
286       if(a.last_dt >= a.interval_sec) {
287         if (a.low.bindings[modifiers].size())
288           bindings[i].axes[j].low.update( modifiers, axis_values[j] < a.low_threshold );
289
290         if (a.high.bindings[modifiers].size())
291           bindings[i].axes[j].high.update( modifiers, axis_values[j] > a.high_threshold );
292
293         a.last_dt -= a.interval_sec;
294       }
295     }
296
297                                 // Fire bindings for the buttons.
298     for (int j = 0; j < bindings[i].nbuttons; j++) {
299       FGButton &b = bindings[i].buttons[j];
300       b.last_dt += dt;
301       if(b.last_dt >= b.interval_sec) {
302         bindings[i].buttons[j].update( modifiers, (buttons & (1 << j)) > 0 );
303         b.last_dt -= b.interval_sec;
304       }
305     }
306   }
307 }
308