//! Which icons the hint bar shows: keyboard keys, or the buttons of the //! controller in use. Icons are Kenney's Input Prompts (CC0), in ui/prompts. use std::collections::HashMap; /// Controller families with their own button icons. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Family { Xbox, PlayStation, Switch, SteamDeck, } /// What the hints are drawn for. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Prompts { Keyboard, Pad(Family), } /// An action the hint bar can point at. Each maps to one control per input: /// the gamepad side matches `button_action` in src/input.rs, the keyboard /// side the key-pressed handler in ui/app.slint. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Control { Activate, Back, PlayPause, Queue, Skip, Sections, Search, NowPlaying, Seek, Volume, Mute, /// Cycles the shuffle mode on now playing: the queue button on a /// controller, a key of its own on the keyboard. Shuffle, /// Shuffle play on a row: a long press of activate on a controller. ShufflePlay, /// Steam + X brings up Steam's on-screen keyboard. Nothing to show for a /// real keyboard. OnScreenKeyboard, } #[cfg(test)] impl Control { pub const ALL: [Control; 14] = [ Control::Activate, Control::Back, Control::PlayPause, Control::Queue, Control::Skip, Control::Sections, Control::Search, Control::NowPlaying, Control::Seek, Control::Volume, Control::Mute, Control::Shuffle, Control::ShufflePlay, Control::OnScreenKeyboard, ]; } macro_rules! icon { ($dir:literal, $name:literal) => { include_bytes!(concat!("../ui/prompts/", $dir, "/", $name, ".svg")) as &'static [u8] }; } /// The SVG icons for `control`, left to right. Empty when there is nothing to /// press for it with this input. pub fn icons(prompts: Prompts, control: Control) -> &'static [&'static [u8]] { use Control::*; match prompts { Prompts::Keyboard => match control { Activate => &[icon!("keyboard", "keyboard_enter")], Back => &[icon!("keyboard", "keyboard_escape")], PlayPause => &[icon!("keyboard", "keyboard_space")], Queue => &[icon!("keyboard", "keyboard_q")], Skip => &[icon!("keyboard", "keyboard_p"), icon!("keyboard", "keyboard_n")], Sections => &[icon!("keyboard", "keyboard_tab")], Search => &[icon!("keyboard", "keyboard_slash_forward")], NowPlaying => &[icon!("keyboard", "keyboard_f")], Seek => &[icon!("keyboard", "keyboard_arrows_horizontal")], Volume => &[icon!("keyboard", "keyboard_minus"), icon!("keyboard", "keyboard_plus")], Mute => &[icon!("keyboard", "keyboard_m")], Shuffle => &[icon!("keyboard", "keyboard_s")], ShufflePlay => &[icon!("keyboard", "keyboard_shift"), icon!("keyboard", "keyboard_s")], OnScreenKeyboard => &[], }, Prompts::Pad(Family::Xbox) => match control { Activate => &[icon!("xbox", "xbox_button_color_a")], ShufflePlay => &[icon!("xbox", "xbox_button_color_a")], Back => &[icon!("xbox", "xbox_button_color_b")], PlayPause => &[icon!("xbox", "xbox_button_color_x")], Queue => &[icon!("xbox", "xbox_button_color_y")], Shuffle => &[icon!("xbox", "xbox_button_color_y")], Skip => &[icon!("xbox", "xbox_lb"), icon!("xbox", "xbox_rb")], Sections => &[icon!("xbox", "xbox_lt"), icon!("xbox", "xbox_rt")], Search => &[icon!("xbox", "xbox_button_view")], NowPlaying => &[icon!("xbox", "xbox_button_menu")], Seek => &[icon!("xbox", "xbox_dpad_horizontal")], Volume => &[icon!("xbox", "xbox_stick_r_vertical")], Mute => &[icon!("xbox", "xbox_stick_r_press")], OnScreenKeyboard => &[icon!("xbox", "xbox_guide"), icon!("xbox", "xbox_button_color_x")], }, Prompts::Pad(Family::PlayStation) => match control { Activate => &[icon!("playstation", "playstation_button_color_cross")], ShufflePlay => &[icon!("playstation", "playstation_button_color_cross")], Back => &[icon!("playstation", "playstation_button_color_circle")], PlayPause => &[icon!("playstation", "playstation_button_color_square")], Queue => &[icon!("playstation", "playstation_button_color_triangle")], Shuffle => &[icon!("playstation", "playstation_button_color_triangle")], Skip => &[ icon!("playstation", "playstation_trigger_l1"), icon!("playstation", "playstation_trigger_r1"), ], Sections => &[ icon!("playstation", "playstation_trigger_l2"), icon!("playstation", "playstation_trigger_r2"), ], Search => &[icon!("playstation", "playstation5_button_create")], NowPlaying => &[icon!("playstation", "playstation5_button_options")], Seek => &[icon!("playstation", "playstation_dpad_horizontal")], Volume => &[icon!("playstation", "playstation_stick_r_vertical")], Mute => &[icon!("playstation", "playstation_stick_r_press")], // The pack has no PS button; Steam's own label says the same thing. OnScreenKeyboard => &[ icon!("steamdeck", "steamdeck_button_guide"), icon!("playstation", "playstation_button_color_square"), ], }, // By position, as gilrs reports buttons: the bottom face button is B on a Switch. Prompts::Pad(Family::Switch) => match control { Activate => &[icon!("switch", "switch_button_b")], ShufflePlay => &[icon!("switch", "switch_button_b")], Back => &[icon!("switch", "switch_button_a")], PlayPause => &[icon!("switch", "switch_button_y")], Queue => &[icon!("switch", "switch_button_x")], Shuffle => &[icon!("switch", "switch_button_x")], Skip => &[icon!("switch", "switch_button_l"), icon!("switch", "switch_button_r")], Sections => &[icon!("switch", "switch_button_zl"), icon!("switch", "switch_button_zr")], Search => &[icon!("switch", "switch_button_minus")], NowPlaying => &[icon!("switch", "switch_button_plus")], Seek => &[icon!("switch", "switch_dpad_horizontal")], Volume => &[icon!("switch", "switch_stick_r_vertical")], Mute => &[icon!("switch", "switch_stick_r_press")], OnScreenKeyboard => &[ icon!("switch", "switch_button_home"), icon!("switch", "switch_button_y"), ], }, Prompts::Pad(Family::SteamDeck) => match control { Activate => &[icon!("steamdeck", "steamdeck_button_a")], ShufflePlay => &[icon!("steamdeck", "steamdeck_button_a")], Back => &[icon!("steamdeck", "steamdeck_button_b")], PlayPause => &[icon!("steamdeck", "steamdeck_button_x")], Queue => &[icon!("steamdeck", "steamdeck_button_y")], Shuffle => &[icon!("steamdeck", "steamdeck_button_y")], Skip => &[ icon!("steamdeck", "steamdeck_button_l1"), icon!("steamdeck", "steamdeck_button_r1"), ], Sections => &[ icon!("steamdeck", "steamdeck_button_l2"), icon!("steamdeck", "steamdeck_button_r2"), ], Search => &[icon!("steamdeck", "steamdeck_button_view")], NowPlaying => &[icon!("steamdeck", "steamdeck_button_options")], Seek => &[icon!("steamdeck", "steamdeck_dpad_horizontal")], Volume => &[icon!("steamdeck", "steamdeck_stick_r_vertical")], Mute => &[icon!("steamdeck", "steamdeck_stick_r_press")], OnScreenKeyboard => &[ icon!("steamdeck", "steamdeck_button_guide"), icon!("steamdeck", "steamdeck_button_x"), ], }, } } const VENDOR_MICROSOFT: u16 = 0x045e; const VENDOR_SONY: u16 = 0x054c; const VENDOR_NINTENDO: u16 = 0x057e; pub const VENDOR_VALVE: u16 = 0x28de; /// Steam's virtual gamepad, which stands in for whatever is really connected. pub const PRODUCT_STEAM_VIRTUAL_GAMEPAD: u16 = 0x11ff; const PRODUCT_STEAM_DECK: u16 = 0x1205; /// The family of a physical controller, by its USB ids. Unknown controllers, /// and Steam's virtual one, get Xbox icons: most pads follow that layout. pub fn family_from_ids(vendor: Option, product: Option) -> Family { match (vendor, product) { (Some(VENDOR_SONY), _) => Family::PlayStation, (Some(VENDOR_NINTENDO), _) => Family::Switch, (Some(VENDOR_VALVE), Some(PRODUCT_STEAM_DECK)) => Family::SteamDeck, (Some(VENDOR_MICROSOFT), _) => Family::Xbox, _ => Family::Xbox, } } /// One physical controller listed in Steam's virtual gamepad file. #[derive(Clone, Debug, PartialEq, Eq)] pub struct VirtualPad { pub vendor: Option, pub product: Option, } /// Parses the file `SteamVirtualGamepadInfo` points at, as SDL does /// (src/joystick/SDL_steam_virtual_gamepad.c): `[slot N]` sections with /// `VID=` and `PID=` lines, numbers in C notation (0x for hex). pub fn parse_virtual_pads(text: &str) -> HashMap { let mut pads = HashMap::new(); let mut slot: Option = None; for line in text.lines().map(str::trim) { if let Some(n) = line.strip_prefix("[slot").and_then(|r| r.strip_suffix(']')) { slot = n.trim().parse().ok(); if let Some(s) = slot { pads.entry(s).or_insert(VirtualPad { vendor: None, product: None, }); } continue; } let (Some(s), Some((key, value))) = (slot, line.split_once('=')) else { continue; }; let Some(pad) = pads.get_mut(&s) else { continue }; match key { "VID" => pad.vendor = parse_c_number(value), "PID" => pad.product = parse_c_number(value), _ => {} } } pads } fn parse_c_number(s: &str) -> Option { let s = s.trim(); match s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) { Some(hex) => u16::from_str_radix(hex, 16).ok(), None if s.len() > 1 && s.starts_with('0') => u16::from_str_radix(&s[1..], 8).ok(), None => s.parse().ok(), } } /// The slot of a Steam virtual pad, from its OS name ("... pad 2" is slot 2), /// as SDL reads it. pub fn virtual_slot(os_name: &str) -> Option { let rest = &os_name[os_name.find("pad ")? + 4..]; let digits: String = rest.chars().take_while(char::is_ascii_digit).collect(); digits.parse().ok() } #[cfg(test)] mod tests { use super::*; #[test] fn every_control_has_icons_for_every_controller() { for family in [Family::Xbox, Family::PlayStation, Family::Switch, Family::SteamDeck] { for control in Control::ALL { assert!( !icons(Prompts::Pad(family), control).is_empty(), "{family:?} {control:?}" ); } } } #[test] fn keyboard_has_icons_for_everything_but_steam_keyboard() { for control in Control::ALL { let n = icons(Prompts::Keyboard, control).len(); if control == Control::OnScreenKeyboard { assert_eq!(n, 0); } else { assert!(n > 0, "{control:?}"); } } } #[test] fn icons_are_svg() { for prompts in [ Prompts::Keyboard, Prompts::Pad(Family::Xbox), Prompts::Pad(Family::PlayStation), Prompts::Pad(Family::Switch), Prompts::Pad(Family::SteamDeck), ] { for control in Control::ALL { for svg in icons(prompts, control) { assert!(svg.starts_with(b"