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