forked from Blendman974/faceclaw
iOS version stays connected to the glasses while in the background
This commit is contained in:
1 parent
e5ba7abc18
commit
ae14edcb59
5 files changed
+174
-40
No files matched your search
@@ -55,7 +55,8 @@ export class GlassesSession {
|
||||
private readonly deflate: (data: Uint8Array) => Uint8Array,
|
||||
private readonly onState: (state: SessionState) => void,
|
||||
private readonly onInput: (input: protocol.GlassesInput) => void,
|
||||
private readonly log: (message: string) => void = () => {}) {
|
||||
private readonly log: (message: string) => void = () => {},
|
||||
private readonly onActivity: () => void = () => {}) {
|
||||
this.off = transport.onEvent(event => this.receive(event))
|
||||
}
|
||||
private update(phase: SessionState['phase'], status: string): void {
|
||||
@@ -210,15 +211,15 @@ export class GlassesSession {
|
||||
return
|
||||
}
|
||||
if (event.kind !== 'notification' || !event.data) return
|
||||
const ring = event.identifier === this.ids.ring
|
||||
if (ring ? !protocol.RING_NOTIFY.includes(event.characteristic ?? '') : event.characteristic !== protocol.G2_NOTIFY) return
|
||||
try {
|
||||
const data = hexToBytes(event.data)
|
||||
if (event.identifier === this.ids.ring) {
|
||||
if (!protocol.RING_NOTIFY.includes(event.characteristic ?? '')) return
|
||||
if (ring) {
|
||||
const input = protocol.decodeRingInput(data)
|
||||
if (input) { this.log(`Direct R1 gesture ${input.eventType}`); this.onInput(input) }
|
||||
return
|
||||
}
|
||||
if (event.characteristic !== protocol.G2_NOTIFY) return
|
||||
for (const message of this.receiver.receive(event.identifier!, data)) {
|
||||
this.log(`RX ${event.identifier === this.ids.left ? 'L' : 'R'} sid=${message.sid.toString(16)} flag=${message.flag.toString(16)} cmd=${message.command} magic=${message.magic}`)
|
||||
if (![1, 6].includes(message.flag)) {
|
||||
@@ -243,6 +244,11 @@ export class GlassesSession {
|
||||
}
|
||||
}
|
||||
} catch (error) { this.log(`Ignored malformed BLE notification: ${this.message(error)}`) }
|
||||
finally {
|
||||
// CoreBluetooth can wake a suspended iOS process for a notification.
|
||||
// Service wall-clock deadlines now, without waiting for a suspended timer.
|
||||
if (this.state.phase === 'connected') { this.onActivity(); this.wake() }
|
||||
}
|
||||
}
|
||||
private applySettings(message: protocol.ProtocolMessage): void {
|
||||
const values = protocol.readBytes(message.payload, 4)
|
||||
@@ -262,6 +268,10 @@ export class GlassesSession {
|
||||
if (this.state.phase !== 'connected') return
|
||||
this.latest = protocol.packGray4(gray, 640, 480); this.schedule(0)
|
||||
}
|
||||
wake(): void {
|
||||
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
||||
void this.pump()
|
||||
}
|
||||
private schedule(delay = 1000): void {
|
||||
if (delay === 0 && this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
||||
if (this.timer !== null || this.pumping || this.state.phase !== 'connected') return
|
||||
@@ -273,7 +283,9 @@ export class GlassesSession {
|
||||
try {
|
||||
const now = Date.now()
|
||||
if (now - this.lastHeartbeat >= 4000) {
|
||||
const gap = now - this.lastHeartbeat
|
||||
await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now()
|
||||
this.log(`Heartbeat acknowledged (gap ${gap}ms)`)
|
||||
}
|
||||
if (now - this.lastLease >= 45_000) { await this.lease(true); this.lastLease = Date.now() }
|
||||
if (now - this.lastSettings >= (this.charging ? 30_000 : 300_000)) {
|
||||
|
||||
@@ -31,6 +31,8 @@ export class IosPreviewController {
|
||||
private clockTimer: ReturnType<typeof setInterval> | null = null
|
||||
private offSettings: (() => void) | null = null
|
||||
private active = false
|
||||
private runtimeRunning = false
|
||||
private clockMinute = -1
|
||||
private shellDirty = true
|
||||
private inputQueue: Promise<void> = Promise.resolve()
|
||||
private lastLayout = ''
|
||||
@@ -39,8 +41,6 @@ export class IosPreviewController {
|
||||
private readonly inProcessApps = new Map<string, InProcessWindow>()
|
||||
private readonly batteryObservers: any[] = []
|
||||
private session: GlassesSession | null = null
|
||||
private resumeConnection = false
|
||||
private suspendedDisconnect: Promise<void> = Promise.resolve()
|
||||
private logLines: string[] = []
|
||||
private logTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private readonly actions: LayerActions = {
|
||||
@@ -88,6 +88,40 @@ export class IosPreviewController {
|
||||
resume(): void {
|
||||
if (this.active) return
|
||||
this.active = true
|
||||
this.syncRuntime()
|
||||
this.logBluetooth('Phone foreground')
|
||||
if (this.session) this.onConnectionState({ ...this.session.state })
|
||||
this.session?.wake()
|
||||
this.relayout()
|
||||
shell.foregroundWindow()?.requestRender()
|
||||
this.requestShellRender()
|
||||
}
|
||||
pause(): void {
|
||||
if (!this.active) return
|
||||
this.active = false
|
||||
// The phone preview is hidden; the glasses still own their screen and input.
|
||||
this.syncRuntime()
|
||||
this.logBluetooth(`Phone background; glasses ${this.session?.state.phase ?? 'disconnected'}`)
|
||||
this.flushBluetoothLog()
|
||||
this.session?.wake()
|
||||
}
|
||||
private get runtimeNeeded(): boolean {
|
||||
return this.active || !!this.session && ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.session.state.phase)
|
||||
}
|
||||
private syncRuntime(): void {
|
||||
const running = this.runtimeNeeded
|
||||
if (running === this.runtimeRunning) return
|
||||
this.runtimeRunning = running
|
||||
for (const window of shell.getWindows()) window.setScreenOn?.(running && shell.isScreenOn())
|
||||
if (!running) {
|
||||
this.offSettings?.(); this.offSettings = null
|
||||
for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
|
||||
UIDevice.currentDevice.batteryMonitoringEnabled = false
|
||||
if (this.clockTimer !== null) clearInterval(this.clockTimer)
|
||||
if (this.renderTimer !== null) clearTimeout(this.renderTimer)
|
||||
this.clockTimer = this.renderTimer = null
|
||||
return
|
||||
}
|
||||
UIDevice.currentDevice.batteryMonitoringEnabled = true
|
||||
for (const name of [UIDeviceBatteryLevelDidChangeNotification, UIDeviceBatteryStateDidChangeNotification]) {
|
||||
this.batteryObservers.push(NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(name, null, NSOperationQueue.mainQueue, () => this.requestShellRender()))
|
||||
@@ -99,28 +133,11 @@ export class IosPreviewController {
|
||||
shell.foregroundWindow()?.requestRender()
|
||||
this.requestShellRender()
|
||||
})
|
||||
this.clockTimer = setInterval(() => this.requestShellRender(), 60_000)
|
||||
for (const window of shell.getWindows()) window.setScreenOn?.(shell.isScreenOn())
|
||||
this.relayout()
|
||||
shell.foregroundWindow()?.requestRender()
|
||||
this.requestShellRender()
|
||||
console.log('[ios-preview] Main screen active')
|
||||
if (this.resumeConnection) {
|
||||
this.resumeConnection = false
|
||||
void this.suspendedDisconnect.then(() => { if (this.active) void this.connect() })
|
||||
}
|
||||
this.clockTimer = setInterval(() => this.refreshClock(), 60_000)
|
||||
}
|
||||
pause(): void {
|
||||
this.resumeConnection = !!this.session && ['connected', 'connecting', 'retrying'].includes(this.session.state.phase)
|
||||
if (this.session) this.suspendedDisconnect = this.session.stop()
|
||||
this.active = false
|
||||
for (const window of shell.getWindows()) window.setScreenOn?.(false)
|
||||
this.offSettings?.(); this.offSettings = null
|
||||
for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
|
||||
UIDevice.currentDevice.batteryMonitoringEnabled = false
|
||||
if (this.clockTimer) clearInterval(this.clockTimer)
|
||||
if (this.renderTimer) clearTimeout(this.renderTimer)
|
||||
this.clockTimer = this.renderTimer = null
|
||||
private refreshClock(): void {
|
||||
const minute = Math.floor(Date.now() / 60_000)
|
||||
if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() }
|
||||
}
|
||||
private configureWindow(window: ShellWindow): void {
|
||||
this.compositor.configureSurface(window.surfaceId, {
|
||||
@@ -144,10 +161,11 @@ export class IosPreviewController {
|
||||
}
|
||||
private requestShellRender(): void { this.shellDirty = true; this.scheduleFrame() }
|
||||
private scheduleFrame(): void {
|
||||
if (!this.active || this.renderTimer !== null) return
|
||||
// Coalesce window/chrome updates into one preview, capped at 30 fps.
|
||||
if (!this.runtimeNeeded || this.renderTimer !== null) return
|
||||
// Coalesce window/chrome updates into one glasses frame, capped at 30 fps.
|
||||
this.renderTimer = setTimeout(() => {
|
||||
this.renderTimer = null
|
||||
if (!this.runtimeNeeded) return
|
||||
try {
|
||||
if (this.shellDirty) {
|
||||
this.shellDirty = false
|
||||
@@ -157,15 +175,18 @@ export class IosPreviewController {
|
||||
}
|
||||
const pixels = this.compositor.composite()
|
||||
this.session?.setFrame(pixels)
|
||||
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
|
||||
this.onFrame(image, `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
|
||||
if (this.active) {
|
||||
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
|
||||
this.onFrame(image, `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
|
||||
}
|
||||
} catch (error) { this.fail(error) }
|
||||
}, 33)
|
||||
}
|
||||
private fail(error: unknown): void {
|
||||
const message = error instanceof Error ? error.stack ?? error.message : String(error)
|
||||
console.error(`[ios-preview] ${message}`)
|
||||
this.onError(error instanceof Error ? error.message : String(error))
|
||||
this.logBluetooth(`App error: ${message}`)
|
||||
if (this.active) this.onError(error instanceof Error ? error.message : String(error))
|
||||
}
|
||||
gesture(gesture: PhoneGesture, origin: 'watch' | 'ring' | 'mirror', nx = 0, ny = 0): void {
|
||||
this.inputQueue = this.inputQueue.then(async () => {
|
||||
@@ -287,31 +308,38 @@ export class IosPreviewController {
|
||||
const { deflate } = require('pako') as { deflate: (data: Uint8Array) => Uint8Array }
|
||||
this.session = new GlassesSession(iosBluetooth(), deflate, state => {
|
||||
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging })
|
||||
this.onConnectionState(state)
|
||||
this.syncRuntime()
|
||||
if (this.active) this.onConnectionState(state)
|
||||
this.requestShellRender()
|
||||
}, input => {
|
||||
this.inputQueue = this.inputQueue.then(async () => {
|
||||
if (!this.active) return
|
||||
if (this.session?.state.phase !== 'connected') return
|
||||
await shell.receiveInput(rawInputEventToInputEvent(input)); this.requestShellRender()
|
||||
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
|
||||
}).catch(error => this.fail(error))
|
||||
}, message => this.logBluetooth(message))
|
||||
}, message => this.logBluetooth(message), () => this.refreshClock())
|
||||
}
|
||||
await this.session.start(addresses)
|
||||
}
|
||||
async disconnect(): Promise<void> { this.resumeConnection = false; await this.session?.stop() }
|
||||
async disconnect(): Promise<void> { await this.session?.stop() }
|
||||
private logBluetooth(message: string): void {
|
||||
const line = `${new Date().toISOString()} ${message}`
|
||||
const line = `${new Date().toISOString()} [${this.active ? 'foreground' : 'background'}${UIApplication.sharedApplication.protectedDataAvailable ? '' : ',protected-data-unavailable'}] ${message}`
|
||||
console.log(`[ios-ble] ${line}`); this.logLines.push(line)
|
||||
if (this.logLines.length > 200) this.logLines.shift()
|
||||
if (this.logLines.length > 1000) this.logLines.shift()
|
||||
if (this.logTimer !== null) return
|
||||
this.logTimer = setTimeout(() => {
|
||||
this.logTimer = null
|
||||
try { File.fromPath(path.join(knownFolders.documents().path, 'bluetooth.log')).writeTextSync(this.logLines.join('\n')) }
|
||||
catch (error) { console.warn(`Bluetooth log: ${error}`) }
|
||||
this.flushBluetoothLog()
|
||||
}, 500)
|
||||
}
|
||||
private flushBluetoothLog(): void {
|
||||
if (this.logTimer !== null) clearTimeout(this.logTimer)
|
||||
this.logTimer = null
|
||||
try { File.fromPath(path.join(knownFolders.documents().path, 'bluetooth.log')).writeTextSync(this.logLines.join('\n')) }
|
||||
catch (error) { console.warn(`Bluetooth log: ${error}`) }
|
||||
}
|
||||
async typeIntoApp(): Promise<void> {
|
||||
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 {
|
||||
@@ -322,6 +350,7 @@ export class IosPreviewController {
|
||||
finally { this.prompting = false }
|
||||
}
|
||||
private async editSetting(setting: { editorTitle: string; get(): string; set(value: string): void }): Promise<void> {
|
||||
if (!this.active) { this.logBluetooth('Editing text settings requires opening Faceclaw on the phone'); return }
|
||||
const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: setting.get(), okButtonText: 'Save', cancelButtonText: 'Cancel' })
|
||||
if (result.result) setting.set(result.text)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user