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