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