displayselect 3.4 KB

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