Created
May 17, 2011 20:52
-
-
Save thouis/977370 to your computer and use it in GitHub Desktop.
idle time in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://thp.io/2007/09/x11-idle-time-and-focused-window-in.html | |
# http://www.freedesktop.org/software/ConsoleKit/doc/ConsoleKit.html#Session:idle-hint | |
import Xlib.display | |
display = Xlib.display.Display() | |
focus = display.get_input_focus() | |
print "WM Class: %s" % ( focus.focus.get_wm_class(), ) | |
print "WM Name: %s" % ( focus.focus.get_wm_name(), ) | |
X11 Idle Time using the XScreenSaver extension | |
import ctypes | |
import 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 | |
xlib = ctypes.cdll.LoadLibrary( 'libX11.so') | |
dpy = xlib.XOpenDisplay( os.environ['DISPLAY']) | |
root = xlib.XDefaultRootWindow( dpy) | |
xss = ctypes.cdll.LoadLibrary( 'libXss.so') | |
xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo) | |
xss_info = xss.XScreenSaverAllocInfo() | |
xss.XScreenSaverQueryInfo( dpy, root, xss_info) | |
print "Idle time in milliseconds: %d" % ( xss_info.contents.idle, ) | |
If |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment