Skip to content

Commit

Permalink
Do not use any poll gamepad input until SKIF's window has been in the…
Browse files Browse the repository at this point in the history
… foreground for 500 ms.

Set gamepad input state to null when SKIF's window loses foreground status.
  • Loading branch information
Kaldaien committed Nov 4, 2024
1 parent 7bdf319 commit 79a1bc5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/utility/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ SKIF_GamePadInputHelper::InvalidateGamePads (void)
XINPUT_STATE
SKIF_GamePadInputHelper::GetXInputState (void)
{
return m_xisGamepad.load();
DWORD dwActivePid = 0x0;
GetWindowThreadProcessId (GetForegroundWindow (), &dwActivePid);

if (dwActivePid == GetCurrentProcessId ())
return m_xisGamepad.load ();

return {};
}

// If we really wanted to limit the exposure of this function,
Expand Down Expand Up @@ -183,7 +189,11 @@ SKIF_GamePadInputHelper::UpdateXInputState (void)
DWORD dwActivePid = 0x0;
GetWindowThreadProcessId (GetForegroundWindow (), &dwActivePid);

static DWORD dwLastActivePid = dwActivePid;
static DWORD dwLastActivePid = dwActivePid;
static DWORD dwTimeOfActivation = 0;

if (dwLastActivePid != dwActivePid && dwActivePid == GetCurrentProcessId ())
dwTimeOfActivation = SKIF_Util_timeGetTime ();

for ( auto idx : XUSER_INDEXES )
{
Expand All @@ -199,7 +209,8 @@ SKIF_GamePadInputHelper::UpdateXInputState (void)
m_bGamepads [idx].store (false);

else if (dwResult == ERROR_SUCCESS && GetCurrentProcessId () == dwActivePid &&
dwLastActivePid == dwActivePid)
dwLastActivePid == dwActivePid &&
dwTimeOfActivation < SKIF_Util_timeGetTime () - 500UL)
{
// If button state is different, this controller is active...
if ( xinput_state.dwPacketNumber != local.last_state.dwPacketNumber )
Expand Down Expand Up @@ -268,7 +279,8 @@ SKIF_GamePadInputHelper::UpdateXInputState (void)
newest.state = XSTATE_EMPTY;

if (dwActivePid != GetCurrentProcessId () ||
!IsWindowVisible (SKIF_ImGui_hWnd))
!IsWindowVisible (SKIF_ImGui_hWnd) ||
dwTimeOfActivation > SKIF_Util_timeGetTime () - 500UL)
{
// Neutralize input because SKIF is not in the foreground
newest.state = XSTATE_EMPTY;
Expand Down Expand Up @@ -312,6 +324,8 @@ SKIF_GamePadInputHelper::WakeThread (void)
{
PLOG_VERBOSE << "Waking the gamepad input child thread...";
m_bThreadAwake.store (true);
m_xisGamepad.store ({});

WakeAllConditionVariable (&m_GamePadInput);
}
}
Expand All @@ -320,6 +334,7 @@ void
SKIF_GamePadInputHelper::SleepThread (void)
{
m_bThreadAwake.store (false);
m_xisGamepad.store ({});
}

void
Expand Down
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#define SKIF_MAJOR 1
#define SKIF_MINOR 1
#define SKIF_BUILD 10
#define SKIF_BUILD 12
#define SKIF_REV 0


Expand Down

0 comments on commit 79a1bc5

Please sign in to comment.