mshtml: Implement MediaQueryList's addListener method.
[wine.git] / dlls / dinput / mouse.c
blobec30c8257334398226958f6e4d025263d392a187
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
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winternl.h"
29 #include "winuser.h"
30 #include "winerror.h"
31 #include "winreg.h"
32 #include "dinput.h"
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;
42 typedef enum
44 WARP_DEFAULT,
45 WARP_DISABLE,
46 WARP_FORCE_ON
47 } WARP_MOUSE;
49 struct mouse
51 struct dinput_device base;
53 /* These are used in case of relative -> absolute transitions */
54 POINT org_coords;
55 BOOL clipped;
56 /* warping: whether we need to move mouse back to middle once we
57 * reach window borders (for e.g. shooters, "surface movement" games) */
58 BOOL need_warp;
59 DWORD last_warped;
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 )
71 DWORD size;
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 );
85 return DI_OK;
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;
105 POINT rel, pt;
106 DWORD seq;
107 BOOL notify = FALSE;
108 int i;
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)
133 GetCursorPos( &pt );
134 rel.x -= pt.x;
135 rel.y -= pt.y;
138 state->lX += rel.x;
139 state->lY += rel.y;
141 if (impl->base.user_format.dwFlags & DIDF_ABSAXIS)
143 pt.x = state->lX;
144 pt.y = state->lY;
146 else
148 pt = rel;
151 if (rel.x)
153 queue_event( iface, 0, pt.x, GetCurrentTime(), seq );
154 notify = TRUE;
157 if (rel.y)
159 queue_event( iface, 1, pt.y, GetCurrentTime(), seq );
160 notify = TRUE;
163 if (rel.x || rel.y)
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 );
174 notify = TRUE;
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 );
183 notify = TRUE;
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;
200 BOOL notify = FALSE;
201 int ret = 0;
202 DWORD seq;
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++;
209 switch(wparam) {
210 case WM_MOUSEMOVE:
212 POINT pt, pt1;
214 GetCursorPos(&pt);
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)
220 pt1.x = state->lX;
221 pt1.y = state->lY;
222 } else
223 pt1 = pt;
225 if (pt.x)
227 queue_event( iface, 0, pt1.x, GetCurrentTime(), seq );
228 notify = TRUE;
230 if (pt.y)
232 queue_event( iface, 1, pt1.y, GetCurrentTime(), seq );
233 notify = TRUE;
236 if (pt.x || pt.y)
238 if ((impl->warp_override == WARP_FORCE_ON) ||
239 (impl->warp_override != WARP_DISABLE && (impl->base.dwCoopLevel & DISCL_EXCLUSIVE)))
240 impl->need_warp = TRUE;
242 break;
244 case WM_MOUSEWHEEL:
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 */
249 ret = impl->clipped;
250 break;
251 case WM_LBUTTONDOWN:
252 state->rgbButtons[0] = 0x80;
253 queue_event( iface, 3, 0x80, GetCurrentTime(), seq );
254 notify = TRUE;
255 break;
256 case WM_LBUTTONUP:
257 state->rgbButtons[0] = 0x00;
258 queue_event( iface, 3, 0x00, GetCurrentTime(), seq );
259 notify = TRUE;
260 break;
261 case WM_RBUTTONDOWN:
262 state->rgbButtons[1] = 0x80;
263 queue_event( iface, 4, 0x80, GetCurrentTime(), seq );
264 notify = TRUE;
265 break;
266 case WM_RBUTTONUP:
267 state->rgbButtons[1] = 0x00;
268 queue_event( iface, 4, 0x00, GetCurrentTime(), seq );
269 notify = TRUE;
270 break;
271 case WM_MBUTTONDOWN:
272 state->rgbButtons[2] = 0x80;
273 queue_event( iface, 5, 0x80, GetCurrentTime(), seq );
274 notify = TRUE;
275 break;
276 case WM_MBUTTONUP:
277 state->rgbButtons[2] = 0x00;
278 queue_event( iface, 5, 0x00, GetCurrentTime(), seq );
279 notify = TRUE;
280 break;
281 case WM_XBUTTONDOWN:
282 state->rgbButtons[2 + HIWORD( hook->mouseData )] = 0x80;
283 queue_event( iface, 5 + HIWORD(hook->mouseData), 0x80, GetCurrentTime(), seq );
284 notify = TRUE;
285 break;
286 case WM_XBUTTONUP:
287 state->rgbButtons[2 + HIWORD( hook->mouseData )] = 0x00;
288 queue_event( iface, 5 + HIWORD(hook->mouseData), 0x00, GetCurrentTime(), seq );
289 notify = TRUE;
290 break;
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 );
299 return ret;
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)))
309 RECT rect, new_rect;
310 POINT mapped_center;
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 );
316 if (!impl->clipped)
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 ));
331 ClipCursor( &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 );
342 return DI_OK;
345 static HRESULT mouse_acquire( IDirectInputDevice8W *iface )
347 struct mouse *impl = impl_from_IDirectInputDevice8W( iface );
348 DIMOUSESTATE2 *state = (DIMOUSESTATE2 *)impl->base.device_state;
349 POINT point;
351 GetCursorPos( &point );
352 if (impl->base.user_format.dwFlags & DIDF_ABSAXIS)
354 state->lX = point.x;
355 state->lY = point.y;
357 else
359 state->lX = 0;
360 state->lY = 0;
361 impl->org_coords = point;
363 state->lZ = 0;
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)
370 ShowCursor( FALSE );
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)
381 ClipCursor( NULL );
382 impl->clipped = FALSE;
385 return DI_OK;
388 static HRESULT mouse_unacquire( IDirectInputDevice8W *iface )
390 struct mouse *impl = impl_from_IDirectInputDevice8W( iface );
392 if (impl->base.dwCoopLevel & DISCL_EXCLUSIVE)
394 ClipCursor( NULL );
395 ShowCursor( TRUE );
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 );
406 return DI_OK;
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)
416 case DIPH_DEVICE:
417 return callback( impl, index, NULL, instance, data );
418 case DIPH_BYOFFSET:
419 if (filter->dwObj != instance->dwOfs) return DIENUM_CONTINUE;
420 return callback( impl, index, NULL, instance, data );
421 case DIPH_BYID:
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,
438 .dwOfs = DIMOFS_X,
439 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(0),
440 .dwFlags = DIDOI_ASPECTPOSITION,
441 .tszName = L"X-axis",
444 .dwSize = sizeof(DIDEVICEOBJECTINSTANCEW),
445 .guidType = GUID_YAxis,
446 .dwOfs = DIMOFS_Y,
447 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(1),
448 .dwFlags = DIDOI_ASPECTPOSITION,
449 .tszName = L"Y-axis",
452 .dwSize = sizeof(DIDEVICEOBJECTINSTANCEW),
453 .guidType = GUID_ZAxis,
454 .dwOfs = DIMOFS_Z,
455 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(2),
456 .dwFlags = DIDOI_ASPECTPOSITION,
457 .tszName = L"Wheel",
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",
495 BOOL ret;
496 DWORD i;
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,
515 struct mouse *impl;
516 HKEY hkey, appkey;
517 WCHAR buffer[20];
518 HRESULT hr;
520 TRACE( "dinput %p, guid %s, out %p\n", dinput, debugstr_guid( guid ), out );
522 *out = NULL;
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;
549 return DI_OK;
551 failed:
552 IDirectInputDevice_Release( &impl->base.IDirectInputDevice8W_iface );
553 return hr;
556 static const struct dinput_device_vtbl mouse_vtbl =
558 NULL,
559 mouse_poll,
560 NULL,
561 mouse_acquire,
562 mouse_unacquire,
563 mouse_enum_objects,
564 NULL,
565 NULL,
566 NULL,
567 NULL,
568 NULL,
569 NULL,