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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "dinput_private.h"
35 #include "device_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
40 /* Wine mouse driver object instances */
41 #define WINE_MOUSE_X_AXIS_INSTANCE 0
42 #define WINE_MOUSE_Y_AXIS_INSTANCE 1
43 #define WINE_MOUSE_Z_AXIS_INSTANCE 2
44 #define WINE_MOUSE_BUTTONS_INSTANCE 3
46 static const struct dinput_device_vtbl mouse_vtbl
;
48 typedef struct SysMouseImpl SysMouseImpl
;
59 struct IDirectInputDeviceImpl base
;
62 /* These are used in case of relative -> absolute transitions */
65 /* warping: whether we need to move mouse back to middle once we
66 * reach window borders (for e.g. shooters, "surface movement" games) */
70 WARP_MOUSE warp_override
;
73 static inline SysMouseImpl
*impl_from_IDirectInputDevice8W(IDirectInputDevice8W
*iface
)
75 return CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8W_iface
), SysMouseImpl
, base
);
78 HRESULT
mouse_enum_device( DWORD type
, DWORD flags
, DIDEVICEINSTANCEW
*instance
, DWORD version
, int index
)
82 TRACE( "type %#x, flags %#x, instance %p, version %#04x, index %d\n", type
, flags
, instance
, version
, index
);
84 if (index
!= 0) return DIERR_GENERIC
;
85 if (flags
& DIEDFL_FORCEFEEDBACK
) return DI_NOEFFECT
;
86 if (version
< 0x0800 && type
!= 0 && type
!= DIDEVTYPE_MOUSE
) return DI_NOEFFECT
;
87 if (version
>= 0x0800 && type
!= DI8DEVCLASS_ALL
&& type
!= DI8DEVCLASS_POINTER
&& type
!= DI8DEVTYPE_MOUSE
)
90 if (instance
->dwSize
!= sizeof(DIDEVICEINSTANCEW
) &&
91 instance
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3W
))
92 return DIERR_INVALIDPARAM
;
94 size
= instance
->dwSize
;
95 memset( instance
, 0, size
);
96 instance
->dwSize
= size
;
97 instance
->guidInstance
= GUID_SysMouse
;
98 instance
->guidProduct
= GUID_SysMouse
;
99 if (version
>= 0x0800) instance
->dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
100 else instance
->dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
101 MultiByteToWideChar( CP_ACP
, 0, "Mouse", -1, instance
->tszInstanceName
, MAX_PATH
);
102 MultiByteToWideChar( CP_ACP
, 0, "Wine Mouse", -1, instance
->tszProductName
, MAX_PATH
);
107 HRESULT
mouse_create_device( IDirectInputImpl
*dinput
, const GUID
*guid
, IDirectInputDevice8W
**out
)
114 TRACE( "dinput %p, guid %s, out %p\n", dinput
, debugstr_guid( guid
), out
);
117 if (!IsEqualGUID( &GUID_SysMouse
, guid
)) return DIERR_DEVICENOTREG
;
119 if (FAILED(hr
= direct_input_device_alloc( sizeof(SysMouseImpl
), &mouse_vtbl
,
120 guid
, dinput
, (void **)&impl
)))
122 impl
->base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": SysMouseImpl*->base.crit");
124 mouse_enum_device( 0, 0, &impl
->base
.instance
, dinput
->dwVersion
, 0 );
125 impl
->base
.caps
.dwDevType
= impl
->base
.instance
.dwDevType
;
126 impl
->base
.caps
.dwFirmwareRevision
= 100;
127 impl
->base
.caps
.dwHardwareRevision
= 100;
128 impl
->base
.dwCoopLevel
= DISCL_NONEXCLUSIVE
| DISCL_BACKGROUND
;
130 get_app_key(&hkey
, &appkey
);
131 if (!get_config_key( hkey
, appkey
, L
"MouseWarpOverride", buffer
, sizeof(buffer
) ))
133 if (!wcsnicmp( buffer
, L
"disable", -1 )) impl
->warp_override
= WARP_DISABLE
;
134 else if (!wcsnicmp( buffer
, L
"force", -1 )) impl
->warp_override
= WARP_FORCE_ON
;
136 if (appkey
) RegCloseKey(appkey
);
137 if (hkey
) RegCloseKey(hkey
);
139 if (FAILED(hr
= direct_input_device_init( &impl
->base
.IDirectInputDevice8W_iface
)))
141 IDirectInputDevice_Release( &impl
->base
.IDirectInputDevice8W_iface
);
145 if (dinput
->dwVersion
>= 0x0800)
147 impl
->base
.use_raw_input
= TRUE
;
148 impl
->base
.raw_device
.usUsagePage
= 1; /* HID generic device page */
149 impl
->base
.raw_device
.usUsage
= 2; /* HID generic mouse */
152 *out
= &impl
->base
.IDirectInputDevice8W_iface
;
156 const struct dinput_device mouse_device
= {
162 /******************************************************************************
163 * SysMouseA (DInput Mouse support)
166 void dinput_mouse_rawinput_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
, RAWINPUT
*ri
)
168 SysMouseImpl
*This
= impl_from_IDirectInputDevice8W( iface
);
169 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)This
->base
.device_state
;
175 static const USHORT mouse_button_flags
[] =
177 RI_MOUSE_BUTTON_1_DOWN
, RI_MOUSE_BUTTON_1_UP
,
178 RI_MOUSE_BUTTON_2_DOWN
, RI_MOUSE_BUTTON_2_UP
,
179 RI_MOUSE_BUTTON_3_DOWN
, RI_MOUSE_BUTTON_3_UP
,
180 RI_MOUSE_BUTTON_4_DOWN
, RI_MOUSE_BUTTON_4_UP
,
181 RI_MOUSE_BUTTON_5_DOWN
, RI_MOUSE_BUTTON_5_UP
184 TRACE( "(%p) wp %08lx, lp %08lx\n", iface
, wparam
, lparam
);
186 if (ri
->data
.mouse
.usFlags
& MOUSE_VIRTUAL_DESKTOP
)
187 FIXME( "Unimplemented MOUSE_VIRTUAL_DESKTOP flag\n" );
188 if (ri
->data
.mouse
.usFlags
& MOUSE_ATTRIBUTES_CHANGED
)
189 FIXME( "Unimplemented MOUSE_ATTRIBUTES_CHANGED flag\n" );
191 EnterCriticalSection( &This
->base
.crit
);
192 seq
= This
->base
.dinput
->evsequence
++;
194 rel
.x
= ri
->data
.mouse
.lLastX
;
195 rel
.y
= ri
->data
.mouse
.lLastY
;
196 if (ri
->data
.mouse
.usFlags
& MOUSE_MOVE_ABSOLUTE
)
206 if (This
->base
.data_format
.user_df
->dwFlags
& DIDF_ABSAXIS
)
218 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
219 pt
.x
, GetCurrentTime(), seq
);
225 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
226 pt
.y
, GetCurrentTime(), seq
);
232 if ((This
->warp_override
== WARP_FORCE_ON
) ||
233 (This
->warp_override
!= WARP_DISABLE
&& (This
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
234 This
->need_warp
= TRUE
;
237 if (ri
->data
.mouse
.usButtonFlags
& RI_MOUSE_WHEEL
)
239 state
->lZ
+= (wdata
= (SHORT
)ri
->data
.mouse
.usButtonData
);
240 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
241 wdata
, GetCurrentTime(), seq
);
245 for (i
= 0; i
< ARRAY_SIZE(mouse_button_flags
); ++i
)
247 if (ri
->data
.mouse
.usButtonFlags
& mouse_button_flags
[i
])
249 state
->rgbButtons
[i
/ 2] = 0x80 - (i
% 2) * 0x80;
250 queue_event( iface
, DIDFT_MAKEINSTANCE( WINE_MOUSE_BUTTONS_INSTANCE
+ (i
/ 2) ) | DIDFT_PSHBUTTON
,
251 state
->rgbButtons
[i
/ 2], GetCurrentTime(), seq
);
256 TRACE( "buttons %02x %02x %02x %02x %02x, x %d, y %d, w %d\n", state
->rgbButtons
[0],
257 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
258 state
->lX
, state
->lY
, state
->lZ
);
260 if (notify
&& This
->base
.hEvent
) SetEvent( This
->base
.hEvent
);
261 LeaveCriticalSection( &This
->base
.crit
);
264 /* low-level mouse hook */
265 int dinput_mouse_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
)
267 MSLLHOOKSTRUCT
*hook
= (MSLLHOOKSTRUCT
*)lparam
;
268 SysMouseImpl
*This
= impl_from_IDirectInputDevice8W( iface
);
269 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)This
->base
.device_state
;
270 int wdata
= 0, inst_id
= -1, ret
= 0;
273 TRACE("msg %lx @ (%d %d)\n", wparam
, hook
->pt
.x
, hook
->pt
.y
);
275 EnterCriticalSection(&This
->base
.crit
);
283 state
->lX
+= pt
.x
= hook
->pt
.x
- pt
.x
;
284 state
->lY
+= pt
.y
= hook
->pt
.y
- pt
.y
;
286 if (This
->base
.data_format
.user_df
->dwFlags
& DIDF_ABSAXIS
)
295 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
300 /* Already have X, need to queue it */
303 queue_event(iface
, inst_id
,
304 wdata
, GetCurrentTime(), This
->base
.dinput
->evsequence
);
307 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
313 if ((This
->warp_override
== WARP_FORCE_ON
) ||
314 (This
->warp_override
!= WARP_DISABLE
&& (This
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
315 This
->need_warp
= TRUE
;
320 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
321 state
->lZ
+= wdata
= (short)HIWORD( hook
->mouseData
);
322 /* FarCry crashes if it gets a mouse wheel message */
323 /* FIXME: should probably filter out other messages too */
327 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
328 state
->rgbButtons
[0] = wdata
= 0x80;
331 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
332 state
->rgbButtons
[0] = wdata
= 0x00;
335 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
336 state
->rgbButtons
[1] = wdata
= 0x80;
339 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
340 state
->rgbButtons
[1] = wdata
= 0x00;
343 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
344 state
->rgbButtons
[2] = wdata
= 0x80;
347 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
348 state
->rgbButtons
[2] = wdata
= 0x00;
351 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
352 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x80;
355 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
356 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x00;
363 queue_event(iface
, inst_id
,
364 wdata
, GetCurrentTime(), This
->base
.dinput
->evsequence
++);
368 TRACE( "buttons %02x %02x %02x %02x %02x, x %d, y %d, w %d\n", state
->rgbButtons
[0],
369 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
370 state
->lX
, state
->lY
, state
->lZ
);
372 if (notify
&& This
->base
.hEvent
) SetEvent( This
->base
.hEvent
);
373 LeaveCriticalSection(&This
->base
.crit
);
377 static void warp_check( SysMouseImpl
* This
, BOOL force
)
379 DWORD now
= GetCurrentTime();
380 const DWORD interval
= This
->clipped
? 500 : 10;
382 if (force
|| (This
->need_warp
&& (now
- This
->last_warped
> interval
)))
387 This
->last_warped
= now
;
388 This
->need_warp
= FALSE
;
389 if (!GetClientRect(This
->base
.win
, &rect
)) return;
390 MapWindowPoints( This
->base
.win
, 0, (POINT
*)&rect
, 2 );
393 mapped_center
.x
= (rect
.left
+ rect
.right
) / 2;
394 mapped_center
.y
= (rect
.top
+ rect
.bottom
) / 2;
395 TRACE("Warping mouse to %d - %d\n", mapped_center
.x
, mapped_center
.y
);
396 SetCursorPos( mapped_center
.x
, mapped_center
.y
);
398 if (This
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
400 /* make sure we clip even if the window covers the whole screen */
401 rect
.left
= max( rect
.left
, GetSystemMetrics( SM_XVIRTUALSCREEN
) + 1 );
402 rect
.top
= max( rect
.top
, GetSystemMetrics( SM_YVIRTUALSCREEN
) + 1 );
403 rect
.right
= min( rect
.right
, rect
.left
+ GetSystemMetrics( SM_CXVIRTUALSCREEN
) - 2 );
404 rect
.bottom
= min( rect
.bottom
, rect
.top
+ GetSystemMetrics( SM_CYVIRTUALSCREEN
) - 2 );
405 TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect
));
407 This
->clipped
= GetClipCursor( &new_rect
) && EqualRect( &rect
, &new_rect
);
412 static HRESULT
mouse_poll( IDirectInputDevice8W
*iface
)
414 SysMouseImpl
*impl
= impl_from_IDirectInputDevice8W( iface
);
415 check_dinput_events();
416 warp_check( impl
, FALSE
);
420 static HRESULT
mouse_acquire( IDirectInputDevice8W
*iface
)
422 SysMouseImpl
*impl
= impl_from_IDirectInputDevice8W( iface
);
423 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
426 /* Init the mouse state */
427 GetCursorPos( &point
);
428 if (impl
->base
.data_format
.user_df
->dwFlags
& DIDF_ABSAXIS
)
437 impl
->org_coords
= point
;
440 state
->rgbButtons
[0] = GetKeyState( VK_LBUTTON
) & 0x80;
441 state
->rgbButtons
[1] = GetKeyState( VK_RBUTTON
) & 0x80;
442 state
->rgbButtons
[2] = GetKeyState( VK_MBUTTON
) & 0x80;
444 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
446 ShowCursor( FALSE
); /* hide cursor */
447 warp_check( impl
, TRUE
);
449 else if (impl
->warp_override
== WARP_FORCE_ON
)
451 /* Need a window to warp mouse in. */
452 if (!impl
->base
.win
) impl
->base
.win
= GetDesktopWindow();
453 warp_check( impl
, TRUE
);
455 else if (impl
->clipped
)
458 impl
->clipped
= FALSE
;
464 static HRESULT
mouse_unacquire( IDirectInputDevice8W
*iface
)
466 SysMouseImpl
*impl
= impl_from_IDirectInputDevice8W( iface
);
468 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
471 ShowCursor( TRUE
); /* show cursor */
472 impl
->clipped
= FALSE
;
475 /* And put the mouse cursor back where it was at acquire time */
476 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
|| impl
->warp_override
== WARP_FORCE_ON
)
478 TRACE( "warping mouse back to %s\n", wine_dbgstr_point( &impl
->org_coords
) );
479 SetCursorPos( impl
->org_coords
.x
, impl
->org_coords
.y
);
485 static BOOL
try_enum_object( const DIPROPHEADER
*filter
, DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
,
486 DIDEVICEOBJECTINSTANCEW
*instance
, void *data
)
488 if (flags
!= DIDFT_ALL
&& !(flags
& DIDFT_GETTYPE( instance
->dwType
))) return DIENUM_CONTINUE
;
490 switch (filter
->dwHow
)
493 return callback( instance
, data
);
495 if (filter
->dwObj
!= instance
->dwOfs
) return DIENUM_CONTINUE
;
496 return callback( instance
, data
);
498 if ((filter
->dwObj
& 0x00ffffff) != (instance
->dwType
& 0x00ffffff)) return DIENUM_CONTINUE
;
499 return callback( instance
, data
);
502 return DIENUM_CONTINUE
;
505 static HRESULT
mouse_enum_objects( IDirectInputDevice8W
*iface
, const DIPROPHEADER
*filter
,
506 DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
, void *context
)
508 DIDEVICEOBJECTINSTANCEW instances
[] =
511 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
512 .guidType
= GUID_XAxis
,
514 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(0),
515 .dwFlags
= DIDOI_ASPECTPOSITION
,
516 .tszName
= L
"X-axis",
519 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
520 .guidType
= GUID_YAxis
,
522 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(1),
523 .dwFlags
= DIDOI_ASPECTPOSITION
,
524 .tszName
= L
"Y-axis",
527 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
528 .guidType
= GUID_ZAxis
,
530 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(2),
531 .dwFlags
= DIDOI_ASPECTPOSITION
,
535 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
536 .guidType
= GUID_Button
,
537 .dwOfs
= DIMOFS_BUTTON0
,
538 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(3),
539 .tszName
= L
"Button 0",
542 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
543 .guidType
= GUID_Button
,
544 .dwOfs
= DIMOFS_BUTTON1
,
545 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(4),
546 .tszName
= L
"Button 1",
549 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
550 .guidType
= GUID_Button
,
551 .dwOfs
= DIMOFS_BUTTON2
,
552 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(5),
553 .tszName
= L
"Button 2",
556 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
557 .guidType
= GUID_Button
,
558 .dwOfs
= DIMOFS_BUTTON3
,
559 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(6),
560 .tszName
= L
"Button 3",
563 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
564 .guidType
= GUID_Button
,
565 .dwOfs
= DIMOFS_BUTTON4
,
566 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(7),
567 .tszName
= L
"Button 4",
573 for (i
= 0; i
< ARRAY_SIZE(instances
); ++i
)
575 ret
= try_enum_object( filter
, flags
, callback
, instances
+ i
, context
);
576 if (ret
!= DIENUM_CONTINUE
) return DIENUM_STOP
;
579 return DIENUM_CONTINUE
;
582 static HRESULT
mouse_get_property( IDirectInputDevice8W
*iface
, DWORD property
,
583 DIPROPHEADER
*header
, DIDEVICEOBJECTINSTANCEW
*instance
)
587 case (DWORD_PTR
)DIPROP_RANGE
:
589 DIPROPRANGE
*range
= (DIPROPRANGE
*)header
;
590 range
->lMin
= DIPROPRANGE_NOMIN
;
591 range
->lMax
= DIPROPRANGE_NOMAX
;
594 case (DWORD_PTR
)DIPROP_GRANULARITY
:
596 DIPROPDWORD
*value
= (DIPROPDWORD
*)header
;
597 if (instance
->dwType
== DIMOFS_Z
) value
->dwData
= WHEEL_DELTA
;
598 else value
->dwData
= 1;
602 return DIERR_UNSUPPORTED
;
605 static HRESULT
mouse_set_property( IDirectInputDevice8W
*iface
, DWORD property
,
606 const DIPROPHEADER
*header
, const DIDEVICEOBJECTINSTANCEW
*instance
)
608 return DIERR_UNSUPPORTED
;
611 static const struct dinput_device_vtbl mouse_vtbl
=