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