]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
dea8995c52eb7a5c818b1469c126bdc7aa96edb9
[flightgear.git] / src / Input / input.cxx
1 // input.cxx -- handle user input from various sources.
2 //
3 // Written by David Megginson, started May 2001.
4 //
5 // Copyright (C) 2001 David Megginson, david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #ifdef HAVE_WINDOWS_H
28 #  include <windows.h>                     
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include <math.h>
34 #include <ctype.h>
35
36 #include STL_FSTREAM
37 #include STL_STRING
38 #include <vector>
39
40 #include <simgear/compiler.h>
41
42 #include <simgear/constants.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/props/props.hxx>
45
46 #include <Aircraft/aircraft.hxx>
47 #include <Autopilot/xmlauto.hxx>
48 #include <Cockpit/hud.hxx>
49 #include <Cockpit/panel.hxx>
50 #include <Cockpit/panel_io.hxx>
51 #include <GUI/gui.h>
52 #include <Model/panelnode.hxx>
53
54 #include <Main/globals.hxx>
55 #include <Main/fg_props.hxx>
56
57 #include "input.hxx"
58
59 SG_USING_STD(ifstream);
60 SG_USING_STD(string);
61 SG_USING_STD(vector);
62
63 void mouseClickHandler(int button, int updown, int x, int y);
64 void mouseMotionHandler(int x, int y);
65 void keyHandler(int key, int keymod, int mousex, int mousey);
66
67 \f
68 ////////////////////////////////////////////////////////////////////////
69 // Local variables.
70 ////////////////////////////////////////////////////////////////////////
71
72 static FGInput * default_input = 0;
73
74
75 \f
76 ////////////////////////////////////////////////////////////////////////
77 // Implementation of FGBinding.
78 ////////////////////////////////////////////////////////////////////////
79
80 FGBinding::FGBinding ()
81   : _command(0),
82     _arg(new SGPropertyNode),
83     _setting(0)
84 {
85 }
86
87 FGBinding::FGBinding (const SGPropertyNode * node)
88   : _command(0),
89     _arg(0),
90     _setting(0)
91 {
92   read(node);
93 }
94
95 void
96 FGBinding::read (const SGPropertyNode * node)
97 {
98   const SGPropertyNode * conditionNode = node->getChild("condition");
99   if (conditionNode != 0)
100     setCondition(sgReadCondition(globals->get_props(), conditionNode));
101
102   _command_name = node->getStringValue("command", "");
103   if (_command_name.empty()) {
104     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
105     _command = 0;
106     return;
107   }
108
109   _arg = new SGPropertyNode;
110   _setting = 0;
111   copyProperties(node, _arg);  // FIXME: don't use whole node!!!
112 }
113
114 void
115 FGBinding::fire () const
116 {
117   if (test()) {
118     if (_command == 0)
119       _command = globals->get_commands()->getCommand(_command_name);
120     if (_command == 0) {
121       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
122     } else if (!(*_command)(_arg)) {
123       SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
124              << _command_name);
125     }
126   }
127 }
128
129 void
130 FGBinding::fire (double offset, double max) const
131 {
132   if (test()) {
133     _arg->setDoubleValue("offset", offset/max);
134     fire();
135   }
136 }
137
138 void
139 FGBinding::fire (double setting) const
140 {
141   if (test()) {
142                                 // A value is automatically added to
143                                 // the args
144     if (_setting == 0)          // save the setting node for efficiency
145       _setting = _arg->getChild("setting", 0, true);
146     _setting->setDoubleValue(setting);
147     fire();
148   }
149 }
150
151
152 \f
153 ////////////////////////////////////////////////////////////////////////
154 // Implementation of FGInput.
155 ////////////////////////////////////////////////////////////////////////
156
157
158 FGInput::FGInput ()
159 {
160     if (default_input == 0)
161         default_input = this;
162 }
163
164 FGInput::~FGInput ()
165 {
166     if (default_input == this)
167         default_input = 0;
168 }
169
170 void
171 FGInput::init ()
172 {
173   _init_keyboard();
174   _init_joystick();
175   _init_mouse();
176
177   fgRegisterKeyHandler(keyHandler);
178   fgRegisterMouseClickHandler(mouseClickHandler);
179   fgRegisterMouseMotionHandler(mouseMotionHandler);
180 }
181
182 void 
183 FGInput::update (double dt)
184 {
185   _update_keyboard();
186   _update_joystick(dt);
187   _update_mouse(dt);
188 }
189
190 void
191 FGInput::suspend ()
192 {
193     // NO-OP
194 }
195
196 void
197 FGInput::resume ()
198 {
199     // NO-OP
200 }
201
202 bool
203 FGInput::is_suspended () const
204 {
205     return false;
206 }
207
208 void
209 FGInput::makeDefault (bool status)
210 {
211     if (status)
212         default_input = this;
213     else if (default_input == this)
214         default_input = 0;
215 }
216
217 void
218 FGInput::doKey (int k, int modifiers, int x, int y)
219 {
220                                 // Sanity check.
221   if (k < 0 || k >= MAX_KEYS) {
222     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
223     return;
224   }
225
226   button &b = _key_bindings[k];
227
228                                 // Key pressed.
229   if (modifiers&KEYMOD_RELEASED == 0) {
230     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
231             << " with modifiers " << modifiers );
232     if (!b.last_state || b.is_repeatable) {
233       const binding_list_t &bindings =
234         _find_key_bindings(k, modifiers);
235       int max = bindings.size();
236       if (max > 0) {
237         for (int i = 0; i < max; i++)
238           bindings[i]->fire();
239         return;
240       }
241     }
242   }
243                                 // Key released.
244   else {
245     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
246            << " with modifiers " << modifiers);
247     if (b.last_state) {
248       const binding_list_t &bindings =
249         _find_key_bindings(k, modifiers);
250       int max = bindings.size();
251       if (max > 0) {
252         for (int i = 0; i < max; i++)
253           bindings[i]->fire();
254         return;
255       }
256     }
257   }
258 }
259
260 void
261 FGInput::doMouseClick (int b, int updown, int x, int y)
262 {
263   int modifiers = fgGetKeyModifiers();
264
265   mouse &m = _mouse_bindings[0];
266   mouse_mode &mode = m.modes[m.current_mode];
267
268                                 // Let the property manager know.
269   if (b >= 0 && b < MAX_MOUSE_BUTTONS)
270     m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
271
272                                 // Pass on to PUI and the panel if
273                                 // requested, and return if one of
274                                 // them consumes the event.
275   if (mode.pass_through) {
276     if (puMouse(b, updown, x, y))
277       return;
278     else if ((globals->get_current_panel() != 0) &&
279              globals->get_current_panel()->getVisibility() &&
280              globals->get_current_panel()->doMouseAction(b, updown, x, y))
281       return;
282     else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
283       return;
284   }
285
286                                 // OK, PUI and the panel didn't want the click
287   if (b >= MAX_MOUSE_BUTTONS) {
288     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
289            << " where only " << MAX_MOUSE_BUTTONS << " expected");
290     return;
291   }
292
293   _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
294 }
295
296 void
297 FGInput::doMouseMotion (int x, int y)
298 {
299   // Don't call fgGetKeyModifiers() here, until we are using a
300   // toolkit that supports getting the mods from outside a key
301   // callback.  Glut doesn't.
302   int modifiers = KEYMOD_NONE;
303
304   int xsize = fgGetInt("/sim/startup/xsize", 800);
305   int ysize = fgGetInt("/sim/startup/ysize", 600);
306
307   mouse &m = _mouse_bindings[0];
308
309   if (m.current_mode < 0 || m.current_mode >= m.nModes) {
310       m.x = x;
311       m.y = y;
312       return;
313   }
314   mouse_mode &mode = m.modes[m.current_mode];
315
316                                 // Pass on to PUI if requested, and return
317                                 // if PUI consumed the event.
318   if (mode.pass_through && puMouse(x, y)) {
319       m.x = x;
320       m.y = y;
321       return;
322   }
323
324                                 // OK, PUI didn't want the event,
325                                 // so we can play with it.
326   if (x != m.x) {
327     int delta = x - m.x;
328     for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
329       mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
330   }
331   if (y != m.y) {
332     int delta = y - m.y;
333     for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
334       mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
335   }
336
337                                 // Constrain the mouse if requested
338   if (mode.constrained) {
339     bool need_warp = false;
340     if (x <= 0) {
341       x = xsize - 2;
342       need_warp = true;
343     } else if (x >= (xsize-1)) {
344       x = 1;
345       need_warp = true;
346     }
347
348     if (y <= 0) {
349       y = ysize - 2;
350       need_warp = true;
351     } else if (y >= (ysize-1)) {
352       y = 1;
353       need_warp = true;
354     }
355
356     if (need_warp)
357       fgWarpMouse(x, y);
358   }
359   m.x = x;
360   m.y = y;
361 }
362
363 void
364 FGInput::_init_keyboard ()
365 {
366   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
367   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
368   if (key_nodes == 0) {
369     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
370     key_nodes = fgGetNode("/input/keyboard", true);
371   }
372   
373   vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
374   for (unsigned int i = 0; i < keys.size(); i++) {
375     int index = keys[i]->getIndex();
376     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
377
378     _key_bindings[index].bindings->clear();
379     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
380     _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
381   }
382 }
383
384
385 void
386 FGInput::_init_joystick ()
387 {
388   jsInit();
389                                 // TODO: zero the old bindings first.
390   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
391   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
392   if (js_nodes == 0) {
393     SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
394     js_nodes = fgGetNode("/input/joysticks", true);
395   }
396
397   for (int i = 0; i < MAX_JOYSTICKS; i++) {
398     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
399     if (js_node == 0) {
400       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
401       js_node = js_nodes->getChild("js", i, true);
402     }
403     jsJoystick * js = new jsJoystick(i);
404     _joystick_bindings[i].js = js;
405     if (js->notWorking()) {
406       SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
407       continue;
408     } else {
409       bool found_js = false;
410       const char * name = js->getName();
411       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
412              << name << '"');
413       vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
414       for (unsigned int i = 0; i < nodes.size(); i++) {
415         SGPropertyNode_ptr node = nodes[i];
416         vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
417         for (unsigned int j = 0; j < name_nodes.size(); j++) {
418             const char * js_name = name_nodes[j]->getStringValue();
419             SG_LOG(SG_INPUT, SG_INFO, "  Trying \"" << js_name << '"');
420             if (!strcmp(js_name, name)) {
421                 SG_LOG(SG_INPUT, SG_INFO, "  Found bindings");
422                 js_node = node;
423                 found_js = true;
424                 break;
425             }
426         }
427         if (found_js)
428             break;
429       }
430     }
431 #ifdef WIN32
432     JOYCAPS jsCaps ;
433     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
434     int nbuttons = jsCaps.wNumButtons;
435     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
436 #else
437     int nbuttons = MAX_JOYSTICK_BUTTONS;
438 #endif
439         
440     int naxes = js->getNumAxes();
441     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
442     _joystick_bindings[i].naxes = naxes;
443     _joystick_bindings[i].nbuttons = nbuttons;
444
445     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
446
447                                 // Set up range arrays
448     float minRange[MAX_JOYSTICK_AXES];
449     float maxRange[MAX_JOYSTICK_AXES];
450     float center[MAX_JOYSTICK_AXES];
451
452                                 // Initialize with default values
453     js->getMinRange(minRange);
454     js->getMaxRange(maxRange);
455     js->getCenter(center);
456
457                                 // Allocate axes and buttons
458     _joystick_bindings[i].axes = new axis[naxes];
459     _joystick_bindings[i].buttons = new button[nbuttons];
460
461
462     //
463     // Initialize the axes.
464     //
465     vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
466     size_t nb_axes = axes.size();
467     int j;
468     for (j = 0; j < (int)nb_axes; j++) {
469       const SGPropertyNode * axis_node = axes[j];
470       const SGPropertyNode * num_node = axis_node->getChild("number");
471       size_t n_axis = axis_node->getIndex();
472       if (num_node != 0) {
473           n_axis = num_node->getIntValue(TGT_PLATFORM,n_axis);
474       }
475
476       axis &a = _joystick_bindings[i].axes[n_axis];
477
478       js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
479
480       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
481       minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
482       maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
483       center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
484
485       _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
486
487       // Initialize the virtual axis buttons.
488       _init_button(axis_node->getChild("low"), a.low, "low");
489       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
490       
491       _init_button(axis_node->getChild("high"), a.high, "high");
492       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
493       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
494       a.last_dt = 0.0;
495     }
496
497     //
498     // Initialize the buttons.
499     //
500     vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
501     char buf[32];
502     for (j = 0; (j < (int)buttons.size()) && (j < nbuttons); j++) {
503       const SGPropertyNode * button_node = buttons[j];
504       const SGPropertyNode * num_node = button_node->getChild("number");
505       size_t n_but = button_node->getIndex();
506       if (num_node != 0) {
507           n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
508       }
509       sprintf(buf, "%d", n_but);
510       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
511       _init_button(button_node,
512                    _joystick_bindings[i].buttons[n_but],
513                    buf);
514       
515       // get interval-sec property             
516       button &b = _joystick_bindings[i].buttons[n_but];
517       if (button_node != 0) {
518         b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
519         b.last_dt = 0.0;
520       }
521     }
522
523     js->setMinRange(minRange);
524     js->setMaxRange(maxRange);
525     js->setCenter(center);
526   }
527 }
528
529 // 
530 // Map of all known cursor names
531 // This used to contain all the Glut cursors, but those are
532 // not defined by other toolkits.  It now supports only the cursor
533 // images we actually use, in the interest of portability.  Someday,
534 // it would be cool to write an OpenGL cursor renderer, with the
535 // cursors defined as textures referenced in the property tree.  This
536 // list could then be eliminated. -Andy
537 //
538 struct {
539   const char * name;
540   int cursor;
541 } mouse_cursor_map[] = {
542   { "none", MOUSE_CURSOR_NONE },
543   { "inherit", MOUSE_CURSOR_POINTER },
544   { "wait", MOUSE_CURSOR_WAIT },
545   { "crosshair", MOUSE_CURSOR_CROSSHAIR },
546   { "left-right", MOUSE_CURSOR_LEFTRIGHT },
547   { 0, 0 }
548 };
549
550 void
551 FGInput::_init_mouse ()
552 {
553   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
554
555   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
556   if (mouse_nodes == 0) {
557     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
558     mouse_nodes = fgGetNode("/input/mice", true);
559   }
560
561   int j;
562   for (int i = 0; i < MAX_MICE; i++) {
563     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
564     mouse &m = _mouse_bindings[i];
565
566                                 // Grab node pointers
567     char buf[64];
568     sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
569     m.mode_node = fgGetNode(buf);
570     if (m.mode_node == NULL) {
571       m.mode_node = fgGetNode(buf, true);
572       m.mode_node->setIntValue(0);
573     }
574     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
575       sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
576       m.mouse_button_nodes[j] = fgGetNode(buf, true);
577       m.mouse_button_nodes[j]->setBoolValue(false);
578     }
579
580                                 // Read all the modes
581     m.nModes = mouse_node->getIntValue("mode-count", 1);
582     m.modes = new mouse_mode[m.nModes];
583
584     for (int j = 0; j < m.nModes; j++) {
585       int k;
586
587                                 // Read the mouse cursor for this mode
588       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
589       const char * cursor_name =
590         mode_node->getStringValue("cursor", "inherit");
591       m.modes[j].cursor = MOUSE_CURSOR_POINTER;
592       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
593         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
594           m.modes[j].cursor = mouse_cursor_map[k].cursor;
595           break;
596         }
597       }
598
599                                 // Read other properties for this mode
600       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
601       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
602
603                                 // Read the button bindings for this mode
604       m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
605       char buf[32];
606       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
607         sprintf(buf, "mouse button %d", k);
608         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
609         _init_button(mode_node->getChild("button", k),
610                      m.modes[j].buttons[k],
611                      buf);
612       }
613
614                                 // Read the axis bindings for this mode
615       _read_bindings(mode_node->getChild("x-axis", 0, true),
616                      m.modes[j].x_bindings,
617                      KEYMOD_NONE);
618       _read_bindings(mode_node->getChild("y-axis", 0, true),
619                      m.modes[j].y_bindings,
620                      KEYMOD_NONE);
621     }
622   }
623 }
624
625
626 void
627 FGInput::_init_button (const SGPropertyNode * node,
628                        button &b,
629                        const string name)
630 {       
631   if (node == 0) {
632     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
633   } else {
634     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
635     
636                 // Get the bindings for the button
637     _read_bindings(node, b.bindings, KEYMOD_NONE);
638   }
639 }
640
641
642 void
643 FGInput::_update_keyboard ()
644 {
645   // no-op
646 }
647
648
649 void
650 FGInput::_update_joystick (double dt)
651 {
652   int modifiers = KEYMOD_NONE;  // FIXME: any way to get the real ones?
653   int buttons;
654   // float js_val, diff;
655   float axis_values[MAX_JOYSTICK_AXES];
656
657   int i;
658   int j;
659
660   for ( i = 0; i < MAX_JOYSTICKS; i++) {
661
662     jsJoystick * js = _joystick_bindings[i].js;
663     if (js == 0 || js->notWorking())
664       continue;
665
666     js->read(&buttons, axis_values);
667
668
669                                 // Fire bindings for the axes.
670     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
671       axis &a = _joystick_bindings[i].axes[j];
672       
673                                 // Do nothing if the axis position
674                                 // is unchanged; only a change in
675                                 // position fires the bindings.
676       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
677 //      SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
678         SGPropertyNode node;
679         a.last_value = axis_values[j];
680 //      SG_LOG(SG_INPUT, SG_DEBUG, "There are "
681 //             << a.bindings[modifiers].size() << " bindings");
682         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
683           a.bindings[modifiers][k]->fire(axis_values[j]);
684       }
685      
686                                 // do we have to emulate axis buttons?
687       a.last_dt += dt;
688       if(a.last_dt >= a.interval_sec) {
689         if (a.low.bindings[modifiers].size())
690           _update_button(_joystick_bindings[i].axes[j].low,
691                          modifiers,
692                          axis_values[j] < a.low_threshold,
693                          -1, -1);
694       
695         if (a.high.bindings[modifiers].size())
696           _update_button(_joystick_bindings[i].axes[j].high,
697                          modifiers,
698                          axis_values[j] > a.high_threshold,
699                          -1, -1);
700          a.last_dt -= a.interval_sec;
701       }
702     }
703
704                                 // Fire bindings for the buttons.
705     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
706       button &b = _joystick_bindings[i].buttons[j];
707       b.last_dt += dt;
708       if(b.last_dt >= b.interval_sec) {
709         _update_button(_joystick_bindings[i].buttons[j],
710                        modifiers,
711                        (buttons & (1 << j)) > 0,
712                        -1, -1);
713         b.last_dt -= b.interval_sec;
714       }
715     }
716   }
717 }
718
719 void
720 FGInput::_update_mouse ( double dt )
721 {
722   mouse &m = _mouse_bindings[0];
723   int mode =  m.mode_node->getIntValue();
724   if (mode != m.current_mode) {
725     m.current_mode = mode;
726     m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
727     if (mode >= 0 && mode < m.nModes) {
728       fgSetMouseCursor(m.modes[mode].cursor);
729       m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
730       m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
731       fgWarpMouse(m.x, m.y);
732     } else {
733       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
734       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
735     }
736   }
737
738   if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
739       if ( m.x != m.save_x || m.y != m.save_y ) {
740           m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
741           fgSetMouseCursor(m.modes[mode].cursor);
742       } else {
743           m.timeout -= dt;
744           if ( m.timeout <= 0.0 ) {
745               fgSetMouseCursor(MOUSE_CURSOR_NONE);
746               m.timeout = 0.0;
747           }
748       }
749       m.save_x = m.x;
750       m.save_y = m.y;
751   }
752 }
753
754 void
755 FGInput::_update_button (button &b, int modifiers, bool pressed,
756                          int x, int y)
757 {
758   if (pressed) {
759                                 // The press event may be repeated.
760     if (!b.last_state || b.is_repeatable) {
761       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
762       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
763         b.bindings[modifiers][k]->fire(x, y);
764     }
765   } else {
766                                 // The release event is never repeated.
767     if (b.last_state) {
768       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
769       for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
770         b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
771     }
772   }
773           
774   b.last_state = pressed;
775 }  
776
777
778 void
779 FGInput::_read_bindings (const SGPropertyNode * node, 
780                          binding_list_t * binding_list,
781                          int modifiers)
782 {
783   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
784   vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
785   for (unsigned int i = 0; i < bindings.size(); i++) {
786     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
787            << bindings[i]->getStringValue("command"));
788     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
789   }
790
791                                 // Read nested bindings for modifiers
792   if (node->getChild("mod-up") != 0)
793     _read_bindings(node->getChild("mod-up"), binding_list,
794                    modifiers|KEYMOD_RELEASED);
795
796   if (node->getChild("mod-shift") != 0)
797     _read_bindings(node->getChild("mod-shift"), binding_list,
798                    modifiers|KEYMOD_SHIFT);
799
800   if (node->getChild("mod-ctrl") != 0)
801     _read_bindings(node->getChild("mod-ctrl"), binding_list,
802                    modifiers|KEYMOD_CTRL);
803
804   if (node->getChild("mod-alt") != 0)
805     _read_bindings(node->getChild("mod-alt"), binding_list,
806                    modifiers|KEYMOD_ALT);
807 }
808
809
810 const vector<FGBinding *> &
811 FGInput::_find_key_bindings (unsigned int k, int modifiers)
812 {
813   unsigned char kc = (unsigned char)k;
814   button &b = _key_bindings[k];
815
816                                 // Try it straight, first.
817   if (b.bindings[modifiers].size() > 0)
818     return b.bindings[modifiers];
819
820                                 // Alt-Gr is CTRL+ALT
821   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
822     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
823
824                                 // Try removing the control modifier
825                                 // for control keys.
826   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
827     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
828
829                                 // Try removing shift modifier 
830                                 // for upper case or any punctuation
831                                 // (since different keyboards will
832                                 // shift different punctuation types)
833   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
834     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
835
836                                 // Try removing alt modifier for
837                                 // high-bit characters.
838   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
839     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
840
841                                 // Give up and return the empty vector.
842   else
843     return b.bindings[modifiers];
844 }
845
846
847 \f
848 ////////////////////////////////////////////////////////////////////////
849 // Implementation of FGInput::button.
850 ////////////////////////////////////////////////////////////////////////
851
852 FGInput::button::button ()
853   : is_repeatable(false),
854     last_state(-1)
855 {
856 }
857
858 FGInput::button::~button ()
859 {
860                                 // FIXME: memory leak
861 //   for (int i = 0; i < KEYMOD_MAX; i++)
862 //     for (int j = 0; i < bindings[i].size(); j++)
863 //       delete bindings[i][j];
864 }
865
866
867 \f
868 ////////////////////////////////////////////////////////////////////////
869 // Implementation of FGInput::axis.
870 ////////////////////////////////////////////////////////////////////////
871
872 FGInput::axis::axis ()
873   : last_value(9999999),
874     tolerance(0.002),
875     low_threshold(-0.9),
876     high_threshold(0.9)
877 {
878 }
879
880 FGInput::axis::~axis ()
881 {
882 //   for (int i = 0; i < KEYMOD_MAX; i++)
883 //     for (int j = 0; i < bindings[i].size(); j++)
884 //       delete bindings[i][j];
885 }
886
887
888 \f
889 ////////////////////////////////////////////////////////////////////////
890 // Implementation of FGInput::joystick.
891 ////////////////////////////////////////////////////////////////////////
892
893 FGInput::joystick::joystick ()
894 {
895 }
896
897 FGInput::joystick::~joystick ()
898 {
899 //   delete js;
900   delete[] axes;
901   delete[] buttons;
902 }
903
904
905 \f
906 ////////////////////////////////////////////////////////////////////////
907 // Implementation of FGInput::mouse_mode
908 ////////////////////////////////////////////////////////////////////////
909
910 FGInput::mouse_mode::mouse_mode ()
911   : cursor(MOUSE_CURSOR_POINTER),
912     constrained(false),
913     pass_through(false),
914     buttons(0)
915 {
916 }
917
918 FGInput::mouse_mode::~mouse_mode ()
919 {
920                                 // FIXME: memory leak
921 //   for (int i = 0; i < KEYMOD_MAX; i++) {
922 //     int j;
923 //     for (j = 0; i < x_bindings[i].size(); j++)
924 //       delete bindings[i][j];
925 //     for (j = 0; j < y_bindings[i].size(); j++)
926 //       delete bindings[i][j];
927 //   }
928   delete [] buttons;
929 }
930
931
932 \f
933 ////////////////////////////////////////////////////////////////////////
934 // Implementation of FGInput::mouse
935 ////////////////////////////////////////////////////////////////////////
936
937 FGInput::mouse::mouse ()
938   : x(-1),
939     y(-1),
940     nModes(1),
941     current_mode(0),
942     modes(0)
943 {
944 }
945
946 FGInput::mouse::~mouse ()
947 {
948   delete [] modes;
949 }
950
951 ////////////////////////////////////////////////////////////////////////
952 // Implementation of OS callbacks.
953 ////////////////////////////////////////////////////////////////////////
954
955 void keyHandler(int key, int keymod, int mousex, int mousey)
956 {
957     if((keymod & KEYMOD_RELEASED) == 0)
958         if(puKeyboard(key, PU_DOWN))
959             return;
960
961     if(default_input)
962         default_input->doKey(key, keymod, mousex, mousey);
963 }
964
965 void mouseClickHandler(int button, int updown, int x, int y)
966 {
967     if(default_input)
968         default_input->doMouseClick(button, updown, x, y);
969 }
970
971 void mouseMotionHandler(int x, int y)
972 {
973     if (default_input != 0)
974         default_input->doMouseMotion(x, y);
975 }