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