version/tests: Enable compilation with long types.
[wine.git] / dlls / dinput / mouse.c
blob46c065d78faa336d0cf91855238bd7af565eda5c
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];
99 HRESULT hr;
101 TRACE( "dinput %p, guid %s, out %p\n", dinput, debugstr_guid( guid ), out );
103 *out = NULL;
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 )))
107 return hr;
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 );
128 return hr;
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;
139 return DI_OK;
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;
146 POINT rel, pt;
147 DWORD seq;
148 int i, wdata = 0;
149 BOOL notify = FALSE;
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)
174 GetCursorPos( &pt );
175 rel.x -= pt.x;
176 rel.y -= pt.y;
179 state->lX += rel.x;
180 state->lY += rel.y;
182 if (impl->base.user_format->dwFlags & DIDF_ABSAXIS)
184 pt.x = state->lX;
185 pt.y = state->lY;
187 else
189 pt = rel;
192 if (rel.x)
194 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS,
195 pt.x, GetCurrentTime(), seq );
196 notify = TRUE;
199 if (rel.y)
201 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS,
202 pt.y, GetCurrentTime(), seq );
203 notify = TRUE;
206 if (rel.x || rel.y)
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 );
218 notify = TRUE;
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 );
228 notify = TRUE;
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;
247 BOOL notify = FALSE;
249 TRACE( "iface %p, msg %#Ix, x %+ld, y %+ld\n", iface, wparam, hook->pt.x, hook->pt.y );
251 EnterCriticalSection( &impl->base.crit );
253 switch(wparam) {
254 case WM_MOUSEMOVE:
256 POINT pt, pt1;
258 GetCursorPos(&pt);
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)
264 pt1.x = state->lX;
265 pt1.y = state->lY;
266 } else
267 pt1 = pt;
269 if (pt.x)
271 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
272 wdata = pt1.x;
274 if (pt.y)
276 /* Already have X, need to queue it */
277 if (inst_id != -1)
279 queue_event( iface, inst_id, wdata, GetCurrentTime(), impl->base.dinput->evsequence );
280 notify = TRUE;
282 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
283 wdata = pt1.y;
286 if (pt.x || pt.y)
288 if ((impl->warp_override == WARP_FORCE_ON) ||
289 (impl->warp_override != WARP_DISABLE && (impl->base.dwCoopLevel & DISCL_EXCLUSIVE)))
290 impl->need_warp = TRUE;
292 break;
294 case WM_MOUSEWHEEL:
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 */
299 ret = impl->clipped;
300 break;
301 case WM_LBUTTONDOWN:
302 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
303 state->rgbButtons[0] = wdata = 0x80;
304 break;
305 case WM_LBUTTONUP:
306 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
307 state->rgbButtons[0] = wdata = 0x00;
308 break;
309 case WM_RBUTTONDOWN:
310 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
311 state->rgbButtons[1] = wdata = 0x80;
312 break;
313 case WM_RBUTTONUP:
314 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
315 state->rgbButtons[1] = wdata = 0x00;
316 break;
317 case WM_MBUTTONDOWN:
318 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
319 state->rgbButtons[2] = wdata = 0x80;
320 break;
321 case WM_MBUTTONUP:
322 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
323 state->rgbButtons[2] = wdata = 0x00;
324 break;
325 case WM_XBUTTONDOWN:
326 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
327 state->rgbButtons[2 + HIWORD( hook->mouseData )] = wdata = 0x80;
328 break;
329 case WM_XBUTTONUP:
330 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
331 state->rgbButtons[2 + HIWORD( hook->mouseData )] = wdata = 0x00;
332 break;
336 if (inst_id != -1)
338 queue_event( iface, inst_id, wdata, GetCurrentTime(), impl->base.dinput->evsequence++ );
339 notify = TRUE;
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 );
348 return ret;
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)))
358 RECT rect, new_rect;
359 POINT mapped_center;
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 );
365 if (!impl->clipped)
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 ));
380 ClipCursor( &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 );
391 return DI_OK;
394 static HRESULT mouse_acquire( IDirectInputDevice8W *iface )
396 struct mouse *impl = impl_from_IDirectInputDevice8W( iface );
397 DIMOUSESTATE2 *state = (DIMOUSESTATE2 *)impl->base.device_state;
398 POINT point;
400 /* Init the mouse state */
401 GetCursorPos( &point );
402 if (impl->base.user_format->dwFlags & DIDF_ABSAXIS)
404 state->lX = point.x;
405 state->lY = point.y;
407 else
409 state->lX = 0;
410 state->lY = 0;
411 impl->org_coords = point;
413 state->lZ = 0;
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)
431 ClipCursor( NULL );
432 impl->clipped = FALSE;
435 return DI_OK;
438 static HRESULT mouse_unacquire( IDirectInputDevice8W *iface )
440 struct mouse *impl = impl_from_IDirectInputDevice8W( iface );
442 if (impl->base.dwCoopLevel & DISCL_EXCLUSIVE)
444 ClipCursor( NULL );
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 );
456 return DI_OK;
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)
466 case DIPH_DEVICE:
467 return callback( instance, data );
468 case DIPH_BYOFFSET:
469 if (filter->dwObj != instance->dwOfs) return DIENUM_CONTINUE;
470 return callback( instance, data );
471 case DIPH_BYID:
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,
487 .dwOfs = DIMOFS_X,
488 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(0),
489 .dwFlags = DIDOI_ASPECTPOSITION,
490 .tszName = L"X-axis",
493 .dwSize = sizeof(DIDEVICEOBJECTINSTANCEW),
494 .guidType = GUID_YAxis,
495 .dwOfs = DIMOFS_Y,
496 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(1),
497 .dwFlags = DIDOI_ASPECTPOSITION,
498 .tszName = L"Y-axis",
501 .dwSize = sizeof(DIDEVICEOBJECTINSTANCEW),
502 .guidType = GUID_ZAxis,
503 .dwOfs = DIMOFS_Z,
504 .dwType = DIDFT_RELAXIS|DIDFT_MAKEINSTANCE(2),
505 .dwFlags = DIDOI_ASPECTPOSITION,
506 .tszName = L"Wheel",
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",
544 BOOL ret;
545 DWORD i;
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 =
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,