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>
65 #include "keyboard.hxx"
66 #include "options.hxx"
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 current_autopilot->set_AltitudeMode(
101 FGAutopilot::FG_ALTITUDE_LOCK );
102 current_autopilot->set_AltitudeEnabled(
103 ! current_autopilot->get_AltitudeEnabled()
106 case 7: // Ctrl-G key
107 current_autopilot->set_AltitudeMode(
108 FGAutopilot::FG_ALTITUDE_GS1 );
109 current_autopilot->set_AltitudeEnabled(
110 ! current_autopilot->get_AltitudeEnabled()
113 case 8: // Ctrl-H key
114 current_autopilot->set_HeadingMode(
115 FGAutopilot::FG_HEADING_LOCK );
116 current_autopilot->set_HeadingEnabled(
117 ! current_autopilot->get_HeadingEnabled()
120 case 14: // Ctrl-N key
121 current_autopilot->set_HeadingMode(
122 FGAutopilot::FG_HEADING_NAV1 );
123 current_autopilot->set_HeadingEnabled(
124 ! current_autopilot->get_HeadingEnabled()
127 case 18: // Ctrl-R key
129 winding_ccw = !winding_ccw;
131 glFrontFace ( GL_CCW );
133 glFrontFace ( GL_CW );
136 case 19: // Ctrl-S key
137 current_autopilot->set_AutoThrottleEnabled(
138 ! current_autopilot->get_AutoThrottleEnabled()
141 case 20: // Ctrl-T key
142 current_autopilot->set_AltitudeMode(
143 FGAutopilot::FG_ALTITUDE_TERRAIN );
144 current_autopilot->set_AltitudeEnabled(
145 ! current_autopilot->get_AltitudeEnabled()
148 case 21: // Ctrl-U key
149 // add 1000' of emergency altitude. Possibly good for
150 // unflipping yourself :-)
152 double alt = cur_fdm_state->get_Altitude() + 1000;
153 fgFDMForceAltitude( current_options.get_flight_model(),
154 alt * FEET_TO_METER );
157 case 49: // numeric keypad 1
158 v->set_goal_view_offset( FG_PI * 0.75 );
160 case 50: // numeric keypad 2
161 v->set_goal_view_offset( FG_PI );
163 case 51: // numeric keypad 3
164 v->set_goal_view_offset( FG_PI * 1.25 );
166 case 52: // numeric keypad 4
167 v->set_goal_view_offset( FG_PI * 0.50 );
169 case 54: // numeric keypad 6
170 v->set_goal_view_offset( FG_PI * 1.50 );
172 case 55: // numeric keypad 7
173 v->set_goal_view_offset( FG_PI * 0.25 );
175 case 56: // numeric keypad 8
176 v->set_goal_view_offset( 0.00 );
178 case 57: // numeric keypad 9
179 v->set_goal_view_offset( FG_PI * 1.75 );
182 speed = current_options.get_speed_up();
187 current_options.set_speed_up( speed );
190 // status = current_options.get_hud_status();
191 // current_options.set_hud_status(!status);
192 HUD_brightkey( true );
196 fgHUDInit2(¤t_aircraft);
200 local_update_sky_and_lighting_params();
203 current_options.toggle_panel();
206 t->adjust_warp_delta(-30);
207 local_update_sky_and_lighting_params();
210 #if defined(FX) && !defined(WIN32)
211 global_fullscreen = ( !global_fullscreen );
212 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
213 XMesaSetFXmode( global_fullscreen ?
214 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
219 fov = current_options.get_fov();
221 if ( fov > FG_FOV_MAX ) {
224 current_options.set_fov(fov);
225 v->force_update_fov_math();
228 #ifndef FG_OLD_WEATHER
229 tmp = WeatherDatabase->getWeatherVisibility();
231 WeatherDatabase->setWeatherVisibility( tmp );
233 tmp = current_weather.get_visibility(); // in meters
235 current_weather.set_visibility( tmp );
240 FG_LOG( FG_INPUT, FG_DEBUG, "" );
242 case 50: // numeric keypad 2
243 if ( current_autopilot->get_AltitudeEnabled() ) {
244 current_autopilot->AltitudeAdjust( 100 );
246 controls.move_elevator(-0.05);
249 case 56: // numeric keypad 8
250 if ( current_autopilot->get_AltitudeEnabled() ) {
251 current_autopilot->AltitudeAdjust( -100 );
253 controls.move_elevator(0.05);
256 case 49: // numeric keypad 1
257 controls.move_elevator_trim(-0.001);
259 case 55: // numeric keypad 7
260 controls.move_elevator_trim(0.001);
262 case 52: // numeric keypad 4
263 controls.move_aileron(-0.05);
265 case 54: // numeric keypad 6
266 controls.move_aileron(0.05);
268 case 48: // numeric keypad Ins
269 if ( current_autopilot->get_HeadingEnabled() ) {
270 current_autopilot->HeadingAdjust( -1 );
272 controls.move_rudder(-0.05);
275 case 13: // numeric keypad Enter
276 if ( current_autopilot->get_HeadingEnabled() ) {
277 current_autopilot->HeadingAdjust( 1 );
279 controls.move_rudder(0.05);
282 case 53: // numeric keypad 5
283 controls.set_aileron(0.0);
284 controls.set_elevator(0.0);
285 controls.set_rudder(0.0);
287 case 57: // numeric keypad 9 (Pg Up)
288 if ( current_autopilot->get_AutoThrottleEnabled() ) {
289 current_autopilot->AutoThrottleAdjust( 5 );
291 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
294 case 51: // numeric keypad 3 (Pg Dn)
295 if ( current_autopilot->get_AutoThrottleEnabled() ) {
296 current_autopilot->AutoThrottleAdjust( -5 );
298 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
302 controls.move_flaps(-0.34);
303 FG_LOG( FG_INPUT, FG_INFO,
304 "Set flaps to " << controls.get_flaps() );
307 controls.move_flaps(0.34);
308 FG_LOG( FG_INPUT, FG_INFO,
309 "Set flaps to " << controls.get_flaps() );
312 speed = current_options.get_speed_up();
314 current_options.set_speed_up( speed );
319 b_ret = int( controls.get_brake( 0 ) );
320 b_set = double(!b_ret);
321 controls.set_brake( FGControls::ALL_WHEELS, b_set);
324 HUD_masterswitch( true );
327 fgHUDInit(¤t_aircraft); // normal HUD
330 t->adjust_warp (+60);
331 local_update_sky_and_lighting_params();
334 t->togglePauseMode();
337 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
338 f->get_Latitude() * RAD_TO_DEG );
339 FGPath tile_path( current_options.get_fg_root() );
340 tile_path.append( "Scenery" );
341 tile_path.append( p.gen_base_path() );
342 tile_path.append( p.gen_index_str() );
344 // printf position and attitude information
345 FG_LOG( FG_INPUT, FG_INFO,
346 "Lon = " << f->get_Longitude() * RAD_TO_DEG
347 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
348 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
350 FG_LOG( FG_INPUT, FG_INFO,
351 "Heading = " << f->get_Psi() * RAD_TO_DEG
352 << " Roll = " << f->get_Phi() * RAD_TO_DEG
353 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
354 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
358 t->adjust_warp_delta (+30);
359 local_update_sky_and_lighting_params();
362 current_options.cycle_view_mode();
365 fov = current_options.get_fov();
367 if ( fov < FG_FOV_MIN ) {
370 current_options.set_fov(fov);
371 v->force_update_fov_math();
374 #ifndef FG_OLD_WEATHER
375 tmp = WeatherDatabase->getWeatherVisibility();
377 WeatherDatabase->setWeatherVisibility( tmp );
379 tmp = current_weather.get_visibility(); // in meters
381 current_weather.set_visibility( tmp );
385 // if( fg_DebugOutput ) {
386 // fclose( fg_DebugOutput );
388 FG_LOG( FG_INPUT, FG_ALERT,
389 "Program exiting normally at user request." );
397 // Handle "special" keyboard events
398 void GLUTspecialkey(int k, int x, int y) {
403 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
405 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
409 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
410 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
413 ifstream input("fgfs.sav");
414 if (input.good() && fgLoadFlight(input)) {
416 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
418 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
423 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
424 ofstream output("fgfs.sav");
425 if (output.good() && fgSaveFlight(output)) {
427 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
429 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
433 case GLUT_KEY_END: // numeric keypad 1
434 v->set_goal_view_offset( FG_PI * 0.75 );
436 case GLUT_KEY_DOWN: // numeric keypad 2
437 v->set_goal_view_offset( FG_PI );
439 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
440 v->set_goal_view_offset( FG_PI * 1.25 );
442 case GLUT_KEY_LEFT: // numeric keypad 4
443 v->set_goal_view_offset( FG_PI * 0.50 );
445 case GLUT_KEY_RIGHT: // numeric keypad 6
446 v->set_goal_view_offset( FG_PI * 1.50 );
448 case GLUT_KEY_HOME: // numeric keypad 7
449 v->set_goal_view_offset( FG_PI * 0.25 );
451 case GLUT_KEY_UP: // numeric keypad 8
452 v->set_goal_view_offset( 0.00 );
454 case GLUT_KEY_PAGE_UP: // numeric keypad 9
455 v->set_goal_view_offset( FG_PI * 1.75 );
459 FG_LOG( FG_INPUT, FG_DEBUG, "" );
461 case GLUT_KEY_F2: // F2 Reload Tile Cache...
464 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
465 FGTime *t = FGTime::cur_time_params;
466 if( (toggle_pause = !t->getPause()) )
467 t->togglePauseMode();
469 if( global_tile_mgr.init() ) {
470 // Load the local scenery data
471 global_tile_mgr.update();
473 FG_LOG( FG_GENERAL, FG_ALERT,
474 "Error in Tile Manager initialization!" );
479 t->togglePauseMode();
482 case GLUT_KEY_F3: // F3 Take a screen shot
485 case GLUT_KEY_F6: // F6 toggles Autopilot target location
486 if ( current_autopilot->get_HeadingMode() !=
487 FGAutopilot::FG_HEADING_WAYPOINT ) {
488 current_autopilot->set_HeadingMode(
489 FGAutopilot::FG_HEADING_WAYPOINT );
491 current_autopilot->set_HeadingMode(
492 FGAutopilot::FG_HEADING_LOCK );
495 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
496 current_options.cycle_fog();
498 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
499 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
500 } else if ( current_options.get_fog() ==
501 fgOPTIONS::FG_FOG_FASTEST )
503 FG_LOG( FG_INPUT, FG_INFO,
504 "Fog enabled, hint set to fastest" );
505 } else if ( current_options.get_fog() ==
506 fgOPTIONS::FG_FOG_NICEST )
508 FG_LOG( FG_INPUT, FG_INFO,
509 "Fog enabled, hint set to nicest" );
513 case GLUT_KEY_F9: // F9 toggles textures on and off...
514 if ( material_mgr.loaded() ) {
515 if (current_options.get_textures()) {
516 current_options.set_textures(false);
517 material_mgr.set_step(1);
519 current_options.set_textures(true);
520 material_mgr.set_step(0);
522 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
524 FG_LOG( FG_INPUT, FG_INFO,
525 "No textures loaded, cannot toggle" );
528 case GLUT_KEY_F10: // F10 toggles menu on and off...
529 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
532 case GLUT_KEY_F11: // F11 Altitude Dialog.
533 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
536 case GLUT_KEY_F12: // F12 Heading Dialog...
537 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
541 if ( current_autopilot->get_AltitudeEnabled() ) {
542 current_autopilot->AltitudeAdjust( -100 );
544 controls.move_elevator(0.05);
548 if ( current_autopilot->get_AltitudeEnabled() ) {
549 current_autopilot->AltitudeAdjust( 100 );
551 controls.move_elevator(-0.05);
555 controls.move_aileron(-0.05);
558 controls.move_aileron(0.05);
560 case GLUT_KEY_HOME: // numeric keypad 1
561 controls.move_elevator_trim(0.001);
563 case GLUT_KEY_END: // numeric keypad 7
564 controls.move_elevator_trim(-0.001);
566 case GLUT_KEY_INSERT: // numeric keypad Ins
567 if ( current_autopilot->get_HeadingEnabled() ) {
568 current_autopilot->HeadingAdjust( -1 );
570 controls.move_rudder(-0.05);
573 case 13: // numeric keypad Enter
574 if ( current_autopilot->get_HeadingEnabled() ) {
575 current_autopilot->HeadingAdjust( 1 );
577 controls.move_rudder(0.05);
580 case 53: // numeric keypad 5
581 controls.set_aileron(0.0);
582 controls.set_elevator(0.0);
583 controls.set_rudder(0.0);
585 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
586 if ( current_autopilot->get_AutoThrottleEnabled() ) {
587 current_autopilot->AutoThrottleAdjust( 5 );
589 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
592 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
593 if ( current_autopilot->get_AutoThrottleEnabled() ) {
594 current_autopilot->AutoThrottleAdjust( -5 );
596 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );