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