1 /* DirectInput Mouse device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
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.
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.
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
23 #include "wine/port.h"
35 #include "dinput_private.h"
36 #include "device_private.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
44 /* Wine mouse driver object instances */
45 #define WINE_MOUSE_X_AXIS_INSTANCE 0
46 #define WINE_MOUSE_Y_AXIS_INSTANCE 1
47 #define WINE_MOUSE_Z_AXIS_INSTANCE 2
48 #define WINE_MOUSE_L_BUTTON_INSTANCE 0
49 #define WINE_MOUSE_R_BUTTON_INSTANCE 1
50 #define WINE_MOUSE_M_BUTTON_INSTANCE 2
51 #define WINE_MOUSE_D_BUTTON_INSTANCE 3
53 /* ------------------------------- */
54 /* Wine mouse internal data format */
55 /* ------------------------------- */
57 /* Constants used to access the offset array */
58 #define WINE_MOUSE_X_POSITION 0
59 #define WINE_MOUSE_Y_POSITION 1
60 #define WINE_MOUSE_Z_POSITION 2
61 #define WINE_MOUSE_L_POSITION 3
62 #define WINE_MOUSE_R_POSITION 4
63 #define WINE_MOUSE_M_POSITION 5
70 } Wine_InternalMouseData;
72 #define WINE_INTERNALMOUSE_NUM_OBJS 6
74 static const DIOBJECTDATAFORMAT Wine_InternalMouseObjectFormat[WINE_INTERNALMOUSE_NUM_OBJS] = {
75 { &GUID_XAxis, FIELD_OFFSET(Wine_InternalMouseData, lX),
76 DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS, 0 },
77 { &GUID_YAxis, FIELD_OFFSET(Wine_InternalMouseData, lY),
78 DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS, 0 },
79 { &GUID_ZAxis, FIELD_OFFSET(Wine_InternalMouseData, lZ),
80 DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS, 0 },
81 { &GUID_Button, (FIELD_OFFSET(Wine_InternalMouseData, rgbButtons)) + 0,
82 DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE) | DIDFT_PSHBUTTON, 0 },
83 { &GUID_Button, (FIELD_OFFSET(Wine_InternalMouseData, rgbButtons)) + 1,
84 DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE) | DIDFT_PSHBUTTON, 0 },
85 { &GUID_Button, (FIELD_OFFSET(Wine_InternalMouseData, rgbButtons)) + 2,
86 DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE) | DIDFT_PSHBUTTON, 0 }
89 static const DIDATAFORMAT Wine_InternalMouseFormat = {
90 0, /* dwSize - unused */
91 0, /* dwObjsize - unused */
92 0, /* dwFlags - unused */
93 sizeof(Wine_InternalMouseData),
94 WINE_INTERNALMOUSE_NUM_OBJS, /* dwNumObjs */
95 (LPDIOBJECTDATAFORMAT) Wine_InternalMouseObjectFormat
98 static const IDirectInputDevice8AVtbl SysMouseAvt;
99 static const IDirectInputDevice8WVtbl SysMouseWvt;
101 typedef struct SysMouseImpl SysMouseImpl;
104 WARP_DONE, /* Warping has been done */
105 WARP_NEEDED, /* Warping is needed */
106 WARP_STARTED /* Warping has been done, waiting for the warp event */
111 struct IDirectInputDevice2AImpl base;
113 IDirectInputImpl *dinput;
115 /* The current data format and the conversion between internal
116 and external data formats */
119 int offset_array[WINE_INTERNALMOUSE_NUM_OBJS];
123 /* Previous position for relative moves */
125 /* These are used in case of relative -> absolute transitions */
130 DWORD win_centerX, win_centerY;
131 LPDIDEVICEOBJECTDATA data_queue;
132 int queue_head, queue_tail, queue_len;
134 /* warping: whether we need to move mouse back to middle once we
135 * reach window borders (for e.g. shooters, "surface movement" games) */
136 WARP_STATUS need_warp;
139 CRITICAL_SECTION crit;
141 /* This is for mouse reporting. */
142 Wine_InternalMouseData m_state;
145 /* FIXME: This is ugly and not thread safe :/ */
146 static IDirectInputDevice8A* current_lock = NULL;
148 static GUID DInput_Wine_Mouse_GUID = { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
152 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
155 static void fill_mouse_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
157 DIDEVICEINSTANCEA ddi;
159 dwSize = lpddi->dwSize;
161 TRACE("%d %p\n", dwSize, lpddi);
163 memset(lpddi, 0, dwSize);
164 memset(&ddi, 0, sizeof(ddi));
167 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
168 ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
169 if (version >= 0x0800)
170 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
172 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
173 strcpy(ddi.tszInstanceName, "Mouse");
174 strcpy(ddi.tszProductName, "Wine Mouse");
176 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
179 static void fill_mouse_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
181 DIDEVICEINSTANCEW ddi;
183 dwSize = lpddi->dwSize;
185 TRACE("%d %p\n", dwSize, lpddi);
187 memset(lpddi, 0, dwSize);
188 memset(&ddi, 0, sizeof(ddi));
191 ddi.guidInstance = GUID_SysMouse;/* DInput's GUID */
192 ddi.guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
193 if (version >= 0x0800)
194 ddi.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
196 ddi.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
197 MultiByteToWideChar(CP_ACP, 0, "Mouse", -1, ddi.tszInstanceName, MAX_PATH);
198 MultiByteToWideChar(CP_ACP, 0, "Wine Mouse", -1, ddi.tszProductName, MAX_PATH);
200 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
203 static BOOL mousedev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
208 if ((dwDevType == 0) ||
209 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
210 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
211 TRACE("Enumerating the mouse device\n");
213 fill_mouse_dideviceinstanceA(lpddi, version);
221 static BOOL mousedev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
226 if ((dwDevType == 0) ||
227 ((dwDevType == DIDEVTYPE_MOUSE) && (version < 0x0800)) ||
228 (((dwDevType == DI8DEVCLASS_POINTER) || (dwDevType == DI8DEVTYPE_MOUSE)) && (version >= 0x0800))) {
229 TRACE("Enumerating the mouse device\n");
231 fill_mouse_dideviceinstanceW(lpddi, version);
239 static SysMouseImpl *alloc_device(REFGUID rguid, const void *mvt, IDirectInputImpl *dinput)
241 int offset_array[WINE_INTERNALMOUSE_NUM_OBJS] = {
242 FIELD_OFFSET(Wine_InternalMouseData, lX),
243 FIELD_OFFSET(Wine_InternalMouseData, lY),
244 FIELD_OFFSET(Wine_InternalMouseData, lZ),
245 FIELD_OFFSET(Wine_InternalMouseData, rgbButtons) + 0,
246 FIELD_OFFSET(Wine_InternalMouseData, rgbButtons) + 1,
247 FIELD_OFFSET(Wine_InternalMouseData, rgbButtons) + 2
249 SysMouseImpl* newDevice;
250 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseImpl));
251 newDevice->base.lpVtbl = mvt;
252 newDevice->base.ref = 1;
253 memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
254 InitializeCriticalSection(&(newDevice->crit));
256 /* Per default, Wine uses its internal data format */
257 newDevice->df = (DIDATAFORMAT *) &Wine_InternalMouseFormat;
258 memcpy(newDevice->offset_array, offset_array, WINE_INTERNALMOUSE_NUM_OBJS * sizeof(int));
259 newDevice->wine_df = HeapAlloc(GetProcessHeap(), 0, sizeof(DataFormat));
260 newDevice->wine_df->size = 0;
261 newDevice->wine_df->internal_format_size = Wine_InternalMouseFormat.dwDataSize;
262 newDevice->wine_df->dt = NULL;
263 newDevice->dinput = dinput;
264 newDevice->dwCoopLevel = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;
269 static HRESULT mousedev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
271 if ((IsEqualGUID(&GUID_SysMouse,rguid)) || /* Generic Mouse */
272 (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
273 if ((riid == NULL) ||
274 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
275 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
276 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
277 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
278 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysMouseAvt, dinput);
279 TRACE("Creating a Mouse device (%p)\n", *pdev);
282 return DIERR_NOINTERFACE;
285 return DIERR_DEVICENOTREG;
288 static HRESULT mousedev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
290 if ((IsEqualGUID(&GUID_SysMouse,rguid)) || /* Generic Mouse */
291 (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
292 if ((riid == NULL) ||
293 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
294 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
295 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
296 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
297 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysMouseWvt, dinput);
298 TRACE("Creating a Mouse device (%p)\n", *pdev);
301 return DIERR_NOINTERFACE;
304 return DIERR_DEVICENOTREG;
307 const struct dinput_device mouse_device = {
309 mousedev_enum_deviceA,
310 mousedev_enum_deviceW,
311 mousedev_create_deviceA,
312 mousedev_create_deviceW
315 /******************************************************************************
316 * SysMouseA (DInput Mouse support)
319 /******************************************************************************
320 * Release : release the mouse buffer.
322 static ULONG WINAPI SysMouseAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
324 SysMouseImpl *This = (SysMouseImpl *)iface;
327 ref = InterlockedDecrement(&This->base.ref);
331 set_dinput_hook(WH_MOUSE_LL, NULL);
333 /* Free the data queue */
334 HeapFree(GetProcessHeap(),0,This->data_queue);
335 DeleteCriticalSection(&(This->crit));
337 /* Free the DataFormat */
338 if (This->df != &(Wine_InternalMouseFormat)) {
339 HeapFree(GetProcessHeap(), 0, This->df->rgodf);
340 HeapFree(GetProcessHeap(), 0, This->df);
343 HeapFree(GetProcessHeap(),0,This);
348 /******************************************************************************
349 * SetCooperativeLevel : store the window in which we will do our
352 static HRESULT WINAPI SysMouseAImpl_SetCooperativeLevel(
353 LPDIRECTINPUTDEVICE8A iface,HWND hwnd,DWORD dwflags
356 SysMouseImpl *This = (SysMouseImpl *)iface;
358 TRACE("(this=%p,%p,0x%08x)\n", This, hwnd, dwflags);
360 if (TRACE_ON(dinput)) {
361 TRACE(" cooperative level : ");
362 _dump_cooperativelevel_DI(dwflags);
365 if ((dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == 0 ||
366 (dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE) ||
367 (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == 0 ||
368 (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == (DISCL_FOREGROUND | DISCL_BACKGROUND))
369 return DIERR_INVALIDPARAM;
371 if (dwflags == (DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))
372 hwnd = GetDesktopWindow();
374 if (!hwnd) return E_HANDLE;
376 if (dwflags & DISCL_EXCLUSIVE && dwflags & DISCL_BACKGROUND) {
377 return DIERR_UNSUPPORTED;
380 /* Store the window which asks for the mouse */
382 This->dwCoopLevel = dwflags;
388 /******************************************************************************
389 * SetDataFormat : the application can choose the format of the data
390 * the device driver sends back with GetDeviceState.
392 * For the moment, only the "standard" configuration (c_dfDIMouse) is supported
393 * in absolute and relative mode.
395 static HRESULT WINAPI SysMouseAImpl_SetDataFormat(
396 LPDIRECTINPUTDEVICE8A iface,LPCDIDATAFORMAT df
399 SysMouseImpl *This = (SysMouseImpl *)iface;
401 TRACE("(this=%p,%p)\n",This,df);
403 _dump_DIDATAFORMAT(df);
405 /* Tests under windows show that a call to SetDataFormat always sets the mouse
406 in relative mode whatever the dwFlags value (DIDF_ABSAXIS/DIDF_RELAXIS).
407 To switch in absolute mode, SetProperty must be used. */
410 /* Store the new data format */
411 This->df = HeapAlloc(GetProcessHeap(),0,df->dwSize);
412 memcpy(This->df, df, df->dwSize);
413 This->df->rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize);
414 memcpy(This->df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize);
416 /* Prepare all the data-conversion filters */
417 This->wine_df = create_DataFormat(&(Wine_InternalMouseFormat), df, This->offset_array);
422 /* low-level mouse hook */
423 static LRESULT CALLBACK dinput_mouse_hook( int code, WPARAM wparam, LPARAM lparam )
425 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
426 SysMouseImpl* This = (SysMouseImpl*) current_lock;
430 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
432 EnterCriticalSection(&(This->crit));
433 dwCoop = This->dwCoopLevel;
435 if (wparam == WM_MOUSEMOVE) {
436 if (This->absolute) {
437 if (hook->pt.x != This->prevX)
438 GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], hook->pt.x,
439 hook->time, This->dinput->evsequence);
440 if (hook->pt.y != This->prevY)
441 GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], hook->pt.y,
442 hook->time, This->dinput->evsequence);
444 /* Now, warp handling */
445 if ((This->need_warp == WARP_STARTED) &&
446 (hook->pt.x == This->mapped_center.x) && (hook->pt.y == This->mapped_center.y)) {
447 /* Warp has been done... */
448 This->need_warp = WARP_DONE;
452 /* Relative mouse input with absolute mouse event : the real fun starts here... */
453 if ((This->need_warp == WARP_NEEDED) ||
454 (This->need_warp == WARP_STARTED)) {
455 if (hook->pt.x != This->prevX)
456 GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], hook->pt.x - This->prevX,
457 hook->time, This->dinput->evsequence);
458 if (hook->pt.y != This->prevY)
459 GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], hook->pt.y - This->prevY,
460 hook->time, This->dinput->evsequence);
462 /* This is the first time the event handler has been called after a
463 GetDeviceData or GetDeviceState. */
464 if (hook->pt.x != This->mapped_center.x) {
465 GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], hook->pt.x - This->mapped_center.x,
466 hook->time, This->dinput->evsequence);
467 This->need_warp = WARP_NEEDED;
470 if (hook->pt.y != This->mapped_center.y) {
471 GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], hook->pt.y - This->mapped_center.y,
472 hook->time, This->dinput->evsequence);
473 This->need_warp = WARP_NEEDED;
478 This->prevX = hook->pt.x;
479 This->prevY = hook->pt.y;
481 if (This->absolute) {
482 This->m_state.lX = hook->pt.x;
483 This->m_state.lY = hook->pt.y;
485 This->m_state.lX = hook->pt.x - This->mapped_center.x;
486 This->m_state.lY = hook->pt.y - This->mapped_center.y;
490 TRACE(" msg %x pt %d %d (W=%d)\n",
491 wparam, hook->pt.x, hook->pt.y, (!This->absolute) && This->need_warp );
495 GEN_EVENT(This->offset_array[WINE_MOUSE_L_POSITION], 0x80,
496 hook->time, This->dinput->evsequence);
497 This->m_state.rgbButtons[0] = 0x80;
500 GEN_EVENT(This->offset_array[WINE_MOUSE_L_POSITION], 0x00,
501 hook->time, This->dinput->evsequence);
502 This->m_state.rgbButtons[0] = 0x00;
505 GEN_EVENT(This->offset_array[WINE_MOUSE_R_POSITION], 0x80,
506 hook->time, This->dinput->evsequence);
507 This->m_state.rgbButtons[1] = 0x80;
510 GEN_EVENT(This->offset_array[WINE_MOUSE_R_POSITION], 0x00,
511 hook->time, This->dinput->evsequence);
512 This->m_state.rgbButtons[1] = 0x00;
515 GEN_EVENT(This->offset_array[WINE_MOUSE_M_POSITION], 0x80,
516 hook->time, This->dinput->evsequence);
517 This->m_state.rgbButtons[2] = 0x80;
520 GEN_EVENT(This->offset_array[WINE_MOUSE_M_POSITION], 0x00,
521 hook->time, This->dinput->evsequence);
522 This->m_state.rgbButtons[2] = 0x00;
525 wdata = (short)HIWORD(hook->mouseData);
526 GEN_EVENT(This->offset_array[WINE_MOUSE_Z_POSITION], wdata,
527 hook->time, This->dinput->evsequence);
528 This->m_state.lZ += wdata;
532 TRACE("(X: %d - Y: %d L: %02x M: %02x R: %02x)\n",
533 This->m_state.lX, This->m_state.lY,
534 This->m_state.rgbButtons[0], This->m_state.rgbButtons[2], This->m_state.rgbButtons[1]);
536 This->dinput->evsequence++;
539 /* Mouse moved -> send event if asked */
540 if (This->base.hEvent) SetEvent(This->base.hEvent);
542 LeaveCriticalSection(&(This->crit));
545 if (dwCoop & DISCL_EXCLUSIVE) return 1;
547 /* Pass the events down to previous handlers (e.g. win32 input) */
548 return CallNextHookEx( 0, code, wparam, lparam );
551 static BOOL dinput_window_check(SysMouseImpl* This) {
553 DWORD centerX, centerY;
555 /* make sure the window hasn't moved */
556 if(!GetWindowRect(This->win, &rect))
558 centerX = (rect.right - rect.left) / 2;
559 centerY = (rect.bottom - rect.top ) / 2;
560 if (This->win_centerX != centerX || This->win_centerY != centerY) {
561 This->win_centerX = centerX;
562 This->win_centerY = centerY;
564 This->mapped_center.x = This->win_centerX;
565 This->mapped_center.y = This->win_centerY;
566 MapWindowPoints(This->win, HWND_DESKTOP, &This->mapped_center, 1);
571 /******************************************************************************
572 * Acquire : gets exclusive control of the mouse
574 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
576 SysMouseImpl *This = (SysMouseImpl *)iface;
580 TRACE("(this=%p)\n",This);
587 /* Store (in a global variable) the current lock */
588 current_lock = (IDirectInputDevice8A*)This;
590 /* Init the mouse state */
591 GetCursorPos( &point );
592 if (This->absolute) {
593 This->m_state.lX = point.x;
594 This->m_state.lY = point.y;
595 This->prevX = point.x;
596 This->prevY = point.y;
598 This->m_state.lX = 0;
599 This->m_state.lY = 0;
600 This->org_coords = point;
602 This->m_state.lZ = 0;
603 This->m_state.rgbButtons[0] = GetKeyState(VK_LBUTTON) & 0x80;
604 This->m_state.rgbButtons[1] = GetKeyState(VK_RBUTTON) & 0x80;
605 This->m_state.rgbButtons[2] = GetKeyState(VK_MBUTTON) & 0x80;
607 /* Install our mouse hook */
608 if (This->dwCoopLevel & DISCL_EXCLUSIVE)
609 ShowCursor(FALSE); /* hide cursor */
610 set_dinput_hook(WH_MOUSE_LL, dinput_mouse_hook);
612 /* Get the window dimension and find the center */
613 GetWindowRect(This->win, &rect);
614 This->win_centerX = (rect.right - rect.left) / 2;
615 This->win_centerY = (rect.bottom - rect.top ) / 2;
617 /* Warp the mouse to the center of the window */
618 if (This->absolute == 0) {
619 This->mapped_center.x = This->win_centerX;
620 This->mapped_center.y = This->win_centerY;
621 MapWindowPoints(This->win, HWND_DESKTOP, &This->mapped_center, 1);
622 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
623 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
624 This->last_warped = GetCurrentTime();
627 This->need_warp = WARP_DONE;
629 This->need_warp = WARP_STARTED;
636 /******************************************************************************
637 * Unacquire : frees the mouse
639 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
641 SysMouseImpl *This = (SysMouseImpl *)iface;
643 TRACE("(this=%p)\n",This);
645 if (0 == This->acquired) {
649 set_dinput_hook(WH_MOUSE_LL, NULL);
650 if (This->dwCoopLevel & DISCL_EXCLUSIVE)
651 ShowCursor(TRUE); /* show cursor */
654 if (current_lock == (IDirectInputDevice8A*) This)
657 ERR("this(%p) != current_lock(%p)\n", This, current_lock);
659 /* Unacquire device */
662 /* And put the mouse cursor back where it was at acquire time */
663 if (This->absolute == 0) {
664 TRACE(" warping mouse back to (%d , %d)\n", This->org_coords.x, This->org_coords.y);
665 SetCursorPos(This->org_coords.x, This->org_coords.y);
671 /******************************************************************************
672 * GetDeviceState : returns the "state" of the mouse.
674 * For the moment, only the "standard" return structure (DIMOUSESTATE) is
677 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(
678 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
680 SysMouseImpl *This = (SysMouseImpl *)iface;
682 if(This->acquired == 0) return DIERR_NOTACQUIRED;
684 EnterCriticalSection(&(This->crit));
685 TRACE("(this=%p,0x%08x,%p):\n", This, len, ptr);
686 TRACE("(X: %d - Y: %d - Z: %d L: %02x M: %02x R: %02x)\n",
687 This->m_state.lX, This->m_state.lY, This->m_state.lZ,
688 This->m_state.rgbButtons[0], This->m_state.rgbButtons[2], This->m_state.rgbButtons[1]);
690 /* Copy the current mouse state */
691 fill_DataFormat(ptr, &(This->m_state), This->wine_df);
693 /* Initialize the buffer when in relative mode */
694 if (This->absolute == 0) {
695 This->m_state.lX = 0;
696 This->m_state.lY = 0;
697 This->m_state.lZ = 0;
700 /* Check if we need to do a mouse warping */
701 if (This->need_warp == WARP_NEEDED && (GetCurrentTime() - This->last_warped > 10)) {
702 if(!dinput_window_check(This))
704 LeaveCriticalSection(&(This->crit));
705 return DIERR_GENERIC;
707 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
708 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
709 This->last_warped = GetCurrentTime();
712 This->need_warp = WARP_DONE;
714 This->need_warp = WARP_STARTED;
718 LeaveCriticalSection(&(This->crit));
723 /******************************************************************************
724 * GetDeviceData : gets buffered input data.
726 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
728 LPDIDEVICEOBJECTDATA dod,
732 SysMouseImpl *This = (SysMouseImpl *)iface;
736 TRACE("(%p)->(dods=%d,dod=%p,entries=%p (%d)%s,fl=0x%08x%s)\n",This,dodsize,dod,
737 entries, *entries,*entries == INFINITE ? " (INFINITE)" : "",
738 flags, (flags & DIGDD_PEEK) ? " (DIGDD_PEEK)": "" );
740 if (This->acquired == 0) {
741 WARN(" application tries to get data from an unacquired device !\n");
742 return DIERR_NOTACQUIRED;
745 EnterCriticalSection(&(This->crit));
747 len = ((This->queue_head < This->queue_tail) ? This->queue_len : 0)
748 + (This->queue_head - This->queue_tail);
749 if ((*entries != INFINITE) && (len > *entries)) len = *entries;
754 if (!(flags & DIGDD_PEEK)) {
756 TRACE("Application discarding %d event(s).\n", len);
758 nqtail = This->queue_tail + len;
759 while (nqtail >= This->queue_len) nqtail -= This->queue_len;
761 TRACE("Telling application that %d event(s) are in the queue.\n", len);
764 if (dodsize < sizeof(DIDEVICEOBJECTDATA_DX3)) {
765 ERR("Wrong structure size !\n");
766 LeaveCriticalSection(&(This->crit));
767 return DIERR_INVALIDPARAM;
771 TRACE("Application retrieving %d event(s):\n", len);
774 nqtail = This->queue_tail;
776 /* Copy the buffered data into the application queue */
777 TRACE(" - queuing Offs:%2d Data:%5d TS:%8d Seq:%8d at address %p from queue tail %4d\n",
778 (This->data_queue)->dwOfs,
779 (This->data_queue)->dwData,
780 (This->data_queue)->dwTimeStamp,
781 (This->data_queue)->dwSequence,
782 (char *)dod + *entries * dodsize,
784 memcpy((char *)dod + *entries * dodsize, This->data_queue + nqtail, dodsize);
785 /* Advance position */
787 if (nqtail >= This->queue_len)
788 nqtail -= This->queue_len;
793 if (!(flags & DIGDD_PEEK))
794 This->queue_tail = nqtail;
796 LeaveCriticalSection(&(This->crit));
798 /* Check if we need to do a mouse warping */
799 if (This->need_warp == WARP_NEEDED && (GetCurrentTime() - This->last_warped > 10)) {
800 if(!dinput_window_check(This))
801 return DIERR_GENERIC;
802 TRACE("Warping mouse to %d - %d\n", This->mapped_center.x, This->mapped_center.y);
803 SetCursorPos( This->mapped_center.x, This->mapped_center.y );
804 This->last_warped = GetCurrentTime();
807 This->need_warp = WARP_DONE;
809 This->need_warp = WARP_STARTED;
815 /******************************************************************************
816 * SetProperty : change input device properties
818 static HRESULT WINAPI SysMouseAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
822 SysMouseImpl *This = (SysMouseImpl *)iface;
824 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
826 if (!HIWORD(rguid)) {
827 switch (LOWORD(rguid)) {
828 case (DWORD) DIPROP_BUFFERSIZE: {
829 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
831 TRACE("buffersize = %d\n", pd->dwData);
833 This->data_queue = HeapAlloc(GetProcessHeap(),0, pd->dwData * sizeof(DIDEVICEOBJECTDATA));
834 This->queue_head = 0;
835 This->queue_tail = 0;
836 This->queue_len = pd->dwData;
839 case (DWORD) DIPROP_AXISMODE: {
840 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
841 This->absolute = !(pd->dwData);
842 TRACE("Using %s coordinates mode now\n", This->absolute ? "absolute" : "relative");
846 FIXME("Unknown type %p (%s)\n",rguid,debugstr_guid(rguid));
854 /******************************************************************************
855 * GetProperty : get input device properties
857 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
859 LPDIPROPHEADER pdiph)
861 SysMouseImpl *This = (SysMouseImpl *)iface;
863 TRACE("(this=%p,%s,%p)\n",
864 iface, debugstr_guid(rguid), pdiph);
866 if (TRACE_ON(dinput))
867 _dump_DIPROPHEADER(pdiph);
869 if (!HIWORD(rguid)) {
870 switch (LOWORD(rguid)) {
871 case (DWORD) DIPROP_BUFFERSIZE: {
872 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
874 TRACE(" return buffersize = %d\n",This->queue_len);
875 pd->dwData = This->queue_len;
879 case (DWORD) DIPROP_GRANULARITY: {
880 LPDIPROPDWORD pr = (LPDIPROPDWORD) pdiph;
882 /* We'll just assume that the app asks about the Z axis */
883 pr->dwData = WHEEL_DELTA;
888 case (DWORD) DIPROP_RANGE: {
889 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
891 if ((pdiph->dwHow == DIPH_BYID) &&
892 ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
893 (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
894 /* Querying the range of either the X or the Y axis. As I do
895 not know the range, do as if the range were
897 pr->lMin = DIPROPRANGE_NOMIN;
898 pr->lMax = DIPROPRANGE_NOMAX;
905 FIXME("Unknown type %p (%s)\n",rguid,debugstr_guid(rguid));
913 /******************************************************************************
914 * GetCapabilities : get the device capablitites
916 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(
917 LPDIRECTINPUTDEVICE8A iface,
918 LPDIDEVCAPS lpDIDevCaps)
920 SysMouseImpl *This = (SysMouseImpl *)iface;
923 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
925 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
926 WARN("invalid parameter\n");
927 return DIERR_INVALIDPARAM;
930 devcaps.dwSize = lpDIDevCaps->dwSize;
931 devcaps.dwFlags = DIDC_ATTACHED;
932 if (This->dinput->dwVersion >= 0x0800)
933 devcaps.dwDevType = DI8DEVTYPE_MOUSE | (DI8DEVTYPEMOUSE_TRADITIONAL << 8);
935 devcaps.dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_TRADITIONAL << 8);
937 devcaps.dwButtons = 3;
939 devcaps.dwFFSamplePeriod = 0;
940 devcaps.dwFFMinTimeResolution = 0;
941 devcaps.dwFirmwareRevision = 100;
942 devcaps.dwHardwareRevision = 100;
943 devcaps.dwFFDriverVersion = 0;
945 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
951 /******************************************************************************
952 * EnumObjects : enumerate the different buttons and axis...
954 static HRESULT WINAPI SysMouseAImpl_EnumObjects(
955 LPDIRECTINPUTDEVICE8A iface,
956 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
960 SysMouseImpl *This = (SysMouseImpl *)iface;
961 DIDEVICEOBJECTINSTANCEA ddoi;
963 TRACE("(this=%p,%p,%p,%08x)\n", This, lpCallback, lpvRef, dwFlags);
964 if (TRACE_ON(dinput)) {
965 TRACE(" - flags = ");
966 _dump_EnumObjects_flags(dwFlags);
970 /* Only the fields till dwFFMaxForce are relevant */
971 memset(&ddoi, 0, sizeof(ddoi));
972 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
974 /* In a mouse, we have : two relative axis and three buttons */
975 if ((dwFlags == DIDFT_ALL) ||
976 (dwFlags & DIDFT_AXIS)) {
978 ddoi.guidType = GUID_XAxis;
979 ddoi.dwOfs = This->offset_array[WINE_MOUSE_X_POSITION];
980 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
981 strcpy(ddoi.tszName, "X-Axis");
982 _dump_OBJECTINSTANCEA(&ddoi);
983 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
986 ddoi.guidType = GUID_YAxis;
987 ddoi.dwOfs = This->offset_array[WINE_MOUSE_Y_POSITION];
988 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
989 strcpy(ddoi.tszName, "Y-Axis");
990 _dump_OBJECTINSTANCEA(&ddoi);
991 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
994 ddoi.guidType = GUID_ZAxis;
995 ddoi.dwOfs = This->offset_array[WINE_MOUSE_Z_POSITION];
996 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_Z_AXIS_INSTANCE) | DIDFT_RELAXIS;
997 strcpy(ddoi.tszName, "Z-Axis");
998 _dump_OBJECTINSTANCEA(&ddoi);
999 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
1002 if ((dwFlags == DIDFT_ALL) ||
1003 (dwFlags & DIDFT_BUTTON)) {
1004 ddoi.guidType = GUID_Button;
1007 ddoi.dwOfs = This->offset_array[WINE_MOUSE_L_POSITION];
1008 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
1009 strcpy(ddoi.tszName, "Left-Button");
1010 _dump_OBJECTINSTANCEA(&ddoi);
1011 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
1014 ddoi.dwOfs = This->offset_array[WINE_MOUSE_R_POSITION];
1015 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
1016 strcpy(ddoi.tszName, "Right-Button");
1017 _dump_OBJECTINSTANCEA(&ddoi);
1018 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
1021 ddoi.dwOfs = This->offset_array[WINE_MOUSE_M_POSITION];
1022 ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
1023 strcpy(ddoi.tszName, "Middle-Button");
1024 _dump_OBJECTINSTANCEA(&ddoi);
1025 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
1031 static HRESULT WINAPI SysMouseWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface, LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback, LPVOID lpvRef,DWORD dwFlags)
1033 SysMouseImpl *This = (SysMouseImpl *)iface;
1035 device_enumobjects_AtoWcb_data data;
1037 data.lpCallBack = lpCallback;
1038 data.lpvRef = lpvRef;
1040 return SysMouseAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
1043 /******************************************************************************
1044 * GetDeviceInfo : get information about a device's identity
1046 static HRESULT WINAPI SysMouseAImpl_GetDeviceInfo(
1047 LPDIRECTINPUTDEVICE8A iface,
1048 LPDIDEVICEINSTANCEA pdidi)
1050 SysMouseImpl *This = (SysMouseImpl *)iface;
1051 TRACE("(this=%p,%p)\n", This, pdidi);
1053 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
1054 WARN(" dinput3 not supporte yet...\n");
1058 fill_mouse_dideviceinstanceA(pdidi, This->dinput->dwVersion);
1063 static HRESULT WINAPI SysMouseWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
1065 SysMouseImpl *This = (SysMouseImpl *)iface;
1066 TRACE("(this=%p,%p)\n", This, pdidi);
1068 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
1069 WARN(" dinput3 not supporte yet...\n");
1073 fill_mouse_dideviceinstanceW(pdidi, This->dinput->dwVersion);
1079 static const IDirectInputDevice8AVtbl SysMouseAvt =
1081 IDirectInputDevice2AImpl_QueryInterface,
1082 IDirectInputDevice2AImpl_AddRef,
1083 SysMouseAImpl_Release,
1084 SysMouseAImpl_GetCapabilities,
1085 SysMouseAImpl_EnumObjects,
1086 SysMouseAImpl_GetProperty,
1087 SysMouseAImpl_SetProperty,
1088 SysMouseAImpl_Acquire,
1089 SysMouseAImpl_Unacquire,
1090 SysMouseAImpl_GetDeviceState,
1091 SysMouseAImpl_GetDeviceData,
1092 SysMouseAImpl_SetDataFormat,
1093 IDirectInputDevice2AImpl_SetEventNotification,
1094 SysMouseAImpl_SetCooperativeLevel,
1095 IDirectInputDevice2AImpl_GetObjectInfo,
1096 SysMouseAImpl_GetDeviceInfo,
1097 IDirectInputDevice2AImpl_RunControlPanel,
1098 IDirectInputDevice2AImpl_Initialize,
1099 IDirectInputDevice2AImpl_CreateEffect,
1100 IDirectInputDevice2AImpl_EnumEffects,
1101 IDirectInputDevice2AImpl_GetEffectInfo,
1102 IDirectInputDevice2AImpl_GetForceFeedbackState,
1103 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1104 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1105 IDirectInputDevice2AImpl_Escape,
1106 IDirectInputDevice2AImpl_Poll,
1107 IDirectInputDevice2AImpl_SendDeviceData,
1108 IDirectInputDevice7AImpl_EnumEffectsInFile,
1109 IDirectInputDevice7AImpl_WriteEffectToFile,
1110 IDirectInputDevice8AImpl_BuildActionMap,
1111 IDirectInputDevice8AImpl_SetActionMap,
1112 IDirectInputDevice8AImpl_GetImageInfo
1115 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1116 # define XCAST(fun) (typeof(SysMouseWvt.fun))
1118 # define XCAST(fun) (void*)
1121 static const IDirectInputDevice8WVtbl SysMouseWvt =
1123 IDirectInputDevice2WImpl_QueryInterface,
1124 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1125 XCAST(Release)SysMouseAImpl_Release,
1126 XCAST(GetCapabilities)SysMouseAImpl_GetCapabilities,
1127 SysMouseWImpl_EnumObjects,
1128 XCAST(GetProperty)SysMouseAImpl_GetProperty,
1129 XCAST(SetProperty)SysMouseAImpl_SetProperty,
1130 XCAST(Acquire)SysMouseAImpl_Acquire,
1131 XCAST(Unacquire)SysMouseAImpl_Unacquire,
1132 XCAST(GetDeviceState)SysMouseAImpl_GetDeviceState,
1133 XCAST(GetDeviceData)SysMouseAImpl_GetDeviceData,
1134 XCAST(SetDataFormat)SysMouseAImpl_SetDataFormat,
1135 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1136 XCAST(SetCooperativeLevel)SysMouseAImpl_SetCooperativeLevel,
1137 IDirectInputDevice2WImpl_GetObjectInfo,
1138 SysMouseWImpl_GetDeviceInfo,
1139 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1140 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1141 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1142 IDirectInputDevice2WImpl_EnumEffects,
1143 IDirectInputDevice2WImpl_GetEffectInfo,
1144 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1145 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1146 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1147 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1148 XCAST(Poll)IDirectInputDevice2AImpl_Poll,
1149 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1150 IDirectInputDevice7WImpl_EnumEffectsInFile,
1151 IDirectInputDevice7WImpl_WriteEffectToFile,
1152 IDirectInputDevice8WImpl_BuildActionMap,
1153 IDirectInputDevice8WImpl_SetActionMap,
1154 IDirectInputDevice8WImpl_GetImageInfo