Adding functions
This commit is contained in:
@@ -1,24 +1,41 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Invoked by Sunshine as a stream-start hook (global_prep_cmd `do`).
|
# Invoked by Sunshine as a stream-start hook (global_prep_cmd `do`).
|
||||||
# Creates/resizes a Hyprland headless output to match the connecting
|
# Resizes the vkms-backed `Virtual-1` connector to match the connecting
|
||||||
# Moonlight client's resolution, and moves the active workspace onto it
|
# Moonlight client's resolution, positions it adjacent to the existing real
|
||||||
# so the user's existing windows are visible on the stream.
|
# 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 env vars set on connect:
|
||||||
# SUNSHINE_CLIENT_WIDTH, SUNSHINE_CLIENT_HEIGHT, SUNSHINE_CLIENT_FPS
|
# SUNSHINE_CLIENT_WIDTH, SUNSHINE_CLIENT_HEIGHT, SUNSHINE_CLIENT_FPS
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
log() { printf '[sunshine-do] %s\n' "$*" >&2; }
|
|
||||||
|
|
||||||
WIDTH="${SUNSHINE_CLIENT_WIDTH:-1920}"
|
WIDTH="${SUNSHINE_CLIENT_WIDTH:-1920}"
|
||||||
HEIGHT="${SUNSHINE_CLIENT_HEIGHT:-1080}"
|
HEIGHT="${SUNSHINE_CLIENT_HEIGHT:-1080}"
|
||||||
FPS="${SUNSHINE_CLIENT_FPS:-60}"
|
FPS="${SUNSHINE_CLIENT_FPS:-60}"
|
||||||
|
VIRT_MON="${OMARCHY_VIRTUAL_OUTPUT:-Virtual-1}"
|
||||||
STATE_DIR="${XDG_RUNTIME_DIR:-/tmp}/sunshine-headless"
|
STATE_DIR="${XDG_RUNTIME_DIR:-/tmp}/sunshine-headless"
|
||||||
mkdir -p "$STATE_DIR"
|
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
|
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
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -37,39 +54,78 @@ if [[ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Snapshot prior state so undo can restore.
|
# Verify the virtual output exists. If the vkms module isn't loaded, this will
|
||||||
hyprctl monitors -j > "$STATE_DIR/prev-monitors.json" 2>/dev/null || true
|
# be empty and we bail cleanly — Sunshine's KMS capture will just see no
|
||||||
PREV_WS="$(hyprctl activeworkspace -j 2>/dev/null | jq -r '.id // 1' || echo 1)"
|
# matching connector and the user gets nothing useful, but at least nothing
|
||||||
echo "$PREV_WS" > "$STATE_DIR/prev-workspace-id"
|
# else gets disturbed.
|
||||||
|
if ! hyprctl monitors -j 2>/dev/null \
|
||||||
# Discover whatever headless output already exists. sunshine-prestart.sh is
|
| jq -e --arg m "$VIRT_MON" '.[] | select(.name == $m)' >/dev/null; then
|
||||||
# responsible for ensuring one exists and aligning sunshine.conf's output_name
|
log "Virtual monitor '$VIRT_MON' not present in Hyprland. Is vkms loaded? (lsmod | grep vkms)"
|
||||||
# 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."
|
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
echo "$MON" > "$STATE_DIR/headless-name"
|
|
||||||
|
|
||||||
# Resize headless to the client's resolution / framerate.
|
# Snapshot prior state so the undo hook can restore focus to where the user
|
||||||
log "Sizing $MON → ${WIDTH}x${HEIGHT}@${FPS}"
|
# actually was at connect time, even if we promote a different workspace
|
||||||
hyprctl keyword monitor "$MON,${WIDTH}x${HEIGHT}@${FPS},auto,1" >/dev/null
|
# 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.
|
# Choose a workspace whose content goes to the stream:
|
||||||
log "Moving workspace $PREV_WS → $MON, focusing it"
|
# 1. active workspace, if it has windows
|
||||||
hyprctl dispatch moveworkspacetomonitor "$PREV_WS $MON" >/dev/null || true
|
# 2. otherwise the lowest-id workspace currently bound to Virtual-1 (sticky)
|
||||||
hyprctl dispatch focusmonitor "$MON" >/dev/null || true
|
# 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)"
|
||||||
|
|||||||
@@ -1,15 +1,25 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Invoked by Sunshine as a stream-stop hook (global_prep_cmd `undo`).
|
# Invoked by Sunshine as a stream-stop hook (global_prep_cmd `undo`).
|
||||||
# Moves the previously-active workspace back to a real monitor (if any
|
# Returns the promoted workspace to the original real monitor and restores
|
||||||
# exist) and tears down the headless output created by sunshine-stream-do.sh.
|
# focus to whichever workspace the user had active at connect time.
|
||||||
|
#
|
||||||
|
# With the vkms-based design, no monitors get disabled, so undo is just a
|
||||||
|
# workspace-and-focus restore. Virtual-1 stays alive; the next stream resizes
|
||||||
|
# it as needed.
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
log() { printf '[sunshine-undo] %s\n' "$*" >&2; }
|
VIRT_MON="${OMARCHY_VIRTUAL_OUTPUT:-Virtual-1}"
|
||||||
|
|
||||||
STATE_DIR="${XDG_RUNTIME_DIR:-/tmp}/sunshine-headless"
|
STATE_DIR="${XDG_RUNTIME_DIR:-/tmp}/sunshine-headless"
|
||||||
# Headless name was captured by sunshine-stream-do.sh; fall back to discovery.
|
HOOK_LOG="$STATE_DIR/hook.log"
|
||||||
MON="$(cat "$STATE_DIR/headless-name" 2>/dev/null || true)"
|
log() {
|
||||||
|
local msg
|
||||||
|
msg="$(date +%H:%M:%S.%3N) [sunshine-undo] $*"
|
||||||
|
printf '%s\n' "$msg" >&2
|
||||||
|
# Append (not truncate) so the undo log lands alongside the do log.
|
||||||
|
printf '%s\n' "$msg" >> "$HOOK_LOG" 2>/dev/null || true
|
||||||
|
}
|
||||||
|
log "undo-hook start"
|
||||||
|
|
||||||
if ! command -v hyprctl >/dev/null 2>&1; then
|
if ! command -v hyprctl >/dev/null 2>&1; then
|
||||||
log "hyprctl not found; nothing to undo."
|
log "hyprctl not found; nothing to undo."
|
||||||
@@ -29,29 +39,27 @@ if [[ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
PREV_WS="$(cat "$STATE_DIR/prev-workspace-id" 2>/dev/null || echo 1)"
|
PREV_WS="$(cat "$STATE_DIR/prev-workspace-id" 2>/dev/null || echo 1)"
|
||||||
|
ORIG_ACTIVE_WS="$(cat "$STATE_DIR/orig-active-workspace-id" 2>/dev/null || echo "$PREV_WS")"
|
||||||
|
|
||||||
if [[ -z "$MON" ]]; then
|
# Find a non-virtual monitor to move the promoted workspace back to.
|
||||||
MON="$(hyprctl monitors -j 2>/dev/null \
|
REAL_MON="$(hyprctl monitors -j 2>/dev/null \
|
||||||
| jq -r '.[] | select(.name | startswith("HEADLESS")) | .name' | head -1)"
|
| jq -r --arg m "$VIRT_MON" '.[] | select(.name != $m) | .name' \
|
||||||
fi
|
| head -n1)"
|
||||||
|
|
||||||
# Find a non-headless monitor to move the workspace back to. If there isn't one
|
|
||||||
# (truly headless host with KVM detached), the workspace just lives on whatever
|
|
||||||
# Hyprland reassigns it to when we remove the output.
|
|
||||||
REAL_MON="$(hyprctl monitors -j 2>/dev/null | jq -r '.[] | select(.name | test("^HEADLESS") | not) | .name' | head -n1)"
|
|
||||||
if [[ -n "$REAL_MON" ]]; then
|
if [[ -n "$REAL_MON" ]]; then
|
||||||
log "Returning workspace $PREV_WS → $REAL_MON"
|
log "Returning workspace $PREV_WS → $REAL_MON"
|
||||||
hyprctl dispatch moveworkspacetomonitor "$PREV_WS $REAL_MON" >/dev/null || true
|
hyprctl dispatch moveworkspacetomonitor "$PREV_WS $REAL_MON" >/dev/null || true
|
||||||
|
# If the do-hook promoted a non-active workspace because the active one was
|
||||||
|
# empty, ORIG_ACTIVE_WS differs from PREV_WS — restore focus to where the
|
||||||
|
# user actually was at connect time.
|
||||||
|
if [[ "$ORIG_ACTIVE_WS" != "$PREV_WS" ]]; then
|
||||||
|
log "Restoring focus to original active workspace $ORIG_ACTIVE_WS"
|
||||||
|
hyprctl dispatch workspace "$ORIG_ACTIVE_WS" >/dev/null || true
|
||||||
|
fi
|
||||||
hyprctl dispatch focusmonitor "$REAL_MON" >/dev/null || true
|
hyprctl dispatch focusmonitor "$REAL_MON" >/dev/null || true
|
||||||
else
|
else
|
||||||
log "No real monitor connected; leaving workspace assignment to Hyprland defaults."
|
log "No real monitor connected; leaving workspace assignment to Hyprland defaults."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Leave HEADLESS-1 in place. It needs to exist persistently for Sunshine's
|
# Clean state files but keep the directory + hook.log for the next run.
|
||||||
# encoder probe to succeed at startup; removing-and-recreating per session
|
rm -f "$STATE_DIR/prev-monitors.json" "$STATE_DIR/prev-workspace-id" "$STATE_DIR/orig-active-workspace-id"
|
||||||
# raced with the probe and caused fatal startup errors. Resizing on each
|
log "Stream teardown complete ($VIRT_MON kept alive for next connect)"
|
||||||
# new client (in sunshine-stream-do.sh) is enough — the output itself stays.
|
|
||||||
|
|
||||||
# Clean state files but keep the directory for the next run.
|
|
||||||
rm -f "$STATE_DIR/prev-monitors.json" "$STATE_DIR/prev-workspace-id"
|
|
||||||
log "Stream teardown complete (HEADLESS-1 kept for next connect)"
|
|
||||||
|
|||||||
@@ -69,8 +69,11 @@ $encoder_block
|
|||||||
# Threading — more threads helps high-bitrate H.265/AV1.
|
# Threading — more threads helps high-bitrate H.265/AV1.
|
||||||
min_threads = 4
|
min_threads = 4
|
||||||
|
|
||||||
# Use the PipeWire pulse compatibility layer for audio.
|
# Audio sink is intentionally left unset so Sunshine auto-detects the default
|
||||||
audio_sink = pulse
|
# PulseAudio/PipeWire sink and creates its virtual `sink-sunshine-stereo`.
|
||||||
|
# Hard-coding audio_sink = pulse here breaks capture: Sunshine treats it as a
|
||||||
|
# literal sink name, can't resolve its monitor source, and pa_simple_new()
|
||||||
|
# fails with "Invalid argument" → no audio in the stream.
|
||||||
|
|
||||||
# Keyboard / mouse / gamepad pass-through via /dev/uinput.
|
# Keyboard / mouse / gamepad pass-through via /dev/uinput.
|
||||||
# (Requires user to be in the 'input' group; install.sh handles this.)
|
# (Requires user to be in the 'input' group; install.sh handles this.)
|
||||||
|
|||||||
Reference in New Issue
Block a user