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 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 /******************************************************************************
214 * IDirectInputA_EnumDevices
216 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
217 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
218 LPVOID pvRef, DWORD dwFlags)
220 IDirectInputImpl *This = (IDirectInputImpl *)iface;
221 DIDEVICEINSTANCEA devInstance;
224 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
225 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
226 lpCallback, pvRef, dwFlags);
227 _dump_EnumDevices_dwFlags(dwFlags);
229 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
230 if (!dinput_devices[i]->enum_deviceA) continue;
231 for (j = 0, r = -1; r != 0; j++) {
232 devInstance.dwSize = sizeof(devInstance);
233 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
234 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
235 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
243 /******************************************************************************
244 * IDirectInputW_EnumDevices
246 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
247 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
248 LPVOID pvRef, DWORD dwFlags)
250 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
251 DIDEVICEINSTANCEW devInstance;
254 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
255 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
256 lpCallback, pvRef, dwFlags);
257 _dump_EnumDevices_dwFlags(dwFlags);
259 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
260 if (!dinput_devices[i]->enum_deviceW) continue;
261 for (j = 0, r = -1; r != 0; j++) {
262 devInstance.dwSize = sizeof(devInstance);
263 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
264 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
265 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
274 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
276 IDirectInputImpl *This = (IDirectInputImpl *)iface;
277 ULONG ref = InterlockedIncrement(&This->ref);
279 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
283 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
285 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
286 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
289 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
291 IDirectInputImpl *This = (IDirectInputImpl *)iface;
292 ULONG ref = InterlockedDecrement( &This->ref );
294 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
298 /* Remove self from the list of the IDirectInputs */
299 EnterCriticalSection( &dinput_hook_crit );
300 list_remove( &This->entry );
301 LeaveCriticalSection( &dinput_hook_crit );
305 This->crit.DebugInfo->Spare[0] = 0;
306 DeleteCriticalSection( &This->crit );
307 HeapFree( GetProcessHeap(), 0, This );
312 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
314 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
315 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
318 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
320 IDirectInputImpl *This = (IDirectInputImpl *)iface;
322 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
324 if (IsEqualGUID( &IID_IUnknown, riid ) ||
325 IsEqualGUID( &IID_IDirectInputA, riid ) ||
326 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
327 IsEqualGUID( &IID_IDirectInput7A, riid ))
329 *ppobj = &This->lpVtbl;
330 IUnknown_AddRef( (IUnknown*)*ppobj );
335 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
336 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
337 IsEqualGUID( &IID_IDirectInput7W, riid ))
339 *ppobj = &This->lpVtbl7w;
340 IUnknown_AddRef( (IUnknown*)*ppobj );
345 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
347 *ppobj = &This->lpVtbl8a;
348 IUnknown_AddRef( (IUnknown*)*ppobj );
353 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
355 *ppobj = &This->lpVtbl8w;
356 IUnknown_AddRef( (IUnknown*)*ppobj );
361 FIXME( "Unsupported interface !\n" );
365 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
367 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
368 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
371 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
372 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
374 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
375 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
376 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
381 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
383 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
384 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
387 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
389 IDirectInputImpl *This = (IDirectInputImpl *)iface;
391 FIXME( "(%p)->(%s): stub\n", This, debugstr_guid(rguid) );
396 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
398 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
399 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
402 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
406 IDirectInputImpl *This = (IDirectInputImpl *)iface;
408 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
413 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
415 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
416 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
419 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
420 LPCSTR pszName, LPGUID pguidInstance)
422 IDirectInputImpl *This = (IDirectInputImpl *)iface;
424 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
429 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
430 LPCWSTR pszName, LPGUID pguidInstance)
432 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
434 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
439 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
440 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
442 IDirectInputImpl *This = (IDirectInputImpl *)iface;
443 HRESULT ret_value = DIERR_DEVICENOTREG;
446 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
448 if (!rguid || !pvOut) return E_POINTER;
450 /* Loop on all the devices to see if anyone matches the given GUID */
451 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
454 if (!dinput_devices[i]->create_deviceA) continue;
455 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
457 EnterCriticalSection( &This->crit );
458 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
459 LeaveCriticalSection( &This->crit );
463 if (ret == DIERR_NOINTERFACE)
464 ret_value = DIERR_NOINTERFACE;
467 if (ret_value == DIERR_NOINTERFACE)
469 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
475 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
476 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
478 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
479 HRESULT ret_value = DIERR_DEVICENOTREG;
482 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
484 if (!rguid || !pvOut) return E_POINTER;
486 /* Loop on all the devices to see if anyone matches the given GUID */
487 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
490 if (!dinput_devices[i]->create_deviceW) continue;
491 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
493 EnterCriticalSection( &This->crit );
494 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
495 LeaveCriticalSection( &This->crit );
499 if (ret == DIERR_NOINTERFACE)
500 ret_value = DIERR_NOINTERFACE;
506 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
507 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
509 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
512 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
513 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
515 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
518 /*******************************************************************************
522 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
524 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
525 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
528 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
530 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
531 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
534 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
536 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
537 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
540 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
542 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
543 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
546 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
548 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
549 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
552 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
554 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
555 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
558 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
559 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
561 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
562 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
565 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
566 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
568 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
569 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
572 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
573 LPVOID pvRef, DWORD dwFlags)
575 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
576 return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
579 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
580 LPVOID pvRef, DWORD dwFlags)
582 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
583 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
586 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
588 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
589 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
592 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
594 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
595 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
598 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
600 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
601 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
604 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
606 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
607 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
610 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
612 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
613 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
616 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
618 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
619 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
622 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
624 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
625 return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
628 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
630 IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
631 return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
634 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
635 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
636 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
637 LPVOID pvRef, DWORD dwFlags
640 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
642 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
643 lpCallback, pvRef, dwFlags);
647 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
648 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
649 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
650 LPVOID pvRef, DWORD dwFlags
653 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
655 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
656 lpCallback, pvRef, dwFlags);
660 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
661 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
662 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
665 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
667 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
672 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
673 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
674 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
677 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
679 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
684 static const IDirectInput7AVtbl ddi7avt = {
685 IDirectInputAImpl_QueryInterface,
686 IDirectInputAImpl_AddRef,
687 IDirectInputAImpl_Release,
688 IDirectInputAImpl_CreateDevice,
689 IDirectInputAImpl_EnumDevices,
690 IDirectInputAImpl_GetDeviceStatus,
691 IDirectInputAImpl_RunControlPanel,
692 IDirectInputAImpl_Initialize,
693 IDirectInput2AImpl_FindDevice,
694 IDirectInput7AImpl_CreateDeviceEx
697 static const IDirectInput7WVtbl ddi7wvt = {
698 IDirectInputWImpl_QueryInterface,
699 IDirectInputWImpl_AddRef,
700 IDirectInputWImpl_Release,
701 IDirectInputWImpl_CreateDevice,
702 IDirectInputWImpl_EnumDevices,
703 IDirectInputWImpl_GetDeviceStatus,
704 IDirectInputWImpl_RunControlPanel,
705 IDirectInputWImpl_Initialize,
706 IDirectInput2WImpl_FindDevice,
707 IDirectInput7WImpl_CreateDeviceEx
710 static const IDirectInput8AVtbl ddi8avt = {
711 IDirectInput8AImpl_QueryInterface,
712 IDirectInput8AImpl_AddRef,
713 IDirectInput8AImpl_Release,
714 IDirectInput8AImpl_CreateDevice,
715 IDirectInput8AImpl_EnumDevices,
716 IDirectInput8AImpl_GetDeviceStatus,
717 IDirectInput8AImpl_RunControlPanel,
718 IDirectInput8AImpl_Initialize,
719 IDirectInput8AImpl_FindDevice,
720 IDirectInput8AImpl_EnumDevicesBySemantics,
721 IDirectInput8AImpl_ConfigureDevices
724 static const IDirectInput8WVtbl ddi8wvt = {
725 IDirectInput8WImpl_QueryInterface,
726 IDirectInput8WImpl_AddRef,
727 IDirectInput8WImpl_Release,
728 IDirectInput8WImpl_CreateDevice,
729 IDirectInput8WImpl_EnumDevices,
730 IDirectInput8WImpl_GetDeviceStatus,
731 IDirectInput8WImpl_RunControlPanel,
732 IDirectInput8WImpl_Initialize,
733 IDirectInput8WImpl_FindDevice,
734 IDirectInput8WImpl_EnumDevicesBySemantics,
735 IDirectInput8WImpl_ConfigureDevices
738 /*******************************************************************************
739 * DirectInput ClassFactory
743 /* IUnknown fields */
744 const IClassFactoryVtbl *lpVtbl;
748 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
749 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
751 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
752 return E_NOINTERFACE;
755 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
756 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
757 return InterlockedIncrement(&(This->ref));
760 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
761 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
762 /* static class, won't be freed */
763 return InterlockedDecrement(&(This->ref));
766 static HRESULT WINAPI DICF_CreateInstance(
767 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
769 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
771 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
772 if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
773 IsEqualGUID( &IID_IDirectInputW, riid ) ||
774 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
775 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
776 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
777 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
778 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
779 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
780 /* FIXME: reuse already created dinput if present? */
781 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
784 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
785 return E_NOINTERFACE;
788 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
789 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
790 FIXME("(%p)->(%d),stub!\n",This,dolock);
794 static const IClassFactoryVtbl DICF_Vtbl = {
801 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
803 /***********************************************************************
804 * DllCanUnloadNow (DINPUT.@)
806 HRESULT WINAPI DllCanUnloadNow(void)
808 FIXME("(void): stub\n");
813 /***********************************************************************
814 * DllGetClassObject (DINPUT.@)
816 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
818 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
819 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
820 *ppv = (LPVOID)&DINPUT_CF;
821 IClassFactory_AddRef((IClassFactory*)*ppv);
825 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
826 return CLASS_E_CLASSNOTAVAILABLE;
829 /******************************************************************************
833 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
835 IDirectInputImpl *dinput;
837 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
839 EnterCriticalSection( &dinput_hook_crit );
840 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
842 IDirectInputDevice2AImpl *dev;
844 EnterCriticalSection( &dinput->crit );
845 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
846 if (dev->acquired && dev->event_proc)
848 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
849 dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
851 LeaveCriticalSection( &dinput->crit );
853 LeaveCriticalSection( &dinput_hook_crit );
855 return CallNextHookEx( 0, code, wparam, lparam );
858 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
860 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
861 IDirectInputImpl *dinput;
864 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
865 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
866 return CallNextHookEx( 0, code, wparam, lparam );
868 foreground = GetForegroundWindow();
870 EnterCriticalSection( &dinput_hook_crit );
872 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
874 IDirectInputDevice2AImpl *dev;
876 EnterCriticalSection( &dinput->crit );
877 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
879 if (!dev->acquired) continue;
881 if (msg->hwnd == dev->win && msg->hwnd != foreground)
883 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
884 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
887 LeaveCriticalSection( &dinput->crit );
889 LeaveCriticalSection( &dinput_hook_crit );
891 return CallNextHookEx( 0, code, wparam, lparam );
894 static DWORD WINAPI hook_thread_proc(void *param)
896 static HHOOK kbd_hook, mouse_hook;
899 /* Force creation of the message queue */
900 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
901 SetEvent(*(LPHANDLE)param);
903 while (GetMessageW( &msg, 0, 0, 0 ))
905 UINT kbd_cnt = 0, mice_cnt = 0;
907 if (msg.message == WM_USER+0x10)
909 IDirectInputImpl *dinput;
911 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
913 if (!msg.wParam && !msg.lParam)
915 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
916 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
917 kbd_hook = mouse_hook = NULL;
921 EnterCriticalSection( &dinput_hook_crit );
923 /* Count acquired keyboards and mice*/
924 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
926 IDirectInputDevice2AImpl *dev;
928 EnterCriticalSection( &dinput->crit );
929 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
931 if (!dev->acquired || !dev->event_proc) continue;
933 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
934 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
937 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
938 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
941 LeaveCriticalSection( &dinput->crit );
943 LeaveCriticalSection( &dinput_hook_crit );
945 if (kbd_cnt && !kbd_hook)
946 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
947 else if (!kbd_cnt && kbd_hook)
949 UnhookWindowsHookEx( kbd_hook );
953 if (mice_cnt && !mouse_hook)
954 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
955 else if (!mice_cnt && mouse_hook)
957 UnhookWindowsHookEx( mouse_hook );
961 TranslateMessage(&msg);
962 DispatchMessageW(&msg);
968 static DWORD hook_thread_id;
970 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
972 0, 0, &dinput_hook_crit,
973 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
974 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
976 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
978 static BOOL check_hook_thread(void)
980 static HANDLE hook_thread;
982 EnterCriticalSection(&dinput_hook_crit);
984 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
985 if (!list_empty(&direct_input_list) && !hook_thread)
989 event = CreateEventW(NULL, FALSE, FALSE, NULL);
990 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
991 if (event && hook_thread)
995 handles[1] = hook_thread;
996 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
998 LeaveCriticalSection(&dinput_hook_crit);
1001 else if (list_empty(&direct_input_list) && hook_thread)
1003 DWORD tid = hook_thread_id;
1006 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1007 LeaveCriticalSection(&dinput_hook_crit);
1009 /* wait for hook thread to exit */
1010 WaitForSingleObject(hook_thread, INFINITE);
1011 CloseHandle(hook_thread);
1015 LeaveCriticalSection(&dinput_hook_crit);
1017 return hook_thread_id != 0;
1020 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1022 static HHOOK callwndproc_hook;
1023 static ULONG foreground_cnt;
1024 IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1026 EnterCriticalSection(&dinput_hook_crit);
1028 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1036 if (foreground_cnt && !callwndproc_hook)
1037 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1038 DINPUT_instance, GetCurrentThreadId() );
1039 else if (!foreground_cnt && callwndproc_hook)
1041 UnhookWindowsHookEx( callwndproc_hook );
1042 callwndproc_hook = NULL;
1045 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1047 LeaveCriticalSection(&dinput_hook_crit);