]> git.rmz.io Git - dotfiles.git/blob - bin/suspend_screensaver
e1081f4e970c013b142c19c89cbd033b584bc10a
[dotfiles.git] / bin / suspend_screensaver
1 #!/bin/bash
2 # lightsOn.sh
3
4 # Copyright (c) 2011 iye.cba at gmail com
5 # url: https://github.com/iye/lightsOn
6 # This script is licensed under GNU GPL version 2.0 or above
7
8 # Description: Bash script that prevents the screensaver and display power
9 # management (DPMS) to be activated when you are watching Flash Videos
10 # fullscreen on Firefox and Chromium.
11 # Can detect mplayer and VLC when they are fullscreen too but I have disabled
12 # this by default.
13 # lightsOn.sh needs xscreensaver or kscreensaver to work.
14
15 # HOW TO USE: Start the script with the number of seconds you want the checks
16 # for fullscreen to be done. Example:
17 # "./lightsOn.sh 120 &" will Check every 120 seconds if Mplayer,
18 # VLC, Firefox or Chromium are fullscreen and delay screensaver and Power Management if so.
19 # You want the number of seconds to be ~10 seconds less than the time it takes
20 # your screensaver or Power Management to activate.
21 # If you don't pass an argument, the checks are done every 50 seconds.
22
23
24 # Modify these variables if you want this script to detect if Mplayer,
25 # VLC or Firefox Flash Video are Fullscreen and disable
26 # xscreensaver/kscreensaver and PowerManagement.
27 mplayer_detection=0
28 vlc_detection=0
29 firefox_flash_detection=1
30 chromium_flash_detection=1
31
32
33 # YOU SHOULD NOT NEED TO MODIFY ANYTHING BELOW THIS LINE
34
35 [[ $(which xdg-screensaver 2> /dev/null) ]] || { echo "You need to install xdg-utils!"; exit 1; }
36
37 # enumerate all the attached screens
38 displays=""
39 while read id
40 do
41 displays="$displays $id"
42 done< <(xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p')
43
44 delayed=0
45 checkFullscreen()
46 {
47 # loop through every display looking for a fullscreen window
48 for display in $displays
49 do
50 #get id of active window and clean output
51 activ_win_id=`DISPLAY=:0.${display} xprop -root _NET_ACTIVE_WINDOW`
52 #activ_win_id=${activ_win_id#*# } #gives error if xprop returns extra ", 0x0" (happens on some distros)
53 activ_win_id=${activ_win_id:40:9}
54
55 # Skip invalid window ids (commented as I could not reproduce a case
56 # where invalid id was returned, plus if id invalid
57 # isActivWinFullscreen will fail anyway.)
58 #if [ "$activ_win_id" = "0x0" ]; then
59 # continue
60 #fi
61
62 # Check if Active Window (the foremost window) is in fullscreen state
63 isActivWinFullscreen=`DISPLAY=:0.${display} xprop -id $activ_win_id | grep _NET_WM_STATE_FULLSCREEN`
64 if [[ "$isActivWinFullscreen" = *NET_WM_STATE_FULLSCREEN* ]];then
65 isAppRunning
66 var=$?
67 if [[ $delayed -eq 0 && $var -eq 1 ]];then
68 delayScreensaver
69 delayed=1
70 elif [[ $delayed -eq 1 && $var -ne 1 ]]; then
71 enableScreensaver
72 delayed=0
73 fi
74
75 fi
76 done
77 }
78
79
80
81
82
83 # check if active windows is mplayer, vlc or firefox
84 #TODO only window name in the variable activ_win_id, not whole line.
85 #Then change IFs to detect more specifically the apps "<vlc>" and if process name exist
86
87 isAppRunning()
88 {
89 #Get title of active window
90 activ_win_title=`xprop -id $activ_win_id | grep "WM_CLASS(STRING)"` # I used WM_NAME(STRING) before, WM_CLASS more accurate.
91
92
93 # Check if user want to detect Video fullscreen on Firefox, modify variable firefox_flash_detection if you dont want Firefox detection
94 if [ $firefox_flash_detection == 1 ];then
95 if [[ "$activ_win_title" = *unknown* || "$activ_win_title" = *plugin-container* ]];then
96 # Check if plugin-container process is running
97 flash_process=`pgrep -l plugin-containe | grep -wc plugin-containe`
98 #(why was I using this line avobe? delete if pgrep -lc works ok)
99 #flash_process=`pgrep -lc plugin-containe`
100 if [[ $flash_process -ge 1 ]];then
101 return 1
102 fi
103 fi
104 fi
105
106
107 # Check if user want to detect Video fullscreen on Chromium, modify variable chromium_flash_detection if you dont want Chromium detection
108 if [ $chromium_flash_detection == 1 ];then
109 if [[ "$activ_win_title" = *exe* ]];then
110 # Check if Chromium Flash process is running
111 flash_process=`pgrep -fl "chromium --type=plugin --plugin-path=/usr/lib/mozilla/plugins/libflashplayer.so" | wc -l`
112 if [[ $flash_process -ge 1 ]];then
113 return 1
114 fi
115 fi
116 fi
117
118
119 #check if user want to detect mplayer fullscreen, modify variable mplayer_detection
120 if [ $mplayer_detection == 1 ];then
121 if [[ "$activ_win_title" = *mplayer* || "$activ_win_title" = *MPlayer* ]];then
122 #check if mplayer is running.
123 mplayer_process=`pgrep -l mplayer | grep -wc mplayer`
124 if [ $mplayer_process -ge 1 ]; then
125 return 1
126 fi
127 fi
128 fi
129
130
131 # Check if user want to detect vlc fullscreen, modify variable vlc_detection
132 if [ $vlc_detection == 1 ];then
133 if [[ "$activ_win_title" = *vlc* ]];then
134 #check if vlc is running.
135 vlc_process=`pgrep -l vlc | grep -wc vlc`
136 if [ $vlc_process -ge 1 ]; then
137 return 1
138 fi
139 fi
140 fi
141
142
143 return 0
144 }
145
146
147 delayScreensaver()
148 {
149
150 # reset inactivity time counter so screensaver is not started
151 xdg-screensaver suspend $activ_win_id
152
153 #Check if DPMS is on. If it is, deactivate and reactivate again. If it is not, do nothing.
154 dpmsStatus=`xset -q | grep -ce 'DPMS is Enabled'`
155 if [ $dpmsStatus == 1 ];then
156 xset -dpms
157 xset dpms
158 fi
159
160 }
161
162 enableScreensaver()
163 {
164 xdg-screensaver resume $activ_win_id
165 }
166
167
168 delay=$1
169
170
171 # If argument empty, use 50 seconds as default.
172 if [ -z "$1" ];then
173 delay=50
174 fi
175
176
177 # If argument is not integer quit.
178 if [[ $1 = *[^0-9]* ]]; then
179 echo "The Argument \"$1\" is not valid, not an integer"
180 echo "Please use the time in seconds you want the checks to repeat."
181 echo "You want it to be ~10 seconds less than the time it takes your screensaver or DPMS to activate"
182 exit 1
183 fi
184
185
186 while true
187 do
188 checkFullscreen
189 sleep $delay
190 done
191
192
193 exit 0