#!/bin/bash # What action to take on closing the laptop lid, # depending on whether a screen is attached or not. # This implies setting HandleLidSwitch=ignore in /etc/systemd/logind.conf # Call this script from /etc/acpi/handler.sh (or other properly set up script # for handling button/lid acpi event) with parameter 0 and 1 for close and open respectively export DISPLAY=:0 export XAUTHORITY=/home/dave/.Xauthority # Check whether first parameter is a number re='^[0-9]+$' if ! [[ $1 =~ $re ]]; then echo "error: Not a number" >&2; exit 1 fi # function for checking if array contains given string containsElement () { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done return 1 } # Get info from xrandr IFS=$'\r\n' connectedOutputs=($(xrandr | grep " connected" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")) # TODO: Add checks for other ports #Check for HDMI1 connection HDMI1=$(containsElement "HDMI1" "${connectedOutputs}") HDMI2=$(containsElement "HDMI2" "${connectedOutputs}") VGA1=$(containsElement "VGA1" "${connectedOutputs}") DP1=$(containsElement "DP1" "${connectedOutputs}") DP2=$(containsElement "DP2" "${connectedOutputs}") # Suspend or switch screen setup, depending on how many screens are connected # If the lid was closed if [ $1 = 0 ];then # Check if more than one screen is connected if [[ ${#connectedOutputs[@]} -gt 1 || $HDMI1 || $HDMI2 || $VGA1 || $DP1 || $DP2 ]];then echo "More than one screen connected." echo "Not suspending, disabling eDP1, making other screen primary." autorandr -l external-docked else echo "Only one screen connected" echo "Suspending." if [[ -f "/home/dave/.config/awesome/i3lock" ]];then echo "Using i3lock" su dave -c "/home/dave/.config/awesome/i3lock" fi systemctl suspend fi # If the lid was opened elif [ $1 = 1 ];then # Check if more than one screen or HDMI1 is connected if [[ ${#connectedOutputs[@]} -gt 1 || $HDMI || $HDMI2 || $VGA1 || $DP1 || $DP2 ]];then echo "More than one screen connected, or HDMI1 connected" echo "Setting up all connected screens." autorandr -l multi-docked fi fi