Improve Android controller matching (#1274)

* improve android controller matching

* more

* more

* more

* more

* more

* more

* more

* more

* more

* more

* more

* more
This commit is contained in:
Logan McNaughton authored and GitHub committed 2026-10-15 10:58:28 +00:00
1 parent 9b312ea60d
commit 3a49a19b07
3 files changed
+82 -42

No files matched your search

+24 -7
View File
@@ -170,7 +170,9 @@ bind_java_type! {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ControllerInfo {
pub name: String,
/// Stable ID from [`InputDevice.getDescriptor`](https://developer.android.com/reference/android/view/InputDevice#getDescriptor()).
/// Identity used to match a stored controller assignment, formatted as
/// `"vendor_id:product_id"`. The running game rebuilds the same string from
/// SDL (see [`android_controller_identity`]).
pub descriptor: String,
}
@@ -514,13 +516,28 @@ fn list_controllers_on_jvm(env: &mut Env<'_>) -> jni::errors::Result<Vec<Control
} else {
ui::input::UNKNOWN_CONTROLLER_NAME.to_string()
};
let descriptor = if let Ok(product_id) = device.get_product_id(env)
&& let Ok(vendor_id) = device.get_vendor_id(env)
{
format!("{}:{}:{}", name, vendor_id, product_id)
} else {
String::new()
let vendor_id = match device.get_vendor_id(env) {
Ok(vendor_id) => vendor_id,
Err(err) => {
eprintln!(
"Skipping Android controller {device_id}: could not read vendor id ({err:?})"
);
continue;
}
};
let product_id = match device.get_product_id(env) {
Ok(product_id) => product_id,
Err(err) => {
eprintln!(
"Skipping Android controller {device_id}: could not read product id ({err:?})"
);
continue;
}
};
// Build the identity the running game will match against. The game can
// rebuild this string purely from SDL (see `ui::input::init`).
let descriptor = format!("{vendor_id}:{product_id}");
controllers.push(ControllerInfo { name, descriptor });
}
+37 -33
View File
@@ -421,7 +421,8 @@ pub fn get(ui: &mut ui::Ui, channel: usize) -> InputData {
#[cfg(target_os = "android")]
{
if channel == 0
&& ui.config.input.controller_assignment[0].is_none()
&& controller.is_null()
&& joystick.is_null()
&& let Ok(touch_overlay) = ui::android::TOUCH_OVERLAY.lock()
{
x = normalize_axis_position(touch_overlay.x);
@@ -459,18 +460,38 @@ pub fn get(ui: &mut ui::Ui, channel: usize) -> InputData {
}
}
fn android_controller_identity(joystick_id: sdl3_sys::joystick::SDL_JoystickID) -> String {
let vendor_id = unsafe { sdl3_sys::joystick::SDL_GetJoystickVendorForID(joystick_id) };
let product_id = unsafe { sdl3_sys::joystick::SDL_GetJoystickProductForID(joystick_id) };
format!("{vendor_id}:{product_id}")
}
fn joystick_available(joystick_id: sdl3_sys::joystick::SDL_JoystickID) -> bool {
unsafe { sdl3_sys::joystick::SDL_GetJoystickFromID(joystick_id) }.is_null()
&& unsafe { sdl3_sys::gamepad::SDL_GetGamepadFromID(joystick_id) }.is_null()
}
pub fn assign_controller(config: &mut ui::config::Config, controller: i32, port: usize) {
let joysticks = get_joysticks();
if controller < 0 {
config.input.controller_assignment[port - 1] = None;
} else if controller < joysticks.len() as i32 {
let path =
unsafe { sdl3_sys::joystick::SDL_GetJoystickPathForID(joysticks[controller as usize]) };
if !path.is_null() {
let joystick_id = joysticks[controller as usize];
if cfg!(target_os = "android") {
// See `init`/`android_controller_identity`: Android devices are
// matched on vendor:product, because the GUI and the running game
// observe them through different APIs that can disagree on other
// details like the device name.
config.input.controller_assignment[port - 1] =
Some(unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap().to_string() });
Some(android_controller_identity(joystick_id));
} else {
eprintln!("Invalid controller path for controller {controller}");
let path = unsafe { sdl3_sys::joystick::SDL_GetJoystickPathForID(joystick_id) };
if !path.is_null() {
config.input.controller_assignment[port - 1] =
Some(unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap().to_string() });
} else {
eprintln!("Invalid controller path for controller {controller}");
}
}
} else {
eprintln!("Invalid controller number")
@@ -523,36 +544,19 @@ pub fn init(ui: &mut ui::Ui) {
let mut joystick_id = sdl3_sys::everything::SDL_JoystickID(0);
for joystick in get_joysticks().iter() {
let path = if cfg!(target_os = "android") {
let name = if let name =
unsafe { sdl3_sys::joystick::SDL_GetJoystickNameForID(*joystick) }
&& !name.is_null()
{
unsafe { std::ffi::CStr::from_ptr(name).to_str().unwrap() }.to_string()
} else {
UNKNOWN_CONTROLLER_NAME.to_string()
};
let vendor_id =
unsafe { sdl3_sys::joystick::SDL_GetJoystickVendorForID(*joystick) };
let product_id =
unsafe { sdl3_sys::joystick::SDL_GetJoystickProductForID(*joystick) };
Some(format!("{}:{}:{}", name, vendor_id, product_id))
let is_match = if cfg!(target_os = "android") {
// The Slint GUI enumerates controllers through the Android
// InputDevice API while the running game sees them through
// SDL, so the two can disagree on details like the device
// name. Match on vendor:product, which both agree on.
*controller_assignment == android_controller_identity(*joystick)
} else {
let path = unsafe { sdl3_sys::joystick::SDL_GetJoystickPathForID(*joystick) };
if !path.is_null() {
Some(
unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }.to_string(),
)
} else {
None
}
!path.is_null()
&& unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }
== *controller_assignment
};
if let Some(path) = path
&& path == *controller_assignment
&& unsafe { sdl3_sys::joystick::SDL_GetJoystickFromID(*joystick) }.is_null()
&& unsafe { sdl3_sys::gamepad::SDL_GetGamepadFromID(*joystick) }.is_null()
{
if is_match && joystick_available(*joystick) {
joystick_id = *joystick;
break;
}
+21 -2
View File
@@ -175,6 +175,7 @@ fn draw_profile_screen(
) -> bool {
let mut selected: (usize, usize) = (0, 0);
let mut auto_advance = dinput && !unsafe { sdl3_sys::keyboard::SDL_HasKeyboard() };
let mut awaiting_axis_release: Option<i32> = None;
let (mut gather, mut gather_timer) = reset_gather(auto_advance);
loop {
@@ -279,9 +280,16 @@ fn draw_profile_screen(
sdl3_sys::events::SDL_EVENT_GAMEPAD_AXIS_MOTION => {
let axis_value = unsafe { event.gaxis }.value;
let axis = unsafe { event.gaxis }.axis;
let magnitude = axis_value.saturating_abs() as i32;
// clear the latch once this axis settles back near center
if awaiting_axis_release == Some(axis as i32) && magnitude < i16::MAX as i32 / 4
{
awaiting_axis_release = None;
}
if !dinput
&& gather != 0
&& axis_value.saturating_abs() > (i16::MAX as i32 * 3 / 4) as i16
&& awaiting_axis_release != Some(axis as i32)
&& magnitude > i16::MAX as i32 * 3 / 4
{
profile.inputs[selected.0][selected.1] =
Some(ui::config::InputItem::ControllerAxis(
@@ -291,6 +299,7 @@ fn draw_profile_screen(
initial_state: 0,
},
));
awaiting_axis_release = Some(axis as i32);
(gather, gather_timer) = advance_cursor(&mut selected, &mut auto_advance);
}
}
@@ -422,7 +431,16 @@ fn draw_profile_screen(
} else {
0
};
if dinput && gather != 0 && axis_value.abs_diff(initial_state) > (u16::MAX / 4)
// clear the latch once this axis settles back near center
if awaiting_axis_release == Some(axis as i32)
&& axis_value.abs_diff(initial_state) < u16::MAX / 8
{
awaiting_axis_release = None;
}
if dinput
&& gather != 0
&& awaiting_axis_release != Some(axis as i32)
&& axis_value.abs_diff(initial_state) > (u16::MAX / 4)
{
profile.inputs[selected.0][selected.1] = Some(
ui::config::InputItem::JoystickAxis(ui::config::InputControllerAxis {
@@ -431,6 +449,7 @@ fn draw_profile_screen(
initial_state,
}),
);
awaiting_axis_release = Some(axis as i32);
(gather, gather_timer) = advance_cursor(&mut selected, &mut auto_advance);
}
}