| 1 | #!/bin/sh |
| 2 | |
| 3 | # == Declare stderr function === |
| 4 | |
| 5 | stderr() { |
| 6 | printf "\033[1;31m%s\n\033[0m" "$@" >&2 |
| 7 | } |
| 8 | |
| 9 | # === Verify `picom --dbus` status === |
| 10 | |
| 11 | if [ -z "$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep compton)" ]; then |
| 12 | stderr "picom DBus interface unavailable" |
| 13 | if [ -n "$(pgrep picom)" ]; then |
| 14 | stderr "picom running without dbus interface" |
| 15 | #killall picom & # Causes all windows to flicker away and come back ugly. |
| 16 | #picom --dbus & # Causes all windows to flicker away and come back beautiful |
| 17 | else |
| 18 | stderr "picom not running" |
| 19 | fi |
| 20 | exit 1 |
| 21 | fi |
| 22 | |
| 23 | # === Setup sed === |
| 24 | |
| 25 | SED="${SED:-$(command -v gsed || printf 'sed')}" |
| 26 | |
| 27 | # === Get connection parameters === |
| 28 | |
| 29 | dpy=$(printf "$DISPLAY" | tr -c '[:alnum:]' _) |
| 30 | |
| 31 | if [ -z "$dpy" ]; then |
| 32 | stderr "Cannot find display." |
| 33 | exit 1 |
| 34 | fi |
| 35 | |
| 36 | service="com.github.chjj.compton.${dpy}" |
| 37 | interface="com.github.chjj.compton" |
| 38 | picom_dbus="dbus-send --print-reply --dest="${service}" / "${interface}"." |
| 39 | type_win='uint32' |
| 40 | type_enum='uint32' |
| 41 | |
| 42 | # === Color Inversion === |
| 43 | |
| 44 | # Get window ID of window to invert |
| 45 | if [ -z "$1" -o "$1" = "selected" ]; then |
| 46 | window=$(xwininfo -frame | sed -n 's/^xwininfo: Window id: \(0x[[:xdigit:]][[:xdigit:]]*\).*/\1/p') # Select window by mouse |
| 47 | elif [ "$1" = "focused" ]; then |
| 48 | # Ensure we are tracking focus |
| 49 | window=$(${picom_dbus}find_win string:focused | $SED -n 's/^[[:space:]]*'${type_win}'[[:space:]]*\([[:digit:]]*\).*/\1/p') # Query picom for the active window |
| 50 | elif echo "$1" | grep -Eiq '^([[:digit:]][[:digit:]]*|0x[[:xdigit:]][[:xdigit:]]*)$'; then |
| 51 | window="$1" # Accept user-specified window-id if the format is correct |
| 52 | else |
| 53 | echo "$0" "[ selected | focused | window-id ]" |
| 54 | fi |
| 55 | |
| 56 | # Color invert the selected or focused window |
| 57 | if [ -n "$window" ]; then |
| 58 | invert_status="$(${picom_dbus}win_get "${type_win}:${window}" string:invert_color | $SED -n 's/^[[:space:]]*boolean[[:space:]]*\([[:alpha:]]*\).*/\1/p')" |
| 59 | if [ "$invert_status" = true ]; then |
| 60 | invert=0 # Set the window to have normal color |
| 61 | else |
| 62 | invert=1 # Set the window to have inverted color |
| 63 | fi |
| 64 | ${picom_dbus}win_set "${type_win}:${window}" string:invert_color_force "${type_enum}:${invert}" & |
| 65 | else |
| 66 | stderr "Cannot find $1 window." |
| 67 | exit 1 |
| 68 | fi |
| 69 | exit 0 |