Ubuntu – When I watch a video online the screen turns off after a few minutes

google-chromelock-screenvideo

in some sites when I watch a movie in a browser and I don't touch the computer the screen turns off. In other sites like Youtube I don't have that problem.
I tried using caffeine but I don't like the idea of having to manually enable and disable it every time I watch a video. I also tried other methods that I found on the internet but nothing worked.
any suggestions?
I'm using chrome and running ubuntu 16.04.

Best Answer

UPDATE 2017/08/23: Changed script to one that doesn't send keystrokes but disables dpms and the screensaver temporarily while a video is playing full screen.

This only works with applications running full screen, not maximized.


First install xdotool:

sudo apt install xdotool

then I created a scripts folder in my home folder that I could drop any scripts into that I wanted to keep:

mkdir -p ~/scripts

then I created a file called check_full.bsh in my ~/scripts folder and added the following content to it:

Since my screensaver is set for 20 minutes, I set the sleep timer in the script to 10 minutes (600 seconds). You can change this based on your screensaver settings.

#!/bin/bash

#Get DPMS settings
dpms_set=$(xset -q | awk '/DPMS is/ {print $NF}')

#Get screensaver type and command
scrnsvr=$(ps -ef | awk '/screensav/ {print $8}' | grep -v awk)
scrnsvrcmd=$(ps -ef | awk '/screensav/ {print substr($0,index($0,$8))}' | grep -v awk)

#Sleep Timer in seconds.
SL=600

while true;
do
result=()
#Get all window geometries
all=($(xdotool search --name - | while read win; do xdotool getwindowgeometry $win | awk '/Geometry/ {print $2}'; done))

#Get all screen resolutions detected and build array.
SCREENS=($(xrandr | awk '/\*/ {print $1}'))

SCRN=" ${SCREENS[*]} "

#Get result of all windows and match

for item in ${all[@]}; do if [[ $SCRN =~ " $item " ]] ; then result+=($item); fi; done

#If a result exists disable power management and screensaver
if [[ ${result[@]} ]]; then
    ps -ef | grep $scrnsvr | grep -v grep >/dev/null
    if [[ $? == 0 ]]; then 
    kill $(ps -ef | grep $scrnsvr | grep -v grep | awk '{print $2}')
    fi
    if [[ $dpms_set == "Enabled" ]];then
    xset -dpms
    fi
else    
    ps -ef | grep $scrnsvr | grep -v grep >/dev/null
    if [[ $? == 1 ]]; then
    ${scrnsvrcmd} &
    fi
    if [[ $dpms_set != "Disabled" ]];then
        xset dpms
        fi
fi
result=()
sleep $SL
done

make the script executable:

chmod +x ~/scripts/check_full.bsh

For some reason, a cron job would not run this correctly. So I added an entry to my startup to call this script. In the ~/.config/autostart/ folder, I created a file called Check_full.desktop with the following content:

I like to have delays added to startup as they usually load better.

[Desktop Entry]
Type=Application
Name=Check_Full
Comment=Prevent screensaver from activating when full screen video is playing
Exec=bash -c 'sleep 5 && /home/<username>/scripts/check_full.bsh'

change above Exec line to match your home folder.

Set proper permissions to the file:

chmod 664 ~/.config/autostart/Check_full.desktop

After logging out and back in, check_full.bsh is now running and checking every 10 minutes if active application is full screen. If not, screensaver / lock screen launch at normal times.

Hope this helps!

Related Question