Window and input: fullscreen, English input method, and a mouse capture that leaves the pointer alone

Three switches, each on by default, each 0 restoring the behaviour before it: VICTORY_FULLSCREEN (borderless, VICTORY_FULLSCREEN_RES for another size), VICTORY_IME_ENGLISH (the window's input context to en-US), and VICTORY_MOUSE_CAPTURE (clip to the client area and hide the pointer, release on focus loss).

Capture also fixes a mistake in the mouse device merged as #9: it warped the pointer back to the centre after every sample, which pins the absolute position the title reads and makes the camera look frozen. Warping is now VICTORY_MOUSE_WARP, off by default; with it on the delta is taken from the centre.

Measured on an RTX 5070 Ti Laptop (12 GB): window 0,0-1920,1080 with a client area of the same size and GetClipCursor returning it. cargo build --release and --profile diag pass, cargo test -p victory-hle: 162 passed, 2 ignored.

Written with an AI assistant.
This commit is contained in:
superboynick committed 2026-11-27 12:12:12 +08:00
1 parent 9d6300fa2d
commit 4a0c8ea373
3 files changed
+211 -9

No files matched your search

+73 -8
View File
@@ -30,6 +30,7 @@ const E_NOINTERFACE: HRESULT = 0x8000_4002_u32 as i32;
/// Input kinds (bits) our devices have.
const KEYBOARD_KIND: u32 = 0x10;
const MOUSE_KIND: u32 = 0x20;
const GAMEPAD_KIND: u32 = 0x4_0000;
/// Device status of a connected device: connected, input enabled.
const STATUS_CONNECTED: u32 = 0x3;
@@ -55,7 +56,8 @@ const RIGHT_STICK: u32 = 0x2000;
const PADS: usize = 4;
const FALLBACK: usize = PADS;
const KEYBOARD: usize = PADS + 1;
const DEVICES: usize = PADS + 2;
const MOUSE: usize = PADS + 2;
const DEVICES: usize = PADS + 3;
/// Readings kept per device for next/previous lookups.
const HISTORY: usize = 256;
@@ -105,8 +107,21 @@ struct KeyState {
const _: () = assert!(size_of::<KeyState>() == 12 && size_of::<PadState>() == 28);
/// GameInput v0: cumulative motion; scroll axes are not sampled.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct MouseState {
buttons: u32,
position_x: i64,
position_y: i64,
wheel_x: i64,
wheel_y: i64,
}
const _: () = assert!(size_of::<MouseState>() == 40);
/// Host input, or a fake in tests.
trait Source: Send {
fn mouse(&mut self) -> Option<MouseState> { None }
/// 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.
@@ -210,6 +225,7 @@ struct DeviceInfo {
const _: () = assert!(std::mem::offset_of!(DeviceInfo, id) == 0x20);
fn kind(index: usize) -> u32 {
if index == MOUSE { return MOUSE_KIND; }
if index == KEYBOARD {
KEYBOARD_KIND
} else {
@@ -247,6 +263,7 @@ impl Device {
#[derive(PartialEq)]
enum Value {
Mouse(MouseState),
Pad(PadState),
Keys(Vec<KeyState>),
}
@@ -264,6 +281,7 @@ pub struct Reading {
impl Reading {
fn kind(&self) -> u32 {
match self.value {
Value::Mouse(_) => MOUSE_KIND,
Value::Pad(_) => GAMEPAD_KIND,
Value::Keys(_) => KEYBOARD_KIND,
}
@@ -430,12 +448,12 @@ impl Hub {
/// Reads the host once; device callbacks run on the calling thread.
fn poll(&self) {
let (pads, keys) = {
let (pads, keys, mouse) = {
let mut source = lock(&self.source);
let pads: [_; PADS] = std::array::from_fn(|slot| source.pad(slot));
(pads, source.keys())
(pads, source.keys(), source.mouse())
};
let events = self.state().poll(&self.devices, self.one_pad, pads, keys);
let events = self.state().poll(&self.devices, self.one_pad, pads, keys, mouse);
self.deliver(events);
}
@@ -481,9 +499,11 @@ impl State {
one_pad: bool,
pads: [Option<PadState>; PADS],
keys: Vec<KeyState>,
mouse: Option<MouseState>,
) -> Vec<Event> {
let now = now();
let mut events = std::mem::take(&mut self.pending);
self.update(devices, MOUSE, mouse.map(Value::Mouse), now, &mut events);
if one_pad {
let pad = pads.into_iter().flatten().fold(fallback(&keys), merged);
self.update(devices, FALLBACK, Some(Value::Pad(pad)), now, &mut events);
@@ -881,7 +901,8 @@ extern "system" fn find_by_id(this: This, id: *const [u8; 32], out: Out) -> HRES
}
// 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) {
match hub(this).devices.iter().find(|d| d.info.id == *id
&& (d.index != MOUSE || hub(this).state().slots[MOUSE].connected)) {
Some(device) => {
// SAFETY: checked non-null.
unsafe { out.write((device as *const Device).cast()) };
@@ -1044,7 +1065,7 @@ extern "system" fn reading_device(this: *const Reading, out: *mut *const Device)
extern "system" fn key_count(this: *const Reading) -> u32 {
match &reading(this).value {
Value::Keys(keys) => keys.len() as u32,
Value::Pad(_) => 0,
_ => 0,
}
}
extern "system" fn key_state(this: *const Reading, count: u32, out: *mut KeyState) -> u32 {
@@ -1069,6 +1090,14 @@ extern "system" fn gamepad_state(this: *const Reading, out: *mut PadState) -> bo
_ => false,
}
}
extern "system" fn mouse_state(this: *const Reading, out: *mut MouseState) -> bool {
if let Value::Mouse(state) = &reading(this).value && !out.is_null() {
// SAFETY: the caller supplies room for one v0 mouse state.
unsafe { out.write(*state) };
return true;
}
false
}
/// Kinds our devices do not have: no count, no state.
extern "system" fn nothing(_: *const c_void) -> usize {
0
@@ -1091,7 +1120,7 @@ static READING: Table<25> = Table([
nothing as *const (), // 0x68
key_count as *const (), // 0x70
key_state as *const (), // 0x78
nothing as *const (), // 0x80 mouse
mouse_state as *const (), // 0x80 mouse
nothing as *const (), // 0x88 touch
nothing as *const (), // 0x90
nothing as *const (), // 0x98 motion
@@ -1177,9 +1206,19 @@ mod host {
#[derive(Default)]
pub(super) struct Host {
empty_until: [Option<Instant>; PADS],
mouse: MouseState,
}
impl Source for Host {
fn mouse(&mut self) -> Option<MouseState> {
let (dx, dy) = crate::modules::lifecycle::captured_mouse_delta()?;
self.mouse.position_x += dx;
self.mouse.position_y += dy;
self.mouse.buttons = [0x01, 0x02, 0x04, 0x05, 0x06].into_iter().enumerate()
// SAFETY: virtual-key state query.
.fold(0, |bits, (i, vk)| bits | ((unsafe { GetAsyncKeyState(vk) } < 0) as u32) << i);
Some(self.mouse)
}
fn pad(&mut self, slot: usize) -> Option<PadState> {
if self.empty_until[slot].is_some_and(|until| Instant::now() < until) {
return None;
@@ -1432,6 +1471,7 @@ mod tests {
#[derive(Default)]
struct FakeHost {
mouse: Option<MouseState>,
pads: [Option<PadState>; PADS],
keys: Vec<KeyState>,
rumble: Vec<(usize, u16, u16)>,
@@ -1440,6 +1480,7 @@ mod tests {
struct Fake(Arc<Mutex<FakeHost>>);
impl Source for Fake {
fn mouse(&mut self) -> Option<MouseState> { self.0.lock().unwrap().mouse }
fn pad(&mut self, slot: usize) -> Option<PadState> {
self.0.lock().unwrap().pads[slot]
}
@@ -1580,6 +1621,30 @@ mod tests {
assert_eq!(r.classify("GameInput.dll", &ImportSymbol::Ordinal(2)), None);
}
#[test]
fn mouse_readings_connect_and_disconnect_in_both_pad_modes() {
for one_pad in [false, true] {
let (hub, host) = fake_with(one_pad);
let log = Log::default();
register(hub, MOUSE_KIND, 2, &log);
assert!(log.lock().unwrap().is_empty());
let sample = MouseState { position_x: 30, position_y: -12, buttons: 1, ..Default::default() };
host.lock().unwrap().mouse = Some(sample);
hub.poll();
let r = current(hub, MOUSE_KIND, MOUSE).unwrap();
let mut got = MouseState::default();
let get = slot::<extern "system" fn(*const Reading, *mut MouseState) -> bool, _>(r, 0x80);
assert!(get(r, &mut got));
assert_eq!(got, sample);
assert!(!get(r, std::ptr::null_mut()));
release(r);
host.lock().unwrap().mouse = None;
hub.poll();
assert_eq!(current(hub, MOUSE_KIND, MOUSE), Err(DEVICE_DISCONNECTED));
assert_eq!(*log.lock().unwrap(), [(MOUSE, STATUS_CONNECTED, 0), (MOUSE, 0, STATUS_CONNECTED)]);
}
}
#[test]
fn pads_connect_report_and_disconnect() {
let (hub, host) = fake();
@@ -1685,7 +1750,7 @@ mod tests {
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!(),
_ => unreachable!(),
};
host.lock().unwrap().keys = vec![key(b'D')];
hub.poll();