Skip to content

Commit ce152dc

Browse files
authored
Merge pull request #5 from Nemirtingas/feature/xinput-1-4-win11
Update XInput1_4 from KB5086672
2 parents ab0fb43 + 96b2edb commit ce152dc

3 files changed

Lines changed: 183 additions & 88 deletions

File tree

src/OpenXinput.cpp

Lines changed: 122 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ struct DeviceInfo_t {
3535
SIZE_T field_1C;
3636
WORD XUSBVersion;
3737
WORD field_22;
38-
OPENXINPUT_STATE_FULL DeviceState;
38+
XINPUT_STATE DeviceState;
3939
XINPUT_VIBRATION DeviceVibration;
40+
XINPUT_SYSTEM_BUTTONS SystemButtons;
4041
WORD field_38;
4142
WORD vendorId;
4243
WORD productId;
@@ -255,7 +256,7 @@ struct GamepadBatteryInformation0102
255256

256257
struct GetStateApiParam_t
257258
{
258-
OPENXINPUT_STATE_FULL* pState;
259+
XINPUT_STATE* pState;
259260
};
260261

261262
struct SetStateApiParam_t
@@ -308,6 +309,11 @@ struct GetBaseBusInformationApiParam_t
308309
XINPUT_BASE_BUS_INFORMATION* pBaseBusInformation;
309310
};
310311

