#!/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 # Define the log file log_file="/home/dave/.log/lid-switch-action.log" if [[ ! -w "$log_file" ]];then touch "$log_file" chown dave:users "$log_file" fi echo "Calling lid-switch-action with $1" >> $log_file # Unsetting all possible connections HDMI1=0 HDMI2=0 VGA1=0 DP1=0 DP2=0 # Store $1 as it will be overwritten soon call=$1 # while reading xrandr line by line, check its output connections=0 while read line do # if line contains connected but not disconnected, use it if [[ "$line" == *connected* && "$line" != *disconnected* ]];then xrandr_output[$connections]=$line set -- ${xrandr_output[$connections]} connected[$connections]=$1 (( connections++ )) fi done < <(xrandr) # Loop all connected displays for i in ${connected[@]} do case $i in "HDMI1") HDMI1=1 ;; "HDMI2") HDMI2=1 ;; "VGA1") VGA1=1 ;; "DP1") DP1=1 ;; "DP2") DP2=1 ;; esac done # Now checking which autorandr profile to use case "$call" in "0") if [[ $connections -gt 1 ]];then # check if more than one connection if [[ $HDMI1 -gt 0 ]];then echo "HDMI1 connected." >> $log_file /usr/bin/autorandr -l external-plugged & >> $log_file elif [[ $DP2 -gt 0 ]];then echo "DP2 connected." >> $log_file /usr/bin/autorandr -l external-docked & >> $log_file fi else # else suspend echo "Only one screen connected." >> $log_file if [[ -f "/home/dave/.config/awesome/i3lock" ]];then echo "Using i3lock to lock screen." >> $log_file su dave -c "/home/dave/.config/awesome/i3lock" fi echo "Suspending." >> $log_file systemctl suspend fi ;; "1") if [[ $connections -gt 1 ]];then # check if more than one connection if [[ $HDMI1 -gt 0 ]];then echo "HDMI1 connected." >> $log_file /usr/bin/autorandr -l multi-plugged & >> $log_file elif [[ $DP2 -gt 0 ]];then echo "DP2 connected." >> $log_file /usr/bin/autorandr -l multi-docked & >> $log_file fi fi ;; esac