dinput: EnumDevicesBySemantics enumerating keyboard and mouse with priority flags.
[wine/multimedia.git] / dlls / dinput / dinput_main.c
blobc032642e9c54de3faaeb76738741ee17e7ef8ea6
1 /* DirectInput
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
23 /* Status:
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
33 #include "config.h"
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <string.h>
38 #define COBJMACROS
39 #define NONAMELESSUNION
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winuser.h"
46 #include "winerror.h"
47 #include "objbase.h"
48 #include "rpcproxy.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 IDirectInputDeviceImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
81 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface);
83 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
85 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
88 static const struct dinput_device *dinput_devices[] =
90 &mouse_device,
91 &keyboard_device,
92 &joystick_linuxinput_device,
93 &joystick_linux_device,
94 &joystick_osx_device
96 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
98 static HINSTANCE DINPUT_instance = NULL;
100 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
102 switch(reason)
104 case DLL_PROCESS_ATTACH:
105 DisableThreadLibraryCalls(inst);
106 DINPUT_instance = inst;
107 break;
108 case DLL_PROCESS_DETACH:
109 break;
111 return TRUE;
114 static BOOL check_hook_thread(void);
115 static CRITICAL_SECTION dinput_hook_crit;
116 static struct list direct_input_list = LIST_INIT( direct_input_list );
118 /******************************************************************************
119 * DirectInputCreateEx (DINPUT.@)
121 HRESULT WINAPI DirectInputCreateEx(
122 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
123 LPUNKNOWN punkOuter)
125 IDirectInputImpl* This;
127 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
129 if (IsEqualGUID( &IID_IUnknown, riid ) ||
130 IsEqualGUID( &IID_IDirectInputA, riid ) ||
131 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
132 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
133 IsEqualGUID( &IID_IDirectInputW, riid ) ||
134 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
135 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
136 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
137 IsEqualGUID( &IID_IDirectInput8W, riid ))
139 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
140 return DIERR_OUTOFMEMORY;
142 else
143 return DIERR_OLDDIRECTINPUTVERSION;
145 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
146 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
147 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
148 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
149 This->ref = 0;
150 This->dwVersion = dwVersion;
151 This->evsequence = 1;
153 InitializeCriticalSection(&This->crit);
154 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
156 list_init( &This->devices_list );
158 /* Add self to the list of the IDirectInputs */
159 EnterCriticalSection( &dinput_hook_crit );
160 list_add_head( &direct_input_list, &This->entry );
161 LeaveCriticalSection( &dinput_hook_crit );
163 if (!check_hook_thread())
165 IUnknown_Release( &This->IDirectInput7A_iface );
166 return DIERR_GENERIC;
169 IDirectInput_QueryInterface( &This->IDirectInput7A_iface, riid, ppDI );
170 return DI_OK;
173 /******************************************************************************
174 * DirectInputCreateA (DINPUT.@)
176 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
178 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
181 /******************************************************************************
182 * DirectInputCreateW (DINPUT.@)
184 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
186 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
189 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
190 switch (dwDevType) {
191 case 0: return "All devices";
192 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
193 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
194 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
195 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
196 default: return "Unknown";
200 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
201 if (TRACE_ON(dinput)) {
202 unsigned int i;
203 static const struct {
204 DWORD mask;
205 const char *name;
206 } flags[] = {
207 #define FE(x) { x, #x}
208 FE(DIEDFL_ALLDEVICES),
209 FE(DIEDFL_ATTACHEDONLY),
210 FE(DIEDFL_FORCEFEEDBACK),
211 FE(DIEDFL_INCLUDEALIASES),
212 FE(DIEDFL_INCLUDEPHANTOMS)
213 #undef FE
215 TRACE(" flags: ");
216 if (dwFlags == 0) {
217 TRACE("DIEDFL_ALLDEVICES\n");
218 return;
220 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
221 if (flags[i].mask & dwFlags)
222 TRACE("%s ",flags[i].name);
224 TRACE("\n");
227 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
228 unsigned int i;
230 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
231 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
232 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
233 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
234 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
235 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
236 FIXME("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
237 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
238 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
239 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
240 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
241 FIXME("diaf.ftTimeStamp ...\n");
242 FIXME("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
243 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
244 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
246 FIXME("diaf.rgoAction[%u]:\n", i);
247 FIXME("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
248 FIXME("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
249 FIXME("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
250 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
251 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
252 FIXME("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
253 FIXME("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
257 /******************************************************************************
258 * IDirectInputA_EnumDevices
260 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
261 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
262 LPVOID pvRef, DWORD dwFlags)
264 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
265 DIDEVICEINSTANCEA devInstance;
266 unsigned int i;
267 int j, r;
269 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
270 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
271 lpCallback, pvRef, dwFlags);
272 _dump_EnumDevices_dwFlags(dwFlags);
274 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
275 if (!dinput_devices[i]->enum_deviceA) continue;
276 for (j = 0, r = -1; r != 0; j++) {
277 devInstance.dwSize = sizeof(devInstance);
278 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
279 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
280 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
281 return 0;
286 return 0;
288 /******************************************************************************
289 * IDirectInputW_EnumDevices
291 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
292 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
293 LPVOID pvRef, DWORD dwFlags)
295 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
296 DIDEVICEINSTANCEW devInstance;
297 unsigned int i;
298 int j, r;
300 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
301 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
302 lpCallback, pvRef, dwFlags);
303 _dump_EnumDevices_dwFlags(dwFlags);
305 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
306 if (!dinput_devices[i]->enum_deviceW) continue;
307 for (j = 0, r = -1; r != 0; j++) {
308 devInstance.dwSize = sizeof(devInstance);
309 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
310 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
311 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
312 return 0;
317 return 0;
320 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
322 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
323 ULONG ref = InterlockedIncrement(&This->ref);
325 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
326 return ref;
329 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
331 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
332 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
335 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
337 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
338 ULONG ref = InterlockedDecrement( &This->ref );
340 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
342 if (ref) return ref;
344 /* Remove self from the list of the IDirectInputs */
345 EnterCriticalSection( &dinput_hook_crit );
346 list_remove( &This->entry );
347 LeaveCriticalSection( &dinput_hook_crit );
349 check_hook_thread();
351 This->crit.DebugInfo->Spare[0] = 0;
352 DeleteCriticalSection( &This->crit );
353 HeapFree( GetProcessHeap(), 0, This );
355 return 0;
358 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
360 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
361 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
364 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
366 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
368 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
370 if (IsEqualGUID( &IID_IUnknown, riid ) ||
371 IsEqualGUID( &IID_IDirectInputA, riid ) ||
372 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
373 IsEqualGUID( &IID_IDirectInput7A, riid ))
375 *ppobj = &This->IDirectInput7A_iface;
376 IUnknown_AddRef( (IUnknown*)*ppobj );
378 return DI_OK;
381 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
382 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
383 IsEqualGUID( &IID_IDirectInput7W, riid ))
385 *ppobj = &This->IDirectInput7W_iface;
386 IUnknown_AddRef( (IUnknown*)*ppobj );
388 return DI_OK;
391 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
393 *ppobj = &This->IDirectInput8A_iface;
394 IUnknown_AddRef( (IUnknown*)*ppobj );
396 return DI_OK;
399 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
401 *ppobj = &This->IDirectInput8W_iface;
402 IUnknown_AddRef( (IUnknown*)*ppobj );
404 return DI_OK;
407 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
408 return E_FAIL;
411 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
413 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
414 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
417 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
418 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
420 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
421 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
422 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
424 return DI_OK;
427 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
429 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
430 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
433 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
435 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
436 HRESULT hr;
437 LPDIRECTINPUTDEVICEA device;
439 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
441 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
442 if (hr != DI_OK) return DI_NOTATTACHED;
444 IUnknown_Release( device );
446 return DI_OK;
449 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
451 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
452 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
455 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
456 HWND hwndOwner,
457 DWORD dwFlags)
459 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
461 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
463 return DI_OK;
466 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
468 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
469 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
472 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
473 LPCSTR pszName, LPGUID pguidInstance)
475 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
477 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
479 return DI_OK;
482 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
483 LPCWSTR pszName, LPGUID pguidInstance)
485 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
487 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
489 return DI_OK;
492 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
493 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
495 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
496 HRESULT ret_value = DIERR_DEVICENOTREG;
497 unsigned int i;
499 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
501 if (!rguid || !pvOut) return E_POINTER;
503 /* Loop on all the devices to see if anyone matches the given GUID */
504 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
505 HRESULT ret;
507 if (!dinput_devices[i]->create_device) continue;
508 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, 0)) == DI_OK)
509 return DI_OK;
511 if (ret == DIERR_NOINTERFACE)
512 ret_value = DIERR_NOINTERFACE;
515 if (ret_value == DIERR_NOINTERFACE)
517 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
520 return ret_value;
523 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
524 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
526 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
527 HRESULT ret_value = DIERR_DEVICENOTREG;
528 unsigned int i;
530 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
532 if (!rguid || !pvOut) return E_POINTER;
534 /* Loop on all the devices to see if anyone matches the given GUID */
535 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
536 HRESULT ret;
538 if (!dinput_devices[i]->create_device) continue;
539 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, 1)) == DI_OK)
540 return DI_OK;
542 if (ret == DIERR_NOINTERFACE)
543 ret_value = DIERR_NOINTERFACE;
546 return ret_value;
549 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
550 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
552 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
555 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
556 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
558 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
561 /*******************************************************************************
562 * DirectInput8
565 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
567 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
568 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
571 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
573 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
574 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
577 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
579 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
580 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
583 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
585 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
586 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
589 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
591 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
592 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
595 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
597 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
598 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
601 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
602 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
604 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
605 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
608 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
609 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
611 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
612 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
615 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
616 LPVOID pvRef, DWORD dwFlags)
618 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
619 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
622 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
623 LPVOID pvRef, DWORD dwFlags)
625 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
626 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
629 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
631 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
632 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
635 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
637 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
638 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
641 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
643 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
644 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
647 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
649 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
650 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
653 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
655 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
656 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
659 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
661 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
662 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
665 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
667 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
668 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
671 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
673 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
674 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
677 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
678 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
679 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
680 LPVOID pvRef, DWORD dwFlags
683 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
684 DIDEVICEINSTANCEA didevi;
685 LPDIRECTINPUTDEVICE8A lpdid;
686 BOOL ret;
687 DWORD callbackFlags = 0;
688 int i;
690 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, ptszUserName, lpdiActionFormat,
691 lpCallback, pvRef, dwFlags);
692 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
693 X(DIEDBSFL_ATTACHEDONLY)
694 X(DIEDBSFL_THISUSER)
695 X(DIEDBSFL_FORCEFEEDBACK)
696 X(DIEDBSFL_AVAILABLEDEVICES)
697 X(DIEDBSFL_MULTIMICEKEYBOARDS)
698 X(DIEDBSFL_NONGAMINGDEVICES)
699 #undef X
701 _dump_diactionformatA(lpdiActionFormat);
703 didevi.dwSize = sizeof(didevi);
705 /* enum the keyboard first */
706 IDirectInput_CreateDevice(iface, &GUID_SysKeyboard, &lpdid, NULL);
707 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
708 /* if there's any DIKEYBOARD action, keyboard is priority 1*/
709 for(i=0; i < lpdiActionFormat->dwActionSize; i++)
710 if ((lpdiActionFormat->rgoAction[i].dwSemantic & DIKEYBOARD_MASK) == DIKEYBOARD_MASK)
711 callbackFlags |= DIEDBS_MAPPEDPRI1;
713 ret = lpCallback(&didevi, lpdid, callbackFlags, 1, pvRef);
715 if (ret == DIENUM_STOP) return DI_OK;
717 callbackFlags = 0;
719 /* and then the mouse */
720 IDirectInput_CreateDevice(iface, &GUID_SysMouse, &lpdid, NULL);
721 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
722 /* same as above, but for the mouse */
723 for(i=0; i < lpdiActionFormat->dwActionSize; i++)
724 if ((lpdiActionFormat->rgoAction[i].dwSemantic & DIMOUSE_MASK) == DIMOUSE_MASK)
725 callbackFlags |= DIEDBS_MAPPEDPRI1;
727 lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef);
729 return DI_OK;
732 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
733 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
734 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
735 LPVOID pvRef, DWORD dwFlags
738 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
739 DIDEVICEINSTANCEW didevi;
740 LPDIRECTINPUTDEVICE8W lpdid;
741 BOOL ret;
742 DWORD callbackFlags = 0;
743 int i;
745 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
746 lpCallback, pvRef, dwFlags);
748 didevi.dwSize = sizeof(didevi);
750 /* enum the keyboard first */
751 IDirectInput_CreateDevice(iface, &GUID_SysKeyboard, &lpdid, NULL);
752 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
753 /* if there's any DIKEYBOARD action, keyboard is priority 1*/
754 for(i=0; i < lpdiActionFormat->dwActionSize; i++)
755 if ((lpdiActionFormat->rgoAction[i].dwSemantic & DIKEYBOARD_MASK) == DIKEYBOARD_MASK)
756 callbackFlags |= DIEDBS_MAPPEDPRI1;
758 ret = lpCallback(&didevi, lpdid, callbackFlags, 1, pvRef);
760 if (ret == DIENUM_STOP) return DI_OK;
762 callbackFlags = 0;
764 /* and then the mouse */
765 IDirectInput_CreateDevice(iface, &GUID_SysMouse, &lpdid, NULL);
766 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
767 /* same as above, but for the mouse */
768 for(i=0; i < lpdiActionFormat->dwActionSize; i++)
769 if ((lpdiActionFormat->rgoAction[i].dwSemantic & DIMOUSE_MASK) == DIMOUSE_MASK)
770 callbackFlags |= DIEDBS_MAPPEDPRI1;
772 lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef);
774 return DI_OK;
777 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
778 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
779 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
782 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
784 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
785 dwFlags, pvRefData);
786 return 0;
789 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
790 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
791 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
794 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
796 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
797 dwFlags, pvRefData);
798 return 0;
801 static const IDirectInput7AVtbl ddi7avt = {
802 IDirectInputAImpl_QueryInterface,
803 IDirectInputAImpl_AddRef,
804 IDirectInputAImpl_Release,
805 IDirectInputAImpl_CreateDevice,
806 IDirectInputAImpl_EnumDevices,
807 IDirectInputAImpl_GetDeviceStatus,
808 IDirectInputAImpl_RunControlPanel,
809 IDirectInputAImpl_Initialize,
810 IDirectInput2AImpl_FindDevice,
811 IDirectInput7AImpl_CreateDeviceEx
814 static const IDirectInput7WVtbl ddi7wvt = {
815 IDirectInputWImpl_QueryInterface,
816 IDirectInputWImpl_AddRef,
817 IDirectInputWImpl_Release,
818 IDirectInputWImpl_CreateDevice,
819 IDirectInputWImpl_EnumDevices,
820 IDirectInputWImpl_GetDeviceStatus,
821 IDirectInputWImpl_RunControlPanel,
822 IDirectInputWImpl_Initialize,
823 IDirectInput2WImpl_FindDevice,
824 IDirectInput7WImpl_CreateDeviceEx
827 static const IDirectInput8AVtbl ddi8avt = {
828 IDirectInput8AImpl_QueryInterface,
829 IDirectInput8AImpl_AddRef,
830 IDirectInput8AImpl_Release,
831 IDirectInput8AImpl_CreateDevice,
832 IDirectInput8AImpl_EnumDevices,
833 IDirectInput8AImpl_GetDeviceStatus,
834 IDirectInput8AImpl_RunControlPanel,
835 IDirectInput8AImpl_Initialize,
836 IDirectInput8AImpl_FindDevice,
837 IDirectInput8AImpl_EnumDevicesBySemantics,
838 IDirectInput8AImpl_ConfigureDevices
841 static const IDirectInput8WVtbl ddi8wvt = {
842 IDirectInput8WImpl_QueryInterface,
843 IDirectInput8WImpl_AddRef,
844 IDirectInput8WImpl_Release,
845 IDirectInput8WImpl_CreateDevice,
846 IDirectInput8WImpl_EnumDevices,
847 IDirectInput8WImpl_GetDeviceStatus,
848 IDirectInput8WImpl_RunControlPanel,
849 IDirectInput8WImpl_Initialize,
850 IDirectInput8WImpl_FindDevice,
851 IDirectInput8WImpl_EnumDevicesBySemantics,
852 IDirectInput8WImpl_ConfigureDevices
855 /*******************************************************************************
856 * DirectInput ClassFactory
858 typedef struct
860 /* IUnknown fields */
861 IClassFactory IClassFactory_iface;
862 LONG ref;
863 } IClassFactoryImpl;
865 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
867 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
870 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
871 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
873 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
874 return E_NOINTERFACE;
877 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
878 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
879 return InterlockedIncrement(&(This->ref));
882 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
883 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
884 /* static class, won't be freed */
885 return InterlockedDecrement(&(This->ref));
888 static HRESULT WINAPI DICF_CreateInstance(
889 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
891 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
893 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
894 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
895 IsEqualGUID( &IID_IDirectInputA, riid ) ||
896 IsEqualGUID( &IID_IDirectInputW, riid ) ||
897 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
898 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
899 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
900 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
901 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
902 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
903 /* FIXME: reuse already created dinput if present? */
904 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
907 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
908 return E_NOINTERFACE;
911 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
912 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
913 FIXME("(%p)->(%d),stub!\n",This,dolock);
914 return S_OK;
917 static const IClassFactoryVtbl DICF_Vtbl = {
918 DICF_QueryInterface,
919 DICF_AddRef,
920 DICF_Release,
921 DICF_CreateInstance,
922 DICF_LockServer
924 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
926 /***********************************************************************
927 * DllCanUnloadNow (DINPUT.@)
929 HRESULT WINAPI DllCanUnloadNow(void)
931 return S_FALSE;
934 /***********************************************************************
935 * DllGetClassObject (DINPUT.@)
937 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
939 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
940 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
941 *ppv = &DINPUT_CF;
942 IClassFactory_AddRef((IClassFactory*)*ppv);
943 return S_OK;
946 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
947 return CLASS_E_CLASSNOTAVAILABLE;
950 /***********************************************************************
951 * DllRegisterServer (DINPUT.@)
953 HRESULT WINAPI DllRegisterServer(void)
955 return __wine_register_resources( DINPUT_instance, NULL );
958 /***********************************************************************
959 * DllUnregisterServer (DINPUT.@)
961 HRESULT WINAPI DllUnregisterServer(void)
963 return __wine_unregister_resources( DINPUT_instance, NULL );
966 /******************************************************************************
967 * DInput hook thread
970 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
972 IDirectInputImpl *dinput;
973 int skip = 0;
975 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
977 EnterCriticalSection( &dinput_hook_crit );
978 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
980 IDirectInputDeviceImpl *dev;
982 EnterCriticalSection( &dinput->crit );
983 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
984 if (dev->acquired && dev->event_proc)
986 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
987 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
989 LeaveCriticalSection( &dinput->crit );
991 LeaveCriticalSection( &dinput_hook_crit );
993 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
996 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
998 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
999 IDirectInputImpl *dinput;
1000 HWND foreground;
1002 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1003 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1004 return CallNextHookEx( 0, code, wparam, lparam );
1006 foreground = GetForegroundWindow();
1008 EnterCriticalSection( &dinput_hook_crit );
1010 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1012 IDirectInputDeviceImpl *dev;
1014 EnterCriticalSection( &dinput->crit );
1015 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1017 if (!dev->acquired) continue;
1019 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1021 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1022 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1025 LeaveCriticalSection( &dinput->crit );
1027 LeaveCriticalSection( &dinput_hook_crit );
1029 return CallNextHookEx( 0, code, wparam, lparam );
1032 static DWORD WINAPI hook_thread_proc(void *param)
1034 static HHOOK kbd_hook, mouse_hook;
1035 MSG msg;
1037 /* Force creation of the message queue */
1038 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1039 SetEvent(*(LPHANDLE)param);
1041 while (GetMessageW( &msg, 0, 0, 0 ))
1043 UINT kbd_cnt = 0, mice_cnt = 0;
1045 if (msg.message == WM_USER+0x10)
1047 IDirectInputImpl *dinput;
1049 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
1051 if (!msg.wParam && !msg.lParam)
1053 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1054 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1055 kbd_hook = mouse_hook = NULL;
1056 break;
1059 EnterCriticalSection( &dinput_hook_crit );
1061 /* Count acquired keyboards and mice*/
1062 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1064 IDirectInputDeviceImpl *dev;
1066 EnterCriticalSection( &dinput->crit );
1067 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1069 if (!dev->acquired || !dev->event_proc) continue;
1071 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1072 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1073 kbd_cnt++;
1074 else
1075 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1076 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1077 mice_cnt++;
1079 LeaveCriticalSection( &dinput->crit );
1081 LeaveCriticalSection( &dinput_hook_crit );
1083 if (kbd_cnt && !kbd_hook)
1084 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1085 else if (!kbd_cnt && kbd_hook)
1087 UnhookWindowsHookEx( kbd_hook );
1088 kbd_hook = NULL;
1091 if (mice_cnt && !mouse_hook)
1092 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1093 else if (!mice_cnt && mouse_hook)
1095 UnhookWindowsHookEx( mouse_hook );
1096 mouse_hook = NULL;
1099 TranslateMessage(&msg);
1100 DispatchMessageW(&msg);
1103 return 0;
1106 static DWORD hook_thread_id;
1108 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1110 0, 0, &dinput_hook_crit,
1111 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1112 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1114 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1116 static BOOL check_hook_thread(void)
1118 static HANDLE hook_thread;
1120 EnterCriticalSection(&dinput_hook_crit);
1122 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1123 if (!list_empty(&direct_input_list) && !hook_thread)
1125 HANDLE event;
1127 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1128 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1129 if (event && hook_thread)
1131 HANDLE handles[2];
1132 handles[0] = event;
1133 handles[1] = hook_thread;
1134 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1136 LeaveCriticalSection(&dinput_hook_crit);
1137 CloseHandle(event);
1139 else if (list_empty(&direct_input_list) && hook_thread)
1141 DWORD tid = hook_thread_id;
1143 hook_thread_id = 0;
1144 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1145 LeaveCriticalSection(&dinput_hook_crit);
1147 /* wait for hook thread to exit */
1148 WaitForSingleObject(hook_thread, INFINITE);
1149 CloseHandle(hook_thread);
1150 hook_thread = NULL;
1152 else
1153 LeaveCriticalSection(&dinput_hook_crit);
1155 return hook_thread_id != 0;
1158 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1160 static HHOOK callwndproc_hook;
1161 static ULONG foreground_cnt;
1162 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1164 EnterCriticalSection(&dinput_hook_crit);
1166 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1168 if (dev->acquired)
1169 foreground_cnt++;
1170 else
1171 foreground_cnt--;
1174 if (foreground_cnt && !callwndproc_hook)
1175 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1176 DINPUT_instance, GetCurrentThreadId() );
1177 else if (!foreground_cnt && callwndproc_hook)
1179 UnhookWindowsHookEx( callwndproc_hook );
1180 callwndproc_hook = NULL;
1183 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1185 LeaveCriticalSection(&dinput_hook_crit);