]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Replaced Durk's mymath.* with plib/sg.h (contributed by Durk).
[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 104: // h key
328             HUD_masterswitch( true );
329             return;
330         case 105: // i key
331             fgHUDInit(&current_aircraft);  // normal HUD
332             return;
333         case 109: // m key
334             t->adjust_warp (+60);
335             local_update_sky_and_lighting_params();
336             return;
337         case 112: // p key
338             t->togglePauseMode();
339
340             {
341                 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
342                             f->get_Latitude() * RAD_TO_DEG );
343                 FGPath tile_path( current_options.get_fg_root() );
344                 tile_path.append( "Scenery" );
345                 tile_path.append( p.gen_base_path() );
346                 tile_path.append( p.gen_index_str() );
347
348                 // printf position and attitude information
349                 FG_LOG( FG_INPUT, FG_INFO,
350                         "Lon = " << f->get_Longitude() * RAD_TO_DEG
351                         << "  Lat = " << f->get_Latitude() * RAD_TO_DEG
352                         << "  Altitude = " << f->get_Altitude() * FEET_TO_METER
353                         );
354                 FG_LOG( FG_INPUT, FG_INFO,
355                         "Heading = " << f->get_Psi() * RAD_TO_DEG 
356                         << "  Roll = " << f->get_Phi() * RAD_TO_DEG
357                         << "  Pitch = " << f->get_Theta() * RAD_TO_DEG );
358                 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
359             }
360             return;
361         case 116: // t key
362             t->adjust_warp_delta (+30);
363             local_update_sky_and_lighting_params();
364             return;
365         case 118: // v key
366             current_options.cycle_view_mode();
367             return;
368         case 120: // x key
369             fov = current_options.get_fov();
370             fov /= 1.05;
371             if ( fov < FG_FOV_MIN ) {
372                 fov = FG_FOV_MIN;
373             }
374             current_options.set_fov(fov);
375             v->force_update_fov_math();
376             return;
377         case 122: // z key
378 #ifndef FG_OLD_WEATHER
379             tmp = WeatherDatabase->getWeatherVisibility();
380             tmp *= 1.10;
381             WeatherDatabase->setWeatherVisibility( tmp );
382 #else
383             tmp = current_weather.get_visibility();   // in meters
384             tmp *= 1.10;
385             current_weather.set_visibility( tmp );
386 #endif
387             return;
388         case 27: // ESC
389             // if( fg_DebugOutput ) {
390             //   fclose( fg_DebugOutput );
391             // }
392             FG_LOG( FG_INPUT, FG_ALERT, 
393                     "Program exiting normally at user request." );
394             ConfirmExitDialog();
395             return;
396         }
397     }
398 }
399
400
401 // Handle "special" keyboard events
402 void GLUTspecialkey(int k, int x, int y) {
403     FGView *v;
404
405     v = &current_view;
406
407     FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
408
409     if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
410         return;
411     }
412
413     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
414         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
415         switch (k) {
416         case GLUT_KEY_F1: {
417             ifstream input("fgfs.sav");
418             if (input.good() && fgLoadFlight(input)) {
419                 input.close();
420                 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
421             } else {
422                 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
423             }
424             return;
425         }
426         case GLUT_KEY_F2: {
427             FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
428             ofstream output("fgfs.sav");
429             if (output.good() && fgSaveFlight(output)) {
430                 output.close();
431                 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
432             } else {
433                 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
434             }
435             return;
436         }
437         case GLUT_KEY_END: // numeric keypad 1
438             v->set_goal_view_offset( FG_PI * 0.75 );
439             return;
440         case GLUT_KEY_DOWN: // numeric keypad 2
441             v->set_goal_view_offset( FG_PI );
442             return;
443         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
444             v->set_goal_view_offset( FG_PI * 1.25 );
445             return;
446         case GLUT_KEY_LEFT: // numeric keypad 4
447             v->set_goal_view_offset( FG_PI * 0.50 );
448             return;
449         case GLUT_KEY_RIGHT: // numeric keypad 6
450             v->set_goal_view_offset( FG_PI * 1.50 );
451             return;
452         case GLUT_KEY_HOME: // numeric keypad 7
453             v->set_goal_view_offset( FG_PI * 0.25 );
454             return;
455         case GLUT_KEY_UP: // numeric keypad 8
456             v->set_goal_view_offset( 0.00 );
457             return;
458         case GLUT_KEY_PAGE_UP: // numeric keypad 9
459             v->set_goal_view_offset( FG_PI * 1.75 );
460             return;
461         }
462     } else {
463         FG_LOG( FG_INPUT, FG_DEBUG, "" );
464         switch (k) {
465         case GLUT_KEY_F2: // F2 Reload Tile Cache...
466             {
467                 int toggle_pause;
468                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
469                 FGTime *t = FGTime::cur_time_params;
470                 if( (toggle_pause = !t->getPause()) )
471                     t->togglePauseMode();
472                 BusyCursor(0);
473                 if( global_tile_mgr.init() ) {
474                     // Load the local scenery data
475                     global_tile_mgr.update();
476                 } else {
477                     FG_LOG( FG_GENERAL, FG_ALERT, 
478                             "Error in Tile Manager initialization!" );
479                     exit(-1);
480                 }
481                 BusyCursor(1);
482                 if(toggle_pause)
483                     t->togglePauseMode();
484                 return;
485             }
486         case GLUT_KEY_F3: // F3 Take a screen shot
487             fgDumpSnapShot();
488             return;
489         case GLUT_KEY_F6: // F6 toggles Autopilot target location
490             if ( current_autopilot->get_HeadingMode() !=
491                  FGAutopilot::FG_HEADING_WAYPOINT ) {
492                 current_autopilot->set_HeadingMode(
493                     FGAutopilot::FG_HEADING_WAYPOINT );
494             } else {
495                 current_autopilot->set_HeadingMode(
496                     FGAutopilot::FG_HEADING_LOCK );
497             }
498             return;
499         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
500             current_options.cycle_fog();
501         
502             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
503                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
504             } else if ( current_options.get_fog() == 
505                         fgOPTIONS::FG_FOG_FASTEST )
506             {
507                 FG_LOG( FG_INPUT, FG_INFO, 
508                         "Fog enabled, hint set to fastest" );
509             } else if ( current_options.get_fog() ==
510                         fgOPTIONS::FG_FOG_NICEST )
511             {
512                 FG_LOG( FG_INPUT, FG_INFO,
513                         "Fog enabled, hint set to nicest" );
514             }
515
516             return;
517         case GLUT_KEY_F9: // F9 toggles textures on and off...
518             if ( material_mgr.loaded() ) {
519                 if (current_options.get_textures()) {
520                     current_options.set_textures(false);
521                     material_mgr.set_step(1);
522                 } else {
523                     current_options.set_textures(true);
524                     material_mgr.set_step(0);
525                 }
526                 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
527             } else {
528                 FG_LOG( FG_INPUT, FG_INFO, 
529                         "No textures loaded, cannot toggle" );
530             }
531             return;
532         case GLUT_KEY_F10: // F10 toggles menu on and off...
533             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
534             guiToggleMenu();
535             return;
536         case GLUT_KEY_F11: // F11 Altitude Dialog.
537             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
538             NewAltitude( NULL );
539             return;
540         case GLUT_KEY_F12: // F12 Heading Dialog...
541             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
542             NewHeading( NULL );
543             return;
544         case GLUT_KEY_UP:
545             if ( current_autopilot->get_AltitudeEnabled() ) {
546                 current_autopilot->AltitudeAdjust( -100 );
547             } else {
548                 controls.move_elevator(0.05);
549             }
550             return;
551         case GLUT_KEY_DOWN:
552             if ( current_autopilot->get_AltitudeEnabled() ) {
553                 current_autopilot->AltitudeAdjust( 100 );
554             } else {
555                 controls.move_elevator(-0.05);
556             }
557             return;
558         case GLUT_KEY_LEFT:
559             controls.move_aileron(-0.05);
560             return;
561         case GLUT_KEY_RIGHT:
562             controls.move_aileron(0.05);
563             return;
564         case GLUT_KEY_HOME: // numeric keypad 1
565             controls.move_elevator_trim(0.001);
566             return;
567         case GLUT_KEY_END: // numeric keypad 7
568             controls.move_elevator_trim(-0.001);
569             return;
570         case GLUT_KEY_INSERT: // numeric keypad Ins
571             if ( current_autopilot->get_HeadingEnabled() ) {
572                 current_autopilot->HeadingAdjust( -1 );
573             } else {
574                 controls.move_rudder(-0.05);
575             }
576             return;
577         case 13: // numeric keypad Enter
578             if ( current_autopilot->get_HeadingEnabled() ) {
579                 current_autopilot->HeadingAdjust( 1 );
580             } else {
581                 controls.move_rudder(0.05);
582             }
583             return;
584         case 53: // numeric keypad 5
585             controls.set_aileron(0.0);
586             controls.set_elevator(0.0);
587             controls.set_rudder(0.0);
588             return;
589         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
590             if ( current_autopilot->get_AutoThrottleEnabled() ) {
591                 current_autopilot->AutoThrottleAdjust( 5 );
592             } else {
593                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
594             }
595             return;
596         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
597             if ( current_autopilot->get_AutoThrottleEnabled() ) {
598                 current_autopilot->AutoThrottleAdjust( -5 );
599             } else {
600                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
601             }
602             return;
603         }
604     }
605 }
606
607