Qt: Add Controller LED options (where supported)

This commit is contained in:
Stenzek
2023-01-15 16:40:35 +10:00
parent 722771fff6
commit c393db419e
15 changed files with 331 additions and 10 deletions

View File

@ -101,8 +101,8 @@ if(SDL2_FOUND)
sdl_input_source.h
)
target_compile_definitions(frontend-common PUBLIC "WITH_SDL2=1")
target_include_directories(frontend-common PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(frontend-common PRIVATE ${SDL2_LIBRARIES})
target_include_directories(frontend-common PUBLIC ${SDL2_INCLUDE_DIRS})
target_link_libraries(frontend-common PUBLIC ${SDL2_LIBRARIES})
# Copy bundled SDL2 to output on Windows.
if(WIN32)

View File

@ -88,6 +88,18 @@ static constexpr const char* s_sdl_hat_direction_names[] = {
// clang-format on
};
static constexpr const char* s_sdl_default_led_colors[] = {
"0000ff", // SDL-0
"ff0000", // SDL-1
"00ff00", // SDL-2
"ffff00", // SDL-3
};
static void SetControllerRGBLED(SDL_GameController* gc, u32 color)
{
SDL_GameControllerSetLED(gc, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff);
}
SDLInputSource::SDLInputSource() = default;
SDLInputSource::~SDLInputSource()
@ -149,6 +161,39 @@ void SDLInputSource::LoadSettings(SettingsInterface& si)
{
m_controller_enhanced_mode = si.GetBoolValue("InputSources", "SDLControllerEnhancedMode", false);
m_sdl_hints = si.GetKeyValueList("SDLHints");
for (u32 i = 0; i < MAX_LED_COLORS; i++)
{
const u32 color = GetRGBForPlayerId(si, i);
if (m_led_colors[i] == color)
continue;
m_led_colors[i] = color;
const auto it = GetControllerDataForPlayerId(i);
if (it == m_controllers.end() || !it->game_controller || !SDL_GameControllerHasLED(it->game_controller))
continue;
SetControllerRGBLED(it->game_controller, color);
}
}
u32 SDLInputSource::GetRGBForPlayerId(SettingsInterface& si, u32 player_id)
{
return ParseRGBForPlayerId(
si.GetStringValue("SDLExtra", fmt::format("Player{}LED", player_id).c_str(), s_sdl_default_led_colors[player_id]),
player_id);
}
u32 SDLInputSource::ParseRGBForPlayerId(const std::string_view& str, u32 player_id)
{
if (player_id >= MAX_LED_COLORS)
return 0;
const u32 default_color = StringUtil::FromChars<u32>(s_sdl_default_led_colors[player_id], 16).value_or(0);
const u32 color = StringUtil::FromChars<u32>(str, 16).value_or(default_color);
return color;
}
void SDLInputSource::SetHints()
@ -624,6 +669,12 @@ bool SDLInputSource::OpenDevice(int index, bool is_gamecontroller)
if (!cd.haptic && !cd.use_game_controller_rumble)
Log_VerbosePrintf("(SDLInputSource) Rumble is not supported on '%s'", name);
if (player_id >= 0 && static_cast<u32>(player_id) < MAX_LED_COLORS && gcontroller &&
SDL_GameControllerHasLED(gcontroller))
{
SetControllerRGBLED(gcontroller, m_led_colors[player_id]);
}
m_controllers.push_back(std::move(cd));
InputManager::OnInputDeviceConnected(StringUtil::StdStringFromFormat("SDL-%d", player_id), name);

View File

@ -14,6 +14,8 @@ class SettingsInterface;
class SDLInputSource final : public InputSource
{
public:
static constexpr u32 MAX_LED_COLORS = 4;
SDLInputSource();
~SDLInputSource();
@ -38,6 +40,9 @@ public:
SDL_Joystick* GetJoystickForDevice(const std::string_view& device);
static u32 GetRGBForPlayerId(SettingsInterface& si, u32 player_id);
static u32 ParseRGBForPlayerId(const std::string_view& str, u32 player_id);
private:
struct ControllerData
{
@ -82,5 +87,6 @@ private:
bool m_sdl_subsystem_initialized = false;
bool m_controller_enhanced_mode = false;
std::array<u32, MAX_LED_COLORS> m_led_colors{};
std::vector<std::pair<std::string, std::string>> m_sdl_hints;
};