PDA

View Full Version : HOWTO: Disable screen saver while Flash is running


sdennie
March 8th, 2009, 09:38 AM
Summary
When playing Flash video in Firefox, the screen saver is not disabled and so you are required to move your mouse every few minutes to prevent the screen saver from turning on. This is highly annoying. This guide provides a method to automatically disable the screen saver whenever Flash is being used in Firefox.

Note: This method disables the screen saver regardless of what type of Flash program is running. It doesn't discriminate between a Youtube video and a Punch the Monkey ad.

Implementation
We are going to create a daemon that detects if the Flash shared library currently has any memory mapped. If it does, we disable the screen saver. If it doesn't, and we know that we previously disabled the screen saver, we turn it back on. First, we need to make sure we have a local bin directory:


mkdir -p ~/bin


Next, save the following script as ~/bin/flash_saver.sh:


#!/bin/bash

# Cleanup any bad state we left behind if the user exited while flash was
# running
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true

we_turned_it_off=0

while true; do
sleep 60
flash_on=0

for pid in `pgrep firefox` ; do
if grep libflashplayer /proc/$pid/maps > /dev/null ; then
flash_on=1
fi

ss_on=`gconftool-2 -g /apps/gnome-screensaver/idle_activation_enabled`

if [ "$flash_on" = "1" ] && [ "$ss_on" = "true" ]; then
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled \
--type bool false
we_turned_it_off=1
elif [ "$flash_on" = "0" ] && [ "$ss_on" = "false" ] \
&& [ "$we_turned_it_off" = "1" ]; then
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled \
--type bool true
we_turned_it_off=0
fi

done
done


Next, we make the script executable:


chmod +x ~/bin/flash_saver.sh


Now, we can turn it on by pressing F2 and running, "~/bin/flash_saver.sh". You can now either test it watching some Flash videos or just proceed to System->Preferences->Session and adding "flash_saver.sh" as one of your startup programs.

Testing
Watch some long Flash videos and verify that screen saver doesn't come on. Afterwards, close all tabs that have any Flash content on them and see if the screen saver now comes on as normal.

binbash
March 9th, 2009, 06:42 AM
thanks, it is very handy when watching movies

jackmcslay
March 9th, 2009, 01:41 PM
which part of this script I need if I just need a terminal command to disable the screen saver?

jkernsjr
March 9th, 2009, 11:12 PM
sdennie, fantastic job with the script..thanks! One problem though. All parts of script seem to work except when I no longer have flash playing, or even close the browser all together, screensaver will not enable itself back to on?

jackmcslay, see my post #7 here: http://ubuntuforums.org/showthread.php?p=6858013#post6858013

sdennie
March 14th, 2009, 04:46 PM
sdennie, fantastic job with the script..thanks! One problem though. All parts of script seem to work except when I no longer have flash playing, or even close the browser all together, screensaver will not enable itself back to on?

jackmcslay, see my post #7 here: http://ubuntuforums.org/showthread.php?p=6858013#post6858013

I'm not sure what would cause this unless some other application has turned off the screen saver. The script goes out of its way to not touch things if something else has changed the settings. The other possibility is that you have some hung firefox process somewhere that is using flash but you can't see it.

h2o4444
March 18th, 2009, 08:25 AM
Thank you very much for this How-To but I don't know how to write codes and using the command line is no natural to me. I'm going to wait for Ubuntu 9.04 to come out and hope that this problem is solved in that version. Also this problem doesn't only occur in Flash, it occurs whenever I'm watching a video (full screen or not) such as on youtube, or just a regular mpg or avi file.

aktiwers
March 18th, 2009, 09:51 AM
Will do this when I get home.. this really should be default setting IMO

Thelasko
March 18th, 2009, 11:02 AM
Any way to only do this only while flash is in full screen, or only on certain websites?

Good work by the way!

sdennie
April 7th, 2009, 04:42 PM
Didn't notice these questions. Sorry for the late replies:

which part of this script I need if I just need a terminal command to disable the screen saver?

To disable:


gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool false


To enable:


gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool false


Any way to only do this only while flash is in full screen, or only on certain websites?

Good work by the way!

You should be able to. It's a bit more work than I'm willing to put into it but, here is a snippet I've pulled out of another script I wrote that only does stuff when a window named "foo" is active:


active_win_line=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)")
active_win_id="${active_win_line:40}"

if xwininfo -id $active_win_id | grep "foo" > /dev/null ; then
# Do stuff
fi

