132 lines
5.8 KiB
Bash
132 lines
5.8 KiB
Bash
#!/usr/bin/env bash
|
|
# Invoked by Sunshine as a stream-start hook (global_prep_cmd `do`).
|
|
# 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
|
|
|
|
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 virtual display. Stream may show whatever Sunshine selects."
|
|
exit 0
|
|
fi
|
|
|
|
# Recover Hyprland signature if it wasn't inherited (defensive — UWSM exports it,
|
|
# but a stray service environment could miss it).
|
|
if [[ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]]; then
|
|
for sig_dir in "${XDG_RUNTIME_DIR:-/tmp}"/hypr/*/; do
|
|
[[ -d "$sig_dir" ]] || continue
|
|
export HYPRLAND_INSTANCE_SIGNATURE="$(basename "$sig_dir")"
|
|
log "Discovered HYPRLAND_INSTANCE_SIGNATURE=$HYPRLAND_INSTANCE_SIGNATURE"
|
|
break
|
|
done
|
|
if [[ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]]; then
|
|
log "Hyprland not running; nothing to configure."
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
# 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
|
|
|
|
# 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"
|
|
|
|
# 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"
|
|
|
|
# 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)"
|