summaryrefslogtreecommitdiff
path: root/.local/bin/notify-action.sh
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2023-10-30 21:21:18 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2023-10-30 21:21:18 -0400
commit0251add4f33d319cf8b43556481db9fb8c3715eb (patch)
tree694980989a5b65e8bdb2c2c7a294cd0ba52dfc45 /.local/bin/notify-action.sh
parentcb1a51092d12db12f7ba29e57daed6d64b311d38 (diff)
Notify-send.sh scripts
Diffstat (limited to '.local/bin/notify-action.sh')
-rwxr-xr-x.local/bin/notify-action.sh68
1 files changed, 68 insertions, 0 deletions
diff --git a/.local/bin/notify-action.sh b/.local/bin/notify-action.sh
new file mode 100755
index 0000000..7b8dbb0
--- /dev/null
+++ b/.local/bin/notify-action.sh
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+
+GDBUS_MONITOR_PID=/tmp/notify-action-dbus-monitor.$$.pid
+GDBUS_MONITOR=(gdbus monitor --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications)
+
+NOTIFICATION_ID="$1"
+if [[ -z "$NOTIFICATION_ID" ]]; then
+ echo "no notification id passed: $@"
+ exit 1;
+fi
+shift
+
+ACTION_COMMANDS=("$@")
+if [[ -z "$ACTION_COMMANDS" ]]; then
+ echo "no action commands passed: $@"
+ exit 1;
+fi
+
+cleanup() {
+ rm -f "$GDBUS_MONITOR_PID"
+}
+
+create_pid_file(){
+ rm -f "$GDBUS_MONITOR_PID"
+ umask 077
+ touch "$GDBUS_MONITOR_PID"
+}
+
+invoke_action() {
+ invoked_action_id="$1"
+ local action="" cmd=""
+ for index in "${!ACTION_COMMANDS[@]}"; do
+ if [[ $((index % 2)) == 0 ]]; then
+ action="${ACTION_COMMANDS[$index]}"
+ else
+ cmd="${ACTION_COMMANDS[$index]}"
+ if [[ "$action" == "$invoked_action_id" ]]; then
+ bash -c "${cmd}" &
+ fi
+ fi
+ done
+}
+
+monitor() {
+
+ create_pid_file
+ ( "${GDBUS_MONITOR[@]}" & echo $! >&3 ) 3>"$GDBUS_MONITOR_PID" | while read -r line
+ do
+ local closed_notification_id="$(sed '/^\/org\/freedesktop\/Notifications: org.freedesktop.Notifications.NotificationClosed (uint32 \([0-9]\+\), uint32 [0-9]\+)$/!d;s//\1/' <<< "$line")"
+ if [[ -n "$closed_notification_id" ]]; then
+ if [[ "$closed_notification_id" == "$NOTIFICATION_ID" ]]; then
+ invoke_action close
+ break
+ fi
+ else
+ local action_invoked="$(sed '/\/org\/freedesktop\/Notifications: org.freedesktop.Notifications.ActionInvoked (uint32 \([0-9]\+\), '\''\(.*\)'\'')$/!d;s//\1:\2/' <<< "$line")"
+ IFS=: read invoked_id action_id <<< "$action_invoked"
+ if [[ "$invoked_id" == "$NOTIFICATION_ID" ]]; then
+ invoke_action "$action_id"
+ break
+ fi
+ fi
+ done
+ kill $(<"$GDBUS_MONITOR_PID")
+ cleanup
+}
+
+monitor