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,
85 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
87 static HINSTANCE DINPUT_instance = NULL;
89 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
93 case DLL_PROCESS_ATTACH:
94 DisableThreadLibraryCalls(inst);
95 DINPUT_instance = inst;
97 case DLL_PROCESS_DETACH:
103 static BOOL check_hook_thread(void);
104 static CRITICAL_SECTION dinput_hook_crit;
105 static struct list direct_input_list = LIST_INIT( direct_input_list );
107 /******************************************************************************
108 * DirectInputCreateEx (DINPUT.@)
110 HRESULT WINAPI DirectInputCreateEx(
111 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
114 IDirectInputImpl* This;
116 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
118 if (IsEqualGUID( &IID_IUnknown, riid ) ||
119 IsEqualGUID( &IID_IDirectInputA, riid ) ||
120 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
121 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
122 IsEqualGUID( &IID_IDirectInputW, riid ) ||
123 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
124 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
125 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
126 IsEqualGUID( &IID_IDirectInput8W, riid ))
128 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
129 return DIERR_OUTOFMEMORY;
132 return DIERR_OLDDIRECTINPUTVERSION;
134 This->lpVtbl = &ddi7avt;
135 This->lpVtbl7w = &ddi7wvt;
136 This->lpVtbl8a = &ddi8avt;
137 This->lpVtbl8w = &ddi8wvt;
139 This->dwVersion = dwVersion;
140 This->evsequence = 1;
142 InitializeCriticalSection(&This->crit);
143 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
145 list_init( &This->devices_list );
147 /* Add self to the list of the IDirectInputs */
148 EnterCriticalSection( &dinput_hook_crit );
149 list_add_head( &direct_input_list, &This->entry );
150 LeaveCriticalSection( &dinput_hook_crit );
152 if (!check_hook_thread())
154 IUnknown_Release( (LPDIRECTINPUT7A)This );
155 return DIERR_GENERIC;
158 IDirectInput_QueryInterface( (IDirectInput7A *)This, riid, ppDI );
162 /******************************************************************************
163 * DirectInputCreateA (DINPUT.@)
165 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
167 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
170 /******************************************************************************
171 * DirectInputCreateW (DINPUT.@)
173 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
175 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
178 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
180 case 0: return "All devices";
181 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
182 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
183 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
184 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
185 default: return "Unknown";
189 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
190 if (TRACE_ON(dinput)) {
192 static const struct {
196 #define FE(x) { x, #x}
197 FE(DIEDFL_ALLDEVICES),
198 FE(DIEDFL_ATTACHEDONLY),
199 FE(DIEDFL_FORCEFEEDBACK),
200 FE(DIEDFL_INCLUDEALIASES),
201 FE(DIEDFL_INCLUDEPHANTOMS)
206 TRACE("DIEDFL_ALLDEVICES\n");
209 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
210 if (flags[i].mask & dwFlags)
211 TRACE("%s ",flags[i].name);
216 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
219 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
220 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
221 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
222 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
223 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
224 for (i=0;i<lpdiActionFormat->dwNumActions;i++) {
225 FIXME("diaf.rgoAction[%u]:\n", i);
226 FIXME("\tuAppData=%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
227 FIXME("\tdwSemantics=%x\n", lpdiActionFormat->rgoAction[i].dwSemantics);
228 FIXME("\tdwFlags=%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
229 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
230 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
231 FIXME("\tdwObjID=%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
232 FIXME("\tdwHow=%x\n", lpdiActionFormat->rgoAction[i].dwHow);
234 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
235 FIXME("diaf.dwGenre = %d\n", lpdiActionFormat->dwGenre);
236 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
237 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
238 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
239 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
240 FIXME("diaf.ftTimeStamp ...\n");
241 FIXME("diaf.dwCRC = %x\n", lpdiActionFormat->dwCRC);
242 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
245 /******************************************************************************
246 * IDirectInputA_EnumDevices
248 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
249 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
250 LPVOID pvRef, DWORD dwFlags)
252 IDirectInputImpl *This = (IDirectInputImpl *)iface;
253 DIDEVICEINSTANCEA devInstance;
257 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
258 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
259 lpCallback, pvRef, dwFlags);
260 _dump_EnumDevices_dwFlags(dwFlags);
262 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
263 if (!dinput_devices[i]->enum_deviceA) continue;
264 for (j = 0, r = -1; r != 0; j++) {
265 devInstance.dwSize = sizeof(devInstance);
266 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
267 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
268 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
276 /******************************************************************************
277 * IDirectInputW_EnumDevices
279 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
280 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
281 LPVOID pvRef, DWORD dwFlags)
283 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
284 DIDEVICEINSTANCEW devInstance;
288 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
289 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
290 lpCallback, pvRef, dwFlags);
291 _dump_EnumDevices_dwFlags(dwFlags);
293 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
294 if (!dinput_devices[i]->enum_deviceW) continue;
295 for (j = 0, r = -1; r != 0; j++) {
296 devInstance.dwSize = sizeof(devInstance);
297 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
298 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
299 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
308 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
310 IDirectInputImpl *This = (IDirectInputImpl *)iface;
311 ULONG ref = InterlockedIncrement(&This->ref);
313 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
317 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
319 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
320 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
323 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
325 IDirectInputImpl *This = (IDirectInputImpl *)iface;
326 ULONG ref = InterlockedDecrement( &This->ref );
328 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
332 /* Remove self from the list of the IDirectInputs */
333 EnterCriticalSection( &dinput_hook_crit );
334 list_remove( &This->entry );
335 LeaveCriticalSection( &dinput_hook_crit );
339 This->crit.DebugInfo->Spare[0] = 0;
340 DeleteCriticalSection( &This->crit );
341 HeapFree( GetProcessHeap(), 0, This );
346 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
348 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
349 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
352 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
354 IDirectInputImpl *This = (IDirectInputImpl *)iface;
356 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
358 if (IsEqualGUID( &IID_IUnknown, riid ) ||
359 IsEqualGUID( &IID_IDirectInputA, riid ) ||
360 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
361 IsEqualGUID( &IID_IDirectInput7A, riid ))
363 *ppobj = &This->lpVtbl;
364 IUnknown_AddRef( (IUnknown*)*ppobj );
369 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
370 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
371 IsEqualGUID( &IID_IDirectInput7W, riid ))
373 *ppobj = &This->lpVtbl7w;
374 IUnknown_AddRef( (IUnknown*)*ppobj );
379 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
381 *ppobj = &This->lpVtbl8a;
382 IUnknown_AddRef( (IUnknown*)*ppobj );
387 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
389 *ppobj = &This->lpVtbl8w;
390 IUnknown_AddRef( (IUnknown*)*ppobj );
395 FIXME( "Unsupported interface !\n" );
399 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
401 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
402 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
405 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
406 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
408 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
409 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
410 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
415 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
417 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
418 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
421 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
423 IDirectInputImpl *This = (IDirectInputImpl *)iface;
425 LPDIRECTINPUTDEVICEA device;
427 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
429 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
430 if (hr != DI_OK) return DI_NOTATTACHED;
432 IUnknown_Release( device );
437 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
439 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
440 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
443 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
447 IDirectInputImpl *This = (IDirectInputImpl *)iface;
449 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
454 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
456 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
457 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
460 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
461 LPCSTR pszName, LPGUID pguidInstance)
463 IDirectInputImpl *This = (IDirectInputImpl *)iface;
465 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
470 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
471 LPCWSTR pszName, LPGUID pguidInstance)
473 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
475 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
480 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
481 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
483 IDirectInputImpl *This = (IDirectInputImpl *)iface;
484 HRESULT ret_value = DIERR_DEVICENOTREG;
487 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
489 if (!rguid || !pvOut) return E_POINTER;
491 /* Loop on all the devices to see if anyone matches the given GUID */
492 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
495 if (!dinput_devices[i]->create_deviceA) continue;
496 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
498 EnterCriticalSection( &This->crit );
499 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
500 LeaveCriticalSection( &This->crit );
504 if (ret == DIERR_NOINTERFACE)
505 ret_value = DIERR_NOINTERFACE;
508 if (ret_value == DIERR_NOINTERFACE)
510 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
516 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
517 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
519 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
520 HRESULT ret_value = DIERR_DEVICENOTREG;
523 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
525 if (!rguid || !pvOut) return E_POINTER;
527 /* Loop on all the devices to see if anyone matches the given GUID */
528 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
531 if (!dinput_devices[i]->create_deviceW) continue;
532 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
534 EnterCriticalSection( &This->crit );
535 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
536 LeaveCriticalSection( &This->crit );
540 if (ret == DIERR_NOINTERFACE)
541 ret_value = DIERR_NOINTERFACE;
547 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
548 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
550 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
553 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
554 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
556 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
559 /*******************************************************************************
563 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
565 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
566 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
569 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
571 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
572 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
575 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
577 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
578 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
581 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
583 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
584 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
587 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
589 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
590 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
593 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
595 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
596 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
599 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
600 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
602 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
603 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
606 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
607 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
609 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
610 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
613 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
614 LPVOID pvRef, DWORD dwFlags)
616 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
617 return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
620 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
621 LPVOID pvRef, DWORD dwFlags)
623 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
624 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
627 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
629 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
630 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
633 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
635 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
636 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
639 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
641 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
642 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
645 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
647 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
648 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
651 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
653 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
654 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
657 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
659 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
660 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
663 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
665 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
666 return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
669 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
671 IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
672 return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
675 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
676 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
677 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
678 LPVOID pvRef, DWORD dwFlags
681 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
683 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
684 lpCallback, pvRef, dwFlags);
685 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
686 X(DIEDBSFL_ATTACHEDONLY)
688 X(DIEDBSFL_FORCEFEEDBACK)
689 X(DIEDBSFL_AVAILABLEDEVICES)
690 X(DIEDBSFL_MULTIMICEKEYBOARDS)
691 X(DIEDBSFL_NONGAMINGDEVICES)
694 _dump_diactionformatA(lpdiActionFormat);
699 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
700 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
701 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
702 LPVOID pvRef, DWORD dwFlags
705 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
707 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
708 lpCallback, pvRef, dwFlags);
712 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
713 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
714 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
717 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
719 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
724 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
725 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
726 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
729 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
731 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
736 static const IDirectInput7AVtbl ddi7avt = {
737 IDirectInputAImpl_QueryInterface,
738 IDirectInputAImpl_AddRef,
739 IDirectInputAImpl_Release,
740 IDirectInputAImpl_CreateDevice,
741 IDirectInputAImpl_EnumDevices,
742 IDirectInputAImpl_GetDeviceStatus,
743 IDirectInputAImpl_RunControlPanel,
744 IDirectInputAImpl_Initialize,
745 IDirectInput2AImpl_FindDevice,
746 IDirectInput7AImpl_CreateDeviceEx
749 static const IDirectInput7WVtbl ddi7wvt = {
750 IDirectInputWImpl_QueryInterface,
751 IDirectInputWImpl_AddRef,
752 IDirectInputWImpl_Release,
753 IDirectInputWImpl_CreateDevice,
754 IDirectInputWImpl_EnumDevices,
755 IDirectInputWImpl_GetDeviceStatus,
756 IDirectInputWImpl_RunControlPanel,
757 IDirectInputWImpl_Initialize,
758 IDirectInput2WImpl_FindDevice,
759 IDirectInput7WImpl_CreateDeviceEx
762 static const IDirectInput8AVtbl ddi8avt = {
763 IDirectInput8AImpl_QueryInterface,
764 IDirectInput8AImpl_AddRef,
765 IDirectInput8AImpl_Release,
766 IDirectInput8AImpl_CreateDevice,
767 IDirectInput8AImpl_EnumDevices,
768 IDirectInput8AImpl_GetDeviceStatus,
769 IDirectInput8AImpl_RunControlPanel,
770 IDirectInput8AImpl_Initialize,
771 IDirectInput8AImpl_FindDevice,
772 IDirectInput8AImpl_EnumDevicesBySemantics,
773 IDirectInput8AImpl_ConfigureDevices
776 static const IDirectInput8WVtbl ddi8wvt = {
777 IDirectInput8WImpl_QueryInterface,
778 IDirectInput8WImpl_AddRef,
779 IDirectInput8WImpl_Release,
780 IDirectInput8WImpl_CreateDevice,
781 IDirectInput8WImpl_EnumDevices,
782 IDirectInput8WImpl_GetDeviceStatus,
783 IDirectInput8WImpl_RunControlPanel,
784 IDirectInput8WImpl_Initialize,
785 IDirectInput8WImpl_FindDevice,
786 IDirectInput8WImpl_EnumDevicesBySemantics,
787 IDirectInput8WImpl_ConfigureDevices
790 /*******************************************************************************
791 * DirectInput ClassFactory
795 /* IUnknown fields */
796 const IClassFactoryVtbl *lpVtbl;
800 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
801 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
803 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
804 return E_NOINTERFACE;
807 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
808 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
809 return InterlockedIncrement(&(This->ref));
812 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
813 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
814 /* static class, won't be freed */
815 return InterlockedDecrement(&(This->ref));
818 static HRESULT WINAPI DICF_CreateInstance(
819 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
821 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
823 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
824 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
825 IsEqualGUID( &IID_IDirectInputA, riid ) ||
826 IsEqualGUID( &IID_IDirectInputW, riid ) ||
827 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
828 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
829 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
830 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
831 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
832 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
833 /* FIXME: reuse already created dinput if present? */
834 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
837 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
838 return E_NOINTERFACE;
841 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
842 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
843 FIXME("(%p)->(%d),stub!\n",This,dolock);
847 static const IClassFactoryVtbl DICF_Vtbl = {
854 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
856 /***********************************************************************
857 * DllCanUnloadNow (DINPUT.@)
859 HRESULT WINAPI DllCanUnloadNow(void)
861 FIXME("(void): stub\n");
866 /***********************************************************************
867 * DllGetClassObject (DINPUT.@)
869 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
871 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
872 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
874 IClassFactory_AddRef((IClassFactory*)*ppv);
878 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
879 return CLASS_E_CLASSNOTAVAILABLE;
882 /******************************************************************************
886 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
888 IDirectInputImpl *dinput;
891 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
893 EnterCriticalSection( &dinput_hook_crit );
894 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
896 IDirectInputDevice2AImpl *dev;
898 EnterCriticalSection( &dinput->crit );
899 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
900 if (dev->acquired && dev->event_proc)
902 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
903 skip |= dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
905 LeaveCriticalSection( &dinput->crit );
907 LeaveCriticalSection( &dinput_hook_crit );
909 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
912 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
914 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
915 IDirectInputImpl *dinput;
918 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
919 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
920 return CallNextHookEx( 0, code, wparam, lparam );
922 foreground = GetForegroundWindow();
924 EnterCriticalSection( &dinput_hook_crit );
926 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
928 IDirectInputDevice2AImpl *dev;
930 EnterCriticalSection( &dinput->crit );
931 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
933 if (!dev->acquired) continue;
935 if (msg->hwnd == dev->win && msg->hwnd != foreground)
937 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
938 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
941 LeaveCriticalSection( &dinput->crit );
943 LeaveCriticalSection( &dinput_hook_crit );
945 return CallNextHookEx( 0, code, wparam, lparam );
948 static DWORD WINAPI hook_thread_proc(void *param)
950 static HHOOK kbd_hook, mouse_hook;
953 /* Force creation of the message queue */
954 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
955 SetEvent(*(LPHANDLE)param);
957 while (GetMessageW( &msg, 0, 0, 0 ))
959 UINT kbd_cnt = 0, mice_cnt = 0;
961 if (msg.message == WM_USER+0x10)
963 IDirectInputImpl *dinput;
965 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
967 if (!msg.wParam && !msg.lParam)
969 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
970 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
971 kbd_hook = mouse_hook = NULL;
975 EnterCriticalSection( &dinput_hook_crit );
977 /* Count acquired keyboards and mice*/
978 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
980 IDirectInputDevice2AImpl *dev;
982 EnterCriticalSection( &dinput->crit );
983 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
985 if (!dev->acquired || !dev->event_proc) continue;
987 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
988 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
991 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
992 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
995 LeaveCriticalSection( &dinput->crit );
997 LeaveCriticalSection( &dinput_hook_crit );
999 if (kbd_cnt && !kbd_hook)
1000 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1001 else if (!kbd_cnt && kbd_hook)
1003 UnhookWindowsHookEx( kbd_hook );
1007 if (mice_cnt && !mouse_hook)
1008 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1009 else if (!mice_cnt && mouse_hook)
1011 UnhookWindowsHookEx( mouse_hook );
1015 TranslateMessage(&msg);
1016 DispatchMessageW(&msg);
1022 static DWORD hook_thread_id;
1024 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1026 0, 0, &dinput_hook_crit,
1027 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1028 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1030 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1032 static BOOL check_hook_thread(void)
1034 static HANDLE hook_thread;
1036 EnterCriticalSection(&dinput_hook_crit);
1038 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1039 if (!list_empty(&direct_input_list) && !hook_thread)
1043 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1044 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1045 if (event && hook_thread)
1049 handles[1] = hook_thread;
1050 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1052 LeaveCriticalSection(&dinput_hook_crit);
1055 else if (list_empty(&direct_input_list) && hook_thread)
1057 DWORD tid = hook_thread_id;
1060 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1061 LeaveCriticalSection(&dinput_hook_crit);
1063 /* wait for hook thread to exit */
1064 WaitForSingleObject(hook_thread, INFINITE);
1065 CloseHandle(hook_thread);
1069 LeaveCriticalSection(&dinput_hook_crit);
1071 return hook_thread_id != 0;
1074 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1076 static HHOOK callwndproc_hook;
1077 static ULONG foreground_cnt;
1078 IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1080 EnterCriticalSection(&dinput_hook_crit);
1082 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1090 if (foreground_cnt && !callwndproc_hook)
1091 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1092 DINPUT_instance, GetCurrentThreadId() );
1093 else if (!foreground_cnt && callwndproc_hook)
1095 UnhookWindowsHookEx( callwndproc_hook );
1096 callwndproc_hook = NULL;
1099 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1101 LeaveCriticalSection(&dinput_hook_crit);