user32: Convert menu and string table resources to po files.
[wine.git] / dlls / dinput / mouse.c
blob0bdec82b6b246f7f5571554b52e51caedc7d1ba9
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 "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winerror.h"
33 #include "winreg.h"
34 #include "dinput.h"
36 #include "dinput_private.h"
37 #include "device_private.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
43 /* Wine mouse driver object instances */
44 #define WINE_MOUSE_X_AXIS_INSTANCE 0
45 #define WINE_MOUSE_Y_AXIS_INSTANCE 1
46 #define WINE_MOUSE_Z_AXIS_INSTANCE 2
47 #define WINE_MOUSE_BUTTONS_INSTANCE 3
49 static const IDirectInputDevice8AVtbl SysMouseAvt;
50 static const IDirectInputDevice8WVtbl SysMouseWvt;
52 typedef struct SysMouseImpl SysMouseImpl;
54 typedef enum
56 WARP_DEFAULT,
57 WARP_DISABLE,
58 WARP_FORCE_ON
59 } WARP_MOUSE;
61 struct SysMouseImpl
63 struct IDirectInputDeviceImpl base;
65 /* SysMouseAImpl */
66 /* These are used in case of relative -> absolute transitions */
67 POINT org_coords;
68 POINT mapped_center;
69 DWORD win_centerX, win_centerY;
70 /* warping: whether we need to move mouse back to middle once we
71 * reach window borders (for e.g. shooters, "surface movement" games) */
72 BOOL need_warp;
73 DWORD last_warped;
75 /* This is for mouse reporting. */
76 DIMOUSESTATE2 m_state;
78 WARP_MOUSE warp_override;
81 static inline SysMouseImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
83 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), SysMouseImpl, base);
85 static inline SysMouseImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
87 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), SysMouseImpl, base);
89 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(SysMouseImpl *This)
91 return &This->base.IDirectInputDevice8A_iface;
93 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(SysMouseImpl *This)
95 return &This->base.IDirectInputDevice8W_iface;
98 static int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam );
100 const GUID DInput_Wine_Mouse_GUID = { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
101 0x9e573ed8, 0x7734, 0x11d2, {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
104 static void _dump_mouse_state(const DIMOUSESTATE2 *m_state)
106 int i;
108 if (!TRACE_ON(dinput)) return;
110 TRACE("(X: %d Y: %d Z: %d", m_state->lX, m_state->lY, m_state->lZ);
111 for (i = 0; i < 5; i++) TRACE(" B%d: %02x", i, m_state->rgbButtons[i]);
112 TRACE(")\n");
115 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
116 DWORD dwSize;
117 DIDEVICEINSTANCEA ddi;
119 dwSize = lpddi->dwSize;
121 TRACE("%d %p\n", dwSize, lpddi);
123 memset(lpddi, 0, dwSize);
124 memset(&ddi, 0, sizeof(ddi));
126 ddi.dwSize = dwSize;
127 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
128 ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
129 if (version >= 0x0800)
130 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
131 else
132 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
133 strcpy(ddi.tszInstanceName, "Mouse");
134 strcpy(ddi.tszProductName, "Wine Mouse");
136 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
139 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
140 DWORD dwSize;
141 DIDEVICEINSTANCEW ddi;
143 dwSize = lpddi->dwSize;
145 TRACE("%d %p\n", dwSize, lpddi);
147 memset(lpddi, 0, dwSize);
148 memset(&ddi, 0, sizeof(ddi));
150 ddi.dwSize = dwSize;
151 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
152 ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
153 if (version >= 0x0800)
154 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
155 else
156 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
157 MultiByteToWideChar(CP_ACP, 0, "Mouse", -1, ddi.tszInstanceName, MAX_PATH);
158 MultiByteToWideChar(CP_ACP, 0, "Wine Mouse", -1, ddi.tszProductName, MAX_PATH);
160 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
163 static BOOL mousedev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
165 if (id != 0)
166 return FALSE;
168 if ((dwDevType == 0) ||
169 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
170 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
171 TRACE("Enumerating the mouse device\n");
173 fill_mouse_dideviceinstanceA(lpddi, version);
175 return TRUE;
178 return FALSE;
181 static BOOL mousedev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
183 if (id != 0)
184 return FALSE;
186 if ((dwDevType == 0) ||
187 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
188 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
189 TRACE("Enumerating the mouse device\n");
191 fill_mouse_dideviceinstanceW(lpddi, version);
193 return TRUE;
196 return FALSE;
199 static SysMouseImpl *alloc_device(REFGUID rguid, IDirectInputImpl *dinput)
201 SysMouseImpl* newDevice;
202 LPDIDATAFORMAT df = NULL;
203 unsigned i;
204 char buffer[20];
205 HKEY hkey, appkey;
207 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseImpl));
208 if (!newDevice) return NULL;
209 newDevice->base.IDirectInputDevice8A_iface.lpVtbl = &SysMouseAvt;
210 newDevice->base.IDirectInputDevice8W_iface.lpVtbl = &SysMouseWvt;
211 newDevice->base.ref = 1;
212 newDevice->base.dwCoopLevel = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;
213 newDevice->base.guid = *rguid;
214 InitializeCriticalSection(&newDevice->base.crit);
215 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysMouseImpl*->base.crit");
216 newDevice->base.dinput = dinput;
217 newDevice->base.event_proc = dinput_mouse_hook;
219 get_app_key(&hkey, &appkey);
220 if (!get_config_key(hkey, appkey, "MouseWarpOverride", buffer, sizeof(buffer)))
222 if (!strcasecmp(buffer, "disable"))
223 newDevice->warp_override = WARP_DISABLE;
224 else if (!strcasecmp(buffer, "force"))
225 newDevice->warp_override = WARP_FORCE_ON;
227 if (appkey) RegCloseKey(appkey);
228 if (hkey) RegCloseKey(hkey);
230 /* Create copy of default data format */
231 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIMouse2.dwSize))) goto failed;
232 memcpy(df, &c_dfDIMouse2, c_dfDIMouse2.dwSize);
233 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
234 memcpy(df->rgodf, c_dfDIMouse2.rgodf, df->dwNumObjs * df->dwObjSize);
236 /* Because we don't do any detection yet just modify instance and type */
237 for (i = 0; i < df->dwNumObjs; i++)
238 if (DIDFT_GETTYPE(df->rgodf[i].dwType) & DIDFT_AXIS)
239 df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_RELAXIS;
240 else
241 df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
243 newDevice->base.data_format.wine_df = df;
244 IDirectInput_AddRef(&newDevice->base.dinput->IDirectInput7A_iface);
245 return newDevice;
247 failed:
248 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
249 HeapFree(GetProcessHeap(), 0, df);
250 HeapFree(GetProcessHeap(), 0, newDevice);
251 return NULL;
254 static HRESULT mousedev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
256 if ((IsEqualGUID(&GUID_SysMouse,rguid)) || /* Generic Mouse */
257 (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
258 if ((riid == NULL) ||
259 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
260 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
261 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
262 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
264 SysMouseImpl* This = alloc_device(rguid, dinput);
265 TRACE("Created a Mouse device (%p)\n", This);
267 if (!This) {
268 *pdev = NULL;
269 return DIERR_OUTOFMEMORY;
271 *pdev = (IDirectInputDeviceA*) &This->base.IDirectInputDevice8A_iface;
272 return DI_OK;
273 } else
274 return DIERR_NOINTERFACE;
277 return DIERR_DEVICENOTREG;
280 static HRESULT mousedev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
282 if ((IsEqualGUID(&GUID_SysMouse,rguid)) || /* Generic Mouse */
283 (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
284 if ((riid == NULL) ||
285 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
286 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
287 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
288 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
290 SysMouseImpl* This = alloc_device(rguid, dinput);
291 TRACE("Created a Mouse device (%p)\n", This);
293 if (!This) {
294 *pdev = NULL;
295 return DIERR_OUTOFMEMORY;
297 *pdev = (IDirectInputDeviceW*) &This->base.IDirectInputDevice8W_iface;
298 return DI_OK;
299 } else
300 return DIERR_NOINTERFACE;
303 return DIERR_DEVICENOTREG;
306 const struct dinput_device mouse_device = {
307 "Wine mouse driver",
308 mousedev_enum_deviceA,
309 mousedev_enum_deviceW,
310 mousedev_create_deviceA,
311 mousedev_create_deviceW
314 /******************************************************************************
315 * SysMouseA (DInput Mouse support)
318 /* low-level mouse hook */
319 static int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam )
321 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
322 SysMouseImpl* This = impl_from_IDirectInputDevice8A(iface);
323 DWORD dwCoop;
324 int wdata = 0, inst_id = -1, ret;
326 TRACE("msg %lx @ (%d %d)\n", wparam, hook->pt.x, hook->pt.y);
328 EnterCriticalSection(&This->base.crit);
329 dwCoop = This->base.dwCoopLevel;
330 ret = dwCoop & DISCL_EXCLUSIVE;
332 switch(wparam) {
333 case WM_MOUSEMOVE:
335 POINT pt, pt1;
337 GetCursorPos(&pt);
338 This->m_state.lX += pt.x = hook->pt.x - pt.x;
339 This->m_state.lY += pt.y = hook->pt.y - pt.y;
341 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
343 pt1.x = This->m_state.lX;
344 pt1.y = This->m_state.lY;
345 } else
346 pt1 = pt;
348 if (pt.x)
350 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
351 wdata = pt1.x;
353 if (pt.y)
355 /* Already have X, need to queue it */
356 if (inst_id != -1)
357 queue_event(iface, inst_id,
358 wdata, GetCurrentTime(), This->base.dinput->evsequence);
359 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
360 wdata = pt1.y;
363 This->need_warp = This->warp_override != WARP_DISABLE &&
364 (pt.x || pt.y) &&
365 (dwCoop & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON);
366 break;
368 case WM_MOUSEWHEEL:
369 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
370 This->m_state.lZ += wdata = (short)HIWORD(hook->mouseData);
371 break;
372 case WM_LBUTTONDOWN:
373 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
374 This->m_state.rgbButtons[0] = wdata = 0x80;
375 break;
376 case WM_LBUTTONUP:
377 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
378 This->m_state.rgbButtons[0] = wdata = 0x00;
379 break;
380 case WM_RBUTTONDOWN:
381 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
382 This->m_state.rgbButtons[1] = wdata = 0x80;
383 break;
384 case WM_RBUTTONUP:
385 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
386 This->m_state.rgbButtons[1] = wdata = 0x00;
387 break;
388 case WM_MBUTTONDOWN:
389 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
390 This->m_state.rgbButtons[2] = wdata = 0x80;
391 break;
392 case WM_MBUTTONUP:
393 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
394 This->m_state.rgbButtons[2] = wdata = 0x00;
395 break;
396 case WM_XBUTTONDOWN:
397 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
398 This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x80;
399 break;
400 case WM_XBUTTONUP:
401 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
402 This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x00;
403 break;
404 default:
405 ret = 0;
409 if (inst_id != -1)
411 _dump_mouse_state(&This->m_state);
412 queue_event(iface, inst_id,
413 wdata, GetCurrentTime(), This->base.dinput->evsequence++);
416 LeaveCriticalSection(&This->base.crit);
417 return ret;
420 static BOOL dinput_window_check(SysMouseImpl* This) {
421 RECT rect;
422 DWORD centerX, centerY;
424 /* make sure the window hasn't moved */
425 if(!GetWindowRect(This->base.win, &rect))
426 return FALSE;
427 centerX = (rect.right - rect.left) / 2;
428 centerY = (rect.bottom - rect.top ) / 2;
429 if (This->win_centerX != centerX || This->win_centerY != centerY) {
430 This->win_centerX = centerX;
431 This->win_centerY = centerY;
433 This->mapped_center.x = This->win_centerX;
434 This->mapped_center.y = This->win_centerY;
435 MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
436 return TRUE;
440 /******************************************************************************
441 * Acquire : gets exclusive control of the mouse
443 static HRESULT WINAPI SysMouseWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
445 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
446 RECT rect;
447 POINT point;
448 HRESULT res;
450 TRACE("(this=%p)\n",This);
452 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK) return res;
454 /* Init the mouse state */
455 GetCursorPos( &point );
456 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
458 This->m_state.lX = point.x;
459 This->m_state.lY = point.y;
460 } else {
461 This->m_state.lX = 0;
462 This->m_state.lY = 0;
463 This->org_coords = point;
465 This->m_state.lZ = 0;
466 This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
467 This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
468 This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
470 /* Install our mouse hook */
471 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
473 RECT rc;
475 ShowCursor(FALSE); /* hide cursor */
476 if (GetWindowRect(This->base.win, &rc))
478 FIXME("Clipping cursor to %s\n", wine_dbgstr_rect( &rc ));
479 ClipCursor(&rc);
481 else
482 ERR("Failed to get RECT: %d\n", GetLastError());
485 /* Need a window to warp mouse in. */
486 if (This->warp_override == WARP_FORCE_ON && !This->base.win)
487 This->base.win = GetDesktopWindow();
489 /* Get the window dimension and find the center */
490 GetWindowRect(This->base.win, &rect);
491 This->win_centerX = (rect.right - rect.left) / 2;
492 This->win_centerY = (rect.bottom - rect.top ) / 2;
494 /* Warp the mouse to the center of the window */
495 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
497 This->mapped_center.x = This->win_centerX;
498 This->mapped_center.y = This->win_centerY;
499 MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
500 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
501 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
502 This->last_warped = GetCurrentTime();
504 This->need_warp = FALSE;
507 return DI_OK;
510 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
512 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
513 return SysMouseWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
516 /******************************************************************************
517 * Unacquire : frees the mouse
519 static HRESULT WINAPI SysMouseWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
521 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
522 HRESULT res;
524 TRACE("(this=%p)\n",This);
526 if ((res = IDirectInputDevice2WImpl_Unacquire(iface)) != DI_OK) return res;
528 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
530 ClipCursor(NULL);
531 ShowCursor(TRUE); /* show cursor */
534 /* And put the mouse cursor back where it was at acquire time */
535 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
537 TRACE(" warping mouse back to (%d , %d)\n", This->org_coords.x, This->org_coords.y);
538 SetCursorPos(This->org_coords.x, This->org_coords.y);
541 return DI_OK;
544 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
546 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
547 return SysMouseWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
550 /******************************************************************************
551 * GetDeviceState : returns the "state" of the mouse.
553 * For the moment, only the "standard" return structure (DIMOUSESTATE) is
554 * supported.
556 static HRESULT WINAPI SysMouseWImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
558 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
560 if(This->base.acquired == 0) return DIERR_NOTACQUIRED;
562 TRACE("(this=%p,0x%08x,%p):\n", This, len, ptr);
563 _dump_mouse_state(&This->m_state);
565 EnterCriticalSection(&This->base.crit);
566 /* Copy the current mouse state */
567 fill_DataFormat(ptr, len, &This->m_state, &This->base.data_format);
569 /* Initialize the buffer when in relative mode */
570 if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
572 This->m_state.lX = 0;
573 This->m_state.lY = 0;
574 This->m_state.lZ = 0;
576 LeaveCriticalSection(&This->base.crit);
578 /* Check if we need to do a mouse warping */
579 if (This->need_warp && (GetCurrentTime() - This->last_warped > 10))
581 if(!dinput_window_check(This))
582 return DIERR_GENERIC;
583 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
584 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
585 This->last_warped = GetCurrentTime();
587 This->need_warp = FALSE;
590 return DI_OK;
593 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
595 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
596 return SysMouseWImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
599 /******************************************************************************
600 * GetDeviceData : gets buffered input data.
602 static HRESULT WINAPI SysMouseWImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface,
603 DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
605 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
606 HRESULT res;
608 res = IDirectInputDevice2WImpl_GetDeviceData(iface, dodsize, dod, entries, flags);
609 if (FAILED(res)) return res;
611 /* Check if we need to do a mouse warping */
612 if (This->need_warp && (GetCurrentTime() - This->last_warped > 10))
614 if(!dinput_window_check(This))
615 return DIERR_GENERIC;
616 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
617 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
618 This->last_warped = GetCurrentTime();
620 This->need_warp = FALSE;
622 return res;
625 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
626 DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
628 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
629 return SysMouseWImpl_GetDeviceData(IDirectInputDevice8W_from_impl(This), dodsize, dod, entries, flags);
632 /******************************************************************************
633 * GetProperty : get input device properties
635 static HRESULT WINAPI SysMouseWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
637 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
639 TRACE("(%p) %s,%p\n", This, debugstr_guid(rguid), pdiph);
640 _dump_DIPROPHEADER(pdiph);
642 if (IS_DIPROP(rguid)) {
643 switch (LOWORD(rguid)) {
644 case (DWORD_PTR) DIPROP_GRANULARITY: {
645 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
647 /* We'll just assume that the app asks about the Z axis */
648 pr->dwData = WHEEL_DELTA;
650 break;
653 case (DWORD_PTR) DIPROP_RANGE: {
654 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
656 if ((pdiph->dwHow == DIPH_BYID) &&
657 ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
658 (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
659 /* Querying the range of either the X or the Y axis. As I do
660 not know the range, do as if the range were
661 unrestricted...*/
662 pr->lMin = DIPROPRANGE_NOMIN;
663 pr->lMax = DIPROPRANGE_NOMAX;
666 break;
669 default:
670 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
674 return DI_OK;
677 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
679 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
680 return SysMouseWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
683 /******************************************************************************
684 * GetCapabilities : get the device capabilities
686 static HRESULT WINAPI SysMouseWImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
688 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
689 DIDEVCAPS devcaps;
691 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
693 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
694 WARN("invalid parameter\n");
695 return DIERR_INVALIDPARAM;
698 devcaps.dwSize = lpDIDevCaps->dwSize;
699 devcaps.dwFlags = DIDC_ATTACHED;
700 if (This->base.dinput->dwVersion >= 0x0800)
701 devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
702 else
703 devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
704 devcaps.dwAxes = 3;
705 devcaps.dwButtons = 8;
706 devcaps.dwPOVs = 0;
707 devcaps.dwFFSamplePeriod = 0;
708 devcaps.dwFFMinTimeResolution = 0;
709 devcaps.dwFirmwareRevision = 100;
710 devcaps.dwHardwareRevision = 100;
711 devcaps.dwFFDriverVersion = 0;
713 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
715 return DI_OK;
718 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
720 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
721 return SysMouseWImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
724 /******************************************************************************
725 * GetObjectInfo : get information about a device object such as a button
726 * or axis
728 static HRESULT WINAPI SysMouseWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
729 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
731 static const WCHAR x_axisW[] = {'X','-','A','x','i','s',0};
732 static const WCHAR y_axisW[] = {'Y','-','A','x','i','s',0};
733 static const WCHAR wheelW[] = {'W','h','e','e','l',0};
734 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
735 HRESULT res;
737 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
738 if (res != DI_OK) return res;
740 if (IsEqualGUID(&pdidoi->guidType, &GUID_XAxis)) strcpyW(pdidoi->tszName, x_axisW);
741 else if (IsEqualGUID(&pdidoi->guidType, &GUID_YAxis)) strcpyW(pdidoi->tszName, y_axisW);
742 else if (IsEqualGUID(&pdidoi->guidType, &GUID_ZAxis)) strcpyW(pdidoi->tszName, wheelW);
743 else if (pdidoi->dwType & DIDFT_BUTTON)
744 wsprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType) - 3);
746 _dump_OBJECTINSTANCEW(pdidoi);
747 return res;
750 static HRESULT WINAPI SysMouseAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
751 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
753 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
754 HRESULT res;
755 DIDEVICEOBJECTINSTANCEW didoiW;
756 DWORD dwSize = pdidoi->dwSize;
758 didoiW.dwSize = sizeof(didoiW);
759 res = SysMouseWImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
760 if (res != DI_OK) return res;
762 memset(pdidoi, 0, pdidoi->dwSize);
763 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
764 pdidoi->dwSize = dwSize;
765 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
766 sizeof(pdidoi->tszName), NULL, NULL);
768 return res;
771 /******************************************************************************
772 * GetDeviceInfo : get information about a device's identity
774 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
775 LPDIRECTINPUTDEVICE8A iface,
776 LPDIDEVICEINSTANCEA pdidi)
778 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
779 TRACE("(this=%p,%p)\n", This, pdidi);
781 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
782 WARN(" dinput3 not supporte yet...\n");
783 return DI_OK;
786 fill_mouse_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
788 return DI_OK;
791 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
793 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
794 TRACE("(this=%p,%p)\n", This, pdidi);
796 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
797 WARN(" dinput3 not supporte yet...\n");
798 return DI_OK;
801 fill_mouse_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
803 return DI_OK;
807 static const IDirectInputDevice8AVtbl SysMouseAvt =
809 IDirectInputDevice2AImpl_QueryInterface,
810 IDirectInputDevice2AImpl_AddRef,
811 IDirectInputDevice2AImpl_Release,
812 SysMouseAImpl_GetCapabilities,
813 IDirectInputDevice2AImpl_EnumObjects,
814 SysMouseAImpl_GetProperty,
815 IDirectInputDevice2AImpl_SetProperty,
816 SysMouseAImpl_Acquire,
817 SysMouseAImpl_Unacquire,
818 SysMouseAImpl_GetDeviceState,
819 SysMouseAImpl_GetDeviceData,
820 IDirectInputDevice2AImpl_SetDataFormat,
821 IDirectInputDevice2AImpl_SetEventNotification,
822 IDirectInputDevice2AImpl_SetCooperativeLevel,
823 SysMouseAImpl_GetObjectInfo,
824 SysMouseAImpl_GetDeviceInfo,
825 IDirectInputDevice2AImpl_RunControlPanel,
826 IDirectInputDevice2AImpl_Initialize,
827 IDirectInputDevice2AImpl_CreateEffect,
828 IDirectInputDevice2AImpl_EnumEffects,
829 IDirectInputDevice2AImpl_GetEffectInfo,
830 IDirectInputDevice2AImpl_GetForceFeedbackState,
831 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
832 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
833 IDirectInputDevice2AImpl_Escape,
834 IDirectInputDevice2AImpl_Poll,
835 IDirectInputDevice2AImpl_SendDeviceData,
836 IDirectInputDevice7AImpl_EnumEffectsInFile,
837 IDirectInputDevice7AImpl_WriteEffectToFile,
838 IDirectInputDevice8AImpl_BuildActionMap,
839 IDirectInputDevice8AImpl_SetActionMap,
840 IDirectInputDevice8AImpl_GetImageInfo
843 static const IDirectInputDevice8WVtbl SysMouseWvt =
845 IDirectInputDevice2WImpl_QueryInterface,
846 IDirectInputDevice2WImpl_AddRef,
847 IDirectInputDevice2WImpl_Release,
848 SysMouseWImpl_GetCapabilities,
849 IDirectInputDevice2WImpl_EnumObjects,
850 SysMouseWImpl_GetProperty,
851 IDirectInputDevice2WImpl_SetProperty,
852 SysMouseWImpl_Acquire,
853 SysMouseWImpl_Unacquire,
854 SysMouseWImpl_GetDeviceState,
855 SysMouseWImpl_GetDeviceData,
856 IDirectInputDevice2WImpl_SetDataFormat,
857 IDirectInputDevice2WImpl_SetEventNotification,
858 IDirectInputDevice2WImpl_SetCooperativeLevel,
859 SysMouseWImpl_GetObjectInfo,
860 SysMouseWImpl_GetDeviceInfo,
861 IDirectInputDevice2WImpl_RunControlPanel,
862 IDirectInputDevice2WImpl_Initialize,
863 IDirectInputDevice2WImpl_CreateEffect,
864 IDirectInputDevice2WImpl_EnumEffects,
865 IDirectInputDevice2WImpl_GetEffectInfo,
866 IDirectInputDevice2WImpl_GetForceFeedbackState,
867 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
868 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
869 IDirectInputDevice2WImpl_Escape,
870 IDirectInputDevice2WImpl_Poll,
871 IDirectInputDevice2WImpl_SendDeviceData,
872 IDirectInputDevice7WImpl_EnumEffectsInFile,
873 IDirectInputDevice7WImpl_WriteEffectToFile,
874 IDirectInputDevice8WImpl_BuildActionMap,
875 IDirectInputDevice8WImpl_SetActionMap,
876 IDirectInputDevice8WImpl_GetImageInfo