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