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 static BOOL CALLBACK
init_object_properties( const DIDEVICEOBJECTINSTANCEW
*instance
, void *data
)
96 struct mouse
*impl
= (struct mouse
*)data
;
97 struct object_properties
*properties
= impl
->base
.object_properties
+ instance
->dwOfs
/ sizeof(LONG
);
99 properties
->range_min
= DIPROPRANGE_NOMIN
;
100 properties
->range_max
= DIPROPRANGE_NOMAX
;
102 /* The z-axis (wheel) has a different granularity */
103 if (instance
->dwOfs
== DIMOFS_Z
)
104 properties
->granularity
= WHEEL_DELTA
;
106 properties
->granularity
= 1;
108 return DIENUM_CONTINUE
;
111 HRESULT
mouse_create_device( struct dinput
*dinput
, const GUID
*guid
, IDirectInputDevice8W
**out
)
117 TRACE( "dinput %p, guid %s, out %p\n", dinput
, debugstr_guid( guid
), out
);
120 if (!IsEqualGUID( &GUID_SysMouse
, guid
)) return DIERR_DEVICENOTREG
;
122 if (!(impl
= calloc( 1, sizeof(*impl
) ))) return E_OUTOFMEMORY
;
123 dinput_device_init( &impl
->base
, &mouse_vtbl
, guid
, dinput
);
124 impl
->base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": struct mouse*->base.crit");
126 mouse_enum_device( 0, 0, &impl
->base
.instance
, dinput
->dwVersion
);
127 impl
->base
.caps
.dwDevType
= impl
->base
.instance
.dwDevType
;
128 impl
->base
.caps
.dwFirmwareRevision
= 100;
129 impl
->base
.caps
.dwHardwareRevision
= 100;
130 impl
->base
.dwCoopLevel
= DISCL_NONEXCLUSIVE
| DISCL_BACKGROUND
;
132 /* One object_properties per axis */
133 impl
->base
.object_properties
= calloc( 3, sizeof(struct object_properties
) );
134 if (!impl
->base
.object_properties
)
136 IDirectInputDevice_Release( &impl
->base
.IDirectInputDevice8W_iface
);
137 return E_OUTOFMEMORY
;
139 IDirectInputDevice8_EnumObjects( &impl
->base
.IDirectInputDevice8W_iface
, init_object_properties
, impl
, DIDFT_RELAXIS
);
141 get_app_key(&hkey
, &appkey
);
142 if (!get_config_key( hkey
, appkey
, L
"MouseWarpOverride", buffer
, sizeof(buffer
) ))
144 if (!wcsnicmp( buffer
, L
"disable", -1 )) impl
->warp_override
= WARP_DISABLE
;
145 else if (!wcsnicmp( buffer
, L
"force", -1 )) impl
->warp_override
= WARP_FORCE_ON
;
147 if (appkey
) RegCloseKey(appkey
);
148 if (hkey
) RegCloseKey(hkey
);
150 if (dinput
->dwVersion
>= 0x0800)
151 impl
->base
.use_raw_input
= TRUE
;
153 *out
= &impl
->base
.IDirectInputDevice8W_iface
;
157 void dinput_mouse_rawinput_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
, RAWINPUT
*ri
)
159 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
160 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
166 static const USHORT mouse_button_flags
[] =
168 RI_MOUSE_BUTTON_1_DOWN
, RI_MOUSE_BUTTON_1_UP
,
169 RI_MOUSE_BUTTON_2_DOWN
, RI_MOUSE_BUTTON_2_UP
,
170 RI_MOUSE_BUTTON_3_DOWN
, RI_MOUSE_BUTTON_3_UP
,
171 RI_MOUSE_BUTTON_4_DOWN
, RI_MOUSE_BUTTON_4_UP
,
172 RI_MOUSE_BUTTON_5_DOWN
, RI_MOUSE_BUTTON_5_UP
175 TRACE( "iface %p, wparam %#Ix, lparam %#Ix, ri %p.\n", iface
, wparam
, lparam
, ri
);
177 if (ri
->data
.mouse
.usFlags
& MOUSE_VIRTUAL_DESKTOP
)
178 FIXME( "Unimplemented MOUSE_VIRTUAL_DESKTOP flag\n" );
179 if (ri
->data
.mouse
.usFlags
& MOUSE_ATTRIBUTES_CHANGED
)
180 FIXME( "Unimplemented MOUSE_ATTRIBUTES_CHANGED flag\n" );
182 EnterCriticalSection( &impl
->base
.crit
);
183 seq
= impl
->base
.dinput
->evsequence
++;
185 rel
.x
= ri
->data
.mouse
.lLastX
;
186 rel
.y
= ri
->data
.mouse
.lLastY
;
187 if (ri
->data
.mouse
.usFlags
& MOUSE_MOVE_ABSOLUTE
)
197 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
209 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
210 pt
.x
, GetCurrentTime(), seq
);
216 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
217 pt
.y
, GetCurrentTime(), seq
);
223 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
224 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
225 impl
->need_warp
= TRUE
;
228 if (ri
->data
.mouse
.usButtonFlags
& RI_MOUSE_WHEEL
)
230 state
->lZ
+= (wdata
= (SHORT
)ri
->data
.mouse
.usButtonData
);
231 queue_event( iface
, DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
,
232 wdata
, GetCurrentTime(), seq
);
236 for (i
= 0; i
< ARRAY_SIZE(mouse_button_flags
); ++i
)
238 if (ri
->data
.mouse
.usButtonFlags
& mouse_button_flags
[i
])
240 state
->rgbButtons
[i
/ 2] = 0x80 - (i
% 2) * 0x80;
241 queue_event( iface
, DIDFT_MAKEINSTANCE( WINE_MOUSE_BUTTONS_INSTANCE
+ (i
/ 2) ) | DIDFT_PSHBUTTON
,
242 state
->rgbButtons
[i
/ 2], GetCurrentTime(), seq
);
247 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
248 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
249 state
->lX
, state
->lY
, state
->lZ
);
251 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
252 LeaveCriticalSection( &impl
->base
.crit
);
255 int dinput_mouse_hook( IDirectInputDevice8W
*iface
, WPARAM wparam
, LPARAM lparam
)
257 MSLLHOOKSTRUCT
*hook
= (MSLLHOOKSTRUCT
*)lparam
;
258 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
259 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
260 int wdata
= 0, inst_id
= -1, ret
= 0;
263 TRACE( "iface %p, msg %#Ix, x %+ld, y %+ld\n", iface
, wparam
, hook
->pt
.x
, hook
->pt
.y
);
265 EnterCriticalSection( &impl
->base
.crit
);
273 state
->lX
+= pt
.x
= hook
->pt
.x
- pt
.x
;
274 state
->lY
+= pt
.y
= hook
->pt
.y
- pt
.y
;
276 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
285 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
290 /* Already have X, need to queue it */
293 queue_event( iface
, inst_id
, wdata
, GetCurrentTime(), impl
->base
.dinput
->evsequence
);
296 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
302 if ((impl
->warp_override
== WARP_FORCE_ON
) ||
303 (impl
->warp_override
!= WARP_DISABLE
&& (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)))
304 impl
->need_warp
= TRUE
;
309 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE
) | DIDFT_RELAXIS
;
310 state
->lZ
+= wdata
= (short)HIWORD( hook
->mouseData
);
311 /* FarCry crashes if it gets a mouse wheel message */
312 /* FIXME: should probably filter out other messages too */
316 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
317 state
->rgbButtons
[0] = wdata
= 0x80;
320 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 0) | DIDFT_PSHBUTTON
;
321 state
->rgbButtons
[0] = wdata
= 0x00;
324 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
325 state
->rgbButtons
[1] = wdata
= 0x80;
328 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 1) | DIDFT_PSHBUTTON
;
329 state
->rgbButtons
[1] = wdata
= 0x00;
332 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
333 state
->rgbButtons
[2] = wdata
= 0x80;
336 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2) | DIDFT_PSHBUTTON
;
337 state
->rgbButtons
[2] = wdata
= 0x00;
340 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
341 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x80;
344 inst_id
= DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE
+ 2 + HIWORD(hook
->mouseData
)) | DIDFT_PSHBUTTON
;
345 state
->rgbButtons
[2 + HIWORD( hook
->mouseData
)] = wdata
= 0x00;
352 queue_event( iface
, inst_id
, wdata
, GetCurrentTime(), impl
->base
.dinput
->evsequence
++ );
356 TRACE( "buttons %02x %02x %02x %02x %02x, x %+ld, y %+ld, w %+ld\n", state
->rgbButtons
[0],
357 state
->rgbButtons
[1], state
->rgbButtons
[2], state
->rgbButtons
[3], state
->rgbButtons
[4],
358 state
->lX
, state
->lY
, state
->lZ
);
360 if (notify
&& impl
->base
.hEvent
) SetEvent( impl
->base
.hEvent
);
361 LeaveCriticalSection( &impl
->base
.crit
);
365 static void warp_check( struct mouse
*impl
, BOOL force
)
367 DWORD now
= GetCurrentTime();
368 const DWORD interval
= impl
->clipped
? 500 : 10;
370 if (force
|| (impl
->need_warp
&& (now
- impl
->last_warped
> interval
)))
375 impl
->last_warped
= now
;
376 impl
->need_warp
= FALSE
;
377 if (!GetClientRect( impl
->base
.win
, &rect
)) return;
378 MapWindowPoints( impl
->base
.win
, 0, (POINT
*)&rect
, 2 );
381 mapped_center
.x
= (rect
.left
+ rect
.right
) / 2;
382 mapped_center
.y
= (rect
.top
+ rect
.bottom
) / 2;
383 TRACE( "Warping mouse to x %+ld, y %+ld.\n", mapped_center
.x
, mapped_center
.y
);
384 SetCursorPos( mapped_center
.x
, mapped_center
.y
);
386 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
388 /* make sure we clip even if the window covers the whole screen */
389 rect
.left
= max( rect
.left
, GetSystemMetrics( SM_XVIRTUALSCREEN
) + 1 );
390 rect
.top
= max( rect
.top
, GetSystemMetrics( SM_YVIRTUALSCREEN
) + 1 );
391 rect
.right
= min( rect
.right
, rect
.left
+ GetSystemMetrics( SM_CXVIRTUALSCREEN
) - 2 );
392 rect
.bottom
= min( rect
.bottom
, rect
.top
+ GetSystemMetrics( SM_CYVIRTUALSCREEN
) - 2 );
393 TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect
));
395 impl
->clipped
= GetClipCursor( &new_rect
) && EqualRect( &rect
, &new_rect
);
400 static HRESULT
mouse_poll( IDirectInputDevice8W
*iface
)
402 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
403 check_dinput_events();
404 warp_check( impl
, FALSE
);
408 static HRESULT
mouse_acquire( IDirectInputDevice8W
*iface
)
410 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
411 DIMOUSESTATE2
*state
= (DIMOUSESTATE2
*)impl
->base
.device_state
;
414 GetCursorPos( &point
);
415 if (impl
->base
.user_format
.dwFlags
& DIDF_ABSAXIS
)
424 impl
->org_coords
= point
;
427 state
->rgbButtons
[0] = GetKeyState( VK_LBUTTON
) & 0x80;
428 state
->rgbButtons
[1] = GetKeyState( VK_RBUTTON
) & 0x80;
429 state
->rgbButtons
[2] = GetKeyState( VK_MBUTTON
) & 0x80;
431 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
434 warp_check( impl
, TRUE
);
436 else if (impl
->warp_override
== WARP_FORCE_ON
)
438 /* Need a window to warp mouse in. */
439 if (!impl
->base
.win
) impl
->base
.win
= GetDesktopWindow();
440 warp_check( impl
, TRUE
);
442 else if (impl
->clipped
)
445 impl
->clipped
= FALSE
;
451 static HRESULT
mouse_unacquire( IDirectInputDevice8W
*iface
)
453 struct mouse
*impl
= impl_from_IDirectInputDevice8W( iface
);
455 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
)
459 impl
->clipped
= FALSE
;
462 /* And put the mouse cursor back where it was at acquire time */
463 if (impl
->base
.dwCoopLevel
& DISCL_EXCLUSIVE
|| impl
->warp_override
== WARP_FORCE_ON
)
465 TRACE( "warping mouse back to %s\n", wine_dbgstr_point( &impl
->org_coords
) );
466 SetCursorPos( impl
->org_coords
.x
, impl
->org_coords
.y
);
472 static BOOL
try_enum_object( const DIPROPHEADER
*filter
, DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
,
473 DIDEVICEOBJECTINSTANCEW
*instance
, void *data
)
475 if (flags
!= DIDFT_ALL
&& !(flags
& DIDFT_GETTYPE( instance
->dwType
))) return DIENUM_CONTINUE
;
477 switch (filter
->dwHow
)
480 return callback( instance
, data
);
482 if (filter
->dwObj
!= instance
->dwOfs
) return DIENUM_CONTINUE
;
483 return callback( instance
, data
);
485 if ((filter
->dwObj
& 0x00ffffff) != (instance
->dwType
& 0x00ffffff)) return DIENUM_CONTINUE
;
486 return callback( instance
, data
);
489 return DIENUM_CONTINUE
;
492 static HRESULT
mouse_enum_objects( IDirectInputDevice8W
*iface
, const DIPROPHEADER
*filter
,
493 DWORD flags
, LPDIENUMDEVICEOBJECTSCALLBACKW callback
, void *context
)
495 DIDEVICEOBJECTINSTANCEW instances
[] =
498 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
499 .guidType
= GUID_XAxis
,
501 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(0),
502 .dwFlags
= DIDOI_ASPECTPOSITION
,
503 .tszName
= L
"X-axis",
506 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
507 .guidType
= GUID_YAxis
,
509 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(1),
510 .dwFlags
= DIDOI_ASPECTPOSITION
,
511 .tszName
= L
"Y-axis",
514 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
515 .guidType
= GUID_ZAxis
,
517 .dwType
= DIDFT_RELAXIS
|DIDFT_MAKEINSTANCE(2),
518 .dwFlags
= DIDOI_ASPECTPOSITION
,
522 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
523 .guidType
= GUID_Button
,
524 .dwOfs
= DIMOFS_BUTTON0
,
525 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(3),
526 .tszName
= L
"Button 0",
529 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
530 .guidType
= GUID_Button
,
531 .dwOfs
= DIMOFS_BUTTON1
,
532 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(4),
533 .tszName
= L
"Button 1",
536 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
537 .guidType
= GUID_Button
,
538 .dwOfs
= DIMOFS_BUTTON2
,
539 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(5),
540 .tszName
= L
"Button 2",
543 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
544 .guidType
= GUID_Button
,
545 .dwOfs
= DIMOFS_BUTTON3
,
546 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(6),
547 .tszName
= L
"Button 3",
550 .dwSize
= sizeof(DIDEVICEOBJECTINSTANCEW
),
551 .guidType
= GUID_Button
,
552 .dwOfs
= DIMOFS_BUTTON4
,
553 .dwType
= DIDFT_PSHBUTTON
|DIDFT_MAKEINSTANCE(7),
554 .tszName
= L
"Button 4",
560 for (i
= 0; i
< ARRAY_SIZE(instances
); ++i
)
562 ret
= try_enum_object( filter
, flags
, callback
, instances
+ i
, context
);
563 if (ret
!= DIENUM_CONTINUE
) return DIENUM_STOP
;
566 return DIENUM_CONTINUE
;
569 static const struct dinput_device_vtbl mouse_vtbl
=