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.
35 #if defined(FX) && defined(XMESA)
42 #include <plib/pu.h> // plib include
44 #include <Debug/logstream.hxx>
45 #include <Aircraft/aircraft.hxx>
46 #include <Astro/solarsystem.hxx>
47 #include <Astro/sky.hxx>
48 #include <Autopilot/autopilot.hxx>
49 #include <Cockpit/hud.hxx>
51 #include <Include/fg_constants.h>
52 #include <Misc/fgpath.hxx>
53 #include <Scenery/tilemgr.hxx>
54 #include <Objects/materialmgr.hxx>
55 #include <Time/fg_time.hxx>
56 #include <Time/light.hxx>
58 #ifndef FG_OLD_WEATHER
59 # include <WeatherCM/FGLocalWeatherDatabase.h>
61 # include <Weather/weather.hxx>
64 #include "keyboard.hxx"
65 #include "options.hxx"
69 extern void NewAltitude( puObject *cb );
70 extern void NewHeading( puObject *cb );
72 // Force an update of the sky and lighting parameters
73 static void local_update_sky_and_lighting_params( void ) {
75 SolarSystem::theSolarSystem->rebuild();
76 cur_light_params.Update();
81 // Handle keyboard events
82 void GLUTkey(unsigned char k, int x, int y) {
87 static bool winding_ccw = true;
90 f = current_aircraft.fdm_state;
91 t = FGTime::cur_time_params;
94 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
95 if ( puKeyboard(k, PU_DOWN) ) {
99 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
100 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
102 case 1: // Ctrl-A key
103 fgAPToggleAltitude();
105 case 8: // Ctrl-H key
108 case 18: // Ctrl-R key
110 winding_ccw = !winding_ccw;
112 glFrontFace ( GL_CCW );
114 glFrontFace ( GL_CW );
117 case 19: // Ctrl-S key
118 fgAPToggleAutoThrottle();
120 case 20: // Ctrl-T key
121 fgAPToggleTerrainFollow();
123 case 21: // Ctrl-U key
124 // add 1000' of emergency altitude. Possibly good for
125 // unflipping yourself :-)
127 double alt = cur_fdm_state->get_Altitude() + 1000;
128 fgFDMForceAltitude( current_options.get_flight_model(),
129 alt * FEET_TO_METER );
132 case 49: // numeric keypad 1
133 v->set_goal_view_offset( FG_PI * 0.75 );
135 case 50: // numeric keypad 2
136 v->set_goal_view_offset( FG_PI );
138 case 51: // numeric keypad 3
139 v->set_goal_view_offset( FG_PI * 1.25 );
141 case 52: // numeric keypad 4
142 v->set_goal_view_offset( FG_PI * 0.50 );
144 case 54: // numeric keypad 6
145 v->set_goal_view_offset( FG_PI * 1.50 );
147 case 55: // numeric keypad 7
148 v->set_goal_view_offset( FG_PI * 0.25 );
150 case 56: // numeric keypad 8
151 v->set_goal_view_offset( 0.00 );
153 case 57: // numeric keypad 9
154 v->set_goal_view_offset( FG_PI * 1.75 );
157 speed = current_options.get_speed_up();
162 current_options.set_speed_up( speed );
165 // status = current_options.get_hud_status();
166 // current_options.set_hud_status(!status);
167 HUD_brightkey( true );
171 fgHUDInit2(¤t_aircraft);
175 local_update_sky_and_lighting_params();
178 current_options.toggle_panel();
181 t->adjust_warp_delta(-30);
182 local_update_sky_and_lighting_params();
185 #if defined(FX) && !defined(WIN32)
186 global_fullscreen = ( !global_fullscreen );
187 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
188 XMesaSetFXmode( global_fullscreen ?
189 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
194 fov = current_options.get_fov();
196 if ( fov > FG_FOV_MAX ) {
199 current_options.set_fov(fov);
200 v->force_update_fov_math();
203 #ifndef FG_OLD_WEATHER
204 tmp = WeatherDatabase->getWeatherVisibility();
206 WeatherDatabase->setWeatherVisibility( tmp );
208 tmp = current_weather.get_visibility(); // in meters
210 current_weather.set_visibility( tmp );
215 FG_LOG( FG_INPUT, FG_DEBUG, "" );
217 case 50: // numeric keypad 2
218 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
219 fgAPAltitudeAdjust( 100 );
221 controls.move_elevator(-0.05);
224 case 56: // numeric keypad 8
225 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
226 fgAPAltitudeAdjust( -100 );
228 controls.move_elevator(0.05);
231 case 49: // numeric keypad 1
232 controls.move_elevator_trim(-0.001);
234 case 55: // numeric keypad 7
235 controls.move_elevator_trim(0.001);
237 case 52: // numeric keypad 4
238 controls.move_aileron(-0.05);
240 case 54: // numeric keypad 6
241 controls.move_aileron(0.05);
243 case 48: // numeric keypad Ins
244 if( fgAPHeadingEnabled() ) {
245 fgAPHeadingAdjust( -1 );
247 controls.move_rudder(-0.05);
250 case 13: // numeric keypad Enter
251 if( fgAPHeadingEnabled() ) {
252 fgAPHeadingAdjust( 1 );
254 controls.move_rudder(0.05);
257 case 53: // numeric keypad 5
258 controls.set_aileron(0.0);
259 controls.set_elevator(0.0);
260 controls.set_rudder(0.0);
262 case 57: // numeric keypad 9 (Pg Up)
263 if( fgAPAutoThrottleEnabled() ) {
264 fgAPAutoThrottleAdjust( 5 );
266 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
269 case 51: // numeric keypad 3 (Pg Dn)
270 if( fgAPAutoThrottleEnabled() ) {
271 fgAPAutoThrottleAdjust( -5 );
273 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
277 controls.move_flaps(-0.34);
278 FG_LOG( FG_INPUT, FG_INFO,
279 "Set flaps to " << controls.get_flaps() );
282 controls.move_flaps(0.34);
283 FG_LOG( FG_INPUT, FG_INFO,
284 "Set flaps to " << controls.get_flaps() );
287 speed = current_options.get_speed_up();
289 current_options.set_speed_up( speed );
294 b_ret = int( controls.get_brake( 0 ) );
295 b_set = double(!b_ret);
296 controls.set_brake( FGControls::ALL_WHEELS, b_set);
299 HUD_brightkey( false );
302 fgHUDInit(¤t_aircraft); // normal HUD
305 t->adjust_warp (+60);
306 local_update_sky_and_lighting_params();
309 t->togglePauseMode();
312 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
313 f->get_Latitude() * RAD_TO_DEG );
314 FGPath tile_path( current_options.get_fg_root() );
315 tile_path.append( "Scenery" );
316 tile_path.append( p.gen_base_path() );
317 tile_path.append( p.gen_index_str() );
319 // printf position and attitude information
320 FG_LOG( FG_INPUT, FG_INFO,
321 "Lon = " << f->get_Longitude() * RAD_TO_DEG
322 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
323 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
325 FG_LOG( FG_INPUT, FG_INFO,
326 "Heading = " << f->get_Psi() * RAD_TO_DEG
327 << " Roll = " << f->get_Phi() * RAD_TO_DEG
328 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
329 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
333 t->adjust_warp_delta (+30);
334 local_update_sky_and_lighting_params();
337 current_options.cycle_view_mode();
340 fov = current_options.get_fov();
342 if ( fov < FG_FOV_MIN ) {
345 current_options.set_fov(fov);
346 v->force_update_fov_math();
349 #ifndef FG_OLD_WEATHER
350 tmp = WeatherDatabase->getWeatherVisibility();
352 WeatherDatabase->setWeatherVisibility( tmp );
354 tmp = current_weather.get_visibility(); // in meters
356 current_weather.set_visibility( tmp );
360 // if( fg_DebugOutput ) {
361 // fclose( fg_DebugOutput );
363 FG_LOG( FG_INPUT, FG_ALERT,
364 "Program exiting normally at user request." );
372 // Handle "special" keyboard events
373 void GLUTspecialkey(int k, int x, int y) {
378 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
380 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
384 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
385 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
388 ifstream input("fgfs.sav");
389 if (input.good() && fgLoadFlight(input)) {
391 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
393 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
398 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
399 ofstream output("fgfs.sav");
400 if (output.good() && fgSaveFlight(output)) {
402 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
404 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
408 case GLUT_KEY_END: // numeric keypad 1
409 v->set_goal_view_offset( FG_PI * 0.75 );
411 case GLUT_KEY_DOWN: // numeric keypad 2
412 v->set_goal_view_offset( FG_PI );
414 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
415 v->set_goal_view_offset( FG_PI * 1.25 );
417 case GLUT_KEY_LEFT: // numeric keypad 4
418 v->set_goal_view_offset( FG_PI * 0.50 );
420 case GLUT_KEY_RIGHT: // numeric keypad 6
421 v->set_goal_view_offset( FG_PI * 1.50 );
423 case GLUT_KEY_HOME: // numeric keypad 7
424 v->set_goal_view_offset( FG_PI * 0.25 );
426 case GLUT_KEY_UP: // numeric keypad 8
427 v->set_goal_view_offset( 0.00 );
429 case GLUT_KEY_PAGE_UP: // numeric keypad 9
430 v->set_goal_view_offset( FG_PI * 1.75 );
434 FG_LOG( FG_INPUT, FG_DEBUG, "" );
436 case GLUT_KEY_F2: // F2 Reload Tile Cache...
439 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
440 FGTime *t = FGTime::cur_time_params;
441 if( (toggle_pause = !t->getPause()) )
442 t->togglePauseMode();
444 if( global_tile_mgr.init() ) {
445 // Load the local scenery data
446 global_tile_mgr.update();
448 FG_LOG( FG_GENERAL, FG_ALERT,
449 "Error in Tile Manager initialization!" );
454 t->togglePauseMode();
457 case GLUT_KEY_F3: // F3 Take a screen shot
460 case GLUT_KEY_F6: // F6 toggles Autopilot target location
461 fgAPToggleWayPoint();
463 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
464 current_options.cycle_fog();
466 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
467 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
468 } else if ( current_options.get_fog() ==
469 fgOPTIONS::FG_FOG_FASTEST )
471 FG_LOG( FG_INPUT, FG_INFO,
472 "Fog enabled, hint set to fastest" );
473 } else if ( current_options.get_fog() ==
474 fgOPTIONS::FG_FOG_NICEST )
476 FG_LOG( FG_INPUT, FG_INFO,
477 "Fog enabled, hint set to nicest" );
481 case GLUT_KEY_F9: // F9 toggles textures on and off...
482 if ( material_mgr.loaded() ) {
483 if (current_options.get_textures()) {
484 current_options.set_textures(false);
485 material_mgr.set_step(1);
487 current_options.set_textures(true);
488 material_mgr.set_step(0);
490 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
492 FG_LOG( FG_INPUT, FG_INFO,
493 "No textures loaded, cannot toggle" );
496 case GLUT_KEY_F10: // F10 toggles menu on and off...
497 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
500 case GLUT_KEY_F11: // F11 Altitude Dialog.
501 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
504 case GLUT_KEY_F12: // F12 Heading Dialog...
505 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
509 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
510 fgAPAltitudeAdjust( -100 );
512 controls.move_elevator(0.05);
516 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
517 fgAPAltitudeAdjust( 100 );
519 controls.move_elevator(-0.05);
523 controls.move_aileron(-0.05);
526 controls.move_aileron(0.05);
528 case GLUT_KEY_HOME: // numeric keypad 1
529 controls.move_elevator_trim(0.001);
531 case GLUT_KEY_END: // numeric keypad 7
532 controls.move_elevator_trim(-0.001);
534 case GLUT_KEY_INSERT: // numeric keypad Ins
535 if( fgAPHeadingEnabled() ) {
536 fgAPHeadingAdjust( -1 );
538 controls.move_rudder(-0.05);
541 case 13: // numeric keypad Enter
542 if( fgAPHeadingEnabled() ) {
543 fgAPHeadingAdjust( 1 );
545 controls.move_rudder(0.05);
548 case 53: // numeric keypad 5
549 controls.set_aileron(0.0);
550 controls.set_elevator(0.0);
551 controls.set_rudder(0.0);
553 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
554 if( fgAPAutoThrottleEnabled() ) {
555 fgAPAutoThrottleAdjust( 5 );
557 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
560 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
561 if( fgAPAutoThrottleEnabled() ) {
562 fgAPAutoThrottleAdjust( -5 );
564 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );