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 "fg_props.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 FGViewer *v = 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 );
157 case 50: // numeric keypad 2
158 v->set_goal_view_offset( FG_PI );
160 case 51: // numeric keypad 3
161 v->set_goal_view_offset( FG_PI * 1.25 );
163 case 52: // numeric keypad 4
164 v->set_goal_view_offset( FG_PI * 0.50 );
166 case 54: // numeric keypad 6
167 v->set_goal_view_offset( FG_PI * 1.50 );
169 case 55: // numeric keypad 7
170 v->set_goal_view_offset( FG_PI * 0.25 );
172 case 56: // numeric keypad 8
173 v->set_goal_view_offset( 0.00 );
175 case 57: // numeric keypad 9
176 v->set_goal_view_offset( FG_PI * 1.75 );
179 speed = globals->get_options()->get_speed_up();
184 globals->get_options()->set_speed_up( speed );
187 // status = globals->get_options()->get_hud_status();
188 // globals->get_options()->set_hud_status(!status);
189 HUD_brightkey( true );
193 fgHUDInit2(¤t_aircraft);
196 globals->inc_warp( -60 );
197 fgUpdateSkyAndLightingParams();
200 globals->get_options()->toggle_panel();
203 globals->inc_warp_delta( -30 );
204 fgUpdateSkyAndLightingParams();
207 #if defined(FX) && !defined(WIN32)
208 global_fullscreen = ( !global_fullscreen );
209 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
210 XMesaSetFXmode( global_fullscreen ?
211 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
216 fov = globals->get_current_view()->get_fov();
218 if ( fov > FG_FOV_MAX ) {
221 globals->get_current_view()->set_fov(fov);
222 // v->force_update_fov_math();
225 #ifndef FG_OLD_WEATHER
226 tmp = WeatherDatabase->getWeatherVisibility();
228 WeatherDatabase->setWeatherVisibility( tmp );
230 tmp = current_weather.get_visibility(); // in meters
232 current_weather.set_visibility( tmp );
237 FG_LOG( FG_INPUT, FG_DEBUG, "" );
239 case 50: // numeric keypad 2
240 if ( current_autopilot->get_AltitudeEnabled() ) {
241 current_autopilot->AltitudeAdjust( 100 );
243 controls.move_elevator(-0.05);
246 case 56: // numeric keypad 8
247 if ( current_autopilot->get_AltitudeEnabled() ) {
248 current_autopilot->AltitudeAdjust( -100 );
250 controls.move_elevator(0.05);
253 case 49: // numeric keypad 1
254 controls.move_elevator_trim(-0.001);
256 case 55: // numeric keypad 7
257 controls.move_elevator_trim(0.001);
259 case 52: // numeric keypad 4
260 controls.move_aileron(-0.05);
262 case 54: // numeric keypad 6
263 controls.move_aileron(0.05);
265 case 48: // numeric keypad Ins
266 if ( current_autopilot->get_HeadingEnabled() ) {
267 current_autopilot->HeadingAdjust( -1 );
269 controls.move_rudder(-0.05);
272 case 13: // numeric keypad Enter
273 if ( current_autopilot->get_HeadingEnabled() ) {
274 current_autopilot->HeadingAdjust( 1 );
276 controls.move_rudder(0.05);
279 case 53: // numeric keypad 5
280 controls.set_aileron(0.0);
281 controls.set_elevator(0.0);
282 controls.set_rudder(0.0);
284 case 57: // numeric keypad 9 (Pg Up)
285 if ( current_autopilot->get_AutoThrottleEnabled() ) {
286 current_autopilot->AutoThrottleAdjust( 5 );
288 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
291 case 51: // numeric keypad 3 (Pg Dn)
292 if ( current_autopilot->get_AutoThrottleEnabled() ) {
293 current_autopilot->AutoThrottleAdjust( -5 );
295 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
299 controls.move_flaps(-0.34);
300 FG_LOG( FG_INPUT, FG_INFO,
301 "Set flaps to " << controls.get_flaps() );
304 controls.move_flaps(0.34);
305 FG_LOG( FG_INPUT, FG_INFO,
306 "Set flaps to " << controls.get_flaps() );
309 speed = globals->get_options()->get_speed_up();
311 globals->get_options()->set_speed_up( speed );
316 b_ret = int( controls.get_brake( 0 ) );
317 b_set = double(!b_ret);
318 controls.set_brake( FGControls::ALL_WHEELS, b_set);
321 if (controls.get_brake(0) > 0.0) {
322 controls.set_brake(0, 0.0);
324 controls.set_brake(0, 1.0);
328 if (controls.get_brake(1) > 0.0) {
329 controls.set_brake(1, 0.0);
331 controls.set_brake(1, 1.0);
335 HUD_masterswitch( true );
338 fgHUDInit(¤t_aircraft); // normal HUD
341 globals->inc_warp( 60 );
342 fgUpdateSkyAndLightingParams();
345 globals->set_freeze( ! globals->get_freeze() );
348 SGBucket p( f->get_Longitude() * RAD_TO_DEG,
349 f->get_Latitude() * RAD_TO_DEG );
350 FGPath tile_path( globals->get_options()->get_fg_root() );
351 tile_path.append( "Scenery" );
352 tile_path.append( p.gen_base_path() );
353 tile_path.append( p.gen_index_str() );
355 // printf position and attitude information
356 FG_LOG( FG_INPUT, FG_INFO,
357 "Lon = " << f->get_Longitude() * RAD_TO_DEG
358 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
359 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
361 FG_LOG( FG_INPUT, FG_INFO,
362 "Heading = " << f->get_Psi() * RAD_TO_DEG
363 << " Roll = " << f->get_Phi() * RAD_TO_DEG
364 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
365 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
369 globals->inc_warp_delta( 30 );
370 fgUpdateSkyAndLightingParams();
373 // handles GUI state as well as Viewer LookAt Direction
375 globals->set_current_view( globals->get_viewmgr()->next_view() );
376 fgReshape( globals->get_options()->get_xsize(),
377 globals->get_options()->get_ysize() );
380 fov = globals->get_current_view()->get_fov();
382 if ( fov < FG_FOV_MIN ) {
385 globals->get_current_view()->set_fov(fov);
386 // v->force_update_fov_math();
389 #ifndef FG_OLD_WEATHER
390 tmp = WeatherDatabase->getWeatherVisibility();
392 WeatherDatabase->setWeatherVisibility( tmp );
394 tmp = current_weather.get_visibility(); // in meters
396 current_weather.set_visibility( tmp );
400 // if( fg_DebugOutput ) {
401 // fclose( fg_DebugOutput );
403 FG_LOG( FG_INPUT, FG_ALERT,
404 "Program exit requested." );
412 // Handle "special" keyboard events
413 void GLUTspecialkey(int k, int x, int y) {
414 FGViewer *v = globals->get_current_view();
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 cerr << "Opening output stream" << endl;
438 ofstream output("fgfs.sav");
439 cerr << "output stream opened" << endl;
440 if (output.good() && fgSaveFlight(output)) {
442 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
444 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
450 globals->get_props()->getStringValue("/sim/panel/path",
451 "Panels/Default/default.xml");
452 FGPanel * new_panel = fgReadPanel(panel_path);
453 if (new_panel == 0) {
454 FG_LOG(FG_INPUT, FG_ALERT,
455 "Error reading new panel from " << panel_path);
458 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
459 delete current_panel;
460 current_panel = new_panel;
464 FGPath props_path(globals->get_options()->get_fg_root());
465 props_path.append("preferences.xml");
466 FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
467 if (!readProperties(props_path.str(), globals->get_props())) {
468 FG_LOG(FG_INPUT, FG_ALERT,
469 "Failed to reread global preferences from "
470 << props_path.str());
472 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
477 current_panel->setYOffset(current_panel->getYOffset() - 5);
478 fgReshape(globals->get_options()->get_xsize(),
479 globals->get_options()->get_ysize());
483 current_panel->setYOffset(current_panel->getYOffset() + 5);
484 fgReshape(globals->get_options()->get_xsize(),
485 globals->get_options()->get_ysize());
489 current_panel->setXOffset(current_panel->getXOffset() - 5);
493 current_panel->setXOffset(current_panel->getXOffset() + 5);
496 case GLUT_KEY_END: // numeric keypad 1
497 v->set_goal_view_offset( FG_PI * 0.75 );
499 case GLUT_KEY_DOWN: // numeric keypad 2
500 v->set_goal_view_offset( FG_PI );
502 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
503 v->set_goal_view_offset( FG_PI * 1.25 );
505 case GLUT_KEY_LEFT: // numeric keypad 4
506 v->set_goal_view_offset( FG_PI * 0.50 );
508 case GLUT_KEY_RIGHT: // numeric keypad 6
509 v->set_goal_view_offset( FG_PI * 1.50 );
511 case GLUT_KEY_HOME: // numeric keypad 7
512 v->set_goal_view_offset( FG_PI * 0.25 );
514 case GLUT_KEY_UP: // numeric keypad 8
515 v->set_goal_view_offset( 0.00 );
517 case GLUT_KEY_PAGE_UP: // numeric keypad 9
518 v->set_goal_view_offset( FG_PI * 1.75 );
522 FG_LOG( FG_INPUT, FG_DEBUG, "" );
524 case GLUT_KEY_F2: // F2 Reload Tile Cache...
526 bool freeze = globals->get_freeze();
527 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
529 globals->set_freeze( true );
531 if ( global_tile_mgr.init() ) {
532 // Load the local scenery data
533 global_tile_mgr.update(
534 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
535 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
537 FG_LOG( FG_GENERAL, FG_ALERT,
538 "Error in Tile Manager initialization!" );
543 globals->set_freeze( false );
546 case GLUT_KEY_F3: // F3 Take a screen shot
549 case GLUT_KEY_F4: // F4 Update lighting manually
550 fgUpdateSkyAndLightingParams();
551 case GLUT_KEY_F6: // F6 toggles Autopilot target location
552 if ( current_autopilot->get_HeadingMode() !=
553 FGAutopilot::FG_HEADING_WAYPOINT ) {
554 current_autopilot->set_HeadingMode(
555 FGAutopilot::FG_HEADING_WAYPOINT );
556 current_autopilot->set_HeadingEnabled( true );
558 current_autopilot->set_HeadingMode(
559 FGAutopilot::FG_HEADING_LOCK );
562 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
563 globals->get_options()->cycle_fog();
565 if ( globals->get_options()->get_fog() ==
566 FGOptions::FG_FOG_DISABLED )
568 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
569 } else if ( globals->get_options()->get_fog() ==
570 FGOptions::FG_FOG_FASTEST )
572 FG_LOG( FG_INPUT, FG_INFO,
573 "Fog enabled, hint set to fastest" );
574 } else if ( globals->get_options()->get_fog() ==
575 FGOptions::FG_FOG_NICEST )
577 FG_LOG( FG_INPUT, FG_INFO,
578 "Fog enabled, hint set to nicest" );
582 case GLUT_KEY_F9: // F9 toggles textures on and off...
583 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
584 if ( globals->get_options()->get_textures() ) {
585 globals->get_options()->set_textures( false );
586 material_lib.set_step( 1 );
588 globals->get_options()->set_textures( true );
589 material_lib.set_step( 0 );
592 case GLUT_KEY_F10: // F10 toggles menu on and off...
593 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
596 case GLUT_KEY_F11: // F11 Altitude Dialog.
597 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
600 case GLUT_KEY_F12: // F12 Heading Dialog...
601 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
605 if ( current_autopilot->get_AltitudeEnabled() ) {
606 current_autopilot->AltitudeAdjust( -100 );
608 controls.move_elevator(0.05);
612 if ( current_autopilot->get_AltitudeEnabled() ) {
613 current_autopilot->AltitudeAdjust( 100 );
615 controls.move_elevator(-0.05);
619 controls.move_aileron(-0.05);
622 controls.move_aileron(0.05);
624 case GLUT_KEY_HOME: // numeric keypad 1
625 controls.move_elevator_trim(0.001);
627 case GLUT_KEY_END: // numeric keypad 7
628 controls.move_elevator_trim(-0.001);
630 case GLUT_KEY_INSERT: // numeric keypad Ins
631 if ( current_autopilot->get_HeadingEnabled() ) {
632 current_autopilot->HeadingAdjust( -1 );
634 controls.move_rudder(-0.05);
637 case 13: // numeric keypad Enter
638 if ( current_autopilot->get_HeadingEnabled() ) {
639 current_autopilot->HeadingAdjust( 1 );
641 controls.move_rudder(0.05);
644 case 53: // numeric keypad 5
645 controls.set_aileron(0.0);
646 controls.set_elevator(0.0);
647 controls.set_rudder(0.0);
649 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
650 if ( current_autopilot->get_AutoThrottleEnabled() ) {
651 current_autopilot->AutoThrottleAdjust( 5 );
653 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
656 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
657 if ( current_autopilot->get_AutoThrottleEnabled() ) {
658 current_autopilot->AutoThrottleAdjust( -5 );
660 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );