pinephone-scripts/sleepwalk

91 lines
2.4 KiB
Plaintext
Raw Normal View History

2023-08-04 12:10:35 +02:00
#! /bin/sh
2022-01-04 21:43:20 +01:00
2023-08-04 12:10:35 +02:00
set -eu
2023-03-13 02:49:33 +01:00
2023-08-04 12:10:35 +02:00
THRESHOLD=15.00
2022-01-04 21:43:20 +01:00
2023-03-13 02:49:33 +01:00
is_charging() {
[ "$(cat /sys/class/power_supply/axp20x-usb/online)" = "1" ]
}
is_inhibitor_active() {
2023-08-04 12:10:35 +02:00
#dbus-send --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.GetInhibitors | tail -n+2 | grep object
#INHIBITORS=$(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=,,") gnome-session-inhibit --list)
2023-03-13 02:49:33 +01:00
# printf "%s\n" "$INHIBITORS"
INHIBITORS=$(systemd-inhibit --list --what=sleep --mode=block --no-legend \
2023-08-04 12:10:35 +02:00
| grep -e 'sleep')
2022-03-04 22:31:09 +01:00
2023-03-13 02:49:33 +01:00
[ -n "$INHIBITORS" ]
# if [ "$INHIBITORS" = "No inhibitors" ]; then
# return 1
# fi
# return 0
}
2022-03-04 22:31:09 +01:00
2023-03-13 02:49:33 +01:00
is_screen_on() {
return "$(cat /sys/class/backlight/backlight/bl_power)"
}
2023-03-20 03:05:34 +01:00
network_activity() {
2023-08-04 12:10:35 +02:00
for n in $(seq 2)
do
for i in wwan0 wlan0
do
A=$(ifstat -i $i 1 1 | tail -n1)
RX=$(echo $A | awk '{print $1}')
TX=$(echo $A | awk '{print $2}')
if [ "$RX" = "n/a" ] || [ "$TX" = "n/a" ]
then
return 1
fi
2022-01-04 21:43:20 +01:00
2023-08-04 12:10:35 +02:00
for v in $RX $TX
do
C=$(echo $v $THRESHOLD |
awk '{if ($1 > $2) print "y"; else print "n"}')
if [ "$C" = "y" ]
then
echo Ongoing network activity >&2
return 0
fi
done
done
done
2022-01-04 21:43:20 +01:00
2023-08-04 12:10:35 +02:00
return 1
2022-01-04 21:43:20 +01:00
}
bluetooth_connected() {
test "$(bluetoothctl devices Connected)" != ""
return
}
hotspot_active() {
nmcli c show --active | grep -e 'Hotspot'
return
}
2023-08-04 12:10:35 +02:00
can_suspend() {
! ( is_inhibitor_active || is_screen_on || is_charging || bluetooth_connected|| hotspot_active || network_activity )
2022-01-04 21:43:20 +01:00
}
2023-08-04 12:10:35 +02:00
cpufreq-set -g powersave
2023-03-13 02:49:33 +01:00
2023-08-04 12:10:35 +02:00
while :
do
if can_suspend
then
(rtcwake -m no -s 600 && systemctl suspend) || :
2023-08-04 12:10:35 +02:00
if can_suspend
then
sleep 30
fi
2023-08-04 12:10:35 +02:00
else
sleep 5
fi
done