nix-files/hosts/common/programs/swaynotificationcenter/swaync-fbcli

75 lines
1.5 KiB
Plaintext
Executable File

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p feedbackd -p procps -p swaynotificationcenter
action="$1"
event="$2"
log() {
if [ -n "$SWAYNC_FBCLI_DEBUG" ]; then
printf "%s\n" "$1"
fi
}
child=
# kill children if killed, to allow that killing this parent process will end the real fbcli call
cleanup() {
log "aborting fbcli notification (PID $child)"
pkill -P "$child"
exit 0 # exit cleanly to avoid swaync alerting a script failure
}
startInline() {
local timeout=
case "$event" in
phone-incoming-call)
timeout=20
;;
*)
;;
esac
fbcliArgs=(fbcli --event "$event")
if [ -n "$timeout" ]; then
fbcliArgs+=(-t "$timeout")
fi
# feedbackd stops playback when the caller exits
# and fbcli will exit immediately if it has no stdin.
# so spoof a stdin.
log "${fbcliArgs[*]}"
sleep $((3 + ${timeout:+ + $timeout})) | ${fbcliArgs[@]}
}
start() {
# if in Do Not Disturb, don't do any feedback
# TODO: better solution is to actually make use of feedbackd profiles.
# i.e. set profile to `quiet` when in DnD mode
if [ "$SWAYNC_URGENCY" != "Critical" ] && [ "$(swaync-client --get-dnd)" = "true" ]; then
exit
fi
trap cleanup SIGINT SIGQUIT SIGTERM
startInline &
child=$!
wait
}
stop() {
pkill --echo --signal SIGINT --full "swaync-fbcli(-wrapped)? start $event"
}
case "$action" in
start)
start
;;
startInline)
startInline # support this for debugging
;;
stop)
stop
;;
esac