#!/usr/bin/env python2 from ctypes import *; import ctypes; import os; import sys; import getopt; import time; import configparser; import string; 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 PowerMan: def __init__( self): self.state=True; self.suspended=False; if(os.path.isfile(os.path.join(os.getenv('HOME'),'.powerman.cfg'))): config=configparser.RawConfigParser(); config.read(os.path.join(os.getenv('HOME'),'.powerman.cfg')); self.stime=int(config.getint('Base', 'time')); self.prog=config.get('Base', 'program'); else: self.stime=20; self.prog=""; opts, args=getopt.getopt(sys.argv[1:], "t:p:"); for o,a in opts: if o=='-t': self.stime=a; if o=='-p': self.prog=a; display_p=ctypes.c_void_p; xid=ctypes.c_ulong; self.xlib = ctypes.cdll.LoadLibrary('libX11.so.6'); self.xlib.XOpenDisplay.restype=display_p self.xlib.XOpenDisplay.argtypes=ctypes.c_char_p, self.xlib.XDefaultRootWindow.restype=xid self.xlib.XDefaultRootWindow.argtypes=display_p, self.dpy=self.xlib.XOpenDisplay(None); 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): # Zde pada self.xss.XScreenSaverQueryInfo(self.dpy,self.root,self.xss_info) self.idle=self.xss_info.contents.idle/1000; def get_idle_workaround(self): dummy=c_int(); standby=c_int(); suspend=c_int(); state=c_int(); onoff=c_int(); off=c_int(); dpms=ctypes.cdll.LoadLibrary("libXext.so"); # Zde pada if dpms.DPMSQueryExtension(self.dpy,byref(dummy),byref(dummy))==True: if dpms.DPMSCapable(self.dpy)==True: dpms.DPMSGetTimeouts(self.dpy,byref(standby),byref(suspend),byref(off)); dpms.DPMSInfo(self.dpy,byref(state),byref(onoff)); if int(onoff.value)==1: pstate=int(state.value); pstandby=int(standby.value); psuspend=int(suspend.value) poff=int(off.value); #DPMSModeStandby if pstate==1: if self.idle=int(self.stime): self.suspended=True; pid=os.fork(); if pid==0: self.idle=0; #os.execl(self.prog); os.execv(self.prog,[self.prog]); self.idle=0; else: self.idle=0; os.wait(); self.idle=0; time.sleep(1); def check_timeout(self): if self.state==True: if self.suspended==True: time.sleep(60); self.suspended=False; self.get_idle(); self.get_idle_workaround(); if int(self.idle/60)>=int(self.stime): self.suspended=True; pid=os.fork(); if pid==0: self.idle=0; os.execv(self.prog,[self.prog]); self.idle=0; else: self.idle=0; os.wait(); self.idle=0; return True; def quit(self): sys.exit(); if __name__ == "__main__": pm=PowerMan(); pm.run();