]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Tie the engine0 rpm to the property manager.
[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 <Cockpit/panel.hxx>
57 #include <Cockpit/panel_io.hxx>
58 #include <GUI/gui.h>
59 #include <Scenery/tilemgr.hxx>
60 #include <Objects/matlib.hxx>
61 #include <Time/light.hxx>
62 #include <Time/tmp.hxx>
63
64 #ifndef FG_OLD_WEATHER
65 #  include <WeatherCM/FGLocalWeatherDatabase.h>
66 #else
67 #  include <Weather/weather.hxx>
68 #endif
69
70 #include "bfi.hxx"
71 #include "globals.hxx"
72 #include "keyboard.hxx"
73 #include "options.hxx"
74 #include "save.hxx"
75 #include "views.hxx"
76
77
78 // Handle keyboard events
79 void GLUTkey(unsigned char k, int x, int y) {
80     FGInterface *f;
81     SGTime *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     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             globals->inc_warp( -60 );
199             fgUpdateSkyAndLightingParams();
200             return;
201         case 80: // P key
202             current_options.toggle_panel();
203             break;
204         case 84: // T key
205             globals->inc_warp_delta( -30 );
206             fgUpdateSkyAndLightingParams();
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 44: // , key
323             if (controls.get_brake(0) > 0.0) {
324                 controls.set_brake(0, 0.0);
325             } else {
326                 controls.set_brake(0, 1.0);
327             }
328             return;
329         case 46: // . key
330             if (controls.get_brake(1) > 0.0) {
331                 controls.set_brake(1, 0.0);
332             } else {
333                 controls.set_brake(1, 1.0);
334             }
335             return;
336         case 104: // h key
337             HUD_masterswitch( true );
338             return;
339         case 105: // i key
340             fgHUDInit(&current_aircraft);  // normal HUD
341             return;
342         case 109: // m key
343             globals->inc_warp( 60 );
344             fgUpdateSkyAndLightingParams();
345             return;
346         case 112: // p key
347             globals->set_freeze( ! globals->get_freeze() );
348
349             {
350                 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
351                             f->get_Latitude() * RAD_TO_DEG );
352                 FGPath tile_path( current_options.get_fg_root() );
353                 tile_path.append( "Scenery" );
354                 tile_path.append( p.gen_base_path() );
355                 tile_path.append( p.gen_index_str() );
356
357                 // printf position and attitude information
358                 FG_LOG( FG_INPUT, FG_INFO,
359                         "Lon = " << f->get_Longitude() * RAD_TO_DEG
360                         << "  Lat = " << f->get_Latitude() * RAD_TO_DEG
361                         << "  Altitude = " << f->get_Altitude() * FEET_TO_METER
362                         );
363                 FG_LOG( FG_INPUT, FG_INFO,
364                         "Heading = " << f->get_Psi() * RAD_TO_DEG 
365                         << "  Roll = " << f->get_Phi() * RAD_TO_DEG
366                         << "  Pitch = " << f->get_Theta() * RAD_TO_DEG );
367                 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
368             }
369             return;
370         case 116: // t key
371             globals->inc_warp_delta( 30 );
372             fgUpdateSkyAndLightingParams();
373             return;
374         case 118: // v key
375             current_options.cycle_view_mode();
376             return;
377         case 120: // x key
378             fov = current_options.get_fov();
379             fov /= 1.05;
380             if ( fov < FG_FOV_MIN ) {
381                 fov = FG_FOV_MIN;
382             }
383             current_options.set_fov(fov);
384             v->force_update_fov_math();
385             return;
386         case 122: // z key
387 #ifndef FG_OLD_WEATHER
388             tmp = WeatherDatabase->getWeatherVisibility();
389             tmp *= 1.10;
390             WeatherDatabase->setWeatherVisibility( tmp );
391 #else
392             tmp = current_weather.get_visibility();   // in meters
393             tmp *= 1.10;
394             current_weather.set_visibility( tmp );
395 #endif
396             return;
397         case 27: // ESC
398             // if( fg_DebugOutput ) {
399             //   fclose( fg_DebugOutput );
400             // }
401             FG_LOG( FG_INPUT, FG_ALERT, 
402                     "Program exiting normally at user request." );
403             ConfirmExitDialog();
404             return;
405         }
406     }
407 }
408
409
410 // Handle "special" keyboard events
411 void GLUTspecialkey(int k, int x, int y) {
412     FGView *v;
413
414     v = &current_view;
415
416     FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
417
418     if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
419         return;
420     }
421
422     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
423         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
424         switch (k) {
425         case GLUT_KEY_F1: {
426             ifstream input("fgfs.sav");
427             if (input.good() && fgLoadFlight(input)) {
428                 input.close();
429                 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
430             } else {
431                 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
432             }
433             return;
434         }
435         case GLUT_KEY_F2: {
436             FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
437             ofstream output("fgfs.sav");
438             if (output.good() && fgSaveFlight(output)) {
439                 output.close();
440                 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
441             } else {
442                 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
443             }
444             return;
445         }
446         case GLUT_KEY_F3: {
447           string panel_path =
448             current_properties.getStringValue("/sim/panel/path",
449                                               "Panels/Default/default.xml");
450           FGPanel * new_panel = fgReadPanel(panel_path);
451           if (new_panel == 0) {
452             FG_LOG(FG_INPUT, FG_ALERT,
453                    "Error reading new panel from " << panel_path);
454             return;
455           }
456           FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
457           delete current_panel;
458           current_panel = new_panel;
459           return;
460         }
461         case GLUT_KEY_F4: {
462           FGPath props_path(current_options.get_fg_root());
463           props_path.append("preferences.xml");
464           FG_LOG(FG_INPUT, FG_INFO, "Rereading global preferences");
465           if (!readPropertyList(props_path.str(), &current_properties)) {
466             FG_LOG(FG_INPUT, FG_ALERT,
467                    "Failed to reread global preferences from "
468                    << props_path.str());
469           } else {
470             FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
471           }
472           return;
473         }
474         case GLUT_KEY_END: // numeric keypad 1
475             v->set_goal_view_offset( FG_PI * 0.75 );
476             return;
477         case GLUT_KEY_DOWN: // numeric keypad 2
478             v->set_goal_view_offset( FG_PI );
479             return;
480         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
481             v->set_goal_view_offset( FG_PI * 1.25 );
482             return;
483         case GLUT_KEY_LEFT: // numeric keypad 4
484             v->set_goal_view_offset( FG_PI * 0.50 );
485             return;
486         case GLUT_KEY_RIGHT: // numeric keypad 6
487             v->set_goal_view_offset( FG_PI * 1.50 );
488             return;
489         case GLUT_KEY_HOME: // numeric keypad 7
490             v->set_goal_view_offset( FG_PI * 0.25 );
491             return;
492         case GLUT_KEY_UP: // numeric keypad 8
493             v->set_goal_view_offset( 0.00 );
494             return;
495         case GLUT_KEY_PAGE_UP: // numeric keypad 9
496             v->set_goal_view_offset( FG_PI * 1.75 );
497             return;
498         }
499     } else {
500         FG_LOG( FG_INPUT, FG_DEBUG, "" );
501         switch (k) {
502         case GLUT_KEY_F2: // F2 Reload Tile Cache...
503             {
504                 bool freeze;
505                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
506                 if ( !freeze ) 
507                     globals->set_freeze( true );
508                 BusyCursor(0);
509                 if ( global_tile_mgr.init() ) {
510                     // Load the local scenery data
511                     global_tile_mgr.update( 
512                         cur_fdm_state->get_Longitude() * RAD_TO_DEG,
513                         cur_fdm_state->get_Latitude() * RAD_TO_DEG );
514                 } else {
515                     FG_LOG( FG_GENERAL, FG_ALERT, 
516                             "Error in Tile Manager initialization!" );
517                     exit(-1);
518                 }
519                 BusyCursor(1);
520                 if ( !freeze )
521                    globals->set_freeze( false );
522                 return;
523             }
524         case GLUT_KEY_F3: // F3 Take a screen shot
525             fgDumpSnapShot();
526             return;
527         case GLUT_KEY_F6: // F6 toggles Autopilot target location
528             if ( current_autopilot->get_HeadingMode() !=
529                  FGAutopilot::FG_HEADING_WAYPOINT ) {
530                 current_autopilot->set_HeadingMode(
531                     FGAutopilot::FG_HEADING_WAYPOINT );
532             } else {
533                 current_autopilot->set_HeadingMode(
534                     FGAutopilot::FG_HEADING_LOCK );
535             }
536             return;
537         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
538             current_options.cycle_fog();
539         
540             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
541                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
542             } else if ( current_options.get_fog() == 
543                         fgOPTIONS::FG_FOG_FASTEST )
544             {
545                 FG_LOG( FG_INPUT, FG_INFO, 
546                         "Fog enabled, hint set to fastest" );
547             } else if ( current_options.get_fog() ==
548                         fgOPTIONS::FG_FOG_NICEST )
549             {
550                 FG_LOG( FG_INPUT, FG_INFO,
551                         "Fog enabled, hint set to nicest" );
552             }
553
554             return;
555         case GLUT_KEY_F9: // F9 toggles textures on and off...
556             FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
557             if ( current_options.get_textures() ) {
558                 current_options.set_textures( false );
559                 material_lib.set_step( 1 );
560             } else {
561                 current_options.set_textures( true );
562                 material_lib.set_step( 0 );
563             }
564             return;
565         case GLUT_KEY_F10: // F10 toggles menu on and off...
566             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
567             guiToggleMenu();
568             return;
569         case GLUT_KEY_F11: // F11 Altitude Dialog.
570             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
571             NewAltitude( NULL );
572             return;
573         case GLUT_KEY_F12: // F12 Heading Dialog...
574             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
575             NewHeading( NULL );
576             return;
577         case GLUT_KEY_UP:
578             if ( current_autopilot->get_AltitudeEnabled() ) {
579                 current_autopilot->AltitudeAdjust( -100 );
580             } else {
581                 controls.move_elevator(0.05);
582             }
583             return;
584         case GLUT_KEY_DOWN:
585             if ( current_autopilot->get_AltitudeEnabled() ) {
586                 current_autopilot->AltitudeAdjust( 100 );
587             } else {
588                 controls.move_elevator(-0.05);
589             }
590             return;
591         case GLUT_KEY_LEFT:
592             controls.move_aileron(-0.05);
593             return;
594         case GLUT_KEY_RIGHT:
595             controls.move_aileron(0.05);
596             return;
597         case GLUT_KEY_HOME: // numeric keypad 1
598             controls.move_elevator_trim(0.001);
599             return;
600         case GLUT_KEY_END: // numeric keypad 7
601             controls.move_elevator_trim(-0.001);
602             return;
603         case GLUT_KEY_INSERT: // numeric keypad Ins
604             if ( current_autopilot->get_HeadingEnabled() ) {
605                 current_autopilot->HeadingAdjust( -1 );
606             } else {
607                 controls.move_rudder(-0.05);
608             }
609             return;
610         case 13: // numeric keypad Enter
611             if ( current_autopilot->get_HeadingEnabled() ) {
612                 current_autopilot->HeadingAdjust( 1 );
613             } else {
614                 controls.move_rudder(0.05);
615             }
616             return;
617         case 53: // numeric keypad 5
618             controls.set_aileron(0.0);
619             controls.set_elevator(0.0);
620             controls.set_rudder(0.0);
621             return;
622         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
623             if ( current_autopilot->get_AutoThrottleEnabled() ) {
624                 current_autopilot->AutoThrottleAdjust( 5 );
625             } else {
626                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
627             }
628             return;
629         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
630             if ( current_autopilot->get_AutoThrottleEnabled() ) {
631                 current_autopilot->AutoThrottleAdjust( -5 );
632             } else {
633                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
634             }
635             return;
636         }
637     }
638 }
639
640