2014-01-04 00:08:27 +08:00
#!/bin/sh
2020-03-31 19:52:13 +08:00
# == Declare stderr function ===
stderr( ) {
printf "\033[1;31m%s\n\033[0m" " $@ " >& 2
}
2020-03-31 20:02:28 +08:00
# === Verify `picom --dbus` status ===
2014-01-06 21:50:10 +08:00
2020-03-31 19:52:13 +08:00
if [ -z " $( dbus-send --session --dest= org.freedesktop.DBus --type= method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep compton) " ] ; then
2020-03-31 19:57:05 +08:00
stderr "picom DBus interface unavailable"
2020-03-31 19:52:13 +08:00
if [ -n " $( pgrep picom) " ] ; then
stderr "picom running without dbus interface"
#killall picom & # Causes all windows to flicker away and come back ugly.
2020-03-31 20:02:28 +08:00
#picom --dbus & # Causes all windows to flicker away and come back beautiful
2014-01-06 21:50:10 +08:00
else
2020-03-31 19:52:13 +08:00
stderr "picom not running"
2014-01-06 21:50:10 +08:00
fi
2020-03-31 19:52:13 +08:00
exit 1
2014-01-06 21:50:10 +08:00
fi
# === Setup sed ===
2014-01-04 00:08:27 +08:00
2020-03-31 19:52:13 +08:00
SED = " ${ SED :- $( command -v gsed || printf 'sed' ) } "
2014-01-04 00:08:27 +08:00
# === Get connection parameters ===
2020-03-31 19:52:13 +08:00
dpy = $( printf " $DISPLAY " | tr -c '[:alnum:]' _)
2014-01-04 00:08:27 +08:00
if [ -z " $dpy " ] ; then
2020-04-01 12:50:03 +08:00
stderr "Cannot find display."
2020-03-31 19:52:13 +08:00
exit 1
2014-01-04 00:08:27 +08:00
fi
service = " com.github.chjj.compton. ${ dpy } "
interface = "com.github.chjj.compton"
2020-03-31 19:52:13 +08:00
picom_dbus = "dbus-send --print-reply --dest=" ${ service } " / " ${ interface } "."
2014-01-04 00:08:27 +08:00
type_win = 'uint32'
2019-08-10 06:40:20 +08:00
type_enum = 'uint32'
2014-01-04 00:08:27 +08:00
# === Color Inversion ===
2014-01-06 21:50:10 +08:00
# Get window ID of window to invert
2014-01-08 21:42:27 +08:00
if [ -z " $1 " -o " $1 " = "selected" ] ; then
2014-01-06 21:50:10 +08:00
window = $( xwininfo -frame | sed -n 's/^xwininfo: Window id: \(0x[[:xdigit:]][[:xdigit:]]*\).*/\1/p' ) # Select window by mouse
elif [ " $1 " = "focused" ] ; then
# Ensure we are tracking focus
2020-03-31 20:02:28 +08:00
window = $( ${ picom_dbus } find_win string:focused | $SED -n 's/^[[:space:]]*' ${ type_win } '[[:space:]]*\([[:digit:]]*\).*/\1/p' ) # Query picom for the active window
2014-01-08 21:42:27 +08:00
elif echo " $1 " | grep -Eiq '^([[:digit:]][[:digit:]]*|0x[[:xdigit:]][[:xdigit:]]*)$' ; then
window = " $1 " # Accept user-specified window-id if the format is correct
2014-01-06 21:50:10 +08:00
else
echo " $0 " "[ selected | focused | window-id ]"
fi
# Color invert the selected or focused window
if [ -n " $window " ] ; then
2020-03-31 19:52:13 +08:00
invert_status = " $( ${ picom_dbus } win_get " ${ type_win } : ${ window } " string:invert_color | $SED -n 's/^[[:space:]]*boolean[[:space:]]*\([[:alpha:]]*\).*/\1/p' ) "
2014-01-08 21:42:27 +08:00
if [ " $invert_status " = true ] ; then
2014-01-06 21:50:10 +08:00
invert = 0 # Set the window to have normal color
2014-01-08 21:42:27 +08:00
else
invert = 1 # Set the window to have inverted color
2014-01-06 21:50:10 +08:00
fi
2020-03-31 19:52:13 +08:00
${ picom_dbus } win_set " ${ type_win } : ${ window } " string:invert_color_force " ${ type_enum } : ${ invert } " &
2014-01-04 00:08:27 +08:00
else
2020-03-31 19:52:13 +08:00
stderr " Cannot find $1 window. "
exit 1
2014-01-04 00:08:27 +08:00
fi
2020-03-31 19:52:13 +08:00
exit 0