From 9a4d202f5dad3d49c396a077b10137eca9381174 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 13:18:04 -0500 Subject: [PATCH 01/12] compton-trans: consistency, better bash practices, refactor. --- bin/compton-trans | 53 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index f813433..d81a8ab 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -37,43 +37,44 @@ while getopts "scn:w:o:" option; do c) active=$(xprop -root -notype "_NET_ACTIVE_WINDOW" \ | sed 's/^.*\(0x\S*\).*$/\1/') - wprefix='-id '; window="$active" + wprefix='-id'; window=$active ;; - n) wprefix='-name '; window="$OPTARG" ;; - w) wprefix='-id '; window="$OPTARG" ;; - o) opacity="$OPTARG" ;; + n) wprefix='-name'; window=$OPTARG ;; + w) wprefix='-id'; window=$OPTARG ;; + o) opacity=$OPTARG ;; \?) exit 1;; esac done # Read positional arguments shift $(($OPTIND - 1)) -[ -n "$1" ] && opacity="$1" +test -n "$1" && opacity=$1 # Validate opacity value -if [ -z "$opacity" ]; then +if test -z "$opacity"; then echo "No opacity specified." exit 1 fi -opacity="$(echo "$opacity" \ - | sed -rn 's/^[[:space:]]*([+-]?[[:digit:]]+)[[:space:]]*$/\1/p')" +opacity=$(echo "$opacity" \ + | sed 's/%//g' \ + | sed -rn 's/^[[:space:]]*([+-]?[[:digit:]]+)[[:space:]]*$/\1/p') -if [ -z "$opacity" ]; then +if test -z "$opacity"; then echo "Invalid opacity value." exit 1 fi # Get ID of the target window -if [ -z "$wprefix" ]; then +if test -z "$wprefix"; then treeout=$(xwininfo -children -frame) else - treeout=$(xwininfo -children $wprefix"$window") + treeout=$(xwininfo -children $wprefix "$window") fi wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p') -if [ -z "$wid" ]; then +if test -z "$wid"; then echo "Failed to find window." exit 1 fi @@ -86,29 +87,31 @@ fi # If it's already the topmost window if echo "$treeout" | grep "Parent window id: 0x[[:xdigit:]]* (the root window)" > /dev/null; then - topmost="$wid" + topmost=$wid else # Get the whole window tree - treeout="$(xwininfo -root -tree)" + treeout=$(xwininfo -root -tree) - if [ -z "$treeout" ]; then + if test -z "$treeout"; then echo "Failed to get root window tree." 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)" + lineno=$(echo -n "$treeout" | grep -nw "$wid" | head -n1 | cut -d ':' -f 1) - if [ -z "$lineno" ]; then + if test -z "$lineno"; then echo "Failed to find window in window tree." exit 1 fi # Find the highest ancestor of the target window below - topmost=$(echo -n "$treeout" | head -n $(($lineno + 1)) | sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' | tail -n1) + topmost=$(echo -n "$treeout" \ + | head -n $(($lineno + 1)) \ + | sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' | tail -n1) fi -if [ -z "$topmost" ]; then +if test -z "$topmost"; then echo "Failed to find the highest parent window below root of the" \ "selected window." exit 1 @@ -116,19 +119,17 @@ fi # Calculate the desired opacity if echo "$opacity" | grep '^[+-]' > /dev/null; then - sign=$(echo "$opacity" | cut -b1) cur=$(xprop -id "$topmost" -notype "_NET_WM_WINDOW_OPACITY" \ | sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/') - [ -z "$cur" ] && cur=0xffffffff + test -z "$cur" && cur=0xffffffff cur=$((cur * 100 / 0xffffffff)) - opacity="$(echo "$opacity" | sed 's/^[+-]//')" - opacity=$(($cur $sign $opacity)) + opacity=$(($cur + $opacity)) fi -[ $opacity -lt 0 ] && opacity=0 -[ $opacity -gt 100 ] && opacity=100 +test $opacity -lt 0 && opacity=0 +test $opacity -gt 100 && opacity=100 # Set opacity -opacity=$(($opacity * 0xffffffff / 100)) +opacity=$((opacity * 0xffffffff / 100)) xprop -id "$topmost" -f _NET_WM_WINDOW_OPACITY 32c \ -set _NET_WM_WINDOW_OPACITY "$opacity" From 054d343622ed46e37fc7a143b1485dce683942d9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 13:34:34 -0500 Subject: [PATCH 02/12] compton-trans: more consistency. --- bin/compton-trans | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index d81a8ab..2b08dc3 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -21,7 +21,7 @@ # "command" is a shell built-in, faster than "which" if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then - echo "Please install x11-utils/xorg-xprop/xorg-xwininfo." >& 2 + echo 'Please install x11-utils/xorg-xprop/xorg-xwininfo.' >& 2 exit 1 fi @@ -31,11 +31,11 @@ cur= i= # Read options -while getopts "scn:w:o:" option; do +while getopts 'scn:w:o:' option; do case "$option" in s) wprefix=''; window='' ;; c) - active=$(xprop -root -notype "_NET_ACTIVE_WINDOW" \ + active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ | sed 's/^.*\(0x\S*\).*$/\1/') wprefix='-id'; window=$active ;; @@ -52,16 +52,16 @@ test -n "$1" && opacity=$1 # Validate opacity value if test -z "$opacity"; then - echo "No opacity specified." + echo 'No opacity specified.' exit 1 fi -opacity=$(echo "$opacity" \ - | sed 's/%//g' \ - | sed -rn 's/^[[:space:]]*([+-]?[[:digit:]]+)[[:space:]]*$/\1/p') +# clean up opacity. xargs == a poor man's trim. +opacity=$(echo "$opacity" | sed 's/%//g' | xargs) +#opacity=$(echo "$opacity" | sed 's/%//g' | sed 's/^ \+\| \+$//g') if test -z "$opacity"; then - echo "Invalid opacity value." + echo 'Invalid opacity value.' exit 1 fi @@ -75,25 +75,25 @@ fi wid=$(echo "$treeout" | sed -n 's/^xwininfo:.*: \(0x[[:xdigit:]]*\).*$/\1/p') if test -z "$wid"; then - echo "Failed to find window." + echo 'Failed to find window.' exit 1 fi # Make sure it's not root window -if echo "$treeout" | fgrep "Parent window id: 0x0" > /dev/null; then - echo "Cannot set opacity on root window." +if echo "$treeout" | fgrep 'Parent window id: 0x0' > /dev/null; then + echo 'Cannot set opacity on root window.' exit 1 fi # If it's already the topmost window -if echo "$treeout" | grep "Parent window id: 0x[[:xdigit:]]* (the root window)" > /dev/null; then +if echo "$treeout" | grep 'Parent window id: 0x[[:xdigit:]]* (the root window)' > /dev/null; then topmost=$wid else # Get the whole window tree treeout=$(xwininfo -root -tree) if test -z "$treeout"; then - echo "Failed to get root window tree." + echo 'Failed to get root window tree.' exit 1 fi @@ -101,25 +101,26 @@ else lineno=$(echo -n "$treeout" | grep -nw "$wid" | head -n1 | cut -d ':' -f 1) if test -z "$lineno"; then - echo "Failed to find window in window tree." + echo 'Failed to find window in window tree.' exit 1 fi # Find the highest ancestor of the target window below topmost=$(echo -n "$treeout" \ | head -n $(($lineno + 1)) \ - | sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' | tail -n1) + | sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \ + | tail -n 1) fi if test -z "$topmost"; then - echo "Failed to find the highest parent window below root of the" \ - "selected window." + echo 'Failed to find the highest parent window below root of the' \ + 'selected window.' exit 1 fi # Calculate the desired opacity if echo "$opacity" | grep '^[+-]' > /dev/null; then - cur=$(xprop -id "$topmost" -notype "_NET_WM_WINDOW_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)) From 25d31a1f5eaab8c55cf977992157af5330c4e542 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 13:48:36 -0500 Subject: [PATCH 03/12] compton-trans: cleanup. add remove-prop option. --- bin/compton-trans | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index 2b08dc3..bd9f00c 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -25,13 +25,19 @@ if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then exit 1 fi +# Variables +wprefix= window= opacity= cur= -i= +delete= +treeout= +wid= +topmost= +lineno= # Read options -while getopts 'scn:w:o:' option; do +while getopts 'scdn:w:o:' option; do case "$option" in s) wprefix=''; window='' ;; c) @@ -39,6 +45,7 @@ while getopts 'scn:w:o:' option; do | sed 's/^.*\(0x\S*\).*$/\1/') wprefix='-id'; window=$active ;; + d) delete=1 ;; n) wprefix='-name'; window=$OPTARG ;; w) wprefix='-id'; window=$OPTARG ;; o) opacity=$OPTARG ;; @@ -50,18 +57,12 @@ done shift $(($OPTIND - 1)) test -n "$1" && opacity=$1 -# Validate opacity value -if test -z "$opacity"; then - echo 'No opacity specified.' - exit 1 -fi - # clean up opacity. xargs == a poor man's trim. opacity=$(echo "$opacity" | sed 's/%//g' | xargs) -#opacity=$(echo "$opacity" | sed 's/%//g' | sed 's/^ \+\| \+$//g') -if test -z "$opacity"; then - echo 'Invalid opacity value.' +# Validate opacity value +if test -z "$opacity" -a -z "$delete"; then + echo 'No opacity specified.' exit 1 fi @@ -80,13 +81,13 @@ if test -z "$wid"; then fi # Make sure it's not root window -if echo "$treeout" | fgrep 'Parent window id: 0x0' > /dev/null; then +if echo "$treeout" | fgrep -q 'Parent window id: 0x0'; then echo 'Cannot set opacity on root window.' exit 1 fi # If it's already the topmost window -if echo "$treeout" | grep 'Parent window id: 0x[[:xdigit:]]* (the root window)' > /dev/null; then +if echo "$treeout" | grep -q 'Parent window id: 0x[[:xdigit:]]* (the root window)'; then topmost=$wid else # Get the whole window tree @@ -118,8 +119,14 @@ if test -z "$topmost"; then exit 1 fi +# Remove the opacity property. +if test -n "$delete"; then + xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY + exit 0 +fi + # Calculate the desired opacity -if echo "$opacity" | grep '^[+-]' > /dev/null; then +if echo "$opacity" | grep -q '^[+-]'; then cur=$(xprop -id "$topmost" -notype _NET_WM_WINDOW_OPACITY \ | sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/') test -z "$cur" && cur=0xffffffff From 86b20e0a082540dd80260f8cfc946ec3a681c0e1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 14:29:10 -0500 Subject: [PATCH 04/12] compton-trans: improve option parsing. --- bin/compton-trans | 62 +++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index bd9f00c..c352128 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -7,6 +7,7 @@ # # Usage: +# $ compton-trans [options] [+|-]opacity # By window id # $ compton-trans -w "$WINDOWID" 75 # By name @@ -26,6 +27,7 @@ if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then fi # Variables +active= wprefix= window= opacity= @@ -36,33 +38,51 @@ wid= topmost= lineno= -# Read options -while getopts 'scdn:w:o:' option; do - case "$option" in - s) wprefix=''; window='' ;; - c) - active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ - | sed 's/^.*\(0x\S*\).*$/\1/') - wprefix='-id'; window=$active - ;; - d) delete=1 ;; - n) wprefix='-name'; window=$OPTARG ;; - w) wprefix='-id'; window=$OPTARG ;; - o) opacity=$OPTARG ;; - \?) exit 1;; - esac -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 + LASTIND=1 -# Read positional arguments -shift $(($OPTIND - 1)) -test -n "$1" && opacity=$1 + # Read options + while getopts ':scdn:w:o:' option "$@"; do + case "$option" in + s) wprefix=''; window='' ;; + c) + active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ + | sed 's/^.*\(0x\S*\).*$/\1/') + wprefix='-id'; window=$active + ;; + d) delete=1 ;; + 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 +done # clean up opacity. xargs == a poor man's trim. opacity=$(echo "$opacity" | sed 's/%//g' | xargs) # Validate opacity value -if test -z "$opacity" -a -z "$delete"; then - echo 'No opacity specified.' +if test -z "$delete" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then + echo "Invalid opacity specified: $opacity." exit 1 fi From ad23872801d6135e87234159e861d46667dbaee2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 16:37:52 -0500 Subject: [PATCH 05/12] compton-trans: add a 'get' option. --- bin/compton-trans | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index c352128..baf99e4 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -32,7 +32,7 @@ wprefix= window= opacity= cur= -delete= +action= treeout= wid= topmost= @@ -48,7 +48,7 @@ while test $# -gt 0; do LASTIND=1 # Read options - while getopts ':scdn:w:o:' option "$@"; do + while getopts ':scdgn:w:o:' option "$@"; do case "$option" in s) wprefix=''; window='' ;; c) @@ -56,7 +56,8 @@ while test $# -gt 0; do | sed 's/^.*\(0x\S*\).*$/\1/') wprefix='-id'; window=$active ;; - d) delete=1 ;; + d) action='delete' ;; + g) action='get' ;; n) wprefix='-name'; window=$OPTARG ;; w) wprefix='-id'; window=$OPTARG ;; o) opacity=$OPTARG ;; @@ -81,7 +82,7 @@ done opacity=$(echo "$opacity" | sed 's/%//g' | xargs) # Validate opacity value -if test -z "$delete" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then +if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then echo "Invalid opacity specified: $opacity." exit 1 fi @@ -140,17 +141,25 @@ if test -z "$topmost"; then fi # Remove the opacity property. -if test -n "$delete"; then +if test x"$action" = x'delete'; then xprop -id "$topmost" -remove _NET_WM_WINDOW_OPACITY exit 0 fi +# 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 - cur=$(xprop -id "$topmost" -notype _NET_WM_WINDOW_OPACITY \ - | sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/') - test -z "$cur" && cur=0xffffffff - cur=$((cur * 100 / 0xffffffff)) opacity=$(($cur + $opacity)) fi From e6b973fe2f5b63271a9565641cf50205de96af7f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 16:54:18 -0500 Subject: [PATCH 06/12] compton-trans: minor. consistency. --- bin/compton-trans | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/compton-trans b/bin/compton-trans index baf99e4..91714f9 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -129,7 +129,7 @@ else # Find the highest ancestor of the target window below topmost=$(echo -n "$treeout" \ - | head -n $(($lineno + 1)) \ + | head -n $((lineno + 1)) \ | sed -n 's/^ \(0x[[:xdigit:]]*\).*/\1/p' \ | tail -n 1) fi From c9bf88cc89a60d1cd465ed8ca994c1d3c2985f34 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 17:50:31 -0500 Subject: [PATCH 07/12] compton-trans: use a different workaround with getopts. --- bin/compton-trans | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index 91714f9..7b2ac7d 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -38,6 +38,11 @@ wid= topmost= lineno= +# 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 @@ -45,10 +50,9 @@ lineno= while test $# -gt 0; do # Reset option index OPTIND=1 - LASTIND=1 # Read options - while getopts ':scdgn:w:o:' option "$@"; do + while getopts 'scdgn:w:o:' option "$@"; do case "$option" in s) wprefix=''; window='' ;; c) @@ -61,16 +65,8 @@ while test $# -gt 0; do 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 - ;; + \?) exit 1 ;; esac - LASTIND=$OPTIND done # Read positional arguments @@ -79,7 +75,7 @@ while test $# -gt 0; do done # clean up opacity. xargs == a poor man's trim. -opacity=$(echo "$opacity" | sed 's/%//g' | xargs) +opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/') # Validate opacity value if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then From 80a3c805619fcec158259f68ad966308712b83f9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 17:51:00 -0500 Subject: [PATCH 08/12] compton-trans: use getopt instead of getopts. --- bin/compton-trans | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index 7b2ac7d..af2d4db 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -27,6 +27,8 @@ if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then fi # Variables +args= +arg= active= wprefix= window= @@ -43,36 +45,34 @@ 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). +# Use getopt to parse arguments. +args=$(getopt -o 'scdgn:w:o:' -l 'select,current,delete,get,name:,window:,opacity:' -- "$@") +if test $? -ne 0; then + echo 'Bad arguments.' + exit 1 +fi + +eval set -- "$args" + while test $# -gt 0; do - # Reset option index - OPTIND=1 - - # Read options - while getopts 'scdgn:w:o:' option "$@"; do - case "$option" in - s) wprefix=''; window='' ;; - c) - active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ - | sed 's/^.*\(0x\S*\).*$/\1/') - wprefix='-id'; window=$active - ;; - d) action='delete' ;; - g) action='get' ;; - n) wprefix='-name'; window=$OPTARG ;; - w) wprefix='-id'; window=$OPTARG ;; - o) opacity=$OPTARG ;; - \?) exit 1 ;; - esac - done - - # Read positional arguments - shift $((OPTIND - 1)) - test -n "$1" && opacity=$1 && shift + arg=$1 + shift + case "$arg" in + --) break ;; + -s | --select) wprefix=''; window='' ;; + -c | --current) + active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ + | sed 's/^.*\(0x\S*\).*$/\1/') + wprefix='-id'; window=$active + ;; + -d | --delete) action='delete' ;; + -g | --get) action='get' ;; + -n | --name) wprefix='-name'; window=$1; shift ;; + -w | --window) wprefix='-id'; window=$1; shift ;; + -o | --opacity) opacity=$1; shift ;; + esac done +test -n "$1" && opacity=$1 # clean up opacity. xargs == a poor man's trim. opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/') From b0db9f4f071e35b103674d528ff4072dce1177f5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 18:14:41 -0500 Subject: [PATCH 09/12] Revert "compton-trans: use getopt instead of getopts." This reverts commit 80a3c805619fcec158259f68ad966308712b83f9. --- bin/compton-trans | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index af2d4db..7b2ac7d 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -27,8 +27,6 @@ if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then fi # Variables -args= -arg= active= wprefix= window= @@ -45,34 +43,36 @@ for v in "$@"; do shift && set -- "$@" "$(echo "$v" | sed 's/^-\([0-9]\+%\?\)$/~\1/')" done -# Use getopt to parse arguments. -args=$(getopt -o 'scdgn:w:o:' -l 'select,current,delete,get,name:,window:,opacity:' -- "$@") -if test $? -ne 0; then - echo 'Bad arguments.' - exit 1 -fi - -eval set -- "$args" - +# 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 - arg=$1 - shift - case "$arg" in - --) break ;; - -s | --select) wprefix=''; window='' ;; - -c | --current) - active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ - | sed 's/^.*\(0x\S*\).*$/\1/') - wprefix='-id'; window=$active - ;; - -d | --delete) action='delete' ;; - -g | --get) action='get' ;; - -n | --name) wprefix='-name'; window=$1; shift ;; - -w | --window) wprefix='-id'; window=$1; shift ;; - -o | --opacity) opacity=$1; shift ;; - esac + # Reset option index + OPTIND=1 + + # Read options + while getopts 'scdgn:w:o:' option "$@"; do + case "$option" in + s) wprefix=''; window='' ;; + c) + active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ + | sed 's/^.*\(0x\S*\).*$/\1/') + wprefix='-id'; window=$active + ;; + d) action='delete' ;; + g) action='get' ;; + n) wprefix='-name'; window=$OPTARG ;; + w) wprefix='-id'; window=$OPTARG ;; + o) opacity=$OPTARG ;; + \?) exit 1 ;; + esac + done + + # Read positional arguments + shift $((OPTIND - 1)) + test -n "$1" && opacity=$1 && shift done -test -n "$1" && opacity=$1 # clean up opacity. xargs == a poor man's trim. opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/') From 7c451c10f770b439e3f08858049cdbd56a959299 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 18:44:31 -0500 Subject: [PATCH 10/12] compton-trans: allow long option names. --- bin/compton-trans | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bin/compton-trans b/bin/compton-trans index 7b2ac7d..25f4669 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -52,7 +52,27 @@ while test $# -gt 0; do OPTIND=1 # Read options - while getopts 'scdgn:w:o:' option "$@"; do + while getopts 'scdgn:w:o:-:' option "$@"; do + if test "$option" = '-'; then + case "$OPTARG" in + select | current | 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) From 216441c7b04b81b5c54a9552f4b1273f87cf8ce2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 20:40:01 -0500 Subject: [PATCH 11/12] compton-trans: add reset option. --- bin/compton-trans | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/bin/compton-trans b/bin/compton-trans index 25f4669..5444e17 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -37,6 +37,8 @@ treeout= wid= topmost= lineno= +option= +v= # Workaround: replace '-5' with '~5' so as not to confuse getopts. for v in "$@"; do @@ -52,10 +54,10 @@ while test $# -gt 0; do OPTIND=1 # Read options - while getopts 'scdgn:w:o:-:' option "$@"; do + while getopts 'scrdgn:w:o:-:' option "$@"; do if test "$option" = '-'; then case "$OPTARG" in - select | current | delete | get) + select | current | reset | delete | get) v='' ;; name | window | opacity) @@ -80,6 +82,7 @@ while test $# -gt 0; do | sed 's/^.*\(0x\S*\).*$/\1/') wprefix='-id'; window=$active ;; + r) action='reset' ;; d) action='delete' ;; g) action='get' ;; n) wprefix='-name'; window=$OPTARG ;; @@ -103,6 +106,16 @@ if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then exit 1 fi +# 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) From a9a3b015e1413c00f817c20ef724d3b6c4ea5082 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 4 May 2013 20:50:42 -0500 Subject: [PATCH 12/12] compton-trans: ensure wid format. misc. xwininfo falls back to cursor selection if the window ID is formatted improperly. --- bin/compton-trans | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/compton-trans b/bin/compton-trans index 5444e17..6beeabf 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -19,6 +19,10 @@ # $ 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 # "command" is a shell built-in, faster than "which" if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then @@ -120,6 +124,9 @@ fi if test -z "$wprefix"; then treeout=$(xwininfo -children -frame) else + test "$wprefix" = '-id' \ + && ! echo "$window" | grep -q '^\(0x\)\?[0-9a-fA-F]\+$' \ + && echo 'Bad window ID.' && exit 1 treeout=$(xwininfo -children $wprefix "$window") fi @@ -189,7 +196,7 @@ fi # Calculate the desired opacity if echo "$opacity" | grep -q '^[+-]'; then - opacity=$(($cur + $opacity)) + opacity=$((cur + opacity)) fi test $opacity -lt 0 && opacity=0