3 # Automatically select a display configuration based on connected devives
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 # autorand --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 # To prevent a profile from being loaded, place a script call "block" in its
25 # directory. The script is evaluated before the screen setup is inspected, and
26 # in case of it returning a value of 0 the profile is skipped. This can be used
27 # to query the status of a docking station you are about to leave.
29 # If no suitable profile can be identified, the current configuration is kept.
30 # To change this behaviour and switch to a fallback configuration, specify
33 # Another script called "postswitch "can be placed in the directory
34 # ~/.auto-disper as well as in all profile directories: The scripts are
35 # executed after a mode switch has taken place and can notify window managers
36 # or other applications about it.
38 XRANDR=/usr/bin/xrandr
39 PROFILES=~/.autorandr/
40 CONFIG=~/.autorandr.conf
46 FP_METHODS="setup_fp_xrandr_edid setup_fp_sysfs_edid"
48 test -f $CONFIG && . $CONFIG
50 setup_fp_xrandr_edid() {
51 $XRANDR -q --verbose | awk '
52 /^[^ ]+ (dis)?connected / { DEV=$1; }
53 $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
54 END { for (X in ID) { print X " " ID[X]; } }'
57 setup_fp_sysfs_edid() {
58 # xrandr triggers the reloading of EDID data
59 $XRANDR -q > /dev/null
60 # hash the EDIDs of all _connected_ devices
61 for P in /sys/class/drm/card*-*/; do
62 if grep -q "^connected$" < "${P}status"; then
63 echo -n "$(basename "$P") "
64 md5sum ${P}edid | awk '{print $1}'
71 for M in $FP_METHODS; do
73 [ -n "$FP" ] && break;
76 echo "Unable to fingerprint display configuration" >&2
85 /^[^ ]+ disconnected / {
93 print "pos "A[2]"x"A[3];
99 [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
101 "$PROFILES/$PROFILE/block" "$PROFILE"
106 if [ "$CHANGE_PROFILE" -eq 1 ] && [ -e "$PROFILES/$PROFILE/config" ] ; then
107 echo " -> loading profile $PROFILE"
108 sed 's!^!--!' "$PROFILES/$PROFILE/config" | xargs xrandr
110 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
111 "$PROFILES/$PROFILE/postswitch" "$PROFILE"
112 [ -x "$PROFILES/postswitch" ] && \
113 "$PROFILES/postswitch" "$PROFILE"
119 Usage: autorandr action [profile-name]
121 -h, --help get this small help
122 -c, --change reload current setup
123 -s, --save <profile> save your current setup to profile <profile>
124 -l, --load <profile> load profile <profile>
125 --fingerprint fingerprints your actual config
127 To prevent a profile from being loaded, place a script call "block" in its
128 directory. The script is evaluated before the screen setup is inspected, and
129 in case of it returning a value of 0 the profile is skipped. This can be used
130 to query the status of a docking station you are about to leave.
132 If no suitable profile can be identified, the current configuration is kept.
133 To change this behaviour and switch to a fallback configuration, specify
136 Another script called "postswitch "can be placed in the directory
137 ~/.auto-disper as well as in all profile directories: The scripts are
138 executed after a mode switch has taken place and can notify window managers
144 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,fingerprint,help -- "$@")
145 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
150 -c|--change) CHANGE_PROFILE=1; shift ;;
151 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
152 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
153 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
155 --fingerprint) setup_fp; exit 0;;
157 *) echo "Error: $1"; exit 1;;
161 CURRENT_SETUP="$(setup_fp)"
163 if [ -n "$SAVE_PROFILE" ]; then
164 echo "Saving current configuration as profile '${SAVE_PROFILE}'"
165 mkdir -p "$PROFILES/$SAVE_PROFILE"
166 echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
167 current_cfg > "$PROFILES/$SAVE_PROFILE/config"
171 if [ -n "$LOAD_PROFILE" ]; then
172 CHANGE_PROFILE=1 load "$LOAD_PROFILE"
176 for SETUP_FILE in $PROFILES/*/setup; do
177 if ! [ -e $SETUP_FILE ]; then
180 PROFILE="$(basename $(dirname "$SETUP_FILE"))"
183 if blocked "$PROFILE"; then
188 FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
189 if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
192 # found the profile, exit with success
199 # we did not find the profile, load default
200 if [ -n "$DEFAULT_PROFILE" ]; then
201 echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
202 load "$DEFAULT_PROFILE"