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.
33 #include <simgear/xgl.h>
35 #if defined(FX) && defined(XMESA)
42 #include <plib/pu.h> // plib include
44 #include <simgear/logstream.hxx>
45 #include <simgear/constants.h>
46 #include <simgear/fgpath.hxx>
48 #include <Aircraft/aircraft.hxx>
49 #include <Astro/solarsystem.hxx>
50 #include <Astro/sky.hxx>
51 #include <Autopilot/autopilot.hxx>
52 #include <Cockpit/hud.hxx>
54 #include <Scenery/tilemgr.hxx>
55 #include <Objects/materialmgr.hxx>
56 #include <Time/fg_time.hxx>
57 #include <Time/light.hxx>
59 #ifndef FG_OLD_WEATHER
60 # include <WeatherCM/FGLocalWeatherDatabase.h>
62 # include <Weather/weather.hxx>
65 #include "keyboard.hxx"
66 #include "options.hxx"
70 extern void NewAltitude( puObject *cb );
71 extern void NewHeading( puObject *cb );
73 // Force an update of the sky and lighting parameters
74 static void local_update_sky_and_lighting_params( void ) {
76 SolarSystem::theSolarSystem->rebuild();
77 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 fgAPToggleAltitude();
106 case 8: // Ctrl-H key
109 case 18: // Ctrl-R key
111 winding_ccw = !winding_ccw;
113 glFrontFace ( GL_CCW );
115 glFrontFace ( GL_CW );
118 case 19: // Ctrl-S key
119 fgAPToggleAutoThrottle();
121 case 20: // Ctrl-T key
122 fgAPToggleTerrainFollow();
124 case 21: // Ctrl-U key
125 // add 1000' of emergency altitude. Possibly good for
126 // unflipping yourself :-)
128 double alt = cur_fdm_state->get_Altitude() + 1000;
129 fgFDMForceAltitude( current_options.get_flight_model(),
130 alt * FEET_TO_METER );
133 case 49: // numeric keypad 1
134 v->set_goal_view_offset( FG_PI * 0.75 );
136 case 50: // numeric keypad 2
137 v->set_goal_view_offset( FG_PI );
139 case 51: // numeric keypad 3
140 v->set_goal_view_offset( FG_PI * 1.25 );
142 case 52: // numeric keypad 4
143 v->set_goal_view_offset( FG_PI * 0.50 );
145 case 54: // numeric keypad 6
146 v->set_goal_view_offset( FG_PI * 1.50 );
148 case 55: // numeric keypad 7
149 v->set_goal_view_offset( FG_PI * 0.25 );
151 case 56: // numeric keypad 8
152 v->set_goal_view_offset( 0.00 );
154 case 57: // numeric keypad 9
155 v->set_goal_view_offset( FG_PI * 1.75 );
158 speed = current_options.get_speed_up();
163 current_options.set_speed_up( speed );
166 // status = current_options.get_hud_status();
167 // current_options.set_hud_status(!status);
168 HUD_brightkey( true );
172 fgHUDInit2(¤t_aircraft);
176 local_update_sky_and_lighting_params();
179 current_options.toggle_panel();
182 t->adjust_warp_delta(-30);
183 local_update_sky_and_lighting_params();
186 #if defined(FX) && !defined(WIN32)
187 global_fullscreen = ( !global_fullscreen );
188 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
189 XMesaSetFXmode( global_fullscreen ?
190 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
195 fov = current_options.get_fov();
197 if ( fov > FG_FOV_MAX ) {
200 current_options.set_fov(fov);
201 v->force_update_fov_math();
204 #ifndef FG_OLD_WEATHER
205 tmp = WeatherDatabase->getWeatherVisibility();
207 WeatherDatabase->setWeatherVisibility( tmp );
209 tmp = current_weather.get_visibility(); // in meters
211 current_weather.set_visibility( tmp );
216 FG_LOG( FG_INPUT, FG_DEBUG, "" );
218 case 50: // numeric keypad 2
219 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
220 fgAPAltitudeAdjust( 100 );
222 controls.move_elevator(-0.05);
225 case 56: // numeric keypad 8
226 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
227 fgAPAltitudeAdjust( -100 );
229 controls.move_elevator(0.05);
232 case 49: // numeric keypad 1
233 controls.move_elevator_trim(-0.001);
235 case 55: // numeric keypad 7
236 controls.move_elevator_trim(0.001);
238 case 52: // numeric keypad 4
239 controls.move_aileron(-0.05);
241 case 54: // numeric keypad 6
242 controls.move_aileron(0.05);
244 case 48: // numeric keypad Ins
245 if( fgAPHeadingEnabled() ) {
246 fgAPHeadingAdjust( -1 );
248 controls.move_rudder(-0.05);
251 case 13: // numeric keypad Enter
252 if( fgAPHeadingEnabled() ) {
253 fgAPHeadingAdjust( 1 );
255 controls.move_rudder(0.05);
258 case 53: // numeric keypad 5
259 controls.set_aileron(0.0);
260 controls.set_elevator(0.0);
261 controls.set_rudder(0.0);
263 case 57: // numeric keypad 9 (Pg Up)
264 if( fgAPAutoThrottleEnabled() ) {
265 fgAPAutoThrottleAdjust( 5 );
267 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
270 case 51: // numeric keypad 3 (Pg Dn)
271 if( fgAPAutoThrottleEnabled() ) {
272 fgAPAutoThrottleAdjust( -5 );
274 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
278 controls.move_flaps(-0.34);
279 FG_LOG( FG_INPUT, FG_INFO,
280 "Set flaps to " << controls.get_flaps() );
283 controls.move_flaps(0.34);
284 FG_LOG( FG_INPUT, FG_INFO,
285 "Set flaps to " << controls.get_flaps() );
288 speed = current_options.get_speed_up();
290 current_options.set_speed_up( speed );
295 b_ret = int( controls.get_brake( 0 ) );
296 b_set = double(!b_ret);
297 controls.set_brake( FGControls::ALL_WHEELS, b_set);
300 HUD_brightkey( false );
303 fgHUDInit(¤t_aircraft); // normal HUD
306 t->adjust_warp (+60);
307 local_update_sky_and_lighting_params();
310 t->togglePauseMode();
313 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
314 f->get_Latitude() * RAD_TO_DEG );
315 FGPath tile_path( current_options.get_fg_root() );
316 tile_path.append( "Scenery" );
317 tile_path.append( p.gen_base_path() );
318 tile_path.append( p.gen_index_str() );
320 // printf position and attitude information
321 FG_LOG( FG_INPUT, FG_INFO,
322 "Lon = " << f->get_Longitude() * RAD_TO_DEG
323 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
324 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
326 FG_LOG( FG_INPUT, FG_INFO,
327 "Heading = " << f->get_Psi() * RAD_TO_DEG
328 << " Roll = " << f->get_Phi() * RAD_TO_DEG
329 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
330 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
334 t->adjust_warp_delta (+30);
335 local_update_sky_and_lighting_params();
338 current_options.cycle_view_mode();
341 fov = current_options.get_fov();
343 if ( fov < FG_FOV_MIN ) {
346 current_options.set_fov(fov);
347 v->force_update_fov_math();
350 #ifndef FG_OLD_WEATHER
351 tmp = WeatherDatabase->getWeatherVisibility();
353 WeatherDatabase->setWeatherVisibility( tmp );
355 tmp = current_weather.get_visibility(); // in meters
357 current_weather.set_visibility( tmp );
361 // if( fg_DebugOutput ) {
362 // fclose( fg_DebugOutput );
364 FG_LOG( FG_INPUT, FG_ALERT,
365 "Program exiting normally at user request." );
373 // Handle "special" keyboard events
374 void GLUTspecialkey(int k, int x, int y) {
379 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
381 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
385 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
386 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
389 ifstream input("fgfs.sav");
390 if (input.good() && fgLoadFlight(input)) {
392 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
394 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
399 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
400 ofstream output("fgfs.sav");
401 if (output.good() && fgSaveFlight(output)) {
403 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
405 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
409 case GLUT_KEY_END: // numeric keypad 1
410 v->set_goal_view_offset( FG_PI * 0.75 );
412 case GLUT_KEY_DOWN: // numeric keypad 2
413 v->set_goal_view_offset( FG_PI );
415 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
416 v->set_goal_view_offset( FG_PI * 1.25 );
418 case GLUT_KEY_LEFT: // numeric keypad 4
419 v->set_goal_view_offset( FG_PI * 0.50 );
421 case GLUT_KEY_RIGHT: // numeric keypad 6
422 v->set_goal_view_offset( FG_PI * 1.50 );
424 case GLUT_KEY_HOME: // numeric keypad 7
425 v->set_goal_view_offset( FG_PI * 0.25 );
427 case GLUT_KEY_UP: // numeric keypad 8
428 v->set_goal_view_offset( 0.00 );
430 case GLUT_KEY_PAGE_UP: // numeric keypad 9
431 v->set_goal_view_offset( FG_PI * 1.75 );
435 FG_LOG( FG_INPUT, FG_DEBUG, "" );
437 case GLUT_KEY_F2: // F2 Reload Tile Cache...
440 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
441 FGTime *t = FGTime::cur_time_params;
442 if( (toggle_pause = !t->getPause()) )
443 t->togglePauseMode();
445 if( global_tile_mgr.init() ) {
446 // Load the local scenery data
447 global_tile_mgr.update();
449 FG_LOG( FG_GENERAL, FG_ALERT,
450 "Error in Tile Manager initialization!" );
455 t->togglePauseMode();
458 case GLUT_KEY_F3: // F3 Take a screen shot
461 case GLUT_KEY_F6: // F6 toggles Autopilot target location
462 fgAPToggleWayPoint();
464 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
465 current_options.cycle_fog();
467 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
468 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
469 } else if ( current_options.get_fog() ==
470 fgOPTIONS::FG_FOG_FASTEST )
472 FG_LOG( FG_INPUT, FG_INFO,
473 "Fog enabled, hint set to fastest" );
474 } else if ( current_options.get_fog() ==
475 fgOPTIONS::FG_FOG_NICEST )
477 FG_LOG( FG_INPUT, FG_INFO,
478 "Fog enabled, hint set to nicest" );
482 case GLUT_KEY_F9: // F9 toggles textures on and off...
483 if ( material_mgr.loaded() ) {
484 if (current_options.get_textures()) {
485 current_options.set_textures(false);
486 material_mgr.set_step(1);
488 current_options.set_textures(true);
489 material_mgr.set_step(0);
491 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
493 FG_LOG( FG_INPUT, FG_INFO,
494 "No textures loaded, cannot toggle" );
497 case GLUT_KEY_F10: // F10 toggles menu on and off...
498 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
501 case GLUT_KEY_F11: // F11 Altitude Dialog.
502 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
505 case GLUT_KEY_F12: // F12 Heading Dialog...
506 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
510 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
511 fgAPAltitudeAdjust( -100 );
513 controls.move_elevator(0.05);
517 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
518 fgAPAltitudeAdjust( 100 );
520 controls.move_elevator(-0.05);
524 controls.move_aileron(-0.05);
527 controls.move_aileron(0.05);
529 case GLUT_KEY_HOME: // numeric keypad 1
530 controls.move_elevator_trim(0.001);
532 case GLUT_KEY_END: // numeric keypad 7
533 controls.move_elevator_trim(-0.001);
535 case GLUT_KEY_INSERT: // numeric keypad Ins
536 if( fgAPHeadingEnabled() ) {
537 fgAPHeadingAdjust( -1 );
539 controls.move_rudder(-0.05);
542 case 13: // numeric keypad Enter
543 if( fgAPHeadingEnabled() ) {
544 fgAPHeadingAdjust( 1 );
546 controls.move_rudder(0.05);
549 case 53: // numeric keypad 5
550 controls.set_aileron(0.0);
551 controls.set_elevator(0.0);
552 controls.set_rudder(0.0);
554 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
555 if( fgAPAutoThrottleEnabled() ) {
556 fgAPAutoThrottleAdjust( 5 );
558 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
561 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
562 if( fgAPAutoThrottleEnabled() ) {
563 fgAPAutoThrottleAdjust( -5 );
565 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );