407 lines
10 KiB
Bash
407 lines
10 KiB
Bash
#!/bin/sh
|
|||
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||
|
|
# Copyright 2022 Sxmo Contributors
|
||
|
|
# title="$icon_clk Timer"
|
||
|
|
# include common definitions
|
||
|
|
# shellcheck source=scripts/core/sxmo_common.sh
|
||
|
|
. sxmo_common.sh
|
||
|
|
|
||
|
|
RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp}"
|
||
|
|
TIMER_DIR="${SXMO_TIMER_DIR:-$RUNTIME_DIR/sxmo_timers}"
|
||
|
|
RING_FLAG="${SXMO_TIMER_RING_FLAG:-/tmp/sxmo_timer_ringing}"
|
||
|
|
TIMER_FILE=""
|
||
|
|
CHILD_SLEEP_PID=""
|
||
|
|
CHILD_SOUND_PID=""
|
||
|
|
CHILD_VIB_PID=""
|
||
|
|
ORIG_STTY=""
|
||
|
|
|
||
|
|
_cleanup_children() {
|
||
|
|
for p in "$CHILD_SLEEP_PID" "$CHILD_SOUND_PID" "$CHILD_VIB_PID"; do
|
||
|
|
if [ -n "$p" ]; then
|
||
|
|
kill "$p" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
CHILD_SLEEP_PID=""
|
||
|
|
CHILD_SOUND_PID=""
|
||
|
|
CHILD_VIB_PID=""
|
||
|
|
}
|
||
|
|
|
||
|
|
_finish_timerrun() {
|
||
|
|
trap - HUP INT TERM EXIT
|
||
|
|
_cleanup_children
|
||
|
|
if [ -n "$TIMER_FILE" ]; then
|
||
|
|
rm -f "$TIMER_FILE" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
if [ -f "$RING_FLAG" ]; then
|
||
|
|
cur_ring_pid=""
|
||
|
|
IFS= read -r cur_ring_pid < "$RING_FLAG" 2>/dev/null || true
|
||
|
|
if [ -z "$cur_ring_pid" ] || [ "$cur_ring_pid" = "$$" ]; then
|
||
|
|
rm -f "$RING_FLAG" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
if [ -n "$ORIG_STTY" ] && [ -t 0 ]; then
|
||
|
|
stty "$ORIG_STTY" 2>/dev/null || true
|
||
|
|
ORIG_STTY=""
|
||
|
|
fi
|
||
|
|
exit 0
|
||
|
|
}
|
||
|
|
|
||
|
|
_fmt_hms() {
|
||
|
|
sec="$1"
|
||
|
|
[ "$sec" -lt 0 ] 2>/dev/null && sec=0
|
||
|
|
h=$(( sec / 3600 ))
|
||
|
|
m=$(( (sec % 3600) / 60 ))
|
||
|
|
s=$(( sec % 60 ))
|
||
|
|
printf '%02d:%02d:%02d' "$h" "$m" "$s"
|
||
|
|
}
|
||
|
|
|
||
|
|
_prune_stale_timers() {
|
||
|
|
[ -d "$TIMER_DIR" ] || return 0
|
||
|
|
for f in "$TIMER_DIR"/*.timer; do
|
||
|
|
[ -e "$f" ] || continue
|
||
|
|
pid="${f##*/}"
|
||
|
|
pid="${pid%.timer}"
|
||
|
|
case "$pid" in
|
||
|
|
''|*[!0-9]*)
|
||
|
|
rm -f "$f" 2>/dev/null || true
|
||
|
|
continue
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
if ! kill -0 "$pid" 2>/dev/null; then
|
||
|
|
rm -f "$f" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
if [ -f "$RING_FLAG" ]; then
|
||
|
|
rpid=""
|
||
|
|
IFS= read -r rpid < "$RING_FLAG" 2>/dev/null || true
|
||
|
|
case "$rpid" in
|
||
|
|
''|*[!0-9]*) rm -f "$RING_FLAG" 2>/dev/null || true ;;
|
||
|
|
*) kill -0 "$rpid" 2>/dev/null || rm -f "$RING_FLAG" 2>/dev/null || true ;;
|
||
|
|
esac
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
status() {
|
||
|
|
_prune_stale_timers
|
||
|
|
found=1
|
||
|
|
if [ -d "$TIMER_DIR" ]; then
|
||
|
|
for f in "$TIMER_DIR"/*.timer; do
|
||
|
|
[ -e "$f" ] || continue
|
||
|
|
pid="${f##*/}"
|
||
|
|
pid="${pid%.timer}"
|
||
|
|
state=""
|
||
|
|
end_ts=""
|
||
|
|
label=""
|
||
|
|
IFS=' ' read -r state end_ts label < "$f" 2>/dev/null || continue
|
||
|
|
printf '%s %s %s %s\n' "$pid" "$state" "$end_ts" "$label"
|
||
|
|
found=0
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
return "$found"
|
||
|
|
}
|
||
|
|
|
||
|
|
stop() {
|
||
|
|
_prune_stale_timers
|
||
|
|
if [ -d "$TIMER_DIR" ]; then
|
||
|
|
for f in "$TIMER_DIR"/*.timer; do
|
||
|
|
[ -e "$f" ] || continue
|
||
|
|
pid="${f##*/}"
|
||
|
|
pid="${pid%.timer}"
|
||
|
|
kill -TERM "$pid" 2>/dev/null || true
|
||
|
|
rm -f "$f" 2>/dev/null || true
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
rm -f "$RING_FLAG" 2>/dev/null || true
|
||
|
|
killall -q -TERM sxmo_vibrate bananarchy-melody pw-play 2>/dev/null || true
|
||
|
|
dunstctl close-all >/dev/null 2>&1 || true
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
stop_ringing() {
|
||
|
|
_prune_stale_timers
|
||
|
|
stopped=0
|
||
|
|
if [ -d "$TIMER_DIR" ]; then
|
||
|
|
for f in "$TIMER_DIR"/*.timer; do
|
||
|
|
[ -e "$f" ] || continue
|
||
|
|
pid="${f##*/}"
|
||
|
|
pid="${pid%.timer}"
|
||
|
|
state=""
|
||
|
|
end_ts=""
|
||
|
|
label=""
|
||
|
|
IFS=' ' read -r state end_ts label < "$f" 2>/dev/null || continue
|
||
|
|
if [ "$state" = "ringing" ]; then
|
||
|
|
kill -TERM "$pid" 2>/dev/null || true
|
||
|
|
rm -f "$f" 2>/dev/null || true
|
||
|
|
stopped=1
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
if [ -f "$RING_FLAG" ]; then
|
||
|
|
rpid=""
|
||
|
|
IFS= read -r rpid < "$RING_FLAG" 2>/dev/null || true
|
||
|
|
case "$rpid" in
|
||
|
|
''|*[!0-9]*) ;;
|
||
|
|
*) kill -TERM "$rpid" 2>/dev/null || true ;;
|
||
|
|
esac
|
||
|
|
rm -f "$RING_FLAG" 2>/dev/null || true
|
||
|
|
stopped=1
|
||
|
|
fi
|
||
|
|
if [ "$stopped" -eq 1 ]; then
|
||
|
|
killall -q -TERM sxmo_vibrate bananarchy-melody pw-play 2>/dev/null || true
|
||
|
|
dunstctl close-all >/dev/null 2>&1 || true
|
||
|
|
fi
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
_is_dnd_active() {
|
||
|
|
[ -f "$RUNTIME_DIR/sxmo.busy" ] && return 0
|
||
|
|
cfg_home="${XDG_CONFIG_HOME:-$HOME/.config}"
|
||
|
|
if [ -f "$cfg_home/sxmo/.busy" ]; then
|
||
|
|
end=""
|
||
|
|
IFS= read -r end < "$cfg_home/sxmo/.busy" 2>/dev/null || true
|
||
|
|
case "$end" in
|
||
|
|
'') return 0 ;;
|
||
|
|
*[!0-9]*) return 1 ;;
|
||
|
|
*) [ "$(date +%s)" -lt "$end" ] && return 0 ;;
|
||
|
|
esac
|
||
|
|
fi
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
|
||
|
|
_play_timer_alert() {
|
||
|
|
_is_dnd_active && return 0
|
||
|
|
cfg_home="${XDG_CONFIG_HOME:-$HOME/.config}"
|
||
|
|
if [ -f "$cfg_home/sxmo/ringsettings" ]; then
|
||
|
|
# shellcheck disable=SC1091
|
||
|
|
. "$cfg_home/sxmo/ringsettings"
|
||
|
|
fi
|
||
|
|
notif_sound=""
|
||
|
|
if [ -s "$cfg_home/sxmo/notif_sound" ]; then
|
||
|
|
IFS= read -r notif_sound < "$cfg_home/sxmo/notif_sound" || true
|
||
|
|
fi
|
||
|
|
[ -z "$notif_sound" ] && notif_sound="${SXMO_TEXTSOUND:-}"
|
||
|
|
if [ -z "$notif_sound" ]; then
|
||
|
|
if [ -f "/usr/share/sxmo/ringtones/Soft_Bell.wav" ]; then
|
||
|
|
notif_sound="/usr/share/sxmo/ringtones/Soft_Bell.wav"
|
||
|
|
elif [ -f "/usr/share/sxmo/notify.ogg" ]; then
|
||
|
|
notif_sound="/usr/share/sxmo/notify.ogg"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
if [ ! -f "$cfg_home/sxmo/.noring" ] && \
|
||
|
|
[ "$notif_sound" != "NONE" ] && [ "$notif_sound" != "none" ] && \
|
||
|
|
[ -n "$notif_sound" ] && [ -f "$notif_sound" ]; then
|
||
|
|
case "${SXMO_RINGVOL:-100}" in
|
||
|
|
''|*[!0-9]*) SXMO_RINGVOL=100 ;;
|
||
|
|
esac
|
||
|
|
if [ "$SXMO_RINGVOL" -gt 0 ]; then
|
||
|
|
notif_ext="$(printf '%s' "${notif_sound##*.}" | tr '[:upper:]' '[:lower:]')"
|
||
|
|
notif_play="$notif_sound"
|
||
|
|
case "$notif_ext" in
|
||
|
|
nrt|rtttl|rtx|emy|imy)
|
||
|
|
notif_play="$RUNTIME_DIR/sxmo_monotone_timer.wav"
|
||
|
|
bananarchy-melody --wav "$notif_sound" "$notif_play" >/dev/null 2>&1 || notif_play=""
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
if [ -n "$notif_play" ] && [ -f "$notif_play" ]; then
|
||
|
|
if command -v pw-play >/dev/null 2>&1 && [ -S "$RUNTIME_DIR/pipewire-0" ]; then
|
||
|
|
pw_vol="$(awk -v v="$SXMO_RINGVOL" 'BEGIN { s = v / 100.0; if (s < 0.0) s = 0.0; if (s > 1.0) s = 1.0; printf "%.2f", s }')"
|
||
|
|
pw-play --volume="$pw_vol" "$notif_play" >/dev/null 2>&1 &
|
||
|
|
CHILD_SOUND_PID=$!
|
||
|
|
elif command -v mpv >/dev/null 2>&1; then
|
||
|
|
mpv_vol=$(( SXMO_RINGVOL * 130 / 100 ))
|
||
|
|
mpv --no-config --no-terminal --no-resume-playback --quiet --no-video \
|
||
|
|
--end=3 --volume-max=150 --volume="$mpv_vol" "$notif_play" >/dev/null 2>&1 &
|
||
|
|
CHILD_SOUND_PID=$!
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
if [ ! -f "$cfg_home/sxmo/.novibrate" ]; then
|
||
|
|
sxmo_vibrate 800 "${SXMO_VIBRATE_STRENGTH:-2}" >/dev/null 2>&1 &
|
||
|
|
CHILD_VIB_PID=$!
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
_read_tty_key_1s() {
|
||
|
|
dd bs=1 count=1 2>/dev/null | LC_ALL=C tr '\r\n\033\177\010' 'EEXBB' 2>/dev/null || true
|
||
|
|
}
|
||
|
|
|
||
|
|
_sleep_1s_or_cancel_key() {
|
||
|
|
if [ -t 0 ]; then
|
||
|
|
key="$(_read_tty_key_1s)"
|
||
|
|
case "$key" in
|
||
|
|
E|"0"|"q"|"Q"|'*'|'#'|X|B)
|
||
|
|
return 0
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
sxmo_aligned_sleep 1 >/dev/null 2>&1 &
|
||
|
|
CHILD_SLEEP_PID=$!
|
||
|
|
wait "$CHILD_SLEEP_PID" 2>/dev/null || true
|
||
|
|
CHILD_SLEEP_PID=""
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
|
||
|
|
_sleep_1s_or_any_key() {
|
||
|
|
if [ -t 0 ]; then
|
||
|
|
key="$(_read_tty_key_1s)"
|
||
|
|
[ -n "$key" ] && return 0
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
sleep 1 &
|
||
|
|
CHILD_SLEEP_PID=$!
|
||
|
|
wait "$CHILD_SLEEP_PID" 2>/dev/null || true
|
||
|
|
CHILD_SLEEP_PID=""
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
|
||
|
|
timerrun() {
|
||
|
|
trap '_finish_timerrun' HUP INT TERM EXIT
|
||
|
|
|
||
|
|
RAW_LABEL="$*"
|
||
|
|
TIME="$(
|
||
|
|
printf '%s\n' "$@" |
|
||
|
|
sed 's/\([^0-9]\)\([0-9]\)/\1+\2/g; s/h/*60m/g; s/m/*60s/g; s/s//g' |
|
||
|
|
bc 2>/dev/null
|
||
|
|
)"
|
||
|
|
case "$TIME" in
|
||
|
|
''|*[!0-9]*)
|
||
|
|
printf 'Invalid timer duration: %s\n' "$RAW_LABEL" >&2
|
||
|
|
exit 1
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
if [ "$TIME" -le 0 ]; then
|
||
|
|
printf 'Timer duration must be > 0s\n' >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
mkdir -p "$TIMER_DIR" 2>/dev/null || true
|
||
|
|
TIMER_FILE="$TIMER_DIR/$$.timer"
|
||
|
|
NOW="$(date +%s)"
|
||
|
|
DATE1="$((NOW + TIME))"
|
||
|
|
printf 'running %s %s\n' "$DATE1" "$RAW_LABEL" > "$TIMER_FILE"
|
||
|
|
|
||
|
|
if [ -t 0 ]; then
|
||
|
|
ORIG_STTY="$(stty -g 2>/dev/null || true)"
|
||
|
|
stty -icanon -echo min 0 time 10 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
|
||
|
|
printf 'Timer: %s\nPress Enter, 0, or q to cancel\n' "$RAW_LABEL"
|
||
|
|
while :; do
|
||
|
|
NOW="$(date +%s)"
|
||
|
|
REM=$((DATE1 - NOW))
|
||
|
|
if [ "$REM" -le 0 ]; then
|
||
|
|
break
|
||
|
|
fi
|
||
|
|
printf '\r%s ' "$(_fmt_hms "$REM")"
|
||
|
|
if _sleep_1s_or_cancel_key; then
|
||
|
|
printf '\nTimer cancelled.\n'
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
|
||
|
|
NOW="$(date +%s)"
|
||
|
|
printf 'ringing %s %s\n' "$NOW" "$RAW_LABEL" > "$TIMER_FILE"
|
||
|
|
printf '%s\n' "$$" > "$RING_FLAG"
|
||
|
|
|
||
|
|
printf '\r00:00:00 \nDone with %s\nPress any key or Menu -> Stop Timer\n' "$RAW_LABEL"
|
||
|
|
|
||
|
|
/usr/bin/bananarchy-screen-wake.sh >/dev/null 2>&1 || true
|
||
|
|
/usr/bin/bananarchy-cover-event.sh notification >/dev/null 2>&1 || true
|
||
|
|
if command -v dunstify >/dev/null 2>&1; then
|
||
|
|
dunstify -u critical -r 991042 -t 30000 "Timer" "Done with $RAW_LABEL" >/dev/null 2>&1 || true
|
||
|
|
else
|
||
|
|
notify-send -u critical -t 30000 "Done with $RAW_LABEL" >/dev/null 2>&1 || true
|
||
|
|
fi
|
||
|
|
|
||
|
|
MAX_RING_SECS="${SXMO_TIMER_RING_SECS:-30}"
|
||
|
|
case "$MAX_RING_SECS" in
|
||
|
|
''|*[!0-9]*) MAX_RING_SECS=30 ;;
|
||
|
|
esac
|
||
|
|
RING_END="$(( $(date +%s) + MAX_RING_SECS ))"
|
||
|
|
|
||
|
|
while [ -f "$RING_FLAG" ] && [ "$(date +%s)" -lt "$RING_END" ]; do
|
||
|
|
_play_timer_alert
|
||
|
|
for _tick in 1 2; do
|
||
|
|
[ ! -f "$RING_FLAG" ] && break 2
|
||
|
|
[ "$(date +%s)" -ge "$RING_END" ] && break 2
|
||
|
|
if _sleep_1s_or_any_key; then
|
||
|
|
printf '\nTimer stopped.\n'
|
||
|
|
_cleanup_children
|
||
|
|
break 2
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
done
|
||
|
|
}
|
||
|
|
|
||
|
|
stopwatchrun() {
|
||
|
|
reset() {
|
||
|
|
start="$(date +%s)"
|
||
|
|
printf '\33[1A\33[2K\r%s\n' "$(date -u -d "@$time" +%H:%M:%S)"
|
||
|
|
if [ -n "$sid" ]; then
|
||
|
|
kill "$sid" >/dev/null 2>&1 || true
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
finish() {
|
||
|
|
jobs -p | xargs -r kill >/dev/null 2>&1 || true
|
||
|
|
wait 2>/dev/null || true
|
||
|
|
exit 0
|
||
|
|
}
|
||
|
|
trap finish HUP INT TERM EXIT
|
||
|
|
start="$(date +%s)"
|
||
|
|
printf 'Stopwatch (Enter=Lap, q/0=Quit)\n'
|
||
|
|
(
|
||
|
|
trap reset USR1
|
||
|
|
while :; do
|
||
|
|
time="$(($(date +%s) - start))"
|
||
|
|
printf '\33[2K\r%s' "$(date -u -d "@$time" +%H:%M:%S)"
|
||
|
|
sxmo_aligned_sleep 1 &
|
||
|
|
sid=$!
|
||
|
|
wait
|
||
|
|
done
|
||
|
|
) &
|
||
|
|
jid=$!
|
||
|
|
while IFS= read -r line; do
|
||
|
|
case "$line" in
|
||
|
|
q|Q|0)
|
||
|
|
kill "$jid" >/dev/null 2>&1 || true
|
||
|
|
exit 0
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
kill -s USR1 "$jid" 2>/dev/null || true
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
}
|
||
|
|
|
||
|
|
menu() {
|
||
|
|
_prune_stale_timers
|
||
|
|
stop_entry=""
|
||
|
|
if [ -f "$RING_FLAG" ]; then
|
||
|
|
stop_entry="Stop Timer\n"
|
||
|
|
elif status >/dev/null 2>&1; then
|
||
|
|
stop_entry="Cancel Active Timer\n"
|
||
|
|
fi
|
||
|
|
TIMEINPUT="$(printf '%bStopwatch\n1h\n30m\n15m\n10m\n9m\n8m\n7m\n6m\n5m\n4m\n3m\n2m\n1m\n30s\nClose Menu\n' "$stop_entry" | sxmo_dmenu.sh -p Timer)" || exit
|
||
|
|
case "$TIMEINPUT" in
|
||
|
|
"Close Menu"|"")
|
||
|
|
exit 0
|
||
|
|
;;
|
||
|
|
"Stop Timer"|"Cancel Active Timer")
|
||
|
|
stop
|
||
|
|
;;
|
||
|
|
"Stopwatch")
|
||
|
|
sxmo_terminal.sh "$0" stopwatchrun
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
sxmo_terminal.sh "$0" timerrun "$TIMEINPUT"
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
}
|
||
|
|
|
||
|
|
if [ $# -gt 0 ]; then
|
||
|
|
"$@"
|
||
|
|
else
|
||
|
|
menu
|
||
|
|
fi
|