diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index 850eedc9..67566f92 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -17,6 +17,7 @@ import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; @@ -33,7 +34,6 @@ import { } from "../../ui/gestures"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -295,16 +295,13 @@ function inferForeground(window: BlocksWindow, focused: boolean): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread - * (same path as frame submission). Effects never exceed one message, so no - * phrase pacing is needed. + * steps on its own timer; the platform bridge routes it from the worker. + * Effects never exceed one message, so no phrase pacing is needed. */ function playSfx(window: BlocksWindow, steps: Step[]): void { if (!window.soundOn || steps.length === 0) return; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`blocks sfx failed: ${error}`); } diff --git a/app/apps/evenhub/manager.ts b/app/apps/evenhub/manager.ts index a247d98b..7b3b327c 100644 --- a/app/apps/evenhub/manager.ts +++ b/app/apps/evenhub/manager.ts @@ -103,7 +103,8 @@ async function startApp( cleanup?: () => void, ): Promise { const windowId = `evenhub:app:${nextSerial++}`; - const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl); + const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl, + payload => ctx.actions.playBuzzerSequence(payload)); const webView = createEvenHubWebView(session); session.attachWebView({ evaluateJs: webView.evaluateJs, diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts index f9b073a3..d0e4421b 100644 --- a/app/apps/evenhub/session.ts +++ b/app/apps/evenhub/session.ts @@ -65,8 +65,6 @@ import { ensureFineLocationPermission } from "../../native/location-permissions" const UPNG = require("upng-js"); -declare const com: any; - /** OsEventTypeList values (PB ordinals). */ const CLICK_EVENT = 0; const SCROLL_TOP_EVENT = 1; @@ -371,6 +369,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH distDir: string, log: (message: string) => void, remoteUrl = "", + private readonly sendBuzzerSequence: (payload: Uint8Array) => Promise | void = () => {}, ) { this.manifest = manifest; this.distDir = distDir; @@ -1164,13 +1163,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH private async playBuzzer(rawSteps: unknown[]): Promise { const steps = normalizeBuzzerSteps(rawSteps); if (steps.length === 0) return; - const communicator = activeCommunicator(); - if (!communicator) return; for (let i = 0; i < steps.length; i += CFW_SEQ_MAX) { const chunk = steps.slice(i, i + CFW_SEQ_MAX); const payload = buildSoundSequencePayload(chunk); try { - communicator.playBuzzerSequence(payload.buffer); + await this.sendBuzzerSequence(payload); } catch (error) { this.log(`evenhub: playBuzzer failed: ${error}`); return; @@ -1274,15 +1271,6 @@ function clampImuFreq(freq: number): number { return Math.min(1000, Math.max(100, snapped)); } -function activeCommunicator(): any { - if (!global.isAndroid) return null; - try { - return com.faceclaw.app.FaceclawBleCommunicator.getActive(); - } catch { - return null; - } -} - /** * Copy content into a `preserve` container from the same-named, same-kind * container in the previous page (if any), so a replaceLayout keeps its pixels / diff --git a/app/apps/flappy/flappy-app.worker.ts b/app/apps/flappy/flappy-app.worker.ts index b0b998db..5ac79967 100644 --- a/app/apps/flappy/flappy-app.worker.ts +++ b/app/apps/flappy/flappy-app.worker.ts @@ -31,6 +31,7 @@ import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; @@ -64,7 +65,6 @@ import { } from "./flappy-sprites"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -311,7 +311,7 @@ function saveHighScore(window: FlappyWindow): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. * Minor (frequent) effects are dropped when they'd arrive within the * rate-limit gap; newsworthy ones always play. */ @@ -321,9 +321,7 @@ function playSfx(window: FlappyWindow, steps: Step[], minor = false): void { if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return; if (minor) window.lastMinorSfxAtMs = now; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`flappy sfx failed: ${error}`); } diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index 070f90ae..f829dfc8 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -25,6 +25,7 @@ import { getFont } from "../../graphics/bdffont"; import { ensurePreinstalledFonts, installedFontPath } from "../../graphics/installed-fonts"; import { TtfFont } from "../../graphics/ttf-font"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; @@ -34,7 +35,6 @@ import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-win import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures"; declare const global: any; -declare const com: any; ensurePreinstalledFonts(); const fontPath = installedFontPath("Roboto-Regular.ttf"); @@ -206,14 +206,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. */ function playSfx(window: FreecellWindow, steps: Step[]): void { if (!window.soundOn || steps.length === 0) return; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`freecell sfx failed: ${error}`); } diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index 2e0b5c5f..dcb4189d 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -25,6 +25,7 @@ import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; @@ -43,7 +44,6 @@ import { import { clamp } from "../../util/numeric-util"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -278,14 +278,12 @@ function saveDifficultyIndex(window: MinesweeperWindow): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. */ function playSfx(window: MinesweeperWindow, steps: Step[]): void { if (!window.soundOn || steps.length === 0) return; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`minesweeper sfx failed: ${error}`); } diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index e9b375a5..8da41a15 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -27,6 +27,7 @@ import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; @@ -44,7 +45,6 @@ import { import { clamp } from "../../util/numeric-util"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -429,7 +429,7 @@ function saveHighScore(window: PinballWindow): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. * Minor (frequent) effects are dropped when they'd arrive within the * rate-limit gap; newsworthy ones always play. */ @@ -439,9 +439,7 @@ function playSfx(window: PinballWindow, steps: Step[], minor = false): void { if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return; if (minor) window.lastMinorSfxAtMs = now; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`pinball sfx failed: ${error}`); } diff --git a/app/g2/glasses-session.ts b/app/g2/glasses-session.ts index b068d563..1a10f9df 100644 --- a/app/g2/glasses-session.ts +++ b/app/g2/glasses-session.ts @@ -427,6 +427,13 @@ export class GlassesSession { if (this.state.phase !== 'connected') return this.latest = protocol.packGray4(gray, 640, 480); this.schedule(0) } + /** Same mode-5 kind-4 sequencer payload and acknowledged image path as Android. */ + async playBuzzerSequence(payload: Uint8Array): Promise { + if (this.state.phase !== 'connected' || !this.layoutCreated || payload.length < 3) return + const generation = this.generation + try { await this.requestCfw('left', new Uint8Array(payload)) } + catch (error) { if (generation === this.generation) this.fail(error, true) } + } /** Serialize mic enable/disable, including a release while enable awaits ACK. */ setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise { const token = ++this.microphoneToken, generation = this.generation diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index ee9f6c0c..13ff469e 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -43,6 +43,8 @@ import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible } from '../ui/shell import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySettingChanged, previewColorSetting, lockScreenEnabledSetting } from '../ui/dashboard-settings' import type { PhoneGesture } from '../phone-ui/phone-gestures' +import { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state' +import { findSoundEffect, playSoundEffect } from '../ui/sound-effects' /** iOS host for the shared app registry, shell, compositor and BLE session. */ export class IosPreviewController { @@ -96,10 +98,12 @@ export class IosPreviewController { private glassesLocked = false private lockEnabled = lockScreenEnabledSetting.get() private session: GlassesSession | null = null + private acknowledgedFrames = 0 private logLines: string[] = [] private logTimer: ReturnType | null = null private readonly actions: LayerActions = { ...noopLayerActions, + playBuzzerSequence: payload => this.session?.playBuzzerSequence(payload), requestRender: () => this.requestShellRender(), disconnect: () => this.disconnect(), startVoiceCapture: endpointing => this.startVoiceCapture(endpointing), @@ -457,6 +461,7 @@ export class IosPreviewController { ? new IosNavigationSensors(event => worker.postMessage({ type: 'navigation-sensors', event })) : undefined const host = new WorkerAppHost({ appId, worker, navigationSensors, + playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload), openUrl: url => { void Utils.openUrl(url) }, configureSurface: async (id, visible, heightMode) => { this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' }) @@ -492,6 +497,7 @@ export class IosPreviewController { if (error) { this.onError(error); return } if (!this.session) { this.session = new GlassesSession(iosBluetooth(), state => { + this.maybePlayWelcomeSound(state) if (state.phase !== 'connected' && state.phase !== 'connecting') this.glassesWorn = null if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded() shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging }) @@ -517,6 +523,17 @@ export class IosPreviewController { await this.session.start(addresses) } async disconnect(): Promise { await this.session?.stop() } + private maybePlayWelcomeSound(state: SessionState): void { + const firstNewFrame = state.frames > this.acknowledgedFrames + this.acknowledgedFrames = state.frames + // Match Android: wait for an acknowledged frame before consuming the jingle. + if (!firstNewFrame || state.phase !== 'connected' || !isWelcomeSoundPending()) return + setWelcomeSoundPending(false) + const effect = findSoundEffect('questcomplete') + if (effect) void playSoundEffect(effect, payload => this.actions.playBuzzerSequence(payload), + ms => new Promise(resolve => setTimeout(resolve, ms))) + .catch(error => this.logBluetooth(`Welcome sound failed: ${error}`)) + } startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() } private async prepareVoiceCapture(): Promise { if (this.glassesLocked) return false diff --git a/app/native/active-display.ts b/app/native/active-display.ts index a82db467..6ca761e9 100644 --- a/app/native/active-display.ts +++ b/app/native/active-display.ts @@ -7,9 +7,8 @@ declare const com: any; * through Java statics because those are shared across isolates, unlike any * JS-side state. * - * Frame submission only. Glasses-hardware calls (e.g. playBuzzerSequence) - * should keep using FaceclawBleCommunicator.getActive() directly, so they - * skip cleanly when nothing is connected. + * Frame submission only. Buzzer effects use worker-buzzer, which routes to + * the platform BLE session and skips playback when disconnected. */ export function getActiveDisplay(): any { return ( diff --git a/app/native/worker-buzzer.ts b/app/native/worker-buzzer.ts new file mode 100644 index 00000000..d3d3d385 --- /dev/null +++ b/app/native/worker-buzzer.ts @@ -0,0 +1,12 @@ +import type { WorkerAppReply } from "../ui/shell/worker-window"; + +declare const com: any; + +/** Workers use the main-thread BLE session on iOS and the native queue on Android. */ +export function playWorkerBuzzerSequence(payload: Uint8Array): void { + if (global.isIOS) { + global.postMessage({ type: "buzzer-sequence", payload: Array.from(payload) } satisfies WorkerAppReply); + } else { + com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(new Uint8Array(payload).buffer); + } +} diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts index 6f5f3251..55687b26 100644 --- a/app/ui/shell/worker-window.ts +++ b/app/ui/shell/worker-window.ts @@ -34,6 +34,7 @@ export type WorkerAppMessage = | { type: "tool-call"; callId: string; windowId: string; name: string; args: unknown }; export type WorkerAppReply = + | { type: "buzzer-sequence"; payload: number[] } | { type: "navigation-sensors"; request: NavigationSensorRequest } | { type: "open-url"; url: string } | { type: "surface-frame"; surfaceId: string; width: number; height: number; pixels: string } @@ -183,6 +184,7 @@ export type WorkerAppHostOptions = { worker: Worker; navigationSensors?: { handle(request: NavigationSensorRequest): void; stop(): void }; openUrl?: (url: string) => void; + playBuzzerSequence?: (payload: Uint8Array) => Promise | void; /** Create/refresh a window surface on the compositor (no-op when disconnected). */ configureSurface: (surfaceId: string, visible: boolean, heightMode: WindowHeightMode) => Promise; setSurfaceVisible: (surfaceId: string, visible: boolean) => void; @@ -240,6 +242,12 @@ export class WorkerAppHost { const message = event.data as WorkerAppReply | undefined; if (!message) return; switch (message.type) { + case "buzzer-sequence": + if (this.openWindows.size) { + void Promise.resolve(this.options.playBuzzerSequence?.(new Uint8Array(message.payload))) + .catch(error => console.warn(`${this.options.appId} buzzer failed: ${error}`)); + } + break; case "navigation-sensors": if (this.openWindows.size) this.options.navigationSensors?.handle(message.request); break; diff --git a/tests/buzzer.test.cjs b/tests/buzzer.test.cjs new file mode 100644 index 00000000..b9d9b20d --- /dev/null +++ b/tests/buzzer.test.cjs @@ -0,0 +1,40 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +const payload = new Uint8Array([5, 4, 1, 0xb8, 0x0b, 25, 80, 0]); + +test('iOS game worker forwards an immutable byte payload through its host and ignores closed windows', async () => { + const played = [], worker = { postMessage() {} }; + const load = loader({ global: { isIOS: true, postMessage: data => worker.onmessage({ data }) } }, { + '../../graphics/image': {}, './chrome-layer': { windowIcon() {} }, + '../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } }, + './geometry': { appViewportSize: () => ({ width: 576, height: 260 }) }, + '../../native/frame-timings': {}, './worker-state': {}, + './shell': { shell: { registerWindow() {} } }, + }); + const { WorkerAppHost } = load('app/ui/shell/worker-window.ts'); + const bridge = load('app/native/worker-buzzer.ts'); + const host = new WorkerAppHost({ appId: 'blocks', worker, configureSurface: async () => {}, + requestShellRender() {}, removeSurface() {}, playBuzzerSequence: bytes => played.push(bytes) }); + bridge.playWorkerBuzzerSequence(payload); + assert.equal(played.length, 0); + const window = host.openWindow({ windowId: 'blocks', title: 'Blocks' }); + const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1); + bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0); + assert.deepEqual(played, [payload]); + window.close(); bridge.playWorkerBuzzerSequence(payload); + assert.equal(played.length, 1); +}); + +test('Android game bridge retains direct native playback and skips a missing communicator', () => { + let active = null; + const played = []; + const bridge = loader({ global: { isIOS: false }, + com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => active } } } } })('app/native/worker-buzzer.ts'); + bridge.playWorkerBuzzerSequence(payload); + active = { playBuzzerSequence: buffer => played.push(new Uint8Array(buffer)) }; + const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1); + bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0); + assert.deepEqual(played, [payload]); +}); diff --git a/tests/fixtures/pinball-worker.cjs b/tests/fixtures/pinball-worker.cjs index e0078c03..bb4cfebe 100644 --- a/tests/fixtures/pinball-worker.cjs +++ b/tests/fixtures/pinball-worker.cjs @@ -36,6 +36,7 @@ module.exports = function worker() { '../../graphics/ui-fonts': { getDefaultSmallFont: () => smallFont }, '../../native/frame-timings': { finishFrame: noop, logFrame: noop }, '../../native/active-display': {}, + '../../native/worker-buzzer': { playWorkerBuzzerSequence() {} }, '../../native/settings-store': { getStringSetting: () => highScore, setStringSetting: (_, value) => { highScore = value; } }, '../../ui/sound-effects': {}, '../../ui/sound-setting': { loadSoundEnabled: () => true, saveSoundEnabled: () => {} }, diff --git a/tests/ios-app-host.test.cjs b/tests/ios-app-host.test.cjs index 84a86468..a5d75a67 100644 --- a/tests/ios-app-host.test.cjs +++ b/tests/ios-app-host.test.cjs @@ -171,7 +171,7 @@ test('settings-driven repaint runs after font cache invalidation, regardless of }); test('background glasses input still composites frames; phone resume preserves the session; explicit stop stays stopped', async () => { - const tasks = new Map(), screenStates = [], inputs = [], frames = [], previews = [], states = []; + const tasks = new Map(), screenStates = [], inputs = [], frames = [], previews = [], states = [], sounds = []; let nextTask = 0, starts = 0, stops = 0, pollStarts = 0, pollStops = 0, session; const window = { windowId: 'launcher', surfaceId: 'launcher', appId: 'launcher', title: 'Apps', setScreenOn: on => screenStates.push(on), requestRender() {} }; @@ -185,6 +185,7 @@ test('background glasses input still composites frames; phone resume preserves t async start() { starts++; this.state = { phase: 'connected' }; this.onState(this.state); } async stop() { stops++; this.state = { phase: 'disconnected' }; this.onState(this.state); } setFrame(pixels) { if (this.state.phase === 'connected') frames.push(pixels); } + playBuzzerSequence(payload) { sounds.push([...payload]); } wake() {} } const settings = { lockScreenEnabledSetting: { get: () => true }, onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } }; @@ -229,7 +230,11 @@ test('background glasses input still composites frames; phone resume preserves t }); const flush = () => { for (const [id, fn] of [...tasks]) { if (tasks.delete(id)) fn(); } }; const controller = new api.IosPreviewController(image => previews.push(image), assert.fail, state => states.push(state)); + await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0])); + assert.equal(sounds.length, 0); controller.resume(); await controller.connect(); flush(); + await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0])); + assert.deepEqual(sounds, [[5, 4, 0]]); const previewCount = previews.length, stateCount = states.length; controller.pause(); controller.pause(); // NativeScript also unloads its root page on background entry. assert.equal(stops, 0); assert.ok(screenStates.every(Boolean)); @@ -361,3 +366,31 @@ test('iOS compositor rejects missing and removed surfaces before crossing into K assert.throws(() => c.submitSurfaceFrame('nightscout', pixels, rect), /Unknown surface/); assert.equal(nativeSubmissions, 1); }); + + +test('iOS welcome sound waits for a new acknowledged frame and is consumed once', async () => { + let pending = true; + const played = [], sounds = load('app/ui/sound-effects.ts', {}); + const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { + require: id => ({ + '../phone-ui/onboarding-state': { isWelcomeSoundPending: () => pending, setWelcomeSoundPending: value => { pending = value; } }, + '../ui/sound-effects': sounds, + })[id] ?? {}, + setTimeout: fn => { fn(); return 1; }, + }); + const host = Object.create(IosPreviewController.prototype); + host.acknowledgedFrames = 0; + host.actions = { playBuzzerSequence: bytes => played.push([...bytes]) }; + host.logBluetooth = assert.fail; + host.maybePlayWelcomeSound({ phase: 'connecting', frames: 0 }); + host.maybePlayWelcomeSound({ phase: 'connected', frames: 0 }); + assert.equal(pending, true); assert.equal(played.length, 0); + host.maybePlayWelcomeSound({ phase: 'connected', frames: 1 }); + assert.equal(pending, false); + await new Promise(resolve => setImmediate(resolve)); + assert.ok(played.length > 0); + const count = played.length; + host.maybePlayWelcomeSound({ phase: 'connected', frames: 1 }); + host.maybePlayWelcomeSound({ phase: 'connected', frames: 2 }); + assert.equal(played.length, count); +}); diff --git a/tests/ios-ble.test.cjs b/tests/ios-ble.test.cjs index e5ae96a8..ea71c669 100644 --- a/tests/ios-ble.test.cjs +++ b/tests/ios-ble.test.cjs @@ -470,6 +470,57 @@ test('delta ids skip reserved values and advance only on emitted deltas; reconne }); const compassMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 10); +const buzzerMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 5); +const buzzerPayload = () => new Uint8Array([5, 4, 2, 0xb8, 0x0b, 25, 80, 0, 1, 0, 0, 20, 0]); +test('buzzer snapshots the complete Android payload and waits for both lens ACKs without counting a frame', async t => { + const h = harness(t); + await h.session.playBuzzerSequence(buzzerPayload()); + assert.equal(h.transport.sent.length, 0); + await h.session.start(addresses); + await h.session.playBuzzerSequence(new Uint8Array()); + assert.equal(buzzerMessages(h.transport).length, 0); + h.transport.hold = true; + const payload = buzzerPayload(), expected = payload.slice(); + let complete = false; + const playing = h.session.playBuzzerSequence(payload).then(() => { complete = true; }); + payload.fill(0); + await until(() => buzzerMessages(h.transport).length === 1); + const sent = buzzerMessages(h.transport)[0]; + assert.equal(sent.id, 'L'); assert.deepEqual(sent.message.payload, expected); + h.transport.ack(sent.id, sent.message, { lenses: [1] }); await tick(); + assert.equal(complete, false); + h.transport.ack(sent.id, sent.message, { lenses: [2] }); await playing; + assert.equal(h.session.state.frames, 0); +}); + +test('buzzer shares display compression and ACK recovery while the frame window is full', async t => { + const h = harness(t); await h.session.start(addresses); h.transport.hold = true; + h.session.setFrame(noisy()); await until(() => imageMessages(h.transport).length === 3); + const playing = h.session.playBuzzerSequence(buzzerPayload()); + await until(() => buzzerMessages(h.transport).length === 1); + const sent = buzzerMessages(h.transport)[0]; + h.transport.ack(sent.id, sent.message, { nack: true }); + await until(() => buzzerMessages(h.transport).length === 2); + const replay = buzzerMessages(h.transport)[1]; + assert.notEqual(replay.message.magic, sent.message.magic); + assert.deepEqual(replay.message.payload, buzzerPayload()); + h.transport.hold = false; // Subsequent bands of the large frame can finish too. + for (const item of h.transport.sent.filter(s => s.message.sid === p.SID.cfw).slice(-4)) ack(h.transport, item); + await playing; + assert.equal(h.session.state.phase, 'connected'); + await until(() => h.session.state.frames === 1); +}); + +test('disconnect cancels queued buzzer work and does not play it on reconnect', async t => { + const h = harness(t); await h.session.start(addresses); h.transport.hold = true; + const playing = h.session.playBuzzerSequence(buzzerPayload()); + await until(() => buzzerMessages(h.transport).length === 1); + h.transport.emit({ kind: 'disconnected', identifier: 'R' }); await playing; + await h.session.playBuzzerSequence(buzzerPayload()); + h.transport.hold = false; await h.session.start(addresses, true); + assert.equal(buzzerMessages(h.transport).length, 1); +}); + test('compass desired state survives connecting and reconnecting; explicit stop disables it first', async t => { const h = harness(t); h.session.setCompassEnabled(true); diff --git a/tests/ios-evenhub.test.cjs b/tests/ios-evenhub.test.cjs index cea6b99b..7b03403c 100644 --- a/tests/ios-evenhub.test.cjs +++ b/tests/ios-evenhub.test.cjs @@ -98,7 +98,7 @@ test('local package runtimes unpack independently and clean up after closing or assert.equal(files.size, 0); }); -function sessionHarness(overrides = {}) { +function sessionHarness(overrides = {}, playBuzzer = () => {}) { 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) } }, @@ -110,7 +110,7 @@ function sessionHarness(overrides = {}) { '../../native/location': {}, '../../native/location-tracker': {}, '../../native/location-permissions': {}, ...overrides, }); 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)); + const session = new api.EvenHubSession({ name: 'Probe', packageId: 'test.probe', permissions: [] }, '/test/dist', line => logs.push(line), '', playBuzzer); session.attachWindow({ requestRender: () => renders.push(1), windowId: 'probe', closeWindow() {} }); const web = vm.createContext({ Promise, Map, Date, console }); web.window = web; @@ -220,3 +220,17 @@ test('store login and catalog requests use platform signing with the shared wire assert.equal(requests[1].headers.sign, 'fixture-signature'); assert.equal(signatures[0], api.signingParts('POST', '/v2/g/login', requests[0].headers.common, '', '', requests[0].body)); }); + + +test('EvenHub buzzer RPC uses the host action on iOS and chunks and paces long sequences', async () => { + const sounds = loader()('app/ui/sound-effects.ts'), played = []; + const h = sessionHarness({ '../../ui/sound-effects': sounds }, bytes => played.push([...bytes])); + const steps = Array.from({ length: 49 }, () => ({ freq: 3000, ms: 1, duty: 25 })); + await h.session.dispatchExtension('playBuzzer', [steps]); + assert.equal(played.length, 2); + assert.deepEqual(played[0], [...sounds.buildSoundSequencePayload(steps.slice(0, 48))]); + assert.deepEqual(played[1], [...sounds.buildSoundSequencePayload(steps.slice(48))]); + await h.session.dispatchExtension('playBuzzer', [[null, { freq: 'bad', ms: 10 }]]); + assert.equal(played.length, 2); + h.session.close(); +}); diff --git a/tests/ios-glanceboard.test.cjs b/tests/ios-glanceboard.test.cjs index 3bb802f4..b221b449 100644 --- a/tests/ios-glanceboard.test.cjs +++ b/tests/ios-glanceboard.test.cjs @@ -92,6 +92,8 @@ function fixture() { '../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': {}, '../apps/evenhub/updates': {}, '../apps/evenhub': {}, '../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' }, '../apps/all-apps': { ALL_APPS: [{ appId: 'glanceboard', glanceboard: provider }] }, + '../phone-ui/onboarding-state': { isWelcomeSoundPending: () => false }, + '../ui/sound-effects': {}, '../ui/shell/worker-window': {}, '../ui/shell/in-process-window': {}, '../ui/dashboard-settings': { lockScreenEnabledSetting: { get: () => settings.lock }, onAnySettingChanged: fn => { settingsChanged = fn; return () => {}; }, previewColorSetting: { get: () => 'white' } }, '../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) }, diff --git a/tests/touch-input.test.cjs b/tests/touch-input.test.cjs index cab8e831..14529017 100644 --- a/tests/touch-input.test.cjs +++ b/tests/touch-input.test.cjs @@ -49,6 +49,7 @@ function worker() { '../../graphics/ui-fonts': { getDefaultSmallFont: noop }, '../../native/frame-timings': { finishFrame: noop, logFrame: noop }, '../../native/active-display': {}, + '../../native/worker-buzzer': { playWorkerBuzzerSequence() {} }, '../../native/settings-store': { getStringSetting: () => '0', setStringSetting: noop }, '../../ui/sound-effects': {}, '../../ui/window-menu': {}, '../../ui/sound-setting': { loadSoundEnabled: () => true, saveSoundEnabled: noop },