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