6beed0ea1c2036545591070ebaff9441a2474d61
[reactos.git] / reactos / dll / directx / wine / dinput / dinput_main.c
1 /* DirectInput
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2002 TransGaming Technologies Inc.
6 * Copyright 2007 Vitaliy Margolen
7 *
8 *
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.
13 *
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.
18 *
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
22 */
23 /* Status:
24 *
25 * - Tomb Raider 2 Demo:
26 * Playable using keyboard only.
27 * - WingCommander Prophecy Demo:
28 * Doesn't get Input Focus.
29 *
30 * - Fallout : works great in X and DGA mode
31 */
32
33 #include <config.h>
34 //#include <assert.h>
35 //#include <stdarg.h>
36 //#include <string.h>
37
38 #define WIN32_NO_STATUS
39 #define _INC_WINDOWS
40 #define COM_NO_WINDOWS_H
41
42 #define COBJMACROS
43 #define NONAMELESSUNION
44
45 #include <wine/debug.h>
46 //#include "wine/unicode.h"
47 //#include "windef.h"
48 #include <winbase.h>
49 #include <winuser.h>
50 #include <winnls.h>
51 //#include "winerror.h"
52 #include <objbase.h>
53 #include <rpcproxy.h>
54 //#include "initguid.h"
55 //#include "dinput_private.h"
56 #include "device_private.h"
57 //#include "dinputd.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
60
61 static const IDirectInput7AVtbl ddi7avt;
62 static const IDirectInput7WVtbl ddi7wvt;
63 static const IDirectInput8AVtbl ddi8avt;
64 static const IDirectInput8WVtbl ddi8wvt;
65 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt;
66
67 static inline IDirectInputImpl *impl_from_IDirectInput7A( IDirectInput7A *iface )
68 {
69 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7A_iface );
70 }
71
72 static inline IDirectInputImpl *impl_from_IDirectInput7W( IDirectInput7W *iface )
73 {
74 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput7W_iface );
75 }
76
77 static inline IDirectInputImpl *impl_from_IDirectInput8A( IDirectInput8A *iface )
78 {
79 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8A_iface );
80 }
81
82 static inline IDirectInputImpl *impl_from_IDirectInput8W( IDirectInput8W *iface )
83 {
84 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInput8W_iface );
85 }
86
87 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
88 {
89 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface);
90 }
91 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
92 {
93 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
94 }
95
96 static const struct dinput_device *dinput_devices[] =
97 {
98 &mouse_device,
99 &keyboard_device,
100 &joystick_linuxinput_device,
101 &joystick_linux_device,
102 &joystick_osx_device
103 };
104 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
105
106 static HINSTANCE DINPUT_instance = NULL;
107
108 static BOOL check_hook_thread(void);
109 static CRITICAL_SECTION dinput_hook_crit;
110 static struct list direct_input_list = LIST_INIT( direct_input_list );
111
112 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion);
113 static void uninitialize_directinput_instance(IDirectInputImpl *This);
114
115 static HRESULT create_directinput_instance(REFIID riid, LPVOID *ppDI, IDirectInputImpl **out)
116 {
117 IDirectInputImpl *This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectInputImpl) );
118 HRESULT hr;
119
120 if (!This)
121 return E_OUTOFMEMORY;
122
123 This->IDirectInput7A_iface.lpVtbl = &ddi7avt;
124 This->IDirectInput7W_iface.lpVtbl = &ddi7wvt;
125 This->IDirectInput8A_iface.lpVtbl = &ddi8avt;
126 This->IDirectInput8W_iface.lpVtbl = &ddi8wvt;
127 This->IDirectInputJoyConfig8_iface.lpVtbl = &JoyConfig8vt;
128
129 hr = IDirectInput_QueryInterface( &This->IDirectInput7A_iface, riid, ppDI );
130 if (FAILED(hr))
131 {
132 HeapFree( GetProcessHeap(), 0, This );
133 return hr;
134 }
135
136 if (out) *out = This;
137 return DI_OK;
138 }
139
140 /******************************************************************************
141 * DirectInputCreateEx (DINPUT.@)
142 */
143 HRESULT WINAPI DirectInputCreateEx(
144 HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
145 LPUNKNOWN punkOuter)
146 {
147 IDirectInputImpl *This;
148 HRESULT hr;
149
150 TRACE("(%p,%04x,%s,%p,%p)\n", hinst, dwVersion, debugstr_guid(riid), ppDI, punkOuter);
151
152 if (IsEqualGUID( &IID_IDirectInputA, riid ) ||
153 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
154 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
155 IsEqualGUID( &IID_IDirectInputW, riid ) ||
156 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
157 IsEqualGUID( &IID_IDirectInput7W, riid ))
158 {
159 hr = create_directinput_instance(riid, ppDI, &This);
160 if (FAILED(hr))
161 return hr;
162 }
163 else
164 return DIERR_NOINTERFACE;
165
166 hr = IDirectInput_Initialize( &This->IDirectInput7A_iface, hinst, dwVersion );
167 if (FAILED(hr))
168 {
169 IDirectInput_Release( &This->IDirectInput7A_iface );
170 *ppDI = NULL;
171 return hr;
172 }
173
174 return DI_OK;
175 }
176
177 /******************************************************************************
178 * DirectInputCreateA (DINPUT.@)
179 */
180 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
181 {
182 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7A, (LPVOID *)ppDI, punkOuter);
183 }
184
185 /******************************************************************************
186 * DirectInputCreateW (DINPUT.@)
187 */
188 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
189 {
190 return DirectInputCreateEx(hinst, dwVersion, &IID_IDirectInput7W, (LPVOID *)ppDI, punkOuter);
191 }
192
193 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType)
194 {
195 switch (dwDevType) {
196 case 0: return "All devices";
197 case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
198 case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
199 case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
200 case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
201 default: return "Unknown";
202 }
203 }
204
205 static void _dump_EnumDevices_dwFlags(DWORD dwFlags)
206 {
207 if (TRACE_ON(dinput)) {
208 unsigned int i;
209 static const struct {
210 DWORD mask;
211 const char *name;
212 } flags[] = {
213 #define FE(x) { x, #x}
214 FE(DIEDFL_ALLDEVICES),
215 FE(DIEDFL_ATTACHEDONLY),
216 FE(DIEDFL_FORCEFEEDBACK),
217 FE(DIEDFL_INCLUDEALIASES),
218 FE(DIEDFL_INCLUDEPHANTOMS),
219 FE(DIEDFL_INCLUDEHIDDEN)
220 #undef FE
221 };
222 TRACE(" flags: ");
223 if (dwFlags == 0) {
224 TRACE("DIEDFL_ALLDEVICES\n");
225 return;
226 }
227 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
228 if (flags[i].mask & dwFlags)
229 TRACE("%s ",flags[i].name);
230 }
231 TRACE("\n");
232 }
233
234 static void _dump_diactionformatA(LPDIACTIONFORMATA lpdiActionFormat)
235 {
236 unsigned int i;
237
238 TRACE("diaf.dwSize = %d\n", lpdiActionFormat->dwSize);
239 TRACE("diaf.dwActionSize = %d\n", lpdiActionFormat->dwActionSize);
240 TRACE("diaf.dwDataSize = %d\n", lpdiActionFormat->dwDataSize);
241 TRACE("diaf.dwNumActions = %d\n", lpdiActionFormat->dwNumActions);
242 TRACE("diaf.rgoAction = %p\n", lpdiActionFormat->rgoAction);
243 TRACE("diaf.guidActionMap = %s\n", debugstr_guid(&lpdiActionFormat->guidActionMap));
244 TRACE("diaf.dwGenre = 0x%08x\n", lpdiActionFormat->dwGenre);
245 TRACE("diaf.dwBufferSize = %d\n", lpdiActionFormat->dwBufferSize);
246 TRACE("diaf.lAxisMin = %d\n", lpdiActionFormat->lAxisMin);
247 TRACE("diaf.lAxisMax = %d\n", lpdiActionFormat->lAxisMax);
248 TRACE("diaf.hInstString = %p\n", lpdiActionFormat->hInstString);
249 TRACE("diaf.ftTimeStamp ...\n");
250 TRACE("diaf.dwCRC = 0x%x\n", lpdiActionFormat->dwCRC);
251 TRACE("diaf.tszActionMap = %s\n", debugstr_a(lpdiActionFormat->tszActionMap));
252 for (i = 0; i < lpdiActionFormat->dwNumActions; i++)
253 {
254 TRACE("diaf.rgoAction[%u]:\n", i);
255 TRACE("\tuAppData=0x%lx\n", lpdiActionFormat->rgoAction[i].uAppData);
256 TRACE("\tdwSemantic=0x%08x\n", lpdiActionFormat->rgoAction[i].dwSemantic);
257 TRACE("\tdwFlags=0x%x\n", lpdiActionFormat->rgoAction[i].dwFlags);
258 TRACE("\tszActionName=%s\n", debugstr_a(lpdiActionFormat->rgoAction[i].u.lptszActionName));
259 TRACE("\tguidInstance=%s\n", debugstr_guid(&lpdiActionFormat->rgoAction[i].guidInstance));
260 TRACE("\tdwObjID=0x%x\n", lpdiActionFormat->rgoAction[i].dwObjID);
261 TRACE("\tdwHow=0x%x\n", lpdiActionFormat->rgoAction[i].dwHow);
262 }
263 }
264
265 void _copy_diactionformatAtoW(LPDIACTIONFORMATW to, LPDIACTIONFORMATA from)
266 {
267 int i;
268
269 to->dwSize = sizeof(DIACTIONFORMATW);
270 to->dwActionSize = sizeof(DIACTIONW);
271 to->dwDataSize = from->dwDataSize;
272 to->dwNumActions = from->dwNumActions;
273 to->guidActionMap = from->guidActionMap;
274 to->dwGenre = from->dwGenre;
275 to->dwBufferSize = from->dwBufferSize;
276 to->lAxisMin = from->lAxisMin;
277 to->lAxisMax = from->lAxisMax;
278 to->dwCRC = from->dwCRC;
279 to->ftTimeStamp = from->ftTimeStamp;
280
281 for (i=0; i < to->dwNumActions; i++)
282 {
283 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
284 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
285 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
286 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
287 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
288 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
289 }
290 }
291
292 void _copy_diactionformatWtoA(LPDIACTIONFORMATA to, LPDIACTIONFORMATW from)
293 {
294 int i;
295
296 to->dwSize = sizeof(DIACTIONFORMATA);
297 to->dwActionSize = sizeof(DIACTIONA);
298 to->dwDataSize = from->dwDataSize;
299 to->dwNumActions = from->dwNumActions;
300 to->guidActionMap = from->guidActionMap;
301 to->dwGenre = from->dwGenre;
302 to->dwBufferSize = from->dwBufferSize;
303 to->lAxisMin = from->lAxisMin;
304 to->lAxisMax = from->lAxisMax;
305 to->dwCRC = from->dwCRC;
306 to->ftTimeStamp = from->ftTimeStamp;
307
308 for (i=0; i < to->dwNumActions; i++)
309 {
310 to->rgoAction[i].uAppData = from->rgoAction[i].uAppData;
311 to->rgoAction[i].dwSemantic = from->rgoAction[i].dwSemantic;
312 to->rgoAction[i].dwFlags = from->rgoAction[i].dwFlags;
313 to->rgoAction[i].guidInstance = from->rgoAction[i].guidInstance;
314 to->rgoAction[i].dwObjID = from->rgoAction[i].dwObjID;
315 to->rgoAction[i].dwHow = from->rgoAction[i].dwHow;
316 }
317 }
318
319 /* diactionformat_priority
320 *
321 * Given a DIACTIONFORMAT structure and a DI genre, returns the enumeration
322 * priority. Joysticks should pass the game genre, and mouse or keyboard their
323 * respective DI*_MASK
324 */
325 static DWORD diactionformat_priorityA(LPDIACTIONFORMATA lpdiaf, DWORD genre)
326 {
327 int i;
328 DWORD priorityFlags = 0;
329
330 /* If there's at least one action for the device it's priority 1 */
331 for(i=0; i < lpdiaf->dwNumActions; i++)
332 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
333 priorityFlags |= DIEDBS_MAPPEDPRI1;
334
335 return priorityFlags;
336 }
337
338 static DWORD diactionformat_priorityW(LPDIACTIONFORMATW lpdiaf, DWORD genre)
339 {
340 int i;
341 DWORD priorityFlags = 0;
342
343 /* If there's at least one action for the device it's priority 1 */
344 for(i=0; i < lpdiaf->dwNumActions; i++)
345 if ((lpdiaf->rgoAction[i].dwSemantic & genre) == genre)
346 priorityFlags |= DIEDBS_MAPPEDPRI1;
347
348 return priorityFlags;
349 }
350
351 /******************************************************************************
352 * IDirectInputA_EnumDevices
353 */
354 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
355 LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
356 LPVOID pvRef, DWORD dwFlags)
357 {
358 IDirectInputImpl *This = impl_from_IDirectInput7A(iface);
359 DIDEVICEINSTANCEA devInstance;
360 unsigned int i;
361 int j, r;
362
363 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
364 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
365 lpCallback, pvRef, dwFlags);
366 _dump_EnumDevices_dwFlags(dwFlags);
367
368 if (!lpCallback ||
369 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
370 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
371 return DIERR_INVALIDPARAM;
372
373 if (!This->initialized)
374 return DIERR_NOTINITIALIZED;
375
376 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
377 if (!dinput_devices[i]->enum_deviceA) continue;
378 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
379 devInstance.dwSize = sizeof(devInstance);
380 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
381 r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
382 if (r == S_OK)
383 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
384 return S_OK;
385 }
386 }
387
388 return S_OK;
389 }
390 /******************************************************************************
391 * IDirectInputW_EnumDevices
392 */
393 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
394 LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
395 LPVOID pvRef, DWORD dwFlags)
396 {
397 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
398 DIDEVICEINSTANCEW devInstance;
399 unsigned int i;
400 int j;
401 HRESULT r;
402
403 TRACE("(this=%p,0x%04x '%s',%p,%p,%04x)\n",
404 This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
405 lpCallback, pvRef, dwFlags);
406 _dump_EnumDevices_dwFlags(dwFlags);
407
408 if (!lpCallback ||
409 dwFlags & ~(DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK | DIEDFL_INCLUDEALIASES | DIEDFL_INCLUDEPHANTOMS | DIEDFL_INCLUDEHIDDEN) ||
410 (dwDevType > DI8DEVCLASS_GAMECTRL && dwDevType < DI8DEVTYPE_DEVICE) || dwDevType > DI8DEVTYPE_SUPPLEMENTAL)
411 return DIERR_INVALIDPARAM;
412
413 if (!This->initialized)
414 return DIERR_NOTINITIALIZED;
415
416 for (i = 0; i < NB_DINPUT_DEVICES; i++) {
417 if (!dinput_devices[i]->enum_deviceW) continue;
418 for (j = 0, r = S_OK; SUCCEEDED(r); j++) {
419 devInstance.dwSize = sizeof(devInstance);
420 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
421 r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j);
422 if (r == S_OK)
423 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
424 return S_OK;
425 }
426 }
427
428 return S_OK;
429 }
430
431 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
432 {
433 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
434 ULONG ref = InterlockedIncrement(&This->ref);
435
436 TRACE( "(%p) incrementing from %d\n", This, ref - 1);
437 return ref;
438 }
439
440 static ULONG WINAPI IDirectInputWImpl_AddRef(LPDIRECTINPUT7W iface)
441 {
442 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
443 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
444 }
445
446 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
447 {
448 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
449 ULONG ref = InterlockedDecrement( &This->ref );
450
451 TRACE( "(%p) releasing from %d\n", This, ref + 1 );
452
453 if (ref == 0)
454 {
455 uninitialize_directinput_instance( This );
456 HeapFree( GetProcessHeap(), 0, This );
457 }
458
459 return ref;
460 }
461
462 static ULONG WINAPI IDirectInputWImpl_Release(LPDIRECTINPUT7W iface)
463 {
464 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
465 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
466 }
467
468 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj)
469 {
470 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
471
472 TRACE( "(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj );
473
474 if (!riid || !ppobj)
475 return E_POINTER;
476
477 if (IsEqualGUID( &IID_IUnknown, riid ) ||
478 IsEqualGUID( &IID_IDirectInputA, riid ) ||
479 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
480 IsEqualGUID( &IID_IDirectInput7A, riid ))
481 {
482 *ppobj = &This->IDirectInput7A_iface;
483 IUnknown_AddRef( (IUnknown*)*ppobj );
484
485 return DI_OK;
486 }
487
488 if (IsEqualGUID( &IID_IDirectInputW, riid ) ||
489 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
490 IsEqualGUID( &IID_IDirectInput7W, riid ))
491 {
492 *ppobj = &This->IDirectInput7W_iface;
493 IUnknown_AddRef( (IUnknown*)*ppobj );
494
495 return DI_OK;
496 }
497
498 if (IsEqualGUID( &IID_IDirectInput8A, riid ))
499 {
500 *ppobj = &This->IDirectInput8A_iface;
501 IUnknown_AddRef( (IUnknown*)*ppobj );
502
503 return DI_OK;
504 }
505
506 if (IsEqualGUID( &IID_IDirectInput8W, riid ))
507 {
508 *ppobj = &This->IDirectInput8W_iface;
509 IUnknown_AddRef( (IUnknown*)*ppobj );
510
511 return DI_OK;
512 }
513
514 if (IsEqualGUID( &IID_IDirectInputJoyConfig8, riid ))
515 {
516 *ppobj = &This->IDirectInputJoyConfig8_iface;
517 IUnknown_AddRef( (IUnknown*)*ppobj );
518
519 return DI_OK;
520 }
521
522 FIXME( "Unsupported interface: %s\n", debugstr_guid(riid));
523 *ppobj = NULL;
524 return E_NOINTERFACE;
525 }
526
527 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj)
528 {
529 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
530 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
531 }
532
533 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion)
534 {
535 if (!This->initialized)
536 {
537 This->dwVersion = dwVersion;
538 This->evsequence = 1;
539
540 InitializeCriticalSection( &This->crit );
541 This->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectInputImpl*->crit");
542
543 list_init( &This->devices_list );
544
545 /* Add self to the list of the IDirectInputs */
546 EnterCriticalSection( &dinput_hook_crit );
547 list_add_head( &direct_input_list, &This->entry );
548 LeaveCriticalSection( &dinput_hook_crit );
549
550 This->initialized = TRUE;
551
552 if (!check_hook_thread())
553 {
554 uninitialize_directinput_instance( This );
555 return DIERR_GENERIC;
556 }
557 }
558
559 return DI_OK;
560 }
561
562 static void uninitialize_directinput_instance(IDirectInputImpl *This)
563 {
564 if (This->initialized)
565 {
566 /* Remove self from the list of the IDirectInputs */
567 EnterCriticalSection( &dinput_hook_crit );
568 list_remove( &This->entry );
569 LeaveCriticalSection( &dinput_hook_crit );
570
571 check_hook_thread();
572
573 This->crit.DebugInfo->Spare[0] = 0;
574 DeleteCriticalSection( &This->crit );
575
576 This->initialized = FALSE;
577 }
578 }
579
580 enum directinput_versions
581 {
582 DIRECTINPUT_VERSION_300 = 0x0300,
583 DIRECTINPUT_VERSION_500 = 0x0500,
584 DIRECTINPUT_VERSION_50A = 0x050A,
585 DIRECTINPUT_VERSION_5B2 = 0x05B2,
586 DIRECTINPUT_VERSION_602 = 0x0602,
587 DIRECTINPUT_VERSION_61A = 0x061A,
588 DIRECTINPUT_VERSION_700 = 0x0700,
589 };
590
591 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD version)
592 {
593 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
594
595 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
596
597 if (!hinst)
598 return DIERR_INVALIDPARAM;
599 else if (version == 0)
600 return DIERR_NOTINITIALIZED;
601 else if (version > DIRECTINPUT_VERSION_700)
602 return DIERR_OLDDIRECTINPUTVERSION;
603 else if (version != DIRECTINPUT_VERSION_300 && version != DIRECTINPUT_VERSION_500 &&
604 version != DIRECTINPUT_VERSION_50A && version != DIRECTINPUT_VERSION_5B2 &&
605 version != DIRECTINPUT_VERSION_602 && version != DIRECTINPUT_VERSION_61A &&
606 version != DIRECTINPUT_VERSION_700 && version != DIRECTINPUT_VERSION)
607 return DIERR_BETADIRECTINPUTVERSION;
608
609 return initialize_directinput_instance(This, version);
610 }
611
612 static HRESULT WINAPI IDirectInputWImpl_Initialize(LPDIRECTINPUT7W iface, HINSTANCE hinst, DWORD x)
613 {
614 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
615 return IDirectInputAImpl_Initialize( &This->IDirectInput7A_iface, hinst, x );
616 }
617
618 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface, REFGUID rguid)
619 {
620 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
621 HRESULT hr;
622 LPDIRECTINPUTDEVICEA device;
623
624 TRACE( "(%p)->(%s)\n", This, debugstr_guid(rguid) );
625
626 if (!rguid) return E_POINTER;
627 if (!This->initialized)
628 return DIERR_NOTINITIALIZED;
629
630 hr = IDirectInput_CreateDevice( iface, rguid, &device, NULL );
631 if (hr != DI_OK) return DI_NOTATTACHED;
632
633 IUnknown_Release( device );
634
635 return DI_OK;
636 }
637
638 static HRESULT WINAPI IDirectInputWImpl_GetDeviceStatus(LPDIRECTINPUT7W iface, REFGUID rguid)
639 {
640 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
641 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
642 }
643
644 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
645 HWND hwndOwner,
646 DWORD dwFlags)
647 {
648 WCHAR control_exeW[] = {'c','o','n','t','r','o','l','.','e','x','e',0};
649 STARTUPINFOW si = {0};
650 PROCESS_INFORMATION pi;
651
652 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
653
654 TRACE( "(%p)->(%p, %08x)\n", This, hwndOwner, dwFlags );
655
656 if (hwndOwner && !IsWindow(hwndOwner))
657 return E_HANDLE;
658
659 if (dwFlags)
660 return DIERR_INVALIDPARAM;
661
662 if (!This->initialized)
663 return DIERR_NOTINITIALIZED;
664
665 if (!CreateProcessW(NULL, control_exeW, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
666 return HRESULT_FROM_WIN32(GetLastError());
667
668 return DI_OK;
669 }
670
671 static HRESULT WINAPI IDirectInputWImpl_RunControlPanel(LPDIRECTINPUT7W iface, HWND hwndOwner, DWORD dwFlags)
672 {
673 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
674 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
675 }
676
677 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
678 LPCSTR pszName, LPGUID pguidInstance)
679 {
680 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
681
682 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance );
683
684 return DI_OK;
685 }
686
687 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
688 LPCWSTR pszName, LPGUID pguidInstance)
689 {
690 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
691
692 FIXME( "(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance );
693
694 return DI_OK;
695 }
696
697 static HRESULT create_device(IDirectInputImpl *This, REFGUID rguid, REFIID riid, LPVOID *pvOut, BOOL unicode)
698 {
699 unsigned int i;
700
701 if (pvOut)
702 *pvOut = NULL;
703
704 if (!rguid || !pvOut)
705 return E_POINTER;
706
707 if (!This->initialized)
708 return DIERR_NOTINITIALIZED;
709
710 /* Loop on all the devices to see if anyone matches the given GUID */
711 for (i = 0; i < NB_DINPUT_DEVICES; i++)
712 {
713 HRESULT ret;
714
715 if (!dinput_devices[i]->create_device) continue;
716 if ((ret = dinput_devices[i]->create_device(This, rguid, riid, pvOut, unicode)) == DI_OK)
717 return DI_OK;
718 }
719
720 WARN("invalid device GUID %s\n", debugstr_guid(rguid));
721 return DIERR_DEVICENOTREG;
722 }
723
724 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
725 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
726 {
727 IDirectInputImpl *This = impl_from_IDirectInput7A( iface );
728
729 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
730
731 return create_device(This, rguid, riid, pvOut, FALSE);
732 }
733
734 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
735 REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
736 {
737 IDirectInputImpl *This = impl_from_IDirectInput7W( iface );
738
739 TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
740
741 return create_device(This, rguid, riid, pvOut, TRUE);
742 }
743
744 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
745 LPDIRECTINPUTDEVICEA* pdev, LPUNKNOWN punk)
746 {
747 return IDirectInput7AImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
748 }
749
750 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
751 LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk)
752 {
753 return IDirectInput7WImpl_CreateDeviceEx(iface, rguid, NULL, (LPVOID*)pdev, punk);
754 }
755
756 /*******************************************************************************
757 * DirectInput8
758 */
759
760 static ULONG WINAPI IDirectInput8AImpl_AddRef(LPDIRECTINPUT8A iface)
761 {
762 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
763 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
764 }
765
766 static ULONG WINAPI IDirectInput8WImpl_AddRef(LPDIRECTINPUT8W iface)
767 {
768 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
769 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
770 }
771
772 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj)
773 {
774 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
775 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
776 }
777
778 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj)
779 {
780 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
781 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
782 }
783
784 static ULONG WINAPI IDirectInput8AImpl_Release(LPDIRECTINPUT8A iface)
785 {
786 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
787 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
788 }
789
790 static ULONG WINAPI IDirectInput8WImpl_Release(LPDIRECTINPUT8W iface)
791 {
792 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
793 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
794 }
795
796 static HRESULT WINAPI IDirectInput8AImpl_CreateDevice(LPDIRECTINPUT8A iface, REFGUID rguid,
797 LPDIRECTINPUTDEVICE8A* pdev, LPUNKNOWN punk)
798 {
799 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
800 return IDirectInput7AImpl_CreateDeviceEx( &This->IDirectInput7A_iface, rguid, NULL, (LPVOID*)pdev, punk );
801 }
802
803 static HRESULT WINAPI IDirectInput8WImpl_CreateDevice(LPDIRECTINPUT8W iface, REFGUID rguid,
804 LPDIRECTINPUTDEVICE8W* pdev, LPUNKNOWN punk)
805 {
806 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
807 return IDirectInput7WImpl_CreateDeviceEx( &This->IDirectInput7W_iface, rguid, NULL, (LPVOID*)pdev, punk );
808 }
809
810 static HRESULT WINAPI IDirectInput8AImpl_EnumDevices(LPDIRECTINPUT8A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
811 LPVOID pvRef, DWORD dwFlags)
812 {
813 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
814 return IDirectInputAImpl_EnumDevices( &This->IDirectInput7A_iface, dwDevType, lpCallback, pvRef, dwFlags );
815 }
816
817 static HRESULT WINAPI IDirectInput8WImpl_EnumDevices(LPDIRECTINPUT8W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
818 LPVOID pvRef, DWORD dwFlags)
819 {
820 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
821 return IDirectInputWImpl_EnumDevices( &This->IDirectInput7W_iface, dwDevType, lpCallback, pvRef, dwFlags );
822 }
823
824 static HRESULT WINAPI IDirectInput8AImpl_GetDeviceStatus(LPDIRECTINPUT8A iface, REFGUID rguid)
825 {
826 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
827 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
828 }
829
830 static HRESULT WINAPI IDirectInput8WImpl_GetDeviceStatus(LPDIRECTINPUT8W iface, REFGUID rguid)
831 {
832 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
833 return IDirectInputAImpl_GetDeviceStatus( &This->IDirectInput7A_iface, rguid );
834 }
835
836 static HRESULT WINAPI IDirectInput8AImpl_RunControlPanel(LPDIRECTINPUT8A iface, HWND hwndOwner, DWORD dwFlags)
837 {
838 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
839 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
840 }
841
842 static HRESULT WINAPI IDirectInput8WImpl_RunControlPanel(LPDIRECTINPUT8W iface, HWND hwndOwner, DWORD dwFlags)
843 {
844 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
845 return IDirectInputAImpl_RunControlPanel( &This->IDirectInput7A_iface, hwndOwner, dwFlags );
846 }
847
848 static HRESULT WINAPI IDirectInput8AImpl_Initialize(LPDIRECTINPUT8A iface, HINSTANCE hinst, DWORD version)
849 {
850 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
851
852 TRACE("(%p)->(%p, 0x%04x)\n", iface, hinst, version);
853
854 if (!hinst)
855 return DIERR_INVALIDPARAM;
856 else if (version == 0)
857 return DIERR_NOTINITIALIZED;
858 else if (version < DIRECTINPUT_VERSION)
859 return DIERR_BETADIRECTINPUTVERSION;
860 else if (version > DIRECTINPUT_VERSION)
861 return DIERR_OLDDIRECTINPUTVERSION;
862
863 return initialize_directinput_instance(This, version);
864 }
865
866 static HRESULT WINAPI IDirectInput8WImpl_Initialize(LPDIRECTINPUT8W iface, HINSTANCE hinst, DWORD version)
867 {
868 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
869 return IDirectInput8AImpl_Initialize( &This->IDirectInput8A_iface, hinst, version );
870 }
871
872 static HRESULT WINAPI IDirectInput8AImpl_FindDevice(LPDIRECTINPUT8A iface, REFGUID rguid, LPCSTR pszName, LPGUID pguidInstance)
873 {
874 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
875 return IDirectInput2AImpl_FindDevice( &This->IDirectInput7A_iface, rguid, pszName, pguidInstance );
876 }
877
878 static HRESULT WINAPI IDirectInput8WImpl_FindDevice(LPDIRECTINPUT8W iface, REFGUID rguid, LPCWSTR pszName, LPGUID pguidInstance)
879 {
880 IDirectInputImpl *This = impl_from_IDirectInput8W( iface );
881 return IDirectInput2WImpl_FindDevice( &This->IDirectInput7W_iface, rguid, pszName, pguidInstance );
882 }
883
884 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
885 LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
886 LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
887 LPVOID pvRef, DWORD dwFlags
888 )
889 {
890 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
891 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
892 IDirectInputImpl *This = impl_from_IDirectInput8A( iface );
893 DIDEVICEINSTANCEA didevi;
894 LPDIRECTINPUTDEVICE8A lpdid;
895 DWORD callbackFlags;
896 int i, j;
897
898
899 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_a(ptszUserName), lpdiActionFormat,
900 lpCallback, pvRef, dwFlags);
901 #define X(x) if (dwFlags & x) FIXME("\tdwFlags |= "#x"\n");
902 X(DIEDBSFL_ATTACHEDONLY)
903 X(DIEDBSFL_THISUSER)
904 X(DIEDBSFL_FORCEFEEDBACK)
905 X(DIEDBSFL_AVAILABLEDEVICES)
906 X(DIEDBSFL_MULTIMICEKEYBOARDS)
907 X(DIEDBSFL_NONGAMINGDEVICES)
908 #undef X
909
910 _dump_diactionformatA(lpdiActionFormat);
911
912 didevi.dwSize = sizeof(didevi);
913
914 /* Enumerate all the joysticks */
915 for (i = 0; i < NB_DINPUT_DEVICES; i++)
916 {
917 HRESULT enumSuccess;
918
919 if (!dinput_devices[i]->enum_deviceA) continue;
920
921 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
922 {
923 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
924
925 callbackFlags = diactionformat_priorityA(lpdiActionFormat, lpdiActionFormat->dwGenre);
926 /* Default behavior is to enumerate attached game controllers */
927 enumSuccess = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
928 if (enumSuccess == S_OK)
929 {
930 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
931
932 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
933 return DI_OK;
934 }
935 }
936 }
937
938 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
939
940 /* Enumerate keyboard and mouse */
941 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
942 {
943 callbackFlags = diactionformat_priorityA(lpdiActionFormat, actionMasks[i]);
944
945 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
946 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
947
948 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
949 return DI_OK;
950 }
951
952 return DI_OK;
953 }
954
955 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
956 LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
957 LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
958 LPVOID pvRef, DWORD dwFlags
959 )
960 {
961 static REFGUID guids[2] = { &GUID_SysKeyboard, &GUID_SysMouse };
962 static const DWORD actionMasks[] = { DIKEYBOARD_MASK, DIMOUSE_MASK };
963 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
964 DIDEVICEINSTANCEW didevi;
965 LPDIRECTINPUTDEVICE8W lpdid;
966 DWORD callbackFlags;
967 int i, j;
968
969 FIXME("(this=%p,%s,%p,%p,%p,%04x): semi-stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
970 lpCallback, pvRef, dwFlags);
971
972 didevi.dwSize = sizeof(didevi);
973
974 /* Enumerate all the joysticks */
975 for (i = 0; i < NB_DINPUT_DEVICES; i++)
976 {
977 HRESULT enumSuccess;
978
979 if (!dinput_devices[i]->enum_deviceW) continue;
980
981 for (j = 0, enumSuccess = S_OK; SUCCEEDED(enumSuccess); j++)
982 {
983 TRACE(" - checking device %u ('%s')\n", i, dinput_devices[i]->name);
984
985 callbackFlags = diactionformat_priorityW(lpdiActionFormat, lpdiActionFormat->dwGenre);
986 /* Default behavior is to enumerate attached game controllers */
987 enumSuccess = dinput_devices[i]->enum_deviceW(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY | dwFlags, &didevi, This->dwVersion, j);
988 if (enumSuccess == S_OK)
989 {
990 IDirectInput_CreateDevice(iface, &didevi.guidInstance, &lpdid, NULL);
991
992 if (lpCallback(&didevi, lpdid, callbackFlags, 0, pvRef) == DIENUM_STOP)
993 return DI_OK;
994 }
995 }
996 }
997
998 if (dwFlags & DIEDBSFL_FORCEFEEDBACK) return DI_OK;
999
1000 /* Enumerate keyboard and mouse */
1001 for(i=0; i < sizeof(guids)/sizeof(guids[0]); i++)
1002 {
1003 callbackFlags = diactionformat_priorityW(lpdiActionFormat, actionMasks[i]);
1004
1005 IDirectInput_CreateDevice(iface, guids[i], &lpdid, NULL);
1006 IDirectInputDevice_GetDeviceInfo(lpdid, &didevi);
1007
1008 if (lpCallback(&didevi, lpdid, callbackFlags, sizeof(guids)/sizeof(guids[0]) - (i+1), pvRef) == DIENUM_STOP)
1009 return DI_OK;
1010 }
1011
1012 return DI_OK;
1013 }
1014
1015 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
1016 LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1017 LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1018 )
1019 {
1020 IDirectInputImpl *This = impl_from_IDirectInput8W(iface);
1021
1022 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1023
1024 /* Call helper function in config.c to do the real work */
1025 return _configure_devices(iface, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1026 }
1027
1028 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
1029 LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
1030 LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
1031 )
1032 {
1033 IDirectInputImpl *This = impl_from_IDirectInput8A(iface);
1034 DIACTIONFORMATW diafW;
1035 DICONFIGUREDEVICESPARAMSW diCDParamsW;
1036 HRESULT hr;
1037 int i;
1038
1039 FIXME("(this=%p,%p,%p,%04x,%p): stub\n", This, lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
1040
1041 /* Copy parameters */
1042 diCDParamsW.dwSize = sizeof(DICONFIGUREDEVICESPARAMSW);
1043 diCDParamsW.dwcFormats = lpdiCDParams->dwcFormats;
1044 diCDParamsW.lprgFormats = &diafW;
1045 diCDParamsW.hwnd = lpdiCDParams->hwnd;
1046
1047 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiCDParams->lprgFormats->dwNumActions);
1048 _copy_diactionformatAtoW(&diafW, lpdiCDParams->lprgFormats);
1049
1050 /* Copy action names */
1051 for (i=0; i < diafW.dwNumActions; i++)
1052 {
1053 const char* from = lpdiCDParams->lprgFormats->rgoAction[i].u.lptszActionName;
1054 int len = MultiByteToWideChar(CP_ACP, 0, from , -1, NULL , 0);
1055 WCHAR *to = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
1056
1057 MultiByteToWideChar(CP_ACP, 0, from , -1, to , len);
1058 diafW.rgoAction[i].u.lptszActionName = to;
1059 }
1060
1061 hr = IDirectInput8WImpl_ConfigureDevices(&This->IDirectInput8W_iface, lpdiCallback, &diCDParamsW, dwFlags, pvRefData);
1062
1063 /* Copy back configuration */
1064 if (SUCCEEDED(hr))
1065 _copy_diactionformatWtoA(lpdiCDParams->lprgFormats, &diafW);
1066
1067 /* Free memory */
1068 for (i=0; i < diafW.dwNumActions; i++)
1069 HeapFree(GetProcessHeap(), 0, (void*) diafW.rgoAction[i].u.lptszActionName);
1070
1071 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1072
1073 return hr;
1074 }
1075
1076 /*****************************************************************************
1077 * IDirectInputJoyConfig8 interface
1078 */
1079
1080 static inline IDirectInputImpl *impl_from_IDirectInputJoyConfig8(IDirectInputJoyConfig8 *iface)
1081 {
1082 return CONTAINING_RECORD( iface, IDirectInputImpl, IDirectInputJoyConfig8_iface );
1083 }
1084
1085 static HRESULT WINAPI JoyConfig8Impl_QueryInterface(IDirectInputJoyConfig8 *iface, REFIID riid, void** ppobj)
1086 {
1087 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1088 return IDirectInputAImpl_QueryInterface( &This->IDirectInput7A_iface, riid, ppobj );
1089 }
1090
1091 static ULONG WINAPI JoyConfig8Impl_AddRef(IDirectInputJoyConfig8 *iface)
1092 {
1093 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1094 return IDirectInputAImpl_AddRef( &This->IDirectInput7A_iface );
1095 }
1096
1097 static ULONG WINAPI JoyConfig8Impl_Release(IDirectInputJoyConfig8 *iface)
1098 {
1099 IDirectInputImpl *This = impl_from_IDirectInputJoyConfig8( iface );
1100 return IDirectInputAImpl_Release( &This->IDirectInput7A_iface );
1101 }
1102
1103 static HRESULT WINAPI JoyConfig8Impl_Acquire(IDirectInputJoyConfig8 *iface)
1104 {
1105 FIXME( "(%p): stub!\n", iface );
1106 return E_NOTIMPL;
1107 }
1108
1109 static HRESULT WINAPI JoyConfig8Impl_Unacquire(IDirectInputJoyConfig8 *iface)
1110 {
1111 FIXME( "(%p): stub!\n", iface );
1112 return E_NOTIMPL;
1113 }
1114
1115 static HRESULT WINAPI JoyConfig8Impl_SetCooperativeLevel(IDirectInputJoyConfig8 *iface, HWND hwnd, DWORD flags)
1116 {
1117 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, hwnd, flags );
1118 return E_NOTIMPL;
1119 }
1120
1121 static HRESULT WINAPI JoyConfig8Impl_SendNotify(IDirectInputJoyConfig8 *iface)
1122 {
1123 FIXME( "(%p): stub!\n", iface );
1124 return E_NOTIMPL;
1125 }
1126
1127 static HRESULT WINAPI JoyConfig8Impl_EnumTypes(IDirectInputJoyConfig8 *iface, LPDIJOYTYPECALLBACK cb, void *ref)
1128 {
1129 FIXME( "(%p)->(%p, %p): stub!\n", iface, cb, ref );
1130 return E_NOTIMPL;
1131 }
1132
1133 static HRESULT WINAPI JoyConfig8Impl_GetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPDIJOYTYPEINFO info, DWORD flags)
1134 {
1135 FIXME( "(%p)->(%s, %p, 0x%08x): stub!\n", iface, debugstr_w(name), info, flags );
1136 return E_NOTIMPL;
1137 }
1138
1139 static HRESULT WINAPI JoyConfig8Impl_SetTypeInfo(IDirectInputJoyConfig8 *iface, LPCWSTR name, LPCDIJOYTYPEINFO info, DWORD flags,
1140 LPWSTR new_name)
1141 {
1142 FIXME( "(%p)->(%s, %p, 0x%08x, %s): stub!\n", iface, debugstr_w(name), info, flags, debugstr_w(new_name) );
1143 return E_NOTIMPL;
1144 }
1145
1146 static HRESULT WINAPI JoyConfig8Impl_DeleteType(IDirectInputJoyConfig8 *iface, LPCWSTR name)
1147 {
1148 FIXME( "(%p)->(%s): stub!\n", iface, debugstr_w(name) );
1149 return E_NOTIMPL;
1150 }
1151
1152 static HRESULT WINAPI JoyConfig8Impl_GetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPDIJOYCONFIG info, DWORD flags)
1153 {
1154 IDirectInputImpl *di = impl_from_IDirectInputJoyConfig8(iface);
1155 UINT found = 0;
1156 int i, j;
1157 HRESULT r;
1158
1159 FIXME("(%p)->(%d, %p, 0x%08x): semi-stub!\n", iface, id, info, flags);
1160
1161 #define X(x) if (flags & x) FIXME("\tflags |= "#x"\n");
1162 X(DIJC_GUIDINSTANCE)
1163 X(DIJC_REGHWCONFIGTYPE)
1164 X(DIJC_GAIN)
1165 X(DIJC_CALLOUT)
1166 #undef X
1167
1168 /* Enumerate all joysticks in order */
1169 for (i = 0; i < NB_DINPUT_DEVICES; i++)
1170 {
1171 if (!dinput_devices[i]->enum_deviceA) continue;
1172
1173 for (j = 0, r = S_OK; SUCCEEDED(r); j++)
1174 {
1175 DIDEVICEINSTANCEA dev;
1176 dev.dwSize = sizeof(dev);
1177 if ((r = dinput_devices[i]->enum_deviceA(DI8DEVCLASS_GAMECTRL, 0, &dev, di->dwVersion, j)) == S_OK)
1178 {
1179 /* Only take into account the chosen id */
1180 if (found == id)
1181 {
1182 if (flags & DIJC_GUIDINSTANCE)
1183 info->guidInstance = dev.guidInstance;
1184
1185 return DI_OK;
1186 }
1187 found += 1;
1188 }
1189 }
1190 }
1191
1192 return DIERR_NOMOREITEMS;
1193 }
1194
1195 static HRESULT WINAPI JoyConfig8Impl_SetConfig(IDirectInputJoyConfig8 *iface, UINT id, LPCDIJOYCONFIG info, DWORD flags)
1196 {
1197 FIXME( "(%p)->(%d, %p, 0x%08x): stub!\n", iface, id, info, flags );
1198 return E_NOTIMPL;
1199 }
1200
1201 static HRESULT WINAPI JoyConfig8Impl_DeleteConfig(IDirectInputJoyConfig8 *iface, UINT id)
1202 {
1203 FIXME( "(%p)->(%d): stub!\n", iface, id );
1204 return E_NOTIMPL;
1205 }
1206
1207 static HRESULT WINAPI JoyConfig8Impl_GetUserValues(IDirectInputJoyConfig8 *iface, LPDIJOYUSERVALUES info, DWORD flags)
1208 {
1209 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1210 return E_NOTIMPL;
1211 }
1212
1213 static HRESULT WINAPI JoyConfig8Impl_SetUserValues(IDirectInputJoyConfig8 *iface, LPCDIJOYUSERVALUES info, DWORD flags)
1214 {
1215 FIXME( "(%p)->(%p, 0x%08x): stub!\n", iface, info, flags );
1216 return E_NOTIMPL;
1217 }
1218
1219 static HRESULT WINAPI JoyConfig8Impl_AddNewHardware(IDirectInputJoyConfig8 *iface, HWND hwnd, REFGUID guid)
1220 {
1221 FIXME( "(%p)->(%p, %s): stub!\n", iface, hwnd, debugstr_guid(guid) );
1222 return E_NOTIMPL;
1223 }
1224
1225 static HRESULT WINAPI JoyConfig8Impl_OpenTypeKey(IDirectInputJoyConfig8 *iface, LPCWSTR name, DWORD security, PHKEY key)
1226 {
1227 FIXME( "(%p)->(%s, 0x%08x, %p): stub!\n", iface, debugstr_w(name), security, key );
1228 return E_NOTIMPL;
1229 }
1230
1231 static HRESULT WINAPI JoyConfig8Impl_OpenAppStatusKey(IDirectInputJoyConfig8 *iface, PHKEY key)
1232 {
1233 FIXME( "(%p)->(%p): stub!\n", iface, key );
1234 return E_NOTIMPL;
1235 }
1236
1237 static const IDirectInput7AVtbl ddi7avt = {
1238 IDirectInputAImpl_QueryInterface,
1239 IDirectInputAImpl_AddRef,
1240 IDirectInputAImpl_Release,
1241 IDirectInputAImpl_CreateDevice,
1242 IDirectInputAImpl_EnumDevices,
1243 IDirectInputAImpl_GetDeviceStatus,
1244 IDirectInputAImpl_RunControlPanel,
1245 IDirectInputAImpl_Initialize,
1246 IDirectInput2AImpl_FindDevice,
1247 IDirectInput7AImpl_CreateDeviceEx
1248 };
1249
1250 static const IDirectInput7WVtbl ddi7wvt = {
1251 IDirectInputWImpl_QueryInterface,
1252 IDirectInputWImpl_AddRef,
1253 IDirectInputWImpl_Release,
1254 IDirectInputWImpl_CreateDevice,
1255 IDirectInputWImpl_EnumDevices,
1256 IDirectInputWImpl_GetDeviceStatus,
1257 IDirectInputWImpl_RunControlPanel,
1258 IDirectInputWImpl_Initialize,
1259 IDirectInput2WImpl_FindDevice,
1260 IDirectInput7WImpl_CreateDeviceEx
1261 };
1262
1263 static const IDirectInput8AVtbl ddi8avt = {
1264 IDirectInput8AImpl_QueryInterface,
1265 IDirectInput8AImpl_AddRef,
1266 IDirectInput8AImpl_Release,
1267 IDirectInput8AImpl_CreateDevice,
1268 IDirectInput8AImpl_EnumDevices,
1269 IDirectInput8AImpl_GetDeviceStatus,
1270 IDirectInput8AImpl_RunControlPanel,
1271 IDirectInput8AImpl_Initialize,
1272 IDirectInput8AImpl_FindDevice,
1273 IDirectInput8AImpl_EnumDevicesBySemantics,
1274 IDirectInput8AImpl_ConfigureDevices
1275 };
1276
1277 static const IDirectInput8WVtbl ddi8wvt = {
1278 IDirectInput8WImpl_QueryInterface,
1279 IDirectInput8WImpl_AddRef,
1280 IDirectInput8WImpl_Release,
1281 IDirectInput8WImpl_CreateDevice,
1282 IDirectInput8WImpl_EnumDevices,
1283 IDirectInput8WImpl_GetDeviceStatus,
1284 IDirectInput8WImpl_RunControlPanel,
1285 IDirectInput8WImpl_Initialize,
1286 IDirectInput8WImpl_FindDevice,
1287 IDirectInput8WImpl_EnumDevicesBySemantics,
1288 IDirectInput8WImpl_ConfigureDevices
1289 };
1290
1291 static const IDirectInputJoyConfig8Vtbl JoyConfig8vt =
1292 {
1293 JoyConfig8Impl_QueryInterface,
1294 JoyConfig8Impl_AddRef,
1295 JoyConfig8Impl_Release,
1296 JoyConfig8Impl_Acquire,
1297 JoyConfig8Impl_Unacquire,
1298 JoyConfig8Impl_SetCooperativeLevel,
1299 JoyConfig8Impl_SendNotify,
1300 JoyConfig8Impl_EnumTypes,
1301 JoyConfig8Impl_GetTypeInfo,
1302 JoyConfig8Impl_SetTypeInfo,
1303 JoyConfig8Impl_DeleteType,
1304 JoyConfig8Impl_GetConfig,
1305 JoyConfig8Impl_SetConfig,
1306 JoyConfig8Impl_DeleteConfig,
1307 JoyConfig8Impl_GetUserValues,
1308 JoyConfig8Impl_SetUserValues,
1309 JoyConfig8Impl_AddNewHardware,
1310 JoyConfig8Impl_OpenTypeKey,
1311 JoyConfig8Impl_OpenAppStatusKey
1312 };
1313
1314 /*******************************************************************************
1315 * DirectInput ClassFactory
1316 */
1317 typedef struct
1318 {
1319 /* IUnknown fields */
1320 IClassFactory IClassFactory_iface;
1321 LONG ref;
1322 } IClassFactoryImpl;
1323
1324 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1325 {
1326 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1327 }
1328
1329 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
1330 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1331
1332 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1333 return E_NOINTERFACE;
1334 }
1335
1336 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
1337 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1338 return InterlockedIncrement(&(This->ref));
1339 }
1340
1341 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
1342 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1343 /* static class, won't be freed */
1344 return InterlockedDecrement(&(This->ref));
1345 }
1346
1347 static HRESULT WINAPI DICF_CreateInstance(
1348 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
1349 ) {
1350 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1351
1352 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1353 if ( IsEqualGUID( &IID_IUnknown, riid ) ||
1354 IsEqualGUID( &IID_IDirectInputA, riid ) ||
1355 IsEqualGUID( &IID_IDirectInputW, riid ) ||
1356 IsEqualGUID( &IID_IDirectInput2A, riid ) ||
1357 IsEqualGUID( &IID_IDirectInput2W, riid ) ||
1358 IsEqualGUID( &IID_IDirectInput7A, riid ) ||
1359 IsEqualGUID( &IID_IDirectInput7W, riid ) ) {
1360 return create_directinput_instance(riid, ppobj, NULL);
1361 }
1362
1363 FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1364 return E_NOINTERFACE;
1365 }
1366
1367 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
1368 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1369 FIXME("(%p)->(%d),stub!\n",This,dolock);
1370 return S_OK;
1371 }
1372
1373 static const IClassFactoryVtbl DICF_Vtbl = {
1374 DICF_QueryInterface,
1375 DICF_AddRef,
1376 DICF_Release,
1377 DICF_CreateInstance,
1378 DICF_LockServer
1379 };
1380 static IClassFactoryImpl DINPUT_CF = {{&DICF_Vtbl}, 1 };
1381
1382 /***********************************************************************
1383 * DllCanUnloadNow (DINPUT.@)
1384 */
1385 HRESULT WINAPI DllCanUnloadNow(void)
1386 {
1387 return S_FALSE;
1388 }
1389
1390 /***********************************************************************
1391 * DllGetClassObject (DINPUT.@)
1392 */
1393 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1394 {
1395 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1396 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
1397 *ppv = &DINPUT_CF;
1398 IClassFactory_AddRef((IClassFactory*)*ppv);
1399 return S_OK;
1400 }
1401
1402 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1403 return CLASS_E_CLASSNOTAVAILABLE;
1404 }
1405
1406 /***********************************************************************
1407 * DllRegisterServer (DINPUT.@)
1408 */
1409 HRESULT WINAPI DllRegisterServer(void)
1410 {
1411 return __wine_register_resources( DINPUT_instance );
1412 }
1413
1414 /***********************************************************************
1415 * DllUnregisterServer (DINPUT.@)
1416 */
1417 HRESULT WINAPI DllUnregisterServer(void)
1418 {
1419 return __wine_unregister_resources( DINPUT_instance );
1420 }
1421
1422 /******************************************************************************
1423 * DInput hook thread
1424 */
1425
1426 static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
1427 {
1428 IDirectInputImpl *dinput;
1429 int skip = 0;
1430
1431 if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
1432
1433 EnterCriticalSection( &dinput_hook_crit );
1434 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1435 {
1436 IDirectInputDeviceImpl *dev;
1437
1438 EnterCriticalSection( &dinput->crit );
1439 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1440 if (dev->acquired && dev->event_proc)
1441 {
1442 TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
1443 skip |= dev->event_proc( &dev->IDirectInputDevice8A_iface, wparam, lparam );
1444 }
1445 LeaveCriticalSection( &dinput->crit );
1446 }
1447 LeaveCriticalSection( &dinput_hook_crit );
1448
1449 return skip ? 1 : CallNextHookEx( 0, code, wparam, lparam );
1450 }
1451
1452 static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
1453 {
1454 CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
1455 IDirectInputImpl *dinput;
1456 HWND foreground;
1457
1458 if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
1459 msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
1460 return CallNextHookEx( 0, code, wparam, lparam );
1461
1462 foreground = GetForegroundWindow();
1463
1464 EnterCriticalSection( &dinput_hook_crit );
1465
1466 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1467 {
1468 IDirectInputDeviceImpl *dev;
1469
1470 EnterCriticalSection( &dinput->crit );
1471 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1472 {
1473 if (!dev->acquired) continue;
1474
1475 if (msg->hwnd == dev->win && msg->hwnd != foreground)
1476 {
1477 TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
1478 IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
1479 }
1480 }
1481 LeaveCriticalSection( &dinput->crit );
1482 }
1483 LeaveCriticalSection( &dinput_hook_crit );
1484
1485 return CallNextHookEx( 0, code, wparam, lparam );
1486 }
1487
1488 static DWORD WINAPI hook_thread_proc(void *param)
1489 {
1490 static HHOOK kbd_hook, mouse_hook;
1491 MSG msg;
1492
1493 /* Force creation of the message queue */
1494 PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
1495 SetEvent(*(LPHANDLE)param);
1496
1497 while (GetMessageW( &msg, 0, 0, 0 ))
1498 {
1499 UINT kbd_cnt = 0, mice_cnt = 0;
1500
1501 if (msg.message == WM_USER+0x10)
1502 {
1503 IDirectInputImpl *dinput;
1504
1505 TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
1506
1507 if (!msg.wParam && !msg.lParam)
1508 {
1509 if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
1510 if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
1511 kbd_hook = mouse_hook = NULL;
1512 break;
1513 }
1514
1515 EnterCriticalSection( &dinput_hook_crit );
1516
1517 /* Count acquired keyboards and mice*/
1518 LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
1519 {
1520 IDirectInputDeviceImpl *dev;
1521
1522 EnterCriticalSection( &dinput->crit );
1523 LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDeviceImpl, entry )
1524 {
1525 if (!dev->acquired || !dev->event_proc) continue;
1526
1527 if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
1528 IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
1529 kbd_cnt++;
1530 else
1531 if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
1532 IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
1533 mice_cnt++;
1534 }
1535 LeaveCriticalSection( &dinput->crit );
1536 }
1537 LeaveCriticalSection( &dinput_hook_crit );
1538
1539 if (kbd_cnt && !kbd_hook)
1540 kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
1541 else if (!kbd_cnt && kbd_hook)
1542 {
1543 UnhookWindowsHookEx( kbd_hook );
1544 kbd_hook = NULL;
1545 }
1546
1547 if (mice_cnt && !mouse_hook)
1548 mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
1549 else if (!mice_cnt && mouse_hook)
1550 {
1551 UnhookWindowsHookEx( mouse_hook );
1552 mouse_hook = NULL;
1553 }
1554 }
1555 TranslateMessage(&msg);
1556 DispatchMessageW(&msg);
1557 }
1558
1559 return 0;
1560 }
1561
1562 static DWORD hook_thread_id;
1563
1564 static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
1565 {
1566 0, 0, &dinput_hook_crit,
1567 { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
1568 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
1569 };
1570 static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
1571
1572 static BOOL check_hook_thread(void)
1573 {
1574 static HANDLE hook_thread;
1575
1576 EnterCriticalSection(&dinput_hook_crit);
1577
1578 TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
1579 if (!list_empty(&direct_input_list) && !hook_thread)
1580 {
1581 HANDLE event;
1582
1583 event = CreateEventW(NULL, FALSE, FALSE, NULL);
1584 hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
1585 if (event && hook_thread)
1586 {
1587 HANDLE handles[2];
1588 handles[0] = event;
1589 handles[1] = hook_thread;
1590 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1591 }
1592 LeaveCriticalSection(&dinput_hook_crit);
1593 CloseHandle(event);
1594 }
1595 else if (list_empty(&direct_input_list) && hook_thread)
1596 {
1597 DWORD tid = hook_thread_id;
1598
1599 hook_thread_id = 0;
1600 PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
1601 LeaveCriticalSection(&dinput_hook_crit);
1602
1603 /* wait for hook thread to exit */
1604 WaitForSingleObject(hook_thread, INFINITE);
1605 CloseHandle(hook_thread);
1606 hook_thread = NULL;
1607 }
1608 else
1609 LeaveCriticalSection(&dinput_hook_crit);
1610
1611 return hook_thread_id != 0;
1612 }
1613
1614 void check_dinput_hooks(LPDIRECTINPUTDEVICE8W iface)
1615 {
1616 static HHOOK callwndproc_hook;
1617 static ULONG foreground_cnt;
1618 IDirectInputDeviceImpl *dev = impl_from_IDirectInputDevice8W(iface);
1619
1620 EnterCriticalSection(&dinput_hook_crit);
1621
1622 if (dev->dwCoopLevel & DISCL_FOREGROUND)
1623 {
1624 if (dev->acquired)
1625 foreground_cnt++;
1626 else
1627 foreground_cnt--;
1628 }
1629
1630 if (foreground_cnt && !callwndproc_hook)
1631 callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
1632 DINPUT_instance, GetCurrentThreadId() );
1633 else if (!foreground_cnt && callwndproc_hook)
1634 {
1635 UnhookWindowsHookEx( callwndproc_hook );
1636 callwndproc_hook = NULL;
1637 }
1638
1639 PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
1640
1641 LeaveCriticalSection(&dinput_hook_crit);
1642 }
1643
1644 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved)
1645 {
1646 switch(reason)
1647 {
1648 case DLL_PROCESS_ATTACH:
1649 DisableThreadLibraryCalls(inst);
1650 DINPUT_instance = inst;
1651 break;
1652 case DLL_PROCESS_DETACH:
1653 if (reserved) break;
1654 DeleteCriticalSection(&dinput_hook_crit);
1655 break;
1656 }
1657 return TRUE;
1658 }