pinephone-scripts

Scripts and small applications for the PinePhone
git clone https://src.jayvii.de/pub/pinephone-scripts.git
Log | Files | Refs | README | LICENSE

sguard.sh (1597B)


      1 #!/usr/bin/env bash
      2 # SPDX-FileCopyrightText: 2021 JayVii, 2021 HazardChem
      3 # SPDX-License-Identifier: GPL-3.0-or-later
      4 # About this header: https://reuse.software
      5 
      6 
      7 # Checks ----------------------------------------------------------------------
      8 
      9 # is pacmd installed?
     10 if [[ -z `whereis pacmd | awk '{ print $2 }'` ]]; then
     11     echo "pacmd is required for sguard to function properly."
     12     exit 1;
     13 fi
     14 
     15 # is gnome-session-inhibit installed?
     16 if [[ -z `whereis gnome-session-inhibit | awk '{ print $2 }'` ]]; then
     17     echo "gnome-session-inhibit is required for sguard to function properly."
     18     exit 2;
     19 fi
     20 
     21 # Configuration ---------------------------------------------------------------
     22 if [[ -z $DEBUG ]]; then
     23     DEBUG="false"
     24 fi
     25 if [[ -z $TIMEOUT ]]; then
     26     TIMEOUT=60
     27 fi
     28 
     29 # Functions -------------------------------------------------------------------
     30 
     31 # check player state.
     32 function check_player_state {
     33     if [[ -z `pacmd list sinks | grep -E "(s|S)tate: RUNNING$"` ]]; then
     34         echo false
     35     else
     36         echo true
     37     fi
     38 }
     39 
     40 # prevent sleep temporary (Timeout + 1 sec)
     41 function prevent_sleep {
     42     gnome-session-inhibit --inhibit suspend \
     43                           --reason "Audio is playing..." \
     44                           sleep $(($TIMEOUT + 1))
     45 }
     46 
     47 # Execution -------------------------------------------------------------------
     48 
     49 while :; do
     50     if [[ `check_player_state` == "true" ]]; then
     51         prevent_sleep &
     52         if [[ "$DEBUG" == "true" ]]; then
     53             echo "inhibit sleep for $TIMEOUT seconds"
     54         fi
     55     fi
     56     sleep $TIMEOUT # Zzzz...
     57 done
     58 
     59 # EOF sguard.sh