102 lines
4.0 KiB
Bash
Executable File
102 lines
4.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# timer-run <total_seconds> [label]
|
|
# Runs entirely in background. No terminal needed after launch.
|
|
# Sends a desktop notification (with Dismiss / Snooze 2m / Snooze 5m action
|
|
# buttons, via mnotifd) + audio beep when done.
|
|
#
|
|
# Install: ~/.config/scripts/timer-run (companion: timer-pick in same dir)
|
|
|
|
TOTAL="${1:?missing seconds}"
|
|
LABEL="${2:-}"
|
|
|
|
# ── ensure runtime dir env vars are set for audio/dbus in detached context ────
|
|
if [[ -z "${XDG_RUNTIME_DIR:-}" ]]; then
|
|
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
|
|
fi
|
|
# paplay resolves the PulseAudio/PipeWire socket via PULSE_RUNTIME_PATH
|
|
if [[ -z "${PULSE_RUNTIME_PATH:-}" ]]; then
|
|
export PULSE_RUNTIME_PATH="${XDG_RUNTIME_DIR}/pulse"
|
|
fi
|
|
|
|
# ── detach from terminal immediately ──────────────────────────────────────────
|
|
# Preserve the full environment across the setsid re-exec (no login shell).
|
|
if [[ -t 0 || -t 1 || -t 2 ]]; then
|
|
( trap '' SIGHUP; exec setsid bash "$0" "$TOTAL" "$LABEL" </dev/null >/dev/null 2>&1 ) &
|
|
exit 0
|
|
fi
|
|
|
|
# ── sleep until done ──────────────────────────────────────────────────────────
|
|
sleep "$TOTAL"
|
|
|
|
# ── format a human-readable duration ─────────────────────────────────────────
|
|
fmt_dur() {
|
|
local s=$1 h=0 m=0 out=""
|
|
h=$(( s / 3600 )); s=$(( s % 3600 ))
|
|
m=$(( s / 60 )); s=$(( s % 60 ))
|
|
(( h > 0 )) && out+="${h}h "
|
|
(( m > 0 )) && out+="${m}m "
|
|
(( s > 0 || (h == 0 && m == 0) )) && out+="${s}s"
|
|
echo "${out% }"
|
|
}
|
|
|
|
DURATION_STR=$(fmt_dur "$TOTAL")
|
|
NOTIF_SUMMARY="⏰ Timer done${LABEL:+ — $LABEL}"
|
|
NOTIF_BODY="Set for ${DURATION_STR}. Finished at $(date '+%H:%M:%S')."
|
|
|
|
# ── desktop notification ──────────────────────────────────────────────────────
|
|
# notify-send needs DBUS_SESSION_BUS_ADDRESS. If not in env, find it via the
|
|
# running mnotifd notif daemon (reliable on single-user Wayland/Hyprland setups).
|
|
if [[ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ]]; then
|
|
local_uid=$(id -u)
|
|
# try to grab it from the environment of any process owned by this user
|
|
for pid in $(pgrep -u "$local_uid" -f '[m]notifd/main.py' 2>/dev/null); do
|
|
addr=$(cat /proc/$pid/environ 2>/dev/null \
|
|
| tr '\0' '\n' \
|
|
| grep '^DBUS_SESSION_BUS_ADDRESS=' \
|
|
| cut -d= -f2-)
|
|
if [[ -n $addr ]]; then
|
|
export DBUS_SESSION_BUS_ADDRESS="$addr"
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# ── audio alert (rings on a loop until dismissed/snoozed) ─────────────────────
|
|
_ALARM_SOUND="/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga"
|
|
|
|
play_alarm() {
|
|
if command -v paplay &>/dev/null && [[ -f "$_ALARM_SOUND" ]]; then
|
|
paplay "$_ALARM_SOUND" 2>/dev/null
|
|
elif command -v canberra-gtk-play &>/dev/null; then
|
|
canberra-gtk-play --id=alarm-clock-elapsed 2>/dev/null
|
|
fi
|
|
}
|
|
|
|
( while true; do play_alarm; sleep 1; done ) &
|
|
RING_PID=$!
|
|
|
|
# ── notification with action buttons ──────────────────────────────────────────
|
|
# -A implies --wait: notify-send blocks until the card is closed or an action
|
|
# is invoked, printing the chosen action's id to stdout. mnotifd supports the
|
|
# spec's Actions/ActionInvoked, so this works without any custom hint.
|
|
ACTION=""
|
|
if command -v notify-send &>/dev/null; then
|
|
ACTION=$(notify-send \
|
|
--urgency=critical \
|
|
--expire-time=0 \
|
|
--icon=alarm-timer \
|
|
-A "dismiss=Dismiss" \
|
|
-A "snooze2=Snooze 2m" \
|
|
-A "snooze5=Snooze 5m" \
|
|
"$NOTIF_SUMMARY" \
|
|
"$NOTIF_BODY")
|
|
fi
|
|
|
|
kill "$RING_PID" 2>/dev/null
|
|
wait "$RING_PID" 2>/dev/null
|
|
|
|
case "$ACTION" in
|
|
snooze2) exec bash "$0" 120 "$LABEL" ;;
|
|
snooze5) exec bash "$0" 300 "$LABEL" ;;
|
|
esac
|