]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
adjust help message
[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 # autorand --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 # 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 # ~/.auto-disper as well as in all profile directories: The scripts are
38 # executed after a mode switch has taken place and can notify window managers
39 # or other applications about it.
40
41 XRANDR=/usr/bin/xrandr
42 PROFILES=~/.autorandr/
43 CONFIG=~/.autorandr.conf
44
45 CHANGE_PROFILE=0
46 FORCE_LOAD=0
47 DEFAULT_PROFILE=""
48 SAVE_PROFILE=""
49
50 FP_METHODS="setup_fp_xrandr_edid setup_fp_sysfs_edid"
51
52 test -f $CONFIG && . $CONFIG 
53
54 setup_fp_xrandr_edid() {
55         $XRANDR -q --verbose | awk '
56         /^[^ ]+ (dis)?connected / { DEV=$1; }
57         $1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
58         END { for (X in ID) { print X " " ID[X]; } }'
59 }
60
61 setup_fp_sysfs_edid() {
62         # xrandr triggers the reloading of EDID data
63         $XRANDR -q > /dev/null
64         # hash the EDIDs of all _connected_ devices
65         for P in /sys/class/drm/card*-*/; do
66                 if grep -q "^connected$" < "${P}status"; then
67                         echo -n "$(basename "$P") "
68                         md5sum ${P}edid | awk '{print $1}'
69                 fi
70         done
71 }
72
73 setup_fp() {
74         local FP="";
75         for M in $FP_METHODS; do
76                 FP="$($M)"
77                 [ -n "$FP" ] && break;
78         done
79         if [ -z "$FP" ]; then
80                 echo "Unable to fingerprint display configuration" >&2
81                 return
82         fi
83         echo "$FP"
84 }
85
86
87 current_cfg() {
88         $XRANDR -q | awk '
89         /^[^ ]+ disconnected / {
90         print "output "$1;
91                 print "off";
92         }
93         /^[^ ]+ connected / {
94                 split($3, A, "+");
95                 print "output "$1;
96                 print "mode "A[1];
97                 print "pos "A[2]"x"A[3];
98         }'
99 }
100
101 blocked() {
102         local PROFILE="$1"
103         [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
104
105         "$PROFILES/$PROFILE/block" "$PROFILE"
106 }
107
108 config_equal() {
109         local PROFILE="$1"
110         if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
111                 echo "Config already loaded"
112                 return 0
113         else
114                 return 1
115         fi
116 }
117
118 load() {
119         local PROFILE="$1"
120         if [ -e "$PROFILES/$PROFILE/config" ] ; then
121                 echo " -> loading profile $PROFILE"
122                 sed 's!^!--!' "$PROFILES/$PROFILE/config" | xargs xrandr
123
124                 [ -x "$PROFILES/$PROFILE/postswitch" ] && \
125                         "$PROFILES/$PROFILE/postswitch" "$PROFILE"
126                 [ -x "$PROFILES/postswitch" ] && \
127                         "$PROFILES/postswitch" "$PROFILE"
128         fi
129 }
130
131 help() {
132         cat <<EOH
133 Usage: autorandr [options]
134
135 -h, --help              get this small help
136 -c, --change            reload current setup
137 -s, --save <profile>    save your current setup to profile <profile>
138 -l, --load <profile>    load profile <profile>
139 -d, --default <profile> make profile <profile> the default profile 
140 --force                 force (re)loading of a profile
141 --fingerprint           fingerprint your current hardware setup
142
143  To prevent a profile from being loaded, place a script call "block" in its
144  directory. The script is evaluated before the screen setup is inspected, and
145  in case of it returning a value of 0 the profile is skipped. This can be used
146  to query the status of a docking station you are about to leave.
147
148  If no suitable profile can be identified, the current configuration is kept.
149  To change this behaviour and switch to a fallback configuration, specify
150  --default <profile>.
151
152  Another script called "postswitch "can be placed in the directory
153  ~/.auto-disper as well as in any profile directories: The scripts are executed
154  after a mode switch has taken place and can notify window managers.
155
156 EOH
157         exit
158 }
159 # process parameters
160 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,help -- "$@")
161 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
162 eval set -- "$OPTS"
163
164 while true; do
165         case "$1" in
166                 -c|--change) CHANGE_PROFILE=1; shift ;;
167                 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
168                 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
169                 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
170                 -h|--help) help ;; 
171                 --force) FORCE_LOAD=1; shift ;;
172                 --fingerprint) setup_fp; exit 0;;
173                 --) shift; break ;;
174                 *) echo "Error: $1"; exit 1;;
175         esac
176 done
177
178 CURRENT_SETUP="$(setup_fp)"
179
180 if [ -n "$SAVE_PROFILE" ]; then
181         echo "Saving current configuration as profile '${SAVE_PROFILE}'"
182         mkdir -p "$PROFILES/$SAVE_PROFILE"
183         echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
184         current_cfg > "$PROFILES/$SAVE_PROFILE/config"
185         exit 0
186 fi
187
188 if [ -n "$LOAD_PROFILE" ]; then
189         CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
190         exit $?
191 fi
192
193 for SETUP_FILE in $PROFILES/*/setup; do
194         if ! [ -e $SETUP_FILE ]; then
195                 break
196         fi
197         PROFILE="$(basename $(dirname "$SETUP_FILE"))"
198         echo -n "$PROFILE"
199
200         if blocked "$PROFILE"; then
201                 echo " (blocked)"
202                 continue
203         fi
204
205         FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
206         if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
207                 echo " (detected)"
208                 if [ "$CHANGE_PROFILE" -eq 1 ]; then
209                         if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
210                                 load "$PROFILE"
211                         fi
212                 fi
213                 # found the profile, exit with success
214                 exit 0
215         else
216                 echo ""
217         fi
218 done
219
220 # we did not find the profile, load default
221 if [ -n "$DEFAULT_PROFILE" ]; then
222         echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
223         load "$DEFAULT_PROFILE"
224 fi
225 exit 1