Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added screen-button script #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions auto-disper
Original file line number Diff line number Diff line change
Expand Up @@ -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 <profile> option.
#
# To prevent a profile from being loaded, place a script call "block" in its
Expand All @@ -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() {
Expand All @@ -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"
Expand All @@ -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"

Expand All @@ -76,18 +89,37 @@ 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
echo "Saving current configuration as profile '${SAVE_PROFILE}'"
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

Expand All @@ -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
Expand Down