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
|
2022-04-21 01:42:42 +02:00
|
|
|
MEDIA_APP=playerctl
|
2021-01-04 18:27:42 +01:00
|
|
|
INHIBIT_FILE=$HOME/.inhibit_suspend
|
2021-01-03 19:51:23 +01:00
|
|
|
|
2021-01-04 18:27:42 +01:00
|
|
|
## helpers
|
2022-04-21 01:42:42 +02:00
|
|
|
# 1: discharging
|
|
|
|
# 0: charging/full
|
2021-01-03 19:51:23 +01:00
|
|
|
function _state() {
|
|
|
|
f=$BAT_PATH/$BAT/status
|
2022-04-21 01:42:42 +02:00
|
|
|
# default to discharging when status file not found
|
2021-01-04 18:27:42 +01:00
|
|
|
s=1
|
2021-01-03 19:51:23 +01:00
|
|
|
if [ -e $f ]; then
|
|
|
|
s=$(grep -c Discharging $f)
|
|
|
|
fi
|
|
|
|
echo $s
|
|
|
|
}
|
|
|
|
|
|
|
|
function _suspend() {
|
2021-01-04 18:27:42 +01:00
|
|
|
_check_inhibit $INHIBIT_FILE
|
2021-01-03 19:51:23 +01:00
|
|
|
state=$(_state)
|
|
|
|
force=${1:-0}
|
2022-05-11 14:08:30 +02:00
|
|
|
mode=${2:-suspend-then-hibernate}
|
2021-01-03 19:51:23 +01:00
|
|
|
if [ $state -eq 1 -o $force -eq 1 ]; then
|
2022-05-11 14:08:30 +02:00
|
|
|
_log SUSPEND INFO starting. state: $state, force: $force, mode: $mode
|
2022-04-21 01:42:42 +02:00
|
|
|
$MEDIA_APP stop
|
2022-05-11 14:08:30 +02:00
|
|
|
systemctl $mode
|
2021-01-03 19:51:23 +01:00
|
|
|
else
|
2022-05-11 14:08:30 +02:00
|
|
|
_log SUSPEND INFO aborting. state: $state, force: $force, mode: $mode
|
2021-01-03 19:51:23 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-01-04 18:27:42 +01:00
|
|
|
## 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*)
|
2021-01-04 18:27:42 +01:00
|
|
|
_set_inhibit $INHIBIT_FILE $2
|
|
|
|
;;
|
2022-04-21 01:42:42 +02:00
|
|
|
suspend-battery)
|
2022-05-11 14:08:30 +02:00
|
|
|
_suspend 0 suspend-then-hibernate
|
2021-01-03 19:51:23 +01:00
|
|
|
;;
|
2022-04-21 01:42:42 +02:00
|
|
|
suspend)
|
2022-05-11 14:08:30 +02:00
|
|
|
_suspend 1 suspend
|
|
|
|
;;
|
|
|
|
suspend-hib)
|
|
|
|
_suspend 1 suspend-then-hibernate
|
2021-01-03 19:51:23 +01:00
|
|
|
;;
|
|
|
|
*)
|
2021-01-04 18:27:42 +01:00
|
|
|
echo "invalid argument: \"$1\""
|
2022-05-11 14:08:30 +02:00
|
|
|
echo "arguments: [inhibit [on|off]|suspend-battery|suspend-only|suspend]"
|
2021-01-03 19:51:23 +01:00
|
|
|
;;
|
|
|
|
esac
|