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>
71 #if defined( WIN32 ) && !defined( __CYGWIN__ )
72 # include <simgear/screen/win32-printer.h>
73 # include <simgear/screen/GlBitmaps.h>
77 #include "gui_local.hxx"
81 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
85 /* --------------------------------------------------------------------
87 ---------------------------------------------------------------------*/
91 static int _savedX = 0;
92 static int _savedY = 0;
93 static int last_buttons = 0 ;
94 static int mouse_active = 0;
95 static int mouse_joystick_control = 0;
97 //static time_t mouse_off_time;
98 //static int mouse_timed_out;
100 // to allow returning to previous view
101 // on second left click in MOUSE_VIEW mode
102 // This has file scope so that it can be reset
103 // if the little rodent is moved NHV
104 static int _mVtoggle;
106 // we break up the glutGetModifiers return mask
107 // once per loop and stash what we need in these
108 static int glut_active_shift;
109 static int glut_active_ctrl;
110 static int glut_active_alt;
112 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
113 // else smooth out the view panning to .01 radian per frame
114 // see view_offset smoothing mechanism in main.cxx
115 #define NO_SMOOTH_MOUSE_VIEW
117 // uncomment following to
118 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
120 /* --------------------------------------------------------------------
121 Support for mouse as control yoke (david@megginson.com)
123 - right button toggles between pointer and yoke
124 - horizontal drag with no buttons moves ailerons
125 - vertical drag with no buttons moves elevators
126 - horizontal drag with left button moves brakes (left=on)
127 - vertical drag with left button moves throttle (up=more)
128 - horizontal drag with middle button moves rudder
129 - vertical drag with middle button moves trim
131 For the *_sensitivity variables, a lower number means more sensitive.
133 TODO: figure out how to keep pointer from leaving window in yoke mode.
134 TODO: add thresholds and null zones
135 TODO: sensitivity should be configurable at user option.
136 TODO: allow differential braking (this will be useful if FlightGear
137 ever supports tail-draggers like the DC-3)
138 ---------------------------------------------------------------------*/
140 MouseMode mouse_mode = MOUSE_POINTER;
142 static double aileron_sensitivity = 1.0/500.0;
143 static double elevator_sensitivity = 1.0/500.0;
144 static double brake_sensitivity = 1.0/250.0;
145 static double throttle_sensitivity = 1.0/250.0;
146 static double rudder_sensitivity = 1.0/500.0;
147 static double trim_sensitivity = 1.0/1000.0;
149 static inline int guiGetMouseButton(void)
154 static inline void guiGetMouse(int *x, int *y)
160 static inline int left_button( void ) {
161 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
164 static inline int middle_button( void ) {
165 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
168 static inline int right_button( void ) {
169 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
172 void TurnCursorOn( void )
175 #if defined(WIN32_CURSOR_TWEAKS)
176 switch (mouse_mode) {
178 glutSetCursor(GLUT_CURSOR_INHERIT);
181 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
184 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
188 #if defined(X_CURSOR_TWEAKS)
189 glutWarpPointer( fgGetInt("/sim/startup/xsize")/2,
190 fgGetInt("/sim/startup/ysize")/2);
194 void TurnCursorOff( void )
197 #if defined(WIN32_CURSOR_TWEAKS)
198 glutSetCursor(GLUT_CURSOR_NONE);
199 #elif defined(X_CURSOR_TWEAKS)
200 glutWarpPointer( fgGetInt("/sim/startup/xsize"),
201 fgGetInt("/sim/startup/ysize"));
205 void maybeToggleMouse( void )
207 #if defined(WIN32_CURSOR_TWEAKS)
208 static int first_time = ~0;
209 static int mouse_changed = 0;
213 mouse_changed = ~mouse_changed;
217 if( mouse_mode != MOUSE_POINTER )
219 if( mouse_changed ) {
220 mouse_changed = ~mouse_changed;
226 first_time = ~first_time;
227 #endif // #ifdef WIN32
230 // Call with FALSE to init and TRUE to restore
231 void BusyCursor( int restore )
233 static GLenum cursor = (GLenum) 0;
235 glutSetCursor(cursor);
237 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
238 #if defined(WIN32_CURSOR_TWEAKS)
241 glutSetCursor( GLUT_CURSOR_WAIT );
246 // Center the view offsets
247 void CenterView( void ) {
248 if( mouse_mode == MOUSE_VIEW ) {
249 mouse_mode = MOUSE_POINTER;
250 _savedX = fgGetInt("/sim/startup/xsize")/2;
251 _savedY = fgGetInt("/sim/startup/ysize")/2;
254 build_rotmatrix(GuiQuat_mat, curGuiQuat);
255 glutSetCursor(GLUT_CURSOR_INHERIT);
257 // Is this necessary ??
258 if( !gui_menu_on ) TurnCursorOff();
260 glutWarpPointer( _savedX, _savedY );
262 globals->get_current_view()->set_goal_view_offset(0.0);
263 globals->get_current_view()->set_view_offset(0.0);
267 void guiMotionFunc ( int x, int y )
269 int ww, wh, need_warp = 0;
273 if (mouse_mode == MOUSE_POINTER) {
275 glutPostRedisplay () ;
277 if( x == _mX && y == _mY)
280 // reset left click MOUSE_VIEW toggle feature
283 ww = fgGetInt("/sim/startup/xsize");
284 wh = fgGetInt("/sim/startup/ysize");
286 switch (mouse_mode) {
288 if( !mouse_joystick_control ) {
289 mouse_joystick_control = 1;
290 fgSetString("/sim/control-mode", "mouse");
292 if ( left_button() ) {
293 offset = (_mX - x) * brake_sensitivity;
294 globals->get_controls()->move_brake(FGControls::ALL_WHEELS, offset);
295 offset = (_mY - y) * throttle_sensitivity;
296 globals->get_controls()->move_throttle(FGControls::ALL_ENGINES, offset);
297 } else if ( right_button() ) {
298 if( ! current_autopilot->get_HeadingEnabled() ) {
299 offset = (x - _mX) * rudder_sensitivity;
300 globals->get_controls()->move_rudder(offset);
302 if( ! current_autopilot->get_AltitudeEnabled() ) {
303 offset = (_mY - y) * trim_sensitivity;
304 globals->get_controls()->move_elevator_trim(offset);
307 if( ! current_autopilot->get_HeadingEnabled() ) {
308 offset = (x - _mX) * aileron_sensitivity;
309 globals->get_controls()->move_aileron(offset);
311 if( ! current_autopilot->get_AltitudeEnabled() ) {
312 offset = (_mY - y) * elevator_sensitivity;
313 globals->get_controls()->move_elevator(offset);
317 // Keep the mouse in the window.
318 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
327 #define CONTRAINED_MOUSE_VIEW_Y
328 #ifdef CONTRAINED_MOUSE_VIEW_Y
332 #endif // CONTRAINED_MOUSE_VIEW_Y
334 } else if( y >= wh-1) {
335 #ifdef CONTRAINED_MOUSE_VIEW_Y
339 #endif // CONTRAINED_MOUSE_VIEW_Y
342 // wrap MOUSE_VIEW mode cursor x position
346 } else if ( x >= ww-1 ) {
350 // try to get SGD_PI movement in each half of screen
354 if( middle_button() ) {
355 trackball(lastGuiQuat,
356 (2.0f * _mX - W) / W,
357 0, //(H - 2.0f * y) / H, // 3
359 0 //(H - 2.0f * _mY) / H // 1
365 trackball(lastGuiQuat,
366 0, //(2.0f * _mX - W) / W, // 0
367 (H - 2.0f * y) / H, // 3
368 0, //(2.0f * x - W) / W, // 2
369 (H - 2.0f * _mY) / H // 1
372 add_quats(lastGuiQuat, curGuiQuat, curGuiQuat);
373 build_rotmatrix(GuiQuat_mat, curGuiQuat);
376 // this could be done in above quat
377 // but requires redoing view pipeline
378 offset = globals->get_current_view()->get_goal_view_offset();
379 offset += ((_mX - x) * SGD_2PI / W );
380 while (offset < 0.0) {
383 while (offset > SGD_2PI) {
386 globals->get_current_view()->set_goal_view_offset(offset);
387 #ifdef NO_SMOOTH_MOUSE_VIEW
388 globals->get_current_view()->set_view_offset(offset);
397 glutWarpPointer(x, y);
399 // Record the new mouse position.
405 void guiMouseFunc(int button, int updown, int x, int y)
409 // private MOUSE_VIEW state variables
410 // to allow alternate left clicks in MOUSE_VIEW mode
411 // to toggle between current offsets and straight ahead
413 static int _mVx, _mVy, _Vx, _Vy;
414 static float _quat[4];
415 static double _view_offset;
417 // general purpose variables
420 glutModifiers = glutGetModifiers();
421 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
422 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
423 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
425 // Was the left button pressed?
426 if (updown == GLUT_DOWN ) {
427 if( button == GLUT_LEFT_BUTTON)
429 switch (mouse_mode) {
436 // resume previous view offsets
441 curGuiQuat[0] = _quat[0];
442 curGuiQuat[1] = _quat[1];
443 curGuiQuat[2] = _quat[2];
444 curGuiQuat[3] = _quat[3];
445 globals->get_current_view()->set_goal_view_offset(_view_offset);
446 #ifdef NO_SMOOTH_MOUSE_VIEW
447 globals->get_current_view()->set_view_offset(_view_offset);
455 _quat[0] = curGuiQuat[0];
456 _quat[1] = curGuiQuat[1];
457 _quat[2] = curGuiQuat[2];
458 _quat[3] = curGuiQuat[3];
459 x = fgGetInt("/sim/startup/xsize")/2;
460 y = fgGetInt("/sim/startup/ysize")/2;
463 globals->get_current_view()->get_goal_view_offset();
464 globals->get_current_view()->set_goal_view_offset(0.0);
465 #ifdef NO_SMOOTH_MOUSE_VIEW
466 globals->get_current_view()->set_view_offset(0.0);
469 glutWarpPointer( x , y);
470 build_rotmatrix(GuiQuat_mat, curGuiQuat);
471 _mVtoggle = ~_mVtoggle;
474 }else if ( button == GLUT_RIGHT_BUTTON) {
475 switch (mouse_mode) {
477 mouse_mode = MOUSE_YOKE;
478 mouse_joystick_control = 0;
481 // start with zero point in center of screen
482 _mX = fgGetInt("/sim/startup/xsize")/2;
483 _mY = fgGetInt("/sim/startup/ysize")/2;
485 // try to have the MOUSE_YOKE position
486 // reflect the current stick position
487 offset = globals->get_controls()->get_aileron();
488 x = _mX - (int)(offset * aileron_sensitivity);
489 offset = globals->get_controls()->get_elevator();
490 y = _mY - (int)(offset * elevator_sensitivity);
492 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
493 SG_LOG( SG_INPUT, SG_INFO, "Mouse in yoke mode" );
497 mouse_mode = MOUSE_VIEW;
498 fgSetString("/sim/control-mode", "joystick");
499 x = fgGetInt("/sim/startup/xsize")/2;
500 y = fgGetInt("/sim/startup/ysize")/2;
503 build_rotmatrix(GuiQuat_mat, curGuiQuat);
504 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
505 SG_LOG( SG_INPUT, SG_INFO, "Mouse in view mode" );
509 mouse_mode = MOUSE_POINTER;
512 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
514 build_rotmatrix(GuiQuat_mat, curGuiQuat);
515 globals->get_current_view()->set_goal_view_offset(0.0);
516 #ifdef NO_SMOOTH_MOUSE_VIEW
517 globals->get_current_view()->set_view_offset(0.0);
519 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
520 glutSetCursor(GLUT_CURSOR_INHERIT);
525 SG_LOG( SG_INPUT, SG_INFO, "Mouse in pointer mode" );
528 glutWarpPointer( x, y );
529 } // END RIGHT BUTTON
530 } // END UPDOWN == GLUT_DOWN
532 // Note which button is pressed.
533 if ( updown == GLUT_DOWN ) {
534 last_buttons |= ( 1 << button ) ;
536 last_buttons &= ~( 1 << button ) ;
539 // If we're in pointer mode, let PUI
540 // know what's going on.
541 if (mouse_mode == MOUSE_POINTER) {
542 if (!puMouse (button, updown, x,y)) {
543 if ( current_panel != NULL ) {
544 current_panel->doMouseAction(button, updown, x, y);
549 // Register the new position (if it
550 // hasn't been registered already).
554 glutPostRedisplay ();