wined3d: Call context_apply_draw_buffers under the GL lock.
[wine/multimedia.git] / dlls / dinput / dinput_main.c
blobd3727bcb570cd4c44d7375597c88a1ab52173cbb
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 "objbase.h"
48 #include "rpcproxy.h"
49 #include "dinput_private.h"
50 #include "device_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
54 static const IDirectInput7AVtbl ddi7avt;
55 static const IDirectInput7WVtbl ddi7wvt;
56 static const IDirectInput8AVtbl ddi8avt;
57 static const IDirectInput8WVtbl ddi8wvt;
59 static inline IDirectInputImpl *impl_from_IDirectInput7A( IDirectInput7A *iface )
61 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7A_iface );
64 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
66 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7W_iface );
69 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
71 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8A_iface );
74 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
76 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8W_iface );
79 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
81 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface);
83 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
85 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
88 static const struct dinput_device *dinput_devices[] =
90 &mouse_device,
91 &keyboard_device,
92 &joystick_linuxinput_device,
93 &joystick_linux_device,
94 &joystick_osx_device
96 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
98 static HINSTANCE DINPUT_instance = NULL;
100 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
102 switch(reason)
104 case DLL_PROCESS_ATTACH:
105 DisableThreadLibraryCalls(inst);
106 DINPUT_instance = inst;
107 break;
108 case DLL_PROCESS_DETACH:
109 break;
111 return TRUE;
114 static BOOL check_hook_thread(void);
115 static CRITICAL_SECTION dinput_hook_crit;
116 static struct list direct_input_list = LIST_INIT( direct_input_list );
118 /******************************************************************************
119 * DirectInputCreateEx (DINPUT.@)
121 HRESULT WINAPI DirectInputCreateEx(
122 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
123 LPUNKNOWN punkOuter)
125 IDirectInputImpl* This;
127 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
129 if (IsEqualGUID( &IID_IUnknown, riid ) ||
130 IsEqualGUID( &IID_IDirectInputA, riid ) ||
131 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
132 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
133 IsEqualGUID( &IID_IDirectInputW, riid ) ||
134 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
135 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
136 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
137 IsEqualGUID( &IID_IDirectInput8W, riid ))
139 if (!(This = HeapAlloc( GetProcessHeap(), 0, sizeof(IDirectInputImpl) )))
140 return DIERR_OUTOFMEMORY;
142 else
143 return DIERR_OLDDIRECTINPUTVERSION;
145 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
146 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
147 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
148 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
149 This->ref = 0;
150 This->dwVersion = dwVersion;
151 This->evsequence = 1;
153 InitializeCriticalSection(&This->crit);
154 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
156 list_init( &This->devices_list );
158 /* Add self to the list of the IDirectInputs */
159 EnterCriticalSection( &dinput_hook_crit );
160 list_add_head( &direct_input_list, &This->entry );
161 LeaveCriticalSection( &dinput_hook_crit );
163 if (!check_hook_thread())
165 IUnknown_Release( &This->IDirectInput7A_iface );
166 return DIERR_GENERIC;
169 IDirectInput_QueryInterface( &This->IDirectInput7A_iface, riid, ppDI );
170 return DI_OK;
173 /******************************************************************************
174 * DirectInputCreateA (DINPUT.@)
176 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
178 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
181 /******************************************************************************
182 * DirectInputCreateW (DINPUT.@)
184 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
186 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
189 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
190 switch (dwDevType) {
191 case 0: return "All devices";
192 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
193 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
194 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
195 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
196 default: return "Unknown";
200 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
201 if (TRACE_ON(dinput)) {
202 unsigned int i;
203 static const struct {
204 DWORD mask;
205 const char *name;
206 } flags[] = {
207 #define FE(x) { x, #x}
208 FE(DIEDFL_ALLDEVICES),
209 FE(DIEDFL_ATTACHEDONLY),
210 FE(DIEDFL_FORCEFEEDBACK),
211 FE(DIEDFL_INCLUDEALIASES),
212 FE(DIEDFL_INCLUDEPHANTOMS)
213 #undef FE
215 TRACE(" flags: ");
216 if (dwFlags == 0) {
217 TRACE("DIEDFL_ALLDEVICES\n");
218 return;
220 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
221 if (flags[i].mask & dwFlags)
222 TRACE("%s ",flags[i].name);
224 TRACE("\n");
227 void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
228 unsigned int i;
230 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
231 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
232 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
233 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
234 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
235 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
236 FIXME("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
237 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
238 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
239 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
240 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
241 FIXME("diaf.ftTimeStamp ...\n");
242 FIXME("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
243 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
244 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
246 FIXME("diaf.rgoAction[%u]:\n", i);
247 FIXME("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
248 FIXME("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
249 FIXME("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
250 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
251 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
252 FIXME("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
253 FIXME("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
257 /******************************************************************************
258 * IDirectInputA_EnumDevices
260 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
261 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
262 LPVOID pvRef, DWORD dwFlags)
264 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
265 DIDEVICEINSTANCEA devInstance;
266 unsigned int i;
267 int j, r;
269 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
270 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
271 lpCallback, pvRef, dwFlags);
272 _dump_EnumDevices_dwFlags(dwFlags);
274 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
275 if (!dinput_devices[i]->enum_deviceA) continue;
276 for (j = 0, r = -1; r != 0; j++) {
277 devInstance.dwSize = sizeof(devInstance);
278 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
279 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
280 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
281 return 0;
286 return 0;
288 /******************************************************************************
289 * IDirectInputW_EnumDevices
291 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
292 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
293 LPVOID pvRef, DWORD dwFlags)
295 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
296 DIDEVICEINSTANCEW devInstance;
297 unsigned int i;
298 int j, r;
300 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
301 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
302 lpCallback, pvRef, dwFlags);
303 _dump_EnumDevices_dwFlags(dwFlags);
305 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
306 if (!dinput_devices[i]->enum_deviceW) continue;
307 for (j = 0, r = -1; r != 0; j++) {
308 devInstance.dwSize = sizeof(devInstance);
309 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
310 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
311 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
312 return 0;
317 return 0;
320 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
322 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
323 ULONG ref = InterlockedIncrement(&This->ref);
325 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
326 return ref;
329 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
331 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
332 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
335 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
337 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
338 ULONG ref = InterlockedDecrement( &This->ref );
340 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
342 if (ref) return ref;
344 /* Remove self from the list of the IDirectInputs */
345 EnterCriticalSection( &dinput_hook_crit );
346 list_remove( &This->entry );
347 LeaveCriticalSection( &dinput_hook_crit );
349 check_hook_thread();
351 This->crit.DebugInfo->Spare[0] = 0;
352 DeleteCriticalSection( &This->crit );
353 HeapFree( GetProcessHeap(), 0, This );
355 return 0;
358 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
360 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
361 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
364 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
366 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
368 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
370 if (IsEqualGUID( &IID_IUnknown, riid ) ||
371 IsEqualGUID( &IID_IDirectInputA, riid ) ||
372 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
373 IsEqualGUID( &IID_IDirectInput7A, riid ))
375 *ppobj = &This->IDirectInput7A_iface;
376 IUnknown_AddRef( (IUnknown*)*ppobj );
378 return DI_OK;
381 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
382 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
383 IsEqualGUID( &IID_IDirectInput7W, riid ))
385 *ppobj = &This->IDirectInput7W_iface;
386 IUnknown_AddRef( (IUnknown*)*ppobj );
388 return DI_OK;
391 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
393 *ppobj = &This->IDirectInput8A_iface;
394 IUnknown_AddRef( (IUnknown*)*ppobj );
396 return DI_OK;
399 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
401 *ppobj = &This->IDirectInput8W_iface;
402 IUnknown_AddRef( (IUnknown*)*ppobj );
404 return DI_OK;
407 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
408 return E_FAIL;
411 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
413 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
414 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
417 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
418 TRACE("(this=%p,%p,%x)\n",iface, hinst, x);
420 /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
421 * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
422 * which broke applications like Tomb Raider Legend because it isn't a legal return value.
424 return DI_OK;
427 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
429 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
430 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
433 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
435 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
436 HRESULT hr;
437 LPDIRECTINPUTDEVICEA device;
439 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
441 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
442 if (hr != DI_OK) return DI_NOTATTACHED;
444 IUnknown_Release( device );
446 return DI_OK;
449 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
451 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
452 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
455 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
456 HWND hwndOwner,
457 DWORD dwFlags)
459 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
461 FIXME( "(%p)->(%p,%08x): stub\n", This, hwndOwner, dwFlags );
463 return DI_OK;
466 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
468 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
469 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
472 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
473 LPCSTR pszName, LPGUID pguidInstance)
475 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
477 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
479 return DI_OK;
482 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
483 LPCWSTR pszName, LPGUID pguidInstance)
485 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
487 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
489 return DI_OK;
492 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
493 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
495 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
496 HRESULT ret_value = DIERR_DEVICENOTREG;
497 unsigned int i;
499 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
501 if (!rguid || !pvOut) return E_POINTER;
503 /* Loop on all the devices to see if anyone matches the given GUID */
504 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
505 HRESULT ret;
507 if (!dinput_devices[i]->create_device) continue;
508 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, 0)) == DI_OK)
509 return DI_OK;
511 if (ret == DIERR_NOINTERFACE)
512 ret_value = DIERR_NOINTERFACE;
515 if (ret_value == DIERR_NOINTERFACE)
517 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
520 return ret_value;
523 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
524 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
526 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
527 HRESULT ret_value = DIERR_DEVICENOTREG;
528 unsigned int i;
530 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
532 if (!rguid || !pvOut) return E_POINTER;
534 /* Loop on all the devices to see if anyone matches the given GUID */
535 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
536 HRESULT ret;
538 if (!dinput_devices[i]->create_device) continue;
539 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, 1)) == DI_OK)
540 return DI_OK;
542 if (ret == DIERR_NOINTERFACE)
543 ret_value = DIERR_NOINTERFACE;
546 return ret_value;
549 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
550 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
552 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
555 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
556 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
558 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
561 /*******************************************************************************
562 * DirectInput8
565 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
567 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
568 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
571 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
573 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
574 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
577 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
579 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
580 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
583 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
585 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
586 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
589 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
591 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
592 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
595 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
597 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
598 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
601 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
602 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
604 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
605 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
608 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
609 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
611 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
612 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
615 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
616 LPVOID pvRef, DWORD dwFlags)
618 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
619 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
622 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
623 LPVOID pvRef, DWORD dwFlags)
625 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
626 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
629 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
631 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
632 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
635 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
637 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
638 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
641 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
643 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
644 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
647 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
649 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
650 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
653 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD x)
655 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
656 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
659 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD x)
661 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
662 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
665 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
667 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
668 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
671 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
673 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
674 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
677 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
678 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
679 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
680 LPVOID pvRef, DWORD dwFlags
683 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
685 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, ptszUserName, lpdiActionFormat,
686 lpCallback, pvRef, dwFlags);
687 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
688 X(DIEDBSFL_ATTACHEDONLY)
689 X(DIEDBSFL_THISUSER)
690 X(DIEDBSFL_FORCEFEEDBACK)
691 X(DIEDBSFL_AVAILABLEDEVICES)
692 X(DIEDBSFL_MULTIMICEKEYBOARDS)
693 X(DIEDBSFL_NONGAMINGDEVICES)
694 #undef X
696 _dump_diactionformatA(lpdiActionFormat);
698 return DI_OK;
701 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
702 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
703 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
704 LPVOID pvRef, DWORD dwFlags
707 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
709 FIXME("(this=%p,%s,%p,%p,%p,%04x): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
710 lpCallback, pvRef, dwFlags);
711 return 0;
714 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
715 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
716 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
719 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
721 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
722 dwFlags, pvRefData);
723 return 0;
726 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
727 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
728 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
731 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
733 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams,
734 dwFlags, pvRefData);
735 return 0;
738 static const IDirectInput7AVtbl ddi7avt = {
739 IDirectInputAImpl_QueryInterface,
740 IDirectInputAImpl_AddRef,
741 IDirectInputAImpl_Release,
742 IDirectInputAImpl_CreateDevice,
743 IDirectInputAImpl_EnumDevices,
744 IDirectInputAImpl_GetDeviceStatus,
745 IDirectInputAImpl_RunControlPanel,
746 IDirectInputAImpl_Initialize,
747 IDirectInput2AImpl_FindDevice,
748 IDirectInput7AImpl_CreateDeviceEx
751 static const IDirectInput7WVtbl ddi7wvt = {
752 IDirectInputWImpl_QueryInterface,
753 IDirectInputWImpl_AddRef,
754 IDirectInputWImpl_Release,
755 IDirectInputWImpl_CreateDevice,
756 IDirectInputWImpl_EnumDevices,
757 IDirectInputWImpl_GetDeviceStatus,
758 IDirectInputWImpl_RunControlPanel,
759 IDirectInputWImpl_Initialize,
760 IDirectInput2WImpl_FindDevice,
761 IDirectInput7WImpl_CreateDeviceEx
764 static const IDirectInput8AVtbl ddi8avt = {
765 IDirectInput8AImpl_QueryInterface,
766 IDirectInput8AImpl_AddRef,
767 IDirectInput8AImpl_Release,
768 IDirectInput8AImpl_CreateDevice,
769 IDirectInput8AImpl_EnumDevices,
770 IDirectInput8AImpl_GetDeviceStatus,
771 IDirectInput8AImpl_RunControlPanel,
772 IDirectInput8AImpl_Initialize,
773 IDirectInput8AImpl_FindDevice,
774 IDirectInput8AImpl_EnumDevicesBySemantics,
775 IDirectInput8AImpl_ConfigureDevices
778 static const IDirectInput8WVtbl ddi8wvt = {
779 IDirectInput8WImpl_QueryInterface,
780 IDirectInput8WImpl_AddRef,
781 IDirectInput8WImpl_Release,
782 IDirectInput8WImpl_CreateDevice,
783 IDirectInput8WImpl_EnumDevices,
784 IDirectInput8WImpl_GetDeviceStatus,
785 IDirectInput8WImpl_RunControlPanel,
786 IDirectInput8WImpl_Initialize,
787 IDirectInput8WImpl_FindDevice,
788 IDirectInput8WImpl_EnumDevicesBySemantics,
789 IDirectInput8WImpl_ConfigureDevices
792 /*******************************************************************************
793 * DirectInput ClassFactory
795 typedef struct
797 /* IUnknown fields */
798 IClassFactory IClassFactory_iface;
799 LONG ref;
800 } IClassFactoryImpl;
802 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
804 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
807 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
808 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
810 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
811 return E_NOINTERFACE;
814 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
815 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
816 return InterlockedIncrement(&(This->ref));
819 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
820 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
821 /* static class, won't be freed */
822 return InterlockedDecrement(&(This->ref));
825 static HRESULT WINAPI DICF_CreateInstance(
826 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
828 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
830 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
831 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
832 IsEqualGUID( &IID_IDirectInputA, riid ) ||
833 IsEqualGUID( &IID_IDirectInputW, riid ) ||
834 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
835 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
836 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
837 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
838 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
839 IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
840 /* FIXME: reuse already created dinput if present? */
841 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
844 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
845 return E_NOINTERFACE;
848 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
849 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
850 FIXME("(%p)->(%d),stub!\n",This,dolock);
851 return S_OK;
854 static const IClassFactoryVtbl DICF_Vtbl = {
855 DICF_QueryInterface,
856 DICF_AddRef,
857 DICF_Release,
858 DICF_CreateInstance,
859 DICF_LockServer
861 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
863 /***********************************************************************
864 * DllCanUnloadNow (DINPUT.@)
866 HRESULT WINAPI DllCanUnloadNow(void)
868 return S_FALSE;
871 /***********************************************************************
872 * DllGetClassObject (DINPUT.@)
874 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
876 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
877 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
878 *ppv = &DINPUT_CF;
879 IClassFactory_AddRef((IClassFactory*)*ppv);
880 return S_OK;
883 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
884 return CLASS_E_CLASSNOTAVAILABLE;
887 /***********************************************************************
888 * DllRegisterServer (DINPUT.@)
890 HRESULT WINAPI DllRegisterServer(void)
892 return __wine_register_resources( DINPUT_instance, NULL );
895 /***********************************************************************
896 * DllUnregisterServer (DINPUT.@)
898 HRESULT WINAPI DllUnregisterServer(void)
900 return __wine_unregister_resources( DINPUT_instance, NULL );
903 /******************************************************************************
904 * DInput hook thread
907 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
909 IDirectInputImpl *dinput;
910 int skip = 0;
912 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
914 EnterCriticalSection( &dinput_hook_crit );
915 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
917 IDirectInputDeviceImpl *dev;
919 EnterCriticalSection( &dinput->crit );
920 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
921 if (dev->acquired && dev->event_proc)
923 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
924 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
926 LeaveCriticalSection( &dinput->crit );
928 LeaveCriticalSection( &dinput_hook_crit );
930 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
933 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
935 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
936 IDirectInputImpl *dinput;
937 HWND foreground;
939 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
940 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
941 return CallNextHookEx( 0, code, wparam, lparam );
943 foreground = GetForegroundWindow();
945 EnterCriticalSection( &dinput_hook_crit );
947 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
949 IDirectInputDeviceImpl *dev;
951 EnterCriticalSection( &dinput->crit );
952 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
954 if (!dev->acquired) continue;
956 if (msg->hwnd == dev->win && msg->hwnd != foreground)
958 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
959 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
962 LeaveCriticalSection( &dinput->crit );
964 LeaveCriticalSection( &dinput_hook_crit );
966 return CallNextHookEx( 0, code, wparam, lparam );
969 static DWORD WINAPI hook_thread_proc(void *param)
971 static HHOOK kbd_hook, mouse_hook;
972 MSG msg;
974 /* Force creation of the message queue */
975 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
976 SetEvent(*(LPHANDLE)param);
978 while (GetMessageW( &msg, 0, 0, 0 ))
980 UINT kbd_cnt = 0, mice_cnt = 0;
982 if (msg.message == WM_USER+0x10)
984 IDirectInputImpl *dinput;
986 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
988 if (!msg.wParam && !msg.lParam)
990 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
991 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
992 kbd_hook = mouse_hook = NULL;
993 break;
996 EnterCriticalSection( &dinput_hook_crit );
998 /* Count acquired keyboards and mice*/
999 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1001 IDirectInputDeviceImpl *dev;
1003 EnterCriticalSection( &dinput->crit );
1004 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1006 if (!dev->acquired || !dev->event_proc) continue;
1008 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1009 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1010 kbd_cnt++;
1011 else
1012 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1013 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1014 mice_cnt++;
1016 LeaveCriticalSection( &dinput->crit );
1018 LeaveCriticalSection( &dinput_hook_crit );
1020 if (kbd_cnt && !kbd_hook)
1021 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1022 else if (!kbd_cnt && kbd_hook)
1024 UnhookWindowsHookEx( kbd_hook );
1025 kbd_hook = NULL;
1028 if (mice_cnt && !mouse_hook)
1029 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1030 else if (!mice_cnt && mouse_hook)
1032 UnhookWindowsHookEx( mouse_hook );
1033 mouse_hook = NULL;
1036 TranslateMessage(&msg);
1037 DispatchMessageW(&msg);
1040 return 0;
1043 static DWORD hook_thread_id;
1045 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1047 0, 0, &dinput_hook_crit,
1048 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1049 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1051 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1053 static BOOL check_hook_thread(void)
1055 static HANDLE hook_thread;
1057 EnterCriticalSection(&dinput_hook_crit);
1059 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1060 if (!list_empty(&direct_input_list) && !hook_thread)
1062 HANDLE event;
1064 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1065 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1066 if (event && hook_thread)
1068 HANDLE handles[2];
1069 handles[0] = event;
1070 handles[1] = hook_thread;
1071 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1073 LeaveCriticalSection(&dinput_hook_crit);
1074 CloseHandle(event);
1076 else if (list_empty(&direct_input_list) && hook_thread)
1078 DWORD tid = hook_thread_id;
1080 hook_thread_id = 0;
1081 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1082 LeaveCriticalSection(&dinput_hook_crit);
1084 /* wait for hook thread to exit */
1085 WaitForSingleObject(hook_thread, INFINITE);
1086 CloseHandle(hook_thread);
1087 hook_thread = NULL;
1089 else
1090 LeaveCriticalSection(&dinput_hook_crit);
1092 return hook_thread_id != 0;
1095 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1097 static HHOOK callwndproc_hook;
1098 static ULONG foreground_cnt;
1099 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1101 EnterCriticalSection(&dinput_hook_crit);
1103 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1105 if (dev->acquired)
1106 foreground_cnt++;
1107 else
1108 foreground_cnt--;
1111 if (foreground_cnt && !callwndproc_hook)
1112 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1113 DINPUT_instance, GetCurrentThreadId() );
1114 else if (!foreground_cnt && callwndproc_hook)
1116 UnhookWindowsHookEx( callwndproc_hook );
1117 callwndproc_hook = NULL;
1120 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1122 LeaveCriticalSection(&dinput_hook_crit);