]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
Fixed a problem where all hardcoded keybindings were being double executed.
[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/constants.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/misc/props.hxx>
43
44 #include <Aircraft/aircraft.hxx>
45 #include <Autopilot/auto_gui.hxx>
46 #include <Autopilot/newauto.hxx>
47 #include <Cockpit/hud.hxx>
48 #include <Cockpit/panel.hxx>
49 #include <Cockpit/panel_io.hxx>
50 #include <GUI/gui.h>
51 #include <Scenery/tilemgr.hxx>
52 #include <Objects/matlib.hxx>
53 #include <Time/light.hxx>
54 #include <Time/tmp.hxx>
55
56 #ifndef FG_OLD_WEATHER
57 #  include <WeatherCM/FGLocalWeatherDatabase.h>
58 #else
59 #  include <Weather/weather.hxx>
60 #endif
61
62 #include <Main/bfi.hxx>
63 #include <Main/globals.hxx>
64 #include <Main/keyboard.hxx>
65 #include <Main/fg_props.hxx>
66 #include <Main/options.hxx>
67
68 #include "input.hxx"
69
70 SG_USING_STD(ifstream);
71 SG_USING_STD(string);
72 SG_USING_STD(vector);
73
74
75 \f
76 ////////////////////////////////////////////////////////////////////////
77 // Local data structures.
78 ////////////////////////////////////////////////////////////////////////
79
80 \f
81 ////////////////////////////////////////////////////////////////////////
82 // Implementation of FGBinding.
83 ////////////////////////////////////////////////////////////////////////
84
85 FGBinding::FGBinding ()
86   : _command(0), _arg(0)
87 {
88 }
89
90 FGBinding::FGBinding (const SGPropertyNode * node)
91   : _command(0), _arg(0)
92 {
93   read(node);
94 }
95
96 FGBinding::~FGBinding ()
97 {
98   // no op
99 }
100
101 void
102 FGBinding::read (const SGPropertyNode * node)
103 {
104   _command_name = node->getStringValue("command", "");
105   if (_command_name == "") {
106     SG_LOG(SG_INPUT, SG_ALERT, "No command supplied for binding.");
107     _command = 0;
108     return;
109   }
110
111   _command = globals->get_commands()->getCommand(_command_name);
112   if (_command == 0) {
113     SG_LOG(SG_INPUT, SG_ALERT, "Command " << _command_name << " is undefined");
114     _arg = 0;
115     return;
116   }
117   _arg = node;                  // FIXME: don't use whole node!!!
118 }
119
120 void
121 FGBinding::fire () const
122 {
123   _fire(_arg);
124 }
125
126 void
127 FGBinding::fire (double setting) const
128 {
129   SGPropertyNode arg;
130   copyProperties(_arg, &arg);
131   arg.setDoubleValue("setting", setting);
132   _fire(&arg);
133 }
134
135 void
136 FGBinding::_fire(const SGPropertyNode * arg) const
137 {
138   if (_command == 0) {
139     SG_LOG(SG_INPUT, SG_ALERT, "No command attached to binding");
140   } else if (!(*_command)(arg)) {
141     SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command " << _command_name);
142   }
143 }
144
145
146 \f
147 ////////////////////////////////////////////////////////////////////////
148 // Implementation of FGInput.
149 ////////////////////////////////////////////////////////////////////////
150
151                                 // From main.cxx
152 extern void fgReshape( int width, int height );
153
154 FGInput current_input;
155
156
157 FGInput::FGInput ()
158 {
159   // no op
160 }
161
162 FGInput::~FGInput ()
163 {
164   // no op
165 }
166
167 void
168 FGInput::init ()
169 {
170   _init_keyboard();
171   _init_joystick();
172 }
173
174 void
175 FGInput::bind ()
176 {
177   // no op
178 }
179
180 void
181 FGInput::unbind ()
182 {
183   // no op
184 }
185
186 void 
187 FGInput::update ()
188 {
189   _update_keyboard();
190   _update_joystick();
191 }
192
193 void
194 FGInput::doKey (int k, int modifiers, int x, int y)
195 {
196                                 // Sanity check.
197   if (k < 0 || k >= MAX_KEYS) {
198     SG_LOG(SG_INPUT, SG_ALERT, "Key value " << k << " out of range");
199     return;
200   }
201
202   button &b = _key_bindings[k];
203
204                                 // Key pressed.
205   if (modifiers&FG_MOD_UP == 0) {
206     // SG_LOG(SG_INPUT, SG_INFO, "User pressed key " << k
207     //        << " with modifiers " << modifiers);
208     if (!b.last_state || b.is_repeatable) {
209       const binding_list_t &bindings =
210         _find_key_bindings(k, modifiers);
211       int max = bindings.size();
212       if (max > 0) {
213         for (int i = 0; i < max; i++)
214           bindings[i].fire();
215         return;
216       }
217     }
218   }
219
220                                 // Key released.
221   else {
222     // SG_LOG(SG_INPUT, SG_INFO, "User released key " << k
223     //        << " with modifiers " << modifiers);
224     if (b.last_state) {
225       const binding_list_t &bindings =
226         _find_key_bindings(k, modifiers);
227       int max = bindings.size();
228       if (max > 0) {
229         for (int i = 0; i < max; i++)
230           bindings[i].fire();
231         return;
232       }
233     }
234   }
235
236
237                                 // Use the old, default actions.
238   SG_LOG(SG_INPUT, SG_INFO, "(No user binding.)");
239   if (modifiers&FG_MOD_UP)
240     return;
241
242   float fov, tmp;
243   static bool winding_ccw = true;
244   int speed;
245   FGInterface *f = current_aircraft.fdm_state;
246   FGViewer *v = globals->get_current_view();
247   
248   // everything after here will be removed sooner or later...
249
250   if (modifiers & FG_MOD_SHIFT) {
251
252         switch (k) {
253         case 7: // Ctrl-G key
254             current_autopilot->set_AltitudeMode( 
255                   FGAutopilot::FG_ALTITUDE_GS1 );
256             current_autopilot->set_AltitudeEnabled(
257                   ! current_autopilot->get_AltitudeEnabled()
258                 );
259             return;
260         case 18: // Ctrl-R key
261             // temporary
262             winding_ccw = !winding_ccw;
263             if ( winding_ccw ) {
264                 glFrontFace ( GL_CCW );
265             } else {
266                 glFrontFace ( GL_CW );
267             }
268             return;
269         case 20: // Ctrl-T key
270             current_autopilot->set_AltitudeMode( 
271                   FGAutopilot::FG_ALTITUDE_TERRAIN );
272             current_autopilot->set_AltitudeEnabled(
273                   ! current_autopilot->get_AltitudeEnabled()
274                 );
275             return;
276         case 72: // H key
277             HUD_brightkey( true );
278             return;
279         case 73: // I key
280             // Minimal Hud
281             fgHUDInit2(&current_aircraft);
282             return;
283         case 77: // M key
284             globals->inc_warp( -60 );
285             fgUpdateSkyAndLightingParams();
286             return;
287         case 84: // T key
288             globals->inc_warp_delta( -30 );
289             fgUpdateSkyAndLightingParams();
290             return;
291         case 87: // W key
292 #if defined(FX) && !defined(WIN32)
293             global_fullscreen = ( !global_fullscreen );
294 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
295             XMesaSetFXmode( global_fullscreen ? 
296                             XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
297 #  endif
298 #endif
299             return;
300         case 88: // X key
301             fov = globals->get_current_view()->get_fov();
302             fov *= 1.05;
303             if ( fov > FG_FOV_MAX ) {
304                 fov = FG_FOV_MAX;
305             }
306             globals->get_current_view()->set_fov(fov);
307             // v->force_update_fov_math();
308             return;
309         case 90: // Z key
310 #ifndef FG_OLD_WEATHER
311             tmp = WeatherDatabase->getWeatherVisibility();
312             tmp /= 1.10;
313             WeatherDatabase->setWeatherVisibility( tmp );
314 #else
315             tmp = current_weather.get_visibility();   // in meters
316             tmp /= 1.10;
317             current_weather.set_visibility( tmp );
318 #endif
319             return;
320
321 // START SPECIALS
322
323         case 256+GLUT_KEY_F1: {
324             ifstream input("fgfs.sav");
325             if (input.good() && fgLoadFlight(input)) {
326                 input.close();
327                 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from fgfs.sav");
328             } else {
329                 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from fgfs.sav");
330             }
331             return;
332         }
333         case 256+GLUT_KEY_F2: {
334             SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
335             ofstream output("fgfs.sav");
336             if (output.good() && fgSaveFlight(output)) {
337                 output.close();
338                 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to fgfs.sav");
339             } else {
340                 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to fgfs.sav");
341             }
342             return;
343         }
344         case 256+GLUT_KEY_F3: {
345             string panel_path =
346                 fgGetString("/sim/panel/path", "Panels/Default/default.xml");
347             FGPanel * new_panel = fgReadPanel(panel_path);
348             if (new_panel == 0) {
349                 SG_LOG(SG_INPUT, SG_ALERT,
350                        "Error reading new panel from " << panel_path);
351                 return;
352             }
353             SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
354             current_panel->unbind();
355             delete current_panel;
356             current_panel = new_panel;
357             current_panel->bind();
358             return;
359         }
360         case 256+GLUT_KEY_F4: {
361             SGPath props_path(globals->get_fg_root());
362             props_path.append("preferences.xml");
363             SG_LOG(SG_INPUT, SG_INFO, "Rereading global preferences");
364             if (!readProperties(props_path.str(), globals->get_props())) {
365                 SG_LOG(SG_INPUT, SG_ALERT,
366                        "Failed to reread global preferences from "
367                        << props_path.str());
368             } else {
369                 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
370             }
371             return;
372         }
373         case 256+GLUT_KEY_F5: {
374             current_panel->setYOffset(current_panel->getYOffset() - 5);
375             fgReshape(fgGetInt("/sim/startup/xsize"),
376                       fgGetInt("/sim/startup/ysize"));
377             return;
378         }
379         case 256+GLUT_KEY_F6: {
380             current_panel->setYOffset(current_panel->getYOffset() + 5);
381             fgReshape(fgGetInt("/sim/startup/xsize"),
382                       fgGetInt("/sim/startup/ysize"));
383             return;
384         }
385         case 256+GLUT_KEY_F7: {
386             current_panel->setXOffset(current_panel->getXOffset() - 5);
387             return;
388         }
389         case 256+GLUT_KEY_F8: {
390             current_panel->setXOffset(current_panel->getXOffset() + 5);
391             return;
392         }
393         case 256+GLUT_KEY_F10: {
394             fgToggleFDMdataLogging();
395             return;
396         }
397
398 // END SPECIALS
399
400         }
401
402
403     } else {
404         SG_LOG( SG_INPUT, SG_DEBUG, "" );
405         switch (k) {
406         case 104: // h key
407             HUD_masterswitch( true );
408             return;
409         case 105: // i key
410             fgHUDInit(&current_aircraft);  // normal HUD
411             return;
412         case 109: // m key
413             globals->inc_warp( 60 );
414             fgUpdateSkyAndLightingParams();
415             return;
416         case 112: // p key
417             globals->set_freeze( ! globals->get_freeze() );
418
419             {
420                 SGBucket p( f->get_Longitude() * SGD_RADIANS_TO_DEGREES,
421                             f->get_Latitude() * SGD_RADIANS_TO_DEGREES );
422                 SGPath tile_path( globals->get_fg_root() );
423                 tile_path.append( "Scenery" );
424                 tile_path.append( p.gen_base_path() );
425                 tile_path.append( p.gen_index_str() );
426
427                 // printf position and attitude information
428                 SG_LOG( SG_INPUT, SG_INFO,
429                         "Lon = " << f->get_Longitude() * SGD_RADIANS_TO_DEGREES
430                         << "  Lat = " << f->get_Latitude() * SGD_RADIANS_TO_DEGREES
431                         << "  Altitude = " << f->get_Altitude() * SG_FEET_TO_METER
432                         );
433                 SG_LOG( SG_INPUT, SG_INFO,
434                         "Heading = " << f->get_Psi() * SGD_RADIANS_TO_DEGREES 
435                         << "  Roll = " << f->get_Phi() * SGD_RADIANS_TO_DEGREES
436                         << "  Pitch = " << f->get_Theta() * SGD_RADIANS_TO_DEGREES );
437                 SG_LOG( SG_INPUT, SG_INFO, tile_path.c_str());
438             }
439             return;
440         case 116: // t key
441             globals->inc_warp_delta( 30 );
442             fgUpdateSkyAndLightingParams();
443             return;
444         case 120: // x key
445             fov = globals->get_current_view()->get_fov();
446             fov /= 1.05;
447             if ( fov < FG_FOV_MIN ) {
448                 fov = FG_FOV_MIN;
449             }
450             globals->get_current_view()->set_fov(fov);
451             // v->force_update_fov_math();
452             return;
453         case 122: // z key
454 #ifndef FG_OLD_WEATHER
455             tmp = WeatherDatabase->getWeatherVisibility();
456             tmp *= 1.10;
457             WeatherDatabase->setWeatherVisibility( tmp );
458 #else
459             tmp = current_weather.get_visibility();   // in meters
460             tmp *= 1.10;
461             current_weather.set_visibility( tmp );
462 #endif
463             return;
464         case 27: // ESC
465             // if( fg_DebugOutput ) {
466             //   fclose( fg_DebugOutput );
467             // }
468             SG_LOG( SG_INPUT, SG_ALERT, 
469                     "Program exit requested." );
470             ConfirmExitDialog();
471             return;
472
473 // START SPECIALS
474
475         case 256+GLUT_KEY_F2: // F2 Reload Tile Cache...
476             {
477                 bool freeze = globals->get_freeze();
478                 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
479                 if ( !freeze ) 
480                     globals->set_freeze( true );
481                 BusyCursor(0);
482                 if ( global_tile_mgr.init() ) {
483                     // Load the local scenery data
484                     global_tile_mgr.update( 
485                         cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES,
486                         cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
487                 } else {
488                     SG_LOG( SG_GENERAL, SG_ALERT, 
489                             "Error in Tile Manager initialization!" );
490                     exit(-1);
491                 }
492                 BusyCursor(1);
493                 if ( !freeze )
494                    globals->set_freeze( false );
495                 return;
496             }
497         case 256+GLUT_KEY_F4: // F4 Update lighting manually
498             fgUpdateSkyAndLightingParams();
499             return;
500         case 256+GLUT_KEY_F6: // F6 toggles Autopilot target location
501             if ( current_autopilot->get_HeadingMode() !=
502                  FGAutopilot::FG_HEADING_WAYPOINT ) {
503                 current_autopilot->set_HeadingMode(
504                     FGAutopilot::FG_HEADING_WAYPOINT );
505                 current_autopilot->set_HeadingEnabled( true );
506             } else {
507                 current_autopilot->set_HeadingMode(
508                     FGAutopilot::FG_TC_HEADING_LOCK );
509             }
510             return;
511         case 256+GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
512             const string &fog = fgGetString("/sim/rendering/fog");
513             if (fog == "disabled") {
514               fgSetString("/sim/rendering/fog", "fastest");
515               SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=fastest");
516             } else if (fog == "fastest") {
517               fgSetString("/sim/rendering/fog", "nicest");
518               SG_LOG(SG_INPUT, SG_INFO, "Fog enabled, hint=nicest");
519             } else if (fog == "nicest") {
520               fgSetString("/sim/rendering/fog", "disabled");
521               SG_LOG(SG_INPUT, SG_INFO, "Fog disabled");
522             } else {
523               fgSetString("/sim/rendering/fog", "disabled");
524               SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized fog type "
525                      << fog << ", changed to 'disabled'");
526             }
527             return;
528         }
529         case 256+GLUT_KEY_F9: // F9 toggles textures on and off...
530             SG_LOG( SG_INPUT, SG_INFO, "Toggling texture" );
531             if ( fgGetBool("/sim/rendering/textures")) {
532                 fgSetBool("/sim/rendering/textures", false);
533                 material_lib.set_step( 1 );
534             } else {
535                 fgSetBool("/sim/rendering/textures", true);
536                 material_lib.set_step( 0 );
537             }
538             return;
539         case 256+GLUT_KEY_F10: // F10 toggles menu on and off...
540             SG_LOG(SG_INPUT, SG_INFO, "Invoking call back function");
541             guiToggleMenu();
542             return;
543         case 256+GLUT_KEY_F11: // F11 Altitude Dialog.
544             SG_LOG(SG_INPUT, SG_INFO, "Invoking Altitude call back function");
545             NewAltitude( NULL );
546             return;
547         case 256+GLUT_KEY_F12: // F12 Heading Dialog...
548             SG_LOG(SG_INPUT, SG_INFO, "Invoking Heading call back function");
549             NewHeading( NULL );
550             return;
551         }
552
553 // END SPECIALS
554
555     }
556 }
557
558
559 void
560 FGInput::_init_keyboard ()
561 {
562                                 // TODO: zero the old bindings first.
563   SG_LOG(SG_INPUT, SG_INFO, "Initializing key bindings");
564   SGPropertyNode * key_nodes = fgGetNode("/input/keyboard");
565   if (key_nodes == 0) {
566     SG_LOG(SG_INPUT, SG_ALERT, "No key bindings (/input/keyboard)!!");
567     return;
568   }
569   
570   vector<SGPropertyNode *> keys = key_nodes->getChildren("key");
571   for (unsigned int i = 0; i < keys.size(); i++) {
572     int index = keys[i]->getIndex();
573     SG_LOG(SG_INPUT, SG_INFO, "Binding key " << index);
574     _key_bindings[index].is_repeatable = keys[i]->getBoolValue("repeatable");
575     _read_bindings(keys[i], _key_bindings[index].bindings, FG_MOD_NONE);
576   }
577 }
578
579
580 void
581 FGInput::_init_joystick ()
582 {
583                                 // TODO: zero the old bindings first.
584   SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick bindings");
585   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
586   if (js_nodes == 0) {
587     SG_LOG(SG_INPUT, SG_ALERT, "No joystick bindings (/input/joysticks)!!");
588     return;
589   }
590
591   for (int i = 0; i < MAX_JOYSTICKS; i++) {
592     const SGPropertyNode * js_node = js_nodes->getChild("js", i);
593     if (js_node == 0) {
594       SG_LOG(SG_INPUT, SG_ALERT, "No bindings for joystick " << i);
595       continue;
596     }
597     jsJoystick * js = new jsJoystick(i);
598     _joystick_bindings[i].js = js;
599     if (js->notWorking()) {
600       SG_LOG(SG_INPUT, SG_INFO, "Joystick " << i << " not found");
601       continue;
602     }
603 #ifdef WIN32
604     JOYCAPS jsCaps ;
605     joyGetDevCaps( i, &jsCaps, sizeof(jsCaps) );
606     int nbuttons = jsCaps.wNumButtons;
607     if (nbuttons > MAX_BUTTONS) nbuttons = MAX_BUTTONS;
608 #else
609     int nbuttons = MAX_BUTTONS;
610 #endif
611         
612     int naxes = js->getNumAxes();
613     if (naxes > MAX_AXES) naxes = MAX_AXES;
614     _joystick_bindings[i].naxes = naxes;
615     _joystick_bindings[i].nbuttons = nbuttons;
616
617     SG_LOG(SG_INPUT, SG_INFO, "Initializing joystick " << i);
618
619                                 // Set up range arrays
620     float minRange[naxes];
621     float maxRange[naxes];
622     float center[naxes];
623
624                                 // Initialize with default values
625     js->getMinRange(minRange);
626     js->getMaxRange(maxRange);
627     js->getCenter(center);
628
629                                 // Allocate axes and buttons
630     _joystick_bindings[i].axes = new axis[naxes];
631     _joystick_bindings[i].buttons = new button[nbuttons];
632
633
634     //
635     // Initialize the axes.
636     //
637     for (int j = 0; j < naxes; j++) {
638       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
639       if (axis_node == 0) {
640         SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
641         continue;
642       }
643       
644       axis &a = _joystick_bindings[i].axes[j];
645
646       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
647
648       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
649       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
650       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
651       center[j] = axis_node->getDoubleValue("center", center[j]);
652
653       _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
654     }
655
656     //
657     // Initialize the buttons.
658     //
659     for (int j = 0; j < nbuttons; j++) {
660       const SGPropertyNode * button_node = js_node->getChild("button", j);
661       if (button_node == 0) {
662         SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << j);
663         continue;
664       }
665
666       button &b = _joystick_bindings[i].buttons[j];
667       
668       b.is_repeatable =
669         button_node->getBoolValue("repeatable", b.is_repeatable);
670
671                                 // Get the bindings for the button
672       _read_bindings(button_node, b.bindings, FG_MOD_NONE);
673     }
674
675     js->setMinRange(minRange);
676     js->setMaxRange(maxRange);
677     js->setCenter(center);
678   }
679 }
680
681
682 void
683 FGInput::_update_keyboard ()
684 {
685   // no-op
686 }
687
688
689 void
690 FGInput::_update_joystick ()
691 {
692   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
693   int buttons;
694   float js_val, diff;
695   float axis_values[MAX_AXES];
696
697   for (int i = 0; i < MAX_JOYSTICKS; i++) {
698
699     jsJoystick * js = _joystick_bindings[i].js;
700     if (js == 0 || js->notWorking())
701       continue;
702
703     js->read(&buttons, axis_values);
704
705
706                                 // Fire bindings for the axes.
707     for (int j = 0; j < _joystick_bindings[i].naxes; j++) {
708       axis &a = _joystick_bindings[i].axes[j];
709       
710                                 // Do nothing if the axis position
711                                 // is unchanged; only a change in
712                                 // position fires the bindings.
713       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
714 //      SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
715         SGPropertyNode node;
716         a.last_value = axis_values[j];
717 //      SG_LOG(SG_INPUT, SG_INFO, "There are "
718 //             << a.bindings[modifiers].size() << " bindings");
719         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
720           a.bindings[modifiers][k].fire(axis_values[j]);
721       }
722     }
723
724                                 // Fire bindings for the buttons.
725     for (int j = 0; j < _joystick_bindings[i].nbuttons; j++) {
726       bool pressed = ((buttons & (1 << j)) > 0);
727       button &b = _joystick_bindings[i].buttons[j];
728
729       if (pressed) {
730                                 // The press event may be repeated.
731         if (!b.last_state || b.is_repeatable) {
732 //        SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been pressed");
733           for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
734             b.bindings[modifiers][k].fire();
735         }
736       } else {
737                                 // The release event is never repeated.
738         if (b.last_state)
739 //        SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been released");
740           for (int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
741             b.bindings[modifiers|FG_MOD_UP][k].fire();
742       }
743           
744       b.last_state = pressed;
745     }
746   }
747 }
748
749
750 void
751 FGInput::_read_bindings (const SGPropertyNode * node, 
752                          binding_list_t * binding_list,
753                          int modifiers)
754 {
755   vector<const SGPropertyNode *> bindings = node->getChildren("binding");
756   for (unsigned int i = 0; i < bindings.size(); i++) {
757     SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
758            << bindings[i]->getStringValue("command"));
759     binding_list[modifiers].push_back(FGBinding(bindings[i]));
760   }
761
762                                 // Read nested bindings for modifiers
763   if (node->getChild("mod-up") != 0)
764     _read_bindings(node->getChild("mod-up"), binding_list,
765                    modifiers|FG_MOD_UP);
766
767   if (node->getChild("mod-shift") != 0)
768     _read_bindings(node->getChild("mod-shift"), binding_list,
769                    modifiers|FG_MOD_SHIFT);
770
771   if (node->getChild("mod-ctrl") != 0)
772     _read_bindings(node->getChild("mod-ctrl"), binding_list,
773                    modifiers|FG_MOD_CTRL);
774
775   if (node->getChild("mod-alt") != 0)
776     _read_bindings(node->getChild("mod-alt"), binding_list,
777                    modifiers|FG_MOD_ALT);
778 }
779
780
781 const vector<FGBinding> &
782 FGInput::_find_key_bindings (unsigned int k, int modifiers)
783 {
784   button &b = _key_bindings[k];
785
786                                 // Try it straight, first.
787   if (b.bindings[modifiers].size() > 0)
788     return b.bindings[modifiers];
789
790                                 // Try removing the control modifier
791                                 // for control keys.
792   else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
793     return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
794
795                                 // Try removing shift modifier 
796                                 // for upper case or any punctuation
797                                 // (since different keyboards will
798                                 // shift different punctuation types)
799   else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
800     return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
801
802                                 // Try removing alt modifier for
803                                 // high-bit characters.
804   else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
805     return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
806
807                                 // Give up and return the empty vector.
808   else
809     return b.bindings[modifiers];
810 }
811
812 // end of input.cxx