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