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