]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Added code to put aircraft at the end of the runway closest to the desired
[flightgear.git] / src / Main / keyboard.cxx
1 // keyboard.cxx -- handle GLUT keyboard events
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997 - 1999  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <simgear/compiler.h>
33
34 #include <GL/glut.h>
35 #include <simgear/xgl/xgl.h>
36
37 #if defined(FX) && defined(XMESA)
38 #include <GL/xmesa.h>
39 #endif
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #include STL_FSTREAM
45
46 #include <plib/pu.h>            // plib include
47
48 #include <simgear/constants.h>
49 #include <simgear/debug/logstream.hxx>
50 #include <simgear/misc/fgpath.hxx>
51
52 #include <Aircraft/aircraft.hxx>
53 #include <Autopilot/auto_gui.hxx>
54 #include <Autopilot/newauto.hxx>
55 #include <Cockpit/hud.hxx>
56 #include <GUI/gui.h>
57 #include <Scenery/tilemgr.hxx>
58 #include <Objects/matlib.hxx>
59 #include <Time/light.hxx>
60 #include <Time/tmp.hxx>
61
62 #ifndef FG_OLD_WEATHER
63 #  include <WeatherCM/FGLocalWeatherDatabase.h>
64 #else
65 #  include <Weather/weather.hxx>
66 #endif
67
68 #include "bfi.hxx"
69 #include "globals.hxx"
70 #include "keyboard.hxx"
71 #include "options.hxx"
72 #include "save.hxx"
73 #include "views.hxx"
74
75
76 // Handle keyboard events
77 void GLUTkey(unsigned char k, int x, int y) {
78     FGInterface *f;
79     SGTime *t;
80     FGView *v;
81     float fov, tmp;
82     static bool winding_ccw = true;
83     int speed;
84
85     f = current_aircraft.fdm_state;
86     v = &current_view;
87
88     FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
89     if ( puKeyboard(k, PU_DOWN) ) {
90         return;
91     }
92
93     if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
94         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
95         switch (k) {
96         case 1: // Ctrl-A key
97             current_autopilot->set_AltitudeMode( 
98                   FGAutopilot::FG_ALTITUDE_LOCK );
99             current_autopilot->set_AltitudeEnabled(
100                   ! current_autopilot->get_AltitudeEnabled()
101                 );
102             return;
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()
108                 );
109             return;
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()
115                 );
116             return;
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()
122                 );
123             return;
124         case 18: // Ctrl-R key
125             // temporary
126             winding_ccw = !winding_ccw;
127             if ( winding_ccw ) {
128                 glFrontFace ( GL_CCW );
129             } else {
130                 glFrontFace ( GL_CW );
131             }
132             return;
133         case 19: // Ctrl-S key
134             current_autopilot->set_AutoThrottleEnabled(
135                   ! current_autopilot->get_AutoThrottleEnabled()
136                 );
137             return;
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()
143                 );
144             return;
145         case 21: // Ctrl-U key
146             // add 1000' of emergency altitude.  Possibly good for 
147             // unflipping yourself :-)
148             {
149                 double alt = cur_fdm_state->get_Altitude() + 1000;
150                 fgFDMForceAltitude( current_options.get_flight_model(), 
151                                     alt * FEET_TO_METER );
152             }
153             return;
154         case 49: // numeric keypad 1
155             v->set_goal_view_offset( FG_PI * 0.75 );
156             return;
157         case 50: // numeric keypad 2
158             v->set_goal_view_offset( FG_PI );
159             return;
160         case 51: // numeric keypad 3
161             v->set_goal_view_offset( FG_PI * 1.25 );
162             return;
163         case 52: // numeric keypad 4
164             v->set_goal_view_offset( FG_PI * 0.50 );
165             return;
166         case 54: // numeric keypad 6
167             v->set_goal_view_offset( FG_PI * 1.50 );
168             return;
169         case 55: // numeric keypad 7
170             v->set_goal_view_offset( FG_PI * 0.25 );
171             return;
172         case 56: // numeric keypad 8
173             v->set_goal_view_offset( 0.00 );
174             return;
175         case 57: // numeric keypad 9
176             v->set_goal_view_offset( FG_PI * 1.75 );
177             return;
178         case 65: // A key
179             speed = current_options.get_speed_up();
180             speed--;
181             if ( speed < 1 ) {
182                 speed = 1;
183             }
184             current_options.set_speed_up( speed );
185             return;
186         case 72: // H key
187             // status = current_options.get_hud_status();
188             // current_options.set_hud_status(!status);
189             HUD_brightkey( true );
190             return;
191         case 73: // I key
192             // Minimal Hud
193             fgHUDInit2(&current_aircraft);
194             return;
195         case 77: // M key
196             globals->inc_warp( -60 );
197             fgUpdateSkyAndLightingParams();
198             return;
199         case 80: // P key
200             current_options.toggle_panel();
201             break;
202         case 84: // T key
203             globals->inc_warp_delta( -30 );
204             fgUpdateSkyAndLightingParams();
205             return;
206         case 87: // W key
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 );
212 #  endif
213 #endif
214             return;
215         case 88: // X key
216             fov = current_options.get_fov();
217             fov *= 1.05;
218             if ( fov > FG_FOV_MAX ) {
219                 fov = FG_FOV_MAX;
220             }
221             current_options.set_fov(fov);
222             v->force_update_fov_math();
223             return;
224         case 90: // Z key
225 #ifndef FG_OLD_WEATHER
226             tmp = WeatherDatabase->getWeatherVisibility();
227             tmp /= 1.10;
228             WeatherDatabase->setWeatherVisibility( tmp );
229 #else
230             tmp = current_weather.get_visibility();   // in meters
231             tmp /= 1.10;
232             current_weather.set_visibility( tmp );
233 #endif
234             return;
235         }
236     } else {
237         FG_LOG( FG_INPUT, FG_DEBUG, "" );
238         switch (k) {
239         case 50: // numeric keypad 2
240             if ( current_autopilot->get_AltitudeEnabled() ) {
241                 current_autopilot->AltitudeAdjust( 100 );
242             } else {
243                 controls.move_elevator(-0.05);
244             }
245             return;
246         case 56: // numeric keypad 8
247             if ( current_autopilot->get_AltitudeEnabled() ) {
248                 current_autopilot->AltitudeAdjust( -100 );
249             } else {
250                 controls.move_elevator(0.05);
251             }
252             return;
253         case 49: // numeric keypad 1
254             controls.move_elevator_trim(-0.001);
255             return;
256         case 55: // numeric keypad 7
257             controls.move_elevator_trim(0.001);
258             return;
259         case 52: // numeric keypad 4
260             controls.move_aileron(-0.05);
261             return;
262         case 54: // numeric keypad 6
263             controls.move_aileron(0.05);
264             return;
265         case 48: // numeric keypad Ins
266             if ( current_autopilot->get_HeadingEnabled() ) {
267                 current_autopilot->HeadingAdjust( -1 );
268             } else {
269                 controls.move_rudder(-0.05);
270             }
271             return;
272         case 13: // numeric keypad Enter
273             if ( current_autopilot->get_HeadingEnabled() ) {
274                 current_autopilot->HeadingAdjust( 1 );
275             } else {
276                 controls.move_rudder(0.05);
277             }
278             return;
279         case 53: // numeric keypad 5
280             controls.set_aileron(0.0);
281             controls.set_elevator(0.0);
282             controls.set_rudder(0.0);
283             return;
284         case 57: // numeric keypad 9 (Pg Up)
285             if ( current_autopilot->get_AutoThrottleEnabled() ) {
286                 current_autopilot->AutoThrottleAdjust( 5 );
287             } else {
288                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
289             }
290             return;
291         case 51: // numeric keypad 3 (Pg Dn)
292             if ( current_autopilot->get_AutoThrottleEnabled() ) {
293                 current_autopilot->AutoThrottleAdjust( -5 );
294             } else {
295                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
296             }
297             return;
298         case 91: // [ key
299             controls.move_flaps(-0.34);
300             FG_LOG( FG_INPUT, FG_INFO,
301                     "Set flaps to " << controls.get_flaps() );
302             return;
303         case 93: // ] key
304             controls.move_flaps(0.34);
305             FG_LOG( FG_INPUT, FG_INFO,
306                     "Set flaps to " << controls.get_flaps() );
307             return;
308         case 97: // a key
309             speed = current_options.get_speed_up();
310             speed++;
311             current_options.set_speed_up( speed );
312             return;
313         case 98: // b key
314             int b_ret;
315             double b_set;
316             b_ret = int( controls.get_brake( 0 ) );
317             b_set = double(!b_ret);
318             controls.set_brake( FGControls::ALL_WHEELS, b_set);
319             return;
320         case 44: // , key
321             if (controls.get_brake(0) > 0.0) {
322                 controls.set_brake(0, 0.0);
323             } else {
324                 controls.set_brake(0, 1.0);
325             }
326             return;
327         case 46: // . key
328             if (controls.get_brake(1) > 0.0) {
329                 controls.set_brake(1, 0.0);
330             } else {
331                 controls.set_brake(1, 1.0);
332             }
333             return;
334         case 104: // h key
335             HUD_masterswitch( true );
336             return;
337         case 105: // i key
338             fgHUDInit(&current_aircraft);  // normal HUD
339             return;
340         case 109: // m key
341             globals->inc_warp( 60 );
342             fgUpdateSkyAndLightingParams();
343             return;
344         case 112: // p key
345             globals->set_freeze( ! globals->get_freeze() );
346
347             {
348                 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
349                             f->get_Latitude() * RAD_TO_DEG );
350                 FGPath tile_path( current_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() );
354
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
360                         );
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());
366             }
367             return;
368         case 116: // t key
369             globals->inc_warp_delta( 30 );
370             fgUpdateSkyAndLightingParams();
371             return;
372         case 118: // v key
373             current_options.cycle_view_mode();
374             return;
375         case 120: // x key
376             fov = current_options.get_fov();
377             fov /= 1.05;
378             if ( fov < FG_FOV_MIN ) {
379                 fov = FG_FOV_MIN;
380             }
381             current_options.set_fov(fov);
382             v->force_update_fov_math();
383             return;
384         case 122: // z key
385 #ifndef FG_OLD_WEATHER
386             tmp = WeatherDatabase->getWeatherVisibility();
387             tmp *= 1.10;
388             WeatherDatabase->setWeatherVisibility( tmp );
389 #else
390             tmp = current_weather.get_visibility();   // in meters
391             tmp *= 1.10;
392             current_weather.set_visibility( tmp );
393 #endif
394             return;
395         case 27: // ESC
396             // if( fg_DebugOutput ) {
397             //   fclose( fg_DebugOutput );
398             // }
399             FG_LOG( FG_INPUT, FG_ALERT, 
400                     "Program exiting normally at user request." );
401             ConfirmExitDialog();
402             return;
403         }
404     }
405 }
406
407
408 // Handle "special" keyboard events
409 void GLUTspecialkey(int k, int x, int y) {
410     FGView *v;
411
412     v = &current_view;
413
414     FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
415
416     if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
417         return;
418     }
419
420     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
421         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
422         switch (k) {
423         case GLUT_KEY_F1: {
424             ifstream input("fgfs.sav");
425             if (input.good() && fgLoadFlight(input)) {
426                 input.close();
427                 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
428             } else {
429                 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
430             }
431             return;
432         }
433         case GLUT_KEY_F2: {
434             FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
435             ofstream output("fgfs.sav");
436             if (output.good() && fgSaveFlight(output)) {
437                 output.close();
438                 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
439             } else {
440                 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
441             }
442             return;
443         }
444         case GLUT_KEY_END: // numeric keypad 1
445             v->set_goal_view_offset( FG_PI * 0.75 );
446             return;
447         case GLUT_KEY_DOWN: // numeric keypad 2
448             v->set_goal_view_offset( FG_PI );
449             return;
450         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
451             v->set_goal_view_offset( FG_PI * 1.25 );
452             return;
453         case GLUT_KEY_LEFT: // numeric keypad 4
454             v->set_goal_view_offset( FG_PI * 0.50 );
455             return;
456         case GLUT_KEY_RIGHT: // numeric keypad 6
457             v->set_goal_view_offset( FG_PI * 1.50 );
458             return;
459         case GLUT_KEY_HOME: // numeric keypad 7
460             v->set_goal_view_offset( FG_PI * 0.25 );
461             return;
462         case GLUT_KEY_UP: // numeric keypad 8
463             v->set_goal_view_offset( 0.00 );
464             return;
465         case GLUT_KEY_PAGE_UP: // numeric keypad 9
466             v->set_goal_view_offset( FG_PI * 1.75 );
467             return;
468         }
469     } else {
470         FG_LOG( FG_INPUT, FG_DEBUG, "" );
471         switch (k) {
472         case GLUT_KEY_F2: // F2 Reload Tile Cache...
473             {
474                 bool freeze;
475                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
476                 if ( !freeze ) 
477                     globals->set_freeze( true );
478                 BusyCursor(0);
479                 if ( global_tile_mgr.init() ) {
480                     // Load the local scenery data
481                     global_tile_mgr.update( 
482                         cur_fdm_state->get_Longitude() * RAD_TO_DEG,
483                         cur_fdm_state->get_Latitude() * RAD_TO_DEG );
484                 } else {
485                     FG_LOG( FG_GENERAL, FG_ALERT, 
486                             "Error in Tile Manager initialization!" );
487                     exit(-1);
488                 }
489                 BusyCursor(1);
490                 if ( !freeze )
491                    globals->set_freeze( false );
492                 return;
493             }
494         case GLUT_KEY_F3: // F3 Take a screen shot
495             fgDumpSnapShot();
496             return;
497         case GLUT_KEY_F6: // F6 toggles Autopilot target location
498             if ( current_autopilot->get_HeadingMode() !=
499                  FGAutopilot::FG_HEADING_WAYPOINT ) {
500                 current_autopilot->set_HeadingMode(
501                     FGAutopilot::FG_HEADING_WAYPOINT );
502             } else {
503                 current_autopilot->set_HeadingMode(
504                     FGAutopilot::FG_HEADING_LOCK );
505             }
506             return;
507         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
508             current_options.cycle_fog();
509         
510             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
511                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
512             } else if ( current_options.get_fog() == 
513                         fgOPTIONS::FG_FOG_FASTEST )
514             {
515                 FG_LOG( FG_INPUT, FG_INFO, 
516                         "Fog enabled, hint set to fastest" );
517             } else if ( current_options.get_fog() ==
518                         fgOPTIONS::FG_FOG_NICEST )
519             {
520                 FG_LOG( FG_INPUT, FG_INFO,
521                         "Fog enabled, hint set to nicest" );
522             }
523
524             return;
525         case GLUT_KEY_F9: // F9 toggles textures on and off...
526             FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
527             if ( current_options.get_textures() ) {
528                 current_options.set_textures( false );
529                 material_lib.set_step( 1 );
530             } else {
531                 current_options.set_textures( true );
532                 material_lib.set_step( 0 );
533             }
534             return;
535         case GLUT_KEY_F10: // F10 toggles menu on and off...
536             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
537             guiToggleMenu();
538             return;
539         case GLUT_KEY_F11: // F11 Altitude Dialog.
540             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
541             NewAltitude( NULL );
542             return;
543         case GLUT_KEY_F12: // F12 Heading Dialog...
544             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
545             NewHeading( NULL );
546             return;
547         case GLUT_KEY_UP:
548             if ( current_autopilot->get_AltitudeEnabled() ) {
549                 current_autopilot->AltitudeAdjust( -100 );
550             } else {
551                 controls.move_elevator(0.05);
552             }
553             return;
554         case GLUT_KEY_DOWN:
555             if ( current_autopilot->get_AltitudeEnabled() ) {
556                 current_autopilot->AltitudeAdjust( 100 );
557             } else {
558                 controls.move_elevator(-0.05);
559             }
560             return;
561         case GLUT_KEY_LEFT:
562             controls.move_aileron(-0.05);
563             return;
564         case GLUT_KEY_RIGHT:
565             controls.move_aileron(0.05);
566             return;
567         case GLUT_KEY_HOME: // numeric keypad 1
568             controls.move_elevator_trim(0.001);
569             return;
570         case GLUT_KEY_END: // numeric keypad 7
571             controls.move_elevator_trim(-0.001);
572             return;
573         case GLUT_KEY_INSERT: // numeric keypad Ins
574             if ( current_autopilot->get_HeadingEnabled() ) {
575                 current_autopilot->HeadingAdjust( -1 );
576             } else {
577                 controls.move_rudder(-0.05);
578             }
579             return;
580         case 13: // numeric keypad Enter
581             if ( current_autopilot->get_HeadingEnabled() ) {
582                 current_autopilot->HeadingAdjust( 1 );
583             } else {
584                 controls.move_rudder(0.05);
585             }
586             return;
587         case 53: // numeric keypad 5
588             controls.set_aileron(0.0);
589             controls.set_elevator(0.0);
590             controls.set_rudder(0.0);
591             return;
592         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
593             if ( current_autopilot->get_AutoThrottleEnabled() ) {
594                 current_autopilot->AutoThrottleAdjust( 5 );
595             } else {
596                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
597             }
598             return;
599         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
600             if ( current_autopilot->get_AutoThrottleEnabled() ) {
601                 current_autopilot->AutoThrottleAdjust( -5 );
602             } else {
603                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
604             }
605             return;
606         }
607     }
608 }
609
610