June 05, 2013

Prevent Screen from Turning Off While Playing Videos on Linux

If you're using Ubuntu with Gnome/Unity and your screen turns off (screensaver) while watching videos in your browser (YouTube or others), this might solve your problem.

#!/bin/bash -eu

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

# Cleanup bad state left behind if 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

Save this script somewhere, chmod +x it, and add it to the Startup Applications so it runs when you log in. Don't forget to add your browsers in the first array!

No comments:

Post a Comment