diff --git a/auto-disper b/auto-disper index 0f8110f..fa9c78a 100755 --- a/auto-disper +++ b/auto-disper @@ -22,6 +22,11 @@ # # To automatically reload your setup, just append --change to the command line # +# To alternate between the detected setup and a default one (such as your laptop +# screen, set DEFAULT_PROFILE below appropriately and use the --toggle command +# line option. This is useful to bind to a keyboard shortcut (via your desktop's +# global shortcuts bindings or similar). +# # To manually load a profile, you can use the --load option. # # To prevent a profile from being loaded, place a script call "block" in its @@ -39,10 +44,12 @@ # or other applications about it. DISPER=/usr/bin/disper +XRANDR=/usr/bin/xrandr PROFILES=~/.auto-disper/ +# 0: no change, 1: switch to detected, 2: toggle between detected and default CHANGE_PROFILE=0 -DEFAULT_PROFILE="" +DEFAULT_PROFILE="laptop" SAVE_PROFILE="" blocked() { @@ -54,9 +61,15 @@ blocked() { load() { local PROFILE="$1" - if [ "$CHANGE_PROFILE" -eq 1 ]; then + if [ "$CHANGE_PROFILE" -ge 1 ]; then echo " -> loading profile $PROFILE" $DISPER -i < "$PROFILES/$PROFILE/config" + #hack to add auto-rotate support (manual config): + if [ -f "$PROFILES/$PROFILE/xrandr-orientation" ]; then + $XRANDR --orientation $(cat "$PROFILES/$PROFILE/xrandr-orientation") + else + $XRANDR --orientation normal + fi [ -x "$PROFILES/$PROFILE/postswitch" ] && \ "$PROFILES/$PROFILE/postswitch" "$PROFILE" @@ -66,7 +79,7 @@ load() { } # process parameters -OPTS=$(getopt -n auto-disper -o s:l:d:c --long change,default:,save:,load: -- "$@") +OPTS=$(getopt -n auto-disper -o s:l:d:cth --long toggle,change,default:,save:,load:,help -- "$@") if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi eval set -- "$OPTS" @@ -76,11 +89,25 @@ while true; do -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;; -s|--save) SAVE_PROFILE="$2"; shift 2 ;; -l|--load) LOAD_PROFILE="$2"; shift 2 ;; + -t|--toggle) CHANGE_PROFILE=2; shift ;; + -h|--help) PRINT_HELP=1; shift ;; --) shift; break ;; *) echo "Error: $1"; exit 1;; esac done +if [ -n "$PRINT_HELP" ]; then + echo "auto-disper. Options:" + echo " -c --change Switch to auto-detected profile" + echo " -d --default Switch to default profile" + echo " -s --save NAME Save current configuration as profile NAME." + echo " -l --load NAME Load profile NAME." + echo " -t --toggle If the default profile is not current, switch to it." + echo "\t\t\tOtherwise, switch to auto-detected profile." + echo " -h --help Print this message." + exit 0 +fi + CURRENT_SETUP="$($DISPER -l | grep '^display ')" if [ -n "$SAVE_PROFILE" ]; then @@ -88,6 +115,11 @@ if [ -n "$SAVE_PROFILE" ]; then mkdir -p "$PROFILES/$SAVE_PROFILE" echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup" $DISPER -p > "$PROFILES/$SAVE_PROFILE/config" + #Note: not really sure if we should search for "default", and this has no multi-display support: + XRANDR_ORIENTATION=$($XRANDR -q | grep default | cut -d ' ' -f4) + if [ "$XRANDR_ORIENTATION" != "(normal" ]; then + echo $XRANDR_ORIENTATION > "$PROFILES/$SAVE_PROFILE/xrandr-orientation" + fi exit 0 fi @@ -111,9 +143,19 @@ for SETUP_FILE in $PROFILES/*/setup; do FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")" if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then echo " (detected)" - load "$PROFILE" - # found the profile, exit with success - exit 0 + if [ "$CHANGE_PROFILE" -eq 1 ]; then + load "$PROFILE" + # found the profile, exit with success + exit 0 + elif [ "$CHANGE_PROFILE" -eq 2 ]; then + ACTIVE_CONFIG="$($DISPER -p)" + FILE_CONFIG="$(cat "$PROFILES/$PROFILE/config")" + if [ "$ACTIVE_CONFIG" != "$FILE_CONFIG" ]; then + load "$PROFILE" + # found the profile, exit with success + exit 0 + fi + fi else echo "" fi