2 * $Logfile: /Freespace2/code/Io/Mouse.cpp $
7 * Routines to read the mouse.
10 * Revision 1.4 2002/06/02 04:26:34 relnev
13 * Revision 1.3 2002/05/29 06:25:13 theoddone33
14 * Keyboard input, mouse tracking now work
16 * Revision 1.2 2002/05/07 03:16:46 theoddone33
17 * The Great Newline Fix
19 * Revision 1.1.1.1 2002/05/03 03:28:09 root
23 * 4 7/15/99 9:20a Andsager
24 * FS2_DEMO initial checkin
26 * 3 6/02/99 6:18p Dave
27 * Fixed TNT lockup problems! Wheeeee!
29 * 2 10/07/98 10:53a Dave
32 * 1 10/07/98 10:49a Dave
34 * 29 6/10/98 2:52p Hoffoss
35 * Made mouse code use DI by default, but fall back on normal code if that
38 * 28 5/24/98 1:35p Hoffoss
39 * Fixed bug where mouse cursor is always recentering with a
40 * mouse_flush() call in debug version.
42 * 27 5/22/98 4:50p Hoffoss
43 * Trying to fix mouse acceleration problem..
45 * 26 5/21/98 12:26p Lawrance
46 * Fixed mouse jerk at mission start while in debug build only.
48 * 25 5/15/98 2:41p Hoffoss
49 * Made mouse default to off (for flying ship) and fixed some new pilot
52 * 24 5/08/98 4:13p Hoffoss
53 * Fixed problem with mouse pointer centering causing lost keypresses.
55 * 23 5/07/98 6:58p Hoffoss
56 * Made changes to mouse code to fix a number of problems.
58 * 22 5/05/98 8:38p Hoffoss
59 * Added sensitivity adjustment to options menu and made it save to pilot
62 * 21 5/05/98 1:03p Hoffoss
63 * Fixed initialization bug.
65 * 20 5/01/98 5:45p Hoffoss
66 * Made further improvements to the mouse code.
68 * 19 5/01/98 3:35p Hoffoss
69 * Made changes to release version of mouse code.
71 * 18 5/01/98 1:14p Hoffoss
72 * Changed mouse usage so directInput is only used for release version.
74 * 17 4/30/98 5:40p Hoffoss
75 * Added mouse as a supported control to fly the ship.
77 * 16 4/29/98 12:13a Lawrance
78 * Add function to check down count of mouse button without reseting the
79 * internal count. Added hook to reset demo trailer timer when a button
82 * 15 4/02/98 5:26p John
84 * 14 1/19/98 6:15p John
85 * Fixed all my Optimized Build compiler warnings
87 * 13 12/04/97 3:47p John
88 * Made joystick move mouse cursor
90 * 12 11/20/97 5:36p Dave
91 * Hooked in a bunch of main hall changes (including sound). Made it
92 * possible to reposition (rewind/ffwd)
93 * sound buffer pointers. Fixed animation direction change framerate
96 * 11 5/12/97 11:41a John
97 * Added range checking to mouse position
99 * 10 4/22/97 5:55p Lawrance
100 * let mouse.cpp decide if mouse has moved
102 * 9 4/22/97 12:29p John
103 * Changed mouse code so that you have to call mouse_init for the mouse
106 * 8 4/22/97 10:56a John
107 * fixed some resource leaks.
109 * 7 3/26/97 10:52a Lawrance
110 * mouse always on in menus, disappears in gameplay after 1 second
112 * 6 3/11/97 1:37p Lawrance
113 * added mouse_up_count(), changed mouse_mark() to mouse_mark_button() &
116 * 5 12/09/96 1:29p Lawrance
117 * adding 3 button support
119 * 4 12/03/96 4:19p John
120 * Added some code so that holding down the mouse buttons between menus
121 * doesn't select the next menu.
128 #include <windowsx.h>
135 #define MOUSE_MODE_DI 0
136 #define MOUSE_MODE_WIN 1
139 LOCAL int Mouse_mode = MOUSE_MODE_DI;
141 LOCAL int Mouse_mode = MOUSE_MODE_WIN;
144 LOCAL int mouse_inited = 0;
145 LOCAL int Di_mouse_inited = 0;
149 CRITICAL_SECTION mouse_lock;
151 // #define USE_DIRECTINPUT
154 int mouse_left_pressed = 0;
155 int mouse_right_pressed = 0;
156 int mouse_middle_pressed = 0;
157 int mouse_left_up = 0;
158 int mouse_right_up = 0;
159 int mouse_middle_up = 0;
164 int Mouse_sensitivity = 4;
165 int Use_mouse_to_fly = 0;
166 int Mouse_hidden = 0;
167 int Keep_mouse_centered = 0;;
171 void mouse_force_pos(int x, int y);
172 void mouse_eval_deltas_di();
174 int mouse_is_visible()
176 return !Mouse_hidden;
184 #ifdef USE_DIRECTINPUT
191 DeleteCriticalSection( &mouse_lock );
198 if ( mouse_inited ) return;
204 InitializeCriticalSection( &mouse_lock );
207 ENTER_CRITICAL_SECTION(&mouse_lock);
210 Mouse_x = gr_screen.max_w / 2;
211 Mouse_y = gr_screen.max_h / 2;
213 #ifdef USE_DIRECTINPUT
215 Mouse_mode = MOUSE_MODE_WIN;
217 Mouse_mode = MOUSE_MODE_WIN;
220 LEAVE_CRITICAL_SECTION(&mouse_lock);
222 atexit( mouse_close );
226 // ----------------------------------------------------------------------------
227 // mouse_mark_button() is called asynchronously by the OS when a mouse button
228 // goes up or down. The mouse button that is affected is passed via the
231 // parameters: flags ==> mouse button pressed/released
232 // set ==> 1 - button is pressed
233 // 0 - button is released
235 void mouse_mark_button( uint flags, int set)
237 if ( !mouse_inited ) return;
239 ENTER_CRITICAL_SECTION(&mouse_lock);
241 if ( !(mouse_flags & MOUSE_LEFT_BUTTON) ) {
243 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
244 mouse_left_pressed++;
246 ////////////////////////////
247 /// SOMETHING TERRIBLE IS ABOUT TO HAPPEN. I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
248 /// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK THE LEFT MOUSE BUTTON.
249 /// PLEASE SEE ALAN FOR MORE INFORMATION.
250 ////////////////////////////
253 extern void demo_reset_trailer_timer();
254 demo_reset_trailer_timer();
257 ////////////////////////////
258 /// IT'S OVER. SEE, IT WASN'T SO BAD RIGHT? IT'S IS VERY UGLY LOOKING, I KNOW.
259 ////////////////////////////
264 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
269 if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) ) {
271 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
272 mouse_right_pressed++;
276 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
281 if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) ) {
283 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
284 mouse_middle_pressed++;
288 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
294 mouse_flags |= flags;
296 mouse_flags &= ~flags;
299 LEAVE_CRITICAL_SECTION(&mouse_lock);
308 Mouse_dx = Mouse_dy = Mouse_dz = 0;
309 ENTER_CRITICAL_SECTION(&mouse_lock);
310 mouse_left_pressed = 0;
311 mouse_right_pressed = 0;
312 mouse_middle_pressed = 0;
314 LEAVE_CRITICAL_SECTION(&mouse_lock);
317 int mouse_down_count(int n, int reset_count)
320 if ( !mouse_inited ) return 0;
322 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
324 ENTER_CRITICAL_SECTION(&mouse_lock);
327 case MOUSE_LEFT_BUTTON:
328 tmp = mouse_left_pressed;
330 mouse_left_pressed = 0;
334 case MOUSE_RIGHT_BUTTON:
335 tmp = mouse_right_pressed;
337 mouse_right_pressed = 0;
341 case MOUSE_MIDDLE_BUTTON:
342 tmp = mouse_middle_pressed;
344 mouse_middle_pressed = 0;
349 LEAVE_CRITICAL_SECTION(&mouse_lock);
354 // mouse_up_count() returns the number of times button n has gone from down to up
355 // since the last call
357 // parameters: n - button of mouse (see #define's in mouse.h)
359 int mouse_up_count(int n)
362 if ( !mouse_inited ) return 0;
364 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
366 ENTER_CRITICAL_SECTION(&mouse_lock);
369 case MOUSE_LEFT_BUTTON:
374 case MOUSE_RIGHT_BUTTON:
375 tmp = mouse_right_up;
379 case MOUSE_MIDDLE_BUTTON:
380 tmp = mouse_middle_up;
385 Assert(0); // can't happen
389 LEAVE_CRITICAL_SECTION(&mouse_lock);
394 // returns 1 if mouse button btn is down, 0 otherwise
396 int mouse_down(int btn)
399 if ( !mouse_inited ) return 0;
401 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0;
404 ENTER_CRITICAL_SECTION(&mouse_lock);
407 if ( mouse_flags & btn )
412 LEAVE_CRITICAL_SECTION(&mouse_lock);
417 // returns the fraction of time btn has been down since last call
418 // (currently returns 1 if buttons is down, 0 otherwise)
420 float mouse_down_time(int btn)
423 if ( !mouse_inited ) return 0.0f;
425 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0.0f;
427 ENTER_CRITICAL_SECTION(&mouse_lock);
429 if ( mouse_flags & btn )
434 LEAVE_CRITICAL_SECTION(&mouse_lock);
439 void mouse_get_delta(int *dx, int *dy, int *dz)
449 // Forces the actual windows cursor to be at (x,y). This may be independent of our tracked (x,y) mouse pos.
450 void mouse_force_pos(int x, int y)
452 if (os_foreground()) { // only mess with windows's mouse if we are in control of it
460 ClientToScreen((HWND) os_get_window(), &pnt);
461 SetCursorPos(pnt.x, pnt.y);
466 #include "gamesequence.h"
468 // change in mouse position since last call
469 void mouse_eval_deltas()
471 static int old_x = 0;
472 static int old_y = 0;
473 int tmp_x, tmp_y, cx, cy;
475 Mouse_dx = Mouse_dy = Mouse_dz = 0;
479 if (Mouse_mode == MOUSE_MODE_DI) {
480 mouse_eval_deltas_di();
484 cx = gr_screen.max_w / 2;
485 cy = gr_screen.max_h / 2;
487 ENTER_CRITICAL_SECTION(&mouse_lock);
490 SDL_GetMouseState (&tmp_x, &tmp_y);
494 ScreenToClient((HWND)os_get_window(), &pnt);
499 Mouse_dx = tmp_x - old_x;
500 Mouse_dy = tmp_y - old_y;
503 if (Keep_mouse_centered && Mouse_hidden) {
504 if (Mouse_dx || Mouse_dy)
505 mouse_force_pos(cx, cy);
515 LEAVE_CRITICAL_SECTION(&mouse_lock);
521 static LPDIRECTINPUT Di_mouse_obj = NULL;
522 static LPDIRECTINPUTDEVICE Di_mouse = NULL;
525 void mouse_eval_deltas_di()
532 DIMOUSESTATE mouse_state;
534 Mouse_dx = Mouse_dy = Mouse_dz = 0;
535 if (!Di_mouse_inited)
539 memset(&mouse_state, 0, sizeof(mouse_state));
543 hr = Di_mouse->GetDeviceState(sizeof(mouse_state), &mouse_state);
544 if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED)) {
545 // DirectInput is telling us that the input stream has
546 // been interrupted. We aren't tracking any state
547 // between polls, so we don't have any special reset
548 // that needs to be done. We just re-acquire and
550 Sleep(500); // Pause a half second...
551 hr = Di_mouse->Acquire();
558 Mouse_dx = (int) mouse_state.lX;
559 Mouse_dy = (int) mouse_state.lY;
560 Mouse_dz = (int) mouse_state.lZ;
563 Mouse_dx = Mouse_dy = Mouse_dz = 0;
575 if (Mouse_x >= gr_screen.max_w)
576 Mouse_x = gr_screen.max_w - 1;
578 if (Mouse_y >= gr_screen.max_h)
579 Mouse_y = gr_screen.max_h - 1;
581 // keep the mouse inside our window so we don't switch applications or anything (debug bug people reported?)
582 // JH: Dang! This makes the mouse readings in DirectInput act screwy!
583 // mouse_force_pos(gr_screen.max_w / 2, gr_screen.max_h / 2);
587 int mouse_get_pos(int *xpos, int *ypos)
591 if (Mouse_mode == MOUSE_MODE_DI) {
607 flags = SDL_GetMouseState (&Mouse_x, &Mouse_y);
612 ScreenToClient((HWND)os_get_window(), &pnt);
614 // EnterCriticalSection(&mouse_lock);
621 // LeaveCriticalSection(&mouse_lock);
631 if (Mouse_x >= gr_screen.max_w){
632 Mouse_x = gr_screen.max_w - 1;
635 if (Mouse_y >= gr_screen.max_h){
636 Mouse_y = gr_screen.max_h - 1;
650 void mouse_get_real_pos(int *mx, int *my)
652 if (Mouse_mode == MOUSE_MODE_DI) {
659 SDL_GetMouseState (mx, my);
663 ScreenToClient((HWND)os_get_window(), &pnt);
670 void mouse_set_pos(int xpos, int ypos)
672 if (Mouse_mode == MOUSE_MODE_DI) {
678 if ((xpos != Mouse_x) || (ypos != Mouse_y)){
679 mouse_force_pos(xpos, ypos);
691 if (Mouse_mode == MOUSE_MODE_WIN){
696 hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &Di_mouse_obj, NULL);
698 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_mouse_obj, NULL);
700 mprintf(( "DirectInputCreate() failed!\n" ));
705 hr = Di_mouse_obj->CreateDevice(GUID_SysMouse, &Di_mouse, NULL);
707 mprintf(( "CreateDevice() failed!\n" ));
711 hr = Di_mouse->SetDataFormat(&c_dfDIMouse);
713 mprintf(( "SetDataFormat() failed!\n" ));
717 hr = Di_mouse->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
719 mprintf(( "SetCooperativeLevel() failed!\n" ));
726 hdr.diph.dwSize = sizeof(DIPROPDWORD);
727 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
729 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
730 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
732 hr = Di_mouse->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
734 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
738 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
739 Assert(Di_event != NULL);
741 hr = Di_mouse->SetEventNotification(Di_event);
743 mprintf(( "SetEventNotification failed\n" ));
759 // Destroy any lingering IDirectInputDevice object.
761 // Unacquire the device one last time just in case we got really confused
762 // and tried to exit while the device is still acquired.
763 Di_mouse->Unacquire();
769 // Destroy any lingering IDirectInput object.
771 Di_mouse_obj->Release();