noracu 0.2.5 → 0.2.7
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 +150 -0
- package/README.md +6 -3
- package/README.zh-CN.md +6 -3
- package/SECURITY.md +2 -2
- package/docs/architecture.md +3 -3
- package/docs/tools.md +9 -9
- package/docs/usage.md +6 -3
- package/imctl/pyproject.toml +1 -1
- package/imctl/src/imctl/__init__.py +1 -1
- package/imctl/src/imctl/cli.py +78 -14
- package/imctl/src/imctl/gestures.py +136 -13
- package/imctl/src/imctl/input.py +171 -23
- package/imctl/src/imctl/pointer.py +81 -0
- package/imctl/src/imctl/verify.py +29 -4
- package/imctl/src/imctl/window.py +39 -6
- package/package.json +3 -3
- package/src/mac-ax.js +53 -16
- package/src/server.mjs +177 -41
package/imctl/src/imctl/input.py
CHANGED
|
@@ -43,11 +43,20 @@ from Quartz import (
|
|
|
43
43
|
CGEventSetFlags,
|
|
44
44
|
)
|
|
45
45
|
|
|
46
|
-
from . import window
|
|
46
|
+
from . import pointer, window
|
|
47
47
|
from .window import WindowBounds, ensure_frontmost, get_bounds
|
|
48
48
|
|
|
49
49
|
TAP_INTERVAL_S = 0.05
|
|
50
50
|
LONG_PRESS_S = 0.8
|
|
51
|
+
# Device run 2026-11-07/08 (macOS 27.2): iPhone Mirroring pastes
|
|
52
|
+
# through Universal Clipboard, which tells the phone about new Mac clipboard content
|
|
53
|
+
# asynchronously. Once in about 30 pastes (23:33, Cmd+V ~0.5 s after the write) the phone pasted
|
|
54
|
+
# the text the Mac clipboard held BEFORE the write; shell pastes 0.4-3.4 s after the write landed
|
|
55
|
+
# 23/23 the same night, so the race is rare. type_text waits this long between its write and
|
|
56
|
+
# Cmd+V to narrow it. The value is a judgement, not a measured threshold, and it does not close
|
|
57
|
+
# the race. It does not address the other failure seen (the phone shows "Pasting from ..." and
|
|
58
|
+
# then pastes nothing), whose cause is not established.
|
|
59
|
+
PASTE_SETTLE_S = 1.5
|
|
51
60
|
SWIPE_STEPS = 20
|
|
52
61
|
SWIPE_NUDGE_PX = 3
|
|
53
62
|
SWIPE_DEFAULT_MS = 300
|
|
@@ -67,6 +76,13 @@ class FocusLost(InputError):
|
|
|
67
76
|
"""
|
|
68
77
|
|
|
69
78
|
|
|
79
|
+
class PasteFocusLost(FocusLost):
|
|
80
|
+
"""type_text's Cmd+V was called off: another app came to the front during
|
|
81
|
+
the wait after the clipboard write. Nothing was pasted (a replace's Cmd+A
|
|
82
|
+
had already been sent), and the clipboard went back under the changeCount
|
|
83
|
+
rule. Raised instead of bringing iPhone Mirroring forward again."""
|
|
84
|
+
|
|
85
|
+
|
|
70
86
|
# F26: consulted right before every synthesized event. Module-level so
|
|
71
87
|
# tests can swap it; window.is_frontmost asks System Events afresh on
|
|
72
88
|
# every call (F30: an in-process NSWorkspace answer is a stale snapshot).
|
|
@@ -139,6 +155,7 @@ def _mouse_event(kind: int, x: float, y: float, source) -> None:
|
|
|
139
155
|
if event is None:
|
|
140
156
|
raise InputError(f"CGEventCreateMouseEvent returned None for {kind}")
|
|
141
157
|
CGEventPost(kCGHIDEventTap, event)
|
|
158
|
+
pointer.moved_to(x, y) # 0.2.7: the CLI puts the pointer back after the command
|
|
142
159
|
|
|
143
160
|
|
|
144
161
|
def _key_event(keycode: int, down: bool, source) -> None:
|
|
@@ -302,6 +319,9 @@ _ANSI_KEYCODES = {
|
|
|
302
319
|
}
|
|
303
320
|
|
|
304
321
|
|
|
322
|
+
_ANSI_CHARS = {code: char for char, code in _ANSI_KEYCODES.items()}
|
|
323
|
+
|
|
324
|
+
|
|
305
325
|
def keycode_for_char(char: str) -> int | None:
|
|
306
326
|
"""ANSI virtual keycode for a single digit/letter, else None."""
|
|
307
327
|
if len(char) != 1:
|
|
@@ -309,7 +329,56 @@ def keycode_for_char(char: str) -> int | None:
|
|
|
309
329
|
return _ANSI_KEYCODES.get(char.lower())
|
|
310
330
|
|
|
311
331
|
|
|
312
|
-
|
|
332
|
+
class KeyboardLayoutMismatch(InputError):
|
|
333
|
+
"""The Mac's keyboard layout does not put the chord's character on its key.
|
|
334
|
+
|
|
335
|
+
Command chords are sent from ANSI key codes; iPhone Mirroring (a Mac
|
|
336
|
+
app) reads its own key equivalents through the Mac's layout first. On
|
|
337
|
+
French AZERTY key code 0 is "q": a ⌘A select-all would arrive as ⌘Q.
|
|
338
|
+
Raised before anything is posted (review 0.2.6 #5).
|
|
339
|
+
"""
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def _layout_char(keycode: int) -> str | None:
|
|
343
|
+
"""What the Mac's current keyboard layout types on ``keycode`` with ⌘ held.
|
|
344
|
+
|
|
345
|
+
The event is created, never posted; AppKit translates it through the
|
|
346
|
+
current input source. None when it cannot be read — the chord then goes
|
|
347
|
+
out as it did before this check.
|
|
348
|
+
"""
|
|
349
|
+
try:
|
|
350
|
+
from AppKit import NSEvent
|
|
351
|
+
|
|
352
|
+
event = CGEventCreateKeyboardEvent(_source(), keycode, True)
|
|
353
|
+
if event is None:
|
|
354
|
+
return None
|
|
355
|
+
CGEventSetFlags(event, kCGEventFlagMaskCommand)
|
|
356
|
+
ns = NSEvent.eventWithCGEvent_(event)
|
|
357
|
+
chars = ns.charactersIgnoringModifiers() if ns is not None else None
|
|
358
|
+
return str(chars) if chars else None
|
|
359
|
+
except Exception:
|
|
360
|
+
return None
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
# Module-level so tests can swap it (like _focus_check).
|
|
364
|
+
_layout_check = _layout_char
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def check_chord_layout(keycode: int) -> None:
|
|
368
|
+
"""Refuse a ⌘ chord whose ANSI key types another character in the Mac's layout."""
|
|
369
|
+
expect = _ANSI_CHARS.get(keycode)
|
|
370
|
+
if expect is None:
|
|
371
|
+
return
|
|
372
|
+
got = _layout_check(keycode)
|
|
373
|
+
if got is None or got.lower() == expect:
|
|
374
|
+
return
|
|
375
|
+
raise KeyboardLayoutMismatch(
|
|
376
|
+
f'the Mac keyboard layout types "{got}" on the key that ⌘{expect.upper()} is sent from '
|
|
377
|
+
f"(key code {keycode}), so iPhone Mirroring would get ⌘{got.upper()} — nothing was sent; "
|
|
378
|
+
"switch the Mac to a QWERTY-style input source (U.S., ABC, Pinyin) and retry")
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def _cmd_chord(keycode: int, before_key=None, activate: bool = True) -> None:
|
|
313
382
|
"""Send a real Command chord: ⌘ down → key down → key up → ⌘ up.
|
|
314
383
|
|
|
315
384
|
iPhone Mirroring forwards the *state* of the physical modifier
|
|
@@ -323,8 +392,16 @@ def _cmd_chord(keycode: int, before_key=None) -> None:
|
|
|
323
392
|
``before_key`` runs after the focus checks, right before the first
|
|
324
393
|
event: the caller's last chance to call the chord off by raising —
|
|
325
394
|
nothing is posted then (F35).
|
|
395
|
+
|
|
396
|
+
``activate=False``: never bring iPhone Mirroring forward — when another
|
|
397
|
+
app is frontmost, raise FocusLost before anything is posted. type_text's
|
|
398
|
+
⌘V uses it: by then the user may have moved to another app during the
|
|
399
|
+
wait after the clipboard write, and pulling the mirror back would send
|
|
400
|
+
the paste (and their next keystrokes) to the phone.
|
|
326
401
|
"""
|
|
327
|
-
|
|
402
|
+
check_chord_layout(keycode) # review 0.2.6 #5: before anything is sent
|
|
403
|
+
if activate:
|
|
404
|
+
ensure_frontmost()
|
|
328
405
|
_require_focus() # F26: before the chord's first event
|
|
329
406
|
if before_key is not None:
|
|
330
407
|
before_key()
|
|
@@ -511,7 +588,8 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
511
588
|
sleep=time.sleep, clock=time.monotonic,
|
|
512
589
|
timeout_s: float = 2.5, poll_s: float = 0.15,
|
|
513
590
|
min_hold_s: float = 0.3, pending: list | None = None,
|
|
514
|
-
report: dict | None = None, restore: bool = True
|
|
591
|
+
report: dict | None = None, restore: bool = True,
|
|
592
|
+
settle_s: float = PASTE_SETTLE_S) -> bool:
|
|
515
593
|
"""Type text via clipboard paste (IME-safe), confirmed on screen.
|
|
516
594
|
|
|
517
595
|
Direct CGEvent keystroke synthesis breaks under CJK IMEs and is
|
|
@@ -582,7 +660,8 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
582
660
|
search-history chip with the same query, an older message) licensed a
|
|
583
661
|
restore at once, and a paste that the phone read late could then paste
|
|
584
662
|
the user's ORIGINAL clipboard. The default checks now count the text's
|
|
585
|
-
probe before the pasteboard write
|
|
663
|
+
probe right before Cmd+V (after the pasteboard write and the
|
|
664
|
+
``settle_s`` wait) — the higher of two reads 0.3 s
|
|
586
665
|
apart, so a read that misses an occurrence cannot fake a rise — and
|
|
587
666
|
license a restore only when the count RISES over it, in two reads 0.3 s
|
|
588
667
|
apart. Without a pre-paste count (a read failed) OCR never licenses a
|
|
@@ -591,6 +670,18 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
591
670
|
count as ``count_before`` (None: unknown), for a caller that keeps
|
|
592
671
|
watching after a False result.
|
|
593
672
|
|
|
673
|
+
``settle_s`` (device run 2026-11-07/08): the wait between writing the
|
|
674
|
+
pasteboard and Cmd+V. The phone reads the Mac clipboard through Universal
|
|
675
|
+
Clipboard, which announces new content asynchronously; a Cmd+V that
|
|
676
|
+
arrives first pastes the phone's OWN clipboard (an older paste, or
|
|
677
|
+
something copied on the phone) instead of ``text``. The wait lowers that
|
|
678
|
+
chance; it cannot rule it out, so an unconfirmed paste is still reported
|
|
679
|
+
as unconfirmed and the caller reads the field. The pre-paste reads come
|
|
680
|
+
after the wait, so the write-to-Cmd+V time is ``settle_s`` plus those
|
|
681
|
+
reads. Cmd+V never brings iPhone Mirroring back to the front: when
|
|
682
|
+
another app took it during the wait, FocusLost (nothing pasted; the
|
|
683
|
+
clipboard goes back under the changeCount rule).
|
|
684
|
+
|
|
594
685
|
``restore=False``: once Cmd+V may be on its way the user's clipboard is
|
|
595
686
|
never put back — no restore here and no PendingRestore — and it keeps
|
|
596
687
|
``text``; only an error before Cmd+V still restores it. open_app uses
|
|
@@ -624,9 +715,27 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
624
715
|
|
|
625
716
|
from . import verify, vision
|
|
626
717
|
|
|
718
|
+
# review 0.2.6 #5: both chords are checked against the Mac's keyboard layout before anything is
|
|
719
|
+
# sent, the mirror is brought forward or the clipboard is touched (the chord checks again itself)
|
|
720
|
+
if replace:
|
|
721
|
+
check_chord_layout(_VK_A)
|
|
722
|
+
check_chord_layout(_VK_V)
|
|
627
723
|
ensure_frontmost()
|
|
724
|
+
default_chord = chord is None
|
|
628
725
|
if chord is None:
|
|
629
726
|
chord = _cmd_chord
|
|
727
|
+
|
|
728
|
+
def paste_chord(keycode, before_key=None):
|
|
729
|
+
# the ⌘V after the wait never re-activates the mirror: PasteFocusLost instead
|
|
730
|
+
# (_cmd_chord raises FocusLost only before its first event)
|
|
731
|
+
try:
|
|
732
|
+
return _cmd_chord(keycode, before_key=before_key, activate=False)
|
|
733
|
+
except PasteFocusLost:
|
|
734
|
+
raise
|
|
735
|
+
except FocusLost as exc:
|
|
736
|
+
raise PasteFocusLost(str(exc)) from exc
|
|
737
|
+
else:
|
|
738
|
+
paste_chord = chord # injected (tests): one chord for both keys
|
|
630
739
|
if pasteboard is None:
|
|
631
740
|
pasteboard = NSPasteboard.generalPasteboard()
|
|
632
741
|
# F27: a single character is invisible to the default OCR probe
|
|
@@ -646,6 +755,13 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
646
755
|
count_before = [None]
|
|
647
756
|
# 0.2.5: where the probe showed before the paste (None = unknown → no confirmation)
|
|
648
757
|
spots_before = [None]
|
|
758
|
+
strip_before = [[]] # and inside the banner strip (review 0.2.6 #7: explains, never confirms)
|
|
759
|
+
# review 0.2.6 #7: why OCR could not confirm a paste the frame diff saw — "too_short" (fewer
|
|
760
|
+
# than VERIFY_MIN_PROBE characters, never confirmed) or "strip_only" (the text appeared only in
|
|
761
|
+
# the banner strip at the top, where a field cannot be told from a banner)
|
|
762
|
+
unverifiable = [None]
|
|
763
|
+
if multi and len(verify.text_probe(text)) < verify.VERIFY_MIN_PROBE:
|
|
764
|
+
unverifiable[0] = "too_short"
|
|
649
765
|
|
|
650
766
|
def sweep_read():
|
|
651
767
|
return vision.read_screen(language_correction=False, sweep=True)
|
|
@@ -707,6 +823,10 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
707
823
|
or verify.probe_count(text, hits) <= count_before[0]
|
|
708
824
|
or spots_before[0] is None
|
|
709
825
|
or not verify.has_new_spot(spots_before[0], verify.probe_spots(text, hits))):
|
|
826
|
+
if (unverifiable[0] is None and not verify.shows_paste_hud(hits)
|
|
827
|
+
and verify.probe_count(text, hits) > count_before[0]
|
|
828
|
+
and verify.has_new_spot(strip_before[0], verify.strip_spots(text, hits))):
|
|
829
|
+
unverifiable[0] = "strip_only"
|
|
710
830
|
return False
|
|
711
831
|
return True
|
|
712
832
|
return verify.screen_shows_text(text, vision.read_screen)
|
|
@@ -719,12 +839,40 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
719
839
|
chord(_VK_A) # select the focused field's content first (see docstring)
|
|
720
840
|
sleep(0.05)
|
|
721
841
|
|
|
842
|
+
# the change count first: a copy that lands while the clipboard is being saved must not be
|
|
843
|
+
# overwritten by our write
|
|
844
|
+
saved_change = pasteboard.changeCount()
|
|
845
|
+
saved = save_pasteboard(pasteboard)
|
|
846
|
+
if pasteboard.changeCount() != saved_change:
|
|
847
|
+
saved_change = None # nothing of ours to undo
|
|
848
|
+
raise ClipboardChanged(
|
|
849
|
+
"another app or device changed the clipboard while it was being saved; "
|
|
850
|
+
"nothing was typed and the clipboard was left as it is")
|
|
851
|
+
|
|
852
|
+
pasteboard.clearContents()
|
|
853
|
+
pasteboard.setString_forType_(text, "public.utf8-plain-text")
|
|
854
|
+
# Device run 2026-11-07/08: let Universal Clipboard tell the phone about the new content
|
|
855
|
+
# before Cmd+V (PASTE_SETTLE_S). The pre-paste reads, the "before" frame, the chord's
|
|
856
|
+
# focus check and last_check (the changeCount guard) all come after the wait, so each
|
|
857
|
+
# describes the moment right before Cmd+V.
|
|
858
|
+
sleep(max(0.05, settle_s))
|
|
859
|
+
# 0.2.7: the wait is when someone copies something or moves to another app (or Space):
|
|
860
|
+
# say so now, before any screen read — the same checks run again right before Cmd+V
|
|
861
|
+
# focus first: when both happened, the user moved away — a caller must not take the
|
|
862
|
+
# clipboard change for a reason to bring the mirror forward again (open_app tidies on it)
|
|
863
|
+
if default_chord and not _focus_check():
|
|
864
|
+
raise PasteFocusLost("iPhone Mirroring is not the frontmost app")
|
|
865
|
+
if pasteboard.changeCount() != saved_change + 1:
|
|
866
|
+
raise ClipboardChanged(
|
|
867
|
+
"another app or device changed the clipboard during the wait before the paste; "
|
|
868
|
+
"nothing was typed and the clipboard was left as it is")
|
|
869
|
+
|
|
722
870
|
if pre_read:
|
|
723
871
|
# F27: the pre-paste sweep for a single character, taken on a
|
|
724
872
|
# still screen only (a mid-transition read could miss a label
|
|
725
873
|
# that is already there and later pass it off as the paste).
|
|
726
|
-
#
|
|
727
|
-
#
|
|
874
|
+
# Taken after the clipboard write and the wait (0.2.7), so
|
|
875
|
+
# the baseline is the screen right before Cmd+V.
|
|
728
876
|
try:
|
|
729
877
|
if verify.wait_still(sleep=sleep, clock=clock):
|
|
730
878
|
sweep_before[0] = sweep_read()
|
|
@@ -738,6 +886,9 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
738
886
|
# 0.3 s apart (a redraw or the select-all highlight can hide an
|
|
739
887
|
# occurrence from one read). A capture failure in either leaves it
|
|
740
888
|
# unknown: then OCR can never license a restore for this paste.
|
|
889
|
+
# 0.2.7: read after the clipboard write and the wait, so
|
|
890
|
+
# text that appears during the wait is part of the baseline and
|
|
891
|
+
# can never pass for the paste (a restore license).
|
|
741
892
|
try:
|
|
742
893
|
r1 = vision.read_screen()
|
|
743
894
|
first = verify.probe_count(text, r1)
|
|
@@ -746,6 +897,7 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
746
897
|
count_before[0] = max(first, verify.probe_count(text, r2))
|
|
747
898
|
# both reads: a spot either one saw is not new after the paste
|
|
748
899
|
spots_before[0] = verify.probe_spots(text, r1) + verify.probe_spots(text, r2)
|
|
900
|
+
strip_before[0] = verify.strip_spots(text, r1) + verify.strip_spots(text, r2)
|
|
749
901
|
except Exception as exc:
|
|
750
902
|
if not verify.is_capture_error(exc):
|
|
751
903
|
raise
|
|
@@ -754,21 +906,14 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
754
906
|
if report is not None:
|
|
755
907
|
report["count_before"] = count_before[0]
|
|
756
908
|
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
"nothing was typed and the clipboard was left as it is")
|
|
766
|
-
|
|
767
|
-
pasteboard.clearContents()
|
|
768
|
-
pasteboard.setString_forType_(text, "public.utf8-plain-text")
|
|
769
|
-
sleep(0.05)
|
|
770
|
-
|
|
771
|
-
changed = before_check()
|
|
909
|
+
try:
|
|
910
|
+
changed = before_check()
|
|
911
|
+
except Exception as exc:
|
|
912
|
+
# the mirror window left the screen during the reads (another Space came to the
|
|
913
|
+
# front): that is the user moving away, not a broken capture — call the paste off
|
|
914
|
+
if default_chord and verify.is_capture_error(exc) and not _focus_check():
|
|
915
|
+
raise PasteFocusLost("iPhone Mirroring is not the frontmost app") from exc
|
|
916
|
+
raise
|
|
772
917
|
|
|
773
918
|
def last_check():
|
|
774
919
|
# F35: the last look at the pasteboard, after the chord's focus
|
|
@@ -783,7 +928,7 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
783
928
|
sent[0] = True
|
|
784
929
|
|
|
785
930
|
# real ⌘ chord — flags alone reach the phone as "v" (F14)
|
|
786
|
-
|
|
931
|
+
paste_chord(_VK_V, before_key=last_check) # never re-activates the mirror after the wait
|
|
787
932
|
t0 = clock()
|
|
788
933
|
|
|
789
934
|
# landed: did the paste show up on screen? (decides the return
|
|
@@ -834,6 +979,8 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
834
979
|
# confirmed a paste that never reached the field — the caller must
|
|
835
980
|
# not report such a paste as verified
|
|
836
981
|
report["seen"] = bool(restore_ok)
|
|
982
|
+
if not restore_ok and unverifiable[0] is not None:
|
|
983
|
+
report["unverifiable"] = unverifiable[0]
|
|
837
984
|
|
|
838
985
|
if not landed:
|
|
839
986
|
# F18 invariant: never restore unconfirmed — a late paste
|
|
@@ -869,6 +1016,7 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
869
1016
|
|
|
870
1017
|
def clear_text() -> None:
|
|
871
1018
|
"""Select-all and delete in the focused field."""
|
|
1019
|
+
check_chord_layout(_VK_A) # review 0.2.6 #5: before the mirror is brought forward
|
|
872
1020
|
ensure_frontmost()
|
|
873
1021
|
_cmd_chord(_VK_A) # Cmd+A — real chord, same reason as type_text
|
|
874
1022
|
time.sleep(0.05)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Put the Mac pointer back after a phone gesture (0.2.7, a Mac shared with a person).
|
|
2
|
+
|
|
3
|
+
A tap, swipe, long press or scroll moves the real pointer onto the mirror
|
|
4
|
+
window (a mouse event with a location moves it). When the Mac is shared
|
|
5
|
+
(NORACU_USER_IDLE_S is not 0 — the same variable and rule as the noracu
|
|
6
|
+
server), the CLI remembers where the pointer was before a command and, after
|
|
7
|
+
it, warps it back — only when the pointer is still where noracu's own last
|
|
8
|
+
event left it: if someone moved it meanwhile, it stays where they put it.
|
|
9
|
+
A warp posts no event (the person's idle time is not reset by it).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import os
|
|
15
|
+
import re
|
|
16
|
+
import time
|
|
17
|
+
|
|
18
|
+
_home = None # where the pointer was before the command (None: not tracking)
|
|
19
|
+
_last = None # where noracu's last mouse event put it (None: no mouse event yet)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def enabled() -> bool:
|
|
23
|
+
"""Off only for a plain zero ("0", "0.0"), like the server; unset or unreadable means on."""
|
|
24
|
+
raw = os.environ.get("NORACU_USER_IDLE_S", "").strip()
|
|
25
|
+
return not re.fullmatch(r"0+(\.0+)?", raw)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _where():
|
|
29
|
+
try:
|
|
30
|
+
import Quartz # type: ignore
|
|
31
|
+
|
|
32
|
+
ev = Quartz.CGEventCreate(None)
|
|
33
|
+
if ev is None:
|
|
34
|
+
return None
|
|
35
|
+
p = Quartz.CGEventGetLocation(ev)
|
|
36
|
+
return (float(p.x), float(p.y))
|
|
37
|
+
except Exception:
|
|
38
|
+
return None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _warp(p) -> None:
|
|
42
|
+
import Quartz # type: ignore
|
|
43
|
+
|
|
44
|
+
Quartz.CGWarpMouseCursorPosition(p)
|
|
45
|
+
Quartz.CGAssociateMouseAndMouseCursorPosition(True)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def remember() -> None:
|
|
49
|
+
"""Called once before a command runs."""
|
|
50
|
+
global _home, _last
|
|
51
|
+
_last = None
|
|
52
|
+
_home = _where() if enabled() else None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def moved_to(x: float, y: float) -> None:
|
|
56
|
+
"""Called after every mouse event noracu posts."""
|
|
57
|
+
global _last
|
|
58
|
+
_last = (float(x), float(y))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def restore() -> bool:
|
|
62
|
+
"""Called once after a command (also after an error). True when the pointer was put back."""
|
|
63
|
+
global _home, _last
|
|
64
|
+
home, last = _home, _last
|
|
65
|
+
_home = _last = None
|
|
66
|
+
if home is None or last is None:
|
|
67
|
+
return False
|
|
68
|
+
def still_ours() -> bool:
|
|
69
|
+
now = _where()
|
|
70
|
+
return now is not None and abs(now[0] - last[0]) <= 2 and abs(now[1] - last[1]) <= 2
|
|
71
|
+
|
|
72
|
+
if not still_ours():
|
|
73
|
+
return False # someone moved it: leave it where they put it
|
|
74
|
+
try:
|
|
75
|
+
time.sleep(0.1) # let the last posted events reach the mirror before the pointer leaves it
|
|
76
|
+
if not still_ours():
|
|
77
|
+
return False # moved during that wait
|
|
78
|
+
_warp(home)
|
|
79
|
+
return True
|
|
80
|
+
except Exception:
|
|
81
|
+
return False
|
|
@@ -216,6 +216,23 @@ def probe_spots(text: str, hits) -> list[tuple[float, float]] | None:
|
|
|
216
216
|
return spots
|
|
217
217
|
|
|
218
218
|
|
|
219
|
+
def strip_spots(text: str, hits) -> list[tuple[float, float]]:
|
|
220
|
+
"""Where ``text``'s probe shows inside the banner strip at the top (the spots probe_spots leaves
|
|
221
|
+
out), the paste-progress box aside; [] when the text is too short to be confirmed at all. Only
|
|
222
|
+
explains an unconfirmed paste (review 0.2.6 #7) — never confirms one."""
|
|
223
|
+
probe = text_probe(text)
|
|
224
|
+
if probe is None or len(probe) < VERIFY_MIN_PROBE:
|
|
225
|
+
return []
|
|
226
|
+
spots = []
|
|
227
|
+
for hit in hits:
|
|
228
|
+
nx, ny = getattr(hit, "nx", None), getattr(hit, "ny", None)
|
|
229
|
+
if nx is None or ny is None or ny >= BANNER_STRIP_NY or _is_paste_hud(hit):
|
|
230
|
+
continue
|
|
231
|
+
if probe in _compact(getattr(hit, "text", hit)):
|
|
232
|
+
spots.append((nx, ny))
|
|
233
|
+
return spots
|
|
234
|
+
|
|
235
|
+
|
|
219
236
|
def has_new_spot(before, after, eps: float = 0.03) -> bool:
|
|
220
237
|
"""True when some spot in ``after`` has no spot of ``before`` within ``eps`` in both axes."""
|
|
221
238
|
return any(all(abs(ax - bx) > eps or abs(ay - by) > eps for bx, by in before) for ax, ay in after)
|
|
@@ -246,10 +263,18 @@ def _is_paste_hud(hit) -> bool:
|
|
|
246
263
|
|
|
247
264
|
|
|
248
265
|
def shows_paste_hud(hits) -> bool:
|
|
249
|
-
"""True when
|
|
250
|
-
|
|
251
|
-
shows, a paste is still on its way to the phone.
|
|
252
|
-
|
|
266
|
+
"""True when the OCR read shows the phone's "Pasting from …" progress box
|
|
267
|
+
(compared like probes: NFKC, lowercase, whitespace removed). While it
|
|
268
|
+
shows, a paste is still on its way to the phone.
|
|
269
|
+
|
|
270
|
+
0.2.7: one hit, or two consecutive hits read together — OCR can
|
|
271
|
+
split "Pasting" and "from" into two hits. Only neighbours are joined
|
|
272
|
+
(not the whole screen), and a false match can only block a restore or a
|
|
273
|
+
confirmation (fail-closed), never license one."""
|
|
274
|
+
texts = [str(getattr(hit, "text", hit)) for hit in hits]
|
|
275
|
+
if any(PASTE_HUD_PROBE in _compact(t) for t in texts):
|
|
276
|
+
return True
|
|
277
|
+
return any(PASTE_HUD_PROBE in _compact(a + b) for a, b in zip(texts, texts[1:]))
|
|
253
278
|
|
|
254
279
|
|
|
255
280
|
def screen_shows_text(text: str, read_screen) -> bool:
|
|
@@ -43,13 +43,34 @@ class WindowBounds:
|
|
|
43
43
|
)
|
|
44
44
|
|
|
45
45
|
|
|
46
|
+
# Device run 2026-11-07: while the phone's keyboard is composing (a Pinyin "x" with its underline),
|
|
47
|
+
# iPhone Mirroring adds a 397x28 AXDialog window — the candidate bar — and System Events lists it as
|
|
48
|
+
# window 1, ahead of the phone (AXStandardWindow). Every script that reads the mirror window starts
|
|
49
|
+
# with this static block: mi is the index of the first AXStandardWindow, else 1 as before. Fixed text
|
|
50
|
+
# joined into the scripts below — no runtime value is interpolated (invariant 1).
|
|
51
|
+
# Device run 2026-11-08 03:44: the first version kept `contents of cand`, which System Events turns
|
|
52
|
+
# into a reference BY NAME (window "iPhone Mirroring"); during a connection blip that name did not
|
|
53
|
+
# resolve and `position of` failed with -1728. The index is re-read on every call, like window 1 was.
|
|
54
|
+
_PICK_WINDOW = '''
|
|
55
|
+
set mi to 1
|
|
56
|
+
repeat with i from 1 to (count of windows)
|
|
57
|
+
try
|
|
58
|
+
if ((subrole of window i) as text) is "AXStandardWindow" then
|
|
59
|
+
set mi to i
|
|
60
|
+
exit repeat
|
|
61
|
+
end if
|
|
62
|
+
end try
|
|
63
|
+
end repeat
|
|
64
|
+
'''
|
|
65
|
+
|
|
46
66
|
_GET_BOUNDS_SCRIPT = '''
|
|
47
67
|
tell application "System Events"
|
|
48
68
|
tell process "iPhone Mirroring"
|
|
49
69
|
set winCount to count of windows
|
|
50
70
|
if winCount is 0 then return "NO_WINDOW"
|
|
51
|
-
|
|
52
|
-
set {
|
|
71
|
+
''' + _PICK_WINDOW + '''
|
|
72
|
+
set {x, y} to position of window mi
|
|
73
|
+
set {w, h} to size of window mi
|
|
53
74
|
return (x as text) & "," & (y as text) & "," & (w as text) & "," & (h as text)
|
|
54
75
|
end tell
|
|
55
76
|
end tell
|
|
@@ -155,7 +176,15 @@ def get_bounds() -> WindowBounds:
|
|
|
155
176
|
Raises WindowError if the app is not running or has no window
|
|
156
177
|
(e.g. the window was closed but the process lives on).
|
|
157
178
|
"""
|
|
158
|
-
|
|
179
|
+
try:
|
|
180
|
+
out = _run_osascript(_GET_BOUNDS_SCRIPT)
|
|
181
|
+
except WindowError:
|
|
182
|
+
# device run 2026-11-08 03:44: System Events lost the window for a moment during a
|
|
183
|
+
# connection blip (-1728). A read sends nothing to the phone, so one retry is safe.
|
|
184
|
+
import time
|
|
185
|
+
|
|
186
|
+
time.sleep(0.3)
|
|
187
|
+
out = _run_osascript(_GET_BOUNDS_SCRIPT)
|
|
159
188
|
if out == "NO_WINDOW":
|
|
160
189
|
# Window closed but process alive: re-activate to restore it.
|
|
161
190
|
subprocess.run(
|
|
@@ -180,7 +209,7 @@ def get_bounds() -> WindowBounds:
|
|
|
180
209
|
return WindowBounds(x=x, y=y, width=w, height=h)
|
|
181
210
|
|
|
182
211
|
|
|
183
|
-
# Language-independent mirror-state probe. When the mirroring session is live, window
|
|
212
|
+
# Language-independent mirror-state probe. When the mirroring session is live, the mirror window (window mi, see _PICK_WINDOW) contains a
|
|
184
213
|
# single group (the AXHostingView) with no children; when an overlay is shown (paused, connecting,
|
|
185
214
|
# "iPhone in use", …) that group gains child elements (image / static text / button). Overlay text
|
|
186
215
|
# is localized, so the decision never matches on text — only on structure (child count, classes).
|
|
@@ -212,7 +241,8 @@ tell application "System Events"
|
|
|
212
241
|
tell process "iPhone Mirroring"
|
|
213
242
|
set winCount to count of windows
|
|
214
243
|
if winCount is 0 then return "NO_WINDOW"
|
|
215
|
-
|
|
244
|
+
''' + _PICK_WINDOW + '''
|
|
245
|
+
tell window mi
|
|
216
246
|
if not (exists group 1) then return "NO_GROUP"
|
|
217
247
|
set els to UI elements of group 1
|
|
218
248
|
if (count of els) is 0 then return ""
|
|
@@ -259,7 +289,8 @@ end tell
|
|
|
259
289
|
_CLICK_RESUME_SCRIPT = '''
|
|
260
290
|
tell application "System Events"
|
|
261
291
|
tell process "iPhone Mirroring"
|
|
262
|
-
|
|
292
|
+
''' + _PICK_WINDOW + '''
|
|
293
|
+
tell window mi
|
|
263
294
|
set target to missing value
|
|
264
295
|
set n to 0
|
|
265
296
|
repeat with b in (buttons of group 1)
|
|
@@ -513,6 +544,8 @@ def _move_pointer_off_window() -> bool:
|
|
|
513
544
|
None, Quartz.kCGEventMouseMoved, (x, y), Quartz.kCGMouseButtonLeft
|
|
514
545
|
)
|
|
515
546
|
Quartz.CGEventPost(Quartz.kCGHIDEventTap, ev)
|
|
547
|
+
from . import pointer
|
|
548
|
+
pointer.moved_to(x, y) # 0.2.7: the CLI puts the pointer back after the command
|
|
516
549
|
return True
|
|
517
550
|
except Exception:
|
|
518
551
|
return False
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "noracu",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"mcpName": "io.github.sgInnora/noracu",
|
|
5
5
|
"description": "Local MCP server: macOS apps and a browser by element index, plus iPhone Mirroring (experimental).",
|
|
6
6
|
"type": "module",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"test": "node test/engine-unit.mjs && node test/e2e.mjs",
|
|
14
14
|
"test:ci": "node test/engine-unit.mjs && NORACU_CI=1 node test/e2e.mjs",
|
|
15
15
|
"test:imctl": "cd imctl && ${NORACU_PYTHON:-python3} tests/test_smoke.py",
|
|
16
|
-
"check": "node --check src/server.mjs && node --check src/mac-ax.js && node --check scripts/gen-tools.mjs && python3 -c \"import ast,sys; [ast.parse(open(f).read(), f) for f in sys.argv[1:]]\" imctl/src/imctl/*.py",
|
|
17
|
-
"prepublishOnly": "npm run check && node scripts/check-pack.mjs && node scripts/gen-tools.mjs && git diff --exit-code docs/tools.md"
|
|
16
|
+
"check": "node --check src/server.mjs && node --check src/mac-ax.js && node --check scripts/gen-tools.mjs && node --check scripts/release-gate.mjs && python3 -c \"import ast,sys; [ast.parse(open(f).read(), f) for f in sys.argv[1:]]\" imctl/src/imctl/*.py",
|
|
17
|
+
"prepublishOnly": "npm run check && node scripts/release-gate.mjs && node scripts/check-pack.mjs && node scripts/gen-tools.mjs && git diff --exit-code docs/tools.md"
|
|
18
18
|
},
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": ">=20"
|