1 // keyboard.cxx -- handle GLUT keyboard events
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 - 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
32 #include <simgear/compiler.h>
35 #include <simgear/xgl/xgl.h>
37 #if defined(FX) && defined(XMESA)
46 #include <plib/pu.h> // plib include
48 #include <simgear/constants.h>
49 #include <simgear/debug/logstream.hxx>
50 #include <simgear/misc/fgpath.hxx>
52 #include <Aircraft/aircraft.hxx>
53 #include <Autopilot/auto_gui.hxx>
54 #include <Autopilot/newauto.hxx>
55 #include <Cockpit/hud.hxx>
56 #include <Cockpit/panel.hxx>
57 #include <Cockpit/panel_io.hxx>
59 #include <Scenery/tilemgr.hxx>
60 #include <Objects/matlib.hxx>
61 #include <Time/light.hxx>
62 #include <Time/tmp.hxx>
64 #ifndef FG_OLD_WEATHER
65 # include <WeatherCM/FGLocalWeatherDatabase.h>
67 # include <Weather/weather.hxx>
71 #include "globals.hxx"
72 #include "keyboard.hxx"
76 extern void fgReshape( int width, int height );
79 // Handle keyboard events
80 void GLUTkey(unsigned char k, int x, int y) {
84 static bool winding_ccw = true;
87 f = current_aircraft.fdm_state;
88 v = globals->get_current_view();
90 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
91 if ( puKeyboard(k, PU_DOWN) ) {
95 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
96 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
99 current_autopilot->set_AltitudeMode(
100 FGAutopilot::FG_ALTITUDE_LOCK );
101 current_autopilot->set_AltitudeEnabled(
102 ! current_autopilot->get_AltitudeEnabled()
105 case 7: // Ctrl-G key
106 current_autopilot->set_AltitudeMode(
107 FGAutopilot::FG_ALTITUDE_GS1 );
108 current_autopilot->set_AltitudeEnabled(
109 ! current_autopilot->get_AltitudeEnabled()
112 case 8: // Ctrl-H key
113 current_autopilot->set_HeadingMode(
114 FGAutopilot::FG_HEADING_LOCK );
115 current_autopilot->set_HeadingEnabled(
116 ! current_autopilot->get_HeadingEnabled()
119 case 14: // Ctrl-N key
120 current_autopilot->set_HeadingMode(
121 FGAutopilot::FG_HEADING_NAV1 );
122 current_autopilot->set_HeadingEnabled(
123 ! current_autopilot->get_HeadingEnabled()
126 case 18: // Ctrl-R key
128 winding_ccw = !winding_ccw;
130 glFrontFace ( GL_CCW );
132 glFrontFace ( GL_CW );
135 case 19: // Ctrl-S key
136 current_autopilot->set_AutoThrottleEnabled(
137 ! current_autopilot->get_AutoThrottleEnabled()
140 case 20: // Ctrl-T key
141 current_autopilot->set_AltitudeMode(
142 FGAutopilot::FG_ALTITUDE_TERRAIN );
143 current_autopilot->set_AltitudeEnabled(
144 ! current_autopilot->get_AltitudeEnabled()
147 case 21: // Ctrl-U key
148 // add 1000' of emergency altitude. Possibly good for
149 // unflipping yourself :-)
151 double alt = cur_fdm_state->get_Altitude() + 1000;
152 fgFDMForceAltitude( globals->get_options()->get_flight_model(),
153 alt * FEET_TO_METER );
156 case 49: // numeric keypad 1
157 v->set_goal_view_offset( FG_PI * 0.75 );
158 if ( globals->get_options()->get_view_mode() ==
159 FGOptions::FG_VIEW_FOLLOW )
161 globals->get_current_view()->set_pilot_offset(-25.0, 25.0, 1.0);
162 v->set_view_offset( FG_PI * 0.75 );
165 case 50: // numeric keypad 2
166 v->set_goal_view_offset( FG_PI );
167 if ( globals->get_options()->get_view_mode() ==
168 FGOptions::FG_VIEW_FOLLOW )
170 globals->get_current_view()->set_pilot_offset(-25.0, 0.0, 1.0);
171 v->set_view_offset( FG_PI );
174 case 51: // numeric keypad 3
175 v->set_goal_view_offset( FG_PI * 1.25 );
176 if ( globals->get_options()->get_view_mode() ==
177 FGOptions::FG_VIEW_FOLLOW )
179 globals->get_current_view()->set_pilot_offset(-25.0, -25.0, 1.0);
180 v->set_view_offset( FG_PI * 1.25 );
183 case 52: // numeric keypad 4
184 v->set_goal_view_offset( FG_PI * 0.50 );
185 if ( globals->get_options()->get_view_mode() ==
186 FGOptions::FG_VIEW_FOLLOW )
188 globals->get_current_view()->set_pilot_offset(0.0, 25.0, 1.0);
189 v->set_view_offset( FG_PI * 0.50 );
192 case 54: // numeric keypad 6
193 v->set_goal_view_offset( FG_PI * 1.50 );
194 if ( globals->get_options()->get_view_mode() ==
195 FGOptions::FG_VIEW_FOLLOW )
197 globals->get_current_view()->set_pilot_offset(0.0, -25.0, 1.0);
198 v->set_view_offset( FG_PI * 1.50 );
201 case 55: // numeric keypad 7
202 v->set_goal_view_offset( FG_PI * 0.25 );
203 if ( globals->get_options()->get_view_mode() ==
204 FGOptions::FG_VIEW_FOLLOW )
206 globals->get_current_view()->set_pilot_offset(25.0, 25.0, 1.0);
207 v->set_view_offset( FG_PI * 0.25 );
210 case 56: // numeric keypad 8
211 v->set_goal_view_offset( 0.00 );
212 if ( globals->get_options()->get_view_mode() ==
213 FGOptions::FG_VIEW_FOLLOW )
215 globals->get_current_view()->set_pilot_offset(25.0, 0.0, 1.0);
216 v->set_view_offset( 0.00 );
219 case 57: // numeric keypad 9
220 v->set_goal_view_offset( FG_PI * 1.75 );
221 if ( globals->get_options()->get_view_mode() ==
222 FGOptions::FG_VIEW_FOLLOW )
224 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
225 v->set_view_offset( FG_PI * 1.75 );
229 speed = globals->get_options()->get_speed_up();
234 globals->get_options()->set_speed_up( speed );
237 // status = globals->get_options()->get_hud_status();
238 // globals->get_options()->set_hud_status(!status);
239 HUD_brightkey( true );
243 fgHUDInit2(¤t_aircraft);
246 globals->inc_warp( -60 );
247 fgUpdateSkyAndLightingParams();
250 globals->get_options()->toggle_panel();
253 globals->inc_warp_delta( -30 );
254 fgUpdateSkyAndLightingParams();
257 #if defined(FX) && !defined(WIN32)
258 global_fullscreen = ( !global_fullscreen );
259 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
260 XMesaSetFXmode( global_fullscreen ?
261 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
266 fov = globals->get_options()->get_fov();
268 if ( fov > FG_FOV_MAX ) {
271 globals->get_options()->set_fov(fov);
272 // v->force_update_fov_math();
275 #ifndef FG_OLD_WEATHER
276 tmp = WeatherDatabase->getWeatherVisibility();
278 WeatherDatabase->setWeatherVisibility( tmp );
280 tmp = current_weather.get_visibility(); // in meters
282 current_weather.set_visibility( tmp );
287 FG_LOG( FG_INPUT, FG_DEBUG, "" );
289 case 50: // numeric keypad 2
290 if ( current_autopilot->get_AltitudeEnabled() ) {
291 current_autopilot->AltitudeAdjust( 100 );
293 controls.move_elevator(-0.05);
296 case 56: // numeric keypad 8
297 if ( current_autopilot->get_AltitudeEnabled() ) {
298 current_autopilot->AltitudeAdjust( -100 );
300 controls.move_elevator(0.05);
303 case 49: // numeric keypad 1
304 controls.move_elevator_trim(-0.001);
306 case 55: // numeric keypad 7
307 controls.move_elevator_trim(0.001);
309 case 52: // numeric keypad 4
310 controls.move_aileron(-0.05);
312 case 54: // numeric keypad 6
313 controls.move_aileron(0.05);
315 case 48: // numeric keypad Ins
316 if ( current_autopilot->get_HeadingEnabled() ) {
317 current_autopilot->HeadingAdjust( -1 );
319 controls.move_rudder(-0.05);
322 case 13: // numeric keypad Enter
323 if ( current_autopilot->get_HeadingEnabled() ) {
324 current_autopilot->HeadingAdjust( 1 );
326 controls.move_rudder(0.05);
329 case 53: // numeric keypad 5
330 controls.set_aileron(0.0);
331 controls.set_elevator(0.0);
332 controls.set_rudder(0.0);
334 case 57: // numeric keypad 9 (Pg Up)
335 if ( current_autopilot->get_AutoThrottleEnabled() ) {
336 current_autopilot->AutoThrottleAdjust( 5 );
338 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
341 case 51: // numeric keypad 3 (Pg Dn)
342 if ( current_autopilot->get_AutoThrottleEnabled() ) {
343 current_autopilot->AutoThrottleAdjust( -5 );
345 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
349 controls.move_flaps(-0.34);
350 FG_LOG( FG_INPUT, FG_INFO,
351 "Set flaps to " << controls.get_flaps() );
354 controls.move_flaps(0.34);
355 FG_LOG( FG_INPUT, FG_INFO,
356 "Set flaps to " << controls.get_flaps() );
359 speed = globals->get_options()->get_speed_up();
361 globals->get_options()->set_speed_up( speed );
366 b_ret = int( controls.get_brake( 0 ) );
367 b_set = double(!b_ret);
368 controls.set_brake( FGControls::ALL_WHEELS, b_set);
371 if (controls.get_brake(0) > 0.0) {
372 controls.set_brake(0, 0.0);
374 controls.set_brake(0, 1.0);
378 if (controls.get_brake(1) > 0.0) {
379 controls.set_brake(1, 0.0);
381 controls.set_brake(1, 1.0);
385 HUD_masterswitch( true );
388 fgHUDInit(¤t_aircraft); // normal HUD
391 globals->inc_warp( 60 );
392 fgUpdateSkyAndLightingParams();
395 globals->set_freeze( ! globals->get_freeze() );
398 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
399 f->get_Latitude() * RAD_TO_DEG );
400 FGPath tile_path( globals->get_options()->get_fg_root() );
401 tile_path.append( "Scenery" );
402 tile_path.append( p.gen_base_path() );
403 tile_path.append( p.gen_index_str() );
405 // printf position and attitude information
406 FG_LOG( FG_INPUT, FG_INFO,
407 "Lon = " << f->get_Longitude() * RAD_TO_DEG
408 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
409 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
411 FG_LOG( FG_INPUT, FG_INFO,
412 "Heading = " << f->get_Psi() * RAD_TO_DEG
413 << " Roll = " << f->get_Phi() * RAD_TO_DEG
414 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
415 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
419 globals->inc_warp_delta( 30 );
420 fgUpdateSkyAndLightingParams();
423 // globals->get_options()->cycle_view_mode();
424 if ( globals->get_options()->get_view_mode() ==
425 FGOptions::FG_VIEW_FOLLOW )
427 globals->get_options()->set_view_mode(FGOptions::FG_VIEW_PILOT);
428 v->set_goal_view_offset( 0.0 );
429 v->set_view_offset( 0.0 );
430 } else if ( globals->get_options()->get_view_mode() ==
431 FGOptions::FG_VIEW_PILOT )
433 globals->get_options()->set_view_mode( FGOptions::FG_VIEW_FOLLOW );
434 v->set_goal_view_offset( FG_PI * 1.75 );
435 v->set_view_offset( FG_PI * 1.75 );
436 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
438 fgReshape( globals->get_options()->get_xsize(),
439 globals->get_options()->get_ysize() );
442 fov = globals->get_options()->get_fov();
444 if ( fov < FG_FOV_MIN ) {
447 globals->get_options()->set_fov(fov);
448 // v->force_update_fov_math();
451 #ifndef FG_OLD_WEATHER
452 tmp = WeatherDatabase->getWeatherVisibility();
454 WeatherDatabase->setWeatherVisibility( tmp );
456 tmp = current_weather.get_visibility(); // in meters
458 current_weather.set_visibility( tmp );
462 // if( fg_DebugOutput ) {
463 // fclose( fg_DebugOutput );
465 FG_LOG( FG_INPUT, FG_ALERT,
466 "Program exit requested." );
474 // Handle "special" keyboard events
475 void GLUTspecialkey(int k, int x, int y) {
478 v = globals->get_current_view();
480 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
482 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
486 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
487 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
490 ifstream input("fgfs.sav");
491 if (input.good() && fgLoadFlight(input)) {
493 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
495 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
500 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
501 ofstream output("fgfs.sav");
502 if (output.good() && fgSaveFlight(output)) {
504 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
506 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
512 current_properties.getStringValue("/sim/panel/path",
513 "Panels/Default/default.xml");
514 FGPanel * new_panel = fgReadPanel(panel_path);
515 if (new_panel == 0) {
516 FG_LOG(FG_INPUT, FG_ALERT,
517 "Error reading new panel from " << panel_path);
520 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
521 delete current_panel;
522 current_panel = new_panel;
526 FGPath props_path(globals->get_options()->get_fg_root());
527 props_path.append("preferences.xml");
528 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
529 if (!readPropertyList(props_path.str(), ¤t_properties)) {
530 FG_LOG(FG_INPUT, FG_ALERT,
531 "Failed to reread global preferences from "
532 << props_path.str());
534 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
539 current_panel->setYOffset(current_panel->getYOffset() - 5);
540 fgReshape(globals->get_options()->get_xsize(),
541 globals->get_options()->get_ysize());
545 current_panel->setYOffset(current_panel->getYOffset() + 5);
546 fgReshape(globals->get_options()->get_xsize(),
547 globals->get_options()->get_ysize());
551 current_panel->setXOffset(current_panel->getXOffset() - 5);
555 current_panel->setXOffset(current_panel->getXOffset() + 5);
558 case GLUT_KEY_END: // numeric keypad 1
559 v->set_goal_view_offset( FG_PI * 0.75 );
560 if ( globals->get_options()->get_view_mode() ==
561 FGOptions::FG_VIEW_FOLLOW)
563 globals->get_current_view()->set_pilot_offset(-25.0, 25.0, 1.0);
564 v->set_view_offset( FG_PI * 0.75 );
567 case GLUT_KEY_DOWN: // numeric keypad 2
568 v->set_goal_view_offset( FG_PI );
569 if ( globals->get_options()->get_view_mode() ==
570 FGOptions::FG_VIEW_FOLLOW )
572 globals->get_current_view()->set_pilot_offset(-25.0, 0.0, 1.0);
573 v->set_view_offset( FG_PI );
576 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
577 v->set_goal_view_offset( FG_PI * 1.25 );
578 if ( globals->get_options()->get_view_mode() ==
579 FGOptions::FG_VIEW_FOLLOW)
581 globals->get_current_view()->set_pilot_offset(-25.0, -25.0, 1.0);
582 v->set_view_offset( FG_PI * 1.25 );
585 case GLUT_KEY_LEFT: // numeric keypad 4
586 v->set_goal_view_offset( FG_PI * 0.50 );
587 if ( globals->get_options()->get_view_mode() ==
588 FGOptions::FG_VIEW_FOLLOW )
590 globals->get_current_view()->set_pilot_offset(0.0, 25.0, 1.0);
591 v->set_view_offset( FG_PI * 0.50 );
594 case GLUT_KEY_RIGHT: // numeric keypad 6
595 v->set_goal_view_offset( FG_PI * 1.50 );
596 if ( globals->get_options()->get_view_mode() ==
597 FGOptions::FG_VIEW_FOLLOW )
599 globals->get_current_view()->set_pilot_offset(0.0, -25.0, 1.0);
600 v->set_view_offset( FG_PI * 1.50 );
603 case GLUT_KEY_HOME: // numeric keypad 7
604 v->set_goal_view_offset( FG_PI * 0.25 );
605 if ( globals->get_options()->get_view_mode() ==
606 FGOptions::FG_VIEW_FOLLOW )
608 globals->get_current_view()->set_pilot_offset(25.0, 25.0, 1.0);
609 v->set_view_offset( FG_PI * 0.25 );
612 case GLUT_KEY_UP: // numeric keypad 8
613 v->set_goal_view_offset( 0.00 );
614 if ( globals->get_options()->get_view_mode() ==
615 FGOptions::FG_VIEW_FOLLOW )
617 globals->get_current_view()->set_pilot_offset(25.0, 0.0, 1.0);
618 v->set_view_offset( 0.00 );
621 case GLUT_KEY_PAGE_UP: // numeric keypad 9
622 v->set_goal_view_offset( FG_PI * 1.75 );
623 if ( globals->get_options()->get_view_mode() ==
624 FGOptions::FG_VIEW_FOLLOW )
626 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
627 v->set_view_offset( FG_PI * 1.75 );
632 FG_LOG( FG_INPUT, FG_DEBUG, "" );
634 case GLUT_KEY_F2: // F2 Reload Tile Cache...
637 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
639 globals->set_freeze( true );
641 if ( global_tile_mgr.init() ) {
642 // Load the local scenery data
643 global_tile_mgr.update(
644 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
645 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
647 FG_LOG( FG_GENERAL, FG_ALERT,
648 "Error in Tile Manager initialization!" );
653 globals->set_freeze( false );
656 case GLUT_KEY_F3: // F3 Take a screen shot
659 case GLUT_KEY_F6: // F6 toggles Autopilot target location
660 if ( current_autopilot->get_HeadingMode() !=
661 FGAutopilot::FG_HEADING_WAYPOINT ) {
662 current_autopilot->set_HeadingMode(
663 FGAutopilot::FG_HEADING_WAYPOINT );
664 current_autopilot->set_HeadingEnabled( true );
666 current_autopilot->set_HeadingMode(
667 FGAutopilot::FG_HEADING_LOCK );
670 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
671 globals->get_options()->cycle_fog();
673 if ( globals->get_options()->get_fog() ==
674 FGOptions::FG_FOG_DISABLED )
676 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
677 } else if ( globals->get_options()->get_fog() ==
678 FGOptions::FG_FOG_FASTEST )
680 FG_LOG( FG_INPUT, FG_INFO,
681 "Fog enabled, hint set to fastest" );
682 } else if ( globals->get_options()->get_fog() ==
683 FGOptions::FG_FOG_NICEST )
685 FG_LOG( FG_INPUT, FG_INFO,
686 "Fog enabled, hint set to nicest" );
690 case GLUT_KEY_F9: // F9 toggles textures on and off...
691 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
692 if ( globals->get_options()->get_textures() ) {
693 globals->get_options()->set_textures( false );
694 material_lib.set_step( 1 );
696 globals->get_options()->set_textures( true );
697 material_lib.set_step( 0 );
700 case GLUT_KEY_F10: // F10 toggles menu on and off...
701 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
704 case GLUT_KEY_F11: // F11 Altitude Dialog.
705 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
708 case GLUT_KEY_F12: // F12 Heading Dialog...
709 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
713 if ( current_autopilot->get_AltitudeEnabled() ) {
714 current_autopilot->AltitudeAdjust( -100 );
716 controls.move_elevator(0.05);
720 if ( current_autopilot->get_AltitudeEnabled() ) {
721 current_autopilot->AltitudeAdjust( 100 );
723 controls.move_elevator(-0.05);
727 controls.move_aileron(-0.05);
730 controls.move_aileron(0.05);
732 case GLUT_KEY_HOME: // numeric keypad 1
733 controls.move_elevator_trim(0.001);
735 case GLUT_KEY_END: // numeric keypad 7
736 controls.move_elevator_trim(-0.001);
738 case GLUT_KEY_INSERT: // numeric keypad Ins
739 if ( current_autopilot->get_HeadingEnabled() ) {
740 current_autopilot->HeadingAdjust( -1 );
742 controls.move_rudder(-0.05);
745 case 13: // numeric keypad Enter
746 if ( current_autopilot->get_HeadingEnabled() ) {
747 current_autopilot->HeadingAdjust( 1 );
749 controls.move_rudder(0.05);
752 case 53: // numeric keypad 5
753 controls.set_aileron(0.0);
754 controls.set_elevator(0.0);
755 controls.set_rudder(0.0);
757 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
758 if ( current_autopilot->get_AutoThrottleEnabled() ) {
759 current_autopilot->AutoThrottleAdjust( 5 );
761 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
764 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
765 if ( current_autopilot->get_AutoThrottleEnabled() ) {
766 current_autopilot->AutoThrottleAdjust( -5 );
768 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );