Last active
April 6, 2020 19:05
-
-
Save kanehekili/fc4a0446a50b626185c4f1a242d0013d to your computer and use it in GitHub Desktop.
bash script to set multiple monitors with xrandr (generic approach)
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
#!/bin/bash | |
# simple script that either connects or disconnects the external monitor | |
# the prime output is the local (laptop) screen | |
# the secondary is either discovered,but not connected or disconnected but not removed (screen size too big) | |
oldifs="$IFS" | |
IFS=$'\n' | |
export DISPLAY=:0.0 | |
#sleep 2 | |
stuff="$(/usr/bin/xrandr -q)" | |
echo "($stuff)" | |
#sometimes not primary??? | |
#primeOutput=($(xrandr | grep -E " connected [1-9]+" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")) | |
#either 'connected primary 1440.. or connected primary (normal | |
primeOutput=$(echo "$stuff" | grep -E " connected primary [1-9(]+" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/") | |
if [[ -z $primeOutput ]]; then | |
#we can't check for the real primary monitor if mirrored | |
#primeOutput=$(echo "$stuff" | grep -E " connected [1-9]+[0-9x]+[+][0]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/") | |
monitors=$(echo "$stuff" | grep -E " connected [1-9]+" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/") | |
monArray=($monitors) | |
primeOutput="${monArray[0]}" | |
secondaryAttached="${monArray[1]}" | |
echo "fallback" | |
else | |
secondaryAttached=($(echo "$stuff" | grep -E " connected [0-9]+x"| sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")) | |
fi | |
#secondaryAttached=$(echo "$stuff" | grep -E " connected [0-9x]+[0-9x]+[+][1-9]"| sed -e "s/\([A-Z0-9]\+\) connected.*/\1/") | |
secondaryFound=($(echo "$stuff" | grep " connected (" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")) | |
#secondaryDisonnected=$(echo "$stuff" | grep -E " disconnected [1-9]+" | sed -e "s/\([A-Z0-9]\+\) disconnected.*/\1/") | |
secondaryDisconnected=($(echo "$stuff" | grep -E " disconnected " | sed -e "s/\([A-Z0-9]\+\) disconnected.*/\1/")) | |
echo "primary: ${primeOutput}" | |
echo "External discovered: ${#secondaryFound[@]}= "${secondaryFound[*]} | |
echo "External already attached: ${#secondaryAttached[@]} = "${secondaryAttached[*]} | |
echo "External to disconnect: ${#secondaryDisconnected[@]} = "${secondaryDisconnected[*]} | |
logger "Monitors: ${primeOutput} EXT: pending:"${secondaryFound[*]}" connected:"${secondaryAttached[*]}" disconnect:"${secondaryDisconnected[*]} | |
if [[ -n $secondaryFound ]]; then | |
for (( i=0; i<${#secondaryFound[@]}; i++ )) do | |
echo "Connecting ${secondaryFound[i]}" | |
xrandr --output $primeOutput --auto --primary --output ${secondaryFound[i]} --auto --right-of $primeOutput | |
done | |
fi | |
if [[ -n $secondaryDisconnected ]]; then | |
for (( i=0; i<${#secondaryDisconnected[@]}; i++ )) do | |
echo "Disconnecting ${secondaryDisconnected[i]} " | |
xrandr --output $primeOutput --auto --output ${secondaryDisconnected[i]} --off | |
done | |
fi | |
if [[ -n $secondaryAttached ]]; then | |
for (( i=0; i<${#secondaryAttached[@]}; i++ )) do | |
echo "enforce layout to attached: ${secondaryAttached[i]}" | |
xrandr --output $primeOutput --auto --primary --output ${secondaryAttached[i]} --auto --right-of $primeOutput | |
done | |
fi | |
IFS="$oldifs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment