sway: structure scripts, add helpers in separate file

This commit is contained in:
Konstantin Koslowski 2021-01-04 18:27:42 +01:00
parent 307d1dd03d
commit 9a4b96932a
5 changed files with 86 additions and 70 deletions

View file

@ -1,13 +1,16 @@
#!/bin/bash
source $HOME/.config/sway/bin/helpers
BAT_PATH=/sys/class/power_supply
BAT=BAT0
MODE=suspend-then-hibernate
INHIBIT_FILE=$HOME/.inhibit_suspend
## helpers
# 0: charging
# 1: discharging/unknown
function _state() {
f=$BAT_PATH/$BAT/status
s=1
s=1
if [ -e $f ]; then
s=$(grep -c Discharging $f)
fi
@ -15,20 +18,23 @@ function _state() {
}
function _suspend() {
_check_inhibit $INHIBIT_FILE
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"
_log SUSPEND INFO aborting. state: $state, force: $force
fi
}
mkdir -p $HOME/log
echo "suspend: $(date) args: $*" >> $HOME/log/sway.log
## main
_log SUSPEND INFO args: $*
case $1 in
inhibit)
_set_inhibit $INHIBIT_FILE $2
;;
suspend)
_suspend 0
;;
@ -36,7 +42,7 @@ case $1 in
_suspend 1
;;
*)
echo "ERROR: invalid command: \"$1\""
echo "possible options: [suspend|force]"
echo "invalid argument: \"$1\""
echo "arguments: [inhibit [on|off]|suspend|force]"
;;
esac