winhttp: Check return value of CLSIDFromProgID (Coverity).
[wine/multimedia.git] / dlls / dinput / dinput_main.c
blobc121ae9ea440be06cdf4815f4f896fda2142e6c9
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 "dinput_private.h"
50 #include "device_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
54 static const IDirectInput7AVtbl ddi7avt;
55 static const IDirectInput7WVtbl ddi7wvt;
56 static const IDirectInput8AVtbl ddi8avt;
57 static const IDirectInput8WVtbl ddi8wvt;
59 static inline IDirectInputImpl *impl_from_IDirectInput7A( IDirectInput7A *iface )
61 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7A_iface );
64 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
66 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7W_iface );
69 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
71 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8A_iface );
74 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
76 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8W_iface );
79 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
81 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface);
83 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
85 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
88 static const struct dinput_device *dinput_devices[] =
90 &mouse_device,
91 &keyboard_device,
92 &joystick_linuxinput_device,
93 &joystick_linux_device,
94 &joystick_osx_device
96 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
98 static HINSTANCE DINPUT_instance = NULL;
100 static BOOL check_hook_thread(void);
101 static CRITICAL_SECTION dinput_hook_crit;
102 static struct list direct_input_list = LIST_INIT( direct_input_list );
104 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion);
105 static void uninitialize_directinput_instance(IDirectInputImpl *This);
107 static HRESULT create_directinput_instance(REFIID riid, LPVOID *ppDI, IDirectInputImpl **out)
109 IDirectInputImpl *This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectInputImpl) );
110 HRESULT hr;
112 if (!This)
113 return E_OUTOFMEMORY;
115 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
116 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
117 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
118 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
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) {
185 switch (dwDevType) {
186 case 0: return "All devices";
187 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
188 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
189 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
190 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
191 default: return "Unknown";
195 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
196 if (TRACE_ON(dinput)) {
197 unsigned int i;
198 static const struct {
199 DWORD mask;
200 const char *name;
201 } flags[] = {
202 #define FE(x) { x, #x}
203 FE(DIEDFL_ALLDEVICES),
204 FE(DIEDFL_ATTACHEDONLY),
205 FE(DIEDFL_FORCEFEEDBACK),
206 FE(DIEDFL_INCLUDEALIASES),
207 FE(DIEDFL_INCLUDEPHANTOMS)
208 #undef FE
210 TRACE(" flags: ");
211 if (dwFlags == 0) {
212 TRACE("DIEDFL_ALLDEVICES\n");
213 return;
215 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
216 if (flags[i].mask & dwFlags)
217 TRACE("%s ",flags[i].name);
219 TRACE("\n");
222 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat) {
223 unsigned int i;
225 FIXME("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
226 FIXME("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
227 FIXME("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
228 FIXME("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
229 FIXME("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
230 FIXME("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
231 FIXME("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
232 FIXME("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
233 FIXME("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
234 FIXME("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
235 FIXME("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
236 FIXME("diaf.ftTimeStamp ...\n");
237 FIXME("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
238 FIXME("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
239 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
241 FIXME("diaf.rgoAction[%u]:\n", i);
242 FIXME("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
243 FIXME("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
244 FIXME("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
245 FIXME("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
246 FIXME("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
247 FIXME("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
248 FIXME("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
252 void _copy_diactionformatAtoW(LPDIACTIONFORMATW to, LPDIACTIONFORMATA from)
254 int i;
256 to->dwSize = sizeof(DIACTIONFORMATW);
257 to->dwActionSize = sizeof(DIACTIONW);
258 to->dwDataSize = from->dwDataSize;
259 to->dwNumActions = from->dwNumActions;
260 to->guidActionMap = from->guidActionMap;
261 to->dwGenre = from->dwGenre;
262 to->dwBufferSize = from->dwBufferSize;
263 to->lAxisMin = from->lAxisMin;
264 to->lAxisMax = from->lAxisMax;
265 to->dwCRC = from->dwCRC;
266 to->ftTimeStamp = from->ftTimeStamp;
268 for (i=0; i < to->dwNumActions; i++)
270 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
271 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
272 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
273 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
274 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
275 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
279 void _copy_diactionformatWtoA(LPDIACTIONFORMATA to, LPDIACTIONFORMATW from)
281 int i;
283 to->dwSize = sizeof(DIACTIONFORMATA);
284 to->dwActionSize = sizeof(DIACTIONA);
285 to->dwDataSize = from->dwDataSize;
286 to->dwNumActions = from->dwNumActions;
287 to->guidActionMap = from->guidActionMap;
288 to->dwGenre = from->dwGenre;
289 to->dwBufferSize = from->dwBufferSize;
290 to->lAxisMin = from->lAxisMin;
291 to->lAxisMax = from->lAxisMax;
292 to->dwCRC = from->dwCRC;
293 to->ftTimeStamp = from->ftTimeStamp;
295 for (i=0; i < to->dwNumActions; i++)
297 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
298 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
299 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
300 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
301 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
302 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
306 /* _diactionformat_priority
308 * Given a DIACTIONFORMAT structure and a DI genre, returns the enumeration
309 * priority. Joysticks should pass the game genre, and mouse or keyboard their
310 * respective DI*_MASK
312 static DWORD _diactionformat_priorityA(LPDIACTIONFORMATA lpdiaf, DWORD genre)
314 int i;
315 DWORD priorityFlags = 0;
317 /* If there's at least one action for the device it's priority 1 */
318 for(i=0; i < lpdiaf->dwActionSize; i++)
319 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
320 priorityFlags |= DIEDBS_MAPPEDPRI1;
322 return priorityFlags;
325 static DWORD _diactionformat_priorityW(LPDIACTIONFORMATW lpdiaf, DWORD genre)
327 int i;
328 DWORD priorityFlags = 0;
330 /* If there's at least one action for the device it's priority 1 */
331 for(i=0; i < lpdiaf->dwActionSize; i++)
332 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
333 priorityFlags |= DIEDBS_MAPPEDPRI1;
335 return priorityFlags;
338 /******************************************************************************
339 * IDirectInputA_EnumDevices
341 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
342 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
343 LPVOID pvRef, DWORD dwFlags)
345 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
346 DIDEVICEINSTANCEA devInstance;
347 unsigned int i;
348 int j, r;
350 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
351 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
352 lpCallback, pvRef, dwFlags);
353 _dump_EnumDevices_dwFlags(dwFlags);
355 if (!lpCallback)
356 return DIERR_INVALIDPARAM;
358 if (!This->initialized)
359 return DIERR_NOTINITIALIZED;
361 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
362 if (!dinput_devices[i]->enum_deviceA) continue;
363 for (j = 0, r = -1; r != 0; j++) {
364 devInstance.dwSize = sizeof(devInstance);
365 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
366 if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
367 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
368 return 0;
373 return 0;
375 /******************************************************************************
376 * IDirectInputW_EnumDevices
378 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
379 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
380 LPVOID pvRef, DWORD dwFlags)
382 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
383 DIDEVICEINSTANCEW devInstance;
384 unsigned int i;
385 int j, r;
387 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
388 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
389 lpCallback, pvRef, dwFlags);
390 _dump_EnumDevices_dwFlags(dwFlags);
392 if (!lpCallback)
393 return DIERR_INVALIDPARAM;
395 if (!This->initialized)
396 return DIERR_NOTINITIALIZED;
398 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
399 if (!dinput_devices[i]->enum_deviceW) continue;
400 for (j = 0, r = -1; r != 0; j++) {
401 devInstance.dwSize = sizeof(devInstance);
402 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
403 if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
404 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
405 return 0;
410 return 0;
413 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
415 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
416 ULONG ref = InterlockedIncrement(&This->ref);
418 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
419 return ref;
422 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
424 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
425 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
428 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
430 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
431 ULONG ref = InterlockedDecrement( &This->ref );
433 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
435 if (ref == 0)
437 uninitialize_directinput_instance( This );
438 HeapFree( GetProcessHeap(), 0, This );
441 return ref;
444 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
446 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
447 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
450 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
452 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
454 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
456 if (!riid || !ppobj)
457 return E_POINTER;
459 if (IsEqualGUID( &IID_IUnknown, riid ) ||
460 IsEqualGUID( &IID_IDirectInputA, riid ) ||
461 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
462 IsEqualGUID( &IID_IDirectInput7A, riid ))
464 *ppobj = &This->IDirectInput7A_iface;
465 IUnknown_AddRef( (IUnknown*)*ppobj );
467 return DI_OK;
470 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
471 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
472 IsEqualGUID( &IID_IDirectInput7W, riid ))
474 *ppobj = &This->IDirectInput7W_iface;
475 IUnknown_AddRef( (IUnknown*)*ppobj );
477 return DI_OK;
480 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
482 *ppobj = &This->IDirectInput8A_iface;
483 IUnknown_AddRef( (IUnknown*)*ppobj );
485 return DI_OK;
488 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
490 *ppobj = &This->IDirectInput8W_iface;
491 IUnknown_AddRef( (IUnknown*)*ppobj );
493 return DI_OK;
496 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
497 *ppobj = NULL;
498 return E_NOINTERFACE;
501 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
503 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
504 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
507 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion)
509 if (!This->initialized)
511 This->dwVersion = dwVersion;
512 This->evsequence = 1;
514 InitializeCriticalSection( &This->crit );
515 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
517 list_init( &This->devices_list );
519 /* Add self to the list of the IDirectInputs */
520 EnterCriticalSection( &dinput_hook_crit );
521 list_add_head( &direct_input_list, &This->entry );
522 LeaveCriticalSection( &dinput_hook_crit );
524 This->initialized = TRUE;
526 if (!check_hook_thread())
528 uninitialize_directinput_instance( This );
529 return DIERR_GENERIC;
533 return DI_OK;
536 static void uninitialize_directinput_instance(IDirectInputImpl *This)
538 if (This->initialized)
540 /* Remove self from the list of the IDirectInputs */
541 EnterCriticalSection( &dinput_hook_crit );
542 list_remove( &This->entry );
543 LeaveCriticalSection( &dinput_hook_crit );
545 check_hook_thread();
547 This->crit.DebugInfo->Spare[0] = 0;
548 DeleteCriticalSection( &This->crit );
550 This->initialized = FALSE;
554 enum directinput_versions
556 DIRECTINPUT_VERSION_300 = 0x0300,
557 DIRECTINPUT_VERSION_500 = 0x0500,
558 DIRECTINPUT_VERSION_50A = 0x050A,
559 DIRECTINPUT_VERSION_5B2 = 0x05B2,
560 DIRECTINPUT_VERSION_602 = 0x0602,
561 DIRECTINPUT_VERSION_61A = 0x061A,
562 DIRECTINPUT_VERSION_700 = 0x0700,
565 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD version)
567 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
569 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
571 if (!hinst)
572 return DIERR_INVALIDPARAM;
573 else if (version == 0)
574 return DIERR_NOTINITIALIZED;
575 else if (version > DIRECTINPUT_VERSION_700)
576 return DIERR_OLDDIRECTINPUTVERSION;
577 else if (version != DIRECTINPUT_VERSION_300 && version != DIRECTINPUT_VERSION_500 &&
578 version != DIRECTINPUT_VERSION_50A && version != DIRECTINPUT_VERSION_5B2 &&
579 version != DIRECTINPUT_VERSION_602 && version != DIRECTINPUT_VERSION_61A &&
580 version != DIRECTINPUT_VERSION_700 && version != DIRECTINPUT_VERSION)
581 return DIERR_BETADIRECTINPUTVERSION;
583 return initialize_directinput_instance(This, version);
586 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
588 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
589 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
592 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
594 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
595 HRESULT hr;
596 LPDIRECTINPUTDEVICEA device;
598 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
600 if (!This->initialized)
601 return DIERR_NOTINITIALIZED;
603 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
604 if (hr != DI_OK) return DI_NOTATTACHED;
606 IUnknown_Release( device );
608 return DI_OK;
611 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
613 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
614 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
617 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
618 HWND hwndOwner,
619 DWORD dwFlags)
621 WCHAR control_exeW[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
622 STARTUPINFOW si = {0};
623 PROCESS_INFORMATION pi;
625 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
627 TRACE( "(%p)->(%p, %08x)\n", This, hwndOwner, dwFlags );
629 if (hwndOwner && !IsWindow(hwndOwner))
630 return E_HANDLE;
632 if (dwFlags)
633 return DIERR_INVALIDPARAM;
635 if (!This->initialized)
636 return DIERR_NOTINITIALIZED;
638 if (!CreateProcessW(NULL, control_exeW, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
639 return HRESULT_FROM_WIN32(GetLastError());
641 return DI_OK;
644 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
646 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
647 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
650 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
651 LPCSTR pszName, LPGUID pguidInstance)
653 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
655 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
657 return DI_OK;
660 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
661 LPCWSTR pszName, LPGUID pguidInstance)
663 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
665 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
667 return DI_OK;
670 static HRESULT create_device(IDirectInputImpl *This, REFGUID rguid, REFIID riid, LPVOID *pvOut, BOOL unicode)
672 unsigned int i;
674 if (pvOut)
675 *pvOut = NULL;
677 if (!rguid || !pvOut)
678 return E_POINTER;
680 if (!This->initialized)
681 return DIERR_NOTINITIALIZED;
683 /* Loop on all the devices to see if anyone matches the given GUID */
684 for (i = 0; i < NB_DINPUT_DEVICES; i++)
686 HRESULT ret;
688 if (!dinput_devices[i]->create_device) continue;
689 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, unicode)) == DI_OK)
690 return DI_OK;
693 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
694 return DIERR_DEVICENOTREG;
697 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
698 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
700 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
702 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
704 return create_device(This, rguid, riid, pvOut, FALSE);
707 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
708 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
710 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
712 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
714 return create_device(This, rguid, riid, pvOut, TRUE);
717 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
718 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
720 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
723 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
724 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
726 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
729 /*******************************************************************************
730 * DirectInput8
733 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
735 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
736 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
739 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
741 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
742 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
745 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
747 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
748 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
751 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
753 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
754 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
757 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
759 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
760 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
763 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
765 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
766 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
769 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
770 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
772 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
773 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
776 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
777 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
779 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
780 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
783 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
784 LPVOID pvRef, DWORD dwFlags)
786 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
787 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
790 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
791 LPVOID pvRef, DWORD dwFlags)
793 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
794 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
797 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
799 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
800 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
803 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
805 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
806 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
809 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
811 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
812 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
815 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
817 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
818 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
821 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD version)
823 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
825 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
827 if (!hinst)
828 return DIERR_INVALIDPARAM;
829 else if (version == 0)
830 return DIERR_NOTINITIALIZED;
831 else if (version < DIRECTINPUT_VERSION)
832 return DIERR_BETADIRECTINPUTVERSION;
833 else if (version > DIRECTINPUT_VERSION)
834 return DIERR_OLDDIRECTINPUTVERSION;
836 return initialize_directinput_instance(This, version);
839 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD version)
841 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
842 return IDirectInput8AImpl_Initialize( &This->IDirectInput8A_iface, hinst, version );
845 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
847 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
848 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
851 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
853 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
854 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
857 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
858 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
859 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
860 LPVOID pvRef, DWORD dwFlags
863 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
864 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
865 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
866 DIDEVICEINSTANCEA didevi;
867 LPDIRECTINPUTDEVICE8A lpdid;
868 DWORD callbackFlags;
869 int i, j;
872 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_a(ptszUserName), lpdiActionFormat,
873 lpCallback, pvRef, dwFlags);
874 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
875 X(DIEDBSFL_ATTACHEDONLY)
876 X(DIEDBSFL_THISUSER)
877 X(DIEDBSFL_FORCEFEEDBACK)
878 X(DIEDBSFL_AVAILABLEDEVICES)
879 X(DIEDBSFL_MULTIMICEKEYBOARDS)
880 X(DIEDBSFL_NONGAMINGDEVICES)
881 #undef X
883 _dump_diactionformatA(lpdiActionFormat);
885 didevi.dwSize = sizeof(didevi);
887 /* Enumerate all the joysticks */
888 for (i = 0; i < NB_DINPUT_DEVICES; i++)
890 BOOL enumSuccess;
892 if (!dinput_devices[i]->enum_deviceA) continue;
894 for (j = 0, enumSuccess = -1; enumSuccess != 0; j++)
896 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
898 callbackFlags = _diactionformat_priorityA(lpdiActionFormat, lpdiActionFormat->dwGenre);
899 /* Default behavior is to enumerate attached game controllers */
900 enumSuccess = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
901 if (enumSuccess)
903 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
905 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
906 return DI_OK;
911 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
913 /* Enumerate keyboard and mouse */
914 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
916 callbackFlags = _diactionformat_priorityA(lpdiActionFormat, actionMasks[i]);
918 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
919 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
921 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
922 return DI_OK;
925 return DI_OK;
928 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
929 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
930 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
931 LPVOID pvRef, DWORD dwFlags
934 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
935 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
936 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
937 DIDEVICEINSTANCEW didevi;
938 LPDIRECTINPUTDEVICE8W lpdid;
939 DWORD callbackFlags;
940 int i, j;
942 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
943 lpCallback, pvRef, dwFlags);
945 didevi.dwSize = sizeof(didevi);
947 /* Enumerate all the joysticks */
948 for (i = 0; i < NB_DINPUT_DEVICES; i++)
950 BOOL enumSuccess;
952 if (!dinput_devices[i]->enum_deviceW) continue;
954 for (j = 0, enumSuccess = -1; enumSuccess != 0; j++)
956 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
958 callbackFlags = _diactionformat_priorityW(lpdiActionFormat, lpdiActionFormat->dwGenre);
959 /* Default behavior is to enumerate attached game controllers */
960 enumSuccess = dinput_devices[i]->enum_deviceW(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
961 if (enumSuccess)
963 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
965 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
966 return DI_OK;
971 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
973 /* Enumerate keyboard and mouse */
974 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
976 callbackFlags = _diactionformat_priorityW(lpdiActionFormat, actionMasks[i]);
978 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
979 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
981 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
982 return DI_OK;
985 return DI_OK;
988 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
989 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
990 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
993 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
995 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
997 /* Call helper function in config.c to do the real work */
998 return _configure_devices(iface, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1001 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
1002 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1003 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1006 IDirectInputImpl *This = impl_from_IDirectInput8A(iface);
1007 DIACTIONFORMATW diafW;
1008 DICONFIGUREDEVICESPARAMSW diCDParamsW;
1009 HRESULT hr;
1010 int i;
1012 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1014 /* Copy parameters */
1015 diCDParamsW.dwSize = sizeof(DICONFIGUREDEVICESPARAMSW);
1016 diCDParamsW.dwcFormats = lpdiCDParams->dwcFormats;
1017 diCDParamsW.lprgFormats = &diafW;
1018 diCDParamsW.hwnd = lpdiCDParams->hwnd;
1020 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiCDParams->lprgFormats->dwNumActions);
1021 _copy_diactionformatAtoW(&diafW, lpdiCDParams->lprgFormats);
1023 /* Copy action names */
1024 for (i=0; i < diafW.dwNumActions; i++)
1026 const char* from = lpdiCDParams->lprgFormats->rgoAction[i].u.lptszActionName;
1027 int len = MultiByteToWideChar(CP_ACP, 0, from , -1, NULL , 0);
1028 WCHAR *to = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1030 MultiByteToWideChar(CP_ACP, 0, from , -1, to , len);
1031 diafW.rgoAction[i].u.lptszActionName = to;
1034 hr = IDirectInput8WImpl_ConfigureDevices(&This->IDirectInput8W_iface, lpdiCallback, &diCDParamsW, dwFlags, pvRefData);
1036 /* Copy back configuration */
1037 if (SUCCEEDED(hr))
1038 _copy_diactionformatWtoA(lpdiCDParams->lprgFormats, &diafW);
1040 /* Free memory */
1041 for (i=0; i < diafW.dwNumActions; i++)
1042 HeapFree(GetProcessHeap(), 0, (void*) diafW.rgoAction[i].u.lptszActionName);
1044 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1046 return hr;
1049 static const IDirectInput7AVtbl ddi7avt = {
1050 IDirectInputAImpl_QueryInterface,
1051 IDirectInputAImpl_AddRef,
1052 IDirectInputAImpl_Release,
1053 IDirectInputAImpl_CreateDevice,
1054 IDirectInputAImpl_EnumDevices,
1055 IDirectInputAImpl_GetDeviceStatus,
1056 IDirectInputAImpl_RunControlPanel,
1057 IDirectInputAImpl_Initialize,
1058 IDirectInput2AImpl_FindDevice,
1059 IDirectInput7AImpl_CreateDeviceEx
1062 static const IDirectInput7WVtbl ddi7wvt = {
1063 IDirectInputWImpl_QueryInterface,
1064 IDirectInputWImpl_AddRef,
1065 IDirectInputWImpl_Release,
1066 IDirectInputWImpl_CreateDevice,
1067 IDirectInputWImpl_EnumDevices,
1068 IDirectInputWImpl_GetDeviceStatus,
1069 IDirectInputWImpl_RunControlPanel,
1070 IDirectInputWImpl_Initialize,
1071 IDirectInput2WImpl_FindDevice,
1072 IDirectInput7WImpl_CreateDeviceEx
1075 static const IDirectInput8AVtbl ddi8avt = {
1076 IDirectInput8AImpl_QueryInterface,
1077 IDirectInput8AImpl_AddRef,
1078 IDirectInput8AImpl_Release,
1079 IDirectInput8AImpl_CreateDevice,
1080 IDirectInput8AImpl_EnumDevices,
1081 IDirectInput8AImpl_GetDeviceStatus,
1082 IDirectInput8AImpl_RunControlPanel,
1083 IDirectInput8AImpl_Initialize,
1084 IDirectInput8AImpl_FindDevice,
1085 IDirectInput8AImpl_EnumDevicesBySemantics,
1086 IDirectInput8AImpl_ConfigureDevices
1089 static const IDirectInput8WVtbl ddi8wvt = {
1090 IDirectInput8WImpl_QueryInterface,
1091 IDirectInput8WImpl_AddRef,
1092 IDirectInput8WImpl_Release,
1093 IDirectInput8WImpl_CreateDevice,
1094 IDirectInput8WImpl_EnumDevices,
1095 IDirectInput8WImpl_GetDeviceStatus,
1096 IDirectInput8WImpl_RunControlPanel,
1097 IDirectInput8WImpl_Initialize,
1098 IDirectInput8WImpl_FindDevice,
1099 IDirectInput8WImpl_EnumDevicesBySemantics,
1100 IDirectInput8WImpl_ConfigureDevices
1103 /*******************************************************************************
1104 * DirectInput ClassFactory
1106 typedef struct
1108 /* IUnknown fields */
1109 IClassFactory IClassFactory_iface;
1110 LONG ref;
1111 } IClassFactoryImpl;
1113 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1115 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1118 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1119 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1121 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1122 return E_NOINTERFACE;
1125 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
1126 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1127 return InterlockedIncrement(&(This->ref));
1130 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
1131 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1132 /* static class, won't be freed */
1133 return InterlockedDecrement(&(This->ref));
1136 static HRESULT WINAPI DICF_CreateInstance(
1137 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1139 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1141 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1142 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
1143 IsEqualGUID( &IID_IDirectInputA, riid ) ||
1144 IsEqualGUID( &IID_IDirectInputW, riid ) ||
1145 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
1146 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
1147 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
1148 IsEqualGUID( &IID_IDirectInput7W, riid ) ) {
1149 return create_directinput_instance(riid, ppobj, NULL);
1152 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1153 return E_NOINTERFACE;
1156 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1157 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1158 FIXME("(%p)->(%d),stub!\n",This,dolock);
1159 return S_OK;
1162 static const IClassFactoryVtbl DICF_Vtbl = {
1163 DICF_QueryInterface,
1164 DICF_AddRef,
1165 DICF_Release,
1166 DICF_CreateInstance,
1167 DICF_LockServer
1169 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
1171 /***********************************************************************
1172 * DllCanUnloadNow (DINPUT.@)
1174 HRESULT WINAPI DllCanUnloadNow(void)
1176 return S_FALSE;
1179 /***********************************************************************
1180 * DllGetClassObject (DINPUT.@)
1182 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1184 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1185 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1186 *ppv = &DINPUT_CF;
1187 IClassFactory_AddRef((IClassFactory*)*ppv);
1188 return S_OK;
1191 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1192 return CLASS_E_CLASSNOTAVAILABLE;
1195 /***********************************************************************
1196 * DllRegisterServer (DINPUT.@)
1198 HRESULT WINAPI DllRegisterServer(void)
1200 return __wine_register_resources( DINPUT_instance );
1203 /***********************************************************************
1204 * DllUnregisterServer (DINPUT.@)
1206 HRESULT WINAPI DllUnregisterServer(void)
1208 return __wine_unregister_resources( DINPUT_instance );
1211 /******************************************************************************
1212 * DInput hook thread
1215 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
1217 IDirectInputImpl *dinput;
1218 int skip = 0;
1220 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
1222 EnterCriticalSection( &dinput_hook_crit );
1223 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1225 IDirectInputDeviceImpl *dev;
1227 EnterCriticalSection( &dinput->crit );
1228 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1229 if (dev->acquired && dev->event_proc)
1231 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
1232 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1234 LeaveCriticalSection( &dinput->crit );
1236 LeaveCriticalSection( &dinput_hook_crit );
1238 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
1241 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
1243 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
1244 IDirectInputImpl *dinput;
1245 HWND foreground;
1247 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1248 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1249 return CallNextHookEx( 0, code, wparam, lparam );
1251 foreground = GetForegroundWindow();
1253 EnterCriticalSection( &dinput_hook_crit );
1255 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1257 IDirectInputDeviceImpl *dev;
1259 EnterCriticalSection( &dinput->crit );
1260 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1262 if (!dev->acquired) continue;
1264 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1266 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1267 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1270 LeaveCriticalSection( &dinput->crit );
1272 LeaveCriticalSection( &dinput_hook_crit );
1274 return CallNextHookEx( 0, code, wparam, lparam );
1277 static DWORD WINAPI hook_thread_proc(void *param)
1279 static HHOOK kbd_hook, mouse_hook;
1280 MSG msg;
1282 /* Force creation of the message queue */
1283 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1284 SetEvent(*(LPHANDLE)param);
1286 while (GetMessageW( &msg, 0, 0, 0 ))
1288 UINT kbd_cnt = 0, mice_cnt = 0;
1290 if (msg.message == WM_USER+0x10)
1292 IDirectInputImpl *dinput;
1294 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
1296 if (!msg.wParam && !msg.lParam)
1298 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1299 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1300 kbd_hook = mouse_hook = NULL;
1301 break;
1304 EnterCriticalSection( &dinput_hook_crit );
1306 /* Count acquired keyboards and mice*/
1307 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1309 IDirectInputDeviceImpl *dev;
1311 EnterCriticalSection( &dinput->crit );
1312 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1314 if (!dev->acquired || !dev->event_proc) continue;
1316 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1317 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1318 kbd_cnt++;
1319 else
1320 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1321 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1322 mice_cnt++;
1324 LeaveCriticalSection( &dinput->crit );
1326 LeaveCriticalSection( &dinput_hook_crit );
1328 if (kbd_cnt && !kbd_hook)
1329 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1330 else if (!kbd_cnt && kbd_hook)
1332 UnhookWindowsHookEx( kbd_hook );
1333 kbd_hook = NULL;
1336 if (mice_cnt && !mouse_hook)
1337 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1338 else if (!mice_cnt && mouse_hook)
1340 UnhookWindowsHookEx( mouse_hook );
1341 mouse_hook = NULL;
1344 TranslateMessage(&msg);
1345 DispatchMessageW(&msg);
1348 return 0;
1351 static DWORD hook_thread_id;
1353 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1355 0, 0, &dinput_hook_crit,
1356 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1357 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1359 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1361 static BOOL check_hook_thread(void)
1363 static HANDLE hook_thread;
1365 EnterCriticalSection(&dinput_hook_crit);
1367 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1368 if (!list_empty(&direct_input_list) && !hook_thread)
1370 HANDLE event;
1372 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1373 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1374 if (event && hook_thread)
1376 HANDLE handles[2];
1377 handles[0] = event;
1378 handles[1] = hook_thread;
1379 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1381 LeaveCriticalSection(&dinput_hook_crit);
1382 CloseHandle(event);
1384 else if (list_empty(&direct_input_list) && hook_thread)
1386 DWORD tid = hook_thread_id;
1388 hook_thread_id = 0;
1389 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1390 LeaveCriticalSection(&dinput_hook_crit);
1392 /* wait for hook thread to exit */
1393 WaitForSingleObject(hook_thread, INFINITE);
1394 CloseHandle(hook_thread);
1395 hook_thread = NULL;
1397 else
1398 LeaveCriticalSection(&dinput_hook_crit);
1400 return hook_thread_id != 0;
1403 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1405 static HHOOK callwndproc_hook;
1406 static ULONG foreground_cnt;
1407 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1409 EnterCriticalSection(&dinput_hook_crit);
1411 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1413 if (dev->acquired)
1414 foreground_cnt++;
1415 else
1416 foreground_cnt--;
1419 if (foreground_cnt && !callwndproc_hook)
1420 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1421 DINPUT_instance, GetCurrentThreadId() );
1422 else if (!foreground_cnt && callwndproc_hook)
1424 UnhookWindowsHookEx( callwndproc_hook );
1425 callwndproc_hook = NULL;
1428 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1430 LeaveCriticalSection(&dinput_hook_crit);
1433 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
1435 switch(reason)
1437 case DLL_PROCESS_ATTACH:
1438 DisableThreadLibraryCalls(inst);
1439 DINPUT_instance = inst;
1440 break;
1441 case DLL_PROCESS_DETACH:
1442 DeleteCriticalSection(&dinput_hook_crit);
1443 break;
1445 return TRUE;