import { ImageSource } from "@nativescript/core"; import { EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName } from "./events"; import { loadDeviceAddresses } from "./device-addresses"; import { ensureBlePermissions, ensureVoicePermissions } from "./android-permissions"; import { FaceclawCommunicatorBridge, type FrameMetrics, type RawInputEvent } from "../native/faceclaw-communicator"; import * as frameTimings from "../native/frame-timings"; import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service"; import { mediaControllerBridge } from "../native/media-controller"; import { nightscoutBridge } from "../native/nightscout-bridge"; import { onAndroidNotificationPosted } from "../native/notification-icons"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { firmwareIncompatibilityMessage } from "./firmware-compat"; import { beginRenderPass, endRenderPass } from "../util/render-freshness"; import { voiceControlBridge } from "../native/voice-control"; import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; import { closeDashboardTextSettingEditor, drawDashboard, logToDashboard, openAndroidNotificationFromSleep, openTelepromptDocument, receiveDashboardWindowInput, resetDashboardSleepTimerAndWake, setDashboardBatteryLevels, setDashboardActions, } from "../ui/dashboard"; import { inputEventToString, rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell"; import { makeLetterWindowIcon } from "../ui/shell/chrome-layer"; import { WorkerAppHost } from "../ui/shell/worker-window"; import { createLauncherWindow, LAUNCHER_SURFACE_ID } from "../ui/shell/launcher-app"; import { APP_VIEWPORT } from "../ui/shell/geometry"; import { elevenLabsApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, nightscoutSiteUrlSetting, onAnySettingChanged, rawScreenshotsEnabledSetting, saveVoiceRecordingsSetting, screenTimeoutSetting, screenTimeoutSettingToMs, systemCardNameSetting, voiceProviderSetting, type ConfigSettingString, } from "../ui/dashboard-settings"; import { saveRawScreenshot } from "../native/raw-screenshot"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; type ConnectionPhase = "disconnected" | "connecting" | "connected" | "charging" | "disconnecting"; export type DashboardSnapshot = { phase: ConnectionPhase; status: string; log: string; displayPreview: ImageSource | null; activeTextSettingId: string | null; activeTextSettingTitle: string; activeTextSettingValue: string; evenAppConflictMessage: string; evenAppConflictWarningVisible: boolean; firmwareWarningMessage: string; firmwareWarningVisible: boolean; rawScreenshotsEnabled: boolean; batteryOptimizationWarningVisible: boolean; }; type DashboardListener = (snapshot: DashboardSnapshot) => void; type LogLevel = "debug"|"info"|"warn"|"error"; const CONTAINER_NAME = "dashboard"; // Compositor surfaces: the dashboard window in the app viewport, and the // shell chrome (sidebar + top bar + overlays) above it with color-key // transparency. const DASHBOARD_WINDOW_SURFACE_ID = "window:dashboard"; const SHELL_SURFACE_ID = "shell"; const DASHBOARD_INTERVAL_MS = 60_000; const SCREEN_TIMEOUT_CHECK_MS = 1_000; const TRANSCRIBE_RENDER_INTERVAL_MS = 250; const FOREGROUND_NOTIFICATION_MIN_UPDATE_MS = 30_000; const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 6_000; const CONNECTED_PREVIEW_MIN_UPDATE_MS = 1_000; const EVEN_APP_DETECTED_MESSAGE = "The Even Realities app appears to be running. If Faceclaw has trouble connecting, open its app settings and force stop it."; function createInitialDisplayPreview(): ImageSource | null { return grayImageToPreviewSource(new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0)); } function formatTimestamp(date: Date): string { return date.toISOString().slice(11, 23); } function pad2(value: number): string { return String(value).padStart(2, "0"); } function formatTime24h(date: Date, includeSeconds: boolean): string { const hours = date.getHours(); const minutes = pad2(date.getMinutes()); const seconds = pad2(date.getSeconds()); return includeSeconds ? `${hours}:${minutes}:${seconds}` : `${hours}:${minutes}`; } function formatUtcOffset(date: Date): string { const offsetMinutes = -date.getTimezoneOffset(); const sign = offsetMinutes >= 0 ? "+" : "-"; const absoluteMinutes = Math.abs(offsetMinutes); const hours = Math.floor(absoluteMinutes / 60); const minutes = absoluteMinutes % 60; return `UTC${sign}${pad2(hours)}:${pad2(minutes)}`; } function formatForStatus(date: Date): string { return formatTime24h(date, true); } function eventName(eventType: number): string { return OsEventTypeName[eventType] ?? `UNKNOWN_${eventType}`; } function sourceName(eventSource: number): string { return EventSourceTypeName[eventSource] ?? `SOURCE_${eventSource}`; } function normalizeContainerName(name: string): string { return name.replace(/[^\x20-\x7e]+/g, ""); } function isDashboardContainerName(name: string): boolean { const normalized = normalizeContainerName(name); return normalized === CONTAINER_NAME || normalized.includes(CONTAINER_NAME); } /** Top-left viewport-sized crop of a full-lens dashboard paint. */ function cropToViewport(image: GrayImage): GrayImage { if (image.width === APP_VIEWPORT.width && image.height === APP_VIEWPORT.height) { return image; } const cropped = new GrayImage(APP_VIEWPORT.width, APP_VIEWPORT.height, 0); cropped.bitBlt(image, 0, 0, { width: APP_VIEWPORT.width, height: APP_VIEWPORT.height }); return cropped; } class DashboardController { private phase: ConnectionPhase = "disconnected"; private status = "Disconnected."; private log = ""; private activeTextSetting: ConfigSettingString | null = null; private evenNotificationActive = false; private evenAppConflictMessage = ""; private firmwareWarningMessage = ""; private batteryOptimizationWarningVisible = false; private displayPreview: ImageSource | null = createInitialDisplayPreview(); private readonly listeners = new Set(); private communicator: FaceclawCommunicatorBridge | null = null; private dashboardTimer: ReturnType | null = null; private screenTimeoutTimer: ReturnType | null = null; private transcribeRenderTimer: ReturnType | null = null; private offState: (() => void) | null = null; private offLog: (() => void) | null = null; private offRing: (() => void) | null = null; private offBattery: (() => void) | null = null; private offEvenAppConflict: (() => void) | null = null; private offFrameMetrics: (() => void) | null = null; private offFirmwareInfo: (() => void) | null = null; private offMedia: (() => void) | null = null; private offNightscout: (() => void) | null = null; private offVoiceStatus: (() => void) | null = null; private offVoiceWakeWord: (() => void) | null = null; private offAndroidNotification: (() => void) | null = null; private lastInput = "waiting..."; private lastSys = "none yet"; private renderInProgress = false; private renderQueued = false; private queuedRenderReason: "initial" | "interval" = "interval"; private queuedFrameId = 0; private shellRenderInProgress = false; private shellRenderQueued = false; // Last painted surface images, for compositing the phone-side preview. private lastShellImage: GrayImage | null = null; private lastWindowImage: GrayImage | null = null; // One shared worker hosts every stopwatch window; spawned on first launch. private stopwatchHost: WorkerAppHost | null = null; private nextWindowSerial = 1; private lastForegroundNotificationUpdateAtMs = 0; private lastConnectedPreviewUpdateAtMs = 0; // Consumed by the next renderDashboard: repaint with data sources not // allowed to serve stale caches (set after a frame painted with stale data). private nextRenderWantsFreshData = false; constructor() { const sharedActions = { disconnect: () => this.disconnect(), startTextSettingEdit: (setting: ConfigSettingString) => this.startTextSettingEdit(setting), endTextSettingEdit: () => this.endTextSettingEdit(), setTranscribeRenderActive: (active: boolean) => this.setTranscribeRenderActive(active), startVoiceCapture: () => this.startVoiceCapture(), stopVoiceCapture: () => this.stopVoiceCapture(), playBuzzerNote: (note: number, oct: number, beat: number) => this.playBuzzerNote(note, oct, beat), }; setDashboardActions({ ...sharedActions, requestRender: () => { void this.requestRender("interval").catch((error) => { this.appendLog(`requested render failed: ${this.formatError(error)}`); }); }, }); shell.configure({ actions: { ...sharedActions, // Shell overlays (voice dialog, long-press menu) live on the shell // surface, so their repaints go through the shell render path. requestRender: () => this.requestShellRender(), }, getScreenTimeoutMs: () => screenTimeoutSettingToMs(screenTimeoutSetting.get()), requestShellRender: () => this.requestShellRender(), onScreenStateChanged: (on) => { const communicator = this.communicator; if (!communicator) return; void (async () => { // Blanking is a compositor-level flag so worker-window surfaces go // dark too; retained state survives for instant wake. await communicator.setScreenBlanked(!on); await communicator.setG2ScreenOn(on); })().catch((error) => { this.appendLog(`screen state change failed: ${this.formatError(error)}`); }); if (on) this.requestShellRender(); }, }); // The launcher is pinned first in the sidebar and is the boot foreground. shell.registerWindow( createLauncherWindow({ actions: { ...sharedActions, requestRender: () => shell.foregroundWindow()?.requestRender(), }, apps: [{ appId: "stopwatch", label: "Stopwatch" }], launchApp: (appId) => this.launchApp(appId), submitFrame: (image, paintMs, frameId) => this.submitWindowFrame(LAUNCHER_SURFACE_ID, image, paintMs, frameId), setSurfaceVisible: (visible) => { this.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible); }, }), ); shell.registerWindow({ appId: "dashboard", windowId: "dashboard", title: "Dashboard", surfaceId: DASHBOARD_WINDOW_SURFACE_ID, closeable: false, drawIcon: makeLetterWindowIcon("D"), handleInput: async (event, frameId) => { await receiveDashboardWindowInput(event); await this.requestRender("interval", frameId); }, requestRender: () => { void this.requestRender("interval").catch((error) => { this.appendLog(`window render failed: ${this.formatError(error)}`); }); }, setForeground: (foreground) => { this.setWindowSurfaceVisible(DASHBOARD_WINDOW_SURFACE_ID, foreground); }, }); this.offAndroidNotification = onAndroidNotificationPosted((notificationKey) => { void this.handleAndroidNotificationPosted(notificationKey).catch((error) => { this.appendLog(`notification wake failed: ${this.formatError(error)}`); }); }); // Settings toggled from the glasses can change what the phone UI shows // (e.g. the raw-screenshot button), so re-emit the snapshot on any change. onAnySettingChanged(() => { this.emit(); // Apply a firmware-debug-flags toggle live while connected (Java dedups). this.pushFirmwareDebugFlags(); }); } private pushFirmwareDebugFlags(): void { if (!this.communicator) return; void this.communicator .setFirmwareDebugFlags(firmwareDebugFlagsSetting.get()) .catch(() => {}); } /** * Save the current dashboard as the raw headerless 4bpp frame buffer the * wire compressor sees; test data for compression experiments. */ saveRawDashboardScreenshot(): string { const path = saveRawScreenshot(drawDashboard()); this.appendLog(`raw screenshot saved: ${path}`); return path; } subscribe(listener: DashboardListener): () => void { this.listeners.add(listener); listener(this.snapshot()); return () => { this.listeners.delete(listener); }; } snapshot(): DashboardSnapshot { return { phase: this.phase, status: this.status, log: this.log, displayPreview: this.displayPreview, activeTextSettingId: this.activeTextSetting?.id ?? null, activeTextSettingTitle: this.activeTextSetting?.editorTitle ?? "", activeTextSettingValue: this.activeTextSetting?.get() ?? "", evenAppConflictMessage: this.evenAppConflictMessage, evenAppConflictWarningVisible: this.evenAppConflictMessage.length > 0, firmwareWarningMessage: this.firmwareWarningMessage, firmwareWarningVisible: this.firmwareWarningMessage.length > 0, rawScreenshotsEnabled: rawScreenshotsEnabledSetting.get(), batteryOptimizationWarningVisible: this.batteryOptimizationWarningVisible, }; } /** * Re-check the Doze exemption (cheap system call, but cached so snapshot() * stays trivial). Called on connect, page load, and after the user answers * the system exemption dialog. */ refreshBatteryOptimizationStatus(): void { const warningVisible = !isIgnoringBatteryOptimizations(); if (warningVisible !== this.batteryOptimizationWarningVisible) { this.batteryOptimizationWarningVisible = warningVisible; this.emit(); } } requestBatteryOptimizationExemption(): void { requestIgnoreBatteryOptimizations(); // The system dialog is asynchronous and there is no result callback from // this context; poll briefly so the banner clears once granted. let checksLeft = 12; const poll = setInterval(() => { this.refreshBatteryOptimizationStatus(); if (!this.batteryOptimizationWarningVisible || --checksLeft <= 0) { clearInterval(poll); } }, 5_000); } refreshEvenAppStatus(): void { this.refreshBatteryOptimizationStatus(); const state = readEvenAppNotificationState(); const wasActive = this.evenNotificationActive; this.evenNotificationActive = state.evenNotificationActive; if (state.evenNotificationActive && !wasActive) { this.appendLog("Even app notification is active."); } if (state.evenNotificationActive && !this.evenAppConflictMessage) { this.evenAppConflictMessage = EVEN_APP_DETECTED_MESSAGE; this.emit(); } if (!state.evenNotificationActive && this.evenAppConflictMessage) { this.evenAppConflictMessage = ""; this.emit(); } } openEvenAppSettings(): void { openEvenAppSettings(); } setSystemCardName(name: string): void { this.updateTextSetting(systemCardNameSetting, name); } setActiveTextSettingValue(value: string): void { shell.noteUserActivity(); const setting = this.activeTextSetting; if (!setting) return; if (setting.get() === value) return; setting.set(value); // Deliberately do NOT emit() here. The phone TextField is the source of // truth while typing; echoing activeTextSettingValue back into its two-way // binding on every keystroke drops fast/pasted characters (observed: a // 51-char API key stored as its first 46 chars). Just refresh the preview. this.previewOrRenderAfterTextSettingChange(setting.label); } async connect(): Promise { if (this.phase !== "disconnected") return; const addresses = loadDeviceAddresses(); if (!addresses.right || !addresses.left) { const message = "Configure both left and right arm MAC addresses before connecting."; this.setPhase("disconnected"); this.setStatus(`Failed: ${message}`); this.appendLog(`error: ${message}`); throw new Error(message); } this.log = ""; this.lastInput = "waiting..."; this.lastSys = "none yet"; this.firmwareWarningMessage = ""; this.refreshBatteryOptimizationStatus(); resetDashboardSleepTimerAndWake(); this.refreshEvenAppStatus(); this.setPhase("connecting"); this.setStatus("Connecting to the glasses..."); this.appendLog( `Using configured arms: R=${addresses.right} L=${addresses.left}${addresses.ring ? ` ring=${addresses.ring}` : ""}`, ); let communicator: FaceclawCommunicatorBridge | null = null; try { await ensureBlePermissions(); startForegroundNotification("Connecting to the glasses"); communicator = new FaceclawCommunicatorBridge({ right: addresses.right, left: addresses.left, ring: addresses.ring, }); this.communicator = communicator; this.offLog = communicator.onLog((line) => { this.appendLog(line); }); this.offState = communicator.onStateChange((state) => { const mappedPhase = state.phase === "connected" ? "connected" : state.phase === "charging" ? "charging" : state.phase === "disconnecting" ? "disconnecting" : state.phase === "disconnected" ? "disconnected" : "connecting"; if (mappedPhase === "charging" && this.phase !== "charging") { // Nobody is wearing the glasses; drop the G2-screen wakelock so the // phone can sleep normally while they charge. void this.communicator?.setG2ScreenOn(false).catch(() => {}); } if (mappedPhase === "connected" && this.phase !== "connected") { // Push the CFW firmware-debug-flags overlay preference; Java emits the // mode-7 control message once the dashboard container is warmed up. this.pushFirmwareDebugFlags(); } this.setPhase(mappedPhase); this.setStatus(state.status); }); this.offRing = communicator.onRingEvent((event) => { void this.handleInputEvent(event).catch((error) => { const message = this.formatError(error); this.appendLog(`input handler failed: ${message}`); }); }); this.offBattery = communicator.onBatteryState((state) => { setDashboardBatteryLevels({ headset: state.battery, headsetCharging: state.chargingStatus > 0, }); shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.chargingStatus > 0, }); if ((this.phase === "connected" || this.phase === "charging") && this.communicator) { this.requestShellRender(); void this.requestRender("interval").catch((error) => { const message = this.formatError(error); this.appendLog(`battery update failed: ${message}`); }); } }); this.offEvenAppConflict = communicator.onEvenAppConflict((message) => { this.refreshEvenAppStatus(); if (!this.evenNotificationActive) { this.appendLog(`Even app conflict suspected, but notification was not active: ${message}`); return; } this.evenAppConflictMessage = message; this.appendLog(message); this.emit(); }); this.offFrameMetrics = communicator.onFrameMetrics((metrics) => { if (this.phase === "connected") { this.setStatus(`Connected. Last frame: ${this.formatFrameMetrics(metrics)}.`); } }); this.offFirmwareInfo = communicator.onFirmwareInfo((info) => { this.appendLog( `firmware: L=${info.leftVersion || "?"} R=${info.rightVersion || "?"}` + (info.capabilities ? ` caps="${info.capabilities}"` : " (no CFW capability string)"), ); const warning = firmwareIncompatibilityMessage(info) ?? ""; if (warning !== this.firmwareWarningMessage) { this.firmwareWarningMessage = warning; if (warning) { this.appendLog(`firmware compatibility warning: ${warning}`); } this.emit(); } }); this.offMedia = mediaControllerBridge.onStateChange(() => { if (this.phase === "connected" && this.communicator) { void this.requestRender("interval").catch((error) => { const message = this.formatError(error); this.appendLog(`media update failed: ${message}`); }); } }); this.offNightscout = nightscoutBridge.onStateChange(() => { if (this.phase === "connected" && this.communicator) { void this.requestRender("interval").catch((error) => { const message = this.formatError(error); this.appendLog(`nightscout update failed: ${message}`); }); } }); this.offVoiceStatus = voiceControlBridge.onStatus((state) => { this.appendLog(state.status); }); this.offVoiceWakeWord = voiceControlBridge.onWakeWord((keyword) => { void this.handleWakeWord(keyword).catch((error) => { this.appendLog(`wake-word handler failed: ${this.formatError(error)}`); }); }); await mediaControllerBridge.start(); await nightscoutBridge.start(); // Register the compositor surfaces: the dashboard window in the app // viewport, and the shell chrome above it. await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT); await communicator.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: 1, transparency: "color-key", }); // Window surfaces for every live window (including any launched while // disconnected); only the foreground window's surface is composited. const foregroundWindowId = shell.foregroundWindow()?.windowId; for (const window of shell.getWindows()) { await this.configureWindowSurface(window.surfaceId, window.windowId === foregroundWindowId); } await communicator.start(); shell.foregroundWindow()?.requestRender(); this.requestShellRender(); await this.requestRender("initial"); this.dashboardTimer = setInterval(() => { // Also refreshes the top-bar clock on the shell surface. this.requestShellRender(); void this.requestRender("interval").catch((error) => { const message = this.formatError(error); this.setStatus(`Dashboard update failed: ${message}`); this.appendLog(`dashboard update failed: ${message}`); }); }, DASHBOARD_INTERVAL_MS); this.screenTimeoutTimer = setInterval(() => { if (this.phase !== "connected" || !this.communicator) return; if (!shell.applyScreenTimeout()) return; this.endTextSettingEdit(); this.requestShellRender(); void this.requestRender("interval").catch((error) => { const message = this.formatError(error); this.appendLog(`screen timeout render failed: ${message}`); }); }, SCREEN_TIMEOUT_CHECK_MS); } catch (error) { const message = this.formatError(error); this.offState?.(); this.offState = null; this.offLog?.(); this.offLog = null; this.offRing?.(); this.offRing = null; this.offBattery?.(); this.offBattery = null; this.offEvenAppConflict?.(); this.offEvenAppConflict = null; this.offFrameMetrics?.(); this.offFrameMetrics = null; this.offFirmwareInfo?.(); this.offFirmwareInfo = null; this.offMedia?.(); this.offMedia = null; this.offNightscout?.(); this.offNightscout = null; this.offVoiceStatus?.(); this.offVoiceStatus = null; this.offVoiceWakeWord?.(); this.offVoiceWakeWord = null; if (this.transcribeRenderTimer) { clearInterval(this.transcribeRenderTimer); this.transcribeRenderTimer = null; } await mediaControllerBridge.stop().catch(() => {}); await nightscoutBridge.stop().catch(() => {}); voiceControlBridge.stop(); if (communicator) { await communicator.close().catch(() => {}); } this.communicator = null; this.clearDashboardTimer(); stopForegroundNotification(); this.setPhase("disconnected"); this.setStatus(`Failed: ${message}`); this.appendLog(`error: ${message}`); throw error; } } async disconnect(): Promise { if (this.phase === "disconnected" || this.phase === "disconnecting") return; this.setPhase("disconnecting"); this.setStatus("Disconnecting..."); this.clearDashboardTimer(); this.offState?.(); this.offState = null; this.offLog?.(); this.offLog = null; this.offRing?.(); this.offRing = null; this.offBattery?.(); this.offBattery = null; this.offEvenAppConflict?.(); this.offEvenAppConflict = null; this.offFrameMetrics?.(); this.offFrameMetrics = null; this.offFirmwareInfo?.(); this.offFirmwareInfo = null; this.offMedia?.(); this.offMedia = null; this.offNightscout?.(); this.offNightscout = null; this.offVoiceStatus?.(); this.offVoiceStatus = null; this.offVoiceWakeWord?.(); this.offVoiceWakeWord = null; const communicator = this.communicator; this.communicator = null; try { const shutdownAcked = await communicator?.sendShutdown(0).catch((error) => { this.appendLog(`shutdown command failed: ${this.formatError(error)}`); return false; }); if (shutdownAcked === true) { this.appendLog("Shutdown command completed."); } else if (communicator) { this.appendLog("Shutdown command did not complete before disconnect."); } await mediaControllerBridge.stop().catch(() => {}); await nightscoutBridge.stop().catch(() => {}); voiceControlBridge.stop(); if (this.transcribeRenderTimer) { clearInterval(this.transcribeRenderTimer); this.transcribeRenderTimer = null; } await communicator?.close().catch(() => {}); } finally { stopForegroundNotification(); this.setPhase("disconnected"); this.setStatus("Disconnected."); this.appendLog("Disconnected from the glasses."); } } async injectSyntheticRingInput(kind: "click" | "double-click" | "scroll-up" | "scroll-down"): Promise { const event = this.buildSyntheticRingInput(kind); await this.handleInputEvent(event); } async openTelepromptDocument(text: string): Promise { openTelepromptDocument(text); this.appendLog(`teleprompt document received (${text.length} chars)`); if (this.phase === "connected" && this.communicator) { this.requestShellRender(); await this.requestRender("interval"); return; } const image = drawDashboard(); this.updateDisplayPreviewFromImage(image); } private startTextSettingEdit(setting: ConfigSettingString): void { this.activeTextSetting = setting; this.emit(); } /** * Begin voice capture with the provider chosen in settings. Used by both * push-to-talk and the Transcribe app. Android mic permission is the consent * gate even though the audio source is the G2 mic over BLE. */ private startVoiceCapture(): void { if (this.phase !== "connected" || !this.communicator) { return; } const communicator = this.communicator; void ensureVoicePermissions() .then(() => { if (this.phase !== "connected" || this.communicator !== communicator) return; voiceControlBridge.startPushToTalk({ communicator: communicator.getNativeCommunicator(), provider: voiceProviderSetting.get(), elevenLabsApiKey: elevenLabsApiKeySetting.get(), saveRecording: saveVoiceRecordingsSetting.get(), }); }) .catch((error) => { this.appendLog(`voice permission failed: ${this.formatError(error)}`); }); } private stopVoiceCapture(): void { voiceControlBridge.stopPushToTalk(); } private endTextSettingEdit(): void { const finishedSetting = this.activeTextSetting; this.activeTextSetting = null; this.emit(); if (finishedSetting === nightscoutSiteUrlSetting || finishedSetting === nightscoutApiTokenSetting) { void this.refreshNightscoutAfterSettingsChange(); } } /** * Finish the active edit from the phone side (e.g. the IME's done key): * ends the edit session and navigates the glasses out of the edit page. */ finishActiveTextSettingEdit(): void { if (!this.activeTextSetting) return; this.endTextSettingEdit(); if (!closeDashboardTextSettingEditor()) return; if (this.phase === "connected" && this.communicator) { void this.requestRender("interval").catch((error) => { this.appendLog(`edit close render failed: ${this.formatError(error)}`); }); } else { this.updateDisplayPreviewFromImage(drawDashboard()); } } private updateTextSetting(setting: ConfigSettingString, value: string): void { if (setting.get() !== value) { setting.set(value); this.emit(); this.previewOrRenderAfterTextSettingChange(setting.label); } } private async refreshNightscoutAfterSettingsChange(): Promise { await nightscoutBridge.refreshNow().catch((error) => { this.appendLog(`nightscout settings refresh failed: ${this.formatError(error)}`); }); if (this.phase === "connected" && this.communicator) { await this.requestRender("interval").catch((error) => { this.appendLog(`nightscout settings render failed: ${this.formatError(error)}`); }); } else { const image = drawDashboard(); this.updateDisplayPreviewFromImage(image); } } private previewOrRenderAfterTextSettingChange(label: string): void { if (this.phase === "connected" && this.communicator) { void this.requestRender("interval").catch((error) => { this.appendLog(`${label} update failed: ${this.formatError(error)}`); }); return; } const image = drawDashboard(); this.updateDisplayPreviewFromImage(image); } /** * Serialize renders; frameId identifies the input event or timer tick that * asked for this render (one is started here for callers that don't have * one). Ownership of the frame passes to renderDashboard and then to the * Java side; requests that coalesce into an already-queued render finish * immediately as discarded. When coalescing we keep the oldest waiting frame * so measured latency reflects the worst-served request. */ private async requestRender(reason: "initial" | "interval", frameId?: number): Promise { const newFrameId = frameId ?? frameTimings.startFrame(`render:${reason}`); this.queuedRenderReason = this.queuedRenderReason === "initial" ? "initial" : reason; if (this.renderInProgress) { if (this.renderQueued && this.queuedFrameId > 0) { frameTimings.finishFrame(newFrameId, `discarded: coalesced into frame#${this.queuedFrameId}`); } else { this.renderQueued = true; this.queuedFrameId = newFrameId; frameTimings.logFrame(newFrameId, "render queued behind in-progress render"); } return; } this.renderInProgress = true; let frameToRender = newFrameId; try { while (true) { const nextReason = this.queuedRenderReason; this.renderQueued = false; this.queuedRenderReason = "interval"; try { await this.renderDashboard(nextReason, frameToRender); } catch (error) { frameTimings.finishFrame(frameToRender, `discarded: render failed: ${this.formatError(error)}`); throw error; } if (!this.renderQueued || this.queuedFrameId <= 0) break; frameToRender = this.queuedFrameId; this.queuedFrameId = 0; } } finally { this.renderInProgress = false; } } private async renderDashboard(reason: "initial" | "interval", frameId: number): Promise { console.log("renderDashboard", reason); frameTimings.logFrame(frameId, `renderDashboard start (${reason})`); const wantFreshData = this.nextRenderWantsFreshData; this.nextRenderWantsFreshData = false; beginRenderPass(!wantFreshData); const paintStartedAtMs = Date.now(); const image = frameTimings.span(frameId, "paint", () => frameTimings.runWithFrame(frameId, () => drawDashboard()), ); const paintMs = Date.now() - paintStartedAtMs; const paintUsedStaleData = endRenderPass(); if (paintUsedStaleData) { frameTimings.logFrame(frameId, "painted with stale data; will schedule a fresh-data repaint"); } // The dashboard still paints the full lens; its window surface only shows // the viewport-sized top-left crop (content near the right/bottom edges // clips until the legacy layouts are resized). const cropped = frameTimings.span(frameId, "crop", () => cropToViewport(image)); const fingerprint = frameTimings.span(frameId, "fingerprint", () => cropped.fingerprint()); this.lastWindowImage = cropped; const updatePreviewAfterTransmit = this.phase === "connected"; if (!updatePreviewAfterTransmit) { this.updateDisplayPreviewFromImage(image); } if (this.communicator && this.phase === "charging") { // Glasses are in the charging case; keep the phone preview fresh but // send nothing. frameTimings.finishFrame(frameId, "discarded: glasses charging"); this.updateConnectedForegroundNotification(); console.log("renderDashbaord finished (charging)"); return; } if (this.communicator) { if (shell.isScreenOn()) { await this.communicator.setG2ScreenOn(true); } console.log("submitSurfaceFrame"); const buffer = frameTimings.span(frameId, "to8bpp", () => cropped.to8bppBuffer()); // The Java side owns the frame from here: it finishes it on last-packet // ack, dedup, supersede, or timeout. await this.communicator.submitSurfaceFrame( DASHBOARD_WINDOW_SURFACE_ID, buffer, { x: 0, y: 0, width: cropped.width, height: cropped.height }, fingerprint, paintMs, frameId, ); const outcome = await this.communicator.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS); if (outcome === null) { frameTimings.logFrame(frameId, "transmit backpressure wait timed out; render loop continuing"); } if (!shell.isScreenOn()) { await this.communicator.setG2ScreenOn(false); } if (updatePreviewAfterTransmit) { this.updateConnectedCompositePreview(); } } else { frameTimings.finishFrame(frameId, "discarded: no communicator, preview only"); } if (paintUsedStaleData) { this.nextRenderWantsFreshData = true; void this.requestRender("interval").catch((error) => { this.appendLog(`fresh-data repaint failed: ${this.formatError(error)}`); }); } this.updateConnectedForegroundNotification(); if (reason === "initial") { this.appendLog("initial dashboard image queued"); } console.log("renderDashboard finished"); } private async handleInputEvent(event: RawInputEvent): Promise { const frameId = event.frameId > 0 ? event.frameId : frameTimings.startFrame(`input:${event.kind} (untracked source)`); frameTimings.logFrame(frameId, `TS input handler start: ${event.kind} ${eventName(event.eventType)}`); let frameOwned = false; try { const inputEvent = rawInputEventToInputEvent(event); logToDashboard(inputEventToString(inputEvent)); frameTimings.spanStart(frameId, "handle-input"); let outcome: ShellInputOutcome; try { // The shell consumes its reserved gestures and forwards the rest to // the focused window; the outcome says which surfaces changed. outcome = await shell.receiveInput(inputEvent, frameId); } finally { frameTimings.spanEnd(frameId, "handle-input"); } if (event.kind === "sys-event") { this.lastSys = `${sourceName(event.eventSource)}/${eventName(event.eventType)}`; this.appendLog(`sys-event ${this.lastSys}`); if ( event.eventType === OsEventTypeList.FOREGROUND_EXIT_EVENT || event.eventType === OsEventTypeList.ABNORMAL_EXIT_EVENT || event.eventType === OsEventTypeList.SYSTEM_EXIT_EVENT ) { this.appendLog("display state invalidated by firmware exit event"); } if (event.eventSource === EventSourceType.TOUCH_EVENT_FROM_RING) { this.lastInput = eventName(event.eventType); } } else if (event.kind === "text-click" && isDashboardContainerName(event.containerName)) { if ( event.eventType === OsEventTypeList.SCROLL_TOP_EVENT || event.eventType === OsEventTypeList.SCROLL_BOTTOM_EVENT ) { this.lastSys = `TEXT/${eventName(event.eventType)}`; this.lastInput = `TEXT_${eventName(event.eventType)}`; this.appendLog(`text-event ${this.lastSys}`); } } if (outcome.shell) { this.requestShellRender(); } if (outcome.window) { // The window adapter owned frameId (render or explicit finish). frameOwned = true; } else if (outcome.shell) { frameOwned = true; frameTimings.finishFrame(frameId, "input consumed by shell; chrome render scheduled"); } } finally { if (!frameOwned) { frameTimings.finishFrame(frameId, "discarded: input did not trigger a render"); } } } /** * Launch an app from the launcher: allocate a window (in the app's shared * worker, spawned on first launch), configure its surface, and foreground * it. Launching an already-running app opens another window of it. */ private async launchApp(appId: string): Promise { if (appId !== "stopwatch") { this.appendLog(`unknown app: ${appId}`); return; } if (!this.stopwatchHost) { this.stopwatchHost = new WorkerAppHost("stopwatch", new Worker("../workers/stopwatch-app.worker")); } const serial = this.nextWindowSerial++; const windowId = `stopwatch:${serial}`; const surfaceId = `window:${windowId}`; const window = this.stopwatchHost.openWindow({ windowId, surfaceId, title: `Stopwatch ${serial}`, iconLetter: "S", viewport: { width: APP_VIEWPORT.width, height: APP_VIEWPORT.height }, setSurfaceVisible: (visible) => this.setWindowSurfaceVisible(surfaceId, visible), removeSurface: () => this.removeWindowSurface(surfaceId), }); shell.registerWindow(window); // Configure the surface before foregrounding so the worker's first frame // has somewhere to land. await this.configureWindowSurface(surfaceId, false); shell.focusWindow(windowId); this.requestShellRender(); this.appendLog(`launched ${windowId}`); } /** Create/refresh a window surface on the compositor, if connected. */ private async configureWindowSurface(surfaceId: string, visible: boolean): Promise { const communicator = this.communicator; if (!communicator) return; await communicator.configureSurface(surfaceId, { x: APP_VIEWPORT.x, y: APP_VIEWPORT.y, width: APP_VIEWPORT.width, height: APP_VIEWPORT.height, zOrder: 0, transparency: "opaque", }); await communicator.setSurfaceVisible(surfaceId, visible); } private removeWindowSurface(surfaceId: string): void { const communicator = this.communicator; if (!communicator) return; void communicator.removeSurface(surfaceId).catch((error) => { this.appendLog(`surface removal failed: ${this.formatError(error)}`); }); } /** Submit a painted frame for an in-process window (e.g. the launcher). */ private async submitWindowFrame(surfaceId: string, image: GrayImage, paintMs: number, frameId: number): Promise { const communicator = this.communicator; if (!communicator || this.phase === "charging") { frameTimings.finishFrame(frameId, "discarded: window frame with no active connection"); return; } const fingerprint = image.fingerprint(); const buffer = image.to8bppBuffer(); await communicator.submitSurfaceFrame( surfaceId, buffer, { x: 0, y: 0, width: image.width, height: image.height }, fingerprint, paintMs, frameId, ); } /** Flip a window surface's compositor visibility; fire-and-forget. */ private setWindowSurfaceVisible(surfaceId: string, visible: boolean): void { const communicator = this.communicator; if (!communicator) return; void communicator.setSurfaceVisible(surfaceId, visible).catch((error) => { this.appendLog(`surface visibility change failed: ${this.formatError(error)}`); }); } /** * Re-render and resubmit the shell surface (sidebar, top bar, shell * overlays). Coalesces like requestRender: one render in flight, at most * one queued. */ requestShellRender(): void { if (this.shellRenderInProgress) { this.shellRenderQueued = true; return; } this.shellRenderInProgress = true; void (async () => { try { do { this.shellRenderQueued = false; await this.renderShell(); } while (this.shellRenderQueued); } catch (error) { this.appendLog(`shell render failed: ${this.formatError(error)}`); } finally { this.shellRenderInProgress = false; } })(); } private async renderShell(): Promise { const frameId = frameTimings.startFrame("render:shell"); beginRenderPass(true); const paintStartedAtMs = Date.now(); const image = frameTimings.span(frameId, "paint", () => frameTimings.runWithFrame(frameId, () => shell.paintSurface()), ); const paintMs = Date.now() - paintStartedAtMs; endRenderPass(); this.lastShellImage = image; if (!this.communicator || this.phase === "charging") { frameTimings.finishFrame(frameId, "discarded: shell render with no active connection"); return; } const fingerprint = frameTimings.span(frameId, "fingerprint", () => image.fingerprint()); const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer()); await this.communicator.submitSurfaceFrame( SHELL_SURFACE_ID, buffer, { x: 0, y: 0, width: image.width, height: image.height }, fingerprint, paintMs, frameId, ); await this.communicator.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS); this.updateConnectedCompositePreview(); } private async handleWakeWord(keyword: string): Promise { const normalized = keyword.trim(); if (normalized.length === 0) return; this.appendLog(`wake-word detected: ${normalized}`); const changed = resetDashboardSleepTimerAndWake(); if (this.phase === "connected" && this.communicator) { if (changed) { this.requestShellRender(); await this.requestRender("initial"); } return; } if (changed) { const image = drawDashboard(); this.updateDisplayPreviewFromImage(image); } } private async handleAndroidNotificationPosted(notificationKey: string): Promise { const changed = openAndroidNotificationFromSleep(notificationKey); if (!changed) { if (this.phase === "connected" && this.communicator) { await this.requestRender("interval"); } return; } this.appendLog("android notification woke dashboard"); if (this.phase === "connected" && this.communicator) { this.requestShellRender(); await this.requestRender("initial"); return; } const image = drawDashboard(); this.updateDisplayPreviewFromImage(image); } private async playBuzzerNote(note: number, oct: number, beat: number): Promise { if (this.phase !== "connected" || !this.communicator) { return; } await this.communicator.playBuzzerNote(note, oct, beat); } private setTranscribeRenderActive(active: boolean): void { if (!active || this.phase !== "connected" || !this.communicator) { if (this.transcribeRenderTimer) { clearInterval(this.transcribeRenderTimer); this.transcribeRenderTimer = null; } return; } if (this.transcribeRenderTimer) return; this.transcribeRenderTimer = setInterval(() => { void this.requestRender("interval").catch((error) => { this.appendLog(`transcribe render failed: ${this.formatError(error)}`); }); }, TRANSCRIBE_RENDER_INTERVAL_MS); } private clearDashboardTimer(): void { if (this.dashboardTimer) { clearInterval(this.dashboardTimer); this.dashboardTimer = null; } if (this.screenTimeoutTimer) { clearInterval(this.screenTimeoutTimer); this.screenTimeoutTimer = null; } if (this.transcribeRenderTimer) { clearInterval(this.transcribeRenderTimer); this.transcribeRenderTimer = null; } } private setPhase(phase: ConnectionPhase): void { if (this.phase === phase) return; this.phase = phase; this.emit(); } private setStatus(status: string): void { if (this.status === status) return; this.status = status; this.emit(); } private updateConnectedForegroundNotification(): void { if (this.phase !== "connected") return; const now = Date.now(); if (now - this.lastForegroundNotificationUpdateAtMs < FOREGROUND_NOTIFICATION_MIN_UPDATE_MS) return; this.lastForegroundNotificationUpdateAtMs = now; updateForegroundNotification("Connected"); } private formatFrameMetrics(metrics: FrameMetrics): string { return `paint=${Math.round(metrics.paintMs)}ms, transmit=${Math.round(metrics.transmitMs)}ms, tiles=${Math.round(metrics.tileCount)}`; } private appendLog(line: string): void { const stamped = `[${formatTimestamp(new Date())}] ${line}`; //this.log = this.log ? `${this.log}\n${stamped}` : stamped; console.log(stamped); //this.emit(); } private setDisplayPreview(preview: ImageSource | null): void { if (this.displayPreview === preview) return; this.displayPreview = preview; this.emit(); } private updateDisplayPreviewFromImage(image: GrayImage): void { this.setDisplayPreview(grayImageToPreviewSource(image)); } /** * Phone-side preview of what is actually on the glasses: the window surface * in its viewport with the shell chrome composited over it, mirroring the * Java compositor. */ private buildCompositePreviewImage(): GrayImage { const preview = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); if (this.lastWindowImage) { preview.bitBlt(this.lastWindowImage, APP_VIEWPORT.x, APP_VIEWPORT.y); } if (this.lastShellImage) { preview.bitBlt(this.lastShellImage, 0, 0, { transparentZero: true }); } return preview; } private updateConnectedCompositePreview(): void { if (!this.communicator) return; const now = Date.now(); if ( this.lastConnectedPreviewUpdateAtMs > 0 && now - this.lastConnectedPreviewUpdateAtMs < CONNECTED_PREVIEW_MIN_UPDATE_MS ) { return; } this.lastConnectedPreviewUpdateAtMs = now; this.updateDisplayPreviewFromImage(this.buildCompositePreviewImage()); } private formatError(error: unknown): string { const raw = (error as Error)?.message ?? String(error); const sanitized = raw.replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim(); if (sanitized.length <= 240) return sanitized; return `${sanitized.slice(0, 237)}...`; } private buildSyntheticRingInput(kind: "click" | "double-click" | "scroll-up" | "scroll-down"): RawInputEvent { const frameId = frameTimings.startFrame(`input:synthetic:${kind}`); switch (kind) { case "click": return { kind: "sys-event", containerName: CONTAINER_NAME, eventType: OsEventTypeList.CLICK_EVENT, eventSource: EventSourceType.TOUCH_EVENT_FROM_RING, systemExitReasonCode: 0, frameId, }; case "double-click": return { kind: "sys-event", containerName: CONTAINER_NAME, eventType: OsEventTypeList.DOUBLE_CLICK_EVENT, eventSource: EventSourceType.TOUCH_EVENT_FROM_RING, systemExitReasonCode: 0, frameId, }; case "scroll-up": return { kind: "text-click", containerName: CONTAINER_NAME, eventType: OsEventTypeList.SCROLL_TOP_EVENT, eventSource: EventSourceType.TOUCH_EVENT_FROM_RING, systemExitReasonCode: 0, frameId, }; case "scroll-down": default: return { kind: "text-click", containerName: CONTAINER_NAME, eventType: OsEventTypeList.SCROLL_BOTTOM_EVENT, eventSource: EventSourceType.TOUCH_EVENT_FROM_RING, systemExitReasonCode: 0, frameId, }; } } private emit(): void { const snapshot = this.snapshot(); for (const listener of this.listeners) { listener(snapshot); } } } export const dashboardController = new DashboardController();