diff --git a/src/SFML/Window/Win32/JoystickImpl.cpp b/src/SFML/Window/Win32/JoystickImpl.cpp index 5098b7639..f14cecfc1 100644 --- a/src/SFML/Window/Win32/JoystickImpl.cpp +++ b/src/SFML/Window/Win32/JoystickImpl.cpp @@ -26,10 +26,24 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include +namespace +{ + struct ConnectionCache + { + ConnectionCache() : connected(false) {} + bool connected; + sf::Clock timer; + }; + + const sf::Time connectionRefreshDelay = sf::milliseconds(500); + ConnectionCache connectionCache[sf::Joystick::Count]; +} + namespace sf { namespace priv @@ -37,11 +51,25 @@ namespace priv //////////////////////////////////////////////////////////// bool JoystickImpl::isConnected(unsigned int index) { - JOYINFOEX joyInfo; - joyInfo.dwSize = sizeof(joyInfo); - joyInfo.dwFlags = 0; + // We check the connection state of joysticks only every N milliseconds, + // because of a strange (buggy?) behaviour of joyGetPosEx when joysticks + // are just plugged/unplugged -- it takes really long and kills the app performances + ConnectionCache& cache = connectionCache[index]; + if (cache.timer.getElapsedTime() > connectionRefreshDelay) + { + cache.timer.restart(); - return joyGetPosEx(JOYSTICKID1 + index, &joyInfo) == JOYERR_NOERROR; + JOYINFOEX joyInfo; + joyInfo.dwSize = sizeof(joyInfo); + joyInfo.dwFlags = 0; + + cache.connected = joyGetPosEx(JOYSTICKID1 + index, &joyInfo) == JOYERR_NOERROR; + return cache.connected; + } + else + { + return cache.connected; + } }