102

My screen brightness used to dim after a few seconds to preserve battery. This is default in Ubuntu 12.04. However when watching video it should not dim.

This works correctly when I watch videos using native applications like VLC. With in-browser video, however, the screen is not prevented from dimming. This is very annoying as you have to move your cursor every 10 seconds or so.

I used to use Mac OSX where I had the same dimming settings and Flash videos were taken into account correctly.

Anyone an idea how you can make YouTube prevent your screen from dimming?

4
  • 4
    So, since this is a problem that most Ubuntu users want a solution for, can we consider this a bug in the OS or a bug in the software (Flash, Chrome, Firefox) and get a fix so that every user doesn't need to come to this AskUbuntu page?
    – blong
    Feb 11, 2015 at 3:07
  • 1
    I appreciate all the help but this is ridiculous that it happens on 2016. Come on, these are basic stuff that aren't supported. Also HTML5 Video - why is it so slow and buggy? Is it such a huge problem to fix..? It was supposed to replace Flash.. now it seems ridiculous.
    – Dor
    Mar 13, 2016 at 21:23
  • Personally, I appreciate that my browser can't prevent my computer from locking, so I do not believe this is a bug. What you're asking for could leave your computer in a less secure setup.
    – earthmeLon
    Jun 5, 2016 at 17:09
  • @earthmeLon or anyone, can you explain how will it be less safe?
    – scadge
    Jan 27, 2018 at 13:39

20 Answers 20

54

HOWTO: Disable screen saver while Flash is running

Create a bash text file containing

#!/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

Make the script executable by running:

chmod +x ~/path/to/script/flash_saver.sh

Run the script:

~/path/to/script/flash_saver.sh

If you prefer, you can set this script to run at logon by doing the following:

  1. Run the program "Startup Applications"
  2. Click "Add"
  3. Under name type "FlashMonitor" or something you will recognise
  4. Under command type the path to the script
  5. Under comment (if you want) type a brief description. E.g. "Stops screen turning off when Flash is running"

Source: HOWTO: Disable screen saver while Flash is running - ubuntuforums

14
  • 4
    If I could I'd vote this up +5. The script from the ubuntu forum is incredibly well written as is your answer. Also big thumb up for actually including the source.
    – con-f-use
    Aug 2, 2012 at 13:18
  • 51
    xkcd.com/196
    – SpellingD
    Aug 2, 2012 at 17:23
  • 4
    This indeed only works for Firefox. Anyone able to rewrite this for multiple-browser support? Oct 16, 2012 at 15:12
  • 3
    will it work with html5 video player?
    – kdureidy
    May 9, 2014 at 0:29
  • 2
    HTML5 video support is needed indeed.
    – ulidtko
    Jun 10, 2014 at 21:25
32
+50

EDIT

This probably will not work if you are using an Ubuntu version newer than 12.04 (have tried in 13.04 and 13.10 and it just does not work at all). It seems the main developer is not working anymore in this project, so the chances of it being fixed are not good.


In my system (Ubuntu 11.10) i use Caffeine. You can try if it works in Xubuntu too. It adds a notification area icon where you can enable/disable screensaver for some programs. To install Caffeine, do the following:

sudo add-apt-repository ppa:caffeine-developers/ppa
sudo apt-get update
sudo apt-get install caffeine

Then execute in a terminal:

caffeine -p &

You can then choose the programs that should disable the screensaver:
vlc for VLC, mplayer for Movie Player, etc.

Caffeine Preferences

Hope it helps.

6
  • This is running pretty fine on 12.04, Thank you! Sep 24, 2012 at 19:47
  • Thanks for contributing a solution, but this is the best solution? Is this not a bug? It's got to be a bug. This happens on gnome, but not on unity. Feb 27, 2013 at 15:47
  • I am now on 12.04 and I still use that application, not to stop the screensaver but to disable decrease of screen brightness quickly. For what I saw if it is a bug, it is in the browser: it should be his responsibility to inhibit the screensaver when in fullscreen.
    – Salem
    Mar 3, 2013 at 19:00
  • Doesn't install in 13.10
    – NoBugs
    Dec 7, 2013 at 4:36
  • 1
    This answer needs to be updated, since Caffeine even works in Ubuntu 16.04 Xenial. ;) Jun 5, 2016 at 17:06
14

There is a nice little script on GitHub called lightsOn which should do the trick you want.

Basically it looks for full screen video (flash in firefox or chromium, mplayer or vlc) and if so disable xscreensaver and also the auto power-manager dim screen capability.

from the script itself:

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,
 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.

Thus call the script from your autostart folder as per my answer here.

Adjust the script for whether you are running flash/vlc/mplayer

remember to give the script execute rights to run i.e.

chmod +x lightsOn.sh
1
12

Not entirely what you're after but this little script would register a mouse movement each time it was run. You could drop it into cron to run once every x-amount-of-time. That would stop the screen from turning off, and a 1 1 movement of the mouse is barely noticeable if it runs when you're NOT watching a video

#!/bin/bash
#move the mouse
xte 'mousermove 1 1' 

I totally got this idea from an xkcd comic by the way. http://xkcd.com/196/

0
7

For a pre-rolled equivalent to the script supplied by @njallam, with a neat little Gnome UI element, try the Caffeine applet:

https://launchpad.net/caffeine
http://ubuntu-tweak.com/app/caffeine/

2
  • I had Caffeine installed but didn't see you had to activate it to let it work. Thanks! Aug 4, 2012 at 9:59
  • Info from Caffeine developers: “Flash video support is gone, as it was too complicated to keep it working.” So you can forget Flash video checkbox in its settings. However, if you are using Firefox, add plugin-container to the list of programs and Caffeine will activate on Flash videos properly (deactivation will be delayed slightly, as plugin-container doesn't unload immediately when you close the tab with video, it takes several minutes).
    – whtyger
    Mar 18, 2014 at 8:43
6
+50

Webupd8 had an article about Caffeine and Lightson.sh to disable the screen saver while Flash or HTML5 videos are playing. The Lightson script works for both Firefox and Chrome and can be set to include VLC. I hope this will help.

In case the code provided by Webupd8 to install the script follows:

mkdir -p ~/scripts && cd ~/scripts

wget https://raw.github.com/hotice/lightsOn/master/lightsOn.sh

chmod +x lightsOn.sh
2
  • 1
    The method used in that script (lightsOn.sh) to detect if the browser utilizes HTML5 player is not really good. In fact, the method determines if Firefox or Chromium is in full-screen... In rest the article is good and your answer is most approached about what I asked. Jun 11, 2013 at 18:20
  • 1
    I have been to doing a little more research found a script based of CPU or Network activity. It located at on the Mint Forums it might give you a little better control. Enjoy watching YouTube.
    – Garry
    Jun 12, 2013 at 2:00
5

You should not only check the screensaver options, but also the power management options, especially the "Put display to sleep when inactive for ..." option.

Note that on laptops, there are two settings with that name: when the laptop is running on AC power and when it is running on battery.

2

Use Caffeine : ppa page.

How to install:

sudo apt-add-repository ppa:caffeine-developers/ppa 
sudo apt-get update
sudo apt-get install caffeine

How to run:

  1. Hit Alt + F2, then type caffeine.

  2. Click the "Caffeine" icon, then click "Disable Screensaver".

3
  • 1
    I don't intend to disable screensaver. Anyway, I tried caffeine, but doesn't work in any way. I don't think that is updated to the latest version of Gnome. Jun 4, 2013 at 15:57
  • @RaduRădeanu caffeine disables the screensaver of your computer it has nothing to do with your web browser.
    – Alvar
    Jun 4, 2013 at 16:02
  • @Alvar It has if you wish... Anyway, didn't worked for me. Jun 4, 2013 at 16:06
2

A little old, but here it is with multiple browsers support:

#!/bin/bash -eu

# List your browsers here
browsers_list=( "firefox" "chrome" "chromium" "opera" )

# 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

idle_off=0

while true; do

    sleep 60

    for browser in "${browsers_list[@]}" ; do
    for pid in `pgrep $browser` ; do

        flash_on=0
        if [ -O /proc/$pid/maps ] && 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
            idle_off=1
        elif [ "$flash_on" = "0" ] && [ "$ss_on" = "false" ] && [ "$idle_off" = "1" ]; then
            gconftool-2 -s /apps/gnome-screensaver/idle_activation_enabled --type bool true
            idle_off=0
        fi

    done
    done

done

Just save the script somewhere, chmod +x it, and add it to the StartupApplications. There might be a way to list all available browsers using something like:

update-alternatives --list x-www-browser

But I don't know how to handle the fact that some executables don't have the name than their processes (for instance /usr/bin/google-chrome runs as chrome).

0
2

For those who are using a screensaver other than gnome-screensaver (which is the default screensaver in Ubuntu starting with the 11.10 version) this answer may be helpful.

Personally, I didn't change out gnome-screensaver for something else and, I don't know for what reasons, Caffeine doesn't work for me in any way (maybe is outdated).

That being said, starting from others' bash scripts (this, this and this), I managed to make a new script, gnome_screensaver_off.sh that works perfectly for what I intended when YouTube is using Flash player for rendering videos.

Disable gnome-screensaver while YouTube is using Flash player

Open a terminal and run next commands followed by instructions:

  • mkdir -p bin - this command will make a bin directory in your home folder if you don't already have it.
  • gedit ~/bin/gnome_screensaver_off.sh -this will create the new file gnome_screensaver_off.sh in gedit.
  • Copy and paste there next script:
#!/bin/bash

# Getting the previous configuration
back=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-display-ac)

screensaver_is_off=0
delay=$[$back - 10]

# Defining the restore function
function restore {
    gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac $back
    gsettings set org.gnome.settings-daemon.plugins.power sleep-display-battery $back
    gsettings set org.gnome.desktop.session idle-delay $back

    bmin=$[$back / 60]
    #notify-send "Screen sleep time restored to $bmin minutes."

    notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "Screen sleep time restored to $bmin minutes."
}

# Defining the disable_screensaver function
function disable_screensaver {
    # Making sure the user don't mess up...
    trap 'restore && exit 0' INT HUP

    # Disabling sleep time
    gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 0
    gsettings set org.gnome.settings-daemon.plugins.power sleep-display-battery 0
    gsettings set org.gnome.desktop.session idle-delay 0

    notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "Screen sleep time turned off."
}

while true; do
    if [ "$delay" -le "50" ]; then
        sleep 50
    else
        sleep $delay
    fi
    flash_on=0

    #Check if Chrome, Chromium or Firefox is open with a flash video running on it
    if pgrep -lfc ".*((c|C)hrome|chromium|firefox|).*flashp.*" > /dev/null ; then
            flash_on=1
    else
        flash_on=0  
    fi

    if [ "$flash_on" -eq "1" ] && [ "$back" -ne  "0" ] && [ "$screensaver_is_off" -eq "0" ]; then
        disable_screensaver
        screensaver_is_off=1
    elif [ "$flash_on" -eq "0" ] && [ "$screensaver_is_off" -eq "1" ]; then
        restore
        screensaver_is_off=0
    fi
done

exit 0
  • Save the file and close it.
  • Go back into terminal and run: chmod +x gnome_screensaver_off.sh - to grant execute access for the script.
  • To run your new script, run ~/bin/gnome_screensaver_off.sh.

If you prefer, you can set this script to run at logon by doing the following:

  1. Search in Dash for Startup Applications, open it and click Add.
  2. Under name type "YouTube Monitor" or something you will recognise.
  3. Under command type "/home/$USER/bin/gnome_screensaver_off.sh" (change $USER with your username).
  4. Under comment (if you want), type a brief description (e.g. "Stops screen turning off when Flash player is running in browser").

Disable gnome-screensaver while YouTube is using HTML5 player

The question remains open in this sense...

Thanks for the suggestions offered by: fossfreedom, Meer Borg, njallam, desgua and others.

0
2

Solution for HTML5 playback in Chrome

Overview

The following script disables the screensaver if it finds an active full-screen Chrome session. By default it will run a check every 5 minutes. The script will automatically resume the screensaver if either of the following three conditions are met:

  • window is not full-screen
  • window is not in focus
  • window does not exist anymore

Script

#!/bin/bash

## Variables ##

ChromeWmClass="Google-chrome"
FullScreenProperty="_NET_WM_STATE_FULLSCREEN"
CheckInterval="300" # check every 5 minutes

## Main ##

while true; do

  sleep "$CheckInterval"

  ActiveWinID=$(xdotool getactivewindow)

  if [[ -z "$ActiveWinID" ]]; then
    continue
  fi

  ActiveWinProp=$(xprop -id "$ActiveWinID")

  if echo "$ActiveWinProp" | grep "$FullScreenProperty" > /dev/null 2>&1 \
  && echo "$ActiveWinProp" | grep "WM_CLASS" | grep "$ChromeWmClass" > /dev/null 2>&1
    then
      if [[ -z "$SuspendingID" ]]; then
        echo "Suspending screensaver for $ActiveWinID"
        xdg-screensaver suspend "$ActiveWinID"
        SuspendingID="$ActiveWinID"
      elif [[ "$ActiveWinID" = "$SuspendingID" ]]; then
        echo "$ActiveWinID is already suspending  screensaver."
      elif [[ -n "$SuspendingID" ]]; then
        echo "Resuming screensaver for $SuspendingID"
        xdg-screensaver resume "$SuspendingID"
        echo "Suspending screensaver for $ActiveWinID"
        xdg-screensaver suspend "$ActiveWinID"
        SuspendingID="$ActiveWinID"
      fi
    else
      if [[ -n "$SuspendingID" ]] \
      && xprop -id "$SuspendingID" > /dev/null 2>&1
        then
          echo "Resuming screensaver for $SuspendingID"
          xdg-screensaver resume "$SuspendingID"
          SuspendingID=""
        else
          echo "No change."
      fi
  fi
done
2

The simplest nonintellectual approach which is desktop-agnostic and relies on xdotool would be to simulate a key stroke every N seconds.

I use a script called heartbeat.sh:

#!/bin/bash
while : 
do
    nice -n 1 xdotool key shift
    date +%T ## show some sign of life
    sleep 100
done

I launch the script while I play the video, and then stop it when it is not needed anymore.

xdotool can be installed with sudo apt-get install xdotool.

If you also want to have some kind of automatic audio detection, and you are ok with a PulseAudio-specific recipe, then below is a variation of the solution above that checks if audio is playing:

#!/bin/bash

while : 
do
    if [[ ! -z $(pacmd list-sink-inputs | grep RUNNING) ]] ; then
        echo 'Audio is playing. Inhibiting screensaver'
        nice -n 1 xdotool key shift ;
    else
        echo 'No audio detected'
    fi
    date +%T ## show some sign of life
    sleep 100
done

If using Alsa then the test would probably be something like (untested):

if [[ ! -z $(grep RUNNING /proc/asound/card*/pcm*/sub*/status) ]] ; then...

A potential downside of this solution is that your screen will not sleep even if you are just listening to music or radio, but I assume that in most cases this is not an issue.

1

For those that like full or manual control at their hands

This command line can set the screensaver delay time:

gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 0

Use "0" (zero) to keep it on or another value to define the delay in seconds.

The following script will keep the screen on until some key is pressed.

#!/bin/bash

# 
# To turn screen sleep time off for a while then back on
#
# by desgua 2013/01/27
#

# Getting the previous configuration
back=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-display-ac)

# Defining the restore function
function RESTORE {

gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac $back
gsettings set org.gnome.settings-daemon.plugins.power sleep-display-battery $back
gsettings set org.gnome.desktop.session idle-delay $back

bmin=$(echo "$back / 60" | bc)
#notify-send "Screen sleep time restored to $bmin minutes."
echo
echo "Screen sleep time restored to $bmin minutes."
echo 

exit 0
}

# Making sure the user don't mess up...
trap 'RESTORE && exit 0' INT HUP

# Disabling sleep time
gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 0
gsettings set org.gnome.settings-daemon.plugins.power sleep-display-battery 0
gsettings set org.gnome.desktop.session idle-delay 0

echo
echo "Screen sleep time turned off."
echo 

#notify-send "Screen sleep time turned off."

# Waiting for user to press some key and then restore the previous configuration
read -n 1 -p 'Press any key to turn it on again. ' b

RESTORE

exit 0

How to run a script:

  1. Copy the text into an empty file,
  2. Save the file,
  3. Make the file executable,
  4. Run it from a terminal.
1

The VLC in this PPA in addition to being a current version of VLC also correctly implements screen saver inhibition.

You can add this PPA using this command

sudo add-apt-repository ppa:n-muench/vlc
1
  • Many different questions appear to have been merged into this one. Check the edit history, and you may see that some of the questions included issues with the screen saver.
    – Rob
    Jul 27, 2016 at 17:45
0

You may simply execute/set this command while startup :

gconftool --type string --set /apps/compiz-1/plugins/fade/screen0/dim_unresponsive false   

