]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
Restore the hard-coded HUD keybindings that I inadvertently killed
[flightgear.git] / src / Input / input.cxx
1 // input.cxx -- handle user input from various sources.
2 //
3 // Written by David Megginson, started May 2001.
4 //
5 // Copyright (C) 2001 David Megginson, david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #ifdef HAVE_WINDOWS_H
28 #  include <windows.h>                     
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include <math.h>
34 #include <ctype.h>
35
36 #include STL_FSTREAM
37 #include STL_STRING
38 #include <vector>
39
40 #include <simgear/compiler.h>
41
42 #include <simgear/constants.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/props/props.hxx>
45
46 #include <Aircraft/aircraft.hxx>
47 #include <Autopilot/xmlauto.hxx>
48 #include <Cockpit/hud.hxx>
49 #include <Cockpit/panel.hxx>
50 #include <Cockpit/panel_io.hxx>
51 #include <GUI/gui.h>
52 #include <Model/panelnode.hxx>
53
54 #include <Main/globals.hxx>
55 #include <Main/fg_props.hxx>
56
57 #include "input.hxx"
58
59 SG_USING_STD(ifstream);
60 SG_USING_STD(string);
61 SG_USING_STD(vector);
62
63 void mouseClickHandler(int button, int updown, int x, int y);
64 void mouseMotionHandler(int x, int y);
65 void keyHandler(int key, int keymod, int mousex, int mousey);
66
67 \f
68 ////////////////////////////////////////////////////////////////////////
69 // Local variables.
70 ////////////////////////////////////////////////////////////////////////
71
72 static FGInput * default_input = 0;
73
74
75 \f
76 ////////////////////////////////////////////////////////////////////////
77 // Implementation of FGBinding.
78 ////////////////////////////////////////////////////////////////////////
79
80 FGBinding::FGBinding ()
81   : _command(0),
82     _arg(new SGPropertyNode),
83     _setting(0)
84 {
85 }
86
87 FGBinding::FGBinding (const SGPropertyNode * node)
88   : _command(0),
89     _arg(0),
90     _setting(0)
91 {
92   read(node);
93 }
94
95 void
96 FGBinding::read (const SGPropertyNode * node)
97 {
98   const SGPropertyNode * conditionNode = node->getChild("condition");
99   if (conditionNode != 0)
100     setCondition(sgReadCondition(globals->get_props(), conditionNode));
101
102   _command_name = node->getStringValue("command", "");
103   if (_command_name.empty()) {
104     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
105     _command = 0;
106     return;
107   }
108
109   _arg = new SGPropertyNode;
110   _setting = 0;
111   copyProperties(node, _arg);  // FIXME: don't use whole node!!!
112 }
113
114 void
115 FGBinding::fire () const
116 {
117   if (test()) {
118     if (_command == 0)
119       _command = globals->get_commands()->getCommand(_command_name);
120     if (_command == 0) {
121       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
122     } else if (!(*_command)(_arg)) {
123       SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
124              << _command_name);
125     }
126   }
127 }
128
129 void
130 FGBinding::fire (double offset, double max) const
131 {
132   if (test()) {
133     _arg->setDoubleValue("offset", offset/max);
134     fire();
135   }
136 }
137
138 void
139 FGBinding::fire (double setting) const
140 {
141   if (test()) {
142                                 // A value is automatically added to
143                                 // the args
144     if (_setting == 0)          // save the setting node for efficiency
145       _setting = _arg->getChild("setting", 0, true);
146     _setting->setDoubleValue(setting);
147     fire();
148   }
149 }
150
151
152 \f
153 ////////////////////////////////////////////////////////////////////////
154 // Implementation of FGInput.
155 ////////////////////////////////////////////////////////////////////////
156
157
158 FGInput::FGInput ()
159 {
160     if (default_input == 0)
161         default_input = this;
162 }
163
164 FGInput::~FGInput ()
165 {
166     if (default_input == this)
167         default_input = 0;
168 }
169
170 void
171 FGInput::init ()
172 {
173   _init_keyboard();
174   _init_joystick();
175   _init_mouse();
176
177   fgRegisterKeyHandler(keyHandler);
178   fgRegisterMouseClickHandler(mouseClickHandler);
179   fgRegisterMouseMotionHandler(mouseMotionHandler);
180 }
181
182 void 
183 FGInput::update (double dt)
184 {
185   _update_keyboard();
186   _update_joystick(dt);
187   _update_mouse();
188 }
189
190 void
191 FGInput::suspend ()
192 {
193     // NO-OP
194 }
195
196 void
197 FGInput::resume ()
198 {
199     // NO-OP
200 }
201
202 bool
203 FGInput::is_suspended () const
204 {
205     return false;
206 }
207
208 void
209 FGInput::makeDefault (bool status)
210 {
211     if (status)
212         default_input = this;
213     else if (default_input == this)
214         default_input = 0;
215 }
216
217 void
218 FGInput::doKey (int k, int modifiers, int x, int y)
219 {
220                                 // Sanity check.
221   if (k < 0 || k >= MAX_KEYS) {
222     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
223     return;
224   }
225
226   button &b = _key_bindings[k];
227
228                                 // Key pressed.
229   if (modifiers&KEYMOD_RELEASED == 0) {
230     SG_LOG( SG_INPUT, SG_DEBUG, "User pressed key " << k
231             << " with modifiers " << modifiers );
232     if (!b.last_state || b.is_repeatable) {
233       const binding_list_t &bindings =
234         _find_key_bindings(k, modifiers);
235       int max = bindings.size();
236       if (max > 0) {
237         for (int i = 0; i < max; i++)
238           bindings[i]->fire();
239         return;
240       }
241     }
242   }
243
244                                 // Key released.
245   else {
246     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
247            << " with modifiers " << modifiers);
248     if (b.last_state) {
249       const binding_list_t &bindings =
250         _find_key_bindings(k, modifiers);
251       int max = bindings.size();
252       if (max > 0) {
253         for (int i = 0; i < max; i++)
254           bindings[i]->fire();
255         return;
256       }
257     }
258   }
259                                 // Use the old, default actions.
260   SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
261
262   if (modifiers & KEYMOD_RELEASED)
263     return;
264   
265   // Hard-coded legacy key handlers.
266   // These need to be turned into FGCommand bindings and mapped via
267   // the input configuration...
268   switch (k) {
269   case 'H':
270     HUD_brightkey( true );
271     break;
272   case 'h':
273     HUD_masterswitch( true );
274     break;
275   case 'I':
276     fgHUDInit2(&current_aircraft); // Minimal Hud
277     break;
278   case 'i':
279     fgHUDInit(&current_aircraft);  // normal HUD
280     break;
281   }
282 }
283
284 void
285 FGInput::doMouseClick (int b, int updown, int x, int y)
286 {
287   int modifiers = fgGetKeyModifiers();
288
289   mouse &m = _mouse_bindings[0];
290   mouse_mode &mode = m.modes[m.current_mode];
291
292                                 // Let the property manager know.
293   if (b >= 0 && b < MAX_MOUSE_BUTTONS)
294     m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
295
296                                 // Pass on to PUI and the panel if
297                                 // requested, and return if one of
298                                 // them consumes the event.
299   if (mode.pass_through) {
300     if (puMouse(b, updown, x, y))
301       return;
302     else if ((globals->get_current_panel() != 0) &&
303              globals->get_current_panel()->getVisibility() &&
304              globals->get_current_panel()->doMouseAction(b, updown, x, y))
305       return;
306     else if (fgHandle3DPanelMouseEvent(b, updown, x, y))
307       return;
308   }
309
310                                 // OK, PUI and the panel didn't want the click
311   if (b >= MAX_MOUSE_BUTTONS) {
312     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
313            << " where only " << MAX_MOUSE_BUTTONS << " expected");
314     return;
315   }
316
317   _update_button(m.modes[m.current_mode].buttons[b], modifiers, 0 != updown, x, y);
318 }
319
320 void
321 FGInput::doMouseMotion (int x, int y)
322 {
323   // Don't call fgGetKeyModifiers() here, until we are using a
324   // toolkit that supports getting the mods from outside a key
325   // callback.  Glut doesn't.
326   int modifiers = KEYMOD_NONE;
327
328   int xsize = fgGetInt("/sim/startup/xsize", 800);
329   int ysize = fgGetInt("/sim/startup/ysize", 600);
330   mouse &m = _mouse_bindings[0];
331   if (m.current_mode < 0 || m.current_mode >= m.nModes)
332     return;
333   mouse_mode &mode = m.modes[m.current_mode];
334
335                                 // Pass on to PUI if requested, and return
336                                 // if PUI consumed the event.
337   if (mode.pass_through && puMouse(x, y))
338     return;
339
340                                 // OK, PUI didn't want the event,
341                                 // so we can play with it.
342   if (x != m.x) {
343     int delta = x - m.x;
344     for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
345       mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
346   }
347   if (y != m.y) {
348     int delta = y - m.y;
349     for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
350       mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
351   }
352
353                                 // Constrain the mouse if requested
354   if (mode.constrained) {
355     bool need_warp = false;
356     if (x <= 0) {
357       x = xsize - 2;
358       need_warp = true;
359     } else if (x >= (xsize-1)) {
360       x = 1;
361       need_warp = true;
362     }
363
364     if (y <= 0) {
365       y = ysize - 2;
366       need_warp = true;
367     } else if (y >= (ysize-1)) {
368       y = 1;
369       need_warp = true;
370     }
371
372     if (need_warp)
373       fgWarpMouse(x, y);
374   }
375   m.x = x;
376   m.y = y;
377 }
378
379 void
380 FGInput::_init_keyboard ()
381 {
382   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings");
383   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
384   if (key_nodes == 0) {
385     SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!");
386     key_nodes = fgGetNode("/input/keyboard", true);
387   }
388   
389   vector<SGPropertyNode_ptr> keys = key_nodes->getChildren("key");
390   for (unsigned int i = 0; i < keys.size(); i++) {
391     int index = keys[i]->getIndex();
392     SG_LOG(SG_INPUT, SG_DEBUG, "Binding key " << index);
393
394     _key_bindings[index].bindings->clear();
395     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
396     _read_bindings(keys[i], _key_bindings[index].bindings, KEYMOD_NONE);
397   }
398 }
399
400
401 void
402 FGInput::_init_joystick ()
403 {
404                                 // TODO: zero the old bindings first.
405   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
406   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
407   if (js_nodes == 0) {
408     SG_LOG(SG_INPUT, SG_WARN, "No joystick bindings (/input/joysticks)!!");
409     js_nodes = fgGetNode("/input/joysticks", true);
410   }
411
412   for (int i = 0; i < MAX_JOYSTICKS; i++) {
413     SGPropertyNode_ptr js_node = js_nodes->getChild("js", i);
414     if (js_node == 0) {
415       SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for joystick " << i);
416       js_node = js_nodes->getChild("js", i, true);
417     }
418     jsJoystick * js = new jsJoystick(i);
419     _joystick_bindings[i].js = js;
420     if (js->notWorking()) {
421       SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found");
422       continue;
423     } else {
424       bool found_js = false;
425       const char * name = js->getName();
426       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \""
427              << name << '"');
428       vector<SGPropertyNode_ptr> nodes = js_nodes->getChildren("js-named");
429       for (unsigned int i = 0; i < nodes.size(); i++) {
430         SGPropertyNode_ptr node = nodes[i];
431         vector<SGPropertyNode_ptr> name_nodes = node->getChildren("name");
432         for (unsigned int j = 0; j < name_nodes.size(); j++) {
433             const char * js_name = name_nodes[j]->getStringValue();
434             SG_LOG(SG_INPUT, SG_INFO, "  Trying \"" << js_name << '"');
435             if (!strcmp(js_name, name)) {
436                 SG_LOG(SG_INPUT, SG_INFO, "  Found bindings");
437                 js_node = node;
438                 found_js = true;
439                 break;
440             }
441         }
442         if (found_js)
443             break;
444       }
445     }
446 #ifdef WIN32
447     JOYCAPS jsCaps ;
448     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
449     int nbuttons = jsCaps.wNumButtons;
450     if (nbuttons > MAX_JOYSTICK_BUTTONS) nbuttons = MAX_JOYSTICK_BUTTONS;
451 #else
452     int nbuttons = MAX_JOYSTICK_BUTTONS;
453 #endif
454         
455     int naxes = js->getNumAxes();
456     if (naxes > MAX_JOYSTICK_AXES) naxes = MAX_JOYSTICK_AXES;
457     _joystick_bindings[i].naxes = naxes;
458     _joystick_bindings[i].nbuttons = nbuttons;
459
460     SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick " << i);
461
462                                 // Set up range arrays
463     float minRange[MAX_JOYSTICK_AXES];
464     float maxRange[MAX_JOYSTICK_AXES];
465     float center[MAX_JOYSTICK_AXES];
466
467                                 // Initialize with default values
468     js->getMinRange(minRange);
469     js->getMaxRange(maxRange);
470     js->getCenter(center);
471
472                                 // Allocate axes and buttons
473     _joystick_bindings[i].axes = new axis[naxes];
474     _joystick_bindings[i].buttons = new button[nbuttons];
475
476
477     //
478     // Initialize the axes.
479     //
480     int j;
481     for (j = 0; j < naxes; j++) {
482       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
483       if (axis_node == 0) {
484         SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
485         axis_node = js_node->getChild("axis", j, true);
486       }
487       
488       axis &a = _joystick_bindings[i].axes[j];
489
490       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
491
492       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
493       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
494       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
495       center[j] = axis_node->getDoubleValue("center", center[j]);
496
497       _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
498
499       // Initialize the virtual axis buttons.
500       _init_button(axis_node->getChild("low"), a.low, "low");
501       a.low_threshold = axis_node->getDoubleValue("low-threshold", -0.9);
502       
503       _init_button(axis_node->getChild("high"), a.high, "high");
504       a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9);
505       a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0);
506       a.last_dt = 0.0;
507     }
508
509     //
510     // Initialize the buttons.
511     //
512     char buf[32];
513     for (j = 0; j < nbuttons; j++) {
514       sprintf(buf, "%d", j);
515       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
516       _init_button(js_node->getChild("button", j),
517                    _joystick_bindings[i].buttons[j],
518                    buf);
519       
520       // get interval-sec property             
521       button &b = _joystick_bindings[i].buttons[j];
522       const SGPropertyNode * button_node = js_node->getChild("button", j);
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 ()
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     if (mode >= 0 && mode < m.nModes) {
733       fgSetMouseCursor(m.modes[mode].cursor);
734       m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
735       m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
736       fgWarpMouse(m.x, m.y);
737     } else {
738       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
739       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
740     }
741   }
742 }
743
744 void
745 FGInput::_update_button (button &b, int modifiers, bool pressed,
746                          int x, int y)
747 {
748   if (pressed) {
749                                 // The press event may be repeated.
750     if (!b.last_state || b.is_repeatable) {
751       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
752       for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
753         b.bindings[modifiers][k]->fire(x, y);
754     }
755   } else {
756                                 // The release event is never repeated.
757     if (b.last_state) {
758       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
759       for (unsigned int k = 0; k < b.bindings[modifiers|KEYMOD_RELEASED].size(); k++)
760         b.bindings[modifiers|KEYMOD_RELEASED][k]->fire(x, y);
761     }
762   }
763           
764   b.last_state = pressed;
765 }  
766
767
768 void
769 FGInput::_read_bindings (const SGPropertyNode * node, 
770                          binding_list_t * binding_list,
771                          int modifiers)
772 {
773   SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
774   vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
775   for (unsigned int i = 0; i < bindings.size(); i++) {
776     SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
777            << bindings[i]->getStringValue("command"));
778     binding_list[modifiers].push_back(new FGBinding(bindings[i]));
779   }
780
781                                 // Read nested bindings for modifiers
782   if (node->getChild("mod-up") != 0)
783     _read_bindings(node->getChild("mod-up"), binding_list,
784                    modifiers|KEYMOD_RELEASED);
785
786   if (node->getChild("mod-shift") != 0)
787     _read_bindings(node->getChild("mod-shift"), binding_list,
788                    modifiers|KEYMOD_SHIFT);
789
790   if (node->getChild("mod-ctrl") != 0)
791     _read_bindings(node->getChild("mod-ctrl"), binding_list,
792                    modifiers|KEYMOD_CTRL);
793
794   if (node->getChild("mod-alt") != 0)
795     _read_bindings(node->getChild("mod-alt"), binding_list,
796                    modifiers|KEYMOD_ALT);
797 }
798
799
800 const vector<FGBinding *> &
801 FGInput::_find_key_bindings (unsigned int k, int modifiers)
802 {
803   unsigned char kc = (unsigned char)k;
804   button &b = _key_bindings[k];
805
806                                 // Try it straight, first.
807   if (b.bindings[modifiers].size() > 0)
808     return b.bindings[modifiers];
809
810                                 // Alt-Gr is CTRL+ALT
811   else if (modifiers&(KEYMOD_CTRL|KEYMOD_ALT))
812     return _find_key_bindings(k, modifiers&~(KEYMOD_CTRL|KEYMOD_ALT));
813
814                                 // Try removing the control modifier
815                                 // for control keys.
816   else if ((modifiers&KEYMOD_CTRL) && iscntrl(kc))
817     return _find_key_bindings(k, modifiers&~KEYMOD_CTRL);
818
819                                 // Try removing shift modifier 
820                                 // for upper case or any punctuation
821                                 // (since different keyboards will
822                                 // shift different punctuation types)
823   else if ((modifiers&KEYMOD_SHIFT) && (isupper(kc) || ispunct(kc)))
824     return _find_key_bindings(k, modifiers&~KEYMOD_SHIFT);
825
826                                 // Try removing alt modifier for
827                                 // high-bit characters.
828   else if ((modifiers&KEYMOD_ALT) && k >= 128 && k < 256)
829     return _find_key_bindings(k, modifiers&~KEYMOD_ALT);
830
831                                 // Give up and return the empty vector.
832   else
833     return b.bindings[modifiers];
834 }
835
836
837 \f
838 ////////////////////////////////////////////////////////////////////////
839 // Implementation of FGInput::button.
840 ////////////////////////////////////////////////////////////////////////
841
842 FGInput::button::button ()
843   : is_repeatable(false),
844     last_state(-1)
845 {
846 }
847
848 FGInput::button::~button ()
849 {
850                                 // FIXME: memory leak
851 //   for (int i = 0; i < KEYMOD_MAX; i++)
852 //     for (int j = 0; i < bindings[i].size(); j++)
853 //       delete bindings[i][j];
854 }
855
856
857 \f
858 ////////////////////////////////////////////////////////////////////////
859 // Implementation of FGInput::axis.
860 ////////////////////////////////////////////////////////////////////////
861
862 FGInput::axis::axis ()
863   : last_value(9999999),
864     tolerance(0.002),
865     low_threshold(-0.9),
866     high_threshold(0.9)
867 {
868 }
869
870 FGInput::axis::~axis ()
871 {
872 //   for (int i = 0; i < KEYMOD_MAX; i++)
873 //     for (int j = 0; i < bindings[i].size(); j++)
874 //       delete bindings[i][j];
875 }
876
877
878 \f
879 ////////////////////////////////////////////////////////////////////////
880 // Implementation of FGInput::joystick.
881 ////////////////////////////////////////////////////////////////////////
882
883 FGInput::joystick::joystick ()
884 {
885 }
886
887 FGInput::joystick::~joystick ()
888 {
889 //   delete js;
890   delete[] axes;
891   delete[] buttons;
892 }
893
894
895 \f
896 ////////////////////////////////////////////////////////////////////////
897 // Implementation of FGInput::mouse_mode
898 ////////////////////////////////////////////////////////////////////////
899
900 FGInput::mouse_mode::mouse_mode ()
901   : cursor(MOUSE_CURSOR_POINTER),
902     constrained(false),
903     pass_through(false),
904     buttons(0)
905 {
906 }
907
908 FGInput::mouse_mode::~mouse_mode ()
909 {
910                                 // FIXME: memory leak
911 //   for (int i = 0; i < KEYMOD_MAX; i++) {
912 //     int j;
913 //     for (j = 0; i < x_bindings[i].size(); j++)
914 //       delete bindings[i][j];
915 //     for (j = 0; j < y_bindings[i].size(); j++)
916 //       delete bindings[i][j];
917 //   }
918   delete [] buttons;
919 }
920
921
922 \f
923 ////////////////////////////////////////////////////////////////////////
924 // Implementation of FGInput::mouse
925 ////////////////////////////////////////////////////////////////////////
926
927 FGInput::mouse::mouse ()
928   : x(-1),
929     y(-1),
930     nModes(1),
931     current_mode(0),
932     modes(0)
933 {
934 }
935
936 FGInput::mouse::~mouse ()
937 {
938   delete [] modes;
939 }
940
941 ////////////////////////////////////////////////////////////////////////
942 // Implementation of OS callbacks.
943 ////////////////////////////////////////////////////////////////////////
944
945 void keyHandler(int key, int keymod, int mousex, int mousey)
946 {
947     if((keymod & KEYMOD_RELEASED) == 0)
948         if(puKeyboard(key, PU_DOWN))
949             return;
950
951     if(default_input)
952         default_input->doKey(key, keymod, mousex, mousey);
953 }
954
955 void mouseClickHandler(int button, int updown, int x, int y)
956 {
957     if(default_input)
958         default_input->doMouseClick(button, updown, x, y);
959 }
960
961 void mouseMotionHandler(int x, int y)
962 {
963     if (default_input != 0)
964         default_input->doMouseMotion(x, y);
965 }