From 7bc6d2789b267ded43a9e96c67d15b925dcc360d Mon Sep 17 00:00:00 2001 From: Levi Woodard Date: Thu, 21 May 2026 19:18:05 -0600 Subject: [PATCH] Adding functions --- bin/sunshine-stream-do.sh | 130 ++++++++++++++++++++++++++---------- bin/sunshine-stream-undo.sh | 54 ++++++++------- lib/config.sh | 7 +- 3 files changed, 129 insertions(+), 62 deletions(-) diff --git a/bin/sunshine-stream-do.sh b/bin/sunshine-stream-do.sh index 213d7a8..b3fc10f 100644 --- a/bin/sunshine-stream-do.sh +++ b/bin/sunshine-stream-do.sh @@ -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)" diff --git a/bin/sunshine-stream-undo.sh b/bin/sunshine-stream-undo.sh index 0051cc1..3452645 100644 --- a/bin/sunshine-stream-undo.sh +++ b/bin/sunshine-stream-undo.sh @@ -1,15 +1,25 @@ #!/usr/bin/env bash # Invoked by Sunshine as a stream-stop hook (global_prep_cmd `undo`). -# Moves the previously-active workspace back to a real monitor (if any -# exist) and tears down the headless output created by sunshine-stream-do.sh. +# Returns the promoted workspace to the original real monitor and restores +# 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 -log() { printf '[sunshine-undo] %s\n' "$*" >&2; } - +VIRT_MON="${OMARCHY_VIRTUAL_OUTPUT:-Virtual-1}" STATE_DIR="${XDG_RUNTIME_DIR:-/tmp}/sunshine-headless" -# Headless name was captured by sunshine-stream-do.sh; fall back to discovery. -MON="$(cat "$STATE_DIR/headless-name" 2>/dev/null || true)" +HOOK_LOG="$STATE_DIR/hook.log" +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 log "hyprctl not found; nothing to undo." @@ -29,29 +39,27 @@ if [[ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]]; then fi 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 - MON="$(hyprctl monitors -j 2>/dev/null \ - | jq -r '.[] | select(.name | startswith("HEADLESS")) | .name' | head -1)" -fi - -# 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)" +# Find a non-virtual monitor to move the promoted workspace back to. +REAL_MON="$(hyprctl monitors -j 2>/dev/null \ + | jq -r --arg m "$VIRT_MON" '.[] | select(.name != $m) | .name' \ + | head -n1)" if [[ -n "$REAL_MON" ]]; then log "Returning workspace $PREV_WS → $REAL_MON" 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 else log "No real monitor connected; leaving workspace assignment to Hyprland defaults." fi -# Leave HEADLESS-1 in place. It needs to exist persistently for Sunshine's -# encoder probe to succeed at startup; removing-and-recreating per session -# raced with the probe and caused fatal startup errors. Resizing on each -# 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)" +# Clean state files but keep the directory + hook.log for the next run. +rm -f "$STATE_DIR/prev-monitors.json" "$STATE_DIR/prev-workspace-id" "$STATE_DIR/orig-active-workspace-id" +log "Stream teardown complete ($VIRT_MON kept alive for next connect)" diff --git a/lib/config.sh b/lib/config.sh index 651bc9d..2e6ffaf 100644 --- a/lib/config.sh +++ b/lib/config.sh @@ -69,8 +69,11 @@ $encoder_block # Threading — more threads helps high-bitrate H.265/AV1. min_threads = 4 -# Use the PipeWire pulse compatibility layer for audio. -audio_sink = pulse +# Audio sink is intentionally left unset so Sunshine auto-detects the default +# 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. # (Requires user to be in the 'input' group; install.sh handles this.)