Created
December 21, 2016 14:39
-
-
Save cyphunk/7a4b3798adbc5f5d9e26a9c8558efdd5 to your computer and use it in GitHub Desktop.
xrandr wrapper for basic external monitor management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
# Script to setup external display. Really on meant for setups with | |
# a laptop display and no more than ONE External display. | |
# import usage_self() from humanism.sh | |
if ! type usage_self >/dev/null 2>&1 ; then | |
source $HOME/git/humanism.sh/humanism.sh usage_self | |
fi | |
ACTIVE=$(xrandr | grep -E " connected (primary )?[1-9]+" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/") | |
CONNECTED=$(xrandr | grep " connected " | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/") | |
LAPTOP=$(echo "$ACTIVE" | head -1) | |
OTHER=$(echo "$CONNECTED" | grep -v "$LAPTOP") | |
case "$1" in | |
list) | |
# List available display ports and connection status | |
xrandr --query | \ | |
egrep 'connected|disconnected' | \ | |
awk '{print $1" "$2}' | \ | |
sed 's/ connected/ connected <--/' | \ | |
egrep --color '\S* ' | |
;; | |
connect) | |
# Setup external display args: [left|right|above|below|mirror] [port] | |
# default possition assumed left. | |
# default port assumed to be first connected monitor other than laptop lcd | |
# eg. | |
# $0 left | |
# $0 VGA1 right | |
# $0 mirror DP1 | |
#echo -e "ACTIVE:\n$ACTIVE\nCONNECTED:\n$CONNECTED\n" | |
#echo -e "LAPTOP:$LAPTOP OTHER:$OTHER\n" | |
if [ -z "$OTHER" ]; then | |
echo -e "No external connected\nAvailable:" | |
$0 list | |
exit 1 | |
fi | |
PORT=$(echo "$OTHER" | tail -1) | |
POS="--left-of $LAPTOP" | |
for arg in "${@:2}"; do | |
case "$arg" in | |
right) POS="--right-of $LAPTOP" ;; | |
above) POS="--above $LAPTOP" ;; | |
below) POS="--below $LAPTOP" ;; | |
mirror) POS="" ;; | |
left) ;; | |
*) | |
if echo "$CONNECTED" | grep -q "$arg"; then | |
PORT="$arg" | |
else | |
echo "Could not find connected display named \"$arg\"" | |
$0 list | |
exit 1 | |
fi | |
;; | |
esac | |
done | |
echo "Connecting \"$PORT\" at \"$POS\"" | |
echo xrandr --output $PORT --auto $POS | |
xrandr --output $PORT --auto $POS | |
;; | |
disconnect) | |
# Disconnect display. | |
#xrandr --auto | |
PORT=$(echo "$OTHER" | head -1) | |
xrandr --output $PORT --off | |
;; | |
*) | |
usage_self | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment