Merge branch 'master' of git+ssh://repo.or.cz/srv/git/wine/winequartzdrv
[wine/winequartzdrv.git] / dlls / dinput / dinput_main.c
blob2e4b4f5717caacd0f89789171a02898def70b79b
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
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winuser.h"
45 #include "winerror.h"
46 #include "dinput_private.h"
47 #include "device_private.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
51 static const IDirectInput7AVtbl ddi7avt;
52 static const IDirectInput7WVtbl ddi7wvt;
53 static const IDirectInput8AVtbl ddi8avt;
54 static const IDirectInput8WVtbl ddi8wvt;
56 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
58 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl7w );
61 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
63 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8a );
66 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
68 return CONTAINING_RECORD( iface, IDirectInputImpl, lpVtbl8w );
71 static inline IDirectInput7W *IDirectInput7W_from_impl( IDirectInputImpl *iface )
73 return (IDirectInput7W *)(&iface->lpVtbl7w);
76 static const struct dinput_device *dinput_devices[] =
78 &mouse_device,
79 &keyboard_device,
80 &joystick_linuxinput_device,
81 &joystick_linux_device
83 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
85 static HINSTANCE DINPUT_instance = NULL;
87 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
89 switch(reason)
91 case DLL_PROCESS_ATTACH:
92 DisableThreadLibraryCalls(inst);
93 DINPUT_instance = inst;
94 break;
95 case DLL_PROCESS_DETACH:
96 break;
98 return TRUE;
101 static BOOL check_hook_thread(void);
102 static CRITICAL_SECTION dinput_hook_crit;
103 static struct list direct_input_list = LIST_INIT( direct_input_list );
105 /******************************************************************************
106 * DirectInputCreateEx (DINPUT.@)
108 HRESULT WINAPI DirectInputCreateEx(
109 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
110 LPUNKNOWN punkOuter)
112 IDirectInputImpl* This;
114 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
116 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
117 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
118 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
119 IsEqualGUID( &IID_IDirectInputW, riid ) ||
120 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
121 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
122 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
123 IsEqualGUID( &IID_IDirectInput8W, riid ))
125 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
126 return DIERR_OUTOFMEMORY;
128 else
129 return DIERR_OLDDIRECTINPUTVERSION;
131 This->lpVtbl = &ddi7avt;
132 This->lpVtbl7w = &ddi7wvt;
133 This->lpVtbl8a = &ddi8avt;
134 This->lpVtbl8w = &ddi8wvt;
135 This->ref = 0;
136 This->dwVersion = dwVersion;
137 This->evsequence = 1;
139 InitializeCriticalSection(&This->crit);
140 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
142 list_init( &This->devices_list );
144 /* Add self to the list of the IDirectInputs */
145 EnterCriticalSection( &dinput_hook_crit );
146 list_add_head( &direct_input_list, &This->entry );
147 LeaveCriticalSection( &dinput_hook_crit );
149 if (!check_hook_thread())
151 IUnknown_Release( (LPDIRECTINPUT7A)This );
152 return DIERR_GENERIC;
155 IDirectInput_QueryInterface( (IDirectInput7A *)This, riid, ppDI );
156 return DI_OK;
159 /******************************************************************************
160 * DirectInputCreateA (DINPUT.@)
162 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
164 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
167 /******************************************************************************
168 * DirectInputCreateW (DINPUT.@)
170 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
172 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
175 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
176 switch (dwDevType) {
177 case 0: return "All devices";
178 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
179 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
180 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
181 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
182 default: return "Unknown";
186 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
187 if (TRACE_ON(dinput)) {
188 unsigned int i;
189 static const struct {
190 DWORD mask;
191 const char *name;
192 } flags[] = {
193 #define FE(x) { x, #x}
194 FE(DIEDFL_ALLDEVICES),
195 FE(DIEDFL_ATTACHEDONLY),
196 FE(DIEDFL_FORCEFEEDBACK),
197 FE(DIEDFL_INCLUDEALIASES),
198 FE(DIEDFL_INCLUDEPHANTOMS)
199 #undef FE
201 TRACE(" flags: ");
202 if (dwFlags == 0) {
203 TRACE("DIEDFL_ALLDEVICES");
204 return;
206 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
207 if (flags[i].mask & dwFlags)
208 TRACE("%s ",flags[i].name);
210 TRACE("\n");
213 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
214 int i;
216 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
217 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
218 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
219 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
220 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
221 for (i=0;i<lpdiActionFormat->dwNumActions;i++) {
222 FIXME("diaf.rgoAction[%d]:\n", i);
223 FIXME("\tuAppData=%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
224 FIXME("\tdwSemantics=%x\n", lpdiActionFormat->rgoAction[i].dwSemantics);
225 FIXME("\tdwFlags=%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
226 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].lptszActionName));
227 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
228 FIXME("\tdwObjID=%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
229 FIXME("\tdwHow=%x\n", lpdiActionFormat->rgoAction[i].dwHow);
231 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
232 FIXME("diaf.dwGenre = %d\n", lpdiActionFormat->dwGenre);
233 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
234 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
235 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
236 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
237 FIXME("diaf.ftTimeStamp ...\n");
238 FIXME("diaf.dwCRC = %x\n", lpdiActionFormat->dwCRC);
239 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
242 /******************************************************************************
243 * IDirectInputA_EnumDevices
245 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
246 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
247 LPVOID pvRef, DWORD dwFlags)
249 IDirectInputImpl *This = (IDirectInputImpl *)iface;
250 DIDEVICEINSTANCEA devInstance;
251 int i, j, r;
253 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
254 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
255 lpCallback, pvRef, dwFlags);
256 _dump_EnumDevices_dwFlags(dwFlags);
258 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
259 if (!dinput_devices[i]->enum_deviceA) continue;
260 for (j = 0, r = -1; r != 0; j++) {
261 devInstance.dwSize = sizeof(devInstance);
262 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
263 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
264 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
265 return 0;
270 return 0;
272 /******************************************************************************
273 * IDirectInputW_EnumDevices
275 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
276 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
277 LPVOID pvRef, DWORD dwFlags)
279 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
280 DIDEVICEINSTANCEW devInstance;
281 int i, j, r;
283 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
284 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
285 lpCallback, pvRef, dwFlags);
286 _dump_EnumDevices_dwFlags(dwFlags);
288 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
289 if (!dinput_devices[i]->enum_deviceW) continue;
290 for (j = 0, r = -1; r != 0; j++) {
291 devInstance.dwSize = sizeof(devInstance);
292 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
293 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
294 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
295 return 0;
300 return 0;
303 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
305 IDirectInputImpl *This = (IDirectInputImpl *)iface;
306 ULONG ref = InterlockedIncrement(&This->ref);
308 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
309 return ref;
312 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
314 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
315 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
318 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
320 IDirectInputImpl *This = (IDirectInputImpl *)iface;
321 ULONG ref = InterlockedDecrement( &This->ref );
323 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
325 if (ref) return ref;
327 /* Remove self from the list of the IDirectInputs */
328 EnterCriticalSection( &dinput_hook_crit );
329 list_remove( &This->entry );
330 LeaveCriticalSection( &dinput_hook_crit );
332 check_hook_thread();
334 This->crit.DebugInfo->Spare[0] = 0;
335 DeleteCriticalSection( &This->crit );
336 HeapFree( GetProcessHeap(), 0, This );
338 return 0;
341 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
343 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
344 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
347 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
349 IDirectInputImpl *This = (IDirectInputImpl *)iface;
351 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
353 if (IsEqualGUID( &IID_IUnknown, riid ) ||
354 IsEqualGUID( &IID_IDirectInputA, riid ) ||
355 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
356 IsEqualGUID( &IID_IDirectInput7A, riid ))
358 *ppobj = &This->lpVtbl;
359 IUnknown_AddRef( (IUnknown*)*ppobj );
361 return DI_OK;
364 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
365 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
366 IsEqualGUID( &IID_IDirectInput7W, riid ))
368 *ppobj = &This->lpVtbl7w;
369 IUnknown_AddRef( (IUnknown*)*ppobj );
371 return DI_OK;
374 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
376 *ppobj = &This->lpVtbl8a;
377 IUnknown_AddRef( (IUnknown*)*ppobj );
379 return DI_OK;
382 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
384 *ppobj = &This->lpVtbl8w;
385 IUnknown_AddRef( (IUnknown*)*ppobj );
387 return DI_OK;
390 FIXME( "Unsupported interface !\n" );
391 return E_FAIL;
394 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
396 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
397 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
400 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
401 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
403 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
404 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
405 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
407 return DI_OK;
410 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
412 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
413 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
416 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
418 IDirectInputImpl *This = (IDirectInputImpl *)iface;
420 FIXME( "(%p)->(%s): stub\n", This, debugstr_guid(rguid) );
422 return DI_OK;
425 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
427 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
428 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
431 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
432 HWND hwndOwner,
433 DWORD dwFlags)
435 IDirectInputImpl *This = (IDirectInputImpl *)iface;
437 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
439 return DI_OK;
442 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
444 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
445 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
448 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
449 LPCSTR pszName, LPGUID pguidInstance)
451 IDirectInputImpl *This = (IDirectInputImpl *)iface;
453 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
455 return DI_OK;
458 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
459 LPCWSTR pszName, LPGUID pguidInstance)
461 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
463 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
465 return DI_OK;
468 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
469 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
471 IDirectInputImpl *This = (IDirectInputImpl *)iface;
472 HRESULT ret_value = DIERR_DEVICENOTREG;
473 int i;
475 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
477 if (!rguid || !pvOut) return E_POINTER;
479 /* Loop on all the devices to see if anyone matches the given GUID */
480 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
481 HRESULT ret;
483 if (!dinput_devices[i]->create_deviceA) continue;
484 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
486 EnterCriticalSection( &This->crit );
487 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
488 LeaveCriticalSection( &This->crit );
489 return DI_OK;
492 if (ret == DIERR_NOINTERFACE)
493 ret_value = DIERR_NOINTERFACE;
496 if (ret_value == DIERR_NOINTERFACE)
498 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
501 return ret_value;
504 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
505 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
507 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
508 HRESULT ret_value = DIERR_DEVICENOTREG;
509 int i;
511 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
513 if (!rguid || !pvOut) return E_POINTER;
515 /* Loop on all the devices to see if anyone matches the given GUID */
516 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
517 HRESULT ret;
519 if (!dinput_devices[i]->create_deviceW) continue;
520 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
522 EnterCriticalSection( &This->crit );
523 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
524 LeaveCriticalSection( &This->crit );
525 return DI_OK;
528 if (ret == DIERR_NOINTERFACE)
529 ret_value = DIERR_NOINTERFACE;
532 return ret_value;
535 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
536 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
538 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
541 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
542 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
544 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
547 /*******************************************************************************
548 * DirectInput8
551 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
553 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
554 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
557 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
559 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
560 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
563 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
565 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
566 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
569 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
571 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
572 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
575 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
577 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
578 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
581 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
583 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
584 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
587 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
588 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
590 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
591 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
594 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
595 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
597 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
598 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
601 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
602 LPVOID pvRef, DWORD dwFlags)
604 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
605 return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
608 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
609 LPVOID pvRef, DWORD dwFlags)
611 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
612 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
615 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
617 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
618 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
621 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
623 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
624 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
627 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
629 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
630 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
633 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
635 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
636 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
639 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
641 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
642 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
645 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
647 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
648 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
651 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
653 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
654 return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
657 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
659 IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
660 return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
663 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
664 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
665 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
666 LPVOID pvRef, DWORD dwFlags
669 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
671 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
672 lpCallback, pvRef, dwFlags);
673 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
674 X(DIEDBSFL_ATTACHEDONLY)
675 X(DIEDBSFL_THISUSER)
676 X(DIEDBSFL_FORCEFEEDBACK)
677 X(DIEDBSFL_AVAILABLEDEVICES)
678 X(DIEDBSFL_MULTIMICEKEYBOARDS)
679 X(DIEDBSFL_NONGAMINGDEVICES)
680 #undef X
682 _dump_diactionformatA(lpdiActionFormat);
684 return DI_OK;
687 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
688 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
689 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
690 LPVOID pvRef, DWORD dwFlags
693 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
695 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
696 lpCallback, pvRef, dwFlags);
697 return 0;
700 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
701 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
702 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
705 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
707 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
708 dwFlags, pvRefData);
709 return 0;
712 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
713 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
714 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
717 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
719 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
720 dwFlags, pvRefData);
721 return 0;
724 static const IDirectInput7AVtbl ddi7avt = {
725 IDirectInputAImpl_QueryInterface,
726 IDirectInputAImpl_AddRef,
727 IDirectInputAImpl_Release,
728 IDirectInputAImpl_CreateDevice,
729 IDirectInputAImpl_EnumDevices,
730 IDirectInputAImpl_GetDeviceStatus,
731 IDirectInputAImpl_RunControlPanel,
732 IDirectInputAImpl_Initialize,
733 IDirectInput2AImpl_FindDevice,
734 IDirectInput7AImpl_CreateDeviceEx
737 static const IDirectInput7WVtbl ddi7wvt = {
738 IDirectInputWImpl_QueryInterface,
739 IDirectInputWImpl_AddRef,
740 IDirectInputWImpl_Release,
741 IDirectInputWImpl_CreateDevice,
742 IDirectInputWImpl_EnumDevices,
743 IDirectInputWImpl_GetDeviceStatus,
744 IDirectInputWImpl_RunControlPanel,
745 IDirectInputWImpl_Initialize,
746 IDirectInput2WImpl_FindDevice,
747 IDirectInput7WImpl_CreateDeviceEx
750 static const IDirectInput8AVtbl ddi8avt = {
751 IDirectInput8AImpl_QueryInterface,
752 IDirectInput8AImpl_AddRef,
753 IDirectInput8AImpl_Release,
754 IDirectInput8AImpl_CreateDevice,
755 IDirectInput8AImpl_EnumDevices,
756 IDirectInput8AImpl_GetDeviceStatus,
757 IDirectInput8AImpl_RunControlPanel,
758 IDirectInput8AImpl_Initialize,
759 IDirectInput8AImpl_FindDevice,
760 IDirectInput8AImpl_EnumDevicesBySemantics,
761 IDirectInput8AImpl_ConfigureDevices
764 static const IDirectInput8WVtbl ddi8wvt = {
765 IDirectInput8WImpl_QueryInterface,
766 IDirectInput8WImpl_AddRef,
767 IDirectInput8WImpl_Release,
768 IDirectInput8WImpl_CreateDevice,
769 IDirectInput8WImpl_EnumDevices,
770 IDirectInput8WImpl_GetDeviceStatus,
771 IDirectInput8WImpl_RunControlPanel,
772 IDirectInput8WImpl_Initialize,
773 IDirectInput8WImpl_FindDevice,
774 IDirectInput8WImpl_EnumDevicesBySemantics,
775 IDirectInput8WImpl_ConfigureDevices
778 /*******************************************************************************
779 * DirectInput ClassFactory
781 typedef struct
783 /* IUnknown fields */
784 const IClassFactoryVtbl *lpVtbl;
785 LONG ref;
786 } IClassFactoryImpl;
788 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
789 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
791 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
792 return E_NOINTERFACE;
795 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
796 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
797 return InterlockedIncrement(&(This->ref));
800 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
801 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
802 /* static class, won't be freed */
803 return InterlockedDecrement(&(This->ref));
806 static HRESULT WINAPI DICF_CreateInstance(
807 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
809 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
811 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
812 if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
813 IsEqualGUID( &IID_IDirectInputW, riid ) ||
814 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
815 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
816 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
817 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
818 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
819 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
820 /* FIXME: reuse already created dinput if present? */
821 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
824 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
825 return E_NOINTERFACE;
828 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
829 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
830 FIXME("(%p)->(%d),stub!\n",This,dolock);
831 return S_OK;
834 static const IClassFactoryVtbl DICF_Vtbl = {
835 DICF_QueryInterface,
836 DICF_AddRef,
837 DICF_Release,
838 DICF_CreateInstance,
839 DICF_LockServer
841 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
843 /***********************************************************************
844 * DllCanUnloadNow (DINPUT.@)
846 HRESULT WINAPI DllCanUnloadNow(void)
848 FIXME("(void): stub\n");
850 return S_FALSE;
853 /***********************************************************************
854 * DllGetClassObject (DINPUT.@)
856 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
858 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
859 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
860 *ppv = (LPVOID)&DINPUT_CF;
861 IClassFactory_AddRef((IClassFactory*)*ppv);
862 return S_OK;
865 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
866 return CLASS_E_CLASSNOTAVAILABLE;
869 /******************************************************************************
870 * DInput hook thread
873 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
875 IDirectInputImpl *dinput;
877 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
879 EnterCriticalSection( &dinput_hook_crit );
880 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
882 IDirectInputDevice2AImpl *dev;
884 EnterCriticalSection( &dinput->crit );
885 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
886 if (dev->acquired && dev->event_proc)
888 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
889 dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
891 LeaveCriticalSection( &dinput->crit );
893 LeaveCriticalSection( &dinput_hook_crit );
895 return CallNextHookEx( 0, code, wparam, lparam );
898 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
900 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
901 IDirectInputImpl *dinput;
902 HWND foreground;
904 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
905 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
906 return CallNextHookEx( 0, code, wparam, lparam );
908 foreground = GetForegroundWindow();
910 EnterCriticalSection( &dinput_hook_crit );
912 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
914 IDirectInputDevice2AImpl *dev;
916 EnterCriticalSection( &dinput->crit );
917 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
919 if (!dev->acquired) continue;
921 if (msg->hwnd == dev->win && msg->hwnd != foreground)
923 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
924 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
927 LeaveCriticalSection( &dinput->crit );
929 LeaveCriticalSection( &dinput_hook_crit );
931 return CallNextHookEx( 0, code, wparam, lparam );
934 static DWORD WINAPI hook_thread_proc(void *param)
936 static HHOOK kbd_hook, mouse_hook;
937 MSG msg;
939 /* Force creation of the message queue */
940 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
941 SetEvent(*(LPHANDLE)param);
943 while (GetMessageW( &msg, 0, 0, 0 ))
945 UINT kbd_cnt = 0, mice_cnt = 0;
947 if (msg.message == WM_USER+0x10)
949 IDirectInputImpl *dinput;
951 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
953 if (!msg.wParam && !msg.lParam)
955 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
956 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
957 kbd_hook = mouse_hook = NULL;
958 break;
961 EnterCriticalSection( &dinput_hook_crit );
963 /* Count acquired keyboards and mice*/
964 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
966 IDirectInputDevice2AImpl *dev;
968 EnterCriticalSection( &dinput->crit );
969 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
971 if (!dev->acquired || !dev->event_proc) continue;
973 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
974 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
975 kbd_cnt++;
976 else
977 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
978 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
979 mice_cnt++;
981 LeaveCriticalSection( &dinput->crit );
983 LeaveCriticalSection( &dinput_hook_crit );
985 if (kbd_cnt && !kbd_hook)
986 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
987 else if (!kbd_cnt && kbd_hook)
989 UnhookWindowsHookEx( kbd_hook );
990 kbd_hook = NULL;
993 if (mice_cnt && !mouse_hook)
994 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
995 else if (!mice_cnt && mouse_hook)
997 UnhookWindowsHookEx( mouse_hook );
998 mouse_hook = NULL;
1001 TranslateMessage(&msg);
1002 DispatchMessageW(&msg);
1005 return 0;
1008 static DWORD hook_thread_id;
1010 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1012 0, 0, &dinput_hook_crit,
1013 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1014 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1016 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1018 static BOOL check_hook_thread(void)
1020 static HANDLE hook_thread;
1022 EnterCriticalSection(&dinput_hook_crit);
1024 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1025 if (!list_empty(&direct_input_list) && !hook_thread)
1027 HANDLE event;
1029 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1030 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1031 if (event && hook_thread)
1033 HANDLE handles[2];
1034 handles[0] = event;
1035 handles[1] = hook_thread;
1036 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1038 LeaveCriticalSection(&dinput_hook_crit);
1039 CloseHandle(event);
1041 else if (list_empty(&direct_input_list) && hook_thread)
1043 DWORD tid = hook_thread_id;
1045 hook_thread_id = 0;
1046 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1047 LeaveCriticalSection(&dinput_hook_crit);
1049 /* wait for hook thread to exit */
1050 WaitForSingleObject(hook_thread, INFINITE);
1051 CloseHandle(hook_thread);
1052 hook_thread = NULL;
1054 else
1055 LeaveCriticalSection(&dinput_hook_crit);
1057 return hook_thread_id != 0;
1060 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1062 static HHOOK callwndproc_hook;
1063 static ULONG foreground_cnt;
1064 IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1066 EnterCriticalSection(&dinput_hook_crit);
1068 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1070 if (dev->acquired)
1071 foreground_cnt++;
1072 else
1073 foreground_cnt--;
1076 if (foreground_cnt && !callwndproc_hook)
1077 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1078 DINPUT_instance, GetCurrentThreadId() );
1079 else if (!foreground_cnt && callwndproc_hook)
1081 UnhookWindowsHookEx( callwndproc_hook );
1082 callwndproc_hook = NULL;
1085 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1087 LeaveCriticalSection(&dinput_hook_crit);