Merge pull request #108 from chjj/compton_trans

compton-trans improvements
This commit is contained in:
Richard Grenville 2013-05-10 16:58:20 -07:00
commit 1cb6058732
1 changed files with 131 additions and 57 deletions

View File

@ -7,6 +7,7 @@
# #
# Usage: # Usage:
# $ compton-trans [options] [+|-]opacity
# By window id # By window id
# $ compton-trans -w "$WINDOWID" 75 # $ compton-trans -w "$WINDOWID" 75
# By name # By name
@ -18,117 +19,190 @@
# $ compton-trans -s 75 # $ compton-trans -s 75
# Increment current window 5% # Increment current window 5%
# $ compton-trans -c +5 # $ compton-trans -c +5
# Delete current window's opacity
# $ compton-trans -c --delete
# Reset all windows
# $ compton-trans --reset
# "command" is a shell built-in, faster than "which" # "command" is a shell built-in, faster than "which"
if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then
echo "Please install x11-utils/xorg-xprop/xorg-xwininfo." >& 2 echo 'Please install x11-utils/xorg-xprop/xorg-xwininfo.' >& 2
exit 1 exit 1
fi fi
# Variables
active=
wprefix=
window= window=
opacity= opacity=
cur= cur=
i= action=
treeout=
wid=
topmost=
lineno=
option=
v=
# Read options # Workaround: replace '-5' with '~5' so as not to confuse getopts.
while getopts "scn:w:o:" option; do for v in "$@"; do
case "$option" in shift && set -- "$@" "$(echo "$v" | sed 's/^-\([0-9]\+%\?\)$/~\1/')"
s) wprefix=''; window='' ;;
c)
active=$(xprop -root -notype "_NET_ACTIVE_WINDOW" \
| sed 's/^.*\(0x\S*\).*$/\1/')
wprefix='-id '; window="$active"
;;
n) wprefix='-name '; window="$OPTARG" ;;
w) wprefix='-id '; window="$OPTARG" ;;
o) opacity="$OPTARG" ;;
\?) exit 1;;
esac
done done
# Read positional arguments # This takes into account the fact that getopts stops on
shift $(($OPTIND - 1)) # any argument it doesn't recongize or errors on. This
[ -n "$1" ] && opacity="$1" # allows for things like `compton-trans -5` as well
# as `compton-trans -c +5 -s` (contrived example).
while test $# -gt 0; do
# Reset option index
OPTIND=1
# Read options
while getopts 'scrdgn:w:o:-:' option "$@"; do
if test "$option" = '-'; then
case "$OPTARG" in
select | current | reset | delete | get)
v=''
;;
name | window | opacity)
eval v=\$$OPTIND
OPTIND=$((OPTIND + 1))
;;
name=* | window=* | opacity=*)
v=$(echo "$OPTARG" | sed 's/^[^=]\+=//')
;;
*)
echo "$0: illegal option $OPTARG" >& 2
exit 1
;;
esac
option=$(echo "$OPTARG" | cut -c 1)
OPTARG=$v
fi
case "$option" in
s) wprefix=''; window='' ;;
c)
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \
| sed 's/^.*\(0x\S*\).*$/\1/')
wprefix='-id'; window=$active
;;
r) action='reset' ;;
d) action='delete' ;;
g) action='get' ;;
n) wprefix='-name'; window=$OPTARG ;;
w) wprefix='-id'; window=$OPTARG ;;
o) opacity=$OPTARG ;;
\?) exit 1 ;;
esac
done
# Read positional arguments
shift $((OPTIND - 1))
test -n "$1" && opacity=$1 && shift
done
# clean up opacity. xargs == a poor man's trim.
opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/')
# Validate opacity value # Validate opacity value
if [ -z "$opacity" ]; then if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then
echo "No opacity specified." echo "Invalid opacity specified: $opacity."
exit 1 exit 1
fi fi
opacity="$(echo "$opacity" \ # Reset opacity for all windows
| sed -rn 's/^[[:space:]]*([+-]?[[:digit:]]+)[[:space:]]*$/\1/p')" if test x"$action" = x'reset'; then
xwininfo -root -tree \
if [ -z "$opacity" ]; then | sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
echo "Invalid opacity value." | while IFS=$'\n' read wid; do
exit 1 xprop -id "$wid" -remove _NET_WM_WINDOW_OPACITY
done
exit 0
fi fi
# Get ID of the target window # Get ID of the target window
if [ -z "$wprefix" ]; then if test -z "$wprefix"; then
treeout=$(xwininfo -children -frame) treeout=$(xwininfo -children -frame)
else else
treeout=$(xwininfo -children $wprefix"$window") test "$wprefix" = '-id' \
&& ! echo "$window" | grep -q '^\(0x\)\?[0-9a-fA-F]\+$' \
&& echo 'Bad window ID.' && exit 1
treeout=$(xwininfo -children $wprefix "$window")
fi fi
wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p') wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p')
if [ -z "$wid" ]; then if test -z "$wid"; then
echo "Failed to find window." echo 'Failed to find window.'
exit 1 exit 1
fi fi
# Make sure it's not root window # Make sure it's not root window
if echo "$treeout" | fgrep "Parent window id: 0x0" > /dev/null; then if echo "$treeout" | fgrep -q 'Parent window id: 0x0'; then
echo "Cannot set opacity on root window." echo 'Cannot set opacity on root window.'
exit 1 exit 1
fi fi
# If it's already the topmost window # If it's already the topmost window
if echo "$treeout" | grep "Parent window id: 0x[[:xdigit:]]* (the root window)" > /dev/null; then if echo "$treeout" | grep -q 'Parent window id: 0x[[:xdigit:]]* (the root window)'; then
topmost="$wid" topmost=$wid
else else
# Get the whole window tree # Get the whole window tree
treeout="$(xwininfo -root -tree)" treeout=$(xwininfo -root -tree)
if [ -z "$treeout" ]; then if test -z "$treeout"; then
echo "Failed to get root window tree." echo 'Failed to get root window tree.'
exit 1 exit 1
fi fi
# Find the line number of the target window in the window tree # 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)" lineno=$(echo -n "$treeout" | grep -nw "$wid" | head -n1 | cut -d ':' -f 1)
if [ -z "$lineno" ]; then if test -z "$lineno"; then
echo "Failed to find window in window tree." echo 'Failed to find window in window tree.'
exit 1 exit 1
fi fi
# Find the highest ancestor of the target window below # 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 -n1) topmost=$(echo -n "$treeout" \
| head -n $((lineno + 1)) \
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
| tail -n 1)
fi fi
if [ -z "$topmost" ]; then if test -z "$topmost"; then
echo "Failed to find the highest parent window below root of the" \ echo 'Failed to find the highest parent window below root of the' \
"selected window." 'selected window.'
exit 1 exit 1
fi fi
# Calculate the desired opacity # Remove the opacity property.
if echo "$opacity" | grep '^[+-]' > /dev/null; then if test x"$action" = x'delete'; then
sign=$(echo "$opacity" | cut -b1) xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY
cur=$(xprop -id "$topmost" -notype "_NET_WM_WINDOW_OPACITY" \ exit 0
| sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/')
[ -z "$cur" ] && cur=0xffffffff
cur=$((cur * 100 / 0xffffffff))
opacity="$(echo "$opacity" | sed 's/^[+-]//')"
opacity=$(($cur $sign $opacity))
fi fi
[ $opacity -lt 0 ] && opacity=0 # Get current opacity.
[ $opacity -gt 100 ] && opacity=100 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 # Set opacity
opacity=$(($opacity * 0xffffffff / 100)) opacity=$((opacity * 0xffffffff / 100))
xprop -id "$topmost" -f _NET_WM_WINDOW_OPACITY 32c \ xprop -id "$topmost" -f _NET_WM_WINDOW_OPACITY 32c \
-set _NET_WM_WINDOW_OPACITY "$opacity" -set _NET_WM_WINDOW_OPACITY "$opacity"