How-to: Screen saver inhibitor for Firefox flash player container-plugin or whatever...

Hi, I want to share my findings. This is a versatile script to inhibit the screen-saver automatic activation by measuring the CPU consumption of a process.
Of course I did not wrote the script, all credits goes to the writer Milosz Galazka and a gentleman called Etan Reisner, who help me fix a bug in it.

#!/bin/sh
# Simple script to demonstrate D-Bus usage

while true
do
  # read Firefox's container plugin cpu usage
  
  ret=`top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'`

  if  -n "$ret" ] &&  "$ret" -gt 5 ]; then
    idle_time=`qdbus org.kde.screensaver /ScreenSaver GetSessionIdleTime`
    if  "$idle_time" -gt 50 ]; then
      qdbus org.kde.screensaver /ScreenSaver SimulateUserActivity
    fi
  fi

  sleep 50
done

“awk '$12” is the twelfth column in “top” = process name and “SUM += $9” is the ninth column, thus CPU usage.

You can edit those four variables:

plugin-containe : for any process you wish to monitor

“$ret” -gt 5 ] : is the CPU threshold

“$idle_time” -gt 50 ] : is the session idle time

sleep 50 : is of course, the delay for the script repetition.

I use OpenSUSE 13.2 with KDE4, for me, I only ad the script to Autostart in System Settings/ Startup and Shutdown.

Thats all.

On 2015-07-03 05:26, noital wrote:
>
> Hi, I want to share my findings. This is a versatile script to inhibit
> the screen-saver automatic activation by measuring the CPU consumption
> of a process.

Interesting idea!

> Code:
> --------------------

> qdbus org.kde.screensaver /ScreenSaver SimulateUserActivity

> --------------------

Thus it will only work for kde.


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

You are right, it’s unfortunately too late to change the title.

But I guess, if you edit the dbus command for some gnome equivalent, it will work. I never used gnome, I could not tell.

Regard

Use “org.freedesktop.ScreenSaver”, that should work for both KDE and GNOME, and probably other DEs as well.

qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity

Or see here for how to temporary disable/enable it:
http://people.freedesktop.org/~hadess/idle-inhibition-spec/re01.html

I.e. something like

cookie=`qdbus org.freedesktop.ScreenSaver /ScreenSaver Inhibit someId reason`

(where someId should uniquely identify your application, and reason is a string that describes the reason)

Afterwards you can enable it again with:

qdbus org.freedesktop.ScreenSaver /ScreenSaver UnInhibit $cookie

On 2015-07-03 18:46, wolfi323 wrote:

> Use “org.freedesktop.ScreenSaver”, that should work for both KDE and
> GNOME, and probably other DEs as well.

But you need have qdbus installed, which is part of qt. I have it, so it
is no problem, but… :-?

I have to experiment with this :slight_smile:


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

[quote="

But you need have qdbus installed, which is part of qt. I have it, so it
is no problem, but… :-?

I have to experiment with this :slight_smile:


Cheers / Saludos,

Carlos E. R.[/QUOTE"]

(from 13.1 x86_64 “Bottle” (Minas Tirith))
[/quote]

I have found this command:

dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity

I didn’t found the “GetSessionIdleTime” command. But if the above command works in a terminal without error. My understanding is that getting session idle time is optional and modifying the script without it is possible and would still do the job:

#!/bin/sh
# Simple script to demonstrate D-Bus usage

while true
do
  # read google chrome cpu usage
  ret=`top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'`

  if  -n "$ret" ] &&  "$ret" -gt 5 ]; then

      dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity
    
  fi

  sleep 50
done

Because the script run every 50 secs as it is and can be edit to more or less.

Regards

As mentioned already by noital, you can use dbus-send as well, which is part of dbus itself, so is available everywhere.
But the syntax is a bit different…

E.g. this should work on any Linux DE (or many at least):

dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity

On 2015-07-03 21:46, wolfi323 wrote:

Ok, I’m trying this. I wrote the script this way:


#!/bin/bash

while true
do
# read Firefox's container plugin cpu usage

DATE=`date --rfc-3339=seconds`
DATE=`date  --iso=ns`

ret=`top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'`

if  -n "$ret" ] &&  "$ret" -gt 5 ]; then
echo "$DATE Simulando actividad ($ret)"
dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver \
/org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity
else
echo "$DATE No hago nada ($ret)"
fi
sleep 50
done

Then I started plying something, but it detects no activity. Thus I tried separately:


cer@minas-tirith:~> top -b -n1 -u $(whoami) | grep plugin-con
30058 cer       20   0  930416 132748  35216      0 132748 R 69.68 3.354   2:08.77 plugin-containe
cer@minas-tirith:~> top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'
0
cer@minas-tirith:~>

You see, CPU is about 70%, yet the code says it is zero.
I don’t know awk, so I can’t trace the issue :-?

I think I know. My “top” is configured to print other fields:


top - 22:55:17 up 11 days, 8 min, 30 users,  load average: 0.49, 1.09, 1.02
Tasks: 288 total,   1 running, 287 sleeping,   0 stopped,   0 zombie
%Cpu(s): 13.2 us,  3.2 sy,  0.0 ni, 80.5 id,  3.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   3957996 total,  2170292 used,  1787704 free,    64372 buffers
KiB Swap:  6289412 total,  1270268 used,  5019144 free,   876920 cached

PID USER      PR  NI    VIRT    RES    SHR   SWAP   USED S  %CPU  %MEM     TIME+ COMMAND
8256 cer       20   0 2289440 532404  28488 295976 828380 S 12.88 13.45 185:56.74 firefox

So, alternatives:

  • customizing the awk line so that it reads the proper field for “me”.
  • same, but first it has to read columns headers to locate the right one.
  • use something else altogether. Perhaps “ps --format”.

This one looks promising:


ps -xo pcpu,user,fname,comm

Further tuning is possible, but I can’t quite get it.


cer@minas-tirith:~> ps -xo pcpu,user,fname,comm | grep plugin
51.3 cer      plugin-c plugin-containe
cer@minas-tirith:~>

cer@minas-tirith:~> ps -C plugin-containe -xo pcpu,user,fname,comm
error: must set personality to get -x option

Usage:
ps [options]

Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.

For more details see ps(1).
cer@minas-tirith:~>

cer@minas-tirith:~> ps -C plugin-containe
PID TTY          TIME CMD
30785 ?        00:02:16 plugin-containe
cer@minas-tirith:~>

You see, it is possible to tell ps to print data for a single process by name;
and it is also possible to print the fields we want and in the order we want;
But I can’t do both. :-?


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

Maybe I’m missing something, but as I mentioned in the original post:

“awk '$12” is the twelfth column in “top” = process name and “SUM += $9” is the ninth column, thus CPU usage.

I see 14 in your “top” so “awk '$14” would be the process “SUM += $11” would be the CPU usage.

On 2015-07-04 00:46, noital wrote:
>
> Maybe I’m missing something, but as I mentioned in the original post:
>
> “awk '$12” is the twelfth column in “top” = process name and “SUM +=
> $9” is the ninth column, thus CPU usage.
>
> I see 14 in your “top” so “awk '$14” would be the process “awk '$11”
> would be the CPU usage.

That’s the point, that the code is not generic, it has to be modified
for each user. I’m trying to find some other method to function for any
user.


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

On 2015-07-03 21:46, wolfi323 wrote:

> E.g. this should work on any Linux DE (or many at least):
>
> Code:
> --------------------
> dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity
> --------------------

Well, it doesn’t in my XFCE on 13.1. I placed the call in a loop every 50 seconds, and after the expected 10 minutes, the laptop display went blank.

The call doesn’t print anything, but has no effect either.

pity…

This other method fails, too:


cer@minas-tirith:~> qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity
Service 'org.freedesktop.ScreenSaver' does not exist.
cer@minas-tirith:~>


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

UPDATE!

After further testing, I realized that “GetSessionIdleTime” returns milliseconds, therefore if you expect 50 seconds in “$idle_time” -gt 50 ] , you may change it to “$idle_time” -gt 50000 ].

Regards

Well, then XFCE doesn’t seem to implement the freedesktop.org ScreenSaver specification.

I don’t have XFCE installed so cannot check, but you can use qdbusviewer to look for a similar service.

There’s also the “xdg-screensaver” utility that’s supposed to work on every desktop, you could try that (see its man page).
But it’s apparently “broken” too, it doesn’t work at all in KDE4 at least…

Just to be sure: you do want to disable the screensaver, i.e. the animations that are played if the system is idle for some time?
Or do you actually want to disable screen blanking, i.e. powermanagement? (inhibiting the screensaver should do that, too, but if you don’t have a screensaver to begin with…)
The latter can probably be done with just:

xset -dpms

On 2015-07-04 09:46, wolfi323 wrote:

