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/auto_gui.hxx>
50 #include <Autopilot/newauto.hxx>
51 #include <Cockpit/hud.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"
70 // Force an update of the sky and lighting parameters
71 static void local_update_sky_and_lighting_params( void ) {
72 FGTime::cur_time_params->updateLocal();
73 cur_light_params.Update();
77 // Handle keyboard events
78 void GLUTkey(unsigned char k, int x, int y) {
83 static bool winding_ccw = true;
86 f = current_aircraft.fdm_state;
87 t = FGTime::cur_time_params;
90 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
91 if ( puKeyboard(k, PU_DOWN) ) {
95 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
96 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
99 current_autopilot->set_AltitudeMode(
100 FGAutopilot::FG_ALTITUDE_LOCK );
101 current_autopilot->set_AltitudeEnabled(
102 ! current_autopilot->get_AltitudeEnabled()
105 case 7: // Ctrl-G key
106 current_autopilot->set_AltitudeMode(
107 FGAutopilot::FG_ALTITUDE_GS1 );
108 current_autopilot->set_AltitudeEnabled(
109 ! current_autopilot->get_AltitudeEnabled()
112 case 8: // Ctrl-H key
113 current_autopilot->set_HeadingMode(
114 FGAutopilot::FG_HEADING_LOCK );
115 current_autopilot->set_HeadingEnabled(
116 ! current_autopilot->get_HeadingEnabled()
119 case 14: // Ctrl-N key
120 current_autopilot->set_HeadingMode(
121 FGAutopilot::FG_HEADING_NAV1 );
122 current_autopilot->set_HeadingEnabled(
123 ! current_autopilot->get_HeadingEnabled()
126 case 18: // Ctrl-R key
128 winding_ccw = !winding_ccw;
130 glFrontFace ( GL_CCW );
132 glFrontFace ( GL_CW );
135 case 19: // Ctrl-S key
136 current_autopilot->set_AutoThrottleEnabled(
137 ! current_autopilot->get_AutoThrottleEnabled()
140 case 20: // Ctrl-T key
141 current_autopilot->set_AltitudeMode(
142 FGAutopilot::FG_ALTITUDE_TERRAIN );
143 current_autopilot->set_AltitudeEnabled(
144 ! current_autopilot->get_AltitudeEnabled()
147 case 21: // Ctrl-U key
148 // add 1000' of emergency altitude. Possibly good for
149 // unflipping yourself :-)
151 double alt = cur_fdm_state->get_Altitude() + 1000;
152 fgFDMForceAltitude( current_options.get_flight_model(),
153 alt * FEET_TO_METER );
156 case 49: // numeric keypad 1
157 v->set_goal_view_offset( FG_PI * 0.75 );
159 case 50: // numeric keypad 2
160 v->set_goal_view_offset( FG_PI );
162 case 51: // numeric keypad 3
163 v->set_goal_view_offset( FG_PI * 1.25 );
165 case 52: // numeric keypad 4
166 v->set_goal_view_offset( FG_PI * 0.50 );
168 case 54: // numeric keypad 6
169 v->set_goal_view_offset( FG_PI * 1.50 );
171 case 55: // numeric keypad 7
172 v->set_goal_view_offset( FG_PI * 0.25 );
174 case 56: // numeric keypad 8
175 v->set_goal_view_offset( 0.00 );
177 case 57: // numeric keypad 9
178 v->set_goal_view_offset( FG_PI * 1.75 );
181 speed = current_options.get_speed_up();
186 current_options.set_speed_up( speed );
189 // status = current_options.get_hud_status();
190 // current_options.set_hud_status(!status);
191 HUD_brightkey( true );
195 fgHUDInit2(¤t_aircraft);
199 local_update_sky_and_lighting_params();
202 current_options.toggle_panel();
205 t->adjust_warp_delta(-30);
206 local_update_sky_and_lighting_params();
209 #if defined(FX) && !defined(WIN32)
210 global_fullscreen = ( !global_fullscreen );
211 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
212 XMesaSetFXmode( global_fullscreen ?
213 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
218 fov = current_options.get_fov();
220 if ( fov > FG_FOV_MAX ) {
223 current_options.set_fov(fov);
224 v->force_update_fov_math();
227 #ifndef FG_OLD_WEATHER
228 tmp = WeatherDatabase->getWeatherVisibility();
230 WeatherDatabase->setWeatherVisibility( tmp );
232 tmp = current_weather.get_visibility(); // in meters
234 current_weather.set_visibility( tmp );
239 FG_LOG( FG_INPUT, FG_DEBUG, "" );
241 case 50: // numeric keypad 2
242 if ( current_autopilot->get_AltitudeEnabled() ) {
243 current_autopilot->AltitudeAdjust( 100 );
245 controls.move_elevator(-0.05);
248 case 56: // numeric keypad 8
249 if ( current_autopilot->get_AltitudeEnabled() ) {
250 current_autopilot->AltitudeAdjust( -100 );
252 controls.move_elevator(0.05);
255 case 49: // numeric keypad 1
256 controls.move_elevator_trim(-0.001);
258 case 55: // numeric keypad 7
259 controls.move_elevator_trim(0.001);
261 case 52: // numeric keypad 4
262 controls.move_aileron(-0.05);
264 case 54: // numeric keypad 6
265 controls.move_aileron(0.05);
267 case 48: // numeric keypad Ins
268 if ( current_autopilot->get_HeadingEnabled() ) {
269 current_autopilot->HeadingAdjust( -1 );
271 controls.move_rudder(-0.05);
274 case 13: // numeric keypad Enter
275 if ( current_autopilot->get_HeadingEnabled() ) {
276 current_autopilot->HeadingAdjust( 1 );
278 controls.move_rudder(0.05);
281 case 53: // numeric keypad 5
282 controls.set_aileron(0.0);
283 controls.set_elevator(0.0);
284 controls.set_rudder(0.0);
286 case 57: // numeric keypad 9 (Pg Up)
287 if ( current_autopilot->get_AutoThrottleEnabled() ) {
288 current_autopilot->AutoThrottleAdjust( 5 );
290 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
293 case 51: // numeric keypad 3 (Pg Dn)
294 if ( current_autopilot->get_AutoThrottleEnabled() ) {
295 current_autopilot->AutoThrottleAdjust( -5 );
297 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
301 controls.move_flaps(-0.34);
302 FG_LOG( FG_INPUT, FG_INFO,
303 "Set flaps to " << controls.get_flaps() );
306 controls.move_flaps(0.34);
307 FG_LOG( FG_INPUT, FG_INFO,
308 "Set flaps to " << controls.get_flaps() );
311 speed = current_options.get_speed_up();
313 current_options.set_speed_up( speed );
318 b_ret = int( controls.get_brake( 0 ) );
319 b_set = double(!b_ret);
320 controls.set_brake( FGControls::ALL_WHEELS, b_set);
323 HUD_brightkey( false );
326 fgHUDInit(¤t_aircraft); // normal HUD
329 t->adjust_warp (+60);
330 local_update_sky_and_lighting_params();
333 t->togglePauseMode();
336 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
337 f->get_Latitude() * RAD_TO_DEG );
338 FGPath tile_path( current_options.get_fg_root() );
339 tile_path.append( "Scenery" );
340 tile_path.append( p.gen_base_path() );
341 tile_path.append( p.gen_index_str() );
343 // printf position and attitude information
344 FG_LOG( FG_INPUT, FG_INFO,
345 "Lon = " << f->get_Longitude() * RAD_TO_DEG
346 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
347 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
349 FG_LOG( FG_INPUT, FG_INFO,
350 "Heading = " << f->get_Psi() * RAD_TO_DEG
351 << " Roll = " << f->get_Phi() * RAD_TO_DEG
352 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
353 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
357 t->adjust_warp_delta (+30);
358 local_update_sky_and_lighting_params();
361 current_options.cycle_view_mode();
364 fov = current_options.get_fov();
366 if ( fov < FG_FOV_MIN ) {
369 current_options.set_fov(fov);
370 v->force_update_fov_math();
373 #ifndef FG_OLD_WEATHER
374 tmp = WeatherDatabase->getWeatherVisibility();
376 WeatherDatabase->setWeatherVisibility( tmp );
378 tmp = current_weather.get_visibility(); // in meters
380 current_weather.set_visibility( tmp );
384 // if( fg_DebugOutput ) {
385 // fclose( fg_DebugOutput );
387 FG_LOG( FG_INPUT, FG_ALERT,
388 "Program exiting normally at user request." );
396 // Handle "special" keyboard events
397 void GLUTspecialkey(int k, int x, int y) {
402 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
404 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
408 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
409 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
412 ifstream input("fgfs.sav");
413 if (input.good() && fgLoadFlight(input)) {
415 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
417 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
422 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
423 ofstream output("fgfs.sav");
424 if (output.good() && fgSaveFlight(output)) {
426 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
428 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
432 case GLUT_KEY_END: // numeric keypad 1
433 v->set_goal_view_offset( FG_PI * 0.75 );
435 case GLUT_KEY_DOWN: // numeric keypad 2
436 v->set_goal_view_offset( FG_PI );
438 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
439 v->set_goal_view_offset( FG_PI * 1.25 );
441 case GLUT_KEY_LEFT: // numeric keypad 4
442 v->set_goal_view_offset( FG_PI * 0.50 );
444 case GLUT_KEY_RIGHT: // numeric keypad 6
445 v->set_goal_view_offset( FG_PI * 1.50 );
447 case GLUT_KEY_HOME: // numeric keypad 7
448 v->set_goal_view_offset( FG_PI * 0.25 );
450 case GLUT_KEY_UP: // numeric keypad 8
451 v->set_goal_view_offset( 0.00 );
453 case GLUT_KEY_PAGE_UP: // numeric keypad 9
454 v->set_goal_view_offset( FG_PI * 1.75 );
458 FG_LOG( FG_INPUT, FG_DEBUG, "" );
460 case GLUT_KEY_F2: // F2 Reload Tile Cache...
463 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
464 FGTime *t = FGTime::cur_time_params;
465 if( (toggle_pause = !t->getPause()) )
466 t->togglePauseMode();
468 if( global_tile_mgr.init() ) {
469 // Load the local scenery data
470 global_tile_mgr.update();
472 FG_LOG( FG_GENERAL, FG_ALERT,
473 "Error in Tile Manager initialization!" );
478 t->togglePauseMode();
481 case GLUT_KEY_F3: // F3 Take a screen shot
484 case GLUT_KEY_F6: // F6 toggles Autopilot target location
485 if ( current_autopilot->get_HeadingMode() !=
486 FGAutopilot::FG_HEADING_WAYPOINT ) {
487 current_autopilot->set_HeadingMode(
488 FGAutopilot::FG_HEADING_WAYPOINT );
490 current_autopilot->set_HeadingMode(
491 FGAutopilot::FG_HEADING_LOCK );
494 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
495 current_options.cycle_fog();
497 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
498 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
499 } else if ( current_options.get_fog() ==
500 fgOPTIONS::FG_FOG_FASTEST )
502 FG_LOG( FG_INPUT, FG_INFO,
503 "Fog enabled, hint set to fastest" );
504 } else if ( current_options.get_fog() ==
505 fgOPTIONS::FG_FOG_NICEST )
507 FG_LOG( FG_INPUT, FG_INFO,
508 "Fog enabled, hint set to nicest" );
512 case GLUT_KEY_F9: // F9 toggles textures on and off...
513 if ( material_mgr.loaded() ) {
514 if (current_options.get_textures()) {
515 current_options.set_textures(false);
516 material_mgr.set_step(1);
518 current_options.set_textures(true);
519 material_mgr.set_step(0);
521 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
523 FG_LOG( FG_INPUT, FG_INFO,
524 "No textures loaded, cannot toggle" );
527 case GLUT_KEY_F10: // F10 toggles menu on and off...
528 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
531 case GLUT_KEY_F11: // F11 Altitude Dialog.
532 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
535 case GLUT_KEY_F12: // F12 Heading Dialog...
536 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
540 if ( current_autopilot->get_AltitudeEnabled() ) {
541 current_autopilot->AltitudeAdjust( -100 );
543 controls.move_elevator(0.05);
547 if ( current_autopilot->get_AltitudeEnabled() ) {
548 current_autopilot->AltitudeAdjust( 100 );
550 controls.move_elevator(-0.05);
554 controls.move_aileron(-0.05);
557 controls.move_aileron(0.05);
559 case GLUT_KEY_HOME: // numeric keypad 1
560 controls.move_elevator_trim(0.001);
562 case GLUT_KEY_END: // numeric keypad 7
563 controls.move_elevator_trim(-0.001);
565 case GLUT_KEY_INSERT: // numeric keypad Ins
566 if ( current_autopilot->get_HeadingEnabled() ) {
567 current_autopilot->HeadingAdjust( -1 );
569 controls.move_rudder(-0.05);
572 case 13: // numeric keypad Enter
573 if ( current_autopilot->get_HeadingEnabled() ) {
574 current_autopilot->HeadingAdjust( 1 );
576 controls.move_rudder(0.05);
579 case 53: // numeric keypad 5
580 controls.set_aileron(0.0);
581 controls.set_elevator(0.0);
582 controls.set_rudder(0.0);
584 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
585 if ( current_autopilot->get_AutoThrottleEnabled() ) {
586 current_autopilot->AutoThrottleAdjust( 5 );
588 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
591 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
592 if ( current_autopilot->get_AutoThrottleEnabled() ) {
593 current_autopilot->AutoThrottleAdjust( -5 );
595 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );