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