(Programmatically) preventing screensaver from activating

Caporegime
Joined
18 Oct 2002
Posts
29,490
Location
Back in East London
Hi all,

We've developed a new system using Ubuntu (Gnome) as the platform.

The system has a hand-held imaging scanner (akin to a bar code scanner,) and printer. It's quite simplistic, but was a dog to get it 'right'.

Anywho, one last problem we're experiencing is the screensaver. We must have it enabled for periods of inactivity, but the scanner does not count as "activity" so it does not disable the SS when scanner triggered, which we'd like it to do. And also, after 5 mins of use the screen saver enables, despite someone scanning/printing furiously away.

Apps like Totem (movie player) do something to prevent the screensaver, but short of it disabling the screensaver on open, and then enabling it on close (which is dirty) I cannot fathom how they do it. No C/sh/bash snippets found when searching the WWW.

Much obliged for opinions and help. :)
 
Associate
Joined
3 Sep 2005
Posts
1,670
Location
Glasgow, Scotland
I've never really did much coding on *nix but maybe http://ubuntuforums.org/archive/index.php/t-284804.html will point you in the right direction?

Here's the source of disablegss.py if you need it:

Code:
#! /usr/bin/env python
#disablegss.py
#
#  Copyright (c) 2006 [email protected]
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA


import dbus
import dbus.glib
import sys
import os
import string
import time
from stat import *

def disable_sleep(myprogram):
    try:
        bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
        devobj = bus.get_object('org.gnome.ScreenSaver',  '/org/gnome/ScreenSaver')
        dev = dbus.Interface(devobj, "org.gnome.ScreenSaver")
        cookie = dev.Inhibit(myprogram, 'Disabled by DisableGSS Daemon')
        print "DisableGSS: gnome screensaver stopped."
        return (dev, cookie)
    except Exception, e:
        print "DisableGSS: could not send the dbus Inhibit signal: %s" % e
        #sys.exit(0)
        return (False, False)


def allow_sleep(dev, cookie):
    try:
        dev.UnInhibit(cookie)
        print "DisableGSS: gnome screensaver enabled."
        return(True)
    except Exception, e:
        print "DisableGSS: could not send the dbus UnInhibit signal: %s" % e
        #sys.exit(0)
        return (False)


def pids(program):
    result = []
    f = os.popen('ps aux', 'r')
    for l in f.readlines():
        fields = string.split(l)
        if fields[10] == program:
            #print fields[1]+"   "+fields[10]
            return(True)

def read_file():
    __psaux = []
    try:
        f=open(homedir+'/.disablegss', 'r')
        for line in f:
            #print line.rstrip('\n')
            __psaux.append(line.rstrip('\n'))
        f.close();
        print "DisableGSS: config file read."
        return __psaux
    except IOError:
        print "DisableGSS: config file ~/.disablegss doesn't exist! Write it by hand. Add applications name that could disable gnome screensaver: one app name for every line of file."
        sys.exit(0)

if __name__ == '__main__':
    homedir = os.getenv('HOME')
    condition = True
    disabled = False
    old_program = ""
    psaux = []
    last_last_time_modified = ""
    last_time_modified = ""

    psaux=read_file()
    last_time_modified= os.stat(homedir+'/.disablegss')[ST_MTIME]
    last_last_time_modified=last_time_modified

    while condition == True:
        found = False
        for i in range(len(psaux)):
            #print "%s->%s" % (i,psaux[i])
            program=psaux[i]
            if pids(program) == True:
                found = True
                if program !=old_program:
                    print "DisableGSS: there is a program ("+program+") in the config list that is currently running."
                    old_program=program
                if disabled==False:
                    (dev, cookie) = disable_sleep(program)
                    disabled=True
                    break

        if found == False and disabled == True:
            print "DisableGSS: there are no more programs that could stop gnome screensaver."
            allow_sleep(dev, cookie)
            disabled = False
        time.sleep(60)
        last_time_modified= os.stat(homedir+'/.disablegss')[ST_MTIME]
        if last_time_modified!=last_last_time_modified:
            print "DisableGSS: config file modified."
            psaux=read_file()
            last_time_modified= os.stat(homedir+'/.disablegss')[ST_MTIME]
            last_last_time_modified=last_time_modified
            old_program = ""
        #condition=False
 
Caporegime
OP
Joined
18 Oct 2002
Posts
29,490
Location
Back in East London
That's looking helpful, thanks. :)

We'll need to modify it of course, as we don't want the SS to be completely inhibited whilst our app is running, only when unused for 'x' minutes of course. But definitely on the right track. :)
 
Back
Top Bottom