Boppy3125
May 11th, 2009, 10:33 PM
I don't know if you are still watching this thread but I am having some difficulty with this. It looks like the script is working and setting the gconftool-2 setting correctly but the screen saver is still starting. I am running MythBuntu 8.10, pretty much vanila. I have the screen saver set to start after like 2 minutes. MythTV correctly stops it from activating. Here is the output from some commands while firefox has a hulu program playing and the screensaver is running. (I got these out of a putty session connected.)
curt@media1:~$ ps -A | grep flash_saver.sh
5908 ? 00:00:00 flash_saver.sh
curt@media1:~$ pgrep firefox
17662
curt@media1:~$ grep libflashplayer /proc/17662/maps
b0990000-b12e2000 r-xp 00000000 08:01 724861 /usr/lib/adobe-flashplugin/libflashplayer.so
b12e2000-b1316000 rw-p 00951000 08:01 724861 /usr/lib/adobe-flashplugin/libflashplayer.so
curt@media1:~$ gconftool-2 -g /apps/gnome-screensaver/idle_activation_enabled
false


Now, that last setting never seems to go back to true, is that a problem? Anyone see anything obviously wrong?

--
[edit - research at work]
Looks like it is possible Mythbuntu based on Xbuntu uses xscreensaver instead of gnome-screensaver. It looks like simply executing "xscreensaver-command -deactivate" periodically may provide a simulated mouse/keyboard movement. Issuing this every 60 seconds while flash is active could be an easy modification to that script. I will try changing it and report back when I have the chance to try this.

Boppy3125
May 15th, 2009, 06:55 AM
Well, it is weird. I do have the gnome-screensaver. I have the original script in place. It doesn't seem like it worked right in testing, it definitely seemed as though any interaction using putty was making it less predictable. Even taking that out of the equation, after shutting down firefox, the script properly changes idle_activation_enabled setting back to true, but then the screensaver doesn't seem to come back on.

Then I gave up with it in that state, next day I noticed the screensaver seems to be working. So I guess it is working.

rfourie
July 10th, 2009, 10:34 AM
Hi, after much searching of the net/forums/mailinglists/etc, I managed to find a way to determine if the current window is full screen. I haven't tested it extensively, so your results may vary, but I think it's relatively robust. I've also changed some of the variables to make it a bit more generic (and added a test for xine, my default dvd/movie player), and also turned off sleep/hibernate.


#!/bin/bash

# cleanup any bad state we left behind if the user exited while flash was running
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true

turn_it_off=0
sleepcomputer0=`gconftool-2 -g /apps/gnome-power-manager/timeout/sleep_computer_ac`
sleepdisplay0=`gconftool-2 -g /apps/gnome-power-manager/timeout/sleep_display_ac`

while true; do
sleep 60
SS_off=0

# check to see if flashplayer is being used by firefox
for pid in `pgrep firefox` ; do
if grep libflashplayer /proc/$pid/maps > /dev/null ; then
SS_off=1
fi
done

# check to see if xine is being used
if pgrep xine > /dev/null; then
SS_off=1
fi

# check to see if current application is fullscreen
current_window_id=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d" " -f5`
if xprop -id $current_window_id | grep "_NET_WM_STATE_FULLSCREEN" > /dev/null; then
SS_off=1
fi

# read current state of screensaver
ss_on=`gconftool-2 -g /apps/gnome-screensaver/idle_activation_enabled`

# change state of screensaver as necessary
if [ "$SS_off" = "1" ] && [ "$ss_on" = "true" ]; then
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool false
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_computer_ac --type int 0
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_display_ac --type int 0
turn_it_off=1
elif [ "$SS_off" = "0" ] && [ "$ss_on" = "false" ] && [ "$turn_it_off" = "1" ]; then
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_computer_ac --type int $sleepcomputer0
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_display_ac --type int $sleepdisplay0
turn_it_off=0
fi

done

Thelasko
July 10th, 2009, 02:32 PM
Thanks, I'll have to give that a try.

Thelasko
July 11th, 2009, 05:43 PM
I gave the new script a try. Unfortunately, it still works whenever flash is running, and not just while in full screen. I'll try to take a look at it and figure out what's wrong.

rfourie
July 12th, 2009, 01:23 AM
I gave the new script a try. Unfortunately, it still works whenever flash is running, and not just while in full screen. I'll try to take a look at it and figure out what's wrong.

Thelasko, do you mean that it doesn't disable the screensaver when flash is running, or that it doesn't disable the screensaver when the current application is full screen, or both?

Thelasko
July 12th, 2009, 09:19 AM
Thelasko, do you mean that it doesn't disable the screensaver when flash is running, or that it doesn't disable the screensaver when the current application is full screen, or both?

No, it works. It's just that I was thinking it would disable the screensaver while flash was running and full screen. However, the script is written so the screensaver is disabled while flash is running or an application is full screen.

I've made my own modification to the script. It seems to work pretty well, but I'm aware of one bug. It will disable the screensaver if flash is running and any application is full screen.

#!/bin/bash

# cleanup any bad state we left behind if the user exited while flash was running
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true

turn_it_off=0
sleepcomputer0=`gconftool-2 -g /apps/gnome-power-manager/timeout/sleep_computer_ac`
sleepdisplay0=`gconftool-2 -g /apps/gnome-power-manager/timeout/sleep_display_ac`

while true; do
sleep 60
SS_off=0

# check to see if flashplayer is being used by firefox
for pid in `pgrep firefox` ; do
if grep libflashplayer /proc/$pid/maps > /dev/null ; then
# check to see if current application is fullscreen
current_window_id=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d" " -f5`
if xprop -id $current_window_id | grep "_NET_WM_STATE_FULLSCREEN" > /dev/null; then
SS_off=1
fi
fi
done

