| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709 |
- //! `gameinput.dll`: input devices from the host. Gamepads come from XInput
- //! (host slots 0-3); while none is connected, the keyboard drives one
- //! stand-in gamepad (`FALLBACK_BUTTONS`). The keyboard is a device of its own
- //! too. A poll thread turns host state into readings (a new one per change)
- //! and runs device callbacks on connect and disconnect.
- #![allow(non_snake_case)]
- // Title code calls these through the objects, passing pointers the real API
- // would trust.
- #![allow(clippy::not_unsafe_ptr_arg_deref)]
- use std::collections::VecDeque;
- use std::ffi::c_void;
- use std::ops::Deref;
- use std::ptr::NonNull;
- use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
- use std::sync::{Condvar, LazyLock, Mutex, MutexGuard, PoisonError};
- use std::thread::ThreadId;
- use std::time::{Duration, Instant};
- use super::xasync::{E_INVALIDARG, E_NOTIMPL};
- use super::xgameruntime::{HRESULT, S_OK};
- use crate::{HleExport, HleModule};
- /// The documented errors.
- pub const DEVICE_DISCONNECTED: HRESULT = 0x838A_0001_u32 as i32;
- pub const DEVICE_NOT_FOUND: HRESULT = 0x838A_0002_u32 as i32;
- pub const READING_NOT_FOUND: HRESULT = 0x838A_0003_u32 as i32;
- pub const REFERENCE_TOO_OLD: HRESULT = 0x838A_0004_u32 as i32;
- const E_NOINTERFACE: HRESULT = 0x8000_4002_u32 as i32;
- /// Input kinds (bits) our devices have.
- const KEYBOARD_KIND: u32 = 0x10;
- const GAMEPAD_KIND: u32 = 0x4_0000;
- /// Device status of a connected device: connected, input enabled.
- const STATUS_CONNECTED: u32 = 0x3;
- const ASYNC_ENUMERATION: u32 = 1;
- /// Gamepad buttons; the title maps each bit to one of its pad buttons.
- const MENU: u32 = 0x1;
- const VIEW: u32 = 0x2;
- const A: u32 = 0x4;
- const B: u32 = 0x8;
- const X: u32 = 0x10;
- const Y: u32 = 0x20;
- const UP: u32 = 0x40;
- const DOWN: u32 = 0x80;
- const LEFT: u32 = 0x100;
- const RIGHT: u32 = 0x200;
- const LEFT_SHOULDER: u32 = 0x400;
- const RIGHT_SHOULDER: u32 = 0x800;
- const LEFT_STICK: u32 = 0x1000;
- const RIGHT_STICK: u32 = 0x2000;
- /// Devices: host pad slots, the keyboard's stand-in pad, the keyboard.
- const PADS: usize = 4;
- const FALLBACK: usize = PADS;
- const KEYBOARD: usize = PADS + 1;
- const DEVICES: usize = PADS + 2;
- /// Readings kept per device for next/previous lookups.
- const HISTORY: usize = 256;
- const POLL: Duration = Duration::from_millis(8);
- /// The keyboard's stand-in gamepad (virtual key, button); README lists it.
- /// Sticks: W/A/S/D left, I/J/K/L right; triggers Z and C.
- const FALLBACK_BUTTONS: [(u8, u32); 14] = [
- (0x1B, MENU), // Esc
- (0x09, VIEW), // Tab
- (0x0D, A), // Enter
- (0x08, B), // Backspace
- (b'X', X),
- (b'Y', Y),
- (0x26, UP), // arrows
- (0x28, DOWN),
- (0x25, LEFT),
- (0x27, RIGHT),
- (b'Q', LEFT_SHOULDER),
- (b'E', RIGHT_SHOULDER),
- (b'F', LEFT_STICK),
- (b'H', RIGHT_STICK),
- ];
- /// A gamepad reading: triggers 0 to 1, sticks -1 to 1 (up positive).
- #[repr(C)]
- #[derive(Clone, Copy, Debug, Default, PartialEq)]
- struct PadState {
- buttons: u32,
- left_trigger: f32,
- right_trigger: f32,
- left_x: f32,
- left_y: f32,
- right_x: f32,
- right_y: f32,
- }
- /// One key held down: scan code (0xE0xx when extended), character, key.
- #[repr(C)]
- #[derive(Clone, Copy, Debug, Default, PartialEq)]
- struct KeyState {
- scan_code: u32,
- code_point: u32,
- virtual_key: u8,
- dead_key: bool,
- }
- const _: () = assert!(size_of::<KeyState>() == 12 && size_of::<PadState>() == 28);
- /// Host input, or a fake in tests.
- trait Source: Send {
- /// The pad in host slot `slot`, `None` when the slot is empty.
- fn pad(&mut self, slot: usize) -> Option<PadState>;
- /// Keys held down; none while the title's window lacks focus.
- fn keys(&mut self) -> Vec<KeyState>;
- /// Motor speeds: low frequency (left), high frequency (right).
- fn rumble(&mut self, slot: usize, low: u16, high: u16);
- }
- fn fallback(keys: &[KeyState]) -> PadState {
- let on = |vk: u8| {
- if keys.iter().any(|k| k.virtual_key == vk) {
- 1.0
- } else {
- 0.0
- }
- };
- PadState {
- buttons: FALLBACK_BUTTONS
- .iter()
- .filter(|(vk, _)| on(*vk) != 0.0)
- .fold(0, |all, (_, bit)| all | bit),
- left_trigger: on(b'Z'),
- right_trigger: on(b'C'),
- left_x: on(b'D') - on(b'A'),
- left_y: on(b'W') - on(b'S'),
- right_x: on(b'L') - on(b'J'),
- right_y: on(b'I') - on(b'K'),
- }
- }
- /// Two pads as one: buttons of both, and of each axis and trigger the one
- /// pushed further.
- fn merged(a: PadState, b: PadState) -> PadState {
- let further = |x: f32, y: f32| if y.abs() > x.abs() { y } else { x };
- PadState {
- buttons: a.buttons | b.buttons,
- left_trigger: further(a.left_trigger, b.left_trigger),
- right_trigger: further(a.right_trigger, b.right_trigger),
- left_x: further(a.left_x, b.left_x),
- left_y: further(a.left_y, b.left_y),
- right_x: further(a.right_x, b.right_x),
- right_y: further(a.right_y, b.right_y),
- }
- }
- /// Whether the title sees one pad that is always there, fed by the keyboard
- /// and by every host pad (on by default; `VICTORY_ONE_PAD=0` shows each host
- /// pad as a device of its own, and the stand-in only while there is none).
- /// The title keeps to the pad it started with: with the stand-in taken away
- /// when a pad was switched on, it showed its controller-disconnected screen
- /// until the pad was off again. It also keeps a pad the host lists twice (its
- /// own slot and a remapper's) from being two players.
- fn one_pad() -> bool {
- std::env::var_os("VICTORY_ONE_PAD").is_none_or(|v| v != "0")
- }
- #[repr(C)]
- struct Table<const N: usize>([*const (); N]);
- // SAFETY: function addresses only.
- unsafe impl<const N: usize> Sync for Table<N> {}
- /// The input object; one per hub.
- #[repr(C)]
- pub struct Object {
- vtable: &'static Table<22>,
- hub: *const Hub,
- }
- #[repr(C)]
- pub struct Device {
- vtable: &'static Table<21>,
- hub: *const Hub,
- index: usize,
- info: DeviceInfo,
- }
- /// What a device says about itself. The title reads `id` (+0x20); the rest
- /// follows the public layout and is zero where we have nothing to say.
- #[repr(C)]
- #[derive(Default)]
- #[allow(dead_code)] // read by title code
- struct DeviceInfo {
- size: u32,
- vendor: u16,
- product: u16,
- revision: u16,
- interface: u8,
- collection: u8,
- usage: [u16; 2],
- versions: [u16; 8],
- id: [u8; 32],
- root_id: [u8; 32],
- family: i32,
- capabilities: u32,
- supported_input: u32,
- rumble_motors: u32,
- /// Counts and optional detail pointers.
- rest: [u64; 26],
- }
- const _: () = assert!(std::mem::offset_of!(DeviceInfo, id) == 0x20);
- fn kind(index: usize) -> u32 {
- if index == KEYBOARD {
- KEYBOARD_KIND
- } else {
- GAMEPAD_KIND
- }
- }
- impl Device {
- fn new(index: usize) -> Device {
- let mut id = [0; 32];
- id[..7].copy_from_slice(b"victory");
- id[31] = index as u8 + 1;
- let info = DeviceInfo {
- size: size_of::<DeviceInfo>() as u32,
- id,
- root_id: id,
- supported_input: kind(index),
- // Low and high frequency.
- rumble_motors: if index <= FALLBACK { 0x3 } else { 0 },
- ..Default::default()
- };
- Device {
- vtable: &DEVICE,
- hub: std::ptr::null(),
- index,
- info,
- }
- }
- fn hub(&self) -> &'static Hub {
- // SAFETY: set by `Hub::new`; hubs are never freed.
- unsafe { &*self.hub }
- }
- }
- #[derive(PartialEq)]
- enum Value {
- Pad(PadState),
- Keys(Vec<KeyState>),
- }
- #[repr(C)]
- pub struct Reading {
- vtable: &'static Table<25>,
- refs: AtomicU32,
- device: *const Device,
- sequence: u64,
- timestamp: u64,
- value: Value,
- }
- impl Reading {
- fn kind(&self) -> u32 {
- match self.value {
- Value::Pad(_) => GAMEPAD_KIND,
- Value::Keys(_) => KEYBOARD_KIND,
- }
- }
- }
- /// A counted reference to a reading.
- struct Ref(NonNull<Reading>);
- impl Ref {
- fn new(reading: Reading) -> Ref {
- Ref(NonNull::from(Box::leak(Box::new(reading))))
- }
- /// Another reference, for the title.
- fn share(&self) -> *const Reading {
- reading_add_ref(self.0.as_ptr());
- self.0.as_ptr()
- }
- }
- impl Deref for Ref {
- type Target = Reading;
- fn deref(&self) -> &Reading {
- // SAFETY: we hold a reference, so it is alive.
- unsafe { self.0.as_ref() }
- }
- }
- impl Drop for Ref {
- fn drop(&mut self) {
- reading_release(self.0.as_ptr());
- }
- }
- type DeviceCallback = extern "system" fn(u64, *mut c_void, *const Device, u64, u32, u32);
- #[derive(Clone, Copy)]
- struct Registration {
- token: u64,
- /// One device's address, or 0 for any.
- device: usize,
- kind: u32,
- status: u32,
- context: usize,
- callback: DeviceCallback,
- }
- impl Registration {
- fn matches(&self, devices: &[Device; DEVICES], index: usize) -> bool {
- kind(index) & self.kind != 0
- && (self.device == 0 || self.device == &devices[index] as *const Device as usize)
- }
- fn event(&self, device: usize, timestamp: u64, current: u32, previous: u32) -> Event {
- Event {
- registration: *self,
- device,
- timestamp,
- current,
- previous,
- }
- }
- }
- struct Event {
- registration: Registration,
- device: usize,
- timestamp: u64,
- current: u32,
- previous: u32,
- }
- #[derive(Default)]
- struct Slot {
- connected: bool,
- sequence: u64,
- history: VecDeque<Ref>,
- }
- struct State {
- slots: [Slot; DEVICES],
- callbacks: Vec<Registration>,
- /// Asynchronous enumerations, delivered by the next poll.
- pending: Vec<Event>,
- /// Device callbacks running now: token and thread.
- running: Vec<(u64, ThreadId)>,
- }
- pub struct Hub {
- object: Object,
- devices: [Device; DEVICES],
- /// Apart from the state: XInput can be slow, and the title reads
- /// readings every frame.
- source: Mutex<Box<dyn Source>>,
- /// The title sees one pad ([`one_pad`]).
- one_pad: bool,
- state: Mutex<State>,
- /// A device callback returned.
- returned: Condvar,
- }
- fn lock<T: ?Sized>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
- mutex.lock().unwrap_or_else(PoisonError::into_inner)
- }
- // SAFETY: the back pointers are set before the hub is shared and never
- // change; what changes sits behind the mutex or is atomic.
- unsafe impl Send for Hub {}
- // SAFETY: as above.
- unsafe impl Sync for Hub {}
- static START: LazyLock<Instant> = LazyLock::new(Instant::now);
- /// Microseconds, the clock of every timestamp we hand out.
- fn now() -> u64 {
- START.elapsed().as_micros() as u64
- }
- impl Hub {
- fn new(source: Box<dyn Source>, one_pad: bool) -> &'static Hub {
- let hub = Box::into_raw(Box::new(Hub {
- object: Object {
- vtable: &INPUT,
- hub: std::ptr::null(),
- },
- devices: std::array::from_fn(Device::new),
- source: Mutex::new(source),
- one_pad,
- state: Mutex::new(State {
- slots: Default::default(),
- callbacks: Vec::new(),
- pending: Vec::new(),
- running: Vec::new(),
- }),
- returned: Condvar::new(),
- }));
- // SAFETY: just allocated and never freed; nothing else sees it yet.
- unsafe {
- (*hub).object.hub = hub;
- for device in &mut (*hub).devices {
- device.hub = hub;
- }
- &*hub
- }
- }
- /// A hub over the host's input, polled from now on.
- fn start(source: Box<dyn Source>) -> &'static Hub {
- let hub = Hub::new(source, one_pad());
- hub.poll();
- std::thread::spawn(move || {
- loop {
- std::thread::sleep(POLL);
- hub.poll();
- }
- });
- hub
- }
- fn state(&self) -> MutexGuard<'_, State> {
- lock(&self.state)
- }
- /// Reads the host once; device callbacks run on the calling thread.
- fn poll(&self) {
- let (pads, keys) = {
- let mut source = lock(&self.source);
- let pads: [_; PADS] = std::array::from_fn(|slot| source.pad(slot));
- (pads, source.keys())
- };
- let events = self.state().poll(&self.devices, self.one_pad, pads, keys);
- self.deliver(events);
- }
- /// Runs callbacks, never under our lock: they call back into us. One
- /// unregistered since the event was taken does not run, and
- /// `UnregisterCallback` waits for one that runs.
- fn deliver(&self, events: Vec<Event>) {
- let me = std::thread::current().id();
- for e in events {
- let r = e.registration;
- {
- let mut state = self.state();
- if !state.callbacks.iter().any(|c| c.token == r.token) {
- continue;
- }
- state.running.push((r.token, me));
- }
- (r.callback)(
- r.token,
- r.context as *mut c_void,
- &self.devices[e.device],
- e.timestamp,
- e.current,
- e.previous,
- );
- let mut state = self.state();
- if let Some(i) = state.running.iter().position(|&x| x == (r.token, me)) {
- state.running.swap_remove(i);
- }
- self.returned.notify_all();
- }
- }
- fn index(&self, device: *const Device) -> Option<usize> {
- self.devices.iter().position(|d| std::ptr::eq(d, device))
- }
- }
- impl State {
- fn poll(
- &mut self,
- devices: &[Device; DEVICES],
- one_pad: bool,
- pads: [Option<PadState>; PADS],
- keys: Vec<KeyState>,
- ) -> Vec<Event> {
- let now = now();
- let mut events = std::mem::take(&mut self.pending);
- if one_pad {
- let pad = pads.into_iter().flatten().fold(fallback(&keys), merged);
- self.update(devices, FALLBACK, Some(Value::Pad(pad)), now, &mut events);
- self.update(devices, KEYBOARD, Some(Value::Keys(keys)), now, &mut events);
- return events;
- }
- let any_pad = pads.iter().any(Option::is_some);
- for (slot, pad) in pads.into_iter().enumerate() {
- self.update(devices, slot, pad.map(Value::Pad), now, &mut events);
- }
- let fallback = (!any_pad).then(|| Value::Pad(fallback(&keys)));
- self.update(devices, FALLBACK, fallback, now, &mut events);
- self.update(devices, KEYBOARD, Some(Value::Keys(keys)), now, &mut events);
- events
- }
- /// Records a device's state: a reading when it changed, callbacks when
- /// the device came or went.
- fn update(
- &mut self,
- devices: &[Device; DEVICES],
- index: usize,
- value: Option<Value>,
- now: u64,
- events: &mut Vec<Event>,
- ) {
- let slot = &mut self.slots[index];
- let was = slot.connected;
- slot.connected = value.is_some();
- if let Some(value) = value
- && (!was || slot.history.back().is_none_or(|r| r.value != value))
- {
- slot.sequence += 1;
- if slot.history.len() == HISTORY {
- slot.history.pop_front();
- }
- slot.history.push_back(Ref::new(Reading {
- vtable: &READING,
- refs: AtomicU32::new(1),
- device: &devices[index],
- sequence: slot.sequence,
- timestamp: now,
- value,
- }));
- }
- if was != slot.connected {
- tracing::info!(device = index, connected = !was, "input device");
- let (current, previous) = if was {
- (0, STATUS_CONNECTED)
- } else {
- (STATUS_CONNECTED, 0)
- };
- events.extend(
- self.callbacks
- .iter()
- .filter(|r| r.matches(devices, index) && r.status & STATUS_CONNECTED != 0)
- .map(|r| r.event(index, now, current, previous)),
- );
- }
- }
- }
- #[cfg(windows)]
- fn host() -> Box<dyn Source> {
- Box::new(host::Host::default())
- }
- #[cfg(not(windows))]
- fn host() -> Box<dyn Source> {
- struct Nothing;
- impl Source for Nothing {
- fn pad(&mut self, _: usize) -> Option<PadState> {
- None
- }
- fn keys(&mut self) -> Vec<KeyState> {
- Vec::new()
- }
- fn rumble(&mut self, _: usize, _: u16, _: u16) {}
- }
- Box::new(Nothing)
- }
- static HUB: LazyLock<&'static Hub> = LazyLock::new(|| Hub::start(host()));
- type This = *const Object;
- type Out = *mut *const c_void;
- /// The DLL's only export (ordinal 1): the input object, static and shared.
- pub extern "system" fn GameInputCreate(out: *mut This) -> HRESULT {
- tracing::info!("GameInputCreate");
- if out.is_null() {
- return E_INVALIDARG;
- }
- // SAFETY: checked non-null; the title passes room for a pointer.
- unsafe { out.write(&HUB.object) };
- S_OK
- }
- fn hub(this: This) -> &'static Hub {
- // SAFETY: `this` is a hub's object; hubs are never freed.
- unsafe { &*(*this).hub }
- }
- /// Clears a COM out pointer and returns `hr`.
- fn fail(out: Out, hr: HRESULT) -> HRESULT {
- if !out.is_null() {
- // SAFETY: checked non-null; the title passes room for a pointer.
- unsafe { out.write(std::ptr::null()) };
- }
- hr
- }
- /// Gives the title a reference to `reading`, or fails as with none.
- fn hand(out: Out, reading: Option<&Ref>) -> HRESULT {
- match reading {
- _ if out.is_null() => E_INVALIDARG,
- Some(reading) => {
- // SAFETY: checked non-null; the title passes room for a pointer.
- unsafe { out.write(reading.share().cast()) };
- S_OK
- }
- None => fail(out, READING_NOT_FOUND),
- }
- }
- extern "system" fn no_interface(_: *const c_void, _iid: *const c_void, out: Out) -> HRESULT {
- tracing::warn!("input QueryInterface not implemented");
- fail(out, E_NOINTERFACE)
- }
- /// The input object and devices live as long as the process.
- extern "system" fn add_ref(_: *const c_void) -> u32 {
- 1
- }
- extern "system" fn timestamp(_: This) -> u64 {
- now()
- }
- extern "system" fn current_reading(
- this: This,
- kind: u32,
- device: *const Device,
- out: Out,
- ) -> HRESULT {
- let hub = hub(this);
- let state = hub.state();
- let newest = |i: usize| {
- state.slots[i]
- .history
- .back()
- .filter(|r| r.kind() & kind != 0)
- };
- let found = if device.is_null() {
- (0..DEVICES)
- .filter(|&i| state.slots[i].connected)
- .filter_map(newest)
- .max_by_key(|r| r.timestamp)
- } else {
- match hub.index(device) {
- None => return fail(out, E_INVALIDARG),
- Some(i) if !state.slots[i].connected => return fail(out, DEVICE_DISCONNECTED),
- Some(i) => newest(i),
- }
- };
- tracing::trace!(
- kind = format_args!("{kind:#x}"),
- device = ?hub.index(device),
- sequence = found.map(|r| r.sequence),
- "current reading"
- );
- hand(out, found)
- }
- /// The reading after (`forward`) or before `reference` on its device.
- fn relative(
- this: This,
- reference: *const Reading,
- kind: u32,
- device: *const Device,
- out: Out,
- forward: bool,
- ) -> HRESULT {
- if reference.is_null() {
- return fail(out, E_INVALIDARG);
- }
- let hub = hub(this);
- // SAFETY: a reading we handed out and the title still holds.
- let reference = unsafe { &*reference };
- // shortcut: with no device given only the reference's device is walked,
- // not every device's readings in time order; the title walks
- // only keyboard readings (one device) and the current pad's previous one.
- let Some(index) = hub.index(reference.device) else {
- return fail(out, E_INVALIDARG);
- };
- if !device.is_null() && !std::ptr::eq(device, reference.device) {
- return fail(out, E_INVALIDARG);
- }
- let state = hub.state();
- let history = &state.slots[index].history;
- if history
- .front()
- .is_none_or(|oldest| reference.sequence < oldest.sequence)
- {
- return fail(out, REFERENCE_TOO_OLD);
- }
- let wanted = |r: &&Ref| r.kind() & kind != 0;
- let found = if forward {
- history
- .iter()
- .filter(|r| r.sequence > reference.sequence)
- .find(wanted)
- } else {
- history
- .iter()
- .rev()
- .filter(|r| r.sequence < reference.sequence)
- .find(wanted)
- };
- hand(out, found)
- }
- extern "system" fn next_reading(
- this: This,
- reference: *const Reading,
- kind: u32,
- device: *const Device,
- out: Out,
- ) -> HRESULT {
- relative(this, reference, kind, device, out, true)
- }
- extern "system" fn previous_reading(
- this: This,
- reference: *const Reading,
- kind: u32,
- device: *const Device,
- out: Out,
- ) -> HRESULT {
- relative(this, reference, kind, device, out, false)
- }
- /// The device's reading at `time`, among those still kept.
- extern "system" fn temporal_reading(
- this: This,
- time: u64,
- device: *const Device,
- out: Out,
- ) -> HRESULT {
- let hub = hub(this);
- let Some(index) = hub.index(device) else {
- return fail(out, E_INVALIDARG);
- };
- let state = hub.state();
- let history = &state.slots[index].history;
- hand(out, history.iter().rev().find(|r| r.timestamp <= time))
- }
- static NEXT_TOKEN: AtomicU64 = AtomicU64::new(1);
- fn token(out: *mut u64) -> HRESULT {
- if !out.is_null() {
- // SAFETY: checked non-null.
- unsafe { out.write(NEXT_TOKEN.fetch_add(1, Ordering::Relaxed)) };
- }
- S_OK
- }
- // shortcut: reading callbacks never run; the title registers none
- // (it polls).
- extern "system" fn register_reading(
- _: This,
- _device: *const c_void,
- _kind: u32,
- _threshold: f32,
- _context: *mut c_void,
- _callback: *const c_void,
- out: *mut u64,
- ) -> HRESULT {
- tracing::warn!("input reading callbacks are never called");
- token(out)
- }
- /// Device callbacks run on our poll thread when a device comes or goes.
- /// Enumeration reports the devices already there: blocking on the caller's
- /// thread before we return (the title registers holding a lock its callback
- /// takes again), asynchronous on the next poll.
- extern "system" fn register_device(
- this: This,
- device: *const Device,
- kind: u32,
- status: u32,
- enumeration: u32,
- context: *mut c_void,
- callback: Option<DeviceCallback>,
- out: *mut u64,
- ) -> HRESULT {
- let Some(callback) = callback else {
- return E_INVALIDARG;
- };
- let hub = hub(this);
- let registration = Registration {
- token: NEXT_TOKEN.fetch_add(1, Ordering::Relaxed),
- device: device as usize,
- kind,
- status,
- context: context as usize,
- callback,
- };
- let mut found = Vec::new();
- {
- let mut state = hub.state();
- if enumeration != 0 && status & STATUS_CONNECTED != 0 {
- let now = now();
- found = (0..DEVICES)
- .filter(|&i| state.slots[i].connected && registration.matches(&hub.devices, i))
- .map(|i| registration.event(i, now, STATUS_CONNECTED, 0))
- .collect();
- }
- tracing::info!(
- kind = format_args!("{kind:#x}"),
- enumeration,
- devices = found.len(),
- "input device callback"
- );
- state.callbacks.push(registration);
- if enumeration == ASYNC_ENUMERATION {
- state.pending.append(&mut found);
- }
- }
- if !out.is_null() {
- // SAFETY: checked non-null.
- unsafe { out.write(registration.token) };
- }
- hub.deliver(found);
- S_OK
- }
- /// Guide button and keyboard layout callbacks: neither ever changes here.
- extern "system" fn register_other(
- _: This,
- _device: *const c_void,
- _context: *mut c_void,
- _callback: *const c_void,
- out: *mut u64,
- ) -> HRESULT {
- token(out)
- }
- fn forget(this: This, token: u64) {
- let mut state = hub(this).state();
- state.callbacks.retain(|r| r.token != token);
- state.pending.retain(|e| e.registration.token != token);
- }
- extern "system" fn stop_callback(this: This, token: u64) {
- forget(this, token);
- }
- /// Stops the callback, then waits up to `timeout` µs for a call of it that
- /// is running; false on time-out, or from inside that callback (it cannot
- /// wait for itself), as documented. Either way it is never called again.
- extern "system" fn unregister_callback(this: This, token: u64, timeout: u64) -> bool {
- forget(this, token);
- let hub = hub(this);
- let me = std::thread::current().id();
- let deadline = Instant::now().checked_add(Duration::from_micros(timeout));
- let mut state = hub.state();
- loop {
- let running = state.running.iter().filter(|r| r.0 == token);
- if running.clone().any(|r| r.1 == me) {
- return false;
- }
- if running.count() == 0 {
- return true;
- }
- state = match deadline {
- None => hub.returned.wait(state).unwrap_or_else(PoisonError::into_inner),
- Some(deadline) => {
- let Some(left) = deadline.checked_duration_since(Instant::now()) else {
- return false;
- };
- let waited = hub.returned.wait_timeout(state, left);
- waited.unwrap_or_else(PoisonError::into_inner).0
- }
- };
- }
- }
- /// Dispatcher and aggregate device: no title of ours asks for them.
- extern "system" fn create_dispatcher(_: This, out: Out) -> HRESULT {
- tracing::warn!("input dispatcher not implemented");
- fail(out, E_NOTIMPL)
- }
- extern "system" fn create_aggregate(_: This, _kind: u32, out: Out) -> HRESULT {
- tracing::warn!("aggregate input device not implemented");
- fail(out, E_NOTIMPL)
- }
- extern "system" fn find_by_id(this: This, id: *const [u8; 32], out: Out) -> HRESULT {
- if id.is_null() || out.is_null() {
- return fail(out, E_INVALIDARG);
- }
- // SAFETY: checked non-null; the title passes a device id.
- let id = unsafe { &*id };
- match hub(this).devices.iter().find(|d| d.info.id == *id) {
- Some(device) => {
- // SAFETY: checked non-null.
- unsafe { out.write((device as *const Device).cast()) };
- S_OK
- }
- None => fail(out, DEVICE_NOT_FOUND),
- }
- }
- /// Lookup by object, platform handle or platform string: our devices have
- /// none of these.
- extern "system" fn find_device(_: This, _value: *const c_void, out: Out) -> HRESULT {
- fail(out, DEVICE_NOT_FOUND)
- }
- extern "system" fn enable_oem_device(
- _: This,
- _vendor: u16,
- _product: u16,
- _interface: u8,
- _collection: u8,
- ) -> HRESULT {
- S_OK
- }
- extern "system" fn set_focus_policy(_: This, _policy: u32) {}
- static INPUT: Table<22> = Table([
- no_interface as *const (), // 0x00
- add_ref as *const (), // 0x08 AddRef
- add_ref as *const (), // 0x10 Release
- timestamp as *const (), // 0x18
- current_reading as *const (), // 0x20
- next_reading as *const (), // 0x28
- previous_reading as *const (), // 0x30
- temporal_reading as *const (), // 0x38
- register_reading as *const (), // 0x40
- register_device as *const (), // 0x48
- register_other as *const (), // 0x50 guide button
- register_other as *const (), // 0x58 keyboard layout
- stop_callback as *const (), // 0x60
- unregister_callback as *const (), // 0x68
- create_dispatcher as *const (), // 0x70
- create_aggregate as *const (), // 0x78
- find_by_id as *const (), // 0x80
- find_device as *const (), // 0x88 by object
- find_device as *const (), // 0x90 by platform handle
- find_device as *const (), // 0x98 by platform string
- enable_oem_device as *const (), // 0xa0
- set_focus_policy as *const (), // 0xa8
- ]);
- extern "system" fn device_info(this: *const Device) -> *const DeviceInfo {
- // SAFETY: one of our devices.
- unsafe { &(*this).info }
- }
- extern "system" fn device_status(this: *const Device) -> u32 {
- // SAFETY: one of our devices.
- let device = unsafe { &*this };
- if device.hub().state().slots[device.index].connected {
- STATUS_CONNECTED
- } else {
- 0
- }
- }
- // shortcut: trigger motors are dropped (XInput has none). The host API that
- // drives them without a redistributable is Windows.Gaming.Input (WinRT
- // `Gamepad.Vibration`), which needs WinRT bindings this crate lacks outside
- // diagnostics builds; the title sends trigger levels in slot 0x50.
- extern "system" fn set_rumble(this: *const Device, params: *const [f32; 4]) {
- // SAFETY: one of our devices.
- let device = unsafe { &*this };
- let one_pad = device.hub().one_pad;
- if device.index >= PADS && !(one_pad && device.index == FALLBACK) {
- return;
- }
- let [low, high, ..] = if params.is_null() {
- [0.0; 4]
- } else {
- // SAFETY: checked non-null; four motor levels.
- unsafe { *params }
- };
- let speed = |level: f32| (level.clamp(0.0, 1.0) * f32::from(u16::MAX)) as u16;
- let mut source = lock(&device.hub().source);
- if one_pad {
- // The one pad is every host pad.
- for slot in 0..PADS {
- if source.pad(slot).is_some() {
- source.rumble(slot, speed(low), speed(high));
- }
- }
- } else {
- source.rumble(device.index, speed(low), speed(high));
- }
- }
- extern "system" fn unsupported<const SLOT: usize>(_: *const c_void) -> HRESULT {
- tracing::warn!(
- slot = format_args!("{SLOT:#x}"),
- "input device method not implemented"
- );
- E_NOTIMPL
- }
- static DEVICE: Table<21> = Table([
- no_interface as *const (), // 0x00
- add_ref as *const (), // 0x08 AddRef
- add_ref as *const (), // 0x10 Release
- device_info as *const (), // 0x18
- device_status as *const (), // 0x20
- unsupported::<0x28> as *const (), // battery
- unsupported::<0x30> as *const (), // force feedback
- unsupported::<0x38> as *const (), //
- unsupported::<0x40> as *const (), //
- unsupported::<0x48> as *const (), // haptics
- set_rumble as *const (), // 0x50
- unsupported::<0x58> as *const (), // input synchronization
- unsupported::<0x60> as *const (), //
- unsupported::<0x68> as *const (), // power off
- unsupported::<0x70> as *const (), // raw device access
- unsupported::<0x78> as *const (), //
- unsupported::<0x80> as *const (), //
- unsupported::<0x88> as *const (), //
- unsupported::<0x90> as *const (), //
- unsupported::<0x98> as *const (), //
- unsupported::<0xa0> as *const (), //
- ]);
- fn reading<'a>(this: *const Reading) -> &'a Reading {
- // SAFETY: a reading we handed out and the title still holds.
- unsafe { &*this }
- }
- extern "system" fn reading_add_ref(this: *const Reading) -> u32 {
- reading(this).refs.fetch_add(1, Ordering::Relaxed) + 1
- }
- extern "system" fn reading_release(this: *const Reading) -> u32 {
- let left = reading(this).refs.fetch_sub(1, Ordering::AcqRel) - 1;
- if left == 0 {
- // SAFETY: the last reference is gone; `Ref::new` boxed it.
- drop(unsafe { Box::from_raw(this.cast_mut()) });
- }
- left
- }
- extern "system" fn input_kind(this: *const Reading) -> u32 {
- reading(this).kind()
- }
- extern "system" fn sequence_number(this: *const Reading, kind: u32) -> u64 {
- let reading = reading(this);
- if reading.kind() & kind != 0 {
- reading.sequence
- } else {
- 0
- }
- }
- extern "system" fn reading_timestamp(this: *const Reading) -> u64 {
- reading(this).timestamp
- }
- extern "system" fn reading_device(this: *const Reading, out: *mut *const Device) {
- if !out.is_null() {
- // SAFETY: checked non-null; devices need no reference.
- unsafe { out.write(reading(this).device) };
- }
- }
- extern "system" fn key_count(this: *const Reading) -> u32 {
- match &reading(this).value {
- Value::Keys(keys) => keys.len() as u32,
- Value::Pad(_) => 0,
- }
- }
- extern "system" fn key_state(this: *const Reading, count: u32, out: *mut KeyState) -> u32 {
- let Value::Keys(keys) = &reading(this).value else {
- return 0;
- };
- if out.is_null() {
- return 0;
- }
- let n = keys.len().min(count as usize);
- // SAFETY: the title passes room for `count` keys.
- unsafe { std::ptr::copy_nonoverlapping(keys.as_ptr(), out, n) };
- n as u32
- }
- extern "system" fn gamepad_state(this: *const Reading, out: *mut PadState) -> bool {
- match &reading(this).value {
- Value::Pad(state) if !out.is_null() => {
- // SAFETY: checked non-null.
- unsafe { out.write(*state) };
- true
- }
- _ => false,
- }
- }
- /// Kinds our devices do not have: no count, no state.
- extern "system" fn nothing(_: *const c_void) -> usize {
- 0
- }
- static READING: Table<25> = Table([
- no_interface as *const (), // 0x00
- reading_add_ref as *const (), // 0x08
- reading_release as *const (), // 0x10
- input_kind as *const (), // 0x18
- sequence_number as *const (), // 0x20
- reading_timestamp as *const (), // 0x28
- reading_device as *const (), // 0x30
- nothing as *const (), // 0x38 raw report
- nothing as *const (), // 0x40 controller axes, buttons, switches
- nothing as *const (), // 0x48
- nothing as *const (), // 0x50
- nothing as *const (), // 0x58
- nothing as *const (), // 0x60
- nothing as *const (), // 0x68
- key_count as *const (), // 0x70
- key_state as *const (), // 0x78
- nothing as *const (), // 0x80 mouse
- nothing as *const (), // 0x88 touch
- nothing as *const (), // 0x90
- nothing as *const (), // 0x98 motion
- nothing as *const (), // 0xa0 arcade stick
- nothing as *const (), // 0xa8 flight stick
- gamepad_state as *const (), // 0xb0
- nothing as *const (), // 0xb8 racing wheel
- nothing as *const (), // 0xc0 UI navigation
- ]);
- pub(crate) fn module() -> HleModule {
- HleModule {
- name: "gameinput",
- dlls: &["gameinput.dll"],
- // Titles import it by ordinal only.
- exports: vec![HleExport {
- name: "#1",
- address: GameInputCreate as *const () as usize,
- }],
- }
- }
- /// Host input: XInput pads, the keyboard while our process has focus.
- #[cfg(windows)]
- mod host {
- use std::time::{Duration, Instant};
- use windows_sys::Win32::System::Threading::GetCurrentProcessId;
- use windows_sys::Win32::UI::Input::KeyboardAndMouse::{
- GetAsyncKeyState, MAPVK_VK_TO_VSC_EX, MapVirtualKeyW,
- };
- use windows_sys::Win32::UI::Input::XboxController::{
- XINPUT_GAMEPAD, XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_BACK,
- XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT, XINPUT_GAMEPAD_DPAD_RIGHT,
- XINPUT_GAMEPAD_DPAD_UP, XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_LEFT_THUMB,
- XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_RIGHT_THUMB, XINPUT_GAMEPAD_START,
- XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y, XINPUT_STATE, XINPUT_VIBRATION, XInputGetState,
- XInputSetState,
- };
- use windows_sys::Win32::UI::WindowsAndMessaging::{
- GetForegroundWindow, GetWindowThreadProcessId,
- };
- use super::*;
- /// Asking XInput about an empty slot is slow; look again this often.
- const EMPTY_SLOT_RECHECK: Duration = Duration::from_secs(1);
- const BUTTONS: [(u16, u32); 14] = [
- (XINPUT_GAMEPAD_START, MENU),
- (XINPUT_GAMEPAD_BACK, VIEW),
- (XINPUT_GAMEPAD_A, A),
- (XINPUT_GAMEPAD_B, B),
- (XINPUT_GAMEPAD_X, X),
- (XINPUT_GAMEPAD_Y, Y),
- (XINPUT_GAMEPAD_DPAD_UP, UP),
- (XINPUT_GAMEPAD_DPAD_DOWN, DOWN),
- (XINPUT_GAMEPAD_DPAD_LEFT, LEFT),
- (XINPUT_GAMEPAD_DPAD_RIGHT, RIGHT),
- (XINPUT_GAMEPAD_LEFT_SHOULDER, LEFT_SHOULDER),
- (XINPUT_GAMEPAD_RIGHT_SHOULDER, RIGHT_SHOULDER),
- (XINPUT_GAMEPAD_LEFT_THUMB, LEFT_STICK),
- (XINPUT_GAMEPAD_RIGHT_THUMB, RIGHT_STICK),
- ];
- /// Raw values, no dead zone: titles apply their own.
- pub(super) fn convert(pad: &XINPUT_GAMEPAD) -> PadState {
- let stick = |v: i16| (f32::from(v) / 32767.0).max(-1.0);
- PadState {
- buttons: BUTTONS
- .iter()
- .filter(|(host, _)| pad.wButtons & host != 0)
- .fold(0, |all, (_, bit)| all | bit),
- left_trigger: f32::from(pad.bLeftTrigger) / 255.0,
- right_trigger: f32::from(pad.bRightTrigger) / 255.0,
- left_x: stick(pad.sThumbLX),
- left_y: stick(pad.sThumbLY),
- right_x: stick(pad.sThumbRX),
- right_y: stick(pad.sThumbRY),
- }
- }
- #[derive(Default)]
- pub(super) struct Host {
- empty_until: [Option<Instant>; PADS],
- }
- impl Source for Host {
- fn pad(&mut self, slot: usize) -> Option<PadState> {
- if self.empty_until[slot].is_some_and(|until| Instant::now() < until) {
- return None;
- }
- let mut state = XINPUT_STATE::default();
- // SAFETY: room for the state.
- if unsafe { XInputGetState(slot as u32, &mut state) } != 0 {
- self.empty_until[slot] = Some(Instant::now() + EMPTY_SLOT_RECHECK);
- return None;
- }
- self.empty_until[slot] = None;
- #[allow(unused_mut)]
- let mut pad = convert(&state.Gamepad);
- // The scripted keys (`VICTORY_KEYS`) also press the first
- // pad's buttons, by the stand-in pad's table: with a real pad
- // connected the stand-in is not there, and a title that listens
- // to the pad (the title's start screen) saw nothing.
- if slot == 0 {
- for vk in scripted() {
- if let Some(&(_, button)) = super::FALLBACK_BUTTONS.iter().find(|(key, _)| *key == vk) {
- pad.buttons |= button;
- }
- }
- }
- Some(pad)
- }
- fn keys(&mut self) -> Vec<KeyState> {
- let mut down = scripted();
- if focused() {
- // Left/right modifier keys only, as devices report them; no
- // mouse buttons (below 0x08).
- let held: Vec<u8> = (0x08..=0xFE_u8)
- .filter(|vk| !(0x10..=0x12).contains(vk) && !down.contains(vk))
- // SAFETY: plain queries.
- .filter(|&vk| unsafe { GetAsyncKeyState(i32::from(vk)) } < 0)
- .collect();
- down.extend(held);
- }
- down.into_iter()
- .map(|vk| KeyState {
- // SAFETY: plain query.
- scan_code: unsafe { MapVirtualKeyW(u32::from(vk), MAPVK_VK_TO_VSC_EX) },
- virtual_key: vk,
- // shortcut: no characters (code point 0);
- // the title reads scan codes and virtual keys only.
- ..Default::default()
- })
- .collect()
- }
- fn rumble(&mut self, slot: usize, low: u16, high: u16) {
- let speeds = XINPUT_VIBRATION {
- wLeftMotorSpeed: low,
- wRightMotorSpeed: high,
- };
- // SAFETY: a valid vibration; an empty slot just fails.
- unsafe { XInputSetState(slot as u32, &speeds) };
- }
- }
- /// Debugging aid: `VICTORY_KEYS=<seconds>:<key>,...` holds each key for
- /// 0.2 s that long after input starts (`<from>-<to>:<key>`: all that
- /// time), with or without focus. A key is a
- /// letter, a digit, or `Enter`, `Esc`, `Tab`, `Back`, `Up`, `Down`,
- /// `Left`, `Right` (the stand-in pad's table maps them to buttons).
- fn scripted() -> Vec<u8> {
- static SCRIPT: LazyLock<(Instant, Vec<(Duration, Duration, u8)>)> = LazyLock::new(|| {
- let list = std::env::var("VICTORY_KEYS").unwrap_or_default();
- let keys = list.split(',').filter_map(|entry| {
- let (at, key) = entry.split_once(':')?;
- let vk = match key.trim() {
- "Enter" => 0x0D,
- "Esc" => 0x1B,
- "Tab" => 0x09,
- "Back" => 0x08,
- "Up" => 0x26,
- "Down" => 0x28,
- "Left" => 0x25,
- "Right" => 0x27,
- k if k.len() == 1 && k.as_bytes()[0].is_ascii_alphanumeric() => {
- k.as_bytes()[0].to_ascii_uppercase()
- }
- _ => return None,
- };
- let time = |t: &str| Duration::try_from_secs_f64(t.trim().parse().ok()?).ok();
- let (from, to) = match at.split_once('-') {
- Some((from, to)) => (time(from)?, time(to)?),
- None => (time(at)?, time(at)? + Duration::from_millis(200)),
- };
- Some((from, to, vk))
- });
- (Instant::now(), keys.collect())
- });
- let (start, keys) = &*SCRIPT;
- let now = start.elapsed();
- keys.iter()
- .filter(|&&(from, to, _)| (from..to).contains(&now))
- .map(|&(_, _, vk)| vk)
- .collect()
- }
- /// The foreground window is one of ours (the title's or our fallback).
- fn focused() -> bool {
- let mut process = 0;
- // SAFETY: plain queries; a null window gives process 0.
- unsafe {
- GetWindowThreadProcessId(GetForegroundWindow(), &mut process);
- process == GetCurrentProcessId()
- }
- }
- #[test]
- fn host_pads_convert_to_console_pads() {
- let pad = XINPUT_GAMEPAD {
- wButtons: XINPUT_GAMEPAD_A | XINPUT_GAMEPAD_START | XINPUT_GAMEPAD_DPAD_LEFT,
- bLeftTrigger: 255,
- sThumbLX: -32768,
- sThumbLY: 32767,
- ..Default::default()
- };
- let state = convert(&pad);
- assert_eq!(state.buttons, A | MENU | LEFT);
- assert_eq!(
- (
- state.left_trigger,
- state.left_x,
- state.left_y,
- state.right_x
- ),
- (1.0, -1.0, 1.0, 0.0)
- );
- }
- }
- #[cfg(test)]
- mod tests {
- use std::sync::Arc;
- use super::*;
- use crate::HleResolver;
- use crate::HostPolicy;
- use victory_loader::Provenance;
- use victory_pe::ImportSymbol;
- #[derive(Default)]
- struct FakeHost {
- pads: [Option<PadState>; PADS],
- keys: Vec<KeyState>,
- rumble: Vec<(usize, u16, u16)>,
- }
- struct Fake(Arc<Mutex<FakeHost>>);
- impl Source for Fake {
- fn pad(&mut self, slot: usize) -> Option<PadState> {
- self.0.lock().unwrap().pads[slot]
- }
- fn keys(&mut self) -> Vec<KeyState> {
- self.0.lock().unwrap().keys.clone()
- }
- fn rumble(&mut self, slot: usize, low: u16, high: u16) {
- self.0.lock().unwrap().rumble.push((slot, low, high));
- }
- }
- fn fake() -> (&'static Hub, Arc<Mutex<FakeHost>>) {
- fake_with(false)
- }
- fn fake_with(one_pad: bool) -> (&'static Hub, Arc<Mutex<FakeHost>>) {
- let host = Arc::new(Mutex::new(FakeHost::default()));
- let hub = Hub::new(Box::new(Fake(Arc::clone(&host))), one_pad);
- hub.poll();
- (hub, host)
- }
- fn key(virtual_key: u8) -> KeyState {
- KeyState {
- scan_code: u32::from(virtual_key) + 0x100,
- virtual_key,
- ..Default::default()
- }
- }
- fn slot<F: Copy, T>(object: *const T, offset: usize) -> F {
- // SAFETY: our objects start with their table, which has the slot.
- let f = unsafe { *(*(object as *const *const *const ())).add(offset / 8) };
- // SAFETY: the caller names the slot's signature.
- unsafe { std::mem::transmute_copy(&f) }
- }
- /// Device callbacks seen: device index, current and previous status.
- type Log = Mutex<Vec<(usize, u32, u32)>>;
- extern "system" fn record(
- _token: u64,
- context: *mut c_void,
- device: *const Device,
- _timestamp: u64,
- current: u32,
- previous: u32,
- ) {
- // SAFETY: the test passes its log and one of the hub's devices.
- let (log, index) = unsafe { (&*(context as *const Log), (*device).index) };
- log.lock().unwrap().push((index, current, previous));
- }
- fn register(hub: &Hub, kind: u32, enumeration: u32, log: &Log) -> u64 {
- type Register = extern "system" fn(
- This,
- *const Device,
- u32,
- u32,
- u32,
- *mut c_void,
- Option<DeviceCallback>,
- *mut u64,
- ) -> HRESULT;
- let mut token = 0;
- let context = log as *const Log as *mut c_void;
- let register = slot::<Register, _>(&hub.object, 0x48);
- let hr = register(
- &hub.object,
- std::ptr::null(),
- kind,
- 1,
- enumeration,
- context,
- Some(record),
- &mut token,
- );
- assert_eq!(hr, S_OK);
- assert_ne!(token, 0);
- token
- }
- fn result(hr: HRESULT, out: *const c_void) -> Result<*const Reading, HRESULT> {
- if hr == S_OK {
- Ok(out.cast())
- } else {
- assert!(out.is_null());
- Err(hr)
- }
- }
- fn current(hub: &Hub, kind: u32, device: usize) -> Result<*const Reading, HRESULT> {
- type Current = extern "system" fn(This, u32, *const Device, Out) -> HRESULT;
- let mut out = std::ptr::null();
- let hr = slot::<Current, _>(&hub.object, 0x20)(
- &hub.object,
- kind,
- &hub.devices[device],
- &mut out,
- );
- result(hr, out)
- }
- fn walk(hub: &Hub, offset: usize, from: *const Reading) -> Result<*const Reading, HRESULT> {
- type Walk = extern "system" fn(This, *const Reading, u32, *const Device, Out) -> HRESULT;
- let mut out = std::ptr::null();
- let device = reading(from).device;
- let hr =
- slot::<Walk, _>(&hub.object, offset)(&hub.object, from, GAMEPAD_KIND, device, &mut out);
- result(hr, out)
- }
- fn pad(reading: *const Reading) -> PadState {
- let mut state = PadState::default();
- let get =
- slot::<extern "system" fn(*const Reading, *mut PadState) -> bool, _>(reading, 0xb0);
- assert!(get(reading, &mut state));
- state
- }
- fn stamp(reading: *const Reading) -> (u64, u64) {
- let time = slot::<extern "system" fn(*const Reading) -> u64, _>(reading, 0x28);
- let sequence = slot::<extern "system" fn(*const Reading, u32) -> u64, _>(reading, 0x20);
- (time(reading), sequence(reading, GAMEPAD_KIND))
- }
- fn release(reading: *const Reading) {
- slot::<extern "system" fn(*const Reading) -> u32, _>(reading, 0x10)(reading);
- }
- #[test]
- fn ordinal_one_resolves_to_create() {
- let r = HleResolver::new(HostPolicy::none());
- assert_eq!(
- r.classify("GameInput.dll", &ImportSymbol::Ordinal(1)),
- Some(Provenance::Hle)
- );
- assert_eq!(r.classify("GameInput.dll", &ImportSymbol::Ordinal(2)), None);
- }
- #[test]
- fn pads_connect_report_and_disconnect() {
- let (hub, host) = fake();
- let log = Log::default();
- register(hub, GAMEPAD_KIND, 2, &log);
- // Blocking enumeration ran before registration returned: with no
- // host pad the keyboard's stand-in is the only one.
- assert_eq!(*log.lock().unwrap(), [(FALLBACK, STATUS_CONNECTED, 0)]);
- host.lock().unwrap().pads[1] = Some(PadState {
- buttons: A,
- left_x: 0.5,
- ..Default::default()
- });
- hub.poll();
- assert_eq!(
- log.lock().unwrap()[1..],
- [(1, STATUS_CONNECTED, 0), (FALLBACK, 0, STATUS_CONNECTED)]
- );
- let first = current(hub, GAMEPAD_KIND, 1).unwrap();
- assert_eq!(pad(first).buttons, A);
- assert_eq!(pad(first).left_x, 0.5);
- // No change, no new reading.
- hub.poll();
- let again = current(hub, GAMEPAD_KIND, 1).unwrap();
- assert_eq!(again, first);
- release(again);
- std::thread::sleep(Duration::from_millis(2));
- host.lock().unwrap().pads[1].as_mut().unwrap().buttons = B;
- hub.poll();
- let second = current(hub, GAMEPAD_KIND, 1).unwrap();
- assert_eq!(pad(second).buttons, B);
- let (t1, s1) = stamp(first);
- let (t2, s2) = stamp(second);
- assert!(t2 > t1 && s2 == s1 + 1);
- assert!(t2 <= now());
- // The title's edge check: previous of current, and back again.
- assert_eq!(walk(hub, 0x30, second), Ok(first));
- assert_eq!(walk(hub, 0x28, first), Ok(second));
- assert_eq!(walk(hub, 0x28, second), Err(READING_NOT_FOUND));
- assert_eq!(walk(hub, 0x30, first), Err(READING_NOT_FOUND));
- for r in [first, first, second, second] {
- release(r);
- }
- host.lock().unwrap().pads[1] = None;
- hub.poll();
- assert_eq!(
- log.lock().unwrap()[3..],
- [(1, 0, STATUS_CONNECTED), (FALLBACK, STATUS_CONNECTED, 0)]
- );
- assert_eq!(current(hub, GAMEPAD_KIND, 1), Err(DEVICE_DISCONNECTED));
- // The stand-in is back, at rest.
- let rest = current(hub, GAMEPAD_KIND, FALLBACK).unwrap();
- assert_eq!(pad(rest), PadState::default());
- release(rest);
- }
- #[test]
- fn keys_feed_the_keyboard_and_the_stand_in_pad() {
- let (hub, host) = fake();
- let log = Log::default();
- register(hub, KEYBOARD_KIND, 2, &log);
- assert_eq!(*log.lock().unwrap(), [(KEYBOARD, STATUS_CONNECTED, 0)]);
- let keys = vec![key(b'W'), key(0x0D), key(b'L'), key(b'C')];
- host.lock().unwrap().keys = keys.clone();
- hub.poll();
- let r = current(hub, KEYBOARD_KIND, KEYBOARD).unwrap();
- let count = slot::<extern "system" fn(*const Reading) -> u32, _>(r, 0x70);
- let state =
- slot::<extern "system" fn(*const Reading, u32, *mut KeyState) -> u32, _>(r, 0x78);
- assert_eq!(count(r), 4);
- let mut got = [KeyState::default(); 8];
- assert_eq!(state(r, 8, got.as_mut_ptr()), 4);
- assert_eq!(got[..4], keys[..]);
- release(r);
- let r = current(hub, GAMEPAD_KIND, FALLBACK).unwrap();
- assert_eq!(
- pad(r),
- PadState {
- buttons: A,
- left_y: 1.0,
- right_x: 1.0,
- right_trigger: 1.0,
- ..Default::default()
- }
- );
- release(r);
- // A keyboard reading is no gamepad reading.
- assert_eq!(current(hub, GAMEPAD_KIND, KEYBOARD), Err(READING_NOT_FOUND));
- }
- #[test]
- fn a_host_pad_feeds_the_one_pad_which_never_goes() {
- let (hub, host) = fake_with(true);
- let log = Log::default();
- register(hub, GAMEPAD_KIND, 2, &log);
- assert_eq!(*log.lock().unwrap(), [(FALLBACK, STATUS_CONNECTED, 0)]);
- let pad = |hub: &Hub| match &hub.state().slots[FALLBACK].history.back().unwrap().value {
- Value::Pad(p) => *p,
- Value::Keys(_) => unreachable!(),
- };
- host.lock().unwrap().keys = vec![key(b'D')];
- hub.poll();
- assert_eq!(pad(hub).left_x, 1.0);
- // A pad is switched on, in any slot: its input arrives, beside the
- // keyboard's, and no device comes or goes.
- host.lock().unwrap().pads[2] = Some(PadState { buttons: A, left_y: -0.5, ..Default::default() });
- hub.poll();
- let p = pad(hub);
- assert_eq!((p.buttons, p.left_x, p.left_y), (A, 1.0, -0.5));
- let device: *const Device = &hub.devices[FALLBACK];
- set_rumble(device, &[1.0, 0.0, 0.0, 0.0]);
- assert_eq!(host.lock().unwrap().rumble, [(2, u16::MAX, 0)]);
- // And off again.
- host.lock().unwrap().pads[2] = None;
- host.lock().unwrap().keys.clear();
- hub.poll();
- assert_eq!(pad(hub), PadState::default());
- assert_eq!(log.lock().unwrap().len(), 1);
- assert!(!hub.state().slots[2].connected);
- }
- #[test]
- fn async_enumeration_waits_for_the_poll_and_unregistering_stops_callbacks() {
- let (hub, host) = fake();
- let log = Log::default();
- let token = register(hub, GAMEPAD_KIND, ASYNC_ENUMERATION, &log);
- assert!(log.lock().unwrap().is_empty());
- hub.poll();
- assert_eq!(*log.lock().unwrap(), [(FALLBACK, STATUS_CONNECTED, 0)]);
- let unregister = slot::<extern "system" fn(This, u64, u64) -> bool, _>(&hub.object, 0x68);
- assert!(unregister(&hub.object, token, 5000));
- host.lock().unwrap().pads[0] = Some(PadState::default());
- hub.poll();
- assert_eq!(log.lock().unwrap().len(), 1);
- }
- #[test]
- fn unregistering_waits_for_a_running_callback() {
- use std::sync::atomic::AtomicBool;
- static INSIDE: AtomicBool = AtomicBool::new(false);
- static LEAVE: AtomicBool = AtomicBool::new(false);
- static FROM_INSIDE: AtomicU32 = AtomicU32::new(0);
- extern "system" fn slow(
- token: u64,
- context: *mut c_void,
- _: *const Device,
- _: u64,
- _: u32,
- _: u32,
- ) {
- // From inside its own callback, unregistering fails at once.
- let unregister = slot::<extern "system" fn(This, u64, u64) -> bool, _>(context, 0x68);
- if !unregister(context.cast(), token, 0) {
- FROM_INSIDE.fetch_add(1, Ordering::SeqCst);
- }
- INSIDE.store(true, Ordering::SeqCst);
- while !LEAVE.load(Ordering::SeqCst) {
- std::thread::sleep(Duration::from_millis(1));
- }
- }
- let (hub, host) = fake();
- type Register = extern "system" fn(
- This,
- *const Device,
- u32,
- u32,
- u32,
- *mut c_void,
- Option<DeviceCallback>,
- *mut u64,
- ) -> HRESULT;
- let register = slot::<Register, _>(&hub.object, 0x48);
- let mut token = 0;
- let context = (&raw const hub.object).cast_mut().cast();
- assert_eq!(
- register(&hub.object, std::ptr::null(), GAMEPAD_KIND, 1, 0, context, Some(slow), &mut token),
- S_OK
- );
- host.lock().unwrap().pads[1] = Some(PadState::default());
- let poller = std::thread::spawn(|| hub.poll());
- while !INSIDE.load(Ordering::SeqCst) {
- std::thread::sleep(Duration::from_millis(1));
- }
- let unregister = slot::<extern "system" fn(This, u64, u64) -> bool, _>(&hub.object, 0x68);
- assert!(!unregister(&hub.object, token, 5000), "times out while it runs");
- let leave = std::thread::spawn(|| {
- std::thread::sleep(Duration::from_millis(50));
- LEAVE.store(true, Ordering::SeqCst);
- });
- let start = Instant::now();
- assert!(unregister(&hub.object, token, u64::MAX), "waits for it");
- assert!(start.elapsed() >= Duration::from_millis(40));
- assert_eq!(FROM_INSIDE.load(Ordering::SeqCst), 1);
- poller.join().unwrap();
- leave.join().unwrap();
- }
- #[test]
- fn devices_are_found_by_id_and_rumble_reaches_the_host() {
- let (hub, host) = fake();
- host.lock().unwrap().pads[2] = Some(PadState::default());
- hub.poll();
- let device = &hub.devices[2] as *const Device;
- let info = slot::<extern "system" fn(*const Device) -> *const u8, _>(device, 0x18);
- // SAFETY: the info has the id at +0x20.
- let id = unsafe { *(info(device).add(0x20) as *const [u8; 32]) };
- assert_ne!(id, [0; 32]);
- type Find = extern "system" fn(This, *const [u8; 32], Out) -> HRESULT;
- let find = slot::<Find, _>(&hub.object, 0x80);
- let mut out = std::ptr::null();
- assert_eq!(find(&hub.object, &id, &mut out), S_OK);
- assert_eq!(out, device.cast());
- assert_eq!(find(&hub.object, &[1; 32], &mut out), DEVICE_NOT_FOUND);
- assert!(out.is_null());
- type Rumble = extern "system" fn(*const Device, *const [f32; 4]);
- let rumble = slot::<Rumble, _>(device, 0x50);
- rumble(device, &[0.5, 1.0, 1.0, 1.0]);
- rumble(&hub.devices[FALLBACK], &[1.0; 4]);
- rumble(device, std::ptr::null());
- assert_eq!(host.lock().unwrap().rumble, [(2, 32767, 65535), (2, 0, 0)]);
- }
- }
|