Skip to content

Commit

Permalink
feature: Add rotate option (#26)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Venriès <thomas.venries@gmail.com>
  • Loading branch information
Thomas Venriès committed Oct 5, 2018
1 parent 125273c commit 12f1800
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions mons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ Daemon mode:
of connected monitors changes, mons calls the given script with the
MONS_NUMBER environment variable.
Extra (in-conjunction or alone):
--dpi <dpi>
Set the DPI, a strictly positive value within the range [0 ; 27432].
Extra (in-conjunction):
--primary <mon_name>
Select a connected monitor as the primary output. Run the script
without argument to print monitors information, the names are in the
second column between ids and status. The primary monitor is marked
by an asterisk.
--rotate <orientation>
It rotates the primary monitor if the -o option is used otherwise it
rotates the second one, [left, right, normal, inverted].
--dpi <dpi>
Set the DPI, a strictly positive value within the range [0 ; 27432].
'
}

Expand Down Expand Up @@ -98,6 +101,10 @@ disable_mons() {
for mon in $@; do "$XRANDR" --output "$mon" --off ; done
}

rotate() {
eval "\"\$XRANDR\" --output \"$1\" --rotate \"$2\""
}

arg2xrandr() {
case $1 in
left) echo '--left-of' ;;
Expand Down Expand Up @@ -167,6 +174,7 @@ main() {
OFlag=false
SFlag=false
pFlag=false
rFlag=false
iFlag=false
xFlag=false
is_flag=false
Expand All @@ -179,11 +187,12 @@ main() {
case "$arg" in
--dpi) set -- "$@" '-i' ;;
--primary) set -- "$@" '-p' ;;
--rotate) set -- "$@" '-r' ;;
*) set -- "$@" "$arg"
esac
done

while getopts 'hvamosde:n:O:S:i:p:x:' opt; do
while getopts 'hvamosde:n:O:S:i:p:r:x:' opt; do
case $opt in
# Long options
i)
Expand All @@ -199,6 +208,12 @@ main() {
fi
pFlag=true; primary="$OPTARG"
;;
r) if ! echo "$OPTARG" | \
grep -E 'left|right|inverted|normal' > /dev/null 2>&1; then
arg_err
fi
rFlag=true; rArg="$OPTARG"
;;
# Short options
a) $is_flag && arg_err
aFlag=true ; is_flag=true
Expand Down Expand Up @@ -369,6 +384,7 @@ main() {
# still turned off or the window manager might need the monitor
# reset to cause the reconfiguration of the layout placement.
"$XRANDR" --auto
$rFlag && rotate "$(list_front "${plug_mons}")" "$rArg"
else
echo 'Only one monitor detected.'
fi
Expand All @@ -378,12 +394,14 @@ main() {
if $oFlag ; then
if [ "$(list_size "${disp_mons}")" -eq 1 ]; then
if [ "$(list_front "${disp_mons}")" = "$(list_front "${plug_mons}")" ]; then
$rFlag && rotate "$(list_front "${plug_mons}")" "$rArg"
exit
fi
fi
disp_mons="$(list_erase "$(list_front "${plug_mons}")" "$disp_mons")"
disable_mons "${disp_mons}"
enable_mon "$(list_front "${plug_mons}")"
$rFlag && rotate "$(list_front "${plug_mons}")" "$rArg"
exit
fi

Expand All @@ -403,6 +421,7 @@ main() {
disp_mons="$(list_erase "${mons_elt}" "${disp_mons}")"
disable_mons "${disp_mons}"
enable_mon "${mons_elt}"
$rFlag && rotate "$mons_elt" "$rArg"
exit
fi

Expand Down Expand Up @@ -430,6 +449,7 @@ main() {
enable_mon "$mon1"
enable_mon "$mon2"
"$XRANDR" --output "$mon2" "$area" "$mon1"
$rFlag && rotate "$mon2" "$rArg"
exit
fi

Expand All @@ -447,6 +467,7 @@ main() {
fi
enable_mon "$(list_get 1 "$plug_mons")"
disable_mons "$(list_front "$disp_mons")"
$rFlag && rotate "$(list_get 1 "${plug_mons}")" "$rArg"
exit
fi

Expand All @@ -457,7 +478,6 @@ main() {
if $dFlag ; then
"$XRANDR" --output "$(list_get 1 "$plug_mons")" \
--same-as "$(list_front "$plug_mons")"
exit $?
fi

if $mFlag ; then
Expand All @@ -474,14 +494,14 @@ main() {
"$XRANDR" --output "$(list_get 1 "$plug_mons")" \
--auto --scale-from "$size" \
--output "$(list_front "$plug_mons")"
exit $?
fi

if $eFlag ; then
"$XRANDR" --output "$(list_get 1 "$plug_mons")" \
"$(arg2xrandr "$eArg")" "$(list_front "$plug_mons")"
exit $?
fi

$rFlag && rotate "$(list_get 1 "${plug_mons}")" "$rArg"
else
echo 'At most two plugged monitors for this option.'
fi
Expand Down

0 comments on commit 12f1800

Please sign in to comment.