Skip to content

Instantly share code, notes, and snippets.

@ampling
Created October 31, 2016 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ampling/70394ac7cc3ca9b079d0cdb1bc377c14 to your computer and use it in GitHub Desktop.
Save ampling/70394ac7cc3ca9b079d0cdb1bc377c14 to your computer and use it in GitHub Desktop.
A soundboard for dmenu.
#!/usr/bin/env bash
shopt -s nullglob globstar failglob
set -euo pipefail
arg1=${1:-}
arg2=${2:-}
notifyit=0
if [[ '--notify' == "$arg1" ]] || [[ '--notify' == "$arg2" ]]; then
notifyit=1;
shift
fi
prefix="${SOUND_STORE_DIR:-$HOME/audio}"
sound_files=( "$prefix"/**/* )
sound_files=( "${sound_files[@]#"$prefix"/}" )
sound_files=( "${sound_files[@]% }" )
sound=( '' )
cleanup=( '' )
userID=( "$(id -u $(whoami))" )
PROGRAM="${0##*/}"
_finish () {
[[ True == $cleanup ]] &&
printf "line 28"
[[ True = "$cleanup" ]] &&
if compgen -G "/tmp/soundmenu.1000*" >/dev/null 2>&1 ;then
rmdir /tmp/soundmenu.*.*."$$".lock >/dev/null 2>&1
fi
exit
}
_notify () {
message=$1
[[ "$notifyit" == 1 ]] &&
notify-send "$message" --icon=dialog-information ||
printf '%s\n' "$message"
}
( [[ "$notifyit" == 1 ]] & hash notify-send 2>/dev/null ||
{ printf '%s\n' "e: cannot find libnotify."; exit 1; } )
## dmenu exits on KeyPress not KeyRelease.
for sound in $(printf '%s\n' "${sound_files[@]}" | dmenu -f "$@"); do
passel+=("$sound")
done
[[ -n $sound ]] || exit
# Runs the _finish funtion on exit.
trap _finish EXIT
## Clearing old filelock(s).
umask 077
stalelock=( "${stalelock:-''}" )
stalelock=( "$(find '/tmp' -maxdepth 1 -name "soundmenu."$userID".*" -user $(whoami) -print0 -quit -type d)" ) &&
if test -n "$stalelock" ;then
stalename=${stalelock%.*}
report=( "$(ps -u $(id -u $(whoami)) aux | grep "bash" |
grep "soundmenu" | grep -v "$$")" )
stalePID=( "$(printf $stalename |
sed -e "s/\/tmp\/soundmenu\.[0-9]\{1,6\}\?\..*\.//g")" )
if [[ "$report" == *"$stalePID"* ]] ;then
kill "$stalePID" || exit 1
else
rmdir /tmp/soundmenu."$userID".* >/dev/null 2>&1 ||
{ _notify ":: Unable to clear old filelock"; exit 1; }
fi
fi
## Adding a new filelock
( mktemp -d "/tmp/soundmenu."$userID".XXXXXXXXXX"."$$".lock >/dev/null 2>&1 && cleanup=True ||
{ _notify ":: Unable to make filelock."; exit 1; } )
cleanup=True
if [ ${#passel[@]} -gt "1" ]; then
round=0
for entry in "${passel[@]}"; do
mpv "$prefix/$entry"
_notify "Sent "${passel[$round]}" via "mpv""
round=`expr $round + 1`
done
else
mpv "$prefix/$sound"
_notify "Sent "$sound" via "mpv""
sleep "$CLIP_TIME"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment