2 * $Logfile: /Freespace2/code/Io/Mouse.cpp $
7 * Routines to read the mouse.
10 * Revision 1.2 2002/05/07 03:16:46 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:09 root
17 * 4 7/15/99 9:20a Andsager
18 * FS2_DEMO initial checkin
20 * 3 6/02/99 6:18p Dave
21 * Fixed TNT lockup problems! Wheeeee!
23 * 2 10/07/98 10:53a Dave
26 * 1 10/07/98 10:49a Dave
28 * 29 6/10/98 2:52p Hoffoss
29 * Made mouse code use DI by default, but fall back on normal code if that
32 * 28 5/24/98 1:35p Hoffoss
33 * Fixed bug where mouse cursor is always recentering with a
34 * mouse_flush() call in debug version.
36 * 27 5/22/98 4:50p Hoffoss
37 * Trying to fix mouse acceleration problem..
39 * 26 5/21/98 12:26p Lawrance
40 * Fixed mouse jerk at mission start while in debug build only.
42 * 25 5/15/98 2:41p Hoffoss
43 * Made mouse default to off (for flying ship) and fixed some new pilot
46 * 24 5/08/98 4:13p Hoffoss
47 * Fixed problem with mouse pointer centering causing lost keypresses.
49 * 23 5/07/98 6:58p Hoffoss
50 * Made changes to mouse code to fix a number of problems.
52 * 22 5/05/98 8:38p Hoffoss
53 * Added sensitivity adjustment to options menu and made it save to pilot
56 * 21 5/05/98 1:03p Hoffoss
57 * Fixed initialization bug.
59 * 20 5/01/98 5:45p Hoffoss
60 * Made further improvements to the mouse code.
62 * 19 5/01/98 3:35p Hoffoss
63 * Made changes to release version of mouse code.
65 * 18 5/01/98 1:14p Hoffoss
66 * Changed mouse usage so directInput is only used for release version.
68 * 17 4/30/98 5:40p Hoffoss
69 * Added mouse as a supported control to fly the ship.
71 * 16 4/29/98 12:13a Lawrance
72 * Add function to check down count of mouse button without reseting the
73 * internal count. Added hook to reset demo trailer timer when a button
76 * 15 4/02/98 5:26p John
78 * 14 1/19/98 6:15p John
79 * Fixed all my Optimized Build compiler warnings
81 * 13 12/04/97 3:47p John
82 * Made joystick move mouse cursor
84 * 12 11/20/97 5:36p Dave
85 * Hooked in a bunch of main hall changes (including sound). Made it
86 * possible to reposition (rewind/ffwd)
87 * sound buffer pointers. Fixed animation direction change framerate
90 * 11 5/12/97 11:41a John
91 * Added range checking to mouse position
93 * 10 4/22/97 5:55p Lawrance
94 * let mouse.cpp decide if mouse has moved
96 * 9 4/22/97 12:29p John
97 * Changed mouse code so that you have to call mouse_init for the mouse
100 * 8 4/22/97 10:56a John
101 * fixed some resource leaks.
103 * 7 3/26/97 10:52a Lawrance
104 * mouse always on in menus, disappears in gameplay after 1 second
106 * 6 3/11/97 1:37p Lawrance
107 * added mouse_up_count(), changed mouse_mark() to mouse_mark_button() &
110 * 5 12/09/96 1:29p Lawrance
111 * adding 3 button support
113 * 4 12/03/96 4:19p John
114 * Added some code so that holding down the mouse buttons between menus
115 * doesn't select the next menu.
122 #include <windowsx.h>
129 #define MOUSE_MODE_DI 0
130 #define MOUSE_MODE_WIN 1
133 LOCAL int Mouse_mode = MOUSE_MODE_DI;
135 LOCAL int Mouse_mode = MOUSE_MODE_WIN;
138 LOCAL int mouse_inited = 0;
139 LOCAL int Di_mouse_inited = 0;
143 CRITICAL_SECTION mouse_lock;
145 // #define USE_DIRECTINPUT
148 int mouse_left_pressed = 0;
149 int mouse_right_pressed = 0;
150 int mouse_middle_pressed = 0;
151 int mouse_left_up = 0;
152 int mouse_right_up = 0;
153 int mouse_middle_up = 0;
158 int Mouse_sensitivity = 4;
159 int Use_mouse_to_fly = 0;
160 int Mouse_hidden = 0;
161 int Keep_mouse_centered = 0;;
165 void mouse_force_pos(int x, int y);
166 void mouse_eval_deltas_di();
168 int mouse_is_visible()
170 return !Mouse_hidden;
178 #ifdef USE_DIRECTINPUT
185 DeleteCriticalSection( &mouse_lock );
192 if ( mouse_inited ) return;
198 InitializeCriticalSection( &mouse_lock );
201 ENTER_CRITICAL_SECTION(&mouse_lock);
204 Mouse_x = gr_screen.max_w / 2;
205 Mouse_y = gr_screen.max_h / 2;
207 #ifdef USE_DIRECTINPUT
209 Mouse_mode = MOUSE_MODE_WIN;
211 Mouse_mode = MOUSE_MODE_WIN;
214 LEAVE_CRITICAL_SECTION(&mouse_lock);
216 atexit( mouse_close );
220 // ----------------------------------------------------------------------------
221 // mouse_mark_button() is called asynchronously by the OS when a mouse button
222 // goes up or down. The mouse button that is affected is passed via the
225 // parameters: flags ==> mouse button pressed/released
226 // set ==> 1 - button is pressed
227 // 0 - button is released
229 void mouse_mark_button( uint flags, int set)
231 if ( !mouse_inited ) return;
233 ENTER_CRITICAL_SECTION(&mouse_lock);
235 if ( !(mouse_flags & MOUSE_LEFT_BUTTON) ) {
237 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
238 mouse_left_pressed++;
240 ////////////////////////////
241 /// SOMETHING TERRIBLE IS ABOUT TO HAPPEN. I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
242 /// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK THE LEFT MOUSE BUTTON.
243 /// PLEASE SEE ALAN FOR MORE INFORMATION.
244 ////////////////////////////
247 extern void demo_reset_trailer_timer();
248 demo_reset_trailer_timer();
251 ////////////////////////////
252 /// IT'S OVER. SEE, IT WASN'T SO BAD RIGHT? IT'S IS VERY UGLY LOOKING, I KNOW.
253 ////////////////////////////
258 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
263 if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) ) {
265 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
266 mouse_right_pressed++;
270 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
275 if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) ) {
277 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
278 mouse_middle_pressed++;
282 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
288 mouse_flags |= flags;
290 mouse_flags &= ~flags;
293 LEAVE_CRITICAL_SECTION(&mouse_lock);
302 Mouse_dx = Mouse_dy = Mouse_dz = 0;
303 ENTER_CRITICAL_SECTION(&mouse_lock);
304 mouse_left_pressed = 0;
305 mouse_right_pressed = 0;
306 mouse_middle_pressed = 0;
308 LEAVE_CRITICAL_SECTION(&mouse_lock);
311 int mouse_down_count(int n, int reset_count)
314 if ( !mouse_inited ) return 0;
316 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
318 ENTER_CRITICAL_SECTION(&mouse_lock);
321 case MOUSE_LEFT_BUTTON:
322 tmp = mouse_left_pressed;
324 mouse_left_pressed = 0;
328 case MOUSE_RIGHT_BUTTON:
329 tmp = mouse_right_pressed;
331 mouse_right_pressed = 0;
335 case MOUSE_MIDDLE_BUTTON:
336 tmp = mouse_middle_pressed;
338 mouse_middle_pressed = 0;
343 LEAVE_CRITICAL_SECTION(&mouse_lock);
348 // mouse_up_count() returns the number of times button n has gone from down to up
349 // since the last call
351 // parameters: n - button of mouse (see #define's in mouse.h)
353 int mouse_up_count(int n)
356 if ( !mouse_inited ) return 0;
358 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
360 ENTER_CRITICAL_SECTION(&mouse_lock);
363 case MOUSE_LEFT_BUTTON:
368 case MOUSE_RIGHT_BUTTON:
369 tmp = mouse_right_up;
373 case MOUSE_MIDDLE_BUTTON:
374 tmp = mouse_middle_up;
379 Assert(0); // can't happen
383 LEAVE_CRITICAL_SECTION(&mouse_lock);
388 // returns 1 if mouse button btn is down, 0 otherwise
390 int mouse_down(int btn)
393 if ( !mouse_inited ) return 0;
395 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0;
398 ENTER_CRITICAL_SECTION(&mouse_lock);
401 if ( mouse_flags & btn )
406 LEAVE_CRITICAL_SECTION(&mouse_lock);
411 // returns the fraction of time btn has been down since last call
412 // (currently returns 1 if buttons is down, 0 otherwise)
414 float mouse_down_time(int btn)
417 if ( !mouse_inited ) return 0.0f;
419 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0.0f;
421 ENTER_CRITICAL_SECTION(&mouse_lock);
423 if ( mouse_flags & btn )
428 LEAVE_CRITICAL_SECTION(&mouse_lock);
433 void mouse_get_delta(int *dx, int *dy, int *dz)
443 // Forces the actual windows cursor to be at (x,y). This may be independent of our tracked (x,y) mouse pos.
444 void mouse_force_pos(int x, int y)
446 if (os_foreground()) { // only mess with windows's mouse if we are in control of it
454 ClientToScreen((HWND) os_get_window(), &pnt);
455 SetCursorPos(pnt.x, pnt.y);
460 #include "gamesequence.h"
462 // change in mouse position since last call
463 void mouse_eval_deltas()
465 static int old_x = 0;
466 static int old_y = 0;
467 int tmp_x, tmp_y, cx, cy;
469 Mouse_dx = Mouse_dy = Mouse_dz = 0;
473 if (Mouse_mode == MOUSE_MODE_DI) {
474 mouse_eval_deltas_di();
478 cx = gr_screen.max_w / 2;
479 cy = gr_screen.max_h / 2;
481 ENTER_CRITICAL_SECTION(&mouse_lock);
488 ScreenToClient((HWND)os_get_window(), &pnt);
493 Mouse_dx = tmp_x - old_x;
494 Mouse_dy = tmp_y - old_y;
497 if (Keep_mouse_centered && Mouse_hidden) {
498 if (Mouse_dx || Mouse_dy)
499 mouse_force_pos(cx, cy);
509 LEAVE_CRITICAL_SECTION(&mouse_lock);
515 static LPDIRECTINPUT Di_mouse_obj = NULL;
516 static LPDIRECTINPUTDEVICE Di_mouse = NULL;
519 void mouse_eval_deltas_di()
526 DIMOUSESTATE mouse_state;
528 Mouse_dx = Mouse_dy = Mouse_dz = 0;
529 if (!Di_mouse_inited)
533 memset(&mouse_state, 0, sizeof(mouse_state));
537 hr = Di_mouse->GetDeviceState(sizeof(mouse_state), &mouse_state);
538 if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED)) {
539 // DirectInput is telling us that the input stream has
540 // been interrupted. We aren't tracking any state
541 // between polls, so we don't have any special reset
542 // that needs to be done. We just re-acquire and
544 Sleep(500); // Pause a half second...
545 hr = Di_mouse->Acquire();
552 Mouse_dx = (int) mouse_state.lX;
553 Mouse_dy = (int) mouse_state.lY;
554 Mouse_dz = (int) mouse_state.lZ;
557 Mouse_dx = Mouse_dy = Mouse_dz = 0;
569 if (Mouse_x >= gr_screen.max_w)
570 Mouse_x = gr_screen.max_w - 1;
572 if (Mouse_y >= gr_screen.max_h)
573 Mouse_y = gr_screen.max_h - 1;
575 // keep the mouse inside our window so we don't switch applications or anything (debug bug people reported?)
576 // JH: Dang! This makes the mouse readings in DirectInput act screwy!
577 // mouse_force_pos(gr_screen.max_w / 2, gr_screen.max_h / 2);
581 int mouse_get_pos(int *xpos, int *ypos)
585 if (Mouse_mode == MOUSE_MODE_DI) {
605 ScreenToClient((HWND)os_get_window(), &pnt);
607 // EnterCriticalSection(&mouse_lock);
614 // LeaveCriticalSection(&mouse_lock);
624 if (Mouse_x >= gr_screen.max_w){
625 Mouse_x = gr_screen.max_w - 1;
628 if (Mouse_y >= gr_screen.max_h){
629 Mouse_y = gr_screen.max_h - 1;
643 void mouse_get_real_pos(int *mx, int *my)
645 if (Mouse_mode == MOUSE_MODE_DI) {
656 ScreenToClient((HWND)os_get_window(), &pnt);
663 void mouse_set_pos(int xpos, int ypos)
665 if (Mouse_mode == MOUSE_MODE_DI) {
671 if ((xpos != Mouse_x) || (ypos != Mouse_y)){
672 mouse_force_pos(xpos, ypos);
684 if (Mouse_mode == MOUSE_MODE_WIN){
689 hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &Di_mouse_obj, NULL);
691 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_mouse_obj, NULL);
693 mprintf(( "DirectInputCreate() failed!\n" ));
698 hr = Di_mouse_obj->CreateDevice(GUID_SysMouse, &Di_mouse, NULL);
700 mprintf(( "CreateDevice() failed!\n" ));
704 hr = Di_mouse->SetDataFormat(&c_dfDIMouse);
706 mprintf(( "SetDataFormat() failed!\n" ));
710 hr = Di_mouse->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
712 mprintf(( "SetCooperativeLevel() failed!\n" ));
719 hdr.diph.dwSize = sizeof(DIPROPDWORD);
720 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
722 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
723 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
725 hr = Di_mouse->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
727 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
731 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
732 Assert(Di_event != NULL);
734 hr = Di_mouse->SetEventNotification(Di_event);
736 mprintf(( "SetEventNotification failed\n" ));
752 // Destroy any lingering IDirectInputDevice object.
754 // Unacquire the device one last time just in case we got really confused
755 // and tried to exit while the device is still acquired.
756 Di_mouse->Unacquire();
762 // Destroy any lingering IDirectInput object.
764 Di_mouse_obj->Release();