Skip to content

Instantly share code, notes, and snippets.

@mjbogusz
Last active March 2, 2017 19:23
Show Gist options
  • Select an option

  • Save mjbogusz/cada2a7189f776f7eafc3aaa31688a69 to your computer and use it in GitHub Desktop.

Select an option

Save mjbogusz/cada2a7189f776f7eafc3aaa31688a69 to your computer and use it in GitHub Desktop.
Automatic screen switching for laptops based on xrandr; Checks for connected screens and laptop lid status.
#!/usr/bin/env zsh
local direction="left"
local -a screens; screens=(`xrandr | grep " connected" | awk -F' ' '{print $1}'`)
local lidStatus=`cat /proc/acpi/button/lid/LID/state | awk -F' ' '{print $2}'`
disableAllButLVDS() {
xrandr --output VGA1 --off
xrandr --output HDMI1 --off
xrandr --output HDMI2 --off
xrandr --output HDMI3 --off
xrandr --output DP1 --off
xrandr --output DP2 --off
xrandr --output DP3 --off
}
disableAll() {
disableAllButLVDS
xrandr --output LVDS1 --off
}
if [[ ${#screens} == 1 ]]; then
print "only internal screen available"
disableAllButLVDS
xrandr --output LVDS1 --auto --dpi 96 --primary
exit
fi
print "lid status: ${lidStatus}"
if [[ ${lidStatus} == 'closed' ]]; then
local -a screens2; screens2=()
for i in ${screens}; do
if [[ ${i} != 'LVDS1' ]]; then
screens2=($screens2 ${i})
fi
done
screens=($screens2)
fi
print "screens (${#screens}): ${screens}"
local lastScreen=""
local primarySet=false
disableAll
for i in ${screens}; do
print "enabling: ${i}"
xrandr --output $i --auto --dpi 96
if [[ $primarySet == false ]]; then
print "\tprimary"
xrandr --output $i --primary
primarySet=true
fi
if [[ -n ${lastScreen} ]]; then
print "\t${direction} of ${lastScreen}"
if [[ $direction == "right" ]]; then
xrandr --output $i --right-of $lastScreen
else
xrandr --output $i --left-of $lastScreen
fi
fi
lastScreen=$i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment