forked from Blendman974/faceclaw
Merge branch 'main' into glanceboard-tilt-angle-and-glance-down
This commit is contained in:
commit
37d81d0308
416 files changed
+34629
-3885
No files matched your search
@@ -152,8 +152,10 @@ export class AncsClient {
|
||||
if (flags & 2) {
|
||||
const data = new Uint8Array(this.fragments); this.fragments = []; this.packetKind = -1
|
||||
if (kind === 0) {
|
||||
if (data.length !== 1 || data[0] > 2) { this.recover(); return true }
|
||||
if (data.length !== 1 || data[0] > 3) { this.recover(); return true }
|
||||
if (data[0] === 2) { this.recover('Glasses reported a notification relay error. Retrying…'); return true }
|
||||
// Only a G1's firmware looks for ANCS itself, so only it can say the phone isn't offering it.
|
||||
if (data[0] === 3) { this.recover('iPhone is not sharing notifications with the glasses. Enable Share System Notifications in Settings → Bluetooth → right arm. Retrying…'); return true }
|
||||
if (data[0] === 0) { this.resetReceived = true; this.log('Glasses accepted notification relay START; awaiting subscription') }
|
||||
if (data[0] === 1) {
|
||||
if (this.timer !== null) clearTimeout(this.timer)
|
||||
|
||||
+1
-14
@@ -27,7 +27,7 @@
|
||||
*
|
||||
* It is **not** a measurement. A hand over the temple, a pocket, or a body
|
||||
* between phone and glasses moves the estimate by metres. The wearer therefore
|
||||
* only ever sees a coarse zone (glyph + label), never a number in metres they
|
||||
* only ever sees a coarse zone (a signal-strength glyph), never a number in metres they
|
||||
* might over-trust; the metres value exists purely to rank and bucket.
|
||||
*
|
||||
* This module is pure (no NativeScript imports) so it can run under node tests.
|
||||
@@ -75,19 +75,6 @@ export function zoneFromMeters(meters: number): ProximityZone {
|
||||
return "distant";
|
||||
}
|
||||
|
||||
export function zoneLabel(zone: ProximityZone): string {
|
||||
switch (zone) {
|
||||
case "immediate":
|
||||
return "In your hand";
|
||||
case "near":
|
||||
return "Nearby";
|
||||
case "far":
|
||||
return "Across the room";
|
||||
case "distant":
|
||||
return "Far away";
|
||||
}
|
||||
}
|
||||
|
||||
/** A glyph for the zone; SF Symbols have no Android equivalent, so plain text. */
|
||||
export function zoneGlyph(zone: ProximityZone): string {
|
||||
switch (zone) {
|
||||
|
||||
@@ -1,5 +1,18 @@
|
||||
export const DEFAULT_BRIGHTNESS_CURVE = "0:0,0.2:7,1.5:33,5:100";
|
||||
const PREVIOUS_DEFAULT_CURVE = "0:0,1:10,10:30,100:65,1000:100";
|
||||
/**
|
||||
* The RayNeo iO's sensor reads real lux, far above the G2's behind-the-lens values, so the
|
||||
* G2's curve would sit at maximum in any lit room. These knots put the default 20–100 range on
|
||||
* the stock firmware's own scene levels: 29, 41, 41, 53, 65, 76, 94 and 100 percent at 3, 10,
|
||||
* 200, 300, 600, 1000, 4000 and 5000 lux.
|
||||
*/
|
||||
export const RAYNEO_IO_BRIGHTNESS_CURVE = "0:0,3:11,10:26,200:26,300:41,600:56,1000:70,4000:93,5000:100";
|
||||
|
||||
/**
|
||||
* An Even G1's OPT3007 also reads lux (centilux on the wire). These knots are the stock firmware's table: levels 0, 10, 25,
|
||||
* 41 and 42 of 42 at 0, 50, 200, 1000 and 2000 lux (g1_firmware/docs/VM.md §6.7).
|
||||
*/
|
||||
export const G1_BRIGHTNESS_CURVE = "0:0,50:24,200:60,1000:98,2000:100";
|
||||
|
||||
/** Percent of the configured range; defaults (20–100) give 20/26/46/100. */
|
||||
export function brightnessCurveError(value: string): string | null {
|
||||
|
||||
+236
-65
@@ -16,7 +16,6 @@ import { mediaControllerBridge } from "../native/media-controller";
|
||||
import { nightscoutBridge } from "../native/nightscout-bridge";
|
||||
import { weatherBridge } from "../native/weather";
|
||||
import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, onAndroidNotificationRemoved, readActiveNotifications } from "../native/notification-icons";
|
||||
import { shouldShowNotificationOnGlasses } from "../native/notification-sources";
|
||||
import { watchNotificationListenerBinding } from "../native/notification-access";
|
||||
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
||||
import { defaultRingName, setGlassesRingLink } from "../native/ring-status";
|
||||
@@ -26,6 +25,8 @@ import { GLANCE_TILT_MAX_MS, glanceTiltMaxMs } from "./glance-state";
|
||||
import { getBooleanSetting, setBooleanSetting } from "../native/settings-store";
|
||||
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat";
|
||||
import { keyboardService } from "./keyboard/service";
|
||||
import type { KeyEvent } from "./keyboard/keys";
|
||||
import { hasExtractedEvenHubFonts } from "./firmware-builder";
|
||||
import { isHaltedSessionPhase, resumeAutoReconnect, suppressAutoReconnect } from "./reconnect-policy";
|
||||
import { WearRemote, type WearRemoteInputKind } from "./wear-remote";
|
||||
@@ -57,9 +58,9 @@ import { type GlanceEvent } from "./glance-state";
|
||||
import { isPreviewOnlyMode } from "../phone-ui/onboarding-state";
|
||||
import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from "../apps/onboarding/onboarding-progress";
|
||||
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||
import { voiceControlBridge } from "../native/voice-control";
|
||||
import { VoiceReplay, voiceControlBridge } from "../native/voice-control";
|
||||
import { voiceActivity } from "../ui/shell/voice-activity";
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
|
||||
import { prepareFrameDraws } from "../graphics/glyph-wire";
|
||||
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from "./lock-screen";
|
||||
@@ -78,10 +79,11 @@ import { ALL_APPS } from "../apps/all-apps";
|
||||
import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition";
|
||||
import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window";
|
||||
import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence";
|
||||
import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry";
|
||||
import { type ContinuousCaptureOptions, type LayerActions, type TextSettingsEditToggle, type TranscriptionChoice } from "../ui/layers";
|
||||
import { appViewportRect, isOnSwitcherEdge, screenHeight, screenWidth, sidebarStripVisible, windowEdges, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry";
|
||||
import { sameEdges } from "../ui/viewport";
|
||||
import { type ContinuousCaptureOptions, type LayerActions, type MicCount, type TextSettingsEditToggle, type TranscriptionChoice } from "../ui/layers";
|
||||
import { type KeyboardInputSession } from "../ui/shell/keyboard-input";
|
||||
import { appSwitcherPositionSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, type RingConnectionMode, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
|
||||
import { appSwitcherPositionSetting, glassesModelName, glassesTypeSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, effectiveRingConnectionMode, type RingConnectionMode, saveRecordingSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, shouldPopUpNewNotification, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
|
||||
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
|
||||
import {
|
||||
getInstalledEvenHubAppById,
|
||||
@@ -101,6 +103,8 @@ import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_W
|
||||
import { isInstalledPackagePresent } from "../apps/evenhub/updates";
|
||||
import { wearerVerificationOptions } from "../apps/microphones/speakers";
|
||||
import { micSession } from "../apps/microphones/mic-session";
|
||||
import { micArrayCaptureConfig, micControlSupported } from "../apps/microphones/mic-control";
|
||||
import { encodeMicControl } from "../apps/microphones/mic-protocol";
|
||||
import {
|
||||
chargingPreviewMessage,
|
||||
glassesDisplayAsleep,
|
||||
@@ -114,8 +118,6 @@ type ConnectionPhase = "disconnected" | "connecting" | "connected" | "charging"
|
||||
|
||||
export type DashboardSnapshot = {
|
||||
phase: ConnectionPhase;
|
||||
/** Last-known battery levels: the glasses, their charging case, the R1 ring and a Wear OS watch. */
|
||||
batteries: ReturnType<typeof shell.getBatteryLevels>;
|
||||
status: string;
|
||||
displayPreview: ImageSource | null;
|
||||
/**
|
||||
@@ -158,7 +160,7 @@ export type DashboardSnapshot = {
|
||||
previewMode: boolean;
|
||||
/**
|
||||
* The foreground glasses window's EvenHub phone UI, when it can be opened
|
||||
* (the action bar's app-icon button); null otherwise.
|
||||
* (the app-icon button under the mirror); null otherwise.
|
||||
*/
|
||||
evenHubPhoneUi: PhoneUiButton | null;
|
||||
};
|
||||
@@ -198,25 +200,27 @@ const LOW_BATTERY_PERCENT = 5;
|
||||
const EVEN_APP_DETECTED_MESSAGE =
|
||||
"The Even Realities app appears to be running. If Faceclaw has trouble connecting, open its app settings and force stop it.";
|
||||
|
||||
// The launcher grid's app list; also fixes the app ids apps.launch accepts.
|
||||
const LAUNCHABLE_APPS = ALL_APPS.filter((app) => app.showInLauncher !== false);
|
||||
// The launcher grid's app list for the paired glasses; also fixes the app ids apps.launch accepts.
|
||||
const launchableApps = () => ALL_APPS.filter((app) => app.showInLauncher !== false && app.isSupported?.() !== false);
|
||||
|
||||
/**
|
||||
* The global settings window viewport sizes depend on: the display mode's
|
||||
* size (a band's position only moves windows), whether the app switcher
|
||||
* The global settings window viewport sizes depend on: the paired glasses'
|
||||
* screen, the display mode's size (a band's position only moves windows), whether the app switcher
|
||||
* takes width beside windows, height below them or neither (a popup), and
|
||||
* the chrome rows each window loses to it: below them, whether the status
|
||||
* bar joins the row (dropping the top bar); with a popup, whether the
|
||||
* status bar keeps rows of its own and the window frame (where the status
|
||||
* bar goes only moves windows).
|
||||
* bar joins the row (dropping the top bar, for the window frame's top row
|
||||
* if it has a frame); with a popup, whether the status bar keeps rows of its
|
||||
* own and the window frame (where the status bar goes only moves windows).
|
||||
*/
|
||||
function viewportSizesKey(): string {
|
||||
const position = appSwitcherPositionSetting.get();
|
||||
const switcher = position === "bottom" ? `row|${statusBarPositionSetting.get()}`
|
||||
: position === "popup" ? `popup|${statusBarVisibilitySetting.get()}|${windowBorderSetting.get()}`
|
||||
const statusBar = statusBarPositionSetting.get();
|
||||
const framed = windowBorderSetting.get() === "all";
|
||||
const switcher = position === "bottom" ? `row|${statusBar}|${statusBar === "bottom" && framed}`
|
||||
: position === "popup" ? `popup|${statusBarVisibilitySetting.get()}|${framed}`
|
||||
: "strip";
|
||||
const layout = displayLayoutSetting.get();
|
||||
return `${layout.startsWith("band-") ? "band" : layout}|${switcher}`;
|
||||
return `${screenWidth()}x${screenHeight()}|${layout.startsWith("band-") ? "band" : layout}|${switcher}`;
|
||||
}
|
||||
|
||||
/** The global settings that move window surfaces without resizing them. */
|
||||
@@ -225,7 +229,7 @@ function windowPlacementKey(): string {
|
||||
}
|
||||
|
||||
function createInitialDisplayPreview(): ImageSource | null {
|
||||
return grayImageToPreviewSource(new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0));
|
||||
return grayImageToPreviewSource(new GrayImage(screenWidth(), screenHeight(), 0));
|
||||
}
|
||||
|
||||
/** Echo the controller's appendLog() trace to the console. */
|
||||
@@ -414,6 +418,7 @@ class DashboardController {
|
||||
startContinuousVoiceCapture: (client: string, options?: ContinuousCaptureOptions) =>
|
||||
this.startContinuousVoiceCapture(client, options),
|
||||
stopContinuousVoiceCapture: (client: string) => this.stopContinuousVoiceCapture(client),
|
||||
openVoiceReplay: (path: string) => this.openVoiceReplay(path),
|
||||
playBuzzerSequence: (payload: Uint8Array) => this.playBuzzerSequence(payload),
|
||||
};
|
||||
this.sharedActions = sharedActions;
|
||||
@@ -426,11 +431,11 @@ class DashboardController {
|
||||
registerRoamTools((appId) => this.launchApp(appId));
|
||||
// apps.* tools mirror the launcher grid and sidebar (launch, focus, close).
|
||||
registerWindowTools({
|
||||
apps: LAUNCHABLE_APPS,
|
||||
apps: launchableApps,
|
||||
launchApp: (appId) => this.launchApp(appId),
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
});
|
||||
// The action bar's EvenHub phone-UI button hides while that UI is showing.
|
||||
// The main page's EvenHub phone-UI button hides while that UI is showing.
|
||||
onPhoneShownChanged(() => this.emit());
|
||||
shell.configure({
|
||||
actions: {
|
||||
@@ -453,13 +458,15 @@ class DashboardController {
|
||||
this.emit();
|
||||
},
|
||||
openNotificationsWindow: () =>
|
||||
this.openInProcessAppInBackground(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow),
|
||||
this.openInProcessAppInBackground(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID,
|
||||
(options) => createNotificationsAppWindow(options, (section) => this.openSettings(section))),
|
||||
onSwitcherBarHeightChanged: () => this.applySwitcherBarHeight(),
|
||||
onScreenStateChanged: (on) => {
|
||||
// Any wake of the regular UI replaces a showing Glanceboard.
|
||||
if (on) this.glance.dismiss();
|
||||
this.handleScreenStateChanged(on);
|
||||
if (on) this.requestShellRender();
|
||||
if (on) keyboardService.retryNow();
|
||||
this.emit();
|
||||
},
|
||||
});
|
||||
@@ -481,14 +488,18 @@ class DashboardController {
|
||||
this.pushFirmwareDebugFlags();
|
||||
this.pushBrightness();
|
||||
this.syncEvenHubScreenOffSetting();
|
||||
this.syncVoiceWake();
|
||||
// Display mode size first: a reflow repositions every surface too, so
|
||||
// the band-position check after it then has nothing left to do.
|
||||
this.applyDisplayModeIfChanged();
|
||||
const resized = this.applyDisplayModeIfChanged();
|
||||
this.applyVerticalPositionIfChanged();
|
||||
this.applyAppLayoutsIfChanged();
|
||||
const appsResized = this.applyAppLayoutsIfChanged();
|
||||
// Either reflow hands windows their new edges along with their size.
|
||||
if (!resized && !appsResized) this.applyWindowEdgesIfChanged();
|
||||
this.syncAssistantBridgeIfChanged();
|
||||
this.syncLockScreenSettingIfChanged();
|
||||
this.applyGlanceAngleIfChanged();
|
||||
this.syncContinuousRecording();
|
||||
// A Preview color or scaling change should show on the mirror at once,
|
||||
// not at the next composited frame (cheap: floor-limited, dedup'd on
|
||||
// the Java side).
|
||||
@@ -509,8 +520,31 @@ class DashboardController {
|
||||
assistantAvailable: () => shell.isAssistantAvailable(),
|
||||
assistant: text => shell.sendToAssistant(text),
|
||||
});
|
||||
// A keyboard paired through the glasses (CFW keyboard bridge). The
|
||||
// service decides what each key means (app/g2/keyboard/key-router.ts);
|
||||
// this is how it reaches the display and the input pipeline.
|
||||
keyboardService.setHost({
|
||||
ready: () => this.phase === "connected",
|
||||
locked: () => this.glassesLocked,
|
||||
wake: () => {
|
||||
void this.handleInputEvent({
|
||||
kind: "display-wake",
|
||||
containerName: "",
|
||||
eventType: OsEventTypeList.DOUBLE_CLICK_EVENT,
|
||||
eventSource: EventSourceType.TOUCH_EVENT_FORM_DUMMY_NULL,
|
||||
systemExitReasonCode: 0,
|
||||
frameId: 0,
|
||||
}).catch((error) => this.appendLog(`keyboard wake failed: ${this.formatError(error)}`));
|
||||
},
|
||||
gesture: (gesture, origin) => this.injectSyntheticRingInput(gesture, origin),
|
||||
log: (line) => this.appendLog(line),
|
||||
// A keyboard that is switched off would otherwise keep the glasses'
|
||||
// radio looking for it around the clock. One still connected when the
|
||||
// display sleeps stays connected, and its next key wakes the display.
|
||||
shouldReconnect: () => this.phase === "connected" && shell.isScreenOn(),
|
||||
});
|
||||
this.wearRemote = new WearRemote({
|
||||
apps: LAUNCHABLE_APPS,
|
||||
apps: launchableApps,
|
||||
injectInput: (kind) => this.injectSyntheticRingInput(kind, "watch"),
|
||||
launchApp: (appId) => this.launchApp(appId),
|
||||
connect: () => this.connect(),
|
||||
@@ -580,9 +614,9 @@ class DashboardController {
|
||||
* that support resizing receive the new viewport. Other worker windows are
|
||||
* closed and launched again at the new size.
|
||||
*/
|
||||
private applyDisplayModeIfChanged(): void {
|
||||
private applyDisplayModeIfChanged(): boolean {
|
||||
const sizes = viewportSizesKey();
|
||||
if (sizes === this.lastViewportSizes) return;
|
||||
if (sizes === this.lastViewportSizes) return false;
|
||||
this.lastViewportSizes = sizes;
|
||||
// The reflow below reconfigures every surface, covering any move too.
|
||||
this.lastWindowPlacement = windowPlacementKey();
|
||||
@@ -614,6 +648,22 @@ class DashboardController {
|
||||
})().catch((error) => {
|
||||
this.appendLog(`display mode change failed: ${this.formatError(error)}`);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* A setting changed what some windows' edges touch (geometry.ts
|
||||
* windowEdges: a border line, or blank space) without resizing them, as
|
||||
* the Border setting does under a top status bar: those windows lay out
|
||||
* again with the padding their new edges suggest. Their surfaces stay as
|
||||
* they are.
|
||||
*/
|
||||
private applyWindowEdgesIfChanged(): void {
|
||||
for (const window of Array.from(shell.getWindows())) {
|
||||
const laidOut = window.layoutSize?.();
|
||||
if (!laidOut || !window.relayout || sameEdges(laidOut.edges, windowEdges(window.appId))) continue;
|
||||
window.relayout();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -623,14 +673,28 @@ class DashboardController {
|
||||
* are. Windows that can't resize keep the surface they have, the switcher
|
||||
* covering any of it that it now overlaps, rather than being relaunched
|
||||
* for it.
|
||||
*
|
||||
* A window is judged by its own size as well as its surface's rect: other
|
||||
* paths (connect, a launch) configure surfaces from the geometry without
|
||||
* resizing windows, so a window laid out before the row's height was known
|
||||
* (restored at startup, at the default height) can sit behind a surface
|
||||
* that already matches, and its frames would not fit it.
|
||||
*/
|
||||
private applySwitcherBarHeight(): void {
|
||||
void (async () => {
|
||||
for (const window of Array.from(shell.getWindows())) {
|
||||
const rect = JSON.stringify(appViewportRect(window.heightMode, window.appId));
|
||||
if (!window.relayout || this.windowSurfaceRects.get(window.surfaceId) === rect) continue;
|
||||
window.relayout();
|
||||
await this.configureWindowSurface(window.surfaceId, this.isForegroundWindow(window.windowId), window.heightMode);
|
||||
if (!window.relayout) continue;
|
||||
const rect = appViewportRect(window.heightMode, window.appId);
|
||||
const size = window.layoutSize?.();
|
||||
const resized = !!size && (size.width !== rect.width || size.height !== rect.height);
|
||||
if (!resized && this.windowSurfaceRects.get(window.surfaceId) === JSON.stringify(rect)) continue;
|
||||
try {
|
||||
window.relayout();
|
||||
await this.configureWindowSurface(window.surfaceId, this.isForegroundWindow(window.windowId), window.heightMode);
|
||||
} catch (error) {
|
||||
// The rest still need to follow the row.
|
||||
this.appendLog(`switcher height change failed for ${window.windowId}: ${this.formatError(error)}`);
|
||||
}
|
||||
}
|
||||
shell.foregroundWindow()?.requestRender();
|
||||
this.requestShellRender();
|
||||
@@ -648,10 +712,10 @@ class DashboardController {
|
||||
};
|
||||
}
|
||||
|
||||
private applyAppLayoutsIfChanged(): void {
|
||||
private applyAppLayoutsIfChanged(): boolean {
|
||||
const layouts = this.appLayouts();
|
||||
const changed = Object.keys(layouts).filter((appId) => layouts[appId] !== this.lastAppLayouts[appId]);
|
||||
if (!changed.length) return;
|
||||
if (!changed.length) return false;
|
||||
this.lastAppLayouts = layouts;
|
||||
void (async () => {
|
||||
for (const window of Array.from(shell.getWindows())) {
|
||||
@@ -662,6 +726,7 @@ class DashboardController {
|
||||
shell.foregroundWindow()?.requestRender();
|
||||
this.requestShellRender();
|
||||
})().catch((error) => this.appendLog(`app layout change failed: ${this.formatError(error)}`));
|
||||
return true;
|
||||
}
|
||||
|
||||
private applyVerticalPositionIfChanged(): void {
|
||||
@@ -821,8 +886,8 @@ class DashboardController {
|
||||
await communicator.configureSurface(LOCK_SCREEN_SURFACE_ID, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: G2_LENS_WIDTH,
|
||||
height: G2_LENS_HEIGHT,
|
||||
width: screenWidth(),
|
||||
height: screenHeight(),
|
||||
zOrder: 1000,
|
||||
transparency: "opaque",
|
||||
});
|
||||
@@ -930,6 +995,24 @@ class DashboardController {
|
||||
return applied;
|
||||
}
|
||||
|
||||
/** What the communicator was last told about the glasses' own wake word (null: not yet). */
|
||||
private voiceWakeSent: boolean | null = null;
|
||||
|
||||
/**
|
||||
* The glasses' own wake word follows the wake action: on a RayNeo iO, its
|
||||
* "Hey RayNeo" switch (G2 glasses use the wake lease, syncFaceclawWakeLease).
|
||||
*/
|
||||
private syncVoiceWake(communicator = this.communicator): void {
|
||||
if (!communicator) return;
|
||||
const enabled = wakeWordActionSetting.get() !== "off";
|
||||
if (this.voiceWakeSent === enabled) return;
|
||||
this.voiceWakeSent = enabled;
|
||||
void communicator.setVoiceWakeEnabled(enabled).catch((error) => {
|
||||
this.voiceWakeSent = null;
|
||||
this.appendLog(`wake word sync failed: ${this.formatError(error)}`);
|
||||
});
|
||||
}
|
||||
|
||||
private syncEvenHubScreenOffSetting(): void {
|
||||
void this.syncFaceclawWakeLease().catch((error) => {
|
||||
this.appendLog(`wake takeover lease sync failed: ${this.formatError(error)}`);
|
||||
@@ -1047,9 +1130,11 @@ class DashboardController {
|
||||
|
||||
/**
|
||||
* A Direct-mode session may have unpaired the R1 from the glasses (see
|
||||
* GlassesSessionRing.releaseRingFromGlassesLocked). The first session in
|
||||
* another mode asks the glasses to connect to it again; the ring's own
|
||||
* glasses targets were cleared, so it accepts them. Called on connect.
|
||||
* GlassesSessionRing.releaseRingFromGlassesLocked), and a RayNeo iO
|
||||
* session (always Direct) may have cleared the ring's glasses targets.
|
||||
* The first G2 session in another mode asks the glasses to connect to it
|
||||
* again; the ring's own glasses targets were cleared, so it accepts them.
|
||||
* Called on connect.
|
||||
*/
|
||||
private handRingBackToGlassesIfNeeded(): void {
|
||||
if (this.ringModeAtConnect === "direct" || !getBooleanSetting(RING_TAKEN_FROM_GLASSES_KEY, false)) return;
|
||||
@@ -1170,11 +1255,10 @@ class DashboardController {
|
||||
alarmReliabilityMessage: this.alarmReliabilityMessage,
|
||||
previewMode: this.isPreviewDisplayActive(),
|
||||
evenHubPhoneUi: phoneUiButton(),
|
||||
batteries: shell.getBatteryLevels(),
|
||||
};
|
||||
}
|
||||
|
||||
/** Overlay an EvenHub app's phone UI (the action bar's app-icon button). */
|
||||
/** Overlay an EvenHub app's phone UI (the app-icon button under the mirror). */
|
||||
showEvenHubPhoneUi(windowId: string): void {
|
||||
showEvenHubPhoneUi(windowId);
|
||||
}
|
||||
@@ -1237,6 +1321,7 @@ class DashboardController {
|
||||
silentMode: this.silentMode,
|
||||
screenOn: shell.isScreenOn(),
|
||||
battery: this.lastHeadsetBattery,
|
||||
glassesModel: glassesModelName(),
|
||||
caseBattery: batteries.case,
|
||||
caseCharging: batteries.caseCharging,
|
||||
foregroundTitle: shell.getForegroundApp()?.title ?? null,
|
||||
@@ -1409,12 +1494,12 @@ class DashboardController {
|
||||
// is in place before any real render can reach the compositor.
|
||||
this.previewTarget = target;
|
||||
try {
|
||||
await target.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT);
|
||||
await target.configureCompositorScreen(screenWidth(), screenHeight());
|
||||
await target.configureSurface(SHELL_SURFACE_ID, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: G2_LENS_WIDTH,
|
||||
height: G2_LENS_HEIGHT,
|
||||
width: screenWidth(),
|
||||
height: screenHeight(),
|
||||
zOrder: SHELL_SURFACE_Z_ORDER,
|
||||
transparency: "color-key",
|
||||
});
|
||||
@@ -1497,17 +1582,21 @@ class DashboardController {
|
||||
this.refreshEvenAppStatus();
|
||||
this.setPhase("connecting");
|
||||
this.setStatus("Connecting to the glasses...");
|
||||
// "Only via glasses" (the default) means the phone never opens its own
|
||||
// link to the ring: the glasses relay its gestures to us anyway. An empty
|
||||
// address disables every direct-ring code path in the communicator.
|
||||
const ringMode = ringConnectionModeSetting.get();
|
||||
// "Only via glasses" (the G2's default) means the phone never opens its
|
||||
// own link to the ring: the glasses relay its gestures to us anyway. An
|
||||
// empty address disables every direct-ring code path in the communicator.
|
||||
// A RayNeo iO (one link, its address in both arm slots) can't relay a
|
||||
// ring, so with one the phone always takes the ring directly.
|
||||
const ringMode = effectiveRingConnectionMode();
|
||||
this.ringModeAtConnect = ringMode;
|
||||
const glassesType = glassesTypeSetting.get();
|
||||
const ringAddress = ringMode !== "glasses" ? addresses.ring : "";
|
||||
// Direct mode may unpair the ring from the glasses; remember to hand it
|
||||
// back once a later session runs in another mode.
|
||||
if (ringMode === "direct" && ringAddress) setBooleanSetting(RING_TAKEN_FROM_GLASSES_KEY, true);
|
||||
this.appendLog(
|
||||
`Using configured arms: R=${addresses.right} L=${addresses.left}${ringAddress ? ` ring=${ringAddress}` : ""}`,
|
||||
this.appendLog((glassesType === "rayneo-io"
|
||||
? `Using configured RayNeo iO: ${addresses.right}`
|
||||
: `Using configured arms: R=${addresses.right} L=${addresses.left}`) + (ringAddress ? ` ring=${ringAddress}` : ""),
|
||||
);
|
||||
|
||||
let communicator: FaceclawCommunicatorBridge | null = null;
|
||||
@@ -1529,6 +1618,7 @@ class DashboardController {
|
||||
// sync needs the phone role, which the session borrows briefly for
|
||||
// each pull (GlassesSessionRing.beginRingHealthWindowLocked).
|
||||
ringRole: ringMode === "direct" ? "glasses" : "phone",
|
||||
glassesType,
|
||||
});
|
||||
this.communicator = communicator;
|
||||
// Size the compositor before anything else is awaited: windows opened
|
||||
@@ -1536,8 +1626,11 @@ class DashboardController {
|
||||
// configure their surfaces through this.communicator, and the Java call
|
||||
// queue is FIFO, so enqueuing the screen size first guarantees it lands
|
||||
// ahead of them. Mirrors ensurePreviewDisplay.
|
||||
await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT);
|
||||
// The paired glasses' screen: the G2's 640x480 or the RayNeo iO's 540x220.
|
||||
await communicator.configureCompositorScreen(screenWidth(), screenHeight());
|
||||
await communicator.configureBrightness(getBrightnessPreferences());
|
||||
this.voiceWakeSent = null;
|
||||
this.syncVoiceWake(communicator);
|
||||
this.offState = communicator.onStateChange((state) => {
|
||||
if (state.phase !== "connected") resetRingInputFilter();
|
||||
if (isHaltedSessionPhase(state.phase)) {
|
||||
@@ -1706,6 +1799,7 @@ class DashboardController {
|
||||
this.appendLog(`wake takeover lease sync failed: ${this.formatError(error)}`);
|
||||
});
|
||||
this.ensureWearStateTracking();
|
||||
this.syncKeyboardLink();
|
||||
}
|
||||
// The shared session halts on incompatible firmware by itself
|
||||
// ("incompatible-firmware" phase); this only explains why.
|
||||
@@ -1731,8 +1825,8 @@ class DashboardController {
|
||||
await communicator.configureSurface(SHELL_SURFACE_ID, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: G2_LENS_WIDTH,
|
||||
height: G2_LENS_HEIGHT,
|
||||
width: screenWidth(),
|
||||
height: screenHeight(),
|
||||
zOrder: SHELL_SURFACE_Z_ORDER,
|
||||
transparency: "color-key",
|
||||
});
|
||||
@@ -2040,8 +2134,8 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
const at = mirrorToPanelFraction(this.displayPreviewCrop, nx, ny);
|
||||
const x = Math.round(Math.min(1, Math.max(0, at.nx)) * G2_LENS_WIDTH);
|
||||
const y = Math.round(Math.min(1, Math.max(0, at.ny)) * G2_LENS_HEIGHT);
|
||||
const x = Math.round(Math.min(1, Math.max(0, at.nx)) * screenWidth());
|
||||
const y = Math.round(Math.min(1, Math.max(0, at.ny)) * screenHeight());
|
||||
const foreground = shell.foregroundWindow();
|
||||
const stripShown = sidebarStripVisible(shell.getFocus(), foreground?.appId);
|
||||
this.appendLog(`mirror tap at ${x},${y}`);
|
||||
@@ -2139,12 +2233,14 @@ class DashboardController {
|
||||
|
||||
private startContinuousVoiceCapture(client: string, options: ContinuousCaptureOptions = {}): void {
|
||||
this.continuousClients.set(client, options);
|
||||
this.syncContinuousRecording();
|
||||
this.retargetContinuousCapture();
|
||||
this.ensureContinuousCapture(true);
|
||||
}
|
||||
|
||||
private stopContinuousVoiceCapture(client: string): void {
|
||||
if (!this.continuousClients.delete(client)) return;
|
||||
this.syncContinuousRecording();
|
||||
if (this.continuousClients.size > 0) {
|
||||
// The window that needed its own provider may be the one that left.
|
||||
this.retargetContinuousCapture();
|
||||
@@ -2170,6 +2266,18 @@ class DashboardController {
|
||||
void this.beginVoiceCapture("continuous").finally(() => this.syncContinuousClaim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Record to the first client's requested name while Save Recording is on
|
||||
* (it is toggled from those windows' menus, so this also runs when a
|
||||
* setting changes).
|
||||
*/
|
||||
private syncContinuousRecording(): void {
|
||||
const name = saveRecordingSetting.get()
|
||||
? Array.from(this.continuousClients.values(), (options) => options.recordAs).find(Boolean) ?? ""
|
||||
: "";
|
||||
voiceControlBridge.recordTo(name);
|
||||
}
|
||||
|
||||
/** Move a running continuous capture onto the provider its windows now call for. */
|
||||
private retargetContinuousCapture(): void {
|
||||
voiceControlBridge.retargetContinuousCapture(this.voiceCaptureOptions(this.communicator));
|
||||
@@ -2183,6 +2291,30 @@ class DashboardController {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* How many of the glasses' microphones the continuous-capture windows call
|
||||
* for (the most any asks for), where the glasses can stream them
|
||||
* separately: G2 with the microphone channel in use. Otherwise 1, the
|
||||
* mixed stream.
|
||||
*/
|
||||
private continuousMics(): MicCount {
|
||||
if (glassesTypeSetting.get() !== "g2" || !micControlSupported()) return 1;
|
||||
let mics: MicCount = 1;
|
||||
for (const options of this.continuousClients.values()) {
|
||||
if ((options.mics ?? 1) > mics) mics = options.mics!;
|
||||
}
|
||||
return mics;
|
||||
}
|
||||
|
||||
/**
|
||||
* A recording as a capture of its own (see VoiceReplay), transcribed with
|
||||
* the provider and processing settings a microphone capture would get.
|
||||
* "My voice only" is left out: a recording is of whoever spoke.
|
||||
*/
|
||||
private openVoiceReplay(path: string): VoiceReplay {
|
||||
return new VoiceReplay(path, () => ({ ...this.voiceCaptureOptions(null), speakerVerification: undefined }));
|
||||
}
|
||||
|
||||
/**
|
||||
* Continuous capture's claim on the mic (the Microphones app and EvenHub
|
||||
* mic apps stand down while it is held) follows whether the capture really
|
||||
@@ -2208,9 +2340,26 @@ class DashboardController {
|
||||
// Otherwise only Whisper server is told a language (its own setting).
|
||||
const choice = this.transcriptionChoice();
|
||||
const provider = choice?.provider ?? voiceProviderSetting.get();
|
||||
// Separate microphones only come from connected glasses.
|
||||
const mics = communicator ? this.continuousMics() : 1;
|
||||
const micConfig = mics === 1 ? null : micArrayCaptureConfig(mics);
|
||||
return {
|
||||
communicator: communicator?.getNativeCommunicator() ?? null,
|
||||
usePhoneMic: communicator === null,
|
||||
micArray: micConfig ? { configure: encodeMicControl("configure", micConfig) } : null,
|
||||
// Stored in a recording of the capture (see Save Recording).
|
||||
captureInfo: {
|
||||
glasses: communicator ? glassesModelName() : null,
|
||||
mics,
|
||||
micConfig: micConfig && {
|
||||
source: micConfig.source,
|
||||
channelMask: micConfig.channelMask,
|
||||
codec: micConfig.codec,
|
||||
format: micConfig.format,
|
||||
sampleRateHz: micConfig.sampleRateHz,
|
||||
directionDetection: micConfig.beamform,
|
||||
},
|
||||
},
|
||||
provider,
|
||||
language: choice ? choice.language : provider === "whisper-server" ? whisperServerLanguageSetting.get() : "",
|
||||
elevenLabsApiKey: elevenLabsApiKeySetting.get(),
|
||||
@@ -2219,7 +2368,6 @@ class DashboardController {
|
||||
whisperServerUrl: whisperServerUrlSetting.get(),
|
||||
whisperServerModel: whisperServerModelSetting.get(),
|
||||
whisperServerApiKey: whisperServerApiKeySetting.get(),
|
||||
saveRecording: saveVoiceRecordingsSetting.get(),
|
||||
// "My voice only" (Microphones app): verify command utterances
|
||||
// against the enrolled wearer voice-print; non-matching speakers'
|
||||
// finals are suppressed by the bridge.
|
||||
@@ -2530,6 +2678,23 @@ class DashboardController {
|
||||
applyGlanceAngle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell the keyboard bridge whether the session can host a keyboard: a G2
|
||||
* on firmware with the bridge, connected and in use. While the glasses
|
||||
* charge in their case the link stays up but nobody is typing, so the
|
||||
* firmware is told to let the keyboard go.
|
||||
*/
|
||||
private syncKeyboardLink(): void {
|
||||
const up =
|
||||
this.phase === "connected" &&
|
||||
this.customFirmwareConfirmed &&
|
||||
glassesTypeSetting.get() === "g2";
|
||||
// Charging, or a disconnect the user asked for: the link is still up, so
|
||||
// the DISABLE gets out ahead of everything the teardown sends.
|
||||
if (up) keyboardService.linkReady();
|
||||
else keyboardService.linkLost(this.phase === "charging" || this.phase === "disconnecting");
|
||||
}
|
||||
|
||||
/**
|
||||
* The Glanceboard event a sleep-time input means, or null when the shell
|
||||
* should see the input as usual. A display-wake carries the head-tilt
|
||||
@@ -2636,11 +2801,7 @@ class DashboardController {
|
||||
setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible),
|
||||
removeSurface: (surfaceId) => this.removeWindowSurface(surfaceId),
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
openSettings: (section) => {
|
||||
void this.launchApp("settings", { section }).catch((error) => {
|
||||
this.appendLog(`settings launch failed: ${this.formatError(error)}`);
|
||||
});
|
||||
},
|
||||
openSettings: (section) => this.openSettings(section),
|
||||
startTextSettingEdit: (settingId) => {
|
||||
const setting = getStringSettingById(settingId);
|
||||
if (setting) {
|
||||
@@ -2650,6 +2811,7 @@ class DashboardController {
|
||||
}
|
||||
},
|
||||
endTextSettingEdit: () => this.endTextSettingEdit(),
|
||||
unhandledKey: (event) => keyboardService.handleUnhandledKey(event as KeyEvent),
|
||||
});
|
||||
this.appHosts.set(appId, host);
|
||||
return host;
|
||||
@@ -2683,10 +2845,11 @@ class DashboardController {
|
||||
if (!this.openAppsRestored || this.suppressOpenAppsPersist) return;
|
||||
const open: string[] = [];
|
||||
for (const window of shell.getWindows()) {
|
||||
if (window.appId === "launcher") continue;
|
||||
if (window.appId === "launcher" || window.restoresApp === false) continue;
|
||||
if (!open.includes(window.appId)) open.push(window.appId);
|
||||
}
|
||||
savePersistedOpenApps({ open, foreground: shell.foregroundWindow()?.appId ?? null });
|
||||
const foreground = shell.foregroundWindow();
|
||||
savePersistedOpenApps({ open, foreground: foreground && foreground.restoresApp !== false ? foreground.appId : null });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2724,6 +2887,13 @@ class DashboardController {
|
||||
this.requestShellRender();
|
||||
}
|
||||
|
||||
/** Open or focus the Settings app, optionally on a section (by label). */
|
||||
private openSettings(section?: string): void {
|
||||
void this.launchApp("settings", { section }).catch((error) => {
|
||||
this.appendLog(`settings launch failed: ${this.formatError(error)}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch an app by id: the launcher grid, assistant tools, window-menu
|
||||
* Settings item, and open-apps restore all come through here. Apps with an
|
||||
@@ -2940,7 +3110,7 @@ class DashboardController {
|
||||
// The tray shows the notification at once; its popup waits out a short
|
||||
// delay, so one withdrawn right away (see NotificationPopupDelay) never shows.
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
if (notification && shouldShowNotificationOnGlasses(notification.packageName)) {
|
||||
if (notification && shouldPopUpNewNotification(notification.packageName)) {
|
||||
this.notificationPopups.post(notificationKey, notificationPopupDelayMs(notification));
|
||||
}
|
||||
this.requestShellRender();
|
||||
@@ -2962,7 +3132,7 @@ class DashboardController {
|
||||
private popUpAndroidNotification(notificationKey: string): void {
|
||||
try {
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
if (!notification || !shouldShowNotificationOnGlasses(notification.packageName)) return;
|
||||
if (!notification || !shouldPopUpNewNotification(notification.packageName)) return;
|
||||
// New notifications open a shell modal over the app viewport; if the
|
||||
// screen was off, wake for it and go back to sleep when it is closed.
|
||||
// Waking while already on would steal focus, so only wake from sleep.
|
||||
@@ -3007,6 +3177,7 @@ class DashboardController {
|
||||
private setPhase(phase: ConnectionPhase): void {
|
||||
if (this.phase === phase) return;
|
||||
this.phase = phase;
|
||||
this.syncKeyboardLink();
|
||||
if (phase !== "connected" && phase !== "charging") {
|
||||
shell.setBatteryLevels({ ring: null, ringCharging: null });
|
||||
}
|
||||
|
||||
@@ -30,7 +30,10 @@ export { bytesToHex, hexToBytes };
|
||||
/** Even Realities' manufacturer-data company identifier (`"ER"` read little-endian). */
|
||||
export const EVEN_COMPANY_IDENTIFIER = 0x5245;
|
||||
|
||||
export type EvenDeviceRole = "left" | "right" | "ring";
|
||||
export type EvenDeviceRole = "left" | "right" | "ring" | "rayneo-io";
|
||||
|
||||
/** The firmware an Even G1 arm advertises: Even's, or Faceclaw's g1v0 (in safe mode: only updates work). */
|
||||
export type G1Firmware = "stock" | "g1v0" | "g1v0-safe";
|
||||
|
||||
/** True when the manufacturer data opens with Even's `"ER"` marker. */
|
||||
export function hasEvenManufacturerSignature(mfg: Uint8Array): boolean {
|
||||
@@ -71,6 +74,61 @@ export function extractG2AdvertisementFlag(mfg: Uint8Array): number | null {
|
||||
return mfg[22]!;
|
||||
}
|
||||
|
||||
/**
|
||||
* ## Even G1 arm
|
||||
*
|
||||
* On Even's firmware the name is `Even G1_<token>_<L|R>_<hex6>` and the manufacturer data `[01 right | 02 left]` +
|
||||
* the ASCII serial, zero-padded (the first two bytes read as a "company id" that differs per arm). On g1v0 the name
|
||||
* is `G1CFW_<L|R>_<hex6>` with the same digits and address as stock's, and the scan response holds company 0xFFFF, a
|
||||
* format byte (2), a flags byte (bit 3: safe mode), the firmware's version in three bytes, and the pair ID in ASCII:
|
||||
* the frame serial, the same on both arms. Before 0.5.66: `G1v0_L` or `G1v0_R` (`G1v0S_…` in safe mode), format 1, no
|
||||
* version.
|
||||
*/
|
||||
export function g1FromName(name: string | null | undefined): { side: "left" | "right"; firmware: G1Firmware } | null {
|
||||
if (!name) return null;
|
||||
const custom = /^G1CFW_([LR])_[0-9A-F]{6}$/i.exec(name.trim());
|
||||
if (custom) return { side: custom[1]!.toUpperCase() === "L" ? "left" : "right", firmware: "g1v0" };
|
||||
const g1v0 = /^G1V0(S?)_([LR])$/i.exec(name.trim());
|
||||
if (g1v0) {
|
||||
return {
|
||||
side: g1v0[2]!.toUpperCase() === "L" ? "left" : "right",
|
||||
firmware: g1v0[1] ? "g1v0-safe" : "g1v0",
|
||||
};
|
||||
}
|
||||
if (!/^EVEN G1_/i.test(name.trim())) return null;
|
||||
const side = g2SideFromName(name);
|
||||
return side ? { side, firmware: "stock" } : null;
|
||||
}
|
||||
|
||||
/** What g1v0's pair record says beyond the serial; null when there is no record of a format that says it. */
|
||||
export function parseG1Record(mfg: Uint8Array): { version: string; safeMode: boolean } | null {
|
||||
if (mfg.length < 7 || mfg[0] !== 0xff || mfg[1] !== 0xff || mfg[2] !== 2) return null;
|
||||
return { version: `${mfg[4]}.${mfg[5]}.${mfg[6]}`, safeMode: (mfg[3]! & 8) !== 0 };
|
||||
}
|
||||
|
||||
/** The pair's serial from an arm's manufacturer data, on either firmware; null when absent. */
|
||||
export function extractG1Serial(mfg: Uint8Array, firmware: G1Firmware): string | null {
|
||||
const record = mfg.length >= 4 && mfg[0] === 0xff && mfg[1] === 0xff;
|
||||
const start = firmware === "stock" ? 1 : record && mfg[2] === 2 ? 7 : record && mfg[2] === 1 ? 4 : -1;
|
||||
if (start < 0 || mfg.length <= start) return null;
|
||||
let serial = "";
|
||||
for (let i = start; i < mfg.length; i++) {
|
||||
const byte = mfg[i]!;
|
||||
if (byte === 0) break;
|
||||
if (byte < 0x20 || byte > 0x7e) return null;
|
||||
serial += String.fromCharCode(byte);
|
||||
}
|
||||
return serial.length ? serial : null;
|
||||
}
|
||||
|
||||
/** RayNeo iO manufacturer data (company 0x5716, little-endian first): `[07][MAC x6]...`, outside pairing mode. */
|
||||
export const RAYNEO_COMPANY_IDENTIFIER = 0x5716;
|
||||
|
||||
export function extractRayNeoMac(mfg: Uint8Array): string | null {
|
||||
if (mfg.length < 9 || mfg[0] !== 0x16 || mfg[1] !== 0x57 || mfg[2] !== 0x07) return null;
|
||||
return Array.from(mfg.subarray(3, 9), (byte) => byte.toString(16).padStart(2, "0").toUpperCase()).join(":");
|
||||
}
|
||||
|
||||
/**
|
||||
* Which side a G2 temple advertisement belongs to, from the local name.
|
||||
* Null when the name carries neither or both markers.
|
||||
@@ -210,6 +268,10 @@ export type EvenAdvertisement = {
|
||||
manufacturerData: string;
|
||||
/** Why an otherwise Even-looking advertisement could not be fully parsed. */
|
||||
note: string | null;
|
||||
/** Set on an Even G1 arm (role left or right): its firmware. */
|
||||
g1Firmware?: G1Firmware;
|
||||
/** The version g1v0 advertises (0.5.66+); absent when it doesn't say. */
|
||||
g1Version?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -230,8 +292,58 @@ export function classifyAdvertisement(raw: RawAdvertisement): EvenAdvertisement
|
||||
const hasSignature = hasEvenManufacturerSignature(mfg);
|
||||
const looksLikeRing = /^EVEN R1([_ ]|$)/.test(upper);
|
||||
const looksLikeG2 = upper.includes("G2");
|
||||
const rayneoMac = extractRayNeoMac(mfg);
|
||||
const looksLikeRayNeo = upper.startsWith("RAYNEO") || rayneoMac !== null;
|
||||
|
||||
if (!looksLikeRing && !looksLikeG2 && !hasSignature) return null;
|
||||
const g1 = g1FromName(name);
|
||||
if (g1) {
|
||||
const serial = extractG1Serial(mfg, g1.firmware);
|
||||
const record = g1.firmware === "stock" ? null : parseG1Record(mfg);
|
||||
return {
|
||||
address: normalizeMacAddress(raw.address),
|
||||
name,
|
||||
role: g1.side,
|
||||
serial,
|
||||
embeddedMac: null,
|
||||
embeddedMacMismatch: false,
|
||||
flag: null,
|
||||
ringSerialSuffix: null,
|
||||
rssi: raw.rssi,
|
||||
txPower: raw.txPower,
|
||||
connectable: raw.connectable,
|
||||
bonded: raw.bonded,
|
||||
source: raw.source,
|
||||
seenAtMs: raw.seenAtMs,
|
||||
manufacturerData: raw.manufacturerData ?? "",
|
||||
note: serial ? null : "no serial in the advertisement (it may still be arriving)",
|
||||
g1Firmware: record?.safeMode ? "g1v0-safe" : g1.firmware,
|
||||
...(record ? { g1Version: record.version } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
if (!looksLikeRing && !looksLikeG2 && !hasSignature && !looksLikeRayNeo) return null;
|
||||
|
||||
if (looksLikeRayNeo && !hasSignature) {
|
||||
const address = normalizeMacAddress(raw.address);
|
||||
return {
|
||||
address,
|
||||
name,
|
||||
role: "rayneo-io",
|
||||
serial: null,
|
||||
embeddedMac: rayneoMac,
|
||||
embeddedMacMismatch: !!rayneoMac && rayneoMac !== address,
|
||||
flag: null,
|
||||
ringSerialSuffix: null,
|
||||
rssi: raw.rssi,
|
||||
txPower: raw.txPower,
|
||||
connectable: raw.connectable,
|
||||
bonded: raw.bonded,
|
||||
source: raw.source,
|
||||
seenAtMs: raw.seenAtMs,
|
||||
manufacturerData: raw.manufacturerData ?? "",
|
||||
note: null,
|
||||
};
|
||||
}
|
||||
|
||||
const base = {
|
||||
address: normalizeMacAddress(raw.address),
|
||||
|
||||
@@ -26,7 +26,7 @@ export type FirmwareInfo = {
|
||||
};
|
||||
|
||||
/** The Faceclaw firmware revision this build of the app needs. */
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 38;
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 39;
|
||||
|
||||
const FACECLAW_PREFIX = "Faceclaw/";
|
||||
const LEGACY_PREFIX = "EVENCFW";
|
||||
@@ -117,6 +117,11 @@ export function firmwareIncompatibilityMessage(info: FirmwareInfo): string | nul
|
||||
const versionsText = `L=${info.leftVersion || "unknown"} R=${info.rightVersion || "unknown"}`;
|
||||
const required = `Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}`;
|
||||
|
||||
// A G1's session names what the arms run instead of a revision (GlassesSessionG1.kt).
|
||||
if (extension.kind === "none" && reportedVersions.some((version) => /^(g1v0|Even's firmware)/.test(version))) {
|
||||
return `These G1 glasses run ${versionsText}. This version of Faceclaw needs the firmware it comes with: install it to continue.`;
|
||||
}
|
||||
|
||||
switch (extension.kind) {
|
||||
case "faceclaw":
|
||||
if (extension.version >= REQUIRED_FACECLAW_FIRMWARE_VERSION) return null;
|
||||
|
||||
@@ -19,12 +19,42 @@ export type FirmwarePatchSet = {
|
||||
export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
"base": "g2_2.3.0.24.bin",
|
||||
"baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979",
|
||||
"outputSha256": "4aac023f49ec6965186d9106e328a516f20a2915ab400d561f662f984023a432",
|
||||
"outputSha256": "e43be9d04031d261574f96d45cb1e0edd67bfb39f0213f323ff82d8560ae454d",
|
||||
"patches": [
|
||||
{
|
||||
"offset": 1379947,
|
||||
"old": "0ef009fa",
|
||||
"new": "0af3b8fb",
|
||||
"desc": "DM callback allocation: retain keyboard lifecycle on queue exhaustion"
|
||||
},
|
||||
{
|
||||
"offset": 1380027,
|
||||
"old": "0ef0e1f9",
|
||||
"new": "0af316fb",
|
||||
"desc": "ATT callback allocation: expose lost keyboard events as relay gaps"
|
||||
},
|
||||
{
|
||||
"offset": 2649417,
|
||||
"old": "0a681279",
|
||||
"new": "d4f167fb",
|
||||
"desc": "SMP Pairing I/O capability: KeyboardDisplay only for owned keyboard"
|
||||
},
|
||||
{
|
||||
"offset": 2651811,
|
||||
"old": "0a681279",
|
||||
"new": "d3f1bafe",
|
||||
"desc": "SMP Pairing I/O capability: KeyboardDisplay only for owned keyboard"
|
||||
},
|
||||
{
|
||||
"offset": 1385975,
|
||||
"old": "38b584b0",
|
||||
"new": "08f3d4be",
|
||||
"desc": "BLE app dispatcher: isolate explicitly enabled keyboard connection"
|
||||
},
|
||||
{
|
||||
"offset": 1045797,
|
||||
"old": "38b584b0",
|
||||
"new": "5cf371bb",
|
||||
"new": "5df31bb9",
|
||||
"desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease"
|
||||
},
|
||||
{
|
||||
@@ -36,49 +66,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
{
|
||||
"offset": 1437923,
|
||||
"old": "fef788ff",
|
||||
"new": "fdf2a2f8",
|
||||
"new": "fdf24cfe",
|
||||
"desc": "cfw_ancs_event"
|
||||
},
|
||||
{
|
||||
"offset": 1437951,
|
||||
"old": "fef7e8fb",
|
||||
"new": "fcf2d0fd",
|
||||
"new": "fdf27afb",
|
||||
"desc": "cfw_ancs_open"
|
||||
},
|
||||
{
|
||||
"offset": 1437969,
|
||||
"old": "fef7e7fb",
|
||||
"new": "fcf2ebfd",
|
||||
"new": "fdf295fb",
|
||||
"desc": "cfw_ancs_close"
|
||||
},
|
||||
{
|
||||
"offset": 1388387,
|
||||
"old": "39694d00",
|
||||
"new": "25597d00",
|
||||
"new": "79647d00",
|
||||
"desc": "connection-bound ANCS EUS write callback"
|
||||
},
|
||||
{
|
||||
"offset": 971497,
|
||||
"old": "fff717f8",
|
||||
"new": "6ef3a1fc",
|
||||
"new": "6ff34bfa",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 971845,
|
||||
"old": "fef769ff",
|
||||
"new": "6ef3f3fb",
|
||||
"new": "6ff39df9",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 979579,
|
||||
"old": "fdf74ef8",
|
||||
"new": "6cf3d8fc",
|
||||
"new": "6df382fa",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 1428895,
|
||||
"old": "f8f76cfd",
|
||||
"new": "fbf250fe",
|
||||
"new": "fbf2acfa",
|
||||
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
|
||||
},
|
||||
{
|
||||
@@ -102,7 +132,7 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
{
|
||||
"offset": 1058287,
|
||||
"old": "fff754fb",
|
||||
"new": "59f342fb",
|
||||
"new": "5af3ecf8",
|
||||
"desc": "bl ble_classify_link (only a <= 11.25 ms / latency-0 link is 'already fast')"
|
||||
},
|
||||
{
|
||||
@@ -114,145 +144,145 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
{
|
||||
"offset": 1248487,
|
||||
"old": "d4f712fb",
|
||||
"new": "29f344fd",
|
||||
"new": "29f396f8",
|
||||
"desc": "bl settings_send_wrapper (append caps field 100)"
|
||||
},
|
||||
{
|
||||
"offset": 1246183,
|
||||
"old": "f5f7e2f8",
|
||||
"new": "29f3e8fe",
|
||||
"new": "29f394fa",
|
||||
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
|
||||
},
|
||||
{
|
||||
"offset": 975219,
|
||||
"old": "02f083fc",
|
||||
"new": "6bf34aff",
|
||||
"new": "6bf3f6fa",
|
||||
"desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 975411,
|
||||
"old": "02f023fc",
|
||||
"new": "6bf3b8ff",
|
||||
"new": "6bf364fb",
|
||||
"desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 975103,
|
||||
"old": "07f085fb",
|
||||
"new": "6df3a6fc",
|
||||
"new": "6ef350fa",
|
||||
"desc": "bl headup_gate (idle input gate: tap/long/release -> field-102 events, stock result kept)"
|
||||
},
|
||||
{
|
||||
"offset": 830549,
|
||||
"old": "19f03efb",
|
||||
"new": "90f37bfe",
|
||||
"new": "91f325fc",
|
||||
"desc": "bl gesture_ring_press_mode (ring touch-down 14, head up 12 / down 15; stock dispatch preserved)"
|
||||
},
|
||||
{
|
||||
"offset": 830795,
|
||||
"old": "1ff071ff",
|
||||
"new": "90f334fe",
|
||||
"new": "91f3defb",
|
||||
"desc": "bl gesture_press (owned event 9 before stock menu gate)"
|
||||
},
|
||||
{
|
||||
"offset": 830887,
|
||||
"old": "1ff043ff",
|
||||
"new": "90f32efe",
|
||||
"new": "91f3d8fb",
|
||||
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
|
||||
},
|
||||
{
|
||||
"offset": 831763,
|
||||
"old": "1ff08dfd",
|
||||
"new": "90f364fc",
|
||||
"new": "91f30efa",
|
||||
"desc": "bl gesture_release (owned event 10 before stock menu gate)"
|
||||
},
|
||||
{
|
||||
"offset": 1570209,
|
||||
"old": "7fb50600",
|
||||
"new": "dbf2d5b8",
|
||||
"new": "daf227bc",
|
||||
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
|
||||
},
|
||||
{
|
||||
"offset": 1051545,
|
||||
"old": "f6f787f9",
|
||||
"new": "55f3c9f8",
|
||||
"new": "54f31dfe",
|
||||
"desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1051861,
|
||||
"old": "f6f7e9f8",
|
||||
"new": "55f32bf8",
|
||||
"new": "54f37ffd",
|
||||
"desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1262237,
|
||||
"old": "d9f7d0fd",
|
||||
"new": "25f3bffd",
|
||||
"new": "25f3c3f9",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1262337,
|
||||
"old": "d9f79efd",
|
||||
"new": "25f38dfd",
|
||||
"new": "25f391f9",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1309405,
|
||||
"old": "67f02dff",
|
||||
"new": "19f35bfe",
|
||||
"new": "19f35ffa",
|
||||
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
|
||||
},
|
||||
{
|
||||
"offset": 1307865,
|
||||
"old": "fff7c9fb",
|
||||
"new": "1af3ddf9",
|
||||
"new": "19f3e1fd",
|
||||
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
|
||||
},
|
||||
{
|
||||
"offset": 1300937,
|
||||
"old": "00210170",
|
||||
"new": "1bf3bbfe",
|
||||
"new": "1bf3bffa",
|
||||
"desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)"
|
||||
},
|
||||
{
|
||||
"offset": 4537963,
|
||||
"old": "",
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc7b04ff29c5bc2f2290b4ff6ff754ff47d7400274ff0010847f67f7a7f261191cde90f3007e000bf48f2a351c0f24501bff32f8f8847dbf80090bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14068b0f57d7f00f096804ff47d7006f004fd002800f0cb814ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff010160446080f80e80a0f8c4a180f8c361058445848584c584058545858585c585058645868586c586058745878587c5875be8001f49457ff452afbff35f8f4be8000121b15be8001f4945f8d047e74ff47d7800274ff0010a47f67f797f2607e000bf48f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f098804ff47d7006f042fc002800f009814ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff010160c0f8048080f80ea0a0f8c49180f8c361058445848584c584058545858585c585058645868586c586058745878587c5875be8001fa1427ff451afbff35f8f00bf4be8000121b15be8001fa142f8d045e7d0f8948242f6f76ac0f2440ab8f1000fbff35f8f4ad00af19a0240464ff0ff314ff0ff379047002840f090811198002861d010980778a7f1170002285bd811980c2840f07d81109c6078a178084300f07781e0780138022800f27281a279e379667a42ea0322237a207942ea034242ea0663a27ae67a6179192f42ea062200f0de81172f40f0f481002a40f05981fe2800f0b086812800f0a386002840f0b68651ea030040f04c8100f000bf04460020d047e8b104f5257151e8008fb8f1000f0cd1bff35f8f00bf41e80002002a00f0e58051e8008fb8f1000ff5d00021bff32f8f0029bff35f8f00f0d480804692e74ff0ff372ae147f6df40c0f24f00cdf83880804700274ff47d784ff0010947f67f744ff07f0a1397adf8507007e000bf48f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f09f804ff47d7006f0f8fa002800f097804ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff010160c0f8048080f80e90a0f8c44180f8c3a1058445848584c584058545858585c585058645868586c586058745878587c5875be8001fb1427ff451afbff35f8f00bf4be8000159b15be8001fb142f8d045e70af5a9718847bce6012120e70020109f42f6f76addf83880c0f2440a9fb1119989b1397801f07f02b2f10b0418bf0124b2f11c0618bf01268a2924dc0b2926d01c2924d03ce00f9012a803f045ff4ff0ff3701249df85100002812a818bf4ff0ff3704f05ff8054644b99df8500028b94af21f30c0f247004c3080470f98002818bf056114e09c2901d08b2918d168b14af21f31c0f247010746884797f8681121b997f824133846109f49b14ff0ff370af5807140468847384647b0bde8f08f0f9012a8344003f007ff387800f07f061e2e05d11199384607f094fe0746b9e71d2e00f07e0018bf1a2819d1dbf800100322cff680720a40b2f1005fbff35f8f12d10a684ef66c63ccf2ff039a420bd14a68b2f57d7f04bf002281f8a12303e0a6f115010129e1d90f2e2cdd142e40f31082152e00f05d83162e00f069831f2e40f02c841198012840f0fc8438781f2840f0f88445f64d55c0f24605a847012841f0828140f24c71c0f2010105f5e2777944e02029220023b8470746002818bf4ff0ff3766e7092e00f3fe80052e00f03e82072e40f0028400274ff0010947f67f764ff07f080d943ae000294ff0ff3708bf002b7ff47caf1328bff479af102a4ff0000138bf01211228b2fa82f218bf510901297ff46caf0Line truncated
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc7b04ff29c5bc2f2290b4ff6ff754ff47e740027012647f67f784ff07f0a1191cde90f3007e000bf48f2a351c0f24501bff32f8f8847dbf80090bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0ad101684ef66c62ccf2ff02914203d14068b0f57e7f3ed04ff47e7006f017fa002800f01b816ff0070142180831b1f57c7f177257729772d772177357739773d773f2d14ef66c61ccf2ff01016044608673a0f8c48180f8c3a1058445848584c584058545858585c585058645868586c586058745878587c5875be8001f4945a9d1bff35f8f4be8000121b15be8001f4945f8d09fe74ff47e7800264ff0010a47f67f797f2707e000bf48f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0ad101684ef66c62ccf2ff02914203d14168b1f57e7f40d04ff47e7006f0adf9002800f0b1806ff0070142180831b1f57c7f167256729672d672167356739673d673f2d14ef66c61ccf2ff010160c0f8048080f80ea0a0f8c49180f8c371058445848584c584058545858585c585058645868586c586058745878587c5875be8001fa142a7d1bff35f8f4be8000121b15be8001fa142f8d09de7d0f8948242f6f76ac0f2440ab8f1000fbff35f8f4ad00af19a0240464ff0ff314ff0ff379047002840f036811198002860d010980778a7f1170002285ad811980c2840f02381109c6078a178084300f01d81e0780138022800f21881a279e379667a42ea0322237a207942ea034242ea0663a27ae67a6179192f42ea062200f08481172f40f09a81002a40f0ff80fe2800f0b285812800f0a585002840f0b88551ea030040f0f28000f002be04460020d047e0b104f5257151e8008fb8f1000f0cd1bff35f8f00bf41e80002002a00f08b8051e8008fb8f1000ff5d00021bff32f8f0029bff35f8f7ad0804693e74ff0ff37d1e047f6df40c0f24f00cdf83880804700274ff47e784ff0010947f67f764ff07f0a1397adf8507006e048f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0ad101684ef66c62ccf2ff02914203d14168b1f57e7f47d04ff47e7006f0bdf8002840d06ff0070100bf42180831b1f57c7f177257729772d772177357739773d773f2d14ef66c61ccf2ff010160c0f8048080f80e90a0f8c46180f8c3a1058445848584c584058545858585c585058645868586c586058745878587c5875be8001fa142a7d1bff35f8f4be8000159b15be8001fa142f8d09de70af5a971884716e701217ae70020109f42f6f76addf83880c0f2440a9fb1119989b1397801f07f02b2f10b0418bf0124b2f11c0618bf01268a2924dc0b2926d01c2924d03ce00f9012a803f055fd4ff0ff3701269df85100002812a818bf4ff0ff3703f019fe054646b99df8500028b94af21f30c0f247004c3080470f98002818bf056114e09c2901d08b2918d168b14af21f31c0f247010746884797f8681121b997f824133846109f49b14ff0ff370af5807140468847384647b0bde8f08f0f9012a8264003f017fd387800f07f041e2c05d11199384608f048fd0746b9e71d2c00f07e0018bf1a2819d1dbf800100322cff680720a40b2f1005fbff35f8f12d10a684ef66c63ccf2ff039a420bd14a68b2f57e7f04bf002281f8a12303e0a4f115010129e1d90f2c2cdd142c40f3bb81152c00f0ba82162c00f0c4821f2c40f088831198012840f0588438781f2840f0548445f64d54c0f24604a047012841f0388041f21051c0f2010104f5e2777944e02029220023b8470746002818bf4ff0ff3766e7092c00f3a980052c00f0f081072c40f05e830d9600264ff0010947f67f744ff07f083ae000294ff0ff3708bf002b7ff47caf1328bff479af102a4ff0000138bf01211228b2fa82f218bf510901297ff46caf00f07abc182f4ff0ff3708bf00297ff463af002b7ff460af352800f36483002800f05284012800f05e84312840f05084082abff451af00f05fbc48f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57e7f00f060854ff47e7005f051ff002800f058856ff0070142180831b1f57c7f167256729672d672167356739673d673f2d14ef66c61ccf2ff0101604ff47e71416080f80e90a0f8c44180f8c381058445848584c584058545858585c585058645868586c586058745878587c5875be8001fb942a5d1bff35f8f4be80001002900f021855be8001fb942f6d099e70a2c00f0bc810b2c40f0b582dbf800500320cff680702840b0f1005fbff35f8f40f0698728684ef66c61ccf2ff01884240f0618768680027b0f57e7f7ff49baed5f8900342f68d49c0f244090028a5f8a07318bfc8470021a5f8d013c5f8cc13c5f86c1185f86a1185f86811d5f87001c5f8641185f86911c5f87011c5f82013a5f88c1320b148f2a351c0f245018847d5f89802002818bf03f099fd0024c5f89842d5f8880320b148f2a351c0f245018847c5f88843a5f88c43686ca5f8484040b1c847686c09f166018847002804bf002068644bf63940c0f251008047dbf800400320cff680702040b0f1005fbff35f8f48d120684ef66c61ccf2ff01884241d16068b0f57e7f3dd194f87b014Line truncated
|
||||
"desc": "append injected blobs to main-app payload"
|
||||
},
|
||||
{
|
||||
"offset": 779123,
|
||||
"old": "805a3900",
|
||||
"new": "68703a00",
|
||||
"new": "187c3a00",
|
||||
"desc": "main-app subheader payload size (ps)"
|
||||
},
|
||||
{
|
||||
"offset": 152,
|
||||
"old": "005b3900",
|
||||
"new": "e8703a00",
|
||||
"new": "987c3a00",
|
||||
"desc": "main-app TOC entry size (ps + 128)"
|
||||
},
|
||||
{
|
||||
"offset": 779243,
|
||||
"old": "805a3904",
|
||||
"new": "68703a04",
|
||||
"new": "187c3a04",
|
||||
"desc": "main-app preamble length (low 24 bits)"
|
||||
},
|
||||
{
|
||||
"offset": 779247,
|
||||
"old": "effdbcea",
|
||||
"new": "0b0c48b0",
|
||||
"new": "ed6f6deb",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
|
||||
},
|
||||
{
|
||||
"offset": 156,
|
||||
"old": "c0c6322a",
|
||||
"new": "52937d21",
|
||||
"new": "7141aa39",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
|
||||
},
|
||||
{
|
||||
"offset": 779127,
|
||||
"old": "c0c6322a",
|
||||
"new": "52937d21",
|
||||
"new": "7141aa39",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -3,6 +3,8 @@ export type GlassesDisplayState = {
|
||||
silentMode: boolean;
|
||||
screenOn: boolean;
|
||||
battery: number | null;
|
||||
/** The paired glasses' model, e.g. "G2" (glassesModelName). */
|
||||
glassesModel: string;
|
||||
/**
|
||||
* The charging case's last-known level, and whether it is on USB (null
|
||||
* while the glasses are out of the case). Shown only while charging, when
|
||||
@@ -20,7 +22,7 @@ export function glassesDisplayLabel(state: GlassesDisplayState): string {
|
||||
// Charging wins over every session-local state: the glasses are in their
|
||||
// case, so neither the lock screen nor the foreground window is visible.
|
||||
if (state.phase === "charging") {
|
||||
return chargingDisplayLabel(state.battery);
|
||||
return chargingDisplayLabel(state.battery, state.glassesModel);
|
||||
}
|
||||
// Preview mode has a live (simulated) screen despite the disconnected
|
||||
// phase, so fall through to the screen-state/foreground labels.
|
||||
@@ -63,8 +65,8 @@ export function glassesDisplayAsleep(state: GlassesDisplayState): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
export function chargingDisplayLabel(battery: number | null): string {
|
||||
return validBatteryLevel(battery) ? `Charging · G2 ${battery}%` : "Charging";
|
||||
export function chargingDisplayLabel(battery: number | null, glassesModel: string): string {
|
||||
return validBatteryLevel(battery) ? `Charging: ${glassesModel} ${battery}%` : "Charging";
|
||||
}
|
||||
|
||||
/** The shell's last-known battery levels (ShellChromeState["battery"]), as far as the phone shows them. */
|
||||
@@ -78,16 +80,16 @@ export type BatteryLevels = {
|
||||
};
|
||||
|
||||
/**
|
||||
* The phone Settings tab's battery row: each device whose level is known,
|
||||
* The phone settings page's battery row: each device whose level is known,
|
||||
* e.g. "G2 100% · Case 98% ⚡ · R1 72%" (⚡ = charging), or "" when none
|
||||
* is. Levels are the last known ones (the case only reports while the glasses
|
||||
* are docked), so ⚡ appears only while it is live: G2 and Case only with a
|
||||
* link to the glasses (`glassesLinked`: connected or charging), and Case only
|
||||
* while they are docked (caseCharging is null otherwise).
|
||||
* are docked), so ⚡ appears only while it is live: the glasses and Case only
|
||||
* with a link to the glasses (`glassesLinked`: connected or charging), and Case
|
||||
* only while they are docked (caseCharging is null otherwise).
|
||||
*/
|
||||
export function formatBatteryLine(levels: BatteryLevels, glassesLinked: boolean): string {
|
||||
export function formatBatteryLine(levels: BatteryLevels, glassesLinked: boolean, glassesModel: string): string {
|
||||
const devices: Array<[string, number | null, boolean | null]> = [
|
||||
["G2", levels.headset, glassesLinked && levels.headsetCharging],
|
||||
[glassesModel, levels.headset, glassesLinked && levels.headsetCharging],
|
||||
["Case", levels.case, glassesLinked && levels.caseCharging],
|
||||
["R1", levels.ring, levels.ringCharging],
|
||||
];
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
import { classifyAdvertisement, hasEvenManufacturerSignature, type EvenAdvertisement } from './even-advertisement'
|
||||
import { classifyAdvertisement, g1FromName, hasEvenManufacturerSignature, type EvenAdvertisement } from './even-advertisement'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
|
||||
export type IosAdvertisement = { identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean }
|
||||
export type IosDevice = EvenAdvertisement & { identifier: string }
|
||||
|
||||
/**
|
||||
* A G1 arm advertises no full MAC, and CoreBluetooth hides the real one. Faceclaw's addresses are MAC-shaped everywhere, so its
|
||||
* address on this phone is made from the peripheral identifier. It's stable as long as the identifier is, which on firmware
|
||||
* 0.5.66 and later is across installs too, and it only has to mean something on this phone: nobody types it in.
|
||||
*/
|
||||
export function g1IosAddress(identifier: string): string {
|
||||
return identifier.replace(/-/g, '').slice(0, 12).toUpperCase().replace(/(..)(?=.)/g, '$1:')
|
||||
}
|
||||
|
||||
/** Only a full vendor-advertised MAC may bind a manually entered address. */
|
||||
export function identifyIosPeripheral(raw: IosAdvertisement): IosDevice | null {
|
||||
if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(raw.identifier)) return null
|
||||
if (g1FromName(raw.name)) {
|
||||
const arm = classifyAdvertisement({ ...raw, address: g1IosAddress(raw.identifier), source: 'scan', bonded: false,
|
||||
txPower: null, seenAtMs: Date.now() })
|
||||
return arm && { ...arm, identifier: raw.identifier.toUpperCase() }
|
||||
}
|
||||
if (!hasEvenManufacturerSignature(hexToBytes(raw.manufacturerData))) return null
|
||||
const candidate = classifyAdvertisement({ ...raw, address: '', source: 'scan', bonded: false,
|
||||
txPower: null, seenAtMs: Date.now() })
|
||||
|
||||
@@ -2,7 +2,6 @@ import { startRemoteInput } from "../remote/service"
|
||||
import type { KeyboardInputSession } from '../ui/shell/keyboard-input'
|
||||
import { acceptInput, resetRingInputFilter } from "../ui/input-monitor";
|
||||
import { bindIosNotifications, iosNotificationsChanged, onIosNotificationPopup, readActiveNotifications } from '../native/notification-icons.ios'
|
||||
import { shouldShowNotificationOnGlasses } from '../native/notification-sources'
|
||||
import { launcherEntries } from '../apps/launcher'
|
||||
import { discardZeroedEvenHubPackage, getInstalledEvenHubAppById, installedEvenHubPackageId, uninstallEvenHubPackage } from '../apps/evenhub/installed-apps'
|
||||
import { isInstalledPackagePresent } from '../apps/evenhub/updates'
|
||||
@@ -23,13 +22,17 @@ import { weatherBridge } from '../native/weather'
|
||||
import { FaceclawCommunicatorBridge, resolveIosPeripherals, type CommunicatorState, type RawInputEvent } from '../native/faceclaw-communicator.ios'
|
||||
import { PreviewDisplayTarget, type DisplayTarget } from '../native/preview-display.ios'
|
||||
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
|
||||
import { G1V0_IMAGE } from '../g1/firmware/g1v0-image'
|
||||
import { GlanceHost, type GlanceDisplay } from './glance-host'
|
||||
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
|
||||
import { OsEventTypeList } from './events'
|
||||
import { loadDeviceAddresses } from './device-addresses'
|
||||
import { updateGlassesPresence } from './glasses-presence'
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from './firmware-compat'
|
||||
import { isHaltedSessionPhase, suppressAutoReconnect } from './reconnect-policy'
|
||||
import { keyboardService } from './keyboard/service'
|
||||
import type { KeyEvent } from './keyboard/keys'
|
||||
import { isHaltedSessionPhase, resumeAutoReconnect, suppressAutoReconnect } from './reconnect-policy'
|
||||
import { WearRemote, type WearRemoteInputKind } from './wear-remote'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
import { createLauncherWindow, LAUNCHER_SURFACE_ID } from '../apps/launcher/launcher-app'
|
||||
import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_WINDOW_ID } from '../apps/notifications/notifications-app'
|
||||
@@ -46,13 +49,13 @@ import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/g
|
||||
import { noopLayerActions, type ContinuousCaptureOptions, type LayerActions } from '../ui/layers'
|
||||
import { TextViewerLayer } from '../apps/files/text-viewer'
|
||||
import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell'
|
||||
import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect } from '../ui/shell/geometry'
|
||||
import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, windowEdges, type ScreenRect } from '../ui/shell/geometry'
|
||||
import { mirrorToPanelFraction, phonePreviewCrop } from './phone-preview-crop'
|
||||
import { onAnySettingChanged,
|
||||
previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences, getStringSettingById,
|
||||
nightscoutSiteUrlSetting, nightscoutApiTokenSetting, voiceProviderSetting, elevenLabsApiKeySetting,
|
||||
openAiApiKeySetting, sonioxApiKeySetting, whisperServerApiKeySetting, whisperServerLanguageSetting,
|
||||
whisperServerModelSetting, whisperServerUrlSetting, ringConnectionModeSetting, saveTranscribeAudioSetting } from '../ui/dashboard-settings'
|
||||
whisperServerModelSetting, whisperServerUrlSetting, ringConnectionModeSetting, saveRecordingSetting, glassesTypeSetting, shouldPopUpNewNotification } from '../ui/dashboard-settings'
|
||||
import { transcriptionKey, type VoiceProviderSelection } from '../native/cloud-stt-provider'
|
||||
import type { PhoneGesture } from '../phone-ui/phone-gestures'
|
||||
import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from '../apps/onboarding/onboarding-progress'
|
||||
@@ -167,6 +170,8 @@ export class IosPreviewController {
|
||||
private continuousPending = false
|
||||
private continuousCaptureToken = 0
|
||||
private sessionConnected = false
|
||||
/** The Apple Watch remote; constructed last so it sees a fully wired shell. */
|
||||
private wearRemote: WearRemote | null = null
|
||||
private readonly actions: LayerActions = {
|
||||
...noopLayerActions,
|
||||
playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload),
|
||||
@@ -210,9 +215,10 @@ export class IosPreviewController {
|
||||
prepareVoiceCapture: () => this.prepareVoiceCapture(),
|
||||
getScreenTimeoutMs: () => null,
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
onWindowsChanged: () => { this.requestShellRender(); this.syncEvenHubPhoneUi() },
|
||||
onWindowsChanged: () => { this.requestShellRender(); this.syncEvenHubPhoneUi(); this.wearRemote?.schedulePublish() },
|
||||
openNotificationsWindow: () =>
|
||||
this.openInProcessAppInBackground(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow),
|
||||
this.openInProcessAppInBackground(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID,
|
||||
options => createNotificationsAppWindow(options, section => { void this.launchApp('settings', { section }) })),
|
||||
onSwitcherBarHeightChanged: () => {
|
||||
this.relayout()
|
||||
shell.foregroundWindow()?.requestRender()
|
||||
@@ -227,6 +233,8 @@ export class IosPreviewController {
|
||||
this.unblankAfterShellRender = on
|
||||
if (!on) void this.display?.setScreenBlanked(true).catch(error => this.fail(error))
|
||||
this.requestShellRender()
|
||||
this.wearRemote?.schedulePublish()
|
||||
if (on) keyboardService.retryNow()
|
||||
},
|
||||
})
|
||||
const launcher = createLauncherWindow({
|
||||
@@ -243,7 +251,7 @@ export class IosPreviewController {
|
||||
shell.focusWindow(launcher.windowId)
|
||||
onIosNotificationPopup(key => {
|
||||
const notification = readActiveNotifications(128).find(n => n.key === key)
|
||||
if (this.glassesLocked || !notification || !shouldShowNotificationOnGlasses(notification.packageName)) return
|
||||
if (this.glassesLocked || !notification || !shouldPopUpNewNotification(notification.packageName)) return
|
||||
const woke = !shell.isScreenOn() && shell.wake('sidebar')
|
||||
shell.openNotificationModal(key, woke)
|
||||
this.requestShellRender()
|
||||
@@ -265,16 +273,52 @@ export class IosPreviewController {
|
||||
assistantAvailable: () => shell.isAssistantAvailable(),
|
||||
assistant: text => shell.sendToAssistant(text),
|
||||
})
|
||||
// A keyboard paired through the glasses (the CFW keyboard bridge). The service decides what
|
||||
// each key means (app/g2/keyboard/key-router.ts); its gestures share the controller queue
|
||||
// with physical input, like the watch's.
|
||||
const queueKeyboardInput = (run: () => Promise<void>): Promise<void> => {
|
||||
const task = this.inputQueue.then(() => { this.onActivity(); return run() })
|
||||
this.inputQueue = task.catch(error => this.fail(error))
|
||||
return task
|
||||
}
|
||||
keyboardService.setHost({
|
||||
ready: () => this.state.phase === 'connected',
|
||||
locked: () => this.glassesLocked,
|
||||
wake: () => { void queueKeyboardInput(() => this.receiveInput(makeInputEvent({ type: 'display-wake' }))).catch(() => {}) },
|
||||
gesture: (gesture, origin) => queueKeyboardInput(async () => {
|
||||
await this.receiveInput(makeInputEvent({ type: gesture, source: origin } as InputEventPayload))
|
||||
// A discrete hold includes its release.
|
||||
if (gesture === 'long-press') await this.receiveInput(makeInputEvent({ type: 'long-press-release', source: origin }))
|
||||
}),
|
||||
log: line => this.logBluetooth(line),
|
||||
// Only while the display is on: a keyboard that is switched off would otherwise keep the
|
||||
// glasses' radio looking for it around the clock.
|
||||
shouldReconnect: () => this.state.phase === 'connected' && shell.isScreenOn(),
|
||||
})
|
||||
// The header's EvenHub phone-UI button hides while that UI is showing.
|
||||
onPhoneShownChanged(() => this.syncEvenHubPhoneUi())
|
||||
registerSystemTools()
|
||||
registerWindowTools({ apps: ALL_APPS.filter(app => !iosAppUnavailableReason(app.appId)),
|
||||
registerWindowTools({ apps: () => ALL_APPS.filter(app => app.isSupported?.() !== false && !iosAppUnavailableReason(app.appId)),
|
||||
launchApp: id => this.launchApp(id), requestShellRender: () => this.requestShellRender() })
|
||||
registerNavigateTools(id => this.launchApp(id))
|
||||
registerRoamTools(id => this.launchApp(id))
|
||||
// Every capture end (drained, failed, replaced) may leave continuous capture to restart.
|
||||
iosVoiceInput.onSpeechEnd(() => this.syncContinuousCapture())
|
||||
for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app))
|
||||
this.wearRemote = new WearRemote({
|
||||
apps: () => ALL_APPS.filter(app => app.showInLauncher !== false && app.isSupported?.() !== false && !iosAppUnavailableReason(app.appId)),
|
||||
injectInput: kind => this.injectWatchInput(kind),
|
||||
launchApp: id => this.launchApp(id),
|
||||
// The watch's Connect/Disconnect are explicit user choices, like the phone's own buttons.
|
||||
connect: () => { resumeAutoReconnect(); return this.connect(true) },
|
||||
disconnect: () => { suppressAutoReconnect(); return this.disconnect() },
|
||||
setGlassesLocked: locked => this.setGlassesLocked(locked),
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
previewInputActive: () => !this.communicator && this.runtimeRunning,
|
||||
getState: () => ({ phase: this.state.phase, status: this.state.status, glassesLocked: this.glassesLocked,
|
||||
glassesWorn: this.glassesWorn, silentMode: false, lastHeadsetBattery: this.state.battery }),
|
||||
appendLog: line => this.logBluetooth(line),
|
||||
})
|
||||
}
|
||||
|
||||
/** The compositor frames go to: the live session, else the headless preview. */
|
||||
@@ -341,7 +385,7 @@ export class IosPreviewController {
|
||||
this.logBluetooth(`Phone background; glasses ${this.state.phase}`)
|
||||
this.flushBluetoothLog()
|
||||
}
|
||||
/** Overlay an EvenHub app's phone UI (the header's app-icon button). */
|
||||
/** Overlay an EvenHub app's phone UI (the app-icon button under the mirror). */
|
||||
showEvenHubPhoneUi(windowId: string): void {
|
||||
showOnPhone(windowId)
|
||||
}
|
||||
@@ -399,6 +443,7 @@ export class IosPreviewController {
|
||||
this.logBluetooth(`Phone battery ${phone.battery ?? "unknown"}% charging=${phone.charging}`)
|
||||
this.offSettings = onAnySettingChanged(() => {
|
||||
this.syncLockSetting()
|
||||
this.syncContinuousRecording()
|
||||
this.pushBrightness()
|
||||
this.relayout()
|
||||
shell.foregroundWindow()?.requestRender()
|
||||
@@ -443,6 +488,7 @@ export class IosPreviewController {
|
||||
// a session's first snapshot) wakes the screen, to the lock notice if locked.
|
||||
const putOn = wearing && this.glassesWorn === false
|
||||
this.glassesWorn = wearing
|
||||
this.wearRemote?.schedulePublish()
|
||||
this.syncPresence()
|
||||
this.logBluetooth(`Glasses wear state: ${wearing ? 'ON_HEAD' : 'OFF_HEAD'}`)
|
||||
if (!wearing && this.phoneLocked && this.lockEnabled) this.setGlassesLocked(true)
|
||||
@@ -451,6 +497,7 @@ export class IosPreviewController {
|
||||
private setGlassesLocked(locked: boolean): void {
|
||||
if (locked === this.glassesLocked) return
|
||||
this.glassesLocked = locked
|
||||
this.wearRemote?.schedulePublish()
|
||||
if (locked) {
|
||||
this.glance.dismiss()
|
||||
if (iosVoiceInput.continuousSource === 'glasses') this.continuousPending = true
|
||||
@@ -483,7 +530,9 @@ export class IosPreviewController {
|
||||
await target.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId)
|
||||
}
|
||||
private relayout(): void {
|
||||
const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";")
|
||||
// Each window's rect, and what its edges touch (which sets its padding).
|
||||
const layout = shell.getWindows().map(window =>
|
||||
JSON.stringify([appViewportRect(window.heightMode, window.appId), windowEdges(window.appId)])).join(";")
|
||||
if (layout === this.lastLayout) return
|
||||
this.lastLayout = layout
|
||||
for (const window of shell.getWindows()) {
|
||||
@@ -599,7 +648,25 @@ export class IosPreviewController {
|
||||
console.log(`[ios-preview] ${origin} ${gesture}: ${shell.describeInputTarget()}`)
|
||||
}).catch(error => this.fail(error))
|
||||
}
|
||||
/** Input from the Apple Watch: shares the controller queue with physical input. */
|
||||
private injectWatchInput(kind: WearRemoteInputKind): Promise<void> {
|
||||
const task = this.inputQueue.then(async () => {
|
||||
this.onActivity()
|
||||
// As on Android, and because the watch may be acting on a stale state snapshot: a dark
|
||||
// display takes a double-click (wake) or a tap/hold (Glanceboard), and scrolls mean nothing.
|
||||
if (!shell.isScreenOn() && (kind === 'scroll-up' || kind === 'scroll-down' || kind === 'short-then-long-press')) return
|
||||
// A real hold releases when the finger lifts; a discrete long-press releases at once.
|
||||
const type = kind === 'long-press-start' ? 'long-press' : kind
|
||||
await this.receiveInput(makeInputEvent({ type, source: 'watch' } as InputEventPayload))
|
||||
if (kind === 'long-press') await this.receiveInput(makeInputEvent({ type: 'long-press-release', source: 'watch' }))
|
||||
console.log(`[ios-preview] apple-watch ${kind}: ${shell.describeInputTarget()}`)
|
||||
})
|
||||
this.inputQueue = task.catch(error => this.fail(error))
|
||||
return task
|
||||
}
|
||||
private async receiveInput(event: InputEvent, headTilt = false): Promise<void> {
|
||||
// Input can change the foreground app or the display state, and the shell doesn't announce those.
|
||||
this.wearRemote?.schedulePublish()
|
||||
if (!acceptInput(event)) return
|
||||
if (this.glassesLocked) {
|
||||
// Preserve the locked display's sleep/wake controls without dispatching
|
||||
@@ -763,10 +830,11 @@ export class IosPreviewController {
|
||||
const display = this.display
|
||||
if (!display) return
|
||||
await display.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
|
||||
await display.setSurfaceVisible(id, visible)
|
||||
await display.setSurfaceVisible(id, typeof visible === 'function' ? visible() : visible)
|
||||
},
|
||||
setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
|
||||
removeSurface: id => this.removeWindowSurface(id),
|
||||
unhandledKey: event => keyboardService.handleUnhandledKey(event as KeyEvent),
|
||||
submitPixels: (id, pixels, width, height, draws) => {
|
||||
void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error))
|
||||
},
|
||||
@@ -788,6 +856,18 @@ export class IosPreviewController {
|
||||
this.state = { ...this.state, phase, status }
|
||||
this.logBluetooth(status)
|
||||
this.emitState()
|
||||
this.syncKeyboardLink()
|
||||
}
|
||||
/**
|
||||
* Tell the keyboard bridge whether the session can host a keyboard: a G2 on firmware with
|
||||
* the bridge, connected. On a disconnect the user asked for, the link is still up when the
|
||||
* phase turns, so the firmware is told to let the keyboard go first.
|
||||
*/
|
||||
private syncKeyboardLink(): void {
|
||||
const up = !!this.communicator && this.state.phase === 'connected' && this.firmwareCompatible
|
||||
&& glassesTypeSetting.get() === 'g2'
|
||||
if (up) keyboardService.linkReady()
|
||||
else keyboardService.linkLost(this.state.phase === 'disconnecting')
|
||||
}
|
||||
/** Mirror the session into glasses-presence (read by EvenHub's device status and Timers). */
|
||||
private syncPresence(): void {
|
||||
@@ -807,6 +887,7 @@ export class IosPreviewController {
|
||||
this.syncRuntime()
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.requestShellRender()
|
||||
this.wearRemote?.schedulePublish()
|
||||
}
|
||||
/** The ring level arrives through the glasses, so like Android it is shown only while connected. */
|
||||
private pushBatteryLevels(): void {
|
||||
@@ -814,11 +895,16 @@ export class IosPreviewController {
|
||||
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging,
|
||||
ring: connected ? this.state.ringBattery : null, ringCharging: connected ? this.state.ringCharging : null })
|
||||
}
|
||||
async connect(): Promise<void> {
|
||||
if (!this.active || this.connecting || this.stopping) return
|
||||
/** `fromBackground`: the watch asked, and the phone may be in a pocket with the app suspended. */
|
||||
async connect(fromBackground = false): Promise<void> {
|
||||
if ((!this.active && !fromBackground) || this.connecting || this.stopping) return
|
||||
if (this.communicator && ['connecting', 'connected', 'retrying', 'disconnecting'].includes(this.state.phase)) return
|
||||
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
|
||||
if (error) { this.onError(error); return }
|
||||
if (error) {
|
||||
if (this.active) this.onError(error)
|
||||
else this.logBluetooth(error)
|
||||
return
|
||||
}
|
||||
this.connecting = true
|
||||
this.sessionCreated = true
|
||||
try {
|
||||
@@ -836,7 +922,7 @@ export class IosPreviewController {
|
||||
return
|
||||
}
|
||||
if (this.communicator) await this.closeCommunicator()
|
||||
const communicator = new FaceclawCommunicatorBridge(identifiers)
|
||||
const communicator = new FaceclawCommunicatorBridge({ ...identifiers, glassesType: glassesTypeSetting.get() })
|
||||
this.teardownPreviewDisplay()
|
||||
this.communicator = communicator
|
||||
this.wearDetectionRequested = false
|
||||
@@ -882,6 +968,7 @@ export class IosPreviewController {
|
||||
this.pushBatteryLevels()
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.requestShellRender()
|
||||
this.wearRemote?.schedulePublish()
|
||||
}),
|
||||
communicator.onCaseState(state => {
|
||||
if (this.communicator !== communicator) return
|
||||
@@ -889,6 +976,7 @@ export class IosPreviewController {
|
||||
// the case's own charging is only known while the glasses are docked.
|
||||
shell.setBatteryLevels({ ...(state.battery !== null ? { case: state.battery } : {}),
|
||||
caseCharging: state.docked ? state.charging : null })
|
||||
this.wearRemote?.schedulePublish()
|
||||
}),
|
||||
communicator.onFirmwareInfo(info => {
|
||||
if (this.communicator !== communicator) return
|
||||
@@ -899,6 +987,7 @@ export class IosPreviewController {
|
||||
const firmwareWarning = firmwareIncompatibilityMessage(info) ?? ''
|
||||
if (firmwareWarning && firmwareWarning !== this.state.firmwareWarning) this.logBluetooth(`Firmware compatibility warning: ${firmwareWarning}`)
|
||||
this.firmwareCompatible = !firmwareWarning && hasCompatibleFirmware(info)
|
||||
this.syncKeyboardLink()
|
||||
this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion,
|
||||
rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities,
|
||||
firmwareWarning }
|
||||
@@ -938,6 +1027,8 @@ export class IosPreviewController {
|
||||
// (or re-pairs / installs the custom firmware, which lift the suppression).
|
||||
if (isHaltedSessionPhase(state.phase)) suppressAutoReconnect()
|
||||
const phase: SessionPhase = state.phase === 'charging' ? 'connected' : isHaltedSessionPhase(state.phase) ? 'error' : state.phase
|
||||
// The relay ends with the link, so a reconnected session has to ask for it again (syncNotifications).
|
||||
if (phase !== 'connected' && this.notifications && this.notifications.state !== 'disconnected') this.notifications.stop()
|
||||
// Wear state is a per-transport snapshot: ask again on every new session.
|
||||
if (phase !== 'connected') this.wearDetectionRequested = false
|
||||
this.update(phase, state.status)
|
||||
@@ -988,6 +1079,11 @@ export class IosPreviewController {
|
||||
if (this.state.capabilities) notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
|
||||
return
|
||||
}
|
||||
// A G1 reports no revision that says so: its session knows from what the right arm answered.
|
||||
if (!communicator.ancsRelayAvailable()) {
|
||||
notifications.stop(`Update the glasses to Faceclaw's firmware ${G1V0_IMAGE.version} for iPhone notifications, then reconnect.`)
|
||||
return
|
||||
}
|
||||
if (notifications.state !== 'disconnected') return
|
||||
notifications.start((Date.now() ^ Math.floor(Math.random() * 0xffffffff)) >>> 0, communicator.rightWriteLimit())
|
||||
}
|
||||
@@ -1061,8 +1157,8 @@ export class IosPreviewController {
|
||||
/**
|
||||
* Continuous capture on behalf of `client` (one id per window: Transcribe,
|
||||
* Translate, Teleprompter, Calculator); it runs while any client wants it, as
|
||||
* on Android. `recordAs` names a WAV of the audio in Downloads when Save
|
||||
* Transcribe audio is on.
|
||||
* on Android. `recordAs` names a recording (.mka) of the audio in Downloads
|
||||
* when Save Recording is on.
|
||||
*/
|
||||
private startContinuousVoiceCapture(client: string, options: ContinuousCaptureOptions = {}): void {
|
||||
this.continuousClients.set(client, options)
|
||||
@@ -1151,9 +1247,12 @@ export class IosPreviewController {
|
||||
private syncContinuousClaim(): void {
|
||||
voiceActivity.setActive(this.continuousClients.size > 0 && iosVoiceInput.continuousActive, CONTINUOUS_CAPTURE_HOLDER)
|
||||
}
|
||||
/** Record to the first client's requested name while Save Transcribe audio is on. */
|
||||
/**
|
||||
* Record to the first client's requested name while Save Recording is on (it is
|
||||
* toggled from those windows' menus, so this also runs when a setting changes).
|
||||
*/
|
||||
private syncContinuousRecording(): void {
|
||||
const name = saveTranscribeAudioSetting.get() ? [...this.continuousClients.values()].map(options => options.recordAs).find(Boolean) ?? '' : ''
|
||||
const name = saveRecordingSetting.get() ? [...this.continuousClients.values()].map(options => options.recordAs).find(Boolean) ?? '' : ''
|
||||
iosVoiceInput.recordTo(name)
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
/**
|
||||
* What keys from a hardware keyboard do, apart from typing: the built-in
|
||||
* defaults, the user's custom keybindings, and how the two combine.
|
||||
*
|
||||
* A key becomes one of the gestures the ring and the watch already have, so
|
||||
* nothing downstream knows about keyboards. Three layers, first match wins:
|
||||
*
|
||||
* 1. Custom keybindings (Settings > Keyboard > Keybindings). These apply
|
||||
* everywhere, including where the focused window takes keys for itself
|
||||
* (a terminal session), since that is the point of binding a remote's
|
||||
* few buttons.
|
||||
* 2. Built-in bindings that also apply there: Escape with Shift, Ctrl or
|
||||
* Alt goes back, which is the way out of a terminal session.
|
||||
* 3. Built-in navigation, only for keys no window took: the arrows are the
|
||||
* watch's four swipes, Enter selects, Escape goes back.
|
||||
*
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
import type { KeyEvent } from "./keys";
|
||||
|
||||
/** A gesture a key can stand for, named as the input pipeline names it. */
|
||||
export type KeyGesture =
|
||||
| "swipe-up"
|
||||
| "swipe-down"
|
||||
| "swipe-left"
|
||||
| "swipe-right"
|
||||
| "scroll-up"
|
||||
| "scroll-down"
|
||||
| "click"
|
||||
| "double-click"
|
||||
| "long-press"
|
||||
| "short-then-long-press";
|
||||
|
||||
/**
|
||||
* What a custom keybinding does. "none" turns off the key's built-in
|
||||
* navigation meaning; it does not stop the key being typed into a window
|
||||
* that takes keys.
|
||||
*/
|
||||
export type KeyAction = KeyGesture | "none";
|
||||
|
||||
/** The actions, in the order the picker lists them. */
|
||||
export const KEY_ACTIONS: readonly { action: KeyAction; label: string }[] = [
|
||||
{ action: "click", label: "Select" },
|
||||
{ action: "double-click", label: "Back" },
|
||||
{ action: "swipe-up", label: "Swipe up" },
|
||||
{ action: "swipe-down", label: "Swipe down" },
|
||||
{ action: "swipe-left", label: "Swipe left" },
|
||||
{ action: "swipe-right", label: "Swipe right" },
|
||||
{ action: "scroll-up", label: "Scroll up" },
|
||||
{ action: "scroll-down", label: "Scroll down" },
|
||||
{ action: "long-press", label: "System menu" },
|
||||
{ action: "short-then-long-press", label: "App menu" },
|
||||
{ action: "none", label: "Nothing" },
|
||||
];
|
||||
|
||||
export function keyActionLabel(action: KeyAction): string {
|
||||
return KEY_ACTIONS.find((entry) => entry.action === action)?.label ?? action;
|
||||
}
|
||||
|
||||
function isKeyAction(value: unknown): value is KeyAction {
|
||||
return KEY_ACTIONS.some((entry) => entry.action === value);
|
||||
}
|
||||
|
||||
/** A physical key with the modifiers held: what a keybinding is keyed on. */
|
||||
export type KeyChord = { code: string; shift: boolean; ctrl: boolean; alt: boolean; meta: boolean };
|
||||
export type KeyBinding = { chord: KeyChord; action: KeyAction };
|
||||
|
||||
const MODIFIER_CODES = new Set([
|
||||
"ControlLeft", "ControlRight", "ShiftLeft", "ShiftRight", "AltLeft", "AltRight", "MetaLeft", "MetaRight",
|
||||
]);
|
||||
|
||||
/** Shift, Ctrl, Alt and Meta themselves: they qualify a chord rather than being one. */
|
||||
export function isModifierCode(code: string): boolean {
|
||||
return MODIFIER_CODES.has(code);
|
||||
}
|
||||
|
||||
export function chordOf(event: KeyEvent): KeyChord {
|
||||
const { shift, ctrl, alt, meta } = event.modifiers;
|
||||
return { code: event.code, shift, ctrl, alt, meta };
|
||||
}
|
||||
|
||||
/** Canonical text of a chord ("Ctrl+Shift+Escape"): the stored form, and the lookup key. */
|
||||
export function chordId(chord: KeyChord): string {
|
||||
return [chord.ctrl && "Ctrl", chord.alt && "Alt", chord.shift && "Shift", chord.meta && "Meta", chord.code]
|
||||
.filter(Boolean).join("+");
|
||||
}
|
||||
|
||||
/** The inverse of chordId, or null for anything it could not have produced. */
|
||||
export function parseChordId(text: string): KeyChord | null {
|
||||
const parts = text.split("+");
|
||||
const code = parts.pop() ?? "";
|
||||
if (!/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(code) || isModifierCode(code)) return null;
|
||||
const chord: KeyChord = { code, shift: false, ctrl: false, alt: false, meta: false };
|
||||
for (const part of parts) {
|
||||
if (part === "Ctrl" && !chord.ctrl) chord.ctrl = true;
|
||||
else if (part === "Alt" && !chord.alt) chord.alt = true;
|
||||
else if (part === "Shift" && !chord.shift) chord.shift = true;
|
||||
else if (part === "Meta" && !chord.meta) chord.meta = true;
|
||||
else return null;
|
||||
}
|
||||
return chord;
|
||||
}
|
||||
|
||||
const CODE_LABELS: Record<string, string> = {
|
||||
ArrowUp: "Up", ArrowDown: "Down", ArrowLeft: "Left", ArrowRight: "Right",
|
||||
Escape: "Esc", PageUp: "Page Up", PageDown: "Page Down", CapsLock: "Caps Lock",
|
||||
PrintScreen: "Print Screen", ScrollLock: "Scroll Lock", NumLock: "Num Lock", ContextMenu: "Menu",
|
||||
Minus: "-", Equal: "=", BracketLeft: "[", BracketRight: "]", Backslash: "\\", IntlBackslash: "\\",
|
||||
IntlHash: "#", Semicolon: ";", Quote: "'", Backquote: "`", Comma: ",", Period: ".", Slash: "/",
|
||||
AudioVolumeUp: "Volume Up", AudioVolumeDown: "Volume Down", AudioVolumeMute: "Mute",
|
||||
MediaPlayPause: "Play/Pause", MediaPlay: "Play", MediaPause: "Pause", MediaStop: "Stop",
|
||||
MediaTrackNext: "Next Track", MediaTrackPrevious: "Previous Track",
|
||||
BrightnessUp: "Brightness Up", BrightnessDown: "Brightness Down",
|
||||
BrowserSearch: "Search", BrowserHome: "Browser Home", BrowserBack: "Browser Back", BrowserForward: "Browser Forward",
|
||||
LaunchMail: "Mail", LaunchApplication1: "App 1", LaunchApplication2: "App 2",
|
||||
};
|
||||
|
||||
/** A key as a person would name it. Unknown keys keep their HID usage number, which is all there is. */
|
||||
function codeLabel(code: string): string {
|
||||
const known = CODE_LABELS[code];
|
||||
if (known) return known;
|
||||
let match = /^Key([A-Z])$/.exec(code);
|
||||
if (match) return match[1]!;
|
||||
match = /^Digit([0-9])$/.exec(code);
|
||||
if (match) return match[1]!;
|
||||
match = /^Numpad(.+)$/.exec(code);
|
||||
if (match) return `Numpad ${match[1]}`;
|
||||
match = /^Usage([0-9a-f]+)$/.exec(code);
|
||||
if (match) return `Key 0x${match[1]}`;
|
||||
match = /^Consumer([0-9a-f]+)$/.exec(code);
|
||||
if (match) return `Media key 0x${match[1]}`;
|
||||
return code;
|
||||
}
|
||||
|
||||
/** A chord for display: "Shift+Esc", "Left", "Volume Up". */
|
||||
export function describeChord(chord: KeyChord): string {
|
||||
return [chord.ctrl && "Ctrl", chord.alt && "Alt", chord.shift && "Shift", chord.meta && "Meta", codeLabel(chord.code)]
|
||||
.filter(Boolean).join("+");
|
||||
}
|
||||
|
||||
/** The most custom keybindings kept; far more than a keyboard has uses for. */
|
||||
export const MAX_KEY_BINDINGS = 64;
|
||||
|
||||
/**
|
||||
* Custom keybindings from their stored JSON. Lenient: whatever doesn't make
|
||||
* sense is dropped, and a later entry for a chord replaces an earlier one.
|
||||
*/
|
||||
export function parseKeyBindings(raw: string): KeyBinding[] {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = raw ? JSON.parse(raw) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
const bindings = new Map<string, KeyBinding>();
|
||||
for (const entry of parsed) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const { key, action } = entry as { key?: unknown; action?: unknown };
|
||||
const chord = typeof key === "string" ? parseChordId(key) : null;
|
||||
if (!chord || !isKeyAction(action)) continue;
|
||||
bindings.set(chordId(chord), { chord, action });
|
||||
if (bindings.size >= MAX_KEY_BINDINGS) break;
|
||||
}
|
||||
return [...bindings.values()];
|
||||
}
|
||||
|
||||
export function serializeKeyBindings(bindings: readonly KeyBinding[]): string {
|
||||
return JSON.stringify(bindings.map((binding) => ({ key: chordId(binding.chord), action: binding.action })));
|
||||
}
|
||||
|
||||
/** Custom keybindings as the lookup resolveKey takes. */
|
||||
export function keyBindingMap(bindings: readonly KeyBinding[]): Map<string, KeyAction> {
|
||||
return new Map(bindings.map((binding) => [chordId(binding.chord), binding.action]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The built-in meaning of a key press, or null when it has none.
|
||||
* `overridesTyping`: it holds even where the focused window takes keys.
|
||||
* Built-ins go by the logical key, so both Enter keys select.
|
||||
*/
|
||||
export function defaultKeyAction(event: KeyEvent): { action: KeyGesture; overridesTyping: boolean } | null {
|
||||
const { shift, ctrl, alt, meta } = event.modifiers;
|
||||
if (meta) return null;
|
||||
if (event.key === "Escape") {
|
||||
// Plain Escape belongs to whatever takes keys (it is a keystroke in a
|
||||
// terminal); with a modifier it always means "back out of here".
|
||||
return { action: "double-click", overridesTyping: shift || ctrl || alt };
|
||||
}
|
||||
if (ctrl || alt) return null;
|
||||
switch (event.key) {
|
||||
case "ArrowUp":
|
||||
return { action: "swipe-up", overridesTyping: false };
|
||||
case "ArrowDown":
|
||||
return { action: "swipe-down", overridesTyping: false };
|
||||
case "ArrowLeft":
|
||||
return { action: "swipe-left", overridesTyping: false };
|
||||
case "ArrowRight":
|
||||
return { action: "swipe-right", overridesTyping: false };
|
||||
case "Enter":
|
||||
return { action: "click", overridesTyping: false };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The built-ins, for showing to the user. Keep in step with defaultKeyAction. */
|
||||
export const DEFAULT_KEYS_HELP =
|
||||
"Arrow keys: swipe up, down, left and right, as on the watch. Enter: select. Esc: back.\n\n" +
|
||||
"In a terminal session those keys are typed instead, and Shift+Esc (or Ctrl+Esc, Alt+Esc) goes back.\n\n" +
|
||||
"A custom keybinding replaces these for its key and works everywhere, terminals included. " +
|
||||
"\"Nothing\" turns a key's meaning here off without stopping it being typed.";
|
||||
|
||||
/**
|
||||
* Whether holding the key repeats the gesture. Moving up and down a list
|
||||
* does. Sideways swipes don't: depending on the layout they enter and leave
|
||||
* things, and a held key would flap between the two. Selecting, backing out
|
||||
* and menus act once per press.
|
||||
*/
|
||||
export function gestureRepeats(gesture: KeyGesture): boolean {
|
||||
return gesture === "scroll-up" || gesture === "scroll-down" || gesture === "swipe-up" || gesture === "swipe-down";
|
||||
}
|
||||
|
||||
export type KeyResolution = {
|
||||
/** The key is spoken for: don't offer it to the focused window. */
|
||||
consume: boolean;
|
||||
/**
|
||||
* The gesture to perform: at once when `consume`, otherwise only if no
|
||||
* window takes the key. Null for none (including a held key's repeats of
|
||||
* a gesture that doesn't repeat).
|
||||
*/
|
||||
gesture: KeyGesture | null;
|
||||
};
|
||||
|
||||
/** What one key press (or auto-repeat) means, given the custom keybindings. */
|
||||
export function resolveKey(event: KeyEvent, custom: ReadonlyMap<string, KeyAction>): KeyResolution {
|
||||
const repeatable = (gesture: KeyGesture): KeyGesture | null =>
|
||||
event.repeat && !gestureRepeats(gesture) ? null : gesture;
|
||||
const bound = custom.get(chordId(chordOf(event)));
|
||||
if (bound !== undefined && bound !== "none") return { consume: true, gesture: repeatable(bound) };
|
||||
// "none" takes the built-in meaning away and leaves the key to be typed.
|
||||
if (bound === "none") return { consume: false, gesture: null };
|
||||
const builtIn = defaultKeyAction(event);
|
||||
if (!builtIn) return { consume: false, gesture: null };
|
||||
return { consume: builtIn.overridesTyping, gesture: repeatable(builtIn.action) };
|
||||
}
|
||||
|
||||
export type GestureOrigin = "ring" | "watch";
|
||||
|
||||
/**
|
||||
* Which device a keyboard's gesture is presented as. Swipes are the watch's
|
||||
* and scrolls the ring's; the two schemes differ in places (an icon grid
|
||||
* moves by cell for the watch, by row and then cell for the ring). A tap, a
|
||||
* double tap or a hold exists in both, so it goes with whichever the
|
||||
* keyboard last moved with: arrow keys make the keyboard a watch, a remote
|
||||
* bound to scroll makes it a ring. Before any movement, the watch.
|
||||
*/
|
||||
export class GestureScheme {
|
||||
private scheme: GestureOrigin = "watch";
|
||||
|
||||
originFor(gesture: KeyGesture): GestureOrigin {
|
||||
if (gesture.startsWith("swipe-")) this.scheme = "watch";
|
||||
else if (gesture.startsWith("scroll-")) this.scheme = "ring";
|
||||
return this.scheme;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,559 @@
|
||||
/**
|
||||
* The phone side of the CFW keyboard bridge for one glasses session: the
|
||||
* client, the remembered keyboard, reconnecting it, and what the UI shows.
|
||||
* The session hands it field-131 records and a way to send controls; the
|
||||
* rest stays in here. Modelled on the Glassly app's G2KeyboardBridge (MIT).
|
||||
*
|
||||
* The firmware keeps nothing across a reboot (it comes back with the bridge
|
||||
* off), and holds the keyboard's connection only while told to. So the
|
||||
* remembered keyboard is brought back whenever the glasses link comes up and
|
||||
* whenever the keyboard drops, backing off while it stays away. The
|
||||
* discovered handle mapping is good for one connection and is never stored.
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
import { KeyboardClient, KeyboardClientError, type KeyboardStatus } from "./client";
|
||||
import type { HidKeyEvent } from "./hid";
|
||||
import {
|
||||
KeyboardDm,
|
||||
KeyboardError,
|
||||
mergeKeyboardAdverts,
|
||||
parseKeyboardAddress,
|
||||
type KeyboardAdvert,
|
||||
} from "./protocol";
|
||||
|
||||
export type SavedKeyboard = { address: string; addressType: number; name: string };
|
||||
|
||||
export type KeyboardBridgeState =
|
||||
/** No glasses link that can host a keyboard. */
|
||||
| "unavailable"
|
||||
| "idle"
|
||||
| "scanning"
|
||||
| "connecting"
|
||||
| "pairing"
|
||||
| "configuring"
|
||||
| "connected"
|
||||
| "error";
|
||||
|
||||
/** A pairing step waiting on the user. */
|
||||
export type KeyboardPairingPrompt =
|
||||
/** Type this on the keyboard, then Enter. */
|
||||
| { kind: "display"; pin: number }
|
||||
/** Confirm both sides show this value (answerCompare). */
|
||||
| { kind: "compare"; value: number }
|
||||
/** The keyboard shows a code to enter here (answerPasskey). */
|
||||
| { kind: "entry" };
|
||||
|
||||
export type KeyboardBridgeSnapshot = {
|
||||
state: KeyboardBridgeState;
|
||||
saved: SavedKeyboard | null;
|
||||
/** Name of the keyboard being connected or in use. */
|
||||
activeName: string;
|
||||
error: string;
|
||||
pairing: KeyboardPairingPrompt | null;
|
||||
/** Advertisers from the last scan, HID devices first, strongest first. */
|
||||
scanResults: KeyboardAdvert[];
|
||||
/** A user-started scan or connect is running. */
|
||||
busy: boolean;
|
||||
/** The remembered keyboard is away and the bridge keeps trying to bring it back. */
|
||||
waiting: boolean;
|
||||
};
|
||||
|
||||
export type KeyboardBridgeHost = {
|
||||
/** Queue a control record on the live session; false when there is none. */
|
||||
send(record: Uint8Array): boolean;
|
||||
loadSaved(): SavedKeyboard | null;
|
||||
storeSaved(saved: SavedKeyboard | null): void;
|
||||
/** Key transitions from the keyboard (including forced releases). */
|
||||
onKeys(events: HidKeyEvent[]): void;
|
||||
/** The keyboard stopped being usable; drop held-key state without releases. */
|
||||
onKeyboardGone(): void;
|
||||
/** Something in snapshot() changed. */
|
||||
onChanged(): void;
|
||||
/**
|
||||
* A line for the log. `routine` marks the ones that repeat for as long as
|
||||
* a remembered keyboard stays away, which only a verbose log wants.
|
||||
*/
|
||||
log(line: string, routine?: boolean): void;
|
||||
/** A fresh six-digit pairing passkey from a good random source. */
|
||||
randomPasskey?(): number;
|
||||
/**
|
||||
* Whether looking for an absent keyboard is worthwhile now. Each attempt
|
||||
* has the glasses' radio listening for the keyboard for up to half a
|
||||
* minute, so the owner limits it to when the glasses are in use, and calls
|
||||
* retryNow() when that starts. Default: always.
|
||||
*/
|
||||
shouldReconnect?(): boolean;
|
||||
};
|
||||
|
||||
/** Delay before each automatic attempt after a failure; the last one repeats. */
|
||||
const RECONNECT_BACKOFF_MS = [2_000, 5_000, 10_000, 20_000, 30_000];
|
||||
/** After the glasses link comes up, before the first attempt. */
|
||||
const RESUME_DELAY_MS = 1_000;
|
||||
/** After the keyboard drops (it may just be going to sleep). */
|
||||
const DROP_RETRY_DELAY_MS = 2_000;
|
||||
const RELEASE_POLLS = 20;
|
||||
const RELEASE_POLL_MS = 150;
|
||||
|
||||
export class KeyboardBridge {
|
||||
private client: KeyboardClient | null = null;
|
||||
private available = false;
|
||||
private state: KeyboardBridgeState = "unavailable";
|
||||
private saved: SavedKeyboard | null;
|
||||
private activeName = "";
|
||||
private error = "";
|
||||
private pairing: KeyboardPairingPrompt | null = null;
|
||||
private scanResults: KeyboardAdvert[] = [];
|
||||
/** A user-started scan/connect holds the bridge. */
|
||||
private busy = false;
|
||||
/** The keyboard is connected, encrypted and subscribed. */
|
||||
private ready = false;
|
||||
private userDisconnected = false;
|
||||
/** The keyboard was let go while the glasses were out of reach; tell the firmware when they are back. */
|
||||
private releaseOnLink = false;
|
||||
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private reconnectAttempt = 0;
|
||||
/** Bumped whenever the client is replaced; work started for an older one stops quietly. */
|
||||
private generation = 0;
|
||||
|
||||
constructor(private readonly host: KeyboardBridgeHost) {
|
||||
this.saved = host.loadSaved();
|
||||
}
|
||||
|
||||
snapshot(): KeyboardBridgeSnapshot {
|
||||
return {
|
||||
state: this.state,
|
||||
saved: this.saved,
|
||||
activeName: this.activeName,
|
||||
error: this.error,
|
||||
pairing: this.pairing,
|
||||
scanResults: sortAdverts(this.scanResults),
|
||||
busy: this.busy,
|
||||
waiting: this.available && !!this.saved && !this.ready && !this.userDisconnected && !this.busy,
|
||||
};
|
||||
}
|
||||
|
||||
/** True while key events can arrive. */
|
||||
isConnected(): boolean {
|
||||
return this.ready;
|
||||
}
|
||||
|
||||
// ---------- Link lifecycle (driven by the session owner) ----------
|
||||
|
||||
/** The glasses link is up on firmware with the bridge: bring the remembered keyboard back. */
|
||||
linkReady(): void {
|
||||
if (this.available) return;
|
||||
this.available = true;
|
||||
this.userDisconnected = false;
|
||||
this.reconnectAttempt = 0;
|
||||
this.error = "";
|
||||
const client = this.replaceClient();
|
||||
this.setState("idle");
|
||||
if (this.saved && !this.releaseOnLink) {
|
||||
this.scheduleReconnect(RESUME_DELAY_MS);
|
||||
} else if (this.releaseOnLink && client) {
|
||||
this.userDisconnected = !!this.saved;
|
||||
client.query().then((status) => {
|
||||
if (status.enabled || status.conn) return client.disable();
|
||||
}).catch(() => {});
|
||||
}
|
||||
this.releaseOnLink = false;
|
||||
}
|
||||
|
||||
/** The glasses link is gone. Only the remembered keyboard survives. */
|
||||
linkLost(): void {
|
||||
if (!this.available && !this.client) return;
|
||||
this.available = false;
|
||||
this.cancelReconnect();
|
||||
this.generation++;
|
||||
this.client?.close();
|
||||
this.client = null;
|
||||
this.busy = false;
|
||||
this.pairing = null;
|
||||
this.error = "";
|
||||
this.dropKeyboard();
|
||||
this.setState("unavailable");
|
||||
}
|
||||
|
||||
/**
|
||||
* The app is about to close the glasses link on purpose: tell the firmware
|
||||
* to let the keyboard go, so it isn't left attached to glasses nobody is
|
||||
* listening to. Best effort; nothing waits for the answer.
|
||||
*/
|
||||
linkClosing(): void {
|
||||
const client = this.client;
|
||||
if (!client || !this.available || !client.status.enabled) return;
|
||||
client.disable().catch(() => {});
|
||||
}
|
||||
|
||||
/** One field-131 payload from the session. */
|
||||
accept(payload: Uint8Array): void {
|
||||
this.client?.accept(payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* The keyboard may be reachable now, or is wanted now (the display woke):
|
||||
* if it is away and the bridge is waiting out a backoff, try at once.
|
||||
*/
|
||||
retryNow(): void {
|
||||
if (!this.available || !this.saved || this.ready || this.userDisconnected || this.busy) return;
|
||||
// No timer means an attempt is already in flight.
|
||||
if (this.reconnectTimer === null) return;
|
||||
this.reconnectAttempt = 0;
|
||||
this.scheduleReconnect(0);
|
||||
}
|
||||
|
||||
// ---------- Commands (the settings UI) ----------
|
||||
|
||||
/** Scan for about five seconds; results accumulate in the snapshot as they arrive. */
|
||||
async scan(): Promise<KeyboardAdvert[]> {
|
||||
const client = this.beginUserOperation();
|
||||
const generation = this.generation;
|
||||
this.scanResults = [];
|
||||
this.error = "";
|
||||
this.setState("scanning");
|
||||
try {
|
||||
await this.releaseLink(client);
|
||||
await client.enable();
|
||||
await client.scan();
|
||||
if (generation === this.generation) this.setState("idle");
|
||||
} catch (e) {
|
||||
if (generation === this.generation) this.fail(e);
|
||||
throw friendly(e);
|
||||
} finally {
|
||||
if (generation === this.generation) this.endUserOperation();
|
||||
}
|
||||
return sortAdverts(this.scanResults);
|
||||
}
|
||||
|
||||
/** Connect, pair and subscribe a keyboard, then remember it. */
|
||||
async connect(keyboard: SavedKeyboard): Promise<void> {
|
||||
if (!parseKeyboardAddress(keyboard.address) || keyboard.addressType < 0 || keyboard.addressType > 3) {
|
||||
throw new KeyboardClientError("Invalid keyboard address");
|
||||
}
|
||||
const client = this.beginUserOperation();
|
||||
const generation = this.generation;
|
||||
this.userDisconnected = false;
|
||||
try {
|
||||
await this.connectWith(client, generation, keyboard, false);
|
||||
} finally {
|
||||
if (generation === this.generation) this.endUserOperation();
|
||||
}
|
||||
}
|
||||
|
||||
/** Try the remembered keyboard again now. */
|
||||
async reconnect(): Promise<void> {
|
||||
if (!this.saved) throw new KeyboardClientError("No remembered keyboard");
|
||||
await this.connect(this.saved);
|
||||
}
|
||||
|
||||
/** Drop the keyboard but keep it remembered. No automatic reconnect until the next explicit connect or glasses session. */
|
||||
async disconnect(): Promise<void> {
|
||||
this.userDisconnected = true;
|
||||
this.cancelReconnect();
|
||||
const client = this.replaceClient();
|
||||
// A scan or connect this interrupts no longer owns the bridge.
|
||||
this.busy = false;
|
||||
this.dropKeyboard();
|
||||
this.pairing = null;
|
||||
this.error = "";
|
||||
if (!client) {
|
||||
this.releaseOnLink = true;
|
||||
this.setState("unavailable");
|
||||
return;
|
||||
}
|
||||
this.setState("idle");
|
||||
try {
|
||||
await client.query();
|
||||
await client.disable();
|
||||
} catch (e) {
|
||||
this.host.log(`keyboard disconnect: ${messageOf(e)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Forget the remembered keyboard. The glasses keep their bond with it. */
|
||||
async forget(): Promise<void> {
|
||||
this.saved = null;
|
||||
this.host.storeSaved(null);
|
||||
await this.disconnect();
|
||||
this.scanResults = [];
|
||||
this.activeName = "";
|
||||
this.host.onChanged();
|
||||
}
|
||||
|
||||
/** Answer a "compare" pairing prompt. */
|
||||
async answerCompare(accept: boolean): Promise<void> {
|
||||
if (!this.client || this.pairing?.kind !== "compare") throw new KeyboardClientError("No pairing comparison is pending");
|
||||
this.pairing = null;
|
||||
this.host.onChanged();
|
||||
await this.client.confirm(accept);
|
||||
}
|
||||
|
||||
/** Answer an "entry" pairing prompt with the code the keyboard shows. */
|
||||
async answerPasskey(pin: number): Promise<void> {
|
||||
if (!this.client || this.pairing?.kind !== "entry") throw new KeyboardClientError("No pairing code is pending");
|
||||
this.pairing = null;
|
||||
this.host.onChanged();
|
||||
await this.client.authenticate(pin);
|
||||
}
|
||||
|
||||
// ---------- Internals ----------
|
||||
|
||||
private beginUserOperation(): KeyboardClient {
|
||||
if (!this.available) throw new KeyboardClientError("Connect to G2 glasses running Faceclaw firmware first");
|
||||
if (this.busy) throw new KeyboardClientError("Another keyboard operation is in progress");
|
||||
// An automatic attempt may be mid-flight (a sleeping keyboard holds one
|
||||
// for up to 30 s): a fresh client fails it at once and takes over.
|
||||
this.cancelReconnect();
|
||||
const client = this.replaceClient()!;
|
||||
this.dropKeyboard();
|
||||
this.pairing = null;
|
||||
this.busy = true;
|
||||
return client;
|
||||
}
|
||||
|
||||
private endUserOperation(): void {
|
||||
this.busy = false;
|
||||
this.host.onChanged();
|
||||
}
|
||||
|
||||
/** Close the current client and, while the link is up, start a fresh one. */
|
||||
private replaceClient(): KeyboardClient | null {
|
||||
this.generation++;
|
||||
this.client?.close();
|
||||
this.client = null;
|
||||
if (!this.available) return null;
|
||||
const generation = this.generation;
|
||||
const live = () => generation === this.generation;
|
||||
const client = new KeyboardClient(
|
||||
(record) => live() && this.host.send(record),
|
||||
{
|
||||
keys: (events) => { if (live() && this.ready) this.host.onKeys(events); },
|
||||
advert: (advert) => { if (live()) this.addAdvert(advert); },
|
||||
status: (status) => { if (live()) this.onStatus(status); },
|
||||
dm: (event, status) => { if (live()) this.onDm(event, status); },
|
||||
passkeyDisplay: (pin) => { if (live()) this.prompt({ kind: "display", pin }); },
|
||||
passkeyRequest: () => { if (live()) this.prompt({ kind: "entry" }); },
|
||||
compareRequest: (value) => { if (live()) this.prompt({ kind: "compare", value }); },
|
||||
servicesChanged: () => { if (live()) this.onServicesChanged(); },
|
||||
error: (message) => { if (live()) this.host.log(`keyboard: ${message}`); },
|
||||
log: (line) => { if (live()) this.host.log(`keyboard setup: ${line}`); },
|
||||
},
|
||||
{ randomPasskey: this.host.randomPasskey ? () => this.host.randomPasskey!() : undefined },
|
||||
);
|
||||
this.client = client;
|
||||
return client;
|
||||
}
|
||||
|
||||
private async connectWith(
|
||||
client: KeyboardClient,
|
||||
generation: number,
|
||||
keyboard: SavedKeyboard,
|
||||
resume: boolean,
|
||||
): Promise<void> {
|
||||
const live = () => generation === this.generation;
|
||||
this.error = "";
|
||||
this.activeName = keyboard.name;
|
||||
this.setState("connecting");
|
||||
try {
|
||||
const status = await client.query();
|
||||
if (resume && status.enabled && status.connected && status.encrypted) {
|
||||
// The glasses kept the keyboard through a phone-side drop (or an app
|
||||
// restart): only the handle mapping is missing.
|
||||
this.host.log("keyboard still connected to the glasses; rediscovering");
|
||||
} else {
|
||||
await this.releaseLink(client);
|
||||
await client.enable();
|
||||
await client.connect(keyboard.addressType, parseKeyboardAddress(keyboard.address)!);
|
||||
}
|
||||
if (!live()) return;
|
||||
this.pairing = null;
|
||||
this.setState("configuring");
|
||||
const config = await client.configure();
|
||||
if (!live()) return;
|
||||
this.ready = true;
|
||||
this.reconnectAttempt = 0;
|
||||
// The list was for choosing; it is stale from here on.
|
||||
this.scanResults = [];
|
||||
if (!sameKeyboard(this.saved, keyboard)) {
|
||||
this.saved = { ...keyboard };
|
||||
this.host.storeSaved(this.saved);
|
||||
}
|
||||
this.host.log(
|
||||
`keyboard ready: ${keyboard.name || keyboard.address}, ${config.watched.length} of ` +
|
||||
`${config.reports.filter((r) => r.type === 1).length} input reports subscribed, epoch ${config.epoch}`);
|
||||
this.setState("connected");
|
||||
} catch (e) {
|
||||
if (!live()) return;
|
||||
this.ready = false;
|
||||
this.pairing = null;
|
||||
if (resume) {
|
||||
// An automatic attempt: a keyboard that is asleep or out of range is
|
||||
// the ordinary case, not an error to put in front of the user.
|
||||
this.host.log(`keyboard reconnect: ${messageOf(friendly(e))}`, true);
|
||||
this.setState("idle");
|
||||
} else {
|
||||
this.fail(e);
|
||||
}
|
||||
throw friendly(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The firmware refuses SCAN and CONNECT while it still owns a keyboard
|
||||
* slot, and an interrupted connect keeps initiating until the close lands.
|
||||
* DISABLE is epoch-checked and a fresh client hasn't seen the epoch yet,
|
||||
* so QUERY first (it isn't, and its answer carries the epoch), then wait
|
||||
* for the slot to clear.
|
||||
*/
|
||||
private async releaseLink(client: KeyboardClient): Promise<void> {
|
||||
const status = await client.query();
|
||||
if (!status.enabled && status.conn === 0) return;
|
||||
await client.disable();
|
||||
for (let i = 0; i < RELEASE_POLLS; i++) {
|
||||
if ((await client.query()).conn === 0) return;
|
||||
await delay(RELEASE_POLL_MS);
|
||||
}
|
||||
throw new KeyboardClientError("The glasses are still closing the previous keyboard link; try again in a moment");
|
||||
}
|
||||
|
||||
private cancelReconnect(): void {
|
||||
if (this.reconnectTimer !== null) clearTimeout(this.reconnectTimer);
|
||||
this.reconnectTimer = null;
|
||||
}
|
||||
|
||||
private scheduleReconnect(delayMs: number): void {
|
||||
this.cancelReconnect();
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.reconnectTimer = null;
|
||||
void this.attemptReconnect();
|
||||
}, delayMs);
|
||||
}
|
||||
|
||||
private async attemptReconnect(): Promise<void> {
|
||||
const saved = this.saved;
|
||||
const client = this.client;
|
||||
if (!this.available || !client || !saved || this.userDisconnected || this.ready || this.busy) return;
|
||||
if (this.host.shouldReconnect && !this.host.shouldReconnect()) {
|
||||
this.parkFirmware(client);
|
||||
this.scheduleReconnect(RECONNECT_BACKOFF_MS[RECONNECT_BACKOFF_MS.length - 1]!);
|
||||
return;
|
||||
}
|
||||
const generation = this.generation;
|
||||
try {
|
||||
await this.connectWith(client, generation, saved, true);
|
||||
} catch {
|
||||
if (generation !== this.generation || !this.available || this.userDisconnected) return;
|
||||
// Keyboards sleep, and the ring's own scan can hold the controller
|
||||
// (BUSY): keep trying, less often, while the glasses stay up.
|
||||
const backoff = RECONNECT_BACKOFF_MS[Math.min(this.reconnectAttempt, RECONNECT_BACKOFF_MS.length - 1)]!;
|
||||
this.reconnectAttempt++;
|
||||
this.scheduleReconnect(backoff);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* While it is enabled the firmware bridge runs a 20 ms timer and reports
|
||||
* its status every second, keyboard or no keyboard. When nothing will be
|
||||
* tried for a while, switch it off; the next attempt enables it again.
|
||||
*/
|
||||
private parkFirmware(client: KeyboardClient): void {
|
||||
if (!client.status.enabled) return;
|
||||
client.disable().catch(() => {});
|
||||
}
|
||||
|
||||
private onStatus(status: KeyboardStatus): void {
|
||||
if (this.ready && (!status.connected || !status.encrypted)) this.keyboardDropped("status");
|
||||
else if (this.state === "connecting" && status.connected && !status.encrypted) this.setState("pairing");
|
||||
}
|
||||
|
||||
private onDm(event: number, status: number): void {
|
||||
if (event === KeyboardDm.connClose || event === KeyboardDm.pairFailed || event === KeyboardDm.encryptFailed) {
|
||||
this.host.log(`keyboard DM event 0x${event.toString(16)} status ${status}`);
|
||||
if (this.ready) this.keyboardDropped("dm");
|
||||
}
|
||||
}
|
||||
|
||||
private onServicesChanged(): void {
|
||||
this.host.log("keyboard services changed; rediscovering");
|
||||
this.dropKeyboard();
|
||||
this.setState("idle");
|
||||
if (this.saved && !this.userDisconnected && !this.busy) this.scheduleReconnect(0);
|
||||
}
|
||||
|
||||
private keyboardDropped(reason: string): void {
|
||||
this.host.log(`keyboard disconnected (${reason})`);
|
||||
this.dropKeyboard();
|
||||
this.setState("idle");
|
||||
if (this.saved && !this.userDisconnected && !this.busy) this.scheduleReconnect(DROP_RETRY_DELAY_MS);
|
||||
}
|
||||
|
||||
/** The keyboard is no longer usable: tell the host once. */
|
||||
private dropKeyboard(): void {
|
||||
if (!this.ready) return;
|
||||
this.ready = false;
|
||||
this.host.onKeyboardGone();
|
||||
}
|
||||
|
||||
private addAdvert(advert: KeyboardAdvert): void {
|
||||
const index = this.scanResults.findIndex(
|
||||
(a) => a.address === advert.address && a.addressType === advert.addressType);
|
||||
if (index < 0) {
|
||||
this.scanResults.push(advert);
|
||||
} else {
|
||||
const merged = mergeKeyboardAdverts(this.scanResults[index]!, advert);
|
||||
const old = this.scanResults[index]!;
|
||||
if (merged.name === old.name && merged.hid === old.hid && merged.rssi === old.rssi) return;
|
||||
this.scanResults[index] = merged;
|
||||
}
|
||||
this.host.onChanged();
|
||||
}
|
||||
|
||||
private prompt(pairing: KeyboardPairingPrompt): void {
|
||||
this.pairing = pairing;
|
||||
this.setState("pairing");
|
||||
}
|
||||
|
||||
private setState(next: KeyboardBridgeState): void {
|
||||
this.state = next;
|
||||
if (next !== "pairing" && next !== "connecting") this.pairing = null;
|
||||
this.host.onChanged();
|
||||
}
|
||||
|
||||
private fail(error: unknown): void {
|
||||
this.error = messageOf(friendly(error));
|
||||
this.host.log(`keyboard error: ${this.error}`);
|
||||
this.setState("error");
|
||||
}
|
||||
}
|
||||
|
||||
function sameKeyboard(a: SavedKeyboard | null, b: SavedKeyboard): boolean {
|
||||
return !!a && a.address === b.address && a.addressType === b.addressType && a.name === b.name;
|
||||
}
|
||||
|
||||
function sortAdverts(adverts: readonly KeyboardAdvert[]): KeyboardAdvert[] {
|
||||
return [...adverts].sort((a, b) => Number(b.hid) - Number(a.hid) || b.rssi - a.rssi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Firmware BUSY nearly always means the ring's own scan or reconnect holds
|
||||
* the radio; say that rather than the raw control error.
|
||||
*/
|
||||
function friendly(error: unknown): Error {
|
||||
if (error instanceof KeyboardClientError && error.code === KeyboardError.busy) {
|
||||
return new KeyboardClientError(
|
||||
"The glasses' radio is busy (usually the ring reconnecting). Wait a few seconds and try again.", error.code);
|
||||
}
|
||||
if (error instanceof KeyboardClientError && error.code === KeyboardError.noSpace) {
|
||||
return new KeyboardClientError(
|
||||
"The glasses have no room for another connection or pairing.", error.code);
|
||||
}
|
||||
return error instanceof Error ? error : new Error(String(error));
|
||||
}
|
||||
|
||||
function messageOf(error: unknown): string {
|
||||
return error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
|
||||
function delay(ms: number): Promise<void> {
|
||||
return new Promise<void>((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
@@ -0,0 +1,709 @@
|
||||
/**
|
||||
* Transport-independent client for the CFW keyboard bridge: enable, scan,
|
||||
* connect and pair a keyboard through the glasses, discover its HID service
|
||||
* over the relayed ATT operations, subscribe its input reports, and decode
|
||||
* them into key transitions. From the reference client in g2flash demos/
|
||||
* (keyboard-client.ts), with the fixes its use in the Glassly app turned up.
|
||||
*
|
||||
* The firmware keeps almost no policy: it owns the connection and the bond,
|
||||
* and forwards one ATT request at a time. Everything HID lives here. Handle
|
||||
* numbers found by configure() are only good for one connection epoch.
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
import { HidKeys, parseReportMap, reportHasKeys, type HidKeyEvent, type HidReport } from "./hid";
|
||||
import {
|
||||
KeyboardDm,
|
||||
KeyboardError,
|
||||
KeyboardKind,
|
||||
KeyboardOp,
|
||||
KeyboardRecords,
|
||||
keyboardControl,
|
||||
keyboardErrorName,
|
||||
le16,
|
||||
parseKeyboardAdvert,
|
||||
u16,
|
||||
u32,
|
||||
type KeyboardAdvert,
|
||||
type KeyboardRecord,
|
||||
} from "./protocol";
|
||||
|
||||
/** Queue one field-130 control record; false when the glasses link can't take it now. */
|
||||
export type KeyboardTransport = (record: Uint8Array) => boolean;
|
||||
|
||||
export type KeyboardStatus = {
|
||||
enabled: boolean;
|
||||
/** The firmware's connection id for the keyboard, 0 when it owns none. */
|
||||
conn: number;
|
||||
connected: boolean;
|
||||
encrypted: boolean;
|
||||
scanning: boolean;
|
||||
};
|
||||
|
||||
export type KeyboardClientEvents = {
|
||||
/** Key transitions from one input report (or the releases a gap forces). */
|
||||
keys?(events: HidKeyEvent[]): void;
|
||||
advert?(advert: KeyboardAdvert): void;
|
||||
status?(status: KeyboardStatus): void;
|
||||
/** The stream may have lost records; held keys were released. */
|
||||
gap?(): void;
|
||||
/** Device-manager event from the glasses' BLE host (KeyboardDm). */
|
||||
dm?(event: number, status: number): void;
|
||||
/** The keyboard shows a six-digit passkey; answer with authenticate(). */
|
||||
passkeyRequest?(): void;
|
||||
/** We chose a passkey and answered with it; the user types it on the keyboard, then Enter. */
|
||||
passkeyDisplay?(pin: number): void;
|
||||
/** Numeric comparison: answer with confirm() once the user has compared. */
|
||||
compareRequest?(value: number): void;
|
||||
/** The keyboard's GATT table changed; the mapping is void until configure() runs again. */
|
||||
servicesChanged?(): void;
|
||||
error?(message: string): void;
|
||||
/** Setup diagnostics: the keyboard's attribute table and what was made of it. Never key contents. */
|
||||
log?(line: string): void;
|
||||
};
|
||||
|
||||
export type KeyboardReport = {
|
||||
/** Handle of the HID service declaration the report belongs to. */
|
||||
service: number;
|
||||
handle: number;
|
||||
id: number;
|
||||
/** 1 input, 2 output, 3 feature. */
|
||||
type: number;
|
||||
ccc?: number;
|
||||
/** From the characteristic's declaration; undefined when the keyboard wouldn't say (see declaredProperties). */
|
||||
properties?: number;
|
||||
map: Uint8Array;
|
||||
};
|
||||
|
||||
export type KeyboardConfiguration = {
|
||||
epoch: number;
|
||||
/** Every report found, input or not. */
|
||||
reports: KeyboardReport[];
|
||||
/** Handles of the input reports subscribed for keys. */
|
||||
watched: number[];
|
||||
serviceChanged?: number;
|
||||
};
|
||||
|
||||
export type KeyboardClientOptions = {
|
||||
/** A fresh six-digit passkey for display pairing. Default: Math.random (pass a stronger source). */
|
||||
randomPasskey?: () => number;
|
||||
};
|
||||
|
||||
export class KeyboardClientError extends Error {
|
||||
/** The firmware's control error code (KeyboardError), when that is what failed. */
|
||||
readonly code: number | undefined;
|
||||
/** The keyboard's ATT error code, when it refused a read or write. */
|
||||
readonly attStatus: number | undefined;
|
||||
constructor(message: string, code?: number, attStatus?: number) {
|
||||
super(message);
|
||||
this.name = "KeyboardClientError";
|
||||
this.code = code;
|
||||
this.attStatus = attStatus;
|
||||
}
|
||||
}
|
||||
|
||||
type Attribute = { handle: number; uuid: number | string };
|
||||
type Pending = {
|
||||
op: number;
|
||||
resolve: (r: KeyboardRecord) => void;
|
||||
reject: (e: Error) => void;
|
||||
timer: ReturnType<typeof setTimeout>;
|
||||
};
|
||||
type Waiter = { test: () => boolean; resolve: () => void; reject: (e: Error) => void; timer: ReturnType<typeof setTimeout> };
|
||||
type Input = { report: HidReport; id: number; service: number };
|
||||
|
||||
const COMMAND_TIMEOUT_MS = 32_000;
|
||||
const MAX_PENDING = 8;
|
||||
const MAX_ATTRIBUTES = 512;
|
||||
const MAX_VALUE = 512;
|
||||
const MAX_WATCHES = 16;
|
||||
|
||||
const UUID_PRIMARY_SERVICE = 0x2800;
|
||||
const UUID_SECONDARY_SERVICE = 0x2801;
|
||||
const UUID_CHARACTERISTIC = 0x2803;
|
||||
const UUID_CCC = 0x2902;
|
||||
const UUID_EXTERNAL_REPORT_REFERENCE = 0x2907;
|
||||
const UUID_REPORT_REFERENCE = 0x2908;
|
||||
const UUID_HID_SERVICE = 0x1812;
|
||||
const UUID_SERVICE_CHANGED = 0x2a05;
|
||||
const UUID_REPORT_MAP = 0x2a4b;
|
||||
const UUID_REPORT = 0x2a4d;
|
||||
const UUID_PROTOCOL_MODE = 0x2a4e;
|
||||
|
||||
/** ATT error codes that end a paged read rather than failing it. */
|
||||
const ATT_INVALID_OFFSET = 7;
|
||||
const ATT_NOT_LONG = 11;
|
||||
const ATT_NOT_FOUND = 10;
|
||||
|
||||
/**
|
||||
* ATT UUIDs are little endian. A 128-bit UUID on the Bluetooth base collapses
|
||||
* to its 16-bit form; vendor UUIDs stay as hex strings.
|
||||
*/
|
||||
export function attributeUuid(bytes: Uint8Array): number | string {
|
||||
if (bytes.length === 2) return u16(bytes);
|
||||
if (bytes.length !== 16) throw new KeyboardClientError("Invalid ATT UUID length");
|
||||
const base = [0xfb, 0x34, 0x9b, 0x5f, 0x80, 0, 0, 0x80, 0, 0x10, 0, 0];
|
||||
if (base.every((v, i) => bytes[i] === v) && bytes[14] === 0 && bytes[15] === 0) return u16(bytes, 12);
|
||||
return Array.from(bytes, (x) => x.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
|
||||
export class KeyboardClient {
|
||||
/** The firmware's connection generation; every control but QUERY/ENABLE must carry the live one. */
|
||||
epoch = 0;
|
||||
status: KeyboardStatus = { enabled: false, conn: 0, connected: false, encrypted: false, scanning: false };
|
||||
configuration: KeyboardConfiguration | undefined;
|
||||
|
||||
private serial = 0;
|
||||
private closed = false;
|
||||
/** The link under connect() went away (close, failed open, failed pairing). */
|
||||
private linkLost = false;
|
||||
private readonly pending = new Map<number, Pending>();
|
||||
private readonly waiters = new Set<Waiter>();
|
||||
private readonly keys = new HidKeys();
|
||||
private inputs = new Map<number, Input>();
|
||||
private readonly records = new KeyboardRecords(() => this.gap());
|
||||
private readonly randomPasskey: () => number;
|
||||
|
||||
constructor(
|
||||
private readonly transport: KeyboardTransport,
|
||||
private readonly events: KeyboardClientEvents = {},
|
||||
options: KeyboardClientOptions = {},
|
||||
) {
|
||||
this.randomPasskey = options.randomPasskey ?? (() => Math.floor(Math.random() * 1_000_000));
|
||||
}
|
||||
|
||||
// ---------- Inbound ----------
|
||||
|
||||
/** Feed one field-131 payload, in arrival order. */
|
||||
accept(data: Uint8Array): void {
|
||||
if (this.closed) return;
|
||||
const r = this.records.push(data);
|
||||
if (!r) return;
|
||||
this.epoch = r.epoch;
|
||||
if (r.kind === KeyboardKind.status && r.data.length === 10) {
|
||||
this.status = {
|
||||
enabled: !!r.data[0], conn: r.data[1]!, connected: !!r.data[2], encrypted: !!r.data[3], scanning: !!r.data[4],
|
||||
};
|
||||
if (!this.status.enabled || !this.status.connected || !this.status.encrypted) this.release();
|
||||
this.events.status?.(this.status);
|
||||
}
|
||||
const pending = this.pending.get(r.request);
|
||||
if (pending && (r.kind === KeyboardKind.status || r.kind === KeyboardKind.att)) {
|
||||
this.pending.delete(r.request);
|
||||
clearTimeout(pending.timer);
|
||||
if (r.kind === KeyboardKind.status && r.code) {
|
||||
pending.reject(new KeyboardClientError(
|
||||
`Keyboard command ${opName(pending.op)}: firmware ${keyboardErrorName(r.code)}`, r.code));
|
||||
} else {
|
||||
pending.resolve(r);
|
||||
}
|
||||
}
|
||||
if (r.kind === KeyboardKind.dm) this.acceptDm(r);
|
||||
else if (r.kind === KeyboardKind.input) this.acceptInput(r);
|
||||
else if (r.kind === KeyboardKind.advert) {
|
||||
const advert = parseKeyboardAdvert(r.data);
|
||||
if (advert) this.events.advert?.(advert);
|
||||
}
|
||||
this.checkWaiters();
|
||||
}
|
||||
|
||||
private acceptDm(r: KeyboardRecord): void {
|
||||
const event = r.code & 255;
|
||||
const status = r.code >>> 8;
|
||||
if (event === KeyboardDm.connClose || event === KeyboardDm.pairFailed || event === KeyboardDm.encryptFailed ||
|
||||
(event === KeyboardDm.connOpen && status !== 0)) {
|
||||
this.linkLost = true;
|
||||
this.gap();
|
||||
this.inputs.clear();
|
||||
this.configuration = undefined;
|
||||
}
|
||||
if (event === KeyboardDm.authRequest && r.data.length === 2) {
|
||||
if (r.data[0]) {
|
||||
this.command(KeyboardOp.disconnect).catch(() => {});
|
||||
this.events.error?.("This keyboard only pairs out of band, which isn't supported.");
|
||||
} else if (r.data[1]) {
|
||||
// KeyboardDisplay on our side: choose the passkey, show it, and
|
||||
// answer at once; the user types it on the keyboard.
|
||||
const pin = Math.min(999_999, Math.max(0, Math.floor(this.randomPasskey())));
|
||||
this.events.passkeyDisplay?.(pin);
|
||||
this.authenticate(pin).catch((e) => this.events.error?.(messageOf(e)));
|
||||
} else {
|
||||
this.events.passkeyRequest?.();
|
||||
}
|
||||
}
|
||||
if (event === KeyboardDm.compare && r.data.length === 4) this.events.compareRequest?.(u32(r.data));
|
||||
this.events.dm?.(event, status);
|
||||
}
|
||||
|
||||
private acceptInput(r: KeyboardRecord): void {
|
||||
const changed = this.configuration?.serviceChanged;
|
||||
if (changed !== undefined && r.handle === changed) {
|
||||
this.gap();
|
||||
this.inputs.clear();
|
||||
this.configuration = undefined;
|
||||
this.events.servicesChanged?.();
|
||||
return;
|
||||
}
|
||||
const input = this.inputs.get(r.handle);
|
||||
if (!input || this.configuration?.epoch !== r.epoch) {
|
||||
this.gap();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const events = this.keys.update(r.handle, input.report, r.data);
|
||||
if (events.length) this.events.keys?.(events);
|
||||
} catch (e) {
|
||||
this.release();
|
||||
this.events.error?.(messageOf(e));
|
||||
}
|
||||
}
|
||||
|
||||
private release(): void {
|
||||
const events = this.keys.reset();
|
||||
if (events.length) this.events.keys?.(events);
|
||||
}
|
||||
|
||||
private gap(): void {
|
||||
this.release();
|
||||
this.events.gap?.();
|
||||
for (const [id, p] of this.pending) {
|
||||
if (p.op < KeyboardOp.find) continue;
|
||||
clearTimeout(p.timer);
|
||||
this.pending.delete(id);
|
||||
p.reject(new KeyboardClientError("Keyboard stream gap during ATT transaction"));
|
||||
}
|
||||
}
|
||||
|
||||
/** The phone's link to the glasses dropped: release held keys and fail everything in flight. */
|
||||
lostPhoneLink(): void {
|
||||
this.records.reset();
|
||||
this.cancel("Glasses link lost");
|
||||
}
|
||||
|
||||
/** Stop for good; later accept() calls are ignored. */
|
||||
close(): void {
|
||||
if (this.closed) return;
|
||||
this.lostPhoneLink();
|
||||
this.closed = true;
|
||||
}
|
||||
|
||||
private cancel(reason: string): void {
|
||||
for (const p of this.pending.values()) {
|
||||
clearTimeout(p.timer);
|
||||
p.reject(new KeyboardClientError(reason));
|
||||
}
|
||||
this.pending.clear();
|
||||
for (const w of this.waiters) {
|
||||
clearTimeout(w.timer);
|
||||
w.reject(new KeyboardClientError(reason));
|
||||
}
|
||||
this.waiters.clear();
|
||||
}
|
||||
|
||||
private checkWaiters(): void {
|
||||
for (const w of [...this.waiters]) {
|
||||
let done = false;
|
||||
try {
|
||||
done = w.test();
|
||||
} catch (e) {
|
||||
this.waiters.delete(w);
|
||||
clearTimeout(w.timer);
|
||||
w.reject(e instanceof Error ? e : new KeyboardClientError(String(e)));
|
||||
continue;
|
||||
}
|
||||
if (!done) continue;
|
||||
this.waiters.delete(w);
|
||||
clearTimeout(w.timer);
|
||||
w.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
/** Resolve once `test` holds; it is re-evaluated after every accepted record. */
|
||||
private waitFor(test: () => boolean, timeoutMs: number, what: string): Promise<void> {
|
||||
if (this.closed) return Promise.reject(new KeyboardClientError("Keyboard client closed"));
|
||||
if (test()) return Promise.resolve();
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const waiter: Waiter = {
|
||||
test, resolve, reject,
|
||||
timer: setTimeout(() => {
|
||||
this.waiters.delete(waiter);
|
||||
reject(new KeyboardClientError(`Timed out waiting for ${what}`));
|
||||
}, timeoutMs),
|
||||
};
|
||||
this.waiters.add(waiter);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------- Commands ----------
|
||||
|
||||
/** Send one control and await the STATUS or ATT record that answers it. */
|
||||
command(op: number, body: readonly number[] = []): Promise<KeyboardRecord> {
|
||||
if (this.closed) return Promise.reject(new KeyboardClientError("Keyboard client closed"));
|
||||
if (this.pending.size >= MAX_PENDING) return Promise.reject(new KeyboardClientError("Too many keyboard commands"));
|
||||
const request = this.serial = this.serial % 65535 + 1;
|
||||
return new Promise<KeyboardRecord>((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
this.pending.delete(request);
|
||||
reject(new KeyboardClientError(`Keyboard command ${opName(op)} timed out`));
|
||||
}, COMMAND_TIMEOUT_MS);
|
||||
this.pending.set(request, { op, resolve, reject, timer });
|
||||
let queued = false;
|
||||
try {
|
||||
queued = this.transport(keyboardControl(op, request, this.epoch, body));
|
||||
} catch {
|
||||
queued = false;
|
||||
}
|
||||
if (!queued) {
|
||||
clearTimeout(timer);
|
||||
this.pending.delete(request);
|
||||
reject(new KeyboardClientError("The glasses aren't ready for keyboard commands"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async enable(): Promise<void> {
|
||||
await this.command(KeyboardOp.enable);
|
||||
}
|
||||
|
||||
/** Disconnect the keyboard and stop the bridge; the bond stays. */
|
||||
async disable(): Promise<void> {
|
||||
await this.command(KeyboardOp.disable);
|
||||
this.release();
|
||||
}
|
||||
|
||||
/** Ask for the firmware's state. Also the way to learn the live epoch: QUERY isn't epoch-checked. */
|
||||
async query(): Promise<KeyboardStatus> {
|
||||
await this.command(KeyboardOp.query);
|
||||
return this.status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Active scan for about five seconds; adverts arrive through events.advert.
|
||||
* The firmware only clears `scanning` once the stock scanner is idle again,
|
||||
* and a ring looking for its lens can hold that well past the window, so a
|
||||
* lingering flag is not an error: the adverts already delivered stand.
|
||||
*/
|
||||
async scan(): Promise<void> {
|
||||
await this.command(KeyboardOp.scan);
|
||||
try {
|
||||
await this.waitFor(() => !this.status.scanning, 8000, "the scan to finish");
|
||||
} catch (e) {
|
||||
if (this.closed) throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/** Connect, then pair unless the link comes up encrypted. `address` is LSB first. */
|
||||
async connect(addressType: number, address: Uint8Array): Promise<void> {
|
||||
if (address.length !== 6 || !Number.isInteger(addressType) || addressType < 0 || addressType > 3) {
|
||||
throw new KeyboardClientError("Invalid BLE address");
|
||||
}
|
||||
this.release();
|
||||
this.inputs.clear();
|
||||
this.configuration = undefined;
|
||||
this.linkLost = false;
|
||||
await this.command(KeyboardOp.connect, [addressType, ...address]);
|
||||
await this.waitFor(() => this.status.connected || this.linkLost, 32_000, "the keyboard to connect");
|
||||
if (!this.status.connected) throw new KeyboardClientError("The keyboard didn't answer");
|
||||
if (!this.status.encrypted) {
|
||||
// The keyboard's own security request may have started pairing already.
|
||||
// BUSY is not success: the encrypted status below still decides.
|
||||
try {
|
||||
await this.command(KeyboardOp.pair);
|
||||
} catch (e) {
|
||||
if (!(e instanceof KeyboardClientError) || e.code !== KeyboardError.busy) throw e;
|
||||
}
|
||||
await this.waitFor(() => this.status.encrypted || this.linkLost, 65_000, "pairing to finish");
|
||||
if (!this.status.encrypted) throw new KeyboardClientError("Pairing with the keyboard failed");
|
||||
}
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
await this.command(KeyboardOp.disconnect);
|
||||
this.release();
|
||||
}
|
||||
|
||||
/** Answer a pending passkey prompt. */
|
||||
async authenticate(pin: number): Promise<void> {
|
||||
if (!Number.isInteger(pin) || pin < 0 || pin > 999_999) throw new KeyboardClientError("Invalid pairing passkey");
|
||||
await this.command(KeyboardOp.auth, [pin & 255, (pin >>> 8) & 255, pin >>> 16]);
|
||||
}
|
||||
|
||||
/** Answer a pending numeric comparison. */
|
||||
async confirm(accept: boolean): Promise<void> {
|
||||
await this.command(KeyboardOp.compare, [accept ? 1 : 0]);
|
||||
}
|
||||
|
||||
private async att(op: number, body: readonly number[]): Promise<KeyboardRecord> {
|
||||
const r = await this.command(op, body);
|
||||
if (r.kind !== KeyboardKind.att) throw new KeyboardClientError("Expected keyboard ATT response");
|
||||
return r;
|
||||
}
|
||||
|
||||
/** Read a whole attribute: Read, then Read Blob until a short or empty reply. Never assumes the MTU. */
|
||||
async read(handle: number): Promise<Uint8Array> {
|
||||
const parts: number[] = [];
|
||||
for (;;) {
|
||||
const r = await this.att(KeyboardOp.read, [...le16(handle), ...le16(parts.length)]);
|
||||
const status = r.code >>> 8;
|
||||
if (parts.length && (status === ATT_INVALID_OFFSET || status === ATT_NOT_LONG)) break;
|
||||
if (status) throw new KeyboardClientError(`ATT read ${handle}: ${status}`, undefined, status);
|
||||
if (!r.data.length) break;
|
||||
for (const b of r.data) parts.push(b);
|
||||
if (parts.length > MAX_VALUE) throw new KeyboardClientError("Attribute exceeds 512 bytes");
|
||||
if (parts.length === MAX_VALUE) break;
|
||||
}
|
||||
return Uint8Array.from(parts);
|
||||
}
|
||||
|
||||
/** Read a short attribute once (a Report Reference, a declaration, a service UUID). */
|
||||
private async shortRead(handle: number): Promise<Uint8Array> {
|
||||
const r = await this.att(KeyboardOp.read, [...le16(handle), 0, 0]);
|
||||
if (r.code >>> 8) throw new KeyboardClientError(`ATT read ${handle}: ${r.code >>> 8}`, undefined, r.code >>> 8);
|
||||
return r.data;
|
||||
}
|
||||
|
||||
/** Write 1..18 bytes (an output report such as the LEDs, a CCC, the protocol mode). */
|
||||
async write(handle: number, value: Uint8Array, withoutResponse = false): Promise<void> {
|
||||
if (!value.length || value.length > 18) throw new KeyboardClientError("Keyboard write supports 1..18 bytes");
|
||||
const r = await this.att(KeyboardOp.write, [...le16(handle), withoutResponse ? 1 : 0, value.length, ...value]);
|
||||
if (r.code >>> 8) throw new KeyboardClientError(`ATT write ${handle}: ${r.code >>> 8}`, undefined, r.code >>> 8);
|
||||
}
|
||||
|
||||
private async attributes(): Promise<Attribute[]> {
|
||||
const attrs: Attribute[] = [];
|
||||
let start = 1;
|
||||
while (start <= 65535) {
|
||||
const r = await this.att(KeyboardOp.find, [...le16(start), 255, 255]);
|
||||
const status = r.code >>> 8;
|
||||
if (status === ATT_NOT_FOUND) break; // the normal end of the table
|
||||
if (status) throw new KeyboardClientError(`ATT Find Information: ${status}`);
|
||||
const width = r.data[0] === 1 ? 4 : r.data[0] === 2 ? 18 : 0;
|
||||
if (!width || r.data.length <= 1 || (r.data.length - 1) % width) {
|
||||
throw new KeyboardClientError("Malformed Find Information response");
|
||||
}
|
||||
for (let i = 1; i < r.data.length; i += width) {
|
||||
const handle = u16(r.data, i);
|
||||
if (handle < start || attrs.length >= MAX_ATTRIBUTES) throw new KeyboardClientError("Invalid or excessive GATT handles");
|
||||
attrs.push({ handle, uuid: attributeUuid(r.data.subarray(i + 2, i + width)) });
|
||||
start = handle + 1;
|
||||
}
|
||||
}
|
||||
return attrs;
|
||||
}
|
||||
|
||||
/**
|
||||
* The properties byte of a characteristic, from its declaration, or
|
||||
* undefined when the keyboard won't say. A GATT client normally learns
|
||||
* declarations through Read By Type, which the bridge doesn't relay, so
|
||||
* this reads the declaration attribute itself. Most stacks answer that
|
||||
* with the full declaration. Some keep only the properties byte there and
|
||||
* build the rest for Read By Type alone (older Telink SDKs, common in
|
||||
* small keyboards), and some refuse the read. None of that is a reason to
|
||||
* turn a keyboard away: the report's handle and UUID are already known.
|
||||
*/
|
||||
private async declaredProperties(attrs: readonly Attribute[], a: Attribute): Promise<number | undefined> {
|
||||
const decl = attrs.filter((d) => d.handle < a.handle && d.uuid === UUID_CHARACTERISTIC).pop();
|
||||
if (!decl) return undefined;
|
||||
let declaration: Uint8Array;
|
||||
try {
|
||||
declaration = await this.shortRead(decl.handle);
|
||||
} catch (e) {
|
||||
const status = attRefusal(e);
|
||||
this.events.log?.(`declaration ${decl.handle} of ${a.handle}: read refused (ATT ${status})`);
|
||||
return undefined;
|
||||
}
|
||||
if (declaration.length === 5 || declaration.length === 19) {
|
||||
if (u16(declaration, 1) === a.handle && attributeUuid(declaration.subarray(3)) === a.uuid) return declaration[0]!;
|
||||
this.events.log?.(`declaration ${decl.handle} of ${a.handle}: describes something else (${hex(declaration)})`);
|
||||
return undefined;
|
||||
}
|
||||
if (declaration.length === 1) return declaration[0]!;
|
||||
this.events.log?.(`declaration ${decl.handle} of ${a.handle}: unexpected value (${hex(declaration)})`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover every HID service, its Report Map and each report's Report
|
||||
* Reference; select Report Protocol and subscribe the input reports that
|
||||
* can carry keys. Needs an encrypted link. No handle is assumed.
|
||||
*
|
||||
* Only what typing needs is required of the keyboard: a Report Map that
|
||||
* parses, and at least one key-bearing input report with a Report
|
||||
* Reference and a CCC. A report that can't be placed is skipped and
|
||||
* logged, as is anything wrong with an external report (the battery
|
||||
* level, usually).
|
||||
*/
|
||||
async configure(): Promise<KeyboardConfiguration> {
|
||||
const epoch = this.epoch;
|
||||
const log = (line: string): void => this.events.log?.(line);
|
||||
// A new discovery replaces any earlier mapping, whether or not it succeeds.
|
||||
this.release();
|
||||
this.inputs.clear();
|
||||
this.configuration = undefined;
|
||||
const attrs = await this.attributes();
|
||||
log(`gatt ${attrs.map((a) => `${a.handle}=${typeof a.uuid === "number" ? a.uuid.toString(16) : a.uuid}`).join(" ")}`);
|
||||
const reports: KeyboardReport[] = [];
|
||||
const watched: KeyboardReport[] = [];
|
||||
const skipped: string[] = [];
|
||||
const descriptorsFor = (handle: number): Attribute[] => {
|
||||
const end = attrs.find((a) => a.handle > handle &&
|
||||
(a.uuid === UUID_PRIMARY_SERVICE || a.uuid === UUID_SECONDARY_SERVICE || a.uuid === UUID_CHARACTERISTIC))?.handle ?? 65536;
|
||||
return attrs.filter((a) => a.handle > handle && a.handle < end);
|
||||
};
|
||||
/** Why a report was left out; the link going away is not a reason to carry on. */
|
||||
const skip = (a: Attribute, reason: string): void => {
|
||||
skipped.push(`${a.handle}: ${reason}`);
|
||||
log(`report ${a.handle} skipped: ${reason}`);
|
||||
};
|
||||
const addReport = async (service: number, a: Attribute, map: Uint8Array, parsed: Map<number, HidReport>) => {
|
||||
if (reports.some((r) => r.handle === a.handle)) return skip(a, "already listed by another HID service");
|
||||
const descriptors = descriptorsFor(a.handle);
|
||||
const ref = descriptors.find((d) => d.uuid === UUID_REPORT_REFERENCE);
|
||||
if (!ref) return skip(a, "no Report Reference descriptor");
|
||||
let reference: Uint8Array;
|
||||
try {
|
||||
reference = await this.shortRead(ref.handle);
|
||||
} catch (e) {
|
||||
return skip(a, `Report Reference ${ref.handle} unreadable (ATT ${attRefusal(e)})`);
|
||||
}
|
||||
if (reference.length !== 2) return skip(a, `Report Reference ${ref.handle} is ${hex(reference) || "empty"}`);
|
||||
const id = reference[0]!;
|
||||
const type = reference[1]!;
|
||||
if (type < 1 || type > 3) return skip(a, `Report Reference type ${type}`);
|
||||
const ccc = descriptors.find((d) => d.uuid === UUID_CCC)?.handle;
|
||||
const properties = await this.declaredProperties(attrs, a);
|
||||
const report: KeyboardReport = { service, handle: a.handle, id, type, ccc, properties, map };
|
||||
if (type === 1) {
|
||||
const layout = parsed.get(id);
|
||||
if (!layout) return skip(a, `input report id ${id} is not in the Report Map`);
|
||||
if (ccc === undefined || (properties !== undefined && !(properties & 0x10))) return skip(a, "input report can't notify");
|
||||
if (reportHasKeys(layout)) watched.push(report);
|
||||
else log(`report ${a.handle} (id ${id}) carries no keys; not subscribed`);
|
||||
}
|
||||
reports.push(report);
|
||||
log(`report ${a.handle}: id ${id} type ${type} ccc ${ccc ?? "-"} properties ${properties ?? "?"}`);
|
||||
};
|
||||
for (let i = 0; i < attrs.length; i++) {
|
||||
const service = attrs[i]!;
|
||||
if (service.uuid !== UUID_PRIMARY_SERVICE) continue;
|
||||
if (attributeUuid(await this.shortRead(service.handle)) !== UUID_HID_SERVICE) continue;
|
||||
const end = attrs.slice(i + 1).find((a) => a.uuid === UUID_PRIMARY_SERVICE || a.uuid === UUID_SECONDARY_SERVICE)?.handle ?? 65536;
|
||||
const members = attrs.filter((a) => a.handle > service.handle && a.handle < end);
|
||||
const mapAttr = members.find((a) => a.uuid === UUID_REPORT_MAP);
|
||||
if (!mapAttr) throw new KeyboardClientError(`HID service ${service.handle} has no Report Map`);
|
||||
const map = await this.read(mapAttr.handle);
|
||||
log(`report map ${mapAttr.handle}: ${hex(map)}`);
|
||||
const parsed = parseReportMap(map);
|
||||
for (const mode of members.filter((a) => a.uuid === UUID_PROTOCOL_MODE)) {
|
||||
try {
|
||||
await this.write(mode.handle, Uint8Array.of(1), true);
|
||||
} catch (e) {
|
||||
// Report Protocol is the default after a connection; a keyboard
|
||||
// that won't take the write is almost certainly in it already.
|
||||
log(`protocol mode ${mode.handle}: write refused (ATT ${attRefusal(e)})`);
|
||||
}
|
||||
}
|
||||
for (const a of members.filter((m) => m.uuid === UUID_REPORT)) await addReport(service.handle, a, map, parsed);
|
||||
// HOGP lets a Report Map refer to reports in other services (commonly Battery).
|
||||
for (const ext of descriptorsFor(mapAttr.handle).filter((a) => a.uuid === UUID_EXTERNAL_REPORT_REFERENCE)) {
|
||||
let value: Uint8Array;
|
||||
try {
|
||||
value = await this.shortRead(ext.handle);
|
||||
} catch (e) {
|
||||
log(`external report reference ${ext.handle} skipped: unreadable (ATT ${attRefusal(e)})`);
|
||||
continue;
|
||||
}
|
||||
if (value.length !== 2 && value.length !== 16) {
|
||||
log(`external report reference ${ext.handle} skipped: value ${hex(value) || "empty"}`);
|
||||
continue;
|
||||
}
|
||||
const uuid = attributeUuid(value);
|
||||
for (const a of attrs.filter((m) => m.uuid === uuid && (m.handle < service.handle || m.handle >= end))) {
|
||||
await addReport(service.handle, a, map, parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.closed || this.epoch !== epoch) throw new KeyboardClientError("The keyboard link changed during setup");
|
||||
if (!watched.length) {
|
||||
throw new KeyboardClientError(reports.length || skipped.length
|
||||
? `No usable keyboard input report (${skipped.join("; ") || "none carries keys"})`
|
||||
: "This device has no HID service");
|
||||
}
|
||||
if (watched.length > MAX_WATCHES) throw new KeyboardClientError("Keyboard has more than 16 input reports");
|
||||
const changed = attrs.find((a) => a.uuid === UUID_SERVICE_CHANGED);
|
||||
const changedCcc = changed && descriptorsFor(changed.handle).find((a) => a.uuid === UUID_CCC);
|
||||
const watches = watched.map((r) => r.handle);
|
||||
if (changed && changedCcc && watches.length < MAX_WATCHES) watches.push(changed.handle);
|
||||
const serviceChanged = changed && changedCcc && watches.includes(changed.handle) ? changed.handle : undefined;
|
||||
const config: KeyboardConfiguration = { epoch: this.epoch, reports, watched: watched.map((r) => r.handle), serviceChanged };
|
||||
this.restore(config);
|
||||
try {
|
||||
await this.command(KeyboardOp.watch, [watches.length, ...watches.flatMap(le16)]);
|
||||
if (serviceChanged !== undefined) {
|
||||
try {
|
||||
await this.write(changedCcc!.handle, Uint8Array.of(2, 0));
|
||||
} catch (e) {
|
||||
log(`service changed ${changedCcc!.handle}: subscription refused (ATT ${attRefusal(e)})`);
|
||||
}
|
||||
}
|
||||
for (const r of watched) await this.write(r.ccc!, Uint8Array.of(1, 0));
|
||||
} catch (e) {
|
||||
this.inputs.clear();
|
||||
this.configuration = undefined;
|
||||
this.release();
|
||||
throw e;
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Install a mapping found by configure(). It is only good while the glasses
|
||||
* keep the same connection (the same epoch): after a keyboard reconnect or
|
||||
* a firmware boot, discover again.
|
||||
*/
|
||||
restore(config: KeyboardConfiguration): void {
|
||||
if (!config.reports.length || config.reports.length > 64) throw new KeyboardClientError("Invalid keyboard configuration");
|
||||
const inputs = new Map<number, Input>();
|
||||
for (const r of config.reports) {
|
||||
if (r.type !== 1 || !config.watched.includes(r.handle)) continue;
|
||||
if (!Number.isInteger(r.handle) || r.handle < 1 || r.handle > 65535 || inputs.has(r.handle)) {
|
||||
throw new KeyboardClientError("Invalid restored Report handle");
|
||||
}
|
||||
if (!r.map.length || r.map.length > MAX_VALUE) throw new KeyboardClientError("Invalid restored Report Map");
|
||||
const report = parseReportMap(r.map).get(r.id);
|
||||
if (!report) throw new KeyboardClientError("Restored Report ID missing");
|
||||
inputs.set(r.handle, { report, id: r.id, service: r.service });
|
||||
}
|
||||
if (!inputs.size || inputs.size > MAX_WATCHES) throw new KeyboardClientError("Invalid restored input count");
|
||||
this.release();
|
||||
this.inputs = inputs;
|
||||
this.configuration = config;
|
||||
}
|
||||
|
||||
/** Output reports of the configured keyboard (the LED report is the usual one). */
|
||||
outputReports(): KeyboardReport[] {
|
||||
return this.configuration?.reports.filter((r) => r.type === 2) ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The ATT error code when `error` is the keyboard refusing a read or write.
|
||||
* Anything else (the link dropping, a timeout, a stream gap) is rethrown:
|
||||
* those end the setup, where a refusal only costs the one attribute.
|
||||
*/
|
||||
function attRefusal(error: unknown): number {
|
||||
if (error instanceof KeyboardClientError && error.attStatus !== undefined) return error.attStatus;
|
||||
throw error;
|
||||
}
|
||||
|
||||
function hex(bytes: Uint8Array): string {
|
||||
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
|
||||
function opName(op: number): string {
|
||||
for (const [name, value] of Object.entries(KeyboardOp)) if (value === op) return name;
|
||||
return String(op);
|
||||
}
|
||||
|
||||
function messageOf(error: unknown): string {
|
||||
return error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* Bounded HID report-descriptor decoder for the keyboard bridge: turns a
|
||||
* keyboard's Report Map into the layout of each input report, and input
|
||||
* reports into the set of keys held. From the reference client in g2flash
|
||||
* demos/ (keyboard-hid.ts).
|
||||
*
|
||||
* HID over GATT carries the Report ID out of band (in each characteristic's
|
||||
* Report Reference descriptor), so byte zero of a report value is data: it is
|
||||
* never stripped as USB HID would. Only the keyboard/keypad page (7) and the
|
||||
* consumer page (12) produce keys; pointer axes and padding are ignored.
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
|
||||
export type HidUsage = { page: number; usage: number };
|
||||
export type HidKeyEvent = HidUsage & { down: boolean };
|
||||
|
||||
type Global = { page: number; min: number; max: number; size: number; count: number; id: number };
|
||||
type Field = {
|
||||
bit: number;
|
||||
size: number;
|
||||
count: number;
|
||||
variable: boolean;
|
||||
min: number;
|
||||
max: number;
|
||||
usages: HidUsage[];
|
||||
range?: [HidUsage, HidUsage];
|
||||
};
|
||||
export type HidReport = { id: number; bits: number; fields: Field[] };
|
||||
|
||||
const HID_PAGE_KEYBOARD = 7;
|
||||
const HID_PAGE_CONSUMER = 12;
|
||||
const REPORT_MAP_MAX = 512;
|
||||
|
||||
function fail(reason: string): never {
|
||||
throw new Error(`Unsupported or malformed HID descriptor: ${reason}`);
|
||||
}
|
||||
|
||||
const usageKey = (u: HidUsage): string => `${u.page}:${u.usage}`;
|
||||
|
||||
/** Input reports by Report ID (0 when the descriptor uses none). Throws on anything out of bounds. */
|
||||
export function parseReportMap(bytes: Uint8Array): Map<number, HidReport> {
|
||||
if (!bytes.length || bytes.length > REPORT_MAP_MAX) fail("report map must contain 1..512 bytes");
|
||||
const reports = new Map<number, HidReport>();
|
||||
let g: Global = { page: 0, min: 0, max: 0, size: 0, count: 0, id: 0 };
|
||||
const stack: Global[] = [];
|
||||
let usages: HidUsage[] = [];
|
||||
let lo: HidUsage | undefined;
|
||||
let hi: HidUsage | undefined;
|
||||
let depth = 0;
|
||||
const usage = (v: number, n: number): HidUsage =>
|
||||
n === 4 ? { page: v >>> 16, usage: v & 65535 } : { page: g.page, usage: v };
|
||||
for (let p = 0; p < bytes.length;) {
|
||||
const prefix = bytes[p++]!;
|
||||
if (prefix === 0xfe) fail("long items");
|
||||
const n = [0, 1, 2, 4][prefix & 3]!;
|
||||
if (p + n > bytes.length) fail("truncated item");
|
||||
let v = 0;
|
||||
for (let j = 0; j < n; j++) v += bytes[p++]! * 2 ** (8 * j);
|
||||
const signed = n && v >= 2 ** (n * 8 - 1) ? v - 2 ** (n * 8) : v;
|
||||
const type = (prefix >> 2) & 3;
|
||||
const tag = prefix >> 4;
|
||||
if (type === 1) {
|
||||
switch (tag) {
|
||||
case 0: g.page = v; break;
|
||||
case 1: g.min = signed; break;
|
||||
case 2: g.max = g.min < 0 ? signed : v; break;
|
||||
case 7: g.size = v; break;
|
||||
case 8: if (!v || v > 255) fail("Report ID"); g.id = v; break;
|
||||
case 9: g.count = v; break;
|
||||
case 10: if (stack.length >= 8) fail("global stack overflow"); stack.push({ ...g }); break;
|
||||
case 11: if (!stack.length) fail("global stack underflow"); g = stack.pop()!; break;
|
||||
case 3: case 4: case 5: case 6: break; // physical range, unit and exponent
|
||||
default: fail("unknown global item");
|
||||
}
|
||||
} else if (type === 2) {
|
||||
if (tag === 0) { if (usages.length >= 512) fail("too many usages"); usages.push(usage(v, n)); }
|
||||
else if (tag === 1) lo = usage(v, n);
|
||||
else if (tag === 2) hi = usage(v, n);
|
||||
else if (tag === 10) fail("usage delimiters");
|
||||
else if (tag > 9) fail("unknown local item");
|
||||
} else if (type === 0) {
|
||||
if (tag === 10) { if (++depth > 32) fail("collection depth"); }
|
||||
else if (tag === 12) { if (--depth < 0) fail("collection underflow"); }
|
||||
else if (tag === 8) {
|
||||
if (!g.size || g.size > 32 || !g.count || g.count > 512) fail("field dimensions");
|
||||
let r = reports.get(g.id);
|
||||
if (!r) {
|
||||
if (reports.size >= 16) fail("too many input reports");
|
||||
r = { id: g.id, bits: 0, fields: [] };
|
||||
reports.set(g.id, r);
|
||||
}
|
||||
const field: Field = {
|
||||
bit: r.bits, size: g.size, count: g.count, variable: !!(v & 2), min: g.min, max: g.max, usages: [...usages],
|
||||
};
|
||||
if (lo || hi) {
|
||||
if (!lo || !hi || lo.page !== hi.page || hi.usage < lo.usage) fail("usage range");
|
||||
field.range = [lo!, hi!];
|
||||
}
|
||||
r.bits += g.size * g.count;
|
||||
if (r.bits > 4096) fail("report exceeds 512 bytes");
|
||||
// Constant padding and relative pointer axes are not keys.
|
||||
if (!(v & 1) && !(v & 4)) r.fields.push(field);
|
||||
} else if (tag !== 9 && tag !== 11) fail("unknown main item");
|
||||
usages = [];
|
||||
lo = hi = undefined;
|
||||
} else fail("reserved item type");
|
||||
}
|
||||
if (depth || stack.length) fail("unbalanced collection/global stack");
|
||||
if (reports.has(0) && reports.size > 1) fail("mixed numbered and unnumbered input reports");
|
||||
if (!reports.size) fail("no input reports");
|
||||
return reports;
|
||||
}
|
||||
|
||||
function usageAt(f: Field, index: number): HidUsage | undefined {
|
||||
if (index < 0) return undefined;
|
||||
if (index < f.usages.length) return f.usages[index];
|
||||
const rangeIndex = index - f.usages.length;
|
||||
if (f.range && rangeIndex <= f.range[1].usage - f.range[0].usage) {
|
||||
return { page: f.range[0].page, usage: f.range[0].usage + rangeIndex };
|
||||
}
|
||||
// Variable items reuse their last usage when Report Count exceeds the usages.
|
||||
if (f.variable) return f.range?.[1] ?? f.usages[f.usages.length - 1];
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an input report can ever say a key is held. False for a pointer's
|
||||
* report (a trackpad or mouse on a combined device), whose movement would
|
||||
* otherwise be relayed, and thrown away, a hundred times a second.
|
||||
*/
|
||||
export function reportHasKeys(report: HidReport): boolean {
|
||||
const isKeyPage = (u: HidUsage): boolean => u.page === HID_PAGE_KEYBOARD || u.page === HID_PAGE_CONSUMER;
|
||||
return report.fields.some((f) => f.usages.some(isKeyPage) || (f.range !== undefined && isKeyPage(f.range[0])));
|
||||
}
|
||||
|
||||
/** The keys one input report says are held. Throws on a length mismatch or a rollover report. */
|
||||
export function reportKeys(report: HidReport, data: Uint8Array): HidUsage[] {
|
||||
if (data.length !== Math.ceil(report.bits / 8)) throw new Error("HID input length does not match Report Map");
|
||||
const keys = new Map<string, HidUsage>();
|
||||
for (const f of report.fields) {
|
||||
for (let i = 0; i < f.count; i++) {
|
||||
let value = 0;
|
||||
for (let j = 0; j < f.size; j++) {
|
||||
const bit = f.bit + i * f.size + j;
|
||||
if (data[bit >>> 3]! & (1 << (bit & 7))) value += 2 ** j;
|
||||
}
|
||||
if (f.min < 0 && value >= 2 ** (f.size - 1)) value -= 2 ** f.size;
|
||||
if (value < f.min || value > f.max) continue; // null state
|
||||
const u = usageAt(f, f.variable ? i : value - f.min);
|
||||
if (!u || (f.variable && value === 0) || !u.usage) continue;
|
||||
if (u.page === HID_PAGE_KEYBOARD && u.usage <= 3) throw new Error("HID keyboard rollover/error report");
|
||||
if (u.page === HID_PAGE_KEYBOARD || u.page === HID_PAGE_CONSUMER) keys.set(usageKey(u), u);
|
||||
}
|
||||
}
|
||||
return [...keys.values()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Held keys across a keyboard's input reports. Ownership is per report
|
||||
* characteristic, then unioned: releasing a key in one report cannot release
|
||||
* a modifier another report still holds.
|
||||
*/
|
||||
export class HidKeys {
|
||||
private reports = new Map<number, HidUsage[]>();
|
||||
private held = new Map<string, HidUsage>();
|
||||
|
||||
/** Apply one report from characteristic `handle`; returns the transitions it caused. */
|
||||
update(handle: number, report: HidReport, data: Uint8Array): HidKeyEvent[] {
|
||||
this.reports.set(handle, reportKeys(report, data));
|
||||
return this.diff();
|
||||
}
|
||||
|
||||
/** Release everything (a stream gap, a dropped link). */
|
||||
reset(): HidKeyEvent[] {
|
||||
this.reports.clear();
|
||||
return this.diff();
|
||||
}
|
||||
|
||||
private diff(): HidKeyEvent[] {
|
||||
const next = new Map<string, HidUsage>();
|
||||
for (const values of this.reports.values()) for (const u of values) next.set(usageKey(u), u);
|
||||
const events: HidKeyEvent[] = [];
|
||||
for (const [k, u] of this.held) if (!next.has(k)) events.push({ ...u, down: false });
|
||||
for (const [k, u] of next) if (!this.held.has(k)) events.push({ ...u, down: true });
|
||||
this.held = next;
|
||||
return events;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Where a key from the hardware keyboard goes: to a keybinding being set up,
|
||||
* to a gesture it is bound to, to the window with input focus, or to the
|
||||
* built-in navigation. The meanings are in bindings.ts; this is the order of
|
||||
* asking, and the bookkeeping around it. Shared by the Android and iOS
|
||||
* controllers through KeyRouterHost.
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
import {
|
||||
GestureScheme,
|
||||
chordOf,
|
||||
isModifierCode,
|
||||
resolveKey,
|
||||
type GestureOrigin,
|
||||
type KeyAction,
|
||||
type KeyChord,
|
||||
type KeyGesture,
|
||||
} from "./bindings";
|
||||
import type { KeyEvent } from "./keys";
|
||||
|
||||
export type KeyRouterHost = {
|
||||
/** Connected to glasses that are in use. */
|
||||
ready(): boolean;
|
||||
locked(): boolean;
|
||||
screenOn(): boolean;
|
||||
/** Turn a dark display on, and nothing else. */
|
||||
wake(): void;
|
||||
/** Offer a key to the window with input focus; true when that window takes keys. */
|
||||
offerToWindow(event: KeyEvent): boolean;
|
||||
/** Perform a gesture as if it came from the ring or the watch. */
|
||||
gesture(gesture: KeyGesture, origin: GestureOrigin): Promise<void>;
|
||||
/** The custom keybindings, by chord id. */
|
||||
bindings(): ReadonlyMap<string, KeyAction>;
|
||||
/** A key was used for something other than a gesture or typing (count it against the screen timeout). */
|
||||
noteActivity(): void;
|
||||
log(line: string): void;
|
||||
now?(): number;
|
||||
};
|
||||
|
||||
/**
|
||||
* A held key repeats twenty times a second, faster than a list can repaint
|
||||
* over the glasses' link. Repeats are dropped while a gesture is still being
|
||||
* handled, and closer together than this.
|
||||
*/
|
||||
const REPEAT_MIN_INTERVAL_MS = 100;
|
||||
|
||||
export class KeyRouter {
|
||||
private capture: ((chord: KeyChord) => void) | null = null;
|
||||
/** Keys whose press was spoken for, so their release is too. */
|
||||
private readonly consumed = new Set<string>();
|
||||
private readonly scheme = new GestureScheme();
|
||||
private gesturesInFlight = 0;
|
||||
private lastGestureAtMs = 0;
|
||||
|
||||
constructor(private readonly host: KeyRouterHost) {}
|
||||
|
||||
/** One key transition from the keyboard. */
|
||||
handleKey(event: KeyEvent): void {
|
||||
const host = this.host;
|
||||
if (!host.ready()) return;
|
||||
if (host.locked() || !host.screenOn()) {
|
||||
// A dark display takes no typing: the first press only wakes it, as
|
||||
// the ring's double tap would, and a locked one stays locked.
|
||||
if (event.phase === "down" && !event.repeat && !host.screenOn()) host.wake();
|
||||
return;
|
||||
}
|
||||
if (this.capture) {
|
||||
this.captureKey(event);
|
||||
return;
|
||||
}
|
||||
if (event.phase === "up") {
|
||||
if (!this.consumed.delete(event.code)) host.offerToWindow(event);
|
||||
return;
|
||||
}
|
||||
const resolution = resolveKey(event, host.bindings());
|
||||
if (resolution.consume) {
|
||||
this.consumed.add(event.code);
|
||||
if (resolution.gesture) this.perform(resolution.gesture, event.repeat);
|
||||
return;
|
||||
}
|
||||
if (host.offerToWindow(event)) return;
|
||||
if (resolution.gesture) this.perform(resolution.gesture, event.repeat);
|
||||
}
|
||||
|
||||
/**
|
||||
* A key the focused window was offered and had no use for (a terminal
|
||||
* showing its session list rather than a session): it gets the built-in
|
||||
* navigation meaning after all.
|
||||
*/
|
||||
handleUnhandledKey(event: KeyEvent): void {
|
||||
const host = this.host;
|
||||
if (event.phase !== "down" || this.capture || !host.ready() || host.locked() || !host.screenOn()) return;
|
||||
const resolution = resolveKey(event, host.bindings());
|
||||
if (!resolution.consume && resolution.gesture) this.perform(resolution.gesture, event.repeat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the next key press instead of acting on it (setting up a
|
||||
* keybinding). Modifiers held with it are part of the chord; pressed alone
|
||||
* they are waited through. Returns a function that calls the capture off.
|
||||
*/
|
||||
captureNextKey(onChord: (chord: KeyChord) => void): () => void {
|
||||
this.capture = onChord;
|
||||
return () => {
|
||||
if (this.capture === onChord) this.capture = null;
|
||||
};
|
||||
}
|
||||
|
||||
/** The keyboard went away: nothing is held any more. */
|
||||
reset(): void {
|
||||
this.consumed.clear();
|
||||
}
|
||||
|
||||
private captureKey(event: KeyEvent): void {
|
||||
this.host.noteActivity();
|
||||
if (event.phase === "up") {
|
||||
this.consumed.delete(event.code);
|
||||
return;
|
||||
}
|
||||
if (event.repeat || isModifierCode(event.code)) return;
|
||||
const capture = this.capture!;
|
||||
this.capture = null;
|
||||
this.consumed.add(event.code);
|
||||
capture(chordOf(event));
|
||||
}
|
||||
|
||||
private perform(gesture: KeyGesture, repeat: boolean): void {
|
||||
const now = this.host.now ? this.host.now() : Date.now();
|
||||
if (repeat && (this.gesturesInFlight > 0 || now - this.lastGestureAtMs < REPEAT_MIN_INTERVAL_MS)) return;
|
||||
this.lastGestureAtMs = now;
|
||||
void this.run(gesture, this.scheme.originFor(gesture));
|
||||
}
|
||||
|
||||
private async run(gesture: KeyGesture, origin: GestureOrigin): Promise<void> {
|
||||
this.gesturesInFlight++;
|
||||
try {
|
||||
await this.host.gesture(gesture, origin);
|
||||
} catch (error) {
|
||||
this.host.log(`keyboard gesture ${gesture} failed: ${error instanceof Error ? error.message : String(error)}`);
|
||||
} finally {
|
||||
this.gesturesInFlight--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
/**
|
||||
* Key events as the rest of Faceclaw sees them, and the translation from the
|
||||
* keyboard bridge's raw HID transitions: layout (US only for now), modifier
|
||||
* and Caps Lock state, and typematic repeat, which a BLE keyboard never sends
|
||||
* (its report just stays asserted). The translation tables follow the Glassly
|
||||
* app's (GlassesKeyboardInput.ts, MIT).
|
||||
* No platform imports: this runs under node in the tests, and in app workers.
|
||||
*/
|
||||
import type { HidKeyEvent } from "./hid";
|
||||
|
||||
export type KeyModifiers = { shift: boolean; ctrl: boolean; alt: boolean; meta: boolean };
|
||||
|
||||
/**
|
||||
* One key transition, shaped like a DOM KeyboardEvent: `key` is the logical
|
||||
* key with the layout applied ("a", "A", "Enter", "ArrowUp"), `code` the
|
||||
* physical key ("KeyA"), `text` what a press types (absent for releases, for
|
||||
* keys that type nothing, and for Ctrl/Alt/Meta chords, which are commands).
|
||||
*/
|
||||
export type KeyEvent = {
|
||||
phase: "down" | "up";
|
||||
key: string;
|
||||
code: string;
|
||||
text?: string;
|
||||
modifiers: KeyModifiers;
|
||||
/** A synthesized auto-repeat of a held key. */
|
||||
repeat: boolean;
|
||||
/** Date.now() when the phone saw the transition. */
|
||||
timestampMs: number;
|
||||
};
|
||||
|
||||
const HID_PAGE_KEYBOARD = 7;
|
||||
const HID_PAGE_CONSUMER = 12;
|
||||
const USAGE_CAPS_LOCK = 57;
|
||||
|
||||
/** [unshifted, shifted, code] for US-layout punctuation on the keyboard page. */
|
||||
const PUNCTUATION: Record<number, [string, string, string]> = {
|
||||
45: ["-", "_", "Minus"],
|
||||
46: ["=", "+", "Equal"],
|
||||
47: ["[", "{", "BracketLeft"],
|
||||
48: ["]", "}", "BracketRight"],
|
||||
49: ["\\", "|", "Backslash"],
|
||||
50: ["#", "~", "IntlHash"],
|
||||
51: [";", ":", "Semicolon"],
|
||||
52: ["'", '"', "Quote"],
|
||||
53: ["`", "~", "Backquote"],
|
||||
54: [",", "<", "Comma"],
|
||||
55: [".", ">", "Period"],
|
||||
56: ["/", "?", "Slash"],
|
||||
100: ["\\", "|", "IntlBackslash"],
|
||||
};
|
||||
|
||||
const DIGITS = "1234567890";
|
||||
const SHIFTED_DIGITS = "!@#$%^&*()";
|
||||
|
||||
/** [key, code] for keys that type nothing. */
|
||||
const NAMED: Record<number, [string, string]> = {
|
||||
40: ["Enter", "Enter"],
|
||||
41: ["Escape", "Escape"],
|
||||
42: ["Backspace", "Backspace"],
|
||||
43: ["Tab", "Tab"],
|
||||
57: ["CapsLock", "CapsLock"],
|
||||
70: ["PrintScreen", "PrintScreen"],
|
||||
71: ["ScrollLock", "ScrollLock"],
|
||||
72: ["Pause", "Pause"],
|
||||
73: ["Insert", "Insert"],
|
||||
74: ["Home", "Home"],
|
||||
75: ["PageUp", "PageUp"],
|
||||
76: ["Delete", "Delete"],
|
||||
77: ["End", "End"],
|
||||
78: ["PageDown", "PageDown"],
|
||||
79: ["ArrowRight", "ArrowRight"],
|
||||
80: ["ArrowLeft", "ArrowLeft"],
|
||||
81: ["ArrowDown", "ArrowDown"],
|
||||
82: ["ArrowUp", "ArrowUp"],
|
||||
83: ["NumLock", "NumLock"],
|
||||
88: ["Enter", "NumpadEnter"],
|
||||
101: ["ContextMenu", "ContextMenu"],
|
||||
102: ["Power", "Power"],
|
||||
224: ["Control", "ControlLeft"],
|
||||
225: ["Shift", "ShiftLeft"],
|
||||
226: ["Alt", "AltLeft"],
|
||||
227: ["Meta", "MetaLeft"],
|
||||
228: ["Control", "ControlRight"],
|
||||
229: ["Shift", "ShiftRight"],
|
||||
230: ["Alt", "AltRight"],
|
||||
231: ["Meta", "MetaRight"],
|
||||
};
|
||||
|
||||
/** [key and text, code] for the numeric keypad's operators. */
|
||||
const NUMPAD: Record<number, [string, string]> = {
|
||||
84: ["/", "NumpadDivide"],
|
||||
85: ["*", "NumpadMultiply"],
|
||||
86: ["-", "NumpadSubtract"],
|
||||
87: ["+", "NumpadAdd"],
|
||||
99: [".", "NumpadDecimal"],
|
||||
103: ["=", "NumpadEqual"],
|
||||
};
|
||||
|
||||
const CONSUMER: Record<number, string> = {
|
||||
0xe9: "AudioVolumeUp",
|
||||
0xea: "AudioVolumeDown",
|
||||
0xe2: "AudioVolumeMute",
|
||||
0xcd: "MediaPlayPause",
|
||||
0xb0: "MediaPlay",
|
||||
0xb1: "MediaPause",
|
||||
0xb5: "MediaTrackNext",
|
||||
0xb6: "MediaTrackPrevious",
|
||||
0xb7: "MediaStop",
|
||||
0x6f: "BrightnessUp",
|
||||
0x70: "BrightnessDown",
|
||||
0x221: "BrowserSearch",
|
||||
0x223: "BrowserHome",
|
||||
0x224: "BrowserBack",
|
||||
0x225: "BrowserForward",
|
||||
0x18a: "LaunchMail",
|
||||
0x192: "LaunchApplication2",
|
||||
0x194: "LaunchApplication1",
|
||||
};
|
||||
|
||||
const MODIFIER_USAGES: Record<number, keyof KeyModifiers> = {
|
||||
224: "ctrl", 228: "ctrl", 225: "shift", 229: "shift", 226: "alt", 230: "alt", 227: "meta", 231: "meta",
|
||||
};
|
||||
|
||||
type Translated = { key: string; code: string; text?: string };
|
||||
|
||||
/** One HID usage under the US layout. Caps Lock only affects letters. */
|
||||
export function translateUsage(page: number, usage: number, shift: boolean, capsLock: boolean): Translated {
|
||||
if (page === HID_PAGE_KEYBOARD) {
|
||||
if (usage >= 4 && usage <= 29) {
|
||||
const lower = String.fromCharCode(97 + usage - 4);
|
||||
const key = shift !== capsLock ? lower.toUpperCase() : lower;
|
||||
return { key, code: `Key${lower.toUpperCase()}`, text: key };
|
||||
}
|
||||
if (usage >= 30 && usage <= 39) {
|
||||
const index = usage - 30;
|
||||
const key = shift ? SHIFTED_DIGITS[index]! : DIGITS[index]!;
|
||||
return { key, code: `Digit${DIGITS[index]}`, text: key };
|
||||
}
|
||||
if (usage === 44) return { key: " ", code: "Space", text: " " };
|
||||
const punctuation = PUNCTUATION[usage];
|
||||
if (punctuation) {
|
||||
const key = shift ? punctuation[1] : punctuation[0];
|
||||
return { key, code: punctuation[2], text: key };
|
||||
}
|
||||
if (usage >= 58 && usage <= 69) return { key: `F${usage - 57}`, code: `F${usage - 57}` };
|
||||
if (usage >= 104 && usage <= 115) return { key: `F${usage - 91}`, code: `F${usage - 91}` };
|
||||
if (usage >= 89 && usage <= 97) {
|
||||
const digit = String(usage - 88);
|
||||
return { key: digit, code: `Numpad${digit}`, text: digit };
|
||||
}
|
||||
if (usage === 98) return { key: "0", code: "Numpad0", text: "0" };
|
||||
const numpad = NUMPAD[usage];
|
||||
if (numpad) return { key: numpad[0], code: numpad[1], text: numpad[0] };
|
||||
const named = NAMED[usage];
|
||||
if (named) return { key: named[0], code: named[1] };
|
||||
return { key: "Unidentified", code: `Usage${usage.toString(16).padStart(2, "0")}` };
|
||||
}
|
||||
if (page === HID_PAGE_CONSUMER) {
|
||||
const name = CONSUMER[usage];
|
||||
return name ? { key: name, code: name } : { key: "Unidentified", code: `Consumer${usage.toString(16)}` };
|
||||
}
|
||||
return { key: "Unidentified", code: `Page${page.toString(16)}Usage${usage.toString(16)}` };
|
||||
}
|
||||
|
||||
export type KeyTranslatorOptions = {
|
||||
emit: (event: KeyEvent) => void;
|
||||
now?: () => number;
|
||||
setTimer?: (callback: () => void, delayMs: number) => unknown;
|
||||
clearTimer?: (handle: unknown) => void;
|
||||
/** Hold time before the first repeat, and the gap between repeats. */
|
||||
repeatDelayMs?: number;
|
||||
repeatIntervalMs?: number;
|
||||
};
|
||||
|
||||
const REPEAT_DELAY_MS = 400;
|
||||
const REPEAT_INTERVAL_MS = 50;
|
||||
|
||||
/**
|
||||
* Raw HID transitions in, KeyEvents out. Modifier state comes from the same
|
||||
* stream, and the bridge reports a release for every key when the stream may
|
||||
* have lost something, so a missed key-up cannot leave a modifier stuck or a
|
||||
* key repeating forever.
|
||||
*/
|
||||
export class KeyTranslator {
|
||||
private readonly emitEvent: (event: KeyEvent) => void;
|
||||
private readonly now: () => number;
|
||||
private readonly setTimer: (callback: () => void, delayMs: number) => unknown;
|
||||
private readonly clearTimer: (handle: unknown) => void;
|
||||
private readonly repeatDelayMs: number;
|
||||
private readonly repeatIntervalMs: number;
|
||||
private readonly held = new Set<string>();
|
||||
private modifiers: KeyModifiers = { shift: false, ctrl: false, alt: false, meta: false };
|
||||
private capsLock = false;
|
||||
private repeatTimer: unknown = null;
|
||||
private repeating: { page: number; usage: number } | null = null;
|
||||
|
||||
constructor(options: KeyTranslatorOptions) {
|
||||
this.emitEvent = options.emit;
|
||||
this.now = options.now ?? (() => Date.now());
|
||||
this.setTimer = options.setTimer ?? ((callback, delayMs) => setTimeout(callback, delayMs));
|
||||
this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle as ReturnType<typeof setTimeout>));
|
||||
this.repeatDelayMs = options.repeatDelayMs ?? REPEAT_DELAY_MS;
|
||||
this.repeatIntervalMs = options.repeatIntervalMs ?? REPEAT_INTERVAL_MS;
|
||||
}
|
||||
|
||||
/** Apply the transitions one input report produced, in order. */
|
||||
handle(events: readonly HidKeyEvent[]): void {
|
||||
for (const event of events) this.handleOne(event);
|
||||
}
|
||||
|
||||
private handleOne(event: HidKeyEvent): void {
|
||||
const id = `${event.page}:${event.usage}`;
|
||||
const modifier = event.page === HID_PAGE_KEYBOARD ? MODIFIER_USAGES[event.usage] : undefined;
|
||||
if (event.down) {
|
||||
if (this.held.has(id)) return;
|
||||
this.held.add(id);
|
||||
if (modifier) this.modifiers[modifier] = true;
|
||||
if (event.page === HID_PAGE_KEYBOARD && event.usage === USAGE_CAPS_LOCK) this.capsLock = !this.capsLock;
|
||||
this.emit("down", event.page, event.usage, false);
|
||||
// The newest non-modifier key is the one that repeats, as on a PC.
|
||||
if (!modifier) this.startRepeat(event.page, event.usage);
|
||||
} else {
|
||||
if (!this.held.delete(id)) return;
|
||||
if (modifier) this.modifiers[modifier] = this.otherHeld(event.usage, modifier);
|
||||
if (this.repeating?.page === event.page && this.repeating.usage === event.usage) this.stopRepeat();
|
||||
this.emit("up", event.page, event.usage, false);
|
||||
}
|
||||
}
|
||||
|
||||
/** The keyboard went away: forget everything without emitting releases. */
|
||||
reset(): void {
|
||||
this.stopRepeat();
|
||||
this.held.clear();
|
||||
this.modifiers = { shift: false, ctrl: false, alt: false, meta: false };
|
||||
this.capsLock = false;
|
||||
}
|
||||
|
||||
/** Whether the same modifier's key on the other side is still down. */
|
||||
private otherHeld(released: number, modifier: keyof KeyModifiers): boolean {
|
||||
for (const [usage, name] of Object.entries(MODIFIER_USAGES)) {
|
||||
if (name === modifier && Number(usage) !== released && this.held.has(`${HID_PAGE_KEYBOARD}:${usage}`)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private emit(phase: "down" | "up", page: number, usage: number, repeat: boolean): void {
|
||||
const translated = translateUsage(page, usage, this.modifiers.shift, this.capsLock);
|
||||
const event: KeyEvent = {
|
||||
phase,
|
||||
key: translated.key,
|
||||
code: translated.code,
|
||||
modifiers: { ...this.modifiers },
|
||||
repeat,
|
||||
timestampMs: this.now(),
|
||||
};
|
||||
if (phase === "down" && translated.text !== undefined &&
|
||||
!this.modifiers.ctrl && !this.modifiers.alt && !this.modifiers.meta) {
|
||||
event.text = translated.text;
|
||||
}
|
||||
this.emitEvent(event);
|
||||
}
|
||||
|
||||
private startRepeat(page: number, usage: number): void {
|
||||
this.stopRepeat();
|
||||
this.repeating = { page, usage };
|
||||
this.repeatTimer = this.setTimer(() => this.repeatTick(), this.repeatDelayMs);
|
||||
}
|
||||
|
||||
private repeatTick(): void {
|
||||
const key = this.repeating;
|
||||
this.repeatTimer = null;
|
||||
if (!key || !this.held.has(`${key.page}:${key.usage}`)) {
|
||||
this.repeating = null;
|
||||
return;
|
||||
}
|
||||
this.emit("down", key.page, key.usage, true);
|
||||
this.repeatTimer = this.setTimer(() => this.repeatTick(), this.repeatIntervalMs);
|
||||
}
|
||||
|
||||
private stopRepeat(): void {
|
||||
if (this.repeatTimer !== null) this.clearTimer(this.repeatTimer);
|
||||
this.repeatTimer = null;
|
||||
this.repeating = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
/**
|
||||
* Wire format of the CFW keyboard bridge (Faceclaw/39). The master temple acts
|
||||
* as a BLE central for one HID-over-GATT keyboard; the phone drives that link
|
||||
* over private settings fields and gets the keyboard's raw input reports back.
|
||||
* The firmware side is g2flash patches/keyboard_core.h, documented in its
|
||||
* docs/keyboard.md. This follows the reference client in g2flash demos/
|
||||
* (keyboard-protocol.ts), which came with the firmware from tetramo-labs.
|
||||
*
|
||||
* Phone -> glasses, settings field 130 (wrapped by the native session):
|
||||
* ['K','B',1,op, request LE16, epoch LE16, body...] at most 80 bytes
|
||||
* Glasses -> phone, settings field 131:
|
||||
* ['K','B',1,kind, sequence LE32, epoch LE16, request LE16, code LE16,
|
||||
* handle LE16, total LE16, offset LE16, timestamp LE32, fragment...]
|
||||
*
|
||||
* All integers are little endian. No platform imports: this runs under node
|
||||
* in the tests.
|
||||
*/
|
||||
|
||||
export const KEYBOARD_PROTOCOL_VERSION = 1;
|
||||
|
||||
/** Fixed part of a field-131 record, before its fragment. */
|
||||
export const KEYBOARD_RECORD_HEADER = 24;
|
||||
/** Most body bytes one record carries; longer bodies are fragmented. */
|
||||
export const KEYBOARD_RECORD_CHUNK = 96;
|
||||
/** Largest reassembled body (an ATT value or a HID report). */
|
||||
export const KEYBOARD_BODY_MAX = 512;
|
||||
/** Largest control body after the 8-byte prefix. */
|
||||
export const KEYBOARD_CONTROL_BODY_MAX = 72;
|
||||
|
||||
export const KeyboardOp = {
|
||||
query: 0,
|
||||
enable: 1,
|
||||
disable: 2,
|
||||
scan: 3,
|
||||
connect: 4,
|
||||
disconnect: 5,
|
||||
pair: 6,
|
||||
auth: 7,
|
||||
compare: 8,
|
||||
find: 9,
|
||||
read: 10,
|
||||
write: 11,
|
||||
watch: 12,
|
||||
} as const;
|
||||
export type KeyboardOpCode = (typeof KeyboardOp)[keyof typeof KeyboardOp];
|
||||
|
||||
export const KeyboardKind = {
|
||||
status: 1,
|
||||
att: 2,
|
||||
input: 3,
|
||||
dm: 4,
|
||||
advert: 5,
|
||||
} as const;
|
||||
|
||||
/** Control error codes, carried in a STATUS record's `code`. */
|
||||
export const KeyboardError = {
|
||||
ok: 0,
|
||||
badRequest: 1,
|
||||
disabled: 2,
|
||||
busy: 3,
|
||||
staleEpoch: 4,
|
||||
noSpace: 5,
|
||||
notConnected: 6,
|
||||
notEncrypted: 7,
|
||||
timeout: 8,
|
||||
noMemory: 9,
|
||||
} as const;
|
||||
|
||||
export function keyboardErrorName(code: number): string {
|
||||
switch (code) {
|
||||
case KeyboardError.ok: return "ok";
|
||||
case KeyboardError.badRequest: return "bad request";
|
||||
case KeyboardError.disabled: return "keyboard bridge disabled";
|
||||
case KeyboardError.busy: return "busy";
|
||||
case KeyboardError.staleEpoch: return "stale epoch";
|
||||
case KeyboardError.noSpace: return "no connection or bond space";
|
||||
case KeyboardError.notConnected: return "not connected";
|
||||
case KeyboardError.notEncrypted: return "not encrypted";
|
||||
case KeyboardError.timeout: return "timeout";
|
||||
case KeyboardError.noMemory: return "allocation failure";
|
||||
default: return `error ${code}`;
|
||||
}
|
||||
}
|
||||
|
||||
/** Cordio device-manager events the bridge relays (a DM record's `code & 0xff`). */
|
||||
export const KeyboardDm = {
|
||||
connOpen: 0x27,
|
||||
connClose: 0x28,
|
||||
pairComplete: 0x2a,
|
||||
pairFailed: 0x2b,
|
||||
encrypted: 0x2c,
|
||||
encryptFailed: 0x2d,
|
||||
authRequest: 0x2e,
|
||||
compare: 0x35,
|
||||
} as const;
|
||||
|
||||
/** ATT client events (an ATT record's `code & 0xff`; the status is `code >> 8`). */
|
||||
export const KeyboardAtt = {
|
||||
notification: 13,
|
||||
indication: 14,
|
||||
} as const;
|
||||
|
||||
export type KeyboardRecord = {
|
||||
kind: number;
|
||||
sequence: number;
|
||||
epoch: number;
|
||||
request: number;
|
||||
code: number;
|
||||
handle: number;
|
||||
/** Glasses uptime in ms when the record was queued. */
|
||||
timestamp: number;
|
||||
data: Uint8Array;
|
||||
};
|
||||
|
||||
export const u16 = (p: Uint8Array, off = 0): number => p[off]! | (p[off + 1]! << 8);
|
||||
export const u32 = (p: Uint8Array, off = 0): number =>
|
||||
(p[off]! | (p[off + 1]! << 8) | (p[off + 2]! << 16) | (p[off + 3]! << 24)) >>> 0;
|
||||
export const le16 = (v: number): number[] => [v & 255, (v >>> 8) & 255];
|
||||
|
||||
/** A field-130 control record. The session wraps it in the settings package. */
|
||||
export function keyboardControl(op: number, request: number, epoch: number, body: readonly number[] = []): Uint8Array {
|
||||
if (!Number.isInteger(op) || op < 0 || op > KeyboardOp.watch || body.length > KEYBOARD_CONTROL_BODY_MAX) {
|
||||
throw new Error("Keyboard control bounds");
|
||||
}
|
||||
return Uint8Array.from([75, 66, KEYBOARD_PROTOCOL_VERSION, op, ...le16(request), ...le16(epoch), ...body]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bounded, ordered reassembly of field-131 records. `onGap` fires whenever
|
||||
* the stream may have lost something (a skipped sequence, an epoch change, a
|
||||
* malformed or orphaned fragment): the caller then releases every held key
|
||||
* and fails any setup transaction in flight. The firmware's once-a-second
|
||||
* status record makes a lost final release visible even when no further keys
|
||||
* arrive.
|
||||
*/
|
||||
export class KeyboardRecords {
|
||||
private epoch: number | undefined;
|
||||
private last: number | undefined;
|
||||
private fragment: { record: KeyboardRecord; total: number; used: number } | undefined;
|
||||
|
||||
constructor(private readonly onGap: () => void) {}
|
||||
|
||||
/** Forget the stream position (the phone link dropped); counts as a gap. */
|
||||
reset(): void {
|
||||
this.epoch = undefined;
|
||||
this.last = undefined;
|
||||
this.fragment = undefined;
|
||||
this.onGap();
|
||||
}
|
||||
|
||||
/** Feed one field-131 payload; returns a complete record, or undefined. */
|
||||
push(p: Uint8Array): KeyboardRecord | undefined {
|
||||
const invalid = (): undefined => {
|
||||
this.fragment = undefined;
|
||||
this.onGap();
|
||||
return undefined;
|
||||
};
|
||||
if (
|
||||
p.length < KEYBOARD_RECORD_HEADER || p.length > KEYBOARD_RECORD_HEADER + KEYBOARD_RECORD_CHUNK ||
|
||||
p[0] !== 75 || p[1] !== 66 || p[2] !== KEYBOARD_PROTOCOL_VERSION
|
||||
) {
|
||||
return invalid();
|
||||
}
|
||||
const r: KeyboardRecord = {
|
||||
kind: p[3]!,
|
||||
sequence: u32(p, 4),
|
||||
epoch: u16(p, 8),
|
||||
request: u16(p, 10),
|
||||
code: u16(p, 12),
|
||||
handle: u16(p, 14),
|
||||
timestamp: u32(p, 20),
|
||||
data: new Uint8Array(0),
|
||||
};
|
||||
const total = u16(p, 16);
|
||||
const offset = u16(p, 18);
|
||||
const n = p.length - KEYBOARD_RECORD_HEADER;
|
||||
if (r.kind < KeyboardKind.status || r.kind > KeyboardKind.advert || total > KEYBOARD_BODY_MAX ||
|
||||
offset + n > total || (total > 0 && n === 0)) {
|
||||
return invalid();
|
||||
}
|
||||
if (this.epoch !== r.epoch) {
|
||||
this.onGap();
|
||||
this.fragment = undefined;
|
||||
this.last = undefined;
|
||||
this.epoch = r.epoch;
|
||||
}
|
||||
if (offset === 0) {
|
||||
if (this.last !== undefined) {
|
||||
const distance = (r.sequence - this.last) >>> 0;
|
||||
if (distance === 0 || distance >= 0x80000000) return undefined; // duplicate or old delivery
|
||||
}
|
||||
if (this.fragment || (this.last !== undefined && r.sequence !== ((this.last + 1) >>> 0))) this.onGap();
|
||||
this.last = r.sequence;
|
||||
this.fragment = { record: { ...r, data: new Uint8Array(total) }, total, used: 0 };
|
||||
}
|
||||
const f = this.fragment;
|
||||
if (!f || f.used !== offset || f.total !== total || f.record.sequence !== r.sequence ||
|
||||
f.record.kind !== r.kind || f.record.request !== r.request || f.record.code !== r.code ||
|
||||
f.record.handle !== r.handle || f.record.timestamp !== r.timestamp) {
|
||||
return invalid();
|
||||
}
|
||||
f.record.data.set(p.subarray(KEYBOARD_RECORD_HEADER), offset);
|
||||
f.used += n;
|
||||
if (f.used !== total) return undefined;
|
||||
this.fragment = undefined;
|
||||
return f.record;
|
||||
}
|
||||
}
|
||||
|
||||
/** BLE address types as the controller reports them. */
|
||||
export const KEYBOARD_ADDRESS_TYPES = ["public", "random", "public-identity", "random-identity"] as const;
|
||||
|
||||
/** One advertiser seen during a scan (an ADVERT record's body, parsed). */
|
||||
export type KeyboardAdvert = {
|
||||
addressType: number;
|
||||
/** "aa:bb:cc:dd:ee:ff", most significant byte first. */
|
||||
address: string;
|
||||
rssi: number;
|
||||
name: string;
|
||||
/** Advertises the HID service, or a HID appearance. */
|
||||
hid: boolean;
|
||||
};
|
||||
|
||||
/** ADVERT body: addrType, addr[6] (LSB first), RSSI, event type, AD length, AD structures. */
|
||||
export function parseKeyboardAdvert(body: Uint8Array): KeyboardAdvert | null {
|
||||
if (body.length < 10 || body[0]! > 3) return null;
|
||||
let name = "";
|
||||
let hid = false;
|
||||
for (let i = 10; i < body.length;) {
|
||||
const n = body[i++]!;
|
||||
if (n === 0 || i + n > body.length) break;
|
||||
const type = body[i]!;
|
||||
if (type === 8 || type === 9) name = decodeUtf8(body.subarray(i + 1, i + n));
|
||||
// 16-bit service lists (incomplete / complete): 0x1812 is HID.
|
||||
if (type === 2 || type === 3) {
|
||||
for (let j = i + 1; j + 1 < i + n; j += 2) if (body[j] === 0x12 && body[j + 1] === 0x18) hid = true;
|
||||
}
|
||||
// Appearance 0x03C0..0x03FF is the HID category (0x03C1 keyboard); some
|
||||
// keyboards advertise that without the service list.
|
||||
if (type === 0x19 && n >= 3 && body[i + 2] === 0x03 && body[i + 1]! >= 0xc0) hid = true;
|
||||
i += n;
|
||||
}
|
||||
return {
|
||||
addressType: body[0]!,
|
||||
address: formatKeyboardAddress(body.subarray(1, 7)),
|
||||
rssi: (body[7]! << 24) >> 24,
|
||||
name,
|
||||
hid,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fold a later record for the same advertiser into an earlier one. An active
|
||||
* scan reports the advertisement and its scan response separately and the
|
||||
* name usually only rides in the latter: keep a name once seen, HID once
|
||||
* seen, and the best signal.
|
||||
*/
|
||||
export function mergeKeyboardAdverts(a: KeyboardAdvert, b: KeyboardAdvert): KeyboardAdvert {
|
||||
return { ...a, rssi: Math.max(a.rssi, b.rssi), name: a.name || b.name, hid: a.hid || b.hid };
|
||||
}
|
||||
|
||||
/** Controller byte order (LSB first) to the usual "aa:bb:cc:dd:ee:ff". */
|
||||
export function formatKeyboardAddress(lsbFirst: Uint8Array): string {
|
||||
return Array.from(lsbFirst).reverse().map((b) => b.toString(16).padStart(2, "0")).join(":");
|
||||
}
|
||||
|
||||
/** "aa:bb:cc:dd:ee:ff" to the controller's LSB-first bytes, or null if malformed. */
|
||||
export function parseKeyboardAddress(text: string): Uint8Array | null {
|
||||
if (!/^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(text)) return null;
|
||||
return Uint8Array.from(text.split(":").reverse().map((part) => parseInt(part, 16)));
|
||||
}
|
||||
|
||||
/** Lenient UTF-8 decode (advertised names); no TextDecoder in every runtime this loads in. */
|
||||
function decodeUtf8(bytes: Uint8Array): string {
|
||||
let out = "";
|
||||
for (let i = 0; i < bytes.length;) {
|
||||
const b = bytes[i++]!;
|
||||
let code = b;
|
||||
let extra = 0;
|
||||
if (b >= 0xf0 && b < 0xf8) { code = b & 0x07; extra = 3; }
|
||||
else if (b >= 0xe0) { code = b & 0x0f; extra = 2; }
|
||||
else if (b >= 0xc0) { code = b & 0x1f; extra = 1; }
|
||||
else if (b >= 0x80) { out += "�"; continue; }
|
||||
if (i + extra > bytes.length) { out += "�"; break; }
|
||||
let valid = true;
|
||||
for (let k = 0; k < extra; k++) {
|
||||
const c = bytes[i + k]!;
|
||||
if ((c & 0xc0) !== 0x80) { valid = false; break; }
|
||||
code = (code << 6) | (c & 0x3f);
|
||||
}
|
||||
if (!valid || code > 0x10ffff) { out += "�"; continue; }
|
||||
i += extra;
|
||||
if (code === 0) continue;
|
||||
out += String.fromCodePoint(code);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/**
|
||||
* The keyboard bridge as the app uses it: one KeyboardBridge for the life of
|
||||
* the app, fed by whichever glasses session is live, its remembered keyboard
|
||||
* and the custom keybindings kept in the settings store, and its key
|
||||
* transitions turned into KeyEvents and routed. The platform's controller
|
||||
* reports the link coming and going and performs the gestures keys stand
|
||||
* for; the Settings app drives pairing through `bridge` and edits the
|
||||
* keybindings.
|
||||
*/
|
||||
import { addKeyboardEventListener, keyboardLinkSession, sendKeyboardControlRecord } from "../../native/keyboard-link";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import {
|
||||
MAX_KEY_BINDINGS,
|
||||
chordId,
|
||||
keyBindingMap,
|
||||
parseKeyBindings,
|
||||
serializeKeyBindings,
|
||||
type GestureOrigin,
|
||||
type KeyAction,
|
||||
type KeyBinding,
|
||||
type KeyChord,
|
||||
type KeyGesture,
|
||||
} from "./bindings";
|
||||
import { KeyboardBridge, type KeyboardBridgeSnapshot, type SavedKeyboard } from "./bridge";
|
||||
import { KeyRouter } from "./key-router";
|
||||
import { KeyTranslator, type KeyEvent } from "./keys";
|
||||
|
||||
declare const java: any;
|
||||
declare const arc4random_uniform: ((bound: number) => number) | undefined;
|
||||
|
||||
/** JSON of the remembered keyboard ({ address, addressType, name }), or "" for none. */
|
||||
const SAVED_KEYBOARD_KEY = "keyboard.saved";
|
||||
/** JSON list of the custom keybindings ({ key, action }); see bindings.ts. */
|
||||
const KEY_BINDINGS_KEY = "keyboard.bindings";
|
||||
|
||||
/** What the platform's controller does for the keyboard. */
|
||||
export type KeyboardServiceHost = {
|
||||
/** Connected to glasses that are in use (not charging in their case). */
|
||||
ready(): boolean;
|
||||
locked(): boolean;
|
||||
/** Turn a dark display on, and nothing else (a locked display stays locked). */
|
||||
wake(): void;
|
||||
/** Perform a gesture as if it came from the ring or the watch. */
|
||||
gesture(gesture: KeyGesture, origin: GestureOrigin): Promise<void>;
|
||||
/** Routine lines only (failed background reconnects); the rest go to the device log directly. */
|
||||
log(line: string): void;
|
||||
/** Whether an absent keyboard is worth looking for now (see KeyboardBridgeHost). */
|
||||
shouldReconnect(): boolean;
|
||||
};
|
||||
|
||||
function loadSavedKeyboard(): SavedKeyboard | null {
|
||||
try {
|
||||
const raw = getStringSetting(SAVED_KEYBOARD_KEY, "");
|
||||
if (!raw) return null;
|
||||
const value = JSON.parse(raw);
|
||||
if (typeof value?.address !== "string" || typeof value?.addressType !== "number") return null;
|
||||
return { address: value.address, addressType: value.addressType, name: typeof value.name === "string" ? value.name : "" };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Passkey entry only protects the pairing if the passkey can't be guessed. */
|
||||
function randomPasskey(): number {
|
||||
try {
|
||||
if (global.isAndroid) return new java.security.SecureRandom().nextInt(1_000_000);
|
||||
if (typeof arc4random_uniform === "function") return arc4random_uniform(1_000_000);
|
||||
} catch {
|
||||
// Fall through to the weaker source rather than failing the pairing.
|
||||
}
|
||||
return Math.floor(Math.random() * 1_000_000);
|
||||
}
|
||||
|
||||
class KeyboardService {
|
||||
private host: KeyboardServiceHost | null = null;
|
||||
private session: unknown = null;
|
||||
private offEvents: (() => void) | null = null;
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private bindings: KeyBinding[] = parseKeyBindings(getStringSetting(KEY_BINDINGS_KEY, ""));
|
||||
private bindingMap = keyBindingMap(this.bindings);
|
||||
|
||||
private readonly router = new KeyRouter({
|
||||
ready: () => this.host?.ready() ?? false,
|
||||
locked: () => this.host?.locked() ?? false,
|
||||
screenOn: () => shell.isScreenOn(),
|
||||
wake: () => this.host?.wake(),
|
||||
offerToWindow: (event) => shell.receiveKeyEvent(event),
|
||||
gesture: (gesture, origin) => this.host?.gesture(gesture, origin) ?? Promise.resolve(),
|
||||
bindings: () => this.bindingMap,
|
||||
noteActivity: () => shell.noteUserActivity(),
|
||||
log: (line) => console.log(`[keyboard] ${line}`),
|
||||
});
|
||||
private readonly translator = new KeyTranslator({ emit: (event) => this.router.handleKey(event) });
|
||||
|
||||
readonly bridge = new KeyboardBridge({
|
||||
send: (record) => {
|
||||
const session = this.session;
|
||||
if (!session) return false;
|
||||
try {
|
||||
return sendKeyboardControlRecord(session, record, `op=${record[3]}`);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
loadSaved: loadSavedKeyboard,
|
||||
storeSaved: (saved) => setStringSetting(SAVED_KEYBOARD_KEY, saved ? JSON.stringify(saved) : ""),
|
||||
onKeys: (events) => this.translator.handle(events),
|
||||
onKeyboardGone: () => {
|
||||
this.translator.reset();
|
||||
this.router.reset();
|
||||
},
|
||||
onChanged: () => {
|
||||
for (const listener of [...this.listeners]) listener();
|
||||
},
|
||||
// Pairing and setup are rare and are what a failure report needs, so
|
||||
// they always reach the device log. Nothing here ever includes a key.
|
||||
log: (line, routine) => {
|
||||
if (routine) this.host?.log(line);
|
||||
else console.log(`[keyboard] ${line}`);
|
||||
},
|
||||
randomPasskey,
|
||||
shouldReconnect: () => this.host?.shouldReconnect() ?? true,
|
||||
});
|
||||
|
||||
setHost(host: KeyboardServiceHost): void {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
snapshot(): KeyboardBridgeSnapshot {
|
||||
return this.bridge.snapshot();
|
||||
}
|
||||
|
||||
/** Called on every bridge state change. Returns an unsubscribe function. */
|
||||
subscribe(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
/** The glasses came into use (the display woke): look for an absent keyboard now. */
|
||||
retryNow(): void {
|
||||
this.bridge.retryNow();
|
||||
}
|
||||
|
||||
/** A G2 session on firmware with the bridge is up. Idempotent. */
|
||||
linkReady(): void {
|
||||
const session = keyboardLinkSession();
|
||||
if (!session) return;
|
||||
if (session !== this.session) {
|
||||
// The native session object is rebuilt across reconnects.
|
||||
this.offEvents?.();
|
||||
this.session = session;
|
||||
this.offEvents = addKeyboardEventListener(session, (body) => this.bridge.accept(body));
|
||||
}
|
||||
this.bridge.linkReady();
|
||||
}
|
||||
|
||||
/**
|
||||
* The session can't host the keyboard any more. `release` asks the firmware
|
||||
* to let the keyboard go first, for when the link itself stays up (the
|
||||
* glasses went into their case, or the app is disconnecting on purpose).
|
||||
* Idempotent.
|
||||
*/
|
||||
linkLost(release = false): void {
|
||||
if (release) this.bridge.linkClosing();
|
||||
this.bridge.linkLost();
|
||||
this.offEvents?.();
|
||||
this.offEvents = null;
|
||||
this.session = null;
|
||||
}
|
||||
|
||||
/** A key a worker's window was sent and had no use for (the "key-unhandled" reply). */
|
||||
handleUnhandledKey(event: KeyEvent): void {
|
||||
this.router.handleUnhandledKey(event);
|
||||
}
|
||||
|
||||
// ---------- Custom keybindings ----------
|
||||
|
||||
/** The custom keybindings, in the order they were added. */
|
||||
keyBindings(): readonly KeyBinding[] {
|
||||
return this.bindings;
|
||||
}
|
||||
|
||||
/** Bind a chord, replacing what it was bound to. False when the list is full. */
|
||||
setKeyBinding(chord: KeyChord, action: KeyAction): boolean {
|
||||
const id = chordId(chord);
|
||||
const index = this.bindings.findIndex((binding) => chordId(binding.chord) === id);
|
||||
if (index < 0 && this.bindings.length >= MAX_KEY_BINDINGS) return false;
|
||||
const next = [...this.bindings];
|
||||
if (index < 0) next.push({ chord, action });
|
||||
else next[index] = { chord, action };
|
||||
this.storeBindings(next);
|
||||
return true;
|
||||
}
|
||||
|
||||
removeKeyBinding(chord: KeyChord): void {
|
||||
const id = chordId(chord);
|
||||
this.storeBindings(this.bindings.filter((binding) => chordId(binding.chord) !== id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the next key pressed on the keyboard instead of acting on it. Returns
|
||||
* a function that calls the capture off.
|
||||
*/
|
||||
captureNextKey(onChord: (chord: KeyChord) => void): () => void {
|
||||
return this.router.captureNextKey(onChord);
|
||||
}
|
||||
|
||||
private storeBindings(bindings: KeyBinding[]): void {
|
||||
this.bindings = bindings;
|
||||
this.bindingMap = keyBindingMap(bindings);
|
||||
setStringSetting(KEY_BINDINGS_KEY, serializeKeyBindings(bindings));
|
||||
}
|
||||
}
|
||||
|
||||
export const keyboardService = new KeyboardService();
|
||||
@@ -1,23 +1,26 @@
|
||||
import { GrayImage, G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { screenHeight, screenWidth } from "../graphics/screen-size";
|
||||
import { getDefaultMediumFont } from "../graphics/ui-fonts";
|
||||
import { wrapText } from "../graphics/textwrap";
|
||||
|
||||
export const LOCK_SCREEN_SURFACE_ID = "lock-screen";
|
||||
const LOCK_SCREEN_MESSAGE = "Glasses locked; unlock the phone to unlock the glasses.";
|
||||
|
||||
/** The paired glasses' whole screen, black, with the message in a box at its centre. */
|
||||
export function createLockScreenImage(): GrayImage {
|
||||
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||
const width = screenWidth(), height = screenHeight();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const font = getDefaultMediumFont();
|
||||
const boxWidth = 480;
|
||||
const boxHeight = 150;
|
||||
const boxX = Math.round((G2_LENS_WIDTH - boxWidth) / 2);
|
||||
const boxY = Math.round((G2_LENS_HEIGHT - boxHeight) / 2);
|
||||
const boxX = Math.round((width - boxWidth) / 2);
|
||||
const boxY = Math.round((height - boxHeight) / 2);
|
||||
image.drawRoundedRect(boxX, boxY, boxWidth, boxHeight, 150, 12);
|
||||
const lines = wrapText(font, LOCK_SCREEN_MESSAGE, boxWidth - 64);
|
||||
const textHeight = lines.length * font.lineHeight;
|
||||
const firstY = boxY + Math.round((boxHeight - textHeight) / 2);
|
||||
lines.forEach((line, index) => {
|
||||
const x = Math.round((G2_LENS_WIDTH - font.measureText(line)) / 2);
|
||||
const x = Math.round((width - font.measureText(line)) / 2);
|
||||
image.drawText(font, x, firstY + index * font.lineHeight, line, 230);
|
||||
});
|
||||
return image;
|
||||
|
||||
+104
-10
@@ -16,12 +16,11 @@ import {
|
||||
RING_CALIBRATION,
|
||||
sortedByProximity,
|
||||
zoneGlyph,
|
||||
zoneLabel,
|
||||
type ProximityEstimate,
|
||||
type ProximityZone,
|
||||
} from "./ble-proximity";
|
||||
import { classifyAdvertisement, normalizeMacAddress, type EvenAdvertisement, type RawAdvertisement } from "./even-advertisement";
|
||||
import { glassesImagePath, R1_IMAGE_PATH } from "./glasses-artwork";
|
||||
import { classifyAdvertisement, normalizeMacAddress, type EvenAdvertisement, type G1Firmware, type RawAdvertisement } from "./even-advertisement";
|
||||
import { GENERIC_G2_IMAGE_PATH, glassesImagePath, R1_IMAGE_PATH } from "./glasses-artwork";
|
||||
import {
|
||||
colorwayDisplayName,
|
||||
colorwaySwatchHex,
|
||||
@@ -48,6 +47,8 @@ export type GlassesPairCandidate = {
|
||||
rssi: number | null;
|
||||
proximity: ProximityEstimate | null;
|
||||
lastSeenMs: number;
|
||||
/** An Even G1 pair: the arms' firmware, the "lower" of the two (stock, then safe mode). Absent on a G2. */
|
||||
g1Firmware?: G1Firmware;
|
||||
/**
|
||||
* Set when this lone arm and a lone arm of the other side carry different
|
||||
* serials — the mixed-pair situation the serial check exists to name.
|
||||
@@ -56,6 +57,15 @@ export type GlassesPairCandidate = {
|
||||
mismatchWarning: string | null;
|
||||
};
|
||||
|
||||
/** A RayNeo iO: one device, one address (no arms to join). */
|
||||
export type RayNeoCandidate = {
|
||||
id: string;
|
||||
advertisement: EvenAdvertisement;
|
||||
rssi: number | null;
|
||||
proximity: ProximityEstimate | null;
|
||||
lastSeenMs: number;
|
||||
};
|
||||
|
||||
export type RingCandidate = {
|
||||
id: string;
|
||||
advertisement: EvenAdvertisement;
|
||||
@@ -176,9 +186,10 @@ export class DiscoveryAggregator {
|
||||
pairs(): GlassesPairCandidate[] {
|
||||
const groups = new Map<string, { left: (EvenAdvertisement & { smoothedRssi: number | null }) | null; right: (EvenAdvertisement & { smoothedRssi: number | null }) | null }>();
|
||||
for (const record of this.byAddress.values()) {
|
||||
if (record.role === "ring") continue;
|
||||
if (record.role === "ring" || record.role === "rayneo-io") continue;
|
||||
const canonical = serialKey(record.serial);
|
||||
const key = canonical ? `serial:${canonical}` : `arm:${record.address}`;
|
||||
const family = record.g1Firmware ? "g1:" : "";
|
||||
const key = canonical ? `${family}serial:${canonical}` : `arm:${record.address}`;
|
||||
const group = groups.get(key) ?? { left: null, right: null };
|
||||
const slot = record.role === "left" ? "left" : "right";
|
||||
const incumbent = group[slot];
|
||||
@@ -199,7 +210,9 @@ export class DiscoveryAggregator {
|
||||
pairs.push({
|
||||
id: serialKey(serial) ?? `arm:${any.address}`,
|
||||
serial,
|
||||
identity: GlassesHardwareIdentity.decode(serial),
|
||||
// Even's G2 serial scheme; a G1's says nothing to it.
|
||||
identity: any.g1Firmware ? null : GlassesHardwareIdentity.decode(serial),
|
||||
...(any.g1Firmware ? { g1Firmware: g1PairFirmware(left, right) } : {}),
|
||||
left: left ? stripSmoothing(left) : null,
|
||||
right: right ? stripSmoothing(right) : null,
|
||||
completeness: left && right ? "complete" : left ? "left-only" : "right-only",
|
||||
@@ -225,6 +238,22 @@ export class DiscoveryAggregator {
|
||||
}
|
||||
|
||||
/** Every R1 ring seen, closest first. */
|
||||
rayneos(): RayNeoCandidate[] {
|
||||
const found: RayNeoCandidate[] = [];
|
||||
for (const record of this.byAddress.values()) {
|
||||
if (record.role !== "rayneo-io") continue;
|
||||
const rssi = record.smoothedRssi != null ? Math.round(record.smoothedRssi) : null;
|
||||
found.push({
|
||||
id: `rayneo:${record.address}`,
|
||||
advertisement: stripSmoothing(record),
|
||||
rssi,
|
||||
proximity: estimateProximity(rssi, GLASSES_CALIBRATION),
|
||||
lastSeenMs: record.seenAtMs,
|
||||
});
|
||||
}
|
||||
return sortedByProximity(found, (candidate) => candidate.proximity, (candidate) => candidate.id);
|
||||
}
|
||||
|
||||
rings(): RingCandidate[] {
|
||||
const rings: RingCandidate[] = [];
|
||||
for (const record of this.byAddress.values()) {
|
||||
@@ -281,7 +310,7 @@ export function isPreviouslyPaired(name: string | null | undefined, pairedSerial
|
||||
|
||||
export type PairingRowPresentation = {
|
||||
id: string;
|
||||
kind: "glasses" | "ring";
|
||||
kind: "glasses" | "ring" | "rayneo-io";
|
||||
/** "Even G2 B" / "Even R1" / fallback product name. */
|
||||
modelTitle: string;
|
||||
/** The serial (glasses) or local name (ring). */
|
||||
@@ -299,7 +328,6 @@ export type PairingRowPresentation = {
|
||||
isPreviouslyPaired: boolean;
|
||||
/** "Yours" / "Closest" / "". */
|
||||
badge: string;
|
||||
proximitySummary: string;
|
||||
proximityGlyph: string;
|
||||
zone: ProximityZone | null;
|
||||
/** "Left + right arms found", "Only the left arm has been seen", …; "" when there is nothing worth a line (an unbonded ring). */
|
||||
@@ -325,12 +353,11 @@ function presentationBase(
|
||||
isNearest: boolean,
|
||||
isPaired: boolean,
|
||||
warnings: readonly string[],
|
||||
): Pick<PairingRowPresentation, "isNearest" | "isPreviouslyPaired" | "badge" | "proximitySummary" | "proximityGlyph" | "zone" | "warning"> {
|
||||
): Pick<PairingRowPresentation, "isNearest" | "isPreviouslyPaired" | "badge" | "proximityGlyph" | "zone" | "warning"> {
|
||||
return {
|
||||
isNearest,
|
||||
isPreviouslyPaired: isPaired,
|
||||
badge: isPaired ? "Yours" : isNearest ? "Closest" : "",
|
||||
proximitySummary: proximity ? zoneLabel(proximity.zone) : "Signal unknown",
|
||||
proximityGlyph: proximity ? zoneGlyph(proximity.zone) : "○○○○",
|
||||
zone: proximity?.zone ?? null,
|
||||
warning: warnings.join(" "),
|
||||
@@ -342,7 +369,52 @@ function embeddedMacMismatchWarning(subject: string, ad: EvenAdvertisement | nul
|
||||
return `${subject} advertised address ${ad.embeddedMac} differs from its radio address ${ad.address}.`;
|
||||
}
|
||||
|
||||
/** The pair can only do what its less capable arm can. */
|
||||
function g1PairFirmware(left: EvenAdvertisement | null, right: EvenAdvertisement | null): G1Firmware {
|
||||
const firmwares = [left?.g1Firmware, right?.g1Firmware];
|
||||
if (firmwares.includes("stock")) return "stock";
|
||||
return firmwares.includes("g1v0-safe") ? "g1v0-safe" : "g1v0";
|
||||
}
|
||||
|
||||
export function g1Row(pair: GlassesPairCandidate, context: RowContext = {}): PairingRowPresentation {
|
||||
const isPreviouslyPairedRow = isPreviouslyPaired(pair.serial, context.pairedSerial);
|
||||
const isNearest = !!context.nearestId && context.nearestId === pair.id;
|
||||
const warnings: string[] = [];
|
||||
if (pair.mismatchWarning) warnings.push(pair.mismatchWarning);
|
||||
let armsSummary: string;
|
||||
let canSelect = false;
|
||||
if (pair.completeness !== "complete") {
|
||||
armsSummary = pair.completeness === "left-only"
|
||||
? "Only the left arm has been seen — waiting for the right"
|
||||
: "Only the right arm has been seen — waiting for the left";
|
||||
} else if (pair.g1Firmware === "stock") {
|
||||
armsSummary = "Runs Even's firmware. Faceclaw will install its own, with the glasses in the case";
|
||||
canSelect = true;
|
||||
} else if (pair.g1Firmware === "g1v0-safe") {
|
||||
armsSummary = "In safe mode: the firmware restarted repeatedly. Faceclaw can install it again, with the glasses in the case";
|
||||
canSelect = true;
|
||||
} else {
|
||||
armsSummary = "Left + right arms found. To pair, keep them in the case with the lid open";
|
||||
canSelect = true;
|
||||
}
|
||||
return {
|
||||
id: pair.id,
|
||||
kind: "glasses",
|
||||
modelTitle: "Even Realities G1",
|
||||
deviceName: pair.serial ?? (pair.left ?? pair.right)!.name,
|
||||
variantSummary: null,
|
||||
colorway: null,
|
||||
swatchHex: null,
|
||||
imagePath: GENERIC_G2_IMAGE_PATH,
|
||||
hasVariantImage: false,
|
||||
...presentationBase(pair.proximity, isNearest, isPreviouslyPairedRow, warnings),
|
||||
armsSummary,
|
||||
canSelect,
|
||||
};
|
||||
}
|
||||
|
||||
export function glassesRow(pair: GlassesPairCandidate, context: RowContext = {}): PairingRowPresentation {
|
||||
if (pair.g1Firmware) return g1Row(pair, context);
|
||||
const identity = pair.identity;
|
||||
const imagePath = glassesImagePath(identity);
|
||||
const isPreviouslyPairedRow = isPreviouslyPaired(pair.serial, context.pairedSerial);
|
||||
@@ -416,6 +488,28 @@ export function ringRow(ring: RingCandidate, context: RowContext = {}): PairingR
|
||||
};
|
||||
}
|
||||
|
||||
export function rayneoRow(candidate: RayNeoCandidate, context: { pairedAddress?: string; nearestId?: string | null } = {}): PairingRowPresentation {
|
||||
const ad = candidate.advertisement;
|
||||
const isPaired = !!context.pairedAddress && normalizeMacAddress(context.pairedAddress) === ad.address;
|
||||
const isNearest = !!context.nearestId && context.nearestId === candidate.id;
|
||||
const warnings: string[] = [];
|
||||
if (ad.embeddedMacMismatch) warnings.push(`It advertises ${ad.embeddedMac} but was heard as ${ad.address}.`);
|
||||
return {
|
||||
id: candidate.id,
|
||||
kind: "rayneo-io",
|
||||
modelTitle: "RayNeo iO",
|
||||
deviceName: ad.name || ad.address,
|
||||
variantSummary: null,
|
||||
colorway: null,
|
||||
swatchHex: null,
|
||||
imagePath: GENERIC_G2_IMAGE_PATH,
|
||||
hasVariantImage: false,
|
||||
...presentationBase(candidate.proximity, isNearest, isPaired, warnings),
|
||||
armsSummary: "Needs the Faceclaw custom firmware. First time: put it in pairing mode (on the charger, hold the button ~5 s until the light blinks blue).",
|
||||
canSelect: true,
|
||||
};
|
||||
}
|
||||
|
||||
/** Convenience: a colourway's display text, for rows that show the swatch alone. */
|
||||
export function colorwayLabel(colorway: GlassesColorway | null): string {
|
||||
return colorway ? colorwayDisplayName(colorway) : "";
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
|
||||
import { previewScalingSetting } from "../ui/dashboard-settings";
|
||||
import { phonePreviewRect, usedScreenRect, type ScreenRect } from "../ui/shell/geometry";
|
||||
import { phonePreviewRect, screenHeight, screenWidth, usedScreenRect, type ScreenRect } from "../ui/shell/geometry";
|
||||
import { shell } from "../ui/shell/shell";
|
||||
|
||||
/**
|
||||
@@ -14,7 +13,7 @@ export function phonePreviewCrop(locked: boolean, glanceRect: ScreenRect | null)
|
||||
if (locked || previewScalingSetting.get() === "raw") return null;
|
||||
const window = shell.foregroundWindow();
|
||||
const crop = phonePreviewRect(glanceRect ?? usedScreenRect(window?.heightMode ?? "min", window?.appId));
|
||||
const wholePanel = crop.x === 0 && crop.y === 0 && crop.width === G2_LENS_WIDTH && crop.height === G2_LENS_HEIGHT;
|
||||
const wholePanel = crop.x === 0 && crop.y === 0 && crop.width === screenWidth() && crop.height === screenHeight();
|
||||
return wholePanel ? null : crop;
|
||||
}
|
||||
|
||||
@@ -27,7 +26,7 @@ export function phonePreviewCrop(locked: boolean, glanceRect: ScreenRect | null)
|
||||
export function mirrorToPanelFraction(crop: ScreenRect | null, nx: number, ny: number): { nx: number; ny: number } {
|
||||
if (!crop) return { nx, ny };
|
||||
return {
|
||||
nx: (crop.x + nx * crop.width) / G2_LENS_WIDTH,
|
||||
ny: (crop.y + ny * crop.height) / G2_LENS_HEIGHT,
|
||||
nx: (crop.x + nx * crop.width) / screenWidth(),
|
||||
ny: (crop.y + ny * crop.height) / screenHeight(),
|
||||
};
|
||||
}
|
||||
+56
-16
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* The Wear OS watch as an input device and status mirror for the glasses.
|
||||
* The watch (Wear OS or Apple Watch) as an input device and status mirror for
|
||||
* the glasses.
|
||||
*
|
||||
* Inbound, the watch sends ring-style gestures (tap, double-tap, hold,
|
||||
* swipe/crown scrolls, "Hey Even") which feed the exact synthetic-ring path
|
||||
@@ -17,22 +18,28 @@
|
||||
* The watch's own battery flows the other way for the glasses' top-bar and
|
||||
* Glanceboard indicators: the phone polls it (the watch answers from a
|
||||
* listener service even while its app is closed) and the open watch app
|
||||
* pushes changes unprompted. The level lives in shell.getBatteryLevels()
|
||||
* beside the G2 and R1 ones and is cleared when no watch is reachable.
|
||||
* pushes changes unprompted. An Apple Watch app can't be woken to answer, so
|
||||
* it reports when it can instead (see wear/PROTOCOL.md). The level lives in
|
||||
* shell.getBatteryLevels() beside the G2 and R1 ones and is cleared when no
|
||||
* watch is reachable or the last report has gone stale.
|
||||
*
|
||||
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge); the
|
||||
* message shapes are documented in wear/PROTOCOL.md.
|
||||
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge) or
|
||||
* wear-bridge.ios.ts (FaceclawWatchBridge); the message shapes are documented
|
||||
* in wear/PROTOCOL.md.
|
||||
*/
|
||||
import { flagAnalyticsEvent } from "../analytics/analytics";
|
||||
import { type AppDefinition } from "../apps/app-definition";
|
||||
import { getInstalledEvenHubApps, installedEvenHubAppId } from "../apps/evenhub/installed-apps";
|
||||
import { WEAR_PATHS, wearBridge, type WearMessage } from "../native/wear-bridge";
|
||||
import {
|
||||
glassesModelName,
|
||||
onAnySettingChanged,
|
||||
watchCanUnlockSetting,
|
||||
watchCrownClockwiseNextSetting,
|
||||
watchMirrorAssistantSetting,
|
||||
watchRemoteEnabledSetting,
|
||||
watchSoundEffectsSetting,
|
||||
watchVibrateOnInputSetting,
|
||||
} from "../ui/dashboard-settings";
|
||||
import { shell, type AssistantActivityEvent } from "../ui/shell/shell";
|
||||
import { voiceActivity } from "../ui/shell/voice-activity";
|
||||
@@ -102,17 +109,26 @@ const WATCH_BATTERY_POLL_INTERVAL_MS = 5 * 60_000;
|
||||
// glasses connecting) would otherwise trigger back to back.
|
||||
const WATCH_BATTERY_POLL_MIN_GAP_MS = 20_000;
|
||||
const WATCH_BATTERY_URGENT_POLL_LIMIT = 3;
|
||||
// Wear OS answers every poll. A closed Apple Watch app only reports when the
|
||||
// system gives it background time, which can be hours apart, so a level this
|
||||
// old says more about the watch being gone than about its charge.
|
||||
const WATCH_BATTERY_MAX_AGE_MS = 4 * 60 * 60_000;
|
||||
|
||||
/** What the dashboard controller lends the watch remote. */
|
||||
export type WearRemoteHost = {
|
||||
/** The launcher-grid apps; installed EvenHub packages are added dynamically. */
|
||||
apps: readonly AppDefinition[];
|
||||
/** The launcher-grid apps for the paired glasses; installed EvenHub packages are added dynamically. */
|
||||
apps: () => readonly AppDefinition[];
|
||||
injectInput: (kind: WearRemoteInputKind) => Promise<void>;
|
||||
launchApp: (appId: string) => Promise<void>;
|
||||
connect: () => Promise<void>;
|
||||
disconnect: () => Promise<void>;
|
||||
setGlassesLocked: (locked: boolean, reason: string) => void;
|
||||
requestShellRender: () => void;
|
||||
/**
|
||||
* The shell is running on the phone's preview with no glasses involved, so
|
||||
* gestures have somewhere to go even though nothing is connected.
|
||||
*/
|
||||
previewInputActive?: () => boolean;
|
||||
getState: () => {
|
||||
phase: string;
|
||||
status: string;
|
||||
@@ -136,6 +152,7 @@ export class WearRemote {
|
||||
private lastAssistantSentAtMs = 0;
|
||||
private lastWatchBatteryPollAtMs = 0;
|
||||
private watchBatteryKnown = false;
|
||||
private watchBatteryReportedAtMs = 0;
|
||||
// Urgent polls that got no answer; a watch app too old to answer them
|
||||
// must not be poked every publish, so after a few the cadence drops back.
|
||||
private watchBatteryPollsUnanswered = 0;
|
||||
@@ -143,7 +160,7 @@ export class WearRemote {
|
||||
constructor(private readonly host: WearRemoteHost) {
|
||||
this.active = wearBridge.isAvailable();
|
||||
if (!this.active) {
|
||||
host.appendLog("watch: Google Play services unavailable; watch remote disabled");
|
||||
host.appendLog(`watch: ${global.isIOS ? "WatchConnectivity" : "Google Play services"} unavailable; watch remote disabled`);
|
||||
return;
|
||||
}
|
||||
wearBridge.onMessage((message) => {
|
||||
@@ -183,10 +200,25 @@ export class WearRemote {
|
||||
// Catch-all refresh for state the controller does not announce (cheap:
|
||||
// identical states are dropped on the Java side).
|
||||
setInterval(() => this.schedulePublish(), STATE_REFRESH_INTERVAL_MS);
|
||||
setInterval(() => this.pollWatchBattery(false), WATCH_BATTERY_POLL_INTERVAL_MS);
|
||||
setInterval(() => {
|
||||
this.expireWatchBattery();
|
||||
this.pollWatchBattery(false);
|
||||
}, WATCH_BATTERY_POLL_INTERVAL_MS);
|
||||
this.schedulePublish();
|
||||
}
|
||||
|
||||
/** Gestures have a display to act on: the glasses, or the phone's preview without them. */
|
||||
private takesInput(): boolean {
|
||||
const phase = this.host.getState().phase;
|
||||
return phase === "connected" || phase === "charging" || this.host.previewInputActive?.() === true;
|
||||
}
|
||||
|
||||
private expireWatchBattery(): void {
|
||||
if (this.watchBatteryKnown && Date.now() - this.watchBatteryReportedAtMs > WATCH_BATTERY_MAX_AGE_MS) {
|
||||
this.setWatchBattery(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask the reachable watch for its battery. `urgent` skips the poll gap for
|
||||
* a level that is not known yet (the glasses just connected, the watch
|
||||
@@ -257,11 +289,13 @@ export class WearRemote {
|
||||
phase: state.phase,
|
||||
status: state.status,
|
||||
connected: state.phase === "connected" || state.phase === "charging",
|
||||
preview: this.host.previewInputActive?.() === true,
|
||||
screenOn: shell.isScreenOn(),
|
||||
locked: state.glassesLocked,
|
||||
worn: state.glassesWorn,
|
||||
listening: voiceActivity.isActive(),
|
||||
battery: state.lastHeadsetBattery,
|
||||
glassesModel: glassesModelName(),
|
||||
charging: state.phase === "charging" || battery.headsetCharging === true,
|
||||
caseBattery: battery.case,
|
||||
caseCharging: battery.caseCharging,
|
||||
@@ -278,6 +312,8 @@ export class WearRemote {
|
||||
apps: this.listApps(),
|
||||
remoteEnabled: watchRemoteEnabledSetting.get(),
|
||||
crownClockwiseNext: watchCrownClockwiseNextSetting.get(),
|
||||
vibrateOnInput: watchVibrateOnInputSetting.get(),
|
||||
soundEffects: watchSoundEffectsSetting.get(),
|
||||
canUnlock: watchCanUnlockSetting.get(),
|
||||
mirrorAssistant: watchMirrorAssistantSetting.get(),
|
||||
assistantAvailable: shell.isAssistantAvailable(),
|
||||
@@ -286,7 +322,7 @@ export class WearRemote {
|
||||
|
||||
/** Launchable apps, matching the launcher grid and the apps.launch tool. */
|
||||
private listApps(): Array<{ appId: string; title: string }> {
|
||||
const apps = this.host.apps.map((app) => ({ appId: app.appId, title: app.title }));
|
||||
const apps = this.host.apps().map((app) => ({ appId: app.appId, title: app.title }));
|
||||
for (const installed of getInstalledEvenHubApps()) {
|
||||
apps.push({ appId: installedEvenHubAppId(installed.packageId), title: installed.name });
|
||||
}
|
||||
@@ -307,7 +343,11 @@ export class WearRemote {
|
||||
// acked (the watch sends it fire-and-forget, without a seq).
|
||||
this.watchBatteryPollsUnanswered = 0;
|
||||
const battery = readNumber(message.payload.battery, -1);
|
||||
const valid = Number.isInteger(battery) && battery >= 0 && battery <= 100;
|
||||
// A report the watch left behind earlier says when it was taken; a live one is current.
|
||||
const reportedAtMs = readNumber(message.payload.at, Date.now());
|
||||
const valid = Number.isInteger(battery) && battery >= 0 && battery <= 100 &&
|
||||
Date.now() - reportedAtMs <= WATCH_BATTERY_MAX_AGE_MS;
|
||||
this.watchBatteryReportedAtMs = reportedAtMs;
|
||||
this.setWatchBattery(valid ? battery : null, valid ? message.payload.charging === true : null);
|
||||
return;
|
||||
}
|
||||
@@ -315,8 +355,7 @@ export class WearRemote {
|
||||
// Touch-down ahead of the gesture that follows it. Fire-and-forget like
|
||||
// the battery (no seq, never acked): wherever that gesture would be
|
||||
// refused, the press is dropped silently and the gesture's ack says why.
|
||||
const phase = this.host.getState().phase;
|
||||
if (watchRemoteEnabledSetting.get() && (phase === "connected" || phase === "charging")) {
|
||||
if (watchRemoteEnabledSetting.get() && this.takesInput()) {
|
||||
await this.host.injectInput("ring-press");
|
||||
}
|
||||
return;
|
||||
@@ -378,8 +417,7 @@ export class WearRemote {
|
||||
return;
|
||||
}
|
||||
const kind = gesture as WearRemoteInputKind;
|
||||
const state = this.host.getState();
|
||||
if (state.phase !== "connected" && state.phase !== "charging") {
|
||||
if (!this.takesInput()) {
|
||||
ack(false, "The glasses are not connected.");
|
||||
return;
|
||||
}
|
||||
@@ -422,7 +460,7 @@ export class WearRemote {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!connected) {
|
||||
if (!this.takesInput()) {
|
||||
ack(false, "The glasses are not connected.");
|
||||
return;
|
||||
}
|
||||
@@ -561,6 +599,8 @@ export class WearRemote {
|
||||
}
|
||||
|
||||
function readNumber(value: unknown, fallback: number): number {
|
||||
// Number(null) is 0: a watch reporting an unknown battery would read as empty.
|
||||
if (value === null) return fallback;
|
||||
const numeric = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(numeric) ? numeric : fallback;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user