Schurger.org

29 mai 2008

Setup usefull key bindings for metacity with python

Filed under: Code,Python — Jean Schurger @ 3:51

 As you will read the following script configure metacity to let you move your windows to the edges of your screen pressing the keys <Alt> + <Shift> + <the direction key of your choice>

Also it bind <Ctrl> + <Shift> + Up and <Ctrl> + <Shift> + Right to respectivly toggle vertical and horizontal window maximization.

#!/usr/bin/python

from gconf import Client

bindings = dict(
    move_to_side_e='<Alt><Shift>Right',
    move_to_side_w= '<Alt><Shift>Left',
    move_to_side_s= '<Alt><Shift>Down',
    move_to_side_n= '<Alt><Shift>Up',
    maximize_vertically= '<Ctrl><Shift>Up',
    maximize_horizontally= '<Ctrl><Shift>Right')

root = "/apps/metacity/window_keybindings"
c = Client()
for key in bindings.keys():
    c.set_value("%s/%s" % (root, key),
                bindings[key])

28 mai 2008

get X idle time with python

Filed under: 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()

Powered by WordPress