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>
57 #include <Scenery/tilemgr.hxx>
58 #include <Objects/matlib.hxx>
59 #include <Time/fg_time.hxx>
60 #include <Time/light.hxx>
62 #ifndef FG_OLD_WEATHER
63 # include <WeatherCM/FGLocalWeatherDatabase.h>
65 # include <Weather/weather.hxx>
69 #include "keyboard.hxx"
70 #include "options.hxx"
75 // Force an update of the sky and lighting parameters
76 static void local_update_sky_and_lighting_params( void ) {
77 FGTime::cur_time_params->updateLocal();
78 cur_light_params.Update();
82 // Handle keyboard events
83 void GLUTkey(unsigned char k, int x, int y) {
88 static bool winding_ccw = true;
91 f = current_aircraft.fdm_state;
92 t = FGTime::cur_time_params;
95 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
96 if ( puKeyboard(k, PU_DOWN) ) {
100 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
101 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
103 case 1: // Ctrl-A key
104 current_autopilot->set_AltitudeMode(
105 FGAutopilot::FG_ALTITUDE_LOCK );
106 current_autopilot->set_AltitudeEnabled(
107 ! current_autopilot->get_AltitudeEnabled()
110 case 7: // Ctrl-G key
111 current_autopilot->set_AltitudeMode(
112 FGAutopilot::FG_ALTITUDE_GS1 );
113 current_autopilot->set_AltitudeEnabled(
114 ! current_autopilot->get_AltitudeEnabled()
117 case 8: // Ctrl-H key
118 current_autopilot->set_HeadingMode(
119 FGAutopilot::FG_HEADING_LOCK );
120 current_autopilot->set_HeadingEnabled(
121 ! current_autopilot->get_HeadingEnabled()
124 case 14: // Ctrl-N key
125 current_autopilot->set_HeadingMode(
126 FGAutopilot::FG_HEADING_NAV1 );
127 current_autopilot->set_HeadingEnabled(
128 ! current_autopilot->get_HeadingEnabled()
131 case 18: // Ctrl-R key
133 winding_ccw = !winding_ccw;
135 glFrontFace ( GL_CCW );
137 glFrontFace ( GL_CW );
140 case 19: // Ctrl-S key
141 current_autopilot->set_AutoThrottleEnabled(
142 ! current_autopilot->get_AutoThrottleEnabled()
145 case 20: // Ctrl-T key
146 current_autopilot->set_AltitudeMode(
147 FGAutopilot::FG_ALTITUDE_TERRAIN );
148 current_autopilot->set_AltitudeEnabled(
149 ! current_autopilot->get_AltitudeEnabled()
152 case 21: // Ctrl-U key
153 // add 1000' of emergency altitude. Possibly good for
154 // unflipping yourself :-)
156 double alt = cur_fdm_state->get_Altitude() + 1000;
157 fgFDMForceAltitude( current_options.get_flight_model(),
158 alt * FEET_TO_METER );
161 case 49: // numeric keypad 1
162 v->set_goal_view_offset( FG_PI * 0.75 );
164 case 50: // numeric keypad 2
165 v->set_goal_view_offset( FG_PI );
167 case 51: // numeric keypad 3
168 v->set_goal_view_offset( FG_PI * 1.25 );
170 case 52: // numeric keypad 4
171 v->set_goal_view_offset( FG_PI * 0.50 );
173 case 54: // numeric keypad 6
174 v->set_goal_view_offset( FG_PI * 1.50 );
176 case 55: // numeric keypad 7
177 v->set_goal_view_offset( FG_PI * 0.25 );
179 case 56: // numeric keypad 8
180 v->set_goal_view_offset( 0.00 );
182 case 57: // numeric keypad 9
183 v->set_goal_view_offset( FG_PI * 1.75 );
186 speed = current_options.get_speed_up();
191 current_options.set_speed_up( speed );
194 // status = current_options.get_hud_status();
195 // current_options.set_hud_status(!status);
196 HUD_brightkey( true );
200 fgHUDInit2(¤t_aircraft);
204 local_update_sky_and_lighting_params();
207 current_options.toggle_panel();
210 t->adjust_warp_delta(-30);
211 local_update_sky_and_lighting_params();
214 #if defined(FX) && !defined(WIN32)
215 global_fullscreen = ( !global_fullscreen );
216 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
217 XMesaSetFXmode( global_fullscreen ?
218 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
223 fov = current_options.get_fov();
225 if ( fov > FG_FOV_MAX ) {
228 current_options.set_fov(fov);
229 v->force_update_fov_math();
232 #ifndef FG_OLD_WEATHER
233 tmp = WeatherDatabase->getWeatherVisibility();
235 WeatherDatabase->setWeatherVisibility( tmp );
237 tmp = current_weather.get_visibility(); // in meters
239 current_weather.set_visibility( tmp );
244 FG_LOG( FG_INPUT, FG_DEBUG, "" );
246 case 50: // numeric keypad 2
247 if ( current_autopilot->get_AltitudeEnabled() ) {
248 current_autopilot->AltitudeAdjust( 100 );
250 controls.move_elevator(-0.05);
253 case 56: // numeric keypad 8
254 if ( current_autopilot->get_AltitudeEnabled() ) {
255 current_autopilot->AltitudeAdjust( -100 );
257 controls.move_elevator(0.05);
260 case 49: // numeric keypad 1
261 controls.move_elevator_trim(-0.001);
263 case 55: // numeric keypad 7
264 controls.move_elevator_trim(0.001);
266 case 52: // numeric keypad 4
267 controls.move_aileron(-0.05);
269 case 54: // numeric keypad 6
270 controls.move_aileron(0.05);
272 case 48: // numeric keypad Ins
273 if ( current_autopilot->get_HeadingEnabled() ) {
274 current_autopilot->HeadingAdjust( -1 );
276 controls.move_rudder(-0.05);
279 case 13: // numeric keypad Enter
280 if ( current_autopilot->get_HeadingEnabled() ) {
281 current_autopilot->HeadingAdjust( 1 );
283 controls.move_rudder(0.05);
286 case 53: // numeric keypad 5
287 controls.set_aileron(0.0);
288 controls.set_elevator(0.0);
289 controls.set_rudder(0.0);
291 case 57: // numeric keypad 9 (Pg Up)
292 if ( current_autopilot->get_AutoThrottleEnabled() ) {
293 current_autopilot->AutoThrottleAdjust( 5 );
295 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
298 case 51: // numeric keypad 3 (Pg Dn)
299 if ( current_autopilot->get_AutoThrottleEnabled() ) {
300 current_autopilot->AutoThrottleAdjust( -5 );
302 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
306 controls.move_flaps(-0.34);
307 FG_LOG( FG_INPUT, FG_INFO,
308 "Set flaps to " << controls.get_flaps() );
311 controls.move_flaps(0.34);
312 FG_LOG( FG_INPUT, FG_INFO,
313 "Set flaps to " << controls.get_flaps() );
316 speed = current_options.get_speed_up();
318 current_options.set_speed_up( speed );
323 b_ret = int( controls.get_brake( 0 ) );
324 b_set = double(!b_ret);
325 controls.set_brake( FGControls::ALL_WHEELS, b_set);
328 if (controls.get_brake(0) > 0.0) {
329 controls.set_brake(0, 0.0);
331 controls.set_brake(0, 1.0);
335 if (controls.get_brake(1) > 0.0) {
336 controls.set_brake(1, 0.0);
338 controls.set_brake(1, 1.0);
342 HUD_masterswitch( true );
345 fgHUDInit(¤t_aircraft); // normal HUD
348 t->adjust_warp (+60);
349 local_update_sky_and_lighting_params();
352 t->togglePauseMode();
355 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
356 f->get_Latitude() * RAD_TO_DEG );
357 FGPath tile_path( current_options.get_fg_root() );
358 tile_path.append( "Scenery" );
359 tile_path.append( p.gen_base_path() );
360 tile_path.append( p.gen_index_str() );
362 // printf position and attitude information
363 FG_LOG( FG_INPUT, FG_INFO,
364 "Lon = " << f->get_Longitude() * RAD_TO_DEG
365 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
366 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
368 FG_LOG( FG_INPUT, FG_INFO,
369 "Heading = " << f->get_Psi() * RAD_TO_DEG
370 << " Roll = " << f->get_Phi() * RAD_TO_DEG
371 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
372 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
376 t->adjust_warp_delta (+30);
377 local_update_sky_and_lighting_params();
380 current_options.cycle_view_mode();
383 fov = current_options.get_fov();
385 if ( fov < FG_FOV_MIN ) {
388 current_options.set_fov(fov);
389 v->force_update_fov_math();
392 #ifndef FG_OLD_WEATHER
393 tmp = WeatherDatabase->getWeatherVisibility();
395 WeatherDatabase->setWeatherVisibility( tmp );
397 tmp = current_weather.get_visibility(); // in meters
399 current_weather.set_visibility( tmp );
403 // if( fg_DebugOutput ) {
404 // fclose( fg_DebugOutput );
406 FG_LOG( FG_INPUT, FG_ALERT,
407 "Program exiting normally at user request." );
415 // Handle "special" keyboard events
416 void GLUTspecialkey(int k, int x, int y) {
421 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
423 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
427 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
428 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
431 ifstream input("fgfs.sav");
432 if (input.good() && fgLoadFlight(input)) {
434 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
436 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
441 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
442 ofstream output("fgfs.sav");
443 if (output.good() && fgSaveFlight(output)) {
445 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
447 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
451 case GLUT_KEY_END: // numeric keypad 1
452 v->set_goal_view_offset( FG_PI * 0.75 );
454 case GLUT_KEY_DOWN: // numeric keypad 2
455 v->set_goal_view_offset( FG_PI );
457 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
458 v->set_goal_view_offset( FG_PI * 1.25 );
460 case GLUT_KEY_LEFT: // numeric keypad 4
461 v->set_goal_view_offset( FG_PI * 0.50 );
463 case GLUT_KEY_RIGHT: // numeric keypad 6
464 v->set_goal_view_offset( FG_PI * 1.50 );
466 case GLUT_KEY_HOME: // numeric keypad 7
467 v->set_goal_view_offset( FG_PI * 0.25 );
469 case GLUT_KEY_UP: // numeric keypad 8
470 v->set_goal_view_offset( 0.00 );
472 case GLUT_KEY_PAGE_UP: // numeric keypad 9
473 v->set_goal_view_offset( FG_PI * 1.75 );
477 FG_LOG( FG_INPUT, FG_DEBUG, "" );
479 case GLUT_KEY_F2: // F2 Reload Tile Cache...
482 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
483 FGTime *t = FGTime::cur_time_params;
484 if( (toggle_pause = !t->getPause()) )
485 t->togglePauseMode();
487 if( global_tile_mgr.init() ) {
488 // Load the local scenery data
489 global_tile_mgr.update();
491 FG_LOG( FG_GENERAL, FG_ALERT,
492 "Error in Tile Manager initialization!" );
497 t->togglePauseMode();
500 case GLUT_KEY_F3: // F3 Take a screen shot
503 case GLUT_KEY_F6: // F6 toggles Autopilot target location
504 if ( current_autopilot->get_HeadingMode() !=
505 FGAutopilot::FG_HEADING_WAYPOINT ) {
506 current_autopilot->set_HeadingMode(
507 FGAutopilot::FG_HEADING_WAYPOINT );
509 current_autopilot->set_HeadingMode(
510 FGAutopilot::FG_HEADING_LOCK );
513 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
514 current_options.cycle_fog();
516 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
517 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
518 } else if ( current_options.get_fog() ==
519 fgOPTIONS::FG_FOG_FASTEST )
521 FG_LOG( FG_INPUT, FG_INFO,
522 "Fog enabled, hint set to fastest" );
523 } else if ( current_options.get_fog() ==
524 fgOPTIONS::FG_FOG_NICEST )
526 FG_LOG( FG_INPUT, FG_INFO,
527 "Fog enabled, hint set to nicest" );
531 case GLUT_KEY_F9: // F9 toggles textures on and off...
532 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
533 if ( current_options.get_textures() ) {
534 current_options.set_textures( false );
535 material_lib.set_step( 1 );
537 current_options.set_textures( true );
538 material_lib.set_step( 0 );
541 case GLUT_KEY_F10: // F10 toggles menu on and off...
542 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
545 case GLUT_KEY_F11: // F11 Altitude Dialog.
546 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
549 case GLUT_KEY_F12: // F12 Heading Dialog...
550 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
554 if ( current_autopilot->get_AltitudeEnabled() ) {
555 current_autopilot->AltitudeAdjust( -100 );
557 controls.move_elevator(0.05);
561 if ( current_autopilot->get_AltitudeEnabled() ) {
562 current_autopilot->AltitudeAdjust( 100 );
564 controls.move_elevator(-0.05);
568 controls.move_aileron(-0.05);
571 controls.move_aileron(0.05);
573 case GLUT_KEY_HOME: // numeric keypad 1
574 controls.move_elevator_trim(0.001);
576 case GLUT_KEY_END: // numeric keypad 7
577 controls.move_elevator_trim(-0.001);
579 case GLUT_KEY_INSERT: // numeric keypad Ins
580 if ( current_autopilot->get_HeadingEnabled() ) {
581 current_autopilot->HeadingAdjust( -1 );
583 controls.move_rudder(-0.05);
586 case 13: // numeric keypad Enter
587 if ( current_autopilot->get_HeadingEnabled() ) {
588 current_autopilot->HeadingAdjust( 1 );
590 controls.move_rudder(0.05);
593 case 53: // numeric keypad 5
594 controls.set_aileron(0.0);
595 controls.set_elevator(0.0);
596 controls.set_rudder(0.0);
598 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
599 if ( current_autopilot->get_AutoThrottleEnabled() ) {
600 current_autopilot->AutoThrottleAdjust( 5 );
602 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
605 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
606 if ( current_autopilot->get_AutoThrottleEnabled() ) {
607 current_autopilot->AutoThrottleAdjust( -5 );
609 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );