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