amigent 1.0.0__py3-none-any.whl
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.
- amigent/__init__.py +3 -0
- amigent/amiga/amigent-repair +65 -0
- amigent/amiga/amigent-repair.info +0 -0
- amigent/amiga/amigent-scan +9 -0
- amigent/amiga/bin/Amigent.info +0 -0
- amigent/amiga/bin/amigentd +0 -0
- amigent/amiga/bin/amigentd.000 +0 -0
- amigent/amiga/bin/amigentd.info +0 -0
- amigent/amiga/menu-add.rexx +45 -0
- amigent/amigactl.py +199 -0
- amigent/amigentd.py +466 -0
- amigent/channel.py +411 -0
- amigent/client.py +26 -0
- amigent/desktop.py +308 -0
- amigent/doctor.py +70 -0
- amigent/errors.py +11 -0
- amigent/files.py +236 -0
- amigent/fleet.py +252 -0
- amigent/inventory.py +37 -0
- amigent/legacy.py +90 -0
- amigent/locks.py +44 -0
- amigent/lzh.py +175 -0
- amigent/machines.py +126 -0
- amigent/macos/Amigent.swift +76 -0
- amigent/macos/Amigent.zip +0 -0
- amigent/net.py +29 -0
- amigent/paths.py +18 -0
- amigent/pictures.py +104 -0
- amigent/platform_util.py +22 -0
- amigent/power.py +66 -0
- amigent/roster.py +163 -0
- amigent/secret.py +73 -0
- amigent/serve.py +84 -0
- amigent/server.py +723 -0
- amigent/setup.py +177 -0
- amigent/tls.py +150 -0
- amigent/transfer.py +64 -0
- amigent/viewer.py +911 -0
- amigent/watch.py +71 -0
- amigent/web/amigent.icns +0 -0
- amigent/web/amigent.ico +0 -0
- amigent/web/fleet.css +22 -0
- amigent/web/fleet.html +29 -0
- amigent/web/fleet.js +158 -0
- amigent/web/icon.png +0 -0
- amigent/web/icon.svg +15 -0
- amigent/web/index.html +246 -0
- amigent/web/locales/cs.json +129 -0
- amigent/web/locales/da.json +129 -0
- amigent/web/locales/de.json +129 -0
- amigent/web/locales/en.json +129 -0
- amigent/web/locales/es.json +129 -0
- amigent/web/locales/fi.json +129 -0
- amigent/web/locales/fr.json +129 -0
- amigent/web/locales/hu.json +129 -0
- amigent/web/locales/it.json +129 -0
- amigent/web/locales/pl.json +129 -0
- amigent/web/locales/sk.json +129 -0
- amigent/web/locales/sv.json +129 -0
- amigent/web/viewer.css +448 -0
- amigent/web/viewer.js +939 -0
- amigent-1.0.0.dist-info/METADATA +138 -0
- amigent-1.0.0.dist-info/RECORD +67 -0
- amigent-1.0.0.dist-info/WHEEL +5 -0
- amigent-1.0.0.dist-info/entry_points.txt +6 -0
- amigent-1.0.0.dist-info/licenses/LICENSE +21 -0
- amigent-1.0.0.dist-info/top_level.txt +1 -0
amigent/amigentd.py
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
"""Client for amigentd: screen tiles and input. See docs/PROTOCOL.md."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import random
|
|
6
|
+
import socket
|
|
7
|
+
import struct
|
|
8
|
+
import time
|
|
9
|
+
from collections import deque
|
|
10
|
+
|
|
11
|
+
from PIL import Image
|
|
12
|
+
|
|
13
|
+
from . import tls
|
|
14
|
+
from .secret import load_key
|
|
15
|
+
|
|
16
|
+
CMD_INFO, CMD_FRAME, CMD_MOVE, CMD_BUTTON, CMD_RAWKEY, CMD_PING, CMD_CHAR = range(1, 8)
|
|
17
|
+
CMD_PUT, CMD_RUN, CMD_SETUP_END = 8, 9, 10
|
|
18
|
+
CMD_SCREENS, CMD_SCREEN_FRONT, CMD_FRAME_RGB, CMD_MOVE_REL, CMD_UITREE, CMD_MENUS = range(11, 17)
|
|
19
|
+
CMD_AUTH, CMD_CLIENT = 17, 18
|
|
20
|
+
RGB_AREA = 512 * 512 # FRAME_RGB answers regions of up to this many pixels
|
|
21
|
+
|
|
22
|
+
RAWKEYS = {
|
|
23
|
+
"space": 0x40, "backspace": 0x41, "tab": 0x42, "enter": 0x43, "return": 0x44,
|
|
24
|
+
"esc": 0x45, "escape": 0x45, "delete": 0x46, "up": 0x4C, "down": 0x4D,
|
|
25
|
+
"right": 0x4E, "left": 0x4F, "help": 0x5F,
|
|
26
|
+
**{f"f{i}": 0x50 + i - 1 for i in range(1, 11)},
|
|
27
|
+
}
|
|
28
|
+
QUALIFIERS = {
|
|
29
|
+
"shift": 0x01, "ctrl": 0x08, "alt": 0x10, "lamiga": 0x40, "ramiga": 0x80,
|
|
30
|
+
}
|
|
31
|
+
BUTTONS = {"left": 1, "right": 2, "middle": 4}
|
|
32
|
+
|
|
33
|
+
_LUT = {
|
|
34
|
+
"r": bytes((v & 7) * 255 // 7 for v in range(256)),
|
|
35
|
+
"g": bytes((v >> 3 & 7) * 255 // 7 for v in range(256)),
|
|
36
|
+
"b": bytes((v >> 6 & 3) * 255 // 3 for v in range(256)),
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class AmigentdError(Exception):
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class AmigentdRefused(AmigentdError):
|
|
45
|
+
"""amigentd answered, but said no: the link is fine, so nothing is retried."""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def decode_tile(data: bytes, w: int, h: int) -> Image.Image:
|
|
49
|
+
size = w * h
|
|
50
|
+
if len(data) != size or size == 0:
|
|
51
|
+
out = bytearray()
|
|
52
|
+
for i in range(0, len(data) - 1, 2):
|
|
53
|
+
out += bytes([data[i + 1]]) * (data[i] + 1)
|
|
54
|
+
data = bytes(out)
|
|
55
|
+
if len(data) != size:
|
|
56
|
+
raise AmigentdError(f"tile decoded to {len(data)} bytes, expected {size}")
|
|
57
|
+
bands = [Image.frombytes("L", (w, h), data.translate(_LUT[c])) for c in "rgb"]
|
|
58
|
+
return Image.merge("RGB", bands)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def unpack_rgb(data: bytes, pixels: int) -> bytes:
|
|
62
|
+
"""FRAME_RGB arg 1: runs of (count-1, r, g, b), or raw RGB when that was shorter."""
|
|
63
|
+
if len(data) == pixels * 3:
|
|
64
|
+
return data
|
|
65
|
+
out = bytearray()
|
|
66
|
+
for i in range(0, len(data) - 3, 4):
|
|
67
|
+
out += data[i + 1:i + 4] * (data[i] + 1)
|
|
68
|
+
if len(out) != pixels * 3:
|
|
69
|
+
raise AmigentdError(f"colour region decoded to {len(out)} bytes, expected {pixels * 3}")
|
|
70
|
+
return bytes(out)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Amigentd:
|
|
74
|
+
def __init__(self, host: str, port: int = 6801, timeout: float = 30.0,
|
|
75
|
+
key: str | None = None):
|
|
76
|
+
self.host = host
|
|
77
|
+
self.key = key
|
|
78
|
+
self.client_id = random.getrandbits(32) # lets amigentd tell us if others took frames
|
|
79
|
+
self.changes = deque(maxlen=4096) # (time, x, y, w, h) of tiles received, by anyone
|
|
80
|
+
self.port = port
|
|
81
|
+
self.timeout = timeout
|
|
82
|
+
self._sock: socket.socket | None = None
|
|
83
|
+
self.width = 0
|
|
84
|
+
self.height = 0
|
|
85
|
+
self._frame: Image.Image | None = None
|
|
86
|
+
self._buttons = 0
|
|
87
|
+
self._last_used = 0.0
|
|
88
|
+
self.protocol = 0
|
|
89
|
+
self.flags = 0
|
|
90
|
+
self.listeners: list = [] # called as listener("size", (w, h)) or ("tiles", [...])
|
|
91
|
+
|
|
92
|
+
def connect(self) -> None:
|
|
93
|
+
self.close()
|
|
94
|
+
try:
|
|
95
|
+
self._sock = tls.connect(self.host, self.port, 5) # TLS when a certificate is pinned
|
|
96
|
+
self._sock.settimeout(self.timeout)
|
|
97
|
+
self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
|
98
|
+
hello = self._exact(8)
|
|
99
|
+
if hello[:4] != b"AMGT":
|
|
100
|
+
raise AmigentdError(f"unexpected greeting {hello!r}")
|
|
101
|
+
self.protocol, self.flags = struct.unpack(">HH", hello[4:8])
|
|
102
|
+
if self.flags & 1: # bit 0: a key is required, bit 1: TLS on the next port
|
|
103
|
+
self._authenticate()
|
|
104
|
+
if self.protocol >= 2:
|
|
105
|
+
self._send(CMD_CLIENT, 0, self.client_id >> 16, self.client_id & 0xFFFF)
|
|
106
|
+
if self._exact(1) != b"\0":
|
|
107
|
+
self._frame = None # frames went to someone else meanwhile
|
|
108
|
+
size = self._info()
|
|
109
|
+
except OSError as e:
|
|
110
|
+
self.close()
|
|
111
|
+
raise AmigentdError(f"amigentd at {self.host}:{self.port} not answering: {e}") from e
|
|
112
|
+
except AmigentdError:
|
|
113
|
+
self.close()
|
|
114
|
+
raise
|
|
115
|
+
self._apply_size(*size)
|
|
116
|
+
|
|
117
|
+
def _authenticate(self) -> None:
|
|
118
|
+
if self.key is None:
|
|
119
|
+
self.key = load_key(self.host) # read late: setup may have created it meanwhile
|
|
120
|
+
if not self.key:
|
|
121
|
+
raise AmigentdError("amigentd wants a key: run amigent-setup, or set AMIGENT_KEY")
|
|
122
|
+
raw = self.key.encode()
|
|
123
|
+
self._send(CMD_AUTH, 0, len(raw))
|
|
124
|
+
assert self._sock
|
|
125
|
+
self._sock.sendall(raw)
|
|
126
|
+
if self._exact(1) != b"\0":
|
|
127
|
+
raise AmigentdError("amigentd refused the key; AMIGENT_KEY or the key in ~/.config/amigent/ "
|
|
128
|
+
"must match S:amigent.key on the Amiga")
|
|
129
|
+
|
|
130
|
+
def _info(self) -> tuple[int, int]:
|
|
131
|
+
self._send(CMD_INFO)
|
|
132
|
+
return struct.unpack(">HH", self._exact(8)[:4])
|
|
133
|
+
|
|
134
|
+
def _apply_size(self, width: int, height: int) -> None:
|
|
135
|
+
if (width, height) != (self.width, self.height):
|
|
136
|
+
self._frame = None
|
|
137
|
+
self.width, self.height = width, height
|
|
138
|
+
self._notify("size", (width, height))
|
|
139
|
+
|
|
140
|
+
def _notify(self, event: str, payload) -> None:
|
|
141
|
+
for listener in self.listeners:
|
|
142
|
+
try:
|
|
143
|
+
listener(event, payload)
|
|
144
|
+
except Exception:
|
|
145
|
+
pass # a broken viewer must not break the client
|
|
146
|
+
|
|
147
|
+
def check_size(self) -> None:
|
|
148
|
+
"""Ask for the front screen's size again; a new size starts a fresh picture."""
|
|
149
|
+
self._apply_size(*self._retry(self._info))
|
|
150
|
+
|
|
151
|
+
def close(self) -> None:
|
|
152
|
+
if self._sock:
|
|
153
|
+
self._sock.close()
|
|
154
|
+
self._sock = None
|
|
155
|
+
|
|
156
|
+
def _exact(self, n: int) -> bytes:
|
|
157
|
+
assert self._sock
|
|
158
|
+
data = bytearray()
|
|
159
|
+
while len(data) < n:
|
|
160
|
+
chunk = self._sock.recv(min(n - len(data), 65536))
|
|
161
|
+
if not chunk:
|
|
162
|
+
raise AmigentdError("connection closed by amigentd")
|
|
163
|
+
data += chunk
|
|
164
|
+
return bytes(data)
|
|
165
|
+
|
|
166
|
+
def _send(self, cmd: int, arg: int = 0, a: int = 0, b: int = 0, c: int = 0, d: int = 0) -> None:
|
|
167
|
+
assert self._sock
|
|
168
|
+
self._sock.sendall(struct.pack(">BBHHHH", cmd, arg, a, b, c, d))
|
|
169
|
+
|
|
170
|
+
def _retry(self, fn):
|
|
171
|
+
"""Run fn, reconnecting once: the service may have dropped an idle link."""
|
|
172
|
+
if not self._sock:
|
|
173
|
+
self.connect()
|
|
174
|
+
try:
|
|
175
|
+
result = fn()
|
|
176
|
+
except AmigentdRefused:
|
|
177
|
+
raise
|
|
178
|
+
except (OSError, AmigentdError, AssertionError):
|
|
179
|
+
if self.protocol < 2 and time.monotonic() - self._last_used < 100:
|
|
180
|
+
# Old amigentd: not the idle timeout, so another client took over
|
|
181
|
+
# and may have received tiles our picture is missing.
|
|
182
|
+
self._frame = None
|
|
183
|
+
self.connect()
|
|
184
|
+
result = fn()
|
|
185
|
+
self._last_used = time.monotonic()
|
|
186
|
+
return result
|
|
187
|
+
|
|
188
|
+
def _ok(self, *args) -> None:
|
|
189
|
+
def call():
|
|
190
|
+
self._send(*args)
|
|
191
|
+
if self._exact(1) != b"\0":
|
|
192
|
+
raise AmigentdRefused(f"command {args[0]} refused")
|
|
193
|
+
|
|
194
|
+
self._retry(call)
|
|
195
|
+
|
|
196
|
+
def ping(self) -> None:
|
|
197
|
+
self._ok(CMD_PING)
|
|
198
|
+
|
|
199
|
+
def _once(self, fn):
|
|
200
|
+
"""For requests that must not be repeated: a broken link closes the socket."""
|
|
201
|
+
if not self._sock:
|
|
202
|
+
self.connect()
|
|
203
|
+
try:
|
|
204
|
+
return fn()
|
|
205
|
+
except (OSError, AmigentdError):
|
|
206
|
+
self.close() # a late reply must not be taken for the next command's
|
|
207
|
+
raise
|
|
208
|
+
|
|
209
|
+
def put(self, path: str, data: bytes) -> None:
|
|
210
|
+
"""Store a file; needs amigentd started with SETUP."""
|
|
211
|
+
raw = path.encode("iso-8859-1")
|
|
212
|
+
|
|
213
|
+
def call():
|
|
214
|
+
self._send(CMD_PUT, 0, len(raw), 0, len(data) >> 16, len(data) & 0xFFFF)
|
|
215
|
+
self._sock.sendall(raw + data)
|
|
216
|
+
return self._exact(1)
|
|
217
|
+
|
|
218
|
+
if self._once(call) != b"\0":
|
|
219
|
+
raise AmigentdRefused(f"could not write {path}")
|
|
220
|
+
|
|
221
|
+
def run(self, command: str) -> int:
|
|
222
|
+
"""Run a command line and return its code; needs SETUP."""
|
|
223
|
+
raw = command.encode("iso-8859-1")
|
|
224
|
+
|
|
225
|
+
def call():
|
|
226
|
+
self._send(CMD_RUN, 0, len(raw))
|
|
227
|
+
self._sock.sendall(raw)
|
|
228
|
+
return self._exact(1)[0]
|
|
229
|
+
|
|
230
|
+
return self._once(call)
|
|
231
|
+
|
|
232
|
+
def end_setup(self) -> None:
|
|
233
|
+
self._ok(CMD_SETUP_END)
|
|
234
|
+
|
|
235
|
+
def poll(self, x: int = 0, y: int = 0, w: int | None = None, h: int | None = None) -> int:
|
|
236
|
+
"""Fetch the changed tiles of a region into the picture; returns how many.
|
|
237
|
+
|
|
238
|
+
Listeners get the tiles as (x, y, w, h, data) with data as sent.
|
|
239
|
+
"""
|
|
240
|
+
|
|
241
|
+
def call():
|
|
242
|
+
region = (x, y, w or self.width, h or self.height)
|
|
243
|
+
force = self._frame is None
|
|
244
|
+
if force:
|
|
245
|
+
self._frame = Image.new("RGB", (self.width, self.height))
|
|
246
|
+
region = (0, 0, self.width, self.height) # a new picture needs everything
|
|
247
|
+
try:
|
|
248
|
+
self._send(CMD_FRAME, int(force), *region)
|
|
249
|
+
tiles = []
|
|
250
|
+
for _ in range(struct.unpack(">I", self._exact(4))[0]):
|
|
251
|
+
tx, ty, tw, th, size = struct.unpack(">HHHHI", self._exact(12))
|
|
252
|
+
data = self._exact(size)
|
|
253
|
+
self._frame.paste(decode_tile(data, tw, th), (tx, ty))
|
|
254
|
+
tiles.append((tx, ty, tw, th, data))
|
|
255
|
+
except (OSError, AmigentdError):
|
|
256
|
+
self._frame = None # the service has moved on, ours is incomplete
|
|
257
|
+
raise
|
|
258
|
+
if tiles:
|
|
259
|
+
now = time.monotonic()
|
|
260
|
+
self.changes.extend((now, t[0], t[1], t[2], t[3]) for t in tiles)
|
|
261
|
+
self._notify("tiles", tiles)
|
|
262
|
+
return len(tiles)
|
|
263
|
+
|
|
264
|
+
return self._retry(call)
|
|
265
|
+
|
|
266
|
+
def capture(self, x: int = 0, y: int = 0, w: int | None = None,
|
|
267
|
+
h: int | None = None) -> Image.Image:
|
|
268
|
+
"""Return a region, transferring only tiles changed since the last call."""
|
|
269
|
+
self._retry(lambda: self._apply_size(*self._info())) # the front screen may have changed
|
|
270
|
+
if self._frame is None:
|
|
271
|
+
self.poll()
|
|
272
|
+
else:
|
|
273
|
+
self.poll(x, y, w, h) # tiles elsewhere wait in amigentd for later
|
|
274
|
+
return self._frame.crop((x, y, x + (w or self.width - x), y + (h or self.height - y)))
|
|
275
|
+
|
|
276
|
+
def snapshot(self) -> Image.Image | None:
|
|
277
|
+
"""The picture as of the last poll, without asking the Amiga."""
|
|
278
|
+
return self._frame
|
|
279
|
+
|
|
280
|
+
def move(self, x: int, y: int) -> None:
|
|
281
|
+
self._ok(CMD_MOVE, 0, x, y)
|
|
282
|
+
|
|
283
|
+
def button(self, name: str, down: bool) -> None:
|
|
284
|
+
mask = BUTTONS[name]
|
|
285
|
+
self.set_buttons(self._buttons | mask if down else self._buttons & ~mask)
|
|
286
|
+
|
|
287
|
+
def set_buttons(self, mask: int) -> None:
|
|
288
|
+
self._buttons = mask & 7
|
|
289
|
+
self._ok(CMD_BUTTON, self._buttons)
|
|
290
|
+
|
|
291
|
+
def rawkey(self, code: int, down: bool, qualifier: int = 0) -> None:
|
|
292
|
+
self._ok(CMD_RAWKEY, int(down), code & 0x7F, qualifier)
|
|
293
|
+
|
|
294
|
+
def char(self, ch: str, qualifier: int = 0) -> bool:
|
|
295
|
+
"""Type one character through the Amiga's keymap; False if it has no key for it."""
|
|
296
|
+
if ord(ch) >= 256:
|
|
297
|
+
return False
|
|
298
|
+
try:
|
|
299
|
+
self._ok(CMD_CHAR, 0, ord(ch), qualifier)
|
|
300
|
+
except AmigentdRefused:
|
|
301
|
+
return False
|
|
302
|
+
return True
|
|
303
|
+
|
|
304
|
+
def click(self, x: int, y: int, button: str = "left", double: bool = False) -> None:
|
|
305
|
+
self.move(x, y)
|
|
306
|
+
time.sleep(0.05)
|
|
307
|
+
for _ in range(2 if double else 1):
|
|
308
|
+
self.button(button, True)
|
|
309
|
+
time.sleep(0.05)
|
|
310
|
+
self.button(button, False)
|
|
311
|
+
time.sleep(0.05)
|
|
312
|
+
|
|
313
|
+
def drag(self, x1: int, y1: int, x2: int, y2: int, button: str = "left") -> None:
|
|
314
|
+
self.move(x1, y1)
|
|
315
|
+
time.sleep(0.05)
|
|
316
|
+
self.button(button, True)
|
|
317
|
+
time.sleep(0.05)
|
|
318
|
+
self.move(x2, y2)
|
|
319
|
+
time.sleep(0.05)
|
|
320
|
+
self.button(button, False)
|
|
321
|
+
|
|
322
|
+
# protocol 2: screens, true colour, relative mouse, the GUI as data
|
|
323
|
+
|
|
324
|
+
def _need(self, version: int) -> None:
|
|
325
|
+
if not self._sock:
|
|
326
|
+
self._retry(lambda: None) # connect, once more if another client cut in
|
|
327
|
+
if self.protocol < version:
|
|
328
|
+
raise AmigentdError(f"amigentd on the Amiga speaks protocol {self.protocol}, this needs "
|
|
329
|
+
f"{version}; install the current amigentd")
|
|
330
|
+
|
|
331
|
+
def _text(self, cmd: int) -> list[list[str]]:
|
|
332
|
+
def call():
|
|
333
|
+
self._send(cmd)
|
|
334
|
+
size = struct.unpack(">I", self._exact(4))[0]
|
|
335
|
+
return self._exact(size).decode("iso-8859-1")
|
|
336
|
+
|
|
337
|
+
self._need(2)
|
|
338
|
+
return [line.split("\t") for line in self._retry(call).splitlines() if line]
|
|
339
|
+
|
|
340
|
+
def screens(self) -> list[dict]:
|
|
341
|
+
"""All Intuition screens, front to back."""
|
|
342
|
+
def call():
|
|
343
|
+
self._send(CMD_SCREENS)
|
|
344
|
+
found = []
|
|
345
|
+
for _ in range(struct.unpack(">H", self._exact(2))[0]):
|
|
346
|
+
handle, w, h, depth, flags, size = struct.unpack(">IHHHHH", self._exact(14))
|
|
347
|
+
found.append({"handle": handle, "width": w, "height": h, "depth": depth,
|
|
348
|
+
"front": bool(flags & 1), "workbench": bool(flags & 2),
|
|
349
|
+
"rtg": bool(flags & 4), "ham": bool(flags & 8),
|
|
350
|
+
"title": self._exact(size).decode("iso-8859-1")})
|
|
351
|
+
return found
|
|
352
|
+
|
|
353
|
+
self._need(2)
|
|
354
|
+
return self._retry(call)
|
|
355
|
+
|
|
356
|
+
def screen_to_front(self, handle: int) -> bool:
|
|
357
|
+
self._need(2)
|
|
358
|
+
|
|
359
|
+
def call():
|
|
360
|
+
self._send(CMD_SCREEN_FRONT, 0, handle >> 16 & 0xFFFF, handle & 0xFFFF)
|
|
361
|
+
return self._exact(1) == b"\0"
|
|
362
|
+
|
|
363
|
+
return self._retry(call)
|
|
364
|
+
|
|
365
|
+
def capture_rgb(self, x: int, y: int, w: int, h: int) -> Image.Image:
|
|
366
|
+
"""A region in full colour, fetched packed in pieces of at most RGB_AREA pixels."""
|
|
367
|
+
self._need(2)
|
|
368
|
+
picture = Image.new("RGB", (w, h))
|
|
369
|
+
piece_w = min(w, RGB_AREA)
|
|
370
|
+
piece_h = max(1, RGB_AREA // piece_w)
|
|
371
|
+
for top in range(0, h, piece_h):
|
|
372
|
+
for left in range(0, w, piece_w):
|
|
373
|
+
pw, ph = min(piece_w, w - left), min(piece_h, h - top)
|
|
374
|
+
|
|
375
|
+
def call():
|
|
376
|
+
self._send(CMD_FRAME_RGB, 1, x + left, y + top, pw, ph)
|
|
377
|
+
gw, gh, size = struct.unpack(">HHI", self._exact(8))
|
|
378
|
+
return gw, gh, self._exact(size)
|
|
379
|
+
|
|
380
|
+
gw, gh, data = self._retry(call)
|
|
381
|
+
if gw and gh:
|
|
382
|
+
picture.paste(Image.frombytes("RGB", (gw, gh), unpack_rgb(data, gw * gh)),
|
|
383
|
+
(left, top))
|
|
384
|
+
return picture
|
|
385
|
+
|
|
386
|
+
def move_relative(self, dx: int, dy: int) -> None:
|
|
387
|
+
"""Mouse movement as deltas, for programs that ignore where the pointer is put."""
|
|
388
|
+
self._need(2)
|
|
389
|
+
self._ok(CMD_MOVE_REL, 0, dx & 0xFFFF, dy & 0xFFFF)
|
|
390
|
+
|
|
391
|
+
def ui_tree(self) -> list[dict]:
|
|
392
|
+
"""Front screen, its windows and their gadgets, with screen coordinates."""
|
|
393
|
+
items = []
|
|
394
|
+
for f in self._text(CMD_UITREE):
|
|
395
|
+
if f[0] == "S" and len(f) >= 5:
|
|
396
|
+
items.append({"kind": "screen", "handle": int(f[1], 16), "width": int(f[2]),
|
|
397
|
+
"height": int(f[3]), "title": f[4]})
|
|
398
|
+
elif f[0] == "W" and len(f) >= 8:
|
|
399
|
+
items.append({"kind": "window", "id": f[1], "x": int(f[2]), "y": int(f[3]),
|
|
400
|
+
"w": int(f[4]), "h": int(f[5]), "active": "a" in f[6],
|
|
401
|
+
"title": f[7]})
|
|
402
|
+
elif f[0] == "G" and len(f) >= 10:
|
|
403
|
+
items.append({"kind": "gadget", "window": f[1], "id": f[2], "x": int(f[3]),
|
|
404
|
+
"y": int(f[4]), "w": int(f[5]), "h": int(f[6]), "type": f[7],
|
|
405
|
+
"disabled": "d" in f[8], "selected": "s" in f[8], "label": f[9]})
|
|
406
|
+
return items
|
|
407
|
+
|
|
408
|
+
def menus(self) -> list[dict]:
|
|
409
|
+
"""Menu strip of the active window with the boxes where titles and items appear."""
|
|
410
|
+
items = []
|
|
411
|
+
for f in self._text(CMD_MENUS):
|
|
412
|
+
kind = {"M": "menu", "I": "item", "U": "sub"}.get(f[0])
|
|
413
|
+
if not kind:
|
|
414
|
+
continue
|
|
415
|
+
index = {"M": 2, "I": 3, "U": 4}[f[0]]
|
|
416
|
+
if len(f) < index + (6 if kind == "menu" else 7):
|
|
417
|
+
continue
|
|
418
|
+
path = [int(v) for v in f[1:index]]
|
|
419
|
+
x, y, w, h = (int(v) for v in f[index:index + 4])
|
|
420
|
+
flags = f[index + 4]
|
|
421
|
+
entry = {"kind": kind, "path": path, "x": x, "y": y, "w": w, "h": h,
|
|
422
|
+
"enabled": "e" in flags, "checked": "c" in flags}
|
|
423
|
+
if kind == "menu":
|
|
424
|
+
entry["text"] = f[index + 5]
|
|
425
|
+
else:
|
|
426
|
+
entry["key"], entry["text"] = f[index + 5], f[index + 6]
|
|
427
|
+
items.append(entry)
|
|
428
|
+
return items
|
|
429
|
+
|
|
430
|
+
def changed_since(self, since: float, x: int = 0, y: int = 0, w: int | None = None,
|
|
431
|
+
h: int | None = None) -> int:
|
|
432
|
+
"""Tiles received after since that touch the region, whoever asked for them."""
|
|
433
|
+
right, bottom = x + (w or self.width), y + (h or self.height)
|
|
434
|
+
return sum(1 for t, tx, ty, tw, th in list(self.changes)
|
|
435
|
+
if t > since and tx < right and tx + tw > x and ty < bottom and ty + th > y)
|
|
436
|
+
|
|
437
|
+
def type_text(self, text: str) -> None:
|
|
438
|
+
"""Type text; the keystrokes go out in batches and are answered together."""
|
|
439
|
+
commands = []
|
|
440
|
+
for ch in text:
|
|
441
|
+
if ch == "\n":
|
|
442
|
+
code = RAWKEYS["return"]
|
|
443
|
+
commands += [(CMD_RAWKEY, 1, code, 0), (CMD_RAWKEY, 0, code, 0)]
|
|
444
|
+
elif ord(ch) < 256:
|
|
445
|
+
commands.append((CMD_CHAR, 0, ord(ch), 0))
|
|
446
|
+
for start in range(0, len(commands), 64):
|
|
447
|
+
batch = commands[start:start + 64]
|
|
448
|
+
|
|
449
|
+
def call():
|
|
450
|
+
self._sock.sendall(b"".join(struct.pack(">BBHHHH", c, a, x, y, 0, 0)
|
|
451
|
+
for c, a, x, y in batch))
|
|
452
|
+
self._exact(len(batch)) # characters the keymap lacks just answer 1
|
|
453
|
+
|
|
454
|
+
self._retry(call)
|
|
455
|
+
|
|
456
|
+
def press(self, combo: str) -> None:
|
|
457
|
+
"""Press a combination such as 'lamiga+n', 'return' or 'shift+f1'."""
|
|
458
|
+
*mods, key = combo.lower().split("+")
|
|
459
|
+
qualifier = 0
|
|
460
|
+
for mod in mods:
|
|
461
|
+
qualifier |= QUALIFIERS[mod]
|
|
462
|
+
if key in RAWKEYS:
|
|
463
|
+
self.rawkey(RAWKEYS[key], True, qualifier)
|
|
464
|
+
self.rawkey(RAWKEYS[key], False, qualifier)
|
|
465
|
+
else:
|
|
466
|
+
self.char(key, qualifier)
|