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