Skip to content
74 changes: 74 additions & 0 deletions dbus-monitor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
#
# Usage:
# dbus-monitor.sh
# dbus-monitor.sh --profile # table like tab separated output
# dbus-monitor.sh --signals # only show signals
# dbus-monitor.sh FILTER # see https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
# dbus-monitor.sh type=signal

set -euo pipefail

ADDITIONAL_ARGS=()
FILTER=("path=/org/github/PaperWM")

script_name=$(basename "${BASH_SOURCE[0]}")

usage() {
cat <<EOF
Usage: ${script_name} [-h|--help] [--profile] [--signals] [FILTER]

Monitor dbus messages to/from PaperWM.

Available options:

--profile Output table like, tab seperated data
--signals Only show signals
FITLER Additional filter to be passed to dbus-monitor, multiple can be provided.

Any other options (prefixed with --) are passed as is to dbus-monitor.

EOF
}

main() {
while [[ $# -gt 0 ]]; do
case "$1" in
--profile)
ADDITIONAL_ARGS+=( --profile )
shift
;;
--signals)
FILTER+=( "type=signal" )
shift
;;
-h|--help)
usage
exit
;;
--*)
ADDITIONAL_ARGS+=( "$1" )
shift
;;
*)
FILTER+=( "$1" )
shift
;;
esac
done

local filter args
filter=$(join_by "," "${FILTER[@]}")
args=( "${ADDITIONAL_ARGS[@]}" )
args+=( "${filter}" )

{ set -x; dbus-monitor "${args[@]}"; { set +x; } &>/dev/null; } | tail -n +5
}

join_by() {
local IFS="$1"
shift
echo "$*"
}

main "$@"
108 changes: 108 additions & 0 deletions dbus-send.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env bash

set -euo pipefail

script_name=$(basename "${BASH_SOURCE[0]}")

usage() {
cat <<EOF
Usage: ${script_name} [-h|--help] COMMAND [...]

Send dbus messages to PaperWM.

Available commands:

action ACTION_NAME
list-actions

EOF
}

main() {
if [[ $# -eq 0 ]]; then
echo "ERROR: Command required." >&2
usage
exit 1
fi

while [[ $# -gt 0 ]]; do
case "$1" in
action)
shift
trigger_action "$1"
return
;;
list-actions)
shift
list_actions
return
;;
-h|--help)
usage
exit
;;
*)
echo "ERROR: Unknown command: $1" >&2
usage
exit 1
;;
esac
done
}

trigger_action() {
METHOD=TriggerAction call "string:$1"
}

list_actions() {
local result
result=$(METHOD=ListActions call)
while IFS=' ' read -ra words; do
for word in "${words[@]}"; do
case "${word}" in
array|\[|\])
;;
*)
echo "${word}"
;;
esac
done
done <<< "$result"
}

send() {
local args
args=( --dest=org.github.PaperWM )
if [[ -n "${OPTIONS:-}" ]]; then
args+=( "${OPTIONS}" )
fi
args+=(
/org/github/PaperWM
"org.github.PaperWM.${METHOD}"
"$@"
)

set -x
dbus-send "${args[@]}"
{ set +x; } &> /dev/null
}

call() {
OPTIONS=--print-reply=literal send "$@"
}

global_rematch() {
local s=$1 regex=$2
while [[ $s =~ $regex ]]; do
echo "${BASH_REMATCH[1]}"
s=${s#*"${BASH_REMATCH[1]}"}
done
}

join_by() {
local IFS="$1"
shift
echo "$*"
}

main "$@"
Loading