From 352b64de5b2ff806abd706f34f55f68aad453c4b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 14 Jun 2026 20:14:55 +0100 Subject: [PATCH] Add Windows PS Move input backend --- crates/muninn-daemon/Cargo.toml | 1 + crates/muninn-daemon/src/main.rs | 172 +++++++++++++++++++++++++++- scripts/health-starfire-muninn.ps1 | 13 ++- scripts/restart-starfire-muninn.ps1 | 12 +- 4 files changed, 194 insertions(+), 4 deletions(-) diff --git a/crates/muninn-daemon/Cargo.toml b/crates/muninn-daemon/Cargo.toml index 741b4d5..34feabc 100644 --- a/crates/muninn-daemon/Cargo.toml +++ b/crates/muninn-daemon/Cargo.toml @@ -25,4 +25,5 @@ windows-sys = { version = "0.59", features = [ "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_IO", + "Win32_System_Threading", ] } diff --git a/crates/muninn-daemon/src/main.rs b/crates/muninn-daemon/src/main.rs index f1b0627..e61599c 100644 --- a/crates/muninn-daemon/src/main.rs +++ b/crates/muninn-daemon/src/main.rs @@ -578,7 +578,7 @@ impl MoveControllerStateReader for HidMoveControllerStateReader { } } - #[cfg(not(unix))] + #[cfg(all(not(unix), not(windows)))] fn read_report(&mut self, hidraw_path: &str) -> Result>> { if hidraw_path.trim().is_empty() { return Ok(None); @@ -588,6 +588,19 @@ impl MoveControllerStateReader for HidMoveControllerStateReader { )) } + #[cfg(windows)] + fn read_report(&mut self, hidraw_path: &str) -> Result>> { + if hidraw_path.trim().is_empty() { + return Ok(None); + } + if is_windows_ps_move_source(hidraw_path) { + return windows_ps_move_input_report(); + } + Err(anyhow!( + "Windows PS Move controller state reads require the windows-psmove source path" + )) + } + #[cfg(unix)] fn read_joystick_events(&mut self, joystick_path: &str) -> Result> { let mut device = fs::OpenOptions::new() @@ -695,6 +708,10 @@ fn is_joystick_path(path: &str) -> bool { path.contains("/dev/input/js") || path.contains("-joystick") } +fn is_windows_ps_move_source(path: &str) -> bool { + path.eq_ignore_ascii_case("windows-psmove") || path.eq_ignore_ascii_case("windows-psmove-col01") +} + fn build_move_controller_state_record( options: &Options, source: &MoveStateSource, @@ -1215,6 +1232,159 @@ fn windows_ps_move_light_paths() -> Result> { Ok(paths) } +#[cfg(windows)] +fn windows_ps_move_input_report() -> Result>> { + use windows_sys::Win32::Devices::HumanInterfaceDevice::{ + HIDP_CAPS, HIDP_STATUS_SUCCESS, HidD_FreePreparsedData, HidD_GetInputReport, + HidD_GetPreparsedData, HidP_GetCaps, + }; + use windows_sys::Win32::Foundation::{CloseHandle, GENERIC_READ, INVALID_HANDLE_VALUE}; + use windows_sys::Win32::Storage::FileSystem::{ + CreateFileW, FILE_ATTRIBUTE_NORMAL, FILE_FLAG_OVERLAPPED, FILE_SHARE_READ, + FILE_SHARE_WRITE, OPEN_EXISTING, + }; + + let Some(path) = windows_ps_move_light_paths()?.into_iter().next() else { + return Ok(None); + }; + let wide = wide_null(&path); + let handle = unsafe { + CreateFileW( + wide.as_ptr(), + GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + std::ptr::null_mut(), + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, + std::ptr::null_mut(), + ) + }; + if handle == INVALID_HANDLE_VALUE { + return Err(std::io::Error::last_os_error()) + .with_context(|| format!("opening Windows PS Move input HID path {path}")); + } + + let mut preparsed = 0; + if unsafe { HidD_GetPreparsedData(handle, &mut preparsed) } == 0 { + let error = std::io::Error::last_os_error(); + unsafe { CloseHandle(handle) }; + return Err(error).with_context(|| format!("reading HID preparsed data from {path}")); + } + + let mut caps: HIDP_CAPS = unsafe { std::mem::zeroed() }; + let caps_ok = unsafe { HidP_GetCaps(preparsed, &mut caps) == HIDP_STATUS_SUCCESS }; + unsafe { HidD_FreePreparsedData(preparsed) }; + if !caps_ok || caps.InputReportByteLength == 0 { + unsafe { CloseHandle(handle) }; + return Ok(None); + } + + let mut report = vec![0u8; caps.InputReportByteLength as usize]; + report[0] = 0x01; + let ok = + unsafe { HidD_GetInputReport(handle, report.as_mut_ptr().cast(), report.len() as u32) }; + if ok != 0 { + unsafe { CloseHandle(handle) }; + return Ok(Some(report)); + } + + let interrupt_report = windows_read_hid_interrupt_report(handle, report.len(), &path)?; + unsafe { CloseHandle(handle) }; + Ok(interrupt_report) +} + +#[cfg(windows)] +fn windows_read_hid_interrupt_report( + handle: *mut std::ffi::c_void, + report_len: usize, + path: &str, +) -> Result>> { + use windows_sys::Win32::Foundation::{ + CloseHandle, ERROR_IO_PENDING, ERROR_OPERATION_ABORTED, ERROR_SEM_TIMEOUT, ERROR_TIMEOUT, + WAIT_OBJECT_0, WAIT_TIMEOUT, + }; + use windows_sys::Win32::Storage::FileSystem::ReadFile; + use windows_sys::Win32::System::IO::{CancelIoEx, GetOverlappedResult, OVERLAPPED}; + use windows_sys::Win32::System::Threading::{CreateEventW, WaitForSingleObject}; + + let event = unsafe { CreateEventW(std::ptr::null(), 1, 0, std::ptr::null()) }; + if event.is_null() { + return Err(std::io::Error::last_os_error()).context("creating Windows HID read event"); + } + + let mut report = vec![0u8; report_len]; + let mut overlapped: OVERLAPPED = unsafe { std::mem::zeroed() }; + overlapped.hEvent = event; + let mut bytes_read = 0; + let ok = unsafe { + ReadFile( + handle, + report.as_mut_ptr(), + report.len() as u32, + &mut bytes_read, + &mut overlapped, + ) + }; + if ok != 0 { + unsafe { CloseHandle(event) }; + report.truncate(bytes_read as usize); + return Ok(Some(report)); + } + + let error = std::io::Error::last_os_error(); + if error.raw_os_error() != Some(ERROR_IO_PENDING as i32) { + unsafe { CloseHandle(event) }; + if matches!( + error.raw_os_error(), + Some(code) + if code == ERROR_TIMEOUT as i32 + || code == ERROR_SEM_TIMEOUT as i32 + || code == ERROR_OPERATION_ABORTED as i32 + ) { + return Ok(None); + } + return Err(error) + .with_context(|| format!("starting Windows PS Move input read from {path}")); + } + + let wait = unsafe { WaitForSingleObject(event, 25) }; + if wait == WAIT_TIMEOUT { + unsafe { + CancelIoEx(handle, &mut overlapped); + CloseHandle(event); + } + return Ok(None); + } + if wait != WAIT_OBJECT_0 { + unsafe { + CancelIoEx(handle, &mut overlapped); + CloseHandle(event); + } + return Err(std::io::Error::last_os_error()) + .with_context(|| format!("waiting for Windows PS Move input read from {path}")); + } + + let mut transferred = 0; + let done = unsafe { GetOverlappedResult(handle, &mut overlapped, &mut transferred, 0) }; + unsafe { CloseHandle(event) }; + if done == 0 { + let error = std::io::Error::last_os_error(); + if matches!( + error.raw_os_error(), + Some(code) + if code == ERROR_OPERATION_ABORTED as i32 + || code == ERROR_TIMEOUT as i32 + || code == ERROR_SEM_TIMEOUT as i32 + ) { + return Ok(None); + } + return Err(error) + .with_context(|| format!("completing Windows PS Move input read from {path}")); + } + report.truncate(transferred as usize); + Ok(Some(report)) +} + #[cfg(windows)] unsafe fn windows_hid_interface_path( info_set: isize, diff --git a/scripts/health-starfire-muninn.ps1 b/scripts/health-starfire-muninn.ps1 index 955b98e..53134cb 100644 --- a/scripts/health-starfire-muninn.ps1 +++ b/scripts/health-starfire-muninn.ps1 @@ -1,6 +1,8 @@ param( [string] $MuninnExe = "C:\Meta\Odin\Muninn\muninn.exe", - [string] $StorePath = "C:\Meta\Odin\state\starfire.muninn.telemetry.cc" + [string] $StorePath = "C:\Meta\Odin\state\starfire.muninn.telemetry.cc", + [string[]] $MoveState = @(), + [int] $IntervalSeconds = 15 ) $ErrorActionPreference = "Stop" @@ -21,7 +23,14 @@ if ($null -eq $process) { throw "Starfire Muninn serve process is not running with Quest access enabled" } -& $MuninnExe --health --store $StorePath +$healthArgs = @("--health", "--store", $StorePath, "--host", "starfire", "--interval-seconds", "$IntervalSeconds") +foreach ($source in $MoveState) { + if (-not [string]::IsNullOrWhiteSpace($source)) { + $healthArgs += @("--move-state", $source) + } +} + +& $MuninnExe @healthArgs if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } diff --git a/scripts/restart-starfire-muninn.ps1 b/scripts/restart-starfire-muninn.ps1 index fb605b9..c8f421e 100644 --- a/scripts/restart-starfire-muninn.ps1 +++ b/scripts/restart-starfire-muninn.ps1 @@ -2,7 +2,9 @@ param( [string] $MuninnExe = "C:\Meta\Odin\Muninn\muninn.exe", [string] $StorePath = "C:\Meta\Odin\state\starfire.muninn.telemetry.cc", [string] $LogRoot = "C:\Meta\Odin\logs\starfire-muninn", - [string] $QuestSerial = "1WMHHB68PG1515" + [string] $QuestSerial = "1WMHHB68PG1515", + [string[]] $MoveState = @(), + [switch] $EnableUsbMoveState ) $ErrorActionPreference = "Stop" @@ -36,6 +38,14 @@ $arguments = @( "--quest-adb", "--quest-serial", $QuestSerial ) +foreach ($source in $MoveState) { + if (-not [string]::IsNullOrWhiteSpace($source)) { + $arguments += @("--move-state", $source) + } +} +if ($EnableUsbMoveState) { + $arguments += @("--move-state", "move-starfire-usb=windows-psmove") +} $process = Start-Process ` -FilePath $MuninnExe `