Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raphaelbruno/0235e92a79c35e2a2bac082fd11ffe1c to your computer and use it in GitHub Desktop.
Save raphaelbruno/0235e92a79c35e2a2bac082fd11ffe1c to your computer and use it in GitHub Desktop.
Cinnamon Toggle Projection Between Displays (xrand)
#!/bin/bash
# This script will toggle between (in this order): Laptop Mode, Projector Mode, Extended Mode and Mirrored Mode
RESOLUTIONS=("1366x768" "2560x1080")
EXTENDED_MODE_PRIMARY_INDEX=1
EXTENDED_MODE_PRIMARY_POSITION=1366x-120
MIRRORED_MODE_PRIMARY_INDEX=0
# List of connected monitors
CONNECTED_MONITORS=`xrandr | grep -P ' connected' | grep -o '^[^ ]*'`
set -- "$CONNECTED_MONITORS"
declare -a LIST_MONITORS=($*)
AMOUNT_MONITORS=${#LIST_MONITORS[*]}
# List of active monitors
ACTIVE_MONITORS=`xrandr --listmonitors | grep -oP "[a-zA-Z]*-[0-9]$"`
set -- "$ACTIVE_MONITORS"
declare -a LIST_ACTIVE_MONITORS=($*)
AMOUNT_ACTIVE_MONITORS=${#LIST_ACTIVE_MONITORS[*]}
# List of mirrored monitors
MIRRORED_MONITORS=`xrandr --listmonitors | grep "0+0"`
set -- "$MIRRORED_MONITORS"
declare -a LIST_MIRRORED_MONITORS=($*)
AMOUNT_MIRRORED_MONITORS=${#LIST_MIRRORED_MONITORS[*]}
PRIMARY=${LIST_MONITORS[0]}
gsettings set org.cinnamon.desktop.background picture-options "stretched"
# Logic to toggle between modes
if [ $AMOUNT_MONITORS -gt 1 ] && [ $AMOUNT_ACTIVE_MONITORS -gt 1 ] && [ $AMOUNT_MIRRORED_MONITORS -le 1 ]
then
# Mirrored Mode
PRIMARY=${LIST_MONITORS[$MIRRORED_MODE_PRIMARY_INDEX]}
xrandr --output ${LIST_MONITORS[0]} --off --output ${LIST_MONITORS[1]} --off
xrandr --output ${LIST_MONITORS[0]} --auto --mode ${RESOLUTIONS[0]} --pos 0x0
xrandr --output ${LIST_MONITORS[1]} --auto --mode ${RESOLUTIONS[1]} --pos 0x0
xrandr --output ${LIST_MONITORS[((1-MIRRORED_MODE_PRIMARY_INDEX))]} --scale-from ${RESOLUTIONS[MIRRORED_MODE_PRIMARY_INDEX]}
echo "Mirror Mode: ${LIST_MONITORS[*]} (${RESOLUTIONS[MIRRORED_MODE_PRIMARY_INDEX]}) Primary: $MIRRORED_MODE_PRIMARY_INDEX"
elif [ $AMOUNT_MONITORS -gt 1 ] && [ "${LIST_ACTIVE_MONITORS[*]}" = "${LIST_MONITORS[1]}" ]
then
# Extended Mode
PRIMARY=${LIST_MONITORS[$EXTENDED_MODE_PRIMARY_INDEX]}
xrandr --output ${LIST_MONITORS[0]} --off --output ${LIST_MONITORS[1]} --off
xrandr --output ${LIST_MONITORS[0]} --auto --mode ${RESOLUTIONS[0]}
xrandr --output ${LIST_MONITORS[1]} --auto --mode ${RESOLUTIONS[1]}
xrandr --output ${LIST_MONITORS[$EXTENDED_MODE_PRIMARY_INDEX]} --pos $EXTENDED_MODE_PRIMARY_POSITION
echo "Extended Mode: ${LIST_MONITORS[*]} (${RESOLUTIONS[0]} ${RESOLUTIONS[1]}) Primary: $EXTENDED_MODE_PRIMARY_INDEX"
elif [ $AMOUNT_MONITORS -gt 1 ] && [ "${LIST_ACTIVE_MONITORS[*]}" = "${LIST_MONITORS[0]}" ]
then
# Projector Mode
PRIMARY=${LIST_MONITORS[1]}
xrandr --output ${LIST_MONITORS[0]} --off --output ${LIST_MONITORS[1]} --off
xrandr --output ${LIST_MONITORS[1]} --auto --mode ${RESOLUTIONS[1]}
echo "Projector Mode: ${LIST_MONITORS[1]} (${RESOLUTIONS[1]})"
else
# Laptop Mode
PRIMARY=${LIST_MONITORS[0]}
if [ $AMOUNT_MONITORS -gt 1 ]
then
xrandr --output ${LIST_MONITORS[1]} --off
fi
xrandr --output ${LIST_MONITORS[0]} --off
xrandr --output ${LIST_MONITORS[0]} --auto --mode ${RESOLUTIONS[0]}
echo "Laptop Mode: ${LIST_MONITORS[0]} (${RESOLUTIONS[0]})"
fi
xrandr --output ${PRIMARY} --primary
gsettings set org.cinnamon.desktop.background picture-options "zoom"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment