diff --git a/.github/workflows/secret-scan.yml b/.github/workflows/secret-scan.yml new file mode 100644 index 00000000..e07f47b0 --- /dev/null +++ b/.github/workflows/secret-scan.yml @@ -0,0 +1,22 @@ +name: Secret scan + +# Scans for committed secrets on pushes to main and on pull requests. + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + gitleaks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: gitleaks/gitleaks-action@v2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6f0f2ce7..dce2bfd9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,3 +17,12 @@ jobs: - uses: actions/checkout@v4 - run: npm ci - run: npm test + + # The shared Kotlin tests (tests/kotlin, on the JVM host). The runner image + # already carries the JDK 17 and Android SDK the build needs. + test-kotlin: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: npm ci + - run: npm run test:kotlin diff --git a/.gitleaks.toml b/.gitleaks.toml new file mode 100644 index 00000000..e87ce09b --- /dev/null +++ b/.gitleaks.toml @@ -0,0 +1,9 @@ +# Secret-scanning config for the workflow in .github/workflows/secret-scan.yml. +[extend] +useDefault = true + +[[rules]] +id = "generic-api-key" + [rules.allowlist] + description = "SIGN_KEY in even-api.ts is a client-side constant the app needs to sign EvenHub requests, not a credential." + paths = ['''app/apps/evenhub/even-api\.ts'''] diff --git a/app/apps/evenhub/privacy-policy.ts b/app/apps/evenhub/privacy-policy.ts index 53fc2681..e5e0c0be 100644 --- a/app/apps/evenhub/privacy-policy.ts +++ b/app/apps/evenhub/privacy-policy.ts @@ -1,5 +1,6 @@ import { Application } from "@nativescript/core"; import { fetchWithUserAgent } from "../../util/http"; +import { copyToJavaByteBuffer } from "../../native/java-direct-buffer"; declare const android: any; declare const androidx: any; @@ -93,8 +94,7 @@ async function downloadAndOpenPdf(url: string, appName: string, activity: any): const file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`); const stream = new java.io.FileOutputStream(file); try { - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index fd110ad5..4dfefaf6 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -2,7 +2,8 @@ * Terminal app, hosted in its own worker thread. Window model: * - "terminal:hub": the window opened from the launcher; shows the list of * live g2mirror sessions across every connected host (grouped by host when - * more than one is connected), and hosts the Manage Connections section + * more than one is connected; selecting a host's heading launches a shell + * there), and hosts the Manage Connections section * where g2mirror:// connections are added, removed, and toggled. * - "terminal:view:N": opened by selecting a session in the hub; each has * its own websocket connection (the protocol allows one attached session @@ -1047,10 +1048,7 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] { label, onSelect: (ctx) => { ctx.stack.pop(); - launchAndOpenView(control, preset).catch((error) => { - // The hub status line also shows the server's error message. - console.warn(`terminal launch ${preset} failed: ${error}`); - }); + launchFromUi(control, preset); }, }); } @@ -1180,7 +1178,10 @@ function applyHistoryReply(window: ViewWindow, reply: { start: number; oldest: n type HubItem = { label: string; - /** Group heading (host name): drawn differently and skipped by selection. */ + /** + * Group heading (host name): drawn unindented and dimmer. Selectable only + * when it has an onSelect (launch a shell on that host). + */ heading?: boolean; /** Session with recent output: an animated indicator marks the row. */ active?: boolean; @@ -1210,7 +1211,7 @@ function hubMenu(window: HubWindow): Menu { function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs): void { const font = chromeFont(); if (item.heading) { - image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, 140); + image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, selected ? 255 : 140); return; } // Activity gutter, open-window number column, then the label, truncated @@ -1282,9 +1283,14 @@ function hubSessionItems(window: HubWindow): HubItem[] { ]; const connected = connectedControls(); const multiHost = connected.length > 1; + const canLaunch = launchPresetNames().length > 0; for (const control of connected) { if (multiHost) { - items.push({ label: connectionDisplayName(control.config), heading: true }); + items.push({ + label: connectionDisplayName(control.config), + heading: true, + onSelect: canLaunch ? () => launchOnHost(window, control) : undefined, + }); } const sessions = orderedSessions(window, control); for (const session of sessions) { @@ -1568,6 +1574,37 @@ function launchPresetNames(): string[] { return names; } +/** + * A host heading in the hub was selected: launch a shell there. With a single + * launch preset it starts right away; with several, a submenu titled with the + * host's name picks which. + */ +function launchOnHost(window: HubWindow, control: ControlConnection): void { + const presets = launchPresetNames(); + if (presets.length === 1) { + launchFromUi(control, presets[0]!); + return; + } + windowMenu(window).open( + presets.map((preset) => ({ + label: `Launch ${preset}`, + onSelect: (ctx) => { + ctx.stack.pop(); + launchFromUi(control, preset); + }, + })), + connectionDisplayName(control.config), + ); +} + +/** Launch started from a menu or list row; failures only get logged. */ +function launchFromUi(control: ControlConnection, preset: string): void { + launchAndOpenView(control, preset).catch((error) => { + // The hub status line also shows the server's error message. + console.warn(`terminal launch ${preset} failed: ${error}`); + }); +} + /** Launch a preset on a host and open a view window on the new session. */ async function launchAndOpenView(control: ControlConnection, preset: string): Promise { if (!control.client) throw new Error("Not connected to the g2mirror server."); diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index de643e1f..ad7b6137 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -510,7 +510,7 @@ class DashboardController { window.relayout(); await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } else if (window.closeable) { @@ -548,7 +548,7 @@ class DashboardController { void (async () => { for (const window of Array.from(shell.getWindows())) { if (!changed.includes(window.appId)) continue; - await this.configureWindowSurface(window.surfaceId, window.windowId === shell.foregroundWindow()?.windowId, window.heightMode); + await this.configureWindowSurface(window.surfaceId, this.isForegroundWindow(window.windowId), window.heightMode); window.relayout?.(); } shell.foregroundWindow()?.requestRender(); @@ -560,12 +560,11 @@ class DashboardController { const position = verticalPositionSetting.get(); if (position === this.lastVerticalPosition) return; this.lastVerticalPosition = position; - const foregroundWindowId = shell.foregroundWindow()?.windowId; void (async () => { for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } @@ -1021,7 +1020,13 @@ class DashboardController { return this.glassesDisplayLabel(); } if (this.silentMode && this.phase === "connected") { - return "Connected (Silent mode enabled)"; + // Say how to leave it. Silent mode is entered and exited only by the + // same gesture on the glasses, the firmware ignores all input and blanks + // the display while it is on, and nothing here can turn it off -- there + // is no setter, only onSilentMode. So a user who triggered it by + // accident sees a connected pair of glasses that answers nothing, with + // no way to find out why. The instruction belongs where they are looking. + return "Connected (Silent mode — long-press both touchpads on the glasses to exit)"; } // The preview compositor keeps the mirror live (including as a black // frame while the simulated screen is off), so never cover it. @@ -1243,11 +1248,10 @@ class DashboardController { // loop re-applies the current dim from here. await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); this.appliedUnderlayDim = 1; - const foregroundWindowId = shell.foregroundWindow()?.windowId; for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, target, ); @@ -1532,11 +1536,10 @@ class DashboardController { await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); this.appliedUnderlayDim = 1; await this.configureLockSurface(communicator); - const foregroundWindowId = shell.foregroundWindow()?.windowId; for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } @@ -2270,8 +2273,7 @@ class DashboardController { reconfigureSurface: (heightMode) => { // Resize the surface rect to the new band; a foreground window stays // visible. The shell re-renders so the chrome (top bar) follows. - const visible = shell.foregroundWindow()?.windowId === windowId; - void this.configureWindowSurface(surfaceId, visible, heightMode); + void this.configureWindowSurface(surfaceId, this.isForegroundWindow(windowId), heightMode); this.requestShellRender(); }, onClosed: () => { @@ -2424,10 +2426,21 @@ class DashboardController { } } - /** Create/refresh a window surface on the compositor, if a display target exists. */ + /** + * Create/refresh a window surface on the compositor, if a display target exists. + * + * `visible` may be a function, asked only after the surface is configured, + * right when the visibility call is queued. Callers that loop over windows + * or resize one in place must pass one (isForegroundWindow): the configure + * call yields, the foreground can change meanwhile, and a visibility decided + * before the await then lands AFTER the focus change's own calls, since the + * bridge runs Java calls in order. At startup, restoreOpenApps focuses + * windows while connect() registers surfaces, so a foreground read before + * that loop left the old window visible and the new foreground hidden. + */ private async configureWindowSurface( surfaceId: string, - visible: boolean, + visible: boolean | (() => boolean), heightMode: WindowHeightMode = "min", // ensurePreviewDisplay passes its not-yet-published target explicitly. target: DisplayTarget | null = this.display, @@ -2438,7 +2451,12 @@ class DashboardController { zOrder: 0, transparency: "opaque", }); - await target.setSurfaceVisible(surfaceId, visible); + await target.setSurfaceVisible(surfaceId, typeof visible === "function" ? visible() : visible); + } + + /** A late answer to "is this window in front?", for configureWindowSurface. */ + private isForegroundWindow(windowId: string): () => boolean { + return () => shell.foregroundWindow()?.windowId === windowId; } private removeWindowSurface(surfaceId: string): void { diff --git a/app/native/active-display.ts b/app/native/active-display.ts index 6ca761e9..104073c7 100644 --- a/app/native/active-display.ts +++ b/app/native/active-display.ts @@ -1,18 +1,54 @@ +import { JavaDirectBuffer } from "./java-direct-buffer"; + declare const com: any; +// Reused Java-side buffers for this isolate's frames: passing a JS ArrayBuffer +// to Java leaks it (java-direct-buffer.ts). Module state is per isolate, so +// each worker gets its own pair. +const framePixelsBuffer = new JavaDirectBuffer(640 * 480); +const frameDrawsBuffer = new JavaDirectBuffer(); + /** - * The Java object a worker isolate submits surface frames to: the live BLE + * The display a worker isolate submits surface frames to: the live BLE * communicator, or the preview-only compositor standing in when no glasses * are paired (both expose the same submitSurfaceFrame signature). Located * through Java statics because those are shared across isolates, unlike any - * JS-side state. + * JS-side state. Null when neither exists. * * Frame submission only. Buzzer effects use worker-buzzer, which routes to * the platform BLE session and skips playback when disconnected. */ -export function getActiveDisplay(): any { +export function getActiveDisplay(): typeof display | null { + return activeJavaDisplay() ? display : null; +} + +function activeJavaDisplay(): any { return ( com.faceclaw.app.FaceclawBleCommunicator.getActive() ?? com.faceclaw.app.FaceclawPreviewCompositor.getActive() ); } + +/** + * Takes the same ArrayBuffer arguments as the iOS active display and copies + * them into the reused Java buffers right before the synchronous Java call. + */ +const display = { + submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, x: number, y: number, width: number, height: number, + fingerprint: string, paintMs: number, frameId: number, draws: ArrayBuffer | null = null): void { + const target = activeJavaDisplay(); + if (!target) return; + target.submitSurfaceFrame( + framePixelsBuffer.load(new Uint8Array(buffer)), + surfaceId, + x, + y, + width, + height, + fingerprint, + paintMs, + frameId, + frameDrawsBuffer.loadOptional(draws), + ); + }, +}; diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 2663195e..37b5f479 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -1,6 +1,7 @@ import type { RingInput } from "../g2/ring-input"; import { ImageSource, Utils } from "@nativescript/core"; import * as frameTimings from "./frame-timings"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; @@ -132,6 +133,13 @@ export class FaceclawCommunicatorBridge { private javaCallQueue: Promise = Promise.resolve(); /** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */ private queuedJavaCalls = 0; + // Reused Java-side buffers for byte payloads; passing a JS ArrayBuffer to + // Java leaks it (see java-direct-buffer.ts). Loaded inside the queued call, + // immediately before the synchronous Java call that consumes them. + private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); + private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); + private readonly buzzerBuffer = new JavaDirectBuffer(); private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); // Recent frame-finished outcomes from the Java side, so waitForFrameFinished // does not race against finishes that land before the wait starts. @@ -538,7 +546,10 @@ export class FaceclawCommunicatorBridge { */ async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { const snapshot = new Uint8Array(bytes); - await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true); + await this.enqueueJavaCall( + () => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId), + true, + ); } async setUnderlayDim(belowZOrder: number, factor: number): Promise { @@ -594,15 +605,17 @@ export class FaceclawCommunicatorBridge { */ glyphs: ArrayBuffer | null = null, ): Promise { - // Snapshot because the Java call is deferred; the buffer is passed as an - // ArrayBuffer, which NativeScript marshals to a ByteBuffer without the - // ~150ms per-element copy a byte[] parameter would need. + // Snapshot because the Java call is deferred. The bytes reach Java as a + // ByteBuffer (no ~150ms per-element copy, as a byte[] parameter would + // need), but through a reused Java direct buffer rather than by passing + // the ArrayBuffer itself, which NativeScript never frees + // (java-direct-buffer.ts). const snapshot = new Uint8Array(pixels8bpp); // inlineWhenIdle: this is the frame path, the Java side of it measures // ~5ms (composite + pack), and the caller awaits it either way. await this.enqueueJavaCall(() => { this.communicator.submitSurfaceFrame( - snapshot.buffer, + this.framePixelsBuffer.load(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), @@ -611,7 +624,7 @@ export class FaceclawCommunicatorBridge { fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), - glyphs, + this.frameDrawsBuffer.loadOptional(glyphs), ); }, true); } @@ -673,7 +686,7 @@ export class FaceclawCommunicatorBridge { async playBuzzerSequence(payload: Uint8Array): Promise { const snapshot = new Uint8Array(payload); await this.enqueueJavaCall(() => { - this.communicator.playBuzzerSequence(snapshot.buffer); + this.communicator.playBuzzerSequence(this.buzzerBuffer.load(snapshot)); }); } diff --git a/app/native/file-access.ts b/app/native/file-access.ts index b79f3d5f..5cf818e5 100644 --- a/app/native/file-access.ts +++ b/app/native/file-access.ts @@ -5,6 +5,7 @@ * reasonable for a sideloaded personal tool). */ import { Utils } from "@nativescript/core"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const android: any; declare const java: any; @@ -150,10 +151,9 @@ export function writeBinaryFile(path: string, bytes: Uint8Array): boolean { if (parent) parent.mkdirs(); const stream = new java.io.FileOutputStream(file); try { - // An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion - // submitSurfaceFrame relies on), so write through the channel. - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + // Write through the channel from a Java direct buffer: a bulk copy, and + // passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/native/firmware-files.ts b/app/native/firmware-files.ts index 59236354..5517af25 100644 --- a/app/native/firmware-files.ts +++ b/app/native/firmware-files.ts @@ -1,3 +1,8 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any -export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(buffer)) } -export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, buffer) } + +// The image crosses as a fresh Java buffer: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). +export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(copyToJavaByteBuffer(new Uint8Array(buffer)))) } +export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, copyToJavaByteBuffer(new Uint8Array(buffer))) } diff --git a/app/native/gray-image-preview.ts b/app/native/gray-image-preview.ts index 690c1e4b..fc8b1656 100644 --- a/app/native/gray-image-preview.ts +++ b/app/native/gray-image-preview.ts @@ -1,16 +1,17 @@ import { ImageSource } from "@nativescript/core"; import { type GrayImage } from "../graphics/image"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const com: any; const PREVIEW_BRIGHTEN_GAMMA = 0.7; /** - * Build the phone-UI preview for a frame. The pixel buffer is passed to Java - * as an ArrayBuffer (marshalled to a ByteBuffer) and expanded to ARGB there; - * doing the per-pixel work in JS or copying element-by-element across the - * bridge costs ~150ms per frame. + * Build the phone-UI preview for a frame. The pixels are copied into a Java + * direct ByteBuffer and expanded to ARGB there; doing the per-pixel work in JS + * or copying element-by-element across the bridge costs ~150ms per frame, and + * passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). */ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { if (!global.isAndroid) { @@ -19,7 +20,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { const baked = image.withDrawsBaked(); const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray( - baked.pixels.buffer, + copyToJavaByteBuffer(baked.pixels), baked.width, baked.height, PREVIEW_BRIGHTEN_GAMMA, diff --git a/app/native/image-bytes.ts b/app/native/image-bytes.ts index 12c7fd5e..cb3f5a0b 100644 --- a/app/native/image-bytes.ts +++ b/app/native/image-bytes.ts @@ -1,7 +1,9 @@ import { grayImageFromPacket } from './image-files' import { type GrayImage } from '../graphics/image' +import { copyToJavaByteBuffer } from './java-direct-buffer' declare const com: any +/** The encoded bytes cross as a fresh Java buffer; passing the ArrayBuffer leaks it (java-direct-buffer.ts). */ export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null { - return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, width, height)) + return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(copyToJavaByteBuffer(new Uint8Array(bytes)), width, height)) } diff --git a/app/native/java-direct-buffer.ts b/app/native/java-direct-buffer.ts new file mode 100644 index 00000000..b882fd71 --- /dev/null +++ b/app/native/java-direct-buffer.ts @@ -0,0 +1,112 @@ +/** + * Hands bytes to a Java `java.nio.ByteBuffer` parameter without leaking them. + * + * Why this exists: NativeScript Android (9.0.4 through at least 9.1.1) + * converts a JS ArrayBuffer or typed array passed to a Java method by wrapping + * its backing store in `NewDirectByteBuffer` and taking a JNI global ref that is + * never deleted (JsArgConverter.cpp, `buffer = env.NewGlobalRef(buffer)`). The + * ByteBuffer is also linked to the JS ArrayBuffer as its native counterpart. + * The leaked global ref keeps the ByteBuffer reachable forever, so the + * runtime's GC handshake (Runtime.makeInstanceWeakAndCheckIfAlive) always says + * "alive", the JS finalizer re-arms, and V8 never frees the ArrayBuffer. Every + * call leaked its whole buffer plus one global ref. + * + * On the frame path that meant a full surface (576x260, 640x452 or 640x480 + * bytes) per submitted frame, including frames the Java side then discards as + * unchanged. Measured on a Galaxy Z Fold7, native-heap growth matched frame + * bytes submitted to within a few percent, and Samsung's Heimdall memory + * manager killed the app at about 6.4 GB. ART also aborts the process once + * 51,200 global refs are held, so the ref leak is a second ceiling behind the + * memory one. + * + * The workaround keeps the conversion out of the call: the bytes are copied + * into a Java-allocated direct ByteBuffer that is reused, through an + * ArrayBuffer view made once with `ArrayBuffer.from` (an external backing + * store over the Java memory, no global ref), and the Java ByteBuffer object + * itself is passed. A JS proxy for an existing Java object takes the ordinary + * object path, which does not leak. + * + * Contract: the buffer returned by load() is only valid until the next load() + * on the same instance, so the Java callee must consume it synchronously and + * not keep a reference. The Kotlin consumers (SurfaceCompositor via + * AndroidByteReader, playBuzzerSequence, ShellScene.decode) copy or parse + * before returning, which satisfies this. One instance belongs to one JS + * context (main thread or one worker); instances are not shared across + * isolates. + */ + +declare const java: any; + +/** Minimal surface of java.nio.ByteBuffer this module uses. */ +export interface JavaByteBufferLike { + clear(): unknown; + limit(newLimit: number): unknown; +} + +/** How to allocate a direct buffer and view its memory; injectable for tests. */ +export interface DirectBufferBackend { + allocateDirect(capacity: number): JavaByteBufferLike; + viewOf(buffer: JavaByteBufferLike): Uint8Array; +} + +const nativeScriptBackend: DirectBufferBackend = { + allocateDirect: (capacity) => java.nio.ByteBuffer.allocateDirect(capacity), + viewOf: (buffer) => new Uint8Array((ArrayBuffer as any).from(buffer)), +}; + +/** Capacity grows in steps of this many bytes, so a slowly growing payload does not reallocate every call. */ +const CAPACITY_STEP_BYTES = 4096; + +export class JavaDirectBuffer { + private javaBuffer: JavaByteBufferLike | null = null; + private view: Uint8Array | null = null; + /** Number of Java buffers allocated so far (a test and diagnostics hook). */ + allocations = 0; + + constructor( + private readonly initialCapacity = 0, + private readonly backend: DirectBufferBackend = nativeScriptBackend, + ) {} + + /** + * Copy `bytes` into the reused Java buffer and return it with position 0 and + * limit `bytes.length`, so `remaining()` equals the byte count. Valid until + * the next load() on this instance. + */ + load(bytes: Uint8Array): JavaByteBufferLike { + if (!this.javaBuffer || !this.view || this.view.length < bytes.length) { + const wanted = Math.max(bytes.length, this.initialCapacity, 1); + const capacity = Math.ceil(wanted / CAPACITY_STEP_BYTES) * CAPACITY_STEP_BYTES; + this.javaBuffer = this.backend.allocateDirect(capacity); + this.view = this.backend.viewOf(this.javaBuffer); + this.allocations++; + } + this.view.set(bytes); + this.javaBuffer.clear(); + this.javaBuffer.limit(bytes.length); + return this.javaBuffer; + } + + /** As load(), for an optional ArrayBuffer (e.g. a frame's draw list); null stays null. */ + loadOptional(buffer: ArrayBuffer | null | undefined): JavaByteBufferLike | null { + if (!buffer || buffer.byteLength === 0) return null; + return this.load(new Uint8Array(buffer)); + } +} + +/** + * Copy `bytes` into a fresh Java direct buffer (position 0, limit + * `bytes.length`) for a one-off crossing such as an atlas registration or a + * file write. Unlike a JavaDirectBuffer nothing is pinned between calls: the + * Java buffer is an ordinary object, freed once the JS side drops it. + */ +export function copyToJavaByteBuffer( + bytes: Uint8Array, + backend: DirectBufferBackend = nativeScriptBackend, +): JavaByteBufferLike { + // A zero-capacity direct buffer may have no address to view. + const javaBuffer = backend.allocateDirect(Math.max(bytes.length, 1)); + backend.viewOf(javaBuffer).set(bytes); + javaBuffer.limit(bytes.length); + return javaBuffer; +} diff --git a/app/native/kotlin-protocol-smoke.ts b/app/native/kotlin-protocol-smoke.ts index a99cc1e2..062ae21d 100644 --- a/app/native/kotlin-protocol-smoke.ts +++ b/app/native/kotlin-protocol-smoke.ts @@ -1,3 +1,5 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any /** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */ @@ -10,11 +12,11 @@ export function runKotlinProtocolSmokeTest(): void { const compositor = new native.SurfaceCompositor() compositor.configureScreen(3, 2) compositor.configureSurface('test', 0, 0, 3, 2, 0, 0) - const pixels = new Uint8Array([0, 16, 255, 32, 48, 64]) - compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1') + const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64])) + compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1') assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer') assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing') - const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer)) + const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels)) assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer') console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android') } diff --git a/app/native/preview-display.ts b/app/native/preview-display.ts index 33a226de..53e3e927 100644 --- a/app/native/preview-display.ts +++ b/app/native/preview-display.ts @@ -1,5 +1,6 @@ import { ImageSource, Utils } from "@nativescript/core"; import { dimFactor256, type FaceclawCommunicatorBridge, type SurfaceOptions } from "./faceclaw-communicator"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; declare const java: any; @@ -49,6 +50,11 @@ function nonNegativeNumber(value: number): number { */ export class PreviewDisplayTarget implements DisplayTarget { private readonly compositor: any; + // Reused Java-side buffers: passing a JS ArrayBuffer to Java leaks it + // (java-direct-buffer.ts). + private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); + private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); constructor() { const context = Utils.android.getApplicationContext(); @@ -104,7 +110,7 @@ export class PreviewDisplayTarget implements DisplayTarget { } async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { - this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId); + this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId); } async setUnderlayDim(belowZOrder: number, factor: number): Promise { @@ -124,11 +130,11 @@ export class PreviewDisplayTarget implements DisplayTarget { frameId = 0, glyphs: ArrayBuffer | null = null, ): Promise { - // Copy for the same reason the bridge does: the source may be a view on a - // larger buffer, and Java receives the backing ArrayBuffer. - const snapshot = new Uint8Array(pixels8bpp); + // Copied into a reused Java direct buffer, sized to exactly this view's + // bytes; passing the ArrayBuffer itself would leak it + // (java-direct-buffer.ts). this.compositor.submitSurfaceFrame( - snapshot.buffer, + this.framePixelsBuffer.load(pixels8bpp), surfaceId, Math.round(rect.x), Math.round(rect.y), @@ -137,7 +143,7 @@ export class PreviewDisplayTarget implements DisplayTarget { fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), - glyphs, + this.frameDrawsBuffer.loadOptional(glyphs), ); // The Java side finishes the frame: the composite is the end of the line, // and worker isolates submit to the same object without coming through here. diff --git a/app/native/texture-atlas.ts b/app/native/texture-atlas.ts index f39d97d7..a934ed40 100644 --- a/app/native/texture-atlas.ts +++ b/app/native/texture-atlas.ts @@ -1,17 +1,21 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any +// Byte payloads cross as fresh Java buffers: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). + /** Android bridge to the process-wide shared Kotlin atlases. */ export function textureAtlasAvailable(): boolean { return !!global.isAndroid } export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) } export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void { - const reader = new com.faceclaw.app.AndroidByteReader(buffer) + const reader = new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer))) if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader) else com.faceclaw.app.GlyphAtlas.register(reader) } export function registerFirmwareGlyphs(buffer: ArrayBuffer): void { - com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer)) + com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer)))) } export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number { - const buffer = pixels.byteOffset === 0 && pixels.byteLength === pixels.buffer.byteLength ? pixels.buffer : pixels.slice().buffer - return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(buffer)) + return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(pixels))) } diff --git a/app/native/worker-buzzer.ts b/app/native/worker-buzzer.ts index d3d3d385..b6730f6a 100644 --- a/app/native/worker-buzzer.ts +++ b/app/native/worker-buzzer.ts @@ -1,12 +1,17 @@ import type { WorkerAppReply } from "../ui/shell/worker-window"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; +// Reused Java-side buffer for this isolate's tone payloads: passing a JS +// ArrayBuffer to Java leaks it (java-direct-buffer.ts). +const buzzerBuffer = new JavaDirectBuffer(); + /** Workers use the main-thread BLE session on iOS and the native queue on Android. */ export function playWorkerBuzzerSequence(payload: Uint8Array): void { if (global.isIOS) { global.postMessage({ type: "buzzer-sequence", payload: Array.from(payload) } satisfies WorkerAppReply); } else { - com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(new Uint8Array(payload).buffer); + com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(buzzerBuffer.load(payload)); } } diff --git a/app/ui/shell/modal-layer.ts b/app/ui/shell/modal-layer.ts index 0e200e7d..9001d0ae 100644 --- a/app/ui/shell/modal-layer.ts +++ b/app/ui/shell/modal-layer.ts @@ -52,11 +52,19 @@ export class ShellModalLayer implements Layer { this.stack.setBaseSize(interior); // Flattening bakes the inner stack's planes (glyphs included) so the blit // below transplants the finished modal content into this layer's plane. - const inner = flattenPlanes(this.stack.paint(), interior); + // The top plane's retained presentations (a menu's selection highlight + // and scroll strip) stay unbaked: they are display lists the glasses + // animate, and baking one freezes it on its first frame. Lower planes' + // are baked so the top plane's raster still covers them. + const planes = this.stack.paint(); + const top = planes[planes.length - 1]!; + const inner = flattenPlanes(planes.map((plane) => plane === top ? { ...plane, image: plane.image.withDrawsBaked(false) } : plane), interior); const rect = modalRect(); + const innerX = rect.x + MODAL_PADDING, innerY = rect.y + MODAL_PADDING; image.fillRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK); image.drawRect(rect.x, rect.y, rect.width, rect.height, 110); - image.bitBlt(inner, rect.x + MODAL_PADDING, rect.y + MODAL_PADDING, { transparentZero: true }); + image.bitBlt(inner, innerX, innerY, { transparentZero: true }); + top.image.copyPresentationsInto(image, innerX + top.x, innerY + top.y); return image; } diff --git a/app/ui/window-menu.ts b/app/ui/window-menu.ts index 599691e9..a06d4165 100644 --- a/app/ui/window-menu.ts +++ b/app/ui/window-menu.ts @@ -92,10 +92,10 @@ export class WindowMenu { /** * Open the menu, or the shell's system menu when this window has no - * entries. `items` overrides the window's own entries for an app that - * reuses this host for another list (a per-row action menu). + * entries. `items` (and optionally `title`) override the window's own for + * an app that reuses this host for another list (a per-row action menu). */ - open(items: MenuItem[] = this.options.items()): void { + open(items: MenuItem[] = this.options.items(), title: string = this.options.title()): void { if (this.stack) return; if (!items.length) { this.options.post({ type: "open-system-menu", windowId: this.options.windowId }); @@ -106,7 +106,7 @@ export class WindowMenu { handleInput: () => {}, }; const stack = new LayerStack(base, { ...noopLayerActions }, this.options.size, this.options.isFocused); - stack.push(new WindowMenuLayer(this.options.title(), items)); + stack.push(new WindowMenuLayer(title, items)); this.stack = stack; } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt index d1b5a3f8..9fb44fa5 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt @@ -13,7 +13,9 @@ import kotlin.concurrent.Volatile * pairing prompt, and doing both here means both prompts appear at the start rather than one * popping up half way through flashing the second lens. While connected, each arm's battery * level is read (they are independent batteries) so the caller can refuse to flash on a low - * charge. + * charge. Before the prompt is shown, the right arm's settings are read once more so a lens + * that reports silent mode (which blanks the display and ignores input) is refused with the + * way out instead of a prompt nobody can see. * * With `skipPrompt` the on-glasses confirmation is not shown: the flow is just connect + auth + * battery read + result(approved). Used to re-check the battery after the user has already @@ -38,6 +40,15 @@ class FlashPromptFlow( const val LIST_CONTAINER_ID = 2 // Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid. val ITEMS = arrayOf("No, cancel", "Yes, flash") + + /** + * Shown instead of a bare "prompt page not acked" error. Silent mode is entered and left + * by the same gesture on the glasses and nothing here can clear it, so the instruction + * has to travel with the error. + */ + const val SILENT_MODE_MESSAGE = + "Your glasses are in silent mode, so they cannot show the confirmation prompt. " + + "Long-press both touchpads on the glasses to leave silent mode, then try again." } private val rightAddress = rightAddress.trim() @@ -121,6 +132,11 @@ class FlashPromptFlow( session.sendPrelude(rightAddress, ": $rightAddress") if (!skipPrompt) { + // Ask before writing a page that cannot be answered: silent mode blanks the + // display and stops the firmware dispatching input while BLE stays up, so the + // create-prompt write is still acked and the user is left waiting on a prompt + // that never appears. + requireNotSilent(rightAddress) showPrompt(rightAddress) startHeartbeat() listener.onState("prompting", "") @@ -238,23 +254,49 @@ class FlashPromptFlow( listener.onBattery(right, left) } - private fun readBattery(address: String, arm: String): Int { + private fun readBattery(address: String, arm: String): Int = readSettingsSnapshot(address, arm)?.battery ?: -1 + + /** + * The sid-0x09 settings read behind both the battery gate and the silent-mode check; one + * ack carries both fields. Two attempts, since on 2.2.9 a request sent right after the + * prelude can be dropped. Null when the arm does not answer or the ack has no battery field. + */ + private fun readSettingsSnapshot(address: String, arm: String): BleProtocol.BatterySnapshot? { var attempt = 0 while (attempt < 2 && !cancelled) { val ack = session.readSettings(address, timings.batteryAckTimeoutMs) if (ack == null) { - emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm") + emitLog("settings read attempt " + (attempt + 1) + " unacked: " + arm + " arm") attempt++ continue } val snapshot = BleProtocol.parseSettingsBattery(ack) if (snapshot != null) { - return snapshot.battery + return snapshot } - emitLog("battery read ack had no battery field: $arm arm") + emitLog("settings read ack had no battery field: $arm arm") attempt++ } - return -1 + return null + } + + /** + * Refuse the prompt only when the arm POSITIVELY reports silent mode. An arm that does not + * answer, and firmware whose ack omits the field, both fall through to the prompt exactly as + * before, so a missing answer can never block a flash. + */ + private fun requireNotSilent(address: String) { + val snapshot = readSettingsSnapshot(address, "right") + val state = when { + snapshot == null -> "unknown (arm did not answer)" + snapshot.silentMode < 0 -> "unknown (ack omits the field)" + snapshot.silentMode > 0 -> "on" + else -> "off" + } + emitLog("silent mode before prompt: $state") + if (snapshot != null && snapshot.silentMode > 0) { + throw IllegalStateException(SILENT_MODE_MESSAGE) + } } private fun startHeartbeat() { diff --git a/tests/buzzer.test.cjs b/tests/buzzer.test.cjs index b9d9b20d..ea8e1e91 100644 --- a/tests/buzzer.test.cjs +++ b/tests/buzzer.test.cjs @@ -30,10 +30,14 @@ test('iOS game worker forwards an immutable byte payload through its host and ig test('Android game bridge retains direct native playback and skips a missing communicator', () => { let active = null; const played = []; + // Stands in for the reused Java direct buffer: the payload reaches Java as a + // copy, never as the caller's own ArrayBuffer. + const directBuffer = { JavaDirectBuffer: class { load(bytes) { return { bytes: bytes.slice() }; } } }; const bridge = loader({ global: { isIOS: false }, - com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => active } } } } })('app/native/worker-buzzer.ts'); + com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => active } } } } }, + { './java-direct-buffer': directBuffer })('app/native/worker-buzzer.ts'); bridge.playWorkerBuzzerSequence(payload); - active = { playBuzzerSequence: buffer => played.push(new Uint8Array(buffer)) }; + active = { playBuzzerSequence: buffer => played.push(buffer.bytes) }; const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1); bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0); assert.deepEqual(played, [payload]); diff --git a/tests/compass-debug.test.cjs b/tests/compass-readout.test.cjs similarity index 57% rename from tests/compass-debug.test.cjs rename to tests/compass-readout.test.cjs index 215dcc88..7a801134 100644 --- a/tests/compass-debug.test.cjs +++ b/tests/compass-readout.test.cjs @@ -24,12 +24,7 @@ const small = font(18), large = font(32); const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 }; function compass(width = 576, height = 260) { - const settings = new Map(); - const debug = load('app/apps/compass/debug.ts', { '../../native/settings-store': { - getBooleanSetting: (key, fallback) => settings.get(key) ?? fallback, - setBooleanSetting: (key, value) => settings.set(key, value), - } }); - let listener, window, renders = 0, pops = 0; + let listener, window; class RecordingImage extends graphics.GrayImage { texts = []; drawText(font, x, y, text, value) { @@ -56,64 +51,62 @@ function compass(width = 576, height = 260) { '../../ui/shell/geometry': { screenCenterInViewportX: () => width / 2 - 32 }, '../../ui/shell/in-process-window': { YieldAtRootLayer: class { constructor(layer) { return layer; } }, - createInProcessWindow: (options) => { window = options; return { requestRender: () => ++renders }; }, + createInProcessWindow: (options) => { window = options; return { requestRender() {} }; }, }, '../../ui/shell/shell': { shell: { isWindowVisible: () => true } }, '../../native/location-permissions': { hasLocationPermission: () => false }, './calibration': calibration, './calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} }, './heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) }, - './debug': debug, }, { setInterval: () => 1, clearInterval() {} }); - const ctx = { stack: { getBaseSize: () => ({ width, height }), pop: () => ++pops } }; - const open = () => app.createCompassAppWindow({ onClosed() {} }); open(); - return { open, debug, settings, menu: () => window.menuItems(), - toggle: () => window.menuItems().find((item) => item.label.startsWith('Debug information:')).onSelect(ctx), + const ctx = { stack: { getBaseSize: () => ({ width, height }), pop() {} } }; + app.createCompassAppWindow({ onClosed() {} }); + return { menu: () => window.menuItems(), paint: () => window.baseLayer.paint(ctx), - heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }), - renders: () => renders, pops: () => pops }; + heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }) }; } -test('debug toggle defaults off, repaints, closes menu and survives reopening', () => { - const app = compass(); app.heading(diagnostics); - assert.equal(app.debug.isCompassDebugEnabled(), false); - assert.ok(!app.paint().texts.some((t) => t.text.startsWith('Mag:'))); - const before = app.renders(); app.toggle(); - assert.equal(app.debug.isCompassDebugEnabled(), true); - assert.ok(app.renders() > before && app.pops() === 1); - app.open(); assert.equal(app.debug.isCompassDebugEnabled(), true); - app.toggle(); assert.equal(app.debug.isCompassDebugEnabled(), false); +const calibrationText = (image) => image.texts.find((t) => t.text.startsWith('Calibration:')); +const overlaps = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height; + +test('calibration readiness is always shown and the debug toggle is gone', () => { + const app = compass(); + assert.ok(!app.menu().some((item) => item.label.startsWith('Debug information'))); + assert.equal(calibrationText(app.paint()).text, 'Calibration: --'); + app.heading(diagnostics); + assert.equal(calibrationText(app.paint()).text, 'Calibration: ●●●'); + assert.ok(!app.paint().texts.some((t) => /^(Mag|Anom|Src):/.test(t.text))); }); -test('diagnostics sit to the right of the heading without overlap or clipping', () => { - for (const width of [240, 320, 576, 640]) { - const app = compass(width); app.toggle(); app.heading(diagnostics); +test('calibration dots track firmware magnetic accuracy and never go stale', () => { + const app = compass(); + for (const [accuracy, dots] of [[0, '○○○'], [1, '●○○'], [2, '●●○'], [3, '●●●'], [4, '●●●'], [-1, '--']]) { + app.heading({ ...diagnostics, magneticAccuracy: accuracy }); + assert.equal(calibrationText(app.paint()).text, `Calibration: ${dots}`, `accuracy ${accuracy}`); + } + app.heading(diagnostics); + // Legacy samples (no 0x80 flag) and missing diagnostics must replace the old value. + app.heading({ ...diagnostics, flags: 0 }); + assert.equal(calibrationText(app.paint()).text, 'Calibration: --'); + app.heading(diagnostics); + app.heading(undefined); + assert.equal(calibrationText(app.paint()).text, 'Calibration: --'); +}); + +test('calibration readout fits the viewport without overlapping the heading or status', () => { + for (const [width, height] of [[240, 260], [320, 260], [576, 260], [640, 260], [576, 288], [576, 140]]) { + const app = compass(width, height); app.heading({ ...diagnostics, magneticAccuracy: 2 }); const image = app.paint(); - const heading = image.texts.find((t) => t.text === '359° N'); - for (const text of ['Mag: 3/3', 'Anom: 2', 'Src: RV']) { - const label = image.texts.find((t) => t.text === text); - assert.ok(label, text); - assert.ok(label.x > heading.x + heading.width); - assert.ok(label.x + label.width <= width && label.y + label.height <= image.height); + const label = calibrationText(image); + assert.ok(label.x >= 0 && label.y >= 0, `${width}x${height}`); + assert.ok(label.x + label.width <= width && label.y + label.height <= image.height, `${width}x${height}`); + for (const other of image.texts.filter((t) => t !== label)) { + assert.ok(!overlaps(label, other), `${width}x${height}: overlaps "${other.text}"`); } - const status = image.texts.find((t) => t.text === 'Magnetic heading'); - assert.ok(image.texts.filter((t) => t.text.startsWith('Src:')).every((t) => t.y + t.height < status.y)); - if (process.env.COMPASS_PREVIEW && width === 576) { + if (process.env.COMPASS_PREVIEW && width === 576 && height === 260) { fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([ Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels), ])); } } }); - -test('legacy and missing samples replace diagnostics instead of leaving stale values', () => { - const app = compass(); app.toggle(); app.heading(diagnostics); - assert.ok(app.paint().texts.some((t) => t.text === 'Mag: 3/3')); - app.heading(undefined); - assert.ok(app.paint().texts.some((t) => t.text === 'unavailable')); - assert.ok(!app.paint().texts.some((t) => t.text === 'Mag: 3/3')); - assert.deepEqual(Array.from(app.debug.compassDebugLines({ ...diagnostics, flags: 0 })), ['Sample data', 'unavailable']); - for (const [source, name] of [[1, 'GRV'], [2, 'GMRV'], [3, 'RV']]) { - assert.equal(app.debug.compassDebugLines({ ...diagnostics, orientationSource: source })[2], `Src: ${name}`); - } -}); diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs index 8cc9670f..740a313e 100644 --- a/tests/compass-texture-cache.test.cjs +++ b/tests/compass-texture-cache.test.cjs @@ -47,7 +47,6 @@ const compass = load('app/apps/compass/compass-app.ts', { './calibration': calibration, './calibration-layer': {}, './compass-rose': rose, - './debug': { compassDebugLines: () => [], isCompassDebugEnabled: () => false, setCompassDebugEnabled: () => {} }, './declination': {}, './heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) }, }, ` @@ -149,7 +148,7 @@ test('heading updates reuse the texture; brightness, layout and clipping changes test('the background registers once and uses a nine-byte image reference on later frames', () => { const registered = []; - const atlas = load('app/native/texture-atlas.ts', {}, '', { + const atlas = load('app/native/texture-atlas.ts', { './java-direct-buffer': { copyToJavaByteBuffer: (bytes) => bytes } }, '', { global: { isAndroid: true }, com: { faceclaw: { app: { AndroidByteReader: class { constructor(buffer) { this.buffer = buffer; } }, ImageAtlas: { ensure: (...args) => { registered.push(args); diff --git a/tests/java-direct-buffer.test.cjs b/tests/java-direct-buffer.test.cjs new file mode 100644 index 00000000..d6d2f67d --- /dev/null +++ b/tests/java-direct-buffer.test.cjs @@ -0,0 +1,122 @@ +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { JavaDirectBuffer, copyToJavaByteBuffer } = require("../.test-build/app/native/java-direct-buffer.js"); + +/** + * A stand-in for java.nio.ByteBuffer.allocateDirect: fixed capacity, a + * position and a limit, and the relative bulk get() SurfaceCompositor uses. + * viewOf() plays ArrayBuffer.from: a view over the same memory, not a copy. + */ +function fakeBackend() { + const allocated = []; + return { + allocated, + allocateDirect(capacity) { + const memory = new Uint8Array(capacity); + const buffer = { + memory, + position: 0, + limitValue: capacity, + clear() { + this.position = 0; + this.limitValue = capacity; + return this; + }, + limit(newLimit) { + if (newLimit > capacity) throw new Error("limit past capacity"); + this.limitValue = newLimit; + if (this.position > newLimit) this.position = newLimit; + return this; + }, + remaining() { + return this.limitValue - this.position; + }, + get(dst, offset, length) { + if (length > this.remaining()) throw new Error("BufferUnderflowException"); + dst.set(memory.subarray(this.position, this.position + length), offset); + this.position += length; + return this; + }, + }; + allocated.push(buffer); + return buffer; + }, + viewOf(buffer) { + return buffer.memory; + }, + }; +} + +/** Consume the way SurfaceCompositor.applyAndComposite does: exact remaining(), then drain. */ +function consume(buffer, expectedBytes) { + assert.equal(buffer.remaining(), expectedBytes); + const out = new Uint8Array(expectedBytes); + buffer.get(out, 0, expectedBytes); + return out; +} + +function frame(length, seed) { + const bytes = new Uint8Array(length); + for (let i = 0; i < length; i++) bytes[i] = (i * 31 + seed) & 0xff; + return bytes; +} + +test("a loaded buffer exposes exactly the payload bytes to a Java-style reader", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(640 * 480, backend); + const shell = frame(640 * 480, 1); + assert.deepEqual(consume(pool.load(shell), shell.length), shell); +}); + +test("successive frames reuse one Java buffer, including smaller ones", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(640 * 480, backend); + for (let i = 0; i < 50; i++) { + const size = i % 2 === 0 ? 640 * 480 : 640 * 452; + const bytes = frame(size, i); + assert.deepEqual(consume(pool.load(bytes), size), bytes); + } + assert.equal(pool.allocations, 1); + assert.equal(backend.allocated.length, 1); +}); + +test("a payload larger than the buffer allocates once more, then reuses", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(0, backend); + consume(pool.load(frame(10, 0)), 10); + consume(pool.load(frame(9000, 0)), 9000); + consume(pool.load(frame(8000, 0)), 8000); + consume(pool.load(frame(9000, 0)), 9000); + assert.equal(pool.allocations, 2); +}); + +test("loadOptional passes null through and loads a draw list", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(0, backend); + assert.equal(pool.loadOptional(null), null); + assert.equal(pool.loadOptional(undefined), null); + assert.equal(pool.loadOptional(new ArrayBuffer(0)), null); + assert.equal(backend.allocated.length, 0); + const draws = frame(36, 7); + assert.deepEqual(consume(pool.loadOptional(draws.buffer), 36), draws); +}); + +test("a view on a larger buffer contributes only its own bytes", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(0, backend); + const whole = frame(100, 3); + const view = whole.subarray(10, 30); + assert.deepEqual(consume(pool.load(view), 20), whole.slice(10, 30)); +}); + +test("copyToJavaByteBuffer gives each crossing its own exact-length buffer", () => { + const backend = fakeBackend(); + const whole = frame(100, 5); + const first = copyToJavaByteBuffer(whole.subarray(10, 30), backend); + const second = copyToJavaByteBuffer(frame(7, 1), backend); + assert.notEqual(first, second); + assert.deepEqual(consume(first, 20), whole.slice(10, 30)); + assert.deepEqual(consume(second, 7), frame(7, 1)); + assert.equal(consume(copyToJavaByteBuffer(new Uint8Array(0), backend), 0).length, 0); +}); diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt index 9573d705..1506f7e1 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt @@ -60,11 +60,12 @@ private fun field(number: Int, text: String): ByteArray = field(number, text.enc private fun authSuccess(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, ByteArray(0)) private fun authPending(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, BleProtocol.encodeVarintField(1, 1)) private fun ack(magic: Int) = BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic) -private fun settingsAck(magic: Int, battery: Int = -1, left: String = "", right: String = "", extension: String = ""): ByteArray { +private fun settingsAck(magic: Int, battery: Int = -1, left: String = "", right: String = "", extension: String = "", silentMode: Int = -1): ByteArray { var request = ByteArray(0) if (left.isNotEmpty()) request += field(5, left) if (right.isNotEmpty()) request += field(6, right) if (battery >= 0) request += BleProtocol.encodeVarintField(12, battery) + if (silentMode >= 0) request += BleProtocol.encodeVarintField(14, silentMode) var pb = BleProtocol.encodeVarintField(1, 2) + BleProtocol.encodeVarintField(2, magic) + field(4, request) if (extension.isNotEmpty()) pb += field(100, extension) return pb @@ -205,11 +206,11 @@ class StockFlowsTest { override fun onResult(approved: Boolean) { result = approved } } - private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map): (FakeStockLink.Written) -> Unit = { w -> + private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map, silentMode: Int = -1): (FakeStockLink.Written) -> Unit = { w -> when (w.sid) { BleProtocol.SID_SECURITY_AUTH -> link.notify(w.address, authSuccess(w.magic), w.sid) BleProtocol.PRELUDE_ACK_SID -> link.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid) - BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, batteries[w.address] ?: -1), w.sid) + BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, batteries[w.address] ?: -1, silentMode = silentMode), w.sid) BleProtocol.SID_EVENHUB -> { val cmd = BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(w.pb), 1, -1) link.notify(w.address, ack(w.magic), w.sid) @@ -275,6 +276,43 @@ class StockFlowsTest { assertEquals("disconnected:Lost connection to the glasses.", events.states.last()) } + @Test + fun promptRefusesOnlyWhenTheLensReportsSilentMode() { + // Silent mode on: refused with the way out, before any page is written. + val silentOn = FakeStockLink() + silentOn.responder = promptResponder(silentOn, 1, mapOf(R to 85), silentMode = 1) + var events = PromptEvents() + FlashPromptFlow(silentOn, R, "", "w", false, events, fast, testPlatform()).run() + assertEquals(null, events.result) + assertEquals("error:${FlashPromptFlow.SILENT_MODE_MESSAGE}", events.states.last()) + assertTrue(events.logs.any { it == "silent mode before prompt: on" }) + assertFalse(silentOn.writes.any { it.sid == BleProtocol.SID_EVENHUB }) + assertTrue(silentOn.closed) + + // Silent mode off: the prompt is shown and answered as usual. + val silentOff = FakeStockLink() + silentOff.responder = promptResponder(silentOff, 1, mapOf(R to 85), silentMode = 0) + events = PromptEvents() + FlashPromptFlow(silentOff, R, "", "w", false, events, fast, testPlatform()).run() + assertEquals(true, events.result, events.logs.joinToString("\n")) + assertTrue(events.logs.any { it == "silent mode before prompt: off" }) + + // Firmware whose ack omits the field: unknown is not a refusal. + val omitted = FakeStockLink() + omitted.responder = promptResponder(omitted, 1, mapOf(R to 85)) + events = PromptEvents() + FlashPromptFlow(omitted, R, "", "w", false, events, fast, testPlatform()).run() + assertEquals(true, events.result, events.logs.joinToString("\n")) + assertTrue(events.logs.any { it == "silent mode before prompt: unknown (ack omits the field)" }) + + // skipPrompt re-checks the battery only; silent mode does not block it. + val skipped = FakeStockLink() + skipped.responder = promptResponder(skipped, null, mapOf(R to 85), silentMode = 1) + events = PromptEvents() + FlashPromptFlow(skipped, R, "", "w", true, events, fast, testPlatform()).run() + assertEquals(true, events.result) + } + // ---- OTA --------------------------------------------------------------- private fun putU32(buf: ByteArray, offset: Int, value: Long) { for (i in 0 until 4) buf[offset + i] = ((value ushr (8 * i)) and 0xff).toByte() } diff --git a/tests/menu-display-list.test.cjs b/tests/menu-display-list.test.cjs index 2a176bea..fdfe12df 100644 --- a/tests/menu-display-list.test.cjs +++ b/tests/menu-display-list.test.cjs @@ -112,3 +112,21 @@ test('unselected sidebar icons retain -2 depth through shell cropping, including state.selectedIndex=1;const next=new graphics.GrayImage(640,480);chrome.drawSidebar(next,state); assert.equal(next.draws.filter(d=>d.presentation).length,2); }); +test('shell modal carries its inner menu highlight animation instead of baking its first frame',async()=>{ + const {ShellModalLayer}=load('app/ui/shell/modal-layer.ts',{ + '../../graphics/image':graphics,'../../graphics/plane':planes,'../gestures':{},'../layers':load('app/ui/layers.ts',{ + '../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()}, + './gestures':{isDirectionalInput:()=>false}, + }), + './geometry':{appViewportSize:()=>({width:120,height:110}),appViewportRect:()=>({x:10,y:20,width:120,height:110}),SHELL_OPAQUE_BLACK:1}, + }); + const modal=new ShellModalLayer(menu().layer,noopLayerActions),paint=()=>modal.paint({},()=>new graphics.GrayImage(200,200)); + const bare=menu().paint().draws.find(d=>d.presentation),before=paint().draws.find(d=>d.presentation); + assert.equal(before.x,bare.x+28);assert.equal(before.y,bare.y+38); // translated into the box interior + await modal.handleInput({type:'scroll-down'},{}); + const image=paint(),row=image.draws.find(d=>d.presentation); + assert.ok(row.y>before.y);assert.ok(row.presentation.displayList.timeline,'the slide stays a timed display list'); + assert.ok(!image.withDrawsBaked(false).pixels.some(v=>v===255),'selected text is not baked into the modal surface'); + const c=new SurfaceCompositor(200,200);c.setShellScene(encodeShellScene([{image,x:0,y:0,shellKey:9}])); + assert.ok(c.composite().some(v=>v===240)); +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index b88019f9..8ec54367 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -27,6 +27,7 @@ "../app/ui/metrics.ts", "../app/ui/menu-core.ts", "../app/phone-ui/phone-gestures.ts", + "../app/native/java-direct-buffer.ts", "../app/g2/firmware-compat.ts", "../app/g2/glasses-hardware-identity.ts", "../app/g2/ble-proximity.ts", @@ -47,6 +48,7 @@ "../app/apps/timer/timer-model.ts", "../app/apps/teleprompter/script-tracker.ts", "../app/apps/nightscout/nightscout-alerts.ts", - "../app/g2/brightness-curve.ts" + "../app/g2/brightness-curve.ts", + "../app/g2/events.ts" ] }