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>
56 #include <Cockpit/panel.hxx>
57 #include <Cockpit/panel_io.hxx>
59 #include <Scenery/tilemgr.hxx>
60 #include <Objects/matlib.hxx>
61 #include <Time/light.hxx>
62 #include <Time/tmp.hxx>
64 #ifndef FG_OLD_WEATHER
65 # include <WeatherCM/FGLocalWeatherDatabase.h>
67 # include <Weather/weather.hxx>
71 #include "globals.hxx"
72 #include "keyboard.hxx"
73 #include "fg_props.hxx"
74 #include "options.hxx"
76 extern void fgReshape( int width, int height );
79 // Handle keyboard events
80 void GLUTkey(unsigned char k, int x, int y) {
82 static bool winding_ccw = true;
85 FGInterface *f = current_aircraft.fdm_state;
86 FGViewer *v = globals->get_current_view();
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_TC_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( fgGetString("/sim/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 = fgGetInt("/sim/speed-up");
184 fgSetInt("/sim/speed-up", speed);
187 HUD_brightkey( true );
191 fgHUDInit2(¤t_aircraft);
194 globals->inc_warp( -60 );
195 fgUpdateSkyAndLightingParams();
198 if (fgGetBool("/sim/panel/visibility"))
199 fgSetBool("/sim/panel/visibility", false);
201 fgSetBool("/sim/panel/visibility", true);
202 fgReshape(fgGetInt("/sim/startup/xsize"),
203 fgGetInt("/sim/startup/ysize"));
206 globals->inc_warp_delta( -30 );
207 fgUpdateSkyAndLightingParams();
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 = globals->get_current_view()->get_fov();
221 if ( fov > FG_FOV_MAX ) {
224 globals->get_current_view()->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 = fgGetInt("/sim/speed-up");
314 fgSetInt("/sim/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 if (controls.get_brake(0) > 0.0) {
325 controls.set_brake(0, 0.0);
327 controls.set_brake(0, 1.0);
331 if (controls.get_brake(1) > 0.0) {
332 controls.set_brake(1, 0.0);
334 controls.set_brake(1, 1.0);
338 HUD_masterswitch( true );
341 fgHUDInit(¤t_aircraft); // normal HUD
344 globals->inc_warp( 60 );
345 fgUpdateSkyAndLightingParams();
348 globals->set_freeze( ! globals->get_freeze() );
351 SGBucket p( f->get_Longitude() * RAD_TO_DEG,
352 f->get_Latitude() * RAD_TO_DEG );
353 FGPath tile_path( globals->get_fg_root() );
354 tile_path.append( "Scenery" );
355 tile_path.append( p.gen_base_path() );
356 tile_path.append( p.gen_index_str() );
358 // printf position and attitude information
359 FG_LOG( FG_INPUT, FG_INFO,
360 "Lon = " << f->get_Longitude() * RAD_TO_DEG
361 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
362 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
364 FG_LOG( FG_INPUT, FG_INFO,
365 "Heading = " << f->get_Psi() * RAD_TO_DEG
366 << " Roll = " << f->get_Phi() * RAD_TO_DEG
367 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
368 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
372 globals->inc_warp_delta( 30 );
373 fgUpdateSkyAndLightingParams();
376 // handles GUI state as well as Viewer LookAt Direction
378 globals->set_current_view( globals->get_viewmgr()->next_view() );
379 fgReshape( fgGetInt("/sim/startup/xsize"),
380 fgGetInt("/sim/startup/ysize") );
383 fov = globals->get_current_view()->get_fov();
385 if ( fov < FG_FOV_MIN ) {
388 globals->get_current_view()->set_fov(fov);
389 // v->force_update_fov_math();
392 #ifndef FG_OLD_WEATHER
393 tmp = WeatherDatabase->getWeatherVisibility();
395 WeatherDatabase->setWeatherVisibility( tmp );
397 tmp = current_weather.get_visibility(); // in meters
399 current_weather.set_visibility( tmp );
403 // if( fg_DebugOutput ) {
404 // fclose( fg_DebugOutput );
406 FG_LOG( FG_INPUT, FG_ALERT,
407 "Program exit requested." );
415 // Handle "special" keyboard events
416 void GLUTspecialkey(int k, int x, int y) {
417 FGViewer *v = globals->get_current_view();
419 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
421 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
425 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
426 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
429 ifstream input("fgfs.sav");
430 if (input.good() && fgLoadFlight(input)) {
432 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
434 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
439 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
440 cerr << "Opening output stream" << endl;
441 ofstream output("fgfs.sav");
442 cerr << "output stream opened" << endl;
443 if (output.good() && fgSaveFlight(output)) {
445 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
447 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
453 fgGetString("/sim/panel/path", "Panels/Default/default.xml");
454 FGPanel * new_panel = fgReadPanel(panel_path);
455 if (new_panel == 0) {
456 FG_LOG(FG_INPUT, FG_ALERT,
457 "Error reading new panel from " << panel_path);
460 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
461 current_panel->unbind();
462 delete current_panel;
463 current_panel = new_panel;
467 FGPath props_path(globals->get_fg_root());
468 props_path.append("preferences.xml");
469 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
470 if (!readProperties(props_path.str(), globals->get_props())) {
471 FG_LOG(FG_INPUT, FG_ALERT,
472 "Failed to reread global preferences from "
473 << props_path.str());
475 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
480 current_panel->setYOffset(current_panel->getYOffset() - 5);
481 fgReshape(fgGetInt("/sim/startup/xsize"),
482 fgGetInt("/sim/startup/ysize"));
486 current_panel->setYOffset(current_panel->getYOffset() + 5);
487 fgReshape(fgGetInt("/sim/startup/xsize"),
488 fgGetInt("/sim/startup/ysize"));
492 current_panel->setXOffset(current_panel->getXOffset() - 5);
496 current_panel->setXOffset(current_panel->getXOffset() + 5);
499 case GLUT_KEY_END: // numeric keypad 1
500 v->set_goal_view_offset( FG_PI * 0.75 );
502 case GLUT_KEY_DOWN: // numeric keypad 2
503 v->set_goal_view_offset( FG_PI );
505 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
506 v->set_goal_view_offset( FG_PI * 1.25 );
508 case GLUT_KEY_LEFT: // numeric keypad 4
509 v->set_goal_view_offset( FG_PI * 0.50 );
511 case GLUT_KEY_RIGHT: // numeric keypad 6
512 v->set_goal_view_offset( FG_PI * 1.50 );
514 case GLUT_KEY_HOME: // numeric keypad 7
515 v->set_goal_view_offset( FG_PI * 0.25 );
517 case GLUT_KEY_UP: // numeric keypad 8
518 v->set_goal_view_offset( 0.00 );
520 case GLUT_KEY_PAGE_UP: // numeric keypad 9
521 v->set_goal_view_offset( FG_PI * 1.75 );
525 FG_LOG( FG_INPUT, FG_DEBUG, "" );
527 case GLUT_KEY_F2: // F2 Reload Tile Cache...
529 bool freeze = globals->get_freeze();
530 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
532 globals->set_freeze( true );
534 if ( global_tile_mgr.init() ) {
535 // Load the local scenery data
536 global_tile_mgr.update(
537 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
538 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
540 FG_LOG( FG_GENERAL, FG_ALERT,
541 "Error in Tile Manager initialization!" );
546 globals->set_freeze( false );
549 case GLUT_KEY_F3: // F3 Take a screen shot
552 case GLUT_KEY_F4: // F4 Update lighting manually
553 fgUpdateSkyAndLightingParams();
554 case GLUT_KEY_F6: // F6 toggles Autopilot target location
555 if ( current_autopilot->get_HeadingMode() !=
556 FGAutopilot::FG_HEADING_WAYPOINT ) {
557 current_autopilot->set_HeadingMode(
558 FGAutopilot::FG_HEADING_WAYPOINT );
559 current_autopilot->set_HeadingEnabled( true );
561 current_autopilot->set_HeadingMode(
562 FGAutopilot::FG_TC_HEADING_LOCK );
565 case GLUT_KEY_F8: {// F8 toggles fog ... off fastest nicest...
566 const string &fog = fgGetString("/sim/rendering/fog");
567 if (fog == "disabled") {
568 fgSetString("/sim/rendering/fog", "fastest");
569 FG_LOG(FG_INPUT, FG_INFO, "Fog enabled, hint=fastest");
570 } else if (fog == "fastest") {
571 fgSetString("/sim/rendering/fog", "nicest");
572 FG_LOG(FG_INPUT, FG_INFO, "Fog enabled, hint=nicest");
573 } else if (fog == "nicest") {
574 fgSetString("/sim/rendering/fog", "disabled");
575 FG_LOG(FG_INPUT, FG_INFO, "Fog disabled");
577 fgSetString("/sim/rendering/fog", "disabled");
578 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized fog type "
579 << fog << ", changed to 'disabled'");
583 case GLUT_KEY_F9: // F9 toggles textures on and off...
584 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
585 if ( fgGetBool("/sim/rendering/textures")) {
586 fgSetBool("/sim/rendering/textures", false);
587 material_lib.set_step( 1 );
589 fgSetBool("/sim/rendering/textures", true);
590 material_lib.set_step( 0 );
593 case GLUT_KEY_F10: // F10 toggles menu on and off...
594 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
597 case GLUT_KEY_F11: // F11 Altitude Dialog.
598 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
601 case GLUT_KEY_F12: // F12 Heading Dialog...
602 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
606 if ( current_autopilot->get_AltitudeEnabled() ) {
607 current_autopilot->AltitudeAdjust( -100 );
609 controls.move_elevator(0.05);
613 if ( current_autopilot->get_AltitudeEnabled() ) {
614 current_autopilot->AltitudeAdjust( 100 );
616 controls.move_elevator(-0.05);
620 controls.move_aileron(-0.05);
623 controls.move_aileron(0.05);
625 case GLUT_KEY_HOME: // numeric keypad 1
626 controls.move_elevator_trim(0.001);
628 case GLUT_KEY_END: // numeric keypad 7
629 controls.move_elevator_trim(-0.001);
631 case GLUT_KEY_INSERT: // numeric keypad Ins
632 if ( current_autopilot->get_HeadingEnabled() ) {
633 current_autopilot->HeadingAdjust( -1 );
635 controls.move_rudder(-0.05);
638 case 13: // numeric keypad Enter
639 if ( current_autopilot->get_HeadingEnabled() ) {
640 current_autopilot->HeadingAdjust( 1 );
642 controls.move_rudder(0.05);
645 case 53: // numeric keypad 5
646 controls.set_aileron(0.0);
647 controls.set_elevator(0.0);
648 controls.set_rudder(0.0);
650 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
651 if ( current_autopilot->get_AutoThrottleEnabled() ) {
652 current_autopilot->AutoThrottleAdjust( 5 );
654 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
657 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
658 if ( current_autopilot->get_AutoThrottleEnabled() ) {
659 current_autopilot->AutoThrottleAdjust( -5 );
661 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );