#!/usr/bin/env bash # Set up headless streaming mode: install prep-cmd hook scripts into a stable # location and ensure they're executable. The actual sunshine.conf entries # (capture=wlr, output_name=HEADLESS-1, global_prep_cmd=[...]) are written # by lib/config.sh. # # Two compositor backends supported: # - hyprland (default on Omarchy/Arch): bin/sunshine-stream-{do,undo}.sh # - sway (default on Debian/Ubuntu): bin/sunshine-stream-{do,undo}-sway.sh # detect_compositor (lib/detect.sh) decides which to install. The script names # at the install target are *always* sunshine-stream-do.sh / -undo.sh, so the # rest of the installer (config.sh, verify.sh) doesn't have to branch. HEADLESS_BIN_DIR="$HOME/.local/share/omarchy-moonlight/bin" DO_SCRIPT="$HEADLESS_BIN_DIR/sunshine-stream-do.sh" UNDO_SCRIPT="$HEADLESS_BIN_DIR/sunshine-stream-undo.sh" PRESTART_SCRIPT="$HEADLESS_BIN_DIR/sunshine-prestart.sh" export DO_SCRIPT UNDO_SCRIPT PRESTART_SCRIPT # Resolve which hook source files to install based on the detected compositor. _headless_hook_sources() { case "${COMPOSITOR:-hyprland}" in sway) echo "$SCRIPT_DIR/bin/sunshine-stream-do-sway.sh" echo "$SCRIPT_DIR/bin/sunshine-stream-undo-sway.sh" echo "$SCRIPT_DIR/bin/sunshine-prestart-sway.sh" ;; hyprland|*) echo "$SCRIPT_DIR/bin/sunshine-stream-do.sh" echo "$SCRIPT_DIR/bin/sunshine-stream-undo.sh" echo "$SCRIPT_DIR/bin/sunshine-prestart.sh" ;; esac } install_headless_hooks() { mkdir -p "$HEADLESS_BIN_DIR" mapfile -t srcs < <(_headless_hook_sources) local do_src="${srcs[0]}" undo_src="${srcs[1]}" pre_src="${srcs[2]}" install -m 0755 "$do_src" "$DO_SCRIPT" install -m 0755 "$undo_src" "$UNDO_SCRIPT" install -m 0755 "$pre_src" "$PRESTART_SCRIPT" ok "Installed prep-cmd + prestart hooks ($COMPOSITOR) to $HEADLESS_BIN_DIR" } # Install a systemd-user drop-in that pre-creates HEADLESS-1 before Sunshine # starts. Without this, Sunshine reports a fatal "Unable to find display or # encoder during startup" on every restart, even though streaming works once # a client connects. install_headless_prestart_dropin() { local dropin_src="$SCRIPT_DIR/files/headless-prestart.conf" if [[ ! -f "$dropin_src" ]]; then err "Drop-in source missing: $dropin_src" return 1 fi # Resolve the actual unit name. Prefer sunshine.service when present (alias, # sunshine-bin, or the .deb on Ubuntu); fall back to the AUR source pkg's # reverse-DNS name. local unit="" for u in sunshine.service app-dev.lizardbyte.app.Sunshine.service; do if systemctl --user list-unit-files "$u" >/dev/null 2>&1 \ && systemctl --user cat "$u" >/dev/null 2>&1; then unit="$u" break fi done if [[ -z "$unit" ]]; then warn "Could not resolve a sunshine unit to attach the drop-in to; skipping." return 0 fi local dropin_dir="$HOME/.config/systemd/user/${unit}.d" mkdir -p "$dropin_dir" install -m 0644 "$dropin_src" "$dropin_dir/headless-prestart.conf" systemctl --user daemon-reload ok "Installed headless prestart drop-in at $dropin_dir/headless-prestart.conf" }