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