2005-01-07 22:52:38 +01:00
|
|
|
/* common/idle.c
|
|
|
|
*
|
|
|
|
* Gajim Team:
|
|
|
|
* - Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
* - Vincent Hanquez <tab@snarc.org>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2005 Gajim Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published
|
|
|
|
* by the Free Software Foundation; version 2 only.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2005-02-15 23:53:54 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
#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;
|
2005-07-18 16:35:32 +02:00
|
|
|
#else
|
|
|
|
Display *display;
|
2005-02-15 23:53:54 +01:00
|
|
|
#endif
|
2004-05-28 06:20:30 +02:00
|
|
|
|
|
|
|
|
2004-05-28 19:54:40 +02:00
|
|
|
static PyObject * idle_init(PyObject *self, PyObject *args)
|
2005-02-15 23:56:49 +01:00
|
|
|
{
|
2005-02-15 23:53:54 +01:00
|
|
|
#ifndef _WIN32
|
2005-07-18 16:35:32 +02:00
|
|
|
display = XOpenDisplay(NULL);
|
2005-02-15 23:53:54 +01:00
|
|
|
#else
|
|
|
|
g_user32 = LoadLibrary("user32.dll");
|
|
|
|
if (g_user32) {
|
|
|
|
g_GetLastInputInfo = (GETLASTINPUTINFO)GetProcAddress(g_user32, "GetLastInputInfo");
|
|
|
|
}
|
|
|
|
#endif
|
2004-05-28 06:20:30 +02:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2004-05-28 19:54:40 +02:00
|
|
|
static PyObject * idle_getIdleSec(PyObject *self, PyObject *args)
|
|
|
|
{
|
2005-02-15 23:53:54 +01:00
|
|
|
#ifndef _WIN32
|
2004-05-28 06:20:30 +02:00
|
|
|
static XScreenSaverInfo *mit_info = NULL;
|
|
|
|
int idle_time, event_base, error_base;
|
2005-02-15 23:53:54 +01:00
|
|
|
#else
|
|
|
|
int idle_time = 0;
|
|
|
|
#endif
|
2004-05-28 06:20:30 +02:00
|
|
|
|
2005-02-15 23:53:54 +01:00
|
|
|
#ifndef _WIN32
|
2005-07-18 23:24:58 +02:00
|
|
|
if (!XScreenSaverQueryExtension(display, &event_base, &error_base))
|
2004-05-28 19:54:40 +02:00
|
|
|
{
|
|
|
|
if (mit_info == NULL)
|
2004-05-28 06:20:30 +02:00
|
|
|
mit_info = XScreenSaverAllocInfo();
|
2005-07-18 23:24:58 +02:00
|
|
|
XScreenSaverQueryInfo(display, RootWindow(display, 0), mit_info);
|
2004-05-28 06:20:30 +02:00
|
|
|
idle_time = (mit_info->idle) / 1000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
idle_time = 0;
|
2005-02-15 23:53:54 +01:00
|
|
|
#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
|
2004-05-28 06:20:30 +02:00
|
|
|
return Py_BuildValue("i", idle_time);
|
|
|
|
}
|
|
|
|
|
2004-05-28 19:54:40 +02:00
|
|
|
static PyObject * idle_close(PyObject *self, PyObject *args)
|
2005-02-15 23:56:49 +01:00
|
|
|
{
|
2005-02-15 23:53:54 +01:00
|
|
|
#ifndef _WIN32
|
2005-07-18 16:35:32 +02:00
|
|
|
XCloseDisplay(display);
|
2005-02-15 23:53:54 +01:00
|
|
|
#else
|
|
|
|
if (g_user32 != NULL)
|
|
|
|
FreeLibrary(g_user32);
|
|
|
|
#endif
|
2004-05-28 06:20:30 +02:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2004-05-28 19:54:40 +02:00
|
|
|
static PyMethodDef idleMethods[] =
|
|
|
|
{
|
2005-07-18 16:35:32 +02:00
|
|
|
{"init", idle_init, METH_VARARGS, "init idle"},
|
2004-05-28 06:20:30 +02:00
|
|
|
{"getIdleSec", idle_getIdleSec, METH_VARARGS, "Give the idle time in secondes"},
|
2005-07-18 16:35:32 +02:00
|
|
|
{"close", idle_close, METH_VARARGS, "close idle"},
|
2004-05-28 06:20:30 +02:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
initidle(void)
|
|
|
|
{
|
|
|
|
(void) Py_InitModule("idle", idleMethods);
|
|
|
|
}
|