feat: show keyboard keys or the controller's own buttons in the hint bar

The hint bar always named Xbox buttons, so someone at a keyboard was
told to press buttons they did not have, and owners of other pads got
the wrong letters. Hints now name an action, and the keys or button
icons come from the input used last: keyboard, or an Xbox, PlayStation,
Switch or Steam Deck controller.

Behind Steam, every pad is Steam's virtual one. remus reads the file
Steam names in SteamVirtualGamepadInfo to find the physical controller,
matching the slot in the pad's name, as SDL does. Icons are Kenney's
Input Prompts (CC0), with the licence beside them.
This commit is contained in:
Ansho Rei
2026-11-04 10:43:56 +01:00
parent 8255e39047
commit 28a8b8af11
69 changed files with 902 additions and 71 deletions
+96 -11
View File
@@ -1,14 +1,27 @@
//! Gamepad input. Buttons map to the same `Action`s as the keyboard bindings in app.slint.
use std::collections::HashMap;
use std::time::{Duration, Instant};
use gilrs::{Axis, Button, EventType, GamepadId, Gilrs};
use crate::Action;
use crate::prompts::{self, Family, PRODUCT_STEAM_VIRTUAL_GAMEPAD, VENDOR_VALVE};
/// 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 STEAM_VENDOR: u16 = VENDOR_VALVE;
/// How often to look again for the physical controller behind a Steam virtual
/// pad that Steam's file did not list yet. SDL checks as often.
const RECHECK_VIRTUAL: Duration = Duration::from_secs(3);
pub enum PadEvent {
/// A press, from a controller of this family.
Action(Action, Family),
/// The family of a connected controller, or None when the last one went.
Present(Option<Family>),
}
const REPEAT_DELAY: Duration = Duration::from_millis(380);
const REPEAT_SLOW: Duration = Duration::from_millis(110);
@@ -27,11 +40,12 @@ enum Source {
struct Held {
source: Source,
action: Action,
family: Family,
since: Instant,
next: Instant,
}
pub fn spawn_gamepad(emit: impl Fn(Action) + Send + 'static) {
pub fn spawn_gamepad(emit: impl Fn(PadEvent) + Send + 'static) {
let spawned = std::thread::Builder::new().name("gamepad".into()).spawn(move || {
let mut gilrs = match Gilrs::new() {
Ok(g) => g,
@@ -40,22 +54,35 @@ pub fn spawn_gamepad(emit: impl Fn(Action) + Send + 'static) {
return;
}
};
for (_, pad) in gilrs.gamepads() {
log::info!("gamepad: {} (vendor {:04x?})", pad.name(), pad.vendor_id());
let mut families = Families::default();
for (id, pad) in gilrs.gamepads() {
log::info!(
"gamepad: {} (vendor {:04x?}, product {:04x?}), {:?} prompts",
pad.name(),
pad.vendor_id(),
pad.product_id(),
families.family(&gilrs, id)
);
}
emit(PadEvent::Present(present(&gilrs, &mut families)));
let mut held: Option<Held> = None;
loop {
while let Some(ev) = gilrs.next_event_blocking(Some(Duration::from_millis(15))) {
if matches!(ev.event, EventType::Connected | EventType::Disconnected) {
families.forget(ev.id);
emit(PadEvent::Present(present(&gilrs, &mut families)));
}
if !accept_from(&gilrs, ev.id) {
continue;
}
let family = families.family(&gilrs, ev.id);
match ev.event {
EventType::ButtonPressed(b, _) => {
if let Some(a) = dpad_action(b) {
held = Some(start_hold(Source::DPad, a));
emit(a);
held = Some(start_hold(Source::DPad, a, family));
emit(PadEvent::Action(a, family));
} else if let Some(a) = button_action(b) {
emit(a);
emit(PadEvent::Action(a, family));
}
}
EventType::ButtonReleased(b, _) => {
@@ -74,8 +101,8 @@ pub fn spawn_gamepad(emit: impl Fn(Action) + Send + 'static) {
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);
held = Some(start_hold(source, a, family));
emit(PadEvent::Action(a, family));
}
(None, Some(h)) if h.source == source => held = None,
_ => {}
@@ -88,7 +115,7 @@ pub fn spawn_gamepad(emit: impl Fn(Action) + Send + 'static) {
if let Some(h) = held.as_mut() {
let now = Instant::now();
if now >= h.next {
emit(h.action);
emit(PadEvent::Action(h.action, h.family));
let step = if now - h.since > FAST_AFTER {
REPEAT_FAST
} else {
@@ -109,16 +136,74 @@ fn accept_from(gilrs: &Gilrs, id: GamepadId) -> bool {
!steam_present || gilrs.gamepad(id).vendor_id() == Some(STEAM_VENDOR)
}
fn start_hold(source: Source, action: Action) -> Held {
fn start_hold(source: Source, action: Action, family: Family) -> Held {
let now = Instant::now();
Held {
source,
action,
family,
since: now,
next: now + REPEAT_DELAY,
}
}
/// The family of the first controller listened to, if any.
fn present(gilrs: &Gilrs, families: &mut Families) -> Option<Family> {
let id = gilrs.gamepads().map(|(id, _)| id).find(|&id| accept_from(gilrs, id))?;
Some(families.family(gilrs, id))
}
/// Controller families per pad. A Steam virtual pad whose physical controller
/// is not in Steam's file yet is looked up again now and then.
#[derive(Default)]
struct Families {
known: HashMap<GamepadId, (Family, Option<Instant>)>,
}
impl Families {
fn family(&mut self, gilrs: &Gilrs, id: GamepadId) -> Family {
if let Some(&(family, unresolved)) = self.known.get(&id)
&& unresolved.is_none_or(|at| at.elapsed() < RECHECK_VIRTUAL)
{
return family;
}
let (family, resolved) = resolve_family(&gilrs.gamepad(id));
self.known.insert(id, (family, (!resolved).then(Instant::now)));
family
}
fn forget(&mut self, id: GamepadId) {
self.known.remove(&id);
}
}
/// The family of a pad, and whether it is certain. For Steam's virtual pad the
/// physical controller comes from the file Steam names in
/// SteamVirtualGamepadInfo, the way SDL finds it.
fn resolve_family(pad: &gilrs::Gamepad<'_>) -> (Family, bool) {
let (vendor, product) = (pad.vendor_id(), pad.product_id());
if (vendor, product) != (Some(VENDOR_VALVE), Some(PRODUCT_STEAM_VIRTUAL_GAMEPAD)) {
return (prompts::family_from_ids(vendor, product), true);
}
let physical = prompts::virtual_slot(pad.os_name()).and_then(|slot| {
let path = std::env::var_os("SteamVirtualGamepadInfo")?;
let text = std::fs::read_to_string(path).ok()?;
prompts::parse_virtual_pads(&text).remove(&slot)
});
match physical {
Some(p) => {
log::info!(
"Steam virtual pad '{}' is a {:04x?}:{:04x?}",
pad.os_name(),
p.vendor,
p.product
);
(prompts::family_from_ids(p.vendor, p.product), true)
}
None => (Family::Xbox, false),
}
}
fn dpad_action(b: Button) -> Option<Action> {
Some(match b {
Button::DPadUp => Action::Up,