displayselect (3523B)
1 #!/usr/bin/env sh 2 3 # A UI for detecting and selecting all displays. 4 # Probes xrandr for connected displays and lets user select one to use. 5 # User may also select "manual selection" which opens arandr. 6 # I plan on adding a routine from multi-monitor setups later. 7 8 twoscreen() { # If multi-monitor is selected and there are two screens. 9 10 mirror=$(printf "no\\nyes" | dmenu -i -p "Mirror displays?") 11 # Mirror displays using native resolution of external display and a scaled 12 # version for the internal display 13 if [ "$mirror" = "yes" ]; then 14 external=$(echo "$screens" | dmenu -i -p "Optimize resolution for:") 15 internal=$(echo "$screens" | grep -v "$external") 16 17 res_external=$(xrandr --query | sed -n "/^$external/,/\+/p" | \ 18 tail -n 1 | awk '{print $1}') 19 res_internal=$(xrandr --query | sed -n "/^$internal/,/\+/p" | \ 20 tail -n 1 | awk '{print $1}') 21 22 res_ext_x=$(echo $res_external | sed 's/x.*//') 23 res_ext_y=$(echo $res_external | sed 's/.*x//') 24 res_int_x=$(echo $res_internal | sed 's/x.*//') 25 res_int_y=$(echo $res_internal | sed 's/.*x//') 26 27 scale_x=$(echo "$res_ext_x / $res_int_x" | bc -l) 28 scale_y=$(echo "$res_ext_y / $res_int_y" | bc -l) 29 30 xrandr --output "$external" --auto --scale 1.0x1.0 \ 31 --output "$internal" --auto --same-as "$external" \ 32 --scale "$scale_x"x"$scale_y" 33 else 34 35 primary=$(echo "$screens" | dmenu -i -p "Select primary display:") 36 secondary=$(echo "$screens" | grep -v "$primary") 37 direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?") 38 xrandr --output "$primary" --auto --scale 1.0x1.0 --output "$secondary" --"$direction"-of "$primary" --auto --scale 1.0x1.0 39 fi 40 } 41 42 morescreen() { # If multi-monitor is selected and there are more than two screens. 43 primary=$(echo "$screens" | dmenu -i -p "Select primary display:") 44 secondary=$(echo "$screens" | grep -v "$primary" | dmenu -i -p "Select secondary display:") 45 direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?") 46 tertiary=$(echo "$screens" | grep -v "$primary" | grep -v "$secondary" | dmenu -i -p "Select third display:") 47 xrandr --output "$primary" --auto --output "$secondary" --"$direction"-of "$primary" --auto --output "$tertiary" --"$(printf "left\\nright" | grep -v "$direction")"-of "$primary" --auto 48 } 49 50 multimon() { # Multi-monitor handler. 51 case "$(echo "$screens" | wc -l)" in 52 1) xrandr $(echo "$allposs" | grep -v "$screens" | awk '{print "--output", $1, "--off"}' | tr '\n' ' ') ;; 53 2) twoscreen ;; 54 *) morescreen ;; 55 esac ;} 56 57 # Get all possible displays 58 allposs=$(xrandr -q | grep "connected") 59 60 # Get all connected screens. 61 screens=$(echo "$allposs" | grep " connected" | awk '{print $1}') 62 63 # Get user choice including multi-monitor and manual selection: 64 chosen=$(printf "%s\\nmulti-monitor\\nmanual selection" "$screens" | dmenu -i -p "Select display arangement:") && 65 case "$chosen" in 66 "manual selection") arandr ; exit ;; 67 "multi-monitor") multimon ;; 68 *) xrandr --output "$chosen" --auto --scale 1.0x1.0 $(echo "$allposs" | grep -v "$chosen" | awk '{print "--output", $1, "--off"}' | tr '\n' ' ') ;; 69 esac 70 71 setbg # Fix background if screen size/arangement has changed. 72 remaps # Re-remap keys if keyboard added (for laptop bases) 73 pgrep -x dunst >/dev/null && killall dunst && setsid dunst & # Restart dunst to ensure proper location on screen