]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
Output "same-as" in the "common" virtual configuration
[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                 if (($4 == "left") || ($4 == "right")) {
136                         split(A[1], B, "x");
137                         A[1] = B[2]"x"B[1];
138                 }
139                 print "mode "A[1];
140                 print "pos "A[2]"x"A[3];
141                 if ($4 !~ /^\(/) {
142                         print "rotate "$4;
143                 }
144                 else {
145                         print "rotate normal";
146                 }
147                 next;
148         }
149         # disconnected or disabled displays
150         /^[^ ]+ (dis)?connected / ||
151         /^[^ ]+ unknown connection / {
152                 print "output "$1;
153                 print "off";
154                 next;
155         }'
156 }
157
158 current_cfg_disper() {
159         $DISPER -p
160 }
161
162 common_cfg_xrandr() {
163         $XRANDR -q | awk '
164         # variables:
165         #   output: current output
166         #   outputlist: space sep list of all outputs
167         #   outputarr: array of all connected outputs
168         #   outputarrsize: number of connected outputs
169         #   modelist[800x600]: space sep list of outputs supporting mode
170         # display is connected
171         /^[^ ]+ connected / {
172             output=$1;
173             outputlist=outputlist " " output
174             outputarr[outputarrsize++]=output
175         }
176         # disconnected or disabled displays
177         /^[^ ]+ disconnected / ||
178         /^[^ ]+ unknown connection / {
179             print "output " $1;
180             print "off";
181         }
182         # modes available on a screen
183         /^   [0-9]+x[0-9]+/ {
184             modelist[$1]=modelist[$1] " " output
185         }
186         END {
187             # find common mode with largest screen area
188             for (m in modelist) {
189                 if (modelist[m] == outputlist) {
190                     # calculate area of resolution
191                     split(m, wh, "x");
192                     if (wh[1]*wh[2] >= maxdim) {
193                         maxdim=wh[1]*wh[2]
194                         maxmode=m
195                     }
196                 }
197             }
198             if (maxmode) {
199                 for (i in outputarr) {
200                     print "output " outputarr[i];
201                     print "mode " maxmode;
202                     print "pos 0x0";
203                     if (i > 0) {
204                         print "same-as " outputarr[0]
205                     }
206                 }
207             }
208         }' \
209                 | load_cfg_xrandr -
210 }
211
212 current_cfg() {
213         $CURRENT_CFG_METHOD;
214 }
215
216 blocked() {
217         local PROFILE="$1"
218         [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
219
220         "$PROFILES/$PROFILE/block" "$PROFILE"
221 }
222
223 config_equal() {
224         local PROFILE="$1"
225         if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
226                 echo "Config already loaded"
227                 return 0
228         else
229                 return 1
230         fi
231 }
232
233 load_cfg_xrandr() {
234         # sed 1: Prefix arguments with "--"
235         # sed 2: Merge arguments into one line per output
236         # sed 3: Merge into two lines, all --off outputs in the first one
237         sed 's/^/--/' "$1" | sed -e '
238                 :START
239                 /\n--output/{P;D}
240                 s/\n/ /
241                 N;bSTART' | sed -e '
242                         ### First line
243                         / --off/{
244                                 G
245                                 # Merge if next line contains --off
246                                 s/\n\([^\n]* --off\)/ \1/
247                                 h
248                                 $!d;b
249                         }
250                         ### Last line
251                         H;x
252                         # Merge if previous line contains --mode
253                         s/\(--mode [^\n]*\)\n/\1 /
254                         h
255                         $!d' | xargs -L 1 $XRANDR
256 }
257
258 load_cfg_disper() {
259         $DISPER -i < "$1"
260 }
261
262 load() {
263         local PROFILE="$1"
264         local CONF="$PROFILES/$PROFILE/config"
265         [ -f "$CONF" -o "$PROFILE" = "common" ] || return 1
266         if [ -x "$PROFILES/preswitch" ]; then
267                 "$PROFILES/preswitch" "$PROFILE"
268         fi
269         if [ -x "$PROFILES/$PROFILE/preswitch" ]; then
270                 "$PROFILES/$PROFILE/preswitch" "$PROFILE"
271         fi
272
273         if [ $PROFILE = "common" ]; then
274                 echo " -> setting largest common mode"
275                 common_cfg_xrandr
276         else
277                 echo " -> loading profile $PROFILE"
278                 $LOAD_METHOD "$CONF"
279         fi
280
281         if [ -x "$PROFILES/$PROFILE/postswitch" ]; then
282                 "$PROFILES/$PROFILE/postswitch" "$PROFILE"
283         fi
284         if [ -x "$PROFILES/postswitch" ]; then
285                 "$PROFILES/postswitch" "$PROFILE"
286         fi
287 }
288
289 help() {
290         cat <<EOH
291 Usage: $SCRIPTNAME [options]
292
293 -h, --help              get this small help
294 -c, --change            reload current setup
295 -s, --save <profile>    save your current setup to profile <profile>
296 -l, --load <profile>    load profile <profile>
297 -d, --default <profile> make profile <profile> the default profile
298 --force                 force (re)loading of a profile
299 --fingerprint           fingerprint your current hardware setup
300 --config                dump your current xrandr setup
301
302  To prevent a profile from being loaded, place a script call "block" in its
303  directory. The script is evaluated before the screen setup is inspected, and
304  in case of it returning a value of 0 the profile is skipped. This can be used
305  to query the status of a docking station you are about to leave.
306
307  If no suitable profile can be identified, the current configuration is kept.
308  To change this behaviour and switch to a fallback configuration, specify
309  --default <profile>.
310
311  Another script called "postswitch "can be placed in the directory
312  ~/.autorandr as well as in any profile directories: The scripts are executed
313  after a mode switch has taken place and can notify window managers.
314
315  When called by the name "autodisper" or "auto-disper", the script uses "disper"
316  instead of "xrandr" to detect, configure and save the display configuration.
317
318 EOH
319         exit
320 }
321 # process parameters
322 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,config,help -- "$@")
323 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
324 eval set -- "$OPTS"
325
326 while true; do
327         case "$1" in
328                 -c|--change) CHANGE_PROFILE=1; shift ;;
329                 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
330                 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
331                 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
332                 -h|--help) help ;;
333                 --force) FORCE_LOAD=1; shift ;;
334                 --fingerprint) setup_fp; exit 0;;
335                 --config) current_cfg; exit 0;;
336                 --) shift; break ;;
337                 *) echo "Error: $1"; exit 1;;
338         esac
339 done
340
341 CURRENT_SETUP="$(setup_fp)"
342
343 if [ -n "$SAVE_PROFILE" ]; then
344         echo "Saving current configuration as profile '${SAVE_PROFILE}'"
345         mkdir -p "$PROFILES/$SAVE_PROFILE"
346         echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
347         $CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
348         exit 0
349 fi
350
351 if [ -n "$LOAD_PROFILE" ]; then
352         CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
353         exit $?
354 fi
355
356 for SETUP_FILE in $PROFILES/*/setup; do
357         if ! [ -e $SETUP_FILE ]; then
358                 break
359         fi
360         PROFILE="$(basename $(dirname "$SETUP_FILE"))"
361         echo -n "$PROFILE"
362
363         if blocked "$PROFILE"; then
364                 echo " (blocked)"
365                 continue
366         fi
367
368         FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
369         if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
370                 echo " (detected)"
371                 if [ "$CHANGE_PROFILE" -eq 1 ]; then
372                         if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
373                                 load "$PROFILE"
374                         fi
375                 fi
376                 # found the profile, exit with success
377                 exit 0
378         else
379                 echo ""
380         fi
381 done
382
383 # we did not find the profile, load default
384 if [ -n "$DEFAULT_PROFILE" ]; then
385         echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
386         load "$DEFAULT_PROFILE"
387 fi
388 exit 1
389
390 # Local Variables:
391 # tab-width: 8
392 # sh-basic-offset: 8
393 # sh-indentation: 8
394 # indent-tabs-mode: t
395 # End: