Inhibit screensaver, suspend, etc., when watching Flash videos

The usual advice about this issue is to disable powersaving when watching a flash video and then reenable it when done. I have always found this to be a bit of a pain.

To use this method, do the following:

  1. Install package “yad” (similar to “kdialog” but with a few more options) from the Packman repository.

  2. Copy and paste the following into a text file named “sleep.sh” and save it into your ~/bin directory. Make it executable:

#!/bin/bash


#get current idle load

IDLE=$(top -b -n 2 | grep Cpu)


#check for parameter; if none, set LIMIT to default of 98.0

if  -z "$1" ]; then
  LIMIT=98.0
else
  LIMIT=$1
fi


#multiply IDLE and LIMIT by 10 and convert both to integers

IDLE=${IDLE:114:4}
IDLE=$(echo "$IDLE*10" | bc)
IDLE=${IDLE%%.*}
LIMIT=$(echo "$LIMIT*10" | bc)
LIMIT=${LIMIT%%.*}

#compare IDLE and LIMIT

if  "$IDLE" -ge "$LIMIT" ]; then

    #play warning sound, start countdown timer
    
    play -q /usr/share/sounds/KDE-Sys-Warning.ogg &

    yad --title Warning!\
        --button=gtk-cancel\
        --text="System suspending in 60 sec! 
"\
        --timeout 55\
        --timeout-indicator bottom\
        --image=/usr/share/icons/default.kde4/64x64/status/dialog-warning.png\
        --auto-close &
       
else

  #simulate keypress
  
  dbus-send --print-reply\
            --type=method_call\
            --dest=org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity
  
fi


exit

  1. Go to System Settings–> Power Management. Check the box next to “Run Script”, and in the box “Script” point it to the file “~/bin/sleep.sh”. In the dropdown box below this (“Run script”) select “After”. Set the timer to one minute before the earliest power management option you have selected, e.g., if you have “Suspend Session” set to 10 minutes, set your script to run after 9 minutes.

What it does: The principle is that if the system idle load is greater than or equal to a certain percentage, the script will conclude that nothing is going on and will allow the system to go into power saving mode. If the idle load is less than this percentage, it will conclude that you are doing something that is using processor resources, such as watching a flash video, talking on Skype, etc., and will then simulate a keypress to restart the power management timer.

First the script executes “top” for two iterations, selects the lines containing the string “Cpu” and stores the output as “IDLE”. It does two iterations of “top” because the first one seems always to show the idle load to be lower than it really is, possibly due to the overhead of loading “top” to start with. It then checks to see if a parameter has been passed to the script. If there is one it will set the variable “LIMIT” to this value, otherwise it will set it to the default value of 98.0%. You might find that a different value works better for your system. If you do, pass it to the script in the form “xx.x”. It then finds the 114th character in the string “IDLE”, which is the location of the second value of the system idle percentage, and resets “IDLE” to the four characters starting there, giving the system idle percentage in “xx.x” form. It then multiplies this value along with “LIMIT” by 10 and then converts both values to integers.

It then compares the two values. If the system idle percentage is greater than or equal to the defined limit, it will sound a warning and display a dialog box with a countdown timer. Clicking on “Cancel” (or actually any mouse or keyboard activity) will reset the power management timer and the system will remain active. If, however, no activity occurs, the countdown timer will expire and the system will then go into power saving mode.

If, on the other hand, the system idle percentage is lower than the defined value, a keypress is simulated and the system remains active.

This works for KDE. It might work with Gnome but I don’t know if this has any option for running a script after a certain time. It could probably be run with a “cron” script if necessary but I haven’t looked into this.

Hi
Yes, there is an app called caffeine for this in the openSUSE
release of GNOME (should work in KDE as well?). But a simple xset
command with options does all of it as well…


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.6-2.10-desktop
up 5 days 3:27, 4 users, load average: 0.34, 0.33, 0.26
CPU Intel i5 CPU M520@2.40GHz | Intel Arrandale GPU

Actually, I did the xset approach for a bit. I had a launcher for firefox that would do “xset -dpms” when it launched and “xset +dpms” when it closed. I also found that you could run scripts with certain actions in Skype, so that it would “xset -dpms” when a call started and “xset +dpms” when I hung up. But then once I had a firefox session open, and then I got a Skype call, and when I finished the Skype call it executed “xset +dpms”. I figured there might be a way of keeping track of how many xset calls had been made and reversing them accordingly, but this approached seemed easier and more elegant. And I just like to write code…

On 2012-09-16 01:21, malcolmlewis wrote:

> But a simple xset
> command with options does all of it as well…

Actually, code like xine emulates a keyboard press while playing a movie. IIRC, it fiddles with
they keyboard hardware toggling the [bloqnum] key, you can sometimes see the led in the
keyboard flash.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)