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 3ec60be27fa26e7ce76216d90e04c66584530bd1
parent c8fa7abe8b09db73220b4729e94641cffe421484
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Fri, 22 Jan 2021 16:29:17 +0100

Using pacmd instead of dbus interface

Diffstat:
Msguard/sguard.sh | 117+++++++++++++++++++++++--------------------------------------------------------
1 file changed, 34 insertions(+), 83 deletions(-)

diff --git a/sguard/sguard.sh b/sguard/sguard.sh @@ -1,104 +1,55 @@ #!/usr/bin/env bash +# Checks ---------------------------------------------------------------------- + +# is pacmd installed? +if [[ ! -z `whereis pacmd | awk '{ print $2 }'` ]]; then + echo "pacmd is required for sguard to function properly." + exit 1; +fi + +# is gnome-session-inhibit installed? +if [[ ! -z `whereis gnome-session-inhibit | awk '{ print $2 }'` ]]; then + echo "gnome-session-inhibit is required for sguard to function properly." + exit 2; +fi + # Configuration --------------------------------------------------------------- -STATEFILE="/run/user/$UID/sguard.state" -ORIGSTATE="/run/user/$UID/sguard.orig" -TIMEOUT=60 +if [[ -z $DEBUG ]]; then + DEBUG="false" +fi +if [[ -z $TIMEOUT ]]; then + TIMEOUT=60 +fi # Functions ------------------------------------------------------------------- # check player state. function check_player_state { - PLAYERS=`list_services` - PL_STATUS="" - # if there are no active players, return "false" - if [[ ! -z "$PLAYERS" ]]; then + if [[ ! -z `pacmd list-sink-inputs | grep "state: RUNNING$"` ]]; then echo false else - # get status of active players - for pl in $PLAYERS; do - PL_STATUS="$PL_STATUS `get_status $pl`" - done - # if active players are playing media, return "true", else "false" - if [[ ! -z `echo $PL_STATUS | grep "Playing"` ]]; then - echo true - else - echo false - fi + echo true fi } -function get_status { # $1 - Player ("vlc", "mpv", ...) - gdbus call --session --dest $1 \ - --object-path /org/mpris/MediaPlayer2 \ - --method org.freedesktop.DBus.Properties.Get \ - org.mpris.MediaPlayer2.Player "PlaybackStatus" -} - -function list_services() { - # https://gist.github.com/amol9/6d47fdd21a1c5e006c84#file-vlc_dbus-sh-L35 - gdbus call --session --dest org.freedesktop.DBus \ - --object-path /org/freedesktop/DBus \ - --method org.freedesktop.DBus.ListNames \ - | grep -Po "'\w.*?'" | tr -d "'" | grep "org\.mpris\.MediaPlayer2" -} - -# prevent sleep (and backup original state) +# prevent sleep temporary (Timeout + 1 sec) 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' -} - -# reset settings to original state -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 + gnome-session-inhibit --inhibit suspend \ + --reason "Audio is playing..." \ + sleep $(($TIMEOUT + 1)) } # Execution ------------------------------------------------------------------- -# reset settings if specified (and exists). -if [[ "$1" == "reset" ]]; then - if [[ -f $ORIGSTATE ]]; then - reset_settings - else - echo "Original State can not be restored..." - fi -else - # initiate state file - echo "false" > $STATEFILE - - # execute suspend-guard - 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 "inhibit sleep" - else - reset_settings - echo "false" > $STATEFILE - echo "uninhibit sleep" - fi +while :; do + if [[ `check_player_state` == "true" ]]; then + prevent_sleep & + if [[ "$DEBUG" == "true" ]]; then + echo "inhibit sleep for $TIMEOUT seconds" fi - sleep $TIMEOUT # Zzzz... - done -fi + fi + sleep $TIMEOUT # Zzzz... +done # EOF sguard.sh