From 1496a6bd2d9590f37be79872f4eb0bde23147461 Mon Sep 17 00:00:00 2001 From: lixonik <71736299+lixonik@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:37:55 +0300 Subject: [PATCH] d3d12-hle: avoid PlayStation live glyphs when Xbox pad is connected --- .../leonida-hle/src/modules/gameinput.rs | 9 +++ .../src/modules/gameinput/family.rs | 37 +++++++++ .../crates/leonida-hle/src/modules/glyphs.rs | 77 +++++++++++++++---- 3 files changed, 108 insertions(+), 15 deletions(-) diff --git a/d3d12/crates/leonida-hle/src/modules/gameinput.rs b/d3d12/crates/leonida-hle/src/modules/gameinput.rs index 81535812e8..2949b8d5b9 100644 --- a/d3d12/crates/leonida-hle/src/modules/gameinput.rs +++ b/d3d12/crates/leonida-hle/src/modules/gameinput.rs @@ -49,6 +49,12 @@ pub fn family_forced() -> bool { family::forced() } +/// Which pad family is currently connected, if any (Sony takes precedence over XInput). +pub fn connected_pad() -> Option { + LazyLock::force(&HUB); + family::connected_pad() +} + /// The documented errors. pub const DEVICE_DISCONNECTED: HRESULT = 0x838A_0001_u32 as i32; pub const DEVICE_NOT_FOUND: HRESULT = 0x838A_0002_u32 as i32; @@ -1645,6 +1651,7 @@ mod host { unsafe { XInputGetState(slot, &mut state) == 0 } }); let (start, why) = family::at_start(self.view.present, xinput); + family::set_connected_pad(family::connected_pad_from(self.view.present, xinput)); let mut shared = family::SHARED.lock().unwrap_or_else(|e| e.into_inner()); shared.tracker().start_with(start); shared.publish(why); @@ -1653,6 +1660,8 @@ mod host { fn observe(&mut self, _keys: &[KeyState], look: (f32, f32)) { let now = clock(); + let xinput = self.xin_connected.iter().any(|&c| c); + family::set_connected_pad(family::connected_pad_from(self.view.present, xinput)); let mut shared = family::SHARED.lock().unwrap_or_else(|e| e.into_inner()); match self.view.pad { Some(pad) => { diff --git a/d3d12/crates/leonida-hle/src/modules/gameinput/family.rs b/d3d12/crates/leonida-hle/src/modules/gameinput/family.rs index 7804b56e6d..5c990c4277 100644 --- a/d3d12/crates/leonida-hle/src/modules/gameinput/family.rs +++ b/d3d12/crates/leonida-hle/src/modules/gameinput/family.rs @@ -63,6 +63,7 @@ impl Family { static ACTIVE: AtomicU8 = AtomicU8::new(Family::Xbox as u8); static GENERATION: AtomicU32 = AtomicU32::new(0); static FORCED: AtomicU8 = AtomicU8::new(0); +static CONNECTED_PAD: AtomicU8 = AtomicU8::new(0); /// The family of the device used last (or the one `LEONIDA_PROMPTS` forces). Lock-free, readable from any thread /// every frame. @@ -80,6 +81,34 @@ pub fn forced() -> bool { FORCED.load(Ordering::Relaxed) != 0 } +/// Which pad family is currently connected, if any (Sony takes precedence over XInput). +pub fn connected_pad() -> Option { + match CONNECTED_PAD.load(Ordering::Relaxed) { + 1 => Some(Family::Xbox), + 2 => Some(Family::PlayStation), + _ => None, + } +} + +pub(super) fn connected_pad_from(sony_pad: bool, other_pad: bool) -> Option { + if sony_pad { + Some(Family::PlayStation) + } else if other_pad { + Some(Family::Xbox) + } else { + None + } +} + +pub(super) fn set_connected_pad(pad: Option) { + let encoded = match pad { + Some(Family::Xbox) => 1, + Some(Family::PlayStation) => 2, + _ => 0, + }; + CONNECTED_PAD.store(encoded, Ordering::Relaxed); +} + /// The family before any deliberate input, from what is connected when the runtime first looks, and why (for /// the log). A Sony pad counts before another pad, as it did; with no pad at all the player is at the keyboard. pub(super) fn at_start(sony_pad: bool, other_pad: bool) -> (Family, &'static str) { @@ -470,4 +499,12 @@ mod tests { s.publish("again"); assert_eq!(generation(), before + 1, "no change, no new generation"); } + + #[test] + fn connected_pad_classification_prefers_sony_over_xinput() { + assert_eq!(connected_pad_from(false, false), None); + assert_eq!(connected_pad_from(false, true), Some(Family::Xbox)); + assert_eq!(connected_pad_from(true, false), Some(Family::PlayStation)); + assert_eq!(connected_pad_from(true, true), Some(Family::PlayStation)); + } } diff --git a/d3d12/crates/leonida-hle/src/modules/glyphs.rs b/d3d12/crates/leonida-hle/src/modules/glyphs.rs index 8620ce1135..d3d4b702c7 100644 --- a/d3d12/crates/leonida-hle/src/modules/glyphs.rs +++ b/d3d12/crates/leonida-hle/src/modules/glyphs.rs @@ -28,9 +28,10 @@ //! Following a switch during play ([`live`], `LEONIDA_PROMPTS_LIVE`, on unless `0`): unless `LEONIDA_PROMPTS` forces //! a family, a PlayStation pad or the keyboard and mouse at the read is served `ps_live_prompt_glyphs.gfx` (or //! `LEONIDA_PS_LIVE_GLYPHS`, by `tools/live_prompt_glyphs.py`), which holds the PlayStation and the keyboard set and -//! shows the one [`live`] writes into it. With the keyboard at the read, `LEONIDA_PROMPTS_PAD=xbox` keeps the -//! keyboard movie instead (the pad half of the live movie is PlayStation). An Xbox pad keeps the title's own movie. -//! When the live movie is missing or not usable, the family's own movie is served as before. +//! shows the one [`live`] writes into it. With the keyboard at the read, `LEONIDA_PROMPTS_PAD=xbox` -- or an Xbox pad +//! already connected when `LEONIDA_PROMPTS_PAD` is unset -- keeps the keyboard movie instead (the pad half of the live +//! movie is PlayStation). An Xbox pad keeps the title's own movie. When the live movie is missing or not usable, the +//! family's own movie is served as before. pub mod live; use std::ffi::OsString; @@ -80,15 +81,29 @@ pub static PLAYSTATION_LIVE: Set = Set { }; /// The live set for the family at the read, when following a switch is on and no family is forced. -/// `pad` is `LEONIDA_PROMPTS_PAD`: `xbox` keeps a keyboard start on the keyboard movie. -pub fn live_set_for(family: Family, live: bool, forced: bool, pad: Option<&str>) -> Option<&'static Set> { +/// `pad` is `LEONIDA_PROMPTS_PAD`: `xbox` keeps a keyboard start on the keyboard movie, and +/// `playstation` forces the PlayStation live movie even when an Xbox pad is connected. When `pad` +/// is unset (or `auto`), a connected Xbox pad (`connected_pad == Some(Family::Xbox)`) also keeps +/// a keyboard start on the keyboard movie so PlayStation prompts are not served to an Xbox pad. +pub fn live_set_for( + family: Family, + live: bool, + forced: bool, + pad: Option<&str>, + connected_pad: Option, +) -> Option<&'static Set> { if !live || forced { return None; } match family { Family::PlayStation => Some(&PLAYSTATION_LIVE), - Family::KeyboardMouse if !pad.is_some_and(|p| p.trim().eq_ignore_ascii_case("xbox")) => Some(&PLAYSTATION_LIVE), - _ => None, + Family::KeyboardMouse => match pad.map(str::trim) { + Some(p) if p.eq_ignore_ascii_case("xbox") => None, + Some(p) if !p.is_empty() && !p.eq_ignore_ascii_case("auto") => Some(&PLAYSTATION_LIVE), + _ if connected_pad == Some(Family::Xbox) => None, + _ => Some(&PLAYSTATION_LIVE), + }, + Family::Xbox => None, } } @@ -237,7 +252,13 @@ pub fn replacement(file: &str, offset: u64, size: u32, family: impl FnOnce() -> let family = family(); let live = live::enabled_by(std::env::var("LEONIDA_PROMPTS_LIVE").ok().as_deref()); let pad = std::env::var("LEONIDA_PROMPTS_PAD").ok(); - let live_served = live_set_for(family, live, super::gameinput::family_forced(), pad.as_deref()) + let live_served = live_set_for( + family, + live, + super::gameinput::family_forced(), + pad.as_deref(), + super::gameinput::connected_pad(), + ) .and_then(|set| live_movie(family).map(|movie| (set, movie))); if live_served.is_some() { live::start(family); @@ -310,17 +331,43 @@ mod tests { #[test] fn the_live_movie_serves_a_playstation_pad_and_the_keyboard_unless_forced_or_off() { - assert_eq!(live_set_for(Family::PlayStation, true, false, None), Some(&PLAYSTATION_LIVE)); - assert_eq!(live_set_for(Family::KeyboardMouse, true, false, None), Some(&PLAYSTATION_LIVE)); - assert_eq!(live_set_for(Family::KeyboardMouse, true, false, Some("XBox")), None, "LEONIDA_PROMPTS_PAD=xbox"); - assert_eq!(live_set_for(Family::KeyboardMouse, true, false, Some("playstation")), Some(&PLAYSTATION_LIVE)); - assert_eq!(live_set_for(Family::Xbox, true, false, None), None, "an Xbox pad keeps the title's movie"); - assert_eq!(live_set_for(Family::PlayStation, false, false, None), None, "LEONIDA_PROMPTS_LIVE=0"); - assert_eq!(live_set_for(Family::KeyboardMouse, true, true, None), None, "LEONIDA_PROMPTS forces one family"); + assert_eq!(live_set_for(Family::PlayStation, true, false, None, None), Some(&PLAYSTATION_LIVE)); + assert_eq!(live_set_for(Family::KeyboardMouse, true, false, None, None), Some(&PLAYSTATION_LIVE)); + assert_eq!(live_set_for(Family::KeyboardMouse, true, false, Some("XBox"), None), None, "LEONIDA_PROMPTS_PAD=xbox"); + assert_eq!(live_set_for(Family::KeyboardMouse, true, false, Some("playstation"), None), Some(&PLAYSTATION_LIVE)); + assert_eq!(live_set_for(Family::Xbox, true, false, None, None), None, "an Xbox pad keeps the title's movie"); + assert_eq!(live_set_for(Family::PlayStation, false, false, None, None), None, "LEONIDA_PROMPTS_LIVE=0"); + assert_eq!(live_set_for(Family::KeyboardMouse, true, true, None, None), None, "LEONIDA_PROMPTS forces one family"); assert_ne!(PLAYSTATION_LIVE, PLAYSTATION); assert_ne!(PLAYSTATION_LIVE, KEYBOARD); } + #[test] + fn connected_xbox_pad_does_not_get_playstation_live_movie_on_keyboard_start() { + assert_eq!( + live_set_for(Family::KeyboardMouse, true, false, None, Some(Family::Xbox)), + None, + "connected Xbox pad should not receive PlayStation+keyboard live movie" + ); + assert_eq!( + live_set_for(Family::KeyboardMouse, true, false, Some("auto"), Some(Family::Xbox)), + None, + ); + assert_eq!( + live_set_for(Family::KeyboardMouse, true, false, Some("playstation"), Some(Family::Xbox)), + Some(&PLAYSTATION_LIVE), + "explicit LEONIDA_PROMPTS_PAD=playstation overrides connected Xbox pad" + ); + assert_eq!( + live_set_for(Family::KeyboardMouse, true, false, None, Some(Family::PlayStation)), + Some(&PLAYSTATION_LIVE), + ); + assert_eq!( + live_set_for(Family::KeyboardMouse, true, false, None, None), + Some(&PLAYSTATION_LIVE), + ); + } + #[test] fn a_variable_names_a_file_or_turns_its_set_off() { assert_eq!(source(None), Source::Search);