2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Io/Mouse.cpp $
15 * Routines to read the mouse.
18 * Revision 1.6 2002/07/13 06:46:48 theoddone33
21 * Revision 1.5 2002/06/09 04:41:22 relnev
22 * added copyright header
24 * Revision 1.4 2002/06/02 04:26:34 relnev
27 * Revision 1.3 2002/05/29 06:25:13 theoddone33
28 * Keyboard input, mouse tracking now work
30 * Revision 1.2 2002/05/07 03:16:46 theoddone33
31 * The Great Newline Fix
33 * Revision 1.1.1.1 2002/05/03 03:28:09 root
37 * 4 7/15/99 9:20a Andsager
38 * FS2_DEMO initial checkin
40 * 3 6/02/99 6:18p Dave
41 * Fixed TNT lockup problems! Wheeeee!
43 * 2 10/07/98 10:53a Dave
46 * 1 10/07/98 10:49a Dave
48 * 29 6/10/98 2:52p Hoffoss
49 * Made mouse code use DI by default, but fall back on normal code if that
52 * 28 5/24/98 1:35p Hoffoss
53 * Fixed bug where mouse cursor is always recentering with a
54 * mouse_flush() call in debug version.
56 * 27 5/22/98 4:50p Hoffoss
57 * Trying to fix mouse acceleration problem..
59 * 26 5/21/98 12:26p Lawrance
60 * Fixed mouse jerk at mission start while in debug build only.
62 * 25 5/15/98 2:41p Hoffoss
63 * Made mouse default to off (for flying ship) and fixed some new pilot
66 * 24 5/08/98 4:13p Hoffoss
67 * Fixed problem with mouse pointer centering causing lost keypresses.
69 * 23 5/07/98 6:58p Hoffoss
70 * Made changes to mouse code to fix a number of problems.
72 * 22 5/05/98 8:38p Hoffoss
73 * Added sensitivity adjustment to options menu and made it save to pilot
76 * 21 5/05/98 1:03p Hoffoss
77 * Fixed initialization bug.
79 * 20 5/01/98 5:45p Hoffoss
80 * Made further improvements to the mouse code.
82 * 19 5/01/98 3:35p Hoffoss
83 * Made changes to release version of mouse code.
85 * 18 5/01/98 1:14p Hoffoss
86 * Changed mouse usage so directInput is only used for release version.
88 * 17 4/30/98 5:40p Hoffoss
89 * Added mouse as a supported control to fly the ship.
91 * 16 4/29/98 12:13a Lawrance
92 * Add function to check down count of mouse button without reseting the
93 * internal count. Added hook to reset demo trailer timer when a button
96 * 15 4/02/98 5:26p John
98 * 14 1/19/98 6:15p John
99 * Fixed all my Optimized Build compiler warnings
101 * 13 12/04/97 3:47p John
102 * Made joystick move mouse cursor
104 * 12 11/20/97 5:36p Dave
105 * Hooked in a bunch of main hall changes (including sound). Made it
106 * possible to reposition (rewind/ffwd)
107 * sound buffer pointers. Fixed animation direction change framerate
110 * 11 5/12/97 11:41a John
111 * Added range checking to mouse position
113 * 10 4/22/97 5:55p Lawrance
114 * let mouse.cpp decide if mouse has moved
116 * 9 4/22/97 12:29p John
117 * Changed mouse code so that you have to call mouse_init for the mouse
120 * 8 4/22/97 10:56a John
121 * fixed some resource leaks.
123 * 7 3/26/97 10:52a Lawrance
124 * mouse always on in menus, disappears in gameplay after 1 second
126 * 6 3/11/97 1:37p Lawrance
127 * added mouse_up_count(), changed mouse_mark() to mouse_mark_button() &
130 * 5 12/09/96 1:29p Lawrance
131 * adding 3 button support
133 * 4 12/03/96 4:19p John
134 * Added some code so that holding down the mouse buttons between menus
135 * doesn't select the next menu.
142 #include <windowsx.h>
149 #define MOUSE_MODE_DI 0
150 #define MOUSE_MODE_WIN 1
153 LOCAL int Mouse_mode = MOUSE_MODE_DI;
155 LOCAL int Mouse_mode = MOUSE_MODE_WIN;
158 LOCAL int mouse_inited = 0;
160 LOCAL int Di_mouse_inited = 0;
165 CRITICAL_SECTION mouse_lock;
167 // #define USE_DIRECTINPUT
170 int mouse_left_pressed = 0;
171 int mouse_right_pressed = 0;
172 int mouse_middle_pressed = 0;
173 int mouse_left_up = 0;
174 int mouse_right_up = 0;
175 int mouse_middle_up = 0;
180 int Mouse_sensitivity = 4;
181 int Use_mouse_to_fly = 0;
182 int Mouse_hidden = 0;
183 int Keep_mouse_centered = 0;;
187 void mouse_force_pos(int x, int y);
188 void mouse_eval_deltas_di();
190 int mouse_is_visible()
192 return !Mouse_hidden;
200 #ifdef USE_DIRECTINPUT
207 DeleteCriticalSection( &mouse_lock );
214 if ( mouse_inited ) return;
220 InitializeCriticalSection( &mouse_lock );
223 ENTER_CRITICAL_SECTION(&mouse_lock);
226 Mouse_x = gr_screen.max_w / 2;
227 Mouse_y = gr_screen.max_h / 2;
229 #ifdef USE_DIRECTINPUT
231 Mouse_mode = MOUSE_MODE_WIN;
233 Mouse_mode = MOUSE_MODE_WIN;
236 LEAVE_CRITICAL_SECTION(&mouse_lock);
238 atexit( mouse_close );
242 // ----------------------------------------------------------------------------
243 // mouse_mark_button() is called asynchronously by the OS when a mouse button
244 // goes up or down. The mouse button that is affected is passed via the
247 // parameters: flags ==> mouse button pressed/released
248 // set ==> 1 - button is pressed
249 // 0 - button is released
251 void mouse_mark_button( uint flags, int set)
253 if ( !mouse_inited ) return;
255 ENTER_CRITICAL_SECTION(&mouse_lock);
257 if ( !(mouse_flags & MOUSE_LEFT_BUTTON) ) {
259 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
260 mouse_left_pressed++;
262 ////////////////////////////
263 /// SOMETHING TERRIBLE IS ABOUT TO HAPPEN. I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
264 /// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK THE LEFT MOUSE BUTTON.
265 /// PLEASE SEE ALAN FOR MORE INFORMATION.
266 ////////////////////////////
269 extern void demo_reset_trailer_timer();
270 demo_reset_trailer_timer();
273 ////////////////////////////
274 /// IT'S OVER. SEE, IT WASN'T SO BAD RIGHT? IT'S IS VERY UGLY LOOKING, I KNOW.
275 ////////////////////////////
280 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
285 if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) ) {
287 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
288 mouse_right_pressed++;
292 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
297 if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) ) {
299 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
300 mouse_middle_pressed++;
304 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
310 mouse_flags |= flags;
312 mouse_flags &= ~flags;
315 LEAVE_CRITICAL_SECTION(&mouse_lock);
324 Mouse_dx = Mouse_dy = Mouse_dz = 0;
325 ENTER_CRITICAL_SECTION(&mouse_lock);
326 mouse_left_pressed = 0;
327 mouse_right_pressed = 0;
328 mouse_middle_pressed = 0;
330 LEAVE_CRITICAL_SECTION(&mouse_lock);
333 int mouse_down_count(int n, int reset_count)
336 if ( !mouse_inited ) return 0;
338 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
340 ENTER_CRITICAL_SECTION(&mouse_lock);
343 case MOUSE_LEFT_BUTTON:
344 tmp = mouse_left_pressed;
346 mouse_left_pressed = 0;
350 case MOUSE_RIGHT_BUTTON:
351 tmp = mouse_right_pressed;
353 mouse_right_pressed = 0;
357 case MOUSE_MIDDLE_BUTTON:
358 tmp = mouse_middle_pressed;
360 mouse_middle_pressed = 0;
365 LEAVE_CRITICAL_SECTION(&mouse_lock);
370 // mouse_up_count() returns the number of times button n has gone from down to up
371 // since the last call
373 // parameters: n - button of mouse (see #define's in mouse.h)
375 int mouse_up_count(int n)
378 if ( !mouse_inited ) return 0;
380 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
382 ENTER_CRITICAL_SECTION(&mouse_lock);
385 case MOUSE_LEFT_BUTTON:
390 case MOUSE_RIGHT_BUTTON:
391 tmp = mouse_right_up;
395 case MOUSE_MIDDLE_BUTTON:
396 tmp = mouse_middle_up;
401 Assert(0); // can't happen
405 LEAVE_CRITICAL_SECTION(&mouse_lock);
410 // returns 1 if mouse button btn is down, 0 otherwise
412 int mouse_down(int btn)
415 if ( !mouse_inited ) return 0;
417 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0;
420 ENTER_CRITICAL_SECTION(&mouse_lock);
423 if ( mouse_flags & btn )
428 LEAVE_CRITICAL_SECTION(&mouse_lock);
433 // returns the fraction of time btn has been down since last call
434 // (currently returns 1 if buttons is down, 0 otherwise)
436 float mouse_down_time(int btn)
439 if ( !mouse_inited ) return 0.0f;
441 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0.0f;
443 ENTER_CRITICAL_SECTION(&mouse_lock);
445 if ( mouse_flags & btn )
450 LEAVE_CRITICAL_SECTION(&mouse_lock);
455 void mouse_get_delta(int *dx, int *dy, int *dz)
465 // Forces the actual windows cursor to be at (x,y). This may be independent of our tracked (x,y) mouse pos.
466 void mouse_force_pos(int x, int y)
468 if (os_foreground()) { // only mess with windows's mouse if we are in control of it
476 ClientToScreen((HWND) os_get_window(), &pnt);
477 SetCursorPos(pnt.x, pnt.y);
482 #include "gamesequence.h"
484 // change in mouse position since last call
485 void mouse_eval_deltas()
487 static int old_x = 0;
488 static int old_y = 0;
489 int tmp_x, tmp_y, cx, cy;
491 Mouse_dx = Mouse_dy = Mouse_dz = 0;
495 if (Mouse_mode == MOUSE_MODE_DI) {
496 mouse_eval_deltas_di();
500 cx = gr_screen.max_w / 2;
501 cy = gr_screen.max_h / 2;
503 ENTER_CRITICAL_SECTION(&mouse_lock);
506 SDL_GetMouseState (&tmp_x, &tmp_y);
510 ScreenToClient((HWND)os_get_window(), &pnt);
515 Mouse_dx = tmp_x - old_x;
516 Mouse_dy = tmp_y - old_y;
519 if (Keep_mouse_centered && Mouse_hidden) {
520 if (Mouse_dx || Mouse_dy)
521 mouse_force_pos(cx, cy);
531 LEAVE_CRITICAL_SECTION(&mouse_lock);
537 static LPDIRECTINPUT Di_mouse_obj = NULL;
538 static LPDIRECTINPUTDEVICE Di_mouse = NULL;
541 void mouse_eval_deltas_di()
548 DIMOUSESTATE mouse_state;
550 Mouse_dx = Mouse_dy = Mouse_dz = 0;
551 if (!Di_mouse_inited)
555 memset(&mouse_state, 0, sizeof(mouse_state));
559 hr = Di_mouse->GetDeviceState(sizeof(mouse_state), &mouse_state);
560 if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED)) {
561 // DirectInput is telling us that the input stream has
562 // been interrupted. We aren't tracking any state
563 // between polls, so we don't have any special reset
564 // that needs to be done. We just re-acquire and
566 Sleep(500); // Pause a half second...
567 hr = Di_mouse->Acquire();
574 Mouse_dx = (int) mouse_state.lX;
575 Mouse_dy = (int) mouse_state.lY;
576 Mouse_dz = (int) mouse_state.lZ;
579 Mouse_dx = Mouse_dy = Mouse_dz = 0;
591 if (Mouse_x >= gr_screen.max_w)
592 Mouse_x = gr_screen.max_w - 1;
594 if (Mouse_y >= gr_screen.max_h)
595 Mouse_y = gr_screen.max_h - 1;
597 // keep the mouse inside our window so we don't switch applications or anything (debug bug people reported?)
598 // JH: Dang! This makes the mouse readings in DirectInput act screwy!
599 // mouse_force_pos(gr_screen.max_w / 2, gr_screen.max_h / 2);
603 int mouse_get_pos(int *xpos, int *ypos)
607 if (Mouse_mode == MOUSE_MODE_DI) {
623 flags = SDL_GetMouseState (&Mouse_x, &Mouse_y);
628 ScreenToClient((HWND)os_get_window(), &pnt);
630 // EnterCriticalSection(&mouse_lock);
637 // LeaveCriticalSection(&mouse_lock);
647 if (Mouse_x >= gr_screen.max_w){
648 Mouse_x = gr_screen.max_w - 1;
651 if (Mouse_y >= gr_screen.max_h){
652 Mouse_y = gr_screen.max_h - 1;
666 void mouse_get_real_pos(int *mx, int *my)
668 if (Mouse_mode == MOUSE_MODE_DI) {
675 SDL_GetMouseState (mx, my);
679 ScreenToClient((HWND)os_get_window(), &pnt);
686 void mouse_set_pos(int xpos, int ypos)
688 if (Mouse_mode == MOUSE_MODE_DI) {
694 if ((xpos != Mouse_x) || (ypos != Mouse_y)){
695 mouse_force_pos(xpos, ypos);
707 if (Mouse_mode == MOUSE_MODE_WIN){
712 hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &Di_mouse_obj, NULL);
714 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_mouse_obj, NULL);
716 mprintf(( "DirectInputCreate() failed!\n" ));
721 hr = Di_mouse_obj->CreateDevice(GUID_SysMouse, &Di_mouse, NULL);
723 mprintf(( "CreateDevice() failed!\n" ));
727 hr = Di_mouse->SetDataFormat(&c_dfDIMouse);
729 mprintf(( "SetDataFormat() failed!\n" ));
733 hr = Di_mouse->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
735 mprintf(( "SetCooperativeLevel() failed!\n" ));
742 hdr.diph.dwSize = sizeof(DIPROPDWORD);
743 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
745 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
746 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
748 hr = Di_mouse->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
750 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
754 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
755 Assert(Di_event != NULL);
757 hr = Di_mouse->SetEventNotification(Di_event);
759 mprintf(( "SetEventNotification failed\n" ));
775 // Destroy any lingering IDirectInputDevice object.
777 // Unacquire the device one last time just in case we got really confused
778 // and tried to exit while the device is still acquired.
779 Di_mouse->Unacquire();
785 // Destroy any lingering IDirectInput object.
787 Di_mouse_obj->Release();