wined3d: Move storing the render target from ActiveRender to SetRenderTarget.
[wine/dibdrv.git] / dlls / dinput / mouse.c
blobd93975a877291bd2de1be3058f53ea443a77b9db
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 "dinput.h"
35 #include "dinput_private.h"
36 #include "device_private.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 #define MOUSE_HACK
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_L_BUTTON_INSTANCE 0
49 #define WINE_MOUSE_R_BUTTON_INSTANCE 1
50 #define WINE_MOUSE_M_BUTTON_INSTANCE 2
51 #define WINE_MOUSE_D_BUTTON_INSTANCE 3
53 /* ------------------------------- */
54 /* Wine mouse internal data format */
55 /* ------------------------------- */
57 /* Constants used to access the offset array */
58 #define WINE_MOUSE_X_POSITION 0
59 #define WINE_MOUSE_Y_POSITION 1
60 #define WINE_MOUSE_Z_POSITION 2
61 #define WINE_MOUSE_L_POSITION 3
62 #define WINE_MOUSE_R_POSITION 4
63 #define WINE_MOUSE_M_POSITION 5
65 static const IDirectInputDevice8AVtbl SysMouseAvt;
66 static const IDirectInputDevice8WVtbl SysMouseWvt;
68 typedef struct SysMouseImpl SysMouseImpl;
70 typedef enum {
71 WARP_DONE, /* Warping has been done */
72 WARP_NEEDED, /* Warping is needed */
73 WARP_STARTED /* Warping has been done, waiting for the warp event */
74 } WARP_STATUS;
76 struct SysMouseImpl
78 struct IDirectInputDevice2AImpl base;
80 IDirectInputImpl *dinput;
82 /* SysMouseAImpl */
83 /* Previous position for relative moves */
84 LONG prevX, prevY;
85 /* These are used in case of relative -> absolute transitions */
86 POINT org_coords;
87 POINT mapped_center;
88 DWORD win_centerX, win_centerY;
89 /* warping: whether we need to move mouse back to middle once we
90 * reach window borders (for e.g. shooters, "surface movement" games) */
91 WARP_STATUS need_warp;
92 DWORD last_warped;
94 /* This is for mouse reporting. */
95 DIMOUSESTATE2 m_state;
98 /* FIXME: This is ugly and not thread safe :/ */
99 static IDirectInputDevice8A* current_lock = NULL;
101 static GUID DInput_Wine_Mouse_GUID = { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
102 0x9e573ed8,
103 0x7734,
104 0x11d2,
105 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
108 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
109 DWORD dwSize;
110 DIDEVICEINSTANCEA ddi;
112 dwSize = lpddi->dwSize;
114 TRACE("%d %p\n", dwSize, lpddi);
116 memset(lpddi, 0, dwSize);
117 memset(&ddi, 0, sizeof(ddi));
119 ddi.dwSize = dwSize;
120 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
121 ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
122 if (version >= 0x0800)
123 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
124 else
125 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
126 strcpy(ddi.tszInstanceName, "Mouse");
127 strcpy(ddi.tszProductName, "Wine Mouse");
129 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
132 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
133 DWORD dwSize;
134 DIDEVICEINSTANCEW ddi;
136 dwSize = lpddi->dwSize;
138 TRACE("%d %p\n", dwSize, lpddi);
140 memset(lpddi, 0, dwSize);
141 memset(&ddi, 0, sizeof(ddi));
143 ddi.dwSize = dwSize;
144 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
145 ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
146 if (version >= 0x0800)
147 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
148 else
149 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
150 MultiByteToWideChar(CP_ACP, 0, "Mouse", -1, ddi.tszInstanceName, MAX_PATH);
151 MultiByteToWideChar(CP_ACP, 0, "Wine Mouse", -1, ddi.tszProductName, MAX_PATH);
153 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
156 static BOOL mousedev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
158 if (id != 0)
159 return FALSE;
161 if ((dwDevType == 0) ||
162 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
163 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
164 TRACE("Enumerating the mouse device\n");
166 fill_mouse_dideviceinstanceA(lpddi, version);
168 return TRUE;
171 return FALSE;
174 static BOOL mousedev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
176 if (id != 0)
177 return FALSE;
179 if ((dwDevType == 0) ||
180 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
181 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
182 TRACE("Enumerating the mouse device\n");
184 fill_mouse_dideviceinstanceW(lpddi, version);
186 return TRUE;
189 return FALSE;
192 static SysMouseImpl *alloc_device(REFGUID rguid, const void *mvt, IDirectInputImpl *dinput)
194 SysMouseImpl* newDevice;
196 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseImpl));
197 if (!newDevice) return NULL;
198 newDevice->base.lpVtbl = mvt;
199 newDevice->base.ref = 1;
200 newDevice->base.dwCoopLevel = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;
201 memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
202 InitializeCriticalSection(&newDevice->base.crit);
203 newDevice->dinput = dinput;
205 newDevice->base.data_format.wine_df = &c_dfDIMouse2;
206 if (create_DataFormat(&c_dfDIMouse2, &newDevice->base.data_format) == DI_OK)
207 return newDevice;
209 return NULL;
212 static HRESULT mousedev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
214 if ((IsEqualGUID(&GUID_SysMouse,rguid)) || /* Generic Mouse */
215 (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
216 if ((riid == NULL) ||
217 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
218 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
219 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
220 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
221 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysMouseAvt, dinput);
222 TRACE("Creating a Mouse device (%p)\n", *pdev);
223 if (!*pdev) return DIERR_OUTOFMEMORY;
224 return DI_OK;
225 } else
226 return DIERR_NOINTERFACE;
229 return DIERR_DEVICENOTREG;
232 static HRESULT mousedev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
234 if ((IsEqualGUID(&GUID_SysMouse,rguid)) || /* Generic Mouse */
235 (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
236 if ((riid == NULL) ||
237 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
238 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
239 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
240 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
241 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysMouseWvt, dinput);
242 TRACE("Creating a Mouse device (%p)\n", *pdev);
243 if (!*pdev) return DIERR_OUTOFMEMORY;
244 return DI_OK;
245 } else
246 return DIERR_NOINTERFACE;
249 return DIERR_DEVICENOTREG;
252 const struct dinput_device mouse_device = {
253 "Wine mouse driver",
254 mousedev_enum_deviceA,
255 mousedev_enum_deviceW,
256 mousedev_create_deviceA,
257 mousedev_create_deviceW
260 /******************************************************************************
261 * SysMouseA (DInput Mouse support)
264 /******************************************************************************
265 * Release : release the mouse buffer.
267 static ULONG WINAPI SysMouseAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
269 SysMouseImpl *This = (SysMouseImpl *)iface;
270 ULONG ref;
272 ref = InterlockedDecrement(&This->base.ref);
273 if (ref)
274 return ref;
276 set_dinput_hook(WH_MOUSE_LL, NULL);
278 /* Free the data queue */
279 HeapFree(GetProcessHeap(), 0, This->base.data_queue);
281 release_DataFormat(&This->base.data_format);
283 DeleteCriticalSection(&This->base.crit);
284 HeapFree(GetProcessHeap(),0,This);
285 return 0;
288 /* low-level mouse hook */
289 static LRESULT CALLBACK dinput_mouse_hook( int code, WPARAM wparam, LPARAM lparam )
291 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
292 SysMouseImpl* This = (SysMouseImpl*) current_lock;
293 DWORD dwCoop;
294 int wdata;
296 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
298 EnterCriticalSection(&This->base.crit);
299 dwCoop = This->base.dwCoopLevel;
301 if (wparam == WM_MOUSEMOVE) {
302 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS) {
303 if (hook->pt.x != This->prevX)
304 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_X_POSITION],
305 hook->pt.x, hook->time, This->dinput->evsequence);
306 if (hook->pt.y != This->prevY)
307 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_Y_POSITION],
308 hook->pt.y, hook->time, This->dinput->evsequence);
309 } else {
310 /* Now, warp handling */
311 if ((This->need_warp == WARP_STARTED) &&
312 (hook->pt.x == This->mapped_center.x) && (hook->pt.y == This->mapped_center.y)) {
313 /* Warp has been done... */
314 This->need_warp = WARP_DONE;
315 goto end;
318 /* Relative mouse input with absolute mouse event : the real fun starts here... */
319 if ((This->need_warp == WARP_NEEDED) ||
320 (This->need_warp == WARP_STARTED)) {
321 if (hook->pt.x != This->prevX)
322 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_X_POSITION],
323 hook->pt.x - This->prevX, hook->time, This->dinput->evsequence);
324 if (hook->pt.y != This->prevY)
325 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_Y_POSITION],
326 hook->pt.y - This->prevY, hook->time, This->dinput->evsequence);
327 } else {
328 /* This is the first time the event handler has been called after a
329 GetDeviceData or GetDeviceState. */
330 if (hook->pt.x != This->mapped_center.x) {
331 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_X_POSITION],
332 hook->pt.x - This->mapped_center.x, hook->time, This->dinput->evsequence);
333 This->need_warp = WARP_NEEDED;
336 if (hook->pt.y != This->mapped_center.y) {
337 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_Y_POSITION],
338 hook->pt.y - This->mapped_center.y, hook->time, This->dinput->evsequence);
339 This->need_warp = WARP_NEEDED;
344 This->prevX = hook->pt.x;
345 This->prevY = hook->pt.y;
347 if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS) {
348 This->m_state.lX = hook->pt.x;
349 This->m_state.lY = hook->pt.y;
350 } else {
351 This->m_state.lX = hook->pt.x - This->mapped_center.x;
352 This->m_state.lY = hook->pt.y - This->mapped_center.y;
356 TRACE(" msg %x pt %d %d (W=%d)\n",
357 wparam, hook->pt.x, hook->pt.y, !(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS) && This->need_warp );
359 switch(wparam) {
360 case WM_LBUTTONDOWN:
361 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_L_POSITION],
362 0x80, hook->time, This->dinput->evsequence);
363 This->m_state.rgbButtons[0] = 0x80;
364 break;
365 case WM_LBUTTONUP:
366 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_L_POSITION],
367 0x00, hook->time, This->dinput->evsequence);
368 This->m_state.rgbButtons[0] = 0x00;
369 break;
370 case WM_RBUTTONDOWN:
371 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_R_POSITION],
372 0x80, hook->time, This->dinput->evsequence);
373 This->m_state.rgbButtons[1] = 0x80;
374 break;
375 case WM_RBUTTONUP:
376 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_R_POSITION],
377 0x00, hook->time, This->dinput->evsequence);
378 This->m_state.rgbButtons[1] = 0x00;
379 break;
380 case WM_MBUTTONDOWN:
381 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_M_POSITION],
382 0x80, hook->time, This->dinput->evsequence);
383 This->m_state.rgbButtons[2] = 0x80;
384 break;
385 case WM_MBUTTONUP:
386 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_M_POSITION],
387 0x00, hook->time, This->dinput->evsequence);
388 This->m_state.rgbButtons[2] = 0x00;
389 break;
390 case WM_MOUSEWHEEL:
391 wdata = (short)HIWORD(hook->mouseData);
392 queue_event((LPDIRECTINPUTDEVICE8A)This, This->base.data_format.offsets[WINE_MOUSE_Z_POSITION],
393 wdata, hook->time, This->dinput->evsequence);
394 This->m_state.lZ += wdata;
395 break;
398 TRACE("(X: %d - Y: %d L: %02x M: %02x R: %02x)\n",
399 This->m_state.lX, This->m_state.lY,
400 This->m_state.rgbButtons[0], This->m_state.rgbButtons[2], This->m_state.rgbButtons[1]);
402 This->dinput->evsequence++;
404 end:
405 /* Mouse moved -> send event if asked */
406 if (This->base.hEvent) SetEvent(This->base.hEvent);
408 LeaveCriticalSection(&This->base.crit);
410 /* Ignore message */
411 if (dwCoop & DISCL_EXCLUSIVE) return 1;
413 /* Pass the events down to previous handlers (e.g. win32 input) */
414 return CallNextHookEx( 0, code, wparam, lparam );
417 static BOOL dinput_window_check(SysMouseImpl* This) {
418 RECT rect;
419 DWORD centerX, centerY;
421 /* make sure the window hasn't moved */
422 if(!GetWindowRect(This->base.win, &rect))
423 return FALSE;
424 centerX = (rect.right - rect.left) / 2;
425 centerY = (rect.bottom - rect.top ) / 2;
426 if (This->win_centerX != centerX || This->win_centerY != centerY) {
427 This->win_centerX = centerX;
428 This->win_centerY = centerY;
430 This->mapped_center.x = This->win_centerX;
431 This->mapped_center.y = This->win_centerY;
432 MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
433 return TRUE;
437 /******************************************************************************
438 * Acquire : gets exclusive control of the mouse
440 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
442 SysMouseImpl *This = (SysMouseImpl *)iface;
443 RECT rect;
444 POINT point;
445 HRESULT res;
447 TRACE("(this=%p)\n",This);
449 if ((res = IDirectInputDevice2AImpl_Acquire(iface)) != DI_OK) return res;
451 /* Store (in a global variable) the current lock */
452 current_lock = (IDirectInputDevice8A*)This;
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 This->prevX = point.x;
461 This->prevY = point.y;
462 } else {
463 This->m_state.lX = 0;
464 This->m_state.lY = 0;
465 This->org_coords = point;
467 This->m_state.lZ = 0;
468 This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
469 This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
470 This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
472 /* Install our mouse hook */
473 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
474 ShowCursor(FALSE); /* hide cursor */
475 set_dinput_hook(WH_MOUSE_LL, dinput_mouse_hook);
477 /* Get the window dimension and find the center */
478 GetWindowRect(This->base.win, &rect);
479 This->win_centerX = (rect.right - rect.left) / 2;
480 This->win_centerY = (rect.bottom - rect.top ) / 2;
482 /* Warp the mouse to the center of the window */
483 if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
485 This->mapped_center.x = This->win_centerX;
486 This->mapped_center.y = This->win_centerY;
487 MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
488 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
489 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
490 This->last_warped = GetCurrentTime();
492 #ifdef MOUSE_HACK
493 This->need_warp = WARP_DONE;
494 #else
495 This->need_warp = WARP_STARTED;
496 #endif
499 return DI_OK;
502 /******************************************************************************
503 * Unacquire : frees the mouse
505 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
507 SysMouseImpl *This = (SysMouseImpl *)iface;
508 HRESULT res;
510 TRACE("(this=%p)\n",This);
512 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
514 set_dinput_hook(WH_MOUSE_LL, NULL);
515 if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
516 ShowCursor(TRUE); /* show cursor */
518 /* No more locks */
519 if (current_lock == (IDirectInputDevice8A*) This)
520 current_lock = NULL;
521 else
522 ERR("this(%p) != current_lock(%p)\n", This, current_lock);
524 /* And put the mouse cursor back where it was at acquire time */
525 if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
527 TRACE(" warping mouse back to (%d , %d)\n", This->org_coords.x, This->org_coords.y);
528 SetCursorPos(This->org_coords.x, This->org_coords.y);
531 return DI_OK;
534 /******************************************************************************
535 * GetDeviceState : returns the "state" of the mouse.
537 * For the moment, only the "standard" return structure (DIMOUSESTATE) is
538 * supported.
540 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(
541 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
543 SysMouseImpl *This = (SysMouseImpl *)iface;
545 if(This->base.acquired == 0) return DIERR_NOTACQUIRED;
547 EnterCriticalSection(&This->base.crit);
548 TRACE("(this=%p,0x%08x,%p):\n", This, len, ptr);
549 TRACE("(X: %d - Y: %d - Z: %d L: %02x M: %02x R: %02x)\n",
550 This->m_state.lX, This->m_state.lY, This->m_state.lZ,
551 This->m_state.rgbButtons[0], This->m_state.rgbButtons[2], This->m_state.rgbButtons[1]);
553 /* Copy the current mouse state */
554 fill_DataFormat(ptr, &(This->m_state), &This->base.data_format);
556 /* Initialize the buffer when in relative mode */
557 if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
559 This->m_state.lX = 0;
560 This->m_state.lY = 0;
561 This->m_state.lZ = 0;
564 /* Check if we need to do a mouse warping */
565 if (This->need_warp == WARP_NEEDED && (GetCurrentTime() - This->last_warped > 10)) {
566 if(!dinput_window_check(This))
568 LeaveCriticalSection(&This->base.crit);
569 return DIERR_GENERIC;
571 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
572 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
573 This->last_warped = GetCurrentTime();
575 #ifdef MOUSE_HACK
576 This->need_warp = WARP_DONE;
577 #else
578 This->need_warp = WARP_STARTED;
579 #endif
582 LeaveCriticalSection(&This->base.crit);
584 return DI_OK;
587 /******************************************************************************
588 * GetDeviceData : gets buffered input data.
590 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
591 DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
593 SysMouseImpl *This = (SysMouseImpl *)iface;
594 HRESULT res;
596 res = IDirectInputDevice2AImpl_GetDeviceData(iface, dodsize, dod, entries, flags);
597 if (FAILED(res)) return res;
599 /* Check if we need to do a mouse warping */
600 if (This->need_warp == WARP_NEEDED && (GetCurrentTime() - This->last_warped > 10)) {
601 if(!dinput_window_check(This))
602 return DIERR_GENERIC;
603 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
604 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
605 This->last_warped = GetCurrentTime();
607 #ifdef MOUSE_HACK
608 This->need_warp = WARP_DONE;
609 #else
610 This->need_warp = WARP_STARTED;
611 #endif
613 return res;
616 /******************************************************************************
617 * GetProperty : get input device properties
619 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
620 REFGUID rguid,
621 LPDIPROPHEADER pdiph)
623 SysMouseImpl *This = (SysMouseImpl *)iface;
625 TRACE("(%p) %s,%p\n", This, debugstr_guid(rguid), pdiph);
626 _dump_DIPROPHEADER(pdiph);
628 if (!HIWORD(rguid)) {
629 switch (LOWORD(rguid)) {
630 case (DWORD) DIPROP_GRANULARITY: {
631 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
633 /* We'll just assume that the app asks about the Z axis */
634 pr->dwData = WHEEL_DELTA;
636 break;
639 case (DWORD) DIPROP_RANGE: {
640 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
642 if ((pdiph->dwHow == DIPH_BYID) &&
643 ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
644 (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
645 /* Querying the range of either the X or the Y axis. As I do
646 not know the range, do as if the range were
647 unrestricted...*/
648 pr->lMin = DIPROPRANGE_NOMIN;
649 pr->lMax = DIPROPRANGE_NOMAX;
652 break;
655 default:
656 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
660 return DI_OK;
663 /******************************************************************************
664 * GetCapabilities : get the device capablitites
666 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(
667 LPDIRECTINPUTDEVICE8A iface,
668 LPDIDEVCAPS lpDIDevCaps)
670 SysMouseImpl *This = (SysMouseImpl *)iface;
671 DIDEVCAPS devcaps;
673 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
675 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
676 WARN("invalid parameter\n");
677 return DIERR_INVALIDPARAM;
680 devcaps.dwSize = lpDIDevCaps->dwSize;
681 devcaps.dwFlags = DIDC_ATTACHED;
682 if (This->dinput->dwVersion >= 0x0800)
683 devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
684 else
685 devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
686 devcaps.dwAxes = 3;
687 devcaps.dwButtons = 3;
688 devcaps.dwPOVs = 0;
689 devcaps.dwFFSamplePeriod = 0;
690 devcaps.dwFFMinTimeResolution = 0;
691 devcaps.dwFirmwareRevision = 100;
692 devcaps.dwHardwareRevision = 100;
693 devcaps.dwFFDriverVersion = 0;
695 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
697 return DI_OK;
701 /******************************************************************************
702 * EnumObjects : enumerate the different buttons and axis...
704 static HRESULT WINAPI SysMouseAImpl_EnumObjects(
705 LPDIRECTINPUTDEVICE8A iface,
706 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
707 LPVOID lpvRef,
708 DWORD dwFlags)
710 SysMouseImpl *This = (SysMouseImpl *)iface;
711 DIDEVICEOBJECTINSTANCEA ddoi;
713 TRACE("(this=%p,%p,%p,%08x)\n", This, lpCallback, lpvRef, dwFlags);
714 if (TRACE_ON(dinput)) {
715 TRACE(" - flags = ");
716 _dump_EnumObjects_flags(dwFlags);
717 TRACE("\n");
720 /* Only the fields till dwFFMaxForce are relevant */
721 memset(&ddoi, 0, sizeof(ddoi));
722 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
724 /* In a mouse, we have : two relative axis and three buttons */
725 if ((dwFlags == DIDFT_ALL) ||
726 (dwFlags & DIDFT_AXIS)) {
727 /* X axis */
728 ddoi.guidType = GUID_XAxis;
729 ddoi.dwOfs = This->base.data_format.offsets[WINE_MOUSE_X_POSITION];
730 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
731 strcpy(ddoi.tszName, "X-Axis");
732 _dump_OBJECTINSTANCEA(&ddoi);
733 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
735 /* Y axis */
736 ddoi.guidType = GUID_YAxis;
737 ddoi.dwOfs = This->base.data_format.offsets[WINE_MOUSE_Y_POSITION];
738 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
739 strcpy(ddoi.tszName, "Y-Axis");
740 _dump_OBJECTINSTANCEA(&ddoi);
741 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
743 /* Z axis */
744 ddoi.guidType = GUID_ZAxis;
745 ddoi.dwOfs = This->base.data_format.offsets[WINE_MOUSE_Z_POSITION];
746 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
747 strcpy(ddoi.tszName, "Z-Axis");
748 _dump_OBJECTINSTANCEA(&ddoi);
749 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
752 if ((dwFlags == DIDFT_ALL) ||
753 (dwFlags & DIDFT_BUTTON)) {
754 ddoi.guidType = GUID_Button;
756 /* Left button */
757 ddoi.dwOfs = This->base.data_format.offsets[WINE_MOUSE_L_POSITION];
758 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
759 strcpy(ddoi.tszName, "Left-Button");
760 _dump_OBJECTINSTANCEA(&ddoi);
761 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
763 /* Right button */
764 ddoi.dwOfs = This->base.data_format.offsets[WINE_MOUSE_R_POSITION];
765 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
766 strcpy(ddoi.tszName, "Right-Button");
767 _dump_OBJECTINSTANCEA(&ddoi);
768 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
770 /* Middle button */
771 ddoi.dwOfs = This->base.data_format.offsets[WINE_MOUSE_M_POSITION];
772 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
773 strcpy(ddoi.tszName, "Middle-Button");
774 _dump_OBJECTINSTANCEA(&ddoi);
775 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
778 return DI_OK;
781 static HRESULT WINAPI SysMouseWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface, LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback, LPVOID lpvRef,DWORD dwFlags)
783 SysMouseImpl *This = (SysMouseImpl *)iface;
785 device_enumobjects_AtoWcb_data data;
787 data.lpCallBack = lpCallback;
788 data.lpvRef = lpvRef;
790 return SysMouseAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
793 /******************************************************************************
794 * GetDeviceInfo : get information about a device's identity
796 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
797 LPDIRECTINPUTDEVICE8A iface,
798 LPDIDEVICEINSTANCEA pdidi)
800 SysMouseImpl *This = (SysMouseImpl *)iface;
801 TRACE("(this=%p,%p)\n", This, pdidi);
803 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
804 WARN(" dinput3 not supporte yet...\n");
805 return DI_OK;
808 fill_mouse_dideviceinstanceA(pdidi, This->dinput->dwVersion);
810 return DI_OK;
813 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
815 SysMouseImpl *This = (SysMouseImpl *)iface;
816 TRACE("(this=%p,%p)\n", This, pdidi);
818 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
819 WARN(" dinput3 not supporte yet...\n");
820 return DI_OK;
823 fill_mouse_dideviceinstanceW(pdidi, This->dinput->dwVersion);
825 return DI_OK;
829 static const IDirectInputDevice8AVtbl SysMouseAvt =
831 IDirectInputDevice2AImpl_QueryInterface,
832 IDirectInputDevice2AImpl_AddRef,
833 SysMouseAImpl_Release,
834 SysMouseAImpl_GetCapabilities,
835 SysMouseAImpl_EnumObjects,
836 SysMouseAImpl_GetProperty,
837 IDirectInputDevice2AImpl_SetProperty,
838 SysMouseAImpl_Acquire,
839 SysMouseAImpl_Unacquire,
840 SysMouseAImpl_GetDeviceState,
841 SysMouseAImpl_GetDeviceData,
842 IDirectInputDevice2AImpl_SetDataFormat,
843 IDirectInputDevice2AImpl_SetEventNotification,
844 IDirectInputDevice2AImpl_SetCooperativeLevel,
845 IDirectInputDevice2AImpl_GetObjectInfo,
846 SysMouseAImpl_GetDeviceInfo,
847 IDirectInputDevice2AImpl_RunControlPanel,
848 IDirectInputDevice2AImpl_Initialize,
849 IDirectInputDevice2AImpl_CreateEffect,
850 IDirectInputDevice2AImpl_EnumEffects,
851 IDirectInputDevice2AImpl_GetEffectInfo,
852 IDirectInputDevice2AImpl_GetForceFeedbackState,
853 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
854 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
855 IDirectInputDevice2AImpl_Escape,
856 IDirectInputDevice2AImpl_Poll,
857 IDirectInputDevice2AImpl_SendDeviceData,
858 IDirectInputDevice7AImpl_EnumEffectsInFile,
859 IDirectInputDevice7AImpl_WriteEffectToFile,
860 IDirectInputDevice8AImpl_BuildActionMap,
861 IDirectInputDevice8AImpl_SetActionMap,
862 IDirectInputDevice8AImpl_GetImageInfo
865 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
866 # define XCAST(fun) (typeof(SysMouseWvt.fun))
867 #else
868 # define XCAST(fun) (void*)
869 #endif
871 static const IDirectInputDevice8WVtbl SysMouseWvt =
873 IDirectInputDevice2WImpl_QueryInterface,
874 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
875 XCAST(Release)SysMouseAImpl_Release,
876 XCAST(GetCapabilities)SysMouseAImpl_GetCapabilities,
877 SysMouseWImpl_EnumObjects,
878 XCAST(GetProperty)SysMouseAImpl_GetProperty,
879 XCAST(SetProperty)IDirectInputDevice2AImpl_SetProperty,
880 XCAST(Acquire)SysMouseAImpl_Acquire,
881 XCAST(Unacquire)SysMouseAImpl_Unacquire,
882 XCAST(GetDeviceState)SysMouseAImpl_GetDeviceState,
883 XCAST(GetDeviceData)SysMouseAImpl_GetDeviceData,
884 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
885 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
886 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
887 IDirectInputDevice2WImpl_GetObjectInfo,
888 SysMouseWImpl_GetDeviceInfo,
889 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
890 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
891 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
892 IDirectInputDevice2WImpl_EnumEffects,
893 IDirectInputDevice2WImpl_GetEffectInfo,
894 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
895 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
896 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
897 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
898 XCAST(Poll)IDirectInputDevice2AImpl_Poll,
899 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
900 IDirectInputDevice7WImpl_EnumEffectsInFile,
901 IDirectInputDevice7WImpl_WriteEffectToFile,
902 IDirectInputDevice8WImpl_BuildActionMap,
903 IDirectInputDevice8WImpl_SetActionMap,
904 IDirectInputDevice8WImpl_GetImageInfo
906 #undef XCAST