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( current_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 (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
161 globals->get_pilot_view()->set_pilot_offset(-25.0, 25.0, 1.0);
162 v->set_view_offset( FG_PI * 0.75 );
165 case 50: // numeric keypad 2
166 v->set_goal_view_offset( FG_PI );
167 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
168 globals->get_pilot_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 (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
175 globals->get_pilot_view()->set_pilot_offset(-25.0, -25.0, 1.0);
176 v->set_view_offset( FG_PI * 1.25 );
179 case 52: // numeric keypad 4
180 v->set_goal_view_offset( FG_PI * 0.50 );
181 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
182 globals->get_pilot_view()->set_pilot_offset(0.0, 25.0, 1.0);
183 v->set_view_offset( FG_PI * 0.50 );
186 case 54: // numeric keypad 6
187 v->set_goal_view_offset( FG_PI * 1.50 );
188 if (current_options.get_view_mode() == 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 * 1.50 );
193 case 55: // numeric keypad 7
194 v->set_goal_view_offset( FG_PI * 0.25 );
195 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
196 globals->get_pilot_view()->set_pilot_offset(25.0, 25.0, 1.0);
197 v->set_view_offset( FG_PI * 0.25 );
200 case 56: // numeric keypad 8
201 v->set_goal_view_offset( 0.00 );
202 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
203 globals->get_pilot_view()->set_pilot_offset(25.0, 0.0, 1.0);
204 v->set_view_offset( 0.00 );
207 case 57: // numeric keypad 9
208 v->set_goal_view_offset( FG_PI * 1.75 );
209 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
210 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
211 v->set_view_offset( FG_PI * 1.75 );
215 speed = current_options.get_speed_up();
220 current_options.set_speed_up( speed );
223 // status = current_options.get_hud_status();
224 // current_options.set_hud_status(!status);
225 HUD_brightkey( true );
229 fgHUDInit2(¤t_aircraft);
232 globals->inc_warp( -60 );
233 fgUpdateSkyAndLightingParams();
236 current_options.toggle_panel();
239 globals->inc_warp_delta( -30 );
240 fgUpdateSkyAndLightingParams();
243 #if defined(FX) && !defined(WIN32)
244 global_fullscreen = ( !global_fullscreen );
245 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
246 XMesaSetFXmode( global_fullscreen ?
247 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
252 fov = current_options.get_fov();
254 if ( fov > FG_FOV_MAX ) {
257 current_options.set_fov(fov);
258 v->force_update_fov_math();
261 #ifndef FG_OLD_WEATHER
262 tmp = WeatherDatabase->getWeatherVisibility();
264 WeatherDatabase->setWeatherVisibility( tmp );
266 tmp = current_weather.get_visibility(); // in meters
268 current_weather.set_visibility( tmp );
273 FG_LOG( FG_INPUT, FG_DEBUG, "" );
275 case 50: // numeric keypad 2
276 if ( current_autopilot->get_AltitudeEnabled() ) {
277 current_autopilot->AltitudeAdjust( 100 );
279 controls.move_elevator(-0.05);
282 case 56: // numeric keypad 8
283 if ( current_autopilot->get_AltitudeEnabled() ) {
284 current_autopilot->AltitudeAdjust( -100 );
286 controls.move_elevator(0.05);
289 case 49: // numeric keypad 1
290 controls.move_elevator_trim(-0.001);
292 case 55: // numeric keypad 7
293 controls.move_elevator_trim(0.001);
295 case 52: // numeric keypad 4
296 controls.move_aileron(-0.05);
298 case 54: // numeric keypad 6
299 controls.move_aileron(0.05);
301 case 48: // numeric keypad Ins
302 if ( current_autopilot->get_HeadingEnabled() ) {
303 current_autopilot->HeadingAdjust( -1 );
305 controls.move_rudder(-0.05);
308 case 13: // numeric keypad Enter
309 if ( current_autopilot->get_HeadingEnabled() ) {
310 current_autopilot->HeadingAdjust( 1 );
312 controls.move_rudder(0.05);
315 case 53: // numeric keypad 5
316 controls.set_aileron(0.0);
317 controls.set_elevator(0.0);
318 controls.set_rudder(0.0);
320 case 57: // numeric keypad 9 (Pg Up)
321 if ( current_autopilot->get_AutoThrottleEnabled() ) {
322 current_autopilot->AutoThrottleAdjust( 5 );
324 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
327 case 51: // numeric keypad 3 (Pg Dn)
328 if ( current_autopilot->get_AutoThrottleEnabled() ) {
329 current_autopilot->AutoThrottleAdjust( -5 );
331 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
335 controls.move_flaps(-0.34);
336 FG_LOG( FG_INPUT, FG_INFO,
337 "Set flaps to " << controls.get_flaps() );
340 controls.move_flaps(0.34);
341 FG_LOG( FG_INPUT, FG_INFO,
342 "Set flaps to " << controls.get_flaps() );
345 speed = current_options.get_speed_up();
347 current_options.set_speed_up( speed );
352 b_ret = int( controls.get_brake( 0 ) );
353 b_set = double(!b_ret);
354 controls.set_brake( FGControls::ALL_WHEELS, b_set);
357 if (controls.get_brake(0) > 0.0) {
358 controls.set_brake(0, 0.0);
360 controls.set_brake(0, 1.0);
364 if (controls.get_brake(1) > 0.0) {
365 controls.set_brake(1, 0.0);
367 controls.set_brake(1, 1.0);
371 HUD_masterswitch( true );
374 fgHUDInit(¤t_aircraft); // normal HUD
377 globals->inc_warp( 60 );
378 fgUpdateSkyAndLightingParams();
381 globals->set_freeze( ! globals->get_freeze() );
384 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
385 f->get_Latitude() * RAD_TO_DEG );
386 FGPath tile_path( current_options.get_fg_root() );
387 tile_path.append( "Scenery" );
388 tile_path.append( p.gen_base_path() );
389 tile_path.append( p.gen_index_str() );
391 // printf position and attitude information
392 FG_LOG( FG_INPUT, FG_INFO,
393 "Lon = " << f->get_Longitude() * RAD_TO_DEG
394 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
395 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
397 FG_LOG( FG_INPUT, FG_INFO,
398 "Heading = " << f->get_Psi() * RAD_TO_DEG
399 << " Roll = " << f->get_Phi() * RAD_TO_DEG
400 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
401 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
405 globals->inc_warp_delta( 30 );
406 fgUpdateSkyAndLightingParams();
409 // current_options.cycle_view_mode();
410 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
411 current_options.set_view_mode(fgOPTIONS::FG_VIEW_PILOT);
412 v->set_goal_view_offset( 0.0 );
413 v->set_view_offset( 0.0 );
414 } else if (current_options.get_view_mode() ==
415 fgOPTIONS::FG_VIEW_PILOT) {
416 current_options.set_view_mode(fgOPTIONS::FG_VIEW_FOLLOW);
417 v->set_goal_view_offset( FG_PI * 1.75 );
418 v->set_view_offset( FG_PI * 1.75 );
419 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
421 fgReshape( globals->get_current_view()->get_winWidth(),
422 globals->get_current_view()->get_winHeight() );
425 fov = current_options.get_fov();
427 if ( fov < FG_FOV_MIN ) {
430 current_options.set_fov(fov);
431 v->force_update_fov_math();
434 #ifndef FG_OLD_WEATHER
435 tmp = WeatherDatabase->getWeatherVisibility();
437 WeatherDatabase->setWeatherVisibility( tmp );
439 tmp = current_weather.get_visibility(); // in meters
441 current_weather.set_visibility( tmp );
445 // if( fg_DebugOutput ) {
446 // fclose( fg_DebugOutput );
448 FG_LOG( FG_INPUT, FG_ALERT,
449 "Program exit requested." );
457 // Handle "special" keyboard events
458 void GLUTspecialkey(int k, int x, int y) {
461 v = globals->get_current_view();
463 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
465 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
469 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
470 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
473 ifstream input("fgfs.sav");
474 if (input.good() && fgLoadFlight(input)) {
476 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
478 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
483 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
484 ofstream output("fgfs.sav");
485 if (output.good() && fgSaveFlight(output)) {
487 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
489 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
495 current_properties.getStringValue("/sim/panel/path",
496 "Panels/Default/default.xml");
497 FGPanel * new_panel = fgReadPanel(panel_path);
498 if (new_panel == 0) {
499 FG_LOG(FG_INPUT, FG_ALERT,
500 "Error reading new panel from " << panel_path);
503 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
504 delete current_panel;
505 current_panel = new_panel;
509 FGPath props_path(current_options.get_fg_root());
510 props_path.append("preferences.xml");
511 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
512 if (!readPropertyList(props_path.str(), ¤t_properties)) {
513 FG_LOG(FG_INPUT, FG_ALERT,
514 "Failed to reread global preferences from "
515 << props_path.str());
517 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
522 current_panel->setYOffset(current_panel->getYOffset() - 5);
523 fgReshape(globals->get_current_view()->get_winWidth(),
524 globals->get_current_view()->get_winHeight());
528 current_panel->setYOffset(current_panel->getYOffset() + 5);
529 fgReshape(globals->get_current_view()->get_winWidth(),
530 globals->get_current_view()->get_winHeight());
534 current_panel->setXOffset(current_panel->getXOffset() - 5);
538 current_panel->setXOffset(current_panel->getXOffset() + 5);
541 case GLUT_KEY_END: // numeric keypad 1
542 v->set_goal_view_offset( FG_PI * 0.75 );
543 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
544 globals->get_pilot_view()->set_pilot_offset(-25.0, 25.0, 1.0);
545 v->set_view_offset( FG_PI * 0.75 );
548 case GLUT_KEY_DOWN: // numeric keypad 2
549 v->set_goal_view_offset( FG_PI );
550 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
551 globals->get_pilot_view()->set_pilot_offset(-25.0, 0.0, 1.0);
552 v->set_view_offset( FG_PI );
555 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
556 v->set_goal_view_offset( FG_PI * 1.25 );
557 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
558 globals->get_pilot_view()->set_pilot_offset(-25.0, -25.0, 1.0);
559 v->set_view_offset( FG_PI * 1.25 );
562 case GLUT_KEY_LEFT: // numeric keypad 4
563 v->set_goal_view_offset( FG_PI * 0.50 );
564 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
565 globals->get_pilot_view()->set_pilot_offset(0.0, 25.0, 1.0);
566 v->set_view_offset( FG_PI * 0.50 );
569 case GLUT_KEY_RIGHT: // numeric keypad 6
570 v->set_goal_view_offset( FG_PI * 1.50 );
571 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
572 globals->get_pilot_view()->set_pilot_offset(0.0, -25.0, 1.0);
573 v->set_view_offset( FG_PI * 1.50 );
576 case GLUT_KEY_HOME: // numeric keypad 7
577 v->set_goal_view_offset( FG_PI * 0.25 );
578 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
579 globals->get_pilot_view()->set_pilot_offset(25.0, 25.0, 1.0);
580 v->set_view_offset( FG_PI * 0.25 );
583 case GLUT_KEY_UP: // numeric keypad 8
584 v->set_goal_view_offset( 0.00 );
585 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
586 globals->get_pilot_view()->set_pilot_offset(25.0, 0.0, 1.0);
587 v->set_view_offset( 0.00 );
590 case GLUT_KEY_PAGE_UP: // numeric keypad 9
591 v->set_goal_view_offset( FG_PI * 1.75 );
592 if (current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW) {
593 globals->get_pilot_view()->set_pilot_offset(25.0, -25.0, 1.0);
594 v->set_view_offset( FG_PI * 1.75 );
599 FG_LOG( FG_INPUT, FG_DEBUG, "" );
601 case GLUT_KEY_F2: // F2 Reload Tile Cache...
604 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
606 globals->set_freeze( true );
608 if ( global_tile_mgr.init() ) {
609 // Load the local scenery data
610 global_tile_mgr.update(
611 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
612 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
614 FG_LOG( FG_GENERAL, FG_ALERT,
615 "Error in Tile Manager initialization!" );
620 globals->set_freeze( false );
623 case GLUT_KEY_F3: // F3 Take a screen shot
626 case GLUT_KEY_F6: // F6 toggles Autopilot target location
627 if ( current_autopilot->get_HeadingMode() !=
628 FGAutopilot::FG_HEADING_WAYPOINT ) {
629 current_autopilot->set_HeadingMode(
630 FGAutopilot::FG_HEADING_WAYPOINT );
631 current_autopilot->set_HeadingEnabled( true );
633 current_autopilot->set_HeadingMode(
634 FGAutopilot::FG_HEADING_LOCK );
637 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
638 current_options.cycle_fog();
640 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
641 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
642 } else if ( current_options.get_fog() ==
643 fgOPTIONS::FG_FOG_FASTEST )
645 FG_LOG( FG_INPUT, FG_INFO,
646 "Fog enabled, hint set to fastest" );
647 } else if ( current_options.get_fog() ==
648 fgOPTIONS::FG_FOG_NICEST )
650 FG_LOG( FG_INPUT, FG_INFO,
651 "Fog enabled, hint set to nicest" );
655 case GLUT_KEY_F9: // F9 toggles textures on and off...
656 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
657 if ( current_options.get_textures() ) {
658 current_options.set_textures( false );
659 material_lib.set_step( 1 );
661 current_options.set_textures( true );
662 material_lib.set_step( 0 );
665 case GLUT_KEY_F10: // F10 toggles menu on and off...
666 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
669 case GLUT_KEY_F11: // F11 Altitude Dialog.
670 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
673 case GLUT_KEY_F12: // F12 Heading Dialog...
674 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
678 if ( current_autopilot->get_AltitudeEnabled() ) {
679 current_autopilot->AltitudeAdjust( -100 );
681 controls.move_elevator(0.05);
685 if ( current_autopilot->get_AltitudeEnabled() ) {
686 current_autopilot->AltitudeAdjust( 100 );
688 controls.move_elevator(-0.05);
692 controls.move_aileron(-0.05);
695 controls.move_aileron(0.05);
697 case GLUT_KEY_HOME: // numeric keypad 1
698 controls.move_elevator_trim(0.001);
700 case GLUT_KEY_END: // numeric keypad 7
701 controls.move_elevator_trim(-0.001);
703 case GLUT_KEY_INSERT: // numeric keypad Ins
704 if ( current_autopilot->get_HeadingEnabled() ) {
705 current_autopilot->HeadingAdjust( -1 );
707 controls.move_rudder(-0.05);
710 case 13: // numeric keypad Enter
711 if ( current_autopilot->get_HeadingEnabled() ) {
712 current_autopilot->HeadingAdjust( 1 );
714 controls.move_rudder(0.05);
717 case 53: // numeric keypad 5
718 controls.set_aileron(0.0);
719 controls.set_elevator(0.0);
720 controls.set_rudder(0.0);
722 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
723 if ( current_autopilot->get_AutoThrottleEnabled() ) {
724 current_autopilot->AutoThrottleAdjust( 5 );
726 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
729 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
730 if ( current_autopilot->get_AutoThrottleEnabled() ) {
731 current_autopilot->AutoThrottleAdjust( -5 );
733 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );