Skip to content

Commit

Permalink
Merge pull request #11519 from unknownbrackets/dinput-slow
Browse files Browse the repository at this point in the history
Windows: Check for devices on Windows events only
  • Loading branch information
hrydgard committed Nov 2, 2018
2 parents fdb0732 + d30d75a commit d89e120
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Windows/DinputDevice.cpp
Expand Up @@ -39,6 +39,7 @@
unsigned int DinputDevice::pInstances = 0;
LPDIRECTINPUT8 DinputDevice::pDI = NULL;
std::vector<DIDEVICEINSTANCE> 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[] = {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -368,6 +369,7 @@ void DinputDevice::ApplyButtons(DIJOYSTATE2 &state) {

size_t DinputDevice::getNumPads()
{
getDevices(true);
getDevices(needsCheck_);
needsCheck_ = false;
return devices.size();
}
5 changes: 5 additions & 0 deletions Windows/DinputDevice.h
Expand Up @@ -35,6 +35,10 @@ class DinputDevice :
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
Expand All @@ -52,6 +56,7 @@ class DinputDevice :
static unsigned int pInstances;
static std::vector<DIDEVICEINSTANCE> devices;
static LPDIRECTINPUT8 pDI;
static bool needsCheck_;
int pDevNum;
LPDIRECTINPUTDEVICE8 pJoystick;
DIJOYSTATE2 pPrevState;
Expand Down
5 changes: 5 additions & 0 deletions Windows/MainWindow.cpp
Expand Up @@ -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"

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d89e120

Please sign in to comment.