Skip to content

Commit f28acf9

Browse files
committed
Fix Input.get_joy_info() regression
SDL input driver did not have the "xinput_index", "raw_name", "vendor_id" and "product_id" fields for this method and exposed an additional, essentially useless for the users "mapping_handled" field. This commit fixes these issues.
1 parent efb40c1 commit f28acf9

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

core/input/input.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,9 @@ void Input::joy_connection_changed(int p_idx, bool p_connected, const String &p_
599599
}
600600
}
601601
}
602+
// We don't want this setting to be exposed to the user, because it's not very useful outside of this method.
603+
js.info.erase("mapping_handled");
604+
602605
_set_joypad_mapping(js, mapping);
603606
} else {
604607
js.connected = false;

doc/classes/Input.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,14 @@
130130
<param index="0" name="device" type="int" />
131131
<description>
132132
Returns a dictionary with extra platform-specific information about the device, e.g. the raw gamepad name from the OS or the Steam Input index.
133-
On Windows, the dictionary contains the following fields:
134-
[code]xinput_index[/code]: The index of the controller in the XInput system. Undefined for DirectInput devices.
135-
[code]vendor_id[/code]: The USB vendor ID of the device.
136-
[code]product_id[/code]: The USB product ID of the device.
137-
On Linux:
138-
[code]raw_name[/code]: The name of the controller as it came from the OS, before getting renamed by the godot controller database.
133+
On Windows, Linux, and macOS, the dictionary contains the following fields:
134+
[code]raw_name[/code]: The name of the controller as it came from the OS, before getting renamed by the controller database.
139135
[code]vendor_id[/code]: The USB vendor ID of the device.
140136
[code]product_id[/code]: The USB product ID of the device.
141137
[code]steam_input_index[/code]: The Steam Input gamepad index, if the device is not a Steam Input device this key won't be present.
142-
[b]Note:[/b] The returned dictionary is always empty on Web, iOS, Android, and macOS.
138+
On Windows, the dictionary can have an additional field:
139+
[code]xinput_index[/code]: The index of the controller in the XInput system. This key won't be present for devices not handled by XInput.
140+
[b]Note:[/b] The returned dictionary is always empty on Android, iOS, visionOS, and Web.
143141
</description>
144142
</method>
145143
<method name="get_joy_name">

drivers/sdl/joypad_sdl.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ void JoypadSDL::process_events() {
128128
print_error("A new joypad was attached but couldn't allocate a new id for it because joypad limit was reached.");
129129
} else {
130130
SDL_Joystick *joy = nullptr;
131+
SDL_Gamepad *gamepad = nullptr;
131132
String device_name;
132133

133134
// Gamepads must be opened with SDL_OpenGamepad to get their special remapped events
134135
if (SDL_IsGamepad(sdl_event.jdevice.which)) {
135-
SDL_Gamepad *gamepad = SDL_OpenGamepad(sdl_event.jdevice.which);
136+
gamepad = SDL_OpenGamepad(sdl_event.jdevice.which);
136137

137138
ERR_CONTINUE_MSG(!gamepad,
138139
vformat("Error opening gamepad at index %d: %s", sdl_event.jdevice.which, SDL_GetError()));
@@ -164,9 +165,22 @@ void JoypadSDL::process_events() {
164165

165166
sdl_instance_id_to_joypad_id.insert(sdl_event.jdevice.which, joy_id);
166167

167-
// Skip Godot's mapping system because SDL already handles the joypad's mapping
168168
Dictionary joypad_info;
169-
joypad_info["mapping_handled"] = true;
169+
joypad_info["mapping_handled"] = true; // Skip Godot's mapping system because SDL already handles the joypad's mapping.
170+
joypad_info["raw_name"] = String(SDL_GetJoystickName(joy));
171+
joypad_info["vendor_id"] = itos(SDL_GetJoystickVendor(joy));
172+
joypad_info["product_id"] = itos(SDL_GetJoystickProduct(joy));
173+
174+
const uint64_t steam_handle = SDL_GetGamepadSteamHandle(gamepad);
175+
if (steam_handle != 0) {
176+
joypad_info["steam_input_index"] = itos(steam_handle);
177+
}
178+
179+
const int player_index = SDL_GetJoystickPlayerIndex(joy);
180+
if (player_index >= 0) {
181+
// For XInput controllers SDL_GetJoystickPlayerIndex returns the XInput user index.
182+
joypad_info["xinput_index"] = itos(player_index);
183+
}
170184

171185
Input::get_singleton()->joy_connection_changed(
172186
joy_id,

0 commit comments

Comments
 (0)