Skip to content

Commit

Permalink
compton-trans: add error handling. better option parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Nov 8, 2012
1 parent 239796a commit c883cde
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 40 deletions.
126 changes: 97 additions & 29 deletions bin/compton-trans
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#!/bin/bash

#
# compton-trans
# transset in a bash script
# Copyright (c) 2011-2012, Christopher Jeffrey
#

# Usage:
# by window id
# settrans -w "$WINDOWID" -o 75
# by name
# settrans -n "urxvt" -o 75
# by current window
# settrans -c -o 75
# by selection
# settrans -s -o 75
# increment current window 5%
# settrans -c -o +5
# 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
# Remove opacity property of current window
# $ compton-trans -c none

if test -z "$(which xprop)" -o -z "$(which xwininfo)"; then
echo "Please install x11-utils/xorg-xprop/xorg-xwininfo." >& 2
Expand All @@ -25,42 +31,82 @@ opacity=
cur=
root=
parent=
active=
i=

while getopts "scn:w:o:" option; do
case "$option" in
s) window="" ;;
c)
#
# Options
#

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" \
| sed 's/^.*\(0x\S*\).*$/\1/')
window="-id $active"
;;
n) window="-name $OPTARG" ;;
w) window="-id $OPTARG" ;;
o) opacity="$OPTARG" ;;
-n | -name | --name)
val="$1"
shift
window="-name $val"
;;
-w | -window | --window)
val="$1"
shift
window="-id $val"
;;
-o | -opacity | --opacity)
val="$1"
shift
opacity="$val"
;;
*)
opacity="$arg"
;;
esac
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 \
| grep "Root window id" \
| sed 's/^.*\(0x\S*\).*$/\1/')

parent=$window
i=0

while true; do
parent=$(xwininfo -all $parent \
| grep Parent \
| sed 's/^.*\(0x\S*\).*$/\1/')

if test -z "$parent"; then
echo "Window not found." >& 2
exit 1
fi

if test "$parent" = "$root"; then
break
fi

parent="-id $parent"
window=$parent

i=$((i+1))
i=$((i + 1))
if test $i -ge 1000; then
echo "An error occurred while traversing up the window tree." >& 2
echo "Please report this to https://github.com/chjj/compton/issues." >& 2
Expand All @@ -69,24 +115,46 @@ while true; do
fi
done

inc=$(echo "$opacity" | sed 's/^\(+\|-\).*$\|^.*$/\1/')
if test -n "$inc"; then
if test -z "$window"; 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" \
| sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/')
test -z "$cur" && cur=$((0xffffffff))
cur=$((cur*100/0xffffffff))
opacity=$(echo "$opacity" | sed 's/\(\+\|\-\)//')
if test "$inc" = "+"; then
opacity=$((cur+opacity))
else
opacity=$((cur-opacity))
fi
cur=$((cur * 100 / 0xffffffff))
opacity=$((cur + opacity))
fi

#
# Determine and Set Opacity
#

if test -n "$opacity" -a -n "$window"; then
test $opacity -lt 0 && opacity=0
test $opacity -gt 100 && opacity=100
opacity=$((opacity*0xffffffff/100))
opacity=$((opacity * 0xffffffff / 100))
xprop $window -f _NET_WM_WINDOW_OPACITY 32c \
-set _NET_WM_WINDOW_OPACITY "$opacity"
fi
25 changes: 14 additions & 11 deletions man/compton-trans.1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ compton\-trans \- an opacity setter tool

.SH SYNOPSIS
.nf
.B compton-trans [-wncs] [window] -o [opacity]
.B compton-trans [-wncs] [window] opacity
.fi

.SH DESCRIPTION
Expand All @@ -17,20 +17,20 @@ It is similar to other utilities like transset(1) or transset-df(1).

.SH OPTIONS
.TP
.BI \-w\ window\-id
.BI \-w,\ \-window,\ \-\-window\ \fIwindow\-id\fP
Specify the window id to target.
.TP
.BI \-n\ window\-name
.BI \-n,\ \-name,\ \-\-name\ \fIwindow\-name\fP
Specify and try to match a window name.
.TP
.BI \-c
.BI \-c,\ \-current,\ \-\-current
Specify the current window as a target.
.TP
.BI \-s
.BI \-s,\ \-select,\ \-\-select
Select target window with mouse cursor.
This is the default if no window has been specified.
.TP
.BI \-o\ opacity
.BI \-o,\ \-opacity,\ \-\-opacity\ \fIopacity\fP
Specify the new opacity value for the window. This value
can be anywhere from 1-100. If it is prefixed with a plus
or minus (+/-), this will increment or decrement from the
Expand All @@ -39,19 +39,22 @@ target window's current opacity instead.
.SH EXAMPLES
.TP
Set window id to opacity of 75%.
compton-trans -w "$WINDOWID" -o 75
compton-trans -w "$WINDOWID" 75
.TP
Set window name, "urxvt", to opacity of 75%.
compton-trans -n "urxvt" -o 75
compton-trans -n "urxvt" 75
.TP
Set current window to opacity of 75%.
compton-trans -c -o 75
compton-trans -c 75
.TP
Select target window and set opacity to 75%.
compton-trans -s -o 75
compton-trans 75
.TP
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
Please report any you find to https://github.com/chjj/compton.
Expand Down

0 comments on commit c883cde

Please sign in to comment.