2021-01-03 19:51:23 +01:00
|
|
|
#!/bin/bash
|
|
|
|
BAT_PATH=/sys/class/power_supply
|
|
|
|
BAT=BAT0
|
|
|
|
MODE=suspend-then-hibernate
|
|
|
|
|
|
|
|
# 0: charging
|
|
|
|
# 1: discharging/unknown
|
|
|
|
function _state() {
|
|
|
|
f=$BAT_PATH/$BAT/status
|
|
|
|
s=1
|
|
|
|
if [ -e $f ]; then
|
|
|
|
s=$(grep -c Discharging $f)
|
|
|
|
fi
|
|
|
|
echo $s
|
|
|
|
}
|
|
|
|
|
|
|
|
function _suspend() {
|
|
|
|
state=$(_state)
|
|
|
|
force=${1:-0}
|
|
|
|
echo "INFO: state: $state, force: $force"
|
|
|
|
if [ $state -eq 1 -o $force -eq 1 ]; then
|
|
|
|
systemctl $MODE
|
|
|
|
else
|
|
|
|
echo "ERROR: state: $state, force: $force"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:34:08 +01:00
|
|
|
mkdir -p $HOME/log
|
|
|
|
echo "suspend: $(date) args: $*" >> $HOME/log/sway.log
|
|
|
|
|
2021-01-03 19:51:23 +01:00
|
|
|
case $1 in
|
|
|
|
suspend)
|
|
|
|
_suspend 0
|
|
|
|
;;
|
|
|
|
force)
|
|
|
|
_suspend 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "ERROR: invalid command: \"$1\""
|
|
|
|
echo "possible options: [suspend|force]"
|
|
|
|
;;
|
|
|
|
esac
|