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