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:
parent
db4cacf239
commit
ae40ef13ae
2 changed files with 70 additions and 53 deletions
|
@ -5,12 +5,8 @@
|
||||||
*
|
*
|
||||||
* Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
|
* Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
|
||||||
* Vincent Hanquez <tab@snarc.org>
|
* Vincent Hanquez <tab@snarc.org>
|
||||||
* Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
|
* Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org>
|
||||||
* Vincent Hanquez <tab@snarc.org>
|
|
||||||
* Nikos Kouremenos <nkour@jabber.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
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published
|
* it under the terms of the GNU General Public License as published
|
||||||
|
@ -26,19 +22,11 @@
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <X11/Xutil.h>
|
#include <X11/Xutil.h>
|
||||||
#include <X11/extensions/scrnsaver.h>
|
#include <X11/extensions/scrnsaver.h>
|
||||||
#else
|
|
||||||
#define _WIN32_WINNT 0x0500
|
|
||||||
#include <windows.h>
|
|
||||||
#define EXPORT __declspec(dllexport)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifndef _WIN32
|
||||||
typedef BOOL (WINAPI *GETLASTINPUTINFO)(LASTINPUTINFO *);
|
|
||||||
static HMODULE g_user32 = NULL;
|
|
||||||
static GETLASTINPUTINFO g_GetLastInputInfo = NULL;
|
|
||||||
#else
|
|
||||||
Display *display;
|
Display *display;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -47,11 +35,6 @@ static PyObject * idle_init(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
display = XOpenDisplay(NULL);
|
display = XOpenDisplay(NULL);
|
||||||
#else
|
|
||||||
g_user32 = LoadLibrary("user32.dll");
|
|
||||||
if (g_user32) {
|
|
||||||
g_GetLastInputInfo = (GETLASTINPUTINFO)GetProcAddress(g_user32, "GetLastInputInfo");
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
|
@ -62,8 +45,6 @@ static PyObject * idle_getIdleSec(PyObject *self, PyObject *args)
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
static XScreenSaverInfo *mit_info = NULL;
|
static XScreenSaverInfo *mit_info = NULL;
|
||||||
int idle_time, event_base, error_base;
|
int idle_time, event_base, error_base;
|
||||||
#else
|
|
||||||
int idle_time = 0;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
@ -76,16 +57,6 @@ static PyObject * idle_getIdleSec(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
idle_time = 0;
|
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
|
#endif
|
||||||
return Py_BuildValue("i", idle_time);
|
return Py_BuildValue("i", idle_time);
|
||||||
}
|
}
|
||||||
|
@ -94,9 +65,6 @@ static PyObject * idle_close(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
XCloseDisplay(display);
|
XCloseDisplay(display);
|
||||||
#else
|
|
||||||
if (g_user32 != NULL)
|
|
||||||
FreeLibrary(g_user32);
|
|
||||||
#endif
|
#endif
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
|
|
|
@ -6,12 +6,8 @@
|
||||||
##
|
##
|
||||||
## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
|
## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
|
||||||
## Vincent Hanquez <tab@snarc.org>
|
## Vincent Hanquez <tab@snarc.org>
|
||||||
## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
|
## Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org>
|
||||||
## Vincent Hanquez <tab@snarc.org>
|
|
||||||
## Nikos Kouremenos <kourem@gmail.com>
|
## 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
|
## This program is free software; you can redistribute it and/or modify
|
||||||
## it under the terms of the GNU General Public License as published
|
## it under the terms of the GNU General Public License as published
|
||||||
|
@ -24,6 +20,7 @@
|
||||||
##
|
##
|
||||||
|
|
||||||
from common import gajim
|
from common import gajim
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
STATE_UNKNOWN = 'OS probably not supported'
|
STATE_UNKNOWN = 'OS probably not supported'
|
||||||
|
@ -33,29 +30,39 @@ STATE_AWAKE = 'awake'
|
||||||
|
|
||||||
SUPPORTED = True
|
SUPPORTED = True
|
||||||
try:
|
try:
|
||||||
|
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
|
import idle
|
||||||
except:
|
except:
|
||||||
gajim.log.debug('Unable to load idle module')
|
gajim.log.debug('Unable to load idle module')
|
||||||
SUPPORTED = False
|
SUPPORTED = False
|
||||||
|
|
||||||
class Sleepy:
|
class SleepyWindows:
|
||||||
|
|
||||||
def __init__(self, away_interval = 60, xa_interval = 120):
|
def __init__(self, away_interval = 60, xa_interval = 120):
|
||||||
self.away_interval = away_interval
|
self.away_interval = away_interval
|
||||||
self.xa_interval = xa_interval
|
self.xa_interval = xa_interval
|
||||||
self.state = STATE_AWAKE # assume we are awake
|
self.state = STATE_AWAKE # assume we are awake
|
||||||
try:
|
|
||||||
idle.init()
|
def getIdleSec(self):
|
||||||
except:
|
GetLastInputInfo(ctypes.byref(lastInputInfo))
|
||||||
SUPPORTED = False
|
idleDelta = float(GetTickCount() - lastInputInfo.dwTime) / 1000
|
||||||
self.state = STATE_UNKNOWN
|
return idleDelta
|
||||||
|
|
||||||
def poll(self):
|
def poll(self):
|
||||||
'''checks to see if we should change state'''
|
'''checks to see if we should change state'''
|
||||||
if not SUPPORTED:
|
idleTime = self.getIdleSec()
|
||||||
return False
|
|
||||||
|
|
||||||
idleTime = idle.getIdleSec()
|
|
||||||
|
|
||||||
# xa is stronger than away so check for xa first
|
# xa is stronger than away so check for xa first
|
||||||
if idleTime > self.xa_interval:
|
if idleTime > self.xa_interval:
|
||||||
|
@ -71,3 +78,45 @@ class Sleepy:
|
||||||
|
|
||||||
def setState(self, val):
|
def setState(self, val):
|
||||||
self.state = 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
|
||||||
|
|
Loading…
Add table
Reference in a new issue