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