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