@reticlehq/computer 0.0.0-stage → 3.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +237 -2
- package/bin/attach.mjs +333 -0
- package/dist/attach.d.ts +105 -0
- package/dist/attach.js +234 -0
- package/dist/channels.d.ts +42 -0
- package/dist/channels.js +46 -0
- package/dist/computer-realm.d.ts +54 -0
- package/dist/computer-realm.js +487 -0
- package/dist/doctor.d.ts +32 -0
- package/dist/doctor.js +42 -0
- package/dist/driver.d.ts +227 -0
- package/dist/driver.js +73 -0
- package/dist/effect.d.ts +60 -0
- package/dist/effect.js +158 -0
- package/dist/files.d.ts +16 -0
- package/dist/files.js +40 -0
- package/dist/hud/port.d.ts +44 -0
- package/dist/hud/port.js +6 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +22 -0
- package/dist/lines.d.ts +34 -0
- package/dist/lines.js +80 -0
- package/dist/mac/from-ax.d.ts +27 -0
- package/dist/mac/from-ax.js +275 -0
- package/dist/mac/mac-driver.d.ts +42 -0
- package/dist/mac/mac-driver.js +355 -0
- package/dist/mac/mac-hud.d.ts +10 -0
- package/dist/mac/mac-hud.js +142 -0
- package/dist/peer/act-effect.d.ts +9 -0
- package/dist/peer/act-effect.js +13 -0
- package/dist/peer/commands.d.ts +87 -0
- package/dist/peer/commands.js +811 -0
- package/dist/peer/form.d.ts +62 -0
- package/dist/peer/form.js +103 -0
- package/dist/peer/link-switch.d.ts +12 -0
- package/dist/peer/link-switch.js +38 -0
- package/dist/peer/peer.d.ts +43 -0
- package/dist/peer/peer.js +216 -0
- package/dist/peer/pointer.d.ts +97 -0
- package/dist/peer/pointer.js +289 -0
- package/dist/peer/scroll.d.ts +12 -0
- package/dist/peer/scroll.js +157 -0
- package/dist/peer/seen.d.ts +18 -0
- package/dist/peer/seen.js +61 -0
- package/dist/peer/select.d.ts +27 -0
- package/dist/peer/select.js +78 -0
- package/dist/peer/window.d.ts +25 -0
- package/dist/peer/window.js +111 -0
- package/dist/ui-node.d.ts +93 -0
- package/dist/ui-node.js +192 -0
- package/dist/win/helper-launch.d.ts +16 -0
- package/dist/win/helper-launch.js +57 -0
- package/native/linux/Dockerfile +22 -0
- package/native/linux/reticle-atspi.py +933 -0
- package/native/linux/sandbox.mjs +127 -0
- package/native/mac/build/reticle-ax +0 -0
- package/native/mac/build/reticle-hud +0 -0
- package/native/mac/hud.swift +749 -0
- package/native/mac/reticle-ax.swift +1786 -0
- package/native/win/reticle-uia.ps1 +620 -0
- package/package.json +65 -3
|
@@ -0,0 +1,933 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# reticle-atspi: the Linux helper of the computer realm. It reads an application through AT-SPI,
|
|
3
|
+
# the accessibility bus of the Linux desktop, and acts on it. It answers in the same shape as the
|
|
4
|
+
# macOS helper (reticle-ax) and the Windows helper (reticle-uia.ps1): the same commands, the same
|
|
5
|
+
# JSON, and the roles named as macOS names them, so the realm reads all three with one reader.
|
|
6
|
+
#
|
|
7
|
+
# python3 reticle-atspi.py serve
|
|
8
|
+
#
|
|
9
|
+
# Each line on standard input is one command as a JSON list of words; each answer is one line.
|
|
10
|
+
# apps
|
|
11
|
+
# tree <pid>
|
|
12
|
+
# reach <pid> <path> <role> <name>
|
|
13
|
+
# act <pid> <path> <role> <name> <kind> <value> <budgetMs> <delivery[:quietMs]>
|
|
14
|
+
# capture <pid> <file> [screen]
|
|
15
|
+
#
|
|
16
|
+
# It needs PyGObject with the AT-SPI and GDK typelibs (python3-gi, gir1.2-atspi-2.0,
|
|
17
|
+
# gir1.2-gtk-3.0 on Debian and Ubuntu), and an X session with xdotool: the real pointer and keys
|
|
18
|
+
# go through XTest, which a Wayland session does not take from a program. The pointer events of
|
|
19
|
+
# AT-SPI itself did not reach the application under Xvfb; xdotool's did.
|
|
20
|
+
import json
|
|
21
|
+
import os
|
|
22
|
+
import shutil
|
|
23
|
+
import subprocess
|
|
24
|
+
import sys
|
|
25
|
+
import time
|
|
26
|
+
|
|
27
|
+
import gi
|
|
28
|
+
|
|
29
|
+
gi.require_version('Atspi', '2.0')
|
|
30
|
+
gi.require_version('Gdk', '3.0')
|
|
31
|
+
from gi.repository import Atspi, Gdk # noqa: E402
|
|
32
|
+
|
|
33
|
+
READ_LIMIT = 20000
|
|
34
|
+
SHORT_LIST = 200
|
|
35
|
+
# A container with more children than this is not read child by child: a sheet of Calc has a child
|
|
36
|
+
# for each cell, and to read them took minutes.
|
|
37
|
+
LONG_TABLE = 5000
|
|
38
|
+
QUIET_MS = 150
|
|
39
|
+
PICTURE_WIDTH = 240
|
|
40
|
+
STEP = 24
|
|
41
|
+
# The two menu bars of LibreOffice put the same menu at most 4 pixels apart.
|
|
42
|
+
TWIN_PX = 8
|
|
43
|
+
|
|
44
|
+
# AT-SPI role names, named as macOS names them. A role that is not here is read as AXGroup.
|
|
45
|
+
ROLE_OF = {
|
|
46
|
+
'frame': 'AXWindow', 'window': 'AXWindow', 'dialog': 'AXWindow', 'alert': 'AXWindow',
|
|
47
|
+
'file chooser': 'AXWindow', 'push button': 'AXButton', 'toggle button': 'AXButton',
|
|
48
|
+
'button': 'AXButton', 'entry': 'AXTextField', 'password text': 'AXTextField',
|
|
49
|
+
'text': 'AXTextArea', 'document text': 'AXTextArea', 'paragraph': 'AXStaticText',
|
|
50
|
+
'label': 'AXStaticText', 'static': 'AXStaticText', 'caption': 'AXStaticText',
|
|
51
|
+
'heading': 'AXHeading', 'check box': 'AXCheckBox', 'check menu item': 'AXMenuItem',
|
|
52
|
+
'radio button': 'AXRadioButton', 'radio menu item': 'AXMenuItem', 'combo box': 'AXComboBox',
|
|
53
|
+
'list': 'AXList', 'list box': 'AXList', 'list item': 'AXRow', 'table': 'AXTable',
|
|
54
|
+
'tree table': 'AXOutline', 'tree': 'AXOutline', 'table row': 'AXRow', 'tree item': 'AXRow',
|
|
55
|
+
'table cell': 'AXCell', 'menu': 'AXMenu', 'menu bar': 'AXMenuBar', 'menu item': 'AXMenuItem',
|
|
56
|
+
'page tab list': 'AXTabGroup', 'page tab': 'AXRadioButton', 'link': 'AXLink',
|
|
57
|
+
'image': 'AXImage', 'icon': 'AXImage', 'canvas': 'AXImage', 'drawing area': 'AXImage',
|
|
58
|
+
'slider': 'AXSlider', 'scroll bar': 'AXScrollBar', 'progress bar': 'AXProgressIndicator',
|
|
59
|
+
'tool bar': 'AXToolbar', 'scroll pane': 'AXScrollArea', 'separator': 'AXSplitter',
|
|
60
|
+
'split pane': 'AXSplitGroup', 'spin button': 'AXIncrementor', 'document frame': 'AXWebArea',
|
|
61
|
+
'document web': 'AXWebArea',
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
# Key names of the realm, as X key symbols.
|
|
65
|
+
KEY_OF = {
|
|
66
|
+
'return': 'Return', 'enter': 'Return', 'escape': 'Escape', 'tab': 'Tab',
|
|
67
|
+
'backspace': 'BackSpace', 'delete': 'Delete', 'arrowup': 'Up', 'arrowdown': 'Down',
|
|
68
|
+
'arrowleft': 'Left', 'arrowright': 'Right', 'home': 'Home', 'end': 'End',
|
|
69
|
+
'pageup': 'Page_Up', 'pagedown': 'Page_Down', 'space': 'space',
|
|
70
|
+
}
|
|
71
|
+
# The modifiers a key or a click holds, as xdotool names them. Meta is Control, as on Windows.
|
|
72
|
+
MOD_KEY = {'shift': 'shift', 'control': 'ctrl', 'ctrl': 'ctrl', 'meta': 'ctrl', 'alt': 'alt', 'option': 'alt'}
|
|
73
|
+
POINTER = ('click', 'dblclick', 'rightclick', 'hover', 'drag', 'wheel')
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def answer(value):
|
|
77
|
+
sys.stdout.write(json.dumps(value) + '\n')
|
|
78
|
+
sys.stdout.flush()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def safe(call, default=None):
|
|
82
|
+
try:
|
|
83
|
+
return call()
|
|
84
|
+
except Exception: # noqa: BLE001 - an element can go away while it is read
|
|
85
|
+
return default
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def app_of(pid):
|
|
89
|
+
desktop = Atspi.get_desktop(0)
|
|
90
|
+
for k in range(desktop.get_child_count()):
|
|
91
|
+
app = safe(lambda: desktop.get_child_at_index(k))
|
|
92
|
+
if app is not None and safe(app.get_process_id) == pid:
|
|
93
|
+
return app
|
|
94
|
+
return None
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def states(el):
|
|
98
|
+
return safe(el.get_state_set)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def has(el, state, held=None):
|
|
102
|
+
# `held` is a state set already read: each read is a call on the bus.
|
|
103
|
+
s = held if held is not None else states(el)
|
|
104
|
+
return s is not None and s.contains(state)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def enabled_of(el, held=None):
|
|
108
|
+
if has(el, Atspi.StateType.ENABLED, held) or has(el, Atspi.StateType.SENSITIVE, held):
|
|
109
|
+
return True
|
|
110
|
+
# An item that the application draws itself (a file in the icon view of Files) gives no
|
|
111
|
+
# enabled state: it takes the state of what holds it.
|
|
112
|
+
if safe(el.get_role_name) == 'canvas' and has(el, Atspi.StateType.FOCUSABLE, held):
|
|
113
|
+
up = safe(el.get_parent)
|
|
114
|
+
return up is not None and (has(up, Atspi.StateType.ENABLED) or has(up, Atspi.StateType.SENSITIVE))
|
|
115
|
+
return False
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def open_gtk2_menu(menu, held=None):
|
|
119
|
+
# GTK2 (GIMP) never says that the items of an open menu are showing, only visible. The open
|
|
120
|
+
# menu is the one with the focus.
|
|
121
|
+
return safe(menu.get_role_name) == 'menu' and has(menu, Atspi.StateType.FOCUSED, held)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def on_screen(el, depth=0):
|
|
125
|
+
if has(el, Atspi.StateType.SHOWING):
|
|
126
|
+
return True
|
|
127
|
+
if depth > 20 or not has(el, Atspi.StateType.VISIBLE):
|
|
128
|
+
return False
|
|
129
|
+
up = safe(el.get_parent)
|
|
130
|
+
return up is not None and open_gtk2_menu(up) and on_screen(up, depth + 1)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def role_of(el, held=None):
|
|
134
|
+
name = safe(el.get_role_name, '') or ''
|
|
135
|
+
role = ROLE_OF.get(name, 'AXGroup')
|
|
136
|
+
# GTK names a one-line field and a text area both "text".
|
|
137
|
+
if role == 'AXTextArea' and has(el, Atspi.StateType.SINGLE_LINE, held):
|
|
138
|
+
return 'AXTextField'
|
|
139
|
+
return role
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def frame_of(el):
|
|
143
|
+
component = safe(el.get_component_iface)
|
|
144
|
+
box = safe(lambda: Atspi.Component.get_extents(component, Atspi.CoordType.SCREEN)) if component is not None else None
|
|
145
|
+
if box is None or box.width <= 0 or box.height <= 0:
|
|
146
|
+
return None
|
|
147
|
+
return [box.x, box.y, box.width, box.height]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def windows_of(app):
|
|
151
|
+
shown, away = [], 0
|
|
152
|
+
for k in range(safe(app.get_child_count, 0)):
|
|
153
|
+
w = safe(lambda: app.get_child_at_index(k))
|
|
154
|
+
if w is None:
|
|
155
|
+
continue
|
|
156
|
+
if has(w, Atspi.StateType.ICONIFIED):
|
|
157
|
+
away += 1
|
|
158
|
+
continue
|
|
159
|
+
if has(w, Atspi.StateType.SHOWING) or has(w, Atspi.StateType.VISIBLE):
|
|
160
|
+
shown.append(w)
|
|
161
|
+
return shown, away
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def children_of(el):
|
|
165
|
+
return [c for c in (safe(lambda k=k: el.get_child_at_index(k)) for k in range(safe(el.get_child_count, 0))) if c is not None]
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def child_at(el, k):
|
|
169
|
+
return safe(lambda: el.get_child_at_index(k))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def on_screen_cells(el):
|
|
173
|
+
"""The places of the cells of a long table that are on the screen, or None if it cannot tell.
|
|
174
|
+
|
|
175
|
+
The cells at two corners of the table's box give the first and the last row and column.
|
|
176
|
+
"""
|
|
177
|
+
table, component = safe(el.get_table_iface), safe(el.get_component_iface)
|
|
178
|
+
box = frame_of(el)
|
|
179
|
+
if table is None or component is None or box is None:
|
|
180
|
+
return None
|
|
181
|
+
x, y, width, height = box
|
|
182
|
+
|
|
183
|
+
def cell_at(px, py):
|
|
184
|
+
cell = safe(lambda: Atspi.Component.get_accessible_at_point(component, px, py, Atspi.CoordType.SCREEN))
|
|
185
|
+
return safe(cell.get_index_in_parent, -1) if cell is not None else -1
|
|
186
|
+
|
|
187
|
+
# A column header has no cell under it: step down from the top, and up from the bottom.
|
|
188
|
+
first = next((k for k in (cell_at(x + 2, y + 2 + STEP * j) for j in range(8)) if k >= 0), -1)
|
|
189
|
+
last = next((k for k in (cell_at(x + width - 3, y + height - 3 - STEP * j) for j in range(8)) if k >= 0), -1)
|
|
190
|
+
at = [first, last]
|
|
191
|
+
if min(at) < 0:
|
|
192
|
+
return None
|
|
193
|
+
# A point on a column header is in no row: the rows start at the first one.
|
|
194
|
+
rows = [max(0, safe(lambda i=i: Atspi.Table.get_row_at_index(table, i), 0)) for i in at]
|
|
195
|
+
columns = [max(0, safe(lambda i=i: Atspi.Table.get_column_at_index(table, i), 0)) for i in at]
|
|
196
|
+
places = []
|
|
197
|
+
for r in range(rows[0], rows[1] + 1):
|
|
198
|
+
for c in range(columns[0], columns[1] + 1):
|
|
199
|
+
cell = safe(lambda r=r, c=c: Atspi.Table.get_accessible_at(table, r, c))
|
|
200
|
+
place = safe(cell.get_index_in_parent, -1) if cell is not None else -1
|
|
201
|
+
if place >= 0:
|
|
202
|
+
places.append(place)
|
|
203
|
+
if len(places) >= LONG_TABLE:
|
|
204
|
+
return places
|
|
205
|
+
return places
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def value_of(el, role, held=None):
|
|
209
|
+
if has(el, Atspi.StateType.CHECKABLE, held) or role in ('AXCheckBox', 'AXRadioButton'):
|
|
210
|
+
return '1' if has(el, Atspi.StateType.CHECKED, held) else '0'
|
|
211
|
+
text = safe(el.get_text_iface)
|
|
212
|
+
# A static text gives its words as its value, as on macOS: a paragraph of LibreOffice has no name.
|
|
213
|
+
if text is not None and role in ('AXTextField', 'AXTextArea', 'AXComboBox', 'AXStaticText'):
|
|
214
|
+
count = safe(lambda: Atspi.Text.get_character_count(text), 0)
|
|
215
|
+
return safe(lambda: Atspi.Text.get_text(text, 0, min(4000, count)), '')
|
|
216
|
+
value = safe(el.get_value_iface)
|
|
217
|
+
if value is not None:
|
|
218
|
+
return str(safe(lambda: Atspi.Value.get_current_value(value), ''))
|
|
219
|
+
return None
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
class Reader:
|
|
223
|
+
def __init__(self):
|
|
224
|
+
self.budget = READ_LIMIT
|
|
225
|
+
self.unread = 0
|
|
226
|
+
|
|
227
|
+
def node(self, el, depth):
|
|
228
|
+
held = states(el)
|
|
229
|
+
role = role_of(el, held)
|
|
230
|
+
node = {'role': role}
|
|
231
|
+
name = safe(el.get_name, '')
|
|
232
|
+
if name:
|
|
233
|
+
node['title'] = name
|
|
234
|
+
help_text = safe(el.get_description, '')
|
|
235
|
+
if help_text:
|
|
236
|
+
node['help'] = help_text
|
|
237
|
+
ident = safe(el.get_accessible_id, '')
|
|
238
|
+
if ident:
|
|
239
|
+
node['id'] = ident
|
|
240
|
+
node['enabled'] = enabled_of(el, held)
|
|
241
|
+
node['focused'] = has(el, Atspi.StateType.FOCUSED, held)
|
|
242
|
+
frame = frame_of(el)
|
|
243
|
+
if frame is not None:
|
|
244
|
+
node['frame'] = frame
|
|
245
|
+
value = value_of(el, role, held)
|
|
246
|
+
if value is not None:
|
|
247
|
+
node['value'] = value
|
|
248
|
+
if has(el, Atspi.StateType.SELECTED, held):
|
|
249
|
+
node['selected'] = True
|
|
250
|
+
count = safe(el.get_child_count, 0) if depth < 80 else 0
|
|
251
|
+
if count > LONG_TABLE:
|
|
252
|
+
# ponytail: no table interface or no cell at a corner, so the first children stand for the
|
|
253
|
+
# rest. GTK says every cell is showing, so their states tell nothing.
|
|
254
|
+
places = on_screen_cells(el)
|
|
255
|
+
if places is None:
|
|
256
|
+
places = list(range(SHORT_LIST))
|
|
257
|
+
node['notShown'] = count - len(places)
|
|
258
|
+
kids = [child_at(el, k) for k in places]
|
|
259
|
+
return self.fill(node, kids, places, depth)
|
|
260
|
+
kids = children_of(el) if depth < 80 else []
|
|
261
|
+
places = None
|
|
262
|
+
# A long list gives the items on the screen, with their places, and says how many it left out.
|
|
263
|
+
# A menu gives only the items on the screen: a closed menu gives none, and says how many.
|
|
264
|
+
# LibreOffice has hundreds of items in closed menus, and to read them took seconds.
|
|
265
|
+
listed = len(kids) > SHORT_LIST and role in ('AXList', 'AXTable', 'AXOutline')
|
|
266
|
+
if listed or (role == 'AXMenu' and kids):
|
|
267
|
+
gtk2 = open_gtk2_menu(el, held)
|
|
268
|
+
places = [k for k, c in enumerate(kids) if has(c, Atspi.StateType.SHOWING) or
|
|
269
|
+
(gtk2 and has(c, Atspi.StateType.VISIBLE))]
|
|
270
|
+
if listed or len(places) < len(kids):
|
|
271
|
+
node['notShown'] = len(kids) - len(places)
|
|
272
|
+
kids = [kids[k] for k in places]
|
|
273
|
+
else:
|
|
274
|
+
places = None
|
|
275
|
+
return self.fill(node, kids, places, depth)
|
|
276
|
+
|
|
277
|
+
def fill(self, node, kids, places, depth):
|
|
278
|
+
if self.budget <= 0 and kids:
|
|
279
|
+
node['notShown'] = node.get('notShown', 0) + len(kids)
|
|
280
|
+
node['children'] = []
|
|
281
|
+
return node
|
|
282
|
+
self.budget -= len(kids)
|
|
283
|
+
children = []
|
|
284
|
+
for k, kid in enumerate(kids):
|
|
285
|
+
try:
|
|
286
|
+
one = self.node(kid, depth + 1)
|
|
287
|
+
except Exception: # noqa: BLE001
|
|
288
|
+
self.unread += 1
|
|
289
|
+
one = {'role': '', 'children': []}
|
|
290
|
+
if places is not None:
|
|
291
|
+
one['i'] = places[k]
|
|
292
|
+
children.append(one)
|
|
293
|
+
node['children'] = children
|
|
294
|
+
return node
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def read_app(pid):
|
|
298
|
+
app = app_of(pid)
|
|
299
|
+
if app is None:
|
|
300
|
+
return None
|
|
301
|
+
reader = Reader()
|
|
302
|
+
shown, away = windows_of(app)
|
|
303
|
+
return {
|
|
304
|
+
'tree': {'role': 'AXApplication', 'title': safe(app.get_name, ''),
|
|
305
|
+
'children': [reader.node(w, 1) for w in shown]},
|
|
306
|
+
'unread': reader.unread,
|
|
307
|
+
'windowsNotOnScreen': away,
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def find(pid, path):
|
|
312
|
+
places = [int(p) for p in path.split('.') if p != '']
|
|
313
|
+
if not places:
|
|
314
|
+
return {'app': True}
|
|
315
|
+
app = app_of(pid)
|
|
316
|
+
if app is None:
|
|
317
|
+
return None
|
|
318
|
+
shown, _ = windows_of(app)
|
|
319
|
+
if places[0] >= len(shown):
|
|
320
|
+
return None
|
|
321
|
+
el = shown[places[0]]
|
|
322
|
+
for place in places[1:]:
|
|
323
|
+
# One child by its place: a sheet of Calc has a child for each cell.
|
|
324
|
+
if place >= safe(el.get_child_count, 0):
|
|
325
|
+
return None
|
|
326
|
+
el = child_at(el, place)
|
|
327
|
+
if el is None:
|
|
328
|
+
return None
|
|
329
|
+
return {'el': el}
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def same(a, b):
|
|
333
|
+
# ponytail: AT-SPI gives no identity to compare; the role, the name and the box stand for it.
|
|
334
|
+
return role_of(a) == role_of(b) and safe(a.get_name) == safe(b.get_name) and frame_of(a) == frame_of(b)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def point_child(el, x, y):
|
|
338
|
+
component = safe(el.get_component_iface)
|
|
339
|
+
if component is None:
|
|
340
|
+
return None
|
|
341
|
+
return safe(lambda: Atspi.Component.get_accessible_at_point(component, x, y, Atspi.CoordType.SCREEN))
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def holds(outer, el):
|
|
345
|
+
"""True if `el` is `outer` or inside it, by its parents."""
|
|
346
|
+
at = el
|
|
347
|
+
for _ in range(80):
|
|
348
|
+
if at is None:
|
|
349
|
+
return False
|
|
350
|
+
if same(at, outer):
|
|
351
|
+
return True
|
|
352
|
+
at = safe(at.get_parent)
|
|
353
|
+
return False
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def inside(point, box):
|
|
357
|
+
return box is not None and box[0] <= point[0] < box[0] + box[2] and box[1] <= point[1] < box[1] + box[3]
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def window_of(el):
|
|
361
|
+
at = el
|
|
362
|
+
for _ in range(80):
|
|
363
|
+
up = safe(at.get_parent)
|
|
364
|
+
if up is None or safe(up.get_role_name) == 'application':
|
|
365
|
+
return at
|
|
366
|
+
at = up
|
|
367
|
+
return None
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def twin(at, el, point):
|
|
371
|
+
# The same control, given twice: LibreOffice gives its menu bar in two trees, with boxes some
|
|
372
|
+
# pixels apart. A twin has the same role and name, a box within TWIN_PX on each edge, and the
|
|
373
|
+
# same window: an "OK" of a dialog over another dialog is in a different window.
|
|
374
|
+
a, b = frame_of(at), frame_of(el)
|
|
375
|
+
if a is None or b is None or not inside(point, a):
|
|
376
|
+
return False
|
|
377
|
+
if any(abs(p - q) > TWIN_PX for p, q in zip(a, b)):
|
|
378
|
+
return False
|
|
379
|
+
if role_of(at) != role_of(el) or (safe(at.get_name, '') or '') != (safe(el.get_name, '') or ''):
|
|
380
|
+
return False
|
|
381
|
+
wa, wb = window_of(at), window_of(el)
|
|
382
|
+
return wa is not None and wb is not None and same(wa, wb)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def covered_by(el, point):
|
|
386
|
+
# A child after the element is drawn over it: a button in a GTK overlay, a shape on a slide.
|
|
387
|
+
up = safe(el.get_parent)
|
|
388
|
+
k = safe(el.get_index_in_parent, -1)
|
|
389
|
+
if up is None or k < 0:
|
|
390
|
+
return None
|
|
391
|
+
for j in range(k + 1, min(safe(up.get_child_count, 0), k + 1 + SHORT_LIST)):
|
|
392
|
+
other = child_at(up, j)
|
|
393
|
+
if other is not None and has(other, Atspi.StateType.SHOWING) and inside(point, frame_of(other)):
|
|
394
|
+
return other
|
|
395
|
+
return None
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def hit_at(el, frame, shown, x, y):
|
|
399
|
+
"""True if a click at (x, y) goes to `el`, else the element that the hit test found there."""
|
|
400
|
+
# The toolkit can stop at a container: then only the order of the children tells what is on top.
|
|
401
|
+
over = covered_by(el, (x, y))
|
|
402
|
+
found = None
|
|
403
|
+
for w in shown:
|
|
404
|
+
at = point_child(w, x, y)
|
|
405
|
+
for _ in range(40):
|
|
406
|
+
if at is None:
|
|
407
|
+
break
|
|
408
|
+
if same(at, el) or twin(at, el, (x, y)):
|
|
409
|
+
return True
|
|
410
|
+
deeper = point_child(at, x, y)
|
|
411
|
+
if deeper is None or same(deeper, at):
|
|
412
|
+
break
|
|
413
|
+
at = deeper
|
|
414
|
+
# The element holds the point it was hit at: what was hit is inside it.
|
|
415
|
+
hit = frame_of(at) if at is not None else None
|
|
416
|
+
if over is None and hit is not None and frame[0] <= hit[0] and frame[1] <= hit[1] and \
|
|
417
|
+
hit[0] + hit[2] <= frame[0] + frame[2] and hit[1] + hit[3] <= frame[1] + frame[3]:
|
|
418
|
+
return True
|
|
419
|
+
# The toolkit stopped answering at a container that holds the element (a dialog of
|
|
420
|
+
# LibreOffice, the sidebar of Files): no other element of the application is at the point.
|
|
421
|
+
if over is None and at is not None and holds(at, el):
|
|
422
|
+
return True
|
|
423
|
+
found = found or over or at
|
|
424
|
+
return found
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def popups_of(el):
|
|
428
|
+
found, at = [], el
|
|
429
|
+
for _ in range(80):
|
|
430
|
+
at = safe(at.get_parent)
|
|
431
|
+
if at is None or safe(at.get_role_name) == 'application':
|
|
432
|
+
return found
|
|
433
|
+
# Qt calls an open menu "popup menu", and Gecko calls it "menu".
|
|
434
|
+
if safe(at.get_role_name) in ('popup menu', 'menu') and on_screen(at):
|
|
435
|
+
found.append(at)
|
|
436
|
+
return found
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def spots(frame):
|
|
440
|
+
# The middle first, then eight points around it, inside the edges: a shape on a slide can
|
|
441
|
+
# cover the middle of another and leave the rest of it open to the pointer.
|
|
442
|
+
x, y, w, h = frame
|
|
443
|
+
yield x + w // 2, y + h // 2
|
|
444
|
+
for fy in (0.25, 0.5, 0.75):
|
|
445
|
+
for fx in (0.25, 0.5, 0.75):
|
|
446
|
+
if (fx, fy) != (0.5, 0.5):
|
|
447
|
+
yield int(x + w * fx), int(y + h * fy)
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def reach(el, pid):
|
|
451
|
+
if not on_screen(el):
|
|
452
|
+
return {'reach': 'unknown', 'detail': 'the element is not showing: it is on a hidden page, in a closed menu or in a hidden bar'}
|
|
453
|
+
frame = frame_of(el)
|
|
454
|
+
if frame is None:
|
|
455
|
+
return {'reach': 'unknown', 'detail': 'the element has no place on the screen'}
|
|
456
|
+
app = app_of(pid)
|
|
457
|
+
shown, _ = windows_of(app) if app is not None else ([], 0)
|
|
458
|
+
# An open menu of Qt or Gecko is not a window of the application: it is under its menu item in
|
|
459
|
+
# the tree. It is over its window, so the hit test starts in it.
|
|
460
|
+
shown = popups_of(el) + shown
|
|
461
|
+
first = None
|
|
462
|
+
for x, y in spots(frame):
|
|
463
|
+
got = hit_at(el, frame, shown, x, y)
|
|
464
|
+
if got is True:
|
|
465
|
+
return {'reach': 'yes', 'at': [x, y]}
|
|
466
|
+
first = first or got
|
|
467
|
+
if first is not None:
|
|
468
|
+
name = safe(first.get_name, '') or ''
|
|
469
|
+
return {'reach': 'unknown', 'detail': f'the hit test found {safe(first.get_role_name, "")} "{name}" over the element, at its middle and at eight other points'}
|
|
470
|
+
return {'reach': 'unknown', 'detail': 'the hit test did not find the element'}
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def wait_quiet(pid, budget_ms, quiet_ms):
|
|
474
|
+
started = time.monotonic()
|
|
475
|
+
last = json.dumps(read_app(pid))
|
|
476
|
+
quiet_since = time.monotonic()
|
|
477
|
+
while (time.monotonic() - started) * 1000 < budget_ms:
|
|
478
|
+
time.sleep(0.05)
|
|
479
|
+
now = json.dumps(read_app(pid))
|
|
480
|
+
if now != last:
|
|
481
|
+
last, quiet_since = now, time.monotonic()
|
|
482
|
+
continue
|
|
483
|
+
if (time.monotonic() - quiet_since) * 1000 >= quiet_ms:
|
|
484
|
+
return True
|
|
485
|
+
return False
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
def xdo(*words):
|
|
489
|
+
subprocess.run(['xdotool', *[str(w) for w in words]], check=True, timeout=10)
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
def move(x, y):
|
|
493
|
+
xdo('mousemove', int(x), int(y))
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def click(x, y, times, button, hold_ms):
|
|
497
|
+
move(x, y)
|
|
498
|
+
for _ in range(times):
|
|
499
|
+
if hold_ms > 0:
|
|
500
|
+
xdo('mousedown', button)
|
|
501
|
+
time.sleep(hold_ms / 1000)
|
|
502
|
+
xdo('mouseup', button)
|
|
503
|
+
else:
|
|
504
|
+
xdo('click', button)
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def no_real_input():
|
|
508
|
+
# Why the real pointer and keys cannot be used here, or None when they can. A Wayland session
|
|
509
|
+
# takes no input from a program; xdotool reaches only X.
|
|
510
|
+
if os.environ.get('XDG_SESSION_TYPE') == 'wayland' or (
|
|
511
|
+
os.environ.get('WAYLAND_DISPLAY') and not os.environ.get('DISPLAY')):
|
|
512
|
+
return 'this Linux session is Wayland, which takes no pointer and no keys from a program'
|
|
513
|
+
if not os.environ.get('DISPLAY'):
|
|
514
|
+
return 'there is no X display (DISPLAY is not set)'
|
|
515
|
+
if shutil.which('xdotool') is None:
|
|
516
|
+
return 'xdotool is not installed (sudo apt-get install xdotool)'
|
|
517
|
+
return None
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
def idle_ms():
|
|
521
|
+
# ponytail: xprintidle reads the idle time of the X server. Without it there is no check.
|
|
522
|
+
if shutil.which('xprintidle') is None:
|
|
523
|
+
return 10 ** 9
|
|
524
|
+
try:
|
|
525
|
+
return int(subprocess.run(['xprintidle'], capture_output=True, text=True, timeout=5).stdout.strip())
|
|
526
|
+
except (ValueError, subprocess.SubprocessError):
|
|
527
|
+
return 10 ** 9
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def arrange(pid, value):
|
|
531
|
+
# The window of the application, moved and sized as X moves it: through the window manager when
|
|
532
|
+
# there is one, and directly when there is none (a screen in memory).
|
|
533
|
+
found = subprocess.run(['xdotool', 'search', '--onlyvisible', '--pid', str(pid)],
|
|
534
|
+
capture_output=True, text=True, timeout=10).stdout.split()
|
|
535
|
+
if not found:
|
|
536
|
+
return None
|
|
537
|
+
wid = found[0]
|
|
538
|
+
width, height = (int(n) for n in subprocess.run(['xdotool', 'getdisplaygeometry'], capture_output=True,
|
|
539
|
+
text=True, timeout=10).stdout.split())
|
|
540
|
+
frames = {'left': (0, 0, width // 2, height), 'right': (width // 2, 0, width // 2, height),
|
|
541
|
+
'zoom': (0, 0, width, height), 'fullscreen': (0, 0, width, height)}
|
|
542
|
+
wanted = None
|
|
543
|
+
if value == 'minimize':
|
|
544
|
+
xdo('windowminimize', wid)
|
|
545
|
+
elif value == 'restore':
|
|
546
|
+
xdo('windowmap', wid)
|
|
547
|
+
elif value in frames or value.startswith('frame:'):
|
|
548
|
+
wanted = list(frames.get(value) or [int(float(n)) for n in value[len('frame:'):].split(',')])
|
|
549
|
+
xdo('windowmove', wid, wanted[0], wanted[1])
|
|
550
|
+
xdo('windowsize', wid, wanted[2], wanted[3])
|
|
551
|
+
else:
|
|
552
|
+
return None
|
|
553
|
+
return {'pid': pid, 'wid': wid, 'wanted': wanted}
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
def window_state(arranged):
|
|
557
|
+
# Read again after the change: the answer of X to a move is not the proof that it moved.
|
|
558
|
+
shell = subprocess.run(['xdotool', 'getwindowgeometry', '--shell', arranged['wid']],
|
|
559
|
+
capture_output=True, text=True, timeout=10).stdout
|
|
560
|
+
geometry = dict(line.split('=', 1) for line in shell.split() if '=' in line)
|
|
561
|
+
shown = subprocess.run(['xdotool', 'search', '--onlyvisible', '--pid', str(arranged['pid'])],
|
|
562
|
+
capture_output=True, text=True, timeout=10).stdout.split()
|
|
563
|
+
state = {'minimized': arranged['wid'] not in shown, 'fullscreen': False}
|
|
564
|
+
if all(k in geometry for k in ('X', 'Y', 'WIDTH', 'HEIGHT')):
|
|
565
|
+
state['frame'] = [int(geometry[k]) for k in ('X', 'Y', 'WIDTH', 'HEIGHT')]
|
|
566
|
+
if arranged['wanted'] is not None:
|
|
567
|
+
state['wanted'] = arranged['wanted']
|
|
568
|
+
return state
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
def key_to_window(pid, el, value):
|
|
572
|
+
if no_real_input() is not None:
|
|
573
|
+
return False
|
|
574
|
+
found = subprocess.run(['xdotool', 'search', '--onlyvisible', '--pid', str(pid)],
|
|
575
|
+
capture_output=True, text=True, timeout=10).stdout.split()
|
|
576
|
+
if not found:
|
|
577
|
+
return False
|
|
578
|
+
# Only when it does not have it: GTK chooses all the text of a field that takes the focus,
|
|
579
|
+
# and a Backspace then cleared the whole field.
|
|
580
|
+
component = safe(el.get_component_iface) if el is not None else None
|
|
581
|
+
if component is not None and not has(el, Atspi.StateType.FOCUSED):
|
|
582
|
+
safe(lambda: Atspi.Component.grab_focus(component))
|
|
583
|
+
parts = value.split('+')
|
|
584
|
+
key = parts[-1]
|
|
585
|
+
name = KEY_OF.get(key.lower(), key if len(key) == 1 else key.capitalize())
|
|
586
|
+
xdo('key', '--window', found[0], '+'.join([*mods_of(parts[:-1]), name]))
|
|
587
|
+
return True
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def mods_of(words):
|
|
591
|
+
return [MOD_KEY[m.lower()] for m in words if m.lower() in MOD_KEY]
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
def press_key(value):
|
|
595
|
+
parts = value.split('+')
|
|
596
|
+
key = parts[-1]
|
|
597
|
+
name = KEY_OF.get(key.lower(), key if len(key) == 1 else key.capitalize())
|
|
598
|
+
xdo('key', '--clearmodifiers', '+'.join([*mods_of(parts[:-1]), name]))
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
def middle_of(el):
|
|
602
|
+
frame = frame_of(el) or [0, 0, 0, 0]
|
|
603
|
+
return frame[0] + frame[2] / 2, frame[1] + frame[3] / 2
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
def press(el):
|
|
607
|
+
action = safe(el.get_action_iface)
|
|
608
|
+
count = safe(lambda: Atspi.Action.get_n_actions(action), 0) if action is not None else 0
|
|
609
|
+
names = [safe(lambda k=k: Atspi.Action.get_action_name(action, k), '') for k in range(count)]
|
|
610
|
+
for k, name in enumerate(names):
|
|
611
|
+
if name in ('click', 'press', 'activate', 'toggle', 'jump'):
|
|
612
|
+
return bool(safe(lambda k=k: Atspi.Action.do_action(action, k), False))
|
|
613
|
+
if count > 0:
|
|
614
|
+
return bool(safe(lambda: Atspi.Action.do_action(action, 0), False))
|
|
615
|
+
# The words of a row: the row or the list that holds them chooses it, as a click chooses it.
|
|
616
|
+
up = safe(el.get_parent)
|
|
617
|
+
for _ in range(4):
|
|
618
|
+
if up is None:
|
|
619
|
+
break
|
|
620
|
+
holder = safe(up.get_parent)
|
|
621
|
+
index = safe(up.get_index_in_parent)
|
|
622
|
+
if holder is not None and index is not None and safe(lambda: holder.select_child(index), False):
|
|
623
|
+
return True
|
|
624
|
+
up = holder
|
|
625
|
+
return False
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
def set_value(el, value):
|
|
629
|
+
try:
|
|
630
|
+
number = float(value)
|
|
631
|
+
except ValueError:
|
|
632
|
+
number = None
|
|
633
|
+
holder = safe(el.get_value_iface)
|
|
634
|
+
if number is not None and holder is not None and safe(lambda: Atspi.Value.set_current_value(holder, number), False):
|
|
635
|
+
return True
|
|
636
|
+
editable = safe(el.get_editable_text_iface)
|
|
637
|
+
return editable is not None and bool(safe(lambda: Atspi.EditableText.set_text_contents(editable, value), False))
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
def front_pid():
|
|
641
|
+
desktop = Atspi.get_desktop(0)
|
|
642
|
+
for k in range(desktop.get_child_count()):
|
|
643
|
+
app = safe(lambda: desktop.get_child_at_index(k))
|
|
644
|
+
if app is None:
|
|
645
|
+
continue
|
|
646
|
+
for w in children_of(app):
|
|
647
|
+
if has(w, Atspi.StateType.ACTIVE):
|
|
648
|
+
return safe(app.get_process_id)
|
|
649
|
+
return None
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
# Pictures of the screen, made small, and what changed between them.
|
|
653
|
+
def shot(x, y, w, h):
|
|
654
|
+
root = Gdk.get_default_root_window()
|
|
655
|
+
pix = safe(lambda: Gdk.pixbuf_get_from_window(root, int(x), int(y), int(w), int(h)))
|
|
656
|
+
if pix is None:
|
|
657
|
+
return None
|
|
658
|
+
pw = min(PICTURE_WIDTH, int(w))
|
|
659
|
+
ph = max(1, int(h) * pw // int(w))
|
|
660
|
+
small = pix.scale_simple(pw, ph, 2) # GdkPixbuf.InterpType.BILINEAR
|
|
661
|
+
return {'bytes': small.get_pixels(), 'w': pw, 'h': ph, 'stride': small.get_rowstride(),
|
|
662
|
+
'n': small.get_n_channels()}
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
def moved(a, b):
|
|
666
|
+
if a is None or b is None or a['w'] != b['w'] or a['h'] != b['h']:
|
|
667
|
+
return None
|
|
668
|
+
out = bytearray(a['w'] * a['h'])
|
|
669
|
+
for y in range(a['h']):
|
|
670
|
+
ra, rb = y * a['stride'], y * b['stride']
|
|
671
|
+
for x in range(a['w']):
|
|
672
|
+
pa, pb = ra + x * a['n'], rb + x * b['n']
|
|
673
|
+
if abs(a['bytes'][pa] - b['bytes'][pb]) > STEP or abs(a['bytes'][pa + 1] - b['bytes'][pb + 1]) > STEP \
|
|
674
|
+
or abs(a['bytes'][pa + 2] - b['bytes'][pb + 2]) > STEP:
|
|
675
|
+
out[y * a['w'] + x] = 1
|
|
676
|
+
return out
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
def change(earlier, before, after, later, wx, wy, ww, wh):
|
|
680
|
+
diff = moved(before, after)
|
|
681
|
+
if diff is None:
|
|
682
|
+
return {'changed': 1.0, 'box': [wx, wy, ww, wh]}
|
|
683
|
+
w, h = before['w'], before['h']
|
|
684
|
+
mask = bytearray(w * h)
|
|
685
|
+
# What moved between the two pictures of each pair moves by itself (a video, a spinner): it is
|
|
686
|
+
# made a little wider, and left out of the change.
|
|
687
|
+
for self_moved in (moved(earlier, before), moved(after, later)):
|
|
688
|
+
if self_moved is None:
|
|
689
|
+
continue
|
|
690
|
+
for p, on in enumerate(self_moved):
|
|
691
|
+
if not on:
|
|
692
|
+
continue
|
|
693
|
+
y, x = divmod(p, w)
|
|
694
|
+
for yy in range(max(0, y - 2), min(h, y + 3)):
|
|
695
|
+
for xx in range(max(0, x - 2), min(w, x + 3)):
|
|
696
|
+
mask[yy * w + xx] = 1
|
|
697
|
+
count = moving = 0
|
|
698
|
+
min_x, min_y, max_x, max_y = w, h, -1, -1
|
|
699
|
+
for p in range(w * h):
|
|
700
|
+
if mask[p]:
|
|
701
|
+
moving += 1
|
|
702
|
+
continue
|
|
703
|
+
if not diff[p]:
|
|
704
|
+
continue
|
|
705
|
+
count += 1
|
|
706
|
+
y, x = divmod(p, w)
|
|
707
|
+
min_x, max_x, min_y, max_y = min(min_x, x), max(max_x, x), min(min_y, y), max(max_y, y)
|
|
708
|
+
result = {'changed': round(count / (w * h), 4)}
|
|
709
|
+
if moving:
|
|
710
|
+
result['moving'] = round(moving / (w * h), 4)
|
|
711
|
+
if count:
|
|
712
|
+
sx, sy = ww / w, wh / h
|
|
713
|
+
result['box'] = [round(wx + min_x * sx), round(wy + min_y * sy),
|
|
714
|
+
round((max_x - min_x + 1) * sx), round((max_y - min_y + 1) * sy)]
|
|
715
|
+
return result
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
def act(words):
|
|
719
|
+
pid, path, role, name, kind, value = int(words[1]), words[2], words[3], words[4], words[5], words[6]
|
|
720
|
+
budget_ms = int(words[7])
|
|
721
|
+
how = words[8].split(':')
|
|
722
|
+
quiet = int(how[1]) if len(how) > 1 else QUIET_MS
|
|
723
|
+
foreground, direct = how[0] == 'foreground', how[0] == 'direct'
|
|
724
|
+
if app_of(pid) is None:
|
|
725
|
+
return {'error': 'app-gone'}
|
|
726
|
+
found = find(pid, path)
|
|
727
|
+
if found is None:
|
|
728
|
+
return {'refused': 'stale', 'detail': 'no element at the path'}
|
|
729
|
+
el = found.get('el')
|
|
730
|
+
if el is not None:
|
|
731
|
+
if role_of(el) != role or (safe(el.get_name, '') or '') != name:
|
|
732
|
+
return {'refused': 'stale', 'detail': 'a different element is at the path'}
|
|
733
|
+
if not enabled_of(el) and kind != 'raise':
|
|
734
|
+
return {'refused': 'disabled'}
|
|
735
|
+
pointer = kind in POINTER
|
|
736
|
+
if pointer and not foreground:
|
|
737
|
+
return {'refused': 'needs-foreground'}
|
|
738
|
+
no_input = no_real_input()
|
|
739
|
+
if no_input is not None and (pointer or kind == 'window' or (foreground and kind in ('type', 'key'))):
|
|
740
|
+
return {'refused': 'no-real-input', 'detail': no_input}
|
|
741
|
+
# A pointer value is its points, then the hand: "x,y|mods=shift+alt|hold=300|pixels=1".
|
|
742
|
+
hand = {}
|
|
743
|
+
if pointer:
|
|
744
|
+
parts = str(value).split('|')
|
|
745
|
+
value = parts[0]
|
|
746
|
+
for part in parts[1:]:
|
|
747
|
+
pair = part.split('=', 1)
|
|
748
|
+
if len(pair) == 2:
|
|
749
|
+
hand[pair[0]] = pair[1]
|
|
750
|
+
held = mods_of(hand.get('mods', '').split('+'))
|
|
751
|
+
hold_ms = min(10000, max(0, int(hand.get('hold', '0') or 0)))
|
|
752
|
+
# The person at the machine is typing or moving the pointer: wait, and refuse when they still are.
|
|
753
|
+
if foreground and idle_ms() < 800:
|
|
754
|
+
time.sleep(0.8)
|
|
755
|
+
if idle_ms() < 800:
|
|
756
|
+
return {'refused': 'person-active', 'detail': 'the person at the machine is typing or moving the pointer'}
|
|
757
|
+
rch = reach(el, pid) if el is not None else {'reach': 'yes'}
|
|
758
|
+
if rch['reach'] != 'yes' and not direct and kind not in ('raise', 'window') and pointer:
|
|
759
|
+
if rch['reach'] == 'no':
|
|
760
|
+
return {'refused': 'unreachable', 'detail': rch.get('detail')}
|
|
761
|
+
return {'refused': 'reach-unknown', 'detail': rch.get('detail')}
|
|
762
|
+
before = read_app(pid)
|
|
763
|
+
shots = None
|
|
764
|
+
if pointer and kind != 'hover' and hand.get('pixels') == '1':
|
|
765
|
+
shown, _ = windows_of(app_of(pid))
|
|
766
|
+
wr = frame_of(shown[0]) if shown else None
|
|
767
|
+
if wr is not None:
|
|
768
|
+
shots = {'rect': wr, 'earlier': shot(*wr)}
|
|
769
|
+
time.sleep(0.1)
|
|
770
|
+
shots['before'] = shot(*wr)
|
|
771
|
+
for m in held:
|
|
772
|
+
xdo('keydown', m)
|
|
773
|
+
delivered = True
|
|
774
|
+
arranged = None
|
|
775
|
+
try:
|
|
776
|
+
if kind == 'press':
|
|
777
|
+
delivered = press(el)
|
|
778
|
+
elif kind == 'set':
|
|
779
|
+
delivered = set_value(el, value)
|
|
780
|
+
elif kind == 'raise':
|
|
781
|
+
target = el if el is not None else (windows_of(app_of(pid))[0] or [None])[0]
|
|
782
|
+
component = safe(target.get_component_iface) if target is not None else None
|
|
783
|
+
delivered = component is not None and bool(safe(lambda: Atspi.Component.grab_focus(component), False))
|
|
784
|
+
elif kind in ('type', 'key'):
|
|
785
|
+
if not foreground and kind == 'type':
|
|
786
|
+
# Text in the background goes to the field as its value.
|
|
787
|
+
delivered = set_value(el, value)
|
|
788
|
+
elif not foreground:
|
|
789
|
+
# A key in the background goes to the window of the application alone, as an X
|
|
790
|
+
# event sent to that window: the pointer and the keyboard of the person are not used.
|
|
791
|
+
# The element takes the focus of its window first, as a click in it does.
|
|
792
|
+
delivered = key_to_window(pid, el, value)
|
|
793
|
+
elif front_pid() not in (None, pid):
|
|
794
|
+
return {'refused': 'no-keyboard'}
|
|
795
|
+
elif kind == 'type':
|
|
796
|
+
xdo('type', '--delay', 8, '--', value)
|
|
797
|
+
else:
|
|
798
|
+
press_key(value)
|
|
799
|
+
elif kind in ('click', 'dblclick', 'rightclick', 'hover'):
|
|
800
|
+
xy = [v for v in str(value).split(',') if v != '']
|
|
801
|
+
# No point given: the point that the reach check found open, else the middle.
|
|
802
|
+
if len(xy) >= 2:
|
|
803
|
+
x, y = float(xy[0]), float(xy[1])
|
|
804
|
+
else:
|
|
805
|
+
x, y = rch.get('at') or middle_of(el)
|
|
806
|
+
if kind == 'hover':
|
|
807
|
+
move(x, y)
|
|
808
|
+
else:
|
|
809
|
+
# "count=3" is a triple-click, "button=middle" the middle button.
|
|
810
|
+
times = 2 if kind == 'dblclick' else min(3, max(1, int(hand.get('count', '1') or 1)))
|
|
811
|
+
button = 3 if kind == 'rightclick' else (2 if hand.get('button') == 'middle' else 1)
|
|
812
|
+
click(x, y, times, button, hold_ms)
|
|
813
|
+
elif kind == 'drag':
|
|
814
|
+
# Two numbers: the end, from the middle of the element. Four or more: a path, from its
|
|
815
|
+
# first point. The pointer walks each part in small steps, as a hand moves.
|
|
816
|
+
n = [float(v) for v in str(value).split(',') if v != '']
|
|
817
|
+
way = [middle_of(el)] if len(n) == 2 else []
|
|
818
|
+
way += [(n[k], n[k + 1]) for k in range(0, len(n) - 1, 2)]
|
|
819
|
+
if len(way) < 2:
|
|
820
|
+
return {'error': 'a drag needs an end'}
|
|
821
|
+
move(*way[0])
|
|
822
|
+
xdo('mousedown', 1)
|
|
823
|
+
if hold_ms:
|
|
824
|
+
time.sleep(hold_ms / 1000)
|
|
825
|
+
for a, b in zip(way, way[1:]):
|
|
826
|
+
steps = max(4, min(40, int(((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2) ** 0.5 / 6)))
|
|
827
|
+
for s in range(1, steps + 1):
|
|
828
|
+
move(a[0] + (b[0] - a[0]) * s / steps, a[1] + (b[1] - a[1]) * s / steps)
|
|
829
|
+
time.sleep(0.008)
|
|
830
|
+
xdo('mouseup', 1)
|
|
831
|
+
elif kind == 'wheel':
|
|
832
|
+
n = str(value).split(',')
|
|
833
|
+
x, y = (float(n[1]), float(n[2])) if len(n) >= 3 else middle_of(el)
|
|
834
|
+
move(x, y)
|
|
835
|
+
lines = float(n[0] or 0)
|
|
836
|
+
for _ in range(int(abs(lines) // 3) or (1 if lines else 0)):
|
|
837
|
+
xdo('click', 5 if lines > 0 else 4)
|
|
838
|
+
# "across=5" turns the wheel to the side: more than 0 is to the right.
|
|
839
|
+
across = int(hand.get('across', '0') or 0)
|
|
840
|
+
for _ in range(abs(across) // 3 or (1 if across else 0)):
|
|
841
|
+
xdo('click', 7 if across > 0 else 6)
|
|
842
|
+
elif kind == 'window':
|
|
843
|
+
arranged = arrange(pid, value)
|
|
844
|
+
delivered = arranged is not None
|
|
845
|
+
else:
|
|
846
|
+
return {'error': f'unknown-action {kind}'}
|
|
847
|
+
except Exception as error: # noqa: BLE001
|
|
848
|
+
delivered = False
|
|
849
|
+
last_error = str(error)
|
|
850
|
+
else:
|
|
851
|
+
last_error = None
|
|
852
|
+
finally:
|
|
853
|
+
for m in held:
|
|
854
|
+
xdo('keyup', m)
|
|
855
|
+
settled = wait_quiet(pid, budget_ms, quiet)
|
|
856
|
+
after = read_app(pid)
|
|
857
|
+
if after is None:
|
|
858
|
+
return {'delivered': delivered, 'reachable': True, 'settled': settled, 'appGone': True,
|
|
859
|
+
'before': before['tree'], 'after': {'role': 'AXApplication', 'children': []},
|
|
860
|
+
'unreadBefore': before['unread'], 'unreadAfter': 0}
|
|
861
|
+
result = {'delivered': delivered, 'reachable': rch['reach'] == 'yes', 'settled': settled,
|
|
862
|
+
'before': before['tree'], 'after': after['tree'], 'unreadBefore': before['unread'],
|
|
863
|
+
'unreadAfter': after['unread']}
|
|
864
|
+
if shots is not None:
|
|
865
|
+
wr = shots['rect']
|
|
866
|
+
seen = shot(*wr)
|
|
867
|
+
time.sleep(0.1)
|
|
868
|
+
later = shot(*wr)
|
|
869
|
+
result['pixels'] = change(shots['earlier'], shots['before'], seen, later, *wr)
|
|
870
|
+
if arranged is not None:
|
|
871
|
+
result['windowState'] = window_state(arranged)
|
|
872
|
+
if os.environ.get('RETICLE_ATSPI_DEBUG') and last_error:
|
|
873
|
+
result['atspiError'] = last_error
|
|
874
|
+
return result
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
def capture(words):
|
|
878
|
+
pid, path, screen = int(words[1]), words[2], len(words) > 3 and words[3] == 'screen'
|
|
879
|
+
app = app_of(pid)
|
|
880
|
+
shown, _ = windows_of(app) if app is not None else ([], 0)
|
|
881
|
+
frame = frame_of(shown[0]) if shown else None
|
|
882
|
+
if frame is None:
|
|
883
|
+
return {'ok': False, 'reason': 'the application has no window on the screen'}
|
|
884
|
+
root = Gdk.get_default_root_window()
|
|
885
|
+
area = [0, 0, root.get_width(), root.get_height()] if screen else frame
|
|
886
|
+
pix = Gdk.pixbuf_get_from_window(root, *area)
|
|
887
|
+
if pix is None:
|
|
888
|
+
return {'ok': False, 'reason': 'the screen could not be read'}
|
|
889
|
+
pix.savev(path, 'png', [], [])
|
|
890
|
+
result = {'ok': True, 'window': area, 'scale': 1}
|
|
891
|
+
if screen:
|
|
892
|
+
result['appWindow'] = frame
|
|
893
|
+
return result
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
def command(words):
|
|
897
|
+
if words[0] == 'apps':
|
|
898
|
+
desktop = Atspi.get_desktop(0)
|
|
899
|
+
apps = []
|
|
900
|
+
for k in range(desktop.get_child_count()):
|
|
901
|
+
app = safe(lambda k=k: desktop.get_child_at_index(k))
|
|
902
|
+
if app is not None and windows_of(app)[0]:
|
|
903
|
+
name = safe(app.get_name, '')
|
|
904
|
+
apps.append({'name': name, 'process': name, 'pid': safe(app.get_process_id), 'regular': True})
|
|
905
|
+
return {'apps': apps}
|
|
906
|
+
if words[0] == 'tree':
|
|
907
|
+
read = read_app(int(words[1]))
|
|
908
|
+
return {'error': 'app-gone'} if read is None else read
|
|
909
|
+
if words[0] == 'reach':
|
|
910
|
+
found = find(int(words[1]), words[2])
|
|
911
|
+
if found is None or found.get('el') is None:
|
|
912
|
+
return {'refused': 'stale'}
|
|
913
|
+
return reach(found['el'], int(words[1]))
|
|
914
|
+
if words[0] == 'act':
|
|
915
|
+
return act(words)
|
|
916
|
+
if words[0] == 'capture':
|
|
917
|
+
return capture(words)
|
|
918
|
+
return {'error': 'usage'}
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
def main():
|
|
922
|
+
if len(sys.argv) > 1 and sys.argv[1] == 'serve':
|
|
923
|
+
for line in sys.stdin:
|
|
924
|
+
try:
|
|
925
|
+
answer(command([str(w) for w in json.loads(line)]))
|
|
926
|
+
except Exception as error: # noqa: BLE001
|
|
927
|
+
answer({'error': str(error)})
|
|
928
|
+
else:
|
|
929
|
+
answer(command(sys.argv[1:]))
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
if __name__ == '__main__':
|
|
933
|
+
main()
|