]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.cxx
2a865002f176fc2d0a6a4c70767caa6dd8f875f3
[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[MAX_AXES];
557     float maxRange[MAX_AXES];
558     float center[MAX_AXES];
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     int j;
574     for (j = 0; j < naxes; j++) {
575       const SGPropertyNode * axis_node = js_node->getChild("axis", j);
576       if (axis_node == 0) {
577         SG_LOG(SG_INPUT, SG_INFO, "No bindings for axis " << j);
578         continue;
579       }
580       
581       axis &a = _joystick_bindings[i].axes[j];
582
583       js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
584
585       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
586       minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
587       maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
588       center[j] = axis_node->getDoubleValue("center", center[j]);
589
590       _read_bindings(axis_node, a.bindings, FG_MOD_NONE);
591     }
592
593     //
594     // Initialize the buttons.
595     //
596     for (j = 0; j < nbuttons; j++) {
597       const SGPropertyNode * button_node = js_node->getChild("button", j);
598       if (button_node == 0) {
599         SG_LOG(SG_INPUT, SG_INFO, "No bindings for button " << j);
600         continue;
601       }
602
603       button &b = _joystick_bindings[i].buttons[j];
604       
605       b.is_repeatable =
606         button_node->getBoolValue("repeatable", b.is_repeatable);
607
608                                 // Get the bindings for the button
609       _read_bindings(button_node, b.bindings, FG_MOD_NONE);
610     }
611
612     js->setMinRange(minRange);
613     js->setMaxRange(maxRange);
614     js->setCenter(center);
615   }
616 }
617
618
619 void
620 FGInput::_update_keyboard ()
621 {
622   // no-op
623 }
624
625
626 void
627 FGInput::_update_joystick ()
628 {
629   int modifiers = FG_MOD_NONE;  // FIXME: any way to get the real ones?
630   int buttons;
631   float js_val, diff;
632   float axis_values[MAX_AXES];
633
634   int i;
635   int j;
636
637   for ( i = 0; i < MAX_JOYSTICKS; i++) {
638
639     jsJoystick * js = _joystick_bindings[i].js;
640     if (js == 0 || js->notWorking())
641       continue;
642
643     js->read(&buttons, axis_values);
644
645
646                                 // Fire bindings for the axes.
647     for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
648       axis &a = _joystick_bindings[i].axes[j];
649       
650                                 // Do nothing if the axis position
651                                 // is unchanged; only a change in
652                                 // position fires the bindings.
653       if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
654 //      SG_LOG(SG_INPUT, SG_INFO, "Axis " << j << " has moved");
655         SGPropertyNode node;
656         a.last_value = axis_values[j];
657 //      SG_LOG(SG_INPUT, SG_INFO, "There are "
658 //             << a.bindings[modifiers].size() << " bindings");
659         for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
660           a.bindings[modifiers][k].fire(axis_values[j]);
661       }
662     }
663
664                                 // Fire bindings for the buttons.
665     for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
666       bool pressed = ((buttons & (1 << j)) > 0);
667       button &b = _joystick_bindings[i].buttons[j];
668
669       if (pressed) {
670                                 // The press event may be repeated.
671         if (!b.last_state || b.is_repeatable) {
672 //        SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been pressed");
673           for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
674             b.bindings[modifiers][k].fire();
675         }
676       } else {
677                                 // The release event is never repeated.
678         if (b.last_state)
679 //        SG_LOG(SG_INPUT, SG_INFO, "Button " << j << " has been released");
680           for (int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
681             b.bindings[modifiers|FG_MOD_UP][k].fire();
682       }
683           
684       b.last_state = pressed;
685     }
686   }
687 }
688
689
690 void
691 FGInput::_read_bindings (const SGPropertyNode * node, 
692                          binding_list_t * binding_list,
693                          int modifiers)
694 {
695   vector<const SGPropertyNode *> bindings = node->getChildren("binding");
696   for (unsigned int i = 0; i < bindings.size(); i++) {
697     SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
698            << bindings[i]->getStringValue("command"));
699     binding_list[modifiers].push_back(FGBinding(bindings[i]));
700   }
701
702                                 // Read nested bindings for modifiers
703   if (node->getChild("mod-up") != 0)
704     _read_bindings(node->getChild("mod-up"), binding_list,
705                    modifiers|FG_MOD_UP);
706
707   if (node->getChild("mod-shift") != 0)
708     _read_bindings(node->getChild("mod-shift"), binding_list,
709                    modifiers|FG_MOD_SHIFT);
710
711   if (node->getChild("mod-ctrl") != 0)
712     _read_bindings(node->getChild("mod-ctrl"), binding_list,
713                    modifiers|FG_MOD_CTRL);
714
715   if (node->getChild("mod-alt") != 0)
716     _read_bindings(node->getChild("mod-alt"), binding_list,
717                    modifiers|FG_MOD_ALT);
718 }
719
720
721 const vector<FGBinding> &
722 FGInput::_find_key_bindings (unsigned int k, int modifiers)
723 {
724   button &b = _key_bindings[k];
725
726                                 // Try it straight, first.
727   if (b.bindings[modifiers].size() > 0)
728     return b.bindings[modifiers];
729
730                                 // Try removing the control modifier
731                                 // for control keys.
732   else if ((modifiers&FG_MOD_CTRL) && iscntrl(k))
733     return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
734
735                                 // Try removing shift modifier 
736                                 // for upper case or any punctuation
737                                 // (since different keyboards will
738                                 // shift different punctuation types)
739   else if ((modifiers&FG_MOD_SHIFT) && (isupper(k) || ispunct(k)))
740     return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
741
742                                 // Try removing alt modifier for
743                                 // high-bit characters.
744   else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
745     return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
746
747                                 // Give up and return the empty vector.
748   else
749     return b.bindings[modifiers];
750 }
751
752
753 /**
754  * Construct the modifiers.
755  */
756 static inline int get_mods ()
757 {
758   int glut_modifiers = glutGetModifiers();
759   int modifiers = 0;
760
761   if (glut_modifiers & GLUT_ACTIVE_SHIFT)
762     modifiers |= FGInput::FG_MOD_SHIFT;
763   if (glut_modifiers & GLUT_ACTIVE_CTRL)
764     modifiers |= FGInput::FG_MOD_CTRL;
765   if (glut_modifiers & GLUT_ACTIVE_ALT)
766     modifiers |= FGInput::FG_MOD_ALT;
767
768   return modifiers;
769 }
770
771
772 /**
773  * Key-down event handler for Glut.
774  *
775  * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
776  *
777  * @param k The integer value for the key pressed.
778  * @param x (unused)
779  * @param y (unused)
780  */
781 void GLUTkey(unsigned char k, int x, int y)
782 {
783                                 // Give PUI a chance to grab it first.
784   if (!puKeyboard(k, PU_DOWN))
785     current_input.doKey(k, get_mods(), x, y);
786 }
787
788
789 /**
790  * Key-up event handler for GLUT.
791  *
792  * <p>PUI doesn't use this, so always pass it to the input manager.</p>
793  *
794  * @param k The integer value for the key pressed.
795  * @param x (unused)
796  * @param y (unused)
797  */
798 void GLUTkeyup(unsigned char k, int x, int y)
799 {
800   current_input.doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
801 }
802
803
804 /**
805  * Special key-down handler for Glut.
806  *
807  * <p>Pass the value on to the FGInput module unless PUI wants it.
808  * The key value will have 256 added to it.</p>
809  *
810  * @param k The integer value for the key pressed (will have 256 added
811  * to it).
812  * @param x (unused)
813  * @param y (unused)
814  */
815 void GLUTspecialkey(int k, int x, int y)
816 {
817                                 // Give PUI a chance to grab it first.
818   if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN))
819     current_input.doKey(k + 256, get_mods(), x, y);
820 }
821
822
823 /**
824  * Special key-up handler for Glut.
825  *
826  * @param k The integer value for the key pressed (will have 256 added
827  * to it).
828  * @param x (unused)
829  * @param y (unused)
830  */
831 void GLUTspecialkeyup(int k, int x, int y)
832 {
833   current_input.doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
834 }
835
836 // end of input.cxx