config_sway/bin/suspend

49 lines
828 B
Text
Raw Normal View History

2021-01-03 19:51:23 +01:00
#!/bin/bash
source $HOME/.config/sway/bin/helpers
2021-01-03 19:51:23 +01:00
BAT_PATH=/sys/class/power_supply
BAT=BAT0
MODE=suspend-then-hibernate
INHIBIT_FILE=$HOME/.inhibit_suspend
2021-01-03 19:51:23 +01:00
## helpers
2021-01-03 19:51:23 +01:00
# 0: charging
# 1: discharging/unknown
function _state() {
f=$BAT_PATH/$BAT/status
s=1
2021-01-03 19:51:23 +01:00
if [ -e $f ]; then
s=$(grep -c Discharging $f)
fi
echo $s
}
function _suspend() {
_check_inhibit $INHIBIT_FILE
2021-01-03 19:51:23 +01:00
state=$(_state)
force=${1:-0}
if [ $state -eq 1 -o $force -eq 1 ]; then
systemctl $MODE
else
_log SUSPEND INFO aborting. state: $state, force: $force
2021-01-03 19:51:23 +01:00
fi
}
## main
_log SUSPEND INFO args: $*
2021-01-03 22:34:08 +01:00
2021-01-03 19:51:23 +01:00
case $1 in
inhibit)
_set_inhibit $INHIBIT_FILE $2
;;
2021-01-03 19:51:23 +01:00
suspend)
_suspend 0
;;
force)
_suspend 1
;;
*)
echo "invalid argument: \"$1\""
echo "arguments: [inhibit [on|off]|suspend|force]"
2021-01-03 19:51:23 +01:00
;;
esac