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/materialmgr.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 HUD_masterswitch( true );
331 fgHUDInit(¤t_aircraft); // normal HUD
334 t->adjust_warp (+60);
335 local_update_sky_and_lighting_params();
338 t->togglePauseMode();
341 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
342 f->get_Latitude() * RAD_TO_DEG );
343 FGPath tile_path( current_options.get_fg_root() );
344 tile_path.append( "Scenery" );
345 tile_path.append( p.gen_base_path() );
346 tile_path.append( p.gen_index_str() );
348 // printf position and attitude information
349 FG_LOG( FG_INPUT, FG_INFO,
350 "Lon = " << f->get_Longitude() * RAD_TO_DEG
351 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
352 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
354 FG_LOG( FG_INPUT, FG_INFO,
355 "Heading = " << f->get_Psi() * RAD_TO_DEG
356 << " Roll = " << f->get_Phi() * RAD_TO_DEG
357 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
358 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
362 t->adjust_warp_delta (+30);
363 local_update_sky_and_lighting_params();
366 current_options.cycle_view_mode();
369 fov = current_options.get_fov();
371 if ( fov < FG_FOV_MIN ) {
374 current_options.set_fov(fov);
375 v->force_update_fov_math();
378 #ifndef FG_OLD_WEATHER
379 tmp = WeatherDatabase->getWeatherVisibility();
381 WeatherDatabase->setWeatherVisibility( tmp );
383 tmp = current_weather.get_visibility(); // in meters
385 current_weather.set_visibility( tmp );
389 // if( fg_DebugOutput ) {
390 // fclose( fg_DebugOutput );
392 FG_LOG( FG_INPUT, FG_ALERT,
393 "Program exiting normally at user request." );
401 // Handle "special" keyboard events
402 void GLUTspecialkey(int k, int x, int y) {
407 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
409 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
413 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
414 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
417 ifstream input("fgfs.sav");
418 if (input.good() && fgLoadFlight(input)) {
420 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
422 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
427 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
428 ofstream output("fgfs.sav");
429 if (output.good() && fgSaveFlight(output)) {
431 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
433 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
437 case GLUT_KEY_END: // numeric keypad 1
438 v->set_goal_view_offset( FG_PI * 0.75 );
440 case GLUT_KEY_DOWN: // numeric keypad 2
441 v->set_goal_view_offset( FG_PI );
443 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
444 v->set_goal_view_offset( FG_PI * 1.25 );
446 case GLUT_KEY_LEFT: // numeric keypad 4
447 v->set_goal_view_offset( FG_PI * 0.50 );
449 case GLUT_KEY_RIGHT: // numeric keypad 6
450 v->set_goal_view_offset( FG_PI * 1.50 );
452 case GLUT_KEY_HOME: // numeric keypad 7
453 v->set_goal_view_offset( FG_PI * 0.25 );
455 case GLUT_KEY_UP: // numeric keypad 8
456 v->set_goal_view_offset( 0.00 );
458 case GLUT_KEY_PAGE_UP: // numeric keypad 9
459 v->set_goal_view_offset( FG_PI * 1.75 );
463 FG_LOG( FG_INPUT, FG_DEBUG, "" );
465 case GLUT_KEY_F2: // F2 Reload Tile Cache...
468 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
469 FGTime *t = FGTime::cur_time_params;
470 if( (toggle_pause = !t->getPause()) )
471 t->togglePauseMode();
473 if( global_tile_mgr.init() ) {
474 // Load the local scenery data
475 global_tile_mgr.update();
477 FG_LOG( FG_GENERAL, FG_ALERT,
478 "Error in Tile Manager initialization!" );
483 t->togglePauseMode();
486 case GLUT_KEY_F3: // F3 Take a screen shot
489 case GLUT_KEY_F6: // F6 toggles Autopilot target location
490 if ( current_autopilot->get_HeadingMode() !=
491 FGAutopilot::FG_HEADING_WAYPOINT ) {
492 current_autopilot->set_HeadingMode(
493 FGAutopilot::FG_HEADING_WAYPOINT );
495 current_autopilot->set_HeadingMode(
496 FGAutopilot::FG_HEADING_LOCK );
499 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
500 current_options.cycle_fog();
502 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
503 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
504 } else if ( current_options.get_fog() ==
505 fgOPTIONS::FG_FOG_FASTEST )
507 FG_LOG( FG_INPUT, FG_INFO,
508 "Fog enabled, hint set to fastest" );
509 } else if ( current_options.get_fog() ==
510 fgOPTIONS::FG_FOG_NICEST )
512 FG_LOG( FG_INPUT, FG_INFO,
513 "Fog enabled, hint set to nicest" );
517 case GLUT_KEY_F9: // F9 toggles textures on and off...
518 if ( material_mgr.loaded() ) {
519 if (current_options.get_textures()) {
520 current_options.set_textures(false);
521 material_mgr.set_step(1);
523 current_options.set_textures(true);
524 material_mgr.set_step(0);
526 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
528 FG_LOG( FG_INPUT, FG_INFO,
529 "No textures loaded, cannot toggle" );
532 case GLUT_KEY_F10: // F10 toggles menu on and off...
533 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
536 case GLUT_KEY_F11: // F11 Altitude Dialog.
537 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
540 case GLUT_KEY_F12: // F12 Heading Dialog...
541 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
545 if ( current_autopilot->get_AltitudeEnabled() ) {
546 current_autopilot->AltitudeAdjust( -100 );
548 controls.move_elevator(0.05);
552 if ( current_autopilot->get_AltitudeEnabled() ) {
553 current_autopilot->AltitudeAdjust( 100 );
555 controls.move_elevator(-0.05);
559 controls.move_aileron(-0.05);
562 controls.move_aileron(0.05);
564 case GLUT_KEY_HOME: // numeric keypad 1
565 controls.move_elevator_trim(0.001);
567 case GLUT_KEY_END: // numeric keypad 7
568 controls.move_elevator_trim(-0.001);
570 case GLUT_KEY_INSERT: // numeric keypad Ins
571 if ( current_autopilot->get_HeadingEnabled() ) {
572 current_autopilot->HeadingAdjust( -1 );
574 controls.move_rudder(-0.05);
577 case 13: // numeric keypad Enter
578 if ( current_autopilot->get_HeadingEnabled() ) {
579 current_autopilot->HeadingAdjust( 1 );
581 controls.move_rudder(0.05);
584 case 53: // numeric keypad 5
585 controls.set_aileron(0.0);
586 controls.set_elevator(0.0);
587 controls.set_rudder(0.0);
589 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
590 if ( current_autopilot->get_AutoThrottleEnabled() ) {
591 current_autopilot->AutoThrottleAdjust( 5 );
593 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
596 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
597 if ( current_autopilot->get_AutoThrottleEnabled() ) {
598 current_autopilot->AutoThrottleAdjust( -5 );
600 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );