//! The player's input, from libretro to famicom-rust's [`HostInput`] (which //! drives the pads and the plugged device as the reference's window does, //! `famicom_rust::host`). //! //! - Gamepads: ports 1-4, by position: libretro's B (the lower face button) //! is the pad's B, A (the right one) its A, X (the upper) a Super NES pad's //! X; L and R the shoulders; the left analog stick too. //! - The mouse's position: the light gun's screen coordinates (a mouse or a //! real gun, as the frontend maps it); off screen is the reference's //! window losing the mouse, and the gun's reload (or the mouse's right //! button) points it off screen. Its motion and buttons: the mouse. //! - Keys: libretro's keyboard, by key, under the reference's pygame names. use famicom_rust::host::{Gamepad, HostInput}; use std::ffi::c_uint; pub type InputStateFn = unsafe extern "C" fn(port: c_uint, device: c_uint, index: c_uint, id: c_uint) -> i16; const DEVICE_JOYPAD: c_uint = 1; const DEVICE_MOUSE: c_uint = 2; const DEVICE_KEYBOARD: c_uint = 3; const DEVICE_LIGHTGUN: c_uint = 4; const DEVICE_ANALOG: c_uint = 5; // RETRO_DEVICE_ID_JOYPAD_* const ID_B: c_uint = 0; const ID_SELECT: c_uint = 2; const ID_START: c_uint = 3; const ID_UP: c_uint = 4; const ID_DOWN: c_uint = 5; const ID_LEFT: c_uint = 6; const ID_RIGHT: c_uint = 7; const ID_A: c_uint = 8; const ID_X: c_uint = 9; const ID_L: c_uint = 10; const ID_R: c_uint = 11; // RETRO_DEVICE_ID_MOUSE_*, _LIGHTGUN_*, _ANALOG_* const MOUSE_X: c_uint = 0; const MOUSE_Y: c_uint = 1; const MOUSE_LEFT: c_uint = 2; const MOUSE_RIGHT: c_uint = 3; const LIGHTGUN_TRIGGER: c_uint = 2; const LIGHTGUN_SCREEN_X: c_uint = 13; const LIGHTGUN_SCREEN_Y: c_uint = 14; const LIGHTGUN_IS_OFFSCREEN: c_uint = 15; const LIGHTGUN_RELOAD: c_uint = 16; const ANALOG_LEFT: c_uint = 0; const ANALOG_X: c_uint = 0; const ANALOG_Y: c_uint = 1; /// The reference's key names and libretro's codes (RETROK_*) for them. fn keys() -> &'static [(String, c_uint)] { static KEYS: std::sync::OnceLock> = std::sync::OnceLock::new(); KEYS.get_or_init(|| { let mut keys: Vec<(String, c_uint)> = Vec::new(); for c in b'a'..=b'z' { keys.push((format!("K_{}", c as char), c as c_uint)); } for d in 0..10u32 { keys.push((format!("K_{d}"), 48 + d)); keys.push((format!("K_KP{d}"), 256 + d)); } for n in 1..=12u32 { keys.push((format!("K_F{n}"), 281 + n)); } for (name, code) in [ ("K_BACKSPACE", 8), ("K_TAB", 9), ("K_RETURN", 13), ("K_PAUSE", 19), ("K_SPACE", 32), ("K_QUOTE", 39), ("K_COMMA", 44), ("K_MINUS", 45), ("K_PERIOD", 46), ("K_SLASH", 47), ("K_SEMICOLON", 59), ("K_EQUALS", 61), ("K_LEFTBRACKET", 91), ("K_BACKSLASH", 92), ("K_RIGHTBRACKET", 93), ("K_BACKQUOTE", 96), ("K_DELETE", 127), ("K_KP_PERIOD", 266), ("K_KP_DIVIDE", 267), ("K_KP_MULTIPLY", 268), ("K_KP_MINUS", 269), ("K_KP_PLUS", 270), ("K_KP_ENTER", 271), ("K_UP", 273), ("K_DOWN", 274), ("K_RIGHT", 275), ("K_LEFT", 276), ("K_INSERT", 277), ("K_HOME", 278), ("K_END", 279), ("K_PAGEUP", 280), ("K_PAGEDOWN", 281), ("K_NUMLOCK", 300), ("K_NUMLOCKCLEAR", 300), ("K_CAPSLOCK", 301), ("K_RSHIFT", 303), ("K_LSHIFT", 304), ("K_RCTRL", 305), ("K_LCTRL", 306), ("K_RALT", 307), ("K_LALT", 308), ] { keys.push((name.to_string(), code)); } keys }) } /// A light gun coordinate (-0x7FFF to 0x7FFF across the picture) in pixels. fn pixels(value: i16, size: i64) -> i64 { ((value as i64 + 0x7FFF) * size) / 0xFFFF } /// This frame's input from the frontend. pub fn read(state: InputStateFn) -> HostInput { // SAFETY: the frontend's input_state, called from retro_run as libretro allows. let get = |port: c_uint, device: c_uint, index: c_uint, id: c_uint| unsafe { state(port, device, index, id) }; let mut input = HostInput::default(); for port in 0..4 { let held = |id| get(port, DEVICE_JOYPAD, 0, id) != 0; input.gamepads.push(Gamepad { up: held(ID_UP), down: held(ID_DOWN), left: held(ID_LEFT), right: held(ID_RIGHT), south: held(ID_B), east: held(ID_A), north: held(ID_X), back: held(ID_SELECT), start: held(ID_START), left_shoulder: held(ID_L), right_shoulder: held(ID_R), stick_x: get(port, DEVICE_ANALOG, ANALOG_LEFT, ANALOG_X) as i32, stick_y: get(port, DEVICE_ANALOG, ANALOG_LEFT, ANALOG_Y) as i32, }); } for (name, code) in keys() { if get(0, DEVICE_KEYBOARD, 0, *code) != 0 { input.keys.insert(name.clone()); } } input.mouse_x = pixels(get(0, DEVICE_LIGHTGUN, 0, LIGHTGUN_SCREEN_X), 256); input.mouse_y = pixels(get(0, DEVICE_LIGHTGUN, 0, LIGHTGUN_SCREEN_Y), 240); input.mouse_focused = get(0, DEVICE_LIGHTGUN, 0, LIGHTGUN_IS_OFFSCREEN) == 0; input.mouse_dx = get(0, DEVICE_MOUSE, 0, MOUSE_X) as i64; input.mouse_dy = get(0, DEVICE_MOUSE, 0, MOUSE_Y) as i64; input.mouse_left = get(0, DEVICE_MOUSE, 0, MOUSE_LEFT) != 0 || get(0, DEVICE_LIGHTGUN, 0, LIGHTGUN_TRIGGER) != 0; input.mouse_right = get(0, DEVICE_MOUSE, 0, MOUSE_RIGHT) != 0 || get(0, DEVICE_LIGHTGUN, 0, LIGHTGUN_RELOAD) != 0; input } /// The core option's devices: the reference window's `--device` names and /// their NES 2.0 expansion numbers ("auto": the cartridge's own). pub const DEVICES: [(&str, Option); 16] = [ ("auto", None), ("pads", Some(0x01)), ("four-score", Some(0x02)), ("famicom-four-players", Some(0x03)), ("zapper", Some(0x08)), ("two-zappers", Some(0x09)), ("power-pad", Some(0x0C)), ("family-trainer", Some(0x0E)), ("vaus", Some(0x0F)), ("hyper-shot", Some(0x12)), ("keyboard", Some(0x23)), ("subor-keyboard", Some(0x26)), ("snes-mouse", Some(0x29)), ("snes-pads", Some(0x2B)), ("turbo-file", Some(0x21)), ("rob", Some(0x1F)), ]; #[cfg(test)] mod tests { use super::*; #[test] fn gun_coordinates_cover_the_picture() { assert_eq!(pixels(-0x7FFF, 256), 0); assert_eq!(pixels(0x7FFF, 256), 255); assert_eq!(pixels(0x7FFF, 240), 239); assert_eq!(pixels(0, 256), 127); // Off screen (-0x8000) truncates to the left edge; IS_OFFSCREEN // unfocuses the window then, so a gun is not aimed there. assert_eq!(pixels(i16::MIN, 256), 0); } #[test] fn every_key_the_window_reads_has_a_code() { let names: std::collections::HashSet<&str> = keys().iter().map(|(n, _)| n.as_str()).collect(); let tables = [ famicom_rust::host::keyboard_keys(), famicom_rust::host::subor_host_keys(), famicom_rust::host::mahjong_keys(), ]; for (name, _) in tables.iter().flatten() { assert!(names.contains(name.as_str()), "{name} has no libretro key"); } for name in famicom_rust::host::MAT_KEYS { assert!(names.contains(name), "{name}"); } for (_, held) in famicom_rust::host::KEYS { for name in held { assert!(names.contains(name), "{name}"); } } for (_, name) in famicom_rust::host::SNES_KEYS { assert!(names.contains(name), "{name}"); } assert!(names.contains("K_m")); // RETROK_* spot checks (libretro.h). let code = |n: &str| keys().iter().find(|(k, _)| k == n).unwrap().1; assert_eq!( (code("K_a"), code("K_RETURN"), code("K_F1"), code("K_UP")), (97, 13, 282, 273) ); assert_eq!( (code("K_KP0"), code("K_LSHIFT"), code("K_RALT")), (256, 304, 307) ); } }