Repository navigation
Expand file tree
/
Copy pathsleepwalk
More file actions
166 lines (144 loc) · 3.74 KB
/
Copy pathsleepwalk
File metadata and controls
166 lines (144 loc) · 3.74 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
SLEEP_STEP=30
SLEEP_SECS_INIT=$SLEEP_STEP
SLEEP_SECS=$SLEEP_SECS_INIT
SLEEP_MAX_SECS=600
WAKE_SECS=30
LOCK_DIR="/tmp/sleepwalk"
IS_NOTIFICATION=0
NOTIFICATION_FILE="/tmp/sleepwalk-notify"
start_deep_sleep() {
led_sleep
echo mem > /sys/power/state 2>/dev/null
return $?
}
wait_for_notifications() {
secs=$WAKE_SECS
is_led_on=0
while [ $secs -ge 0 ]; do
sleep 1
if [ $is_led_on == 0 ]; then
led_wake
is_led_on=1
else
led_disable
is_led_on=0
fi
if ! can_sleep; then
SLEEP_SECS=$SLEEP_SECS_INIT
break
fi
check_notifications
secs=$(($secs-1))
done
led_disable
}
schedule_wake_time() {
rtcwake -m no --date "+${SLEEP_SECS}s" 2>&1 >/dev/null
}
reset_wake_time() {
led_wake
> /sys/class/rtc/rtc0/wakealarm
}
can_sleep() {
if [ "$(cat /sys/class/backlight/backlight/bl_power)" == 0 ] || [ "$(cat /sys/class/power_supply/axp20x-usb/online)" == "1" ]; then
return 1
else
return 0
fi
}
led_sleep() {
if [ $IS_NOTIFICATION == 0 ]; then
echo 1 > /sys/class/leds/green\:indicator/brightness
echo 0 > /sys/class/leds/red\:indicator/brightness
echo 0 > /sys/class/leds/blue\:indicator/brightness
else
echo 0 > /sys/class/leds/green\:indicator/brightness
echo 0 > /sys/class/leds/red\:indicator/brightness
echo 1 > /sys/class/leds/blue\:indicator/brightness
fi
}
led_wake() {
if [ $IS_NOTIFICATION == 0 ]; then
echo 0 > /sys/class/leds/green\:indicator/brightness
echo 1 > /sys/class/leds/red\:indicator/brightness
echo 0 > /sys/class/leds/blue\:indicator/brightness
else
echo 0 > /sys/class/leds/green\:indicator/brightness
echo 0 > /sys/class/leds/red\:indicator/brightness
echo 1 > /sys/class/leds/blue\:indicator/brightness
fi
}
led_disable() {
echo 0 > /sys/class/leds/green\:indicator/brightness
echo 0 > /sys/class/leds/red\:indicator/brightness
echo 0 > /sys/class/leds/blue\:indicator/brightness
}
notification_watcher() {
sudo -u "$(cat /proc/$(pidof -s gnome-session-ctl)/environ | grep -z USER | sed 's,\x0,,;s,USER=,,')" DBUS_SESSION_BUS_ADDRESS=$(cat /proc/$(pidof -s gnome-session-ctl)/environ | grep -z DBUS_SESSION_BUS_ADDRESS | sed "s,\x0,,;s,DBUS_SESSION_BUS_ADDRESS=,,") dbus-monitor interface='org.freedesktop.Notifications' > "$NOTIFICATION_FILE"
}
check_notifications() {
if cat "$NOTIFICATION_FILE" | grep "member=NotificationClosed" >/dev/null; then
IS_NOTIFICATION=0
> "$NOTIFICATION_FILE"
elif cat "$NOTIFICATION_FILE" | grep "member=Notify" >/dev/null; then
IS_NOTIFICATION=1
else
IS_NOTIFICATION=0
fi
}
if [ $EUID -ne 0 ]; then
echo "Needs root" >&2
exit 1
fi
if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
cat <<-EOF >&2
Usage: sleepwalk [start/stop]
Commands:
start Start sleep cycle
stop Stop sleep cycle
EOF
exit
fi
if [ "$1" == "start" ]; then
if [ -d "$LOCK_DIR" ] || ! mkdir "$LOCK_DIR"; then
echo "sleepwalk is already running - run 'sleepwalk stop' to stop it" >&2
exit 1
fi
trap "kill $NOTIFICATION_WATCHER; $0 stop" EXIT
touch "$NOTIFICATION_FILE"
notification_watcher &
NOTIFICATION_WATCHER=$!
while [ -d "$LOCK_DIR" ]; do
while ! can_sleep; do
sleep 60
done
reset_wake_time
schedule_wake_time
check_notifications
while ! start_deep_sleep; do
reset_wake_time
echo "Failed going to sleep, try again in $WAKE_SECS seconds ..." >&2
wait_for_notifications
schedule_wake_time
if ! can_sleep; then
break
fi
done
if [ $((SLEEP_SECS + $SLEEP_STEP)) -lt $SLEEP_MAX_SECS ]; then
SLEEP_SECS=$((SLEEP_SECS + SLEEP_STEP))
else
SLEEP_SECS=$SLEEP_MAX_SECS
fi
wait_for_notifications
done
kill $NOTIFICATION_WATCHER
elif [ "$1" == "stop" ]; then
reset_wake_time
led_disable
rmdir "$LOCK_DIR" 2>/dev/null
rm "$NOTIFICATION_FILE" 2>/dev/null
else
echo "Unknown command: $1" >&2
exit 1
fi