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