#!/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 1‑second cooldown before the next sound can start sleep 1 # Lock released automatically by the trap