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