Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions d3d12/crates/leonida-hle/src/modules/gameinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Family> {
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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down
37 changes: 37 additions & 0 deletions d3d12/crates/leonida-hle/src/modules/gameinput/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Family> {
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<Family> {
if sony_pad {
Some(Family::PlayStation)
} else if other_pad {
Some(Family::Xbox)
} else {
None
}
}

pub(super) fn set_connected_pad(pad: Option<Family>) {
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) {
Expand Down Expand Up @@ -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));
}
}
77 changes: 62 additions & 15 deletions d3d12/crates/leonida-hle/src/modules/glyphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Family>,
) -> 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,
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading