Release 980201
[wine] / windows / dinput.c
1 /*              DirectInput
2  *
3  * Copyright 1998 Marcus Meissner
4  */
5 /* Status:
6  *
7  * - Tomb Raider 2 Demo:
8  *   Doesn't accept input yet. Although I am sure I've done everything right...
9  * - WingCommander Prophecy Demo:
10  *   dito.
11  */
12
13 #include "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
18 #include <X11/Xlib.h>
19 #include <sys/signal.h>
20
21 #include "windows.h"
22 #include "winerror.h"
23 #include "shell.h"
24 #include "ole.h"
25 #include "compobj.h"
26 #include "interfaces.h"
27 #include "gdi.h"
28 #include "heap.h"
29 #include "win.h"
30 #include "dinput.h"
31 #include "stddebug.h"
32 #include "debug.h"
33
34 static IDirectInputA_VTable ddiavt;
35 static IDirectInputDeviceA_VTable SysKeyboardAvt;
36 static IDirectInputDeviceA_VTable SysMouseAvt;
37
38 HRESULT WINAPI DirectInputCreate32A(HINSTANCE32 hinst, DWORD dwVersion, LPDIRECTINPUT32A *ppDI, LPUNKNOWN punkOuter) {
39         fprintf(stderr,"DirectInputCreate32A(0x%08lx,%04lx,%p,%p)\n",
40                 (DWORD)hinst,dwVersion,ppDI,punkOuter
41         );
42         (*ppDI) = (LPDIRECTINPUT32A)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectInput32A));
43         (*ppDI)->ref = 1;
44         (*ppDI)->lpvtbl = &ddiavt;
45         return 0;
46 }
47 /******************************************************************************
48  *              IDirectInputA
49  */
50 static HRESULT WINAPI IDirectInputA_EnumDevices(
51         LPDIRECTINPUT32A this,DWORD dwFlags,LPDIENUMDEVICESCALLBACK32A cb,
52         LPVOID context,DWORD x
53 ) {
54         fprintf(stderr,"IDirectInputA(%p)->EnumDevices(0x%08lx,%p,%p,0x%08lx),stub!\n",this,dwFlags,cb,context,x);
55         return 0;
56 }
57
58 static ULONG WINAPI IDirectInputA_AddRef(LPDIRECTINPUT32A this) {
59         return ++(this->ref);
60 }
61
62 static ULONG WINAPI IDirectInputA_Release(LPDIRECTINPUT32A this) {
63         if (!(--this->ref)) {
64                 HeapFree(GetProcessHeap(),0,this);
65                 return 0;
66         }
67         return this->ref;
68 }
69
70 static HRESULT WINAPI IDirectInputA_CreateDevice(
71         LPDIRECTINPUT32A this,REFGUID rguid,LPDIRECTINPUTDEVICE32A* pdev,
72         LPUNKNOWN punk
73 ) {
74         char    xbuf[50];
75         
76         StringFromCLSID(rguid,xbuf);
77         fprintf(stderr,"IDirectInputA(%p)->CreateDevice(%s,%p,%p),stub!\n",this,xbuf,pdev,punk);
78         if (!memcmp(&GUID_SysKeyboard,rguid,sizeof(GUID_SysKeyboard))) {
79                 *pdev = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectInputDevice32A));
80                 (*pdev)->ref = 1;
81                 (*pdev)->lpvtbl = &SysKeyboardAvt;
82                 memcpy(&((*pdev)->guid),rguid,sizeof(*rguid));
83                 return 0;
84         }
85         if (!memcmp(&GUID_SysMouse,rguid,sizeof(GUID_SysMouse))) {
86                 *pdev = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectInputDevice32A));
87                 (*pdev)->ref = 1;
88                 (*pdev)->lpvtbl = &SysMouseAvt;
89                 memcpy(&((*pdev)->guid),rguid,sizeof(*rguid));
90                 return 0;
91         }
92         return E_FAIL;
93 }
94
95 static HRESULT WINAPI IDirectInputA_QueryInterface(
96         LPDIRECTINPUT32A this,REFIID riid,LPVOID *ppobj
97 ) {
98         char    xbuf[50];
99
100         StringFromCLSID(riid,xbuf);
101         fprintf(stderr,"IDirectInputA(%p)->QueryInterface(%s,%p)\n",this,xbuf,ppobj);
102         if (!memcmp(&IID_IUnknown,riid,sizeof(*riid))) {
103                 this->lpvtbl->fnAddRef(this);
104                 *ppobj = this;
105                 return 0;
106         }
107         if (!memcmp(&IID_IDirectInputA,riid,sizeof(*riid))) {
108                 this->lpvtbl->fnAddRef(this);
109                 *ppobj = this;
110                 return 0;
111         }
112         return E_FAIL;
113 }
114
115 static IDirectInputA_VTable ddiavt= {
116         IDirectInputA_QueryInterface,
117         IDirectInputA_AddRef,
118         IDirectInputA_Release,
119         IDirectInputA_CreateDevice,
120         IDirectInputA_EnumDevices,
121         (void*)6,
122         (void*)7,
123         (void*)8
124 };
125
126 /******************************************************************************
127  *      IDirectInputDeviceA
128  */
129 static HRESULT WINAPI IDirectInputDeviceA_SetDataFormat(
130         LPDIRECTINPUTDEVICE32A this,LPCDIDATAFORMAT df
131 ) {
132         /*
133         int i;
134         fprintf(stderr,"IDirectInputDeviceA(%p)->SetDataFormat(%p)\n",this,df);
135
136         fprintf(stderr,"df.dwSize %ld\n",df->dwSize);
137         fprintf(stderr,"df.dwObjsize %ld\n",df->dwObjSize);
138         fprintf(stderr,"df.dwFlags 0x%08lx\n",df->dwFlags);
139         fprintf(stderr,"df.dwDataSize %ld\n",df->dwDataSize);
140         fprintf(stderr,"df.dwNumObjs %ld\n",df->dwNumObjs);
141
142         for (i=0;i<df->dwNumObjs;i++) {
143                 char    xbuf[50];
144
145                 if (df->rgodf[i].pguid)
146                         StringFromCLSID(df->rgodf[i].pguid,xbuf);
147                 else
148                         strcpy(xbuf,"<no guid>");
149                 fprintf(stderr,"df.rgodf[%d].guid %s\n",i,xbuf);
150                 fprintf(stderr,"df.rgodf[%d].dwOfs %ld\n",i,df->rgodf[i].dwOfs);
151                 fprintf(stderr,"        dwType 0x%02lx,dwInstance %ld\n",DIDFT_GETTYPE(df->rgodf[i].dwType),DIDFT_GETINSTANCE(df->rgodf[i].dwType));
152                 fprintf(stderr,"df.rgodf[%d].dwFlags 0x%08lx\n",i,df->rgodf[i].dwFlags);
153         }
154         */
155         return 0;
156 }
157
158 static HRESULT WINAPI IDirectInputDeviceA_SetCooperativeLevel(
159         LPDIRECTINPUTDEVICE32A this,HWND32 hwnd,DWORD dwflags
160 ) {
161         fprintf(stderr,"IDirectInputDeviceA(%p)->SetCooperativeLevel(0x%08lx,0x%08lx)\n",this,(DWORD)hwnd,dwflags);
162         return 0;
163 }
164
165 static HRESULT WINAPI IDirectInputDeviceA_SetProperty(
166         LPDIRECTINPUTDEVICE32A this,REFGUID rguid,LPCDIPROPHEADER ph
167 ) {
168         char    xbuf[50];
169
170         if (HIWORD(rguid))
171                 StringFromCLSID(rguid,xbuf);
172         else
173                 strcpy(xbuf,"<no guid>");
174         fprintf(stderr,"IDirectInputDeviceA(%p)->SetProperty(%s,%p)\n",this,xbuf,ph);
175         return 0;
176 }
177
178 static HRESULT WINAPI IDirectInputDeviceA_SetEventNotification(
179         LPDIRECTINPUTDEVICE32A this,HANDLE32 hnd
180 ) {
181         fprintf(stderr,"IDirectInputDeviceA(%p)->SetEventNotification(0x%08lx)\n",this,(DWORD)hnd);
182         return 0;
183 }
184
185 static HRESULT WINAPI IDirectInputDeviceA_GetDeviceData(
186         LPDIRECTINPUTDEVICE32A this,DWORD x,LPDIDEVICEOBJECTDATA dod,
187         LPDWORD y,DWORD z
188 ) {
189 /*
190         fprintf(stderr,"IDirectInputDeviceA(%p)->GetDeviceData(0x%08lx,%p,%p,0x%08lx)\n",this,x,dod,y,z);
191  */
192         return 0;
193 }
194
195 static HRESULT WINAPI IDirectInputDeviceA_Acquire(LPDIRECTINPUTDEVICE32A this) {
196         fprintf(stderr,"IDirectInputDeviceA(%p)->Aquire()\n",this);
197         return 0;
198 }
199
200 static HRESULT WINAPI IDirectInputDeviceA_Unacquire(LPDIRECTINPUTDEVICE32A this) {
201         fprintf(stderr,"IDirectInputDeviceA(%p)->Unaquire()\n",this);
202         return 0;
203 }
204
205 static ULONG WINAPI IDirectInputDeviceA_Release(LPDIRECTINPUTDEVICE32A this) {
206         this->ref--;
207         if (this->ref)
208                 return this->ref;
209         HeapFree(GetProcessHeap(),0,this);
210         return 0;
211 }
212
213
214 extern BYTE InputKeyStateTable[256];
215 extern BYTE vkey2scode[512];
216
217 static HRESULT WINAPI IDirectInputDeviceA_GetDeviceState(
218         LPDIRECTINPUTDEVICE32A this,DWORD len,LPVOID ptr
219 ) {
220 /*
221         fprintf(stderr,"IDirectInputDeviceA(%p)->GetDeviceState(0x%08lx,%p)\n",
222                 this,len,ptr
223         );
224  */
225         if (len==256) {/* && this_is_a_keyboard */
226                 int     i;
227
228                 memset(ptr,0,256);
229                 for (i=0;i<256;i++) {
230                         if (InputKeyStateTable[i]&0x80)
231                                 fprintf(stderr,"VKEY %d pressed (DIK 0x%02x\n",i,vkey2scode[i]);
232                         ((LPBYTE)ptr)[i]=vkey2scode[InputKeyStateTable[i]]&0x80;
233                 }
234                 return 0;
235         }
236         return 0;
237 }
238
239 static HRESULT WINAPI IDirectInputDeviceA_QueryInterface(
240         LPDIRECTINPUTDEVICE32A this,REFIID riid,LPVOID *ppobj
241 ) {
242         char    xbuf[50];
243
244         StringFromCLSID(riid,xbuf);
245         fprintf(stderr,"IDirectInputA(%p)->QueryInterface(%s,%p)\n",this,xbuf,ppobj);
246         if (!memcmp(&IID_IUnknown,riid,sizeof(*riid))) {
247                 this->lpvtbl->fnAddRef(this);
248                 *ppobj = this;
249                 return 0;
250         }
251         if (!memcmp(&IID_IDirectInputDeviceA,riid,sizeof(*riid))) {
252                 this->lpvtbl->fnAddRef(this);
253                 *ppobj = this;
254                 return 0;
255         }
256         return E_FAIL;
257 }
258
259 static IDirectInputDeviceA_VTable SysKeyboardAvt={
260         IDirectInputDeviceA_QueryInterface,
261         (void*)2,
262         IDirectInputDeviceA_Release,
263         (void*)4,
264         (void*)5,
265         (void*)6,
266         IDirectInputDeviceA_SetProperty,
267         IDirectInputDeviceA_Acquire,
268         IDirectInputDeviceA_Unacquire,
269         IDirectInputDeviceA_GetDeviceState,
270         IDirectInputDeviceA_GetDeviceData,
271         IDirectInputDeviceA_SetDataFormat,
272         IDirectInputDeviceA_SetEventNotification,
273         IDirectInputDeviceA_SetCooperativeLevel,
274         (void*)15,
275         (void*)16,
276         (void*)17,
277         (void*)18,
278 };
279
280 static IDirectInputDeviceA_VTable SysMouseAvt={
281         IDirectInputDeviceA_QueryInterface,
282         (void*)2,
283         IDirectInputDeviceA_Release,
284         (void*)4,
285         (void*)5,
286         (void*)6,
287         IDirectInputDeviceA_SetProperty,
288         IDirectInputDeviceA_Acquire,
289         IDirectInputDeviceA_Unacquire,
290         IDirectInputDeviceA_GetDeviceState,
291         IDirectInputDeviceA_GetDeviceData,
292         IDirectInputDeviceA_SetDataFormat,
293         IDirectInputDeviceA_SetEventNotification,
294         IDirectInputDeviceA_SetCooperativeLevel,
295         (void*)15,
296         (void*)16,
297         (void*)17,
298         (void*)18,
299 };