refactor: swaync notification sound cooldown;
fix: swaync bluetooth status check broken since bluetoothctl does not output anything unless in interactive mode
This commit is contained in:
@@ -127,7 +127,7 @@
|
||||
"label": "",
|
||||
"type": "toggle",
|
||||
"command": "sh -c '[[ $SWAYNC_TOGGLE_STATE == true ]] && bluetoothctl power on || bluetoothctl power off'",
|
||||
"update-command": "sh -c \'bluetoothctl show | awk \\\"/Powered/ {print \\$2}\\\" | grep -q yes && echo true || echo false\'"
|
||||
"update-command": "sh -c \'echo show | bluetoothctl | awk \\\"/Powered/ {print \\$2}\\\" | grep -q yes && echo true || echo false\'"
|
||||
},
|
||||
{
|
||||
"label": "",
|
||||
@@ -139,7 +139,7 @@
|
||||
"label": "",
|
||||
"type": "toggle",
|
||||
"command": "sh -c 'nohup ~/.config/hypr/scripts/toggle_proxy.sh > /dev/null 2>&1 & disown'",
|
||||
"update-command": "sh -c 'pgrep sslocal > /dev/null && echo true || echo false'"
|
||||
"update-command": "sh -c '~/.config/hypr/scripts/toggle_proxy.sh get'"
|
||||
},
|
||||
{
|
||||
"label": "",
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
#!/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"
|
||||
@@ -25,7 +39,6 @@ case $1 in
|
||||
;;
|
||||
esac
|
||||
|
||||
# Enable nullglob to prevent literal interpretation when no files match
|
||||
shopt -s nullglob
|
||||
|
||||
SOUND_FILES=()
|
||||
@@ -34,13 +47,16 @@ for ext in wav mp3 ogg flac; do
|
||||
done
|
||||
|
||||
if [ ${#SOUND_FILES[@]} -eq 0 ]; then
|
||||
echo "No sound files found in $SOUND_NORMAL_DIR"
|
||||
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 || {
|
||||
echo "Failed to play $RANDOM_SOUND"
|
||||
}
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user