aboutsummarylogtreecommitdiffstats
path: root/wakeup-triggers.sh
blob: 5a5c6e75d55cb949926b0846af6ec5fe2db04343 (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
#!/usr/bin/bash

# This is a script to disable all wakeup triggers but the power button (PWRB)
# Based on the workaround provided for the problem described on Arch Wiki
# https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate#Instantaneous_wakeups_from_suspend
#
# @author Sávio Carlos Martins Costa (saviocmc at protonmail dot com)

# Sets the character of separation to be the new line \n (the default is a space)
IFS=$'\n'

WAKEUP=/proc/acpi/wakeup
CONFIG=/etc/wakeup-triggers.conf
BACKUP=/tmp/wakeup-triggers

backupAndDisableAllCurrentTriggers() {
    enabledTriggers=($(cat $WAKEUP | grep "enable"))
    for trigger in "${enabledTriggers[@]}"; do
        triggerCode=${trigger:0:4}
        echo $triggerCode >> $BACKUP
        echo $triggerCode > $WAKEUP
    done
}

enableSelectedTriggers() {
    selectedTriggers=($(cat $CONFIG | grep --invert-match '#' ))
    for trigger in "${selectedTriggers[@]}"; do
        echo $trigger > $WAKEUP
    done
}

restoreBackup() {
    enabledTriggers=($(cat $WAKEUP | grep "enable"))
    for trigger in "${enabledTriggers[@]}"; do
        triggerCode=${trigger:0:4}
        echo $triggerCode > $WAKEUP
    done
    backupTriggers=($(cat $BACKUP))
    for trigger in "${backupTriggers[@]}"; do
        echo $trigger > $WAKEUP
    done
    rm $BACKUP
}

start() {
    backupAndDisableAllCurrentTriggers
    enableSelectedTriggers
    exit 0
}

stop () {
    restoreBackup
    exit 0
}

case "$1" in
    "start") start;;
    "stop") stop;;
    *) exit 1;;
esac