summaryrefslogtreecommitdiff
path: root/idle.sh
blob: 4750e28268aa342b79a8ea0dc6cf640bb83b15e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/bash

renice -n 19 $$
ionice -c 3 -p $$

# arguments with signal trap
#trap idle SIGQUIT
#trap stopidle SIGILL
#trap "kill $$" SIGINT

# sys vars
lockpath="/var/lock"
lockname="idle"
lockpath="$lockpath/$lockname"
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

if [ ! -d $lockpath ]; then
	/sbin/echo "Lock path does not exist, creating it"
	sudo -n /sbin/mkdir -vp $lockpath || /sbin/echo "Failed to create lock path at $lockpath"
fi

function cleanup() {
	/sbin/echo "Cleaning up..."
	sudo -n /sbin/rm -rf $lockpath || /sbin/echo "Failed removing locks at $lockpath"
	/sbin/echo "Exiting..."
	exit 0
}

trap cleanup SIGINT SIGTERM

function idle() {
	if [[ -f $lockpath/$disablelockfile ]]; then
		/sbin/echo "idle: Idle is disabled"
	fi
	/sbin/echo "User is idle(afk) in $(date)"
	/sbin/echo "Creating lock file"
	sudo -n /sbin/touch $lockpath/$lockfile || /sbin/echo "Failed to create lock file at $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
	script -c 'topgrade -y --no-retry' /var/log/sysupdate.log -a --force

	if [ $pmusic ]; then
		if [[ $(playerctl status -p spotify) == "Playing" ]]; then
			m=true
		fi
	fi
	if [ $pmusic ]; then
		playerctl --all-players 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 &
}

function stopidle() {
	if [[ -f $lockpath/$disablelockfile ]]; then
		/sbin/echo "stopidle: Idle is disabled"
	fi
	/sbin/echo "User is back from idle(afk) in $(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
	$HOME/scripts/flipper-ac.py on &
	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
		playerctl play -p spotify
		unset m
	fi
	/sbin/echo "Removing lock file"
	sudo -n /sbin/rm -f $lockpath/$lockfile || /sbin/echo "Failed removing lock file at $lockpath/$lockfile"
}

if [ "$1" ]; then
	if [[ "$1" == '-d' || "$1" == '--daemon' ]]; then
		if ! pgrep "$0"; then
			/sbin/echo -n "PID: "
			/sbin/echo $$ | sudo -n /sbin/tee $lockpath/$pidfile || echo "PID Grab failed"
		else
			/sbin/echo "An instance of this program is dectected, please close that instance in order to launch another"
		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 [[ ! -f $lockpath/$lockfile && $1 ]]; then
					sleep 10
					continue
				fi
				if [[ "$(xprintidle)" -le $IDLEBACKTIME && -f $lockpath/$lockfile ]]; then
					stopidle
					continue
				else
					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)
		sudo -n /sbin/touch $lockpath/$lockfile
		exit 0
	elif [[ "$1" == '--disable' || "$1" == '-dis' ]]; then
		sudo -n /sbin/touch $lockpath/$disablelockfile
		exit 0
	elif [[ "$1" == '--enable' || "$1" == '-en' ]]; then
		sudo -n /sbin/rm -f $lockpath/$disablelockfile
		exit 0
	fi
fi

/sbin/echo -e "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"
exit 1