dmstyle: Assign to structs instead of using memcpy.
[wine] / dlls / dinput / dinput_main.c
1 /*              DirectInput
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2002 TransGaming Technologies Inc.
6  * Copyright 2007 Vitaliy Margolen
7  *
8  *
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.
13  *
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.
18  *
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
22  */
23 /* Status:
24  *
25  * - Tomb Raider 2 Demo:
26  *   Playable using keyboard only.
27  * - WingCommander Prophecy Demo:
28  *   Doesn't get Input Focus.
29  *
30  * - Fallout : works great in X and DGA mode
31  */
32
33 #include "config.h"
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <string.h>
37
38 #define COBJMACROS
39
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winuser.h"
45 #include "winerror.h"
46 #include "dinput_private.h"
47 #include "device_private.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
50
51 static const IDirectInput7AVtbl ddi7avt;
52 static const IDirectInput7WVtbl ddi7wvt;
53 static const IDirectInput8AVtbl ddi8avt;
54 static const IDirectInput8WVtbl ddi8wvt;
55
56 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
57 {
58     return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl7w );
59 }
60
61 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
62 {
63     return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8a );
64 }
65
66 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
67 {
68     return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8w );
69 }
70
71 static inline IDirectInput7W *IDirectInput7W_from_impl( IDirectInputImpl *iface )
72 {
73     return (IDirectInput7W *)(&iface->lpVtbl7w);
74 }
75
76 static const struct dinput_device *dinput_devices[] =
77 {
78     &mouse_device,
79     &keyboard_device,
80     &joystick_linuxinput_device,
81     &joystick_linux_device
82 };
83 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
84
85 static HINSTANCE DINPUT_instance = NULL;
86
87 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
88 {
89     switch(reason)
90     {
91       case DLL_PROCESS_ATTACH:
92         DisableThreadLibraryCalls(inst);
93         DINPUT_instance = inst;
94         break;
95       case DLL_PROCESS_DETACH:
96         break;
97     }
98     return TRUE;
99 }
100
101 static BOOL check_hook_thread(void);
102 static CRITICAL_SECTION dinput_hook_crit;
103 static struct list direct_input_list = LIST_INIT( direct_input_list );
104
105 /******************************************************************************
106  *      DirectInputCreateEx (DINPUT.@)
107  */
108 HRESULT WINAPI DirectInputCreateEx(
109         HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
110         LPUNKNOWN punkOuter) 
111 {
112     IDirectInputImpl* This;
113
114     TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
115
116     if (IsEqualGUID( &IID_IDirectInputA,  riid ) ||
117         IsEqualGUID( &IID_IDirectInput2A, riid ) ||
118         IsEqualGUID( &IID_IDirectInput7A, riid ) ||
119         IsEqualGUID( &IID_IDirectInputW,  riid ) ||
120         IsEqualGUID( &IID_IDirectInput2W, riid ) ||
121         IsEqualGUID( &IID_IDirectInput7W, riid ) ||
122         IsEqualGUID( &IID_IDirectInput8A, riid ) ||
123         IsEqualGUID( &IID_IDirectInput8W, riid ))
124     {
125         if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
126             return DIERR_OUTOFMEMORY;
127     }
128     else
129         return DIERR_OLDDIRECTINPUTVERSION;
130
131     This->lpVtbl      = &ddi7avt;
132     This->lpVtbl7w    = &ddi7wvt;
133     This->lpVtbl8a    = &ddi8avt;
134     This->lpVtbl8w    = &ddi8wvt;
135     This->ref         = 0;
136     This->dwVersion   = dwVersion;
137     This->evsequence  = 1;
138
139     InitializeCriticalSection(&This->crit);
140     This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
141
142     list_init( &This->devices_list );
143
144     /* Add self to the list of the IDirectInputs */
145     EnterCriticalSection( &dinput_hook_crit );
146     list_add_head( &direct_input_list, &This->entry );
147     LeaveCriticalSection( &dinput_hook_crit );
148
149     if (!check_hook_thread())
150     {
151         IUnknown_Release( (LPDIRECTINPUT7A)This );
152         return DIERR_GENERIC;
153     }
154
155     IDirectInput_QueryInterface( (IDirectInput7A *)This, riid, ppDI );
156     return DI_OK;
157 }
158
159 /******************************************************************************
160  *      DirectInputCreateA (DINPUT.@)
161  */
162 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
163 {
164     return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
165 }
166
167 /******************************************************************************
168  *      DirectInputCreateW (DINPUT.@)
169  */
170 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
171 {
172     return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
173 }
174
175 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
176     switch (dwDevType) {
177         case 0: return "All devices";
178         case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
179         case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
180         case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
181         case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
182         default: return "Unknown";
183     }
184 }
185
186 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
187     if (TRACE_ON(dinput)) {
188         unsigned int   i;
189         static const struct {
190             DWORD       mask;
191             const char  *name;
192         } flags[] = {
193 #define FE(x) { x, #x}
194             FE(DIEDFL_ALLDEVICES),
195             FE(DIEDFL_ATTACHEDONLY),
196             FE(DIEDFL_FORCEFEEDBACK),
197             FE(DIEDFL_INCLUDEALIASES),
198             FE(DIEDFL_INCLUDEPHANTOMS)
199 #undef FE
200         };
201         TRACE(" flags: ");
202         if (dwFlags == 0) {
203             TRACE("DIEDFL_ALLDEVICES");
204             return;
205         }
206         for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
207             if (flags[i].mask & dwFlags)
208                 TRACE("%s ",flags[i].name);
209     }
210     TRACE("\n");
211 }
212
213 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
214     int i;
215
216     FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
217     FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
218     FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
219     FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
220     FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
221     for (i=0;i<lpdiActionFormat->dwNumActions;i++) {
222         FIXME("diaf.rgoAction[%d]:\n", i);
223         FIXME("\tuAppData=%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
224         FIXME("\tdwSemantics=%x\n", lpdiActionFormat->rgoAction[i].dwSemantics);
225         FIXME("\tdwFlags=%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
226         FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].lptszActionName));
227         FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
228         FIXME("\tdwObjID=%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
229         FIXME("\tdwHow=%x\n", lpdiActionFormat->rgoAction[i].dwHow);
230     }
231     FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
232     FIXME("diaf.dwGenre = %d\n", lpdiActionFormat->dwGenre);
233     FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
234     FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
235     FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
236     FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
237     FIXME("diaf.ftTimeStamp ...\n");
238     FIXME("diaf.dwCRC = %x\n", lpdiActionFormat->dwCRC);
239     FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
240 }
241
242 /******************************************************************************
243  *      IDirectInputA_EnumDevices
244  */
245 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
246         LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
247         LPVOID pvRef, DWORD dwFlags)
248 {
249     IDirectInputImpl *This = (IDirectInputImpl *)iface;
250     DIDEVICEINSTANCEA devInstance;
251     int i, j, r;
252     
253     TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
254           This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
255           lpCallback, pvRef, dwFlags);
256     _dump_EnumDevices_dwFlags(dwFlags);
257
258     for (i = 0; i < NB_DINPUT_DEVICES; i++) {
259         if (!dinput_devices[i]->enum_deviceA) continue;
260         for (j = 0, r = -1; r != 0; j++) {
261             devInstance.dwSize = sizeof(devInstance);
262             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
263             if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
264                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
265                     return 0;
266             }
267         }
268     }
269     
270     return 0;
271 }
272 /******************************************************************************
273  *      IDirectInputW_EnumDevices
274  */
275 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
276         LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
277         LPVOID pvRef, DWORD dwFlags) 
278 {
279     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
280     DIDEVICEINSTANCEW devInstance;
281     int i, j, r;
282     
283     TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
284           This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
285           lpCallback, pvRef, dwFlags);
286     _dump_EnumDevices_dwFlags(dwFlags);
287
288     for (i = 0; i < NB_DINPUT_DEVICES; i++) {
289         if (!dinput_devices[i]->enum_deviceW) continue;
290         for (j = 0, r = -1; r != 0; j++) {
291             devInstance.dwSize = sizeof(devInstance);
292             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
293             if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
294                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
295                     return 0;
296             }
297         }
298     }
299     
300     return 0;
301 }
302
303 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
304 {
305     IDirectInputImpl *This = (IDirectInputImpl *)iface;
306     ULONG ref = InterlockedIncrement(&This->ref);
307
308     TRACE( "(%p) incrementing from %d\n", This, ref - 1);
309     return ref;
310 }
311
312 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
313 {
314     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
315     return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
316 }
317
318 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
319 {
320     IDirectInputImpl *This = (IDirectInputImpl *)iface;
321     ULONG ref = InterlockedDecrement( &This->ref );
322
323     TRACE( "(%p) releasing from %d\n", This, ref + 1 );
324
325     if (ref) return ref;
326
327     /* Remove self from the list of the IDirectInputs */
328     EnterCriticalSection( &dinput_hook_crit );
329     list_remove( &This->entry );
330     LeaveCriticalSection( &dinput_hook_crit );
331
332     check_hook_thread();
333
334     This->crit.DebugInfo->Spare[0] = 0;
335     DeleteCriticalSection( &This->crit );
336     HeapFree( GetProcessHeap(), 0, This );
337
338     return 0;
339 }
340
341 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
342 {
343     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
344     return IDirectInputAImpl_Release( (IDirectInput7A *)This );
345 }
346
347 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
348 {
349     IDirectInputImpl *This = (IDirectInputImpl *)iface;
350
351     TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
352
353     if (IsEqualGUID( &IID_IUnknown, riid ) ||
354         IsEqualGUID( &IID_IDirectInputA,  riid ) ||
355         IsEqualGUID( &IID_IDirectInput2A, riid ) ||
356         IsEqualGUID( &IID_IDirectInput7A, riid ))
357     {
358         *ppobj = &This->lpVtbl;
359         IUnknown_AddRef( (IUnknown*)*ppobj );
360
361         return DI_OK;
362     }
363
364     if (IsEqualGUID( &IID_IDirectInputW,  riid ) ||
365         IsEqualGUID( &IID_IDirectInput2W, riid ) ||
366         IsEqualGUID( &IID_IDirectInput7W, riid ))
367     {
368         *ppobj = &This->lpVtbl7w;
369         IUnknown_AddRef( (IUnknown*)*ppobj );
370
371         return DI_OK;
372     }
373
374     if (IsEqualGUID( &IID_IDirectInput8A, riid ))
375     {
376         *ppobj = &This->lpVtbl8a;
377         IUnknown_AddRef( (IUnknown*)*ppobj );
378
379         return DI_OK;
380     }
381
382     if (IsEqualGUID( &IID_IDirectInput8W, riid ))
383     {
384         *ppobj = &This->lpVtbl8w;
385         IUnknown_AddRef( (IUnknown*)*ppobj );
386
387         return DI_OK;
388     }
389
390     FIXME( "Unsupported interface !\n" );
391     return E_FAIL;
392 }
393
394 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
395 {
396     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
397     return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
398 }
399
400 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
401         TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
402         
403         /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
404          * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
405          * which broke applications like Tomb Raider Legend because it isn't a legal return value.
406          */
407         return DI_OK;
408 }
409
410 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
411 {
412     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
413     return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
414 }
415
416 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
417 {
418     IDirectInputImpl *This = (IDirectInputImpl *)iface;
419
420     FIXME( "(%p)->(%s): stub\n", This, debugstr_guid(rguid) );
421
422     return DI_OK;
423 }
424
425 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
426 {
427     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
428     return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
429 }
430
431 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
432                                                         HWND hwndOwner,
433                                                         DWORD dwFlags)
434 {
435     IDirectInputImpl *This = (IDirectInputImpl *)iface;
436
437     FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
438
439     return DI_OK;
440 }
441
442 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
443 {
444     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
445     return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
446 }
447
448 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
449                                                     LPCSTR pszName, LPGUID pguidInstance)
450 {
451     IDirectInputImpl *This = (IDirectInputImpl *)iface;
452
453     FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
454
455     return DI_OK;
456 }
457
458 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
459                                                     LPCWSTR pszName, LPGUID pguidInstance)
460 {
461     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
462
463     FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
464
465     return DI_OK;
466 }
467
468 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
469                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
470 {
471   IDirectInputImpl *This = (IDirectInputImpl *)iface;
472   HRESULT ret_value = DIERR_DEVICENOTREG;
473   int i;
474
475   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
476
477   if (!rguid || !pvOut) return E_POINTER;
478
479   /* Loop on all the devices to see if anyone matches the given GUID */
480   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
481     HRESULT ret;
482
483     if (!dinput_devices[i]->create_deviceA) continue;
484     if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
485     {
486       EnterCriticalSection( &This->crit );
487       list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
488       LeaveCriticalSection( &This->crit );
489       return DI_OK;
490     }
491
492     if (ret == DIERR_NOINTERFACE)
493       ret_value = DIERR_NOINTERFACE;
494   }
495
496   if (ret_value == DIERR_NOINTERFACE)
497   {
498     WARN("invalid device GUID %s\n", debugstr_guid(rguid));
499   }
500
501   return ret_value;
502 }
503
504 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
505                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
506 {
507   IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
508   HRESULT ret_value = DIERR_DEVICENOTREG;
509   int i;
510
511   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
512
513   if (!rguid || !pvOut) return E_POINTER;
514
515   /* Loop on all the devices to see if anyone matches the given GUID */
516   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
517     HRESULT ret;
518
519     if (!dinput_devices[i]->create_deviceW) continue;
520     if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
521     {
522       EnterCriticalSection( &This->crit );
523       list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
524       LeaveCriticalSection( &This->crit );
525       return DI_OK;
526     }
527
528     if (ret == DIERR_NOINTERFACE)
529       ret_value = DIERR_NOINTERFACE;
530   }
531
532   return ret_value;
533 }
534
535 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
536                                                      LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
537 {
538     return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
539 }
540
541 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
542                                                      LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
543 {
544     return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
545 }
546
547 /*******************************************************************************
548  *      DirectInput8
549  */
550
551 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
552 {
553     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
554     return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
555 }
556
557 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
558 {
559     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
560     return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
561 }
562
563 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
564 {
565     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
566     return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
567 }
568
569 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
570 {
571     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
572     return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
573 }
574
575 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
576 {
577     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
578     return IDirectInputAImpl_Release( (IDirectInput7A *)This );
579 }
580
581 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
582 {
583     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
584     return IDirectInputAImpl_Release( (IDirectInput7A *)This );
585 }
586
587 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
588                                                       LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
589 {
590     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
591     return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
592 }
593
594 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
595                                                       LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
596 {
597     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
598     return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
599 }
600
601 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
602                                                      LPVOID pvRef, DWORD dwFlags)
603 {
604     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
605     return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
606 }
607
608 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
609                                                      LPVOID pvRef, DWORD dwFlags)
610 {
611     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
612     return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
613 }
614
615 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
616 {
617     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
618     return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
619 }
620
621 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
622 {
623     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
624     return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
625 }
626
627 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
628 {
629     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
630     return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
631 }
632
633 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
634 {
635     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
636     return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
637 }
638
639 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
640 {
641     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
642     return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
643 }
644
645 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
646 {
647     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
648     return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
649 }
650
651 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
652 {
653     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
654     return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
655 }
656
657 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
658 {
659     IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
660     return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
661 }
662
663 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
664       LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
665       LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
666       LPVOID pvRef, DWORD dwFlags
667 )
668 {
669     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
670
671     FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
672           lpCallback, pvRef, dwFlags);
673 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
674         X(DIEDBSFL_ATTACHEDONLY)
675         X(DIEDBSFL_THISUSER)
676         X(DIEDBSFL_FORCEFEEDBACK)
677         X(DIEDBSFL_AVAILABLEDEVICES)
678         X(DIEDBSFL_MULTIMICEKEYBOARDS)
679         X(DIEDBSFL_NONGAMINGDEVICES)
680 #undef X
681
682     _dump_diactionformatA(lpdiActionFormat);
683
684     return DI_OK;
685 }
686
687 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
688       LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
689       LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
690       LPVOID pvRef, DWORD dwFlags
691 )
692 {
693       IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
694
695       FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
696             lpCallback, pvRef, dwFlags);
697       return 0;
698 }
699
700 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
701       LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
702       LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
703 )
704 {
705       IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
706
707       FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
708             dwFlags, pvRefData);
709       return 0;
710 }
711
712 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
713       LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
714       LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
715 )
716 {
717       IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
718
719       FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
720             dwFlags, pvRefData);
721       return 0;
722 }
723
724 static const IDirectInput7AVtbl ddi7avt = {
725     IDirectInputAImpl_QueryInterface,
726     IDirectInputAImpl_AddRef,
727     IDirectInputAImpl_Release,
728     IDirectInputAImpl_CreateDevice,
729     IDirectInputAImpl_EnumDevices,
730     IDirectInputAImpl_GetDeviceStatus,
731     IDirectInputAImpl_RunControlPanel,
732     IDirectInputAImpl_Initialize,
733     IDirectInput2AImpl_FindDevice,
734     IDirectInput7AImpl_CreateDeviceEx
735 };
736
737 static const IDirectInput7WVtbl ddi7wvt = {
738     IDirectInputWImpl_QueryInterface,
739     IDirectInputWImpl_AddRef,
740     IDirectInputWImpl_Release,
741     IDirectInputWImpl_CreateDevice,
742     IDirectInputWImpl_EnumDevices,
743     IDirectInputWImpl_GetDeviceStatus,
744     IDirectInputWImpl_RunControlPanel,
745     IDirectInputWImpl_Initialize,
746     IDirectInput2WImpl_FindDevice,
747     IDirectInput7WImpl_CreateDeviceEx
748 };
749
750 static const IDirectInput8AVtbl ddi8avt = {
751     IDirectInput8AImpl_QueryInterface,
752     IDirectInput8AImpl_AddRef,
753     IDirectInput8AImpl_Release,
754     IDirectInput8AImpl_CreateDevice,
755     IDirectInput8AImpl_EnumDevices,
756     IDirectInput8AImpl_GetDeviceStatus,
757     IDirectInput8AImpl_RunControlPanel,
758     IDirectInput8AImpl_Initialize,
759     IDirectInput8AImpl_FindDevice,
760     IDirectInput8AImpl_EnumDevicesBySemantics,
761     IDirectInput8AImpl_ConfigureDevices
762 };
763
764 static const IDirectInput8WVtbl ddi8wvt = {
765     IDirectInput8WImpl_QueryInterface,
766     IDirectInput8WImpl_AddRef,
767     IDirectInput8WImpl_Release,
768     IDirectInput8WImpl_CreateDevice,
769     IDirectInput8WImpl_EnumDevices,
770     IDirectInput8WImpl_GetDeviceStatus,
771     IDirectInput8WImpl_RunControlPanel,
772     IDirectInput8WImpl_Initialize,
773     IDirectInput8WImpl_FindDevice,
774     IDirectInput8WImpl_EnumDevicesBySemantics,
775     IDirectInput8WImpl_ConfigureDevices
776 };
777
778 /*******************************************************************************
779  * DirectInput ClassFactory
780  */
781 typedef struct
782 {
783     /* IUnknown fields */
784     const IClassFactoryVtbl    *lpVtbl;
785     LONG                        ref;
786 } IClassFactoryImpl;
787
788 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
789         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
790
791         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
792         return E_NOINTERFACE;
793 }
794
795 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
796         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
797         return InterlockedIncrement(&(This->ref));
798 }
799
800 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
801         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
802         /* static class, won't be  freed */
803         return InterlockedDecrement(&(This->ref));
804 }
805
806 static HRESULT WINAPI DICF_CreateInstance(
807         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
808 ) {
809         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
810
811         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
812         if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
813              IsEqualGUID( &IID_IDirectInputW, riid ) ||
814              IsEqualGUID( &IID_IDirectInput2A, riid ) ||
815              IsEqualGUID( &IID_IDirectInput2W, riid ) ||
816              IsEqualGUID( &IID_IDirectInput7A, riid ) ||
817              IsEqualGUID( &IID_IDirectInput7W, riid ) ||
818              IsEqualGUID( &IID_IDirectInput8A, riid ) ||
819              IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
820                 /* FIXME: reuse already created dinput if present? */
821                 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
822         }
823
824         FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);    
825         return E_NOINTERFACE;
826 }
827
828 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
829         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
830         FIXME("(%p)->(%d),stub!\n",This,dolock);
831         return S_OK;
832 }
833
834 static const IClassFactoryVtbl DICF_Vtbl = {
835         DICF_QueryInterface,
836         DICF_AddRef,
837         DICF_Release,
838         DICF_CreateInstance,
839         DICF_LockServer
840 };
841 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
842
843 /***********************************************************************
844  *              DllCanUnloadNow (DINPUT.@)
845  */
846 HRESULT WINAPI DllCanUnloadNow(void)
847 {
848     FIXME("(void): stub\n");
849
850     return S_FALSE;
851 }
852
853 /***********************************************************************
854  *              DllGetClassObject (DINPUT.@)
855  */
856 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
857 {
858     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
859     if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
860         *ppv = (LPVOID)&DINPUT_CF;
861         IClassFactory_AddRef((IClassFactory*)*ppv);
862     return S_OK;
863     }
864
865     FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
866     return CLASS_E_CLASSNOTAVAILABLE;
867 }
868
869 /******************************************************************************
870  *      DInput hook thread
871  */
872
873 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
874 {
875     IDirectInputImpl *dinput;
876
877     if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
878
879     EnterCriticalSection( &dinput_hook_crit );
880     LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
881     {
882         IDirectInputDevice2AImpl *dev;
883
884         EnterCriticalSection( &dinput->crit );
885         LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
886             if (dev->acquired && dev->event_proc)
887             {
888                 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
889                 dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
890             }
891         LeaveCriticalSection( &dinput->crit );
892     }
893     LeaveCriticalSection( &dinput_hook_crit );
894
895     return CallNextHookEx( 0, code, wparam, lparam );
896 }
897
898 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
899 {
900     CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
901     IDirectInputImpl *dinput;
902     HWND foreground;
903
904     if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
905         msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
906         return CallNextHookEx( 0, code, wparam, lparam );
907
908     foreground = GetForegroundWindow();
909
910     EnterCriticalSection( &dinput_hook_crit );
911
912     LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
913     {
914         IDirectInputDevice2AImpl *dev;
915
916         EnterCriticalSection( &dinput->crit );
917         LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
918         {
919             if (!dev->acquired) continue;
920
921             if (msg->hwnd == dev->win && msg->hwnd != foreground)
922             {
923                 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
924                 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
925             }
926         }
927         LeaveCriticalSection( &dinput->crit );
928     }
929     LeaveCriticalSection( &dinput_hook_crit );
930
931     return CallNextHookEx( 0, code, wparam, lparam );
932 }
933
934 static DWORD WINAPI hook_thread_proc(void *param)
935 {
936     static HHOOK kbd_hook, mouse_hook;
937     MSG msg;
938
939     /* Force creation of the message queue */
940     PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
941     SetEvent(*(LPHANDLE)param);
942
943     while (GetMessageW( &msg, 0, 0, 0 ))
944     {
945         UINT kbd_cnt = 0, mice_cnt = 0;
946
947         if (msg.message == WM_USER+0x10)
948         {
949             IDirectInputImpl *dinput;
950
951             TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
952
953             if (!msg.wParam && !msg.lParam)
954             {
955                 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
956                 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
957                 kbd_hook = mouse_hook = NULL;
958                 break;
959             }
960
961             EnterCriticalSection( &dinput_hook_crit );
962
963             /* Count acquired keyboards and mice*/
964             LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
965             {
966                 IDirectInputDevice2AImpl *dev;
967
968                 EnterCriticalSection( &dinput->crit );
969                 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
970                 {
971                     if (!dev->acquired || !dev->event_proc) continue;
972
973                     if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
974                         IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
975                         kbd_cnt++;
976                     else
977                         if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
978                             IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
979                             mice_cnt++;
980                 }
981                 LeaveCriticalSection( &dinput->crit );
982             }
983             LeaveCriticalSection( &dinput_hook_crit );
984
985             if (kbd_cnt && !kbd_hook)
986                 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
987             else if (!kbd_cnt && kbd_hook)
988             {
989                 UnhookWindowsHookEx( kbd_hook );
990                 kbd_hook = NULL;
991             }
992
993             if (mice_cnt && !mouse_hook)
994                 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
995             else if (!mice_cnt && mouse_hook)
996             {
997                 UnhookWindowsHookEx( mouse_hook );
998                 mouse_hook = NULL;
999             }
1000         }
1001         TranslateMessage(&msg);
1002         DispatchMessageW(&msg);
1003     }
1004
1005     return 0;
1006 }
1007
1008 static DWORD hook_thread_id;
1009
1010 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1011 {
1012     0, 0, &dinput_hook_crit,
1013     { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1014       0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1015 };
1016 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1017
1018 static BOOL check_hook_thread(void)
1019 {
1020     static HANDLE hook_thread;
1021
1022     EnterCriticalSection(&dinput_hook_crit);
1023
1024     TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1025     if (!list_empty(&direct_input_list) && !hook_thread)
1026     {
1027         HANDLE event;
1028
1029         event = CreateEventW(NULL, FALSE, FALSE, NULL);
1030         hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1031         if (event && hook_thread)
1032         {
1033             HANDLE handles[2];
1034             handles[0] = event;
1035             handles[1] = hook_thread;
1036             WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1037         }
1038         LeaveCriticalSection(&dinput_hook_crit);
1039         CloseHandle(event);
1040     }
1041     else if (list_empty(&direct_input_list) && hook_thread)
1042     {
1043         DWORD tid = hook_thread_id;
1044
1045         hook_thread_id = 0;
1046         PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1047         LeaveCriticalSection(&dinput_hook_crit);
1048
1049         /* wait for hook thread to exit */
1050         WaitForSingleObject(hook_thread, INFINITE);
1051         CloseHandle(hook_thread);
1052         hook_thread = NULL;
1053     }
1054     else
1055         LeaveCriticalSection(&dinput_hook_crit);
1056
1057     return hook_thread_id != 0;
1058 }
1059
1060 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1061 {
1062     static HHOOK callwndproc_hook;
1063     static ULONG foreground_cnt;
1064     IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1065
1066     EnterCriticalSection(&dinput_hook_crit);
1067
1068     if (dev->dwCoopLevel & DISCL_FOREGROUND)
1069     {
1070         if (dev->acquired)
1071             foreground_cnt++;
1072         else
1073             foreground_cnt--;
1074     }
1075
1076     if (foreground_cnt && !callwndproc_hook)
1077         callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1078                                               DINPUT_instance, GetCurrentThreadId() );
1079     else if (!foreground_cnt && callwndproc_hook)
1080     {
1081         UnhookWindowsHookEx( callwndproc_hook );
1082         callwndproc_hook = NULL;
1083     }
1084
1085     PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1086
1087     LeaveCriticalSection(&dinput_hook_crit);
1088 }