Skip to content

Commit

Permalink
misc: Primary one conserves its index (#16)
Browse files Browse the repository at this point in the history
Moving the primary monitor to the head of the
all-monitors list causes to increase all others
monitor indexes with 1.

To avoid confusion and breaking users's static
scripts, we prefer conserving a fixed id for each
monitor regardless a reboot.

Signed-off-by: Thomas Venriès <thomas.venries@gmail.com>
  • Loading branch information
Thomas Venriès committed Oct 30, 2017
1 parent 8a1294e commit 340d10f
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions mons
Original file line number Diff line number Diff line change
Expand Up @@ -252,29 +252,30 @@ main() {
[ -z "${enabled_out}" ] && { echo 'No monitor output detected.'; exit; }
mons="$(echo "${enabled_out}" | cut -d' ' -f1)"

# List plugged-in and turned-on outputs
enabled_out="$(echo "${enabled_out}" | grep ' connect')"
[ -z "${enabled_out}" ] && { echo 'No plugged-in monitor detected.'; exit 1; }
plug_mons="$(echo "${enabled_out}" | cut -d' ' -f1)"

# Set primary output
if $pFlag; then
if list_contains "${primary}" "${mons}"; then
if list_contains "${primary}" "${plug_mons}"; then
"${XRANDR}" --output "${primary}" --primary
else
echo "${primary}: output not found."; exit 1
echo "${primary}: output not connected."; exit 1
fi
else
primary="$(echo "${enabled_out}" | grep 'primary' | cut -d' ' -f1)"
fi

# Move primary to the head
# Move the primary monitor to the head if connected otherwise the first
# connected monitor that appears in the xrandr output is considerate as
# the primary one.
if [ -n "${primary}" ]; then
mons="$(list_del "${primary}" "${mons}")"
mons="$(list_insert "${primary}" 0 "${mons}")"
plug_mons="$(list_del "${primary}" "${plug_mons}")"
plug_mons="$(list_insert "${primary}" 0 "${plug_mons}")"
fi

# List plugged-in and turned-on outputs
enabled_out="$(echo "${enabled_out}" | grep ' connect')"
[ -z "${enabled_out}" ] && { echo 'No plugged-in monitor detected.'; exit 1; }
plug_mons="$(echo "${enabled_out}" | cut -d' ' -f1)"
plug_mons="$(list_del "${primary}" "${plug_mons}")"
plug_mons="$(list_insert "${primary}" 0 "${plug_mons}")"
enabled_out="$(echo "${enabled_out}" | grep -E '\+[0-9]{1,4}\+[0-9]{1,4}')"
disp_mons="$(echo "${enabled_out}" | cut -d' ' -f1)"

Expand Down

0 comments on commit 340d10f

Please sign in to comment.