2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 * $Source: /cvs/cvsroot/d2x/arch/win32/mouse.c,v $
15 * $Revision: 1.1.1.1 $
17 * $Date: 2001-01-19 03:30:15 $
19 * Functions to access Mouse and Cyberman...
21 * $Log: not supported by cvs2svn $
22 * Revision 1.5 1999/10/15 05:27:48 donut
23 * include to fix undef'd err
25 * Revision 1.4 1999/10/14 03:08:10 donut
26 * changed exit to mprintf on unknown mouse event
28 * Revision 1.3 1999/10/09 05:03:57 donut
29 * fixed win32 exit on mouse move
31 * Revision 1.2 1999/09/05 04:19:19 sekmu
32 * made mouse exclusive for windows
34 * Revision 1.1.1.1 1999/06/14 22:00:37 donut
35 * Import of d1x 1.37 source.
37 * Revision 1.8 1996/02/21 13:57:36 allender
38 * cursor device manager stuff added here so as not to
39 * rely on InterfaceLib anymore
41 * Revision 1.7 1995/10/17 15:42:21 allender
42 * new mouse function to determine single button press
44 * Revision 1.6 1995/10/03 11:27:31 allender
45 * fixed up hotspot problems with the mouse on multiple monitors
47 * Revision 1.5 1995/07/13 11:27:08 allender
48 * trap button checks at MAX_MOUSE_BUTTONS
50 * Revision 1.4 1995/06/25 21:56:53 allender
51 * added events include
53 * Revision 1.3 1995/05/11 17:06:38 allender
54 * fixed up mouse routines
56 * Revision 1.2 1995/05/11 13:05:53 allender
57 * of mouse handler code
59 * Revision 1.1 1995/05/05 09:54:45 allender
62 * Revision 1.9 1995/01/14 19:19:52 john
63 * Fixed signed short error cmp with -1 that caused mouse
64 * to break under Watcom 10.0
66 * Revision 1.8 1994/12/27 12:38:23 john
67 * Made mouse use temporary dos buffer instead of
72 * Revision 1.7 1994/12/05 23:54:53 john
73 * Fixed bug with mouse_get_delta only returning positive numbers..
75 * Revision 1.6 1994/11/18 23:18:18 john
76 * Changed some shorts to ints.
78 * Revision 1.5 1994/09/13 12:34:02 john
79 * Added functions to get down count and state.
81 * Revision 1.4 1994/08/29 20:52:19 john
82 * Added better cyberman support; also, joystick calibration
83 * value return funcctiionn,
85 * Revision 1.3 1994/08/24 18:54:32 john
86 * *** empty log message ***
88 * Revision 1.2 1994/08/24 18:53:46 john
89 * Made Cyberman read like normal mouse; added dpmi module; moved
90 * mouse from assembly to c. Made mouse buttons return time_down.
92 * Revision 1.1 1994/08/24 13:56:37 john
98 #define WIN32_LEAN_AND_MEAN
112 // These are to kludge up a bit my slightly broken GCC directx port.
114 #define E_FAIL (HRESULT)0x80004005L
117 #define SUCCEEDED(a) ((HRESULT)(a) >= 0)
123 #ifndef SEVERITY_SUCCESS
124 #define SEVERITY_SUCCESS 0
125 #define SEVERITY_ERROR 1
127 #ifndef FACILITY_WIN32
128 #define FACILITY_WIN32 7
131 #define FIELD_OFFSET(type, field) ((LONG)&(((type *)0)->field))
134 #define ME_CURSOR_MOVED (1<<0)
135 #define ME_LB_P (1<<1)
136 #define ME_LB_R (1<<2)
137 #define ME_RB_P (1<<3)
138 #define ME_RB_R (1<<4)
139 #define ME_MB_P (1<<5)
140 #define ME_MB_R (1<<6)
141 #define ME_OB_P (1<<7)
142 #define ME_OB_R (1<<8)
143 #define ME_X_C (1<<9)
144 #define ME_Y_C (1<<10)
145 #define ME_Z_C (1<<11)
146 #define ME_P_C (1<<12)
147 #define ME_B_C (1<<13)
148 #define ME_H_C (1<<14)
149 #define ME_O_C (1<<15)
151 typedef struct event_info {
158 ushort button_status;
159 ushort device_dependant;
162 typedef struct mouse_info {
166 ubyte pressed[MOUSE_MAX_BUTTONS];
167 fix time_went_down[MOUSE_MAX_BUTTONS];
168 fix time_held_down[MOUSE_MAX_BUTTONS];
169 uint num_downs[MOUSE_MAX_BUTTONS];
170 uint num_ups[MOUSE_MAX_BUTTONS];
171 // ubyte went_down; /* Not in PC version, not needed with 'num_downs' etc */
173 ushort button_status;
176 typedef struct cyberman_info {
183 ubyte pitch_descriptor;
184 ubyte roll_descriptor;
185 ubyte yaw_descriptor;
189 static mouse_info Mouse;
191 static int Mouse_installed = 0;
193 int WMMouse_Handler_Ready = 0;
194 int mouse_wparam, mouse_lparam, mouse_msg;
198 //extern ggi_visual_t visual;
199 //extern ggi_directbuffer_t dbuf; // GGI direct acces to screen memory
200 //extern ggi_pixellinearbuffer *plb;
203 static double mouse_x, mouse_y;
204 static double mouse_saved_x, mouse_saved_y; //used when hiding/unhiding to reset the real (displayed) postion
205 double mouse_accel=1.0;
207 void DrawMouse(void);
208 void EraseMouse(void);
209 void MoveMouse(/*int button,*/ int x, int y);
211 #define WIN_WIDTH 640
212 #define WIN_HEIGHT 480
213 #define SCR_WIDTH 640
214 #define SCR_HEIGHT 480
216 LPDIRECTINPUT g_lpdi;
217 LPDIRECTINPUTDEVICE g_lpdidMouse;
221 HRESULT ReadMouse (DIDEVICEOBJECTDATA *pdidod)
226 if (g_lpdidMouse == NULL)
229 hr = IDirectInputDevice_GetDeviceData (
236 if (hr == DIERR_INPUTLOST)
238 hr = IDirectInputDevice_Acquire (g_lpdidMouse);
241 hr = IDirectInputDevice_GetDeviceData (
250 if (SUCCEEDED (hr) && cElements != 1)
257 void UpdateMouseState (DIDEVICEOBJECTDATA *pdidod)
259 // fix timeNow = timer_get_fixed_seconds ();
261 ULONG iEvt = pdidod->dwOfs;
269 BOOL bPressed = pdidod->dwData & 0x80;
270 ULONG iButton = (iEvt - DIMOFS_BUTTON0) + MB_LEFT;
273 if (!Mouse.pressed [iButton])
275 Mouse.pressed [iButton] = 1;
276 Mouse.time_went_down [iButton] = Mouse.ctime;
277 Mouse.num_downs [iButton]++;
278 // Mouse.went_down = 1;
280 Mouse.num_downs [iButton] ++;
284 if (Mouse.pressed [iButton])
286 Mouse.pressed [iButton] = 0;
287 Mouse.time_held_down [iButton] += Mouse.ctime - Mouse.time_went_down [iButton];
288 Mouse.num_ups [iButton]++;
289 // Mouse.went_down = 0;
295 mouse_x += (double) ((LONG) pdidod->dwData);
299 mouse_y += (double) ((LONG) pdidod->dwData);
302 break;//hm, handle this?
304 mprintf((0,"unknown mouse event %i\n",iEvt));
305 // exit (iEvt);//not happy.
312 DIDEVICEOBJECTDATA didod;
314 Mouse.ctime = timer_get_fixed_seconds();
316 while (SUCCEEDED (ReadMouse (&didod)))
318 UpdateMouseState (&didod);
327 if (!Mouse_installed)
332 CurTime = timer_get_fixed_seconds();
333 for (i = 0; i < MOUSE_MAX_BUTTONS; i++) {
334 Mouse.pressed[i] = 0;
335 Mouse.time_went_down[i] = CurTime;
336 Mouse.time_held_down[i] = 0;
337 Mouse.num_downs[i] = 0;
338 Mouse.num_ups[i] = 0;
340 // Mouse.went_down = 0; /* mac only */
344 ULONG cElements = INFINITE;
346 IDirectInputDevice_GetDeviceData (
348 sizeof (DIDEVICEOBJECTDATA),
356 void mouse_close(void)
358 // if (Mouse_installed) // DPH: Unnecessary...
359 WMMouse_Handler_Ready=Mouse_installed = 0;
361 if (g_lpdidMouse != NULL)
363 IDirectInputDevice_Unacquire (g_lpdidMouse);
364 IDirectInputDevice_Release (g_lpdidMouse);
369 IDirectInput_Release (g_lpdi);
376 int mouse_init(int unused)
379 return Mouse.num_buttons;
384 if (SUCCEEDED (hr = DirectInputCreate (GetModuleHandle (NULL), DIRECTINPUT_VERSION, &g_lpdi, NULL)))
386 if (SUCCEEDED (hr = IDirectInput_CreateDevice (g_lpdi,(void *) &GUID_SysMouse, &g_lpdidMouse, NULL)))
389 dipdw.diph.dwSize = sizeof (DIPROPDWORD);
390 dipdw.diph.dwHeaderSize = sizeof (DIPROPHEADER);
391 dipdw.diph.dwObj = 0;
392 dipdw.diph.dwHow = DIPH_DEVICE;
395 if (SUCCEEDED (hr = IDirectInputDevice_SetDataFormat (g_lpdidMouse, &c_dfDIMouse)) &&
396 //changed on 9/4/99 by Victor Rachels NONEX -> Exclusive
397 SUCCEEDED (hr = IDirectInputDevice_SetCooperativeLevel (g_lpdidMouse, g_hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND)) &&
398 //end this section edit -VR
399 SUCCEEDED (hr = IDirectInputDevice_SetProperty (g_lpdidMouse, DIPROP_BUFFERSIZE, &dipdw.diph)) &&
400 SUCCEEDED (hr = IDirectInputDevice_Acquire (g_lpdidMouse)))
405 IDirectInputDevice_Release (g_lpdidMouse);
412 Mouse.num_buttons = 3;
414 WMMouse_Handler_Ready=Mouse_installed = 1;
417 // mouse_set_center();
419 return Mouse.num_buttons;
423 void mouse_center() {
424 mouse_x=mouse_saved_x=WIN_WIDTH/2;
425 mouse_y=mouse_saved_y=WIN_HEIGHT/2;
428 void mouse_get_pos( int *x, int *y)
430 mouse_handler(); //temp
436 void mouse_get_delta( int *dx, int *dy )
438 if (!Mouse_installed) {
442 mouse_handler(); //temp
444 *dx = (int) mouse_x - WIN_WIDTH/2;
445 *dy = (int) mouse_y - WIN_HEIGHT/2;
447 //Now reset the mouse position to the center of the screen
448 mouse_x = (double) WIN_WIDTH/2;
449 mouse_y = (double) WIN_HEIGHT/2;
452 void mouse_get_delta_no_reset( int *dx, int *dy )
454 if (!Mouse_installed) {
458 mouse_handler(); //temp
460 *dx = (int) mouse_x - WIN_WIDTH/2;
461 *dy = (int) mouse_y - WIN_HEIGHT/2;
470 if (!Mouse_installed)
473 mouse_handler(); //temp
475 for (i = 0; i < MOUSE_MAX_BUTTONS; i++) {
476 if (Mouse.pressed[i])
483 /* This should be replaced with mouse_button_down_count(int button) */
484 int mouse_went_down(int button)
488 if (!Mouse_installed)
491 mouse_handler(); //temp
493 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
497 count = Mouse.num_downs[button];
498 Mouse.num_downs[button] = 0;
504 // Returns how many times this button has went down since last call.
505 int mouse_button_down_count(int button)
509 if (!Mouse_installed)
512 mouse_handler(); //temp
514 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
518 count = Mouse.num_downs[button];
519 Mouse.num_downs[button] = 0;
524 // Returns 1 if this button is currently down
525 int mouse_button_state(int button)
529 if (!Mouse_installed)
532 mouse_handler(); //temp
534 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
538 state = Mouse.pressed[button];
543 // Returns how long this button has been down since last call.
544 fix mouse_button_down_time(int button)
548 if (!Mouse_installed)
551 mouse_handler(); //temp
553 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
557 if (!Mouse.pressed[button]) {
558 time_down = Mouse.time_held_down[button];
559 Mouse.time_held_down[button] = 0;
561 time = timer_get_fixed_seconds();
562 time_down = time - Mouse.time_held_down[button];
563 Mouse.time_held_down[button] = 0;
570 void mouse_get_cyberman_pos( int *x, int *y )
576 //WHS: I made this :)
588 /* New mouse pointer stuff for GGI/DGA */
589 //#include "cursor.h" /* cursor and mask */
591 typedef struct Sprite {
598 //Sprite mouse_sprite = { cursor_width, cursor_height, cursor_bits, cursor_mask_bits};
600 //byte *behind_mouse;
601 //byte behind_mouse[cursor_width*cursor_height];
609 void EraseMouse(void)
613 //Should add a mode, relative, absolute
616 //void MoveMouse(int mode, int x, int y) {
618 void MoveMouse(int x, int y)