forked from Blendman974/faceclaw
Refactor iOS version to use a bunch more shared code
This commit is contained in:
1 parent
c3a0ba9f56
commit
4a194e37bb
40 files changed
+1874
-2605
No files matched your search
@@ -6,7 +6,6 @@ const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
const images = require('../.test-build/app/graphics/image.js');
|
||||
const { SurfaceCompositor } = require('../.test-build/app/graphics/surface-compositor.js');
|
||||
const protocol = require('../.test-build/app/g2/ble-protocol.js');
|
||||
const events = require('../.test-build/app/g2/events.js');
|
||||
|
||||
function load(file, modules, globals = {}) {
|
||||
@@ -24,8 +23,8 @@ const timings = load('app/native/frame-timings.ts', {});
|
||||
|
||||
function fixture() {
|
||||
const observers = new Map(), phoneState = { protectedDataAvailable: true };
|
||||
let settingsChanged, remoteHost;
|
||||
let now = 1000, nextTask = 0, screenOn = true, shellOptions, session;
|
||||
let settingsChanged, remoteHost, controller, bridge = null;
|
||||
let now = 1000, nextTask = 0, screenOn = true, shellOptions;
|
||||
const tasks = new Map(), sent = [], previews = [], received = [], errors = [];
|
||||
const boardStats = { starts: 0, stops: 0, paints: 0 };
|
||||
const settings = { lock: true, enabled: true, tap: true, hold: true, tilt: true, duration: 3000 };
|
||||
@@ -39,8 +38,10 @@ function fixture() {
|
||||
'../util/render-freshness': { beginRenderPass() {}, endRenderPass: () => false },
|
||||
'./glance-state': require('../.test-build/app/g2/glance-state.js'),
|
||||
}, clock);
|
||||
const launcherPixels = new Uint8Array(640 * 480).fill(75), fullRect = { x: 0, y: 0, width: 640, height: 480 };
|
||||
const window = { windowId: 'launcher', surfaceId: 'launcher', appId: 'launcher', title: 'Apps',
|
||||
requestRender() {}, setScreenOn() {} };
|
||||
// The launcher repaints into whichever display target the controller has after a switch.
|
||||
requestRender() { void controller?.displayReady.then(() => controller.display?.submitSurfaceFrame('launcher', launcherPixels, fullRect, 'launcher')); }, setScreenOn() {} };
|
||||
const shell = {
|
||||
configure: options => { shellOptions = options; }, registerWindow() {}, focusWindow() {},
|
||||
wake: () => { screenOn = true; shellOptions.onScreenStateChanged(true); },
|
||||
@@ -54,15 +55,43 @@ function fixture() {
|
||||
if (input.type === 'display-wake' || !screenOn && input.type === 'double-click') shell.wake();
|
||||
},
|
||||
};
|
||||
class Session {
|
||||
state = { phase: 'disconnected' };
|
||||
constructor(_transport, onState, onInput, _log, _activity, _compass, onWear) { session = this; this.onState = onState; this.onInput = onInput; this.onWear = onWear; }
|
||||
async enableWearDetectionAndRequestState() {}
|
||||
async start() { this.state = { phase: 'connected' }; this.onState(this.state); }
|
||||
async stop() { this.state = { phase: 'disconnected' }; this.onState(this.state); }
|
||||
async setBrightness() {}
|
||||
setFrame(pixels) { if (this.state.phase === 'connected') sent.push(pixels); }
|
||||
wake() {}
|
||||
// Both display targets compose with the real (TypeScript reference) compositor so
|
||||
// the pixel assertions below exercise real layering, blanking and lock surfaces.
|
||||
class Display {
|
||||
compositor = new SurfaceCompositor(640, 480);
|
||||
async configureCompositorScreen() {}
|
||||
async configureSurface(id, options) { this.compositor.configureSurface(id, options); }
|
||||
async removeSurface(id) { this.compositor.removeSurface(id); this.changed(); }
|
||||
async setSurfaceVisible(id, visible) { this.compositor.setSurfaceVisible(id, visible); this.changed(); }
|
||||
async setUnderlayDim(below, factor) { this.compositor.setUnderlayDim(below, factor); this.changed(); }
|
||||
async setScreenBlanked(blanked) { this.compositor.setScreenBlanked(blanked); this.changed(); }
|
||||
async submitSurfaceFrame(id, pixels, rect, _fingerprint, _paintMs, _frameId, draws = null) { this.compositor.submitSurfaceFrame(id, pixels, rect, draws); this.changed(); }
|
||||
async submitShellScene(bytes) { this.compositor.setShellScene(bytes); this.changed(); }
|
||||
getCompositePreview() { return this.compositor.composite(); }
|
||||
changed() {}
|
||||
}
|
||||
class PreviewDisplayTarget extends Display {
|
||||
activate(fn) { this.onFrame = fn; } release() { this.onFrame = null; }
|
||||
changed() { this.onFrame?.(); }
|
||||
waitForFrameFinished() { return Promise.resolve('composited'); }
|
||||
}
|
||||
const noop = () => () => {};
|
||||
class FaceclawCommunicatorBridge extends Display {
|
||||
phase = 'disconnected'; listeners = {};
|
||||
constructor() { super(); bridge = this; }
|
||||
on(kind, fn) { this.listeners[kind] = fn; return () => { if (this.listeners[kind] === fn) delete this.listeners[kind]; }; }
|
||||
onStateChange(fn) { return this.on('state', fn); } onRingEvent(fn) { return this.on('ring', fn); }
|
||||
onWearState(fn) { return this.on('wear', fn); } onBatteryState() { return noop(); } onFirmwareInfo() { return noop(); }
|
||||
onFrameMetrics() { return noop(); } addCompassListener() { return noop(); } onAncsRelayFrame() { return noop(); }
|
||||
onAncsAuthorization() { return noop(); } setRequiresAncs() {} rightWriteLimit() { return 20; } onPhoneLockSignal() {}
|
||||
async writeRawToRight() {} async setBrightness() {} async enableWearDetectionAndRequestState() {} async playBuzzerSequence() {} async close() {}
|
||||
emitState(phase, status = phase) { if (phase === 'connected' || phase === 'disconnected') this.phase = phase; this.listeners.state?.({ phase, status }); }
|
||||
emitRing(input) { this.listeners.ring?.(input); }
|
||||
emitWear(wearing) { this.listeners.wear?.(wearing); }
|
||||
async start() { this.emitState('connected', 'Connected.'); }
|
||||
async disconnect() { this.emitState('disconnected', 'Disconnected.'); }
|
||||
waitForFrameFinished() { return Promise.resolve('sent'); }
|
||||
changed() { if (this.phase === 'connected') sent.push(this.compositor.composite()); }
|
||||
}
|
||||
const provider = {
|
||||
isEnabled: () => settings.enabled, showOnTap: () => settings.tap,
|
||||
@@ -85,13 +114,14 @@ function fixture() {
|
||||
"../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} },
|
||||
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
|
||||
knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
|
||||
'../native/ios-bluetooth': { iosBluetooth: () => ({}) },
|
||||
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {}, stopPhoneCapture() {} } },
|
||||
'./glasses-session': { GlassesSession: Session },
|
||||
'../native/faceclaw-communicator.ios': { FaceclawCommunicatorBridge, resolveIosPeripherals: async addresses => addresses },
|
||||
'../native/preview-display.ios': { PreviewDisplayTarget },
|
||||
'./ancs-client': { ANCS_FIRMWARE_VERSION: 16, AncsClient: class { state = 'disconnected'; start() {} stop() {} stopCommand() { return new Uint8Array(); } receive() { return false; } } },
|
||||
'../native/nightscout-bridge': { nightscoutBridge: { async start() {}, async stop() {} } },
|
||||
'./glance-host': { GlanceHost }, './events': events,
|
||||
'./lock-screen': { LOCK_SCREEN_SURFACE_ID: 'lock-screen', createLockScreenImage: () => new images.GrayImage(640, 480, 123) },
|
||||
'./device-addresses': { loadDeviceAddresses: () => ({}) }, './ios-peripheral-identity': { deviceAddressError: () => null },
|
||||
'./device-addresses': { loadDeviceAddresses: () => ({ right: 'AA', left: 'BB', ring: '' }) }, './ios-peripheral-identity': { deviceAddressError: () => null },
|
||||
'../apps/launcher': { launcherEntries: () => [] },
|
||||
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': {}, '../apps/evenhub/updates': {}, '../apps/evenhub': {},
|
||||
'../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' },
|
||||
@@ -103,9 +133,7 @@ function fixture() {
|
||||
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
|
||||
'../apps/ios-availability': { iosAppUnavailableReason: () => null },
|
||||
'../graphics/glyph-wire': { prepareFrameDraws: () => null },
|
||||
'../native/texture-planner.ios': { IosTexturePlanner: class {} },
|
||||
'../graphics/surface-compositor': { SurfaceCompositor }, '../graphics/plane': planes, '../graphics/image': images,
|
||||
'../native/ios-graphics': { previewPixels: pixels => pixels },
|
||||
'../graphics/plane': planes, '../graphics/image': images,
|
||||
'../ui/gestures': { makeInputEvent: event => event }, '../ui/layers': { noopLayerActions: {} },
|
||||
'../apps/files/text-viewer': {},
|
||||
'../ui/shell/shell': { shell, rawInputEventToInputEvent: event => ({
|
||||
@@ -124,8 +152,8 @@ function fixture() {
|
||||
NSNotificationCenter: { defaultCenter: { addObserverForNameObjectQueueUsingBlock(name, _object, _queue, fn) { observers.set(name, fn); return name; }, removeObserver(name) { observers.delete(name); } } },
|
||||
NSOperationQueue: { mainQueue: {} },
|
||||
});
|
||||
const controller = new IosPreviewController((pixels, title) => previews.push({ pixels, title }), message => errors.push(message));
|
||||
controller.compositor.submitSurfaceFrame('launcher', new Uint8Array(640 * 480).fill(75), { x: 0, y: 0, width: 640, height: 480 });
|
||||
controller = new IosPreviewController((pixels, title) => previews.push({ pixels, title }), message => errors.push(message));
|
||||
window.requestRender();
|
||||
controller.resume();
|
||||
async function drain() { await controller.inputQueue; await controller.glance.queue; }
|
||||
async function advance(ms) {
|
||||
@@ -133,16 +161,18 @@ function fixture() {
|
||||
for (const [id, task] of [...tasks]) if (task.at <= now && tasks.delete(id)) task.fn();
|
||||
await drain();
|
||||
}
|
||||
async function render() { await drain(); await advance(33); assert.deepEqual(errors, []); }
|
||||
async function render() { await drain(); await advance(33); await drain(); assert.deepEqual(errors, []); }
|
||||
async function hardware(type, source = 2, ringTick) {
|
||||
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, type), protocol.integer(2, source))));
|
||||
const input = protocol.decodeGlassesInput({ sid: protocol.SID.hub, flag: 1, payload });
|
||||
if (ringTick !== undefined) input.ringInput = { type: type === 3 ? 2 : 1, tick: ringTick, aux:0, speed:0 };
|
||||
session.onInput(input);
|
||||
const input = { kind: type === 12 ? 'display-wake' : 'sys-event', containerName: '', eventType: type, eventSource: source,
|
||||
systemExitReasonCode: 0, frameId: 0 };
|
||||
if (ringTick !== undefined) input.ringInput = { type: type === 3 ? 2 : 1, tick: ringTick, aux: 0, speed: 0 };
|
||||
bridge.emitRing(input);
|
||||
await render();
|
||||
}
|
||||
async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); }
|
||||
return { controller, shell, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers, wear: wearing => session.onWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
|
||||
async function connect() { await controller.connect(); await render(); }
|
||||
return { controller, shell, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
|
||||
get bridge() { return bridge; }, connect, wear: wearing => bridge.emitWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
|
||||
}
|
||||
const assertBlank = pixels => assert.ok(pixels.every(p => p === 0));
|
||||
const assertBoard = pixels => {
|
||||
@@ -151,7 +181,7 @@ const assertBoard = pixels => {
|
||||
};
|
||||
|
||||
test('iOS sleep tap shows the shared board; repeated tap renews timeout and hides to black', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.shell.sleep(); await f.render();
|
||||
const f = fixture(); await f.connect(); f.shell.sleep(); await f.render();
|
||||
assertBlank(f.sent.at(-1));
|
||||
await f.hardware(0); assertBoard(f.sent.at(-1)); assert.equal(f.shell.isScreenOn(), false);
|
||||
assert.equal(f.previews.at(-1).title, 'Glanceboard'); assert.equal(f.received.length, 0);
|
||||
@@ -165,7 +195,7 @@ test('iOS sleep tap shows the shared board; repeated tap renews timeout and hide
|
||||
});
|
||||
|
||||
test('iOS holds and tap-then-holds last until release, including with phone backgrounded', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.shell.sleep(); f.controller.pause();
|
||||
const f = fixture(); await f.connect(); f.shell.sleep(); f.controller.pause();
|
||||
const previews = f.previews.length;
|
||||
for (const type of [9, 11]) {
|
||||
await f.hardware(type, 1); assertBoard(f.sent.at(-1));
|
||||
@@ -176,7 +206,7 @@ test('iOS holds and tap-then-holds last until release, including with phone back
|
||||
});
|
||||
|
||||
test('double-tap and other shell wakes replace Glanceboard without waking it again', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.shell.sleep();
|
||||
const f = fixture(); await f.connect(); f.shell.sleep();
|
||||
await f.hardware(9); await f.hardware(3);
|
||||
assert.equal(f.shell.isScreenOn(), true); assert.equal(f.controller.glance.isVisible(), false);
|
||||
assert.equal(f.sent.at(-1)[0], 80); assert.equal(f.boardStats.stops, 1);
|
||||
@@ -186,14 +216,14 @@ test('double-tap and other shell wakes replace Glanceboard without waking it aga
|
||||
});
|
||||
|
||||
test('head tilt uses Glanceboard when enabled and otherwise wakes the regular UI', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.shell.sleep();
|
||||
const f = fixture(); await f.connect(); f.shell.sleep();
|
||||
await f.hardware(12, 1); assertBoard(f.sent.at(-1)); assert.equal(f.shell.isScreenOn(), false);
|
||||
await f.hardware(3); f.shell.sleep(); f.settings.tilt = false;
|
||||
await f.hardware(12, 1); assert.equal(f.shell.isScreenOn(), true); assert.equal(f.sent.at(-1)[0], 80);
|
||||
});
|
||||
|
||||
test('disabled triggers and scrolls do not show the board; awake taps still reach the shell', async () => {
|
||||
const f = fixture(); await f.controller.connect();
|
||||
const f = fixture(); await f.connect();
|
||||
await f.hardware(0); assert.equal(f.received.at(-1).type, 'click'); assert.equal(f.boardStats.starts, 0);
|
||||
f.shell.sleep(); f.settings.tap = false; f.settings.hold = false;
|
||||
for (const type of [0, 9, 11, 10, 1, 2]) await f.hardware(type);
|
||||
@@ -218,7 +248,7 @@ test('phone preview gestures use Glanceboard; losing runtime cleans up holds and
|
||||
|
||||
|
||||
test('iOS lock hides apps and Glanceboard, blocks input, and stays locked when put back on', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.shell.sleep(); await f.hardware(0);
|
||||
const f = fixture(); await f.connect(); f.shell.sleep(); await f.hardware(0);
|
||||
f.controller.pause();
|
||||
// The notification precedes the property change: do not immediately undo it.
|
||||
f.observers.get('lock')(); f.wear(false); await f.render();
|
||||
@@ -239,7 +269,7 @@ test('iOS lock hides apps and Glanceboard, blocks input, and stays locked when p
|
||||
});
|
||||
|
||||
test('iOS removal before phone lock, setting changes, and observer lifetime', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.wear(false); await f.render();
|
||||
const f = fixture(); await f.connect(); f.wear(false); await f.render();
|
||||
assert.equal(f.controller.glassesLocked, false);
|
||||
f.observers.get('lock')(); await f.render();
|
||||
assert.equal(f.controller.glassesLocked, true);
|
||||
@@ -255,7 +285,7 @@ test('iOS removal before phone lock, setting changes, and observer lifetime', as
|
||||
});
|
||||
|
||||
test('iOS wear notifications catch protected-data changes missed while suspended', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.controller.pause();
|
||||
const f = fixture(); await f.connect(); f.controller.pause();
|
||||
f.phoneState.protectedDataAvailable = false; f.wear(false); await f.render();
|
||||
assert.equal(f.controller.glassesLocked, true);
|
||||
f.controller.resume(); await f.render();
|
||||
@@ -267,7 +297,7 @@ test('iOS wear notifications catch protected-data changes missed while suspended
|
||||
|
||||
|
||||
test('iOS locked phone controls cannot dispatch mirror, voice, or keyboard input', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.wear(false);
|
||||
const f = fixture(); await f.connect(); f.wear(false);
|
||||
f.observers.get('lock')(); await f.render();
|
||||
await f.phone('tap', 'mirror'); await f.phone('long-press', 'watch');
|
||||
f.controller.startVoiceInput();
|
||||
@@ -276,16 +306,16 @@ test('iOS locked phone controls cannot dispatch mirror, voice, or keyboard input
|
||||
assert.equal(f.received.length, 0);
|
||||
assert.equal(f.controller.glassesLocked, true);
|
||||
// Locked state also survives transport recovery until a phone unlock.
|
||||
f.controller.session.onState({ phase: 'retrying' });
|
||||
f.controller.session.onState({ phase: 'connecting' });
|
||||
f.controller.session.onState({ phase: 'connected' });
|
||||
f.bridge.emitState('retrying');
|
||||
f.bridge.emitState('connecting');
|
||||
f.bridge.emitState('connected');
|
||||
await f.render();
|
||||
assert.ok(f.sent.at(-1).every(p => p === 123));
|
||||
});
|
||||
|
||||
|
||||
test('iOS deduplicates original ring ticks before apps, lock toggles and Glanceboard', async () => {
|
||||
const f = fixture(); await f.controller.connect();
|
||||
const f = fixture(); await f.connect();
|
||||
await f.hardware(0,2,1000); await f.hardware(0,2,1050);
|
||||
assert.equal(f.received.length,1);
|
||||
await f.hardware(0,2,1100); assert.equal(f.received.length,2);
|
||||
@@ -299,8 +329,8 @@ test('iOS deduplicates original ring ticks before apps, lock toggles and Glanceb
|
||||
assert.equal(f.shell.isScreenOn(),true);
|
||||
await f.hardware(3,2,2050); assert.equal(f.shell.isScreenOn(),true);
|
||||
// A new transport session starts a fresh suppression window.
|
||||
f.controller.session.onState({ phase:'retrying' });
|
||||
f.controller.session.onState({ phase:'connected' });
|
||||
f.bridge.emitState('retrying');
|
||||
f.bridge.emitState('connected');
|
||||
await f.hardware(3,2,2051); assert.equal(f.shell.isScreenOn(),false);
|
||||
});
|
||||
|
||||
@@ -325,7 +355,7 @@ test('keyboard sessions cannot deliver text after the glasses lock or phone leav
|
||||
|
||||
|
||||
test('external input shares iOS dispatch, including hold release and phone-lock gating', async () => {
|
||||
const f = fixture(); await f.controller.connect(); f.wear(false);
|
||||
const f = fixture(); await f.connect(); f.wear(false);
|
||||
assert.equal(f.remoteHost.ready(), true);
|
||||
assert.equal(f.remoteHost.locked(), false);
|
||||
await f.remoteHost.input('swipe-left', 'watch');
|
||||
|
||||
Reference in new issue
Block a user