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