wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / dinput / dinput_main.c
blob5da60cf1f7c8141dcc70460e5be63ebb15c74565
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 "wine/asm.h"
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winuser.h"
47 #include "winerror.h"
48 #include "objbase.h"
49 #include "rpcproxy.h"
50 #include "initguid.h"
51 #include "devguid.h"
52 #include "dinput_private.h"
53 #include "device_private.h"
54 #include "dinputd.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
58 static const IDirectInput7AVtbl ddi7avt;
59 static const IDirectInput7WVtbl ddi7wvt;
60 static const IDirectInput8AVtbl ddi8avt;
61 static const IDirectInput8WVtbl ddi8wvt;
62 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt;
64 static inline IDirectInputImpl *impl_from_IDirectInput7A( IDirectInput7A *iface )
66 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7A_iface );
69 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
71 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7W_iface );
74 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
76 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8A_iface );
79 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
81 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8W_iface );
84 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
86 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
89 static const struct dinput_device *dinput_devices[] =
91 &mouse_device,
92 &keyboard_device,
93 &joystick_linuxinput_device,
94 &joystick_linux_device,
95 &joystick_osx_device
98 HINSTANCE DINPUT_instance;
100 static const WCHAR di_em_win_w[] = {'D','I','E','m','W','i','n',0};
101 static HWND di_em_win;
103 static BOOL check_hook_thread(void);
104 static CRITICAL_SECTION dinput_hook_crit;
105 static struct list direct_input_list = LIST_INIT( direct_input_list );
106 static struct list acquired_mouse_list = LIST_INIT( acquired_mouse_list );
107 static struct list acquired_rawmouse_list = LIST_INIT( acquired_rawmouse_list );
108 static struct list acquired_keyboard_list = LIST_INIT( acquired_keyboard_list );
109 static struct list acquired_device_list = LIST_INIT( acquired_device_list );
111 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion);
112 static void uninitialize_directinput_instance(IDirectInputImpl *This);
114 void dinput_hooks_acquire_device(LPDIRECTINPUTDEVICE8W iface)
116 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
118 EnterCriticalSection( &dinput_hook_crit );
119 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ))
120 list_add_tail( dev->use_raw_input ? &acquired_rawmouse_list : &acquired_mouse_list, &dev->entry );
121 else if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ))
122 list_add_tail( &acquired_keyboard_list, &dev->entry );
123 else
124 list_add_tail( &acquired_device_list, &dev->entry );
125 LeaveCriticalSection( &dinput_hook_crit );
128 void dinput_hooks_unacquire_device(LPDIRECTINPUTDEVICE8W iface)
130 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
132 EnterCriticalSection( &dinput_hook_crit );
133 list_remove( &dev->entry );
134 LeaveCriticalSection( &dinput_hook_crit );
137 static HRESULT create_directinput_instance(REFIID riid, LPVOID *ppDI, IDirectInputImpl **out)
139 IDirectInputImpl *This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectInputImpl) );
140 HRESULT hr;
142 if (!This)
143 return E_OUTOFMEMORY;
145 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
146 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
147 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
148 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
149 This->IDirectInputJoyConfig8_iface.lpVtbl = &JoyConfig8vt;
151 hr = IDirectInput_QueryInterface( &This->IDirectInput7A_iface, riid, ppDI );
152 if (FAILED(hr))
154 HeapFree( GetProcessHeap(), 0, This );
155 return hr;
158 if (out) *out = This;
159 return DI_OK;
162 /******************************************************************************
163 * DirectInputCreateEx (DINPUT.@)
165 HRESULT WINAPI DirectInputCreateEx(
166 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
167 LPUNKNOWN punkOuter)
169 IDirectInputImpl *This;
170 HRESULT hr;
172 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
174 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
175 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
176 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
177 IsEqualGUID( &IID_IDirectInputW, riid ) ||
178 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
179 IsEqualGUID( &IID_IDirectInput7W, riid ))
181 hr = create_directinput_instance(riid, ppDI, &This);
182 if (FAILED(hr))
183 return hr;
185 else
186 return DIERR_NOINTERFACE;
188 hr = IDirectInput_Initialize( &This->IDirectInput7A_iface, hinst, dwVersion );
189 if (FAILED(hr))
191 IDirectInput_Release( &This->IDirectInput7A_iface );
192 *ppDI = NULL;
193 return hr;
196 return DI_OK;
199 /******************************************************************************
200 * DirectInput8Create (DINPUT8.@)
202 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInput8Create(HINSTANCE hinst,
203 DWORD version, REFIID iid, void **out, IUnknown *outer)
205 IDirectInputImpl *This;
206 HRESULT hr;
208 TRACE("hinst %p, version %#x, iid %s, out %p, outer %p.\n",
209 hinst, version, debugstr_guid(iid), out, outer);
211 if (!out)
212 return E_POINTER;
214 if (!IsEqualGUID(&IID_IDirectInput8A, iid) &&
215 !IsEqualGUID(&IID_IDirectInput8W, iid) &&
216 !IsEqualGUID(&IID_IUnknown, iid))
218 *out = NULL;
219 return DIERR_NOINTERFACE;
222 hr = create_directinput_instance(iid, out, &This);
224 if (FAILED(hr))
226 ERR("Failed to create DirectInput, hr %#x.\n", hr);
227 return hr;
230 /* When aggregation is used, the application needs to manually call Initialize(). */
231 if (!outer && IsEqualGUID(&IID_IDirectInput8A, iid))
233 hr = IDirectInput8_Initialize(&This->IDirectInput8A_iface, hinst, version);
234 if (FAILED(hr))
236 IDirectInput8_Release(&This->IDirectInput8A_iface);
237 *out = NULL;
238 return hr;
242 if (!outer && IsEqualGUID(&IID_IDirectInput8W, iid))
244 hr = IDirectInput8_Initialize(&This->IDirectInput8W_iface, hinst, version);
245 if (FAILED(hr))
247 IDirectInput8_Release(&This->IDirectInput8W_iface);
248 *out = NULL;
249 return hr;
253 return S_OK;
256 /******************************************************************************
257 * DirectInputCreateA (DINPUT.@)
259 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
261 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
264 /******************************************************************************
265 * DirectInputCreateW (DINPUT.@)
267 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
269 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
272 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType, DWORD dwVersion)
274 if (dwVersion < 0x0800) {
275 switch (dwDevType) {
276 case 0: return "All devices";
277 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
278 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
279 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
280 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
281 default: return "Unknown";
283 } else {
284 switch (dwDevType) {
285 case DI8DEVCLASS_ALL: return "All devices";
286 case DI8DEVCLASS_POINTER: return "DI8DEVCLASS_POINTER";
287 case DI8DEVCLASS_KEYBOARD: return "DI8DEVCLASS_KEYBOARD";
288 case DI8DEVCLASS_DEVICE: return "DI8DEVCLASS_DEVICE";
289 case DI8DEVCLASS_GAMECTRL: return "DI8DEVCLASS_GAMECTRL";
290 default: return "Unknown";
295 static void _dump_EnumDevices_dwFlags(DWORD dwFlags)
297 if (TRACE_ON(dinput)) {
298 unsigned int i;
299 static const struct {
300 DWORD mask;
301 const char *name;
302 } flags[] = {
303 #define FE(x) { x, #x}
304 FE(DIEDFL_ALLDEVICES),
305 FE(DIEDFL_ATTACHEDONLY),
306 FE(DIEDFL_FORCEFEEDBACK),
307 FE(DIEDFL_INCLUDEALIASES),
308 FE(DIEDFL_INCLUDEPHANTOMS),
309 FE(DIEDFL_INCLUDEHIDDEN)
310 #undef FE
312 TRACE(" flags: ");
313 if (dwFlags == 0) {
314 TRACE("DIEDFL_ALLDEVICES\n");
315 return;
317 for (i = 0; i < ARRAY_SIZE(flags); i++)
318 if (flags[i].mask & dwFlags)
319 TRACE("%s ",flags[i].name);
321 TRACE("\n");
324 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat)
326 unsigned int i;
328 TRACE("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
329 TRACE("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
330 TRACE("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
331 TRACE("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
332 TRACE("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
333 TRACE("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
334 TRACE("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
335 TRACE("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
336 TRACE("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
337 TRACE("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
338 TRACE("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
339 TRACE("diaf.ftTimeStamp ...\n");
340 TRACE("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
341 TRACE("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
342 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
344 TRACE("diaf.rgoAction[%u]:\n", i);
345 TRACE("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
346 TRACE("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
347 TRACE("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
348 TRACE("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
349 TRACE("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
350 TRACE("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
351 TRACE("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
355 void _copy_diactionformatAtoW(LPDIACTIONFORMATW to, LPDIACTIONFORMATA from)
357 int i;
359 to->dwSize = sizeof(DIACTIONFORMATW);
360 to->dwActionSize = sizeof(DIACTIONW);
361 to->dwDataSize = from->dwDataSize;
362 to->dwNumActions = from->dwNumActions;
363 to->guidActionMap = from->guidActionMap;
364 to->dwGenre = from->dwGenre;
365 to->dwBufferSize = from->dwBufferSize;
366 to->lAxisMin = from->lAxisMin;
367 to->lAxisMax = from->lAxisMax;
368 to->dwCRC = from->dwCRC;
369 to->ftTimeStamp = from->ftTimeStamp;
371 for (i=0; i < to->dwNumActions; i++)
373 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
374 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
375 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
376 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
377 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
378 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
382 void _copy_diactionformatWtoA(LPDIACTIONFORMATA to, LPDIACTIONFORMATW from)
384 int i;
386 to->dwSize = sizeof(DIACTIONFORMATA);
387 to->dwActionSize = sizeof(DIACTIONA);
388 to->dwDataSize = from->dwDataSize;
389 to->dwNumActions = from->dwNumActions;
390 to->guidActionMap = from->guidActionMap;
391 to->dwGenre = from->dwGenre;
392 to->dwBufferSize = from->dwBufferSize;
393 to->lAxisMin = from->lAxisMin;
394 to->lAxisMax = from->lAxisMax;
395 to->dwCRC = from->dwCRC;
396 to->ftTimeStamp = from->ftTimeStamp;
398 for (i=0; i < to->dwNumActions; i++)
400 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
401 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
402 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
403 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
404 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
405 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
409 /* diactionformat_priority
411 * Given a DIACTIONFORMAT structure and a DI genre, returns the enumeration
412 * priority. Joysticks should pass the game genre, and mouse or keyboard their
413 * respective DI*_MASK
415 static DWORD diactionformat_priorityA(LPDIACTIONFORMATA lpdiaf, DWORD genre)
417 int i;
418 DWORD priorityFlags = 0;
420 /* If there's at least one action for the device it's priority 1 */
421 for(i=0; i < lpdiaf->dwNumActions; i++)
422 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
423 priorityFlags |= DIEDBS_MAPPEDPRI1;
425 return priorityFlags;
428 static DWORD diactionformat_priorityW(LPDIACTIONFORMATW lpdiaf, DWORD genre)
430 int i;
431 DWORD priorityFlags = 0;
433 /* If there's at least one action for the device it's priority 1 */
434 for(i=0; i < lpdiaf->dwNumActions; i++)
435 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
436 priorityFlags |= DIEDBS_MAPPEDPRI1;
438 return priorityFlags;
441 #if defined __i386__ && defined _MSC_VER
442 __declspec(naked) BOOL enum_callback_wrapper(void *callback, const void *instance, void *ref)
444 __asm
446 push ebp
447 mov ebp, esp
448 push [ebp+16]
449 push [ebp+12]
450 call [ebp+8]
451 leave
455 #elif defined __i386__ && defined __GNUC__
456 extern BOOL enum_callback_wrapper(void *callback, const void *instance, void *ref);
457 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
458 "pushl %ebp\n\t"
459 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
460 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
461 "movl %esp,%ebp\n\t"
462 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
463 "pushl 16(%ebp)\n\t"
464 "pushl 12(%ebp)\n\t"
465 "call *8(%ebp)\n\t"
466 "leave\n\t"
467 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
468 __ASM_CFI(".cfi_same_value %ebp\n\t")
469 "ret" )
470 #else
471 #define enum_callback_wrapper(callback, instance, ref) (callback)((instance), (ref))
472 #endif
474 /******************************************************************************
475 * IDirectInputA_EnumDevices
477 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
478 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
479 LPVOID pvRef, DWORD dwFlags)
481 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
482 DIDEVICEINSTANCEA devInstance;
483 unsigned int i;
484 int j;
485 HRESULT r;
487 TRACE("(this=%p,0x%04x '%s',%p,%p,0x%04x)\n",
488 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType, This->dwVersion),
489 lpCallback, pvRef, dwFlags);
490 _dump_EnumDevices_dwFlags(dwFlags);
492 if (!lpCallback ||
493 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
494 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
495 return DIERR_INVALIDPARAM;
497 if (!This->initialized)
498 return DIERR_NOTINITIALIZED;
500 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++) {
501 if (!dinput_devices[i]->enum_deviceA) continue;
503 TRACE(" Checking device %u ('%s')\n", i, dinput_devices[i]->name);
504 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
505 devInstance.dwSize = sizeof(devInstance);
506 r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
507 if (r == S_OK)
508 if (enum_callback_wrapper(lpCallback, &devInstance, pvRef) == DIENUM_STOP)
509 return S_OK;
513 return S_OK;
515 /******************************************************************************
516 * IDirectInputW_EnumDevices
518 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
519 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
520 LPVOID pvRef, DWORD dwFlags)
522 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
523 DIDEVICEINSTANCEW devInstance;
524 unsigned int i;
525 int j;
526 HRESULT r;
528 TRACE("(this=%p,0x%04x '%s',%p,%p,0x%04x)\n",
529 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType, This->dwVersion),
530 lpCallback, pvRef, dwFlags);
531 _dump_EnumDevices_dwFlags(dwFlags);
533 if (!lpCallback ||
534 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
535 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
536 return DIERR_INVALIDPARAM;
538 if (!This->initialized)
539 return DIERR_NOTINITIALIZED;
541 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++) {
542 if (!dinput_devices[i]->enum_deviceW) continue;
543 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
544 devInstance.dwSize = sizeof(devInstance);
545 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
546 r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
547 if (r == S_OK)
548 if (enum_callback_wrapper(lpCallback, &devInstance, pvRef) == DIENUM_STOP)
549 return S_OK;
553 return S_OK;
556 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
558 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
559 ULONG ref = InterlockedIncrement(&This->ref);
561 TRACE( "(%p) ref %d\n", This, ref );
562 return ref;
565 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
567 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
568 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
571 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
573 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
574 ULONG ref = InterlockedDecrement( &This->ref );
576 TRACE( "(%p) ref %d\n", This, ref );
578 if (ref == 0)
580 uninitialize_directinput_instance( This );
581 HeapFree( GetProcessHeap(), 0, This );
584 return ref;
587 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
589 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
590 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
593 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
595 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
597 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
599 if (!riid || !ppobj)
600 return E_POINTER;
602 *ppobj = NULL;
604 #if DIRECTINPUT_VERSION == 0x0700
605 if (IsEqualGUID( &IID_IUnknown, riid ) ||
606 IsEqualGUID( &IID_IDirectInputA, riid ) ||
607 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
608 IsEqualGUID( &IID_IDirectInput7A, riid ))
609 *ppobj = &This->IDirectInput7A_iface;
610 else if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
611 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
612 IsEqualGUID( &IID_IDirectInput7W, riid ))
613 *ppobj = &This->IDirectInput7W_iface;
615 #else
616 if (IsEqualGUID( &IID_IUnknown, riid ) ||
617 IsEqualGUID( &IID_IDirectInput8A, riid ))
618 *ppobj = &This->IDirectInput8A_iface;
620 else if (IsEqualGUID( &IID_IDirectInput8W, riid ))
621 *ppobj = &This->IDirectInput8W_iface;
623 #endif
625 if (IsEqualGUID( &IID_IDirectInputJoyConfig8, riid ))
626 *ppobj = &This->IDirectInputJoyConfig8_iface;
628 if(*ppobj)
630 IUnknown_AddRef( (IUnknown*)*ppobj );
631 return DI_OK;
634 WARN( "Unsupported interface: %s\n", debugstr_guid(riid));
635 return E_NOINTERFACE;
638 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
640 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
641 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
644 static LRESULT WINAPI di_em_win_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
646 IDirectInputDeviceImpl *dev;
647 RAWINPUT ri;
648 UINT size = sizeof(ri);
649 int rim = GET_RAWINPUT_CODE_WPARAM( wparam );
651 TRACE( "%p %d %lx %lx\n", hwnd, msg, wparam, lparam );
653 if (msg == WM_INPUT && (rim == RIM_INPUT || rim == RIM_INPUTSINK))
655 size = GetRawInputData( (HRAWINPUT)lparam, RID_INPUT, &ri, &size, sizeof(RAWINPUTHEADER) );
656 if (size == (UINT)-1 || size < sizeof(RAWINPUTHEADER))
657 WARN( "Unable to read raw input data\n" );
658 else if (ri.header.dwType == RIM_TYPEMOUSE)
660 EnterCriticalSection( &dinput_hook_crit );
661 LIST_FOR_EACH_ENTRY( dev, &acquired_rawmouse_list, IDirectInputDeviceImpl, entry )
662 dinput_mouse_rawinput_hook( &dev->IDirectInputDevice8A_iface, wparam, lparam, &ri );
663 LeaveCriticalSection( &dinput_hook_crit );
667 return DefWindowProcW( hwnd, msg, wparam, lparam );
670 static void register_di_em_win_class(void)
672 WNDCLASSEXW class;
674 memset(&class, 0, sizeof(class));
675 class.cbSize = sizeof(class);
676 class.lpfnWndProc = di_em_win_wndproc;
677 class.hInstance = DINPUT_instance;
678 class.lpszClassName = di_em_win_w;
680 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
681 WARN( "Unable to register message window class\n" );
684 static void unregister_di_em_win_class(void)
686 if (!UnregisterClassW( di_em_win_w, NULL ) && GetLastError() != ERROR_CLASS_DOES_NOT_EXIST)
687 WARN( "Unable to unregister message window class\n" );
690 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion)
692 if (!This->initialized)
694 This->dwVersion = dwVersion;
695 This->evsequence = 1;
697 list_init( &This->device_players );
699 /* Add self to the list of the IDirectInputs */
700 EnterCriticalSection( &dinput_hook_crit );
701 list_add_head( &direct_input_list, &This->entry );
702 LeaveCriticalSection( &dinput_hook_crit );
704 This->initialized = TRUE;
706 if (!check_hook_thread())
708 uninitialize_directinput_instance( This );
709 return DIERR_GENERIC;
713 return DI_OK;
716 static void uninitialize_directinput_instance(IDirectInputImpl *This)
718 if (This->initialized)
720 struct DevicePlayer *device_player, *device_player2;
721 /* Remove self from the list of the IDirectInputs */
722 EnterCriticalSection( &dinput_hook_crit );
723 list_remove( &This->entry );
724 LeaveCriticalSection( &dinput_hook_crit );
726 LIST_FOR_EACH_ENTRY_SAFE( device_player, device_player2,
727 &This->device_players, struct DevicePlayer, entry )
728 HeapFree(GetProcessHeap(), 0, device_player);
730 check_hook_thread();
732 This->initialized = FALSE;
736 enum directinput_versions
738 DIRECTINPUT_VERSION_300 = 0x0300,
739 DIRECTINPUT_VERSION_500 = 0x0500,
740 DIRECTINPUT_VERSION_50A = 0x050A,
741 DIRECTINPUT_VERSION_5B2 = 0x05B2,
742 DIRECTINPUT_VERSION_602 = 0x0602,
743 DIRECTINPUT_VERSION_61A = 0x061A,
744 DIRECTINPUT_VERSION_700 = 0x0700,
747 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD version)
749 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
751 TRACE("(%p)->(%p, 0x%04x)\n", This, hinst, version);
753 if (!hinst)
754 return DIERR_INVALIDPARAM;
755 else if (version == 0)
756 return DIERR_NOTINITIALIZED;
757 else if (version > DIRECTINPUT_VERSION_700)
758 return DIERR_OLDDIRECTINPUTVERSION;
759 else if (version != DIRECTINPUT_VERSION_300 && version != DIRECTINPUT_VERSION_500 &&
760 version != DIRECTINPUT_VERSION_50A && version != DIRECTINPUT_VERSION_5B2 &&
761 version != DIRECTINPUT_VERSION_602 && version != DIRECTINPUT_VERSION_61A &&
762 version != DIRECTINPUT_VERSION_700 && version != DIRECTINPUT_VERSION)
763 return DIERR_BETADIRECTINPUTVERSION;
765 return initialize_directinput_instance(This, version);
768 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
770 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
771 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
774 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
776 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
777 HRESULT hr;
778 LPDIRECTINPUTDEVICEA device;
780 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
782 if (!rguid) return E_POINTER;
783 if (!This->initialized)
784 return DIERR_NOTINITIALIZED;
786 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
787 if (hr != DI_OK) return DI_NOTATTACHED;
789 IUnknown_Release( device );
791 return DI_OK;
794 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
796 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
797 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
800 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
801 HWND hwndOwner,
802 DWORD dwFlags)
804 WCHAR control_exeW[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
805 STARTUPINFOW si = {0};
806 PROCESS_INFORMATION pi;
808 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
810 TRACE( "(%p)->(%p, %08x)\n", This, hwndOwner, dwFlags );
812 if (hwndOwner && !IsWindow(hwndOwner))
813 return E_HANDLE;
815 if (dwFlags)
816 return DIERR_INVALIDPARAM;
818 if (!This->initialized)
819 return DIERR_NOTINITIALIZED;
821 if (!CreateProcessW(NULL, control_exeW, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
822 return HRESULT_FROM_WIN32(GetLastError());
824 return DI_OK;
827 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
829 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
830 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
833 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
834 LPCSTR pszName, LPGUID pguidInstance)
836 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
838 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
840 return DI_OK;
843 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
844 LPCWSTR pszName, LPGUID pguidInstance)
846 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
848 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
850 return DI_OK;
853 static HRESULT create_device(IDirectInputImpl *This, REFGUID rguid, REFIID riid, LPVOID *pvOut, BOOL unicode)
855 unsigned int i;
857 if (pvOut)
858 *pvOut = NULL;
860 if (!rguid || !pvOut)
861 return E_POINTER;
863 if (!This->initialized)
864 return DIERR_NOTINITIALIZED;
866 /* Loop on all the devices to see if anyone matches the given GUID */
867 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
869 HRESULT ret;
871 if (!dinput_devices[i]->create_device) continue;
872 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, unicode)) == DI_OK)
873 return DI_OK;
876 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
877 return DIERR_DEVICENOTREG;
880 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
881 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
883 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
885 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
887 return create_device(This, rguid, riid, pvOut, FALSE);
890 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
891 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
893 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
895 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
897 return create_device(This, rguid, riid, pvOut, TRUE);
900 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
901 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
903 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
906 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
907 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
909 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
912 /*******************************************************************************
913 * DirectInput8
916 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
918 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
919 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
922 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
924 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
925 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
928 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
930 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
931 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
934 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
936 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
937 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
940 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
942 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
943 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
946 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
948 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
949 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
952 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
953 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
955 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
956 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
959 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
960 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
962 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
963 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
966 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
967 LPVOID pvRef, DWORD dwFlags)
969 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
970 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
973 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
974 LPVOID pvRef, DWORD dwFlags)
976 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
977 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
980 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
982 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
983 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
986 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
988 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
989 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
992 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
994 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
995 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
998 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
1000 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
1001 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
1004 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD version)
1006 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
1008 TRACE("(%p)->(%p, 0x%04x)\n", This, hinst, version);
1010 if (!hinst)
1011 return DIERR_INVALIDPARAM;
1012 else if (version == 0)
1013 return DIERR_NOTINITIALIZED;
1014 else if (version < DIRECTINPUT_VERSION)
1015 return DIERR_BETADIRECTINPUTVERSION;
1016 else if (version > DIRECTINPUT_VERSION)
1017 return DIERR_OLDDIRECTINPUTVERSION;
1019 return initialize_directinput_instance(This, version);
1022 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD version)
1024 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
1025 return IDirectInput8AImpl_Initialize( &This->IDirectInput8A_iface, hinst, version );
1028 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
1030 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
1031 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
1034 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
1036 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
1037 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
1040 static BOOL should_enumerate_device(const WCHAR *username, DWORD dwFlags,
1041 struct list *device_players, REFGUID guid)
1043 BOOL should_enumerate = TRUE;
1044 struct DevicePlayer *device_player;
1046 /* Check if user owns this device */
1047 if (dwFlags & DIEDBSFL_THISUSER && username && *username)
1049 should_enumerate = FALSE;
1050 LIST_FOR_EACH_ENTRY(device_player, device_players, struct DevicePlayer, entry)
1052 if (IsEqualGUID(&device_player->instance_guid, guid))
1054 if (*device_player->username && !lstrcmpW(username, device_player->username))
1055 return TRUE; /* Device username matches */
1056 break;
1061 /* Check if this device is not owned by anyone */
1062 if (dwFlags & DIEDBSFL_AVAILABLEDEVICES) {
1063 BOOL found = FALSE;
1064 should_enumerate = FALSE;
1065 LIST_FOR_EACH_ENTRY(device_player, device_players, struct DevicePlayer, entry)
1067 if (IsEqualGUID(&device_player->instance_guid, guid))
1069 if (*device_player->username)
1070 found = TRUE;
1071 break;
1074 if (!found)
1075 return TRUE; /* Device does not have a username */
1078 return should_enumerate;
1081 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
1082 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
1083 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
1084 LPVOID pvRef, DWORD dwFlags
1087 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
1088 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
1089 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
1090 DIDEVICEINSTANCEA didevi;
1091 LPDIRECTINPUTDEVICE8A lpdid;
1092 DWORD callbackFlags;
1093 int i, j;
1094 int device_count = 0;
1095 int remain;
1096 DIDEVICEINSTANCEA *didevis = 0;
1097 WCHAR *username_w = 0;
1099 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_a(ptszUserName), lpdiActionFormat,
1100 lpCallback, pvRef, dwFlags);
1101 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
1102 X(DIEDBSFL_ATTACHEDONLY)
1103 X(DIEDBSFL_THISUSER)
1104 X(DIEDBSFL_FORCEFEEDBACK)
1105 X(DIEDBSFL_AVAILABLEDEVICES)
1106 X(DIEDBSFL_MULTIMICEKEYBOARDS)
1107 X(DIEDBSFL_NONGAMINGDEVICES)
1108 #undef X
1110 _dump_diactionformatA(lpdiActionFormat);
1112 didevi.dwSize = sizeof(didevi);
1114 if (ptszUserName)
1116 int len = MultiByteToWideChar(CP_ACP, 0, ptszUserName, -1, 0, 0);
1118 username_w = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1119 MultiByteToWideChar(CP_ACP, 0, ptszUserName, -1, username_w, len);
1122 /* Enumerate all the joysticks */
1123 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
1125 HRESULT enumSuccess;
1127 if (!dinput_devices[i]->enum_deviceA) continue;
1129 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
1131 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
1133 /* Default behavior is to enumerate attached game controllers */
1134 enumSuccess = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
1135 if (enumSuccess == S_OK &&
1136 should_enumerate_device(username_w, dwFlags, &This->device_players, &didevi.guidInstance))
1138 if (device_count++)
1139 didevis = HeapReAlloc(GetProcessHeap(), 0, didevis, sizeof(DIDEVICEINSTANCEA)*device_count);
1140 else
1141 didevis = HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEA)*device_count);
1142 didevis[device_count-1] = didevi;
1147 remain = device_count;
1148 /* Add keyboard and mouse to remaining device count */
1149 if (!(dwFlags & DIEDBSFL_FORCEFEEDBACK))
1151 for (i = 0; i < ARRAY_SIZE(guids); i++)
1153 if (should_enumerate_device(username_w, dwFlags, &This->device_players, guids[i]))
1154 remain++;
1158 for (i = 0; i < device_count; i++)
1160 callbackFlags = diactionformat_priorityA(lpdiActionFormat, lpdiActionFormat->dwGenre);
1161 IDirectInput_CreateDevice(iface, &didevis[i].guidInstance, &lpdid, NULL);
1163 if (lpCallback(&didevis[i], lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1165 IDirectInputDevice_Release(lpdid);
1166 HeapFree(GetProcessHeap(), 0, didevis);
1167 HeapFree(GetProcessHeap(), 0, username_w);
1168 return DI_OK;
1170 IDirectInputDevice_Release(lpdid);
1173 HeapFree(GetProcessHeap(), 0, didevis);
1175 if (dwFlags & DIEDBSFL_FORCEFEEDBACK)
1177 HeapFree(GetProcessHeap(), 0, username_w);
1178 return DI_OK;
1181 /* Enumerate keyboard and mouse */
1182 for (i = 0; i < ARRAY_SIZE(guids); i++)
1184 if (should_enumerate_device(username_w, dwFlags, &This->device_players, guids[i]))
1186 callbackFlags = diactionformat_priorityA(lpdiActionFormat, actionMasks[i]);
1188 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1189 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1191 if (lpCallback(&didevi, lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1193 IDirectInputDevice_Release(lpdid);
1194 HeapFree(GetProcessHeap(), 0, username_w);
1195 return DI_OK;
1197 IDirectInputDevice_Release(lpdid);
1201 HeapFree(GetProcessHeap(), 0, username_w);
1202 return DI_OK;
1205 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
1206 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
1207 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
1208 LPVOID pvRef, DWORD dwFlags
1211 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
1212 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
1213 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1214 DIDEVICEINSTANCEW didevi;
1215 LPDIRECTINPUTDEVICE8W lpdid;
1216 DWORD callbackFlags;
1217 int i, j;
1218 int device_count = 0;
1219 int remain;
1220 DIDEVICEINSTANCEW *didevis = 0;
1222 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
1223 lpCallback, pvRef, dwFlags);
1225 didevi.dwSize = sizeof(didevi);
1227 /* Enumerate all the joysticks */
1228 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
1230 HRESULT enumSuccess;
1232 if (!dinput_devices[i]->enum_deviceW) continue;
1234 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
1236 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
1238 /* Default behavior is to enumerate attached game controllers */
1239 enumSuccess = dinput_devices[i]->enum_deviceW(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
1240 if (enumSuccess == S_OK &&
1241 should_enumerate_device(ptszUserName, dwFlags, &This->device_players, &didevi.guidInstance))
1243 if (device_count++)
1244 didevis = HeapReAlloc(GetProcessHeap(), 0, didevis, sizeof(DIDEVICEINSTANCEW)*device_count);
1245 else
1246 didevis = HeapAlloc(GetProcessHeap(), 0, sizeof(DIDEVICEINSTANCEW)*device_count);
1247 didevis[device_count-1] = didevi;
1252 remain = device_count;
1253 /* Add keyboard and mouse to remaining device count */
1254 if (!(dwFlags & DIEDBSFL_FORCEFEEDBACK))
1256 for (i = 0; i < ARRAY_SIZE(guids); i++)
1258 if (should_enumerate_device(ptszUserName, dwFlags, &This->device_players, guids[i]))
1259 remain++;
1263 for (i = 0; i < device_count; i++)
1265 callbackFlags = diactionformat_priorityW(lpdiActionFormat, lpdiActionFormat->dwGenre);
1266 IDirectInput_CreateDevice(iface, &didevis[i].guidInstance, &lpdid, NULL);
1268 if (lpCallback(&didevis[i], lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1270 HeapFree(GetProcessHeap(), 0, didevis);
1271 return DI_OK;
1275 HeapFree(GetProcessHeap(), 0, didevis);
1277 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
1279 /* Enumerate keyboard and mouse */
1280 for (i = 0; i < ARRAY_SIZE(guids); i++)
1282 if (should_enumerate_device(ptszUserName, dwFlags, &This->device_players, guids[i]))
1284 callbackFlags = diactionformat_priorityW(lpdiActionFormat, actionMasks[i]);
1286 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1287 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1289 if (lpCallback(&didevi, lpdid, callbackFlags, --remain, pvRef) == DIENUM_STOP)
1290 return DI_OK;
1294 return DI_OK;
1297 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
1298 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1299 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1302 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1304 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1306 /* Call helper function in config.c to do the real work */
1307 return _configure_devices(iface, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1310 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
1311 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1312 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1315 IDirectInputImpl *This = impl_from_IDirectInput8A(iface);
1316 DIACTIONFORMATW diafW;
1317 DICONFIGUREDEVICESPARAMSW diCDParamsW;
1318 HRESULT hr;
1319 int i;
1321 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1323 /* Copy parameters */
1324 diCDParamsW.dwSize = sizeof(DICONFIGUREDEVICESPARAMSW);
1325 diCDParamsW.dwcFormats = lpdiCDParams->dwcFormats;
1326 diCDParamsW.lprgFormats = &diafW;
1327 diCDParamsW.hwnd = lpdiCDParams->hwnd;
1329 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiCDParams->lprgFormats->dwNumActions);
1330 _copy_diactionformatAtoW(&diafW, lpdiCDParams->lprgFormats);
1332 /* Copy action names */
1333 for (i=0; i < diafW.dwNumActions; i++)
1335 const char* from = lpdiCDParams->lprgFormats->rgoAction[i].u.lptszActionName;
1336 int len = MultiByteToWideChar(CP_ACP, 0, from , -1, NULL , 0);
1337 WCHAR *to = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1339 MultiByteToWideChar(CP_ACP, 0, from , -1, to , len);
1340 diafW.rgoAction[i].u.lptszActionName = to;
1343 hr = IDirectInput8WImpl_ConfigureDevices(&This->IDirectInput8W_iface, lpdiCallback, &diCDParamsW, dwFlags, pvRefData);
1345 /* Copy back configuration */
1346 if (SUCCEEDED(hr))
1347 _copy_diactionformatWtoA(lpdiCDParams->lprgFormats, &diafW);
1349 /* Free memory */
1350 for (i=0; i < diafW.dwNumActions; i++)
1351 HeapFree(GetProcessHeap(), 0, (void*) diafW.rgoAction[i].u.lptszActionName);
1353 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1355 return hr;
1358 /*****************************************************************************
1359 * IDirectInputJoyConfig8 interface
1362 static inline IDirectInputImpl *impl_from_IDirectInputJoyConfig8(IDirectInputJoyConfig8 *iface)
1364 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInputJoyConfig8_iface );
1367 static HRESULT WINAPI JoyConfig8Impl_QueryInterface(IDirectInputJoyConfig8 *iface, REFIID riid, void** ppobj)
1369 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1370 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
1373 static ULONG WINAPI JoyConfig8Impl_AddRef(IDirectInputJoyConfig8 *iface)
1375 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1376 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
1379 static ULONG WINAPI JoyConfig8Impl_Release(IDirectInputJoyConfig8 *iface)
1381 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1382 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
1385 static HRESULT WINAPI JoyConfig8Impl_Acquire(IDirectInputJoyConfig8 *iface)
1387 FIXME( "(%p): stub!\n", iface );
1388 return E_NOTIMPL;
1391 static HRESULT WINAPI JoyConfig8Impl_Unacquire(IDirectInputJoyConfig8 *iface)
1393 FIXME( "(%p): stub!\n", iface );
1394 return E_NOTIMPL;
1397 static HRESULT WINAPI JoyConfig8Impl_SetCooperativeLevel(IDirectInputJoyConfig8 *iface, HWND hwnd, DWORD flags)
1399 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, hwnd, flags );
1400 return E_NOTIMPL;
1403 static HRESULT WINAPI JoyConfig8Impl_SendNotify(IDirectInputJoyConfig8 *iface)
1405 FIXME( "(%p): stub!\n", iface );
1406 return E_NOTIMPL;
1409 static HRESULT WINAPI JoyConfig8Impl_EnumTypes(IDirectInputJoyConfig8 *iface, LPDIJOYTYPECALLBACK cb, void *ref)
1411 FIXME( "(%p)->(%p, %p): stub!\n", iface, cb, ref );
1412 return E_NOTIMPL;
1415 static HRESULT WINAPI JoyConfig8Impl_GetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPDIJOYTYPEINFO info, DWORD flags)
1417 FIXME( "(%p)->(%s, %p, 0x%08x): stub!\n", iface, debugstr_w(name), info, flags );
1418 return E_NOTIMPL;
1421 static HRESULT WINAPI JoyConfig8Impl_SetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPCDIJOYTYPEINFO info, DWORD flags,
1422 LPWSTR new_name)
1424 FIXME( "(%p)->(%s, %p, 0x%08x, %s): stub!\n", iface, debugstr_w(name), info, flags, debugstr_w(new_name) );
1425 return E_NOTIMPL;
1428 static HRESULT WINAPI JoyConfig8Impl_DeleteType(IDirectInputJoyConfig8 *iface, LPCWSTR name)
1430 FIXME( "(%p)->(%s): stub!\n", iface, debugstr_w(name) );
1431 return E_NOTIMPL;
1434 static HRESULT WINAPI JoyConfig8Impl_GetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPDIJOYCONFIG info, DWORD flags)
1436 IDirectInputImpl *di = impl_from_IDirectInputJoyConfig8(iface);
1437 UINT found = 0;
1438 int i, j;
1439 HRESULT r;
1441 FIXME("(%p)->(%d, %p, 0x%08x): semi-stub!\n", iface, id, info, flags);
1443 #define X(x) if (flags & x) FIXME("\tflags |= "#x"\n");
1444 X(DIJC_GUIDINSTANCE)
1445 X(DIJC_REGHWCONFIGTYPE)
1446 X(DIJC_GAIN)
1447 X(DIJC_CALLOUT)
1448 #undef X
1450 /* Enumerate all joysticks in order */
1451 for (i = 0; i < ARRAY_SIZE(dinput_devices); i++)
1453 if (!dinput_devices[i]->enum_deviceA) continue;
1455 for (j = 0, r = S_OK; SUCCEEDED(r); j++)
1457 DIDEVICEINSTANCEA dev;
1458 dev.dwSize = sizeof(dev);
1459 if ((r = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, 0, &dev, di->dwVersion, j)) == S_OK)
1461 /* Only take into account the chosen id */
1462 if (found == id)
1464 if (flags & DIJC_GUIDINSTANCE)
1465 info->guidInstance = dev.guidInstance;
1467 return DI_OK;
1469 found += 1;
1474 return DIERR_NOMOREITEMS;
1477 static HRESULT WINAPI JoyConfig8Impl_SetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPCDIJOYCONFIG info, DWORD flags)
1479 FIXME( "(%p)->(%d, %p, 0x%08x): stub!\n", iface, id, info, flags );
1480 return E_NOTIMPL;
1483 static HRESULT WINAPI JoyConfig8Impl_DeleteConfig(IDirectInputJoyConfig8 *iface, UINT id)
1485 FIXME( "(%p)->(%d): stub!\n", iface, id );
1486 return E_NOTIMPL;
1489 static HRESULT WINAPI JoyConfig8Impl_GetUserValues(IDirectInputJoyConfig8 *iface, LPDIJOYUSERVALUES info, DWORD flags)
1491 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1492 return E_NOTIMPL;
1495 static HRESULT WINAPI JoyConfig8Impl_SetUserValues(IDirectInputJoyConfig8 *iface, LPCDIJOYUSERVALUES info, DWORD flags)
1497 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1498 return E_NOTIMPL;
1501 static HRESULT WINAPI JoyConfig8Impl_AddNewHardware(IDirectInputJoyConfig8 *iface, HWND hwnd, REFGUID guid)
1503 FIXME( "(%p)->(%p, %s): stub!\n", iface, hwnd, debugstr_guid(guid) );
1504 return E_NOTIMPL;
1507 static HRESULT WINAPI JoyConfig8Impl_OpenTypeKey(IDirectInputJoyConfig8 *iface, LPCWSTR name, DWORD security, PHKEY key)
1509 FIXME( "(%p)->(%s, 0x%08x, %p): stub!\n", iface, debugstr_w(name), security, key );
1510 return E_NOTIMPL;
1513 static HRESULT WINAPI JoyConfig8Impl_OpenAppStatusKey(IDirectInputJoyConfig8 *iface, PHKEY key)
1515 FIXME( "(%p)->(%p): stub!\n", iface, key );
1516 return E_NOTIMPL;
1519 static const IDirectInput7AVtbl ddi7avt = {
1520 IDirectInputAImpl_QueryInterface,
1521 IDirectInputAImpl_AddRef,
1522 IDirectInputAImpl_Release,
1523 IDirectInputAImpl_CreateDevice,
1524 IDirectInputAImpl_EnumDevices,
1525 IDirectInputAImpl_GetDeviceStatus,
1526 IDirectInputAImpl_RunControlPanel,
1527 IDirectInputAImpl_Initialize,
1528 IDirectInput2AImpl_FindDevice,
1529 IDirectInput7AImpl_CreateDeviceEx
1532 static const IDirectInput7WVtbl ddi7wvt = {
1533 IDirectInputWImpl_QueryInterface,
1534 IDirectInputWImpl_AddRef,
1535 IDirectInputWImpl_Release,
1536 IDirectInputWImpl_CreateDevice,
1537 IDirectInputWImpl_EnumDevices,
1538 IDirectInputWImpl_GetDeviceStatus,
1539 IDirectInputWImpl_RunControlPanel,
1540 IDirectInputWImpl_Initialize,
1541 IDirectInput2WImpl_FindDevice,
1542 IDirectInput7WImpl_CreateDeviceEx
1545 static const IDirectInput8AVtbl ddi8avt = {
1546 IDirectInput8AImpl_QueryInterface,
1547 IDirectInput8AImpl_AddRef,
1548 IDirectInput8AImpl_Release,
1549 IDirectInput8AImpl_CreateDevice,
1550 IDirectInput8AImpl_EnumDevices,
1551 IDirectInput8AImpl_GetDeviceStatus,
1552 IDirectInput8AImpl_RunControlPanel,
1553 IDirectInput8AImpl_Initialize,
1554 IDirectInput8AImpl_FindDevice,
1555 IDirectInput8AImpl_EnumDevicesBySemantics,
1556 IDirectInput8AImpl_ConfigureDevices
1559 static const IDirectInput8WVtbl ddi8wvt = {
1560 IDirectInput8WImpl_QueryInterface,
1561 IDirectInput8WImpl_AddRef,
1562 IDirectInput8WImpl_Release,
1563 IDirectInput8WImpl_CreateDevice,
1564 IDirectInput8WImpl_EnumDevices,
1565 IDirectInput8WImpl_GetDeviceStatus,
1566 IDirectInput8WImpl_RunControlPanel,
1567 IDirectInput8WImpl_Initialize,
1568 IDirectInput8WImpl_FindDevice,
1569 IDirectInput8WImpl_EnumDevicesBySemantics,
1570 IDirectInput8WImpl_ConfigureDevices
1573 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt =
1575 JoyConfig8Impl_QueryInterface,
1576 JoyConfig8Impl_AddRef,
1577 JoyConfig8Impl_Release,
1578 JoyConfig8Impl_Acquire,
1579 JoyConfig8Impl_Unacquire,
1580 JoyConfig8Impl_SetCooperativeLevel,
1581 JoyConfig8Impl_SendNotify,
1582 JoyConfig8Impl_EnumTypes,
1583 JoyConfig8Impl_GetTypeInfo,
1584 JoyConfig8Impl_SetTypeInfo,
1585 JoyConfig8Impl_DeleteType,
1586 JoyConfig8Impl_GetConfig,
1587 JoyConfig8Impl_SetConfig,
1588 JoyConfig8Impl_DeleteConfig,
1589 JoyConfig8Impl_GetUserValues,
1590 JoyConfig8Impl_SetUserValues,
1591 JoyConfig8Impl_AddNewHardware,
1592 JoyConfig8Impl_OpenTypeKey,
1593 JoyConfig8Impl_OpenAppStatusKey
1596 /*******************************************************************************
1597 * DirectInput ClassFactory
1599 typedef struct
1601 /* IUnknown fields */
1602 IClassFactory IClassFactory_iface;
1603 LONG ref;
1604 } IClassFactoryImpl;
1606 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1608 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1611 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1612 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1614 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1615 return E_NOINTERFACE;
1618 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
1619 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1620 return InterlockedIncrement(&(This->ref));
1623 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
1624 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1625 /* static class, won't be freed */
1626 return InterlockedDecrement(&(This->ref));
1629 static HRESULT WINAPI DICF_CreateInstance(
1630 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1632 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1634 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1635 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
1636 IsEqualGUID( &IID_IDirectInputA, riid ) ||
1637 IsEqualGUID( &IID_IDirectInputW, riid ) ||
1638 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
1639 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
1640 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
1641 IsEqualGUID( &IID_IDirectInput7W, riid ) ||
1642 IsEqualGUID( &IID_IDirectInput8A, riid ) ||
1643 IsEqualGUID( &IID_IDirectInput8W, riid ) )
1645 return create_directinput_instance(riid, ppobj, NULL);
1648 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1649 return E_NOINTERFACE;
1652 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1653 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1654 FIXME("(%p)->(%d),stub!\n",This,dolock);
1655 return S_OK;
1658 static const IClassFactoryVtbl DICF_Vtbl = {
1659 DICF_QueryInterface,
1660 DICF_AddRef,
1661 DICF_Release,
1662 DICF_CreateInstance,
1663 DICF_LockServer
1665 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
1667 /***********************************************************************
1668 * DllCanUnloadNow (DINPUT.@)
1670 HRESULT WINAPI DllCanUnloadNow(void)
1672 return S_FALSE;
1675 /***********************************************************************
1676 * DllGetClassObject (DINPUT.@)
1678 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1680 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1681 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1682 *ppv = &DINPUT_CF;
1683 IClassFactory_AddRef((IClassFactory*)*ppv);
1684 return S_OK;
1687 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1688 return CLASS_E_CLASSNOTAVAILABLE;
1691 /***********************************************************************
1692 * DllRegisterServer (DINPUT.@)
1694 HRESULT WINAPI DllRegisterServer(void)
1696 return __wine_register_resources( DINPUT_instance );
1699 /***********************************************************************
1700 * DllUnregisterServer (DINPUT.@)
1702 HRESULT WINAPI DllUnregisterServer(void)
1704 return __wine_unregister_resources( DINPUT_instance );
1707 /******************************************************************************
1708 * DInput hook thread
1711 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
1713 IDirectInputDeviceImpl *dev;
1714 int skip = 0;
1716 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
1718 EnterCriticalSection( &dinput_hook_crit );
1719 LIST_FOR_EACH_ENTRY( dev, &acquired_mouse_list, IDirectInputDeviceImpl, entry )
1721 TRACE("calling dinput_mouse_hook (%p %lx %lx)\n", dev, wparam, lparam);
1722 skip |= dinput_mouse_hook( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1724 LIST_FOR_EACH_ENTRY( dev, &acquired_keyboard_list, IDirectInputDeviceImpl, entry )
1726 if (dev->use_raw_input) continue;
1727 TRACE("calling dinput_keyboard_hook (%p %lx %lx)\n", dev, wparam, lparam);
1728 skip |= dinput_keyboard_hook( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1730 LeaveCriticalSection( &dinput_hook_crit );
1732 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
1735 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
1737 IDirectInputDeviceImpl *dev, *next;
1738 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
1739 HWND foreground;
1741 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1742 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1743 return CallNextHookEx( 0, code, wparam, lparam );
1745 foreground = GetForegroundWindow();
1747 EnterCriticalSection( &dinput_hook_crit );
1748 LIST_FOR_EACH_ENTRY_SAFE( dev, next, &acquired_device_list, IDirectInputDeviceImpl, entry )
1750 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1752 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1753 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1756 LIST_FOR_EACH_ENTRY_SAFE( dev, next, &acquired_mouse_list, IDirectInputDeviceImpl, entry )
1758 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1760 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1761 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1764 LIST_FOR_EACH_ENTRY_SAFE( dev, next, &acquired_rawmouse_list, IDirectInputDeviceImpl, entry )
1766 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1768 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1769 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1772 LIST_FOR_EACH_ENTRY_SAFE( dev, next, &acquired_keyboard_list, IDirectInputDeviceImpl, entry )
1774 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1776 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1777 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1780 LeaveCriticalSection( &dinput_hook_crit );
1782 return CallNextHookEx( 0, code, wparam, lparam );
1785 static DWORD WINAPI hook_thread_proc(void *param)
1787 static HHOOK kbd_hook, mouse_hook;
1788 MSG msg;
1790 di_em_win = CreateWindowW( di_em_win_w, di_em_win_w, 0, 0, 0, 0, 0,
1791 HWND_MESSAGE, 0, DINPUT_instance, NULL );
1793 /* Force creation of the message queue */
1794 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1795 SetEvent(param);
1797 while (GetMessageW( &msg, 0, 0, 0 ))
1799 UINT kbd_cnt = 0, mice_cnt = 0;
1801 if (msg.message == WM_USER+0x10)
1803 HANDLE finished_event = (HANDLE)msg.lParam;
1805 TRACE( "Processing hook change notification wp:%ld lp:%#lx\n", msg.wParam, msg.lParam );
1807 if (!msg.wParam)
1809 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1810 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1811 kbd_hook = mouse_hook = NULL;
1812 break;
1815 EnterCriticalSection( &dinput_hook_crit );
1816 kbd_cnt = list_count( &acquired_keyboard_list );
1817 mice_cnt = list_count( &acquired_mouse_list );
1818 LeaveCriticalSection( &dinput_hook_crit );
1820 if (kbd_cnt && !kbd_hook)
1821 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1822 else if (!kbd_cnt && kbd_hook)
1824 UnhookWindowsHookEx( kbd_hook );
1825 kbd_hook = NULL;
1828 if (mice_cnt && !mouse_hook)
1829 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1830 else if (!mice_cnt && mouse_hook)
1832 UnhookWindowsHookEx( mouse_hook );
1833 mouse_hook = NULL;
1836 if (finished_event)
1837 SetEvent(finished_event);
1839 TranslateMessage(&msg);
1840 DispatchMessageW(&msg);
1843 DestroyWindow( di_em_win );
1844 di_em_win = NULL;
1846 FreeLibraryAndExitThread(DINPUT_instance, 0);
1849 static DWORD hook_thread_id;
1850 static HANDLE hook_thread_event;
1852 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1854 0, 0, &dinput_hook_crit,
1855 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1856 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1858 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1860 static BOOL check_hook_thread(void)
1862 static HANDLE hook_thread;
1863 HMODULE module;
1864 HANDLE wait_handle = NULL;
1866 EnterCriticalSection(&dinput_hook_crit);
1868 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1869 if (!list_empty(&direct_input_list) && !hook_thread)
1871 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)DINPUT_instance, &module);
1872 hook_thread_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1873 hook_thread = CreateThread(NULL, 0, hook_thread_proc, hook_thread_event, 0, &hook_thread_id);
1875 else if (list_empty(&direct_input_list) && hook_thread)
1877 DWORD tid = hook_thread_id;
1879 if (hook_thread_event) /* if thread is not started yet */
1881 WaitForSingleObject(hook_thread_event, INFINITE);
1882 CloseHandle(hook_thread_event);
1883 hook_thread_event = NULL;
1886 hook_thread_id = 0;
1887 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1888 wait_handle = hook_thread;
1889 hook_thread = NULL;
1892 LeaveCriticalSection(&dinput_hook_crit);
1894 if (wait_handle)
1896 WaitForSingleObject(wait_handle, INFINITE);
1897 CloseHandle(wait_handle);
1899 return hook_thread_id != 0;
1902 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface, BOOL acquired)
1904 static HHOOK callwndproc_hook;
1905 static ULONG foreground_cnt;
1906 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1907 HANDLE hook_change_finished_event = NULL;
1909 EnterCriticalSection(&dinput_hook_crit);
1911 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1913 if (acquired)
1914 foreground_cnt++;
1915 else
1916 foreground_cnt--;
1919 if (foreground_cnt && !callwndproc_hook)
1920 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1921 DINPUT_instance, GetCurrentThreadId() );
1922 else if (!foreground_cnt && callwndproc_hook)
1924 UnhookWindowsHookEx( callwndproc_hook );
1925 callwndproc_hook = NULL;
1928 if (hook_thread_event) /* if thread is not started yet */
1930 WaitForSingleObject(hook_thread_event, INFINITE);
1931 CloseHandle(hook_thread_event);
1932 hook_thread_event = NULL;
1935 if (dev->use_raw_input)
1937 if (acquired)
1939 dev->raw_device.dwFlags = 0;
1940 if (dev->dwCoopLevel & DISCL_BACKGROUND)
1941 dev->raw_device.dwFlags |= RIDEV_INPUTSINK;
1942 if (dev->dwCoopLevel & DISCL_EXCLUSIVE)
1943 dev->raw_device.dwFlags |= RIDEV_NOLEGACY;
1944 if ((dev->dwCoopLevel & DISCL_EXCLUSIVE) && dev->raw_device.usUsage == 2)
1945 dev->raw_device.dwFlags |= RIDEV_CAPTUREMOUSE;
1946 if ((dev->dwCoopLevel & DISCL_EXCLUSIVE) && dev->raw_device.usUsage == 6)
1947 dev->raw_device.dwFlags |= RIDEV_NOHOTKEYS;
1948 dev->raw_device.hwndTarget = di_em_win;
1950 else
1952 dev->raw_device.dwFlags = RIDEV_REMOVE;
1953 dev->raw_device.hwndTarget = NULL;
1956 if (!RegisterRawInputDevices( &dev->raw_device, 1, sizeof(RAWINPUTDEVICE) ))
1957 WARN( "Unable to (un)register raw device %x:%x\n", dev->raw_device.usUsagePage, dev->raw_device.usUsage );
1960 if (acquired)
1961 hook_change_finished_event = CreateEventW( NULL, FALSE, FALSE, NULL );
1962 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, (LPARAM)hook_change_finished_event );
1964 LeaveCriticalSection(&dinput_hook_crit);
1966 if (acquired)
1968 WaitForSingleObject(hook_change_finished_event, INFINITE);
1969 CloseHandle(hook_change_finished_event);
1973 void check_dinput_events(void)
1975 /* Windows does not do that, but our current implementation of winex11
1976 * requires periodic event polling to forward events to the wineserver.
1978 * We have to call this function from multiple places, because:
1979 * - some games do not explicitly poll for mouse events
1980 * (for example Culpa Innata)
1981 * - some games only poll the device, and neither keyboard nor mouse
1982 * (for example Civilization: Call to Power 2)
1984 MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
1987 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved)
1989 switch(reason)
1991 case DLL_PROCESS_ATTACH:
1992 DisableThreadLibraryCalls(inst);
1993 DINPUT_instance = inst;
1994 register_di_em_win_class();
1995 break;
1996 case DLL_PROCESS_DETACH:
1997 if (reserved) break;
1998 unregister_di_em_win_class();
1999 DeleteCriticalSection(&dinput_hook_crit);
2000 break;
2002 return TRUE;