Fix bug found by Piotr Caban, where our function tried to delete a
[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 IDirectInput7AVtbl ddi7avt;
48 static IDirectInput7WVtbl ddi7wvt;
49 static IDirectInput8AVtbl ddi8avt;
50 static IDirectInput8WVtbl 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         unsigned 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     IDirectInputImpl *This = (IDirectInputImpl *)iface;
240     DIDEVICEINSTANCEA devInstance;
241     int i, j, r;
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         for (j = 0, r = -1; r != 0; j++) {
250             devInstance.dwSize = sizeof(devInstance);
251             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
252             if ((r = dinput_devices[i]->enum_deviceA(dwDevType, dwFlags, &devInstance, This->version, j))) {
253                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
254                     return 0;
255             }
256         }
257     }
258     
259     return 0;
260 }
261 /******************************************************************************
262  *      IDirectInputW_EnumDevices
263  */
264 static HRESULT WINAPI IDirectInputWImpl_EnumDevices(
265         LPDIRECTINPUT7W iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKW lpCallback,
266         LPVOID pvRef, DWORD dwFlags) 
267 {
268     IDirectInputImpl *This = (IDirectInputImpl *)iface;
269     DIDEVICEINSTANCEW devInstance;
270     int i, j, r;
271     
272     TRACE("(this=%p,0x%04lx '%s',%p,%p,%04lx)\n",
273           This, dwDevType, _dump_DIDEVTYPE_value(dwDevType),
274           lpCallback, pvRef, dwFlags);
275     TRACE(" flags: "); _dump_EnumDevices_dwFlags(dwFlags); TRACE("\n");
276     
277     for (i = 0; i < nrof_dinput_devices; i++) {
278         for (j = 0, r = -1; r != 0; j++) {
279             devInstance.dwSize = sizeof(devInstance);
280             TRACE("  - checking device %d ('%s')\n", i, dinput_devices[i]->name);
281             if ((r = dinput_devices[i]->enum_deviceW(dwDevType, dwFlags, &devInstance, This->version, j))) {
282                 if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
283                     return 0;
284             }
285         }
286     }
287     
288     return 0;
289 }
290
291 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUT7A iface)
292 {
293         IDirectInputImpl *This = (IDirectInputImpl *)iface;
294         return InterlockedIncrement((&This->ref));
295 }
296
297 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUT7A iface)
298 {
299         IDirectInputImpl *This = (IDirectInputImpl *)iface;
300         ULONG ref;
301         ref = InterlockedDecrement(&(This->ref));
302         if (ref == 0)
303                 HeapFree(GetProcessHeap(),0,This);
304         return ref;
305 }
306
307 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(LPDIRECTINPUT7A iface, REFIID riid, LPVOID *ppobj) {
308         IDirectInputImpl *This = (IDirectInputImpl *)iface;
309
310         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
311         if (IsEqualGUID(&IID_IUnknown,riid) ||
312             IsEqualGUID(&IID_IDirectInputA,riid) ||
313             IsEqualGUID(&IID_IDirectInput2A,riid) ||
314             IsEqualGUID(&IID_IDirectInput7A,riid)) {
315                 IDirectInputAImpl_AddRef(iface);
316                 *ppobj = This;
317                 return 0;
318         }
319         TRACE("Unsupported interface !\n");
320         return E_FAIL;
321 }
322
323 static HRESULT WINAPI IDirectInputWImpl_QueryInterface(LPDIRECTINPUT7W iface, REFIID riid, LPVOID *ppobj) {
324         IDirectInputImpl *This = (IDirectInputImpl *)iface;
325
326         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
327         if (IsEqualGUID(&IID_IUnknown,riid) ||
328             IsEqualGUID(&IID_IDirectInputW,riid) ||
329             IsEqualGUID(&IID_IDirectInput2W,riid) ||
330             IsEqualGUID(&IID_IDirectInput7W,riid)) {
331                 IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
332                 *ppobj = This;
333                 return 0;
334         }
335         TRACE("Unsupported interface !\n");
336         return E_FAIL;
337 }
338
339 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(
340         LPDIRECTINPUT7A iface,REFGUID rguid,LPDIRECTINPUTDEVICEA* pdev,
341         LPUNKNOWN punk
342 ) {
343         IDirectInputImpl *This = (IDirectInputImpl *)iface;
344         HRESULT ret_value = DIERR_DEVICENOTREG;
345         int i;
346
347         TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
348
349         /* Loop on all the devices to see if anyone matches the given GUID */
350         for (i = 0; i < nrof_dinput_devices; i++) {
351           HRESULT ret;
352           if ((ret = dinput_devices[i]->create_deviceA(This, rguid, NULL, pdev)) == DI_OK)
353             return DI_OK;
354
355           if (ret == DIERR_NOINTERFACE)
356             ret_value = DIERR_NOINTERFACE;
357         }
358
359         return ret_value;
360 }
361
362 static HRESULT WINAPI IDirectInputWImpl_CreateDevice(LPDIRECTINPUT7A iface, 
363                                                      REFGUID rguid, LPDIRECTINPUTDEVICEW* pdev, LPUNKNOWN punk) {
364         IDirectInputImpl *This = (IDirectInputImpl *)iface;
365         HRESULT ret_value = DIERR_DEVICENOTREG;
366         int i;
367
368         TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
369
370         /* Loop on all the devices to see if anyone matches the given GUID */
371         for (i = 0; i < nrof_dinput_devices; i++) {
372           HRESULT ret;
373           if ((ret = dinput_devices[i]->create_deviceW(This, rguid, NULL, pdev)) == DI_OK)
374             return DI_OK;
375
376           if (ret == DIERR_NOINTERFACE)
377             ret_value = DIERR_NOINTERFACE;
378         }
379
380         return ret_value;
381 }
382
383 static HRESULT WINAPI IDirectInputAImpl_Initialize(LPDIRECTINPUT7A iface, HINSTANCE hinst, DWORD x) {
384         return DIERR_ALREADYINITIALIZED;
385 }
386
387 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUT7A iface,
388                                                         REFGUID rguid) {
389   IDirectInputImpl *This = (IDirectInputImpl *)iface;
390
391   FIXME("(%p)->(%s): stub\n",This,debugstr_guid(rguid));
392
393   return DI_OK;
394 }
395
396 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUT7A iface,
397                                                         HWND hwndOwner,
398                                                         DWORD dwFlags) {
399   IDirectInputImpl *This = (IDirectInputImpl *)iface;
400   FIXME("(%p)->(%08lx,%08lx): stub\n",This, (DWORD) hwndOwner, dwFlags);
401
402   return DI_OK;
403 }
404
405 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT7A iface, REFGUID rguid,
406                                                     LPCSTR pszName, LPGUID pguidInstance) {
407   IDirectInputImpl *This = (IDirectInputImpl *)iface;
408   FIXME("(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance);
409
410   return DI_OK;
411 }
412
413 static HRESULT WINAPI IDirectInput2WImpl_FindDevice(LPDIRECTINPUT7W iface, REFGUID rguid,
414                                                     LPCWSTR pszName, LPGUID pguidInstance) {
415   IDirectInputImpl *This = (IDirectInputImpl *)iface;
416   FIXME("(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), debugstr_w(pszName), pguidInstance);
417
418   return DI_OK;
419 }
420
421 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
422                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
423 {
424   IDirectInputImpl *This = (IDirectInputImpl *)iface;
425   HRESULT ret_value = DIERR_DEVICENOTREG;
426   int i;
427
428   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
429
430   /* Loop on all the devices to see if anyone matches the given GUID */
431   for (i = 0; i < nrof_dinput_devices; i++) {
432     HRESULT ret;
433     if ((ret = dinput_devices[i]->create_deviceA(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
434       return DI_OK;
435
436     if (ret == DIERR_NOINTERFACE)
437       ret_value = DIERR_NOINTERFACE;
438   }
439
440   return ret_value;
441 }
442
443 static HRESULT WINAPI IDirectInput7WImpl_CreateDeviceEx(LPDIRECTINPUT7W iface, REFGUID rguid,
444                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
445 {
446   IDirectInputImpl *This = (IDirectInputImpl *)iface;
447   HRESULT ret_value = DIERR_DEVICENOTREG;
448   int i;
449
450   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
451
452   /* Loop on all the devices to see if anyone matches the given GUID */
453   for (i = 0; i < nrof_dinput_devices; i++) {
454     HRESULT ret;
455     if ((ret = dinput_devices[i]->create_deviceW(This, rguid, riid, (LPDIRECTINPUTDEVICEW*) pvOut)) == DI_OK)
456       return DI_OK;
457
458     if (ret == DIERR_NOINTERFACE)
459       ret_value = DIERR_NOINTERFACE;
460   }
461
462   return ret_value;
463 }
464
465 static HRESULT WINAPI IDirectInput8AImpl_QueryInterface(LPDIRECTINPUT8A iface, REFIID riid, LPVOID *ppobj) {
466       IDirectInputImpl *This = (IDirectInputImpl *)iface;
467
468       TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
469       if (IsEqualGUID(&IID_IUnknown,riid) ||
470           IsEqualGUID(&IID_IDirectInput8A,riid)) {
471               IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
472               *ppobj = This;
473               return 0;
474       }
475       TRACE("Unsupported interface !\n");
476       return E_NOINTERFACE;
477 }
478
479 static HRESULT WINAPI IDirectInput8WImpl_QueryInterface(LPDIRECTINPUT8W iface, REFIID riid, LPVOID *ppobj) {
480       IDirectInputImpl *This = (IDirectInputImpl *)iface;
481
482       TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
483       if (IsEqualGUID(&IID_IUnknown,riid) ||
484           IsEqualGUID(&IID_IDirectInput8W,riid)) {
485               IDirectInputAImpl_AddRef((LPDIRECTINPUT7A) iface);
486               *ppobj = This;
487               return 0;
488       }
489       TRACE("Unsupported interface !\n");
490       return E_NOINTERFACE;
491 }
492
493 static HRESULT WINAPI IDirectInput8AImpl_EnumDevicesBySemantics(
494       LPDIRECTINPUT8A iface, LPCSTR ptszUserName, LPDIACTIONFORMATA lpdiActionFormat,
495       LPDIENUMDEVICESBYSEMANTICSCBA lpCallback,
496       LPVOID pvRef, DWORD dwFlags
497 )
498 {
499       IDirectInputImpl *This = (IDirectInputImpl *)iface;
500
501       FIXME("(this=%p,%s,%p,%p,%p,%04lx): stub\n", This, ptszUserName, lpdiActionFormat,
502             lpCallback, pvRef, dwFlags);
503       return 0;
504 }
505
506 static HRESULT WINAPI IDirectInput8WImpl_EnumDevicesBySemantics(
507       LPDIRECTINPUT8W iface, LPCWSTR ptszUserName, LPDIACTIONFORMATW lpdiActionFormat,
508       LPDIENUMDEVICESBYSEMANTICSCBW lpCallback,
509       LPVOID pvRef, DWORD dwFlags
510 )
511 {
512       IDirectInputImpl *This = (IDirectInputImpl *)iface;
513
514       FIXME("(this=%p,%s,%p,%p,%p,%04lx): stub\n", This, debugstr_w(ptszUserName), lpdiActionFormat,
515             lpCallback, pvRef, dwFlags);
516       return 0;
517 }
518
519 static HRESULT WINAPI IDirectInput8AImpl_ConfigureDevices(
520       LPDIRECTINPUT8A iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
521       LPDICONFIGUREDEVICESPARAMSA lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
522 )
523 {
524       IDirectInputImpl *This = (IDirectInputImpl *)iface;
525
526       FIXME("(this=%p,%p,%p,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
527             dwFlags, pvRefData);
528       return 0;
529 }
530
531 static HRESULT WINAPI IDirectInput8WImpl_ConfigureDevices(
532       LPDIRECTINPUT8W iface, LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
533       LPDICONFIGUREDEVICESPARAMSW lpdiCDParams, DWORD dwFlags, LPVOID pvRefData
534 )
535 {
536       IDirectInputImpl *This = (IDirectInputImpl *)iface;
537
538       FIXME("(this=%p,%p,%p,%04lx,%p): stub\n", This, lpdiCallback, lpdiCDParams,
539             dwFlags, pvRefData);
540       return 0;
541 }
542
543 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
544 # define XCAST(fun)   (typeof(ddi7avt.fun))
545 #else
546 # define XCAST(fun)     (void*)
547 #endif
548
549 static IDirectInput7AVtbl ddi7avt = {
550         XCAST(QueryInterface)IDirectInputAImpl_QueryInterface,
551         XCAST(AddRef)IDirectInputAImpl_AddRef,
552         XCAST(Release)IDirectInputAImpl_Release,
553         XCAST(CreateDevice)IDirectInputAImpl_CreateDevice,
554         XCAST(EnumDevices)IDirectInputAImpl_EnumDevices,
555         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
556         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
557         XCAST(Initialize)IDirectInputAImpl_Initialize,
558         XCAST(FindDevice)IDirectInput2AImpl_FindDevice,
559         XCAST(CreateDeviceEx)IDirectInput7AImpl_CreateDeviceEx
560 };
561
562 #undef XCAST
563 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
564 # define XCAST(fun)   (typeof(ddi7wvt.fun))
565 #else
566 # define XCAST(fun)     (void*)
567 #endif
568
569 static IDirectInput7WVtbl ddi7wvt = {
570         XCAST(QueryInterface)IDirectInputWImpl_QueryInterface,
571         XCAST(AddRef)IDirectInputAImpl_AddRef,
572         XCAST(Release)IDirectInputAImpl_Release,
573         XCAST(CreateDevice)IDirectInputWImpl_CreateDevice,
574         XCAST(EnumDevices)IDirectInputWImpl_EnumDevices,
575         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
576         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
577         XCAST(Initialize)IDirectInputAImpl_Initialize,
578         XCAST(FindDevice)IDirectInput2WImpl_FindDevice,
579         XCAST(CreateDeviceEx)IDirectInput7WImpl_CreateDeviceEx
580 };
581 #undef XCAST
582
583 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
584 # define XCAST(fun)     (typeof(ddi8avt.fun))
585 #else
586 # define XCAST(fun)     (void*)
587 #endif
588
589 static IDirectInput8AVtbl ddi8avt = {
590         XCAST(QueryInterface)IDirectInput8AImpl_QueryInterface,
591         XCAST(AddRef)IDirectInputAImpl_AddRef,
592         XCAST(Release)IDirectInputAImpl_Release,
593         XCAST(CreateDevice)IDirectInputAImpl_CreateDevice,
594         XCAST(EnumDevices)IDirectInputAImpl_EnumDevices,
595         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
596         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
597         XCAST(Initialize)IDirectInputAImpl_Initialize,
598         XCAST(FindDevice)IDirectInput2AImpl_FindDevice,
599         XCAST(EnumDevicesBySemantics)IDirectInput8AImpl_EnumDevicesBySemantics,
600         XCAST(ConfigureDevices)IDirectInput8AImpl_ConfigureDevices
601 };
602 #undef XCAST
603
604 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
605 # define XCAST(fun)     (typeof(ddi8wvt.fun))
606 #else
607 # define XCAST(fun)     (void*)
608 #endif
609 static IDirectInput8WVtbl ddi8wvt = {
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         IClassFactoryImpl *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         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
643         return InterlockedIncrement(&(This->ref));
644 }
645
646 static ULONG WINAPI DICF_Release(LPCLASSFACTORY iface) {
647         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
648         /* static class, won't be  freed */
649         return InterlockedDecrement(&(This->ref));
650 }
651
652 static HRESULT WINAPI DICF_CreateInstance(
653         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
654 ) {
655         IClassFactoryImpl *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         IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
676         FIXME("(%p)->(%d),stub!\n",This,dolock);
677         return S_OK;
678 }
679
680 static IClassFactoryVtbl DICF_Vtbl = {
681         DICF_QueryInterface,
682         DICF_AddRef,
683         DICF_Release,
684         DICF_CreateInstance,
685         DICF_LockServer
686 };
687 static IClassFactoryImpl DINPUT_CF = {&DICF_Vtbl, 1 };
688
689 /***********************************************************************
690  *              DllCanUnloadNow (DINPUT.@)
691  */
692 HRESULT WINAPI DINPUT_DllCanUnloadNow(void)
693 {
694     FIXME("(void): stub\n");
695
696     return S_FALSE;
697 }
698
699 /***********************************************************************
700  *              DllGetClassObject (DINPUT.@)
701  */
702 HRESULT WINAPI DINPUT_DllGetClassObject(REFCLSID rclsid, REFIID riid,
703                                         LPVOID *ppv)
704 {
705     TRACE("(%p,%p,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
706     if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
707         *ppv = (LPVOID)&DINPUT_CF;
708         IClassFactory_AddRef((IClassFactory*)*ppv);
709     return S_OK;
710     }
711
712     FIXME("(%p,%p,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
713     return CLASS_E_CLASSNOTAVAILABLE;
714 }