# check to see if xine is being used
# if pgrep xine > /dev/null; then
# SS_off=1
# fi
#
# # check to see if current application is fullscreen
# current_window_id=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d" " -f5`
# if xprop -id $current_window_id | grep "_NET_WM_STATE_FULLSCREEN" > /dev/null; then
# SS_off=1
# fi

# read current state of screensaver
ss_on=`gconftool-2 -g /apps/gnome-screensaver/idle_activation_enabled`

# change state of screensaver as necessary
if [ "$SS_off" = "1" ] && [ "$ss_on" = "true" ]; then
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool false
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_computer_ac --type int 0
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_display_ac --type int 0
turn_it_off=1
elif [ "$SS_off" = "0" ] && [ "$ss_on" = "false" ] && [ "$turn_it_off" = "1" ]; then
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_computer_ac --type int $sleepcomputer0
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_display_ac --type int $sleepdisplay0
turn_it_off=0
fi

done

Also note that I commented out the part about Xine. I don't use Xine so I disabled it. I might change that to work with Miro instead.

rfourie
July 12th, 2009, 09:23 AM
Oh, cool. This is my first real fix and I'm a bit proud of it...
I did make it pretty generic for full-screen applications so that no matter what is playing full screen, it disables the screensaver. I might post to other threads about that part of the solution.

Lockheed
August 22nd, 2009, 10:32 AM
I was trying to adopt it to mplayer so my screen doesn't dim while playing movies but it didn't work. I modified two lines:
for pid in `pgrep mplayer` ; do
if grep smplayer /proc/$pid/maps > /dev/null ; then
flash_on=1
but the screen still dims :/

Boppy3125
August 23rd, 2009, 08:32 PM
Two thoughts without looking up anything. Isn't MPlayer case sensitive? Maybe that grep isn't. Can't MPlayer do that with some command line switch?

Hope that at least gets you googling in the right direction.

Lockheed
August 24th, 2009, 01:25 AM
Well, smplayer has such optionin the menus and I was already using it but with no effect. However, yesterday, this option starded to work spontaneously (after a month!) so I don't need the script anymore.

Gen2ly
August 24th, 2009, 11:21 AM
This script doesn't detect flash but will suspend the screen saver for KDE 4, disable xorg server blanking, and disable dpms.

#!/bin/bash
# screenblank - Toggle screen blanking/powersaving/screensaver on/off in KDE 4.
# Useful for when watching movies...

# Tests for X server blanking / Monitor blanking
xblanktest=$(xset -q | grep timeout | awk '{printf $2}')
dpmstest=$(xset -q | grep " DPMS is Enabled")

if [[ "$xblanktest" -gt 0 ]] || [[ -n "$dpmstest" ]]; then
# Turn off X blanking, Monitor blanking
xset s off; xset -dpms
# Supend screensaver
echo '#!/bin/bash' > /tmp/suspend-dbus-screensaver
echo 'while :' >> /tmp/suspend-dbus-screensaver
echo 'do' >> /tmp/suspend-dbus-screensaver
echo 'qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity' \
>> /tmp/suspend-dbus-screensaver
echo 'sleep 119' >> /tmp/suspend-dbus-screensaver
echo 'done' >> /tmp/suspend-dbus-screensaver
chmod u+x /tmp/suspend-dbus-screensaver
nohup "/tmp/suspend-dbus-screensaver" &> /dev/null &
echo " - Disabled screen blanking, powersaving, and screensaver"; else
# Resume X blanking, Monitor blanking
xset s on; xset +dpms
# Resume screensaver
pkill suspend-dbus
echo " + Enabled screen blanking and powersaving, and screensaver"
fi

# Limitations:
# Unable to tell if screensaver is active
# qdbus org.freedesktop.ScreenSaver /ScreenSaver GetActive always returns
# false
# Enables both X server blanking and DPMS even if one is orginally turned off

rvm4000
August 25th, 2009, 07:26 AM
Well, smplayer has such optionin the menus and I was already using it but with no effect. However, yesterday, this option starded to work spontaneously (after a month!) so I don't need the script anymore.

From the smplayer FAQ:

21. The screensaver doesn't turn off, why?

If you use a recent version of MPlayer you may need to add a line like this in your ~/.mplayer/config:
(gnome)
heartbeat-cmd="gnome-screensaver-command -p &>/dev/null"
(kde)
heartbeat-cmd="dcop kdesktop KScreensaverIface enable false &>/dev/null && dcop kdesktop KScreensaverIface enable true &>/dev/null"
Please take a look at the MPlayer manpage for more info.

Lockheed
August 25th, 2009, 07:34 AM
From the smplayer FAQ:

I will try that cause the dimming is back again for no apparent reason.

Whoopie
September 17th, 2009, 11:20 AM
Hi,

there's also a new project called Caffeine (https://code.launchpad.net/caffeine).

Best regards,
Whoopie

Thelasko
September 29th, 2009, 08:08 PM
I've been having some trouble with the script not exiting cleanly. Basically, when I close a Flash movie the screen saver remains disabled.

Does anybody have any suggestions to fix this?

Thelasko
September 30th, 2009, 10:35 AM
What is the while loop for? Why does it say "while true" while what is true?

Edit: Nevermind
Note the use of the true statement. This means: continue execution until we are forcibly interrupted (with kill or Ctrl+C).

Thelasko
September 30th, 2009, 11:50 AM
I think I fixed it!

#!/bin/bash

# cleanup any bad state we left behind if the user exited while flash was running
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true

turn_it_off=0
sleepcomputer0=`gconftool-2 -g /apps/gnome-power-manager/timeout/sleep_computer_ac`
sleepdisplay0=`gconftool-2 -g /apps/gnome-power-manager/timeout/sleep_display_ac`

while true; do
sleep 60
SS_off=0

# check to see if flashplayer is being used by firefox
for pid in `pgrep firefox` ; do
if grep libflashplayer /proc/$pid/maps > /dev/null ; then
# check to see if current application is fullscreen
current_window_id=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d" " -f5`
if xprop -id $current_window_id | grep "_NET_WM_STATE_FULLSCREEN" > /dev/null; then
SS_off=1
fi
fi
done

# check to see if xine is being used
if pgrep miro > /dev/null; then
current_window_id=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d" " -f5`
if xprop -id $current_window_id | grep "_NET_WM_STATE_FULLSCREEN" > /dev/null; then
SS_off=1
fi
fi
#
# # check to see if current application is fullscreen
# current_window_id=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d" " -f5`
# if xprop -id $current_window_id | grep "_NET_WM_STATE_FULLSCREEN" > /dev/null; then
# SS_off=1
# fi

# read current state of screensaver
ss_on=`gconftool-2 -g /apps/gnome-screensaver/idle_activation_enabled`

# change state of screensaver as necessary
if [ "$SS_off" = "1" ] && [ "$ss_on" = "true" ]; then
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool false
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_computer_ac --type int 0
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_display_ac --type int 0
turn_it_off=1
# I think this is the problem. Some states are not accounted for, so we use else instead
# elif [ "$SS_off" = "0" ] && [ "$ss_on" = "false" ] && [ "$turn_it_off" = "1" ]; then
else
gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_computer_ac --type int $sleepcomputer0
gconftool-2 -s /apps/gnome-power-manager/timeout/sleep_display_ac --type int $sleepdisplay0
turn_it_off=0
fi

done

edit: Never mind, this doesn't work at all.

javajesse
November 19th, 2009, 01:45 PM
Its a manual click, but I use the Inhibit Applet on the Panel. Quick and easy to disable / enable screensaver & sleep.

Thelasko
November 19th, 2009, 04:35 PM
If you can get it to work, Hulu Desktop automatically disables the screen saver.