def _xwininfo(identifier, action): if identifier is None: args = "-root" else: args = "-id " + identifier s = _readfrom(_get_x11_vars() + "xwininfo %s -%s" % (args, action), shell=1) # Return a mapping of keys to values for the "stats" action. if action == "stats": d = {} for line in s.split("\n"): fields = line.split(":") if len(fields) < 2: continue key, value = fields[0].strip(), ":".join(fields[1:]).strip() d[key] = value return d # Otherwise, return the raw output. else: return s
def position(self): "Return a tuple containing the upper left co-ordinates of this window." s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1) d = _xwininfo(s) return _get_int_properties(d, ["Absolute upper-left X", "Absolute upper-left Y"])
def size(self): "Return a tuple containing the width and height of this window." s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1) d = _xwininfo(s) return _get_int_properties(d, ["Width", "Height"])
def position(self): "Return a tuple containing the upper left co-ordinates of this window." s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1) d = _xwininfo(s) return _get_int_properties( d, ["Absolute upper-left X", "Absolute upper-left Y"])
def children(self): "Return a list of windows which are children of this window." s = _readfrom(_get_x11_vars() + "xwininfo -id %s -children" % self.identifier, shell=1) handles = [] adding = 0 for line in s.split("\n"): if not adding and line.endswith("children:"): adding = 1 elif adding and line: handles.append(line.strip().split()[0]) return [Window(handle) for handle in handles]
def _xwininfo(identifier, action): if identifier is None: args = "-root" else: args = "-id " + identifier s = _readfrom(_get_x11_vars() + "xwininfo %s -%s" % (args, action), shell=1) if action == "stats": d = {} for line in s.split("\n"): fields = line.split(":") if len(fields) < 2: continue key, value = fields[0].strip(), ":".join(fields[1:]).strip() d[key] = value return d else: return s
def name(self): "Return the name of the window." s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1) for line in s.split("\n"): if line.startswith("xwininfo:"): # Format is 'xwininfo: Window id: <handle> "<name>" fields = line.split(":") handle_and_name = fields[2].strip() fields2 = handle_and_name.split(" ") # Get the "<name>" part, stripping off the quotes. return " ".join(fields2[1:]).strip('"') return None
def list(desktop=None): """ Return a list of windows for the current desktop. If the optional 'desktop' parameter is specified then attempt to use that particular desktop environment's mechanisms to look for windows. """ # NOTE: The desktop parameter is currently ignored and X11 is tested for # NOTE: directly. if _is_x11(): s = _readfrom(_get_x11_vars() + "xlsclients -a -l", shell=1) prefix = "Window " prefix_end = len(prefix) handles = [] for line in s.split("\n"): if line.startswith(prefix): handles.append(line[prefix_end:-1]) # NOTE: Assume ":" at end. else: raise OSError, "Desktop '%s' not supported" % use_desktop(desktop) return [Window(handle) for handle in handles]