aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbin/lid-switch-action47
1 files changed, 47 insertions, 0 deletions
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
+