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