3 # Automatically select a display configuration based on connected devives
5 # Stefan Tomanek <stefan.tomanek@wertarbyte.de>
7 # requires disper, the command line display switcher:
8 # http://willem.engen.nl/projects/disper/
13 # Save your current display configuration and setup with
14 # auto-disper --save mobil
16 # Connect an additional display, configure your setup and save it
17 # auto-disper --save docked
19 # Now auto-disper can detect which hardware setup is active: # auto-disper
23 # To automatically reload your setup, just append --change to the command line
25 # To manually load a profile, you can use the --load <profile> option.
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 DISPER=/usr/bin/disper
42 PROFILES=~/.auto-disper/
50 [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
52 "$PROFILES/$PROFILE/block" "$PROFILE"
57 if [ "$CHANGE_PROFILE" -eq 1 ]; then
58 echo " -> loading profile $PROFILE"
59 $DISPER -i < "$PROFILES/$PROFILE/config"
61 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
62 "$PROFILES/$PROFILE/postswitch" "$PROFILE"
63 [ -x "$PROFILES/postswitch" ] && \
64 "$PROFILES/postswitch" "$PROFILE"
69 OPTS=$(getopt -n auto-disper -o s:l:d:c --long change,default:,save:,load: -- "$@")
70 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
75 -c|--change) CHANGE_PROFILE=1; shift ;;
76 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
77 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
78 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
80 *) echo "Error: $1"; exit 1;;
84 CURRENT_SETUP="$($DISPER -l | grep '^display ')"
86 if [ -n "$SAVE_PROFILE" ]; then
87 echo "Saving current configuration as profile '${SAVE_PROFILE}'"
88 mkdir -p "$PROFILES/$SAVE_PROFILE"
89 echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
90 $DISPER -p > "$PROFILES/$SAVE_PROFILE/config"
94 if [ -n "$LOAD_PROFILE" ]; then
95 CHANGE_PROFILE=1 load "$LOAD_PROFILE"
99 for SETUP_FILE in $PROFILES/*/setup; do
100 if ! [ -e $SETUP_FILE ]; then
103 PROFILE="$(basename $(dirname "$SETUP_FILE"))"
106 if blocked "$PROFILE"; then
111 FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
112 if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
115 # found the profile, exit with success
122 # we did not find the profile, load default
123 if [ -n "$DEFAULT_PROFILE" ]; then
124 echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
125 load "$DEFAULT_PROFILE"