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
)
101 TRACE( "dinput %p, guid %s, out %p\n", dinput
, debugstr_guid( guid
), out
);
104 if (!IsEqualGUID( &GUID_SysMouse
, guid
)) return DIERR_DEVICENOTREG
;
106 if (FAILED(hr
= dinput_device_alloc( sizeof(struct mouse
), &mouse_vtbl
, guid
, dinput
, (void **)&impl
)))
108 impl
->base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": struct mouse*->base.crit");
110 mouse_enum_device( 0, 0, &impl
->base
.instance
, dinput
->dwVersion
);
111 impl
->base
.caps
.dwDevType
= impl
->base
.instance
.dwDevType
;
112 impl
->base
.caps
.dwFirmwareRevision
= 100;
113 impl
->base
.caps
.dwHardwareRevision
= 100;
114 impl
->base
.dwCoopLevel
= DISCL_NONEXCLUSIVE
| DISCL_BACKGROUND
;
116 get_app_key(&hkey
, &appkey
);
117 if (!get_config_key( hkey
, appkey
, L
"MouseWarpOverride", buffer
, sizeof(buffer
) ))
119 if (!wcsnicmp( buffer
, L
"disable", -1 )) impl
->warp_override
= WARP_DISABLE
;
120 else if (!wcsnicmp( buffer
, L
"force", -1 )) impl
->warp_override
= WARP_FORCE_ON
;
122 if (appkey
) RegCloseKey(appkey
);
123 if (hkey
) RegCloseKey(hkey
);
125 if (FAILED(hr
= dinput_device_init( &impl
->base
.IDirectInputDevice8W_iface
)))
127 IDirectInputDevice_Release( &impl
->base
.IDirectInputDevice8W_iface
);
131 if (dinput
->dwVersion
>= 0x0800)
133 impl
->base
.use_raw_input
= TRUE
;
134 impl
->base
.raw_device
.usUsagePage
= 1; /* HID generic device page */
135 impl
->base
.raw_device
.usUsage
= 2; /* HID generic mouse */
138 *out
= &impl
->base
.IDirectInputDevice8W_iface
;
142 void dinput_mouse_rawinput_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
, RAWINPUT
*ri
)
144 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
145 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
151 static const USHORT mouse_button_flags
[] =
153 RI_MOUSE_BUTTON_1_DOWN
, RI_MOUSE_BUTTON_1_UP
,
154 RI_MOUSE_BUTTON_2_DOWN
, RI_MOUSE_BUTTON_2_UP
,
155 RI_MOUSE_BUTTON_3_DOWN
, RI_MOUSE_BUTTON_3_UP
,
156 RI_MOUSE_BUTTON_4_DOWN
, RI_MOUSE_BUTTON_4_UP
,
157 RI_MOUSE_BUTTON_5_DOWN
, RI_MOUSE_BUTTON_5_UP
160 TRACE( "iface %p, wparam %#Ix, lparam %#Ix, ri %p.\n", iface
, wparam
, lparam
, ri
);
162 if (ri
->data
.mouse
.usFlags
& MOUSE_VIRTUAL_DESKTOP
)
163 FIXME( "Unimplemented MOUSE_VIRTUAL_DESKTOP flag\n" );
164 if (ri
->data
.mouse
.usFlags
& MOUSE_ATTRIBUTES_CHANGED
)
165 FIXME( "Unimplemented MOUSE_ATTRIBUTES_CHANGED flag\n" );
167 EnterCriticalSection( &impl
->base
.crit
);
168 seq
= impl
->base
.dinput
->evsequence
++;
170 rel
.x
= ri
->data
.mouse
.lLastX
;
171 rel
.y
= ri
->data
.mouse
.lLastY
;
172 if (ri
->data
.mouse
.usFlags
& MOUSE_MOVE_ABSOLUTE
)
182 if (impl
->base
.user_format
->dwFlags
& DIDF_ABSAXIS
)
194 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
195 pt
.x
, GetCurrentTime(), seq
);
201 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
202 pt
.y
, GetCurrentTime(), seq
);
208 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
209 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
210 impl
->need_warp
= TRUE
;
213 if (ri
->data
.mouse
.usButtonFlags
& RI_MOUSE_WHEEL
)
215 state
->lZ
+= (wdata
= (SHORT
)ri
->data
.mouse
.usButtonData
);
216 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
217 wdata
, GetCurrentTime(), seq
);
221 for (i
= 0; i
< ARRAY_SIZE(mouse_button_flags
); ++i
)
223 if (ri
->data
.mouse
.usButtonFlags
& mouse_button_flags
[i
])
225 state
->rgbButtons
[i
/ 2] = 0x80 - (i
% 2) * 0x80;
226 queue_event( iface
, DIDFT_MAKEINSTANCE( WINE_MOUSE_BUTTONS_INSTANCE
+ (i
/ 2) ) | DIDFT_PSHBUTTON
,
227 state
->rgbButtons
[i
/ 2], GetCurrentTime(), seq
);
232 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
233 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
234 state
->lX
, state
->lY
, state
->lZ
);
236 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
237 LeaveCriticalSection( &impl
->base
.crit
);
240 /* low-level mouse hook */
241 int dinput_mouse_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
)
243 MSLLHOOKSTRUCT
*hook
= (MSLLHOOKSTRUCT
*)lparam
;
244 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
245 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
246 int wdata
= 0, inst_id
= -1, ret
= 0;
249 TRACE( "iface %p, msg %#Ix, x %+ld, y %+ld\n", iface
, wparam
, hook
->pt
.x
, hook
->pt
.y
);
251 EnterCriticalSection( &impl
->base
.crit
);
259 state
->lX
+= pt
.x
= hook
->pt
.x
- pt
.x
;
260 state
->lY
+= pt
.y
= hook
->pt
.y
- pt
.y
;
262 if (impl
->base
.user_format
->dwFlags
& DIDF_ABSAXIS
)
271 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
276 /* Already have X, need to queue it */
279 queue_event( iface
, inst_id
, wdata
, GetCurrentTime(), impl
->base
.dinput
->evsequence
);
282 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
288 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
289 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
290 impl
->need_warp
= TRUE
;
295 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
296 state
->lZ
+= wdata
= (short)HIWORD( hook
->mouseData
);
297 /* FarCry crashes if it gets a mouse wheel message */
298 /* FIXME: should probably filter out other messages too */
302 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
303 state
->rgbButtons
[0] = wdata
= 0x80;
306 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
307 state
->rgbButtons
[0] = wdata
= 0x00;
310 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
311 state
->rgbButtons
[1] = wdata
= 0x80;
314 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
315 state
->rgbButtons
[1] = wdata
= 0x00;
318 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
319 state
->rgbButtons
[2] = wdata
= 0x80;
322 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
323 state
->rgbButtons
[2] = wdata
= 0x00;
326 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
327 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x80;
330 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
331 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x00;
338 queue_event( iface
, inst_id
, wdata
, GetCurrentTime(), impl
->base
.dinput
->evsequence
++ );
342 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
343 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
344 state
->lX
, state
->lY
, state
->lZ
);
346 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
347 LeaveCriticalSection( &impl
->base
.crit
);
351 static void warp_check( struct mouse
*impl
, BOOL force
)
353 DWORD now
= GetCurrentTime();
354 const DWORD interval
= impl
->clipped
? 500 : 10;
356 if (force
|| (impl
->need_warp
&& (now
- impl
->last_warped
> interval
)))
361 impl
->last_warped
= now
;
362 impl
->need_warp
= FALSE
;
363 if (!GetClientRect( impl
->base
.win
, &rect
)) return;
364 MapWindowPoints( impl
->base
.win
, 0, (POINT
*)&rect
, 2 );
367 mapped_center
.x
= (rect
.left
+ rect
.right
) / 2;
368 mapped_center
.y
= (rect
.top
+ rect
.bottom
) / 2;
369 TRACE( "Warping mouse to x %+ld, y %+ld.\n", mapped_center
.x
, mapped_center
.y
);
370 SetCursorPos( mapped_center
.x
, mapped_center
.y
);
372 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
374 /* make sure we clip even if the window covers the whole screen */
375 rect
.left
= max( rect
.left
, GetSystemMetrics( SM_XVIRTUALSCREEN
) + 1 );
376 rect
.top
= max( rect
.top
, GetSystemMetrics( SM_YVIRTUALSCREEN
) + 1 );
377 rect
.right
= min( rect
.right
, rect
.left
+ GetSystemMetrics( SM_CXVIRTUALSCREEN
) - 2 );
378 rect
.bottom
= min( rect
.bottom
, rect
.top
+ GetSystemMetrics( SM_CYVIRTUALSCREEN
) - 2 );
379 TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect
));
381 impl
->clipped
= GetClipCursor( &new_rect
) && EqualRect( &rect
, &new_rect
);
386 static HRESULT
mouse_poll( IDirectInputDevice8W
*iface
)
388 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
389 check_dinput_events();
390 warp_check( impl
, FALSE
);
394 static HRESULT
mouse_acquire( IDirectInputDevice8W
*iface
)
396 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
397 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
400 /* Init the mouse state */
401 GetCursorPos( &point
);
402 if (impl
->base
.user_format
->dwFlags
& DIDF_ABSAXIS
)
411 impl
->org_coords
= point
;
414 state
->rgbButtons
[0] = GetKeyState( VK_LBUTTON
) & 0x80;
415 state
->rgbButtons
[1] = GetKeyState( VK_RBUTTON
) & 0x80;
416 state
->rgbButtons
[2] = GetKeyState( VK_MBUTTON
) & 0x80;
418 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
420 ShowCursor( FALSE
); /* hide cursor */
421 warp_check( impl
, TRUE
);
423 else if (impl
->warp_override
== WARP_FORCE_ON
)
425 /* Need a window to warp mouse in. */
426 if (!impl
->base
.win
) impl
->base
.win
= GetDesktopWindow();
427 warp_check( impl
, TRUE
);
429 else if (impl
->clipped
)
432 impl
->clipped
= FALSE
;
438 static HRESULT
mouse_unacquire( IDirectInputDevice8W
*iface
)
440 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
442 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
445 ShowCursor( TRUE
); /* show cursor */
446 impl
->clipped
= FALSE
;
449 /* And put the mouse cursor back where it was at acquire time */
450 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
|| impl
->warp_override
== WARP_FORCE_ON
)
452 TRACE( "warping mouse back to %s\n", wine_dbgstr_point( &impl
->org_coords
) );
453 SetCursorPos( impl
->org_coords
.x
, impl
->org_coords
.y
);
459 static BOOL
try_enum_object( const DIPROPHEADER
*filter
, DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
,
460 DIDEVICEOBJECTINSTANCEW
*instance
, void *data
)
462 if (flags
!= DIDFT_ALL
&& !(flags
& DIDFT_GETTYPE( instance
->dwType
))) return DIENUM_CONTINUE
;
464 switch (filter
->dwHow
)
467 return callback( instance
, data
);
469 if (filter
->dwObj
!= instance
->dwOfs
) return DIENUM_CONTINUE
;
470 return callback( instance
, data
);
472 if ((filter
->dwObj
& 0x00ffffff) != (instance
->dwType
& 0x00ffffff)) return DIENUM_CONTINUE
;
473 return callback( instance
, data
);
476 return DIENUM_CONTINUE
;
479 static HRESULT
mouse_enum_objects( IDirectInputDevice8W
*iface
, const DIPROPHEADER
*filter
,
480 DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
, void *context
)
482 DIDEVICEOBJECTINSTANCEW instances
[] =
485 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
486 .guidType
= GUID_XAxis
,
488 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(0),
489 .dwFlags
= DIDOI_ASPECTPOSITION
,
490 .tszName
= L
"X-axis",
493 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
494 .guidType
= GUID_YAxis
,
496 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(1),
497 .dwFlags
= DIDOI_ASPECTPOSITION
,
498 .tszName
= L
"Y-axis",
501 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
502 .guidType
= GUID_ZAxis
,
504 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(2),
505 .dwFlags
= DIDOI_ASPECTPOSITION
,
509 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
510 .guidType
= GUID_Button
,
511 .dwOfs
= DIMOFS_BUTTON0
,
512 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(3),
513 .tszName
= L
"Button 0",
516 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
517 .guidType
= GUID_Button
,
518 .dwOfs
= DIMOFS_BUTTON1
,
519 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(4),
520 .tszName
= L
"Button 1",
523 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
524 .guidType
= GUID_Button
,
525 .dwOfs
= DIMOFS_BUTTON2
,
526 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(5),
527 .tszName
= L
"Button 2",
530 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
531 .guidType
= GUID_Button
,
532 .dwOfs
= DIMOFS_BUTTON3
,
533 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(6),
534 .tszName
= L
"Button 3",
537 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
538 .guidType
= GUID_Button
,
539 .dwOfs
= DIMOFS_BUTTON4
,
540 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(7),
541 .tszName
= L
"Button 4",
547 for (i
= 0; i
< ARRAY_SIZE(instances
); ++i
)
549 ret
= try_enum_object( filter
, flags
, callback
, instances
+ i
, context
);
550 if (ret
!= DIENUM_CONTINUE
) return DIENUM_STOP
;
553 return DIENUM_CONTINUE
;
556 static const struct dinput_device_vtbl mouse_vtbl
=