Skip to content

Commit

Permalink
Reapply DualSense trigger resistance upon Alt-Tabbing into a game, to…
Browse files Browse the repository at this point in the history
… avoid problems switching between two games that have different force feedback policies.
  • Loading branch information
Kaldaien committed Dec 15, 2024
1 parent 8ef7346 commit 8a5913e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
13 changes: 8 additions & 5 deletions include/SpecialK/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -1089,11 +1089,13 @@ struct SK_HID_PlayStationDevice
bool request_input_report (void);
bool write_output_report (bool force = false);

bool setPollingFrequency (DWORD dwFreq);
bool setBufferCount (DWORD dwBuffers);
bool setPollingFrequency (DWORD dwFreq);
bool setBufferCount (DWORD dwBuffers);

void reset_force_feedback (void);

bool initialize_serial (void);
void reset_device (void);
bool initialize_serial (void);
void reset_device (void);
};

struct SK_HID_OverlappedRequest {
Expand Down Expand Up @@ -1139,7 +1141,8 @@ bool SK_ImGui_HasPlayStationController (void);
bool SK_ImGui_HasDualSenseController (void);
bool SK_ImGui_HasDualSenseEdgeController (void);

void SK_HID_SetupPlayStationControllers (void);
void SK_HID_SetupPlayStationControllers (void);
void SK_HID_FlushPlayStationForceFeedback (void);


// Temporarily override game's preferences for input device window message generation
Expand Down
73 changes: 48 additions & 25 deletions src/input/hid_reports/playstation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ SK_HID_PlayStationDevice::~SK_HID_PlayStationDevice (void)

extern bool SK_SetThreadIOPriority (HANDLE hThread, int priority);

void SK_HID_FlushPlayStationForceFeedback (void)
{
for (auto& ps_controller : SK_HID_PlayStationControllers)
{
if ( ps_controller.bConnected &&
( ps_controller.bDualSense ||
ps_controller.bDualSenseEdge ) )
{
WriteRelease (
&ps_controller.bNeedOutput, TRUE
); ps_controller.reset_force_feedback ();
ps_controller.write_output_report ();
}
}
}

void SK_HID_SetupPlayStationControllers (void)
{
// Only do this once, and make all other threads trying to do it wait
Expand Down Expand Up @@ -2732,13 +2748,13 @@ SK_HID_PlayStationDevice::write_output_report (bool force)
const bool bRumble =
(dwRightMotor != 0 ||
dwLeftMotor != 0) || (dwLeftTrigger != 0
|| dwRightTrigger != 0) || bResistChange;
|| dwRightTrigger != 0);

// 500 msec grace period before allowing controller to use native haptics
output->UseRumbleNotHaptics = bRumble ||
(ReadULongAcquire (&pDevice->_vibration.last_set) > SK::ControlPanel::current_time - 500UL)
|| last_trigger_r != 0
|| last_trigger_l != 0;
output->UseRumbleNotHaptics = bRumble || last_trigger_r != 0
|| last_trigger_l != 0
|| bResistChange ||
(ReadULongAcquire (&pDevice->_vibration.last_set) > SK::ControlPanel::current_time - 500UL);

if (bRumble || (last_trigger_r != 0 || last_trigger_l != 0))
{
Expand Down Expand Up @@ -2772,7 +2788,7 @@ SK_HID_PlayStationDevice::write_output_report (bool force)
ReadULongAcquire (&pDevice->_vibration.left)
);

if (bRumble || (last_trigger_r != 0 || last_trigger_l != 0) || bResistChange)
if ((last_trigger_r != 0 || last_trigger_l != 0) || bResistChange)
{
auto effect = (dwLeftTrigger != 0) ?
playstation_trigger_effect::Vibration :
Expand Down Expand Up @@ -2950,13 +2966,13 @@ SK_HID_PlayStationDevice::write_output_report (bool force)
const bool bRumble =
(dwRightMotor != 0 ||
dwLeftMotor != 0) || (dwLeftTrigger != 0
|| dwRightTrigger != 0) || bResistChange;
|| dwRightTrigger != 0);

// 500 msec grace period before allowing controller to use native haptics
output->UseRumbleNotHaptics = bRumble ||
(ReadULongAcquire (&pDevice->_vibration.last_set) > SK::ControlPanel::current_time - 500UL)
|| last_trigger_r != 0
|| last_trigger_l != 0;
output->UseRumbleNotHaptics = bRumble || last_trigger_r != 0
|| last_trigger_l != 0
|| bResistChange ||
(ReadULongAcquire (&pDevice->_vibration.last_set) > SK::ControlPanel::current_time - 500UL);

if (bRumble || (last_trigger_r != 0 || last_trigger_l != 0))
{
Expand Down Expand Up @@ -2990,7 +3006,7 @@ SK_HID_PlayStationDevice::write_output_report (bool force)
ReadULongAcquire (&pDevice->_vibration.left)
);

if (bRumble || (last_trigger_r != 0 || last_trigger_l != 0) || bResistChange)
if (last_trigger_r != 0 || last_trigger_l != 0 || bResistChange)
{
auto effect = (dwLeftTrigger != 0) ?
playstation_trigger_effect::Vibration :
Expand Down Expand Up @@ -3959,6 +3975,15 @@ SK_HID_PlayStationDevice::initialize_serial (void)
return false;
}

void
SK_HID_PlayStationDevice::reset_force_feedback (void)
{
_vibration.trigger.last_resist_start_l = -2.0f;
_vibration.trigger.last_resist_start_r = -2.0f;
_vibration.trigger.last_resist_str_l = -2.0f;
_vibration.trigger.last_resist_str_r = -2.0f;
}

void
SK_HID_PlayStationDevice::reset_device (void)
{
Expand All @@ -3977,14 +4002,12 @@ SK_HID_PlayStationDevice::reset_device (void)
WriteULongRelease (&_vibration.last_set, 0);
WriteULongRelease (&_vibration.max_val, 0);

_vibration.trigger.last_left = 0;
_vibration.trigger.last_right = 0;
_vibration.trigger.start_left = 0;
_vibration.trigger.start_right = 0;
_vibration.trigger.last_resist_start_l = -2.0f;
_vibration.trigger.last_resist_start_r = -2.0f;
_vibration.trigger.last_resist_str_l = -2.0f;
_vibration.trigger.last_resist_str_r = -2.0f;
_vibration.trigger.last_left = 0;
_vibration.trigger.last_right = 0;
_vibration.trigger.start_left = 0;
_vibration.trigger.start_right = 0;

reset_force_feedback ();

WriteULong64Release (&xinput.last_active, 0);

Expand All @@ -3999,7 +4022,7 @@ SK_HID_PlayStationDevice::reset_device (void)
dwLastTimeOutput = 0;
ulLastFrameOutput = 0;

bNeedOutput = true;
WriteRelease (&bNeedOutput, TRUE);
bSimpleMode = true;

for ( auto& button : buttons )
Expand All @@ -4020,10 +4043,10 @@ SK_HID_DualSense_SetStateDataImpl (SK_HID_DualSense_SetStateData* report, bool l
else report->AllowRightTriggerFFB = true;

uint8_t effects [][11] = {
{ 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x01, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x06, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // Off
{ 0x01, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0 }, // Feedback
{ 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // ...
{ 0x06, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // Vibration
};

if (effect >= playstation_trigger_effect::Invalid)
Expand Down
8 changes: 8 additions & 0 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,14 @@ ActivateWindow ( HWND hWnd,

if (game_window.active)
{
if (is_game_window)
{
if (active)
{
SK_HID_FlushPlayStationForceFeedback ();
}
}

// Release the AltKin
for ( BYTE VKey = 0x8 ; VKey < 255 ; ++VKey )
{
Expand Down

0 comments on commit 8a5913e

Please sign in to comment.