Also can you try unchecking the hardware acceleration option on firefox?

1
  • 2
    I said this before: I don't intend to disable screensaver. I want only to prevent my screen from dimming when watching YouTube. Jun 10, 2013 at 6:58
0

For those using Ubuntu-Gnome 14.04 there is a simple yet useful Extension called Caffeine which provides a toggle icon which allows you to disable the light dimming and screen saver behaviour from the top bar with a click.

The extension can be installed following this link to the Gnome-Shell Extensions website:

https://extensions.gnome.org/extension/517/caffeine/

Source: comments' section in this article:

http://www.maketecheasier.com/prevent-ubuntu-sleep-while-watching-video/

0

This is my solution:

https://github.com/yanyingwang/shanghai-tools/blob/master/kit/the-bund-light.md

https://github.com/yanyingwang/shanghai-tools/blob/master/kit/the-bund-light.sh

Just download the the-bund-light.sh and add it to startbooting application:

sudo apt-get install xdotool

cd ~ && wget https://raw.githubusercontent.com/yanyingwang/shanghai-tools/master/kit/the-bund-light.sh

cat >> ~/.config/autostart/the-bund-light.sh.desktop <<EOF
[Desktop Entry]
Type=Application
Exec=$HOME/shanghai-tools/kit/the-bund-light.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[zh_CN]=TheBundLight
Name=TheBundLight
Comment[zh_CN]=
Comment=
EOF

CAUTION: the Exec's value in the code should be path of the-bund-light.sh in your computer.

0

It depends somewhat on what version of Ubuntu you are using. In 11.10, you can find "System Settings" in by clicking the gear icon in the top right corner of the screen. Select this, and in the dialog that pops up, select "Screen". You can set the amount of time that your computer will be idle before the screen blanks with the dropdown menu, or disable that feature by clicking the button labeled "Lock".

If you have an older system, you may have to look elsewhere for this setting, but it is there somewhere.

1
  • 4
    Yeah, but I do not want to disable it permanently.
    – ste_kwr
    Feb 20, 2012 at 10:38
0

I use the following script as a workaround:

#!/bin/bash
while true
do
   xdotool key Shift_L
   sleep 1m
done

It simulates a left shift key press once every minute, which prevents the screen from going dim. The drawback of this, of course, is that you have to run the script every time you watch youtube. On the other hand, it is simple and works with most, if not all, configurations.

You may need to install this

sudo apt-get install xdotool
0

The script at the above top does not work for me on Ubuntu 14.04 LTS Unity. Here its updated variant which works:

#!/bin/bash

id_saved=0
sac_saved=0
sbat_saved=0
dim_saved=false

ss_save()
{
    id_saved=`gsettings get org.gnome.desktop.session idle-delay`
    sac_saved=`gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout`
    sbat_saved=`gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout`
    dim_saved=`gsettings get org.gnome.settings-daemon.plugins.power idle-dim`
}

ss_set()
{
    gsettings set org.gnome.desktop.session idle-delay "$1"
    gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout "$2"
    gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout "$3"
    gsettings set org.gnome.settings-daemon.plugins.power idle-dim "$4"
}

ss_on()
{
    ss_set "$id_saved" "$sac_saved" "$sbat_saved" "$dim_saved"
}

ss_off()
{
    ss_set 0 0 0 false
}

trap ss_on EXIT

already_off=0
flash_on=0
ss_save

while true; do
    if top -bcw 200 | sed -e '1,/PID/d' | head -1 | grep flash > /dev/null
    then
        flash_on=1
        off_count=0
    else
        off_count=`expr $off_count + 1`
        echo "flash_off_count=$off_count"
        if [ $off_count -gt 5 ]; then
            flash_on=0
        fi
    fi

    if [ "$flash_on" = "1" -a "$already_off" = "0" ]; then
        echo "screensaver: off"
        ss_off
        already_off=1
    elif [ "$flash_on" = "0" -a "$already_off" = "1" ]; then
        echo "screensaver: on"
        ss_on
        already_off=0
    elif [ "$already_off" = "0" ]; then
        echo "screensaver: save possibly new params"
        ss_save # user may update the values
    fi

    sleep 30
done

BTW, this script disables screensaver ONLY when the video is playing, i.e. when flashplayer is in the top of CPU consumers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .