]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
55a311e6066049bc05bee795cc9398c247c0007e
[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::reinit ()
184 {
185     init();
186 }
187
188 void 
189 FGInput::update (double dt)
190 {
191   _update_keyboard();
192   _update_joystick(dt);
193   _update_mouse(dt);
194 }
195
196 void
197 FGInput::suspend ()
198 {
199     // NO-OP
200 }
201
202 void
203 FGInput::resume ()
204 {
205     // NO-OP
206 }
207
208 bool
209 FGInput::is_suspended () const
210 {
211     return false;
212 }
213
214 void
215 FGInput::makeDefault (bool status)
216 {
217     if (status)
218         default_input = this;
219     else if (default_input == this)
220         default_input = 0;
221 }
222
223 void
224 FGInput::doKey (int k, int modifiers, int x, int y)
225 {
226                                 // Sanity check.
227   if (k < 0 || k >= MAX_KEYS) {
228     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
229     return;
230   }
231
232   button &b = _key_bindings[k];
233
234                                 // Key pressed.
235   if (modifiers&KEYMOD_RELEASED == 0) {
236     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
237             << " with modifiers " << modifiers );
238     if (!b.last_state || b.is_repeatable) {
239       const binding_list_t &bindings =
240         _find_key_bindings(k, modifiers);
241       int max = bindings.size();
242       if (max > 0) {
243         for (int i = 0; i < max; i++)
244           bindings[i]->fire();
245         return;
246       }
247     }
248   }
249                                 // Key released.
250   else {
251     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
252            << " with modifiers " << modifiers);
253     if (b.last_state) {
254       const binding_list_t &bindings =
255         _find_key_bindings(k, modifiers);
256       int max = bindings.size();
257       if (max > 0) {
258         for (int i = 0; i < max; i++)
259           bindings[i]->fire();
260         return;
261       }
262     }
263   }
264 }
265
266 void
267 FGInput::doMouseClick (int b, int updown, int x, int y)
268 {
269   int modifiers = fgGetKeyModifiers();
270
271   mouse &m = _mouse_bindings[0];
272   mouse_mode &mode = m.modes[m.current_mode];
273
274                                 // Let the property manager know.
275   if (b >= 0 && b < MAX_MOUSE_BUTTONS)
276     m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
277
278                                 // Pass on to PUI and the panel if
279                                 // requested, and return if one of
280                                 // them consumes the event.
281   if (mode.pass_through) {
282     if (puMouse(b, updown, x, y))
283       return;
284     else if ((globals->get_current_panel() != 0) &&
285              globals->get_current_panel()->getVisibility() &&
286              globals->get_current_panel()->doMouseAction(b, updown, x, y))
287       return;
288     else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
289       return;
290   }
291
292                                 // OK, PUI and the panel didn't want the click
293   if (b >= MAX_MOUSE_BUTTONS) {
294     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
295            << " where only " << MAX_MOUSE_BUTTONS << " expected");
296     return;
297   }
298
299   _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
300 }
301
302 void
303 FGInput::doMouseMotion (int x, int y)
304 {
305   // Don't call fgGetKeyModifiers() here, until we are using a
306   // toolkit that supports getting the mods from outside a key
307   // callback.  Glut doesn't.
308   int modifiers = KEYMOD_NONE;
309
310   int xsize = fgGetInt("/sim/startup/xsize", 800);
311   int ysize = fgGetInt("/sim/startup/ysize", 600);
312
313   mouse &m = _mouse_bindings[0];
314
315   if (m.current_mode < 0 || m.current_mode >= m.nModes) {
316       m.x = x;
317       m.y = y;
318       return;
319   }
320   mouse_mode &mode = m.modes[m.current_mode];
321
322                                 // Pass on to PUI if requested, and return
323                                 // if PUI consumed the event.
324   if (mode.pass_through && puMouse(x, y)) {
325       m.x = x;
326       m.y = y;
327       return;
328   }
329
330                                 // OK, PUI didn't want the event,
331                                 // so we can play with it.
332   if (x != m.x) {
333     int delta = x - m.x;
334     for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
335       mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
336   }
337   if (y != m.y) {
338     int delta = y - m.y;
339     for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
340       mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
341   }
342
343                                 // Constrain the mouse if requested
344   if (mode.constrained) {
345     bool need_warp = false;
346     if (x <= 0) {
347       x = xsize - 2;
348       need_warp = true;
349     } else if (x >= (xsize-1)) {
350       x = 1;
351       need_warp = true;
352     }
353
354     if (y <= 0) {
355       y = ysize - 2;
356       need_warp = true;
357     } else if (y >= (ysize-1)) {
358       y = 1;
359       need_warp = true;
360     }
361
362     if (need_warp)
363       fgWarpMouse(x, y);
364   }
365   m.x = x;
366   m.y = y;
367 }
368
369 void
370 FGInput::_init_keyboard ()
371 {
372   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
373   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
374   if (key_nodes == 0) {
375     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
376     key_nodes = fgGetNode("/input/keyboard", true);
377   }
378   
379   vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
380   for (unsigned int i = 0; i < keys.size(); i++) {
381     int index = keys[i]->getIndex();
382     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
383
384     _key_bindings[index].bindings->clear();
385     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
386     _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
387   }
388 }
389
390
391 void
392 FGInput::_init_joystick ()
393 {
394   jsInit();
395                                 // TODO: zero the old bindings first.
396   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
397   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
398   if (js_nodes == 0) {
399     SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
400     js_nodes = fgGetNode("/input/joysticks", true);
401   }
402
403   for (int i = 0; i < MAX_JOYSTICKS; i++) {
404     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
405     if (js_node == 0) {
406       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
407       js_node = js_nodes->getChild("js", i, true);
408     }
409     jsJoystick * js = new jsJoystick(i);
410     _joystick_bindings[i].js = js;
411     if (js->notWorking()) {
412       SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
413       continue;
414     } else {
415       bool found_js = false;
416       const char * name = js->getName();
417       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
418              << name << '"');
419       vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
420       for (unsigned int i = 0; i < nodes.size(); i++) {
421         SGPropertyNode_ptr node = nodes[i];
422         vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
423         for (unsigned int j = 0; j < name_nodes.size(); j++) {
424             const char * js_name = name_nodes[j]->getStringValue();
425             SG_LOG(SG_INPUT, SG_INFO, "  Trying \"" << js_name << '"');
426             if (!strcmp(js_name, name)) {
427                 SG_LOG(SG_INPUT, SG_INFO, "  Found bindings");
428                 js_node = node;
429                 found_js = true;
430                 break;
431             }
432         }
433         if (found_js)
434             break;
435       }
436     }
437 #ifdef WIN32
438     JOYCAPS jsCaps ;
439     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
440     int nbuttons = jsCaps.wNumButtons;
441     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
442 #else
443     int nbuttons = MAX_JOYSTICK_BUTTONS;
444 #endif
445         
446     int naxes = js->getNumAxes();
447     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
448     _joystick_bindings[i].naxes = naxes;
449     _joystick_bindings[i].nbuttons = nbuttons;
450
451     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
452
453                                 // Set up range arrays
454     float minRange[MAX_JOYSTICK_AXES];
455     float maxRange[MAX_JOYSTICK_AXES];
456     float center[MAX_JOYSTICK_AXES];
457
458                                 // Initialize with default values
459     js->getMinRange(minRange);
460     js->getMaxRange(maxRange);
461     js->getCenter(center);
462
463                                 // Allocate axes and buttons
464     _joystick_bindings[i].axes = new axis[naxes];
465     _joystick_bindings[i].buttons = new button[nbuttons];
466
467
468     //
469     // Initialize the axes.
470     //
471     vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
472     size_t nb_axes = axes.size();
473     int j;
474     for (j = 0; j < (int)nb_axes; j++) {
475       const SGPropertyNode * axis_node = axes[j];
476       const SGPropertyNode * num_node = axis_node->getChild("number");
477       size_t n_axis = axis_node->getIndex();
478       if (num_node != 0) {
479           n_axis = num_node->getIntValue(TGT_PLATFORM,n_axis);
480       }
481
482       axis &a = _joystick_bindings[i].axes[n_axis];
483
484       js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
485
486       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
487       minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
488       maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
489       center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
490
491       _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
492
493       // Initialize the virtual axis buttons.
494       _init_button(axis_node->getChild("low"), a.low, "low");
495       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
496       
497       _init_button(axis_node->getChild("high"), a.high, "high");
498       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
499       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
500       a.last_dt = 0.0;
501     }
502
503     //
504     // Initialize the buttons.
505     //
506     vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
507     char buf[32];
508     for (j = 0; (j < (int)buttons.size()) && (j < nbuttons); j++) {
509       const SGPropertyNode * button_node = buttons[j];
510       const SGPropertyNode * num_node = button_node->getChild("number");
511       size_t n_but = button_node->getIndex();
512       if (num_node != 0) {
513           n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
514       }
515       sprintf(buf, "%d", n_but);
516       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
517       _init_button(button_node,
518                    _joystick_bindings[i].buttons[n_but],
519                    buf);
520       
521       // get interval-sec property             
522       button &b = _joystick_bindings[i].buttons[n_but];
523       if (button_node != 0) {
524         b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
525         b.last_dt = 0.0;
526       }
527     }
528
529     js->setMinRange(minRange);
530     js->setMaxRange(maxRange);
531     js->setCenter(center);
532   }
533 }
534
535 // 
536 // Map of all known cursor names
537 // This used to contain all the Glut cursors, but those are
538 // not defined by other toolkits.  It now supports only the cursor
539 // images we actually use, in the interest of portability.  Someday,
540 // it would be cool to write an OpenGL cursor renderer, with the
541 // cursors defined as textures referenced in the property tree.  This
542 // list could then be eliminated. -Andy
543 //
544 struct {
545   const char * name;
546   int cursor;
547 } mouse_cursor_map[] = {
548   { "none", MOUSE_CURSOR_NONE },
549   { "inherit", MOUSE_CURSOR_POINTER },
550   { "wait", MOUSE_CURSOR_WAIT },
551   { "crosshair", MOUSE_CURSOR_CROSSHAIR },
552   { "left-right", MOUSE_CURSOR_LEFTRIGHT },
553   { 0, 0 }
554 };
555
556 void
557 FGInput::_init_mouse ()
558 {
559   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
560
561   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
562   if (mouse_nodes == 0) {
563     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
564     mouse_nodes = fgGetNode("/input/mice", true);
565   }
566
567   int j;
568   for (int i = 0; i < MAX_MICE; i++) {
569     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
570     mouse &m = _mouse_bindings[i];
571
572                                 // Grab node pointers
573     char buf[64];
574     sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
575     m.mode_node = fgGetNode(buf);
576     if (m.mode_node == NULL) {
577       m.mode_node = fgGetNode(buf, true);
578       m.mode_node->setIntValue(0);
579     }
580     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
581       sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
582       m.mouse_button_nodes[j] = fgGetNode(buf, true);
583       m.mouse_button_nodes[j]->setBoolValue(false);
584     }
585
586                                 // Read all the modes
587     m.nModes = mouse_node->getIntValue("mode-count", 1);
588     m.modes = new mouse_mode[m.nModes];
589
590     for (int j = 0; j < m.nModes; j++) {
591       int k;
592
593                                 // Read the mouse cursor for this mode
594       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
595       const char * cursor_name =
596         mode_node->getStringValue("cursor", "inherit");
597       m.modes[j].cursor = MOUSE_CURSOR_POINTER;
598       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
599         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
600           m.modes[j].cursor = mouse_cursor_map[k].cursor;
601           break;
602         }
603       }
604
605                                 // Read other properties for this mode
606       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
607       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
608
609                                 // Read the button bindings for this mode
610       m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
611       char buf[32];
612       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
613         sprintf(buf, "mouse button %d", k);
614         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
615         _init_button(mode_node->getChild("button", k),
616                      m.modes[j].buttons[k],
617                      buf);
618       }
619
620                                 // Read the axis bindings for this mode
621       _read_bindings(mode_node->getChild("x-axis", 0, true),
622                      m.modes[j].x_bindings,
623                      KEYMOD_NONE);
624       _read_bindings(mode_node->getChild("y-axis", 0, true),
625                      m.modes[j].y_bindings,
626                      KEYMOD_NONE);
627     }
628   }
629 }
630
631
632 void
633 FGInput::_init_button (const SGPropertyNode * node,
634                        button &b,
635                        const string name)
636 {       
637   if (node == 0) {
638     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
639   } else {
640     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
641     
642                 // Get the bindings for the button
643     _read_bindings(node, b.bindings, KEYMOD_NONE);
644   }
645 }
646
647
648 void
649 FGInput::_update_keyboard ()
650 {
651   // no-op
652 }
653
654
655 void
656 FGInput::_update_joystick (double dt)
657 {
658   int modifiers = KEYMOD_NONE;  // FIXME: any way to get the real ones?
659   int buttons;
660   // float js_val, diff;
661   float axis_values[MAX_JOYSTICK_AXES];
662
663   int i;
664   int j;
665
666   for ( i = 0; i < MAX_JOYSTICKS; i++) {
667
668     jsJoystick * js = _joystick_bindings[i].js;
669     if (js == 0 || js->notWorking())
670       continue;
671
672     js->read(&buttons, axis_values);
673
674
675                                 // Fire bindings for the axes.
676     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
677       axis &a = _joystick_bindings[i].axes[j];
678       
679                                 // Do nothing if the axis position
680                                 // is unchanged; only a change in
681                                 // position fires the bindings.
682       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
683 //      SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
684         SGPropertyNode node;
685         a.last_value = axis_values[j];
686 //      SG_LOG(SG_INPUT, SG_DEBUG, "There are "
687 //             << a.bindings[modifiers].size() << " bindings");
688         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
689           a.bindings[modifiers][k]->fire(axis_values[j]);
690       }
691      
692                                 // do we have to emulate axis buttons?
693       a.last_dt += dt;
694       if(a.last_dt >= a.interval_sec) {
695         if (a.low.bindings[modifiers].size())
696           _update_button(_joystick_bindings[i].axes[j].low,
697                          modifiers,
698                          axis_values[j] < a.low_threshold,
699                          -1, -1);
700       
701         if (a.high.bindings[modifiers].size())
702           _update_button(_joystick_bindings[i].axes[j].high,
703                          modifiers,
704                          axis_values[j] > a.high_threshold,
705                          -1, -1);
706          a.last_dt -= a.interval_sec;
707       }
708     }
709
710                                 // Fire bindings for the buttons.
711     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
712       button &b = _joystick_bindings[i].buttons[j];
713       b.last_dt += dt;
714       if(b.last_dt >= b.interval_sec) {
715         _update_button(_joystick_bindings[i].buttons[j],
716                        modifiers,
717                        (buttons & (1 << j)) > 0,
718                        -1, -1);
719         b.last_dt -= b.interval_sec;
720       }
721     }
722   }
723 }
724
725 void
726 FGInput::_update_mouse ( double dt )
727 {
728   mouse &m = _mouse_bindings[0];
729   int mode =  m.mode_node->getIntValue();
730   if (mode != m.current_mode) {
731     m.current_mode = mode;
732     m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
733     if (mode >= 0 && mode < m.nModes) {
734       fgSetMouseCursor(m.modes[mode].cursor);
735       m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
736       m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
737       fgWarpMouse(m.x, m.y);
738     } else {
739       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
740       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
741     }
742   }
743
744   if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
745       if ( m.x != m.save_x || m.y != m.save_y ) {
746           m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
747           fgSetMouseCursor(m.modes[mode].cursor);
748       } else {
749           m.timeout -= dt;
750           if ( m.timeout <= 0.0 ) {
751               fgSetMouseCursor(MOUSE_CURSOR_NONE);
752               m.timeout = 0.0;
753           }
754       }
755       m.save_x = m.x;
756       m.save_y = m.y;
757   }
758 }
759
760 void
761 FGInput::_update_button (button &b, int modifiers, bool pressed,
762                          int x, int y)
763 {
764   if (pressed) {
765                                 // The press event may be repeated.
766     if (!b.last_state || b.is_repeatable) {
767       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
768       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
769         b.bindings[modifiers][k]->fire(x, y);
770     }
771   } else {
772                                 // The release event is never repeated.
773     if (b.last_state) {
774       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
775       for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
776         b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
777     }
778   }
779           
780   b.last_state = pressed;
781 }  
782
783
784 void
785 FGInput::_read_bindings (const SGPropertyNode * node, 
786                          binding_list_t * binding_list,
787                          int modifiers)
788 {
789   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
790   vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
791   for (unsigned int i = 0; i < bindings.size(); i++) {
792     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
793            << bindings[i]->getStringValue("command"));
794     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
795   }
796
797                                 // Read nested bindings for modifiers
798   if (node->getChild("mod-up") != 0)
799     _read_bindings(node->getChild("mod-up"), binding_list,
800                    modifiers|KEYMOD_RELEASED);
801
802   if (node->getChild("mod-shift") != 0)
803     _read_bindings(node->getChild("mod-shift"), binding_list,
804                    modifiers|KEYMOD_SHIFT);
805
806   if (node->getChild("mod-ctrl") != 0)
807     _read_bindings(node->getChild("mod-ctrl"), binding_list,
808                    modifiers|KEYMOD_CTRL);
809
810   if (node->getChild("mod-alt") != 0)
811     _read_bindings(node->getChild("mod-alt"), binding_list,
812                    modifiers|KEYMOD_ALT);
813 }
814
815
816 const vector<FGBinding *> &
817 FGInput::_find_key_bindings (unsigned int k, int modifiers)
818 {
819   unsigned char kc = (unsigned char)k;
820   button &b = _key_bindings[k];
821
822                                 // Try it straight, first.
823   if (b.bindings[modifiers].size() > 0)
824     return b.bindings[modifiers];
825
826                                 // Alt-Gr is CTRL+ALT
827   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
828     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
829
830                                 // Try removing the control modifier
831                                 // for control keys.
832   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
833     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
834
835                                 // Try removing shift modifier 
836                                 // for upper case or any punctuation
837                                 // (since different keyboards will
838                                 // shift different punctuation types)
839   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
840     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
841
842                                 // Try removing alt modifier for
843                                 // high-bit characters.
844   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
845     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
846
847                                 // Give up and return the empty vector.
848   else
849     return b.bindings[modifiers];
850 }
851
852
853 \f
854 ////////////////////////////////////////////////////////////////////////
855 // Implementation of FGInput::button.
856 ////////////////////////////////////////////////////////////////////////
857
858 FGInput::button::button ()
859   : is_repeatable(false),
860     last_state(-1)
861 {
862 }
863
864 FGInput::button::~button ()
865 {
866                                 // FIXME: memory leak
867 //   for (int i = 0; i < KEYMOD_MAX; i++)
868 //     for (int j = 0; i < bindings[i].size(); j++)
869 //       delete bindings[i][j];
870 }
871
872
873 \f
874 ////////////////////////////////////////////////////////////////////////
875 // Implementation of FGInput::axis.
876 ////////////////////////////////////////////////////////////////////////
877
878 FGInput::axis::axis ()
879   : last_value(9999999),
880     tolerance(0.002),
881     low_threshold(-0.9),
882     high_threshold(0.9)
883 {
884 }
885
886 FGInput::axis::~axis ()
887 {
888 //   for (int i = 0; i < KEYMOD_MAX; i++)
889 //     for (int j = 0; i < bindings[i].size(); j++)
890 //       delete bindings[i][j];
891 }
892
893
894 \f
895 ////////////////////////////////////////////////////////////////////////
896 // Implementation of FGInput::joystick.
897 ////////////////////////////////////////////////////////////////////////
898
899 FGInput::joystick::joystick ()
900 {
901 }
902
903 FGInput::joystick::~joystick ()
904 {
905 //   delete js;
906   delete[] axes;
907   delete[] buttons;
908 }
909
910
911 \f
912 ////////////////////////////////////////////////////////////////////////
913 // Implementation of FGInput::mouse_mode
914 ////////////////////////////////////////////////////////////////////////
915
916 FGInput::mouse_mode::mouse_mode ()
917   : cursor(MOUSE_CURSOR_POINTER),
918     constrained(false),
919     pass_through(false),
920     buttons(0)
921 {
922 }
923
924 FGInput::mouse_mode::~mouse_mode ()
925 {
926                                 // FIXME: memory leak
927 //   for (int i = 0; i < KEYMOD_MAX; i++) {
928 //     int j;
929 //     for (j = 0; i < x_bindings[i].size(); j++)
930 //       delete bindings[i][j];
931 //     for (j = 0; j < y_bindings[i].size(); j++)
932 //       delete bindings[i][j];
933 //   }
934   delete [] buttons;
935 }
936
937
938 \f
939 ////////////////////////////////////////////////////////////////////////
940 // Implementation of FGInput::mouse
941 ////////////////////////////////////////////////////////////////////////
942
943 FGInput::mouse::mouse ()
944   : x(-1),
945     y(-1),
946     nModes(1),
947     current_mode(0),
948     modes(0)
949 {
950 }
951
952 FGInput::mouse::~mouse ()
953 {
954   delete [] modes;
955 }
956
957 ////////////////////////////////////////////////////////////////////////
958 // Implementation of OS callbacks.
959 ////////////////////////////////////////////////////////////////////////
960
961 void keyHandler(int key, int keymod, int mousex, int mousey)
962 {
963     if((keymod & KEYMOD_RELEASED) == 0)
964         if(puKeyboard(key, PU_DOWN))
965             return;
966
967     if(default_input)
968         default_input->doKey(key, keymod, mousex, mousey);
969 }
970
971 void mouseClickHandler(int button, int updown, int x, int y)
972 {
973     if(default_input)
974         default_input->doMouseClick(button, updown, x, y);
975 }
976
977 void mouseMotionHandler(int x, int y)
978 {
979     if (default_input != 0)
980         default_input->doMouseMotion(x, y);
981 }