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/xgl.h>
35 #if defined(FX) && defined(XMESA)
42 #include <plib/pu.h> // plib include
44 #include <simgear/constants.h>
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/misc/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();
78 current_sky.repaint( cur_light_params.sky_color,
79 cur_light_params.fog_color,
80 cur_light_params.sun_angle );
84 // Handle keyboard events
85 void GLUTkey(unsigned char k, int x, int y) {
90 static bool winding_ccw = true;
93 f = current_aircraft.fdm_state;
94 t = FGTime::cur_time_params;
97 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
98 if ( puKeyboard(k, PU_DOWN) ) {
102 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
103 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
105 case 1: // Ctrl-A key
106 fgAPToggleAltitude();
108 case 8: // Ctrl-H key
111 case 18: // Ctrl-R key
113 winding_ccw = !winding_ccw;
115 glFrontFace ( GL_CCW );
117 glFrontFace ( GL_CW );
120 case 19: // Ctrl-S key
121 fgAPToggleAutoThrottle();
123 case 20: // Ctrl-T key
124 fgAPToggleTerrainFollow();
126 case 21: // Ctrl-U key
127 // add 1000' of emergency altitude. Possibly good for
128 // unflipping yourself :-)
130 double alt = cur_fdm_state->get_Altitude() + 1000;
131 fgFDMForceAltitude( current_options.get_flight_model(),
132 alt * FEET_TO_METER );
135 case 49: // numeric keypad 1
136 v->set_goal_view_offset( FG_PI * 0.75 );
138 case 50: // numeric keypad 2
139 v->set_goal_view_offset( FG_PI );
141 case 51: // numeric keypad 3
142 v->set_goal_view_offset( FG_PI * 1.25 );
144 case 52: // numeric keypad 4
145 v->set_goal_view_offset( FG_PI * 0.50 );
147 case 54: // numeric keypad 6
148 v->set_goal_view_offset( FG_PI * 1.50 );
150 case 55: // numeric keypad 7
151 v->set_goal_view_offset( FG_PI * 0.25 );
153 case 56: // numeric keypad 8
154 v->set_goal_view_offset( 0.00 );
156 case 57: // numeric keypad 9
157 v->set_goal_view_offset( FG_PI * 1.75 );
160 speed = current_options.get_speed_up();
165 current_options.set_speed_up( speed );
168 // status = current_options.get_hud_status();
169 // current_options.set_hud_status(!status);
170 HUD_brightkey( true );
174 fgHUDInit2(¤t_aircraft);
178 local_update_sky_and_lighting_params();
181 current_options.toggle_panel();
184 t->adjust_warp_delta(-30);
185 local_update_sky_and_lighting_params();
188 #if defined(FX) && !defined(WIN32)
189 global_fullscreen = ( !global_fullscreen );
190 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
191 XMesaSetFXmode( global_fullscreen ?
192 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
197 fov = current_options.get_fov();
199 if ( fov > FG_FOV_MAX ) {
202 current_options.set_fov(fov);
203 v->force_update_fov_math();
206 #ifndef FG_OLD_WEATHER
207 tmp = WeatherDatabase->getWeatherVisibility();
209 WeatherDatabase->setWeatherVisibility( tmp );
211 tmp = current_weather.get_visibility(); // in meters
213 current_weather.set_visibility( tmp );
218 FG_LOG( FG_INPUT, FG_DEBUG, "" );
220 case 50: // numeric keypad 2
221 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
222 fgAPAltitudeAdjust( 100 );
224 controls.move_elevator(-0.05);
227 case 56: // numeric keypad 8
228 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
229 fgAPAltitudeAdjust( -100 );
231 controls.move_elevator(0.05);
234 case 49: // numeric keypad 1
235 controls.move_elevator_trim(-0.001);
237 case 55: // numeric keypad 7
238 controls.move_elevator_trim(0.001);
240 case 52: // numeric keypad 4
241 controls.move_aileron(-0.05);
243 case 54: // numeric keypad 6
244 controls.move_aileron(0.05);
246 case 48: // numeric keypad Ins
247 if( fgAPHeadingEnabled() ) {
248 fgAPHeadingAdjust( -1 );
250 controls.move_rudder(-0.05);
253 case 13: // numeric keypad Enter
254 if( fgAPHeadingEnabled() ) {
255 fgAPHeadingAdjust( 1 );
257 controls.move_rudder(0.05);
260 case 53: // numeric keypad 5
261 controls.set_aileron(0.0);
262 controls.set_elevator(0.0);
263 controls.set_rudder(0.0);
265 case 57: // numeric keypad 9 (Pg Up)
266 if( fgAPAutoThrottleEnabled() ) {
267 fgAPAutoThrottleAdjust( 5 );
269 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
272 case 51: // numeric keypad 3 (Pg Dn)
273 if( fgAPAutoThrottleEnabled() ) {
274 fgAPAutoThrottleAdjust( -5 );
276 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
280 controls.move_flaps(-0.34);
281 FG_LOG( FG_INPUT, FG_INFO,
282 "Set flaps to " << controls.get_flaps() );
285 controls.move_flaps(0.34);
286 FG_LOG( FG_INPUT, FG_INFO,
287 "Set flaps to " << controls.get_flaps() );
290 speed = current_options.get_speed_up();
292 current_options.set_speed_up( speed );
297 b_ret = int( controls.get_brake( 0 ) );
298 b_set = double(!b_ret);
299 controls.set_brake( FGControls::ALL_WHEELS, b_set);
302 HUD_brightkey( false );
305 fgHUDInit(¤t_aircraft); // normal HUD
308 t->adjust_warp (+60);
309 local_update_sky_and_lighting_params();
312 t->togglePauseMode();
315 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
316 f->get_Latitude() * RAD_TO_DEG );
317 FGPath tile_path( current_options.get_fg_root() );
318 tile_path.append( "Scenery" );
319 tile_path.append( p.gen_base_path() );
320 tile_path.append( p.gen_index_str() );
322 // printf position and attitude information
323 FG_LOG( FG_INPUT, FG_INFO,
324 "Lon = " << f->get_Longitude() * RAD_TO_DEG
325 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
326 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
328 FG_LOG( FG_INPUT, FG_INFO,
329 "Heading = " << f->get_Psi() * RAD_TO_DEG
330 << " Roll = " << f->get_Phi() * RAD_TO_DEG
331 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
332 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
336 t->adjust_warp_delta (+30);
337 local_update_sky_and_lighting_params();
340 current_options.cycle_view_mode();
343 fov = current_options.get_fov();
345 if ( fov < FG_FOV_MIN ) {
348 current_options.set_fov(fov);
349 v->force_update_fov_math();
352 #ifndef FG_OLD_WEATHER
353 tmp = WeatherDatabase->getWeatherVisibility();
355 WeatherDatabase->setWeatherVisibility( tmp );
357 tmp = current_weather.get_visibility(); // in meters
359 current_weather.set_visibility( tmp );
363 // if( fg_DebugOutput ) {
364 // fclose( fg_DebugOutput );
366 FG_LOG( FG_INPUT, FG_ALERT,
367 "Program exiting normally at user request." );
375 // Handle "special" keyboard events
376 void GLUTspecialkey(int k, int x, int y) {
381 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
383 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
387 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
388 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
391 ifstream input("fgfs.sav");
392 if (input.good() && fgLoadFlight(input)) {
394 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
396 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
401 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
402 ofstream output("fgfs.sav");
403 if (output.good() && fgSaveFlight(output)) {
405 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
407 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
411 case GLUT_KEY_END: // numeric keypad 1
412 v->set_goal_view_offset( FG_PI * 0.75 );
414 case GLUT_KEY_DOWN: // numeric keypad 2
415 v->set_goal_view_offset( FG_PI );
417 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
418 v->set_goal_view_offset( FG_PI * 1.25 );
420 case GLUT_KEY_LEFT: // numeric keypad 4
421 v->set_goal_view_offset( FG_PI * 0.50 );
423 case GLUT_KEY_RIGHT: // numeric keypad 6
424 v->set_goal_view_offset( FG_PI * 1.50 );
426 case GLUT_KEY_HOME: // numeric keypad 7
427 v->set_goal_view_offset( FG_PI * 0.25 );
429 case GLUT_KEY_UP: // numeric keypad 8
430 v->set_goal_view_offset( 0.00 );
432 case GLUT_KEY_PAGE_UP: // numeric keypad 9
433 v->set_goal_view_offset( FG_PI * 1.75 );
437 FG_LOG( FG_INPUT, FG_DEBUG, "" );
439 case GLUT_KEY_F2: // F2 Reload Tile Cache...
442 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
443 FGTime *t = FGTime::cur_time_params;
444 if( (toggle_pause = !t->getPause()) )
445 t->togglePauseMode();
447 if( global_tile_mgr.init() ) {
448 // Load the local scenery data
449 global_tile_mgr.update();
451 FG_LOG( FG_GENERAL, FG_ALERT,
452 "Error in Tile Manager initialization!" );
457 t->togglePauseMode();
460 case GLUT_KEY_F3: // F3 Take a screen shot
463 case GLUT_KEY_F6: // F6 toggles Autopilot target location
464 fgAPToggleWayPoint();
466 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
467 current_options.cycle_fog();
469 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
470 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
471 } else if ( current_options.get_fog() ==
472 fgOPTIONS::FG_FOG_FASTEST )
474 FG_LOG( FG_INPUT, FG_INFO,
475 "Fog enabled, hint set to fastest" );
476 } else if ( current_options.get_fog() ==
477 fgOPTIONS::FG_FOG_NICEST )
479 FG_LOG( FG_INPUT, FG_INFO,
480 "Fog enabled, hint set to nicest" );
484 case GLUT_KEY_F9: // F9 toggles textures on and off...
485 if ( material_mgr.loaded() ) {
486 if (current_options.get_textures()) {
487 current_options.set_textures(false);
488 material_mgr.set_step(1);
490 current_options.set_textures(true);
491 material_mgr.set_step(0);
493 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
495 FG_LOG( FG_INPUT, FG_INFO,
496 "No textures loaded, cannot toggle" );
499 case GLUT_KEY_F10: // F10 toggles menu on and off...
500 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
503 case GLUT_KEY_F11: // F11 Altitude Dialog.
504 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
507 case GLUT_KEY_F12: // F12 Heading Dialog...
508 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
512 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
513 fgAPAltitudeAdjust( -100 );
515 controls.move_elevator(0.05);
519 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
520 fgAPAltitudeAdjust( 100 );
522 controls.move_elevator(-0.05);
526 controls.move_aileron(-0.05);
529 controls.move_aileron(0.05);
531 case GLUT_KEY_HOME: // numeric keypad 1
532 controls.move_elevator_trim(0.001);
534 case GLUT_KEY_END: // numeric keypad 7
535 controls.move_elevator_trim(-0.001);
537 case GLUT_KEY_INSERT: // numeric keypad Ins
538 if( fgAPHeadingEnabled() ) {
539 fgAPHeadingAdjust( -1 );
541 controls.move_rudder(-0.05);
544 case 13: // numeric keypad Enter
545 if( fgAPHeadingEnabled() ) {
546 fgAPHeadingAdjust( 1 );
548 controls.move_rudder(0.05);
551 case 53: // numeric keypad 5
552 controls.set_aileron(0.0);
553 controls.set_elevator(0.0);
554 controls.set_rudder(0.0);
556 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
557 if( fgAPAutoThrottleEnabled() ) {
558 fgAPAutoThrottleAdjust( 5 );
560 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
563 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
564 if( fgAPAutoThrottleEnabled() ) {
565 fgAPAutoThrottleAdjust( -5 );
567 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );