dinput: Prevent race between destroying the hook window and unloading the dll.
[wine/multimedia.git] / dlls / dinput / dinput_main.c
blob402855e52179ea27a15073bab2242a8429e32bca
1 /* DirectInput
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2002 TransGaming Technologies Inc.
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.
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.
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
22 /* Status:
24 * - Tomb Raider 2 Demo:
25 * Playable using keyboard only.
26 * - WingCommander Prophecy Demo:
27 * Doesn't get Input Focus.
29 * - Fallout : works great in X and DGA mode
32 #include "config.h"
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <string.h>
37 #define COBJMACROS
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"
47 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
49 static const IDirectInput7AVtbl ddi7avt;
50 static const IDirectInput7WVtbl ddi7wvt;
51 static const IDirectInput8AVtbl ddi8avt;
52 static const IDirectInput8WVtbl ddi8wvt;
54 static const struct dinput_device *dinput_devices[] =
56 &mouse_device,
57 &keyboard_device,
58 &joystick_linuxinput_device,
59 &joystick_linux_device
61 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
63 HINSTANCE DINPUT_instance = NULL;
65 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
67 switch(reason)
69 case DLL_PROCESS_ATTACH:
70 DisableThreadLibraryCalls(inst);
71 DINPUT_instance = inst;
72 break;
73 case DLL_PROCESS_DETACH:
74 break;
76 return TRUE;
79 static BOOL create_hook_thread(void);
80 static void release_hook_thread(void);
82 /******************************************************************************
83 * DirectInputCreateEx (DINPUT.@)
85 HRESULT WINAPI DirectInputCreateEx(
86 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
87 LPUNKNOWN punkOuter)
89 IDirectInputImpl* This;
90 HRESULT res = DIERR_OLDDIRECTINPUTVERSION;
91 LPCVOID vtable = NULL;
93 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
95 if (IsEqualGUID(&IID_IDirectInputA,riid) ||
96 IsEqualGUID(&IID_IDirectInput2A,riid) ||
97 IsEqualGUID(&IID_IDirectInput7A,riid))
99 vtable = &ddi7avt;
100 res = DI_OK;
103 if (IsEqualGUID(&IID_IDirectInputW,riid) ||
104 IsEqualGUID(&IID_IDirectInput2W,riid) ||
105 IsEqualGUID(&IID_IDirectInput7W,riid))
107 vtable = &ddi7wvt;
108 res = DI_OK;
111 if (IsEqualGUID(&IID_IDirectInput8A,riid))
113 vtable = &ddi8avt;
114 res = DI_OK;
117 if (IsEqualGUID(&IID_IDirectInput8W,riid))
119 vtable = &ddi8wvt;
120 res = DI_OK;
123 if (res == DI_OK && !create_hook_thread()) res = DIERR_GENERIC;
124 if (res == DI_OK)
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;
133 return res;
136 /******************************************************************************
137 * DirectInputCreateA (DINPUT.@)
139 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
141 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
144 /******************************************************************************
145 * DirectInputCreateW (DINPUT.@)
147 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
149 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
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";
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
178 if (dwFlags == 0) {
179 DPRINTF("DIEDFL_ALLDEVICES");
180 return;
182 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
183 if (flags[i].mask & dwFlags)
184 DPRINTF("%s ",flags[i].name);
188 /******************************************************************************
189 * IDirectInputA_EnumDevices
191 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
192 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
193 LPVOID pvRef, DWORD dwFlags)
195 IDirectInputImpl *This = (IDirectInputImpl *)iface;
196 DIDEVICEINSTANCEA devInstance;
197 int i, j, r;
199 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
200 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
201 lpCallback, pvRef, dwFlags);
202 TRACE(" flags: "); _dump_EnumDevices_dwFlags(dwFlags); TRACE("\n");
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;
216 return 0;
218 /******************************************************************************
219 * IDirectInputW_EnumDevices
221 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
222 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
223 LPVOID pvRef, DWORD dwFlags)
225 IDirectInputImpl *This = (IDirectInputImpl *)iface;
226 DIDEVICEINSTANCEW devInstance;
227 int i, j, r;
229 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
230 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
231 lpCallback, pvRef, dwFlags);
232 TRACE(" flags: "); _dump_EnumDevices_dwFlags(dwFlags); TRACE("\n");
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;
246 return 0;
249 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
251 IDirectInputImpl *This = (IDirectInputImpl *)iface;
252 return InterlockedIncrement((&This->ref));
255 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
257 IDirectInputImpl *This = (IDirectInputImpl *)iface;
258 ULONG ref;
259 ref = InterlockedDecrement(&(This->ref));
260 if (ref == 0)
262 HeapFree(GetProcessHeap(), 0, This);
263 release_hook_thread();
265 return ref;
268 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj) {
269 IDirectInputImpl *This = (IDirectInputImpl *)iface;
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;
280 TRACE("Unsupported interface !\n");
281 return E_FAIL;
284 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj) {
285 IDirectInputImpl *This = (IDirectInputImpl *)iface;
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;
296 TRACE("Unsupported interface !\n");
297 return E_FAIL;
300 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(
301 LPDIRECTINPUT7A iface,REFGUID rguid,LPDIRECTINPUTDEVICEA* pdev,
302 LPUNKNOWN punk
304 IDirectInputImpl *This = (IDirectInputImpl *)iface;
305 HRESULT ret_value = DIERR_DEVICENOTREG;
306 int i;
308 TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
310 if (pdev == NULL) {
311 WARN("invalid pointer: pdev == NULL\n");
312 return E_POINTER;
315 if (rguid == NULL) {
316 WARN("invalid pointer: rguid == NULL\n");
317 return E_POINTER;
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;
327 if (ret == DIERR_NOINTERFACE)
328 ret_value = DIERR_NOINTERFACE;
331 return ret_value;
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;
340 TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
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;
349 if (ret == DIERR_NOINTERFACE)
350 ret_value = DIERR_NOINTERFACE;
353 return ret_value;
356 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
357 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
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.
363 return DI_OK;
366 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface,
367 REFGUID rguid) {
368 IDirectInputImpl *This = (IDirectInputImpl *)iface;
370 FIXME("(%p)->(%s): stub\n",This,debugstr_guid(rguid));
372 return DI_OK;
375 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
376 HWND hwndOwner,
377 DWORD dwFlags) {
378 IDirectInputImpl *This = (IDirectInputImpl *)iface;
379 FIXME("(%p)->(%p,%08x): stub\n",This, hwndOwner, dwFlags);
381 return DI_OK;
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);
389 return DI_OK;
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);
397 return DI_OK;
400 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
401 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
403 IDirectInputImpl *This = (IDirectInputImpl *)iface;
404 HRESULT ret_value = DIERR_DEVICENOTREG;
405 int i;
407 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
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;
416 if (ret == DIERR_NOINTERFACE)
417 ret_value = DIERR_NOINTERFACE;
420 return ret_value;
423 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
424 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
426 IDirectInputImpl *This = (IDirectInputImpl *)iface;
427 HRESULT ret_value = DIERR_DEVICENOTREG;
428 int i;
430 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
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;
439 if (ret == DIERR_NOINTERFACE)
440 ret_value = DIERR_NOINTERFACE;
443 return ret_value;
446 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj) {
447 IDirectInputImpl *This = (IDirectInputImpl *)iface;
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;
456 TRACE("Unsupported interface !\n");
457 return E_NOINTERFACE;
460 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj) {
461 IDirectInputImpl *This = (IDirectInputImpl *)iface;
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;
470 TRACE("Unsupported interface !\n");
471 return E_NOINTERFACE;
474 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
475 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
476 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
477 LPVOID pvRef, DWORD dwFlags
480 IDirectInputImpl *This = (IDirectInputImpl *)iface;
482 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
483 lpCallback, pvRef, dwFlags);
484 return 0;
487 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
488 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
489 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
490 LPVOID pvRef, DWORD dwFlags
493 IDirectInputImpl *This = (IDirectInputImpl *)iface;
495 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
496 lpCallback, pvRef, dwFlags);
497 return 0;
500 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
501 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
502 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
505 IDirectInputImpl *This = (IDirectInputImpl *)iface;
507 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
508 dwFlags, pvRefData);
509 return 0;
512 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
513 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
514 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
517 IDirectInputImpl *This = (IDirectInputImpl *)iface;
519 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
520 dwFlags, pvRefData);
521 return 0;
524 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
525 # define XCAST(fun) (typeof(ddi7avt.fun))
526 #else
527 # define XCAST(fun) (void*)
528 #endif
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
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
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
562 #undef XCAST
564 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
565 # define XCAST(fun) (typeof(ddi8avt.fun))
566 #else
567 # define XCAST(fun) (void*)
568 #endif
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
583 #undef XCAST
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
603 #undef XCAST
605 /*******************************************************************************
606 * DirectInput ClassFactory
608 typedef struct
610 /* IUnknown fields */
611 const IClassFactoryVtbl *lpVtbl;
612 LONG ref;
613 } IClassFactoryImpl;
615 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
616 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
618 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
619 return E_NOINTERFACE;
622 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
623 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
624 return InterlockedIncrement(&(This->ref));
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));
633 static HRESULT WINAPI DICF_CreateInstance(
634 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
636 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
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);
651 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
652 return E_NOINTERFACE;
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;
661 static const IClassFactoryVtbl DICF_Vtbl = {
662 DICF_QueryInterface,
663 DICF_AddRef,
664 DICF_Release,
665 DICF_CreateInstance,
666 DICF_LockServer
668 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
670 /***********************************************************************
671 * DllCanUnloadNow (DINPUT.@)
673 HRESULT WINAPI DllCanUnloadNow(void)
675 FIXME("(void): stub\n");
677 return S_FALSE;
680 /***********************************************************************
681 * DllGetClassObject (DINPUT.@)
683 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
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;
692 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
693 return CLASS_E_CLASSNOTAVAILABLE;
696 /******************************************************************************
697 * DInput hook thread
700 static LRESULT CALLBACK dinput_hook_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
702 static HHOOK kbd_hook, mouse_hook;
703 BOOL res;
705 TRACE("got message %x %p %p\n", message, (LPVOID)wParam, (LPVOID)lParam);
706 switch (message)
708 case WM_USER+0x10:
709 if (wParam == WH_KEYBOARD_LL)
711 if (lParam)
713 if (kbd_hook) return 0;
714 kbd_hook = SetWindowsHookExW(WH_KEYBOARD_LL, (LPVOID)lParam, DINPUT_instance, 0);
715 return (LRESULT)kbd_hook;
717 else
719 if (!kbd_hook) return 0;
720 res = UnhookWindowsHookEx(kbd_hook);
721 kbd_hook = NULL;
722 return res;
725 else if (wParam == WH_MOUSE_LL)
727 if (lParam)
729 if (mouse_hook) return 0;
730 mouse_hook = SetWindowsHookExW(WH_MOUSE_LL, (LPVOID)lParam, DINPUT_instance, 0);
731 return (LRESULT)mouse_hook;
733 else
735 if (!mouse_hook) return 0;
736 res = UnhookWindowsHookEx(mouse_hook);
737 mouse_hook = NULL;
738 return res;
741 else if (!wParam && !lParam)
742 DestroyWindow(hWnd);
744 return 0;
746 case WM_DESTROY:
747 if (kbd_hook) UnhookWindowsHookEx(kbd_hook);
748 if (mouse_hook) UnhookWindowsHookEx(mouse_hook);
749 PostQuitMessage(0);
751 return DefWindowProcW(hWnd, message, wParam, lParam);
754 static HWND hook_thread_hwnd;
755 static LONG hook_thread_refcount;
756 static HANDLE hook_thread;
758 static const WCHAR classW[]={'H','o','o','k','_','L','L','_','C','L',0};
760 static DWORD WINAPI hook_thread_proc(void *param)
762 MSG msg;
763 HWND hwnd;
765 hwnd = CreateWindowExW(0, classW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, 0);
766 hook_thread_hwnd = hwnd;
768 SetEvent(*(LPHANDLE)param);
769 if (hwnd)
771 while (GetMessageW(&msg, 0, 0, 0))
773 TranslateMessage(&msg);
774 DispatchMessageW(&msg);
776 DestroyWindow(hwnd);
778 else ERR("Error creating message window\n");
780 UnregisterClassW(classW, DINPUT_instance);
781 return 0;
784 static CRITICAL_SECTION dinput_hook_crit;
785 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
787 0, 0, &dinput_hook_crit,
788 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
789 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
791 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
793 static BOOL create_hook_thread(void)
795 LONG ref;
797 EnterCriticalSection(&dinput_hook_crit);
799 ref = ++hook_thread_refcount;
800 TRACE("Refcount %d\n", ref);
801 if (ref == 1)
803 DWORD tid;
804 HANDLE event;
806 /* Create window class */
807 WNDCLASSEXW wcex;
808 memset(&wcex, 0, sizeof(wcex));
809 wcex.cbSize = sizeof(wcex);
810 wcex.lpfnWndProc = dinput_hook_WndProc;
811 wcex.lpszClassName = classW;
812 wcex.hInstance = DINPUT_instance;
813 if (!RegisterClassExW(&wcex))
814 ERR("Error registering window class\n");
816 event = CreateEventW(NULL, FALSE, FALSE, NULL);
817 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &tid);
818 if (event && hook_thread)
820 HANDLE handles[2];
821 handles[0] = event;
822 handles[1] = hook_thread;
823 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
825 CloseHandle(event);
827 LeaveCriticalSection(&dinput_hook_crit);
829 return hook_thread_hwnd != 0;
832 static void release_hook_thread(void)
834 LONG ref;
836 EnterCriticalSection(&dinput_hook_crit);
837 ref = --hook_thread_refcount;
838 TRACE("Releasing to %d\n", ref);
839 if (ref == 0)
841 HWND hwnd = hook_thread_hwnd;
842 hook_thread_hwnd = 0;
843 SendMessageW(hwnd, WM_USER+0x10, 0, 0);
844 /* wait for hook thread to exit */
845 WaitForSingleObject(hook_thread, INFINITE);
846 CloseHandle(hook_thread);
848 LeaveCriticalSection(&dinput_hook_crit);
851 HHOOK set_dinput_hook(int hook_id, LPVOID proc)
853 HWND hwnd;
855 EnterCriticalSection(&dinput_hook_crit);
856 hwnd = hook_thread_hwnd;
857 LeaveCriticalSection(&dinput_hook_crit);
858 return (HHOOK)SendMessageW(hwnd, WM_USER+0x10, (WPARAM)hook_id, (LPARAM)proc);