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!

This commit is contained in:
Nikos Kouremenos 2006-12-30 00:54:20 +00:00
parent db4cacf239
commit ae40ef13ae
2 changed files with 70 additions and 53 deletions

View File

@ -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;

View File

@ -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,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