aboutsummaryrefslogtreecommitdiffstats
path: root/bin/lid-switch-action
blob: f543d8692e352c4b061069d5ec1c62b79ebf9df7 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/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
  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" >> $log_file
        su dave -c "/home/dave/.config/awesome/i3lock"
      fi
      echo "Suspending." >> $log_file
      systemctl suspend
    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