Refactor iOS version to use a bunch more shared code

This commit is contained in:
James Babcock committed 2026-09-22 21:26:51 -07:00
1 parent c3a0ba9f56
commit 4a194e37bb
40 files changed
+1874 -2605

No files matched your search

+14 -2
View File
@@ -1,3 +1,15 @@
import { iosBleTraffic } from '../g2/ble-traffic-counters'
export type { BleTrafficSample } from './ble-traffic'
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } { return iosBleTraffic.sample() }
declare const FaceclawKitIosBleTraffic: any
// The Kotlin counters reset on every sample; accumulate here so callers see
// running totals like FaceclawBleManager.sampleOutboundTraffic on Android.
// CoreBluetooth has no per-message boundary; messages counts write calls.
const totals = { messages: 0, bytes: 0, frames: 0 }
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } {
const sample = FaceclawKitIosBleTraffic.shared.sample()
const written = Number(sample.getIndex(0)), frames = Number(sample.getIndex(2))
totals.bytes += written
totals.frames += frames
if (written > 0) totals.messages++
return { ...totals }
}
+79 -58
View File
@@ -1,68 +1,89 @@
import { StockConnection, StockTimeout } from '../g2/stock-connection'
import * as p from '../g2/ble-protocol'
import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { DeviceInfo, DeviceInfoState } from './device-info-probe'
export type { DeviceInfo, DeviceInfoState } from './device-info-probe'
declare const FaceclawKitIosDeviceInfoProbe: any, FaceclawKitFaceclawDeviceInfoProbeListener: any
/**
* iOS twin of the Android DeviceInfoProbe wrapper: the shared Kotlin DeviceInfoProbeFlow
* runs over a CoreBluetooth central (FaceclawKitIosDeviceInfoProbe); TypeScript only maps
* the stored MAC addresses to peripheral identifiers first. Callbacks arrive on the main
* queue and are fanned out asynchronously like the Android wrapper does.
*/
export class DeviceInfoProbe {
private logs = new Set<(line: string) => void>()
private states = new Set<(state: DeviceInfoState, detail: string) => void>()
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) })
constructor(private right: string, private left = '') {}
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: DeviceInfoState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
async run(): Promise<DeviceInfo> {
const snapshots = new Map<string, DeviceInfo>()
const parse = (message: p.ProtocolMessage): DeviceInfo => {
const values = p.readBytes(message.payload, 4)
return { leftVersion: values ? p.readString(values, 5) : '', rightVersion: values ? p.readString(values, 6) : '', extension: p.readString(message.payload, 100) }
}
const hasVersion = (info: DeviceInfo) => !!(info.leftVersion || info.rightVersion || info.extension)
// Some stock builds push settings using their own magic instead of the
// query's magic. Keep a valid snapshot as the Android probe does.
this.link.onMessage((role, message) => {
if (message.sid === p.SID.settings) { const info = parse(message); if (hasVersion(info)) snapshots.set(role, info) }
private probe: any = null
private listenerProxy: object | null = null
private readonly logListeners = new Set<(line: string) => void>()
private readonly stateListeners = new Set<(state: DeviceInfoState, detail: string) => void>()
private settled = false
private cancelled = false
private resolveFn: ((info: DeviceInfo) => void) | null = null
private rejectFn: ((error: Error) => void) | null = null
constructor(private readonly rightAddress: string, private readonly leftAddress = '') {}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener)
return () => this.logListeners.delete(listener)
}
onStateChange(listener: (state: DeviceInfoState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
run(): Promise<DeviceInfo> {
return new Promise<DeviceInfo>((resolve, reject) => {
this.resolveFn = resolve
this.rejectFn = reject
void this.start()
})
}
private async start(): Promise<void> {
let identifiers: Record<string, string>
try {
await this.link.resolve({ right: this.right, left: this.left, ring: '' })
for (const role of ['right', ...(this.left ? ['left'] : [])]) {
await this.bringUp(role)
}
let lastError: unknown
for (const role of ['right', ...(this.left ? ['left'] : [])]) {
for (const fn of this.states) fn('querying', role)
try {
if (!this.link.isConnected(role)) await this.bringUp(role)
await this.link.request(role, p.SID.launch, p.prelude, 3500, 156)
for (let attempt = 0; attempt < 2; attempt++) {
try {
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
const info = parse(message)
if (hasVersion(info)) return info
} catch (error) { this.link.check(); lastError = error }
if (snapshots.has(role)) return snapshots.get(role)!
}
} catch (error) { this.link.check(); lastError = error }
}
throw lastError ?? new Error("Connected, but couldn't read a firmware version.")
} finally { this.close() }
}
private async bringUp(role: string): Promise<void> {
for (let attempt = 0; ; attempt++) {
try {
for (const fn of this.states) fn('connecting', role)
await this.link.connect(role)
for (const fn of this.states) fn('authenticating', role)
// Version reads remain available on older stock builds without an auth ACK.
try { await this.link.authenticate(role) } catch (error) { if (!(error instanceof StockTimeout)) throw error }
return
} catch (error) {
this.link.check(); this.link.disconnect(role)
if (attempt === 2) throw error
await new Promise(resolve => setTimeout(resolve, 1500)); this.link.check()
}
this.emit(this.stateListeners, 'connecting', 'right')
identifiers = await iosBluetooth().resolveDevices({ right: this.rightAddress, left: this.leftAddress, ring: '' })
} catch (error) {
this.settle(error instanceof Error ? error : new Error(String(error)), null)
return
}
if (this.cancelled) { this.settle(new Error('Cancelled.'), null); return }
this.probe = FaceclawKitIosDeviceInfoProbe.alloc().initWithRightAddressLeftAddress(identifiers.right ?? '', identifiers.left ?? '')
this.listenerProxy = kotlinListener(FaceclawKitFaceclawDeviceInfoProbeListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onStateStateDetail: (state: string, detail: string) =>
this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? '')),
onResultLeftVersionRightVersionExtension: (leftVersion: string, rightVersion: string, extension: string) =>
this.settle(null, { leftVersion: String(leftVersion ?? ''), rightVersion: String(rightVersion ?? ''), extension: String(extension ?? '') }),
onErrorMessage: (message: string) => this.settle(new Error(String(message ?? '')), null),
})
this.probe.setListenerListener(this.listenerProxy)
this.probe.start()
}
cancel(): void {
this.cancelled = true
try { this.probe?.cancel() } catch { /* ignore */ }
}
close(): void {
this.cancelled = true
try { this.probe?.close() } catch { /* ignore */ }
}
private settle(error: Error | null, info: DeviceInfo | null): void {
if (this.settled) return
this.settled = true
this.close()
if (error) this.rejectFn?.(error)
else if (info) this.resolveFn?.(info)
}
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const snapshot = Array.from(listeners)
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
}
cancel(): void { this.link.close() }
close(): void { this.link.close() }
}
+677
View File
@@ -0,0 +1,677 @@
import type { RingInput } from "../g2/ring-input";
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { type FirmwareInfo } from "../g2/firmware-compat";
import { type CompassEvent } from "./compass-types";
import { fromData, toData } from "./kotlin-data";
import { previewPixels } from "./ios-graphics";
import { iosBluetooth } from "./ios-bluetooth";
export type { FirmwareInfo };
// Kotlin/Native facades exported by FaceclawKit (see native/kotlin/shared/src/iosMain/.../ble).
declare const FaceclawKitIosGlassesSession: any;
declare const FaceclawKitIosProtocolPlatform: any;
declare const FaceclawKitFaceclawBleCommunicatorListener: any;
declare const FaceclawKitIosAncsListener: any;
declare const FaceclawKitIosAudioPacketListener: any;
declare const FaceclawKitFaceclawCompassListener: any;
export type CommunicatorPhase =
| "disconnected"
| "connecting"
| "connected"
| "charging"
| "retrying"
| "unpaired"
| "disconnecting";
export type CommunicatorState = {
phase: CommunicatorPhase;
status: string;
};
export type HeadsetBatteryState = {
battery: number;
chargingStatus: number;
ringBattery?: number;
ringChargingStatus?: number;
};
export type FrameMetrics = {
paintMs: number;
transmitMs: number;
tileCount: number;
};
/** See the Android twin: compositor surface configuration in screen pixels. */
export type SurfaceOptions = {
x: number;
y: number;
width: number;
height: number;
zOrder: number;
transparency: "opaque" | "color-key";
};
export type RawInputEvent = { ringInput?: RingInput } & (
| { kind: "list-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "text-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "sys-event"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "even-ai"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "watch-gesture"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "display-wake"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
);
function nonNegativeNumber(value: number): number {
const numeric = Number(value);
return Number.isFinite(numeric) ? Math.max(0, numeric) : 0;
}
/** A 0..1 brightness factor as the compositor's 0..256 fixed-point form. */
export function dimFactor256(factor: number): number {
return Math.round(Math.max(0, Math.min(1, factor)) * 256);
}
function timestamp(): string {
const now = new Date();
const pad = (value: number, width = 2) => String(value).padStart(width, "0");
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`;
}
export type PeripheralIdentifiers = { right: string; left: string; ring: string };
/**
* Map the configured MAC addresses to CoreBluetooth identifiers through the scan
* layer (app/native/ios-bluetooth.ts): the mapping a previous scan persisted, else a
* fresh scan that classifies Even advertisements. The scan layer also owns Bluetooth
* readiness (creating the central triggers the permission prompt on first use).
*/
export async function resolveIosPeripherals(addresses: { right: string; left: string; ring?: string }): Promise<PeripheralIdentifiers> {
const resolved = await iosBluetooth().resolveDevices({ right: addresses.right, left: addresses.left, ring: addresses.ring ?? "" });
return {
right: String(resolved.right ?? "").toUpperCase(),
left: String(resolved.left ?? "").toUpperCase(),
ring: String(resolved.ring ?? "").toUpperCase(),
};
}
/** Bluetooth powered on and authorized (prompts for permission the first time); rejects with the user-facing reason. */
export function ensureIosBluetoothReady(): Promise<void> {
return iosBluetooth().ensureReady();
}
@NativeClass()
class SessionListener extends NSObject {
static ObjCProtocols = [FaceclawKitFaceclawBleCommunicatorListener];
bridge!: FaceclawCommunicatorBridge;
onStateChangePhaseStatus(phase: string, status: string): void { this.bridge.handleStateChange(String(phase ?? ""), String(status ?? "")); }
onRingEventKindContainerNameEventTypeEventSourceSystemExitReasonCodeFrameIdRingTickRingTypeRingAuxRingSpeed(
kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number,
): void {
this.bridge.handleRingEvent(String(kind ?? ""), String(containerName ?? ""), Number(eventType), Number(eventSource),
Number(systemExitReasonCode), Number(frameId), Number(ringTick), Number(ringType), Number(ringAux), Number(ringSpeed));
}
onBatteryStateHeadsetBatteryHeadsetChargingRingBatteryRingCharging(headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number): void {
this.bridge.handleBatteryState(Number(headsetBattery), Number(headsetCharging), Number(ringBattery), Number(ringCharging));
}
onSilentModeSilent(silent: boolean): void { this.bridge.handleSilentMode(Boolean(silent)); }
onWearStateWearing(wearing: boolean): void { this.bridge.handleWearState(Boolean(wearing)); }
onPhoneLockStateLocked(locked: boolean): void { this.bridge.handlePhoneLockState(Boolean(locked)); }
onEvenAppConflictMessage(message: string): void { this.bridge.handleEvenAppConflict(String(message ?? "")); }
onFrameMetricsPaintMsTransmitMsTileCount(paintMs: number, transmitMs: number, tileCount: number): void {
this.bridge.handleFrameMetrics(Number(paintMs), Number(transmitMs), Number(tileCount));
}
onFrameFinishedFrameIdOutcome(frameId: number, outcome: string): void { this.bridge.handleFrameFinished(Number(frameId), String(outcome ?? "")); }
onFirmwareInfoLeftVersionRightVersionExtension(leftVersion: string, rightVersion: string, extension: string): void {
this.bridge.handleFirmwareInfo(String(leftVersion ?? ""), String(rightVersion ?? ""), String(extension ?? ""));
}
}
@NativeClass()
class AncsListener extends NSObject {
static ObjCProtocols = [FaceclawKitIosAncsListener];
bridge!: FaceclawCommunicatorBridge;
onAncsAuthorizationAuthorized(authorized: boolean): void { this.bridge.handleAncsAuthorization(Boolean(authorized)); }
onRelayFrameData(data: NSData): void { this.bridge.handleAncsRelayFrame(fromData(data)); }
}
@NativeClass()
class AudioPacketListener extends NSObject {
static ObjCProtocols = [FaceclawKitIosAudioPacketListener];
handler!: (packet: Uint8Array, arm: string, arrivalMs: number) => void;
onAudioPacketDataArmArrivalMs(data: NSData, arm: string, arrivalMs: number): void {
this.handler(fromData(data), String(arm ?? ""), Number(arrivalMs));
}
}
@NativeClass()
class CompassListener extends NSObject {
static ObjCProtocols = [FaceclawKitFaceclawCompassListener];
handler!: (event: CompassEvent) => void;
onCompassEventCommandHeadingDegreesMagneticAccuracyMagneticAnomaliesOrientationSourceDiagnosticFlagsSampleTimeMs(
command: number, headingDegrees: number, magneticAccuracy: number, magneticAnomalies: number,
orientationSource: number, diagnosticFlags: number, sampleTimeMs: number,
): void {
this.handler({
command: Number(command), headingDegrees: Number(headingDegrees),
diagnostics: diagnosticFlags >= 0 ? {
magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies),
orientationSource: Number(orientationSource), flags: Number(diagnosticFlags), sampleTimeMs: Number(sampleTimeMs),
} : undefined,
});
}
}
/**
* The iOS twin of the Android communicator bridge: the same public API over
* the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth
* underneath). Addresses are CoreBluetooth identifiers (resolveIosPeripherals
* maps the configured MACs). Every Kotlin callback already arrives on the
* main queue; listener fan-out is still deferred with setTimeout like on
* Android so callers never re-enter the bridge from a Kotlin frame.
*/
export class FaceclawCommunicatorBridge {
private readonly communicator: any;
private readonly listenerProxy: SessionListener;
private ancsProxy: AncsListener | null = null;
private nativeCallQueue: Promise<void> = Promise.resolve();
private queuedNativeCalls = 0;
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
private readonly finishedFrameOutcomes = new Map<number, string>();
private readonly frameFinishedWaiters = new Map<number, Set<(outcome: string) => void>>();
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
private readonly silentModeListeners = new Set<(silent: boolean) => void>();
private readonly wearStateListeners = new Set<(wearing: boolean) => void>();
private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>();
private latestWearState: boolean | null = null;
private latestPhoneLockState: boolean | null = null;
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
private readonly ancsAuthorizationListeners = new Set<(authorized: boolean) => void>();
private readonly ancsRelayListeners = new Set<(frame: Uint8Array) => void>();
private audioListener: AudioPacketListener | null = null;
private microphoneWork: Promise<void> = Promise.resolve();
private microphoneToken = 0;
constructor(addresses: { right: string; left: string; ring?: string }) {
this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress(
String(addresses.right ?? "").toUpperCase(),
String(addresses.left ?? "").toUpperCase(),
String(addresses.ring ?? "").toUpperCase(),
);
this.listenerProxy = SessionListener.new() as SessionListener;
this.listenerProxy.bridge = this;
this.communicator.setListenerListener(this.listenerProxy);
}
// ----- Kotlin listener entry points (main queue) -----------------------------------
/** @internal */
handleStateChange(phase: string, status: string): void {
this.emitAsync(this.stateListeners, { phase: phase as CommunicatorPhase, status });
}
/** @internal */
handleRingEvent(kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number): void {
const event = {
kind: kind as RawInputEvent["kind"], containerName, eventType, eventSource, systemExitReasonCode, frameId,
ringInput: ringTick >= 0 ? { tick: ringTick, type: ringType, aux: ringAux, speed: ringSpeed } : undefined,
} as RawInputEvent;
this.emitAsync(this.ringListeners, event);
}
/** @internal */
handleBatteryState(battery: number, chargingStatus: number, ringBattery: number, ringChargingStatus: number): void {
this.emitAsync(this.batteryListeners, { battery, chargingStatus, ringBattery, ringChargingStatus });
}
/** @internal */
handleSilentMode(silent: boolean): void { this.emitAsync(this.silentModeListeners, silent); }
/** @internal */
handleWearState(wearing: boolean): void {
this.latestWearState = wearing;
this.emitAsync(this.wearStateListeners, wearing);
}
/** @internal */
handlePhoneLockState(locked: boolean): void {
this.latestPhoneLockState = locked;
this.emitAsync(this.phoneLockStateListeners, locked);
}
/** @internal */
handleEvenAppConflict(message: string): void { this.emitAsync(this.evenAppConflictListeners, message); }
/** @internal */
handleFrameMetrics(paintMs: number, transmitMs: number, tileCount: number): void {
const metrics = { paintMs: nonNegativeNumber(paintMs), transmitMs: nonNegativeNumber(transmitMs), tileCount: nonNegativeNumber(tileCount) };
const waiters = Array.from(this.frameMetricWaiters);
this.frameMetricWaiters.clear();
for (const waiter of waiters) setTimeout(() => waiter(metrics), 0);
this.emitAsync(this.frameMetricsListeners, metrics);
}
/** @internal */
handleFrameFinished(frameId: number, outcome: string): void { this.recordFrameFinished(frameId, outcome); }
/** @internal */
handleFirmwareInfo(leftVersion: string, rightVersion: string, extension: string): void {
this.emitAsync(this.firmwareInfoListeners, { leftVersion, rightVersion, extension });
}
/** @internal */
handleAncsAuthorization(authorized: boolean): void { this.emitAsync(this.ancsAuthorizationListeners, authorized); }
/** @internal */
handleAncsRelayFrame(frame: Uint8Array): void { this.emitAsync(this.ancsRelayListeners, frame); }
private emitAsync<T>(listeners: Set<(value: T) => void>, value: T): void {
const snapshot = Array.from(listeners);
setTimeout(() => {
for (const listener of snapshot) listener(value);
}, 0);
}
/** Same FIFO as the Android bridge: native calls stay ordered; the fast path skips a task hop when idle. */
private enqueueNativeCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
if (inlineWhenIdle && this.queuedNativeCalls === 0) {
try {
return Promise.resolve(operation());
} catch (error) {
return Promise.reject(error);
}
}
this.queuedNativeCalls++;
const run = () =>
new Promise<T>((resolve, reject) => {
setTimeout(() => {
this.queuedNativeCalls--;
try {
resolve(operation());
} catch (error) {
reject(error);
}
}, 0);
});
const result = this.nativeCallQueue.then(run, run);
this.nativeCallQueue = result.then(() => undefined, () => undefined);
return result;
}
// ----- listeners -------------------------------------------------------------------
onStateChange(listener: (state: CommunicatorState) => void): () => void {
this.stateListeners.add(listener);
return () => this.stateListeners.delete(listener);
}
onRingEvent(listener: (event: RawInputEvent) => void): () => void {
this.ringListeners.add(listener);
return () => this.ringListeners.delete(listener);
}
onBatteryState(listener: (state: HeadsetBatteryState) => void): () => void {
this.batteryListeners.add(listener);
return () => this.batteryListeners.delete(listener);
}
onSilentMode(listener: (silent: boolean) => void): () => void {
this.silentModeListeners.add(listener);
return () => this.silentModeListeners.delete(listener);
}
onWearState(listener: (wearing: boolean) => void): () => void {
this.wearStateListeners.add(listener);
if (this.latestWearState !== null) {
const wearing = this.latestWearState;
setTimeout(() => { if (this.wearStateListeners.has(listener)) listener(wearing); }, 0);
}
return () => this.wearStateListeners.delete(listener);
}
onPhoneLockState(listener: (locked: boolean) => void): () => void {
this.phoneLockStateListeners.add(listener);
if (this.latestPhoneLockState !== null) {
const locked = this.latestPhoneLockState;
setTimeout(() => { if (this.phoneLockStateListeners.has(listener)) listener(locked); }, 0);
}
return () => this.phoneLockStateListeners.delete(listener);
}
onEvenAppConflict(listener: (message: string) => void): () => void {
this.evenAppConflictListeners.add(listener);
return () => this.evenAppConflictListeners.delete(listener);
}
onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void {
this.frameMetricsListeners.add(listener);
return () => this.frameMetricsListeners.delete(listener);
}
onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void {
this.firmwareInfoListeners.add(listener);
return () => this.firmwareInfoListeners.delete(listener);
}
getNativeCommunicator(): any {
return this.communicator;
}
private recordFrameFinished(frameId: number, outcome: string): void {
if (!Number.isFinite(frameId) || frameId <= 0) return;
this.finishedFrameOutcomes.set(frameId, outcome);
while (this.finishedFrameOutcomes.size > 128) {
const oldest = this.finishedFrameOutcomes.keys().next().value;
if (oldest === undefined) break;
this.finishedFrameOutcomes.delete(oldest);
}
const waiters = this.frameFinishedWaiters.get(frameId);
if (waiters) {
this.frameFinishedWaiters.delete(frameId);
for (const waiter of waiters) setTimeout(() => waiter(outcome), 0);
}
}
waitForFrameFinished(frameId: number, timeoutMs: number): Promise<string | null> {
const known = this.finishedFrameOutcomes.get(frameId);
if (known !== undefined) return Promise.resolve(known);
if (frameId <= 0) return Promise.resolve(null);
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
return new Promise((resolve) => {
let settled = false;
const onFinished = (outcome: string) => {
if (settled) return;
settled = true;
clearTimeout(timeoutHandle);
resolve(outcome);
};
let waiters = this.frameFinishedWaiters.get(frameId);
if (!waiters) {
waiters = new Set();
this.frameFinishedWaiters.set(frameId, waiters);
}
waiters.add(onFinished);
const timeoutHandle = setTimeout(() => {
if (settled) return;
settled = true;
const pending = this.frameFinishedWaiters.get(frameId);
if (pending) {
pending.delete(onFinished);
if (pending.size === 0) this.frameFinishedWaiters.delete(frameId);
}
resolve(null);
}, delayMs);
});
}
waitForNextFrameMetrics(timeoutMs: number): Promise<FrameMetrics | null> {
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
return new Promise((resolve) => {
let settled = false;
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
const complete = (metrics: FrameMetrics | null) => {
if (settled) return;
settled = true;
if (timeoutHandle !== null) clearTimeout(timeoutHandle);
this.frameMetricWaiters.delete(onMetrics);
resolve(metrics);
};
const onMetrics = (metrics: FrameMetrics) => complete(metrics);
this.frameMetricWaiters.add(onMetrics);
timeoutHandle = setTimeout(() => complete(null), delayMs);
});
}
// ----- session ---------------------------------------------------------------------
async start(): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.start(); });
}
async setG2ScreenOn(screenOn: boolean): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.setG2ScreenOnScreenOn(Boolean(screenOn)));
}
async setFirmwareDebugFlags(enabled: boolean): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.setFirmwareDebugFlagsEnabled(Boolean(enabled)));
}
/** Set lens brightness: auto (ambient sensor) or an explicit 0-100 level. */
async setBrightness(autoAdjust: boolean, level: number): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.setBrightnessAutoAdjustBrightnessLevel(Boolean(autoAdjust), Math.round(level)));
}
async enableWearDetectionAndRequestState(): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.enableWearDetectionAndRequestState());
}
isSessionReady(): boolean {
return Boolean(this.communicator.isSessionReady());
}
/** The platform's lock/screen signal (the iOS host also observes protected data itself). */
onPhoneLockSignal(): void {
this.communicator.onPhoneLockSignal();
}
// ----- compositor ------------------------------------------------------------------
async configureCompositorScreen(width: number, height: number): Promise<void> {
await this.enqueueNativeCall(() => {
this.communicator.configureCompositorScreenWidthHeight(Math.round(width), Math.round(height));
});
}
/** Phone-UI preview of the current composited screen, or null if none yet. */
getCompositePreview(green = false): ImageSource | null {
const frame = this.communicator.previewComposite();
if (!frame) return null;
const width = Number(this.communicator.compositeWidth()), height = Number(this.communicator.compositeHeight());
if (width <= 0 || height <= 0) return null;
return previewPixels(fromData(frame.pixels), width, height, green);
}
/** Save the current composite as a 4-bit grayscale PNG under Documents; returns the path (empty if none). Cropping is not supported on iOS. */
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
if (crop) return "";
const png = this.communicator.screenshotPng();
if (!png) return "";
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
File.fromPath(target).writeSync(png);
return target;
}
/** Screen recording is not available on iOS yet. */
startScreenRecording(): void {}
recordScreenFrame(): void {}
stopScreenRecording(): string { return ""; }
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
const transparency = options.transparency === "color-key" ? 1 : 0;
await this.enqueueNativeCall(() => {
this.communicator.configureSurfaceIdXYWidthHeightZOrderTransparency(
id, Math.round(options.x), Math.round(options.y), Math.round(options.width), Math.round(options.height),
Math.round(options.zOrder), transparency,
);
});
}
async removeSurface(id: string): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.removeSurfaceId(id); });
}
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
const snapshot = new Uint8Array(bytes);
await this.enqueueNativeCall(() => this.communicator.submitShellSceneBytesPaintMsFrameId(toData(snapshot), Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId))), true);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
await this.enqueueNativeCall(() => {
this.communicator.setUnderlayDimBelowZOrderFactor256(Math.round(belowZOrder), dimFactor256(factor));
});
}
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.setSurfaceVisibleIdVisible(id, Boolean(visible)); });
}
async setScreenBlanked(blanked: boolean): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); });
}
async submitSurfaceFrame(
surfaceId: string,
pixels8bpp: Uint8Array,
rect: { x: number; y: number; width: number; height: number },
fingerprint: string,
paintMs = -1,
frameId = 0,
glyphs: ArrayBuffer | null = null,
): Promise<void> {
const snapshot = new Uint8Array(pixels8bpp);
const draws = glyphs ? toData(new Uint8Array(glyphs)) : null;
await this.enqueueNativeCall(() => {
this.communicator.submitSurfaceFramePixels8bppSurfaceIdRectXRectYRectWidthRectHeightContentFingerprintPaintMsFrameIdGlyphs(
toData(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), Math.round(rect.width), Math.round(rect.height),
fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), draws,
);
}, true);
}
// ----- controls --------------------------------------------------------------------
async disconnect(): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.disconnect());
}
async sendShutdown(exitMode = 0): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.sendShutdownExitMode(exitMode)));
}
async sendCfwCleanup(): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.sendCfwCleanup()));
}
async suspendEvenHubSession(): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.suspendEvenHubSession()));
}
async resumeEvenHubSession(): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.resumeEvenHubSession()));
}
async setFaceclawWakeLeaseEnabled(enabled: boolean): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.setFaceclawWakeLeaseEnabledEnabled(Boolean(enabled))));
}
async awaitEvenHubSessionReady(timeoutMs: number): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.awaitEvenHubSessionReadyTimeoutMs(Math.round(nonNegativeNumber(timeoutMs)))));
}
/** Play a CFW mode-5 kind-4 tone sequence (complete wire payload). */
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
const snapshot = new Uint8Array(payload);
await this.enqueueNativeCall(() => { this.communicator.playBuzzerSequencePayload(toData(snapshot)); });
}
async close(): Promise<void> {
this.setAncsListeners(false);
await this.enqueueNativeCall(() => this.communicator.close());
}
// ----- iOS extras: compass, microphone, iPhone notifications (ANCS) ----------------------
/** CFW mode-10 compass on behalf of `owner`; the magnetometer runs while any owner wants it. */
setCompassEnabled(enabled: boolean, owner = "compass"): void {
this.communicator.setCompassEnabledForOwnerOwnerEnable(owner, Boolean(enabled));
}
/** Compass heading/calibration events, delivered on the main queue. */
addCompassListener(listener: (event: CompassEvent) => void): () => void {
const proxy = CompassListener.new() as CompassListener;
proxy.handler = listener;
this.communicator.addCompassListenerListener(proxy);
return () => { this.communicator.removeCompassListenerListener(proxy); };
}
/**
* Serialized glasses microphone enable/disable (the voice bridge's
* IosMicrophoneSession contract). A release while an enable awaits its ACK
* still wins: the token check drops the stale packets.
*/
setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise<void> {
const token = ++this.microphoneToken;
const work = this.microphoneWork.catch(() => {}).then(async () => {
if (token !== this.microphoneToken) return;
if (!enabled) {
this.audioListener = null;
await this.enqueueNativeCall(() => this.communicator.stopG2AudioCapture());
return;
}
if (!this.isSessionReady()) throw new Error("Connect the glasses before starting voice input.");
const proxy = AudioPacketListener.new() as AudioPacketListener;
proxy.handler = (packet) => { if (token === this.microphoneToken && listener) listener(packet); };
this.audioListener = proxy;
const started = await this.enqueueNativeCall(() => Boolean(this.communicator.startG2AudioCaptureNsDataListener(proxy)));
if (!started) {
this.audioListener = null;
throw new Error("The glasses did not enable their microphone. Reconnect and try again.");
}
});
this.microphoneWork = work;
return work;
}
/** Ask CoreBluetooth for ANCS on the right arm's next connection (call before start()). */
setRequiresAncs(required: boolean): void {
this.communicator.setRequiresAncsRequired(Boolean(required));
this.setAncsListeners(required);
}
isAncsAuthorized(): boolean {
return Boolean(this.communicator.isAncsAuthorized());
}
onAncsAuthorization(listener: (authorized: boolean) => void): () => void {
this.ancsAuthorizationListeners.add(listener);
return () => this.ancsAuthorizationListeners.delete(listener);
}
/** Relay frames ('A','N',...) received on the right arm, for the ANCS client. */
onAncsRelayFrame(listener: (frame: Uint8Array) => void): () => void {
this.ancsRelayListeners.add(listener);
return () => this.ancsRelayListeners.delete(listener);
}
/** Queue one relay packet for the right arm; the Kotlin side writes it off the JS thread. */
async writeRawToRight(packet: Uint8Array): Promise<void> {
if (!this.isSessionReady()) throw new Error("Glasses are not connected.");
const snapshot = new Uint8Array(packet);
this.communicator.writeRawToRightPacket(toData(snapshot));
}
/** The right arm's usable write payload for relay packets (20 until connected). */
rightWriteLimit(): number {
return Number(this.communicator.rightWriteLimit()) || 20;
}
private setAncsListeners(install: boolean): void {
if (install && !this.ancsProxy) {
this.ancsProxy = AncsListener.new() as AncsListener;
this.ancsProxy.bridge = this;
this.communicator.setAncsListenerListener(this.ancsProxy);
} else if (!install && this.ancsProxy) {
this.communicator.setAncsListenerListener(null);
this.ancsProxy = null;
}
}
}
+97 -56
View File
@@ -1,83 +1,124 @@
import { Application } from '@nativescript/core'
import { StockConnection } from '../g2/stock-connection'
import { flashLensImage, validateFirmware } from '../g2/firmware-ota'
import { CFW_PATCH_SET } from '../g2/firmware/cfw-patches'
import { firmwareSha256, readFirmwareFile } from './firmware-files.ios'
import { iosBluetooth } from './ios-bluetooth'
import type { FlashState, FlashProgress } from './firmware-flasher'
export type { FlashState, FlashProgress } from './firmware-flasher'
import { kotlinListener } from './kotlin-listener.ios'
import type { FlashState, FlashProgress } from '../g2/firmware-types'
export type { FlashState, FlashProgress } from '../g2/firmware-types'
declare const FaceclawKitIosFirmwareFlasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any
/**
* iOS twin of the Android FirmwareFlasher wrapper. The OTA transfer itself is the shared
* Kotlin OtaFlashFlow over a CoreBluetooth central (FaceclawKitIosFirmwareFlasher); this
* wrapper keeps the iOS-only policy around it: the image must still hash to the prepared
* custom (or pinned stock) firmware, the phone must stay in the foreground with the screen
* awake, and MAC addresses are mapped to peripheral identifiers first.
*/
export class FirmwareFlasher {
private logs = new Set<(line: string) => void>()
private states = new Set<(state: FlashState, detail: string) => void>()
private progresses = new Set<(value: FlashProgress) => void>()
private completes = new Set<(success: boolean, detail: string) => void>()
private link = new StockConnection(iosBluetooth(), line => this.log(line))
private flasher: any = null
private listenerProxy: object | null = null
private readonly logListeners = new Set<(line: string) => void>()
private readonly progressListeners = new Set<(progress: FlashProgress) => void>()
private readonly stateListeners = new Set<(state: FlashState, detail: string) => void>()
private readonly completeListeners = new Set<(success: boolean, detail: string) => void>()
private started = false
private finished = false
private cancelled = false
private sleeps = new Set<() => void>()
private foregroundError = ''
constructor(private addresses: { right: string; left: string }, private path: string) {}
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: FlashState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
onProgress(fn: (value: FlashProgress) => void): () => void { this.progresses.add(fn); return () => this.progresses.delete(fn) }
onComplete(fn: (success: boolean, detail: string) => void): () => void { this.completes.add(fn); return () => this.completes.delete(fn) }
private log(line: string): void { for (const fn of this.logs) fn(line) }
private state(state: FlashState, detail = ''): void { for (const fn of this.states) fn(state, detail) }
start(): void { if (!this.started) { this.started = true; void this.run() } }
private previousIdle = false
constructor(private readonly addresses: { right: string; left: string }, private readonly path: string) {}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener)
return () => this.logListeners.delete(listener)
}
onProgress(listener: (progress: FlashProgress) => void): () => void {
this.progressListeners.add(listener)
return () => this.progressListeners.delete(listener)
}
onStateChange(listener: (state: FlashState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
onComplete(listener: (success: boolean, detail: string) => void): () => void {
this.completeListeners.add(listener)
return () => this.completeListeners.delete(listener)
}
start(): void {
if (this.started) return
this.started = true
void this.run()
}
private readonly backgrounded = () => {
this.foregroundError = 'Flashing was interrupted because Faceclaw left the foreground. Keep both lenses charged, return to Faceclaw, and retry both lenses.'
this.cancel()
}
private async run(): Promise<void> {
const previousIdle = UIApplication.sharedApplication.idleTimerDisabled
this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled
UIApplication.sharedApplication.idleTimerDisabled = true
Application.on(Application.suspendEvent, this.backgrounded)
let success = false, detail = ''
try {
this.link.check()
if (UIApplication.sharedApplication.applicationState !== UIApplicationState.Active) throw new Error('Keep Faceclaw open on the iPhone while flashing.')
this.state('validating')
this.emit(this.stateListeners, 'validating', '')
const image = readFirmwareFile(this.path)
const hash = firmwareSha256(image.buffer)
if (![CFW_PATCH_SET.outputSha256, CFW_PATCH_SET.baseSha256].includes(hash)) throw new Error('Prepared firmware failed SHA-256 verification. Prepare the firmware again before flashing.')
const segments = validateFirmware(image)
await this.link.resolve({ ...this.addresses, ring: '' })
for (const role of ['left', 'right']) {
if (role === 'right') {
this.state('rebooting', 'Left lens done. Both lenses reboot briefly; reconnecting for the right lens.')
await this.delay(5000)
}
this.state('connecting', role)
const deadline = Date.now() + (role === 'left' ? 30_000 : 120_000)
for (;;) {
try { await this.link.connect(role, true); await this.link.authenticate(role); break }
catch (error) { this.link.check(); this.link.disconnect(role); if (Date.now() >= deadline) throw error; await this.delay(2500) }
}
await this.delay(2500)
this.state('flashing', role)
await flashLensImage(this.link, role, image, segments, value => { for (const fn of this.progresses) fn(value) }, line => this.log(line), ms => this.delay(ms))
this.link.disconnect(role)
}
success = true; detail = 'Both lenses flashed. Your glasses are rebooting.'
const identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
if (this.cancelled) throw new Error('Cancelled.')
this.flasher = FaceclawKitIosFirmwareFlasher.alloc().initWithRightAddressLeftAddressFirmwarePath(identifiers.right ?? '', identifiers.left ?? '', this.path)
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: (
lens: string, componentIndex: number, componentCount: number, blockIndex: number, blockCount: number, bytesSent: number, bytesTotal: number,
) => this.emit(this.progressListeners, {
lens: String(lens ?? ''), componentIndex: Number(componentIndex), componentCount: Number(componentCount),
blockIndex: Number(blockIndex), blockCount: Number(blockCount), bytesSent: Number(bytesSent), bytesTotal: Number(bytesTotal),
}),
onStateStateDetail: (state: string, detail: string) => {
// The terminal state is reported by finish() so a foreground/cancel reason can replace the detail.
const name = String(state) as FlashState
if (name !== 'done' && name !== 'error') this.emit(this.stateListeners, name, String(detail ?? ''))
},
onCompleteSuccessDetail: (success: boolean, detail: string) => this.finish(Boolean(success), String(detail ?? '')),
})
this.flasher.setListenerListener(this.listenerProxy)
this.flasher.start()
} catch (error) {
detail = this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : String((error as Error).message ?? error))
} finally {
Application.off(Application.suspendEvent, this.backgrounded)
UIApplication.sharedApplication.idleTimerDisabled = previousIdle
this.link.close()
this.finish(false, String((error as Error).message ?? error))
}
this.state(success ? 'done' : 'error', detail)
for (const fn of this.completes) fn(success, detail)
}
private delay(ms: number): Promise<void> {
this.link.check()
return new Promise((resolve, reject) => {
const cancel = () => { clearTimeout(timer); this.sleeps.delete(cancel); reject(new Error('Cancelled.')) }
const timer = setTimeout(() => { this.sleeps.delete(cancel); resolve() }, ms)
this.sleeps.add(cancel)
})
private finish(success: boolean, detail: string): void {
if (this.finished) return
this.finished = true
Application.off(Application.suspendEvent, this.backgrounded)
UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle
try { this.flasher?.close() } catch { /* ignore */ }
const message = success ? detail
: this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : detail)
this.emit(this.stateListeners, success ? 'done' : 'error', message)
this.emit(this.completeListeners, success, message)
}
cancel(): void { this.cancelled = true; this.link.close(); for (const cancel of [...this.sleeps]) cancel() }
cancel(): void {
this.cancelled = true
try { this.flasher?.cancel() } catch { /* ignore */ }
// Before the Kotlin flow exists (validation, address resolution) nothing will report completion.
if (!this.flasher) this.finish(false, 'Cancelled.')
}
close(): void { this.cancel() }
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const snapshot = Array.from(listeners)
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
}
}
+87 -77
View File
@@ -1,88 +1,98 @@
import { StockConnection } from '../g2/stock-connection'
import { createFlashPrompt, flashPromptSelection } from '../g2/flash-prompt-protocol'
import * as p from '../g2/ble-protocol'
import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
declare const FaceclawKitIosFlashPrompt: any, FaceclawKitFaceclawFlashPromptListener: any
function normalizePercent(value: number): number | null {
const n = Number(value)
return Number.isFinite(n) && n >= 0 ? n : null
}
/**
* iOS twin of the Android FlashPromptCommunicator wrapper over the shared Kotlin
* FlashPromptFlow (FaceclawKitIosFlashPrompt): connects and authenticates both arms, shows
* the pre-flash Yes/No page on the glasses (unless skipPrompt) and reads each arm's battery.
*/
export class FlashPromptCommunicator {
private logs = new Set<(line: string) => void>()
private states = new Set<(state: FlashPromptState, detail: string) => void>()
private results = new Set<(approved: boolean) => void>()
private batteries = new Set<(battery: FlashPromptBattery) => void>()
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) })
private communicator: any = null
private listenerProxy: object | null = null
private readonly logListeners = new Set<(line: string) => void>()
private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>()
private readonly resultListeners = new Set<(approved: boolean) => void>()
private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>()
private started = false
private closed = false
private started = false
private heartbeat: ReturnType<typeof setInterval> | null = null
private stopSelection: (() => void) | null = null
constructor(private addresses: { right: string; left: string }, private warning: string, private options?: { skipPrompt?: boolean }) {}
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: FlashPromptState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
onResult(fn: (approved: boolean) => void): () => void { this.results.add(fn); return () => this.results.delete(fn) }
onBattery(fn: (battery: FlashPromptBattery) => void): () => void { this.batteries.add(fn); return () => this.batteries.delete(fn) }
private state(state: FlashPromptState, detail = ''): void { for (const fn of this.states) fn(state, detail) }
start(): void { if (!this.started) { this.started = true; void this.run() } }
constructor(
private readonly addresses: { right: string; left: string },
private readonly warningText: string,
private readonly options?: { skipPrompt?: boolean },
) {}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener)
return () => this.logListeners.delete(listener)
}
onStateChange(listener: (state: FlashPromptState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
onBattery(listener: (battery: FlashPromptBattery) => void): () => void {
this.batteryListeners.add(listener)
return () => this.batteryListeners.delete(listener)
}
onResult(listener: (approved: boolean) => void): () => void {
this.resultListeners.add(listener)
return () => this.resultListeners.delete(listener)
}
start(): void {
if (this.started) return
this.started = true
void this.run()
}
private async run(): Promise<void> {
let identifiers: Record<string, string>
try {
this.state('connecting')
await this.link.resolve({ ...this.addresses, ring: '' })
// Complete pairing for one peripheral before starting the other.
for (const role of ['right', 'left']) {
await this.link.connect(role)
if (role === 'right') this.state('connected')
await this.link.authenticate(role)
}
await this.link.request('right', p.SID.launch, p.prelude, 3500, 156)
if (!this.options?.skipPrompt) {
// Listen before creating the page: an immediate tap may precede its ACK.
const selection = this.selection()
// Attach a rejection handler immediately, even while create-page is pending.
void selection.catch(() => {})
for (let attempt = 0; ; attempt++) {
try { await this.link.request('right', p.SID.hub, magic => createFlashPrompt(magic, this.warning)); break }
catch (error) { this.link.check(); if (attempt) throw error }
}
this.state('prompting')
this.heartbeat = setInterval(() => {
void this.link.write('right', p.SID.hub, p.heartbeat(this.link.nextMagic())).catch(error => {
if (!this.closed) this.state('error', String(error.message ?? error))
this.close()
})
}, 4000)
const approved = await selection
this.stopHeartbeat()
// Stock shutdown returns the glasses to their normal UI before OTA.
await this.link.write('right', p.SID.hub, p.concat(p.integer(1, 9), p.integer(2, this.link.nextMagic()), p.bytes(11, p.integer(1, 0))))
if (!approved) { for (const fn of this.results) fn(false); return }
}
this.state('battery')
const battery: FlashPromptBattery = { right: null, left: null }
for (const role of ['right', 'left'] as const) {
for (let attempt = 0; attempt < 2; attempt++) {
try {
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
const values = p.readBytes(message.payload, 4), value = values ? p.readInteger(values, 12, -1) : -1
if (value >= 0 && value <= 100) { battery[role] = value; break }
} catch (error) { this.link.check() }
}
}
for (const fn of this.batteries) fn(battery)
for (const fn of this.results) fn(true)
} catch (error) { if (!this.closed) this.state('error', String((error as Error).message ?? error)) }
finally { this.close() }
this.emit(this.stateListeners, 'connecting', '')
identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
} catch (error) {
if (!this.closed) this.emit(this.stateListeners, 'error', String((error as Error).message ?? error))
return
}
if (this.closed) return
this.communicator = FaceclawKitIosFlashPrompt.alloc().initWithRightAddressLeftAddressWarningTextSkipPrompt(
identifiers.right ?? '', identifiers.left ?? '', this.warningText, Boolean(this.options?.skipPrompt))
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFlashPromptListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onStateStateDetail: (state: string, detail: string) =>
this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? '')),
onBatteryRightPercentLeftPercent: (rightPercent: number, leftPercent: number) =>
this.emit(this.batteryListeners, { right: normalizePercent(rightPercent), left: normalizePercent(leftPercent) }),
onResultApproved: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)),
})
this.communicator.setListenerListener(this.listenerProxy)
this.communicator.start()
}
cancel(): void {
this.closed = true
try { this.communicator?.cancel() } catch { /* ignore */ }
}
private selection(): Promise<boolean> {
return new Promise((resolve, reject) => {
const cleanup = () => { clearTimeout(timer); off(); this.stopSelection = null }
const off = this.link.onMessage((role, message) => {
const result = role === 'right' ? flashPromptSelection(message) : null
if (result !== null) { cleanup(); resolve(result) }
})
const timer = setTimeout(() => { cleanup(); reject(new Error('No response from the glasses.')) }, 120_000)
this.stopSelection = () => { cleanup(); reject(new Error('Cancelled.')) }
})
close(): void {
this.closed = true
try { this.communicator?.close() } catch { /* ignore */ }
}
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const snapshot = Array.from(listeners)
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
}
private stopHeartbeat(): void { if (this.heartbeat) clearInterval(this.heartbeat); this.heartbeat = null }
cancel(): void { this.close() }
close(): void { this.closed = true; this.stopHeartbeat(); this.stopSelection?.(); this.link.close() }
}
+56 -73
View File
@@ -1,75 +1,80 @@
import { ApplicationSettings } from '@nativescript/core'
import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity'
import { hexToBytes } from '../util/hex-util'
import { kotlinListener } from './kotlin-listener.ios'
declare const FaceclawBluetooth: any
type NativeEvent = { kind: string; id?: number; error?: string; details?: ConnectionDetails; state?: number;
identifier?: string; characteristic?: string; data?: string; message?: string } & Partial<IosAdvertisement>
export type ConnectionDetails = { characteristics: string[]; maxWrite: number }
export type BluetoothEvent = NativeEvent | { kind: 'device'; device: IosDevice }
type Pending = { resolve: (details: ConnectionDetails) => void; reject: (error: Error) => void; timer: ReturnType<typeof setTimeout> }
declare const FaceclawKitIosBleCentral: any, FaceclawKitIosProtocolPlatform: any, FaceclawKitIosBleScanListener: any
/** One central manager per app, with asynchronous main-queue GATT operations. */
/** CBManagerState raw values reported by the Kotlin central. */
export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_POWERED_OFF = 4, BLE_STATE_POWERED_ON = 5
export type BluetoothEvent =
| { kind: 'state'; state: number; authorization: number }
| { kind: 'advertisement'; identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean }
| { kind: 'device'; device: IosDevice }
| { kind: 'scan-started' }
| { kind: 'scan-stopped' }
/**
* Scanning and MAC-address-to-identifier resolution over the shared Kotlin CoreBluetooth
* central (FaceclawKitIosBleCentral). GATT sessions no longer pass through here: the
* session and stock-firmware flows own their own Kotlin centrals. What stays in TypeScript
* is the Even classification of advertisements and the persisted identifier map, which the
* Android address model (MACs) needs on iOS.
*/
export class IosBluetooth {
private readonly native = FaceclawBluetooth.new()
private readonly native = FaceclawKitIosBleCentral.alloc().initWithPlatform(FaceclawKitIosProtocolPlatform.shared)
private readonly listeners = new Set<(event: BluetoothEvent) => void>()
private readonly pending = new Map<number, Pending>()
private readonly advertisements = new Map<string, IosAdvertisement>()
readonly devices = new Map<string, IosDevice>()
private requestId = 0
private scanTimer: ReturnType<typeof setTimeout> | null = null
private scanGeneration = 0
private readonly scanListener: object
state = 0
authorization = 0
scanning = false
constructor() {
this.native.eventHandler = (json: string) => {
try { this.receive(JSON.parse(json)) } catch (error) { console.error(`[ios-ble] ${error}`) }
}
this.scanListener = kotlinListener(FaceclawKitIosBleScanListener, {
onBluetoothStateStateAuthorization: (state: number, authorization: number) => {
this.state = Number(state); this.authorization = Number(authorization)
this.emit({ kind: 'state', state: this.state, authorization: this.authorization })
},
onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean) => {
// The Kotlin central already merges split advertisements and bounds its cache.
const raw: IosAdvertisement = { identifier: String(identifier), name: String(name ?? ''), manufacturerData: String(manufacturerData ?? ''), rssi: Number(rssi), connectable: connectable !== false }
this.emit({ kind: 'advertisement', ...raw })
const device = identifyIosPeripheral(raw)
if (device) {
this.devices.set(device.address, device)
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
this.emit({ kind: 'device', device })
}
},
})
this.native.setScanListenerListener(this.scanListener)
}
onEvent(listener: (event: BluetoothEvent) => void): () => void {
this.listeners.add(listener); return () => { this.listeners.delete(listener) }
}
private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) }
private receive(event: NativeEvent): void {
if (event.kind === 'completion') {
const pending = this.pending.get(event.id!)
if (pending) {
this.pending.delete(event.id!); clearTimeout(pending.timer)
if (event.error) pending.reject(new Error(event.error)); else pending.resolve(event.details!)
}
return
}
if (event.kind === 'state') this.state = event.state!
if (event.kind === 'advertisement') {
const previous = this.advertisements.get(event.identifier!)
const raw: IosAdvertisement = { identifier: event.identifier!, name: event.name || previous?.name || '',
manufacturerData: event.manufacturerData || previous?.manufacturerData || '', rssi: event.rssi!, connectable: event.connectable !== false }
// Bound the cache even in a crowded radio environment.
if (this.advertisements.size >= 512 && !this.advertisements.has(raw.identifier)) this.advertisements.delete(this.advertisements.keys().next().value!)
this.advertisements.set(raw.identifier, raw)
const device = identifyIosPeripheral(raw)
if (device) {
this.devices.set(device.address, device)
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
this.emit({ kind: 'device', device })
}
return
}
this.emit(event)
/** Creates the central (first use shows the Bluetooth permission prompt) without blocking the JS thread. */
private ensureCentral(): void {
// A zero timeout returns immediately after creating the central; the state arrives
// through the scan listener on the main queue.
this.native.waitForPoweredOnTimeoutMs(0)
}
ensureReady(): Promise<void> {
if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve()
return new Promise((resolve, reject) => {
const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() }
const off = this.onEvent(event => {
if (event.kind !== 'state') return
const state = (event as NativeEvent).state
if (state === 5) finish()
else if (state === 2) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
else if (state === 3) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
else if (state === 4) finish(new Error('Turn on Bluetooth, then try again.'))
if (event.state === BLE_STATE_POWERED_ON) finish()
else if (event.state === BLE_STATE_UNSUPPORTED) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
else if (event.state === BLE_STATE_UNAUTHORIZED) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
else if (event.state === BLE_STATE_POWERED_OFF) finish(new Error('Turn on Bluetooth, then try again.'))
})
const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000)
this.native.initializeBluetooth()
this.ensureCentral()
})
}
async startScan(durationMs = 12_000): Promise<void> {
@@ -77,17 +82,18 @@ export class IosBluetooth {
const generation = this.scanGeneration
await this.ensureReady()
if (generation !== this.scanGeneration) throw new Error('Bluetooth scan cancelled')
this.scanning = true; this.native.scan(true)
this.scanning = true; this.native.startScan()
this.scanTimer = setTimeout(() => this.stopScan(), durationMs)
this.emit({ kind: 'scan-started' })
}
stopScan(): void {
++this.scanGeneration
if (this.scanTimer) clearTimeout(this.scanTimer)
this.scanTimer = null; this.native.scan(false)
this.scanTimer = null
const wasScanning = this.scanning; this.scanning = false
if (wasScanning) this.emit({ kind: 'scan-stopped' })
if (wasScanning) { this.native.stopScan(); this.emit({ kind: 'scan-stopped' }) }
}
/** Peripheral identifiers for the stored MAC addresses, scanning for any that are not remembered. */
async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise<Record<string, string>> {
const generation = this.scanGeneration
await this.ensureReady()
@@ -109,7 +115,7 @@ export class IosBluetooth {
await new Promise<void>((resolve, reject) => {
const off = this.onEvent(event => {
if (event.kind === 'device') {
const device = (event as { device: IosDevice }).device
const device = event.device
for (const role of ['left', 'right', 'ring'] as const) {
if (addresses[role] === device.address && device.role === role) result[role] = device.identifier
}
@@ -123,29 +129,6 @@ export class IosBluetooth {
})
return result
}
private operation(identifier: string, call: (id: number) => void, timeoutMs = 10_000, disconnectOnTimeout = true): Promise<ConnectionDetails> {
const id = ++this.requestId
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
this.pending.delete(id); if (disconnectOnTimeout) this.native.disconnect(identifier)
reject(new Error('Bluetooth operation timed out. Wake the device and try connecting again.'))
}, timeoutMs)
this.pending.set(id, { resolve, reject, timer })
try { call(id) } catch (error) { clearTimeout(timer); this.pending.delete(id); reject(error) }
})
}
connect(identifier: string, requiresANCS = false): Promise<ConnectionDetails> { return this.operation(identifier, id => this.native.connectRequiresANCSRequestId(identifier, requiresANCS, id), 20_000) }
async subscribe(identifier: string, characteristic: string): Promise<void> {
// R1 exposes two notification channels; one may never complete its CCCD
// request. Keep the link alive so the other channel can still be tried.
await this.operation(identifier, id => this.native.subscribeCharacteristicRequestId(identifier, characteristic, id), 30_000, false)
}
async write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void> {
const copy = new Uint8Array(bytes)
const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength)
await this.operation(identifier, id => this.native.writeCharacteristicDataRequestId(identifier, characteristic, data, id))
}
disconnect(identifier: string): void { this.native.disconnect(identifier) }
forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) }
}
let instance: IosBluetooth | null = null
+10
View File
@@ -0,0 +1,10 @@
/**
* Implements a FaceclawKit (Kotlin) listener protocol with plain JavaScript methods.
* NativeScript maps the method names to the protocol's selectors by camel-casing the
* selector parts (`onLog:` -> `onLogLine`, `onState:detail:` -> `onStateStateDetail`, ...).
* Keep a reference to the returned object for as long as the Kotlin side may call it.
*/
export function kotlinListener<T extends object>(protocol: unknown, methods: T): T {
const NativeListener = (NSObject as any).extend(methods, { protocols: [protocol] })
return NativeListener.new() as T
}
+2 -28
View File
@@ -1,14 +1,9 @@
import { CFW_MSG_BOUNDING_BOX, CFW_MSG_CREATE_SURFACE, CFW_MSG_PRESENT, CFW_MSG_UPLOAD_RESOURCE } from "../g2/cfw-message-type";
import { GrayImage } from "../graphics/image"
import { encodeShellScene } from "../graphics/shell-scene"
import { decodeImageBytes } from './image-bytes.ios'
import * as protocol from '../g2/ble-protocol.ios'
import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios'
import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios'
import { lvglMetrics } from './lvgl-font.ios'
import { textureImageId } from './texture-atlas.ios'
import { IosTexturePlanner } from './texture-planner.ios'
/** Development-only checks of real NativeScript selectors and bulk data crossings. */
export function runKotlinProtocolSmokeTest(): void {
@@ -24,11 +19,6 @@ export function runKotlinProtocolSmokeTest(): void {
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2))))
const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 })
assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event')
const transport = new CfwTransport()
const packets = transport.encode(new Uint8Array(128).fill(9), 255, 3, 185)
assert(packets.length > 0 && packets.every(p => p.length <= 185), 'compression')
transport.reset()
assert(parseCfwAcks(new Uint8Array()) === null, 'invalid ACK')
const c = new SurfaceCompositor(3, 2)
c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' })
c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 })
@@ -37,28 +27,12 @@ export function runKotlinProtocolSmokeTest(): void {
c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255]))
assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition')
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
const textures = new IosTexturePlanner(), textured = new SurfaceCompositor(4, 2)
textured.configureSurface('icon', { x: 0, y: 0, width: 2, height: 2, zOrder: 0, transparency: 'opaque' })
const white = new Uint8Array(4).fill(255)
const icon = textureImageId('protocol-smoke-white', 2, 2, white)
const draw = new DataView(new ArrayBuffer(9)); draw.setUint8(0, 1); draw.setUint32(1, icon, true)
textured.submitSurfaceFrame('icon', white, { x: 0, y: 0, width: 2, height: 2 }, draw.buffer)
const snapshot = textured.compositeFrame(), packed = protocol.packGray4(snapshot.pixels, 4, 2)
const cold = textures.plan(null, packed, snapshot.textures, 1)
assert(!!cold && cold.resourceCommands.some(command => command[0] === CFW_MSG_UPLOAD_RESOURCE) && cold.payload[0] === CFW_MSG_PRESENT, 'texture upload and draw')
assert(textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length === 0, 'texture reuse')
textures.reset()
assert((textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length ?? 0) > 0, 'texture reset')
const selectedSurface = new GrayImage(8, 4, 1)
selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2)
const selectedPreview = new SurfaceCompositor(8, 4)
selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }]))
const selectedSnapshot = selectedPreview.compositeFrame()
assert(selectedSnapshot.pixels[10] === 240 && selectedSnapshot.pixels[9] === 0, 'menu selection depth and bridge')
const selectedPlan = new IosTexturePlanner().plan(null, protocol.packGray4(selectedSnapshot.pixels, 8, 4), selectedSnapshot.textures, 1)
assert(!!selectedPlan && selectedPlan.payload[0] === CFW_MSG_PRESENT && selectedPlan.resourceCommands.some(c => c[0] === CFW_MSG_CREATE_SURFACE), 'menu resource planning')
const changed = new Uint8Array(16); changed[0] = 255
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === CFW_MSG_BOUNDING_BOX, 'image update')
const selectedPixels = selectedPreview.composite()
assert(selectedPixels[10] === 240 && selectedPixels[9] === 0, 'menu selection depth and bridge')
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0,
payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
+141
View File
@@ -0,0 +1,141 @@
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { SurfaceCompositor } from "../graphics/surface-compositor";
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image";
import { previewPixels } from "./ios-graphics";
import { toData } from "./kotlin-data";
import type { FaceclawCommunicatorBridge, SurfaceOptions } from "./faceclaw-communicator.ios";
declare const FaceclawKitIosProtocol: any;
/** The display subset of the communicator bridge; see the Android twin. */
export type DisplayTarget = Pick<
FaceclawCommunicatorBridge,
| "configureCompositorScreen"
| "configureSurface"
| "removeSurface"
| "setSurfaceVisible"
| "setUnderlayDim"
| "setScreenBlanked"
| "submitSurfaceFrame"
| "submitShellScene"
| "waitForFrameFinished"
| "getCompositePreview"
| "saveScreenshot"
| "startScreenRecording"
| "recordScreenFrame"
| "stopScreenRecording"
>;
function timestamp(): string {
const now = new Date();
const pad = (value: number, width = 2) => String(value).padStart(width, "0");
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`;
}
/**
* Headless display target for iOS while no glasses session exists: the same
* shared Kotlin SurfaceCompositor a live session composes with, so the phone
* mirror, apps and shell render identically before connecting. Frames end
* at the compositor; there is no transmit backpressure to wait on.
*/
export class PreviewDisplayTarget implements DisplayTarget {
private compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT);
private onFrameComposited: (() => void) | null = null;
private released = false;
/** Receive a callback per applied frame, the stand-in for the connected path's frame-finished callback. */
activate(onFrameComposited: () => void): void {
this.onFrameComposited = onFrameComposited;
}
release(): void {
this.released = true;
this.onFrameComposited = null;
}
private composited(): void {
if (!this.released) this.onFrameComposited?.();
}
async configureCompositorScreen(width: number, height: number): Promise<void> {
if (Math.round(width) !== this.compositor.width || Math.round(height) !== this.compositor.height) {
this.compositor = new SurfaceCompositor(Math.round(width), Math.round(height));
}
}
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
this.compositor.configureSurface(id, {
x: Math.round(options.x), y: Math.round(options.y), width: Math.round(options.width), height: Math.round(options.height),
zOrder: Math.round(options.zOrder), transparency: options.transparency,
});
}
async removeSurface(id: string): Promise<void> {
this.compositor.removeSurface(id);
this.composited();
}
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
this.compositor.setSurfaceVisible(id, Boolean(visible));
this.composited();
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
this.compositor.setUnderlayDim(Math.round(belowZOrder), Math.max(0, Math.min(1, factor)));
this.composited();
}
async setScreenBlanked(blanked: boolean): Promise<void> {
this.compositor.setScreenBlanked(Boolean(blanked));
this.composited();
}
async submitSurfaceFrame(
surfaceId: string,
pixels8bpp: Uint8Array,
rect: { x: number; y: number; width: number; height: number },
_fingerprint: string,
_paintMs = -1,
_frameId = 0,
glyphs: ArrayBuffer | null = null,
): Promise<void> {
this.compositor.submitSurfaceFrame(surfaceId, pixels8bpp,
{ x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }, glyphs);
this.composited();
}
async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise<void> {
this.compositor.setShellScene(bytes);
this.composited();
}
/** Nothing transmits: every frame is finished as soon as it is composited. */
waitForFrameFinished(frameId: number, _timeoutMs: number): Promise<string | null> {
return Promise.resolve(frameId > 0 ? "composited" : null);
}
/** The current composite as gray pixels (row-major, compositor size). */
composite(): Uint8Array {
return this.compositor.composite();
}
getCompositePreview(green = false): ImageSource | null {
return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green);
}
/** Save the current composite as a 4-bit grayscale PNG under Documents; cropping is not supported on iOS. */
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
if (crop) return "";
const png = FaceclawKitIosProtocol.new().pngDataWidthHeight(toData(this.compositor.composite()), this.compositor.width, this.compositor.height);
if (!png) return "";
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
File.fromPath(target).writeSync(png);
return target;
}
startScreenRecording(): void {}
recordScreenFrame(): void {}
stopScreenRecording(): string { return ""; }
}
+10 -8
View File
@@ -1,14 +1,16 @@
import { createListenerPool, type InterfaceAddress } from '../remote/listeners';
declare const FaceclawRemoteInput: any;
declare const FaceclawKitIosRemoteInput: any;
/** The shared Kotlin RemoteInputSession over POSIX sockets; request notifications arrive on the main queue. */
export function remoteNative(onRequestReady: () => void) {
return createListenerPool(() => {
const native = FaceclawRemoteInput.alloc().init();
native.setRequestListener(onRequestReady);
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); },
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeResponse(id, response) };
const native = FaceclawKitIosRemoteInput.new();
native.setRequestListenerListener(onRequestReady);
return { start: (port, address) => String(native.startAddressPortAddress(port, address)), stop: () => { native.setRequestListenerListener(null); native.stop(); },
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeIdValue(id, response) };
});
}
export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(FaceclawRemoteInput.interfaces())); }
export function randomTokenSecret(): string { return String(FaceclawRemoteInput.shared().randomSecret()); }
export function tokenHash(value: string): string { return String(FaceclawRemoteInput.shared().tokenDigest(value)); }
function shared(): any { return FaceclawKitIosRemoteInput.companion.shared; }
export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(shared().interfaces())); }
export function randomTokenSecret(): string { return String(shared().randomSecret()); }
export function tokenHash(value: string): string { return String(shared().tokenDigestValue(value)); }
export function copyRemoteToken(value: string): void { UIPasteboard.generalPasteboard.string = value; }
+12 -21
View File
@@ -1,28 +1,19 @@
import { type SseListener } from './sse-types';
import { withUserAgent } from '../util/http';
import { kotlinListener } from './kotlin-listener.ios';
export { type SseListener } from './sse-types';
declare const FaceclawSseRequest: any;
declare const FaceclawKitIosSseRequest: any, FaceclawKitFaceclawSseListener: any;
/** Drain native events on the requesting JS isolate, never on a URLSession thread. */
/** The shared Kotlin SseStream over NSURLSession; the Kotlin side delivers every callback on the main queue, in order. */
export function openSseRequest(url: string, body: string, headers: Record<string, string>, listener: SseListener): { cancel(): void } {
const native = FaceclawSseRequest.alloc().initWithURLBodyHeaders(url, body, JSON.stringify(withUserAgent(headers)));
let ended = false;
const cancel = () => { if (ended) return; ended = true; clearInterval(timer); native.cancel(); };
const timer = setInterval(() => {
try {
for (const event of JSON.parse(native.takeEvents())) {
if (ended) break;
if (event.kind === 'line') listener.onLine(event.text);
else {
cancel();
if (event.kind === 'complete') listener.onComplete();
else if (event.kind === 'http-error') listener.onHttpError(event.code, event.text);
else listener.onFailure(event.text);
}
}
} catch (error) {
cancel(); listener.onFailure(String((error as Error)?.message ?? error));
}
}, 25);
return { cancel };
const once = (deliver: () => void) => { if (ended) return; ended = true; deliver(); };
const nativeListener = kotlinListener(FaceclawKitFaceclawSseListener, {
onLineLine: (line: string) => { if (!ended) listener.onLine(String(line ?? '')); },
onHttpErrorCodeBody: (code: number, errorBody: string) => once(() => listener.onHttpError(Number(code), String(errorBody ?? ''))),
onComplete: () => once(() => listener.onComplete()),
onFailureMessage_: (message: string) => once(() => listener.onFailure(String(message ?? ''))),
});
const native = FaceclawKitIosSseRequest.alloc().initWithUrlBodyHeadersJsonListener(url, body, JSON.stringify(withUserAgent(headers)), nativeListener);
return { cancel: () => { if (ended) return; ended = true; native.cancel(); } };
}
-13
View File
@@ -1,13 +0,0 @@
import { fromData, toData, nativeArray } from './kotlin-data'
import type { SessionTexturePlanner, TextureFrame, TexturePlan } from '../g2/texture-planner'
declare const FaceclawKitIosTexturePlanner: any
export class IosTexturePlanner implements SessionTexturePlanner {
private readonly native = FaceclawKitIosTexturePlanner.new()
reset(): void { this.native.reset() }
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null {
const result = this.native.planPreviousNextFrameFirstId(previous ? toData(previous) : null, toData(next), frame.native, firstId)
return result ? { payload: fromData(result.payload), resourceCommands: nativeArray(result.resourceCommands, fromData),
nextFid: result.nextFid, usedBytes: result.usedBytes } : null
}
}