dmenudisplaysel.sh 3.4 KB

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