18

I would like to know how long it's been since the user last hit a key or moved the mouse - not just in my application, but on the whole "computer" (i.e. display), in order to guess whether they're still at the computer and able to observe notifications that pop up on the screen.

I'd like to do this purely from (Py)GTK+, but I am amenable to calling platform-specific functions. Ideally I'd like to call functions which have already been wrapped from Python, but if that's not possible, I'm not above a little bit of C or ctypes code, as long as I know what I'm actually looking for.

On Windows I think the function I want is GetLastInputInfo, but that doesn't seem to be wrapped by pywin32; I hope I'm missing something.

Glyph
  • 31,152
  • 11
  • 87
  • 129

3 Answers3

12

Gajim does it this way on Windows, OS X and GNU/Linux (and other *nixes):

  1. Python wrapper module (also includes Windows idle time detection code, using GetTickCount with ctypes);
  2. Ctypes-based module to get X11 idle time (using XScreenSaverQueryInfo, was a C module in old Gajim versions);
  3. C module to get OS X idle time (using HIDIdleTime system property).

Those links are to quite dated 0.12 version, so you may want to check current source for possible further improvements and changes.

devsnd
  • 7,382
  • 3
  • 42
  • 50
drdaeman
  • 11,159
  • 7
  • 59
  • 104
7

If you use PyGTK and X11 on Linux, you can do something like this, which is based on what Pidgin does:

import ctypes
import ctypes.util
import platform

class XScreenSaverInfo(ctypes.Structure):
    _fields_ = [('window', ctypes.c_long),
                ('state', ctypes.c_int),
                ('kind', ctypes.c_int),
                ('til_or_since', ctypes.c_ulong),
                ('idle', ctypes.c_ulong),
                ('eventMask', ctypes.c_ulong)]

class IdleXScreenSaver(object):
    def __init__(self):
        self.xss = self._get_library('Xss')
        self.gdk = self._get_library('gdk-x11-2.0')

        self.gdk.gdk_display_get_default.restype = ctypes.c_void_p
        # GDK_DISPLAY_XDISPLAY expands to gdk_x11_display_get_xdisplay
        self.gdk.gdk_x11_display_get_xdisplay.restype = ctypes.c_void_p
        self.gdk.gdk_x11_display_get_xdisplay.argtypes = [ctypes.c_void_p]
        # GDK_ROOT_WINDOW expands to gdk_x11_get_default_root_xwindow
        self.gdk.gdk_x11_get_default_root_xwindow.restype = ctypes.c_void_p

        self.xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
        self.xss.XScreenSaverQueryExtension.restype = ctypes.c_int
        self.xss.XScreenSaverQueryExtension.argtypes = [ctypes.c_void_p,
                                                        ctypes.POINTER(ctypes.c_int),
                                                        ctypes.POINTER(ctypes.c_int)]
        self.xss.XScreenSaverQueryInfo.restype = ctypes.c_int
        self.xss.XScreenSaverQueryInfo.argtypes = [ctypes.c_void_p,
                                                   ctypes.c_void_p,
                                                   ctypes.POINTER(XScreenSaverInfo)]

        # gtk_init() must have been called for this to work
        import gtk
        gtk  # pyflakes

        # has_extension = XScreenSaverQueryExtension(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
        #                                            &event_base, &error_base);
        event_base = ctypes.c_int()
        error_base = ctypes.c_int()
        gtk_display = self.gdk.gdk_display_get_default()
        self.dpy = self.gdk.gdk_x11_display_get_xdisplay(gtk_display)
        available = self.xss.XScreenSaverQueryExtension(self.dpy,
                                                        ctypes.byref(event_base),
                                                        ctypes.byref(error_base))
        if available == 1:
            self.xss_info = self.xss.XScreenSaverAllocInfo()
        else:
            self.xss_info = None

    def _get_library(self, libname):
        path = ctypes.util.find_library(libname)
        if not path:
            raise ImportError('Could not find library "%s"' % (libname, ))
        lib = ctypes.cdll.LoadLibrary(path)
        assert lib
        return lib

    def get_idle(self):
        if not self.xss_info:
            return 0

        # XScreenSaverQueryInfo(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
        #                       GDK_ROOT_WINDOW(), mit_info);
        drawable = self.gdk.gdk_x11_get_default_root_xwindow()
        self.xss.XScreenSaverQueryInfo(self.dpy, drawable, self.xss_info)
        # return (mit_info->idle) / 1000;
        return self.xss_info.contents.idle / 1000

The example above uses gdk via ctypes to be able to access the X11 specific. Xscreensaver APIs also need to be accessed via ctypes.

It should be pretty easy to port it to use PyGI and introspection.

Johan Dahlin
  • 25,300
  • 6
  • 40
  • 55
  • It might be interesting to port these `ctypes` interfaces to https://pypi.python.org/pypi/cffi instead. – Glyph May 28 '13 at 04:38
  • yeah, probably a good idea. This comment was based on my own program and I wanted to avoid adding the cffi dependency there. Hopefully cffi will enter into CPython at some point. – Johan Dahlin May 28 '13 at 22:19
  • I really hope so. Frankly they should do a 2.7 maintenance release that includes it. – Glyph May 29 '13 at 22:53
  • The code has to be run in **python2** to work. For testing try: `time.sleep(2.1); print IdleXScreenSaver().get_idle()` – SurpriseDog Mar 14 '21 at 17:51
2

I got an answer regarding mouse-clicks suggesting to use pyHook:

Detecting Mouse clicks in windows using python

Here's some other code I did to detect mouse-position via ctypes: http://monkut.webfactional.com/blog/archive/2008/10/2/python-win-mouse-position

A more round-about method to accomplish this would be via screen capture and comparing any change in images using PIL.

http://www.wellho.net/forum/Programming-in-Python-and-Ruby/Python-Imaging-Library-PIL.html

Community
  • 1
  • 1
monkut
  • 42,176
  • 24
  • 124
  • 155