dotfiles/.config/i3/auto-xrandr

52 lines
1.6 KiB
Plaintext
Raw Normal View History

2019-07-20 12:14:25 +02:00
#!/bin/bash
# preferred order of monitors (highest to lowest priority)
order=(DP HDMI)
# preferred position of monitors (relative to previsous monitor in $order)
position=${1:-above}
# maximum number of monitors to use (only names from $order will be used)
max=10
if [[ "$1" = -h || "$1" = --help ]]; then
echo "Usage: $(basename "$0") [position]" >&2
exit
fi
connected=($(xrandr | grep ' connected ' | cut -f 1 -d ' '))
options=()
for choice in ${order[@]}; do
# iterate over the indices of the connected monitors
for monitor in ${!connected[@]}; do
if [[ ${connected[$monitor]} =~ $choice ]]; then
if [[ ${#options[@]} -eq 0 ]]; then
# set first match from $order as primary monitor
options=(--output ${connected[$monitor]} --auto --primary)
else
# put monitor in $position of the $last one
options=(${options[@]} --output ${connected[$monitor]} --auto --$position $last)
fi
# remember this monitor to position the next one
last=${connected[$monitor]}
# delete this monitor from the list of connected monitors
unset connected[$monitor]
connected=(${connected[@]})
# count down the number of monitors that is desired at most
((max--))
if [[ $max -eq 0 ]]; then
break 2
else
break
fi
fi
done
done
# let xrandr turn all disconnected monitors off (just in case) and also all
# connected monitors that where above the maximum number of desired monitors
for disconnected in $(xrandr | grep disconnected | cut -f 1 -d ' ') ${connected[@]}; do
options=(${options[@]} --output $disconnected --off)
done
xrandr --verbose ${options[@]}