push 16813bf814fcb5d03ddef80ecfd11fc0f25812b5
[wine/hacks.git] / dlls / dinput / dinput_main.c
blob6b52de4a87196e0814c70d7365954b23f5d6c41f
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_IUnknown, riid ) ||
118 IsEqualGUID( &IID_IDirectInputA, riid ) ||
119 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
120 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
121 IsEqualGUID( &IID_IDirectInputW, riid ) ||
122 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
123 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
124 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
125 IsEqualGUID( &IID_IDirectInput8W, riid ))
127 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
128 return DIERR_OUTOFMEMORY;
130 else
131 return DIERR_OLDDIRECTINPUTVERSION;
133 This->lpVtbl = &ddi7avt;
134 This->lpVtbl7w = &ddi7wvt;
135 This->lpVtbl8a = &ddi8avt;
136 This->lpVtbl8w = &ddi8wvt;
137 This->ref = 0;
138 This->dwVersion = dwVersion;
139 This->evsequence = 1;
141 InitializeCriticalSection(&This->crit);
142 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
144 list_init( &This->devices_list );
146 /* Add self to the list of the IDirectInputs */
147 EnterCriticalSection( &dinput_hook_crit );
148 list_add_head( &direct_input_list, &This->entry );
149 LeaveCriticalSection( &dinput_hook_crit );
151 if (!check_hook_thread())
153 IUnknown_Release( (LPDIRECTINPUT7A)This );
154 return DIERR_GENERIC;
157 IDirectInput_QueryInterface( (IDirectInput7A *)This, riid, ppDI );
158 return DI_OK;
161 /******************************************************************************
162 * DirectInputCreateA (DINPUT.@)
164 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
166 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
169 /******************************************************************************
170 * DirectInputCreateW (DINPUT.@)
172 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
174 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
177 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
178 switch (dwDevType) {
179 case 0: return "All devices";
180 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
181 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
182 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
183 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
184 default: return "Unknown";
188 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
189 if (TRACE_ON(dinput)) {
190 unsigned int i;
191 static const struct {
192 DWORD mask;
193 const char *name;
194 } flags[] = {
195 #define FE(x) { x, #x}
196 FE(DIEDFL_ALLDEVICES),
197 FE(DIEDFL_ATTACHEDONLY),
198 FE(DIEDFL_FORCEFEEDBACK),
199 FE(DIEDFL_INCLUDEALIASES),
200 FE(DIEDFL_INCLUDEPHANTOMS)
201 #undef FE
203 TRACE(" flags: ");
204 if (dwFlags == 0) {
205 TRACE("DIEDFL_ALLDEVICES");
206 return;
208 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
209 if (flags[i].mask & dwFlags)
210 TRACE("%s ",flags[i].name);
212 TRACE("\n");
215 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
216 int i;
218 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
219 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
220 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
221 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
222 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
223 for (i=0;i<lpdiActionFormat->dwNumActions;i++) {
224 FIXME("diaf.rgoAction[%d]:\n", i);
225 FIXME("\tuAppData=%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
226 FIXME("\tdwSemantics=%x\n", lpdiActionFormat->rgoAction[i].dwSemantics);
227 FIXME("\tdwFlags=%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
228 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
229 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
230 FIXME("\tdwObjID=%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
231 FIXME("\tdwHow=%x\n", lpdiActionFormat->rgoAction[i].dwHow);
233 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
234 FIXME("diaf.dwGenre = %d\n", lpdiActionFormat->dwGenre);
235 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
236 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
237 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
238 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
239 FIXME("diaf.ftTimeStamp ...\n");
240 FIXME("diaf.dwCRC = %x\n", lpdiActionFormat->dwCRC);
241 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
244 /******************************************************************************
245 * IDirectInputA_EnumDevices
247 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
248 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
249 LPVOID pvRef, DWORD dwFlags)
251 IDirectInputImpl *This = (IDirectInputImpl *)iface;
252 DIDEVICEINSTANCEA devInstance;
253 int i, j, r;
255 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
256 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
257 lpCallback, pvRef, dwFlags);
258 _dump_EnumDevices_dwFlags(dwFlags);
260 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
261 if (!dinput_devices[i]->enum_deviceA) continue;
262 for (j = 0, r = -1; r != 0; j++) {
263 devInstance.dwSize = sizeof(devInstance);
264 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
265 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
266 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
267 return 0;
272 return 0;
274 /******************************************************************************
275 * IDirectInputW_EnumDevices
277 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
278 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
279 LPVOID pvRef, DWORD dwFlags)
281 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
282 DIDEVICEINSTANCEW devInstance;
283 int i, j, r;
285 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
286 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
287 lpCallback, pvRef, dwFlags);
288 _dump_EnumDevices_dwFlags(dwFlags);
290 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
291 if (!dinput_devices[i]->enum_deviceW) continue;
292 for (j = 0, r = -1; r != 0; j++) {
293 devInstance.dwSize = sizeof(devInstance);
294 TRACE(" - checking device %d ('%s')\n", i, dinput_devices[i]->name);
295 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
296 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
297 return 0;
302 return 0;
305 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
307 IDirectInputImpl *This = (IDirectInputImpl *)iface;
308 ULONG ref = InterlockedIncrement(&This->ref);
310 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
311 return ref;
314 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
316 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
317 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
320 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
322 IDirectInputImpl *This = (IDirectInputImpl *)iface;
323 ULONG ref = InterlockedDecrement( &This->ref );
325 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
327 if (ref) return ref;
329 /* Remove self from the list of the IDirectInputs */
330 EnterCriticalSection( &dinput_hook_crit );
331 list_remove( &This->entry );
332 LeaveCriticalSection( &dinput_hook_crit );
334 check_hook_thread();
336 This->crit.DebugInfo->Spare[0] = 0;
337 DeleteCriticalSection( &This->crit );
338 HeapFree( GetProcessHeap(), 0, This );
340 return 0;
343 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
345 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
346 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
349 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
351 IDirectInputImpl *This = (IDirectInputImpl *)iface;
353 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
355 if (IsEqualGUID( &IID_IUnknown, riid ) ||
356 IsEqualGUID( &IID_IDirectInputA, riid ) ||
357 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
358 IsEqualGUID( &IID_IDirectInput7A, riid ))
360 *ppobj = &This->lpVtbl;
361 IUnknown_AddRef( (IUnknown*)*ppobj );
363 return DI_OK;
366 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
367 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
368 IsEqualGUID( &IID_IDirectInput7W, riid ))
370 *ppobj = &This->lpVtbl7w;
371 IUnknown_AddRef( (IUnknown*)*ppobj );
373 return DI_OK;
376 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
378 *ppobj = &This->lpVtbl8a;
379 IUnknown_AddRef( (IUnknown*)*ppobj );
381 return DI_OK;
384 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
386 *ppobj = &This->lpVtbl8w;
387 IUnknown_AddRef( (IUnknown*)*ppobj );
389 return DI_OK;
392 FIXME( "Unsupported interface !\n" );
393 return E_FAIL;
396 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
398 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
399 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
402 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
403 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
405 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
406 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
407 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
409 return DI_OK;
412 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
414 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
415 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
418 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
420 IDirectInputImpl *This = (IDirectInputImpl *)iface;
421 HRESULT hr;
422 LPDIRECTINPUTDEVICEA device;
424 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
426 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
427 if (hr != DI_OK) return DI_NOTATTACHED;
429 IUnknown_Release( device );
431 return DI_OK;
434 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
436 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
437 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
440 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
441 HWND hwndOwner,
442 DWORD dwFlags)
444 IDirectInputImpl *This = (IDirectInputImpl *)iface;
446 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
448 return DI_OK;
451 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
453 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
454 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
457 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
458 LPCSTR pszName, LPGUID pguidInstance)
460 IDirectInputImpl *This = (IDirectInputImpl *)iface;
462 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
464 return DI_OK;
467 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
468 LPCWSTR pszName, LPGUID pguidInstance)
470 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
472 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
474 return DI_OK;
477 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
478 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
480 IDirectInputImpl *This = (IDirectInputImpl *)iface;
481 HRESULT ret_value = DIERR_DEVICENOTREG;
482 int i;
484 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
486 if (!rguid || !pvOut) return E_POINTER;
488 /* Loop on all the devices to see if anyone matches the given GUID */
489 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
490 HRESULT ret;
492 if (!dinput_devices[i]->create_deviceA) continue;
493 if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
495 EnterCriticalSection( &This->crit );
496 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
497 LeaveCriticalSection( &This->crit );
498 return DI_OK;
501 if (ret == DIERR_NOINTERFACE)
502 ret_value = DIERR_NOINTERFACE;
505 if (ret_value == DIERR_NOINTERFACE)
507 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
510 return ret_value;
513 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
514 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
516 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
517 HRESULT ret_value = DIERR_DEVICENOTREG;
518 int i;
520 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
522 if (!rguid || !pvOut) return E_POINTER;
524 /* Loop on all the devices to see if anyone matches the given GUID */
525 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
526 HRESULT ret;
528 if (!dinput_devices[i]->create_deviceW) continue;
529 if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
531 EnterCriticalSection( &This->crit );
532 list_add_tail( &This->devices_list, &(*(IDirectInputDevice2AImpl**)pvOut)->entry );
533 LeaveCriticalSection( &This->crit );
534 return DI_OK;
537 if (ret == DIERR_NOINTERFACE)
538 ret_value = DIERR_NOINTERFACE;
541 return ret_value;
544 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
545 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
547 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
550 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
551 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
553 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
556 /*******************************************************************************
557 * DirectInput8
560 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
562 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
563 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
566 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
568 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
569 return IDirectInputAImpl_AddRef( (IDirectInput7A *)This );
572 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
574 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
575 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
578 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
580 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
581 return IDirectInputAImpl_QueryInterface( (IDirectInput7A *)This, riid, ppobj );
584 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
586 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
587 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
590 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
592 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
593 return IDirectInputAImpl_Release( (IDirectInput7A *)This );
596 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
597 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
599 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
600 return IDirectInput7AImpl_CreateDeviceEx( (IDirectInput7A *)This, rguid, NULL, (LPVOID*)pdev, punk );
603 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
604 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
606 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
607 return IDirectInput7WImpl_CreateDeviceEx( IDirectInput7W_from_impl( This ), rguid, NULL, (LPVOID*)pdev, punk );
610 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
611 LPVOID pvRef, DWORD dwFlags)
613 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
614 return IDirectInputAImpl_EnumDevices( (IDirectInput7A *)This, dwDevType, lpCallback, pvRef, dwFlags );
617 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
618 LPVOID pvRef, DWORD dwFlags)
620 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
621 return IDirectInputWImpl_EnumDevices( IDirectInput7W_from_impl( This ), dwDevType, lpCallback, pvRef, dwFlags );
624 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
626 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
627 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
630 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
632 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
633 return IDirectInputAImpl_GetDeviceStatus( (IDirectInput7A *)This, rguid );
636 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
638 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
639 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
642 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
644 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
645 return IDirectInputAImpl_RunControlPanel( (IDirectInput7A *)This, hwndOwner, dwFlags );
648 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
650 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
651 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
654 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
656 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
657 return IDirectInputAImpl_Initialize( (IDirectInput7A *)This, hinst, x );
660 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
662 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
663 return IDirectInput2AImpl_FindDevice( (IDirectInput7A *)This, rguid, pszName, pguidInstance );
666 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
668 IDirectInput7W *This = IDirectInput7W_from_impl( impl_from_IDirectInput8W( iface ) );
669 return IDirectInput2WImpl_FindDevice( This, rguid, pszName, pguidInstance );
672 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
673 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
674 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
675 LPVOID pvRef, DWORD dwFlags
678 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
680 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
681 lpCallback, pvRef, dwFlags);
682 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
683 X(DIEDBSFL_ATTACHEDONLY)
684 X(DIEDBSFL_THISUSER)
685 X(DIEDBSFL_FORCEFEEDBACK)
686 X(DIEDBSFL_AVAILABLEDEVICES)
687 X(DIEDBSFL_MULTIMICEKEYBOARDS)
688 X(DIEDBSFL_NONGAMINGDEVICES)
689 #undef X
691 _dump_diactionformatA(lpdiActionFormat);
693 return DI_OK;
696 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
697 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
698 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
699 LPVOID pvRef, DWORD dwFlags
702 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
704 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
705 lpCallback, pvRef, dwFlags);
706 return 0;
709 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
710 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
711 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
714 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
716 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
717 dwFlags, pvRefData);
718 return 0;
721 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
722 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
723 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
726 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
728 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
729 dwFlags, pvRefData);
730 return 0;
733 static const IDirectInput7AVtbl ddi7avt = {
734 IDirectInputAImpl_QueryInterface,
735 IDirectInputAImpl_AddRef,
736 IDirectInputAImpl_Release,
737 IDirectInputAImpl_CreateDevice,
738 IDirectInputAImpl_EnumDevices,
739 IDirectInputAImpl_GetDeviceStatus,
740 IDirectInputAImpl_RunControlPanel,
741 IDirectInputAImpl_Initialize,
742 IDirectInput2AImpl_FindDevice,
743 IDirectInput7AImpl_CreateDeviceEx
746 static const IDirectInput7WVtbl ddi7wvt = {
747 IDirectInputWImpl_QueryInterface,
748 IDirectInputWImpl_AddRef,
749 IDirectInputWImpl_Release,
750 IDirectInputWImpl_CreateDevice,
751 IDirectInputWImpl_EnumDevices,
752 IDirectInputWImpl_GetDeviceStatus,
753 IDirectInputWImpl_RunControlPanel,
754 IDirectInputWImpl_Initialize,
755 IDirectInput2WImpl_FindDevice,
756 IDirectInput7WImpl_CreateDeviceEx
759 static const IDirectInput8AVtbl ddi8avt = {
760 IDirectInput8AImpl_QueryInterface,
761 IDirectInput8AImpl_AddRef,
762 IDirectInput8AImpl_Release,
763 IDirectInput8AImpl_CreateDevice,
764 IDirectInput8AImpl_EnumDevices,
765 IDirectInput8AImpl_GetDeviceStatus,
766 IDirectInput8AImpl_RunControlPanel,
767 IDirectInput8AImpl_Initialize,
768 IDirectInput8AImpl_FindDevice,
769 IDirectInput8AImpl_EnumDevicesBySemantics,
770 IDirectInput8AImpl_ConfigureDevices
773 static const IDirectInput8WVtbl ddi8wvt = {
774 IDirectInput8WImpl_QueryInterface,
775 IDirectInput8WImpl_AddRef,
776 IDirectInput8WImpl_Release,
777 IDirectInput8WImpl_CreateDevice,
778 IDirectInput8WImpl_EnumDevices,
779 IDirectInput8WImpl_GetDeviceStatus,
780 IDirectInput8WImpl_RunControlPanel,
781 IDirectInput8WImpl_Initialize,
782 IDirectInput8WImpl_FindDevice,
783 IDirectInput8WImpl_EnumDevicesBySemantics,
784 IDirectInput8WImpl_ConfigureDevices
787 /*******************************************************************************
788 * DirectInput ClassFactory
790 typedef struct
792 /* IUnknown fields */
793 const IClassFactoryVtbl *lpVtbl;
794 LONG ref;
795 } IClassFactoryImpl;
797 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
798 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
800 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
801 return E_NOINTERFACE;
804 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
805 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
806 return InterlockedIncrement(&(This->ref));
809 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
810 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
811 /* static class, won't be freed */
812 return InterlockedDecrement(&(This->ref));
815 static HRESULT WINAPI DICF_CreateInstance(
816 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
818 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
820 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
821 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
822 IsEqualGUID( &IID_IDirectInputA, riid ) ||
823 IsEqualGUID( &IID_IDirectInputW, riid ) ||
824 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
825 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
826 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
827 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
828 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
829 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
830 /* FIXME: reuse already created dinput if present? */
831 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
834 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
835 return E_NOINTERFACE;
838 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
839 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
840 FIXME("(%p)->(%d),stub!\n",This,dolock);
841 return S_OK;
844 static const IClassFactoryVtbl DICF_Vtbl = {
845 DICF_QueryInterface,
846 DICF_AddRef,
847 DICF_Release,
848 DICF_CreateInstance,
849 DICF_LockServer
851 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
853 /***********************************************************************
854 * DllCanUnloadNow (DINPUT.@)
856 HRESULT WINAPI DllCanUnloadNow(void)
858 FIXME("(void): stub\n");
860 return S_FALSE;
863 /***********************************************************************
864 * DllGetClassObject (DINPUT.@)
866 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
868 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
869 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
870 *ppv = (LPVOID)&DINPUT_CF;
871 IClassFactory_AddRef((IClassFactory*)*ppv);
872 return S_OK;
875 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
876 return CLASS_E_CLASSNOTAVAILABLE;
879 /******************************************************************************
880 * DInput hook thread
883 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
885 IDirectInputImpl *dinput;
887 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
889 EnterCriticalSection( &dinput_hook_crit );
890 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
892 IDirectInputDevice2AImpl *dev;
894 EnterCriticalSection( &dinput->crit );
895 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
896 if (dev->acquired && dev->event_proc)
898 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
899 dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
901 LeaveCriticalSection( &dinput->crit );
903 LeaveCriticalSection( &dinput_hook_crit );
905 return CallNextHookEx( 0, code, wparam, lparam );
908 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
910 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
911 IDirectInputImpl *dinput;
912 HWND foreground;
914 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
915 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
916 return CallNextHookEx( 0, code, wparam, lparam );
918 foreground = GetForegroundWindow();
920 EnterCriticalSection( &dinput_hook_crit );
922 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
924 IDirectInputDevice2AImpl *dev;
926 EnterCriticalSection( &dinput->crit );
927 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
929 if (!dev->acquired) continue;
931 if (msg->hwnd == dev->win && msg->hwnd != foreground)
933 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
934 IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
937 LeaveCriticalSection( &dinput->crit );
939 LeaveCriticalSection( &dinput_hook_crit );
941 return CallNextHookEx( 0, code, wparam, lparam );
944 static DWORD WINAPI hook_thread_proc(void *param)
946 static HHOOK kbd_hook, mouse_hook;
947 MSG msg;
949 /* Force creation of the message queue */
950 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
951 SetEvent(*(LPHANDLE)param);
953 while (GetMessageW( &msg, 0, 0, 0 ))
955 UINT kbd_cnt = 0, mice_cnt = 0;
957 if (msg.message == WM_USER+0x10)
959 IDirectInputImpl *dinput;
961 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
963 if (!msg.wParam && !msg.lParam)
965 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
966 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
967 kbd_hook = mouse_hook = NULL;
968 break;
971 EnterCriticalSection( &dinput_hook_crit );
973 /* Count acquired keyboards and mice*/
974 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
976 IDirectInputDevice2AImpl *dev;
978 EnterCriticalSection( &dinput->crit );
979 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
981 if (!dev->acquired || !dev->event_proc) continue;
983 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
984 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
985 kbd_cnt++;
986 else
987 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
988 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
989 mice_cnt++;
991 LeaveCriticalSection( &dinput->crit );
993 LeaveCriticalSection( &dinput_hook_crit );
995 if (kbd_cnt && !kbd_hook)
996 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
997 else if (!kbd_cnt && kbd_hook)
999 UnhookWindowsHookEx( kbd_hook );
1000 kbd_hook = NULL;
1003 if (mice_cnt && !mouse_hook)
1004 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1005 else if (!mice_cnt && mouse_hook)
1007 UnhookWindowsHookEx( mouse_hook );
1008 mouse_hook = NULL;
1011 TranslateMessage(&msg);
1012 DispatchMessageW(&msg);
1015 return 0;
1018 static DWORD hook_thread_id;
1020 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1022 0, 0, &dinput_hook_crit,
1023 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1024 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1026 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1028 static BOOL check_hook_thread(void)
1030 static HANDLE hook_thread;
1032 EnterCriticalSection(&dinput_hook_crit);
1034 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1035 if (!list_empty(&direct_input_list) && !hook_thread)
1037 HANDLE event;
1039 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1040 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1041 if (event && hook_thread)
1043 HANDLE handles[2];
1044 handles[0] = event;
1045 handles[1] = hook_thread;
1046 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1048 LeaveCriticalSection(&dinput_hook_crit);
1049 CloseHandle(event);
1051 else if (list_empty(&direct_input_list) && hook_thread)
1053 DWORD tid = hook_thread_id;
1055 hook_thread_id = 0;
1056 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1057 LeaveCriticalSection(&dinput_hook_crit);
1059 /* wait for hook thread to exit */
1060 WaitForSingleObject(hook_thread, INFINITE);
1061 CloseHandle(hook_thread);
1062 hook_thread = NULL;
1064 else
1065 LeaveCriticalSection(&dinput_hook_crit);
1067 return hook_thread_id != 0;
1070 void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
1072 static HHOOK callwndproc_hook;
1073 static ULONG foreground_cnt;
1074 IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
1076 EnterCriticalSection(&dinput_hook_crit);
1078 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1080 if (dev->acquired)
1081 foreground_cnt++;
1082 else
1083 foreground_cnt--;
1086 if (foreground_cnt && !callwndproc_hook)
1087 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1088 DINPUT_instance, GetCurrentThreadId() );
1089 else if (!foreground_cnt && callwndproc_hook)
1091 UnhookWindowsHookEx( callwndproc_hook );
1092 callwndproc_hook = NULL;
1095 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1097 LeaveCriticalSection(&dinput_hook_crit);