Adding functions
This commit is contained in:
@@ -1,24 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# Invoked by Sunshine as a stream-start hook (global_prep_cmd `do`).
|
||||
# Creates/resizes a Hyprland headless output to match the connecting
|
||||
# Moonlight client's resolution, and moves the active workspace onto it
|
||||
# so the user's existing windows are visible on the stream.
|
||||
# Resizes the vkms-backed `Virtual-1` connector to match the connecting
|
||||
# Moonlight client's resolution, positions it adjacent to the existing real
|
||||
# monitor(s), and (optionally) moves a content-bearing workspace onto it so
|
||||
# the user sees something instead of an empty desktop.
|
||||
#
|
||||
# This script no longer disables eDP-* — vkms gives us a real DRM connector
|
||||
# whose dmabuf is hardware-encoder-friendly, so Sunshine's `capture = kms`
|
||||
# matches it unambiguously by name and we don't need to touch other outputs.
|
||||
#
|
||||
# Sunshine env vars set on connect:
|
||||
# SUNSHINE_CLIENT_WIDTH, SUNSHINE_CLIENT_HEIGHT, SUNSHINE_CLIENT_FPS
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
log() { printf '[sunshine-do] %s\n' "$*" >&2; }
|
||||
|
||||
WIDTH="${SUNSHINE_CLIENT_WIDTH:-1920}"
|
||||
HEIGHT="${SUNSHINE_CLIENT_HEIGHT:-1080}"
|
||||
FPS="${SUNSHINE_CLIENT_FPS:-60}"
|
||||
VIRT_MON="${OMARCHY_VIRTUAL_OUTPUT:-Virtual-1}"
|
||||
STATE_DIR="${XDG_RUNTIME_DIR:-/tmp}/sunshine-headless"
|
||||
mkdir -p "$STATE_DIR"
|
||||
|
||||
# Sunshine doesn't forward prep-cmd stderr to its journal, so also tee every
|
||||
# log line to a runtime file. Truncates on each stream so the file is scoped
|
||||
# to one connect/disconnect cycle.
|
||||
HOOK_LOG="$STATE_DIR/hook.log"
|
||||
: > "$HOOK_LOG"
|
||||
log() {
|
||||
local msg
|
||||
msg="$(date +%H:%M:%S.%3N) [sunshine-do] $*"
|
||||
printf '%s\n' "$msg" >&2
|
||||
printf '%s\n' "$msg" >> "$HOOK_LOG"
|
||||
}
|
||||
log "do-hook start: client=${WIDTH}x${HEIGHT}@${FPS} target=${VIRT_MON}"
|
||||
|
||||
if ! command -v hyprctl >/dev/null 2>&1; then
|
||||
log "hyprctl not found; cannot configure headless. Stream will use whatever output Sunshine selects."
|
||||
log "hyprctl not found; cannot configure virtual display. Stream may show whatever Sunshine selects."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
@@ -37,39 +54,78 @@ if [[ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# Snapshot prior state so undo can restore.
|
||||
hyprctl monitors -j > "$STATE_DIR/prev-monitors.json" 2>/dev/null || true
|
||||
PREV_WS="$(hyprctl activeworkspace -j 2>/dev/null | jq -r '.id // 1' || echo 1)"
|
||||
echo "$PREV_WS" > "$STATE_DIR/prev-workspace-id"
|
||||
|
||||
# Discover whatever headless output already exists. sunshine-prestart.sh is
|
||||
# responsible for ensuring one exists and aligning sunshine.conf's output_name
|
||||
# to its actual name (Hyprland's HEADLESS-N counter drifts across restarts).
|
||||
MON="$(hyprctl monitors -j 2>/dev/null \
|
||||
| jq -r '.[] | select(.name | startswith("HEADLESS")) | .name' | head -1)"
|
||||
if [[ -z "$MON" ]]; then
|
||||
log "No headless output found; creating one"
|
||||
hyprctl output create headless >/dev/null
|
||||
for _ in 1 2 3 4 5; do
|
||||
MON="$(hyprctl monitors -j 2>/dev/null \
|
||||
| jq -r '.[] | select(.name | startswith("HEADLESS")) | .name' | head -1)"
|
||||
[[ -n "$MON" ]] && break
|
||||
sleep 0.1
|
||||
done
|
||||
fi
|
||||
if [[ -z "$MON" ]]; then
|
||||
log "Failed to obtain a headless output; bailing."
|
||||
# Verify the virtual output exists. If the vkms module isn't loaded, this will
|
||||
# be empty and we bail cleanly — Sunshine's KMS capture will just see no
|
||||
# matching connector and the user gets nothing useful, but at least nothing
|
||||
# else gets disturbed.
|
||||
if ! hyprctl monitors -j 2>/dev/null \
|
||||
| jq -e --arg m "$VIRT_MON" '.[] | select(.name == $m)' >/dev/null; then
|
||||
log "Virtual monitor '$VIRT_MON' not present in Hyprland. Is vkms loaded? (lsmod | grep vkms)"
|
||||
exit 0
|
||||
fi
|
||||
echo "$MON" > "$STATE_DIR/headless-name"
|
||||
|
||||
# Resize headless to the client's resolution / framerate.
|
||||
log "Sizing $MON → ${WIDTH}x${HEIGHT}@${FPS}"
|
||||
hyprctl keyword monitor "$MON,${WIDTH}x${HEIGHT}@${FPS},auto,1" >/dev/null
|
||||
# Snapshot prior state so the undo hook can restore focus to where the user
|
||||
# actually was at connect time, even if we promote a different workspace
|
||||
# onto Virtual-1 below.
|
||||
hyprctl monitors -j > "$STATE_DIR/prev-monitors.json" 2>/dev/null || true
|
||||
ACTIVE_WS_ID="$(hyprctl activeworkspace -j 2>/dev/null | jq -r '.id // 1' || echo 1)"
|
||||
ACTIVE_WS_WINDOWS="$(hyprctl activeworkspace -j 2>/dev/null | jq -r '.windows // 0' || echo 0)"
|
||||
echo "$ACTIVE_WS_ID" > "$STATE_DIR/orig-active-workspace-id"
|
||||
|
||||
# Move the active workspace onto the headless so existing windows appear in the stream.
|
||||
log "Moving workspace $PREV_WS → $MON, focusing it"
|
||||
hyprctl dispatch moveworkspacetomonitor "$PREV_WS $MON" >/dev/null || true
|
||||
hyprctl dispatch focusmonitor "$MON" >/dev/null || true
|
||||
# Choose a workspace whose content goes to the stream:
|
||||
# 1. active workspace, if it has windows
|
||||
# 2. otherwise the lowest-id workspace currently bound to Virtual-1 (sticky)
|
||||
# 3. otherwise the lowest-id workspace with any windows
|
||||
# 4. otherwise the active workspace id (stream will show wallpaper only)
|
||||
if [[ "${ACTIVE_WS_WINDOWS:-0}" -gt 0 ]]; then
|
||||
PREV_WS="$ACTIVE_WS_ID"
|
||||
log "Active workspace $PREV_WS has $ACTIVE_WS_WINDOWS window(s); promoting it to $VIRT_MON"
|
||||
else
|
||||
# Workspace already on Virtual-1 (a sticky one from a previous stream).
|
||||
PREV_WS="$(hyprctl workspaces -j 2>/dev/null \
|
||||
| jq -r --arg m "$VIRT_MON" '[.[] | select(.monitor == $m)] | sort_by(.id) | first | .id // empty' \
|
||||
|| true)"
|
||||
if [[ -z "$PREV_WS" ]]; then
|
||||
PREV_WS="$(hyprctl workspaces -j 2>/dev/null \
|
||||
| jq -r '[.[] | select(.windows > 0)] | sort_by(.id) | first | .id // empty' \
|
||||
|| true)"
|
||||
fi
|
||||
if [[ -z "$PREV_WS" ]]; then
|
||||
PREV_WS="$ACTIVE_WS_ID"
|
||||
log "No populated workspace found; using empty active WS $PREV_WS (stream may show wallpaper only)"
|
||||
else
|
||||
log "Active WS $ACTIVE_WS_ID is empty; promoting WS $PREV_WS to $VIRT_MON"
|
||||
fi
|
||||
fi
|
||||
echo "$PREV_WS" > "$STATE_DIR/prev-workspace-id"
|
||||
|
||||
log "Stream ready: ${WIDTH}x${HEIGHT}@${FPS} on $MON"
|
||||
# Compute a non-overlapping position for Virtual-1: just to the right of the
|
||||
# rightmost real monitor's logical edge. Real monitors keep their position;
|
||||
# Virtual-1 ends up as a new "right of laptop" workspace the user can drift to.
|
||||
MAX_RIGHT="$(hyprctl monitors -j 2>/dev/null \
|
||||
| jq -r --arg m "$VIRT_MON" '
|
||||
[.[] | select(.name != $m)
|
||||
| (.x + ((.width / .scale) | floor))]
|
||||
| max // 0' \
|
||||
|| echo 0)"
|
||||
# Resize Virtual-1 to the client's requested mode at that x-offset. vkms
|
||||
# supports arbitrary modes via DRM mode-set; if a refresh-rate variant of the
|
||||
# exact mode isn't in the reported list, Hyprland still negotiates.
|
||||
log "Sizing $VIRT_MON → ${WIDTH}x${HEIGHT}@${FPS} at ${MAX_RIGHT}x0 (scale=1)"
|
||||
hyprctl keyword monitor "$VIRT_MON,${WIDTH}x${HEIGHT}@${FPS},${MAX_RIGHT}x0,1" >/dev/null
|
||||
|
||||
# Move the chosen workspace onto Virtual-1 and focus it.
|
||||
log "Moving workspace $PREV_WS → $VIRT_MON, focusing it"
|
||||
hyprctl dispatch moveworkspacetomonitor "$PREV_WS $VIRT_MON" >/dev/null || true
|
||||
hyprctl dispatch focusmonitor "$VIRT_MON" >/dev/null || true
|
||||
|
||||
# Dump post-state so we can verify everything ended up where intended.
|
||||
post_mons="$(hyprctl monitors -j 2>/dev/null \
|
||||
| jq -r '.[] | "\(.name) \(.width)x\(.height)@\(.refreshRate) at \(.x)x\(.y) activeWS=\(.activeWorkspace.id)"' \
|
||||
| tr '\n' ';' || true)"
|
||||
post_ws="$(hyprctl workspaces -j 2>/dev/null \
|
||||
| jq -r '.[] | "ws\(.id)=\(.windows)win on \(.monitor)"' \
|
||||
| tr '\n' ';' || true)"
|
||||
log "post-state monitors: $post_mons"
|
||||
log "post-state workspaces: $post_ws"
|
||||
log "Stream ready: ${WIDTH}x${HEIGHT}@${FPS} on $VIRT_MON (eDP-* untouched)"
|
||||
|
||||
Reference in New Issue
Block a user