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