From d5019b72b1882e9d68e4224208b52c7628e7606a Mon Sep 17 00:00:00 2001 From: David Runge Date: Wed, 19 Feb 2014 19:48:26 +0100 Subject: Adding lid-switch-action script, working in conjunction with acpi event button/lid open/close for setting up screens or suspending on closing of laptop lid --- bin/lid-switch-action | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 bin/lid-switch-action diff --git a/bin/lid-switch-action b/bin/lid-switch-action new file mode 100755 index 0000000..1a22015 --- /dev/null +++ b/bin/lid-switch-action @@ -0,0 +1,47 @@ +#!/bin/bash +# What action to take on closing the laptop lid, +# depending on whether a screen is attached or not + +# 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 +HDMI=$(containsElement "HDMI1" "${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 or HDMI1 is connected + if [[ ${#connectedOutputs[@]} -gt 1 || $HDMI ]];then + echo "More than one screen connected, or HDMI1 connected" + echo "Not suspending, disabling eDP1, making HDMI1 primary screen." + autorandr -l external-docked + else + echo "Only one screen connected" + echo "Suspending." + 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 ]];then + echo "More than one screen connected, or HDMI1 connected" + echo "Setting up all connected screens." + autorandr -l multi-docked + fi +fi + -- cgit v1.2.3-70-g09d2