this is a short manual how to fix the behaviour of the screensaver in Linux Mint 11 (or any other distro using Gnome 2.x as a Desktop manager)
What is the problem? The screensaver in plain Linux Mint 11 is not deactivated when you play a movie with 'vlc' or a flash movie in Firefox. That is a little annoying because the screensaver will blank your screen while you are watching the movie. Of course you could just switch the screensaver off but then the screen and computer stays awake even if you fall asleep... This is not what you want.
There is a way how to handle this stuff nicely but not all applications make use of this possibility. Fortunately the default movie player Totem does it right. It inhibits the screensaver when you start playing something (movie, audio) and it uninhibits the screensaver when it is finished playing your media. But there are a great number of players / processes that do not make use of the possibilities of the gnome-screensaver (yet).
This is where my little script comes in. It is not very sophisticated and it does not make use of the official way to handle (un)inhibiting the screensaver via the Dbus but for me it works great at the moment and perhaps someone is happy with it. I will test it some more days and perhaps after that I will rewrite it so that it uses the official way of doing things.
What the script does is the following. It checks whether some predefined processes eat up too much CPU and decides whether this should (un)inhibit the screensaver. The processes can be defined in the script itself in the following section:
Code: Select all
# define process names (from 'top' output) and their CPU usage thresholds
my %processes = (
'firefox' => '50',
'plugin-container' => '15',
'vlc' => '8',
);
Of course, this is based on assumptions and it will not be the same for every system but I found that I can make a 100% matching profile for my own computer which is great. The screensaver does exactly what it should do. Keep quiet when I am watching a movie and kick in afterwards. That's great!!
There is another problem. If one undefinded process is making heavy use of the processor, the defined processes could stay below their thresholds because looking at the percentages the processes seem not very busy even if you are watching video. That's why I added the possibility to inhibit the screensaver when the total load of the system gets "very high" as well. You can adjust this in the following part of the script:
Code: Select all
# define total load that wil inhibit the screensaver
my $maxLoad = 0.6;
You can get the whole script below.
Play around and get the parameters right for your needs. (Yes, this could be a hard part for Newbies, just ask if you do not know what to do)
After that add it to the startup items of your Gnome desktop.
And then you will never have to wiggle the mouse again to keep watching your Flash movie in Firefox.
Have fun and take care!
Magnetizer
-----
Here is the whole script (tried to add it as an attachment but to no prevail):
Code: Select all
#!/usr/bin/perl -W
# ====================================================================
#
# screensaver_inhibitor -- script to (un)inhibit the screensaver due
# to the CPU usages of several predefined
# processes and the total system load.
#
# Usage: screensaver_inhibitor
#
# Author: <magnetizer@live.com>
#
# written on 10/08/2011
#
# Copyright: Magnetizer, october 2011
#
# ====================================================================
# ====================================================================
# Configuration
# ====================================================================
# --------------------------------------------------------------------
# General settings
# --------------------------------------------------------------------
# define process names (from 'top' output) and their CPU usage thresholds
my %processes = (
'firefox' => '50',
'plugin-container' => '15',
'vlc' => '8',
);
# define total load that wil inhibit the screensaver
my $maxLoad = 0.6;
# --------------------------------------------------------------------
# Required perl modules
# --------------------------------------------------------------------
use strict;
# --------------------------------------------------------------------
# Subroutine to get the current date
# --------------------------------------------------------------------
sub getDate() {
# determine the current date
my @dateTimeData = localtime(time);
# format the date to be used as a MySQL datetime field
my $date = $dateTimeData[5] + 1900;
$date .= '-';
$date .= sprintf("%02d", $dateTimeData[4] + 1);
$date .= '-';
$date .= sprintf("%02d", $dateTimeData[3]);
# return date
return $date;
}
# --------------------------------------------------------------------
# Subroutine to get the current time
# --------------------------------------------------------------------
sub getTime() {
# determine the current time
my @dateTimeData = localtime(time);
# format the date to be used as a MySQL datetime field
my $time = sprintf("%02d", $dateTimeData[2]);
$time .= ':';
$time .= sprintf("%02d", $dateTimeData[1]);
$time .= ':';
$time .= sprintf("%02d", $dateTimeData[0]);
# return time
return $time;
}
# ====================================================================
# Main loop
# ====================================================================
my $process = undef;
my $processShort = undef;
my $threshold = undef;
my $topOutput = undef;
my $top = undef;
my $CPU = undef;
my @topValues = undef;
my $inhibit = undef;
my $processName = undef;
my $reason = undef;
my $last_inhibit_status = 0;
while (1 == 1) {
$inhibit = 0;
$reason = undef;
$processName = undef;
# get 'top' twice (the first time the output of 'top' is often distorted by 'top' itself)
#$topOutput = `top -b -n1`;
$topOutput = `top -b -n1`;
# check the definded processes
while (($process, $threshold) = each(%processes)) {
# 'top' only outputs the first 16 characters of the process name
$processShort = substr($process, 0, 15);
# initialize varibles
$top = undef;
$CPU = undef;
@topValues = undef;
# search for process name in 'top' output
if ($topOutput =~ /\n(.*?$processShort.*?)\n/i) {
# get the result
$top = $1;
chomp($top);
# get the CPU usage from the result
@topValues = split(' ', $top);
$CPU = $topValues[8];
if (defined($CPU) && ($CPU >= $threshold)) {
print getDate() . " " .getTime() . " -- process above threshhold : " . $processShort . " -- usage : " . $CPU . " (threshold : " . $threshold . ")\n";
$processName = $process;
$reason = 'busy';
$inhibit = 1;
}
}
}
# also check the total load
if ($topOutput =~ /load average:\s*(.*?),/i) {
$CPU = $1;
if (defined($CPU) && ($CPU >= $maxLoad)) {
print getDate() . " " . getTime() . " -- system load above threshold -- load : " . $CPU . " (threshold : " . $maxLoad . ")\n";
$processName = 'system-load';
$reason = 'too-high';
$inhibit = 1;
}
}
# do the appropriate action depending on whether the 'inhibit' flag is set ...
if ($inhibit == 1) {
if ($last_inhibit_status == 0) {
print getDate() . " " . getTime() . " action: inhibiting screensaver\n";
system("killall gnome-screensaver-command");
system("/usr/bin/gnome-screensaver-command -i -n $processName -r $reason &");
} else {
print getDate() . " " . getTime() . " action: nothing, screensaver still inhibited\n";
}
$last_inhibit_status = 1;
}
# or not ...
if ($inhibit == 0) {
if ($last_inhibit_status == 1) {
print getDate() . " " . getTime() . " action: uninhibiting screensaver\n";
system("killall gnome-screensaver-command");
system("killall gnome-screensaver");
system("gnome-screensaver-command -q");
} else {
print getDate() . " " . getTime() . " action: nothing, screensaver still unihibited\n";
}
$last_inhibit_status = 0;
}
# sleep a while before running again
sleep(10);
}
# ====================================================================
# End of script
# ====================================================================
exit 0;