diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..6f079def8 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +all: + python setup.py build_ext -i + mv idle.so common/ + msgfmt Messages/fr/LC_MESSAGES/gajim.po -o Messages/fr/LC_MESSAGES/gajim.mo diff --git a/Messages/fr/LC_MESSAGES/gajim.mo b/Messages/fr/LC_MESSAGES/gajim.mo deleted file mode 100644 index 1dbb24567..000000000 Binary files a/Messages/fr/LC_MESSAGES/gajim.mo and /dev/null differ diff --git a/common/idle.cpp b/common/idle.cpp new file mode 100644 index 000000000..6cf5f970f --- /dev/null +++ b/common/idle.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include + +#include + +static PyObject * idle_init(PyObject *self, PyObject *args) { + gtk_init (NULL, NULL); + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * idle_getIdleSec(PyObject *self, PyObject *args) { + static XScreenSaverInfo *mit_info = NULL; + int idle_time, event_base, error_base; + + gtk_init (NULL, NULL); + if (XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base)) { + if (mit_info == NULL) { + mit_info = XScreenSaverAllocInfo(); + } + XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info); + idle_time = (mit_info->idle) / 1000; + } + else + idle_time = 0; + return Py_BuildValue("i", idle_time); +} + +static PyObject * idle_close(PyObject *self, PyObject *args) { + gtk_main_quit (); + Py_INCREF(Py_None); + return Py_None; +} + +static PyMethodDef idleMethods[] = { + {"init", idle_init, METH_VARARGS, "init gtk"}, + {"getIdleSec", idle_getIdleSec, METH_VARARGS, "Give the idle time in secondes"}, + {"close", idle_close, METH_VARARGS, "close gtk"}, + {NULL, NULL, 0, NULL} +}; + +PyMODINIT_FUNC +initidle(void) +{ + (void) Py_InitModule("idle", idleMethods); +} diff --git a/common/sleepy.py b/common/sleepy.py index e5e58442c..23bf19ab3 100644 --- a/common/sleepy.py +++ b/common/sleepy.py @@ -2,6 +2,7 @@ """A Quick class to tell if theres any activity on your machine""" import time +import idle from string import find, lower @@ -16,45 +17,26 @@ class Sleepy: def __init__(self, interval1 = 60, interval2 = 120, devices = ['keyboard', 'mouse', 'ts'] ): - self.devices = devices - self.time_marker = time.time() - self.interval = self.interval_orig = interval1 - self.interval_orig2 = interval2 - self.last_proc_vals = {} - for dev in self.devices: self.last_proc_vals[dev] = 0 - + self.interval1 = interval1 + self.interval2 = interval2 self.state = STATE_AWAKE ## assume were awake to stake with try: - self.proc_int_fh = open("/proc/interrupts",'r') + idle.init() except: NOT_SUPPORTED = 1 self.state = STATE_UNKNOWN - self.proc_int_fh.close() - def poll(self): if NOT_SUPPORTED: return -1 now = time.time() - changed = 0 ## figure out if we have recieved interupts - for dev in self.devices: ## any of the selected devices - proc_val = self._read_proc(dev) - changed = changed or ( self.last_proc_vals[dev] != proc_val ) - self.last_proc_vals[dev] = proc_val - - if changed: - ## we are awake :) - self.time_marker = time.time() ## reset marker - self.state = STATE_AWAKE - self.interval = self.interval_orig - else: - if (now - self.time_marker >= self.interval): - ## we are asleep - if self.state == STATE_AWAKE: - self.state = STATE_AWAY - self.interval = self.interval_orig2 #second interval - else: - self.state = STATE_XAWAY + idleTime = idle.getIdleSec() + if idleTime > self.interval2: + self.state = STATE_XAWAY + elif idleTime > self.interval1: + self.state = STATE_AWAY + else: + self.state = STATE_AWAKE return 1 def getState(self): @@ -63,20 +45,8 @@ class Sleepy: def setState(self,val): self.state = val - def _read_proc(self, device = 'mouse'): - proc_int_fh = open("/proc/interrupts",'r') - info = proc_int_fh.readlines() - ## ignore first line - for line in info[1:]: - cols = line.strip().split() - if (find(lower(cols[-1]),device) != -1): - proc_int_fh.close() - return int(cols[1]) - proc_int_fh.close() - return 1 - if __name__ == '__main__': s = Sleepy(10) while s.poll(): print "state is %s" % s.getState() - time.sleep(10) + time.sleep(5) diff --git a/plugins/gtkgui/config.py b/plugins/gtkgui/config.py index 2653a9384..c4e54c9dd 100644 --- a/plugins/gtkgui/config.py +++ b/plugins/gtkgui/config.py @@ -23,6 +23,7 @@ import gtk from gtk import TRUE, FALSE import gtk.glade,gobject import os,string +import common.sleepy from common import i18n _ = i18n._ APP = i18n.APP @@ -216,7 +217,8 @@ class preference_Window: self.plugin.config['autoxatime'] = axt if self.plugin.sleeper: self.plugin.sleeper = common.sleepy.Sleepy(\ - self.plugin['autoawaytime']*60, self.plugin['autoxatime']*60) + self.plugin.config['autoawaytime']*60, \ + self.plugin.config['autoxatime']*60) self.plugin.send('CONFIG', None, ('GtkGui', self.plugin.config)) self.plugin.roster.draw_roster() @@ -306,28 +308,24 @@ class preference_Window: if self.plugin.config.has_key('autoaway'): st = self.plugin.config['autoaway'] self.chk_autoaway.set_active(st) - self.chk_autoaway.set_sensitive(0) #Autoawaytime st = 10 if self.plugin.config.has_key('autoawaytime'): st = self.plugin.config['autoawaytime'] self.spin_autoawaytime.set_value(st) - self.spin_autoawaytime.set_sensitive(0) #Autoxa st = 1 if self.plugin.config.has_key('autoxa'): st = self.plugin.config['autoxa'] self.chk_autoxa.set_active(st) - self.chk_autoxa.set_sensitive(0) #Autoxatime st = 20 if self.plugin.config.has_key('autoxatime'): st = self.plugin.config['autoxatime'] self.spin_autoxatime.set_value(st) - self.spin_autoxatime.set_sensitive(0) self.xml.signal_connect('gtk_widget_destroy', self.delete_event) self.xml.signal_connect('on_but_col_clicked', \ diff --git a/plugins/gtkgui/gtkgui.py b/plugins/gtkgui/gtkgui.py index d1ec68d9f..f42faa7db 100644 --- a/plugins/gtkgui/gtkgui.py +++ b/plugins/gtkgui/gtkgui.py @@ -785,6 +785,8 @@ class roster_Window: else: txt = status self.plugin.send('STATUS', account, (status, txt)) + if status == 'online': + self.plugin.sleeper_state[account] = 1 def on_optionmenu_changed(self, widget): """When we change our status""" @@ -805,6 +807,8 @@ class roster_Window: return for acct in accounts: self.plugin.send('STATUS', acct, (status, txt)) + if status == 'online': + self.plugin.sleeper_state[acct] = 1 def set_optionmenu(self): #table to change index in plugin.connected to index in optionmenu @@ -833,9 +837,10 @@ class roster_Window: for user in luser: self.chg_user_status(user, 'offline', 'Disconnected', account) elif self.plugin.connected[account] == 0: - self.plugin.sleeper = None#common.sleepy.Sleepy(\ - #self.plugin.config['autoawaytime']*60, \ - #self.plugin.config['autoxatime']*60) + if (self.plugin.config['autoaway'] or self.plugin.config['autoxa']): + self.plugin.sleeper = common.sleepy.Sleepy(\ + self.plugin.config['autoawaytime']*60, \ + self.plugin.config['autoxatime']*60) self.plugin.connected[account] = statuss.index(status) self.set_optionmenu() @@ -1349,25 +1354,25 @@ class plugin: def read_sleepy(self): """Check if we are idle""" - if self.sleeper and (self.config['autoaway'] or self.config['autoxa'])\ - and (self.roster.optionmenu.get_history()==0 or \ - self.sleeper_state!=common.sleepy.STATE_AWAKE): + if self.sleeper: self.sleeper.poll() state = self.sleeper.getState() - if state != self.sleeper_state: - if state == common.sleepy.STATE_AWAKE: - #we go online - self.roster.optionmenu.set_history(0) - self.send('STATUS', None, ('online', '')) - elif state == common.sleepy.STATE_AWAY and self.config['autoaway']: - #we go away - self.roster.optionmenu.set_history(1) - self.send('STATUS', None, ('away', 'auto away (idle)')) - elif state == common.sleepy.STATE_XAWAY and self.config['autoxa']: - #we go extended away - self.roster.optionmenu.set_history(2) - self.send('STATUS',('xa', 'auto away (idel)')) - self.sleeper_state = state + for account in self.accounts.keys(): + if self.sleeper_state[account]: + if state == common.sleepy.STATE_AWAKE and \ + self.connected[account] > 1: + #we go online + self.send('STATUS', account, ('online', '')) + elif state == common.sleepy.STATE_AWAY and \ + self.connected[account] == 1 and \ + self.config['autoaway']: + #we go away + self.send('STATUS', account, ('away', 'auto away (idle)')) + elif state == common.sleepy.STATE_XAWAY and \ + self.connected[account] == 2 and \ + self.config['autoxa']: + #we go extended away + self.send('STATUS', account, ('xa', 'auto away (idle)')) return 1 def __init__(self, quIN, quOUT): @@ -1392,6 +1397,7 @@ class plugin: self.queues = {} self.connected = {} self.nicks = {} + self.sleeper_state = {} #whether we pass auto away / xa or not for a in self.accounts.keys(): self.windows[a] = {} self.windows[a]['infos'] = {} @@ -1399,11 +1405,11 @@ class plugin: self.queues[a] = {} self.connected[a] = 0 self.nicks[a] = self.accounts[a]['name'] + self.sleeper_state[a] = 0 self.roster = roster_Window(self) gtk.timeout_add(100, self.read_queue) gtk.timeout_add(1000, self.read_sleepy) self.sleeper = None - self.sleeper_state = None gtk.main() gtk.threads_leave() diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..ecf4c94c0 --- /dev/null +++ b/setup.py @@ -0,0 +1,11 @@ +from distutils.core import setup, Extension + +module1 = Extension( 'idle', + sources = ['common/idle.cpp'], +# extra_compile_args = ['-W'], + libraries = ['gtk-x11-2.0','gdk-x11-2.0','glib-2.0','X11','Xext','Xss','atk-1.0'], + library_dirs = ['/usr/X11R6/lib'], + include_dirs = ['/usr/include/gtk-2.0', '/usr/include/glib-2.0','/usr/lib/gtk-2.0/include','/usr/lib/glib-2.0/include','/usr/include/pango-1.0','/usr/include/atk-1.0'] + ) + +setup (name = 'idle', version = '1.0', description = 'interface to X11/scrnserver.h', ext_modules = [module1])