2013-01-28 11:31:16 -05:00
#!/bin/sh
2011-12-07 13:35:22 -05:00
2012-11-08 09:30:42 -05:00
#
2019-10-23 14:27:30 -04:00
# picom-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
2012-11-08 09:30:42 -05:00
#
2011-12-07 13:35:22 -05:00
2012-04-01 00:54:42 -04:00
# Usage:
2019-10-23 14:27:30 -04:00
# $ picom-trans [options] [+|-]opacity
2012-11-08 09:30:42 -05:00
# By window id
2019-10-23 14:27:30 -04:00
# $ picom-trans -w "$WINDOWID" 75
2012-11-08 09:30:42 -05:00
# By name
2019-10-23 14:27:30 -04:00
# $ picom-trans -n "urxvt" 75
2012-11-08 09:30:42 -05:00
# By current window
2019-10-23 14:27:30 -04:00
# $ picom-trans -c 75
2012-11-08 09:30:42 -05:00
# By selection
2019-10-23 14:27:30 -04:00
# $ picom-trans 75
# $ picom-trans -s 75
2012-11-08 09:30:42 -05:00
# Increment current window 5%
2019-10-23 14:27:30 -04:00
# $ picom-trans -c +5
2013-05-04 21:50:42 -04:00
# Delete current window's opacity
2019-10-23 14:27:30 -04:00
# $ picom-trans -c --delete
2020-10-22 17:06:17 -04:00
# Toggle current window's opacity between 90 and unset
# $ picom-trans -c --toggle 90
2013-05-04 21:50:42 -04:00
# Reset all windows
2019-10-23 14:27:30 -04:00
# $ picom-trans --reset
2020-10-22 16:48:59 -04:00
print_usage() {
echo "Usage: $0 [options] [+|-]opacity"
echo ""
echo "Options:"
echo " -h, --help Print this help message."
echo " -o, --opacity OPACITY Specify the new opacity value in range 1-100 for the window. If"
echo " prefixed with + or -, increment or decrement from the target"
echo " window’ s current opacity."
echo ""
echo "Actions:"
echo " -g, --get Print the target window's opacity."
echo " -d, --delete Delete opacity of the target window."
2020-10-22 17:06:17 -04:00
echo " -t, --toggle Toggle the target window's opacity, i.e. set if not already set"
echo " and delete else."
2020-10-22 16:48:59 -04:00
echo " -r, --reset Reset opacity for all windows."
echo ""
echo "Window Selection:"
echo " -s, --select Select target window with mouse cursor. (DEFAULT)"
echo " -c, --current Select the currently active window as target."
echo " -n, --name WINDOW_NAME Specify and try to match a window name."
echo " -w, --window WINDOW_ID Specify the window id of the target window."
}
2019-10-23 14:27:30 -04:00
case "$0" in
*compton-trans*) echo "Warning: compton has been renamed, please use picom-trans instead" >& 2;;
esac
2011-12-07 13:35:22 -05:00
2012-11-08 09:34:53 -05:00
# "command" is a shell built-in, faster than "which"
if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then
2014-02-25 09:13:43 -05:00
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
2020-10-22 16:48:59 -04:00
if test $# -eq 0; then
print_usage >& 2
exit 1
fi
2013-05-04 14:48:36 -04:00
# Variables
2013-05-04 15:29:10 -04:00
active=
2013-05-04 14:48:36 -04:00
wprefix=
2011-12-07 13:35:22 -05:00
window=
opacity=
cur=
2013-05-04 17:37:52 -04:00
action=
2013-05-04 14:48:36 -04:00
treeout=
wid=
topmost=
lineno=
2013-05-04 21:40:01 -04:00
option=
v=
2011-12-07 13:35:22 -05:00
2013-05-04 18:50:31 -04:00
# Workaround: replace '-5' with '~5' so as not to confuse getopts.
for v in "$@"; do
picom-trans: Use POSIX-compatible sed, grep
\b \? \+ \| are GNU extensions to sed
In BRE (Basic Regular Expressions) there is no \? \+ or \|
In ERE (Extended Regular Expressions) there is ? + and |
To specify sed to use ERE, specify the -E flag.
GNU grep does not distinguish between BRE and ERE, but other
implementations do. To make grep use ERE instead of BRE, specify
the -E flag.
The GNU extension \b has no equivalent in either BRE or ERE.
So, in line number 216, I used the whole initial expected output.
For quick reference (n/a means 'not available') -
GNU BRE | POSIX BRE | POSIX ERE
-------------------------------
\( | \( | (
\) | \) | )
\? | \{0,1\} | ? or {0,1}
\+ | \{1,\} | + or {1,}
\| | n/a | |
\b | n/a | n/a
2021-05-30 13:49:02 -04:00
shift && set -- "$@" "$(echo "$v" | sed -E 's/^-([0-9]+%?)$/~\1/')"
2013-05-04 18:50:31 -04:00
done
2021-05-31 02:57:18 -04:00
# We make getopts stop on any argument it doesn't recognize
# or errors on. This allows for things like `picom-trans -5`
# as well as `picom-trans -c +5 -s` (contrived example).
2013-05-04 19:14:41 -04:00
while test $# -gt 0; do
# Reset option index
OPTIND=1
2013-05-04 15:29:10 -04:00
2013-05-04 19:14:41 -04:00
# Read options
2021-05-31 02:57:18 -04:00
while getopts ':hscrtdgn:w:o:-:' option "$@"; do
2013-05-04 19:44:31 -04:00
if test "$option" = '-'; then
case "$OPTARG" in
2020-10-22 17:06:17 -04:00
help | select | current | reset | toggle | delete | get)
2013-05-04 19:44:31 -04:00
v=''
;;
name | window | opacity)
eval v=\$$OPTIND
OPTIND=$((OPTIND + 1))
;;
name=* | window=* | opacity=*)
picom-trans: Use POSIX-compatible sed, grep
\b \? \+ \| are GNU extensions to sed
In BRE (Basic Regular Expressions) there is no \? \+ or \|
In ERE (Extended Regular Expressions) there is ? + and |
To specify sed to use ERE, specify the -E flag.
GNU grep does not distinguish between BRE and ERE, but other
implementations do. To make grep use ERE instead of BRE, specify
the -E flag.
The GNU extension \b has no equivalent in either BRE or ERE.
So, in line number 216, I used the whole initial expected output.
For quick reference (n/a means 'not available') -
GNU BRE | POSIX BRE | POSIX ERE
-------------------------------
\( | \( | (
\) | \) | )
\? | \{0,1\} | ? or {0,1}
\+ | \{1,\} | + or {1,}
\| | n/a | |
\b | n/a | n/a
2021-05-30 13:49:02 -04:00
v=$(echo "$OPTARG" | sed -E 's/^[^=]+=//')
2013-05-04 19:44:31 -04:00
;;
*)
echo "$0: illegal option $OPTARG" >& 2
exit 1
;;
esac
option=$(echo "$OPTARG" | cut -c 1)
OPTARG=$v
fi
2013-05-04 19:14:41 -04:00
case "$option" in
2020-10-22 16:48:59 -04:00
h) print_usage; exit 0 ;;
2013-05-04 19:14:41 -04:00
s) wprefix=''; window='' ;;
c)
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \
2015-03-25 20:31:00 -04:00
| grep -Eo '0x[[:xdigit:]]+' | head -n 1)
2013-05-04 19:14:41 -04:00
wprefix='-id'; window=$active
;;
2013-05-04 21:40:01 -04:00
r) action='reset' ;;
2020-10-22 17:06:17 -04:00
t) action='toggle' ;;
2013-05-04 19:14:41 -04:00
d) action='delete' ;;
g) action='get' ;;
n) wprefix='-name'; window=$OPTARG ;;
w) wprefix='-id'; window=$OPTARG ;;
o) opacity=$OPTARG ;;
2021-05-31 02:57:18 -04:00
\?) break ;;
2013-05-04 19:14:41 -04:00
esac
done
2013-05-04 15:29:10 -04:00
2013-05-04 19:14:41 -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.
picom-trans: Use POSIX-compatible sed, grep
\b \? \+ \| are GNU extensions to sed
In BRE (Basic Regular Expressions) there is no \? \+ or \|
In ERE (Extended Regular Expressions) there is ? + and |
To specify sed to use ERE, specify the -E flag.
GNU grep does not distinguish between BRE and ERE, but other
implementations do. To make grep use ERE instead of BRE, specify
the -E flag.
The GNU extension \b has no equivalent in either BRE or ERE.
So, in line number 216, I used the whole initial expected output.
For quick reference (n/a means 'not available') -
GNU BRE | POSIX BRE | POSIX ERE
-------------------------------
\( | \( | (
\) | \) | )
\? | \{0,1\} | ? or {0,1}
\+ | \{1,\} | + or {1,}
\| | n/a | |
\b | n/a | n/a
2021-05-30 13:49:02 -04:00
opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed -E 's/^~([0-9]+)$/-\1/')
2012-11-08 09:34:53 -05:00
2013-05-04 14:48:36 -04:00
# Validate opacity value
picom-trans: Use POSIX-compatible sed, grep
\b \? \+ \| are GNU extensions to sed
In BRE (Basic Regular Expressions) there is no \? \+ or \|
In ERE (Extended Regular Expressions) there is ? + and |
To specify sed to use ERE, specify the -E flag.
GNU grep does not distinguish between BRE and ERE, but other
implementations do. To make grep use ERE instead of BRE, specify
the -E flag.
The GNU extension \b has no equivalent in either BRE or ERE.
So, in line number 216, I used the whole initial expected output.
For quick reference (n/a means 'not available') -
GNU BRE | POSIX BRE | POSIX ERE
-------------------------------
\( | \( | (
\) | \) | )
\? | \{0,1\} | ? or {0,1}
\+ | \{1,\} | + or {1,}
\| | n/a | |
\b | n/a | n/a
2021-05-30 13:49:02 -04:00
if test -z "$action" && ! echo "$opacity" | grep -qE '^[+-]?[0-9]+$'; then
2013-05-04 15:29:10 -04:00
echo "Invalid opacity specified: $opacity."
2012-11-14 05:42:09 -05:00
exit 1
2012-11-08 09:34:53 -05:00
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' \
2016-04-28 00:05:49 -04:00
| while IFS=$(printf '\n') read wid; do
2013-05-04 21:40:01 -04:00
xprop -id "$wid" -remove _NET_WM_WINDOW_OPACITY
done
exit 0
fi
2012-11-08 09:34:53 -05:00
# Get ID of the target window
2013-05-04 14:18:04 -04:00
if test -z "$wprefix"; then
2012-11-14 05:42:09 -05:00
treeout=$(xwininfo -children -frame)
2012-11-08 11:03:22 -05:00
else
2013-05-04 21:50:42 -04:00
test "$wprefix" = '-id' \
2013-05-10 20:13:06 -04:00
&& ! echo "$window" | grep -Eiq '^[[:space:]]*(0x[[:xdigit:]]+|[[:digit:]]+)[[:space:]]*$' \
2013-05-04 21:50:42 -04:00
&& echo 'Bad window ID.' && exit 1
2013-05-04 14:18:04 -04:00
treeout=$(xwininfo -children $wprefix "$window")
2012-11-08 11:03:22 -05:00
fi
2012-11-08 09:34:53 -05:00
2012-11-11 00:02:14 -05:00
wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p')
2013-05-04 14:18:04 -04:00
if test -z "$wid"; then
2013-05-04 14:34:34 -04:00
echo 'Failed to find window.'
2012-11-14 05:42:09 -05:00
exit 1
2012-11-08 09:34:53 -05:00
fi
# Make sure it's not root window
2013-05-04 14:48:36 -04:00
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.'
2012-11-14 05:42:09 -05:00
exit 1
2012-11-08 09:34:53 -05:00
fi
2012-11-11 00:02:14 -05:00
# If it's already the topmost window
2013-05-04 14:48:36 -04:00
if echo "$treeout" | grep -q 'Parent window id: 0x[[:xdigit:]]* (the root window)'; then
2013-05-04 14:18:04 -04:00
topmost=$wid
2012-11-11 00:02:14 -05:00
else
2012-11-14 05:42:09 -05:00
# Get the whole window tree
2013-05-04 14:18:04 -04:00
treeout=$(xwininfo -root -tree)
2012-11-08 09:34:53 -05:00
2013-05-04 14:18:04 -04:00
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
2012-11-08 09:34:53 -05:00
2012-11-14 05:42:09 -05:00
# Find the line number of the target window in the window tree
2020-08-25 14:21:31 -04:00
lineno=$(echo -n "$treeout" | grep -nw "^\s*$wid" | head -n1 | cut -d ':' -f 1)
2011-12-07 13:35:22 -05:00
2013-05-04 14:18:04 -04:00
if test -z "$lineno"; then
2013-05-04 14:34:34 -04:00
echo 'Failed to find window in window tree.'
2012-11-14 05:42:09 -05:00
exit 1
fi
2011-12-07 13:35:22 -05:00
2012-11-14 05:42:09 -05:00
# Find the highest ancestor of the target window below
2013-05-04 14:18:04 -04:00
topmost=$(echo -n "$treeout" \
2020-08-25 14:21:31 -04:00
| head -n $lineno \
2013-05-04 14:34:34 -04:00
| sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \
| tail -n 1)
2012-11-11 00:02:14 -05:00
fi
2012-11-08 09:34:53 -05:00
2013-05-04 14:18:04 -04:00
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.'
2012-11-08 09:30:42 -05:00
exit 1
2012-11-08 09:34:53 -05:00
fi
2020-10-22 17:06:17 -04:00
# Get current opacity.
cur=$(xprop -id "$topmost" -notype _NET_WM_WINDOW_OPACITY \
picom-trans: Use POSIX-compatible sed, grep
\b \? \+ \| are GNU extensions to sed
In BRE (Basic Regular Expressions) there is no \? \+ or \|
In ERE (Extended Regular Expressions) there is ? + and |
To specify sed to use ERE, specify the -E flag.
GNU grep does not distinguish between BRE and ERE, but other
implementations do. To make grep use ERE instead of BRE, specify
the -E flag.
The GNU extension \b has no equivalent in either BRE or ERE.
So, in line number 216, I used the whole initial expected output.
For quick reference (n/a means 'not available') -
GNU BRE | POSIX BRE | POSIX ERE
-------------------------------
\( | \( | (
\) | \) | )
\? | \{0,1\} | ? or {0,1}
\+ | \{1,\} | + or {1,}
\| | n/a | |
\b | n/a | n/a
2021-05-30 13:49:02 -04:00
| sed -E 's/^_NET_WM_WINDOW_OPACITY = ([0-9]*)$|^.*$/\1/')
2020-10-22 17:06:17 -04:00
2013-05-04 14:48:36 -04:00
# Remove the opacity property.
2020-10-22 17:06:17 -04:00
if test x"$action" = x'delete' -o \( x"$action" = x'toggle' -a -n "$cur" \); then
2013-05-04 14:48:36 -04:00
xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY
exit 0
fi
2020-10-22 17:06:17 -04:00
# Unset opacity equals fully opaque
2013-05-04 17:37:52 -04:00
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 09:34:53 -05:00
# Calculate the desired opacity
2013-05-04 14:48:36 -04:00
if echo "$opacity" | grep -q '^[+-]'; then
2013-05-04 21:50:42 -04:00
opacity=$((cur + opacity))
2011-12-07 13:35:22 -05:00
fi
2012-11-08 09:34:53 -05:00
2013-05-04 14:18:04 -04:00
test $opacity -lt 0 && opacity=0
test $opacity -gt 100 && opacity=100
2012-11-08 09:34:53 -05:00
# Set opacity
2013-05-04 14:18:04 -04:00
opacity=$((opacity * 0xffffffff / 100))
2012-11-08 09:34:53 -05:00
xprop -id "$topmost" -f _NET_WM_WINDOW_OPACITY 32c \
2012-11-14 05:42:09 -05:00
-set _NET_WM_WINDOW_OPACITY "$opacity"