source: trunk/src/dinput/keyboard.c@ 8346

Last change on this file since 8346 was 8346, checked in by sandervl, 23 years ago

Resync with last X11 licensed Wine

File size: 9.8 KB
Line 
1/* DirectInput Keyboard device
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 *
6 */
7
8#include "config.h"
9#include <string.h>
10#ifdef HAVE_SYS_ERRNO_H
11# include <sys/errno.h>
12#endif
13
14#include "winbase.h"
15#include "winuser.h"
16#include "winerror.h"
17#include "dinput.h"
18
19#include "dinput_private.h"
20#include "device_private.h"
21#include "debugtools.h"
22
23DEFAULT_DEBUG_CHANNEL(dinput);
24
25static ICOM_VTABLE(IDirectInputDevice2A) SysKeyboardAvt;
26static ICOM_VTABLE(IDirectInputDevice7A) SysKeyboard7Avt;
27
28typedef struct SysKeyboardAImpl SysKeyboardAImpl;
29struct SysKeyboardAImpl
30{
31 /* IDirectInputDevice2AImpl */
32 ICOM_VFIELD(IDirectInputDevice2A);
33 DWORD ref;
34 GUID guid;
35
36 IDirectInputAImpl *dinput;
37
38 /* SysKeyboardAImpl */
39 BYTE keystate[256];
40 int acquired;
41};
42
43static GUID DInput_Wine_Keyboard_GUID = { /* 0ab8648a-7735-11d2-8c73-71df54a96441 */
44 0x0ab8648a,
45 0x7735,
46 0x11d2,
47 {0x8c, 0x73, 0x71, 0xdf, 0x54, 0xa9, 0x64, 0x41}
48};
49
50static BOOL keyboarddev_enum_device(DWORD dwDevType, DWORD dwFlags, LPCDIDEVICEINSTANCEA lpddi)
51{
52 if ((dwDevType == 0) || (dwDevType == DIDEVTYPE_KEYBOARD)) {
53 TRACE("Enumerating the Keyboard device\n");
54
55 lpddi->guidInstance = GUID_SysKeyboard;/* DInput's GUID */
56 lpddi->guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
57 lpddi->dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
58 strcpy(lpddi->tszInstanceName, "Keyboard");
59 strcpy(lpddi->tszProductName, "Wine Keyboard");
60
61 return TRUE;
62 }
63
64 return FALSE;
65}
66
67static SysKeyboardAImpl *alloc_device(REFGUID rguid, ICOM_VTABLE(IDirectInputDevice2A) *kvt, IDirectInputAImpl *dinput)
68{
69 SysKeyboardAImpl* newDevice;
70 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardAImpl));
71 newDevice->ref = 1;
72 ICOM_VTBL(newDevice) = kvt;
73 memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
74 memset(newDevice->keystate,0,256);
75 newDevice->dinput = dinput;
76
77 return newDevice;
78}
79
80
81static HRESULT keyboarddev_create_device(IDirectInputAImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
82{
83 if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) || /* Generic Keyboard */
84 (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
85 if ((riid == NULL) || (IsEqualGUID(&IID_IDirectInputDevice2A,riid)) || (IsEqualGUID(&IID_IDirectInputDevice2A,riid))) {
86 *pdev=(IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
87
88 TRACE("Creating a Keyboard device (%p)\n", *pdev);
89 return DI_OK;
90 } else if (IsEqualGUID(&IID_IDirectInputDevice7A,riid)) {
91 *pdev=(IDirectInputDeviceA*) alloc_device(rguid, (ICOM_VTABLE(IDirectInputDevice2A) *) &SysKeyboard7Avt, dinput);
92
93 TRACE("Creating a Keyboard DInput7A device (%p)\n", *pdev);
94 return DI_OK;
95 } else
96 return DIERR_NOINTERFACE;
97 }
98
99 return DIERR_DEVICENOTREG;
100}
101
102static dinput_device keyboarddev = {
103 100,
104 keyboarddev_enum_device,
105 keyboarddev_create_device
106};
107
108DECL_GLOBAL_CONSTRUCTOR(keyboarddev_register) { dinput_register_device(&keyboarddev); }
109
110static HRESULT WINAPI SysKeyboardAImpl_SetProperty(
111 LPDIRECTINPUTDEVICE2A iface,REFGUID rguid,LPCDIPROPHEADER ph
112)
113{
114 ICOM_THIS(SysKeyboardAImpl,iface);
115
116 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
117 TRACE("(size=%ld,headersize=%ld,obj=%ld,how=%ld\n",
118 ph->dwSize,ph->dwHeaderSize,ph->dwObj,ph->dwHow);
119 if (!HIWORD(rguid)) {
120 switch ((DWORD)rguid) {
121 case (DWORD) DIPROP_BUFFERSIZE: {
122 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
123
124 TRACE("(buffersize=%ld)\n",pd->dwData);
125 break;
126 }
127 default:
128 WARN("Unknown type %ld\n",(DWORD)rguid);
129 break;
130 }
131 }
132 return 0;
133}
134
135static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
136 LPDIRECTINPUTDEVICE2A iface,DWORD len,LPVOID ptr
137)
138{
139 DWORD i;
140
141 memset( ptr, 0, len );
142 if (len != 256)
143 {
144 WARN("whoops, got len %ld?\n", len);
145 return DI_OK;
146 }
147 for (i = 0; i < 0x80; i++)
148 {
149 WORD vkey = MapVirtualKeyA( i, 1 );
150 if (vkey && (GetAsyncKeyState( vkey ) & 0x8000))
151 {
152 ((LPBYTE)ptr)[i] = 0x80;
153 ((LPBYTE)ptr)[i | 0x80] = 0x80;
154 }
155 }
156 return DI_OK;
157}
158
159static HRESULT WINAPI SysKeyboardAImpl_GetDeviceData(
160 LPDIRECTINPUTDEVICE2A iface,DWORD dodsize,LPDIDEVICEOBJECTDATA dod,
161 LPDWORD entries,DWORD flags
162)
163{
164 ICOM_THIS(SysKeyboardAImpl,iface);
165 int i, n;
166
167 TRACE("(this=%p,%ld,%p,%p(%ld)),0x%08lx)\n",
168 This,dodsize,dod,entries,entries?*entries:0,flags);
169
170
171 for (i = n = 0; (i < 0x80) && (n < *entries); i++)
172 {
173 WORD state, vkey = MapVirtualKeyA( i, 1 );
174 if (!vkey) continue;
175 state = (GetAsyncKeyState( vkey ) >> 8) & 0x80;
176 if (state != This->keystate[vkey])
177 {
178 if (dod)
179 {
180 /* add an entry */
181 dod[n].dwOfs = i; /* scancode */
182 dod[n].dwData = state;
183 dod[n].dwTimeStamp = GetCurrentTime(); /* umm */
184 dod[n].dwSequence = This->dinput->evsequence++;
185 n++;
186 }
187 if (!(flags & DIGDD_PEEK)) This->keystate[vkey] = state;
188 }
189 }
190 if (n) TRACE_(dinput)("%d entries\n",n);
191 *entries = n;
192 return DI_OK;
193}
194
195static HRESULT WINAPI SysKeyboardAImpl_Acquire(LPDIRECTINPUTDEVICE2A iface)
196{
197 ICOM_THIS(SysKeyboardAImpl,iface);
198
199 TRACE("(this=%p)\n",This);
200
201 if (This->acquired == 0) {
202 This->acquired = 1;
203 }
204
205 return DI_OK;
206}
207
208static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE2A iface)
209{
210 ICOM_THIS(SysKeyboardAImpl,iface);
211 TRACE("(this=%p)\n",This);
212
213 if (This->acquired == 1) {
214 This->acquired = 0;
215 } else {
216 ERR("Unacquiring a not-acquired device !!!\n");
217 }
218
219 return DI_OK;
220}
221
222/******************************************************************************
223 * GetCapabilities : get the device capablitites
224 */
225static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
226 LPDIRECTINPUTDEVICE2A iface,
227 LPDIDEVCAPS lpDIDevCaps)
228{
229 ICOM_THIS(SysKeyboardAImpl,iface);
230
231 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
232
233 if (lpDIDevCaps->dwSize == sizeof(DIDEVCAPS)) {
234 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
235 lpDIDevCaps->dwDevType = DIDEVTYPE_KEYBOARD;
236 lpDIDevCaps->dwAxes = 0;
237 lpDIDevCaps->dwButtons = 0;
238 lpDIDevCaps->dwPOVs = 0;
239 lpDIDevCaps->dwFFSamplePeriod = 0;
240 lpDIDevCaps->dwFFMinTimeResolution = 0;
241 lpDIDevCaps->dwFirmwareRevision = 100;
242 lpDIDevCaps->dwHardwareRevision = 100;
243 lpDIDevCaps->dwFFDriverVersion = 0;
244 } else {
245 /* DirectX 3.0 */
246 FIXME("DirectX 3.0 not supported....\n");
247 }
248
249 return DI_OK;
250}
251
252static ICOM_VTABLE(IDirectInputDevice2A) SysKeyboardAvt =
253{
254 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
255 IDirectInputDevice2AImpl_QueryInterface,
256 IDirectInputDevice2AImpl_AddRef,
257 IDirectInputDevice2AImpl_Release,
258 SysKeyboardAImpl_GetCapabilities,
259 IDirectInputDevice2AImpl_EnumObjects,
260 IDirectInputDevice2AImpl_GetProperty,
261 SysKeyboardAImpl_SetProperty,
262 SysKeyboardAImpl_Acquire,
263 SysKeyboardAImpl_Unacquire,
264 SysKeyboardAImpl_GetDeviceState,
265 SysKeyboardAImpl_GetDeviceData,
266 IDirectInputDevice2AImpl_SetDataFormat,
267 IDirectInputDevice2AImpl_SetEventNotification,
268 IDirectInputDevice2AImpl_SetCooperativeLevel,
269 IDirectInputDevice2AImpl_GetObjectInfo,
270 IDirectInputDevice2AImpl_GetDeviceInfo,
271 IDirectInputDevice2AImpl_RunControlPanel,
272 IDirectInputDevice2AImpl_Initialize,
273 IDirectInputDevice2AImpl_CreateEffect,
274 IDirectInputDevice2AImpl_EnumEffects,
275 IDirectInputDevice2AImpl_GetEffectInfo,
276 IDirectInputDevice2AImpl_GetForceFeedbackState,
277 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
278 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
279 IDirectInputDevice2AImpl_Escape,
280 IDirectInputDevice2AImpl_Poll,
281 IDirectInputDevice2AImpl_SendDeviceData
282};
283
284#if !defined(__STRICT_ANSI__) && defined(__GNUC__)
285# define XCAST(fun) (typeof(SysKeyboard7Avt.fun))
286#else
287# define XCAST(fun) (void*)
288#endif
289
290static ICOM_VTABLE(IDirectInputDevice7A) SysKeyboard7Avt =
291{
292 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
293 XCAST(QueryInterface)IDirectInputDevice2AImpl_QueryInterface,
294 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
295 XCAST(Release)IDirectInputDevice2AImpl_Release,
296 XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
297 XCAST(EnumObjects)IDirectInputDevice2AImpl_EnumObjects,
298 XCAST(GetProperty)IDirectInputDevice2AImpl_GetProperty,
299 XCAST(SetProperty)SysKeyboardAImpl_SetProperty,
300 XCAST(Acquire)SysKeyboardAImpl_Acquire,
301 XCAST(Unacquire)SysKeyboardAImpl_Unacquire,
302 XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
303 XCAST(GetDeviceData)SysKeyboardAImpl_GetDeviceData,
304 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
305 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
306 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
307 XCAST(GetObjectInfo)IDirectInputDevice2AImpl_GetObjectInfo,
308 XCAST(GetDeviceInfo)IDirectInputDevice2AImpl_GetDeviceInfo,
309 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
310 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
311 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
312 XCAST(EnumEffects)IDirectInputDevice2AImpl_EnumEffects,
313 XCAST(GetEffectInfo)IDirectInputDevice2AImpl_GetEffectInfo,
314 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
315 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
316 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
317 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
318 XCAST(Poll)IDirectInputDevice2AImpl_Poll,
319 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
320 IDirectInputDevice7AImpl_EnumEffectsInFile,
321 IDirectInputDevice7AImpl_WriteEffectToFile
322};
323
324#undef XCAST
Note: See TracBrowser for help on using the repository browser.