3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2002 TransGaming Technologies Inc.
6 * Copyright 2007 Vitaliy Margolen
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * - Tomb Raider 2 Demo:
26 * Playable using keyboard only.
27 * - WingCommander Prophecy Demo:
28 * Doesn't get Input Focus.
30 * - Fallout : works great in X and DGA mode
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
46 #include "dinput_private.h"
47 #include "device_private.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
51 static const IDirectInput7AVtbl ddi7avt
;
52 static const IDirectInput7WVtbl ddi7wvt
;
53 static const IDirectInput8AVtbl ddi8avt
;
54 static const IDirectInput8WVtbl ddi8wvt
;
56 static inline IDirectInputImpl
*impl_from_IDirectInput7W( IDirectInput7W
*iface
)
58 return CONTAINING_RECORD( iface
, IDirectInputImpl
, lpVtbl7w
);
61 static inline IDirectInputImpl
*impl_from_IDirectInput8A( IDirectInput8A
*iface
)
63 return CONTAINING_RECORD( iface
, IDirectInputImpl
, lpVtbl8a
);
66 static inline IDirectInputImpl
*impl_from_IDirectInput8W( IDirectInput8W
*iface
)
68 return CONTAINING_RECORD( iface
, IDirectInputImpl
, lpVtbl8w
);
71 static inline IDirectInput7W
*IDirectInput7W_from_impl( IDirectInputImpl
*iface
)
73 return (IDirectInput7W
*)(&iface
->lpVtbl7w
);
76 static const struct dinput_device
*dinput_devices
[] =
80 &joystick_linuxinput_device
,
81 &joystick_linux_device
83 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
85 static HINSTANCE DINPUT_instance
= NULL
;
87 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserv
)
91 case DLL_PROCESS_ATTACH
:
92 DisableThreadLibraryCalls(inst
);
93 DINPUT_instance
= inst
;
95 case DLL_PROCESS_DETACH
:
101 static BOOL
check_hook_thread(void);
102 static CRITICAL_SECTION dinput_hook_crit
;
103 static struct list direct_input_list
= LIST_INIT( direct_input_list
);
105 /******************************************************************************
106 * DirectInputCreateEx (DINPUT.@)
108 HRESULT WINAPI
DirectInputCreateEx(
109 HINSTANCE hinst
, DWORD dwVersion
, REFIID riid
, LPVOID
*ppDI
,
112 IDirectInputImpl
* This
;
114 TRACE("(%p,%04x,%s,%p,%p)\n", hinst
, dwVersion
, debugstr_guid(riid
), ppDI
, punkOuter
);
116 if (IsEqualGUID( &IID_IDirectInputA
, riid
) ||
117 IsEqualGUID( &IID_IDirectInput2A
, riid
) ||
118 IsEqualGUID( &IID_IDirectInput7A
, riid
) ||
119 IsEqualGUID( &IID_IDirectInputW
, riid
) ||
120 IsEqualGUID( &IID_IDirectInput2W
, riid
) ||
121 IsEqualGUID( &IID_IDirectInput7W
, riid
) ||
122 IsEqualGUID( &IID_IDirectInput8A
, riid
) ||
123 IsEqualGUID( &IID_IDirectInput8W
, riid
))
125 if (!(This
= HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl
) )))
126 return DIERR_OUTOFMEMORY
;
129 return DIERR_OLDDIRECTINPUTVERSION
;
131 This
->lpVtbl
= &ddi7avt
;
132 This
->lpVtbl7w
= &ddi7wvt
;
133 This
->lpVtbl8a
= &ddi8avt
;
134 This
->lpVtbl8w
= &ddi8wvt
;
136 This
->dwVersion
= dwVersion
;
137 This
->evsequence
= 1;
139 InitializeCriticalSection(&This
->crit
);
140 This
->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": IDirectInputImpl*->crit");
142 list_init( &This
->devices_list
);
144 /* Add self to the list of the IDirectInputs */
145 EnterCriticalSection( &dinput_hook_crit
);
146 list_add_head( &direct_input_list
, &This
->entry
);
147 LeaveCriticalSection( &dinput_hook_crit
);
149 if (!check_hook_thread())
151 IUnknown_Release( (LPDIRECTINPUT7A
)This
);
152 return DIERR_GENERIC
;
155 IDirectInput_QueryInterface( (IDirectInput7A
*)This
, riid
, ppDI
);
159 /******************************************************************************
160 * DirectInputCreateA (DINPUT.@)
162 HRESULT WINAPI
DirectInputCreateA(HINSTANCE hinst
, DWORD dwVersion
, LPDIRECTINPUTA
*ppDI
, LPUNKNOWN punkOuter
)
164 return DirectInputCreateEx(hinst
, dwVersion
, &IID_IDirectInput7A
, (LPVOID
*)ppDI
, punkOuter
);
167 /******************************************************************************
168 * DirectInputCreateW (DINPUT.@)
170 HRESULT WINAPI
DirectInputCreateW(HINSTANCE hinst
, DWORD dwVersion
, LPDIRECTINPUTW
*ppDI
, LPUNKNOWN punkOuter
)
172 return DirectInputCreateEx(hinst
, dwVersion
, &IID_IDirectInput7W
, (LPVOID
*)ppDI
, punkOuter
);
175 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType
) {
177 case 0: return "All devices";
178 case DIDEVTYPE_MOUSE
: return "DIDEVTYPE_MOUSE";
179 case DIDEVTYPE_KEYBOARD
: return "DIDEVTYPE_KEYBOARD";
180 case DIDEVTYPE_JOYSTICK
: return "DIDEVTYPE_JOYSTICK";
181 case DIDEVTYPE_DEVICE
: return "DIDEVTYPE_DEVICE";
182 default: return "Unknown";
186 static void _dump_EnumDevices_dwFlags(DWORD dwFlags
) {
187 if (TRACE_ON(dinput
)) {
189 static const struct {
193 #define FE(x) { x, #x}
194 FE(DIEDFL_ALLDEVICES
),
195 FE(DIEDFL_ATTACHEDONLY
),
196 FE(DIEDFL_FORCEFEEDBACK
),
197 FE(DIEDFL_INCLUDEALIASES
),
198 FE(DIEDFL_INCLUDEPHANTOMS
)
203 TRACE("DIEDFL_ALLDEVICES");
206 for (i
= 0; i
< (sizeof(flags
) / sizeof(flags
[0])); i
++)
207 if (flags
[i
].mask
& dwFlags
)
208 TRACE("%s ",flags
[i
].name
);
213 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat
) {
216 FIXME("diaf.dwSize = %d\n", lpdiActionFormat
->dwSize
);
217 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat
->dwActionSize
);
218 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat
->dwDataSize
);
219 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat
->dwNumActions
);
220 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat
->rgoAction
);
221 for (i
=0;i
<lpdiActionFormat
->dwNumActions
;i
++) {
222 FIXME("diaf.rgoAction[%d]:\n", i
);
223 FIXME("\tuAppData=%lx\n", lpdiActionFormat
->rgoAction
[i
].uAppData
);
224 FIXME("\tdwSemantics=%x\n", lpdiActionFormat
->rgoAction
[i
].dwSemantics
);
225 FIXME("\tdwFlags=%x\n", lpdiActionFormat
->rgoAction
[i
].dwFlags
);
226 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat
->rgoAction
[i
].lptszActionName
));
227 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat
->rgoAction
[i
].guidInstance
));
228 FIXME("\tdwObjID=%x\n", lpdiActionFormat
->rgoAction
[i
].dwObjID
);
229 FIXME("\tdwHow=%x\n", lpdiActionFormat
->rgoAction
[i
].dwHow
);
231 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat
->guidActionMap
));
232 FIXME("diaf.dwGenre = %d\n", lpdiActionFormat
->dwGenre
);
233 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat
->dwBufferSize
);
234 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat
->lAxisMin
);
235 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat
->lAxisMax
);
236 FIXME("diaf.hInstString = %p\n", lpdiActionFormat
->hInstString
);
237 FIXME("diaf.ftTimeStamp ...\n");
238 FIXME("diaf.dwCRC = %x\n", lpdiActionFormat
->dwCRC
);
239 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat
->tszActionMap
));
242 /******************************************************************************
243 * IDirectInputA_EnumDevices
245 static HRESULT WINAPI
IDirectInputAImpl_EnumDevices(
246 LPDIRECTINPUT7A iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKA lpCallback
,
247 LPVOID pvRef
, DWORD dwFlags
)
249 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
250 DIDEVICEINSTANCEA devInstance
;
253 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
254 This
, dwDevType
, _dump_DIDEVTYPE_value(dwDevType
),
255 lpCallback
, pvRef
, dwFlags
);
256 _dump_EnumDevices_dwFlags(dwFlags
);
258 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++) {
259 if (!dinput_devices
[i
]->enum_deviceA
) continue;
260 for (j
= 0, r
= -1; r
!= 0; j
++) {
261 devInstance
.dwSize
= sizeof(devInstance
);
262 TRACE(" - checking device %d ('%s')\n", i
, dinput_devices
[i
]->name
);
263 if ((r
= dinput_devices
[i
]->enum_deviceA(dwDevType
, dwFlags
, &devInstance
, This
->dwVersion
, j
))) {
264 if (lpCallback(&devInstance
,pvRef
) == DIENUM_STOP
)
272 /******************************************************************************
273 * IDirectInputW_EnumDevices
275 static HRESULT WINAPI
IDirectInputWImpl_EnumDevices(
276 LPDIRECTINPUT7W iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKW lpCallback
,
277 LPVOID pvRef
, DWORD dwFlags
)
279 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
280 DIDEVICEINSTANCEW devInstance
;
283 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
284 This
, dwDevType
, _dump_DIDEVTYPE_value(dwDevType
),
285 lpCallback
, pvRef
, dwFlags
);
286 _dump_EnumDevices_dwFlags(dwFlags
);
288 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++) {
289 if (!dinput_devices
[i
]->enum_deviceW
) continue;
290 for (j
= 0, r
= -1; r
!= 0; j
++) {
291 devInstance
.dwSize
= sizeof(devInstance
);
292 TRACE(" - checking device %d ('%s')\n", i
, dinput_devices
[i
]->name
);
293 if ((r
= dinput_devices
[i
]->enum_deviceW(dwDevType
, dwFlags
, &devInstance
, This
->dwVersion
, j
))) {
294 if (lpCallback(&devInstance
,pvRef
) == DIENUM_STOP
)
303 static ULONG WINAPI
IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface
)
305 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
306 ULONG ref
= InterlockedIncrement(&This
->ref
);
308 TRACE( "(%p) incrementing from %d\n", This
, ref
- 1);
312 static ULONG WINAPI
IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface
)
314 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
315 return IDirectInputAImpl_AddRef( (IDirectInput7A
*)This
);
318 static ULONG WINAPI
IDirectInputAImpl_Release(LPDIRECTINPUT7A iface
)
320 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
321 ULONG ref
= InterlockedDecrement( &This
->ref
);
323 TRACE( "(%p) releasing from %d\n", This
, ref
+ 1 );
327 /* Remove self from the list of the IDirectInputs */
328 EnterCriticalSection( &dinput_hook_crit
);
329 list_remove( &This
->entry
);
330 LeaveCriticalSection( &dinput_hook_crit
);
334 This
->crit
.DebugInfo
->Spare
[0] = 0;
335 DeleteCriticalSection( &This
->crit
);
336 HeapFree( GetProcessHeap(), 0, This
);
341 static ULONG WINAPI
IDirectInputWImpl_Release(LPDIRECTINPUT7W iface
)
343 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
344 return IDirectInputAImpl_Release( (IDirectInput7A
*)This
);
347 static HRESULT WINAPI
IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface
, REFIID riid
, LPVOID
*ppobj
)
349 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
351 TRACE( "(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), ppobj
);
353 if (IsEqualGUID( &IID_IUnknown
, riid
) ||
354 IsEqualGUID( &IID_IDirectInputA
, riid
) ||
355 IsEqualGUID( &IID_IDirectInput2A
, riid
) ||
356 IsEqualGUID( &IID_IDirectInput7A
, riid
))
358 *ppobj
= &This
->lpVtbl
;
359 IUnknown_AddRef( (IUnknown
*)*ppobj
);
364 if (IsEqualGUID( &IID_IDirectInputW
, riid
) ||
365 IsEqualGUID( &IID_IDirectInput2W
, riid
) ||
366 IsEqualGUID( &IID_IDirectInput7W
, riid
))
368 *ppobj
= &This
->lpVtbl7w
;
369 IUnknown_AddRef( (IUnknown
*)*ppobj
);
374 if (IsEqualGUID( &IID_IDirectInput8A
, riid
))
376 *ppobj
= &This
->lpVtbl8a
;
377 IUnknown_AddRef( (IUnknown
*)*ppobj
);
382 if (IsEqualGUID( &IID_IDirectInput8W
, riid
))
384 *ppobj
= &This
->lpVtbl8w
;
385 IUnknown_AddRef( (IUnknown
*)*ppobj
);
390 FIXME( "Unsupported interface !\n" );
394 static HRESULT WINAPI
IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface
, REFIID riid
, LPVOID
*ppobj
)
396 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
397 return IDirectInputAImpl_QueryInterface( (IDirectInput7A
*)This
, riid
, ppobj
);
400 static HRESULT WINAPI
IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface
, HINSTANCE hinst
, DWORD x
) {
401 TRACE("(this=%p,%p,%x)\n",iface
, hinst
, x
);
403 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
404 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
405 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
410 static HRESULT WINAPI
IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface
, HINSTANCE hinst
, DWORD x
)
412 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
413 return IDirectInputAImpl_Initialize( (IDirectInput7A
*)This
, hinst
, x
);
416 static HRESULT WINAPI
IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface
, REFGUID rguid
)
418 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
420 FIXME( "(%p)->(%s): stub\n", This
, debugstr_guid(rguid
) );
425 static HRESULT WINAPI
IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface
, REFGUID rguid
)
427 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
428 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A
*)This
, rguid
);
431 static HRESULT WINAPI
IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface
,
435 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
437 FIXME( "(%p)->(%p,%08x): stub\n", This
, hwndOwner
, dwFlags
);
442 static HRESULT WINAPI
IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface
, HWND hwndOwner
, DWORD dwFlags
)
444 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
445 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A
*)This
, hwndOwner
, dwFlags
);
448 static HRESULT WINAPI
IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface
, REFGUID rguid
,
449 LPCSTR pszName
, LPGUID pguidInstance
)
451 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
453 FIXME( "(%p)->(%s, %s, %p): stub\n", This
, debugstr_guid(rguid
), pszName
, pguidInstance
);
458 static HRESULT WINAPI
IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface
, REFGUID rguid
,
459 LPCWSTR pszName
, LPGUID pguidInstance
)
461 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
463 FIXME( "(%p)->(%s, %s, %p): stub\n", This
, debugstr_guid(rguid
), debugstr_w(pszName
), pguidInstance
);
468 static HRESULT WINAPI
IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface
, REFGUID rguid
,
469 REFIID riid
, LPVOID
* pvOut
, LPUNKNOWN lpUnknownOuter
)
471 IDirectInputImpl
*This
= (IDirectInputImpl
*)iface
;
472 HRESULT ret_value
= DIERR_DEVICENOTREG
;
475 TRACE("(%p)->(%s, %s, %p, %p)\n", This
, debugstr_guid(rguid
), debugstr_guid(riid
), pvOut
, lpUnknownOuter
);
477 if (!rguid
|| !pvOut
) return E_POINTER
;
479 /* Loop on all the devices to see if anyone matches the given GUID */
480 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++) {
483 if (!dinput_devices
[i
]->create_deviceA
) continue;
484 if ((ret
= dinput_devices
[i
]->create_deviceA(This
, rguid
, riid
, (LPDIRECTINPUTDEVICEA
*) pvOut
)) == DI_OK
)
486 EnterCriticalSection( &This
->crit
);
487 list_add_tail( &This
->devices_list
, &(*(IDirectInputDevice2AImpl
**)pvOut
)->entry
);
488 LeaveCriticalSection( &This
->crit
);
492 if (ret
== DIERR_NOINTERFACE
)
493 ret_value
= DIERR_NOINTERFACE
;
496 if (ret_value
== DIERR_NOINTERFACE
)
498 WARN("invalid device GUID %s\n", debugstr_guid(rguid
));
504 static HRESULT WINAPI
IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface
, REFGUID rguid
,
505 REFIID riid
, LPVOID
* pvOut
, LPUNKNOWN lpUnknownOuter
)
507 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
508 HRESULT ret_value
= DIERR_DEVICENOTREG
;
511 TRACE("(%p)->(%s, %s, %p, %p)\n", This
, debugstr_guid(rguid
), debugstr_guid(riid
), pvOut
, lpUnknownOuter
);
513 if (!rguid
|| !pvOut
) return E_POINTER
;
515 /* Loop on all the devices to see if anyone matches the given GUID */
516 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++) {
519 if (!dinput_devices
[i
]->create_deviceW
) continue;
520 if ((ret
= dinput_devices
[i
]->create_deviceW(This
, rguid
, riid
, (LPDIRECTINPUTDEVICEW
*) pvOut
)) == DI_OK
)
522 EnterCriticalSection( &This
->crit
);
523 list_add_tail( &This
->devices_list
, &(*(IDirectInputDevice2AImpl
**)pvOut
)->entry
);
524 LeaveCriticalSection( &This
->crit
);
528 if (ret
== DIERR_NOINTERFACE
)
529 ret_value
= DIERR_NOINTERFACE
;
535 static HRESULT WINAPI
IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface
, REFGUID rguid
,
536 LPDIRECTINPUTDEVICEA
* pdev
, LPUNKNOWN punk
)
538 return IDirectInput7AImpl_CreateDeviceEx(iface
, rguid
, NULL
, (LPVOID
*)pdev
, punk
);
541 static HRESULT WINAPI
IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface
, REFGUID rguid
,
542 LPDIRECTINPUTDEVICEW
* pdev
, LPUNKNOWN punk
)
544 return IDirectInput7WImpl_CreateDeviceEx(iface
, rguid
, NULL
, (LPVOID
*)pdev
, punk
);
547 /*******************************************************************************
551 static ULONG WINAPI
IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface
)
553 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
554 return IDirectInputAImpl_AddRef( (IDirectInput7A
*)This
);
557 static ULONG WINAPI
IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface
)
559 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
560 return IDirectInputAImpl_AddRef( (IDirectInput7A
*)This
);
563 static HRESULT WINAPI
IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface
, REFIID riid
, LPVOID
*ppobj
)
565 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
566 return IDirectInputAImpl_QueryInterface( (IDirectInput7A
*)This
, riid
, ppobj
);
569 static HRESULT WINAPI
IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface
, REFIID riid
, LPVOID
*ppobj
)
571 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
572 return IDirectInputAImpl_QueryInterface( (IDirectInput7A
*)This
, riid
, ppobj
);
575 static ULONG WINAPI
IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface
)
577 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
578 return IDirectInputAImpl_Release( (IDirectInput7A
*)This
);
581 static ULONG WINAPI
IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface
)
583 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
584 return IDirectInputAImpl_Release( (IDirectInput7A
*)This
);
587 static HRESULT WINAPI
IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface
, REFGUID rguid
,
588 LPDIRECTINPUTDEVICE8A
* pdev
, LPUNKNOWN punk
)
590 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
591 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A
*)This
, rguid
, NULL
, (LPVOID
*)pdev
, punk
);
594 static HRESULT WINAPI
IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface
, REFGUID rguid
,
595 LPDIRECTINPUTDEVICE8W
* pdev
, LPUNKNOWN punk
)
597 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
598 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This
), rguid
, NULL
, (LPVOID
*)pdev
, punk
);
601 static HRESULT WINAPI
IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKA lpCallback
,
602 LPVOID pvRef
, DWORD dwFlags
)
604 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
605 return IDirectInputAImpl_EnumDevices( (IDirectInput7A
*)This
, dwDevType
, lpCallback
, pvRef
, dwFlags
);
608 static HRESULT WINAPI
IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKW lpCallback
,
609 LPVOID pvRef
, DWORD dwFlags
)
611 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
612 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This
), dwDevType
, lpCallback
, pvRef
, dwFlags
);
615 static HRESULT WINAPI
IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface
, REFGUID rguid
)
617 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
618 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A
*)This
, rguid
);
621 static HRESULT WINAPI
IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface
, REFGUID rguid
)
623 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
624 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A
*)This
, rguid
);
627 static HRESULT WINAPI
IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface
, HWND hwndOwner
, DWORD dwFlags
)
629 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
630 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A
*)This
, hwndOwner
, dwFlags
);
633 static HRESULT WINAPI
IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface
, HWND hwndOwner
, DWORD dwFlags
)
635 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
636 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A
*)This
, hwndOwner
, dwFlags
);
639 static HRESULT WINAPI
IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface
, HINSTANCE hinst
, DWORD x
)
641 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
642 return IDirectInputAImpl_Initialize( (IDirectInput7A
*)This
, hinst
, x
);
645 static HRESULT WINAPI
IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface
, HINSTANCE hinst
, DWORD x
)
647 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
648 return IDirectInputAImpl_Initialize( (IDirectInput7A
*)This
, hinst
, x
);
651 static HRESULT WINAPI
IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface
, REFGUID rguid
, LPCSTR pszName
, LPGUID pguidInstance
)
653 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
654 return IDirectInput2AImpl_FindDevice( (IDirectInput7A
*)This
, rguid
, pszName
, pguidInstance
);
657 static HRESULT WINAPI
IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface
, REFGUID rguid
, LPCWSTR pszName
, LPGUID pguidInstance
)
659 IDirectInput7W
*This
= IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface
) );
660 return IDirectInput2WImpl_FindDevice( This
, rguid
, pszName
, pguidInstance
);
663 static HRESULT WINAPI
IDirectInput8AImpl_EnumDevicesBySemantics(
664 LPDIRECTINPUT8A iface
, LPCSTR ptszUserName
, LPDIACTIONFORMATA lpdiActionFormat
,
665 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback
,
666 LPVOID pvRef
, DWORD dwFlags
669 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
671 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This
, ptszUserName
, lpdiActionFormat
,
672 lpCallback
, pvRef
, dwFlags
);
673 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
674 X(DIEDBSFL_ATTACHEDONLY
)
676 X(DIEDBSFL_FORCEFEEDBACK
)
677 X(DIEDBSFL_AVAILABLEDEVICES
)
678 X(DIEDBSFL_MULTIMICEKEYBOARDS
)
679 X(DIEDBSFL_NONGAMINGDEVICES
)
682 _dump_diactionformatA(lpdiActionFormat
);
687 static HRESULT WINAPI
IDirectInput8WImpl_EnumDevicesBySemantics(
688 LPDIRECTINPUT8W iface
, LPCWSTR ptszUserName
, LPDIACTIONFORMATW lpdiActionFormat
,
689 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback
,
690 LPVOID pvRef
, DWORD dwFlags
693 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
695 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This
, debugstr_w(ptszUserName
), lpdiActionFormat
,
696 lpCallback
, pvRef
, dwFlags
);
700 static HRESULT WINAPI
IDirectInput8AImpl_ConfigureDevices(
701 LPDIRECTINPUT8A iface
, LPDICONFIGUREDEVICESCALLBACK lpdiCallback
,
702 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams
, DWORD dwFlags
, LPVOID pvRefData
705 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
707 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This
, lpdiCallback
, lpdiCDParams
,
712 static HRESULT WINAPI
IDirectInput8WImpl_ConfigureDevices(
713 LPDIRECTINPUT8W iface
, LPDICONFIGUREDEVICESCALLBACK lpdiCallback
,
714 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams
, DWORD dwFlags
, LPVOID pvRefData
717 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
719 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This
, lpdiCallback
, lpdiCDParams
,
724 static const IDirectInput7AVtbl ddi7avt
= {
725 IDirectInputAImpl_QueryInterface
,
726 IDirectInputAImpl_AddRef
,
727 IDirectInputAImpl_Release
,
728 IDirectInputAImpl_CreateDevice
,
729 IDirectInputAImpl_EnumDevices
,
730 IDirectInputAImpl_GetDeviceStatus
,
731 IDirectInputAImpl_RunControlPanel
,
732 IDirectInputAImpl_Initialize
,
733 IDirectInput2AImpl_FindDevice
,
734 IDirectInput7AImpl_CreateDeviceEx
737 static const IDirectInput7WVtbl ddi7wvt
= {
738 IDirectInputWImpl_QueryInterface
,
739 IDirectInputWImpl_AddRef
,
740 IDirectInputWImpl_Release
,
741 IDirectInputWImpl_CreateDevice
,
742 IDirectInputWImpl_EnumDevices
,
743 IDirectInputWImpl_GetDeviceStatus
,
744 IDirectInputWImpl_RunControlPanel
,
745 IDirectInputWImpl_Initialize
,
746 IDirectInput2WImpl_FindDevice
,
747 IDirectInput7WImpl_CreateDeviceEx
750 static const IDirectInput8AVtbl ddi8avt
= {
751 IDirectInput8AImpl_QueryInterface
,
752 IDirectInput8AImpl_AddRef
,
753 IDirectInput8AImpl_Release
,
754 IDirectInput8AImpl_CreateDevice
,
755 IDirectInput8AImpl_EnumDevices
,
756 IDirectInput8AImpl_GetDeviceStatus
,
757 IDirectInput8AImpl_RunControlPanel
,
758 IDirectInput8AImpl_Initialize
,
759 IDirectInput8AImpl_FindDevice
,
760 IDirectInput8AImpl_EnumDevicesBySemantics
,
761 IDirectInput8AImpl_ConfigureDevices
764 static const IDirectInput8WVtbl ddi8wvt
= {
765 IDirectInput8WImpl_QueryInterface
,
766 IDirectInput8WImpl_AddRef
,
767 IDirectInput8WImpl_Release
,
768 IDirectInput8WImpl_CreateDevice
,
769 IDirectInput8WImpl_EnumDevices
,
770 IDirectInput8WImpl_GetDeviceStatus
,
771 IDirectInput8WImpl_RunControlPanel
,
772 IDirectInput8WImpl_Initialize
,
773 IDirectInput8WImpl_FindDevice
,
774 IDirectInput8WImpl_EnumDevicesBySemantics
,
775 IDirectInput8WImpl_ConfigureDevices
778 /*******************************************************************************
779 * DirectInput ClassFactory
783 /* IUnknown fields */
784 const IClassFactoryVtbl
*lpVtbl
;
788 static HRESULT WINAPI
DICF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
,LPVOID
*ppobj
) {
789 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
791 FIXME("(%p)->(%s,%p),stub!\n",This
,debugstr_guid(riid
),ppobj
);
792 return E_NOINTERFACE
;
795 static ULONG WINAPI
DICF_AddRef(LPCLASSFACTORY iface
) {
796 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
797 return InterlockedIncrement(&(This
->ref
));
800 static ULONG WINAPI
DICF_Release(LPCLASSFACTORY iface
) {
801 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
802 /* static class, won't be freed */
803 return InterlockedDecrement(&(This
->ref
));
806 static HRESULT WINAPI
DICF_CreateInstance(
807 LPCLASSFACTORY iface
,LPUNKNOWN pOuter
,REFIID riid
,LPVOID
*ppobj
809 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
811 TRACE("(%p)->(%p,%s,%p)\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
812 if ( IsEqualGUID( &IID_IDirectInputA
, riid
) ||
813 IsEqualGUID( &IID_IDirectInputW
, riid
) ||
814 IsEqualGUID( &IID_IDirectInput2A
, riid
) ||
815 IsEqualGUID( &IID_IDirectInput2W
, riid
) ||
816 IsEqualGUID( &IID_IDirectInput7A
, riid
) ||
817 IsEqualGUID( &IID_IDirectInput7W
, riid
) ||
818 IsEqualGUID( &IID_IDirectInput8A
, riid
) ||
819 IsEqualGUID( &IID_IDirectInput8W
, riid
) ) {
820 /* FIXME: reuse already created dinput if present? */
821 return DirectInputCreateEx(0,0,riid
,ppobj
,pOuter
);
824 FIXME("(%p,%p,%s,%p) Interface not found!\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
825 return E_NOINTERFACE
;
828 static HRESULT WINAPI
DICF_LockServer(LPCLASSFACTORY iface
,BOOL dolock
) {
829 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
830 FIXME("(%p)->(%d),stub!\n",This
,dolock
);
834 static const IClassFactoryVtbl DICF_Vtbl
= {
841 static IClassFactoryImpl DINPUT_CF
= {&DICF_Vtbl
, 1 };
843 /***********************************************************************
844 * DllCanUnloadNow (DINPUT.@)
846 HRESULT WINAPI
DllCanUnloadNow(void)
848 FIXME("(void): stub\n");
853 /***********************************************************************
854 * DllGetClassObject (DINPUT.@)
856 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
858 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
859 if ( IsEqualCLSID( &IID_IClassFactory
, riid
) ) {
860 *ppv
= (LPVOID
)&DINPUT_CF
;
861 IClassFactory_AddRef((IClassFactory
*)*ppv
);
865 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
866 return CLASS_E_CLASSNOTAVAILABLE
;
869 /******************************************************************************
873 static LRESULT CALLBACK
LL_hook_proc( int code
, WPARAM wparam
, LPARAM lparam
)
875 IDirectInputImpl
*dinput
;
877 if (code
!= HC_ACTION
) return CallNextHookEx( 0, code
, wparam
, lparam
);
879 EnterCriticalSection( &dinput_hook_crit
);
880 LIST_FOR_EACH_ENTRY( dinput
, &direct_input_list
, IDirectInputImpl
, entry
)
882 IDirectInputDevice2AImpl
*dev
;
884 EnterCriticalSection( &dinput
->crit
);
885 LIST_FOR_EACH_ENTRY( dev
, &dinput
->devices_list
, IDirectInputDevice2AImpl
, entry
)
886 if (dev
->acquired
&& dev
->event_proc
)
888 TRACE("calling %p->%p (%lx %lx)\n", dev
, dev
->event_proc
, wparam
, lparam
);
889 dev
->event_proc( (LPDIRECTINPUTDEVICE8A
)dev
, wparam
, lparam
);
891 LeaveCriticalSection( &dinput
->crit
);
893 LeaveCriticalSection( &dinput_hook_crit
);
895 return CallNextHookEx( 0, code
, wparam
, lparam
);
898 static LRESULT CALLBACK
callwndproc_proc( int code
, WPARAM wparam
, LPARAM lparam
)
900 CWPSTRUCT
*msg
= (CWPSTRUCT
*)lparam
;
901 IDirectInputImpl
*dinput
;
904 if (code
!= HC_ACTION
|| (msg
->message
!= WM_KILLFOCUS
&&
905 msg
->message
!= WM_ACTIVATEAPP
&& msg
->message
!= WM_ACTIVATE
))
906 return CallNextHookEx( 0, code
, wparam
, lparam
);
908 foreground
= GetForegroundWindow();
910 EnterCriticalSection( &dinput_hook_crit
);
912 LIST_FOR_EACH_ENTRY( dinput
, &direct_input_list
, IDirectInputImpl
, entry
)
914 IDirectInputDevice2AImpl
*dev
;
916 EnterCriticalSection( &dinput
->crit
);
917 LIST_FOR_EACH_ENTRY( dev
, &dinput
->devices_list
, IDirectInputDevice2AImpl
, entry
)
919 if (!dev
->acquired
) continue;
921 if (msg
->hwnd
== dev
->win
&& msg
->hwnd
!= foreground
)
923 TRACE( "%p window is not foreground - unacquiring %p\n", dev
->win
, dev
);
924 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A
)dev
);
927 LeaveCriticalSection( &dinput
->crit
);
929 LeaveCriticalSection( &dinput_hook_crit
);
931 return CallNextHookEx( 0, code
, wparam
, lparam
);
934 static DWORD WINAPI
hook_thread_proc(void *param
)
936 static HHOOK kbd_hook
, mouse_hook
;
939 /* Force creation of the message queue */
940 PeekMessageW( &msg
, 0, 0, 0, PM_NOREMOVE
);
941 SetEvent(*(LPHANDLE
)param
);
943 while (GetMessageW( &msg
, 0, 0, 0 ))
945 UINT kbd_cnt
= 0, mice_cnt
= 0;
947 if (msg
.message
== WM_USER
+0x10)
949 IDirectInputImpl
*dinput
;
951 TRACE( "Processing hook change notification lp:%ld\n", msg
.lParam
);
953 if (!msg
.wParam
&& !msg
.lParam
)
955 if (kbd_hook
) UnhookWindowsHookEx( kbd_hook
);
956 if (mouse_hook
) UnhookWindowsHookEx( mouse_hook
);
957 kbd_hook
= mouse_hook
= NULL
;
961 EnterCriticalSection( &dinput_hook_crit
);
963 /* Count acquired keyboards and mice*/
964 LIST_FOR_EACH_ENTRY( dinput
, &direct_input_list
, IDirectInputImpl
, entry
)
966 IDirectInputDevice2AImpl
*dev
;
968 EnterCriticalSection( &dinput
->crit
);
969 LIST_FOR_EACH_ENTRY( dev
, &dinput
->devices_list
, IDirectInputDevice2AImpl
, entry
)
971 if (!dev
->acquired
|| !dev
->event_proc
) continue;
973 if (IsEqualGUID( &dev
->guid
, &GUID_SysKeyboard
) ||
974 IsEqualGUID( &dev
->guid
, &DInput_Wine_Keyboard_GUID
))
977 if (IsEqualGUID( &dev
->guid
, &GUID_SysMouse
) ||
978 IsEqualGUID( &dev
->guid
, &DInput_Wine_Mouse_GUID
))
981 LeaveCriticalSection( &dinput
->crit
);
983 LeaveCriticalSection( &dinput_hook_crit
);
985 if (kbd_cnt
&& !kbd_hook
)
986 kbd_hook
= SetWindowsHookExW( WH_KEYBOARD_LL
, LL_hook_proc
, DINPUT_instance
, 0 );
987 else if (!kbd_cnt
&& kbd_hook
)
989 UnhookWindowsHookEx( kbd_hook
);
993 if (mice_cnt
&& !mouse_hook
)
994 mouse_hook
= SetWindowsHookExW( WH_MOUSE_LL
, LL_hook_proc
, DINPUT_instance
, 0 );
995 else if (!mice_cnt
&& mouse_hook
)
997 UnhookWindowsHookEx( mouse_hook
);
1001 TranslateMessage(&msg
);
1002 DispatchMessageW(&msg
);
1008 static DWORD hook_thread_id
;
1010 static CRITICAL_SECTION_DEBUG dinput_critsect_debug
=
1012 0, 0, &dinput_hook_crit
,
1013 { &dinput_critsect_debug
.ProcessLocksList
, &dinput_critsect_debug
.ProcessLocksList
},
1014 0, 0, { (DWORD_PTR
)(__FILE__
": dinput_hook_crit") }
1016 static CRITICAL_SECTION dinput_hook_crit
= { &dinput_critsect_debug
, -1, 0, 0, 0, 0 };
1018 static BOOL
check_hook_thread(void)
1020 static HANDLE hook_thread
;
1022 EnterCriticalSection(&dinput_hook_crit
);
1024 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list
));
1025 if (!list_empty(&direct_input_list
) && !hook_thread
)
1029 event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1030 hook_thread
= CreateThread(NULL
, 0, hook_thread_proc
, &event
, 0, &hook_thread_id
);
1031 if (event
&& hook_thread
)
1035 handles
[1] = hook_thread
;
1036 WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1038 LeaveCriticalSection(&dinput_hook_crit
);
1041 else if (list_empty(&direct_input_list
) && hook_thread
)
1043 DWORD tid
= hook_thread_id
;
1046 PostThreadMessageW(tid
, WM_USER
+0x10, 0, 0);
1047 LeaveCriticalSection(&dinput_hook_crit
);
1049 /* wait for hook thread to exit */
1050 WaitForSingleObject(hook_thread
, INFINITE
);
1051 CloseHandle(hook_thread
);
1055 LeaveCriticalSection(&dinput_hook_crit
);
1057 return hook_thread_id
!= 0;
1060 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface
)
1062 static HHOOK callwndproc_hook
;
1063 static ULONG foreground_cnt
;
1064 IDirectInputDevice2AImpl
*dev
= (IDirectInputDevice2AImpl
*)iface
;
1066 EnterCriticalSection(&dinput_hook_crit
);
1068 if (dev
->dwCoopLevel
& DISCL_FOREGROUND
)
1076 if (foreground_cnt
&& !callwndproc_hook
)
1077 callwndproc_hook
= SetWindowsHookExW( WH_CALLWNDPROC
, callwndproc_proc
,
1078 DINPUT_instance
, GetCurrentThreadId() );
1079 else if (!foreground_cnt
&& callwndproc_hook
)
1081 UnhookWindowsHookEx( callwndproc_hook
);
1082 callwndproc_hook
= NULL
;
1085 PostThreadMessageW( hook_thread_id
, WM_USER
+0x10, 1, 0 );
1087 LeaveCriticalSection(&dinput_hook_crit
);