autorandr 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/bin/sh
  2. #
  3. # Automatically select a display configuration based on connected devices
  4. #
  5. # Stefan Tomanek <stefan.tomanek@wertarbyte.de>
  6. #
  7. # How to use:
  8. #
  9. # Save your current display configuration and setup with:
  10. # $ autorandr --save mobile
  11. #
  12. # Connect an additional display, configure your setup and save it:
  13. # $ autorandr --save docked
  14. #
  15. # Now autorandr can detect which hardware setup is active:
  16. # $ autorandr
  17. # mobile
  18. # docked (detected)
  19. #
  20. # To automatically reload your setup, just append --change to the command line
  21. #
  22. # To manually load a profile, you can use the --load <profile> option.
  23. #
  24. # autorandr tries to avoid reloading an identical configuration. To force the
  25. # (re)configuration, apply --force.
  26. #
  27. # To prevent a profile from being loaded, place a script call "block" in its
  28. # directory. The script is evaluated before the screen setup is inspected, and
  29. # in case of it returning a value of 0 the profile is skipped. This can be used
  30. # to query the status of a docking station you are about to leave.
  31. #
  32. # If no suitable profile can be identified, the current configuration is kept.
  33. # To change this behaviour and switch to a fallback configuration, specify
  34. # --default <profile>
  35. #
  36. # Another script called "postswitch "can be placed in the directory
  37. # ~/.autorandr as well as in all profile directories: The scripts are executed
  38. # after a mode switch has taken place and can notify window managers or other
  39. # applications about it.
  40. #
  41. #
  42. # While the script uses xrandr by default, calling it by the name "autodisper"
  43. # or "auto-disper" forces it to use the "disper" utility, which is useful for
  44. # controlling nvidia chipsets. The formats for fingerprinting the current setup
  45. # and saving/loading the current configuration are adjusted accordingly.
  46. XRANDR=/usr/bin/xrandr
  47. DISPER=/usr/bin/disper
  48. XDPYINFO=/usr/bin/xdpyinfo
  49. PROFILES=~/.autorandr/
  50. CONFIG=~/.autorandr.conf
  51. CHANGE_PROFILE=0
  52. FORCE_LOAD=0
  53. DEFAULT_PROFILE=""
  54. SAVE_PROFILE=""
  55. FP_METHODS="setup_fp_xrandr_edid setup_fp_sysfs_edid"
  56. CURRENT_CFG_METHOD="current_cfg_xrandr"
  57. LOAD_METHOD="load_cfg_xrandr"
  58. SCRIPTNAME="$(basename $0)"
  59. # when called as autodisper/auto-disper, we assume different defaults
  60. if [ "$SCRIPTNAME" = "auto-disper" ] || [ "$SCRIPTNAME" = "autodisper" ]; then
  61. echo "Assuming disper defaults..." >&2
  62. FP_METHODS="setup_fp_disper"
  63. CURRENT_CFG_METHOD="current_cfg_disper"
  64. LOAD_METHOD="load_cfg_disper"
  65. fi
  66. if [ -f $CONFIG ]; then
  67. echo "Loading configuration from '$CONFIG'" >&2
  68. . $CONFIG
  69. fi
  70. setup_fp_xrandr_edid() {
  71. $XRANDR -q --verbose | awk '
  72. /^[^ ]+ (dis)?connected / { DEV=$1; }
  73. $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
  74. END { for (X in ID) { print X " " ID[X]; } }'
  75. }
  76. setup_fp_sysfs_edid() {
  77. # xrandr triggers the reloading of EDID data
  78. $XRANDR -q > /dev/null
  79. # hash the EDIDs of all _connected_ devices
  80. for P in /sys/class/drm/card*-*/; do
  81. # nothing found
  82. [ ! -d "$P" ] && continue
  83. if grep -q "^connected$" < "${P}status"; then
  84. echo -n "$(basename "$P") "
  85. md5sum ${P}edid | awk '{print $1}'
  86. fi
  87. done
  88. }
  89. setup_fp_disper() {
  90. $DISPER -l | grep '^display '
  91. }
  92. setup_fp() {
  93. local FP="";
  94. for M in $FP_METHODS; do
  95. FP="$($M)"
  96. if [ -n "$FP" ]; then
  97. break
  98. fi
  99. done
  100. if [ -z "$FP" ]; then
  101. echo "Unable to fingerprint display configuration" >&2
  102. return
  103. fi
  104. echo "$FP"
  105. }
  106. current_cfg_xrandr() {
  107. local PRIMARY_SETUP="";
  108. if [ -x "$XDPYINFO" ]; then
  109. PRIMARY_SETUP="$($XDPYINFO -ext XINERAMA | awk '/^ head #0:/ {printf $3 $5}')"
  110. fi
  111. $XRANDR -q | awk -v primary_setup="${PRIMARY_SETUP}" '
  112. # display is connected and has a mode
  113. /^[^ ]+ connected [^(]/ {
  114. split($3, A, "+");
  115. print "output "$1;
  116. print "mode "A[1];
  117. print "pos "A[2]"x"A[3];
  118. if ($4 !~ /^\(/) {
  119. print "rotate "$4;
  120. }
  121. if (A[1] A[2] "," A[3] == primary_setup)
  122. print "primary";
  123. next;
  124. }
  125. # disconnected or disabled displays
  126. /^[^ ]+ (dis)?connected / ||
  127. /^[^ ]+ unknown connection / {
  128. print "output "$1;
  129. print "off";
  130. next;
  131. }'
  132. }
  133. current_cfg_disper() {
  134. $DISPER -p
  135. }
  136. current_cfg() {
  137. $CURRENT_CFG_METHOD;
  138. }
  139. blocked() {
  140. local PROFILE="$1"
  141. [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
  142. "$PROFILES/$PROFILE/block" "$PROFILE"
  143. }
  144. config_equal() {
  145. local PROFILE="$1"
  146. if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
  147. echo "Config already loaded"
  148. return 0
  149. else
  150. return 1
  151. fi
  152. }
  153. load_cfg_xrandr() {
  154. sed 's!^!--!' "$1" | xargs $XRANDR
  155. }
  156. load_cfg_disper() {
  157. $DISPER -i < "$1"
  158. }
  159. load() {
  160. local PROFILE="$1"
  161. local CONF="$PROFILES/$PROFILE/config"
  162. if [ -e "$CONF" ] ; then
  163. echo " -> loading profile $PROFILE"
  164. $LOAD_METHOD "$CONF"
  165. [ -x "$PROFILES/$PROFILE/postswitch" ] && \
  166. "$PROFILES/$PROFILE/postswitch" "$PROFILE"
  167. [ -x "$PROFILES/postswitch" ] && \
  168. "$PROFILES/postswitch" "$PROFILE"
  169. fi
  170. }
  171. help() {
  172. cat <<EOH
  173. Usage: $SCRIPTNAME [options]
  174. -h, --help get this small help
  175. -c, --change reload current setup
  176. -s, --save <profile> save your current setup to profile <profile>
  177. -l, --load <profile> load profile <profile>
  178. -d, --default <profile> make profile <profile> the default profile
  179. --force force (re)loading of a profile
  180. --fingerprint fingerprint your current hardware setup
  181. --config dump your current xrandr setup
  182. To prevent a profile from being loaded, place a script call "block" in its
  183. directory. The script is evaluated before the screen setup is inspected, and
  184. in case of it returning a value of 0 the profile is skipped. This can be used
  185. to query the status of a docking station you are about to leave.
  186. If no suitable profile can be identified, the current configuration is kept.
  187. To change this behaviour and switch to a fallback configuration, specify
  188. --default <profile>.
  189. Another script called "postswitch "can be placed in the directory
  190. ~/.autorandr as well as in any profile directories: The scripts are executed
  191. after a mode switch has taken place and can notify window managers.
  192. When called by the name "autodisper" or "auto-disper", the script uses "disper"
  193. instead of "xrandr" to detect, configure and save the display configuration.
  194. EOH
  195. exit
  196. }
  197. # process parameters
  198. OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,config,help -- "$@")
  199. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  200. eval set -- "$OPTS"
  201. while true; do
  202. case "$1" in
  203. -c|--change) CHANGE_PROFILE=1; shift ;;
  204. -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
  205. -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
  206. -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
  207. -h|--help) help ;;
  208. --force) FORCE_LOAD=1; shift ;;
  209. --fingerprint) setup_fp; exit 0;;
  210. --config) current_cfg; exit 0;;
  211. --) shift; break ;;
  212. *) echo "Error: $1"; exit 1;;
  213. esac
  214. done
  215. CURRENT_SETUP="$(setup_fp)"
  216. if [ -n "$SAVE_PROFILE" ]; then
  217. echo "Saving current configuration as profile '${SAVE_PROFILE}'"
  218. mkdir -p "$PROFILES/$SAVE_PROFILE"
  219. echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
  220. $CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
  221. exit 0
  222. fi
  223. if [ -n "$LOAD_PROFILE" ]; then
  224. CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
  225. exit $?
  226. fi
  227. for SETUP_FILE in $PROFILES/*/setup; do
  228. if ! [ -e $SETUP_FILE ]; then
  229. break
  230. fi
  231. PROFILE="$(basename $(dirname "$SETUP_FILE"))"
  232. echo -n "$PROFILE"
  233. if blocked "$PROFILE"; then
  234. echo " (blocked)"
  235. continue
  236. fi
  237. FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
  238. if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
  239. echo " (detected)"
  240. if [ "$CHANGE_PROFILE" -eq 1 ]; then
  241. if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
  242. load "$PROFILE"
  243. fi
  244. fi
  245. # found the profile, exit with success
  246. exit 0
  247. else
  248. echo ""
  249. fi
  250. done
  251. # we did not find the profile, load default
  252. if [ -n "$DEFAULT_PROFILE" ]; then
  253. echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
  254. load "$DEFAULT_PROFILE"
  255. fi
  256. exit 1