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
;
136 /* warping: whether we need to move mouse back to middle once we
137 * reach window borders (for e.g. shooters, "surface movement" games) */
138 WARP_STATUS need_warp
;
141 CRITICAL_SECTION crit
;
143 /* This is for mouse reporting. */
144 Wine_InternalMouseData m_state
;
147 /* FIXME: This is ugly and not thread safe :/ */
148 static IDirectInputDevice8A
* current_lock
= NULL
;
150 static GUID DInput_Wine_Mouse_GUID
= { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
154 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
157 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi
, int version
) {
159 DIDEVICEINSTANCEA ddi
;
161 dwSize
= lpddi
->dwSize
;
163 TRACE("%ld %p\n", dwSize
, lpddi
);
165 memset(lpddi
, 0, dwSize
);
166 memset(&ddi
, 0, sizeof(ddi
));
169 ddi
.guidInstance
= GUID_SysMouse
;/* DInput's GUID */
170 ddi
.guidProduct
= DInput_Wine_Mouse_GUID
; /* Vendor's GUID */
172 ddi
.dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
174 ddi
.dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
175 strcpy(ddi
.tszInstanceName
, "Mouse");
176 strcpy(ddi
.tszProductName
, "Wine Mouse");
178 memcpy(lpddi
, &ddi
, (dwSize
< sizeof(ddi
) ? dwSize
: sizeof(ddi
)));
181 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi
, int version
) {
183 DIDEVICEINSTANCEW ddi
;
185 dwSize
= lpddi
->dwSize
;
187 TRACE("%ld %p\n", dwSize
, lpddi
);
189 memset(lpddi
, 0, dwSize
);
190 memset(&ddi
, 0, sizeof(ddi
));
193 ddi
.guidInstance
= GUID_SysMouse
;/* DInput's GUID */
194 ddi
.guidProduct
= DInput_Wine_Mouse_GUID
; /* Vendor's GUID */
196 ddi
.dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
198 ddi
.dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
199 MultiByteToWideChar(CP_ACP
, 0, "Mouse", -1, ddi
.tszInstanceName
, MAX_PATH
);
200 MultiByteToWideChar(CP_ACP
, 0, "Wine Mouse", -1, ddi
.tszProductName
, MAX_PATH
);
202 memcpy(lpddi
, &ddi
, (dwSize
< sizeof(ddi
) ? dwSize
: sizeof(ddi
)));
205 static BOOL
mousedev_enum_deviceA(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEA lpddi
, int version
)
207 if ((dwDevType
== 0) ||
208 ((dwDevType
== DIDEVTYPE_MOUSE
) && (version
< 8)) ||
209 ((dwDevType
== DI8DEVTYPE_MOUSE
) && (version
>= 8))) {
210 TRACE("Enumerating the mouse device\n");
212 fill_mouse_dideviceinstanceA(lpddi
, version
);
220 static BOOL
mousedev_enum_deviceW(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEW lpddi
, int version
)
222 if ((dwDevType
== 0) ||
223 ((dwDevType
== DIDEVTYPE_MOUSE
) && (version
< 8)) ||
224 ((dwDevType
== DI8DEVTYPE_MOUSE
) && (version
>= 8))) {
225 TRACE("Enumerating the mouse device\n");
227 fill_mouse_dideviceinstanceW(lpddi
, version
);
235 static SysMouseImpl
*alloc_device(REFGUID rguid
, LPVOID mvt
, IDirectInputImpl
*dinput
)
237 int offset_array
[WINE_INTERNALMOUSE_NUM_OBJS
] = {
238 FIELD_OFFSET(Wine_InternalMouseData
, lX
),
239 FIELD_OFFSET(Wine_InternalMouseData
, lY
),
240 FIELD_OFFSET(Wine_InternalMouseData
, lZ
),
241 FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
) + 0,
242 FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
) + 1,
243 FIELD_OFFSET(Wine_InternalMouseData
, rgbButtons
) + 2
245 SysMouseImpl
* newDevice
;
246 newDevice
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(SysMouseImpl
));
248 newDevice
->lpVtbl
= mvt
;
249 InitializeCriticalSection(&(newDevice
->crit
));
250 memcpy(&(newDevice
->guid
),rguid
,sizeof(*rguid
));
252 /* Per default, Wine uses its internal data format */
253 newDevice
->df
= (DIDATAFORMAT
*) &Wine_InternalMouseFormat
;
254 memcpy(newDevice
->offset_array
, offset_array
, WINE_INTERNALMOUSE_NUM_OBJS
* sizeof(int));
255 newDevice
->wine_df
= (DataFormat
*) HeapAlloc(GetProcessHeap(), 0, sizeof(DataFormat
));
256 newDevice
->wine_df
->size
= 0;
257 newDevice
->wine_df
->internal_format_size
= Wine_InternalMouseFormat
.dwDataSize
;
258 newDevice
->wine_df
->dt
= NULL
;
259 newDevice
->dinput
= dinput
;
264 static HRESULT
mousedev_create_deviceA(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEA
* pdev
)
266 if ((IsEqualGUID(&GUID_SysMouse
,rguid
)) || /* Generic Mouse */
267 (IsEqualGUID(&DInput_Wine_Mouse_GUID
,rguid
))) { /* Wine Mouse */
268 if ((riid
== NULL
) ||
269 IsEqualGUID(&IID_IDirectInputDeviceA
,riid
) ||
270 IsEqualGUID(&IID_IDirectInputDevice2A
,riid
) ||
271 IsEqualGUID(&IID_IDirectInputDevice7A
,riid
) ||
272 IsEqualGUID(&IID_IDirectInputDevice8A
,riid
)) {
273 *pdev
= (IDirectInputDeviceA
*) alloc_device(rguid
, &SysMouseAvt
, dinput
);
274 TRACE("Creating a Mouse device (%p)\n", *pdev
);
277 return DIERR_NOINTERFACE
;
280 return DIERR_DEVICENOTREG
;
283 static HRESULT
mousedev_create_deviceW(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEW
* pdev
)
285 if ((IsEqualGUID(&GUID_SysMouse
,rguid
)) || /* Generic Mouse */
286 (IsEqualGUID(&DInput_Wine_Mouse_GUID
,rguid
))) { /* Wine Mouse */
287 if ((riid
== NULL
) ||
288 IsEqualGUID(&IID_IDirectInputDeviceW
,riid
) ||
289 IsEqualGUID(&IID_IDirectInputDevice2W
,riid
) ||
290 IsEqualGUID(&IID_IDirectInputDevice7W
,riid
) ||
291 IsEqualGUID(&IID_IDirectInputDevice8W
,riid
)) {
292 *pdev
= (IDirectInputDeviceW
*) alloc_device(rguid
, &SysMouseWvt
, dinput
);
293 TRACE("Creating a Mouse device (%p)\n", *pdev
);
296 return DIERR_NOINTERFACE
;
299 return DIERR_DEVICENOTREG
;
301 static dinput_device mousedev
= {
304 mousedev_enum_deviceA
,
305 mousedev_enum_deviceW
,
306 mousedev_create_deviceA
,
307 mousedev_create_deviceW
310 DECL_GLOBAL_CONSTRUCTOR(mousedev_register
) { dinput_register_device(&mousedev
); }
312 /******************************************************************************
313 * SysMouseA (DInput Mouse support)
316 /******************************************************************************
317 * Release : release the mouse buffer.
319 static ULONG WINAPI
SysMouseAImpl_Release(LPDIRECTINPUTDEVICE8A iface
)
321 ICOM_THIS(SysMouseImpl
,iface
);
327 /* Free the data queue */
328 if (This
->data_queue
!= NULL
)
329 HeapFree(GetProcessHeap(),0,This
->data_queue
);
332 UnhookWindowsHookEx( This
->hook
);
333 if (This
->dwCoopLevel
& DISCL_EXCLUSIVE
)
334 ShowCursor(TRUE
); /* show cursor */
336 DeleteCriticalSection(&(This
->crit
));
338 /* Free the DataFormat */
339 if (This
->df
!= &(Wine_InternalMouseFormat
)) {
340 HeapFree(GetProcessHeap(), 0, This
->df
->rgodf
);
341 HeapFree(GetProcessHeap(), 0, This
->df
);
344 HeapFree(GetProcessHeap(),0,This
);
349 /******************************************************************************
350 * SetCooperativeLevel : store the window in which we will do our
353 static HRESULT WINAPI
SysMouseAImpl_SetCooperativeLevel(
354 LPDIRECTINPUTDEVICE8A iface
,HWND hwnd
,DWORD dwflags
357 ICOM_THIS(SysMouseImpl
,iface
);
359 TRACE("(this=%p,0x%08lx,0x%08lx)\n",This
,(DWORD
)hwnd
,dwflags
);
361 if (TRACE_ON(dinput
)) {
362 TRACE(" cooperative level : ");
363 _dump_cooperativelevel_DI(dwflags
);
366 /* Store the window which asks for the mouse */
368 hwnd
= GetDesktopWindow();
370 This
->dwCoopLevel
= dwflags
;
376 /******************************************************************************
377 * SetDataFormat : the application can choose the format of the data
378 * the device driver sends back with GetDeviceState.
380 * For the moment, only the "standard" configuration (c_dfDIMouse) is supported
381 * in absolute and relative mode.
383 static HRESULT WINAPI
SysMouseAImpl_SetDataFormat(
384 LPDIRECTINPUTDEVICE8A iface
,LPCDIDATAFORMAT df
387 ICOM_THIS(SysMouseImpl
,iface
);
389 TRACE("(this=%p,%p)\n",This
,df
);
391 _dump_DIDATAFORMAT(df
);
393 /* Tests under windows show that a call to SetDataFormat always sets the mouse
394 in relative mode whatever the dwFlags value (DIDF_ABSAXIS/DIDF_RELAXIS).
395 To switch in absolute mode, SetProperty must be used. */
398 /* Store the new data format */
399 This
->df
= HeapAlloc(GetProcessHeap(),0,df
->dwSize
);
400 memcpy(This
->df
, df
, df
->dwSize
);
401 This
->df
->rgodf
= HeapAlloc(GetProcessHeap(),0,df
->dwNumObjs
*df
->dwObjSize
);
402 memcpy(This
->df
->rgodf
,df
->rgodf
,df
->dwNumObjs
*df
->dwObjSize
);
404 /* Prepare all the data-conversion filters */
405 This
->wine_df
= create_DataFormat(&(Wine_InternalMouseFormat
), df
, This
->offset_array
);
410 /* low-level mouse hook */
411 static LRESULT CALLBACK
dinput_mouse_hook( int code
, WPARAM wparam
, LPARAM lparam
)
414 MSLLHOOKSTRUCT
*hook
= (MSLLHOOKSTRUCT
*)lparam
;
415 SysMouseImpl
* This
= (SysMouseImpl
*) current_lock
;
417 static long last_event
= 0;
420 if (code
!= HC_ACTION
) return CallNextHookEx( This
->hook
, code
, wparam
, lparam
);
422 EnterCriticalSection(&(This
->crit
));
423 dwCoop
= This
->dwCoopLevel
;
425 /* Only allow mouse events every 10 ms.
426 * This is to allow the cursor to start acceleration before
427 * the warps happen. But if it involves a mouse button event we
428 * allow it since we don't want to loose the clicks.
430 if (((GetCurrentTime() - last_event
) < 10)
431 && wparam
== WM_MOUSEMOVE
)
433 else last_event
= GetCurrentTime();
435 /* Mouse moved -> send event if asked */
437 SetEvent(This
->hEvent
);
439 if (wparam
== WM_MOUSEMOVE
) {
440 if (This
->absolute
) {
441 if (hook
->pt
.x
!= This
->prevX
)
442 GEN_EVENT(This
->offset_array
[WINE_MOUSE_X_POSITION
], hook
->pt
.x
, hook
->time
, 0);
443 if (hook
->pt
.y
!= This
->prevY
)
444 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Y_POSITION
], hook
->pt
.y
, hook
->time
, 0);
446 /* Now, warp handling */
447 if ((This
->need_warp
== WARP_STARTED
) &&
448 (hook
->pt
.x
== This
->mapped_center
.x
) && (hook
->pt
.y
== This
->mapped_center
.y
)) {
449 /* Warp has been done... */
450 This
->need_warp
= WARP_DONE
;
454 /* Relative mouse input with absolute mouse event : the real fun starts here... */
455 if ((This
->need_warp
== WARP_NEEDED
) ||
456 (This
->need_warp
== WARP_STARTED
)) {
457 if (hook
->pt
.x
!= This
->prevX
)
458 GEN_EVENT(This
->offset_array
[WINE_MOUSE_X_POSITION
], hook
->pt
.x
- This
->prevX
,
459 hook
->time
, (This
->dinput
->evsequence
)++);
460 if (hook
->pt
.y
!= This
->prevY
)
461 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Y_POSITION
], hook
->pt
.y
- This
->prevY
,
462 hook
->time
, (This
->dinput
->evsequence
)++);
464 /* This is the first time the event handler has been called after a
465 GetDeviceData or GetDeviceState. */
466 if (hook
->pt
.x
!= This
->mapped_center
.x
) {
467 GEN_EVENT(This
->offset_array
[WINE_MOUSE_X_POSITION
], hook
->pt
.x
- This
->mapped_center
.x
,
468 hook
->time
, (This
->dinput
->evsequence
)++);
469 This
->need_warp
= WARP_NEEDED
;
472 if (hook
->pt
.y
!= This
->mapped_center
.y
) {
473 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Y_POSITION
], hook
->pt
.y
- This
->mapped_center
.y
,
474 hook
->time
, (This
->dinput
->evsequence
)++);
475 This
->need_warp
= WARP_NEEDED
;
480 This
->prevX
= hook
->pt
.x
;
481 This
->prevY
= hook
->pt
.y
;
483 if (This
->absolute
) {
484 This
->m_state
.lX
= hook
->pt
.x
;
485 This
->m_state
.lY
= hook
->pt
.y
;
487 This
->m_state
.lX
= hook
->pt
.x
- This
->mapped_center
.x
;
488 This
->m_state
.lY
= hook
->pt
.y
- This
->mapped_center
.y
;
492 TRACE(" msg %x pt %ld %ld (W=%d)\n",
493 wparam
, hook
->pt
.x
, hook
->pt
.y
, (!This
->absolute
) && This
->need_warp
);
497 GEN_EVENT(This
->offset_array
[WINE_MOUSE_L_POSITION
], 0xFF,
498 hook
->time
, This
->dinput
->evsequence
++);
499 This
->m_state
.rgbButtons
[0] = 0xFF;
502 GEN_EVENT(This
->offset_array
[WINE_MOUSE_L_POSITION
], 0x00,
503 hook
->time
, This
->dinput
->evsequence
++);
504 This
->m_state
.rgbButtons
[0] = 0x00;
507 GEN_EVENT(This
->offset_array
[WINE_MOUSE_R_POSITION
], 0xFF,
508 hook
->time
, This
->dinput
->evsequence
++);
509 This
->m_state
.rgbButtons
[1] = 0xFF;
512 GEN_EVENT(This
->offset_array
[WINE_MOUSE_R_POSITION
], 0x00,
513 hook
->time
, This
->dinput
->evsequence
++);
514 This
->m_state
.rgbButtons
[1] = 0x00;
517 GEN_EVENT(This
->offset_array
[WINE_MOUSE_M_POSITION
], 0xFF,
518 hook
->time
, This
->dinput
->evsequence
++);
519 This
->m_state
.rgbButtons
[2] = 0xFF;
522 GEN_EVENT(This
->offset_array
[WINE_MOUSE_M_POSITION
], 0x00,
523 hook
->time
, This
->dinput
->evsequence
++);
524 This
->m_state
.rgbButtons
[2] = 0x00;
527 wdata
= (short)HIWORD(hook
->mouseData
);
528 GEN_EVENT(This
->offset_array
[WINE_MOUSE_Z_POSITION
], wdata
,
529 hook
->time
, This
->dinput
->evsequence
++);
530 This
->m_state
.lZ
+= wdata
;
534 TRACE("(X: %ld - Y: %ld L: %02x M: %02x R: %02x)\n",
535 This
->m_state
.lX
, This
->m_state
.lY
,
536 This
->m_state
.rgbButtons
[0], This
->m_state
.rgbButtons
[2], This
->m_state
.rgbButtons
[1]);
539 LeaveCriticalSection(&(This
->crit
));
541 if (dwCoop
& DISCL_NONEXCLUSIVE
) {
542 /* Pass the events down to previous handlers (e.g. win32 input) */
543 ret
= CallNextHookEx( This
->hook
, code
, wparam
, lparam
);
552 static void dinput_window_check(SysMouseImpl
* This
) {
554 DWORD centerX
, centerY
;
556 /* make sure the window hasn't moved */
557 GetWindowRect(This
->win
, &rect
);
558 centerX
= (rect
.right
- rect
.left
) / 2;
559 centerY
= (rect
.bottom
- rect
.top
) / 2;
560 if (This
->win_centerX
!= centerX
|| This
->win_centerY
!= centerY
) {
561 This
->win_centerX
= centerX
;
562 This
->win_centerY
= centerY
;
564 This
->mapped_center
.x
= This
->win_centerX
;
565 This
->mapped_center
.y
= This
->win_centerY
;
566 MapWindowPoints(This
->win
, HWND_DESKTOP
, &This
->mapped_center
, 1);
570 /******************************************************************************
571 * Acquire : gets exclusive control of the mouse
573 static HRESULT WINAPI
SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface
)
575 ICOM_THIS(SysMouseImpl
,iface
);
578 TRACE("(this=%p)\n",This
);
580 if (This
->acquired
== 0) {
583 /* Store (in a global variable) the current lock */
584 current_lock
= (IDirectInputDevice8A
*)This
;
586 /* Init the mouse state */
587 GetCursorPos( &point
);
588 if (This
->absolute
) {
589 This
->m_state
.lX
= point
.x
;
590 This
->m_state
.lY
= point
.y
;
591 This
->prevX
= point
.x
;
592 This
->prevY
= point
.y
;
594 This
->m_state
.lX
= 0;
595 This
->m_state
.lY
= 0;
596 This
->org_coords
= point
;
598 This
->m_state
.lZ
= 0;
599 This
->m_state
.rgbButtons
[0] = ((GetKeyState(VK_LBUTTON
) & 0x80) ? 0xFF : 0x00);
600 This
->m_state
.rgbButtons
[1] = ((GetKeyState(VK_RBUTTON
) & 0x80) ? 0xFF : 0x00);
601 This
->m_state
.rgbButtons
[2] = ((GetKeyState(VK_MBUTTON
) & 0x80) ? 0xFF : 0x00);
603 /* Install our mouse hook */
604 if (This
->dwCoopLevel
& DISCL_EXCLUSIVE
)
605 ShowCursor(FALSE
); /* hide cursor */
606 This
->hook
= SetWindowsHookExA( WH_MOUSE_LL
, dinput_mouse_hook
, DINPUT_instance
, 0 );
608 /* Get the window dimension and find the center */
609 GetWindowRect(This
->win
, &rect
);
610 This
->win_centerX
= (rect
.right
- rect
.left
) / 2;
611 This
->win_centerY
= (rect
.bottom
- rect
.top
) / 2;
613 /* Warp the mouse to the center of the window */
614 if (This
->absolute
== 0) {
615 This
->mapped_center
.x
= This
->win_centerX
;
616 This
->mapped_center
.y
= This
->win_centerY
;
617 MapWindowPoints(This
->win
, HWND_DESKTOP
, &This
->mapped_center
, 1);
618 TRACE("Warping mouse to %ld - %ld\n", This
->mapped_center
.x
, This
->mapped_center
.y
);
619 SetCursorPos( This
->mapped_center
.x
, This
->mapped_center
.y
);
621 This
->need_warp
= WARP_DONE
;
623 This
->need_warp
= WARP_STARTED
;
633 /******************************************************************************
634 * Unacquire : frees the mouse
636 static HRESULT WINAPI
SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface
)
638 ICOM_THIS(SysMouseImpl
,iface
);
640 TRACE("(this=%p)\n",This
);
642 if (This
->acquired
) {
643 /* Reinstall previous mouse event handler */
645 UnhookWindowsHookEx( This
->hook
);
648 if (This
->dwCoopLevel
& DISCL_EXCLUSIVE
)
649 ShowCursor(TRUE
); /* show cursor */
655 /* Unacquire device */
658 /* And put the mouse cursor back where it was at acquire time */
659 if (This
->absolute
== 0) {
660 TRACE(" warping mouse back to (%ld , %ld)\n", This
->org_coords
.x
, This
->org_coords
.y
);
661 SetCursorPos(This
->org_coords
.x
, This
->org_coords
.y
);
670 /******************************************************************************
671 * GetDeviceState : returns the "state" of the mouse.
673 * For the moment, only the "standard" return structure (DIMOUSESTATE) is
676 static HRESULT WINAPI
SysMouseAImpl_GetDeviceState(
677 LPDIRECTINPUTDEVICE8A iface
,DWORD len
,LPVOID ptr
679 ICOM_THIS(SysMouseImpl
,iface
);
681 EnterCriticalSection(&(This
->crit
));
682 TRACE("(this=%p,0x%08lx,%p): \n",This
,len
,ptr
);
684 /* Copy the current mouse state */
685 fill_DataFormat(ptr
, &(This
->m_state
), This
->wine_df
);
687 /* Initialize the buffer when in relative mode */
688 if (This
->absolute
== 0) {
689 This
->m_state
.lX
= 0;
690 This
->m_state
.lY
= 0;
691 This
->m_state
.lZ
= 0;
694 /* Check if we need to do a mouse warping */
695 if (This
->need_warp
== WARP_NEEDED
) {
696 dinput_window_check(This
);
697 TRACE("Warping mouse to %ld - %ld\n", This
->mapped_center
.x
, This
->mapped_center
.y
);
698 SetCursorPos( This
->mapped_center
.x
, This
->mapped_center
.y
);
701 This
->need_warp
= WARP_DONE
;
703 This
->need_warp
= WARP_STARTED
;
707 LeaveCriticalSection(&(This
->crit
));
709 TRACE("(X: %ld - Y: %ld - Z: %ld L: %02x M: %02x R: %02x)\n",
710 This
->m_state
.lX
, This
->m_state
.lY
, This
->m_state
.lZ
,
711 This
->m_state
.rgbButtons
[0], This
->m_state
.rgbButtons
[2], This
->m_state
.rgbButtons
[1]);
716 /******************************************************************************
717 * GetDeviceState : gets buffered input data.
719 static HRESULT WINAPI
SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface
,
721 LPDIDEVICEOBJECTDATA dod
,
725 ICOM_THIS(SysMouseImpl
,iface
);
729 TRACE("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx)\n",This
,dodsize
,*entries
,flags
);
731 if (This
->acquired
== 0) {
732 WARN(" application tries to get data from an unacquired device !\n");
733 return DIERR_NOTACQUIRED
;
736 EnterCriticalSection(&(This
->crit
));
738 len
= ((This
->queue_head
< This
->queue_tail
) ? This
->queue_len
: 0)
739 + (This
->queue_head
- This
->queue_tail
);
740 if (len
> *entries
) len
= *entries
;
744 TRACE("Application discarding %ld event(s).\n", len
);
747 nqtail
= This
->queue_tail
+ len
;
748 while (nqtail
>= This
->queue_len
) nqtail
-= This
->queue_len
;
750 if (dodsize
< sizeof(DIDEVICEOBJECTDATA_DX3
)) {
751 ERR("Wrong structure size !\n");
752 LeaveCriticalSection(&(This
->crit
));
753 return DIERR_INVALIDPARAM
;
757 TRACE("Application retrieving %ld event(s).\n", len
);
760 nqtail
= This
->queue_tail
;
762 DWORD span
= ((This
->queue_head
< nqtail
) ? This
->queue_len
: This
->queue_head
) - nqtail
;
766 /* Copy the buffered data into the application queue */
767 memcpy(dod
+ *entries
, This
->data_queue
+ nqtail
, span
* dodsize
);
768 /* Advance position */
770 if (nqtail
>= This
->queue_len
) nqtail
-= This
->queue_len
;
775 if (!(flags
& DIGDD_PEEK
))
776 This
->queue_tail
= nqtail
;
778 LeaveCriticalSection(&(This
->crit
));
780 /* Check if we need to do a mouse warping */
781 if (This
->need_warp
== WARP_NEEDED
) {
782 dinput_window_check(This
);
783 TRACE("Warping mouse to %ld - %ld\n", This
->mapped_center
.x
, This
->mapped_center
.y
);
784 SetCursorPos( This
->mapped_center
.x
, This
->mapped_center
.y
);
787 This
->need_warp
= WARP_DONE
;
789 This
->need_warp
= WARP_STARTED
;
795 /******************************************************************************
796 * SetProperty : change input device properties
798 static HRESULT WINAPI
SysMouseAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface
,
802 ICOM_THIS(SysMouseImpl
,iface
);
804 TRACE("(this=%p,%s,%p)\n",This
,debugstr_guid(rguid
),ph
);
806 if (!HIWORD(rguid
)) {
807 switch ((DWORD
)rguid
) {
808 case (DWORD
) DIPROP_BUFFERSIZE
: {
809 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
811 TRACE("buffersize = %ld\n",pd
->dwData
);
813 This
->data_queue
= (LPDIDEVICEOBJECTDATA
)HeapAlloc(GetProcessHeap(),0,
814 pd
->dwData
* sizeof(DIDEVICEOBJECTDATA
));
815 This
->queue_head
= 0;
816 This
->queue_tail
= 0;
817 This
->queue_len
= pd
->dwData
;
820 case (DWORD
) DIPROP_AXISMODE
: {
821 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
822 This
->absolute
= !(pd
->dwData
);
823 TRACE("Using %s coordinates mode now\n", This
->absolute
? "absolute" : "relative");
827 FIXME("Unknown type %ld (%s)\n",(DWORD
)rguid
,debugstr_guid(rguid
));
835 /******************************************************************************
836 * GetProperty : get input device properties
838 static HRESULT WINAPI
SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface
,
840 LPDIPROPHEADER pdiph
)
842 ICOM_THIS(SysMouseImpl
,iface
);
844 TRACE("(this=%p,%s,%p): stub!\n",
845 iface
, debugstr_guid(rguid
), pdiph
);
847 if (TRACE_ON(dinput
))
848 _dump_DIPROPHEADER(pdiph
);
850 if (!HIWORD(rguid
)) {
851 switch ((DWORD
)rguid
) {
852 case (DWORD
) DIPROP_BUFFERSIZE
: {
853 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
855 TRACE(" return buffersize = %d\n",This
->queue_len
);
856 pd
->dwData
= This
->queue_len
;
860 case (DWORD
) DIPROP_GRANULARITY
: {
861 LPDIPROPDWORD pr
= (LPDIPROPDWORD
) pdiph
;
863 /* We'll just assume that the app asks about the Z axis */
864 pr
->dwData
= WHEEL_DELTA
;
869 case (DWORD
) DIPROP_RANGE
: {
870 LPDIPROPRANGE pr
= (LPDIPROPRANGE
) pdiph
;
872 if ((pdiph
->dwHow
== DIPH_BYID
) &&
873 ((pdiph
->dwObj
== (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
)) ||
874 (pdiph
->dwObj
== (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
)))) {
875 /* Querying the range of either the X or the Y axis. As I do
876 not know the range, do as if the range were
878 pr
->lMin
= DIPROPRANGE_NOMIN
;
879 pr
->lMax
= DIPROPRANGE_NOMAX
;
886 FIXME("Unknown type %ld (%s)\n",(DWORD
)rguid
,debugstr_guid(rguid
));
896 /******************************************************************************
897 * SetEventNotification : specifies event to be sent on state change
899 static HRESULT WINAPI
SysMouseAImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface
,
901 ICOM_THIS(SysMouseImpl
,iface
);
903 TRACE("(this=%p,0x%08lx)\n",This
,(DWORD
)hnd
);
910 /******************************************************************************
911 * GetCapabilities : get the device capablitites
913 static HRESULT WINAPI
SysMouseAImpl_GetCapabilities(
914 LPDIRECTINPUTDEVICE8A iface
,
915 LPDIDEVCAPS lpDIDevCaps
)
917 ICOM_THIS(SysMouseImpl
,iface
);
919 TRACE("(this=%p,%p)\n",This
,lpDIDevCaps
);
921 if (lpDIDevCaps
->dwSize
== sizeof(DIDEVCAPS
)) {
922 lpDIDevCaps
->dwFlags
= DIDC_ATTACHED
;
923 if (This
->dinput
->version
>= 8)
924 lpDIDevCaps
->dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
926 lpDIDevCaps
->dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
927 lpDIDevCaps
->dwAxes
= 3;
928 lpDIDevCaps
->dwButtons
= 3;
929 lpDIDevCaps
->dwPOVs
= 0;
930 lpDIDevCaps
->dwFFSamplePeriod
= 0;
931 lpDIDevCaps
->dwFFMinTimeResolution
= 0;
932 lpDIDevCaps
->dwFirmwareRevision
= 100;
933 lpDIDevCaps
->dwHardwareRevision
= 100;
934 lpDIDevCaps
->dwFFDriverVersion
= 0;
937 FIXME("DirectX 3.0 not supported....\n");
944 /******************************************************************************
945 * EnumObjects : enumerate the different buttons and axis...
947 static HRESULT WINAPI
SysMouseAImpl_EnumObjects(
948 LPDIRECTINPUTDEVICE8A iface
,
949 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback
,
953 ICOM_THIS(SysMouseImpl
,iface
);
954 DIDEVICEOBJECTINSTANCEA ddoi
;
956 TRACE("(this=%p,%p,%p,%08lx)\n", This
, lpCallback
, lpvRef
, dwFlags
);
957 if (TRACE_ON(dinput
)) {
958 TRACE(" - flags = ");
959 _dump_EnumObjects_flags(dwFlags
);
963 /* Only the fields till dwFFMaxForce are relevant */
964 memset(&ddoi
, 0, sizeof(ddoi
));
965 ddoi
.dwSize
= FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA
, dwFFMaxForce
);
967 /* In a mouse, we have : two relative axis and three buttons */
968 if ((dwFlags
== DIDFT_ALL
) ||
969 (dwFlags
& DIDFT_AXIS
)) {
971 ddoi
.guidType
= GUID_XAxis
;
972 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_X_POSITION
];
973 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
974 strcpy(ddoi
.tszName
, "X-Axis");
975 _dump_OBJECTINSTANCEA(&ddoi
);
976 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
979 ddoi
.guidType
= GUID_YAxis
;
980 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_Y_POSITION
];
981 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
982 strcpy(ddoi
.tszName
, "Y-Axis");
983 _dump_OBJECTINSTANCEA(&ddoi
);
984 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
987 ddoi
.guidType
= GUID_ZAxis
;
988 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_Z_POSITION
];
989 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
990 strcpy(ddoi
.tszName
, "Z-Axis");
991 _dump_OBJECTINSTANCEA(&ddoi
);
992 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
995 if ((dwFlags
== DIDFT_ALL
) ||
996 (dwFlags
& DIDFT_BUTTON
)) {
997 ddoi
.guidType
= GUID_Button
;
1000 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_L_POSITION
];
1001 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
;
1002 strcpy(ddoi
.tszName
, "Left-Button");
1003 _dump_OBJECTINSTANCEA(&ddoi
);
1004 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
1007 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_R_POSITION
];
1008 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
;
1009 strcpy(ddoi
.tszName
, "Right-Button");
1010 _dump_OBJECTINSTANCEA(&ddoi
);
1011 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
1014 ddoi
.dwOfs
= This
->offset_array
[WINE_MOUSE_M_POSITION
];
1015 ddoi
.dwType
= DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE
) | DIDFT_PSHBUTTON
;
1016 strcpy(ddoi
.tszName
, "Middle-Button");
1017 _dump_OBJECTINSTANCEA(&ddoi
);
1018 if (lpCallback(&ddoi
, lpvRef
) != DIENUM_CONTINUE
) return DI_OK
;
1024 static HRESULT WINAPI
SysMouseWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface
, LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback
, LPVOID lpvRef
,DWORD dwFlags
)
1026 ICOM_THIS(SysMouseImpl
,iface
);
1028 device_enumobjects_AtoWcb_data data
;
1030 data
.lpCallBack
= lpCallback
;
1031 data
.lpvRef
= lpvRef
;
1033 return SysMouseAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A
) This
, (LPDIENUMDEVICEOBJECTSCALLBACKA
) DIEnumDevicesCallbackAtoW
, (LPVOID
) &data
, dwFlags
);
1036 /******************************************************************************
1037 * GetDeviceInfo : get information about a device's identity
1039 static HRESULT WINAPI
SysMouseAImpl_GetDeviceInfo(
1040 LPDIRECTINPUTDEVICE8A iface
,
1041 LPDIDEVICEINSTANCEA pdidi
)
1043 ICOM_THIS(SysMouseImpl
,iface
);
1044 TRACE("(this=%p,%p)\n", This
, pdidi
);
1046 if (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
)) {
1047 WARN(" dinput3 not supporte yet...\n");
1051 fill_mouse_dideviceinstanceA(pdidi
, This
->dinput
->version
);
1056 static HRESULT WINAPI
SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface
, LPDIDEVICEINSTANCEW pdidi
)
1058 ICOM_THIS(SysMouseImpl
,iface
);
1059 TRACE("(this=%p,%p)\n", This
, pdidi
);
1061 if (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
)) {
1062 WARN(" dinput3 not supporte yet...\n");
1066 fill_mouse_dideviceinstanceW(pdidi
, This
->dinput
->version
);
1072 static IDirectInputDevice8AVtbl SysMouseAvt
=
1074 IDirectInputDevice2AImpl_QueryInterface
,
1075 IDirectInputDevice2AImpl_AddRef
,
1076 SysMouseAImpl_Release
,
1077 SysMouseAImpl_GetCapabilities
,
1078 SysMouseAImpl_EnumObjects
,
1079 SysMouseAImpl_GetProperty
,
1080 SysMouseAImpl_SetProperty
,
1081 SysMouseAImpl_Acquire
,
1082 SysMouseAImpl_Unacquire
,
1083 SysMouseAImpl_GetDeviceState
,
1084 SysMouseAImpl_GetDeviceData
,
1085 SysMouseAImpl_SetDataFormat
,
1086 SysMouseAImpl_SetEventNotification
,
1087 SysMouseAImpl_SetCooperativeLevel
,
1088 IDirectInputDevice2AImpl_GetObjectInfo
,
1089 SysMouseAImpl_GetDeviceInfo
,
1090 IDirectInputDevice2AImpl_RunControlPanel
,
1091 IDirectInputDevice2AImpl_Initialize
,
1092 IDirectInputDevice2AImpl_CreateEffect
,
1093 IDirectInputDevice2AImpl_EnumEffects
,
1094 IDirectInputDevice2AImpl_GetEffectInfo
,
1095 IDirectInputDevice2AImpl_GetForceFeedbackState
,
1096 IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1097 IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1098 IDirectInputDevice2AImpl_Escape
,
1099 IDirectInputDevice2AImpl_Poll
,
1100 IDirectInputDevice2AImpl_SendDeviceData
,
1101 IDirectInputDevice7AImpl_EnumEffectsInFile
,
1102 IDirectInputDevice7AImpl_WriteEffectToFile
,
1103 IDirectInputDevice8AImpl_BuildActionMap
,
1104 IDirectInputDevice8AImpl_SetActionMap
,
1105 IDirectInputDevice8AImpl_GetImageInfo
1108 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1109 # define XCAST(fun) (typeof(SysMouseWvt.fun))
1111 # define XCAST(fun) (void*)
1114 static IDirectInputDevice8WVtbl SysMouseWvt
=
1116 IDirectInputDevice2WImpl_QueryInterface
,
1117 XCAST(AddRef
)IDirectInputDevice2AImpl_AddRef
,
1118 XCAST(Release
)SysMouseAImpl_Release
,
1119 XCAST(GetCapabilities
)SysMouseAImpl_GetCapabilities
,
1120 SysMouseWImpl_EnumObjects
,
1121 XCAST(GetProperty
)SysMouseAImpl_GetProperty
,
1122 XCAST(SetProperty
)SysMouseAImpl_SetProperty
,
1123 XCAST(Acquire
)SysMouseAImpl_Acquire
,
1124 XCAST(Unacquire
)SysMouseAImpl_Unacquire
,
1125 XCAST(GetDeviceState
)SysMouseAImpl_GetDeviceState
,
1126 XCAST(GetDeviceData
)SysMouseAImpl_GetDeviceData
,
1127 XCAST(SetDataFormat
)SysMouseAImpl_SetDataFormat
,
1128 XCAST(SetEventNotification
)SysMouseAImpl_SetEventNotification
,
1129 XCAST(SetCooperativeLevel
)SysMouseAImpl_SetCooperativeLevel
,
1130 IDirectInputDevice2WImpl_GetObjectInfo
,
1131 SysMouseWImpl_GetDeviceInfo
,
1132 XCAST(RunControlPanel
)IDirectInputDevice2AImpl_RunControlPanel
,
1133 XCAST(Initialize
)IDirectInputDevice2AImpl_Initialize
,
1134 XCAST(CreateEffect
)IDirectInputDevice2AImpl_CreateEffect
,
1135 IDirectInputDevice2WImpl_EnumEffects
,
1136 IDirectInputDevice2WImpl_GetEffectInfo
,
1137 XCAST(GetForceFeedbackState
)IDirectInputDevice2AImpl_GetForceFeedbackState
,
1138 XCAST(SendForceFeedbackCommand
)IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1139 XCAST(EnumCreatedEffectObjects
)IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1140 XCAST(Escape
)IDirectInputDevice2AImpl_Escape
,
1141 XCAST(Poll
)IDirectInputDevice2AImpl_Poll
,
1142 XCAST(SendDeviceData
)IDirectInputDevice2AImpl_SendDeviceData
,
1143 IDirectInputDevice7WImpl_EnumEffectsInFile
,
1144 IDirectInputDevice7WImpl_WriteEffectToFile
,
1145 IDirectInputDevice8WImpl_BuildActionMap
,
1146 IDirectInputDevice8WImpl_SetActionMap
,
1147 IDirectInputDevice8WImpl_GetImageInfo