| 1 | /* common/idle.c |
|---|
| 2 | * |
|---|
| 3 | * Gajim Team: |
|---|
| 4 | * - Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 7 | * Vincent Hanquez <tab@snarc.org> |
|---|
| 8 | * Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 9 | * Vincent Hanquez <tab@snarc.org> |
|---|
| 10 | * Nikos Kouremenos <nkour@jabber.org> |
|---|
| 11 | * Dimitur Kirov <dkirov@gmail.com> |
|---|
| 12 | * Travis Shirk <travis@pobox.com> |
|---|
| 13 | * Norman Rasmussen <norman@rasmussen.co.za> |
|---|
| 14 | * |
|---|
| 15 | * This program is free software; you can redistribute it and/or modify |
|---|
| 16 | * it under the terms of the GNU General Public License as published |
|---|
| 17 | * by the Free Software Foundation; version 2 only. |
|---|
| 18 | * |
|---|
| 19 | * This program is distributed in the hope that it will be useful, |
|---|
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | * GNU General Public License for more details. |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | #ifndef _WIN32 |
|---|
| 26 | #include <X11/Xlib.h> |
|---|
| 27 | #include <X11/Xutil.h> |
|---|
| 28 | #include <X11/extensions/scrnsaver.h> |
|---|
| 29 | #else |
|---|
| 30 | #define _WIN32_WINNT 0x0500 |
|---|
| 31 | #include <windows.h> |
|---|
| 32 | #define EXPORT __declspec(dllexport) |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #include <Python.h> |
|---|
| 36 | |
|---|
| 37 | #ifdef _WIN32 |
|---|
| 38 | typedef BOOL (WINAPI *GETLASTINPUTINFO)(LASTINPUTINFO *); |
|---|
| 39 | static HMODULE g_user32 = NULL; |
|---|
| 40 | static GETLASTINPUTINFO g_GetLastInputInfo = NULL; |
|---|
| 41 | #else |
|---|
| 42 | Display *display; |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | static PyObject * idle_init(PyObject *self, PyObject *args) |
|---|
| 47 | { |
|---|
| 48 | #ifndef _WIN32 |
|---|
| 49 | display = XOpenDisplay(NULL); |
|---|
| 50 | #else |
|---|
| 51 | g_user32 = LoadLibrary("user32.dll"); |
|---|
| 52 | if (g_user32) { |
|---|
| 53 | g_GetLastInputInfo = (GETLASTINPUTINFO)GetProcAddress(g_user32, "GetLastInputInfo"); |
|---|
| 54 | } |
|---|
| 55 | #endif |
|---|
| 56 | Py_INCREF(Py_None); |
|---|
| 57 | return Py_None; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | static PyObject * idle_getIdleSec(PyObject *self, PyObject *args) |
|---|
| 61 | { |
|---|
| 62 | #ifndef _WIN32 |
|---|
| 63 | static XScreenSaverInfo *mit_info = NULL; |
|---|
| 64 | int idle_time, event_base, error_base; |
|---|
| 65 | #else |
|---|
| 66 | int idle_time = 0; |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | #ifndef _WIN32 |
|---|
| 70 | if (XScreenSaverQueryExtension(display, &event_base, &error_base)) |
|---|
| 71 | { |
|---|
| 72 | if (mit_info == NULL) |
|---|
| 73 | mit_info = XScreenSaverAllocInfo(); |
|---|
| 74 | XScreenSaverQueryInfo(display, RootWindow(display, 0), mit_info); |
|---|
| 75 | idle_time = (mit_info->idle) / 1000; |
|---|
| 76 | } |
|---|
| 77 | else |
|---|
| 78 | idle_time = 0; |
|---|
| 79 | #else |
|---|
| 80 | if (g_GetLastInputInfo != NULL) { |
|---|
| 81 | LASTINPUTINFO lii; |
|---|
| 82 | memset(&lii, 0, sizeof(lii)); |
|---|
| 83 | lii.cbSize = sizeof(lii); |
|---|
| 84 | if (g_GetLastInputInfo(&lii)) { |
|---|
| 85 | idle_time = lii.dwTime; |
|---|
| 86 | } |
|---|
| 87 | idle_time = (GetTickCount() - idle_time) / 1000; |
|---|
| 88 | } |
|---|
| 89 | #endif |
|---|
| 90 | return Py_BuildValue("i", idle_time); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | static PyObject * idle_close(PyObject *self, PyObject *args) |
|---|
| 94 | { |
|---|
| 95 | #ifndef _WIN32 |
|---|
| 96 | XCloseDisplay(display); |
|---|
| 97 | #else |
|---|
| 98 | if (g_user32 != NULL) |
|---|
| 99 | FreeLibrary(g_user32); |
|---|
| 100 | #endif |
|---|
| 101 | Py_INCREF(Py_None); |
|---|
| 102 | return Py_None; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | static PyMethodDef idleMethods[] = |
|---|
| 106 | { |
|---|
| 107 | {"init", idle_init, METH_VARARGS, "init idle"}, |
|---|
| 108 | {"getIdleSec", idle_getIdleSec, METH_VARARGS, "Give the idle time in secondes"}, |
|---|
| 109 | {"close", idle_close, METH_VARARGS, "close idle"}, |
|---|
| 110 | {NULL, NULL, 0, NULL} |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | PyMODINIT_FUNC |
|---|
| 114 | initidle(void) |
|---|
| 115 | { |
|---|
| 116 | (void) Py_InitModule("idle", idleMethods); |
|---|
| 117 | } |
|---|