dsh-quay-bot 0.7.4 → 1.0.0
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 +21 -0
- package/README.md +105 -28
- package/bin/doctor.mjs +148 -0
- package/botscreen/requirements-offline.txt +11 -0
- package/client.js +1 -2
- package/cordis.patch.yml +1 -1
- package/host/api.js +0 -4
- package/host/remote.js +2 -140
- package/host/screen.js +85 -21
- package/market/index.json +41 -0
- package/package.json +32 -13
- package/vendor/wheels/any/annotated_doc-0.0.7-py3-none-any.whl +0 -0
- package/vendor/wheels/any/annotated_types-0.8.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/anyio-4.16.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/certifi-2026.11.20-py3-none-any.whl +0 -0
- package/vendor/wheels/any/click-8.5.1-py3-none-any.whl +0 -0
- package/vendor/wheels/any/fastapi-0.165.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/h11-0.16.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/httpcore-1.0.9-py3-none-any.whl +0 -0
- package/vendor/wheels/any/httpx-0.29.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/idna-3.20-py3-none-any.whl +0 -0
- package/vendor/wheels/any/mss-10.2.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/opentelemetry_api-1.45.2-py3-none-any.whl +0 -0
- package/vendor/wheels/any/pydantic-2.14.4-py3-none-any.whl +0 -0
- package/vendor/wheels/any/python_multipart-0.0.34-py3-none-any.whl +0 -0
- package/vendor/wheels/any/python_xlib-0.33-py2.py3-none-any.whl +0 -0
- package/vendor/wheels/any/six-1.17.0-py2.py3-none-any.whl +0 -0
- package/vendor/wheels/any/starlette-1.10.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/typing_extensions-4.16.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/typing_inspection-0.4.5-py3-none-any.whl +0 -0
- package/vendor/wheels/any/uvicorn-0.54.0-py3-none-any.whl +0 -0
- package/vendor/wheels/linux-aarch64/pillow-12.2.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl +0 -0
- package/vendor/wheels/linux-aarch64/pydantic_core-2.50.5-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl +0 -0
- package/vendor/wheels/linux-aarch64/websockets-17.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl +0 -0
- package/vendor/wheels/linux-x86_64/pillow-12.2.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl +0 -0
- package/vendor/wheels/linux-x86_64/pydantic_core-2.50.5-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl +0 -0
- package/vendor/wheels/linux-x86_64/websockets-17.4-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl +0 -0
- package/botscreen/botscreen/__init__.py +0 -2
- package/botscreen/botscreen/__main__.py +0 -5
- package/botscreen/botscreen/artifacts.py +0 -388
- package/botscreen/botscreen/audit.py +0 -94
- package/botscreen/botscreen/cgroup.py +0 -293
- package/botscreen/botscreen/config.py +0 -320
- package/botscreen/botscreen/desktop/assets/chrome.png +0 -0
- package/botscreen/botscreen/desktop/assets/codex.png +0 -0
- package/botscreen/botscreen/desktop/assets/drive.png +0 -0
- package/botscreen/botscreen/desktop/assets/folder.png +0 -0
- package/botscreen/botscreen/desktop/assets/search.png +0 -0
- package/botscreen/botscreen/desktop/assets/terminal.png +0 -0
- package/botscreen/botscreen/desktop/assets/trash.png +0 -0
- package/botscreen/botscreen/desktop/assets/wallpaper.png +0 -0
- package/botscreen/botscreen/desktop.py +0 -244
- package/botscreen/botscreen/driver.py +0 -832
- package/botscreen/botscreen/gateway.py +0 -2089
- package/botscreen/botscreen/iconlayer.py +0 -841
- package/botscreen/botscreen/mcp_server.py +0 -435
- package/botscreen/botscreen/resources.py +0 -494
- package/botscreen/botscreen/screen.py +0 -1282
- package/botscreen/botscreen/session.py +0 -706
- package/botscreen/botscreen/state.py +0 -330
- package/botscreen/botscreen/streamer.py +0 -388
- package/remote/bin/dsh-remote.mjs +0 -418
- package/remote/package.json +0 -27
- package/remote/src/config.mjs +0 -183
- package/remote/src/crypto.mjs +0 -220
- package/remote/src/keys.mjs +0 -226
- package/remote/src/proxy.mjs +0 -1028
- package/remote/src/sessions.mjs +0 -163
- package/remote/src/shim.js +0 -838
- package/remote/src/supervisor.mjs +0 -207
- package/remote/src/tunnel.mjs +0 -489
- package/remote/src/upstream.mjs +0 -372
- package/remote/src/util.mjs +0 -139
- package/remote/src/ws.mjs +0 -313
- package/requirements.txt +0 -8
|
@@ -1,832 +0,0 @@
|
|
|
1
|
-
"""执行器:按需 XShm 零拷贝截屏(忽略光标层) + 免 Root X11 XTest 毫秒级键鼠注入。
|
|
2
|
-
|
|
3
|
-
核心特性
|
|
4
|
-
--------
|
|
5
|
-
1. **XShm 共享内存抓帧(默认)**:直接调用 `libX11` + `libXext` 的 `XShmGetImage` 读取
|
|
6
|
-
Root Window 像素缓冲,不捕获鼠标光标层,支持 24/32 位(BGRX)与 16 位(BGR;16)色深;
|
|
7
|
-
Attach 后立即标记 `IPC_RMID`,即使进程异常退出也绝不残留共享内存段。若运行环境不支持
|
|
8
|
-
MIT-SHM 则自动回退 `python-xlib` ZPixmap。
|
|
9
|
-
2. **空闲自动释放抓屏资源**:提供 `close_capture()`,当无人类观看超过 5 秒时由推流器调用,
|
|
10
|
-
立即卸载共享内存并关闭抓屏 X11 连接,平时常驻 0 抓屏开销。
|
|
11
|
-
3. **比例坐标映射 + 免 Root XTest 事件注入**:支持前端传入归一化比例 `(rx, ry)` 自动按当前
|
|
12
|
-
虚拟屏真实分辨率换算,鼠标点击/拖拽/滚轮与 ASCII 键盘按键直走 X11 `XTest` 扩展(<0.5ms),
|
|
13
|
-
中文/Emoji 自动走剪贴板粘贴通道。
|
|
14
|
-
"""
|
|
15
|
-
from __future__ import annotations
|
|
16
|
-
|
|
17
|
-
import ctypes
|
|
18
|
-
import io
|
|
19
|
-
import os
|
|
20
|
-
import subprocess
|
|
21
|
-
import threading
|
|
22
|
-
import time
|
|
23
|
-
from dataclasses import dataclass
|
|
24
|
-
from typing import Optional, Sequence, Tuple
|
|
25
|
-
|
|
26
|
-
from PIL import Image
|
|
27
|
-
|
|
28
|
-
from .config import SHARED_XAUTHORITY, BotPaths, SCREEN_HEIGHT, SCREEN_WIDTH, THUMB_WIDTH
|
|
29
|
-
|
|
30
|
-
CAPTURE_BACKEND = os.environ.get("BOTSCREEN_CAPTURE", "xshm").strip().lower()
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class DriverError(RuntimeError):
|
|
34
|
-
pass
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
@dataclass
|
|
38
|
-
class RawFrame:
|
|
39
|
-
data: bytes
|
|
40
|
-
width: int
|
|
41
|
-
height: int
|
|
42
|
-
stride: int
|
|
43
|
-
bpp: int
|
|
44
|
-
|
|
45
|
-
def to_image(self) -> Image.Image:
|
|
46
|
-
mode = "BGR;16" if self.bpp == 16 else "BGRX"
|
|
47
|
-
img = Image.frombytes("RGB", (self.width, self.height), self.data, "raw", mode, self.stride, 1)
|
|
48
|
-
if img.size != (SCREEN_WIDTH, SCREEN_HEIGHT):
|
|
49
|
-
img = img.crop((0, 0, min(img.width, SCREEN_WIDTH), min(img.height, SCREEN_HEIGHT)))
|
|
50
|
-
return img
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
@dataclass
|
|
54
|
-
class Shot:
|
|
55
|
-
data: bytes
|
|
56
|
-
width: int
|
|
57
|
-
height: int
|
|
58
|
-
mime: str
|
|
59
|
-
thumb: Optional[bytes] = None
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
# ---- Ctypes MIT-SHM 绑定(懒加载) ------------------------------------------
|
|
63
|
-
class _XImage(ctypes.Structure):
|
|
64
|
-
_fields_ = [
|
|
65
|
-
("width", ctypes.c_int),
|
|
66
|
-
("height", ctypes.c_int),
|
|
67
|
-
("xoffset", ctypes.c_int),
|
|
68
|
-
("format", ctypes.c_int),
|
|
69
|
-
("data", ctypes.c_void_p),
|
|
70
|
-
("byte_order", ctypes.c_int),
|
|
71
|
-
("bitmap_unit", ctypes.c_int),
|
|
72
|
-
("bitmap_bit_order", ctypes.c_int),
|
|
73
|
-
("bitmap_pad", ctypes.c_int),
|
|
74
|
-
("depth", ctypes.c_int),
|
|
75
|
-
("bytes_per_line", ctypes.c_int),
|
|
76
|
-
("bits_per_pixel", ctypes.c_int),
|
|
77
|
-
]
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
class _XShmSegmentInfo(ctypes.Structure):
|
|
81
|
-
_fields_ = [
|
|
82
|
-
("shmseg", ctypes.c_ulong),
|
|
83
|
-
("shmid", ctypes.c_int),
|
|
84
|
-
("shmaddr", ctypes.c_void_p),
|
|
85
|
-
("readOnly", ctypes.c_int),
|
|
86
|
-
]
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
_SHM_LIBS = None
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def _load_shm_libs():
|
|
93
|
-
global _SHM_LIBS
|
|
94
|
-
if _SHM_LIBS is not None:
|
|
95
|
-
return _SHM_LIBS
|
|
96
|
-
libX11 = ctypes.CDLL("libX11.so.6")
|
|
97
|
-
libXext = ctypes.CDLL("libXext.so.6")
|
|
98
|
-
libc = ctypes.CDLL("libc.so.6")
|
|
99
|
-
|
|
100
|
-
libX11.XOpenDisplay.argtypes = [ctypes.c_char_p]
|
|
101
|
-
libX11.XOpenDisplay.restype = ctypes.c_void_p
|
|
102
|
-
libX11.XDefaultScreen.argtypes = [ctypes.c_void_p]
|
|
103
|
-
libX11.XDefaultScreen.restype = ctypes.c_int
|
|
104
|
-
libX11.XRootWindow.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
105
|
-
libX11.XRootWindow.restype = ctypes.c_ulong
|
|
106
|
-
libX11.XDefaultVisual.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
107
|
-
libX11.XDefaultVisual.restype = ctypes.c_void_p
|
|
108
|
-
libX11.XDefaultDepth.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
109
|
-
libX11.XDefaultDepth.restype = ctypes.c_int
|
|
110
|
-
libX11.XDisplayWidth.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
111
|
-
libX11.XDisplayWidth.restype = ctypes.c_int
|
|
112
|
-
libX11.XDisplayHeight.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
113
|
-
libX11.XDisplayHeight.restype = ctypes.c_int
|
|
114
|
-
libX11.XSync.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
115
|
-
libX11.XDestroyImage.argtypes = [ctypes.POINTER(_XImage)]
|
|
116
|
-
libX11.XCloseDisplay.argtypes = [ctypes.c_void_p]
|
|
117
|
-
|
|
118
|
-
libXext.XShmQueryExtension.argtypes = [ctypes.c_void_p]
|
|
119
|
-
libXext.XShmQueryExtension.restype = ctypes.c_int
|
|
120
|
-
libXext.XShmCreateImage.argtypes = [
|
|
121
|
-
ctypes.c_void_p,
|
|
122
|
-
ctypes.c_void_p,
|
|
123
|
-
ctypes.c_uint,
|
|
124
|
-
ctypes.c_int,
|
|
125
|
-
ctypes.c_void_p,
|
|
126
|
-
ctypes.POINTER(_XShmSegmentInfo),
|
|
127
|
-
ctypes.c_uint,
|
|
128
|
-
ctypes.c_uint,
|
|
129
|
-
]
|
|
130
|
-
libXext.XShmCreateImage.restype = ctypes.POINTER(_XImage)
|
|
131
|
-
libXext.XShmAttach.argtypes = [ctypes.c_void_p, ctypes.POINTER(_XShmSegmentInfo)]
|
|
132
|
-
libXext.XShmAttach.restype = ctypes.c_int
|
|
133
|
-
libXext.XShmDetach.argtypes = [ctypes.c_void_p, ctypes.POINTER(_XShmSegmentInfo)]
|
|
134
|
-
libXext.XShmGetImage.argtypes = [
|
|
135
|
-
ctypes.c_void_p,
|
|
136
|
-
ctypes.c_ulong,
|
|
137
|
-
ctypes.POINTER(_XImage),
|
|
138
|
-
ctypes.c_int,
|
|
139
|
-
ctypes.c_int,
|
|
140
|
-
ctypes.c_ulong,
|
|
141
|
-
]
|
|
142
|
-
libXext.XShmGetImage.restype = ctypes.c_int
|
|
143
|
-
|
|
144
|
-
libc.shmget.argtypes = [ctypes.c_int, ctypes.c_size_t, ctypes.c_int]
|
|
145
|
-
libc.shmget.restype = ctypes.c_int
|
|
146
|
-
libc.shmat.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_int]
|
|
147
|
-
libc.shmat.restype = ctypes.c_void_p
|
|
148
|
-
libc.shmdt.argtypes = [ctypes.c_void_p]
|
|
149
|
-
libc.shmctl.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_void_p]
|
|
150
|
-
|
|
151
|
-
_SHM_LIBS = (libX11, libXext, libc)
|
|
152
|
-
return _SHM_LIBS
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
class _XShmGrabber:
|
|
156
|
-
"""MIT-SHM 零拷贝抓屏器;抓 Root Window 天然忽略光标层。"""
|
|
157
|
-
|
|
158
|
-
def __init__(self, display_num: int) -> None:
|
|
159
|
-
os.environ.setdefault("XAUTHORITY", SHARED_XAUTHORITY)
|
|
160
|
-
libX11, libXext, libc = _load_shm_libs()
|
|
161
|
-
self._libX11 = libX11
|
|
162
|
-
self._libXext = libXext
|
|
163
|
-
self._libc = libc
|
|
164
|
-
self._dpy = libX11.XOpenDisplay(f":{display_num}".encode())
|
|
165
|
-
if not self._dpy:
|
|
166
|
-
raise DriverError(f"XOpenDisplay :{display_num} failed")
|
|
167
|
-
self._ximg = None
|
|
168
|
-
self._addr = None
|
|
169
|
-
self._attached = False
|
|
170
|
-
try:
|
|
171
|
-
if not libXext.XShmQueryExtension(self._dpy):
|
|
172
|
-
raise DriverError("MIT-SHM extension unavailable")
|
|
173
|
-
scr = libX11.XDefaultScreen(self._dpy)
|
|
174
|
-
self.width = min(SCREEN_WIDTH, libX11.XDisplayWidth(self._dpy, scr))
|
|
175
|
-
self.height = min(SCREEN_HEIGHT, libX11.XDisplayHeight(self._dpy, scr))
|
|
176
|
-
depth = libX11.XDefaultDepth(self._dpy, scr)
|
|
177
|
-
vis = libX11.XDefaultVisual(self._dpy, scr)
|
|
178
|
-
self._root = libX11.XRootWindow(self._dpy, scr)
|
|
179
|
-
self._shminfo = _XShmSegmentInfo()
|
|
180
|
-
# ZPixmap == 2
|
|
181
|
-
self._ximg = libXext.XShmCreateImage(
|
|
182
|
-
self._dpy, vis, depth, 2, None, ctypes.byref(self._shminfo), self.width, self.height
|
|
183
|
-
)
|
|
184
|
-
if not self._ximg:
|
|
185
|
-
raise DriverError("XShmCreateImage failed")
|
|
186
|
-
self.stride = int(self._ximg.contents.bytes_per_line)
|
|
187
|
-
self.bpp = int(self._ximg.contents.bits_per_pixel)
|
|
188
|
-
self._size = self.stride * self.height
|
|
189
|
-
shmid = libc.shmget(0, self._size, 0o1000 | 0o600)
|
|
190
|
-
if shmid < 0:
|
|
191
|
-
raise DriverError("shmget failed")
|
|
192
|
-
addr = libc.shmat(shmid, None, 0)
|
|
193
|
-
if addr in (None, ctypes.c_void_p(-1).value, 0xFFFFFFFFFFFFFFFF):
|
|
194
|
-
libc.shmctl(shmid, 0, None)
|
|
195
|
-
raise DriverError("shmat failed")
|
|
196
|
-
self._addr = addr
|
|
197
|
-
self._shminfo.shmid = shmid
|
|
198
|
-
self._shminfo.shmaddr = addr
|
|
199
|
-
self._shminfo.readOnly = 0
|
|
200
|
-
self._ximg.contents.data = addr
|
|
201
|
-
if not libXext.XShmAttach(self._dpy, ctypes.byref(self._shminfo)):
|
|
202
|
-
libc.shmctl(shmid, 0, None)
|
|
203
|
-
raise DriverError("XShmAttach failed")
|
|
204
|
-
self._attached = True
|
|
205
|
-
libX11.XSync(self._dpy, 0)
|
|
206
|
-
# 立即标记 IPC_RMID:双方 detach 后内核自动回收,绝不泄漏 shm 段
|
|
207
|
-
libc.shmctl(shmid, 0, None)
|
|
208
|
-
except BaseException:
|
|
209
|
-
self.close()
|
|
210
|
-
raise
|
|
211
|
-
|
|
212
|
-
def grab_raw(self) -> RawFrame:
|
|
213
|
-
ok = self._libXext.XShmGetImage(self._dpy, self._root, self._ximg, 0, 0, 0xFFFFFFFF)
|
|
214
|
-
if not ok:
|
|
215
|
-
raise DriverError("XShmGetImage failed")
|
|
216
|
-
raw = ctypes.string_at(self._addr, self._size)
|
|
217
|
-
return RawFrame(raw, self.width, self.height, self.stride, self.bpp)
|
|
218
|
-
|
|
219
|
-
def grab(self) -> Image.Image:
|
|
220
|
-
return self.grab_raw().to_image()
|
|
221
|
-
|
|
222
|
-
def close(self) -> None:
|
|
223
|
-
try:
|
|
224
|
-
if self._attached and self._dpy:
|
|
225
|
-
self._libXext.XShmDetach(self._dpy, ctypes.byref(self._shminfo))
|
|
226
|
-
self._libX11.XSync(self._dpy, 0)
|
|
227
|
-
except Exception: # noqa: BLE001
|
|
228
|
-
pass
|
|
229
|
-
self._attached = False
|
|
230
|
-
try:
|
|
231
|
-
if self._ximg:
|
|
232
|
-
self._ximg.contents.data = None
|
|
233
|
-
self._libX11.XDestroyImage(self._ximg)
|
|
234
|
-
except Exception: # noqa: BLE001
|
|
235
|
-
pass
|
|
236
|
-
self._ximg = None
|
|
237
|
-
try:
|
|
238
|
-
if self._addr:
|
|
239
|
-
self._libc.shmdt(self._addr)
|
|
240
|
-
except Exception: # noqa: BLE001
|
|
241
|
-
pass
|
|
242
|
-
self._addr = None
|
|
243
|
-
try:
|
|
244
|
-
if self._dpy:
|
|
245
|
-
self._libX11.XCloseDisplay(self._dpy)
|
|
246
|
-
except Exception: # noqa: BLE001
|
|
247
|
-
pass
|
|
248
|
-
self._dpy = None
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
class _XlibGrabber:
|
|
252
|
-
"""回退方案:python-xlib ZPixmap 读取 Root Window(同样忽略光标层)。"""
|
|
253
|
-
|
|
254
|
-
def __init__(self, display_num: int) -> None:
|
|
255
|
-
from Xlib import display as xdisplay
|
|
256
|
-
|
|
257
|
-
self.display_num = display_num
|
|
258
|
-
os.environ.setdefault("XAUTHORITY", SHARED_XAUTHORITY)
|
|
259
|
-
self._dpy = xdisplay.Display(f":{display_num}")
|
|
260
|
-
self._screen = self._dpy.screen()
|
|
261
|
-
self.width = min(SCREEN_WIDTH, self._screen.width_in_pixels)
|
|
262
|
-
self.height = min(SCREEN_HEIGHT, self._screen.height_in_pixels)
|
|
263
|
-
self.bpp = 16 if self._screen.root_depth == 16 else 32
|
|
264
|
-
self.stride = self.width * (2 if self.bpp == 16 else 4)
|
|
265
|
-
|
|
266
|
-
def grab_raw(self) -> RawFrame:
|
|
267
|
-
from Xlib import X
|
|
268
|
-
|
|
269
|
-
raw = self._screen.root.get_image(0, 0, self.width, self.height, X.ZPixmap, 0xFFFFFFFF)
|
|
270
|
-
data = raw.data
|
|
271
|
-
if isinstance(data, str):
|
|
272
|
-
data = data.encode("latin-1")
|
|
273
|
-
return RawFrame(data, self.width, self.height, self.stride, self.bpp)
|
|
274
|
-
|
|
275
|
-
def grab(self) -> Image.Image:
|
|
276
|
-
return self.grab_raw().to_image()
|
|
277
|
-
|
|
278
|
-
def close(self) -> None:
|
|
279
|
-
try:
|
|
280
|
-
self._dpy.close()
|
|
281
|
-
except Exception: # noqa: BLE001
|
|
282
|
-
pass
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
_SPECIAL_KEYSYMS = {
|
|
286
|
-
"enter": "Return",
|
|
287
|
-
"return": "Return",
|
|
288
|
-
"backspace": "BackSpace",
|
|
289
|
-
"tab": "Tab",
|
|
290
|
-
"esc": "Escape",
|
|
291
|
-
"escape": "Escape",
|
|
292
|
-
"space": "space",
|
|
293
|
-
"delete": "Delete",
|
|
294
|
-
"del": "Delete",
|
|
295
|
-
"up": "Up",
|
|
296
|
-
"down": "Down",
|
|
297
|
-
"left": "Left",
|
|
298
|
-
"right": "Right",
|
|
299
|
-
"home": "Home",
|
|
300
|
-
"end": "End",
|
|
301
|
-
"pageup": "Prior",
|
|
302
|
-
"prior": "Prior",
|
|
303
|
-
"pagedown": "Next",
|
|
304
|
-
"next": "Next",
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
_MODIFIER_KEYSYMS = {
|
|
308
|
-
"ctrl": "Control_L",
|
|
309
|
-
"control": "Control_L",
|
|
310
|
-
"alt": "Alt_L",
|
|
311
|
-
"shift": "Shift_L",
|
|
312
|
-
"super": "Super_L",
|
|
313
|
-
"meta": "Super_L",
|
|
314
|
-
"win": "Super_L",
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
class ScreenDriver:
|
|
319
|
-
def __init__(self, bot_id: str, display: int, paths: BotPaths) -> None:
|
|
320
|
-
self.bot_id = bot_id
|
|
321
|
-
self.display = display
|
|
322
|
-
self.paths = paths
|
|
323
|
-
self._grabber: Optional[object] = None
|
|
324
|
-
self._input_dpy: Optional[object] = None
|
|
325
|
-
self._lock = threading.Lock()
|
|
326
|
-
self._input_lock = threading.Lock()
|
|
327
|
-
self._last_capture_at: float = 0.0
|
|
328
|
-
|
|
329
|
-
# ---- 环境 -------------------------------------------------------------
|
|
330
|
-
@property
|
|
331
|
-
def env(self) -> dict:
|
|
332
|
-
env = dict(os.environ)
|
|
333
|
-
env["DISPLAY"] = f":{self.display}"
|
|
334
|
-
env["XAUTHORITY"] = SHARED_XAUTHORITY
|
|
335
|
-
env["HOME"] = self.paths.home
|
|
336
|
-
env.pop("WAYLAND_DISPLAY", None)
|
|
337
|
-
return env
|
|
338
|
-
|
|
339
|
-
def _xdo(self, *args: str, timeout: float = 5.0) -> str:
|
|
340
|
-
try:
|
|
341
|
-
proc = subprocess.run(
|
|
342
|
-
["xdotool", *args],
|
|
343
|
-
env=self.env,
|
|
344
|
-
capture_output=True,
|
|
345
|
-
text=True,
|
|
346
|
-
timeout=timeout,
|
|
347
|
-
check=False,
|
|
348
|
-
)
|
|
349
|
-
except subprocess.TimeoutExpired as exc:
|
|
350
|
-
raise DriverError(f"xdotool {' '.join(args)} timed out after {timeout}s") from exc
|
|
351
|
-
if proc.returncode != 0:
|
|
352
|
-
raise DriverError(
|
|
353
|
-
f"xdotool {' '.join(args)} rc={proc.returncode}: {proc.stderr.strip()[:200]}"
|
|
354
|
-
)
|
|
355
|
-
return proc.stdout
|
|
356
|
-
|
|
357
|
-
# ---- 截屏(按需分配,空闲可立即销毁) ----------------------------------
|
|
358
|
-
def _backend(self):
|
|
359
|
-
if self._grabber is None:
|
|
360
|
-
if CAPTURE_BACKEND != "xlib":
|
|
361
|
-
try:
|
|
362
|
-
self._grabber = _XShmGrabber(self.display)
|
|
363
|
-
return self._grabber
|
|
364
|
-
except Exception: # noqa: BLE001
|
|
365
|
-
self._grabber = None
|
|
366
|
-
self._grabber = _XlibGrabber(self.display)
|
|
367
|
-
return self._grabber
|
|
368
|
-
|
|
369
|
-
def close_capture(self) -> None:
|
|
370
|
-
"""销毁抓屏连接与 XShm 缓冲区(空闲休眠时调用)。"""
|
|
371
|
-
with self._lock:
|
|
372
|
-
if self._grabber is not None:
|
|
373
|
-
try:
|
|
374
|
-
self._grabber.close()
|
|
375
|
-
except Exception: # noqa: BLE001
|
|
376
|
-
pass
|
|
377
|
-
self._grabber = None
|
|
378
|
-
|
|
379
|
-
def reset_grabber(self) -> None:
|
|
380
|
-
self.close_capture()
|
|
381
|
-
with self._input_lock:
|
|
382
|
-
if self._input_dpy is not None:
|
|
383
|
-
try:
|
|
384
|
-
self._input_dpy.close()
|
|
385
|
-
except Exception: # noqa: BLE001
|
|
386
|
-
pass
|
|
387
|
-
self._input_dpy = None
|
|
388
|
-
|
|
389
|
-
def grab_raw(self) -> RawFrame:
|
|
390
|
-
with self._lock:
|
|
391
|
-
for attempt in range(2):
|
|
392
|
-
try:
|
|
393
|
-
frame = self._backend().grab_raw()
|
|
394
|
-
self._last_capture_at = time.time()
|
|
395
|
-
return frame
|
|
396
|
-
except Exception:
|
|
397
|
-
if self._grabber is not None:
|
|
398
|
-
try:
|
|
399
|
-
self._grabber.close()
|
|
400
|
-
except Exception: # noqa: BLE001
|
|
401
|
-
pass
|
|
402
|
-
self._grabber = None
|
|
403
|
-
if attempt == 1:
|
|
404
|
-
raise
|
|
405
|
-
raise DriverError("unreachable")
|
|
406
|
-
|
|
407
|
-
def capture(self) -> Image.Image:
|
|
408
|
-
return self.grab_raw().to_image()
|
|
409
|
-
|
|
410
|
-
def screenshot(
|
|
411
|
-
self,
|
|
412
|
-
fmt: str = "jpeg",
|
|
413
|
-
quality: int = 82,
|
|
414
|
-
with_thumb: bool = False,
|
|
415
|
-
) -> Shot:
|
|
416
|
-
img = self.capture()
|
|
417
|
-
buf = io.BytesIO()
|
|
418
|
-
if fmt == "png":
|
|
419
|
-
img.save(buf, format="PNG", compress_level=1)
|
|
420
|
-
mime = "image/png"
|
|
421
|
-
elif fmt == "webp":
|
|
422
|
-
img.save(buf, format="WEBP", quality=quality, method=0)
|
|
423
|
-
mime = "image/webp"
|
|
424
|
-
else:
|
|
425
|
-
img.save(buf, format="JPEG", quality=quality, optimize=False, subsampling=1)
|
|
426
|
-
mime = "image/jpeg"
|
|
427
|
-
thumb = None
|
|
428
|
-
if with_thumb:
|
|
429
|
-
h = max(1, int(THUMB_WIDTH * img.height / img.width))
|
|
430
|
-
tb = io.BytesIO()
|
|
431
|
-
img.resize((THUMB_WIDTH, h), Image.BILINEAR).save(
|
|
432
|
-
tb, format="JPEG", quality=50, optimize=False
|
|
433
|
-
)
|
|
434
|
-
thumb = tb.getvalue()
|
|
435
|
-
return Shot(buf.getvalue(), img.width, img.height, mime, thumb)
|
|
436
|
-
|
|
437
|
-
# ---- 比例坐标换算 + XTest 注入 -----------------------------------------
|
|
438
|
-
@staticmethod
|
|
439
|
-
def _clamp_xy(x: int, y: int, w: int = SCREEN_WIDTH, h: int = SCREEN_HEIGHT) -> Tuple[int, int]:
|
|
440
|
-
return (
|
|
441
|
-
max(0, min(max(1, w) - 1, int(x))),
|
|
442
|
-
max(0, min(max(1, h) - 1, int(y))),
|
|
443
|
-
)
|
|
444
|
-
|
|
445
|
-
def resolve_xy(
|
|
446
|
-
self,
|
|
447
|
-
x: int = 0,
|
|
448
|
-
y: int = 0,
|
|
449
|
-
rx: Optional[float] = None,
|
|
450
|
-
ry: Optional[float] = None,
|
|
451
|
-
) -> Tuple[int, int]:
|
|
452
|
-
"""支持前端传入归一化比例 (rx, ry in [0,1]),直接乘以虚拟屏分辨率。"""
|
|
453
|
-
w, h = SCREEN_WIDTH, SCREEN_HEIGHT
|
|
454
|
-
if rx is not None and ry is not None:
|
|
455
|
-
crx = max(0.0, min(1.0, float(rx)))
|
|
456
|
-
cry = max(0.0, min(1.0, float(ry)))
|
|
457
|
-
return (
|
|
458
|
-
int(round(crx * max(0, w - 1))),
|
|
459
|
-
int(round(cry * max(0, h - 1))),
|
|
460
|
-
)
|
|
461
|
-
return self._clamp_xy(x, y, w, h)
|
|
462
|
-
|
|
463
|
-
def _xtest_display(self):
|
|
464
|
-
from Xlib import display as xdisplay
|
|
465
|
-
|
|
466
|
-
if self._input_dpy is None:
|
|
467
|
-
os.environ.setdefault("XAUTHORITY", SHARED_XAUTHORITY)
|
|
468
|
-
dpy = xdisplay.Display(f":{self.display}")
|
|
469
|
-
if not dpy.has_extension("XTEST"):
|
|
470
|
-
dpy.close()
|
|
471
|
-
return None, None
|
|
472
|
-
self._input_dpy = dpy
|
|
473
|
-
return self._input_dpy, self._input_dpy.screen().root
|
|
474
|
-
|
|
475
|
-
def _with_xtest(self, fn) -> bool:
|
|
476
|
-
with self._input_lock:
|
|
477
|
-
for attempt in range(2):
|
|
478
|
-
try:
|
|
479
|
-
dpy, root = self._xtest_display()
|
|
480
|
-
if dpy is None:
|
|
481
|
-
return False
|
|
482
|
-
if fn(dpy, root) is False:
|
|
483
|
-
return False
|
|
484
|
-
dpy.sync()
|
|
485
|
-
return True
|
|
486
|
-
except Exception: # noqa: BLE001
|
|
487
|
-
if self._input_dpy is not None:
|
|
488
|
-
try:
|
|
489
|
-
self._input_dpy.close()
|
|
490
|
-
except Exception: # noqa: BLE001
|
|
491
|
-
pass
|
|
492
|
-
self._input_dpy = None
|
|
493
|
-
if attempt == 1:
|
|
494
|
-
return False
|
|
495
|
-
return False
|
|
496
|
-
|
|
497
|
-
def _xdo_move(self, x: int, y: int) -> None:
|
|
498
|
-
try:
|
|
499
|
-
self._xdo("mousemove", str(x), str(y), timeout=3)
|
|
500
|
-
out = self._xdo("getmouselocation", "--shell", timeout=3)
|
|
501
|
-
pos = dict(item.split("=", 1) for item in out.split() if "=" in item)
|
|
502
|
-
if (int(pos.get("X", -1)), int(pos.get("Y", -1))) != (x, y):
|
|
503
|
-
self._xdo("mousemove", str(x), str(y), timeout=3)
|
|
504
|
-
except subprocess.TimeoutExpired as exc:
|
|
505
|
-
raise DriverError(f"mousemove {x},{y} timed out") from exc
|
|
506
|
-
|
|
507
|
-
def move(self, x: int, y: int) -> None:
|
|
508
|
-
cx, cy = self._clamp_xy(x, y)
|
|
509
|
-
from Xlib import X
|
|
510
|
-
from Xlib.ext import xtest
|
|
511
|
-
|
|
512
|
-
if self._with_xtest(lambda d, r: xtest.fake_input(d, X.MotionNotify, detail=0, root=r, x=cx, y=cy)):
|
|
513
|
-
return
|
|
514
|
-
with self._input_lock:
|
|
515
|
-
self._xdo_move(cx, cy)
|
|
516
|
-
|
|
517
|
-
def move_path(self, points: Sequence[Sequence[float]], normalized: bool = False) -> None:
|
|
518
|
-
cleaned = []
|
|
519
|
-
for p in points:
|
|
520
|
-
if len(p) >= 2:
|
|
521
|
-
if normalized:
|
|
522
|
-
cleaned.append(self.resolve_xy(rx=p[0], ry=p[1]))
|
|
523
|
-
else:
|
|
524
|
-
cleaned.append(self._clamp_xy(int(p[0]), int(p[1])))
|
|
525
|
-
if not cleaned:
|
|
526
|
-
return
|
|
527
|
-
from Xlib import X
|
|
528
|
-
from Xlib.ext import xtest
|
|
529
|
-
|
|
530
|
-
def _send(dpy, root) -> None:
|
|
531
|
-
for cx, cy in cleaned:
|
|
532
|
-
xtest.fake_input(dpy, X.MotionNotify, detail=0, root=root, x=cx, y=cy)
|
|
533
|
-
|
|
534
|
-
if self._with_xtest(_send):
|
|
535
|
-
return
|
|
536
|
-
with self._input_lock:
|
|
537
|
-
self._xdo_move(*cleaned[-1])
|
|
538
|
-
|
|
539
|
-
def mousedown(self, x: int, y: int, button: int = 1) -> None:
|
|
540
|
-
cx, cy = self._clamp_xy(x, y)
|
|
541
|
-
btn = max(1, min(9, int(button)))
|
|
542
|
-
from Xlib import X
|
|
543
|
-
from Xlib.ext import xtest
|
|
544
|
-
|
|
545
|
-
def _send(dpy, root) -> None:
|
|
546
|
-
xtest.fake_input(dpy, X.MotionNotify, detail=0, root=root, x=cx, y=cy)
|
|
547
|
-
xtest.fake_input(dpy, X.ButtonPress, detail=btn)
|
|
548
|
-
|
|
549
|
-
if self._with_xtest(_send):
|
|
550
|
-
return
|
|
551
|
-
with self._input_lock:
|
|
552
|
-
self._xdo_move(cx, cy)
|
|
553
|
-
self._xdo("mousedown", str(btn))
|
|
554
|
-
|
|
555
|
-
def mouseup(self, x: int, y: int, button: int = 1) -> None:
|
|
556
|
-
cx, cy = self._clamp_xy(x, y)
|
|
557
|
-
btn = max(1, min(9, int(button)))
|
|
558
|
-
from Xlib import X
|
|
559
|
-
from Xlib.ext import xtest
|
|
560
|
-
|
|
561
|
-
def _send(dpy, root) -> None:
|
|
562
|
-
xtest.fake_input(dpy, X.MotionNotify, detail=0, root=root, x=cx, y=cy)
|
|
563
|
-
xtest.fake_input(dpy, X.ButtonRelease, detail=btn)
|
|
564
|
-
|
|
565
|
-
if self._with_xtest(_send):
|
|
566
|
-
return
|
|
567
|
-
with self._input_lock:
|
|
568
|
-
self._xdo_move(cx, cy)
|
|
569
|
-
self._xdo("mouseup", str(btn))
|
|
570
|
-
|
|
571
|
-
def click(self, x: int, y: int, button: int = 1, double: bool = False) -> None:
|
|
572
|
-
cx, cy = self._clamp_xy(x, y)
|
|
573
|
-
btn = max(1, min(9, int(button)))
|
|
574
|
-
from Xlib import X
|
|
575
|
-
from Xlib.ext import xtest
|
|
576
|
-
|
|
577
|
-
def _send(dpy, root) -> None:
|
|
578
|
-
xtest.fake_input(dpy, X.MotionNotify, detail=0, root=root, x=cx, y=cy)
|
|
579
|
-
xtest.fake_input(dpy, X.ButtonPress, detail=btn)
|
|
580
|
-
xtest.fake_input(dpy, X.ButtonRelease, detail=btn)
|
|
581
|
-
if double:
|
|
582
|
-
dpy.sync()
|
|
583
|
-
time.sleep(0.06)
|
|
584
|
-
xtest.fake_input(dpy, X.ButtonPress, detail=btn)
|
|
585
|
-
xtest.fake_input(dpy, X.ButtonRelease, detail=btn)
|
|
586
|
-
|
|
587
|
-
if self._with_xtest(_send):
|
|
588
|
-
return
|
|
589
|
-
with self._input_lock:
|
|
590
|
-
self._xdo_move(cx, cy)
|
|
591
|
-
if double:
|
|
592
|
-
self._xdo("click", "--repeat", "2", "--delay", "80", str(btn))
|
|
593
|
-
else:
|
|
594
|
-
self._xdo("click", str(btn))
|
|
595
|
-
|
|
596
|
-
def scroll(self, x: int, y: int, clicks: int) -> None:
|
|
597
|
-
if clicks == 0:
|
|
598
|
-
return
|
|
599
|
-
cx, cy = self._clamp_xy(x, y)
|
|
600
|
-
btn = 5 if clicks > 0 else 4
|
|
601
|
-
repeat = min(50, abs(int(clicks)))
|
|
602
|
-
from Xlib import X
|
|
603
|
-
from Xlib.ext import xtest
|
|
604
|
-
|
|
605
|
-
def _send(dpy, root) -> None:
|
|
606
|
-
xtest.fake_input(dpy, X.MotionNotify, detail=0, root=root, x=cx, y=cy)
|
|
607
|
-
for _ in range(repeat):
|
|
608
|
-
xtest.fake_input(dpy, X.ButtonPress, detail=btn)
|
|
609
|
-
xtest.fake_input(dpy, X.ButtonRelease, detail=btn)
|
|
610
|
-
|
|
611
|
-
if self._with_xtest(_send):
|
|
612
|
-
return
|
|
613
|
-
with self._input_lock:
|
|
614
|
-
self._xdo_move(cx, cy)
|
|
615
|
-
self._xdo("click", "--repeat", str(repeat), "--delay", "40", str(btn))
|
|
616
|
-
|
|
617
|
-
def drag(self, x1: int, y1: int, x2: int, y2: int, button: int = 1) -> None:
|
|
618
|
-
sx, sy = self._clamp_xy(x1, y1)
|
|
619
|
-
ex, ey = self._clamp_xy(x2, y2)
|
|
620
|
-
btn = max(1, min(9, int(button)))
|
|
621
|
-
steps = max(1, min(20, max(abs(ex - sx), abs(ey - sy)) // 16))
|
|
622
|
-
pts = [
|
|
623
|
-
(
|
|
624
|
-
int(round(sx + (ex - sx) * i / steps)),
|
|
625
|
-
int(round(sy + (ey - sy) * i / steps)),
|
|
626
|
-
)
|
|
627
|
-
for i in range(1, steps + 1)
|
|
628
|
-
]
|
|
629
|
-
from Xlib import X
|
|
630
|
-
from Xlib.ext import xtest
|
|
631
|
-
|
|
632
|
-
def _send(dpy, root) -> None:
|
|
633
|
-
xtest.fake_input(dpy, X.MotionNotify, detail=0, root=root, x=sx, y=sy)
|
|
634
|
-
xtest.fake_input(dpy, X.ButtonPress, detail=btn)
|
|
635
|
-
for px, py in pts:
|
|
636
|
-
xtest.fake_input(dpy, X.MotionNotify, detail=0, root=root, x=px, y=py)
|
|
637
|
-
xtest.fake_input(dpy, X.ButtonRelease, detail=btn)
|
|
638
|
-
|
|
639
|
-
if self._with_xtest(_send):
|
|
640
|
-
return
|
|
641
|
-
with self._input_lock:
|
|
642
|
-
self._xdo_move(sx, sy)
|
|
643
|
-
self._xdo("mousedown", str(btn))
|
|
644
|
-
for px, py in pts:
|
|
645
|
-
self._xdo_move(px, py)
|
|
646
|
-
self._xdo("mouseup", str(btn))
|
|
647
|
-
|
|
648
|
-
@staticmethod
|
|
649
|
-
def _resolve_keycode(dpy, token: str) -> Tuple[int, bool]:
|
|
650
|
-
from Xlib import XK
|
|
651
|
-
|
|
652
|
-
sym_name = _SPECIAL_KEYSYMS.get(token.lower(), token)
|
|
653
|
-
keysym = XK.string_to_keysym(sym_name)
|
|
654
|
-
if not keysym and len(token) == 1 and 0x20 <= ord(token) <= 0x7E:
|
|
655
|
-
keysym = ord(token)
|
|
656
|
-
if not keysym:
|
|
657
|
-
return 0, False
|
|
658
|
-
kc = dpy.keysym_to_keycode(keysym)
|
|
659
|
-
if not kc:
|
|
660
|
-
return 0, False
|
|
661
|
-
need_shift = (
|
|
662
|
-
dpy.keycode_to_keysym(kc, 0) != keysym
|
|
663
|
-
and dpy.keycode_to_keysym(kc, 1) == keysym
|
|
664
|
-
)
|
|
665
|
-
return int(kc), need_shift
|
|
666
|
-
|
|
667
|
-
def key(self, combo: str) -> None:
|
|
668
|
-
parts = [p.strip() for p in combo.split("+") if p.strip()]
|
|
669
|
-
if not parts:
|
|
670
|
-
return
|
|
671
|
-
from Xlib import X, XK
|
|
672
|
-
from Xlib.ext import xtest
|
|
673
|
-
|
|
674
|
-
def _send(dpy, _root) -> bool:
|
|
675
|
-
mod_kcs: list[int] = []
|
|
676
|
-
for mod in parts[:-1]:
|
|
677
|
-
mod_sym_name = _MODIFIER_KEYSYMS.get(mod.lower())
|
|
678
|
-
if not mod_sym_name:
|
|
679
|
-
return False
|
|
680
|
-
mkc = dpy.keysym_to_keycode(XK.string_to_keysym(mod_sym_name))
|
|
681
|
-
if not mkc:
|
|
682
|
-
return False
|
|
683
|
-
mod_kcs.append(int(mkc))
|
|
684
|
-
kc, need_shift = self._resolve_keycode(dpy, parts[-1])
|
|
685
|
-
if not kc:
|
|
686
|
-
return False
|
|
687
|
-
if need_shift:
|
|
688
|
-
skc = dpy.keysym_to_keycode(XK.string_to_keysym("Shift_L"))
|
|
689
|
-
if skc and int(skc) not in mod_kcs:
|
|
690
|
-
mod_kcs.append(int(skc))
|
|
691
|
-
for mkc in mod_kcs:
|
|
692
|
-
xtest.fake_input(dpy, X.KeyPress, detail=mkc)
|
|
693
|
-
xtest.fake_input(dpy, X.KeyPress, detail=kc)
|
|
694
|
-
xtest.fake_input(dpy, X.KeyRelease, detail=kc)
|
|
695
|
-
for mkc in reversed(mod_kcs):
|
|
696
|
-
xtest.fake_input(dpy, X.KeyRelease, detail=mkc)
|
|
697
|
-
return True
|
|
698
|
-
|
|
699
|
-
if self._with_xtest(_send):
|
|
700
|
-
return
|
|
701
|
-
with self._input_lock:
|
|
702
|
-
self._xdo("key", "--clearmodifiers", combo)
|
|
703
|
-
|
|
704
|
-
def _type_ascii_xtest(self, text: str, press_enter: bool) -> bool:
|
|
705
|
-
from Xlib import X, XK
|
|
706
|
-
from Xlib.ext import xtest
|
|
707
|
-
|
|
708
|
-
def _send(dpy, _root) -> bool:
|
|
709
|
-
shift_kc = int(dpy.keysym_to_keycode(XK.string_to_keysym("Shift_L")) or 0)
|
|
710
|
-
ops: list[Tuple[int, bool]] = []
|
|
711
|
-
for ch in text:
|
|
712
|
-
if ch == "\n":
|
|
713
|
-
kc, sh = self._resolve_keycode(dpy, "Return")
|
|
714
|
-
elif ch == "\t":
|
|
715
|
-
kc, sh = self._resolve_keycode(dpy, "Tab")
|
|
716
|
-
else:
|
|
717
|
-
kc, sh = self._resolve_keycode(dpy, ch)
|
|
718
|
-
if not kc or (sh and not shift_kc):
|
|
719
|
-
return False
|
|
720
|
-
ops.append((kc, sh))
|
|
721
|
-
if press_enter:
|
|
722
|
-
rkc, _ = self._resolve_keycode(dpy, "Return")
|
|
723
|
-
if not rkc:
|
|
724
|
-
return False
|
|
725
|
-
ops.append((rkc, False))
|
|
726
|
-
for kc, sh in ops:
|
|
727
|
-
if sh:
|
|
728
|
-
xtest.fake_input(dpy, X.KeyPress, detail=shift_kc)
|
|
729
|
-
xtest.fake_input(dpy, X.KeyPress, detail=kc)
|
|
730
|
-
xtest.fake_input(dpy, X.KeyRelease, detail=kc)
|
|
731
|
-
if sh:
|
|
732
|
-
xtest.fake_input(dpy, X.KeyRelease, detail=shift_kc)
|
|
733
|
-
return True
|
|
734
|
-
|
|
735
|
-
return self._with_xtest(_send)
|
|
736
|
-
|
|
737
|
-
def type_text(
|
|
738
|
-
self,
|
|
739
|
-
text: str,
|
|
740
|
-
press_enter: bool = False,
|
|
741
|
-
paste_key: str = "ctrl+v",
|
|
742
|
-
also_primary: bool = True,
|
|
743
|
-
) -> None:
|
|
744
|
-
"""短纯 ASCII 直接走 XTest 扫描码注入;中文/Emoji/长文本走剪贴板粘贴通道。"""
|
|
745
|
-
if not text:
|
|
746
|
-
if press_enter:
|
|
747
|
-
self.key("Return")
|
|
748
|
-
return
|
|
749
|
-
|
|
750
|
-
if text.isascii() and len(text) <= 64 and self._type_ascii_xtest(text, press_enter):
|
|
751
|
-
return
|
|
752
|
-
|
|
753
|
-
selections = ["clipboard"] + (["primary"] if also_primary else [])
|
|
754
|
-
holders: list[subprocess.Popen] = []
|
|
755
|
-
try:
|
|
756
|
-
for selection in selections:
|
|
757
|
-
holder = subprocess.Popen(
|
|
758
|
-
["xclip", "-selection", selection, "-i", "-loops", "0"],
|
|
759
|
-
env=self.env,
|
|
760
|
-
stdin=subprocess.PIPE,
|
|
761
|
-
text=True,
|
|
762
|
-
)
|
|
763
|
-
holders.append(holder)
|
|
764
|
-
assert holder.stdin is not None
|
|
765
|
-
holder.stdin.write(text)
|
|
766
|
-
holder.stdin.flush()
|
|
767
|
-
holder.stdin.close()
|
|
768
|
-
time.sleep(0.09)
|
|
769
|
-
self.key(paste_key)
|
|
770
|
-
time.sleep(0.12)
|
|
771
|
-
if press_enter:
|
|
772
|
-
self.key("Return")
|
|
773
|
-
finally:
|
|
774
|
-
for holder in holders:
|
|
775
|
-
try:
|
|
776
|
-
holder.terminate()
|
|
777
|
-
holder.wait(timeout=1)
|
|
778
|
-
except Exception: # noqa: BLE001
|
|
779
|
-
try:
|
|
780
|
-
holder.kill()
|
|
781
|
-
except Exception: # noqa: BLE001
|
|
782
|
-
pass
|
|
783
|
-
|
|
784
|
-
def focused_window(self) -> dict:
|
|
785
|
-
try:
|
|
786
|
-
title = self._xdo("getactivewindow", "getwindowname", timeout=3).strip()
|
|
787
|
-
geom = self._xdo("getactivewindow", "getwindowgeometry", "--shell", timeout=3).strip()
|
|
788
|
-
wid = ""
|
|
789
|
-
for line in geom.splitlines():
|
|
790
|
-
if line.startswith("WINDOW="):
|
|
791
|
-
wid = line.split("=", 1)[1]
|
|
792
|
-
return {
|
|
793
|
-
"window_id": wid or None,
|
|
794
|
-
"title": title,
|
|
795
|
-
"x": dict(l.split("=", 1) for l in geom.splitlines() if "=" in l) if geom else {},
|
|
796
|
-
}
|
|
797
|
-
except (DriverError, ValueError):
|
|
798
|
-
return {"window_id": None, "title": None}
|
|
799
|
-
|
|
800
|
-
def list_windows(self) -> list[dict]:
|
|
801
|
-
try:
|
|
802
|
-
raw = self._xdo("search", "--onlyvisible", "--name", "", timeout=3)
|
|
803
|
-
except DriverError:
|
|
804
|
-
return []
|
|
805
|
-
windows = []
|
|
806
|
-
for wid in raw.split():
|
|
807
|
-
wid = wid.strip()
|
|
808
|
-
if not wid:
|
|
809
|
-
continue
|
|
810
|
-
try:
|
|
811
|
-
title = self._xdo("getwindowname", wid, timeout=2).strip()
|
|
812
|
-
except DriverError:
|
|
813
|
-
title = ""
|
|
814
|
-
if title:
|
|
815
|
-
windows.append({"window_id": wid, "title": title})
|
|
816
|
-
return windows[:30]
|
|
817
|
-
|
|
818
|
-
def focus_window(self, window_id: str) -> None:
|
|
819
|
-
self._xdo("windowactivate", "--sync", str(window_id), timeout=3)
|
|
820
|
-
|
|
821
|
-
@staticmethod
|
|
822
|
-
def map_coords(
|
|
823
|
-
x: float,
|
|
824
|
-
y: float,
|
|
825
|
-
from_size: Tuple[int, int],
|
|
826
|
-
to_size: Tuple[int, int] = (SCREEN_WIDTH, SCREEN_HEIGHT),
|
|
827
|
-
) -> Tuple[int, int]:
|
|
828
|
-
fw, fh = from_size
|
|
829
|
-
tw, th = to_size
|
|
830
|
-
if fw <= 0 or fh <= 0:
|
|
831
|
-
return int(x), int(y)
|
|
832
|
-
return int(round(x * tw / fw)), int(round(y * th / fh))
|