Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Kouremenos <kourem@gmail.com>2006-12-30 03:54:20 +0300
committerNikos Kouremenos <kourem@gmail.com>2006-12-30 03:54:20 +0300
commitae40ef13ae7db11b8c6ad455d6a82ea47b1b481e (patch)
tree5e8358bd9f247e774f9dcb318d2a545ab9765e0e
parentdb4cacf2396be363807efd20de08f14dd298b358 (diff)
make idle detection work in Windows without C compilation stuff (use ctypes and do the call to user32.dll via python [py25 includes ctypes else get it externally]. an Xmas gift from to Yann for .11 wonderful release!
-rw-r--r--src/common/idle.c36
-rw-r--r--src/common/sleepy.py67
2 files changed, 60 insertions, 43 deletions
diff --git a/src/common/idle.c b/src/common/idle.c
index 7a7badc71..52fca6477 100644
--- a/src/common/idle.c
+++ b/src/common/idle.c
@@ -5,12 +5,8 @@
*
* Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
* Vincent Hanquez <tab@snarc.org>
- * Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
- * Vincent Hanquez <tab@snarc.org>
+ * Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org>
* Nikos Kouremenos <nkour@jabber.org>
- * Dimitur Kirov <dkirov@gmail.com>
- * Travis Shirk <travis@pobox.com>
- * Norman Rasmussen <norman@rasmussen.co.za>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
@@ -26,19 +22,11 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>
-#else
- #define _WIN32_WINNT 0x0500
- #include <windows.h>
- #define EXPORT __declspec(dllexport)
#endif
#include <Python.h>
-#ifdef _WIN32
- typedef BOOL (WINAPI *GETLASTINPUTINFO)(LASTINPUTINFO *);
- static HMODULE g_user32 = NULL;
- static GETLASTINPUTINFO g_GetLastInputInfo = NULL;
-#else
+#ifndef _WIN32
Display *display;
#endif
@@ -47,11 +35,6 @@ static PyObject * idle_init(PyObject *self, PyObject *args)
{
#ifndef _WIN32
display = XOpenDisplay(NULL);
-#else
- g_user32 = LoadLibrary("user32.dll");
- if (g_user32) {
- g_GetLastInputInfo = (GETLASTINPUTINFO)GetProcAddress(g_user32, "GetLastInputInfo");
- }
#endif
Py_INCREF(Py_None);
return Py_None;
@@ -62,8 +45,6 @@ static PyObject * idle_getIdleSec(PyObject *self, PyObject *args)
#ifndef _WIN32
static XScreenSaverInfo *mit_info = NULL;
int idle_time, event_base, error_base;
-#else
- int idle_time = 0;
#endif
#ifndef _WIN32
@@ -76,16 +57,6 @@ static PyObject * idle_getIdleSec(PyObject *self, PyObject *args)
}
else
idle_time = 0;
-#else
- if (g_GetLastInputInfo != NULL) {
- LASTINPUTINFO lii;
- memset(&lii, 0, sizeof(lii));
- lii.cbSize = sizeof(lii);
- if (g_GetLastInputInfo(&lii)) {
- idle_time = lii.dwTime;
- }
- idle_time = (GetTickCount() - idle_time) / 1000;
- }
#endif
return Py_BuildValue("i", idle_time);
}
@@ -94,9 +65,6 @@ static PyObject * idle_close(PyObject *self, PyObject *args)
{
#ifndef _WIN32
XCloseDisplay(display);
-#else
- if (g_user32 != NULL)
- FreeLibrary(g_user32);
#endif
Py_INCREF(Py_None);
return Py_None;
diff --git a/src/common/sleepy.py b/src/common/sleepy.py
index a0ce13ae7..7ba700809 100644
--- a/src/common/sleepy.py
+++ b/src/common/sleepy.py
@@ -6,12 +6,8 @@
##
## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
## Vincent Hanquez <tab@snarc.org>
-## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
-## Vincent Hanquez <tab@snarc.org>
+## Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org>
## Nikos Kouremenos <kourem@gmail.com>
-## Dimitur Kirov <dkirov@gmail.com>
-## Travis Shirk <travis@pobox.com>
-## Norman Rasmussen <norman@rasmussen.co.za>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
@@ -24,6 +20,7 @@
##
from common import gajim
+import os
STATE_UNKNOWN = 'OS probably not supported'
@@ -33,12 +30,56 @@ STATE_AWAKE = 'awake'
SUPPORTED = True
try:
- import idle
+ if os.name == 'nt':
+ import ctypes
+
+ GetTickCount = ctypes.windll.kernel32.GetTickCount
+ GetLastInputInfo = ctypes.windll.user32.GetLastInputInfo
+
+ class LASTINPUTINFO(ctypes.Structure):
+ _fields_ = [('cbSize', ctypes.c_uint),
+ ('dwTime', ctypes.c_uint)]
+
+ lastInputInfo = LASTINPUTINFO()
+ lastInputInfo.cbSize = ctypes.sizeof(lastInputInfo)
+
+ else: # unix
+ import idle
except:
gajim.log.debug('Unable to load idle module')
SUPPORTED = False
-class Sleepy:
+class SleepyWindows:
+ def __init__(self, away_interval = 60, xa_interval = 120):
+ self.away_interval = away_interval
+ self.xa_interval = xa_interval
+ self.state = STATE_AWAKE # assume we are awake
+
+ def getIdleSec(self):
+ GetLastInputInfo(ctypes.byref(lastInputInfo))
+ idleDelta = float(GetTickCount() - lastInputInfo.dwTime) / 1000
+ return idleDelta
+
+ def poll(self):
+ '''checks to see if we should change state'''
+ idleTime = self.getIdleSec()
+
+ # xa is stronger than away so check for xa first
+ if idleTime > self.xa_interval:
+ self.state = STATE_XA
+ elif idleTime > self.away_interval:
+ self.state = STATE_AWAY
+ else:
+ self.state = STATE_AWAKE
+ return True
+
+ def getState(self):
+ return self.state
+
+ def setState(self, val):
+ self.state = val
+
+class SleepyUnix:
def __init__(self, away_interval = 60, xa_interval = 120):
self.away_interval = away_interval
@@ -50,12 +91,15 @@ class Sleepy:
SUPPORTED = False
self.state = STATE_UNKNOWN
+ def getIdleSec(self):
+ return idle.getIdleSec()
+
def poll(self):
'''checks to see if we should change state'''
if not SUPPORTED:
return False
- idleTime = idle.getIdleSec()
+ idleTime = self.getIdleSec()
# xa is stronger than away so check for xa first
if idleTime > self.xa_interval:
@@ -69,5 +113,10 @@ class Sleepy:
def getState(self):
return self.state
- def setState(self,val):
+ def setState(self, val):
self.state = val
+
+if os.name == 'nt':
+ Sleepy = SleepyWindows
+else:
+ Sleepy = SleepyUnix