picom/bin/compton-trans

137 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2011-12-08 02:35:22 +08:00
#
# compton-trans
2011-12-08 02:35:22 +08:00
# transset in a bash script
2012-04-01 12:54:42 +08:00
# Copyright (c) 2011-2012, Christopher Jeffrey
#
2011-12-08 02:35:22 +08:00
2012-04-01 12:54:42 +08:00
# Usage:
# 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
2011-12-08 02:35:22 +08:00
# "command" is a shell built-in, faster than "which"
if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then
2013-05-05 02:34:34 +08:00
echo 'Please install x11-utils/xorg-xprop/xorg-xwininfo.' >& 2
2011-12-22 03:28:55 +08:00
exit 1
fi
2011-12-08 02:35:22 +08:00
window=
opacity=
cur=
2012-05-28 08:21:14 +08:00
i=
2011-12-08 02:35:22 +08:00
# Read options
2013-05-05 02:34:34 +08:00
while getopts 'scn:w:o:' option; do
2012-02-06 16:16:49 +08:00
case "$option" in
s) wprefix=''; window='' ;;
2011-12-08 02:35:22 +08:00
c)
2013-05-05 02:34:34 +08:00
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \
2011-12-08 02:35:22 +08:00
| sed 's/^.*\(0x\S*\).*$/\1/')
wprefix='-id'; window=$active
2011-12-08 02:35:22 +08:00
;;
n) wprefix='-name'; window=$OPTARG ;;
w) wprefix='-id'; window=$OPTARG ;;
o) opacity=$OPTARG ;;
\?) exit 1;;
2011-12-08 02:35:22 +08:00
esac
done
# Read positional arguments
shift $(($OPTIND - 1))
test -n "$1" && opacity=$1
# Validate opacity value
if test -z "$opacity"; then
2013-05-05 02:34:34 +08:00
echo 'No opacity specified.'
exit 1
fi
2013-05-05 02:34:34 +08:00
# clean up opacity. xargs == a poor man's trim.
opacity=$(echo "$opacity" | sed 's/%//g' | xargs)
#opacity=$(echo "$opacity" | sed 's/%//g' | sed 's/^ \+\| \+$//g')
if test -z "$opacity"; then
2013-05-05 02:34:34 +08:00
echo 'Invalid opacity value.'
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
2013-05-05 02:34:34 +08:00
echo 'Failed to find window.'
exit 1
fi
# Make sure it's not root window
2013-05-05 02:34:34 +08:00
if echo "$treeout" | fgrep 'Parent window id: 0x0' > /dev/null; then
echo 'Cannot set opacity on root window.'
exit 1
fi
# If it's already the topmost window
2013-05-05 02:34:34 +08:00
if echo "$treeout" | grep 'Parent window id: 0x[[:xdigit:]]* (the root window)' > /dev/null; then
topmost=$wid
else
# Get the whole window tree
treeout=$(xwininfo -root -tree)
if test -z "$treeout"; then
2013-05-05 02:34:34 +08:00
echo 'Failed to get root window tree.'
2012-04-01 12:54:42 +08:00
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)
2011-12-08 02:35:22 +08:00
if test -z "$lineno"; then
2013-05-05 02:34:34 +08:00
echo 'Failed to find window in window tree.'
exit 1
fi
2011-12-08 02:35:22 +08:00
# Find the highest ancestor of the target window below
topmost=$(echo -n "$treeout" \
| head -n $(($lineno + 1)) \
2013-05-05 02:34:34 +08:00
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
| tail -n 1)
fi
if test -z "$topmost"; then
2013-05-05 02:34:34 +08:00
echo 'Failed to find the highest parent window below root of the' \
'selected window.'
exit 1
fi
# Calculate the desired opacity
if echo "$opacity" | grep '^[+-]' > /dev/null; then
2013-05-05 02:34:34 +08:00
cur=$(xprop -id "$topmost" -notype _NET_WM_WINDOW_OPACITY \
2011-12-22 03:28:55 +08:00
| sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/')
test -z "$cur" && cur=0xffffffff
cur=$((cur * 100 / 0xffffffff))
opacity=$(($cur + $opacity))
2011-12-08 02:35:22 +08:00
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"