Port lock screen to iOS

This commit is contained in:
James Babcock committed 2026-09-19 02:55:13 -07:00
1 parent ef7878359f
commit e2b35ada1a
12 files changed
+273 -46

No files matched your search

+1 -22
View File
@@ -51,8 +51,7 @@ import { voiceControlBridge } from "../native/voice-control";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
import { prepareFrameDraws } from "../graphics/glyph-wire";
import { getDefaultMediumFont } from "../graphics/ui-fonts";
import { wrapText } from "../graphics/textwrap";
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from "./lock-screen";
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
import { type InputEvent } from "../ui/gestures";
import { registerSystemTools } from "../assistant/system-tools";
@@ -141,8 +140,6 @@ type DashboardListener = (snapshot: DashboardSnapshot) => void;
const SHELL_SURFACE_ID = "shell";
/** The shell chrome composites above every window surface (zOrder 0). */
const SHELL_SURFACE_Z_ORDER = 1;
const LOCK_SCREEN_SURFACE_ID = "lock-screen";
const LOCK_SCREEN_MESSAGE = "Glasses locked; unlock the phone to unlock the glasses.";
// Top-bar clock refresh; the phone-side preview polls the Java composite so
// it reflects every app (including worker apps the TS side never renders).
const SHELL_REFRESH_INTERVAL_MS = 60_000;
@@ -172,24 +169,6 @@ function createInitialDisplayPreview(): ImageSource | null {
return grayImageToPreviewSource(new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0));
}
function createLockScreenImage(): GrayImage {
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
const font = getDefaultMediumFont();
const boxWidth = 480;
const boxHeight = 150;
const boxX = Math.round((G2_LENS_WIDTH - boxWidth) / 2);
const boxY = Math.round((G2_LENS_HEIGHT - boxHeight) / 2);
image.drawRoundedRect(boxX, boxY, boxWidth, boxHeight, 150, 12);
const lines = wrapText(font, LOCK_SCREEN_MESSAGE, boxWidth - 64);
const textHeight = lines.length * font.lineHeight;
const firstY = boxY + Math.round((boxHeight - textHeight) / 2);
lines.forEach((line, index) => {
const x = Math.round((G2_LENS_WIDTH - font.measureText(line)) / 2);
image.drawText(font, x, firstY + index * font.lineHeight, line, 230);
});
return image;
}
function formatTimestamp(date: Date): string {
return date.toISOString().slice(11, 23);
}
+16 -1
View File
@@ -6,6 +6,7 @@ import { CfwTransport, CFW_MAX_MESSAGE, parseCfwAcks } from './cfw-transport'
import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLAW_FIRMWARE_VERSION } from './firmware-compat'
import { hexToBytes } from '../util/hex-util'
import { deviceAddressError } from './ios-peripheral-identity'
import { decodeWearState, enableWearDetection, queryWearState } from './wear-protocol'
declare function setTimeout(callback: () => void, ms: number): number
declare function clearTimeout(id: number): void
@@ -84,7 +85,8 @@ export class GlassesSession {
private readonly onInput: (input: protocol.GlassesInput) => void,
private readonly log: (message: string) => void = () => {},
private readonly onActivity: () => void = () => {},
private readonly onCompass: (event: CompassEvent) => void = () => {}) {
private readonly onCompass: (event: CompassEvent) => void = () => {},
private readonly onWearState: (wearing: boolean) => void = () => {}) {
this.off = transport.onEvent(event => this.receive(event))
}
private update(phase: SessionState['phase'], status: string): void {
@@ -134,6 +136,7 @@ export class GlassesSession {
await this.lease(true)
await this.request('right', protocol.SID.hub, protocol.createLayout, 'Create display layout')
this.check(generation); this.layoutCreated = true
await this.enableWearDetectionAndRequestState(); this.check(generation)
this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
this.displayed = null; this.retryCount = 0
this.update('connected', 'Glasses connected')
@@ -353,6 +356,8 @@ export class GlassesSession {
}
}
if (message.sid === protocol.SID.settings) this.applySettings(message)
const wearing = decodeWearState(message)
if (wearing !== null) this.onWearState(wearing)
if (event.identifier === this.ids.right) {
if (this.state.phase === 'connected') {
const compass = protocol.decodeCompassInput(message)
@@ -389,6 +394,16 @@ export class GlassesSession {
}
this.onState({ ...this.state })
}
async enableWearDetectionAndRequestState(): Promise<void> {
const generation = this.generation
// Like Android, a missing enable ACK must not suppress the current-state query.
try { await this.request('right', protocol.SID.settings, enableWearDetection, 'Enable wear detection') }
catch (error) { if (!(error instanceof AckTimeout)) throw error; this.log(this.message(error)) }
this.check(generation)
await this.send('right', protocol.SID.settings, 0x20, queryWearState())
this.check(generation)
await this.send('left', protocol.SID.settings, 0x20, queryWearState())
}
setFrame(gray: Uint8Array): void {
if (this.state.phase !== 'connected') return
this.latest = protocol.packGray4(gray, 640, 480); this.schedule(0)
+88 -9
View File
@@ -16,6 +16,7 @@ import { iosVoiceInput } from '../native/ios-voice-input'
import { nightscoutBridge } from '../native/nightscout-bridge'
import { GlassesSession, type SessionState } from './glasses-session'
import { GlanceHost, type GlanceDisplay } from './glance-host'
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
import { OsEventTypeList } from './events'
import { loadDeviceAddresses } from './device-addresses'
import { deviceAddressError } from './ios-peripheral-identity'
@@ -37,7 +38,7 @@ import { TextViewerLayer } from '../apps/files/text-viewer'
import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell'
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible } from '../ui/shell/geometry'
import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySettingChanged,
previewColorSetting } from '../ui/dashboard-settings'
previewColorSetting, lockScreenEnabledSetting } from '../ui/dashboard-settings'
import type { PhoneGesture } from '../phone-ui/phone-gestures'
/** iOS host for the shared app registry, shell, compositor and BLE session. */
@@ -60,7 +61,7 @@ export class IosPreviewController {
private readonly glance = new GlanceHost({
getDisplay: () => this.glanceDisplay,
getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null,
canShow: () => this.runtimeNeeded,
canShow: () => this.runtimeNeeded && !this.glassesLocked,
// iOS retains the EvenHub session while the shell sleeps. The board's
// opaque first frame is ready before we unblank the compositor.
ensureSessionActive: async () => {
@@ -86,6 +87,11 @@ export class IosPreviewController {
private readonly appHosts = new Map<string, WorkerAppHost>()
private readonly inProcessApps = new Map<string, InProcessWindow>()
private readonly batteryObservers: any[] = []
private readonly lockObservers: any[] = []
private phoneLocked = false
private glassesWorn: boolean | null = null
private glassesLocked = false
private lockEnabled = lockScreenEnabledSetting.get()
private session: GlassesSession | null = null
private logLines: string[] = []
private logTimer: ReturnType<typeof setTimeout> | null = null
@@ -112,6 +118,10 @@ export class IosPreviewController {
private readonly onError: (message: string) => void,
private readonly onConnectionState: (state: SessionState) => void = () => {}) {
this.compositor.configureSurface('shell', { x: 0, y: 0, width: 640, height: 480, zOrder: 1, transparency: 'color-key' })
this.compositor.configureSurface(LOCK_SCREEN_SURFACE_ID, {
x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: 1000, transparency: 'opaque',
})
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, false)
shell.configure({
actions: this.actions,
voiceInputEnabled: true,
@@ -148,6 +158,7 @@ export class IosPreviewController {
if (this.active) return
this.active = true
this.syncRuntime()
this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable)
this.logBluetooth('Phone foreground')
if (this.session) this.onConnectionState({ ...this.session.state })
this.session?.wake()
@@ -178,6 +189,7 @@ export class IosPreviewController {
this.compositor.setScreenBlanked(!shell.isScreenOn())
this.offSettings?.(); this.offSettings = null
for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
UIDevice.currentDevice.batteryMonitoringEnabled = false
if (this.clockTimer !== null) clearInterval(this.clockTimer)
if (this.renderTimer !== null) clearTimeout(this.renderTimer)
@@ -185,6 +197,16 @@ export class IosPreviewController {
return
}
UIDevice.currentDevice.batteryMonitoringEnabled = true
// Keep these observers alive while BLE owns the runtime, including background.
for (const [name, locked] of [
[UIApplicationProtectedDataWillBecomeUnavailable, true],
[UIApplicationProtectedDataDidBecomeAvailable, false],
] as const) {
this.lockObservers.push(NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(
name, null, NSOperationQueue.mainQueue, () => this.handlePhoneLockState(locked)))
}
this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable)
this.syncLockSetting()
void nightscoutBridge.start().catch(error => this.fail(error))
for (const name of [UIDeviceBatteryLevelDidChangeNotification, UIDeviceBatteryStateDidChangeNotification]) {
this.batteryObservers.push(NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(name, null, NSOperationQueue.mainQueue, () => this.requestShellRender()))
@@ -192,12 +214,53 @@ export class IosPreviewController {
const phone = readPhoneBatteryState()
this.logBluetooth(`Phone battery ${phone.battery ?? "unknown"}% charging=${phone.charging}`)
this.offSettings = onAnySettingChanged(() => {
this.syncLockSetting()
this.relayout()
shell.foregroundWindow()?.requestRender()
this.requestShellRender()
})
this.clockTimer = setInterval(() => this.refreshClock(), 60_000)
}
private syncLockSetting(): void {
const enabled = lockScreenEnabledSetting.get()
if (enabled === this.lockEnabled) return
this.lockEnabled = enabled
if (!enabled) this.setGlassesLocked(false)
else {
if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true)
if (this.session?.state.phase === 'connected')
void this.session.enableWearDetectionAndRequestState().catch(error => this.fail(error))
}
}
private handlePhoneLockState(locked: boolean): void {
this.phoneLocked = locked
if (!locked) this.setGlassesLocked(false)
else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true)
}
private handleWearState(wearing: boolean): void {
// Sample false to catch a notification missed during suspension. Do not
// sample true here: WillBecomeUnavailable precedes the property transition.
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
this.glassesWorn = wearing
this.logBluetooth(`Glasses wear state: ${wearing ? 'ON_HEAD' : 'OFF_HEAD'}`)
if (!wearing && this.phoneLocked && this.lockEnabled) this.setGlassesLocked(true)
}
private setGlassesLocked(locked: boolean): void {
if (locked === this.glassesLocked) return
if (locked) {
const image = createLockScreenImage()
this.compositor.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(),
{ x: 0, y: 0, width: image.width, height: image.height })
}
this.glassesLocked = locked
if (locked) {
this.glance.dismiss()
iosVoiceInput.handleSessionEnded('Glasses locked. Unlock your phone to start voice input again.')
}
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked)
this.logBluetooth(`Glasses ${locked ? 'locked' : 'unlocked'}`)
this.requestShellRender()
}
private refreshClock(): void {
const minute = Math.floor(Date.now() / 60_000)
if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() }
@@ -240,7 +303,7 @@ export class IosPreviewController {
this.session?.setFrame(pixels)
if (this.active) {
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
this.onFrame(image, this.glance.isVisible() ? 'Glanceboard'
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
}
} catch (error) { this.fail(error) }
@@ -255,7 +318,7 @@ export class IosPreviewController {
gesture(gesture: PhoneGesture, origin: 'watch' | 'ring' | 'mirror', nx = 0, ny = 0): void {
this.inputQueue = this.inputQueue.then(async () => {
if (!this.active) return
if (gesture === 'tap' && origin === 'mirror' && shell.isScreenOn()) {
if (gesture === 'tap' && origin === 'mirror' && shell.isScreenOn() && !this.glassesLocked) {
await this.mirrorTap(nx, ny)
} else {
let type: string = ({ tap: 'click', 'double-tap': 'double-click' } as Record<string, string>)[gesture] ?? gesture
@@ -270,6 +333,16 @@ export class IosPreviewController {
}).catch(error => this.fail(error))
}
private async receiveInput(event: InputEvent, headTilt = false): Promise<void> {
if (this.glassesLocked) {
// Preserve the locked display's sleep/wake controls without dispatching
// gestures to apps, shell menus, voice input or the Glanceboard.
if (event.type === 'double-click' && (event.source === 'ring' || event.source === 'watch')) {
if (shell.isScreenOn()) shell.sleep()
else shell.wake('sidebar')
} else if (event.type === 'display-wake' && !shell.isScreenOn()) shell.wake('sidebar')
this.requestShellRender()
return
}
if (!shell.isScreenOn()) {
const glanceEvent = this.glance.eventForGesture(headTilt ? 'head-tilt' : event.type)
if (glanceEvent?.type === 'dismiss') this.glance.dismiss()
@@ -409,6 +482,7 @@ export class IosPreviewController {
if (error) { this.onError(error); return }
if (!this.session) {
this.session = new GlassesSession(iosBluetooth(), state => {
if (state.phase !== 'connected' && state.phase !== 'connecting') this.glassesWorn = null
if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging })
this.syncRuntime()
@@ -421,24 +495,28 @@ export class IosPreviewController {
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
}).catch(error => this.fail(error))
}, message => this.logBluetooth(message), () => this.refreshClock(), receiveCompassEvent)
}, message => this.logBluetooth(message), () => {
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
this.refreshClock()
}, receiveCompassEvent, wearing => this.handleWearState(wearing))
bindCompassSession(this.session)
}
await this.session.start(addresses)
}
async disconnect(): Promise<void> { await this.session?.stop() }
startVoiceInput(): void { shell.startVoiceInput() }
startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() }
private async prepareVoiceCapture(): Promise<boolean> {
if (this.glassesLocked) return false
if (this.session?.state.phase !== 'connected') {
if (this.active) this.onError('Connect the glasses to use their microphone.')
return false
}
const ready = await iosVoiceInput.prepare(this.active)
if (!ready && this.active) this.onError(iosVoiceInput.statusText)
return ready && this.session?.state.phase === 'connected'
return ready && !this.glassesLocked && this.session?.state.phase === 'connected'
}
private async startVoiceCapture(endpointing = false): Promise<void> {
if (!this.session || this.session.state.phase !== 'connected') return
if (this.glassesLocked || !this.session || this.session.state.phase !== 'connected') return
await iosVoiceInput.startGlassesCapture(this.session, message => this.logBluetooth(message), endpointing)
}
private logBluetooth(message: string): void {
@@ -458,13 +536,14 @@ export class IosPreviewController {
catch (error) { console.warn(`Bluetooth log: ${error}`) }
}
async typeIntoApp(): Promise<void> {
if (this.glassesLocked) return
if (!this.active) { this.logBluetooth('Text input requires opening Faceclaw on the phone'); return }
if (this.prompting || !shell.foregroundWindow()?.receiveTextInput) return
this.prompting = true
try {
const result = await Dialogs.prompt({ title: 'Type into ' + shell.foregroundWindow()?.title,
message: 'Enter text to send to this app.', okButtonText: 'Send', cancelButtonText: 'Cancel' })
if (result.result) shell.sendTextToForegroundWindow(result.text)
if (result.result && !this.glassesLocked) shell.sendTextToForegroundWindow(result.text)
} catch (error) { this.fail(error) }
finally { this.prompting = false }
}
+24
View File
@@ -0,0 +1,24 @@
import { GrayImage, G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image";
import { getDefaultMediumFont } from "../graphics/ui-fonts";
import { wrapText } from "../graphics/textwrap";
export const LOCK_SCREEN_SURFACE_ID = "lock-screen";
const LOCK_SCREEN_MESSAGE = "Glasses locked; unlock the phone to unlock the glasses.";
export function createLockScreenImage(): GrayImage {
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
const font = getDefaultMediumFont();
const boxWidth = 480;
const boxHeight = 150;
const boxX = Math.round((G2_LENS_WIDTH - boxWidth) / 2);
const boxY = Math.round((G2_LENS_HEIGHT - boxHeight) / 2);
image.drawRoundedRect(boxX, boxY, boxWidth, boxHeight, 150, 12);
const lines = wrapText(font, LOCK_SCREEN_MESSAGE, boxWidth - 64);
const textHeight = lines.length * font.lineHeight;
const firstY = boxY + Math.round((boxHeight - textHeight) / 2);
lines.forEach((line, index) => {
const x = Math.round((G2_LENS_WIDTH - font.measureText(line)) / 2);
image.drawText(font, x, firstY + index * font.lineHeight, line, 230);
});
return image;
}
+15
View File
@@ -0,0 +1,15 @@
import { bytes, concat, integer, readBytes, readInteger, type ProtocolMessage } from './ble-protocol'
// Matches BleProtocol.buildSetWearDetection/buildFaceclawWakeControl on Android.
export const enableWearDetection = (magic: number): Uint8Array => concat(
integer(1, 1), integer(2, magic), bytes(3, bytes(5, integer(1, 1))))
export const queryWearState = (): Uint8Array => concat(
integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, 7, 0, 0])))
export function decodeWearState(message: ProtocolMessage): boolean | null {
if (message.sid !== 0x10 || message.command !== 3) return null
const event = readBytes(message.payload, 5)
if (!event || readInteger(event, 1, -1) !== 1) return null
const state = readInteger(event, 2, -1)
return state === 0 ? false : state === 1 ? true : null
}