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