wined3d: Allow use of pixel shaders with drawStridedSlow.
[wine] / dlls / 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  *
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 /* Status:
23  *
24  * - Tomb Raider 2 Demo:
25  *   Playable using keyboard only.
26  * - WingCommander Prophecy Demo:
27  *   Doesn't get Input Focus.
28  *
29  * - Fallout : works great in X and DGA mode
30  */
31
32 #include "config.h"
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <string.h>
36
37 #define COBJMACROS
38
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winuser.h"
44 #include "winerror.h"
45 #include "dinput_private.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
48
49 static const IDirectInput7AVtbl ddi7avt;
50 static const IDirectInput7WVtbl ddi7wvt;
51 static const IDirectInput8AVtbl ddi8avt;
52 static const IDirectInput8WVtbl ddi8wvt;
53
54 static const struct dinput_device *dinput_devices[] =
55 {
56     &mouse_device,
57     &keyboard_device,
58     &joystick_linuxinput_device,
59     &joystick_linux_device
60 };
61 #define NB_DINPUT_DEVICES (sizeof(dinput_devices)/sizeof(dinput_devices[0]))
62
63 HINSTANCE DINPUT_instance = NULL;
64
65 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserv)
66 {
67     switch(reason)
68     {
69       case DLL_PROCESS_ATTACH:
70         DisableThreadLibraryCalls(inst);
71         DINPUT_instance = inst;
72         break;
73       case DLL_PROCESS_DETACH:
74         break;
75     }
76     return TRUE;
77 }
78
79
80 /******************************************************************************
81  *      DirectInputCreateEx (DINPUT.@)
82  */
83 HRESULT WINAPI DirectInputCreateEx(
84         HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
85         LPUNKNOWN punkOuter) 
86 {
87         IDirectInputImpl* This;
88
89         TRACE("(%p,%04lx,%s,%p,%p)\n", hinst,dwVersion,debugstr_guid(riid),ppDI,punkOuter);
90
91         if (IsEqualGUID(&IID_IDirectInputA,riid) ||
92             IsEqualGUID(&IID_IDirectInput2A,riid) ||
93             IsEqualGUID(&IID_IDirectInput7A,riid)) {
94           This = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputImpl));
95           This->lpVtbl = &ddi7avt;
96           This->ref = 1;
97           This->dwVersion = dwVersion; 
98           *ppDI = This;
99
100           return DI_OK;
101         }
102
103         if (IsEqualGUID(&IID_IDirectInputW,riid) ||
104             IsEqualGUID(&IID_IDirectInput2W,riid) ||
105             IsEqualGUID(&IID_IDirectInput7W,riid)) {
106           This = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputImpl));
107           This->lpVtbl = &ddi7wvt;
108           This->ref = 1;
109           This->dwVersion = dwVersion; 
110           *ppDI = This;
111
112           return DI_OK;
113         }
114
115         if (IsEqualGUID(&IID_IDirectInput8A,riid)) {
116           This = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputImpl));
117           This->lpVtbl = &ddi8avt;
118           This->ref = 1;
119           This->dwVersion = dwVersion; 
120           *ppDI = This;
121
122           return DI_OK;
123         }
124
125         if (IsEqualGUID(&IID_IDirectInput8W,riid)) {
126           This = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputImpl));
127           This->lpVtbl = &ddi8wvt;
128           This->ref = 1;
129           This->dwVersion = dwVersion; 
130           *ppDI = This;
131
132           return DI_OK;
133         }
134
135         return DIERR_OLDDIRECTINPUTVERSION;
136 }
137
138 /******************************************************************************
139  *      DirectInputCreateA (DINPUT.@)
140  */
141 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
142 {
143         IDirectInputImpl* This;
144         TRACE("(%p,%04lx,%p,%p)\n", hinst,dwVersion,ppDI,punkOuter);
145         This = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputImpl));
146         This->lpVtbl = &ddi7avt;
147         This->ref = 1;
148         This->dwVersion = dwVersion; 
149         *ppDI = (IDirectInputA*)This;
150         return 0;
151
152 }
153
154 /******************************************************************************
155  *      DirectInputCreateW (DINPUT.@)
156  */
157 HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW *ppDI, LPUNKNOWN punkOuter)
158 {
159         IDirectInputImpl* This;
160         TRACE("(%p,%04lx,%p,%p)\n", hinst,dwVersion,ppDI,punkOuter);
161         This = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputImpl));
162         This->lpVtbl = &ddi7wvt;
163         This->ref = 1;
164         This->dwVersion = dwVersion; 
165         *ppDI = (IDirectInputW*)This;
166         return 0;
167 }
168
169 static const char *_dump_DIDEVTYPE_value(DWORD dwDevType) {
170     switch (dwDevType) {
171         case 0: return "All devices";
172         case DIDEVTYPE_MOUSE: return "DIDEVTYPE_MOUSE";
173         case DIDEVTYPE_KEYBOARD: return "DIDEVTYPE_KEYBOARD";
174         case DIDEVTYPE_JOYSTICK: return "DIDEVTYPE_JOYSTICK";
175         case DIDEVTYPE_DEVICE: return "DIDEVTYPE_DEVICE";
176         default: return "Unknown";
177     }
178 }
179
180 static void _dump_EnumDevices_dwFlags(DWORD dwFlags) {
181     if (TRACE_ON(dinput)) {
182         unsigned int   i;
183         static const struct {
184             DWORD       mask;
185             const char  *name;
186         } flags[] = {
187 #define FE(x) { x, #x}
188             FE(DIEDFL_ALLDEVICES),
189             FE(DIEDFL_ATTACHEDONLY),
190             FE(DIEDFL_FORCEFEEDBACK),
191             FE(DIEDFL_INCLUDEALIASES),
192             FE(DIEDFL_INCLUDEPHANTOMS)
193 #undef FE
194         };
195         if (dwFlags == 0) {
196             DPRINTF("DIEDFL_ALLDEVICES");
197             return;
198         }
199         for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
200             if (flags[i].mask & dwFlags)
201                 DPRINTF("%s ",flags[i].name);
202     }
203 }
204
205 /******************************************************************************
206  *      IDirectInputA_EnumDevices
207  */
208 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
209         LPDIRECTINPUT7A iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
210         LPVOID pvRef, DWORD dwFlags)
211 {
212     IDirectInputImpl *This = (IDirectInputImpl *)iface;
213     DIDEVICEINSTANCEA devInstance;
214     int i, j, r;
215     
216     TRACE("(this=%p,0x%04lx '%s',%p,%p,%04lx)\n",
217           This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
218           lpCallback, pvRef, dwFlags);
219     TRACE(" flags: "); _dump_EnumDevices_dwFlags(dwFlags); TRACE("\n");
220
221     for (i = 0; i < NB_DINPUT_DEVICES; i++) {
222         if (!dinput_devices[i]->enum_deviceA) continue;
223         for (j = 0, r = -1; r != 0; j++) {
224             devInstance.dwSize = sizeof(devInstance);
225             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
226             if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
227                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
228                     return 0;
229             }
230         }
231     }
232     
233     return 0;
234 }
235 /******************************************************************************
236  *      IDirectInputW_EnumDevices
237  */
238 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
239         LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
240         LPVOID pvRef, DWORD dwFlags) 
241 {
242     IDirectInputImpl *This = (IDirectInputImpl *)iface;
243     DIDEVICEINSTANCEW devInstance;
244     int i, j, r;
245     
246     TRACE("(this=%p,0x%04lx '%s',%p,%p,%04lx)\n",
247           This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
248           lpCallback, pvRef, dwFlags);
249     TRACE(" flags: "); _dump_EnumDevices_dwFlags(dwFlags); TRACE("\n");
250
251     for (i = 0; i < NB_DINPUT_DEVICES; i++) {
252         if (!dinput_devices[i]->enum_deviceW) continue;
253         for (j = 0, r = -1; r != 0; j++) {
254             devInstance.dwSize = sizeof(devInstance);
255             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
256             if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->dwVersion, j))) {
257                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
258                     return 0;
259             }
260         }
261     }
262     
263     return 0;
264 }
265
266 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
267 {
268         IDirectInputImpl *This = (IDirectInputImpl *)iface;
269         return InterlockedIncrement((&This->ref));
270 }
271
272 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
273 {
274         IDirectInputImpl *This = (IDirectInputImpl *)iface;
275         ULONG ref;
276         ref = InterlockedDecrement(&(This->ref));
277         if (ref == 0)
278                 HeapFree(GetProcessHeap(),0,This);
279         return ref;
280 }
281
282 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj) {
283         IDirectInputImpl *This = (IDirectInputImpl *)iface;
284
285         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
286         if (IsEqualGUID(&IID_IUnknown,riid) ||
287             IsEqualGUID(&IID_IDirectInputA,riid) ||
288             IsEqualGUID(&IID_IDirectInput2A,riid) ||
289             IsEqualGUID(&IID_IDirectInput7A,riid)) {
290                 IDirectInputAImpl_AddRef(iface);
291                 *ppobj = This;
292                 return 0;
293         }
294         TRACE("Unsupported interface !\n");
295         return E_FAIL;
296 }
297
298 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj) {
299         IDirectInputImpl *This = (IDirectInputImpl *)iface;
300
301         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
302         if (IsEqualGUID(&IID_IUnknown,riid) ||
303             IsEqualGUID(&IID_IDirectInputW,riid) ||
304             IsEqualGUID(&IID_IDirectInput2W,riid) ||
305             IsEqualGUID(&IID_IDirectInput7W,riid)) {
306                 IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
307                 *ppobj = This;
308                 return 0;
309         }
310         TRACE("Unsupported interface !\n");
311         return E_FAIL;
312 }
313
314 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(
315         LPDIRECTINPUT7A iface,REFGUID rguid,LPDIRECTINPUTDEVICEA* pdev,
316         LPUNKNOWN punk
317 ) {
318         IDirectInputImpl *This = (IDirectInputImpl *)iface;
319         HRESULT ret_value = DIERR_DEVICENOTREG;
320         int i;
321
322         TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
323
324         if (pdev == NULL) {
325                 WARN("invalid pointer: pdev == NULL\n");
326                 return E_POINTER;
327         }
328
329         if (rguid == NULL) {
330                 WARN("invalid pointer: rguid == NULL\n");
331                 return E_POINTER;
332         }
333
334         /* Loop on all the devices to see if anyone matches the given GUID */
335         for (i = 0; i < NB_DINPUT_DEVICES; i++) {
336           HRESULT ret;
337           if (!dinput_devices[i]->create_deviceA) continue;
338           if ((ret = dinput_devices[i]->create_deviceA(This, rguid, NULL, pdev)) == DI_OK)
339             return DI_OK;
340
341           if (ret == DIERR_NOINTERFACE)
342             ret_value = DIERR_NOINTERFACE;
343         }
344
345         return ret_value;
346 }
347
348 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7A iface, 
349                                                      REFGUID rguid, LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk) {
350         IDirectInputImpl *This = (IDirectInputImpl *)iface;
351         HRESULT ret_value = DIERR_DEVICENOTREG;
352         int i;
353
354         TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
355
356         /* Loop on all the devices to see if anyone matches the given GUID */
357         for (i = 0; i < NB_DINPUT_DEVICES; i++) {
358           HRESULT ret;
359           if (!dinput_devices[i]->create_deviceW) continue;
360           if ((ret = dinput_devices[i]->create_deviceW(This, rguid, NULL, pdev)) == DI_OK)
361             return DI_OK;
362
363           if (ret == DIERR_NOINTERFACE)
364             ret_value = DIERR_NOINTERFACE;
365         }
366
367         return ret_value;
368 }
369
370 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
371         TRACE("(this=%p,%p,%lx)\n",iface, hinst, x);
372         
373         /* Initialize can return: DIERR_BETADIRECTINPUTVERSION, DIERR_OLDDIRECTINPUTVERSION and DI_OK.
374          * Since we already initialized the device, return DI_OK. In the past we returned DIERR_ALREADYINITIALIZED
375          * which broke applications like Tomb Raider Legend because it isn't a legal return value.
376          */
377         return DI_OK;
378 }
379
380 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface,
381                                                         REFGUID rguid) {
382   IDirectInputImpl *This = (IDirectInputImpl *)iface;
383
384   FIXME("(%p)->(%s): stub\n",This,debugstr_guid(rguid));
385
386   return DI_OK;
387 }
388
389 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
390                                                         HWND hwndOwner,
391                                                         DWORD dwFlags) {
392   IDirectInputImpl *This = (IDirectInputImpl *)iface;
393   FIXME("(%p)->(%p,%08lx): stub\n",This, hwndOwner, dwFlags);
394
395   return DI_OK;
396 }
397
398 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
399                                                     LPCSTR pszName, LPGUID pguidInstance) {
400   IDirectInputImpl *This = (IDirectInputImpl *)iface;
401   FIXME("(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance);
402
403   return DI_OK;
404 }
405
406 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
407                                                     LPCWSTR pszName, LPGUID pguidInstance) {
408   IDirectInputImpl *This = (IDirectInputImpl *)iface;
409   FIXME("(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance);
410
411   return DI_OK;
412 }
413
414 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
415                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
416 {
417   IDirectInputImpl *This = (IDirectInputImpl *)iface;
418   HRESULT ret_value = DIERR_DEVICENOTREG;
419   int i;
420
421   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
422
423   /* Loop on all the devices to see if anyone matches the given GUID */
424   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
425     HRESULT ret;
426     if (!dinput_devices[i]->create_deviceA) continue;
427     if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
428       return DI_OK;
429
430     if (ret == DIERR_NOINTERFACE)
431       ret_value = DIERR_NOINTERFACE;
432   }
433
434   return ret_value;
435 }
436
437 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
438                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
439 {
440   IDirectInputImpl *This = (IDirectInputImpl *)iface;
441   HRESULT ret_value = DIERR_DEVICENOTREG;
442   int i;
443
444   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
445
446   /* Loop on all the devices to see if anyone matches the given GUID */
447   for (i = 0; i < NB_DINPUT_DEVICES; i++) {
448     HRESULT ret;
449     if (!dinput_devices[i]->create_deviceW) continue;
450     if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
451       return DI_OK;
452
453     if (ret == DIERR_NOINTERFACE)
454       ret_value = DIERR_NOINTERFACE;
455   }
456
457   return ret_value;
458 }
459
460 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj) {
461       IDirectInputImpl *This = (IDirectInputImpl *)iface;
462
463       TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
464       if (IsEqualGUID(&IID_IUnknown,riid) ||
465           IsEqualGUID(&IID_IDirectInput8A,riid)) {
466               IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
467               *ppobj = This;
468               return 0;
469       }
470       TRACE("Unsupported interface !\n");
471       return E_NOINTERFACE;
472 }
473
474 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj) {
475       IDirectInputImpl *This = (IDirectInputImpl *)iface;
476
477       TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
478       if (IsEqualGUID(&IID_IUnknown,riid) ||
479           IsEqualGUID(&IID_IDirectInput8W,riid)) {
480               IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
481               *ppobj = This;
482               return 0;
483       }
484       TRACE("Unsupported interface !\n");
485       return E_NOINTERFACE;
486 }
487
488 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
489       LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
490       LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
491       LPVOID pvRef, DWORD dwFlags
492 )
493 {
494       IDirectInputImpl *This = (IDirectInputImpl *)iface;
495
496       FIXME("(this=%p,%s,%p,%p,%p,%04lx): stub\n", This, ptszUserName, lpdiActionFormat,
497             lpCallback, pvRef, dwFlags);
498       return 0;
499 }
500
501 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
502       LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
503       LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
504       LPVOID pvRef, DWORD dwFlags
505 )
506 {
507       IDirectInputImpl *This = (IDirectInputImpl *)iface;
508
509       FIXME("(this=%p,%s,%p,%p,%p,%04lx): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
510             lpCallback, pvRef, dwFlags);
511       return 0;
512 }
513
514 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
515       LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
516       LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
517 )
518 {
519       IDirectInputImpl *This = (IDirectInputImpl *)iface;
520
521       FIXME("(this=%p,%p,%p,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
522             dwFlags, pvRefData);
523       return 0;
524 }
525
526 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
527       LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
528       LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
529 )
530 {
531       IDirectInputImpl *This = (IDirectInputImpl *)iface;
532
533       FIXME("(this=%p,%p,%p,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
534             dwFlags, pvRefData);
535       return 0;
536 }
537
538 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
539 # define XCAST(fun)   (typeof(ddi7avt.fun))
540 #else
541 # define XCAST(fun)     (void*)
542 #endif
543
544 static const IDirectInput7AVtbl ddi7avt = {
545         XCAST(QueryInterface)IDirectInputAImpl_QueryInterface,
546         XCAST(AddRef)IDirectInputAImpl_AddRef,
547         XCAST(Release)IDirectInputAImpl_Release,
548         XCAST(CreateDevice)IDirectInputAImpl_CreateDevice,
549         XCAST(EnumDevices)IDirectInputAImpl_EnumDevices,
550         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
551         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
552         XCAST(Initialize)IDirectInputAImpl_Initialize,
553         XCAST(FindDevice)IDirectInput2AImpl_FindDevice,
554         XCAST(CreateDeviceEx)IDirectInput7AImpl_CreateDeviceEx
555 };
556
557 #undef XCAST
558 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
559 # define XCAST(fun)   (typeof(ddi7wvt.fun))
560 #else
561 # define XCAST(fun)     (void*)
562 #endif
563
564 static const IDirectInput7WVtbl ddi7wvt = {
565         XCAST(QueryInterface)IDirectInputWImpl_QueryInterface,
566         XCAST(AddRef)IDirectInputAImpl_AddRef,
567         XCAST(Release)IDirectInputAImpl_Release,
568         XCAST(CreateDevice)IDirectInputWImpl_CreateDevice,
569         XCAST(EnumDevices)IDirectInputWImpl_EnumDevices,
570         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
571         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
572         XCAST(Initialize)IDirectInputAImpl_Initialize,
573         XCAST(FindDevice)IDirectInput2WImpl_FindDevice,
574         XCAST(CreateDeviceEx)IDirectInput7WImpl_CreateDeviceEx
575 };
576 #undef XCAST
577
578 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
579 # define XCAST(fun)     (typeof(ddi8avt.fun))
580 #else
581 # define XCAST(fun)     (void*)
582 #endif
583
584 static const IDirectInput8AVtbl ddi8avt = {
585         XCAST(QueryInterface)IDirectInput8AImpl_QueryInterface,
586         XCAST(AddRef)IDirectInputAImpl_AddRef,
587         XCAST(Release)IDirectInputAImpl_Release,
588         XCAST(CreateDevice)IDirectInputAImpl_CreateDevice,
589         XCAST(EnumDevices)IDirectInputAImpl_EnumDevices,
590         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
591         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
592         XCAST(Initialize)IDirectInputAImpl_Initialize,
593         XCAST(FindDevice)IDirectInput2AImpl_FindDevice,
594         XCAST(EnumDevicesBySemantics)IDirectInput8AImpl_EnumDevicesBySemantics,
595         XCAST(ConfigureDevices)IDirectInput8AImpl_ConfigureDevices
596 };
597 #undef XCAST
598
599 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
600 # define XCAST(fun)     (typeof(ddi8wvt.fun))
601 #else
602 # define XCAST(fun)     (void*)
603 #endif
604 static const IDirectInput8WVtbl ddi8wvt = {
605         XCAST(QueryInterface)IDirectInput8WImpl_QueryInterface,
606         XCAST(AddRef)IDirectInputAImpl_AddRef,
607         XCAST(Release)IDirectInputAImpl_Release,
608         XCAST(CreateDevice)IDirectInputWImpl_CreateDevice,
609         XCAST(EnumDevices)IDirectInputWImpl_EnumDevices,
610         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
611         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
612         XCAST(Initialize)IDirectInputAImpl_Initialize,
613         XCAST(FindDevice)IDirectInput2WImpl_FindDevice,
614         XCAST(EnumDevicesBySemantics)IDirectInput8WImpl_EnumDevicesBySemantics,
615         XCAST(ConfigureDevices)IDirectInput8WImpl_ConfigureDevices
616 };
617 #undef XCAST
618
619 /*******************************************************************************
620  * DirectInput ClassFactory
621  */
622 typedef struct
623 {
624     /* IUnknown fields */
625     const IClassFactoryVtbl    *lpVtbl;
626     LONG                        ref;
627 } IClassFactoryImpl;
628
629 static HRESULT WINAPI DICF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
630         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
631
632         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
633         return E_NOINTERFACE;
634 }
635
636 static ULONG WINAPI DICF_AddRef(LPCLASSFACTORY iface) {
637         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
638         return InterlockedIncrement(&(This->ref));
639 }
640
641 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
642         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
643         /* static class, won't be  freed */
644         return InterlockedDecrement(&(This->ref));
645 }
646
647 static HRESULT WINAPI DICF_CreateInstance(
648         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
649 ) {
650         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
651
652         TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
653         if ( IsEqualGUID( &IID_IDirectInputA, riid ) ||
654              IsEqualGUID( &IID_IDirectInputW, riid ) ||
655              IsEqualGUID( &IID_IDirectInput2A, riid ) ||
656              IsEqualGUID( &IID_IDirectInput2W, riid ) ||
657              IsEqualGUID( &IID_IDirectInput7A, riid ) ||
658              IsEqualGUID( &IID_IDirectInput7W, riid ) ||
659              IsEqualGUID( &IID_IDirectInput8A, riid ) ||
660              IsEqualGUID( &IID_IDirectInput8W, riid ) ) {
661                 /* FIXME: reuse already created dinput if present? */
662                 return DirectInputCreateEx(0,0,riid,ppobj,pOuter);
663         }
664
665         FIXME("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);    
666         return E_NOINTERFACE;
667 }
668
669 static HRESULT WINAPI DICF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
670         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
671         FIXME("(%p)->(%d),stub!\n",This,dolock);
672         return S_OK;
673 }
674
675 static const IClassFactoryVtbl DICF_Vtbl = {
676         DICF_QueryInterface,
677         DICF_AddRef,
678         DICF_Release,
679         DICF_CreateInstance,
680         DICF_LockServer
681 };
682 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
683
684 /***********************************************************************
685  *              DllCanUnloadNow (DINPUT.@)
686  */
687 HRESULT WINAPI DllCanUnloadNow(void)
688 {
689     FIXME("(void): stub\n");
690
691     return S_FALSE;
692 }
693
694 /***********************************************************************
695  *              DllGetClassObject (DINPUT.@)
696  */
697 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
698 {
699     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
700     if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
701         *ppv = (LPVOID)&DINPUT_CF;
702         IClassFactory_AddRef((IClassFactory*)*ppv);
703     return S_OK;
704     }
705
706     FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
707     return CLASS_E_CLASSNOTAVAILABLE;
708 }