ntoskrnl.exe/tests: Fix path buffer allocation size.
[wine.git] / dlls / dinput / dinput_main.c
blob7074704d7ae3d22735bedffd1324d90af6566109
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 "initguid.h"
50 #include "dinput_private.h"
51 #include "device_private.h"
52 #include "dinputd.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
56 static const IDirectInput7AVtbl ddi7avt;
57 static const IDirectInput7WVtbl ddi7wvt;
58 static const IDirectInput8AVtbl ddi8avt;
59 static const IDirectInput8WVtbl ddi8wvt;
60 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt;
62 static inline IDirectInputImpl *impl_from_IDirectInput7A( IDirectInput7A *iface )
64 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7A_iface );
67 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
69 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7W_iface );
72 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
74 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8A_iface );
77 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
79 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8W_iface );
82 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
84 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
87 static const struct dinput_device *dinput_devices[] =
89 &mouse_device,
90 &keyboard_device,
91 &joystick_linuxinput_device,
92 &joystick_linux_device,
93 &joystick_osx_device
96 HINSTANCE DINPUT_instance;
98 static BOOL check_hook_thread(void);
99 static CRITICAL_SECTION dinput_hook_crit;
100 static struct list direct_input_list = LIST_INIT( direct_input_list );
102 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion);
103 static void uninitialize_directinput_instance(IDirectInputImpl *This);
105 static HRESULT create_directinput_instance(REFIID riid, LPVOID *ppDI, IDirectInputImpl **out)
107 IDirectInputImpl *This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectInputImpl) );
108 HRESULT hr;
110 if (!This)
111 return E_OUTOFMEMORY;
113 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
114 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
115 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
116 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
117 This->IDirectInputJoyConfig8_iface.lpVtbl = &JoyConfig8vt;
119 hr = IDirectInput_QueryInterface( &This->IDirectInput7A_iface, riid, ppDI );
120 if (FAILED(hr))
122 HeapFree( GetProcessHeap(), 0, This );
123 return hr;
126 if (out) *out = This;
127 return DI_OK;
130 /******************************************************************************
131 * DirectInputCreateEx (DINPUT.@)
133 HRESULT WINAPI DirectInputCreateEx(
134 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
135 LPUNKNOWN punkOuter)
137 IDirectInputImpl *This;
138 HRESULT hr;
140 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
142 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
143 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
144 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
145 IsEqualGUID( &IID_IDirectInputW, riid ) ||
146 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
147 IsEqualGUID( &IID_IDirectInput7W, riid ))
149 hr = create_directinput_instance(riid, ppDI, &This);
150 if (FAILED(hr))
151 return hr;
153 else
154 return DIERR_NOINTERFACE;
156 hr = IDirectInput_Initialize( &This->IDirectInput7A_iface, hinst, dwVersion );
157 if (FAILED(hr))
159 IDirectInput_Release( &This->IDirectInput7A_iface );
160 *ppDI = NULL;
161 return hr;
164 return DI_OK;
167 /******************************************************************************
168 * DirectInput8Create (DINPUT8.@)
170 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInput8Create(HINSTANCE hinst,
171 DWORD version, REFIID iid, void **out, IUnknown *outer)
173 IDirectInputImpl *This;
174 HRESULT hr;
176 TRACE("hinst %p, version %#x, iid %s, out %p, outer %p.\n",
177 hinst, version, debugstr_guid(iid), out, outer);
179 if (!out)
180 return E_POINTER;
182 if (!IsEqualGUID(&IID_IDirectInput8A, iid) &&
183 !IsEqualGUID(&IID_IDirectInput8W, iid) &&
184 !IsEqualGUID(&IID_IUnknown, iid))
186 *out = NULL;
187 return DIERR_NOINTERFACE;
190 hr = create_directinput_instance(iid, out, &This);
192 if (FAILED(hr))
194 ERR("Failed to create DirectInput, hr %#x.\n", hr);
195 return hr;
198 /* When aggregation is used, the application needs to manually call Initialize(). */
199 if (!outer && IsEqualGUID(&IID_IDirectInput8A, iid))
201 hr = IDirectInput8_Initialize(&This->IDirectInput8A_iface, hinst, version);
202 if (FAILED(hr))
204 IDirectInput8_Release(&This->IDirectInput8A_iface);
205 *out = NULL;
206 return hr;
210 if (!outer && IsEqualGUID(&IID_IDirectInput8W, iid))
212 hr = IDirectInput8_Initialize(&This->IDirectInput8W_iface, hinst, version);
213 if (FAILED(hr))
215 IDirectInput8_Release(&This->IDirectInput8W_iface);
216 *out = NULL;
217 return hr;
221 return S_OK;
224 /******************************************************************************
225 * DirectInputCreateA (DINPUT.@)
227 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
229 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
232 /******************************************************************************
233 * DirectInputCreateW (DINPUT.@)
235 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
237 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
240 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType, DWORD dwVersion)
242 if (dwVersion < 0x0800) {
243 switch (dwDevType) {
244 case 0: return "All devices";
245 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
246 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
247 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
248 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
249 default: return "Unknown";
251 } else {
252 switch (dwDevType) {
253 case DI8DEVCLASS_ALL: return "All devices";
254 case DI8DEVCLASS_POINTER: return "DI8DEVCLASS_POINTER";
255 case DI8DEVCLASS_KEYBOARD: return "DI8DEVCLASS_KEYBOARD";
256 case DI8DEVCLASS_DEVICE: return "DI8DEVCLASS_DEVICE";
257 case DI8DEVCLASS_GAMECTRL: return "DI8DEVCLASS_GAMECTRL";
258 default: return "Unknown";
263 static void _dump_EnumDevices_dwFlags(DWORD dwFlags)
265 if (TRACE_ON(dinput)) {
266 unsigned int i;
267 static const struct {
268 DWORD mask;
269 const char *name;
270 } flags[] = {
271 #define FE(x) { x, #x}
272 FE(DIEDFL_ALLDEVICES),
273 FE(DIEDFL_ATTACHEDONLY),
274 FE(DIEDFL_FORCEFEEDBACK),
275 FE(DIEDFL_INCLUDEALIASES),
276 FE(DIEDFL_INCLUDEPHANTOMS),
277 FE(DIEDFL_INCLUDEHIDDEN)
278 #undef FE
280 TRACE(" flags: ");
281 if (dwFlags == 0) {
282 TRACE("DIEDFL_ALLDEVICES\n");
283 return;
285 for (i = 0; i < ARRAY_SIZE(flags); i++)
286 if (flags[i].mask & dwFlags)
287 TRACE("%s ",flags[i].name);
289 TRACE("\n");
292 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat)
294 unsigned int i;
296 TRACE("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
297 TRACE("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
298 TRACE("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
299 TRACE("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
300 TRACE("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
301 TRACE("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
302 TRACE("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
303 TRACE("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
304 TRACE("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
305 TRACE("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
306 TRACE("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
307 TRACE("diaf.ftTimeStamp ...\n");
308 TRACE("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
309 TRACE("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
310 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
312 TRACE("diaf.rgoAction[%u]:\n", i);
313 TRACE("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
314 TRACE("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
315 TRACE("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
316 TRACE("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
317 TRACE("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
318 TRACE("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
319 TRACE("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
323 void _copy_diactionformatAtoW(LPDIACTIONFORMATW to, LPDIACTIONFORMATA from)
325 int i;
327 to->dwSize = sizeof(DIACTIONFORMATW);
328 to->dwActionSize = sizeof(DIACTIONW);
329 to->dwDataSize = from->dwDataSize;
330 to->dwNumActions = from->dwNumActions;
331 to->guidActionMap = from->guidActionMap;
332 to->dwGenre = from->dwGenre;
333 to->dwBufferSize = from->dwBufferSize;
334 to->lAxisMin = from->lAxisMin;
335 to->lAxisMax = from->lAxisMax;
336 to->dwCRC = from->dwCRC;
337 to->ftTimeStamp = from->ftTimeStamp;
339 for (i=0; i < to->dwNumActions; i++)
341 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
342 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
343 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
344 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
345 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
346 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
350 void _copy_diactionformatWtoA(LPDIACTIONFORMATA to, LPDIACTIONFORMATW from)
352 int i;
354 to->dwSize = sizeof(DIACTIONFORMATA);
355 to->dwActionSize = sizeof(DIACTIONA);
356 to->dwDataSize = from->dwDataSize;
357 to->dwNumActions = from->dwNumActions;
358 to->guidActionMap = from->guidActionMap;
359 to->dwGenre = from->dwGenre;
360 to->dwBufferSize = from->dwBufferSize;
361 to->lAxisMin = from->lAxisMin;
362 to->lAxisMax = from->lAxisMax;
363 to->dwCRC = from->dwCRC;
364 to->ftTimeStamp = from->ftTimeStamp;
366 for (i=0; i < to->dwNumActions; i++)
368 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
369 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
370 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
371 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
372 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
373 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
377 /* diactionformat_priority
379 * Given a DIACTIONFORMAT structure and a DI genre, returns the enumeration
380 * priority. Joysticks should pass the game genre, and mouse or keyboard their
381 * respective DI*_MASK
383 static DWORD diactionformat_priorityA(LPDIACTIONFORMATA lpdiaf, DWORD genre)
385 int i;
386 DWORD priorityFlags = 0;
388 /* If there's at least one action for the device it's priority 1 */
389 for(i=0; i < lpdiaf->dwNumActions; i++)
390 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
391 priorityFlags |= DIEDBS_MAPPEDPRI1;
393 return priorityFlags;
396 static DWORD diactionformat_priorityW(LPDIACTIONFORMATW lpdiaf, DWORD genre)
398 int i;
399 DWORD priorityFlags = 0;
401 /* If there's at least one action for the device it's priority 1 */
402 for(i=0; i < lpdiaf->dwNumActions; i++)
403 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
404 priorityFlags |= DIEDBS_MAPPEDPRI1;
406 return priorityFlags;
409 #if defined __i386__ && defined _MSC_VER
410 __declspec(naked) BOOL enum_callback_wrapper(void *callback, const void *instance, void *ref)
412 __asm
414 push ebp
415 mov ebp, esp
416 push [ebp+16]
417 push [ebp+12]
418 call [ebp+8]
419 leave
423 #elif defined __i386__ && defined __GNUC__
424 extern BOOL enum_callback_wrapper(void *callback, const void *instance, void *ref);
425 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
426 "pushl %ebp\n\t"
427 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
428 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
429 "movl %esp,%ebp\n\t"
430 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
431 "pushl 16(%ebp)\n\t"
432 "pushl 12(%ebp)\n\t"
433 "call *8(%ebp)\n\t"
434 "leave\n\t"
435 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
436 __ASM_CFI(".cfi_same_value %ebp\n\t")
437 "ret" )
438 #else
439 #define enum_callback_wrapper(callback, instance, ref) (callback)((instance), (ref))
440 #endif
442 /******************************************************************************
443 * IDirectInputA_EnumDevices
445 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
446 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
447 LPVOID pvRef, DWORD dwFlags)
449 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
450 DIDEVICEINSTANCEA devInstance;
451 unsigned int i;
452 int j;
453 HRESULT r;
455 TRACE("(this=%p,0x%04x '%s',%p,%p,0x%04x)\n",
456 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType, This->dwVersion),
457 lpCallback, pvRef, dwFlags);
458 _dump_EnumDevices_dwFlags(dwFlags);
460 if (!lpCallback ||
461 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
462 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
463 return DIERR_INVALIDPARAM;
465 if (!This->initialized)
466 return DIERR_NOTINITIALIZED;
468 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++) {
469 if (!dinput_devices[i]->enum_deviceA) continue;
470 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
471 devInstance.dwSize = sizeof(devInstance);
472 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
473 r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
474 if (r == S_OK)
475 if (enum_callback_wrapper(lpCallback, &devInstance, pvRef) == DIENUM_STOP)
476 return S_OK;
480 return S_OK;
482 /******************************************************************************
483 * IDirectInputW_EnumDevices
485 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
486 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
487 LPVOID pvRef, DWORD dwFlags)
489 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
490 DIDEVICEINSTANCEW devInstance;
491 unsigned int i;
492 int j;
493 HRESULT r;
495 TRACE("(this=%p,0x%04x '%s',%p,%p,0x%04x)\n",
496 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType, This->dwVersion),
497 lpCallback, pvRef, dwFlags);
498 _dump_EnumDevices_dwFlags(dwFlags);
500 if (!lpCallback ||
501 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
502 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
503 return DIERR_INVALIDPARAM;
505 if (!This->initialized)
506 return DIERR_NOTINITIALIZED;
508 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++) {
509 if (!dinput_devices[i]->enum_deviceW) continue;
510 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
511 devInstance.dwSize = sizeof(devInstance);
512 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
513 r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
514 if (r == S_OK)
515 if (enum_callback_wrapper(lpCallback, &devInstance, pvRef) == DIENUM_STOP)
516 return S_OK;
520 return S_OK;
523 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
525 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
526 ULONG ref = InterlockedIncrement(&This->ref);
528 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
529 return ref;
532 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
534 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
535 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
538 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
540 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
541 ULONG ref = InterlockedDecrement( &This->ref );
543 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
545 if (ref == 0)
547 uninitialize_directinput_instance( This );
548 HeapFree( GetProcessHeap(), 0, This );
551 return ref;
554 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
556 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
557 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
560 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
562 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
564 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
566 if (!riid || !ppobj)
567 return E_POINTER;
569 if (IsEqualGUID( &IID_IUnknown, riid ) ||
570 IsEqualGUID( &IID_IDirectInputA, riid ) ||
571 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
572 IsEqualGUID( &IID_IDirectInput7A, riid ))
574 *ppobj = &This->IDirectInput7A_iface;
575 IUnknown_AddRef( (IUnknown*)*ppobj );
577 return DI_OK;
580 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
581 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
582 IsEqualGUID( &IID_IDirectInput7W, riid ))
584 *ppobj = &This->IDirectInput7W_iface;
585 IUnknown_AddRef( (IUnknown*)*ppobj );
587 return DI_OK;
590 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
592 *ppobj = &This->IDirectInput8A_iface;
593 IUnknown_AddRef( (IUnknown*)*ppobj );
595 return DI_OK;
598 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
600 *ppobj = &This->IDirectInput8W_iface;
601 IUnknown_AddRef( (IUnknown*)*ppobj );
603 return DI_OK;
606 if (IsEqualGUID( &IID_IDirectInputJoyConfig8, riid ))
608 *ppobj = &This->IDirectInputJoyConfig8_iface;
609 IUnknown_AddRef( (IUnknown*)*ppobj );
611 return DI_OK;
614 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
615 *ppobj = NULL;
616 return E_NOINTERFACE;
619 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
621 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
622 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
625 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion)
627 if (!This->initialized)
629 This->dwVersion = dwVersion;
630 This->evsequence = 1;
632 InitializeCriticalSection( &This->crit );
633 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
635 list_init( &This->devices_list );
636 list_init( &This->device_players );
638 /* Add self to the list of the IDirectInputs */
639 EnterCriticalSection( &dinput_hook_crit );
640 list_add_head( &direct_input_list, &This->entry );
641 LeaveCriticalSection( &dinput_hook_crit );
643 This->initialized = TRUE;
645 if (!check_hook_thread())
647 uninitialize_directinput_instance( This );
648 return DIERR_GENERIC;
652 return DI_OK;
655 static void uninitialize_directinput_instance(IDirectInputImpl *This)
657 if (This->initialized)
659 struct DevicePlayer *device_player, *device_player2;
660 /* Remove self from the list of the IDirectInputs */
661 EnterCriticalSection( &dinput_hook_crit );
662 list_remove( &This->entry );
663 LeaveCriticalSection( &dinput_hook_crit );
665 LIST_FOR_EACH_ENTRY_SAFE( device_player, device_player2,
666 &This->device_players, struct DevicePlayer, entry )
667 HeapFree(GetProcessHeap(), 0, device_player);
669 check_hook_thread();
671 This->crit.DebugInfo->Spare[0] = 0;
672 DeleteCriticalSection( &This->crit );
674 This->initialized = FALSE;
678 enum directinput_versions
680 DIRECTINPUT_VERSION_300 = 0x0300,
681 DIRECTINPUT_VERSION_500 = 0x0500,
682 DIRECTINPUT_VERSION_50A = 0x050A,
683 DIRECTINPUT_VERSION_5B2 = 0x05B2,
684 DIRECTINPUT_VERSION_602 = 0x0602,
685 DIRECTINPUT_VERSION_61A = 0x061A,
686 DIRECTINPUT_VERSION_700 = 0x0700,
689 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD version)
691 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
693 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
695 if (!hinst)
696 return DIERR_INVALIDPARAM;
697 else if (version == 0)
698 return DIERR_NOTINITIALIZED;
699 else if (version > DIRECTINPUT_VERSION_700)
700 return DIERR_OLDDIRECTINPUTVERSION;
701 else if (version != DIRECTINPUT_VERSION_300 && version != DIRECTINPUT_VERSION_500 &&
702 version != DIRECTINPUT_VERSION_50A && version != DIRECTINPUT_VERSION_5B2 &&
703 version != DIRECTINPUT_VERSION_602 && version != DIRECTINPUT_VERSION_61A &&
704 version != DIRECTINPUT_VERSION_700 && version != DIRECTINPUT_VERSION)
705 return DIERR_BETADIRECTINPUTVERSION;
707 return initialize_directinput_instance(This, version);
710 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
712 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
713 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
716 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
718 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
719 HRESULT hr;
720 LPDIRECTINPUTDEVICEA device;
722 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
724 if (!rguid) return E_POINTER;
725 if (!This->initialized)
726 return DIERR_NOTINITIALIZED;
728 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
729 if (hr != DI_OK) return DI_NOTATTACHED;
731 IUnknown_Release( device );
733 return DI_OK;
736 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
738 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
739 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
742 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
743 HWND hwndOwner,
744 DWORD dwFlags)
746 WCHAR control_exeW[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
747 STARTUPINFOW si = {0};
748 PROCESS_INFORMATION pi;
750 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
752 TRACE( "(%p)->(%p, %08x)\n", This, hwndOwner, dwFlags );
754 if (hwndOwner && !IsWindow(hwndOwner))
755 return E_HANDLE;
757 if (dwFlags)
758 return DIERR_INVALIDPARAM;
760 if (!This->initialized)
761 return DIERR_NOTINITIALIZED;
763 if (!CreateProcessW(NULL, control_exeW, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
764 return HRESULT_FROM_WIN32(GetLastError());
766 return DI_OK;
769 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
771 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
772 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
775 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
776 LPCSTR pszName, LPGUID pguidInstance)
778 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
780 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
782 return DI_OK;
785 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
786 LPCWSTR pszName, LPGUID pguidInstance)
788 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
790 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
792 return DI_OK;
795 static HRESULT create_device(IDirectInputImpl *This, REFGUID rguid, REFIID riid, LPVOID *pvOut, BOOL unicode)
797 unsigned int i;
799 if (pvOut)
800 *pvOut = NULL;
802 if (!rguid || !pvOut)
803 return E_POINTER;
805 if (!This->initialized)
806 return DIERR_NOTINITIALIZED;
808 /* Loop on all the devices to see if anyone matches the given GUID */
809 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
811 HRESULT ret;
813 if (!dinput_devices[i]->create_device) continue;
814 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, unicode)) == DI_OK)
815 return DI_OK;
818 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
819 return DIERR_DEVICENOTREG;
822 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
823 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
825 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
827 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
829 return create_device(This, rguid, riid, pvOut, FALSE);
832 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
833 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
835 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
837 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
839 return create_device(This, rguid, riid, pvOut, TRUE);
842 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
843 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
845 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
848 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
849 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
851 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
854 /*******************************************************************************
855 * DirectInput8
858 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
860 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
861 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
864 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
866 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
867 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
870 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
872 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
873 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
876 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
878 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
879 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
882 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
884 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
885 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
888 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
890 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
891 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
894 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
895 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
897 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
898 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
901 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
902 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
904 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
905 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
908 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
909 LPVOID pvRef, DWORD dwFlags)
911 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
912 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
915 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
916 LPVOID pvRef, DWORD dwFlags)
918 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
919 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
922 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
924 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
925 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
928 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
930 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
931 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
934 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
936 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
937 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
940 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
942 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
943 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
946 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD version)
948 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
950 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
952 if (!hinst)
953 return DIERR_INVALIDPARAM;
954 else if (version == 0)
955 return DIERR_NOTINITIALIZED;
956 else if (version < DIRECTINPUT_VERSION)
957 return DIERR_BETADIRECTINPUTVERSION;
958 else if (version > DIRECTINPUT_VERSION)
959 return DIERR_OLDDIRECTINPUTVERSION;
961 return initialize_directinput_instance(This, version);
964 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD version)
966 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
967 return IDirectInput8AImpl_Initialize( &This->IDirectInput8A_iface, hinst, version );
970 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
972 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
973 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
976 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
978 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
979 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
982 static BOOL should_enumerate_device(const WCHAR *username, DWORD dwFlags,
983 struct list *device_players, REFGUID guid)
985 BOOL should_enumerate = TRUE;
986 struct DevicePlayer *device_player;
988 /* Check if user owns this device */
989 if (dwFlags & DIEDBSFL_THISUSER && username && *username)
991 should_enumerate = FALSE;
992 LIST_FOR_EACH_ENTRY(device_player, device_players, struct DevicePlayer, entry)
994 if (IsEqualGUID(&device_player->instance_guid, guid))
996 if (*device_player->username && !lstrcmpW(username, device_player->username))
997 return TRUE; /* Device username matches */
998 break;
1003 /* Check if this device is not owned by anyone */
1004 if (dwFlags & DIEDBSFL_AVAILABLEDEVICES) {
1005 BOOL found = FALSE;
1006 should_enumerate = FALSE;
1007 LIST_FOR_EACH_ENTRY(device_player, device_players, struct DevicePlayer, entry)
1009 if (IsEqualGUID(&device_player->instance_guid, guid))
1011 if (*device_player->username)
1012 found = TRUE;
1013 break;
1016 if (!found)
1017 return TRUE; /* Device does not have a username */
1020 return should_enumerate;
1023 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
1024 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
1025 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
1026 LPVOID pvRef, DWORD dwFlags
1029 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
1030 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
1031 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
1032 DIDEVICEINSTANCEA didevi;
1033 LPDIRECTINPUTDEVICE8A lpdid;
1034 DWORD callbackFlags;
1035 int i, j;
1036 int device_count = 0;
1037 int remain;
1038 DIDEVICEINSTANCEA *didevis = 0;
1039 WCHAR *username_w = 0;
1041 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_a(ptszUserName), lpdiActionFormat,
1042 lpCallback, pvRef, dwFlags);
1043 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
1044 X(DIEDBSFL_ATTACHEDONLY)
1045 X(DIEDBSFL_THISUSER)
1046 X(DIEDBSFL_FORCEFEEDBACK)
1047 X(DIEDBSFL_AVAILABLEDEVICES)
1048 X(DIEDBSFL_MULTIMICEKEYBOARDS)
1049 X(DIEDBSFL_NONGAMINGDEVICES)
1050 #undef X
1052 _dump_diactionformatA(lpdiActionFormat);
1054 didevi.dwSize = sizeof(didevi);
1056 if (ptszUserName)
1058 int len = MultiByteToWideChar(CP_ACP, 0, ptszUserName, -1, 0, 0);
1060 username_w = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1061 MultiByteToWideChar(CP_ACP, 0, ptszUserName, -1, username_w, len);
1064 /* Enumerate all the joysticks */
1065 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
1067 HRESULT enumSuccess;
1069 if (!dinput_devices[i]->enum_deviceA) continue;
1071 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
1073 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
1075 /* Default behavior is to enumerate attached game controllers */
1076 enumSuccess = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
1077 if (enumSuccess == S_OK &&
1078 should_enumerate_device(username_w, dwFlags, &This->device_players, &didevi.guidInstance))
1080 if (device_count++)
1081 didevis = HeapReAlloc(GetProcessHeap(), 0, didevis, sizeof(DIDEVICEINSTANCEA)*device_count);
1082 else
1083 didevis = HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEA)*device_count);
1084 didevis[device_count-1] = didevi;
1089 remain = device_count;
1090 /* Add keyboard and mouse to remaining device count */
1091 if (!(dwFlags & DIEDBSFL_FORCEFEEDBACK))
1093 for (i = 0; i < ARRAY_SIZE(guids); i++)
1095 if (should_enumerate_device(username_w, dwFlags, &This->device_players, guids[i]))
1096 remain++;
1100 for (i = 0; i < device_count; i++)
1102 callbackFlags = diactionformat_priorityA(lpdiActionFormat, lpdiActionFormat->dwGenre);
1103 IDirectInput_CreateDevice(iface, &didevis[i].guidInstance, &lpdid, NULL);
1105 if (lpCallback(&didevis[i], lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1107 HeapFree(GetProcessHeap(), 0, didevis);
1108 HeapFree(GetProcessHeap(), 0, username_w);
1109 return DI_OK;
1113 HeapFree(GetProcessHeap(), 0, didevis);
1115 if (dwFlags & DIEDBSFL_FORCEFEEDBACK)
1117 HeapFree(GetProcessHeap(), 0, username_w);
1118 return DI_OK;
1121 /* Enumerate keyboard and mouse */
1122 for (i = 0; i < ARRAY_SIZE(guids); i++)
1124 if (should_enumerate_device(username_w, dwFlags, &This->device_players, guids[i]))
1126 callbackFlags = diactionformat_priorityA(lpdiActionFormat, actionMasks[i]);
1128 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1129 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1131 if (lpCallback(&didevi, lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1133 HeapFree(GetProcessHeap(), 0, username_w);
1134 return DI_OK;
1139 HeapFree(GetProcessHeap(), 0, username_w);
1140 return DI_OK;
1143 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
1144 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
1145 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
1146 LPVOID pvRef, DWORD dwFlags
1149 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
1150 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
1151 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1152 DIDEVICEINSTANCEW didevi;
1153 LPDIRECTINPUTDEVICE8W lpdid;
1154 DWORD callbackFlags;
1155 int i, j;
1156 int device_count = 0;
1157 int remain;
1158 DIDEVICEINSTANCEW *didevis = 0;
1160 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
1161 lpCallback, pvRef, dwFlags);
1163 didevi.dwSize = sizeof(didevi);
1165 /* Enumerate all the joysticks */
1166 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
1168 HRESULT enumSuccess;
1170 if (!dinput_devices[i]->enum_deviceW) continue;
1172 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
1174 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
1176 /* Default behavior is to enumerate attached game controllers */
1177 enumSuccess = dinput_devices[i]->enum_deviceW(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
1178 if (enumSuccess == S_OK &&
1179 should_enumerate_device(ptszUserName, dwFlags, &This->device_players, &didevi.guidInstance))
1181 if (device_count++)
1182 didevis = HeapReAlloc(GetProcessHeap(), 0, didevis, sizeof(DIDEVICEINSTANCEW)*device_count);
1183 else
1184 didevis = HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEW)*device_count);
1185 didevis[device_count-1] = didevi;
1190 remain = device_count;
1191 /* Add keyboard and mouse to remaining device count */
1192 if (!(dwFlags & DIEDBSFL_FORCEFEEDBACK))
1194 for (i = 0; i < ARRAY_SIZE(guids); i++)
1196 if (should_enumerate_device(ptszUserName, dwFlags, &This->device_players, guids[i]))
1197 remain++;
1201 for (i = 0; i < device_count; i++)
1203 callbackFlags = diactionformat_priorityW(lpdiActionFormat, lpdiActionFormat->dwGenre);
1204 IDirectInput_CreateDevice(iface, &didevis[i].guidInstance, &lpdid, NULL);
1206 if (lpCallback(&didevis[i], lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1208 HeapFree(GetProcessHeap(), 0, didevis);
1209 return DI_OK;
1213 HeapFree(GetProcessHeap(), 0, didevis);
1215 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
1217 /* Enumerate keyboard and mouse */
1218 for (i = 0; i < ARRAY_SIZE(guids); i++)
1220 if (should_enumerate_device(ptszUserName, dwFlags, &This->device_players, guids[i]))
1222 callbackFlags = diactionformat_priorityW(lpdiActionFormat, actionMasks[i]);
1224 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1225 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1227 if (lpCallback(&didevi, lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1228 return DI_OK;
1232 return DI_OK;
1235 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
1236 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1237 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1240 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1242 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1244 /* Call helper function in config.c to do the real work */
1245 return _configure_devices(iface, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1248 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
1249 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1250 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1253 IDirectInputImpl *This = impl_from_IDirectInput8A(iface);
1254 DIACTIONFORMATW diafW;
1255 DICONFIGUREDEVICESPARAMSW diCDParamsW;
1256 HRESULT hr;
1257 int i;
1259 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1261 /* Copy parameters */
1262 diCDParamsW.dwSize = sizeof(DICONFIGUREDEVICESPARAMSW);
1263 diCDParamsW.dwcFormats = lpdiCDParams->dwcFormats;
1264 diCDParamsW.lprgFormats = &diafW;
1265 diCDParamsW.hwnd = lpdiCDParams->hwnd;
1267 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiCDParams->lprgFormats->dwNumActions);
1268 _copy_diactionformatAtoW(&diafW, lpdiCDParams->lprgFormats);
1270 /* Copy action names */
1271 for (i=0; i < diafW.dwNumActions; i++)
1273 const char* from = lpdiCDParams->lprgFormats->rgoAction[i].u.lptszActionName;
1274 int len = MultiByteToWideChar(CP_ACP, 0, from , -1, NULL , 0);
1275 WCHAR *to = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1277 MultiByteToWideChar(CP_ACP, 0, from , -1, to , len);
1278 diafW.rgoAction[i].u.lptszActionName = to;
1281 hr = IDirectInput8WImpl_ConfigureDevices(&This->IDirectInput8W_iface, lpdiCallback, &diCDParamsW, dwFlags, pvRefData);
1283 /* Copy back configuration */
1284 if (SUCCEEDED(hr))
1285 _copy_diactionformatWtoA(lpdiCDParams->lprgFormats, &diafW);
1287 /* Free memory */
1288 for (i=0; i < diafW.dwNumActions; i++)
1289 HeapFree(GetProcessHeap(), 0, (void*) diafW.rgoAction[i].u.lptszActionName);
1291 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1293 return hr;
1296 /*****************************************************************************
1297 * IDirectInputJoyConfig8 interface
1300 static inline IDirectInputImpl *impl_from_IDirectInputJoyConfig8(IDirectInputJoyConfig8 *iface)
1302 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInputJoyConfig8_iface );
1305 static HRESULT WINAPI JoyConfig8Impl_QueryInterface(IDirectInputJoyConfig8 *iface, REFIID riid, void** ppobj)
1307 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1308 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
1311 static ULONG WINAPI JoyConfig8Impl_AddRef(IDirectInputJoyConfig8 *iface)
1313 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1314 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
1317 static ULONG WINAPI JoyConfig8Impl_Release(IDirectInputJoyConfig8 *iface)
1319 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1320 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
1323 static HRESULT WINAPI JoyConfig8Impl_Acquire(IDirectInputJoyConfig8 *iface)
1325 FIXME( "(%p): stub!\n", iface );
1326 return E_NOTIMPL;
1329 static HRESULT WINAPI JoyConfig8Impl_Unacquire(IDirectInputJoyConfig8 *iface)
1331 FIXME( "(%p): stub!\n", iface );
1332 return E_NOTIMPL;
1335 static HRESULT WINAPI JoyConfig8Impl_SetCooperativeLevel(IDirectInputJoyConfig8 *iface, HWND hwnd, DWORD flags)
1337 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, hwnd, flags );
1338 return E_NOTIMPL;
1341 static HRESULT WINAPI JoyConfig8Impl_SendNotify(IDirectInputJoyConfig8 *iface)
1343 FIXME( "(%p): stub!\n", iface );
1344 return E_NOTIMPL;
1347 static HRESULT WINAPI JoyConfig8Impl_EnumTypes(IDirectInputJoyConfig8 *iface, LPDIJOYTYPECALLBACK cb, void *ref)
1349 FIXME( "(%p)->(%p, %p): stub!\n", iface, cb, ref );
1350 return E_NOTIMPL;
1353 static HRESULT WINAPI JoyConfig8Impl_GetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPDIJOYTYPEINFO info, DWORD flags)
1355 FIXME( "(%p)->(%s, %p, 0x%08x): stub!\n", iface, debugstr_w(name), info, flags );
1356 return E_NOTIMPL;
1359 static HRESULT WINAPI JoyConfig8Impl_SetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPCDIJOYTYPEINFO info, DWORD flags,
1360 LPWSTR new_name)
1362 FIXME( "(%p)->(%s, %p, 0x%08x, %s): stub!\n", iface, debugstr_w(name), info, flags, debugstr_w(new_name) );
1363 return E_NOTIMPL;
1366 static HRESULT WINAPI JoyConfig8Impl_DeleteType(IDirectInputJoyConfig8 *iface, LPCWSTR name)
1368 FIXME( "(%p)->(%s): stub!\n", iface, debugstr_w(name) );
1369 return E_NOTIMPL;
1372 static HRESULT WINAPI JoyConfig8Impl_GetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPDIJOYCONFIG info, DWORD flags)
1374 IDirectInputImpl *di = impl_from_IDirectInputJoyConfig8(iface);
1375 UINT found = 0;
1376 int i, j;
1377 HRESULT r;
1379 FIXME("(%p)->(%d, %p, 0x%08x): semi-stub!\n", iface, id, info, flags);
1381 #define X(x) if (flags & x) FIXME("\tflags |= "#x"\n");
1382 X(DIJC_GUIDINSTANCE)
1383 X(DIJC_REGHWCONFIGTYPE)
1384 X(DIJC_GAIN)
1385 X(DIJC_CALLOUT)
1386 #undef X
1388 /* Enumerate all joysticks in order */
1389 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
1391 if (!dinput_devices[i]->enum_deviceA) continue;
1393 for (j = 0, r = S_OK; SUCCEEDED(r); j++)
1395 DIDEVICEINSTANCEA dev;
1396 dev.dwSize = sizeof(dev);
1397 if ((r = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, 0, &dev, di->dwVersion, j)) == S_OK)
1399 /* Only take into account the chosen id */
1400 if (found == id)
1402 if (flags & DIJC_GUIDINSTANCE)
1403 info->guidInstance = dev.guidInstance;
1405 return DI_OK;
1407 found += 1;
1412 return DIERR_NOMOREITEMS;
1415 static HRESULT WINAPI JoyConfig8Impl_SetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPCDIJOYCONFIG info, DWORD flags)
1417 FIXME( "(%p)->(%d, %p, 0x%08x): stub!\n", iface, id, info, flags );
1418 return E_NOTIMPL;
1421 static HRESULT WINAPI JoyConfig8Impl_DeleteConfig(IDirectInputJoyConfig8 *iface, UINT id)
1423 FIXME( "(%p)->(%d): stub!\n", iface, id );
1424 return E_NOTIMPL;
1427 static HRESULT WINAPI JoyConfig8Impl_GetUserValues(IDirectInputJoyConfig8 *iface, LPDIJOYUSERVALUES info, DWORD flags)
1429 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1430 return E_NOTIMPL;
1433 static HRESULT WINAPI JoyConfig8Impl_SetUserValues(IDirectInputJoyConfig8 *iface, LPCDIJOYUSERVALUES info, DWORD flags)
1435 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1436 return E_NOTIMPL;
1439 static HRESULT WINAPI JoyConfig8Impl_AddNewHardware(IDirectInputJoyConfig8 *iface, HWND hwnd, REFGUID guid)
1441 FIXME( "(%p)->(%p, %s): stub!\n", iface, hwnd, debugstr_guid(guid) );
1442 return E_NOTIMPL;
1445 static HRESULT WINAPI JoyConfig8Impl_OpenTypeKey(IDirectInputJoyConfig8 *iface, LPCWSTR name, DWORD security, PHKEY key)
1447 FIXME( "(%p)->(%s, 0x%08x, %p): stub!\n", iface, debugstr_w(name), security, key );
1448 return E_NOTIMPL;
1451 static HRESULT WINAPI JoyConfig8Impl_OpenAppStatusKey(IDirectInputJoyConfig8 *iface, PHKEY key)
1453 FIXME( "(%p)->(%p): stub!\n", iface, key );
1454 return E_NOTIMPL;
1457 static const IDirectInput7AVtbl ddi7avt = {
1458 IDirectInputAImpl_QueryInterface,
1459 IDirectInputAImpl_AddRef,
1460 IDirectInputAImpl_Release,
1461 IDirectInputAImpl_CreateDevice,
1462 IDirectInputAImpl_EnumDevices,
1463 IDirectInputAImpl_GetDeviceStatus,
1464 IDirectInputAImpl_RunControlPanel,
1465 IDirectInputAImpl_Initialize,
1466 IDirectInput2AImpl_FindDevice,
1467 IDirectInput7AImpl_CreateDeviceEx
1470 static const IDirectInput7WVtbl ddi7wvt = {
1471 IDirectInputWImpl_QueryInterface,
1472 IDirectInputWImpl_AddRef,
1473 IDirectInputWImpl_Release,
1474 IDirectInputWImpl_CreateDevice,
1475 IDirectInputWImpl_EnumDevices,
1476 IDirectInputWImpl_GetDeviceStatus,
1477 IDirectInputWImpl_RunControlPanel,
1478 IDirectInputWImpl_Initialize,
1479 IDirectInput2WImpl_FindDevice,
1480 IDirectInput7WImpl_CreateDeviceEx
1483 static const IDirectInput8AVtbl ddi8avt = {
1484 IDirectInput8AImpl_QueryInterface,
1485 IDirectInput8AImpl_AddRef,
1486 IDirectInput8AImpl_Release,
1487 IDirectInput8AImpl_CreateDevice,
1488 IDirectInput8AImpl_EnumDevices,
1489 IDirectInput8AImpl_GetDeviceStatus,
1490 IDirectInput8AImpl_RunControlPanel,
1491 IDirectInput8AImpl_Initialize,
1492 IDirectInput8AImpl_FindDevice,
1493 IDirectInput8AImpl_EnumDevicesBySemantics,
1494 IDirectInput8AImpl_ConfigureDevices
1497 static const IDirectInput8WVtbl ddi8wvt = {
1498 IDirectInput8WImpl_QueryInterface,
1499 IDirectInput8WImpl_AddRef,
1500 IDirectInput8WImpl_Release,
1501 IDirectInput8WImpl_CreateDevice,
1502 IDirectInput8WImpl_EnumDevices,
1503 IDirectInput8WImpl_GetDeviceStatus,
1504 IDirectInput8WImpl_RunControlPanel,
1505 IDirectInput8WImpl_Initialize,
1506 IDirectInput8WImpl_FindDevice,
1507 IDirectInput8WImpl_EnumDevicesBySemantics,
1508 IDirectInput8WImpl_ConfigureDevices
1511 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt =
1513 JoyConfig8Impl_QueryInterface,
1514 JoyConfig8Impl_AddRef,
1515 JoyConfig8Impl_Release,
1516 JoyConfig8Impl_Acquire,
1517 JoyConfig8Impl_Unacquire,
1518 JoyConfig8Impl_SetCooperativeLevel,
1519 JoyConfig8Impl_SendNotify,
1520 JoyConfig8Impl_EnumTypes,
1521 JoyConfig8Impl_GetTypeInfo,
1522 JoyConfig8Impl_SetTypeInfo,
1523 JoyConfig8Impl_DeleteType,
1524 JoyConfig8Impl_GetConfig,
1525 JoyConfig8Impl_SetConfig,
1526 JoyConfig8Impl_DeleteConfig,
1527 JoyConfig8Impl_GetUserValues,
1528 JoyConfig8Impl_SetUserValues,
1529 JoyConfig8Impl_AddNewHardware,
1530 JoyConfig8Impl_OpenTypeKey,
1531 JoyConfig8Impl_OpenAppStatusKey
1534 /*******************************************************************************
1535 * DirectInput ClassFactory
1537 typedef struct
1539 /* IUnknown fields */
1540 IClassFactory IClassFactory_iface;
1541 LONG ref;
1542 } IClassFactoryImpl;
1544 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1546 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1549 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1550 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1552 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1553 return E_NOINTERFACE;
1556 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
1557 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1558 return InterlockedIncrement(&(This->ref));
1561 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
1562 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1563 /* static class, won't be freed */
1564 return InterlockedDecrement(&(This->ref));
1567 static HRESULT WINAPI DICF_CreateInstance(
1568 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1570 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1572 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1573 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
1574 IsEqualGUID( &IID_IDirectInputA, riid ) ||
1575 IsEqualGUID( &IID_IDirectInputW, riid ) ||
1576 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
1577 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
1578 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
1579 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
1580 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
1581 IsEqualGUID( &IID_IDirectInput8W, riid ) )
1583 return create_directinput_instance(riid, ppobj, NULL);
1586 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1587 return E_NOINTERFACE;
1590 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1591 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1592 FIXME("(%p)->(%d),stub!\n",This,dolock);
1593 return S_OK;
1596 static const IClassFactoryVtbl DICF_Vtbl = {
1597 DICF_QueryInterface,
1598 DICF_AddRef,
1599 DICF_Release,
1600 DICF_CreateInstance,
1601 DICF_LockServer
1603 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
1605 /***********************************************************************
1606 * DllCanUnloadNow (DINPUT.@)
1608 HRESULT WINAPI DllCanUnloadNow(void)
1610 return S_FALSE;
1613 /***********************************************************************
1614 * DllGetClassObject (DINPUT.@)
1616 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1618 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1619 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1620 *ppv = &DINPUT_CF;
1621 IClassFactory_AddRef((IClassFactory*)*ppv);
1622 return S_OK;
1625 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1626 return CLASS_E_CLASSNOTAVAILABLE;
1629 /***********************************************************************
1630 * DllRegisterServer (DINPUT.@)
1632 HRESULT WINAPI DllRegisterServer(void)
1634 return __wine_register_resources( DINPUT_instance );
1637 /***********************************************************************
1638 * DllUnregisterServer (DINPUT.@)
1640 HRESULT WINAPI DllUnregisterServer(void)
1642 return __wine_unregister_resources( DINPUT_instance );
1645 /******************************************************************************
1646 * DInput hook thread
1649 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
1651 IDirectInputImpl *dinput;
1652 int skip = 0;
1654 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
1656 EnterCriticalSection( &dinput_hook_crit );
1657 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1659 IDirectInputDeviceImpl *dev;
1661 EnterCriticalSection( &dinput->crit );
1662 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1663 if (dev->acquired && dev->event_proc)
1665 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
1666 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1668 LeaveCriticalSection( &dinput->crit );
1670 LeaveCriticalSection( &dinput_hook_crit );
1672 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
1675 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
1677 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
1678 IDirectInputImpl *dinput;
1679 HWND foreground;
1681 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1682 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1683 return CallNextHookEx( 0, code, wparam, lparam );
1685 foreground = GetForegroundWindow();
1687 EnterCriticalSection( &dinput_hook_crit );
1689 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1691 IDirectInputDeviceImpl *dev;
1693 EnterCriticalSection( &dinput->crit );
1694 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1696 if (!dev->acquired) continue;
1698 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1700 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1701 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1704 LeaveCriticalSection( &dinput->crit );
1706 LeaveCriticalSection( &dinput_hook_crit );
1708 return CallNextHookEx( 0, code, wparam, lparam );
1711 static DWORD WINAPI hook_thread_proc(void *param)
1713 static HHOOK kbd_hook, mouse_hook;
1714 MSG msg;
1716 /* Force creation of the message queue */
1717 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1718 SetEvent(*(LPHANDLE)param);
1720 while (GetMessageW( &msg, 0, 0, 0 ))
1722 UINT kbd_cnt = 0, mice_cnt = 0;
1724 if (msg.message == WM_USER+0x10)
1726 IDirectInputImpl *dinput;
1728 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
1730 if (!msg.wParam && !msg.lParam)
1732 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1733 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1734 kbd_hook = mouse_hook = NULL;
1735 break;
1738 EnterCriticalSection( &dinput_hook_crit );
1740 /* Count acquired keyboards and mice*/
1741 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1743 IDirectInputDeviceImpl *dev;
1745 EnterCriticalSection( &dinput->crit );
1746 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1748 if (!dev->acquired || !dev->event_proc) continue;
1750 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ))
1751 kbd_cnt++;
1752 else
1753 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ))
1754 mice_cnt++;
1756 LeaveCriticalSection( &dinput->crit );
1758 LeaveCriticalSection( &dinput_hook_crit );
1760 if (kbd_cnt && !kbd_hook)
1761 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1762 else if (!kbd_cnt && kbd_hook)
1764 UnhookWindowsHookEx( kbd_hook );
1765 kbd_hook = NULL;
1768 if (mice_cnt && !mouse_hook)
1769 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1770 else if (!mice_cnt && mouse_hook)
1772 UnhookWindowsHookEx( mouse_hook );
1773 mouse_hook = NULL;
1776 TranslateMessage(&msg);
1777 DispatchMessageW(&msg);
1780 return 0;
1783 static DWORD hook_thread_id;
1785 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1787 0, 0, &dinput_hook_crit,
1788 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1789 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1791 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1793 static BOOL check_hook_thread(void)
1795 static HANDLE hook_thread;
1797 EnterCriticalSection(&dinput_hook_crit);
1799 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1800 if (!list_empty(&direct_input_list) && !hook_thread)
1802 HANDLE event;
1804 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1805 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1806 if (event && hook_thread)
1808 HANDLE handles[2];
1809 handles[0] = event;
1810 handles[1] = hook_thread;
1811 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1813 LeaveCriticalSection(&dinput_hook_crit);
1814 CloseHandle(event);
1816 else if (list_empty(&direct_input_list) && hook_thread)
1818 DWORD tid = hook_thread_id;
1820 hook_thread_id = 0;
1821 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1822 LeaveCriticalSection(&dinput_hook_crit);
1824 /* wait for hook thread to exit */
1825 WaitForSingleObject(hook_thread, INFINITE);
1826 CloseHandle(hook_thread);
1827 hook_thread = NULL;
1829 else
1830 LeaveCriticalSection(&dinput_hook_crit);
1832 return hook_thread_id != 0;
1835 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1837 static HHOOK callwndproc_hook;
1838 static ULONG foreground_cnt;
1839 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1841 EnterCriticalSection(&dinput_hook_crit);
1843 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1845 if (dev->acquired)
1846 foreground_cnt++;
1847 else
1848 foreground_cnt--;
1851 if (foreground_cnt && !callwndproc_hook)
1852 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1853 DINPUT_instance, GetCurrentThreadId() );
1854 else if (!foreground_cnt && callwndproc_hook)
1856 UnhookWindowsHookEx( callwndproc_hook );
1857 callwndproc_hook = NULL;
1860 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1862 LeaveCriticalSection(&dinput_hook_crit);
1865 void check_dinput_events(void)
1867 /* Windows does not do that, but our current implementation of winex11
1868 * requires periodic event polling to forward events to the wineserver.
1870 * We have to call this function from multiple places, because:
1871 * - some games do not explicitly poll for mouse events
1872 * (for example Culpa Innata)
1873 * - some games only poll the device, and neither keyboard nor mouse
1874 * (for example Civilization: Call to Power 2)
1876 MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
1879 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved)
1881 switch(reason)
1883 case DLL_PROCESS_ATTACH:
1884 DisableThreadLibraryCalls(inst);
1885 DINPUT_instance = inst;
1886 break;
1887 case DLL_PROCESS_DETACH:
1888 if (reserved) break;
1889 DeleteCriticalSection(&dinput_hook_crit);
1890 break;
1892 return TRUE;