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