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 static const struct dinput_device_vtbl mouse_vtbl
;
51 struct dinput_device base
;
53 /* These are used in case of relative -> absolute transitions */
56 /* warping: whether we need to move mouse back to middle once we
57 * reach window borders (for e.g. shooters, "surface movement" games) */
61 WARP_MOUSE warp_override
;
64 static inline struct mouse
*impl_from_IDirectInputDevice8W( IDirectInputDevice8W
*iface
)
66 return CONTAINING_RECORD( CONTAINING_RECORD( iface
, struct dinput_device
, IDirectInputDevice8W_iface
), struct mouse
, base
);
69 HRESULT
mouse_enum_device( DWORD type
, DWORD flags
, DIDEVICEINSTANCEW
*instance
, DWORD version
)
73 TRACE( "type %#lx, flags %#lx, instance %p, version %#lx\n", type
, flags
, instance
, version
);
75 size
= instance
->dwSize
;
76 memset( instance
, 0, size
);
77 instance
->dwSize
= size
;
78 instance
->guidInstance
= GUID_SysMouse
;
79 instance
->guidProduct
= GUID_SysMouse
;
80 if (version
>= 0x0800) instance
->dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
81 else instance
->dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
82 MultiByteToWideChar( CP_ACP
, 0, "Mouse", -1, instance
->tszInstanceName
, MAX_PATH
);
83 MultiByteToWideChar( CP_ACP
, 0, "Wine Mouse", -1, instance
->tszProductName
, MAX_PATH
);
88 static BOOL
init_object_properties( struct dinput_device
*device
, UINT index
, struct hid_value_caps
*caps
,
89 const DIDEVICEOBJECTINSTANCEW
*instance
, void *data
)
91 struct object_properties
*properties
;
93 if (index
== -1) return DIENUM_STOP
;
94 properties
= device
->object_properties
+ index
;
96 /* The z-axis (wheel) has a different granularity */
97 if (instance
->dwOfs
== DIMOFS_Z
) properties
->granularity
= WHEEL_DELTA
;
98 return DIENUM_CONTINUE
;
101 void dinput_mouse_rawinput_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
, RAWINPUT
*ri
)
103 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
104 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
110 static const USHORT mouse_button_flags
[] =
112 RI_MOUSE_BUTTON_1_DOWN
, RI_MOUSE_BUTTON_1_UP
,
113 RI_MOUSE_BUTTON_2_DOWN
, RI_MOUSE_BUTTON_2_UP
,
114 RI_MOUSE_BUTTON_3_DOWN
, RI_MOUSE_BUTTON_3_UP
,
115 RI_MOUSE_BUTTON_4_DOWN
, RI_MOUSE_BUTTON_4_UP
,
116 RI_MOUSE_BUTTON_5_DOWN
, RI_MOUSE_BUTTON_5_UP
119 TRACE( "iface %p, wparam %#Ix, lparam %#Ix, ri %p.\n", iface
, wparam
, lparam
, ri
);
121 if (ri
->data
.mouse
.usFlags
& MOUSE_VIRTUAL_DESKTOP
)
122 FIXME( "Unimplemented MOUSE_VIRTUAL_DESKTOP flag\n" );
123 if (ri
->data
.mouse
.usFlags
& MOUSE_ATTRIBUTES_CHANGED
)
124 FIXME( "Unimplemented MOUSE_ATTRIBUTES_CHANGED flag\n" );
126 EnterCriticalSection( &impl
->base
.crit
);
127 seq
= impl
->base
.dinput
->evsequence
++;
129 rel
.x
= ri
->data
.mouse
.lLastX
;
130 rel
.y
= ri
->data
.mouse
.lLastY
;
131 if (ri
->data
.mouse
.usFlags
& MOUSE_MOVE_ABSOLUTE
)
141 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
153 queue_event( iface
, 0, pt
.x
, GetCurrentTime(), seq
);
159 queue_event( iface
, 1, pt
.y
, GetCurrentTime(), seq
);
165 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
166 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
167 impl
->need_warp
= TRUE
;
170 if (ri
->data
.mouse
.usButtonFlags
& RI_MOUSE_WHEEL
)
172 state
->lZ
+= (SHORT
)ri
->data
.mouse
.usButtonData
;
173 queue_event( iface
, 2, (SHORT
)ri
->data
.mouse
.usButtonData
, GetCurrentTime(), seq
);
177 for (i
= 0; i
< ARRAY_SIZE(mouse_button_flags
); ++i
)
179 if (ri
->data
.mouse
.usButtonFlags
& mouse_button_flags
[i
])
181 state
->rgbButtons
[i
/ 2] = 0x80 - (i
% 2) * 0x80;
182 queue_event( iface
, 3 + (i
/ 2), state
->rgbButtons
[i
/ 2], GetCurrentTime(), seq
);
187 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
188 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
189 state
->lX
, state
->lY
, state
->lZ
);
191 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
192 LeaveCriticalSection( &impl
->base
.crit
);
195 int dinput_mouse_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
)
197 MSLLHOOKSTRUCT
*hook
= (MSLLHOOKSTRUCT
*)lparam
;
198 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
199 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
204 TRACE( "iface %p, msg %#Ix, x %+ld, y %+ld\n", iface
, wparam
, hook
->pt
.x
, hook
->pt
.y
);
206 EnterCriticalSection( &impl
->base
.crit
);
207 seq
= impl
->base
.dinput
->evsequence
++;
215 state
->lX
+= pt
.x
= hook
->pt
.x
- pt
.x
;
216 state
->lY
+= pt
.y
= hook
->pt
.y
- pt
.y
;
218 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
227 queue_event( iface
, 0, pt1
.x
, GetCurrentTime(), seq
);
232 queue_event( iface
, 1, pt1
.y
, GetCurrentTime(), seq
);
238 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
239 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
240 impl
->need_warp
= TRUE
;
245 state
->lZ
+= (short)HIWORD( hook
->mouseData
);
246 queue_event( iface
, 2, state
->lZ
, GetCurrentTime(), seq
);
247 /* FarCry crashes if it gets a mouse wheel message */
248 /* FIXME: should probably filter out other messages too */
252 state
->rgbButtons
[0] = 0x80;
253 queue_event( iface
, 3, 0x80, GetCurrentTime(), seq
);
257 state
->rgbButtons
[0] = 0x00;
258 queue_event( iface
, 3, 0x00, GetCurrentTime(), seq
);
262 state
->rgbButtons
[1] = 0x80;
263 queue_event( iface
, 4, 0x80, GetCurrentTime(), seq
);
267 state
->rgbButtons
[1] = 0x00;
268 queue_event( iface
, 4, 0x00, GetCurrentTime(), seq
);
272 state
->rgbButtons
[2] = 0x80;
273 queue_event( iface
, 5, 0x80, GetCurrentTime(), seq
);
277 state
->rgbButtons
[2] = 0x00;
278 queue_event( iface
, 5, 0x00, GetCurrentTime(), seq
);
282 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = 0x80;
283 queue_event( iface
, 5 + HIWORD(hook
->mouseData
), 0x80, GetCurrentTime(), seq
);
287 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = 0x00;
288 queue_event( iface
, 5 + HIWORD(hook
->mouseData
), 0x00, GetCurrentTime(), seq
);
293 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
294 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
295 state
->lX
, state
->lY
, state
->lZ
);
297 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
298 LeaveCriticalSection( &impl
->base
.crit
);
302 static void warp_check( struct mouse
*impl
, BOOL force
)
304 DWORD now
= GetCurrentTime();
305 const DWORD interval
= impl
->clipped
? 500 : 10;
307 if (force
|| (impl
->need_warp
&& (now
- impl
->last_warped
> interval
)))
312 impl
->last_warped
= now
;
313 impl
->need_warp
= FALSE
;
314 if (!GetClientRect( impl
->base
.win
, &rect
)) return;
315 MapWindowPoints( impl
->base
.win
, 0, (POINT
*)&rect
, 2 );
318 mapped_center
.x
= (rect
.left
+ rect
.right
) / 2;
319 mapped_center
.y
= (rect
.top
+ rect
.bottom
) / 2;
320 TRACE( "Warping mouse to x %+ld, y %+ld.\n", mapped_center
.x
, mapped_center
.y
);
321 SetCursorPos( mapped_center
.x
, mapped_center
.y
);
323 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
325 /* make sure we clip even if the window covers the whole screen */
326 rect
.left
= max( rect
.left
, GetSystemMetrics( SM_XVIRTUALSCREEN
) + 1 );
327 rect
.top
= max( rect
.top
, GetSystemMetrics( SM_YVIRTUALSCREEN
) + 1 );
328 rect
.right
= min( rect
.right
, rect
.left
+ GetSystemMetrics( SM_CXVIRTUALSCREEN
) - 2 );
329 rect
.bottom
= min( rect
.bottom
, rect
.top
+ GetSystemMetrics( SM_CYVIRTUALSCREEN
) - 2 );
330 TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect
));
332 impl
->clipped
= GetClipCursor( &new_rect
) && EqualRect( &rect
, &new_rect
);
337 static HRESULT
mouse_poll( IDirectInputDevice8W
*iface
)
339 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
340 check_dinput_events();
341 warp_check( impl
, FALSE
);
345 static HRESULT
mouse_acquire( IDirectInputDevice8W
*iface
)
347 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
348 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
351 GetCursorPos( &point
);
352 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
361 impl
->org_coords
= point
;
364 state
->rgbButtons
[0] = GetKeyState( VK_LBUTTON
) & 0x80;
365 state
->rgbButtons
[1] = GetKeyState( VK_RBUTTON
) & 0x80;
366 state
->rgbButtons
[2] = GetKeyState( VK_MBUTTON
) & 0x80;
368 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
371 warp_check( impl
, TRUE
);
373 else if (impl
->warp_override
== WARP_FORCE_ON
)
375 /* Need a window to warp mouse in. */
376 if (!impl
->base
.win
) impl
->base
.win
= GetDesktopWindow();
377 warp_check( impl
, TRUE
);
379 else if (impl
->clipped
)
382 impl
->clipped
= FALSE
;
388 static HRESULT
mouse_unacquire( IDirectInputDevice8W
*iface
)
390 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
392 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
396 impl
->clipped
= FALSE
;
399 /* And put the mouse cursor back where it was at acquire time */
400 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
|| impl
->warp_override
== WARP_FORCE_ON
)
402 TRACE( "warping mouse back to %s\n", wine_dbgstr_point( &impl
->org_coords
) );
403 SetCursorPos( impl
->org_coords
.x
, impl
->org_coords
.y
);
409 static BOOL
try_enum_object( struct dinput_device
*impl
, const DIPROPHEADER
*filter
, DWORD flags
, enum_object_callback callback
,
410 UINT index
, DIDEVICEOBJECTINSTANCEW
*instance
, void *data
)
412 if (flags
!= DIDFT_ALL
&& !(flags
& DIDFT_GETTYPE( instance
->dwType
))) return DIENUM_CONTINUE
;
414 switch (filter
->dwHow
)
417 return callback( impl
, index
, NULL
, instance
, data
);
419 if (filter
->dwObj
!= instance
->dwOfs
) return DIENUM_CONTINUE
;
420 return callback( impl
, index
, NULL
, instance
, data
);
422 if ((filter
->dwObj
& 0x00ffffff) != (instance
->dwType
& 0x00ffffff)) return DIENUM_CONTINUE
;
423 return callback( impl
, index
, NULL
, instance
, data
);
426 return DIENUM_CONTINUE
;
429 static HRESULT
mouse_enum_objects( IDirectInputDevice8W
*iface
, const DIPROPHEADER
*filter
,
430 DWORD flags
, enum_object_callback callback
, void *context
)
432 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
433 DIDEVICEOBJECTINSTANCEW instances
[] =
436 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
437 .guidType
= GUID_XAxis
,
439 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(0),
440 .dwFlags
= DIDOI_ASPECTPOSITION
,
441 .tszName
= L
"X-axis",
444 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
445 .guidType
= GUID_YAxis
,
447 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(1),
448 .dwFlags
= DIDOI_ASPECTPOSITION
,
449 .tszName
= L
"Y-axis",
452 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
453 .guidType
= GUID_ZAxis
,
455 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(2),
456 .dwFlags
= DIDOI_ASPECTPOSITION
,
460 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
461 .guidType
= GUID_Button
,
462 .dwOfs
= DIMOFS_BUTTON0
,
463 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(3),
464 .tszName
= L
"Button 0",
467 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
468 .guidType
= GUID_Button
,
469 .dwOfs
= DIMOFS_BUTTON1
,
470 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(4),
471 .tszName
= L
"Button 1",
474 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
475 .guidType
= GUID_Button
,
476 .dwOfs
= DIMOFS_BUTTON2
,
477 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(5),
478 .tszName
= L
"Button 2",
481 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
482 .guidType
= GUID_Button
,
483 .dwOfs
= DIMOFS_BUTTON3
,
484 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(6),
485 .tszName
= L
"Button 3",
488 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
489 .guidType
= GUID_Button
,
490 .dwOfs
= DIMOFS_BUTTON4
,
491 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(7),
492 .tszName
= L
"Button 4",
498 for (i
= 0; i
< ARRAY_SIZE(instances
); ++i
)
500 ret
= try_enum_object( &impl
->base
, filter
, flags
, callback
, i
, instances
+ i
, context
);
501 if (ret
!= DIENUM_CONTINUE
) return DIENUM_STOP
;
504 return DIENUM_CONTINUE
;
507 HRESULT
mouse_create_device( struct dinput
*dinput
, const GUID
*guid
, IDirectInputDevice8W
**out
)
509 static const DIPROPHEADER filter
=
511 .dwSize
= sizeof(filter
),
512 .dwHeaderSize
= sizeof(filter
),
513 .dwHow
= DIPH_DEVICE
,
520 TRACE( "dinput %p, guid %s, out %p\n", dinput
, debugstr_guid( guid
), out
);
523 if (!IsEqualGUID( &GUID_SysMouse
, guid
)) return DIERR_DEVICENOTREG
;
525 if (!(impl
= calloc( 1, sizeof(*impl
) ))) return E_OUTOFMEMORY
;
526 dinput_device_init( &impl
->base
, &mouse_vtbl
, guid
, dinput
);
527 impl
->base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": struct mouse*->base.crit");
529 mouse_enum_device( 0, 0, &impl
->base
.instance
, dinput
->dwVersion
);
530 impl
->base
.caps
.dwDevType
= impl
->base
.instance
.dwDevType
;
531 impl
->base
.caps
.dwFirmwareRevision
= 100;
532 impl
->base
.caps
.dwHardwareRevision
= 100;
533 impl
->base
.dwCoopLevel
= DISCL_NONEXCLUSIVE
| DISCL_BACKGROUND
;
534 if (dinput
->dwVersion
>= 0x0800) impl
->base
.use_raw_input
= TRUE
;
536 if (FAILED(hr
= dinput_device_init_device_format( &impl
->base
.IDirectInputDevice8W_iface
))) goto failed
;
537 mouse_enum_objects( &impl
->base
.IDirectInputDevice8W_iface
, &filter
, DIDFT_AXIS
, init_object_properties
, NULL
);
539 get_app_key(&hkey
, &appkey
);
540 if (!get_config_key( hkey
, appkey
, L
"MouseWarpOverride", buffer
, sizeof(buffer
) ))
542 if (!wcsnicmp( buffer
, L
"disable", -1 )) impl
->warp_override
= WARP_DISABLE
;
543 else if (!wcsnicmp( buffer
, L
"force", -1 )) impl
->warp_override
= WARP_FORCE_ON
;
545 if (appkey
) RegCloseKey(appkey
);
546 if (hkey
) RegCloseKey(hkey
);
548 *out
= &impl
->base
.IDirectInputDevice8W_iface
;
552 IDirectInputDevice_Release( &impl
->base
.IDirectInputDevice8W_iface
);
556 static const struct dinput_device_vtbl mouse_vtbl
=