noracu 0.1.4 → 0.2.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/CHANGELOG.md +437 -0
- package/README.md +34 -22
- package/README.zh-CN.md +36 -24
- package/SECURITY.md +15 -8
- package/docs/architecture.md +58 -27
- package/docs/tools.md +45 -34
- package/docs/usage.md +180 -64
- package/imctl/README.md +16 -13
- package/imctl/pyproject.toml +1 -1
- package/imctl/src/imctl/__init__.py +1 -1
- package/imctl/src/imctl/cli.py +152 -38
- package/imctl/src/imctl/gestures.py +21 -25
- package/imctl/src/imctl/input.py +187 -50
- package/imctl/src/imctl/window.py +10 -3
- package/package.json +5 -4
- package/src/mac-ax.js +2494 -0
- package/src/server.mjs +1719 -483
package/imctl/src/imctl/cli.py
CHANGED
|
@@ -30,13 +30,25 @@ import time
|
|
|
30
30
|
|
|
31
31
|
from . import gestures, input, verify, vision
|
|
32
32
|
from .vision import CaptureError
|
|
33
|
-
|
|
33
|
+
import subprocess
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
#
|
|
38
|
-
#
|
|
35
|
+
from .window import WindowError, ensure_frontmost, get_overlay, is_frontmost
|
|
36
|
+
|
|
37
|
+
# F31 / R13 §3: every phone input command starts in _require_live(), which probes
|
|
38
|
+
# get_overlay() first so a paused or stopped mirror refuses without stealing focus,
|
|
39
|
+
# then brings the mirror to the front (switching Spaces if needed and waiting for a
|
|
40
|
+
# real frame) before any capture or input. Module-level callables so tests can swap them.
|
|
39
41
|
_bring_front = ensure_frontmost
|
|
42
|
+
_is_front = is_frontmost
|
|
43
|
+
sleep = time.sleep
|
|
44
|
+
|
|
45
|
+
_MIRROR_NOT_RUNNING_LINE = (
|
|
46
|
+
"mirror_not_live: iPhone Mirroring is not running — call phone_launch first"
|
|
47
|
+
)
|
|
48
|
+
_MIRROR_STATE_UNKNOWN_LINE = (
|
|
49
|
+
"mirror_state_unknown: could not tell whether iPhone Mirroring is live — "
|
|
50
|
+
"nothing was sent; retry, or call phone_status"
|
|
51
|
+
)
|
|
40
52
|
|
|
41
53
|
# F25: one line, one exit code for every capture failure. The pixels a check
|
|
42
54
|
# needed are not the phone's — report that, never guess from whatever
|
|
@@ -47,29 +59,85 @@ _CAPTURE_FAILED_LINE = (
|
|
|
47
59
|
)
|
|
48
60
|
|
|
49
61
|
|
|
62
|
+
# R19: the same failure while checking --expect, before the first event
|
|
63
|
+
_CAPTURE_FAILED_EXPECT_LINE = (
|
|
64
|
+
"capture_failed: could not capture the iPhone Mirroring window to check --expect "
|
|
65
|
+
"(it may be hidden, minimized or on another Space); no image was returned, so nothing was sent"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
50
69
|
def _capture_failed() -> int:
|
|
51
70
|
"""Print the F25 capture-failure line; exit code 7."""
|
|
52
71
|
print(_CAPTURE_FAILED_LINE, file=sys.stderr)
|
|
53
72
|
return 7
|
|
54
73
|
|
|
55
74
|
|
|
56
|
-
def
|
|
57
|
-
""
|
|
75
|
+
def _mirror_not_live(overlay: dict) -> int:
|
|
76
|
+
label = overlay.get("overlay") or "overlay shown"
|
|
77
|
+
print(
|
|
78
|
+
f"mirror_not_live: {label} — call phone_launch (or imctl resume) first",
|
|
79
|
+
file=sys.stderr,
|
|
80
|
+
)
|
|
81
|
+
return 3
|
|
82
|
+
|
|
58
83
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
84
|
+
def _require_live() -> int | None:
|
|
85
|
+
"""Refuse phone input unless iPhone Mirroring is verifiably live (fail-closed).
|
|
86
|
+
|
|
87
|
+
Probes get_overlay() first to avoid stealing focus when the mirror is paused or off:
|
|
88
|
+
- reason in ("not_running", "no_window") -> mirror_not_live (exit 3), no _bring_front
|
|
89
|
+
- live is False -> mirror_not_live (exit 3), no _bring_front
|
|
90
|
+
- live is True -> _bring_front() (F31), then allow (None)
|
|
91
|
+
- live is None -> _bring_front(), sleep(0.4), probe again:
|
|
92
|
+
True -> allow; False -> mirror_not_live; still None -> mirror_state_unknown (exit 3).
|
|
62
93
|
"""
|
|
63
|
-
_bring_front()
|
|
64
94
|
overlay = get_overlay()
|
|
95
|
+
if overlay.get("reason") in ("not_running", "no_window"):
|
|
96
|
+
print(_MIRROR_NOT_RUNNING_LINE, file=sys.stderr)
|
|
97
|
+
return 3
|
|
65
98
|
if overlay["live"] is False:
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
99
|
+
return _mirror_not_live(overlay)
|
|
100
|
+
if overlay["live"] is True:
|
|
101
|
+
# R18: bringing it forward can take a Space switch; the mirror may stop being live meanwhile
|
|
102
|
+
# (the iPhone was picked up) — so when it was not in front, look again afterwards
|
|
103
|
+
try:
|
|
104
|
+
was_front = _is_front()
|
|
105
|
+
except Exception:
|
|
106
|
+
was_front = False
|
|
107
|
+
try:
|
|
108
|
+
_bring_front()
|
|
109
|
+
except (WindowError, subprocess.TimeoutExpired):
|
|
110
|
+
print(_MIRROR_STATE_UNKNOWN_LINE, file=sys.stderr)
|
|
111
|
+
return 3
|
|
112
|
+
if was_front:
|
|
113
|
+
return None
|
|
114
|
+
overlay = get_overlay()
|
|
115
|
+
if overlay["live"] is True:
|
|
116
|
+
return None
|
|
117
|
+
if overlay["live"] is False:
|
|
118
|
+
return _mirror_not_live(overlay)
|
|
119
|
+
print(_MIRROR_STATE_UNKNOWN_LINE, file=sys.stderr)
|
|
71
120
|
return 3
|
|
72
|
-
|
|
121
|
+
try:
|
|
122
|
+
_bring_front()
|
|
123
|
+
except (WindowError, subprocess.TimeoutExpired):
|
|
124
|
+
again = get_overlay()
|
|
125
|
+
if again.get("reason") in ("not_running", "no_window"):
|
|
126
|
+
print(_MIRROR_NOT_RUNNING_LINE, file=sys.stderr)
|
|
127
|
+
return 3
|
|
128
|
+
print(_MIRROR_STATE_UNKNOWN_LINE, file=sys.stderr)
|
|
129
|
+
return 3
|
|
130
|
+
sleep(0.4)
|
|
131
|
+
overlay = get_overlay()
|
|
132
|
+
if overlay.get("reason") in ("not_running", "no_window"):
|
|
133
|
+
print(_MIRROR_NOT_RUNNING_LINE, file=sys.stderr)
|
|
134
|
+
return 3
|
|
135
|
+
if overlay["live"] is True:
|
|
136
|
+
return None
|
|
137
|
+
if overlay["live"] is False:
|
|
138
|
+
return _mirror_not_live(overlay)
|
|
139
|
+
print(_MIRROR_STATE_UNKNOWN_LINE, file=sys.stderr)
|
|
140
|
+
return 3
|
|
73
141
|
|
|
74
142
|
|
|
75
143
|
def _require_expect(args, read_screen=None, read_sweep=None) -> int | None:
|
|
@@ -102,8 +170,9 @@ def _require_expect(args, read_screen=None, read_sweep=None) -> int | None:
|
|
|
102
170
|
hits = read_sweep()
|
|
103
171
|
except (WindowError, CaptureError):
|
|
104
172
|
# F25: an unreadable screen is not a match — refuse exactly like
|
|
105
|
-
# expect_not_found; nothing is sent.
|
|
106
|
-
|
|
173
|
+
# expect_not_found; nothing is sent (R19: and the line says so).
|
|
174
|
+
print(_CAPTURE_FAILED_EXPECT_LINE, file=sys.stderr)
|
|
175
|
+
return 7
|
|
107
176
|
if verify.screen_contains(expect, hits):
|
|
108
177
|
return None
|
|
109
178
|
print(
|
|
@@ -143,19 +212,40 @@ def _cmd_tap(args) -> int:
|
|
|
143
212
|
return 0
|
|
144
213
|
|
|
145
214
|
|
|
215
|
+
def _invalid_duration(exc: ValueError) -> int:
|
|
216
|
+
print(f"invalid_args: {exc}; nothing was sent", file=sys.stderr)
|
|
217
|
+
return 2
|
|
218
|
+
|
|
219
|
+
|
|
146
220
|
def _cmd_longpress(args) -> int:
|
|
221
|
+
# R19: a bad duration is refused before the mirror is brought forward
|
|
222
|
+
try:
|
|
223
|
+
input._check_duration(_longpress_duration(args), 0.01, 10, "long-press duration (s)")
|
|
224
|
+
except ValueError as exc:
|
|
225
|
+
return _invalid_duration(exc)
|
|
147
226
|
guard = _require_live()
|
|
148
227
|
if guard is not None:
|
|
149
228
|
return guard
|
|
150
229
|
guard = _require_expect(args)
|
|
151
230
|
if guard is not None:
|
|
152
231
|
return guard
|
|
153
|
-
|
|
154
|
-
|
|
232
|
+
duration = _longpress_duration(args)
|
|
233
|
+
input.long_press(args.x, args.y, duration)
|
|
234
|
+
print(f"long-pressed ({args.x}, {args.y}) for {duration}s")
|
|
155
235
|
return 0
|
|
156
236
|
|
|
157
237
|
|
|
238
|
+
def _longpress_duration(args) -> float:
|
|
239
|
+
opt = getattr(args, "duration_opt", None)
|
|
240
|
+
pos = getattr(args, "duration", None)
|
|
241
|
+
return opt if opt is not None else pos if pos is not None else 0.8
|
|
242
|
+
|
|
243
|
+
|
|
158
244
|
def _cmd_swipe(args) -> int:
|
|
245
|
+
try:
|
|
246
|
+
input._check_duration(args.ms, *input.SWIPE_MS_RANGE, "swipe duration (ms)")
|
|
247
|
+
except ValueError as exc:
|
|
248
|
+
return _invalid_duration(exc)
|
|
159
249
|
guard = _require_live()
|
|
160
250
|
if guard is not None:
|
|
161
251
|
return guard
|
|
@@ -168,14 +258,20 @@ def _cmd_swipe(args) -> int:
|
|
|
168
258
|
|
|
169
259
|
|
|
170
260
|
def _cmd_home(_args) -> int:
|
|
261
|
+
guard = _require_live()
|
|
262
|
+
if guard is not None:
|
|
263
|
+
return guard
|
|
171
264
|
gestures.home()
|
|
172
265
|
print("home gesture sent")
|
|
173
266
|
return 0
|
|
174
267
|
|
|
175
268
|
|
|
176
269
|
def _cmd_app_switcher(_args) -> int:
|
|
270
|
+
guard = _require_live()
|
|
271
|
+
if guard is not None:
|
|
272
|
+
return guard
|
|
177
273
|
gestures.app_switcher()
|
|
178
|
-
print("app switcher
|
|
274
|
+
print("app switcher gesture sent")
|
|
179
275
|
return 0
|
|
180
276
|
|
|
181
277
|
|
|
@@ -229,8 +325,18 @@ def _cmd_type(args) -> int:
|
|
|
229
325
|
return guard
|
|
230
326
|
report = {}
|
|
231
327
|
if input.type_text(args.text, replace=args.replace, report=report):
|
|
232
|
-
|
|
233
|
-
|
|
328
|
+
if report.get("seen", True):
|
|
329
|
+
print(f"typed {len(args.text)} chars")
|
|
330
|
+
return 0
|
|
331
|
+
if _late_paste_probe(args.text) is None:
|
|
332
|
+
# one character: OCR cannot read it back, only the picture changed
|
|
333
|
+
print(f"typed {len(args.text)} chars (the screen changed after the paste, but "
|
|
334
|
+
"OCR cannot read this text back — check the field; the clipboard still "
|
|
335
|
+
"holds the typed text)")
|
|
336
|
+
return 0
|
|
337
|
+
# device run 2026-11-06: only the picture changed (a selection highlight, a
|
|
338
|
+
# results list redraw) and OCR never saw the text — watch for it like an
|
|
339
|
+
# unconfirmed paste instead of reporting it typed
|
|
234
340
|
count_before = report.get("count_before")
|
|
235
341
|
late = _watch_late_paste(args.text, baseline=count_before)
|
|
236
342
|
if late is not None:
|
|
@@ -240,7 +346,7 @@ def _cmd_type(args) -> int:
|
|
|
240
346
|
# minimum hold, an untouched changeCount); after the fact, leaving the
|
|
241
347
|
# typed text on the clipboard is the choice that can never hand the phone
|
|
242
348
|
# the user's original clipboard (possibly a password).
|
|
243
|
-
print(f"typed {args.text
|
|
349
|
+
print(f"typed {len(args.text)} chars (the paste showed up late, during an extra "
|
|
244
350
|
f"{late:.0f}s check after the 2.5s confirmation window; the clipboard "
|
|
245
351
|
"still holds the typed text)")
|
|
246
352
|
return 0
|
|
@@ -388,18 +494,11 @@ def _cmd_open(args) -> int:
|
|
|
388
494
|
return guard
|
|
389
495
|
# Open Spotlight here (not inside open_app) so a failure can be
|
|
390
496
|
# reported as its own condition instead of a bogus "app not found".
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
# phone on the Home Screen rather than in Spotlight with text typed in.
|
|
397
|
-
# Best effort: the server sends SIGKILL 1.5 s after the SIGTERM.
|
|
398
|
-
try:
|
|
399
|
-
gestures.home_via_menu()
|
|
400
|
-
except BaseException:
|
|
401
|
-
pass
|
|
402
|
-
raise
|
|
497
|
+
# Stopped from outside (SIGTERM → SystemExit): no tidy-up trip of its own. Going Home from here
|
|
498
|
+
# would bring the mirror forward and send a Cmd chord while the server may be stopping (and a
|
|
499
|
+
# SIGKILL 1.5 s later could leave Cmd held); after a timeout the server returns the phone to the
|
|
500
|
+
# Home Screen itself through the mirror's menu (no focus change, no chord).
|
|
501
|
+
return _open_and_report(args)
|
|
403
502
|
|
|
404
503
|
|
|
405
504
|
def _open_and_report(args) -> int:
|
|
@@ -473,6 +572,9 @@ def _cmd_verify(args) -> int:
|
|
|
473
572
|
if args.action not in actions:
|
|
474
573
|
print(f"unknown action: {args.action}", file=sys.stderr)
|
|
475
574
|
return 2
|
|
575
|
+
guard = _require_live()
|
|
576
|
+
if guard is not None:
|
|
577
|
+
return guard
|
|
476
578
|
ok = verify.verify_action(actions[args.action])
|
|
477
579
|
print("screen changed: yes" if ok else "screen changed: NO (action may have been dropped)")
|
|
478
580
|
return 0 if ok else 1
|
|
@@ -555,7 +657,10 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
555
657
|
sp = sub.add_parser("longpress", help="long-press at normalized coords")
|
|
556
658
|
sp.add_argument("x", type=float)
|
|
557
659
|
sp.add_argument("y", type=float)
|
|
558
|
-
|
|
660
|
+
# positional (older callers) or --duration; argparse would let the positional's default overwrite
|
|
661
|
+
# the option, so they land in separate fields and _cmd_longpress picks one
|
|
662
|
+
sp.add_argument("duration", nargs="?", type=float, default=None)
|
|
663
|
+
sp.add_argument("--duration", dest="duration_opt", type=float, default=None)
|
|
559
664
|
sp.add_argument("--expect", help="refuse to act unless this text is on screen (OCR)")
|
|
560
665
|
sp.set_defaults(func=_cmd_longpress)
|
|
561
666
|
|
|
@@ -618,10 +723,19 @@ def _on_sigterm(signum, _frame):
|
|
|
618
723
|
raise SystemExit(128 + signum)
|
|
619
724
|
|
|
620
725
|
|
|
726
|
+
def _stdin_values(args) -> None:
|
|
727
|
+
# type / find / open take their text as "-" to read it from stdin: text in argv is visible to every
|
|
728
|
+
# local user in the process list while imctl runs
|
|
729
|
+
for field in ("text", "app"):
|
|
730
|
+
if getattr(args, field, None) == "-" and getattr(args, "command", None) in ("type", "find", "open"):
|
|
731
|
+
setattr(args, field, sys.stdin.read())
|
|
732
|
+
|
|
733
|
+
|
|
621
734
|
def main(argv: list[str] | None = None) -> int:
|
|
622
735
|
signal.signal(signal.SIGTERM, _on_sigterm)
|
|
623
736
|
parser = build_parser()
|
|
624
737
|
args = parser.parse_args(argv)
|
|
738
|
+
_stdin_values(args)
|
|
625
739
|
try:
|
|
626
740
|
return args.func(args)
|
|
627
741
|
except input.FocusLost:
|
|
@@ -40,34 +40,30 @@ def home() -> None:
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
def app_switcher() -> None:
|
|
43
|
-
"""Swipe up and hold:
|
|
43
|
+
"""Swipe up and hold: send the app-switcher gesture."""
|
|
44
|
+
ensure_frontmost()
|
|
44
45
|
b = get_bounds()
|
|
45
|
-
from Quartz import (
|
|
46
|
-
CGEventCreateMouseEvent, CGEventPost, CGEventSourceCreate,
|
|
47
|
-
kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGEventLeftMouseDragged,
|
|
48
|
-
kCGEventSourceStateHIDSystemState, kCGHIDEventTap, kCGMouseButtonLeft,
|
|
49
|
-
)
|
|
50
|
-
import time
|
|
51
|
-
|
|
52
46
|
x0, y0 = b.denormalize(0.5, 0.90)
|
|
53
47
|
x1, y1 = b.denormalize(0.5, 0.45)
|
|
54
|
-
src =
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
48
|
+
src = inp._source()
|
|
49
|
+
inp._require_focus() # F26 / R13 §3: before mouse-down
|
|
50
|
+
cx, cy = x0, y0
|
|
51
|
+
inp._mouse_event(inp.kCGEventLeftMouseDown, cx, cy, src)
|
|
52
|
+
try:
|
|
53
|
+
time.sleep(0.02)
|
|
54
|
+
# Eased drag to mid-screen.
|
|
55
|
+
steps = 15
|
|
56
|
+
for i in range(1, steps + 1):
|
|
57
|
+
t = i / steps
|
|
58
|
+
e = t * t * (3 - 2 * t) # smoothstep
|
|
59
|
+
nx, ny = x0 + (x1 - x0) * e, y0 + (y1 - y0) * e
|
|
60
|
+
inp._mouse_event(inp.kCGEventLeftMouseDragged, nx, ny, src)
|
|
61
|
+
cx, cy = nx, ny
|
|
62
|
+
time.sleep(0.016)
|
|
63
|
+
inp._require_focus() # R18: before the hold, the mirror must still be in front
|
|
64
|
+
time.sleep(0.55) # hold to trigger the switcher
|
|
65
|
+
finally:
|
|
66
|
+
inp._mouse_event(inp.kCGEventLeftMouseUp, cx, cy, src)
|
|
71
67
|
|
|
72
68
|
|
|
73
69
|
# Wheel pixels per window point of list movement, measured on iPhone Mirroring
|