aboutsummaryrefslogtreecommitdiffstats
path: root/bin/lid-switch-action
diff options
context:
space:
mode:
Diffstat (limited to 'bin/lid-switch-action')
-rwxr-xr-xbin/lid-switch-action126
1 files changed, 86 insertions, 40 deletions
diff --git a/bin/lid-switch-action b/bin/lid-switch-action
index b5b7f62..f543d86 100755
--- a/bin/lid-switch-action
+++ b/bin/lid-switch-action
@@ -14,48 +14,94 @@ 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."
+# 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
+ echo "Display connected: $i" >> $log_file
+ 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")
+ echo "Called to close">> $log_file
+ if [[ $connections -gt 1 ]];then
+ 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
+ echo "Only one screen connected" >> $log_file
if [[ -f "/home/dave/.config/awesome/i3lock" ]];then
- echo "Using i3lock"
+ echo "Using i3lock" >> $log_file
su dave -c "/home/dave/.config/awesome/i3lock"
fi
+ echo "Suspending." >> $log_file
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
+ fi
+ ;;
+ "1")
+ echo "Called to open">> $log_file
+ if [[ $connections -gt 1 ]];then
+ 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