config_sway/bin/sway_suspend

52 lines
939 B
Text
Raw Normal View History

2021-01-03 19:51:23 +01:00
#!/bin/bash
2021-01-04 18:46:33 +01:00
source $HOME/.config/sway/bin/sway_helpers
2021-01-03 19:51:23 +01:00
BAT_PATH=/sys/class/power_supply
BAT=BAT0
MODE=suspend-then-hibernate
MEDIA_APP=playerctl
INHIBIT_FILE=$HOME/.inhibit_suspend
2021-01-03 19:51:23 +01:00
## helpers
# 1: discharging
# 0: charging/full
2021-01-03 19:51:23 +01:00
function _state() {
f=$BAT_PATH/$BAT/status
# default to discharging when status file not found
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
$MEDIA_APP stop
2021-01-03 19:51:23 +01:00
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
2021-01-06 19:12:31 +01:00
i*)
_set_inhibit $INHIBIT_FILE $2
;;
suspend-battery)
2021-01-03 19:51:23 +01:00
_suspend 0
;;
suspend)
2021-01-03 19:51:23 +01:00
_suspend 1
;;
*)
echo "invalid argument: \"$1\""
echo "arguments: [inhibit [on|off]|suspend-battery|suspend]"
2021-01-03 19:51:23 +01:00
;;
esac