forked from revolunet/sublimetext-markdown-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows.py
273 lines (189 loc) · 7.81 KB
/
windows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python
"""
Simple desktop window enumeration for Python.
Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
--------
Finding Open Windows on the Desktop
-----------------------------------
To obtain a list of windows, use the desktop.windows.list function as follows:
windows = desktop.windows.list()
To obtain the root window, typically the desktop background, use the
desktop.windows.root function as follows:
root = desktop.windows.root()
Each window object can be inspected through a number of methods. For example:
name = window.name()
width, height = window.size()
x, y = window.position()
child_windows = window.children()
See the desktop.windows.Window class for more information.
"""
from desktop import _is_x11, _get_x11_vars, _readfrom, use_desktop
import re
# System functions.
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 _get_int_properties(d, properties):
results = []
for property in properties:
results.append(int(d[property]))
return results
# Finder functions.
def find_all(name):
return 1
def find_named(name):
return name is not None
def find_by_name(name):
return lambda n, t=name: n == t
# Window classes.
# NOTE: X11 is the only supported desktop so far.
class Window:
"A window on the desktop."
_name_pattern = re.compile(r':\s+\(.*?\)\s+[-0-9x+]+\s+[-0-9+]+$')
_absent_names = "(has no name)", "(the root window) (has no name)"
def __init__(self, identifier):
"Initialise the window with the given 'identifier'."
self.identifier = identifier
# Finder methods (from above).
self.find_all = find_all
self.find_named = find_named
self.find_by_name = find_by_name
def __repr__(self):
return "Window(%r)" % self.identifier
# Methods which deal with the underlying commands.
def _get_handle_and_name(self, text):
fields = text.strip().split(" ")
handle = fields[0]
# Get the "<name>" part, stripping off the quotes.
name = " ".join(fields[1:])
if len(name) > 1 and name[0] == '"' and name[-1] == '"':
name = name[1:-1]
if name in self._absent_names:
return handle, None
else:
return handle, name
def _get_this_handle_and_name(self, line):
fields = line.split(":")
return self._get_handle_and_name(":".join(fields[1:]))
def _get_descendant_handle_and_name(self, line):
match = self._name_pattern.search(line)
if match:
return self._get_handle_and_name(line[:match.start()].strip())
else:
raise OSError, "Window information from %r did not contain window details." % line
def _descendants(self, s, fn):
handles = []
adding = 0
for line in s.split("\n"):
if line.endswith("child:") or line.endswith("children:"):
if not adding:
adding = 1
elif adding and line:
handle, name = self._get_descendant_handle_and_name(line)
if fn(name):
handles.append(handle)
return [Window(handle) for handle in handles]
# Public methods.
def children(self, all=0):
"""
Return a list of windows which are children of this window. If the
optional 'all' parameter is set to a true value, all such windows will
be returned regardless of whether they have any name information.
"""
s = _xwininfo(self.identifier, "children")
return self._descendants(s, all and self.find_all or self.find_named)
def descendants(self, all=0):
"""
Return a list of windows which are descendants of this window. If the
optional 'all' parameter is set to a true value, all such windows will
be returned regardless of whether they have any name information.
"""
s = _xwininfo(self.identifier, "tree")
return self._descendants(s, all and self.find_all or self.find_named)
def find(self, callable):
"""
Return windows using the given 'callable' (returning a true or a false
value when invoked with a window name) for descendants of this window.
"""
s = _xwininfo(self.identifier, "tree")
return self._descendants(s, callable)
def name(self):
"Return the name of the window."
d = _xwininfo(self.identifier, "stats")
# Format is 'xwininfo: Window id: <handle> "<name>"
return self._get_this_handle_and_name(d["xwininfo"])[1]
def size(self):
"Return a tuple containing the width and height of this window."
d = _xwininfo(self.identifier, "stats")
return _get_int_properties(d, ["Width", "Height"])
def position(self):
"Return a tuple containing the upper left co-ordinates of this window."
d = _xwininfo(self.identifier, "stats")
return _get_int_properties(d, ["Absolute upper-left X", "Absolute upper-left Y"])
def displayed(self):
"""
Return whether the window is displayed in some way (but not necessarily
visible on the current screen).
"""
d = _xwininfo(self.identifier, "stats")
return d["Map State"] != "IsUnviewable"
def visible(self):
"Return whether the window is displayed and visible."
d = _xwininfo(self.identifier, "stats")
return d["Map State"] == "IsViewable"
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.
"""
root_window = root(desktop)
window_list = [window for window in root_window.descendants() if window.displayed()]
window_list.insert(0, root_window)
return window_list
def root(desktop=None):
"""
Return the root window 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():
return Window(None)
else:
raise OSError, "Desktop '%s' not supported" % use_desktop(desktop)
def find(callable, desktop=None):
"""
Find and return windows using the given 'callable' 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.
"""
return root(desktop).find(callable)
# vim: tabstop=4 expandtab shiftwidth=4