3 # Automatically select a display configuration based on connected devices
5 # Stefan Tomanek <stefan.tomanek@wertarbyte.de>
9 # Save your current display configuration and setup with:
10 # $ autorandr --save mobile
12 # Connect an additional display, configure your setup and save it:
13 # $ autorandr --save docked
15 # Now autorandr can detect which hardware setup is active:
20 # To automatically reload your setup, just append --change to the command line
22 # To manually load a profile, you can use the --load <profile> option.
24 # autorandr tries to avoid reloading an identical configuration. To force the
25 # (re)configuration, apply --force.
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.
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
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.
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.
47 XRANDR=/usr/bin/xrandr
48 DISPER=/usr/bin/disper
49 XDPYINFO=/usr/bin/xdpyinfo
50 PROFILES=~/.autorandr/
51 CONFIG=~/.autorandr.conf
58 FP_METHODS="setup_fp_sysfs_edid setup_fp_xrandr_edid"
59 CURRENT_CFG_METHOD="current_cfg_xrandr"
60 LOAD_METHOD="load_cfg_xrandr"
62 SCRIPTNAME="$(basename $0)"
63 # when called as autodisper/auto-disper, we assume different defaults
64 if [ "$SCRIPTNAME" = "auto-disper" ] || [ "$SCRIPTNAME" = "autodisper" ]; then
65 echo "Assuming disper defaults..." >&2
66 FP_METHODS="setup_fp_disper"
67 CURRENT_CFG_METHOD="current_cfg_disper"
68 LOAD_METHOD="load_cfg_disper"
71 if [ -f $CONFIG ]; then
72 echo "Loading configuration from '$CONFIG'" >&2
76 setup_fp_xrandr_edid() {
77 $XRANDR -q --verbose | awk '
78 /^[^ ]+ (dis)?connected / { DEV=$1; }
79 $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
80 END { for (X in ID) { print X " " ID[X]; } }'
83 setup_fp_sysfs_edid() {
84 # xrandr triggers the reloading of EDID data
85 $XRANDR -q > /dev/null
86 # hash the EDIDs of all _connected_ devices
87 for P in /sys/class/drm/card*-*/; do
89 [ ! -d "$P" ] && continue
90 if grep -q "^connected$" < "${P}status"; then
91 echo -n "$(basename "$P") "
92 md5sum ${P}edid | awk '{print $1}'
98 $DISPER -l | grep '^display '
103 for M in $FP_METHODS; do
105 if [ -n "$FP" ]; then
109 if [ -z "$FP" ]; then
110 echo "Unable to fingerprint display configuration" >&2
116 current_cfg_xrandr() {
117 local PRIMARY_SETUP="";
118 if [ -x "$XDPYINFO" ]; then
119 PRIMARY_SETUP="$($XDPYINFO -ext XINERAMA | awk '/^ head #0:/ {printf $3 $5}')"
121 $XRANDR -q | awk -v primary_setup="${PRIMARY_SETUP}" '
122 # display is connected and has a mode
123 /^[^ ]+ connected [^(]/ {
125 if ($3 == "primary") {
132 if (A[1] A[2] "," A[3] == primary_setup)
135 if (($4 == "left") || ($4 == "right")) {
141 print "pos "A[2]"x"A[3];
147 # disconnected or disabled displays
148 /^[^ ]+ (dis)?connected / ||
149 /^[^ ]+ unknown connection / {
156 current_cfg_disper() {
166 [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
168 "$PROFILES/$PROFILE/block" "$PROFILE"
173 if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
174 echo "Config already loaded"
182 # sed 1: Prefix arguments with "--"
183 # sed 2: Merge arguments into one line per output
184 # sed 3: Merge into two lines, all --off outputs in the first one
185 sed 's/^/--/' "$1" | sed -e '
193 # Merge if next line contains --off
194 s/\n\([^\n]* --off\)/ \1/
200 # Merge if previous line contains --mode
201 s/\(--mode [^\n]*\)\n/\1 /
203 $!d' | xargs -L 1 $XRANDR
212 local CONF="$PROFILES/$PROFILE/config"
213 if [ -e "$CONF" ] ; then
214 [ -x "$PROFILES/preswitch" ] && \
215 "$PROFILES/preswitch" "$PROFILE"
216 [ -x "$PROFILES/$PROFILE/preswitch" ] && \
217 "$PROFILES/$PROFILE/preswitch" "$PROFILE"
219 echo " -> loading profile $PROFILE"
222 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
223 "$PROFILES/$PROFILE/postswitch" "$PROFILE"
224 [ -x "$PROFILES/postswitch" ] && \
225 "$PROFILES/postswitch" "$PROFILE"
231 Usage: $SCRIPTNAME [options]
233 -h, --help get this small help
234 -c, --change reload current setup
235 -s, --save <profile> save your current setup to profile <profile>
236 -l, --load <profile> load profile <profile>
237 -d, --default <profile> make profile <profile> the default profile
238 --force force (re)loading of a profile
239 --fingerprint fingerprint your current hardware setup
240 --config dump your current xrandr setup
242 To prevent a profile from being loaded, place a script call "block" in its
243 directory. The script is evaluated before the screen setup is inspected, and
244 in case of it returning a value of 0 the profile is skipped. This can be used
245 to query the status of a docking station you are about to leave.
247 If no suitable profile can be identified, the current configuration is kept.
248 To change this behaviour and switch to a fallback configuration, specify
251 Another script called "postswitch "can be placed in the directory
252 ~/.autorandr as well as in any profile directories: The scripts are executed
253 after a mode switch has taken place and can notify window managers.
255 When called by the name "autodisper" or "auto-disper", the script uses "disper"
256 instead of "xrandr" to detect, configure and save the display configuration.
262 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,config,help -- "$@")
263 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
268 -c|--change) CHANGE_PROFILE=1; shift ;;
269 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
270 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
271 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
273 --force) FORCE_LOAD=1; shift ;;
274 --fingerprint) setup_fp; exit 0;;
275 --config) current_cfg; exit 0;;
277 *) echo "Error: $1"; exit 1;;
281 CURRENT_SETUP="$(setup_fp)"
283 if [ -n "$SAVE_PROFILE" ]; then
284 echo "Saving current configuration as profile '${SAVE_PROFILE}'"
285 mkdir -p "$PROFILES/$SAVE_PROFILE"
286 echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
287 $CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
291 if [ -n "$LOAD_PROFILE" ]; then
292 CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
296 for SETUP_FILE in $PROFILES/*/setup; do
297 if ! [ -e $SETUP_FILE ]; then
300 PROFILE="$(basename $(dirname "$SETUP_FILE"))"
303 if blocked "$PROFILE"; then
308 FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
309 if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
311 if [ "$CHANGE_PROFILE" -eq 1 ]; then
312 if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
316 # found the profile, exit with success
323 # we did not find the profile, load default
324 if [ -n "$DEFAULT_PROFILE" ]; then
325 echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
326 load "$DEFAULT_PROFILE"