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