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 "options.hxx"
78 extern void fgReshape( int width, int height );
81 // Handle keyboard events
82 void GLUTkey(unsigned char k, int x, int y) {
87 static bool winding_ccw = true;
90 f = current_aircraft.fdm_state;
93 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
94 if ( puKeyboard(k, PU_DOWN) ) {
98 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
99 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
101 case 1: // Ctrl-A key
102 current_autopilot->set_AltitudeMode(
103 FGAutopilot::FG_ALTITUDE_LOCK );
104 current_autopilot->set_AltitudeEnabled(
105 ! current_autopilot->get_AltitudeEnabled()
108 case 7: // Ctrl-G key
109 current_autopilot->set_AltitudeMode(
110 FGAutopilot::FG_ALTITUDE_GS1 );
111 current_autopilot->set_AltitudeEnabled(
112 ! current_autopilot->get_AltitudeEnabled()
115 case 8: // Ctrl-H key
116 current_autopilot->set_HeadingMode(
117 FGAutopilot::FG_HEADING_LOCK );
118 current_autopilot->set_HeadingEnabled(
119 ! current_autopilot->get_HeadingEnabled()
122 case 14: // Ctrl-N key
123 current_autopilot->set_HeadingMode(
124 FGAutopilot::FG_HEADING_NAV1 );
125 current_autopilot->set_HeadingEnabled(
126 ! current_autopilot->get_HeadingEnabled()
129 case 18: // Ctrl-R key
131 winding_ccw = !winding_ccw;
133 glFrontFace ( GL_CCW );
135 glFrontFace ( GL_CW );
138 case 19: // Ctrl-S key
139 current_autopilot->set_AutoThrottleEnabled(
140 ! current_autopilot->get_AutoThrottleEnabled()
143 case 20: // Ctrl-T key
144 current_autopilot->set_AltitudeMode(
145 FGAutopilot::FG_ALTITUDE_TERRAIN );
146 current_autopilot->set_AltitudeEnabled(
147 ! current_autopilot->get_AltitudeEnabled()
150 case 21: // Ctrl-U key
151 // add 1000' of emergency altitude. Possibly good for
152 // unflipping yourself :-)
154 double alt = cur_fdm_state->get_Altitude() + 1000;
155 fgFDMForceAltitude( current_options.get_flight_model(),
156 alt * FEET_TO_METER );
159 case 49: // numeric keypad 1
160 v->set_goal_view_offset( FG_PI * 0.75 );
161 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
162 pilot_view.set_pilot_offset(-25.0, 25.0, 1.0);
163 v->set_view_offset( FG_PI * 0.75 );
166 case 50: // numeric keypad 2
167 v->set_goal_view_offset( FG_PI );
168 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
169 pilot_view.set_pilot_offset(-25.0, 0.0, 1.0);
170 v->set_view_offset( FG_PI );
173 case 51: // numeric keypad 3
174 v->set_goal_view_offset( FG_PI * 1.25 );
175 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
176 pilot_view.set_pilot_offset(-25.0, -25.0, 1.0);
177 v->set_view_offset( FG_PI * 1.25 );
180 case 52: // numeric keypad 4
181 v->set_goal_view_offset( FG_PI * 0.50 );
182 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
183 pilot_view.set_pilot_offset(0.0, 25.0, 1.0);
184 v->set_view_offset( FG_PI * 0.50 );
187 case 54: // numeric keypad 6
188 v->set_goal_view_offset( FG_PI * 1.50 );
189 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
190 pilot_view.set_pilot_offset(0.0, -25.0, 1.0);
191 v->set_view_offset( FG_PI * 1.50 );
194 case 55: // numeric keypad 7
195 v->set_goal_view_offset( FG_PI * 0.25 );
196 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
197 pilot_view.set_pilot_offset(25.0, 25.0, 1.0);
198 v->set_view_offset( FG_PI * 0.25 );
201 case 56: // numeric keypad 8
202 v->set_goal_view_offset( 0.00 );
203 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
204 pilot_view.set_pilot_offset(25.0, 0.0, 1.0);
205 v->set_view_offset( 0.00 );
208 case 57: // numeric keypad 9
209 v->set_goal_view_offset( FG_PI * 1.75 );
210 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
211 pilot_view.set_pilot_offset(25.0, -25.0, 1.0);
212 v->set_view_offset( FG_PI * 1.75 );
216 speed = current_options.get_speed_up();
221 current_options.set_speed_up( speed );
224 // status = current_options.get_hud_status();
225 // current_options.set_hud_status(!status);
226 HUD_brightkey( true );
230 fgHUDInit2(¤t_aircraft);
233 globals->inc_warp( -60 );
234 fgUpdateSkyAndLightingParams();
237 current_options.toggle_panel();
240 globals->inc_warp_delta( -30 );
241 fgUpdateSkyAndLightingParams();
244 #if defined(FX) && !defined(WIN32)
245 global_fullscreen = ( !global_fullscreen );
246 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
247 XMesaSetFXmode( global_fullscreen ?
248 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
253 fov = current_options.get_fov();
255 if ( fov > FG_FOV_MAX ) {
258 current_options.set_fov(fov);
259 v->force_update_fov_math();
262 #ifndef FG_OLD_WEATHER
263 tmp = WeatherDatabase->getWeatherVisibility();
265 WeatherDatabase->setWeatherVisibility( tmp );
267 tmp = current_weather.get_visibility(); // in meters
269 current_weather.set_visibility( tmp );
274 FG_LOG( FG_INPUT, FG_DEBUG, "" );
276 case 50: // numeric keypad 2
277 if ( current_autopilot->get_AltitudeEnabled() ) {
278 current_autopilot->AltitudeAdjust( 100 );
280 controls.move_elevator(-0.05);
283 case 56: // numeric keypad 8
284 if ( current_autopilot->get_AltitudeEnabled() ) {
285 current_autopilot->AltitudeAdjust( -100 );
287 controls.move_elevator(0.05);
290 case 49: // numeric keypad 1
291 controls.move_elevator_trim(-0.001);
293 case 55: // numeric keypad 7
294 controls.move_elevator_trim(0.001);
296 case 52: // numeric keypad 4
297 controls.move_aileron(-0.05);
299 case 54: // numeric keypad 6
300 controls.move_aileron(0.05);
302 case 48: // numeric keypad Ins
303 if ( current_autopilot->get_HeadingEnabled() ) {
304 current_autopilot->HeadingAdjust( -1 );
306 controls.move_rudder(-0.05);
309 case 13: // numeric keypad Enter
310 if ( current_autopilot->get_HeadingEnabled() ) {
311 current_autopilot->HeadingAdjust( 1 );
313 controls.move_rudder(0.05);
316 case 53: // numeric keypad 5
317 controls.set_aileron(0.0);
318 controls.set_elevator(0.0);
319 controls.set_rudder(0.0);
321 case 57: // numeric keypad 9 (Pg Up)
322 if ( current_autopilot->get_AutoThrottleEnabled() ) {
323 current_autopilot->AutoThrottleAdjust( 5 );
325 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
328 case 51: // numeric keypad 3 (Pg Dn)
329 if ( current_autopilot->get_AutoThrottleEnabled() ) {
330 current_autopilot->AutoThrottleAdjust( -5 );
332 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
336 controls.move_flaps(-0.34);
337 FG_LOG( FG_INPUT, FG_INFO,
338 "Set flaps to " << controls.get_flaps() );
341 controls.move_flaps(0.34);
342 FG_LOG( FG_INPUT, FG_INFO,
343 "Set flaps to " << controls.get_flaps() );
346 speed = current_options.get_speed_up();
348 current_options.set_speed_up( speed );
353 b_ret = int( controls.get_brake( 0 ) );
354 b_set = double(!b_ret);
355 controls.set_brake( FGControls::ALL_WHEELS, b_set);
358 if (controls.get_brake(0) > 0.0) {
359 controls.set_brake(0, 0.0);
361 controls.set_brake(0, 1.0);
365 if (controls.get_brake(1) > 0.0) {
366 controls.set_brake(1, 0.0);
368 controls.set_brake(1, 1.0);
372 HUD_masterswitch( true );
375 fgHUDInit(¤t_aircraft); // normal HUD
378 globals->inc_warp( 60 );
379 fgUpdateSkyAndLightingParams();
382 globals->set_freeze( ! globals->get_freeze() );
385 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
386 f->get_Latitude() * RAD_TO_DEG );
387 FGPath tile_path( current_options.get_fg_root() );
388 tile_path.append( "Scenery" );
389 tile_path.append( p.gen_base_path() );
390 tile_path.append( p.gen_index_str() );
392 // printf position and attitude information
393 FG_LOG( FG_INPUT, FG_INFO,
394 "Lon = " << f->get_Longitude() * RAD_TO_DEG
395 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
396 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
398 FG_LOG( FG_INPUT, FG_INFO,
399 "Heading = " << f->get_Psi() * RAD_TO_DEG
400 << " Roll = " << f->get_Phi() * RAD_TO_DEG
401 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
402 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
406 globals->inc_warp_delta( 30 );
407 fgUpdateSkyAndLightingParams();
410 // current_options.cycle_view_mode();
411 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
412 current_options.set_view_mode(fgOPTIONS::FG_VIEW_PILOT);
413 v->set_goal_view_offset( 0.0 );
414 v->set_view_offset( 0.0 );
415 } else if (current_options.get_view_mode() ==
416 fgOPTIONS::FG_VIEW_PILOT) {
417 current_options.set_view_mode(fgOPTIONS::FG_VIEW_FOLLOW);
418 v->set_goal_view_offset( FG_PI * 1.75 );
419 v->set_view_offset( FG_PI * 1.75 );
420 pilot_view.set_pilot_offset(25.0, -25.0, 1.0);
422 fgReshape( current_view.get_winWidth(),
423 current_view.get_winHeight() );
426 fov = current_options.get_fov();
428 if ( fov < FG_FOV_MIN ) {
431 current_options.set_fov(fov);
432 v->force_update_fov_math();
435 #ifndef FG_OLD_WEATHER
436 tmp = WeatherDatabase->getWeatherVisibility();
438 WeatherDatabase->setWeatherVisibility( tmp );
440 tmp = current_weather.get_visibility(); // in meters
442 current_weather.set_visibility( tmp );
446 // if( fg_DebugOutput ) {
447 // fclose( fg_DebugOutput );
449 FG_LOG( FG_INPUT, FG_ALERT,
450 "Program exit requested." );
458 // Handle "special" keyboard events
459 void GLUTspecialkey(int k, int x, int y) {
464 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
466 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
470 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
471 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
474 ifstream input("fgfs.sav");
475 if (input.good() && fgLoadFlight(input)) {
477 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
479 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
484 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
485 ofstream output("fgfs.sav");
486 if (output.good() && fgSaveFlight(output)) {
488 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
490 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
496 current_properties.getStringValue("/sim/panel/path",
497 "Panels/Default/default.xml");
498 FGPanel * new_panel = fgReadPanel(panel_path);
499 if (new_panel == 0) {
500 FG_LOG(FG_INPUT, FG_ALERT,
501 "Error reading new panel from " << panel_path);
504 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
505 delete current_panel;
506 current_panel = new_panel;
510 FGPath props_path(current_options.get_fg_root());
511 props_path.append("preferences.xml");
512 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
513 if (!readPropertyList(props_path.str(), ¤t_properties)) {
514 FG_LOG(FG_INPUT, FG_ALERT,
515 "Failed to reread global preferences from "
516 << props_path.str());
518 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
523 current_panel->setYOffset(current_panel->getYOffset() - 5);
524 fgReshape(current_view.get_winWidth(),
525 current_view.get_winHeight());
529 current_panel->setYOffset(current_panel->getYOffset() + 5);
530 fgReshape(current_view.get_winWidth(),
531 current_view.get_winHeight());
535 current_panel->setXOffset(current_panel->getXOffset() - 5);
539 current_panel->setXOffset(current_panel->getXOffset() + 5);
542 case GLUT_KEY_END: // numeric keypad 1
543 v->set_goal_view_offset( FG_PI * 0.75 );
544 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
545 pilot_view.set_pilot_offset(-25.0, 25.0, 1.0);
546 v->set_view_offset( FG_PI * 0.75 );
549 case GLUT_KEY_DOWN: // numeric keypad 2
550 v->set_goal_view_offset( FG_PI );
551 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
552 pilot_view.set_pilot_offset(-25.0, 0.0, 1.0);
553 v->set_view_offset( FG_PI );
556 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
557 v->set_goal_view_offset( FG_PI * 1.25 );
558 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
559 pilot_view.set_pilot_offset(-25.0, -25.0, 1.0);
560 v->set_view_offset( FG_PI * 1.25 );
563 case GLUT_KEY_LEFT: // numeric keypad 4
564 v->set_goal_view_offset( FG_PI * 0.50 );
565 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
566 pilot_view.set_pilot_offset(0.0, 25.0, 1.0);
567 v->set_view_offset( FG_PI * 0.50 );
570 case GLUT_KEY_RIGHT: // numeric keypad 6
571 v->set_goal_view_offset( FG_PI * 1.50 );
572 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
573 pilot_view.set_pilot_offset(0.0, -25.0, 1.0);
574 v->set_view_offset( FG_PI * 1.50 );
577 case GLUT_KEY_HOME: // numeric keypad 7
578 v->set_goal_view_offset( FG_PI * 0.25 );
579 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
580 pilot_view.set_pilot_offset(25.0, 25.0, 1.0);
581 v->set_view_offset( FG_PI * 0.25 );
584 case GLUT_KEY_UP: // numeric keypad 8
585 v->set_goal_view_offset( 0.00 );
586 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
587 pilot_view.set_pilot_offset(25.0, 0.0, 1.0);
588 v->set_view_offset( 0.00 );
591 case GLUT_KEY_PAGE_UP: // numeric keypad 9
592 v->set_goal_view_offset( FG_PI * 1.75 );
593 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
594 pilot_view.set_pilot_offset(25.0, -25.0, 1.0);
595 v->set_view_offset( FG_PI * 1.75 );
600 FG_LOG( FG_INPUT, FG_DEBUG, "" );
602 case GLUT_KEY_F2: // F2 Reload Tile Cache...
605 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
607 globals->set_freeze( true );
609 if ( global_tile_mgr.init() ) {
610 // Load the local scenery data
611 global_tile_mgr.update(
612 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
613 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
615 FG_LOG( FG_GENERAL, FG_ALERT,
616 "Error in Tile Manager initialization!" );
621 globals->set_freeze( false );
624 case GLUT_KEY_F3: // F3 Take a screen shot
627 case GLUT_KEY_F6: // F6 toggles Autopilot target location
628 if ( current_autopilot->get_HeadingMode() !=
629 FGAutopilot::FG_HEADING_WAYPOINT ) {
630 current_autopilot->set_HeadingMode(
631 FGAutopilot::FG_HEADING_WAYPOINT );
632 current_autopilot->set_HeadingEnabled( true );
634 current_autopilot->set_HeadingMode(
635 FGAutopilot::FG_HEADING_LOCK );
638 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
639 current_options.cycle_fog();
641 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
642 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
643 } else if ( current_options.get_fog() ==
644 fgOPTIONS::FG_FOG_FASTEST )
646 FG_LOG( FG_INPUT, FG_INFO,
647 "Fog enabled, hint set to fastest" );
648 } else if ( current_options.get_fog() ==
649 fgOPTIONS::FG_FOG_NICEST )
651 FG_LOG( FG_INPUT, FG_INFO,
652 "Fog enabled, hint set to nicest" );
656 case GLUT_KEY_F9: // F9 toggles textures on and off...
657 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
658 if ( current_options.get_textures() ) {
659 current_options.set_textures( false );
660 material_lib.set_step( 1 );
662 current_options.set_textures( true );
663 material_lib.set_step( 0 );
666 case GLUT_KEY_F10: // F10 toggles menu on and off...
667 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
670 case GLUT_KEY_F11: // F11 Altitude Dialog.
671 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
674 case GLUT_KEY_F12: // F12 Heading Dialog...
675 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
679 if ( current_autopilot->get_AltitudeEnabled() ) {
680 current_autopilot->AltitudeAdjust( -100 );
682 controls.move_elevator(0.05);
686 if ( current_autopilot->get_AltitudeEnabled() ) {
687 current_autopilot->AltitudeAdjust( 100 );
689 controls.move_elevator(-0.05);
693 controls.move_aileron(-0.05);
696 controls.move_aileron(0.05);
698 case GLUT_KEY_HOME: // numeric keypad 1
699 controls.move_elevator_trim(0.001);
701 case GLUT_KEY_END: // numeric keypad 7
702 controls.move_elevator_trim(-0.001);
704 case GLUT_KEY_INSERT: // numeric keypad Ins
705 if ( current_autopilot->get_HeadingEnabled() ) {
706 current_autopilot->HeadingAdjust( -1 );
708 controls.move_rudder(-0.05);
711 case 13: // numeric keypad Enter
712 if ( current_autopilot->get_HeadingEnabled() ) {
713 current_autopilot->HeadingAdjust( 1 );
715 controls.move_rudder(0.05);
718 case 53: // numeric keypad 5
719 controls.set_aileron(0.0);
720 controls.set_elevator(0.0);
721 controls.set_rudder(0.0);
723 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
724 if ( current_autopilot->get_AutoThrottleEnabled() ) {
725 current_autopilot->AutoThrottleAdjust( 5 );
727 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
730 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
731 if ( current_autopilot->get_AutoThrottleEnabled() ) {
732 current_autopilot->AutoThrottleAdjust( -5 );
734 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );