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 (1452B)


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