1.1 --- a/desktop/windows.py Sun Jun 07 16:19:31 2009 +0200
1.2 +++ b/desktop/windows.py Sun Jun 07 17:28:16 2009 +0200
1.3 @@ -48,15 +48,31 @@
1.4
1.5 # System functions.
1.6
1.7 -def _xwininfo(s):
1.8 - d = {}
1.9 - for line in s.split("\n"):
1.10 - fields = line.split(":")
1.11 - if len(fields) < 2:
1.12 - continue
1.13 - key, value = fields[0].strip(), ":".join(fields[1:]).strip()
1.14 - d[key] = value
1.15 - return d
1.16 +def _xwininfo(identifier, action):
1.17 + if identifier is None:
1.18 + args = "-root"
1.19 + else:
1.20 + args = "-id " + identifier
1.21 +
1.22 + s = _readfrom(_get_x11_vars() + "xwininfo %s -%s" % (args, action), shell=1)
1.23 +
1.24 + # Return a mapping of keys to values for the "stats" action.
1.25 +
1.26 + if action == "stats":
1.27 + d = {}
1.28 + for line in s.split("\n"):
1.29 + fields = line.split(":")
1.30 + if len(fields) < 2:
1.31 + continue
1.32 + key, value = fields[0].strip(), ":".join(fields[1:]).strip()
1.33 + d[key] = value
1.34 +
1.35 + return d
1.36 +
1.37 + # Otherwise, return the raw output.
1.38 +
1.39 + else:
1.40 + return s
1.41
1.42 def _get_int_properties(d, properties):
1.43 results = []
1.44 @@ -102,15 +118,6 @@
1.45
1.46 # Methods which deal with the underlying commands.
1.47
1.48 - def _get_identifier_args(self):
1.49 - if self.identifier is None:
1.50 - return "-root"
1.51 - else:
1.52 - return "-id " + self.identifier
1.53 -
1.54 - def _get_command_output(self, action):
1.55 - return _readfrom(_get_x11_vars() + "xwininfo %s -%s" % (self._get_identifier_args(), action), shell=1)
1.56 -
1.57 def _get_handle_and_name(self, text):
1.58 fields = text.strip().split(" ")
1.59 handle = fields[0]
1.60 @@ -128,7 +135,7 @@
1.61
1.62 def _get_this_handle_and_name(self, line):
1.63 fields = line.split(":")
1.64 - return self._get_handle_and_name(":".join(fields[2:]))
1.65 + return self._get_handle_and_name(":".join(fields[1:]))
1.66
1.67 def _get_descendant_handle_and_name(self, line):
1.68 match = self._name_pattern.search(line)
1.69 @@ -160,7 +167,7 @@
1.70 be returned regardless of whether they have any name information.
1.71 """
1.72
1.73 - s = self._get_command_output("children")
1.74 + s = _xwininfo(self.identifier, "children")
1.75 return self._descendants(s, all and self.find_all or self.find_named)
1.76
1.77 def descendants(self, all=0):
1.78 @@ -171,7 +178,7 @@
1.79 be returned regardless of whether they have any name information.
1.80 """
1.81
1.82 - s = self._get_command_output("tree")
1.83 + s = _xwininfo(self.identifier, "tree")
1.84 return self._descendants(s, all and self.find_all or self.find_named)
1.85
1.86 def find(self, callable):
1.87 @@ -181,37 +188,31 @@
1.88 value when invoked with a window name) for descendants of this window.
1.89 """
1.90
1.91 - s = self._get_command_output("tree")
1.92 + s = _xwininfo(self.identifier, "tree")
1.93 return self._descendants(s, callable)
1.94
1.95 def name(self):
1.96
1.97 "Return the name of the window."
1.98
1.99 - s = self._get_command_output("stats")
1.100 - for line in s.split("\n"):
1.101 - if line.startswith("xwininfo:"):
1.102 + d = _xwininfo(self.identifier, "stats")
1.103
1.104 - # Format is 'xwininfo: Window id: <handle> "<name>"
1.105 + # Format is 'xwininfo: Window id: <handle> "<name>"
1.106
1.107 - return self._get_this_handle_and_name(line)[1]
1.108 -
1.109 - return None
1.110 + return self._get_this_handle_and_name(d["xwininfo"])[1]
1.111
1.112 def size(self):
1.113
1.114 "Return a tuple containing the width and height of this window."
1.115
1.116 - s = self._get_command_output("stats")
1.117 - d = _xwininfo(s)
1.118 + d = _xwininfo(self.identifier, "stats")
1.119 return _get_int_properties(d, ["Width", "Height"])
1.120
1.121 def position(self):
1.122
1.123 "Return a tuple containing the upper left co-ordinates of this window."
1.124
1.125 - s = self._get_command_output("stats")
1.126 - d = _xwininfo(s)
1.127 + d = _xwininfo(self.identifier, "stats")
1.128 return _get_int_properties(d, ["Absolute upper-left X", "Absolute upper-left Y"])
1.129
1.130 def displayed(self):
1.131 @@ -221,16 +222,14 @@
1.132 visible on the current screen).
1.133 """
1.134
1.135 - s = self._get_command_output("stats")
1.136 - d = _xwininfo(s)
1.137 + d = _xwininfo(self.identifier, "stats")
1.138 return d["Map State"] != "IsUnviewable"
1.139
1.140 def visible(self):
1.141
1.142 "Return whether the window is displayed and visible."
1.143
1.144 - s = self._get_command_output("stats")
1.145 - d = _xwininfo(s)
1.146 + d = _xwininfo(self.identifier, "stats")
1.147 return d["Map State"] == "IsViewable"
1.148
1.149 def list(desktop=None):