]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Added support for parsing socket options.
[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 91: // [ key
267             controls.move_flaps(-0.34);
268             FG_LOG( FG_INPUT, FG_INFO,
269                     "Set flaps to " << controls.get_flaps() );
270             return;
271         case 93: // ] key
272             controls.move_flaps(0.34);
273             FG_LOG( FG_INPUT, FG_INFO,
274                     "Set flaps to " << controls.get_flaps() );
275             return;
276         case 97: // a key
277             speed = current_options.get_speed_up();
278             speed++;
279             current_options.set_speed_up( speed );
280             return;
281         case 98: // b key
282             int b_ret;
283             double b_set;
284             b_ret = int( controls.get_brake( 0 ) );
285             b_set = double(!b_ret);
286             controls.set_brake( FGControls::ALL_WHEELS, b_set);
287             return;
288         case 104: // h key
289             HUD_brightkey( false );
290             return;
291         case 105: // i key
292             fgHUDInit(&current_aircraft);  // normal HUD
293             return;
294         case 109: // m key
295             t->adjust_warp (+60);
296             local_update_sky_and_lighting_params();
297             return;
298         case 112: // p key
299             t->togglePauseMode();
300
301             {
302                 FGBucket p( f->get_Longitude() * RAD_TO_DEG,
303                             f->get_Latitude() * RAD_TO_DEG );
304                 FGPath tile_path( current_options.get_fg_root() );
305                 tile_path.append( "Scenery" );
306                 tile_path.append( p.gen_base_path() );
307                 tile_path.append( p.gen_index_str() );
308
309                 // printf position and attitude information
310                 FG_LOG( FG_INPUT, FG_INFO,
311                         "Lon = " << f->get_Longitude() * RAD_TO_DEG
312                         << "  Lat = " << f->get_Latitude() * RAD_TO_DEG
313                         << "  Altitude = " << f->get_Altitude() * FEET_TO_METER
314                         );
315                 FG_LOG( FG_INPUT, FG_INFO,
316                         "Heading = " << f->get_Psi() * RAD_TO_DEG 
317                         << "  Roll = " << f->get_Phi() * RAD_TO_DEG
318                         << "  Pitch = " << f->get_Theta() * RAD_TO_DEG );
319                 FG_LOG( FG_INPUT, FG_INFO, tile_path.c_str());
320             }
321             return;
322         case 116: // t key
323             t->adjust_warp_delta (+30);
324             local_update_sky_and_lighting_params();
325             return;
326         case 118: // v key
327             current_options.cycle_view_mode();
328             return;
329         case 120: // x key
330             fov = current_options.get_fov();
331             fov /= 1.05;
332             if ( fov < FG_FOV_MIN ) {
333                 fov = FG_FOV_MIN;
334             }
335             current_options.set_fov(fov);
336             v->force_update_fov_math();
337             return;
338         case 122: // z key
339 #ifndef FG_OLD_WEATHER
340             tmp = WeatherDatabase->getWeatherVisibility();
341             tmp *= 1.10;
342             WeatherDatabase->setWeatherVisibility( tmp );
343 #else
344             tmp = current_weather.get_visibility();   // in meters
345             tmp *= 1.10;
346             current_weather.set_visibility( tmp );
347 #endif
348             return;
349         case 27: // ESC
350             // if( fg_DebugOutput ) {
351             //   fclose( fg_DebugOutput );
352             // }
353             FG_LOG( FG_INPUT, FG_ALERT, 
354                     "Program exiting normally at user request." );
355             ConfirmExitDialog();
356             return;
357         }
358     }
359 }
360
361
362 // Handle "special" keyboard events
363 void GLUTspecialkey(int k, int x, int y) {
364     FGView *v;
365
366     v = &current_view;
367
368     FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
369
370     if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
371         return;
372     }
373
374     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
375         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
376         switch (k) {
377         case GLUT_KEY_END: // numeric keypad 1
378             v->set_goal_view_offset( FG_PI * 0.75 );
379             return;
380         case GLUT_KEY_DOWN: // numeric keypad 2
381             v->set_goal_view_offset( FG_PI );
382             return;
383         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
384             v->set_goal_view_offset( FG_PI * 1.25 );
385             return;
386         case GLUT_KEY_LEFT: // numeric keypad 4
387             v->set_goal_view_offset( FG_PI * 0.50 );
388             return;
389         case GLUT_KEY_RIGHT: // numeric keypad 6
390             v->set_goal_view_offset( FG_PI * 1.50 );
391             return;
392         case GLUT_KEY_HOME: // numeric keypad 7
393             v->set_goal_view_offset( FG_PI * 0.25 );
394             return;
395         case GLUT_KEY_UP: // numeric keypad 8
396             v->set_goal_view_offset( 0.00 );
397             return;
398         case GLUT_KEY_PAGE_UP: // numeric keypad 9
399             v->set_goal_view_offset( FG_PI * 1.75 );
400             return;
401         }
402     } else {
403         FG_LOG( FG_INPUT, FG_DEBUG, "" );
404         switch (k) {
405         case GLUT_KEY_F2: // F2 Reload Tile Cache...
406             {
407                 int toggle_pause;
408                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
409                 FGTime *t = FGTime::cur_time_params;
410                 if( (toggle_pause = !t->getPause()) )
411                     t->togglePauseMode();
412                 BusyCursor(0);
413                 if( global_tile_mgr.init() ) {
414                     // Load the local scenery data
415                     global_tile_mgr.update();
416                 } else {
417                     FG_LOG( FG_GENERAL, FG_ALERT, 
418                             "Error in Tile Manager initialization!" );
419                     exit(-1);
420                 }
421                 BusyCursor(1);
422                 if(toggle_pause)
423                     t->togglePauseMode();
424                 return;
425             }
426         case GLUT_KEY_F3: // F2 Take a screen shot
427             fgDumpSnapShot();
428             return;
429         case GLUT_KEY_F6: // F6 toggles Autopilot target location
430             fgAPToggleWayPoint();
431             return;
432         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
433             current_options.cycle_fog();
434         
435             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
436                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
437             } else if ( current_options.get_fog() == 
438                         fgOPTIONS::FG_FOG_FASTEST )
439             {
440                 FG_LOG( FG_INPUT, FG_INFO, 
441                         "Fog enabled, hint set to fastest" );
442             } else if ( current_options.get_fog() ==
443                         fgOPTIONS::FG_FOG_NICEST )
444             {
445                 FG_LOG( FG_INPUT, FG_INFO,
446                         "Fog enabled, hint set to nicest" );
447             }
448
449             return;
450         case GLUT_KEY_F9: // F9 toggles textures on and off...
451             if ( material_mgr.loaded() ) {
452                 if (current_options.get_textures()) {
453                     current_options.set_textures(false);
454                     material_mgr.set_step(1);
455                 } else {
456                     current_options.set_textures(true);
457                     material_mgr.set_step(0);
458                 }
459                 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
460             } else {
461                 FG_LOG( FG_INPUT, FG_INFO, 
462                         "No textures loaded, cannot toggle" );
463             }
464             return;
465         case GLUT_KEY_F10: // F10 toggles menu on and off...
466             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
467             guiToggleMenu();
468             return;
469         case GLUT_KEY_F11: // F11 Altitude Dialog.
470             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
471             NewAltitude( NULL );
472             return;
473         case GLUT_KEY_F12: // F12 Heading Dialog...
474             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
475             NewHeading( NULL );
476             return;
477         case GLUT_KEY_UP:
478             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
479                 fgAPAltitudeAdjust( -100 );
480             } else {
481                 controls.move_elevator(0.05);
482             }
483             return;
484         case GLUT_KEY_DOWN:
485             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
486                 fgAPAltitudeAdjust( 100 );
487             } else {
488                 controls.move_elevator(-0.05);
489             }
490             return;
491         case GLUT_KEY_LEFT:
492             controls.move_aileron(-0.05);
493             return;
494         case GLUT_KEY_RIGHT:
495             controls.move_aileron(0.05);
496             return;
497         case GLUT_KEY_HOME: // numeric keypad 1
498             controls.move_elevator_trim(0.001);
499             return;
500         case GLUT_KEY_END: // numeric keypad 7
501             controls.move_elevator_trim(-0.001);
502             return;
503         case GLUT_KEY_INSERT: // numeric keypad Ins
504             if( fgAPHeadingEnabled() ) {
505                 fgAPHeadingAdjust( -1 );
506             } else {
507                 controls.move_rudder(-0.05);
508             }
509             return;
510         case 13: // numeric keypad Enter
511             if( fgAPHeadingEnabled() ) {
512                 fgAPHeadingAdjust( 1 );
513             } else {
514                 controls.move_rudder(0.05);
515             }
516             return;
517         case 53: // numeric keypad 5
518             controls.set_aileron(0.0);
519             controls.set_elevator(0.0);
520             controls.set_rudder(0.0);
521             return;
522         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
523             if( fgAPAutoThrottleEnabled() ) {
524                 fgAPAutoThrottleAdjust( 5 );
525             } else {
526                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
527             }
528             return;
529         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
530             if( fgAPAutoThrottleEnabled() ) {
531                 fgAPAutoThrottleAdjust( -5 );
532             } else {
533                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
534             }
535             return;
536         }
537     }
538 }
539
540