]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
detect primary screen and save to config.
[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                 print "output "$1;
131                 print "off";
132                 next;
133         }'
134 }
135
136 current_cfg_disper() {
137         $DISPER -p
138 }
139
140 current_cfg() {
141         $CURRENT_CFG_METHOD;
142 }
143
144 blocked() {
145         local PROFILE="$1"
146         [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
147
148         "$PROFILES/$PROFILE/block" "$PROFILE"
149 }
150
151 config_equal() {
152         local PROFILE="$1"
153         if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
154                 echo "Config already loaded"
155                 return 0
156         else
157                 return 1
158         fi
159 }
160
161 load_cfg_xrandr() {
162         sed 's!^!--!' "$1" | xargs $XRANDR
163 }
164
165 load_cfg_disper() {
166         $DISPER -i < "$1"
167 }
168
169 load() {
170         local PROFILE="$1"
171         local CONF="$PROFILES/$PROFILE/config"
172         if [ -e "$CONF" ] ; then
173                 echo " -> loading profile $PROFILE"
174                 $LOAD_METHOD "$CONF"
175
176                 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
177                         "$PROFILES/$PROFILE/postswitch" "$PROFILE"
178                 [ -x "$PROFILES/postswitch" ] && \
179                         "$PROFILES/postswitch" "$PROFILE"
180         fi
181 }
182
183 help() {
184         cat <<EOH
185 Usage: $SCRIPTNAME [options]
186
187 -h, --help              get this small help
188 -c, --change            reload current setup
189 -s, --save <profile>    save your current setup to profile <profile>
190 -l, --load <profile>    load profile <profile>
191 -d, --default <profile> make profile <profile> the default profile 
192 --force                 force (re)loading of a profile
193 --fingerprint           fingerprint your current hardware setup
194
195  To prevent a profile from being loaded, place a script call "block" in its
196  directory. The script is evaluated before the screen setup is inspected, and
197  in case of it returning a value of 0 the profile is skipped. This can be used
198  to query the status of a docking station you are about to leave.
199
200  If no suitable profile can be identified, the current configuration is kept.
201  To change this behaviour and switch to a fallback configuration, specify
202  --default <profile>.
203
204  Another script called "postswitch "can be placed in the directory
205  ~/.autorandr as well as in any profile directories: The scripts are executed
206  after a mode switch has taken place and can notify window managers.
207
208  When called by the name "autodisper" or "auto-disper", the script uses "disper"
209  instead of "xrandr" to detect, configure and save the display configuration.
210
211 EOH
212         exit
213 }
214 # process parameters
215 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,help -- "$@")
216 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
217 eval set -- "$OPTS"
218
219 while true; do
220         case "$1" in
221                 -c|--change) CHANGE_PROFILE=1; shift ;;
222                 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
223                 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
224                 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
225                 -h|--help) help ;; 
226                 --force) FORCE_LOAD=1; shift ;;
227                 --fingerprint) setup_fp; exit 0;;
228                 --) shift; break ;;
229                 *) echo "Error: $1"; exit 1;;
230         esac
231 done
232
233 CURRENT_SETUP="$(setup_fp)"
234
235 if [ -n "$SAVE_PROFILE" ]; then
236         echo "Saving current configuration as profile '${SAVE_PROFILE}'"
237         mkdir -p "$PROFILES/$SAVE_PROFILE"
238         echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
239         $CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
240         exit 0
241 fi
242
243 if [ -n "$LOAD_PROFILE" ]; then
244         CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
245         exit $?
246 fi
247
248 for SETUP_FILE in $PROFILES/*/setup; do
249         if ! [ -e $SETUP_FILE ]; then
250                 break
251         fi
252         PROFILE="$(basename $(dirname "$SETUP_FILE"))"
253         echo -n "$PROFILE"
254
255         if blocked "$PROFILE"; then
256                 echo " (blocked)"
257                 continue
258         fi
259
260         FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
261         if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
262                 echo " (detected)"
263                 if [ "$CHANGE_PROFILE" -eq 1 ]; then
264                         if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
265                                 load "$PROFILE"
266                         fi
267                 fi
268                 # found the profile, exit with success
269                 exit 0
270         else
271                 echo ""
272         fi
273 done
274
275 # we did not find the profile, load default
276 if [ -n "$DEFAULT_PROFILE" ]; then
277         echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
278         load "$DEFAULT_PROFILE"
279 fi
280 exit 1