| 1 | /**************************************************************************/ |
| 2 | /* joypad_sdl.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "joypad_sdl.h" |
| 32 | |
| 33 | #ifdef SDL_ENABLED |
| 34 | |
| 35 | #include "core/input/default_controller_mappings.h" |
| 36 | #include "core/os/time.h" |
| 37 | #include "core/variant/dictionary.h" |
| 38 | |
| 39 | #include <SDL3/SDL.h> |
| 40 | #include <SDL3/SDL_error.h> |
| 41 | #include <SDL3/SDL_events.h> |
| 42 | #include <SDL3/SDL_gamepad.h> |
| 43 | #include <SDL3/SDL_iostream.h> |
| 44 | #include <SDL3/SDL_joystick.h> |
| 45 | |
| 46 | JoypadSDL *JoypadSDL::singleton = nullptr; |
| 47 | |
| 48 | // Macro to skip the SDL joystick event handling if the device is an SDL gamepad, because |
| 49 | // there are separate events for SDL gamepads |
| 50 | #define SKIP_EVENT_FOR_GAMEPAD \ |
| 51 | if (SDL_IsGamepad(sdl_event.jdevice.which)) { \ |
| 52 | continue; \ |
| 53 | } |
| 54 | |
| 55 | JoypadSDL::JoypadSDL() { |
| 56 | singleton = this; |
| 57 | } |
| 58 | |
| 59 | JoypadSDL::~JoypadSDL() { |
| 60 | // Process any remaining input events |
| 61 | process_events(); |
| 62 | for (int i = 0; i < Input::JOYPADS_MAX; i++) { |
| 63 | if (joypads[i].attached) { |
| 64 | close_joypad(i); |
| 65 | } |
| 66 | } |
| 67 | SDL_Quit(); |
| 68 | singleton = nullptr; |
| 69 | } |
| 70 | |
| 71 | JoypadSDL *JoypadSDL::get_singleton() { |
| 72 | return singleton; |
| 73 | } |
| 74 | |
| 75 | Error JoypadSDL::initialize() { |
| 76 | SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1"); |
| 77 | SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1"); |
| 78 | ERR_FAIL_COND_V_MSG(!SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD), FAILED, SDL_GetError()); |
| 79 | |
| 80 | // Add Godot's mapping database from memory |
| 81 | int i = 0; |
| 82 | while (DefaultControllerMappings::mappings[i]) { |
| 83 | String mapping_string = DefaultControllerMappings::mappings[i++]; |
| 84 | CharString data = mapping_string.utf8(); |
| 85 | SDL_IOStream *rw = SDL_IOFromMem((void *)data.ptr(), data.size()); |
| 86 | SDL_AddGamepadMappingsFromIO(rw, 1); |
| 87 | } |
| 88 | |
| 89 | // Make sure that we handle already connected joypads when the driver is initialized. |
| 90 | process_events(); |
| 91 | |
| 92 | print_verbose("SDL: Init OK!"); |
| 93 | return OK; |
| 94 | } |
| 95 | |
| 96 | void JoypadSDL::process_events() { |
| 97 | // Update rumble first for it to be applied when we handle SDL events |
| 98 | for (int i = 0; i < Input::JOYPADS_MAX; i++) { |
| 99 | Joypad &joy = joypads[i]; |
| 100 | if (joy.attached && joy.supports_force_feedback) { |
| 101 | uint64_t timestamp = Input::get_singleton()->get_joy_vibration_timestamp(i); |
| 102 | |
| 103 | // Update the joypad rumble only if there was a new vibration request |
| 104 | if (timestamp > joy.ff_effect_timestamp) { |
| 105 | joy.ff_effect_timestamp = timestamp; |
| 106 | |
| 107 | SDL_Joystick *sdl_joy = SDL_GetJoystickFromID(joypads[i].sdl_instance_idx); |
| 108 | Vector2 strength = Input::get_singleton()->get_joy_vibration_strength(i); |
| 109 | |
| 110 | /* |
| 111 | If the vibration was requested to start, SDL_RumbleJoystick will start it. |
| 112 | If the vibration was requested to stop, strength and duration will be 0, so SDL will stop the rumble. |
| 113 | |
| 114 | Here strength.y goes first and then strength.x, because Input.get_joy_vibration_strength().x |
| 115 | is vibration's weak magnitude (high frequency rumble), and .y is strong magnitude (low frequency rumble), |
| 116 | SDL_RumbleJoystick takes low frequency rumble first and then high frequency rumble. |
| 117 | */ |
| 118 | SDL_RumbleJoystick( |
| 119 | sdl_joy, |
| 120 | // Rumble strength goes from 0 to 0xFFFF |
| 121 | strength.y * UINT16_MAX, |
| 122 | strength.x * UINT16_MAX, |
| 123 | Input::get_singleton()->get_joy_vibration_duration(i) * 1000); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | SDL_Event sdl_event; |
| 129 | while (SDL_PollEvent(&sdl_event)) { |
| 130 | // A new joypad was attached |
| 131 | if (sdl_event.type == SDL_EVENT_JOYSTICK_ADDED) { |
| 132 | int joy_id = Input::get_singleton()->get_unused_joy_id(); |
| 133 | if (joy_id == -1) { |
| 134 | // There is no space for more joypads... |
| 135 | print_error("A new joypad was attached but couldn't allocate a new id for it because joypad limit was reached."); |
| 136 | } else { |
| 137 | SDL_Joystick *joy = nullptr; |
| 138 | SDL_Gamepad *gamepad = nullptr; |
| 139 | String device_name; |
| 140 | |
| 141 | // Gamepads must be opened with SDL_OpenGamepad to get their special remapped events |
| 142 | if (SDL_IsGamepad(sdl_event.jdevice.which)) { |
| 143 | gamepad = SDL_OpenGamepad(sdl_event.jdevice.which); |
| 144 | |
| 145 | ERR_CONTINUE_MSG(!gamepad, |
| 146 | vformat("Error opening gamepad at index %d: %s", sdl_event.jdevice.which, SDL_GetError())); |
| 147 | |
| 148 | device_name = SDL_GetGamepadName(gamepad); |
| 149 | joy = SDL_GetGamepadJoystick(gamepad); |
| 150 | |
| 151 | print_verbose(vformat("SDL: Gamepad %s connected", SDL_GetGamepadName(gamepad))); |
| 152 | } else { |
| 153 | joy = SDL_OpenJoystick(sdl_event.jdevice.which); |
| 154 | ERR_CONTINUE_MSG(!joy, |
| 155 | vformat("Error opening joystick at index %d: %s", sdl_event.jdevice.which, SDL_GetError())); |
| 156 | |
| 157 | device_name = SDL_GetJoystickName(joy); |
| 158 | |
| 159 | print_verbose(vformat("SDL: Joystick %s connected", SDL_GetJoystickName(joy))); |
| 160 | } |
| 161 | |
| 162 | const int MAX_GUID_SIZE = 64; |
| 163 | char guid[MAX_GUID_SIZE] = {}; |
| 164 | |
| 165 | SDL_GUIDToString(SDL_GetJoystickGUID(joy), guid, MAX_GUID_SIZE); |
| 166 | SDL_PropertiesID propertiesID = SDL_GetJoystickProperties(joy); |
| 167 | |
| 168 | joypads[joy_id].attached = true; |
| 169 | joypads[joy_id].sdl_instance_idx = sdl_event.jdevice.which; |
| 170 | joypads[joy_id].supports_force_feedback = SDL_GetBooleanProperty(propertiesID, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false); |
| 171 | joypads[joy_id].guid = StringName(String(guid)); |
| 172 | |
| 173 | sdl_instance_id_to_joypad_id.insert(sdl_event.jdevice.which, joy_id); |
| 174 | |
| 175 | Dictionary joypad_info; |
| 176 | // Skip Godot's mapping system if SDL already handles the joypad's mapping. |
| 177 | joypad_info["mapping_handled"] = SDL_IsGamepad(sdl_event.jdevice.which); |
| 178 | joypad_info["raw_name"] = String::utf8(SDL_GetJoystickName(joy)); |
| 179 | joypad_info["vendor_id"] = itos(SDL_GetJoystickVendor(joy)); |
| 180 | joypad_info["product_id"] = itos(SDL_GetJoystickProduct(joy)); |
| 181 | |
| 182 | const uint64_t steam_handle = SDL_GetGamepadSteamHandle(gamepad); |
| 183 | if (steam_handle != 0) { |
| 184 | joypad_info["steam_input_index"] = itos(steam_handle); |
| 185 | } |
| 186 | |
| 187 | const int player_index = SDL_GetJoystickPlayerIndex(joy); |
| 188 | if (player_index >= 0) { |
| 189 | // For XInput controllers SDL_GetJoystickPlayerIndex returns the XInput user index. |
| 190 | joypad_info["xinput_index"] = itos(player_index); |
| 191 | } |
| 192 | |
| 193 | Input::get_singleton()->joy_connection_changed( |
| 194 | joy_id, |
| 195 | true, |
| 196 | device_name, |
| 197 | joypads[joy_id].guid, |
| 198 | joypad_info); |
| 199 | |
| 200 | Input::get_singleton()->set_joy_features(joy_id, &joypads[joy_id]); |
| 201 | } |
| 202 | // An event for an attached joypad |
| 203 | } else if (sdl_event.type >= SDL_EVENT_JOYSTICK_AXIS_MOTION && sdl_event.type < SDL_EVENT_FINGER_DOWN && sdl_instance_id_to_joypad_id.has(sdl_event.jdevice.which)) { |
| 204 | int joy_id = sdl_instance_id_to_joypad_id.get(sdl_event.jdevice.which); |
| 205 | |
| 206 | switch (sdl_event.type) { |
| 207 | case SDL_EVENT_JOYSTICK_REMOVED: |
| 208 | Input::get_singleton()->joy_connection_changed(joy_id, false, ""); |
| 209 | close_joypad(joy_id); |
| 210 | break; |
| 211 | |
| 212 | case SDL_EVENT_JOYSTICK_AXIS_MOTION: |
| 213 | SKIP_EVENT_FOR_GAMEPAD; |
| 214 | |
| 215 | Input::get_singleton()->joy_axis( |
| 216 | joy_id, |
| 217 | static_cast<JoyAxis>(sdl_event.jaxis.axis), // Godot joy axis constants are already intentionally the same as SDL's |
| 218 | ((sdl_event.jaxis.value - SDL_JOYSTICK_AXIS_MIN) / (float)(SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) - 0.5f) * 2.0f); |
| 219 | break; |
| 220 | |
| 221 | case SDL_EVENT_JOYSTICK_BUTTON_UP: |
| 222 | case SDL_EVENT_JOYSTICK_BUTTON_DOWN: |
| 223 | SKIP_EVENT_FOR_GAMEPAD; |
| 224 | |
| 225 | // Some devices report pressing buttons with indices like 232+, 241+, etc. that are not valid, |
| 226 | // so we ignore them here. |
| 227 | if (sdl_event.jbutton.button >= (int)JoyButton::MAX) { |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | Input::get_singleton()->joy_button( |
| 232 | joy_id, |
| 233 | static_cast<JoyButton>(sdl_event.jbutton.button), // Godot button constants are intentionally the same as SDL's, so we can just straight up use them |
| 234 | sdl_event.jbutton.down); |
| 235 | break; |
| 236 | |
| 237 | case SDL_EVENT_JOYSTICK_HAT_MOTION: |
| 238 | SKIP_EVENT_FOR_GAMEPAD; |
| 239 | |
| 240 | Input::get_singleton()->joy_hat( |
| 241 | joy_id, |
| 242 | (HatMask)sdl_event.jhat.value // Godot hat masks are identical to SDL hat masks, so we can just use them as-is. |
| 243 | ); |
| 244 | break; |
| 245 | |
| 246 | case SDL_EVENT_GAMEPAD_AXIS_MOTION: { |
| 247 | float axis_value; |
| 248 | |
| 249 | if (sdl_event.gaxis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER || sdl_event.gaxis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) { |
| 250 | // Gamepad triggers go from 0 to SDL_JOYSTICK_AXIS_MAX |
| 251 | axis_value = sdl_event.gaxis.value / (float)SDL_JOYSTICK_AXIS_MAX; |
| 252 | } else { |
| 253 | // Other axis go from SDL_JOYSTICK_AXIS_MIN to SDL_JOYSTICK_AXIS_MAX |
| 254 | axis_value = |
| 255 | ((sdl_event.gaxis.value - SDL_JOYSTICK_AXIS_MIN) / (float)(SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) - 0.5f) * 2.0f; |
| 256 | } |
| 257 | |
| 258 | Input::get_singleton()->joy_axis( |
| 259 | joy_id, |
| 260 | static_cast<JoyAxis>(sdl_event.gaxis.axis), // Godot joy axis constants are already intentionally the same as SDL's |
| 261 | axis_value); |
| 262 | } break; |
| 263 | |
| 264 | // Do note SDL gamepads do not have separate events for the dpad |
| 265 | case SDL_EVENT_GAMEPAD_BUTTON_UP: |
| 266 | case SDL_EVENT_GAMEPAD_BUTTON_DOWN: |
| 267 | Input::get_singleton()->joy_button( |
| 268 | joy_id, |
| 269 | static_cast<JoyButton>(sdl_event.gbutton.button), // Godot button constants are intentionally the same as SDL's, so we can just straight up use them |
| 270 | sdl_event.gbutton.down); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void JoypadSDL::close_joypad(int p_pad_idx) { |
| 278 | int sdl_instance_idx = joypads[p_pad_idx].sdl_instance_idx; |
| 279 | |
| 280 | joypads[p_pad_idx].attached = false; |
| 281 | sdl_instance_id_to_joypad_id.erase(sdl_instance_idx); |
| 282 | |
| 283 | if (SDL_IsGamepad(sdl_instance_idx)) { |
| 284 | SDL_Gamepad *gamepad = SDL_GetGamepadFromID(sdl_instance_idx); |
| 285 | SDL_CloseGamepad(gamepad); |
| 286 | } else { |
| 287 | SDL_Joystick *joy = SDL_GetJoystickFromID(sdl_instance_idx); |
| 288 | SDL_CloseJoystick(joy); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | bool JoypadSDL::Joypad::has_joy_light() const { |
| 293 | SDL_PropertiesID properties_id = SDL_GetJoystickProperties(get_sdl_joystick()); |
| 294 | if (properties_id == 0) { |
| 295 | return false; |
| 296 | } |
| 297 | return SDL_GetBooleanProperty(properties_id, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, false) || SDL_GetBooleanProperty(properties_id, SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN, false); |
| 298 | } |
| 299 | |
| 300 | void JoypadSDL::Joypad::set_joy_light(const Color &p_color) { |
| 301 | SDL_SetJoystickLED(get_sdl_joystick(), p_color.get_r8(), p_color.get_g8(), p_color.get_b8()); |
| 302 | } |
| 303 | |
| 304 | SDL_Joystick *JoypadSDL::Joypad::get_sdl_joystick() const { |
| 305 | return SDL_GetJoystickFromID(sdl_instance_idx); |
| 306 | } |
| 307 | |
| 308 | SDL_Gamepad *JoypadSDL::Joypad::get_sdl_gamepad() const { |
| 309 | return SDL_GetGamepadFromID(sdl_instance_idx); |
| 310 | } |
| 311 | |
| 312 | #endif // SDL_ENABLED |