from Xlib import X, display, Xutil, Xatom import struct class Ratpoison: """The class allows programmatic access to the ratpoison window manager""" def __init__(self, display = display.Display()): self.dpy = display self.command = self.dpy.intern_atom ("RP_COMMAND"); self.command_request = self.dpy.intern_atom ("RP_COMMAND_REQUEST"); self.command_result = self.dpy.intern_atom ("RP_COMMAND_RESULT"); screen = self.dpy.screen() self.root = screen.root self.w = self.root.create_window(0, 0, 1, 1, 0, 0, event_mask = (X.PropertyChangeMask) ) def _recieve_command_result(self, w): status = w.get_full_property (self.command_result, Xatom.STRING) assert status != None return status.value def send_command(self, cmd): done = 0 self.w.change_property(self.command, Xatom.STRING, 8, chr(0) + cmd) self.root.change_property(self.command_request, Xatom.WINDOW, 8, struct.pack("l", self.w.id), mode=X.PropModeAppend) while not done: ev = self.dpy.next_event() if ev.atom == self.command_result and \ ev.state == X.PropertyNewValue: val = self._recieve_command_result(ev.window); done = 1; return val def echo(self, str): """Echo a string to the ratpoison bar""" self.send_command("echo %s" % str) def next(self): """Change to the next window""" self.send_command("next") def prev(self): """Change to the previous window""" self.send_command("prev") def windows(self): """Return a string listing the windows""" return self.send_command("windows") def select(self, win): """Select a specific window""" print "Selecting: ", win self.send_command("select %s" % win) def echo_windows(self): """Display a list of windows""" self.echo(self.windows()) def other(self): """Jump to the other window""" self.send_command("other") def focus(self): self.send_command("focus") if __name__ == "__main__": import sys import time rp = Ratpoison() rp.echo(sys.argv[1]) #print rp.send_command("windows")