> Well, then XFCE doesn’t seem to implement the freedesktop.org
> ScreenSaver specification.

yep…

> I don’t have XFCE installed so cannot check, but you can use qdbusviewer
> to look for a similar service.

I’ll try. …] Mmm… nothing named screen or saver there.

> Just to be sure: you do want to disable the screensaver, i.e. the
> animations that are played if the system is idle for some time?
> Or do you actually want to disable screen blanking, i.e.
> powermanagement? (inhibiting the screensaver should do that, too, but if
> you don’t have a screensaver to begin with…)

Just the screen saver that kicks in when watching youtube for some
minutes. If I use xine it already inhibits it, IIRC, but not firefox.
The idea in this script is just it :slight_smile:

I remember reading that xine fiddles with one of the keyboard LED, in
the upper case block key, perhaps. It toggles it on/off, so that the
system thinks there is some activity. Kind of a hack, but it works. If I
find how to do that in a script :-?


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

That’s right xdotool can do that:

xdotool set_desktop 0 key shift

Well, as I don’t have/use XFCE, I don’t know what screen saver kicks in.
But to disable Xorg’s screensaver, you could also use “xset s off”, “xset s on” would turn it on again.

If I use xine it already inhibits it, IIRC, but not firefox.

AFAIK, xine periodicly creates fake keypresses while playing.

Btw, I just played a HTML5 video with Firefox yesterday (in KDE4), and it did disable the screensaver… :wink:
Flash probably still doesn’t though.

On 2015-07-04 14:16, noital wrote:
> robin_listas;2718063 Wrote:

> That’s right xdotool can do that:
>
>
> Code:
> --------------------
> xdotool set_desktop 0 key shift
> --------------------

I got a loop that works — I just tested it:


#!/bin/bash
while true
do
xdotool key shift
sleep 50
done

I had to install “xdotool-2.2012+git.20130201.65cb0b1-2.1.3.x86_64”.

Now the next step is to concoct a ‘ps’ line. :slight_smile:


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

On 2015-07-04 14:56, wolfi323 wrote:

> Well, as I don’t have/use XFCE, I don’t know what screen saver kicks in.

It seems XScreensaver 5.22 in my 13.1. But I have it set to “blank
only”, so perhaps the screensaver doesn’t kick in really.

> But to disable Xorg’s screensaver, you could also use “xset s off”,
> “xset s on” would turn it on again.

Ah, yes… dunno… might be a problem if the script stops at the wrong
point, leaving the incorrect setting enabled. Faking a keyboard touch
seems to do the trick very nicely, I just tested it :slight_smile:

>> If I use xine it already inhibits it, IIRC, but not firefox.
> AFAIK, xine periodicly creates fake keypresses while playing.
>
> Btw, I just played a HTML5 video with Firefox yesterday (in KDE4), and
> it did disable the screensaver… :wink:
> Flash probably still doesn’t though.

Well, not surprising. HTML5 is integrated, Flash is not. Worse, it is
proprietary.

It loads my two cores (in this laptop) at 40% continuously, just to
watch TV. Even listening to the radio is terrible. High CPU load in
summer means the thing gets hot:


minas-tirith:~ # sensors ; hddtemp /dev/sda
acpitz-virtual-0
Adapter: Virtual device
temp1:        +64.0°C

coretemp-isa-0000
Adapter: ISA adapter
Core 0:       +55.0°C  (high = +105.0°C, crit = +105.0°C)
Core 1:       +55.0°C  (high = +105.0°C, crit = +105.0°C)

/dev/sda: ST9500325AS: 48°C
minas-tirith:~ #

And that’s with a cooling fan running under the laptop… sigh. I hate
Flash.


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))

“sigh. I hate flash”

Hi, it’s only a suggestion, but maybe you could write a script using a ps command and xdotool?

ps x |grep -v grep |grep -c "plugin-containe"

On 2015-07-07 18:56, noital wrote:
>
>> “sigh. I hate flash”
>
> Hi, it’s only a suggestion, but maybe you could write a script using a
> ‘ps command’ (http://tinyurl.com/o7k5d5n) and xdotool?
>
>
> Code:
> --------------------
> ps x |grep -v grep |grep -c “plugin-containe”
> --------------------

That’s what I want to do. If you go back, one of my posts explains what
I have tried. You can tell ps to print data about a single process by
name, so grep is not needed. And there are options to print the wanted
fields in a specific order.

But I have problems getting both options to work in a single command line.


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))