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>
58 #include <Cockpit/sp_panel.hxx>
60 #include <Scenery/tilemgr.hxx>
61 #include <Objects/matlib.hxx>
62 #include <Time/light.hxx>
63 #include <Time/tmp.hxx>
65 #ifndef FG_OLD_WEATHER
66 # include <WeatherCM/FGLocalWeatherDatabase.h>
68 # include <Weather/weather.hxx>
72 #include "globals.hxx"
73 #include "keyboard.hxx"
74 #include "options.hxx"
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;
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( current_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 );
160 case 50: // numeric keypad 2
161 v->set_goal_view_offset( FG_PI );
163 case 51: // numeric keypad 3
164 v->set_goal_view_offset( FG_PI * 1.25 );
166 case 52: // numeric keypad 4
167 v->set_goal_view_offset( FG_PI * 0.50 );
169 case 54: // numeric keypad 6
170 v->set_goal_view_offset( FG_PI * 1.50 );
172 case 55: // numeric keypad 7
173 v->set_goal_view_offset( FG_PI * 0.25 );
175 case 56: // numeric keypad 8
176 v->set_goal_view_offset( 0.00 );
178 case 57: // numeric keypad 9
179 v->set_goal_view_offset( FG_PI * 1.75 );
182 speed = current_options.get_speed_up();
187 current_options.set_speed_up( speed );
190 // status = current_options.get_hud_status();
191 // current_options.set_hud_status(!status);
192 HUD_brightkey( true );
196 fgHUDInit2(¤t_aircraft);
199 globals->inc_warp( -60 );
200 fgUpdateSkyAndLightingParams();
203 current_options.toggle_panel();
206 globals->inc_warp_delta( -30 );
207 fgUpdateSkyAndLightingParams();
210 #if defined(FX) && !defined(WIN32)
211 global_fullscreen = ( !global_fullscreen );
212 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
213 XMesaSetFXmode( global_fullscreen ?
214 XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
219 fov = current_options.get_fov();
221 if ( fov > FG_FOV_MAX ) {
224 current_options.set_fov(fov);
225 v->force_update_fov_math();
228 #ifndef FG_OLD_WEATHER
229 tmp = WeatherDatabase->getWeatherVisibility();
231 WeatherDatabase->setWeatherVisibility( tmp );
233 tmp = current_weather.get_visibility(); // in meters
235 current_weather.set_visibility( tmp );
240 FG_LOG( FG_INPUT, FG_DEBUG, "" );
242 case 50: // numeric keypad 2
243 if ( current_autopilot->get_AltitudeEnabled() ) {
244 current_autopilot->AltitudeAdjust( 100 );
246 controls.move_elevator(-0.05);
249 case 56: // numeric keypad 8
250 if ( current_autopilot->get_AltitudeEnabled() ) {
251 current_autopilot->AltitudeAdjust( -100 );
253 controls.move_elevator(0.05);
256 case 49: // numeric keypad 1
257 controls.move_elevator_trim(-0.001);
259 case 55: // numeric keypad 7
260 controls.move_elevator_trim(0.001);
262 case 52: // numeric keypad 4
263 controls.move_aileron(-0.05);
265 case 54: // numeric keypad 6
266 controls.move_aileron(0.05);
268 case 48: // numeric keypad Ins
269 if ( current_autopilot->get_HeadingEnabled() ) {
270 current_autopilot->HeadingAdjust( -1 );
272 controls.move_rudder(-0.05);
275 case 13: // numeric keypad Enter
276 if ( current_autopilot->get_HeadingEnabled() ) {
277 current_autopilot->HeadingAdjust( 1 );
279 controls.move_rudder(0.05);
282 case 53: // numeric keypad 5
283 controls.set_aileron(0.0);
284 controls.set_elevator(0.0);
285 controls.set_rudder(0.0);
287 case 57: // numeric keypad 9 (Pg Up)
288 if ( current_autopilot->get_AutoThrottleEnabled() ) {
289 current_autopilot->AutoThrottleAdjust( 5 );
291 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
294 case 51: // numeric keypad 3 (Pg Dn)
295 if ( current_autopilot->get_AutoThrottleEnabled() ) {
296 current_autopilot->AutoThrottleAdjust( -5 );
298 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
302 controls.move_flaps(-0.34);
303 FG_LOG( FG_INPUT, FG_INFO,
304 "Set flaps to " << controls.get_flaps() );
307 controls.move_flaps(0.34);
308 FG_LOG( FG_INPUT, FG_INFO,
309 "Set flaps to " << controls.get_flaps() );
312 speed = current_options.get_speed_up();
314 current_options.set_speed_up( speed );
319 b_ret = int( controls.get_brake( 0 ) );
320 b_set = double(!b_ret);
321 controls.set_brake( FGControls::ALL_WHEELS, b_set);
324 if (controls.get_brake(0) > 0.0) {
325 controls.set_brake(0, 0.0);
327 controls.set_brake(0, 1.0);
331 if (controls.get_brake(1) > 0.0) {
332 controls.set_brake(1, 0.0);
334 controls.set_brake(1, 1.0);
338 HUD_masterswitch( true );
341 fgHUDInit(¤t_aircraft); // normal HUD
344 globals->inc_warp( 60 );
345 fgUpdateSkyAndLightingParams();
348 globals->set_freeze( ! globals->get_freeze() );
351 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
352 f->get_Latitude() * RAD_TO_DEG );
353 FGPath tile_path( current_options.get_fg_root() );
354 tile_path.append( "Scenery" );
355 tile_path.append( p.gen_base_path() );
356 tile_path.append( p.gen_index_str() );
358 // printf position and attitude information
359 FG_LOG( FG_INPUT, FG_INFO,
360 "Lon = " << f->get_Longitude() * RAD_TO_DEG
361 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
362 << " Altitude = " << f->get_Altitude() * FEET_TO_METER
364 FG_LOG( FG_INPUT, FG_INFO,
365 "Heading = " << f->get_Psi() * RAD_TO_DEG
366 << " Roll = " << f->get_Phi() * RAD_TO_DEG
367 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
368 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
372 globals->inc_warp_delta( 30 );
373 fgUpdateSkyAndLightingParams();
376 current_options.cycle_view_mode();
379 fov = current_options.get_fov();
381 if ( fov < FG_FOV_MIN ) {
384 current_options.set_fov(fov);
385 v->force_update_fov_math();
388 #ifndef FG_OLD_WEATHER
389 tmp = WeatherDatabase->getWeatherVisibility();
391 WeatherDatabase->setWeatherVisibility( tmp );
393 tmp = current_weather.get_visibility(); // in meters
395 current_weather.set_visibility( tmp );
399 // if( fg_DebugOutput ) {
400 // fclose( fg_DebugOutput );
402 FG_LOG( FG_INPUT, FG_ALERT,
403 "Program exiting normally at user request." );
411 // Handle "special" keyboard events
412 void GLUTspecialkey(int k, int x, int y) {
417 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
419 if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
423 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
424 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
427 ifstream input("fgfs.sav");
428 if (input.good() && fgLoadFlight(input)) {
430 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
432 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
437 FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
438 ofstream output("fgfs.sav");
439 if (output.good() && fgSaveFlight(output)) {
441 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
443 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
449 current_properties.getStringValue("/sim/panel",
450 "Panels/Default/default.xml");
451 FGPanel * new_panel = fgReadPanel(panel_path);
452 if (new_panel == 0) {
453 FG_LOG(FG_INPUT, FG_ALERT,
454 "Error reading new panel from " << panel_path);
457 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
458 delete current_panel;
459 current_panel = new_panel;
462 case GLUT_KEY_END: // numeric keypad 1
463 v->set_goal_view_offset( FG_PI * 0.75 );
465 case GLUT_KEY_DOWN: // numeric keypad 2
466 v->set_goal_view_offset( FG_PI );
468 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
469 v->set_goal_view_offset( FG_PI * 1.25 );
471 case GLUT_KEY_LEFT: // numeric keypad 4
472 v->set_goal_view_offset( FG_PI * 0.50 );
474 case GLUT_KEY_RIGHT: // numeric keypad 6
475 v->set_goal_view_offset( FG_PI * 1.50 );
477 case GLUT_KEY_HOME: // numeric keypad 7
478 v->set_goal_view_offset( FG_PI * 0.25 );
480 case GLUT_KEY_UP: // numeric keypad 8
481 v->set_goal_view_offset( 0.00 );
483 case GLUT_KEY_PAGE_UP: // numeric keypad 9
484 v->set_goal_view_offset( FG_PI * 1.75 );
488 FG_LOG( FG_INPUT, FG_DEBUG, "" );
490 case GLUT_KEY_F2: // F2 Reload Tile Cache...
493 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
495 globals->set_freeze( true );
497 if ( global_tile_mgr.init() ) {
498 // Load the local scenery data
499 global_tile_mgr.update(
500 cur_fdm_state->get_Longitude() * RAD_TO_DEG,
501 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
503 FG_LOG( FG_GENERAL, FG_ALERT,
504 "Error in Tile Manager initialization!" );
509 globals->set_freeze( false );
512 case GLUT_KEY_F3: // F3 Take a screen shot
515 case GLUT_KEY_F6: // F6 toggles Autopilot target location
516 if ( current_autopilot->get_HeadingMode() !=
517 FGAutopilot::FG_HEADING_WAYPOINT ) {
518 current_autopilot->set_HeadingMode(
519 FGAutopilot::FG_HEADING_WAYPOINT );
521 current_autopilot->set_HeadingMode(
522 FGAutopilot::FG_HEADING_LOCK );
525 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
526 current_options.cycle_fog();
528 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
529 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
530 } else if ( current_options.get_fog() ==
531 fgOPTIONS::FG_FOG_FASTEST )
533 FG_LOG( FG_INPUT, FG_INFO,
534 "Fog enabled, hint set to fastest" );
535 } else if ( current_options.get_fog() ==
536 fgOPTIONS::FG_FOG_NICEST )
538 FG_LOG( FG_INPUT, FG_INFO,
539 "Fog enabled, hint set to nicest" );
543 case GLUT_KEY_F9: // F9 toggles textures on and off...
544 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
545 if ( current_options.get_textures() ) {
546 current_options.set_textures( false );
547 material_lib.set_step( 1 );
549 current_options.set_textures( true );
550 material_lib.set_step( 0 );
553 case GLUT_KEY_F10: // F10 toggles menu on and off...
554 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
557 case GLUT_KEY_F11: // F11 Altitude Dialog.
558 FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
561 case GLUT_KEY_F12: // F12 Heading Dialog...
562 FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
566 if ( current_autopilot->get_AltitudeEnabled() ) {
567 current_autopilot->AltitudeAdjust( -100 );
569 controls.move_elevator(0.05);
573 if ( current_autopilot->get_AltitudeEnabled() ) {
574 current_autopilot->AltitudeAdjust( 100 );
576 controls.move_elevator(-0.05);
580 controls.move_aileron(-0.05);
583 controls.move_aileron(0.05);
585 case GLUT_KEY_HOME: // numeric keypad 1
586 controls.move_elevator_trim(0.001);
588 case GLUT_KEY_END: // numeric keypad 7
589 controls.move_elevator_trim(-0.001);
591 case GLUT_KEY_INSERT: // numeric keypad Ins
592 if ( current_autopilot->get_HeadingEnabled() ) {
593 current_autopilot->HeadingAdjust( -1 );
595 controls.move_rudder(-0.05);
598 case 13: // numeric keypad Enter
599 if ( current_autopilot->get_HeadingEnabled() ) {
600 current_autopilot->HeadingAdjust( 1 );
602 controls.move_rudder(0.05);
605 case 53: // numeric keypad 5
606 controls.set_aileron(0.0);
607 controls.set_elevator(0.0);
608 controls.set_rudder(0.0);
610 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
611 if ( current_autopilot->get_AutoThrottleEnabled() ) {
612 current_autopilot->AutoThrottleAdjust( 5 );
614 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
617 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
618 if ( current_autopilot->get_AutoThrottleEnabled() ) {
619 current_autopilot->AutoThrottleAdjust( -5 );
621 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );