picom/bin/compton-trans

169 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
#
# compton-trans
# transset in a bash script
# Copyright (c) 2011-2012, Christopher Jeffrey
#
# Usage:
# $ compton-trans [options] [+|-]opacity
# By window id
# $ compton-trans -w "$WINDOWID" 75
# By name
# $ compton-trans -n "urxvt" 75
# By current window
# $ compton-trans -c 75
# By selection
# $ compton-trans 75
# $ compton-trans -s 75
# Increment current window 5%
# $ compton-trans -c +5
# "command" is a shell built-in, faster than "which"
if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then
echo 'Please install x11-utils/xorg-xprop/xorg-xwininfo.' >& 2
exit 1
fi
# Variables
args=
arg=
active=
wprefix=
window=
opacity=
cur=
action=
treeout=
wid=
topmost=
lineno=
# Workaround: replace '-5' with '~5' so as not to confuse getopts.
for v in "$@"; do
shift && set -- "$@" "$(echo "$v" | sed 's/^-\([0-9]\+%\?\)$/~\1/')"
done
# Use getopt to parse arguments.
args=$(getopt -o 'scdgn:w:o:' -l 'select,current,delete,get,name:,window:,opacity:' -- "$@")
if test $? -ne 0; then
echo 'Bad arguments.'
exit 1
fi
eval set -- "$args"
while test $# -gt 0; do
arg=$1
shift
case "$arg" in
--) break ;;
-s | --select) wprefix=''; window='' ;;
-c | --current)
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \
| sed 's/^.*\(0x\S*\).*$/\1/')
wprefix='-id'; window=$active
;;
-d | --delete) action='delete' ;;
-g | --get) action='get' ;;
-n | --name) wprefix='-name'; window=$1; shift ;;
-w | --window) wprefix='-id'; window=$1; shift ;;
-o | --opacity) opacity=$1; shift ;;
esac
done
test -n "$1" && opacity=$1
# clean up opacity. xargs == a poor man's trim.
opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/')
# Validate opacity value
if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then
echo "Invalid opacity specified: $opacity."
exit 1
fi
# Get ID of the target window
if test -z "$wprefix"; then
treeout=$(xwininfo -children -frame)
else
treeout=$(xwininfo -children $wprefix "$window")
fi
wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p')
if test -z "$wid"; then
echo 'Failed to find window.'
exit 1
fi
# Make sure it's not root window
if echo "$treeout" | fgrep -q 'Parent window id: 0x0'; then
echo 'Cannot set opacity on root window.'
exit 1
fi
# If it's already the topmost window
if echo "$treeout" | grep -q 'Parent window id: 0x[[:xdigit:]]* (the root window)'; then
topmost=$wid
else
# Get the whole window tree
treeout=$(xwininfo -root -tree)
if test -z "$treeout"; then
echo 'Failed to get root window tree.'
exit 1
fi
# Find the line number of the target window in the window tree
lineno=$(echo -n "$treeout" | grep -nw "$wid" | head -n1 | cut -d ':' -f 1)
if test -z "$lineno"; then
echo 'Failed to find window in window tree.'
exit 1
fi
# Find the highest ancestor of the target window below
topmost=$(echo -n "$treeout" \
| head -n $((lineno + 1)) \
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
| tail -n 1)
fi
if test -z "$topmost"; then
echo 'Failed to find the highest parent window below root of the' \
'selected window.'
exit 1
fi
# Remove the opacity property.
if test x"$action" = x'delete'; then
xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY
exit 0
fi
# Get current opacity.
cur=$(xprop -id "$topmost" -notype _NET_WM_WINDOW_OPACITY \
| sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/')
test -z "$cur" && cur=0xffffffff
cur=$((cur * 100 / 0xffffffff))
# Output current opacity.
if test x"$action" = x'get'; then
echo "$cur"
exit 0
fi
# Calculate the desired opacity
if echo "$opacity" | grep -q '^[+-]'; then
opacity=$(($cur + $opacity))
fi
test $opacity -lt 0 && opacity=0
test $opacity -gt 100 && opacity=100
# Set opacity
opacity=$((opacity * 0xffffffff / 100))
xprop -id "$topmost" -f _NET_WM_WINDOW_OPACITY 32c \
-set _NET_WM_WINDOW_OPACITY "$opacity"