forked from iye/lightsOn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightsOn.sh
executable file
·332 lines (253 loc) · 10.8 KB
/
lightsOn.sh
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
# lightsOn.sh
set -x
# Copyright (c) 2013 iye.cba at gmail com
# url: https://github.com/iye/lightsOn
# This script is licensed under GNU GPL version 2.0 or above
# Modified by Stephane HUC
# year: 2015 >
# url: https://github.com/hucste/lightsOn
# email: [email protected]
# Description: Bash script that prevents the screensaver and display power
# management (DPMS) to be activated when you are watching Flash Videos
# fullscreen on Firefox and Chromium.
# Can detect mplayer, minitube, and VLC when they are fullscreen too.
# Also, screensaver can be prevented when certain specified programs are running.
# lightsOn.sh needs xscreensaver or kscreensaver to work.
# HOW TO USE: Start the script with the number of seconds you want the checks
# for fullscreen to be done. Example:
# "./lightsOn.sh 120 &" will Check every 120 seconds if Mplayer, Minitube
# VLC, Firefox or Chromium are fullscreen and delay screensaver and Power Management if so.
# You want the number of seconds to be ~10 seconds less than the time it takes
# your screensaver or Power Management to activate.
# If you don't pass an argument, the checks are done every 50 seconds.
#
# An optional array variable exists here to add the names of programs that will delay the screensaver if they're running.
# This can be useful if you want to maintain a view of the program from a distance, like a music playlist for DJing,
# or if the screensaver eats up CPU that chops into any background processes you have running,
# such as realtime music programs like Ardour in MIDI keyboard mode.
# If you use this feature, make sure you use the name of the binary of the program (which may exist, for instance, in /usr/bin).
clear
# Modify these variables if you want this script to detect if Mplayer,
# VLC, Minitube, or Firefox or Chromium Flash Video are Fullscreen and disable
# xscreensaver/kscreensaver and PowerManagement.
declare -a APPS=("chromium-browser" "firefox" "google-chrome" "opera")
APPS=("${APPS[@]}" "gnome-mplayer" "mplayer" "mplayer2" "mpv" "smplayer" "totem" "vlc")
APPS=("${APPS[@]}" "minitube" "popcorn-time" "skype" "smtube")
# for plugins
APPS=("${APPS[@]}" "operapluginwrapper-native" "plugin-container")
# Names of programs which, when running, you wish to delay the screensaver.
delay_progs=() # For example ('ardour2' 'gmpc')
# Screensavers Names Software
declare -a screensavers=("cinnamon-screensaver" "gnome-screensaver" "kscreensaver" "xautolock" "xscreensaver")
# YOU SHOULD NOT NEED TO MODIFY ANYTHING BELOW THIS LINE
delay="$1"
displays=""
LOCKFILE="/var/run/lock/$(basename "$0")" ;
pwd="$(dirname "$(readlink -f "$0")")"
function activeScreensaver() {
#case "${screensaver}" in
#"cinnamon-screensaver") cinnamon-screensaver-command --activate > /dev/null ;;
#"gnome-screensaver") gnome-screensaver-command --activate > /dev/null ;;
#"kscreensaver") qdbus org.kde.ksmserver /ScreenSaver SetActive false > /dev/null ;;
#"xautolock")
#xautolock -enable
#;;
#"xscreensaver") xscreensaver-command -activate > /dev/null ;;
#esac
# Activate DPMS is on.
xset dpms
}
function checkDelayProgs() {
for prog in "${delay_progs[@]}"; do
if [[ $(pidof "${prog}") -ge 1 ]]; then
echo "Delaying the screensaver because a program on the delay list, '${prog}', is running..."
delayScreensaver
break
fi
done
}
function checkFullscreen() {
# loop through every display looking for a fullscreen window
for display in $displays
do
#get id of active window and clean output
activ_win_id="$(DISPLAY=:0.${display} xprop -root _NET_ACTIVE_WINDOW)"
#activ_win_id=${activ_win_id#*# } #gives error if xprop returns extra ", 0x0" (happens on some distros)
activ_win_id="${activ_win_id:40:9}"
# Skip invalid window ids (commented as I could not reproduce a case
# where invalid id was returned, plus if id invalid
# isActivWinFullscreen will fail anyway.)
#if [ "$activ_win_id" = "0x0" ]; then
# continue
#fi
if [[ -n "${activ_win_id}" ]]; then
# Check if Active Window (the foremost window) is in fullscreen state
#isActivWinFullscreen="$(DISPLAY=:0.${display} xprop -id ${activ_win_id} | grep _NET_WM_STATE_FULLSCREEN)"
#if [[ "${isActivWinFullscreen}" = *NET_WM_STATE_FULLSCREEN* ]]; then
#if isAppRunning; then delayScreensaver; fi
#else
#xset dpms
#fi
if isAppRunning; then delayScreensaver; else activeScreensaver; fi
fi
done
}
function detect_id_displays() {
# enumerate all the attached screens
#while read id; do
#displays="${displays} ${id}"
#done < <(xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p')
displays=$(xvinfo | awk -F'#' '/^screen/ {print $2}' | xargs)
}
function detect_screensaver_used() {
# Detect screensaver been used
for saver in "${screensavers[@]}"; do
if [[ -n $(pidof "${saver}") ]]; then screensaver="${saver}"; fi
done
if [[ -z "${screensaver}" ]]; then
screensaver=None
echo "No screensaver detected"
fi
}
function icon_into_systray() {
# launch icon into systray
"${pwd}"/launcher_indicator.py &
}
function in_array() {
# equivalent to PHP in_array
# call: in_array needle array
local i=0 needle="$1" IFS=" "; shift; read -a array <<< "$@"
while [ $i -le ${#array[@]} ]; do
if [[ "${array[$i]}" == "${needle}" ]]; then return 0; fi # true
let i=i+1
done
return 1
unset i needle IFS array
}
function isAppRunning() {
# check if active windows is mplayer, vlc or firefox...
#Get title of active window
activ_win_id=$(xprop -id "${activ_win_id}")
activ_win_pid="$(grep "_NET_WM_PID(CARDINAL)" <<< "${activ_win_id}")"
activ_win_pid=${activ_win_pid##* }
app_name="$(awk '{print tolower($0)}' <<< $(basename $(lsof -aFn -p ${activ_win_pid} -d txt | sed -ne 's/^n//p')))"
if in_array "${app_name}" "${APPS[@]}"; then return 0; else return 1; fi
#activ_win_title="$(grep "WM_CLASS(STRING)" <<< "${activ_win_id}")" # I used WM_NAME(STRING) before, WM_CLASS more accurate.
#if [[ -n "${activ_win_title}" ]]; then
#activ_app_name="$(echo "${activ_win_title}" | awk -F "," '{print $2}' | tr -s ' ')" # just name app
#activ_app_name="${activ_app_name:2:-1}"
#app_name="$(awk '{print tolower($0)}' <<< "${activ_app_name}")" # app name string lower
#else
#app_name="$(awk '{print tolower($0)}' <<< $(basename $(lsof -aFn -p ${activ_win_pid} -d txt | sed -ne 's/^n//p')))"
#fi
#if in_array "${app_name}" "${APPS[@]}"; then
#case "${app_name}" in
#"chromium-browser"|"firefox"|"google-chrome"|"opera")
## detect if flashplayer run
#if lsof -p "${activ_win_pid}" | grep -i "flashplayer.so"; then
#process=$(pidof "${activ_app_name}")
#else
## other method to detect if flashplayer run
#case "${app_name}" in
#"chromium-browser"|"google-chrome")
#if [[ "$activ_win_title" = *${activ_app_name}* ]]; then
#process=$(pgrep -fc ".*((c|C)hrom(e|ium)).*flashp.*")
#fi
#;;
#"firefox")
#if [[ "$activ_win_title" = *unknown* || "$activ_win_title" = *plugin-container* ]]; then
#process=$(pgrep -l plugin-containe | grep -wc plugin-containe)
#fi
#;;
#esac
#fi
#if [[ -z "${process}" && "${activ_win_title}" = *${activ_app_name}* ]]; then
#case "${app_name}" in
#"google-chrome")
#if [[ "$activ_win_title" = *${activ_app_name}* ]]; then
#process="$(pidof chrome)"
#fi
#;;
#*)
#process=$(pidof "${app_name}")
#;;
#esac
#fi
#;;
#"popcorn-time")
#if [[ "${activ_win_title}" = *${activ_app_name}* ]]; then
#process="$(pidof "${activ_app_name}")"
#fi
#;;
#*)
#if [[ "${activ_win_title}" = *${activ_app_name}* ]]; then
#process=$(pidof "${app_name}")
#fi
#;;
#esac
#fi
#if [[ -n ${process} ]]; then unset process; return 0; else return 1; fi
}
function delayScreensaver() {
# reset inactivity time counter so screensaver is not started
case "${screensaver}" in
"cinnamon-screensaver") cinnamon-screensaver-command --deactivate > /dev/null ;;
"gnome-screensaver") gnome-screensaver-command --deactivate > /dev/null ;;
"kscreensaver") qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity > /dev/null ;;
"xautolock")
xautolock -disable
#xautolock -enable
;;
"xscreensaver") xscreensaver-command -deactivate > /dev/null ;;
esac
#Check if DPMS is on. If it is, deactivate and reactivate again. If it is not, do nothing.
dpmsStatus="$(xset -q | grep -ce 'DPMS is Enabled')"
if [[ ${dpmsStatus} -eq 1 ]]; then xset -dpms; fi
}
function manage_pid() {
pid="${$}"
if [[ -e "${LOCKFILE}" ]]; then
if [[ ! -d /proc/$(cat "${LOCKFILE}") ]]; then
rm "${LOCKFILE}"
stop
else
pid_lf=$(<"${LOCKFILE}")
fi
if [[ ${pid} && ${pid_lf} ]]; then
if [[ "${delay}" == "stop" ]]; then pid=${pid_lf}; fi
stop
fi
else
echo "${pid}" > "${LOCKFILE}"
fi
}
function start() {
manage_pid
test_delay
icon_into_systray
detect_id_displays
detect_screensaver_used
while true; do
checkDelayProgs
checkFullscreen
sleep "${delay}"
done
}
function stop() {
kill -9 "${pid}"
if [[ -e "${LOCKFILE}" ]]; then rm "${LOCKFILE}"; fi
exit 0
}
function test_delay() {
# If argument empty, use 50 seconds as default.
if [ -z "${delay}" ]; then delay=50; fi
if [[ ${delay} = *[^0-9]* ]]; then
# If argument is not integer quit.
echo "The Argument '${delay}' is not valid, not an integer"
echo "Please use the time in seconds you want the checks to repeat."
echo "You want it to be ~10 seconds less than the time it takes your screensaver or DPMS to activate"
exit 1
fi
}
start