My dotfiles for my Linux rice managed with stow and make
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
3.4 KiB

  1. #!/bin/env 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. layoutloc=$XDG_CONFIG_HOME/screenlayout
  7. twoscreen() { # If multi-monitor is selected and there are two screens.
  8. mirror=$(printf "no\\nyes" | bemenucolor -i -p "Mirror?")
  9. # Mirror displays using native resolution of external display and a scaled
  10. # version for the internal display
  11. if [ "$mirror" = "yes" ]; then
  12. external=$(echo "$screens" | bemenucolor -i -p "Optimize:")
  13. internal=$(echo "$screens" | grep -v "$external")
  14. res_external=$(xrandr --query | sed -n "/^$external/,/\+/p" | \
  15. tail -n 1 | awk '{print $1}')
  16. res_internal=$(xrandr --query | sed -n "/^$internal/,/\+/p" | \
  17. tail -n 1 | awk '{print $1}')
  18. res_ext_x=$(echo "$res_external" | sed 's/x.*//')
  19. res_ext_y=$(echo "$res_external" | sed 's/.*x//')
  20. res_int_x=$(echo "$res_internal" | sed 's/x.*//')
  21. res_int_y=$(echo "$res_internal" | sed 's/.*x//')
  22. scale_x=$(echo "$res_ext_x / $res_int_x" | bc -l)
  23. scale_y=$(echo "$res_ext_y / $res_int_y" | bc -l)
  24. xrandr --output "$external" --auto --scale 1.0x1.0 \
  25. --output "$internal" --auto --same-as "$external" \
  26. --scale "$scale_x"x"$scale_y"
  27. else
  28. primary=$(echo "$screens" | bemenucolor -i -p "Primary:")
  29. secondary=$(echo "$screens" | grep -v "$primary")
  30. direction=$(printf "left-of\\nright-of\\nabove\\nbelow" | bemenucolor -i -p "Side:")
  31. xrandr --output "$primary" --auto --scale 1.0x1.0 --output "$secondary" --"$direction" "$primary" --auto --scale 1.0x1.0
  32. fi
  33. }
  34. morescreen() { # If multi-monitor is selected and there are more than two screens.
  35. primary=$(echo "$screens" | bemenucolor -i -p "Primary:")
  36. secondary=$(echo "$screens" | grep -v "$primary" | bemenucolor -i -p "Secondary:")
  37. direction=$(printf "left-of\\nright-of\\nabove\\nbelow" | bemenucolor -i -p "Side:")
  38. tertiary=$(echo "$screens" | grep -v "$primary" | grep -v "$secondary" | bemenucolor -i -p "Tertiary:")
  39. direction2=$(printf "left-of\\nright-of\\nabove\\nbelow" | grep -v "$direction" | bemenucolor -i -p "Side:")
  40. xrandr --output "$primary" --auto --output "$secondary" --"$direction" "$primary" --auto --output "$tertiary" --"$direction2" "$primary" --auto
  41. }
  42. multimon() { # Multi-monitor handler.
  43. case "$(echo "$screens" | wc -l)" in
  44. 1) xrandr "$(echo "$allposs" | grep -v "$screens" | awk '{print "--output", $1, "--off"}' | tr '\n' ' ')" ;;
  45. 2) twoscreen ;;
  46. *) morescreen ;;
  47. esac ;}
  48. # Get all possible displays
  49. allposs=$(xrandr -q | grep "connected")
  50. # Get all connected screens.
  51. screens=$(echo "$allposs" | grep " connected" | awk '{print $1}')
  52. layouts=$(find "$layoutloc" -type f -name "*.sh" -printf "%f\n")
  53. # Get user choice including multi-monitor and manual selection:
  54. chosen=$(printf "%s\\nmulti-monitor\\nmanual selection\\n%s\\n" "$screens" "$layouts" | bemenucolor -i -p "Display:") &&
  55. case "$chosen" in
  56. "manual selection") arandr ; exit ;;
  57. "multi-monitor") multimon ;;
  58. *.sh) "$layoutloc"/"$chosen" ;;
  59. *) xrandr --output "$chosen" --auto --scale 1.0x1.0 $(echo "$allposs" | grep -v "$chosen" | awk '{print "--output", $1, "--off"}' | tr '\n' ' ') ;;
  60. esac
  61. setbackground
  62. remaps