comctl32/tests: Flush events before testing edit control IME messages.
[wine.git] / dlls / dinput / mouse.c
blob1572e73bac2537507cdd7a1362dd38893c6a1458
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 /* 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;
48 typedef enum
50 WARP_DEFAULT,
51 WARP_DISABLE,
52 WARP_FORCE_ON
53 } WARP_MOUSE;
55 struct mouse
57 struct dinput_device base;
59 /* These are used in case of relative -> absolute transitions */
60 POINT org_coords;
61 BOOL clipped;
62 /* warping: whether we need to move mouse back to middle once we
63 * reach window borders (for e.g. shooters, "surface movement" games) */
64 BOOL need_warp;
65 DWORD last_warped;
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 )
77 DWORD size;
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 );
91 return DI_OK;
94 HRESULT mouse_create_device( struct dinput *dinput, const GUID *guid, IDirectInputDevice8W **out )
96 struct mouse *impl;
97 HKEY hkey, appkey;
98 WCHAR buffer[20];
100 TRACE( "dinput %p, guid %s, out %p\n", dinput, debugstr_guid( guid ), out );
102 *out = NULL;
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;
132 return DI_OK;
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;
139 POINT rel, pt;
140 DWORD seq;
141 int i, wdata = 0;
142 BOOL notify = FALSE;
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)
167 GetCursorPos( &pt );
168 rel.x -= pt.x;
169 rel.y -= pt.y;
172 state->lX += rel.x;
173 state->lY += rel.y;
175 if (impl->base.user_format.dwFlags & DIDF_ABSAXIS)
177 pt.x = state->lX;
178 pt.y = state->lY;
180 else
182 pt = rel;
185 if (rel.x)
187 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS,
188 pt.x, GetCurrentTime(), seq );
189 notify = TRUE;
192 if (rel.y)
194 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS,
195 pt.y, GetCurrentTime(), seq );
196 notify = TRUE;
199 if (rel.x || rel.y)
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 );
211 notify = TRUE;
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 );
221 notify = TRUE;
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;
240 BOOL notify = FALSE;
242 TRACE( "iface %p, msg %#Ix, x %+ld, y %+ld\n", iface, wparam, hook->pt.x, hook->pt.y );
244 EnterCriticalSection( &impl->base.crit );
246 switch(wparam) {
247 case WM_MOUSEMOVE:
249 POINT pt, pt1;
251 GetCursorPos(&pt);
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)
257 pt1.x = state->lX;
258 pt1.y = state->lY;
259 } else
260 pt1 = pt;
262 if (pt.x)
264 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
265 wdata = pt1.x;
267 if (pt.y)
269 /* Already have X, need to queue it */
270 if (inst_id != -1)
272 queue_event( iface, inst_id, wdata, GetCurrentTime(), impl->base.dinput->evsequence );
273 notify = TRUE;
275 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
276 wdata = pt1.y;
279 if (pt.x || pt.y)
281 if ((impl->warp_override == WARP_FORCE_ON) ||
282 (impl->warp_override != WARP_DISABLE && (impl->base.dwCoopLevel & DISCL_EXCLUSIVE)))
283 impl->need_warp = TRUE;
285 break;
287 case WM_MOUSEWHEEL:
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 */
292 ret = impl->clipped;
293 break;
294 case WM_LBUTTONDOWN:
295 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
296 state->rgbButtons[0] = wdata = 0x80;
297 break;
298 case WM_LBUTTONUP:
299 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
300 state->rgbButtons[0] = wdata = 0x00;
301 break;
302 case WM_RBUTTONDOWN:
303 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
304 state->rgbButtons[1] = wdata = 0x80;
305 break;
306 case WM_RBUTTONUP:
307 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
308 state->rgbButtons[1] = wdata = 0x00;
309 break;
310 case WM_MBUTTONDOWN:
311 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
312 state->rgbButtons[2] = wdata = 0x80;
313 break;
314 case WM_MBUTTONUP:
315 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
316 state->rgbButtons[2] = wdata = 0x00;
317 break;
318 case WM_XBUTTONDOWN:
319 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
320 state->rgbButtons[2 + HIWORD( hook->mouseData )] = wdata = 0x80;
321 break;
322 case WM_XBUTTONUP:
323 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
324 state->rgbButtons[2 + HIWORD( hook->mouseData )] = wdata = 0x00;
325 break;
329 if (inst_id != -1)
331 queue_event( iface, inst_id, wdata, GetCurrentTime(), impl->base.dinput->evsequence++ );
332 notify = TRUE;
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 );
341 return ret;
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)))
351 RECT rect, new_rect;
352 POINT mapped_center;
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 );
358 if (!impl->clipped)
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 ));
373 ClipCursor( &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 );
384 return DI_OK;
387 static HRESULT mouse_acquire( IDirectInputDevice8W *iface )
389 struct mouse *impl = impl_from_IDirectInputDevice8W( iface );
390 DIMOUSESTATE2 *state = (DIMOUSESTATE2 *)impl->base.device_state;
391 POINT point;
393 /* Init the mouse state */
394 GetCursorPos( &point );
395 if (impl->base.user_format.dwFlags & DIDF_ABSAXIS)
397 state->lX = point.x;
398 state->lY = point.y;
400 else
402 state->lX = 0;
403 state->lY = 0;
404 impl->org_coords = point;
406 state->lZ = 0;
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)
424 ClipCursor( NULL );
425 impl->clipped = FALSE;
428 return DI_OK;
431 static HRESULT mouse_unacquire( IDirectInputDevice8W *iface )
433 struct mouse *impl = impl_from_IDirectInputDevice8W( iface );
435 if (impl->base.dwCoopLevel & DISCL_EXCLUSIVE)
437 ClipCursor( NULL );
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 );
449 return DI_OK;
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)
459 case DIPH_DEVICE:
460 return callback( instance, data );
461 case DIPH_BYOFFSET:
462 if (filter->dwObj != instance->dwOfs) return DIENUM_CONTINUE;
463 return callback( instance, data );
464 case DIPH_BYID:
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,
480 .dwOfs = DIMOFS_X,
481 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(0),
482 .dwFlags = DIDOI_ASPECTPOSITION,
483 .tszName = L"X-axis",
486 .dwSize = sizeof(DIDEVICEOBJECTINSTANCEW),
487 .guidType = GUID_YAxis,
488 .dwOfs = DIMOFS_Y,
489 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(1),
490 .dwFlags = DIDOI_ASPECTPOSITION,
491 .tszName = L"Y-axis",
494 .dwSize = sizeof(DIDEVICEOBJECTINSTANCEW),
495 .guidType = GUID_ZAxis,
496 .dwOfs = DIMOFS_Z,
497 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(2),
498 .dwFlags = DIDOI_ASPECTPOSITION,
499 .tszName = L"Wheel",
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",
537 BOOL ret;
538 DWORD i;
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 =
551 NULL,
552 mouse_poll,
553 NULL,
554 mouse_acquire,
555 mouse_unacquire,
556 mouse_enum_objects,
557 NULL,
558 NULL,
559 NULL,
560 NULL,
561 NULL,
562 NULL,