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 capabilities
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,
365 sizeof(pdidoi
->tszName
)/sizeof(pdidoi
->tszName
[0])))
366 return DIERR_OBJECTNOTFOUND
;
368 _dump_OBJECTINSTANCEW(pdidoi
);
372 /******************************************************************************
373 * GetDeviceInfo : get information about a device's identity
375 static HRESULT WINAPI
SysKeyboardAImpl_GetDeviceInfo(
376 LPDIRECTINPUTDEVICE8A iface
,
377 LPDIDEVICEINSTANCEA pdidi
)
379 SysKeyboardImpl
*This
= (SysKeyboardImpl
*)iface
;
380 TRACE("(this=%p,%p)\n", This
, pdidi
);
382 if (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
)) {
383 WARN(" dinput3 not supported yet...\n");
387 fill_keyboard_dideviceinstanceA(pdidi
, This
->base
.dinput
->dwVersion
);
392 static HRESULT WINAPI
SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface
, LPDIDEVICEINSTANCEW pdidi
)
394 SysKeyboardImpl
*This
= (SysKeyboardImpl
*)iface
;
395 TRACE("(this=%p,%p)\n", This
, pdidi
);
397 if (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
)) {
398 WARN(" dinput3 not supported yet...\n");
402 fill_keyboard_dideviceinstanceW(pdidi
, This
->base
.dinput
->dwVersion
);
407 static const IDirectInputDevice8AVtbl SysKeyboardAvt
=
409 IDirectInputDevice2AImpl_QueryInterface
,
410 IDirectInputDevice2AImpl_AddRef
,
411 IDirectInputDevice2AImpl_Release
,
412 SysKeyboardAImpl_GetCapabilities
,
413 IDirectInputDevice2AImpl_EnumObjects
,
414 IDirectInputDevice2AImpl_GetProperty
,
415 IDirectInputDevice2AImpl_SetProperty
,
416 IDirectInputDevice2AImpl_Acquire
,
417 IDirectInputDevice2AImpl_Unacquire
,
418 SysKeyboardAImpl_GetDeviceState
,
419 IDirectInputDevice2AImpl_GetDeviceData
,
420 IDirectInputDevice2AImpl_SetDataFormat
,
421 IDirectInputDevice2AImpl_SetEventNotification
,
422 IDirectInputDevice2AImpl_SetCooperativeLevel
,
423 SysKeyboardAImpl_GetObjectInfo
,
424 SysKeyboardAImpl_GetDeviceInfo
,
425 IDirectInputDevice2AImpl_RunControlPanel
,
426 IDirectInputDevice2AImpl_Initialize
,
427 IDirectInputDevice2AImpl_CreateEffect
,
428 IDirectInputDevice2AImpl_EnumEffects
,
429 IDirectInputDevice2AImpl_GetEffectInfo
,
430 IDirectInputDevice2AImpl_GetForceFeedbackState
,
431 IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
432 IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
433 IDirectInputDevice2AImpl_Escape
,
434 IDirectInputDevice2AImpl_Poll
,
435 IDirectInputDevice2AImpl_SendDeviceData
,
436 IDirectInputDevice7AImpl_EnumEffectsInFile
,
437 IDirectInputDevice7AImpl_WriteEffectToFile
,
438 IDirectInputDevice8AImpl_BuildActionMap
,
439 IDirectInputDevice8AImpl_SetActionMap
,
440 IDirectInputDevice8AImpl_GetImageInfo
443 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
444 # define XCAST(fun) (typeof(SysKeyboardWvt.fun))
446 # define XCAST(fun) (void*)
449 static const IDirectInputDevice8WVtbl SysKeyboardWvt
=
451 IDirectInputDevice2WImpl_QueryInterface
,
452 XCAST(AddRef
)IDirectInputDevice2AImpl_AddRef
,
453 XCAST(Release
)IDirectInputDevice2AImpl_Release
,
454 XCAST(GetCapabilities
)SysKeyboardAImpl_GetCapabilities
,
455 IDirectInputDevice2WImpl_EnumObjects
,
456 XCAST(GetProperty
)IDirectInputDevice2AImpl_GetProperty
,
457 XCAST(SetProperty
)IDirectInputDevice2AImpl_SetProperty
,
458 XCAST(Acquire
)IDirectInputDevice2AImpl_Acquire
,
459 XCAST(Unacquire
)IDirectInputDevice2AImpl_Unacquire
,
460 XCAST(GetDeviceState
)SysKeyboardAImpl_GetDeviceState
,
461 XCAST(GetDeviceData
)IDirectInputDevice2AImpl_GetDeviceData
,
462 XCAST(SetDataFormat
)IDirectInputDevice2AImpl_SetDataFormat
,
463 XCAST(SetEventNotification
)IDirectInputDevice2AImpl_SetEventNotification
,
464 XCAST(SetCooperativeLevel
)IDirectInputDevice2AImpl_SetCooperativeLevel
,
465 SysKeyboardWImpl_GetObjectInfo
,
466 SysKeyboardWImpl_GetDeviceInfo
,
467 XCAST(RunControlPanel
)IDirectInputDevice2AImpl_RunControlPanel
,
468 XCAST(Initialize
)IDirectInputDevice2AImpl_Initialize
,
469 XCAST(CreateEffect
)IDirectInputDevice2AImpl_CreateEffect
,
470 IDirectInputDevice2WImpl_EnumEffects
,
471 IDirectInputDevice2WImpl_GetEffectInfo
,
472 XCAST(GetForceFeedbackState
)IDirectInputDevice2AImpl_GetForceFeedbackState
,
473 XCAST(SendForceFeedbackCommand
)IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
474 XCAST(EnumCreatedEffectObjects
)IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
475 XCAST(Escape
)IDirectInputDevice2AImpl_Escape
,
476 XCAST(Poll
)IDirectInputDevice2AImpl_Poll
,
477 XCAST(SendDeviceData
)IDirectInputDevice2AImpl_SendDeviceData
,
478 IDirectInputDevice7WImpl_EnumEffectsInFile
,
479 IDirectInputDevice7WImpl_WriteEffectToFile
,
480 IDirectInputDevice8WImpl_BuildActionMap
,
481 IDirectInputDevice8WImpl_SetActionMap
,
482 IDirectInputDevice8WImpl_GetImageInfo