1 /**************************************************************************
4 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
7 * Additional mouse supported added by David Megginson, 1999.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **************************************************************************/
31 #include <simgear/compiler.h>
33 #ifdef SG_MATH_EXCEPTION_CLASH
43 #if defined(FX) && defined(XMESA)
44 # include <GL/xmesa.h>
52 #include <simgear/constants.h>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/misc/sg_path.hxx>
55 #include <simgear/screen/screen-dump.hxx>
57 #include <Include/general.hxx>
58 //#include <Include/fg_memory.h>
59 #include <Aircraft/aircraft.hxx>
60 #include <Airports/simple.hxx>
61 //#include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/panel.hxx>
64 #include <Controls/controls.hxx>
65 #include <FDM/flight.hxx>
66 #include <Main/fg_init.hxx>
67 #include <Main/fg_props.hxx>
68 #include <Main/viewmgr.hxx>
71 #include "gui_local.hxx"
75 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
79 /* --------------------------------------------------------------------
81 ---------------------------------------------------------------------*/
85 static int _savedX = 0;
86 static int _savedY = 0;
87 static int last_buttons = 0 ;
88 static int mouse_active = 0;
89 static int mouse_joystick_control = 0;
91 //static time_t mouse_off_time;
92 //static int mouse_timed_out;
94 // to allow returning to previous view
95 // on second left click in MOUSE_VIEW mode
96 // This has file scope so that it can be reset
97 // if the little rodent is moved NHV
98 static int _mVtoggle = 0;
100 // we break up the glutGetModifiers return mask
101 // once per loop and stash what we need in these
102 static int glut_active_shift = 0;
103 static int glut_active_ctrl = 0;
104 static int glut_active_alt = 0;
106 static int MOUSE_XSIZE = 0;
107 static int MOUSE_YSIZE = 0;
109 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
110 // else smooth out the view panning to .01 radian per frame
111 // see view_offset smoothing mechanism in main.cxx
112 #define NO_SMOOTH_MOUSE_VIEW
114 // uncomment following to
115 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
117 /* --------------------------------------------------------------------
118 Support for mouse as control yoke (david@megginson.com)
120 - right button toggles between pointer and yoke
121 - horizontal drag with no buttons moves ailerons
122 - vertical drag with no buttons moves elevators
123 - horizontal drag with left button moves brakes (left=on)
124 - vertical drag with left button moves throttle (up=more)
125 - horizontal drag with middle button moves rudder
126 - vertical drag with middle button moves trim
128 For the *_sensitivity variables, a lower number means more sensitive.
130 TODO: figure out how to keep pointer from leaving window in yoke mode.
131 TODO: add thresholds and null zones
132 TODO: sensitivity should be configurable at user option.
133 TODO: allow differential braking (this will be useful if FlightGear
134 ever supports tail-draggers like the DC-3)
135 ---------------------------------------------------------------------*/
137 MouseMode mouse_mode = MOUSE_POINTER;
139 static double aileron_sensitivity = 1.0/500.0;
140 static double elevator_sensitivity = 1.0/500.0;
141 static double brake_sensitivity = 1.0/250.0;
142 static double throttle_sensitivity = 1.0/250.0;
143 static double rudder_sensitivity = 1.0/500.0;
144 static double trim_sensitivity = 1.0/1000.0;
146 void guiInitMouse(int width, int height)
149 MOUSE_YSIZE = height;
152 static inline int guiGetMouseButton(void)
157 static inline void guiGetMouse(int *x, int *y)
163 static inline int left_button( void ) {
164 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
167 static inline int middle_button( void ) {
168 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
171 static inline int right_button( void ) {
172 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
175 static inline void set_goal_view_offset( float offset )
177 globals->get_current_view()->setGoalHeadingOffset_deg(offset * SGD_RADIANS_TO_DEGREES);
180 static inline void set_view_offset( float offset )
182 globals->get_current_view()->setHeadingOffset_deg(offset * SGD_RADIANS_TO_DEGREES);
185 static inline void set_goal_view_tilt( float tilt )
187 globals->get_current_view()->setGoalPitchOffset_deg(tilt);
190 static inline void set_view_tilt( float tilt )
192 globals->get_current_view()->setPitchOffset_deg(tilt);
195 static inline float get_view_offset() {
196 return globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
199 static inline float get_goal_view_offset() {
200 return globals->get_current_view()->getGoalHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
203 static inline void move_brake(float offset) {
204 globals->get_controls()->move_brake(FGControls::ALL_WHEELS, offset);
207 static inline void move_throttle(float offset) {
208 globals->get_controls()->move_throttle(FGControls::ALL_ENGINES, offset);
211 static inline void move_rudder(float offset) {
212 globals->get_controls()->move_rudder(offset);
215 static inline void move_elevator_trim(float offset) {
216 globals->get_controls()->move_elevator_trim(offset);
219 static inline void move_aileron(float offset) {
220 globals->get_controls()->move_aileron(offset);
223 static inline void move_elevator(float offset) {
224 globals->get_controls()->move_elevator(offset);
227 static inline float get_aileron() {
228 return globals->get_controls()->get_aileron();
231 static inline float get_elevator() {
232 return globals->get_controls()->get_elevator();
235 static inline bool AP_HeadingEnabled() {
236 return globals->get_autopilot()->get_HeadingEnabled();
239 static inline bool AP_AltitudeEnabled() {
240 return globals->get_autopilot()->get_AltitudeEnabled();
243 void TurnCursorOn( void )
247 switch (mouse_mode) {
249 glutSetCursor(GLUT_CURSOR_INHERIT);
252 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
255 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
259 #if defined(X_CURSOR_TWEAKS)
260 glutWarpPointer( MOUSE_XSIZE/2,
265 void TurnCursorOff( void )
268 #if defined(WIN32_CURSOR_TWEAKS)
269 glutSetCursor(GLUT_CURSOR_NONE);
270 #elif defined(X_CURSOR_TWEAKS)
271 glutWarpPointer( MOUSE_XSIZE,
276 void maybeToggleMouse( void )
278 #if defined(WIN32_CURSOR_TWEAKS_OFF)
279 static int first_time = ~0;
280 static int mouse_changed = 0;
284 mouse_changed = ~mouse_changed;
288 if( mouse_mode != MOUSE_POINTER )
290 if( mouse_changed ) {
291 mouse_changed = ~mouse_changed;
297 first_time = ~first_time;
298 #endif // #ifdef WIN32
301 // Call with FALSE to init and TRUE to restore
302 void BusyCursor( int restore )
304 static GLenum cursor = (GLenum) 0;
306 glutSetCursor(cursor);
308 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
309 #if defined(WIN32_CURSOR_TWEAKS)
312 glutSetCursor( GLUT_CURSOR_WAIT );
317 // Center the view offsets
318 void CenterView( void ) {
319 if( mouse_mode == MOUSE_VIEW ) {
320 mouse_mode = MOUSE_POINTER;
321 _savedX = MOUSE_XSIZE/2;
322 _savedY = MOUSE_YSIZE/2;
325 build_rotmatrix(GuiQuat_mat, curGuiQuat);
326 glutSetCursor(GLUT_CURSOR_INHERIT);
328 // Is this necessary ??
329 if( !gui_menu_on ) TurnCursorOff();
331 glutWarpPointer( _savedX, _savedY );
333 set_goal_view_offset(0.0);
334 set_view_offset(0.0);
338 //#define TRANSLATE_HUD
339 // temporary hack until pitch_offset is added to view pipeline
340 void fgTranslateHud( void ) {
342 if(mouse_mode == MOUSE_VIEW) {
344 int ww = MOUSE_XSIZE;
345 int wh = MOUSE_YSIZE;
347 float y = 4*(_mY-(wh/2));// * ((wh/SGD_PI)*SG_RADIANS_TO_DEGREES);
349 float x = get_view_offset() * SG_RADIANS_TO_DEGREES;
351 if( x < -180 ) x += 360;
352 else if( x > 180 ) x -= 360;
358 // glTranslatef( x*ww/640, y*wh/480, 0 );
359 glTranslatef( x*640/ww, y*480/wh, 0 );
361 #endif // TRANSLATE_HUD
364 void guiMotionFunc ( int x, int y )
366 int ww, wh, need_warp = 0;
373 if (mouse_mode == MOUSE_POINTER) {
374 // TURN MENU ON IF MOUSE AT TOP
379 // TURN MENU OFF IF MOUSE AT BOTTOM
380 else if( y > wh-2 ) {
385 glutPostRedisplay () ;
387 if( x == _mX && y == _mY)
390 // reset left click MOUSE_VIEW toggle feature
393 switch (mouse_mode) {
395 if( !mouse_joystick_control ) {
396 mouse_joystick_control = 1;
397 fgSetString("/sim/control-mode", "mouse");
399 if ( left_button() ) {
400 move_brake( (_mX - x) * brake_sensitivity);
401 move_throttle((_mY - y) * throttle_sensitivity);
402 } else if ( right_button() ) {
403 if( ! AP_HeadingEnabled() ) {
404 move_rudder((x - _mX) * rudder_sensitivity);
406 if( ! AP_AltitudeEnabled() ) {
407 move_elevator_trim((_mY - y) * trim_sensitivity);
410 if( ! AP_HeadingEnabled() ) {
411 move_aileron((x - _mX) * aileron_sensitivity);
413 if( ! AP_AltitudeEnabled() ) {
414 move_elevator((_mY - y) * elevator_sensitivity);
418 // Keep the mouse in the window.
419 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
428 #define CONTRAINED_MOUSE_VIEW_Y
429 #ifdef CONTRAINED_MOUSE_VIEW_Y
433 #endif // CONTRAINED_MOUSE_VIEW_Y
435 } else if( y >= wh-1) {
436 #ifdef CONTRAINED_MOUSE_VIEW_Y
440 #endif // CONTRAINED_MOUSE_VIEW_Y
443 // wrap MOUSE_VIEW mode cursor x position
447 } else if ( x >= ww-1 ) {
451 // try to get SGD_PI movement in each half of screen
455 if( middle_button() ) {
456 trackball(lastGuiQuat,
457 (2.0f * _mX - W) / W,
458 0, //(H - 2.0f * y) / H, // 3
460 0 //(H - 2.0f * _mY) / H // 1
466 trackball(lastGuiQuat,
467 0, //(2.0f * _mX - W) / W, // 0
468 (H - 2.0f * y) / H, // 3
469 0, //(2.0f * x - W) / W, // 2
470 (H - 2.0f * _mY) / H // 1
473 add_quats(lastGuiQuat, curGuiQuat, curGuiQuat);
474 build_rotmatrix(GuiQuat_mat, curGuiQuat);
477 // this could be done in above quat
478 // but requires redoing view pipeline
479 offset = get_goal_view_offset();
480 offset += ((_mX - x) * SGD_2PI / W );
481 while (offset < 0.0) {
484 while (offset > SGD_2PI) {
487 set_goal_view_offset(offset);
488 set_goal_view_tilt(asin( GuiQuat_mat[1][2]) * SGD_RADIANS_TO_DEGREES );
489 #ifdef NO_SMOOTH_MOUSE_VIEW
490 set_view_offset(offset);
491 set_view_tilt(asin( GuiQuat_mat[1][2]) * SGD_RADIANS_TO_DEGREES );
500 glutWarpPointer(x, y);
502 // Record the new mouse position.
508 void guiMouseFunc(int button, int updown, int x, int y)
512 // private MOUSE_VIEW state variables
513 // to allow alternate left clicks in MOUSE_VIEW mode
514 // to toggle between current offsets and straight ahead
516 static int _mVx, _mVy, _Vx, _Vy;
517 static float _quat[4];
518 static double _view_offset;
520 glutModifiers = glutGetModifiers();
521 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
522 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
523 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
525 // Was the left button pressed?
526 if (updown == GLUT_DOWN ) {
527 if( button == GLUT_LEFT_BUTTON)
529 switch (mouse_mode) {
536 // resume previous view offsets
541 sgCopyVec4(curGuiQuat, _quat);
542 set_goal_view_offset(_view_offset);
543 set_goal_view_tilt(0.0);
544 #ifdef NO_SMOOTH_MOUSE_VIEW
545 set_view_offset(_view_offset);
553 sgCopyVec4(_quat,curGuiQuat);
557 _view_offset = get_goal_view_offset();
558 set_goal_view_offset(0.0);
559 set_goal_view_tilt(0.0);
560 #ifdef NO_SMOOTH_MOUSE_VIEW
561 set_view_offset(0.0);
565 glutWarpPointer( x , y);
566 build_rotmatrix(GuiQuat_mat, curGuiQuat);
567 _mVtoggle = ~_mVtoggle;
570 } else if ( button == GLUT_RIGHT_BUTTON) {
571 switch (mouse_mode) {
574 SG_LOG( SG_INPUT, SG_INFO, "Mouse in yoke mode" );
576 mouse_mode = MOUSE_YOKE;
577 mouse_joystick_control = 0;
580 // start with zero point in center of screen
584 // try to have the MOUSE_YOKE position
585 // reflect the current stick position
586 x = _mX - (int)(get_aileron() * aileron_sensitivity);
587 y = _mY - (int)(get_elevator() * elevator_sensitivity);
589 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
593 SG_LOG( SG_INPUT, SG_INFO, "Mouse in view mode" );
595 mouse_mode = MOUSE_VIEW;
596 fgSetString("/sim/control-mode", "joystick");
598 // recenter cursor and reset
602 // #ifndef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
604 build_rotmatrix(GuiQuat_mat, curGuiQuat);
606 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
610 SG_LOG( SG_INPUT, SG_INFO, "Mouse in pointer mode" );
612 mouse_mode = MOUSE_POINTER;
615 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
617 build_rotmatrix(GuiQuat_mat, curGuiQuat);
618 set_goal_view_offset(0.0);
619 set_goal_view_tilt(0.0);
620 #ifdef NO_SMOOTH_MOUSE_VIEW
621 set_view_offset(0.0);
623 #endif // NO_SMOOTH_MOUSE_VIEW
624 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
625 glutSetCursor(GLUT_CURSOR_INHERIT);
627 #if defined(WIN32_CURSOR_TWEAKS_OFF)
630 #endif // WIN32_CURSOR_TWEAKS_OFF
632 } // end switch (mouse_mode)
633 glutWarpPointer( x, y );
634 } // END RIGHT BUTTON
635 } // END UPDOWN == GLUT_DOWN
637 // Note which button is pressed.
638 if ( updown == GLUT_DOWN ) {
639 last_buttons |= ( 1 << button ) ;
641 last_buttons &= ~( 1 << button ) ;
644 // If we're in pointer mode, let PUI
645 // know what's going on.
646 if (mouse_mode == MOUSE_POINTER) {
647 if (!puMouse (button, updown, x,y)) {
648 if ( current_panel != NULL ) {
649 current_panel->doMouseAction(button, updown, x, y);
654 // Register the new position (if it
655 // hasn't been registered already).
659 glutPostRedisplay ();