TLAC: proper raw input for PS4 controller slider

This commit is contained in:
somewhatlurker
2020-08-12 22:34:21 +10:00
parent e20d4c6781
commit 8a5cb75954
5 changed files with 89 additions and 29 deletions
@@ -53,6 +53,45 @@ namespace TLAC::Input
return result;
}
HRESULT DirectInputDevice::DI_SetRange(LONG min, LONG max)
{
DIPROPRANGE propData;
propData.lMin = min;
propData.lMax = max;
propData.diph.dwSize = sizeof(DIPROPRANGE);
propData.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propData.diph.dwHow = DIPH_DEVICE;
propData.diph.dwObj = 0;
HRESULT result = directInputdevice->SetProperty(DIPROP_RANGE, &propData.diph);
return result;
}
HRESULT DirectInputDevice::DI_SetRawMode(BOOL raw)
{
// extra unacquire/acquire logic so this can be called on an active device
BOOL wasAcquired = DI_Unacquire() == DI_OK;
DIPROPDWORD propData;
propData.dwData = raw ? DIPROPCALIBRATIONMODE_RAW : DIPROPCALIBRATIONMODE_COOKED;
propData.diph.dwSize = sizeof(DIPROPDWORD);
propData.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propData.diph.dwHow = DIPH_DEVICE;
propData.diph.dwObj = 0;
HRESULT result = NULL;
if (FAILED(result = directInputdevice->SetProperty(DIPROP_CALIBRATIONMODE, &propData.diph)))
return result;
if (wasAcquired)
result = DI_Acquire();
return result;
}
void DirectInputDevice::DI_Dispose()
{
if (directInputdevice == nullptr)
@@ -16,6 +16,8 @@ namespace TLAC::Input
HRESULT DI_Release();
HRESULT DI_Poll();
HRESULT DI_GetDeviceState(DWORD size, LPVOID data);
HRESULT DI_SetRange(LONG min, LONG max);
HRESULT DI_SetRawMode(BOOL raw);
void DI_Dispose();
};
@@ -6,6 +6,7 @@
namespace TLAC::Input
{
DualShock4* DualShock4::instance;
bool DualShock4::rawMode = false;
DualShock4::DualShock4()
{
@@ -53,11 +54,29 @@ namespace TLAC::Input
if (FAILED(result = DI_SetDataFormat(&c_dfDIJoystick2)))
return false;
if (FAILED(result = DI_SetRange(0, 255))) // use 8-bit data
return false;
// set raw mode from previous instance
if (rawMode && FAILED(result = DI_SetRawMode(true)))
return false;
result = DI_Acquire();
return true;
}
bool DualShock4::SetRawMode(bool raw)
{
HRESULT result = NULL;
if (FAILED(result = DI_SetRawMode(raw)))
return false;
rawMode = raw;
return true;
}
bool DualShock4::PollInput()
{
lastState = currentState;
@@ -31,6 +31,7 @@ namespace TLAC::Input
static bool TryInitializeInstance();
bool Initialize();
bool SetRawMode(bool raw);
bool PollInput() override;
bool IsDown(Ds4Button button);
@@ -48,6 +49,8 @@ namespace TLAC::Input
static inline DualShock4* GetInstance() { return instance; };
static inline void DeleteInstance() { delete instance; instance = nullptr; };
static bool rawMode; // kinda dodgy, used to set raw mode for all instances without waiting for a single one
private:
static DualShock4* instance;
@@ -58,8 +61,8 @@ namespace TLAC::Input
const float joystickThreshold = 0.5f;
const float dpadThreshold = 0.5f;
inline float NormalizeTrigger(long value) { return (float)value / USHRT_MAX; };
inline float NormalizeStick(long value) { return (float)value / USHRT_MAX * 2.0f - 1.0f; };
inline float NormalizeTrigger(long value) { return (float)value / UCHAR_MAX; };
inline float NormalizeStick(long value) { return (float)value / UCHAR_MAX * 2.0f - 1.0f; };
inline Joystick NormalizeStick(long x, long y) { return Joystick(NormalizeStick(x), NormalizeStick(y)); };
void UpdateInternalDs4State(Ds4State &state);