2000-11-05 21:25:02 +01:00
|
|
|
/* DirectInput Keyboard device
|
|
|
|
*
|
|
|
|
* Copyright 1998 Marcus Meissner
|
|
|
|
* Copyright 1998,1999 Lionel Ulmer
|
2002-06-14 02:39:44 +02:00
|
|
|
* Copyright 2000-2001 TransGaming Technologies Inc.
|
2005-03-24 16:08:27 +01:00
|
|
|
* Copyright 2005 Raphael Junqueira
|
2000-11-05 21:25:02 +01:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-11-05 21:25:02 +01:00
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-11-05 21:25:02 +01:00
|
|
|
#include <string.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2001-10-18 23:30:06 +02:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winuser.h"
|
2000-11-05 21:25:02 +01:00
|
|
|
#include "winerror.h"
|
|
|
|
#include "dinput.h"
|
|
|
|
|
|
|
|
#include "dinput_private.h"
|
|
|
|
#include "device_private.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2000-11-05 21:25:02 +01:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dinput);
|
2000-11-05 21:25:02 +01:00
|
|
|
|
2021-10-22 09:33:54 +02:00
|
|
|
static const struct dinput_device_vtbl keyboard_vtbl;
|
2002-05-07 03:49:19 +02:00
|
|
|
|
2021-10-27 09:41:00 +02:00
|
|
|
struct keyboard
|
2000-11-05 21:25:02 +01:00
|
|
|
{
|
2021-10-27 09:40:59 +02:00
|
|
|
struct dinput_device base;
|
2000-11-05 21:25:02 +01:00
|
|
|
};
|
|
|
|
|
2021-10-27 09:41:00 +02:00
|
|
|
static inline struct keyboard *impl_from_IDirectInputDevice8W( IDirectInputDevice8W *iface )
|
2011-01-09 23:43:32 +01:00
|
|
|
{
|
2021-10-27 09:40:59 +02:00
|
|
|
return CONTAINING_RECORD( CONTAINING_RECORD( iface, struct dinput_device, IDirectInputDevice8W_iface ),
|
2021-10-27 09:41:00 +02:00
|
|
|
struct keyboard, base );
|
2011-01-09 23:43:32 +01:00
|
|
|
}
|
|
|
|
|
2020-04-17 20:35:07 +02:00
|
|
|
static BYTE map_dik_code(DWORD scanCode, DWORD vkCode, DWORD subType, DWORD version)
|
2008-08-06 15:04:36 +02:00
|
|
|
{
|
2020-04-17 20:35:07 +02:00
|
|
|
if (!scanCode && version < 0x0800)
|
2018-04-16 16:30:47 +02:00
|
|
|
scanCode = MapVirtualKeyW(vkCode, MAPVK_VK_TO_VSC);
|
2008-08-06 15:04:36 +02:00
|
|
|
|
2018-08-17 10:15:02 +02:00
|
|
|
if (subType == DIDEVTYPEKEYBOARD_JAPAN106)
|
|
|
|
{
|
|
|
|
switch (scanCode)
|
|
|
|
{
|
|
|
|
case 0x0d: /* ^ */
|
|
|
|
scanCode = DIK_CIRCUMFLEX;
|
|
|
|
break;
|
|
|
|
case 0x1a: /* @ */
|
|
|
|
scanCode = DIK_AT;
|
|
|
|
break;
|
|
|
|
case 0x1b: /* [ */
|
|
|
|
scanCode = DIK_LBRACKET;
|
|
|
|
break;
|
|
|
|
case 0x28: /* : */
|
|
|
|
scanCode = DIK_COLON;
|
|
|
|
break;
|
|
|
|
case 0x29: /* Hankaku/Zenkaku */
|
|
|
|
scanCode = DIK_KANJI;
|
|
|
|
break;
|
|
|
|
case 0x2b: /* ] */
|
|
|
|
scanCode = DIK_RBRACKET;
|
|
|
|
break;
|
|
|
|
case 0x73: /* \ */
|
|
|
|
scanCode = DIK_BACKSLASH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 18:07:46 +02:00
|
|
|
if (scanCode & 0x100) scanCode |= 0x80;
|
|
|
|
return (BYTE)scanCode;
|
2008-08-06 15:04:36 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:48:53 +02:00
|
|
|
int dinput_keyboard_hook( IDirectInputDevice8W *iface, WPARAM wparam, LPARAM lparam )
|
2002-05-20 00:26:16 +02:00
|
|
|
{
|
2021-10-27 09:41:00 +02:00
|
|
|
struct keyboard *impl = impl_from_IDirectInputDevice8W( iface );
|
|
|
|
BYTE new_diks, subtype = GET_DIDEVICE_SUBTYPE( impl->base.instance.dwDevType );
|
|
|
|
int dik_code, ret = impl->base.dwCoopLevel & DISCL_EXCLUSIVE;
|
2006-03-18 19:26:37 +01:00
|
|
|
KBDLLHOOKSTRUCT *hook = (KBDLLHOOKSTRUCT *)lparam;
|
2021-10-21 18:07:46 +02:00
|
|
|
DWORD scan_code;
|
2005-03-24 16:08:27 +01:00
|
|
|
|
2007-07-07 07:36:30 +02:00
|
|
|
if (wparam != WM_KEYDOWN && wparam != WM_KEYUP &&
|
|
|
|
wparam != WM_SYSKEYDOWN && wparam != WM_SYSKEYUP)
|
2009-02-21 20:54:37 +01:00
|
|
|
return 0;
|
2007-07-07 07:36:30 +02:00
|
|
|
|
2018-04-16 16:30:53 +02:00
|
|
|
TRACE("(%p) wp %08lx, lp %08lx, vk %02x, scan %02x\n",
|
|
|
|
iface, wparam, lparam, hook->vkCode, hook->scanCode);
|
2002-08-07 01:49:12 +02:00
|
|
|
|
2009-02-27 17:20:08 +01:00
|
|
|
switch (hook->vkCode)
|
|
|
|
{
|
|
|
|
/* R-Shift is special - it is an extended key with separate scan code */
|
|
|
|
case VK_RSHIFT : dik_code = DIK_RSHIFT; break;
|
|
|
|
case VK_PAUSE : dik_code = DIK_PAUSE; break;
|
|
|
|
case VK_NUMLOCK : dik_code = DIK_NUMLOCK; break;
|
|
|
|
case VK_SUBTRACT: dik_code = DIK_SUBTRACT; break;
|
|
|
|
default:
|
2021-10-21 18:07:46 +02:00
|
|
|
scan_code = hook->scanCode & 0xff;
|
|
|
|
if (hook->flags & LLKHF_EXTENDED) scan_code |= 0x100;
|
2021-10-27 09:41:00 +02:00
|
|
|
dik_code = map_dik_code( scan_code, hook->vkCode, subtype, impl->base.dinput->dwVersion );
|
2009-02-27 17:20:08 +01:00
|
|
|
}
|
2006-07-27 16:19:59 +02:00
|
|
|
new_diks = hook->flags & LLKHF_UP ? 0 : 0x80;
|
|
|
|
|
|
|
|
/* returns now if key event already known */
|
2021-10-27 09:41:00 +02:00
|
|
|
if (new_diks == impl->base.device_state[dik_code]) return ret;
|
2005-03-24 16:08:27 +01:00
|
|
|
|
2021-10-27 09:41:00 +02:00
|
|
|
impl->base.device_state[dik_code] = new_diks;
|
|
|
|
TRACE( " setting key %02x to %02x\n", dik_code, impl->base.device_state[dik_code] );
|
2009-02-21 20:54:37 +01:00
|
|
|
|
2021-10-27 09:41:00 +02:00
|
|
|
EnterCriticalSection( &impl->base.crit );
|
|
|
|
queue_event( iface, DIDFT_MAKEINSTANCE( dik_code ) | DIDFT_PSHBUTTON, new_diks,
|
|
|
|
GetCurrentTime(), impl->base.dinput->evsequence++ );
|
|
|
|
if (impl->base.hEvent) SetEvent( impl->base.hEvent );
|
|
|
|
LeaveCriticalSection( &impl->base.crit );
|
2009-02-21 20:54:37 +01:00
|
|
|
|
|
|
|
return ret;
|
2002-05-20 00:26:16 +02:00
|
|
|
}
|
2002-05-07 03:49:19 +02:00
|
|
|
|
2018-08-17 10:14:54 +02:00
|
|
|
static DWORD get_keyboard_subtype(void)
|
|
|
|
{
|
|
|
|
DWORD kbd_type, kbd_subtype, dev_subtype;
|
|
|
|
kbd_type = GetKeyboardType(0);
|
|
|
|
kbd_subtype = GetKeyboardType(1);
|
|
|
|
|
|
|
|
if (kbd_type == 4 || (kbd_type == 7 && kbd_subtype == 0))
|
|
|
|
dev_subtype = DIDEVTYPEKEYBOARD_PCENH;
|
|
|
|
else if (kbd_type == 7 && kbd_subtype == 2)
|
|
|
|
dev_subtype = DIDEVTYPEKEYBOARD_JAPAN106;
|
|
|
|
else {
|
|
|
|
FIXME("Unknown keyboard type=%u, subtype=%u\n", kbd_type, kbd_subtype);
|
|
|
|
dev_subtype = DIDEVTYPEKEYBOARD_PCENH;
|
|
|
|
}
|
|
|
|
return dev_subtype;
|
|
|
|
}
|
|
|
|
|
2021-11-16 08:23:42 +01:00
|
|
|
HRESULT keyboard_enum_device( DWORD type, DWORD flags, DIDEVICEINSTANCEW *instance, DWORD version )
|
2000-11-05 21:25:02 +01:00
|
|
|
{
|
2021-10-22 09:33:53 +02:00
|
|
|
BYTE subtype = get_keyboard_subtype();
|
|
|
|
DWORD size;
|
|
|
|
|
2021-11-16 08:23:42 +01:00
|
|
|
TRACE( "type %#x, flags %#x, instance %p, version %#04x\n", type, flags, instance, version );
|
2021-10-22 09:33:53 +02:00
|
|
|
|
|
|
|
size = instance->dwSize;
|
|
|
|
memset( instance, 0, size );
|
|
|
|
instance->dwSize = size;
|
|
|
|
instance->guidInstance = GUID_SysKeyboard;
|
|
|
|
instance->guidProduct = GUID_SysKeyboard;
|
|
|
|
if (version >= 0x0800) instance->dwDevType = DI8DEVTYPE_KEYBOARD | (subtype << 8);
|
|
|
|
else instance->dwDevType = DIDEVTYPE_KEYBOARD | (subtype << 8);
|
|
|
|
MultiByteToWideChar( CP_ACP, 0, "Keyboard", -1, instance->tszInstanceName, MAX_PATH );
|
|
|
|
MultiByteToWideChar( CP_ACP, 0, "Wine Keyboard", -1, instance->tszProductName, MAX_PATH );
|
2000-11-05 21:25:02 +01:00
|
|
|
|
2021-10-22 09:33:53 +02:00
|
|
|
return DI_OK;
|
2000-11-05 21:25:02 +01:00
|
|
|
}
|
|
|
|
|
2021-10-26 09:17:29 +02:00
|
|
|
HRESULT keyboard_create_device( IDirectInputImpl *dinput, const GUID *guid, IDirectInputDevice8W **out )
|
2000-11-05 21:25:02 +01:00
|
|
|
{
|
2021-10-27 09:41:00 +02:00
|
|
|
struct keyboard *impl;
|
2021-05-28 11:41:27 +02:00
|
|
|
HRESULT hr;
|
2005-03-24 16:08:27 +01:00
|
|
|
|
2021-10-22 09:33:53 +02:00
|
|
|
TRACE( "dinput %p, guid %s, out %p\n", dinput, debugstr_guid( guid ), out );
|
|
|
|
|
|
|
|
*out = NULL;
|
|
|
|
if (!IsEqualGUID( &GUID_SysKeyboard, guid )) return DIERR_DEVICENOTREG;
|
|
|
|
|
2021-10-27 09:41:00 +02:00
|
|
|
if (FAILED(hr = dinput_device_alloc( sizeof(struct keyboard), &keyboard_vtbl, guid, dinput, (void **)&impl )))
|
2021-05-28 11:41:27 +02:00
|
|
|
return hr;
|
2021-10-27 09:41:00 +02:00
|
|
|
impl->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": struct keyboard*->base.crit");
|
2021-05-28 11:41:27 +02:00
|
|
|
|
2021-11-16 08:23:42 +01:00
|
|
|
keyboard_enum_device( 0, 0, &impl->base.instance, dinput->dwVersion );
|
2021-10-22 09:33:53 +02:00
|
|
|
impl->base.caps.dwDevType = impl->base.instance.dwDevType;
|
|
|
|
impl->base.caps.dwFirmwareRevision = 100;
|
|
|
|
impl->base.caps.dwHardwareRevision = 100;
|
2000-11-05 21:25:02 +01:00
|
|
|
|
2021-10-27 09:40:59 +02:00
|
|
|
if (FAILED(hr = dinput_device_init( &impl->base.IDirectInputDevice8W_iface )))
|
2006-12-22 18:54:29 +01:00
|
|
|
{
|
2021-10-22 09:33:53 +02:00
|
|
|
IDirectInputDevice_Release( &impl->base.IDirectInputDevice8W_iface );
|
2021-10-18 10:21:55 +02:00
|
|
|
return hr;
|
2006-12-22 18:54:29 +01:00
|
|
|
}
|
|
|
|
|
2021-10-22 09:33:53 +02:00
|
|
|
*out = &impl->base.IDirectInputDevice8W_iface;
|
2021-05-28 11:41:26 +02:00
|
|
|
return DI_OK;
|
2000-11-05 21:25:02 +01:00
|
|
|
}
|
|
|
|
|
2021-10-22 09:33:54 +02:00
|
|
|
static HRESULT keyboard_poll( IDirectInputDevice8W *iface )
|
2021-10-20 11:29:30 +02:00
|
|
|
{
|
|
|
|
check_dinput_events();
|
|
|
|
return DI_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-22 09:33:54 +02:00
|
|
|
static HRESULT keyboard_acquire( IDirectInputDevice8W *iface )
|
2015-04-11 10:06:05 +02:00
|
|
|
{
|
2021-10-13 09:21:17 +02:00
|
|
|
return DI_OK;
|
|
|
|
}
|
2015-04-11 10:06:05 +02:00
|
|
|
|
2021-10-22 09:33:54 +02:00
|
|
|
static HRESULT keyboard_unacquire( IDirectInputDevice8W *iface )
|
2021-10-13 09:21:17 +02:00
|
|
|
{
|
2021-10-27 09:41:00 +02:00
|
|
|
struct keyboard *impl = impl_from_IDirectInputDevice8W( iface );
|
|
|
|
memset( impl->base.device_state, 0, sizeof(impl->base.device_state) );
|
2021-10-13 09:21:17 +02:00
|
|
|
return DI_OK;
|
2015-04-11 10:06:05 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 10:21:55 +02:00
|
|
|
static BOOL try_enum_object( const DIPROPHEADER *filter, DWORD flags, LPDIENUMDEVICEOBJECTSCALLBACKW callback,
|
|
|
|
DIDEVICEOBJECTINSTANCEW *instance, void *data )
|
|
|
|
{
|
|
|
|
if (flags != DIDFT_ALL && !(flags & DIDFT_GETTYPE( instance->dwType ))) return DIENUM_CONTINUE;
|
|
|
|
|
|
|
|
switch (filter->dwHow)
|
|
|
|
{
|
|
|
|
case DIPH_DEVICE:
|
|
|
|
return callback( instance, data );
|
|
|
|
case DIPH_BYOFFSET:
|
|
|
|
if (filter->dwObj != instance->dwOfs) return DIENUM_CONTINUE;
|
|
|
|
return callback( instance, data );
|
|
|
|
case DIPH_BYID:
|
|
|
|
if ((filter->dwObj & 0x00ffffff) != (instance->dwType & 0x00ffffff)) return DIENUM_CONTINUE;
|
|
|
|
return callback( instance, data );
|
|
|
|
}
|
|
|
|
|
|
|
|
return DIENUM_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-10-22 09:33:54 +02:00
|
|
|
static HRESULT keyboard_enum_objects( IDirectInputDevice8W *iface, const DIPROPHEADER *filter,
|
|
|
|
DWORD flags, LPDIENUMDEVICEOBJECTSCALLBACKW callback, void *context )
|
2021-10-18 10:21:55 +02:00
|
|
|
{
|
2021-10-27 09:41:00 +02:00
|
|
|
struct keyboard *impl = impl_from_IDirectInputDevice8W( iface );
|
2021-10-18 10:21:55 +02:00
|
|
|
BYTE subtype = GET_DIDEVICE_SUBTYPE( impl->base.instance.dwDevType );
|
|
|
|
DIDEVICEOBJECTINSTANCEW instance =
|
|
|
|
{
|
|
|
|
.dwSize = sizeof(DIDEVICEOBJECTINSTANCEW),
|
|
|
|
.guidType = GUID_Key,
|
|
|
|
.dwOfs = DIK_ESCAPE,
|
|
|
|
.dwType = DIDFT_PSHBUTTON | DIDFT_MAKEINSTANCE( DIK_ESCAPE ),
|
|
|
|
};
|
|
|
|
DWORD i, dik;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
for (i = 0; i < 512; ++i)
|
|
|
|
{
|
|
|
|
if (!GetKeyNameTextW( i << 16, instance.tszName, ARRAY_SIZE(instance.tszName) )) continue;
|
|
|
|
if (!(dik = map_dik_code( i, 0, subtype, impl->base.dinput->dwVersion ))) continue;
|
|
|
|
instance.dwOfs = dik;
|
|
|
|
instance.dwType = DIDFT_PSHBUTTON | DIDFT_MAKEINSTANCE( dik );
|
2021-10-20 11:29:29 +02:00
|
|
|
ret = try_enum_object( filter, flags, callback, &instance, context );
|
2021-10-18 10:21:55 +02:00
|
|
|
if (ret != DIENUM_CONTINUE) return DIENUM_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DIENUM_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-10-22 09:33:54 +02:00
|
|
|
static HRESULT keyboard_get_property( IDirectInputDevice8W *iface, DWORD property,
|
2021-11-18 09:53:17 +01:00
|
|
|
DIPROPHEADER *header, const DIDEVICEOBJECTINSTANCEW *instance )
|
2021-10-19 09:20:02 +02:00
|
|
|
{
|
|
|
|
switch (property)
|
|
|
|
{
|
|
|
|
case (DWORD_PTR)DIPROP_KEYNAME:
|
|
|
|
{
|
|
|
|
DIPROPSTRING *value = (DIPROPSTRING *)header;
|
|
|
|
lstrcpynW( value->wsz, instance->tszName, ARRAY_SIZE(value->wsz) );
|
|
|
|
return DI_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DIERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2021-10-22 09:33:54 +02:00
|
|
|
static const struct dinput_device_vtbl keyboard_vtbl =
|
2021-10-13 09:21:17 +02:00
|
|
|
{
|
2021-10-20 11:29:33 +02:00
|
|
|
NULL,
|
2021-10-22 09:33:54 +02:00
|
|
|
keyboard_poll,
|
2021-10-13 09:21:17 +02:00
|
|
|
NULL,
|
2021-10-22 09:33:54 +02:00
|
|
|
keyboard_acquire,
|
|
|
|
keyboard_unacquire,
|
|
|
|
keyboard_enum_objects,
|
|
|
|
keyboard_get_property,
|
2021-10-21 09:53:43 +02:00
|
|
|
NULL,
|
2021-10-21 09:53:45 +02:00
|
|
|
NULL,
|
2021-10-21 09:53:46 +02:00
|
|
|
NULL,
|
2021-10-21 09:53:47 +02:00
|
|
|
NULL,
|
2021-11-17 11:51:49 +01:00
|
|
|
NULL,
|
2021-10-13 09:21:17 +02:00
|
|
|
};
|