dinput: Return proper errors in SetCooperativeLevel.
[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  *
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 /* Status:
23  *
24  * - Tomb Raider 2 Demo:
25  *   Playable using keyboard only.
26  * - WingCommander Prophecy Demo:
27  *   Doesn't get Input Focus.
28  *
29  * - Fallout : works great in X and DGA mode
30  */
31
32 #include "config.h"
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <string.h>
36
37 #define COBJMACROS
38
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winuser.h"
44 #include "winerror.h"
45 #include "dinput_private.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
48
49 static const IDirectInput7AVtbl ddi7avt;
50 static const IDirectInput7WVtbl ddi7wvt;
51 static const IDirectInput8AVtbl ddi8avt;
52 static const IDirectInput8WVtbl ddi8wvt;
53
54 static const struct dinput_device *dinput_devices[] =
55 {
56     &mouse_device,
57     &keyboard_device,
58     &joystick_linuxinput_device,
59     &joystick_linux_device
60 };
61 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
62
63 HINSTANCE DINPUT_instance = NULL;
64
65 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
66 {
67     switch(reason)
68     {
69       case DLL_PROCESS_ATTACH:
70         DisableThreadLibraryCalls(inst);
71         DINPUT_instance = inst;
72         break;
73       case DLL_PROCESS_DETACH:
74         break;
75     }
76     return TRUE;
77 }
78
79 static BOOL create_hook_thread(void);
80 static void release_hook_thread(void);
81
82 /******************************************************************************
83  *      DirectInputCreateEx (DINPUT.@)
84  */
85 HRESULT WINAPI DirectInputCreateEx(
86         HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
87         LPUNKNOWN punkOuter) 
88 {
89     IDirectInputImpl* This;
90     HRESULT res = DIERR_OLDDIRECTINPUTVERSION;
91     LPCVOID vtable = NULL;
92
93     TRACE("(%p,%04lx,%s,%p,%p)\n", hinst,dwVersion,debugstr_guid(riid),ppDI,punkOuter);
94
95     if (IsEqualGUID(&IID_IDirectInputA,riid) ||
96         IsEqualGUID(&IID_IDirectInput2A,riid) ||
97         IsEqualGUID(&IID_IDirectInput7A,riid))
98     {
99         vtable = &ddi7avt;
100         res = DI_OK;
101     }
102
103     if (IsEqualGUID(&IID_IDirectInputW,riid) ||
104         IsEqualGUID(&IID_IDirectInput2W,riid) ||
105         IsEqualGUID(&IID_IDirectInput7W,riid))
106     {
107         vtable = &ddi7wvt;
108         res = DI_OK;
109     }
110
111     if (IsEqualGUID(&IID_IDirectInput8A,riid))
112     {
113         vtable = &ddi8avt;
114         res = DI_OK;
115     }
116
117     if (IsEqualGUID(&IID_IDirectInput8W,riid))
118     {
119         vtable = &ddi8wvt;
120         res = DI_OK;
121     }
122
123     if (res == DI_OK && !create_hook_thread()) res = DIERR_GENERIC;
124     if (res == DI_OK)
125     {
126         This = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectInputImpl));
127         This->lpVtbl = vtable;
128         This->ref = 1;
129         This->dwVersion = dwVersion;
130         This->evsequence = 1;
131         *ppDI = This;
132     }
133     return res;
134 }
135
136 /******************************************************************************
137  *      DirectInputCreateA (DINPUT.@)
138  */
139 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
140 {
141     return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
142 }
143
144 /******************************************************************************
145  *      DirectInputCreateW (DINPUT.@)
146  */
147 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
148 {
149     return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
150 }
151
152 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
153     switch (dwDevType) {
154         case 0: return "All devices";
155         case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
156         case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
157         case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
158         case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
159         default: return "Unknown";
160     }
161 }
162
163 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
164     if (TRACE_ON(dinput)) {
165         unsigned int   i;
166         static const struct {
167             DWORD       mask;
168             const char  *name;
169         } flags[] = {
170 #define FE(x) { x, #x}
171             FE(DIEDFL_ALLDEVICES),
172             FE(DIEDFL_ATTACHEDONLY),
173             FE(DIEDFL_FORCEFEEDBACK),
174             FE(DIEDFL_INCLUDEALIASES),
175             FE(DIEDFL_INCLUDEPHANTOMS)
176 #undef FE
177         };
178         if (dwFlags == 0) {
179             DPRINTF("DIEDFL_ALLDEVICES");
180             return;
181         }
182         for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
183             if (flags[i].mask & dwFlags)
184                 DPRINTF("%s ",flags[i].name);
185     }
186 }
187
188 /******************************************************************************
189  *      IDirectInputA_EnumDevices
190  */
191 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
192         LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
193         LPVOID pvRef, DWORD dwFlags)
194 {
195     IDirectInputImpl *This = (IDirectInputImpl *)iface;
196     DIDEVICEINSTANCEA devInstance;
197     int i, j, r;
198     
199     TRACE("(this=%p,0x%04lx '%s',%p,%p,%04lx)\n",
200           This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
201           lpCallback, pvRef, dwFlags);
202     TRACE(" flags: "); _dump_EnumDevices_dwFlags(dwFlags); TRACE("\n");
203
204     for (i = 0; i < NB_DINPUT_DEVICES; i++) {
205         if (!dinput_devices[i]->enum_deviceA) continue;
206         for (j = 0, r = -1; r != 0; j++) {
207             devInstance.dwSize = sizeof(devInstance);
208             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
209             if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
210                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
211                     return 0;
212             }
213         }
214     }
215     
216     return 0;
217 }
218 /******************************************************************************
219  *      IDirectInputW_EnumDevices
220  */
221 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
222         LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
223         LPVOID pvRef, DWORD dwFlags) 
224 {
225     IDirectInputImpl *This = (IDirectInputImpl *)iface;
226     DIDEVICEINSTANCEW devInstance;
227     int i, j, r;
228     
229     TRACE("(this=%p,0x%04lx '%s',%p,%p,%04lx)\n",
230           This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
231           lpCallback, pvRef, dwFlags);
232     TRACE(" flags: "); _dump_EnumDevices_dwFlags(dwFlags); TRACE("\n");
233
234     for (i = 0; i < NB_DINPUT_DEVICES; i++) {
235         if (!dinput_devices[i]->enum_deviceW) continue;
236         for (j = 0, r = -1; r != 0; j++) {
237             devInstance.dwSize = sizeof(devInstance);
238             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
239             if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
240                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
241                     return 0;
242             }
243         }
244     }
245     
246     return 0;
247 }
248
249 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
250 {
251         IDirectInputImpl *This = (IDirectInputImpl *)iface;
252         return InterlockedIncrement((&This->ref));
253 }
254
255 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
256 {
257         IDirectInputImpl *This = (IDirectInputImpl *)iface;
258         ULONG ref;
259         ref = InterlockedDecrement(&(This->ref));
260         if (ref == 0)
261         {
262             HeapFree(GetProcessHeap(), 0, This);
263             release_hook_thread();
264         }
265         return ref;
266 }
267
268 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj) {
269         IDirectInputImpl *This = (IDirectInputImpl *)iface;
270
271         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
272         if (IsEqualGUID(&IID_IUnknown,riid) ||
273             IsEqualGUID(&IID_IDirectInputA,riid) ||
274             IsEqualGUID(&IID_IDirectInput2A,riid) ||
275             IsEqualGUID(&IID_IDirectInput7A,riid)) {
276                 IDirectInputAImpl_AddRef(iface);
277                 *ppobj = This;
278                 return 0;
279         }
280         TRACE("Unsupported interface !\n");
281         return E_FAIL;
282 }
283
284 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj) {
285         IDirectInputImpl *This = (IDirectInputImpl *)iface;
286
287         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
288         if (IsEqualGUID(&IID_IUnknown,riid) ||
289             IsEqualGUID(&IID_IDirectInputW,riid) ||
290             IsEqualGUID(&IID_IDirectInput2W,riid) ||
291             IsEqualGUID(&IID_IDirectInput7W,riid)) {
292                 IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
293                 *ppobj = This;
294                 return 0;
295         }
296         TRACE("Unsupported interface !\n");
297         return E_FAIL;
298 }
299
300 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(
301         LPDIRECTINPUT7A iface,REFGUID rguid,LPDIRECTINPUTDEVICEA* pdev,
302         LPUNKNOWN punk
303 ) {
304         IDirectInputImpl *This = (IDirectInputImpl *)iface;
305         HRESULT ret_value = DIERR_DEVICENOTREG;
306         int i;
307
308         TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
309
310         if (pdev == NULL) {
311                 WARN("invalid pointer: pdev == NULL\n");
312                 return E_POINTER;
313         }
314
315         if (rguid == NULL) {
316                 WARN("invalid pointer: rguid == NULL\n");
317                 return E_POINTER;
318         }
319
320         /* Loop on all the devices to see if anyone matches the given GUID */
321         for (i = 0; i < NB_DINPUT_DEVICES; i++) {
322           HRESULT ret;
323           if (!dinput_devices[i]->create_deviceA) continue;
324           if ((ret = dinput_devices[i]->create_deviceA(This, rguid, NULL, pdev)) == DI_OK)
325             return DI_OK;
326
327           if (ret == DIERR_NOINTERFACE)
328             ret_value = DIERR_NOINTERFACE;
329         }
330
331         return ret_value;
332 }
333
334 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, 
335                                                      REFGUID rguid, LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk) {
336         IDirectInputImpl *This = (IDirectInputImpl *)iface;
337         HRESULT ret_value = DIERR_DEVICENOTREG;
338         int i;
339
340         TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
341
342         /* Loop on all the devices to see if anyone matches the given GUID */
343         for (i = 0; i < NB_DINPUT_DEVICES; i++) {
344           HRESULT ret;
345           if (!dinput_devices[i]->create_deviceW) continue;
346           if ((ret = dinput_devices[i]->create_deviceW(This, rguid, NULL, pdev)) == DI_OK)
347             return DI_OK;
348
349           if (ret == DIERR_NOINTERFACE)
350             ret_value = DIERR_NOINTERFACE;
351         }
352
353         return ret_value;
354 }
355
356 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
357         TRACE("(this=%p,%p,%lx)\n",iface, hinst, x);
358         
359         /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
360          * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
361          * which broke applications like Tomb Raider Legend because it isn't a legal return value.
362          */
363         return DI_OK;
364 }
365
366 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface,
367                                                         REFGUID rguid) {
368   IDirectInputImpl *This = (IDirectInputImpl *)iface;
369
370   FIXME("(%p)->(%s): stub\n",This,debugstr_guid(rguid));
371
372   return DI_OK;
373 }
374
375 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
376                                                         HWND hwndOwner,
377                                                         DWORD dwFlags) {
378   IDirectInputImpl *This = (IDirectInputImpl *)iface;
379   FIXME("(%p)->(%p,%08lx): stub\n",This, hwndOwner, dwFlags);
380
381   return DI_OK;
382 }
383
384 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
385                                                     LPCSTR pszName, LPGUID pguidInstance) {
386   IDirectInputImpl *This = (IDirectInputImpl *)iface;
387   FIXME("(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance);
388
389   return DI_OK;
390 }
391
392 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
393                                                     LPCWSTR pszName, LPGUID pguidInstance) {
394   IDirectInputImpl *This = (IDirectInputImpl *)iface;
395   FIXME("(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance);
396
397   return DI_OK;
398 }
399
400 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
401                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
402 {
403   IDirectInputImpl *This = (IDirectInputImpl *)iface;
404   HRESULT ret_value = DIERR_DEVICENOTREG;
405   int i;
406
407   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
408
409   /* Loop on all the devices to see if anyone matches the given GUID */
410   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
411     HRESULT ret;
412     if (!dinput_devices[i]->create_deviceA) continue;
413     if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
414       return DI_OK;
415
416     if (ret == DIERR_NOINTERFACE)
417       ret_value = DIERR_NOINTERFACE;
418   }
419
420   return ret_value;
421 }
422
423 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
424                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
425 {
426   IDirectInputImpl *This = (IDirectInputImpl *)iface;
427   HRESULT ret_value = DIERR_DEVICENOTREG;
428   int i;
429
430   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
431
432   /* Loop on all the devices to see if anyone matches the given GUID */
433   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
434     HRESULT ret;
435     if (!dinput_devices[i]->create_deviceW) continue;
436     if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
437       return DI_OK;
438
439     if (ret == DIERR_NOINTERFACE)
440       ret_value = DIERR_NOINTERFACE;
441   }
442
443   return ret_value;
444 }
445
446 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj) {
447       IDirectInputImpl *This = (IDirectInputImpl *)iface;
448
449       TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
450       if (IsEqualGUID(&IID_IUnknown,riid) ||
451           IsEqualGUID(&IID_IDirectInput8A,riid)) {
452               IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
453               *ppobj = This;
454               return 0;
455       }
456       TRACE("Unsupported interface !\n");
457       return E_NOINTERFACE;
458 }
459
460 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj) {
461       IDirectInputImpl *This = (IDirectInputImpl *)iface;
462
463       TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
464       if (IsEqualGUID(&IID_IUnknown,riid) ||
465           IsEqualGUID(&IID_IDirectInput8W,riid)) {
466               IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
467               *ppobj = This;
468               return 0;
469       }
470       TRACE("Unsupported interface !\n");
471       return E_NOINTERFACE;
472 }
473
474 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
475       LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
476       LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
477       LPVOID pvRef, DWORD dwFlags
478 )
479 {
480       IDirectInputImpl *This = (IDirectInputImpl *)iface;
481
482       FIXME("(this=%p,%s,%p,%p,%p,%04lx): stub\n", This, ptszUserName, lpdiActionFormat,
483             lpCallback, pvRef, dwFlags);
484       return 0;
485 }
486
487 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
488       LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
489       LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
490       LPVOID pvRef, DWORD dwFlags
491 )
492 {
493       IDirectInputImpl *This = (IDirectInputImpl *)iface;
494
495       FIXME("(this=%p,%s,%p,%p,%p,%04lx): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
496             lpCallback, pvRef, dwFlags);
497       return 0;
498 }
499
500 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
501       LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
502       LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
503 )
504 {
505       IDirectInputImpl *This = (IDirectInputImpl *)iface;
506
507       FIXME("(this=%p,%p,%p,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
508             dwFlags, pvRefData);
509       return 0;
510 }
511
512 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
513       LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
514       LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
515 )
516 {
517       IDirectInputImpl *This = (IDirectInputImpl *)iface;
518
519       FIXME("(this=%p,%p,%p,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
520             dwFlags, pvRefData);
521       return 0;
522 }
523
524 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
525 # define XCAST(fun)   (typeof(ddi7avt.fun))
526 #else
527 # define XCAST(fun)     (void*)
528 #endif
529
530 static const IDirectInput7AVtbl ddi7avt = {
531         XCAST(QueryInterface)IDirectInputAImpl_QueryInterface,
532         XCAST(AddRef)IDirectInputAImpl_AddRef,
533         XCAST(Release)IDirectInputAImpl_Release,
534         XCAST(CreateDevice)IDirectInputAImpl_CreateDevice,
535         XCAST(EnumDevices)IDirectInputAImpl_EnumDevices,
536         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
537         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
538         XCAST(Initialize)IDirectInputAImpl_Initialize,
539         XCAST(FindDevice)IDirectInput2AImpl_FindDevice,
540         XCAST(CreateDeviceEx)IDirectInput7AImpl_CreateDeviceEx
541 };
542
543 #undef XCAST
544 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
545 # define XCAST(fun)   (typeof(ddi7wvt.fun))
546 #else
547 # define XCAST(fun)     (void*)
548 #endif
549
550 static const IDirectInput7WVtbl ddi7wvt = {
551         XCAST(QueryInterface)IDirectInputWImpl_QueryInterface,
552         XCAST(AddRef)IDirectInputAImpl_AddRef,
553         XCAST(Release)IDirectInputAImpl_Release,
554         XCAST(CreateDevice)IDirectInputWImpl_CreateDevice,
555         XCAST(EnumDevices)IDirectInputWImpl_EnumDevices,
556         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
557         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
558         XCAST(Initialize)IDirectInputAImpl_Initialize,
559         XCAST(FindDevice)IDirectInput2WImpl_FindDevice,
560         XCAST(CreateDeviceEx)IDirectInput7WImpl_CreateDeviceEx
561 };
562 #undef XCAST
563
564 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
565 # define XCAST(fun)     (typeof(ddi8avt.fun))
566 #else
567 # define XCAST(fun)     (void*)
568 #endif
569
570 static const IDirectInput8AVtbl ddi8avt = {
571         XCAST(QueryInterface)IDirectInput8AImpl_QueryInterface,
572         XCAST(AddRef)IDirectInputAImpl_AddRef,
573         XCAST(Release)IDirectInputAImpl_Release,
574         XCAST(CreateDevice)IDirectInputAImpl_CreateDevice,
575         XCAST(EnumDevices)IDirectInputAImpl_EnumDevices,
576         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
577         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
578         XCAST(Initialize)IDirectInputAImpl_Initialize,
579         XCAST(FindDevice)IDirectInput2AImpl_FindDevice,
580         XCAST(EnumDevicesBySemantics)IDirectInput8AImpl_EnumDevicesBySemantics,
581         XCAST(ConfigureDevices)IDirectInput8AImpl_ConfigureDevices
582 };
583 #undef XCAST
584
585 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
586 # define XCAST(fun)     (typeof(ddi8wvt.fun))
587 #else
588 # define XCAST(fun)     (void*)
589 #endif
590 static const IDirectInput8WVtbl ddi8wvt = {
591         XCAST(QueryInterface)IDirectInput8WImpl_QueryInterface,
592         XCAST(AddRef)IDirectInputAImpl_AddRef,
593         XCAST(Release)IDirectInputAImpl_Release,
594         XCAST(CreateDevice)IDirectInputWImpl_CreateDevice,
595         XCAST(EnumDevices)IDirectInputWImpl_EnumDevices,
596         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
597         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
598         XCAST(Initialize)IDirectInputAImpl_Initialize,
599         XCAST(FindDevice)IDirectInput2WImpl_FindDevice,
600         XCAST(EnumDevicesBySemantics)IDirectInput8WImpl_EnumDevicesBySemantics,
601         XCAST(ConfigureDevices)IDirectInput8WImpl_ConfigureDevices
602 };
603 #undef XCAST
604
605 /*******************************************************************************
606  * DirectInput ClassFactory
607  */
608 typedef struct
609 {
610     /* IUnknown fields */
611     const IClassFactoryVtbl    *lpVtbl;
612     LONG                        ref;
613 } IClassFactoryImpl;
614
615 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
616         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
617
618         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
619         return E_NOINTERFACE;
620 }
621
622 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
623         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
624         return InterlockedIncrement(&(This->ref));
625 }
626
627 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
628         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
629         /* static class, won't be  freed */
630         return InterlockedDecrement(&(This->ref));
631 }
632
633 static HRESULT WINAPI DICF_CreateInstance(
634         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
635 ) {
636         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
637
638         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
639         if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
640              IsEqualGUID( &IID_IDirectInputW, riid ) ||
641              IsEqualGUID( &IID_IDirectInput2A, riid ) ||
642              IsEqualGUID( &IID_IDirectInput2W, riid ) ||
643              IsEqualGUID( &IID_IDirectInput7A, riid ) ||
644              IsEqualGUID( &IID_IDirectInput7W, riid ) ||
645              IsEqualGUID( &IID_IDirectInput8A, riid ) ||
646              IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
647                 /* FIXME: reuse already created dinput if present? */
648                 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
649         }
650
651         FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);    
652         return E_NOINTERFACE;
653 }
654
655 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
656         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
657         FIXME("(%p)->(%d),stub!\n",This,dolock);
658         return S_OK;
659 }
660
661 static const IClassFactoryVtbl DICF_Vtbl = {
662         DICF_QueryInterface,
663         DICF_AddRef,
664         DICF_Release,
665         DICF_CreateInstance,
666         DICF_LockServer
667 };
668 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
669
670 /***********************************************************************
671  *              DllCanUnloadNow (DINPUT.@)
672  */
673 HRESULT WINAPI DllCanUnloadNow(void)
674 {
675     FIXME("(void): stub\n");
676
677     return S_FALSE;
678 }
679
680 /***********************************************************************
681  *              DllGetClassObject (DINPUT.@)
682  */
683 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
684 {
685     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
686     if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
687         *ppv = (LPVOID)&DINPUT_CF;
688         IClassFactory_AddRef((IClassFactory*)*ppv);
689     return S_OK;
690     }
691
692     FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
693     return CLASS_E_CLASSNOTAVAILABLE;
694 }
695
696 /******************************************************************************
697  *      DInput hook thread
698  */
699
700 static LRESULT CALLBACK dinput_hook_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
701 {
702     static HHOOK kbd_hook, mouse_hook;
703     BOOL res;
704
705     TRACE("got message %x %p %p\n", message, (LPVOID)wParam, (LPVOID)lParam);
706     switch (message)
707     {
708     case WM_USER+0x10:
709         if (wParam == WH_KEYBOARD_LL)
710         {
711             if (lParam)
712             {
713                 if (kbd_hook) return 0;
714                 kbd_hook = SetWindowsHookExW(WH_KEYBOARD_LL, (LPVOID)lParam, DINPUT_instance, 0);
715                 return (LRESULT)kbd_hook;
716             }
717             else
718             {
719                 if (!kbd_hook) return 0;
720                 res = UnhookWindowsHookEx(kbd_hook);
721                 kbd_hook = NULL;
722                 return res;
723             }
724         }
725         else if (wParam == WH_MOUSE_LL)
726         {
727             if (lParam)
728             {
729                 if (mouse_hook) return 0;
730                 mouse_hook = SetWindowsHookExW(WH_MOUSE_LL, (LPVOID)lParam, DINPUT_instance, 0);
731                 return (LRESULT)mouse_hook;
732             }
733             else
734             {
735                 if (!mouse_hook) return 0;
736                 res = UnhookWindowsHookEx(mouse_hook);
737                 mouse_hook = NULL;
738                 return res;
739             }
740         }
741         else if (!wParam && !lParam)
742             DestroyWindow(hWnd);
743
744         return 0;
745
746     case WM_DESTROY:
747         if (kbd_hook) UnhookWindowsHookEx(kbd_hook);
748         if (mouse_hook) UnhookWindowsHookEx(mouse_hook);
749         PostQuitMessage(0);
750     }
751     return DefWindowProcW(hWnd, message, wParam, lParam);
752 }
753
754 static HWND hook_thread_hwnd;
755 static LONG hook_thread_refcount;
756
757 static DWORD WINAPI hook_thread_proc(void *param)
758 {
759     static const WCHAR classW[]={'H','o','o','k','_','L','L','_','C','L',0};
760     MSG msg;
761     WNDCLASSEXW wcex;
762     HWND hwnd;
763
764     memset(&wcex, 0, sizeof(wcex));
765     wcex.cbSize = sizeof(wcex);
766     wcex.lpfnWndProc = dinput_hook_WndProc;
767     wcex.lpszClassName = classW;
768     wcex.hInstance = GetModuleHandleW(0);
769
770     if (!RegisterClassExW(&wcex)) ERR("Error registering window class\n");
771     hwnd = CreateWindowExW(0, classW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, 0);
772     hook_thread_hwnd = hwnd;
773
774     SetEvent(*(LPHANDLE)param);
775     if (hwnd)
776     {
777         while (GetMessageW(&msg, 0, 0, 0))
778         {
779             TranslateMessage(&msg);
780             DispatchMessageW(&msg);
781         }
782         DestroyWindow(hwnd);
783     }
784     else ERR("Error creating message window\n");
785
786     UnregisterClassW(wcex.lpszClassName, wcex.hInstance);
787     return 0;
788 }
789
790 static CRITICAL_SECTION dinput_hook_crit;
791 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
792 {
793     0, 0, &dinput_hook_crit,
794     { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
795       0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
796 };
797 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
798
799 static BOOL create_hook_thread(void)
800 {
801     LONG ref;
802
803     EnterCriticalSection(&dinput_hook_crit);
804     ref = ++hook_thread_refcount;
805     TRACE("Refcount %ld\n", ref);
806     if (ref == 1)
807     {
808         DWORD tid;
809         HANDLE thread, event;
810
811         event = CreateEventW(NULL, FALSE, FALSE, NULL);
812         thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &tid);
813         if (event && thread)
814         {
815             HANDLE handles[2];
816             handles[0] = event;
817             handles[1] = thread;
818             WaitForMultipleObjects(2, handles, FALSE, INFINITE);
819         }
820         CloseHandle(event);
821         CloseHandle(thread);
822     }
823     LeaveCriticalSection(&dinput_hook_crit);
824
825     return hook_thread_hwnd != 0;
826 }
827
828 static void release_hook_thread(void)
829 {
830     LONG ref;
831
832     EnterCriticalSection(&dinput_hook_crit);
833     ref = --hook_thread_refcount;
834     TRACE("Releasing to %ld\n", ref);
835     if (ref == 0) 
836     {
837         HWND hwnd = hook_thread_hwnd;
838         hook_thread_hwnd = 0;
839         SendMessageW(hwnd, WM_USER+0x10, 0, 0);
840     }
841     LeaveCriticalSection(&dinput_hook_crit);
842 }
843
844 HHOOK set_dinput_hook(int hook_id, LPVOID proc)
845 {
846     HWND hwnd;
847
848     EnterCriticalSection(&dinput_hook_crit);
849     hwnd = hook_thread_hwnd;
850     LeaveCriticalSection(&dinput_hook_crit);
851     return (HHOOK)SendMessageW(hwnd, WM_USER+0x10, (WPARAM)hook_id, (LPARAM)proc);
852 }