]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
Merge branch 'feature/preswitch_scripts' of http://github.com/torrancew/autorandr
[deb_pkgs/autorandr.git] / autorandr
1 #!/bin/sh
2 #
3 # Automatically select a display configuration based on connected devices
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 default, 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 XDPYINFO=/usr/bin/xdpyinfo
50 PROFILES=~/.autorandr/
51 CONFIG=~/.autorandr.conf
52
53 CHANGE_PROFILE=0
54 FORCE_LOAD=0
55 DEFAULT_PROFILE=""
56 SAVE_PROFILE=""
57
58 FP_METHODS="setup_fp_sysfs_edid setup_fp_xrandr_edid"
59 CURRENT_CFG_METHOD="current_cfg_xrandr"
60 LOAD_METHOD="load_cfg_xrandr"
61
62 SCRIPTNAME="$(basename $0)"
63 # when called as autodisper/auto-disper, we assume different defaults
64 if [ "$SCRIPTNAME" = "auto-disper" ] || [ "$SCRIPTNAME" = "autodisper" ]; then
65         echo "Assuming disper defaults..." >&2
66         FP_METHODS="setup_fp_disper"
67         CURRENT_CFG_METHOD="current_cfg_disper"
68         LOAD_METHOD="load_cfg_disper"
69 fi
70
71 if [ -f $CONFIG ]; then
72         echo "Loading configuration from '$CONFIG'" >&2
73         . $CONFIG
74 fi
75
76 setup_fp_xrandr_edid() {
77         $XRANDR -q --verbose | awk '
78         /^[^ ]+ (dis)?connected / { DEV=$1; }
79         $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
80         END { for (X in ID) { print X " " ID[X]; } }'
81 }
82
83 setup_fp_sysfs_edid() {
84         # xrandr triggers the reloading of EDID data
85         $XRANDR -q > /dev/null
86         # hash the EDIDs of all _connected_ devices
87         for P in /sys/class/drm/card*-*/; do
88                 # nothing found
89                 [ ! -d "$P" ] && continue
90                 if grep -q "^connected$" < "${P}status"; then
91                         echo -n "$(basename "$P") "
92                         md5sum ${P}edid | awk '{print $1}'
93                 fi
94         done
95 }
96
97 setup_fp_disper() {
98         $DISPER -l | grep '^display '
99 }
100
101 setup_fp() {
102         local FP="";
103         for M in $FP_METHODS; do
104                 FP="$($M)"
105                 if [ -n "$FP" ]; then
106                         break
107                 fi
108         done
109         if [ -z "$FP" ]; then
110                 echo "Unable to fingerprint display configuration" >&2
111                 return
112         fi
113         echo "$FP"
114 }
115
116 current_cfg_xrandr() {
117         local PRIMARY_SETUP="";
118         if [ -x "$XDPYINFO" ]; then
119                 PRIMARY_SETUP="$($XDPYINFO -ext XINERAMA | awk '/^  head #0:/ {printf $3 $5}')"
120         fi
121         $XRANDR -q | awk -v primary_setup="${PRIMARY_SETUP}" '
122         # display is connected and has a mode
123         /^[^ ]+ connected [^(]/ {
124                 print "output "$1;
125                 if ($3 == "primary") {
126                         print $3
127                         split($4, A, "+")
128                         $4=$5
129                 }
130                 else {
131                         split($3, A, "+");
132                         if (A[1] A[2] "," A[3] == primary_setup)
133                                 print "primary";
134                 }
135                 print "mode "A[1];
136                 print "pos "A[2]"x"A[3];
137                 if ($4 !~ /^\(/) {
138                         print "rotate "$4;
139                 }
140                 next;
141         }
142         # disconnected or disabled displays
143         /^[^ ]+ (dis)?connected / ||
144         /^[^ ]+ unknown connection / {
145                 print "output "$1;
146                 print "off";
147                 next;
148         }'
149 }
150
151 current_cfg_disper() {
152         $DISPER -p
153 }
154
155 current_cfg() {
156         $CURRENT_CFG_METHOD;
157 }
158
159 blocked() {
160         local PROFILE="$1"
161         [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
162
163         "$PROFILES/$PROFILE/block" "$PROFILE"
164 }
165
166 config_equal() {
167         local PROFILE="$1"
168         if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
169                 echo "Config already loaded"
170                 return 0
171         else
172                 return 1
173         fi
174 }
175
176 load_cfg_xrandr() {
177         # sed 1: Prefix arguments with "--"
178         # sed 2: Merge arguments into one line per output
179         # sed 3: Merge into two lines, all --off outputs in the first one
180         sed 's/^/--/' "$1" | sed -e '
181                 :START
182                 /\n--output/{P;D}
183                 s/\n/ /
184                 N;bSTART' | sed -e '
185                         ### First line
186                         / --off/{
187                                 G
188                                 # Merge if next line contains --off
189                                 s/\n\([^\n]* --off\)/ \1/
190                                 h
191                                 $!d;b
192                         }
193                         ### Last line
194                         H;x
195                         # Merge if previous line contains --mode
196                         s/\(--mode [^\n]*\)\n/\1 /
197                         h
198                         $!d' | xargs -L 1 $XRANDR
199 }
200
201 load_cfg_disper() {
202         $DISPER -i < "$1"
203 }
204
205 load() {
206         local PROFILE="$1"
207         local CONF="$PROFILES/$PROFILE/config"
208         if [ -e "$CONF" ] ; then
209     [ -x "$PROFILES/preswitch" ] && \
210       "$PROFILES/preswitch" "$PROFILE"
211     [ -x "$PROFILES/$PROFILE/preswitch" ] && \
212       "$PROFILES/$PROFILE/preswitch" "$PROFILE"
213
214                 echo " -> loading profile $PROFILE"
215                 $LOAD_METHOD "$CONF"
216
217                 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
218                         "$PROFILES/$PROFILE/postswitch" "$PROFILE"
219                 [ -x "$PROFILES/postswitch" ] && \
220                         "$PROFILES/postswitch" "$PROFILE"
221         fi
222 }
223
224 help() {
225         cat <<EOH
226 Usage: $SCRIPTNAME [options]
227
228 -h, --help              get this small help
229 -c, --change            reload current setup
230 -s, --save <profile>    save your current setup to profile <profile>
231 -l, --load <profile>    load profile <profile>
232 -d, --default <profile> make profile <profile> the default profile 
233 --force                 force (re)loading of a profile
234 --fingerprint           fingerprint your current hardware setup
235 --config                dump your current xrandr setup
236
237  To prevent a profile from being loaded, place a script call "block" in its
238  directory. The script is evaluated before the screen setup is inspected, and
239  in case of it returning a value of 0 the profile is skipped. This can be used
240  to query the status of a docking station you are about to leave.
241
242  If no suitable profile can be identified, the current configuration is kept.
243  To change this behaviour and switch to a fallback configuration, specify
244  --default <profile>.
245
246  Another script called "postswitch "can be placed in the directory
247  ~/.autorandr as well as in any profile directories: The scripts are executed
248  after a mode switch has taken place and can notify window managers.
249
250  When called by the name "autodisper" or "auto-disper", the script uses "disper"
251  instead of "xrandr" to detect, configure and save the display configuration.
252
253 EOH
254         exit
255 }
256 # process parameters
257 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,config,help -- "$@")
258 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
259 eval set -- "$OPTS"
260
261 while true; do
262         case "$1" in
263                 -c|--change) CHANGE_PROFILE=1; shift ;;
264                 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
265                 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
266                 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
267                 -h|--help) help ;; 
268                 --force) FORCE_LOAD=1; shift ;;
269                 --fingerprint) setup_fp; exit 0;;
270                 --config) current_cfg; exit 0;;
271                 --) shift; break ;;
272                 *) echo "Error: $1"; exit 1;;
273         esac
274 done
275
276 CURRENT_SETUP="$(setup_fp)"
277
278 if [ -n "$SAVE_PROFILE" ]; then
279         echo "Saving current configuration as profile '${SAVE_PROFILE}'"
280         mkdir -p "$PROFILES/$SAVE_PROFILE"
281         echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
282         $CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
283         exit 0
284 fi
285
286 if [ -n "$LOAD_PROFILE" ]; then
287         CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
288         exit $?
289 fi
290
291 for SETUP_FILE in $PROFILES/*/setup; do
292         if ! [ -e $SETUP_FILE ]; then
293                 break
294         fi
295         PROFILE="$(basename $(dirname "$SETUP_FILE"))"
296         echo -n "$PROFILE"
297
298         if blocked "$PROFILE"; then
299                 echo " (blocked)"
300                 continue
301         fi
302
303         FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
304         if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
305                 echo " (detected)"
306                 if [ "$CHANGE_PROFILE" -eq 1 ]; then
307                         if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
308                                 load "$PROFILE"
309                         fi
310                 fi
311                 # found the profile, exit with success
312                 exit 0
313         else
314                 echo ""
315         fi
316 done
317
318 # we did not find the profile, load default
319 if [ -n "$DEFAULT_PROFILE" ]; then
320         echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
321         load "$DEFAULT_PROFILE"
322 fi
323 exit 1