From 2529039f49f0d078c9721f7b0787435e80004706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Tue, 11 Aug 2020 15:28:47 +0200 Subject: [PATCH] Fixed getIdle for all platforms --- actor/src/udsactor/linux/xss.py | 45 ++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/actor/src/udsactor/linux/xss.py b/actor/src/udsactor/linux/xss.py index d655c4ab6..9b97d8d2f 100644 --- a/actor/src/udsactor/linux/xss.py +++ b/actor/src/udsactor/linux/xss.py @@ -47,30 +47,59 @@ class XScreenSaverInfo(ctypes.Structure): # pylint: disable=too-few-public-meth ('idle', ctypes.c_ulong), ('eventMask', ctypes.c_ulong)] +class c_ptr(ctypes.c_void_p): + pass + def _ensureInitialized(): global xlib, xss, xssInfo, display, initialized # pylint: disable=global-statement + if initialized: return initialized = True - # Initialize xlib & xss try: xlibPath = ctypes.util.find_library('X11') xssPath = ctypes.util.find_library('Xss') xlib = xss = None if not xlibPath or not xssPath: - raise Exception() + raise Exception('Library Not found!!') + xlib = ctypes.cdll.LoadLibrary(xlibPath) xss = ctypes.cdll.LoadLibrary(xssPath) # Fix result type to XScreenSaverInfo Structure xss.XScreenSaverQueryExtension.restype = ctypes.c_int + xss.XScreenSaverQueryExtension.argtypes = [ + ctypes.c_void_p, + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int) + ] xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo) # Result in a XScreenSaverInfo structure + xss.XScreenSaverQueryInfo.argtypes = [ + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.POINTER(XScreenSaverInfo) + ] + xlib.XOpenDisplay.argtypes = [ctypes.c_char_p] + xlib.XOpenDisplay.restype = c_ptr + display = xlib.XOpenDisplay(None) + + if not display.value: + raise Exception('Display not found!') # Invalid display, not accesible + xssInfo = xss.XScreenSaverAllocInfo() - if display <= 0: - raise Exception() # Invalid display, not accesible + + # Ensures screen saver extension is available + event_base = ctypes.c_int() + error_base = ctypes.c_int() + + available = xss.XScreenSaverQueryExtension(display, ctypes.byref(event_base), ctypes.byref(error_base)) + + if available != 1: + raise Exception('ScreenSaver not available') + except Exception: # Libraries not accesible, not found or whatever.. xlib = xss = display = xssInfo = None @@ -90,14 +119,6 @@ def getIdleDuration() -> float: if not initialized or not xlib or not xss or not xssInfo: return 0 # Libraries not available - event_base = ctypes.c_int() - error_base = ctypes.c_int() - - available = xss.XScreenSaverQueryExtension(display, ctypes.byref(event_base), ctypes.byref(error_base)) - - if available != 1: - return 0 # No screen saver is available, no way of getting idle - xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), xssInfo) # States: 0 = off, 1 = On, 2 = Cycle, 3 = Disabled, ...?