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 <Autopilot/autopilot.hxx>
50 #include <Cockpit/hud.hxx>
52 #include <Scenery/tilemgr.hxx>
53 #include <Objects/materialmgr.hxx>
54 #include <Time/fg_time.hxx>
55 #include <Time/light.hxx>
57 #ifndef FG_OLD_WEATHER
58 # include <WeatherCM/FGLocalWeatherDatabase.h>
60 # include <Weather/weather.hxx>
63 #include "keyboard.hxx"
64 #include "options.hxx"
68 extern void NewAltitude( puObject *cb );
69 extern void NewHeading( puObject *cb );
71 // Force an update of the sky and lighting parameters
72 static void local_update_sky_and_lighting_params( void ) {
73 FGTime::cur_time_params->updateLocal();
74 cur_light_params.Update();
78 // Handle keyboard events
79 void GLUTkey(unsigned char k, int x, int y) {
84 static bool winding_ccw = true;
87 f = current_aircraft.fdm_state;
88 t = FGTime::cur_time_params;
91 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
92 if ( puKeyboard(k, PU_DOWN) ) {
96 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
97 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
100 fgAPToggleAltitude();
102 case 8: // Ctrl-H key
105 case 18: // Ctrl-R key
107 winding_ccw = !winding_ccw;
109 glFrontFace ( GL_CCW );
111 glFrontFace ( GL_CW );
114 case 19: // Ctrl-S key
115 fgAPToggleAutoThrottle();
117 case 20: // Ctrl-T key
118 fgAPToggleTerrainFollow();
120 case 21: // Ctrl-U key
121 // add 1000' of emergency altitude. Possibly good for
122 // unflipping yourself :-)
124 double alt = cur_fdm_state->get_Altitude() + 1000;
125 fgFDMForceAltitude( current_options.get_flight_model(),
126 alt * FEET_TO_METER );
129 case 49: // numeric keypad 1
130 v->set_goal_view_offset( FG_PI * 0.75 );
132 case 50: // numeric keypad 2
133 v->set_goal_view_offset( FG_PI );
135 case 51: // numeric keypad 3
136 v->set_goal_view_offset( FG_PI * 1.25 );
138 case 52: // numeric keypad 4
139 v->set_goal_view_offset( FG_PI * 0.50 );
141 case 54: // numeric keypad 6
142 v->set_goal_view_offset( FG_PI * 1.50 );
144 case 55: // numeric keypad 7
145 v->set_goal_view_offset( FG_PI * 0.25 );
147 case 56: // numeric keypad 8
148 v->set_goal_view_offset( 0.00 );
150 case 57: // numeric keypad 9
151 v->set_goal_view_offset( FG_PI * 1.75 );
154 speed = current_options.get_speed_up();
159 current_options.set_speed_up( speed );
162 // status = current_options.get_hud_status();
163 // current_options.set_hud_status(!status);
164 HUD_brightkey( true );
168 fgHUDInit2(¤t_aircraft);
172 local_update_sky_and_lighting_params();
175 current_options.toggle_panel();
178 t->adjust_warp_delta(-30);
179 local_update_sky_and_lighting_params();
182 #if defined(FX) && !defined(WIN32)
183 global_fullscreen = ( !global_fullscreen );
184 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
185 XMesaSetFXmode( global_fullscreen ?
186 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
191 fov = current_options.get_fov();
193 if ( fov > FG_FOV_MAX ) {
196 current_options.set_fov(fov);
197 v->force_update_fov_math();
200 #ifndef FG_OLD_WEATHER
201 tmp = WeatherDatabase->getWeatherVisibility();
203 WeatherDatabase->setWeatherVisibility( tmp );
205 tmp = current_weather.get_visibility(); // in meters
207 current_weather.set_visibility( tmp );
212 FG_LOG( FG_INPUT, FG_DEBUG, "" );
214 case 50: // numeric keypad 2
215 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
216 fgAPAltitudeAdjust( 100 );
218 controls.move_elevator(-0.05);
221 case 56: // numeric keypad 8
222 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
223 fgAPAltitudeAdjust( -100 );
225 controls.move_elevator(0.05);
228 case 49: // numeric keypad 1
229 controls.move_elevator_trim(-0.001);
231 case 55: // numeric keypad 7
232 controls.move_elevator_trim(0.001);
234 case 52: // numeric keypad 4
235 controls.move_aileron(-0.05);
237 case 54: // numeric keypad 6
238 controls.move_aileron(0.05);
240 case 48: // numeric keypad Ins
241 if( fgAPHeadingEnabled() ) {
242 fgAPHeadingAdjust( -1 );
244 controls.move_rudder(-0.05);
247 case 13: // numeric keypad Enter
248 if( fgAPHeadingEnabled() ) {
249 fgAPHeadingAdjust( 1 );
251 controls.move_rudder(0.05);
254 case 53: // numeric keypad 5
255 controls.set_aileron(0.0);
256 controls.set_elevator(0.0);
257 controls.set_rudder(0.0);
259 case 57: // numeric keypad 9 (Pg Up)
260 if( fgAPAutoThrottleEnabled() ) {
261 fgAPAutoThrottleAdjust( 5 );
263 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
266 case 51: // numeric keypad 3 (Pg Dn)
267 if( fgAPAutoThrottleEnabled() ) {
268 fgAPAutoThrottleAdjust( -5 );
270 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
274 controls.move_flaps(-0.34);
275 FG_LOG( FG_INPUT, FG_INFO,
276 "Set flaps to " << controls.get_flaps() );
279 controls.move_flaps(0.34);
280 FG_LOG( FG_INPUT, FG_INFO,
281 "Set flaps to " << controls.get_flaps() );
284 speed = current_options.get_speed_up();
286 current_options.set_speed_up( speed );
291 b_ret = int( controls.get_brake( 0 ) );
292 b_set = double(!b_ret);
293 controls.set_brake( FGControls::ALL_WHEELS, b_set);
296 HUD_brightkey( false );
299 fgHUDInit(¤t_aircraft); // normal HUD
302 t->adjust_warp (+60);
303 local_update_sky_and_lighting_params();
306 t->togglePauseMode();
309 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
310 f->get_Latitude() * RAD_TO_DEG );
311 FGPath tile_path( current_options.get_fg_root() );
312 tile_path.append( "Scenery" );
313 tile_path.append( p.gen_base_path() );
314 tile_path.append( p.gen_index_str() );
316 // printf position and attitude information
317 FG_LOG( FG_INPUT, FG_INFO,
318 "Lon = " << f->get_Longitude() * RAD_TO_DEG
319 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
320 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
322 FG_LOG( FG_INPUT, FG_INFO,
323 "Heading = " << f->get_Psi() * RAD_TO_DEG
324 << " Roll = " << f->get_Phi() * RAD_TO_DEG
325 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
326 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
330 t->adjust_warp_delta (+30);
331 local_update_sky_and_lighting_params();
334 current_options.cycle_view_mode();
337 fov = current_options.get_fov();
339 if ( fov < FG_FOV_MIN ) {
342 current_options.set_fov(fov);
343 v->force_update_fov_math();
346 #ifndef FG_OLD_WEATHER
347 tmp = WeatherDatabase->getWeatherVisibility();
349 WeatherDatabase->setWeatherVisibility( tmp );
351 tmp = current_weather.get_visibility(); // in meters
353 current_weather.set_visibility( tmp );
357 // if( fg_DebugOutput ) {
358 // fclose( fg_DebugOutput );
360 FG_LOG( FG_INPUT, FG_ALERT,
361 "Program exiting normally at user request." );
369 // Handle "special" keyboard events
370 void GLUTspecialkey(int k, int x, int y) {
375 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
377 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
381 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
382 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
385 ifstream input("fgfs.sav");
386 if (input.good() && fgLoadFlight(input)) {
388 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
390 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
395 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
396 ofstream output("fgfs.sav");
397 if (output.good() && fgSaveFlight(output)) {
399 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
401 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
405 case GLUT_KEY_END: // numeric keypad 1
406 v->set_goal_view_offset( FG_PI * 0.75 );
408 case GLUT_KEY_DOWN: // numeric keypad 2
409 v->set_goal_view_offset( FG_PI );
411 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
412 v->set_goal_view_offset( FG_PI * 1.25 );
414 case GLUT_KEY_LEFT: // numeric keypad 4
415 v->set_goal_view_offset( FG_PI * 0.50 );
417 case GLUT_KEY_RIGHT: // numeric keypad 6
418 v->set_goal_view_offset( FG_PI * 1.50 );
420 case GLUT_KEY_HOME: // numeric keypad 7
421 v->set_goal_view_offset( FG_PI * 0.25 );
423 case GLUT_KEY_UP: // numeric keypad 8
424 v->set_goal_view_offset( 0.00 );
426 case GLUT_KEY_PAGE_UP: // numeric keypad 9
427 v->set_goal_view_offset( FG_PI * 1.75 );
431 FG_LOG( FG_INPUT, FG_DEBUG, "" );
433 case GLUT_KEY_F2: // F2 Reload Tile Cache...
436 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
437 FGTime *t = FGTime::cur_time_params;
438 if( (toggle_pause = !t->getPause()) )
439 t->togglePauseMode();
441 if( global_tile_mgr.init() ) {
442 // Load the local scenery data
443 global_tile_mgr.update();
445 FG_LOG( FG_GENERAL, FG_ALERT,
446 "Error in Tile Manager initialization!" );
451 t->togglePauseMode();
454 case GLUT_KEY_F3: // F3 Take a screen shot
457 case GLUT_KEY_F6: // F6 toggles Autopilot target location
458 fgAPToggleWayPoint();
460 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
461 current_options.cycle_fog();
463 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
464 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
465 } else if ( current_options.get_fog() ==
466 fgOPTIONS::FG_FOG_FASTEST )
468 FG_LOG( FG_INPUT, FG_INFO,
469 "Fog enabled, hint set to fastest" );
470 } else if ( current_options.get_fog() ==
471 fgOPTIONS::FG_FOG_NICEST )
473 FG_LOG( FG_INPUT, FG_INFO,
474 "Fog enabled, hint set to nicest" );
478 case GLUT_KEY_F9: // F9 toggles textures on and off...
479 if ( material_mgr.loaded() ) {
480 if (current_options.get_textures()) {
481 current_options.set_textures(false);
482 material_mgr.set_step(1);
484 current_options.set_textures(true);
485 material_mgr.set_step(0);
487 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
489 FG_LOG( FG_INPUT, FG_INFO,
490 "No textures loaded, cannot toggle" );
493 case GLUT_KEY_F10: // F10 toggles menu on and off...
494 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
497 case GLUT_KEY_F11: // F11 Altitude Dialog.
498 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
501 case GLUT_KEY_F12: // F12 Heading Dialog...
502 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
506 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
507 fgAPAltitudeAdjust( -100 );
509 controls.move_elevator(0.05);
513 if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
514 fgAPAltitudeAdjust( 100 );
516 controls.move_elevator(-0.05);
520 controls.move_aileron(-0.05);
523 controls.move_aileron(0.05);
525 case GLUT_KEY_HOME: // numeric keypad 1
526 controls.move_elevator_trim(0.001);
528 case GLUT_KEY_END: // numeric keypad 7
529 controls.move_elevator_trim(-0.001);
531 case GLUT_KEY_INSERT: // numeric keypad Ins
532 if( fgAPHeadingEnabled() ) {
533 fgAPHeadingAdjust( -1 );
535 controls.move_rudder(-0.05);
538 case 13: // numeric keypad Enter
539 if( fgAPHeadingEnabled() ) {
540 fgAPHeadingAdjust( 1 );
542 controls.move_rudder(0.05);
545 case 53: // numeric keypad 5
546 controls.set_aileron(0.0);
547 controls.set_elevator(0.0);
548 controls.set_rudder(0.0);
550 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
551 if( fgAPAutoThrottleEnabled() ) {
552 fgAPAutoThrottleAdjust( 5 );
554 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
557 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
558 if( fgAPAutoThrottleEnabled() ) {
559 fgAPAutoThrottleAdjust( -5 );
561 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );