oleaut32/tests: Initialize reserved fields to unique values.
[wine/multimedia.git] / dlls / dinput / keyboard.c
blobcb290f6f07a7cc551e39c40af48879b2de7c1a13
1 /* DirectInput Keyboard device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2005 Raphael Junqueira
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winerror.h"
32 #include "dinput.h"
34 #include "dinput_private.h"
35 #include "device_private.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
41 #define WINE_DINPUT_KEYBOARD_MAX_KEYS 256
43 static const IDirectInputDevice8AVtbl SysKeyboardAvt;
44 static const IDirectInputDevice8WVtbl SysKeyboardWvt;
46 typedef struct SysKeyboardImpl SysKeyboardImpl;
47 struct SysKeyboardImpl
49 struct IDirectInputDevice2AImpl base;
50 BYTE DInputKeyState[WINE_DINPUT_KEYBOARD_MAX_KEYS];
53 static void KeyboardCallback( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam )
55 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
56 int dik_code;
57 KBDLLHOOKSTRUCT *hook = (KBDLLHOOKSTRUCT *)lparam;
58 BYTE new_diks;
60 if (wparam != WM_KEYDOWN && wparam != WM_KEYUP &&
61 wparam != WM_SYSKEYDOWN && wparam != WM_SYSKEYUP)
62 return;
64 TRACE("(%p) %ld,%ld\n", iface, wparam, lparam);
66 dik_code = hook->scanCode & 0xff;
67 /* R-Shift is special - it is an extended key with separate scan code */
68 if (hook->flags & LLKHF_EXTENDED && dik_code != 0x36)
69 dik_code |= 0x80;
71 new_diks = hook->flags & LLKHF_UP ? 0 : 0x80;
73 /* returns now if key event already known */
74 if (new_diks == This->DInputKeyState[dik_code])
75 return;
77 This->DInputKeyState[dik_code] = new_diks;
78 TRACE(" setting %02X to %02X\n", dik_code, This->DInputKeyState[dik_code]);
80 dik_code = id_to_offset(&This->base.data_format, DIDFT_MAKEINSTANCE(dik_code) | DIDFT_PSHBUTTON);
81 EnterCriticalSection(&This->base.crit);
82 queue_event((LPDIRECTINPUTDEVICE8A)This, dik_code, new_diks, hook->time, This->base.dinput->evsequence++);
83 LeaveCriticalSection(&This->base.crit);
86 const GUID DInput_Wine_Keyboard_GUID = { /* 0ab8648a-7735-11d2-8c73-71df54a96441 */
87 0x0ab8648a, 0x7735, 0x11d2, {0x8c, 0x73, 0x71, 0xdf, 0x54, 0xa9, 0x64, 0x41}
90 static void fill_keyboard_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
91 DWORD dwSize;
92 DIDEVICEINSTANCEA ddi;
94 dwSize = lpddi->dwSize;
96 TRACE("%d %p\n", dwSize, lpddi);
98 memset(lpddi, 0, dwSize);
99 memset(&ddi, 0, sizeof(ddi));
101 ddi.dwSize = dwSize;
102 ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
103 ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
104 if (version >= 0x0800)
105 ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
106 else
107 ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
108 strcpy(ddi.tszInstanceName, "Keyboard");
109 strcpy(ddi.tszProductName, "Wine Keyboard");
111 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
114 static void fill_keyboard_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
115 DWORD dwSize;
116 DIDEVICEINSTANCEW ddi;
118 dwSize = lpddi->dwSize;
120 TRACE("%d %p\n", dwSize, lpddi);
122 memset(lpddi, 0, dwSize);
123 memset(&ddi, 0, sizeof(ddi));
125 ddi.dwSize = dwSize;
126 ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
127 ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
128 if (version >= 0x0800)
129 ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
130 else
131 ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
132 MultiByteToWideChar(CP_ACP, 0, "Keyboard", -1, ddi.tszInstanceName, MAX_PATH);
133 MultiByteToWideChar(CP_ACP, 0, "Wine Keyboard", -1, ddi.tszProductName, MAX_PATH);
135 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
138 static BOOL keyboarddev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
140 if (id != 0)
141 return FALSE;
143 if ((dwDevType == 0) ||
144 ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
145 (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
146 TRACE("Enumerating the Keyboard device\n");
148 fill_keyboard_dideviceinstanceA(lpddi, version);
150 return TRUE;
153 return FALSE;
156 static BOOL keyboarddev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
158 if (id != 0)
159 return FALSE;
161 if ((dwDevType == 0) ||
162 ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
163 (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
164 TRACE("Enumerating the Keyboard device\n");
166 fill_keyboard_dideviceinstanceW(lpddi, version);
168 return TRUE;
171 return FALSE;
174 static SysKeyboardImpl *alloc_device(REFGUID rguid, const void *kvt, IDirectInputImpl *dinput)
176 SysKeyboardImpl* newDevice;
177 LPDIDATAFORMAT df = NULL;
178 int i, idx = 0;
180 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
181 newDevice->base.lpVtbl = kvt;
182 newDevice->base.ref = 1;
183 memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
184 newDevice->base.dinput = dinput;
185 newDevice->base.event_proc = KeyboardCallback;
186 InitializeCriticalSection(&newDevice->base.crit);
187 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysKeyboardImpl*->base.crit");
189 /* Create copy of default data format */
190 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIKeyboard.dwSize))) goto failed;
191 memcpy(df, &c_dfDIKeyboard, c_dfDIKeyboard.dwSize);
192 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
194 for (i = 0; i < df->dwNumObjs; i++)
196 char buf[MAX_PATH];
198 if (!GetKeyNameTextA(((i & 0x7f) << 16) | ((i & 0x80) << 17), buf, sizeof(buf)))
199 continue;
201 memcpy(&df->rgodf[idx], &c_dfDIKeyboard.rgodf[i], df->dwObjSize);
202 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
204 df->dwNumObjs = idx;
206 newDevice->base.data_format.wine_df = df;
207 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
208 return newDevice;
210 failed:
211 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
212 HeapFree(GetProcessHeap(), 0, df);
213 HeapFree(GetProcessHeap(), 0, newDevice);
214 return NULL;
218 static HRESULT keyboarddev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
220 if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) || /* Generic Keyboard */
221 (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
222 if ((riid == NULL) ||
223 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
224 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
225 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
226 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
227 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
228 TRACE("Creating a Keyboard device (%p)\n", *pdev);
229 if (!*pdev) return DIERR_OUTOFMEMORY;
230 return DI_OK;
231 } else
232 return DIERR_NOINTERFACE;
234 return DIERR_DEVICENOTREG;
237 static HRESULT keyboarddev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
239 if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) || /* Generic Keyboard */
240 (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
241 if ((riid == NULL) ||
242 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
243 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
244 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
245 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
246 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysKeyboardWvt, dinput);
247 TRACE("Creating a Keyboard device (%p)\n", *pdev);
248 if (!*pdev) return DIERR_OUTOFMEMORY;
249 return DI_OK;
250 } else
251 return DIERR_NOINTERFACE;
253 return DIERR_DEVICENOTREG;
256 const struct dinput_device keyboard_device = {
257 "Wine keyboard driver",
258 keyboarddev_enum_deviceA,
259 keyboarddev_enum_deviceW,
260 keyboarddev_create_deviceA,
261 keyboarddev_create_deviceW
264 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
265 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
268 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
269 TRACE("(%p)->(%d,%p)\n", This, len, ptr);
271 if (!This->base.acquired) return DIERR_NOTACQUIRED;
273 if (len != This->base.data_format.user_df->dwDataSize )
274 return DIERR_INVALIDPARAM;
276 EnterCriticalSection(&This->base.crit);
278 if (TRACE_ON(dinput)) {
279 int i;
280 for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
281 if (This->DInputKeyState[i] != 0x00)
282 TRACE(" - %02X: %02x\n", i, This->DInputKeyState[i]);
286 fill_DataFormat(ptr, This->DInputKeyState, &This->base.data_format);
287 LeaveCriticalSection(&This->base.crit);
289 return DI_OK;
292 /******************************************************************************
293 * GetCapabilities : get the device capabilities
295 static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
296 LPDIRECTINPUTDEVICE8A iface,
297 LPDIDEVCAPS lpDIDevCaps)
299 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
300 DIDEVCAPS devcaps;
302 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
304 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
305 WARN("invalid parameter\n");
306 return DIERR_INVALIDPARAM;
309 devcaps.dwSize = lpDIDevCaps->dwSize;
310 devcaps.dwFlags = DIDC_ATTACHED;
311 if (This->base.dinput->dwVersion >= 0x0800)
312 devcaps.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
313 else
314 devcaps.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
315 devcaps.dwAxes = 0;
316 devcaps.dwButtons = This->base.data_format.wine_df->dwNumObjs;
317 devcaps.dwPOVs = 0;
318 devcaps.dwFFSamplePeriod = 0;
319 devcaps.dwFFMinTimeResolution = 0;
320 devcaps.dwFirmwareRevision = 100;
321 devcaps.dwHardwareRevision = 100;
322 devcaps.dwFFDriverVersion = 0;
324 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
326 return DI_OK;
329 /******************************************************************************
330 * GetObjectInfo : get information about a device object such as a button
331 * or axis
333 static HRESULT WINAPI
334 SysKeyboardAImpl_GetObjectInfo(
335 LPDIRECTINPUTDEVICE8A iface,
336 LPDIDEVICEOBJECTINSTANCEA pdidoi,
337 DWORD dwObj,
338 DWORD dwHow)
340 HRESULT res;
342 res = IDirectInputDevice2AImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
343 if (res != DI_OK) return res;
345 if (!GetKeyNameTextA((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
346 (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
347 pdidoi->tszName, sizeof(pdidoi->tszName)))
348 return DIERR_OBJECTNOTFOUND;
350 _dump_OBJECTINSTANCEA(pdidoi);
351 return res;
354 static HRESULT WINAPI SysKeyboardWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
355 LPDIDEVICEOBJECTINSTANCEW pdidoi,
356 DWORD dwObj,
357 DWORD dwHow)
359 HRESULT res;
361 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
362 if (res != DI_OK) return res;
364 if (!GetKeyNameTextW((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
365 (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
366 pdidoi->tszName,
367 sizeof(pdidoi->tszName)/sizeof(pdidoi->tszName[0])))
368 return DIERR_OBJECTNOTFOUND;
370 _dump_OBJECTINSTANCEW(pdidoi);
371 return res;
374 /******************************************************************************
375 * GetDeviceInfo : get information about a device's identity
377 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceInfo(
378 LPDIRECTINPUTDEVICE8A iface,
379 LPDIDEVICEINSTANCEA pdidi)
381 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
382 TRACE("(this=%p,%p)\n", This, pdidi);
384 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
385 WARN(" dinput3 not supported yet...\n");
386 return DI_OK;
389 fill_keyboard_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
391 return DI_OK;
394 static HRESULT WINAPI SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
396 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
397 TRACE("(this=%p,%p)\n", This, pdidi);
399 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
400 WARN(" dinput3 not supported yet...\n");
401 return DI_OK;
404 fill_keyboard_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
406 return DI_OK;
409 static const IDirectInputDevice8AVtbl SysKeyboardAvt =
411 IDirectInputDevice2AImpl_QueryInterface,
412 IDirectInputDevice2AImpl_AddRef,
413 IDirectInputDevice2AImpl_Release,
414 SysKeyboardAImpl_GetCapabilities,
415 IDirectInputDevice2AImpl_EnumObjects,
416 IDirectInputDevice2AImpl_GetProperty,
417 IDirectInputDevice2AImpl_SetProperty,
418 IDirectInputDevice2AImpl_Acquire,
419 IDirectInputDevice2AImpl_Unacquire,
420 SysKeyboardAImpl_GetDeviceState,
421 IDirectInputDevice2AImpl_GetDeviceData,
422 IDirectInputDevice2AImpl_SetDataFormat,
423 IDirectInputDevice2AImpl_SetEventNotification,
424 IDirectInputDevice2AImpl_SetCooperativeLevel,
425 SysKeyboardAImpl_GetObjectInfo,
426 SysKeyboardAImpl_GetDeviceInfo,
427 IDirectInputDevice2AImpl_RunControlPanel,
428 IDirectInputDevice2AImpl_Initialize,
429 IDirectInputDevice2AImpl_CreateEffect,
430 IDirectInputDevice2AImpl_EnumEffects,
431 IDirectInputDevice2AImpl_GetEffectInfo,
432 IDirectInputDevice2AImpl_GetForceFeedbackState,
433 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
434 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
435 IDirectInputDevice2AImpl_Escape,
436 IDirectInputDevice2AImpl_Poll,
437 IDirectInputDevice2AImpl_SendDeviceData,
438 IDirectInputDevice7AImpl_EnumEffectsInFile,
439 IDirectInputDevice7AImpl_WriteEffectToFile,
440 IDirectInputDevice8AImpl_BuildActionMap,
441 IDirectInputDevice8AImpl_SetActionMap,
442 IDirectInputDevice8AImpl_GetImageInfo
445 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
446 # define XCAST(fun) (typeof(SysKeyboardWvt.fun))
447 #else
448 # define XCAST(fun) (void*)
449 #endif
451 static const IDirectInputDevice8WVtbl SysKeyboardWvt =
453 IDirectInputDevice2WImpl_QueryInterface,
454 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
455 XCAST(Release)IDirectInputDevice2AImpl_Release,
456 XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
457 IDirectInputDevice2WImpl_EnumObjects,
458 XCAST(GetProperty)IDirectInputDevice2AImpl_GetProperty,
459 XCAST(SetProperty)IDirectInputDevice2AImpl_SetProperty,
460 XCAST(Acquire)IDirectInputDevice2AImpl_Acquire,
461 XCAST(Unacquire)IDirectInputDevice2AImpl_Unacquire,
462 XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
463 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
464 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
465 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
466 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
467 SysKeyboardWImpl_GetObjectInfo,
468 SysKeyboardWImpl_GetDeviceInfo,
469 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
470 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
471 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
472 IDirectInputDevice2WImpl_EnumEffects,
473 IDirectInputDevice2WImpl_GetEffectInfo,
474 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
475 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
476 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
477 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
478 XCAST(Poll)IDirectInputDevice2AImpl_Poll,
479 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
480 IDirectInputDevice7WImpl_EnumEffectsInFile,
481 IDirectInputDevice7WImpl_WriteEffectToFile,
482 IDirectInputDevice8WImpl_BuildActionMap,
483 IDirectInputDevice8WImpl_SetActionMap,
484 IDirectInputDevice8WImpl_GetImageInfo
486 #undef XCAST