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