pub / sguard

Suspend Guard for the PhoneShell "Phosh"
git clone https://https://src.jayvii.de/pub/sguard.git
Home | Log | Files | Exports | Refs | README | RSS

commit cde3f79d5ef42ff446494182175fd6ce4a14cff0
parent 55c53932358b4bbbe87ef9494935f8de89902da3
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Mon, 11 Jan 2021 14:01:53 +0100

Add Suspend-inhibitor script

Diffstat:
Asguard/Makefile | 6++++++
Asguard/sguard.service | 10++++++++++
Asguard/sguard.sh | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/sguard/Makefile b/sguard/Makefile @@ -0,0 +1,6 @@ +install: + mkdir -p ${HOME}/.local/bin + mkdir -p ${HOME}/.config/systemd/user + install -m 700 ./sguard.sh ${HOME}/.local/bin/ + install -m 755 ./sguard.service ${HOME}/.config/systemd/user/ + systemctl --user daemon-reload diff --git a/sguard/sguard.service b/sguard/sguard.service @@ -0,0 +1,10 @@ +[Unit] +Description=Sleep Inhibitor + +[Service] +ExecStartPre=%h/.local/bin/sguard.sh reset +ExecStart=%h/.local/bin/sguard.sh +Restart=always + +[Install] +WantedBy=default.target diff --git a/sguard/sguard.sh b/sguard/sguard.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +STATEFILE="/run/user/1000/sguard.state" +ORIGSTATE="/run/user/1000/sguard.orig" +TIMEOUT=60 + +# supported applications +SUPPAPP="mpv gnome-podcasts lollypop" + +# check player state. FIXME: use mpris state via dbus instead of PIDs +function check_player_state { + if [[ ! -z `pidof $1` ]]; then + echo true + else + echo false + fi +} + +function prevent_sleep { + # take note of original settings + gsettings get org.gnome.settings-daemon.plugins.power \ + sleep-inactive-battery-type > $ORIGSTATE + gsettings get org.gnome.settings-daemon.plugins.power \ + sleep-inactive-ac-type >> $ORIGSTATE + # set new settings (prevent sleep) + gsettings set org.gnome.settings-daemon.plugins.power \ + sleep-inactive-battery-type 'nothing' + gsettings set org.gnome.settings-daemon.plugins.power \ + sleep-inactive-ac-type 'nothing' +} + +function reset_settings { + # recover original settings from state file + orig_ba=`sed -n 1p $ORIGSTATE` + orig_ac=`sed -n 2p $ORIGSTATE` + # set original values + gsettings set org.gnome.settings-daemon.plugins.power \ + sleep-inactive-battery-type $orig_ba + gsettings set org.gnome.settings-daemon.plugins.power \ + sleep-inactive-ac-type $orig_ac +} + +if [[ "$1" == "reset" ]]; then + reset_settings +else + # initiate state file + echo "false" > $STATEFILE + + # run loop + while :; do + CURRENT_STATE=`cat $STATEFILE` + SHOULD_STATE=`check_player_state "$SUPPAPP"` + if [[ "$CURRENT_STATE" != "$SHOULD_STATE" ]]; then + if [[ "$SHOULD_STATE" == "true" ]]; then + prevent_sleep + echo "true" > $STATEFILE + echo "[info] `date '+%Y-%m-%d %H-%M-%S'` inhibit sleep" + else + reset_settings + echo "false" > $STATEFILE + echo "[info] `date '+%Y-%m-%d %H-%M-%S'` uninhibit sleep" + fi + fi + sleep $TIMEOUT + done +fi + +# EOF sguard.sh