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