msvcp140: Add task_continuation_context constructor implementation.
[wine.git] / dlls / dinput / dinput_main.c
blobab6a2e8787494ebe4490a35e9c2f85a432dbbffb
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
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) );
109 HRESULT hr;
111 if (!This)
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 );
121 if (FAILED(hr))
123 HeapFree( GetProcessHeap(), 0, This );
124 return hr;
127 if (out) *out = This;
128 return DI_OK;
131 /******************************************************************************
132 * DirectInputCreateEx (DINPUT.@)
134 HRESULT WINAPI DirectInputCreateEx(
135 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
136 LPUNKNOWN punkOuter)
138 IDirectInputImpl *This;
139 HRESULT hr;
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);
151 if (FAILED(hr))
152 return hr;
154 else
155 return DIERR_NOINTERFACE;
157 hr = IDirectInput_Initialize( &This->IDirectInput7A_iface, hinst, dwVersion );
158 if (FAILED(hr))
160 IDirectInput_Release( &This->IDirectInput7A_iface );
161 *ppDI = NULL;
162 return hr;
165 return DI_OK;
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) {
187 switch (dwDevType) {
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";
195 } else {
196 switch (dwDevType) {
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)) {
210 unsigned int i;
211 static const struct {
212 DWORD mask;
213 const char *name;
214 } flags[] = {
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)
222 #undef FE
224 TRACE(" flags: ");
225 if (dwFlags == 0) {
226 TRACE("DIEDFL_ALLDEVICES\n");
227 return;
229 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
230 if (flags[i].mask & dwFlags)
231 TRACE("%s ",flags[i].name);
233 TRACE("\n");
236 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat)
238 unsigned int i;
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)
269 int i;
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)
296 int i;
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)
329 int i;
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)
342 int i;
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)
356 __asm
358 push ebp
359 mov ebp, esp
360 push [ebp+16]
361 push [ebp+12]
362 call [ebp+8]
363 leave
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,
370 "pushl %ebp\n\t"
371 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
372 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
373 "movl %esp,%ebp\n\t"
374 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
375 "pushl 16(%ebp)\n\t"
376 "pushl 12(%ebp)\n\t"
377 "call *8(%ebp)\n\t"
378 "leave\n\t"
379 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
380 __ASM_CFI(".cfi_same_value %ebp\n\t")
381 "ret" )
382 #else
383 #define enum_callback_wrapper(callback, instance, ref) (callback)((instance), (ref))
384 #endif
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;
395 unsigned int i;
396 int j;
397 HRESULT r;
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);
404 if (!lpCallback ||
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);
418 if (r == S_OK)
419 if (enum_callback_wrapper(lpCallback, &devInstance, pvRef) == DIENUM_STOP)
420 return S_OK;
424 return S_OK;
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;
435 unsigned int i;
436 int j;
437 HRESULT r;
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);
444 if (!lpCallback ||
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);
458 if (r == S_OK)
459 if (enum_callback_wrapper(lpCallback, &devInstance, pvRef) == DIENUM_STOP)
460 return S_OK;
464 return S_OK;
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);
473 return ref;
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 );
489 if (ref == 0)
491 uninitialize_directinput_instance( This );
492 HeapFree( GetProcessHeap(), 0, This );
495 return ref;
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 );
510 if (!riid || !ppobj)
511 return E_POINTER;
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 );
521 return DI_OK;
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 );
531 return DI_OK;
534 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
536 *ppobj = &This->IDirectInput8A_iface;
537 IUnknown_AddRef( (IUnknown*)*ppobj );
539 return DI_OK;
542 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
544 *ppobj = &This->IDirectInput8W_iface;
545 IUnknown_AddRef( (IUnknown*)*ppobj );
547 return DI_OK;
550 if (IsEqualGUID( &IID_IDirectInputJoyConfig8, riid ))
552 *ppobj = &This->IDirectInputJoyConfig8_iface;
553 IUnknown_AddRef( (IUnknown*)*ppobj );
555 return DI_OK;
558 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
559 *ppobj = NULL;
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 );
581 /* Add self to the list of the IDirectInputs */
582 EnterCriticalSection( &dinput_hook_crit );
583 list_add_head( &direct_input_list, &This->entry );
584 LeaveCriticalSection( &dinput_hook_crit );
586 This->initialized = TRUE;
588 if (!check_hook_thread())
590 uninitialize_directinput_instance( This );
591 return DIERR_GENERIC;
595 return DI_OK;
598 static void uninitialize_directinput_instance(IDirectInputImpl *This)
600 if (This->initialized)
602 /* Remove self from the list of the IDirectInputs */
603 EnterCriticalSection( &dinput_hook_crit );
604 list_remove( &This->entry );
605 LeaveCriticalSection( &dinput_hook_crit );
607 check_hook_thread();
609 This->crit.DebugInfo->Spare[0] = 0;
610 DeleteCriticalSection( &This->crit );
612 This->initialized = FALSE;
616 enum directinput_versions
618 DIRECTINPUT_VERSION_300 = 0x0300,
619 DIRECTINPUT_VERSION_500 = 0x0500,
620 DIRECTINPUT_VERSION_50A = 0x050A,
621 DIRECTINPUT_VERSION_5B2 = 0x05B2,
622 DIRECTINPUT_VERSION_602 = 0x0602,
623 DIRECTINPUT_VERSION_61A = 0x061A,
624 DIRECTINPUT_VERSION_700 = 0x0700,
627 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD version)
629 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
631 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
633 if (!hinst)
634 return DIERR_INVALIDPARAM;
635 else if (version == 0)
636 return DIERR_NOTINITIALIZED;
637 else if (version > DIRECTINPUT_VERSION_700)
638 return DIERR_OLDDIRECTINPUTVERSION;
639 else if (version != DIRECTINPUT_VERSION_300 && version != DIRECTINPUT_VERSION_500 &&
640 version != DIRECTINPUT_VERSION_50A && version != DIRECTINPUT_VERSION_5B2 &&
641 version != DIRECTINPUT_VERSION_602 && version != DIRECTINPUT_VERSION_61A &&
642 version != DIRECTINPUT_VERSION_700 && version != DIRECTINPUT_VERSION)
643 return DIERR_BETADIRECTINPUTVERSION;
645 return initialize_directinput_instance(This, version);
648 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
650 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
651 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
654 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
656 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
657 HRESULT hr;
658 LPDIRECTINPUTDEVICEA device;
660 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
662 if (!rguid) return E_POINTER;
663 if (!This->initialized)
664 return DIERR_NOTINITIALIZED;
666 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
667 if (hr != DI_OK) return DI_NOTATTACHED;
669 IUnknown_Release( device );
671 return DI_OK;
674 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
676 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
677 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
680 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
681 HWND hwndOwner,
682 DWORD dwFlags)
684 WCHAR control_exeW[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
685 STARTUPINFOW si = {0};
686 PROCESS_INFORMATION pi;
688 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
690 TRACE( "(%p)->(%p, %08x)\n", This, hwndOwner, dwFlags );
692 if (hwndOwner && !IsWindow(hwndOwner))
693 return E_HANDLE;
695 if (dwFlags)
696 return DIERR_INVALIDPARAM;
698 if (!This->initialized)
699 return DIERR_NOTINITIALIZED;
701 if (!CreateProcessW(NULL, control_exeW, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
702 return HRESULT_FROM_WIN32(GetLastError());
704 return DI_OK;
707 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
709 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
710 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
713 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
714 LPCSTR pszName, LPGUID pguidInstance)
716 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
718 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
720 return DI_OK;
723 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
724 LPCWSTR pszName, LPGUID pguidInstance)
726 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
728 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
730 return DI_OK;
733 static HRESULT create_device(IDirectInputImpl *This, REFGUID rguid, REFIID riid, LPVOID *pvOut, BOOL unicode)
735 unsigned int i;
737 if (pvOut)
738 *pvOut = NULL;
740 if (!rguid || !pvOut)
741 return E_POINTER;
743 if (!This->initialized)
744 return DIERR_NOTINITIALIZED;
746 /* Loop on all the devices to see if anyone matches the given GUID */
747 for (i = 0; i < NB_DINPUT_DEVICES; i++)
749 HRESULT ret;
751 if (!dinput_devices[i]->create_device) continue;
752 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, unicode)) == DI_OK)
753 return DI_OK;
756 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
757 return DIERR_DEVICENOTREG;
760 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
761 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
763 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
765 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
767 return create_device(This, rguid, riid, pvOut, FALSE);
770 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
771 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
773 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
775 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
777 return create_device(This, rguid, riid, pvOut, TRUE);
780 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
781 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
783 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
786 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
787 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
789 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
792 /*******************************************************************************
793 * DirectInput8
796 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
798 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
799 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
802 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
804 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
805 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
808 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
810 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
811 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
814 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
816 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
817 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
820 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
822 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
823 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
826 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
828 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
829 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
832 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
833 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
835 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
836 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
839 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
840 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
842 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
843 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
846 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
847 LPVOID pvRef, DWORD dwFlags)
849 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
850 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
853 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
854 LPVOID pvRef, DWORD dwFlags)
856 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
857 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
860 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
862 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
863 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
866 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
868 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
869 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
872 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
874 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
875 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
878 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
880 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
881 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
884 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD version)
886 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
888 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
890 if (!hinst)
891 return DIERR_INVALIDPARAM;
892 else if (version == 0)
893 return DIERR_NOTINITIALIZED;
894 else if (version < DIRECTINPUT_VERSION)
895 return DIERR_BETADIRECTINPUTVERSION;
896 else if (version > DIRECTINPUT_VERSION)
897 return DIERR_OLDDIRECTINPUTVERSION;
899 return initialize_directinput_instance(This, version);
902 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD version)
904 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
905 return IDirectInput8AImpl_Initialize( &This->IDirectInput8A_iface, hinst, version );
908 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
910 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
911 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
914 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
916 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
917 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
920 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
921 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
922 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
923 LPVOID pvRef, DWORD dwFlags
926 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
927 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
928 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
929 DIDEVICEINSTANCEA didevi;
930 LPDIRECTINPUTDEVICE8A lpdid;
931 DWORD callbackFlags;
932 int i, j;
933 int device_count = 0;
934 int remain;
935 DIDEVICEINSTANCEA *didevis = 0;
937 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_a(ptszUserName), lpdiActionFormat,
938 lpCallback, pvRef, dwFlags);
939 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
940 X(DIEDBSFL_ATTACHEDONLY)
941 X(DIEDBSFL_THISUSER)
942 X(DIEDBSFL_FORCEFEEDBACK)
943 X(DIEDBSFL_AVAILABLEDEVICES)
944 X(DIEDBSFL_MULTIMICEKEYBOARDS)
945 X(DIEDBSFL_NONGAMINGDEVICES)
946 #undef X
948 _dump_diactionformatA(lpdiActionFormat);
950 didevi.dwSize = sizeof(didevi);
952 /* Enumerate all the joysticks */
953 for (i = 0; i < NB_DINPUT_DEVICES; i++)
955 HRESULT enumSuccess;
957 if (!dinput_devices[i]->enum_deviceA) continue;
959 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
961 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
963 /* Default behavior is to enumerate attached game controllers */
964 enumSuccess = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
965 if (enumSuccess == S_OK)
967 if (device_count++)
968 didevis = HeapReAlloc(GetProcessHeap(), 0, didevis, sizeof(DIDEVICEINSTANCEA)*device_count);
969 else
970 didevis = HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEA)*device_count);
971 didevis[device_count-1] = didevi;
976 remain = device_count;
977 if (!(dwFlags & DIEDBSFL_FORCEFEEDBACK))
978 remain += sizeof(guids)/sizeof(guids[0]);
980 for (i = 0; i < device_count; i++)
982 callbackFlags = diactionformat_priorityA(lpdiActionFormat, lpdiActionFormat->dwGenre);
983 IDirectInput_CreateDevice(iface, &didevis[i].guidInstance, &lpdid, NULL);
985 if (lpCallback(&didevis[i], lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
987 HeapFree(GetProcessHeap(), 0, didevis);
988 return DI_OK;
992 HeapFree(GetProcessHeap(), 0, didevis);
994 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
996 /* Enumerate keyboard and mouse */
997 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
999 callbackFlags = diactionformat_priorityA(lpdiActionFormat, actionMasks[i]);
1001 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1002 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1004 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
1005 return DI_OK;
1008 return DI_OK;
1011 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
1012 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
1013 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
1014 LPVOID pvRef, DWORD dwFlags
1017 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
1018 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
1019 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1020 DIDEVICEINSTANCEW didevi;
1021 LPDIRECTINPUTDEVICE8W lpdid;
1022 DWORD callbackFlags;
1023 int i, j;
1024 int device_count = 0;
1025 int remain;
1026 DIDEVICEINSTANCEW *didevis = 0;
1028 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
1029 lpCallback, pvRef, dwFlags);
1031 didevi.dwSize = sizeof(didevi);
1033 /* Enumerate all the joysticks */
1034 for (i = 0; i < NB_DINPUT_DEVICES; i++)
1036 HRESULT enumSuccess;
1038 if (!dinput_devices[i]->enum_deviceW) continue;
1040 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
1042 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
1044 /* Default behavior is to enumerate attached game controllers */
1045 enumSuccess = dinput_devices[i]->enum_deviceW(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
1046 if (enumSuccess == S_OK)
1048 if (device_count++)
1049 didevis = HeapReAlloc(GetProcessHeap(), 0, didevis, sizeof(DIDEVICEINSTANCEW)*device_count);
1050 else
1051 didevis = HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEW)*device_count);
1052 didevis[device_count-1] = didevi;
1057 remain = device_count;
1058 if (!(dwFlags & DIEDBSFL_FORCEFEEDBACK))
1059 remain += sizeof(guids)/sizeof(guids[0]);
1061 for (i = 0; i < device_count; i++)
1063 callbackFlags = diactionformat_priorityW(lpdiActionFormat, lpdiActionFormat->dwGenre);
1064 IDirectInput_CreateDevice(iface, &didevis[i].guidInstance, &lpdid, NULL);
1066 if (lpCallback(&didevis[i], lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1068 HeapFree(GetProcessHeap(), 0, didevis);
1069 return DI_OK;
1073 HeapFree(GetProcessHeap(), 0, didevis);
1075 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
1077 /* Enumerate keyboard and mouse */
1078 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
1080 callbackFlags = diactionformat_priorityW(lpdiActionFormat, actionMasks[i]);
1082 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1083 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1085 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
1086 return DI_OK;
1089 return DI_OK;
1092 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
1093 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1094 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1097 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1099 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1101 /* Call helper function in config.c to do the real work */
1102 return _configure_devices(iface, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1105 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
1106 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1107 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1110 IDirectInputImpl *This = impl_from_IDirectInput8A(iface);
1111 DIACTIONFORMATW diafW;
1112 DICONFIGUREDEVICESPARAMSW diCDParamsW;
1113 HRESULT hr;
1114 int i;
1116 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1118 /* Copy parameters */
1119 diCDParamsW.dwSize = sizeof(DICONFIGUREDEVICESPARAMSW);
1120 diCDParamsW.dwcFormats = lpdiCDParams->dwcFormats;
1121 diCDParamsW.lprgFormats = &diafW;
1122 diCDParamsW.hwnd = lpdiCDParams->hwnd;
1124 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiCDParams->lprgFormats->dwNumActions);
1125 _copy_diactionformatAtoW(&diafW, lpdiCDParams->lprgFormats);
1127 /* Copy action names */
1128 for (i=0; i < diafW.dwNumActions; i++)
1130 const char* from = lpdiCDParams->lprgFormats->rgoAction[i].u.lptszActionName;
1131 int len = MultiByteToWideChar(CP_ACP, 0, from , -1, NULL , 0);
1132 WCHAR *to = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1134 MultiByteToWideChar(CP_ACP, 0, from , -1, to , len);
1135 diafW.rgoAction[i].u.lptszActionName = to;
1138 hr = IDirectInput8WImpl_ConfigureDevices(&This->IDirectInput8W_iface, lpdiCallback, &diCDParamsW, dwFlags, pvRefData);
1140 /* Copy back configuration */
1141 if (SUCCEEDED(hr))
1142 _copy_diactionformatWtoA(lpdiCDParams->lprgFormats, &diafW);
1144 /* Free memory */
1145 for (i=0; i < diafW.dwNumActions; i++)
1146 HeapFree(GetProcessHeap(), 0, (void*) diafW.rgoAction[i].u.lptszActionName);
1148 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1150 return hr;
1153 /*****************************************************************************
1154 * IDirectInputJoyConfig8 interface
1157 static inline IDirectInputImpl *impl_from_IDirectInputJoyConfig8(IDirectInputJoyConfig8 *iface)
1159 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInputJoyConfig8_iface );
1162 static HRESULT WINAPI JoyConfig8Impl_QueryInterface(IDirectInputJoyConfig8 *iface, REFIID riid, void** ppobj)
1164 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1165 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
1168 static ULONG WINAPI JoyConfig8Impl_AddRef(IDirectInputJoyConfig8 *iface)
1170 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1171 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
1174 static ULONG WINAPI JoyConfig8Impl_Release(IDirectInputJoyConfig8 *iface)
1176 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1177 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
1180 static HRESULT WINAPI JoyConfig8Impl_Acquire(IDirectInputJoyConfig8 *iface)
1182 FIXME( "(%p): stub!\n", iface );
1183 return E_NOTIMPL;
1186 static HRESULT WINAPI JoyConfig8Impl_Unacquire(IDirectInputJoyConfig8 *iface)
1188 FIXME( "(%p): stub!\n", iface );
1189 return E_NOTIMPL;
1192 static HRESULT WINAPI JoyConfig8Impl_SetCooperativeLevel(IDirectInputJoyConfig8 *iface, HWND hwnd, DWORD flags)
1194 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, hwnd, flags );
1195 return E_NOTIMPL;
1198 static HRESULT WINAPI JoyConfig8Impl_SendNotify(IDirectInputJoyConfig8 *iface)
1200 FIXME( "(%p): stub!\n", iface );
1201 return E_NOTIMPL;
1204 static HRESULT WINAPI JoyConfig8Impl_EnumTypes(IDirectInputJoyConfig8 *iface, LPDIJOYTYPECALLBACK cb, void *ref)
1206 FIXME( "(%p)->(%p, %p): stub!\n", iface, cb, ref );
1207 return E_NOTIMPL;
1210 static HRESULT WINAPI JoyConfig8Impl_GetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPDIJOYTYPEINFO info, DWORD flags)
1212 FIXME( "(%p)->(%s, %p, 0x%08x): stub!\n", iface, debugstr_w(name), info, flags );
1213 return E_NOTIMPL;
1216 static HRESULT WINAPI JoyConfig8Impl_SetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPCDIJOYTYPEINFO info, DWORD flags,
1217 LPWSTR new_name)
1219 FIXME( "(%p)->(%s, %p, 0x%08x, %s): stub!\n", iface, debugstr_w(name), info, flags, debugstr_w(new_name) );
1220 return E_NOTIMPL;
1223 static HRESULT WINAPI JoyConfig8Impl_DeleteType(IDirectInputJoyConfig8 *iface, LPCWSTR name)
1225 FIXME( "(%p)->(%s): stub!\n", iface, debugstr_w(name) );
1226 return E_NOTIMPL;
1229 static HRESULT WINAPI JoyConfig8Impl_GetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPDIJOYCONFIG info, DWORD flags)
1231 IDirectInputImpl *di = impl_from_IDirectInputJoyConfig8(iface);
1232 UINT found = 0;
1233 int i, j;
1234 HRESULT r;
1236 FIXME("(%p)->(%d, %p, 0x%08x): semi-stub!\n", iface, id, info, flags);
1238 #define X(x) if (flags & x) FIXME("\tflags |= "#x"\n");
1239 X(DIJC_GUIDINSTANCE)
1240 X(DIJC_REGHWCONFIGTYPE)
1241 X(DIJC_GAIN)
1242 X(DIJC_CALLOUT)
1243 #undef X
1245 /* Enumerate all joysticks in order */
1246 for (i = 0; i < NB_DINPUT_DEVICES; i++)
1248 if (!dinput_devices[i]->enum_deviceA) continue;
1250 for (j = 0, r = S_OK; SUCCEEDED(r); j++)
1252 DIDEVICEINSTANCEA dev;
1253 dev.dwSize = sizeof(dev);
1254 if ((r = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, 0, &dev, di->dwVersion, j)) == S_OK)
1256 /* Only take into account the chosen id */
1257 if (found == id)
1259 if (flags & DIJC_GUIDINSTANCE)
1260 info->guidInstance = dev.guidInstance;
1262 return DI_OK;
1264 found += 1;
1269 return DIERR_NOMOREITEMS;
1272 static HRESULT WINAPI JoyConfig8Impl_SetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPCDIJOYCONFIG info, DWORD flags)
1274 FIXME( "(%p)->(%d, %p, 0x%08x): stub!\n", iface, id, info, flags );
1275 return E_NOTIMPL;
1278 static HRESULT WINAPI JoyConfig8Impl_DeleteConfig(IDirectInputJoyConfig8 *iface, UINT id)
1280 FIXME( "(%p)->(%d): stub!\n", iface, id );
1281 return E_NOTIMPL;
1284 static HRESULT WINAPI JoyConfig8Impl_GetUserValues(IDirectInputJoyConfig8 *iface, LPDIJOYUSERVALUES info, DWORD flags)
1286 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1287 return E_NOTIMPL;
1290 static HRESULT WINAPI JoyConfig8Impl_SetUserValues(IDirectInputJoyConfig8 *iface, LPCDIJOYUSERVALUES info, DWORD flags)
1292 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1293 return E_NOTIMPL;
1296 static HRESULT WINAPI JoyConfig8Impl_AddNewHardware(IDirectInputJoyConfig8 *iface, HWND hwnd, REFGUID guid)
1298 FIXME( "(%p)->(%p, %s): stub!\n", iface, hwnd, debugstr_guid(guid) );
1299 return E_NOTIMPL;
1302 static HRESULT WINAPI JoyConfig8Impl_OpenTypeKey(IDirectInputJoyConfig8 *iface, LPCWSTR name, DWORD security, PHKEY key)
1304 FIXME( "(%p)->(%s, 0x%08x, %p): stub!\n", iface, debugstr_w(name), security, key );
1305 return E_NOTIMPL;
1308 static HRESULT WINAPI JoyConfig8Impl_OpenAppStatusKey(IDirectInputJoyConfig8 *iface, PHKEY key)
1310 FIXME( "(%p)->(%p): stub!\n", iface, key );
1311 return E_NOTIMPL;
1314 static const IDirectInput7AVtbl ddi7avt = {
1315 IDirectInputAImpl_QueryInterface,
1316 IDirectInputAImpl_AddRef,
1317 IDirectInputAImpl_Release,
1318 IDirectInputAImpl_CreateDevice,
1319 IDirectInputAImpl_EnumDevices,
1320 IDirectInputAImpl_GetDeviceStatus,
1321 IDirectInputAImpl_RunControlPanel,
1322 IDirectInputAImpl_Initialize,
1323 IDirectInput2AImpl_FindDevice,
1324 IDirectInput7AImpl_CreateDeviceEx
1327 static const IDirectInput7WVtbl ddi7wvt = {
1328 IDirectInputWImpl_QueryInterface,
1329 IDirectInputWImpl_AddRef,
1330 IDirectInputWImpl_Release,
1331 IDirectInputWImpl_CreateDevice,
1332 IDirectInputWImpl_EnumDevices,
1333 IDirectInputWImpl_GetDeviceStatus,
1334 IDirectInputWImpl_RunControlPanel,
1335 IDirectInputWImpl_Initialize,
1336 IDirectInput2WImpl_FindDevice,
1337 IDirectInput7WImpl_CreateDeviceEx
1340 static const IDirectInput8AVtbl ddi8avt = {
1341 IDirectInput8AImpl_QueryInterface,
1342 IDirectInput8AImpl_AddRef,
1343 IDirectInput8AImpl_Release,
1344 IDirectInput8AImpl_CreateDevice,
1345 IDirectInput8AImpl_EnumDevices,
1346 IDirectInput8AImpl_GetDeviceStatus,
1347 IDirectInput8AImpl_RunControlPanel,
1348 IDirectInput8AImpl_Initialize,
1349 IDirectInput8AImpl_FindDevice,
1350 IDirectInput8AImpl_EnumDevicesBySemantics,
1351 IDirectInput8AImpl_ConfigureDevices
1354 static const IDirectInput8WVtbl ddi8wvt = {
1355 IDirectInput8WImpl_QueryInterface,
1356 IDirectInput8WImpl_AddRef,
1357 IDirectInput8WImpl_Release,
1358 IDirectInput8WImpl_CreateDevice,
1359 IDirectInput8WImpl_EnumDevices,
1360 IDirectInput8WImpl_GetDeviceStatus,
1361 IDirectInput8WImpl_RunControlPanel,
1362 IDirectInput8WImpl_Initialize,
1363 IDirectInput8WImpl_FindDevice,
1364 IDirectInput8WImpl_EnumDevicesBySemantics,
1365 IDirectInput8WImpl_ConfigureDevices
1368 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt =
1370 JoyConfig8Impl_QueryInterface,
1371 JoyConfig8Impl_AddRef,
1372 JoyConfig8Impl_Release,
1373 JoyConfig8Impl_Acquire,
1374 JoyConfig8Impl_Unacquire,
1375 JoyConfig8Impl_SetCooperativeLevel,
1376 JoyConfig8Impl_SendNotify,
1377 JoyConfig8Impl_EnumTypes,
1378 JoyConfig8Impl_GetTypeInfo,
1379 JoyConfig8Impl_SetTypeInfo,
1380 JoyConfig8Impl_DeleteType,
1381 JoyConfig8Impl_GetConfig,
1382 JoyConfig8Impl_SetConfig,
1383 JoyConfig8Impl_DeleteConfig,
1384 JoyConfig8Impl_GetUserValues,
1385 JoyConfig8Impl_SetUserValues,
1386 JoyConfig8Impl_AddNewHardware,
1387 JoyConfig8Impl_OpenTypeKey,
1388 JoyConfig8Impl_OpenAppStatusKey
1391 /*******************************************************************************
1392 * DirectInput ClassFactory
1394 typedef struct
1396 /* IUnknown fields */
1397 IClassFactory IClassFactory_iface;
1398 LONG ref;
1399 } IClassFactoryImpl;
1401 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1403 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1406 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1407 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1409 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1410 return E_NOINTERFACE;
1413 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
1414 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1415 return InterlockedIncrement(&(This->ref));
1418 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
1419 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1420 /* static class, won't be freed */
1421 return InterlockedDecrement(&(This->ref));
1424 static HRESULT WINAPI DICF_CreateInstance(
1425 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1427 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1429 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1430 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
1431 IsEqualGUID( &IID_IDirectInputA, riid ) ||
1432 IsEqualGUID( &IID_IDirectInputW, riid ) ||
1433 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
1434 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
1435 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
1436 IsEqualGUID( &IID_IDirectInput7W, riid ) ) {
1437 return create_directinput_instance(riid, ppobj, NULL);
1440 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1441 return E_NOINTERFACE;
1444 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1445 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1446 FIXME("(%p)->(%d),stub!\n",This,dolock);
1447 return S_OK;
1450 static const IClassFactoryVtbl DICF_Vtbl = {
1451 DICF_QueryInterface,
1452 DICF_AddRef,
1453 DICF_Release,
1454 DICF_CreateInstance,
1455 DICF_LockServer
1457 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
1459 /***********************************************************************
1460 * DllCanUnloadNow (DINPUT.@)
1462 HRESULT WINAPI DllCanUnloadNow(void)
1464 return S_FALSE;
1467 /***********************************************************************
1468 * DllGetClassObject (DINPUT.@)
1470 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1472 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1473 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1474 *ppv = &DINPUT_CF;
1475 IClassFactory_AddRef((IClassFactory*)*ppv);
1476 return S_OK;
1479 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1480 return CLASS_E_CLASSNOTAVAILABLE;
1483 /***********************************************************************
1484 * DllRegisterServer (DINPUT.@)
1486 HRESULT WINAPI DllRegisterServer(void)
1488 return __wine_register_resources( DINPUT_instance );
1491 /***********************************************************************
1492 * DllUnregisterServer (DINPUT.@)
1494 HRESULT WINAPI DllUnregisterServer(void)
1496 return __wine_unregister_resources( DINPUT_instance );
1499 /******************************************************************************
1500 * DInput hook thread
1503 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
1505 IDirectInputImpl *dinput;
1506 int skip = 0;
1508 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
1510 EnterCriticalSection( &dinput_hook_crit );
1511 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1513 IDirectInputDeviceImpl *dev;
1515 EnterCriticalSection( &dinput->crit );
1516 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1517 if (dev->acquired && dev->event_proc)
1519 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
1520 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1522 LeaveCriticalSection( &dinput->crit );
1524 LeaveCriticalSection( &dinput_hook_crit );
1526 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
1529 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
1531 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
1532 IDirectInputImpl *dinput;
1533 HWND foreground;
1535 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1536 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1537 return CallNextHookEx( 0, code, wparam, lparam );
1539 foreground = GetForegroundWindow();
1541 EnterCriticalSection( &dinput_hook_crit );
1543 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1545 IDirectInputDeviceImpl *dev;
1547 EnterCriticalSection( &dinput->crit );
1548 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1550 if (!dev->acquired) continue;
1552 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1554 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1555 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1558 LeaveCriticalSection( &dinput->crit );
1560 LeaveCriticalSection( &dinput_hook_crit );
1562 return CallNextHookEx( 0, code, wparam, lparam );
1565 static DWORD WINAPI hook_thread_proc(void *param)
1567 static HHOOK kbd_hook, mouse_hook;
1568 MSG msg;
1570 /* Force creation of the message queue */
1571 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1572 SetEvent(*(LPHANDLE)param);
1574 while (GetMessageW( &msg, 0, 0, 0 ))
1576 UINT kbd_cnt = 0, mice_cnt = 0;
1578 if (msg.message == WM_USER+0x10)
1580 IDirectInputImpl *dinput;
1582 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
1584 if (!msg.wParam && !msg.lParam)
1586 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1587 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1588 kbd_hook = mouse_hook = NULL;
1589 break;
1592 EnterCriticalSection( &dinput_hook_crit );
1594 /* Count acquired keyboards and mice*/
1595 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1597 IDirectInputDeviceImpl *dev;
1599 EnterCriticalSection( &dinput->crit );
1600 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1602 if (!dev->acquired || !dev->event_proc) continue;
1604 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1605 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1606 kbd_cnt++;
1607 else
1608 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1609 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1610 mice_cnt++;
1612 LeaveCriticalSection( &dinput->crit );
1614 LeaveCriticalSection( &dinput_hook_crit );
1616 if (kbd_cnt && !kbd_hook)
1617 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1618 else if (!kbd_cnt && kbd_hook)
1620 UnhookWindowsHookEx( kbd_hook );
1621 kbd_hook = NULL;
1624 if (mice_cnt && !mouse_hook)
1625 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1626 else if (!mice_cnt && mouse_hook)
1628 UnhookWindowsHookEx( mouse_hook );
1629 mouse_hook = NULL;
1632 TranslateMessage(&msg);
1633 DispatchMessageW(&msg);
1636 return 0;
1639 static DWORD hook_thread_id;
1641 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1643 0, 0, &dinput_hook_crit,
1644 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1645 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1647 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1649 static BOOL check_hook_thread(void)
1651 static HANDLE hook_thread;
1653 EnterCriticalSection(&dinput_hook_crit);
1655 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1656 if (!list_empty(&direct_input_list) && !hook_thread)
1658 HANDLE event;
1660 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1661 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1662 if (event && hook_thread)
1664 HANDLE handles[2];
1665 handles[0] = event;
1666 handles[1] = hook_thread;
1667 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1669 LeaveCriticalSection(&dinput_hook_crit);
1670 CloseHandle(event);
1672 else if (list_empty(&direct_input_list) && hook_thread)
1674 DWORD tid = hook_thread_id;
1676 hook_thread_id = 0;
1677 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1678 LeaveCriticalSection(&dinput_hook_crit);
1680 /* wait for hook thread to exit */
1681 WaitForSingleObject(hook_thread, INFINITE);
1682 CloseHandle(hook_thread);
1683 hook_thread = NULL;
1685 else
1686 LeaveCriticalSection(&dinput_hook_crit);
1688 return hook_thread_id != 0;
1691 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1693 static HHOOK callwndproc_hook;
1694 static ULONG foreground_cnt;
1695 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1697 EnterCriticalSection(&dinput_hook_crit);
1699 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1701 if (dev->acquired)
1702 foreground_cnt++;
1703 else
1704 foreground_cnt--;
1707 if (foreground_cnt && !callwndproc_hook)
1708 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1709 DINPUT_instance, GetCurrentThreadId() );
1710 else if (!foreground_cnt && callwndproc_hook)
1712 UnhookWindowsHookEx( callwndproc_hook );
1713 callwndproc_hook = NULL;
1716 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1718 LeaveCriticalSection(&dinput_hook_crit);
1721 void check_dinput_events(void)
1723 /* Windows does not do that, but our current implementation of winex11
1724 * requires periodic event polling to forward events to the wineserver.
1726 * We have to call this function from multiple places, because:
1727 * - some games do not explicitly poll for mouse events
1728 * (for example Culpa Innata)
1729 * - some games only poll the device, and neither keyboard nor mouse
1730 * (for example Civilization: Call to Power 2)
1732 MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
1735 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved)
1737 switch(reason)
1739 case DLL_PROCESS_ATTACH:
1740 DisableThreadLibraryCalls(inst);
1741 DINPUT_instance = inst;
1742 break;
1743 case DLL_PROCESS_DETACH:
1744 if (reserved) break;
1745 DeleteCriticalSection(&dinput_hook_crit);
1746 break;
1748 return TRUE;