taskmgr: Remove dead code.
[wine/multimedia.git] / dlls / dinput / dinput_main.c
blob18840b2963c6b49757c161db75878fd83c63e54c
1 /* DirectInput
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2002 TransGaming Technologies Inc.
6 * Copyright 2007 Vitaliy Margolen
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* Status:
25 * - Tomb Raider 2 Demo:
26 * Playable using keyboard only.
27 * - WingCommander Prophecy Demo:
28 * Doesn't get Input Focus.
30 * - Fallout : works great in X and DGA mode
33 #include "config.h"
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <string.h>
38 #define COBJMACROS
39 #define NONAMELESSUNION
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winuser.h"
46 #include "winerror.h"
47 #include "objbase.h"
48 #include "rpcproxy.h"
49 #include "initguid.h"
50 #include "dinput_private.h"
51 #include "device_private.h"
52 #include "dinputd.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
56 static const IDirectInput7AVtbl ddi7avt;
57 static const IDirectInput7WVtbl ddi7wvt;
58 static const IDirectInput8AVtbl ddi8avt;
59 static const IDirectInput8WVtbl ddi8wvt;
60 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt;
62 static inline IDirectInputImpl *impl_from_IDirectInput7A( IDirectInput7A *iface )
64 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7A_iface );
67 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
69 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7W_iface );
72 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
74 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8A_iface );
77 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
79 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8W_iface );
82 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
84 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
87 static const struct dinput_device *dinput_devices[] =
89 &mouse_device,
90 &keyboard_device,
91 &joystick_linuxinput_device,
92 &joystick_linux_device,
93 &joystick_osx_device
95 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
97 static HINSTANCE DINPUT_instance = NULL;
99 static BOOL check_hook_thread(void);
100 static CRITICAL_SECTION dinput_hook_crit;
101 static struct list direct_input_list = LIST_INIT( direct_input_list );
103 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion);
104 static void uninitialize_directinput_instance(IDirectInputImpl *This);
106 static HRESULT create_directinput_instance(REFIID riid, LPVOID *ppDI, IDirectInputImpl **out)
108 IDirectInputImpl *This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectInputImpl) );
109 HRESULT hr;
111 if (!This)
112 return E_OUTOFMEMORY;
114 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
115 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
116 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
117 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
118 This->IDirectInputJoyConfig8_iface.lpVtbl = &JoyConfig8vt;
120 hr = IDirectInput_QueryInterface( &This->IDirectInput7A_iface, riid, ppDI );
121 if (FAILED(hr))
123 HeapFree( GetProcessHeap(), 0, This );
124 return hr;
127 if (out) *out = This;
128 return DI_OK;
131 /******************************************************************************
132 * DirectInputCreateEx (DINPUT.@)
134 HRESULT WINAPI DirectInputCreateEx(
135 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
136 LPUNKNOWN punkOuter)
138 IDirectInputImpl *This;
139 HRESULT hr;
141 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
143 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
144 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
145 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
146 IsEqualGUID( &IID_IDirectInputW, riid ) ||
147 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
148 IsEqualGUID( &IID_IDirectInput7W, riid ))
150 hr = create_directinput_instance(riid, ppDI, &This);
151 if (FAILED(hr))
152 return hr;
154 else
155 return DIERR_NOINTERFACE;
157 hr = IDirectInput_Initialize( &This->IDirectInput7A_iface, hinst, dwVersion );
158 if (FAILED(hr))
160 IDirectInput_Release( &This->IDirectInput7A_iface );
161 *ppDI = NULL;
162 return hr;
165 return DI_OK;
168 /******************************************************************************
169 * DirectInputCreateA (DINPUT.@)
171 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
173 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
176 /******************************************************************************
177 * DirectInputCreateW (DINPUT.@)
179 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
181 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
184 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType)
186 switch (dwDevType) {
187 case 0: return "All devices";
188 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
189 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
190 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
191 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
192 default: return "Unknown";
196 static void _dump_EnumDevices_dwFlags(DWORD dwFlags)
198 if (TRACE_ON(dinput)) {
199 unsigned int i;
200 static const struct {
201 DWORD mask;
202 const char *name;
203 } flags[] = {
204 #define FE(x) { x, #x}
205 FE(DIEDFL_ALLDEVICES),
206 FE(DIEDFL_ATTACHEDONLY),
207 FE(DIEDFL_FORCEFEEDBACK),
208 FE(DIEDFL_INCLUDEALIASES),
209 FE(DIEDFL_INCLUDEPHANTOMS),
210 FE(DIEDFL_INCLUDEHIDDEN)
211 #undef FE
213 TRACE(" flags: ");
214 if (dwFlags == 0) {
215 TRACE("DIEDFL_ALLDEVICES\n");
216 return;
218 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
219 if (flags[i].mask & dwFlags)
220 TRACE("%s ",flags[i].name);
222 TRACE("\n");
225 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat)
227 unsigned int i;
229 TRACE("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
230 TRACE("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
231 TRACE("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
232 TRACE("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
233 TRACE("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
234 TRACE("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
235 TRACE("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
236 TRACE("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
237 TRACE("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
238 TRACE("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
239 TRACE("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
240 TRACE("diaf.ftTimeStamp ...\n");
241 TRACE("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
242 TRACE("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
243 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
245 TRACE("diaf.rgoAction[%u]:\n", i);
246 TRACE("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
247 TRACE("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
248 TRACE("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
249 TRACE("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
250 TRACE("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
251 TRACE("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
252 TRACE("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
256 void _copy_diactionformatAtoW(LPDIACTIONFORMATW to, LPDIACTIONFORMATA from)
258 int i;
260 to->dwSize = sizeof(DIACTIONFORMATW);
261 to->dwActionSize = sizeof(DIACTIONW);
262 to->dwDataSize = from->dwDataSize;
263 to->dwNumActions = from->dwNumActions;
264 to->guidActionMap = from->guidActionMap;
265 to->dwGenre = from->dwGenre;
266 to->dwBufferSize = from->dwBufferSize;
267 to->lAxisMin = from->lAxisMin;
268 to->lAxisMax = from->lAxisMax;
269 to->dwCRC = from->dwCRC;
270 to->ftTimeStamp = from->ftTimeStamp;
272 for (i=0; i < to->dwNumActions; i++)
274 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
275 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
276 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
277 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
278 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
279 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
283 void _copy_diactionformatWtoA(LPDIACTIONFORMATA to, LPDIACTIONFORMATW from)
285 int i;
287 to->dwSize = sizeof(DIACTIONFORMATA);
288 to->dwActionSize = sizeof(DIACTIONA);
289 to->dwDataSize = from->dwDataSize;
290 to->dwNumActions = from->dwNumActions;
291 to->guidActionMap = from->guidActionMap;
292 to->dwGenre = from->dwGenre;
293 to->dwBufferSize = from->dwBufferSize;
294 to->lAxisMin = from->lAxisMin;
295 to->lAxisMax = from->lAxisMax;
296 to->dwCRC = from->dwCRC;
297 to->ftTimeStamp = from->ftTimeStamp;
299 for (i=0; i < to->dwNumActions; i++)
301 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
302 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
303 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
304 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
305 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
306 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
310 /* diactionformat_priority
312 * Given a DIACTIONFORMAT structure and a DI genre, returns the enumeration
313 * priority. Joysticks should pass the game genre, and mouse or keyboard their
314 * respective DI*_MASK
316 static DWORD diactionformat_priorityA(LPDIACTIONFORMATA lpdiaf, DWORD genre)
318 int i;
319 DWORD priorityFlags = 0;
321 /* If there's at least one action for the device it's priority 1 */
322 for(i=0; i < lpdiaf->dwNumActions; i++)
323 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
324 priorityFlags |= DIEDBS_MAPPEDPRI1;
326 return priorityFlags;
329 static DWORD diactionformat_priorityW(LPDIACTIONFORMATW lpdiaf, DWORD genre)
331 int i;
332 DWORD priorityFlags = 0;
334 /* If there's at least one action for the device it's priority 1 */
335 for(i=0; i < lpdiaf->dwNumActions; i++)
336 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
337 priorityFlags |= DIEDBS_MAPPEDPRI1;
339 return priorityFlags;
342 /******************************************************************************
343 * IDirectInputA_EnumDevices
345 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
346 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
347 LPVOID pvRef, DWORD dwFlags)
349 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
350 DIDEVICEINSTANCEA devInstance;
351 unsigned int i;
352 int j, r;
354 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
355 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
356 lpCallback, pvRef, dwFlags);
357 _dump_EnumDevices_dwFlags(dwFlags);
359 if (!lpCallback ||
360 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
361 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
362 return DIERR_INVALIDPARAM;
364 if (!This->initialized)
365 return DIERR_NOTINITIALIZED;
367 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
368 if (!dinput_devices[i]->enum_deviceA) continue;
369 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
370 devInstance.dwSize = sizeof(devInstance);
371 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
372 r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
373 if (r == S_OK)
374 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
375 return S_OK;
379 return S_OK;
381 /******************************************************************************
382 * IDirectInputW_EnumDevices
384 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
385 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
386 LPVOID pvRef, DWORD dwFlags)
388 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
389 DIDEVICEINSTANCEW devInstance;
390 unsigned int i;
391 int j;
392 HRESULT r;
394 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
395 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
396 lpCallback, pvRef, dwFlags);
397 _dump_EnumDevices_dwFlags(dwFlags);
399 if (!lpCallback ||
400 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
401 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
402 return DIERR_INVALIDPARAM;
404 if (!This->initialized)
405 return DIERR_NOTINITIALIZED;
407 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
408 if (!dinput_devices[i]->enum_deviceW) continue;
409 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
410 devInstance.dwSize = sizeof(devInstance);
411 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
412 r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
413 if (r == S_OK)
414 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
415 return S_OK;
419 return S_OK;
422 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
424 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
425 ULONG ref = InterlockedIncrement(&This->ref);
427 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
428 return ref;
431 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
433 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
434 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
437 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
439 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
440 ULONG ref = InterlockedDecrement( &This->ref );
442 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
444 if (ref == 0)
446 uninitialize_directinput_instance( This );
447 HeapFree( GetProcessHeap(), 0, This );
450 return ref;
453 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
455 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
456 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
459 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
461 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
463 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
465 if (!riid || !ppobj)
466 return E_POINTER;
468 if (IsEqualGUID( &IID_IUnknown, riid ) ||
469 IsEqualGUID( &IID_IDirectInputA, riid ) ||
470 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
471 IsEqualGUID( &IID_IDirectInput7A, riid ))
473 *ppobj = &This->IDirectInput7A_iface;
474 IUnknown_AddRef( (IUnknown*)*ppobj );
476 return DI_OK;
479 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
480 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
481 IsEqualGUID( &IID_IDirectInput7W, riid ))
483 *ppobj = &This->IDirectInput7W_iface;
484 IUnknown_AddRef( (IUnknown*)*ppobj );
486 return DI_OK;
489 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
491 *ppobj = &This->IDirectInput8A_iface;
492 IUnknown_AddRef( (IUnknown*)*ppobj );
494 return DI_OK;
497 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
499 *ppobj = &This->IDirectInput8W_iface;
500 IUnknown_AddRef( (IUnknown*)*ppobj );
502 return DI_OK;
505 if (IsEqualGUID( &IID_IDirectInputJoyConfig8, riid ))
507 *ppobj = &This->IDirectInputJoyConfig8_iface;
508 IUnknown_AddRef( (IUnknown*)*ppobj );
510 return DI_OK;
513 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
514 *ppobj = NULL;
515 return E_NOINTERFACE;
518 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
520 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
521 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
524 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion)
526 if (!This->initialized)
528 This->dwVersion = dwVersion;
529 This->evsequence = 1;
531 InitializeCriticalSection( &This->crit );
532 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
534 list_init( &This->devices_list );
536 /* Add self to the list of the IDirectInputs */
537 EnterCriticalSection( &dinput_hook_crit );
538 list_add_head( &direct_input_list, &This->entry );
539 LeaveCriticalSection( &dinput_hook_crit );
541 This->initialized = TRUE;
543 if (!check_hook_thread())
545 uninitialize_directinput_instance( This );
546 return DIERR_GENERIC;
550 return DI_OK;
553 static void uninitialize_directinput_instance(IDirectInputImpl *This)
555 if (This->initialized)
557 /* Remove self from the list of the IDirectInputs */
558 EnterCriticalSection( &dinput_hook_crit );
559 list_remove( &This->entry );
560 LeaveCriticalSection( &dinput_hook_crit );
562 check_hook_thread();
564 This->crit.DebugInfo->Spare[0] = 0;
565 DeleteCriticalSection( &This->crit );
567 This->initialized = FALSE;
571 enum directinput_versions
573 DIRECTINPUT_VERSION_300 = 0x0300,
574 DIRECTINPUT_VERSION_500 = 0x0500,
575 DIRECTINPUT_VERSION_50A = 0x050A,
576 DIRECTINPUT_VERSION_5B2 = 0x05B2,
577 DIRECTINPUT_VERSION_602 = 0x0602,
578 DIRECTINPUT_VERSION_61A = 0x061A,
579 DIRECTINPUT_VERSION_700 = 0x0700,
582 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD version)
584 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
586 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
588 if (!hinst)
589 return DIERR_INVALIDPARAM;
590 else if (version == 0)
591 return DIERR_NOTINITIALIZED;
592 else if (version > DIRECTINPUT_VERSION_700)
593 return DIERR_OLDDIRECTINPUTVERSION;
594 else if (version != DIRECTINPUT_VERSION_300 && version != DIRECTINPUT_VERSION_500 &&
595 version != DIRECTINPUT_VERSION_50A && version != DIRECTINPUT_VERSION_5B2 &&
596 version != DIRECTINPUT_VERSION_602 && version != DIRECTINPUT_VERSION_61A &&
597 version != DIRECTINPUT_VERSION_700 && version != DIRECTINPUT_VERSION)
598 return DIERR_BETADIRECTINPUTVERSION;
600 return initialize_directinput_instance(This, version);
603 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
605 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
606 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
609 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
611 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
612 HRESULT hr;
613 LPDIRECTINPUTDEVICEA device;
615 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
617 if (!rguid) return E_POINTER;
618 if (!This->initialized)
619 return DIERR_NOTINITIALIZED;
621 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
622 if (hr != DI_OK) return DI_NOTATTACHED;
624 IUnknown_Release( device );
626 return DI_OK;
629 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
631 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
632 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
635 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
636 HWND hwndOwner,
637 DWORD dwFlags)
639 WCHAR control_exeW[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
640 STARTUPINFOW si = {0};
641 PROCESS_INFORMATION pi;
643 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
645 TRACE( "(%p)->(%p, %08x)\n", This, hwndOwner, dwFlags );
647 if (hwndOwner && !IsWindow(hwndOwner))
648 return E_HANDLE;
650 if (dwFlags)
651 return DIERR_INVALIDPARAM;
653 if (!This->initialized)
654 return DIERR_NOTINITIALIZED;
656 if (!CreateProcessW(NULL, control_exeW, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
657 return HRESULT_FROM_WIN32(GetLastError());
659 return DI_OK;
662 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
664 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
665 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
668 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
669 LPCSTR pszName, LPGUID pguidInstance)
671 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
673 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
675 return DI_OK;
678 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
679 LPCWSTR pszName, LPGUID pguidInstance)
681 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
683 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
685 return DI_OK;
688 static HRESULT create_device(IDirectInputImpl *This, REFGUID rguid, REFIID riid, LPVOID *pvOut, BOOL unicode)
690 unsigned int i;
692 if (pvOut)
693 *pvOut = NULL;
695 if (!rguid || !pvOut)
696 return E_POINTER;
698 if (!This->initialized)
699 return DIERR_NOTINITIALIZED;
701 /* Loop on all the devices to see if anyone matches the given GUID */
702 for (i = 0; i < NB_DINPUT_DEVICES; i++)
704 HRESULT ret;
706 if (!dinput_devices[i]->create_device) continue;
707 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, unicode)) == DI_OK)
708 return DI_OK;
711 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
712 return DIERR_DEVICENOTREG;
715 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
716 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
718 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
720 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
722 return create_device(This, rguid, riid, pvOut, FALSE);
725 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
726 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
728 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
730 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
732 return create_device(This, rguid, riid, pvOut, TRUE);
735 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
736 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
738 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
741 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
742 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
744 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
747 /*******************************************************************************
748 * DirectInput8
751 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
753 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
754 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
757 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
759 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
760 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
763 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
765 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
766 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
769 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
771 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
772 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
775 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
777 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
778 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
781 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
783 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
784 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
787 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
788 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
790 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
791 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
794 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
795 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
797 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
798 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
801 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
802 LPVOID pvRef, DWORD dwFlags)
804 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
805 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
808 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
809 LPVOID pvRef, DWORD dwFlags)
811 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
812 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
815 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
817 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
818 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
821 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
823 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
824 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
827 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
829 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
830 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
833 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
835 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
836 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
839 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD version)
841 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
843 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
845 if (!hinst)
846 return DIERR_INVALIDPARAM;
847 else if (version == 0)
848 return DIERR_NOTINITIALIZED;
849 else if (version < DIRECTINPUT_VERSION)
850 return DIERR_BETADIRECTINPUTVERSION;
851 else if (version > DIRECTINPUT_VERSION)
852 return DIERR_OLDDIRECTINPUTVERSION;
854 return initialize_directinput_instance(This, version);
857 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD version)
859 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
860 return IDirectInput8AImpl_Initialize( &This->IDirectInput8A_iface, hinst, version );
863 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
865 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
866 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
869 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
871 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
872 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
875 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
876 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
877 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
878 LPVOID pvRef, DWORD dwFlags
881 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
882 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
883 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
884 DIDEVICEINSTANCEA didevi;
885 LPDIRECTINPUTDEVICE8A lpdid;
886 DWORD callbackFlags;
887 int i, j;
890 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_a(ptszUserName), lpdiActionFormat,
891 lpCallback, pvRef, dwFlags);
892 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
893 X(DIEDBSFL_ATTACHEDONLY)
894 X(DIEDBSFL_THISUSER)
895 X(DIEDBSFL_FORCEFEEDBACK)
896 X(DIEDBSFL_AVAILABLEDEVICES)
897 X(DIEDBSFL_MULTIMICEKEYBOARDS)
898 X(DIEDBSFL_NONGAMINGDEVICES)
899 #undef X
901 _dump_diactionformatA(lpdiActionFormat);
903 didevi.dwSize = sizeof(didevi);
905 /* Enumerate all the joysticks */
906 for (i = 0; i < NB_DINPUT_DEVICES; i++)
908 HRESULT enumSuccess;
910 if (!dinput_devices[i]->enum_deviceA) continue;
912 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
914 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
916 callbackFlags = diactionformat_priorityA(lpdiActionFormat, lpdiActionFormat->dwGenre);
917 /* Default behavior is to enumerate attached game controllers */
918 enumSuccess = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
919 if (enumSuccess == S_OK)
921 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
923 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
924 return DI_OK;
929 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
931 /* Enumerate keyboard and mouse */
932 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
934 callbackFlags = diactionformat_priorityA(lpdiActionFormat, actionMasks[i]);
936 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
937 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
939 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
940 return DI_OK;
943 return DI_OK;
946 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
947 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
948 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
949 LPVOID pvRef, DWORD dwFlags
952 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
953 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
954 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
955 DIDEVICEINSTANCEW didevi;
956 LPDIRECTINPUTDEVICE8W lpdid;
957 DWORD callbackFlags;
958 int i, j;
960 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
961 lpCallback, pvRef, dwFlags);
963 didevi.dwSize = sizeof(didevi);
965 /* Enumerate all the joysticks */
966 for (i = 0; i < NB_DINPUT_DEVICES; i++)
968 HRESULT enumSuccess;
970 if (!dinput_devices[i]->enum_deviceW) continue;
972 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
974 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
976 callbackFlags = diactionformat_priorityW(lpdiActionFormat, lpdiActionFormat->dwGenre);
977 /* Default behavior is to enumerate attached game controllers */
978 enumSuccess = dinput_devices[i]->enum_deviceW(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
979 if (enumSuccess == S_OK)
981 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
983 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
984 return DI_OK;
989 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
991 /* Enumerate keyboard and mouse */
992 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
994 callbackFlags = diactionformat_priorityW(lpdiActionFormat, actionMasks[i]);
996 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
997 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
999 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
1000 return DI_OK;
1003 return DI_OK;
1006 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
1007 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1008 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1011 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1013 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1015 /* Call helper function in config.c to do the real work */
1016 return _configure_devices(iface, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1019 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
1020 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1021 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1024 IDirectInputImpl *This = impl_from_IDirectInput8A(iface);
1025 DIACTIONFORMATW diafW;
1026 DICONFIGUREDEVICESPARAMSW diCDParamsW;
1027 HRESULT hr;
1028 int i;
1030 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1032 /* Copy parameters */
1033 diCDParamsW.dwSize = sizeof(DICONFIGUREDEVICESPARAMSW);
1034 diCDParamsW.dwcFormats = lpdiCDParams->dwcFormats;
1035 diCDParamsW.lprgFormats = &diafW;
1036 diCDParamsW.hwnd = lpdiCDParams->hwnd;
1038 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiCDParams->lprgFormats->dwNumActions);
1039 _copy_diactionformatAtoW(&diafW, lpdiCDParams->lprgFormats);
1041 /* Copy action names */
1042 for (i=0; i < diafW.dwNumActions; i++)
1044 const char* from = lpdiCDParams->lprgFormats->rgoAction[i].u.lptszActionName;
1045 int len = MultiByteToWideChar(CP_ACP, 0, from , -1, NULL , 0);
1046 WCHAR *to = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1048 MultiByteToWideChar(CP_ACP, 0, from , -1, to , len);
1049 diafW.rgoAction[i].u.lptszActionName = to;
1052 hr = IDirectInput8WImpl_ConfigureDevices(&This->IDirectInput8W_iface, lpdiCallback, &diCDParamsW, dwFlags, pvRefData);
1054 /* Copy back configuration */
1055 if (SUCCEEDED(hr))
1056 _copy_diactionformatWtoA(lpdiCDParams->lprgFormats, &diafW);
1058 /* Free memory */
1059 for (i=0; i < diafW.dwNumActions; i++)
1060 HeapFree(GetProcessHeap(), 0, (void*) diafW.rgoAction[i].u.lptszActionName);
1062 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1064 return hr;
1067 /*****************************************************************************
1068 * IDirectInputJoyConfig8 interface
1071 static inline IDirectInputImpl *impl_from_IDirectInputJoyConfig8(IDirectInputJoyConfig8 *iface)
1073 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInputJoyConfig8_iface );
1076 static HRESULT WINAPI JoyConfig8Impl_QueryInterface(IDirectInputJoyConfig8 *iface, REFIID riid, void** ppobj)
1078 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1079 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
1082 static ULONG WINAPI JoyConfig8Impl_AddRef(IDirectInputJoyConfig8 *iface)
1084 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1085 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
1088 static ULONG WINAPI JoyConfig8Impl_Release(IDirectInputJoyConfig8 *iface)
1090 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1091 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
1094 static HRESULT WINAPI JoyConfig8Impl_Acquire(IDirectInputJoyConfig8 *iface)
1096 FIXME( "(%p): stub!\n", iface );
1097 return E_NOTIMPL;
1100 static HRESULT WINAPI JoyConfig8Impl_Unacquire(IDirectInputJoyConfig8 *iface)
1102 FIXME( "(%p): stub!\n", iface );
1103 return E_NOTIMPL;
1106 static HRESULT WINAPI JoyConfig8Impl_SetCooperativeLevel(IDirectInputJoyConfig8 *iface, HWND hwnd, DWORD flags)
1108 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, hwnd, flags );
1109 return E_NOTIMPL;
1112 static HRESULT WINAPI JoyConfig8Impl_SendNotify(IDirectInputJoyConfig8 *iface)
1114 FIXME( "(%p): stub!\n", iface );
1115 return E_NOTIMPL;
1118 static HRESULT WINAPI JoyConfig8Impl_EnumTypes(IDirectInputJoyConfig8 *iface, LPDIJOYTYPECALLBACK cb, void *ref)
1120 FIXME( "(%p)->(%p, %p): stub!\n", iface, cb, ref );
1121 return E_NOTIMPL;
1124 static HRESULT WINAPI JoyConfig8Impl_GetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPDIJOYTYPEINFO info, DWORD flags)
1126 FIXME( "(%p)->(%s, %p, 0x%08x): stub!\n", iface, debugstr_w(name), info, flags );
1127 return E_NOTIMPL;
1130 static HRESULT WINAPI JoyConfig8Impl_SetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPCDIJOYTYPEINFO info, DWORD flags,
1131 LPWSTR new_name)
1133 FIXME( "(%p)->(%s, %p, 0x%08x, %s): stub!\n", iface, debugstr_w(name), info, flags, debugstr_w(new_name) );
1134 return E_NOTIMPL;
1137 static HRESULT WINAPI JoyConfig8Impl_DeleteType(IDirectInputJoyConfig8 *iface, LPCWSTR name)
1139 FIXME( "(%p)->(%s): stub!\n", iface, debugstr_w(name) );
1140 return E_NOTIMPL;
1143 static HRESULT WINAPI JoyConfig8Impl_GetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPDIJOYCONFIG info, DWORD flags)
1145 IDirectInputImpl *di = impl_from_IDirectInputJoyConfig8(iface);
1146 UINT found = 0;
1147 int i, j;
1148 HRESULT r;
1150 FIXME("(%p)->(%d, %p, 0x%08x): semi-stub!\n", iface, id, info, flags);
1152 #define X(x) if (flags & x) FIXME("\tflags |= "#x"\n");
1153 X(DIJC_GUIDINSTANCE)
1154 X(DIJC_REGHWCONFIGTYPE)
1155 X(DIJC_GAIN)
1156 X(DIJC_CALLOUT)
1157 #undef X
1159 /* Enumerate all joysticks in order */
1160 for (i = 0; i < NB_DINPUT_DEVICES; i++)
1162 if (!dinput_devices[i]->enum_deviceA) continue;
1164 for (j = 0, r = S_OK; SUCCEEDED(r); j++)
1166 DIDEVICEINSTANCEA dev;
1167 dev.dwSize = sizeof(dev);
1168 if ((r = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, 0, &dev, di->dwVersion, j)) == S_OK)
1170 /* Only take into account the chosen id */
1171 if (found == id)
1173 if (flags & DIJC_GUIDINSTANCE)
1174 info->guidInstance = dev.guidInstance;
1176 return DI_OK;
1178 found += 1;
1183 return DIERR_NOMOREITEMS;
1186 static HRESULT WINAPI JoyConfig8Impl_SetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPCDIJOYCONFIG info, DWORD flags)
1188 FIXME( "(%p)->(%d, %p, 0x%08x): stub!\n", iface, id, info, flags );
1189 return E_NOTIMPL;
1192 static HRESULT WINAPI JoyConfig8Impl_DeleteConfig(IDirectInputJoyConfig8 *iface, UINT id)
1194 FIXME( "(%p)->(%d): stub!\n", iface, id );
1195 return E_NOTIMPL;
1198 static HRESULT WINAPI JoyConfig8Impl_GetUserValues(IDirectInputJoyConfig8 *iface, LPDIJOYUSERVALUES info, DWORD flags)
1200 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1201 return E_NOTIMPL;
1204 static HRESULT WINAPI JoyConfig8Impl_SetUserValues(IDirectInputJoyConfig8 *iface, LPCDIJOYUSERVALUES info, DWORD flags)
1206 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1207 return E_NOTIMPL;
1210 static HRESULT WINAPI JoyConfig8Impl_AddNewHardware(IDirectInputJoyConfig8 *iface, HWND hwnd, REFGUID guid)
1212 FIXME( "(%p)->(%p, %s): stub!\n", iface, hwnd, debugstr_guid(guid) );
1213 return E_NOTIMPL;
1216 static HRESULT WINAPI JoyConfig8Impl_OpenTypeKey(IDirectInputJoyConfig8 *iface, LPCWSTR name, DWORD security, PHKEY key)
1218 FIXME( "(%p)->(%s, 0x%08x, %p): stub!\n", iface, debugstr_w(name), security, key );
1219 return E_NOTIMPL;
1222 static HRESULT WINAPI JoyConfig8Impl_OpenAppStatusKey(IDirectInputJoyConfig8 *iface, PHKEY key)
1224 FIXME( "(%p)->(%p): stub!\n", iface, key );
1225 return E_NOTIMPL;
1228 static const IDirectInput7AVtbl ddi7avt = {
1229 IDirectInputAImpl_QueryInterface,
1230 IDirectInputAImpl_AddRef,
1231 IDirectInputAImpl_Release,
1232 IDirectInputAImpl_CreateDevice,
1233 IDirectInputAImpl_EnumDevices,
1234 IDirectInputAImpl_GetDeviceStatus,
1235 IDirectInputAImpl_RunControlPanel,
1236 IDirectInputAImpl_Initialize,
1237 IDirectInput2AImpl_FindDevice,
1238 IDirectInput7AImpl_CreateDeviceEx
1241 static const IDirectInput7WVtbl ddi7wvt = {
1242 IDirectInputWImpl_QueryInterface,
1243 IDirectInputWImpl_AddRef,
1244 IDirectInputWImpl_Release,
1245 IDirectInputWImpl_CreateDevice,
1246 IDirectInputWImpl_EnumDevices,
1247 IDirectInputWImpl_GetDeviceStatus,
1248 IDirectInputWImpl_RunControlPanel,
1249 IDirectInputWImpl_Initialize,
1250 IDirectInput2WImpl_FindDevice,
1251 IDirectInput7WImpl_CreateDeviceEx
1254 static const IDirectInput8AVtbl ddi8avt = {
1255 IDirectInput8AImpl_QueryInterface,
1256 IDirectInput8AImpl_AddRef,
1257 IDirectInput8AImpl_Release,
1258 IDirectInput8AImpl_CreateDevice,
1259 IDirectInput8AImpl_EnumDevices,
1260 IDirectInput8AImpl_GetDeviceStatus,
1261 IDirectInput8AImpl_RunControlPanel,
1262 IDirectInput8AImpl_Initialize,
1263 IDirectInput8AImpl_FindDevice,
1264 IDirectInput8AImpl_EnumDevicesBySemantics,
1265 IDirectInput8AImpl_ConfigureDevices
1268 static const IDirectInput8WVtbl ddi8wvt = {
1269 IDirectInput8WImpl_QueryInterface,
1270 IDirectInput8WImpl_AddRef,
1271 IDirectInput8WImpl_Release,
1272 IDirectInput8WImpl_CreateDevice,
1273 IDirectInput8WImpl_EnumDevices,
1274 IDirectInput8WImpl_GetDeviceStatus,
1275 IDirectInput8WImpl_RunControlPanel,
1276 IDirectInput8WImpl_Initialize,
1277 IDirectInput8WImpl_FindDevice,
1278 IDirectInput8WImpl_EnumDevicesBySemantics,
1279 IDirectInput8WImpl_ConfigureDevices
1282 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt =
1284 JoyConfig8Impl_QueryInterface,
1285 JoyConfig8Impl_AddRef,
1286 JoyConfig8Impl_Release,
1287 JoyConfig8Impl_Acquire,
1288 JoyConfig8Impl_Unacquire,
1289 JoyConfig8Impl_SetCooperativeLevel,
1290 JoyConfig8Impl_SendNotify,
1291 JoyConfig8Impl_EnumTypes,
1292 JoyConfig8Impl_GetTypeInfo,
1293 JoyConfig8Impl_SetTypeInfo,
1294 JoyConfig8Impl_DeleteType,
1295 JoyConfig8Impl_GetConfig,
1296 JoyConfig8Impl_SetConfig,
1297 JoyConfig8Impl_DeleteConfig,
1298 JoyConfig8Impl_GetUserValues,
1299 JoyConfig8Impl_SetUserValues,
1300 JoyConfig8Impl_AddNewHardware,
1301 JoyConfig8Impl_OpenTypeKey,
1302 JoyConfig8Impl_OpenAppStatusKey
1305 /*******************************************************************************
1306 * DirectInput ClassFactory
1308 typedef struct
1310 /* IUnknown fields */
1311 IClassFactory IClassFactory_iface;
1312 LONG ref;
1313 } IClassFactoryImpl;
1315 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1317 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1320 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1321 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1323 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1324 return E_NOINTERFACE;
1327 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
1328 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1329 return InterlockedIncrement(&(This->ref));
1332 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
1333 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1334 /* static class, won't be freed */
1335 return InterlockedDecrement(&(This->ref));
1338 static HRESULT WINAPI DICF_CreateInstance(
1339 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1341 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1343 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1344 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
1345 IsEqualGUID( &IID_IDirectInputA, riid ) ||
1346 IsEqualGUID( &IID_IDirectInputW, riid ) ||
1347 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
1348 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
1349 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
1350 IsEqualGUID( &IID_IDirectInput7W, riid ) ) {
1351 return create_directinput_instance(riid, ppobj, NULL);
1354 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1355 return E_NOINTERFACE;
1358 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1359 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1360 FIXME("(%p)->(%d),stub!\n",This,dolock);
1361 return S_OK;
1364 static const IClassFactoryVtbl DICF_Vtbl = {
1365 DICF_QueryInterface,
1366 DICF_AddRef,
1367 DICF_Release,
1368 DICF_CreateInstance,
1369 DICF_LockServer
1371 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
1373 /***********************************************************************
1374 * DllCanUnloadNow (DINPUT.@)
1376 HRESULT WINAPI DllCanUnloadNow(void)
1378 return S_FALSE;
1381 /***********************************************************************
1382 * DllGetClassObject (DINPUT.@)
1384 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1386 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1387 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1388 *ppv = &DINPUT_CF;
1389 IClassFactory_AddRef((IClassFactory*)*ppv);
1390 return S_OK;
1393 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1394 return CLASS_E_CLASSNOTAVAILABLE;
1397 /***********************************************************************
1398 * DllRegisterServer (DINPUT.@)
1400 HRESULT WINAPI DllRegisterServer(void)
1402 return __wine_register_resources( DINPUT_instance );
1405 /***********************************************************************
1406 * DllUnregisterServer (DINPUT.@)
1408 HRESULT WINAPI DllUnregisterServer(void)
1410 return __wine_unregister_resources( DINPUT_instance );
1413 /******************************************************************************
1414 * DInput hook thread
1417 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
1419 IDirectInputImpl *dinput;
1420 int skip = 0;
1422 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
1424 EnterCriticalSection( &dinput_hook_crit );
1425 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1427 IDirectInputDeviceImpl *dev;
1429 EnterCriticalSection( &dinput->crit );
1430 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1431 if (dev->acquired && dev->event_proc)
1433 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
1434 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1436 LeaveCriticalSection( &dinput->crit );
1438 LeaveCriticalSection( &dinput_hook_crit );
1440 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
1443 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
1445 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
1446 IDirectInputImpl *dinput;
1447 HWND foreground;
1449 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1450 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1451 return CallNextHookEx( 0, code, wparam, lparam );
1453 foreground = GetForegroundWindow();
1455 EnterCriticalSection( &dinput_hook_crit );
1457 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1459 IDirectInputDeviceImpl *dev;
1461 EnterCriticalSection( &dinput->crit );
1462 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1464 if (!dev->acquired) continue;
1466 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1468 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1469 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1472 LeaveCriticalSection( &dinput->crit );
1474 LeaveCriticalSection( &dinput_hook_crit );
1476 return CallNextHookEx( 0, code, wparam, lparam );
1479 static DWORD WINAPI hook_thread_proc(void *param)
1481 static HHOOK kbd_hook, mouse_hook;
1482 MSG msg;
1484 /* Force creation of the message queue */
1485 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1486 SetEvent(*(LPHANDLE)param);
1488 while (GetMessageW( &msg, 0, 0, 0 ))
1490 UINT kbd_cnt = 0, mice_cnt = 0;
1492 if (msg.message == WM_USER+0x10)
1494 IDirectInputImpl *dinput;
1496 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
1498 if (!msg.wParam && !msg.lParam)
1500 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1501 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1502 kbd_hook = mouse_hook = NULL;
1503 break;
1506 EnterCriticalSection( &dinput_hook_crit );
1508 /* Count acquired keyboards and mice*/
1509 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1511 IDirectInputDeviceImpl *dev;
1513 EnterCriticalSection( &dinput->crit );
1514 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1516 if (!dev->acquired || !dev->event_proc) continue;
1518 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1519 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1520 kbd_cnt++;
1521 else
1522 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1523 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1524 mice_cnt++;
1526 LeaveCriticalSection( &dinput->crit );
1528 LeaveCriticalSection( &dinput_hook_crit );
1530 if (kbd_cnt && !kbd_hook)
1531 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1532 else if (!kbd_cnt && kbd_hook)
1534 UnhookWindowsHookEx( kbd_hook );
1535 kbd_hook = NULL;
1538 if (mice_cnt && !mouse_hook)
1539 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1540 else if (!mice_cnt && mouse_hook)
1542 UnhookWindowsHookEx( mouse_hook );
1543 mouse_hook = NULL;
1546 TranslateMessage(&msg);
1547 DispatchMessageW(&msg);
1550 return 0;
1553 static DWORD hook_thread_id;
1555 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1557 0, 0, &dinput_hook_crit,
1558 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1559 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1561 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1563 static BOOL check_hook_thread(void)
1565 static HANDLE hook_thread;
1567 EnterCriticalSection(&dinput_hook_crit);
1569 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1570 if (!list_empty(&direct_input_list) && !hook_thread)
1572 HANDLE event;
1574 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1575 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1576 if (event && hook_thread)
1578 HANDLE handles[2];
1579 handles[0] = event;
1580 handles[1] = hook_thread;
1581 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1583 LeaveCriticalSection(&dinput_hook_crit);
1584 CloseHandle(event);
1586 else if (list_empty(&direct_input_list) && hook_thread)
1588 DWORD tid = hook_thread_id;
1590 hook_thread_id = 0;
1591 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1592 LeaveCriticalSection(&dinput_hook_crit);
1594 /* wait for hook thread to exit */
1595 WaitForSingleObject(hook_thread, INFINITE);
1596 CloseHandle(hook_thread);
1597 hook_thread = NULL;
1599 else
1600 LeaveCriticalSection(&dinput_hook_crit);
1602 return hook_thread_id != 0;
1605 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1607 static HHOOK callwndproc_hook;
1608 static ULONG foreground_cnt;
1609 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1611 EnterCriticalSection(&dinput_hook_crit);
1613 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1615 if (dev->acquired)
1616 foreground_cnt++;
1617 else
1618 foreground_cnt--;
1621 if (foreground_cnt && !callwndproc_hook)
1622 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1623 DINPUT_instance, GetCurrentThreadId() );
1624 else if (!foreground_cnt && callwndproc_hook)
1626 UnhookWindowsHookEx( callwndproc_hook );
1627 callwndproc_hook = NULL;
1630 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1632 LeaveCriticalSection(&dinput_hook_crit);
1635 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved)
1637 switch(reason)
1639 case DLL_PROCESS_ATTACH:
1640 DisableThreadLibraryCalls(inst);
1641 DINPUT_instance = inst;
1642 break;
1643 case DLL_PROCESS_DETACH:
1644 if (reserved) break;
1645 DeleteCriticalSection(&dinput_hook_crit);
1646 break;
1648 return TRUE;