Removed some unnecessary includes.
[wine] / dlls / dinput / dinput_main.c
1 /*              DirectInput
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  *
6  */
7 /* Status:
8  *
9  * - Tomb Raider 2 Demo:
10  *   Playable using keyboard only.
11  * - WingCommander Prophecy Demo:
12  *   Doesn't get Input Focus.
13  * 
14  * - Fallout : works great in X and DGA mode
15  *
16  * FIXME: The keyboard handling needs to (and will) be merged into keyboard.c
17  *        (The current implementation is currently only a proof of concept and
18  *         an utter mess.)
19  */
20
21 #include "config.h"
22 #include <assert.h>
23
24 #include "debugtools.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "windef.h"
28
29 #include "initguid.h"
30 #include "dinput.h"
31 #include "dinput_private.h"
32
33 DEFAULT_DEBUG_CHANNEL(dinput);
34
35 static ICOM_VTABLE(IDirectInputA) ddiavt;
36 static ICOM_VTABLE(IDirectInput7A) ddi7avt;
37
38 /* This array will be filled a dinput.so loading */
39 #define MAX_WINE_DINPUT_DEVICES 3
40 static dinput_device * dinput_devices[MAX_WINE_DINPUT_DEVICES];
41 static int nrof_dinput_devices = 0;
42
43 /* register a direct draw driver. We better not use malloc for we are in 
44  * the ELF startup initialisation at this point.
45  */
46 void dinput_register_device(dinput_device *device) {
47     dinput_devices[nrof_dinput_devices++] = device;
48
49     /* increase MAX_DDRAW_DRIVERS if the line below triggers */
50     assert(nrof_dinput_devices <= MAX_WINE_DINPUT_DEVICES);
51 }
52
53 /******************************************************************************
54  *      DirectInputCreateEx
55  */
56 HRESULT WINAPI DirectInputCreateEx(
57         HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI,
58         LPUNKNOWN punkOuter
59 ) {
60         IDirectInputAImpl* This;
61
62         TRACE("(0x%08lx,%04lx,%s,%p,%p)\n",
63                 (DWORD)hinst,dwVersion,debugstr_guid(riid),ppDI,punkOuter
64         );
65         if (IsEqualGUID(&IID_IDirectInputA,riid)) {
66           This = (IDirectInputAImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputAImpl));
67           This->ref = 1;
68           ICOM_VTBL(This) = &ddiavt;
69           *ppDI = This;
70           
71           return DI_OK;
72         }
73         
74         if (IsEqualGUID(&IID_IDirectInput7A,riid)) {
75           This = (IDirectInputAImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputAImpl));
76           This->ref = 1;
77           ICOM_VTBL(This) = (ICOM_VTABLE(IDirectInputA) *) &ddi7avt;
78           *ppDI = This;
79           
80           return DI_OK;
81         }
82
83         return DIERR_OLDDIRECTINPUTVERSION;
84 }
85
86 /******************************************************************************
87  *      DirectInputCreateA
88  */
89 HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter)
90 {
91         IDirectInputAImpl* This;
92         TRACE("(0x%08lx,%04lx,%p,%p)\n",
93                 (DWORD)hinst,dwVersion,ppDI,punkOuter
94         );
95         This = (IDirectInputAImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInputAImpl));
96         This->ref = 1;
97         ICOM_VTBL(This) = &ddiavt;
98         *ppDI=(IDirectInputA*)This;
99         return 0;
100 }
101 /******************************************************************************
102  *      IDirectInputA_EnumDevices
103  */
104 static HRESULT WINAPI IDirectInputAImpl_EnumDevices(
105         LPDIRECTINPUTA iface, DWORD dwDevType, LPDIENUMDEVICESCALLBACKA lpCallback,
106         LPVOID pvRef, DWORD dwFlags
107 )
108 {
109         ICOM_THIS(IDirectInputAImpl,iface);
110         DIDEVICEINSTANCEA devInstance;
111         int i;
112
113         TRACE("(this=%p,0x%04lx,%p,%p,%04lx)\n", This, dwDevType, lpCallback, pvRef, dwFlags);
114
115         for (i = 0; i < nrof_dinput_devices; i++) {
116           if (dinput_devices[i]->enum_device(dwDevType, dwFlags, &devInstance)) {
117             if (lpCallback(&devInstance,pvRef) == DIENUM_STOP)
118               return 0;
119           }
120         }
121         
122         return 0;
123 }
124
125 static ULONG WINAPI IDirectInputAImpl_AddRef(LPDIRECTINPUTA iface)
126 {
127         ICOM_THIS(IDirectInputAImpl,iface);
128         return ++(This->ref);
129 }
130
131 static ULONG WINAPI IDirectInputAImpl_Release(LPDIRECTINPUTA iface)
132 {
133         ICOM_THIS(IDirectInputAImpl,iface);
134         if (!(--This->ref)) {
135                 HeapFree(GetProcessHeap(),0,This);
136                 return 0;
137         }
138         return This->ref;
139 }
140
141 static HRESULT WINAPI IDirectInputAImpl_CreateDevice(
142         LPDIRECTINPUTA iface,REFGUID rguid,LPDIRECTINPUTDEVICEA* pdev,
143         LPUNKNOWN punk
144 ) {
145         ICOM_THIS(IDirectInputAImpl,iface);
146         HRESULT ret_value = DIERR_DEVICENOTREG;
147         int i;
148         
149         TRACE("(this=%p,%s,%p,%p)\n",This,debugstr_guid(rguid),pdev,punk);
150
151         /* Loop on all the devices to see if anyone matches the given GUID */
152         for (i = 0; i < nrof_dinput_devices; i++) {
153           HRESULT ret;
154           if ((ret = dinput_devices[i]->create_device(This, rguid, NULL, pdev)) == DI_OK)
155             return DI_OK;
156
157           if (ret == DIERR_NOINTERFACE)
158             ret_value = DIERR_NOINTERFACE;
159         }
160
161         return ret_value;
162 }
163
164 static HRESULT WINAPI IDirectInputAImpl_QueryInterface(
165         LPDIRECTINPUTA iface,REFIID riid,LPVOID *ppobj
166 ) {
167         ICOM_THIS(IDirectInputAImpl,iface);
168
169         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
170         if (IsEqualGUID(&IID_IUnknown,riid)) {
171                 IDirectInputA_AddRef(iface);
172                 *ppobj = This;
173                 return 0;
174         }
175         if (IsEqualGUID(&IID_IDirectInputA,riid)) {
176                 IDirectInputA_AddRef(iface);
177                 *ppobj = This;
178                 return 0;
179         }
180         TRACE("Unsupported interface !\n");
181         return E_FAIL;
182 }
183
184 static HRESULT WINAPI IDirectInputAImpl_Initialize(
185         LPDIRECTINPUTA iface,HINSTANCE hinst,DWORD x
186 ) {
187         return DIERR_ALREADYINITIALIZED;
188 }
189
190 static HRESULT WINAPI IDirectInputAImpl_GetDeviceStatus(LPDIRECTINPUTA iface,
191                                                         REFGUID rguid) {
192   ICOM_THIS(IDirectInputAImpl,iface);
193   
194   FIXME("(%p)->(%s): stub\n",This,debugstr_guid(rguid));
195   
196   return DI_OK;
197 }
198
199 static HRESULT WINAPI IDirectInputAImpl_RunControlPanel(LPDIRECTINPUTA iface,
200                                                         HWND hwndOwner,
201                                                         DWORD dwFlags) {
202   ICOM_THIS(IDirectInputAImpl,iface);
203   FIXME("(%p)->(%08lx,%08lx): stub\n",This, (DWORD) hwndOwner, dwFlags);
204   
205   return DI_OK;
206 }
207
208 static HRESULT WINAPI IDirectInput2AImpl_FindDevice(LPDIRECTINPUT2A iface, REFGUID rguid,
209                                                     LPCSTR pszName, LPGUID pguidInstance) {
210   ICOM_THIS(IDirectInputAImpl,iface);
211   FIXME("(%p)->(%s, %s, %p): stub\n", This, debugstr_guid(rguid), pszName, pguidInstance);
212   
213   return DI_OK;
214 }
215
216 static HRESULT WINAPI IDirectInput7AImpl_CreateDeviceEx(LPDIRECTINPUT7A iface, REFGUID rguid,
217                                                         REFIID riid, LPVOID* pvOut, LPUNKNOWN lpUnknownOuter)
218 {
219   ICOM_THIS(IDirectInputAImpl,iface);
220   HRESULT ret_value = DIERR_DEVICENOTREG;
221   int i;
222         
223   TRACE("(%p)->(%s, %s, %p, %p)\n", This, debugstr_guid(rguid), debugstr_guid(riid), pvOut, lpUnknownOuter);
224
225   /* Loop on all the devices to see if anyone matches the given GUID */
226   for (i = 0; i < nrof_dinput_devices; i++) {
227     HRESULT ret;
228     if ((ret = dinput_devices[i]->create_device(This, rguid, riid, (LPDIRECTINPUTDEVICEA*) pvOut)) == DI_OK)
229       return DI_OK;
230     
231     if (ret == DIERR_NOINTERFACE)
232       ret_value = DIERR_NOINTERFACE;
233   }
234   
235   return ret_value;
236 }
237
238 static ICOM_VTABLE(IDirectInputA) ddiavt = 
239 {
240         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
241         IDirectInputAImpl_QueryInterface,
242         IDirectInputAImpl_AddRef,
243         IDirectInputAImpl_Release,
244         IDirectInputAImpl_CreateDevice,
245         IDirectInputAImpl_EnumDevices,
246         IDirectInputAImpl_GetDeviceStatus,
247         IDirectInputAImpl_RunControlPanel,
248         IDirectInputAImpl_Initialize
249 };
250
251 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
252 # define XCAST(fun)     (typeof(ddi7avt.fn##fun))
253 #else
254 # define XCAST(fun)     (void*)
255 #endif
256
257 static ICOM_VTABLE(IDirectInput7A) ddi7avt = {
258         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
259         XCAST(QueryInterface)IDirectInputAImpl_QueryInterface,
260         XCAST(AddRef)IDirectInputAImpl_AddRef,
261         XCAST(Release)IDirectInputAImpl_Release,
262         XCAST(CreateDevice)IDirectInputAImpl_CreateDevice,
263         XCAST(EnumDevices)IDirectInputAImpl_EnumDevices,
264         XCAST(GetDeviceStatus)IDirectInputAImpl_GetDeviceStatus,
265         XCAST(RunControlPanel)IDirectInputAImpl_RunControlPanel,
266         XCAST(Initialize)IDirectInputAImpl_Initialize,
267         XCAST(FindDevice)IDirectInput2AImpl_FindDevice,
268         IDirectInput7AImpl_CreateDeviceEx
269 };
270 #undef XCAST
271
272 /***********************************************************************
273  *              DllCanUnloadNow (DINPUT.@)
274  */
275 HRESULT WINAPI DINPUT_DllCanUnloadNow(void)
276 {
277     FIXME("(void): stub\n");
278
279     return S_FALSE;
280 }
281
282 /***********************************************************************
283  *              DllGetClassObject (DINPUT.@)
284  */
285 HRESULT WINAPI DINPUT_DllGetClassObject(REFCLSID rclsid, REFIID riid,
286                                         LPVOID *ppv)
287 {
288     FIXME("(%p, %p, %p): stub\n", debugstr_guid(rclsid), 
289           debugstr_guid(riid), ppv);
290
291     return CLASS_E_CLASSNOTAVAILABLE;
292 }
293
294 /***********************************************************************
295  *              DllRegisterServer (DINPUT.@)
296  */
297 HRESULT WINAPI DINPUT_DllRegisterServer(void)
298 {
299     FIXME("(void): stub\n");
300
301     return S_OK;
302 }
303
304 /***********************************************************************
305  *              DllUnregisterServer (DINPUT.@)
306  */
307 HRESULT WINAPI DINPUT_DllUnregisterServer(void)
308 {
309     FIXME("(void): stub\n");
310
311     return S_OK;
312 }