diff options
| author | littleblack111 <jerrylam110209@gmail.com> | 2023-08-14 16:15:47 +0800 |
|---|---|---|
| committer | littleblack111 <jerrylam110209@gmail.com> | 2023-08-14 16:15:47 +0800 |
| commit | c95750b8b3a33f7146379571047d64f9adf55846 (patch) | |
| tree | e08e3efe5f31f065c66603b2dad30ef54cf0ee08 /idle.sh | |
| parent | 31929cd6f44ffe7cfb65d992a8541671715c9875 (diff) | |
updated
Diffstat (limited to 'idle.sh')
| -rwxr-xr-x | idle.sh | 156 |
1 files changed, 99 insertions, 57 deletions
@@ -1,68 +1,110 @@ #!/usr/bin/bash +# arguments with signal trap +#trap idle SIGQUIT +#trap stopidle SIGILL +#trap "kill $$" SIGINT + +# sys vars +lockpath="/tmp/lock" +lockfile="idle.lck" +pidfile="idle.pid" + +# usr-def vars mutev=true # stop volume/sound(mute) - comment or un-comment pmusic=true # stop/start music from playing - command or un-comment IDLETIME=1800000 IDLEBACKTIME=100 +function idle() { + printf "User is idle(afk) in %s\n" "$(date)" + sudo /sbin/touch $lockpath/$lockfile + #if [ $mutev ]; then + # soundlevel=$(awk -F"[][]" '/Left:/ { print $2 }' <(amixer sget Master)) + # amixer set Master 0% + #fi + if [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]]; then + amixer set Master mute + elif [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then + notify-send "Warning: Volume is already muted" + elif [ $mutev ] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then + notify-send -u critical "Unable to get volume(sound) status: $(pactl get-sink-mute @DEFAULT_SINK@)" + fi + script -c "$HOME/scripts/update.sh" /var/log/sysupdate.log -a --force + if [ $pmusic ] && [[ $(NekoMC --status) == "Playing" ]]; then + m=true + fi + if [ $pmusic ] && [ "$m" ]; then + NekoMC --pause & + fi + # re-cache neofetch + script -c "$HOME/scripts/neofetchCache.sh" "$HOME"/scripts/logs/neofetchCache.log -a --force + # clean clipboard history + script -c "$HOME/scripts/clearcliphis.sh" "$HOME"/scripts/logs/clearcliphis.log -a --force + #/sbin/killall picom; bspcomp & # paused in freezeapp and unfreezeapp + # Disable mouse just in case accidental awake for high DPI rate mouse + xinput --disable 12 + freezeapp & +} -while true; do - if [[ "$(systemctl is-system-running)" == "running" || "$(systemctl is-system-running)" == "degraded" ]]; then - if [[ "$(xprintidle)" -gt $IDLETIME && ! -f /tmp/lock/idle.lock ]]; then # Changed from 600000 (10 minutes) to 3600000 (1 hour) - printf "User is idle(afk) in %s\n", "$(date)" - sudo /sbin/touch /tmp/lock/idle.lock - #if [ $mutev ]; then - # soundlevel=$(awk -F"[][]" '/Left:/ { print $2 }' <(amixer sget Master)) - # amixer set Master 0% - #fi - if [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]]; then - amixer set Master mute - elif [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then - notify-send "Warning: Volume is already muted" - elif [ $mutev ] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then - notify-send -u critical "Unable to get volume(sound) status: $(pactl get-sink-mute @DEFAULT_SINK@)" - fi - script -c "$HOME/scripts/update.sh" /var/log/sysupdate.log -a --force - if [ $pmusic ] && [[ $(NekoMC --status) == "Playing" ]]; then - m=true - fi - if [ $pmusic ] && [ $m ]; then - NekoMC --pause & - fi - script -c "$HOME/scripts/clearcliphis.sh" $HOME/scripts/logs/clearcliphis.log -a --force - #killall picom; bspcomp & # paused in freezeapp and unfreezeapp - # Disable mouse just in case accidental awake for high DPI rate mouse - xinput --disable 12 - freezeapp & - continue - elif [[ "$(xprintidle)" -le $IDLEBACKTIME && -f /tmp/lock/idle.lock ]]; then - printf "User is back from idle(afk) in %s\n", "$(date)" - #if [ $mutev ] && [ $soundlevel ]; then - # amixer set Master $soundlevel - # unset $soundlevel - #elif [ $mutev ] && [ ! $soundlevel ]; then - # notify-send "ERROR: \$soundlevel is not set, unknown sound level..." - #fi - # Re-enable mouse - xinput --enable 12 - if [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then - amixer set Master unmute - elif [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]]; then - notify-send -u critical "ERROR: Volume(sound) is not muted" - elif [ $mutev ] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then - notify-send -u critical "Unable to get volume(sound) status: $(pactl get-sink-mute @DEFAULT_SINK@)" - fi - killall update.sh yay - unfreezeapp & - if [ $pmusic ] && [ $m ]; then - NekoMC --play & - unset m - fi - sudo /sbin/rm -f /tmp/lock/idle.lock - continue +function stopidle() { + printf "User is back from idle(afk) in %s\n", "$(date)" + #if [ $mutev ] && [ $soundlevel ]; then + # amixer set Master $soundlevel + # unset $soundlevel + #elif [ $mutev ] && [ ! $soundlevel ]; then + # notify-send "ERROR: \$soundlevel is not set, unknown sound level..." + #fi + # Re-enable mouse + xinput --enable 12 + if [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then + amixer set Master unmute + elif [ $mutev ] && [[ $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]]; then + notify-send -u critical "ERROR: Volume(sound) is not muted" + elif [ $mutev ] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: no" ]] && [[ ! $(pactl get-sink-mute @DEFAULT_SINK@) == "Mute: yes" ]]; then + notify-send -u critical "Unable to get volume(sound) status: $(pactl get-sink-mute @DEFAULT_SINK@)" + fi + /sbin/killall update.sh yay + unfreezeapp & + if [ $pmusic ] && [ "$m" ]; then + NekoMC --play & + unset m + fi + sudo /sbin/rm -f $lockpath/$lockfile +} + +if [ "$1" ]; then + if [[ "$1" == '-d' || "$1" == '--daemon' ]]; then + if ! pgrep "$0"; then + /sbin/printf "PID: " + /sbin/echo $$ | sudo /sbin/tee $lockpath/$pidfile else - sleep 1 - continue + /sbin/printf "An instance of this program is dectected, please close that instance in order to launch another\n" fi + while true; do + if [[ "$(systemctl is-system-running)" == "running" || "$(systemctl is-system-running)" == "degraded" ]]; then + if [[ "$(xprintidle)" -gt $IDLETIME && ! -f $lockpath/$lockfile && $1 ]]; then # Changed from 600000 (10 minutes) to 3600000 (1 hour) + idle + continue + elif [[ "$(xprintidle)" -le $IDLEBACKTIME && -f $lockpath/$lockfile ]]; then + stopidle + continue + else + sleep 1 + continue + fi + fi + done + elif [[ "$1" == '--pause' || "$1" == '--idle' ]]; then + #/sbin/kill -s 3 $(/sbin/cat $lockpath/$pidfile) + idle + exit 0 + elif [[ "$1" == '--continue' || "$1" == '--stopidle' ]]; then + #/sbin/kill -s 4 $(/sbin/cat $lockpath/$pidfile) + stopidle + exit 0 fi -done +fi + +/sbin/printf "Un-recornized option: '$@', Please use options:\n\t-d|--daemon\tTo start daemon(auto idle actions)\n\t--idle|--pause\tTo call \`idle\` function\n\t--continue|--stopidle\tTo call \`stopidle\` function\n" +exit 1 |
