Skip to content

Instantly share code, notes, and snippets.

@eaydin
Last active August 11, 2020 07:34
Show Gist options
  • Select an option

  • Save eaydin/8dd27d72bc3182bc510c2422fb9a2dcf to your computer and use it in GitHub Desktop.

Select an option

Save eaydin/8dd27d72bc3182bc510c2422fb9a2dcf to your computer and use it in GitHub Desktop.
Autoscreen Switch with Dock
#!/bin/bash
#######
# A **very** dirty hack to switch second and third screen.
# The reasons behind this is that, my Thinkpad dock always assigns random device names
# whenever I connect the Thinkpad to it. Therefore I cannot save the setting and call it back.
# This script has some assumptions:
# * Alternative screen names always start with 'DP-'
# * The laptop's screen name is always 'eDP-1'
# * The laptop is the left-most screen.
# * The 'middle' screen is the primary one.
# * All outputs are in normal rotation and in 1920x1080 resolution.
# * xrandr is available in path
# interestingly, the method below sometimes (mostyl) doesn't detect connected monitors.
# even though they are available, and listed in 'xrandr' output, --listmonitors or --listactivemonitors
# doesn't list anything. under these circumstances, arandr lists the monitors.
# arr=( $(xrandr --listmonitors | grep ' DP-' | awk '{print $4}') )
# instead, we're detecting monitors directly from xrandr's output.
# (I know, the double grep is unnecessary bla bla...)
arr=( $(xrandr | grep " connected " | grep -v "eDP-1" | awk '{print $1}') )
SCREEN0=eDP-1
ALTSCREEN1=${arr[0]}
ALTSCREEN2=${arr[1]}
#echo "ALTSCREEN 1 is $ALTSCREEN1"
#echo "ALTSCREEN 2 is $ALTSCREEN2"
echo 'running setting 1'
sleep 1
xrandr --output $SCREEN0 --mode 1920x1080 --pos 0x0 --rotate normal --output $ALTSCREEN2 --primary --mode 1920x1080 --pos 1920x0 --rotate normal --output $ALTSCREEN1 --mode 1920x1080 --pos 3840x0 --rotate normal
echo "Press space or enter to switch, any other key if you're happy."
read -rsn1 input
#echo "the input is $input"
if [ "$input" = "" ]; then
echo 'switching screens...'
sleep 1
xrandr --output $SCREEN0 --mode 1920x1080 --pos 0x0 --rotate normal --output $ALTSCREEN1 --primary --mode 1920x1080 --pos 1920x0 --rotate normal --output $ALTSCREEN2 --mode 1920x1080 --pos 3840x0 --rotate normal
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment