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