1
0
Fork 0
mirror of https://github.com/yshui/picom.git synced 2024-11-18 13:55:36 -05:00
picom/bin/compton-trans

209 lines
5.1 KiB
Text
Raw Normal View History

#!/bin/sh
2011-12-07 13:35:22 -05:00
#
# compton-trans
2011-12-07 13:35:22 -05:00
# transset in a bash script
2012-04-01 00:54:42 -04:00
# Copyright (c) 2011-2012, Christopher Jeffrey
#
2011-12-07 13:35:22 -05:00
2012-04-01 00:54:42 -04:00
# Usage:
2013-05-04 15:29:10 -04:00
# $ 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
# Delete current window's opacity
# $ compton-trans -c --delete
# Reset all windows
# $ compton-trans --reset
2011-12-07 13:35:22 -05:00
# "command" is a shell built-in, faster than "which"
if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then
echo 'The command xwininfo or xprop is not available. They might reside in a package named xwininfo, xprop, x11-utils, xorg-xprop, or xorg-xwininfo.' >& 2
2011-12-21 14:28:55 -05:00
exit 1
fi
2011-12-07 13:35:22 -05:00
# Variables
2013-05-04 15:29:10 -04:00
active=
wprefix=
2011-12-07 13:35:22 -05:00
window=
opacity=
cur=
2013-05-04 17:37:52 -04:00
action=
treeout=
wid=
topmost=
lineno=
2013-05-04 21:40:01 -04:00
option=
v=
2011-12-07 13:35:22 -05:00
# 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
# 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
2013-05-04 15:29:10 -04:00
# Read options
2013-05-04 21:40:01 -04:00
while getopts 'scrdgn:w:o:-:' option "$@"; do
if test "$option" = '-'; then
case "$OPTARG" in
2013-05-04 21:40:01 -04:00
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 \
| grep -Eo '0x[[:xdigit:]]+' | head -n 1)
wprefix='-id'; window=$active
;;
2013-05-04 21:40:01 -04:00
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
2013-05-04 15:29:10 -04:00
# Read positional arguments
shift $((OPTIND - 1))
test -n "$1" && opacity=$1 && shift
2011-12-07 13:35:22 -05:00
done
2013-05-04 14:34:34 -04:00
# clean up opacity. xargs == a poor man's trim.
opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/')
# Validate opacity value
2013-05-04 17:37:52 -04:00
if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then
2013-05-04 15:29:10 -04:00
echo "Invalid opacity specified: $opacity."
exit 1
fi
2013-05-04 21:40:01 -04:00
# Reset opacity for all windows
if test x"$action" = x'reset'; then
xwininfo -root -tree \
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
| while IFS=$'\n' read wid; do
xprop -id "$wid" -remove _NET_WM_WINDOW_OPACITY
done
exit 0
fi
# Get ID of the target window
if test -z "$wprefix"; then
treeout=$(xwininfo -children -frame)
else
test "$wprefix" = '-id' \
&& ! echo "$window" | grep -Eiq '^[[:space:]]*(0x[[:xdigit:]]+|[[:digit:]]+)[[:space:]]*$' \
&& echo 'Bad window ID.' && exit 1
treeout=$(xwininfo -children $wprefix "$window")
fi
wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p')
if test -z "$wid"; then
2013-05-04 14:34:34 -04:00
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
2013-05-04 14:34:34 -04:00
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
2013-05-04 14:34:34 -04:00
echo 'Failed to get root window tree.'
2012-04-01 00:54:42 -04: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-07 13:35:22 -05:00
if test -z "$lineno"; then
2013-05-04 14:34:34 -04:00
echo 'Failed to find window in window tree.'
exit 1
fi
2011-12-07 13:35:22 -05:00
# Find the highest ancestor of the target window below
topmost=$(echo -n "$treeout" \
2013-05-04 17:54:18 -04:00
| head -n $((lineno + 1)) \
2013-05-04 14:34:34 -04:00
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
| tail -n 1)
fi
if test -z "$topmost"; then
2013-05-04 14:34:34 -04:00
echo 'Failed to find the highest parent window below root of the' \
'selected window.'
exit 1
fi
# Remove the opacity property.
2013-05-04 17:37:52 -04:00
if test x"$action" = x'delete'; then
xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY
exit 0
fi
2013-05-04 17:37:52 -04: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
# Calculate the desired opacity
if echo "$opacity" | grep -q '^[+-]'; then
opacity=$((cur + opacity))
2011-12-07 13:35:22 -05: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"