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
42 #include <simgear/xgl/xgl.h>
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/views.hxx>
70 //#include <Network/network.h>
71 //#include <Time/fg_time.hxx>
73 #if defined( WIN32 ) && !defined( __CYGWIN__ )
74 # include <simgear/screen/win32-printer.h>
75 # include <simgear/screen/GlBitmaps.h>
79 #include "gui_local.hxx"
83 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
87 /* --------------------------------------------------------------------
89 ---------------------------------------------------------------------*/
93 static int _savedX = 0;
94 static int _savedY = 0;
95 static int last_buttons = 0 ;
96 static int mouse_active = 0;
97 static int mouse_joystick_control = 0;
99 //static time_t mouse_off_time;
100 //static int mouse_timed_out;
102 // to allow returning to previous view
103 // on second left click in MOUSE_VIEW mode
104 // This has file scope so that it can be reset
105 // if the little rodent is moved NHV
106 static int _mVtoggle;
108 // we break up the glutGetModifiers return mask
109 // once per loop and stash what we need in these
110 static int glut_active_shift;
111 static int glut_active_ctrl;
112 static int glut_active_alt;
114 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
115 // else smooth out the view panning to .01 radian per frame
116 // see view_offset smoothing mechanism in main.cxx
117 #define NO_SMOOTH_MOUSE_VIEW
119 // uncomment following to
120 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
122 /* --------------------------------------------------------------------
123 Support for mouse as control yoke (david@megginson.com)
125 - right button toggles between pointer and yoke
126 - horizontal drag with no buttons moves ailerons
127 - vertical drag with no buttons moves elevators
128 - horizontal drag with left button moves brakes (left=on)
129 - vertical drag with left button moves throttle (up=more)
130 - horizontal drag with middle button moves rudder
131 - vertical drag with middle button moves trim
133 For the *_sensitivity variables, a lower number means more sensitive.
135 TODO: figure out how to keep pointer from leaving window in yoke mode.
136 TODO: add thresholds and null zones
137 TODO: sensitivity should be configurable at user option.
138 TODO: allow differential braking (this will be useful if FlightGear
139 ever supports tail-draggers like the DC-3)
140 ---------------------------------------------------------------------*/
142 MouseMode mouse_mode = MOUSE_POINTER;
144 static double aileron_sensitivity = 1.0/500.0;
145 static double elevator_sensitivity = 1.0/500.0;
146 static double brake_sensitivity = 1.0/250.0;
147 static double throttle_sensitivity = 1.0/250.0;
148 static double rudder_sensitivity = 1.0/500.0;
149 static double trim_sensitivity = 1.0/1000.0;
151 static inline int guiGetMouseButton(void)
156 static inline void guiGetMouse(int *x, int *y)
162 static inline int left_button( void ) {
163 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
166 static inline int middle_button( void ) {
167 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
170 static inline int right_button( void ) {
171 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
174 void TurnCursorOn( void )
177 #if defined(WIN32_CURSOR_TWEAKS)
178 switch (mouse_mode) {
180 glutSetCursor(GLUT_CURSOR_INHERIT);
183 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
186 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
190 #if defined(X_CURSOR_TWEAKS)
191 glutWarpPointer( fgGetInt("/sim/startup/xsize")/2,
192 fgGetInt("/sim/startup/ysize")/2);
196 void TurnCursorOff( void )
199 #if defined(WIN32_CURSOR_TWEAKS)
200 glutSetCursor(GLUT_CURSOR_NONE);
201 #elif defined(X_CURSOR_TWEAKS)
202 glutWarpPointer( fgGetInt("/sim/startup/xsize"),
203 fgGetInt("/sim/startup/ysize"));
207 void maybeToggleMouse( void )
209 #if defined(WIN32_CURSOR_TWEAKS)
210 static int first_time = ~0;
211 static int mouse_changed = 0;
215 mouse_changed = ~mouse_changed;
219 if( mouse_mode != MOUSE_POINTER )
221 if( mouse_changed ) {
222 mouse_changed = ~mouse_changed;
228 first_time = ~first_time;
229 #endif // #ifdef WIN32
232 // Call with FALSE to init and TRUE to restore
233 void BusyCursor( int restore )
235 static GLenum cursor = (GLenum) 0;
237 glutSetCursor(cursor);
239 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
240 #if defined(WIN32_CURSOR_TWEAKS)
243 glutSetCursor( GLUT_CURSOR_WAIT );
248 // Center the view offsets
249 void CenterView( void ) {
250 if( mouse_mode == MOUSE_VIEW ) {
251 mouse_mode = MOUSE_POINTER;
252 _savedX = fgGetInt("/sim/startup/xsize")/2;
253 _savedY = fgGetInt("/sim/startup/ysize")/2;
256 build_rotmatrix(GuiQuat_mat, curGuiQuat);
257 glutSetCursor(GLUT_CURSOR_INHERIT);
259 // Is this necessary ??
260 if( !gui_menu_on ) TurnCursorOff();
262 glutWarpPointer( _savedX, _savedY );
264 globals->get_current_view()->set_goal_view_offset(0.0);
265 globals->get_current_view()->set_view_offset(0.0);
269 void guiMotionFunc ( int x, int y )
271 int ww, wh, need_warp = 0;
275 if (mouse_mode == MOUSE_POINTER) {
277 glutPostRedisplay () ;
279 if( x == _mX && y == _mY)
282 // reset left click MOUSE_VIEW toggle feature
285 ww = fgGetInt("/sim/startup/xsize");
286 wh = fgGetInt("/sim/startup/ysize");
288 switch (mouse_mode) {
290 if( !mouse_joystick_control ) {
291 mouse_joystick_control = 1;
292 fgSetString("/sim/control-mode", "mouse");
294 if ( left_button() ) {
295 offset = (_mX - x) * brake_sensitivity;
296 controls.move_brake(FGControls::ALL_WHEELS, offset);
297 offset = (_mY - y) * throttle_sensitivity;
298 controls.move_throttle(FGControls::ALL_ENGINES, offset);
299 } else if ( right_button() ) {
300 if( ! current_autopilot->get_HeadingEnabled() ) {
301 offset = (x - _mX) * rudder_sensitivity;
302 controls.move_rudder(offset);
304 if( ! current_autopilot->get_AltitudeEnabled() ) {
305 offset = (_mY - y) * trim_sensitivity;
306 controls.move_elevator_trim(offset);
309 if( ! current_autopilot->get_HeadingEnabled() ) {
310 offset = (x - _mX) * aileron_sensitivity;
311 controls.move_aileron(offset);
313 if( ! current_autopilot->get_AltitudeEnabled() ) {
314 offset = (_mY - y) * elevator_sensitivity;
315 controls.move_elevator(offset);
319 // Keep the mouse in the window.
320 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
329 #define CONTRAINED_MOUSE_VIEW_Y
330 #ifdef CONTRAINED_MOUSE_VIEW_Y
334 #endif // CONTRAINED_MOUSE_VIEW_Y
336 } else if( y >= wh-1) {
337 #ifdef CONTRAINED_MOUSE_VIEW_Y
341 #endif // CONTRAINED_MOUSE_VIEW_Y
344 // wrap MOUSE_VIEW mode cursor x position
348 } else if ( x >= ww-1 ) {
352 // try to get SGD_PI movement in each half of screen
356 if( middle_button() ) {
357 trackball(lastGuiQuat,
358 (2.0f * _mX - W) / W,
359 0, //(H - 2.0f * y) / H, // 3
361 0 //(H - 2.0f * _mY) / H // 1
367 trackball(lastGuiQuat,
368 0, //(2.0f * _mX - W) / W, // 0
369 (H - 2.0f * y) / H, // 3
370 0, //(2.0f * x - W) / W, // 2
371 (H - 2.0f * _mY) / H // 1
374 add_quats(lastGuiQuat, curGuiQuat, curGuiQuat);
375 build_rotmatrix(GuiQuat_mat, curGuiQuat);
378 // this could be done in above quat
379 // but requires redoing view pipeline
380 offset = globals->get_current_view()->get_goal_view_offset();
381 offset += ((_mX - x) * SGD_2PI / W );
382 while (offset < 0.0) {
385 while (offset > SGD_2PI) {
388 globals->get_current_view()->set_goal_view_offset(offset);
389 #ifdef NO_SMOOTH_MOUSE_VIEW
390 globals->get_current_view()->set_view_offset(offset);
399 glutWarpPointer(x, y);
401 // Record the new mouse position.
407 void guiMouseFunc(int button, int updown, int x, int y)
411 // private MOUSE_VIEW state variables
412 // to allow alternate left clicks in MOUSE_VIEW mode
413 // to toggle between current offsets and straight ahead
415 static int _mVx, _mVy, _Vx, _Vy;
416 static float _quat[4];
417 static double _view_offset;
419 // general purpose variables
422 glutModifiers = glutGetModifiers();
423 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
424 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
425 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
427 // Was the left button pressed?
428 if (updown == GLUT_DOWN ) {
429 if( button == GLUT_LEFT_BUTTON)
431 switch (mouse_mode) {
438 // resume previous view offsets
443 curGuiQuat[0] = _quat[0];
444 curGuiQuat[1] = _quat[1];
445 curGuiQuat[2] = _quat[2];
446 curGuiQuat[3] = _quat[3];
447 globals->get_current_view()->set_goal_view_offset(_view_offset);
448 #ifdef NO_SMOOTH_MOUSE_VIEW
449 globals->get_current_view()->set_view_offset(_view_offset);
457 _quat[0] = curGuiQuat[0];
458 _quat[1] = curGuiQuat[1];
459 _quat[2] = curGuiQuat[2];
460 _quat[3] = curGuiQuat[3];
461 x = fgGetInt("/sim/startup/xsize")/2;
462 y = fgGetInt("/sim/startup/ysize")/2;
465 globals->get_current_view()->get_goal_view_offset();
466 globals->get_current_view()->set_goal_view_offset(0.0);
467 #ifdef NO_SMOOTH_MOUSE_VIEW
468 globals->get_current_view()->set_view_offset(0.0);
471 glutWarpPointer( x , y);
472 build_rotmatrix(GuiQuat_mat, curGuiQuat);
473 _mVtoggle = ~_mVtoggle;
476 }else if ( button == GLUT_RIGHT_BUTTON) {
477 switch (mouse_mode) {
479 mouse_mode = MOUSE_YOKE;
480 mouse_joystick_control = 0;
483 // start with zero point in center of screen
484 _mX = fgGetInt("/sim/startup/xsize")/2;
485 _mY = fgGetInt("/sim/startup/ysize")/2;
487 // try to have the MOUSE_YOKE position
488 // reflect the current stick position
489 offset = controls.get_aileron();
490 x = _mX - (int)(offset * aileron_sensitivity);
491 offset = controls.get_elevator();
492 y = _mY - (int)(offset * elevator_sensitivity);
494 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
495 SG_LOG( SG_INPUT, SG_INFO, "Mouse in yoke mode" );
499 mouse_mode = MOUSE_VIEW;
500 fgSetString("/sim/control-mode", "joystick");
501 x = fgGetInt("/sim/startup/xsize")/2;
502 y = fgGetInt("/sim/startup/ysize")/2;
505 build_rotmatrix(GuiQuat_mat, curGuiQuat);
506 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
507 SG_LOG( SG_INPUT, SG_INFO, "Mouse in view mode" );
511 mouse_mode = MOUSE_POINTER;
514 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
516 build_rotmatrix(GuiQuat_mat, curGuiQuat);
517 globals->get_current_view()->set_goal_view_offset(0.0);
518 #ifdef NO_SMOOTH_MOUSE_VIEW
519 globals->get_current_view()->set_view_offset(0.0);
521 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
522 glutSetCursor(GLUT_CURSOR_INHERIT);
527 SG_LOG( SG_INPUT, SG_INFO, "Mouse in pointer mode" );
530 glutWarpPointer( x, y );
531 } // END RIGHT BUTTON
532 } // END UPDOWN == GLUT_DOWN
534 // Note which button is pressed.
535 if ( updown == GLUT_DOWN ) {
536 last_buttons |= ( 1 << button ) ;
538 last_buttons &= ~( 1 << button ) ;
541 // If we're in pointer mode, let PUI
542 // know what's going on.
543 if (mouse_mode == MOUSE_POINTER) {
544 if (!puMouse (button, updown, x,y)) {
545 current_panel->doMouseAction(button, updown, x, y);
549 // Register the new position (if it
550 // hasn't been registered already).
554 glutPostRedisplay ();