]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
Migrate FlightGear code to use "#include SG_GL*" defined in
[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       if (n_axis >= (size_t)naxes) {
483           SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
484           continue;
485       }
486       axis &a = _joystick_bindings[i].axes[n_axis];
487
488       js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
489
490       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
491       minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
492       maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
493       center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
494
495       _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
496
497       // Initialize the virtual axis buttons.
498       _init_button(axis_node->getChild("low"), a.low, "low");
499       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
500       
501       _init_button(axis_node->getChild("high"), a.high, "high");
502       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
503       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
504       a.last_dt = 0.0;
505     }
506
507     //
508     // Initialize the buttons.
509     //
510     vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
511     char buf[32];
512     for (j = 0; (j < (int)buttons.size()) && (j < nbuttons); j++) {
513       const SGPropertyNode * button_node = buttons[j];
514       const SGPropertyNode * num_node = button_node->getChild("number");
515       size_t n_but = button_node->getIndex();
516       if (num_node != 0) {
517           n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
518       }
519
520       if (n_but >= (size_t)nbuttons) {
521           SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
522           continue;
523       }
524
525       sprintf(buf, "%d", n_but);
526       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
527       _init_button(button_node,
528                    _joystick_bindings[i].buttons[n_but],
529                    buf);
530       
531       // get interval-sec property             
532       button &b = _joystick_bindings[i].buttons[n_but];
533       if (button_node != 0) {
534         b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
535         b.last_dt = 0.0;
536       }
537     }
538
539     js->setMinRange(minRange);
540     js->setMaxRange(maxRange);
541     js->setCenter(center);
542   }
543 }
544
545 // 
546 // Map of all known cursor names
547 // This used to contain all the Glut cursors, but those are
548 // not defined by other toolkits.  It now supports only the cursor
549 // images we actually use, in the interest of portability.  Someday,
550 // it would be cool to write an OpenGL cursor renderer, with the
551 // cursors defined as textures referenced in the property tree.  This
552 // list could then be eliminated. -Andy
553 //
554 struct {
555   const char * name;
556   int cursor;
557 } mouse_cursor_map[] = {
558   { "none", MOUSE_CURSOR_NONE },
559   { "inherit", MOUSE_CURSOR_POINTER },
560   { "wait", MOUSE_CURSOR_WAIT },
561   { "crosshair", MOUSE_CURSOR_CROSSHAIR },
562   { "left-right", MOUSE_CURSOR_LEFTRIGHT },
563   { 0, 0 }
564 };
565
566 void
567 FGInput::_init_mouse ()
568 {
569   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
570
571   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
572   if (mouse_nodes == 0) {
573     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
574     mouse_nodes = fgGetNode("/input/mice", true);
575   }
576
577   int j;
578   for (int i = 0; i < MAX_MICE; i++) {
579     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
580     mouse &m = _mouse_bindings[i];
581
582                                 // Grab node pointers
583     char buf[64];
584     sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
585     m.mode_node = fgGetNode(buf);
586     if (m.mode_node == NULL) {
587       m.mode_node = fgGetNode(buf, true);
588       m.mode_node->setIntValue(0);
589     }
590     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
591       sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
592       m.mouse_button_nodes[j] = fgGetNode(buf, true);
593       m.mouse_button_nodes[j]->setBoolValue(false);
594     }
595
596                                 // Read all the modes
597     m.nModes = mouse_node->getIntValue("mode-count", 1);
598     m.modes = new mouse_mode[m.nModes];
599
600     for (int j = 0; j < m.nModes; j++) {
601       int k;
602
603                                 // Read the mouse cursor for this mode
604       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
605       const char * cursor_name =
606         mode_node->getStringValue("cursor", "inherit");
607       m.modes[j].cursor = MOUSE_CURSOR_POINTER;
608       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
609         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
610           m.modes[j].cursor = mouse_cursor_map[k].cursor;
611           break;
612         }
613       }
614
615                                 // Read other properties for this mode
616       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
617       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
618
619                                 // Read the button bindings for this mode
620       m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
621       char buf[32];
622       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
623         sprintf(buf, "mouse button %d", k);
624         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
625         _init_button(mode_node->getChild("button", k),
626                      m.modes[j].buttons[k],
627                      buf);
628       }
629
630                                 // Read the axis bindings for this mode
631       _read_bindings(mode_node->getChild("x-axis", 0, true),
632                      m.modes[j].x_bindings,
633                      KEYMOD_NONE);
634       _read_bindings(mode_node->getChild("y-axis", 0, true),
635                      m.modes[j].y_bindings,
636                      KEYMOD_NONE);
637     }
638   }
639 }
640
641
642 void
643 FGInput::_init_button (const SGPropertyNode * node,
644                        button &b,
645                        const string name)
646 {       
647   if (node == 0) {
648     SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
649   } else {
650     b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
651     
652                 // Get the bindings for the button
653     _read_bindings(node, b.bindings, KEYMOD_NONE);
654   }
655 }
656
657
658 void
659 FGInput::_update_keyboard ()
660 {
661   // no-op
662 }
663
664
665 void
666 FGInput::_update_joystick (double dt)
667 {
668   int modifiers = KEYMOD_NONE;  // FIXME: any way to get the real ones?
669   int buttons;
670   // float js_val, diff;
671   float axis_values[MAX_JOYSTICK_AXES];
672
673   int i;
674   int j;
675
676   for ( i = 0; i < MAX_JOYSTICKS; i++) {
677
678     jsJoystick * js = _joystick_bindings[i].js;
679     if (js == 0 || js->notWorking())
680       continue;
681
682     js->read(&buttons, axis_values);
683
684
685                                 // Fire bindings for the axes.
686     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
687       axis &a = _joystick_bindings[i].axes[j];
688       
689                                 // Do nothing if the axis position
690                                 // is unchanged; only a change in
691                                 // position fires the bindings.
692       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
693 //      SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
694         SGPropertyNode node;
695         a.last_value = axis_values[j];
696 //      SG_LOG(SG_INPUT, SG_DEBUG, "There are "
697 //             << a.bindings[modifiers].size() << " bindings");
698         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
699           a.bindings[modifiers][k]->fire(axis_values[j]);
700       }
701      
702                                 // do we have to emulate axis buttons?
703       a.last_dt += dt;
704       if(a.last_dt >= a.interval_sec) {
705         if (a.low.bindings[modifiers].size())
706           _update_button(_joystick_bindings[i].axes[j].low,
707                          modifiers,
708                          axis_values[j] < a.low_threshold,
709                          -1, -1);
710       
711         if (a.high.bindings[modifiers].size())
712           _update_button(_joystick_bindings[i].axes[j].high,
713                          modifiers,
714                          axis_values[j] > a.high_threshold,
715                          -1, -1);
716          a.last_dt -= a.interval_sec;
717       }
718     }
719
720                                 // Fire bindings for the buttons.
721     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
722       button &b = _joystick_bindings[i].buttons[j];
723       b.last_dt += dt;
724       if(b.last_dt >= b.interval_sec) {
725         _update_button(_joystick_bindings[i].buttons[j],
726                        modifiers,
727                        (buttons & (1 << j)) > 0,
728                        -1, -1);
729         b.last_dt -= b.interval_sec;
730       }
731     }
732   }
733 }
734
735 void
736 FGInput::_update_mouse ( double dt )
737 {
738   mouse &m = _mouse_bindings[0];
739   int mode =  m.mode_node->getIntValue();
740   if (mode != m.current_mode) {
741     m.current_mode = mode;
742     m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
743     if (mode >= 0 && mode < m.nModes) {
744       fgSetMouseCursor(m.modes[mode].cursor);
745       m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
746       m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
747       fgWarpMouse(m.x, m.y);
748     } else {
749       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
750       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
751     }
752   }
753
754   if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
755       if ( m.x != m.save_x || m.y != m.save_y ) {
756           m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
757           fgSetMouseCursor(m.modes[mode].cursor);
758       } else {
759           m.timeout -= dt;
760           if ( m.timeout <= 0.0 ) {
761               fgSetMouseCursor(MOUSE_CURSOR_NONE);
762               m.timeout = 0.0;
763           }
764       }
765       m.save_x = m.x;
766       m.save_y = m.y;
767   }
768 }
769
770 void
771 FGInput::_update_button (button &b, int modifiers, bool pressed,
772                          int x, int y)
773 {
774   if (pressed) {
775                                 // The press event may be repeated.
776     if (!b.last_state || b.is_repeatable) {
777       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
778       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
779         b.bindings[modifiers][k]->fire(x, y);
780     }
781   } else {
782                                 // The release event is never repeated.
783     if (b.last_state) {
784       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
785       for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
786         b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
787     }
788   }
789           
790   b.last_state = pressed;
791 }  
792
793
794 void
795 FGInput::_read_bindings (const SGPropertyNode * node, 
796                          binding_list_t * binding_list,
797                          int modifiers)
798 {
799   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
800   vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
801   for (unsigned int i = 0; i < bindings.size(); i++) {
802     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
803            << bindings[i]->getStringValue("command"));
804     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
805   }
806
807                                 // Read nested bindings for modifiers
808   if (node->getChild("mod-up") != 0)
809     _read_bindings(node->getChild("mod-up"), binding_list,
810                    modifiers|KEYMOD_RELEASED);
811
812   if (node->getChild("mod-shift") != 0)
813     _read_bindings(node->getChild("mod-shift"), binding_list,
814                    modifiers|KEYMOD_SHIFT);
815
816   if (node->getChild("mod-ctrl") != 0)
817     _read_bindings(node->getChild("mod-ctrl"), binding_list,
818                    modifiers|KEYMOD_CTRL);
819
820   if (node->getChild("mod-alt") != 0)
821     _read_bindings(node->getChild("mod-alt"), binding_list,
822                    modifiers|KEYMOD_ALT);
823 }
824
825
826 const vector<FGBinding *> &
827 FGInput::_find_key_bindings (unsigned int k, int modifiers)
828 {
829   unsigned char kc = (unsigned char)k;
830   button &b = _key_bindings[k];
831
832                                 // Try it straight, first.
833   if (b.bindings[modifiers].size() > 0)
834     return b.bindings[modifiers];
835
836                                 // Alt-Gr is CTRL+ALT
837   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
838     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
839
840                                 // Try removing the control modifier
841                                 // for control keys.
842   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
843     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
844
845                                 // Try removing shift modifier 
846                                 // for upper case or any punctuation
847                                 // (since different keyboards will
848                                 // shift different punctuation types)
849   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
850     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
851
852                                 // Try removing alt modifier for
853                                 // high-bit characters.
854   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
855     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
856
857                                 // Give up and return the empty vector.
858   else
859     return b.bindings[modifiers];
860 }
861
862
863 \f
864 ////////////////////////////////////////////////////////////////////////
865 // Implementation of FGInput::button.
866 ////////////////////////////////////////////////////////////////////////
867
868 FGInput::button::button ()
869   : is_repeatable(false),
870     last_state(-1)
871 {
872 }
873
874 FGInput::button::~button ()
875 {
876                                 // FIXME: memory leak
877 //   for (int i = 0; i < KEYMOD_MAX; i++)
878 //     for (int j = 0; i < bindings[i].size(); j++)
879 //       delete bindings[i][j];
880 }
881
882
883 \f
884 ////////////////////////////////////////////////////////////////////////
885 // Implementation of FGInput::axis.
886 ////////////////////////////////////////////////////////////////////////
887
888 FGInput::axis::axis ()
889   : last_value(9999999),
890     tolerance(0.002),
891     low_threshold(-0.9),
892     high_threshold(0.9)
893 {
894 }
895
896 FGInput::axis::~axis ()
897 {
898 //   for (int i = 0; i < KEYMOD_MAX; i++)
899 //     for (int j = 0; i < bindings[i].size(); j++)
900 //       delete bindings[i][j];
901 }
902
903
904 \f
905 ////////////////////////////////////////////////////////////////////////
906 // Implementation of FGInput::joystick.
907 ////////////////////////////////////////////////////////////////////////
908
909 FGInput::joystick::joystick ()
910 {
911 }
912
913 FGInput::joystick::~joystick ()
914 {
915 //   delete js;
916   delete[] axes;
917   delete[] buttons;
918 }
919
920
921 \f
922 ////////////////////////////////////////////////////////////////////////
923 // Implementation of FGInput::mouse_mode
924 ////////////////////////////////////////////////////////////////////////
925
926 FGInput::mouse_mode::mouse_mode ()
927   : cursor(MOUSE_CURSOR_POINTER),
928     constrained(false),
929     pass_through(false),
930     buttons(0)
931 {
932 }
933
934 FGInput::mouse_mode::~mouse_mode ()
935 {
936                                 // FIXME: memory leak
937 //   for (int i = 0; i < KEYMOD_MAX; i++) {
938 //     int j;
939 //     for (j = 0; i < x_bindings[i].size(); j++)
940 //       delete bindings[i][j];
941 //     for (j = 0; j < y_bindings[i].size(); j++)
942 //       delete bindings[i][j];
943 //   }
944   delete [] buttons;
945 }
946
947
948 \f
949 ////////////////////////////////////////////////////////////////////////
950 // Implementation of FGInput::mouse
951 ////////////////////////////////////////////////////////////////////////
952
953 FGInput::mouse::mouse ()
954   : x(-1),
955     y(-1),
956     nModes(1),
957     current_mode(0),
958     modes(0)
959 {
960 }
961
962 FGInput::mouse::~mouse ()
963 {
964   delete [] modes;
965 }
966
967 ////////////////////////////////////////////////////////////////////////
968 // Implementation of OS callbacks.
969 ////////////////////////////////////////////////////////////////////////
970
971 void keyHandler(int key, int keymod, int mousex, int mousey)
972 {
973     if((keymod & KEYMOD_RELEASED) == 0)
974         if(puKeyboard(key, PU_DOWN))
975             return;
976
977     if(default_input)
978         default_input->doKey(key, keymod, mousex, mousey);
979 }
980
981 void mouseClickHandler(int button, int updown, int x, int y)
982 {
983     if(default_input)
984         default_input->doMouseClick(button, updown, x, y);
985 }
986
987 void mouseMotionHandler(int x, int y)
988 {
989     if (default_input != 0)
990         default_input->doMouseMotion(x, y);
991 }