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