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
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
39 #define NONAMELESSUNION
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
50 #include "dinput_private.h"
51 #include "device_private.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
[] =
91 &joystick_linuxinput_device
,
92 &joystick_linux_device
,
95 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
97 static HINSTANCE DINPUT_instance
= NULL
;
99 static BOOL
check_hook_thread(void);
100 static CRITICAL_SECTION dinput_hook_crit
;
101 static struct list direct_input_list
= LIST_INIT( direct_input_list
);
103 static HRESULT
initialize_directinput_instance(IDirectInputImpl
*This
, DWORD dwVersion
);
104 static void uninitialize_directinput_instance(IDirectInputImpl
*This
);
106 static HRESULT
create_directinput_instance(REFIID riid
, LPVOID
*ppDI
, IDirectInputImpl
**out
)
108 IDirectInputImpl
*This
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirectInputImpl
) );
112 return E_OUTOFMEMORY
;
114 This
->IDirectInput7A_iface
.lpVtbl
= &ddi7avt
;
115 This
->IDirectInput7W_iface
.lpVtbl
= &ddi7wvt
;
116 This
->IDirectInput8A_iface
.lpVtbl
= &ddi8avt
;
117 This
->IDirectInput8W_iface
.lpVtbl
= &ddi8wvt
;
118 This
->IDirectInputJoyConfig8_iface
.lpVtbl
= &JoyConfig8vt
;
120 hr
= IDirectInput_QueryInterface( &This
->IDirectInput7A_iface
, riid
, ppDI
);
123 HeapFree( GetProcessHeap(), 0, This
);
127 if (out
) *out
= This
;
131 /******************************************************************************
132 * DirectInputCreateEx (DINPUT.@)
134 HRESULT WINAPI
DirectInputCreateEx(
135 HINSTANCE hinst
, DWORD dwVersion
, REFIID riid
, LPVOID
*ppDI
,
138 IDirectInputImpl
*This
;
141 TRACE("(%p,%04x,%s,%p,%p)\n", hinst
, dwVersion
, debugstr_guid(riid
), ppDI
, punkOuter
);
143 if (IsEqualGUID( &IID_IDirectInputA
, riid
) ||
144 IsEqualGUID( &IID_IDirectInput2A
, riid
) ||
145 IsEqualGUID( &IID_IDirectInput7A
, riid
) ||
146 IsEqualGUID( &IID_IDirectInputW
, riid
) ||
147 IsEqualGUID( &IID_IDirectInput2W
, riid
) ||
148 IsEqualGUID( &IID_IDirectInput7W
, riid
))
150 hr
= create_directinput_instance(riid
, ppDI
, &This
);
155 return DIERR_NOINTERFACE
;
157 hr
= IDirectInput_Initialize( &This
->IDirectInput7A_iface
, hinst
, dwVersion
);
160 IDirectInput_Release( &This
->IDirectInput7A_iface
);
168 /******************************************************************************
169 * DirectInputCreateA (DINPUT.@)
171 HRESULT WINAPI DECLSPEC_HOTPATCH
DirectInputCreateA(HINSTANCE hinst
, DWORD dwVersion
, LPDIRECTINPUTA
*ppDI
, LPUNKNOWN punkOuter
)
173 return DirectInputCreateEx(hinst
, dwVersion
, &IID_IDirectInput7A
, (LPVOID
*)ppDI
, punkOuter
);
176 /******************************************************************************
177 * DirectInputCreateW (DINPUT.@)
179 HRESULT WINAPI DECLSPEC_HOTPATCH
DirectInputCreateW(HINSTANCE hinst
, DWORD dwVersion
, LPDIRECTINPUTW
*ppDI
, LPUNKNOWN punkOuter
)
181 return DirectInputCreateEx(hinst
, dwVersion
, &IID_IDirectInput7W
, (LPVOID
*)ppDI
, punkOuter
);
184 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType
, DWORD dwVersion
)
186 if (dwVersion
< 0x0800) {
188 case 0: return "All devices";
189 case DIDEVTYPE_MOUSE
: return "DIDEVTYPE_MOUSE";
190 case DIDEVTYPE_KEYBOARD
: return "DIDEVTYPE_KEYBOARD";
191 case DIDEVTYPE_JOYSTICK
: return "DIDEVTYPE_JOYSTICK";
192 case DIDEVTYPE_DEVICE
: return "DIDEVTYPE_DEVICE";
193 default: return "Unknown";
197 case DI8DEVCLASS_ALL
: return "All devices";
198 case DI8DEVCLASS_POINTER
: return "DI8DEVCLASS_POINTER";
199 case DI8DEVCLASS_KEYBOARD
: return "DI8DEVCLASS_KEYBOARD";
200 case DI8DEVCLASS_DEVICE
: return "DI8DEVCLASS_DEVICE";
201 case DI8DEVCLASS_GAMECTRL
: return "DI8DEVCLASS_GAMECTRL";
202 default: return "Unknown";
207 static void _dump_EnumDevices_dwFlags(DWORD dwFlags
)
209 if (TRACE_ON(dinput
)) {
211 static const struct {
215 #define FE(x) { x, #x}
216 FE(DIEDFL_ALLDEVICES
),
217 FE(DIEDFL_ATTACHEDONLY
),
218 FE(DIEDFL_FORCEFEEDBACK
),
219 FE(DIEDFL_INCLUDEALIASES
),
220 FE(DIEDFL_INCLUDEPHANTOMS
),
221 FE(DIEDFL_INCLUDEHIDDEN
)
226 TRACE("DIEDFL_ALLDEVICES\n");
229 for (i
= 0; i
< (sizeof(flags
) / sizeof(flags
[0])); i
++)
230 if (flags
[i
].mask
& dwFlags
)
231 TRACE("%s ",flags
[i
].name
);
236 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat
)
240 TRACE("diaf.dwSize = %d\n", lpdiActionFormat
->dwSize
);
241 TRACE("diaf.dwActionSize = %d\n", lpdiActionFormat
->dwActionSize
);
242 TRACE("diaf.dwDataSize = %d\n", lpdiActionFormat
->dwDataSize
);
243 TRACE("diaf.dwNumActions = %d\n", lpdiActionFormat
->dwNumActions
);
244 TRACE("diaf.rgoAction = %p\n", lpdiActionFormat
->rgoAction
);
245 TRACE("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat
->guidActionMap
));
246 TRACE("diaf.dwGenre = 0x%08x\n", lpdiActionFormat
->dwGenre
);
247 TRACE("diaf.dwBufferSize = %d\n", lpdiActionFormat
->dwBufferSize
);
248 TRACE("diaf.lAxisMin = %d\n", lpdiActionFormat
->lAxisMin
);
249 TRACE("diaf.lAxisMax = %d\n", lpdiActionFormat
->lAxisMax
);
250 TRACE("diaf.hInstString = %p\n", lpdiActionFormat
->hInstString
);
251 TRACE("diaf.ftTimeStamp ...\n");
252 TRACE("diaf.dwCRC = 0x%x\n", lpdiActionFormat
->dwCRC
);
253 TRACE("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat
->tszActionMap
));
254 for (i
= 0; i
< lpdiActionFormat
->dwNumActions
; i
++)
256 TRACE("diaf.rgoAction[%u]:\n", i
);
257 TRACE("\tuAppData=0x%lx\n", lpdiActionFormat
->rgoAction
[i
].uAppData
);
258 TRACE("\tdwSemantic=0x%08x\n", lpdiActionFormat
->rgoAction
[i
].dwSemantic
);
259 TRACE("\tdwFlags=0x%x\n", lpdiActionFormat
->rgoAction
[i
].dwFlags
);
260 TRACE("\tszActionName=%s\n", debugstr_a(lpdiActionFormat
->rgoAction
[i
].u
.lptszActionName
));
261 TRACE("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat
->rgoAction
[i
].guidInstance
));
262 TRACE("\tdwObjID=0x%x\n", lpdiActionFormat
->rgoAction
[i
].dwObjID
);
263 TRACE("\tdwHow=0x%x\n", lpdiActionFormat
->rgoAction
[i
].dwHow
);
267 void _copy_diactionformatAtoW(LPDIACTIONFORMATW to
, LPDIACTIONFORMATA from
)
271 to
->dwSize
= sizeof(DIACTIONFORMATW
);
272 to
->dwActionSize
= sizeof(DIACTIONW
);
273 to
->dwDataSize
= from
->dwDataSize
;
274 to
->dwNumActions
= from
->dwNumActions
;
275 to
->guidActionMap
= from
->guidActionMap
;
276 to
->dwGenre
= from
->dwGenre
;
277 to
->dwBufferSize
= from
->dwBufferSize
;
278 to
->lAxisMin
= from
->lAxisMin
;
279 to
->lAxisMax
= from
->lAxisMax
;
280 to
->dwCRC
= from
->dwCRC
;
281 to
->ftTimeStamp
= from
->ftTimeStamp
;
283 for (i
=0; i
< to
->dwNumActions
; i
++)
285 to
->rgoAction
[i
].uAppData
= from
->rgoAction
[i
].uAppData
;
286 to
->rgoAction
[i
].dwSemantic
= from
->rgoAction
[i
].dwSemantic
;
287 to
->rgoAction
[i
].dwFlags
= from
->rgoAction
[i
].dwFlags
;
288 to
->rgoAction
[i
].guidInstance
= from
->rgoAction
[i
].guidInstance
;
289 to
->rgoAction
[i
].dwObjID
= from
->rgoAction
[i
].dwObjID
;
290 to
->rgoAction
[i
].dwHow
= from
->rgoAction
[i
].dwHow
;
294 void _copy_diactionformatWtoA(LPDIACTIONFORMATA to
, LPDIACTIONFORMATW from
)
298 to
->dwSize
= sizeof(DIACTIONFORMATA
);
299 to
->dwActionSize
= sizeof(DIACTIONA
);
300 to
->dwDataSize
= from
->dwDataSize
;
301 to
->dwNumActions
= from
->dwNumActions
;
302 to
->guidActionMap
= from
->guidActionMap
;
303 to
->dwGenre
= from
->dwGenre
;
304 to
->dwBufferSize
= from
->dwBufferSize
;
305 to
->lAxisMin
= from
->lAxisMin
;
306 to
->lAxisMax
= from
->lAxisMax
;
307 to
->dwCRC
= from
->dwCRC
;
308 to
->ftTimeStamp
= from
->ftTimeStamp
;
310 for (i
=0; i
< to
->dwNumActions
; i
++)
312 to
->rgoAction
[i
].uAppData
= from
->rgoAction
[i
].uAppData
;
313 to
->rgoAction
[i
].dwSemantic
= from
->rgoAction
[i
].dwSemantic
;
314 to
->rgoAction
[i
].dwFlags
= from
->rgoAction
[i
].dwFlags
;
315 to
->rgoAction
[i
].guidInstance
= from
->rgoAction
[i
].guidInstance
;
316 to
->rgoAction
[i
].dwObjID
= from
->rgoAction
[i
].dwObjID
;
317 to
->rgoAction
[i
].dwHow
= from
->rgoAction
[i
].dwHow
;
321 /* diactionformat_priority
323 * Given a DIACTIONFORMAT structure and a DI genre, returns the enumeration
324 * priority. Joysticks should pass the game genre, and mouse or keyboard their
325 * respective DI*_MASK
327 static DWORD
diactionformat_priorityA(LPDIACTIONFORMATA lpdiaf
, DWORD genre
)
330 DWORD priorityFlags
= 0;
332 /* If there's at least one action for the device it's priority 1 */
333 for(i
=0; i
< lpdiaf
->dwNumActions
; i
++)
334 if ((lpdiaf
->rgoAction
[i
].dwSemantic
& genre
) == genre
)
335 priorityFlags
|= DIEDBS_MAPPEDPRI1
;
337 return priorityFlags
;
340 static DWORD
diactionformat_priorityW(LPDIACTIONFORMATW lpdiaf
, DWORD genre
)
343 DWORD priorityFlags
= 0;
345 /* If there's at least one action for the device it's priority 1 */
346 for(i
=0; i
< lpdiaf
->dwNumActions
; i
++)
347 if ((lpdiaf
->rgoAction
[i
].dwSemantic
& genre
) == genre
)
348 priorityFlags
|= DIEDBS_MAPPEDPRI1
;
350 return priorityFlags
;
353 #if defined __i386__ && defined _MSC_VER
354 __declspec(naked
) BOOL
enum_callback_wrapper(void *callback
, const void *instance
, void *ref
)
367 #elif defined __i386__ && defined __GNUC__
368 extern BOOL
enum_callback_wrapper(void *callback
, const void *instance
, void *ref
);
369 __ASM_GLOBAL_FUNC( enum_callback_wrapper
,
371 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
372 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
374 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
379 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
380 __ASM_CFI(".cfi_same_value %ebp\n\t")
383 #define enum_callback_wrapper(callback, instance, ref) (callback)((instance), (ref))
386 /******************************************************************************
387 * IDirectInputA_EnumDevices
389 static HRESULT WINAPI
IDirectInputAImpl_EnumDevices(
390 LPDIRECTINPUT7A iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKA lpCallback
,
391 LPVOID pvRef
, DWORD dwFlags
)
393 IDirectInputImpl
*This
= impl_from_IDirectInput7A(iface
);
394 DIDEVICEINSTANCEA devInstance
;
399 TRACE("(this=%p,0x%04x '%s',%p,%p,0x%04x)\n",
400 This
, dwDevType
, _dump_DIDEVTYPE_value(dwDevType
, This
->dwVersion
),
401 lpCallback
, pvRef
, dwFlags
);
402 _dump_EnumDevices_dwFlags(dwFlags
);
405 dwFlags
& ~(DIEDFL_ATTACHEDONLY
| DIEDFL_FORCEFEEDBACK
| DIEDFL_INCLUDEALIASES
| DIEDFL_INCLUDEPHANTOMS
| DIEDFL_INCLUDEHIDDEN
) ||
406 (dwDevType
> DI8DEVCLASS_GAMECTRL
&& dwDevType
< DI8DEVTYPE_DEVICE
) || dwDevType
> DI8DEVTYPE_SUPPLEMENTAL
)
407 return DIERR_INVALIDPARAM
;
409 if (!This
->initialized
)
410 return DIERR_NOTINITIALIZED
;
412 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++) {
413 if (!dinput_devices
[i
]->enum_deviceA
) continue;
414 for (j
= 0, r
= S_OK
; SUCCEEDED(r
); j
++) {
415 devInstance
.dwSize
= sizeof(devInstance
);
416 TRACE(" - checking device %u ('%s')\n", i
, dinput_devices
[i
]->name
);
417 r
= dinput_devices
[i
]->enum_deviceA(dwDevType
, dwFlags
, &devInstance
, This
->dwVersion
, j
);
419 if (enum_callback_wrapper(lpCallback
, &devInstance
, pvRef
) == DIENUM_STOP
)
426 /******************************************************************************
427 * IDirectInputW_EnumDevices
429 static HRESULT WINAPI
IDirectInputWImpl_EnumDevices(
430 LPDIRECTINPUT7W iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKW lpCallback
,
431 LPVOID pvRef
, DWORD dwFlags
)
433 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
434 DIDEVICEINSTANCEW devInstance
;
439 TRACE("(this=%p,0x%04x '%s',%p,%p,0x%04x)\n",
440 This
, dwDevType
, _dump_DIDEVTYPE_value(dwDevType
, This
->dwVersion
),
441 lpCallback
, pvRef
, dwFlags
);
442 _dump_EnumDevices_dwFlags(dwFlags
);
445 dwFlags
& ~(DIEDFL_ATTACHEDONLY
| DIEDFL_FORCEFEEDBACK
| DIEDFL_INCLUDEALIASES
| DIEDFL_INCLUDEPHANTOMS
| DIEDFL_INCLUDEHIDDEN
) ||
446 (dwDevType
> DI8DEVCLASS_GAMECTRL
&& dwDevType
< DI8DEVTYPE_DEVICE
) || dwDevType
> DI8DEVTYPE_SUPPLEMENTAL
)
447 return DIERR_INVALIDPARAM
;
449 if (!This
->initialized
)
450 return DIERR_NOTINITIALIZED
;
452 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++) {
453 if (!dinput_devices
[i
]->enum_deviceW
) continue;
454 for (j
= 0, r
= S_OK
; SUCCEEDED(r
); j
++) {
455 devInstance
.dwSize
= sizeof(devInstance
);
456 TRACE(" - checking device %u ('%s')\n", i
, dinput_devices
[i
]->name
);
457 r
= dinput_devices
[i
]->enum_deviceW(dwDevType
, dwFlags
, &devInstance
, This
->dwVersion
, j
);
459 if (enum_callback_wrapper(lpCallback
, &devInstance
, pvRef
) == DIENUM_STOP
)
467 static ULONG WINAPI
IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface
)
469 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
470 ULONG ref
= InterlockedIncrement(&This
->ref
);
472 TRACE( "(%p) incrementing from %d\n", This
, ref
- 1);
476 static ULONG WINAPI
IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface
)
478 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
479 return IDirectInputAImpl_AddRef( &This
->IDirectInput7A_iface
);
482 static ULONG WINAPI
IDirectInputAImpl_Release(LPDIRECTINPUT7A iface
)
484 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
485 ULONG ref
= InterlockedDecrement( &This
->ref
);
487 TRACE( "(%p) releasing from %d\n", This
, ref
+ 1 );
491 uninitialize_directinput_instance( This
);
492 HeapFree( GetProcessHeap(), 0, This
);
498 static ULONG WINAPI
IDirectInputWImpl_Release(LPDIRECTINPUT7W iface
)
500 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
501 return IDirectInputAImpl_Release( &This
->IDirectInput7A_iface
);
504 static HRESULT WINAPI
IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface
, REFIID riid
, LPVOID
*ppobj
)
506 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
508 TRACE( "(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), ppobj
);
513 if (IsEqualGUID( &IID_IUnknown
, riid
) ||
514 IsEqualGUID( &IID_IDirectInputA
, riid
) ||
515 IsEqualGUID( &IID_IDirectInput2A
, riid
) ||
516 IsEqualGUID( &IID_IDirectInput7A
, riid
))
518 *ppobj
= &This
->IDirectInput7A_iface
;
519 IUnknown_AddRef( (IUnknown
*)*ppobj
);
524 if (IsEqualGUID( &IID_IDirectInputW
, riid
) ||
525 IsEqualGUID( &IID_IDirectInput2W
, riid
) ||
526 IsEqualGUID( &IID_IDirectInput7W
, riid
))
528 *ppobj
= &This
->IDirectInput7W_iface
;
529 IUnknown_AddRef( (IUnknown
*)*ppobj
);
534 if (IsEqualGUID( &IID_IDirectInput8A
, riid
))
536 *ppobj
= &This
->IDirectInput8A_iface
;
537 IUnknown_AddRef( (IUnknown
*)*ppobj
);
542 if (IsEqualGUID( &IID_IDirectInput8W
, riid
))
544 *ppobj
= &This
->IDirectInput8W_iface
;
545 IUnknown_AddRef( (IUnknown
*)*ppobj
);
550 if (IsEqualGUID( &IID_IDirectInputJoyConfig8
, riid
))
552 *ppobj
= &This
->IDirectInputJoyConfig8_iface
;
553 IUnknown_AddRef( (IUnknown
*)*ppobj
);
558 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid
));
560 return E_NOINTERFACE
;
563 static HRESULT WINAPI
IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface
, REFIID riid
, LPVOID
*ppobj
)
565 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
566 return IDirectInputAImpl_QueryInterface( &This
->IDirectInput7A_iface
, riid
, ppobj
);
569 static HRESULT
initialize_directinput_instance(IDirectInputImpl
*This
, DWORD dwVersion
)
571 if (!This
->initialized
)
573 This
->dwVersion
= dwVersion
;
574 This
->evsequence
= 1;
576 InitializeCriticalSection( &This
->crit
);
577 This
->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": IDirectInputImpl*->crit");
579 list_init( &This
->devices_list
);
580 list_init( &This
->device_players
);
582 /* Add self to the list of the IDirectInputs */
583 EnterCriticalSection( &dinput_hook_crit
);
584 list_add_head( &direct_input_list
, &This
->entry
);
585 LeaveCriticalSection( &dinput_hook_crit
);
587 This
->initialized
= TRUE
;
589 if (!check_hook_thread())
591 uninitialize_directinput_instance( This
);
592 return DIERR_GENERIC
;
599 static void uninitialize_directinput_instance(IDirectInputImpl
*This
)
601 if (This
->initialized
)
603 struct DevicePlayer
*device_player
, *device_player2
;
604 /* Remove self from the list of the IDirectInputs */
605 EnterCriticalSection( &dinput_hook_crit
);
606 list_remove( &This
->entry
);
607 LeaveCriticalSection( &dinput_hook_crit
);
609 LIST_FOR_EACH_ENTRY_SAFE( device_player
, device_player2
,
610 &This
->device_players
, struct DevicePlayer
, entry
)
611 HeapFree(GetProcessHeap(), 0, device_player
);
615 This
->crit
.DebugInfo
->Spare
[0] = 0;
616 DeleteCriticalSection( &This
->crit
);
618 This
->initialized
= FALSE
;
622 enum directinput_versions
624 DIRECTINPUT_VERSION_300
= 0x0300,
625 DIRECTINPUT_VERSION_500
= 0x0500,
626 DIRECTINPUT_VERSION_50A
= 0x050A,
627 DIRECTINPUT_VERSION_5B2
= 0x05B2,
628 DIRECTINPUT_VERSION_602
= 0x0602,
629 DIRECTINPUT_VERSION_61A
= 0x061A,
630 DIRECTINPUT_VERSION_700
= 0x0700,
633 static HRESULT WINAPI
IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface
, HINSTANCE hinst
, DWORD version
)
635 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
637 TRACE("(%p)->(%p, 0x%04x)\n", iface
, hinst
, version
);
640 return DIERR_INVALIDPARAM
;
641 else if (version
== 0)
642 return DIERR_NOTINITIALIZED
;
643 else if (version
> DIRECTINPUT_VERSION_700
)
644 return DIERR_OLDDIRECTINPUTVERSION
;
645 else if (version
!= DIRECTINPUT_VERSION_300
&& version
!= DIRECTINPUT_VERSION_500
&&
646 version
!= DIRECTINPUT_VERSION_50A
&& version
!= DIRECTINPUT_VERSION_5B2
&&
647 version
!= DIRECTINPUT_VERSION_602
&& version
!= DIRECTINPUT_VERSION_61A
&&
648 version
!= DIRECTINPUT_VERSION_700
&& version
!= DIRECTINPUT_VERSION
)
649 return DIERR_BETADIRECTINPUTVERSION
;
651 return initialize_directinput_instance(This
, version
);
654 static HRESULT WINAPI
IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface
, HINSTANCE hinst
, DWORD x
)
656 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
657 return IDirectInputAImpl_Initialize( &This
->IDirectInput7A_iface
, hinst
, x
);
660 static HRESULT WINAPI
IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface
, REFGUID rguid
)
662 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
664 LPDIRECTINPUTDEVICEA device
;
666 TRACE( "(%p)->(%s)\n", This
, debugstr_guid(rguid
) );
668 if (!rguid
) return E_POINTER
;
669 if (!This
->initialized
)
670 return DIERR_NOTINITIALIZED
;
672 hr
= IDirectInput_CreateDevice( iface
, rguid
, &device
, NULL
);
673 if (hr
!= DI_OK
) return DI_NOTATTACHED
;
675 IUnknown_Release( device
);
680 static HRESULT WINAPI
IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface
, REFGUID rguid
)
682 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
683 return IDirectInputAImpl_GetDeviceStatus( &This
->IDirectInput7A_iface
, rguid
);
686 static HRESULT WINAPI
IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface
,
690 WCHAR control_exeW
[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
691 STARTUPINFOW si
= {0};
692 PROCESS_INFORMATION pi
;
694 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
696 TRACE( "(%p)->(%p, %08x)\n", This
, hwndOwner
, dwFlags
);
698 if (hwndOwner
&& !IsWindow(hwndOwner
))
702 return DIERR_INVALIDPARAM
;
704 if (!This
->initialized
)
705 return DIERR_NOTINITIALIZED
;
707 if (!CreateProcessW(NULL
, control_exeW
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
708 return HRESULT_FROM_WIN32(GetLastError());
713 static HRESULT WINAPI
IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface
, HWND hwndOwner
, DWORD dwFlags
)
715 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
716 return IDirectInputAImpl_RunControlPanel( &This
->IDirectInput7A_iface
, hwndOwner
, dwFlags
);
719 static HRESULT WINAPI
IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface
, REFGUID rguid
,
720 LPCSTR pszName
, LPGUID pguidInstance
)
722 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
724 FIXME( "(%p)->(%s, %s, %p): stub\n", This
, debugstr_guid(rguid
), pszName
, pguidInstance
);
729 static HRESULT WINAPI
IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface
, REFGUID rguid
,
730 LPCWSTR pszName
, LPGUID pguidInstance
)
732 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
734 FIXME( "(%p)->(%s, %s, %p): stub\n", This
, debugstr_guid(rguid
), debugstr_w(pszName
), pguidInstance
);
739 static HRESULT
create_device(IDirectInputImpl
*This
, REFGUID rguid
, REFIID riid
, LPVOID
*pvOut
, BOOL unicode
)
746 if (!rguid
|| !pvOut
)
749 if (!This
->initialized
)
750 return DIERR_NOTINITIALIZED
;
752 /* Loop on all the devices to see if anyone matches the given GUID */
753 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++)
757 if (!dinput_devices
[i
]->create_device
) continue;
758 if ((ret
= dinput_devices
[i
]->create_device(This
, rguid
, riid
, pvOut
, unicode
)) == DI_OK
)
762 WARN("invalid device GUID %s\n", debugstr_guid(rguid
));
763 return DIERR_DEVICENOTREG
;
766 static HRESULT WINAPI
IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface
, REFGUID rguid
,
767 REFIID riid
, LPVOID
* pvOut
, LPUNKNOWN lpUnknownOuter
)
769 IDirectInputImpl
*This
= impl_from_IDirectInput7A( iface
);
771 TRACE("(%p)->(%s, %s, %p, %p)\n", This
, debugstr_guid(rguid
), debugstr_guid(riid
), pvOut
, lpUnknownOuter
);
773 return create_device(This
, rguid
, riid
, pvOut
, FALSE
);
776 static HRESULT WINAPI
IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface
, REFGUID rguid
,
777 REFIID riid
, LPVOID
* pvOut
, LPUNKNOWN lpUnknownOuter
)
779 IDirectInputImpl
*This
= impl_from_IDirectInput7W( iface
);
781 TRACE("(%p)->(%s, %s, %p, %p)\n", This
, debugstr_guid(rguid
), debugstr_guid(riid
), pvOut
, lpUnknownOuter
);
783 return create_device(This
, rguid
, riid
, pvOut
, TRUE
);
786 static HRESULT WINAPI
IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface
, REFGUID rguid
,
787 LPDIRECTINPUTDEVICEA
* pdev
, LPUNKNOWN punk
)
789 return IDirectInput7AImpl_CreateDeviceEx(iface
, rguid
, NULL
, (LPVOID
*)pdev
, punk
);
792 static HRESULT WINAPI
IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface
, REFGUID rguid
,
793 LPDIRECTINPUTDEVICEW
* pdev
, LPUNKNOWN punk
)
795 return IDirectInput7WImpl_CreateDeviceEx(iface
, rguid
, NULL
, (LPVOID
*)pdev
, punk
);
798 /*******************************************************************************
802 static ULONG WINAPI
IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface
)
804 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
805 return IDirectInputAImpl_AddRef( &This
->IDirectInput7A_iface
);
808 static ULONG WINAPI
IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface
)
810 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
811 return IDirectInputAImpl_AddRef( &This
->IDirectInput7A_iface
);
814 static HRESULT WINAPI
IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface
, REFIID riid
, LPVOID
*ppobj
)
816 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
817 return IDirectInputAImpl_QueryInterface( &This
->IDirectInput7A_iface
, riid
, ppobj
);
820 static HRESULT WINAPI
IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface
, REFIID riid
, LPVOID
*ppobj
)
822 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
823 return IDirectInputAImpl_QueryInterface( &This
->IDirectInput7A_iface
, riid
, ppobj
);
826 static ULONG WINAPI
IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface
)
828 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
829 return IDirectInputAImpl_Release( &This
->IDirectInput7A_iface
);
832 static ULONG WINAPI
IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface
)
834 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
835 return IDirectInputAImpl_Release( &This
->IDirectInput7A_iface
);
838 static HRESULT WINAPI
IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface
, REFGUID rguid
,
839 LPDIRECTINPUTDEVICE8A
* pdev
, LPUNKNOWN punk
)
841 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
842 return IDirectInput7AImpl_CreateDeviceEx( &This
->IDirectInput7A_iface
, rguid
, NULL
, (LPVOID
*)pdev
, punk
);
845 static HRESULT WINAPI
IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface
, REFGUID rguid
,
846 LPDIRECTINPUTDEVICE8W
* pdev
, LPUNKNOWN punk
)
848 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
849 return IDirectInput7WImpl_CreateDeviceEx( &This
->IDirectInput7W_iface
, rguid
, NULL
, (LPVOID
*)pdev
, punk
);
852 static HRESULT WINAPI
IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKA lpCallback
,
853 LPVOID pvRef
, DWORD dwFlags
)
855 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
856 return IDirectInputAImpl_EnumDevices( &This
->IDirectInput7A_iface
, dwDevType
, lpCallback
, pvRef
, dwFlags
);
859 static HRESULT WINAPI
IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface
, DWORD dwDevType
, LPDIENUMDEVICESCALLBACKW lpCallback
,
860 LPVOID pvRef
, DWORD dwFlags
)
862 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
863 return IDirectInputWImpl_EnumDevices( &This
->IDirectInput7W_iface
, dwDevType
, lpCallback
, pvRef
, dwFlags
);
866 static HRESULT WINAPI
IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface
, REFGUID rguid
)
868 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
869 return IDirectInputAImpl_GetDeviceStatus( &This
->IDirectInput7A_iface
, rguid
);
872 static HRESULT WINAPI
IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface
, REFGUID rguid
)
874 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
875 return IDirectInputAImpl_GetDeviceStatus( &This
->IDirectInput7A_iface
, rguid
);
878 static HRESULT WINAPI
IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface
, HWND hwndOwner
, DWORD dwFlags
)
880 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
881 return IDirectInputAImpl_RunControlPanel( &This
->IDirectInput7A_iface
, hwndOwner
, dwFlags
);
884 static HRESULT WINAPI
IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface
, HWND hwndOwner
, DWORD dwFlags
)
886 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
887 return IDirectInputAImpl_RunControlPanel( &This
->IDirectInput7A_iface
, hwndOwner
, dwFlags
);
890 static HRESULT WINAPI
IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface
, HINSTANCE hinst
, DWORD version
)
892 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
894 TRACE("(%p)->(%p, 0x%04x)\n", iface
, hinst
, version
);
897 return DIERR_INVALIDPARAM
;
898 else if (version
== 0)
899 return DIERR_NOTINITIALIZED
;
900 else if (version
< DIRECTINPUT_VERSION
)
901 return DIERR_BETADIRECTINPUTVERSION
;
902 else if (version
> DIRECTINPUT_VERSION
)
903 return DIERR_OLDDIRECTINPUTVERSION
;
905 return initialize_directinput_instance(This
, version
);
908 static HRESULT WINAPI
IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface
, HINSTANCE hinst
, DWORD version
)
910 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
911 return IDirectInput8AImpl_Initialize( &This
->IDirectInput8A_iface
, hinst
, version
);
914 static HRESULT WINAPI
IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface
, REFGUID rguid
, LPCSTR pszName
, LPGUID pguidInstance
)
916 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
917 return IDirectInput2AImpl_FindDevice( &This
->IDirectInput7A_iface
, rguid
, pszName
, pguidInstance
);
920 static HRESULT WINAPI
IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface
, REFGUID rguid
, LPCWSTR pszName
, LPGUID pguidInstance
)
922 IDirectInputImpl
*This
= impl_from_IDirectInput8W( iface
);
923 return IDirectInput2WImpl_FindDevice( &This
->IDirectInput7W_iface
, rguid
, pszName
, pguidInstance
);
926 static BOOL
should_enumerate_device(const WCHAR
*username
, DWORD dwFlags
,
927 struct list
*device_players
, REFGUID guid
)
929 BOOL should_enumerate
= TRUE
;
930 struct DevicePlayer
*device_player
;
932 /* Check if user owns this device */
933 if (dwFlags
& DIEDBSFL_THISUSER
&& username
&& *username
)
935 should_enumerate
= FALSE
;
936 LIST_FOR_EACH_ENTRY(device_player
, device_players
, struct DevicePlayer
, entry
)
938 if (IsEqualGUID(&device_player
->instance_guid
, guid
))
940 if (*device_player
->username
&& !lstrcmpW(username
, device_player
->username
))
941 return TRUE
; /* Device username matches */
947 /* Check if this device is not owned by anyone */
948 if (dwFlags
& DIEDBSFL_AVAILABLEDEVICES
) {
950 should_enumerate
= FALSE
;
951 LIST_FOR_EACH_ENTRY(device_player
, device_players
, struct DevicePlayer
, entry
)
953 if (IsEqualGUID(&device_player
->instance_guid
, guid
))
955 if (*device_player
->username
)
961 return TRUE
; /* Device does not have a username */
964 return should_enumerate
;
967 static HRESULT WINAPI
IDirectInput8AImpl_EnumDevicesBySemantics(
968 LPDIRECTINPUT8A iface
, LPCSTR ptszUserName
, LPDIACTIONFORMATA lpdiActionFormat
,
969 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback
,
970 LPVOID pvRef
, DWORD dwFlags
973 static REFGUID guids
[2] = { &GUID_SysKeyboard
, &GUID_SysMouse
};
974 static const DWORD actionMasks
[] = { DIKEYBOARD_MASK
, DIMOUSE_MASK
};
975 IDirectInputImpl
*This
= impl_from_IDirectInput8A( iface
);
976 DIDEVICEINSTANCEA didevi
;
977 LPDIRECTINPUTDEVICE8A lpdid
;
980 int device_count
= 0;
982 DIDEVICEINSTANCEA
*didevis
= 0;
983 WCHAR
*username_w
= 0;
985 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This
, debugstr_a(ptszUserName
), lpdiActionFormat
,
986 lpCallback
, pvRef
, dwFlags
);
987 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
988 X(DIEDBSFL_ATTACHEDONLY
)
990 X(DIEDBSFL_FORCEFEEDBACK
)
991 X(DIEDBSFL_AVAILABLEDEVICES
)
992 X(DIEDBSFL_MULTIMICEKEYBOARDS
)
993 X(DIEDBSFL_NONGAMINGDEVICES
)
996 _dump_diactionformatA(lpdiActionFormat
);
998 didevi
.dwSize
= sizeof(didevi
);
1002 int len
= MultiByteToWideChar(CP_ACP
, 0, ptszUserName
, -1, 0, 0);
1004 username_w
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*len
);
1005 MultiByteToWideChar(CP_ACP
, 0, ptszUserName
, -1, username_w
, len
);
1008 /* Enumerate all the joysticks */
1009 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++)
1011 HRESULT enumSuccess
;
1013 if (!dinput_devices
[i
]->enum_deviceA
) continue;
1015 for (j
= 0, enumSuccess
= S_OK
; SUCCEEDED(enumSuccess
); j
++)
1017 TRACE(" - checking device %u ('%s')\n", i
, dinput_devices
[i
]->name
);
1019 /* Default behavior is to enumerate attached game controllers */
1020 enumSuccess
= dinput_devices
[i
]->enum_deviceA(DI8DEVCLASS_GAMECTRL
, DIEDFL_ATTACHEDONLY
| dwFlags
, &didevi
, This
->dwVersion
, j
);
1021 if (enumSuccess
== S_OK
&&
1022 should_enumerate_device(username_w
, dwFlags
, &This
->device_players
, &didevi
.guidInstance
))
1025 didevis
= HeapReAlloc(GetProcessHeap(), 0, didevis
, sizeof(DIDEVICEINSTANCEA
)*device_count
);
1027 didevis
= HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEA
)*device_count
);
1028 didevis
[device_count
-1] = didevi
;
1033 remain
= device_count
;
1034 /* Add keyboard and mouse to remaining device count */
1035 if (!(dwFlags
& DIEDBSFL_FORCEFEEDBACK
))
1037 for (i
= 0; i
< sizeof(guids
) / sizeof(guids
[0]); i
++)
1039 if (should_enumerate_device(username_w
, dwFlags
, &This
->device_players
, guids
[i
]))
1044 for (i
= 0; i
< device_count
; i
++)
1046 callbackFlags
= diactionformat_priorityA(lpdiActionFormat
, lpdiActionFormat
->dwGenre
);
1047 IDirectInput_CreateDevice(iface
, &didevis
[i
].guidInstance
, &lpdid
, NULL
);
1049 if (lpCallback(&didevis
[i
], lpdid
, callbackFlags
, --remain
, pvRef
) == DIENUM_STOP
)
1051 HeapFree(GetProcessHeap(), 0, didevis
);
1052 HeapFree(GetProcessHeap(), 0, username_w
);
1057 HeapFree(GetProcessHeap(), 0, didevis
);
1059 if (dwFlags
& DIEDBSFL_FORCEFEEDBACK
)
1061 HeapFree(GetProcessHeap(), 0, username_w
);
1065 /* Enumerate keyboard and mouse */
1066 for(i
=0; i
< sizeof(guids
)/sizeof(guids
[0]); i
++)
1068 if (should_enumerate_device(username_w
, dwFlags
, &This
->device_players
, guids
[i
]))
1070 callbackFlags
= diactionformat_priorityA(lpdiActionFormat
, actionMasks
[i
]);
1072 IDirectInput_CreateDevice(iface
, guids
[i
], &lpdid
, NULL
);
1073 IDirectInputDevice_GetDeviceInfo(lpdid
, &didevi
);
1075 if (lpCallback(&didevi
, lpdid
, callbackFlags
, --remain
, pvRef
) == DIENUM_STOP
)
1077 HeapFree(GetProcessHeap(), 0, username_w
);
1083 HeapFree(GetProcessHeap(), 0, username_w
);
1087 static HRESULT WINAPI
IDirectInput8WImpl_EnumDevicesBySemantics(
1088 LPDIRECTINPUT8W iface
, LPCWSTR ptszUserName
, LPDIACTIONFORMATW lpdiActionFormat
,
1089 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback
,
1090 LPVOID pvRef
, DWORD dwFlags
1093 static REFGUID guids
[2] = { &GUID_SysKeyboard
, &GUID_SysMouse
};
1094 static const DWORD actionMasks
[] = { DIKEYBOARD_MASK
, DIMOUSE_MASK
};
1095 IDirectInputImpl
*This
= impl_from_IDirectInput8W(iface
);
1096 DIDEVICEINSTANCEW didevi
;
1097 LPDIRECTINPUTDEVICE8W lpdid
;
1098 DWORD callbackFlags
;
1100 int device_count
= 0;
1102 DIDEVICEINSTANCEW
*didevis
= 0;
1104 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This
, debugstr_w(ptszUserName
), lpdiActionFormat
,
1105 lpCallback
, pvRef
, dwFlags
);
1107 didevi
.dwSize
= sizeof(didevi
);
1109 /* Enumerate all the joysticks */
1110 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++)
1112 HRESULT enumSuccess
;
1114 if (!dinput_devices
[i
]->enum_deviceW
) continue;
1116 for (j
= 0, enumSuccess
= S_OK
; SUCCEEDED(enumSuccess
); j
++)
1118 TRACE(" - checking device %u ('%s')\n", i
, dinput_devices
[i
]->name
);
1120 /* Default behavior is to enumerate attached game controllers */
1121 enumSuccess
= dinput_devices
[i
]->enum_deviceW(DI8DEVCLASS_GAMECTRL
, DIEDFL_ATTACHEDONLY
| dwFlags
, &didevi
, This
->dwVersion
, j
);
1122 if (enumSuccess
== S_OK
&&
1123 should_enumerate_device(ptszUserName
, dwFlags
, &This
->device_players
, &didevi
.guidInstance
))
1126 didevis
= HeapReAlloc(GetProcessHeap(), 0, didevis
, sizeof(DIDEVICEINSTANCEW
)*device_count
);
1128 didevis
= HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEW
)*device_count
);
1129 didevis
[device_count
-1] = didevi
;
1134 remain
= device_count
;
1135 /* Add keyboard and mouse to remaining device count */
1136 if (!(dwFlags
& DIEDBSFL_FORCEFEEDBACK
))
1138 for (i
= 0; i
< sizeof(guids
) / sizeof(guids
[0]); i
++)
1140 if (should_enumerate_device(ptszUserName
, dwFlags
, &This
->device_players
, guids
[i
]))
1145 for (i
= 0; i
< device_count
; i
++)
1147 callbackFlags
= diactionformat_priorityW(lpdiActionFormat
, lpdiActionFormat
->dwGenre
);
1148 IDirectInput_CreateDevice(iface
, &didevis
[i
].guidInstance
, &lpdid
, NULL
);
1150 if (lpCallback(&didevis
[i
], lpdid
, callbackFlags
, --remain
, pvRef
) == DIENUM_STOP
)
1152 HeapFree(GetProcessHeap(), 0, didevis
);
1157 HeapFree(GetProcessHeap(), 0, didevis
);
1159 if (dwFlags
& DIEDBSFL_FORCEFEEDBACK
) return DI_OK
;
1161 /* Enumerate keyboard and mouse */
1162 for(i
=0; i
< sizeof(guids
)/sizeof(guids
[0]); i
++)
1164 if (should_enumerate_device(ptszUserName
, dwFlags
, &This
->device_players
, guids
[i
]))
1166 callbackFlags
= diactionformat_priorityW(lpdiActionFormat
, actionMasks
[i
]);
1168 IDirectInput_CreateDevice(iface
, guids
[i
], &lpdid
, NULL
);
1169 IDirectInputDevice_GetDeviceInfo(lpdid
, &didevi
);
1171 if (lpCallback(&didevi
, lpdid
, callbackFlags
, --remain
, pvRef
) == DIENUM_STOP
)
1179 static HRESULT WINAPI
IDirectInput8WImpl_ConfigureDevices(
1180 LPDIRECTINPUT8W iface
, LPDICONFIGUREDEVICESCALLBACK lpdiCallback
,
1181 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams
, DWORD dwFlags
, LPVOID pvRefData
1184 IDirectInputImpl
*This
= impl_from_IDirectInput8W(iface
);
1186 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This
, lpdiCallback
, lpdiCDParams
, dwFlags
, pvRefData
);
1188 /* Call helper function in config.c to do the real work */
1189 return _configure_devices(iface
, lpdiCallback
, lpdiCDParams
, dwFlags
, pvRefData
);
1192 static HRESULT WINAPI
IDirectInput8AImpl_ConfigureDevices(
1193 LPDIRECTINPUT8A iface
, LPDICONFIGUREDEVICESCALLBACK lpdiCallback
,
1194 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams
, DWORD dwFlags
, LPVOID pvRefData
1197 IDirectInputImpl
*This
= impl_from_IDirectInput8A(iface
);
1198 DIACTIONFORMATW diafW
;
1199 DICONFIGUREDEVICESPARAMSW diCDParamsW
;
1203 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This
, lpdiCallback
, lpdiCDParams
, dwFlags
, pvRefData
);
1205 /* Copy parameters */
1206 diCDParamsW
.dwSize
= sizeof(DICONFIGUREDEVICESPARAMSW
);
1207 diCDParamsW
.dwcFormats
= lpdiCDParams
->dwcFormats
;
1208 diCDParamsW
.lprgFormats
= &diafW
;
1209 diCDParamsW
.hwnd
= lpdiCDParams
->hwnd
;
1211 diafW
.rgoAction
= HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW
)*lpdiCDParams
->lprgFormats
->dwNumActions
);
1212 _copy_diactionformatAtoW(&diafW
, lpdiCDParams
->lprgFormats
);
1214 /* Copy action names */
1215 for (i
=0; i
< diafW
.dwNumActions
; i
++)
1217 const char* from
= lpdiCDParams
->lprgFormats
->rgoAction
[i
].u
.lptszActionName
;
1218 int len
= MultiByteToWideChar(CP_ACP
, 0, from
, -1, NULL
, 0);
1219 WCHAR
*to
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*len
);
1221 MultiByteToWideChar(CP_ACP
, 0, from
, -1, to
, len
);
1222 diafW
.rgoAction
[i
].u
.lptszActionName
= to
;
1225 hr
= IDirectInput8WImpl_ConfigureDevices(&This
->IDirectInput8W_iface
, lpdiCallback
, &diCDParamsW
, dwFlags
, pvRefData
);
1227 /* Copy back configuration */
1229 _copy_diactionformatWtoA(lpdiCDParams
->lprgFormats
, &diafW
);
1232 for (i
=0; i
< diafW
.dwNumActions
; i
++)
1233 HeapFree(GetProcessHeap(), 0, (void*) diafW
.rgoAction
[i
].u
.lptszActionName
);
1235 HeapFree(GetProcessHeap(), 0, diafW
.rgoAction
);
1240 /*****************************************************************************
1241 * IDirectInputJoyConfig8 interface
1244 static inline IDirectInputImpl
*impl_from_IDirectInputJoyConfig8(IDirectInputJoyConfig8
*iface
)
1246 return CONTAINING_RECORD( iface
, IDirectInputImpl
, IDirectInputJoyConfig8_iface
);
1249 static HRESULT WINAPI
JoyConfig8Impl_QueryInterface(IDirectInputJoyConfig8
*iface
, REFIID riid
, void** ppobj
)
1251 IDirectInputImpl
*This
= impl_from_IDirectInputJoyConfig8( iface
);
1252 return IDirectInputAImpl_QueryInterface( &This
->IDirectInput7A_iface
, riid
, ppobj
);
1255 static ULONG WINAPI
JoyConfig8Impl_AddRef(IDirectInputJoyConfig8
*iface
)
1257 IDirectInputImpl
*This
= impl_from_IDirectInputJoyConfig8( iface
);
1258 return IDirectInputAImpl_AddRef( &This
->IDirectInput7A_iface
);
1261 static ULONG WINAPI
JoyConfig8Impl_Release(IDirectInputJoyConfig8
*iface
)
1263 IDirectInputImpl
*This
= impl_from_IDirectInputJoyConfig8( iface
);
1264 return IDirectInputAImpl_Release( &This
->IDirectInput7A_iface
);
1267 static HRESULT WINAPI
JoyConfig8Impl_Acquire(IDirectInputJoyConfig8
*iface
)
1269 FIXME( "(%p): stub!\n", iface
);
1273 static HRESULT WINAPI
JoyConfig8Impl_Unacquire(IDirectInputJoyConfig8
*iface
)
1275 FIXME( "(%p): stub!\n", iface
);
1279 static HRESULT WINAPI
JoyConfig8Impl_SetCooperativeLevel(IDirectInputJoyConfig8
*iface
, HWND hwnd
, DWORD flags
)
1281 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface
, hwnd
, flags
);
1285 static HRESULT WINAPI
JoyConfig8Impl_SendNotify(IDirectInputJoyConfig8
*iface
)
1287 FIXME( "(%p): stub!\n", iface
);
1291 static HRESULT WINAPI
JoyConfig8Impl_EnumTypes(IDirectInputJoyConfig8
*iface
, LPDIJOYTYPECALLBACK cb
, void *ref
)
1293 FIXME( "(%p)->(%p, %p): stub!\n", iface
, cb
, ref
);
1297 static HRESULT WINAPI
JoyConfig8Impl_GetTypeInfo(IDirectInputJoyConfig8
*iface
, LPCWSTR name
, LPDIJOYTYPEINFO info
, DWORD flags
)
1299 FIXME( "(%p)->(%s, %p, 0x%08x): stub!\n", iface
, debugstr_w(name
), info
, flags
);
1303 static HRESULT WINAPI
JoyConfig8Impl_SetTypeInfo(IDirectInputJoyConfig8
*iface
, LPCWSTR name
, LPCDIJOYTYPEINFO info
, DWORD flags
,
1306 FIXME( "(%p)->(%s, %p, 0x%08x, %s): stub!\n", iface
, debugstr_w(name
), info
, flags
, debugstr_w(new_name
) );
1310 static HRESULT WINAPI
JoyConfig8Impl_DeleteType(IDirectInputJoyConfig8
*iface
, LPCWSTR name
)
1312 FIXME( "(%p)->(%s): stub!\n", iface
, debugstr_w(name
) );
1316 static HRESULT WINAPI
JoyConfig8Impl_GetConfig(IDirectInputJoyConfig8
*iface
, UINT id
, LPDIJOYCONFIG info
, DWORD flags
)
1318 IDirectInputImpl
*di
= impl_from_IDirectInputJoyConfig8(iface
);
1323 FIXME("(%p)->(%d, %p, 0x%08x): semi-stub!\n", iface
, id
, info
, flags
);
1325 #define X(x) if (flags & x) FIXME("\tflags |= "#x"\n");
1326 X(DIJC_GUIDINSTANCE
)
1327 X(DIJC_REGHWCONFIGTYPE
)
1332 /* Enumerate all joysticks in order */
1333 for (i
= 0; i
< NB_DINPUT_DEVICES
; i
++)
1335 if (!dinput_devices
[i
]->enum_deviceA
) continue;
1337 for (j
= 0, r
= S_OK
; SUCCEEDED(r
); j
++)
1339 DIDEVICEINSTANCEA dev
;
1340 dev
.dwSize
= sizeof(dev
);
1341 if ((r
= dinput_devices
[i
]->enum_deviceA(DI8DEVCLASS_GAMECTRL
, 0, &dev
, di
->dwVersion
, j
)) == S_OK
)
1343 /* Only take into account the chosen id */
1346 if (flags
& DIJC_GUIDINSTANCE
)
1347 info
->guidInstance
= dev
.guidInstance
;
1356 return DIERR_NOMOREITEMS
;
1359 static HRESULT WINAPI
JoyConfig8Impl_SetConfig(IDirectInputJoyConfig8
*iface
, UINT id
, LPCDIJOYCONFIG info
, DWORD flags
)
1361 FIXME( "(%p)->(%d, %p, 0x%08x): stub!\n", iface
, id
, info
, flags
);
1365 static HRESULT WINAPI
JoyConfig8Impl_DeleteConfig(IDirectInputJoyConfig8
*iface
, UINT id
)
1367 FIXME( "(%p)->(%d): stub!\n", iface
, id
);
1371 static HRESULT WINAPI
JoyConfig8Impl_GetUserValues(IDirectInputJoyConfig8
*iface
, LPDIJOYUSERVALUES info
, DWORD flags
)
1373 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface
, info
, flags
);
1377 static HRESULT WINAPI
JoyConfig8Impl_SetUserValues(IDirectInputJoyConfig8
*iface
, LPCDIJOYUSERVALUES info
, DWORD flags
)
1379 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface
, info
, flags
);
1383 static HRESULT WINAPI
JoyConfig8Impl_AddNewHardware(IDirectInputJoyConfig8
*iface
, HWND hwnd
, REFGUID guid
)
1385 FIXME( "(%p)->(%p, %s): stub!\n", iface
, hwnd
, debugstr_guid(guid
) );
1389 static HRESULT WINAPI
JoyConfig8Impl_OpenTypeKey(IDirectInputJoyConfig8
*iface
, LPCWSTR name
, DWORD security
, PHKEY key
)
1391 FIXME( "(%p)->(%s, 0x%08x, %p): stub!\n", iface
, debugstr_w(name
), security
, key
);
1395 static HRESULT WINAPI
JoyConfig8Impl_OpenAppStatusKey(IDirectInputJoyConfig8
*iface
, PHKEY key
)
1397 FIXME( "(%p)->(%p): stub!\n", iface
, key
);
1401 static const IDirectInput7AVtbl ddi7avt
= {
1402 IDirectInputAImpl_QueryInterface
,
1403 IDirectInputAImpl_AddRef
,
1404 IDirectInputAImpl_Release
,
1405 IDirectInputAImpl_CreateDevice
,
1406 IDirectInputAImpl_EnumDevices
,
1407 IDirectInputAImpl_GetDeviceStatus
,
1408 IDirectInputAImpl_RunControlPanel
,
1409 IDirectInputAImpl_Initialize
,
1410 IDirectInput2AImpl_FindDevice
,
1411 IDirectInput7AImpl_CreateDeviceEx
1414 static const IDirectInput7WVtbl ddi7wvt
= {
1415 IDirectInputWImpl_QueryInterface
,
1416 IDirectInputWImpl_AddRef
,
1417 IDirectInputWImpl_Release
,
1418 IDirectInputWImpl_CreateDevice
,
1419 IDirectInputWImpl_EnumDevices
,
1420 IDirectInputWImpl_GetDeviceStatus
,
1421 IDirectInputWImpl_RunControlPanel
,
1422 IDirectInputWImpl_Initialize
,
1423 IDirectInput2WImpl_FindDevice
,
1424 IDirectInput7WImpl_CreateDeviceEx
1427 static const IDirectInput8AVtbl ddi8avt
= {
1428 IDirectInput8AImpl_QueryInterface
,
1429 IDirectInput8AImpl_AddRef
,
1430 IDirectInput8AImpl_Release
,
1431 IDirectInput8AImpl_CreateDevice
,
1432 IDirectInput8AImpl_EnumDevices
,
1433 IDirectInput8AImpl_GetDeviceStatus
,
1434 IDirectInput8AImpl_RunControlPanel
,
1435 IDirectInput8AImpl_Initialize
,
1436 IDirectInput8AImpl_FindDevice
,
1437 IDirectInput8AImpl_EnumDevicesBySemantics
,
1438 IDirectInput8AImpl_ConfigureDevices
1441 static const IDirectInput8WVtbl ddi8wvt
= {
1442 IDirectInput8WImpl_QueryInterface
,
1443 IDirectInput8WImpl_AddRef
,
1444 IDirectInput8WImpl_Release
,
1445 IDirectInput8WImpl_CreateDevice
,
1446 IDirectInput8WImpl_EnumDevices
,
1447 IDirectInput8WImpl_GetDeviceStatus
,
1448 IDirectInput8WImpl_RunControlPanel
,
1449 IDirectInput8WImpl_Initialize
,
1450 IDirectInput8WImpl_FindDevice
,
1451 IDirectInput8WImpl_EnumDevicesBySemantics
,
1452 IDirectInput8WImpl_ConfigureDevices
1455 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt
=
1457 JoyConfig8Impl_QueryInterface
,
1458 JoyConfig8Impl_AddRef
,
1459 JoyConfig8Impl_Release
,
1460 JoyConfig8Impl_Acquire
,
1461 JoyConfig8Impl_Unacquire
,
1462 JoyConfig8Impl_SetCooperativeLevel
,
1463 JoyConfig8Impl_SendNotify
,
1464 JoyConfig8Impl_EnumTypes
,
1465 JoyConfig8Impl_GetTypeInfo
,
1466 JoyConfig8Impl_SetTypeInfo
,
1467 JoyConfig8Impl_DeleteType
,
1468 JoyConfig8Impl_GetConfig
,
1469 JoyConfig8Impl_SetConfig
,
1470 JoyConfig8Impl_DeleteConfig
,
1471 JoyConfig8Impl_GetUserValues
,
1472 JoyConfig8Impl_SetUserValues
,
1473 JoyConfig8Impl_AddNewHardware
,
1474 JoyConfig8Impl_OpenTypeKey
,
1475 JoyConfig8Impl_OpenAppStatusKey
1478 /*******************************************************************************
1479 * DirectInput ClassFactory
1483 /* IUnknown fields */
1484 IClassFactory IClassFactory_iface
;
1486 } IClassFactoryImpl
;
1488 static inline IClassFactoryImpl
*impl_from_IClassFactory(IClassFactory
*iface
)
1490 return CONTAINING_RECORD(iface
, IClassFactoryImpl
, IClassFactory_iface
);
1493 static HRESULT WINAPI
DICF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
,LPVOID
*ppobj
) {
1494 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
1496 FIXME("(%p)->(%s,%p),stub!\n",This
,debugstr_guid(riid
),ppobj
);
1497 return E_NOINTERFACE
;
1500 static ULONG WINAPI
DICF_AddRef(LPCLASSFACTORY iface
) {
1501 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
1502 return InterlockedIncrement(&(This
->ref
));
1505 static ULONG WINAPI
DICF_Release(LPCLASSFACTORY iface
) {
1506 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
1507 /* static class, won't be freed */
1508 return InterlockedDecrement(&(This
->ref
));
1511 static HRESULT WINAPI
DICF_CreateInstance(
1512 LPCLASSFACTORY iface
,LPUNKNOWN pOuter
,REFIID riid
,LPVOID
*ppobj
1514 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
1516 TRACE("(%p)->(%p,%s,%p)\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
1517 if ( IsEqualGUID( &IID_IUnknown
, riid
) ||
1518 IsEqualGUID( &IID_IDirectInputA
, riid
) ||
1519 IsEqualGUID( &IID_IDirectInputW
, riid
) ||
1520 IsEqualGUID( &IID_IDirectInput2A
, riid
) ||
1521 IsEqualGUID( &IID_IDirectInput2W
, riid
) ||
1522 IsEqualGUID( &IID_IDirectInput7A
, riid
) ||
1523 IsEqualGUID( &IID_IDirectInput7W
, riid
) ) {
1524 return create_directinput_instance(riid
, ppobj
, NULL
);
1527 FIXME("(%p,%p,%s,%p) Interface not found!\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
1528 return E_NOINTERFACE
;
1531 static HRESULT WINAPI
DICF_LockServer(LPCLASSFACTORY iface
,BOOL dolock
) {
1532 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
1533 FIXME("(%p)->(%d),stub!\n",This
,dolock
);
1537 static const IClassFactoryVtbl DICF_Vtbl
= {
1538 DICF_QueryInterface
,
1541 DICF_CreateInstance
,
1544 static IClassFactoryImpl DINPUT_CF
= {{&DICF_Vtbl
}, 1 };
1546 /***********************************************************************
1547 * DllCanUnloadNow (DINPUT.@)
1549 HRESULT WINAPI
DllCanUnloadNow(void)
1554 /***********************************************************************
1555 * DllGetClassObject (DINPUT.@)
1557 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
1559 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
1560 if ( IsEqualCLSID( &IID_IClassFactory
, riid
) ) {
1562 IClassFactory_AddRef((IClassFactory
*)*ppv
);
1566 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
1567 return CLASS_E_CLASSNOTAVAILABLE
;
1570 /***********************************************************************
1571 * DllRegisterServer (DINPUT.@)
1573 HRESULT WINAPI
DllRegisterServer(void)
1575 return __wine_register_resources( DINPUT_instance
);
1578 /***********************************************************************
1579 * DllUnregisterServer (DINPUT.@)
1581 HRESULT WINAPI
DllUnregisterServer(void)
1583 return __wine_unregister_resources( DINPUT_instance
);
1586 /******************************************************************************
1587 * DInput hook thread
1590 static LRESULT CALLBACK
LL_hook_proc( int code
, WPARAM wparam
, LPARAM lparam
)
1592 IDirectInputImpl
*dinput
;
1595 if (code
!= HC_ACTION
) return CallNextHookEx( 0, code
, wparam
, lparam
);
1597 EnterCriticalSection( &dinput_hook_crit
);
1598 LIST_FOR_EACH_ENTRY( dinput
, &direct_input_list
, IDirectInputImpl
, entry
)
1600 IDirectInputDeviceImpl
*dev
;
1602 EnterCriticalSection( &dinput
->crit
);
1603 LIST_FOR_EACH_ENTRY( dev
, &dinput
->devices_list
, IDirectInputDeviceImpl
, entry
)
1604 if (dev
->acquired
&& dev
->event_proc
)
1606 TRACE("calling %p->%p (%lx %lx)\n", dev
, dev
->event_proc
, wparam
, lparam
);
1607 skip
|= dev
->event_proc( &dev
->IDirectInputDevice8A_iface
, wparam
, lparam
);
1609 LeaveCriticalSection( &dinput
->crit
);
1611 LeaveCriticalSection( &dinput_hook_crit
);
1613 return skip
? 1 : CallNextHookEx( 0, code
, wparam
, lparam
);
1616 static LRESULT CALLBACK
callwndproc_proc( int code
, WPARAM wparam
, LPARAM lparam
)
1618 CWPSTRUCT
*msg
= (CWPSTRUCT
*)lparam
;
1619 IDirectInputImpl
*dinput
;
1622 if (code
!= HC_ACTION
|| (msg
->message
!= WM_KILLFOCUS
&&
1623 msg
->message
!= WM_ACTIVATEAPP
&& msg
->message
!= WM_ACTIVATE
))
1624 return CallNextHookEx( 0, code
, wparam
, lparam
);
1626 foreground
= GetForegroundWindow();
1628 EnterCriticalSection( &dinput_hook_crit
);
1630 LIST_FOR_EACH_ENTRY( dinput
, &direct_input_list
, IDirectInputImpl
, entry
)
1632 IDirectInputDeviceImpl
*dev
;
1634 EnterCriticalSection( &dinput
->crit
);
1635 LIST_FOR_EACH_ENTRY( dev
, &dinput
->devices_list
, IDirectInputDeviceImpl
, entry
)
1637 if (!dev
->acquired
) continue;
1639 if (msg
->hwnd
== dev
->win
&& msg
->hwnd
!= foreground
)
1641 TRACE( "%p window is not foreground - unacquiring %p\n", dev
->win
, dev
);
1642 IDirectInputDevice_Unacquire( &dev
->IDirectInputDevice8A_iface
);
1645 LeaveCriticalSection( &dinput
->crit
);
1647 LeaveCriticalSection( &dinput_hook_crit
);
1649 return CallNextHookEx( 0, code
, wparam
, lparam
);
1652 static DWORD WINAPI
hook_thread_proc(void *param
)
1654 static HHOOK kbd_hook
, mouse_hook
;
1657 /* Force creation of the message queue */
1658 PeekMessageW( &msg
, 0, 0, 0, PM_NOREMOVE
);
1659 SetEvent(*(LPHANDLE
)param
);
1661 while (GetMessageW( &msg
, 0, 0, 0 ))
1663 UINT kbd_cnt
= 0, mice_cnt
= 0;
1665 if (msg
.message
== WM_USER
+0x10)
1667 IDirectInputImpl
*dinput
;
1669 TRACE( "Processing hook change notification lp:%ld\n", msg
.lParam
);
1671 if (!msg
.wParam
&& !msg
.lParam
)
1673 if (kbd_hook
) UnhookWindowsHookEx( kbd_hook
);
1674 if (mouse_hook
) UnhookWindowsHookEx( mouse_hook
);
1675 kbd_hook
= mouse_hook
= NULL
;
1679 EnterCriticalSection( &dinput_hook_crit
);
1681 /* Count acquired keyboards and mice*/
1682 LIST_FOR_EACH_ENTRY( dinput
, &direct_input_list
, IDirectInputImpl
, entry
)
1684 IDirectInputDeviceImpl
*dev
;
1686 EnterCriticalSection( &dinput
->crit
);
1687 LIST_FOR_EACH_ENTRY( dev
, &dinput
->devices_list
, IDirectInputDeviceImpl
, entry
)
1689 if (!dev
->acquired
|| !dev
->event_proc
) continue;
1691 if (IsEqualGUID( &dev
->guid
, &GUID_SysKeyboard
) ||
1692 IsEqualGUID( &dev
->guid
, &DInput_Wine_Keyboard_GUID
))
1695 if (IsEqualGUID( &dev
->guid
, &GUID_SysMouse
) ||
1696 IsEqualGUID( &dev
->guid
, &DInput_Wine_Mouse_GUID
))
1699 LeaveCriticalSection( &dinput
->crit
);
1701 LeaveCriticalSection( &dinput_hook_crit
);
1703 if (kbd_cnt
&& !kbd_hook
)
1704 kbd_hook
= SetWindowsHookExW( WH_KEYBOARD_LL
, LL_hook_proc
, DINPUT_instance
, 0 );
1705 else if (!kbd_cnt
&& kbd_hook
)
1707 UnhookWindowsHookEx( kbd_hook
);
1711 if (mice_cnt
&& !mouse_hook
)
1712 mouse_hook
= SetWindowsHookExW( WH_MOUSE_LL
, LL_hook_proc
, DINPUT_instance
, 0 );
1713 else if (!mice_cnt
&& mouse_hook
)
1715 UnhookWindowsHookEx( mouse_hook
);
1719 TranslateMessage(&msg
);
1720 DispatchMessageW(&msg
);
1726 static DWORD hook_thread_id
;
1728 static CRITICAL_SECTION_DEBUG dinput_critsect_debug
=
1730 0, 0, &dinput_hook_crit
,
1731 { &dinput_critsect_debug
.ProcessLocksList
, &dinput_critsect_debug
.ProcessLocksList
},
1732 0, 0, { (DWORD_PTR
)(__FILE__
": dinput_hook_crit") }
1734 static CRITICAL_SECTION dinput_hook_crit
= { &dinput_critsect_debug
, -1, 0, 0, 0, 0 };
1736 static BOOL
check_hook_thread(void)
1738 static HANDLE hook_thread
;
1740 EnterCriticalSection(&dinput_hook_crit
);
1742 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list
));
1743 if (!list_empty(&direct_input_list
) && !hook_thread
)
1747 event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1748 hook_thread
= CreateThread(NULL
, 0, hook_thread_proc
, &event
, 0, &hook_thread_id
);
1749 if (event
&& hook_thread
)
1753 handles
[1] = hook_thread
;
1754 WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1756 LeaveCriticalSection(&dinput_hook_crit
);
1759 else if (list_empty(&direct_input_list
) && hook_thread
)
1761 DWORD tid
= hook_thread_id
;
1764 PostThreadMessageW(tid
, WM_USER
+0x10, 0, 0);
1765 LeaveCriticalSection(&dinput_hook_crit
);
1767 /* wait for hook thread to exit */
1768 WaitForSingleObject(hook_thread
, INFINITE
);
1769 CloseHandle(hook_thread
);
1773 LeaveCriticalSection(&dinput_hook_crit
);
1775 return hook_thread_id
!= 0;
1778 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface
)
1780 static HHOOK callwndproc_hook
;
1781 static ULONG foreground_cnt
;
1782 IDirectInputDeviceImpl
*dev
= impl_from_IDirectInputDevice8W(iface
);
1784 EnterCriticalSection(&dinput_hook_crit
);
1786 if (dev
->dwCoopLevel
& DISCL_FOREGROUND
)
1794 if (foreground_cnt
&& !callwndproc_hook
)
1795 callwndproc_hook
= SetWindowsHookExW( WH_CALLWNDPROC
, callwndproc_proc
,
1796 DINPUT_instance
, GetCurrentThreadId() );
1797 else if (!foreground_cnt
&& callwndproc_hook
)
1799 UnhookWindowsHookEx( callwndproc_hook
);
1800 callwndproc_hook
= NULL
;
1803 PostThreadMessageW( hook_thread_id
, WM_USER
+0x10, 1, 0 );
1805 LeaveCriticalSection(&dinput_hook_crit
);
1808 void check_dinput_events(void)
1810 /* Windows does not do that, but our current implementation of winex11
1811 * requires periodic event polling to forward events to the wineserver.
1813 * We have to call this function from multiple places, because:
1814 * - some games do not explicitly poll for mouse events
1815 * (for example Culpa Innata)
1816 * - some games only poll the device, and neither keyboard nor mouse
1817 * (for example Civilization: Call to Power 2)
1819 MsgWaitForMultipleObjectsEx(0, NULL
, 0, QS_ALLINPUT
, 0);
1822 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
1826 case DLL_PROCESS_ATTACH
:
1827 DisableThreadLibraryCalls(inst
);
1828 DINPUT_instance
= inst
;
1830 case DLL_PROCESS_DETACH
:
1831 if (reserved
) break;
1832 DeleteCriticalSection(&dinput_hook_crit
);