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