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.5 2002/06/09 04:41:22 relnev
19 * added copyright header
21 * Revision 1.4 2002/06/02 04:26:34 relnev
24 * Revision 1.3 2002/05/29 06:25:13 theoddone33
25 * Keyboard input, mouse tracking now work
27 * Revision 1.2 2002/05/07 03:16:46 theoddone33
28 * The Great Newline Fix
30 * Revision 1.1.1.1 2002/05/03 03:28:09 root
34 * 4 7/15/99 9:20a Andsager
35 * FS2_DEMO initial checkin
37 * 3 6/02/99 6:18p Dave
38 * Fixed TNT lockup problems! Wheeeee!
40 * 2 10/07/98 10:53a Dave
43 * 1 10/07/98 10:49a Dave
45 * 29 6/10/98 2:52p Hoffoss
46 * Made mouse code use DI by default, but fall back on normal code if that
49 * 28 5/24/98 1:35p Hoffoss
50 * Fixed bug where mouse cursor is always recentering with a
51 * mouse_flush() call in debug version.
53 * 27 5/22/98 4:50p Hoffoss
54 * Trying to fix mouse acceleration problem..
56 * 26 5/21/98 12:26p Lawrance
57 * Fixed mouse jerk at mission start while in debug build only.
59 * 25 5/15/98 2:41p Hoffoss
60 * Made mouse default to off (for flying ship) and fixed some new pilot
63 * 24 5/08/98 4:13p Hoffoss
64 * Fixed problem with mouse pointer centering causing lost keypresses.
66 * 23 5/07/98 6:58p Hoffoss
67 * Made changes to mouse code to fix a number of problems.
69 * 22 5/05/98 8:38p Hoffoss
70 * Added sensitivity adjustment to options menu and made it save to pilot
73 * 21 5/05/98 1:03p Hoffoss
74 * Fixed initialization bug.
76 * 20 5/01/98 5:45p Hoffoss
77 * Made further improvements to the mouse code.
79 * 19 5/01/98 3:35p Hoffoss
80 * Made changes to release version of mouse code.
82 * 18 5/01/98 1:14p Hoffoss
83 * Changed mouse usage so directInput is only used for release version.
85 * 17 4/30/98 5:40p Hoffoss
86 * Added mouse as a supported control to fly the ship.
88 * 16 4/29/98 12:13a Lawrance
89 * Add function to check down count of mouse button without reseting the
90 * internal count. Added hook to reset demo trailer timer when a button
93 * 15 4/02/98 5:26p John
95 * 14 1/19/98 6:15p John
96 * Fixed all my Optimized Build compiler warnings
98 * 13 12/04/97 3:47p John
99 * Made joystick move mouse cursor
101 * 12 11/20/97 5:36p Dave
102 * Hooked in a bunch of main hall changes (including sound). Made it
103 * possible to reposition (rewind/ffwd)
104 * sound buffer pointers. Fixed animation direction change framerate
107 * 11 5/12/97 11:41a John
108 * Added range checking to mouse position
110 * 10 4/22/97 5:55p Lawrance
111 * let mouse.cpp decide if mouse has moved
113 * 9 4/22/97 12:29p John
114 * Changed mouse code so that you have to call mouse_init for the mouse
117 * 8 4/22/97 10:56a John
118 * fixed some resource leaks.
120 * 7 3/26/97 10:52a Lawrance
121 * mouse always on in menus, disappears in gameplay after 1 second
123 * 6 3/11/97 1:37p Lawrance
124 * added mouse_up_count(), changed mouse_mark() to mouse_mark_button() &
127 * 5 12/09/96 1:29p Lawrance
128 * adding 3 button support
130 * 4 12/03/96 4:19p John
131 * Added some code so that holding down the mouse buttons between menus
132 * doesn't select the next menu.
139 #include <windowsx.h>
146 #define MOUSE_MODE_DI 0
147 #define MOUSE_MODE_WIN 1
150 LOCAL int Mouse_mode = MOUSE_MODE_DI;
152 LOCAL int Mouse_mode = MOUSE_MODE_WIN;
155 LOCAL int mouse_inited = 0;
156 LOCAL int Di_mouse_inited = 0;
160 CRITICAL_SECTION mouse_lock;
162 // #define USE_DIRECTINPUT
165 int mouse_left_pressed = 0;
166 int mouse_right_pressed = 0;
167 int mouse_middle_pressed = 0;
168 int mouse_left_up = 0;
169 int mouse_right_up = 0;
170 int mouse_middle_up = 0;
175 int Mouse_sensitivity = 4;
176 int Use_mouse_to_fly = 0;
177 int Mouse_hidden = 0;
178 int Keep_mouse_centered = 0;;
182 void mouse_force_pos(int x, int y);
183 void mouse_eval_deltas_di();
185 int mouse_is_visible()
187 return !Mouse_hidden;
195 #ifdef USE_DIRECTINPUT
202 DeleteCriticalSection( &mouse_lock );
209 if ( mouse_inited ) return;
215 InitializeCriticalSection( &mouse_lock );
218 ENTER_CRITICAL_SECTION(&mouse_lock);
221 Mouse_x = gr_screen.max_w / 2;
222 Mouse_y = gr_screen.max_h / 2;
224 #ifdef USE_DIRECTINPUT
226 Mouse_mode = MOUSE_MODE_WIN;
228 Mouse_mode = MOUSE_MODE_WIN;
231 LEAVE_CRITICAL_SECTION(&mouse_lock);
233 atexit( mouse_close );
237 // ----------------------------------------------------------------------------
238 // mouse_mark_button() is called asynchronously by the OS when a mouse button
239 // goes up or down. The mouse button that is affected is passed via the
242 // parameters: flags ==> mouse button pressed/released
243 // set ==> 1 - button is pressed
244 // 0 - button is released
246 void mouse_mark_button( uint flags, int set)
248 if ( !mouse_inited ) return;
250 ENTER_CRITICAL_SECTION(&mouse_lock);
252 if ( !(mouse_flags & MOUSE_LEFT_BUTTON) ) {
254 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
255 mouse_left_pressed++;
257 ////////////////////////////
258 /// SOMETHING TERRIBLE IS ABOUT TO HAPPEN. I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
259 /// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK THE LEFT MOUSE BUTTON.
260 /// PLEASE SEE ALAN FOR MORE INFORMATION.
261 ////////////////////////////
264 extern void demo_reset_trailer_timer();
265 demo_reset_trailer_timer();
268 ////////////////////////////
269 /// IT'S OVER. SEE, IT WASN'T SO BAD RIGHT? IT'S IS VERY UGLY LOOKING, I KNOW.
270 ////////////////////////////
275 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
280 if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) ) {
282 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
283 mouse_right_pressed++;
287 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
292 if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) ) {
294 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
295 mouse_middle_pressed++;
299 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
305 mouse_flags |= flags;
307 mouse_flags &= ~flags;
310 LEAVE_CRITICAL_SECTION(&mouse_lock);
319 Mouse_dx = Mouse_dy = Mouse_dz = 0;
320 ENTER_CRITICAL_SECTION(&mouse_lock);
321 mouse_left_pressed = 0;
322 mouse_right_pressed = 0;
323 mouse_middle_pressed = 0;
325 LEAVE_CRITICAL_SECTION(&mouse_lock);
328 int mouse_down_count(int n, int reset_count)
331 if ( !mouse_inited ) return 0;
333 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
335 ENTER_CRITICAL_SECTION(&mouse_lock);
338 case MOUSE_LEFT_BUTTON:
339 tmp = mouse_left_pressed;
341 mouse_left_pressed = 0;
345 case MOUSE_RIGHT_BUTTON:
346 tmp = mouse_right_pressed;
348 mouse_right_pressed = 0;
352 case MOUSE_MIDDLE_BUTTON:
353 tmp = mouse_middle_pressed;
355 mouse_middle_pressed = 0;
360 LEAVE_CRITICAL_SECTION(&mouse_lock);
365 // mouse_up_count() returns the number of times button n has gone from down to up
366 // since the last call
368 // parameters: n - button of mouse (see #define's in mouse.h)
370 int mouse_up_count(int n)
373 if ( !mouse_inited ) return 0;
375 if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
377 ENTER_CRITICAL_SECTION(&mouse_lock);
380 case MOUSE_LEFT_BUTTON:
385 case MOUSE_RIGHT_BUTTON:
386 tmp = mouse_right_up;
390 case MOUSE_MIDDLE_BUTTON:
391 tmp = mouse_middle_up;
396 Assert(0); // can't happen
400 LEAVE_CRITICAL_SECTION(&mouse_lock);
405 // returns 1 if mouse button btn is down, 0 otherwise
407 int mouse_down(int btn)
410 if ( !mouse_inited ) return 0;
412 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0;
415 ENTER_CRITICAL_SECTION(&mouse_lock);
418 if ( mouse_flags & btn )
423 LEAVE_CRITICAL_SECTION(&mouse_lock);
428 // returns the fraction of time btn has been down since last call
429 // (currently returns 1 if buttons is down, 0 otherwise)
431 float mouse_down_time(int btn)
434 if ( !mouse_inited ) return 0.0f;
436 if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0.0f;
438 ENTER_CRITICAL_SECTION(&mouse_lock);
440 if ( mouse_flags & btn )
445 LEAVE_CRITICAL_SECTION(&mouse_lock);
450 void mouse_get_delta(int *dx, int *dy, int *dz)
460 // Forces the actual windows cursor to be at (x,y). This may be independent of our tracked (x,y) mouse pos.
461 void mouse_force_pos(int x, int y)
463 if (os_foreground()) { // only mess with windows's mouse if we are in control of it
471 ClientToScreen((HWND) os_get_window(), &pnt);
472 SetCursorPos(pnt.x, pnt.y);
477 #include "gamesequence.h"
479 // change in mouse position since last call
480 void mouse_eval_deltas()
482 static int old_x = 0;
483 static int old_y = 0;
484 int tmp_x, tmp_y, cx, cy;
486 Mouse_dx = Mouse_dy = Mouse_dz = 0;
490 if (Mouse_mode == MOUSE_MODE_DI) {
491 mouse_eval_deltas_di();
495 cx = gr_screen.max_w / 2;
496 cy = gr_screen.max_h / 2;
498 ENTER_CRITICAL_SECTION(&mouse_lock);
501 SDL_GetMouseState (&tmp_x, &tmp_y);
505 ScreenToClient((HWND)os_get_window(), &pnt);
510 Mouse_dx = tmp_x - old_x;
511 Mouse_dy = tmp_y - old_y;
514 if (Keep_mouse_centered && Mouse_hidden) {
515 if (Mouse_dx || Mouse_dy)
516 mouse_force_pos(cx, cy);
526 LEAVE_CRITICAL_SECTION(&mouse_lock);
532 static LPDIRECTINPUT Di_mouse_obj = NULL;
533 static LPDIRECTINPUTDEVICE Di_mouse = NULL;
536 void mouse_eval_deltas_di()
543 DIMOUSESTATE mouse_state;
545 Mouse_dx = Mouse_dy = Mouse_dz = 0;
546 if (!Di_mouse_inited)
550 memset(&mouse_state, 0, sizeof(mouse_state));
554 hr = Di_mouse->GetDeviceState(sizeof(mouse_state), &mouse_state);
555 if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED)) {
556 // DirectInput is telling us that the input stream has
557 // been interrupted. We aren't tracking any state
558 // between polls, so we don't have any special reset
559 // that needs to be done. We just re-acquire and
561 Sleep(500); // Pause a half second...
562 hr = Di_mouse->Acquire();
569 Mouse_dx = (int) mouse_state.lX;
570 Mouse_dy = (int) mouse_state.lY;
571 Mouse_dz = (int) mouse_state.lZ;
574 Mouse_dx = Mouse_dy = Mouse_dz = 0;
586 if (Mouse_x >= gr_screen.max_w)
587 Mouse_x = gr_screen.max_w - 1;
589 if (Mouse_y >= gr_screen.max_h)
590 Mouse_y = gr_screen.max_h - 1;
592 // keep the mouse inside our window so we don't switch applications or anything (debug bug people reported?)
593 // JH: Dang! This makes the mouse readings in DirectInput act screwy!
594 // mouse_force_pos(gr_screen.max_w / 2, gr_screen.max_h / 2);
598 int mouse_get_pos(int *xpos, int *ypos)
602 if (Mouse_mode == MOUSE_MODE_DI) {
618 flags = SDL_GetMouseState (&Mouse_x, &Mouse_y);
623 ScreenToClient((HWND)os_get_window(), &pnt);
625 // EnterCriticalSection(&mouse_lock);
632 // LeaveCriticalSection(&mouse_lock);
642 if (Mouse_x >= gr_screen.max_w){
643 Mouse_x = gr_screen.max_w - 1;
646 if (Mouse_y >= gr_screen.max_h){
647 Mouse_y = gr_screen.max_h - 1;
661 void mouse_get_real_pos(int *mx, int *my)
663 if (Mouse_mode == MOUSE_MODE_DI) {
670 SDL_GetMouseState (mx, my);
674 ScreenToClient((HWND)os_get_window(), &pnt);
681 void mouse_set_pos(int xpos, int ypos)
683 if (Mouse_mode == MOUSE_MODE_DI) {
689 if ((xpos != Mouse_x) || (ypos != Mouse_y)){
690 mouse_force_pos(xpos, ypos);
702 if (Mouse_mode == MOUSE_MODE_WIN){
707 hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &Di_mouse_obj, NULL);
709 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_mouse_obj, NULL);
711 mprintf(( "DirectInputCreate() failed!\n" ));
716 hr = Di_mouse_obj->CreateDevice(GUID_SysMouse, &Di_mouse, NULL);
718 mprintf(( "CreateDevice() failed!\n" ));
722 hr = Di_mouse->SetDataFormat(&c_dfDIMouse);
724 mprintf(( "SetDataFormat() failed!\n" ));
728 hr = Di_mouse->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
730 mprintf(( "SetCooperativeLevel() failed!\n" ));
737 hdr.diph.dwSize = sizeof(DIPROPDWORD);
738 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
740 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
741 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
743 hr = Di_mouse->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
745 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
749 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
750 Assert(Di_event != NULL);
752 hr = Di_mouse->SetEventNotification(Di_event);
754 mprintf(( "SetEventNotification failed\n" ));
770 // Destroy any lingering IDirectInputDevice object.
772 // Unacquire the device one last time just in case we got really confused
773 // and tried to exit while the device is still acquired.
774 Di_mouse->Unacquire();
780 // Destroy any lingering IDirectInput object.
782 Di_mouse_obj->Release();