kernel32/tests: Correctly skip tests when the Unicode drive functions are not available.
[wine] / dlls / dinput / mouse.c
1 /*              DirectInput Mouse device
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winerror.h"
33 #include "winreg.h"
34 #include "dinput.h"
35
36 #include "dinput_private.h"
37 #include "device_private.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
42
43 /* Wine mouse driver object instances */
44 #define WINE_MOUSE_X_AXIS_INSTANCE   0
45 #define WINE_MOUSE_Y_AXIS_INSTANCE   1
46 #define WINE_MOUSE_Z_AXIS_INSTANCE   2
47 #define WINE_MOUSE_BUTTONS_INSTANCE  3
48
49 static const IDirectInputDevice8AVtbl SysMouseAvt;
50 static const IDirectInputDevice8WVtbl SysMouseWvt;
51
52 typedef struct SysMouseImpl SysMouseImpl;
53
54 typedef enum
55 {
56     WARP_DEFAULT,
57     WARP_DISABLE,
58     WARP_FORCE_ON
59 } WARP_MOUSE;
60
61 struct SysMouseImpl
62 {
63     struct IDirectInputDevice2AImpl base;
64     
65     /* SysMouseAImpl */
66     /* These are used in case of relative -> absolute transitions */
67     POINT                           org_coords;
68     POINT                           mapped_center;
69     DWORD                           win_centerX, win_centerY;
70     /* warping: whether we need to move mouse back to middle once we
71      * reach window borders (for e.g. shooters, "surface movement" games) */
72     BOOL                            need_warp;
73     DWORD                           last_warped;
74
75     /* This is for mouse reporting. */
76     DIMOUSESTATE2                   m_state;
77
78     WARP_MOUSE                      warp_override;
79 };
80
81 static int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam );
82
83 const GUID DInput_Wine_Mouse_GUID = { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
84     0x9e573ed8, 0x7734, 0x11d2, {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
85 };
86
87 static void _dump_mouse_state(DIMOUSESTATE2 *m_state)
88 {
89     int i;
90
91     if (!TRACE_ON(dinput)) return;
92
93     TRACE("(X: %d Y: %d Z: %d", m_state->lX, m_state->lY, m_state->lZ);
94     for (i = 0; i < 5; i++) TRACE(" B%d: %02x", i, m_state->rgbButtons[i]);
95     TRACE(")\n");
96 }
97
98 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
99     DWORD dwSize;
100     DIDEVICEINSTANCEA ddi;
101     
102     dwSize = lpddi->dwSize;
103
104     TRACE("%d %p\n", dwSize, lpddi);
105     
106     memset(lpddi, 0, dwSize);
107     memset(&ddi, 0, sizeof(ddi));
108
109     ddi.dwSize = dwSize;
110     ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
111     ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
112     if (version >= 0x0800)
113         ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
114     else
115         ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
116     strcpy(ddi.tszInstanceName, "Mouse");
117     strcpy(ddi.tszProductName, "Wine Mouse");
118
119     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
120 }
121
122 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
123     DWORD dwSize;
124     DIDEVICEINSTANCEW ddi;
125     
126     dwSize = lpddi->dwSize;
127
128     TRACE("%d %p\n", dwSize, lpddi);
129     
130     memset(lpddi, 0, dwSize);
131     memset(&ddi, 0, sizeof(ddi));
132
133     ddi.dwSize = dwSize;
134     ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
135     ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
136     if (version >= 0x0800)
137         ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
138     else
139         ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
140     MultiByteToWideChar(CP_ACP, 0, "Mouse", -1, ddi.tszInstanceName, MAX_PATH);
141     MultiByteToWideChar(CP_ACP, 0, "Wine Mouse", -1, ddi.tszProductName, MAX_PATH);
142
143     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
144 }
145
146 static BOOL mousedev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
147 {
148     if (id != 0)
149         return FALSE;
150
151     if ((dwDevType == 0) ||
152         ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
153         (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
154         TRACE("Enumerating the mouse device\n");
155         
156         fill_mouse_dideviceinstanceA(lpddi, version);
157         
158         return TRUE;
159     }
160     
161     return FALSE;
162 }
163
164 static BOOL mousedev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
165 {
166     if (id != 0)
167         return FALSE;
168
169     if ((dwDevType == 0) ||
170         ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
171         (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
172         TRACE("Enumerating the mouse device\n");
173         
174         fill_mouse_dideviceinstanceW(lpddi, version);
175         
176         return TRUE;
177     }
178     
179     return FALSE;
180 }
181
182 static SysMouseImpl *alloc_device(REFGUID rguid, const void *mvt, IDirectInputImpl *dinput)
183 {
184     SysMouseImpl* newDevice;
185     LPDIDATAFORMAT df = NULL;
186     unsigned i;
187     char buffer[20];
188     HKEY hkey, appkey;
189
190     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseImpl));
191     if (!newDevice) return NULL;
192     newDevice->base.lpVtbl = mvt;
193     newDevice->base.ref = 1;
194     newDevice->base.dwCoopLevel = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;
195     newDevice->base.guid = *rguid;
196     InitializeCriticalSection(&newDevice->base.crit);
197     newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysMouseImpl*->base.crit");
198     newDevice->base.dinput = dinput;
199     newDevice->base.event_proc = dinput_mouse_hook;
200
201     get_app_key(&hkey, &appkey);
202     if (!get_config_key(hkey, appkey, "MouseWarpOverride", buffer, sizeof(buffer)))
203     {
204         if (!strcasecmp(buffer, "disable"))
205             newDevice->warp_override = WARP_DISABLE;
206         else if (!strcasecmp(buffer, "force"))
207             newDevice->warp_override = WARP_FORCE_ON;
208     }
209     if (appkey) RegCloseKey(appkey);
210     if (hkey) RegCloseKey(hkey);
211
212     /* Create copy of default data format */
213     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIMouse2.dwSize))) goto failed;
214     memcpy(df, &c_dfDIMouse2, c_dfDIMouse2.dwSize);
215     if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
216     memcpy(df->rgodf, c_dfDIMouse2.rgodf, df->dwNumObjs * df->dwObjSize);
217
218     /* Because we don't do any detection yet just modify instance and type */
219     for (i = 0; i < df->dwNumObjs; i++)
220         if (DIDFT_GETTYPE(df->rgodf[i].dwType) & DIDFT_AXIS)
221             df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_RELAXIS;
222         else
223             df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
224
225     newDevice->base.data_format.wine_df = df;
226     IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
227     return newDevice;
228
229 failed:
230     if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
231     HeapFree(GetProcessHeap(), 0, df);
232     HeapFree(GetProcessHeap(), 0, newDevice);
233     return NULL;
234 }
235
236 static HRESULT mousedev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
237 {
238     if ((IsEqualGUID(&GUID_SysMouse,rguid)) ||             /* Generic Mouse */
239         (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
240         if ((riid == NULL) ||
241             IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
242             IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
243             IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
244             IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
245             *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysMouseAvt, dinput);
246             TRACE("Creating a Mouse device (%p)\n", *pdev);
247             if (!*pdev) return DIERR_OUTOFMEMORY;
248             return DI_OK;
249         } else
250             return DIERR_NOINTERFACE;
251     }
252     
253     return DIERR_DEVICENOTREG;
254 }
255
256 static HRESULT mousedev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
257 {
258     if ((IsEqualGUID(&GUID_SysMouse,rguid)) ||             /* Generic Mouse */
259         (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
260         if ((riid == NULL) ||
261             IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
262             IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
263             IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
264             IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
265             *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysMouseWvt, dinput);
266             TRACE("Creating a Mouse device (%p)\n", *pdev);
267             if (!*pdev) return DIERR_OUTOFMEMORY;
268             return DI_OK;
269         } else
270             return DIERR_NOINTERFACE;
271     }
272     
273     return DIERR_DEVICENOTREG;
274 }
275
276 const struct dinput_device mouse_device = {
277     "Wine mouse driver",
278     mousedev_enum_deviceA,
279     mousedev_enum_deviceW,
280     mousedev_create_deviceA,
281     mousedev_create_deviceW
282 };
283
284 /******************************************************************************
285  *      SysMouseA (DInput Mouse support)
286  */
287
288 /* low-level mouse hook */
289 static int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam )
290 {
291     MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
292     SysMouseImpl* This = (SysMouseImpl*) iface;
293     DWORD dwCoop;
294     int wdata = 0, inst_id = -1, ret;
295
296     TRACE("msg %lx @ (%d %d)\n", wparam, hook->pt.x, hook->pt.y);
297
298     EnterCriticalSection(&This->base.crit);
299     dwCoop = This->base.dwCoopLevel;
300     ret = dwCoop & DISCL_EXCLUSIVE;
301
302     switch(wparam) {
303         case WM_MOUSEMOVE:
304         {
305             POINT pt, pt1;
306
307             GetCursorPos(&pt);
308             This->m_state.lX += pt.x = hook->pt.x - pt.x;
309             This->m_state.lY += pt.y = hook->pt.y - pt.y;
310
311             if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
312             {
313                 pt1.x = This->m_state.lX;
314                 pt1.y = This->m_state.lY;
315             } else
316                 pt1 = pt;
317
318             if (pt.x)
319             {
320                 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
321                 wdata = pt1.x;
322             }
323             if (pt.y)
324             {
325                 /* Already have X, need to queue it */
326                 if (inst_id != -1)
327                     queue_event((LPDIRECTINPUTDEVICE8A)This, id_to_offset(&This->base.data_format, inst_id),
328                                 wdata, GetCurrentTime(), This->base.dinput->evsequence);
329                 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
330                 wdata = pt1.y;
331             }
332
333             This->need_warp = This->warp_override != WARP_DISABLE &&
334                               (pt.x || pt.y) &&
335                               (dwCoop & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON);
336             break;
337         }
338         case WM_MOUSEWHEEL:
339             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
340             This->m_state.lZ += wdata = (short)HIWORD(hook->mouseData);
341             break;
342         case WM_LBUTTONDOWN:
343             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
344             This->m_state.rgbButtons[0] = wdata = 0x80;
345             break;
346         case WM_LBUTTONUP:
347             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
348             This->m_state.rgbButtons[0] = wdata = 0x00;
349             break;
350         case WM_RBUTTONDOWN:
351             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
352             This->m_state.rgbButtons[1] = wdata = 0x80;
353             break;
354         case WM_RBUTTONUP:
355             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
356             This->m_state.rgbButtons[1] = wdata = 0x00;
357             break;
358         case WM_MBUTTONDOWN:
359             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
360             This->m_state.rgbButtons[2] = wdata = 0x80;
361             break;
362         case WM_MBUTTONUP:
363             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
364             This->m_state.rgbButtons[2] = wdata = 0x00;
365             break;
366         case WM_XBUTTONDOWN:
367             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
368             This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x80;
369             break;
370         case WM_XBUTTONUP:
371             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
372             This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x00;
373             break;
374         default:
375             ret = 0;
376     }
377
378
379     if (inst_id != -1)
380     {
381         _dump_mouse_state(&This->m_state);
382         queue_event((LPDIRECTINPUTDEVICE8A)This, id_to_offset(&This->base.data_format, inst_id),
383                     wdata, GetCurrentTime(), This->base.dinput->evsequence++);
384     }
385
386     LeaveCriticalSection(&This->base.crit);
387     return ret;
388 }
389
390 static BOOL dinput_window_check(SysMouseImpl* This) {
391     RECT rect;
392     DWORD centerX, centerY;
393
394     /* make sure the window hasn't moved */
395     if(!GetWindowRect(This->base.win, &rect))
396         return FALSE;
397     centerX = (rect.right  - rect.left) / 2;
398     centerY = (rect.bottom - rect.top ) / 2;
399     if (This->win_centerX != centerX || This->win_centerY != centerY) {
400         This->win_centerX = centerX;
401         This->win_centerY = centerY;
402     }
403     This->mapped_center.x = This->win_centerX;
404     This->mapped_center.y = This->win_centerY;
405     MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
406     return TRUE;
407 }
408
409
410 /******************************************************************************
411   *     Acquire : gets exclusive control of the mouse
412   */
413 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
414 {
415     SysMouseImpl *This = (SysMouseImpl *)iface;
416     RECT  rect;
417     POINT point;
418     HRESULT res;
419     
420     TRACE("(this=%p)\n",This);
421
422     if ((res = IDirectInputDevice2AImpl_Acquire(iface)) != DI_OK) return res;
423
424     /* Init the mouse state */
425     GetCursorPos( &point );
426     if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
427     {
428       This->m_state.lX = point.x;
429       This->m_state.lY = point.y;
430     } else {
431       This->m_state.lX = 0;
432       This->m_state.lY = 0;
433       This->org_coords = point;
434     }
435     This->m_state.lZ = 0;
436     This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
437     This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
438     This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
439     
440     /* Install our mouse hook */
441     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
442     {
443       RECT rc;
444
445       ShowCursor(FALSE); /* hide cursor */
446       if (GetWindowRect(This->base.win, &rc))
447       {
448         FIXME("Clipping cursor to %s\n", wine_dbgstr_rect( &rc ));
449         ClipCursor(&rc);
450       }
451       else
452         ERR("Failed to get RECT: %d\n", GetLastError());
453     }
454
455     /* Need a window to warp mouse in. */
456     if (This->warp_override == WARP_FORCE_ON && !This->base.win)
457         This->base.win = GetDesktopWindow();
458
459     /* Get the window dimension and find the center */
460     GetWindowRect(This->base.win, &rect);
461     This->win_centerX = (rect.right  - rect.left) / 2;
462     This->win_centerY = (rect.bottom - rect.top ) / 2;
463
464     /* Warp the mouse to the center of the window */
465     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
466     {
467       This->mapped_center.x = This->win_centerX;
468       This->mapped_center.y = This->win_centerY;
469       MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
470       TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
471       SetCursorPos( This->mapped_center.x, This->mapped_center.y );
472       This->last_warped = GetCurrentTime();
473
474       This->need_warp = FALSE;
475     }
476
477     return DI_OK;
478 }
479
480 /******************************************************************************
481   *     Unacquire : frees the mouse
482   */
483 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
484 {
485     SysMouseImpl *This = (SysMouseImpl *)iface;
486     HRESULT res;
487     
488     TRACE("(this=%p)\n",This);
489
490     if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
491
492     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
493     {
494         ClipCursor(NULL);
495         ShowCursor(TRUE); /* show cursor */
496     }
497
498     /* And put the mouse cursor back where it was at acquire time */
499     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
500     {
501       TRACE(" warping mouse back to (%d , %d)\n", This->org_coords.x, This->org_coords.y);
502       SetCursorPos(This->org_coords.x, This->org_coords.y);
503     }
504         
505     return DI_OK;
506 }
507
508 /******************************************************************************
509   *     GetDeviceState : returns the "state" of the mouse.
510   *
511   *   For the moment, only the "standard" return structure (DIMOUSESTATE) is
512   *   supported.
513   */
514 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(
515         LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
516 ) {
517     SysMouseImpl *This = (SysMouseImpl *)iface;
518
519     if(This->base.acquired == 0) return DIERR_NOTACQUIRED;
520
521     TRACE("(this=%p,0x%08x,%p):\n", This, len, ptr);
522     _dump_mouse_state(&This->m_state);
523
524     EnterCriticalSection(&This->base.crit);
525     /* Copy the current mouse state */
526     fill_DataFormat(ptr, len, &This->m_state, &This->base.data_format);
527
528     /* Initialize the buffer when in relative mode */
529     if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
530     {
531         This->m_state.lX = 0;
532         This->m_state.lY = 0;
533         This->m_state.lZ = 0;
534     }
535     LeaveCriticalSection(&This->base.crit);
536
537     /* Check if we need to do a mouse warping */
538     if (This->need_warp && (GetCurrentTime() - This->last_warped > 10))
539     {
540         if(!dinput_window_check(This))
541             return DIERR_GENERIC;
542         TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
543         SetCursorPos( This->mapped_center.x, This->mapped_center.y );
544         This->last_warped = GetCurrentTime();
545
546         This->need_warp = FALSE;
547     }
548     
549     return DI_OK;
550 }
551
552 /******************************************************************************
553   *     GetDeviceData : gets buffered input data.
554   */
555 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
556         DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
557 {
558     SysMouseImpl *This = (SysMouseImpl *)iface;
559     HRESULT res;
560
561     res = IDirectInputDevice2AImpl_GetDeviceData(iface, dodsize, dod, entries, flags);
562     if (FAILED(res)) return res;
563
564     /* Check if we need to do a mouse warping */
565     if (This->need_warp && (GetCurrentTime() - This->last_warped > 10))
566     {
567         if(!dinput_window_check(This))
568             return DIERR_GENERIC;
569         TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
570         SetCursorPos( This->mapped_center.x, This->mapped_center.y );
571         This->last_warped = GetCurrentTime();
572
573         This->need_warp = FALSE;
574     }
575     return res;
576 }
577
578 /******************************************************************************
579   *     GetProperty : get input device properties
580   */
581 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
582                                                 REFGUID rguid,
583                                                 LPDIPROPHEADER pdiph)
584 {
585     SysMouseImpl *This = (SysMouseImpl *)iface;
586     
587     TRACE("(%p) %s,%p\n", This, debugstr_guid(rguid), pdiph);
588     _dump_DIPROPHEADER(pdiph);
589     
590     if (!HIWORD(rguid)) {
591         switch (LOWORD(rguid)) {
592             case (DWORD_PTR) DIPROP_GRANULARITY: {
593                 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
594                 
595                 /* We'll just assume that the app asks about the Z axis */
596                 pr->dwData = WHEEL_DELTA;
597                 
598                 break;
599             }
600               
601             case (DWORD_PTR) DIPROP_RANGE: {
602                 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
603                 
604                 if ((pdiph->dwHow == DIPH_BYID) &&
605                     ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
606                      (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
607                     /* Querying the range of either the X or the Y axis.  As I do
608                        not know the range, do as if the range were
609                        unrestricted...*/
610                     pr->lMin = DIPROPRANGE_NOMIN;
611                     pr->lMax = DIPROPRANGE_NOMAX;
612                 }
613                 
614                 break;
615             }
616
617             default:
618                 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
619         }
620     }
621     
622     return DI_OK;
623 }
624
625 /******************************************************************************
626   *     GetCapabilities : get the device capabilities
627   */
628 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(
629         LPDIRECTINPUTDEVICE8A iface,
630         LPDIDEVCAPS lpDIDevCaps)
631 {
632     SysMouseImpl *This = (SysMouseImpl *)iface;
633     DIDEVCAPS devcaps;
634
635     TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
636
637     if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
638         WARN("invalid parameter\n");
639         return DIERR_INVALIDPARAM;
640     }
641
642     devcaps.dwSize = lpDIDevCaps->dwSize;
643     devcaps.dwFlags = DIDC_ATTACHED;
644     if (This->base.dinput->dwVersion >= 0x0800)
645         devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
646     else
647         devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
648     devcaps.dwAxes = 3;
649     devcaps.dwButtons = 8;
650     devcaps.dwPOVs = 0;
651     devcaps.dwFFSamplePeriod = 0;
652     devcaps.dwFFMinTimeResolution = 0;
653     devcaps.dwFirmwareRevision = 100;
654     devcaps.dwHardwareRevision = 100;
655     devcaps.dwFFDriverVersion = 0;
656
657     memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
658     
659     return DI_OK;
660 }
661
662 /******************************************************************************
663   *     GetObjectInfo : get information about a device object such as a button
664   *                     or axis
665   */
666 static HRESULT WINAPI SysMouseWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
667         LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
668 {
669     static const WCHAR x_axisW[] = {'X','-','A','x','i','s',0};
670     static const WCHAR y_axisW[] = {'Y','-','A','x','i','s',0};
671     static const WCHAR wheelW[] = {'W','h','e','e','l',0};
672     static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
673     HRESULT res;
674
675     res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
676     if (res != DI_OK) return res;
677
678     if      (IsEqualGUID(&pdidoi->guidType, &GUID_XAxis)) strcpyW(pdidoi->tszName, x_axisW);
679     else if (IsEqualGUID(&pdidoi->guidType, &GUID_YAxis)) strcpyW(pdidoi->tszName, y_axisW);
680     else if (IsEqualGUID(&pdidoi->guidType, &GUID_ZAxis)) strcpyW(pdidoi->tszName, wheelW);
681     else if (pdidoi->dwType & DIDFT_BUTTON)
682         wsprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType) - 3);
683
684     _dump_OBJECTINSTANCEW(pdidoi);
685     return res;
686 }
687
688 static HRESULT WINAPI SysMouseAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
689         LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
690 {
691     HRESULT res;
692     DIDEVICEOBJECTINSTANCEW didoiW;
693     DWORD dwSize = pdidoi->dwSize;
694
695     didoiW.dwSize = sizeof(didoiW);
696     res = SysMouseWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
697     if (res != DI_OK) return res;
698
699     memset(pdidoi, 0, pdidoi->dwSize);
700     memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
701     pdidoi->dwSize = dwSize;
702     WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
703                         sizeof(pdidoi->tszName), NULL, NULL);
704
705     return res;
706 }
707
708 /******************************************************************************
709   *     GetDeviceInfo : get information about a device's identity
710   */
711 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
712         LPDIRECTINPUTDEVICE8A iface,
713         LPDIDEVICEINSTANCEA pdidi)
714 {
715     SysMouseImpl *This = (SysMouseImpl *)iface;
716     TRACE("(this=%p,%p)\n", This, pdidi);
717
718     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
719         WARN(" dinput3 not supporte yet...\n");
720         return DI_OK;
721     }
722
723     fill_mouse_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
724     
725     return DI_OK;
726 }
727
728 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
729 {
730     SysMouseImpl *This = (SysMouseImpl *)iface;
731     TRACE("(this=%p,%p)\n", This, pdidi);
732
733     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
734         WARN(" dinput3 not supporte yet...\n");
735         return DI_OK;
736     }
737
738     fill_mouse_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
739     
740     return DI_OK;
741 }
742
743
744 static const IDirectInputDevice8AVtbl SysMouseAvt =
745 {
746     IDirectInputDevice2AImpl_QueryInterface,
747     IDirectInputDevice2AImpl_AddRef,
748     IDirectInputDevice2AImpl_Release,
749     SysMouseAImpl_GetCapabilities,
750     IDirectInputDevice2AImpl_EnumObjects,
751     SysMouseAImpl_GetProperty,
752     IDirectInputDevice2AImpl_SetProperty,
753     SysMouseAImpl_Acquire,
754     SysMouseAImpl_Unacquire,
755     SysMouseAImpl_GetDeviceState,
756     SysMouseAImpl_GetDeviceData,
757     IDirectInputDevice2AImpl_SetDataFormat,
758     IDirectInputDevice2AImpl_SetEventNotification,
759     IDirectInputDevice2AImpl_SetCooperativeLevel,
760     SysMouseAImpl_GetObjectInfo,
761     SysMouseAImpl_GetDeviceInfo,
762     IDirectInputDevice2AImpl_RunControlPanel,
763     IDirectInputDevice2AImpl_Initialize,
764     IDirectInputDevice2AImpl_CreateEffect,
765     IDirectInputDevice2AImpl_EnumEffects,
766     IDirectInputDevice2AImpl_GetEffectInfo,
767     IDirectInputDevice2AImpl_GetForceFeedbackState,
768     IDirectInputDevice2AImpl_SendForceFeedbackCommand,
769     IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
770     IDirectInputDevice2AImpl_Escape,
771     IDirectInputDevice2AImpl_Poll,
772     IDirectInputDevice2AImpl_SendDeviceData,
773     IDirectInputDevice7AImpl_EnumEffectsInFile,
774     IDirectInputDevice7AImpl_WriteEffectToFile,
775     IDirectInputDevice8AImpl_BuildActionMap,
776     IDirectInputDevice8AImpl_SetActionMap,
777     IDirectInputDevice8AImpl_GetImageInfo
778 };
779
780 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
781 # define XCAST(fun)     (typeof(SysMouseWvt.fun))
782 #else
783 # define XCAST(fun)     (void*)
784 #endif
785
786 static const IDirectInputDevice8WVtbl SysMouseWvt =
787 {
788     IDirectInputDevice2WImpl_QueryInterface,
789     XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
790     XCAST(Release)IDirectInputDevice2AImpl_Release,
791     XCAST(GetCapabilities)SysMouseAImpl_GetCapabilities,
792     IDirectInputDevice2WImpl_EnumObjects,
793     XCAST(GetProperty)SysMouseAImpl_GetProperty,
794     XCAST(SetProperty)IDirectInputDevice2AImpl_SetProperty,
795     XCAST(Acquire)SysMouseAImpl_Acquire,
796     XCAST(Unacquire)SysMouseAImpl_Unacquire,
797     XCAST(GetDeviceState)SysMouseAImpl_GetDeviceState,
798     XCAST(GetDeviceData)SysMouseAImpl_GetDeviceData,
799     XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
800     XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
801     XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
802     SysMouseWImpl_GetObjectInfo,
803     SysMouseWImpl_GetDeviceInfo,
804     XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
805     XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
806     XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
807     IDirectInputDevice2WImpl_EnumEffects,
808     IDirectInputDevice2WImpl_GetEffectInfo,
809     XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
810     XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
811     XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
812     XCAST(Escape)IDirectInputDevice2AImpl_Escape,
813     XCAST(Poll)IDirectInputDevice2AImpl_Poll,
814     XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
815     IDirectInputDevice7WImpl_EnumEffectsInFile,
816     IDirectInputDevice7WImpl_WriteEffectToFile,
817     IDirectInputDevice8WImpl_BuildActionMap,
818     IDirectInputDevice8WImpl_SetActionMap,
819     IDirectInputDevice8WImpl_GetImageInfo
820 };
821 #undef XCAST