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