wined3d: Correctly destroy the adapter on format initialization failure in no3d mode.
[wine/zf.git] / dlls / dinput / mouse.c
blob5e6f34f0ecac3aca5fc0b99ff930f8157dd2ccdb
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 "winternl.h"
32 #include "winuser.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "dinput.h"
37 #include "dinput_private.h"
38 #include "device_private.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
44 /* Wine mouse driver object instances */
45 #define WINE_MOUSE_X_AXIS_INSTANCE 0
46 #define WINE_MOUSE_Y_AXIS_INSTANCE 1
47 #define WINE_MOUSE_Z_AXIS_INSTANCE 2
48 #define WINE_MOUSE_BUTTONS_INSTANCE 3
50 static const IDirectInputDevice8AVtbl SysMouseAvt;
51 static const IDirectInputDevice8WVtbl SysMouseWvt;
53 typedef struct SysMouseImpl SysMouseImpl;
55 typedef enum
57 WARP_DEFAULT,
58 WARP_DISABLE,
59 WARP_FORCE_ON
60 } WARP_MOUSE;
62 struct SysMouseImpl
64 struct IDirectInputDeviceImpl base;
66 /* SysMouseAImpl */
67 /* These are used in case of relative -> absolute transitions */
68 POINT org_coords;
69 BOOL clipped;
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);
90 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(SysMouseImpl *This)
92 return &This->base.IDirectInputDevice8W_iface;
95 static void _dump_mouse_state(const DIMOUSESTATE2 *m_state)
97 int i;
99 if (!TRACE_ON(dinput)) return;
101 TRACE("(X: %d Y: %d Z: %d", m_state->lX, m_state->lY, m_state->lZ);
102 for (i = 0; i < 5; i++) TRACE(" B%d: %02x", i, m_state->rgbButtons[i]);
103 TRACE(")\n");
106 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
107 DWORD dwSize;
108 DIDEVICEINSTANCEA ddi;
110 dwSize = lpddi->dwSize;
112 TRACE("%d %p\n", dwSize, lpddi);
114 memset(lpddi, 0, dwSize);
115 memset(&ddi, 0, sizeof(ddi));
117 ddi.dwSize = dwSize;
118 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
119 ddi.guidProduct = GUID_SysMouse;
120 if (version >= 0x0800)
121 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
122 else
123 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
124 strcpy(ddi.tszInstanceName, "Mouse");
125 strcpy(ddi.tszProductName, "Wine Mouse");
127 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
130 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
131 DWORD dwSize;
132 DIDEVICEINSTANCEW ddi;
134 dwSize = lpddi->dwSize;
136 TRACE("%d %p\n", dwSize, lpddi);
138 memset(lpddi, 0, dwSize);
139 memset(&ddi, 0, sizeof(ddi));
141 ddi.dwSize = dwSize;
142 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
143 ddi.guidProduct = GUID_SysMouse;
144 if (version >= 0x0800)
145 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
146 else
147 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
148 MultiByteToWideChar(CP_ACP, 0, "Mouse", -1, ddi.tszInstanceName, MAX_PATH);
149 MultiByteToWideChar(CP_ACP, 0, "Wine Mouse", -1, ddi.tszProductName, MAX_PATH);
151 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
154 static HRESULT mousedev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
156 if (id != 0)
157 return E_FAIL;
159 if (dwFlags & DIEDFL_FORCEFEEDBACK)
160 return S_FALSE;
162 if ((dwDevType == 0) ||
163 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
164 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
165 TRACE("Enumerating the mouse device\n");
167 fill_mouse_dideviceinstanceA(lpddi, version);
169 return S_OK;
172 return S_FALSE;
175 static HRESULT mousedev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
177 if (id != 0)
178 return E_FAIL;
180 if (dwFlags & DIEDFL_FORCEFEEDBACK)
181 return S_FALSE;
183 if ((dwDevType == 0) ||
184 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
185 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
186 TRACE("Enumerating the mouse device\n");
188 fill_mouse_dideviceinstanceW(lpddi, version);
190 return S_OK;
193 return S_FALSE;
196 static SysMouseImpl *alloc_device(REFGUID rguid, IDirectInputImpl *dinput)
198 SysMouseImpl* newDevice;
199 LPDIDATAFORMAT df = NULL;
200 unsigned i;
201 char buffer[20];
202 HKEY hkey, appkey;
204 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseImpl));
205 if (!newDevice) return NULL;
206 newDevice->base.IDirectInputDevice8A_iface.lpVtbl = &SysMouseAvt;
207 newDevice->base.IDirectInputDevice8W_iface.lpVtbl = &SysMouseWvt;
208 newDevice->base.ref = 1;
209 newDevice->base.dwCoopLevel = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;
210 newDevice->base.guid = *rguid;
211 InitializeCriticalSection(&newDevice->base.crit);
212 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysMouseImpl*->base.crit");
213 newDevice->base.dinput = dinput;
215 get_app_key(&hkey, &appkey);
216 if (!get_config_key(hkey, appkey, "MouseWarpOverride", buffer, sizeof(buffer)))
218 if (!_strnicmp(buffer, "disable", -1))
219 newDevice->warp_override = WARP_DISABLE;
220 else if (!_strnicmp(buffer, "force", -1))
221 newDevice->warp_override = WARP_FORCE_ON;
223 if (appkey) RegCloseKey(appkey);
224 if (hkey) RegCloseKey(hkey);
226 /* Create copy of default data format */
227 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIMouse2.dwSize))) goto failed;
228 memcpy(df, &c_dfDIMouse2, c_dfDIMouse2.dwSize);
229 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
230 memcpy(df->rgodf, c_dfDIMouse2.rgodf, df->dwNumObjs * df->dwObjSize);
232 /* Because we don't do any detection yet just modify instance and type */
233 for (i = 0; i < df->dwNumObjs; i++)
234 if (DIDFT_GETTYPE(df->rgodf[i].dwType) & DIDFT_AXIS)
235 df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_RELAXIS;
236 else
237 df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
239 newDevice->base.data_format.wine_df = df;
240 IDirectInput_AddRef(&newDevice->base.dinput->IDirectInput7A_iface);
242 return newDevice;
244 failed:
245 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
246 HeapFree(GetProcessHeap(), 0, df);
247 HeapFree(GetProcessHeap(), 0, newDevice);
248 return NULL;
251 static HRESULT mousedev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
253 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
254 *pdev = NULL;
256 if (IsEqualGUID(&GUID_SysMouse, rguid)) /* Wine Mouse */
258 SysMouseImpl *This;
260 if (riid == NULL)
261 ;/* nothing */
262 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
263 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
264 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
265 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
267 unicode = 0;
269 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
270 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
271 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
272 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
274 unicode = 1;
276 else
278 WARN("no interface\n");
279 return DIERR_NOINTERFACE;
282 This = alloc_device(rguid, dinput);
283 TRACE("Created a Mouse device (%p)\n", This);
285 if (!This) return DIERR_OUTOFMEMORY;
287 if (unicode)
288 *pdev = &This->base.IDirectInputDevice8W_iface;
289 else
290 *pdev = &This->base.IDirectInputDevice8A_iface;
292 return DI_OK;
295 return DIERR_DEVICENOTREG;
298 const struct dinput_device mouse_device = {
299 "Wine mouse driver",
300 mousedev_enum_deviceA,
301 mousedev_enum_deviceW,
302 mousedev_create_device
305 /******************************************************************************
306 * SysMouseA (DInput Mouse support)
309 /* low-level mouse hook */
310 int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam )
312 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
313 SysMouseImpl* This = impl_from_IDirectInputDevice8A(iface);
314 int wdata = 0, inst_id = -1, ret = 0;
316 TRACE("msg %lx @ (%d %d)\n", wparam, hook->pt.x, hook->pt.y);
318 EnterCriticalSection(&This->base.crit);
320 switch(wparam) {
321 case WM_MOUSEMOVE:
323 POINT pt, pt1;
325 GetCursorPos(&pt);
326 This->m_state.lX += pt.x = hook->pt.x - pt.x;
327 This->m_state.lY += pt.y = hook->pt.y - pt.y;
329 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
331 pt1.x = This->m_state.lX;
332 pt1.y = This->m_state.lY;
333 } else
334 pt1 = pt;
336 if (pt.x)
338 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
339 wdata = pt1.x;
341 if (pt.y)
343 /* Already have X, need to queue it */
344 if (inst_id != -1)
345 queue_event(iface, inst_id,
346 wdata, GetCurrentTime(), This->base.dinput->evsequence);
347 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
348 wdata = pt1.y;
351 if (pt.x || pt.y)
353 if ((This->warp_override == WARP_FORCE_ON) ||
354 (This->warp_override != WARP_DISABLE && (This->base.dwCoopLevel & DISCL_EXCLUSIVE)))
355 This->need_warp = TRUE;
357 break;
359 case WM_MOUSEWHEEL:
360 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
361 This->m_state.lZ += wdata = (short)HIWORD(hook->mouseData);
362 /* FarCry crashes if it gets a mouse wheel message */
363 /* FIXME: should probably filter out other messages too */
364 ret = This->clipped;
365 break;
366 case WM_LBUTTONDOWN:
367 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
368 This->m_state.rgbButtons[0] = wdata = 0x80;
369 break;
370 case WM_LBUTTONUP:
371 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
372 This->m_state.rgbButtons[0] = wdata = 0x00;
373 break;
374 case WM_RBUTTONDOWN:
375 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
376 This->m_state.rgbButtons[1] = wdata = 0x80;
377 break;
378 case WM_RBUTTONUP:
379 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
380 This->m_state.rgbButtons[1] = wdata = 0x00;
381 break;
382 case WM_MBUTTONDOWN:
383 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
384 This->m_state.rgbButtons[2] = wdata = 0x80;
385 break;
386 case WM_MBUTTONUP:
387 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
388 This->m_state.rgbButtons[2] = wdata = 0x00;
389 break;
390 case WM_XBUTTONDOWN:
391 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
392 This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x80;
393 break;
394 case WM_XBUTTONUP:
395 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
396 This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x00;
397 break;
401 if (inst_id != -1)
403 _dump_mouse_state(&This->m_state);
404 queue_event(iface, inst_id,
405 wdata, GetCurrentTime(), This->base.dinput->evsequence++);
408 LeaveCriticalSection(&This->base.crit);
409 return ret;
412 static void warp_check( SysMouseImpl* This, BOOL force )
414 DWORD now = GetCurrentTime();
415 const DWORD interval = This->clipped ? 500 : 10;
417 if (force || (This->need_warp && (now - This->last_warped > interval)))
419 RECT rect, new_rect;
420 POINT mapped_center;
422 This->last_warped = now;
423 This->need_warp = FALSE;
424 if (!GetClientRect(This->base.win, &rect)) return;
425 MapWindowPoints( This->base.win, 0, (POINT *)&rect, 2 );
426 if (!This->clipped)
428 mapped_center.x = (rect.left + rect.right) / 2;
429 mapped_center.y = (rect.top + rect.bottom) / 2;
430 TRACE("Warping mouse to %d - %d\n", mapped_center.x, mapped_center.y);
431 SetCursorPos( mapped_center.x, mapped_center.y );
433 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
435 /* make sure we clip even if the window covers the whole screen */
436 rect.left = max( rect.left, GetSystemMetrics( SM_XVIRTUALSCREEN ) + 1 );
437 rect.top = max( rect.top, GetSystemMetrics( SM_YVIRTUALSCREEN ) + 1 );
438 rect.right = min( rect.right, rect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN ) - 2 );
439 rect.bottom = min( rect.bottom, rect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN ) - 2 );
440 TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect ));
441 ClipCursor( &rect );
442 This->clipped = GetClipCursor( &new_rect ) && EqualRect( &rect, &new_rect );
448 /******************************************************************************
449 * Acquire : gets exclusive control of the mouse
451 static HRESULT WINAPI SysMouseWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
453 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
454 POINT point;
455 HRESULT res;
457 TRACE("(this=%p)\n",This);
459 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK) return res;
461 /* Init the mouse state */
462 GetCursorPos( &point );
463 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
465 This->m_state.lX = point.x;
466 This->m_state.lY = point.y;
467 } else {
468 This->m_state.lX = 0;
469 This->m_state.lY = 0;
470 This->org_coords = point;
472 This->m_state.lZ = 0;
473 This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
474 This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
475 This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
477 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
479 ShowCursor(FALSE); /* hide cursor */
480 warp_check( This, TRUE );
482 else if (This->warp_override == WARP_FORCE_ON)
484 /* Need a window to warp mouse in. */
485 if (!This->base.win) This->base.win = GetDesktopWindow();
486 warp_check( This, TRUE );
488 else if (This->clipped)
490 ClipCursor( NULL );
491 This->clipped = FALSE;
494 return DI_OK;
497 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
499 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
500 return SysMouseWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
503 /******************************************************************************
504 * Unacquire : frees the mouse
506 static HRESULT WINAPI SysMouseWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
508 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
509 HRESULT res;
511 TRACE("(this=%p)\n",This);
513 if ((res = IDirectInputDevice2WImpl_Unacquire(iface)) != DI_OK) return res;
515 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
517 ClipCursor(NULL);
518 ShowCursor(TRUE); /* show cursor */
519 This->clipped = FALSE;
522 /* And put the mouse cursor back where it was at acquire time */
523 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
525 TRACE("warping mouse back to %s\n", wine_dbgstr_point(&This->org_coords));
526 SetCursorPos(This->org_coords.x, This->org_coords.y);
529 return DI_OK;
532 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
534 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
535 return SysMouseWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
538 /******************************************************************************
539 * GetDeviceState : returns the "state" of the mouse.
541 * For the moment, only the "standard" return structure (DIMOUSESTATE) is
542 * supported.
544 static HRESULT WINAPI SysMouseWImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
546 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
547 TRACE("(%p)->(%u,%p)\n", This, len, ptr);
549 if(This->base.acquired == 0) return DIERR_NOTACQUIRED;
551 check_dinput_events();
553 EnterCriticalSection(&This->base.crit);
554 _dump_mouse_state(&This->m_state);
556 /* Copy the current mouse state */
557 fill_DataFormat(ptr, len, &This->m_state, &This->base.data_format);
559 /* Initialize the buffer when in relative mode */
560 if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
562 This->m_state.lX = 0;
563 This->m_state.lY = 0;
564 This->m_state.lZ = 0;
566 LeaveCriticalSection(&This->base.crit);
568 warp_check( This, FALSE );
569 return DI_OK;
572 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
574 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
575 return SysMouseWImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
578 /******************************************************************************
579 * GetDeviceData : gets buffered input data.
581 static HRESULT WINAPI SysMouseWImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface,
582 DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
584 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
585 HRESULT res;
587 res = IDirectInputDevice2WImpl_GetDeviceData(iface, dodsize, dod, entries, flags);
588 if (SUCCEEDED(res)) warp_check( This, FALSE );
589 return res;
592 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
593 DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
595 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
596 return SysMouseWImpl_GetDeviceData(IDirectInputDevice8W_from_impl(This), dodsize, dod, entries, flags);
599 /******************************************************************************
600 * GetProperty : get input device properties
602 static HRESULT WINAPI SysMouseWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
604 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
606 TRACE("(%p) %s,%p\n", This, debugstr_guid(rguid), pdiph);
607 _dump_DIPROPHEADER(pdiph);
609 if (IS_DIPROP(rguid)) {
610 switch (LOWORD(rguid)) {
611 case (DWORD_PTR) DIPROP_GRANULARITY: {
612 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
614 if (
615 ((pdiph->dwHow == DIPH_BYOFFSET) &&
616 ((pdiph->dwObj == DIMOFS_X) ||
617 (pdiph->dwObj == DIMOFS_Y)))
619 ((pdiph->dwHow == DIPH_BYID) &&
620 ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
621 (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS))))
623 /* Set granularity of X/Y Axis to 1. See MSDN on DIPROP_GRANULARITY */
624 pr->dwData = 1;
625 } else {
626 /* We'll just assume that the app asks about the Z axis */
627 pr->dwData = WHEEL_DELTA;
630 break;
633 case (DWORD_PTR) DIPROP_RANGE: {
634 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
636 if ((pdiph->dwHow == DIPH_BYID) &&
637 ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
638 (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
639 /* Querying the range of either the X or the Y axis. As I do
640 not know the range, do as if the range were
641 unrestricted...*/
642 pr->lMin = DIPROPRANGE_NOMIN;
643 pr->lMax = DIPROPRANGE_NOMAX;
646 break;
648 case (DWORD_PTR) DIPROP_VIDPID:
649 return DIERR_UNSUPPORTED;
650 default:
651 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
655 return DI_OK;
658 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
660 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
661 return SysMouseWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
664 /******************************************************************************
665 * GetCapabilities : get the device capabilities
667 static HRESULT WINAPI SysMouseWImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
669 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
670 DIDEVCAPS devcaps;
672 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
674 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
675 WARN("invalid parameter\n");
676 return DIERR_INVALIDPARAM;
679 devcaps.dwSize = lpDIDevCaps->dwSize;
680 devcaps.dwFlags = DIDC_ATTACHED | DIDC_EMULATED;
681 if (This->base.dinput->dwVersion >= 0x0800)
682 devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
683 else
684 devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
685 devcaps.dwAxes = 3;
686 devcaps.dwButtons = 8;
687 devcaps.dwPOVs = 0;
688 devcaps.dwFFSamplePeriod = 0;
689 devcaps.dwFFMinTimeResolution = 0;
690 devcaps.dwFirmwareRevision = 100;
691 devcaps.dwHardwareRevision = 100;
692 devcaps.dwFFDriverVersion = 0;
694 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
696 return DI_OK;
699 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
701 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
702 return SysMouseWImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
705 /******************************************************************************
706 * GetObjectInfo : get information about a device object such as a button
707 * or axis
709 static HRESULT WINAPI SysMouseWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
710 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
712 static const WCHAR x_axisW[] = {'X','-','A','x','i','s',0};
713 static const WCHAR y_axisW[] = {'Y','-','A','x','i','s',0};
714 static const WCHAR wheelW[] = {'W','h','e','e','l',0};
715 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
716 HRESULT res;
718 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
719 if (res != DI_OK) return res;
721 if (IsEqualGUID(&pdidoi->guidType, &GUID_XAxis)) strcpyW(pdidoi->tszName, x_axisW);
722 else if (IsEqualGUID(&pdidoi->guidType, &GUID_YAxis)) strcpyW(pdidoi->tszName, y_axisW);
723 else if (IsEqualGUID(&pdidoi->guidType, &GUID_ZAxis)) strcpyW(pdidoi->tszName, wheelW);
724 else if (pdidoi->dwType & DIDFT_BUTTON)
725 wsprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType) - 3);
727 if(pdidoi->dwType & DIDFT_AXIS)
728 pdidoi->dwFlags |= DIDOI_ASPECTPOSITION;
730 _dump_OBJECTINSTANCEW(pdidoi);
731 return res;
734 static HRESULT WINAPI SysMouseAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
735 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
737 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
738 HRESULT res;
739 DIDEVICEOBJECTINSTANCEW didoiW;
740 DWORD dwSize = pdidoi->dwSize;
742 didoiW.dwSize = sizeof(didoiW);
743 res = SysMouseWImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
744 if (res != DI_OK) return res;
746 memset(pdidoi, 0, pdidoi->dwSize);
747 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
748 pdidoi->dwSize = dwSize;
749 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
750 sizeof(pdidoi->tszName), NULL, NULL);
752 return res;
755 /******************************************************************************
756 * GetDeviceInfo : get information about a device's identity
758 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
759 LPDIRECTINPUTDEVICE8A iface,
760 LPDIDEVICEINSTANCEA pdidi)
762 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
763 TRACE("(this=%p,%p)\n", This, pdidi);
765 fill_mouse_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
767 return DI_OK;
770 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
772 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
773 TRACE("(this=%p,%p)\n", This, pdidi);
775 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
776 WARN(" dinput3 not supported yet...\n");
777 return DI_OK;
780 fill_mouse_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
782 return DI_OK;
785 static HRESULT WINAPI SysMouseWImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
786 LPDIACTIONFORMATW lpdiaf,
787 LPCWSTR lpszUserName,
788 DWORD dwFlags)
790 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
792 return _build_action_map(iface, lpdiaf, lpszUserName, dwFlags, DIMOUSE_MASK, &c_dfDIMouse2);
795 static HRESULT WINAPI SysMouseAImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
796 LPDIACTIONFORMATA lpdiaf,
797 LPCSTR lpszUserName,
798 DWORD dwFlags)
800 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
801 DIACTIONFORMATW diafW;
802 HRESULT hr;
803 WCHAR *lpszUserNameW = NULL;
804 int username_size;
806 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
807 _copy_diactionformatAtoW(&diafW, lpdiaf);
809 if (lpszUserName != NULL)
811 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
812 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
813 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
816 hr = SysMouseWImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
818 _copy_diactionformatWtoA(lpdiaf, &diafW);
819 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
820 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
822 return hr;
825 static HRESULT WINAPI SysMouseWImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
826 LPDIACTIONFORMATW lpdiaf,
827 LPCWSTR lpszUserName,
828 DWORD dwFlags)
830 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
831 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This, lpdiaf, debugstr_w(lpszUserName), dwFlags);
833 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, &c_dfDIMouse2);
836 static HRESULT WINAPI SysMouseAImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
837 LPDIACTIONFORMATA lpdiaf,
838 LPCSTR lpszUserName,
839 DWORD dwFlags)
841 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
842 DIACTIONFORMATW diafW;
843 HRESULT hr;
844 WCHAR *lpszUserNameW = NULL;
845 int username_size;
847 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
848 _copy_diactionformatAtoW(&diafW, lpdiaf);
850 if (lpszUserName != NULL)
852 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
853 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
854 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
857 hr = SysMouseWImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
859 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
860 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
862 return hr;
865 static const IDirectInputDevice8AVtbl SysMouseAvt =
867 IDirectInputDevice2AImpl_QueryInterface,
868 IDirectInputDevice2AImpl_AddRef,
869 IDirectInputDevice2AImpl_Release,
870 SysMouseAImpl_GetCapabilities,
871 IDirectInputDevice2AImpl_EnumObjects,
872 SysMouseAImpl_GetProperty,
873 IDirectInputDevice2AImpl_SetProperty,
874 SysMouseAImpl_Acquire,
875 SysMouseAImpl_Unacquire,
876 SysMouseAImpl_GetDeviceState,
877 SysMouseAImpl_GetDeviceData,
878 IDirectInputDevice2AImpl_SetDataFormat,
879 IDirectInputDevice2AImpl_SetEventNotification,
880 IDirectInputDevice2AImpl_SetCooperativeLevel,
881 SysMouseAImpl_GetObjectInfo,
882 SysMouseAImpl_GetDeviceInfo,
883 IDirectInputDevice2AImpl_RunControlPanel,
884 IDirectInputDevice2AImpl_Initialize,
885 IDirectInputDevice2AImpl_CreateEffect,
886 IDirectInputDevice2AImpl_EnumEffects,
887 IDirectInputDevice2AImpl_GetEffectInfo,
888 IDirectInputDevice2AImpl_GetForceFeedbackState,
889 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
890 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
891 IDirectInputDevice2AImpl_Escape,
892 IDirectInputDevice2AImpl_Poll,
893 IDirectInputDevice2AImpl_SendDeviceData,
894 IDirectInputDevice7AImpl_EnumEffectsInFile,
895 IDirectInputDevice7AImpl_WriteEffectToFile,
896 SysMouseAImpl_BuildActionMap,
897 SysMouseAImpl_SetActionMap,
898 IDirectInputDevice8AImpl_GetImageInfo
901 static const IDirectInputDevice8WVtbl SysMouseWvt =
903 IDirectInputDevice2WImpl_QueryInterface,
904 IDirectInputDevice2WImpl_AddRef,
905 IDirectInputDevice2WImpl_Release,
906 SysMouseWImpl_GetCapabilities,
907 IDirectInputDevice2WImpl_EnumObjects,
908 SysMouseWImpl_GetProperty,
909 IDirectInputDevice2WImpl_SetProperty,
910 SysMouseWImpl_Acquire,
911 SysMouseWImpl_Unacquire,
912 SysMouseWImpl_GetDeviceState,
913 SysMouseWImpl_GetDeviceData,
914 IDirectInputDevice2WImpl_SetDataFormat,
915 IDirectInputDevice2WImpl_SetEventNotification,
916 IDirectInputDevice2WImpl_SetCooperativeLevel,
917 SysMouseWImpl_GetObjectInfo,
918 SysMouseWImpl_GetDeviceInfo,
919 IDirectInputDevice2WImpl_RunControlPanel,
920 IDirectInputDevice2WImpl_Initialize,
921 IDirectInputDevice2WImpl_CreateEffect,
922 IDirectInputDevice2WImpl_EnumEffects,
923 IDirectInputDevice2WImpl_GetEffectInfo,
924 IDirectInputDevice2WImpl_GetForceFeedbackState,
925 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
926 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
927 IDirectInputDevice2WImpl_Escape,
928 IDirectInputDevice2WImpl_Poll,
929 IDirectInputDevice2WImpl_SendDeviceData,
930 IDirectInputDevice7WImpl_EnumEffectsInFile,
931 IDirectInputDevice7WImpl_WriteEffectToFile,
932 SysMouseWImpl_BuildActionMap,
933 SysMouseWImpl_SetActionMap,
934 IDirectInputDevice8WImpl_GetImageInfo