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