From 2986a40c5a9fdb4c44813071cb99081fa610dc8e Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 29 Jan 2019 22:01:05 -0800 Subject: [PATCH] Fix some gamepads working for UI navigation but not in game --- app/gui/sdlgamepadkeynavigation.cpp | 5 +++++ app/streaming/input.cpp | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/gui/sdlgamepadkeynavigation.cpp b/app/gui/sdlgamepadkeynavigation.cpp index 3f1f7047..9f48b001 100644 --- a/app/gui/sdlgamepadkeynavigation.cpp +++ b/app/gui/sdlgamepadkeynavigation.cpp @@ -254,6 +254,11 @@ int SdlGamepadKeyNavigation::getConnectedGamepads() return 0; } + // Applying mappings is necessary to ensure gamepad without + // a built-in mapping are properly counted. + MappingManager mappingManager; + mappingManager.applyMappings(); + int count = 0; for (int i = 0; i < SDL_NumJoysticks(); i++) { if (SDL_IsGameController(i)) { diff --git a/app/streaming/input.cpp b/app/streaming/input.cpp index 2ae95f56..bef4dfc3 100644 --- a/app/streaming/input.cpp +++ b/app/streaming/input.cpp @@ -57,6 +57,21 @@ SdlInputHandler::SdlInputHandler(StreamingPreferences& prefs, NvComputer*, int s SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, prefs.mouseAcceleration ? "1" : "0"); + // We must initialize joystick explicitly before gamecontroller in order + // to ensure we receive gamecontroller attach events for gamepads where + // SDL doesn't have a built-in mapping. By starting joystick first, we + // can allow mapping manager to update the mappings before GC attach + // events are generated. + SDL_assert(!SDL_WasInit(SDL_INIT_JOYSTICK)); + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) != 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "SDL_InitSubSystem(SDL_INIT_JOYSTICK) failed: %s", + SDL_GetError()); + } + + MappingManager mappingManager; + mappingManager.applyMappings(); + // We need to reinit this each time, since you only get // an initial set of gamepad arrival events once per init. SDL_assert(!SDL_WasInit(SDL_INIT_GAMECONTROLLER)); @@ -66,9 +81,6 @@ SdlInputHandler::SdlInputHandler(StreamingPreferences& prefs, NvComputer*, int s SDL_GetError()); } - MappingManager mappingManager; - mappingManager.applyMappings(); - // Initialize the gamepad mask with currently attached gamepads to avoid // causing gamepads to unexpectedly disappear and reappear on the host // during stream startup as we detect currently attached gamepads one at a time. @@ -99,6 +111,9 @@ SdlInputHandler::~SdlInputHandler() SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER); SDL_assert(!SDL_WasInit(SDL_INIT_GAMECONTROLLER)); + + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + SDL_assert(!SDL_WasInit(SDL_INIT_JOYSTICK)); } void SdlInputHandler::handleKeyEvent(SDL_KeyboardEvent* event)