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 # autorandr tries to avoid reloading an identical configuration. To force the
25 # 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 # ~/.auto-disper as well as in all profile directories: The scripts are
38 # executed after a mode switch has taken place and can notify window managers
39 # or other applications about it.
41 XRANDR=/usr/bin/xrandr
42 PROFILES=~/.autorandr/
43 CONFIG=~/.autorandr.conf
50 FP_METHODS="setup_fp_xrandr_edid setup_fp_sysfs_edid"
52 test -f $CONFIG && . $CONFIG
54 setup_fp_xrandr_edid() {
55 $XRANDR -q --verbose | awk '
56 /^[^ ]+ (dis)?connected / { DEV=$1; }
57 $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
58 END { for (X in ID) { print X " " ID[X]; } }'
61 setup_fp_sysfs_edid() {
62 # xrandr triggers the reloading of EDID data
63 $XRANDR -q > /dev/null
64 # hash the EDIDs of all _connected_ devices
65 for P in /sys/class/drm/card*-*/; do
66 if grep -q "^connected$" < "${P}status"; then
67 echo -n "$(basename "$P") "
68 md5sum ${P}edid | awk '{print $1}'
75 for M in $FP_METHODS; do
77 [ -n "$FP" ] && break;
80 echo "Unable to fingerprint display configuration" >&2
89 /^[^ ]+ disconnected / {
97 print "pos "A[2]"x"A[3];
103 [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
105 "$PROFILES/$PROFILE/block" "$PROFILE"
110 if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
111 echo "Config already loaded"
120 if [ -e "$PROFILES/$PROFILE/config" ] ; then
121 echo " -> loading profile $PROFILE"
122 sed 's!^!--!' "$PROFILES/$PROFILE/config" | xargs xrandr
124 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
125 "$PROFILES/$PROFILE/postswitch" "$PROFILE"
126 [ -x "$PROFILES/postswitch" ] && \
127 "$PROFILES/postswitch" "$PROFILE"
133 Usage: autorandr action [profile-name]
135 -h, --help get this small help
136 -c, --change reload current setup
137 -s, --save <profile> save your current setup to profile <profile>
138 -l, --load <profile> load profile <profile>
139 --force force loading of a profile
140 --fingerprint fingerprints your actual config
142 To prevent a profile from being loaded, place a script call "block" in its
143 directory. The script is evaluated before the screen setup is inspected, and
144 in case of it returning a value of 0 the profile is skipped. This can be used
145 to query the status of a docking station you are about to leave.
147 If no suitable profile can be identified, the current configuration is kept.
148 To change this behaviour and switch to a fallback configuration, specify
151 Another script called "postswitch "can be placed in the directory
152 ~/.auto-disper as well as in all profile directories: The scripts are
153 executed after a mode switch has taken place and can notify window managers
159 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,help -- "$@")
160 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
165 -c|--change) CHANGE_PROFILE=1; shift ;;
166 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
167 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
168 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
170 --force) FORCE_LOAD=1; shift ;;
171 --fingerprint) setup_fp; exit 0;;
173 *) echo "Error: $1"; exit 1;;
177 CURRENT_SETUP="$(setup_fp)"
179 if [ -n "$SAVE_PROFILE" ]; then
180 echo "Saving current configuration as profile '${SAVE_PROFILE}'"
181 mkdir -p "$PROFILES/$SAVE_PROFILE"
182 echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
183 current_cfg > "$PROFILES/$SAVE_PROFILE/config"
187 if [ -n "$LOAD_PROFILE" ]; then
188 CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
192 for SETUP_FILE in $PROFILES/*/setup; do
193 if ! [ -e $SETUP_FILE ]; then
196 PROFILE="$(basename $(dirname "$SETUP_FILE"))"
199 if blocked "$PROFILE"; then
204 FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
205 if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
207 if [ "$CHANGE_PROFILE" -eq 1 ]; then
208 if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
212 # found the profile, exit with success
219 # we did not find the profile, load default
220 if [ -n "$DEFAULT_PROFILE" ]; then
221 echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
222 load "$DEFAULT_PROFILE"