feat: add a controller-driven Subsonic player for SteamOS
Browses a Subsonic library and plays it through mpv, driven by a gamepad or keyboard. The UI scales from the window's physical height rather than the compositor's scale factor, so it keeps its proportions on a Steam Deck and on a 4K TV, where other players open small or scale badly.
This commit is contained in:
+175
@@ -0,0 +1,175 @@
|
||||
//! Gamepad input. Buttons map to the same `Action`s as the keyboard bindings in app.slint.
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use gilrs::{Axis, Button, EventType, GamepadId, Gilrs};
|
||||
|
||||
use crate::Action;
|
||||
|
||||
/// Steam's virtual controllers. When one is present the physical pad behind it
|
||||
/// also shows up as an evdev device, so only Steam's is listened to.
|
||||
const STEAM_VENDOR: u16 = 0x28de;
|
||||
|
||||
const REPEAT_DELAY: Duration = Duration::from_millis(380);
|
||||
const REPEAT_SLOW: Duration = Duration::from_millis(110);
|
||||
const REPEAT_FAST: Duration = Duration::from_millis(45);
|
||||
const FAST_AFTER: Duration = Duration::from_millis(1500);
|
||||
const STICK_ON: f32 = 0.55;
|
||||
const STICK_OFF: f32 = 0.35;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum Source {
|
||||
DPad,
|
||||
LeftStick,
|
||||
RightStick,
|
||||
}
|
||||
|
||||
struct Held {
|
||||
source: Source,
|
||||
action: Action,
|
||||
since: Instant,
|
||||
next: Instant,
|
||||
}
|
||||
|
||||
pub fn spawn_gamepad(emit: impl Fn(Action) + Send + 'static) {
|
||||
let spawned = std::thread::Builder::new().name("gamepad".into()).spawn(move || {
|
||||
let mut gilrs = match Gilrs::new() {
|
||||
Ok(g) => g,
|
||||
Err(e) => {
|
||||
log::warn!("gamepad support unavailable: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
for (_, pad) in gilrs.gamepads() {
|
||||
log::info!("gamepad: {} (vendor {:04x?})", pad.name(), pad.vendor_id());
|
||||
}
|
||||
let mut held: Option<Held> = None;
|
||||
loop {
|
||||
while let Some(ev) = gilrs.next_event_blocking(Some(Duration::from_millis(15))) {
|
||||
if !accept_from(&gilrs, ev.id) {
|
||||
continue;
|
||||
}
|
||||
match ev.event {
|
||||
EventType::ButtonPressed(b, _) => {
|
||||
if let Some(a) = dpad_action(b) {
|
||||
held = Some(start_hold(Source::DPad, a));
|
||||
emit(a);
|
||||
} else if let Some(a) = button_action(b) {
|
||||
emit(a);
|
||||
}
|
||||
}
|
||||
EventType::ButtonReleased(b, _) => {
|
||||
if dpad_action(b).is_some() && held.as_ref().is_some_and(|h| h.source == Source::DPad) {
|
||||
held = None;
|
||||
}
|
||||
}
|
||||
EventType::AxisChanged(axis, value, _) => {
|
||||
let source = match axis {
|
||||
Axis::LeftStickX | Axis::LeftStickY => Source::LeftStick,
|
||||
Axis::RightStickX => Source::RightStick,
|
||||
_ => continue,
|
||||
};
|
||||
let pad = gilrs.gamepad(ev.id);
|
||||
let dir = stick_direction(source, &pad, value, held.as_ref());
|
||||
match (dir, held.as_ref()) {
|
||||
(Some(a), Some(h)) if h.source == source && h.action == a => {}
|
||||
(Some(a), _) => {
|
||||
held = Some(start_hold(source, a));
|
||||
emit(a);
|
||||
}
|
||||
(None, Some(h)) if h.source == source => held = None,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
EventType::Disconnected => held = None,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if let Some(h) = held.as_mut() {
|
||||
let now = Instant::now();
|
||||
if now >= h.next {
|
||||
emit(h.action);
|
||||
let step = if now - h.since > FAST_AFTER {
|
||||
REPEAT_FAST
|
||||
} else {
|
||||
REPEAT_SLOW
|
||||
};
|
||||
h.next = now + step;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if let Err(e) = spawned {
|
||||
log::warn!("could not start gamepad thread: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
fn accept_from(gilrs: &Gilrs, id: GamepadId) -> bool {
|
||||
let steam_present = gilrs.gamepads().any(|(_, p)| p.vendor_id() == Some(STEAM_VENDOR));
|
||||
!steam_present || gilrs.gamepad(id).vendor_id() == Some(STEAM_VENDOR)
|
||||
}
|
||||
|
||||
fn start_hold(source: Source, action: Action) -> Held {
|
||||
let now = Instant::now();
|
||||
Held {
|
||||
source,
|
||||
action,
|
||||
since: now,
|
||||
next: now + REPEAT_DELAY,
|
||||
}
|
||||
}
|
||||
|
||||
fn dpad_action(b: Button) -> Option<Action> {
|
||||
Some(match b {
|
||||
Button::DPadUp => Action::Up,
|
||||
Button::DPadDown => Action::Down,
|
||||
Button::DPadLeft => Action::Left,
|
||||
Button::DPadRight => Action::Right,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn button_action(b: Button) -> Option<Action> {
|
||||
Some(match b {
|
||||
Button::South => Action::Activate,
|
||||
Button::East => Action::Back,
|
||||
Button::West => Action::PlayPause,
|
||||
Button::North => Action::Queue,
|
||||
Button::LeftTrigger => Action::Prev,
|
||||
Button::RightTrigger => Action::Next,
|
||||
Button::LeftTrigger2 => Action::SectionPrev,
|
||||
Button::RightTrigger2 => Action::SectionNext,
|
||||
Button::Start => Action::NowPlaying,
|
||||
Button::Select => Action::Search,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Reads the stick's dominant direction, with hysteresis so a stick resting
|
||||
/// near the threshold does not flicker between held and released.
|
||||
fn stick_direction(source: Source, pad: &gilrs::Gamepad<'_>, _changed: f32, held: Option<&Held>) -> Option<Action> {
|
||||
let holding = held.is_some_and(|h| h.source == source);
|
||||
let threshold = if holding { STICK_OFF } else { STICK_ON };
|
||||
match source {
|
||||
Source::LeftStick => {
|
||||
let x = pad.value(Axis::LeftStickX);
|
||||
let y = pad.value(Axis::LeftStickY);
|
||||
if x.abs() < threshold && y.abs() < threshold {
|
||||
None
|
||||
} else if x.abs() > y.abs() {
|
||||
Some(if x > 0.0 { Action::Right } else { Action::Left })
|
||||
} else {
|
||||
Some(if y > 0.0 { Action::Up } else { Action::Down })
|
||||
}
|
||||
}
|
||||
Source::RightStick => {
|
||||
let x = pad.value(Axis::RightStickX);
|
||||
if x.abs() < threshold {
|
||||
None
|
||||
} else {
|
||||
Some(if x > 0.0 { Action::SeekForward } else { Action::SeekBack })
|
||||
}
|
||||
}
|
||||
Source::DPad => None,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user