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

63 lines
1.3 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
}
# 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
}
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
local timeout=
case "$event" in
phone-incoming-call)
timeout=20
;;
*)
;;
esac
local cli="fbcli --event $event ${timeout:+ -t $timeout}"
# feedbackd stops playback when the caller exits
# and fbcli will exit immediately if it has no stdin.
# so spoof a stdin.
log "$cli"
/bin/sh -c "sleep $((3 ${timeout:+ + $timeout})) | $cli" &
child=$!
wait
}
stop() {
pkill -e -f "swaync-fbcli(-wrapped)? start $event"
}
case "$action" in
start)
start
;;
stop)
stop
;;
esac