2 * $Logfile: /Freespace2/code/Io/Mouse.cpp $
7 * Routines to read the mouse.
10 * Revision 1.3 2002/05/29 06:25:13 theoddone33
11 * Keyboard input, mouse tracking now work
13 * Revision 1.2 2002/05/07 03:16:46 theoddone33
14 * The Great Newline Fix
16 * Revision 1.1.1.1 2002/05/03 03:28:09 root
20 * 4 7/15/99 9:20a Andsager
21 * FS2_DEMO initial checkin
23 * 3 6/02/99 6:18p Dave
24 * Fixed TNT lockup problems! Wheeeee!
26 * 2 10/07/98 10:53a Dave
29 * 1 10/07/98 10:49a Dave
31 * 29 6/10/98 2:52p Hoffoss
32 * Made mouse code use DI by default, but fall back on normal code if that
35 * 28 5/24/98 1:35p Hoffoss
36 * Fixed bug where mouse cursor is always recentering with a
37 * mouse_flush() call in debug version.
39 * 27 5/22/98 4:50p Hoffoss
40 * Trying to fix mouse acceleration problem..
42 * 26 5/21/98 12:26p Lawrance
43 * Fixed mouse jerk at mission start while in debug build only.
45 * 25 5/15/98 2:41p Hoffoss
46 * Made mouse default to off (for flying ship) and fixed some new pilot
49 * 24 5/08/98 4:13p Hoffoss
50 * Fixed problem with mouse pointer centering causing lost keypresses.
52 * 23 5/07/98 6:58p Hoffoss
53 * Made changes to mouse code to fix a number of problems.
55 * 22 5/05/98 8:38p Hoffoss
56 * Added sensitivity adjustment to options menu and made it save to pilot
59 * 21 5/05/98 1:03p Hoffoss
60 * Fixed initialization bug.
62 * 20 5/01/98 5:45p Hoffoss
63 * Made further improvements to the mouse code.
65 * 19 5/01/98 3:35p Hoffoss
66 * Made changes to release version of mouse code.
68 * 18 5/01/98 1:14p Hoffoss
69 * Changed mouse usage so directInput is only used for release version.
71 * 17 4/30/98 5:40p Hoffoss
72 * Added mouse as a supported control to fly the ship.
74 * 16 4/29/98 12:13a Lawrance
75 * Add function to check down count of mouse button without reseting the
76 * internal count. Added hook to reset demo trailer timer when a button
79 * 15 4/02/98 5:26p John
81 * 14 1/19/98 6:15p John
82 * Fixed all my Optimized Build compiler warnings
84 * 13 12/04/97 3:47p John
85 * Made joystick move mouse cursor
87 * 12 11/20/97 5:36p Dave
88 * Hooked in a bunch of main hall changes (including sound). Made it
89 * possible to reposition (rewind/ffwd)
90 * sound buffer pointers. Fixed animation direction change framerate
93 * 11 5/12/97 11:41a John
94 * Added range checking to mouse position
96 * 10 4/22/97 5:55p Lawrance
97 * let mouse.cpp decide if mouse has moved
99 * 9 4/22/97 12:29p John
100 * Changed mouse code so that you have to call mouse_init for the mouse
103 * 8 4/22/97 10:56a John
104 * fixed some resource leaks.
106 * 7 3/26/97 10:52a Lawrance
107 * mouse always on in menus, disappears in gameplay after 1 second
109 * 6 3/11/97 1:37p Lawrance
110 * added mouse_up_count(), changed mouse_mark() to mouse_mark_button() &
113 * 5 12/09/96 1:29p Lawrance
114 * adding 3 button support
116 * 4 12/03/96 4:19p John
117 * Added some code so that holding down the mouse buttons between menus
118 * doesn't select the next menu.
125 #include <windowsx.h>
132 #define MOUSE_MODE_DI 0
133 #define MOUSE_MODE_WIN 1
136 LOCAL int Mouse_mode = MOUSE_MODE_DI;
138 LOCAL int Mouse_mode = MOUSE_MODE_WIN;
141 LOCAL int mouse_inited = 0;
142 LOCAL int Di_mouse_inited = 0;
146 CRITICAL_SECTION mouse_lock;
148 // #define USE_DIRECTINPUT
151 int mouse_left_pressed = 0;
152 int mouse_right_pressed = 0;
153 int mouse_middle_pressed = 0;
154 int mouse_left_up = 0;
155 int mouse_right_up = 0;
156 int mouse_middle_up = 0;
161 int Mouse_sensitivity = 4;
162 int Use_mouse_to_fly = 0;
163 int Mouse_hidden = 0;
164 int Keep_mouse_centered = 0;;
168 void mouse_force_pos(int x, int y);
169 void mouse_eval_deltas_di();
171 int mouse_is_visible()
173 return !Mouse_hidden;
181 #ifdef USE_DIRECTINPUT
188 DeleteCriticalSection( &mouse_lock );
195 if ( mouse_inited ) return;
201 InitializeCriticalSection( &mouse_lock );
204 ENTER_CRITICAL_SECTION(&mouse_lock);
207 Mouse_x = gr_screen.max_w / 2;
208 Mouse_y = gr_screen.max_h / 2;
210 #ifdef USE_DIRECTINPUT
212 Mouse_mode = MOUSE_MODE_WIN;
214 Mouse_mode = MOUSE_MODE_WIN;
217 LEAVE_CRITICAL_SECTION(&mouse_lock);
219 atexit( mouse_close );
223 // ----------------------------------------------------------------------------
224 // mouse_mark_button() is called asynchronously by the OS when a mouse button
225 // goes up or down. The mouse button that is affected is passed via the
228 // parameters: flags ==> mouse button pressed/released
229 // set ==> 1 - button is pressed
230 // 0 - button is released
232 void mouse_mark_button( uint flags, int set)
234 if ( !mouse_inited ) return;
236 ENTER_CRITICAL_SECTION(&mouse_lock);
238 if ( !(mouse_flags & MOUSE_LEFT_BUTTON) ) {
240 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
241 mouse_left_pressed++;
243 ////////////////////////////
244 /// SOMETHING TERRIBLE IS ABOUT TO HAPPEN. I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
245 /// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK THE LEFT MOUSE BUTTON.
246 /// PLEASE SEE ALAN FOR MORE INFORMATION.
247 ////////////////////////////
250 extern void demo_reset_trailer_timer();
251 demo_reset_trailer_timer();
254 ////////////////////////////
255 /// IT'S OVER. SEE, IT WASN'T SO BAD RIGHT? IT'S IS VERY UGLY LOOKING, I KNOW.
256 ////////////////////////////
261 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
266 if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) ) {
268 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
269 mouse_right_pressed++;
273 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
278 if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) ) {
280 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
281 mouse_middle_pressed++;
285 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
291 mouse_flags |= flags;
293 mouse_flags &= ~flags;
296 LEAVE_CRITICAL_SECTION(&mouse_lock);
305 Mouse_dx = Mouse_dy = Mouse_dz = 0;
306 ENTER_CRITICAL_SECTION(&mouse_lock);
307 mouse_left_pressed = 0;
308 mouse_right_pressed = 0;
309 mouse_middle_pressed = 0;
311 LEAVE_CRITICAL_SECTION(&mouse_lock);
314 int mouse_down_count(int n, int reset_count)
317 if ( !mouse_inited ) return 0;
319 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
321 ENTER_CRITICAL_SECTION(&mouse_lock);
324 case MOUSE_LEFT_BUTTON:
325 tmp = mouse_left_pressed;
327 mouse_left_pressed = 0;
331 case MOUSE_RIGHT_BUTTON:
332 tmp = mouse_right_pressed;
334 mouse_right_pressed = 0;
338 case MOUSE_MIDDLE_BUTTON:
339 tmp = mouse_middle_pressed;
341 mouse_middle_pressed = 0;
346 LEAVE_CRITICAL_SECTION(&mouse_lock);
351 // mouse_up_count() returns the number of times button n has gone from down to up
352 // since the last call
354 // parameters: n - button of mouse (see #define's in mouse.h)
356 int mouse_up_count(int n)
359 if ( !mouse_inited ) return 0;
361 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
363 ENTER_CRITICAL_SECTION(&mouse_lock);
366 case MOUSE_LEFT_BUTTON:
371 case MOUSE_RIGHT_BUTTON:
372 tmp = mouse_right_up;
376 case MOUSE_MIDDLE_BUTTON:
377 tmp = mouse_middle_up;
382 Assert(0); // can't happen
386 LEAVE_CRITICAL_SECTION(&mouse_lock);
391 // returns 1 if mouse button btn is down, 0 otherwise
393 int mouse_down(int btn)
396 if ( !mouse_inited ) return 0;
398 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0;
401 ENTER_CRITICAL_SECTION(&mouse_lock);
404 if ( mouse_flags & btn )
409 LEAVE_CRITICAL_SECTION(&mouse_lock);
414 // returns the fraction of time btn has been down since last call
415 // (currently returns 1 if buttons is down, 0 otherwise)
417 float mouse_down_time(int btn)
420 if ( !mouse_inited ) return 0.0f;
422 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0.0f;
424 ENTER_CRITICAL_SECTION(&mouse_lock);
426 if ( mouse_flags & btn )
431 LEAVE_CRITICAL_SECTION(&mouse_lock);
436 void mouse_get_delta(int *dx, int *dy, int *dz)
446 // Forces the actual windows cursor to be at (x,y). This may be independent of our tracked (x,y) mouse pos.
447 void mouse_force_pos(int x, int y)
449 if (os_foreground()) { // only mess with windows's mouse if we are in control of it
457 ClientToScreen((HWND) os_get_window(), &pnt);
458 SetCursorPos(pnt.x, pnt.y);
463 #include "gamesequence.h"
465 // change in mouse position since last call
466 void mouse_eval_deltas()
468 static int old_x = 0;
469 static int old_y = 0;
470 int tmp_x, tmp_y, cx, cy;
472 Mouse_dx = Mouse_dy = Mouse_dz = 0;
476 if (Mouse_mode == MOUSE_MODE_DI) {
477 mouse_eval_deltas_di();
481 cx = gr_screen.max_w / 2;
482 cy = gr_screen.max_h / 2;
484 ENTER_CRITICAL_SECTION(&mouse_lock);
491 ScreenToClient((HWND)os_get_window(), &pnt);
496 Mouse_dx = tmp_x - old_x;
497 Mouse_dy = tmp_y - old_y;
500 if (Keep_mouse_centered && Mouse_hidden) {
501 if (Mouse_dx || Mouse_dy)
502 mouse_force_pos(cx, cy);
512 LEAVE_CRITICAL_SECTION(&mouse_lock);
518 static LPDIRECTINPUT Di_mouse_obj = NULL;
519 static LPDIRECTINPUTDEVICE Di_mouse = NULL;
522 void mouse_eval_deltas_di()
529 DIMOUSESTATE mouse_state;
531 Mouse_dx = Mouse_dy = Mouse_dz = 0;
532 if (!Di_mouse_inited)
536 memset(&mouse_state, 0, sizeof(mouse_state));
540 hr = Di_mouse->GetDeviceState(sizeof(mouse_state), &mouse_state);
541 if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED)) {
542 // DirectInput is telling us that the input stream has
543 // been interrupted. We aren't tracking any state
544 // between polls, so we don't have any special reset
545 // that needs to be done. We just re-acquire and
547 Sleep(500); // Pause a half second...
548 hr = Di_mouse->Acquire();
555 Mouse_dx = (int) mouse_state.lX;
556 Mouse_dy = (int) mouse_state.lY;
557 Mouse_dz = (int) mouse_state.lZ;
560 Mouse_dx = Mouse_dy = Mouse_dz = 0;
572 if (Mouse_x >= gr_screen.max_w)
573 Mouse_x = gr_screen.max_w - 1;
575 if (Mouse_y >= gr_screen.max_h)
576 Mouse_y = gr_screen.max_h - 1;
578 // keep the mouse inside our window so we don't switch applications or anything (debug bug people reported?)
579 // JH: Dang! This makes the mouse readings in DirectInput act screwy!
580 // mouse_force_pos(gr_screen.max_w / 2, gr_screen.max_h / 2);
584 int mouse_get_pos(int *xpos, int *ypos)
588 if (Mouse_mode == MOUSE_MODE_DI) {
604 flags = SDL_GetMouseState (&Mouse_x, &Mouse_y);
609 ScreenToClient((HWND)os_get_window(), &pnt);
611 // EnterCriticalSection(&mouse_lock);
618 // LeaveCriticalSection(&mouse_lock);
628 if (Mouse_x >= gr_screen.max_w){
629 Mouse_x = gr_screen.max_w - 1;
632 if (Mouse_y >= gr_screen.max_h){
633 Mouse_y = gr_screen.max_h - 1;
647 void mouse_get_real_pos(int *mx, int *my)
649 if (Mouse_mode == MOUSE_MODE_DI) {
656 SDL_GetMouseState (mx, my);
660 ScreenToClient((HWND)os_get_window(), &pnt);
667 void mouse_set_pos(int xpos, int ypos)
669 if (Mouse_mode == MOUSE_MODE_DI) {
675 if ((xpos != Mouse_x) || (ypos != Mouse_y)){
676 mouse_force_pos(xpos, ypos);
688 if (Mouse_mode == MOUSE_MODE_WIN){
693 hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &Di_mouse_obj, NULL);
695 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_mouse_obj, NULL);
697 mprintf(( "DirectInputCreate() failed!\n" ));
702 hr = Di_mouse_obj->CreateDevice(GUID_SysMouse, &Di_mouse, NULL);
704 mprintf(( "CreateDevice() failed!\n" ));
708 hr = Di_mouse->SetDataFormat(&c_dfDIMouse);
710 mprintf(( "SetDataFormat() failed!\n" ));
714 hr = Di_mouse->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
716 mprintf(( "SetCooperativeLevel() failed!\n" ));
723 hdr.diph.dwSize = sizeof(DIPROPDWORD);
724 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
726 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
727 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
729 hr = Di_mouse->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
731 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
735 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
736 Assert(Di_event != NULL);
738 hr = Di_mouse->SetEventNotification(Di_event);
740 mprintf(( "SetEventNotification failed\n" ));
756 // Destroy any lingering IDirectInputDevice object.
758 // Unacquire the device one last time just in case we got really confused
759 // and tried to exit while the device is still acquired.
760 Di_mouse->Unacquire();
766 // Destroy any lingering IDirectInput object.
768 Di_mouse_obj->Release();