noracu 0.1.1 → 0.1.3
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/CHANGELOG.md +98 -0
- package/README.md +22 -13
- package/README.zh-CN.md +21 -12
- package/SECURITY.md +5 -1
- package/docs/architecture.md +3 -2
- package/docs/tools.md +13 -13
- package/docs/usage.md +37 -11
- package/imctl/README.md +5 -3
- package/imctl/src/imctl/cli.py +261 -18
- package/imctl/src/imctl/gestures.py +555 -28
- package/imctl/src/imctl/input.py +443 -45
- package/imctl/src/imctl/verify.py +250 -1
- package/imctl/src/imctl/vision.py +214 -29
- package/imctl/src/imctl/window.py +285 -27
- package/package.json +3 -2
- package/src/server.mjs +113 -26
package/imctl/src/imctl/cli.py
CHANGED
|
@@ -21,10 +21,107 @@ from __future__ import annotations
|
|
|
21
21
|
|
|
22
22
|
import argparse
|
|
23
23
|
import json
|
|
24
|
+
import os
|
|
25
|
+
import signal
|
|
24
26
|
import sys
|
|
27
|
+
import tempfile
|
|
25
28
|
|
|
26
29
|
from . import gestures, input, verify, vision
|
|
27
|
-
from .
|
|
30
|
+
from .vision import CaptureError
|
|
31
|
+
from .window import WindowError, ensure_frontmost, get_overlay
|
|
32
|
+
|
|
33
|
+
# F31: every phone input command starts in _require_live(), which first brings
|
|
34
|
+
# the mirror to the front — switching Spaces if needed and waiting for a real
|
|
35
|
+
# frame — so the expect OCR and scroll baseline that follow read the phone, not
|
|
36
|
+
# another Space or a transition frame. Module-level so tests can swap it.
|
|
37
|
+
_bring_front = ensure_frontmost
|
|
38
|
+
|
|
39
|
+
# F25: one line, one exit code for every capture failure. The pixels a check
|
|
40
|
+
# needed are not the phone's — report that, never guess from whatever
|
|
41
|
+
# another window happened to show inside the region.
|
|
42
|
+
_CAPTURE_FAILED_LINE = (
|
|
43
|
+
"capture_failed: could not capture the iPhone Mirroring window "
|
|
44
|
+
"(it may be hidden, minimized or on another Space); no image was returned"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _capture_failed() -> int:
|
|
49
|
+
"""Print the F25 capture-failure line; exit code 7."""
|
|
50
|
+
print(_CAPTURE_FAILED_LINE, file=sys.stderr)
|
|
51
|
+
return 7
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _require_live() -> int | None:
|
|
55
|
+
"""Refuse phone input while the mirror shows an overlay instead of the phone stream.
|
|
56
|
+
|
|
57
|
+
Returns exit code 3 after printing a `mirror_not_live:` line when the mirror is
|
|
58
|
+
verifiably not live; returns None (allow) when live or unknown (fail-open).
|
|
59
|
+
F31: brings the mirror to the front first (see _bring_front).
|
|
60
|
+
"""
|
|
61
|
+
_bring_front()
|
|
62
|
+
overlay = get_overlay()
|
|
63
|
+
if overlay["live"] is False:
|
|
64
|
+
label = overlay["overlay"] or "overlay shown"
|
|
65
|
+
print(
|
|
66
|
+
f"mirror_not_live: {label} — call phone_launch (or imctl resume) first",
|
|
67
|
+
file=sys.stderr,
|
|
68
|
+
)
|
|
69
|
+
return 3
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _require_expect(args, read_screen=None, read_sweep=None) -> int | None:
|
|
74
|
+
"""Refuse input when the --expect anchor text is not on screen.
|
|
75
|
+
|
|
76
|
+
Between two phone_* calls the iPhone foreground can change (the user
|
|
77
|
+
picked up the phone, tapped a notification); the mirror cannot report
|
|
78
|
+
the phone's foreground app identity, so an OCR text anchor is the
|
|
79
|
+
only available guard — the iPhone-side counterpart of mac_key's app
|
|
80
|
+
focus guard. Returns None when no --expect was passed or the text is
|
|
81
|
+
on screen; returns exit code 5 after printing an ``expect_not_found:``
|
|
82
|
+
line when it is not, in which case nothing is sent. A capture failure
|
|
83
|
+
refuses the same way with ``capture_failed:`` and exit code 7 — an
|
|
84
|
+
unreadable screen is not a match, so nothing is sent then either.
|
|
85
|
+
"""
|
|
86
|
+
expect = getattr(args, "expect", None)
|
|
87
|
+
if expect is None:
|
|
88
|
+
return None
|
|
89
|
+
if read_screen is None:
|
|
90
|
+
read_screen = vision.read_screen
|
|
91
|
+
if read_sweep is None:
|
|
92
|
+
def read_sweep():
|
|
93
|
+
return vision.read_screen(language_correction=False, sweep=True)
|
|
94
|
+
try:
|
|
95
|
+
hits = read_screen()
|
|
96
|
+
if (read_sweep is not None and verify.is_short_text(expect)
|
|
97
|
+
and not verify.screen_contains(expect, hits)):
|
|
98
|
+
# F27: a one- or two-character anchor (the app "X") can be invisible
|
|
99
|
+
# to the default OCR pass; read again with the sweep before refusing.
|
|
100
|
+
hits = read_sweep()
|
|
101
|
+
except (WindowError, CaptureError):
|
|
102
|
+
# F25: an unreadable screen is not a match — refuse exactly like
|
|
103
|
+
# expect_not_found; nothing is sent.
|
|
104
|
+
return _capture_failed()
|
|
105
|
+
if verify.screen_contains(expect, hits):
|
|
106
|
+
return None
|
|
107
|
+
print(
|
|
108
|
+
f"expect_not_found: {expect!r} is not on screen; nothing was sent",
|
|
109
|
+
file=sys.stderr,
|
|
110
|
+
)
|
|
111
|
+
return 5
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _warn_if_not_live() -> None:
|
|
115
|
+
"""Print a heads-up before ocr/find output when the mirror is not live.
|
|
116
|
+
|
|
117
|
+
The captured pixels then show the overlay (paused/connecting notice), not the phone.
|
|
118
|
+
"""
|
|
119
|
+
overlay = get_overlay()
|
|
120
|
+
if overlay["live"] is False:
|
|
121
|
+
print(
|
|
122
|
+
f"warning: mirror not live (overlay: {overlay['overlay'] or 'unknown'}) — "
|
|
123
|
+
"the text below comes from the overlay, not the phone; call phone_launch"
|
|
124
|
+
)
|
|
28
125
|
|
|
29
126
|
|
|
30
127
|
def _cmd_status(_args) -> int:
|
|
@@ -33,18 +130,36 @@ def _cmd_status(_args) -> int:
|
|
|
33
130
|
|
|
34
131
|
|
|
35
132
|
def _cmd_tap(args) -> int:
|
|
133
|
+
guard = _require_live()
|
|
134
|
+
if guard is not None:
|
|
135
|
+
return guard
|
|
136
|
+
guard = _require_expect(args)
|
|
137
|
+
if guard is not None:
|
|
138
|
+
return guard
|
|
36
139
|
input.tap(args.x, args.y)
|
|
37
140
|
print(f"tapped ({args.x}, {args.y})")
|
|
38
141
|
return 0
|
|
39
142
|
|
|
40
143
|
|
|
41
144
|
def _cmd_longpress(args) -> int:
|
|
145
|
+
guard = _require_live()
|
|
146
|
+
if guard is not None:
|
|
147
|
+
return guard
|
|
148
|
+
guard = _require_expect(args)
|
|
149
|
+
if guard is not None:
|
|
150
|
+
return guard
|
|
42
151
|
input.long_press(args.x, args.y, args.duration)
|
|
43
152
|
print(f"long-pressed ({args.x}, {args.y}) for {args.duration}s")
|
|
44
153
|
return 0
|
|
45
154
|
|
|
46
155
|
|
|
47
156
|
def _cmd_swipe(args) -> int:
|
|
157
|
+
guard = _require_live()
|
|
158
|
+
if guard is not None:
|
|
159
|
+
return guard
|
|
160
|
+
guard = _require_expect(args)
|
|
161
|
+
if guard is not None:
|
|
162
|
+
return guard
|
|
48
163
|
input.swipe(args.fx, args.fy, args.tx, args.ty, args.ms)
|
|
49
164
|
print(f"swiped ({args.fx},{args.fy}) -> ({args.tx},{args.ty})")
|
|
50
165
|
return 0
|
|
@@ -63,15 +178,38 @@ def _cmd_app_switcher(_args) -> int:
|
|
|
63
178
|
|
|
64
179
|
|
|
65
180
|
def _cmd_scroll(args) -> int:
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
181
|
+
guard = _require_live()
|
|
182
|
+
if guard is not None:
|
|
183
|
+
return guard
|
|
184
|
+
guard = _require_expect(args)
|
|
185
|
+
if guard is not None:
|
|
186
|
+
return guard
|
|
187
|
+
try:
|
|
188
|
+
moved = gestures.scroll_checked(args.direction, args.distance)
|
|
189
|
+
except ValueError:
|
|
190
|
+
print(
|
|
191
|
+
"phone_scroll: distance is in window heights, "
|
|
192
|
+
"0 < distance <= 3 (default 0.5)",
|
|
193
|
+
file=sys.stderr,
|
|
194
|
+
)
|
|
195
|
+
return 2
|
|
196
|
+
except (WindowError, CaptureError):
|
|
197
|
+
return _capture_failed()
|
|
198
|
+
if not moved:
|
|
199
|
+
print(
|
|
200
|
+
"scroll_no_effect: the screen did not change (end of the list, "
|
|
201
|
+
"or the view under the cursor does not scroll)",
|
|
202
|
+
file=sys.stderr,
|
|
203
|
+
)
|
|
204
|
+
return 4
|
|
70
205
|
print(f"scrolled {args.direction} {args.distance}")
|
|
71
206
|
return 0
|
|
72
207
|
|
|
73
208
|
|
|
74
209
|
def _cmd_page(args) -> int:
|
|
210
|
+
guard = _require_live()
|
|
211
|
+
if guard is not None:
|
|
212
|
+
return guard
|
|
75
213
|
if args.direction == "left":
|
|
76
214
|
gestures.page_left()
|
|
77
215
|
else:
|
|
@@ -81,19 +219,42 @@ def _cmd_page(args) -> int:
|
|
|
81
219
|
|
|
82
220
|
|
|
83
221
|
def _cmd_type(args) -> int:
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
222
|
+
guard = _require_live()
|
|
223
|
+
if guard is not None:
|
|
224
|
+
return guard
|
|
225
|
+
guard = _require_expect(args)
|
|
226
|
+
if guard is not None:
|
|
227
|
+
return guard
|
|
228
|
+
if input.type_text(args.text, replace=args.replace):
|
|
229
|
+
print(f"typed {args.text!r}")
|
|
230
|
+
return 0
|
|
231
|
+
print(
|
|
232
|
+
"type_not_confirmed: the paste did not show on screen within 2.5s; "
|
|
233
|
+
"the clipboard still holds the typed text and your previous clipboard "
|
|
234
|
+
"was not restored",
|
|
235
|
+
file=sys.stderr,
|
|
236
|
+
)
|
|
237
|
+
return 4
|
|
87
238
|
|
|
88
239
|
|
|
89
240
|
def _cmd_key(args) -> int:
|
|
241
|
+
guard = _require_live()
|
|
242
|
+
if guard is not None:
|
|
243
|
+
return guard
|
|
244
|
+
guard = _require_expect(args)
|
|
245
|
+
if guard is not None:
|
|
246
|
+
return guard
|
|
90
247
|
input.press_key(args.key)
|
|
91
248
|
print(f"pressed {args.key}")
|
|
92
249
|
return 0
|
|
93
250
|
|
|
94
251
|
|
|
95
252
|
def _cmd_ocr(_args) -> int:
|
|
96
|
-
|
|
253
|
+
_warn_if_not_live()
|
|
254
|
+
try:
|
|
255
|
+
hits = gestures.read_text()
|
|
256
|
+
except (WindowError, CaptureError):
|
|
257
|
+
return _capture_failed()
|
|
97
258
|
for h in hits:
|
|
98
259
|
print(h)
|
|
99
260
|
print(f"-- {len(hits)} hits")
|
|
@@ -101,7 +262,11 @@ def _cmd_ocr(_args) -> int:
|
|
|
101
262
|
|
|
102
263
|
|
|
103
264
|
def _cmd_find(args) -> int:
|
|
104
|
-
|
|
265
|
+
_warn_if_not_live()
|
|
266
|
+
try:
|
|
267
|
+
hit = vision.find(args.text, exact=args.exact)
|
|
268
|
+
except (WindowError, CaptureError):
|
|
269
|
+
return _capture_failed()
|
|
105
270
|
if hit is None:
|
|
106
271
|
print(f"not found: {args.text!r}", file=sys.stderr)
|
|
107
272
|
return 1
|
|
@@ -110,9 +275,39 @@ def _cmd_find(args) -> int:
|
|
|
110
275
|
|
|
111
276
|
|
|
112
277
|
def _cmd_open(args) -> int:
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
278
|
+
guard = _require_live()
|
|
279
|
+
if guard is not None:
|
|
280
|
+
return guard
|
|
281
|
+
# Open Spotlight here (not inside open_app) so a failure can be
|
|
282
|
+
# reported as its own condition instead of a bogus "app not found".
|
|
283
|
+
try:
|
|
284
|
+
# F34: one retry — on the device the Spotlight command was once
|
|
285
|
+
# ignored right after an app closed. Each try starts from the Home
|
|
286
|
+
# Screen, so a second try cannot toggle an open Spotlight shut.
|
|
287
|
+
if not (gestures.open_spotlight_checked()
|
|
288
|
+
or gestures.open_spotlight_checked()):
|
|
289
|
+
result = "spotlight_unavailable"
|
|
290
|
+
else:
|
|
291
|
+
result = gestures.open_app(args.app, spotlight_open=True)
|
|
292
|
+
except (WindowError, CaptureError):
|
|
293
|
+
# F25: the Spotlight gate and the result matching both read the
|
|
294
|
+
# mirroring window's pixels; without them nothing about the
|
|
295
|
+
# phone is known — refuse rather than guess.
|
|
296
|
+
return _capture_failed()
|
|
297
|
+
if result == "opened":
|
|
298
|
+
print(f"opened {args.app!r}")
|
|
299
|
+
return 0
|
|
300
|
+
if result == "not_found":
|
|
301
|
+
print(f"app not found: {args.app!r}")
|
|
302
|
+
return 1
|
|
303
|
+
if result == "spotlight_unavailable":
|
|
304
|
+
print("spotlight unavailable (could not reach the Home Screen, the menu command failed, or the screen did not change)")
|
|
305
|
+
return 1
|
|
306
|
+
# "not_confirmed" (F24): the row — and possibly the Top Hit icon —
|
|
307
|
+
# was tapped but the app never verifiably opened; the phone is
|
|
308
|
+
# back on the Home Screen.
|
|
309
|
+
print(f"open_not_confirmed: tapped {args.app!r} in Spotlight but the app did not open; back on the Home Screen", file=sys.stderr)
|
|
310
|
+
return 4
|
|
116
311
|
|
|
117
312
|
|
|
118
313
|
def _cmd_verify(args) -> int:
|
|
@@ -131,20 +326,43 @@ def _cmd_verify(args) -> int:
|
|
|
131
326
|
|
|
132
327
|
|
|
133
328
|
def _cmd_launch(_args) -> int:
|
|
134
|
-
gestures.launch()
|
|
135
|
-
|
|
329
|
+
live = gestures.launch()
|
|
330
|
+
if live is False:
|
|
331
|
+
# F29: never report "ready" while the overlay is still up.
|
|
332
|
+
print(
|
|
333
|
+
"mirror_not_live: iPhone Mirroring is still paused — lock the iPhone, keep it "
|
|
334
|
+
"near the Mac, then call phone_launch again",
|
|
335
|
+
file=sys.stderr,
|
|
336
|
+
)
|
|
337
|
+
return 3
|
|
338
|
+
print("iPhone Mirroring ready" if live else "iPhone Mirroring launched (stream state unknown)")
|
|
136
339
|
return 0
|
|
137
340
|
|
|
138
341
|
|
|
139
342
|
def _cmd_screenshot(args) -> int:
|
|
140
|
-
path =
|
|
343
|
+
path = args.path
|
|
344
|
+
if path is None:
|
|
345
|
+
# A screenshot asked for outlives this process: an owner-only (0600)
|
|
346
|
+
# file in the temp directory, unlike the OCR frames, which are
|
|
347
|
+
# deleted once read.
|
|
348
|
+
fd, path = tempfile.mkstemp(prefix="imctl_screenshot_", suffix=".png")
|
|
349
|
+
os.close(fd)
|
|
350
|
+
try:
|
|
351
|
+
path = vision.capture_window(path)
|
|
352
|
+
except (WindowError, CaptureError):
|
|
353
|
+
if args.path is None:
|
|
354
|
+
try:
|
|
355
|
+
os.remove(path) # the empty placeholder made above
|
|
356
|
+
except OSError:
|
|
357
|
+
pass
|
|
358
|
+
return _capture_failed()
|
|
141
359
|
print(path)
|
|
142
360
|
return 0
|
|
143
361
|
|
|
144
362
|
|
|
145
363
|
def _cmd_resume(_args) -> int:
|
|
146
364
|
ok = gestures.resume()
|
|
147
|
-
print("resumed" if ok else "still paused (
|
|
365
|
+
print("resumed" if ok else "still paused (lock the iPhone, keep it near the Mac, then retry)")
|
|
148
366
|
return 0 if ok else 1
|
|
149
367
|
|
|
150
368
|
|
|
@@ -159,18 +377,20 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
159
377
|
sp = sub.add_parser("screenshot", help="capture the mirroring window to PNG (by window id, works when covered)")
|
|
160
378
|
sp.add_argument("path", nargs="?", default=None)
|
|
161
379
|
sp.set_defaults(func=_cmd_screenshot)
|
|
162
|
-
sub.add_parser("resume", help="click Resume when the session
|
|
380
|
+
sub.add_parser("resume", help="click Resume when the session is paused").set_defaults(func=_cmd_resume)
|
|
163
381
|
sub.add_parser("launch", help="ensure mirroring is running and frontmost").set_defaults(func=_cmd_launch)
|
|
164
382
|
|
|
165
383
|
sp = sub.add_parser("tap", help="tap at normalized coords")
|
|
166
384
|
sp.add_argument("x", type=float)
|
|
167
385
|
sp.add_argument("y", type=float)
|
|
386
|
+
sp.add_argument("--expect", help="refuse to act unless this text is on screen (OCR)")
|
|
168
387
|
sp.set_defaults(func=_cmd_tap)
|
|
169
388
|
|
|
170
389
|
sp = sub.add_parser("longpress", help="long-press at normalized coords")
|
|
171
390
|
sp.add_argument("x", type=float)
|
|
172
391
|
sp.add_argument("y", type=float)
|
|
173
392
|
sp.add_argument("--duration", type=float, default=0.8)
|
|
393
|
+
sp.add_argument("--expect", help="refuse to act unless this text is on screen (OCR)")
|
|
174
394
|
sp.set_defaults(func=_cmd_longpress)
|
|
175
395
|
|
|
176
396
|
sp = sub.add_parser("swipe", help="swipe between two normalized points")
|
|
@@ -179,6 +399,7 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
179
399
|
sp.add_argument("tx", type=float)
|
|
180
400
|
sp.add_argument("ty", type=float)
|
|
181
401
|
sp.add_argument("--ms", type=int, default=300)
|
|
402
|
+
sp.add_argument("--expect", help="refuse to act unless this text is on screen (OCR)")
|
|
182
403
|
sp.set_defaults(func=_cmd_swipe)
|
|
183
404
|
|
|
184
405
|
sub.add_parser("home", help="home gesture").set_defaults(func=_cmd_home)
|
|
@@ -187,6 +408,7 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
187
408
|
sp = sub.add_parser("scroll", help="scroll up/down")
|
|
188
409
|
sp.add_argument("direction", choices=["up", "down"])
|
|
189
410
|
sp.add_argument("distance", type=float, nargs="?", default=0.5)
|
|
411
|
+
sp.add_argument("--expect", help="refuse to act unless this text is on screen (OCR)")
|
|
190
412
|
sp.set_defaults(func=_cmd_scroll)
|
|
191
413
|
|
|
192
414
|
sp = sub.add_parser("page", help="page left/right on home screen")
|
|
@@ -195,10 +417,14 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
195
417
|
|
|
196
418
|
sp = sub.add_parser("type", help="type text (clipboard-based, IME-safe)")
|
|
197
419
|
sp.add_argument("text")
|
|
420
|
+
sp.add_argument("--replace", action="store_true",
|
|
421
|
+
help="select all in the focused field first, then overwrite it")
|
|
422
|
+
sp.add_argument("--expect", help="refuse to act unless this text is on screen (OCR)")
|
|
198
423
|
sp.set_defaults(func=_cmd_type)
|
|
199
424
|
|
|
200
425
|
sp = sub.add_parser("key", help="press a key (return/delete/up/down/left/right)")
|
|
201
426
|
sp.add_argument("key")
|
|
427
|
+
sp.add_argument("--expect", help="refuse to act unless this text is on screen (OCR)")
|
|
202
428
|
sp.set_defaults(func=_cmd_key)
|
|
203
429
|
|
|
204
430
|
sub.add_parser("ocr", help="OCR the screen").set_defaults(func=_cmd_ocr)
|
|
@@ -219,11 +445,28 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
219
445
|
return p
|
|
220
446
|
|
|
221
447
|
|
|
448
|
+
def _on_sigterm(signum, _frame):
|
|
449
|
+
# The server ends a call that runs past its time limit with SIGTERM. As
|
|
450
|
+
# SystemExit it unwinds normally: OCR frames are deleted, and a paste
|
|
451
|
+
# that never went out puts the clipboard back (type_text decides).
|
|
452
|
+
raise SystemExit(128 + signum)
|
|
453
|
+
|
|
454
|
+
|
|
222
455
|
def main(argv: list[str] | None = None) -> int:
|
|
456
|
+
signal.signal(signal.SIGTERM, _on_sigterm)
|
|
223
457
|
parser = build_parser()
|
|
224
458
|
args = parser.parse_args(argv)
|
|
225
459
|
try:
|
|
226
460
|
return args.func(args)
|
|
461
|
+
except input.FocusLost:
|
|
462
|
+
# F26: the frontmost check failed right before an event — either
|
|
463
|
+
# nothing was sent at all, or (mid-scroll) nothing further was.
|
|
464
|
+
print(
|
|
465
|
+
"focus_lost: iPhone Mirroring is not the frontmost app; "
|
|
466
|
+
"nothing (or nothing further) was sent",
|
|
467
|
+
file=sys.stderr,
|
|
468
|
+
)
|
|
469
|
+
return 6
|
|
227
470
|
except WindowError as exc:
|
|
228
471
|
print(f"window error: {exc}", file=sys.stderr)
|
|
229
472
|
return 3
|