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