From 73065898c8984900b791dbeab77997b7ee63df20 Mon Sep 17 00:00:00 2001 From: Random06457 <28494085+Random06457@users.noreply.github.com> Date: Wed, 11 May 2022 14:13:54 +0900 Subject: [PATCH] fix rumble support for linux --- libultraship/libultraship/SDLController.cpp | 34 ++++++++++----------- libultraship/libultraship/SDLController.h | 8 +++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/libultraship/libultraship/SDLController.cpp b/libultraship/libultraship/SDLController.cpp index c710d45c9..ab2c48b30 100644 --- a/libultraship/libultraship/SDLController.cpp +++ b/libultraship/libultraship/SDLController.cpp @@ -61,17 +61,17 @@ namespace Ship { if (Conf[ConfSection]["GUID"].compare("") == 0 || Conf[ConfSection]["GUID"].compare(INVALID_SDL_CONTROLLER_GUID) == 0 || Conf[ConfSection]["GUID"].compare(NewGuid) == 0) { auto NewCont = SDL_GameControllerOpen(i); - if (SDL_GameControllerHasSensor(NewCont, SDL_SENSOR_GYRO)) - { - SDL_GameControllerSetSensorEnabled(NewCont, SDL_SENSOR_GYRO, SDL_TRUE); - } - // We failed to load the controller. Go to next. if (NewCont == nullptr) { SPDLOG_ERROR("SDL Controller failed to open: ({})", SDL_GetError()); continue; } + if (SDL_GameControllerHasSensor(NewCont, SDL_SENSOR_GYRO)) + { + SDL_GameControllerSetSensorEnabled(NewCont, SDL_SENSOR_GYRO, SDL_TRUE); + } + guid = NewGuid; Cont = NewCont; @@ -101,10 +101,9 @@ namespace Ship { } bool SDLController::Close() { - // LINUX_TODO: - // if (SDL_GameControllerHasRumble(Cont)) { - // SDL_GameControllerRumble(Cont, 0, 0, 0); - // } + if (CanRumble()) { + SDL_GameControllerRumble(Cont, 0, 0, 0); + } if (Cont != nullptr) { SDL_GameControllerClose(Cont); } @@ -348,15 +347,14 @@ namespace Ship { void SDLController::WriteToSource(ControllerCallback* controller) { - // LINUX_TODO: - // if (SDL_GameControllerHasRumble(Cont)) { - // if (controller->rumble > 0) { - // float rumble_strength = CVar_GetFloat(StringHelper::Sprintf("gCont%i_RumbleStrength", GetControllerNumber()).c_str(), 1.0f); - // SDL_GameControllerRumble(Cont, 0xFFFF * rumble_strength, 0xFFFF * rumble_strength, 0); - // } else { - // SDL_GameControllerRumble(Cont, 0, 0, 0); - // } - // } + if (CanRumble()) { + if (controller->rumble > 0) { + float rumble_strength = CVar_GetFloat(StringHelper::Sprintf("gCont%i_RumbleStrength", GetControllerNumber()).c_str(), 1.0f); + SDL_GameControllerRumble(Cont, 0xFFFF * rumble_strength, 0xFFFF * rumble_strength, 0); + } else { + SDL_GameControllerRumble(Cont, 0, 0, 0); + } + } if (SDL_GameControllerHasLED(Cont)) { switch (controller->ledColor) { diff --git a/libultraship/libultraship/SDLController.h b/libultraship/libultraship/SDLController.h index e150bf0ab..fbffa478f 100644 --- a/libultraship/libultraship/SDLController.h +++ b/libultraship/libultraship/SDLController.h @@ -13,8 +13,12 @@ namespace Ship { void ReadFromSource(); void WriteToSource(ControllerCallback* controller); bool Connected() const { return Cont != nullptr; } - // LINUX_TODO: - bool CanRumble() const { return false; /* return SDL_GameControllerHasRumble(Cont); */ } + bool CanRumble() const { +#if SDL_COMPILEDVERSION >= SDL_VERSIONNUM(2,0,18) + return SDL_GameControllerHasRumble(Cont); +#endif + return true; + } std::string GetGuid() { return guid; };