diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig index b8939fa9..950bebe4 100644 --- a/App_Resources/iOS/build.xcconfig +++ b/App_Resources/iOS/build.xcconfig @@ -3,7 +3,7 @@ TARGETED_DEVICE_FAMILY = 1; CODE_SIGN_STYLE = Automatic; // Select the local signing team in Xcode or pass --teamId to the iOS scripts. -OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreText -framework Speech -framework AVFoundation +OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; diff --git a/App_Resources/iOS/src/FaceclawEvenHubWebView.h b/App_Resources/iOS/src/FaceclawEvenHubWebView.h new file mode 100644 index 00000000..04f3827d --- /dev/null +++ b/App_Resources/iOS/src/FaceclawEvenHubWebView.h @@ -0,0 +1,13 @@ +#import +#import + +/** One local EHPK runtime, retained by its TS session until its window closes. */ +@interface FaceclawEvenHubWebView : NSObject +@property(nonatomic, copy) void (^eventHandler)(NSString *json); +@property(nonatomic, copy) NSString *packageIdentifier; +- (void)start:(NSString *)directory entrypoint:(NSString *)entrypoint script:(NSString *)script; +- (void)evaluate:(NSString *)script; +- (void)showOnPhone; +- (void)hideOnPhone; +- (void)destroy; +@end diff --git a/App_Resources/iOS/src/FaceclawEvenHubWebView.m b/App_Resources/iOS/src/FaceclawEvenHubWebView.m new file mode 100644 index 00000000..f31b1cec --- /dev/null +++ b/App_Resources/iOS/src/FaceclawEvenHubWebView.m @@ -0,0 +1,148 @@ +#import "FaceclawEvenHubWebView.h" +#import +#import +#import + +@interface FaceclawEvenHubWebView () +@property(nonatomic) WKWebView *webView; +@property(nonatomic) UIView *host; +@property(nonatomic) UIButton *back; +@property(nonatomic) NSTimer *timer; +@property(nonatomic) NSString *directory; +@property(nonatomic) BOOL ticking; +@property(nonatomic) BOOL destroyed; +@end + +@implementation FaceclawEvenHubWebView +- (void)emit:(NSDictionary *)event { + if (!_eventHandler || _destroyed) return; + NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil]; + if (data) _eventHandler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); +} +- (void)start:(NSString *)directory entrypoint:(NSString *)entrypoint script:(NSString *)script { + _directory = directory.stringByStandardizingPath.stringByResolvingSymlinksInPath; + WKWebViewConfiguration *config = [WKWebViewConfiguration new]; + // Isolate browser storage by package, retaining it across closing/reopening. + // SDK storage also persists independently through the shared TS session. + config.websiteDataStore = WKWebsiteDataStore.nonPersistentDataStore; + if (@available(iOS 17.0, *)) { + if (_packageIdentifier.length) { + NSData *key = [[@"com.faceclaw.ehpk:" stringByAppendingString:_packageIdentifier] dataUsingEncoding:NSUTF8StringEncoding]; + unsigned char digest[CC_SHA256_DIGEST_LENGTH]; CC_SHA256(key.bytes, (CC_LONG)key.length, digest); + config.websiteDataStore = [WKWebsiteDataStore dataStoreForIdentifier:[[NSUUID alloc] initWithUUIDBytes:digest]]; + } + } + [config setURLSchemeHandler:self forURLScheme:@"faceclaw-ehpk"]; + [config.userContentController addScriptMessageHandler:self name:@"faceclaw"]; + [config.userContentController addUserScript:[[WKUserScript alloc] initWithSource:script + injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES]]; + _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config]; + _webView.navigationDelegate = self; + _webView.accessibilityIdentifier = @"evenhub-webview"; + UIWindow *window = nil; + for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) { + if (![scene isKindOfClass:UIWindowScene.class]) continue; + for (UIWindow *candidate in ((UIWindowScene *)scene).windows) if (candidate.isKeyWindow) window = candidate; + } + if (!window) window = UIApplication.sharedApplication.keyWindow; + if (!window) { [self emit:@{@"kind": @"error", @"message": @"No phone window is available."}]; return; } + _host = [[UIView alloc] initWithFrame:window.bounds]; + _host.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + _host.backgroundColor = UIColor.systemBackgroundColor; + _host.userInteractionEnabled = NO; + _host.accessibilityElementsHidden = YES; + [window insertSubview:_host atIndex:0]; + [_host addSubview:_webView]; + _webView.translatesAutoresizingMaskIntoConstraints = NO; + _back = [UIButton buttonWithType:UIButtonTypeSystem]; + [_back setTitle:@"Back to Faceclaw" forState:UIControlStateNormal]; + _back.accessibilityIdentifier = @"evenhub-back"; + [_back addTarget:self action:@selector(hideOnPhone) forControlEvents:UIControlEventTouchUpInside]; + [_host addSubview:_back]; _back.translatesAutoresizingMaskIntoConstraints = NO; + [NSLayoutConstraint activateConstraints:@[ + [_back.topAnchor constraintEqualToAnchor:_host.safeAreaLayoutGuide.topAnchor], + [_back.leadingAnchor constraintEqualToAnchor:_host.leadingAnchor], + [_back.trailingAnchor constraintEqualToAnchor:_host.trailingAnchor], + [_back.heightAnchor constraintEqualToConstant:44], + [_webView.topAnchor constraintEqualToAnchor:_back.bottomAnchor], + [_webView.leadingAnchor constraintEqualToAnchor:_host.leadingAnchor], + [_webView.trailingAnchor constraintEqualToAnchor:_host.trailingAnchor], + [_webView.bottomAnchor constraintEqualToAnchor:_host.safeAreaLayoutGuide.bottomAnchor] + ]]; + // Keep a nonzero attached view behind the dashboard; do not hide/detach it. + // Bound host ticks to one evaluation in flight, including across suspension. + __weak typeof(self) weakSelf = self; + _timer = [NSTimer timerWithTimeInterval:1.0 / 60 repeats:YES block:^(NSTimer *timer) { + typeof(self) strongSelf = weakSelf; + if (!strongSelf || strongSelf.ticking || UIApplication.sharedApplication.applicationState != UIApplicationStateActive) return; + strongSelf.ticking = YES; + [strongSelf.webView evaluateJavaScript:@"window.__fcTimerTick && window.__fcTimerTick(); window.__fcRafTick && window.__fcRafTick();" completionHandler:^(id result, NSError *error) { + weakSelf.ticking = NO; + }]; + }]; + [NSRunLoop.mainRunLoop addTimer:_timer forMode:NSRunLoopCommonModes]; + NSURLComponents *url = [NSURLComponents new]; + url.scheme = @"faceclaw-ehpk"; url.host = @"app"; url.path = [@"/" stringByAppendingString:entrypoint]; + [_webView loadRequest:[NSURLRequest requestWithURL:url.URL]]; +} +- (void)evaluate:(NSString *)script { + if (!_destroyed) [_webView evaluateJavaScript:script completionHandler:nil]; +} +- (void)showOnPhone { + if (_destroyed) return; + [_host.superview bringSubviewToFront:_host]; _host.userInteractionEnabled = YES; _host.accessibilityElementsHidden = NO; +} +- (void)hideOnPhone { + [_host endEditing:YES]; [_host.superview sendSubviewToBack:_host]; _host.userInteractionEnabled = NO; _host.accessibilityElementsHidden = YES; + [self emit:@{@"kind": @"hidden"}]; +} +- (void)destroy { + if (_destroyed) return; + _destroyed = YES; [_timer invalidate]; _timer = nil; + [_webView stopLoading]; _webView.navigationDelegate = nil; + [_webView.configuration.userContentController removeScriptMessageHandlerForName:@"faceclaw"]; + [_webView.configuration.userContentController removeAllUserScripts]; + [_host removeFromSuperview]; _host = nil; _back = nil; _webView = nil; _eventHandler = nil; +} +- (void)userContentController:(WKUserContentController *)controller didReceiveScriptMessage:(WKScriptMessage *)message { + // Subframes and navigated third-party documents never receive host access. + NSURL *url = message.frameInfo.request.URL; + if (!message.frameInfo.isMainFrame || ![url.scheme isEqual:@"faceclaw-ehpk"] || ![url.host isEqual:@"app"]) return; + if ([message.body isKindOfClass:NSDictionary.class]) [self emit:message.body]; +} +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)action decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { + NSURL *url = action.request.URL; + BOOL local = [url.scheme isEqual:@"faceclaw-ehpk"] && [url.host isEqual:@"app"]; + decisionHandler(local && action.targetFrame.isMainFrame ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel); +} +- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { + [self emit:@{@"kind": @"loaded"}]; +} +- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error { + [self emit:@{@"kind": @"error", @"message": error.localizedDescription}]; +} +- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error { + [self emit:@{@"kind": @"error", @"message": error.localizedDescription}]; +} +- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView { + [self emit:@{@"kind": @"error", @"message": @"The app's web process ended. Close and reopen the app."}]; +} +- (void)webView:(WKWebView *)webView startURLSchemeTask:(id)task { + NSURL *url = task.request.URL; + NSString *relative = [url.path stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; + NSString *file = [[_directory stringByAppendingPathComponent:relative] stringByStandardizingPath].stringByResolvingSymlinksInPath; + BOOL allowed = !_destroyed && [url.host isEqual:@"app"] && + [file hasPrefix:[_directory stringByAppendingString:@"/"]]; + NSData *data = allowed ? [NSData dataWithContentsOfFile:file options:NSDataReadingMappedIfSafe error:nil] : nil; + NSString *ext = file.pathExtension.lowercaseString; + NSString *mime = [@{@"js": @"text/javascript", @"mjs": @"text/javascript", @"html": @"text/html", + @"css": @"text/css", @"json": @"application/json", @"wasm": @"application/wasm", @"svg": @"image/svg+xml"} objectForKey:ext]; + if (!mime) mime = [UTType typeWithFilenameExtension:ext].preferredMIMEType ?: @"application/octet-stream"; + NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:data ? 200 : 404 + HTTPVersion:@"HTTP/1.1" headerFields:@{@"Content-Type": mime, @"Access-Control-Allow-Origin": @"*", @"Cache-Control": @"no-store"}]; + // Synchronous completion on WebKit's main-queue callback means there is no + // outstanding task to finish after stopURLSchemeTask cancels it. + [task didReceiveResponse:response]; [task didReceiveData:data ?: NSData.data]; [task didFinish]; +} +- (void)webView:(WKWebView *)webView stopURLSchemeTask:(id)task {} +@end diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap index e98bde65..7f9d2b72 100644 --- a/App_Resources/iOS/src/module.modulemap +++ b/App_Resources/iOS/src/module.modulemap @@ -6,5 +6,6 @@ module FaceclawGraphics { header "FaceclawLc3Decoder.h" header "FaceclawSpeech.h" header "FaceclawConfigPort.h" + header "FaceclawEvenHubWebView.h" export * } diff --git a/app/apps/evenhub/ehpk.ts b/app/apps/evenhub/ehpk.ts index f082a2be..28638f1a 100644 --- a/app/apps/evenhub/ehpk.ts +++ b/app/apps/evenhub/ehpk.ts @@ -18,6 +18,11 @@ const RECORD_FILE = 0xe4; const RECORD_DIR = 0xe5; const RECORD_FOOTER = 0xe3; +/** Archive paths are relative to one package, never to the phone container. */ +export function safeEhpkPath(value: string): boolean { + return !!value && !/[\\\u0000:]/.test(value) && value.split('/').every(part => !!part && part !== '.' && part !== '..'); +} + export type EhpkArchive = { /** Relative path (e.g. "app.json", "dist/index.html") to contents. */ files: Map; @@ -90,6 +95,7 @@ export function parseEhpk(data: Uint8Array): EhpkArchive { const uncompressedLength = view.getUint32(p + 8, true); const nameLength = view.getUint16(p + 14, true); const name = utf8Decode(unxor(data, p + 16, nameLength)); + if (!safeEhpkPath(name)) throw new Error(`ehpk: invalid file path ${name}`); const blob = unxor(data, p + 16 + nameLength, compressedLength); const content = decompress(blob, new Uint8Array(uncompressedLength)); if (content.length !== uncompressedLength) { @@ -119,6 +125,7 @@ export function parseManifest(appJsonText: string): EvenHubManifest { const name = String(raw.name ?? packageId); const version = String(raw.version ?? "0"); const entrypoint = String(raw.entrypoint ?? "index.html"); + if (!safeEhpkPath(entrypoint)) throw new Error('Invalid EHPK entrypoint'); const permissions = parsePermissions(raw.permissions); const privacyPolicyUrl = firstSafeHttpsUrl( diff --git a/app/apps/evenhub/manager.ts b/app/apps/evenhub/manager.ts index 12b45346..036f9b3e 100644 --- a/app/apps/evenhub/manager.ts +++ b/app/apps/evenhub/manager.ts @@ -66,16 +66,15 @@ export async function launchPackage( ): Promise { const data = readBinaryFile(ehpkPath); if (!data) { - ctx.appendLog(`evenhub: could not read ${ehpkPath}`); - return; + throw new Error('Could not read the EHPK file.'); } const archive = parseEhpk(data); const appJson = archive.files.get("app.json"); if (!appJson) { - ctx.appendLog("evenhub: package has no app.json"); - return; + throw new Error('The EHPK package has no app.json.'); } const manifest = parseManifest(utf8Decode(appJson)); + if (!archive.files.has(`dist/${manifest.entrypoint}`)) throw new Error('The EHPK entrypoint is missing from dist/.'); const appId = options.appId ?? "evenhub"; if (options.singleton) { const existing = Array.from(running.values()).find((app) => app.appId === appId); @@ -88,16 +87,18 @@ export async function launchPackage( // Unpack fresh into app-private storage, keyed by package id. const safeId = manifest.packageId.replace(/[^A-Za-z0-9._-]/g, "_"); - const baseDir = `${appFilesDirPath()}/evenhub-apps/${safeId}`; + if (!safeId || safeId === '.' || safeId === '..') throw new Error('Invalid EHPK package ID'); + const baseDir = `${appFilesDirPath()}/evenhub-apps/${safeId}${global.isIOS ? `-${Date.now()}-${nextSerial}` : ''}`; deletePathRecursively(baseDir); for (const [name, content] of Array.from(archive.files)) { if (!writeBinaryFile(`${baseDir}/${name}`, content)) { - ctx.appendLog(`evenhub: failed to write ${name}`); - return; + deletePathRecursively(baseDir); + throw new Error(`Could not unpack ${name}.`); } } - await startApp(ctx, manifest, `${baseDir}/dist`, "", appId); + try { await startApp(ctx, manifest, `${baseDir}/dist`, "", appId, global.isIOS ? () => deletePathRecursively(baseDir) : undefined); } + catch (error) { deletePathRecursively(baseDir); throw error; } } /** @@ -111,6 +112,7 @@ async function startApp( distDir: string, remoteUrl: string, appId: string, + cleanup?: () => void, ): Promise { const windowId = `evenhub:app:${nextSerial++}`; const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl); @@ -123,6 +125,7 @@ async function startApp( phoneShownWindowId = null; } webView.destroy(); + cleanup?.(); running.delete(windowId); }, }); @@ -130,9 +133,11 @@ async function startApp( ensureBackHandler(); ctx.appendLog(`evenhub: launching ${manifest.name} ${manifest.version} (${manifest.packageId})`); - await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) => - createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)), - ); + try { + await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) => + createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)), + ); + } catch (error) { session.close(); throw error; } } /** @@ -210,6 +215,7 @@ export function closeRunningPackage(packageId: string): void { export function showOnPhone(windowId: string): void { const app = running.get(windowId); if (!app) return; + if (phoneShownWindowId && phoneShownWindowId !== windowId) hidePhone(); app.webView.showOnPhone(); phoneShownWindowId = windowId; } diff --git a/app/apps/evenhub/privacy-policy.ios.ts b/app/apps/evenhub/privacy-policy.ios.ts new file mode 100644 index 00000000..98d20710 --- /dev/null +++ b/app/apps/evenhub/privacy-policy.ios.ts @@ -0,0 +1,5 @@ +import { Utils } from '@nativescript/core' +/** Open outside the app runtime; this page has no EvenHub bridge. */ +export function openPrivacyPolicyOnPhone(url: string, _appName: string): void { + if (/^https:\/\//i.test(url)) Utils.openUrl(url) +} diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts index c9341c8c..318f7cd6 100644 --- a/app/apps/evenhub/session.ts +++ b/app/apps/evenhub/session.ts @@ -334,6 +334,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH private windowHooks: EvenHubWindowHooks | null = null; private closed = false; private launchContextPushed = false; + private loadError = ''; private systemExitSent = false; /** The one-shot launch FOREGROUND_ENTER has been delivered (page exists). */ private launchEnterSent = false; @@ -383,6 +384,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH this.webViewHandle = handle; } + webViewFailed(message: string): void { + if (this.closed) return; + this.loadError = message; this.log(`evenhub: ${message}`); this.windowHooks?.requestRender(); + } + /** The WebView finished loading the app: push the one-shot launch context. */ webViewLoaded(): void { if (this.launchContextPushed) return; @@ -416,11 +422,12 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH // Focus changes always trigger a repaint of the foreground window, so paint // is a reliable place to sample shell input-focus for lifecycle events. this.updateShellFocus(focused); - if (!this.pageCreated || !this.page) { + if (this.loadError || !this.pageCreated || !this.page) { const image = new GrayImage(size.width, size.height, 0); const font = EvenHubFont.get(); - font.drawText(image, 16, 16, `Loading ${this.manifest.name}...`, 255); - font.drawText(image, 16, 16 + 2 * font.lineHeight, "(waiting for the app to build its page)", 120); + font.drawText(image, 16, 16, `${this.loadError ? 'Could not run' : 'Loading'} ${this.manifest.name}...`, 255); + font.drawTextWrapped(image, 16, 16 + 2 * font.lineHeight, Math.max(1, size.width - 32), + this.loadError || "(waiting for the app to build its page)", 120); return image; } return compositePage(this.page, size, focused); @@ -604,6 +611,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH * flows (foreground + screen on + no assistant modal). */ private audioControl(data: Record): boolean { + if (global.isIOS) return false; // EvenHub PCM routing is not ported yet. if (!permissionsIncludeMicrophone(this.manifest.permissions)) { this.log("evenhub: audioControl denied (app declares no microphone permission)"); return false; @@ -634,6 +642,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH /** getAppLocation(): one-shot fix, or null on denial/error (SDK contract). */ private async getAppLocation(): Promise { + if (global.isIOS) return null; if (!this.declaresLocation()) return null; if (!(await ensureFineLocationPermission())) return null; try { @@ -657,6 +666,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH * Navigate app's foreground-service-backed tracking. */ private async startLocationUpdates(data: Record): Promise { + if (global.isIOS) return false; if (!this.declaresLocation()) return false; if (!(await ensureFineLocationPermission())) return false; if (this.closed) return false; @@ -697,6 +707,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH * to the foreground app. reportFrq is one of 100..1000 (step 100). */ private imuControl(data: Record): boolean { + if (global.isIOS) return false; const enable = readEnableFlag(data); const freq = clampImuFreq(readOptionalNumber(data, "reportFrq") ?? readOptionalNumber(data, "reportFreq") ?? 100); // Payload field spelling isn't fully captured; log once per toggle. diff --git a/app/apps/evenhub/webview.ios.ts b/app/apps/evenhub/webview.ios.ts new file mode 100644 index 00000000..d4421b0a --- /dev/null +++ b/app/apps/evenhub/webview.ios.ts @@ -0,0 +1,43 @@ +import { EVENHUB_BRIDGE_INJECT_SCRIPT, buildFaceclawExtensionsScript, type EvenHubSession } from './session' +import { FACECLAW_VERSION } from '../../version' +import type { EvenHubWebView } from './webview' +declare const FaceclawEvenHubWebView: any + +export const IOS_EVENHUB_TRANSPORT_SCRIPT = ` +window.__faceclawEvenHub = { postMessage: function(name, args, id) { + window.webkit.messageHandlers.faceclaw.postMessage({kind:'call', name:name, args:args, id:id}); +}}; +(function() { + ['log','warn','error'].forEach(function(level) { + var original = console[level]; + console[level] = function() { + original.apply(console, arguments); + window.webkit.messageHandlers.faceclaw.postMessage({kind:'console', message:Array.prototype.map.call(arguments, String).join(' ').slice(0,4000)}); + }; + }); + window.addEventListener('error', function(e) { console.error(e.message); }); + window.addEventListener('unhandledrejection', function(e) { console.error(String(e.reason)); }); +})(); +` + +export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView { + if (session.remoteUrl) throw new Error('On iOS, open a local .ehpk package from Files.') + const host = FaceclawEvenHubWebView.new() + host.packageIdentifier = session.manifest.packageId + host.eventHandler = (json: string) => { + try { + const event = JSON.parse(json) + if (event.kind === 'call' && typeof event.name === 'string' && typeof event.args === 'string' && Number.isSafeInteger(event.id)) + session.handleBridgeCall(event.name, event.args, event.id) + else if (event.kind === 'loaded') session.webViewLoaded() + else if (event.kind === 'error') session.webViewFailed(String(event.message)) + else if (event.kind === 'console') console.log(`[evenhub:${session.manifest.name}] ${event.message}`) + } catch (error) { console.warn(`EvenHub bridge: ${error}`) } + } + // Defer loading until manager.ts has attached the session/window handles. + const loading = setTimeout(() => host.startEntrypointScript(session.distDir, session.manifest.entrypoint, + IOS_EVENHUB_TRANSPORT_SCRIPT + EVENHUB_BRIDGE_INJECT_SCRIPT + buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`)), 0) + return { native: host, evaluateJs: js => host.evaluate(js), + destroy: () => { clearTimeout(loading); host.destroy() }, + showOnPhone: () => host.showOnPhone(), hideOnPhone: () => host.hideOnPhone() } +} diff --git a/app/apps/files/files-app.ts b/app/apps/files/files-app.ts index 540be7d2..f628289d 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) || isFontFile(name) || (!global.isIOS && (isDecodableImageFile(name) || EHPK_FILE.test(name))), + isSupportedFile: (name) => TEXT_FILE.test(name) || isFontFile(name) || EHPK_FILE.test(name) || (!global.isIOS && isDecodableImageFile(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,7 +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) && !isFontFile(entry.name)) return []; + if (global.isIOS && !TEXT_FILE.test(entry.name) && !isFontFile(entry.name) && !EHPK_FILE.test(entry.name)) return []; if (TEXT_FILE.test(entry.name)) { return [ { @@ -192,7 +192,7 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI ]; } if (EHPK_FILE.test(entry.name)) { - return [ + const actions: FileInfoAction[] = [ { label: "Run app", onSelect: (ctx) => { @@ -210,6 +210,7 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI }, }, ]; + return global.isIOS ? actions.slice(0, 1) : actions; } if (isDecodableImageFile(entry.name)) { return [ diff --git a/app/apps/files/index.ts b/app/apps/files/index.ts index 0b744e48..92301cca 100644 --- a/app/apps/files/index.ts +++ b/app/apps/files/index.ts @@ -64,6 +64,7 @@ const filesApp: AppDefinition = { openEhpkApp: (path) => { void openEvenHubPackage(ctx, path).catch((error) => { ctx.appendLog(`evenhub launch failed: ${error}`); + openTextDocumentWindow(ctx, 'Could not run app', String(error)); }); }, installEhpkApp: async (path) => { diff --git a/app/apps/ios-availability.ts b/app/apps/ios-availability.ts index 0a157dbe..4a6b794a 100644 --- a/app/apps/ios-availability.ts +++ b/app/apps/ios-availability.ts @@ -8,7 +8,7 @@ const unavailable: Record = { 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.', + evenhub: 'Open a local .ehpk file in Files to run an EvenHub app. The EvenHub store is not available on iOS yet.', } export function iosAppUnavailableReason(appId: string): string | null { return unavailable[appId] ?? null diff --git a/app/graphics/evenhub-font.ts b/app/graphics/evenhub-font.ts index e1a8f55f..61e5025a 100644 --- a/app/graphics/evenhub-font.ts +++ b/app/graphics/evenhub-font.ts @@ -22,12 +22,10 @@ import { File, knownFolders } from "@nativescript/core"; import { getTextWidth } from "@evenrealities/pretext"; import { EVENHUB_RUNTIME_FONT_FILENAME, isEvenHubFontAsset } from "../g2/firmware-fonts"; -import { toUint8Array } from "../util/array-util"; +import { lvglMetrics, lvglGlyph } from "../native/lvgl-font"; import { GrayImage, grayToNibble, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image"; import { TtfFont } from "./ttf-font"; -declare const com: any; - const CJK_ASSET_PATH = "fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin"; /** @@ -124,7 +122,7 @@ export class EvenHubFont implements FwTextFont { this.glyphs.set(Number(key), asset.glyphs[key]!); } this.cjkPath = knownFolders.currentApp().getFile(CJK_ASSET_PATH).path; - const metrics = toUint8Array(com.faceclaw.app.LvglFontFile.getMetrics(this.cjkPath)); + const metrics = lvglMetrics(this.cjkPath); if (metrics.length < 2) { throw new Error("The bundled Source Han Sans font could not be loaded."); } @@ -388,7 +386,7 @@ export class EvenHubFont implements FwTextFont { private getCjkGlyph(codePoint: number): CjkGlyph | null { const cached = this.cjkGlyphs.get(codePoint); if (cached !== undefined) return cached; - const bytes = toUint8Array(com.faceclaw.app.LvglFontFile.getGlyph(this.cjkPath, codePoint)); + const bytes = lvglGlyph(this.cjkPath, codePoint); if (bytes.length < 8) { this.cjkGlyphs.set(codePoint, null); return null; diff --git a/app/graphics/lvgl-font.ts b/app/graphics/lvgl-font.ts new file mode 100644 index 00000000..60dd57e2 --- /dev/null +++ b/app/graphics/lvgl-font.ts @@ -0,0 +1,53 @@ +/** Reader for the bundled Source Han Sans serialized LVGL font. Matches + * Android LvglFontFile's metrics/glyph byte format, without a Java dependency. */ +export class LvglFont { + readonly metrics: Uint8Array + private readonly view: DataView + private readonly ids = new Map() + private readonly bitmaps: number + private readonly descriptors: number + constructor(private readonly bytes: Uint8Array) { + this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength) + if (bytes.length < 64 || bytes.slice(0, 4).some(v => v !== 90)) throw new Error('Missing LVGL font header') + const height = this.u16(0x38), baseline = height - this.u16(0x3a) + const descriptor = this.pointer(this.u32(0x34)) + this.bitmaps = this.pointer(this.u32(descriptor)); this.descriptors = this.pointer(this.u32(descriptor + 4)) + const cmaps = this.pointer(this.u32(descriptor + 8)), packed = this.u16(descriptor + 18), count = packed & 0x1ff + if (!height || baseline < 0 || !count || ((packed >>> 9) & 15) !== 4 || (packed >>> 14) !== 3) throw new Error('Unsupported LVGL font') + this.metrics = new Uint8Array([height, baseline]) + for (let i = 0; i < count; i++) { + const at = cmaps + i * 20, start = this.u32(at), length = this.u16(at + 4), first = this.u16(at + 6) + const unicodePtr = this.u32(at + 8), offsetsPtr = this.u32(at + 12), listLength = this.u16(at + 16), type = this.u8(at + 18) + if (type === 2 || type === 0) { + const offsets = type === 0 ? this.pointer(offsetsPtr) : 0 + for (let n = 0; n < length; n++) { + const offset = type === 0 ? this.u8(offsets + n) : n + if (n && !offset) continue + this.ids.set(start + n, first + offset) + } + } else if (type === 1 || type === 3) { + const unicode = this.pointer(unicodePtr), offsets = offsetsPtr ? this.pointer(offsetsPtr) : -1 + for (let n = 0; n < listLength; n++) { + const offset = offsets < 0 ? n : type === 1 ? this.u16(offsets + n * 2) : this.u8(offsets + n) + if (n && !offset) continue + this.ids.set(start + this.u16(unicode + n * 2), first + offset) + } + } else throw new Error('Unsupported LVGL cmap') + } + } + glyph(codePoint: number): Uint8Array { + const id = this.ids.get(codePoint) + if (!id) return new Uint8Array() + const at = this.descriptors + id * 16, bitmap = this.bitmaps + this.u32(at) + const width = this.u16(at + 8), height = this.u16(at + 10), length = width && height ? (Math.floor(width / 2) + 1) * height : 0 + this.check(bitmap, length) + const result = new Uint8Array(8 + length) + result.set(this.bytes.subarray(at + 8, at + 16)); result.set(this.bytes.subarray(bitmap, bitmap + length), 8) + return result + } + private check(at: number, size: number): void { if (at < 0 || at + size > this.bytes.length) throw new Error('Truncated LVGL font') } + private pointer(address: number): number { const at = address - 0x80100000; this.check(at, 1); return at } + private u8(at: number): number { this.check(at, 1); return this.bytes[at] } + private u16(at: number): number { return this.view.getUint16(at, true) } + private u32(at: number): number { return this.view.getUint32(at, true) } +} diff --git a/app/native/lvgl-font.ios.ts b/app/native/lvgl-font.ios.ts new file mode 100644 index 00000000..99dbdd01 --- /dev/null +++ b/app/native/lvgl-font.ios.ts @@ -0,0 +1,16 @@ +import { LvglFont } from '../graphics/lvgl-font' +import { readBinaryFile } from './file-access' +const cache = new Map() +function load(path: string): LvglFont { + let font = cache.get(path) + if (!font) { + const bytes = readBinaryFile(path) + if (!bytes) throw new Error('Cannot read LVGL font') + font = new LvglFont(bytes) + if (cache.size >= 2) cache.delete(cache.keys().next().value!) + cache.set(path, font) + } + return font +} +export function lvglMetrics(path: string): Uint8Array { return load(path).metrics } +export function lvglGlyph(path: string, codePoint: number): Uint8Array { return load(path).glyph(codePoint) } diff --git a/app/native/lvgl-font.ts b/app/native/lvgl-font.ts new file mode 100644 index 00000000..9b403e91 --- /dev/null +++ b/app/native/lvgl-font.ts @@ -0,0 +1,4 @@ +import { toUint8Array } from '../util/array-util' +declare const com: any +export function lvglMetrics(path: string): Uint8Array { return toUint8Array(com.faceclaw.app.LvglFontFile.getMetrics(path)) } +export function lvglGlyph(path: string, codePoint: number): Uint8Array { return toUint8Array(com.faceclaw.app.LvglFontFile.getGlyph(path, codePoint)) } diff --git a/scripts/ios-config.md b/scripts/ios-config.md new file mode 100644 index 00000000..e53b6ac7 --- /dev/null +++ b/scripts/ios-config.md @@ -0,0 +1,53 @@ +# iOS config transfer + +Requires Xcode, Python 3, a development build with `FaceclawConfigPort`, and an +unlocked/trusted iPhone. Both commands restart Faceclaw, briefly interrupting its +Bluetooth and Terminal sessions. + +```sh +scripts/pull_config_ios.sh # faceclaw_settings.ios.json +scripts/push_config_ios.sh # import the edited file +scripts/pull_config_ios.sh -f /tmp/settings.json --device IPHONE_UDID +scripts/push_config_ios.sh faceclaw_settings.xml # Android pull_config.sh output +``` + +Use `--device` / `-d` or set `IOS_DEVICE` when more than one device is connected. +Without either, the script selects the only device with an active Xcode tunnel. +For the simulator, add `--simulator` (defaults to `booted`; `--device` selects one). + +The JSON format preserves setting types: + +```json +{ + "schema": 1, + "settings": { + "terminal.connections": "[]", + "terminal.autoReconnect": true + } +} +``` + +Pull accepts an `.xml` output filename to write Android shared-preferences XML. +Push detects JSON or XML by content and supports strings, booleans and numbers. +Malformed input, unsupported values and files over 2 MiB fail before restarting +the app. Push **merges supplied keys**, preserving omitted settings (including +phone-specific pairing). To clear a value, supply its empty/default value. + +Transfers use `devicectl` app-container access. The app consumes a uniquely +identified request at bootstrap, before loading settings getters and workers; +it applies imports through NSUserDefaults rather than overwriting a plist that +cfprefsd could restore from cache. Each successful push first saves the previous +preferences to `Library/FaceclawConfigPort/previous.plist`. Replies are checked +against the request ID, so a stale export cannot appear to confirm an import. + +Exports may contain API keys and Terminal tokens. Scripts do not print values, +write local exports with mode 600, and remove their private temporary directory +on exit. Default export filenames are gitignored. Device request/reply/backup +files stay in the private Library directory, outside Files/Finder Documents. + +Terminal's configured `g2mirror://` scheme uses plain WebSockets; `g2mirrors://` +uses TLS. The iOS build declares local networking and an explicit ATS exception +for the current development server `100.68.94.67`. On iOS 17+, another literal IP +requires its own build-time `NSExceptionDomains` entry, or a TLS endpoint. The +exception does not disable TLS certificate verification. See Apple's +[IP exception rules](https://developer.apple.com/documentation/bundleresources/information-property-list/nsapptransportsecurity/nsexceptiondomains). diff --git a/scripts/ios_config.py b/scripts/ios_config.py new file mode 100644 index 00000000..f5962a39 --- /dev/null +++ b/scripts/ios_config.py @@ -0,0 +1,197 @@ +#!/usr/bin/env python3 +"""Transfer Faceclaw preferences using Xcode's supported app-container APIs.""" +import argparse +import json +import math +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import time +import uuid +import xml.etree.ElementTree as ET + +BUNDLE = 'com.faceclaw.app' +REMOTE = 'Library/FaceclawConfigPort' +LIMIT = 2 * 1024 * 1024 + + +def validate(settings): + if not isinstance(settings, dict) or len(settings) > 4096: + raise ValueError('Expected a settings object with at most 4096 entries') + for key, value in settings.items(): + if not isinstance(key, str) or not key or len(key) > 512: + raise ValueError('Invalid setting name') + if type(value) not in (str, bool, int, float) or isinstance(value, float) and not math.isfinite(value): + raise ValueError('Settings must contain only strings, booleans and finite numbers') + return settings + + +def read_config(path): + data = Path(path).read_bytes() + if len(data) > LIMIT: + raise ValueError('Config exceeds 2 MiB') + if data.lstrip().startswith(b'<'): + if b'') + settings = {} + for item in root: + key = item.get('name') + if key in settings: + raise ValueError('Duplicate setting name') + if item.tag == 'string': + value = item.text or '' + elif item.tag == 'boolean' and item.get('value') in ('true', 'false'): + value = item.get('value') == 'true' + elif item.tag in ('int', 'long', 'float'): + try: + value = float(item.get('value', '')) if item.tag == 'float' else int(item.get('value', '')) + except ValueError: + raise ValueError('Invalid numeric preference value') from None + else: + raise ValueError('Unsupported Android preference type') + if len(item): + raise ValueError('Nested preference content is not supported') + settings[key] = value + else: + config = json.loads(data) + if not isinstance(config, dict) or config.get('schema') != 1: + raise ValueError('Expected Faceclaw config with schema: 1 and a settings object') + settings = config.get('settings') + return validate(settings) + + +def encode_config(settings, xml=False): + validate(settings) + if not xml: + return (json.dumps({'schema': 1, 'settings': settings}, indent=2, ensure_ascii=False, allow_nan=False) + '\n').encode() + root = ET.Element('map') + for key, value in sorted(settings.items()): + if isinstance(value, str): + ET.SubElement(root, 'string', name=key).text = value + else: + kind = 'boolean' if isinstance(value, bool) else 'long' if isinstance(value, int) else 'float' + ET.SubElement(root, kind, name=key, value=str(value).lower()) + ET.indent(root) + return ET.tostring(root, encoding='utf-8', xml_declaration=True) + b'\n' + + +def run(args, tolerate=False): + try: + result = subprocess.run(['xcrun', *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30) + except subprocess.TimeoutExpired: + if tolerate: + return subprocess.CompletedProcess(args, 1, '', 'Xcode device command timed out') + raise RuntimeError('Xcode device command timed out; check that the phone is connected and unlocked') from None + if result.returncode and not tolerate: + raise RuntimeError(result.stderr.strip() or result.stdout.strip() or 'Xcode device command failed') + return result + + +def atomic_write(path, data): + path = Path(path).absolute() + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as output: + temporary = Path(output.name) + try: + os.chmod(temporary, 0o600) + output.write(data) + output.flush() + os.fsync(output.fileno()) + os.replace(temporary, path) + finally: + temporary.unlink(missing_ok=True) + + +def transfer(args): + # Parse and validate before touching the device or restarting its app. + settings = read_config(args.file) if args.operation == 'push' else None + with tempfile.TemporaryDirectory(prefix='faceclaw-config-') as work: + work = Path(work) + device = args.device or os.environ.get('IOS_DEVICE') + if args.simulator: + device = device or 'booted' + container = Path(run(['simctl', 'get_app_container', device, BUNDLE, 'data']).stdout.strip()) + else: + if not device: + listing = work / 'devices.json' + run(['devicectl', 'list', 'devices', '--json-output', str(listing)]) + devices = json.loads(listing.read_text())['result']['devices'] + available = [d for d in devices if d.get('connectionProperties', {}).get('tunnelState') == 'connected'] + if len(available) != 1: + raise ValueError('Specify the iPhone with --device UDID (or IOS_DEVICE)') + device = available[0]['identifier'] + identifier = str(uuid.uuid4()) + payload = {'id': identifier, 'operation': args.operation} + if settings is not None: + payload['settings'] = settings + encoded = json.dumps(payload, ensure_ascii=False, allow_nan=False).encode() + if len(encoded) > LIMIT: + raise ValueError('Config request exceeds 2 MiB') + stage = work / 'FaceclawConfigPort' + stage.mkdir(mode=0o700) + atomic_write(stage / 'request.json', encoded) + if args.simulator: + remote = container / REMOTE + remote.mkdir(parents=True, exist_ok=True) + atomic_write(remote / 'request.json', encoded) + run(['simctl', 'terminate', device, BUNDLE], tolerate=True) + run(['simctl', 'launch', device, BUNDLE]) + else: + run(['devicectl', 'device', 'copy', 'to', '--device', device, '--domain-type', 'appDataContainer', + '--domain-identifier', BUNDLE, '--source', str(stage), '--destination', REMOTE]) + run(['devicectl', 'device', 'process', 'launch', '--device', device, '--terminate-existing', BUNDLE]) + deadline = time.monotonic() + 25 + response = None + while time.monotonic() < deadline: + if args.simulator: + response_path = remote / 'response.json' + else: + response_path = work / 'response.json' + run(['devicectl', 'device', 'copy', 'from', '--device', device, '--domain-type', 'appDataContainer', + '--domain-identifier', BUNDLE, '--source', REMOTE + '/response.json', '--destination', str(response_path)], tolerate=True) + try: + candidate = json.loads(response_path.read_text()) + if candidate.get('id') == identifier: + response = candidate + break + except (OSError, ValueError): + pass + time.sleep(0.5) + if response is None: + raise RuntimeError('No matching config response. Unlock the phone and install a build with config-port support, then retry.') + if not response.get('ok'): + raise RuntimeError(response.get('message', 'Config transfer failed')) + if args.operation == 'pull': + settings = response['config']['settings'] + atomic_write(args.file, encode_config(settings, Path(args.file).suffix.lower() == '.xml')) + print(f'Pulled {len(settings)} settings to {args.file} (mode 600). App restarted.') + else: + print(f'Pushed {response["count"]} settings; app restarted. Unspecified settings preserved; previous preferences backed up in {REMOTE}/previous.plist.') + + +def main(): + os.umask(0o077) + parser = argparse.ArgumentParser(description='Pull/push iOS Faceclaw config; restarts the app. Requires Xcode and an unlocked development device. Push merges supplied keys; accepts Android XML or Faceclaw JSON.') + parser.add_argument('operation', choices=['pull', 'push']) + parser.add_argument('path', nargs='?') + parser.add_argument('-f', '--file') + parser.add_argument('-d', '--device', help='iPhone UDID/name, or simulator ID with --simulator; defaults to IOS_DEVICE or the only connected device') + parser.add_argument('--simulator', action='store_true', help='Use simctl (default device: booted)') + args = parser.parse_args() + if args.path and args.file: + parser.error('Specify one file, either positional or with -f') + args.file = args.file or args.path or 'faceclaw_settings.ios.json' + try: + transfer(args) + except (OSError, ValueError, RuntimeError, ET.ParseError) as error: + print(f'Error: {error}', file=sys.stderr) + return 1 + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/scripts/pull_config_ios.sh b/scripts/pull_config_ios.sh new file mode 100755 index 00000000..65a482c4 --- /dev/null +++ b/scripts/pull_config_ios.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +set -euo pipefail +exec python3 "$(cd "$(dirname "$0")" && pwd)/ios_config.py" pull "$@" diff --git a/scripts/push_config_ios.sh b/scripts/push_config_ios.sh new file mode 100755 index 00000000..f3cc5bd0 --- /dev/null +++ b/scripts/push_config_ios.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +set -euo pipefail +exec python3 "$(cd "$(dirname "$0")" && pwd)/ios_config.py" push "$@" diff --git a/tests/ios-evenhub-native.cjs b/tests/ios-evenhub-native.cjs new file mode 100644 index 00000000..209caffc --- /dev/null +++ b/tests/ios-evenhub-native.cjs @@ -0,0 +1,42 @@ +// Run the production WKWebView host in an isolated simulator app, no glasses. +const { execFileSync } = require('node:child_process'); +const fs = require('node:fs'), path = require('node:path'), os = require('node:os'); +const { loader } = require('./helpers/load-typescript.cjs'); +const root = path.resolve(__dirname, '..'), work = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-evenhub-probe-')); +const bundle = path.join(work, 'EvenHubProbe.app'), dist = path.join(bundle, 'dist'); +fs.mkdirSync(dist, { recursive: true }); +const source = fs.readFileSync(path.join(root, 'app/apps/evenhub/session.ts'), 'utf8'); +const shim = source.match(/export const EVENHUB_BRIDGE_INJECT_SCRIPT = `([\s\S]*?)`;/)[1]; +const adapter = loader({}, { './session': {}, '../../version': {} })('app/apps/evenhub/webview.ios.ts').IOS_EVENHUB_TRANSPORT_SCRIPT; +fs.writeFileSync(path.join(bundle, 'bridge.js'), adapter + shim); +fs.writeFileSync(path.join(bundle, 'secret.txt'), 'must not be served'); +fs.writeFileSync(path.join(dist, 'index.html'), ``); +fs.writeFileSync(path.join(dist, 'app.js'), ` +import { value } from './module.js'; +const call = name => flutter_inappwebview.callHandler(name); +if (value === 42) await call('module'); +const previous = localStorage.getItem('probe'); localStorage.setItem('probe','saved'); await call(previous === 'saved' ? 'restored' : 'stored'); +if ((await (await fetch('/data.json')).json()).ok) await call('fetch'); +const image = new Image(); image.onload = () => call('image'); image.src = '/pixel.png'; +setTimeout(() => call('timer'), 40); requestAnimationFrame(() => call('raf')); +await WebAssembly.instantiate(new Uint8Array([0,97,115,109,1,0,0,0])); await call('wasm'); +if (!(await fetch('/%2e%2e/secret.txt')).ok) await call('traversal'); +`); +fs.writeFileSync(path.join(dist, 'module.js'), 'export const value = 42;'); +fs.writeFileSync(path.join(dist, 'data.json'), '{"ok":true}'); +fs.writeFileSync(path.join(dist, 'pixel.png'), Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aBVkAAAAASUVORK5CYII=', 'base64')); +fs.writeFileSync(path.join(bundle, 'Info.plist'), `CFBundleIdentifiercom.faceclaw.evenhub-probeCFBundleExecutableEvenHubProbeCFBundleNameEvenHubProbeCFBundlePackageTypeAPPLCFBundleVersion1CFBundleShortVersionString1UILaunchScreenLSRequiresIPhoneOS`); +const sdk = execFileSync('xcrun', ['--sdk', 'iphonesimulator', '--show-sdk-path'], { encoding: 'utf8' }).trim(); +execFileSync('xcrun', ['clang', '-fobjc-arc', '-target', `${process.arch === 'arm64' ? 'arm64' : 'x86_64'}-apple-ios15.1-simulator`, '-isysroot', sdk, + '-framework', 'UIKit', '-framework', 'Foundation', '-framework', 'CoreGraphics', '-framework', 'WebKit', '-framework', 'UniformTypeIdentifiers', + '-I', 'App_Resources/iOS/src', 'tests/native/ios-evenhub-test.m', 'App_Resources/iOS/src/FaceclawEvenHubWebView.m', '-o', path.join(bundle, 'EvenHubProbe')], { cwd: root, stdio: 'inherit' }); +const device = process.argv[2] || 'booted'; +execFileSync('xcrun', ['simctl', 'install', device, bundle], { stdio: 'inherit' }); +execFileSync('xcrun', ['simctl', 'launch', '--terminate-running-process', device, 'com.faceclaw.evenhub-probe'], { stdio: 'inherit' }); +const container = execFileSync('xcrun', ['simctl', 'get_app_container', device, 'com.faceclaw.evenhub-probe', 'data'], { encoding: 'utf8' }).trim(); +const resultFile = path.join(container, 'Documents/result.json'); +setTimeout(() => { + const result = JSON.parse(fs.readFileSync(resultFile, 'utf8')); + console.log(JSON.stringify({ work, ...result })); + if (!result.passed) process.exitCode = 1; +}, 14000); diff --git a/tests/ios-evenhub.test.cjs b/tests/ios-evenhub.test.cjs new file mode 100644 index 00000000..69953879 --- /dev/null +++ b/tests/ios-evenhub.test.cjs @@ -0,0 +1,156 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'), vm = require('node:vm'); +const { createHash } = require('node:crypto'); +const { zstdCompressSync } = require('node:zlib'); +const { loader } = require('./helpers/load-typescript.cjs'); +const { LvglFont } = require('../.test-build/app/graphics/lvgl-font.js'); + +function pack(files) { + const key = Buffer.from('EVEN REALITIES'); + const xor = b => Buffer.from(b.map((v, i) => v ^ key[i % key.length])); + const header = Buffer.alloc(20); header.write('EHPK'); header.writeUInt32LE(20, 8); + return Buffer.concat([header, ...Object.entries(files).map(([name, value]) => { + const text = Buffer.from(value), body = zstdCompressSync(text), path = Buffer.from(name), record = Buffer.alloc(16); + record.writeUInt32LE(0xbaa9bae4); record.writeUInt32LE(body.length, 4); record.writeUInt32LE(text.length, 8); record.writeUInt16LE(path.length, 14); + return Buffer.concat([record, xor(path), xor(body)]); + })]); +} + +test('iOS LVGL reader loads the bundled font including CJK and game symbols', () => { + const bytes = new Uint8Array(fs.readFileSync('app/fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin')); + const font = new LvglFont(bytes); + // Metrics and full glyph SHA-256 vectors from production Java LvglFontFile. + assert.deepEqual([...font.metrics], [25, 19]); + const vectors = [ + [0x25a6, 'b08ecb603fd059b0a00ba3088f18b79185c421e73049797cd3cb0828e1e5cd04'], + [0x25c6, 'c176d5dcdb5f7e3a7220455528efde3760672c40cee39b422fc3f5ad20df66dd'], + [0x4e2d, '9bde2c8f1ffcf7afc598f4d0350c9ff262484816aa0c9b46d31f0c3c09613003'], + [0xff21, '0074f96299a75b86912282416e47e0823790e091352aec852eff49b89b2e719d'], + ]; + for (const [cp, hash] of vectors) { + const glyph = font.glyph(cp); + const w = glyph[0] | glyph[1] << 8, h = glyph[2] | glyph[3] << 8; + assert.ok(w > 0 && h > 0); assert.equal(glyph.length, 8 + (Math.floor(w / 2) + 1) * h); + assert.equal(createHash('sha256').update(glyph).digest('hex'), hash); + } + assert.equal(font.glyph(0x10ffff).length, 0); + assert.throws(() => new LvglFont(bytes.subarray(0, 80)), /bounds|Truncated/); +}); + +test('EHPK entrypoints and extraction paths cannot escape their package directory', () => { + const load = loader({}, { fzstd: require('fzstd') }); + const api = load('app/apps/evenhub/ehpk.ts'); + assert.equal(api.safeEhpkPath('dist/assets/game.js'), true); + for (const path of ['../settings.json', '/private/file', 'dist/../../file', 'dist\\file', 'dist//x', '.', '..', 'x\0y', 'https://host/app']) { + assert.equal(api.safeEhpkPath(path), false); + assert.throws(() => api.parseManifest(JSON.stringify({ entrypoint: path })), /entrypoint/); + assert.throws(() => api.parseEhpk(pack({ [path]: 'test' })), /invalid file path/); + } + const archive = api.parseEhpk(pack({ 'app.json': '{"package_id":"test.app"}', 'dist/index.html': 'test' })); + assert.ok(archive.files.has('dist/index.html')); +}); + +test('Files on iOS exposes Run app for EHPKs and passes the selected local path', () => { + let browserOptions, actions, opened; + const load = loader({ global: { isIOS: true } }, { + '../../native/file-access': {}, '../../native/font-files': { isFontFile: () => false }, + '../../graphics/installed-fonts': {}, '../../native/image-files': { isDecodableImageFile: () => false }, + '../evenhub/installed-apps': { readEvenHubPackageManifest: () => null }, '../evenhub/permission-dialog': {}, + './file-browser': { FileBrowserLayer: class { constructor(options) { browserOptions = options; } } }, + './file-info-dialog': { FileInfoDialogLayer: class { constructor(entry, value) { actions = value; } } }, + './font-previewer': {}, './image-viewer': {}, './text-viewer': {}, + '../../ui/shell/in-process-window': { createInProcessWindow: () => ({}) }, '../../ui/shell/shell': {}, + }); + load('app/apps/files/files-app.ts').createFilesAppWindow({ openEhpkApp: path => { opened = path; } }); + assert.equal(browserOptions.isSupportedFile('test.EHPK'), true); + const ctx = { stack: { push() {}, pop() {} } }; + browserOptions.onFilePicked({ name: 'test.ehpk', path: '/Documents/test.ehpk' }, ctx); + assert.deepEqual(Array.from(actions, a => a.label), ['Run app']); + actions[0].onSelect(ctx); assert.equal(opened, '/Documents/test.ehpk'); +}); + +test('local package runtimes unpack independently and clean up after closing or a failed launch', async () => { + const files = new Map(), sessions = []; + const manifest = { package_id: 'test.app', name: 'Test' }; + let archive = pack({ 'app.json': JSON.stringify(manifest), 'dist/index.html': 'test' }); + class Session { constructor(manifest, distDir) { this.manifest = manifest; this.distDir = distDir; sessions.push(this); } attachWebView(handle) { this.handle = handle; } close() { this.handle.destroy(); } } + const load = loader({ global: { isIOS: true } }, { + '@nativescript/core': { Application: {} }, fzstd: require('fzstd'), + '../../native/file-access': { appFilesDirPath: () => '/private/app', readBinaryFile: () => archive, + writeBinaryFile: (path, bytes) => { files.set(path, bytes); return true; }, + deletePathRecursively: path => { for (const key of files.keys()) if (key.startsWith(path + '/')) files.delete(key); } }, + './session': { EvenHubSession: Session }, './evenhub-window': { createEvenHubWindow: () => ({}) }, + './webview': { createEvenHubWebView: () => ({ evaluateJs() {}, destroy() {} }) }, + '../../ui/shell/shell': {}, './installed-apps': {}, + }); + const manager = load('app/apps/evenhub/manager.ts'), ctx = { appendLog() {}, launchInProcessApp: async () => {} }; + await manager.launchPackage(ctx, '/test.ehpk'); await manager.launchPackage(ctx, '/test.ehpk'); + assert.notEqual(sessions[0].distDir, sessions[1].distDir); assert.equal(manager.runningAppCount(), 2); + sessions[0].close(); assert.equal(manager.runningAppCount(), 1); + assert.ok(files.has(sessions[1].distDir + '/index.html')); assert.ok(!files.has(sessions[0].distDir + '/index.html')); + sessions[1].close(); assert.equal(files.size, 0); + await assert.rejects(manager.launchPackage({ ...ctx, launchInProcessApp: async () => { throw new Error('test launch failed'); } }, '/test.ehpk'), /test launch failed/); + assert.equal(files.size, 0); assert.equal(manager.runningAppCount(), 0); + archive = pack({ 'app.json': JSON.stringify(manifest) }); + await assert.rejects(manager.launchPackage(ctx, '/bad.ehpk'), /entrypoint is missing/); + assert.equal(files.size, 0); +}); + +function sessionHarness() { + const settings = new Map(), logs = [], renders = []; + const load = loader({ global: { isIOS: true }, setTimeout, clearTimeout, Promise }, { + '@nativescript/core': { ApplicationSettings: { getString: (k, d) => settings.get(k) ?? d, setString: (k, v) => settings.set(k, v) } }, + 'upng-js': require('upng-js'), + '../../graphics/evenhub-font': { EvenHubFont: { get: () => ({ drawText() {}, drawTextWrapped() {}, lineHeight: 20 }) } }, + '../../ui/dashboard-settings': {}, '../../ui/sound-effects': {}, './api-key-dialog': {}, + './mic-router': { evenHubMicRouter: {} }, './imu-router': { evenHubImuRouter: {} }, + './compass-router': { evenHubCompassRouter: {} }, '../../assistant/tool-registry': {}, + '../../native/location': {}, '../../native/location-tracker': {}, '../../g2/android-permissions': {}, + }); + const api = load('app/apps/evenhub/session.ts'); + const session = new api.EvenHubSession({ name: 'Probe', packageId: 'test.probe', permissions: [] }, '/test/dist', line => logs.push(line)); + session.attachWindow({ requestRender: () => renders.push(1), windowId: 'probe', closeWindow() {} }); + const web = vm.createContext({ Promise, Map, Date, console }); + web.window = web; + web.__faceclawEvenHub = { postMessage: (name, args, id) => session.handleBridgeCall(name, args, id) }; + let destroyed = false; + session.attachWebView({ evaluateJs: script => vm.runInContext(script, web), destroy: () => { destroyed = true; } }); + vm.runInContext(api.EVENHUB_BRIDGE_INJECT_SCRIPT, web); + const call = (method, data = {}) => web.flutter_inappwebview.callHandler('evenAppMessage', JSON.stringify({ method, data })); + return { session, call, web, logs, renders, destroyed: () => destroyed }; +} + +test('injected EvenHub RPC creates, updates and composites an image page with no Android globals', async () => { + const h = sessionHarness(); + assert.equal(await h.call('createStartUpPageContainer', { containerTotalNum: 1, + imageObject: [{ containerID: 1, containerName: 'board', xPosition: 0, yPosition: 0, width: 2, height: 2 }] }), 0); + assert.equal(await h.call('updateImageRawData', { containerID: 1, containerName: 'board', imageData: [0, 80, 160, 255] }), 0); + const painted = h.session.paint({ width: 576, height: 288 }, true); + assert.deepEqual([painted.pixels[0], painted.pixels[1], painted.pixels[576], painted.pixels[577]], [0, 80, 160, 255]); + assert.equal(await h.call('setLocalStorage', { key: 'score', value: '42' }), true); + assert.equal(await h.call('getLocalStorage', { key: 'score' }), '42'); + const events = []; + h.web._listenEvenAppMessage = value => events.push(typeof value === 'string' ? JSON.parse(value) : value); + h.session.handleGesture({ type: 'click', source: 'ring' }); + assert.ok(events.some(e => e.method === 'evenHubEvent')); + assert.equal(await h.call('audioControl', { isOpen: true }), false); + assert.equal(await h.call('imuControl', { isOpen: true }), false); + assert.equal(await h.call('getAppLocation'), null); + h.session.close(); await new Promise(resolve => setTimeout(resolve, 120)); assert.equal(h.destroyed(), true); +}); + +test('iOS webview adapter defers loading until handles attach and destroys a not-yet-loaded app', () => { + const timers = new Map(), calls = []; + const native = { startEntrypointScript: (...args) => calls.push(args), destroy: () => calls.push('destroy') }; + const load = loader({ FaceclawEvenHubWebView: { new: () => native }, + setTimeout: fn => { timers.set(1, fn); return 1; }, clearTimeout: id => timers.delete(id) }, { + './session': { EVENHUB_BRIDGE_INJECT_SCRIPT: 'BRIDGE', buildFaceclawExtensionsScript: () => 'EXT' }, '../../version': { FACECLAW_VERSION: 'test' }, + }); + const { createEvenHubWebView } = load('app/apps/evenhub/webview.ios.ts'); + const session = { manifest: { name: 'Probe', entrypoint: 'index.html' }, distDir: '/app/dist' }; + const host = createEvenHubWebView(session); assert.equal(calls.length, 0); + timers.get(1)(); assert.equal(calls[0][0], '/app/dist'); assert.match(calls[0][2], /postMessage/); assert.match(calls[0][2], /BRIDGEEXT$/); + host.destroy(); assert.equal(timers.size, 0); assert.equal(calls.at(-1), 'destroy'); + assert.throws(() => createEvenHubWebView({ ...session, remoteUrl: 'https://example.test' }), /local/); +}); diff --git a/tests/native/ios-evenhub-test.m b/tests/native/ios-evenhub-test.m new file mode 100644 index 00000000..83c3230e --- /dev/null +++ b/tests/native/ios-evenhub-test.m @@ -0,0 +1,57 @@ +#import +#import "FaceclawEvenHubWebView.h" + +@interface Probe : UIResponder +@property(nonatomic) UIWindow *window; +@property(nonatomic) FaceclawEvenHubWebView *host; +@property(nonatomic) NSMutableSet *seen; +@property(nonatomic) NSInteger phase; +@end +@implementation Probe +- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options { + self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; + self.window.rootViewController = [UIViewController new]; + self.window.rootViewController.view.backgroundColor = UIColor.whiteColor; + [self.window makeKeyAndVisible]; + self.seen = [NSMutableSet new]; self.host = [FaceclawEvenHubWebView new]; self.phase = 1; + NSString *package = NSUUID.UUID.UUIDString; + self.host.packageIdentifier = package; + __weak Probe *weakSelf = self; + self.host.eventHandler = ^(NSString *json) { + NSLog(@"EHPROBE %@", json); + NSDictionary *event = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; + if ([event[@"kind"] isEqual:@"call"]) { + [weakSelf.seen addObject:event[@"name"]]; + [weakSelf.seen addObject:[NSString stringWithFormat:@"%ld:%@", (long)weakSelf.phase, event[@"name"]]]; + [weakSelf.host evaluate:[NSString stringWithFormat:@"window.__fcResolve(%@,true,true)", event[@"id"]]]; + } + }; + NSString *root = NSBundle.mainBundle.resourcePath; + NSString *script = [NSString stringWithContentsOfFile:[root stringByAppendingPathComponent:@"bridge.js"] encoding:NSUTF8StringEncoding error:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self.host start:[root stringByAppendingPathComponent:@"dist"] entrypoint:@"index.html" script:script]; + }); + for (NSInteger phase = 2; phase <= 3; phase++) { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (phase - 1) * 4 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + void (^handler)(NSString *) = self.host.eventHandler; + [self.host showOnPhone]; [self.host hideOnPhone]; [self.host destroy]; + self.phase = phase; self.host = [FaceclawEvenHubWebView new]; + self.host.eventHandler = handler; + self.host.packageIdentifier = phase == 2 ? package : [package stringByAppendingString:@".other"]; + [self.host start:[root stringByAppendingPathComponent:@"dist"] entrypoint:@"index.html" script:script]; + }); + } + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 12 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + NSArray *required = @[@"early", @"module", @"fetch", @"image", @"timer", @"raf", @"wasm", @"traversal", @"1:stored", @"2:restored", @"3:stored"]; + BOOL passed = [[NSSet setWithArray:required] isSubsetOfSet:self.seen]; + [self.host showOnPhone]; [self.host hideOnPhone]; [self.host destroy]; + NSDictionary *result = @{@"passed": @(passed), @"seen": self.seen.allObjects}; + NSData *data = [NSJSONSerialization dataWithJSONObject:result options:0 error:nil]; + NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; + [data writeToFile:[documents stringByAppendingPathComponent:@"result.json"] atomically:YES]; + NSLog(@"EHPROBE RESULT %@", result); + }); + return YES; +} +@end +int main(int argc, char **argv) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass(Probe.class)); } } diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 068ece2a..655701c7 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -12,6 +12,7 @@ }, "include": [ "../app/g2/ble-protocol.ts", + "../app/graphics/lvgl-font.ts", "../app/phone-ui/ble-bandwidth-meter.ts", "../app/apps/terminal/hub-header.ts", "../app/g2/ios-peripheral-identity.ts",