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
44 #if defined(FX) && defined(XMESA)
45 # include <GL/xmesa.h>
53 #include <simgear/constants.h>
54 #include <simgear/debug/logstream.hxx>
55 #include <simgear/misc/sg_path.hxx>
56 #include <simgear/screen/screen-dump.hxx>
58 #include <Include/general.hxx>
59 //#include <Include/fg_memory.h>
60 #include <Aircraft/aircraft.hxx>
61 #include <Airports/simple.hxx>
62 //#include <Autopilot/auto_gui.hxx>
63 #include <Autopilot/newauto.hxx>
64 #include <Cockpit/panel.hxx>
65 #include <Controls/controls.hxx>
66 #include <FDM/flight.hxx>
67 #include <Main/fg_init.hxx>
68 #include <Main/fg_props.hxx>
69 #include <Main/viewmgr.hxx>
72 #include "gui_local.hxx"
76 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
80 /* --------------------------------------------------------------------
82 ---------------------------------------------------------------------*/
86 static int _savedX = 0;
87 static int _savedY = 0;
88 static int last_buttons = 0 ;
89 static int mouse_active = 0;
90 static int mouse_joystick_control = 0;
92 //static time_t mouse_off_time;
93 //static int mouse_timed_out;
95 // to allow returning to previous view
96 // on second left click in MOUSE_VIEW mode
97 // This has file scope so that it can be reset
98 // if the little rodent is moved NHV
99 static int _mVtoggle = 0;
101 // we break up the glutGetModifiers return mask
102 // once per loop and stash what we need in these
103 static int glut_active_shift = 0;
104 static int glut_active_ctrl = 0;
105 static int glut_active_alt = 0;
107 static int MOUSE_XSIZE = 0;
108 static int MOUSE_YSIZE = 0;
110 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
111 // else smooth out the view panning to .01 radian per frame
112 // see view_offset smoothing mechanism in main.cxx
113 #define NO_SMOOTH_MOUSE_VIEW
115 // uncomment following to
116 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
118 /* --------------------------------------------------------------------
119 Support for mouse as control yoke (david@megginson.com)
121 - right button toggles between pointer and yoke
122 - horizontal drag with no buttons moves ailerons
123 - vertical drag with no buttons moves elevators
124 - horizontal drag with left button moves brakes (left=on)
125 - vertical drag with left button moves throttle (up=more)
126 - horizontal drag with middle button moves rudder
127 - vertical drag with middle button moves trim
129 For the *_sensitivity variables, a lower number means more sensitive.
131 TODO: figure out how to keep pointer from leaving window in yoke mode.
132 TODO: add thresholds and null zones
133 TODO: sensitivity should be configurable at user option.
134 TODO: allow differential braking (this will be useful if FlightGear
135 ever supports tail-draggers like the DC-3)
136 ---------------------------------------------------------------------*/
138 MouseMode mouse_mode = MOUSE_POINTER;
140 static double aileron_sensitivity = 1.0/500.0;
141 static double elevator_sensitivity = 1.0/500.0;
142 static double brake_sensitivity = 1.0/250.0;
143 static double throttle_sensitivity = 1.0/250.0;
144 static double rudder_sensitivity = 1.0/500.0;
145 static double trim_sensitivity = 1.0/1000.0;
147 void guiInitMouse(int width, int height)
150 MOUSE_YSIZE = height;
153 static inline int guiGetMouseButton(void)
158 static inline void guiGetMouse(int *x, int *y)
164 static inline int left_button( void ) {
165 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
168 static inline int middle_button( void ) {
169 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
172 static inline int right_button( void ) {
173 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
176 static inline void set_goal_view_offset( float offset )
178 globals->get_current_view()->setGoalHeadingOffset_deg(offset * SGD_RADIANS_TO_DEGREES);
181 static inline void set_view_offset( float offset )
183 globals->get_current_view()->setHeadingOffset_deg(offset * SGD_RADIANS_TO_DEGREES);
186 static inline void set_goal_view_tilt( float tilt )
188 globals->get_current_view()->setGoalPitchOffset_deg(tilt);
191 static inline void set_view_tilt( float tilt )
193 globals->get_current_view()->setPitchOffset_deg(tilt);
196 static inline float get_view_offset() {
197 return globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
200 static inline float get_goal_view_offset() {
201 return globals->get_current_view()->getGoalHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
204 static inline void move_brake(float offset) {
205 globals->get_controls()->move_brake(FGControls::ALL_WHEELS, offset);
208 static inline void move_throttle(float offset) {
209 globals->get_controls()->move_throttle(FGControls::ALL_ENGINES, offset);
212 static inline void move_rudder(float offset) {
213 globals->get_controls()->move_rudder(offset);
216 static inline void move_elevator_trim(float offset) {
217 globals->get_controls()->move_elevator_trim(offset);
220 static inline void move_aileron(float offset) {
221 globals->get_controls()->move_aileron(offset);
224 static inline void move_elevator(float offset) {
225 globals->get_controls()->move_elevator(offset);
228 static inline float get_aileron() {
229 return globals->get_controls()->get_aileron();
232 static inline float get_elevator() {
233 return globals->get_controls()->get_elevator();
236 static inline bool AP_HeadingEnabled() {
237 return globals->get_autopilot()->get_HeadingEnabled();
240 static inline bool AP_AltitudeEnabled() {
241 return globals->get_autopilot()->get_AltitudeEnabled();
244 void TurnCursorOn( void )
248 switch (mouse_mode) {
250 glutSetCursor(GLUT_CURSOR_INHERIT);
253 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
256 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
260 #if defined(X_CURSOR_TWEAKS)
261 glutWarpPointer( MOUSE_XSIZE/2,
266 void TurnCursorOff( void )
269 #if defined(WIN32_CURSOR_TWEAKS)
270 glutSetCursor(GLUT_CURSOR_NONE);
271 #elif defined(X_CURSOR_TWEAKS)
272 glutWarpPointer( MOUSE_XSIZE,
277 void maybeToggleMouse( void )
279 #if defined(WIN32_CURSOR_TWEAKS_OFF)
280 static int first_time = ~0;
281 static int mouse_changed = 0;
285 mouse_changed = ~mouse_changed;
289 if( mouse_mode != MOUSE_POINTER )
291 if( mouse_changed ) {
292 mouse_changed = ~mouse_changed;
298 first_time = ~first_time;
299 #endif // #ifdef WIN32
302 // Call with FALSE to init and TRUE to restore
303 void BusyCursor( int restore )
305 static GLenum cursor = (GLenum) 0;
307 glutSetCursor(cursor);
309 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
310 #if defined(WIN32_CURSOR_TWEAKS)
313 glutSetCursor( GLUT_CURSOR_WAIT );
318 // Center the view offsets
319 void CenterView( void ) {
320 if( mouse_mode == MOUSE_VIEW ) {
321 mouse_mode = MOUSE_POINTER;
322 _savedX = MOUSE_XSIZE/2;
323 _savedY = MOUSE_YSIZE/2;
326 build_rotmatrix(GuiQuat_mat, curGuiQuat);
327 glutSetCursor(GLUT_CURSOR_INHERIT);
329 // Is this necessary ??
330 if( !gui_menu_on ) TurnCursorOff();
332 glutWarpPointer( _savedX, _savedY );
334 set_goal_view_offset(0.0);
335 set_view_offset(0.0);
339 //#define TRANSLATE_HUD
340 // temporary hack until pitch_offset is added to view pipeline
341 void fgTranslateHud( void ) {
343 if(mouse_mode == MOUSE_VIEW) {
345 int ww = MOUSE_XSIZE;
346 int wh = MOUSE_YSIZE;
348 float y = 4*(_mY-(wh/2));// * ((wh/SGD_PI)*SG_RADIANS_TO_DEGREES);
350 float x = get_view_offset() * SG_RADIANS_TO_DEGREES;
352 if( x < -180 ) x += 360;
353 else if( x > 180 ) x -= 360;
359 // glTranslatef( x*ww/640, y*wh/480, 0 );
360 glTranslatef( x*640/ww, y*480/wh, 0 );
362 #endif // TRANSLATE_HUD
365 void guiMotionFunc ( int x, int y )
367 int ww, wh, need_warp = 0;
374 if (mouse_mode == MOUSE_POINTER) {
375 // TURN MENU ON IF MOUSE AT TOP
380 // TURN MENU OFF IF MOUSE AT BOTTOM
381 else if( y > wh-2 ) {
386 glutPostRedisplay () ;
388 if( x == _mX && y == _mY)
391 // reset left click MOUSE_VIEW toggle feature
394 switch (mouse_mode) {
396 if( !mouse_joystick_control ) {
397 mouse_joystick_control = 1;
398 fgSetString("/sim/control-mode", "mouse");
400 if ( left_button() ) {
401 move_brake( (_mX - x) * brake_sensitivity);
402 move_throttle((_mY - y) * throttle_sensitivity);
403 } else if ( right_button() ) {
404 if( ! AP_HeadingEnabled() ) {
405 move_rudder((x - _mX) * rudder_sensitivity);
407 if( ! AP_AltitudeEnabled() ) {
408 move_elevator_trim((_mY - y) * trim_sensitivity);
411 if( ! AP_HeadingEnabled() ) {
412 move_aileron((x - _mX) * aileron_sensitivity);
414 if( ! AP_AltitudeEnabled() ) {
415 move_elevator((_mY - y) * elevator_sensitivity);
419 // Keep the mouse in the window.
420 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
429 #define CONTRAINED_MOUSE_VIEW_Y
430 #ifdef CONTRAINED_MOUSE_VIEW_Y
434 #endif // CONTRAINED_MOUSE_VIEW_Y
436 } else if( y >= wh-1) {
437 #ifdef CONTRAINED_MOUSE_VIEW_Y
441 #endif // CONTRAINED_MOUSE_VIEW_Y
444 // wrap MOUSE_VIEW mode cursor x position
448 } else if ( x >= ww-1 ) {
452 // try to get SGD_PI movement in each half of screen
456 if( middle_button() ) {
457 trackball(lastGuiQuat,
458 (2.0f * _mX - W) / W,
459 0, //(H - 2.0f * y) / H, // 3
461 0 //(H - 2.0f * _mY) / H // 1
467 trackball(lastGuiQuat,
468 0, //(2.0f * _mX - W) / W, // 0
469 (H - 2.0f * y) / H, // 3
470 0, //(2.0f * x - W) / W, // 2
471 (H - 2.0f * _mY) / H // 1
474 add_quats(lastGuiQuat, curGuiQuat, curGuiQuat);
475 build_rotmatrix(GuiQuat_mat, curGuiQuat);
478 // this could be done in above quat
479 // but requires redoing view pipeline
480 offset = get_goal_view_offset();
481 offset += ((_mX - x) * SGD_2PI / W );
482 while (offset < 0.0) {
485 while (offset > SGD_2PI) {
488 set_goal_view_offset(offset);
489 set_goal_view_tilt(asin( GuiQuat_mat[1][2]) * SGD_RADIANS_TO_DEGREES );
490 #ifdef NO_SMOOTH_MOUSE_VIEW
491 set_view_offset(offset);
492 set_view_tilt(asin( GuiQuat_mat[1][2]) * SGD_RADIANS_TO_DEGREES );
501 glutWarpPointer(x, y);
503 // Record the new mouse position.
509 void guiMouseFunc(int button, int updown, int x, int y)
513 // private MOUSE_VIEW state variables
514 // to allow alternate left clicks in MOUSE_VIEW mode
515 // to toggle between current offsets and straight ahead
517 static int _mVx, _mVy, _Vx, _Vy;
518 static float _quat[4];
519 static double _view_offset;
521 // general purpose variables
524 glutModifiers = glutGetModifiers();
525 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
526 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
527 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
529 // Was the left button pressed?
530 if (updown == GLUT_DOWN ) {
531 if( button == GLUT_LEFT_BUTTON)
533 switch (mouse_mode) {
540 // resume previous view offsets
545 sgCopyVec4(curGuiQuat, _quat);
546 set_goal_view_offset(_view_offset);
547 set_goal_view_tilt(0.0);
548 #ifdef NO_SMOOTH_MOUSE_VIEW
549 set_view_offset(_view_offset);
557 sgCopyVec4(_quat,curGuiQuat);
561 _view_offset = get_goal_view_offset();
562 set_goal_view_offset(0.0);
563 set_goal_view_tilt(0.0);
564 #ifdef NO_SMOOTH_MOUSE_VIEW
565 set_view_offset(0.0);
569 glutWarpPointer( x , y);
570 build_rotmatrix(GuiQuat_mat, curGuiQuat);
571 _mVtoggle = ~_mVtoggle;
574 } else if ( button == GLUT_RIGHT_BUTTON) {
575 switch (mouse_mode) {
578 SG_LOG( SG_INPUT, SG_INFO, "Mouse in yoke mode" );
580 mouse_mode = MOUSE_YOKE;
581 mouse_joystick_control = 0;
584 // start with zero point in center of screen
588 // try to have the MOUSE_YOKE position
589 // reflect the current stick position
590 x = _mX - (int)(get_aileron() * aileron_sensitivity);
591 y = _mY - (int)(get_elevator() * elevator_sensitivity);
593 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
597 SG_LOG( SG_INPUT, SG_INFO, "Mouse in view mode" );
599 mouse_mode = MOUSE_VIEW;
600 fgSetString("/sim/control-mode", "joystick");
602 // recenter cursor and reset
606 // #ifndef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
608 build_rotmatrix(GuiQuat_mat, curGuiQuat);
610 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
614 SG_LOG( SG_INPUT, SG_INFO, "Mouse in pointer mode" );
616 mouse_mode = MOUSE_POINTER;
619 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
621 build_rotmatrix(GuiQuat_mat, curGuiQuat);
622 set_goal_view_offset(0.0);
623 set_goal_view_tilt(0.0);
624 #ifdef NO_SMOOTH_MOUSE_VIEW
625 set_view_offset(0.0);
627 #endif // NO_SMOOTH_MOUSE_VIEW
628 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
629 glutSetCursor(GLUT_CURSOR_INHERIT);
631 #if defined(WIN32_CURSOR_TWEAKS_OFF)
634 #endif // WIN32_CURSOR_TWEAKS_OFF
636 } // end switch (mouse_mode)
637 glutWarpPointer( x, y );
638 } // END RIGHT BUTTON
639 } // END UPDOWN == GLUT_DOWN
641 // Note which button is pressed.
642 if ( updown == GLUT_DOWN ) {
643 last_buttons |= ( 1 << button ) ;
645 last_buttons &= ~( 1 << button ) ;
648 // If we're in pointer mode, let PUI
649 // know what's going on.
650 if (mouse_mode == MOUSE_POINTER) {
651 if (!puMouse (button, updown, x,y)) {
652 if ( current_panel != NULL ) {
653 current_panel->doMouseAction(button, updown, x, y);
658 // Register the new position (if it
659 // hasn't been registered already).
663 glutPostRedisplay ();