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
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
39 #include "wine/debug.h"
40 #include "wine/unicode.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[] =
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)
69 case DLL_PROCESS_ATTACH:
70 DisableThreadLibraryCalls(inst);
71 DINPUT_instance = inst;
73 case DLL_PROCESS_DETACH:
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,
89 IDirectInputImpl* This;
90 HRESULT res = DIERR_OLDDIRECTINPUTVERSION;
91 LPCVOID vtable = NULL;
93 TRACE("(%p,%04lx,%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))
103 if (IsEqualGUID(&IID_IDirectInputW,riid) ||
104 IsEqualGUID(&IID_IDirectInput2W,riid) ||
105 IsEqualGUID(&IID_IDirectInput7W,riid))
111 if (IsEqualGUID(&IID_IDirectInput8A,riid))
117 if (IsEqualGUID(&IID_IDirectInput8W,riid))
123 if (res == DI_OK && !create_hook_thread()) res = DIERR_GENERIC;
126 This = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectInputImpl));
127 This->lpVtbl = vtable;
129 This->dwVersion = dwVersion;
130 This->evsequence = 1;
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) {
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)) {
166 static const struct {
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)
179 DPRINTF("DIEDFL_ALLDEVICES");
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;
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");
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)
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;
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");
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)
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;
259 ref = InterlockedDecrement(&(This->ref));
262 HeapFree(GetProcessHeap(), 0, This);
263 release_hook_thread();
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);
280 TRACE("Unsupported interface !\n");
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);
296 TRACE("Unsupported interface !\n");
300 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(
301 LPDIRECTINPUT7A iface,REFGUID rguid,LPDIRECTINPUTDEVICEA* pdev,
304 IDirectInputImpl *This = (IDirectInputImpl *)iface;
305 HRESULT ret_value = DIERR_DEVICENOTREG;
308 TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
311 WARN("invalid pointer: pdev == NULL\n");
316 WARN("invalid pointer: rguid == NULL\n");
320 /* Loop on all the devices to see if anyone matches the given GUID */
321 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
323 if (!dinput_devices[i]->create_deviceA) continue;
324 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, NULL, pdev)) == DI_OK)
327 if (ret == DIERR_NOINTERFACE)
328 ret_value = DIERR_NOINTERFACE;
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;
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++) {
345 if (!dinput_devices[i]->create_deviceW) continue;
346 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, NULL, pdev)) == DI_OK)
349 if (ret == DIERR_NOINTERFACE)
350 ret_value = DIERR_NOINTERFACE;
356 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
357 TRACE("(this=%p,%p,%lx)\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.
366 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface,
368 IDirectInputImpl *This = (IDirectInputImpl *)iface;
370 FIXME("(%p)->(%s): stub\n",This,debugstr_guid(rguid));
375 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
378 IDirectInputImpl *This = (IDirectInputImpl *)iface;
379 FIXME("(%p)->(%p,%08lx): stub\n",This, hwndOwner, dwFlags);
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);
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);
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;
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++) {
412 if (!dinput_devices[i]->create_deviceA) continue;
413 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
416 if (ret == DIERR_NOINTERFACE)
417 ret_value = DIERR_NOINTERFACE;
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;
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++) {
435 if (!dinput_devices[i]->create_deviceW) continue;
436 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
439 if (ret == DIERR_NOINTERFACE)
440 ret_value = DIERR_NOINTERFACE;
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);
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);
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,%04lx): stub\n", This, ptszUserName, lpdiActionFormat,
483 lpCallback, pvRef, dwFlags);
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,%04lx): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
496 lpCallback, pvRef, dwFlags);
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,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
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,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
524 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
525 # define XCAST(fun) (typeof(ddi7avt.fun))
527 # define XCAST(fun) (void*)
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
544 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
545 # define XCAST(fun) (typeof(ddi7wvt.fun))
547 # define XCAST(fun) (void*)
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
564 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
565 # define XCAST(fun) (typeof(ddi8avt.fun))
567 # define XCAST(fun) (void*)
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
585 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
586 # define XCAST(fun) (typeof(ddi8wvt.fun))
588 # define XCAST(fun) (void*)
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
605 /*******************************************************************************
606 * DirectInput ClassFactory
610 /* IUnknown fields */
611 const IClassFactoryVtbl *lpVtbl;
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);
661 static const IClassFactoryVtbl DICF_Vtbl = {
668 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
670 /***********************************************************************
671 * DllCanUnloadNow (DINPUT.@)
673 HRESULT WINAPI DllCanUnloadNow(void)
675 FIXME("(void): stub\n");
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);
692 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
693 return CLASS_E_CLASSNOTAVAILABLE;
696 /******************************************************************************
700 static LRESULT CALLBACK dinput_hook_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
702 static HHOOK kbd_hook, mouse_hook;
705 TRACE("got message %x %p %p\n", message, (LPVOID)wParam, (LPVOID)lParam);
709 if (wParam == WH_KEYBOARD_LL)
713 if (kbd_hook) return 0;
714 kbd_hook = SetWindowsHookExW(WH_KEYBOARD_LL, (LPVOID)lParam, DINPUT_instance, 0);
715 return (LRESULT)kbd_hook;
719 if (!kbd_hook) return 0;
720 res = UnhookWindowsHookEx(kbd_hook);
725 else if (wParam == WH_MOUSE_LL)
729 if (mouse_hook) return 0;
730 mouse_hook = SetWindowsHookExW(WH_MOUSE_LL, (LPVOID)lParam, DINPUT_instance, 0);
731 return (LRESULT)mouse_hook;
735 if (!mouse_hook) return 0;
736 res = UnhookWindowsHookEx(mouse_hook);
741 else if (!wParam && !lParam)
747 if (kbd_hook) UnhookWindowsHookEx(kbd_hook);
748 if (mouse_hook) UnhookWindowsHookEx(mouse_hook);
751 return DefWindowProcW(hWnd, message, wParam, lParam);
754 static HWND hook_thread_hwnd;
755 static LONG hook_thread_refcount;
757 static DWORD WINAPI hook_thread_proc(void *param)
759 static const WCHAR classW[]={'H','o','o','k','_','L','L','_','C','L',0};
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);
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;
774 SetEvent(*(LPHANDLE)param);
777 while (GetMessageW(&msg, 0, 0, 0))
779 TranslateMessage(&msg);
780 DispatchMessageW(&msg);
784 else ERR("Error creating message window\n");
786 UnregisterClassW(wcex.lpszClassName, wcex.hInstance);
790 static CRITICAL_SECTION dinput_hook_crit;
791 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
793 0, 0, &dinput_hook_crit,
794 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
795 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
797 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
799 static BOOL create_hook_thread(void)
803 EnterCriticalSection(&dinput_hook_crit);
804 ref = ++hook_thread_refcount;
805 TRACE("Refcount %ld\n", ref);
809 HANDLE thread, event;
811 event = CreateEventW(NULL, FALSE, FALSE, NULL);
812 thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &tid);
818 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
823 LeaveCriticalSection(&dinput_hook_crit);
825 return hook_thread_hwnd != 0;
828 static void release_hook_thread(void)
832 EnterCriticalSection(&dinput_hook_crit);
833 ref = --hook_thread_refcount;
834 TRACE("Releasing to %ld\n", ref);
837 HWND hwnd = hook_thread_hwnd;
838 hook_thread_hwnd = 0;
839 SendMessageW(hwnd, WM_USER+0x10, 0, 0);
841 LeaveCriticalSection(&dinput_hook_crit);
844 HHOOK set_dinput_hook(int hook_id, LPVOID proc)
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);