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"
47 #include "dinput_private.h"
48 #include "device_private.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
52 static const IDirectInput7AVtbl ddi7avt;
53 static const IDirectInput7WVtbl ddi7wvt;
54 static const IDirectInput8AVtbl ddi8avt;
55 static const IDirectInput8WVtbl ddi8wvt;
57 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
59 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl7w );
62 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
64 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8a );
67 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
69 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8w );
72 static inline IDirectInput7W *IDirectInput7W_from_impl( IDirectInputImpl *iface )
74 return (IDirectInput7W *)(&iface->lpVtbl7w);
77 static const struct dinput_device *dinput_devices[] =
81 &joystick_linuxinput_device,
82 &joystick_linux_device
84 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
86 static HINSTANCE DINPUT_instance = NULL;
88 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
92 case DLL_PROCESS_ATTACH:
93 DisableThreadLibraryCalls(inst);
94 DINPUT_instance = inst;
96 case DLL_PROCESS_DETACH:
102 static BOOL check_hook_thread(void);
103 static CRITICAL_SECTION dinput_hook_crit;
104 static struct list direct_input_list = LIST_INIT( direct_input_list );
106 /******************************************************************************
107 * DirectInputCreateEx (DINPUT.@)
109 HRESULT WINAPI DirectInputCreateEx(
110 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
113 IDirectInputImpl* This;
115 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
117 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
118 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
119 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
120 IsEqualGUID( &IID_IDirectInputW, riid ) ||
121 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
122 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
123 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
124 IsEqualGUID( &IID_IDirectInput8W, riid ))
126 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
127 return DIERR_OUTOFMEMORY;
130 return DIERR_OLDDIRECTINPUTVERSION;
132 This->lpVtbl = &ddi7avt;
133 This->lpVtbl7w = &ddi7wvt;
134 This->lpVtbl8a = &ddi8avt;
135 This->lpVtbl8w = &ddi8wvt;
137 This->dwVersion = dwVersion;
138 This->evsequence = 1;
140 InitializeCriticalSection(&This->crit);
141 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
143 list_init( &This->devices_list );
145 /* Add self to the list of the IDirectInputs */
146 EnterCriticalSection( &dinput_hook_crit );
147 list_add_head( &direct_input_list, &This->entry );
148 LeaveCriticalSection( &dinput_hook_crit );
150 if (!check_hook_thread())
152 IUnknown_Release( (LPDIRECTINPUT7A)This );
153 return DIERR_GENERIC;
156 IDirectInput_QueryInterface( (IDirectInput7A *)This, riid, ppDI );
160 /******************************************************************************
161 * DirectInputCreateA (DINPUT.@)
163 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
165 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
168 /******************************************************************************
169 * DirectInputCreateW (DINPUT.@)
171 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
173 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
176 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
178 case 0: return "All devices";
179 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
180 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
181 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
182 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
183 default: return "Unknown";
187 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
188 if (TRACE_ON(dinput)) {
190 static const struct {
194 #define FE(x) { x, #x}
195 FE(DIEDFL_ALLDEVICES),
196 FE(DIEDFL_ATTACHEDONLY),
197 FE(DIEDFL_FORCEFEEDBACK),
198 FE(DIEDFL_INCLUDEALIASES),
199 FE(DIEDFL_INCLUDEPHANTOMS)
204 TRACE("DIEDFL_ALLDEVICES");
207 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
208 if (flags[i].mask & dwFlags)
209 TRACE("%s ",flags[i].name);
214 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
217 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
218 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
219 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
220 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
221 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
222 for (i=0;i<lpdiActionFormat->dwNumActions;i++) {
223 FIXME("diaf.rgoAction[%d]:\n", i);
224 FIXME("\tuAppData=%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
225 FIXME("\tdwSemantics=%x\n", lpdiActionFormat->rgoAction[i].dwSemantics);
226 FIXME("\tdwFlags=%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
227 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
228 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
229 FIXME("\tdwObjID=%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
230 FIXME("\tdwHow=%x\n", lpdiActionFormat->rgoAction[i].dwHow);
232 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
233 FIXME("diaf.dwGenre = %d\n", lpdiActionFormat->dwGenre);
234 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
235 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
236 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
237 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
238 FIXME("diaf.ftTimeStamp ...\n");
239 FIXME("diaf.dwCRC = %x\n", lpdiActionFormat->dwCRC);
240 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
243 /******************************************************************************
244 * IDirectInputA_EnumDevices
246 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
247 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
248 LPVOID pvRef, DWORD dwFlags)
250 IDirectInputImpl *This = (IDirectInputImpl *)iface;
251 DIDEVICEINSTANCEA 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_deviceA) 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_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
265 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
273 /******************************************************************************
274 * IDirectInputW_EnumDevices
276 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
277 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
278 LPVOID pvRef, DWORD dwFlags)
280 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
281 DIDEVICEINSTANCEW devInstance;
284 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
285 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
286 lpCallback, pvRef, dwFlags);
287 _dump_EnumDevices_dwFlags(dwFlags);
289 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
290 if (!dinput_devices[i]->enum_deviceW) continue;
291 for (j = 0, r = -1; r != 0; j++) {
292 devInstance.dwSize = sizeof(devInstance);
293 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
294 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
295 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
304 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
306 IDirectInputImpl *This = (IDirectInputImpl *)iface;
307 ULONG ref = InterlockedIncrement(&This->ref);
309 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
313 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
315 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
316 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
319 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
321 IDirectInputImpl *This = (IDirectInputImpl *)iface;
322 ULONG ref = InterlockedDecrement( &This->ref );
324 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
328 /* Remove self from the list of the IDirectInputs */
329 EnterCriticalSection( &dinput_hook_crit );
330 list_remove( &This->entry );
331 LeaveCriticalSection( &dinput_hook_crit );
335 This->crit.DebugInfo->Spare[0] = 0;
336 DeleteCriticalSection( &This->crit );
337 HeapFree( GetProcessHeap(), 0, This );
342 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
344 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
345 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
348 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
350 IDirectInputImpl *This = (IDirectInputImpl *)iface;
352 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
354 if (IsEqualGUID( &IID_IUnknown, riid ) ||
355 IsEqualGUID( &IID_IDirectInputA, riid ) ||
356 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
357 IsEqualGUID( &IID_IDirectInput7A, riid ))
359 *ppobj = &This->lpVtbl;
360 IUnknown_AddRef( (IUnknown*)*ppobj );
365 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
366 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
367 IsEqualGUID( &IID_IDirectInput7W, riid ))
369 *ppobj = &This->lpVtbl7w;
370 IUnknown_AddRef( (IUnknown*)*ppobj );
375 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
377 *ppobj = &This->lpVtbl8a;
378 IUnknown_AddRef( (IUnknown*)*ppobj );
383 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
385 *ppobj = &This->lpVtbl8w;
386 IUnknown_AddRef( (IUnknown*)*ppobj );
391 FIXME( "Unsupported interface !\n" );
395 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
397 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
398 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
401 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
402 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
404 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
405 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
406 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
411 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
413 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
414 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
417 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
419 IDirectInputImpl *This = (IDirectInputImpl *)iface;
421 LPDIRECTINPUTDEVICEA device;
423 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
425 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
426 if (hr != DI_OK) return DI_NOTATTACHED;
428 IUnknown_Release( device );
433 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
435 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
436 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
439 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
443 IDirectInputImpl *This = (IDirectInputImpl *)iface;
445 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
450 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
452 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
453 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
456 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
457 LPCSTR pszName, LPGUID pguidInstance)
459 IDirectInputImpl *This = (IDirectInputImpl *)iface;
461 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
466 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
467 LPCWSTR pszName, LPGUID pguidInstance)
469 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
471 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
476 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
477 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
479 IDirectInputImpl *This = (IDirectInputImpl *)iface;
480 HRESULT ret_value = DIERR_DEVICENOTREG;
483 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
485 if (!rguid || !pvOut) return E_POINTER;
487 /* Loop on all the devices to see if anyone matches the given GUID */
488 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
491 if (!dinput_devices[i]->create_deviceA) continue;
492 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
494 EnterCriticalSection( &This->crit );
495 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
496 LeaveCriticalSection( &This->crit );
500 if (ret == DIERR_NOINTERFACE)
501 ret_value = DIERR_NOINTERFACE;
504 if (ret_value == DIERR_NOINTERFACE)
506 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
512 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
513 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
515 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
516 HRESULT ret_value = DIERR_DEVICENOTREG;
519 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
521 if (!rguid || !pvOut) return E_POINTER;
523 /* Loop on all the devices to see if anyone matches the given GUID */
524 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
527 if (!dinput_devices[i]->create_deviceW) continue;
528 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
530 EnterCriticalSection( &This->crit );
531 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
532 LeaveCriticalSection( &This->crit );
536 if (ret == DIERR_NOINTERFACE)
537 ret_value = DIERR_NOINTERFACE;
543 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
544 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
546 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
549 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
550 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
552 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
555 /*******************************************************************************
559 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
561 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
562 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
565 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
567 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
568 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
571 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
573 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
574 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
577 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
579 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
580 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
583 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
585 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
586 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
589 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
591 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
592 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
595 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
596 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
598 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
599 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
602 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
603 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
605 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
606 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
609 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
610 LPVOID pvRef, DWORD dwFlags)
612 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
613 return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
616 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
617 LPVOID pvRef, DWORD dwFlags)
619 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
620 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
623 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
625 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
626 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
629 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
631 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
632 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
635 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
637 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
638 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
641 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
643 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
644 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
647 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
649 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
650 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
653 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
655 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
656 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
659 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
661 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
662 return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
665 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
667 IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
668 return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
671 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
672 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
673 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
674 LPVOID pvRef, DWORD dwFlags
677 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
679 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
680 lpCallback, pvRef, dwFlags);
681 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
682 X(DIEDBSFL_ATTACHEDONLY)
684 X(DIEDBSFL_FORCEFEEDBACK)
685 X(DIEDBSFL_AVAILABLEDEVICES)
686 X(DIEDBSFL_MULTIMICEKEYBOARDS)
687 X(DIEDBSFL_NONGAMINGDEVICES)
690 _dump_diactionformatA(lpdiActionFormat);
695 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
696 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
697 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
698 LPVOID pvRef, DWORD dwFlags
701 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
703 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
704 lpCallback, pvRef, dwFlags);
708 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
709 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
710 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
713 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
715 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
720 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
721 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
722 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
725 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
727 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
732 static const IDirectInput7AVtbl ddi7avt = {
733 IDirectInputAImpl_QueryInterface,
734 IDirectInputAImpl_AddRef,
735 IDirectInputAImpl_Release,
736 IDirectInputAImpl_CreateDevice,
737 IDirectInputAImpl_EnumDevices,
738 IDirectInputAImpl_GetDeviceStatus,
739 IDirectInputAImpl_RunControlPanel,
740 IDirectInputAImpl_Initialize,
741 IDirectInput2AImpl_FindDevice,
742 IDirectInput7AImpl_CreateDeviceEx
745 static const IDirectInput7WVtbl ddi7wvt = {
746 IDirectInputWImpl_QueryInterface,
747 IDirectInputWImpl_AddRef,
748 IDirectInputWImpl_Release,
749 IDirectInputWImpl_CreateDevice,
750 IDirectInputWImpl_EnumDevices,
751 IDirectInputWImpl_GetDeviceStatus,
752 IDirectInputWImpl_RunControlPanel,
753 IDirectInputWImpl_Initialize,
754 IDirectInput2WImpl_FindDevice,
755 IDirectInput7WImpl_CreateDeviceEx
758 static const IDirectInput8AVtbl ddi8avt = {
759 IDirectInput8AImpl_QueryInterface,
760 IDirectInput8AImpl_AddRef,
761 IDirectInput8AImpl_Release,
762 IDirectInput8AImpl_CreateDevice,
763 IDirectInput8AImpl_EnumDevices,
764 IDirectInput8AImpl_GetDeviceStatus,
765 IDirectInput8AImpl_RunControlPanel,
766 IDirectInput8AImpl_Initialize,
767 IDirectInput8AImpl_FindDevice,
768 IDirectInput8AImpl_EnumDevicesBySemantics,
769 IDirectInput8AImpl_ConfigureDevices
772 static const IDirectInput8WVtbl ddi8wvt = {
773 IDirectInput8WImpl_QueryInterface,
774 IDirectInput8WImpl_AddRef,
775 IDirectInput8WImpl_Release,
776 IDirectInput8WImpl_CreateDevice,
777 IDirectInput8WImpl_EnumDevices,
778 IDirectInput8WImpl_GetDeviceStatus,
779 IDirectInput8WImpl_RunControlPanel,
780 IDirectInput8WImpl_Initialize,
781 IDirectInput8WImpl_FindDevice,
782 IDirectInput8WImpl_EnumDevicesBySemantics,
783 IDirectInput8WImpl_ConfigureDevices
786 /*******************************************************************************
787 * DirectInput ClassFactory
791 /* IUnknown fields */
792 const IClassFactoryVtbl *lpVtbl;
796 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
797 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
799 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
800 return E_NOINTERFACE;
803 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
804 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
805 return InterlockedIncrement(&(This->ref));
808 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
809 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
810 /* static class, won't be freed */
811 return InterlockedDecrement(&(This->ref));
814 static HRESULT WINAPI DICF_CreateInstance(
815 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
817 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
819 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
820 if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
821 IsEqualGUID( &IID_IDirectInputW, riid ) ||
822 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
823 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
824 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
825 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
826 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
827 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
828 /* FIXME: reuse already created dinput if present? */
829 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
832 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
833 return E_NOINTERFACE;
836 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
837 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
838 FIXME("(%p)->(%d),stub!\n",This,dolock);
842 static const IClassFactoryVtbl DICF_Vtbl = {
849 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
851 /***********************************************************************
852 * DllCanUnloadNow (DINPUT.@)
854 HRESULT WINAPI DllCanUnloadNow(void)
856 FIXME("(void): stub\n");
861 /***********************************************************************
862 * DllGetClassObject (DINPUT.@)
864 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
866 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
867 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
868 *ppv = (LPVOID)&DINPUT_CF;
869 IClassFactory_AddRef((IClassFactory*)*ppv);
873 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
874 return CLASS_E_CLASSNOTAVAILABLE;
877 /******************************************************************************
881 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
883 IDirectInputImpl *dinput;
885 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
887 EnterCriticalSection( &dinput_hook_crit );
888 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
890 IDirectInputDevice2AImpl *dev;
892 EnterCriticalSection( &dinput->crit );
893 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
894 if (dev->acquired && dev->event_proc)
896 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
897 dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
899 LeaveCriticalSection( &dinput->crit );
901 LeaveCriticalSection( &dinput_hook_crit );
903 return CallNextHookEx( 0, code, wparam, lparam );
906 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
908 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
909 IDirectInputImpl *dinput;
912 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
913 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
914 return CallNextHookEx( 0, code, wparam, lparam );
916 foreground = GetForegroundWindow();
918 EnterCriticalSection( &dinput_hook_crit );
920 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
922 IDirectInputDevice2AImpl *dev;
924 EnterCriticalSection( &dinput->crit );
925 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
927 if (!dev->acquired) continue;
929 if (msg->hwnd == dev->win && msg->hwnd != foreground)
931 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
932 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
935 LeaveCriticalSection( &dinput->crit );
937 LeaveCriticalSection( &dinput_hook_crit );
939 return CallNextHookEx( 0, code, wparam, lparam );
942 static DWORD WINAPI hook_thread_proc(void *param)
944 static HHOOK kbd_hook, mouse_hook;
947 /* Force creation of the message queue */
948 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
949 SetEvent(*(LPHANDLE)param);
951 while (GetMessageW( &msg, 0, 0, 0 ))
953 UINT kbd_cnt = 0, mice_cnt = 0;
955 if (msg.message == WM_USER+0x10)
957 IDirectInputImpl *dinput;
959 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
961 if (!msg.wParam && !msg.lParam)
963 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
964 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
965 kbd_hook = mouse_hook = NULL;
969 EnterCriticalSection( &dinput_hook_crit );
971 /* Count acquired keyboards and mice*/
972 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
974 IDirectInputDevice2AImpl *dev;
976 EnterCriticalSection( &dinput->crit );
977 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
979 if (!dev->acquired || !dev->event_proc) continue;
981 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
982 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
985 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
986 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
989 LeaveCriticalSection( &dinput->crit );
991 LeaveCriticalSection( &dinput_hook_crit );
993 if (kbd_cnt && !kbd_hook)
994 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
995 else if (!kbd_cnt && kbd_hook)
997 UnhookWindowsHookEx( kbd_hook );
1001 if (mice_cnt && !mouse_hook)
1002 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1003 else if (!mice_cnt && mouse_hook)
1005 UnhookWindowsHookEx( mouse_hook );
1009 TranslateMessage(&msg);
1010 DispatchMessageW(&msg);
1016 static DWORD hook_thread_id;
1018 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1020 0, 0, &dinput_hook_crit,
1021 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1022 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1024 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1026 static BOOL check_hook_thread(void)
1028 static HANDLE hook_thread;
1030 EnterCriticalSection(&dinput_hook_crit);
1032 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1033 if (!list_empty(&direct_input_list) && !hook_thread)
1037 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1038 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1039 if (event && hook_thread)
1043 handles[1] = hook_thread;
1044 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1046 LeaveCriticalSection(&dinput_hook_crit);
1049 else if (list_empty(&direct_input_list) && hook_thread)
1051 DWORD tid = hook_thread_id;
1054 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1055 LeaveCriticalSection(&dinput_hook_crit);
1057 /* wait for hook thread to exit */
1058 WaitForSingleObject(hook_thread, INFINITE);
1059 CloseHandle(hook_thread);
1063 LeaveCriticalSection(&dinput_hook_crit);
1065 return hook_thread_id != 0;
1068 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1070 static HHOOK callwndproc_hook;
1071 static ULONG foreground_cnt;
1072 IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1074 EnterCriticalSection(&dinput_hook_crit);
1076 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1084 if (foreground_cnt && !callwndproc_hook)
1085 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1086 DINPUT_instance, GetCurrentThreadId() );
1087 else if (!foreground_cnt && callwndproc_hook)
1089 UnhookWindowsHookEx( callwndproc_hook );
1090 callwndproc_hook = NULL;
1093 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1095 LeaveCriticalSection(&dinput_hook_crit);