This commit is contained in:
James Babcock committed 2026-09-25 23:53:23 -07:00
commit 51edaee883
27 files changed
+634 -127

No files matched your search

+22
View File
@@ -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 }}
+9
View File
@@ -17,3 +17,12 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- run: npm ci - run: npm ci
- run: npm test - 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
+9
View File
@@ -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''']
+2 -2
View File
@@ -1,5 +1,6 @@
import { Application } from "@nativescript/core"; import { Application } from "@nativescript/core";
import { fetchWithUserAgent } from "../../util/http"; import { fetchWithUserAgent } from "../../util/http";
import { copyToJavaByteBuffer } from "../../native/java-direct-buffer";
declare const android: any; declare const android: any;
declare const androidx: 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 file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`);
const stream = new java.io.FileOutputStream(file); const stream = new java.io.FileOutputStream(file);
try { try {
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); stream.getChannel().write(copyToJavaByteBuffer(bytes));
stream.getChannel().write(buffer);
} finally { } finally {
stream.close(); stream.close();
} }
+45 -8
View File
@@ -2,7 +2,8 @@
* Terminal app, hosted in its own worker thread. Window model: * Terminal app, hosted in its own worker thread. Window model:
* - "terminal:hub": the window opened from the launcher; shows the list of * - "terminal:hub": the window opened from the launcher; shows the list of
* live g2mirror sessions across every connected host (grouped by host when * 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. * where g2mirror:// connections are added, removed, and toggled.
* - "terminal:view:N": opened by selecting a session in the hub; each has * - "terminal:view:N": opened by selecting a session in the hub; each has
* its own websocket connection (the protocol allows one attached session * its own websocket connection (the protocol allows one attached session
@@ -1047,10 +1048,7 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] {
label, label,
onSelect: (ctx) => { onSelect: (ctx) => {
ctx.stack.pop(); ctx.stack.pop();
launchAndOpenView(control, preset).catch((error) => { launchFromUi(control, preset);
// The hub status line also shows the server's error message.
console.warn(`terminal launch ${preset} failed: ${error}`);
});
}, },
}); });
} }
@@ -1180,7 +1178,10 @@ function applyHistoryReply(window: ViewWindow, reply: { start: number; oldest: n
type HubItem = { type HubItem = {
label: string; 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; heading?: boolean;
/** Session with recent output: an animated indicator marks the row. */ /** Session with recent output: an animated indicator marks the row. */
active?: boolean; active?: boolean;
@@ -1210,7 +1211,7 @@ function hubMenu(window: HubWindow): Menu<HubItem> {
function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs<HubItem>): void { function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs<HubItem>): void {
const font = chromeFont(); const font = chromeFont();
if (item.heading) { 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; return;
} }
// Activity gutter, open-window number column, then the label, truncated // Activity gutter, open-window number column, then the label, truncated
@@ -1282,9 +1283,14 @@ function hubSessionItems(window: HubWindow): HubItem[] {
]; ];
const connected = connectedControls(); const connected = connectedControls();
const multiHost = connected.length > 1; const multiHost = connected.length > 1;
const canLaunch = launchPresetNames().length > 0;
for (const control of connected) { for (const control of connected) {
if (multiHost) { 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); const sessions = orderedSessions(window, control);
for (const session of sessions) { for (const session of sessions) {
@@ -1568,6 +1574,37 @@ function launchPresetNames(): string[] {
return names; 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. */ /** Launch a preset on a host and open a view window on the new session. */
async function launchAndOpenView(control: ControlConnection, preset: string): Promise<string> { async function launchAndOpenView(control: ControlConnection, preset: string): Promise<string> {
if (!control.client) throw new Error("Not connected to the g2mirror server."); if (!control.client) throw new Error("Not connected to the g2mirror server.");
+32 -14
View File
@@ -510,7 +510,7 @@ class DashboardController {
window.relayout(); window.relayout();
await this.configureWindowSurface( await this.configureWindowSurface(
window.surfaceId, window.surfaceId,
window.windowId === foregroundWindowId, this.isForegroundWindow(window.windowId),
window.heightMode, window.heightMode,
); );
} else if (window.closeable) { } else if (window.closeable) {
@@ -548,7 +548,7 @@ class DashboardController {
void (async () => { void (async () => {
for (const window of Array.from(shell.getWindows())) { for (const window of Array.from(shell.getWindows())) {
if (!changed.includes(window.appId)) continue; 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?.(); window.relayout?.();
} }
shell.foregroundWindow()?.requestRender(); shell.foregroundWindow()?.requestRender();
@@ -560,12 +560,11 @@ class DashboardController {
const position = verticalPositionSetting.get(); const position = verticalPositionSetting.get();
if (position === this.lastVerticalPosition) return; if (position === this.lastVerticalPosition) return;
this.lastVerticalPosition = position; this.lastVerticalPosition = position;
const foregroundWindowId = shell.foregroundWindow()?.windowId;
void (async () => { void (async () => {
for (const window of shell.getWindows()) { for (const window of shell.getWindows()) {
await this.configureWindowSurface( await this.configureWindowSurface(
window.surfaceId, window.surfaceId,
window.windowId === foregroundWindowId, this.isForegroundWindow(window.windowId),
window.heightMode, window.heightMode,
); );
} }
@@ -1021,7 +1020,13 @@ class DashboardController {
return this.glassesDisplayLabel(); return this.glassesDisplayLabel();
} }
if (this.silentMode && this.phase === "connected") { 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 // The preview compositor keeps the mirror live (including as a black
// frame while the simulated screen is off), so never cover it. // 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. // loop re-applies the current dim from here.
await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
this.appliedUnderlayDim = 1; this.appliedUnderlayDim = 1;
const foregroundWindowId = shell.foregroundWindow()?.windowId;
for (const window of shell.getWindows()) { for (const window of shell.getWindows()) {
await this.configureWindowSurface( await this.configureWindowSurface(
window.surfaceId, window.surfaceId,
window.windowId === foregroundWindowId, this.isForegroundWindow(window.windowId),
window.heightMode, window.heightMode,
target, target,
); );
@@ -1532,11 +1536,10 @@ class DashboardController {
await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
this.appliedUnderlayDim = 1; this.appliedUnderlayDim = 1;
await this.configureLockSurface(communicator); await this.configureLockSurface(communicator);
const foregroundWindowId = shell.foregroundWindow()?.windowId;
for (const window of shell.getWindows()) { for (const window of shell.getWindows()) {
await this.configureWindowSurface( await this.configureWindowSurface(
window.surfaceId, window.surfaceId,
window.windowId === foregroundWindowId, this.isForegroundWindow(window.windowId),
window.heightMode, window.heightMode,
); );
} }
@@ -2270,8 +2273,7 @@ class DashboardController {
reconfigureSurface: (heightMode) => { reconfigureSurface: (heightMode) => {
// Resize the surface rect to the new band; a foreground window stays // Resize the surface rect to the new band; a foreground window stays
// visible. The shell re-renders so the chrome (top bar) follows. // visible. The shell re-renders so the chrome (top bar) follows.
const visible = shell.foregroundWindow()?.windowId === windowId; void this.configureWindowSurface(surfaceId, this.isForegroundWindow(windowId), heightMode);
void this.configureWindowSurface(surfaceId, visible, heightMode);
this.requestShellRender(); this.requestShellRender();
}, },
onClosed: () => { 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( private async configureWindowSurface(
surfaceId: string, surfaceId: string,
visible: boolean, visible: boolean | (() => boolean),
heightMode: WindowHeightMode = "min", heightMode: WindowHeightMode = "min",
// ensurePreviewDisplay passes its not-yet-published target explicitly. // ensurePreviewDisplay passes its not-yet-published target explicitly.
target: DisplayTarget | null = this.display, target: DisplayTarget | null = this.display,
@@ -2438,7 +2451,12 @@ class DashboardController {
zOrder: 0, zOrder: 0,
transparency: "opaque", 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 { private removeWindowSurface(surfaceId: string): void {
+39 -3
View File
@@ -1,18 +1,54 @@
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any; 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 * communicator, or the preview-only compositor standing in when no glasses
* are paired (both expose the same submitSurfaceFrame signature). Located * are paired (both expose the same submitSurfaceFrame signature). Located
* through Java statics because those are shared across isolates, unlike any * 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 * Frame submission only. Buzzer effects use worker-buzzer, which routes to
* the platform BLE session and skips playback when disconnected. * 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 ( return (
com.faceclaw.app.FaceclawBleCommunicator.getActive() ?? com.faceclaw.app.FaceclawBleCommunicator.getActive() ??
com.faceclaw.app.FaceclawPreviewCompositor.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),
);
},
};
+20 -7
View File
@@ -1,6 +1,7 @@
import type { RingInput } from "../g2/ring-input"; import type { RingInput } from "../g2/ring-input";
import { ImageSource, Utils } from "@nativescript/core"; import { ImageSource, Utils } from "@nativescript/core";
import * as frameTimings from "./frame-timings"; import * as frameTimings from "./frame-timings";
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any; declare const com: any;
@@ -132,6 +133,13 @@ export class FaceclawCommunicatorBridge {
private javaCallQueue: Promise<void> = Promise.resolve(); private javaCallQueue: Promise<void> = Promise.resolve();
/** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */ /** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */
private queuedJavaCalls = 0; 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>(); private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
// Recent frame-finished outcomes from the Java side, so waitForFrameFinished // Recent frame-finished outcomes from the Java side, so waitForFrameFinished
// does not race against finishes that land before the wait starts. // 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<void> { async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
const snapshot = new Uint8Array(bytes); 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<void> { async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
@@ -594,15 +605,17 @@ export class FaceclawCommunicatorBridge {
*/ */
glyphs: ArrayBuffer | null = null, glyphs: ArrayBuffer | null = null,
): Promise<void> { ): Promise<void> {
// Snapshot because the Java call is deferred; the buffer is passed as an // Snapshot because the Java call is deferred. The bytes reach Java as a
// ArrayBuffer, which NativeScript marshals to a ByteBuffer without the // ByteBuffer (no ~150ms per-element copy, as a byte[] parameter would
// ~150ms per-element copy a byte[] parameter would need. // 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); const snapshot = new Uint8Array(pixels8bpp);
// inlineWhenIdle: this is the frame path, the Java side of it measures // inlineWhenIdle: this is the frame path, the Java side of it measures
// ~5ms (composite + pack), and the caller awaits it either way. // ~5ms (composite + pack), and the caller awaits it either way.
await this.enqueueJavaCall(() => { await this.enqueueJavaCall(() => {
this.communicator.submitSurfaceFrame( this.communicator.submitSurfaceFrame(
snapshot.buffer, this.framePixelsBuffer.load(snapshot),
surfaceId, surfaceId,
Math.round(rect.x), Math.round(rect.x),
Math.round(rect.y), Math.round(rect.y),
@@ -611,7 +624,7 @@ export class FaceclawCommunicatorBridge {
fingerprint, fingerprint,
Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(paintMs)),
Math.round(nonNegativeNumber(frameId)), Math.round(nonNegativeNumber(frameId)),
glyphs, this.frameDrawsBuffer.loadOptional(glyphs),
); );
}, true); }, true);
} }
@@ -673,7 +686,7 @@ export class FaceclawCommunicatorBridge {
async playBuzzerSequence(payload: Uint8Array): Promise<void> { async playBuzzerSequence(payload: Uint8Array): Promise<void> {
const snapshot = new Uint8Array(payload); const snapshot = new Uint8Array(payload);
await this.enqueueJavaCall(() => { await this.enqueueJavaCall(() => {
this.communicator.playBuzzerSequence(snapshot.buffer); this.communicator.playBuzzerSequence(this.buzzerBuffer.load(snapshot));
}); });
} }
+4 -4
View File
@@ -5,6 +5,7 @@
* reasonable for a sideloaded personal tool). * reasonable for a sideloaded personal tool).
*/ */
import { Utils } from "@nativescript/core"; import { Utils } from "@nativescript/core";
import { copyToJavaByteBuffer } from "./java-direct-buffer";
declare const android: any; declare const android: any;
declare const java: any; declare const java: any;
@@ -150,10 +151,9 @@ export function writeBinaryFile(path: string, bytes: Uint8Array): boolean {
if (parent) parent.mkdirs(); if (parent) parent.mkdirs();
const stream = new java.io.FileOutputStream(file); const stream = new java.io.FileOutputStream(file);
try { try {
// An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion // Write through the channel from a Java direct buffer: a bulk copy, and
// submitSurfaceFrame relies on), so write through the channel. // passing the ArrayBuffer itself would leak it (java-direct-buffer.ts).
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); stream.getChannel().write(copyToJavaByteBuffer(bytes));
stream.getChannel().write(buffer);
} finally { } finally {
stream.close(); stream.close();
} }
+7 -2
View File
@@ -1,3 +1,8 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any 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))) }
+6 -5
View File
@@ -1,16 +1,17 @@
import { ImageSource } from "@nativescript/core"; import { ImageSource } from "@nativescript/core";
import { type GrayImage } from "../graphics/image"; import { type GrayImage } from "../graphics/image";
import { copyToJavaByteBuffer } from "./java-direct-buffer";
declare const com: any; declare const com: any;
const PREVIEW_BRIGHTEN_GAMMA = 0.7; const PREVIEW_BRIGHTEN_GAMMA = 0.7;
/** /**
* Build the phone-UI preview for a frame. The pixel buffer is passed to Java * Build the phone-UI preview for a frame. The pixels are copied into a Java
* as an ArrayBuffer (marshalled to a ByteBuffer) and expanded to ARGB there; * direct ByteBuffer and expanded to ARGB there; doing the per-pixel work in JS
* doing the per-pixel work in JS or copying element-by-element across the * or copying element-by-element across the bridge costs ~150ms per frame, and
* bridge costs ~150ms per frame. * passing the ArrayBuffer itself would leak it (java-direct-buffer.ts).
*/ */
export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
if (!global.isAndroid) { if (!global.isAndroid) {
@@ -19,7 +20,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
const baked = image.withDrawsBaked(); const baked = image.withDrawsBaked();
const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray( const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray(
baked.pixels.buffer, copyToJavaByteBuffer(baked.pixels),
baked.width, baked.width,
baked.height, baked.height,
PREVIEW_BRIGHTEN_GAMMA, PREVIEW_BRIGHTEN_GAMMA,
+3 -1
View File
@@ -1,7 +1,9 @@
import { grayImageFromPacket } from './image-files' import { grayImageFromPacket } from './image-files'
import { type GrayImage } from '../graphics/image' import { type GrayImage } from '../graphics/image'
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any 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 { 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))
} }
+112
View File
@@ -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;
}
+5 -3
View File
@@ -1,3 +1,5 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any declare const com: any
/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */ /** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */
@@ -10,11 +12,11 @@ export function runKotlinProtocolSmokeTest(): void {
const compositor = new native.SurfaceCompositor() const compositor = new native.SurfaceCompositor()
compositor.configureScreen(3, 2) compositor.configureScreen(3, 2)
compositor.configureSurface('test', 0, 0, 3, 2, 0, 0) compositor.configureSurface('test', 0, 0, 3, 2, 0, 0)
const pixels = new Uint8Array([0, 16, 255, 32, 48, 64]) const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64]))
compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1') compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1')
assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer') assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer')
assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing') 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') assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android') console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android')
} }
+12 -6
View File
@@ -1,5 +1,6 @@
import { ImageSource, Utils } from "@nativescript/core"; import { ImageSource, Utils } from "@nativescript/core";
import { dimFactor256, type FaceclawCommunicatorBridge, type SurfaceOptions } from "./faceclaw-communicator"; import { dimFactor256, type FaceclawCommunicatorBridge, type SurfaceOptions } from "./faceclaw-communicator";
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any; declare const com: any;
declare const java: any; declare const java: any;
@@ -49,6 +50,11 @@ function nonNegativeNumber(value: number): number {
*/ */
export class PreviewDisplayTarget implements DisplayTarget { export class PreviewDisplayTarget implements DisplayTarget {
private readonly compositor: any; 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() { constructor() {
const context = Utils.android.getApplicationContext(); const context = Utils.android.getApplicationContext();
@@ -104,7 +110,7 @@ export class PreviewDisplayTarget implements DisplayTarget {
} }
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> { async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
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<void> { async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
@@ -124,11 +130,11 @@ export class PreviewDisplayTarget implements DisplayTarget {
frameId = 0, frameId = 0,
glyphs: ArrayBuffer | null = null, glyphs: ArrayBuffer | null = null,
): Promise<void> { ): Promise<void> {
// Copy for the same reason the bridge does: the source may be a view on a // Copied into a reused Java direct buffer, sized to exactly this view's
// larger buffer, and Java receives the backing ArrayBuffer. // bytes; passing the ArrayBuffer itself would leak it
const snapshot = new Uint8Array(pixels8bpp); // (java-direct-buffer.ts).
this.compositor.submitSurfaceFrame( this.compositor.submitSurfaceFrame(
snapshot.buffer, this.framePixelsBuffer.load(pixels8bpp),
surfaceId, surfaceId,
Math.round(rect.x), Math.round(rect.x),
Math.round(rect.y), Math.round(rect.y),
@@ -137,7 +143,7 @@ export class PreviewDisplayTarget implements DisplayTarget {
fingerprint, fingerprint,
Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(paintMs)),
Math.round(nonNegativeNumber(frameId)), Math.round(nonNegativeNumber(frameId)),
glyphs, this.frameDrawsBuffer.loadOptional(glyphs),
); );
// The Java side finishes the frame: the composite is the end of the line, // 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. // and worker isolates submit to the same object without coming through here.
+8 -4
View File
@@ -1,17 +1,21 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any 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. */ /** Android bridge to the process-wide shared Kotlin atlases. */
export function textureAtlasAvailable(): boolean { return !!global.isAndroid } export function textureAtlasAvailable(): boolean { return !!global.isAndroid }
export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) } export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) }
export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void { 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) if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader)
else com.faceclaw.app.GlyphAtlas.register(reader) else com.faceclaw.app.GlyphAtlas.register(reader)
} }
export function registerFirmwareGlyphs(buffer: ArrayBuffer): void { 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 { 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(copyToJavaByteBuffer(pixels)))
return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(buffer))
} }
+6 -1
View File
@@ -1,12 +1,17 @@
import type { WorkerAppReply } from "../ui/shell/worker-window"; import type { WorkerAppReply } from "../ui/shell/worker-window";
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any; 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. */ /** Workers use the main-thread BLE session on iOS and the native queue on Android. */
export function playWorkerBuzzerSequence(payload: Uint8Array): void { export function playWorkerBuzzerSequence(payload: Uint8Array): void {
if (global.isIOS) { if (global.isIOS) {
global.postMessage({ type: "buzzer-sequence", payload: Array.from(payload) } satisfies WorkerAppReply); global.postMessage({ type: "buzzer-sequence", payload: Array.from(payload) } satisfies WorkerAppReply);
} else { } else {
com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(new Uint8Array(payload).buffer); com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(buzzerBuffer.load(payload));
} }
} }
+10 -2
View File
@@ -52,11 +52,19 @@ export class ShellModalLayer implements Layer {
this.stack.setBaseSize(interior); this.stack.setBaseSize(interior);
// Flattening bakes the inner stack's planes (glyphs included) so the blit // Flattening bakes the inner stack's planes (glyphs included) so the blit
// below transplants the finished modal content into this layer's plane. // 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 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.fillRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK);
image.drawRect(rect.x, rect.y, rect.width, rect.height, 110); 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; return image;
} }
+4 -4
View File
@@ -92,10 +92,10 @@ export class WindowMenu {
/** /**
* Open the menu, or the shell's system menu when this window has no * 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 * entries. `items` (and optionally `title`) override the window's own for
* reuses this host for another list (a per-row action menu). * 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 (this.stack) return;
if (!items.length) { if (!items.length) {
this.options.post({ type: "open-system-menu", windowId: this.options.windowId }); this.options.post({ type: "open-system-menu", windowId: this.options.windowId });
@@ -106,7 +106,7 @@ export class WindowMenu {
handleInput: () => {}, handleInput: () => {},
}; };
const stack = new LayerStack(base, { ...noopLayerActions }, this.options.size, this.options.isFocused); 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; this.stack = stack;
} }
@@ -13,7 +13,9 @@ import kotlin.concurrent.Volatile
* pairing prompt, and doing both here means both prompts appear at the start rather than one * 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 * 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 * 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 + * 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 * 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 const val LIST_CONTAINER_ID = 2
// Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid. // Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid.
val ITEMS = arrayOf("No, cancel", "Yes, flash") 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() private val rightAddress = rightAddress.trim()
@@ -121,6 +132,11 @@ class FlashPromptFlow(
session.sendPrelude(rightAddress, ": $rightAddress") session.sendPrelude(rightAddress, ": $rightAddress")
if (!skipPrompt) { 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) showPrompt(rightAddress)
startHeartbeat() startHeartbeat()
listener.onState("prompting", "") listener.onState("prompting", "")
@@ -238,23 +254,49 @@ class FlashPromptFlow(
listener.onBattery(right, left) 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 var attempt = 0
while (attempt < 2 && !cancelled) { while (attempt < 2 && !cancelled) {
val ack = session.readSettings(address, timings.batteryAckTimeoutMs) val ack = session.readSettings(address, timings.batteryAckTimeoutMs)
if (ack == null) { if (ack == null) {
emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm") emitLog("settings read attempt " + (attempt + 1) + " unacked: " + arm + " arm")
attempt++ attempt++
continue continue
} }
val snapshot = BleProtocol.parseSettingsBattery(ack) val snapshot = BleProtocol.parseSettingsBattery(ack)
if (snapshot != null) { 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++ 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() { private fun startHeartbeat() {
+6 -2
View File
@@ -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', () => { test('Android game bridge retains direct native playback and skips a missing communicator', () => {
let active = null; let active = null;
const played = []; 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 }, 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); 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); const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1);
bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0); bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0);
assert.deepEqual(played, [payload]); assert.deepEqual(played, [payload]);
@@ -24,12 +24,7 @@ const small = font(18), large = font(32);
const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 }; const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 };
function compass(width = 576, height = 260) { function compass(width = 576, height = 260) {
const settings = new Map(); let listener, window;
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;
class RecordingImage extends graphics.GrayImage { class RecordingImage extends graphics.GrayImage {
texts = []; texts = [];
drawText(font, x, y, text, value) { 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/geometry': { screenCenterInViewportX: () => width / 2 - 32 },
'../../ui/shell/in-process-window': { '../../ui/shell/in-process-window': {
YieldAtRootLayer: class { constructor(layer) { return layer; } }, 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 } }, '../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
'../../native/location-permissions': { hasLocationPermission: () => false }, '../../native/location-permissions': { hasLocationPermission: () => false },
'./calibration': calibration, './calibration': calibration,
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} }, './calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) }, './heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
'./debug': debug,
}, { setInterval: () => 1, clearInterval() {} }); }, { setInterval: () => 1, clearInterval() {} });
const ctx = { stack: { getBaseSize: () => ({ width, height }), pop: () => ++pops } }; const ctx = { stack: { getBaseSize: () => ({ width, height }), pop() {} } };
const open = () => app.createCompassAppWindow({ onClosed() {} }); open(); app.createCompassAppWindow({ onClosed() {} });
return { open, debug, settings, menu: () => window.menuItems(), return { menu: () => window.menuItems(),
toggle: () => window.menuItems().find((item) => item.label.startsWith('Debug information:')).onSelect(ctx),
paint: () => window.baseLayer.paint(ctx), paint: () => window.baseLayer.paint(ctx),
heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }), heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }) };
renders: () => renders, pops: () => pops };
} }
test('debug toggle defaults off, repaints, closes menu and survives reopening', () => { const calibrationText = (image) => image.texts.find((t) => t.text.startsWith('Calibration:'));
const app = compass(); app.heading(diagnostics); 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;
assert.equal(app.debug.isCompassDebugEnabled(), false);
assert.ok(!app.paint().texts.some((t) => t.text.startsWith('Mag:'))); test('calibration readiness is always shown and the debug toggle is gone', () => {
const before = app.renders(); app.toggle(); const app = compass();
assert.equal(app.debug.isCompassDebugEnabled(), true); assert.ok(!app.menu().some((item) => item.label.startsWith('Debug information')));
assert.ok(app.renders() > before && app.pops() === 1); assert.equal(calibrationText(app.paint()).text, 'Calibration: --');
app.open(); assert.equal(app.debug.isCompassDebugEnabled(), true); app.heading(diagnostics);
app.toggle(); assert.equal(app.debug.isCompassDebugEnabled(), false); 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', () => { test('calibration dots track firmware magnetic accuracy and never go stale', () => {
for (const width of [240, 320, 576, 640]) { const app = compass();
const app = compass(width); app.toggle(); app.heading(diagnostics); 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 image = app.paint();
const heading = image.texts.find((t) => t.text === '359° N'); const label = calibrationText(image);
for (const text of ['Mag: 3/3', 'Anom: 2', 'Src: RV']) { assert.ok(label.x >= 0 && label.y >= 0, `${width}x${height}`);
const label = image.texts.find((t) => t.text === text); assert.ok(label.x + label.width <= width && label.y + label.height <= image.height, `${width}x${height}`);
assert.ok(label, text); for (const other of image.texts.filter((t) => t !== label)) {
assert.ok(label.x > heading.x + heading.width); assert.ok(!overlaps(label, other), `${width}x${height}: overlaps "${other.text}"`);
assert.ok(label.x + label.width <= width && label.y + label.height <= image.height);
} }
const status = image.texts.find((t) => t.text === 'Magnetic heading'); if (process.env.COMPASS_PREVIEW && width === 576 && height === 260) {
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) {
fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([ fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([
Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels), 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}`);
}
});
+1 -2
View File
@@ -47,7 +47,6 @@ const compass = load('app/apps/compass/compass-app.ts', {
'./calibration': calibration, './calibration': calibration,
'./calibration-layer': {}, './calibration-layer': {},
'./compass-rose': rose, './compass-rose': rose,
'./debug': { compassDebugLines: () => [], isCompassDebugEnabled: () => false, setCompassDebugEnabled: () => {} },
'./declination': {}, './declination': {},
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) }, './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', () => { test('the background registers once and uses a nine-byte image reference on later frames', () => {
const registered = []; 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 }, global: { isAndroid: true },
com: { faceclaw: { app: { AndroidByteReader: class { constructor(buffer) { this.buffer = buffer; } }, ImageAtlas: { ensure: (...args) => { com: { faceclaw: { app: { AndroidByteReader: class { constructor(buffer) { this.buffer = buffer; } }, ImageAtlas: { ensure: (...args) => {
registered.push(args); registered.push(args);
+122
View File
@@ -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);
});
@@ -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 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 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 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) var request = ByteArray(0)
if (left.isNotEmpty()) request += field(5, left) if (left.isNotEmpty()) request += field(5, left)
if (right.isNotEmpty()) request += field(6, right) if (right.isNotEmpty()) request += field(6, right)
if (battery >= 0) request += BleProtocol.encodeVarintField(12, battery) 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) var pb = BleProtocol.encodeVarintField(1, 2) + BleProtocol.encodeVarintField(2, magic) + field(4, request)
if (extension.isNotEmpty()) pb += field(100, extension) if (extension.isNotEmpty()) pb += field(100, extension)
return pb return pb
@@ -205,11 +206,11 @@ class StockFlowsTest {
override fun onResult(approved: Boolean) { result = approved } override fun onResult(approved: Boolean) { result = approved }
} }
private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map<String, Int>): (FakeStockLink.Written) -> Unit = { w -> private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map<String, Int>, silentMode: Int = -1): (FakeStockLink.Written) -> Unit = { w ->
when (w.sid) { when (w.sid) {
BleProtocol.SID_SECURITY_AUTH -> link.notify(w.address, authSuccess(w.magic), 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.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 -> { BleProtocol.SID_EVENHUB -> {
val cmd = BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(w.pb), 1, -1) val cmd = BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(w.pb), 1, -1)
link.notify(w.address, ack(w.magic), w.sid) 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()) 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 --------------------------------------------------------------- // ---- 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() } 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() }
+18
View File
@@ -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); state.selectedIndex=1;const next=new graphics.GrayImage(640,480);chrome.drawSidebar(next,state);
assert.equal(next.draws.filter(d=>d.presentation).length,2); 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));
});
+3 -1
View File
@@ -27,6 +27,7 @@
"../app/ui/metrics.ts", "../app/ui/metrics.ts",
"../app/ui/menu-core.ts", "../app/ui/menu-core.ts",
"../app/phone-ui/phone-gestures.ts", "../app/phone-ui/phone-gestures.ts",
"../app/native/java-direct-buffer.ts",
"../app/g2/firmware-compat.ts", "../app/g2/firmware-compat.ts",
"../app/g2/glasses-hardware-identity.ts", "../app/g2/glasses-hardware-identity.ts",
"../app/g2/ble-proximity.ts", "../app/g2/ble-proximity.ts",
@@ -47,6 +48,7 @@
"../app/apps/timer/timer-model.ts", "../app/apps/timer/timer-model.ts",
"../app/apps/teleprompter/script-tracker.ts", "../app/apps/teleprompter/script-tracker.ts",
"../app/apps/nightscout/nightscout-alerts.ts", "../app/apps/nightscout/nightscout-alerts.ts",
"../app/g2/brightness-curve.ts" "../app/g2/brightness-curve.ts",
"../app/g2/events.ts"
] ]
} }