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/matlib.hxx>
59 #include <Time/light.hxx>
60 #include <Time/tmp.hxx>
62 #ifndef FG_OLD_WEATHER
63 # include <WeatherCM/FGLocalWeatherDatabase.h>
65 # include <Weather/weather.hxx>
69 #include "globals.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;
88 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
89 if ( puKeyboard(k, PU_DOWN) ) {
93 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
94 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
97 current_autopilot->set_AltitudeMode(
98 FGAutopilot::FG_ALTITUDE_LOCK );
99 current_autopilot->set_AltitudeEnabled(
100 ! current_autopilot->get_AltitudeEnabled()
103 case 7: // Ctrl-G key
104 current_autopilot->set_AltitudeMode(
105 FGAutopilot::FG_ALTITUDE_GS1 );
106 current_autopilot->set_AltitudeEnabled(
107 ! current_autopilot->get_AltitudeEnabled()
110 case 8: // Ctrl-H key
111 current_autopilot->set_HeadingMode(
112 FGAutopilot::FG_HEADING_LOCK );
113 current_autopilot->set_HeadingEnabled(
114 ! current_autopilot->get_HeadingEnabled()
117 case 14: // Ctrl-N key
118 current_autopilot->set_HeadingMode(
119 FGAutopilot::FG_HEADING_NAV1 );
120 current_autopilot->set_HeadingEnabled(
121 ! current_autopilot->get_HeadingEnabled()
124 case 18: // Ctrl-R key
126 winding_ccw = !winding_ccw;
128 glFrontFace ( GL_CCW );
130 glFrontFace ( GL_CW );
133 case 19: // Ctrl-S key
134 current_autopilot->set_AutoThrottleEnabled(
135 ! current_autopilot->get_AutoThrottleEnabled()
138 case 20: // Ctrl-T key
139 current_autopilot->set_AltitudeMode(
140 FGAutopilot::FG_ALTITUDE_TERRAIN );
141 current_autopilot->set_AltitudeEnabled(
142 ! current_autopilot->get_AltitudeEnabled()
145 case 21: // Ctrl-U key
146 // add 1000' of emergency altitude. Possibly good for
147 // unflipping yourself :-)
149 double alt = cur_fdm_state->get_Altitude() + 1000;
150 fgFDMForceAltitude( current_options.get_flight_model(),
151 alt * FEET_TO_METER );
154 case 49: // numeric keypad 1
155 v->set_goal_view_offset( FG_PI * 0.75 );
157 case 50: // numeric keypad 2
158 v->set_goal_view_offset( FG_PI );
160 case 51: // numeric keypad 3
161 v->set_goal_view_offset( FG_PI * 1.25 );
163 case 52: // numeric keypad 4
164 v->set_goal_view_offset( FG_PI * 0.50 );
166 case 54: // numeric keypad 6
167 v->set_goal_view_offset( FG_PI * 1.50 );
169 case 55: // numeric keypad 7
170 v->set_goal_view_offset( FG_PI * 0.25 );
172 case 56: // numeric keypad 8
173 v->set_goal_view_offset( 0.00 );
175 case 57: // numeric keypad 9
176 v->set_goal_view_offset( FG_PI * 1.75 );
179 speed = current_options.get_speed_up();
184 current_options.set_speed_up( speed );
187 // status = current_options.get_hud_status();
188 // current_options.set_hud_status(!status);
189 HUD_brightkey( true );
193 fgHUDInit2(¤t_aircraft);
196 globals->inc_warp( -60 );
197 fgUpdateSkyAndLightingParams();
200 current_options.toggle_panel();
203 globals->inc_warp_delta( -30 );
204 fgUpdateSkyAndLightingParams();
207 #if defined(FX) && !defined(WIN32)
208 global_fullscreen = ( !global_fullscreen );
209 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
210 XMesaSetFXmode( global_fullscreen ?
211 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
216 fov = current_options.get_fov();
218 if ( fov > FG_FOV_MAX ) {
221 current_options.set_fov(fov);
222 v->force_update_fov_math();
225 #ifndef FG_OLD_WEATHER
226 tmp = WeatherDatabase->getWeatherVisibility();
228 WeatherDatabase->setWeatherVisibility( tmp );
230 tmp = current_weather.get_visibility(); // in meters
232 current_weather.set_visibility( tmp );
237 FG_LOG( FG_INPUT, FG_DEBUG, "" );
239 case 50: // numeric keypad 2
240 if ( current_autopilot->get_AltitudeEnabled() ) {
241 current_autopilot->AltitudeAdjust( 100 );
243 controls.move_elevator(-0.05);
246 case 56: // numeric keypad 8
247 if ( current_autopilot->get_AltitudeEnabled() ) {
248 current_autopilot->AltitudeAdjust( -100 );
250 controls.move_elevator(0.05);
253 case 49: // numeric keypad 1
254 controls.move_elevator_trim(-0.001);
256 case 55: // numeric keypad 7
257 controls.move_elevator_trim(0.001);
259 case 52: // numeric keypad 4
260 controls.move_aileron(-0.05);
262 case 54: // numeric keypad 6
263 controls.move_aileron(0.05);
265 case 48: // numeric keypad Ins
266 if ( current_autopilot->get_HeadingEnabled() ) {
267 current_autopilot->HeadingAdjust( -1 );
269 controls.move_rudder(-0.05);
272 case 13: // numeric keypad Enter
273 if ( current_autopilot->get_HeadingEnabled() ) {
274 current_autopilot->HeadingAdjust( 1 );
276 controls.move_rudder(0.05);
279 case 53: // numeric keypad 5
280 controls.set_aileron(0.0);
281 controls.set_elevator(0.0);
282 controls.set_rudder(0.0);
284 case 57: // numeric keypad 9 (Pg Up)
285 if ( current_autopilot->get_AutoThrottleEnabled() ) {
286 current_autopilot->AutoThrottleAdjust( 5 );
288 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
291 case 51: // numeric keypad 3 (Pg Dn)
292 if ( current_autopilot->get_AutoThrottleEnabled() ) {
293 current_autopilot->AutoThrottleAdjust( -5 );
295 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
299 controls.move_flaps(-0.34);
300 FG_LOG( FG_INPUT, FG_INFO,
301 "Set flaps to " << controls.get_flaps() );
304 controls.move_flaps(0.34);
305 FG_LOG( FG_INPUT, FG_INFO,
306 "Set flaps to " << controls.get_flaps() );
309 speed = current_options.get_speed_up();
311 current_options.set_speed_up( speed );
316 b_ret = int( controls.get_brake( 0 ) );
317 b_set = double(!b_ret);
318 controls.set_brake( FGControls::ALL_WHEELS, b_set);
321 if (controls.get_brake(0) > 0.0) {
322 controls.set_brake(0, 0.0);
324 controls.set_brake(0, 1.0);
328 if (controls.get_brake(1) > 0.0) {
329 controls.set_brake(1, 0.0);
331 controls.set_brake(1, 1.0);
335 HUD_masterswitch( true );
338 fgHUDInit(¤t_aircraft); // normal HUD
341 globals->inc_warp( 60 );
342 fgUpdateSkyAndLightingParams();
345 globals->set_freeze( ! globals->get_freeze() );
348 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
349 f->get_Latitude() * RAD_TO_DEG );
350 FGPath tile_path( current_options.get_fg_root() );
351 tile_path.append( "Scenery" );
352 tile_path.append( p.gen_base_path() );
353 tile_path.append( p.gen_index_str() );
355 // printf position and attitude information
356 FG_LOG( FG_INPUT, FG_INFO,
357 "Lon = " << f->get_Longitude() * RAD_TO_DEG
358 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
359 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
361 FG_LOG( FG_INPUT, FG_INFO,
362 "Heading = " << f->get_Psi() * RAD_TO_DEG
363 << " Roll = " << f->get_Phi() * RAD_TO_DEG
364 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
365 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
369 globals->inc_warp_delta( 30 );
370 fgUpdateSkyAndLightingParams();
373 current_options.cycle_view_mode();
376 fov = current_options.get_fov();
378 if ( fov < FG_FOV_MIN ) {
381 current_options.set_fov(fov);
382 v->force_update_fov_math();
385 #ifndef FG_OLD_WEATHER
386 tmp = WeatherDatabase->getWeatherVisibility();
388 WeatherDatabase->setWeatherVisibility( tmp );
390 tmp = current_weather.get_visibility(); // in meters
392 current_weather.set_visibility( tmp );
396 // if( fg_DebugOutput ) {
397 // fclose( fg_DebugOutput );
399 FG_LOG( FG_INPUT, FG_ALERT,
400 "Program exiting normally at user request." );
408 // Handle "special" keyboard events
409 void GLUTspecialkey(int k, int x, int y) {
414 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
416 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
420 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
421 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
424 ifstream input("fgfs.sav");
425 if (input.good() && fgLoadFlight(input)) {
427 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
429 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
434 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
435 ofstream output("fgfs.sav");
436 if (output.good() && fgSaveFlight(output)) {
438 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
440 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
444 case GLUT_KEY_END: // numeric keypad 1
445 v->set_goal_view_offset( FG_PI * 0.75 );
447 case GLUT_KEY_DOWN: // numeric keypad 2
448 v->set_goal_view_offset( FG_PI );
450 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
451 v->set_goal_view_offset( FG_PI * 1.25 );
453 case GLUT_KEY_LEFT: // numeric keypad 4
454 v->set_goal_view_offset( FG_PI * 0.50 );
456 case GLUT_KEY_RIGHT: // numeric keypad 6
457 v->set_goal_view_offset( FG_PI * 1.50 );
459 case GLUT_KEY_HOME: // numeric keypad 7
460 v->set_goal_view_offset( FG_PI * 0.25 );
462 case GLUT_KEY_UP: // numeric keypad 8
463 v->set_goal_view_offset( 0.00 );
465 case GLUT_KEY_PAGE_UP: // numeric keypad 9
466 v->set_goal_view_offset( FG_PI * 1.75 );
470 FG_LOG( FG_INPUT, FG_DEBUG, "" );
472 case GLUT_KEY_F2: // F2 Reload Tile Cache...
475 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
477 globals->set_freeze( true );
479 if( global_tile_mgr.init() ) {
480 // Load the local scenery data
481 global_tile_mgr.update( cur_fdm_state->get_Longitude(),
482 cur_fdm_state->get_Latitude() );
484 FG_LOG( FG_GENERAL, FG_ALERT,
485 "Error in Tile Manager initialization!" );
490 globals->set_freeze( false );
493 case GLUT_KEY_F3: // F3 Take a screen shot
496 case GLUT_KEY_F6: // F6 toggles Autopilot target location
497 if ( current_autopilot->get_HeadingMode() !=
498 FGAutopilot::FG_HEADING_WAYPOINT ) {
499 current_autopilot->set_HeadingMode(
500 FGAutopilot::FG_HEADING_WAYPOINT );
502 current_autopilot->set_HeadingMode(
503 FGAutopilot::FG_HEADING_LOCK );
506 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
507 current_options.cycle_fog();
509 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
510 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
511 } else if ( current_options.get_fog() ==
512 fgOPTIONS::FG_FOG_FASTEST )
514 FG_LOG( FG_INPUT, FG_INFO,
515 "Fog enabled, hint set to fastest" );
516 } else if ( current_options.get_fog() ==
517 fgOPTIONS::FG_FOG_NICEST )
519 FG_LOG( FG_INPUT, FG_INFO,
520 "Fog enabled, hint set to nicest" );
524 case GLUT_KEY_F9: // F9 toggles textures on and off...
525 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
526 if ( current_options.get_textures() ) {
527 current_options.set_textures( false );
528 material_lib.set_step( 1 );
530 current_options.set_textures( true );
531 material_lib.set_step( 0 );
534 case GLUT_KEY_F10: // F10 toggles menu on and off...
535 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
538 case GLUT_KEY_F11: // F11 Altitude Dialog.
539 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
542 case GLUT_KEY_F12: // F12 Heading Dialog...
543 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
547 if ( current_autopilot->get_AltitudeEnabled() ) {
548 current_autopilot->AltitudeAdjust( -100 );
550 controls.move_elevator(0.05);
554 if ( current_autopilot->get_AltitudeEnabled() ) {
555 current_autopilot->AltitudeAdjust( 100 );
557 controls.move_elevator(-0.05);
561 controls.move_aileron(-0.05);
564 controls.move_aileron(0.05);
566 case GLUT_KEY_HOME: // numeric keypad 1
567 controls.move_elevator_trim(0.001);
569 case GLUT_KEY_END: // numeric keypad 7
570 controls.move_elevator_trim(-0.001);
572 case GLUT_KEY_INSERT: // numeric keypad Ins
573 if ( current_autopilot->get_HeadingEnabled() ) {
574 current_autopilot->HeadingAdjust( -1 );
576 controls.move_rudder(-0.05);
579 case 13: // numeric keypad Enter
580 if ( current_autopilot->get_HeadingEnabled() ) {
581 current_autopilot->HeadingAdjust( 1 );
583 controls.move_rudder(0.05);
586 case 53: // numeric keypad 5
587 controls.set_aileron(0.0);
588 controls.set_elevator(0.0);
589 controls.set_rudder(0.0);
591 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
592 if ( current_autopilot->get_AutoThrottleEnabled() ) {
593 current_autopilot->AutoThrottleAdjust( 5 );
595 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
598 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
599 if ( current_autopilot->get_AutoThrottleEnabled() ) {
600 current_autopilot->AutoThrottleAdjust( -5 );
602 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );