2013-01-29 00:31:16 +08:00
|
|
|
#!/bin/sh
|
2011-12-08 02:35:22 +08:00
|
|
|
|
2012-11-08 22:30:42 +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
|
2012-11-08 22:30:42 +08:00
|
|
|
#
|
2011-12-08 02:35:22 +08:00
|
|
|
|
2012-04-01 12:54:42 +08:00
|
|
|
# Usage:
|
2013-05-05 03:29:10 +08:00
|
|
|
# $ compton-trans [options] [+|-]opacity
|
2012-11-08 22:30:42 +08:00
|
|
|
# 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
|
|
|
|
2012-11-08 22:34:53 +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
|
|
|
|
2013-05-05 02:48:36 +08:00
|
|
|
# Variables
|
2013-05-05 03:29:10 +08:00
|
|
|
active=
|
2013-05-05 02:48:36 +08:00
|
|
|
wprefix=
|
2011-12-08 02:35:22 +08:00
|
|
|
window=
|
|
|
|
opacity=
|
|
|
|
cur=
|
2013-05-05 05:37:52 +08:00
|
|
|
action=
|
2013-05-05 02:48:36 +08:00
|
|
|
treeout=
|
|
|
|
wid=
|
|
|
|
topmost=
|
|
|
|
lineno=
|
2011-12-08 02:35:22 +08:00
|
|
|
|
2013-05-05 03:29:10 +08:00
|
|
|
# This takes into account the fact that getopts stops on
|
|
|
|
# any argument it doesn't recongize or errors on. This
|
|
|
|
# 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
|
|
|
|
LASTIND=1
|
|
|
|
|
|
|
|
# Read options
|
2013-05-05 05:37:52 +08:00
|
|
|
while getopts ':scdgn:w:o:' option "$@"; do
|
2013-05-05 03:29:10 +08:00
|
|
|
case "$option" in
|
|
|
|
s) wprefix=''; window='' ;;
|
|
|
|
c)
|
|
|
|
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \
|
|
|
|
| sed 's/^.*\(0x\S*\).*$/\1/')
|
|
|
|
wprefix='-id'; window=$active
|
|
|
|
;;
|
2013-05-05 05:37:52 +08:00
|
|
|
d) action='delete' ;;
|
|
|
|
g) action='get' ;;
|
2013-05-05 03:29:10 +08:00
|
|
|
n) wprefix='-name'; window=$OPTARG ;;
|
|
|
|
w) wprefix='-id'; window=$OPTARG ;;
|
|
|
|
o) opacity=$OPTARG ;;
|
|
|
|
\?)
|
|
|
|
# For some reason single char options `-x` stop on the OPTIND after the
|
|
|
|
# argument index, whereas two or more character options (`-foo`) stop
|
|
|
|
# on the argument's index, so we use LASTIND here instead. If OPTIND is
|
|
|
|
# not equal to LASTIND, this means -x was used instead of -xx.
|
|
|
|
OPTIND=$LASTIND
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
LASTIND=$OPTIND
|
|
|
|
done
|
|
|
|
|
|
|
|
# Read positional arguments
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
test -n "$1" && opacity=$1 && shift
|
2011-12-08 02:35:22 +08:00
|
|
|
done
|
|
|
|
|
2013-05-05 02:34:34 +08:00
|
|
|
# clean up opacity. xargs == a poor man's trim.
|
|
|
|
opacity=$(echo "$opacity" | sed 's/%//g' | xargs)
|
2012-11-08 22:34:53 +08:00
|
|
|
|
2013-05-05 02:48:36 +08:00
|
|
|
# Validate opacity value
|
2013-05-05 05:37:52 +08:00
|
|
|
if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then
|
2013-05-05 03:29:10 +08:00
|
|
|
echo "Invalid opacity specified: $opacity."
|
2012-11-14 18:42:09 +08:00
|
|
|
exit 1
|
2012-11-08 22:34:53 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Get ID of the target window
|
2013-05-05 02:18:04 +08:00
|
|
|
if test -z "$wprefix"; then
|
2012-11-14 18:42:09 +08:00
|
|
|
treeout=$(xwininfo -children -frame)
|
2012-11-09 00:03:22 +08:00
|
|
|
else
|
2013-05-05 02:18:04 +08:00
|
|
|
treeout=$(xwininfo -children $wprefix "$window")
|
2012-11-09 00:03:22 +08:00
|
|
|
fi
|
2012-11-08 22:34:53 +08:00
|
|
|
|
2012-11-11 13:02:14 +08:00
|
|
|
wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p')
|
|
|
|
|
2013-05-05 02:18:04 +08:00
|
|
|
if test -z "$wid"; then
|
2013-05-05 02:34:34 +08:00
|
|
|
echo 'Failed to find window.'
|
2012-11-14 18:42:09 +08:00
|
|
|
exit 1
|
2012-11-08 22:34:53 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Make sure it's not root window
|
2013-05-05 02:48:36 +08:00
|
|
|
if echo "$treeout" | fgrep -q 'Parent window id: 0x0'; then
|
2013-05-05 02:34:34 +08:00
|
|
|
echo 'Cannot set opacity on root window.'
|
2012-11-14 18:42:09 +08:00
|
|
|
exit 1
|
2012-11-08 22:34:53 +08:00
|
|
|
fi
|
|
|
|
|
2012-11-11 13:02:14 +08:00
|
|
|
# If it's already the topmost window
|
2013-05-05 02:48:36 +08:00
|
|
|
if echo "$treeout" | grep -q 'Parent window id: 0x[[:xdigit:]]* (the root window)'; then
|
2013-05-05 02:18:04 +08:00
|
|
|
topmost=$wid
|
2012-11-11 13:02:14 +08:00
|
|
|
else
|
2012-11-14 18:42:09 +08:00
|
|
|
# Get the whole window tree
|
2013-05-05 02:18:04 +08:00
|
|
|
treeout=$(xwininfo -root -tree)
|
2012-11-08 22:34:53 +08:00
|
|
|
|
2013-05-05 02:18:04 +08:00
|
|
|
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
|
2012-11-08 22:34:53 +08:00
|
|
|
|
2012-11-14 18:42:09 +08:00
|
|
|
# Find the line number of the target window in the window tree
|
2013-05-05 02:18:04 +08:00
|
|
|
lineno=$(echo -n "$treeout" | grep -nw "$wid" | head -n1 | cut -d ':' -f 1)
|
2011-12-08 02:35:22 +08:00
|
|
|
|
2013-05-05 02:18:04 +08:00
|
|
|
if test -z "$lineno"; then
|
2013-05-05 02:34:34 +08:00
|
|
|
echo 'Failed to find window in window tree.'
|
2012-11-14 18:42:09 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2011-12-08 02:35:22 +08:00
|
|
|
|
2012-11-14 18:42:09 +08:00
|
|
|
# Find the highest ancestor of the target window below
|
2013-05-05 02:18:04 +08:00
|
|
|
topmost=$(echo -n "$treeout" \
|
2013-05-05 05:54:18 +08:00
|
|
|
| head -n $((lineno + 1)) \
|
2013-05-05 02:34:34 +08:00
|
|
|
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
|
|
|
|
| tail -n 1)
|
2012-11-11 13:02:14 +08:00
|
|
|
fi
|
2012-11-08 22:34:53 +08:00
|
|
|
|
2013-05-05 02:18:04 +08:00
|
|
|
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.'
|
2012-11-08 22:30:42 +08:00
|
|
|
exit 1
|
2012-11-08 22:34:53 +08:00
|
|
|
fi
|
|
|
|
|
2013-05-05 02:48:36 +08:00
|
|
|
# Remove the opacity property.
|
2013-05-05 05:37:52 +08:00
|
|
|
if test x"$action" = x'delete'; then
|
2013-05-05 02:48:36 +08:00
|
|
|
xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2013-05-05 05:37:52 +08:00
|
|
|
# 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
|
|
|
|
|
2012-11-08 22:34:53 +08:00
|
|
|
# Calculate the desired opacity
|
2013-05-05 02:48:36 +08:00
|
|
|
if echo "$opacity" | grep -q '^[+-]'; then
|
2013-05-05 02:18:04 +08:00
|
|
|
opacity=$(($cur + $opacity))
|
2011-12-08 02:35:22 +08:00
|
|
|
fi
|
2012-11-08 22:34:53 +08:00
|
|
|
|
2013-05-05 02:18:04 +08:00
|
|
|
test $opacity -lt 0 && opacity=0
|
|
|
|
test $opacity -gt 100 && opacity=100
|
2012-11-08 22:34:53 +08:00
|
|
|
|
|
|
|
# Set opacity
|
2013-05-05 02:18:04 +08:00
|
|
|
opacity=$((opacity * 0xffffffff / 100))
|
2012-11-08 22:34:53 +08:00
|
|
|
xprop -id "$topmost" -f _NET_WM_WINDOW_OPACITY 32c \
|
2012-11-14 18:42:09 +08:00
|
|
|
-set _NET_WM_WINDOW_OPACITY "$opacity"
|