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
24 #include "wine/port.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;
57 KBDLLHOOKSTRUCT *hook = (KBDLLHOOKSTRUCT *)lparam;
60 if (wparam != WM_KEYDOWN && wparam != WM_KEYUP &&
61 wparam != WM_SYSKEYDOWN && wparam != WM_SYSKEYUP)
64 TRACE("(%p) %ld,%ld\n", iface, wparam, lparam);
66 dik_code = hook->scanCode & 0xff;
67 if (hook->flags & LLKHF_EXTENDED) dik_code |= 0x80;
69 new_diks = hook->flags & LLKHF_UP ? 0 : 0x80;
71 /* returns now if key event already known */
72 if (new_diks == This->DInputKeyState[dik_code])
75 This->DInputKeyState[dik_code] = new_diks;
76 TRACE(" setting %02X to %02X\n", dik_code, This->DInputKeyState[dik_code]);
78 dik_code = id_to_offset(&This->base.data_format, DIDFT_MAKEINSTANCE(dik_code) | DIDFT_PSHBUTTON);
79 EnterCriticalSection(&This->base.crit);
80 queue_event((LPDIRECTINPUTDEVICE8A)This, dik_code, new_diks, hook->time, This->base.dinput->evsequence++);
81 LeaveCriticalSection(&This->base.crit);
84 const GUID DInput_Wine_Keyboard_GUID = { /* 0ab8648a-7735-11d2-8c73-71df54a96441 */
85 0x0ab8648a, 0x7735, 0x11d2, {0x8c, 0x73, 0x71, 0xdf, 0x54, 0xa9, 0x64, 0x41}
88 static void fill_keyboard_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
90 DIDEVICEINSTANCEA ddi;
92 dwSize = lpddi->dwSize;
94 TRACE("%d %p\n", dwSize, lpddi);
96 memset(lpddi, 0, dwSize);
97 memset(&ddi, 0, sizeof(ddi));
100 ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
101 ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
102 if (version >= 0x0800)
103 ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
105 ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
106 strcpy(ddi.tszInstanceName, "Keyboard");
107 strcpy(ddi.tszProductName, "Wine Keyboard");
109 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
112 static void fill_keyboard_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
114 DIDEVICEINSTANCEW ddi;
116 dwSize = lpddi->dwSize;
118 TRACE("%d %p\n", dwSize, lpddi);
120 memset(lpddi, 0, dwSize);
121 memset(&ddi, 0, sizeof(ddi));
124 ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
125 ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
126 if (version >= 0x0800)
127 ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
129 ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
130 MultiByteToWideChar(CP_ACP, 0, "Keyboard", -1, ddi.tszInstanceName, MAX_PATH);
131 MultiByteToWideChar(CP_ACP, 0, "Wine Keyboard", -1, ddi.tszProductName, MAX_PATH);
133 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
136 static BOOL keyboarddev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
141 if ((dwDevType == 0) ||
142 ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
143 (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
144 TRACE("Enumerating the Keyboard device\n");
146 fill_keyboard_dideviceinstanceA(lpddi, version);
154 static BOOL keyboarddev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
159 if ((dwDevType == 0) ||
160 ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
161 (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
162 TRACE("Enumerating the Keyboard device\n");
164 fill_keyboard_dideviceinstanceW(lpddi, version);
172 static SysKeyboardImpl *alloc_device(REFGUID rguid, const void *kvt, IDirectInputImpl *dinput)
174 SysKeyboardImpl* newDevice;
175 LPDIDATAFORMAT df = NULL;
178 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
179 newDevice->base.lpVtbl = kvt;
180 newDevice->base.ref = 1;
181 memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
182 newDevice->base.dinput = dinput;
183 newDevice->base.event_proc = KeyboardCallback;
184 InitializeCriticalSection(&newDevice->base.crit);
185 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysKeyboardImpl*->base.crit");
187 /* Create copy of default data format */
188 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIKeyboard.dwSize))) goto failed;
189 memcpy(df, &c_dfDIKeyboard, c_dfDIKeyboard.dwSize);
190 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
192 for (i = 0; i < df->dwNumObjs; i++)
196 if (!GetKeyNameTextA(((i & 0x7f) << 16) | ((i & 0x80) << 17), buf, sizeof(buf)))
199 memcpy(&df->rgodf[idx], &c_dfDIKeyboard.rgodf[i], df->dwObjSize);
200 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
204 newDevice->base.data_format.wine_df = df;
205 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
209 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
210 HeapFree(GetProcessHeap(), 0, df);
211 HeapFree(GetProcessHeap(), 0, newDevice);
216 static HRESULT keyboarddev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
218 if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) || /* Generic Keyboard */
219 (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
220 if ((riid == NULL) ||
221 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
222 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
223 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
224 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
225 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
226 TRACE("Creating a Keyboard device (%p)\n", *pdev);
227 if (!*pdev) return DIERR_OUTOFMEMORY;
230 return DIERR_NOINTERFACE;
232 return DIERR_DEVICENOTREG;
235 static HRESULT keyboarddev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
237 if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) || /* Generic Keyboard */
238 (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
239 if ((riid == NULL) ||
240 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
241 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
242 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
243 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
244 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysKeyboardWvt, dinput);
245 TRACE("Creating a Keyboard device (%p)\n", *pdev);
246 if (!*pdev) return DIERR_OUTOFMEMORY;
249 return DIERR_NOINTERFACE;
251 return DIERR_DEVICENOTREG;
254 const struct dinput_device keyboard_device = {
255 "Wine keyboard driver",
256 keyboarddev_enum_deviceA,
257 keyboarddev_enum_deviceW,
258 keyboarddev_create_deviceA,
259 keyboarddev_create_deviceW
262 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
263 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
266 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
267 TRACE("(%p)->(%d,%p)\n", This, len, ptr);
269 if (!This->base.acquired) return DIERR_NOTACQUIRED;
271 if (len != WINE_DINPUT_KEYBOARD_MAX_KEYS)
272 return DIERR_INVALIDPARAM;
274 EnterCriticalSection(&This->base.crit);
276 if (TRACE_ON(dinput)) {
278 for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
279 if (This->DInputKeyState[i] != 0x00)
280 TRACE(" - %02X: %02x\n", i, This->DInputKeyState[i]);
284 memcpy(ptr, This->DInputKeyState, WINE_DINPUT_KEYBOARD_MAX_KEYS);
285 LeaveCriticalSection(&This->base.crit);
290 /******************************************************************************
291 * GetCapabilities : get the device capablitites
293 static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
294 LPDIRECTINPUTDEVICE8A iface,
295 LPDIDEVCAPS lpDIDevCaps)
297 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
300 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
302 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
303 WARN("invalid parameter\n");
304 return DIERR_INVALIDPARAM;
307 devcaps.dwSize = lpDIDevCaps->dwSize;
308 devcaps.dwFlags = DIDC_ATTACHED;
309 if (This->base.dinput->dwVersion >= 0x0800)
310 devcaps.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
312 devcaps.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
314 devcaps.dwButtons = This->base.data_format.wine_df->dwNumObjs;
316 devcaps.dwFFSamplePeriod = 0;
317 devcaps.dwFFMinTimeResolution = 0;
318 devcaps.dwFirmwareRevision = 100;
319 devcaps.dwHardwareRevision = 100;
320 devcaps.dwFFDriverVersion = 0;
322 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
327 /******************************************************************************
328 * GetObjectInfo : get information about a device object such as a button
331 static HRESULT WINAPI
332 SysKeyboardAImpl_GetObjectInfo(
333 LPDIRECTINPUTDEVICE8A iface,
334 LPDIDEVICEOBJECTINSTANCEA pdidoi,
340 res = IDirectInputDevice2AImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
341 if (res != DI_OK) return res;
343 if (!GetKeyNameTextA((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
344 (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
345 pdidoi->tszName, sizeof(pdidoi->tszName)))
346 return DIERR_OBJECTNOTFOUND;
348 _dump_OBJECTINSTANCEA(pdidoi);
352 static HRESULT WINAPI SysKeyboardWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
353 LPDIDEVICEOBJECTINSTANCEW pdidoi,
359 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
360 if (res != DI_OK) return res;
362 if (!GetKeyNameTextW((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
363 (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
364 pdidoi->tszName, sizeof(pdidoi->tszName)))
365 return DIERR_OBJECTNOTFOUND;
367 _dump_OBJECTINSTANCEW(pdidoi);
371 /******************************************************************************
372 * GetDeviceInfo : get information about a device's identity
374 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceInfo(
375 LPDIRECTINPUTDEVICE8A iface,
376 LPDIDEVICEINSTANCEA pdidi)
378 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
379 TRACE("(this=%p,%p)\n", This, pdidi);
381 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
382 WARN(" dinput3 not supported yet...\n");
386 fill_keyboard_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
391 static HRESULT WINAPI SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
393 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
394 TRACE("(this=%p,%p)\n", This, pdidi);
396 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
397 WARN(" dinput3 not supported yet...\n");
401 fill_keyboard_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
406 static const IDirectInputDevice8AVtbl SysKeyboardAvt =
408 IDirectInputDevice2AImpl_QueryInterface,
409 IDirectInputDevice2AImpl_AddRef,
410 IDirectInputDevice2AImpl_Release,
411 SysKeyboardAImpl_GetCapabilities,
412 IDirectInputDevice2AImpl_EnumObjects,
413 IDirectInputDevice2AImpl_GetProperty,
414 IDirectInputDevice2AImpl_SetProperty,
415 IDirectInputDevice2AImpl_Acquire,
416 IDirectInputDevice2AImpl_Unacquire,
417 SysKeyboardAImpl_GetDeviceState,
418 IDirectInputDevice2AImpl_GetDeviceData,
419 IDirectInputDevice2AImpl_SetDataFormat,
420 IDirectInputDevice2AImpl_SetEventNotification,
421 IDirectInputDevice2AImpl_SetCooperativeLevel,
422 SysKeyboardAImpl_GetObjectInfo,
423 SysKeyboardAImpl_GetDeviceInfo,
424 IDirectInputDevice2AImpl_RunControlPanel,
425 IDirectInputDevice2AImpl_Initialize,
426 IDirectInputDevice2AImpl_CreateEffect,
427 IDirectInputDevice2AImpl_EnumEffects,
428 IDirectInputDevice2AImpl_GetEffectInfo,
429 IDirectInputDevice2AImpl_GetForceFeedbackState,
430 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
431 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
432 IDirectInputDevice2AImpl_Escape,
433 IDirectInputDevice2AImpl_Poll,
434 IDirectInputDevice2AImpl_SendDeviceData,
435 IDirectInputDevice7AImpl_EnumEffectsInFile,
436 IDirectInputDevice7AImpl_WriteEffectToFile,
437 IDirectInputDevice8AImpl_BuildActionMap,
438 IDirectInputDevice8AImpl_SetActionMap,
439 IDirectInputDevice8AImpl_GetImageInfo
442 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
443 # define XCAST(fun) (typeof(SysKeyboardWvt.fun))
445 # define XCAST(fun) (void*)
448 static const IDirectInputDevice8WVtbl SysKeyboardWvt =
450 IDirectInputDevice2WImpl_QueryInterface,
451 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
452 XCAST(Release)IDirectInputDevice2AImpl_Release,
453 XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
454 IDirectInputDevice2WImpl_EnumObjects,
455 XCAST(GetProperty)IDirectInputDevice2AImpl_GetProperty,
456 XCAST(SetProperty)IDirectInputDevice2AImpl_SetProperty,
457 XCAST(Acquire)IDirectInputDevice2AImpl_Acquire,
458 XCAST(Unacquire)IDirectInputDevice2AImpl_Unacquire,
459 XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
460 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
461 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
462 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
463 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
464 SysKeyboardWImpl_GetObjectInfo,
465 SysKeyboardWImpl_GetDeviceInfo,
466 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
467 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
468 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
469 IDirectInputDevice2WImpl_EnumEffects,
470 IDirectInputDevice2WImpl_GetEffectInfo,
471 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
472 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
473 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
474 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
475 XCAST(Poll)IDirectInputDevice2AImpl_Poll,
476 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
477 IDirectInputDevice7WImpl_EnumEffectsInFile,
478 IDirectInputDevice7WImpl_WriteEffectToFile,
479 IDirectInputDevice8WImpl_BuildActionMap,
480 IDirectInputDevice8WImpl_SetActionMap,
481 IDirectInputDevice8WImpl_GetImageInfo