diff --git a/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/AppIcon.png b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/AppIcon.png new file mode 100644 index 00000000..42d0c7b7 Binary files /dev/null and b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/AppIcon.png differ diff --git a/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/Contents.json b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 00000000..0a42994e --- /dev/null +++ b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1 @@ +{"images":[{"filename":"AppIcon.png","idiom":"universal","platform":"ios","size":"1024x1024"}],"info":{"author":"xcode","version":1}} diff --git a/App_Resources/iOS/Assets.xcassets/Contents.json b/App_Resources/iOS/Assets.xcassets/Contents.json new file mode 100644 index 00000000..f223229d --- /dev/null +++ b/App_Resources/iOS/Assets.xcassets/Contents.json @@ -0,0 +1 @@ +{"info":{"author":"xcode","version":1}} diff --git a/App_Resources/iOS/Info.plist b/App_Resources/iOS/Info.plist index d8615900..3740c24b 100644 --- a/App_Resources/iOS/Info.plist +++ b/App_Resources/iOS/Info.plist @@ -20,6 +20,8 @@ NSBluetoothAlwaysUsageDescription Faceclaw uses Bluetooth to connect to your glasses and ring, display your apps, and receive gestures. + NSLocalNetworkUsageDescription + Faceclaw connects to the terminal servers you configure on your local network. UILaunchScreen UISupportedInterfaceOrientations @@ -30,5 +32,7 @@ UIRequiresFullScreen + UIFileSharingEnabled + LSSupportsOpeningDocumentsInPlace diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig index 05b2c3b1..bfe1e0b6 100644 --- a/App_Resources/iOS/build.xcconfig +++ b/App_Resources/iOS/build.xcconfig @@ -4,3 +4,5 @@ CODE_SIGN_STYLE = Automatic; // Select the local signing team in Xcode or pass --teamId to the iOS scripts. OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth + +ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; diff --git a/App_Resources/iOS/src/FaceclawSocket.h b/App_Resources/iOS/src/FaceclawSocket.h new file mode 100644 index 00000000..84a058c5 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSocket.h @@ -0,0 +1,8 @@ +#import +/** Thread-safe event queue; each NativeScript worker drains it on its own thread. */ +@interface FaceclawSocket : NSObject +- (instancetype)initWithURL:(NSString *)url; +- (NSString *)takeEvents; +- (void)sendText:(NSString *)text; +- (void)close; +@end diff --git a/App_Resources/iOS/src/FaceclawSocket.m b/App_Resources/iOS/src/FaceclawSocket.m new file mode 100644 index 00000000..a53e6e6b --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSocket.m @@ -0,0 +1,75 @@ +#import "FaceclawSocket.h" +@interface FaceclawSocket () +@property(nonatomic, strong) NSURLSession *session; +@property(nonatomic, strong) NSURLSessionWebSocketTask *task; +@property(nonatomic, strong) NSMutableArray *events; +@property(nonatomic) BOOL ended; +@property(nonatomic) NSUInteger queuedBytes; +@end +@implementation FaceclawSocket +- (instancetype)initWithURL:(NSString *)url { + if ((self = [super init])) { + _events = [NSMutableArray array]; + NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration]; + config.timeoutIntervalForRequest = 20; + _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; + _task = [_session webSocketTaskWithURL:[NSURL URLWithString:url]]; + _task.maximumMessageSize = 8 * 1024 * 1024; + [_task resume]; + } + return self; +} +- (void)enqueue:(NSDictionary *)event { + @synchronized(self) { + if (_ended) return; + _queuedBytes += [event[@"text"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; + if (_events.count >= 256 || _queuedBytes > 16 * 1024 * 1024) { + [_events removeAllObjects]; _queuedBytes = 0; + [_events addObject:@{@"kind": @"error", @"text": @"Terminal input queue overflow"}]; + [self close]; + } else [_events addObject:event]; + } +} +- (NSString *)takeEvents { + @synchronized(self) { + NSData *data = [NSJSONSerialization dataWithJSONObject:_events options:0 error:nil]; + [_events removeAllObjects]; _queuedBytes = 0; + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]"; + } +} +- (void)receiveNext { + __weak FaceclawSocket *weakSelf = self; + [_task receiveMessageWithCompletionHandler:^(NSURLSessionWebSocketMessage *message, NSError *error) { + FaceclawSocket *owner = weakSelf; if (!owner) return; + @synchronized(owner) { if (owner.ended) return; } + if (error) { [owner enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [owner close]; return; } + if (message.type == NSURLSessionWebSocketMessageTypeString) + [owner enqueue:@{@"kind": @"text", @"text": message.string}]; + [owner receiveNext]; + }]; +} +- (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketTask *)task didOpenWithProtocol:(NSString *)protocol { + [self enqueue:@{@"kind": @"open"}]; [self receiveNext]; +} +- (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketTask *)task didCloseWithCode:(NSURLSessionWebSocketCloseCode)code reason:(NSData *)reason { + [self enqueue:@{@"kind": @"closed", @"code": @(code), @"text": reason ? ([[NSString alloc] initWithData:reason encoding:NSUTF8StringEncoding] ?: @"") : @""}]; + [self close]; +} +- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { + if (error) [self enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; + [self close]; +} +- (void)sendText:(NSString *)text { + __weak FaceclawSocket *weakSelf = self; + [_task sendMessage:[[NSURLSessionWebSocketMessage alloc] initWithString:text] completionHandler:^(NSError *error) { + if (error) { [weakSelf enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [weakSelf close]; } + }]; +} +- (void)close { + @synchronized(self) { + if (_ended) return; _ended = YES; + [_task cancelWithCloseCode:NSURLSessionWebSocketCloseCodeNormalClosure reason:nil]; + [_session invalidateAndCancel]; _session = nil; + } +} +@end diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap index 03ec29ec..e1c2c706 100644 --- a/App_Resources/iOS/src/module.modulemap +++ b/App_Resources/iOS/src/module.modulemap @@ -1,5 +1,6 @@ module FaceclawGraphics { header "FaceclawGraphics.h" header "FaceclawBluetooth.h" + header "FaceclawSocket.h" export * } diff --git a/app/apps/developer/developer-app.ts b/app/apps/developer/developer-app.ts index ab45de75..eead6675 100644 --- a/app/apps/developer/developer-app.ts +++ b/app/apps/developer/developer-app.ts @@ -58,18 +58,24 @@ function debugTestsMenu(): MenuLayer { }, { label: "Buzzer demo", + disabled: global.isIOS, + description: global.isIOS ? "Glasses sound playback is not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new BuzzerDemoLayer()); }, }, { label: "Accelerometer demo", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new AccelerometerDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); }, }, { label: "Light sensor", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new LightSensorDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); }, @@ -91,6 +97,8 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc [ { label: "Load app from URL", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { const layer = new LoadAppFromUrlLayer(appContext); ctx.stack.push(layer); @@ -99,6 +107,8 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc }, { label: "Load app from QR code", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { const layer = new LoadAppFromQrLayer(appContext); ctx.stack.push(layer); @@ -107,6 +117,8 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc }, { label: "Show resource usage", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new ResourceUsageLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); }, diff --git a/app/apps/files/file-browser.ts b/app/apps/files/file-browser.ts index 819f53ef..eac9645e 100644 --- a/app/apps/files/file-browser.ts +++ b/app/apps/files/file-browser.ts @@ -585,8 +585,8 @@ export class FileBrowserLayer implements Layer { for (const path of getBookmarkedPaths()) { rows.push(this.bookmarkItem(path)); } - rows.push(this.placeItem("Internal storage", externalStorageRootPath())); - rows.push(this.placeItem("/", "/")); + rows.push(this.placeItem(global.isIOS ? "Faceclaw documents" : "Internal storage", externalStorageRootPath())); + if (!global.isIOS) rows.push(this.placeItem("/", "/")); return rows; } this.loadEntries(); diff --git a/app/apps/files/files-app.ts b/app/apps/files/files-app.ts index 01eaa065..60b53725 100644 --- a/app/apps/files/files-app.ts +++ b/app/apps/files/files-app.ts @@ -45,7 +45,7 @@ export type FilesAppOptions = InProcessAppOptions & { export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow { let created: InProcessWindow | null = null; const browser = new FileBrowserLayer({ - isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name) || EHPK_FILE.test(name) || isFontFile(name), + isSupportedFile: (name) => TEXT_FILE.test(name) || (!global.isIOS && (isDecodableImageFile(name) || EHPK_FILE.test(name) || isFontFile(name))), // The browser handles double-click itself (up a level), so it is not // wrapped in YieldAtRootLayer; it yields explicitly from the top level. onLeave: () => shell.yieldFocusToSidebar(), @@ -170,6 +170,7 @@ function confirmEhpkPermissions(ctx: LayerContext, entry: DirectoryEntry, procee * window for viewable types, empty for everything else (metadata only). */ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileInfoAction[] { + if (global.isIOS && !TEXT_FILE.test(entry.name)) return []; if (TEXT_FILE.test(entry.name)) { return [ { diff --git a/app/apps/ios-availability.ts b/app/apps/ios-availability.ts new file mode 100644 index 00000000..0a157dbe --- /dev/null +++ b/app/apps/ios-availability.ts @@ -0,0 +1,15 @@ +/** Native services still outside the current iOS app-screen milestone. */ +const unavailable: Record = { + music: 'Media-player integration is not available on iOS yet.', + notifications: 'Phone notification integration is not available on iOS yet.', + transcribe: 'Voice capture and transcription are not available on iOS yet.', + microphones: 'Glasses microphone capture is not available on iOS yet.', + compass: 'Glasses compass streaming is not available on iOS yet.', + navigate: 'Navigation location tracking is not available on iOS yet.', + weather: 'Weather location access is not available on iOS yet.', + calendar: 'Calendar access is not available on iOS yet.', + evenhub: 'The EvenHub store and app runtime are not available on iOS yet.', +} +export function iosAppUnavailableReason(appId: string): string | null { + return unavailable[appId] ?? null +} diff --git a/app/g2/glasses-session.ts b/app/g2/glasses-session.ts index 26c9418d..697128eb 100644 --- a/app/g2/glasses-session.ts +++ b/app/g2/glasses-session.ts @@ -16,14 +16,14 @@ export interface SessionTransport { stopScan(): void } export type SessionState = { phase: 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error'; - status: string; battery: number | null; ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string } + status: string; battery: number | null; charging: boolean | null; ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string } type PendingAck = { resolve: (message: protocol.ProtocolMessage) => void; reject: (error: Error) => void; timer: number | null; command: number; label: string } class AckTimeout extends Error {} /** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */ export class GlassesSession { - state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, ring: false, + state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false, frames: 0, capabilities: '', leftVersion: '', rightVersion: '' } private generation = 0 private ids: Record = {} @@ -68,7 +68,7 @@ export class GlassesSession { this.addresses = { ...addresses }; this.wanted = true if (!retry) this.retryCount = 0 const generation = ++this.generation - this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, ring: false } + this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false } this.charging = false this.update('connecting', 'Finding configured devices…') try { @@ -254,7 +254,7 @@ export class GlassesSession { if (right) this.state.rightVersion = right const battery = protocol.readInteger(values, 12, -1), charging = protocol.readInteger(values, 13, -1) if (battery >= 0 && battery <= 100) this.state.battery = battery - if (charging >= 0) this.charging = charging > 0 + if (charging >= 0) this.state.charging = this.charging = charging > 0 } this.onState({ ...this.state }) } diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index f1da35f9..32c48277 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -4,23 +4,27 @@ import { GlassesSession, type SessionState } from './glasses-session' import { loadDeviceAddresses } from './device-addresses' import { deviceAddressError } from './ios-peripheral-identity' import { createLauncherWindow, LAUNCHER_SURFACE_ID } from '../apps/launcher/launcher-app' -import { CalculatorLayer } from '../apps/calculator/calculator' -import { MathCoordinator } from '../apps/calculator/math/coordinator' +import { ALL_APPS } from '../apps/all-apps' +import type { AppContext, AppDefinition, AppLaunchParams } from '../apps/app-definition' +import { WorkerAppHost } from '../ui/shell/worker-window' +import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from '../ui/shell/in-process-window' +import { getStringSettingById } from '../ui/dashboard-settings' +import { readPhoneBatteryState } from '../native/phone-battery' +import { iosAppUnavailableReason } from '../apps/ios-availability' import { SurfaceCompositor } from '../graphics/surface-compositor' import { flattenPlanes, type Plane } from '../graphics/plane' import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image' import { previewPixels } from '../native/ios-graphics' import { makeInputEvent, type InputEventPayload } from '../ui/gestures' import { noopLayerActions, type LayerActions } from '../ui/layers' -import { MenuLayer } from '../ui/menu' -import { createInProcessWindow } from '../ui/shell/in-process-window' +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, timeFormatSetting, verticalPositionSetting } from '../ui/dashboard-settings' + previewColorSetting } from '../ui/dashboard-settings' import type { PhoneGesture } from '../phone-ui/phone-gestures' -/** Local display host: shared shell/windows, no device-service initialization. */ +/** iOS host for the shared app registry, shell, compositor and BLE session. */ export class IosPreviewController { private readonly compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT) private renderTimer: ReturnType | null = null @@ -31,6 +35,9 @@ export class IosPreviewController { private inputQueue: Promise = Promise.resolve() private lastLayout = '' private prompting = false + private readonly appHosts = new Map() + private readonly inProcessApps = new Map() + private readonly batteryObservers: any[] = [] private session: GlassesSession | null = null private resumeConnection = false private suspendedDisconnect: Promise = Promise.resolve() @@ -39,6 +46,9 @@ export class IosPreviewController { private readonly actions: LayerActions = { ...noopLayerActions, requestRender: () => this.requestShellRender(), + disconnect: () => this.disconnect(), + startVoiceCapture: () => this.typeIntoApp(), + startContinuousVoiceCapture: () => this.onError("Voice capture is not available on iOS yet."), startTextSettingEdit: setting => this.editSetting(setting), startTextSettingsEdit: async (settings, _title, finished) => { for (const setting of settings) await this.editSetting(setting) @@ -60,10 +70,9 @@ export class IosPreviewController { }) const launcher = createLauncherWindow({ actions: this.actions, - apps: () => [ - { appId: 'calculator', label: 'Calculator', icon: 'calculator' }, - { appId: 'display', label: 'Display', icon: 'settings' }, - ], + apps: () => ALL_APPS.filter(app => app.showInLauncher !== false).map(app => ({ + appId: app.appId, label: app.title, icon: app.icon, renderIcon: app.renderIcon, + })), launchApp: id => this.launchApp(id), uninstallApp: () => {}, submitFrame: planes => this.submit(LAUNCHER_SURFACE_ID, planes), @@ -73,17 +82,25 @@ export class IosPreviewController { shell.registerWindow(launcher) shell.wake('window') shell.focusWindow(launcher.windowId) + for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app)) } resume(): void { if (this.active) return this.active = true + UIDevice.currentDevice.batteryMonitoringEnabled = true + for (const name of [UIDeviceBatteryLevelDidChangeNotification, UIDeviceBatteryStateDidChangeNotification]) { + this.batteryObservers.push(NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(name, null, NSOperationQueue.mainQueue, () => this.requestShellRender())) + } + const phone = readPhoneBatteryState() + this.logBluetooth(`Phone battery ${phone.battery ?? "unknown"}% charging=${phone.charging}`) this.offSettings = onAnySettingChanged(() => { this.relayout() 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() @@ -97,7 +114,10 @@ export class IosPreviewController { 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 @@ -109,7 +129,7 @@ export class IosPreviewController { this.compositor.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId) } private relayout(): void { - const layout = `${displayModeSetting.get()}:${verticalPositionSetting.get()}` + const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";") if (layout === this.lastLayout) return this.lastLayout = layout for (const window of shell.getWindows()) { @@ -183,39 +203,72 @@ export class IosPreviewController { await shell.receiveInput(makeInputEvent({ type: 'click', source: 'watch' })) this.requestShellRender() } - launchApp(id: string): void { - const existing = shell.getWindows().find(window => window.appId === id) - if (existing) { shell.focusWindow(existing.windowId); existing.requestRender(); this.requestShellRender(); return } - const surfaceId = `window:${id}` - let render = () => {} - const actions = { ...this.actions, requestRender: () => render() } - const layer = id === 'calculator' - ? new CalculatorLayer(new MathCoordinator(), actions, () => this.typeIntoApp()) - : new MenuLayer('Display', [ - { label: 'Change screen size', onSelect: () => this.cycleDisplayMode() }, - { label: 'Toggle green / grayscale preview', onSelect: () => { previewColorSetting.set(previewColorSetting.get() === 'green' ? 'white' : 'green') } }, - { label: 'Toggle 12 / 24 hour clock', onSelect: () => { timeFormatSetting.set(timeFormatSetting.get() === '24h' ? '12h' : '24h') } }, - ], { x: 8, y: 8, width: 320, minHeight: 160, opaque: true }) - const app = createInProcessWindow({ - appId: id, windowId: id, title: id === 'calculator' ? 'Calculator' : 'Display', - iconLetter: id === 'calculator' ? '=' : 'D', icon: id === 'calculator' ? 'calculator' : 'settings', - closeable: true, actions, baseLayer: layer, - receiveTextInput: id === 'calculator' ? text => { app.stack.receiveTextInput(text); app.requestRender() } : undefined, - submitFrame: planes => this.submit(surfaceId, planes), - setSurfaceVisible: visible => { - if (layer instanceof CalculatorLayer) layer.setForeground(visible) - this.compositor.setSurfaceVisible(surfaceId, visible); this.scheduleFrame() - }, + async launchApp(id: string, params?: AppLaunchParams): Promise { + const app = ALL_APPS.find(app => app.appId === id) + if (!app) return + try { + const reason = iosAppUnavailableReason(id) + if (reason) { + await this.launchInProcessApp(id, `window:${id}`, options => createInProcessWindow({ + ...options, appId: id, windowId: id, title: app.title, icon: app.icon, + iconLetter: app.title[0], closeable: true, + baseLayer: new YieldAtRootLayer(new TextViewerLayer(reason, app.title)), + })) + } else await app.launch(this.buildAppContext(app), params) + console.log(`[ios-preview] Launched ${id}`) + } catch (error) { this.fail(error) } + } + private buildAppContext(app: AppDefinition): AppContext { + return { + appId: app.appId, apps: ALL_APPS, actions: this.actions, + launchApp: (id, params) => this.launchApp(id, params), uninstallApp: async () => {}, + launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create), + ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create), + submitWindowFrame: (id, planes) => this.submit(id, planes), + setWindowSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() }, + requestShellRender: () => this.requestShellRender(), appendLog: message => console.log(`[ios-app] ${message}`), + setTextEditorHost: () => {}, + } + } + private async launchInProcessApp(windowId: string, surfaceId: string, + create: (options: InProcessAppOptions) => InProcessWindow): Promise { + const existing = this.inProcessApps.get(windowId) + if (existing) { shell.focusWindow(windowId); existing.requestRender(); this.requestShellRender(); return } + const app = create({ + actions: this.actions, submitFrame: planes => this.submit(surfaceId, planes), + setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(surfaceId, visible); this.scheduleFrame() }, removeSurface: () => { this.compositor.removeSurface(surfaceId); this.scheduleFrame() }, - onClosed: () => { if (layer instanceof CalculatorLayer) layer.onRemoved() }, + reconfigureSurface: () => { const window = shell.getWindows().find(w => w.windowId === windowId); if (window) this.configureWindow(window); this.requestShellRender() }, + onClosed: () => { this.inProcessApps.delete(windowId) }, }) - render = app.requestRender - if (layer instanceof CalculatorLayer) layer.requestRender = render + this.inProcessApps.set(windowId, app) this.configureWindow(app.window) - shell.registerWindow(app.window) - shell.focusWindow(app.window.windowId) + shell.registerWindow(app.window); shell.focusWindow(windowId) app.requestRender(); this.requestShellRender() } + private ensureWorkerHost(appId: string, create: () => Worker): WorkerAppHost { + const existing = this.appHosts.get(appId) + if (existing) return existing + const host = new WorkerAppHost({ + appId, worker: create(), + configureSurface: async (id, visible, heightMode) => { + this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' }) + this.compositor.setSurfaceVisible(id, visible) + }, + setSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() }, + removeSurface: id => { this.compositor.removeSurface(id); this.scheduleFrame() }, + submitPixels: (id, pixels, width, height) => { + this.compositor.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }); this.scheduleFrame() + }, + requestShellRender: () => this.requestShellRender(), + openSettings: section => { void this.launchApp('settings', { section }) }, + startTextSettingEdit: id => { const setting = getStringSettingById(id); if (setting) void this.editSetting(setting) }, + endTextSettingEdit: () => {}, + startTextInput: () => { void this.typeIntoApp() }, + }) + this.appHosts.set(appId, host) + return host + } cycleDisplayMode(): void { const values = DISPLAY_MODE_VALUES displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length]) @@ -233,8 +286,9 @@ export class IosPreviewController { if (!this.session) { 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) - if (state.phase === 'connected') this.requestShellRender() + this.requestShellRender() }, input => { this.inputQueue = this.inputQueue.then(async () => { if (!this.active) return @@ -262,7 +316,7 @@ export class IosPreviewController { this.prompting = true try { const result = await Dialogs.prompt({ title: 'Type into ' + shell.foregroundWindow()?.title, - message: 'Enter an expression or follow-up.', okButtonText: 'Send', cancelButtonText: 'Cancel' }) + message: 'Enter text to send to this app.', okButtonText: 'Send', cancelButtonText: 'Cancel' }) if (result.result) shell.sendTextToForegroundWindow(result.text) } catch (error) { this.fail(error) } finally { this.prompting = false } diff --git a/app/native/active-display.ios.ts b/app/native/active-display.ios.ts new file mode 100644 index 00000000..c3a3d946 --- /dev/null +++ b/app/native/active-display.ios.ts @@ -0,0 +1,8 @@ +/** Workers send baked grayscale frames to the main-thread iOS compositor. */ +const display = { + submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, _x: number, _y: number, width: number, height: number): void { + const data = NSData.dataWithBytesLength(interop.handleof(buffer), buffer.byteLength) + global.postMessage({ type: 'surface-frame', surfaceId, width, height, pixels: data.base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions) }) + }, +} +export function getActiveDisplay(): typeof display { return display } diff --git a/app/native/alarms.ts b/app/native/alarms.ts index ca44672c..77181bfb 100644 --- a/app/native/alarms.ts +++ b/app/native/alarms.ts @@ -124,6 +124,7 @@ export function drainPhoneAlarmJournal(): PhoneAlarmAction[] { /** Conditions under which the phone may fail to ring, most serious first. */ export function checkPhoneAlarmReliability(): AlarmReliabilityIssue[] { + if (global.isIOS) return [{ code: "ios-foreground-only", message: "Keep Faceclaw open: background timer and alarm alerts are not available on iOS yet.", fixable: false }]; const context = getContext(); if (!context) return []; let parsed: any; diff --git a/app/native/file-access.ios.ts b/app/native/file-access.ios.ts new file mode 100644 index 00000000..214cf2ec --- /dev/null +++ b/app/native/file-access.ios.ts @@ -0,0 +1,51 @@ +import { knownFolders, path as paths } from '@nativescript/core' +export type DirectoryEntry = { name: string; path: string; isDirectory: boolean; sizeBytes: number; modifiedMs: number } +const fm = NSFileManager.defaultManager +// The iOS Files app exposes Documents; access is limited to Faceclaw's container. +export function hasAllFilesAccess(): boolean { return true } +export function requestAllFilesAccess(): void {} +export function externalStorageRootPath(): string { return knownFolders.documents().path } +export function appFilesDirPath(): string { return knownFolders.documents().path } +export function statPath(path: string): DirectoryEntry | null { + try { + const attrs = fm.attributesOfItemAtPathError(path) + if (!attrs) return null + const modified: any = attrs.objectForKey(NSFileModificationDate) + return { name: path.slice(path.lastIndexOf('/') + 1), path, isDirectory: attrs.objectForKey(NSFileType) === NSFileTypeDirectory, + sizeBytes: Number(attrs.objectForKey(NSFileSize)) || 0, + modifiedMs: modified instanceof Date ? modified.getTime() : Number(modified?.timeIntervalSince1970 ?? 0) * 1000 } + } catch { return null } +} +export function listDirectory(path: string): DirectoryEntry[] | null { + try { + const names = fm.contentsOfDirectoryAtPathError(path), entries: DirectoryEntry[] = [] + for (let i = 0; i < names.count; i++) { const entry = statPath(paths.join(path, names.objectAtIndex(i))); if (entry) entries.push(entry) } + return entries.sort((a, b) => Number(b.isDirectory) - Number(a.isDirectory) || a.name.localeCompare(b.name)) + } catch { return null } +} +export function readBinaryFile(path: string): Uint8Array | null { + const data = NSData.dataWithContentsOfFile(path) + return data ? new Uint8Array(interop.bufferFromData(data)).slice() : null +} +export function writeBinaryFile(path: string, bytes: Uint8Array): boolean { + try { + fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path.slice(0, path.lastIndexOf('/')), true, null) + const copy = bytes.slice(), data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength) + return data.writeToFileAtomically(path, true) + } catch { return false } +} +export function readTextFile(path: string): string | null { + const entry = statPath(path) + if (!entry || entry.isDirectory || entry.sizeBytes > 2_000_000) return null + const data = NSData.dataWithContentsOfFile(path) + const text = data ? NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding) : null + return text == null ? null : String(text).slice(0, 500_000) +} +export function writeTextToDownloads(filename: string, text: string): string | null { + const path = paths.join(knownFolders.documents().path, 'Downloads', filename.slice(filename.lastIndexOf('/') + 1)) + const data = NSString.stringWithString(text).dataUsingEncoding(NSUTF8StringEncoding) + return writeBinaryFile(path, new Uint8Array(interop.bufferFromData(data))) ? path : null +} +export function deletePathRecursively(path: string): void { + try { if (fm.fileExistsAtPath(path)) fm.removeItemAtPathError(path) } catch (error) { console.warn(`Delete failed: ${error}`) } +} diff --git a/app/native/g2mirror-client.ts b/app/native/g2mirror-client.ts index 038dfe03..ae58dc40 100644 --- a/app/native/g2mirror-client.ts +++ b/app/native/g2mirror-client.ts @@ -1,3 +1,4 @@ +import { openSocket } from "./socket"; import { toUint8Array } from "../util/array-util"; declare const com: any; @@ -181,7 +182,7 @@ export class G2MirrorClient { const generation = ++this.connectionGeneration; const url = `${this.options.secure ? "wss" : "ws"}://${this.options.host}:${this.options.port}`; this.setState("connecting", `Connecting to ${this.options.host}:${this.options.port}...`); - this.listenerProxy = new com.faceclaw.app.FaceclawWebSocketListener({ + this.listenerProxy = { onOpen: () => { if (this.stopped || generation !== this.connectionGeneration) return; this.setState("connecting", "Authenticating..."); @@ -206,9 +207,9 @@ export class G2MirrorClient { if (this.stopped || generation !== this.connectionGeneration) return; this.handleConnectionLost(`Connection failed: ${shortenError(String(message))}`); }, - }); + }; try { - this.ws = new com.faceclaw.app.FaceclawWebSocket(url, this.listenerProxy, null, null); + this.ws = openSocket(url, this.listenerProxy); } catch (error) { this.ws = null; this.handleConnectionLost(`Connection failed: ${shortenError(String((error as Error)?.message ?? error))}`); @@ -571,11 +572,19 @@ export class G2MirrorClient { function decodeBase64(data: string): Uint8Array { if (!data) return new Uint8Array(0); + if (global.isIOS) { + const decoded = NSData.alloc().initWithBase64EncodedStringOptions(data, 0 as NSDataBase64DecodingOptions); + return decoded ? new Uint8Array(interop.bufferFromData(decoded)).slice() : new Uint8Array(); + } return toUint8Array(android.util.Base64.decode(data, android.util.Base64.DEFAULT)); } function decodeBase64Utf8(data: string): string { if (!data) return ""; + if (global.isIOS) { + const decoded = NSData.alloc().initWithBase64EncodedStringOptions(data, 0 as NSDataBase64DecodingOptions); + return decoded ? String(NSString.alloc().initWithDataEncoding(decoded, NSUTF8StringEncoding) ?? "") : ""; + } const bytes = android.util.Base64.decode(data, android.util.Base64.DEFAULT); return String(new java.lang.String(bytes, "UTF-8")); } @@ -587,6 +596,7 @@ function stripAnsi(text: string): string { } function encodeBase64Utf8(text: string): string { + if (global.isIOS) return NSString.stringWithString(text).dataUsingEncoding(NSUTF8StringEncoding).base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions); const bytes = new java.lang.String(text).getBytes("UTF-8"); return String(android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP)); } diff --git a/app/native/phone-battery.ts b/app/native/phone-battery.ts index d93be911..b758fcd3 100644 --- a/app/native/phone-battery.ts +++ b/app/native/phone-battery.ts @@ -8,6 +8,15 @@ export type PhoneBatteryState = { }; export function readPhoneBatteryState(): PhoneBatteryState { + if (global.isIOS) { + const device = UIDevice.currentDevice + const level = device.batteryLevel + const state = device.batteryState + return { + battery: level >= 0 ? Math.round(level * 100) : null, + charging: state === UIDeviceBatteryState.Unknown ? null : state === UIDeviceBatteryState.Charging || state === UIDeviceBatteryState.Full, + } + } if (!global.isAndroid) { return { battery: null, charging: null }; } diff --git a/app/native/settings-store.ios.ts b/app/native/settings-store.ios.ts index 2d4c3dd5..7ffd322f 100644 --- a/app/native/settings-store.ios.ts +++ b/app/native/settings-store.ios.ts @@ -1,28 +1,56 @@ import { ApplicationSettings } from '@nativescript/core' -// Main-isolate settings for the initial iOS port. Worker synchronization will -// be added when worker-hosted apps are enabled on iOS. +// NSUserDefaults is shared by all NativeScript isolates. Each isolate polls +// just its observed keys so callbacks always execute on the owning JS thread. +const CHANGE_TOKEN = 'ios.settings.changeToken' +let lastToken = ApplicationSettings.getString(CHANGE_TOKEN, '') +const observed = new Map string; value: string }>() +function observe(key: string, read: () => string): void { + if (!observed.has(key)) observed.set(key, { read, value: read() }) +} +function markChanged(key: string): void { + // A unique token lets other isolates check one key while idle. It does not + // depend on a read/increment/write counter that concurrent writers could lose. + ApplicationSettings.setString(CHANGE_TOKEN, `${Date.now()}:${Math.random()}`) + const item = observed.get(key) + if (item) item.value = item.read() + changed(key) +} +let pollTimer: ReturnType | null = null const listeners = new Set<(key: string) => void>() function changed(key: string): void { setTimeout(() => { for (const listener of [...listeners]) listener(key) }, 0) } export function getStringSetting(key: string, fallback: string): string { - return ApplicationSettings.getString(key, fallback) + const value = ApplicationSettings.getString(key, fallback) + observe(key, () => JSON.stringify(ApplicationSettings.getString(key, fallback))) + return value } export function setStringSetting(key: string, value: string): void { if (getStringSetting(key, '') === value && ApplicationSettings.hasKey(key)) return ApplicationSettings.setString(key, value) - changed(key) + markChanged(key) } export function getBooleanSetting(key: string, fallback: boolean): boolean { - return ApplicationSettings.getBoolean(key, fallback) + const value = ApplicationSettings.getBoolean(key, fallback) + observe(key, () => JSON.stringify(ApplicationSettings.getBoolean(key, fallback))) + return value } export function setBooleanSetting(key: string, value: boolean): void { if (getBooleanSetting(key, false) === value && ApplicationSettings.hasKey(key)) return ApplicationSettings.setBoolean(key, value) - changed(key) + markChanged(key) } export function onSettingsStoreChanged(listener: (key: string) => void): () => void { listeners.add(listener) - return () => { listeners.delete(listener) } + if (!pollTimer) pollTimer = setInterval(() => { + const token = ApplicationSettings.getString(CHANGE_TOKEN, '') + if (token === lastToken) return + lastToken = token + for (const [key, item] of observed) { + const value = item.read() + if (value !== item.value) { item.value = value; changed(key) } + } + }, 250) + return () => { listeners.delete(listener); if (!listeners.size && pollTimer) { clearInterval(pollTimer); pollTimer = null } } } diff --git a/app/native/socket.ios.ts b/app/native/socket.ios.ts new file mode 100644 index 00000000..e0bb5897 --- /dev/null +++ b/app/native/socket.ios.ts @@ -0,0 +1,16 @@ +export type SocketListener = { onOpen(): void; onTextMessage(message: string): void; onClosed(code: number, reason: string): void; onFailure(message: string): void } +declare const FaceclawSocket: any +export function openSocket(url: string, listener: SocketListener): { sendText(text: string): void; close(code: number, reason: string): void } { + const native = FaceclawSocket.alloc().initWithURL(url) + let ended = false + const close = () => { if (ended) return; ended = true; clearInterval(timer); native.close() } + const timer = setInterval(() => { + for (const event of JSON.parse(native.takeEvents())) { + if (ended) break + if (event.kind === 'open') listener.onOpen() + else if (event.kind === 'text') listener.onTextMessage(event.text) + else { close(); if (event.kind === 'closed') listener.onClosed(event.code, event.text); else listener.onFailure(event.text) } + } + }, 25) + return { sendText: text => { if (!ended) native.sendText(text) }, close } +} diff --git a/app/native/socket.ts b/app/native/socket.ts new file mode 100644 index 00000000..7b952acc --- /dev/null +++ b/app/native/socket.ts @@ -0,0 +1,9 @@ +declare const com: any +export type SocketListener = { onOpen(): void; onTextMessage(message: string): void; onClosed(code: number, reason: string): void; onFailure(message: string): void } +export function openSocket(url: string, listener: SocketListener): { sendText(text: string): void; close(code: number, reason: string): void } { + const proxy = new com.faceclaw.app.FaceclawWebSocketListener(listener) + const native = new com.faceclaw.app.FaceclawWebSocket(url, proxy, null, null) + const connection = { listenerProxy: proxy, sendText: (text: string) => native.sendText(text), + close: (code: number, reason: string) => native.close(code, reason) } + return connection +} diff --git a/app/phone-ui/main-page.ios.ts b/app/phone-ui/main-page.ios.ts index ba014754..518e7eb5 100644 --- a/app/phone-ui/main-page.ios.ts +++ b/app/phone-ui/main-page.ios.ts @@ -1,5 +1,6 @@ import { Application, Button, Color, Dialogs, GridLayout, Image, Label, Page, StackLayout, type TouchGestureEventData } from '@nativescript/core' import { createConfigureDevicesPage } from './config-page.ios' +import { ALL_APPS } from '../apps/all-apps' import { IosPreviewController } from '../g2/ios-preview-controller' import { PhoneGestureRecognizer } from './phone-gestures' import { onAnySettingChanged, previewColorSetting } from '../ui/dashboard-settings' @@ -103,6 +104,12 @@ export function createMainPage(): Page { GridLayout.setColumn(view, index); tabs.addChild(view); tabButtons.push(view) } const sizeButton = button(controller.displayModeLabel, () => controller.cycleDisplayMode()) + settings.addChild(button('Open app', () => { void (async () => { + const apps = ALL_APPS.filter(app => app.showInLauncher !== false).slice().sort((a, b) => a.title.localeCompare(b.title)) + const title = await Dialogs.action({ title: 'Open app', cancelButtonText: 'Cancel', actions: apps.map(app => app.title) }) + const app = apps.find(app => app.title === title) + if (app) { await controller.launchApp(app.appId); selectTab('watch') } + })() })) settings.addChild(sizeButton) settings.addChild(button('Toggle green / grayscale', () => previewColorSetting.set(previewColorSetting.get() === 'green' ? 'white' : 'green'))) const help = new Label() diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index b609de6e..2e6c0989 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -83,7 +83,7 @@ export function createSettingsPanelLayer(): SettingsPanelLayer { } function settingsSections(): SettingsSection[] { - return [ + const sections: SettingsSection[] = [ { label: "Display", items: [ @@ -232,6 +232,18 @@ function settingsSections(): SettingsSection[] { ], }, ]; + if (!global.isIOS) return sections; + const deferred = new Set(["Voice", "Assistant", "Navigate", "Watch", "Developer"]); + return sections.map(section => { + if (deferred.has(section.label)) return { label: section.label, items: [{ + label: "Not available on iOS yet", disabled: true, onSelect: () => {}, + description: `${section.label} integration has not been ported to iOS.`, + }] }; + if (section.label === "Display") return { ...section, items: section.items.filter(item => + ![brightnessSetting.label, screenTimeoutSetting.label, lockScreenEnabledSetting.label].includes(item.label)) }; + if (section.label === "Phone display") return { ...section, items: section.items.filter(item => item.label !== mirrorTouchSetting.label) }; + return section; + }); } const LOCAL_MODEL_GB = `${(LOCAL_MODEL.sizeBytes / 1e9).toFixed(1)}GB`; diff --git a/app/ui/font-picker.ts b/app/ui/font-picker.ts index 70b113ca..66d05364 100644 --- a/app/ui/font-picker.ts +++ b/app/ui/font-picker.ts @@ -278,6 +278,7 @@ export class FontPickerLayer implements Layer { /** Installed fonts grouped into families (styles sorted by weight). */ function collectFamilies(monospaceOnly: boolean): FontFamily[] { + if (global.isIOS) return []; // Native TTF rendering has not been ported. const byLabel = new Map(); for (const font of listInstalledFonts()) { if (monospaceOnly && !font.monospace) continue; @@ -326,7 +327,7 @@ export function terminalFontPickerMenuItem(): MenuItem { function fontPickerMenuItem(options: FontPickerOptions & { rowLabel: string; description: string }): MenuItem { return { label: options.rowLabel, - description: options.description, + description: global.isIOS ? "Choose a bundled bitmap font. Native TTF rendering is not available on iOS yet." : options.description, onSelect: (ctx) => { ctx.stack.push(new FontPickerLayer(options)); }, @@ -338,7 +339,7 @@ function fontPickerMenuItem(options: FontPickerOptions & { rowLabel: string; des y, width, options.rowLabel, - fontSelectionLabel(options.get()), + global.isIOS && options.get().kind === "ttf" ? "Terminus (iOS)" : fontSelectionLabel(options.get()), ); }, }; diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts index c268054c..cc6f92c0 100644 --- a/app/ui/shell/worker-window.ts +++ b/app/ui/shell/worker-window.ts @@ -10,7 +10,7 @@ import { shell, type ShellWindow } from "./shell"; * Messages between the shell (main thread) and an app worker. One worker * hosts one app, which may have several windows; messages are routed by * windowId. Everything crossing this boundary is small JSON; pixels go - * worker→Java directly. + * worker→Java directly on Android; iOS posts baked frames to the main host. */ export type WorkerAppMessage = | { type: "open-window"; windowId: string; surfaceId: string; title: string; viewport: { width: number; height: number } } @@ -25,6 +25,7 @@ export type WorkerAppMessage = | { type: "tool-call"; callId: string; windowId: string; name: string; args: unknown }; export type WorkerAppReply = + | { type: "surface-frame"; surfaceId: string; width: number; height: number; pixels: string } | { /** * The worker's bundle has evaluated and its onmessage handler is @@ -163,6 +164,8 @@ export type WorkerAppHostOptions = { setSurfaceVisible: (surfaceId: string, visible: boolean) => void; removeSurface: (surfaceId: string) => void; requestShellRender: () => void; + submitPixels?: (surfaceId: string, pixels: Uint8Array, width: number, height: number) => void; + startTextInput?: () => void; /** Open or focus the Settings app, optionally selecting a section. */ openSettings: (section?: string) => void; /** Open the phone app's text editor on a string setting (by id). */ @@ -175,8 +178,8 @@ export type WorkerAppHostOptions = { * Owns the Worker for one app and adapts its windows to the shell's window * interface: forwards input and lifecycle over postMessage, relays worker * requests (yield-focus, new windows, attention flags) back to the shell, - * and manages compositor surfaces for the app's windows. The worker submits - * frames straight to the Java compositor, so no pixels cross this boundary. + * and manages compositor surfaces for the app's windows. Android workers send + * frames straight to Java; iOS workers use surface-frame replies. */ /** A tool-call awaiting its worker reply; also its own leak-safety timeout. */ type PendingToolCall = { @@ -212,6 +215,14 @@ export class WorkerAppHost { const message = event.data as WorkerAppReply | undefined; if (!message) return; switch (message.type) { + case "surface-frame": { + if (!global.isIOS || !this.options.submitPixels || !this.openWindows.has(message.surfaceId.replace(/^window:/, ""))) break; + const { width, height } = message; + if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || width > 640 || height > 480) break; + const data = NSData.alloc().initWithBase64EncodedStringOptions(message.pixels, 0 as NSDataBase64DecodingOptions); + if (data?.length === width * height) this.options.submitPixels(message.surfaceId, new Uint8Array(interop.bufferFromData(data)), width, height); + break; + } case "worker-ready": this.workerReady = true; for (const queued of this.queuedMessages.splice(0)) { @@ -256,7 +267,8 @@ export class WorkerAppHost { // Menus only open on the focused window, but re-check foreground: // the reply crosses a thread boundary and focus may have moved. if (shell.foregroundWindow()?.windowId === message.windowId) { - shell.startVoiceInput(); + if (this.options.startTextInput) this.options.startTextInput(); + else shell.startVoiceInput(); } break; case "close-window-request": diff --git a/tests/app-layout.test.cjs b/tests/app-layout.test.cjs index 38450863..4dfecb33 100644 --- a/tests/app-layout.test.cjs +++ b/tests/app-layout.test.cjs @@ -148,6 +148,9 @@ test('resizing a terminal reconnects with the new grid and ignores callbacks fro }, } } }, }; + const socketContext = { exports: {}, com: context.com }; + vm.runInNewContext(js(read('app/native/socket.ts')), socketContext); + context.require = name => name === './socket' ? socketContext.exports : {}; vm.runInNewContext(js(read('app/native/g2mirror-client.ts')), context); const client = new context.exports.G2MirrorClient({ host: 'localhost', port: 1234, cols: 72, rows: 28 }); client.start(); diff --git a/tests/ios-app-host.test.cjs b/tests/ios-app-host.test.cjs new file mode 100644 index 00000000..ea75fff1 --- /dev/null +++ b/tests/ios-app-host.test.cjs @@ -0,0 +1,56 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const vm = require('node:vm'); +const ts = require('typescript'); +const path = require('node:path'); +function load(file, context) { + const sandbox = { exports: {}, ...context }; + const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); + vm.runInNewContext(ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox); + return sandbox.exports; +} +test('iOS phone battery distinguishes unknown, charging, full and unplugged readings', () => { + const device = { batteryLevel: -1, batteryState: 0 }; + const api = load('app/native/phone-battery.ts', { require: () => ({}), global: { isIOS: true }, + UIDevice: { currentDevice: device }, UIDeviceBatteryState: { Unknown: 0, Unplugged: 1, Charging: 2, Full: 3 } }); + assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: null, charging: null }); + device.batteryLevel = 0.726; device.batteryState = 2; + assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 73, charging: true }); + device.batteryLevel = 1; device.batteryState = 3; + assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 100, charging: true }); + device.batteryLevel = 0; device.batteryState = 1; + assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 0, charging: false }); +}); +test('settings changes propagate between iOS worker isolates once, including after a getter read', () => { + const store = new Map(), ticks = new Set(), tasks = []; + const ApplicationSettings = { getString: (k, d) => store.get(k) ?? d, getBoolean: (k, d) => store.get(k) ?? d, + setString: (k, v) => store.set(k, v), setBoolean: (k, v) => store.set(k, v), hasKey: k => store.has(k) }; + const context = { require: () => ({ ApplicationSettings }), setTimeout: fn => tasks.push(fn), + setInterval: fn => { ticks.add(fn); return fn; }, clearInterval: fn => ticks.delete(fn) }; + const main = load('app/native/settings-store.ios.ts', context), worker = load('app/native/settings-store.ios.ts', context); + const seenMain = [], seenWorker = []; + const offMain = main.onSettingsStoreChanged(k => seenMain.push(k)); + const offWorker = worker.onSettingsStoreChanged(k => seenWorker.push(k)); + main.getStringSetting('terminal.connections', '[]'); worker.getStringSetting('terminal.connections', '[]'); + main.setStringSetting('terminal.connections', '["test"]'); + assert.equal(worker.getStringSetting('terminal.connections', '[]'), '["test"]'); + const flush = () => { for (const tick of ticks) tick(); while (tasks.length) tasks.shift()(); }; + flush(); flush(); + assert.deepEqual(seenMain, ['terminal.connections']); assert.deepEqual(seenWorker, ['terminal.connections']); + main.getBooleanSetting('sound', true); worker.getBooleanSetting('sound', true); + worker.setBooleanSetting('sound', false); flush(); + assert.equal(main.getBooleanSetting('sound', true), false); + assert.deepEqual(seenMain, ['terminal.connections', 'sound']); + offWorker(); offMain(); assert.equal(ticks.size, 0); +}); +test('iOS worker frames preserve baked grayscale bytes through the message boundary', () => { + const messages = []; + const api = load('app/native/active-display.ios.ts', { global: { postMessage: m => messages.push(m) }, + interop: { handleof: b => b }, NSData: { dataWithBytesLength: (b, n) => ({ base64EncodedStringWithOptions: () => Buffer.from(b, 0, n).toString('base64') }) } }); + const pixels = new Uint8Array([0, 1, 15, 127, 254, 255]); + api.getActiveDisplay().submitSurfaceFrame(pixels.buffer, 'window:blocks:main', 0, 0, 3, 2); + assert.equal(messages[0].surfaceId, 'window:blocks:main'); + assert.equal(messages[0].width, 3); assert.equal(messages[0].height, 2); + assert.deepEqual(Buffer.from(messages[0].pixels, 'base64'), Buffer.from(pixels)); +}); diff --git a/tests/ios-ble.test.cjs b/tests/ios-ble.test.cjs index 22645eb0..295f563a 100644 --- a/tests/ios-ble.test.cjs +++ b/tests/ios-ble.test.cjs @@ -133,6 +133,7 @@ test('session authenticates both arms, checks firmware and sends acknowledged di const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' }); assert.equal(h.session.state.phase, 'connected'); assert.equal(h.session.state.ring, true); assert.equal(h.session.state.battery, 90); + assert.equal(h.session.state.charging, false); assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']); h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1); const image = h.transport.sent.find(s => s.message.sid === 224 && s.message.command === 3); diff --git a/tests/ios-socket-native.cjs b/tests/ios-socket-native.cjs new file mode 100644 index 00000000..6bfd8d93 --- /dev/null +++ b/tests/ios-socket-native.cjs @@ -0,0 +1,15 @@ +// macOS integration test for the same NSURLSession transport compiled into iOS. +const { Server: WebSocketServer } = require('ws'); +const { spawn, execFileSync } = require('node:child_process'); +const { mkdtempSync, rmSync } = require('node:fs'); +const { tmpdir } = require('node:os'); +const { join, resolve } = require('node:path'); +const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-socket-')); +execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src', + 'tests/native/ios-socket-test.m', 'App_Resources/iOS/src/FaceclawSocket.m', '-o', join(dir, 'socket-test')], { cwd: root, stdio: 'inherit' }); +const server = new WebSocketServer({ host: '127.0.0.1', port: 0 }); +server.on('connection', socket => socket.on('message', data => socket.send(data.toString(), () => socket.close(1000, 'done')))); +server.on('listening', () => { + const child = spawn(join(dir, 'socket-test'), [`ws://127.0.0.1:${server.address().port}`], { stdio: 'inherit' }); + child.on('exit', code => { server.close(); rmSync(dir, { recursive: true, force: true }); process.exitCode = code ?? 1; }); +}); diff --git a/tests/native/ios-socket-test.m b/tests/native/ios-socket-test.m new file mode 100644 index 00000000..19db162b --- /dev/null +++ b/tests/native/ios-socket-test.m @@ -0,0 +1,23 @@ +#import +#import "FaceclawSocket.h" +int main(int argc, const char **argv) { + @autoreleasepool { + FaceclawSocket *socket = [[FaceclawSocket alloc] initWithURL:[NSString stringWithUTF8String:argv[1]]]; + BOOL opened = NO, echoed = NO, closed = NO; + NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:10]; + while (!closed && deadline.timeIntervalSinceNow > 0) { + NSArray *events = [NSJSONSerialization JSONObjectWithData:[[socket takeEvents] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; + for (NSDictionary *event in events) { + NSString *kind = event[@"kind"]; + if ([kind isEqual:@"open"]) { opened = YES; [socket sendText:@"Faceclaw echo: π 🔋"]; } + if ([kind isEqual:@"text"]) echoed = [event[@"text"] isEqual:@"Faceclaw echo: π 🔋"]; + if ([kind isEqual:@"closed"] || [kind isEqual:@"error"]) closed = YES; + } + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; + } + [socket close]; + if (!opened || !echoed || !closed) { NSLog(@"FAIL open=%d echo=%d close=%d", opened, echoed, closed); return 1; } + puts("PASS: native WebSocket opens, echoes UTF-8 and reports close"); + } + return 0; +}