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 );
162 case 50: // numeric keypad 2
163 v->set_goal_view_offset( FG_PI );
165 case 51: // numeric keypad 3
166 v->set_goal_view_offset( FG_PI * 1.25 );
168 case 52: // numeric keypad 4
169 v->set_goal_view_offset( FG_PI * 0.50 );
171 case 54: // numeric keypad 6
172 v->set_goal_view_offset( FG_PI * 1.50 );
174 case 55: // numeric keypad 7
175 v->set_goal_view_offset( FG_PI * 0.25 );
177 case 56: // numeric keypad 8
178 v->set_goal_view_offset( 0.00 );
180 case 57: // numeric keypad 9
181 v->set_goal_view_offset( FG_PI * 1.75 );
184 speed = current_options.get_speed_up();
189 current_options.set_speed_up( speed );
192 // status = current_options.get_hud_status();
193 // current_options.set_hud_status(!status);
194 HUD_brightkey( true );
198 fgHUDInit2(¤t_aircraft);
201 globals->inc_warp( -60 );
202 fgUpdateSkyAndLightingParams();
205 current_options.toggle_panel();
208 globals->inc_warp_delta( -30 );
209 fgUpdateSkyAndLightingParams();
212 #if defined(FX) && !defined(WIN32)
213 global_fullscreen = ( !global_fullscreen );
214 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
215 XMesaSetFXmode( global_fullscreen ?
216 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
221 fov = current_options.get_fov();
223 if ( fov > FG_FOV_MAX ) {
226 current_options.set_fov(fov);
227 v->force_update_fov_math();
230 #ifndef FG_OLD_WEATHER
231 tmp = WeatherDatabase->getWeatherVisibility();
233 WeatherDatabase->setWeatherVisibility( tmp );
235 tmp = current_weather.get_visibility(); // in meters
237 current_weather.set_visibility( tmp );
242 FG_LOG( FG_INPUT, FG_DEBUG, "" );
244 case 50: // numeric keypad 2
245 if ( current_autopilot->get_AltitudeEnabled() ) {
246 current_autopilot->AltitudeAdjust( 100 );
248 controls.move_elevator(-0.05);
251 case 56: // numeric keypad 8
252 if ( current_autopilot->get_AltitudeEnabled() ) {
253 current_autopilot->AltitudeAdjust( -100 );
255 controls.move_elevator(0.05);
258 case 49: // numeric keypad 1
259 controls.move_elevator_trim(-0.001);
261 case 55: // numeric keypad 7
262 controls.move_elevator_trim(0.001);
264 case 52: // numeric keypad 4
265 controls.move_aileron(-0.05);
267 case 54: // numeric keypad 6
268 controls.move_aileron(0.05);
270 case 48: // numeric keypad Ins
271 if ( current_autopilot->get_HeadingEnabled() ) {
272 current_autopilot->HeadingAdjust( -1 );
274 controls.move_rudder(-0.05);
277 case 13: // numeric keypad Enter
278 if ( current_autopilot->get_HeadingEnabled() ) {
279 current_autopilot->HeadingAdjust( 1 );
281 controls.move_rudder(0.05);
284 case 53: // numeric keypad 5
285 controls.set_aileron(0.0);
286 controls.set_elevator(0.0);
287 controls.set_rudder(0.0);
289 case 57: // numeric keypad 9 (Pg Up)
290 if ( current_autopilot->get_AutoThrottleEnabled() ) {
291 current_autopilot->AutoThrottleAdjust( 5 );
293 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
296 case 51: // numeric keypad 3 (Pg Dn)
297 if ( current_autopilot->get_AutoThrottleEnabled() ) {
298 current_autopilot->AutoThrottleAdjust( -5 );
300 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
304 controls.move_flaps(-0.34);
305 FG_LOG( FG_INPUT, FG_INFO,
306 "Set flaps to " << controls.get_flaps() );
309 controls.move_flaps(0.34);
310 FG_LOG( FG_INPUT, FG_INFO,
311 "Set flaps to " << controls.get_flaps() );
314 speed = current_options.get_speed_up();
316 current_options.set_speed_up( speed );
321 b_ret = int( controls.get_brake( 0 ) );
322 b_set = double(!b_ret);
323 controls.set_brake( FGControls::ALL_WHEELS, b_set);
326 if (controls.get_brake(0) > 0.0) {
327 controls.set_brake(0, 0.0);
329 controls.set_brake(0, 1.0);
333 if (controls.get_brake(1) > 0.0) {
334 controls.set_brake(1, 0.0);
336 controls.set_brake(1, 1.0);
340 HUD_masterswitch( true );
343 fgHUDInit(¤t_aircraft); // normal HUD
346 globals->inc_warp( 60 );
347 fgUpdateSkyAndLightingParams();
350 globals->set_freeze( ! globals->get_freeze() );
353 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
354 f->get_Latitude() * RAD_TO_DEG );
355 FGPath tile_path( current_options.get_fg_root() );
356 tile_path.append( "Scenery" );
357 tile_path.append( p.gen_base_path() );
358 tile_path.append( p.gen_index_str() );
360 // printf position and attitude information
361 FG_LOG( FG_INPUT, FG_INFO,
362 "Lon = " << f->get_Longitude() * RAD_TO_DEG
363 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
364 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
366 FG_LOG( FG_INPUT, FG_INFO,
367 "Heading = " << f->get_Psi() * RAD_TO_DEG
368 << " Roll = " << f->get_Phi() * RAD_TO_DEG
369 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
370 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
374 globals->inc_warp_delta( 30 );
375 fgUpdateSkyAndLightingParams();
378 current_options.cycle_view_mode();
381 fov = current_options.get_fov();
383 if ( fov < FG_FOV_MIN ) {
386 current_options.set_fov(fov);
387 v->force_update_fov_math();
390 #ifndef FG_OLD_WEATHER
391 tmp = WeatherDatabase->getWeatherVisibility();
393 WeatherDatabase->setWeatherVisibility( tmp );
395 tmp = current_weather.get_visibility(); // in meters
397 current_weather.set_visibility( tmp );
401 // if( fg_DebugOutput ) {
402 // fclose( fg_DebugOutput );
404 FG_LOG( FG_INPUT, FG_ALERT,
405 "Program exit requested." );
413 // Handle "special" keyboard events
414 void GLUTspecialkey(int k, int x, int y) {
419 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
421 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
425 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
426 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
429 ifstream input("fgfs.sav");
430 if (input.good() && fgLoadFlight(input)) {
432 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
434 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
439 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
440 ofstream output("fgfs.sav");
441 if (output.good() && fgSaveFlight(output)) {
443 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
445 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
451 current_properties.getStringValue("/sim/panel/path",
452 "Panels/Default/default.xml");
453 FGPanel * new_panel = fgReadPanel(panel_path);
454 if (new_panel == 0) {
455 FG_LOG(FG_INPUT, FG_ALERT,
456 "Error reading new panel from " << panel_path);
459 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
460 delete current_panel;
461 current_panel = new_panel;
465 FGPath props_path(current_options.get_fg_root());
466 props_path.append("preferences.xml");
467 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
468 if (!readPropertyList(props_path.str(), ¤t_properties)) {
469 FG_LOG(FG_INPUT, FG_ALERT,
470 "Failed to reread global preferences from "
471 << props_path.str());
473 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
478 current_panel->setYOffset(current_panel->getYOffset() - 5);
479 fgReshape(current_view.get_winWidth(),
480 current_view.get_winHeight());
484 current_panel->setYOffset(current_panel->getYOffset() + 5);
485 fgReshape(current_view.get_winWidth(),
486 current_view.get_winHeight());
490 current_panel->setXOffset(current_panel->getXOffset() - 5);
494 current_panel->setXOffset(current_panel->getXOffset() + 5);
497 case GLUT_KEY_END: // numeric keypad 1
498 v->set_goal_view_offset( FG_PI * 0.75 );
500 case GLUT_KEY_DOWN: // numeric keypad 2
501 v->set_goal_view_offset( FG_PI );
503 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
504 v->set_goal_view_offset( FG_PI * 1.25 );
506 case GLUT_KEY_LEFT: // numeric keypad 4
507 v->set_goal_view_offset( FG_PI * 0.50 );
509 case GLUT_KEY_RIGHT: // numeric keypad 6
510 v->set_goal_view_offset( FG_PI * 1.50 );
512 case GLUT_KEY_HOME: // numeric keypad 7
513 v->set_goal_view_offset( FG_PI * 0.25 );
515 case GLUT_KEY_UP: // numeric keypad 8
516 v->set_goal_view_offset( 0.00 );
518 case GLUT_KEY_PAGE_UP: // numeric keypad 9
519 v->set_goal_view_offset( FG_PI * 1.75 );
523 FG_LOG( FG_INPUT, FG_DEBUG, "" );
525 case GLUT_KEY_F2: // F2 Reload Tile Cache...
528 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
530 globals->set_freeze( true );
532 if ( global_tile_mgr.init() ) {
533 // Load the local scenery data
534 global_tile_mgr.update(
535 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
536 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
538 FG_LOG( FG_GENERAL, FG_ALERT,
539 "Error in Tile Manager initialization!" );
544 globals->set_freeze( false );
547 case GLUT_KEY_F3: // F3 Take a screen shot
550 case GLUT_KEY_F6: // F6 toggles Autopilot target location
551 if ( current_autopilot->get_HeadingMode() !=
552 FGAutopilot::FG_HEADING_WAYPOINT ) {
553 current_autopilot->set_HeadingMode(
554 FGAutopilot::FG_HEADING_WAYPOINT );
555 current_autopilot->set_HeadingEnabled( true );
557 current_autopilot->set_HeadingMode(
558 FGAutopilot::FG_HEADING_LOCK );
561 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
562 current_options.cycle_fog();
564 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
565 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
566 } else if ( current_options.get_fog() ==
567 fgOPTIONS::FG_FOG_FASTEST )
569 FG_LOG( FG_INPUT, FG_INFO,
570 "Fog enabled, hint set to fastest" );
571 } else if ( current_options.get_fog() ==
572 fgOPTIONS::FG_FOG_NICEST )
574 FG_LOG( FG_INPUT, FG_INFO,
575 "Fog enabled, hint set to nicest" );
579 case GLUT_KEY_F9: // F9 toggles textures on and off...
580 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
581 if ( current_options.get_textures() ) {
582 current_options.set_textures( false );
583 material_lib.set_step( 1 );
585 current_options.set_textures( true );
586 material_lib.set_step( 0 );
589 case GLUT_KEY_F10: // F10 toggles menu on and off...
590 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
593 case GLUT_KEY_F11: // F11 Altitude Dialog.
594 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
597 case GLUT_KEY_F12: // F12 Heading Dialog...
598 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
602 if ( current_autopilot->get_AltitudeEnabled() ) {
603 current_autopilot->AltitudeAdjust( -100 );
605 controls.move_elevator(0.05);
609 if ( current_autopilot->get_AltitudeEnabled() ) {
610 current_autopilot->AltitudeAdjust( 100 );
612 controls.move_elevator(-0.05);
616 controls.move_aileron(-0.05);
619 controls.move_aileron(0.05);
621 case GLUT_KEY_HOME: // numeric keypad 1
622 controls.move_elevator_trim(0.001);
624 case GLUT_KEY_END: // numeric keypad 7
625 controls.move_elevator_trim(-0.001);
627 case GLUT_KEY_INSERT: // numeric keypad Ins
628 if ( current_autopilot->get_HeadingEnabled() ) {
629 current_autopilot->HeadingAdjust( -1 );
631 controls.move_rudder(-0.05);
634 case 13: // numeric keypad Enter
635 if ( current_autopilot->get_HeadingEnabled() ) {
636 current_autopilot->HeadingAdjust( 1 );
638 controls.move_rudder(0.05);
641 case 53: // numeric keypad 5
642 controls.set_aileron(0.0);
643 controls.set_elevator(0.0);
644 controls.set_rudder(0.0);
646 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
647 if ( current_autopilot->get_AutoThrottleEnabled() ) {
648 current_autopilot->AutoThrottleAdjust( 5 );
650 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
653 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
654 if ( current_autopilot->get_AutoThrottleEnabled() ) {
655 current_autopilot->AutoThrottleAdjust( -5 );
657 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );