From 82069c35036c4151004ff24141d48eddc50b24a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 9 Mar 2026 11:02:04 +0100 Subject: [PATCH] Windows input optimizations Fixes a bug where the thread wouldn't sleep when it should in some configurations. Also increases the time between polls for new controllers. --- Windows/DinputDevice.cpp | 2 +- Windows/Hid/HidInputDevice.h | 2 +- Windows/InputDevice.cpp | 4 ++-- Windows/InputDevice.h | 2 +- Windows/XinputDevice.cpp | 5 ++--- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Windows/DinputDevice.cpp b/Windows/DinputDevice.cpp index 634126ad07..4c48dc7c3c 100644 --- a/Windows/DinputDevice.cpp +++ b/Windows/DinputDevice.cpp @@ -417,7 +417,7 @@ DInputMetaDevice::DInputMetaDevice() { } int DInputMetaDevice::UpdateState() { - static const int CHECK_FREQUENCY = 71; // Just an arbitrary prime to try to not collide with other periodic checks. + constexpr int CHECK_FREQUENCY = 787; // Just an arbitrary prime to try to not collide with other periodic checks. if (checkCounter_++ > CHECK_FREQUENCY) { const size_t newCount = DinputDevice::getNumPads(); if (newCount > numDinputDevices_) { diff --git a/Windows/Hid/HidInputDevice.h b/Windows/Hid/HidInputDevice.h index 28f622040c..a24d8a8adb 100644 --- a/Windows/Hid/HidInputDevice.h +++ b/Windows/Hid/HidInputDevice.h @@ -68,6 +68,6 @@ private: int inReportSize_ = 0; int outReportSize_ = 0; enum { - POLL_FREQ = 283, // a prime number. + POLL_FREQ = 709, // a prime number. }; }; diff --git a/Windows/InputDevice.cpp b/Windows/InputDevice.cpp index 0b92d6ca55..0b9c56d328 100644 --- a/Windows/InputDevice.cpp +++ b/Windows/InputDevice.cpp @@ -42,12 +42,12 @@ void InputManager::InputThread() { // NOTE: The keyboard and mouse buttons are handled via raw input, not here. // This is mainly for controllers which need to be polled, instead of generating events. - bool noSleep = false; while (runThread_.load(std::memory_order_relaxed)) { + bool noSleep = false; if (focused_.load(std::memory_order_relaxed) || !g_Config.bGamepadOnlyFocused) { System_Notify(SystemNotification::POLL_CONTROLLERS); for (const auto &device : devices_) { - int state = device->UpdateState(); + const int state = device->UpdateState(); if (state == InputDevice::UPDATESTATE_SKIP_PAD) break; if (state == InputDevice::UPDATESTATE_NO_SLEEP) { diff --git a/Windows/InputDevice.h b/Windows/InputDevice.h index 184990a794..12fe505b6f 100644 --- a/Windows/InputDevice.h +++ b/Windows/InputDevice.h @@ -32,7 +32,7 @@ public: virtual void Shutdown() {} virtual bool HasAccelerometer() const { return false; } - enum { UPDATESTATE_SKIP_PAD = 0x1234, UPDATESTATE_NO_SLEEP = 0x2345}; + enum { UPDATESTATE_NORMAL = 0, UPDATESTATE_SKIP_PAD = 0x1234, UPDATESTATE_NO_SLEEP = 0x2345}; virtual int UpdateState() = 0; }; diff --git a/Windows/XinputDevice.cpp b/Windows/XinputDevice.cpp index 38c75dc0a6..9960d139f8 100644 --- a/Windows/XinputDevice.cpp +++ b/Windows/XinputDevice.cpp @@ -201,9 +201,8 @@ int XinputDevice::UpdateState() { } } - // If we get XInput, skip the others. This might not actually be a good idea, - // and was done to avoid conflicts between DirectInput and XInput. - return 0; // anySuccess ? UPDATESTATE_SKIP_PAD : 0; + // Previously we returned SKIP_PAD here but it wasn't a good idea. + return UPDATESTATE_NORMAL; } void XinputDevice::ReleaseAllKeys(int pad) {