Schurger.org

28 mai 2008

get X idle time with python

Classé dans : Code — Jean Schurger @ 20:46
#!/usr/bin/python

import ctypes, os

class XScreenSaverInfo(ctypes.Structure):
    """ typedef struct { ... } XScreenSaverInfo; """
    _fields_ = [('window',      ctypes.c_ulong), # screen saver window
                ('state',       ctypes.c_int),   # off,on,disabled
                ('kind',        ctypes.c_int),   # blanked,internal,external
                ('since',       ctypes.c_ulong), # milliseconds
                ('idle',        ctypes.c_ulong), # milliseconds
                ('event_mask',  ctypes.c_ulong)] # events

class XScreenSaverSession(object):
    def __init__( self):
        self.xlib = ctypes.cdll.LoadLibrary( 'libX11.so')
        self.dpy = self.xlib.XOpenDisplay( os.environ['DISPLAY'])
        if not self.dpy:
            raise Exception('Cannot open display')
        self.root = self.xlib.XDefaultRootWindow( self.dpy)
        self.xss = ctypes.cdll.LoadLibrary( 'libXss.so.1')
        self.xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
        self.xss_info = self.xss.XScreenSaverAllocInfo()

    def get_idle( self):
        self.xss.XScreenSaverQueryInfo( self.dpy, self.root, self.xss_info)
        return self.xss_info.contents.idle / 1000

if __name__ == "__main__":
    s = XScreenSaverSession()
    print s.get_idle()

Un commentaire sur “get X idle time with python”

  1. [...] through a database full of empty data to find something useful is frustrating. Thanks to Jean Schurger, I’ve integrated a method to check the idle time in X. Nothing will be logged when the [...]

Laisser une réponse

Powered by WordPress