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"
77 extern void fgReshape( int width, int height );
80 // Handle keyboard events
81 void GLUTkey(unsigned char k, int x, int y) {
86 static bool winding_ccw = true;
89 f = current_aircraft.fdm_state;
90 v = globals->get_current_view();
92 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
93 if ( puKeyboard(k, PU_DOWN) ) {
97 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
98 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
100 case 1: // Ctrl-A key
101 current_autopilot->set_AltitudeMode(
102 FGAutopilot::FG_ALTITUDE_LOCK );
103 current_autopilot->set_AltitudeEnabled(
104 ! current_autopilot->get_AltitudeEnabled()
107 case 7: // Ctrl-G key
108 current_autopilot->set_AltitudeMode(
109 FGAutopilot::FG_ALTITUDE_GS1 );
110 current_autopilot->set_AltitudeEnabled(
111 ! current_autopilot->get_AltitudeEnabled()
114 case 8: // Ctrl-H key
115 current_autopilot->set_HeadingMode(
116 FGAutopilot::FG_HEADING_LOCK );
117 current_autopilot->set_HeadingEnabled(
118 ! current_autopilot->get_HeadingEnabled()
121 case 14: // Ctrl-N key
122 current_autopilot->set_HeadingMode(
123 FGAutopilot::FG_HEADING_NAV1 );
124 current_autopilot->set_HeadingEnabled(
125 ! current_autopilot->get_HeadingEnabled()
128 case 18: // Ctrl-R key
130 winding_ccw = !winding_ccw;
132 glFrontFace ( GL_CCW );
134 glFrontFace ( GL_CW );
137 case 19: // Ctrl-S key
138 current_autopilot->set_AutoThrottleEnabled(
139 ! current_autopilot->get_AutoThrottleEnabled()
142 case 20: // Ctrl-T key
143 current_autopilot->set_AltitudeMode(
144 FGAutopilot::FG_ALTITUDE_TERRAIN );
145 current_autopilot->set_AltitudeEnabled(
146 ! current_autopilot->get_AltitudeEnabled()
149 case 21: // Ctrl-U key
150 // add 1000' of emergency altitude. Possibly good for
151 // unflipping yourself :-)
153 double alt = cur_fdm_state->get_Altitude() + 1000;
154 fgFDMForceAltitude( globals->get_options()->get_flight_model(),
155 alt * FEET_TO_METER );
158 case 49: // numeric keypad 1
159 v->set_goal_view_offset( FG_PI * 0.75 );
160 if ( globals->get_options()->get_view_mode() ==
161 FGOptions::FG_VIEW_FOLLOW )
163 globals->get_pilot_view()->set_pilot_offset(-25.0, 25.0, 1.0);
164 v->set_view_offset( FG_PI * 0.75 );
167 case 50: // numeric keypad 2
168 v->set_goal_view_offset( FG_PI );
169 if ( globals->get_options()->get_view_mode() ==
170 FGOptions::FG_VIEW_FOLLOW )
172 globals->get_pilot_view()->set_pilot_offset(-25.0, 0.0, 1.0);
173 v->set_view_offset( FG_PI );
176 case 51: // numeric keypad 3
177 v->set_goal_view_offset( FG_PI * 1.25 );
178 if ( globals->get_options()->get_view_mode() ==
179 FGOptions::FG_VIEW_FOLLOW )
181 globals->get_pilot_view()->set_pilot_offset(-25.0, -25.0, 1.0);
182 v->set_view_offset( FG_PI * 1.25 );
185 case 52: // numeric keypad 4
186 v->set_goal_view_offset( FG_PI * 0.50 );
187 if ( globals->get_options()->get_view_mode() ==
188 FGOptions::FG_VIEW_FOLLOW )
190 globals->get_pilot_view()->set_pilot_offset(0.0, 25.0, 1.0);
191 v->set_view_offset( FG_PI * 0.50 );
194 case 54: // numeric keypad 6
195 v->set_goal_view_offset( FG_PI * 1.50 );
196 if ( globals->get_options()->get_view_mode() ==
197 FGOptions::FG_VIEW_FOLLOW )
199 globals->get_pilot_view()->set_pilot_offset(0.0, -25.0, 1.0);
200 v->set_view_offset( FG_PI * 1.50 );
203 case 55: // numeric keypad 7
204 v->set_goal_view_offset( FG_PI * 0.25 );
205 if ( globals->get_options()->get_view_mode() ==
206 FGOptions::FG_VIEW_FOLLOW )
208 globals->get_pilot_view()->set_pilot_offset(25.0, 25.0, 1.0);
209 v->set_view_offset( FG_PI * 0.25 );
212 case 56: // numeric keypad 8
213 v->set_goal_view_offset( 0.00 );
214 if ( globals->get_options()->get_view_mode() ==
215 FGOptions::FG_VIEW_FOLLOW )
217 globals->get_pilot_view()->set_pilot_offset(25.0, 0.0, 1.0);
218 v->set_view_offset( 0.00 );
221 case 57: // numeric keypad 9
222 v->set_goal_view_offset( FG_PI * 1.75 );
223 if ( globals->get_options()->get_view_mode() ==
224 FGOptions::FG_VIEW_FOLLOW )
226 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
227 v->set_view_offset( FG_PI * 1.75 );
231 speed = globals->get_options()->get_speed_up();
236 globals->get_options()->set_speed_up( speed );
239 // status = globals->get_options()->get_hud_status();
240 // globals->get_options()->set_hud_status(!status);
241 HUD_brightkey( true );
245 fgHUDInit2(¤t_aircraft);
248 globals->inc_warp( -60 );
249 fgUpdateSkyAndLightingParams();
252 globals->get_options()->toggle_panel();
255 globals->inc_warp_delta( -30 );
256 fgUpdateSkyAndLightingParams();
259 #if defined(FX) && !defined(WIN32)
260 global_fullscreen = ( !global_fullscreen );
261 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
262 XMesaSetFXmode( global_fullscreen ?
263 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
268 fov = globals->get_options()->get_fov();
270 if ( fov > FG_FOV_MAX ) {
273 globals->get_options()->set_fov(fov);
274 v->force_update_fov_math();
277 #ifndef FG_OLD_WEATHER
278 tmp = WeatherDatabase->getWeatherVisibility();
280 WeatherDatabase->setWeatherVisibility( tmp );
282 tmp = current_weather.get_visibility(); // in meters
284 current_weather.set_visibility( tmp );
289 FG_LOG( FG_INPUT, FG_DEBUG, "" );
291 case 50: // numeric keypad 2
292 if ( current_autopilot->get_AltitudeEnabled() ) {
293 current_autopilot->AltitudeAdjust( 100 );
295 controls.move_elevator(-0.05);
298 case 56: // numeric keypad 8
299 if ( current_autopilot->get_AltitudeEnabled() ) {
300 current_autopilot->AltitudeAdjust( -100 );
302 controls.move_elevator(0.05);
305 case 49: // numeric keypad 1
306 controls.move_elevator_trim(-0.001);
308 case 55: // numeric keypad 7
309 controls.move_elevator_trim(0.001);
311 case 52: // numeric keypad 4
312 controls.move_aileron(-0.05);
314 case 54: // numeric keypad 6
315 controls.move_aileron(0.05);
317 case 48: // numeric keypad Ins
318 if ( current_autopilot->get_HeadingEnabled() ) {
319 current_autopilot->HeadingAdjust( -1 );
321 controls.move_rudder(-0.05);
324 case 13: // numeric keypad Enter
325 if ( current_autopilot->get_HeadingEnabled() ) {
326 current_autopilot->HeadingAdjust( 1 );
328 controls.move_rudder(0.05);
331 case 53: // numeric keypad 5
332 controls.set_aileron(0.0);
333 controls.set_elevator(0.0);
334 controls.set_rudder(0.0);
336 case 57: // numeric keypad 9 (Pg Up)
337 if ( current_autopilot->get_AutoThrottleEnabled() ) {
338 current_autopilot->AutoThrottleAdjust( 5 );
340 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
343 case 51: // numeric keypad 3 (Pg Dn)
344 if ( current_autopilot->get_AutoThrottleEnabled() ) {
345 current_autopilot->AutoThrottleAdjust( -5 );
347 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
351 controls.move_flaps(-0.34);
352 FG_LOG( FG_INPUT, FG_INFO,
353 "Set flaps to " << controls.get_flaps() );
356 controls.move_flaps(0.34);
357 FG_LOG( FG_INPUT, FG_INFO,
358 "Set flaps to " << controls.get_flaps() );
361 speed = globals->get_options()->get_speed_up();
363 globals->get_options()->set_speed_up( speed );
368 b_ret = int( controls.get_brake( 0 ) );
369 b_set = double(!b_ret);
370 controls.set_brake( FGControls::ALL_WHEELS, b_set);
373 if (controls.get_brake(0) > 0.0) {
374 controls.set_brake(0, 0.0);
376 controls.set_brake(0, 1.0);
380 if (controls.get_brake(1) > 0.0) {
381 controls.set_brake(1, 0.0);
383 controls.set_brake(1, 1.0);
387 HUD_masterswitch( true );
390 fgHUDInit(¤t_aircraft); // normal HUD
393 globals->inc_warp( 60 );
394 fgUpdateSkyAndLightingParams();
397 globals->set_freeze( ! globals->get_freeze() );
400 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
401 f->get_Latitude() * RAD_TO_DEG );
402 FGPath tile_path( globals->get_options()->get_fg_root() );
403 tile_path.append( "Scenery" );
404 tile_path.append( p.gen_base_path() );
405 tile_path.append( p.gen_index_str() );
407 // printf position and attitude information
408 FG_LOG( FG_INPUT, FG_INFO,
409 "Lon = " << f->get_Longitude() * RAD_TO_DEG
410 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
411 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
413 FG_LOG( FG_INPUT, FG_INFO,
414 "Heading = " << f->get_Psi() * RAD_TO_DEG
415 << " Roll = " << f->get_Phi() * RAD_TO_DEG
416 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
417 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
421 globals->inc_warp_delta( 30 );
422 fgUpdateSkyAndLightingParams();
425 // globals->get_options()->cycle_view_mode();
426 if ( globals->get_options()->get_view_mode() ==
427 FGOptions::FG_VIEW_FOLLOW )
429 globals->get_options()->set_view_mode(FGOptions::FG_VIEW_PILOT);
430 v->set_goal_view_offset( 0.0 );
431 v->set_view_offset( 0.0 );
432 } else if ( globals->get_options()->get_view_mode() ==
433 FGOptions::FG_VIEW_PILOT )
435 globals->get_options()->set_view_mode( FGOptions::FG_VIEW_FOLLOW );
436 v->set_goal_view_offset( FG_PI * 1.75 );
437 v->set_view_offset( FG_PI * 1.75 );
438 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
440 fgReshape( globals->get_current_view()->get_winWidth(),
441 globals->get_current_view()->get_winHeight() );
444 fov = globals->get_options()->get_fov();
446 if ( fov < FG_FOV_MIN ) {
449 globals->get_options()->set_fov(fov);
450 v->force_update_fov_math();
453 #ifndef FG_OLD_WEATHER
454 tmp = WeatherDatabase->getWeatherVisibility();
456 WeatherDatabase->setWeatherVisibility( tmp );
458 tmp = current_weather.get_visibility(); // in meters
460 current_weather.set_visibility( tmp );
464 // if( fg_DebugOutput ) {
465 // fclose( fg_DebugOutput );
467 FG_LOG( FG_INPUT, FG_ALERT,
468 "Program exit requested." );
476 // Handle "special" keyboard events
477 void GLUTspecialkey(int k, int x, int y) {
480 v = globals->get_current_view();
482 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
484 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
488 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
489 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
492 ifstream input("fgfs.sav");
493 if (input.good() && fgLoadFlight(input)) {
495 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
497 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
502 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
503 ofstream output("fgfs.sav");
504 if (output.good() && fgSaveFlight(output)) {
506 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
508 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
514 current_properties.getStringValue("/sim/panel/path",
515 "Panels/Default/default.xml");
516 FGPanel * new_panel = fgReadPanel(panel_path);
517 if (new_panel == 0) {
518 FG_LOG(FG_INPUT, FG_ALERT,
519 "Error reading new panel from " << panel_path);
522 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
523 delete current_panel;
524 current_panel = new_panel;
528 FGPath props_path(globals->get_options()->get_fg_root());
529 props_path.append("preferences.xml");
530 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
531 if (!readPropertyList(props_path.str(), ¤t_properties)) {
532 FG_LOG(FG_INPUT, FG_ALERT,
533 "Failed to reread global preferences from "
534 << props_path.str());
536 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
541 current_panel->setYOffset(current_panel->getYOffset() - 5);
542 fgReshape(globals->get_current_view()->get_winWidth(),
543 globals->get_current_view()->get_winHeight());
547 current_panel->setYOffset(current_panel->getYOffset() + 5);
548 fgReshape(globals->get_current_view()->get_winWidth(),
549 globals->get_current_view()->get_winHeight());
553 current_panel->setXOffset(current_panel->getXOffset() - 5);
557 current_panel->setXOffset(current_panel->getXOffset() + 5);
560 case GLUT_KEY_END: // numeric keypad 1
561 v->set_goal_view_offset( FG_PI * 0.75 );
562 if ( globals->get_options()->get_view_mode() ==
563 FGOptions::FG_VIEW_FOLLOW)
565 globals->get_pilot_view()->set_pilot_offset(-25.0, 25.0, 1.0);
566 v->set_view_offset( FG_PI * 0.75 );
569 case GLUT_KEY_DOWN: // numeric keypad 2
570 v->set_goal_view_offset( FG_PI );
571 if ( globals->get_options()->get_view_mode() ==
572 FGOptions::FG_VIEW_FOLLOW )
574 globals->get_pilot_view()->set_pilot_offset(-25.0, 0.0, 1.0);
575 v->set_view_offset( FG_PI );
578 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
579 v->set_goal_view_offset( FG_PI * 1.25 );
580 if ( globals->get_options()->get_view_mode() ==
581 FGOptions::FG_VIEW_FOLLOW)
583 globals->get_pilot_view()->set_pilot_offset(-25.0, -25.0, 1.0);
584 v->set_view_offset( FG_PI * 1.25 );
587 case GLUT_KEY_LEFT: // numeric keypad 4
588 v->set_goal_view_offset( FG_PI * 0.50 );
589 if ( globals->get_options()->get_view_mode() ==
590 FGOptions::FG_VIEW_FOLLOW )
592 globals->get_pilot_view()->set_pilot_offset(0.0, 25.0, 1.0);
593 v->set_view_offset( FG_PI * 0.50 );
596 case GLUT_KEY_RIGHT: // numeric keypad 6
597 v->set_goal_view_offset( FG_PI * 1.50 );
598 if ( globals->get_options()->get_view_mode() ==
599 FGOptions::FG_VIEW_FOLLOW )
601 globals->get_pilot_view()->set_pilot_offset(0.0, -25.0, 1.0);
602 v->set_view_offset( FG_PI * 1.50 );
605 case GLUT_KEY_HOME: // numeric keypad 7
606 v->set_goal_view_offset( FG_PI * 0.25 );
607 if ( globals->get_options()->get_view_mode() ==
608 FGOptions::FG_VIEW_FOLLOW )
610 globals->get_pilot_view()->set_pilot_offset(25.0, 25.0, 1.0);
611 v->set_view_offset( FG_PI * 0.25 );
614 case GLUT_KEY_UP: // numeric keypad 8
615 v->set_goal_view_offset( 0.00 );
616 if ( globals->get_options()->get_view_mode() ==
617 FGOptions::FG_VIEW_FOLLOW )
619 globals->get_pilot_view()->set_pilot_offset(25.0, 0.0, 1.0);
620 v->set_view_offset( 0.00 );
623 case GLUT_KEY_PAGE_UP: // numeric keypad 9
624 v->set_goal_view_offset( FG_PI * 1.75 );
625 if ( globals->get_options()->get_view_mode() ==
626 FGOptions::FG_VIEW_FOLLOW )
628 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
629 v->set_view_offset( FG_PI * 1.75 );
634 FG_LOG( FG_INPUT, FG_DEBUG, "" );
636 case GLUT_KEY_F2: // F2 Reload Tile Cache...
639 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
641 globals->set_freeze( true );
643 if ( global_tile_mgr.init() ) {
644 // Load the local scenery data
645 global_tile_mgr.update(
646 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
647 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
649 FG_LOG( FG_GENERAL, FG_ALERT,
650 "Error in Tile Manager initialization!" );
655 globals->set_freeze( false );
658 case GLUT_KEY_F3: // F3 Take a screen shot
661 case GLUT_KEY_F6: // F6 toggles Autopilot target location
662 if ( current_autopilot->get_HeadingMode() !=
663 FGAutopilot::FG_HEADING_WAYPOINT ) {
664 current_autopilot->set_HeadingMode(
665 FGAutopilot::FG_HEADING_WAYPOINT );
666 current_autopilot->set_HeadingEnabled( true );
668 current_autopilot->set_HeadingMode(
669 FGAutopilot::FG_HEADING_LOCK );
672 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
673 globals->get_options()->cycle_fog();
675 if ( globals->get_options()->get_fog() ==
676 FGOptions::FG_FOG_DISABLED )
678 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
679 } else if ( globals->get_options()->get_fog() ==
680 FGOptions::FG_FOG_FASTEST )
682 FG_LOG( FG_INPUT, FG_INFO,
683 "Fog enabled, hint set to fastest" );
684 } else if ( globals->get_options()->get_fog() ==
685 FGOptions::FG_FOG_NICEST )
687 FG_LOG( FG_INPUT, FG_INFO,
688 "Fog enabled, hint set to nicest" );
692 case GLUT_KEY_F9: // F9 toggles textures on and off...
693 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
694 if ( globals->get_options()->get_textures() ) {
695 globals->get_options()->set_textures( false );
696 material_lib.set_step( 1 );
698 globals->get_options()->set_textures( true );
699 material_lib.set_step( 0 );
702 case GLUT_KEY_F10: // F10 toggles menu on and off...
703 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
706 case GLUT_KEY_F11: // F11 Altitude Dialog.
707 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
710 case GLUT_KEY_F12: // F12 Heading Dialog...
711 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
715 if ( current_autopilot->get_AltitudeEnabled() ) {
716 current_autopilot->AltitudeAdjust( -100 );
718 controls.move_elevator(0.05);
722 if ( current_autopilot->get_AltitudeEnabled() ) {
723 current_autopilot->AltitudeAdjust( 100 );
725 controls.move_elevator(-0.05);
729 controls.move_aileron(-0.05);
732 controls.move_aileron(0.05);
734 case GLUT_KEY_HOME: // numeric keypad 1
735 controls.move_elevator_trim(0.001);
737 case GLUT_KEY_END: // numeric keypad 7
738 controls.move_elevator_trim(-0.001);
740 case GLUT_KEY_INSERT: // numeric keypad Ins
741 if ( current_autopilot->get_HeadingEnabled() ) {
742 current_autopilot->HeadingAdjust( -1 );
744 controls.move_rudder(-0.05);
747 case 13: // numeric keypad Enter
748 if ( current_autopilot->get_HeadingEnabled() ) {
749 current_autopilot->HeadingAdjust( 1 );
751 controls.move_rudder(0.05);
754 case 53: // numeric keypad 5
755 controls.set_aileron(0.0);
756 controls.set_elevator(0.0);
757 controls.set_rudder(0.0);
759 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
760 if ( current_autopilot->get_AutoThrottleEnabled() ) {
761 current_autopilot->AutoThrottleAdjust( 5 );
763 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
766 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
767 if ( current_autopilot->get_AutoThrottleEnabled() ) {
768 current_autopilot->AutoThrottleAdjust( -5 );
770 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );