dinput: Remove redundant variable.
[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
421     FIXME( "(%p)->(%s): stub\n", This, debugstr_guid(rguid) );
422
423     return DI_OK;
424 }
425
426 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
427 {
428     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
429     return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
430 }
431
432 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
433                                                         HWND hwndOwner,
434                                                         DWORD dwFlags)
435 {
436     IDirectInputImpl *This = (IDirectInputImpl *)iface;
437
438     FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
439
440     return DI_OK;
441 }
442
443 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
444 {
445     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
446     return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
447 }
448
449 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
450                                                     LPCSTR pszName, LPGUID pguidInstance)
451 {
452     IDirectInputImpl *This = (IDirectInputImpl *)iface;
453
454     FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
455
456     return DI_OK;
457 }
458
459 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
460                                                     LPCWSTR pszName, LPGUID pguidInstance)
461 {
462     IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
463
464     FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
465
466     return DI_OK;
467 }
468
469 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
470                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
471 {
472   IDirectInputImpl *This = (IDirectInputImpl *)iface;
473   HRESULT ret_value = DIERR_DEVICENOTREG;
474   int i;
475
476   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
477
478   if (!rguid || !pvOut) return E_POINTER;
479
480   /* Loop on all the devices to see if anyone matches the given GUID */
481   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
482     HRESULT ret;
483
484     if (!dinput_devices[i]->create_deviceA) continue;
485     if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
486     {
487       EnterCriticalSection( &This->crit );
488       list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
489       LeaveCriticalSection( &This->crit );
490       return DI_OK;
491     }
492
493     if (ret == DIERR_NOINTERFACE)
494       ret_value = DIERR_NOINTERFACE;
495   }
496
497   if (ret_value == DIERR_NOINTERFACE)
498   {
499     WARN("invalid device GUID %s\n", debugstr_guid(rguid));
500   }
501
502   return ret_value;
503 }
504
505 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
506                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
507 {
508   IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
509   HRESULT ret_value = DIERR_DEVICENOTREG;
510   int i;
511
512   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
513
514   if (!rguid || !pvOut) return E_POINTER;
515
516   /* Loop on all the devices to see if anyone matches the given GUID */
517   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
518     HRESULT ret;
519
520     if (!dinput_devices[i]->create_deviceW) continue;
521     if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
522     {
523       EnterCriticalSection( &This->crit );
524       list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
525       LeaveCriticalSection( &This->crit );
526       return DI_OK;
527     }
528
529     if (ret == DIERR_NOINTERFACE)
530       ret_value = DIERR_NOINTERFACE;
531   }
532
533   return ret_value;
534 }
535
536 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
537                                                      LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
538 {
539     return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
540 }
541
542 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
543                                                      LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
544 {
545     return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
546 }
547
548 /*******************************************************************************
549  *      DirectInput8
550  */
551
552 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
553 {
554     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
555     return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
556 }
557
558 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
559 {
560     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
561     return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
562 }
563
564 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
565 {
566     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
567     return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
568 }
569
570 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
571 {
572     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
573     return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
574 }
575
576 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
577 {
578     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
579     return IDirectInputAImpl_Release( (IDirectInput7A *)This );
580 }
581
582 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
583 {
584     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
585     return IDirectInputAImpl_Release( (IDirectInput7A *)This );
586 }
587
588 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
589                                                       LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
590 {
591     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
592     return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
593 }
594
595 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
596                                                       LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
597 {
598     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
599     return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
600 }
601
602 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
603                                                      LPVOID pvRef, DWORD dwFlags)
604 {
605     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
606     return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
607 }
608
609 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
610                                                      LPVOID pvRef, DWORD dwFlags)
611 {
612     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
613     return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
614 }
615
616 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
617 {
618     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
619     return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
620 }
621
622 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
623 {
624     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
625     return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
626 }
627
628 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
629 {
630     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
631     return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
632 }
633
634 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
635 {
636     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
637     return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
638 }
639
640 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
641 {
642     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
643     return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
644 }
645
646 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
647 {
648     IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
649     return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
650 }
651
652 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
653 {
654     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
655     return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
656 }
657
658 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
659 {
660     IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
661     return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
662 }
663
664 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
665       LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
666       LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
667       LPVOID pvRef, DWORD dwFlags
668 )
669 {
670     IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
671
672     FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
673           lpCallback, pvRef, dwFlags);
674 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
675         X(DIEDBSFL_ATTACHEDONLY)
676         X(DIEDBSFL_THISUSER)
677         X(DIEDBSFL_FORCEFEEDBACK)
678         X(DIEDBSFL_AVAILABLEDEVICES)
679         X(DIEDBSFL_MULTIMICEKEYBOARDS)
680         X(DIEDBSFL_NONGAMINGDEVICES)
681 #undef X
682
683     _dump_diactionformatA(lpdiActionFormat);
684
685     return DI_OK;
686 }
687
688 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
689       LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
690       LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
691       LPVOID pvRef, DWORD dwFlags
692 )
693 {
694       IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
695
696       FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
697             lpCallback, pvRef, dwFlags);
698       return 0;
699 }
700
701 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
702       LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
703       LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
704 )
705 {
706       IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
707
708       FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
709             dwFlags, pvRefData);
710       return 0;
711 }
712
713 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
714       LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
715       LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
716 )
717 {
718       IDirectInputImpl *This = impl_from_IDirectInput8W( 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 const IDirectInput7AVtbl ddi7avt = {
726     IDirectInputAImpl_QueryInterface,
727     IDirectInputAImpl_AddRef,
728     IDirectInputAImpl_Release,
729     IDirectInputAImpl_CreateDevice,
730     IDirectInputAImpl_EnumDevices,
731     IDirectInputAImpl_GetDeviceStatus,
732     IDirectInputAImpl_RunControlPanel,
733     IDirectInputAImpl_Initialize,
734     IDirectInput2AImpl_FindDevice,
735     IDirectInput7AImpl_CreateDeviceEx
736 };
737
738 static const IDirectInput7WVtbl ddi7wvt = {
739     IDirectInputWImpl_QueryInterface,
740     IDirectInputWImpl_AddRef,
741     IDirectInputWImpl_Release,
742     IDirectInputWImpl_CreateDevice,
743     IDirectInputWImpl_EnumDevices,
744     IDirectInputWImpl_GetDeviceStatus,
745     IDirectInputWImpl_RunControlPanel,
746     IDirectInputWImpl_Initialize,
747     IDirectInput2WImpl_FindDevice,
748     IDirectInput7WImpl_CreateDeviceEx
749 };
750
751 static const IDirectInput8AVtbl ddi8avt = {
752     IDirectInput8AImpl_QueryInterface,
753     IDirectInput8AImpl_AddRef,
754     IDirectInput8AImpl_Release,
755     IDirectInput8AImpl_CreateDevice,
756     IDirectInput8AImpl_EnumDevices,
757     IDirectInput8AImpl_GetDeviceStatus,
758     IDirectInput8AImpl_RunControlPanel,
759     IDirectInput8AImpl_Initialize,
760     IDirectInput8AImpl_FindDevice,
761     IDirectInput8AImpl_EnumDevicesBySemantics,
762     IDirectInput8AImpl_ConfigureDevices
763 };
764
765 static const IDirectInput8WVtbl ddi8wvt = {
766     IDirectInput8WImpl_QueryInterface,
767     IDirectInput8WImpl_AddRef,
768     IDirectInput8WImpl_Release,
769     IDirectInput8WImpl_CreateDevice,
770     IDirectInput8WImpl_EnumDevices,
771     IDirectInput8WImpl_GetDeviceStatus,
772     IDirectInput8WImpl_RunControlPanel,
773     IDirectInput8WImpl_Initialize,
774     IDirectInput8WImpl_FindDevice,
775     IDirectInput8WImpl_EnumDevicesBySemantics,
776     IDirectInput8WImpl_ConfigureDevices
777 };
778
779 /*******************************************************************************
780  * DirectInput ClassFactory
781  */
782 typedef struct
783 {
784     /* IUnknown fields */
785     const IClassFactoryVtbl    *lpVtbl;
786     LONG                        ref;
787 } IClassFactoryImpl;
788
789 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
790         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
791
792         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
793         return E_NOINTERFACE;
794 }
795
796 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
797         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
798         return InterlockedIncrement(&(This->ref));
799 }
800
801 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
802         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
803         /* static class, won't be  freed */
804         return InterlockedDecrement(&(This->ref));
805 }
806
807 static HRESULT WINAPI DICF_CreateInstance(
808         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
809 ) {
810         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
811
812         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
813         if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
814              IsEqualGUID( &IID_IDirectInputW, riid ) ||
815              IsEqualGUID( &IID_IDirectInput2A, riid ) ||
816              IsEqualGUID( &IID_IDirectInput2W, riid ) ||
817              IsEqualGUID( &IID_IDirectInput7A, riid ) ||
818              IsEqualGUID( &IID_IDirectInput7W, riid ) ||
819              IsEqualGUID( &IID_IDirectInput8A, riid ) ||
820              IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
821                 /* FIXME: reuse already created dinput if present? */
822                 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
823         }
824
825         FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);    
826         return E_NOINTERFACE;
827 }
828
829 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
830         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
831         FIXME("(%p)->(%d),stub!\n",This,dolock);
832         return S_OK;
833 }
834
835 static const IClassFactoryVtbl DICF_Vtbl = {
836         DICF_QueryInterface,
837         DICF_AddRef,
838         DICF_Release,
839         DICF_CreateInstance,
840         DICF_LockServer
841 };
842 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
843
844 /***********************************************************************
845  *              DllCanUnloadNow (DINPUT.@)
846  */
847 HRESULT WINAPI DllCanUnloadNow(void)
848 {
849     FIXME("(void): stub\n");
850
851     return S_FALSE;
852 }
853
854 /***********************************************************************
855  *              DllGetClassObject (DINPUT.@)
856  */
857 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
858 {
859     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
860     if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
861         *ppv = (LPVOID)&DINPUT_CF;
862         IClassFactory_AddRef((IClassFactory*)*ppv);
863     return S_OK;
864     }
865
866     FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
867     return CLASS_E_CLASSNOTAVAILABLE;
868 }
869
870 /******************************************************************************
871  *      DInput hook thread
872  */
873
874 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
875 {
876     IDirectInputImpl *dinput;
877
878     if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
879
880     EnterCriticalSection( &dinput_hook_crit );
881     LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
882     {
883         IDirectInputDevice2AImpl *dev;
884
885         EnterCriticalSection( &dinput->crit );
886         LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
887             if (dev->acquired && dev->event_proc)
888             {
889                 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
890                 dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
891             }
892         LeaveCriticalSection( &dinput->crit );
893     }
894     LeaveCriticalSection( &dinput_hook_crit );
895
896     return CallNextHookEx( 0, code, wparam, lparam );
897 }
898
899 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
900 {
901     CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
902     IDirectInputImpl *dinput;
903     HWND foreground;
904
905     if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
906         msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
907         return CallNextHookEx( 0, code, wparam, lparam );
908
909     foreground = GetForegroundWindow();
910
911     EnterCriticalSection( &dinput_hook_crit );
912
913     LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
914     {
915         IDirectInputDevice2AImpl *dev;
916
917         EnterCriticalSection( &dinput->crit );
918         LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
919         {
920             if (!dev->acquired) continue;
921
922             if (msg->hwnd == dev->win && msg->hwnd != foreground)
923             {
924                 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
925                 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
926             }
927         }
928         LeaveCriticalSection( &dinput->crit );
929     }
930     LeaveCriticalSection( &dinput_hook_crit );
931
932     return CallNextHookEx( 0, code, wparam, lparam );
933 }
934
935 static DWORD WINAPI hook_thread_proc(void *param)
936 {
937     static HHOOK kbd_hook, mouse_hook;
938     MSG msg;
939
940     /* Force creation of the message queue */
941     PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
942     SetEvent(*(LPHANDLE)param);
943
944     while (GetMessageW( &msg, 0, 0, 0 ))
945     {
946         UINT kbd_cnt = 0, mice_cnt = 0;
947
948         if (msg.message == WM_USER+0x10)
949         {
950             IDirectInputImpl *dinput;
951
952             TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
953
954             if (!msg.wParam && !msg.lParam)
955             {
956                 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
957                 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
958                 kbd_hook = mouse_hook = NULL;
959                 break;
960             }
961
962             EnterCriticalSection( &dinput_hook_crit );
963
964             /* Count acquired keyboards and mice*/
965             LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
966             {
967                 IDirectInputDevice2AImpl *dev;
968
969                 EnterCriticalSection( &dinput->crit );
970                 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
971                 {
972                     if (!dev->acquired || !dev->event_proc) continue;
973
974                     if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
975                         IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
976                         kbd_cnt++;
977                     else
978                         if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
979                             IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
980                             mice_cnt++;
981                 }
982                 LeaveCriticalSection( &dinput->crit );
983             }
984             LeaveCriticalSection( &dinput_hook_crit );
985
986             if (kbd_cnt && !kbd_hook)
987                 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
988             else if (!kbd_cnt && kbd_hook)
989             {
990                 UnhookWindowsHookEx( kbd_hook );
991                 kbd_hook = NULL;
992             }
993
994             if (mice_cnt && !mouse_hook)
995                 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
996             else if (!mice_cnt && mouse_hook)
997             {
998                 UnhookWindowsHookEx( mouse_hook );
999                 mouse_hook = NULL;
1000             }
1001         }
1002         TranslateMessage(&msg);
1003         DispatchMessageW(&msg);
1004     }
1005
1006     return 0;
1007 }
1008
1009 static DWORD hook_thread_id;
1010
1011 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1012 {
1013     0, 0, &dinput_hook_crit,
1014     { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1015       0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1016 };
1017 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1018
1019 static BOOL check_hook_thread(void)
1020 {
1021     static HANDLE hook_thread;
1022
1023     EnterCriticalSection(&dinput_hook_crit);
1024
1025     TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1026     if (!list_empty(&direct_input_list) && !hook_thread)
1027     {
1028         HANDLE event;
1029
1030         event = CreateEventW(NULL, FALSE, FALSE, NULL);
1031         hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1032         if (event && hook_thread)
1033         {
1034             HANDLE handles[2];
1035             handles[0] = event;
1036             handles[1] = hook_thread;
1037             WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1038         }
1039         LeaveCriticalSection(&dinput_hook_crit);
1040         CloseHandle(event);
1041     }
1042     else if (list_empty(&direct_input_list) && hook_thread)
1043     {
1044         DWORD tid = hook_thread_id;
1045
1046         hook_thread_id = 0;
1047         PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1048         LeaveCriticalSection(&dinput_hook_crit);
1049
1050         /* wait for hook thread to exit */
1051         WaitForSingleObject(hook_thread, INFINITE);
1052         CloseHandle(hook_thread);
1053         hook_thread = NULL;
1054     }
1055     else
1056         LeaveCriticalSection(&dinput_hook_crit);
1057
1058     return hook_thread_id != 0;
1059 }
1060
1061 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1062 {
1063     static HHOOK callwndproc_hook;
1064     static ULONG foreground_cnt;
1065     IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1066
1067     EnterCriticalSection(&dinput_hook_crit);
1068
1069     if (dev->dwCoopLevel & DISCL_FOREGROUND)
1070     {
1071         if (dev->acquired)
1072             foreground_cnt++;
1073         else
1074             foreground_cnt--;
1075     }
1076
1077     if (foreground_cnt && !callwndproc_hook)
1078         callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1079                                               DINPUT_instance, GetCurrentThreadId() );
1080     else if (!foreground_cnt && callwndproc_hook)
1081     {
1082         UnhookWindowsHookEx( callwndproc_hook );
1083         callwndproc_hook = NULL;
1084     }
1085
1086     PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1087
1088     LeaveCriticalSection(&dinput_hook_crit);
1089 }