]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
avoid (re)loading of identical configurations
[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 action [profile-name]
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 --force                 force loading of a profile
140 --fingerprint           fingerprints your actual config 
141
142  To prevent a profile from being loaded, place a script call "block" in its
143  directory. The script is evaluated before the screen setup is inspected, and
144  in case of it returning a value of 0 the profile is skipped. This can be used
145  to query the status of a docking station you are about to leave.
146
147  If no suitable profile can be identified, the current configuration is kept.
148  To change this behaviour and switch to a fallback configuration, specify
149  --default <profile>
150
151  Another script called "postswitch "can be placed in the directory
152  ~/.auto-disper as well as in all profile directories: The scripts are
153  executed after a mode switch has taken place and can notify window managers
154
155 EOH
156         exit
157 }
158 # process parameters
159 OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,help -- "$@")
160 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
161 eval set -- "$OPTS"
162
163 while true; do
164         case "$1" in
165                 -c|--change) CHANGE_PROFILE=1; shift ;;
166                 -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
167                 -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
168                 -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
169                 -h|--help) help ;; 
170                 --force) FORCE_LOAD=1; shift ;;
171                 --fingerprint) setup_fp; exit 0;;
172                 --) shift; break ;;
173                 *) echo "Error: $1"; exit 1;;
174         esac
175 done
176
177 CURRENT_SETUP="$(setup_fp)"
178
179 if [ -n "$SAVE_PROFILE" ]; then
180         echo "Saving current configuration as profile '${SAVE_PROFILE}'"
181         mkdir -p "$PROFILES/$SAVE_PROFILE"
182         echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
183         current_cfg > "$PROFILES/$SAVE_PROFILE/config"
184         exit 0
185 fi
186
187 if [ -n "$LOAD_PROFILE" ]; then
188         CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
189         exit $?
190 fi
191
192 for SETUP_FILE in $PROFILES/*/setup; do
193         if ! [ -e $SETUP_FILE ]; then
194                 break
195         fi
196         PROFILE="$(basename $(dirname "$SETUP_FILE"))"
197         echo -n "$PROFILE"
198
199         if blocked "$PROFILE"; then
200                 echo " (blocked)"
201                 continue
202         fi
203
204         FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
205         if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
206                 echo " (detected)"
207                 if [ "$CHANGE_PROFILE" -eq 1 ]; then
208                         if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
209                                 load "$PROFILE"
210                         fi
211                 fi
212                 # found the profile, exit with success
213                 exit 0
214         else
215                 echo ""
216         fi
217 done
218
219 # we did not find the profile, load default
220 if [ -n "$DEFAULT_PROFILE" ]; then
221         echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
222         load "$DEFAULT_PROFILE"
223 fi
224 exit 1