From ae40ef13ae7db11b8c6ad455d6a82ea47b1b481e Mon Sep 17 00:00:00 2001 From: Nikos Kouremenos Date: Sat, 30 Dec 2006 00:54:20 +0000 Subject: [PATCH] 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! --- src/common/idle.c | 36 +----------------- src/common/sleepy.py | 87 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 70 insertions(+), 53 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 * Vincent Hanquez - * Copyright (C) 2005 Yann Le Boulanger - * Vincent Hanquez + * Copyright (C) 2005-2006 Yann Le Boulanger * Nikos Kouremenos - * Dimitur Kirov - * Travis Shirk - * Norman Rasmussen * * 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 #include #include -#else - #define _WIN32_WINNT 0x0500 - #include - #define EXPORT __declspec(dllexport) #endif #include -#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 ## Vincent Hanquez -## Copyright (C) 2005 Yann Le Boulanger -## Vincent Hanquez +## Copyright (C) 2005-2006 Yann Le Boulanger ## Nikos Kouremenos -## Dimitur Kirov -## Travis Shirk -## Norman Rasmussen ## ## 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,29 +30,39 @@ 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: - - def __init__(self, away_interval = 60, xa_interval = 120): +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 - try: - idle.init() - except: - SUPPORTED = False - self.state = STATE_UNKNOWN + + 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''' - 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 +76,47 @@ class Sleepy: def getState(self): return self.state - def setState(self,val): + def setState(self, val): self.state = val + +class SleepyUnix: + + 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 + try: + idle.init() + except: + 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 = 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 + +if os.name == 'nt': + Sleepy = SleepyWindows +else: + Sleepy = SleepyUnix