Add experimental option to end EvenHub session when the screen is off for power saving

This commit is contained in:
James Babcock committed 2026-07-23 18:40:48 -07:00
1 parent 7455775390
commit e5649e9782
8 files changed
+315 -12

No files matched your search

+123 -10
View File
@@ -79,6 +79,7 @@ import {
saveVoiceRecordingsSetting,
screenTimeoutSetting,
screenTimeoutSettingToMs,
suspendEvenHubWhenScreenOffSetting,
systemCardNameSetting,
voiceProviderSetting,
type ConfigSettingString,
@@ -119,6 +120,7 @@ const SHELL_SURFACE_ID = "shell";
const SHELL_REFRESH_INTERVAL_MS = 60_000;
const PREVIEW_INTERVAL_MS = 1_000;
const SCREEN_TIMEOUT_CHECK_MS = 1_000;
const EVENHUB_SCREEN_OFF_SUSPEND_DELAY_MS = 5_000;
const FOREGROUND_NOTIFICATION_MIN_UPDATE_MS = 30_000;
const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 6_000;
const CONNECTED_PREVIEW_MIN_UPDATE_MS = 1_000;
@@ -180,6 +182,8 @@ class DashboardController {
private shellRefreshTimer: ReturnType<typeof setInterval> | null = null;
private previewTimer: ReturnType<typeof setInterval> | null = null;
private screenTimeoutTimer: ReturnType<typeof setInterval> | null = null;
private evenHubSuspendTimer: ReturnType<typeof setTimeout> | null = null;
private evenHubSessionSuspended = false;
private offState: (() => void) | null = null;
private offLog: (() => void) | null = null;
private offRing: (() => void) | null = null;
@@ -230,16 +234,7 @@ class DashboardController {
getScreenTimeoutMs: () => screenTimeoutSettingToMs(screenTimeoutSetting.get()),
requestShellRender: () => this.requestShellRender(),
onScreenStateChanged: (on) => {
const communicator = this.communicator;
if (!communicator) return;
void (async () => {
// Blanking is a compositor-level flag so worker-window surfaces go
// dark too; retained state survives for instant wake.
await communicator.setScreenBlanked(!on);
await communicator.setG2ScreenOn(on);
})().catch((error) => {
this.appendLog(`screen state change failed: ${this.formatError(error)}`);
});
this.handleScreenStateChanged(on);
if (on) this.requestShellRender();
},
});
@@ -283,9 +278,115 @@ class DashboardController {
// Apply a firmware-debug-flags toggle live while connected (Java dedups).
this.pushFirmwareDebugFlags();
this.pushBrightness();
this.syncEvenHubScreenOffSetting();
});
}
private handleScreenStateChanged(on: boolean): void {
const communicator = this.communicator;
if (!communicator) return;
if (on) {
this.cancelEvenHubSuspendTimer();
const shouldResumeEvenHub = this.evenHubSessionSuspended;
this.evenHubSessionSuspended = false;
void (async () => {
await communicator.setG2ScreenOn(true);
// Restore the retained frame before allowing Java's session driver to
// recreate the layout, so its first real image is the current screen
// rather than the black sleep frame.
await communicator.setScreenBlanked(false);
if (shouldResumeEvenHub) {
await communicator.resumeEvenHubSession();
}
})().catch((error) => {
this.appendLog(`screen wake failed: ${this.formatError(error)}`);
});
return;
}
// Blanking is a compositor-level flag so worker-window surfaces go dark
// too; retained state survives while the EvenHub page is absent.
void (async () => {
await communicator.setScreenBlanked(true);
await communicator.setG2ScreenOn(false);
})().catch((error) => {
this.appendLog(`screen sleep failed: ${this.formatError(error)}`);
});
this.scheduleEvenHubSuspend();
}
private syncEvenHubScreenOffSetting(): void {
if (suspendEvenHubWhenScreenOffSetting.get() && !shell.isScreenOn()) {
this.scheduleEvenHubSuspend();
return;
}
this.cancelEvenHubSuspendTimer();
if (!this.evenHubSessionSuspended) return;
// Turning the experiment off restores the legacy all-black page even if
// the screen remains asleep.
const communicator = this.communicator;
this.evenHubSessionSuspended = false;
if (!communicator) return;
void communicator.resumeEvenHubSession().catch((error) => {
this.appendLog(`EvenHub resume failed: ${this.formatError(error)}`);
});
}
private scheduleEvenHubSuspend(): void {
this.cancelEvenHubSuspendTimer();
if (
!suspendEvenHubWhenScreenOffSetting.get() ||
shell.isScreenOn() ||
this.phase !== "connected" ||
!this.communicator ||
this.evenHubSessionSuspended
) {
return;
}
const communicator = this.communicator;
this.evenHubSuspendTimer = setTimeout(() => {
this.evenHubSuspendTimer = null;
if (
!suspendEvenHubWhenScreenOffSetting.get() ||
shell.isScreenOn() ||
this.phase !== "connected" ||
this.communicator !== communicator
) {
return;
}
this.evenHubSessionSuspended = true;
void communicator
.suspendEvenHubSession()
.then((suspended) => {
if (suspended) {
this.appendLog("EvenHub session suspended while screen is off");
return;
}
if (this.communicator !== communicator || shell.isScreenOn()) return;
this.evenHubSessionSuspended = false;
this.scheduleEvenHubSuspend();
})
.catch((error) => {
if (this.communicator === communicator) {
this.evenHubSessionSuspended = false;
this.appendLog(`EvenHub suspend failed: ${this.formatError(error)}`);
this.scheduleEvenHubSuspend();
}
});
}, EVENHUB_SCREEN_OFF_SUSPEND_DELAY_MS);
}
private cancelEvenHubSuspendTimer(): void {
if (this.evenHubSuspendTimer === null) return;
clearTimeout(this.evenHubSuspendTimer);
this.evenHubSuspendTimer = null;
}
private pushFirmwareDebugFlags(): void {
if (!this.communicator) return;
void this.communicator
@@ -487,6 +588,9 @@ class DashboardController {
void this.communicator?.setG2ScreenOn(false).catch(() => {});
}
if (mappedPhase === "connected" && this.phase !== "connected") {
// A transport reconnect starts with a live EvenHub lifecycle again;
// if the shell is asleep, begin a fresh five-second grace period.
this.evenHubSessionSuspended = false;
// Push the CFW firmware-debug-flags overlay preference; Java emits the
// mode-7 control message once the dashboard container is warmed up.
this.pushFirmwareDebugFlags();
@@ -494,6 +598,12 @@ class DashboardController {
}
this.setPhase(mappedPhase);
this.setStatus(state.status);
if (mappedPhase === "connected") {
this.syncEvenHubScreenOffSetting();
} else if (mappedPhase !== "charging") {
this.cancelEvenHubSuspendTimer();
this.evenHubSessionSuspended = false;
}
});
this.offRing = communicator.onRingEvent((event) => {
void this.handleInputEvent(event).catch((error) => {
@@ -582,6 +692,7 @@ class DashboardController {
await this.configureWindowSurface(window.surfaceId, window.windowId === foregroundWindowId);
}
await communicator.start();
this.syncEvenHubScreenOffSetting();
shell.foregroundWindow()?.requestRender();
this.requestShellRender();
// Refresh the top-bar clock and the phone-side preview once a minute,
@@ -665,6 +776,7 @@ class DashboardController {
const communicator = this.communicator;
this.communicator = null;
this.evenHubSessionSuspended = false;
try {
const shutdownAcked = await communicator?.sendShutdown(0).catch((error) => {
@@ -1203,6 +1315,7 @@ class DashboardController {
}
private clearDashboardTimer(): void {
this.cancelEvenHubSuspendTimer();
if (this.shellRefreshTimer) {
clearInterval(this.shellRefreshTimer);
this.shellRefreshTimer = null;
+28
View File
@@ -88,6 +88,18 @@ export type RawInputEvent =
eventSource: number;
systemExitReasonCode: number;
frameId: number;
}
| {
/**
* Stock display-lifecycle wake observed after a ring or arm double tap
* while Faceclaw's EvenHub page is suspended.
*/
kind: "display-wake";
containerName: string;
eventType: number;
eventSource: number;
systemExitReasonCode: number;
frameId: number;
};
function nonNegativeNumber(value: number): number {
@@ -467,6 +479,22 @@ export class FaceclawCommunicatorBridge {
return this.enqueueJavaCall(() => Boolean(this.communicator.sendShutdown(exitMode)));
}
/**
* End only the EvenHub page/session, leaving both arm BLE connections and
* their async notification subscriptions alive.
*/
async suspendEvenHubSession(): Promise<boolean> {
return this.enqueueJavaCall(() => Boolean(this.communicator.suspendEvenHubSession()));
}
/**
* Re-enable the EvenHub page lifecycle. The retained compositor frame is
* sent after the layout and image path have been recreated.
*/
async resumeEvenHubSession(): Promise<boolean> {
return this.enqueueJavaCall(() => Boolean(this.communicator.resumeEvenHubSession()));
}
/** Play a CFW mode-5 kind-4 tone sequence (complete wire payload). */
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
const snapshot = new Uint8Array(payload);
+7
View File
@@ -248,6 +248,13 @@ export const firmwareDebugFlagsSetting = new ConfigSettingBoolean({
defaultValue: false,
});
export const suspendEvenHubWhenScreenOffSetting = new ConfigSettingBoolean({
id: "suspend-evenhub-screen-off",
label: "Suspend EvenHub when screen off",
storageKey: "developer.suspendEvenHubWhenScreenOff",
defaultValue: false,
});
export type VoiceProvider = "onboard" | "elevenlabs" | "whisper";
const voiceProviderLabels: Record<VoiceProvider, string> = {
+2
View File
@@ -11,6 +11,7 @@ import {
firmwareDebugFlagsSetting,
rawScreenshotsEnabledSetting,
saveVoiceRecordingsSetting,
suspendEvenHubWhenScreenOffSetting,
terminalAuthTokenSetting,
terminalHostSetting,
terminalPortSetting,
@@ -79,6 +80,7 @@ function settingsSections(): SettingsSection[] {
toggleSettingMenuItem(rawScreenshotsEnabledSetting),
toggleSettingMenuItem(saveVoiceRecordingsSetting),
toggleSettingMenuItem(firmwareDebugFlagsSetting),
toggleSettingMenuItem(suspendEvenHubWhenScreenOffSetting),
],
},
{
+5
View File
@@ -10,6 +10,11 @@ export type DashboardInputEvent =
| { type: "scroll-down" }
| { type: "long-press"; source: "ring" | "left-arm" | "right-arm" }
| { type: "long-press-release"; source: "ring" | "left-arm" | "right-arm" }
/**
* The stock display lifecycle woke while no EvenHub page was running. This
* is wake-only, unlike a normal double-click (which turns an on screen off).
*/
| { type: "display-wake" }
/**
* The on-glasses "Hey Even" wakeword fired. Delivered on sid 0x07 by the
* stock firmware regardless of CFW; the CFW additionally suppresses the stock
+14
View File
@@ -337,6 +337,14 @@ class Shell {
}
async receiveInput(event: DashboardInputEvent, frameId = 0): Promise<ShellInputOutcome> {
// The stock lifecycle has already interpreted the physical double tap as
// "wake". Keep that directionality if delivery is delayed or duplicated.
if (event.type === "display-wake") {
this.lastInputAtMs = Date.now();
const wokeScreen = !this.screenOn && this.wake("sidebar");
return { shell: wokeScreen, window: false };
}
// The wakeword is handled before the screen-off short-circuit so its
// configured action can work from a dark screen. With the CFW the stock
// Even AI app never launches, so the firmware does not power the display
@@ -609,6 +617,10 @@ export function rawInputEventToInputEvent(event: RawInputEvent): DashboardInputE
if (event.eventType === EvenAIStatus.EVEN_AI_WAKE_UP) {
return { type: "wakeword" };
}
} else if (event.kind === "display-wake") {
// Outside an EvenHub page the firmware consumes the physical double tap
// itself and reports only that the stock display lifecycle woke.
return { type: "display-wake" };
} else if (event.kind === "text-click") {
if (event.eventType === OsEventTypeList.SCROLL_BOTTOM_EVENT) {
return { type: "scroll-down" };
@@ -649,6 +661,8 @@ export function inputEventToString(event: DashboardInputEvent): string {
return `Long press from ${event.source}`;
case "long-press-release":
return `Long press release from ${event.source}`;
case "display-wake":
return `Display wake`;
case "wakeword":
return `Wakeword`;
default: