compton-trans: add error handling. better option parsing.

This commit is contained in:
Christopher Jeffrey 2012-11-08 08:30:42 -06:00
parent 239796abc6
commit c883cdec3e
2 changed files with 111 additions and 40 deletions

View File

@ -1,19 +1,25 @@
#!/bin/bash #!/bin/bash
#
# compton-trans
# transset in a bash script # transset in a bash script
# Copyright (c) 2011-2012, Christopher Jeffrey # Copyright (c) 2011-2012, Christopher Jeffrey
#
# Usage: # Usage:
# by window id # By window id
# settrans -w "$WINDOWID" -o 75 # $ compton-trans -w "$WINDOWID" 75
# by name # By name
# settrans -n "urxvt" -o 75 # $ compton-trans -n "urxvt" 75
# by current window # By current window
# settrans -c -o 75 # $ compton-trans -c 75
# by selection # By selection
# settrans -s -o 75 # $ compton-trans 75
# increment current window 5% # $ compton-trans -s 75
# settrans -c -o +5 # Increment current window 5%
# $ compton-trans -c +5
# Remove opacity property of current window
# $ compton-trans -c none
if test -z "$(which xprop)" -o -z "$(which xwininfo)"; then if test -z "$(which xprop)" -o -z "$(which xwininfo)"; then
echo "Please install x11-utils/xorg-xprop/xorg-xwininfo." >& 2 echo "Please install x11-utils/xorg-xprop/xorg-xwininfo." >& 2
@ -25,34 +31,74 @@ opacity=
cur= cur=
root= root=
parent= parent=
active=
i= i=
while getopts "scn:w:o:" option; do #
case "$option" in # Options
s) window="" ;; #
c)
arg=
val=
active=
select=
while test $# -gt 0; do
arg="$1"
shift
case "$arg" in
-s | -select | --select) ;;
-c | -current | --current)
active=$(xprop -root -notype "_NET_ACTIVE_WINDOW" \ active=$(xprop -root -notype "_NET_ACTIVE_WINDOW" \
| sed 's/^.*\(0x\S*\).*$/\1/') | sed 's/^.*\(0x\S*\).*$/\1/')
window="-id $active" window="-id $active"
;; ;;
n) window="-name $OPTARG" ;; -n | -name | --name)
w) window="-id $OPTARG" ;; val="$1"
o) opacity="$OPTARG" ;; shift
window="-name $val"
;;
-w | -window | --window)
val="$1"
shift
window="-id $val"
;;
-o | -opacity | --opacity)
val="$1"
shift
opacity="$val"
;;
*)
opacity="$arg"
;;
esac esac
done done
if test -z "$window"; then
select=$(xwininfo | grep 'Window id:' | sed 's/^.*\(0x\S*\).*$/\1/')
window="-id $select"
fi
#
# Find Window
#
root=$(xwininfo -all -root \ root=$(xwininfo -all -root \
| grep "Root window id" \ | grep "Root window id" \
| sed 's/^.*\(0x\S*\).*$/\1/') | sed 's/^.*\(0x\S*\).*$/\1/')
parent=$window parent=$window
i=0 i=0
while true; do while true; do
parent=$(xwininfo -all $parent \ parent=$(xwininfo -all $parent \
| grep Parent \ | grep Parent \
| sed 's/^.*\(0x\S*\).*$/\1/') | sed 's/^.*\(0x\S*\).*$/\1/')
if test -z "$parent"; then
echo "Window not found." >& 2
exit 1
fi
if test "$parent" = "$root"; then if test "$parent" = "$root"; then
break break
fi fi
@ -60,7 +106,7 @@ while true; do
parent="-id $parent" parent="-id $parent"
window=$parent window=$parent
i=$((i+1)) i=$((i + 1))
if test $i -ge 1000; then if test $i -ge 1000; then
echo "An error occurred while traversing up the window tree." >& 2 echo "An error occurred while traversing up the window tree." >& 2
echo "Please report this to https://github.com/chjj/compton/issues." >& 2 echo "Please report this to https://github.com/chjj/compton/issues." >& 2
@ -69,24 +115,46 @@ while true; do
fi fi
done done
inc=$(echo "$opacity" | sed 's/^\(+\|-\).*$\|^.*$/\1/') if test -z "$window"; then
if test -n "$inc"; then echo "Window not found." >& 2
exit 1
fi
#
# Check (or Delete) Opacity
#
if test "$opacity" = "none"; then
xprop $window -remove _NET_WM_WINDOW_OPACITY
exit $?
fi
opacity=$(echo "$opacity" | sed 's/[^-+0-9]//g')
if test -z "$opacity"; then
echo "Bad opacity value." >& 2
exit 1
fi
#
# Determine Relative Opacity
#
if test -n "$(echo "$opacity" | sed 's/^\(+\|-\).*$\|^.*$/\1/')"; then
cur=$(xprop $window -notype "_NET_WM_WINDOW_OPACITY" \ cur=$(xprop $window -notype "_NET_WM_WINDOW_OPACITY" \
| sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/') | sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/')
test -z "$cur" && cur=$((0xffffffff)) test -z "$cur" && cur=$((0xffffffff))
cur=$((cur*100/0xffffffff)) cur=$((cur * 100 / 0xffffffff))
opacity=$(echo "$opacity" | sed 's/\(\+\|\-\)//') opacity=$((cur + opacity))
if test "$inc" = "+"; then
opacity=$((cur+opacity))
else
opacity=$((cur-opacity))
fi
fi fi
#
# Determine and Set Opacity
#
if test -n "$opacity" -a -n "$window"; then if test -n "$opacity" -a -n "$window"; then
test $opacity -lt 0 && opacity=0 test $opacity -lt 0 && opacity=0
test $opacity -gt 100 && opacity=100 test $opacity -gt 100 && opacity=100
opacity=$((opacity*0xffffffff/100)) opacity=$((opacity * 0xffffffff / 100))
xprop $window -f _NET_WM_WINDOW_OPACITY 32c \ xprop $window -f _NET_WM_WINDOW_OPACITY 32c \
-set _NET_WM_WINDOW_OPACITY "$opacity" -set _NET_WM_WINDOW_OPACITY "$opacity"
fi fi

View File

@ -6,7 +6,7 @@ compton\-trans \- an opacity setter tool
.SH SYNOPSIS .SH SYNOPSIS
.nf .nf
.B compton-trans [-wncs] [window] -o [opacity] .B compton-trans [-wncs] [window] opacity
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION
@ -17,20 +17,20 @@ It is similar to other utilities like transset(1) or transset-df(1).
.SH OPTIONS .SH OPTIONS
.TP .TP
.BI \-w\ window\-id .BI \-w,\ \-window,\ \-\-window\ \fIwindow\-id\fP
Specify the window id to target. Specify the window id to target.
.TP .TP
.BI \-n\ window\-name .BI \-n,\ \-name,\ \-\-name\ \fIwindow\-name\fP
Specify and try to match a window name. Specify and try to match a window name.
.TP .TP
.BI \-c .BI \-c,\ \-current,\ \-\-current
Specify the current window as a target. Specify the current window as a target.
.TP .TP
.BI \-s .BI \-s,\ \-select,\ \-\-select
Select target window with mouse cursor. Select target window with mouse cursor.
This is the default if no window has been specified. This is the default if no window has been specified.
.TP .TP
.BI \-o\ opacity .BI \-o,\ \-opacity,\ \-\-opacity\ \fIopacity\fP
Specify the new opacity value for the window. This value Specify the new opacity value for the window. This value
can be anywhere from 1-100. If it is prefixed with a plus can be anywhere from 1-100. If it is prefixed with a plus
or minus (+/-), this will increment or decrement from the or minus (+/-), this will increment or decrement from the
@ -39,19 +39,22 @@ target window's current opacity instead.
.SH EXAMPLES .SH EXAMPLES
.TP .TP
Set window id to opacity of 75%. Set window id to opacity of 75%.
compton-trans -w "$WINDOWID" -o 75 compton-trans -w "$WINDOWID" 75
.TP .TP
Set window name, "urxvt", to opacity of 75%. Set window name, "urxvt", to opacity of 75%.
compton-trans -n "urxvt" -o 75 compton-trans -n "urxvt" 75
.TP .TP
Set current window to opacity of 75%. Set current window to opacity of 75%.
compton-trans -c -o 75 compton-trans -c 75
.TP .TP
Select target window and set opacity to 75%. Select target window and set opacity to 75%.
compton-trans -s -o 75 compton-trans 75
.TP .TP
Increment current window 5% opacity. Increment current window 5% opacity.
compton-trans -c -o +5 compton-trans -c +5
.TP
Remove opacity property of current window.
compton-trans -c none
.SH BUGS .SH BUGS
Please report any you find to https://github.com/chjj/compton. Please report any you find to https://github.com/chjj/compton.