]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - autorandr
f68392eb8eb8f50a9977f5c7e2c95e0d5210179e
[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_edid() {
46         $XRANDR -q --verbose | awk '
47         /^[^ ]+ (dis)?connected / { DEV=$1; ID[DEV] = ""; }
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_dim() {
53         $XRANDR -q --verbose | awk '
54         /^[^ ]+ connected / { CONNECTED=1 }
55         /^[^ ]+ disconnected / { CONNECTED=0 }
56         /^[^ ]+ (dis)?connected / \
57         && match($0, "([0-9]+mm) x ([0-9]+mm)", A) {
58                 print $1, (CONNECTED==1 ? A[1]"x"A[2] : "" )
59         }'
60 }
61
62
63 current_cfg() {
64         $XRANDR -q | awk '
65         /^[^ ]+ disconnected / {
66         print "output "$1;
67                 print "off";
68         }
69         /^[^ ]+ connected / {
70                 split($3, A, "+");
71                 print "output "$1;
72                 print "mode "A[1];
73                 print "pos "A[2]"x"A[3];
74         }'
75 }
76
77 blocked() {
78     local PROFILE="$1"
79     [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
80
81     "$PROFILES/$PROFILE/block" "$PROFILE"
82 }
83
84 load() {
85     local PROFILE="$1"
86     if [ "$CHANGE_PROFILE" -eq 1 ]; then
87         echo " -> loading profile $PROFILE"
88         sed 's!^!--!' "$PROFILES/$PROFILE/config" | xargs xrandr
89
90         [ -x "$PROFILES/$PROFILE/postswitch" ] && \
91             "$PROFILES/$PROFILE/postswitch" "$PROFILE"
92         [ -x "$PROFILES/postswitch" ] && \
93             "$PROFILES/postswitch" "$PROFILE"
94     fi
95 }
96
97 # process parameters
98 OPTS=$(getopt -n autorandr -o s:l:d:cf --long change,default:,save:,load:,fingerprint -- "$@")
99 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
100 eval set -- "$OPTS"
101
102 while true; do
103     case "$1" in
104         -c|--change) CHANGE_PROFILE=1; shift ;;
105         -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
106         -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
107         -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
108         --fingerprint) setup_fp_edid; exit 0;;
109         --) shift; break ;;
110         *) echo "Error: $1"; exit 1;;
111     esac
112 done
113
114 CURRENT_SETUP="$(setup_fp_edid)"
115
116 if [ -n "$SAVE_PROFILE" ]; then
117     echo "Saving current configuration as profile '${SAVE_PROFILE}'"
118     mkdir -p "$PROFILES/$SAVE_PROFILE"
119     echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
120     current_cfg > "$PROFILES/$SAVE_PROFILE/config"
121     exit 0
122 fi
123
124 if [ -n "$LOAD_PROFILE" ]; then
125     CHANGE_PROFILE=1 load "$LOAD_PROFILE"
126     exit $?
127 fi
128
129 for SETUP_FILE in $PROFILES/*/setup; do
130     if ! [ -e $SETUP_FILE ]; then
131         break
132     fi
133     PROFILE="$(basename $(dirname "$SETUP_FILE"))"
134     echo -n "$PROFILE"
135
136     if blocked "$PROFILE"; then
137         echo " (blocked)"
138         continue
139     fi
140
141     FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
142     if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
143         echo " (detected)"
144         load "$PROFILE"
145         # found the profile, exit with success
146         exit 0
147     else
148         echo ""
149     fi
150 done
151
152 # we did not find the profile, load default
153 if [ -n "$DEFAULT_PROFILE" ]; then
154     echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
155     load "$DEFAULT_PROFILE"
156 fi
157 exit 1