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
;
57 struct dinput_device base
;
59 /* These are used in case of relative -> absolute transitions */
62 /* warping: whether we need to move mouse back to middle once we
63 * reach window borders (for e.g. shooters, "surface movement" games) */
67 WARP_MOUSE warp_override
;
70 static inline struct mouse
*impl_from_IDirectInputDevice8W( IDirectInputDevice8W
*iface
)
72 return CONTAINING_RECORD( CONTAINING_RECORD( iface
, struct dinput_device
, IDirectInputDevice8W_iface
), struct mouse
, base
);
75 HRESULT
mouse_enum_device( DWORD type
, DWORD flags
, DIDEVICEINSTANCEW
*instance
, DWORD version
)
79 TRACE( "type %#lx, flags %#lx, instance %p, version %#lx\n", type
, flags
, instance
, version
);
81 size
= instance
->dwSize
;
82 memset( instance
, 0, size
);
83 instance
->dwSize
= size
;
84 instance
->guidInstance
= GUID_SysMouse
;
85 instance
->guidProduct
= GUID_SysMouse
;
86 if (version
>= 0x0800) instance
->dwDevType
= DI8DEVTYPE_MOUSE
| (DI8DEVTYPEMOUSE_TRADITIONAL
<< 8);
87 else instance
->dwDevType
= DIDEVTYPE_MOUSE
| (DIDEVTYPEMOUSE_TRADITIONAL
<< 8);
88 MultiByteToWideChar( CP_ACP
, 0, "Mouse", -1, instance
->tszInstanceName
, MAX_PATH
);
89 MultiByteToWideChar( CP_ACP
, 0, "Wine Mouse", -1, instance
->tszProductName
, MAX_PATH
);
94 HRESULT
mouse_create_device( struct dinput
*dinput
, const GUID
*guid
, IDirectInputDevice8W
**out
)
100 TRACE( "dinput %p, guid %s, out %p\n", dinput
, debugstr_guid( guid
), out
);
103 if (!IsEqualGUID( &GUID_SysMouse
, guid
)) return DIERR_DEVICENOTREG
;
105 if (!(impl
= calloc( 1, sizeof(*impl
) ))) return E_OUTOFMEMORY
;
106 dinput_device_init( &impl
->base
, &mouse_vtbl
, guid
, dinput
);
107 impl
->base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": struct mouse*->base.crit");
109 mouse_enum_device( 0, 0, &impl
->base
.instance
, dinput
->dwVersion
);
110 impl
->base
.caps
.dwDevType
= impl
->base
.instance
.dwDevType
;
111 impl
->base
.caps
.dwFirmwareRevision
= 100;
112 impl
->base
.caps
.dwHardwareRevision
= 100;
113 impl
->base
.dwCoopLevel
= DISCL_NONEXCLUSIVE
| DISCL_BACKGROUND
;
115 get_app_key(&hkey
, &appkey
);
116 if (!get_config_key( hkey
, appkey
, L
"MouseWarpOverride", buffer
, sizeof(buffer
) ))
118 if (!wcsnicmp( buffer
, L
"disable", -1 )) impl
->warp_override
= WARP_DISABLE
;
119 else if (!wcsnicmp( buffer
, L
"force", -1 )) impl
->warp_override
= WARP_FORCE_ON
;
121 if (appkey
) RegCloseKey(appkey
);
122 if (hkey
) RegCloseKey(hkey
);
124 if (dinput
->dwVersion
>= 0x0800)
126 impl
->base
.use_raw_input
= TRUE
;
127 impl
->base
.raw_device
.usUsagePage
= 1; /* HID generic device page */
128 impl
->base
.raw_device
.usUsage
= 2; /* HID generic mouse */
131 *out
= &impl
->base
.IDirectInputDevice8W_iface
;
135 void dinput_mouse_rawinput_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
, RAWINPUT
*ri
)
137 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
138 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
144 static const USHORT mouse_button_flags
[] =
146 RI_MOUSE_BUTTON_1_DOWN
, RI_MOUSE_BUTTON_1_UP
,
147 RI_MOUSE_BUTTON_2_DOWN
, RI_MOUSE_BUTTON_2_UP
,
148 RI_MOUSE_BUTTON_3_DOWN
, RI_MOUSE_BUTTON_3_UP
,
149 RI_MOUSE_BUTTON_4_DOWN
, RI_MOUSE_BUTTON_4_UP
,
150 RI_MOUSE_BUTTON_5_DOWN
, RI_MOUSE_BUTTON_5_UP
153 TRACE( "iface %p, wparam %#Ix, lparam %#Ix, ri %p.\n", iface
, wparam
, lparam
, ri
);
155 if (ri
->data
.mouse
.usFlags
& MOUSE_VIRTUAL_DESKTOP
)
156 FIXME( "Unimplemented MOUSE_VIRTUAL_DESKTOP flag\n" );
157 if (ri
->data
.mouse
.usFlags
& MOUSE_ATTRIBUTES_CHANGED
)
158 FIXME( "Unimplemented MOUSE_ATTRIBUTES_CHANGED flag\n" );
160 EnterCriticalSection( &impl
->base
.crit
);
161 seq
= impl
->base
.dinput
->evsequence
++;
163 rel
.x
= ri
->data
.mouse
.lLastX
;
164 rel
.y
= ri
->data
.mouse
.lLastY
;
165 if (ri
->data
.mouse
.usFlags
& MOUSE_MOVE_ABSOLUTE
)
175 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
187 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
188 pt
.x
, GetCurrentTime(), seq
);
194 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
195 pt
.y
, GetCurrentTime(), seq
);
201 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
202 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
203 impl
->need_warp
= TRUE
;
206 if (ri
->data
.mouse
.usButtonFlags
& RI_MOUSE_WHEEL
)
208 state
->lZ
+= (wdata
= (SHORT
)ri
->data
.mouse
.usButtonData
);
209 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
210 wdata
, GetCurrentTime(), seq
);
214 for (i
= 0; i
< ARRAY_SIZE(mouse_button_flags
); ++i
)
216 if (ri
->data
.mouse
.usButtonFlags
& mouse_button_flags
[i
])
218 state
->rgbButtons
[i
/ 2] = 0x80 - (i
% 2) * 0x80;
219 queue_event( iface
, DIDFT_MAKEINSTANCE( WINE_MOUSE_BUTTONS_INSTANCE
+ (i
/ 2) ) | DIDFT_PSHBUTTON
,
220 state
->rgbButtons
[i
/ 2], GetCurrentTime(), seq
);
225 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
226 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
227 state
->lX
, state
->lY
, state
->lZ
);
229 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
230 LeaveCriticalSection( &impl
->base
.crit
);
233 /* low-level mouse hook */
234 int dinput_mouse_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
)
236 MSLLHOOKSTRUCT
*hook
= (MSLLHOOKSTRUCT
*)lparam
;
237 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
238 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
239 int wdata
= 0, inst_id
= -1, ret
= 0;
242 TRACE( "iface %p, msg %#Ix, x %+ld, y %+ld\n", iface
, wparam
, hook
->pt
.x
, hook
->pt
.y
);
244 EnterCriticalSection( &impl
->base
.crit
);
252 state
->lX
+= pt
.x
= hook
->pt
.x
- pt
.x
;
253 state
->lY
+= pt
.y
= hook
->pt
.y
- pt
.y
;
255 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
264 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
269 /* Already have X, need to queue it */
272 queue_event( iface
, inst_id
, wdata
, GetCurrentTime(), impl
->base
.dinput
->evsequence
);
275 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
281 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
282 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
283 impl
->need_warp
= TRUE
;
288 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
289 state
->lZ
+= wdata
= (short)HIWORD( hook
->mouseData
);
290 /* FarCry crashes if it gets a mouse wheel message */
291 /* FIXME: should probably filter out other messages too */
295 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
296 state
->rgbButtons
[0] = wdata
= 0x80;
299 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
300 state
->rgbButtons
[0] = wdata
= 0x00;
303 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
304 state
->rgbButtons
[1] = wdata
= 0x80;
307 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
308 state
->rgbButtons
[1] = wdata
= 0x00;
311 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
312 state
->rgbButtons
[2] = wdata
= 0x80;
315 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
316 state
->rgbButtons
[2] = wdata
= 0x00;
319 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
320 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x80;
323 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
324 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x00;
331 queue_event( iface
, inst_id
, wdata
, GetCurrentTime(), impl
->base
.dinput
->evsequence
++ );
335 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
336 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
337 state
->lX
, state
->lY
, state
->lZ
);
339 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
340 LeaveCriticalSection( &impl
->base
.crit
);
344 static void warp_check( struct mouse
*impl
, BOOL force
)
346 DWORD now
= GetCurrentTime();
347 const DWORD interval
= impl
->clipped
? 500 : 10;
349 if (force
|| (impl
->need_warp
&& (now
- impl
->last_warped
> interval
)))
354 impl
->last_warped
= now
;
355 impl
->need_warp
= FALSE
;
356 if (!GetClientRect( impl
->base
.win
, &rect
)) return;
357 MapWindowPoints( impl
->base
.win
, 0, (POINT
*)&rect
, 2 );
360 mapped_center
.x
= (rect
.left
+ rect
.right
) / 2;
361 mapped_center
.y
= (rect
.top
+ rect
.bottom
) / 2;
362 TRACE( "Warping mouse to x %+ld, y %+ld.\n", mapped_center
.x
, mapped_center
.y
);
363 SetCursorPos( mapped_center
.x
, mapped_center
.y
);
365 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
367 /* make sure we clip even if the window covers the whole screen */
368 rect
.left
= max( rect
.left
, GetSystemMetrics( SM_XVIRTUALSCREEN
) + 1 );
369 rect
.top
= max( rect
.top
, GetSystemMetrics( SM_YVIRTUALSCREEN
) + 1 );
370 rect
.right
= min( rect
.right
, rect
.left
+ GetSystemMetrics( SM_CXVIRTUALSCREEN
) - 2 );
371 rect
.bottom
= min( rect
.bottom
, rect
.top
+ GetSystemMetrics( SM_CYVIRTUALSCREEN
) - 2 );
372 TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect
));
374 impl
->clipped
= GetClipCursor( &new_rect
) && EqualRect( &rect
, &new_rect
);
379 static HRESULT
mouse_poll( IDirectInputDevice8W
*iface
)
381 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
382 check_dinput_events();
383 warp_check( impl
, FALSE
);
387 static HRESULT
mouse_acquire( IDirectInputDevice8W
*iface
)
389 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
390 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
393 /* Init the mouse state */
394 GetCursorPos( &point
);
395 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
404 impl
->org_coords
= point
;
407 state
->rgbButtons
[0] = GetKeyState( VK_LBUTTON
) & 0x80;
408 state
->rgbButtons
[1] = GetKeyState( VK_RBUTTON
) & 0x80;
409 state
->rgbButtons
[2] = GetKeyState( VK_MBUTTON
) & 0x80;
411 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
413 ShowCursor( FALSE
); /* hide cursor */
414 warp_check( impl
, TRUE
);
416 else if (impl
->warp_override
== WARP_FORCE_ON
)
418 /* Need a window to warp mouse in. */
419 if (!impl
->base
.win
) impl
->base
.win
= GetDesktopWindow();
420 warp_check( impl
, TRUE
);
422 else if (impl
->clipped
)
425 impl
->clipped
= FALSE
;
431 static HRESULT
mouse_unacquire( IDirectInputDevice8W
*iface
)
433 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
435 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
438 ShowCursor( TRUE
); /* show cursor */
439 impl
->clipped
= FALSE
;
442 /* And put the mouse cursor back where it was at acquire time */
443 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
|| impl
->warp_override
== WARP_FORCE_ON
)
445 TRACE( "warping mouse back to %s\n", wine_dbgstr_point( &impl
->org_coords
) );
446 SetCursorPos( impl
->org_coords
.x
, impl
->org_coords
.y
);
452 static BOOL
try_enum_object( const DIPROPHEADER
*filter
, DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
,
453 DIDEVICEOBJECTINSTANCEW
*instance
, void *data
)
455 if (flags
!= DIDFT_ALL
&& !(flags
& DIDFT_GETTYPE( instance
->dwType
))) return DIENUM_CONTINUE
;
457 switch (filter
->dwHow
)
460 return callback( instance
, data
);
462 if (filter
->dwObj
!= instance
->dwOfs
) return DIENUM_CONTINUE
;
463 return callback( instance
, data
);
465 if ((filter
->dwObj
& 0x00ffffff) != (instance
->dwType
& 0x00ffffff)) return DIENUM_CONTINUE
;
466 return callback( instance
, data
);
469 return DIENUM_CONTINUE
;
472 static HRESULT
mouse_enum_objects( IDirectInputDevice8W
*iface
, const DIPROPHEADER
*filter
,
473 DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
, void *context
)
475 DIDEVICEOBJECTINSTANCEW instances
[] =
478 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
479 .guidType
= GUID_XAxis
,
481 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(0),
482 .dwFlags
= DIDOI_ASPECTPOSITION
,
483 .tszName
= L
"X-axis",
486 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
487 .guidType
= GUID_YAxis
,
489 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(1),
490 .dwFlags
= DIDOI_ASPECTPOSITION
,
491 .tszName
= L
"Y-axis",
494 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
495 .guidType
= GUID_ZAxis
,
497 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(2),
498 .dwFlags
= DIDOI_ASPECTPOSITION
,
502 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
503 .guidType
= GUID_Button
,
504 .dwOfs
= DIMOFS_BUTTON0
,
505 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(3),
506 .tszName
= L
"Button 0",
509 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
510 .guidType
= GUID_Button
,
511 .dwOfs
= DIMOFS_BUTTON1
,
512 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(4),
513 .tszName
= L
"Button 1",
516 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
517 .guidType
= GUID_Button
,
518 .dwOfs
= DIMOFS_BUTTON2
,
519 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(5),
520 .tszName
= L
"Button 2",
523 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
524 .guidType
= GUID_Button
,
525 .dwOfs
= DIMOFS_BUTTON3
,
526 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(6),
527 .tszName
= L
"Button 3",
530 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
531 .guidType
= GUID_Button
,
532 .dwOfs
= DIMOFS_BUTTON4
,
533 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(7),
534 .tszName
= L
"Button 4",
540 for (i
= 0; i
< ARRAY_SIZE(instances
); ++i
)
542 ret
= try_enum_object( filter
, flags
, callback
, instances
+ i
, context
);
543 if (ret
!= DIENUM_CONTINUE
) return DIENUM_STOP
;
546 return DIENUM_CONTINUE
;
549 static const struct dinput_device_vtbl mouse_vtbl
=