From c9aa0396950eefc189692e267b8042c613f77cc2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 1 Nov 2018 21:42:12 -0700 Subject: [PATCH] Windows: Check for devices on Windows events only. A user reported stuttering after the periodic checks, and devices failing until unplug/replug. Presumably, this is caused by poor drivers reacting badly to periodic DirectInput queries, so less queries should help. --- Windows/DinputDevice.cpp | 6 ++++-- Windows/DinputDevice.h | 5 +++++ Windows/MainWindow.cpp | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Windows/DinputDevice.cpp b/Windows/DinputDevice.cpp index c72b7c0dbc..5d89e4ab54 100644 --- a/Windows/DinputDevice.cpp +++ b/Windows/DinputDevice.cpp @@ -39,6 +39,7 @@ unsigned int DinputDevice::pInstances = 0; LPDIRECTINPUT8 DinputDevice::pDI = NULL; std::vector DinputDevice::devices; +bool DinputDevice::needsCheck_ = true; // In order from 0. There can be 128, but most controllers do not have that many. static const int dinput_buttons[] = { @@ -120,7 +121,7 @@ BOOL CALLBACK DinputDevice::DevicesCallback( void DinputDevice::getDevices(bool refresh) { - if (devices.empty() || refresh) + if (refresh) { getPDI()->EnumDevices(DI8DEVCLASS_GAMECTRL, &DinputDevice::DevicesCallback, NULL, DIEDFL_ATTACHEDONLY); } @@ -368,6 +369,7 @@ void DinputDevice::ApplyButtons(DIJOYSTATE2 &state) { size_t DinputDevice::getNumPads() { - getDevices(true); + getDevices(needsCheck_); + needsCheck_ = false; return devices.size(); } diff --git a/Windows/DinputDevice.h b/Windows/DinputDevice.h index 366805c856..6314a055ed 100644 --- a/Windows/DinputDevice.h +++ b/Windows/DinputDevice.h @@ -35,6 +35,10 @@ public: virtual int UpdateState(); virtual bool IsPad() { return true; } static size_t getNumPads(); + static void CheckDevices() { + needsCheck_ = true; + } + private: void ApplyButtons(DIJOYSTATE2 &state); //unfortunate and unclean way to keep only one DirectInput instance around @@ -52,6 +56,7 @@ private: static unsigned int pInstances; static std::vector devices; static LPDIRECTINPUT8 pDI; + static bool needsCheck_; int pDevNum; LPDIRECTINPUTDEVICE8 pJoystick; DIJOYSTATE2 pPrevState; diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index 848b8cb5fa..9cf3eedfd3 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -54,6 +54,7 @@ #include "Windows/GEDebugger/GEDebugger.h" #include "Windows/main.h" +#include "Windows/DinputDevice.h" #include "Windows/EmuThread.h" #include "Windows/resource.h" @@ -814,6 +815,10 @@ namespace MainWindow case WM_CHAR: return WindowsRawInput::ProcessChar(hWnd, wParam, lParam); + case WM_DEVICECHANGE: + DinputDevice::CheckDevices(); + return DefWindowProc(hWnd, message, wParam, lParam); + case WM_VERYSLEEPY_MSG: switch (wParam) { case VERYSLEEPY_WPARAM_SUPPORTED: