]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
Handle outputs with "unknown connection" as "off".
[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         echo "Assuming disper defaults..." >&2
65         FP_METHODS="setup_fp_disper"
66         CURRENT_CFG_METHOD="current_cfg_disper"
67         LOAD_METHOD="load_cfg_disper"
68 fi
69
70 if [ -f $CONFIG ]; then
71         echo "Loading configuration from '$CONFIG'" >&2
72         . $CONFIG
73 fi
74
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]; } }'
80 }
81
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
87                 # nothing found
88                 [ ! -d "$P" ] && continue
89                 if grep -q "^connected$" < "${P}status"; then
90                         echo -n "$(basename "$P") "
91                         md5sum ${P}edid | awk '{print $1}'
92                 fi
93         done
94 }
95
96 setup_fp_disper() {
97         $DISPER -l | grep '^display '
98 }
99
100 setup_fp() {
101         local FP="";
102         for M in $FP_METHODS; do
103                 FP="$($M)"
104                 if [ -n "$FP" ]; then
105                         break
106                 fi
107         done
108         if [ -z "$FP" ]; then
109                 echo "Unable to fingerprint display configuration" >&2
110                 return
111         fi
112         echo "$FP"
113 }
114
115 current_cfg_xrandr() {
116         local PRIMARY_SETUP=$(xdpyinfo -ext XINERAMA|awk '/^  head #0:/ {printf $3 $5}')
117         $XRANDR -q | awk -v primary_setup=${PRIMARY_SETUP} '
118         # display is connected and has a mode
119         /^[^ ]+ connected [^(]/ {
120                 split($3, A, "+");
121                 print "output "$1;
122                 print "mode "A[1];
123                 print "pos "A[2]"x"A[3];
124                 if (A[1] A[2] "," A[3] == primary_setup)
125                         print "primary";
126                 next;
127         }
128         # disconnected or disabled displays
129         /^[^ ]+ (dis)?connected / ||
130         /^[^ ]+ unknown connection / {
131                 print "output "$1;
132                 print "off";
133                 next;
134         }'
135 }
136
137 current_cfg_disper() {
138         $DISPER -p
139 }
140
141 current_cfg() {
142         $CURRENT_CFG_METHOD;
143 }
144
145 blocked() {
146         local PROFILE="$1"
147         [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
148
149         "$PROFILES/$PROFILE/block" "$PROFILE"
150 }
151
152 config_equal() {
153         local PROFILE="$1"
154         if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
155                 echo "Config already loaded"
156                 return 0
157         else
158                 return 1
159         fi
160 }
161
162 load_cfg_xrandr() {
163         sed 's!^!--!' "$1" | xargs $XRANDR
164 }
165
166 load_cfg_disper() {
167         $DISPER -i < "$1"
168 }
169
170 load() {
171         local PROFILE="$1"
172         local CONF="$PROFILES/$PROFILE/config"
173         if [ -e "$CONF" ] ; then
174                 echo " -> loading profile $PROFILE"
175                 $LOAD_METHOD "$CONF"
176
177                 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
178                         "$PROFILES/$PROFILE/postswitch" "$PROFILE"
179                 [ -x "$PROFILES/postswitch" ] && \
180                         "$PROFILES/postswitch" "$PROFILE"
181         fi
182 }
183
184 help() {
185         cat <<EOH
186 Usage: $SCRIPTNAME [options]
187
188 -h, --help              get this small help
189 -c, --change            reload current setup
190 -s, --save <profile>    save your current setup to profile <profile>
191 -l, --load <profile>    load profile <profile>
192 -d, --default <profile> make profile <profile> the default profile 
193 --force                 force (re)loading of a profile
194 --fingerprint           fingerprint your current hardware setup
195
196  To prevent a profile from being loaded, place a script call "block" in its
197  directory. The script is evaluated before the screen setup is inspected, and
198  in case of it returning a value of 0 the profile is skipped. This can be used
199  to query the status of a docking station you are about to leave.
200
201  If no suitable profile can be identified, the current configuration is kept.
202  To change this behaviour and switch to a fallback configuration, specify
203  --default <profile>.
204
205  Another script called "postswitch "can be placed in the directory
206  ~/.autorandr as well as in any profile directories: The scripts are executed
207  after a mode switch has taken place and can notify window managers.
208
209  When called by the name "autodisper" or "auto-disper", the script uses "disper"
210  instead of "xrandr" to detect, configure and save the display configuration.
211
212 EOH
213         exit
214 }
215 # process parameters
216 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,help -- "$@")
217 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
218 eval set -- "$OPTS"
219
220 while true; do
221         case "$1" in
222                 -c|--change) CHANGE_PROFILE=1; shift ;;
223                 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
224                 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
225                 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
226                 -h|--help) help ;; 
227                 --force) FORCE_LOAD=1; shift ;;
228                 --fingerprint) setup_fp; exit 0;;
229                 --) shift; break ;;
230                 *) echo "Error: $1"; exit 1;;
231         esac
232 done
233
234 CURRENT_SETUP="$(setup_fp)"
235
236 if [ -n "$SAVE_PROFILE" ]; then
237         echo "Saving current configuration as profile '${SAVE_PROFILE}'"
238         mkdir -p "$PROFILES/$SAVE_PROFILE"
239         echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
240         $CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
241         exit 0
242 fi
243
244 if [ -n "$LOAD_PROFILE" ]; then
245         CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
246         exit $?
247 fi
248
249 for SETUP_FILE in $PROFILES/*/setup; do
250         if ! [ -e $SETUP_FILE ]; then
251                 break
252         fi
253         PROFILE="$(basename $(dirname "$SETUP_FILE"))"
254         echo -n "$PROFILE"
255
256         if blocked "$PROFILE"; then
257                 echo " (blocked)"
258                 continue
259         fi
260
261         FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
262         if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
263                 echo " (detected)"
264                 if [ "$CHANGE_PROFILE" -eq 1 ]; then
265                         if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
266                                 load "$PROFILE"
267                         fi
268                 fi
269                 # found the profile, exit with success
270                 exit 0
271         else
272                 echo ""
273         fi
274 done
275
276 # we did not find the profile, load default
277 if [ -n "$DEFAULT_PROFILE" ]; then
278         echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
279         load "$DEFAULT_PROFILE"
280 fi
281 exit 1