]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
6d1f91f2fe08a349415c336a0b108d15a8f10882
[deb_pkgs/autorandr.git] / autorandr
1 #!/bin/sh
2 #
3 # Automatically select a display configuration based on connected devives
4 #
5 # Stefan Tomanek <stefan.tomanek@wertarbyte.de>
6 #
7 # How to use:
8 #
9 # Save your current display configuration and setup with:
10 #  $ autorandr --save mobile
11 #
12 # Connect an additional display, configure your setup and save it:
13 #  $ autorandr --save docked
14 #
15 # Now autorandr can detect which hardware setup is active:
16 #  $ autorandr
17 #    mobile
18 #    docked (detected)
19 #
20 # To automatically reload your setup, just append --change to the command line
21 #
22 # To manually load a profile, you can use the --load <profile> option.
23 #
24 # autorandr tries to avoid reloading an identical configuration. To force the
25 # (re)configuration, apply --force.
26 #
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.
31 #
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
34 # --default <profile>
35 #
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.
40 #
41 #
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.
46
47 XRANDR=/usr/bin/xrandr
48 DISPER=/usr/bin/disper
49 PROFILES=~/.autorandr/
50 CONFIG=~/.autorandr.conf
51
52 CHANGE_PROFILE=0
53 FORCE_LOAD=0
54 DEFAULT_PROFILE=""
55 SAVE_PROFILE=""
56
57 FP_METHODS="setup_fp_xrandr_edid setup_fp_sysfs_edid"
58 CURRENT_CFG_METHOD="current_cfg_xrandr"
59 LOAD_METHOD="load_cfg_xrandr"
60
61 SCRIPTNAME="$(basename $0)"
62 # when called as autodisper/auto-disper, we assume different defaults
63 if [ "$SCRIPTNAME" = "auto-disper" ] || [ "$SCRIPTNAME" = "autodisper" ]; then
64         FP_METHODS="setup_fp_disper"
65         CURRENT_CFG_METHOD="current_cfg_disper"
66         LOAD_METHOD="load_cfg_disper"
67 fi
68
69 test -f $CONFIG && . $CONFIG 
70
71 setup_fp_xrandr_edid() {
72         $XRANDR -q --verbose | awk '
73         /^[^ ]+ (dis)?connected / { DEV=$1; }
74         $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
75         END { for (X in ID) { print X " " ID[X]; } }'
76 }
77
78 setup_fp_sysfs_edid() {
79         # xrandr triggers the reloading of EDID data
80         $XRANDR -q > /dev/null
81         # hash the EDIDs of all _connected_ devices
82         for P in /sys/class/drm/card*-*/; do
83                 if grep -q "^connected$" < "${P}status"; then
84                         echo -n "$(basename "$P") "
85                         md5sum ${P}edid | awk '{print $1}'
86                 fi
87         done
88 }
89
90 setup_fp_disper() {
91         $DISPER -l | grep '^display '
92 }
93
94 setup_fp() {
95         local FP="";
96         for M in $FP_METHODS; do
97                 FP="$($M)"
98                 [ -n "$FP" ] && break;
99         done
100         if [ -z "$FP" ]; then
101                 echo "Unable to fingerprint display configuration" >&2
102                 return
103         fi
104         echo "$FP"
105 }
106
107 current_cfg_xrandr() {
108         $XRANDR -q | awk '
109         /^[^ ]+ disconnected / {
110         print "output "$1;
111                 print "off";
112         }
113         /^[^ ]+ connected / {
114                 split($3, A, "+");
115                 print "output "$1;
116                 print "mode "A[1];
117                 print "pos "A[2]"x"A[3];
118         }'
119 }
120
121 current_cfg_disper() {
122         $DISPER -p
123 }
124
125 current_cfg() {
126         $CURRENT_METHOD;
127 }
128
129 blocked() {
130         local PROFILE="$1"
131         [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
132
133         "$PROFILES/$PROFILE/block" "$PROFILE"
134 }
135
136 config_equal() {
137         local PROFILE="$1"
138         if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
139                 echo "Config already loaded"
140                 return 0
141         else
142                 return 1
143         fi
144 }
145
146 load_cfg_xrandr() {
147         sed 's!^!--!' "$1" | xargs $XRANDR
148 }
149
150 load_cfg_disper() {
151         $DISPER < -i < "$1"
152 }
153
154 load() {
155         local PROFILE="$1"
156         local CONF="$PROFILES/$PROFILE/config"
157         if [ -e "$CONF" ] ; then
158                 echo " -> loading profile $PROFILE"
159                 $LOAD_METHOD "$CONF"
160
161                 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
162                         "$PROFILES/$PROFILE/postswitch" "$PROFILE"
163                 [ -x "$PROFILES/postswitch" ] && \
164                         "$PROFILES/postswitch" "$PROFILE"
165         fi
166 }
167
168 help() {
169         cat <<EOH
170 Usage: $SCRIPTNAME [options]
171
172 -h, --help              get this small help
173 -c, --change            reload current setup
174 -s, --save <profile>    save your current setup to profile <profile>
175 -l, --load <profile>    load profile <profile>
176 -d, --default <profile> make profile <profile> the default profile 
177 --force                 force (re)loading of a profile
178 --fingerprint           fingerprint your current hardware setup
179
180  To prevent a profile from being loaded, place a script call "block" in its
181  directory. The script is evaluated before the screen setup is inspected, and
182  in case of it returning a value of 0 the profile is skipped. This can be used
183  to query the status of a docking station you are about to leave.
184
185  If no suitable profile can be identified, the current configuration is kept.
186  To change this behaviour and switch to a fallback configuration, specify
187  --default <profile>.
188
189  Another script called "postswitch "can be placed in the directory
190  ~/.autorandr as well as in any profile directories: The scripts are executed
191  after a mode switch has taken place and can notify window managers.
192
193  When called by the name "autodisper" or "auto-disper", the script uses "disper"
194  instead of "xrandr" to detect, configure and save the display configuration.
195
196 EOH
197         exit
198 }
199 # process parameters
200 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,help -- "$@")
201 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
202 eval set -- "$OPTS"
203
204 while true; do
205         case "$1" in
206                 -c|--change) CHANGE_PROFILE=1; shift ;;
207                 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
208                 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
209                 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
210                 -h|--help) help ;; 
211                 --force) FORCE_LOAD=1; shift ;;
212                 --fingerprint) setup_fp; exit 0;;
213                 --) shift; break ;;
214                 *) echo "Error: $1"; exit 1;;
215         esac
216 done
217
218 CURRENT_SETUP="$(setup_fp)"
219
220 if [ -n "$SAVE_PROFILE" ]; then
221         echo "Saving current configuration as profile '${SAVE_PROFILE}'"
222         mkdir -p "$PROFILES/$SAVE_PROFILE"
223         echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
224         current_cfg > "$PROFILES/$SAVE_PROFILE/config"
225         exit 0
226 fi
227
228 if [ -n "$LOAD_PROFILE" ]; then
229         CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
230         exit $?
231 fi
232
233 for SETUP_FILE in $PROFILES/*/setup; do
234         if ! [ -e $SETUP_FILE ]; then
235                 break
236         fi
237         PROFILE="$(basename $(dirname "$SETUP_FILE"))"
238         echo -n "$PROFILE"
239
240         if blocked "$PROFILE"; then
241                 echo " (blocked)"
242                 continue
243         fi
244
245         FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
246         if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
247                 echo " (detected)"
248                 if [ "$CHANGE_PROFILE" -eq 1 ]; then
249                         if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
250                                 load "$PROFILE"
251                         fi
252                 fi
253                 # found the profile, exit with success
254                 exit 0
255         else
256                 echo ""
257         fi
258 done
259
260 # we did not find the profile, load default
261 if [ -n "$DEFAULT_PROFILE" ]; then
262         echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
263         load "$DEFAULT_PROFILE"
264 fi
265 exit 1