]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - auto-disper
initial commit
[deb_pkgs/autorandr.git] / auto-disper
1 #!/bin/sh
2 #
3 # Automatically select a display configuration based on connected devives
4 #
5 # Stefan Tomanek <stefan.tomanek@wertarbyte.de>
6 #
7 # requires disper, the command line display switcher:
8 # http://willem.engen.nl/projects/disper/
9 #
10 #
11 # How to use:
12 #
13 # Save your current display configuration and setup with
14 # auto-disper --save mobil
15 #
16 # Connect an additional display, configure your setup and save it
17 # auto-disper --save docked
18 #
19 # Now auto-disper can detect which hardware setup is active: # auto-disper
20 # mobile
21 # docked (detected)
22 #
23 # To automatically reload your setup, just append --change to the command line
24 #
25 # To manually load a profile, you can use the --load <profile> option.
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 DISPER=/usr/bin/disper
42 PROFILES=~/.auto-disper/
43
44 CHANGE_PROFILE=0
45 DEFAULT_PROFILE=""
46 SAVE_PROFILE=""
47
48 blocked() {
49     local PROFILE="$1"
50     [ ! -x "$PROFILES/$PROFILE/block" ] && return 1
51
52     "$PROFILES/$PROFILE/block"  "$PROFILE"
53 }
54
55 load() {
56     local PROFILE="$1"
57     if [ "$CHANGE_PROFILE" -eq 1 ]; then
58         echo " -> loading profile $PROFILE"
59         $DISPER -i < "$PROFILES/$PROFILE/config"
60
61         [ -x "$PROFILES/$PROFILE/postswitch" ] && \
62             "$PROFILES/$PROFILE/postswitch" "$PROFILE"
63         [ -x "$PROFILES/postswitch" ] && \
64             "$PROFILES/postswitch" "$PROFILE"
65     fi
66 }
67
68 # process parameters
69 OPTS=$(getopt -n auto-disper -o s:l:d:c --long change,default:,save:,load: -- "$@")
70 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
71 eval set -- "$OPTS"
72
73 while true; do
74     case "$1" in
75         -c|--change) CHANGE_PROFILE=1; shift ;;
76         -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
77         -s|--save) SAVE_PROFILE="$2"; shift 2 ;;
78         -l|--load) LOAD_PROFILE="$2"; shift 2 ;;
79         --) shift; break ;;
80         *) echo "Error: $1"; exit 1;;
81     esac
82 done
83
84 CURRENT_SETUP="$($DISPER -l | grep '^display ')"
85
86 if [ -n "$SAVE_PROFILE" ]; then
87     echo "Saving current configuration as profile '${SAVE_PROFILE}'"
88     mkdir -p "$PROFILES/$SAVE_PROFILE"
89     echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
90     $DISPER -p > "$PROFILES/$SAVE_PROFILE/config"
91     exit 0
92 fi
93
94 if [ -n "$LOAD_PROFILE" ]; then
95     CHANGE_PROFILE=1 load "$LOAD_PROFILE"
96     exit $?
97 fi
98
99 for SETUP_FILE in $PROFILES/*/setup; do
100     if ! [ -e $SETUP_FILE ]; then
101         break
102     fi
103     PROFILE="$(basename $(dirname "$SETUP_FILE"))"
104     echo -n "$PROFILE"
105
106     if blocked "$PROFILE"; then
107         echo " (blocked)"
108         continue
109     fi
110
111     FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
112     if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
113         echo " (detected)"
114         load "$PROFILE"
115         # found the profile, exit with success
116         exit 0
117     else
118         echo ""
119     fi
120 done
121
122 # we did not find the profile, load default
123 if [ -n "$DEFAULT_PROFILE" ]; then
124     echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
125     load "$DEFAULT_PROFILE"
126 fi
127 exit 1