aboutsummaryrefslogtreecommitdiffstats
path: root/bin/lid-switch-action
blob: b5b7f628cd5cde4b2f0459a347ed69798b6b5fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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