Package common :: Module idle
[hide private]
[frames] | no frames]

Source Code for Module common.idle

  1  ## src/common/idle.py 
  2  ## 
  3  ## (C) 2008 Thorsten P. 'dGhvcnN0ZW5wIEFUIHltYWlsIGNvbQ==\n'.decode("base64") 
  4  ## 
  5  ## This file is part of Gajim. 
  6  ## 
  7  ## Gajim is free software; you can redistribute it and/or modify 
  8  ## it under the terms of the GNU General Public License as published 
  9  ## by the Free Software Foundation; version 3 only. 
 10  ## 
 11  ## Gajim is distributed in the hope that it will be useful, 
 12  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 14  ## GNU General Public License for more details. 
 15  ## 
 16  ## You should have received a copy of the GNU General Public License 
 17  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
 18   
 19  import ctypes 
 20  import ctypes.util 
 21   
22 -class XScreenSaverInfo(ctypes.Structure):
23 _fields_ = [ 24 ('window', ctypes.c_ulong), 25 ('state', ctypes.c_int), 26 ('kind', ctypes.c_int), 27 ('til_or_since', ctypes.c_ulong), 28 ('idle', ctypes.c_ulong), 29 ('eventMask', ctypes.c_ulong) 30 ]
31 XScreenSaverInfo_p = ctypes.POINTER(XScreenSaverInfo) 32 33 display_p = ctypes.c_void_p 34 xid = ctypes.c_ulong 35 c_int_p = ctypes.POINTER(ctypes.c_int) 36 37 try: 38 libX11path = ctypes.util.find_library('X11') 39 if libX11path == None: 40 raise OSError('libX11 could not be found.') 41 libX11 = ctypes.cdll.LoadLibrary(libX11path) 42 libX11.XOpenDisplay.restype = display_p 43 libX11.XOpenDisplay.argtypes = ctypes.c_char_p, 44 libX11.XDefaultRootWindow.restype = xid 45 libX11.XDefaultRootWindow.argtypes = display_p, 46 47 libXsspath = ctypes.util.find_library('Xss') 48 if libXsspath == None: 49 raise OSError('libXss could not be found.') 50 libXss = ctypes.cdll.LoadLibrary(libXsspath) 51 libXss.XScreenSaverQueryExtension.argtypes = display_p, c_int_p, c_int_p 52 libXss.XScreenSaverAllocInfo.restype = XScreenSaverInfo_p 53 libXss.XScreenSaverQueryInfo.argtypes = (display_p, xid, XScreenSaverInfo_p) 54 55 dpy_p = libX11.XOpenDisplay(None) 56 if dpy_p == None: 57 raise OSError('Could not open X Display.') 58 59 _event_basep = ctypes.c_int() 60 _error_basep = ctypes.c_int() 61 if libXss.XScreenSaverQueryExtension(dpy_p, ctypes.byref(_event_basep), 62 ctypes.byref(_error_basep)) == 0: 63 raise OSError('XScreenSaver Extension not available on display.') 64 65 xss_info_p = libXss.XScreenSaverAllocInfo() 66 if xss_info_p == None: 67 raise OSError('XScreenSaverAllocInfo: Out of Memory.') 68 69 rootwindow = libX11.XDefaultRootWindow(dpy_p) 70 xss_available = True 71 except OSError, e: 72 # Logging? 73 xss_available = False 74
75 -def getIdleSec():
76 global xss_available 77 """ 78 Return the idle time in seconds 79 """ 80 if not xss_available: 81 return 0 82 if libXss.XScreenSaverQueryInfo(dpy_p, rootwindow, xss_info_p) == 0: 83 return 0 84 else: 85 return int(xss_info_p.contents.idle) / 1000
86
87 -def close():
88 global xss_available 89 if xss_available: 90 libX11.XFree(xss_info_p) 91 libX11.XCloseDisplay(dpy_p) 92 xss_available = False
93 94 if __name__ == '__main__': 95 import time 96 time.sleep(2.1) 97 print getIdleSec() 98 close() 99 print getIdleSec() 100