push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / dinput / dinput_main.c
blob5fa21f0d6a4f400848c11e4da8bf225802b2437d
1 /* DirectInput
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2002 TransGaming Technologies Inc.
6 * Copyright 2007 Vitaliy Margolen
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.
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.
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
23 /* Status:
25 * - Tomb Raider 2 Demo:
26 * Playable using keyboard only.
27 * - WingCommander Prophecy Demo:
28 * Doesn't get Input Focus.
30 * - Fallout : works great in X and DGA mode
33 #include "config.h"
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <string.h>
38 #define COBJMACROS
39 #define NONAMELESSUNION
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 "dinput_private.h"
48 #include "device_private.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
52 static const IDirectInput7AVtbl ddi7avt;
53 static const IDirectInput7WVtbl ddi7wvt;
54 static const IDirectInput8AVtbl ddi8avt;
55 static const IDirectInput8WVtbl ddi8wvt;
57 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
59 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl7w );
62 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
64 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8a );
67 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
69 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8w );
72 static inline IDirectInput7W *IDirectInput7W_from_impl( IDirectInputImpl *iface )
74 return (IDirectInput7W *)(&iface->lpVtbl7w);
77 static const struct dinput_device *dinput_devices[] =
79 &mouse_device,
80 &keyboard_device,
81 &joystick_linuxinput_device,
82 &joystick_linux_device
84 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
86 static HINSTANCE DINPUT_instance = NULL;
88 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
90 switch(reason)
92 case DLL_PROCESS_ATTACH:
93 DisableThreadLibraryCalls(inst);
94 DINPUT_instance = inst;
95 break;
96 case DLL_PROCESS_DETACH:
97 break;
99 return TRUE;
102 static BOOL check_hook_thread(void);
103 static CRITICAL_SECTION dinput_hook_crit;
104 static struct list direct_input_list = LIST_INIT( direct_input_list );
106 /******************************************************************************
107 * DirectInputCreateEx (DINPUT.@)
109 HRESULT WINAPI DirectInputCreateEx(
110 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
111 LPUNKNOWN punkOuter)
113 IDirectInputImpl* This;
115 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
117 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
118 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
119 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
120 IsEqualGUID( &IID_IDirectInputW, riid ) ||
121 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
122 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
123 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
124 IsEqualGUID( &IID_IDirectInput8W, riid ))
126 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
127 return DIERR_OUTOFMEMORY;
129 else
130 return DIERR_OLDDIRECTINPUTVERSION;
132 This->lpVtbl = &ddi7avt;
133 This->lpVtbl7w = &ddi7wvt;
134 This->lpVtbl8a = &ddi8avt;
135 This->lpVtbl8w = &ddi8wvt;
136 This->ref = 0;
137 This->dwVersion = dwVersion;
138 This->evsequence = 1;
140 InitializeCriticalSection(&This->crit);
141 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
143 list_init( &This->devices_list );
145 /* Add self to the list of the IDirectInputs */
146 EnterCriticalSection( &dinput_hook_crit );
147 list_add_head( &direct_input_list, &This->entry );
148 LeaveCriticalSection( &dinput_hook_crit );
150 if (!check_hook_thread())
152 IUnknown_Release( (LPDIRECTINPUT7A)This );
153 return DIERR_GENERIC;
156 IDirectInput_QueryInterface( (IDirectInput7A *)This, riid, ppDI );
157 return DI_OK;
160 /******************************************************************************
161 * DirectInputCreateA (DINPUT.@)
163 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
165 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
168 /******************************************************************************
169 * DirectInputCreateW (DINPUT.@)
171 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
173 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
176 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
177 switch (dwDevType) {
178 case 0: return "All devices";
179 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
180 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
181 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
182 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
183 default: return "Unknown";
187 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
188 if (TRACE_ON(dinput)) {
189 unsigned int i;
190 static const struct {
191 DWORD mask;
192 const char *name;
193 } flags[] = {
194 #define FE(x) { x, #x}
195 FE(DIEDFL_ALLDEVICES),
196 FE(DIEDFL_ATTACHEDONLY),
197 FE(DIEDFL_FORCEFEEDBACK),
198 FE(DIEDFL_INCLUDEALIASES),
199 FE(DIEDFL_INCLUDEPHANTOMS)
200 #undef FE
202 TRACE(" flags: ");
203 if (dwFlags == 0) {
204 TRACE("DIEDFL_ALLDEVICES");
205 return;
207 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
208 if (flags[i].mask & dwFlags)
209 TRACE("%s ",flags[i].name);
211 TRACE("\n");
214 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
215 int i;
217 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
218 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
219 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
220 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
221 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
222 for (i=0;i<lpdiActionFormat->dwNumActions;i++) {
223 FIXME("diaf.rgoAction[%d]:\n", i);
224 FIXME("\tuAppData=%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
225 FIXME("\tdwSemantics=%x\n", lpdiActionFormat->rgoAction[i].dwSemantics);
226 FIXME("\tdwFlags=%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
227 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
228 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
229 FIXME("\tdwObjID=%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
230 FIXME("\tdwHow=%x\n", lpdiActionFormat->rgoAction[i].dwHow);
232 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
233 FIXME("diaf.dwGenre = %d\n", lpdiActionFormat->dwGenre);
234 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
235 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
236 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
237 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
238 FIXME("diaf.ftTimeStamp ...\n");
239 FIXME("diaf.dwCRC = %x\n", lpdiActionFormat->dwCRC);
240 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
243 /******************************************************************************
244 * IDirectInputA_EnumDevices
246 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
247 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
248 LPVOID pvRef, DWORD dwFlags)
250 IDirectInputImpl *This = (IDirectInputImpl *)iface;
251 DIDEVICEINSTANCEA devInstance;
252 int i, j, r;
254 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
255 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
256 lpCallback, pvRef, dwFlags);
257 _dump_EnumDevices_dwFlags(dwFlags);
259 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
260 if (!dinput_devices[i]->enum_deviceA) continue;
261 for (j = 0, r = -1; r != 0; j++) {
262 devInstance.dwSize = sizeof(devInstance);
263 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
264 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
265 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
266 return 0;
271 return 0;
273 /******************************************************************************
274 * IDirectInputW_EnumDevices
276 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
277 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
278 LPVOID pvRef, DWORD dwFlags)
280 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
281 DIDEVICEINSTANCEW devInstance;
282 int i, j, r;
284 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
285 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
286 lpCallback, pvRef, dwFlags);
287 _dump_EnumDevices_dwFlags(dwFlags);
289 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
290 if (!dinput_devices[i]->enum_deviceW) continue;
291 for (j = 0, r = -1; r != 0; j++) {
292 devInstance.dwSize = sizeof(devInstance);
293 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
294 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
295 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
296 return 0;
301 return 0;
304 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
306 IDirectInputImpl *This = (IDirectInputImpl *)iface;
307 ULONG ref = InterlockedIncrement(&This->ref);
309 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
310 return ref;
313 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
315 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
316 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
319 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
321 IDirectInputImpl *This = (IDirectInputImpl *)iface;
322 ULONG ref = InterlockedDecrement( &This->ref );
324 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
326 if (ref) return ref;
328 /* Remove self from the list of the IDirectInputs */
329 EnterCriticalSection( &dinput_hook_crit );
330 list_remove( &This->entry );
331 LeaveCriticalSection( &dinput_hook_crit );
333 check_hook_thread();
335 This->crit.DebugInfo->Spare[0] = 0;
336 DeleteCriticalSection( &This->crit );
337 HeapFree( GetProcessHeap(), 0, This );
339 return 0;
342 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
344 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
345 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
348 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
350 IDirectInputImpl *This = (IDirectInputImpl *)iface;
352 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
354 if (IsEqualGUID( &IID_IUnknown, riid ) ||
355 IsEqualGUID( &IID_IDirectInputA, riid ) ||
356 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
357 IsEqualGUID( &IID_IDirectInput7A, riid ))
359 *ppobj = &This->lpVtbl;
360 IUnknown_AddRef( (IUnknown*)*ppobj );
362 return DI_OK;
365 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
366 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
367 IsEqualGUID( &IID_IDirectInput7W, riid ))
369 *ppobj = &This->lpVtbl7w;
370 IUnknown_AddRef( (IUnknown*)*ppobj );
372 return DI_OK;
375 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
377 *ppobj = &This->lpVtbl8a;
378 IUnknown_AddRef( (IUnknown*)*ppobj );
380 return DI_OK;
383 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
385 *ppobj = &This->lpVtbl8w;
386 IUnknown_AddRef( (IUnknown*)*ppobj );
388 return DI_OK;
391 FIXME( "Unsupported interface !\n" );
392 return E_FAIL;
395 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
397 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
398 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
401 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
402 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
404 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
405 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
406 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
408 return DI_OK;
411 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
413 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
414 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
417 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
419 IDirectInputImpl *This = (IDirectInputImpl *)iface;
421 FIXME( "(%p)->(%s): stub\n", This, debugstr_guid(rguid) );
423 return DI_OK;
426 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
428 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
429 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
432 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
433 HWND hwndOwner,
434 DWORD dwFlags)
436 IDirectInputImpl *This = (IDirectInputImpl *)iface;
438 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
440 return DI_OK;
443 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
445 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
446 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
449 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
450 LPCSTR pszName, LPGUID pguidInstance)
452 IDirectInputImpl *This = (IDirectInputImpl *)iface;
454 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
456 return DI_OK;
459 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
460 LPCWSTR pszName, LPGUID pguidInstance)
462 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
464 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
466 return DI_OK;
469 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
470 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
472 IDirectInputImpl *This = (IDirectInputImpl *)iface;
473 HRESULT ret_value = DIERR_DEVICENOTREG;
474 int i;
476 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
478 if (!rguid || !pvOut) return E_POINTER;
480 /* Loop on all the devices to see if anyone matches the given GUID */
481 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
482 HRESULT ret;
484 if (!dinput_devices[i]->create_deviceA) continue;
485 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
487 EnterCriticalSection( &This->crit );
488 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
489 LeaveCriticalSection( &This->crit );
490 return DI_OK;
493 if (ret == DIERR_NOINTERFACE)
494 ret_value = DIERR_NOINTERFACE;
497 if (ret_value == DIERR_NOINTERFACE)
499 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
502 return ret_value;
505 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
506 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
508 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
509 HRESULT ret_value = DIERR_DEVICENOTREG;
510 int i;
512 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
514 if (!rguid || !pvOut) return E_POINTER;
516 /* Loop on all the devices to see if anyone matches the given GUID */
517 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
518 HRESULT ret;
520 if (!dinput_devices[i]->create_deviceW) continue;
521 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
523 EnterCriticalSection( &This->crit );
524 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
525 LeaveCriticalSection( &This->crit );
526 return DI_OK;
529 if (ret == DIERR_NOINTERFACE)
530 ret_value = DIERR_NOINTERFACE;
533 return ret_value;
536 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
537 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
539 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
542 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
543 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
545 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
548 /*******************************************************************************
549 * DirectInput8
552 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
554 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
555 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
558 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
560 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
561 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
564 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
566 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
567 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
570 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
572 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
573 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
576 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
578 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
579 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
582 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
584 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
585 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
588 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
589 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
591 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
592 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
595 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
596 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
598 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
599 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
602 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
603 LPVOID pvRef, DWORD dwFlags)
605 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
606 return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
609 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
610 LPVOID pvRef, DWORD dwFlags)
612 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
613 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
616 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
618 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
619 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
622 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
624 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
625 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
628 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
630 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
631 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
634 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
636 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
637 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
640 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
642 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
643 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
646 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
648 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
649 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
652 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
654 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
655 return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
658 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
660 IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
661 return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
664 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
665 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
666 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
667 LPVOID pvRef, DWORD dwFlags
670 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
672 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
673 lpCallback, pvRef, dwFlags);
674 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
675 X(DIEDBSFL_ATTACHEDONLY)
676 X(DIEDBSFL_THISUSER)
677 X(DIEDBSFL_FORCEFEEDBACK)
678 X(DIEDBSFL_AVAILABLEDEVICES)
679 X(DIEDBSFL_MULTIMICEKEYBOARDS)
680 X(DIEDBSFL_NONGAMINGDEVICES)
681 #undef X
683 _dump_diactionformatA(lpdiActionFormat);
685 return DI_OK;
688 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
689 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
690 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
691 LPVOID pvRef, DWORD dwFlags
694 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
696 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
697 lpCallback, pvRef, dwFlags);
698 return 0;
701 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
702 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
703 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
706 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
708 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
709 dwFlags, pvRefData);
710 return 0;
713 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
714 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
715 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
718 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
720 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
721 dwFlags, pvRefData);
722 return 0;
725 static const IDirectInput7AVtbl ddi7avt = {
726 IDirectInputAImpl_QueryInterface,
727 IDirectInputAImpl_AddRef,
728 IDirectInputAImpl_Release,
729 IDirectInputAImpl_CreateDevice,
730 IDirectInputAImpl_EnumDevices,
731 IDirectInputAImpl_GetDeviceStatus,
732 IDirectInputAImpl_RunControlPanel,
733 IDirectInputAImpl_Initialize,
734 IDirectInput2AImpl_FindDevice,
735 IDirectInput7AImpl_CreateDeviceEx
738 static const IDirectInput7WVtbl ddi7wvt = {
739 IDirectInputWImpl_QueryInterface,
740 IDirectInputWImpl_AddRef,
741 IDirectInputWImpl_Release,
742 IDirectInputWImpl_CreateDevice,
743 IDirectInputWImpl_EnumDevices,
744 IDirectInputWImpl_GetDeviceStatus,
745 IDirectInputWImpl_RunControlPanel,
746 IDirectInputWImpl_Initialize,
747 IDirectInput2WImpl_FindDevice,
748 IDirectInput7WImpl_CreateDeviceEx
751 static const IDirectInput8AVtbl ddi8avt = {
752 IDirectInput8AImpl_QueryInterface,
753 IDirectInput8AImpl_AddRef,
754 IDirectInput8AImpl_Release,
755 IDirectInput8AImpl_CreateDevice,
756 IDirectInput8AImpl_EnumDevices,
757 IDirectInput8AImpl_GetDeviceStatus,
758 IDirectInput8AImpl_RunControlPanel,
759 IDirectInput8AImpl_Initialize,
760 IDirectInput8AImpl_FindDevice,
761 IDirectInput8AImpl_EnumDevicesBySemantics,
762 IDirectInput8AImpl_ConfigureDevices
765 static const IDirectInput8WVtbl ddi8wvt = {
766 IDirectInput8WImpl_QueryInterface,
767 IDirectInput8WImpl_AddRef,
768 IDirectInput8WImpl_Release,
769 IDirectInput8WImpl_CreateDevice,
770 IDirectInput8WImpl_EnumDevices,
771 IDirectInput8WImpl_GetDeviceStatus,
772 IDirectInput8WImpl_RunControlPanel,
773 IDirectInput8WImpl_Initialize,
774 IDirectInput8WImpl_FindDevice,
775 IDirectInput8WImpl_EnumDevicesBySemantics,
776 IDirectInput8WImpl_ConfigureDevices
779 /*******************************************************************************
780 * DirectInput ClassFactory
782 typedef struct
784 /* IUnknown fields */
785 const IClassFactoryVtbl *lpVtbl;
786 LONG ref;
787 } IClassFactoryImpl;
789 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
790 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
792 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
793 return E_NOINTERFACE;
796 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
797 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
798 return InterlockedIncrement(&(This->ref));
801 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
802 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
803 /* static class, won't be freed */
804 return InterlockedDecrement(&(This->ref));
807 static HRESULT WINAPI DICF_CreateInstance(
808 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
810 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
812 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
813 if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
814 IsEqualGUID( &IID_IDirectInputW, riid ) ||
815 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
816 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
817 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
818 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
819 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
820 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
821 /* FIXME: reuse already created dinput if present? */
822 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
825 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
826 return E_NOINTERFACE;
829 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
830 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
831 FIXME("(%p)->(%d),stub!\n",This,dolock);
832 return S_OK;
835 static const IClassFactoryVtbl DICF_Vtbl = {
836 DICF_QueryInterface,
837 DICF_AddRef,
838 DICF_Release,
839 DICF_CreateInstance,
840 DICF_LockServer
842 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
844 /***********************************************************************
845 * DllCanUnloadNow (DINPUT.@)
847 HRESULT WINAPI DllCanUnloadNow(void)
849 FIXME("(void): stub\n");
851 return S_FALSE;
854 /***********************************************************************
855 * DllGetClassObject (DINPUT.@)
857 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
859 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
860 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
861 *ppv = (LPVOID)&DINPUT_CF;
862 IClassFactory_AddRef((IClassFactory*)*ppv);
863 return S_OK;
866 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
867 return CLASS_E_CLASSNOTAVAILABLE;
870 /******************************************************************************
871 * DInput hook thread
874 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
876 IDirectInputImpl *dinput;
878 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
880 EnterCriticalSection( &dinput_hook_crit );
881 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
883 IDirectInputDevice2AImpl *dev;
885 EnterCriticalSection( &dinput->crit );
886 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
887 if (dev->acquired && dev->event_proc)
889 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
890 dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
892 LeaveCriticalSection( &dinput->crit );
894 LeaveCriticalSection( &dinput_hook_crit );
896 return CallNextHookEx( 0, code, wparam, lparam );
899 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
901 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
902 IDirectInputImpl *dinput;
903 HWND foreground;
905 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
906 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
907 return CallNextHookEx( 0, code, wparam, lparam );
909 foreground = GetForegroundWindow();
911 EnterCriticalSection( &dinput_hook_crit );
913 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
915 IDirectInputDevice2AImpl *dev;
917 EnterCriticalSection( &dinput->crit );
918 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
920 if (!dev->acquired) continue;
922 if (msg->hwnd == dev->win && msg->hwnd != foreground)
924 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
925 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
928 LeaveCriticalSection( &dinput->crit );
930 LeaveCriticalSection( &dinput_hook_crit );
932 return CallNextHookEx( 0, code, wparam, lparam );
935 static DWORD WINAPI hook_thread_proc(void *param)
937 static HHOOK kbd_hook, mouse_hook;
938 MSG msg;
940 /* Force creation of the message queue */
941 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
942 SetEvent(*(LPHANDLE)param);
944 while (GetMessageW( &msg, 0, 0, 0 ))
946 UINT kbd_cnt = 0, mice_cnt = 0;
948 if (msg.message == WM_USER+0x10)
950 IDirectInputImpl *dinput;
952 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
954 if (!msg.wParam && !msg.lParam)
956 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
957 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
958 kbd_hook = mouse_hook = NULL;
959 break;
962 EnterCriticalSection( &dinput_hook_crit );
964 /* Count acquired keyboards and mice*/
965 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
967 IDirectInputDevice2AImpl *dev;
969 EnterCriticalSection( &dinput->crit );
970 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
972 if (!dev->acquired || !dev->event_proc) continue;
974 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
975 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
976 kbd_cnt++;
977 else
978 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
979 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
980 mice_cnt++;
982 LeaveCriticalSection( &dinput->crit );
984 LeaveCriticalSection( &dinput_hook_crit );
986 if (kbd_cnt && !kbd_hook)
987 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
988 else if (!kbd_cnt && kbd_hook)
990 UnhookWindowsHookEx( kbd_hook );
991 kbd_hook = NULL;
994 if (mice_cnt && !mouse_hook)
995 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
996 else if (!mice_cnt && mouse_hook)
998 UnhookWindowsHookEx( mouse_hook );
999 mouse_hook = NULL;
1002 TranslateMessage(&msg);
1003 DispatchMessageW(&msg);
1006 return 0;
1009 static DWORD hook_thread_id;
1011 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1013 0, 0, &dinput_hook_crit,
1014 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1015 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1017 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1019 static BOOL check_hook_thread(void)
1021 static HANDLE hook_thread;
1023 EnterCriticalSection(&dinput_hook_crit);
1025 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1026 if (!list_empty(&direct_input_list) && !hook_thread)
1028 HANDLE event;
1030 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1031 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1032 if (event && hook_thread)
1034 HANDLE handles[2];
1035 handles[0] = event;
1036 handles[1] = hook_thread;
1037 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1039 LeaveCriticalSection(&dinput_hook_crit);
1040 CloseHandle(event);
1042 else if (list_empty(&direct_input_list) && hook_thread)
1044 DWORD tid = hook_thread_id;
1046 hook_thread_id = 0;
1047 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1048 LeaveCriticalSection(&dinput_hook_crit);
1050 /* wait for hook thread to exit */
1051 WaitForSingleObject(hook_thread, INFINITE);
1052 CloseHandle(hook_thread);
1053 hook_thread = NULL;
1055 else
1056 LeaveCriticalSection(&dinput_hook_crit);
1058 return hook_thread_id != 0;
1061 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1063 static HHOOK callwndproc_hook;
1064 static ULONG foreground_cnt;
1065 IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1067 EnterCriticalSection(&dinput_hook_crit);
1069 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1071 if (dev->acquired)
1072 foreground_cnt++;
1073 else
1074 foreground_cnt--;
1077 if (foreground_cnt && !callwndproc_hook)
1078 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1079 DINPUT_instance, GetCurrentThreadId() );
1080 else if (!foreground_cnt && callwndproc_hook)
1082 UnhookWindowsHookEx( callwndproc_hook );
1083 callwndproc_hook = NULL;
1086 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1088 LeaveCriticalSection(&dinput_hook_crit);