From 3276e5fbd3e7fbce5506db1ff2e58a0d25eafe75 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 14 Jun 2026 15:18:08 +0100 Subject: [PATCH] Discover Nightwing Move sources for Muninn --- scripts/get-nightwing-move-state-sources.ps1 | 14 ++++++++ scripts/health-nightwing-muninn.ps1 | 24 ++++++++++--- scripts/nightwing-move-state-sources.sh | 36 ++++++++++++++++++++ scripts/restart-nightwing-muninn.ps1 | 26 +++++++++++--- 4 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 scripts/get-nightwing-move-state-sources.ps1 create mode 100644 scripts/nightwing-move-state-sources.sh diff --git a/scripts/get-nightwing-move-state-sources.ps1 b/scripts/get-nightwing-move-state-sources.ps1 new file mode 100644 index 0000000..3d7e05d --- /dev/null +++ b/scripts/get-nightwing-move-state-sources.ps1 @@ -0,0 +1,14 @@ +param( + [string] $SshTarget = "nightwing" +) + +$ErrorActionPreference = "Stop" + +$scriptPath = Join-Path $PSScriptRoot "nightwing-move-state-sources.sh" +$command = 'ssh.exe -o BatchMode=yes -o ConnectTimeout=5 ' + + $SshTarget + + ' "sh -s" < "' + + $scriptPath + + '"' +& cmd.exe /d /c $command +exit $LASTEXITCODE diff --git a/scripts/health-nightwing-muninn.ps1 b/scripts/health-nightwing-muninn.ps1 index d1fc0d7..fee2e90 100644 --- a/scripts/health-nightwing-muninn.ps1 +++ b/scripts/health-nightwing-muninn.ps1 @@ -3,12 +3,26 @@ param( [string] $MuninnExe = "/home/metacrat/.local/bin/muninn", [string] $StorePath = "/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc", [string] $PidPath = "/home/metacrat/.local/state/gamecult/muninn/muninn.pid", - [string] $MoveState = "move-usb=/dev/input/by-id/usb-Sony_Computer_Entertainment_Motion_Controller-joystick", + [string[]] $MoveState = @(), [int] $IntervalSeconds = 15 ) $ErrorActionPreference = "Stop" +function Quote-ShSingle([string] $Value) { + return "'" + ($Value -replace "'", "'\''") + "'" +} + +$moveStateSpecs = @($MoveState | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }) +if ($moveStateSpecs.Count -eq 0) { + $sourceScript = Join-Path $PSScriptRoot "get-nightwing-move-state-sources.ps1" + $moveStateSpecs = @(& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $sourceScript -SshTarget $SshTarget) +} + +$moveStateSetLines = ($moveStateSpecs | ForEach-Object { + "set -- ""`$@"" --move-state $(Quote-ShSingle $_)" +}) -join "`n" + $remoteScript = @" set -eu if [ ! -x '$MuninnExe' ]; then @@ -23,11 +37,13 @@ else echo 'Muninn serve process is not running on Nightwing' >&2 exit 1 fi -'$MuninnExe' --health \ +set -- --health \ --store '$StorePath' \ - --host nightwing \ - --move-state '$MoveState' \ + --host nightwing +$moveStateSetLines +set -- "`$@" \ --interval-seconds '$IntervalSeconds' +'$MuninnExe' "`$@" "@ ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget $remoteScript diff --git a/scripts/nightwing-move-state-sources.sh b/scripts/nightwing-move-state-sources.sh new file mode 100644 index 0000000..fa6e1b9 --- /dev/null +++ b/scripts/nightwing-move-state-sources.sh @@ -0,0 +1,36 @@ +#!/bin/sh +set -eu + +for js in /dev/input/js*; do + [ -e "$js" ] || continue + props="$(udevadm info -q property -n "$js" 2>/dev/null || true)" + uevent="/sys/class/input/$(basename "$js")/device/device/uevent" + uevent_text="$(cat "$uevent" 2>/dev/null || true)" + case "$props +$uevent_text" in + *ID_VENDOR_ID=054c*ID_MODEL_ID=03d5*|*ID_MODEL=Motion_Controller*|*HID_ID=0005:0000054C:000003D5*|*HID_ID=0003:0000054C:000003D5*) + ;; + *) + continue + ;; + esac + + bus="$(printf '%s\n' "$props" | awk -F= '$1=="ID_BUS"{print $2; exit}')" + uniq="$(printf '%s\n' "$uevent_text" | awk -F= '$1=="HID_UNIQ"{print $2; exit}' | tr -d ':')" + path_tag="$(printf '%s\n' "$props" | awk -F= '$1=="ID_PATH_TAG"{print $2; exit}' | tr -c 'A-Za-z0-9_' '_' | sed 's/_*$//')" + + case "$bus:$uniq" in + bluetooth:?*) + id="move-bt-$uniq" + ;; + *) + if [ -n "$path_tag" ]; then + id="move-usb-$path_tag" + else + id="move-$(basename "$js")" + fi + ;; + esac + + printf '%s=%s\n' "$id" "$js" +done diff --git a/scripts/restart-nightwing-muninn.ps1 b/scripts/restart-nightwing-muninn.ps1 index 03a09d6..1d06a04 100644 --- a/scripts/restart-nightwing-muninn.ps1 +++ b/scripts/restart-nightwing-muninn.ps1 @@ -3,13 +3,27 @@ param( [string] $MuninnExe = "/home/metacrat/.local/bin/muninn", [string] $StorePath = "/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc", [string] $LogRoot = "/home/metacrat/.local/state/gamecult/muninn", - [string] $MoveState = "move-usb=/dev/input/by-id/usb-Sony_Computer_Entertainment_Motion_Controller-joystick", + [string[]] $MoveState = @(), [string] $MoveEvidenceStream = "muninn:nightwing:move-evidence", [int] $IntervalSeconds = 15 ) $ErrorActionPreference = "Stop" +function Quote-ShSingle([string] $Value) { + return "'" + ($Value -replace "'", "'\''") + "'" +} + +$moveStateSpecs = @($MoveState | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }) +if ($moveStateSpecs.Count -eq 0) { + $sourceScript = Join-Path $PSScriptRoot "get-nightwing-move-state-sources.ps1" + $moveStateSpecs = @(& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $sourceScript -SshTarget $SshTarget) +} + +$moveStateSetLines = ($moveStateSpecs | ForEach-Object { + "set -- ""`$@"" --move-state $(Quote-ShSingle $_)" +}) -join "`n" + $remoteScript = @" set -eu mkdir -p '$LogRoot' @@ -24,13 +38,15 @@ fi for pid in `$(pgrep -f '[m]uninn serve .*--host nightwing' 2>/dev/null || true); do kill "`$pid" 2>/dev/null || true done -nohup '$MuninnExe' serve \ +set -- serve \ --store '$StorePath' \ --log-root '$LogRoot' \ - --host nightwing \ - --move-state '$MoveState' \ + --host nightwing +$moveStateSetLines +set -- "`$@" \ --move-evidence-stream '$MoveEvidenceStream' \ - --interval-seconds '$IntervalSeconds' \ + --interval-seconds '$IntervalSeconds' +nohup '$MuninnExe' "`$@" \ > '$LogRoot/muninn-serve.out.log' \ 2> '$LogRoot/muninn-serve.err.log' \ < /dev/null &