| 1 | // Copyright 2010 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerInterface/SDL/SDL.h" |
| 5 | |
| 6 | #include <span> |
| 7 | #include <thread> |
| 8 | #include <vector> |
| 9 | |
| 10 | #ifdef _WIN32 |
| 11 | #include <Windows.h> |
| 12 | #endif |
| 13 | |
| 14 | #include <SDL3/SDL.h> |
| 15 | |
| 16 | #include "Common/Event.h" |
| 17 | #include "Common/Logging/Log.h" |
| 18 | #include "Common/ScopeGuard.h" |
| 19 | #include "Common/Thread.h" |
| 20 | |
| 21 | #include "Core/Config/MainSettings.h" |
| 22 | #include "Core/HW/SI/SI.h" |
| 23 | #include "Core/HW/SI/SI_Device.h" |
| 24 | |
| 25 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 26 | #include "InputCommon/ControllerInterface/SDL/SDLGamepad.h" |
| 27 | |
| 28 | namespace ciface::SDL |
| 29 | { |
| 30 | |
| 31 | class InputBackend final : public ciface::InputBackend |
| 32 | { |
| 33 | public: |
| 34 | InputBackend(ControllerInterface* controller_interface); |
| 35 | ~InputBackend() override; |
| 36 | void PopulateDevices() override; |
| 37 | void UpdateInput(std::vector<std::weak_ptr<ciface::Core::Device>>& devices_to_remove) override; |
| 38 | |
| 39 | private: |
| 40 | void OpenAndAddDevice(SDL_JoystickID instance_id); |
| 41 | |
| 42 | bool HandleEventAndContinue(const SDL_Event& e); |
| 43 | |
| 44 | Common::Event m_init_event; |
| 45 | Uint32 m_stop_event_type; |
| 46 | Uint32 m_populate_event_type; |
| 47 | std::thread m_hotplug_thread; |
| 48 | }; |
| 49 | |
| 50 | std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface) |
| 51 | { |
| 52 | return std::make_unique<InputBackend>(controller_interface); |
| 53 | } |
| 54 | |
| 55 | static void EnableSDLLogging() |
| 56 | { |
| 57 | SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE); |
| 58 | SDL_SetLogOutputFunction( |
| 59 | [](void*, int category, SDL_LogPriority priority, const char* message) { |
| 60 | std::string_view category_name{}; |
| 61 | switch (category) |
| 62 | { |
| 63 | case SDL_LOG_CATEGORY_APPLICATION: |
| 64 | category_name = "app"; |
| 65 | break; |
| 66 | case SDL_LOG_CATEGORY_ERROR: |
| 67 | category_name = "error"; |
| 68 | break; |
| 69 | case SDL_LOG_CATEGORY_ASSERT: |
| 70 | category_name = "assert"; |
| 71 | break; |
| 72 | case SDL_LOG_CATEGORY_SYSTEM: |
| 73 | category_name = "system"; |
| 74 | break; |
| 75 | case SDL_LOG_CATEGORY_AUDIO: |
| 76 | category_name = "audio"; |
| 77 | break; |
| 78 | case SDL_LOG_CATEGORY_VIDEO: |
| 79 | category_name = "video"; |
| 80 | break; |
| 81 | case SDL_LOG_CATEGORY_RENDER: |
| 82 | category_name = "render"; |
| 83 | break; |
| 84 | case SDL_LOG_CATEGORY_INPUT: |
| 85 | category_name = "input"; |
| 86 | break; |
| 87 | case SDL_LOG_CATEGORY_TEST: |
| 88 | category_name = "test"; |
| 89 | break; |
| 90 | case SDL_LOG_CATEGORY_GPU: |
| 91 | category_name = "gpu"; |
| 92 | break; |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | auto log_level = Common::Log::LogLevel::LNOTICE; |
| 98 | switch (priority) |
| 99 | { |
| 100 | case SDL_LOG_PRIORITY_VERBOSE: |
| 101 | case SDL_LOG_PRIORITY_DEBUG: |
| 102 | log_level = Common::Log::LogLevel::LDEBUG; |
| 103 | break; |
| 104 | case SDL_LOG_PRIORITY_INFO: |
| 105 | log_level = Common::Log::LogLevel::LINFO; |
| 106 | break; |
| 107 | case SDL_LOG_PRIORITY_WARN: |
| 108 | log_level = Common::Log::LogLevel::LWARNING; |
| 109 | break; |
| 110 | case SDL_LOG_PRIORITY_ERROR: |
| 111 | log_level = Common::Log::LogLevel::LERROR; |
| 112 | break; |
| 113 | case SDL_LOG_PRIORITY_CRITICAL: |
| 114 | default: |
| 115 | log_level = Common::Log::LogLevel::LNOTICE; |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | if (category_name.empty()) |
| 120 | { |
| 121 | GENERIC_LOG_FMT(Common::Log::LogType::CONTROLLERINTERFACE, log_level, "unknown({}): {}", |
| 122 | category, message); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | GENERIC_LOG_FMT(Common::Log::LogType::CONTROLLERINTERFACE, log_level, "{}: {}", |
| 127 | category_name, message); |
| 128 | } |
| 129 | }, |
| 130 | nullptr); |
| 131 | } |
| 132 | |
| 133 | InputBackend::InputBackend(ControllerInterface* controller_interface) |
| 134 | : ciface::InputBackend(controller_interface) |
| 135 | { |
| 136 | EnableSDLLogging(); |
| 137 | |
| 138 | if (Config::Get(Config::MAIN_SDL_HINT_JOYSTICK_ENHANCED_REPORTS) == "") |
| 139 | Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_ENHANCED_REPORTS, "1"); |
| 140 | |
| 141 | // We have our own WGI backend. Enabling SDL's WGI handling creates even more redundant devices. |
| 142 | if (Config::Get(Config::MAIN_SDL_HINT_JOYSTICK_WGI) == "") |
| 143 | Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_WGI, "0"); |
| 144 | |
| 145 | // Disable DualSense Player LEDs; We already colorize the Primary LED |
| 146 | if (Config::Get(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED) == "") |
| 147 | Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, "0"); |
| 148 | |
| 149 | // Disabling DirectInput support apparently solves hangs on shutdown for users with |
| 150 | // "8BitDo Ultimate 2" controllers, however, it also breaks hotplug support for Dual Sense |
| 151 | // and DS4 Controllers, so we leave it enabled for now. |
| 152 | // It also works around a possibly related random hang on a IDirectInputDevice8_Acquire |
| 153 | // call within SDL. |
| 154 | if (Config::Get(Config::MAIN_SDL_HINT_JOYSTICK_DIRECTINPUT) == "") |
| 155 | Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_DIRECTINPUT, "1"); |
| 156 | |
| 157 | // Pre-populate the default Joy-Con hints so they can be easily changed |
| 158 | if (Config::Get(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS) == "") |
| 159 | Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS, "1"); |
| 160 | |
| 161 | if (Config::Get(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS) == "") |
| 162 | Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS, "0"); |
| 163 | |
| 164 | // Disable SDL's GC Adapter handling when we want to handle it ourselves. |
| 165 | bool is_gc_adapter_configured = false; |
| 166 | for (int i = 0; i != SerialInterface::MAX_SI_CHANNELS; ++i) |
| 167 | { |
| 168 | if (Config::Get(Config::GetInfoForSIDevice(i)) == SerialInterface::SIDEVICE_WIIU_ADAPTER) |
| 169 | { |
| 170 | is_gc_adapter_configured = true; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | // TODO: This hint should be adjusted when the config changes, |
| 175 | // but SDL requires it be set before joystick initialization, |
| 176 | // and ControllerInterface isn't prepared for SDL to spontaneously re-initialize itself. |
| 177 | SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, is_gc_adapter_configured ? "0" : "1"); |
| 178 | |
| 179 | // Load all the hints from the config file |
| 180 | std::shared_ptr<Config::Layer> layer = Config::GetLayer(Config::LayerType::Base); |
| 181 | const Config::Section& section = layer->GetSection(Config::System::Main, "SDL_Hints"); |
| 182 | for (auto& row_data : section) |
| 183 | { |
| 184 | const Config::Location& location = row_data.first; |
| 185 | const std::optional<std::string>& value = row_data.second; |
| 186 | |
| 187 | if (value) |
| 188 | SDL_SetHint(location.key.c_str(), value->c_str()); |
| 189 | } |
| 190 | |
| 191 | m_hotplug_thread = std::thread([this] { |
| 192 | Common::SetCurrentThreadName("SDL Hotplug Thread"); |
| 193 | |
| 194 | Common::ScopeGuard quit_guard([] { |
| 195 | // TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up |
| 196 | SDL_Quit(); |
| 197 | }); |
| 198 | { |
| 199 | Common::ScopeGuard init_guard([this] { m_init_event.Set(); }); |
| 200 | |
| 201 | if (!SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMEPAD)) |
| 202 | { |
| 203 | ERROR_LOG_FMT(CONTROLLERINTERFACE, "SDL failed to initialize"); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | const Uint32 custom_events_start = SDL_RegisterEvents(2); |
| 208 | if (custom_events_start == static_cast<Uint32>(-1)) |
| 209 | { |
| 210 | ERROR_LOG_FMT(CONTROLLERINTERFACE, "SDL failed to register custom events"); |
| 211 | return; |
| 212 | } |
| 213 | m_stop_event_type = custom_events_start; |
| 214 | m_populate_event_type = custom_events_start + 1; |
| 215 | |
| 216 | // Drain all of the events and add the initial joysticks before returning. Otherwise, the |
| 217 | // individual joystick events as well as the custom populate event will be handled _after_ |
| 218 | // ControllerInterface::Init/RefreshDevices has cleared its list of devices, resulting in |
| 219 | // duplicate devices. Adding devices will actually "fail" here, as the ControllerInterface |
| 220 | // hasn't finished initializing yet. |
| 221 | SDL_Event e; |
| 222 | while (SDL_PollEvent(&e)) |
| 223 | { |
| 224 | if (!HandleEventAndContinue(e)) |
| 225 | return; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | SDL_Event e; |
| 230 | while (SDL_WaitEvent(&e)) |
| 231 | { |
| 232 | if (!HandleEventAndContinue(e)) |
| 233 | return; |
| 234 | } |
| 235 | }); |
| 236 | |
| 237 | m_init_event.Wait(); |
| 238 | } |
| 239 | |
| 240 | InputBackend::~InputBackend() |
| 241 | { |
| 242 | if (!m_hotplug_thread.joinable()) |
| 243 | return; |
| 244 | |
| 245 | SDL_Event stop_event{m_stop_event_type}; |
| 246 | SDL_PushEvent(&stop_event); |
| 247 | |
| 248 | m_hotplug_thread.join(); |
| 249 | } |
| 250 | |
| 251 | void InputBackend::PopulateDevices() |
| 252 | { |
| 253 | if (!m_hotplug_thread.joinable()) |
| 254 | return; |
| 255 | |
| 256 | SDL_Event populate_event{m_populate_event_type}; |
| 257 | SDL_PushEvent(&populate_event); |
| 258 | } |
| 259 | |
| 260 | void InputBackend::UpdateInput(std::vector<std::weak_ptr<ciface::Core::Device>>&) |
| 261 | { |
| 262 | SDL_UpdateGamepads(); |
| 263 | } |
| 264 | |
| 265 | void InputBackend::OpenAndAddDevice(SDL_JoystickID instance_id) |
| 266 | { |
| 267 | SDL_Gamepad* gc = SDL_OpenGamepad(instance_id); |
| 268 | SDL_Joystick* js = SDL_OpenJoystick(instance_id); |
| 269 | |
| 270 | if (js != nullptr) |
| 271 | { |
| 272 | if (SDL_GetNumJoystickButtons(js) > 255 || SDL_GetNumJoystickAxes(js) > 255 || |
| 273 | SDL_GetNumJoystickHats(js) > 255 || SDL_GetNumJoystickBalls(js) > 255) |
| 274 | { |
| 275 | // This device is invalid, don't use it |
| 276 | // Some crazy devices (HP webcam 2100) end up as HID devices |
| 277 | // SDL tries parsing these as Joysticks |
| 278 | return; |
| 279 | } |
| 280 | auto gamepad = std::make_shared<Gamepad>(gc, js); |
| 281 | if (!gamepad->Inputs().empty() || !gamepad->Outputs().empty()) |
| 282 | GetControllerInterface().AddDevice(std::move(gamepad)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | bool InputBackend::HandleEventAndContinue(const SDL_Event& e) |
| 287 | { |
| 288 | if (e.type == SDL_EVENT_JOYSTICK_ADDED) |
| 289 | { |
| 290 | OpenAndAddDevice(e.jdevice.which); |
| 291 | } |
| 292 | else if (e.type == SDL_EVENT_JOYSTICK_REMOVED) |
| 293 | { |
| 294 | GetControllerInterface().RemoveDevice([&e](const auto* device) { |
| 295 | return device->GetSource() == "SDL" && |
| 296 | static_cast<const Gamepad*>(device)->GetSDLInstanceID() == e.jdevice.which; |
| 297 | }); |
| 298 | } |
| 299 | else if (e.type == m_populate_event_type) |
| 300 | { |
| 301 | GetControllerInterface().PlatformPopulateDevices([this] { |
| 302 | int joystick_count = 0; |
| 303 | auto* const joystick_ids = SDL_GetJoysticks(&joystick_count); |
| 304 | for (auto instance_id : std::span(joystick_ids, joystick_count)) |
| 305 | OpenAndAddDevice(instance_id); |
| 306 | |
| 307 | SDL_free(joystick_ids); |
| 308 | }); |
| 309 | } |
| 310 | else if (e.type == m_stop_event_type) |
| 311 | { |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | } // namespace ciface::SDL |