From 1cf49e0dbe74007a077f3c032b28e335fccb6d75 Mon Sep 17 00:00:00 2001 From: StapleButter Date: Sun, 30 Dec 2018 00:27:12 +0100 Subject: make joystick hotpluggable --- src/libui_sdl/main.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp index cd350f7..9f8f678 100644 --- a/src/libui_sdl/main.cpp +++ b/src/libui_sdl/main.cpp @@ -401,7 +401,11 @@ int EmuThreadFunc(void* burp) if (Joystick) { njoybuttons = SDL_JoystickNumButtons(Joystick); - if (njoybuttons) joybuttons = new Uint8[njoybuttons]; + if (njoybuttons) + { + joybuttons = new Uint8[njoybuttons]; + memset(joybuttons, 0, sizeof(Uint8)*njoybuttons); + } } u32 nframes = 0; @@ -416,15 +420,27 @@ int EmuThreadFunc(void* burp) if (EmuRunning == 1) { EmuStatus = 1; - + + SDL_JoystickUpdate(); + + if (Joystick) + { + if (!SDL_JoystickGetAttached(Joystick)) + { + SDL_JoystickClose(Joystick); + Joystick = NULL; + } + } + if (!Joystick && (SDL_NumJoysticks() > 0)) + { + Joystick = SDL_JoystickOpen(0); + } // poll input u32 keymask = KeyInputMask; u32 joymask = 0xFFF; if (Joystick) { - SDL_JoystickUpdate(); - Uint32 hat = SDL_JoystickGetHat(Joystick, 0); Sint16 axisX = SDL_JoystickGetAxis(Joystick, 0); Sint16 axisY = SDL_JoystickGetAxis(Joystick, 1); -- cgit v1.2.3