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