1 /* DirectInput Mouse device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
35 #include "dinput_private.h"
36 #include "device_private.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
44 /* Wine mouse driver object instances */
45 #define WINE_MOUSE_X_AXIS_INSTANCE 0
46 #define WINE_MOUSE_Y_AXIS_INSTANCE 1
47 #define WINE_MOUSE_Z_AXIS_INSTANCE 2
48 #define WINE_MOUSE_L_BUTTON_INSTANCE 0
49 #define WINE_MOUSE_R_BUTTON_INSTANCE 1
50 #define WINE_MOUSE_M_BUTTON_INSTANCE 2
51 #define WINE_MOUSE_D_BUTTON_INSTANCE 3
53 /* ------------------------------- */
54 /* Wine mouse internal data format */
55 /* ------------------------------- */
57 /* Constants used to access the offset array */
58 #define WINE_MOUSE_X_POSITION 0
59 #define WINE_MOUSE_Y_POSITION 1
60 #define WINE_MOUSE_Z_POSITION 2
61 #define WINE_MOUSE_L_POSITION 3
62 #define WINE_MOUSE_R_POSITION 4
63 #define WINE_MOUSE_M_POSITION 5
70 } Wine_InternalMouseData
;
72 #define WINE_INTERNALMOUSE_NUM_OBJS 6
74 static const DIOBJECTDATAFORMAT Wine_InternalMouseObjectFormat
[WINE_INTERNALMOUSE_NUM_OBJS
] = {
75 { &GUID_XAxis
, FIELD_OFFSET(Wine_InternalMouseData
, lX
),
76 DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
, 0 },
77 { &GUID_YAxis
, FIELD_OFFSET(Wine_InternalMouseData
, lY
),
78 DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
, 0 },
79 { &GUID_ZAxis
, FIELD_OFFSET(Wine_InternalMouseData
, lZ
),
80 DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
, 0 },
81 { &GUID_Button
, (FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
)) + 0,
82 DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
, 0 },
83 { &GUID_Button
, (FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
)) + 1,
84 DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
, 0 },
85 { &GUID_Button
, (FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
)) + 2,
86 DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
, 0 }
89 static const DIDATAFORMAT Wine_InternalMouseFormat
= {
90 0, /* dwSize - unused */
91 0, /* dwObjsize - unused */
92 0, /* dwFlags - unused */
93 sizeof(Wine_InternalMouseData
),
94 WINE_INTERNALMOUSE_NUM_OBJS
, /* dwNumObjs */
95 (LPDIOBJECTDATAFORMAT
) Wine_InternalMouseObjectFormat
98 static IDirectInputDevice8AVtbl SysMouseAvt
;
99 static IDirectInputDevice8WVtbl SysMouseWvt
;
101 typedef struct SysMouseImpl SysMouseImpl
;
104 WARP_DONE
, /* Warping has been done */
105 WARP_NEEDED
, /* Warping is needed */
106 WARP_STARTED
/* Warping has been done, waiting for the warp event */
115 IDirectInputImpl
*dinput
;
117 /* The current data format and the conversion between internal
118 and external data formats */
121 int offset_array
[WINE_INTERNALMOUSE_NUM_OBJS
];
125 /* Previous position for relative moves */
127 /* These are used in case of relative -> absolute transitions */
133 DWORD win_centerX
, win_centerY
;
134 LPDIDEVICEOBJECTDATA data_queue
;
135 int queue_head
, queue_tail
, queue_len
;
137 /* warping: whether we need to move mouse back to middle once we
138 * reach window borders (for e.g. shooters, "surface movement" games) */
139 WARP_STATUS need_warp
;
142 CRITICAL_SECTION crit
;
144 /* This is for mouse reporting. */
145 Wine_InternalMouseData m_state
;
148 /* FIXME: This is ugly and not thread safe :/ */
149 static IDirectInputDevice8A
* current_lock
= NULL
;
151 static GUID DInput_Wine_Mouse_GUID
= { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
155 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
158 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi
, int version
) {
160 DIDEVICEINSTANCEA ddi
;
162 dwSize
= lpddi
->dwSize
;
164 TRACE("%ld %p\n", dwSize
, lpddi
);
166 memset(lpddi
, 0, dwSize
);
167 memset(&ddi
, 0, sizeof(ddi
));
170 ddi
.guidInstance
= GUID_SysMouse
;/* DInput's GUID */
171 ddi
.guidProduct
= DInput_Wine_Mouse_GUID
; /* Vendor's GUID */
173 ddi
.dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
175 ddi
.dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
176 strcpy(ddi
.tszInstanceName
, "Mouse");
177 strcpy(ddi
.tszProductName
, "Wine Mouse");
179 memcpy(lpddi
, &ddi
, (dwSize
< sizeof(ddi
) ? dwSize
: sizeof(ddi
)));
182 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi
, int version
) {
184 DIDEVICEINSTANCEW ddi
;
186 dwSize
= lpddi
->dwSize
;
188 TRACE("%ld %p\n", dwSize
, lpddi
);
190 memset(lpddi
, 0, dwSize
);
191 memset(&ddi
, 0, sizeof(ddi
));
194 ddi
.guidInstance
= GUID_SysMouse
;/* DInput's GUID */
195 ddi
.guidProduct
= DInput_Wine_Mouse_GUID
; /* Vendor's GUID */
197 ddi
.dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
199 ddi
.dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
200 MultiByteToWideChar(CP_ACP
, 0, "Mouse", -1, ddi
.tszInstanceName
, MAX_PATH
);
201 MultiByteToWideChar(CP_ACP
, 0, "Wine Mouse", -1, ddi
.tszProductName
, MAX_PATH
);
203 memcpy(lpddi
, &ddi
, (dwSize
< sizeof(ddi
) ? dwSize
: sizeof(ddi
)));
206 static BOOL
mousedev_enum_deviceA(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEA lpddi
, int version
, int id
)
211 if ((dwDevType
== 0) ||
212 ((dwDevType
== DIDEVTYPE_MOUSE
) && (version
< 8)) ||
213 (((dwDevType
== DI8DEVCLASS_POINTER
) || (dwDevType
== DI8DEVTYPE_MOUSE
)) && (version
>= 8))) {
214 TRACE("Enumerating the mouse device\n");
216 fill_mouse_dideviceinstanceA(lpddi
, version
);
224 static BOOL
mousedev_enum_deviceW(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEW lpddi
, int version
, int id
)
229 if ((dwDevType
== 0) ||
230 ((dwDevType
== DIDEVTYPE_MOUSE
) && (version
< 8)) ||
231 (((dwDevType
== DI8DEVCLASS_POINTER
) || (dwDevType
== DI8DEVTYPE_MOUSE
)) && (version
>= 8))) {
232 TRACE("Enumerating the mouse device\n");
234 fill_mouse_dideviceinstanceW(lpddi
, version
);
242 static SysMouseImpl
*alloc_device(REFGUID rguid
, LPVOID mvt
, IDirectInputImpl
*dinput
)
244 int offset_array
[WINE_INTERNALMOUSE_NUM_OBJS
] = {
245 FIELD_OFFSET(Wine_InternalMouseData
, lX
),
246 FIELD_OFFSET(Wine_InternalMouseData
, lY
),
247 FIELD_OFFSET(Wine_InternalMouseData
, lZ
),
248 FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
) + 0,
249 FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
) + 1,
250 FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
) + 2
252 SysMouseImpl
* newDevice
;
253 newDevice
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(SysMouseImpl
));
255 newDevice
->lpVtbl
= mvt
;
256 InitializeCriticalSection(&(newDevice
->crit
));
257 memcpy(&(newDevice
->guid
),rguid
,sizeof(*rguid
));
259 /* Per default, Wine uses its internal data format */
260 newDevice
->df
= (DIDATAFORMAT
*) &Wine_InternalMouseFormat
;
261 memcpy(newDevice
->offset_array
, offset_array
, WINE_INTERNALMOUSE_NUM_OBJS
* sizeof(int));
262 newDevice
->wine_df
= HeapAlloc(GetProcessHeap(), 0, sizeof(DataFormat
));
263 newDevice
->wine_df
->size
= 0;
264 newDevice
->wine_df
->internal_format_size
= Wine_InternalMouseFormat
.dwDataSize
;
265 newDevice
->wine_df
->dt
= NULL
;
266 newDevice
->dinput
= dinput
;
271 static HRESULT
mousedev_create_deviceA(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEA
* pdev
)
273 if ((IsEqualGUID(&GUID_SysMouse
,rguid
)) || /* Generic Mouse */
274 (IsEqualGUID(&DInput_Wine_Mouse_GUID
,rguid
))) { /* Wine Mouse */
275 if ((riid
== NULL
) ||
276 IsEqualGUID(&IID_IDirectInputDeviceA
,riid
) ||
277 IsEqualGUID(&IID_IDirectInputDevice2A
,riid
) ||
278 IsEqualGUID(&IID_IDirectInputDevice7A
,riid
) ||
279 IsEqualGUID(&IID_IDirectInputDevice8A
,riid
)) {
280 *pdev
= (IDirectInputDeviceA
*) alloc_device(rguid
, &SysMouseAvt
, dinput
);
281 TRACE("Creating a Mouse device (%p)\n", *pdev
);
284 return DIERR_NOINTERFACE
;
287 return DIERR_DEVICENOTREG
;
290 static HRESULT
mousedev_create_deviceW(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEW
* pdev
)
292 if ((IsEqualGUID(&GUID_SysMouse
,rguid
)) || /* Generic Mouse */
293 (IsEqualGUID(&DInput_Wine_Mouse_GUID
,rguid
))) { /* Wine Mouse */
294 if ((riid
== NULL
) ||
295 IsEqualGUID(&IID_IDirectInputDeviceW
,riid
) ||
296 IsEqualGUID(&IID_IDirectInputDevice2W
,riid
) ||
297 IsEqualGUID(&IID_IDirectInputDevice7W
,riid
) ||
298 IsEqualGUID(&IID_IDirectInputDevice8W
,riid
)) {
299 *pdev
= (IDirectInputDeviceW
*) alloc_device(rguid
, &SysMouseWvt
, dinput
);
300 TRACE("Creating a Mouse device (%p)\n", *pdev
);
303 return DIERR_NOINTERFACE
;
306 return DIERR_DEVICENOTREG
;
309 const struct dinput_device mouse_device
= {
311 mousedev_enum_deviceA
,
312 mousedev_enum_deviceW
,
313 mousedev_create_deviceA
,
314 mousedev_create_deviceW
317 /******************************************************************************
318 * SysMouseA (DInput Mouse support)
321 /******************************************************************************
322 * Release : release the mouse buffer.
324 static ULONG WINAPI
SysMouseAImpl_Release(LPDIRECTINPUTDEVICE8A iface
)
326 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
329 ref
= InterlockedDecrement(&(This
->ref
));
333 /* Free the data queue */
334 HeapFree(GetProcessHeap(),0,This
->data_queue
);
337 UnhookWindowsHookEx( This
->hook
);
338 if (This
->dwCoopLevel
& DISCL_EXCLUSIVE
)
339 ShowCursor(TRUE
); /* show cursor */
341 DeleteCriticalSection(&(This
->crit
));
343 /* Free the DataFormat */
344 if (This
->df
!= &(Wine_InternalMouseFormat
)) {
345 HeapFree(GetProcessHeap(), 0, This
->df
->rgodf
);
346 HeapFree(GetProcessHeap(), 0, This
->df
);
349 HeapFree(GetProcessHeap(),0,This
);
354 /******************************************************************************
355 * SetCooperativeLevel : store the window in which we will do our
358 static HRESULT WINAPI
SysMouseAImpl_SetCooperativeLevel(
359 LPDIRECTINPUTDEVICE8A iface
,HWND hwnd
,DWORD dwflags
362 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
364 TRACE("(this=%p,0x%08lx,0x%08lx)\n",This
,(DWORD
)hwnd
,dwflags
);
366 if (TRACE_ON(dinput
)) {
367 TRACE(" cooperative level : ");
368 _dump_cooperativelevel_DI(dwflags
);
371 /* Store the window which asks for the mouse */
373 hwnd
= GetDesktopWindow();
375 This
->dwCoopLevel
= dwflags
;
381 /******************************************************************************
382 * SetDataFormat : the application can choose the format of the data
383 * the device driver sends back with GetDeviceState.
385 * For the moment, only the "standard" configuration (c_dfDIMouse) is supported
386 * in absolute and relative mode.
388 static HRESULT WINAPI
SysMouseAImpl_SetDataFormat(
389 LPDIRECTINPUTDEVICE8A iface
,LPCDIDATAFORMAT df
392 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
394 TRACE("(this=%p,%p)\n",This
,df
);
396 _dump_DIDATAFORMAT(df
);
398 /* Tests under windows show that a call to SetDataFormat always sets the mouse
399 in relative mode whatever the dwFlags value (DIDF_ABSAXIS/DIDF_RELAXIS).
400 To switch in absolute mode, SetProperty must be used. */
403 /* Store the new data format */
404 This
->df
= HeapAlloc(GetProcessHeap(),0,df
->dwSize
);
405 memcpy(This
->df
, df
, df
->dwSize
);
406 This
->df
->rgodf
= HeapAlloc(GetProcessHeap(),0,df
->dwNumObjs
*df
->dwObjSize
);
407 memcpy(This
->df
->rgodf
,df
->rgodf
,df
->dwNumObjs
*df
->dwObjSize
);
409 /* Prepare all the data-conversion filters */
410 This
->wine_df
= create_DataFormat(&(Wine_InternalMouseFormat
), df
, This
->offset_array
);
415 /* low-level mouse hook */
416 static LRESULT CALLBACK
dinput_mouse_hook( int code
, WPARAM wparam
, LPARAM lparam
)
419 MSLLHOOKSTRUCT
*hook
= (MSLLHOOKSTRUCT
*)lparam
;
420 SysMouseImpl
* This
= (SysMouseImpl
*) current_lock
;
422 static long last_event
= 0;
425 if (code
!= HC_ACTION
) return CallNextHookEx( This
->hook
, code
, wparam
, lparam
);
427 EnterCriticalSection(&(This
->crit
));
428 dwCoop
= This
->dwCoopLevel
;
430 /* Only allow mouse events every 10 ms.
431 * This is to allow the cursor to start acceleration before
432 * the warps happen. But if it involves a mouse button event we
433 * allow it since we don't want to lose the clicks.
435 if (((GetCurrentTime() - last_event
) < 10)
436 && wparam
== WM_MOUSEMOVE
)
438 else last_event
= GetCurrentTime();
440 /* Mouse moved -> send event if asked */
442 SetEvent(This
->hEvent
);
444 if (wparam
== WM_MOUSEMOVE
) {
445 if (This
->absolute
) {
446 if (hook
->pt
.x
!= This
->prevX
)
447 GEN_EVENT(This
->offset_array
[WINE_MOUSE_X_POSITION
], hook
->pt
.x
, hook
->time
, 0);
448 if (hook
->pt
.y
!= This
->prevY
)
449 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Y_POSITION
], hook
->pt
.y
, hook
->time
, 0);
451 /* Now, warp handling */
452 if ((This
->need_warp
== WARP_STARTED
) &&
453 (hook
->pt
.x
== This
->mapped_center
.x
) && (hook
->pt
.y
== This
->mapped_center
.y
)) {
454 /* Warp has been done... */
455 This
->need_warp
= WARP_DONE
;
459 /* Relative mouse input with absolute mouse event : the real fun starts here... */
460 if ((This
->need_warp
== WARP_NEEDED
) ||
461 (This
->need_warp
== WARP_STARTED
)) {
462 if (hook
->pt
.x
!= This
->prevX
)
463 GEN_EVENT(This
->offset_array
[WINE_MOUSE_X_POSITION
], hook
->pt
.x
- This
->prevX
,
464 hook
->time
, (This
->dinput
->evsequence
)++);
465 if (hook
->pt
.y
!= This
->prevY
)
466 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Y_POSITION
], hook
->pt
.y
- This
->prevY
,
467 hook
->time
, (This
->dinput
->evsequence
)++);
469 /* This is the first time the event handler has been called after a
470 GetDeviceData or GetDeviceState. */
471 if (hook
->pt
.x
!= This
->mapped_center
.x
) {
472 GEN_EVENT(This
->offset_array
[WINE_MOUSE_X_POSITION
], hook
->pt
.x
- This
->mapped_center
.x
,
473 hook
->time
, (This
->dinput
->evsequence
)++);
474 This
->need_warp
= WARP_NEEDED
;
477 if (hook
->pt
.y
!= This
->mapped_center
.y
) {
478 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Y_POSITION
], hook
->pt
.y
- This
->mapped_center
.y
,
479 hook
->time
, (This
->dinput
->evsequence
)++);
480 This
->need_warp
= WARP_NEEDED
;
485 This
->prevX
= hook
->pt
.x
;
486 This
->prevY
= hook
->pt
.y
;
488 if (This
->absolute
) {
489 This
->m_state
.lX
= hook
->pt
.x
;
490 This
->m_state
.lY
= hook
->pt
.y
;
492 This
->m_state
.lX
= hook
->pt
.x
- This
->mapped_center
.x
;
493 This
->m_state
.lY
= hook
->pt
.y
- This
->mapped_center
.y
;
497 TRACE(" msg %x pt %ld %ld (W=%d)\n",
498 wparam
, hook
->pt
.x
, hook
->pt
.y
, (!This
->absolute
) && This
->need_warp
);
502 GEN_EVENT(This
->offset_array
[WINE_MOUSE_L_POSITION
], 0x80,
503 hook
->time
, This
->dinput
->evsequence
++);
504 This
->m_state
.rgbButtons
[0] = 0x80;
507 GEN_EVENT(This
->offset_array
[WINE_MOUSE_L_POSITION
], 0x00,
508 hook
->time
, This
->dinput
->evsequence
++);
509 This
->m_state
.rgbButtons
[0] = 0x00;
512 GEN_EVENT(This
->offset_array
[WINE_MOUSE_R_POSITION
], 0x80,
513 hook
->time
, This
->dinput
->evsequence
++);
514 This
->m_state
.rgbButtons
[1] = 0x80;
517 GEN_EVENT(This
->offset_array
[WINE_MOUSE_R_POSITION
], 0x00,
518 hook
->time
, This
->dinput
->evsequence
++);
519 This
->m_state
.rgbButtons
[1] = 0x00;
522 GEN_EVENT(This
->offset_array
[WINE_MOUSE_M_POSITION
], 0x80,
523 hook
->time
, This
->dinput
->evsequence
++);
524 This
->m_state
.rgbButtons
[2] = 0x80;
527 GEN_EVENT(This
->offset_array
[WINE_MOUSE_M_POSITION
], 0x00,
528 hook
->time
, This
->dinput
->evsequence
++);
529 This
->m_state
.rgbButtons
[2] = 0x00;
532 wdata
= (short)HIWORD(hook
->mouseData
);
533 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Z_POSITION
], wdata
,
534 hook
->time
, This
->dinput
->evsequence
++);
535 This
->m_state
.lZ
+= wdata
;
539 TRACE("(X: %ld - Y: %ld L: %02x M: %02x R: %02x)\n",
540 This
->m_state
.lX
, This
->m_state
.lY
,
541 This
->m_state
.rgbButtons
[0], This
->m_state
.rgbButtons
[2], This
->m_state
.rgbButtons
[1]);
544 LeaveCriticalSection(&(This
->crit
));
546 if (dwCoop
& DISCL_NONEXCLUSIVE
) {
547 /* Pass the events down to previous handlers (e.g. win32 input) */
548 ret
= CallNextHookEx( This
->hook
, code
, wparam
, lparam
);
557 static void dinput_window_check(SysMouseImpl
* This
) {
559 DWORD centerX
, centerY
;
561 /* make sure the window hasn't moved */
562 GetWindowRect(This
->win
, &rect
);
563 centerX
= (rect
.right
- rect
.left
) / 2;
564 centerY
= (rect
.bottom
- rect
.top
) / 2;
565 if (This
->win_centerX
!= centerX
|| This
->win_centerY
!= centerY
) {
566 This
->win_centerX
= centerX
;
567 This
->win_centerY
= centerY
;
569 This
->mapped_center
.x
= This
->win_centerX
;
570 This
->mapped_center
.y
= This
->win_centerY
;
571 MapWindowPoints(This
->win
, HWND_DESKTOP
, &This
->mapped_center
, 1);
575 /******************************************************************************
576 * Acquire : gets exclusive control of the mouse
578 static HRESULT WINAPI
SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface
)
580 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
584 TRACE("(this=%p)\n",This
);
591 /* Store (in a global variable) the current lock */
592 current_lock
= (IDirectInputDevice8A
*)This
;
594 /* Init the mouse state */
595 GetCursorPos( &point
);
596 if (This
->absolute
) {
597 This
->m_state
.lX
= point
.x
;
598 This
->m_state
.lY
= point
.y
;
599 This
->prevX
= point
.x
;
600 This
->prevY
= point
.y
;
602 This
->m_state
.lX
= 0;
603 This
->m_state
.lY
= 0;
604 This
->org_coords
= point
;
606 This
->m_state
.lZ
= 0;
607 This
->m_state
.rgbButtons
[0] = ((GetKeyState(VK_LBUTTON
) & 0x80) ? 0xFF : 0x00);
608 This
->m_state
.rgbButtons
[1] = ((GetKeyState(VK_RBUTTON
) & 0x80) ? 0xFF : 0x00);
609 This
->m_state
.rgbButtons
[2] = ((GetKeyState(VK_MBUTTON
) & 0x80) ? 0xFF : 0x00);
611 /* Install our mouse hook */
612 if (This
->dwCoopLevel
& DISCL_EXCLUSIVE
)
613 ShowCursor(FALSE
); /* hide cursor */
614 This
->hook
= SetWindowsHookExA( WH_MOUSE_LL
, dinput_mouse_hook
, DINPUT_instance
, 0 );
616 /* Get the window dimension and find the center */
617 GetWindowRect(This
->win
, &rect
);
618 This
->win_centerX
= (rect
.right
- rect
.left
) / 2;
619 This
->win_centerY
= (rect
.bottom
- rect
.top
) / 2;
621 /* Warp the mouse to the center of the window */
622 if (This
->absolute
== 0) {
623 This
->mapped_center
.x
= This
->win_centerX
;
624 This
->mapped_center
.y
= This
->win_centerY
;
625 MapWindowPoints(This
->win
, HWND_DESKTOP
, &This
->mapped_center
, 1);
626 TRACE("Warping mouse to %ld - %ld\n", This
->mapped_center
.x
, This
->mapped_center
.y
);
627 SetCursorPos( This
->mapped_center
.x
, This
->mapped_center
.y
);
629 This
->need_warp
= WARP_DONE
;
631 This
->need_warp
= WARP_STARTED
;
638 /******************************************************************************
639 * Unacquire : frees the mouse
641 static HRESULT WINAPI
SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface
)
643 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
645 TRACE("(this=%p)\n",This
);
647 if (0 == This
->acquired
) {
651 /* Reinstall previous mouse event handler */
653 UnhookWindowsHookEx( This
->hook
);
656 if (This
->dwCoopLevel
& DISCL_EXCLUSIVE
)
657 ShowCursor(TRUE
); /* show cursor */
661 if (current_lock
== (IDirectInputDevice8A
*) This
)
664 ERR("this(%p) != current_lock(%p)\n", This
, current_lock
);
666 /* Unacquire device */
669 /* And put the mouse cursor back where it was at acquire time */
670 if (This
->absolute
== 0) {
671 TRACE(" warping mouse back to (%ld , %ld)\n", This
->org_coords
.x
, This
->org_coords
.y
);
672 SetCursorPos(This
->org_coords
.x
, This
->org_coords
.y
);
678 /******************************************************************************
679 * GetDeviceState : returns the "state" of the mouse.
681 * For the moment, only the "standard" return structure (DIMOUSESTATE) is
684 static HRESULT WINAPI
SysMouseAImpl_GetDeviceState(
685 LPDIRECTINPUTDEVICE8A iface
,DWORD len
,LPVOID ptr
687 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
689 EnterCriticalSection(&(This
->crit
));
690 TRACE("(this=%p,0x%08lx,%p): \n",This
,len
,ptr
);
692 /* Copy the current mouse state */
693 fill_DataFormat(ptr
, &(This
->m_state
), This
->wine_df
);
695 /* Initialize the buffer when in relative mode */
696 if (This
->absolute
== 0) {
697 This
->m_state
.lX
= 0;
698 This
->m_state
.lY
= 0;
699 This
->m_state
.lZ
= 0;
702 /* Check if we need to do a mouse warping */
703 if (This
->need_warp
== WARP_NEEDED
) {
704 dinput_window_check(This
);
705 TRACE("Warping mouse to %ld - %ld\n", This
->mapped_center
.x
, This
->mapped_center
.y
);
706 SetCursorPos( This
->mapped_center
.x
, This
->mapped_center
.y
);
709 This
->need_warp
= WARP_DONE
;
711 This
->need_warp
= WARP_STARTED
;
715 LeaveCriticalSection(&(This
->crit
));
717 TRACE("(X: %ld - Y: %ld - Z: %ld L: %02x M: %02x R: %02x)\n",
718 This
->m_state
.lX
, This
->m_state
.lY
, This
->m_state
.lZ
,
719 This
->m_state
.rgbButtons
[0], This
->m_state
.rgbButtons
[2], This
->m_state
.rgbButtons
[1]);
724 /******************************************************************************
725 * GetDeviceState : gets buffered input data.
727 static HRESULT WINAPI
SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface
,
729 LPDIDEVICEOBJECTDATA dod
,
733 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
737 TRACE("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx)\n",This
,dodsize
,*entries
,flags
);
739 if (This
->acquired
== 0) {
740 WARN(" application tries to get data from an unacquired device !\n");
741 return DIERR_NOTACQUIRED
;
744 EnterCriticalSection(&(This
->crit
));
746 len
= ((This
->queue_head
< This
->queue_tail
) ? This
->queue_len
: 0)
747 + (This
->queue_head
- This
->queue_tail
);
748 if (len
> *entries
) len
= *entries
;
752 TRACE("Application discarding %ld event(s).\n", len
);
755 nqtail
= This
->queue_tail
+ len
;
756 while (nqtail
>= This
->queue_len
) nqtail
-= This
->queue_len
;
758 if (dodsize
< sizeof(DIDEVICEOBJECTDATA_DX3
)) {
759 ERR("Wrong structure size !\n");
760 LeaveCriticalSection(&(This
->crit
));
761 return DIERR_INVALIDPARAM
;
765 TRACE("Application retrieving %ld event(s).\n", len
);
768 nqtail
= This
->queue_tail
;
770 /* Copy the buffered data into the application queue */
771 memcpy((char *)dod
+ *entries
* dodsize
, This
->data_queue
+ nqtail
, dodsize
);
772 /* Advance position */
774 if (nqtail
>= This
->queue_len
)
775 nqtail
-= This
->queue_len
;
780 if (!(flags
& DIGDD_PEEK
))
781 This
->queue_tail
= nqtail
;
783 LeaveCriticalSection(&(This
->crit
));
785 /* Check if we need to do a mouse warping */
786 if (This
->need_warp
== WARP_NEEDED
) {
787 dinput_window_check(This
);
788 TRACE("Warping mouse to %ld - %ld\n", This
->mapped_center
.x
, This
->mapped_center
.y
);
789 SetCursorPos( This
->mapped_center
.x
, This
->mapped_center
.y
);
792 This
->need_warp
= WARP_DONE
;
794 This
->need_warp
= WARP_STARTED
;
800 /******************************************************************************
801 * SetProperty : change input device properties
803 static HRESULT WINAPI
SysMouseAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface
,
807 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
809 TRACE("(this=%p,%s,%p)\n",This
,debugstr_guid(rguid
),ph
);
811 if (!HIWORD(rguid
)) {
812 switch ((DWORD
)rguid
) {
813 case (DWORD
) DIPROP_BUFFERSIZE
: {
814 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
816 TRACE("buffersize = %ld\n",pd
->dwData
);
818 This
->data_queue
= HeapAlloc(GetProcessHeap(),0, pd
->dwData
* sizeof(DIDEVICEOBJECTDATA
));
819 This
->queue_head
= 0;
820 This
->queue_tail
= 0;
821 This
->queue_len
= pd
->dwData
;
824 case (DWORD
) DIPROP_AXISMODE
: {
825 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
826 This
->absolute
= !(pd
->dwData
);
827 TRACE("Using %s coordinates mode now\n", This
->absolute
? "absolute" : "relative");
831 FIXME("Unknown type %ld (%s)\n",(DWORD
)rguid
,debugstr_guid(rguid
));
839 /******************************************************************************
840 * GetProperty : get input device properties
842 static HRESULT WINAPI
SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface
,
844 LPDIPROPHEADER pdiph
)
846 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
848 TRACE("(this=%p,%s,%p)\n",
849 iface
, debugstr_guid(rguid
), pdiph
);
851 if (TRACE_ON(dinput
))
852 _dump_DIPROPHEADER(pdiph
);
854 if (!HIWORD(rguid
)) {
855 switch ((DWORD
)rguid
) {
856 case (DWORD
) DIPROP_BUFFERSIZE
: {
857 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
859 TRACE(" return buffersize = %d\n",This
->queue_len
);
860 pd
->dwData
= This
->queue_len
;
864 case (DWORD
) DIPROP_GRANULARITY
: {
865 LPDIPROPDWORD pr
= (LPDIPROPDWORD
) pdiph
;
867 /* We'll just assume that the app asks about the Z axis */
868 pr
->dwData
= WHEEL_DELTA
;
873 case (DWORD
) DIPROP_RANGE
: {
874 LPDIPROPRANGE pr
= (LPDIPROPRANGE
) pdiph
;
876 if ((pdiph
->dwHow
== DIPH_BYID
) &&
877 ((pdiph
->dwObj
== (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
)) ||
878 (pdiph
->dwObj
== (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
)))) {
879 /* Querying the range of either the X or the Y axis. As I do
880 not know the range, do as if the range were
882 pr
->lMin
= DIPROPRANGE_NOMIN
;
883 pr
->lMax
= DIPROPRANGE_NOMAX
;
890 FIXME("Unknown type %ld (%s)\n",(DWORD
)rguid
,debugstr_guid(rguid
));
900 /******************************************************************************
901 * SetEventNotification : specifies event to be sent on state change
903 static HRESULT WINAPI
SysMouseAImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface
,
905 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
907 TRACE("(this=%p,0x%08lx)\n",This
,(DWORD
)hnd
);
914 /******************************************************************************
915 * GetCapabilities : get the device capablitites
917 static HRESULT WINAPI
SysMouseAImpl_GetCapabilities(
918 LPDIRECTINPUTDEVICE8A iface
,
919 LPDIDEVCAPS lpDIDevCaps
)
921 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
924 TRACE("(this=%p,%p)\n",This
,lpDIDevCaps
);
926 if ((lpDIDevCaps
->dwSize
!= sizeof(DIDEVCAPS
)) && (lpDIDevCaps
->dwSize
!= sizeof(DIDEVCAPS_DX3
))) {
927 WARN("invalid parameter\n");
928 return DIERR_INVALIDPARAM
;
931 devcaps
.dwSize
= lpDIDevCaps
->dwSize
;
932 devcaps
.dwFlags
= DIDC_ATTACHED
;
933 if (This
->dinput
->version
>= 8)
934 devcaps
.dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
936 devcaps
.dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
938 devcaps
.dwButtons
= 3;
940 devcaps
.dwFFSamplePeriod
= 0;
941 devcaps
.dwFFMinTimeResolution
= 0;
942 devcaps
.dwFirmwareRevision
= 100;
943 devcaps
.dwHardwareRevision
= 100;
944 devcaps
.dwFFDriverVersion
= 0;
946 memcpy(lpDIDevCaps
, &devcaps
, lpDIDevCaps
->dwSize
);
952 /******************************************************************************
953 * EnumObjects : enumerate the different buttons and axis...
955 static HRESULT WINAPI
SysMouseAImpl_EnumObjects(
956 LPDIRECTINPUTDEVICE8A iface
,
957 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback
,
961 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
962 DIDEVICEOBJECTINSTANCEA ddoi
;
964 TRACE("(this=%p,%p,%p,%08lx)\n", This
, lpCallback
, lpvRef
, dwFlags
);
965 if (TRACE_ON(dinput
)) {
966 TRACE(" - flags = ");
967 _dump_EnumObjects_flags(dwFlags
);
971 /* Only the fields till dwFFMaxForce are relevant */
972 memset(&ddoi
, 0, sizeof(ddoi
));
973 ddoi
.dwSize
= FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA
, dwFFMaxForce
);
975 /* In a mouse, we have : two relative axis and three buttons */
976 if ((dwFlags
== DIDFT_ALL
) ||
977 (dwFlags
& DIDFT_AXIS
)) {
979 ddoi
.guidType
= GUID_XAxis
;
980 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_X_POSITION
];
981 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
982 strcpy(ddoi
.tszName
, "X-Axis");
983 _dump_OBJECTINSTANCEA(&ddoi
);
984 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
987 ddoi
.guidType
= GUID_YAxis
;
988 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_Y_POSITION
];
989 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
990 strcpy(ddoi
.tszName
, "Y-Axis");
991 _dump_OBJECTINSTANCEA(&ddoi
);
992 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
995 ddoi
.guidType
= GUID_ZAxis
;
996 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_Z_POSITION
];
997 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
998 strcpy(ddoi
.tszName
, "Z-Axis");
999 _dump_OBJECTINSTANCEA(&ddoi
);
1000 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
1003 if ((dwFlags
== DIDFT_ALL
) ||
1004 (dwFlags
& DIDFT_BUTTON
)) {
1005 ddoi
.guidType
= GUID_Button
;
1008 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_L_POSITION
];
1009 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
;
1010 strcpy(ddoi
.tszName
, "Left-Button");
1011 _dump_OBJECTINSTANCEA(&ddoi
);
1012 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
1015 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_R_POSITION
];
1016 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
;
1017 strcpy(ddoi
.tszName
, "Right-Button");
1018 _dump_OBJECTINSTANCEA(&ddoi
);
1019 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
1022 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_M_POSITION
];
1023 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
;
1024 strcpy(ddoi
.tszName
, "Middle-Button");
1025 _dump_OBJECTINSTANCEA(&ddoi
);
1026 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
1032 static HRESULT WINAPI
SysMouseWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface
, LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback
, LPVOID lpvRef
,DWORD dwFlags
)
1034 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
1036 device_enumobjects_AtoWcb_data data
;
1038 data
.lpCallBack
= lpCallback
;
1039 data
.lpvRef
= lpvRef
;
1041 return SysMouseAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A
) This
, (LPDIENUMDEVICEOBJECTSCALLBACKA
) DIEnumDevicesCallbackAtoW
, (LPVOID
) &data
, dwFlags
);
1044 /******************************************************************************
1045 * GetDeviceInfo : get information about a device's identity
1047 static HRESULT WINAPI
SysMouseAImpl_GetDeviceInfo(
1048 LPDIRECTINPUTDEVICE8A iface
,
1049 LPDIDEVICEINSTANCEA pdidi
)
1051 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
1052 TRACE("(this=%p,%p)\n", This
, pdidi
);
1054 if (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
)) {
1055 WARN(" dinput3 not supporte yet...\n");
1059 fill_mouse_dideviceinstanceA(pdidi
, This
->dinput
->version
);
1064 static HRESULT WINAPI
SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface
, LPDIDEVICEINSTANCEW pdidi
)
1066 SysMouseImpl
*This
= (SysMouseImpl
*)iface
;
1067 TRACE("(this=%p,%p)\n", This
, pdidi
);
1069 if (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
)) {
1070 WARN(" dinput3 not supporte yet...\n");
1074 fill_mouse_dideviceinstanceW(pdidi
, This
->dinput
->version
);
1080 static IDirectInputDevice8AVtbl SysMouseAvt
=
1082 IDirectInputDevice2AImpl_QueryInterface
,
1083 IDirectInputDevice2AImpl_AddRef
,
1084 SysMouseAImpl_Release
,
1085 SysMouseAImpl_GetCapabilities
,
1086 SysMouseAImpl_EnumObjects
,
1087 SysMouseAImpl_GetProperty
,
1088 SysMouseAImpl_SetProperty
,
1089 SysMouseAImpl_Acquire
,
1090 SysMouseAImpl_Unacquire
,
1091 SysMouseAImpl_GetDeviceState
,
1092 SysMouseAImpl_GetDeviceData
,
1093 SysMouseAImpl_SetDataFormat
,
1094 SysMouseAImpl_SetEventNotification
,
1095 SysMouseAImpl_SetCooperativeLevel
,
1096 IDirectInputDevice2AImpl_GetObjectInfo
,
1097 SysMouseAImpl_GetDeviceInfo
,
1098 IDirectInputDevice2AImpl_RunControlPanel
,
1099 IDirectInputDevice2AImpl_Initialize
,
1100 IDirectInputDevice2AImpl_CreateEffect
,
1101 IDirectInputDevice2AImpl_EnumEffects
,
1102 IDirectInputDevice2AImpl_GetEffectInfo
,
1103 IDirectInputDevice2AImpl_GetForceFeedbackState
,
1104 IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1105 IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1106 IDirectInputDevice2AImpl_Escape
,
1107 IDirectInputDevice2AImpl_Poll
,
1108 IDirectInputDevice2AImpl_SendDeviceData
,
1109 IDirectInputDevice7AImpl_EnumEffectsInFile
,
1110 IDirectInputDevice7AImpl_WriteEffectToFile
,
1111 IDirectInputDevice8AImpl_BuildActionMap
,
1112 IDirectInputDevice8AImpl_SetActionMap
,
1113 IDirectInputDevice8AImpl_GetImageInfo
1116 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1117 # define XCAST(fun) (typeof(SysMouseWvt.fun))
1119 # define XCAST(fun) (void*)
1122 static IDirectInputDevice8WVtbl SysMouseWvt
=
1124 IDirectInputDevice2WImpl_QueryInterface
,
1125 XCAST(AddRef
)IDirectInputDevice2AImpl_AddRef
,
1126 XCAST(Release
)SysMouseAImpl_Release
,
1127 XCAST(GetCapabilities
)SysMouseAImpl_GetCapabilities
,
1128 SysMouseWImpl_EnumObjects
,
1129 XCAST(GetProperty
)SysMouseAImpl_GetProperty
,
1130 XCAST(SetProperty
)SysMouseAImpl_SetProperty
,
1131 XCAST(Acquire
)SysMouseAImpl_Acquire
,
1132 XCAST(Unacquire
)SysMouseAImpl_Unacquire
,
1133 XCAST(GetDeviceState
)SysMouseAImpl_GetDeviceState
,
1134 XCAST(GetDeviceData
)SysMouseAImpl_GetDeviceData
,
1135 XCAST(SetDataFormat
)SysMouseAImpl_SetDataFormat
,
1136 XCAST(SetEventNotification
)SysMouseAImpl_SetEventNotification
,
1137 XCAST(SetCooperativeLevel
)SysMouseAImpl_SetCooperativeLevel
,
1138 IDirectInputDevice2WImpl_GetObjectInfo
,
1139 SysMouseWImpl_GetDeviceInfo
,
1140 XCAST(RunControlPanel
)IDirectInputDevice2AImpl_RunControlPanel
,
1141 XCAST(Initialize
)IDirectInputDevice2AImpl_Initialize
,
1142 XCAST(CreateEffect
)IDirectInputDevice2AImpl_CreateEffect
,
1143 IDirectInputDevice2WImpl_EnumEffects
,
1144 IDirectInputDevice2WImpl_GetEffectInfo
,
1145 XCAST(GetForceFeedbackState
)IDirectInputDevice2AImpl_GetForceFeedbackState
,
1146 XCAST(SendForceFeedbackCommand
)IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1147 XCAST(EnumCreatedEffectObjects
)IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1148 XCAST(Escape
)IDirectInputDevice2AImpl_Escape
,
1149 XCAST(Poll
)IDirectInputDevice2AImpl_Poll
,
1150 XCAST(SendDeviceData
)IDirectInputDevice2AImpl_SendDeviceData
,
1151 IDirectInputDevice7WImpl_EnumEffectsInFile
,
1152 IDirectInputDevice7WImpl_WriteEffectToFile
,
1153 IDirectInputDevice8WImpl_BuildActionMap
,
1154 IDirectInputDevice8WImpl_SetActionMap
,
1155 IDirectInputDevice8WImpl_GetImageInfo