]> git.mxchange.org Git - flightgear.git/blob - Main/GLUTkey.cxx
Changes to event manager from Bernie Bright.
[flightgear.git] / Main / GLUTkey.cxx
1 /**************************************************************************
2  * GLUTkey.c -- handle GLUT keyboard events
3  *
4  * Written by Curtis Olson, started May 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #ifdef HAVE_WINDOWS_H
32 #  include <windows.h>                     
33 #endif
34
35 #include <GL/glut.h>
36 #include <XGL/xgl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include <Aircraft/aircraft.h>
41 #include <Astro/orbits.hxx>
42 #include <Astro/sun.hxx>
43 #include <Astro/sky.hxx>
44 #include <Autopilot/autopilot.h> // Added autopilot.h to list, Jeff Goeke-Smith
45 #include <Cockpit/hud.hxx>
46 #include <Debug/fg_debug.h>
47 #include <GUI/gui.h>
48 #include <Include/fg_constants.h>
49 #include <PUI/pu.h>
50 #include <Time/light.hxx>
51 #include <Weather/weather.h>
52
53 #include "GLUTkey.hxx"
54 #include "options.hxx"
55 #include "views.hxx"
56
57 #if defined(FX) && defined(XMESA)
58 #  include <GL/xmesa.h>
59    static int fullscreen = 1;
60 #endif
61
62
63 // Force an update of the sky and lighting parameters
64 static void local_update_sky_and_lighting_params( void ) {
65     fgSunInit();
66     cur_light_params.Update();
67     fgSkyColorsInit();
68 }
69
70
71 /* Handle keyboard events */
72 void GLUTkey(unsigned char k, int x, int y) {
73     fgCONTROLS *c;
74     fgFLIGHT *f;
75     fgTIME *t;
76     fgVIEW *v;
77     struct fgWEATHER *w;
78     float fov, tmp;
79     int status;
80
81     c = current_aircraft.controls;
82     f = current_aircraft.flight;
83     t = &cur_time_params;
84     v = &current_view;
85     w = &current_weather;
86
87     fgPrintf( FG_INPUT, FG_DEBUG, "Key hit = %d", k);
88     puKeyboard(k, PU_DOWN );
89
90     if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
91         fgPrintf( FG_INPUT, FG_DEBUG, " SHIFTED\n");
92         switch (k) {
93         case 49: /* numeric keypad 1 */
94             v->goal_view_offset = FG_PI * 0.75;
95             return;
96         case 50: /* numeric keypad 2 */
97             v->goal_view_offset = FG_PI;
98             return;
99         case 51: /* numeric keypad 3 */
100             v->goal_view_offset = FG_PI * 1.25;
101             return;
102         case 52: /* numeric keypad 4 */
103             v->goal_view_offset = FG_PI * 0.50;
104             return;
105         case 54: /* numeric keypad 6 */
106             v->goal_view_offset = FG_PI * 1.50;
107             return;
108         case 55: /* numeric keypad 7 */
109             v->goal_view_offset = FG_PI * 0.25;
110             return;
111         case 56: /* numeric keypad 8 */
112             v->goal_view_offset = 0.00;
113             return;
114         case 57: /* numeric keypad 9 */
115             v->goal_view_offset = FG_PI * 1.75;
116             return;
117         case 72: /* H key */
118             // status = current_options.get_hud_status();
119             // current_options.set_hud_status(!status);
120             HUD_brightkey( true );
121             return;
122         case 73: /* i key */
123             // Minimal Hud
124             fgHUDInit2(&current_aircraft);
125             return;
126         case 77: /* M key */
127             t->warp -= 60;
128             local_update_sky_and_lighting_params();
129             return;
130         case 84: /* T key */
131             t->warp_delta -= 30;
132             local_update_sky_and_lighting_params();
133             return;
134         case 87: /* W key */
135 #if defined(FX) && !defined(WIN32)
136             fullscreen = ( !fullscreen );
137 #if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
138             XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
139 #endif
140 #endif
141             return;
142         case 88: /* X key */
143             fov = current_options.get_fov();
144             fov *= 1.05;
145             if ( fov > FG_FOV_MAX ) {
146                 fov = FG_FOV_MAX;
147             }
148             current_options.set_fov(fov);
149             v->update_fov = TRUE;
150             return;
151         case 90: /* Z key */
152             tmp = fgWeatherGetVisibility();   /* in meters */
153             tmp /= 1.10;
154             fgWeatherSetVisibility( tmp );
155             return;
156         // autopilot additions
157         case 65: /* A key */
158                 fgAPSetMode(1);
159                 return;
160         case 83: /* S key */
161                 fgAPSetMode(0);
162                 return;
163         case 68: /* D key */
164                 fgAPSetHeading(AP_CURRENT_HEADING);
165                 return;
166         }
167     } else {
168         fgPrintf( FG_INPUT, FG_DEBUG, "\n");
169         switch (k) {
170         case 50: /* numeric keypad 2 */
171             fgElevMove(-0.05);
172             return;
173         case 56: /* numeric keypad 8 */
174             fgElevMove(0.05);
175             return;
176         case 49: /* numeric keypad 1 */
177             fgElevTrimMove(-0.001);
178             return;
179         case 55: /* numeric keypad 7 */
180             fgElevTrimMove(0.001);
181             return;
182         case 52: /* numeric keypad 4 */
183             fgAileronMove(-0.05);
184             return;
185         case 54: /* numeric keypad 6 */
186             fgAileronMove(0.05);
187             return;
188         case 48: /* numeric keypad Ins */
189             fgRudderMove(-0.05);
190             return;
191         case 13: /* numeric keypad Enter */
192             fgRudderMove(0.05);
193             return;
194         case 53: /* numeric keypad 5 */
195             fgAileronSet(0.0);
196             fgElevSet(0.0);
197             fgRudderSet(0.0);
198             return;
199         case 57: /* numeric keypad 9 (Pg Up) */
200             fgThrottleMove(0, 0.01);
201             return;
202         case 51: /* numeric keypad 3 (Pg Dn) */
203             fgThrottleMove(0, -0.01);
204             return;
205         case 104: /* h key */
206             HUD_brightkey( false );
207             return;
208         case 105: /* i key */
209             fgHUDInit(&current_aircraft);  // normal HUD
210             return;
211         case 109: /* m key */
212             t->warp += 60;
213             local_update_sky_and_lighting_params();
214             return;
215         case 112: /* p key */
216             t->pause = !t->pause;
217             // printf position and attitude information
218             fgPrintf( FG_INPUT, FG_INFO,
219                       "Lon = %.4f  Lat = %.4f  Altitude = %.1f\n", 
220                       FG_Longitude * RAD_TO_DEG,
221                       FG_Latitude * RAD_TO_DEG,
222                       FG_Altitude * FEET_TO_METER);
223             fgPrintf( FG_INPUT, FG_INFO,
224                       "Heading = %.2f  Roll = %.2f  Pitch = %.2f\n", 
225                       FG_Psi * RAD_TO_DEG,
226                       FG_Phi * RAD_TO_DEG,
227                       FG_Theta * RAD_TO_DEG);
228             return;
229         case 116: /* t key */
230             t->warp_delta += 30;
231             local_update_sky_and_lighting_params();
232             return;
233         case 120: /* X key */
234             fov = current_options.get_fov();
235             fov /= 1.05;
236             if ( fov < FG_FOV_MIN ) {
237                 fov = FG_FOV_MIN;
238             }
239             current_options.set_fov(fov);
240             v->update_fov = TRUE;
241             return;
242         case 122: /* z key */
243             tmp = fgWeatherGetVisibility();   /* in meters */
244             tmp *= 1.10;
245             fgWeatherSetVisibility( tmp );
246             return;
247         case 27: /* ESC */
248             // if( fg_DebugOutput ) {
249             //   fclose( fg_DebugOutput );
250             // }
251             fgPrintf( FG_INPUT, FG_EXIT, 
252                       "Program exiting normally at user request.\n");
253         }
254     }
255 }
256
257
258 /* Handle "special" keyboard events */
259 void GLUTspecialkey(int k, int x, int y) {
260     fgCONTROLS *c;
261     fgVIEW *v;
262
263     c = current_aircraft.controls;
264     v = &current_view;
265
266     fgPrintf( FG_INPUT, FG_DEBUG, "Special key hit = %d", k);
267     puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN);
268
269     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
270         fgPrintf( FG_INPUT, FG_DEBUG, " SHIFTED\n");
271         switch (k) {
272         case GLUT_KEY_END: /* numeric keypad 1 */
273             v->goal_view_offset = FG_PI * 0.75;
274             return;
275         case GLUT_KEY_DOWN: /* numeric keypad 2 */
276             v->goal_view_offset = FG_PI;
277             return;
278         case GLUT_KEY_PAGE_DOWN: /* numeric keypad 3 */
279             v->goal_view_offset = FG_PI * 1.25;
280             return;
281         case GLUT_KEY_LEFT: /* numeric keypad 4 */
282             v->goal_view_offset = FG_PI * 0.50;
283             return;
284         case GLUT_KEY_RIGHT: /* numeric keypad 6 */
285             v->goal_view_offset = FG_PI * 1.50;
286             return;
287         case GLUT_KEY_HOME: /* numeric keypad 7 */
288             v->goal_view_offset = FG_PI * 0.25;
289             return;
290         case GLUT_KEY_UP: /* numeric keypad 8 */
291             v->goal_view_offset = 0.00;
292             return;
293         case GLUT_KEY_PAGE_UP: /* numeric keypad 9 */
294             v->goal_view_offset = FG_PI * 1.75;
295             return;
296         }
297     } else {
298         fgPrintf( FG_INPUT, FG_DEBUG, "\n");
299         switch (k) {
300         case GLUT_KEY_F10: /* F10 toggles menu on and off... */
301             printf("Invoking call back function");
302             hideMenuButton -> 
303                 setValue ((int) !(hideMenuButton -> getValue() ) );
304             hideMenuButton -> invokeCallback();
305             //exit(1);
306             return;
307         case GLUT_KEY_UP:
308             fgElevMove(0.05);
309             return;
310         case GLUT_KEY_DOWN:
311             fgElevMove(-0.05);
312             return;
313         case GLUT_KEY_LEFT:
314             fgAileronMove(-0.05);
315             return;
316         case GLUT_KEY_RIGHT:
317             fgAileronMove(0.05);
318             return;
319         case GLUT_KEY_HOME: /* numeric keypad 1 */
320             fgElevTrimMove(0.001);
321             return;
322         case GLUT_KEY_END: /* numeric keypad 7 */
323             fgElevTrimMove(-0.001);
324             return;
325         case GLUT_KEY_INSERT: /* numeric keypad Ins */
326             fgRudderMove(-0.05);
327             return;
328         case 13: /* numeric keypad Enter */
329             fgRudderMove(0.05);
330             return;
331         case 53: /* numeric keypad 5 */
332             fgAileronSet(0.0);
333             fgElevSet(0.0);
334             fgRudderSet(0.0);
335             return;
336         case GLUT_KEY_PAGE_UP: /* numeric keypad 9 (Pg Up) */
337             fgThrottleMove(0, 0.01);
338             return;
339         case GLUT_KEY_PAGE_DOWN: /* numeric keypad 3 (Pg Dn) */
340             fgThrottleMove(0, -0.01);
341             return;
342         }
343     }
344 }
345
346
347 /* $Log$
348 /* Revision 1.21  1998/08/29 13:09:25  curt
349 /* Changes to event manager from Bernie Bright.
350 /*
351  * Revision 1.20  1998/08/24 20:11:12  curt
352  * Added i/I to toggle full vs. minimal HUD.
353  * Added a --hud-tris vs --hud-culled option.
354  * Moved options accessor funtions to options.hxx.
355  *
356  * Revision 1.19  1998/08/05 00:19:33  curt
357  * Added a local routine to update lighting params every frame when time is
358  * accelerated.
359  *
360  * Revision 1.18  1998/07/30 23:48:24  curt
361  * Output position & orientation when pausing.
362  * Eliminated libtool use.
363  * Added options to specify initial position and orientation.
364  * Changed default fov to 55 degrees.
365  * Added command line option to start in paused or unpaused state.
366  *
367  * Revision 1.17  1998/07/27 18:41:23  curt
368  * Added a pause command "p"
369  * Fixed some initialization order problems between pui and glut.
370  * Added an --enable/disable-sound option.
371  *
372  * Revision 1.16  1998/07/16 17:33:34  curt
373  * "H" / "h" now control hud brightness as well with off being one of the
374  *   states.
375  * Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
376  *   whether or not to build in the feature.
377  * Translucent menu support.
378  * HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
379  * Use fork() / wait() for playing mp3 init music in background under unix.
380  * Changed default tile diameter to 5.
381  *
382  * Revision 1.15  1998/07/13 21:01:34  curt
383  * Wrote access functions for current fgOPTIONS.
384  *
385  * Revision 1.14  1998/07/06 02:42:02  curt
386  * Added support for switching between fullscreen and window mode for
387  * Mesa/3dfx/glide.
388  *
389  * Added a basic splash screen.  Restructured the main loop and top level
390  * initialization routines to do this.
391  *
392  * Hacked in some support for playing a startup mp3 sound file while rest
393  * of sim initializes.  Currently only works in Unix using the mpg123 player.
394  * Waits for the mpg123 player to finish before initializing internal
395  * sound drivers.
396  *
397  * Revision 1.13  1998/06/27 16:54:32  curt
398  * Replaced "extern displayInstruments" with a entry in fgOPTIONS.
399  * Don't change the view port when displaying the panel.
400  *
401  * Revision 1.12  1998/06/12 14:27:26  curt
402  * Pui -> PUI, Gui -> GUI.
403  *
404  * Revision 1.11  1998/06/12 00:57:38  curt
405  * Added support for Pui/Gui.
406  * Converted fog to GL_FOG_EXP2.
407  * Link to static simulator parts.
408  * Update runfg.bat to try to be a little smarter.
409  *
410  * Revision 1.10  1998/05/27 02:24:05  curt
411  * View optimizations by Norman Vine.
412  *
413  * Revision 1.9  1998/05/16 13:05:21  curt
414  * Added limits to fov.
415  *
416  * Revision 1.8  1998/05/13 18:29:56  curt
417  * Added a keyboard binding to dynamically adjust field of view.
418  * Added a command line option to specify fov.
419  * Adjusted terrain color.
420  * Root path info moved to fgOPTIONS.
421  * Added ability to parse options out of a config file.
422  *
423  * Revision 1.7  1998/05/07 23:14:14  curt
424  * Added "D" key binding to set autopilot heading.
425  * Made frame rate calculation average out over last 10 frames.
426  * Borland C++ floating point exception workaround.
427  * Added a --tile-radius=n option.
428  *
429  * Revision 1.6  1998/04/28 01:20:20  curt
430  * Type-ified fgTIME and fgVIEW.
431  * Added a command line option to disable textures.
432  *
433  * Revision 1.5  1998/04/25 22:06:29  curt
434  * Edited cvs log messages in source files ... bad bad bad!
435  *
436  * Revision 1.4  1998/04/25 20:24:00  curt
437  * Cleaned up initialization sequence to eliminate interdependencies
438  * between sun position, lighting, and view position.  This creates a
439  * valid single pass initialization path.
440  *
441  * Revision 1.3  1998/04/24 14:19:29  curt
442  * Fog tweaks.
443  *
444  * Revision 1.2  1998/04/24 00:49:17  curt
445  * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
446  * Trying out some different option parsing code.
447  * Some code reorganization.
448  *
449  * Revision 1.1  1998/04/22 13:25:40  curt
450  * C++ - ifing the code.
451  * Starting a bit of reorganization of lighting code.
452  *
453  * Revision 1.33  1998/04/18 04:11:25  curt
454  * Moved fg_debug to it's own library, added zlib support.
455  *
456  * Revision 1.32  1998/04/14 02:21:01  curt
457  * Incorporated autopilot heading hold contributed by:  Jeff Goeke-Smith
458  * <jgoeke@voyager.net>
459  *
460  * Revision 1.31  1998/04/08 23:34:05  curt
461  * Patch from Durk to fix trim reversal with numlock key active.
462  *
463  * Revision 1.30  1998/04/03 22:09:02  curt
464  * Converting to Gnu autoconf system.
465  *
466  * Revision 1.29  1998/02/07 15:29:40  curt
467  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
468  * <chotchkiss@namg.us.anritsu.com>
469  *
470  * Revision 1.28  1998/02/03 23:20:23  curt
471  * Lots of little tweaks to fix various consistency problems discovered by
472  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
473  * passed arguments along to the real printf().  Also incorporated HUD changes
474  * by Michele America.
475  *
476  * Revision 1.27  1998/01/27 00:47:55  curt
477  * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
478  * system and commandline/config file processing code.
479  *
480  * Revision 1.26  1998/01/19 19:27:07  curt
481  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
482  * This should simplify things tremendously.
483  *
484  * Revision 1.25  1998/01/05 18:44:34  curt
485  * Add an option to advance/decrease time from keyboard.
486  *
487  * Revision 1.24  1997/12/30 16:36:46  curt
488  * Merged in Durk's changes ...
489  *
490  * Revision 1.23  1997/12/15 23:54:44  curt
491  * Add xgl wrappers for debugging.
492  * Generate terrain normals on the fly.
493  *
494  * Revision 1.22  1997/12/10 22:37:45  curt
495  * Prepended "fg" on the name of all global structures that didn't have it yet.
496  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
497  *
498  * Revision 1.21  1997/08/27 21:32:23  curt
499  * Restructured view calculation code.  Added stars.
500  *
501  * Revision 1.20  1997/08/27 03:30:13  curt
502  * Changed naming scheme of basic shared structures.
503  *
504  * Revision 1.19  1997/08/25 20:27:21  curt
505  * Merged in initial HUD and Joystick code.
506  *
507  * Revision 1.18  1997/08/22 21:34:38  curt
508  * Doing a bit of reorganizing and house cleaning.
509  *
510  * Revision 1.17  1997/07/19 22:34:02  curt
511  * Moved PI definitions to ../constants.h
512  * Moved random() stuff to ../Utils/ and renamed fg_random()
513  *
514  * Revision 1.16  1997/07/18 23:41:24  curt
515  * Tweaks for building with Cygnus Win32 compiler.
516  *
517  * Revision 1.15  1997/07/16 20:04:47  curt
518  * Minor tweaks to aid Win32 port.
519  *
520  * Revision 1.14  1997/07/12 03:50:20  curt
521  * Added an #include <Windows32/Base.h> to help compiling for Win32
522  *
523  * Revision 1.13  1997/06/25 15:39:46  curt
524  * Minor changes to compile with rsxnt/win32.
525  *
526  * Revision 1.12  1997/06/21 17:12:52  curt
527  * Capitalized subdirectory names.
528  *
529  * Revision 1.11  1997/06/18 04:10:31  curt
530  * A couple more runway tweaks ...
531  *
532  * Revision 1.10  1997/06/18 02:21:23  curt
533  * Hacked in a runway
534  *
535  * Revision 1.9  1997/06/02 03:40:06  curt
536  * A tiny bit more view tweaking.
537  *
538  * Revision 1.8  1997/06/02 03:01:38  curt
539  * Working on views (side, front, back, transitions, etc.)
540  *
541  * Revision 1.7  1997/05/31 19:16:25  curt
542  * Elevator trim added.
543  *
544  * Revision 1.6  1997/05/31 04:13:52  curt
545  * WE CAN NOW FLY!!!
546  *
547  * Continuing work on the LaRCsim flight model integration.
548  * Added some MSFS-like keyboard input handling.
549  *
550  * Revision 1.5  1997/05/30 23:26:19  curt
551  * Added elevator/aileron controls.
552  *
553  * Revision 1.4  1997/05/27 17:44:31  curt
554  * Renamed & rearranged variables and routines.   Added some initial simple
555  * timer/alarm routines so the flight model can be updated on a regular 
556  * interval.
557  *
558  * Revision 1.3  1997/05/23 15:40:25  curt
559  * Added GNU copyright headers.
560  * Fog now works!
561  *
562  * Revision 1.2  1997/05/23 00:35:12  curt
563  * Trying to get fog to work ...
564  *
565  * Revision 1.1  1997/05/21 15:57:50  curt
566  * Renamed due to added GLUT support.
567  *
568  * Revision 1.2  1997/05/19 18:22:41  curt
569  * Parameter tweaking ... starting to stub in fog support.
570  *
571  * Revision 1.1  1997/05/16 16:05:51  curt
572  * Initial revision.
573  *
574  */