Files
dotfiles/swaync/scripts/sound.sh
Nikolai Papin 80cab05851 refactor: swaync notification sound cooldown;
fix: swaync bluetooth status check broken since bluetoothctl does not output anything unless in interactive mode
2026-03-19 02:16:56 +03:00

63 lines
1.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Exit silently if Do Not Disturb is enabled
if [ $(swaync-client --get-dnd) == "true" ]; then
echo "Do Not Disturb is on, skipping sound"
exit 0
fi
LOCKFILE="/tmp/swaync-sound-lock-$UID"
# Open file descriptor 200 for locking
exec 200>"$LOCKFILE"
# Try to acquire an exclusive lock without waiting (non-blocking)
if ! flock -x -n 200; then
# Another sound is playing or in cooldown ignore this notification
exit 0
fi
# Lock acquired: set a trap to release it on exit
trap 'flock -u 200' EXIT
SOUND_LOW_DIR="$HOME/.config/swaync/scripts/sounds/low"
SOUND_NORMAL_DIR="$HOME/.config/swaync/scripts/sounds/normal"
SOUND_CRITICAL_DIR="$HOME/.config/swaync/scripts/sounds/critical"
case $1 in
low)
DIR=$SOUND_LOW_DIR
;;
normal)
DIR=$SOUND_NORMAL_DIR
;;
critical)
DIR=$SOUND_CRITICAL_DIR
;;
*)
echo "Unknown urgency level '$1'"
exit 1
;;
esac
shopt -s nullglob
SOUND_FILES=()
for ext in wav mp3 ogg flac; do
SOUND_FILES+=("$DIR"/*."$ext")
done
if [ ${#SOUND_FILES[@]} -eq 0 ]; then
echo "No sound files found in $DIR"
exit 0
fi
RANDOM_INDEX=$((RANDOM % ${#SOUND_FILES[@]}))
RANDOM_SOUND="${SOUND_FILES[$RANDOM_INDEX]}"
play -v 0.5 "$RANDOM_SOUND" 2>/dev/null & disown;
# Enforce a 1second cooldown before the next sound can start
sleep 1
# Lock released automatically by the trap