312+
struct GetSystemButtonsApiParam_t
313+
{
314+
XINPUT_SYSTEM_BUTTONS* pSystemButtons;
315+
};
316+
311317
struct GetDeviceUSBIdsApiParam_t
312318
{
313319
WORD* pVendorId;
@@ -495,10 +501,12 @@ namespace DeviceInfo {
495501
namespace Enabled {
496502
HRESULT GetState(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
497503
HRESULT SetVibration(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
504+
HRESULT GetSystemButtons(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
498505
}
499506
namespace Disabled {
500507
HRESULT GetState(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
501508
HRESULT SetVibration(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
509+
HRESULT GetSystemButtons(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
502510
}
503511

504512
DeviceInfo_t* Create(HANDLE hDevice, LPCWSTR lpDevicePath);
@@ -523,6 +531,7 @@ HRESULT GetDeviceUSBIds(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
523531

524532
static HRESULT(*g_pfnGetStateDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
525533
static HRESULT(*g_pfnSetVibrationDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
534+
static HRESULT(*g_pfnGetSystemButtonsDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
526535

527536
}
528537
}
@@ -829,16 +838,14 @@ HRESULT GrowList(DWORD newSize)
829838

830839
void CopyGamepadStateToDeviceInfo(DeviceInfo_t* pDevice, GamepadState0100 *pGamepadState)
831840
{
832-
pDevice->DeviceState.XinputState.dwPacketNumber = pGamepadState->dwPacketNumber;
833-
pDevice->DeviceState.XinputState.Gamepad.bLeftTrigger = pGamepadState->bLeftTrigger;
834-
pDevice->DeviceState.XinputState.Gamepad.bRightTrigger = pGamepadState->bRightTrigger;
835-
pDevice->DeviceState.XinputState.Gamepad.sThumbLX = pGamepadState->sThumbLX;
836-
pDevice->DeviceState.XinputState.Gamepad.sThumbLY = pGamepadState->sThumbLY;
837-
pDevice->DeviceState.XinputState.Gamepad.sThumbRX = pGamepadState->sThumbRX;
838-
pDevice->DeviceState.XinputState.Gamepad.sThumbRY = pGamepadState->sThumbRY;
839-
pDevice->DeviceState.XinputState.Gamepad.wButtons = pGamepadState->wButtons;
840-
841-
pDevice->DeviceState.GamepadExtras.dwExtraButtons = 0;
841+
pDevice->DeviceState.dwPacketNumber = pGamepadState->dwPacketNumber;
842+
pDevice->DeviceState.Gamepad.bLeftTrigger = pGamepadState->bLeftTrigger;
843+
pDevice->DeviceState.Gamepad.bRightTrigger = pGamepadState->bRightTrigger;
844+
pDevice->DeviceState.Gamepad.sThumbLX = pGamepadState->sThumbLX;
845+
pDevice->DeviceState.Gamepad.sThumbLY = pGamepadState->sThumbLY;
846+
pDevice->DeviceState.Gamepad.sThumbRX = pGamepadState->sThumbRX;
847+
pDevice->DeviceState.Gamepad.sThumbRY = pGamepadState->sThumbRY;
848+
pDevice->DeviceState.Gamepad.wButtons = pGamepadState->wButtons;
842849

843850
if (pGamepadState->status == 1)
844851
pDevice->status |= DEVICE_STATUS_ACTIVE;
@@ -850,19 +857,18 @@ void CopyGamepadStateToDeviceInfo(DeviceInfo_t* pDevice, GamepadState0100 *pGame
850857

851858
void CopyGamepadStateToDeviceInfo(DeviceInfo_t* pDevice, GamepadState0101* pGamepadState)
852859
{
853-
pDevice->DeviceState.XinputState.dwPacketNumber = pGamepadState->dwPacketNumber;
854-
pDevice->DeviceState.XinputState.Gamepad.bLeftTrigger = pGamepadState->bLeftTrigger;
855-
pDevice->DeviceState.XinputState.Gamepad.bRightTrigger = pGamepadState->bRightTrigger;
856-
pDevice->DeviceState.XinputState.Gamepad.sThumbLX = pGamepadState->sThumbLX;
857-
pDevice->DeviceState.XinputState.Gamepad.sThumbLY = pGamepadState->sThumbLY;
858-
pDevice->DeviceState.XinputState.Gamepad.sThumbRX = pGamepadState->sThumbRX;
859-
pDevice->DeviceState.XinputState.Gamepad.sThumbRY = pGamepadState->sThumbRY;
860-
pDevice->DeviceState.XinputState.Gamepad.wButtons = pGamepadState->wButtons;
861-
pDevice->DeviceState.XinputState.Gamepad.wButtons &= XINPUT_BUTTON_MASK;
860+
pDevice->DeviceState.dwPacketNumber = pGamepadState->dwPacketNumber;
861+
pDevice->DeviceState.Gamepad.bLeftTrigger = pGamepadState->bLeftTrigger;
862+
pDevice->DeviceState.Gamepad.bRightTrigger = pGamepadState->bRightTrigger;
863+
pDevice->DeviceState.Gamepad.sThumbLX = pGamepadState->sThumbLX;
864+
pDevice->DeviceState.Gamepad.sThumbLY = pGamepadState->sThumbLY;
865+
pDevice->DeviceState.Gamepad.sThumbRX = pGamepadState->sThumbRX;
866+
pDevice->DeviceState.Gamepad.sThumbRY = pGamepadState->sThumbRY;
867+
pDevice->DeviceState.Gamepad.wButtons = pGamepadState->wButtons;
868+
pDevice->DeviceState.Gamepad.wButtons &= XINPUT_BUTTON_MASK;
862869

863-
pDevice->DeviceState.GamepadExtras.dwExtraButtons = 0;
864-
if (pGamepadState->bExtraButtons & 0x01)
865-
pDevice->DeviceState.GamepadExtras.dwExtraButtons |= OPENXINPUT_GAMEPAD_EXTRAS_SHARE;
870+
pDevice->SystemButtons.StandardSystemButtons = pGamepadState->wButtons & XINPUT_GAMEPAD_GUIDE;
871+
pDevice->SystemButtons.ExtraSystemButtons = pGamepadState->bExtraButtons & XINPUT_GAMEPAD_EXTRAS_SHARE;
866872

867873
if (pGamepadState->status == 1)
868874
pDevice->status |= DEVICE_STATUS_ACTIVE;
@@ -1569,6 +1575,20 @@ HRESULT SetVibration(DeviceInfo_t* pDevice, void* pParams, DWORD reserved)
15691575
return DriverComm::SendDeviceVibration(pDevice);
15701576
}
15711577

1578+
HRESULT GetSystemButtons(DeviceInfo_t* pDevice, void* pParams, DWORD reserved)
1579+
{
1580+
HRESULT hr;
1581+
GetSystemButtonsApiParam_t* pApiParam = (GetSystemButtonsApiParam_t*)pParams;
1582+
1583+
if (IsDeviceInactive(pDevice))
1584+
return E_FAIL;
1585+
1586+
if ((hr = DriverComm::GetLatestDeviceInfo(pDevice)) < 0)
1587+
return hr;
1588+
1589+
return Utilities::SafeCopyToUntrustedBuffer(pApiParam->pSystemButtons, &pDevice->SystemButtons, sizeof(XINPUT_SYSTEM_BUTTONS));
1590+
}
1591+
15721592
}
15731593

15741594
namespace Disabled {
@@ -1581,8 +1601,8 @@ HRESULT GetState(DeviceInfo_t* pDevice, void* pParams, DWORD reserved)
15811601
if (IsDeviceInactive(pDevice))
15821602
return E_FAIL;
15831603

1584-
if (Utilities::SafeCopyToUntrustedBuffer(pApiParam->pState, &DisabledXINPUT_STATE, sizeof(XINPUT_STATE)) >= 0)
1585-
pApiParam->pState->XinputState.dwPacketNumber = pDevice->DeviceState.XinputState.dwPacketNumber + 1;
1604+
if (Utilities::SafeCopyToUntrustedBuffer(pApiParam->pState, &DisabledXINPUT_STATE, sizeof(DisabledXINPUT_STATE)) >= 0)
1605+
pApiParam->pState->dwPacketNumber = pDevice->DeviceState.dwPacketNumber + 1;
15861606

15871607
return S_OK;
15881608
}
@@ -1597,14 +1617,25 @@ HRESULT SetVibration(DeviceInfo_t* pDevice, void* pParams, DWORD reserved)
15971617

15981618
if (pApiParam->pVibration != nullptr)
15991619
{
1600-
return Utilities::SafeCopyToUntrustedBuffer(&pDevice->DeviceVibration, pApiParam->pVibration, sizeof(XINPUT_VIBRATION));
1620+
return Utilities::SafeCopyToUntrustedBuffer(&pDevice->DeviceVibration, pApiParam->pVibration, sizeof(*pApiParam->pVibration));
16011621
}
16021622
CopyMemory(&tmpDevice, pDevice, sizeof(DeviceInfo_t));
16031623
tmpDevice.DeviceVibration.wLeftMotorSpeed = 0;
16041624
tmpDevice.DeviceVibration.wRightMotorSpeed = 0;
16051625
return DriverComm::SendDeviceVibration(&tmpDevice);
16061626
}
16071627

1628+
HRESULT GetSystemButtons(DeviceInfo_t* pDevice, void* pParams, DWORD reserved)
1629+
{
1630+
static XINPUT_SYSTEM_BUTTONS DisabledSystemButtons = {};
1631+
GetSystemButtonsApiParam_t* pApiParam = (GetSystemButtonsApiParam_t*)pParams;
1632+
1633+
if (IsDeviceInactive(pDevice))
1634+
return E_FAIL;
1635+
1636+
return Utilities::SafeCopyToUntrustedBuffer(pApiParam->pSystemButtons, &DisabledSystemButtons, sizeof(DisabledSystemButtons));
1637+
}
1638+
16081639
}
16091640

16101641
DeviceInfo_t* Create(HANDLE hDevice, LPCWSTR lpDevicePath)
@@ -1693,11 +1724,13 @@ void OnEnableSettingChanged(BOOL bEnabled)
16931724
{
16941725
g_pfnGetStateDispatcher = Enabled::GetState;
16951726
g_pfnSetVibrationDispatcher = Enabled::SetVibration;
1727+
g_pfnGetSystemButtonsDispatcher = Enabled::GetSystemButtons;
16961728
}
16971729
else
16981730
{
16991731
g_pfnGetStateDispatcher = Disabled::GetState;
17001732
g_pfnSetVibrationDispatcher = Disabled::SetVibration;
1733+
g_pfnGetSystemButtonsDispatcher = Disabled::GetSystemButtons;
17011734
}
17021735
}
17031736

@@ -3050,7 +3083,7 @@ DWORD Controller_GetUserKeystroke(DeviceInfo_t* pDevice, BYTE bUserIndex, DWORD
30503083
static DWORD s_dwKeyPressStart;
30513084
static BOOL s_bKeyDown;
30523085

3053-
OPENXINPUT_STATE_FULL gamepadState;
3086+
XINPUT_STATE gamepadState;
30543087
GetStateApiParam_t apiParam;
30553088
DWORD virtualKey;
30563089
int key;
@@ -3061,14 +3094,14 @@ DWORD Controller_GetUserKeystroke(DeviceInfo_t* pDevice, BYTE bUserIndex, DWORD
30613094
if (XInputInternal::DeviceInfo::g_pfnGetStateDispatcher(pDevice, &apiParam, 1) < 0)
30623095
return ERROR_EMPTY;
30633096

3064-
gamepadState.XinputState.Gamepad.wButtons &= XINPUT_BUTTON_MASK_WITHOUT_GUIDE;
3097+
gamepadState.Gamepad.wButtons &= XINPUT_BUTTON_MASK_WITHOUT_GUIDE;
30653098

30663099
pKeystroke->UserIndex = bUserIndex;
30673100
pKeystroke->Unicode = 0;
30683101

30693102
key = 0;
30703103

3071-
virtualKey = Controller_CalculateKeyFromThumbPos(VK_PAD_LTHUMB_UP, gamepadState.XinputState.Gamepad.sThumbLX, gamepadState.XinputState.Gamepad.sThumbLY);
3104+
virtualKey = Controller_CalculateKeyFromThumbPos(VK_PAD_LTHUMB_UP, gamepadState.Gamepad.sThumbLX, gamepadState.Gamepad.sThumbLY);
30723105
pKeystroke->VirtualKey = (WORD)virtualKey;
30733106
if (pDevice->LeftStickVirtualKey != virtualKey)
30743107
{
@@ -3084,7 +3117,7 @@ DWORD Controller_GetUserKeystroke(DeviceInfo_t* pDevice, BYTE bUserIndex, DWORD
30843117
pDevice->LeftStickVirtualKey = (WORD)virtualKey;
30853118
}
30863119

3087-
virtualKey = Controller_CalculateKeyFromThumbPos(VK_PAD_RTHUMB_UP, gamepadState.XinputState.Gamepad.sThumbRX, gamepadState.XinputState.Gamepad.sThumbRY);
3120+
virtualKey = Controller_CalculateKeyFromThumbPos(VK_PAD_RTHUMB_UP, gamepadState.Gamepad.sThumbRX, gamepadState.Gamepad.sThumbRY);
30883121

30893122
if (virtualKey)
30903123
pKeystroke->VirtualKey = (WORD)virtualKey;
@@ -3103,9 +3136,9 @@ DWORD Controller_GetUserKeystroke(DeviceInfo_t* pDevice, BYTE bUserIndex, DWORD
31033136
}
31043137

31053138
pressedTriggers = 0;
3106-
if (gamepadState.XinputState.Gamepad.bLeftTrigger > 30u)
3139+
if (gamepadState.Gamepad.bLeftTrigger > 30u)
31073140
pressedTriggers = 1;
3108-
if (gamepadState.XinputState.Gamepad.bRightTrigger > 30u)
3141+
if (gamepadState.Gamepad.bRightTrigger > 30u)
31093142
pressedTriggers = 2;
31103143

31113144
virtualKey = VK_PAD_LTRIGGER;
@@ -3145,7 +3178,7 @@ DWORD Controller_GetUserKeystroke(DeviceInfo_t* pDevice, BYTE bUserIndex, DWORD
31453178

31463179
if(pDevice->wButtons & keyBit)
31473180
{
3148-
if (!(gamepadState.XinputState.Gamepad.wButtons & keyBit))
3181+
if (!(gamepadState.Gamepad.wButtons & keyBit))
31493182
{
31503183
pDevice->wButtons &= ~keyBit;
31513184
pKeystroke->VirtualKey = (WORD)virtualKey;
@@ -3155,7 +3188,7 @@ DWORD Controller_GetUserKeystroke(DeviceInfo_t* pDevice, BYTE bUserIndex, DWORD
31553188
}
31563189
pKeystroke->VirtualKey = (WORD)virtualKey;
31573190
}
3158-
else if (gamepadState.XinputState.Gamepad.wButtons & keyBit)
3191+
else if (gamepadState.Gamepad.wButtons & keyBit)
31593192
{
31603193
pDevice->wButtons |= keyBit;
31613194
key = virtualKey;
@@ -3725,14 +3758,22 @@ DWORD WINAPI OpenXInputGetStateEx(_In_ DWORD dwUserIndex, _Out_ XINPUT_STATE* pS
37253758
{
37263759
DWORD result;
37273760
HRESULT hr;
3728-
OPENXINPUT_STATE_FULL stateFull;
3761+
GetStateApiParam_t apiParam;
37293762

3730-
result = OpenXInputGetStateFull(dwUserIndex, &stateFull);
3731-
if (result == ERROR_SUCCESS)
3763+
if (dwUserIndex >= XUSER_MAX_COUNT || pState == nullptr)
3764+
return ERROR_BAD_ARGUMENTS;
3765+
3766+
if (XInputCore::g_pfnXInputGetState_Override)
37323767
{
3733-
memcpy(pState, &stateFull.XinputState, sizeof(XINPUT_STATE));
3768+
result = XInputCore::g_pfnXInputGetState_Override(dwUserIndex, pState);
37343769
}
3770+
else
3771+
{
3772+
apiParam.pState = pState;
37353773

3774+
hr = XInputCore::ProcessAPIRequest(dwUserIndex, XInputInternal::DeviceInfo::g_pfnGetStateDispatcher, &apiParam, 1, FALSE);
3775+
result = XInputReturnCodeFromHRESULT(hr);
3776+
}
37363777
return result;
37373778
}
37383779

@@ -3821,6 +3862,37 @@ DWORD WINAPI OpenXInputGetCapabilitiesEx(_In_ DWORD dwReserved, _In_ DWORD dwUse
38213862
return result;
38223863
}
38233864

3865+
DWORD WINAPI OpenXInputGetSystemButtons(
3866+
_In_ DWORD dwUserIndex,
3867+
_In_ XINPUT_SYSTEM_BUTTONS* pSystemButtons,
3868+
_In_ PVOID vReserved)
3869+
{
3870+
DWORD result;
3871+
HRESULT hr;
3872+
GetSystemButtonsApiParam_t apiParam;
3873+
3874+
if (dwUserIndex >= XUSER_MAX_COUNT || pSystemButtons == nullptr)
3875+
return ERROR_BAD_ARGUMENTS;
3876+
3877+
if (XInputCore::g_pfnXInputGetState_Override != nullptr)
3878+
{
3879+
result = ERROR_ACCESS_DENIED;
3880+
}
3881+
else
3882+
{
3883+
apiParam.pSystemButtons = pSystemButtons;
3884+
hr = XInputCore::ProcessAPIRequest(
3885+
dwUserIndex,
3886+
XInputInternal::DeviceInfo::g_pfnGetSystemButtonsDispatcher,
3887+
&apiParam,
3888+
1,
3889+
FALSE);
3890+
result = XInputReturnCodeFromHRESULT(hr);
3891+
}
3892+
3893+
return result;
3894+
}
3895+
38243896
DWORD WINAPI OpenXInputGetMaxControllerCount()
38253897
{
38263898
return XUSER_MAX_COUNT;
@@ -3845,11 +3917,12 @@ DWORD WINAPI OpenXInputGetDeviceUSBIds(DWORD dwUserIndex, WORD* pVendorId, WORD*
38453917
return result;
38463918
}
38473919

3848-
DWORD WINAPI OpenXInputGetStateFull(_In_ DWORD dwUserIndex, _Out_ OPENXINPUT_STATE_FULL* pState)
3920+
DWORD WINAPI OpenXInputGetStateFull(_In_ DWORD dwUserIndex, _Out_ OPENXINPUT_STATE_FULL* pState)
38493921
{
38503922
DWORD result;
38513923
HRESULT hr;
3852-
GetStateApiParam_t apiParam;
3924+
GetStateApiParam_t getStateApiParam;
3925+
GetSystemButtonsApiParam_t getSystemButtonsApiParam;
38533926

38543927
if (dwUserIndex >= XUSER_MAX_COUNT || pState == nullptr)
38553928
return ERROR_BAD_ARGUMENTS;
@@ -3860,9 +3933,15 @@ DWORD WINAPI OpenXInputGetStateFull(_In_ DWORD dwUserIndex, _Out_ OPENXINPUT_ST
38603933
}
38613934
else
38623935
{
3863-
apiParam.pState = pState;
3936+
getStateApiParam.pState = &pState->XinputState;
3937+
3938+
hr = XInputCore::ProcessAPIRequest(dwUserIndex, XInputInternal::DeviceInfo::g_pfnGetStateDispatcher, &getStateApiParam, 1, FALSE);
3939+
if (hr == S_OK)
3940+
{
3941+
getSystemButtonsApiParam.pSystemButtons = &pState->XinputSystemButtons;
3942+
hr = XInputCore::ProcessAPIRequest(dwUserIndex, XInputInternal::DeviceInfo::g_pfnGetSystemButtonsDispatcher, &getSystemButtonsApiParam, 1, FALSE);
3943+
}
38643944

3865-
hr = XInputCore::ProcessAPIRequest(dwUserIndex, XInputInternal::DeviceInfo::g_pfnGetStateDispatcher, &apiParam, 1, FALSE);
38663945
result = XInputReturnCodeFromHRESULT(hr);
38673946
}
38683947
return result;

src/OpenXinput.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ XInputCancelGuideButtonWait @102 NONAME
1414
XInputPowerOffController @103 NONAME
1515
XInputGetBaseBusInformation @104 NONAME
1616
XInputGetCapabilitiesEx @108 NONAME
17+
XInputGetSystemButtons @109 NONAME
1718

1819
OpenXInputGetMaxControllerCount @999
1920
OpenXInputGetDeviceUSBIds @1000

0 commit comments

Comments
 (0)