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