| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# |
| 4 |
# compton-trans |
| 5 |
# transset in a bash script |
| 6 |
# Copyright (c) 2011-2012, Christopher Jeffrey |
| 7 |
# |
| 8 |
|
| 9 |
# Usage: |
| 10 |
# $ compton-trans [options] [+|-]opacity |
| 11 |
# By window id |
| 12 |
# $ compton-trans -w "$WINDOWID" 75 |
| 13 |
# By name |
| 14 |
# $ compton-trans -n "urxvt" 75 |
| 15 |
# By current window |
| 16 |
# $ compton-trans -c 75 |
| 17 |
# By selection |
| 18 |
# $ compton-trans 75 |
| 19 |
# $ compton-trans -s 75 |
| 20 |
# Increment current window 5% |
| 21 |
# $ compton-trans -c +5 |
| 22 |
# Delete current window's opacity |
| 23 |
# $ compton-trans -c --delete |
| 24 |
# Reset all windows |
| 25 |
# $ compton-trans --reset |
| 26 |
|
| 27 |
# "command" is a shell built-in, faster than "which" |
| 28 |
if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then |
| 29 |
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 |
| 30 |
exit 1 |
| 31 |
fi |
| 32 |
|
| 33 |
# Variables |
| 34 |
active= |
| 35 |
wprefix= |
| 36 |
window= |
| 37 |
opacity= |
| 38 |
cur= |
| 39 |
action= |
| 40 |
treeout= |
| 41 |
wid= |
| 42 |
topmost= |
| 43 |
lineno= |
| 44 |
option= |
| 45 |
v= |
| 46 |
|
| 47 |
# Workaround: replace '-5' with '~5' so as not to confuse getopts. |
| 48 |
for v in "$@"; do |
| 49 |
shift && set -- "$@" "$(echo "$v" | sed 's/^-\([0-9]\+%\?\)$/~\1/')" |
| 50 |
done |
| 51 |
|
| 52 |
# This takes into account the fact that getopts stops on |
| 53 |
# any argument it doesn't recongize or errors on. This |
| 54 |
# allows for things like `compton-trans -5` as well |
| 55 |
# as `compton-trans -c +5 -s` (contrived example). |
| 56 |
while test $# -gt 0; do |
| 57 |
# Reset option index |
| 58 |
OPTIND=1 |
| 59 |
|
| 60 |
# Read options |
| 61 |
while getopts 'scrdgn:w:o:-:' option "$@"; do |
| 62 |
if test "$option" = '-'; then |
| 63 |
case "$OPTARG" in |
| 64 |
select | current | reset | delete | get) |
| 65 |
v='' |
| 66 |
;; |
| 67 |
name | window | opacity) |
| 68 |
eval v=\$$OPTIND |
| 69 |
OPTIND=$((OPTIND + 1)) |
| 70 |
;; |
| 71 |
name=* | window=* | opacity=*) |
| 72 |
v=$(echo "$OPTARG" | sed 's/^[^=]\+=//') |
| 73 |
;; |
| 74 |
*) |
| 75 |
echo "$0: illegal option $OPTARG" >& 2 |
| 76 |
exit 1 |
| 77 |
;; |
| 78 |
esac |
| 79 |
option=$(echo "$OPTARG" | cut -c 1) |
| 80 |
OPTARG=$v |
| 81 |
fi |
| 82 |
case "$option" in |
| 83 |
s) wprefix=''; window='' ;; |
| 84 |
c) |
| 85 |
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ |
| 86 |
| grep -Eo '0x[[:xdigit:]]+' | head -n 1) |
| 87 |
wprefix='-id'; window=$active |
| 88 |
;; |
| 89 |
r) action='reset' ;; |
| 90 |
d) action='delete' ;; |
| 91 |
g) action='get' ;; |
| 92 |
n) wprefix='-name'; window=$OPTARG ;; |
| 93 |
w) wprefix='-id'; window=$OPTARG ;; |
| 94 |
o) opacity=$OPTARG ;; |
| 95 |
\?) exit 1 ;; |
| 96 |
esac |
| 97 |
done |
| 98 |
|
| 99 |
# Read positional arguments |
| 100 |
shift $((OPTIND - 1)) |
| 101 |
test -n "$1" && opacity=$1 && shift |
| 102 |
done |
| 103 |
|
| 104 |
# clean up opacity. xargs == a poor man's trim. |
| 105 |
opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/') |
| 106 |
|
| 107 |
# Validate opacity value |
| 108 |
if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then |
| 109 |
echo "Invalid opacity specified: $opacity." |
| 110 |
exit 1 |
| 111 |
fi |
| 112 |
|
| 113 |
# Reset opacity for all windows |
| 114 |
if test x"$action" = x'reset'; then |
| 115 |
xwininfo -root -tree \ |
| 116 |
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \ |
| 117 |
| while IFS=$(printf '\n') read wid; do |
| 118 |
xprop -id "$wid" -remove _NET_WM_WINDOW_OPACITY |
| 119 |
done |
| 120 |
exit 0 |
| 121 |
fi |
| 122 |
|
| 123 |
# Get ID of the target window |
| 124 |
if test -z "$wprefix"; then |
| 125 |
treeout=$(xwininfo -children -frame) |
| 126 |
else |
| 127 |
test "$wprefix" = '-id' \ |
| 128 |
&& ! echo "$window" | grep -Eiq '^[[:space:]]*(0x[[:xdigit:]]+|[[:digit:]]+)[[:space:]]*$' \ |
| 129 |
&& echo 'Bad window ID.' && exit 1 |
| 130 |
treeout=$(xwininfo -children $wprefix "$window") |
| 131 |
fi |
| 132 |
|
| 133 |
wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p') |
| 134 |
|
| 135 |
if test -z "$wid"; then |
| 136 |
echo 'Failed to find window.' |
| 137 |
exit 1 |
| 138 |
fi |
| 139 |
|
| 140 |
# Make sure it's not root window |
| 141 |
if echo "$treeout" | fgrep -q 'Parent window id: 0x0'; then |
| 142 |
echo 'Cannot set opacity on root window.' |
| 143 |
exit 1 |
| 144 |
fi |
| 145 |
|
| 146 |
# If it's already the topmost window |
| 147 |
if echo "$treeout" | grep -q 'Parent window id: 0x[[:xdigit:]]* (the root window)'; then |
| 148 |
topmost=$wid |
| 149 |
else |
| 150 |
# Get the whole window tree |
| 151 |
treeout=$(xwininfo -root -tree) |
| 152 |
|
| 153 |
if test -z "$treeout"; then |
| 154 |
echo 'Failed to get root window tree.' |
| 155 |
exit 1 |
| 156 |
fi |
| 157 |
|
| 158 |
# Find the line number of the target window in the window tree |
| 159 |
lineno=$(echo -n "$treeout" | grep -nw "$wid" | head -n1 | cut -d ':' -f 1) |
| 160 |
|
| 161 |
if test -z "$lineno"; then |
| 162 |
echo 'Failed to find window in window tree.' |
| 163 |
exit 1 |
| 164 |
fi |
| 165 |
|
| 166 |
# Find the highest ancestor of the target window below |
| 167 |
topmost=$(echo -n "$treeout" \ |
| 168 |
| head -n $((lineno + 1)) \ |
| 169 |
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \ |
| 170 |
| tail -n 1) |
| 171 |
fi |
| 172 |
|
| 173 |
if test -z "$topmost"; then |
| 174 |
echo 'Failed to find the highest parent window below root of the' \ |
| 175 |
'selected window.' |
| 176 |
exit 1 |
| 177 |
fi |
| 178 |
|
| 179 |
# Remove the opacity property. |
| 180 |
if test x"$action" = x'delete'; then |
| 181 |
xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY |
| 182 |
exit 0 |
| 183 |
fi |
| 184 |
|
| 185 |
# Get current opacity. |
| 186 |
cur=$(xprop -id "$topmost" -notype _NET_WM_WINDOW_OPACITY \ |
| 187 |
| sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/') |
| 188 |
test -z "$cur" && cur=0xffffffff |
| 189 |
cur=$((cur * 100 / 0xffffffff)) |
| 190 |
|
| 191 |
# Output current opacity. |
| 192 |
if test x"$action" = x'get'; then |
| 193 |
echo "$cur" |
| 194 |
exit 0 |
| 195 |
fi |
| 196 |
|
| 197 |
# Calculate the desired opacity |
| 198 |
if echo "$opacity" | grep -q '^[+-]'; then |
| 199 |
opacity=$((cur + opacity)) |
| 200 |
fi |
| 201 |
|
| 202 |
test $opacity -lt 0 && opacity=0 |
| 203 |
test $opacity -gt 100 && opacity=100 |
| 204 |
|
| 205 |
# Set opacity |
| 206 |
opacity=$((opacity * 0xffffffff / 100)) |
| 207 |
xprop -id "$topmost" -f _NET_WM_WINDOW_OPACITY 32c \ |
| 208 |
-set _NET_WM_WINDOW_OPACITY "$opacity" |