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>
51 #include <simgear/timing/fg_time.hxx>
53 #include <Aircraft/aircraft.hxx>
54 #include <Autopilot/auto_gui.hxx>
55 #include <Autopilot/newauto.hxx>
56 #include <Cockpit/hud.hxx>
58 #include <Scenery/tilemgr.hxx>
59 #include <Objects/matlib.hxx>
60 #include <Time/light.hxx>
61 #include <Time/tmp.hxx>
63 #ifndef FG_OLD_WEATHER
64 # include <WeatherCM/FGLocalWeatherDatabase.h>
66 # include <Weather/weather.hxx>
70 #include "keyboard.hxx"
71 #include "options.hxx"
76 // Handle keyboard events
77 void GLUTkey(unsigned char k, int x, int y) {
82 static bool winding_ccw = true;
85 f = current_aircraft.fdm_state;
86 t = FGTime::cur_time_params;
89 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
90 if ( puKeyboard(k, PU_DOWN) ) {
94 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
95 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
98 current_autopilot->set_AltitudeMode(
99 FGAutopilot::FG_ALTITUDE_LOCK );
100 current_autopilot->set_AltitudeEnabled(
101 ! current_autopilot->get_AltitudeEnabled()
104 case 7: // Ctrl-G key
105 current_autopilot->set_AltitudeMode(
106 FGAutopilot::FG_ALTITUDE_GS1 );
107 current_autopilot->set_AltitudeEnabled(
108 ! current_autopilot->get_AltitudeEnabled()
111 case 8: // Ctrl-H key
112 current_autopilot->set_HeadingMode(
113 FGAutopilot::FG_HEADING_LOCK );
114 current_autopilot->set_HeadingEnabled(
115 ! current_autopilot->get_HeadingEnabled()
118 case 14: // Ctrl-N key
119 current_autopilot->set_HeadingMode(
120 FGAutopilot::FG_HEADING_NAV1 );
121 current_autopilot->set_HeadingEnabled(
122 ! current_autopilot->get_HeadingEnabled()
125 case 18: // Ctrl-R key
127 winding_ccw = !winding_ccw;
129 glFrontFace ( GL_CCW );
131 glFrontFace ( GL_CW );
134 case 19: // Ctrl-S key
135 current_autopilot->set_AutoThrottleEnabled(
136 ! current_autopilot->get_AutoThrottleEnabled()
139 case 20: // Ctrl-T key
140 current_autopilot->set_AltitudeMode(
141 FGAutopilot::FG_ALTITUDE_TERRAIN );
142 current_autopilot->set_AltitudeEnabled(
143 ! current_autopilot->get_AltitudeEnabled()
146 case 21: // Ctrl-U key
147 // add 1000' of emergency altitude. Possibly good for
148 // unflipping yourself :-)
150 double alt = cur_fdm_state->get_Altitude() + 1000;
151 fgFDMForceAltitude( current_options.get_flight_model(),
152 alt * FEET_TO_METER );
155 case 49: // numeric keypad 1
156 v->set_goal_view_offset( FG_PI * 0.75 );
158 case 50: // numeric keypad 2
159 v->set_goal_view_offset( FG_PI );
161 case 51: // numeric keypad 3
162 v->set_goal_view_offset( FG_PI * 1.25 );
164 case 52: // numeric keypad 4
165 v->set_goal_view_offset( FG_PI * 0.50 );
167 case 54: // numeric keypad 6
168 v->set_goal_view_offset( FG_PI * 1.50 );
170 case 55: // numeric keypad 7
171 v->set_goal_view_offset( FG_PI * 0.25 );
173 case 56: // numeric keypad 8
174 v->set_goal_view_offset( 0.00 );
176 case 57: // numeric keypad 9
177 v->set_goal_view_offset( FG_PI * 1.75 );
180 speed = current_options.get_speed_up();
185 current_options.set_speed_up( speed );
188 // status = current_options.get_hud_status();
189 // current_options.set_hud_status(!status);
190 HUD_brightkey( true );
194 fgHUDInit2(¤t_aircraft);
198 fgUpdateSkyAndLightingParams();
201 current_options.toggle_panel();
204 t->adjust_warp_delta(-30);
205 fgUpdateSkyAndLightingParams();
208 #if defined(FX) && !defined(WIN32)
209 global_fullscreen = ( !global_fullscreen );
210 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
211 XMesaSetFXmode( global_fullscreen ?
212 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
217 fov = current_options.get_fov();
219 if ( fov > FG_FOV_MAX ) {
222 current_options.set_fov(fov);
223 v->force_update_fov_math();
226 #ifndef FG_OLD_WEATHER
227 tmp = WeatherDatabase->getWeatherVisibility();
229 WeatherDatabase->setWeatherVisibility( tmp );
231 tmp = current_weather.get_visibility(); // in meters
233 current_weather.set_visibility( tmp );
238 FG_LOG( FG_INPUT, FG_DEBUG, "" );
240 case 50: // numeric keypad 2
241 if ( current_autopilot->get_AltitudeEnabled() ) {
242 current_autopilot->AltitudeAdjust( 100 );
244 controls.move_elevator(-0.05);
247 case 56: // numeric keypad 8
248 if ( current_autopilot->get_AltitudeEnabled() ) {
249 current_autopilot->AltitudeAdjust( -100 );
251 controls.move_elevator(0.05);
254 case 49: // numeric keypad 1
255 controls.move_elevator_trim(-0.001);
257 case 55: // numeric keypad 7
258 controls.move_elevator_trim(0.001);
260 case 52: // numeric keypad 4
261 controls.move_aileron(-0.05);
263 case 54: // numeric keypad 6
264 controls.move_aileron(0.05);
266 case 48: // numeric keypad Ins
267 if ( current_autopilot->get_HeadingEnabled() ) {
268 current_autopilot->HeadingAdjust( -1 );
270 controls.move_rudder(-0.05);
273 case 13: // numeric keypad Enter
274 if ( current_autopilot->get_HeadingEnabled() ) {
275 current_autopilot->HeadingAdjust( 1 );
277 controls.move_rudder(0.05);
280 case 53: // numeric keypad 5
281 controls.set_aileron(0.0);
282 controls.set_elevator(0.0);
283 controls.set_rudder(0.0);
285 case 57: // numeric keypad 9 (Pg Up)
286 if ( current_autopilot->get_AutoThrottleEnabled() ) {
287 current_autopilot->AutoThrottleAdjust( 5 );
289 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
292 case 51: // numeric keypad 3 (Pg Dn)
293 if ( current_autopilot->get_AutoThrottleEnabled() ) {
294 current_autopilot->AutoThrottleAdjust( -5 );
296 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
300 controls.move_flaps(-0.34);
301 FG_LOG( FG_INPUT, FG_INFO,
302 "Set flaps to " << controls.get_flaps() );
305 controls.move_flaps(0.34);
306 FG_LOG( FG_INPUT, FG_INFO,
307 "Set flaps to " << controls.get_flaps() );
310 speed = current_options.get_speed_up();
312 current_options.set_speed_up( speed );
317 b_ret = int( controls.get_brake( 0 ) );
318 b_set = double(!b_ret);
319 controls.set_brake( FGControls::ALL_WHEELS, b_set);
322 if (controls.get_brake(0) > 0.0) {
323 controls.set_brake(0, 0.0);
325 controls.set_brake(0, 1.0);
329 if (controls.get_brake(1) > 0.0) {
330 controls.set_brake(1, 0.0);
332 controls.set_brake(1, 1.0);
336 HUD_masterswitch( true );
339 fgHUDInit(¤t_aircraft); // normal HUD
342 t->adjust_warp (+60);
343 fgUpdateSkyAndLightingParams();
346 current_options.toggle_pause();
349 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
350 f->get_Latitude() * RAD_TO_DEG );
351 FGPath tile_path( current_options.get_fg_root() );
352 tile_path.append( "Scenery" );
353 tile_path.append( p.gen_base_path() );
354 tile_path.append( p.gen_index_str() );
356 // printf position and attitude information
357 FG_LOG( FG_INPUT, FG_INFO,
358 "Lon = " << f->get_Longitude() * RAD_TO_DEG
359 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
360 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
362 FG_LOG( FG_INPUT, FG_INFO,
363 "Heading = " << f->get_Psi() * RAD_TO_DEG
364 << " Roll = " << f->get_Phi() * RAD_TO_DEG
365 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
366 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
370 t->adjust_warp_delta (+30);
371 fgUpdateSkyAndLightingParams();
374 current_options.cycle_view_mode();
377 fov = current_options.get_fov();
379 if ( fov < FG_FOV_MIN ) {
382 current_options.set_fov(fov);
383 v->force_update_fov_math();
386 #ifndef FG_OLD_WEATHER
387 tmp = WeatherDatabase->getWeatherVisibility();
389 WeatherDatabase->setWeatherVisibility( tmp );
391 tmp = current_weather.get_visibility(); // in meters
393 current_weather.set_visibility( tmp );
397 // if( fg_DebugOutput ) {
398 // fclose( fg_DebugOutput );
400 FG_LOG( FG_INPUT, FG_ALERT,
401 "Program exiting normally at user request." );
409 // Handle "special" keyboard events
410 void GLUTspecialkey(int k, int x, int y) {
415 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
417 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
421 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
422 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
425 ifstream input("fgfs.sav");
426 if (input.good() && fgLoadFlight(input)) {
428 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
430 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
435 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
436 ofstream output("fgfs.sav");
437 if (output.good() && fgSaveFlight(output)) {
439 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
441 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
445 case GLUT_KEY_END: // numeric keypad 1
446 v->set_goal_view_offset( FG_PI * 0.75 );
448 case GLUT_KEY_DOWN: // numeric keypad 2
449 v->set_goal_view_offset( FG_PI );
451 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
452 v->set_goal_view_offset( FG_PI * 1.25 );
454 case GLUT_KEY_LEFT: // numeric keypad 4
455 v->set_goal_view_offset( FG_PI * 0.50 );
457 case GLUT_KEY_RIGHT: // numeric keypad 6
458 v->set_goal_view_offset( FG_PI * 1.50 );
460 case GLUT_KEY_HOME: // numeric keypad 7
461 v->set_goal_view_offset( FG_PI * 0.25 );
463 case GLUT_KEY_UP: // numeric keypad 8
464 v->set_goal_view_offset( 0.00 );
466 case GLUT_KEY_PAGE_UP: // numeric keypad 9
467 v->set_goal_view_offset( FG_PI * 1.75 );
471 FG_LOG( FG_INPUT, FG_DEBUG, "" );
473 case GLUT_KEY_F2: // F2 Reload Tile Cache...
476 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
477 if( (toggle_pause = !current_options.get_pause()) )
478 current_options.toggle_pause();
480 if( global_tile_mgr.init() ) {
481 // Load the local scenery data
482 global_tile_mgr.update( cur_fdm_state->get_Longitude(),
483 cur_fdm_state->get_Latitude() );
485 FG_LOG( FG_GENERAL, FG_ALERT,
486 "Error in Tile Manager initialization!" );
491 current_options.toggle_pause();
494 case GLUT_KEY_F3: // F3 Take a screen shot
497 case GLUT_KEY_F6: // F6 toggles Autopilot target location
498 if ( current_autopilot->get_HeadingMode() !=
499 FGAutopilot::FG_HEADING_WAYPOINT ) {
500 current_autopilot->set_HeadingMode(
501 FGAutopilot::FG_HEADING_WAYPOINT );
503 current_autopilot->set_HeadingMode(
504 FGAutopilot::FG_HEADING_LOCK );
507 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
508 current_options.cycle_fog();
510 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
511 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
512 } else if ( current_options.get_fog() ==
513 fgOPTIONS::FG_FOG_FASTEST )
515 FG_LOG( FG_INPUT, FG_INFO,
516 "Fog enabled, hint set to fastest" );
517 } else if ( current_options.get_fog() ==
518 fgOPTIONS::FG_FOG_NICEST )
520 FG_LOG( FG_INPUT, FG_INFO,
521 "Fog enabled, hint set to nicest" );
525 case GLUT_KEY_F9: // F9 toggles textures on and off...
526 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
527 if ( current_options.get_textures() ) {
528 current_options.set_textures( false );
529 material_lib.set_step( 1 );
531 current_options.set_textures( true );
532 material_lib.set_step( 0 );
535 case GLUT_KEY_F10: // F10 toggles menu on and off...
536 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
539 case GLUT_KEY_F11: // F11 Altitude Dialog.
540 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
543 case GLUT_KEY_F12: // F12 Heading Dialog...
544 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
548 if ( current_autopilot->get_AltitudeEnabled() ) {
549 current_autopilot->AltitudeAdjust( -100 );
551 controls.move_elevator(0.05);
555 if ( current_autopilot->get_AltitudeEnabled() ) {
556 current_autopilot->AltitudeAdjust( 100 );
558 controls.move_elevator(-0.05);
562 controls.move_aileron(-0.05);
565 controls.move_aileron(0.05);
567 case GLUT_KEY_HOME: // numeric keypad 1
568 controls.move_elevator_trim(0.001);
570 case GLUT_KEY_END: // numeric keypad 7
571 controls.move_elevator_trim(-0.001);
573 case GLUT_KEY_INSERT: // numeric keypad Ins
574 if ( current_autopilot->get_HeadingEnabled() ) {
575 current_autopilot->HeadingAdjust( -1 );
577 controls.move_rudder(-0.05);
580 case 13: // numeric keypad Enter
581 if ( current_autopilot->get_HeadingEnabled() ) {
582 current_autopilot->HeadingAdjust( 1 );
584 controls.move_rudder(0.05);
587 case 53: // numeric keypad 5
588 controls.set_aileron(0.0);
589 controls.set_elevator(0.0);
590 controls.set_rudder(0.0);
592 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
593 if ( current_autopilot->get_AutoThrottleEnabled() ) {
594 current_autopilot->AutoThrottleAdjust( 5 );
596 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
599 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
600 if ( current_autopilot->get_AutoThrottleEnabled() ) {
601 current_autopilot->AutoThrottleAdjust( -5 );
603 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );