From beed394f45488d499e3f70a344efcc9ffe71ce59 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 17 Jun 2026 11:27:33 +0100 Subject: [PATCH] Preserve USB Move identity during Bluetooth pickup --- crates/muninn-daemon/src/main.rs | 35 +++++++++++++++++--- scripts/claim-nightwing-usb-moves.ps1 | 15 ++++++--- scripts/get-nightwing-move-state-sources.ps1 | 13 ++++++-- scripts/restart-nightwing-muninn.ps1 | 6 ++++ 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/crates/muninn-daemon/src/main.rs b/crates/muninn-daemon/src/main.rs index 574a2d9..65afec2 100644 --- a/crates/muninn-daemon/src/main.rs +++ b/crates/muninn-daemon/src/main.rs @@ -427,10 +427,7 @@ fn publish_bluetooth_move_identity_records( let Some(move_id) = bluetooth_address_move_id(&device.address) else { continue; }; - if active.iter().any(|state| { - state.source.move_id.eq_ignore_ascii_case(&move_id) - && !is_joystick_path(&state.source.hidraw_path) - }) { + if active_usb_move_source_has_move_id(active, &move_id) { continue; } let Some(record) = build_bluetooth_move_identity_record( @@ -679,6 +676,13 @@ fn active_move_source_has_bluetooth_address( }) } +fn active_usb_move_source_has_move_id(active: &[ActiveMoveStateSource], move_id: &str) -> bool { + active.iter().any(|state| { + state.source.move_id.eq_ignore_ascii_case(move_id) + && move_identity_source_state_and_detail(&state.source.hidraw_path).0 == "usb-visible" + }) +} + fn bluetooth_address_move_id(address: &str) -> Option { parse_bluetooth_address_little_endian(address) .ok() @@ -4899,6 +4903,29 @@ MODALIAS=input:b0005v054Cp03D5e0220 ); } + #[test] + fn usb_visible_active_move_blocks_bluetooth_identity_overwrite() { + let active = vec![ActiveMoveStateSource { + source: MoveStateSource { + move_id: "move-000704a39772".to_string(), + hidraw_path: "/dev/input/js0".to_string(), + }, + sequence: 0, + joystick_axes: [0; 16], + joystick_buttons: [false; 32], + light_hidraw_path: None, + }]; + + assert!(active_usb_move_source_has_move_id( + &active, + "move-000704a39772" + )); + assert!(!active_usb_move_source_has_move_id( + &active, + "move-000704a800d0" + )); + } + #[cfg(windows)] #[test] fn windows_ps_move_source_token_preserves_concrete_hid_path() { diff --git a/scripts/claim-nightwing-usb-moves.ps1 b/scripts/claim-nightwing-usb-moves.ps1 index 26f7929..57d6eac 100644 --- a/scripts/claim-nightwing-usb-moves.ps1 +++ b/scripts/claim-nightwing-usb-moves.ps1 @@ -7,17 +7,24 @@ $ErrorActionPreference = "Stop" $scriptPath = Join-Path $PSScriptRoot "nightwing-claim-usb-moves.sh" $sshExe = Join-Path $env:SystemRoot "System32\OpenSSH\ssh.exe" + +function Quote-ShSingle([string] $Value) { + return "'" + ($Value -replace "'", "'\''") + "'" +} + +$scriptContent = [System.IO.File]::ReadAllText($scriptPath) -replace "`r?`n", "`n" +$script64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent)) $remoteArgs = "" if (-not [string]::IsNullOrWhiteSpace($HostAddress)) { - $remoteArgs = " " + ($HostAddress -replace "'", "'\''") + $remoteArgs = " " + (Quote-ShSingle $HostAddress) } $command = '"' + $sshExe + '" -o BatchMode=yes -o ConnectTimeout=5 ' + $SshTarget + - ' "sh -s' + + ' "printf %s ' + + (Quote-ShSingle $script64) + + ' | base64 -d | sh -s --' + $remoteArgs + - '" < "' + - $scriptPath + '"' cmd.exe /c $command diff --git a/scripts/get-nightwing-move-state-sources.ps1 b/scripts/get-nightwing-move-state-sources.ps1 index cfd4cb6..f91b832 100644 --- a/scripts/get-nightwing-move-state-sources.ps1 +++ b/scripts/get-nightwing-move-state-sources.ps1 @@ -6,10 +6,17 @@ $ErrorActionPreference = "Stop" $scriptPath = Join-Path $PSScriptRoot "nightwing-move-state-sources.sh" $sshExe = Join-Path $env:SystemRoot "System32\OpenSSH\ssh.exe" + +function Quote-ShSingle([string] $Value) { + return "'" + ($Value -replace "'", "'\''") + "'" +} + +$scriptContent = [System.IO.File]::ReadAllText($scriptPath) -replace "`r?`n", "`n" +$script64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent)) $command = '"' + $sshExe + '" -o BatchMode=yes -o ConnectTimeout=5 ' + $SshTarget + - ' "sh -s" < "' + - $scriptPath + - '"' + ' "printf %s ' + + (Quote-ShSingle $script64) + + ' | base64 -d | sh -s"' & cmd.exe /d /c $command exit $LASTEXITCODE diff --git a/scripts/restart-nightwing-muninn.ps1 b/scripts/restart-nightwing-muninn.ps1 index 22f00f4..70940b8 100644 --- a/scripts/restart-nightwing-muninn.ps1 +++ b/scripts/restart-nightwing-muninn.ps1 @@ -62,8 +62,14 @@ $moveStateSpecs = @($MoveState | Where-Object { -not [string]::IsNullOrWhiteSpac if ($moveStateSpecs.Count -eq 0) { $claimScript = Join-Path $PSScriptRoot "claim-nightwing-usb-moves.ps1" & powershell.exe -NoProfile -ExecutionPolicy Bypass -File $claimScript -SshTarget $SshTarget + if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE + } $sourceScript = Join-Path $PSScriptRoot "get-nightwing-move-state-sources.ps1" $moveStateSpecs = @(& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $sourceScript -SshTarget $SshTarget) + if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE + } } $moveStateSetLines = ($moveStateSpecs | ForEach-Object {