]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Updates to go along with SGTime tweaks in SimGear.
[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 #include <simgear/timing/sg_time.hxx>
52
53 #include <Aircraft/aircraft.hxx>
54 #include <Autopilot/auto_gui.hxx>
55 #include <Autopilot/newauto.hxx>
56 #include <Cockpit/hud.hxx>
57 #include <GUI/gui.h>
58 #include <Scenery/tilemgr.hxx>
59 #include <Objects/matlib.hxx>
60 #include <Time/light.hxx>
61 #include <Time/tmp.hxx>
62
63 #ifndef FG_OLD_WEATHER
64 #  include <WeatherCM/FGLocalWeatherDatabase.h>
65 #else
66 #  include <Weather/weather.hxx>
67 #endif
68
69 #include "bfi.hxx"
70 #include "globals.hxx"
71 #include "keyboard.hxx"
72 #include "options.hxx"
73 #include "save.hxx"
74 #include "views.hxx"
75
76
77 // Handle keyboard events
78 void GLUTkey(unsigned char k, int x, int y) {
79     FGInterface *f;
80     SGTime *t;
81     FGView *v;
82     float fov, tmp;
83     static bool winding_ccw = true;
84     int speed;
85
86     f = current_aircraft.fdm_state;
87     t = SGTime::cur_time_params;
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             current_options.toggle_pause();
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_END: // numeric keypad 1
447             v->set_goal_view_offset( FG_PI * 0.75 );
448             return;
449         case GLUT_KEY_DOWN: // numeric keypad 2
450             v->set_goal_view_offset( FG_PI );
451             return;
452         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
453             v->set_goal_view_offset( FG_PI * 1.25 );
454             return;
455         case GLUT_KEY_LEFT: // numeric keypad 4
456             v->set_goal_view_offset( FG_PI * 0.50 );
457             return;
458         case GLUT_KEY_RIGHT: // numeric keypad 6
459             v->set_goal_view_offset( FG_PI * 1.50 );
460             return;
461         case GLUT_KEY_HOME: // numeric keypad 7
462             v->set_goal_view_offset( FG_PI * 0.25 );
463             return;
464         case GLUT_KEY_UP: // numeric keypad 8
465             v->set_goal_view_offset( 0.00 );
466             return;
467         case GLUT_KEY_PAGE_UP: // numeric keypad 9
468             v->set_goal_view_offset( FG_PI * 1.75 );
469             return;
470         }
471     } else {
472         FG_LOG( FG_INPUT, FG_DEBUG, "" );
473         switch (k) {
474         case GLUT_KEY_F2: // F2 Reload Tile Cache...
475             {
476                 int toggle_pause;
477                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
478                 if( (toggle_pause = !current_options.get_pause()) )
479                     current_options.toggle_pause();
480                 BusyCursor(0);
481                 if( global_tile_mgr.init() ) {
482                     // Load the local scenery data
483                     global_tile_mgr.update( cur_fdm_state->get_Longitude(),
484                                             cur_fdm_state->get_Latitude() );
485                 } else {
486                     FG_LOG( FG_GENERAL, FG_ALERT, 
487                             "Error in Tile Manager initialization!" );
488                     exit(-1);
489                 }
490                 BusyCursor(1);
491                 if(toggle_pause)
492                     current_options.toggle_pause();
493                 return;
494             }
495         case GLUT_KEY_F3: // F3 Take a screen shot
496             fgDumpSnapShot();
497             return;
498         case GLUT_KEY_F6: // F6 toggles Autopilot target location
499             if ( current_autopilot->get_HeadingMode() !=
500                  FGAutopilot::FG_HEADING_WAYPOINT ) {
501                 current_autopilot->set_HeadingMode(
502                     FGAutopilot::FG_HEADING_WAYPOINT );
503             } else {
504                 current_autopilot->set_HeadingMode(
505                     FGAutopilot::FG_HEADING_LOCK );
506             }
507             return;
508         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
509             current_options.cycle_fog();
510         
511             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
512                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
513             } else if ( current_options.get_fog() == 
514                         fgOPTIONS::FG_FOG_FASTEST )
515             {
516                 FG_LOG( FG_INPUT, FG_INFO, 
517                         "Fog enabled, hint set to fastest" );
518             } else if ( current_options.get_fog() ==
519                         fgOPTIONS::FG_FOG_NICEST )
520             {
521                 FG_LOG( FG_INPUT, FG_INFO,
522                         "Fog enabled, hint set to nicest" );
523             }
524
525             return;
526         case GLUT_KEY_F9: // F9 toggles textures on and off...
527             FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
528             if ( current_options.get_textures() ) {
529                 current_options.set_textures( false );
530                 material_lib.set_step( 1 );
531             } else {
532                 current_options.set_textures( true );
533                 material_lib.set_step( 0 );
534             }
535             return;
536         case GLUT_KEY_F10: // F10 toggles menu on and off...
537             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
538             guiToggleMenu();
539             return;
540         case GLUT_KEY_F11: // F11 Altitude Dialog.
541             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
542             NewAltitude( NULL );
543             return;
544         case GLUT_KEY_F12: // F12 Heading Dialog...
545             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
546             NewHeading( NULL );
547             return;
548         case GLUT_KEY_UP:
549             if ( current_autopilot->get_AltitudeEnabled() ) {
550                 current_autopilot->AltitudeAdjust( -100 );
551             } else {
552                 controls.move_elevator(0.05);
553             }
554             return;
555         case GLUT_KEY_DOWN:
556             if ( current_autopilot->get_AltitudeEnabled() ) {
557                 current_autopilot->AltitudeAdjust( 100 );
558             } else {
559                 controls.move_elevator(-0.05);
560             }
561             return;
562         case GLUT_KEY_LEFT:
563             controls.move_aileron(-0.05);
564             return;
565         case GLUT_KEY_RIGHT:
566             controls.move_aileron(0.05);
567             return;
568         case GLUT_KEY_HOME: // numeric keypad 1
569             controls.move_elevator_trim(0.001);
570             return;
571         case GLUT_KEY_END: // numeric keypad 7
572             controls.move_elevator_trim(-0.001);
573             return;
574         case GLUT_KEY_INSERT: // numeric keypad Ins
575             if ( current_autopilot->get_HeadingEnabled() ) {
576                 current_autopilot->HeadingAdjust( -1 );
577             } else {
578                 controls.move_rudder(-0.05);
579             }
580             return;
581         case 13: // numeric keypad Enter
582             if ( current_autopilot->get_HeadingEnabled() ) {
583                 current_autopilot->HeadingAdjust( 1 );
584             } else {
585                 controls.move_rudder(0.05);
586             }
587             return;
588         case 53: // numeric keypad 5
589             controls.set_aileron(0.0);
590             controls.set_elevator(0.0);
591             controls.set_rudder(0.0);
592             return;
593         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
594             if ( current_autopilot->get_AutoThrottleEnabled() ) {
595                 current_autopilot->AutoThrottleAdjust( 5 );
596             } else {
597                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
598             }
599             return;
600         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
601             if ( current_autopilot->get_AutoThrottleEnabled() ) {
602                 current_autopilot->AutoThrottleAdjust( -5 );
603             } else {
604                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
605             }
606             return;
607         }
608     }
609 }
610
611