1.1 --- a/desktop/windows.py Thu Dec 13 21:41:26 2007 +0000
1.2 +++ b/desktop/windows.py Sat Dec 15 18:32:41 2007 +0000
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Simple desktop window enumeration for Python.
1.6
1.7 -Copyright (C) 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -26,16 +26,107 @@
1.13
1.14 To obtain a list of windows, use the desktop.windows.list function as follows:
1.15
1.16 -desktop.windows.list()
1.17 +windows = desktop.windows.list()
1.18 +
1.19 +Each window object can be inspected through a number of methods. For example:
1.20 +
1.21 +name = window.name()
1.22 +width, height = window.size()
1.23 +x, y = window.position()
1.24 +child_windows = window.children()
1.25 +
1.26 +See the desktop.windows.Window class for more information.
1.27 """
1.28
1.29 from desktop import _is_x11, _get_x11_vars, _readfrom, use_desktop
1.30
1.31 +def _xwininfo(s):
1.32 + d = {}
1.33 + for line in s.split("\n"):
1.34 + fields = line.split(":")
1.35 + if len(fields) < 2:
1.36 + continue
1.37 + key, values = fields[0].strip(), fields[1:]
1.38 + d[key] = values
1.39 + return d
1.40 +
1.41 +def _get_int_properties(d, properties):
1.42 + results = []
1.43 + for property in properties:
1.44 + results.append(int(d[property][0].strip()))
1.45 + return results
1.46 +
1.47 +# Window classes.
1.48 +# NOTE: X11 is the only supported desktop so far.
1.49 +
1.50 +class Window:
1.51 +
1.52 + "A window on the desktop."
1.53 +
1.54 + def __init__(self, identifier):
1.55 +
1.56 + "Initialise the window with the given 'identifier'."
1.57 +
1.58 + self.identifier = identifier
1.59 +
1.60 + def __repr__(self):
1.61 + return "Window(%r)" % self.identifier
1.62 +
1.63 + def children(self):
1.64 +
1.65 + "Return a list of windows which are children of this window."
1.66 +
1.67 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -children" % self.identifier, shell=1)
1.68 + handles = []
1.69 + adding = 0
1.70 + for line in s.split("\n"):
1.71 + if not adding and line.endswith("children:"):
1.72 + adding = 1
1.73 + elif adding and line:
1.74 + handles.append(line.strip().split()[0])
1.75 + return [Window(handle) for handle in handles]
1.76 +
1.77 + def name(self):
1.78 +
1.79 + "Return the name of the window."
1.80 +
1.81 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1)
1.82 + for line in s.split("\n"):
1.83 + if line.startswith("xwininfo:"):
1.84 +
1.85 + # Format is 'xwininfo: Window id: <handle> "<name>"
1.86 +
1.87 + fields = line.split(":")
1.88 + handle_and_name = fields[2].strip()
1.89 + fields2 = handle_and_name.split(" ")
1.90 +
1.91 + # Get the "<name>" part, stripping off the quotes.
1.92 +
1.93 + return " ".join(fields2[1:]).strip('"')
1.94 +
1.95 + return None
1.96 +
1.97 + def size(self):
1.98 +
1.99 + "Return a tuple containing the width and height of this window."
1.100 +
1.101 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1)
1.102 + d = _xwininfo(s)
1.103 + return _get_int_properties(d, ["Width", "Height"])
1.104 +
1.105 + def position(self):
1.106 +
1.107 + "Return a tuple containing the upper left co-ordinates of this window."
1.108 +
1.109 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1)
1.110 + d = _xwininfo(s)
1.111 + return _get_int_properties(d, ["Absolute upper-left X", "Absolute upper-left Y"])
1.112 +
1.113 def list(desktop=None):
1.114
1.115 """
1.116 - Return a list of window handles for the current desktop. If the optional
1.117 - 'desktop' parameter is specified then attempt to use that particular desktop
1.118 + Return a list of windows for the current desktop. If the optional 'desktop'
1.119 + parameter is specified then attempt to use that particular desktop
1.120 environment's mechanisms to look for windows.
1.121 """
1.122
1.123 @@ -54,6 +145,6 @@
1.124 else:
1.125 raise OSError, "Desktop '%s' not supported" % use_desktop(desktop)
1.126
1.127 - return handles
1.128 + return [Window(handle) for handle in handles]
1.129
1.130 # vim: tabstop=4 expandtab shiftwidth=4