wpcap: Add version resource.
[wine/multimedia.git] / dlls / dinput / dinput_main.c
blob14d5285e00c9c4f4c13fed7a623fb9e853a58dd5
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_IDirectInputDevice8A(IDirectInputDevice8A *iface)
84 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface);
86 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
88 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
91 static const struct dinput_device *dinput_devices[] =
93 &mouse_device,
94 &keyboard_device,
95 &joystick_linuxinput_device,
96 &joystick_linux_device,
97 &joystick_osx_device
99 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
101 static HINSTANCE DINPUT_instance = NULL;
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 );
107 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion);
108 static void uninitialize_directinput_instance(IDirectInputImpl *This);
110 static HRESULT create_directinput_instance(REFIID riid, LPVOID *ppDI, IDirectInputImpl **out)
112 IDirectInputImpl *This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectInputImpl) );
113 HRESULT hr;
115 if (!This)
116 return E_OUTOFMEMORY;
118 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
119 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
120 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
121 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
122 This->IDirectInputJoyConfig8_iface.lpVtbl = &JoyConfig8vt;
124 hr = IDirectInput_QueryInterface( &This->IDirectInput7A_iface, riid, ppDI );
125 if (FAILED(hr))
127 HeapFree( GetProcessHeap(), 0, This );
128 return hr;
131 if (out) *out = This;
132 return DI_OK;
135 /******************************************************************************
136 * DirectInputCreateEx (DINPUT.@)
138 HRESULT WINAPI DirectInputCreateEx(
139 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
140 LPUNKNOWN punkOuter)
142 IDirectInputImpl *This;
143 HRESULT hr;
145 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
147 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
148 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
149 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
150 IsEqualGUID( &IID_IDirectInputW, riid ) ||
151 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
152 IsEqualGUID( &IID_IDirectInput7W, riid ))
154 hr = create_directinput_instance(riid, ppDI, &This);
155 if (FAILED(hr))
156 return hr;
158 else
159 return DIERR_NOINTERFACE;
161 hr = IDirectInput_Initialize( &This->IDirectInput7A_iface, hinst, dwVersion );
162 if (FAILED(hr))
164 IDirectInput_Release( &This->IDirectInput7A_iface );
165 *ppDI = NULL;
166 return hr;
169 return DI_OK;
172 /******************************************************************************
173 * DirectInputCreateA (DINPUT.@)
175 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
177 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
180 /******************************************************************************
181 * DirectInputCreateW (DINPUT.@)
183 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
185 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
188 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType)
190 switch (dwDevType) {
191 case 0: return "All devices";
192 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
193 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
194 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
195 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
196 default: return "Unknown";
200 static void _dump_EnumDevices_dwFlags(DWORD dwFlags)
202 if (TRACE_ON(dinput)) {
203 unsigned int i;
204 static const struct {
205 DWORD mask;
206 const char *name;
207 } flags[] = {
208 #define FE(x) { x, #x}
209 FE(DIEDFL_ALLDEVICES),
210 FE(DIEDFL_ATTACHEDONLY),
211 FE(DIEDFL_FORCEFEEDBACK),
212 FE(DIEDFL_INCLUDEALIASES),
213 FE(DIEDFL_INCLUDEPHANTOMS),
214 FE(DIEDFL_INCLUDEHIDDEN)
215 #undef FE
217 TRACE(" flags: ");
218 if (dwFlags == 0) {
219 TRACE("DIEDFL_ALLDEVICES\n");
220 return;
222 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
223 if (flags[i].mask & dwFlags)
224 TRACE("%s ",flags[i].name);
226 TRACE("\n");
229 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat)
231 unsigned int i;
233 TRACE("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
234 TRACE("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
235 TRACE("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
236 TRACE("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
237 TRACE("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
238 TRACE("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
239 TRACE("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
240 TRACE("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
241 TRACE("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
242 TRACE("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
243 TRACE("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
244 TRACE("diaf.ftTimeStamp ...\n");
245 TRACE("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
246 TRACE("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
247 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
249 TRACE("diaf.rgoAction[%u]:\n", i);
250 TRACE("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
251 TRACE("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
252 TRACE("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
253 TRACE("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
254 TRACE("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
255 TRACE("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
256 TRACE("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
260 void _copy_diactionformatAtoW(LPDIACTIONFORMATW to, LPDIACTIONFORMATA from)
262 int i;
264 to->dwSize = sizeof(DIACTIONFORMATW);
265 to->dwActionSize = sizeof(DIACTIONW);
266 to->dwDataSize = from->dwDataSize;
267 to->dwNumActions = from->dwNumActions;
268 to->guidActionMap = from->guidActionMap;
269 to->dwGenre = from->dwGenre;
270 to->dwBufferSize = from->dwBufferSize;
271 to->lAxisMin = from->lAxisMin;
272 to->lAxisMax = from->lAxisMax;
273 to->dwCRC = from->dwCRC;
274 to->ftTimeStamp = from->ftTimeStamp;
276 for (i=0; i < to->dwNumActions; i++)
278 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
279 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
280 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
281 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
282 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
283 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
287 void _copy_diactionformatWtoA(LPDIACTIONFORMATA to, LPDIACTIONFORMATW from)
289 int i;
291 to->dwSize = sizeof(DIACTIONFORMATA);
292 to->dwActionSize = sizeof(DIACTIONA);
293 to->dwDataSize = from->dwDataSize;
294 to->dwNumActions = from->dwNumActions;
295 to->guidActionMap = from->guidActionMap;
296 to->dwGenre = from->dwGenre;
297 to->dwBufferSize = from->dwBufferSize;
298 to->lAxisMin = from->lAxisMin;
299 to->lAxisMax = from->lAxisMax;
300 to->dwCRC = from->dwCRC;
301 to->ftTimeStamp = from->ftTimeStamp;
303 for (i=0; i < to->dwNumActions; i++)
305 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
306 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
307 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
308 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
309 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
310 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
314 /* diactionformat_priority
316 * Given a DIACTIONFORMAT structure and a DI genre, returns the enumeration
317 * priority. Joysticks should pass the game genre, and mouse or keyboard their
318 * respective DI*_MASK
320 static DWORD diactionformat_priorityA(LPDIACTIONFORMATA lpdiaf, DWORD genre)
322 int i;
323 DWORD priorityFlags = 0;
325 /* If there's at least one action for the device it's priority 1 */
326 for(i=0; i < lpdiaf->dwNumActions; i++)
327 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
328 priorityFlags |= DIEDBS_MAPPEDPRI1;
330 return priorityFlags;
333 static DWORD diactionformat_priorityW(LPDIACTIONFORMATW lpdiaf, DWORD genre)
335 int i;
336 DWORD priorityFlags = 0;
338 /* If there's at least one action for the device it's priority 1 */
339 for(i=0; i < lpdiaf->dwNumActions; i++)
340 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
341 priorityFlags |= DIEDBS_MAPPEDPRI1;
343 return priorityFlags;
346 /******************************************************************************
347 * IDirectInputA_EnumDevices
349 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
350 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
351 LPVOID pvRef, DWORD dwFlags)
353 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
354 DIDEVICEINSTANCEA devInstance;
355 unsigned int i;
356 int j, r;
358 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
359 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
360 lpCallback, pvRef, dwFlags);
361 _dump_EnumDevices_dwFlags(dwFlags);
363 if (!lpCallback ||
364 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
365 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
366 return DIERR_INVALIDPARAM;
368 if (!This->initialized)
369 return DIERR_NOTINITIALIZED;
371 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
372 if (!dinput_devices[i]->enum_deviceA) continue;
373 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
374 devInstance.dwSize = sizeof(devInstance);
375 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
376 r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
377 if (r == S_OK)
378 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
379 return S_OK;
383 return S_OK;
385 /******************************************************************************
386 * IDirectInputW_EnumDevices
388 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
389 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
390 LPVOID pvRef, DWORD dwFlags)
392 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
393 DIDEVICEINSTANCEW devInstance;
394 unsigned int i;
395 int j;
396 HRESULT r;
398 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
399 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
400 lpCallback, pvRef, dwFlags);
401 _dump_EnumDevices_dwFlags(dwFlags);
403 if (!lpCallback ||
404 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
405 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
406 return DIERR_INVALIDPARAM;
408 if (!This->initialized)
409 return DIERR_NOTINITIALIZED;
411 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
412 if (!dinput_devices[i]->enum_deviceW) continue;
413 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
414 devInstance.dwSize = sizeof(devInstance);
415 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
416 r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
417 if (r == S_OK)
418 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
419 return S_OK;
423 return S_OK;
426 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
428 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
429 ULONG ref = InterlockedIncrement(&This->ref);
431 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
432 return ref;
435 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
437 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
438 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
441 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
443 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
444 ULONG ref = InterlockedDecrement( &This->ref );
446 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
448 if (ref == 0)
450 uninitialize_directinput_instance( This );
451 HeapFree( GetProcessHeap(), 0, This );
454 return ref;
457 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
459 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
460 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
463 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
465 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
467 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
469 if (!riid || !ppobj)
470 return E_POINTER;
472 if (IsEqualGUID( &IID_IUnknown, riid ) ||
473 IsEqualGUID( &IID_IDirectInputA, riid ) ||
474 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
475 IsEqualGUID( &IID_IDirectInput7A, riid ))
477 *ppobj = &This->IDirectInput7A_iface;
478 IUnknown_AddRef( (IUnknown*)*ppobj );
480 return DI_OK;
483 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
484 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
485 IsEqualGUID( &IID_IDirectInput7W, riid ))
487 *ppobj = &This->IDirectInput7W_iface;
488 IUnknown_AddRef( (IUnknown*)*ppobj );
490 return DI_OK;
493 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
495 *ppobj = &This->IDirectInput8A_iface;
496 IUnknown_AddRef( (IUnknown*)*ppobj );
498 return DI_OK;
501 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
503 *ppobj = &This->IDirectInput8W_iface;
504 IUnknown_AddRef( (IUnknown*)*ppobj );
506 return DI_OK;
509 if (IsEqualGUID( &IID_IDirectInputJoyConfig8, riid ))
511 *ppobj = &This->IDirectInputJoyConfig8_iface;
512 IUnknown_AddRef( (IUnknown*)*ppobj );
514 return DI_OK;
517 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
518 *ppobj = NULL;
519 return E_NOINTERFACE;
522 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
524 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
525 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
528 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion)
530 if (!This->initialized)
532 This->dwVersion = dwVersion;
533 This->evsequence = 1;
535 InitializeCriticalSection( &This->crit );
536 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
538 list_init( &This->devices_list );
540 /* Add self to the list of the IDirectInputs */
541 EnterCriticalSection( &dinput_hook_crit );
542 list_add_head( &direct_input_list, &This->entry );
543 LeaveCriticalSection( &dinput_hook_crit );
545 This->initialized = TRUE;
547 if (!check_hook_thread())
549 uninitialize_directinput_instance( This );
550 return DIERR_GENERIC;
554 return DI_OK;
557 static void uninitialize_directinput_instance(IDirectInputImpl *This)
559 if (This->initialized)
561 /* Remove self from the list of the IDirectInputs */
562 EnterCriticalSection( &dinput_hook_crit );
563 list_remove( &This->entry );
564 LeaveCriticalSection( &dinput_hook_crit );
566 check_hook_thread();
568 This->crit.DebugInfo->Spare[0] = 0;
569 DeleteCriticalSection( &This->crit );
571 This->initialized = FALSE;
575 enum directinput_versions
577 DIRECTINPUT_VERSION_300 = 0x0300,
578 DIRECTINPUT_VERSION_500 = 0x0500,
579 DIRECTINPUT_VERSION_50A = 0x050A,
580 DIRECTINPUT_VERSION_5B2 = 0x05B2,
581 DIRECTINPUT_VERSION_602 = 0x0602,
582 DIRECTINPUT_VERSION_61A = 0x061A,
583 DIRECTINPUT_VERSION_700 = 0x0700,
586 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD version)
588 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
590 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
592 if (!hinst)
593 return DIERR_INVALIDPARAM;
594 else if (version == 0)
595 return DIERR_NOTINITIALIZED;
596 else if (version > DIRECTINPUT_VERSION_700)
597 return DIERR_OLDDIRECTINPUTVERSION;
598 else if (version != DIRECTINPUT_VERSION_300 && version != DIRECTINPUT_VERSION_500 &&
599 version != DIRECTINPUT_VERSION_50A && version != DIRECTINPUT_VERSION_5B2 &&
600 version != DIRECTINPUT_VERSION_602 && version != DIRECTINPUT_VERSION_61A &&
601 version != DIRECTINPUT_VERSION_700 && version != DIRECTINPUT_VERSION)
602 return DIERR_BETADIRECTINPUTVERSION;
604 return initialize_directinput_instance(This, version);
607 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
609 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
610 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
613 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
615 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
616 HRESULT hr;
617 LPDIRECTINPUTDEVICEA device;
619 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
621 if (!rguid) return E_POINTER;
622 if (!This->initialized)
623 return DIERR_NOTINITIALIZED;
625 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
626 if (hr != DI_OK) return DI_NOTATTACHED;
628 IUnknown_Release( device );
630 return DI_OK;
633 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
635 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
636 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
639 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
640 HWND hwndOwner,
641 DWORD dwFlags)
643 WCHAR control_exeW[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
644 STARTUPINFOW si = {0};
645 PROCESS_INFORMATION pi;
647 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
649 TRACE( "(%p)->(%p, %08x)\n", This, hwndOwner, dwFlags );
651 if (hwndOwner && !IsWindow(hwndOwner))
652 return E_HANDLE;
654 if (dwFlags)
655 return DIERR_INVALIDPARAM;
657 if (!This->initialized)
658 return DIERR_NOTINITIALIZED;
660 if (!CreateProcessW(NULL, control_exeW, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
661 return HRESULT_FROM_WIN32(GetLastError());
663 return DI_OK;
666 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
668 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
669 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
672 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
673 LPCSTR pszName, LPGUID pguidInstance)
675 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
677 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
679 return DI_OK;
682 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
683 LPCWSTR pszName, LPGUID pguidInstance)
685 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
687 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
689 return DI_OK;
692 static HRESULT create_device(IDirectInputImpl *This, REFGUID rguid, REFIID riid, LPVOID *pvOut, BOOL unicode)
694 unsigned int i;
696 if (pvOut)
697 *pvOut = NULL;
699 if (!rguid || !pvOut)
700 return E_POINTER;
702 if (!This->initialized)
703 return DIERR_NOTINITIALIZED;
705 /* Loop on all the devices to see if anyone matches the given GUID */
706 for (i = 0; i < NB_DINPUT_DEVICES; i++)
708 HRESULT ret;
710 if (!dinput_devices[i]->create_device) continue;
711 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, unicode)) == DI_OK)
712 return DI_OK;
715 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
716 return DIERR_DEVICENOTREG;
719 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
720 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
722 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
724 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
726 return create_device(This, rguid, riid, pvOut, FALSE);
729 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
730 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
732 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
734 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
736 return create_device(This, rguid, riid, pvOut, TRUE);
739 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
740 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
742 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
745 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
746 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
748 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
751 /*******************************************************************************
752 * DirectInput8
755 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
757 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
758 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
761 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
763 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
764 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
767 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
769 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
770 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
773 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
775 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
776 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
779 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
781 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
782 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
785 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
787 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
788 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
791 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
792 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
794 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
795 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
798 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
799 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
801 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
802 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
805 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
806 LPVOID pvRef, DWORD dwFlags)
808 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
809 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
812 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
813 LPVOID pvRef, DWORD dwFlags)
815 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
816 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
819 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
821 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
822 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
825 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
827 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
828 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
831 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
833 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
834 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
837 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
839 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
840 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
843 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD version)
845 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
847 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
849 if (!hinst)
850 return DIERR_INVALIDPARAM;
851 else if (version == 0)
852 return DIERR_NOTINITIALIZED;
853 else if (version < DIRECTINPUT_VERSION)
854 return DIERR_BETADIRECTINPUTVERSION;
855 else if (version > DIRECTINPUT_VERSION)
856 return DIERR_OLDDIRECTINPUTVERSION;
858 return initialize_directinput_instance(This, version);
861 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD version)
863 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
864 return IDirectInput8AImpl_Initialize( &This->IDirectInput8A_iface, hinst, version );
867 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
869 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
870 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
873 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
875 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
876 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
879 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
880 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
881 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
882 LPVOID pvRef, DWORD dwFlags
885 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
886 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
887 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
888 DIDEVICEINSTANCEA didevi;
889 LPDIRECTINPUTDEVICE8A lpdid;
890 DWORD callbackFlags;
891 int i, j;
894 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_a(ptszUserName), lpdiActionFormat,
895 lpCallback, pvRef, dwFlags);
896 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
897 X(DIEDBSFL_ATTACHEDONLY)
898 X(DIEDBSFL_THISUSER)
899 X(DIEDBSFL_FORCEFEEDBACK)
900 X(DIEDBSFL_AVAILABLEDEVICES)
901 X(DIEDBSFL_MULTIMICEKEYBOARDS)
902 X(DIEDBSFL_NONGAMINGDEVICES)
903 #undef X
905 _dump_diactionformatA(lpdiActionFormat);
907 didevi.dwSize = sizeof(didevi);
909 /* Enumerate all the joysticks */
910 for (i = 0; i < NB_DINPUT_DEVICES; i++)
912 HRESULT enumSuccess;
914 if (!dinput_devices[i]->enum_deviceA) continue;
916 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
918 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
920 callbackFlags = diactionformat_priorityA(lpdiActionFormat, lpdiActionFormat->dwGenre);
921 /* Default behavior is to enumerate attached game controllers */
922 enumSuccess = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
923 if (enumSuccess == S_OK)
925 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
927 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
928 return DI_OK;
933 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
935 /* Enumerate keyboard and mouse */
936 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
938 callbackFlags = diactionformat_priorityA(lpdiActionFormat, actionMasks[i]);
940 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
941 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
943 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
944 return DI_OK;
947 return DI_OK;
950 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
951 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
952 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
953 LPVOID pvRef, DWORD dwFlags
956 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
957 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
958 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
959 DIDEVICEINSTANCEW didevi;
960 LPDIRECTINPUTDEVICE8W lpdid;
961 DWORD callbackFlags;
962 int i, j;
964 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
965 lpCallback, pvRef, dwFlags);
967 didevi.dwSize = sizeof(didevi);
969 /* Enumerate all the joysticks */
970 for (i = 0; i < NB_DINPUT_DEVICES; i++)
972 HRESULT enumSuccess;
974 if (!dinput_devices[i]->enum_deviceW) continue;
976 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
978 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
980 callbackFlags = diactionformat_priorityW(lpdiActionFormat, lpdiActionFormat->dwGenre);
981 /* Default behavior is to enumerate attached game controllers */
982 enumSuccess = dinput_devices[i]->enum_deviceW(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
983 if (enumSuccess == S_OK)
985 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
987 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
988 return DI_OK;
993 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
995 /* Enumerate keyboard and mouse */
996 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
998 callbackFlags = diactionformat_priorityW(lpdiActionFormat, actionMasks[i]);
1000 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1001 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1003 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
1004 return DI_OK;
1007 return DI_OK;
1010 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
1011 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1012 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1015 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1017 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1019 /* Call helper function in config.c to do the real work */
1020 return _configure_devices(iface, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1023 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
1024 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1025 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1028 IDirectInputImpl *This = impl_from_IDirectInput8A(iface);
1029 DIACTIONFORMATW diafW;
1030 DICONFIGUREDEVICESPARAMSW diCDParamsW;
1031 HRESULT hr;
1032 int i;
1034 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1036 /* Copy parameters */
1037 diCDParamsW.dwSize = sizeof(DICONFIGUREDEVICESPARAMSW);
1038 diCDParamsW.dwcFormats = lpdiCDParams->dwcFormats;
1039 diCDParamsW.lprgFormats = &diafW;
1040 diCDParamsW.hwnd = lpdiCDParams->hwnd;
1042 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiCDParams->lprgFormats->dwNumActions);
1043 _copy_diactionformatAtoW(&diafW, lpdiCDParams->lprgFormats);
1045 /* Copy action names */
1046 for (i=0; i < diafW.dwNumActions; i++)
1048 const char* from = lpdiCDParams->lprgFormats->rgoAction[i].u.lptszActionName;
1049 int len = MultiByteToWideChar(CP_ACP, 0, from , -1, NULL , 0);
1050 WCHAR *to = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1052 MultiByteToWideChar(CP_ACP, 0, from , -1, to , len);
1053 diafW.rgoAction[i].u.lptszActionName = to;
1056 hr = IDirectInput8WImpl_ConfigureDevices(&This->IDirectInput8W_iface, lpdiCallback, &diCDParamsW, dwFlags, pvRefData);
1058 /* Copy back configuration */
1059 if (SUCCEEDED(hr))
1060 _copy_diactionformatWtoA(lpdiCDParams->lprgFormats, &diafW);
1062 /* Free memory */
1063 for (i=0; i < diafW.dwNumActions; i++)
1064 HeapFree(GetProcessHeap(), 0, (void*) diafW.rgoAction[i].u.lptszActionName);
1066 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1068 return hr;
1071 /*****************************************************************************
1072 * IDirectInputJoyConfig8 interface
1075 static inline IDirectInputImpl *impl_from_IDirectInputJoyConfig8(IDirectInputJoyConfig8 *iface)
1077 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInputJoyConfig8_iface );
1080 static HRESULT WINAPI JoyConfig8Impl_QueryInterface(IDirectInputJoyConfig8 *iface, REFIID riid, void** ppobj)
1082 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1083 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
1086 static ULONG WINAPI JoyConfig8Impl_AddRef(IDirectInputJoyConfig8 *iface)
1088 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1089 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
1092 static ULONG WINAPI JoyConfig8Impl_Release(IDirectInputJoyConfig8 *iface)
1094 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1095 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
1098 static HRESULT WINAPI JoyConfig8Impl_Acquire(IDirectInputJoyConfig8 *iface)
1100 FIXME( "(%p): stub!\n", iface );
1101 return E_NOTIMPL;
1104 static HRESULT WINAPI JoyConfig8Impl_Unacquire(IDirectInputJoyConfig8 *iface)
1106 FIXME( "(%p): stub!\n", iface );
1107 return E_NOTIMPL;
1110 static HRESULT WINAPI JoyConfig8Impl_SetCooperativeLevel(IDirectInputJoyConfig8 *iface, HWND hwnd, DWORD flags)
1112 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, hwnd, flags );
1113 return E_NOTIMPL;
1116 static HRESULT WINAPI JoyConfig8Impl_SendNotify(IDirectInputJoyConfig8 *iface)
1118 FIXME( "(%p): stub!\n", iface );
1119 return E_NOTIMPL;
1122 static HRESULT WINAPI JoyConfig8Impl_EnumTypes(IDirectInputJoyConfig8 *iface, LPDIJOYTYPECALLBACK cb, void *ref)
1124 FIXME( "(%p)->(%p, %p): stub!\n", iface, cb, ref );
1125 return E_NOTIMPL;
1128 static HRESULT WINAPI JoyConfig8Impl_GetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPDIJOYTYPEINFO info, DWORD flags)
1130 FIXME( "(%p)->(%s, %p, 0x%08x): stub!\n", iface, debugstr_w(name), info, flags );
1131 return E_NOTIMPL;
1134 static HRESULT WINAPI JoyConfig8Impl_SetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPCDIJOYTYPEINFO info, DWORD flags,
1135 LPWSTR new_name)
1137 FIXME( "(%p)->(%s, %p, 0x%08x, %s): stub!\n", iface, debugstr_w(name), info, flags, debugstr_w(new_name) );
1138 return E_NOTIMPL;
1141 static HRESULT WINAPI JoyConfig8Impl_DeleteType(IDirectInputJoyConfig8 *iface, LPCWSTR name)
1143 FIXME( "(%p)->(%s): stub!\n", iface, debugstr_w(name) );
1144 return E_NOTIMPL;
1147 static HRESULT WINAPI JoyConfig8Impl_GetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPDIJOYCONFIG info, DWORD flags)
1149 IDirectInputImpl *di = impl_from_IDirectInputJoyConfig8(iface);
1150 UINT found = 0;
1151 int i, j;
1152 HRESULT r;
1154 FIXME("(%p)->(%d, %p, 0x%08x): semi-stub!\n", iface, id, info, flags);
1156 #define X(x) if (flags & x) FIXME("\tflags |= "#x"\n");
1157 X(DIJC_GUIDINSTANCE)
1158 X(DIJC_REGHWCONFIGTYPE)
1159 X(DIJC_GAIN)
1160 X(DIJC_CALLOUT)
1161 #undef X
1163 /* Enumerate all joysticks in order */
1164 for (i = 0; i < NB_DINPUT_DEVICES; i++)
1166 if (!dinput_devices[i]->enum_deviceA) continue;
1168 for (j = 0, r = S_OK; SUCCEEDED(r); j++)
1170 DIDEVICEINSTANCEA dev;
1171 dev.dwSize = sizeof(dev);
1172 if ((r = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, 0, &dev, di->dwVersion, j)) == S_OK)
1174 /* Only take into account the chosen id */
1175 if (found == id)
1177 if (flags & DIJC_GUIDINSTANCE)
1178 info->guidInstance = dev.guidInstance;
1180 return DI_OK;
1182 found += 1;
1187 return DIERR_NOMOREITEMS;
1190 static HRESULT WINAPI JoyConfig8Impl_SetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPCDIJOYCONFIG info, DWORD flags)
1192 FIXME( "(%p)->(%d, %p, 0x%08x): stub!\n", iface, id, info, flags );
1193 return E_NOTIMPL;
1196 static HRESULT WINAPI JoyConfig8Impl_DeleteConfig(IDirectInputJoyConfig8 *iface, UINT id)
1198 FIXME( "(%p)->(%d): stub!\n", iface, id );
1199 return E_NOTIMPL;
1202 static HRESULT WINAPI JoyConfig8Impl_GetUserValues(IDirectInputJoyConfig8 *iface, LPDIJOYUSERVALUES info, DWORD flags)
1204 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1205 return E_NOTIMPL;
1208 static HRESULT WINAPI JoyConfig8Impl_SetUserValues(IDirectInputJoyConfig8 *iface, LPCDIJOYUSERVALUES info, DWORD flags)
1210 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1211 return E_NOTIMPL;
1214 static HRESULT WINAPI JoyConfig8Impl_AddNewHardware(IDirectInputJoyConfig8 *iface, HWND hwnd, REFGUID guid)
1216 FIXME( "(%p)->(%p, %s): stub!\n", iface, hwnd, debugstr_guid(guid) );
1217 return E_NOTIMPL;
1220 static HRESULT WINAPI JoyConfig8Impl_OpenTypeKey(IDirectInputJoyConfig8 *iface, LPCWSTR name, DWORD security, PHKEY key)
1222 FIXME( "(%p)->(%s, 0x%08x, %p): stub!\n", iface, debugstr_w(name), security, key );
1223 return E_NOTIMPL;
1226 static HRESULT WINAPI JoyConfig8Impl_OpenAppStatusKey(IDirectInputJoyConfig8 *iface, PHKEY key)
1228 FIXME( "(%p)->(%p): stub!\n", iface, key );
1229 return E_NOTIMPL;
1232 static const IDirectInput7AVtbl ddi7avt = {
1233 IDirectInputAImpl_QueryInterface,
1234 IDirectInputAImpl_AddRef,
1235 IDirectInputAImpl_Release,
1236 IDirectInputAImpl_CreateDevice,
1237 IDirectInputAImpl_EnumDevices,
1238 IDirectInputAImpl_GetDeviceStatus,
1239 IDirectInputAImpl_RunControlPanel,
1240 IDirectInputAImpl_Initialize,
1241 IDirectInput2AImpl_FindDevice,
1242 IDirectInput7AImpl_CreateDeviceEx
1245 static const IDirectInput7WVtbl ddi7wvt = {
1246 IDirectInputWImpl_QueryInterface,
1247 IDirectInputWImpl_AddRef,
1248 IDirectInputWImpl_Release,
1249 IDirectInputWImpl_CreateDevice,
1250 IDirectInputWImpl_EnumDevices,
1251 IDirectInputWImpl_GetDeviceStatus,
1252 IDirectInputWImpl_RunControlPanel,
1253 IDirectInputWImpl_Initialize,
1254 IDirectInput2WImpl_FindDevice,
1255 IDirectInput7WImpl_CreateDeviceEx
1258 static const IDirectInput8AVtbl ddi8avt = {
1259 IDirectInput8AImpl_QueryInterface,
1260 IDirectInput8AImpl_AddRef,
1261 IDirectInput8AImpl_Release,
1262 IDirectInput8AImpl_CreateDevice,
1263 IDirectInput8AImpl_EnumDevices,
1264 IDirectInput8AImpl_GetDeviceStatus,
1265 IDirectInput8AImpl_RunControlPanel,
1266 IDirectInput8AImpl_Initialize,
1267 IDirectInput8AImpl_FindDevice,
1268 IDirectInput8AImpl_EnumDevicesBySemantics,
1269 IDirectInput8AImpl_ConfigureDevices
1272 static const IDirectInput8WVtbl ddi8wvt = {
1273 IDirectInput8WImpl_QueryInterface,
1274 IDirectInput8WImpl_AddRef,
1275 IDirectInput8WImpl_Release,
1276 IDirectInput8WImpl_CreateDevice,
1277 IDirectInput8WImpl_EnumDevices,
1278 IDirectInput8WImpl_GetDeviceStatus,
1279 IDirectInput8WImpl_RunControlPanel,
1280 IDirectInput8WImpl_Initialize,
1281 IDirectInput8WImpl_FindDevice,
1282 IDirectInput8WImpl_EnumDevicesBySemantics,
1283 IDirectInput8WImpl_ConfigureDevices
1286 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt =
1288 JoyConfig8Impl_QueryInterface,
1289 JoyConfig8Impl_AddRef,
1290 JoyConfig8Impl_Release,
1291 JoyConfig8Impl_Acquire,
1292 JoyConfig8Impl_Unacquire,
1293 JoyConfig8Impl_SetCooperativeLevel,
1294 JoyConfig8Impl_SendNotify,
1295 JoyConfig8Impl_EnumTypes,
1296 JoyConfig8Impl_GetTypeInfo,
1297 JoyConfig8Impl_SetTypeInfo,
1298 JoyConfig8Impl_DeleteType,
1299 JoyConfig8Impl_GetConfig,
1300 JoyConfig8Impl_SetConfig,
1301 JoyConfig8Impl_DeleteConfig,
1302 JoyConfig8Impl_GetUserValues,
1303 JoyConfig8Impl_SetUserValues,
1304 JoyConfig8Impl_AddNewHardware,
1305 JoyConfig8Impl_OpenTypeKey,
1306 JoyConfig8Impl_OpenAppStatusKey
1309 /*******************************************************************************
1310 * DirectInput ClassFactory
1312 typedef struct
1314 /* IUnknown fields */
1315 IClassFactory IClassFactory_iface;
1316 LONG ref;
1317 } IClassFactoryImpl;
1319 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1321 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1324 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1325 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1327 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1328 return E_NOINTERFACE;
1331 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
1332 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1333 return InterlockedIncrement(&(This->ref));
1336 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
1337 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1338 /* static class, won't be freed */
1339 return InterlockedDecrement(&(This->ref));
1342 static HRESULT WINAPI DICF_CreateInstance(
1343 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1345 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1347 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1348 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
1349 IsEqualGUID( &IID_IDirectInputA, riid ) ||
1350 IsEqualGUID( &IID_IDirectInputW, riid ) ||
1351 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
1352 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
1353 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
1354 IsEqualGUID( &IID_IDirectInput7W, riid ) ) {
1355 return create_directinput_instance(riid, ppobj, NULL);
1358 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1359 return E_NOINTERFACE;
1362 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1363 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1364 FIXME("(%p)->(%d),stub!\n",This,dolock);
1365 return S_OK;
1368 static const IClassFactoryVtbl DICF_Vtbl = {
1369 DICF_QueryInterface,
1370 DICF_AddRef,
1371 DICF_Release,
1372 DICF_CreateInstance,
1373 DICF_LockServer
1375 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
1377 /***********************************************************************
1378 * DllCanUnloadNow (DINPUT.@)
1380 HRESULT WINAPI DllCanUnloadNow(void)
1382 return S_FALSE;
1385 /***********************************************************************
1386 * DllGetClassObject (DINPUT.@)
1388 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1390 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1391 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1392 *ppv = &DINPUT_CF;
1393 IClassFactory_AddRef((IClassFactory*)*ppv);
1394 return S_OK;
1397 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1398 return CLASS_E_CLASSNOTAVAILABLE;
1401 /***********************************************************************
1402 * DllRegisterServer (DINPUT.@)
1404 HRESULT WINAPI DllRegisterServer(void)
1406 return __wine_register_resources( DINPUT_instance );
1409 /***********************************************************************
1410 * DllUnregisterServer (DINPUT.@)
1412 HRESULT WINAPI DllUnregisterServer(void)
1414 return __wine_unregister_resources( DINPUT_instance );
1417 /******************************************************************************
1418 * DInput hook thread
1421 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
1423 IDirectInputImpl *dinput;
1424 int skip = 0;
1426 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
1428 EnterCriticalSection( &dinput_hook_crit );
1429 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1431 IDirectInputDeviceImpl *dev;
1433 EnterCriticalSection( &dinput->crit );
1434 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1435 if (dev->acquired && dev->event_proc)
1437 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
1438 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1440 LeaveCriticalSection( &dinput->crit );
1442 LeaveCriticalSection( &dinput_hook_crit );
1444 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
1447 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
1449 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
1450 IDirectInputImpl *dinput;
1451 HWND foreground;
1453 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1454 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1455 return CallNextHookEx( 0, code, wparam, lparam );
1457 foreground = GetForegroundWindow();
1459 EnterCriticalSection( &dinput_hook_crit );
1461 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1463 IDirectInputDeviceImpl *dev;
1465 EnterCriticalSection( &dinput->crit );
1466 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1468 if (!dev->acquired) continue;
1470 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1472 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1473 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1476 LeaveCriticalSection( &dinput->crit );
1478 LeaveCriticalSection( &dinput_hook_crit );
1480 return CallNextHookEx( 0, code, wparam, lparam );
1483 static DWORD WINAPI hook_thread_proc(void *param)
1485 static HHOOK kbd_hook, mouse_hook;
1486 MSG msg;
1488 /* Force creation of the message queue */
1489 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1490 SetEvent(*(LPHANDLE)param);
1492 while (GetMessageW( &msg, 0, 0, 0 ))
1494 UINT kbd_cnt = 0, mice_cnt = 0;
1496 if (msg.message == WM_USER+0x10)
1498 IDirectInputImpl *dinput;
1500 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
1502 if (!msg.wParam && !msg.lParam)
1504 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1505 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1506 kbd_hook = mouse_hook = NULL;
1507 break;
1510 EnterCriticalSection( &dinput_hook_crit );
1512 /* Count acquired keyboards and mice*/
1513 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1515 IDirectInputDeviceImpl *dev;
1517 EnterCriticalSection( &dinput->crit );
1518 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1520 if (!dev->acquired || !dev->event_proc) continue;
1522 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1523 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1524 kbd_cnt++;
1525 else
1526 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1527 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1528 mice_cnt++;
1530 LeaveCriticalSection( &dinput->crit );
1532 LeaveCriticalSection( &dinput_hook_crit );
1534 if (kbd_cnt && !kbd_hook)
1535 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1536 else if (!kbd_cnt && kbd_hook)
1538 UnhookWindowsHookEx( kbd_hook );
1539 kbd_hook = NULL;
1542 if (mice_cnt && !mouse_hook)
1543 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1544 else if (!mice_cnt && mouse_hook)
1546 UnhookWindowsHookEx( mouse_hook );
1547 mouse_hook = NULL;
1550 TranslateMessage(&msg);
1551 DispatchMessageW(&msg);
1554 return 0;
1557 static DWORD hook_thread_id;
1559 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1561 0, 0, &dinput_hook_crit,
1562 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1563 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1565 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1567 static BOOL check_hook_thread(void)
1569 static HANDLE hook_thread;
1571 EnterCriticalSection(&dinput_hook_crit);
1573 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1574 if (!list_empty(&direct_input_list) && !hook_thread)
1576 HANDLE event;
1578 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1579 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1580 if (event && hook_thread)
1582 HANDLE handles[2];
1583 handles[0] = event;
1584 handles[1] = hook_thread;
1585 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1587 LeaveCriticalSection(&dinput_hook_crit);
1588 CloseHandle(event);
1590 else if (list_empty(&direct_input_list) && hook_thread)
1592 DWORD tid = hook_thread_id;
1594 hook_thread_id = 0;
1595 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1596 LeaveCriticalSection(&dinput_hook_crit);
1598 /* wait for hook thread to exit */
1599 WaitForSingleObject(hook_thread, INFINITE);
1600 CloseHandle(hook_thread);
1601 hook_thread = NULL;
1603 else
1604 LeaveCriticalSection(&dinput_hook_crit);
1606 return hook_thread_id != 0;
1609 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1611 static HHOOK callwndproc_hook;
1612 static ULONG foreground_cnt;
1613 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1615 EnterCriticalSection(&dinput_hook_crit);
1617 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1619 if (dev->acquired)
1620 foreground_cnt++;
1621 else
1622 foreground_cnt--;
1625 if (foreground_cnt && !callwndproc_hook)
1626 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1627 DINPUT_instance, GetCurrentThreadId() );
1628 else if (!foreground_cnt && callwndproc_hook)
1630 UnhookWindowsHookEx( callwndproc_hook );
1631 callwndproc_hook = NULL;
1634 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1636 LeaveCriticalSection(&dinput_hook_crit);
1639 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved)
1641 switch(reason)
1643 case DLL_PROCESS_ATTACH:
1644 DisableThreadLibraryCalls(inst);
1645 DINPUT_instance = inst;
1646 break;
1647 case DLL_PROCESS_DETACH:
1648 if (reserved) break;
1649 DeleteCriticalSection(&dinput_hook_crit);
1650 break;
1652 return TRUE;