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) {
85 static bool winding_ccw = true;
88 f = current_aircraft.fdm_state;
89 v = globals->get_current_view();
91 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
92 if ( puKeyboard(k, PU_DOWN) ) {
96 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
97 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
100 current_autopilot->set_AltitudeMode(
101 FGAutopilot::FG_ALTITUDE_LOCK );
102 current_autopilot->set_AltitudeEnabled(
103 ! current_autopilot->get_AltitudeEnabled()
106 case 7: // Ctrl-G key
107 current_autopilot->set_AltitudeMode(
108 FGAutopilot::FG_ALTITUDE_GS1 );
109 current_autopilot->set_AltitudeEnabled(
110 ! current_autopilot->get_AltitudeEnabled()
113 case 8: // Ctrl-H key
114 current_autopilot->set_HeadingMode(
115 FGAutopilot::FG_HEADING_LOCK );
116 current_autopilot->set_HeadingEnabled(
117 ! current_autopilot->get_HeadingEnabled()
120 case 14: // Ctrl-N key
121 current_autopilot->set_HeadingMode(
122 FGAutopilot::FG_HEADING_NAV1 );
123 current_autopilot->set_HeadingEnabled(
124 ! current_autopilot->get_HeadingEnabled()
127 case 18: // Ctrl-R key
129 winding_ccw = !winding_ccw;
131 glFrontFace ( GL_CCW );
133 glFrontFace ( GL_CW );
136 case 19: // Ctrl-S key
137 current_autopilot->set_AutoThrottleEnabled(
138 ! current_autopilot->get_AutoThrottleEnabled()
141 case 20: // Ctrl-T key
142 current_autopilot->set_AltitudeMode(
143 FGAutopilot::FG_ALTITUDE_TERRAIN );
144 current_autopilot->set_AltitudeEnabled(
145 ! current_autopilot->get_AltitudeEnabled()
148 case 21: // Ctrl-U key
149 // add 1000' of emergency altitude. Possibly good for
150 // unflipping yourself :-)
152 double alt = cur_fdm_state->get_Altitude() + 1000;
153 fgFDMForceAltitude( globals->get_options()->get_flight_model(),
154 alt * FEET_TO_METER );
157 case 49: // numeric keypad 1
158 v->set_goal_view_offset( FG_PI * 0.75 );
159 if ( globals->get_options()->get_view_mode() ==
160 FGOptions::FG_VIEW_FOLLOW )
162 globals->get_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 ( globals->get_options()->get_view_mode() ==
169 FGOptions::FG_VIEW_FOLLOW )
171 globals->get_pilot_view()->set_pilot_offset(-25.0, 0.0, 1.0);
172 v->set_view_offset( FG_PI );
175 case 51: // numeric keypad 3
176 v->set_goal_view_offset( FG_PI * 1.25 );
177 if ( globals->get_options()->get_view_mode() ==
178 FGOptions::FG_VIEW_FOLLOW )
180 globals->get_pilot_view()->set_pilot_offset(-25.0, -25.0, 1.0);
181 v->set_view_offset( FG_PI * 1.25 );
184 case 52: // numeric keypad 4
185 v->set_goal_view_offset( FG_PI * 0.50 );
186 if ( globals->get_options()->get_view_mode() ==
187 FGOptions::FG_VIEW_FOLLOW )
189 globals->get_pilot_view()->set_pilot_offset(0.0, 25.0, 1.0);
190 v->set_view_offset( FG_PI * 0.50 );
193 case 54: // numeric keypad 6
194 v->set_goal_view_offset( FG_PI * 1.50 );
195 if ( globals->get_options()->get_view_mode() ==
196 FGOptions::FG_VIEW_FOLLOW )
198 globals->get_pilot_view()->set_pilot_offset(0.0, -25.0, 1.0);
199 v->set_view_offset( FG_PI * 1.50 );
202 case 55: // numeric keypad 7
203 v->set_goal_view_offset( FG_PI * 0.25 );
204 if ( globals->get_options()->get_view_mode() ==
205 FGOptions::FG_VIEW_FOLLOW )
207 globals->get_pilot_view()->set_pilot_offset(25.0, 25.0, 1.0);
208 v->set_view_offset( FG_PI * 0.25 );
211 case 56: // numeric keypad 8
212 v->set_goal_view_offset( 0.00 );
213 if ( globals->get_options()->get_view_mode() ==
214 FGOptions::FG_VIEW_FOLLOW )
216 globals->get_pilot_view()->set_pilot_offset(25.0, 0.0, 1.0);
217 v->set_view_offset( 0.00 );
220 case 57: // numeric keypad 9
221 v->set_goal_view_offset( FG_PI * 1.75 );
222 if ( globals->get_options()->get_view_mode() ==
223 FGOptions::FG_VIEW_FOLLOW )
225 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
226 v->set_view_offset( FG_PI * 1.75 );
230 speed = globals->get_options()->get_speed_up();
235 globals->get_options()->set_speed_up( speed );
238 // status = globals->get_options()->get_hud_status();
239 // globals->get_options()->set_hud_status(!status);
240 HUD_brightkey( true );
244 fgHUDInit2(¤t_aircraft);
247 globals->inc_warp( -60 );
248 fgUpdateSkyAndLightingParams();
251 globals->get_options()->toggle_panel();
254 globals->inc_warp_delta( -30 );
255 fgUpdateSkyAndLightingParams();
258 #if defined(FX) && !defined(WIN32)
259 global_fullscreen = ( !global_fullscreen );
260 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
261 XMesaSetFXmode( global_fullscreen ?
262 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
267 fov = globals->get_options()->get_fov();
269 if ( fov > FG_FOV_MAX ) {
272 globals->get_options()->set_fov(fov);
273 v->force_update_fov_math();
276 #ifndef FG_OLD_WEATHER
277 tmp = WeatherDatabase->getWeatherVisibility();
279 WeatherDatabase->setWeatherVisibility( tmp );
281 tmp = current_weather.get_visibility(); // in meters
283 current_weather.set_visibility( tmp );
288 FG_LOG( FG_INPUT, FG_DEBUG, "" );
290 case 50: // numeric keypad 2
291 if ( current_autopilot->get_AltitudeEnabled() ) {
292 current_autopilot->AltitudeAdjust( 100 );
294 controls.move_elevator(-0.05);
297 case 56: // numeric keypad 8
298 if ( current_autopilot->get_AltitudeEnabled() ) {
299 current_autopilot->AltitudeAdjust( -100 );
301 controls.move_elevator(0.05);
304 case 49: // numeric keypad 1
305 controls.move_elevator_trim(-0.001);
307 case 55: // numeric keypad 7
308 controls.move_elevator_trim(0.001);
310 case 52: // numeric keypad 4
311 controls.move_aileron(-0.05);
313 case 54: // numeric keypad 6
314 controls.move_aileron(0.05);
316 case 48: // numeric keypad Ins
317 if ( current_autopilot->get_HeadingEnabled() ) {
318 current_autopilot->HeadingAdjust( -1 );
320 controls.move_rudder(-0.05);
323 case 13: // numeric keypad Enter
324 if ( current_autopilot->get_HeadingEnabled() ) {
325 current_autopilot->HeadingAdjust( 1 );
327 controls.move_rudder(0.05);
330 case 53: // numeric keypad 5
331 controls.set_aileron(0.0);
332 controls.set_elevator(0.0);
333 controls.set_rudder(0.0);
335 case 57: // numeric keypad 9 (Pg Up)
336 if ( current_autopilot->get_AutoThrottleEnabled() ) {
337 current_autopilot->AutoThrottleAdjust( 5 );
339 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
342 case 51: // numeric keypad 3 (Pg Dn)
343 if ( current_autopilot->get_AutoThrottleEnabled() ) {
344 current_autopilot->AutoThrottleAdjust( -5 );
346 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
350 controls.move_flaps(-0.34);
351 FG_LOG( FG_INPUT, FG_INFO,
352 "Set flaps to " << controls.get_flaps() );
355 controls.move_flaps(0.34);
356 FG_LOG( FG_INPUT, FG_INFO,
357 "Set flaps to " << controls.get_flaps() );
360 speed = globals->get_options()->get_speed_up();
362 globals->get_options()->set_speed_up( speed );
367 b_ret = int( controls.get_brake( 0 ) );
368 b_set = double(!b_ret);
369 controls.set_brake( FGControls::ALL_WHEELS, b_set);
372 if (controls.get_brake(0) > 0.0) {
373 controls.set_brake(0, 0.0);
375 controls.set_brake(0, 1.0);
379 if (controls.get_brake(1) > 0.0) {
380 controls.set_brake(1, 0.0);
382 controls.set_brake(1, 1.0);
386 HUD_masterswitch( true );
389 fgHUDInit(¤t_aircraft); // normal HUD
392 globals->inc_warp( 60 );
393 fgUpdateSkyAndLightingParams();
396 globals->set_freeze( ! globals->get_freeze() );
399 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
400 f->get_Latitude() * RAD_TO_DEG );
401 FGPath tile_path( globals->get_options()->get_fg_root() );
402 tile_path.append( "Scenery" );
403 tile_path.append( p.gen_base_path() );
404 tile_path.append( p.gen_index_str() );
406 // printf position and attitude information
407 FG_LOG( FG_INPUT, FG_INFO,
408 "Lon = " << f->get_Longitude() * RAD_TO_DEG
409 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
410 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
412 FG_LOG( FG_INPUT, FG_INFO,
413 "Heading = " << f->get_Psi() * RAD_TO_DEG
414 << " Roll = " << f->get_Phi() * RAD_TO_DEG
415 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
416 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
420 globals->inc_warp_delta( 30 );
421 fgUpdateSkyAndLightingParams();
424 // globals->get_options()->cycle_view_mode();
425 if ( globals->get_options()->get_view_mode() ==
426 FGOptions::FG_VIEW_FOLLOW )
428 globals->get_options()->set_view_mode(FGOptions::FG_VIEW_PILOT);
429 v->set_goal_view_offset( 0.0 );
430 v->set_view_offset( 0.0 );
431 } else if ( globals->get_options()->get_view_mode() ==
432 FGOptions::FG_VIEW_PILOT )
434 globals->get_options()->set_view_mode( FGOptions::FG_VIEW_FOLLOW );
435 v->set_goal_view_offset( FG_PI * 1.75 );
436 v->set_view_offset( FG_PI * 1.75 );
437 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
439 fgReshape( globals->get_options()->get_xsize(),
440 globals->get_options()->get_ysize() );
443 fov = globals->get_options()->get_fov();
445 if ( fov < FG_FOV_MIN ) {
448 globals->get_options()->set_fov(fov);
449 v->force_update_fov_math();
452 #ifndef FG_OLD_WEATHER
453 tmp = WeatherDatabase->getWeatherVisibility();
455 WeatherDatabase->setWeatherVisibility( tmp );
457 tmp = current_weather.get_visibility(); // in meters
459 current_weather.set_visibility( tmp );
463 // if( fg_DebugOutput ) {
464 // fclose( fg_DebugOutput );
466 FG_LOG( FG_INPUT, FG_ALERT,
467 "Program exit requested." );
475 // Handle "special" keyboard events
476 void GLUTspecialkey(int k, int x, int y) {
479 v = globals->get_current_view();
481 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
483 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
487 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
488 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
491 ifstream input("fgfs.sav");
492 if (input.good() && fgLoadFlight(input)) {
494 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
496 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
501 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
502 ofstream output("fgfs.sav");
503 if (output.good() && fgSaveFlight(output)) {
505 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
507 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
513 current_properties.getStringValue("/sim/panel/path",
514 "Panels/Default/default.xml");
515 FGPanel * new_panel = fgReadPanel(panel_path);
516 if (new_panel == 0) {
517 FG_LOG(FG_INPUT, FG_ALERT,
518 "Error reading new panel from " << panel_path);
521 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
522 delete current_panel;
523 current_panel = new_panel;
527 FGPath props_path(globals->get_options()->get_fg_root());
528 props_path.append("preferences.xml");
529 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
530 if (!readPropertyList(props_path.str(), ¤t_properties)) {
531 FG_LOG(FG_INPUT, FG_ALERT,
532 "Failed to reread global preferences from "
533 << props_path.str());
535 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
540 current_panel->setYOffset(current_panel->getYOffset() - 5);
541 fgReshape(globals->get_options()->get_xsize(),
542 globals->get_options()->get_ysize());
546 current_panel->setYOffset(current_panel->getYOffset() + 5);
547 fgReshape(globals->get_options()->get_xsize(),
548 globals->get_options()->get_ysize());
552 current_panel->setXOffset(current_panel->getXOffset() - 5);
556 current_panel->setXOffset(current_panel->getXOffset() + 5);
559 case GLUT_KEY_END: // numeric keypad 1
560 v->set_goal_view_offset( FG_PI * 0.75 );
561 if ( globals->get_options()->get_view_mode() ==
562 FGOptions::FG_VIEW_FOLLOW)
564 globals->get_pilot_view()->set_pilot_offset(-25.0, 25.0, 1.0);
565 v->set_view_offset( FG_PI * 0.75 );
568 case GLUT_KEY_DOWN: // numeric keypad 2
569 v->set_goal_view_offset( FG_PI );
570 if ( globals->get_options()->get_view_mode() ==
571 FGOptions::FG_VIEW_FOLLOW )
573 globals->get_pilot_view()->set_pilot_offset(-25.0, 0.0, 1.0);
574 v->set_view_offset( FG_PI );
577 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
578 v->set_goal_view_offset( FG_PI * 1.25 );
579 if ( globals->get_options()->get_view_mode() ==
580 FGOptions::FG_VIEW_FOLLOW)
582 globals->get_pilot_view()->set_pilot_offset(-25.0, -25.0, 1.0);
583 v->set_view_offset( FG_PI * 1.25 );
586 case GLUT_KEY_LEFT: // numeric keypad 4
587 v->set_goal_view_offset( FG_PI * 0.50 );
588 if ( globals->get_options()->get_view_mode() ==
589 FGOptions::FG_VIEW_FOLLOW )
591 globals->get_pilot_view()->set_pilot_offset(0.0, 25.0, 1.0);
592 v->set_view_offset( FG_PI * 0.50 );
595 case GLUT_KEY_RIGHT: // numeric keypad 6
596 v->set_goal_view_offset( FG_PI * 1.50 );
597 if ( globals->get_options()->get_view_mode() ==
598 FGOptions::FG_VIEW_FOLLOW )
600 globals->get_pilot_view()->set_pilot_offset(0.0, -25.0, 1.0);
601 v->set_view_offset( FG_PI * 1.50 );
604 case GLUT_KEY_HOME: // numeric keypad 7
605 v->set_goal_view_offset( FG_PI * 0.25 );
606 if ( globals->get_options()->get_view_mode() ==
607 FGOptions::FG_VIEW_FOLLOW )
609 globals->get_pilot_view()->set_pilot_offset(25.0, 25.0, 1.0);
610 v->set_view_offset( FG_PI * 0.25 );
613 case GLUT_KEY_UP: // numeric keypad 8
614 v->set_goal_view_offset( 0.00 );
615 if ( globals->get_options()->get_view_mode() ==
616 FGOptions::FG_VIEW_FOLLOW )
618 globals->get_pilot_view()->set_pilot_offset(25.0, 0.0, 1.0);
619 v->set_view_offset( 0.00 );
622 case GLUT_KEY_PAGE_UP: // numeric keypad 9
623 v->set_goal_view_offset( FG_PI * 1.75 );
624 if ( globals->get_options()->get_view_mode() ==
625 FGOptions::FG_VIEW_FOLLOW )
627 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
628 v->set_view_offset( FG_PI * 1.75 );
633 FG_LOG( FG_INPUT, FG_DEBUG, "" );
635 case GLUT_KEY_F2: // F2 Reload Tile Cache...
638 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
640 globals->set_freeze( true );
642 if ( global_tile_mgr.init() ) {
643 // Load the local scenery data
644 global_tile_mgr.update(
645 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
646 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
648 FG_LOG( FG_GENERAL, FG_ALERT,
649 "Error in Tile Manager initialization!" );
654 globals->set_freeze( false );
657 case GLUT_KEY_F3: // F3 Take a screen shot
660 case GLUT_KEY_F6: // F6 toggles Autopilot target location
661 if ( current_autopilot->get_HeadingMode() !=
662 FGAutopilot::FG_HEADING_WAYPOINT ) {
663 current_autopilot->set_HeadingMode(
664 FGAutopilot::FG_HEADING_WAYPOINT );
665 current_autopilot->set_HeadingEnabled( true );
667 current_autopilot->set_HeadingMode(
668 FGAutopilot::FG_HEADING_LOCK );
671 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
672 globals->get_options()->cycle_fog();
674 if ( globals->get_options()->get_fog() ==
675 FGOptions::FG_FOG_DISABLED )
677 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
678 } else if ( globals->get_options()->get_fog() ==
679 FGOptions::FG_FOG_FASTEST )
681 FG_LOG( FG_INPUT, FG_INFO,
682 "Fog enabled, hint set to fastest" );
683 } else if ( globals->get_options()->get_fog() ==
684 FGOptions::FG_FOG_NICEST )
686 FG_LOG( FG_INPUT, FG_INFO,
687 "Fog enabled, hint set to nicest" );
691 case GLUT_KEY_F9: // F9 toggles textures on and off...
692 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
693 if ( globals->get_options()->get_textures() ) {
694 globals->get_options()->set_textures( false );
695 material_lib.set_step( 1 );
697 globals->get_options()->set_textures( true );
698 material_lib.set_step( 0 );
701 case GLUT_KEY_F10: // F10 toggles menu on and off...
702 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
705 case GLUT_KEY_F11: // F11 Altitude Dialog.
706 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
709 case GLUT_KEY_F12: // F12 Heading Dialog...
710 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
714 if ( current_autopilot->get_AltitudeEnabled() ) {
715 current_autopilot->AltitudeAdjust( -100 );
717 controls.move_elevator(0.05);
721 if ( current_autopilot->get_AltitudeEnabled() ) {
722 current_autopilot->AltitudeAdjust( 100 );
724 controls.move_elevator(-0.05);
728 controls.move_aileron(-0.05);
731 controls.move_aileron(0.05);
733 case GLUT_KEY_HOME: // numeric keypad 1
734 controls.move_elevator_trim(0.001);
736 case GLUT_KEY_END: // numeric keypad 7
737 controls.move_elevator_trim(-0.001);
739 case GLUT_KEY_INSERT: // numeric keypad Ins
740 if ( current_autopilot->get_HeadingEnabled() ) {
741 current_autopilot->HeadingAdjust( -1 );
743 controls.move_rudder(-0.05);
746 case 13: // numeric keypad Enter
747 if ( current_autopilot->get_HeadingEnabled() ) {
748 current_autopilot->HeadingAdjust( 1 );
750 controls.move_rudder(0.05);
753 case 53: // numeric keypad 5
754 controls.set_aileron(0.0);
755 controls.set_elevator(0.0);
756 controls.set_rudder(0.0);
758 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
759 if ( current_autopilot->get_AutoThrottleEnabled() ) {
760 current_autopilot->AutoThrottleAdjust( 5 );
762 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
765 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
766 if ( current_autopilot->get_AutoThrottleEnabled() ) {
767 current_autopilot->AutoThrottleAdjust( -5 );
769 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );