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;
51 IDirectInputImpl* dinput;
54 static SysKeyboardImpl* current_lock = NULL;
55 /* Today's acquired device
56 * FIXME: currently this can be only one.
57 * Maybe this should be a linked list or st.
58 * I don't know what the rules are for multiple acquired keyboards,
59 * but 'DI_LOSTFOCUS' and 'DI_UNACQUIRED' exist for a reason.
62 static BYTE DInputKeyState[WINE_DINPUT_KEYBOARD_MAX_KEYS]; /* array for 'GetDeviceState' */
64 static LRESULT CALLBACK KeyboardCallback( int code, WPARAM wparam, LPARAM lparam )
66 SysKeyboardImpl *This = (SysKeyboardImpl *)current_lock;
68 KBDLLHOOKSTRUCT *hook = (KBDLLHOOKSTRUCT *)lparam;
71 TRACE("(%d,%d,%ld)\n", code, wparam, lparam);
73 /* returns now if not HC_ACTION */
74 if (code != HC_ACTION) return CallNextHookEx(0, code, wparam, lparam);
76 dik_code = hook->scanCode & 0xff;
77 if (hook->flags & LLKHF_EXTENDED) dik_code |= 0x80;
79 new_diks = hook->flags & LLKHF_UP ? 0 : 0x80;
81 /* returns now if key event already known */
82 if (new_diks == DInputKeyState[dik_code])
83 return CallNextHookEx(0, code, wparam, lparam);
85 DInputKeyState[dik_code] = new_diks;
86 TRACE(" setting %02X to %02X\n", dik_code, DInputKeyState[dik_code]);
88 dik_code = id_to_offset(&This->base.data_format, DIDFT_MAKEINSTANCE(dik_code) | DIDFT_PSHBUTTON);
89 EnterCriticalSection(&This->base.crit);
90 queue_event((LPDIRECTINPUTDEVICE8A)This, dik_code, new_diks, hook->time, This->dinput->evsequence++);
91 LeaveCriticalSection(&This->base.crit);
93 return CallNextHookEx(0, code, wparam, lparam);
96 static const GUID DInput_Wine_Keyboard_GUID = { /* 0ab8648a-7735-11d2-8c73-71df54a96441 */
100 {0x8c, 0x73, 0x71, 0xdf, 0x54, 0xa9, 0x64, 0x41}
103 static void fill_keyboard_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
105 DIDEVICEINSTANCEA ddi;
107 dwSize = lpddi->dwSize;
109 TRACE("%d %p\n", dwSize, lpddi);
111 memset(lpddi, 0, dwSize);
112 memset(&ddi, 0, sizeof(ddi));
115 ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
116 ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
117 if (version >= 0x0800)
118 ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
120 ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
121 strcpy(ddi.tszInstanceName, "Keyboard");
122 strcpy(ddi.tszProductName, "Wine Keyboard");
124 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
127 static void fill_keyboard_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
129 DIDEVICEINSTANCEW ddi;
131 dwSize = lpddi->dwSize;
133 TRACE("%d %p\n", dwSize, lpddi);
135 memset(lpddi, 0, dwSize);
136 memset(&ddi, 0, sizeof(ddi));
139 ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
140 ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
141 if (version >= 0x0800)
142 ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
144 ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
145 MultiByteToWideChar(CP_ACP, 0, "Keyboard", -1, ddi.tszInstanceName, MAX_PATH);
146 MultiByteToWideChar(CP_ACP, 0, "Wine Keyboard", -1, ddi.tszProductName, MAX_PATH);
148 memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
151 static BOOL keyboarddev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
156 if ((dwDevType == 0) ||
157 ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
158 (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
159 TRACE("Enumerating the Keyboard device\n");
161 fill_keyboard_dideviceinstanceA(lpddi, version);
169 static BOOL keyboarddev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
174 if ((dwDevType == 0) ||
175 ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
176 (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
177 TRACE("Enumerating the Keyboard device\n");
179 fill_keyboard_dideviceinstanceW(lpddi, version);
187 static SysKeyboardImpl *alloc_device(REFGUID rguid, const void *kvt, IDirectInputImpl *dinput)
189 SysKeyboardImpl* newDevice;
190 LPDIDATAFORMAT df = NULL;
193 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
194 newDevice->base.lpVtbl = kvt;
195 newDevice->base.ref = 1;
196 memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
197 newDevice->dinput = dinput;
198 InitializeCriticalSection(&newDevice->base.crit);
200 /* Create copy of default data format */
201 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIKeyboard.dwSize))) goto failed;
202 memcpy(df, &c_dfDIKeyboard, c_dfDIKeyboard.dwSize);
203 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
205 for (i = 0; i < df->dwNumObjs; i++)
209 if (!GetKeyNameTextA(((i & 0x7f) << 16) | ((i & 0x80) << 17), buf, sizeof(buf)))
212 memcpy(&df->rgodf[idx], &c_dfDIKeyboard.rgodf[i], df->dwObjSize);
213 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
217 newDevice->base.data_format.wine_df = df;
218 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->dinput);
222 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
223 HeapFree(GetProcessHeap(), 0, df);
224 HeapFree(GetProcessHeap(), 0, newDevice);
229 static HRESULT keyboarddev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
231 if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) || /* Generic Keyboard */
232 (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
233 if ((riid == NULL) ||
234 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
235 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
236 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
237 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
238 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
239 TRACE("Creating a Keyboard device (%p)\n", *pdev);
240 if (!*pdev) return DIERR_OUTOFMEMORY;
243 return DIERR_NOINTERFACE;
245 return DIERR_DEVICENOTREG;
248 static HRESULT keyboarddev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
250 if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) || /* Generic Keyboard */
251 (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
252 if ((riid == NULL) ||
253 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
254 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
255 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
256 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
257 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysKeyboardWvt, dinput);
258 TRACE("Creating a Keyboard device (%p)\n", *pdev);
259 if (!*pdev) return DIERR_OUTOFMEMORY;
262 return DIERR_NOINTERFACE;
264 return DIERR_DEVICENOTREG;
267 const struct dinput_device keyboard_device = {
268 "Wine keyboard driver",
269 keyboarddev_enum_deviceA,
270 keyboarddev_enum_deviceW,
271 keyboarddev_create_deviceA,
272 keyboarddev_create_deviceW
275 static ULONG WINAPI SysKeyboardAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
277 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
280 ref = InterlockedDecrement(&This->base.ref);
283 set_dinput_hook(WH_KEYBOARD_LL, NULL);
285 HeapFree(GetProcessHeap(), 0, This->base.data_queue);
287 /* Free data format */
288 HeapFree(GetProcessHeap(), 0, This->base.data_format.wine_df->rgodf);
289 HeapFree(GetProcessHeap(), 0, This->base.data_format.wine_df);
290 release_DataFormat(&This->base.data_format);
292 IDirectInput_Release((LPDIRECTINPUTDEVICE8A)This->dinput);
293 DeleteCriticalSection(&This->base.crit);
294 HeapFree(GetProcessHeap(), 0, This);
299 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
300 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
303 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
304 TRACE("(%p)->(%d,%p)\n", This, len, ptr);
306 if (!This->base.acquired) return DIERR_NOTACQUIRED;
308 if (len != WINE_DINPUT_KEYBOARD_MAX_KEYS)
309 return DIERR_INVALIDPARAM;
311 EnterCriticalSection(&This->base.crit);
313 if (TRACE_ON(dinput)) {
315 for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
316 if (DInputKeyState[i] != 0x00) {
317 TRACE(" - %02X: %02x\n", i, DInputKeyState[i]);
322 memcpy(ptr, DInputKeyState, WINE_DINPUT_KEYBOARD_MAX_KEYS);
323 LeaveCriticalSection(&This->base.crit);
328 static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface);
330 static HRESULT WINAPI SysKeyboardAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
332 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
335 TRACE("(%p)\n",This);
337 if ((res = IDirectInputDevice2AImpl_Acquire(iface)) != DI_OK) return res;
339 if (current_lock != NULL) {
340 FIXME("Not more than one keyboard can be acquired at the same time.\n");
341 SysKeyboardAImpl_Unacquire((LPDIRECTINPUTDEVICE8A)current_lock);
345 set_dinput_hook(WH_KEYBOARD_LL, KeyboardCallback);
350 static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
352 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
355 TRACE("(this=%p)\n",This);
357 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
359 set_dinput_hook(WH_KEYBOARD_LL, NULL);
362 if (current_lock == This)
365 ERR("this != current_lock\n");
370 /******************************************************************************
371 * GetCapabilities : get the device capablitites
373 static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
374 LPDIRECTINPUTDEVICE8A iface,
375 LPDIDEVCAPS lpDIDevCaps)
377 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
380 TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
382 if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
383 WARN("invalid parameter\n");
384 return DIERR_INVALIDPARAM;
387 devcaps.dwSize = lpDIDevCaps->dwSize;
388 devcaps.dwFlags = DIDC_ATTACHED;
389 if (This->dinput->dwVersion >= 0x0800)
390 devcaps.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
392 devcaps.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
394 devcaps.dwButtons = This->base.data_format.wine_df->dwNumObjs;
396 devcaps.dwFFSamplePeriod = 0;
397 devcaps.dwFFMinTimeResolution = 0;
398 devcaps.dwFirmwareRevision = 100;
399 devcaps.dwHardwareRevision = 100;
400 devcaps.dwFFDriverVersion = 0;
402 memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
407 /******************************************************************************
408 * GetObjectInfo : get information about a device object such as a button
411 static HRESULT WINAPI
412 SysKeyboardAImpl_GetObjectInfo(
413 LPDIRECTINPUTDEVICE8A iface,
414 LPDIDEVICEOBJECTINSTANCEA pdidoi,
420 res = IDirectInputDevice2AImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
421 if (res != DI_OK) return res;
423 if (!GetKeyNameTextA((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
424 (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
425 pdidoi->tszName, sizeof(pdidoi->tszName)))
426 return DIERR_OBJECTNOTFOUND;
428 _dump_OBJECTINSTANCEA(pdidoi);
432 static HRESULT WINAPI SysKeyboardWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
433 LPDIDEVICEOBJECTINSTANCEW pdidoi,
439 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
440 if (res != DI_OK) return res;
442 if (!GetKeyNameTextW((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
443 (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
444 pdidoi->tszName, sizeof(pdidoi->tszName)))
445 return DIERR_OBJECTNOTFOUND;
447 _dump_OBJECTINSTANCEW(pdidoi);
451 /******************************************************************************
452 * GetDeviceInfo : get information about a device's identity
454 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceInfo(
455 LPDIRECTINPUTDEVICE8A iface,
456 LPDIDEVICEINSTANCEA pdidi)
458 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
459 TRACE("(this=%p,%p)\n", This, pdidi);
461 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
462 WARN(" dinput3 not supported yet...\n");
466 fill_keyboard_dideviceinstanceA(pdidi, This->dinput->dwVersion);
471 static HRESULT WINAPI SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi)
473 SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
474 TRACE("(this=%p,%p)\n", This, pdidi);
476 if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
477 WARN(" dinput3 not supported yet...\n");
481 fill_keyboard_dideviceinstanceW(pdidi, This->dinput->dwVersion);
486 static const IDirectInputDevice8AVtbl SysKeyboardAvt =
488 IDirectInputDevice2AImpl_QueryInterface,
489 IDirectInputDevice2AImpl_AddRef,
490 SysKeyboardAImpl_Release,
491 SysKeyboardAImpl_GetCapabilities,
492 IDirectInputDevice2AImpl_EnumObjects,
493 IDirectInputDevice2AImpl_GetProperty,
494 IDirectInputDevice2AImpl_SetProperty,
495 SysKeyboardAImpl_Acquire,
496 SysKeyboardAImpl_Unacquire,
497 SysKeyboardAImpl_GetDeviceState,
498 IDirectInputDevice2AImpl_GetDeviceData,
499 IDirectInputDevice2AImpl_SetDataFormat,
500 IDirectInputDevice2AImpl_SetEventNotification,
501 IDirectInputDevice2AImpl_SetCooperativeLevel,
502 SysKeyboardAImpl_GetObjectInfo,
503 SysKeyboardAImpl_GetDeviceInfo,
504 IDirectInputDevice2AImpl_RunControlPanel,
505 IDirectInputDevice2AImpl_Initialize,
506 IDirectInputDevice2AImpl_CreateEffect,
507 IDirectInputDevice2AImpl_EnumEffects,
508 IDirectInputDevice2AImpl_GetEffectInfo,
509 IDirectInputDevice2AImpl_GetForceFeedbackState,
510 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
511 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
512 IDirectInputDevice2AImpl_Escape,
513 IDirectInputDevice2AImpl_Poll,
514 IDirectInputDevice2AImpl_SendDeviceData,
515 IDirectInputDevice7AImpl_EnumEffectsInFile,
516 IDirectInputDevice7AImpl_WriteEffectToFile,
517 IDirectInputDevice8AImpl_BuildActionMap,
518 IDirectInputDevice8AImpl_SetActionMap,
519 IDirectInputDevice8AImpl_GetImageInfo
522 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
523 # define XCAST(fun) (typeof(SysKeyboardWvt.fun))
525 # define XCAST(fun) (void*)
528 static const IDirectInputDevice8WVtbl SysKeyboardWvt =
530 IDirectInputDevice2WImpl_QueryInterface,
531 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
532 XCAST(Release)SysKeyboardAImpl_Release,
533 XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
534 IDirectInputDevice2WImpl_EnumObjects,
535 XCAST(GetProperty)IDirectInputDevice2AImpl_GetProperty,
536 XCAST(SetProperty)IDirectInputDevice2AImpl_SetProperty,
537 XCAST(Acquire)SysKeyboardAImpl_Acquire,
538 XCAST(Unacquire)SysKeyboardAImpl_Unacquire,
539 XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
540 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
541 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
542 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
543 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
544 SysKeyboardWImpl_GetObjectInfo,
545 SysKeyboardWImpl_GetDeviceInfo,
546 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
547 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
548 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
549 IDirectInputDevice2WImpl_EnumEffects,
550 IDirectInputDevice2WImpl_GetEffectInfo,
551 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
552 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
553 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
554 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
555 XCAST(Poll)IDirectInputDevice2AImpl_Poll,
556 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
557 IDirectInputDevice7WImpl_EnumEffectsInFile,
558 IDirectInputDevice7WImpl_WriteEffectToFile,
559 IDirectInputDevice8WImpl_BuildActionMap,
560 IDirectInputDevice8WImpl_SetActionMap,
561 IDirectInputDevice8WImpl_GetImageInfo