Lynsi 4.0 Secondary Display Architecture: App Store Split, Isolated Stream, and Touch Injection
Lynsi 4.0.0 is now live on both the Mac App Store and AppGallery, turning a HarmonyOS phone or tablet into a Mac secondary display. Here is why the virtual display helper had to be split out, why video runs on an isolated TLS connection, and why injecting touch into macOS differs completely from controlling HarmonyOS.
Lynsi 4.0.0 is now live on both the Mac App Store and Huawei AppGallery (4.0.0 / 1000016), adding “Use phone or tablet as a Mac secondary display”: screen mirroring, single-window casting, and an extended @2x Retina desktop up to 60 fps over LAN or USB AOA, with optional touch control.
Looking back from the September feasibility study (PHONE_AS_MAC_DISPLAY_RESEARCH.md) and October architecture plan (PHONE_AS_MAC_DISPLAY_PLAN.md) to shipping 4.0.0, the hardest problems weren’t H.264 encoder flags—they were three structural boundaries.
1. Why Extended Desktop Is Split Into a Companion Helper App
macOS offers zero public APIs to create a virtual display. Checking all 11 frameworks in the DriverKit 27.0 SDK confirmed that VideoDriverKit only covers CoreMedia I/O capture devices (cameras), not display outputs.
The only working software path is the private CGVirtualDisplay family (Descriptor, Settings, Mode), which violates App Review Guideline 2.5.1. Putting it inside the main binary would keep Lynsi out of the Mac App Store permanently.
So 4.0.0 splits responsibilities cleanly:
- Main app
Lynsi.app(shared across MAS and direct builds): Uses publicScreenCaptureKit(#available(macOS 12.3, *)),VideoToolboxH.264 hardware encoding, andCGEvent. Store builds compile with-DAPP_STORE_BUILD, stripping every private symbol (nm -gUfinds 0VirtualDisplaysymbols;stringsfinds 0CGVirtualDisplayorbootstrap_registerstrings) and containing zero links or prompts pointing to external downloads. Standalone,Lynsi.apphandles full-screen mirroring (SCDisplay) and single-window casting (SCWindow). - Free companion
LynsiVirtualDisplay.app(macos/DisplayHelper/, Universalarm64 + x86_64, Developer ID signed with Hardened Runtime): Creates, renews (3s lease / 20s timeout), and tears down@2xvirtual displays.Lynsi.apppassively discovers Mach portC4T9277GDX.me.mahui.app.lynsi.vdviabootstrap_look_up. On the helper side, Mach trailer verification (MACH_RCV_TRAILER_AUDIT+kSecGuestAttributeAudit) validates the caller’s Apple Developer Team ID (C4T9277GDX) and signature from the kernel’saudit_token_t, ignoring self-reported PIDs.
One counterintuitive finding from September testing (2.1): setting hiDPI = 1 alone does not yield @2x Retina scaling. macOS decides @2x from the PPI implied by sizeInMillimeters, granting it only around 180–260 ppi. Reporting a phone’s true 6.5-inch size (461 ppi) gives 1x (2720×1260 tiny points), and creating a 1360×630 display directly fails to come online. You have to report virtual physical dimensions in that ~180–260 ppi band—and always verify online status via CGGetOnlineDisplayList rather than trusting applySettings: returning YES.
2. Why Secondary Display Needs an Isolated Connection
Lynsi’s primary connection is a single ordered TLS stream carrying the 5 < 12 < 15 < 20 heartbeat (replyTimeoutMs = 15s), file transfers, and clipboard sync.
Reverse display video runs at 12–25 Mb/s up to 60 fps. Pushing that volume down the primary FIFO socket means any Wi-Fi hiccup queues ping and pong frames behind buffered video—hitting the 15-second heartbeat timeout and tearing down file transfers and clipboard sync along with the display.
4.0.0 separates control from data:
- Control stays on
primary: Only lightweightdisplay.invite,display.open,display.offer,display.state,display.stop, andhello(extDisplay='1') flow over the primary link. - Video and touch use an isolated bypass (
purpose='display'): Authorized via a 30-second single-usedisplayTicketbound to(sessionId, deviceId), occupying its owndisplayTokenslot inHandoffServerwithout displacingprimaryor the threeshareslots (and sharing USB AOA’s127.0.0.1:47288tunnel up to 5 channels). - Bounded queues and off-main-thread decoding: On macOS,
DisplayStreamOutboundQueuecaps in-flight frames at 8 and queue age at 150 ms, tracking keyframe debt to request an IDR (throttled to 200 ms) whenever frames drop. On HarmonyOS,DisplayVideoWorker.etsruns the pinnedTLSSocketinside a dedicatedThreadWorkerand feeds Annex B access units directly to C++display_decoder.cpp(OH_VideoDecoderzero-copy intoXComponentSurface) so 60 fps packets never stall the ArkUI main thread.
3. Touch Injection Into macOS vs. Controlling HarmonyOS
Even though both look like “touch control,” injecting touch into macOS (4.0) is fundamentally different from controlling HarmonyOS from a Mac (3.0):
- Injecting into HarmonyOS (3.0): No public app-level API exists for cross-app input injection, forcing reliance on developer mode,
hdc, anduitest—subject toUiOpArgs.keyHoldMs_(100 ms default key hold) and needing a BLE HID identity workaround just to enter hardware-keyboard IME mode. - Injecting into macOS (4.0): The Mac is injecting events into itself. Once the user grants Accessibility (
CGPreflightPostEventAccess/CGRequestPostEventAccess),CGEvent(...).post(tap: .cghidEventTap)is a standard public API.
The engineering invariants sit around coordinate mapping and cleanup: MacDisplayOverlay.ets normalizes coordinates strictly inside the letterboxed content rectangle, and macOS maps them onto the virtual display’s desktop bounds (supporting pixel-level two-finger scrolling via scrollWheelEvent2Source). High-frequency drags and scrolls coalesce in flight while press/up/click events stay strictly ordered. Any focus loss, rotation, disconnect, pause, or toggle-off immediately releases held mouse buttons—while read-only extended display works cleanly even if Accessibility permission is never granted.
When adding high-bandwidth real-time features to an existing cross-device app, channel isolation and store-boundary separation have to come before frame-rate tuning—otherwise a faster video stream just breaks the connections users already rely on.
Comments