config_sway/bin/sway_power

59 lines
1.2 KiB
Text
Raw Permalink 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
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}
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
$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
}
## 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)
2022-05-11 14:08:30 +02:00
_suspend 0 suspend-then-hibernate
2021-01-03 19:51:23 +01: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
;;
2024-10-09 16:57:05 +02:00
poweroff)
systemctl poweroff
;;
2021-01-03 19:51:23 +01:00
*)
echo "invalid argument: \"$1\""
2024-10-09 16:57:05 +02:00
echo "arguments: [inhibit [on|off]|suspend-battery|suspend-only|suspend|poweroff]"
2021-01-03 19:51:23 +01:00
;;
esac