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