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"
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_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 FGBFI::setAltitude(FGBFI::getAltitude() + 1000);
150 // double alt = cur_fdm_state->get_Altitude() + 1000;
151 // fgFDMForceAltitude( globals->get_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 = globals->get_options()->get_speed_up();
185 globals->get_options()->set_speed_up( speed );
188 // status = globals->get_options()->get_hud_status();
189 // globals->get_options()->set_hud_status(!status);
190 HUD_brightkey( true );
194 fgHUDInit2(¤t_aircraft);
197 globals->inc_warp( -60 );
198 fgUpdateSkyAndLightingParams();
201 globals->get_options()->toggle_panel();
204 globals->inc_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 = globals->get_current_view()->get_fov();
219 if ( fov > FG_FOV_MAX ) {
222 globals->get_current_view()->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 = globals->get_options()->get_speed_up();
312 globals->get_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 globals->inc_warp( 60 );
343 fgUpdateSkyAndLightingParams();
346 globals->set_freeze( ! globals->get_freeze() );
349 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
350 f->get_Latitude() * RAD_TO_DEG );
351 FGPath tile_path( globals->get_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 globals->inc_warp_delta( 30 );
371 fgUpdateSkyAndLightingParams();
374 globals->set_current_view( globals->get_viewmgr()->next_view() );
375 fgReshape( globals->get_options()->get_xsize(),
376 globals->get_options()->get_ysize() );
379 fov = globals->get_current_view()->get_fov();
381 if ( fov < FG_FOV_MIN ) {
384 globals->get_current_view()->set_fov(fov);
385 // v->force_update_fov_math();
388 #ifndef FG_OLD_WEATHER
389 tmp = WeatherDatabase->getWeatherVisibility();
391 WeatherDatabase->setWeatherVisibility( tmp );
393 tmp = current_weather.get_visibility(); // in meters
395 current_weather.set_visibility( tmp );
399 // if( fg_DebugOutput ) {
400 // fclose( fg_DebugOutput );
402 FG_LOG( FG_INPUT, FG_ALERT,
403 "Program exit requested." );
411 // Handle "special" keyboard events
412 void GLUTspecialkey(int k, int x, int y) {
413 FGViewer *v = globals->get_current_view();
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");
447 current_properties.getStringValue("/sim/panel/path",
448 "Panels/Default/default.xml");
449 FGPanel * new_panel = fgReadPanel(panel_path);
450 if (new_panel == 0) {
451 FG_LOG(FG_INPUT, FG_ALERT,
452 "Error reading new panel from " << panel_path);
455 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
456 delete current_panel;
457 current_panel = new_panel;
461 FGPath props_path(globals->get_options()->get_fg_root());
462 props_path.append("preferences.xml");
463 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
464 if (!readPropertyList(props_path.str(), ¤t_properties)) {
465 FG_LOG(FG_INPUT, FG_ALERT,
466 "Failed to reread global preferences from "
467 << props_path.str());
469 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
474 current_panel->setYOffset(current_panel->getYOffset() - 5);
475 fgReshape(globals->get_options()->get_xsize(),
476 globals->get_options()->get_ysize());
480 current_panel->setYOffset(current_panel->getYOffset() + 5);
481 fgReshape(globals->get_options()->get_xsize(),
482 globals->get_options()->get_ysize());
486 current_panel->setXOffset(current_panel->getXOffset() - 5);
490 current_panel->setXOffset(current_panel->getXOffset() + 5);
493 case GLUT_KEY_END: // numeric keypad 1
494 v->set_goal_view_offset( FG_PI * 0.75 );
496 case GLUT_KEY_DOWN: // numeric keypad 2
497 v->set_goal_view_offset( FG_PI );
499 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
500 v->set_goal_view_offset( FG_PI * 1.25 );
502 case GLUT_KEY_LEFT: // numeric keypad 4
503 v->set_goal_view_offset( FG_PI * 0.50 );
505 case GLUT_KEY_RIGHT: // numeric keypad 6
506 v->set_goal_view_offset( FG_PI * 1.50 );
508 case GLUT_KEY_HOME: // numeric keypad 7
509 v->set_goal_view_offset( FG_PI * 0.25 );
511 case GLUT_KEY_UP: // numeric keypad 8
512 v->set_goal_view_offset( 0.00 );
514 case GLUT_KEY_PAGE_UP: // numeric keypad 9
515 v->set_goal_view_offset( FG_PI * 1.75 );
519 FG_LOG( FG_INPUT, FG_DEBUG, "" );
521 case GLUT_KEY_F2: // F2 Reload Tile Cache...
523 bool freeze = globals->get_freeze();
524 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
526 globals->set_freeze( true );
528 if ( global_tile_mgr.init() ) {
529 // Load the local scenery data
530 global_tile_mgr.update(
531 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
532 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
534 FG_LOG( FG_GENERAL, FG_ALERT,
535 "Error in Tile Manager initialization!" );
540 globals->set_freeze( false );
543 case GLUT_KEY_F3: // F3 Take a screen shot
546 case GLUT_KEY_F4: // F4 Update lighting manually
547 fgUpdateSkyAndLightingParams();
548 case GLUT_KEY_F6: // F6 toggles Autopilot target location
549 if ( current_autopilot->get_HeadingMode() !=
550 FGAutopilot::FG_HEADING_WAYPOINT ) {
551 current_autopilot->set_HeadingMode(
552 FGAutopilot::FG_HEADING_WAYPOINT );
553 current_autopilot->set_HeadingEnabled( true );
555 current_autopilot->set_HeadingMode(
556 FGAutopilot::FG_HEADING_LOCK );
559 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
560 globals->get_options()->cycle_fog();
562 if ( globals->get_options()->get_fog() ==
563 FGOptions::FG_FOG_DISABLED )
565 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
566 } else if ( globals->get_options()->get_fog() ==
567 FGOptions::FG_FOG_FASTEST )
569 FG_LOG( FG_INPUT, FG_INFO,
570 "Fog enabled, hint set to fastest" );
571 } else if ( globals->get_options()->get_fog() ==
572 FGOptions::FG_FOG_NICEST )
574 FG_LOG( FG_INPUT, FG_INFO,
575 "Fog enabled, hint set to nicest" );
579 case GLUT_KEY_F9: // F9 toggles textures on and off...
580 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
581 if ( globals->get_options()->get_textures() ) {
582 globals->get_options()->set_textures( false );
583 material_lib.set_step( 1 );
585 globals->get_options()->set_textures( true );
586 material_lib.set_step( 0 );
589 case GLUT_KEY_F10: // F10 toggles menu on and off...
590 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
593 case GLUT_KEY_F11: // F11 Altitude Dialog.
594 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
597 case GLUT_KEY_F12: // F12 Heading Dialog...
598 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
602 if ( current_autopilot->get_AltitudeEnabled() ) {
603 current_autopilot->AltitudeAdjust( -100 );
605 controls.move_elevator(0.05);
609 if ( current_autopilot->get_AltitudeEnabled() ) {
610 current_autopilot->AltitudeAdjust( 100 );
612 controls.move_elevator(-0.05);
616 controls.move_aileron(-0.05);
619 controls.move_aileron(0.05);
621 case GLUT_KEY_HOME: // numeric keypad 1
622 controls.move_elevator_trim(0.001);
624 case GLUT_KEY_END: // numeric keypad 7
625 controls.move_elevator_trim(-0.001);
627 case GLUT_KEY_INSERT: // numeric keypad Ins
628 if ( current_autopilot->get_HeadingEnabled() ) {
629 current_autopilot->HeadingAdjust( -1 );
631 controls.move_rudder(-0.05);
634 case 13: // numeric keypad Enter
635 if ( current_autopilot->get_HeadingEnabled() ) {
636 current_autopilot->HeadingAdjust( 1 );
638 controls.move_rudder(0.05);
641 case 53: // numeric keypad 5
642 controls.set_aileron(0.0);
643 controls.set_elevator(0.0);
644 controls.set_rudder(0.0);
646 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
647 if ( current_autopilot->get_AutoThrottleEnabled() ) {
648 current_autopilot->AutoThrottleAdjust( 5 );
650 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
653 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
654 if ( current_autopilot->get_AutoThrottleEnabled() ) {
655 current_autopilot->AutoThrottleAdjust( -5 );
657 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );