Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically configure display to detect and use external monitor, if connected.
#!/bin/bash
# Author: Pushpak Dagade
# This script automatically detects if an external monitor is connected,
# and if so, expands my screen with the monitor on the left and my
# laptop screen on the right. If my laptop lid is shut & an external
# monitor is connected, this script uses only the external monitor as
# the display.
#
# TODOS:
# When external monitor is connected, automatically find out its maximum
# supported resolution and set it.
# As of now, it is hard coded to be 1600x900
export DISPLAY=:0 ## notify-send involves GUI
#notify-send "testing as user $USER"
xrandr | grep VGA | grep " connected "
if [ $? -eq 0 ]; then
# External monitor is connected
# If laptop lid open: Turn on both screens
# If laptop lid closed: Turn only external monitor screen
grep -q closed /proc/acpi/button/lid/LID/state
if [ $? = 0 ]
then
# lid is closed
xrandr --output VGA1 --mode 1600x900 --pos 0x0 --output LVDS1 --off --output HDMI1 --off
notify-send 'Laptop lid state: Closed' 'Only external monitor turned ON' -i info
else
# lid is open
xrandr --output VGA1 --mode 1600x900 --pos 0x0 --output LVDS1 --mode 1366x768 --pos 1600x0 --output HDMI1 --off
notify-send 'Laptop lid state: Open' 'Both screens are now turned ON. \nLEFT :\tExternal Monitor \nRIGHT :\tLaptop Screen' -i info
fi
if [ $? -ne 0 ]; then
# Something went wrong. Autoconfigure the internal monitor and disable the external one
xrandr --output LVDS1 --mode auto --output VGA --off
fi
else
# External monitor is not connected
notify-send 'No external monitor connected' 'Only laptop screen turned ON.' -i info
xrandr --output LVDS1 --mode 1366x768 --output VGA1 --off --output HDMI1 --off
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment