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 FGViewerRPH *v = (FGViewerRPH *)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 double alt = cur_fdm_state->get_Altitude() + 1000;
150 fgFDMForceAltitude( globals->get_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 );
156 if ( globals->get_options()->get_view_mode() ==
157 FGOptions::FG_VIEW_FOLLOW )
159 globals->get_current_view()->set_pilot_offset(-25.0, 25.0, 1.0);
160 v->set_view_offset( FG_PI * 0.75 );
163 case 50: // numeric keypad 2
164 v->set_goal_view_offset( FG_PI );
165 if ( globals->get_options()->get_view_mode() ==
166 FGOptions::FG_VIEW_FOLLOW )
168 globals->get_current_view()->set_pilot_offset(-25.0, 0.0, 1.0);
169 v->set_view_offset( FG_PI );
172 case 51: // numeric keypad 3
173 v->set_goal_view_offset( FG_PI * 1.25 );
174 if ( globals->get_options()->get_view_mode() ==
175 FGOptions::FG_VIEW_FOLLOW )
177 globals->get_current_view()->set_pilot_offset(-25.0, -25.0, 1.0);
178 v->set_view_offset( FG_PI * 1.25 );
181 case 52: // numeric keypad 4
182 v->set_goal_view_offset( FG_PI * 0.50 );
183 if ( globals->get_options()->get_view_mode() ==
184 FGOptions::FG_VIEW_FOLLOW )
186 globals->get_current_view()->set_pilot_offset(0.0, 25.0, 1.0);
187 v->set_view_offset( FG_PI * 0.50 );
190 case 54: // numeric keypad 6
191 v->set_goal_view_offset( FG_PI * 1.50 );
192 if ( globals->get_options()->get_view_mode() ==
193 FGOptions::FG_VIEW_FOLLOW )
195 globals->get_current_view()->set_pilot_offset(0.0, -25.0, 1.0);
196 v->set_view_offset( FG_PI * 1.50 );
199 case 55: // numeric keypad 7
200 v->set_goal_view_offset( FG_PI * 0.25 );
201 if ( globals->get_options()->get_view_mode() ==
202 FGOptions::FG_VIEW_FOLLOW )
204 globals->get_current_view()->set_pilot_offset(25.0, 25.0, 1.0);
205 v->set_view_offset( FG_PI * 0.25 );
208 case 56: // numeric keypad 8
209 v->set_goal_view_offset( 0.00 );
210 if ( globals->get_options()->get_view_mode() ==
211 FGOptions::FG_VIEW_FOLLOW )
213 globals->get_current_view()->set_pilot_offset(25.0, 0.0, 1.0);
214 v->set_view_offset( 0.00 );
217 case 57: // numeric keypad 9
218 v->set_goal_view_offset( FG_PI * 1.75 );
219 if ( globals->get_options()->get_view_mode() ==
220 FGOptions::FG_VIEW_FOLLOW )
222 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
223 v->set_view_offset( FG_PI * 1.75 );
227 speed = globals->get_options()->get_speed_up();
232 globals->get_options()->set_speed_up( speed );
235 // status = globals->get_options()->get_hud_status();
236 // globals->get_options()->set_hud_status(!status);
237 HUD_brightkey( true );
241 fgHUDInit2(¤t_aircraft);
244 globals->inc_warp( -60 );
245 fgUpdateSkyAndLightingParams();
248 globals->get_options()->toggle_panel();
251 globals->inc_warp_delta( -30 );
252 fgUpdateSkyAndLightingParams();
255 #if defined(FX) && !defined(WIN32)
256 global_fullscreen = ( !global_fullscreen );
257 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
258 XMesaSetFXmode( global_fullscreen ?
259 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
264 fov = globals->get_options()->get_fov();
266 if ( fov > FG_FOV_MAX ) {
269 globals->get_options()->set_fov(fov);
270 // v->force_update_fov_math();
273 #ifndef FG_OLD_WEATHER
274 tmp = WeatherDatabase->getWeatherVisibility();
276 WeatherDatabase->setWeatherVisibility( tmp );
278 tmp = current_weather.get_visibility(); // in meters
280 current_weather.set_visibility( tmp );
285 FG_LOG( FG_INPUT, FG_DEBUG, "" );
287 case 50: // numeric keypad 2
288 if ( current_autopilot->get_AltitudeEnabled() ) {
289 current_autopilot->AltitudeAdjust( 100 );
291 controls.move_elevator(-0.05);
294 case 56: // numeric keypad 8
295 if ( current_autopilot->get_AltitudeEnabled() ) {
296 current_autopilot->AltitudeAdjust( -100 );
298 controls.move_elevator(0.05);
301 case 49: // numeric keypad 1
302 controls.move_elevator_trim(-0.001);
304 case 55: // numeric keypad 7
305 controls.move_elevator_trim(0.001);
307 case 52: // numeric keypad 4
308 controls.move_aileron(-0.05);
310 case 54: // numeric keypad 6
311 controls.move_aileron(0.05);
313 case 48: // numeric keypad Ins
314 if ( current_autopilot->get_HeadingEnabled() ) {
315 current_autopilot->HeadingAdjust( -1 );
317 controls.move_rudder(-0.05);
320 case 13: // numeric keypad Enter
321 if ( current_autopilot->get_HeadingEnabled() ) {
322 current_autopilot->HeadingAdjust( 1 );
324 controls.move_rudder(0.05);
327 case 53: // numeric keypad 5
328 controls.set_aileron(0.0);
329 controls.set_elevator(0.0);
330 controls.set_rudder(0.0);
332 case 57: // numeric keypad 9 (Pg Up)
333 if ( current_autopilot->get_AutoThrottleEnabled() ) {
334 current_autopilot->AutoThrottleAdjust( 5 );
336 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
339 case 51: // numeric keypad 3 (Pg Dn)
340 if ( current_autopilot->get_AutoThrottleEnabled() ) {
341 current_autopilot->AutoThrottleAdjust( -5 );
343 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
347 controls.move_flaps(-0.34);
348 FG_LOG( FG_INPUT, FG_INFO,
349 "Set flaps to " << controls.get_flaps() );
352 controls.move_flaps(0.34);
353 FG_LOG( FG_INPUT, FG_INFO,
354 "Set flaps to " << controls.get_flaps() );
357 speed = globals->get_options()->get_speed_up();
359 globals->get_options()->set_speed_up( speed );
364 b_ret = int( controls.get_brake( 0 ) );
365 b_set = double(!b_ret);
366 controls.set_brake( FGControls::ALL_WHEELS, b_set);
369 if (controls.get_brake(0) > 0.0) {
370 controls.set_brake(0, 0.0);
372 controls.set_brake(0, 1.0);
376 if (controls.get_brake(1) > 0.0) {
377 controls.set_brake(1, 0.0);
379 controls.set_brake(1, 1.0);
383 HUD_masterswitch( true );
386 fgHUDInit(¤t_aircraft); // normal HUD
389 globals->inc_warp( 60 );
390 fgUpdateSkyAndLightingParams();
393 globals->set_freeze( ! globals->get_freeze() );
396 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
397 f->get_Latitude() * RAD_TO_DEG );
398 FGPath tile_path( globals->get_options()->get_fg_root() );
399 tile_path.append( "Scenery" );
400 tile_path.append( p.gen_base_path() );
401 tile_path.append( p.gen_index_str() );
403 // printf position and attitude information
404 FG_LOG( FG_INPUT, FG_INFO,
405 "Lon = " << f->get_Longitude() * RAD_TO_DEG
406 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
407 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
409 FG_LOG( FG_INPUT, FG_INFO,
410 "Heading = " << f->get_Psi() * RAD_TO_DEG
411 << " Roll = " << f->get_Phi() * RAD_TO_DEG
412 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
413 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
417 globals->inc_warp_delta( 30 );
418 fgUpdateSkyAndLightingParams();
421 // globals->get_options()->cycle_view_mode();
422 if ( globals->get_options()->get_view_mode() ==
423 FGOptions::FG_VIEW_FOLLOW )
425 globals->get_options()->set_view_mode(FGOptions::FG_VIEW_PILOT);
426 v->set_goal_view_offset( 0.0 );
427 v->set_view_offset( 0.0 );
428 } else if ( globals->get_options()->get_view_mode() ==
429 FGOptions::FG_VIEW_PILOT )
431 globals->get_options()->set_view_mode( FGOptions::FG_VIEW_FOLLOW );
432 v->set_goal_view_offset( FG_PI * 1.75 );
433 v->set_view_offset( FG_PI * 1.75 );
434 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
436 fgReshape( globals->get_options()->get_xsize(),
437 globals->get_options()->get_ysize() );
440 fov = globals->get_options()->get_fov();
442 if ( fov < FG_FOV_MIN ) {
445 globals->get_options()->set_fov(fov);
446 // v->force_update_fov_math();
449 #ifndef FG_OLD_WEATHER
450 tmp = WeatherDatabase->getWeatherVisibility();
452 WeatherDatabase->setWeatherVisibility( tmp );
454 tmp = current_weather.get_visibility(); // in meters
456 current_weather.set_visibility( tmp );
460 // if( fg_DebugOutput ) {
461 // fclose( fg_DebugOutput );
463 FG_LOG( FG_INPUT, FG_ALERT,
464 "Program exit requested." );
472 // Handle "special" keyboard events
473 void GLUTspecialkey(int k, int x, int y) {
474 FGViewerRPH *v = (FGViewerRPH *)globals->get_current_view();
476 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
478 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
482 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
483 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
486 ifstream input("fgfs.sav");
487 if (input.good() && fgLoadFlight(input)) {
489 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
491 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
496 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
497 ofstream output("fgfs.sav");
498 if (output.good() && fgSaveFlight(output)) {
500 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
502 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
508 current_properties.getStringValue("/sim/panel/path",
509 "Panels/Default/default.xml");
510 FGPanel * new_panel = fgReadPanel(panel_path);
511 if (new_panel == 0) {
512 FG_LOG(FG_INPUT, FG_ALERT,
513 "Error reading new panel from " << panel_path);
516 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
517 delete current_panel;
518 current_panel = new_panel;
522 FGPath props_path(globals->get_options()->get_fg_root());
523 props_path.append("preferences.xml");
524 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
525 if (!readPropertyList(props_path.str(), ¤t_properties)) {
526 FG_LOG(FG_INPUT, FG_ALERT,
527 "Failed to reread global preferences from "
528 << props_path.str());
530 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
535 current_panel->setYOffset(current_panel->getYOffset() - 5);
536 fgReshape(globals->get_options()->get_xsize(),
537 globals->get_options()->get_ysize());
541 current_panel->setYOffset(current_panel->getYOffset() + 5);
542 fgReshape(globals->get_options()->get_xsize(),
543 globals->get_options()->get_ysize());
547 current_panel->setXOffset(current_panel->getXOffset() - 5);
551 current_panel->setXOffset(current_panel->getXOffset() + 5);
554 case GLUT_KEY_END: // numeric keypad 1
555 v->set_goal_view_offset( FG_PI * 0.75 );
556 if ( globals->get_options()->get_view_mode() ==
557 FGOptions::FG_VIEW_FOLLOW)
559 globals->get_current_view()->set_pilot_offset(-25.0, 25.0, 1.0);
560 v->set_view_offset( FG_PI * 0.75 );
563 case GLUT_KEY_DOWN: // numeric keypad 2
564 v->set_goal_view_offset( FG_PI );
565 if ( globals->get_options()->get_view_mode() ==
566 FGOptions::FG_VIEW_FOLLOW )
568 globals->get_current_view()->set_pilot_offset(-25.0, 0.0, 1.0);
569 v->set_view_offset( FG_PI );
572 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
573 v->set_goal_view_offset( FG_PI * 1.25 );
574 if ( globals->get_options()->get_view_mode() ==
575 FGOptions::FG_VIEW_FOLLOW)
577 globals->get_current_view()->set_pilot_offset(-25.0, -25.0, 1.0);
578 v->set_view_offset( FG_PI * 1.25 );
581 case GLUT_KEY_LEFT: // numeric keypad 4
582 v->set_goal_view_offset( FG_PI * 0.50 );
583 if ( globals->get_options()->get_view_mode() ==
584 FGOptions::FG_VIEW_FOLLOW )
586 globals->get_current_view()->set_pilot_offset(0.0, 25.0, 1.0);
587 v->set_view_offset( FG_PI * 0.50 );
590 case GLUT_KEY_RIGHT: // numeric keypad 6
591 v->set_goal_view_offset( FG_PI * 1.50 );
592 if ( globals->get_options()->get_view_mode() ==
593 FGOptions::FG_VIEW_FOLLOW )
595 globals->get_current_view()->set_pilot_offset(0.0, -25.0, 1.0);
596 v->set_view_offset( FG_PI * 1.50 );
599 case GLUT_KEY_HOME: // numeric keypad 7
600 v->set_goal_view_offset( FG_PI * 0.25 );
601 if ( globals->get_options()->get_view_mode() ==
602 FGOptions::FG_VIEW_FOLLOW )
604 globals->get_current_view()->set_pilot_offset(25.0, 25.0, 1.0);
605 v->set_view_offset( FG_PI * 0.25 );
608 case GLUT_KEY_UP: // numeric keypad 8
609 v->set_goal_view_offset( 0.00 );
610 if ( globals->get_options()->get_view_mode() ==
611 FGOptions::FG_VIEW_FOLLOW )
613 globals->get_current_view()->set_pilot_offset(25.0, 0.0, 1.0);
614 v->set_view_offset( 0.00 );
617 case GLUT_KEY_PAGE_UP: // numeric keypad 9
618 v->set_goal_view_offset( FG_PI * 1.75 );
619 if ( globals->get_options()->get_view_mode() ==
620 FGOptions::FG_VIEW_FOLLOW )
622 globals->get_current_view()->set_pilot_offset(25.0, -25.0, 1.0);
623 v->set_view_offset( FG_PI * 1.75 );
628 FG_LOG( FG_INPUT, FG_DEBUG, "" );
630 case GLUT_KEY_F2: // F2 Reload Tile Cache...
633 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
635 globals->set_freeze( true );
637 if ( global_tile_mgr.init() ) {
638 // Load the local scenery data
639 global_tile_mgr.update(
640 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
641 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
643 FG_LOG( FG_GENERAL, FG_ALERT,
644 "Error in Tile Manager initialization!" );
649 globals->set_freeze( false );
652 case GLUT_KEY_F3: // F3 Take a screen shot
655 case GLUT_KEY_F6: // F6 toggles Autopilot target location
656 if ( current_autopilot->get_HeadingMode() !=
657 FGAutopilot::FG_HEADING_WAYPOINT ) {
658 current_autopilot->set_HeadingMode(
659 FGAutopilot::FG_HEADING_WAYPOINT );
660 current_autopilot->set_HeadingEnabled( true );
662 current_autopilot->set_HeadingMode(
663 FGAutopilot::FG_HEADING_LOCK );
666 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
667 globals->get_options()->cycle_fog();
669 if ( globals->get_options()->get_fog() ==
670 FGOptions::FG_FOG_DISABLED )
672 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
673 } else if ( globals->get_options()->get_fog() ==
674 FGOptions::FG_FOG_FASTEST )
676 FG_LOG( FG_INPUT, FG_INFO,
677 "Fog enabled, hint set to fastest" );
678 } else if ( globals->get_options()->get_fog() ==
679 FGOptions::FG_FOG_NICEST )
681 FG_LOG( FG_INPUT, FG_INFO,
682 "Fog enabled, hint set to nicest" );
686 case GLUT_KEY_F9: // F9 toggles textures on and off...
687 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
688 if ( globals->get_options()->get_textures() ) {
689 globals->get_options()->set_textures( false );
690 material_lib.set_step( 1 );
692 globals->get_options()->set_textures( true );
693 material_lib.set_step( 0 );
696 case GLUT_KEY_F10: // F10 toggles menu on and off...
697 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
700 case GLUT_KEY_F11: // F11 Altitude Dialog.
701 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
704 case GLUT_KEY_F12: // F12 Heading Dialog...
705 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
709 if ( current_autopilot->get_AltitudeEnabled() ) {
710 current_autopilot->AltitudeAdjust( -100 );
712 controls.move_elevator(0.05);
716 if ( current_autopilot->get_AltitudeEnabled() ) {
717 current_autopilot->AltitudeAdjust( 100 );
719 controls.move_elevator(-0.05);
723 controls.move_aileron(-0.05);
726 controls.move_aileron(0.05);
728 case GLUT_KEY_HOME: // numeric keypad 1
729 controls.move_elevator_trim(0.001);
731 case GLUT_KEY_END: // numeric keypad 7
732 controls.move_elevator_trim(-0.001);
734 case GLUT_KEY_INSERT: // numeric keypad Ins
735 if ( current_autopilot->get_HeadingEnabled() ) {
736 current_autopilot->HeadingAdjust( -1 );
738 controls.move_rudder(-0.05);
741 case 13: // numeric keypad Enter
742 if ( current_autopilot->get_HeadingEnabled() ) {
743 current_autopilot->HeadingAdjust( 1 );
745 controls.move_rudder(0.05);
748 case 53: // numeric keypad 5
749 controls.set_aileron(0.0);
750 controls.set_elevator(0.0);
751 controls.set_rudder(0.0);
753 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
754 if ( current_autopilot->get_AutoThrottleEnabled() ) {
755 current_autopilot->AutoThrottleAdjust( 5 );
757 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
760 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
761 if ( current_autopilot->get_AutoThrottleEnabled() ) {
762 current_autopilot->AutoThrottleAdjust( -5 );
764 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );