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