mshtml: Added script 'for' and 'event' attributes tests.
[wine] / dlls / dinput / mouse.c
1 /*              DirectInput Mouse device
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winerror.h"
33 #include "winreg.h"
34 #include "dinput.h"
35
36 #include "dinput_private.h"
37 #include "device_private.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
42
43 /* Wine mouse driver object instances */
44 #define WINE_MOUSE_X_AXIS_INSTANCE   0
45 #define WINE_MOUSE_Y_AXIS_INSTANCE   1
46 #define WINE_MOUSE_Z_AXIS_INSTANCE   2
47 #define WINE_MOUSE_BUTTONS_INSTANCE  3
48
49 static const IDirectInputDevice8AVtbl SysMouseAvt;
50 static const IDirectInputDevice8WVtbl SysMouseWvt;
51
52 typedef struct SysMouseImpl SysMouseImpl;
53
54 typedef enum
55 {
56     WARP_DEFAULT,
57     WARP_DISABLE,
58     WARP_FORCE_ON
59 } WARP_MOUSE;
60
61 struct SysMouseImpl
62 {
63     struct IDirectInputDeviceImpl   base;
64
65     /* SysMouseAImpl */
66     /* These are used in case of relative -> absolute transitions */
67     POINT                           org_coords;
68     BOOL                            clipped;
69     /* warping: whether we need to move mouse back to middle once we
70      * reach window borders (for e.g. shooters, "surface movement" games) */
71     BOOL                            need_warp;
72     DWORD                           last_warped;
73
74     /* This is for mouse reporting. */
75     DIMOUSESTATE2                   m_state;
76
77     WARP_MOUSE                      warp_override;
78 };
79
80 static inline SysMouseImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
81 {
82     return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), SysMouseImpl, base);
83 }
84 static inline SysMouseImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
85 {
86     return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), SysMouseImpl, base);
87 }
88 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(SysMouseImpl *This)
89 {
90     return &This->base.IDirectInputDevice8A_iface;
91 }
92 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(SysMouseImpl *This)
93 {
94     return &This->base.IDirectInputDevice8W_iface;
95 }
96
97 static int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam );
98
99 const GUID DInput_Wine_Mouse_GUID = { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
100     0x9e573ed8, 0x7734, 0x11d2, {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
101 };
102
103 static void _dump_mouse_state(const DIMOUSESTATE2 *m_state)
104 {
105     int i;
106
107     if (!TRACE_ON(dinput)) return;
108
109     TRACE("(X: %d Y: %d Z: %d", m_state->lX, m_state->lY, m_state->lZ);
110     for (i = 0; i < 5; i++) TRACE(" B%d: %02x", i, m_state->rgbButtons[i]);
111     TRACE(")\n");
112 }
113
114 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
115     DWORD dwSize;
116     DIDEVICEINSTANCEA ddi;
117     
118     dwSize = lpddi->dwSize;
119
120     TRACE("%d %p\n", dwSize, lpddi);
121     
122     memset(lpddi, 0, dwSize);
123     memset(&ddi, 0, sizeof(ddi));
124
125     ddi.dwSize = dwSize;
126     ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
127     ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
128     if (version >= 0x0800)
129         ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
130     else
131         ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
132     strcpy(ddi.tszInstanceName, "Mouse");
133     strcpy(ddi.tszProductName, "Wine Mouse");
134
135     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
136 }
137
138 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
139     DWORD dwSize;
140     DIDEVICEINSTANCEW ddi;
141     
142     dwSize = lpddi->dwSize;
143
144     TRACE("%d %p\n", dwSize, lpddi);
145     
146     memset(lpddi, 0, dwSize);
147     memset(&ddi, 0, sizeof(ddi));
148
149     ddi.dwSize = dwSize;
150     ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
151     ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
152     if (version >= 0x0800)
153         ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
154     else
155         ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
156     MultiByteToWideChar(CP_ACP, 0, "Mouse", -1, ddi.tszInstanceName, MAX_PATH);
157     MultiByteToWideChar(CP_ACP, 0, "Wine Mouse", -1, ddi.tszProductName, MAX_PATH);
158
159     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
160 }
161
162 static BOOL mousedev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
163 {
164     if (id != 0)
165         return FALSE;
166
167     if ((dwDevType == 0) ||
168         ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
169         (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
170         TRACE("Enumerating the mouse device\n");
171         
172         fill_mouse_dideviceinstanceA(lpddi, version);
173         
174         return TRUE;
175     }
176     
177     return FALSE;
178 }
179
180 static BOOL mousedev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
181 {
182     if (id != 0)
183         return FALSE;
184
185     if ((dwDevType == 0) ||
186         ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
187         (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
188         TRACE("Enumerating the mouse device\n");
189         
190         fill_mouse_dideviceinstanceW(lpddi, version);
191         
192         return TRUE;
193     }
194     
195     return FALSE;
196 }
197
198 static SysMouseImpl *alloc_device(REFGUID rguid, IDirectInputImpl *dinput)
199 {
200     SysMouseImpl* newDevice;
201     LPDIDATAFORMAT df = NULL;
202     unsigned i;
203     char buffer[20];
204     HKEY hkey, appkey;
205
206     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseImpl));
207     if (!newDevice) return NULL;
208     newDevice->base.IDirectInputDevice8A_iface.lpVtbl = &SysMouseAvt;
209     newDevice->base.IDirectInputDevice8W_iface.lpVtbl = &SysMouseWvt;
210     newDevice->base.ref = 1;
211     newDevice->base.dwCoopLevel = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;
212     newDevice->base.guid = *rguid;
213     InitializeCriticalSection(&newDevice->base.crit);
214     newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysMouseImpl*->base.crit");
215     newDevice->base.dinput = dinput;
216     newDevice->base.event_proc = dinput_mouse_hook;
217
218     get_app_key(&hkey, &appkey);
219     if (!get_config_key(hkey, appkey, "MouseWarpOverride", buffer, sizeof(buffer)))
220     {
221         if (!strcasecmp(buffer, "disable"))
222             newDevice->warp_override = WARP_DISABLE;
223         else if (!strcasecmp(buffer, "force"))
224             newDevice->warp_override = WARP_FORCE_ON;
225     }
226     if (appkey) RegCloseKey(appkey);
227     if (hkey) RegCloseKey(hkey);
228
229     /* Create copy of default data format */
230     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIMouse2.dwSize))) goto failed;
231     memcpy(df, &c_dfDIMouse2, c_dfDIMouse2.dwSize);
232     if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
233     memcpy(df->rgodf, c_dfDIMouse2.rgodf, df->dwNumObjs * df->dwObjSize);
234
235     /* Because we don't do any detection yet just modify instance and type */
236     for (i = 0; i < df->dwNumObjs; i++)
237         if (DIDFT_GETTYPE(df->rgodf[i].dwType) & DIDFT_AXIS)
238             df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_RELAXIS;
239         else
240             df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
241
242     newDevice->base.data_format.wine_df = df;
243     IDirectInput_AddRef(&newDevice->base.dinput->IDirectInput7A_iface);
244
245     EnterCriticalSection(&dinput->crit);
246     list_add_tail(&dinput->devices_list, &newDevice->base.entry);
247     LeaveCriticalSection(&dinput->crit);
248
249     return newDevice;
250
251 failed:
252     if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
253     HeapFree(GetProcessHeap(), 0, df);
254     HeapFree(GetProcessHeap(), 0, newDevice);
255     return NULL;
256 }
257
258 static HRESULT mousedev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
259 {
260     TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
261     *pdev = NULL;
262
263     if (IsEqualGUID(&GUID_SysMouse, rguid) ||        /* Generic Mouse */
264         IsEqualGUID(&DInput_Wine_Mouse_GUID, rguid)) /* Wine Mouse */
265     {
266         SysMouseImpl *This;
267
268         if (riid == NULL)
269             ;/* nothing */
270         else if (IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
271                  IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
272                  IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
273                  IsEqualGUID(&IID_IDirectInputDevice8A, riid))
274         {
275             unicode = 0;
276         }
277         else if (IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
278                  IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
279                  IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
280                  IsEqualGUID(&IID_IDirectInputDevice8W, riid))
281         {
282             unicode = 1;
283         }
284         else
285         {
286             WARN("no interface\n");
287             return DIERR_NOINTERFACE;
288         }
289
290         This = alloc_device(rguid, dinput);
291         TRACE("Created a Mouse device (%p)\n", This);
292
293         if (!This) return DIERR_OUTOFMEMORY;
294
295         if (unicode)
296             *pdev = &This->base.IDirectInputDevice8W_iface;
297         else
298             *pdev = &This->base.IDirectInputDevice8A_iface;
299
300         return DI_OK;
301     }
302
303     return DIERR_DEVICENOTREG;
304 }
305
306 const struct dinput_device mouse_device = {
307     "Wine mouse driver",
308     mousedev_enum_deviceA,
309     mousedev_enum_deviceW,
310     mousedev_create_device
311 };
312
313 /******************************************************************************
314  *      SysMouseA (DInput Mouse support)
315  */
316
317 /* low-level mouse hook */
318 static int dinput_mouse_hook( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam )
319 {
320     MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
321     SysMouseImpl* This = impl_from_IDirectInputDevice8A(iface);
322     int wdata = 0, inst_id = -1, ret = 0;
323
324     TRACE("msg %lx @ (%d %d)\n", wparam, hook->pt.x, hook->pt.y);
325
326     EnterCriticalSection(&This->base.crit);
327
328     switch(wparam) {
329         case WM_MOUSEMOVE:
330         {
331             POINT pt, pt1;
332
333             GetCursorPos(&pt);
334             This->m_state.lX += pt.x = hook->pt.x - pt.x;
335             This->m_state.lY += pt.y = hook->pt.y - pt.y;
336
337             if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
338             {
339                 pt1.x = This->m_state.lX;
340                 pt1.y = This->m_state.lY;
341             } else
342                 pt1 = pt;
343
344             if (pt.x)
345             {
346                 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
347                 wdata = pt1.x;
348             }
349             if (pt.y)
350             {
351                 /* Already have X, need to queue it */
352                 if (inst_id != -1)
353                     queue_event(iface, inst_id,
354                                 wdata, GetCurrentTime(), This->base.dinput->evsequence);
355                 inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
356                 wdata = pt1.y;
357             }
358
359             if (pt.x || pt.y)
360             {
361                 if ((This->warp_override == WARP_FORCE_ON) ||
362                     (This->warp_override != WARP_DISABLE && (This->base.dwCoopLevel & DISCL_EXCLUSIVE)))
363                     This->need_warp = TRUE;
364             }
365             break;
366         }
367         case WM_MOUSEWHEEL:
368             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
369             This->m_state.lZ += wdata = (short)HIWORD(hook->mouseData);
370             /* FarCry crashes if it gets a mouse wheel message */
371             /* FIXME: should probably filter out other messages too */
372             ret = This->clipped;
373             break;
374         case WM_LBUTTONDOWN:
375             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
376             This->m_state.rgbButtons[0] = wdata = 0x80;
377             break;
378         case WM_LBUTTONUP:
379             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 0) | DIDFT_PSHBUTTON;
380             This->m_state.rgbButtons[0] = wdata = 0x00;
381             break;
382         case WM_RBUTTONDOWN:
383             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
384             This->m_state.rgbButtons[1] = wdata = 0x80;
385             break;
386         case WM_RBUTTONUP:
387             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 1) | DIDFT_PSHBUTTON;
388             This->m_state.rgbButtons[1] = wdata = 0x00;
389             break;
390         case WM_MBUTTONDOWN:
391             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
392             This->m_state.rgbButtons[2] = wdata = 0x80;
393             break;
394         case WM_MBUTTONUP:
395             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2) | DIDFT_PSHBUTTON;
396             This->m_state.rgbButtons[2] = wdata = 0x00;
397             break;
398         case WM_XBUTTONDOWN:
399             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
400             This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x80;
401             break;
402         case WM_XBUTTONUP:
403             inst_id = DIDFT_MAKEINSTANCE(WINE_MOUSE_BUTTONS_INSTANCE + 2 + HIWORD(hook->mouseData)) | DIDFT_PSHBUTTON;
404             This->m_state.rgbButtons[2 + HIWORD(hook->mouseData)] = wdata = 0x00;
405             break;
406     }
407
408
409     if (inst_id != -1)
410     {
411         _dump_mouse_state(&This->m_state);
412         queue_event(iface, inst_id,
413                     wdata, GetCurrentTime(), This->base.dinput->evsequence++);
414     }
415
416     LeaveCriticalSection(&This->base.crit);
417     return ret;
418 }
419
420 static void warp_check( SysMouseImpl* This, BOOL force )
421 {
422     DWORD now = GetCurrentTime();
423     const DWORD interval = This->clipped ? 500 : 10;
424
425     if (force || (This->need_warp && (now - This->last_warped > interval)))
426     {
427         RECT rect, new_rect;
428         POINT mapped_center;
429
430         This->last_warped = now;
431         This->need_warp = FALSE;
432         if (!GetClientRect(This->base.win, &rect)) return;
433         MapWindowPoints( This->base.win, 0, (POINT *)&rect, 2 );
434         if (!This->clipped)
435         {
436             mapped_center.x = (rect.left + rect.right) / 2;
437             mapped_center.y = (rect.top + rect.bottom) / 2;
438             TRACE("Warping mouse to %d - %d\n", mapped_center.x, mapped_center.y);
439             SetCursorPos( mapped_center.x, mapped_center.y );
440         }
441         if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
442         {
443             /* make sure we clip even if the window covers the whole screen */
444             rect.left = max( rect.left, GetSystemMetrics( SM_XVIRTUALSCREEN ) + 1 );
445             rect.top = max( rect.top, GetSystemMetrics( SM_YVIRTUALSCREEN ) + 1 );
446             rect.right = min( rect.right, rect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN ) - 2 );
447             rect.bottom = min( rect.bottom, rect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN ) - 2 );
448             TRACE("Clipping mouse to %s\n", wine_dbgstr_rect( &rect ));
449             ClipCursor( &rect );
450             This->clipped = GetClipCursor( &new_rect ) && EqualRect( &rect, &new_rect );
451         }
452     }
453 }
454
455
456 /******************************************************************************
457   *     Acquire : gets exclusive control of the mouse
458   */
459 static HRESULT WINAPI SysMouseWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
460 {
461     SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
462     POINT point;
463     HRESULT res;
464
465     TRACE("(this=%p)\n",This);
466
467     if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK) return res;
468
469     /* Init the mouse state */
470     GetCursorPos( &point );
471     if (This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS)
472     {
473       This->m_state.lX = point.x;
474       This->m_state.lY = point.y;
475     } else {
476       This->m_state.lX = 0;
477       This->m_state.lY = 0;
478       This->org_coords = point;
479     }
480     This->m_state.lZ = 0;
481     This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
482     This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
483     This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
484
485     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
486     {
487         ShowCursor(FALSE); /* hide cursor */
488         warp_check( This, TRUE );
489     }
490     else if (This->warp_override == WARP_FORCE_ON)
491     {
492         /* Need a window to warp mouse in. */
493         if (!This->base.win) This->base.win = GetDesktopWindow();
494         warp_check( This, TRUE );
495     }
496     else if (This->clipped)
497     {
498         ClipCursor( NULL );
499         This->clipped = FALSE;
500     }
501
502     return DI_OK;
503 }
504
505 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
506 {
507     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
508     return SysMouseWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
509 }
510
511 /******************************************************************************
512   *     Unacquire : frees the mouse
513   */
514 static HRESULT WINAPI SysMouseWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
515 {
516     SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
517     HRESULT res;
518
519     TRACE("(this=%p)\n",This);
520
521     if ((res = IDirectInputDevice2WImpl_Unacquire(iface)) != DI_OK) return res;
522
523     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE)
524     {
525         ClipCursor(NULL);
526         ShowCursor(TRUE); /* show cursor */
527         This->clipped = FALSE;
528     }
529
530     /* And put the mouse cursor back where it was at acquire time */
531     if (This->base.dwCoopLevel & DISCL_EXCLUSIVE || This->warp_override == WARP_FORCE_ON)
532     {
533         TRACE("warping mouse back to %s\n", wine_dbgstr_point(&This->org_coords));
534         SetCursorPos(This->org_coords.x, This->org_coords.y);
535     }
536
537     return DI_OK;
538 }
539
540 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
541 {
542     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
543     return SysMouseWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
544 }
545
546 /******************************************************************************
547   *     GetDeviceState : returns the "state" of the mouse.
548   *
549   *   For the moment, only the "standard" return structure (DIMOUSESTATE) is
550   *   supported.
551   */
552 static HRESULT WINAPI SysMouseWImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
553 {
554     SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
555
556     if(This->base.acquired == 0) return DIERR_NOTACQUIRED;
557
558     TRACE("(this=%p,0x%08x,%p):\n", This, len, ptr);
559     _dump_mouse_state(&This->m_state);
560
561     EnterCriticalSection(&This->base.crit);
562     /* Copy the current mouse state */
563     fill_DataFormat(ptr, len, &This->m_state, &This->base.data_format);
564
565     /* Initialize the buffer when in relative mode */
566     if (!(This->base.data_format.user_df->dwFlags & DIDF_ABSAXIS))
567     {
568         This->m_state.lX = 0;
569         This->m_state.lY = 0;
570         This->m_state.lZ = 0;
571     }
572     LeaveCriticalSection(&This->base.crit);
573
574     warp_check( This, FALSE );
575     return DI_OK;
576 }
577
578 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
579 {
580     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
581     return SysMouseWImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
582 }
583
584 /******************************************************************************
585   *     GetDeviceData : gets buffered input data.
586   */
587 static HRESULT WINAPI SysMouseWImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface,
588         DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
589 {
590     SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
591     HRESULT res;
592
593     res = IDirectInputDevice2WImpl_GetDeviceData(iface, dodsize, dod, entries, flags);
594     if (SUCCEEDED(res)) warp_check( This, FALSE );
595     return res;
596 }
597
598 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
599         DWORD dodsize, LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
600 {
601     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
602     return SysMouseWImpl_GetDeviceData(IDirectInputDevice8W_from_impl(This), dodsize, dod, entries, flags);
603 }
604
605 /******************************************************************************
606   *     GetProperty : get input device properties
607   */
608 static HRESULT WINAPI SysMouseWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
609 {
610     SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
611
612     TRACE("(%p) %s,%p\n", This, debugstr_guid(rguid), pdiph);
613     _dump_DIPROPHEADER(pdiph);
614
615     if (IS_DIPROP(rguid)) {
616         switch (LOWORD(rguid)) {
617             case (DWORD_PTR) DIPROP_GRANULARITY: {
618                 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
619                 
620                 /* We'll just assume that the app asks about the Z axis */
621                 pr->dwData = WHEEL_DELTA;
622                 
623                 break;
624             }
625               
626             case (DWORD_PTR) DIPROP_RANGE: {
627                 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
628                 
629                 if ((pdiph->dwHow == DIPH_BYID) &&
630                     ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
631                      (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
632                     /* Querying the range of either the X or the Y axis.  As I do
633                        not know the range, do as if the range were
634                        unrestricted...*/
635                     pr->lMin = DIPROPRANGE_NOMIN;
636                     pr->lMax = DIPROPRANGE_NOMAX;
637                 }
638                 
639                 break;
640             }
641
642             default:
643                 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
644         }
645     }
646
647     return DI_OK;
648 }
649
650 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
651 {
652     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
653     return SysMouseWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
654 }
655
656 /******************************************************************************
657   *     GetCapabilities : get the device capabilities
658   */
659 static HRESULT WINAPI SysMouseWImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
660 {
661     SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
662     DIDEVCAPS devcaps;
663
664     TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
665
666     if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
667         WARN("invalid parameter\n");
668         return DIERR_INVALIDPARAM;
669     }
670
671     devcaps.dwSize = lpDIDevCaps->dwSize;
672     devcaps.dwFlags = DIDC_ATTACHED | DIDC_EMULATED;
673     if (This->base.dinput->dwVersion >= 0x0800)
674         devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
675     else
676         devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
677     devcaps.dwAxes = 3;
678     devcaps.dwButtons = 8;
679     devcaps.dwPOVs = 0;
680     devcaps.dwFFSamplePeriod = 0;
681     devcaps.dwFFMinTimeResolution = 0;
682     devcaps.dwFirmwareRevision = 100;
683     devcaps.dwHardwareRevision = 100;
684     devcaps.dwFFDriverVersion = 0;
685
686     memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
687
688     return DI_OK;
689 }
690
691 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
692 {
693     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
694     return SysMouseWImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
695 }
696
697 /******************************************************************************
698   *     GetObjectInfo : get information about a device object such as a button
699   *                     or axis
700   */
701 static HRESULT WINAPI SysMouseWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
702         LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
703 {
704     static const WCHAR x_axisW[] = {'X','-','A','x','i','s',0};
705     static const WCHAR y_axisW[] = {'Y','-','A','x','i','s',0};
706     static const WCHAR wheelW[] = {'W','h','e','e','l',0};
707     static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
708     HRESULT res;
709
710     res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
711     if (res != DI_OK) return res;
712
713     if      (IsEqualGUID(&pdidoi->guidType, &GUID_XAxis)) strcpyW(pdidoi->tszName, x_axisW);
714     else if (IsEqualGUID(&pdidoi->guidType, &GUID_YAxis)) strcpyW(pdidoi->tszName, y_axisW);
715     else if (IsEqualGUID(&pdidoi->guidType, &GUID_ZAxis)) strcpyW(pdidoi->tszName, wheelW);
716     else if (pdidoi->dwType & DIDFT_BUTTON)
717         wsprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType) - 3);
718
719     _dump_OBJECTINSTANCEW(pdidoi);
720     return res;
721 }
722
723 static HRESULT WINAPI SysMouseAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
724         LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
725 {
726     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
727     HRESULT res;
728     DIDEVICEOBJECTINSTANCEW didoiW;
729     DWORD dwSize = pdidoi->dwSize;
730
731     didoiW.dwSize = sizeof(didoiW);
732     res = SysMouseWImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
733     if (res != DI_OK) return res;
734
735     memset(pdidoi, 0, pdidoi->dwSize);
736     memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
737     pdidoi->dwSize = dwSize;
738     WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
739                         sizeof(pdidoi->tszName), NULL, NULL);
740
741     return res;
742 }
743
744 /******************************************************************************
745   *     GetDeviceInfo : get information about a device's identity
746   */
747 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
748         LPDIRECTINPUTDEVICE8A iface,
749         LPDIDEVICEINSTANCEA pdidi)
750 {
751     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
752     TRACE("(this=%p,%p)\n", This, pdidi);
753
754     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
755         WARN(" dinput3 not supported yet...\n");
756         return DI_OK;
757     }
758
759     fill_mouse_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
760     
761     return DI_OK;
762 }
763
764 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
765 {
766     SysMouseImpl *This = impl_from_IDirectInputDevice8W(iface);
767     TRACE("(this=%p,%p)\n", This, pdidi);
768
769     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
770         WARN(" dinput3 not supported yet...\n");
771         return DI_OK;
772     }
773
774     fill_mouse_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
775     
776     return DI_OK;
777 }
778
779 static HRESULT WINAPI SysMouseWImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
780                                                    LPDIACTIONFORMATW lpdiaf,
781                                                    LPCWSTR lpszUserName,
782                                                    DWORD dwFlags)
783 {
784     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
785
786     return _build_action_map(iface, lpdiaf, lpszUserName, dwFlags, DIMOUSE_MASK, &c_dfDIMouse2);
787 }
788
789 static HRESULT WINAPI SysMouseAImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
790                                                    LPDIACTIONFORMATA lpdiaf,
791                                                    LPCSTR lpszUserName,
792                                                    DWORD dwFlags)
793 {
794     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
795     DIACTIONFORMATW diafW;
796     HRESULT hr;
797     WCHAR *lpszUserNameW = NULL;
798     int username_size;
799
800     diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
801     _copy_diactionformatAtoW(&diafW, lpdiaf);
802
803     if (lpszUserName != NULL)
804     {
805         username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
806         lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
807         MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
808     }
809
810     hr = SysMouseWImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
811
812     _copy_diactionformatWtoA(lpdiaf, &diafW);
813     HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
814     HeapFree(GetProcessHeap(), 0, lpszUserNameW);
815
816     return hr;
817 }
818
819 static HRESULT WINAPI SysMouseWImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
820                                                  LPDIACTIONFORMATW lpdiaf,
821                                                  LPCWSTR lpszUserName,
822                                                  DWORD dwFlags)
823 {
824     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
825
826     return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, &c_dfDIMouse2);
827 }
828
829 static HRESULT WINAPI SysMouseAImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
830                                                  LPDIACTIONFORMATA lpdiaf,
831                                                  LPCSTR lpszUserName,
832                                                  DWORD dwFlags)
833 {
834     SysMouseImpl *This = impl_from_IDirectInputDevice8A(iface);
835     DIACTIONFORMATW diafW;
836     HRESULT hr;
837     WCHAR *lpszUserNameW = NULL;
838     int username_size;
839
840     diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
841     _copy_diactionformatAtoW(&diafW, lpdiaf);
842
843     if (lpszUserName != NULL)
844     {
845         username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
846         lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
847         MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
848     }
849
850     hr = SysMouseWImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
851
852     HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
853     HeapFree(GetProcessHeap(), 0, lpszUserNameW);
854
855     return hr;
856 }
857
858 static const IDirectInputDevice8AVtbl SysMouseAvt =
859 {
860     IDirectInputDevice2AImpl_QueryInterface,
861     IDirectInputDevice2AImpl_AddRef,
862     IDirectInputDevice2AImpl_Release,
863     SysMouseAImpl_GetCapabilities,
864     IDirectInputDevice2AImpl_EnumObjects,
865     SysMouseAImpl_GetProperty,
866     IDirectInputDevice2AImpl_SetProperty,
867     SysMouseAImpl_Acquire,
868     SysMouseAImpl_Unacquire,
869     SysMouseAImpl_GetDeviceState,
870     SysMouseAImpl_GetDeviceData,
871     IDirectInputDevice2AImpl_SetDataFormat,
872     IDirectInputDevice2AImpl_SetEventNotification,
873     IDirectInputDevice2AImpl_SetCooperativeLevel,
874     SysMouseAImpl_GetObjectInfo,
875     SysMouseAImpl_GetDeviceInfo,
876     IDirectInputDevice2AImpl_RunControlPanel,
877     IDirectInputDevice2AImpl_Initialize,
878     IDirectInputDevice2AImpl_CreateEffect,
879     IDirectInputDevice2AImpl_EnumEffects,
880     IDirectInputDevice2AImpl_GetEffectInfo,
881     IDirectInputDevice2AImpl_GetForceFeedbackState,
882     IDirectInputDevice2AImpl_SendForceFeedbackCommand,
883     IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
884     IDirectInputDevice2AImpl_Escape,
885     IDirectInputDevice2AImpl_Poll,
886     IDirectInputDevice2AImpl_SendDeviceData,
887     IDirectInputDevice7AImpl_EnumEffectsInFile,
888     IDirectInputDevice7AImpl_WriteEffectToFile,
889     SysMouseAImpl_BuildActionMap,
890     SysMouseAImpl_SetActionMap,
891     IDirectInputDevice8AImpl_GetImageInfo
892 };
893
894 static const IDirectInputDevice8WVtbl SysMouseWvt =
895 {
896     IDirectInputDevice2WImpl_QueryInterface,
897     IDirectInputDevice2WImpl_AddRef,
898     IDirectInputDevice2WImpl_Release,
899     SysMouseWImpl_GetCapabilities,
900     IDirectInputDevice2WImpl_EnumObjects,
901     SysMouseWImpl_GetProperty,
902     IDirectInputDevice2WImpl_SetProperty,
903     SysMouseWImpl_Acquire,
904     SysMouseWImpl_Unacquire,
905     SysMouseWImpl_GetDeviceState,
906     SysMouseWImpl_GetDeviceData,
907     IDirectInputDevice2WImpl_SetDataFormat,
908     IDirectInputDevice2WImpl_SetEventNotification,
909     IDirectInputDevice2WImpl_SetCooperativeLevel,
910     SysMouseWImpl_GetObjectInfo,
911     SysMouseWImpl_GetDeviceInfo,
912     IDirectInputDevice2WImpl_RunControlPanel,
913     IDirectInputDevice2WImpl_Initialize,
914     IDirectInputDevice2WImpl_CreateEffect,
915     IDirectInputDevice2WImpl_EnumEffects,
916     IDirectInputDevice2WImpl_GetEffectInfo,
917     IDirectInputDevice2WImpl_GetForceFeedbackState,
918     IDirectInputDevice2WImpl_SendForceFeedbackCommand,
919     IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
920     IDirectInputDevice2WImpl_Escape,
921     IDirectInputDevice2WImpl_Poll,
922     IDirectInputDevice2WImpl_SendDeviceData,
923     IDirectInputDevice7WImpl_EnumEffectsInFile,
924     IDirectInputDevice7WImpl_WriteEffectToFile,
925     SysMouseWImpl_BuildActionMap,
926     SysMouseWImpl_SetActionMap,
927     IDirectInputDevice8WImpl_GetImageInfo
928 };