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 # $ 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 defult, 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 PROFILES=~/.autorandr/
50 CONFIG=~/.autorandr.conf
57 FP_METHODS="setup_fp_xrandr_edid setup_fp_sysfs_edid"
58 CURRENT_CFG_METHOD="current_cfg_xrandr"
59 LOAD_METHOD="load_cfg_xrandr"
61 SCRIPTNAME="$(basename $0)"
62 # when called as autodisper/auto-disper, we assume different defaults
63 if [ "$SCRIPTNAME" = "auto-disper" ] || [ "$SCRIPTNAME" = "autodisper" ]; then
64 echo "Assuming disper defaults..." >&2
65 FP_METHODS="setup_fp_disper"
66 CURRENT_CFG_METHOD="current_cfg_disper"
67 LOAD_METHOD="load_cfg_disper"
70 if [ -f $CONFIG ]; then
71 echo "Loading configuration from '$CONFIG'" >&2
75 setup_fp_xrandr_edid() {
76 $XRANDR -q --verbose | awk '
77 /^[^ ]+ (dis)?connected / { DEV=$1; }
78 $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
79 END { for (X in ID) { print X " " ID[X]; } }'
82 setup_fp_sysfs_edid() {
83 # xrandr triggers the reloading of EDID data
84 $XRANDR -q > /dev/null
85 # hash the EDIDs of all _connected_ devices
86 for P in /sys/class/drm/card*-*/; do
88 [ ! -d "$P" ] && continue
89 if grep -q "^connected$" < "${P}status"; then
90 echo -n "$(basename "$P") "
91 md5sum ${P}edid | awk '{print $1}'
97 $DISPER -l | grep '^display '
102 for M in $FP_METHODS; do
104 if [ -n "$FP" ]; then
108 if [ -z "$FP" ]; then
109 echo "Unable to fingerprint display configuration" >&2
115 current_cfg_xrandr() {
117 # display is connected and has a mode
118 /^[^ ]+ connected [^(]/ {
122 print "pos "A[2]"x"A[3];
125 # disconnected or disabled displays
126 /^[^ ]+ (dis)?connected / {
133 current_cfg_disper() {
143 [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
145 "$PROFILES/$PROFILE/block" "$PROFILE"
150 if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
151 echo "Config already loaded"
159 sed 's!^!--!' "$1" | xargs $XRANDR
168 local CONF="$PROFILES/$PROFILE/config"
169 if [ -e "$CONF" ] ; then
170 echo " -> loading profile $PROFILE"
173 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
174 "$PROFILES/$PROFILE/postswitch" "$PROFILE"
175 [ -x "$PROFILES/postswitch" ] && \
176 "$PROFILES/postswitch" "$PROFILE"
182 Usage: $SCRIPTNAME [options]
184 -h, --help get this small help
185 -c, --change reload current setup
186 -s, --save <profile> save your current setup to profile <profile>
187 -l, --load <profile> load profile <profile>
188 -d, --default <profile> make profile <profile> the default profile
189 --force force (re)loading of a profile
190 --fingerprint fingerprint your current hardware setup
192 To prevent a profile from being loaded, place a script call "block" in its
193 directory. The script is evaluated before the screen setup is inspected, and
194 in case of it returning a value of 0 the profile is skipped. This can be used
195 to query the status of a docking station you are about to leave.
197 If no suitable profile can be identified, the current configuration is kept.
198 To change this behaviour and switch to a fallback configuration, specify
201 Another script called "postswitch "can be placed in the directory
202 ~/.autorandr as well as in any profile directories: The scripts are executed
203 after a mode switch has taken place and can notify window managers.
205 When called by the name "autodisper" or "auto-disper", the script uses "disper"
206 instead of "xrandr" to detect, configure and save the display configuration.
212 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,help -- "$@")
213 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
218 -c|--change) CHANGE_PROFILE=1; shift ;;
219 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
220 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
221 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
223 --force) FORCE_LOAD=1; shift ;;
224 --fingerprint) setup_fp; exit 0;;
226 *) echo "Error: $1"; exit 1;;
230 CURRENT_SETUP="$(setup_fp)"
232 if [ -n "$SAVE_PROFILE" ]; then
233 echo "Saving current configuration as profile '${SAVE_PROFILE}'"
234 mkdir -p "$PROFILES/$SAVE_PROFILE"
235 echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
236 $CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
240 if [ -n "$LOAD_PROFILE" ]; then
241 CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
245 for SETUP_FILE in $PROFILES/*/setup; do
246 if ! [ -e $SETUP_FILE ]; then
249 PROFILE="$(basename $(dirname "$SETUP_FILE"))"
252 if blocked "$PROFILE"; then
257 FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
258 if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
260 if [ "$CHANGE_PROFILE" -eq 1 ]; then
261 if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
265 # found the profile, exit with success
272 # we did not find the profile, load default
273 if [ -n "$DEFAULT_PROFILE" ]; then
274 echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
275 load "$DEFAULT_PROFILE"