Motion-Wallpaper-Omarchy/motion-wallpaper-watcher
28allday c0c5758bfe Recover HYPRLAND_INSTANCE_SIGNATURE when launcher env is stripped
Running the TUI from XDG launchers (and some cron/ssh contexts) can leave
HYPRLAND_INSTANCE_SIGNATURE unset. hyprctl then prints a plain-text error
to stdout and exits 0, which made jq emit "Invalid numeric literal" to the
terminal before the tui_err message.

- ensure_hyprland_env: detect the running instance via `hyprctl instances`
  and export HIS before the first hyprctl call.
- get_monitors: validate JSON with `jq -e` before parsing, fall back to
  text-mode `hyprctl monitors` if the JSON is broken.
- pick_target: surface a clear error when HIS can't be recovered.
- watcher: same recovery path so auto-pause still works when launched
  without HIS in env.
2026-04-23 22:47:02 +01:00

87 lines
3 KiB
Bash

#!/usr/bin/env bash
# ==============================================================================
# Motion Wallpaper — auto-pause watcher.
#
# Subscribes to Hyprland's event socket (socket2) and toggles mpv pause/resume
# via the IPC socket that mpvpaper was started with. Replaces mpvpaper's own
# --auto-pause / -p flag, which is unreliable on recent Hyprland releases.
#
# This script runs as a background sibling to mpvpaper. The main toggle script
# starts it and kills it.
# ==============================================================================
set -euo pipefail
RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
MPV_IPC="$RUNTIME_DIR/motion-wallpaper-mpv.sock"
LOG_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/motion-wallpaper.log"
log() {
printf '[%s] watcher: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >> "$LOG_FILE"
}
if [ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]; then
# Recover the signature from `hyprctl instances` — same trick the toggle
# script uses when launched from an env that didn't inherit HIS.
sig="$(hyprctl instances 2>/dev/null | awk '/^instance /{sub(/:$/,"",$2); print $2; exit}')"
if [ -n "$sig" ]; then
export HYPRLAND_INSTANCE_SIGNATURE="$sig"
log "recovered HYPRLAND_INSTANCE_SIGNATURE=$sig from hyprctl instances"
else
log "HYPRLAND_INSTANCE_SIGNATURE not set and no instance found — exiting"
exit 0
fi
fi
HYPR_SOCK="$RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
if [ ! -S "$HYPR_SOCK" ]; then
log "hyprland event socket not found ($HYPR_SOCK) — exiting"
exit 0
fi
if ! command -v socat >/dev/null 2>&1; then
log "socat not installed — exiting"
exit 0
fi
send_mpv() {
# Best-effort — silently no-op if mpv's IPC socket isn't up yet.
[ -S "$MPV_IPC" ] || return 0
printf '%s\n' "$1" | socat - "UNIX-CONNECT:$MPV_IPC" >/dev/null 2>&1 || true
}
pause_mpv() { send_mpv '{ "command": ["set_property", "pause", true] }'; }
resume_mpv() { send_mpv '{ "command": ["set_property", "pause", false] }'; }
log "watching $HYPR_SOCK"
# Kill socat child on exit so it doesn't spam "Broken pipe" to the log when
# the watcher is killed by the main toggle script.
cleanup() {
# `[ ] && kill` short-circuits to non-zero when SOCAT_PID is unset, which
# under `set -e` would abort the trap before `exit 0` — wrap safely.
if [ -n "${SOCAT_PID:-}" ]; then
kill "$SOCAT_PID" 2>/dev/null || true
fi
exit 0
}
trap cleanup EXIT INT TERM
# Hyprland socket2 emits newline-separated "EVENT>>DATA" lines. We only care
# about the fullscreen state. `fullscreen>>1` = entered, `fullscreen>>0` = left.
# socat stderr is dropped so EPIPE on shutdown doesn't bloat the log.
coproc SOCAT { socat -u "UNIX-CONNECT:$HYPR_SOCK" - 2>/dev/null; }
# Bash auto-exports SOCAT_PID from `coproc SOCAT`; used in cleanup trap.
while IFS= read -r line <&"${SOCAT[0]}"; do
case "$line" in
fullscreen\>\>1)
log "fullscreen entered — pause"
pause_mpv
;;
fullscreen\>\>0)
log "fullscreen left — resume"
resume_mpv
;;
esac
done