d3dxof: Move X files parsing into a separate file.
[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 void 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 void 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;
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
301     switch(wparam) {
302         case WM_MOUSEMOVE:
303         {
304             POINT pt, pt1;
305
306             GetCursorPos(&pt);
307             This->m_state.lX += pt.x = hook->pt.x - pt.x;
308             This->m_state.lY += pt.y = hook->pt.y - pt.y;
309
310             if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
311             {
312                 pt1.x = This->m_state.lX;
313                 pt1.y = This->m_state.lY;
314             } else
315                 pt1 = pt;
316
317             if (pt.x)
318             {
319                 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
320                 wdata = pt1.x;
321             }
322             if (pt.y)
323             {
324                 /* Already have X, need to queue it */
325                 if (inst_id != -1)
326                     queue_event((LPDIRECTINPUTDEVICE8A)This, id_to_offset(&This->base.data_format, inst_id),
327                                 wdata, GetCurrentTime(), This->base.dinput->evsequence);
328                 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
329                 wdata = pt1.y;
330             }
331
332             This->need_warp = This->warp_override != WARP_DISABLE &&
333                               (pt.x || pt.y) &&
334                               (dwCoop & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON);
335             break;
336         }
337         case WM_MOUSEWHEEL:
338             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
339             This->m_state.lZ += wdata = (short)HIWORD(hook->mouseData);
340             break;
341         case WM_LBUTTONDOWN:
342             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
343             This->m_state.rgbButtons[0] = wdata = 0x80;
344             break;
345         case WM_LBUTTONUP:
346             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
347             This->m_state.rgbButtons[0] = wdata = 0x00;
348             break;
349         case WM_RBUTTONDOWN:
350             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
351             This->m_state.rgbButtons[1] = wdata = 0x80;
352             break;
353         case WM_RBUTTONUP:
354             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
355             This->m_state.rgbButtons[1] = wdata = 0x00;
356             break;
357         case WM_MBUTTONDOWN:
358             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
359             This->m_state.rgbButtons[2] = wdata = 0x80;
360             break;
361         case WM_MBUTTONUP:
362             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
363             This->m_state.rgbButtons[2] = wdata = 0x00;
364             break;
365         case WM_XBUTTONDOWN:
366             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
367             This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x80;
368             break;
369         case WM_XBUTTONUP:
370             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
371             This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x00;
372             break;
373     }
374
375
376     if (inst_id != -1)
377     {
378         _dump_mouse_state(&This->m_state);
379         queue_event((LPDIRECTINPUTDEVICE8A)This, id_to_offset(&This->base.data_format, inst_id),
380                     wdata, GetCurrentTime(), This->base.dinput->evsequence++);
381     }
382
383     LeaveCriticalSection(&This->base.crit);
384 }
385
386 static BOOL dinput_window_check(SysMouseImpl* This) {
387     RECT rect;
388     DWORD centerX, centerY;
389
390     /* make sure the window hasn't moved */
391     if(!GetWindowRect(This->base.win, &rect))
392         return FALSE;
393     centerX = (rect.right  - rect.left) / 2;
394     centerY = (rect.bottom - rect.top ) / 2;
395     if (This->win_centerX != centerX || This->win_centerY != centerY) {
396         This->win_centerX = centerX;
397         This->win_centerY = centerY;
398     }
399     This->mapped_center.x = This->win_centerX;
400     This->mapped_center.y = This->win_centerY;
401     MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
402     return TRUE;
403 }
404
405
406 /******************************************************************************
407   *     Acquire : gets exclusive control of the mouse
408   */
409 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
410 {
411     SysMouseImpl *This = (SysMouseImpl *)iface;
412     RECT  rect;
413     POINT point;
414     HRESULT res;
415     
416     TRACE("(this=%p)\n",This);
417
418     if ((res = IDirectInputDevice2AImpl_Acquire(iface)) != DI_OK) return res;
419
420     /* Init the mouse state */
421     GetCursorPos( &point );
422     if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
423     {
424       This->m_state.lX = point.x;
425       This->m_state.lY = point.y;
426     } else {
427       This->m_state.lX = 0;
428       This->m_state.lY = 0;
429       This->org_coords = point;
430     }
431     This->m_state.lZ = 0;
432     This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
433     This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
434     This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
435     
436     /* Install our mouse hook */
437     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
438     {
439       RECT rc;
440
441       ShowCursor(FALSE); /* hide cursor */
442       if (GetWindowRect(This->base.win, &rc))
443       {
444         FIXME("Clipping cursor to %s\n", wine_dbgstr_rect( &rc ));
445         ClipCursor(&rc);
446       }
447       else
448         ERR("Failed to get RECT: %d\n", GetLastError());
449     }
450
451     /* Need a window to warp mouse in. */
452     if (This->warp_override == WARP_FORCE_ON && !This->base.win)
453         This->base.win = GetDesktopWindow();
454
455     /* Get the window dimension and find the center */
456     GetWindowRect(This->base.win, &rect);
457     This->win_centerX = (rect.right  - rect.left) / 2;
458     This->win_centerY = (rect.bottom - rect.top ) / 2;
459
460     /* Warp the mouse to the center of the window */
461     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
462     {
463       This->mapped_center.x = This->win_centerX;
464       This->mapped_center.y = This->win_centerY;
465       MapWindowPoints(This->base.win, HWND_DESKTOP, &This->mapped_center, 1);
466       TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
467       SetCursorPos( This->mapped_center.x, This->mapped_center.y );
468       This->last_warped = GetCurrentTime();
469
470       This->need_warp = FALSE;
471     }
472
473     return DI_OK;
474 }
475
476 /******************************************************************************
477   *     Unacquire : frees the mouse
478   */
479 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
480 {
481     SysMouseImpl *This = (SysMouseImpl *)iface;
482     HRESULT res;
483     
484     TRACE("(this=%p)\n",This);
485
486     if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
487
488     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
489     {
490         ClipCursor(NULL);
491         ShowCursor(TRUE); /* show cursor */
492     }
493
494     /* And put the mouse cursor back where it was at acquire time */
495     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
496     {
497       TRACE(" warping mouse back to (%d , %d)\n", This->org_coords.x, This->org_coords.y);
498       SetCursorPos(This->org_coords.x, This->org_coords.y);
499     }
500         
501     return DI_OK;
502 }
503
504 /******************************************************************************
505   *     GetDeviceState : returns the "state" of the mouse.
506   *
507   *   For the moment, only the "standard" return structure (DIMOUSESTATE) is
508   *   supported.
509   */
510 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(
511         LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
512 ) {
513     SysMouseImpl *This = (SysMouseImpl *)iface;
514
515     if(This->base.acquired == 0) return DIERR_NOTACQUIRED;
516
517     TRACE("(this=%p,0x%08x,%p):\n", This, len, ptr);
518     _dump_mouse_state(&This->m_state);
519
520     EnterCriticalSection(&This->base.crit);
521     /* Copy the current mouse state */
522     fill_DataFormat(ptr, len, &This->m_state, &This->base.data_format);
523
524     /* Initialize the buffer when in relative mode */
525     if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
526     {
527         This->m_state.lX = 0;
528         This->m_state.lY = 0;
529         This->m_state.lZ = 0;
530     }
531     LeaveCriticalSection(&This->base.crit);
532
533     /* Check if we need to do a mouse warping */
534     if (This->need_warp && (GetCurrentTime() - This->last_warped > 10))
535     {
536         if(!dinput_window_check(This))
537             return DIERR_GENERIC;
538         TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
539         SetCursorPos( This->mapped_center.x, This->mapped_center.y );
540         This->last_warped = GetCurrentTime();
541
542         This->need_warp = FALSE;
543     }
544     
545     return DI_OK;
546 }
547
548 /******************************************************************************
549   *     GetDeviceData : gets buffered input data.
550   */
551 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
552         DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
553 {
554     SysMouseImpl *This = (SysMouseImpl *)iface;
555     HRESULT res;
556
557     res = IDirectInputDevice2AImpl_GetDeviceData(iface, dodsize, dod, entries, flags);
558     if (FAILED(res)) return res;
559
560     /* Check if we need to do a mouse warping */
561     if (This->need_warp && (GetCurrentTime() - This->last_warped > 10))
562     {
563         if(!dinput_window_check(This))
564             return DIERR_GENERIC;
565         TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
566         SetCursorPos( This->mapped_center.x, This->mapped_center.y );
567         This->last_warped = GetCurrentTime();
568
569         This->need_warp = FALSE;
570     }
571     return res;
572 }
573
574 /******************************************************************************
575   *     GetProperty : get input device properties
576   */
577 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
578                                                 REFGUID rguid,
579                                                 LPDIPROPHEADER pdiph)
580 {
581     SysMouseImpl *This = (SysMouseImpl *)iface;
582     
583     TRACE("(%p) %s,%p\n", This, debugstr_guid(rguid), pdiph);
584     _dump_DIPROPHEADER(pdiph);
585     
586     if (!HIWORD(rguid)) {
587         switch (LOWORD(rguid)) {
588             case (DWORD) DIPROP_GRANULARITY: {
589                 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
590                 
591                 /* We'll just assume that the app asks about the Z axis */
592                 pr->dwData = WHEEL_DELTA;
593                 
594                 break;
595             }
596               
597             case (DWORD) DIPROP_RANGE: {
598                 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
599                 
600                 if ((pdiph->dwHow == DIPH_BYID) &&
601                     ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
602                      (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
603                     /* Querying the range of either the X or the Y axis.  As I do
604                        not know the range, do as if the range were
605                        unrestricted...*/
606                     pr->lMin = DIPROPRANGE_NOMIN;
607                     pr->lMax = DIPROPRANGE_NOMAX;
608                 }
609                 
610                 break;
611             }
612
613             default:
614                 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
615         }
616     }
617     
618     return DI_OK;
619 }
620
621 /******************************************************************************
622   *     GetCapabilities : get the device capabilities
623   */
624 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(
625         LPDIRECTINPUTDEVICE8A iface,
626         LPDIDEVCAPS lpDIDevCaps)
627 {
628     SysMouseImpl *This = (SysMouseImpl *)iface;
629     DIDEVCAPS devcaps;
630
631     TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
632
633     if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
634         WARN("invalid parameter\n");
635         return DIERR_INVALIDPARAM;
636     }
637
638     devcaps.dwSize = lpDIDevCaps->dwSize;
639     devcaps.dwFlags = DIDC_ATTACHED;
640     if (This->base.dinput->dwVersion >= 0x0800)
641         devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
642     else
643         devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
644     devcaps.dwAxes = 3;
645     devcaps.dwButtons = 8;
646     devcaps.dwPOVs = 0;
647     devcaps.dwFFSamplePeriod = 0;
648     devcaps.dwFFMinTimeResolution = 0;
649     devcaps.dwFirmwareRevision = 100;
650     devcaps.dwHardwareRevision = 100;
651     devcaps.dwFFDriverVersion = 0;
652
653     memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
654     
655     return DI_OK;
656 }
657
658 /******************************************************************************
659   *     GetObjectInfo : get information about a device object such as a button
660   *                     or axis
661   */
662 static HRESULT WINAPI SysMouseWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
663         LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
664 {
665     static const WCHAR x_axisW[] = {'X','-','A','x','i','s',0};
666     static const WCHAR y_axisW[] = {'Y','-','A','x','i','s',0};
667     static const WCHAR wheelW[] = {'W','h','e','e','l',0};
668     static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
669     HRESULT res;
670
671     res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
672     if (res != DI_OK) return res;
673
674     if      (IsEqualGUID(&pdidoi->guidType, &GUID_XAxis)) strcpyW(pdidoi->tszName, x_axisW);
675     else if (IsEqualGUID(&pdidoi->guidType, &GUID_YAxis)) strcpyW(pdidoi->tszName, y_axisW);
676     else if (IsEqualGUID(&pdidoi->guidType, &GUID_ZAxis)) strcpyW(pdidoi->tszName, wheelW);
677     else if (pdidoi->dwType & DIDFT_BUTTON)
678         wsprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType) - 3);
679
680     _dump_OBJECTINSTANCEW(pdidoi);
681     return res;
682 }
683
684 static HRESULT WINAPI SysMouseAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
685         LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
686 {
687     HRESULT res;
688     DIDEVICEOBJECTINSTANCEW didoiW;
689     DWORD dwSize = pdidoi->dwSize;
690
691     didoiW.dwSize = sizeof(didoiW);
692     res = SysMouseWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
693     if (res != DI_OK) return res;
694
695     memset(pdidoi, 0, pdidoi->dwSize);
696     memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
697     pdidoi->dwSize = dwSize;
698     WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
699                         sizeof(pdidoi->tszName), NULL, NULL);
700
701     return res;
702 }
703
704 /******************************************************************************
705   *     GetDeviceInfo : get information about a device's identity
706   */
707 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
708         LPDIRECTINPUTDEVICE8A iface,
709         LPDIDEVICEINSTANCEA pdidi)
710 {
711     SysMouseImpl *This = (SysMouseImpl *)iface;
712     TRACE("(this=%p,%p)\n", This, pdidi);
713
714     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
715         WARN(" dinput3 not supporte yet...\n");
716         return DI_OK;
717     }
718
719     fill_mouse_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
720     
721     return DI_OK;
722 }
723
724 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
725 {
726     SysMouseImpl *This = (SysMouseImpl *)iface;
727     TRACE("(this=%p,%p)\n", This, pdidi);
728
729     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
730         WARN(" dinput3 not supporte yet...\n");
731         return DI_OK;
732     }
733
734     fill_mouse_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
735     
736     return DI_OK;
737 }
738
739
740 static const IDirectInputDevice8AVtbl SysMouseAvt =
741 {
742     IDirectInputDevice2AImpl_QueryInterface,
743     IDirectInputDevice2AImpl_AddRef,
744     IDirectInputDevice2AImpl_Release,
745     SysMouseAImpl_GetCapabilities,
746     IDirectInputDevice2AImpl_EnumObjects,
747     SysMouseAImpl_GetProperty,
748     IDirectInputDevice2AImpl_SetProperty,
749     SysMouseAImpl_Acquire,
750     SysMouseAImpl_Unacquire,
751     SysMouseAImpl_GetDeviceState,
752     SysMouseAImpl_GetDeviceData,
753     IDirectInputDevice2AImpl_SetDataFormat,
754     IDirectInputDevice2AImpl_SetEventNotification,
755     IDirectInputDevice2AImpl_SetCooperativeLevel,
756     SysMouseAImpl_GetObjectInfo,
757     SysMouseAImpl_GetDeviceInfo,
758     IDirectInputDevice2AImpl_RunControlPanel,
759     IDirectInputDevice2AImpl_Initialize,
760     IDirectInputDevice2AImpl_CreateEffect,
761     IDirectInputDevice2AImpl_EnumEffects,
762     IDirectInputDevice2AImpl_GetEffectInfo,
763     IDirectInputDevice2AImpl_GetForceFeedbackState,
764     IDirectInputDevice2AImpl_SendForceFeedbackCommand,
765     IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
766     IDirectInputDevice2AImpl_Escape,
767     IDirectInputDevice2AImpl_Poll,
768     IDirectInputDevice2AImpl_SendDeviceData,
769     IDirectInputDevice7AImpl_EnumEffectsInFile,
770     IDirectInputDevice7AImpl_WriteEffectToFile,
771     IDirectInputDevice8AImpl_BuildActionMap,
772     IDirectInputDevice8AImpl_SetActionMap,
773     IDirectInputDevice8AImpl_GetImageInfo
774 };
775
776 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
777 # define XCAST(fun)     (typeof(SysMouseWvt.fun))
778 #else
779 # define XCAST(fun)     (void*)
780 #endif
781
782 static const IDirectInputDevice8WVtbl SysMouseWvt =
783 {
784     IDirectInputDevice2WImpl_QueryInterface,
785     XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
786     XCAST(Release)IDirectInputDevice2AImpl_Release,
787     XCAST(GetCapabilities)SysMouseAImpl_GetCapabilities,
788     IDirectInputDevice2WImpl_EnumObjects,
789     XCAST(GetProperty)SysMouseAImpl_GetProperty,
790     XCAST(SetProperty)IDirectInputDevice2AImpl_SetProperty,
791     XCAST(Acquire)SysMouseAImpl_Acquire,
792     XCAST(Unacquire)SysMouseAImpl_Unacquire,
793     XCAST(GetDeviceState)SysMouseAImpl_GetDeviceState,
794     XCAST(GetDeviceData)SysMouseAImpl_GetDeviceData,
795     XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
796     XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
797     XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
798     SysMouseWImpl_GetObjectInfo,
799     SysMouseWImpl_GetDeviceInfo,
800     XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
801     XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
802     XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
803     IDirectInputDevice2WImpl_EnumEffects,
804     IDirectInputDevice2WImpl_GetEffectInfo,
805     XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
806     XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
807     XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
808     XCAST(Escape)IDirectInputDevice2AImpl_Escape,
809     XCAST(Poll)IDirectInputDevice2AImpl_Poll,
810     XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
811     IDirectInputDevice7WImpl_EnumEffectsInFile,
812     IDirectInputDevice7WImpl_WriteEffectToFile,
813     IDirectInputDevice8WImpl_BuildActionMap,
814     IDirectInputDevice8WImpl_SetActionMap,
815     IDirectInputDevice8WImpl_GetImageInfo
816 };
817 #undef XCAST