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 // Handle keyboard events
79 void GLUTkey(unsigned char k, int x, int y) {
84 static bool winding_ccw = true;
87 f = current_aircraft.fdm_state;
90 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
91 if ( puKeyboard(k, PU_DOWN) ) {
95 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
96 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
99 current_autopilot->set_AltitudeMode(
100 FGAutopilot::FG_ALTITUDE_LOCK );
101 current_autopilot->set_AltitudeEnabled(
102 ! current_autopilot->get_AltitudeEnabled()
105 case 7: // Ctrl-G key
106 current_autopilot->set_AltitudeMode(
107 FGAutopilot::FG_ALTITUDE_GS1 );
108 current_autopilot->set_AltitudeEnabled(
109 ! current_autopilot->get_AltitudeEnabled()
112 case 8: // Ctrl-H key
113 current_autopilot->set_HeadingMode(
114 FGAutopilot::FG_HEADING_LOCK );
115 current_autopilot->set_HeadingEnabled(
116 ! current_autopilot->get_HeadingEnabled()
119 case 14: // Ctrl-N key
120 current_autopilot->set_HeadingMode(
121 FGAutopilot::FG_HEADING_NAV1 );
122 current_autopilot->set_HeadingEnabled(
123 ! current_autopilot->get_HeadingEnabled()
126 case 18: // Ctrl-R key
128 winding_ccw = !winding_ccw;
130 glFrontFace ( GL_CCW );
132 glFrontFace ( GL_CW );
135 case 19: // Ctrl-S key
136 current_autopilot->set_AutoThrottleEnabled(
137 ! current_autopilot->get_AutoThrottleEnabled()
140 case 20: // Ctrl-T key
141 current_autopilot->set_AltitudeMode(
142 FGAutopilot::FG_ALTITUDE_TERRAIN );
143 current_autopilot->set_AltitudeEnabled(
144 ! current_autopilot->get_AltitudeEnabled()
147 case 21: // Ctrl-U key
148 // add 1000' of emergency altitude. Possibly good for
149 // unflipping yourself :-)
151 double alt = cur_fdm_state->get_Altitude() + 1000;
152 fgFDMForceAltitude( current_options.get_flight_model(),
153 alt * FEET_TO_METER );
156 case 49: // numeric keypad 1
157 v->set_goal_view_offset( FG_PI * 0.75 );
159 case 50: // numeric keypad 2
160 v->set_goal_view_offset( FG_PI );
162 case 51: // numeric keypad 3
163 v->set_goal_view_offset( FG_PI * 1.25 );
165 case 52: // numeric keypad 4
166 v->set_goal_view_offset( FG_PI * 0.50 );
168 case 54: // numeric keypad 6
169 v->set_goal_view_offset( FG_PI * 1.50 );
171 case 55: // numeric keypad 7
172 v->set_goal_view_offset( FG_PI * 0.25 );
174 case 56: // numeric keypad 8
175 v->set_goal_view_offset( 0.00 );
177 case 57: // numeric keypad 9
178 v->set_goal_view_offset( FG_PI * 1.75 );
181 speed = current_options.get_speed_up();
186 current_options.set_speed_up( speed );
189 // status = current_options.get_hud_status();
190 // current_options.set_hud_status(!status);
191 HUD_brightkey( true );
195 fgHUDInit2(¤t_aircraft);
198 globals->inc_warp( -60 );
199 fgUpdateSkyAndLightingParams();
202 current_options.toggle_panel();
205 globals->inc_warp_delta( -30 );
206 fgUpdateSkyAndLightingParams();
209 #if defined(FX) && !defined(WIN32)
210 global_fullscreen = ( !global_fullscreen );
211 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
212 XMesaSetFXmode( global_fullscreen ?
213 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
218 fov = current_options.get_fov();
220 if ( fov > FG_FOV_MAX ) {
223 current_options.set_fov(fov);
224 v->force_update_fov_math();
227 #ifndef FG_OLD_WEATHER
228 tmp = WeatherDatabase->getWeatherVisibility();
230 WeatherDatabase->setWeatherVisibility( tmp );
232 tmp = current_weather.get_visibility(); // in meters
234 current_weather.set_visibility( tmp );
239 FG_LOG( FG_INPUT, FG_DEBUG, "" );
241 case 50: // numeric keypad 2
242 if ( current_autopilot->get_AltitudeEnabled() ) {
243 current_autopilot->AltitudeAdjust( 100 );
245 controls.move_elevator(-0.05);
248 case 56: // numeric keypad 8
249 if ( current_autopilot->get_AltitudeEnabled() ) {
250 current_autopilot->AltitudeAdjust( -100 );
252 controls.move_elevator(0.05);
255 case 49: // numeric keypad 1
256 controls.move_elevator_trim(-0.001);
258 case 55: // numeric keypad 7
259 controls.move_elevator_trim(0.001);
261 case 52: // numeric keypad 4
262 controls.move_aileron(-0.05);
264 case 54: // numeric keypad 6
265 controls.move_aileron(0.05);
267 case 48: // numeric keypad Ins
268 if ( current_autopilot->get_HeadingEnabled() ) {
269 current_autopilot->HeadingAdjust( -1 );
271 controls.move_rudder(-0.05);
274 case 13: // numeric keypad Enter
275 if ( current_autopilot->get_HeadingEnabled() ) {
276 current_autopilot->HeadingAdjust( 1 );
278 controls.move_rudder(0.05);
281 case 53: // numeric keypad 5
282 controls.set_aileron(0.0);
283 controls.set_elevator(0.0);
284 controls.set_rudder(0.0);
286 case 57: // numeric keypad 9 (Pg Up)
287 if ( current_autopilot->get_AutoThrottleEnabled() ) {
288 current_autopilot->AutoThrottleAdjust( 5 );
290 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
293 case 51: // numeric keypad 3 (Pg Dn)
294 if ( current_autopilot->get_AutoThrottleEnabled() ) {
295 current_autopilot->AutoThrottleAdjust( -5 );
297 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
301 controls.move_flaps(-0.34);
302 FG_LOG( FG_INPUT, FG_INFO,
303 "Set flaps to " << controls.get_flaps() );
306 controls.move_flaps(0.34);
307 FG_LOG( FG_INPUT, FG_INFO,
308 "Set flaps to " << controls.get_flaps() );
311 speed = current_options.get_speed_up();
313 current_options.set_speed_up( speed );
318 b_ret = int( controls.get_brake( 0 ) );
319 b_set = double(!b_ret);
320 controls.set_brake( FGControls::ALL_WHEELS, b_set);
323 if (controls.get_brake(0) > 0.0) {
324 controls.set_brake(0, 0.0);
326 controls.set_brake(0, 1.0);
330 if (controls.get_brake(1) > 0.0) {
331 controls.set_brake(1, 0.0);
333 controls.set_brake(1, 1.0);
337 HUD_masterswitch( true );
340 fgHUDInit(¤t_aircraft); // normal HUD
343 globals->inc_warp( 60 );
344 fgUpdateSkyAndLightingParams();
347 globals->set_freeze( ! globals->get_freeze() );
350 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
351 f->get_Latitude() * RAD_TO_DEG );
352 FGPath tile_path( current_options.get_fg_root() );
353 tile_path.append( "Scenery" );
354 tile_path.append( p.gen_base_path() );
355 tile_path.append( p.gen_index_str() );
357 // printf position and attitude information
358 FG_LOG( FG_INPUT, FG_INFO,
359 "Lon = " << f->get_Longitude() * RAD_TO_DEG
360 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
361 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
363 FG_LOG( FG_INPUT, FG_INFO,
364 "Heading = " << f->get_Psi() * RAD_TO_DEG
365 << " Roll = " << f->get_Phi() * RAD_TO_DEG
366 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
367 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
371 globals->inc_warp_delta( 30 );
372 fgUpdateSkyAndLightingParams();
375 current_options.cycle_view_mode();
378 fov = current_options.get_fov();
380 if ( fov < FG_FOV_MIN ) {
383 current_options.set_fov(fov);
384 v->force_update_fov_math();
387 #ifndef FG_OLD_WEATHER
388 tmp = WeatherDatabase->getWeatherVisibility();
390 WeatherDatabase->setWeatherVisibility( tmp );
392 tmp = current_weather.get_visibility(); // in meters
394 current_weather.set_visibility( tmp );
398 // if( fg_DebugOutput ) {
399 // fclose( fg_DebugOutput );
401 FG_LOG( FG_INPUT, FG_ALERT,
402 "Program exiting normally at user request." );
410 // Handle "special" keyboard events
411 void GLUTspecialkey(int k, int x, int y) {
416 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
418 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
422 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
423 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
426 ifstream input("fgfs.sav");
427 if (input.good() && fgLoadFlight(input)) {
429 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
431 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
436 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
437 ofstream output("fgfs.sav");
438 if (output.good() && fgSaveFlight(output)) {
440 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
442 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
448 current_properties.getStringValue("/sim/panel/path",
449 "Panels/Default/default.xml");
450 FGPanel * new_panel = fgReadPanel(panel_path);
451 if (new_panel == 0) {
452 FG_LOG(FG_INPUT, FG_ALERT,
453 "Error reading new panel from " << panel_path);
456 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
457 delete current_panel;
458 current_panel = new_panel;
462 FGPath props_path(current_options.get_fg_root());
463 props_path.append("preferences.xml");
464 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
465 if (!readPropertyList(props_path.str(), ¤t_properties)) {
466 FG_LOG(FG_INPUT, FG_ALERT,
467 "Failed to reread global preferences from "
468 << props_path.str());
470 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
474 case GLUT_KEY_END: // numeric keypad 1
475 v->set_goal_view_offset( FG_PI * 0.75 );
477 case GLUT_KEY_DOWN: // numeric keypad 2
478 v->set_goal_view_offset( FG_PI );
480 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
481 v->set_goal_view_offset( FG_PI * 1.25 );
483 case GLUT_KEY_LEFT: // numeric keypad 4
484 v->set_goal_view_offset( FG_PI * 0.50 );
486 case GLUT_KEY_RIGHT: // numeric keypad 6
487 v->set_goal_view_offset( FG_PI * 1.50 );
489 case GLUT_KEY_HOME: // numeric keypad 7
490 v->set_goal_view_offset( FG_PI * 0.25 );
492 case GLUT_KEY_UP: // numeric keypad 8
493 v->set_goal_view_offset( 0.00 );
495 case GLUT_KEY_PAGE_UP: // numeric keypad 9
496 v->set_goal_view_offset( FG_PI * 1.75 );
500 FG_LOG( FG_INPUT, FG_DEBUG, "" );
502 case GLUT_KEY_F2: // F2 Reload Tile Cache...
505 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
507 globals->set_freeze( true );
509 if ( global_tile_mgr.init() ) {
510 // Load the local scenery data
511 global_tile_mgr.update(
512 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
513 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
515 FG_LOG( FG_GENERAL, FG_ALERT,
516 "Error in Tile Manager initialization!" );
521 globals->set_freeze( false );
524 case GLUT_KEY_F3: // F3 Take a screen shot
527 case GLUT_KEY_F6: // F6 toggles Autopilot target location
528 if ( current_autopilot->get_HeadingMode() !=
529 FGAutopilot::FG_HEADING_WAYPOINT ) {
530 current_autopilot->set_HeadingMode(
531 FGAutopilot::FG_HEADING_WAYPOINT );
533 current_autopilot->set_HeadingMode(
534 FGAutopilot::FG_HEADING_LOCK );
537 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
538 current_options.cycle_fog();
540 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
541 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
542 } else if ( current_options.get_fog() ==
543 fgOPTIONS::FG_FOG_FASTEST )
545 FG_LOG( FG_INPUT, FG_INFO,
546 "Fog enabled, hint set to fastest" );
547 } else if ( current_options.get_fog() ==
548 fgOPTIONS::FG_FOG_NICEST )
550 FG_LOG( FG_INPUT, FG_INFO,
551 "Fog enabled, hint set to nicest" );
555 case GLUT_KEY_F9: // F9 toggles textures on and off...
556 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
557 if ( current_options.get_textures() ) {
558 current_options.set_textures( false );
559 material_lib.set_step( 1 );
561 current_options.set_textures( true );
562 material_lib.set_step( 0 );
565 case GLUT_KEY_F10: // F10 toggles menu on and off...
566 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
569 case GLUT_KEY_F11: // F11 Altitude Dialog.
570 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
573 case GLUT_KEY_F12: // F12 Heading Dialog...
574 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
578 if ( current_autopilot->get_AltitudeEnabled() ) {
579 current_autopilot->AltitudeAdjust( -100 );
581 controls.move_elevator(0.05);
585 if ( current_autopilot->get_AltitudeEnabled() ) {
586 current_autopilot->AltitudeAdjust( 100 );
588 controls.move_elevator(-0.05);
592 controls.move_aileron(-0.05);
595 controls.move_aileron(0.05);
597 case GLUT_KEY_HOME: // numeric keypad 1
598 controls.move_elevator_trim(0.001);
600 case GLUT_KEY_END: // numeric keypad 7
601 controls.move_elevator_trim(-0.001);
603 case GLUT_KEY_INSERT: // numeric keypad Ins
604 if ( current_autopilot->get_HeadingEnabled() ) {
605 current_autopilot->HeadingAdjust( -1 );
607 controls.move_rudder(-0.05);
610 case 13: // numeric keypad Enter
611 if ( current_autopilot->get_HeadingEnabled() ) {
612 current_autopilot->HeadingAdjust( 1 );
614 controls.move_rudder(0.05);
617 case 53: // numeric keypad 5
618 controls.set_aileron(0.0);
619 controls.set_elevator(0.0);
620 controls.set_rudder(0.0);
622 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
623 if ( current_autopilot->get_AutoThrottleEnabled() ) {
624 current_autopilot->AutoThrottleAdjust( 5 );
626 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
629 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
630 if ( current_autopilot->get_AutoThrottleEnabled() ) {
631 current_autopilot->AutoThrottleAdjust( -5 );
633 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );