conhost: Update tty output in fill_output.
[wine.git] / dlls / dinput / mouse.c
blobe50731fda415f9dc956a09308a04111cc1a884dc
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 if (dinput->dwVersion >= 0x0800)
244 newDevice->base.use_raw_input = TRUE;
245 newDevice->base.raw_device.usUsagePage = 1; /* HID generic device page */
246 newDevice->base.raw_device.usUsage = 2; /* HID generic mouse */
249 return newDevice;
251 failed:
252 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
253 HeapFree(GetProcessHeap(), 0, df);
254 HeapFree(GetProcessHeap(), 0, newDevice);
255 return NULL;
258 static HRESULT mousedev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
260 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
261 *pdev = NULL;
263 if (IsEqualGUID(&GUID_SysMouse, rguid)) /* Wine Mouse */
265 SysMouseImpl *This;
267 if (riid == NULL)
268 ;/* nothing */
269 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
270 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
271 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
272 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
274 unicode = 0;
276 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
277 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
278 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
279 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
281 unicode = 1;
283 else
285 WARN("no interface\n");
286 return DIERR_NOINTERFACE;
289 This = alloc_device(rguid, dinput);
290 TRACE("Created a Mouse device (%p)\n", This);
292 if (!This) return DIERR_OUTOFMEMORY;
294 if (unicode)
295 *pdev = &This->base.IDirectInputDevice8W_iface;
296 else
297 *pdev = &This->base.IDirectInputDevice8A_iface;
299 return DI_OK;
302 return DIERR_DEVICENOTREG;
305 const struct dinput_device mouse_device = {
306 "Wine mouse driver",
307 mousedev_enum_deviceA,
308 mousedev_enum_deviceW,
309 mousedev_create_device
312 /******************************************************************************
313 * SysMouseA (DInput Mouse support)
316 void dinput_mouse_rawinput_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam, RAWINPUT *ri )
318 SysMouseImpl* This = impl_from_IDirectInputDevice8A( iface );
319 POINT rel, pt;
320 DWORD seq;
321 int i, wdata = 0;
323 static const USHORT mouse_button_flags[] =
325 RI_MOUSE_BUTTON_1_DOWN, RI_MOUSE_BUTTON_1_UP,
326 RI_MOUSE_BUTTON_2_DOWN, RI_MOUSE_BUTTON_2_UP,
327 RI_MOUSE_BUTTON_3_DOWN, RI_MOUSE_BUTTON_3_UP,
328 RI_MOUSE_BUTTON_4_DOWN, RI_MOUSE_BUTTON_4_UP,
329 RI_MOUSE_BUTTON_5_DOWN, RI_MOUSE_BUTTON_5_UP
332 TRACE( "(%p) wp %08lx, lp %08lx\n", iface, wparam, lparam );
334 if (ri->data.mouse.usFlags & MOUSE_VIRTUAL_DESKTOP)
335 FIXME( "Unimplemented MOUSE_VIRTUAL_DESKTOP flag\n" );
336 if (ri->data.mouse.usFlags & MOUSE_ATTRIBUTES_CHANGED)
337 FIXME( "Unimplemented MOUSE_ATTRIBUTES_CHANGED flag\n" );
339 EnterCriticalSection( &This->base.crit );
340 seq = This->base.dinput->evsequence++;
342 rel.x = ri->data.mouse.lLastX;
343 rel.y = ri->data.mouse.lLastY;
344 if (ri->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE)
346 GetCursorPos( &pt );
347 rel.x -= pt.x;
348 rel.y -= pt.y;
351 This->m_state.lX += rel.x;
352 This->m_state.lY += rel.y;
354 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
356 pt.x = This->m_state.lX;
357 pt.y = This->m_state.lY;
359 else
361 pt = rel;
364 if (rel.x)
365 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS,
366 pt.x, GetCurrentTime(), seq );
368 if (rel.y)
369 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS,
370 pt.y, GetCurrentTime(), seq );
372 if (rel.x || rel.y)
374 if ((This->warp_override == WARP_FORCE_ON) ||
375 (This->warp_override != WARP_DISABLE && (This->base.dwCoopLevel & DISCL_EXCLUSIVE)))
376 This->need_warp = TRUE;
379 if (ri->data.mouse.usButtonFlags & RI_MOUSE_WHEEL)
381 This->m_state.lZ += (wdata = (SHORT)ri->data.mouse.usButtonData);
382 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS,
383 wdata, GetCurrentTime(), seq );
386 for (i = 0; i < ARRAY_SIZE(mouse_button_flags); ++i)
388 if (ri->data.mouse.usButtonFlags & mouse_button_flags[i])
390 This->m_state.rgbButtons[i / 2] = 0x80 - (i % 2) * 0x80;
391 queue_event( iface, DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE +(i / 2) ) | DIDFT_PSHBUTTON,
392 This->m_state.rgbButtons[i / 2], GetCurrentTime(), seq );
396 LeaveCriticalSection( &This->base.crit );
399 /* low-level mouse hook */
400 int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam )
402 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
403 SysMouseImpl* This = impl_from_IDirectInputDevice8A(iface);
404 int wdata = 0, inst_id = -1, ret = 0;
406 TRACE("msg %lx @ (%d %d)\n", wparam, hook->pt.x, hook->pt.y);
408 EnterCriticalSection(&This->base.crit);
410 switch(wparam) {
411 case WM_MOUSEMOVE:
413 POINT pt, pt1;
415 GetCursorPos(&pt);
416 This->m_state.lX += pt.x = hook->pt.x - pt.x;
417 This->m_state.lY += pt.y = hook->pt.y - pt.y;
419 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
421 pt1.x = This->m_state.lX;
422 pt1.y = This->m_state.lY;
423 } else
424 pt1 = pt;
426 if (pt.x)
428 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
429 wdata = pt1.x;
431 if (pt.y)
433 /* Already have X, need to queue it */
434 if (inst_id != -1)
435 queue_event(iface, inst_id,
436 wdata, GetCurrentTime(), This->base.dinput->evsequence);
437 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
438 wdata = pt1.y;
441 if (pt.x || pt.y)
443 if ((This->warp_override == WARP_FORCE_ON) ||
444 (This->warp_override != WARP_DISABLE && (This->base.dwCoopLevel & DISCL_EXCLUSIVE)))
445 This->need_warp = TRUE;
447 break;
449 case WM_MOUSEWHEEL:
450 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
451 This->m_state.lZ += wdata = (short)HIWORD(hook->mouseData);
452 /* FarCry crashes if it gets a mouse wheel message */
453 /* FIXME: should probably filter out other messages too */
454 ret = This->clipped;
455 break;
456 case WM_LBUTTONDOWN:
457 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
458 This->m_state.rgbButtons[0] = wdata = 0x80;
459 break;
460 case WM_LBUTTONUP:
461 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
462 This->m_state.rgbButtons[0] = wdata = 0x00;
463 break;
464 case WM_RBUTTONDOWN:
465 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
466 This->m_state.rgbButtons[1] = wdata = 0x80;
467 break;
468 case WM_RBUTTONUP:
469 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
470 This->m_state.rgbButtons[1] = wdata = 0x00;
471 break;
472 case WM_MBUTTONDOWN:
473 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
474 This->m_state.rgbButtons[2] = wdata = 0x80;
475 break;
476 case WM_MBUTTONUP:
477 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
478 This->m_state.rgbButtons[2] = wdata = 0x00;
479 break;
480 case WM_XBUTTONDOWN:
481 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
482 This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x80;
483 break;
484 case WM_XBUTTONUP:
485 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
486 This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x00;
487 break;
491 if (inst_id != -1)
493 _dump_mouse_state(&This->m_state);
494 queue_event(iface, inst_id,
495 wdata, GetCurrentTime(), This->base.dinput->evsequence++);
498 LeaveCriticalSection(&This->base.crit);
499 return ret;
502 static void warp_check( SysMouseImpl* This, BOOL force )
504 DWORD now = GetCurrentTime();
505 const DWORD interval = This->clipped ? 500 : 10;
507 if (force || (This->need_warp && (now - This->last_warped > interval)))
509 RECT rect, new_rect;
510 POINT mapped_center;
512 This->last_warped = now;
513 This->need_warp = FALSE;
514 if (!GetClientRect(This->base.win, &rect)) return;
515 MapWindowPoints( This->base.win, 0, (POINT *)&rect, 2 );
516 if (!This->clipped)
518 mapped_center.x = (rect.left + rect.right) / 2;
519 mapped_center.y = (rect.top + rect.bottom) / 2;
520 TRACE("Warping mouse to %d - %d\n", mapped_center.x, mapped_center.y);
521 SetCursorPos( mapped_center.x, mapped_center.y );
523 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
525 /* make sure we clip even if the window covers the whole screen */
526 rect.left = max( rect.left, GetSystemMetrics( SM_XVIRTUALSCREEN ) + 1 );
527 rect.top = max( rect.top, GetSystemMetrics( SM_YVIRTUALSCREEN ) + 1 );
528 rect.right = min( rect.right, rect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN ) - 2 );
529 rect.bottom = min( rect.bottom, rect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN ) - 2 );
530 TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect ));
531 ClipCursor( &rect );
532 This->clipped = GetClipCursor( &new_rect ) && EqualRect( &rect, &new_rect );
538 /******************************************************************************
539 * Acquire : gets exclusive control of the mouse
541 static HRESULT WINAPI SysMouseWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
543 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
544 POINT point;
545 HRESULT res;
547 TRACE("(this=%p)\n",This);
549 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK) return res;
551 /* Init the mouse state */
552 GetCursorPos( &point );
553 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
555 This->m_state.lX = point.x;
556 This->m_state.lY = point.y;
557 } else {
558 This->m_state.lX = 0;
559 This->m_state.lY = 0;
560 This->org_coords = point;
562 This->m_state.lZ = 0;
563 This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
564 This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
565 This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
567 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
569 ShowCursor(FALSE); /* hide cursor */
570 warp_check( This, TRUE );
572 else if (This->warp_override == WARP_FORCE_ON)
574 /* Need a window to warp mouse in. */
575 if (!This->base.win) This->base.win = GetDesktopWindow();
576 warp_check( This, TRUE );
578 else if (This->clipped)
580 ClipCursor( NULL );
581 This->clipped = FALSE;
584 return DI_OK;
587 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
589 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
590 return SysMouseWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
593 /******************************************************************************
594 * Unacquire : frees the mouse
596 static HRESULT WINAPI SysMouseWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
598 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
599 HRESULT res;
601 TRACE("(this=%p)\n",This);
603 if ((res = IDirectInputDevice2WImpl_Unacquire(iface)) != DI_OK) return res;
605 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
607 ClipCursor(NULL);
608 ShowCursor(TRUE); /* show cursor */
609 This->clipped = FALSE;
612 /* And put the mouse cursor back where it was at acquire time */
613 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
615 TRACE("warping mouse back to %s\n", wine_dbgstr_point(&This->org_coords));
616 SetCursorPos(This->org_coords.x, This->org_coords.y);
619 return DI_OK;
622 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
624 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
625 return SysMouseWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
628 /******************************************************************************
629 * GetDeviceState : returns the "state" of the mouse.
631 * For the moment, only the "standard" return structure (DIMOUSESTATE) is
632 * supported.
634 static HRESULT WINAPI SysMouseWImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
636 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
637 TRACE("(%p)->(%u,%p)\n", This, len, ptr);
639 if(This->base.acquired == 0) return DIERR_NOTACQUIRED;
641 check_dinput_events();
643 EnterCriticalSection(&This->base.crit);
644 _dump_mouse_state(&This->m_state);
646 /* Copy the current mouse state */
647 fill_DataFormat(ptr, len, &This->m_state, &This->base.data_format);
649 /* Initialize the buffer when in relative mode */
650 if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
652 This->m_state.lX = 0;
653 This->m_state.lY = 0;
654 This->m_state.lZ = 0;
656 LeaveCriticalSection(&This->base.crit);
658 warp_check( This, FALSE );
659 return DI_OK;
662 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
664 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
665 return SysMouseWImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
668 /******************************************************************************
669 * GetDeviceData : gets buffered input data.
671 static HRESULT WINAPI SysMouseWImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface,
672 DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
674 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
675 HRESULT res;
677 res = IDirectInputDevice2WImpl_GetDeviceData(iface, dodsize, dod, entries, flags);
678 if (SUCCEEDED(res)) warp_check( This, FALSE );
679 return res;
682 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
683 DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
685 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
686 return SysMouseWImpl_GetDeviceData(IDirectInputDevice8W_from_impl(This), dodsize, dod, entries, flags);
689 /******************************************************************************
690 * GetProperty : get input device properties
692 static HRESULT WINAPI SysMouseWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
694 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
696 TRACE("(%p) %s,%p\n", This, debugstr_guid(rguid), pdiph);
697 _dump_DIPROPHEADER(pdiph);
699 if (IS_DIPROP(rguid)) {
700 switch (LOWORD(rguid)) {
701 case (DWORD_PTR) DIPROP_GRANULARITY: {
702 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
704 if (
705 ((pdiph->dwHow == DIPH_BYOFFSET) &&
706 ((pdiph->dwObj == DIMOFS_X) ||
707 (pdiph->dwObj == DIMOFS_Y)))
709 ((pdiph->dwHow == DIPH_BYID) &&
710 ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
711 (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS))))
713 /* Set granularity of X/Y Axis to 1. See MSDN on DIPROP_GRANULARITY */
714 pr->dwData = 1;
715 } else {
716 /* We'll just assume that the app asks about the Z axis */
717 pr->dwData = WHEEL_DELTA;
720 break;
723 case (DWORD_PTR) DIPROP_RANGE: {
724 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
726 if ((pdiph->dwHow == DIPH_BYID) &&
727 ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
728 (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
729 /* Querying the range of either the X or the Y axis. As I do
730 not know the range, do as if the range were
731 unrestricted...*/
732 pr->lMin = DIPROPRANGE_NOMIN;
733 pr->lMax = DIPROPRANGE_NOMAX;
736 break;
738 case (DWORD_PTR) DIPROP_VIDPID:
739 return DIERR_UNSUPPORTED;
740 default:
741 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
745 return DI_OK;
748 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
750 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
751 return SysMouseWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
754 /******************************************************************************
755 * GetCapabilities : get the device capabilities
757 static HRESULT WINAPI SysMouseWImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
759 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
760 DIDEVCAPS devcaps;
762 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
764 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
765 WARN("invalid parameter\n");
766 return DIERR_INVALIDPARAM;
769 devcaps.dwSize = lpDIDevCaps->dwSize;
770 devcaps.dwFlags = DIDC_ATTACHED | DIDC_EMULATED;
771 if (This->base.dinput->dwVersion >= 0x0800)
772 devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
773 else
774 devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
775 devcaps.dwAxes = 3;
776 devcaps.dwButtons = 8;
777 devcaps.dwPOVs = 0;
778 devcaps.dwFFSamplePeriod = 0;
779 devcaps.dwFFMinTimeResolution = 0;
780 devcaps.dwFirmwareRevision = 100;
781 devcaps.dwHardwareRevision = 100;
782 devcaps.dwFFDriverVersion = 0;
784 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
786 return DI_OK;
789 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
791 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
792 return SysMouseWImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
795 /******************************************************************************
796 * GetObjectInfo : get information about a device object such as a button
797 * or axis
799 static HRESULT WINAPI SysMouseWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
800 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
802 static const WCHAR x_axisW[] = {'X','-','A','x','i','s',0};
803 static const WCHAR y_axisW[] = {'Y','-','A','x','i','s',0};
804 static const WCHAR wheelW[] = {'W','h','e','e','l',0};
805 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
806 HRESULT res;
808 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
809 if (res != DI_OK) return res;
811 if (IsEqualGUID(&pdidoi->guidType, &GUID_XAxis)) strcpyW(pdidoi->tszName, x_axisW);
812 else if (IsEqualGUID(&pdidoi->guidType, &GUID_YAxis)) strcpyW(pdidoi->tszName, y_axisW);
813 else if (IsEqualGUID(&pdidoi->guidType, &GUID_ZAxis)) strcpyW(pdidoi->tszName, wheelW);
814 else if (pdidoi->dwType & DIDFT_BUTTON)
815 wsprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType) - 3);
817 if(pdidoi->dwType & DIDFT_AXIS)
818 pdidoi->dwFlags |= DIDOI_ASPECTPOSITION;
820 _dump_OBJECTINSTANCEW(pdidoi);
821 return res;
824 static HRESULT WINAPI SysMouseAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
825 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
827 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
828 HRESULT res;
829 DIDEVICEOBJECTINSTANCEW didoiW;
830 DWORD dwSize = pdidoi->dwSize;
832 didoiW.dwSize = sizeof(didoiW);
833 res = SysMouseWImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
834 if (res != DI_OK) return res;
836 memset(pdidoi, 0, pdidoi->dwSize);
837 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
838 pdidoi->dwSize = dwSize;
839 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
840 sizeof(pdidoi->tszName), NULL, NULL);
842 return res;
845 /******************************************************************************
846 * GetDeviceInfo : get information about a device's identity
848 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
849 LPDIRECTINPUTDEVICE8A iface,
850 LPDIDEVICEINSTANCEA pdidi)
852 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
853 TRACE("(this=%p,%p)\n", This, pdidi);
855 fill_mouse_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
857 return DI_OK;
860 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
862 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
863 TRACE("(this=%p,%p)\n", This, pdidi);
865 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
866 WARN(" dinput3 not supported yet...\n");
867 return DI_OK;
870 fill_mouse_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
872 return DI_OK;
875 static HRESULT WINAPI SysMouseWImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
876 LPDIACTIONFORMATW lpdiaf,
877 LPCWSTR lpszUserName,
878 DWORD dwFlags)
880 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
882 return _build_action_map(iface, lpdiaf, lpszUserName, dwFlags, DIMOUSE_MASK, &c_dfDIMouse2);
885 static HRESULT WINAPI SysMouseAImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
886 LPDIACTIONFORMATA lpdiaf,
887 LPCSTR lpszUserName,
888 DWORD dwFlags)
890 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
891 DIACTIONFORMATW diafW;
892 HRESULT hr;
893 WCHAR *lpszUserNameW = NULL;
894 int username_size;
896 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
897 _copy_diactionformatAtoW(&diafW, lpdiaf);
899 if (lpszUserName != NULL)
901 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
902 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
903 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
906 hr = SysMouseWImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
908 _copy_diactionformatWtoA(lpdiaf, &diafW);
909 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
910 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
912 return hr;
915 static HRESULT WINAPI SysMouseWImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
916 LPDIACTIONFORMATW lpdiaf,
917 LPCWSTR lpszUserName,
918 DWORD dwFlags)
920 SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
921 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This, lpdiaf, debugstr_w(lpszUserName), dwFlags);
923 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, &c_dfDIMouse2);
926 static HRESULT WINAPI SysMouseAImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
927 LPDIACTIONFORMATA lpdiaf,
928 LPCSTR lpszUserName,
929 DWORD dwFlags)
931 SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
932 DIACTIONFORMATW diafW;
933 HRESULT hr;
934 WCHAR *lpszUserNameW = NULL;
935 int username_size;
937 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
938 _copy_diactionformatAtoW(&diafW, lpdiaf);
940 if (lpszUserName != NULL)
942 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
943 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
944 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
947 hr = SysMouseWImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
949 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
950 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
952 return hr;
955 static const IDirectInputDevice8AVtbl SysMouseAvt =
957 IDirectInputDevice2AImpl_QueryInterface,
958 IDirectInputDevice2AImpl_AddRef,
959 IDirectInputDevice2AImpl_Release,
960 SysMouseAImpl_GetCapabilities,
961 IDirectInputDevice2AImpl_EnumObjects,
962 SysMouseAImpl_GetProperty,
963 IDirectInputDevice2AImpl_SetProperty,
964 SysMouseAImpl_Acquire,
965 SysMouseAImpl_Unacquire,
966 SysMouseAImpl_GetDeviceState,
967 SysMouseAImpl_GetDeviceData,
968 IDirectInputDevice2AImpl_SetDataFormat,
969 IDirectInputDevice2AImpl_SetEventNotification,
970 IDirectInputDevice2AImpl_SetCooperativeLevel,
971 SysMouseAImpl_GetObjectInfo,
972 SysMouseAImpl_GetDeviceInfo,
973 IDirectInputDevice2AImpl_RunControlPanel,
974 IDirectInputDevice2AImpl_Initialize,
975 IDirectInputDevice2AImpl_CreateEffect,
976 IDirectInputDevice2AImpl_EnumEffects,
977 IDirectInputDevice2AImpl_GetEffectInfo,
978 IDirectInputDevice2AImpl_GetForceFeedbackState,
979 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
980 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
981 IDirectInputDevice2AImpl_Escape,
982 IDirectInputDevice2AImpl_Poll,
983 IDirectInputDevice2AImpl_SendDeviceData,
984 IDirectInputDevice7AImpl_EnumEffectsInFile,
985 IDirectInputDevice7AImpl_WriteEffectToFile,
986 SysMouseAImpl_BuildActionMap,
987 SysMouseAImpl_SetActionMap,
988 IDirectInputDevice8AImpl_GetImageInfo
991 static const IDirectInputDevice8WVtbl SysMouseWvt =
993 IDirectInputDevice2WImpl_QueryInterface,
994 IDirectInputDevice2WImpl_AddRef,
995 IDirectInputDevice2WImpl_Release,
996 SysMouseWImpl_GetCapabilities,
997 IDirectInputDevice2WImpl_EnumObjects,
998 SysMouseWImpl_GetProperty,
999 IDirectInputDevice2WImpl_SetProperty,
1000 SysMouseWImpl_Acquire,
1001 SysMouseWImpl_Unacquire,
1002 SysMouseWImpl_GetDeviceState,
1003 SysMouseWImpl_GetDeviceData,
1004 IDirectInputDevice2WImpl_SetDataFormat,
1005 IDirectInputDevice2WImpl_SetEventNotification,
1006 IDirectInputDevice2WImpl_SetCooperativeLevel,
1007 SysMouseWImpl_GetObjectInfo,
1008 SysMouseWImpl_GetDeviceInfo,
1009 IDirectInputDevice2WImpl_RunControlPanel,
1010 IDirectInputDevice2WImpl_Initialize,
1011 IDirectInputDevice2WImpl_CreateEffect,
1012 IDirectInputDevice2WImpl_EnumEffects,
1013 IDirectInputDevice2WImpl_GetEffectInfo,
1014 IDirectInputDevice2WImpl_GetForceFeedbackState,
1015 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
1016 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
1017 IDirectInputDevice2WImpl_Escape,
1018 IDirectInputDevice2WImpl_Poll,
1019 IDirectInputDevice2WImpl_SendDeviceData,
1020 IDirectInputDevice7WImpl_EnumEffectsInFile,
1021 IDirectInputDevice7WImpl_WriteEffectToFile,
1022 SysMouseWImpl_BuildActionMap,
1023 SysMouseWImpl_SetActionMap,
1024 IDirectInputDevice8WImpl_GetImageInfo