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
39 #define NONAMELESSUNION
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
49 #include "dinput_private.h"
50 #include "device_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
54 static const IDirectInput7AVtbl ddi7avt;
55 static const IDirectInput7WVtbl ddi7wvt;
56 static const IDirectInput8AVtbl ddi8avt;
57 static const IDirectInput8WVtbl ddi8wvt;
59 static inline IDirectInputImpl *impl_from_IDirectInput7A( IDirectInput7A *iface )
61 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7A_iface );
64 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
66 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7W_iface );
69 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
71 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8A_iface );
74 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
76 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8W_iface );
79 static inline IDirectInput7W *IDirectInput7W_from_impl( IDirectInputImpl *iface )
81 return (IDirectInput7W *)(&iface->IDirectInput7W_iface);
84 static const struct dinput_device *dinput_devices[] =
88 &joystick_linuxinput_device,
89 &joystick_linux_device,
92 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
94 static HINSTANCE DINPUT_instance = NULL;
96 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
100 case DLL_PROCESS_ATTACH:
101 DisableThreadLibraryCalls(inst);
102 DINPUT_instance = inst;
104 case DLL_PROCESS_DETACH:
110 static BOOL check_hook_thread(void);
111 static CRITICAL_SECTION dinput_hook_crit;
112 static struct list direct_input_list = LIST_INIT( direct_input_list );
114 /******************************************************************************
115 * DirectInputCreateEx (DINPUT.@)
117 HRESULT WINAPI DirectInputCreateEx(
118 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
121 IDirectInputImpl* This;
123 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
125 if (IsEqualGUID( &IID_IUnknown, riid ) ||
126 IsEqualGUID( &IID_IDirectInputA, riid ) ||
127 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
128 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
129 IsEqualGUID( &IID_IDirectInputW, riid ) ||
130 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
131 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
132 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
133 IsEqualGUID( &IID_IDirectInput8W, riid ))
135 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
136 return DIERR_OUTOFMEMORY;
139 return DIERR_OLDDIRECTINPUTVERSION;
141 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
142 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
143 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
144 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
146 This->dwVersion = dwVersion;
147 This->evsequence = 1;
149 InitializeCriticalSection(&This->crit);
150 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
152 list_init( &This->devices_list );
154 /* Add self to the list of the IDirectInputs */
155 EnterCriticalSection( &dinput_hook_crit );
156 list_add_head( &direct_input_list, &This->entry );
157 LeaveCriticalSection( &dinput_hook_crit );
159 if (!check_hook_thread())
161 IUnknown_Release( (LPDIRECTINPUT7A)This );
162 return DIERR_GENERIC;
165 IDirectInput_QueryInterface( (IDirectInput7A *)This, riid, ppDI );
169 /******************************************************************************
170 * DirectInputCreateA (DINPUT.@)
172 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
174 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
177 /******************************************************************************
178 * DirectInputCreateW (DINPUT.@)
180 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
182 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
185 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
187 case 0: return "All devices";
188 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
189 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
190 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
191 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
192 default: return "Unknown";
196 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
197 if (TRACE_ON(dinput)) {
199 static const struct {
203 #define FE(x) { x, #x}
204 FE(DIEDFL_ALLDEVICES),
205 FE(DIEDFL_ATTACHEDONLY),
206 FE(DIEDFL_FORCEFEEDBACK),
207 FE(DIEDFL_INCLUDEALIASES),
208 FE(DIEDFL_INCLUDEPHANTOMS)
213 TRACE("DIEDFL_ALLDEVICES\n");
216 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
217 if (flags[i].mask & dwFlags)
218 TRACE("%s ",flags[i].name);
223 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
226 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
227 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
228 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
229 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
230 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
231 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
232 FIXME("diaf.dwGenre = 0x%08x\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 = 0x%x\n", lpdiActionFormat->dwCRC);
239 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
240 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
242 FIXME("diaf.rgoAction[%u]:\n", i);
243 FIXME("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
244 FIXME("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
245 FIXME("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
246 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
247 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
248 FIXME("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
249 FIXME("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
253 /******************************************************************************
254 * IDirectInputA_EnumDevices
256 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
257 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
258 LPVOID pvRef, DWORD dwFlags)
260 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
261 DIDEVICEINSTANCEA devInstance;
265 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
266 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
267 lpCallback, pvRef, dwFlags);
268 _dump_EnumDevices_dwFlags(dwFlags);
270 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
271 if (!dinput_devices[i]->enum_deviceA) continue;
272 for (j = 0, r = -1; r != 0; j++) {
273 devInstance.dwSize = sizeof(devInstance);
274 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
275 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
276 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
284 /******************************************************************************
285 * IDirectInputW_EnumDevices
287 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
288 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
289 LPVOID pvRef, DWORD dwFlags)
291 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
292 DIDEVICEINSTANCEW devInstance;
296 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
297 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
298 lpCallback, pvRef, dwFlags);
299 _dump_EnumDevices_dwFlags(dwFlags);
301 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
302 if (!dinput_devices[i]->enum_deviceW) continue;
303 for (j = 0, r = -1; r != 0; j++) {
304 devInstance.dwSize = sizeof(devInstance);
305 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
306 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
307 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
316 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
318 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
319 ULONG ref = InterlockedIncrement(&This->ref);
321 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
325 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
327 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
328 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
331 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
333 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
334 ULONG ref = InterlockedDecrement( &This->ref );
336 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
340 /* Remove self from the list of the IDirectInputs */
341 EnterCriticalSection( &dinput_hook_crit );
342 list_remove( &This->entry );
343 LeaveCriticalSection( &dinput_hook_crit );
347 This->crit.DebugInfo->Spare[0] = 0;
348 DeleteCriticalSection( &This->crit );
349 HeapFree( GetProcessHeap(), 0, This );
354 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
356 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
357 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
360 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
362 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
364 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
366 if (IsEqualGUID( &IID_IUnknown, riid ) ||
367 IsEqualGUID( &IID_IDirectInputA, riid ) ||
368 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
369 IsEqualGUID( &IID_IDirectInput7A, riid ))
371 *ppobj = &This->IDirectInput7A_iface;
372 IUnknown_AddRef( (IUnknown*)*ppobj );
377 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
378 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
379 IsEqualGUID( &IID_IDirectInput7W, riid ))
381 *ppobj = &This->IDirectInput7W_iface;
382 IUnknown_AddRef( (IUnknown*)*ppobj );
387 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
389 *ppobj = &This->IDirectInput8A_iface;
390 IUnknown_AddRef( (IUnknown*)*ppobj );
395 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
397 *ppobj = &This->IDirectInput8W_iface;
398 IUnknown_AddRef( (IUnknown*)*ppobj );
403 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
407 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
409 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
410 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
413 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
414 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
416 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
417 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
418 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
423 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
425 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
426 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
429 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
431 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
433 LPDIRECTINPUTDEVICEA device;
435 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
437 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
438 if (hr != DI_OK) return DI_NOTATTACHED;
440 IUnknown_Release( device );
445 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
447 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
448 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
451 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
455 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
457 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
462 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
464 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
465 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
468 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
469 LPCSTR pszName, LPGUID pguidInstance)
471 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
473 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
478 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
479 LPCWSTR pszName, LPGUID pguidInstance)
481 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
483 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
488 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
489 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
491 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
492 HRESULT ret_value = DIERR_DEVICENOTREG;
495 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
497 if (!rguid || !pvOut) return E_POINTER;
499 /* Loop on all the devices to see if anyone matches the given GUID */
500 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
503 if (!dinput_devices[i]->create_deviceA) continue;
504 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
506 EnterCriticalSection( &This->crit );
507 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
508 LeaveCriticalSection( &This->crit );
512 if (ret == DIERR_NOINTERFACE)
513 ret_value = DIERR_NOINTERFACE;
516 if (ret_value == DIERR_NOINTERFACE)
518 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
524 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
525 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
527 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
528 HRESULT ret_value = DIERR_DEVICENOTREG;
531 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
533 if (!rguid || !pvOut) return E_POINTER;
535 /* Loop on all the devices to see if anyone matches the given GUID */
536 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
539 if (!dinput_devices[i]->create_deviceW) continue;
540 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
542 EnterCriticalSection( &This->crit );
543 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
544 LeaveCriticalSection( &This->crit );
548 if (ret == DIERR_NOINTERFACE)
549 ret_value = DIERR_NOINTERFACE;
555 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
556 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
558 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
561 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
562 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
564 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
567 /*******************************************************************************
571 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
573 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
574 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
577 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
579 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
580 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
583 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
585 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
586 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
589 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
591 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
592 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
595 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
597 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
598 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
601 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
603 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
604 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
607 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
608 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
610 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
611 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
614 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
615 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
617 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
618 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
621 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
622 LPVOID pvRef, DWORD dwFlags)
624 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
625 return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
628 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
629 LPVOID pvRef, DWORD dwFlags)
631 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
632 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
635 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
637 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
638 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
641 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
643 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
644 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
647 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
649 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
650 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
653 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
655 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
656 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
659 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
661 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
662 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
665 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
667 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
668 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
671 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
673 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
674 return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
677 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
679 IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
680 return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
683 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
684 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
685 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
686 LPVOID pvRef, DWORD dwFlags
689 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
691 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
692 lpCallback, pvRef, dwFlags);
693 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
694 X(DIEDBSFL_ATTACHEDONLY)
696 X(DIEDBSFL_FORCEFEEDBACK)
697 X(DIEDBSFL_AVAILABLEDEVICES)
698 X(DIEDBSFL_MULTIMICEKEYBOARDS)
699 X(DIEDBSFL_NONGAMINGDEVICES)
702 _dump_diactionformatA(lpdiActionFormat);
707 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
708 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
709 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
710 LPVOID pvRef, DWORD dwFlags
713 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
715 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
716 lpCallback, pvRef, dwFlags);
720 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
721 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
722 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
725 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
727 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
732 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
733 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
734 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
737 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
739 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
744 static const IDirectInput7AVtbl ddi7avt = {
745 IDirectInputAImpl_QueryInterface,
746 IDirectInputAImpl_AddRef,
747 IDirectInputAImpl_Release,
748 IDirectInputAImpl_CreateDevice,
749 IDirectInputAImpl_EnumDevices,
750 IDirectInputAImpl_GetDeviceStatus,
751 IDirectInputAImpl_RunControlPanel,
752 IDirectInputAImpl_Initialize,
753 IDirectInput2AImpl_FindDevice,
754 IDirectInput7AImpl_CreateDeviceEx
757 static const IDirectInput7WVtbl ddi7wvt = {
758 IDirectInputWImpl_QueryInterface,
759 IDirectInputWImpl_AddRef,
760 IDirectInputWImpl_Release,
761 IDirectInputWImpl_CreateDevice,
762 IDirectInputWImpl_EnumDevices,
763 IDirectInputWImpl_GetDeviceStatus,
764 IDirectInputWImpl_RunControlPanel,
765 IDirectInputWImpl_Initialize,
766 IDirectInput2WImpl_FindDevice,
767 IDirectInput7WImpl_CreateDeviceEx
770 static const IDirectInput8AVtbl ddi8avt = {
771 IDirectInput8AImpl_QueryInterface,
772 IDirectInput8AImpl_AddRef,
773 IDirectInput8AImpl_Release,
774 IDirectInput8AImpl_CreateDevice,
775 IDirectInput8AImpl_EnumDevices,
776 IDirectInput8AImpl_GetDeviceStatus,
777 IDirectInput8AImpl_RunControlPanel,
778 IDirectInput8AImpl_Initialize,
779 IDirectInput8AImpl_FindDevice,
780 IDirectInput8AImpl_EnumDevicesBySemantics,
781 IDirectInput8AImpl_ConfigureDevices
784 static const IDirectInput8WVtbl ddi8wvt = {
785 IDirectInput8WImpl_QueryInterface,
786 IDirectInput8WImpl_AddRef,
787 IDirectInput8WImpl_Release,
788 IDirectInput8WImpl_CreateDevice,
789 IDirectInput8WImpl_EnumDevices,
790 IDirectInput8WImpl_GetDeviceStatus,
791 IDirectInput8WImpl_RunControlPanel,
792 IDirectInput8WImpl_Initialize,
793 IDirectInput8WImpl_FindDevice,
794 IDirectInput8WImpl_EnumDevicesBySemantics,
795 IDirectInput8WImpl_ConfigureDevices
798 /*******************************************************************************
799 * DirectInput ClassFactory
803 /* IUnknown fields */
804 IClassFactory IClassFactory_iface;
808 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
810 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
813 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
814 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
816 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
817 return E_NOINTERFACE;
820 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
821 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
822 return InterlockedIncrement(&(This->ref));
825 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
826 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
827 /* static class, won't be freed */
828 return InterlockedDecrement(&(This->ref));
831 static HRESULT WINAPI DICF_CreateInstance(
832 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
834 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
836 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
837 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
838 IsEqualGUID( &IID_IDirectInputA, riid ) ||
839 IsEqualGUID( &IID_IDirectInputW, riid ) ||
840 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
841 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
842 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
843 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
844 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
845 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
846 /* FIXME: reuse already created dinput if present? */
847 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
850 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
851 return E_NOINTERFACE;
854 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
855 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
856 FIXME("(%p)->(%d),stub!\n",This,dolock);
860 static const IClassFactoryVtbl DICF_Vtbl = {
867 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
869 /***********************************************************************
870 * DllCanUnloadNow (DINPUT.@)
872 HRESULT WINAPI DllCanUnloadNow(void)
877 /***********************************************************************
878 * DllGetClassObject (DINPUT.@)
880 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
882 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
883 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
885 IClassFactory_AddRef((IClassFactory*)*ppv);
889 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
890 return CLASS_E_CLASSNOTAVAILABLE;
893 /***********************************************************************
894 * DllRegisterServer (DINPUT.@)
896 HRESULT WINAPI DllRegisterServer(void)
898 return __wine_register_resources( DINPUT_instance, NULL );
901 /***********************************************************************
902 * DllUnregisterServer (DINPUT.@)
904 HRESULT WINAPI DllUnregisterServer(void)
906 return __wine_unregister_resources( DINPUT_instance, NULL );
909 /******************************************************************************
913 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
915 IDirectInputImpl *dinput;
918 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
920 EnterCriticalSection( &dinput_hook_crit );
921 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
923 IDirectInputDevice2AImpl *dev;
925 EnterCriticalSection( &dinput->crit );
926 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
927 if (dev->acquired && dev->event_proc)
929 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
930 skip |= dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
932 LeaveCriticalSection( &dinput->crit );
934 LeaveCriticalSection( &dinput_hook_crit );
936 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
939 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
941 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
942 IDirectInputImpl *dinput;
945 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
946 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
947 return CallNextHookEx( 0, code, wparam, lparam );
949 foreground = GetForegroundWindow();
951 EnterCriticalSection( &dinput_hook_crit );
953 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
955 IDirectInputDevice2AImpl *dev;
957 EnterCriticalSection( &dinput->crit );
958 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
960 if (!dev->acquired) continue;
962 if (msg->hwnd == dev->win && msg->hwnd != foreground)
964 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
965 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
968 LeaveCriticalSection( &dinput->crit );
970 LeaveCriticalSection( &dinput_hook_crit );
972 return CallNextHookEx( 0, code, wparam, lparam );
975 static DWORD WINAPI hook_thread_proc(void *param)
977 static HHOOK kbd_hook, mouse_hook;
980 /* Force creation of the message queue */
981 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
982 SetEvent(*(LPHANDLE)param);
984 while (GetMessageW( &msg, 0, 0, 0 ))
986 UINT kbd_cnt = 0, mice_cnt = 0;
988 if (msg.message == WM_USER+0x10)
990 IDirectInputImpl *dinput;
992 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
994 if (!msg.wParam && !msg.lParam)
996 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
997 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
998 kbd_hook = mouse_hook = NULL;
1002 EnterCriticalSection( &dinput_hook_crit );
1004 /* Count acquired keyboards and mice*/
1005 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1007 IDirectInputDevice2AImpl *dev;
1009 EnterCriticalSection( &dinput->crit );
1010 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
1012 if (!dev->acquired || !dev->event_proc) continue;
1014 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1015 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1018 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1019 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1022 LeaveCriticalSection( &dinput->crit );
1024 LeaveCriticalSection( &dinput_hook_crit );
1026 if (kbd_cnt && !kbd_hook)
1027 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1028 else if (!kbd_cnt && kbd_hook)
1030 UnhookWindowsHookEx( kbd_hook );
1034 if (mice_cnt && !mouse_hook)
1035 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1036 else if (!mice_cnt && mouse_hook)
1038 UnhookWindowsHookEx( mouse_hook );
1042 TranslateMessage(&msg);
1043 DispatchMessageW(&msg);
1049 static DWORD hook_thread_id;
1051 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1053 0, 0, &dinput_hook_crit,
1054 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1055 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1057 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1059 static BOOL check_hook_thread(void)
1061 static HANDLE hook_thread;
1063 EnterCriticalSection(&dinput_hook_crit);
1065 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1066 if (!list_empty(&direct_input_list) && !hook_thread)
1070 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1071 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1072 if (event && hook_thread)
1076 handles[1] = hook_thread;
1077 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1079 LeaveCriticalSection(&dinput_hook_crit);
1082 else if (list_empty(&direct_input_list) && hook_thread)
1084 DWORD tid = hook_thread_id;
1087 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1088 LeaveCriticalSection(&dinput_hook_crit);
1090 /* wait for hook thread to exit */
1091 WaitForSingleObject(hook_thread, INFINITE);
1092 CloseHandle(hook_thread);
1096 LeaveCriticalSection(&dinput_hook_crit);
1098 return hook_thread_id != 0;
1101 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1103 static HHOOK callwndproc_hook;
1104 static ULONG foreground_cnt;
1105 IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1107 EnterCriticalSection(&dinput_hook_crit);
1109 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1117 if (foreground_cnt && !callwndproc_hook)
1118 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1119 DINPUT_instance, GetCurrentThreadId() );
1120 else if (!foreground_cnt && callwndproc_hook)
1122 UnhookWindowsHookEx( callwndproc_hook );
1123 callwndproc_hook = NULL;
1126 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1128 LeaveCriticalSection(&dinput_hook_crit);