Release 980315
[wine/multimedia.git] / windows / dinput.c
blob101435f7c6c6318b3cff7e8fb035b0de7e876aac
1 /* DirectInput
3 * Copyright 1998 Marcus Meissner
4 */
5 /* Status:
7 * - Tomb Raider 2 Demo:
8 * Playable using keyboard only.
9 * - WingCommander Prophecy Demo:
10 * Doesn't get Input Focus.
12 * FIXME: The keyboard handling needs to (and will) be merged into keyboard.c
13 * (The current implementation is currently only a proof of concept and
14 * an utter mess.)
17 #include "config.h"
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <assert.h>
22 #include <X11/Xlib.h>
23 #include <sys/signal.h>
25 #include "windows.h"
26 #include "winerror.h"
27 #include "shell.h"
28 #include "ole.h"
29 #include "compobj.h"
30 #include "interfaces.h"
31 #include "gdi.h"
32 #include "heap.h"
33 #include "win.h"
34 #include "dinput.h"
35 #include "debug.h"
37 extern BYTE InputKeyStateTable[256];
38 extern BYTE vkey2scode[512];
40 static IDirectInputA_VTable ddiavt;
41 static IDirectInputDeviceA_VTable SysKeyboardAvt;
42 static IDirectInputDeviceA_VTable SysMouseAvt;
44 HRESULT WINAPI DirectInputCreate32A(HINSTANCE32 hinst, DWORD dwVersion, LPDIRECTINPUT32A *ppDI, LPUNKNOWN punkOuter) {
45 fprintf(stderr,"DirectInputCreate32A(0x%08lx,%04lx,%p,%p)\n",
46 (DWORD)hinst,dwVersion,ppDI,punkOuter
48 (*ppDI) = (LPDIRECTINPUT32A)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInput32A));
49 (*ppDI)->ref = 1;
50 (*ppDI)->lpvtbl = &ddiavt;
51 return 0;
53 /******************************************************************************
54 * IDirectInputA
56 static HRESULT WINAPI IDirectInputA_EnumDevices(
57 LPDIRECTINPUT32A this,DWORD dwFlags,LPDIENUMDEVICESCALLBACK32A cb,
58 LPVOID context,DWORD x
59 ) {
60 fprintf(stderr,"IDirectInputA(%p)->EnumDevices(0x%08lx,%p,%p,0x%08lx),stub!\n",this,dwFlags,cb,context,x);
61 return 0;
64 static ULONG WINAPI IDirectInputA_AddRef(LPDIRECTINPUT32A this) {
65 return ++(this->ref);
68 static ULONG WINAPI IDirectInputA_Release(LPDIRECTINPUT32A this) {
69 if (!(--this->ref)) {
70 HeapFree(GetProcessHeap(),0,this);
71 return 0;
73 return this->ref;
76 static HRESULT WINAPI IDirectInputA_CreateDevice(
77 LPDIRECTINPUT32A this,REFGUID rguid,LPDIRECTINPUTDEVICE32A* pdev,
78 LPUNKNOWN punk
79 ) {
80 char xbuf[50];
82 WINE_StringFromCLSID(rguid,xbuf);
83 fprintf(stderr,"IDirectInputA(%p)->CreateDevice(%s,%p,%p),stub!\n",this,xbuf,pdev,punk);
84 if (!memcmp(&GUID_SysKeyboard,rguid,sizeof(GUID_SysKeyboard))) {
85 *pdev = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboard32A));
86 (*pdev)->ref = 1;
87 (*pdev)->lpvtbl = &SysKeyboardAvt;
88 memcpy(&((*pdev)->guid),rguid,sizeof(*rguid));
89 memset(((LPSYSKEYBOARD32A)(*pdev))->keystate,0,256);
90 return 0;
92 if (!memcmp(&GUID_SysMouse,rguid,sizeof(GUID_SysMouse))) {
93 *pdev = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectInputDevice32A));
94 (*pdev)->ref = 1;
95 (*pdev)->lpvtbl = &SysMouseAvt;
96 memcpy(&((*pdev)->guid),rguid,sizeof(*rguid));
97 return 0;
99 return E_FAIL;
102 static HRESULT WINAPI IDirectInputA_QueryInterface(
103 LPDIRECTINPUT32A this,REFIID riid,LPVOID *ppobj
105 char xbuf[50];
107 WINE_StringFromCLSID(riid,xbuf);
108 fprintf(stderr,"IDirectInputA(%p)->QueryInterface(%s,%p)\n",this,xbuf,ppobj);
109 if (!memcmp(&IID_IUnknown,riid,sizeof(*riid))) {
110 this->lpvtbl->fnAddRef(this);
111 *ppobj = this;
112 return 0;
114 if (!memcmp(&IID_IDirectInputA,riid,sizeof(*riid))) {
115 this->lpvtbl->fnAddRef(this);
116 *ppobj = this;
117 return 0;
119 return E_FAIL;
122 static HRESULT WINAPI IDirectInputA_Initialize(
123 LPDIRECTINPUT32A this,HINSTANCE32 hinst,DWORD x
125 return DIERR_ALREADYINITIALIZED;
128 static IDirectInputA_VTable ddiavt= {
129 IDirectInputA_QueryInterface,
130 IDirectInputA_AddRef,
131 IDirectInputA_Release,
132 IDirectInputA_CreateDevice,
133 IDirectInputA_EnumDevices,
134 (void*)6,
135 (void*)7,
136 IDirectInputA_Initialize
139 /******************************************************************************
140 * IDirectInputDeviceA
142 static HRESULT WINAPI IDirectInputDeviceA_SetDataFormat(
143 LPDIRECTINPUTDEVICE32A this,LPCDIDATAFORMAT df
146 int i;
147 fprintf(stderr,"IDirectInputDeviceA(%p)->SetDataFormat(%p)\n",this,df);
149 fprintf(stderr,"df.dwSize %ld\n",df->dwSize);
150 fprintf(stderr,"df.dwObjsize %ld\n",df->dwObjSize);
151 fprintf(stderr,"df.dwFlags 0x%08lx\n",df->dwFlags);
152 fprintf(stderr,"df.dwDataSize %ld\n",df->dwDataSize);
153 fprintf(stderr,"df.dwNumObjs %ld\n",df->dwNumObjs);
155 for (i=0;i<df->dwNumObjs;i++) {
156 char xbuf[50];
158 if (df->rgodf[i].pguid)
159 WINE_StringFromCLSID(df->rgodf[i].pguid,xbuf);
160 else
161 strcpy(xbuf,"<no guid>");
162 fprintf(stderr,"df.rgodf[%d].guid %s\n",i,xbuf);
163 fprintf(stderr,"df.rgodf[%d].dwOfs %ld\n",i,df->rgodf[i].dwOfs);
164 fprintf(stderr," dwType 0x%02lx,dwInstance %ld\n",DIDFT_GETTYPE(df->rgodf[i].dwType),DIDFT_GETINSTANCE(df->rgodf[i].dwType));
165 fprintf(stderr,"df.rgodf[%d].dwFlags 0x%08lx\n",i,df->rgodf[i].dwFlags);
168 return 0;
171 static HRESULT WINAPI IDirectInputDeviceA_SetCooperativeLevel(
172 LPDIRECTINPUTDEVICE32A this,HWND32 hwnd,DWORD dwflags
174 fprintf(stderr,"IDirectInputDeviceA(%p)->SetCooperativeLevel(0x%08lx,0x%08lx)\n",this,(DWORD)hwnd,dwflags);
175 return 0;
178 static HRESULT WINAPI IDirectInputDeviceA_SetProperty(
179 LPDIRECTINPUTDEVICE32A this,REFGUID rguid,LPCDIPROPHEADER ph
181 char xbuf[50];
183 if (HIWORD(rguid))
184 WINE_StringFromCLSID(rguid,xbuf);
185 else
186 sprintf(xbuf,"<special guid %ld>",(DWORD)rguid);
187 fprintf(stderr,"IDirectInputDeviceA(%p)->SetProperty(%s,%p)\n",this,xbuf,ph);
188 if (!HIWORD(rguid)) {
189 switch ((DWORD)rguid) {
190 case DIPROP_BUFFERSIZE: {
191 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
193 fprintf(stderr," buffersize = %ld\n",pd->dwData);
194 break;
196 default:
197 fprintf(stderr," unknown type %ld\n",(DWORD)rguid);
198 break;
201 return 0;
204 static HRESULT WINAPI IDirectInputDeviceA_SetEventNotification(
205 LPDIRECTINPUTDEVICE32A this,HANDLE32 hnd
207 fprintf(stderr,"IDirectInputDeviceA(%p)->SetEventNotification(0x%08lx)\n",this,(DWORD)hnd);
208 return 0;
211 static HRESULT WINAPI IDirectInputDeviceA_GetDeviceData(
212 LPDIRECTINPUTDEVICE32A this,DWORD dodsize,LPDIDEVICEOBJECTDATA dod,
213 LPDWORD entries,DWORD flags
215 /* fprintf(stderr,"IDirectInputDeviceA(%p)->GetDeviceData(%ld,%p,%p(0x%08lx),0x%08lx)\n",this,dodsize,dod,entries,*entries,flags);*/
216 return 0;
220 static HRESULT WINAPI IDirectInputDeviceA_Acquire(LPDIRECTINPUTDEVICE32A this) {
221 fprintf(stderr,"IDirectInputDeviceA(%p)->Aquire()\n",this);
222 return 0;
225 static HRESULT WINAPI IDirectInputDeviceA_Unacquire(LPDIRECTINPUTDEVICE32A this) {
226 fprintf(stderr,"IDirectInputDeviceA(%p)->Unaquire()\n",this);
227 return 0;
230 static ULONG WINAPI IDirectInputDeviceA_Release(LPDIRECTINPUTDEVICE32A this) {
231 this->ref--;
232 if (this->ref)
233 return this->ref;
234 HeapFree(GetProcessHeap(),0,this);
235 return 0;
238 static HRESULT WINAPI SysKeyboardA_SetProperty(
239 LPDIRECTINPUTDEVICE32A this,REFGUID rguid,LPCDIPROPHEADER ph
241 LPSYSKEYBOARD32A kthis = (LPSYSKEYBOARD32A)this;
242 char xbuf[50];
244 if (HIWORD(rguid))
245 WINE_StringFromCLSID(rguid,xbuf);
246 else
247 sprintf(xbuf,"<special guid %ld>",(DWORD)rguid);
248 fprintf(stderr,"SysKeyboardA(%p)->SetProperty(%s,%p)\n",this,xbuf,ph);
249 fprintf(stderr," size is %ld, headersize is %ld,obj is %ld,how is %ld\n",ph->dwSize,ph->dwHeaderSize,ph->dwObj,ph->dwHow);
250 if (!HIWORD(rguid)) {
251 switch ((DWORD)rguid) {
252 case DIPROP_BUFFERSIZE: {
253 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
255 fprintf(stderr," buffersize = %ld\n",pd->dwData);
256 break;
258 default:
259 fprintf(stderr," unknown type %ld\n",(DWORD)rguid);
260 break;
263 return 0;
266 static HRESULT WINAPI SysKeyboardA_GetDeviceState(
267 LPDIRECTINPUTDEVICE32A this,DWORD len,LPVOID ptr
269 if (len==256) {
270 int i;
272 memset(ptr,0,256);
273 for (i=0;i<256;i++)
274 if (InputKeyStateTable[i]&0x80) {
275 ((LPBYTE)ptr)[vkey2scode[i] ]=0x80;
276 ((LPBYTE)ptr)[vkey2scode[i]|0x80]=0x80;
278 return 0;
280 fprintf(stderr,"whoops, SysKeyboardA_GetDeviceState got len %ld?\n",len);
281 return 0;
284 static HRESULT WINAPI SysKeyboardA_GetDeviceData(
285 LPDIRECTINPUTDEVICE32A this,DWORD dodsize,LPDIDEVICEOBJECTDATA dod,
286 LPDWORD entries,DWORD flags
288 int i,n,xentries;
289 LPSYSKEYBOARD32A kthis = (LPSYSKEYBOARD32A)this;
291 /* fprintf(stderr,"SysKeyboardA(%p)->GetDeviceData(%ld,%p,%p(%ld),0x%08lx)\n",this,dodsize,dod,entries,entries?*entries:0,flags);*/
292 if (entries)
293 xentries = *entries;
294 else
295 xentries = 1;
297 for (n=i=0;(i<256) && (n<*entries);i++) {
298 if (kthis->keystate[i] == (InputKeyStateTable[i]&0x80))
299 continue;
300 if (dod) {
301 /* add an entry */
302 dod[n].dwOfs = vkey2scode[i];
303 dod[n].dwData = InputKeyStateTable[i]&0x80;
304 dod[n].dwTimeStamp = 0; /* umm */
305 dod[n].dwSequence = 0; /* umm */
306 n++;
308 if (!(flags & DIGDD_PEEK))
309 kthis->keystate[i] = InputKeyStateTable[i]&0x80;
311 *entries = n;
312 return 0;
315 static HRESULT WINAPI SysKeyboardA_Acquire(LPDIRECTINPUTDEVICE32A this) {
316 fprintf(stderr,"SysKeyboardA(%p)->Aquire()\n",this);
317 return 0;
320 static HRESULT WINAPI SysKeyboardA_Unacquire(LPDIRECTINPUTDEVICE32A this) {
321 fprintf(stderr,"SysKeyboardA(%p)->Unaquire()\n",this);
322 return 0;
326 static HRESULT WINAPI IDirectInputDeviceA_GetDeviceState(
327 LPDIRECTINPUTDEVICE32A this,DWORD len,LPVOID ptr
329 fprintf(stderr,"IDirectInputDeviceA(%p)->GetDeviceState(0x%08lx,%p)\n",
330 this,len,ptr
332 return 0;
335 static HRESULT WINAPI IDirectInputDeviceA_QueryInterface(
336 LPDIRECTINPUTDEVICE32A this,REFIID riid,LPVOID *ppobj
338 char xbuf[50];
340 WINE_StringFromCLSID(riid,xbuf);
341 fprintf(stderr,"IDirectInputA(%p)->QueryInterface(%s,%p)\n",this,xbuf,ppobj);
342 if (!memcmp(&IID_IUnknown,riid,sizeof(*riid))) {
343 this->lpvtbl->fnAddRef(this);
344 *ppobj = this;
345 return 0;
347 if (!memcmp(&IID_IDirectInputDeviceA,riid,sizeof(*riid))) {
348 this->lpvtbl->fnAddRef(this);
349 *ppobj = this;
350 return 0;
352 return E_FAIL;
355 static IDirectInputDeviceA_VTable SysKeyboardAvt={
356 IDirectInputDeviceA_QueryInterface,
357 (void*)2,
358 IDirectInputDeviceA_Release,
359 (void*)4,
360 (void*)5,
361 (void*)6,
362 SysKeyboardA_SetProperty,
363 SysKeyboardA_Acquire,
364 SysKeyboardA_Unacquire,
365 SysKeyboardA_GetDeviceState,
366 SysKeyboardA_GetDeviceData,
367 IDirectInputDeviceA_SetDataFormat,
368 IDirectInputDeviceA_SetEventNotification,
369 IDirectInputDeviceA_SetCooperativeLevel,
370 (void*)15,
371 (void*)16,
372 (void*)17,
373 (void*)18,
376 static IDirectInputDeviceA_VTable SysMouseAvt={
377 IDirectInputDeviceA_QueryInterface,
378 (void*)2,
379 IDirectInputDeviceA_Release,
380 (void*)4,
381 (void*)5,
382 (void*)6,
383 IDirectInputDeviceA_SetProperty,
384 IDirectInputDeviceA_Acquire,
385 IDirectInputDeviceA_Unacquire,
386 IDirectInputDeviceA_GetDeviceState,
387 IDirectInputDeviceA_GetDeviceData,
388 IDirectInputDeviceA_SetDataFormat,
389 IDirectInputDeviceA_SetEventNotification,
390 IDirectInputDeviceA_SetCooperativeLevel,
391 (void*)15,
392 (void*)16,
393 (void*)17,
394 (void*)18,