updated on Tue Jan 10 12:02:00 UTC 2012
[aur-mirror.git] / disablegss / disablegss.py
blobb9b50f7ef38326905dd02d72eaaca4a7643587da
1 #! /usr/bin/env python
2 #disablegss.py
3 #
4 # Copyright (c) 2006 crazy___cow@hotmail.com
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 # USA
23 import dbus
24 import dbus.glib
25 import sys
26 import os
27 import string
28 import time
29 from stat import *
33 def disable_sleep(myprogram):
34 try:
35 bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
36 devobj = bus.get_object('org.gnome.ScreenSaver', '/org/gnome/ScreenSaver')
37 dev = dbus.Interface(devobj, "org.gnome.ScreenSaver")
38 cookie = dev.Inhibit(myprogram, 'Disabled by DisableGSS Daemon')
39 print "DisableGSS: gnome screensaver stopped."
40 return (dev, cookie)
41 except Exception, e:
42 print "DisableGSS: could not send the dbus Inhibit signal: %s" % e
43 #sys.exit(0)
44 return (False, False)
47 def allow_sleep(dev, cookie):
48 try:
49 dev.UnInhibit(cookie)
50 print "DisableGSS: gnome screensaver enabled."
51 return(True)
52 except Exception, e:
53 print "DisableGSS: could not send the dbus UnInhibit signal: %s" % e
54 #sys.exit(0)
55 return (False)
58 def pids(program):
59 result = []
60 f = os.popen('ps aux', 'r')
61 for l in f.readlines():
62 fields = string.split(l)
63 if fields[10] == program:
64 #print fields[1]+" "+fields[10]
65 return(True)
69 def read_file():
70 __psaux = []
71 try:
72 f=open(homedir+'/.disablegss', 'r')
73 for line in f:
74 #print line.rstrip('\n')
75 __psaux.append(line.rstrip('\n'))
76 f.close();
77 print "DisableGSS: config file read."
78 return __psaux
79 except IOError:
80 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."
81 sys.exit(0)
87 if __name__ == '__main__':
90 homedir = os.getenv('HOME')
91 condition = True
92 disabled = False
93 old_program = ""
94 psaux = []
95 last_last_time_modified = ""
96 last_time_modified = ""
98 psaux=read_file()
99 last_time_modified= os.stat(homedir+'/.disablegss')[ST_MTIME]
100 last_last_time_modified=last_time_modified
102 while condition == True:
103 found = False
104 for i in range(len(psaux)):
105 #print "%s->%s" % (i,psaux[i])
106 program=psaux[i]
107 if pids(program) == True:
108 found = True
109 if program !=old_program:
110 print "DisableGSS: there is a program ("+program+") in the config list that is currently running."
111 old_program=program
112 if disabled==False:
113 (dev, cookie) = disable_sleep(program)
114 disabled=True
115 break
117 if found == False and disabled == True:
118 print "DisableGSS: there are no more programs that could stop gnome screensaver."
119 allow_sleep(dev, cookie)
120 disabled = False
121 time.sleep(60)
122 last_time_modified= os.stat(homedir+'/.disablegss')[ST_MTIME]
123 if last_time_modified!=last_last_time_modified:
124 print "DisableGSS: config file modified."
125 psaux=read_file()
126 last_time_modified= os.stat(homedir+'/.disablegss')[ST_MTIME]
127 last_last_time_modified=last_time_modified
128 old_program = ""
129 #condition=False