]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Borland C++ tweaks.
[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 <Scenery/tilemgr.hxx>
53 #include <Objects/materialmgr.hxx>
54 #include <Time/fg_time.hxx>
55 #include <Time/light.hxx>
56 #include <Weather/weather.hxx>
57
58 #include "keyboard.hxx"
59 #include "options.hxx"
60 #include "views.hxx"
61
62 extern void NewAltitude( puObject *cb );
63 extern void NewHeading( puObject *cb );
64
65 // Force an update of the sky and lighting parameters
66 static void local_update_sky_and_lighting_params( void ) {
67     // fgSunInit();
68     SolarSystem::theSolarSystem->rebuild();
69     cur_light_params.Update();
70     fgSkyColorsInit();
71 }
72
73
74 // Handle keyboard events
75 void GLUTkey(unsigned char k, int x, int y) {
76     FGInterface *f;
77     FGTime *t;
78     FGView *v;
79     FGWeather *w;
80     float fov, tmp;
81     static bool winding_ccw = true;
82
83     f = current_aircraft.fdm_state;
84     t = FGTime::cur_time_params;
85     v = &current_view;
86     w = &current_weather;
87
88     FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
89     if ( puKeyboard(k, PU_DOWN) ) {
90         return;
91     }
92
93     if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
94         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
95         switch (k) {
96         case 1: // Ctrl-A key
97             fgAPToggleAltitude();
98             return;
99         case 8: // Ctrl-H key
100             fgAPToggleHeading();
101             return;
102         case 18: // Ctrl-R key
103             // temporary
104             winding_ccw = !winding_ccw;
105             if ( winding_ccw ) {
106                 glFrontFace ( GL_CCW );
107             } else {
108                 glFrontFace ( GL_CW );
109             }
110             return;
111         case 19: // Ctrl-S key
112             fgAPToggleAutoThrottle();
113             return;
114         case 20: // Ctrl-T key
115             fgAPToggleTerrainFollow();
116             return;
117         case 49: // numeric keypad 1
118             v->set_goal_view_offset( FG_PI * 0.75 );
119             return;
120         case 50: // numeric keypad 2
121             v->set_goal_view_offset( FG_PI );
122             return;
123         case 51: // numeric keypad 3
124             v->set_goal_view_offset( FG_PI * 1.25 );
125             return;
126         case 52: // numeric keypad 4
127             v->set_goal_view_offset( FG_PI * 0.50 );
128             return;
129         case 54: // numeric keypad 6
130             v->set_goal_view_offset( FG_PI * 1.50 );
131             return;
132         case 55: // numeric keypad 7
133             v->set_goal_view_offset( FG_PI * 0.25 );
134             return;
135         case 56: // numeric keypad 8
136             v->set_goal_view_offset( 0.00 );
137             return;
138         case 57: // numeric keypad 9
139             v->set_goal_view_offset( FG_PI * 1.75 );
140             return;
141         case 72: // H key
142             // status = current_options.get_hud_status();
143             // current_options.set_hud_status(!status);
144             HUD_brightkey( true );
145             return;
146         case 73: // I key
147             // Minimal Hud
148             fgHUDInit2(&current_aircraft);
149             return;
150         case 77: // M key
151             t->adjust_warp(-60);
152             local_update_sky_and_lighting_params();
153             return;
154         case 80: // P key
155             current_options.toggle_panel();
156             break;
157         case 84: // T key
158             t->adjust_warp_delta(-30);
159             local_update_sky_and_lighting_params();
160             return;
161         case 87: // W key
162 #if defined(FX) && !defined(WIN32)
163             global_fullscreen = ( !global_fullscreen );
164 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
165             XMesaSetFXmode( global_fullscreen ? 
166                             XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
167 #  endif
168 #endif
169             return;
170         case 88: // X key
171             fov = current_options.get_fov();
172             fov *= 1.05;
173             if ( fov > FG_FOV_MAX ) {
174                 fov = FG_FOV_MAX;
175             }
176             current_options.set_fov(fov);
177             v->force_update_fov_math();
178             return;
179         case 90: // Z key
180             tmp = w->get_visibility();   // in meters
181             tmp /= 1.10;
182             w->set_visibility( tmp );
183             return;
184         }
185     } else {
186         FG_LOG( FG_INPUT, FG_DEBUG, "" );
187         switch (k) {
188         case 50: // numeric keypad 2
189             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
190                 fgAPAltitudeAdjust( 100 );
191             } else {
192                 controls.move_elevator(-0.05);
193             }
194             return;
195         case 56: // numeric keypad 8
196             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
197                 fgAPAltitudeAdjust( -100 );
198             } else {
199                 controls.move_elevator(0.05);
200             }
201             return;
202         case 49: // numeric keypad 1
203             controls.move_elevator_trim(-0.001);
204             return;
205         case 55: // numeric keypad 7
206             controls.move_elevator_trim(0.001);
207             return;
208         case 52: // numeric keypad 4
209             controls.move_aileron(-0.05);
210             return;
211         case 54: // numeric keypad 6
212             controls.move_aileron(0.05);
213             return;
214         case 48: // numeric keypad Ins
215             if( fgAPHeadingEnabled() ) {
216                 fgAPHeadingAdjust( -1 );
217             } else {
218                 controls.move_rudder(-0.05);
219             }
220             return;
221         case 13: // numeric keypad Enter
222             if( fgAPHeadingEnabled() ) {
223                 fgAPHeadingAdjust( 1 );
224             } else {
225                 controls.move_rudder(0.05);
226             }
227             return;
228         case 53: // numeric keypad 5
229             controls.set_aileron(0.0);
230             controls.set_elevator(0.0);
231             controls.set_rudder(0.0);
232             return;
233         case 57: // numeric keypad 9 (Pg Up)
234             if( fgAPAutoThrottleEnabled() ) {
235                 fgAPAutoThrottleAdjust( 5 );
236             } else {
237                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
238             }
239             return;
240         case 51: // numeric keypad 3 (Pg Dn)
241             if( fgAPAutoThrottleEnabled() ) {
242                 fgAPAutoThrottleAdjust( -5 );
243             } else {
244                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
245             }
246             return;
247         case 98: // b key
248             int b_ret;
249             double b_set;
250             b_ret = int( controls.get_brake( 0 ) );
251             b_set = double(!b_ret);
252             controls.set_brake( FGControls::ALL_WHEELS, b_set);
253             return;
254         case 104: // h key
255             HUD_brightkey( false );
256             return;
257         case 105: // i key
258             fgHUDInit(&current_aircraft);  // normal HUD
259             return;
260         case 109: // m key
261             t->adjust_warp (+60);
262             local_update_sky_and_lighting_params();
263             return;
264         case 112: // p key
265             t->togglePauseMode();
266             // printf position and attitude information
267             FG_LOG( FG_INPUT, FG_INFO,
268                     "Lon = " << f->get_Longitude() * RAD_TO_DEG
269                     << "  Lat = " << f->get_Latitude() * RAD_TO_DEG
270                     << "  Altitude = " << f->get_Altitude() * FEET_TO_METER );
271             FG_LOG( FG_INPUT, FG_INFO,
272                     "Heading = " << f->get_Psi() * RAD_TO_DEG 
273                     << "  Roll = " << f->get_Phi() * RAD_TO_DEG
274                     << "  Pitch = " << f->get_Theta() * RAD_TO_DEG );
275             return;
276         case 116: // t key
277             t->adjust_warp_delta (+30);
278             local_update_sky_and_lighting_params();
279             return;
280         case 120: // X key
281             fov = current_options.get_fov();
282             fov /= 1.05;
283             if ( fov < FG_FOV_MIN ) {
284                 fov = FG_FOV_MIN;
285             }
286             current_options.set_fov(fov);
287             v->force_update_fov_math();
288             return;
289         case 122: // z key
290             tmp = w->get_visibility();   // in meters
291             tmp *= 1.10;
292             w->set_visibility( tmp );
293             return;
294         case 27: // ESC
295             // if( fg_DebugOutput ) {
296             //   fclose( fg_DebugOutput );
297             // }
298             FG_LOG( FG_INPUT, FG_ALERT, 
299                     "Program exiting normally at user request." );
300             ConfirmExitDialog();
301             return;
302         }
303     }
304 }
305
306
307 // Handle "special" keyboard events
308 void GLUTspecialkey(int k, int x, int y) {
309     FGView *v;
310
311     v = &current_view;
312
313     FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
314
315     if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
316         return;
317     }
318
319     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
320         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
321         switch (k) {
322         case GLUT_KEY_END: // numeric keypad 1
323             v->set_goal_view_offset( FG_PI * 0.75 );
324             return;
325         case GLUT_KEY_DOWN: // numeric keypad 2
326             v->set_goal_view_offset( FG_PI );
327             return;
328         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
329             v->set_goal_view_offset( FG_PI * 1.25 );
330             return;
331         case GLUT_KEY_LEFT: // numeric keypad 4
332             v->set_goal_view_offset( FG_PI * 0.50 );
333             return;
334         case GLUT_KEY_RIGHT: // numeric keypad 6
335             v->set_goal_view_offset( FG_PI * 1.50 );
336             return;
337         case GLUT_KEY_HOME: // numeric keypad 7
338             v->set_goal_view_offset( FG_PI * 0.25 );
339             return;
340         case GLUT_KEY_UP: // numeric keypad 8
341             v->set_goal_view_offset( 0.00 );
342             return;
343         case GLUT_KEY_PAGE_UP: // numeric keypad 9
344             v->set_goal_view_offset( FG_PI * 1.75 );
345             return;
346         }
347     } else {
348         FG_LOG( FG_INPUT, FG_DEBUG, "" );
349         switch (k) {
350         case GLUT_KEY_F2: // F2 Reload Tile Cache...
351             {
352                 int toggle_pause;
353                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
354                 FGTime *t = FGTime::cur_time_params;
355                 if( (toggle_pause = !t->getPause()) )
356                     t->togglePauseMode();
357                 BusyCursor(0);
358                 if( global_tile_mgr.init() ) {
359                     // Load the local scenery data
360                     global_tile_mgr.update();
361                 } else {
362                     FG_LOG( FG_GENERAL, FG_ALERT, 
363                             "Error in Tile Manager initialization!" );
364                     exit(-1);
365                 }
366                 BusyCursor(1);
367                 if(toggle_pause)
368                     t->togglePauseMode();
369                 return;
370             }
371         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
372             current_options.cycle_fog();
373         
374             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
375                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
376             } else if ( current_options.get_fog() == 
377                         fgOPTIONS::FG_FOG_FASTEST )
378             {
379                 FG_LOG( FG_INPUT, FG_INFO, 
380                         "Fog enabled, hint set to fastest" );
381             } else if ( current_options.get_fog() ==
382                         fgOPTIONS::FG_FOG_NICEST )
383             {
384                 FG_LOG( FG_INPUT, FG_INFO,
385                         "Fog enabled, hint set to nicest" );
386             }
387
388             return;
389         case GLUT_KEY_F9: // F9 toggles textures on and off...
390             if ( material_mgr.loaded() ) {
391                 current_options.get_textures() ?
392                     current_options.set_textures(false) :
393                     current_options.set_textures(true);
394                 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
395             } else {
396                 FG_LOG( FG_INPUT, FG_INFO, 
397                         "No textures loaded, cannot toggle" );
398             }
399             return;
400         case GLUT_KEY_F10: // F10 toggles menu on and off...
401             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
402             guiToggleMenu();
403             return;
404         case GLUT_KEY_F11: // F11 Altitude Dialog.
405             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
406             NewAltitude( NULL );
407             return;
408         case GLUT_KEY_F12: // F12 Heading Dialog...
409             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
410             NewHeading( NULL );
411             return;
412         case GLUT_KEY_UP:
413             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
414                 fgAPAltitudeAdjust( -100 );
415             } else {
416                 controls.move_elevator(0.05);
417             }
418             return;
419         case GLUT_KEY_DOWN:
420             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
421                 fgAPAltitudeAdjust( 100 );
422             } else {
423                 controls.move_elevator(-0.05);
424             }
425             return;
426         case GLUT_KEY_LEFT:
427             controls.move_aileron(-0.05);
428             return;
429         case GLUT_KEY_RIGHT:
430             controls.move_aileron(0.05);
431             return;
432         case GLUT_KEY_HOME: // numeric keypad 1
433             controls.move_elevator_trim(0.001);
434             return;
435         case GLUT_KEY_END: // numeric keypad 7
436             controls.move_elevator_trim(-0.001);
437             return;
438         case GLUT_KEY_INSERT: // numeric keypad Ins
439             if( fgAPHeadingEnabled() ) {
440                 fgAPHeadingAdjust( -1 );
441             } else {
442                 controls.move_rudder(-0.05);
443             }
444             return;
445         case 13: // numeric keypad Enter
446             if( fgAPHeadingEnabled() ) {
447                 fgAPHeadingAdjust( 1 );
448             } else {
449                 controls.move_rudder(0.05);
450             }
451             return;
452         case 53: // numeric keypad 5
453             controls.set_aileron(0.0);
454             controls.set_elevator(0.0);
455             controls.set_rudder(0.0);
456             return;
457         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
458             if( fgAPAutoThrottleEnabled() ) {
459                 fgAPAutoThrottleAdjust( 5 );
460             } else {
461                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
462             }
463             return;
464         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
465             if( fgAPAutoThrottleEnabled() ) {
466                 fgAPAutoThrottleAdjust( -5 );
467             } else {
468                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
469             }
470             return;
471         }
472     }
473 }
474
475