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) {
82 static bool winding_ccw = true;
85 FGInterface *f = current_aircraft.fdm_state;
86 FGViewerRPH *v = (FGViewerRPH *)globals->get_current_view();
88 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
89 if ( puKeyboard(k, PU_DOWN) ) {
93 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
94 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
97 current_autopilot->set_AltitudeMode(
98 FGAutopilot::FG_ALTITUDE_LOCK );
99 current_autopilot->set_AltitudeEnabled(
100 ! current_autopilot->get_AltitudeEnabled()
103 case 7: // Ctrl-G key
104 current_autopilot->set_AltitudeMode(
105 FGAutopilot::FG_ALTITUDE_GS1 );
106 current_autopilot->set_AltitudeEnabled(
107 ! current_autopilot->get_AltitudeEnabled()
110 case 8: // Ctrl-H key
111 current_autopilot->set_HeadingMode(
112 FGAutopilot::FG_HEADING_LOCK );
113 current_autopilot->set_HeadingEnabled(
114 ! current_autopilot->get_HeadingEnabled()
117 case 14: // Ctrl-N key
118 current_autopilot->set_HeadingMode(
119 FGAutopilot::FG_HEADING_NAV1 );
120 current_autopilot->set_HeadingEnabled(
121 ! current_autopilot->get_HeadingEnabled()
124 case 18: // Ctrl-R key
126 winding_ccw = !winding_ccw;
128 glFrontFace ( GL_CCW );
130 glFrontFace ( GL_CW );
133 case 19: // Ctrl-S key
134 current_autopilot->set_AutoThrottleEnabled(
135 ! current_autopilot->get_AutoThrottleEnabled()
138 case 20: // Ctrl-T key
139 current_autopilot->set_AltitudeMode(
140 FGAutopilot::FG_ALTITUDE_TERRAIN );
141 current_autopilot->set_AltitudeEnabled(
142 ! current_autopilot->get_AltitudeEnabled()
145 case 21: // Ctrl-U key
146 // add 1000' of emergency altitude. Possibly good for
147 // unflipping yourself :-)
149 double alt = cur_fdm_state->get_Altitude() + 1000;
150 fgFDMForceAltitude( globals->get_options()->get_flight_model(),
151 alt * FEET_TO_METER );
154 case 49: // numeric keypad 1
155 v->set_goal_view_offset( FG_PI * 0.75 );
156 if ( globals->get_options()->get_view_mode() ==
157 FGOptions::FG_VIEW_FOLLOW )
159 globals->get_current_view()->set_pilot_offset(-25.0, 25.0, 1.0);
160 v->set_view_offset( FG_PI * 0.75 );
163 case 50: // numeric keypad 2
164 v->set_goal_view_offset( FG_PI );
165 if ( globals->get_options()->get_view_mode() ==
166 FGOptions::FG_VIEW_FOLLOW )
168 globals->get_current_view()->set_pilot_offset(-25.0, 0.0, 1.0);
169 v->set_view_offset( FG_PI );
172 case 51: // numeric keypad 3
173 v->set_goal_view_offset( FG_PI * 1.25 );
174 if ( globals->get_options()->get_view_mode() ==
175 FGOptions::FG_VIEW_FOLLOW )
177 globals->get_current_view()->set_pilot_offset(-25.0, -25.0, 1.0);
178 v->set_view_offset( FG_PI * 1.25 );
181 case 52: // numeric keypad 4
182 v->set_goal_view_offset( FG_PI * 0.50 );
183 if ( globals->get_options()->get_view_mode() ==
184 FGOptions::FG_VIEW_FOLLOW )
186 globals->get_current_view()->set_pilot_offset(0.0, 25.0, 1.0);
187 v->set_view_offset( FG_PI * 0.50 );
190 case 54: // numeric keypad 6
191 v->set_goal_view_offset( FG_PI * 1.50 );
192 if ( globals->get_options()->get_view_mode() ==
193 FGOptions::FG_VIEW_FOLLOW )
195 globals->get_current_view()->set_pilot_offset(0.0, -25.0, 1.0);
196 v->set_view_offset( FG_PI * 1.50 );
199 case 55: // numeric keypad 7
200 v->set_goal_view_offset( FG_PI * 0.25 );
201 if ( globals->get_options()->get_view_mode() ==
202 FGOptions::FG_VIEW_FOLLOW )
204 globals->get_current_view()->set_pilot_offset(25.0, 25.0, 1.0);
205 v->set_view_offset( FG_PI * 0.25 );
208 case 56: // numeric keypad 8
209 v->set_goal_view_offset( 0.00 );
210 if ( globals->get_options()->get_view_mode() ==
211 FGOptions::FG_VIEW_FOLLOW )
213 globals->get_current_view()->set_pilot_offset(25.0, 0.0, 1.0);
214 v->set_view_offset( 0.00 );
217 case 57: // numeric keypad 9
218 v->set_goal_view_offset( FG_PI * 1.75 );
219 if ( globals->get_options()->get_view_mode() ==
220 FGOptions::FG_VIEW_FOLLOW )
222 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
223 v->set_view_offset( FG_PI * 1.75 );
227 speed = globals->get_options()->get_speed_up();
232 globals->get_options()->set_speed_up( speed );
235 // status = globals->get_options()->get_hud_status();
236 // globals->get_options()->set_hud_status(!status);
237 HUD_brightkey( true );
241 fgHUDInit2(¤t_aircraft);
244 globals->inc_warp( -60 );
245 fgUpdateSkyAndLightingParams();
248 globals->get_options()->toggle_panel();
251 globals->inc_warp_delta( -30 );
252 fgUpdateSkyAndLightingParams();
255 #if defined(FX) && !defined(WIN32)
256 global_fullscreen = ( !global_fullscreen );
257 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
258 XMesaSetFXmode( global_fullscreen ?
259 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
264 fov = globals->get_options()->get_fov();
266 if ( fov > FG_FOV_MAX ) {
269 globals->get_options()->set_fov(fov);
270 // v->force_update_fov_math();
273 #ifndef FG_OLD_WEATHER
274 tmp = WeatherDatabase->getWeatherVisibility();
276 WeatherDatabase->setWeatherVisibility( tmp );
278 tmp = current_weather.get_visibility(); // in meters
280 current_weather.set_visibility( tmp );
285 FG_LOG( FG_INPUT, FG_DEBUG, "" );
287 case 50: // numeric keypad 2
288 if ( current_autopilot->get_AltitudeEnabled() ) {
289 current_autopilot->AltitudeAdjust( 100 );
291 controls.move_elevator(-0.05);
294 case 56: // numeric keypad 8
295 if ( current_autopilot->get_AltitudeEnabled() ) {
296 current_autopilot->AltitudeAdjust( -100 );
298 controls.move_elevator(0.05);
301 case 49: // numeric keypad 1
302 controls.move_elevator_trim(-0.001);
304 case 55: // numeric keypad 7
305 controls.move_elevator_trim(0.001);
307 case 52: // numeric keypad 4
308 controls.move_aileron(-0.05);
310 case 54: // numeric keypad 6
311 controls.move_aileron(0.05);
313 case 48: // numeric keypad Ins
314 if ( current_autopilot->get_HeadingEnabled() ) {
315 current_autopilot->HeadingAdjust( -1 );
317 controls.move_rudder(-0.05);
320 case 13: // numeric keypad Enter
321 if ( current_autopilot->get_HeadingEnabled() ) {
322 current_autopilot->HeadingAdjust( 1 );
324 controls.move_rudder(0.05);
327 case 53: // numeric keypad 5
328 controls.set_aileron(0.0);
329 controls.set_elevator(0.0);
330 controls.set_rudder(0.0);
332 case 57: // numeric keypad 9 (Pg Up)
333 if ( current_autopilot->get_AutoThrottleEnabled() ) {
334 current_autopilot->AutoThrottleAdjust( 5 );
336 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
339 case 51: // numeric keypad 3 (Pg Dn)
340 if ( current_autopilot->get_AutoThrottleEnabled() ) {
341 current_autopilot->AutoThrottleAdjust( -5 );
343 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
347 controls.move_flaps(-0.34);
348 FG_LOG( FG_INPUT, FG_INFO,
349 "Set flaps to " << controls.get_flaps() );
352 controls.move_flaps(0.34);
353 FG_LOG( FG_INPUT, FG_INFO,
354 "Set flaps to " << controls.get_flaps() );
357 speed = globals->get_options()->get_speed_up();
359 globals->get_options()->set_speed_up( speed );
364 b_ret = int( controls.get_brake( 0 ) );
365 b_set = double(!b_ret);
366 controls.set_brake( FGControls::ALL_WHEELS, b_set);
369 if (controls.get_brake(0) > 0.0) {
370 controls.set_brake(0, 0.0);
372 controls.set_brake(0, 1.0);
376 if (controls.get_brake(1) > 0.0) {
377 controls.set_brake(1, 0.0);
379 controls.set_brake(1, 1.0);
383 HUD_masterswitch( true );
386 fgHUDInit(¤t_aircraft); // normal HUD
389 globals->inc_warp( 60 );
390 fgUpdateSkyAndLightingParams();
393 globals->set_freeze( ! globals->get_freeze() );
396 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
397 f->get_Latitude() * RAD_TO_DEG );
398 FGPath tile_path( globals->get_options()->get_fg_root() );
399 tile_path.append( "Scenery" );
400 tile_path.append( p.gen_base_path() );
401 tile_path.append( p.gen_index_str() );
403 // printf position and attitude information
404 FG_LOG( FG_INPUT, FG_INFO,
405 "Lon = " << f->get_Longitude() * RAD_TO_DEG
406 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
407 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
409 FG_LOG( FG_INPUT, FG_INFO,
410 "Heading = " << f->get_Psi() * RAD_TO_DEG
411 << " Roll = " << f->get_Phi() * RAD_TO_DEG
412 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
413 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
417 globals->inc_warp_delta( 30 );
418 fgUpdateSkyAndLightingParams();
421 globals->set_current_view( globals->get_viewmgr()->next_view() );
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);
439 fgReshape( globals->get_options()->get_xsize(),
440 globals->get_options()->get_ysize() );
443 fov = globals->get_options()->get_fov();
445 if ( fov < FG_FOV_MIN ) {
448 globals->get_options()->set_fov(fov);
449 // v->force_update_fov_math();
452 #ifndef FG_OLD_WEATHER
453 tmp = WeatherDatabase->getWeatherVisibility();
455 WeatherDatabase->setWeatherVisibility( tmp );
457 tmp = current_weather.get_visibility(); // in meters
459 current_weather.set_visibility( tmp );
463 // if( fg_DebugOutput ) {
464 // fclose( fg_DebugOutput );
466 FG_LOG( FG_INPUT, FG_ALERT,
467 "Program exit requested." );
475 // Handle "special" keyboard events
476 void GLUTspecialkey(int k, int x, int y) {
477 FGViewerRPH *v = (FGViewerRPH *)globals->get_current_view();
479 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
481 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
485 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
486 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
489 ifstream input("fgfs.sav");
490 if (input.good() && fgLoadFlight(input)) {
492 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
494 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
499 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
500 ofstream output("fgfs.sav");
501 if (output.good() && fgSaveFlight(output)) {
503 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
505 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
511 current_properties.getStringValue("/sim/panel/path",
512 "Panels/Default/default.xml");
513 FGPanel * new_panel = fgReadPanel(panel_path);
514 if (new_panel == 0) {
515 FG_LOG(FG_INPUT, FG_ALERT,
516 "Error reading new panel from " << panel_path);
519 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
520 delete current_panel;
521 current_panel = new_panel;
525 FGPath props_path(globals->get_options()->get_fg_root());
526 props_path.append("preferences.xml");
527 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
528 if (!readPropertyList(props_path.str(), ¤t_properties)) {
529 FG_LOG(FG_INPUT, FG_ALERT,
530 "Failed to reread global preferences from "
531 << props_path.str());
533 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
538 current_panel->setYOffset(current_panel->getYOffset() - 5);
539 fgReshape(globals->get_options()->get_xsize(),
540 globals->get_options()->get_ysize());
544 current_panel->setYOffset(current_panel->getYOffset() + 5);
545 fgReshape(globals->get_options()->get_xsize(),
546 globals->get_options()->get_ysize());
550 current_panel->setXOffset(current_panel->getXOffset() - 5);
554 current_panel->setXOffset(current_panel->getXOffset() + 5);
557 case GLUT_KEY_END: // numeric keypad 1
558 v->set_goal_view_offset( FG_PI * 0.75 );
559 if ( globals->get_options()->get_view_mode() ==
560 FGOptions::FG_VIEW_FOLLOW)
562 globals->get_current_view()->set_pilot_offset(-25.0, 25.0, 1.0);
563 v->set_view_offset( FG_PI * 0.75 );
566 case GLUT_KEY_DOWN: // numeric keypad 2
567 v->set_goal_view_offset( FG_PI );
568 if ( globals->get_options()->get_view_mode() ==
569 FGOptions::FG_VIEW_FOLLOW )
571 globals->get_current_view()->set_pilot_offset(-25.0, 0.0, 1.0);
572 v->set_view_offset( FG_PI );
575 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
576 v->set_goal_view_offset( FG_PI * 1.25 );
577 if ( globals->get_options()->get_view_mode() ==
578 FGOptions::FG_VIEW_FOLLOW)
580 globals->get_current_view()->set_pilot_offset(-25.0, -25.0, 1.0);
581 v->set_view_offset( FG_PI * 1.25 );
584 case GLUT_KEY_LEFT: // numeric keypad 4
585 v->set_goal_view_offset( FG_PI * 0.50 );
586 if ( globals->get_options()->get_view_mode() ==
587 FGOptions::FG_VIEW_FOLLOW )
589 globals->get_current_view()->set_pilot_offset(0.0, 25.0, 1.0);
590 v->set_view_offset( FG_PI * 0.50 );
593 case GLUT_KEY_RIGHT: // numeric keypad 6
594 v->set_goal_view_offset( FG_PI * 1.50 );
595 if ( globals->get_options()->get_view_mode() ==
596 FGOptions::FG_VIEW_FOLLOW )
598 globals->get_current_view()->set_pilot_offset(0.0, -25.0, 1.0);
599 v->set_view_offset( FG_PI * 1.50 );
602 case GLUT_KEY_HOME: // numeric keypad 7
603 v->set_goal_view_offset( FG_PI * 0.25 );
604 if ( globals->get_options()->get_view_mode() ==
605 FGOptions::FG_VIEW_FOLLOW )
607 globals->get_current_view()->set_pilot_offset(25.0, 25.0, 1.0);
608 v->set_view_offset( FG_PI * 0.25 );
611 case GLUT_KEY_UP: // numeric keypad 8
612 v->set_goal_view_offset( 0.00 );
613 if ( globals->get_options()->get_view_mode() ==
614 FGOptions::FG_VIEW_FOLLOW )
616 globals->get_current_view()->set_pilot_offset(25.0, 0.0, 1.0);
617 v->set_view_offset( 0.00 );
620 case GLUT_KEY_PAGE_UP: // numeric keypad 9
621 v->set_goal_view_offset( FG_PI * 1.75 );
622 if ( globals->get_options()->get_view_mode() ==
623 FGOptions::FG_VIEW_FOLLOW )
625 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
626 v->set_view_offset( FG_PI * 1.75 );
631 FG_LOG( FG_INPUT, FG_DEBUG, "" );
633 case GLUT_KEY_F2: // F2 Reload Tile Cache...
636 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
638 globals->set_freeze( true );
640 if ( global_tile_mgr.init() ) {
641 // Load the local scenery data
642 global_tile_mgr.update(
643 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
644 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
646 FG_LOG( FG_GENERAL, FG_ALERT,
647 "Error in Tile Manager initialization!" );
652 globals->set_freeze( false );
655 case GLUT_KEY_F3: // F3 Take a screen shot
658 case GLUT_KEY_F6: // F6 toggles Autopilot target location
659 if ( current_autopilot->get_HeadingMode() !=
660 FGAutopilot::FG_HEADING_WAYPOINT ) {
661 current_autopilot->set_HeadingMode(
662 FGAutopilot::FG_HEADING_WAYPOINT );
663 current_autopilot->set_HeadingEnabled( true );
665 current_autopilot->set_HeadingMode(
666 FGAutopilot::FG_HEADING_LOCK );
669 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
670 globals->get_options()->cycle_fog();
672 if ( globals->get_options()->get_fog() ==
673 FGOptions::FG_FOG_DISABLED )
675 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
676 } else if ( globals->get_options()->get_fog() ==
677 FGOptions::FG_FOG_FASTEST )
679 FG_LOG( FG_INPUT, FG_INFO,
680 "Fog enabled, hint set to fastest" );
681 } else if ( globals->get_options()->get_fog() ==
682 FGOptions::FG_FOG_NICEST )
684 FG_LOG( FG_INPUT, FG_INFO,
685 "Fog enabled, hint set to nicest" );
689 case GLUT_KEY_F9: // F9 toggles textures on and off...
690 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
691 if ( globals->get_options()->get_textures() ) {
692 globals->get_options()->set_textures( false );
693 material_lib.set_step( 1 );
695 globals->get_options()->set_textures( true );
696 material_lib.set_step( 0 );
699 case GLUT_KEY_F10: // F10 toggles menu on and off...
700 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
703 case GLUT_KEY_F11: // F11 Altitude Dialog.
704 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
707 case GLUT_KEY_F12: // F12 Heading Dialog...
708 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
712 if ( current_autopilot->get_AltitudeEnabled() ) {
713 current_autopilot->AltitudeAdjust( -100 );
715 controls.move_elevator(0.05);
719 if ( current_autopilot->get_AltitudeEnabled() ) {
720 current_autopilot->AltitudeAdjust( 100 );
722 controls.move_elevator(-0.05);
726 controls.move_aileron(-0.05);
729 controls.move_aileron(0.05);
731 case GLUT_KEY_HOME: // numeric keypad 1
732 controls.move_elevator_trim(0.001);
734 case GLUT_KEY_END: // numeric keypad 7
735 controls.move_elevator_trim(-0.001);
737 case GLUT_KEY_INSERT: // numeric keypad Ins
738 if ( current_autopilot->get_HeadingEnabled() ) {
739 current_autopilot->HeadingAdjust( -1 );
741 controls.move_rudder(-0.05);
744 case 13: // numeric keypad Enter
745 if ( current_autopilot->get_HeadingEnabled() ) {
746 current_autopilot->HeadingAdjust( 1 );
748 controls.move_rudder(0.05);
751 case 53: // numeric keypad 5
752 controls.set_aileron(0.0);
753 controls.set_elevator(0.0);
754 controls.set_rudder(0.0);
756 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
757 if ( current_autopilot->get_AutoThrottleEnabled() ) {
758 current_autopilot->AutoThrottleAdjust( 5 );
760 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
763 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
764 if ( current_autopilot->get_AutoThrottleEnabled() ) {
765 current_autopilot->AutoThrottleAdjust( -5 );
767 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );