Fix DirectInput controllers on game startup

Now SDL and DirectInput don't complain when a game starts with a DirectInput controller already connected. Fixes "JoypadSDL::process_events: Error opening gamepad at index 1: IDirectInputDevice8::SetCooperativeLevel() DirectX error 0x80070006"
This commit is contained in:
Nintorch
2025-08-21 13:21:48 +05:00
parent 7cc3f374a5
commit dd2e1b104b
3 changed files with 18 additions and 18 deletions

View File

@ -58,6 +58,19 @@ JoypadSDL::JoypadSDL() {
singleton = this; singleton = this;
} }
#ifdef WINDOWS_ENABLED
extern "C" {
HWND SDL_HelperWindow;
}
// Required for DInput joypads to work
// TODO: remove this workaround when we update to newer version of SDL
JoypadSDL::JoypadSDL(HWND p_helper_window) :
JoypadSDL() {
SDL_HelperWindow = p_helper_window;
}
#endif
JoypadSDL::~JoypadSDL() { JoypadSDL::~JoypadSDL() {
// Process any remaining input events // Process any remaining input events
process_events(); process_events();
@ -247,17 +260,6 @@ void JoypadSDL::process_events() {
} }
} }
#ifdef WINDOWS_ENABLED
extern "C" {
HWND SDL_HelperWindow;
}
// Required for DInput joypads to work
void JoypadSDL::setup_sdl_helper_window(HWND p_hwnd) {
SDL_HelperWindow = p_hwnd;
}
#endif
void JoypadSDL::close_joypad(int p_pad_idx) { void JoypadSDL::close_joypad(int p_pad_idx) {
int sdl_instance_idx = joypads[p_pad_idx].sdl_instance_idx; int sdl_instance_idx = joypads[p_pad_idx].sdl_instance_idx;

View File

@ -39,15 +39,15 @@ typedef struct HWND__ *HWND;
class JoypadSDL { class JoypadSDL {
public: public:
JoypadSDL(); JoypadSDL();
#ifdef WINDOWS_ENABLED
JoypadSDL(HWND p_helper_window);
#endif
~JoypadSDL(); ~JoypadSDL();
static JoypadSDL *get_singleton(); static JoypadSDL *get_singleton();
Error initialize(); Error initialize();
void process_events(); void process_events();
#ifdef WINDOWS_ENABLED
void setup_sdl_helper_window(HWND p_hwnd);
#endif
private: private:
struct Joypad { struct Joypad {

View File

@ -7188,10 +7188,8 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
} }
#ifdef SDL_ENABLED #ifdef SDL_ENABLED
joypad_sdl = memnew(JoypadSDL()); joypad_sdl = memnew(JoypadSDL(windows[MAIN_WINDOW_ID].hWnd));
if (joypad_sdl->initialize() == OK) { if (joypad_sdl->initialize() != OK) {
joypad_sdl->setup_sdl_helper_window(windows[MAIN_WINDOW_ID].hWnd);
} else {
ERR_PRINT("Couldn't initialize SDL joypad input driver."); ERR_PRINT("Couldn't initialize SDL joypad input driver.");
memdelete(joypad_sdl); memdelete(joypad_sdl);
joypad_sdl = nullptr; joypad_sdl = nullptr;