2020-02-08 23:43:37 +00:00
|
|
|
#!/bin/sh
|
2018-04-07 06:22:10 +00:00
|
|
|
|
2019-06-01 14:53:34 +00:00
|
|
|
# A UI for detecting and selecting all displays. Probes xrandr for connected
|
|
|
|
# displays and lets user select one to use. User may also select "manual
|
|
|
|
# selection" which opens arandr.
|
2018-04-07 06:22:10 +00:00
|
|
|
|
2018-09-17 19:46:44 +00:00
|
|
|
twoscreen() { # If multi-monitor is selected and there are two screens.
|
2018-12-23 16:30:42 +00:00
|
|
|
|
|
|
|
mirror=$(printf "no\\nyes" | dmenu -i -p "Mirror displays?")
|
|
|
|
# Mirror displays using native resolution of external display and a scaled
|
|
|
|
# version for the internal display
|
|
|
|
if [ "$mirror" = "yes" ]; then
|
2018-12-23 18:11:22 +00:00
|
|
|
external=$(echo "$screens" | dmenu -i -p "Optimize resolution for:")
|
2018-12-23 16:30:42 +00:00
|
|
|
internal=$(echo "$screens" | grep -v "$external")
|
|
|
|
|
|
|
|
res_external=$(xrandr --query | sed -n "/^$external/,/\+/p" | \
|
2021-10-19 14:53:06 +00:00
|
|
|
tail -n 1 | awk '{print $1}')
|
2018-12-23 16:30:42 +00:00
|
|
|
res_internal=$(xrandr --query | sed -n "/^$internal/,/\+/p" | \
|
2021-10-19 14:53:06 +00:00
|
|
|
tail -n 1 | awk '{print $1}')
|
2018-12-23 16:30:42 +00:00
|
|
|
|
2020-06-01 19:29:57 +00:00
|
|
|
res_ext_x=$(echo "$res_external" | sed 's/x.*//')
|
|
|
|
res_ext_y=$(echo "$res_external" | sed 's/.*x//')
|
|
|
|
res_int_x=$(echo "$res_internal" | sed 's/x.*//')
|
|
|
|
res_int_y=$(echo "$res_internal" | sed 's/.*x//')
|
2018-12-23 16:30:42 +00:00
|
|
|
|
|
|
|
scale_x=$(echo "$res_ext_x / $res_int_x" | bc -l)
|
|
|
|
scale_y=$(echo "$res_ext_y / $res_int_y" | bc -l)
|
|
|
|
|
2018-12-23 18:11:22 +00:00
|
|
|
xrandr --output "$external" --auto --scale 1.0x1.0 \
|
2018-12-23 16:30:42 +00:00
|
|
|
--output "$internal" --auto --same-as "$external" \
|
|
|
|
--scale "$scale_x"x"$scale_y"
|
|
|
|
else
|
|
|
|
|
|
|
|
primary=$(echo "$screens" | dmenu -i -p "Select primary display:")
|
|
|
|
secondary=$(echo "$screens" | grep -v "$primary")
|
|
|
|
direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?")
|
2018-12-23 18:11:22 +00:00
|
|
|
xrandr --output "$primary" --auto --scale 1.0x1.0 --output "$secondary" --"$direction"-of "$primary" --auto --scale 1.0x1.0
|
2018-12-23 16:30:42 +00:00
|
|
|
fi
|
|
|
|
}
|
2018-09-17 19:46:44 +00:00
|
|
|
|
|
|
|
morescreen() { # If multi-monitor is selected and there are more than two screens.
|
2018-12-20 19:24:50 +00:00
|
|
|
primary=$(echo "$screens" | dmenu -i -p "Select primary display:")
|
2018-09-17 19:46:44 +00:00
|
|
|
secondary=$(echo "$screens" | grep -v "$primary" | dmenu -i -p "Select secondary display:")
|
|
|
|
direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?")
|
|
|
|
tertiary=$(echo "$screens" | grep -v "$primary" | grep -v "$secondary" | dmenu -i -p "Select third display:")
|
|
|
|
xrandr --output "$primary" --auto --output "$secondary" --"$direction"-of "$primary" --auto --output "$tertiary" --"$(printf "left\\nright" | grep -v "$direction")"-of "$primary" --auto
|
|
|
|
}
|
|
|
|
|
|
|
|
multimon() { # Multi-monitor handler.
|
|
|
|
case "$(echo "$screens" | wc -l)" in
|
|
|
|
2) twoscreen ;;
|
|
|
|
*) morescreen ;;
|
|
|
|
esac ;}
|
|
|
|
|
2020-04-20 20:48:32 +00:00
|
|
|
onescreen() { # If only one output available or chosen.
|
2020-12-11 14:51:50 +00:00
|
|
|
xrandr --output "$1" --auto --scale 1.0x1.0 $(echo "$allposs" | grep -v "\b$1" | awk '{print "--output", $1, "--off"}' | paste -sd ' ' -)
|
2020-04-20 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
postrun() { # Stuff to run to clean up.
|
|
|
|
setbg # Fix background if screen size/arangement has changed.
|
|
|
|
remaps # Re-remap keys if keyboard added (for laptop bases)
|
2020-05-30 23:02:51 +00:00
|
|
|
{ killall dunst ; setsid -f dunst ;} >/dev/null 2>&1 # Restart dunst to ensure proper location on screen
|
2020-04-20 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 19:46:44 +00:00
|
|
|
# Get all possible displays
|
|
|
|
allposs=$(xrandr -q | grep "connected")
|
|
|
|
|
|
|
|
# Get all connected screens.
|
2020-04-20 20:48:32 +00:00
|
|
|
screens=$(echo "$allposs" | awk '/ connected/ {print $1}')
|
|
|
|
|
|
|
|
# If there's only one screen
|
|
|
|
[ "$(echo "$screens" | wc -l)" -lt 2 ] &&
|
|
|
|
{ onescreen "$screens"; postrun; notify-send "💻 Only one screen detected." "Using it in its optimal settings..."; exit ;}
|
2018-09-17 19:46:44 +00:00
|
|
|
|
|
|
|
# Get user choice including multi-monitor and manual selection:
|
|
|
|
chosen=$(printf "%s\\nmulti-monitor\\nmanual selection" "$screens" | dmenu -i -p "Select display arangement:") &&
|
2018-04-07 06:22:10 +00:00
|
|
|
case "$chosen" in
|
2018-09-17 19:46:44 +00:00
|
|
|
"manual selection") arandr ; exit ;;
|
|
|
|
"multi-monitor") multimon ;;
|
2020-04-20 20:48:32 +00:00
|
|
|
*) onescreen "$chosen" ;;
|
2018-04-07 06:22:10 +00:00
|
|
|
esac
|
|
|
|
|
2020-04-20 20:48:32 +00:00
|
|
|
postrun
|