Merge branch 'evenhub-compatibility'

This commit is contained in:
James Babcock committed 2026-08-20 08:46:47 -07:00
commit 0c0fc7d7f6
152 files changed
+13199 -747

No files matched your search

+5
View File
@@ -15,3 +15,8 @@ The TerminusV proportional font is by ohnonot, derived from Terminus:
https://framagit.org/ohnonot/terv-terc. It is also distributed under the SIL
Open Font License; see app/fonts/terminusv/LICENSE.
The CJK font used for EvenHub compatibility is Source Han Sans SC Light by
Adobe: https://github.com/adobe-fonts/source-han-sans. It is distributed under
the SIL Open Font License, Version 1.1; see app/fonts/source-han-sans/LICENSE.txt.
Faceclaw includes the G2's serialized 20 px LVGL build of the font so phone-side
rendering matches the glasses.
@@ -55,6 +55,17 @@
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true">
<!-- Read-only content URIs for cached documents handed to system viewers. -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
<activity
android:name="com.tns.NativeScriptActivity"
android:label="@string/title_activity_kimera"
@@ -42,6 +42,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private static final String G2_SCREEN_WAKE_LOCK_TAG = "Faceclaw:G2Screen";
private static final long FACECLAW_WAKE_LEASE_RENEW_MS = 45_000;
private static final long FACECLAW_WAKE_CONTROL_WAIT_MS = 1_500;
private static final long CFW_CLEANUP_WAIT_MS = 4_000;
private final Context appContext;
private final PowerManager powerManager;
@@ -102,6 +103,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private int faceclawFramebufferControlGeneration;
private int faceclawFramebufferControlSentCount;
private int faceclawWakePendingNonce = -1;
private boolean cfwCleanupSupported;
private boolean cfwCleanupDelivered;
private int lastCfwCleanupAckMagic;
private long reconnectAfterMs;
private long ringReconnectAfterMs;
@@ -174,6 +178,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private int desiredHeight;
private int desiredPaintMs;
private int desiredFrameId;
// Screen-space deferred draws (glyphs + images) whose pixels are baked
// into desiredPacked; the texture-cache planner may replay them as
// on-glasses cached draws.
private SurfaceCompositor.ScreenDraw[] desiredDraws = new SurfaceCompositor.ScreenDraw[0];
// (frame, reason) of the last "waiting to send" line, so a frame that
// stalls for seconds records one line per state change (see
// noteImageStallLocked). Send-loop thread only.
private int stallFrameId;
private String stallReason = "";
// Highest compositor sequence stored as the desired frame; composites that
// lost a store race to a newer one are discarded (their content is already
// included in the newer composite).
@@ -181,6 +194,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private final SurfaceCompositor compositor = new SurfaceCompositor();
// Phone-side model of the CFW's 64 KiB texture cache (modes 12/13/14).
// Reset whenever the image pipeline / EvenHub session is torn down: the
// firmware frees the cache with the fb lease, and after any resync the
// cheap safe assumption is an empty cache (glyphs re-upload lazily).
private final TextureCacheState textureCache = new TextureCacheState();
private boolean textureCacheSupported;
private boolean textureImagesSupported;
private boolean fwTextSupported;
private final ArrayDeque<OutboundMessage> pendingMessages = new ArrayDeque<>();
private final ArrayDeque<OutboundMessage> inFlightMessages = new ArrayDeque<>();
private OutboundMessage prewrittenMessage;
@@ -226,7 +248,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
public void disconnect() {
releaseFaceclawFramebufferLease();
/* On the normal path DashboardController already sent mode 11 after
* quiescing its producers. Also cover direct/early close callers here;
* a successful cleanup must remain the final BLE message. Older CFWs
* fall back to the standalone framebuffer-lease release. */
if (!cfwCleanupDelivered && !sendCfwCleanup()) {
releaseFaceclawFramebufferLease();
}
Thread threadToJoin;
synchronized (lock) {
userDisconnectRequested = true;
@@ -623,10 +651,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
* frame landed while briefly hidden would stay blank until its next repaint.
*/
public void setSurfaceVisible(String id, boolean visible) {
// Its own frame: this recomposite is a real screen update with real
// latency, and without one it would show up in other frames' logs only
// as an anonymous "superseded by frame#0".
int frameId = FrameTimings.getInstance().startFrame(
"compositor:visible " + id + "=" + visible);
compositor.setSurfaceVisible(id, visible);
SurfaceCompositor.Composite composite = compositor.composite();
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
storeDesiredComposite(composite, packed, 0, 0);
storeDesiredComposite(composite, packed, 0, frameId);
}
/**
@@ -635,10 +668,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
* unblanking restores the previous screen content without repaints.
*/
public void setScreenBlanked(boolean blanked) {
int frameId = FrameTimings.getInstance().startFrame(
"compositor:" + (blanked ? "blank" : "unblank"));
compositor.setBlanked(blanked);
SurfaceCompositor.Composite composite = compositor.composite();
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
storeDesiredComposite(composite, packed, 0, 0);
storeDesiredComposite(composite, packed, 0, frameId);
}
/**
@@ -674,11 +709,37 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
String contentFingerprint,
int paintMs,
int frameId
) {
submitSurfaceFrame(pixels8bpp, surfaceId, rectX, rectY, rectWidth, rectHeight,
contentFingerprint, paintMs, frameId, null);
}
/**
* As above, with the frame's glyph draws (see SurfaceCompositor's glyph
* overload for the buffer format): the surface's full text content as
* structured draws, letting the texture-cache planner ship glyphs as
* on-glasses cached draws instead of pixels. Null when the submitter has
* no glyph metadata; the pixels alone remain fully correct.
*/
public void submitSurfaceFrame(
java.nio.ByteBuffer pixels8bpp,
String surfaceId,
int rectX,
int rectY,
int rectWidth,
int rectHeight,
String contentFingerprint,
int paintMs,
int frameId,
java.nio.ByteBuffer glyphs
) {
Log.i(TAG, "Received an updated frame for surface " + surfaceId);
FrameTimings.getInstance().log(frameId, "surface " + surfaceId + " updated rect="
+ rectWidth + "x" + rectHeight + "+" + rectX + "+" + rectY
+ (glyphs == null ? " (no glyph draws)" : ""));
FrameTimings.getInstance().spanStart(frameId, "composite");
SurfaceCompositor.Composite composite = compositor.applyAndComposite(
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint);
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs);
FrameTimings.getInstance().spanEnd(frameId, "composite");
// Pack the composited 8bpp buffer down to the headerless 4bpp frame
// format the wire planners consume; BMP framing is added later only on
@@ -707,6 +768,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
desiredFingerprint = composite.fingerprint;
desiredPaintMs = paintMs;
desiredFrameId = frameId;
desiredDraws = composite.draws;
}
}
if (stale) {
@@ -760,12 +822,110 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return sendShutdownInternal(exitMode, true);
}
/**
* Send CFW image-handler mode 11 after quiescing normal traffic. A successful
* return means the cleanup was ACKed and no later Faceclaw message should be
* emitted before closing BLE. Unsupported/older CFWs return false so callers
* can use the legacy shutdown-and-lease-release path.
*/
public boolean sendCfwCleanup() {
int magic;
synchronized (lock) {
if (cfwCleanupDelivered) {
return true;
}
if (!cfwCleanupSupported || !running || !sessionReady
|| shutdownRequested || !fixedLayoutCreated || !warmedUp) {
logLine("skip CFW cleanup; mode 11 unavailable or image path not ready");
return false;
}
/* Stop auto-renewals, heartbeats, image generation, and control
* retries, then discard everything that has not reached BLE yet. */
shutdownRequested = true;
lastCfwCleanupAckMagic = 0;
clearPendingMessagesLocked("CFW cleanup requested");
logLine("quiescing transport for CFW cleanup");
}
interruptibleSleep.interrupt();
/* WINDOW_SIZE can exceed one, so merely appending cleanup would allow it
* to overlap previously-written image fragments. Wait until all of those
* ACK or time out; shutdownRequested prevents the drive loop from adding
* any fresh automatic traffic meanwhile. */
long drainDeadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS;
synchronized (lock) {
while (running && sessionReady && !inFlightMessages.isEmpty()) {
long remaining = drainDeadline - SystemClock.elapsedRealtime();
if (remaining <= 0) break;
try {
lock.wait(Math.min(remaining, 100));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
if (!running || !sessionReady || !inFlightMessages.isEmpty()) {
shutdownRequested = false;
logLine("CFW cleanup could not drain prior traffic");
return false;
}
/* External producers are expected to be stopped by the caller, but
* clear once more at the barrier so cleanup is definitely last. */
clearPendingMessagesLocked("CFW cleanup barrier");
OutboundMessage message = messageBuilder.cfwCleanup(
DASHBOARD_TILE,
nextMapSessionId(),
connectionOptions.sendImagesToLeft
);
magic = message.magic;
message.onAck = () -> {
lastCfwCleanupAckMagic = message.magic;
cfwCleanupDelivered = true;
compassMaybeOn = false;
faceclawWakeLeaseEnabled = false;
logLine("CFW cleanup completed");
};
message.onTimeout = () -> logLine("CFW cleanup ack timeout");
pendingMessages.addLast(message);
logLine("queue CFW cleanup");
}
interruptibleSleep.interrupt();
long deadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS;
synchronized (lock) {
while (running
&& sessionReady
&& lastCfwCleanupAckMagic != magic
&& hasPendingOrInflightMagicLocked(magic)) {
long remaining = deadline - SystemClock.elapsedRealtime();
if (remaining <= 0) break;
try {
lock.wait(Math.min(remaining, 100));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
boolean acked = lastCfwCleanupAckMagic == magic;
if (!acked) shutdownRequested = false;
return acked;
}
}
/**
* End the EvenHub page while retaining both arm GATT connections and all
* notification subscriptions. A missed ACK is intentionally non-fatal:
* reconnecting Bluetooth here would defeat the power-saving mode.
*/
public boolean suspendEvenHubSession() {
// Ending the plugin task releases the fb lease, which frees the
// on-glasses texture cache; forget it phone-side either way (a lost
// ack may still have taken effect).
synchronized (lock) {
textureCache.reset();
}
if (sendShutdownInternal(0, false)) {
return true;
}
@@ -1253,6 +1413,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
lastAudioControlAckMagic = 0;
audioCaptureActive = false;
faceclawWakePendingNonce = -1;
cfwCleanupDelivered = false;
lastCfwCleanupAckMagic = 0;
lastFaceclawWakeLeaseQueuedAtMs = 0;
lastFaceclawFramebufferLeaseQueuedAtMs = 0;
enqueueFaceclawFramebufferControlLocked(
@@ -1456,6 +1618,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
maybeFinishNoChangeDesiredFrame();
synchronized (lock) {
if (cfwCleanupDelivered) {
/* Successful mode 11 must be the last Faceclaw write. Drop
* anything a late external producer attempted to enqueue
* while DashboardController was closing the transport. */
clearPendingMessagesLocked("after CFW cleanup");
return 250;
}
if (sessionReady
&& now - lastFaceclawFramebufferLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS
&& !hasPendingOrInflightKindLocked("framebuffer-lease-control")) {
@@ -1548,10 +1717,19 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
// A frame ready to send right now: don't inject a fresh
// heartbeat in front of it (the image's own ack resets the
// firmware heartbeat timer, so the heartbeat is redundant).
// "Ready" covers both a fresh composite still to be planned
// and a planned image already queued but not yet written --
// enqueueing sets lastEnqueuedFingerprint, so testing only
// the desired fingerprint left the queued-but-unwritten
// window unprotected and a heartbeat that came due there
// cost the frame a full ack round trip (measured 124ms on
// frame#232 of the 2026-08-20 02:27 capture).
boolean imageWaiting = !shutdownRequested && fixedLayoutCreated && warmedUp
&& !hasPendingOrInflightKindLocked("heartbeat")
&& now >= imageRetryAfterMs
&& !getDesiredFingerprint().equals(lastEnqueuedFingerprint);
&& (hasPendingImageLocked()
|| !getDesiredFingerprint().equals(lastEnqueuedFingerprint));
noteImageStallLocked(now, windowHasRoom);
if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) {
return ConnectionOptions.IDLE_SLEEP_MS;
}
@@ -2044,12 +2222,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
int height;
int paintMs;
int frameId;
SurfaceCompositor.ScreenDraw[] draws;
synchronized (desiredTilesLock) {
packed = desiredPacked;
width = desiredWidth;
height = desiredHeight;
paintMs = desiredPaintMs;
frameId = desiredFrameId;
draws = desiredDraws;
desiredFrameId = 0;
}
if (packed == null) {
@@ -2062,6 +2242,48 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return;
}
// Texture-cache path: ship text as cached-glyph draws, punching their
// ink out of the baked deltas. Uploads (mode 12) ride ahead of the
// image message on the ordered transport. Falls through to the plain
// paths whenever the planner has nothing to draw.
if (textureCacheSupported && connectionOptions.TEXTURE_CACHE_FRAMES
&& draws != null && draws.length > 0 && packed.length > 0) {
byte[] deltaBase = (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0
&& lastEnqueuedWidth == width && lastEnqueuedHeight == height)
? lastEnqueuedPacked : null;
FrameTimings.getInstance().spanStart(frameId, "texture-plan");
TexturePlanner.Result tex = TexturePlanner.plan(
deltaBase, packed, width, height, draws, textureCache,
nextImageFrameId,
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS,
textureImagesSupported, fwTextSupported);
if (tex != null) {
nextImageFrameId = tex.nextFid;
for (byte[] upload : tex.uploads) {
enqueueTextureUploadLocked(upload);
}
BleImageOptimizer.TileImagePlan plan = new BleImageOptimizer.TileImagePlan(
0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), tex.payload);
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
FrameTimings.getInstance().spanEnd(frameId, "texture-plan");
String texLog = "texture update " + (tex.fullFrame ? "full" : ("rects=" + tex.rectCount))
+ " glyphs=" + tex.drawnGlyphs + " runs=" + tex.runCount
+ " images=" + tex.drawnImages
+ " fw=" + tex.fwGlyphs + "/" + tex.fwRuns
+ " baked=" + tex.bakedCandidates
+ (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "")
+ " cache=" + textureCache.usedBytes() + "B"
+ " payload=" + tex.payload.length + "B"
+ " (rects " + tex.rectsMs + "ms, match " + tex.matchMs
+ "ms, cache " + tex.cacheMs + "ms, punch " + tex.punchMs
+ "ms, encode " + tex.encodeMs + "ms)";
FrameTimings.getInstance().log(frameId, texLog);
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
return;
}
FrameTimings.getInstance().spanEnd(frameId, "texture-plan");
}
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
// Incremental (mode 3 bounding box) update against the last ENQUEUED frame
// (the base the firmware shadow will hold when this update is applied).
@@ -2110,7 +2332,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (incrementalLog != null) {
FrameTimings.getInstance().log(frameId, incrementalLog);
}
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
}
/** Shared tail of enqueueDesiredImageLocked: enqueue fragments, advance the delta base, log. */
private void finishEnqueueDesiredImageLocked(
BleImageOptimizer.TileImagePlan plan, String fingerprint, int paintMs, int frameId) {
int updateId = nextImageUpdateId++;
int messageCount = plan.fragments.size();
imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId));
@@ -2133,6 +2360,29 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
+ " messages=" + messageCount);
}
/**
* Enqueue one mode-12 texture-cache upload ahead of the image message that
* references its glyphs (the transport is FIFO, so no ack round trip is
* needed before use). A timeout means the on-glasses cache state is
* unknown; forget everything phone-side (glyphs re-upload lazily) and let
* the accompanying image update's own timeout drive the frame resync.
*/
private void enqueueTextureUploadLocked(byte[] payload) {
OutboundMessage message = messageBuilder.imagePayload(
"texcache",
DASHBOARD_TILE,
nextMapSessionId(),
payload,
"texture upload " + payload.length + "B",
connectionOptions.sendImagesToLeft);
message.onTimeout = () -> {
textureCache.reset();
logLine("texture upload ack timeout; texture cache state reset");
};
pendingMessages.addLast(message);
logLine("queue " + message.label);
}
private void enqueueImageFragmentLocked(
BleImageOptimizer.TileImagePlan plan,
BleProtocol.ImageFragment fragment,
@@ -2221,6 +2471,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload);
if (firmwareInfo != null) {
cfwCleanupSupported = hasCapability(firmwareInfo.capabilities, "cleanup11");
textureCacheSupported = hasCapability(firmwareInfo.capabilities, "texcache12")
&& hasCapability(firmwareInfo.capabilities, "texstr14");
textureImagesSupported = hasCapability(firmwareInfo.capabilities, "teximg13");
fwTextSupported = hasCapability(firmwareInfo.capabilities, "font15");
emitFirmwareInfo(firmwareInfo);
}
};
@@ -2283,6 +2538,117 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
: "Glasses charging.";
}
/**
* Record, into the frame that is waiting, why it did not go out on this
* pass of the send loop. Without this the export shows a bare multi-second
* jump between "image submitted as desired frame" and the first BLE
* packet, with no hint whether we were blocked on warmup, a heartbeat, the
* BLE window, or another message queued ahead. Deduped on (frame, reason),
* so a frame stalled for seconds gets one line per state change rather
* than one per loop pass.
*/
private void noteImageStallLocked(long now, boolean windowHasRoom) {
int frameId;
synchronized (desiredTilesLock) {
frameId = desiredFrameId;
}
String reason;
if (frameId != 0 && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) {
reason = describeEnqueueBlockerLocked(now, windowHasRoom);
} else {
// Nothing waiting to be planned; an already-planned image may still
// be queued behind other traffic.
OutboundMessage queuedImage = firstPendingImageLocked();
frameId = queuedImage == null ? 0 : imageUpdateFrameIdLocked(queuedImage.imageUpdateId);
reason = queuedImage == null ? null : describeWriteBlockerLocked(now, windowHasRoom, queuedImage);
}
if (frameId == 0 || reason == null) {
stallFrameId = 0;
stallReason = "";
return;
}
if (frameId == stallFrameId && reason.equals(stallReason)) {
return;
}
stallFrameId = frameId;
stallReason = reason;
FrameTimings.getInstance().log(frameId, "waiting to send: " + reason);
}
/** Why the desired composite has not been turned into wire messages yet, or null. */
private String describeEnqueueBlockerLocked(long now, boolean windowHasRoom) {
if (shutdownRequested) {
return "shutdown requested";
}
if (!sessionReady) {
return "BLE session not ready";
}
if (!fixedLayoutCreated) {
return "display layout not created yet";
}
if (!warmedUp) {
return "waiting for the warmup frame";
}
if (now < imageRetryAfterMs) {
return "image retry backoff (" + (imageRetryAfterMs - now) + "ms left)";
}
if (hasPendingImageLocked()) {
return "an earlier image is still queued";
}
if (!windowHasRoom) {
return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)";
}
if (hasPendingOrInflightKindLocked("heartbeat")) {
return "heartbeat in flight";
}
if (!pendingMessages.isEmpty()) {
return pendingMessages.size() + " message(s) queued ahead, next "
+ pendingMessages.peekFirst().label;
}
return null;
}
/** Why a planned image message has not been written to BLE yet, or null. */
private String describeWriteBlockerLocked(long now, boolean windowHasRoom, OutboundMessage queuedImage) {
if (!sessionReady) {
return "BLE session not ready";
}
if (!windowHasRoom) {
return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)";
}
if (hasPendingOrInflightKindLocked("heartbeat")) {
return "heartbeat in flight";
}
// handleHeartbeat is a barrier: while one is due it holds back every
// other write, so a frame queued at the wrong moment waits a heartbeat
// round trip. Reported explicitly because it is otherwise invisible --
// heartbeats belong to no frame.
long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs;
if (warmedUp && fixedLayoutCreated && !shutdownRequested
&& heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS) {
return "heartbeat due (" + heartbeatElapsedMs + "ms since the last one acked)";
}
OutboundMessage head = pendingMessages.peekFirst();
if (head != null && head != queuedImage) {
return "queued behind " + head.label;
}
return null;
}
private OutboundMessage firstPendingImageLocked() {
for (OutboundMessage message : pendingMessages) {
if (message.imageUpdateId > 0) {
return message;
}
}
return null;
}
private int imageUpdateFrameIdLocked(int imageUpdateId) {
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(imageUpdateId);
return stats == null ? 0 : stats.frameId;
}
private void finishDesiredFrameLocked(String outcome) {
int frameIdToFinish;
synchronized (desiredTilesLock) {
@@ -2509,6 +2875,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
// image is a full keyframe rather than a delta onto a stale base.
lastEnqueuedPacked = new byte[0];
lastEnqueuedFingerprint = "";
// Queued texture uploads (if any) were dropped with the rest, and the
// session churn behind a full clear may have freed the on-glasses
// cache; forget it phone-side so glyphs re-upload lazily.
textureCache.reset();
}
/**
@@ -2539,6 +2909,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
clearInFlightMessagesLocked(reason);
lastEnqueuedPacked = new byte[0];
lastEnqueuedFingerprint = "";
textureCache.reset();
}
/** Any image update whose fragments are still queued (not yet sent). */
@@ -2656,6 +3027,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
faceclawWakePendingNonce = -1;
lastFaceclawWakeLeaseQueuedAtMs = 0;
faceclawWakeControlSentCount = 0;
cfwCleanupDelivered = false;
lastCfwCleanupAckMagic = 0;
wearState = -1;
displayedFingerprint = "";
// Deliberately not clearing silentMode: it is a property of the glasses,
@@ -2663,6 +3036,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
// the very cause of the session teardown that got us here.
}
private static boolean hasCapability(String capabilities, String token) {
if (capabilities == null || token == null || token.isEmpty()) return false;
for (String capability : capabilities.trim().split("\\s+")) {
if (token.equals(capability)) return true;
}
return false;
}
private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId) {
final FaceclawBleCommunicatorListener current = listener;
if (current == null) {
@@ -0,0 +1,34 @@
package com.faceclaw.app;
import android.os.Handler;
import android.os.Looper;
import android.webkit.JavascriptInterface;
/**
* The object injected into a hosted EvenHub app's WebView as
* window.__faceclawEvenHub. The document-start shim (served inline by
* FaceclawEvenHubWebViewClient) wraps it in a promise-returning
* window.flutter_inappwebview.callHandler, which is what the EvenHub SDK
* actually talks to.
*
* JavascriptInterface methods run on a WebView-internal thread; everything
* is bounced to the main thread before touching the NativeScript listener.
*/
public class FaceclawEvenHubJsBridge {
private final FaceclawEvenHubListener listener;
private final Handler mainHandler = new Handler(Looper.getMainLooper());
public FaceclawEvenHubJsBridge(FaceclawEvenHubListener listener) {
this.listener = listener;
}
@JavascriptInterface
public void postMessage(final String handlerName, final String argsJson, final int callId) {
mainHandler.post(new Runnable() {
@Override
public void run() {
listener.onEvenAppMessage(handlerName, argsJson, callId);
}
});
}
}
@@ -0,0 +1,18 @@
package com.faceclaw.app;
/**
* Callbacks from a hosted EvenHub app's WebView to the TypeScript host.
* All methods are delivered on the Android main thread (NativeScript's JS
* thread), regardless of which WebView-internal thread produced them.
*/
public interface FaceclawEvenHubListener {
/**
* The app invoked window.flutter_inappwebview.callHandler(handlerName, ...args).
* argsJson is the JSON-encoded argument array; callId identifies the
* promise to resolve via window.__fcResolve(callId, ok, value).
*/
void onEvenAppMessage(String handlerName, String argsJson, int callId);
/** The WebView finished loading the app's page. */
void onPageFinished(String url);
}
@@ -0,0 +1,40 @@
package com.faceclaw.app;
import android.content.Context;
import android.view.View;
import android.webkit.WebView;
/**
* A WebView for hosting an EvenHub app that keeps rendering and running JS even
* when the whole Faceclaw activity is backgrounded (phone on the home screen or
* another app).
*
* The problem: Chromium ties a WebView's page-visibility to its window's
* visibility. When the host activity stops, every view gets
* onWindowVisibilityChanged(GONE); Chromium then marks the page hidden and
* freezes/throttles the renderer. A frozen renderer doesn't execute
* evaluateJavascript, so the host timer/rAF ticks AND pushed input events queue
* up and only run once the app is foregrounded again — exactly the "Hub apps
* freeze in the background" symptom (also seen in early official-app versions).
*
* The occluded-overlay trick in FaceclawEvenHubWebViewHost keeps the VIEW
* visible while the activity is foreground, but can't help once the WINDOW goes
* away. So we lie about window visibility: this WebView always tells Chromium
* the window is VISIBLE, so the page never goes hidden and the renderer keeps
* running in the background. We drive the glasses over BLE from the foreground
* service regardless of the phone's screen, so "always rendering" is what we
* actually want; there is no on-phone cost while the app is unseen.
*/
public class FaceclawEvenHubWebView extends WebView {
public FaceclawEvenHubWebView(Context context) {
super(context);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
// Never propagate a hidden window: keep Chromium's page-visibility
// "visible" so the renderer isn't frozen when Faceclaw backgrounds.
super.onWindowVisibilityChanged(View.VISIBLE);
}
}
@@ -0,0 +1,147 @@
package com.faceclaw.app;
import android.os.Handler;
import android.os.Looper;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.Uri;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
/**
* Serves an unpacked EvenHub app's files to its WebView from a fake per-app
* https origin, entirely offline. HTML responses get the host bridge shim
* injected at the top of the document, guaranteeing
* window.flutter_inappwebview exists before any app JS runs (apps race
* waitForEvenAppBridge against 4-6s timeouts and silently fall back to demo
* modes if the handler appears late).
*
* shouldInterceptRequest runs on a WebView-internal thread; it only touches
* files, never the NativeScript runtime. Requests to other hosts fall
* through to the network (the manifest whitelist is not yet enforced).
*/
public class FaceclawEvenHubWebViewClient extends WebViewClient {
private static final String TAG = "FaceclawEvenHub";
private final String rootDir;
private final String host;
private final String injectScript;
private final FaceclawEvenHubListener listener;
private final Handler mainHandler = new Handler(Looper.getMainLooper());
public FaceclawEvenHubWebViewClient(
String rootDir, String host, String injectScript, FaceclawEvenHubListener listener) {
this.rootDir = rootDir;
this.host = host;
this.injectScript = injectScript;
this.listener = listener;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
Uri url = request.getUrl();
if (url == null || !host.equals(url.getHost())) {
return null; // External hosts: normal network handling.
}
String path = url.getPath();
if (path == null || path.isEmpty() || path.equals("/")) {
path = "/index.html";
}
try {
File root = new File(rootDir).getCanonicalFile();
File target = new File(root, path.substring(1)).getCanonicalFile();
if (!target.getPath().startsWith(root.getPath()) || !target.isFile()) {
Log.w(TAG, "404 " + path);
return new WebResourceResponse(
"text/plain", "utf-8",
new ByteArrayInputStream("not found".getBytes(StandardCharsets.UTF_8)));
}
String mime = mimeTypeFor(target.getName());
if (mime.equals("text/html")) {
return new WebResourceResponse(mime, "utf-8", new ByteArrayInputStream(injectIntoHtml(target)));
}
InputStream stream = new FileInputStream(target);
return new WebResourceResponse(mime, null, stream);
} catch (IOException e) {
Log.e(TAG, "serve failed for " + path, e);
return new WebResourceResponse(
"text/plain", "utf-8",
new ByteArrayInputStream("error".getBytes(StandardCharsets.UTF_8)));
}
}
@Override
public void onPageFinished(WebView view, final String url) {
mainHandler.post(new Runnable() {
@Override
public void run() {
listener.onPageFinished(url);
}
});
}
private byte[] injectIntoHtml(File file) throws IOException {
byte[] raw = readAll(file);
String html = new String(raw, StandardCharsets.UTF_8);
String tag = "<script>" + injectScript + "</script>";
// After <head...> if present, else before everything.
int headIndex = indexOfIgnoreCase(html, "<head");
if (headIndex >= 0) {
int close = html.indexOf('>', headIndex);
if (close >= 0) {
return (html.substring(0, close + 1) + tag + html.substring(close + 1))
.getBytes(StandardCharsets.UTF_8);
}
}
return (tag + html).getBytes(StandardCharsets.UTF_8);
}
private static int indexOfIgnoreCase(String haystack, String needle) {
return haystack.toLowerCase(Locale.ROOT).indexOf(needle);
}
private static byte[] readAll(File file) throws IOException {
FileInputStream in = new FileInputStream(file);
try {
byte[] data = new byte[(int) file.length()];
int off = 0;
while (off < data.length) {
int n = in.read(data, off, data.length - off);
if (n < 0) throw new IOException("short read: " + file);
off += n;
}
return data;
} finally {
in.close();
}
}
private static String mimeTypeFor(String name) {
String lower = name.toLowerCase(Locale.ROOT);
if (lower.endsWith(".html") || lower.endsWith(".htm")) return "text/html";
if (lower.endsWith(".js") || lower.endsWith(".mjs")) return "application/javascript";
if (lower.endsWith(".css")) return "text/css";
if (lower.endsWith(".json")) return "application/json";
if (lower.endsWith(".png")) return "image/png";
if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) return "image/jpeg";
if (lower.endsWith(".gif")) return "image/gif";
if (lower.endsWith(".svg")) return "image/svg+xml";
if (lower.endsWith(".webp")) return "image/webp";
if (lower.endsWith(".ico")) return "image/x-icon";
if (lower.endsWith(".wasm")) return "application/wasm";
if (lower.endsWith(".woff")) return "font/woff";
if (lower.endsWith(".woff2")) return "font/woff2";
if (lower.endsWith(".ttf")) return "font/ttf";
if (lower.endsWith(".txt") || lower.endsWith(".map")) return "text/plain";
return "application/octet-stream";
}
}
@@ -0,0 +1,157 @@
package com.faceclaw.app;
import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.List;
/**
* Keeps EvenHub app WebViews alive and rendering while the phone shows the
* Faceclaw dashboard instead of the app.
*
* The problem: an EvenHub app only paints (canvas / requestAnimationFrame) and
* runs un-throttled timers while its WebView is attached to the window and
* VISIBLE. Faceclaw is glasses-first — the phone normally shows its own UI, not
* the app — so the app's WebView must render off-screen.
*
* The trick: a single full-screen overlay FrameLayout is inserted as the FIRST
* child of the activity's content view, i.e. BEHIND the NativeScript UI. Every
* app WebView lives in it, full-size and VISIBLE, but occluded by the opaque
* dashboard on top. Chromium doesn't stop rendering a view merely because a
* sibling covers it (only VISIBILITY flags / detachment / zero-size do that),
* so the apps keep driving their glasses windows while unseen. Touches go to
* the NativeScript UI on top.
*
* To show one app's UI on the phone, the overlay is raised to the front (and
* the chosen WebView to the top of the overlay); hiding sends it back behind.
* All view work happens on the main thread (callers are on the NS/JS thread).
*
* Timer keep-alive: when the phone screen turns off, Chromium heavily throttles
* the page's own setTimeout/setInterval (intensive background throttling clamps
* them to ~1/sec), so timer-driven apps (e.g. snake's game loop) slow to a
* crawl even though the renderer is still alive. Host-initiated
* evaluateJavascript is NOT subject to that throttle, so we drive the app's
* timers ourselves: a document-start shim replaces setTimeout/setInterval (and
* requestAnimationFrame) with JS queues fired by window.__fcTimerTick() /
* __fcRafTick(), and this host ticks those on the main thread at a fixed rate
* regardless of screen state. The main Looper keeps running under the
* foreground service, so the tick survives the screen turning off. (Matches the
* official Even app's approach.)
*
* Background keep-alive: the occluded-overlay trick only works while the
* activity is foreground. When Faceclaw itself is backgrounded, the window goes
* invisible and Chromium freezes the renderer — the timer/rAF ticks and pushed
* input events then queue up and only run once foregrounded again. Two things
* prevent that: the WebViews are {@link FaceclawEvenHubWebView}, which lies to
* Chromium about window visibility so the page never goes hidden; and each is
* pinned to IMPORTANT renderer priority (not waived when not visible) with
* timers/onResume asserted on attach. The main Looper keeps ticking under the
* foreground service, so apps keep driving their glasses windows in the
* background.
*/
public class FaceclawEvenHubWebViewHost {
private static FaceclawEvenHubWebViewHost instance;
/** How often the host fires the JS timer queue. 60Hz keeps games smooth. */
private static final long TICK_MS = 16;
private FrameLayout overlay;
private boolean shown = false;
private final Handler mainHandler = new Handler(Looper.getMainLooper());
private final List<WebView> webViews = new ArrayList<>();
private boolean ticking = false;
private final Runnable ticker = new Runnable() {
@Override
public void run() {
for (int i = 0; i < webViews.size(); i++) {
webViews.get(i).evaluateJavascript(
"window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null);
}
if (ticking) mainHandler.postDelayed(this, TICK_MS);
}
};
public static synchronized FaceclawEvenHubWebViewHost getInstance() {
if (instance == null) instance = new FaceclawEvenHubWebViewHost();
return instance;
}
private FrameLayout ensureOverlay(Activity activity) {
if (overlay != null) return overlay;
ViewGroup content = activity.findViewById(android.R.id.content);
overlay = new FrameLayout(activity);
// index 0 = behind the NativeScript content view.
content.addView(overlay, 0, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return overlay;
}
/** Add a WebView to the host, full-size and rendering, hidden behind the UI. */
public void attach(Activity activity, WebView web) {
FrameLayout o = ensureOverlay(activity);
web.setVisibility(View.VISIBLE);
if (web.getParent() == null) {
o.addView(web, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
// resumeTimers() is process-global; make sure nothing left timers paused.
// onResume() undoes any per-WebView pause. Together with
// FaceclawEvenHubWebView faking window visibility, this keeps the page
// running JS while Faceclaw is backgrounded.
web.resumeTimers();
web.onResume();
// Keep the renderer process at IMPORTANT priority even when the WebView
// isn't visible (waivedWhenNotVisible=false), so Android doesn't
// deprioritize/kill it in the background.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false);
}
if (!webViews.contains(web)) webViews.add(web);
if (!ticking) {
ticking = true;
mainHandler.postDelayed(ticker, TICK_MS);
}
}
/** Bring an app's WebView to the front so it is visible on the phone. */
public void showOnPhone(WebView web) {
if (overlay == null) return;
web.bringToFront();
overlay.bringToFront();
shown = true;
}
/** Send the overlay back behind the NativeScript UI. */
public void hideOnPhone() {
shown = false;
if (overlay == null) return;
ViewGroup content = (ViewGroup) overlay.getParent();
if (content != null) {
content.removeView(overlay);
content.addView(overlay, 0);
}
}
public boolean isShown() {
return shown;
}
/** Remove and destroy a WebView (its app is closing). */
public void detach(WebView web) {
webViews.remove(web);
if (webViews.isEmpty() && ticking) {
ticking = false;
mainHandler.removeCallbacks(ticker);
}
if (overlay != null) overlay.removeView(web);
web.destroy();
}
}
@@ -67,8 +67,10 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener {
// END ack statuses that mean "component accepted": SUCCESS, UPDATING, SYS_RESTART.
private static final int[] END_OK = new int[] {0, 8, 9};
// Firmware layout expectations (mirrors g2flash.py).
private static final int EXPECTED_SEGMENTS = 5;
// Firmware layout expectations (mirrors g2flash.py). Firmware 2.2.4 has 5
// components; 2.2.6 has 6.
private static final int MIN_SEGMENTS = 5;
private static final int MAX_SEGMENTS = 6;
private static final String REQUIRED_SEGMENT = "ota/s200_firmware_ota.bin";
private static final long APP_LOAD_ADDR = 0x00438000L;
private static final long APP_MAX_END = 0x007F0000L;
@@ -444,8 +446,9 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener {
private List<Segment> validate(byte[] img) {
List<Segment> segs = parseSegments(img);
if (segs.size() != EXPECTED_SEGMENTS) {
throw new IllegalStateException("expected " + EXPECTED_SEGMENTS + " components, found " + segs.size());
if (segs.size() < MIN_SEGMENTS || segs.size() > MAX_SEGMENTS) {
throw new IllegalStateException(
"expected " + MIN_SEGMENTS + "-" + MAX_SEGMENTS + " components, found " + segs.size());
}
Segment main = null;
for (Segment s : segs) {
@@ -175,6 +175,136 @@ public final class FontFileRenderer {
}
/**
<<<<<<< HEAD
||||||| d7d12af
=======
* Render one codepoint as a tight-ink-box antialiased glyph cell for the
* texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures
* are disabled so a per-codepoint raster plus pairwise kerning (measured
* via measureTextExact) fully describes a line. Little-endian packet:
* [advance u16, 26.6 fixed point]
* [bearingX s16] ink left relative to the pen position
* [inkTop s16] ink top relative to the line top (ascent above baseline)
* [width u16][height u16]
* [width*height coverage bytes, gamma-mapped]
* An ink-free glyph (space) has width = height = 0 but a valid advance.
* Empty array when the font cannot be loaded or the render fails.
*/
public static byte[] renderGlyphCell(String path, float sizePx, int codePoint, double gamma) {
Typeface typeface = loadTypeface(path);
if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) {
return new byte[0];
}
try {
Paint paint = glyphPaint(typeface, sizePx);
String text = new String(Character.toChars(codePoint));
float advance = paint.measureText(text);
int advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f)));
android.graphics.Rect bounds = new android.graphics.Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
if (bounds.isEmpty()) {
return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null);
}
// getTextBounds can be off by a hair on AA edges; render with
// padding, then trim to the actually painted box.
int pad = 2;
int bw = bounds.width() + 2 * pad;
int bh = bounds.height() + 2 * pad;
if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) {
return new byte[0];
}
int penX = pad - bounds.left;
int baselineY = pad - bounds.top;
Bitmap bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, penX, baselineY, paint);
byte[] pixels = alpha8Pixels(bitmap, bw, bh);
bitmap.recycle();
int x0 = bw, x1 = -1, y0 = bh, y1 = -1;
for (int y = 0; y < bh; y++) {
int row = y * bw;
for (int x = 0; x < bw; x++) {
if (pixels[row + x] != 0) {
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
}
}
if (x1 < x0) {
return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null);
}
int width = x1 - x0 + 1;
int height = y1 - y0 + 1;
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
int bearingX = x0 - penX;
int inkTop = (-fm.ascent) + (y0 - baselineY);
byte[] lut = gammaLut(gamma);
byte[] coverage = new byte[width * height];
for (int y = 0; y < height; y++) {
int src = (y0 + y) * bw + x0;
int dst = y * width;
for (int x = 0; x < width; x++) {
coverage[dst + x] = lut[pixels[src + x] & 0xff];
}
}
return glyphCellPacket(advanceFixed, bearingX, inkTop, width, height, coverage);
} catch (Exception | OutOfMemoryError e) {
Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e);
return new byte[0];
}
}
/**
* Exact (float) advance width of a text run, with the same paint setup as
* renderGlyphCell (subpixel advances, ligatures off), so pair kerning can
* be derived as measure(ab) - measure(a) - measure(b).
*/
public static double measureTextExact(String path, String text, float sizePx) {
Typeface typeface = loadTypeface(path);
if (typeface == null || text == null || sizePx <= 0) {
return 0;
}
return glyphPaint(typeface, sizePx).measureText(text);
}
/** Paint used by both the glyph-cell renderer and its measurements. */
private static Paint glyphPaint(Typeface typeface, float sizePx) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
paint.setTypeface(typeface);
paint.setTextSize(sizePx);
paint.setColor(Color.WHITE);
// Ligatures merge codepoints into one glyph, which the per-codepoint
// cache model cannot represent; kerning still applies.
paint.setFontFeatureSettings("'liga' off, 'clig' off");
return paint;
}
private static byte[] glyphCellPacket(
int advanceFixed, int bearingX, int inkTop, int width, int height, byte[] coverage) {
byte[] out = new byte[10 + (coverage == null ? 0 : coverage.length)];
out[0] = (byte) (advanceFixed & 0xff);
out[1] = (byte) ((advanceFixed >> 8) & 0xff);
out[2] = (byte) (bearingX & 0xff);
out[3] = (byte) ((bearingX >> 8) & 0xff);
out[4] = (byte) (inkTop & 0xff);
out[5] = (byte) ((inkTop >> 8) & 0xff);
out[6] = (byte) (width & 0xff);
out[7] = (byte) ((width >> 8) & 0xff);
out[8] = (byte) (height & 0xff);
out[9] = (byte) ((height >> 8) & 0xff);
if (coverage != null) {
System.arraycopy(coverage, 0, out, 10, coverage.length);
}
return out;
}
/**
>>>>>>> evenhub-compatibility
* Line metrics for a font at a pixel size, as "ascent descent lineGap"
* (integers, pixels). Empty string if the font cannot be loaded.
*/
@@ -14,8 +14,7 @@ import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -27,14 +26,21 @@ import java.util.Map;
* update has been fully transmitted to the glasses, is discarded unsent, or
* times out because nobody reported finishing it.
*
* All public methods are thread-safe and cheap enough to call from the BLE
* worker thread, the Android main thread, and the JS thread. Frame IDs are
* positive ints; 0 means "no frame" and is silently ignored everywhere, so
* callers do not need to null-check.
* Frames form a tree. One input event can fan out into several renders (the
* focused app's window plus the shell chrome, say), and each of those is its
* own frame linked back to the input frame that caused it via
* {@link #startFrame(String, int)}. The export nests descendants under their
* root and renders every line against the root's clock, so a single block
* shows the whole input-to-pixels story. Latency percentiles are measured over
* roots: root start to the first descendant that actually reached the glasses.
*
* Statistics (p50/p90/p99 of event-to-last-ack for sent frames) and full log
* lines for recent and slowest frames are periodically exported to
* getExternalFilesDir()/frame-timings.txt, retrievable via
* All public methods are thread-safe and cheap enough to call from the BLE
* worker thread, the Android main thread, the JS thread, and app worker
* threads. Frame IDs are positive ints; 0 means "no frame" and is silently
* ignored everywhere, so callers do not need to null-check.
*
* Statistics and full log lines for recent and slowest frames are periodically
* exported to getExternalFilesDir()/frame-timings.txt, retrievable via
* adb pull /sdcard/Android/data/<pkg>/files/frame-timings.txt
*/
public final class FrameTimings {
@@ -45,31 +51,66 @@ public final class FrameTimings {
private static final long FRAME_TIMEOUT_MS = 30_000;
private static final long EXPORT_INTERVAL_MS = 15_000;
private static final long SWEEP_INTERVAL_MS = 5_000;
private static final int RECENT_FRAMES_KEPT = 50;
private static final int SLOWEST_FRAMES_KEPT = 20;
private static final int RECENT_ROOTS_KEPT = 40;
private static final int SLOWEST_ROOTS_KEPT = 20;
private static final int SENT_DURATIONS_WINDOW = 512;
private static final int MAX_LINES_PER_FRAME = 200;
/**
* Frames retained for ID lookup. Well above the number any export can
* reference, so a late log/span/finish call always finds its frame; frames
* older than this are unreachable and get collected.
*/
private static final int FRAMES_RETAINED = 1024;
private static final String EXPORT_FILE_NAME = "frame-timings.txt";
public static FrameTimings getInstance() {
return INSTANCE;
}
private static final class Line {
final long atMs; // SystemClock.elapsedRealtime()
final String thread;
final String message;
Line(long atMs, String thread, String message) {
this.atMs = atMs;
this.thread = thread;
this.message = message;
}
}
private static final class Frame {
final int id;
final String reason;
final long startedAtMs; // SystemClock.elapsedRealtime()
final long startedWallClockMs;
final List<String> lines = new ArrayList<>();
final Map<String, Long> openSpans = new HashMap<>();
final Frame parent;
final List<Frame> children = new ArrayList<>(0);
final List<Line> lines = new ArrayList<>();
final Map<String, Long> openSpans = new LinkedHashMap<>();
String reason;
long finishedAtMs;
String outcome; // null while open; "sent", "discarded: ...", "timeout: ..."
/** Set on the root once some frame in its subtree reached the glasses. */
boolean subtreeSentRecorded;
Frame(int id, String reason, long startedAtMs, long startedWallClockMs) {
Frame(int id, String reason, long startedAtMs, long startedWallClockMs, Frame parent) {
this.id = id;
this.reason = reason;
this.startedAtMs = startedAtMs;
this.startedWallClockMs = startedWallClockMs;
this.parent = parent;
}
Frame root() {
Frame frame = this;
while (frame.parent != null) {
frame = frame.parent;
}
return frame;
}
boolean isOpen() {
return outcome == null;
}
long durationMs() {
@@ -79,13 +120,45 @@ public final class FrameTimings {
boolean wasSent() {
return outcome != null && outcome.startsWith("sent");
}
/** Last moment anything in this subtree happened, for whole-tree duration. */
long subtreeEndAtMs(long nowMs) {
long end = isOpen() ? nowMs : finishedAtMs;
for (Frame child : children) {
end = Math.max(end, child.subtreeEndAtMs(nowMs));
}
return end;
}
boolean subtreeHasOpenFrame() {
if (isOpen()) {
return true;
}
for (Frame child : children) {
if (child.subtreeHasOpenFrame()) {
return true;
}
}
return false;
}
}
private final Object lock = new Object();
private final Map<Integer, Frame> activeFrames = new HashMap<>();
private final ArrayDeque<Frame> recentFrames = new ArrayDeque<>();
private final List<Frame> slowestSentFrames = new ArrayList<>();
private final ArrayDeque<Long> sentDurations = new ArrayDeque<>();
/**
* Every frame we still retain, keyed by ID: open frames plus enough
* history that late calls and the export can still resolve one.
*/
private final LinkedHashMap<Integer, Frame> framesById =
new LinkedHashMap<Integer, Frame>(64, 0.75f, false) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Frame> eldest) {
return size() > FRAMES_RETAINED && !eldest.getValue().isOpen();
}
};
private final ArrayDeque<Frame> recentRoots = new ArrayDeque<>();
private final List<Frame> slowestRoots = new ArrayList<>();
private final ArrayDeque<Long> inputLatencies = new ArrayDeque<>();
private final ArrayDeque<Long> renderLatencies = new ArrayDeque<>();
private int nextFrameId = 1;
private long framesStarted;
private long framesSent;
@@ -114,22 +187,56 @@ public final class FrameTimings {
}
}
/** Begin a frame; reason is a short label like "input:sys-event:0". Returns the frame ID. */
/** Begin a root frame; reason is a short label like "input:sys-event type=0". */
public int startFrame(String reason) {
return startFrame(reason, 0);
}
/**
* Begin a frame caused by an existing one (parentFrameId; 0 for a root).
* The child is nested under its root in the export and its latency counts
* against the root, so an input event that fans out into an app render and
* a shell-chrome render reads as one timeline instead of three.
*/
public int startFrame(String reason, int parentFrameId) {
long now = SystemClock.elapsedRealtime();
Frame frame;
synchronized (lock) {
frame = new Frame(nextFrameId++, reason == null ? "" : reason, now, System.currentTimeMillis());
activeFrames.put(frame.id, frame);
Frame parent = framesById.get(parentFrameId);
frame = new Frame(nextFrameId++, reason == null ? "" : reason, now,
System.currentTimeMillis(), parent);
framesById.put(frame.id, frame);
framesStarted++;
if (parent != null) {
parent.children.add(frame);
addLineLocked(parent, now, "spawned frame#" + frame.id + " (" + frame.reason + ")");
}
}
return frame.id;
}
/**
* Append to a frame's label, for facts only known after it started (which
* app is in the foreground, which window a render is for). Shows up in the
* frame's header line, so the export is scannable without reading bodies.
*/
public void annotate(int frameId, String text) {
if (text == null || text.isEmpty()) {
return;
}
synchronized (lock) {
Frame frame = framesById.get(frameId);
if (frame == null) {
return;
}
frame.reason = frame.reason.isEmpty() ? text : frame.reason + " " + text;
}
}
public void log(int frameId, String message) {
long now = SystemClock.elapsedRealtime();
synchronized (lock) {
Frame frame = activeFrames.get(frameId);
Frame frame = openFrameLocked(frameId);
if (frame == null) {
return;
}
@@ -140,7 +247,7 @@ public final class FrameTimings {
public void spanStart(int frameId, String name) {
long now = SystemClock.elapsedRealtime();
synchronized (lock) {
Frame frame = activeFrames.get(frameId);
Frame frame = openFrameLocked(frameId);
if (frame == null) {
return;
}
@@ -152,7 +259,7 @@ public final class FrameTimings {
public void spanEnd(int frameId, String name) {
long now = SystemClock.elapsedRealtime();
synchronized (lock) {
Frame frame = activeFrames.get(frameId);
Frame frame = openFrameLocked(frameId);
if (frame == null) {
return;
}
@@ -174,7 +281,7 @@ public final class FrameTimings {
long now = SystemClock.elapsedRealtime();
Frame finished;
synchronized (lock) {
Frame frame = activeFrames.remove(frameId);
Frame frame = openFrameLocked(frameId);
if (frame == null) {
return;
}
@@ -188,73 +295,94 @@ public final class FrameTimings {
/** One-line stats summary, e.g. for showing in the phone UI. */
public String statsSummary() {
synchronized (lock) {
long[] percentiles = sentPercentilesLocked();
long[] input = percentilesLocked(inputLatencies);
return "frames started=" + framesStarted + " sent=" + framesSent
+ " discarded=" + framesDiscarded + " timeout=" + framesTimedOut
+ (percentiles == null
+ (input == null
? ""
: " | sent-latency p50=" + percentiles[0] + "ms p90=" + percentiles[1]
+ "ms p99=" + percentiles[2] + "ms max=" + percentiles[3] + "ms");
: " | input-to-display p50=" + input[0] + "ms p90=" + input[1]
+ "ms p99=" + input[2] + "ms max=" + input[3] + "ms");
}
}
// ---------------------------------------------------------------------
/** The frame with this ID if it exists and is still open, else null. */
private Frame openFrameLocked(int frameId) {
if (frameId <= 0) {
return null;
}
Frame frame = framesById.get(frameId);
return frame != null && frame.isOpen() ? frame : null;
}
private void addLineLocked(Frame frame, long now, String message) {
if (frame.lines.size() >= MAX_LINES_PER_FRAME) {
if (frame.lines.size() == MAX_LINES_PER_FRAME) {
frame.lines.add("... line cap reached");
frame.lines.add(new Line(now, Thread.currentThread().getName(), "... line cap reached"));
}
return;
}
frame.lines.add(String.format(Locale.US, "%+6dms [%s] %s",
now - frame.startedAtMs, Thread.currentThread().getName(), message));
frame.lines.add(new Line(now, Thread.currentThread().getName(), message));
}
private void finishFrameLocked(Frame frame, long now, String outcome) {
frame.finishedAtMs = now;
frame.outcome = outcome;
for (Map.Entry<String, Long> open : frame.openSpans.entrySet()) {
frame.lines.add(String.format(Locale.US, "%+6dms span %s never ended (started at +%dms)",
now - frame.startedAtMs, open.getKey(), open.getValue() - frame.startedAtMs));
frame.lines.add(new Line(now, Thread.currentThread().getName(),
"span " + open.getKey() + " never ended (started at +"
+ (open.getValue() - frame.startedAtMs) + "ms)"));
}
frame.openSpans.clear();
addLineLocked(frame, now, "finished: " + outcome);
Frame root = frame.root();
if (frame.wasSent()) {
framesSent++;
sentDurations.addLast(frame.durationMs());
while (sentDurations.size() > SENT_DURATIONS_WINDOW) {
sentDurations.removeFirst();
// The user-visible latency is input (or timer) to the first pixels
// that actually reached the glasses, wherever in the tree that was.
if (!root.subtreeSentRecorded) {
root.subtreeSentRecorded = true;
recordLatencyLocked(root, now - root.startedAtMs);
insertSlowestLocked(root);
}
insertSlowestLocked(frame);
} else if (outcome.startsWith("timeout")) {
framesTimedOut++;
} else {
framesDiscarded++;
}
recentFrames.addLast(frame);
while (recentFrames.size() > RECENT_FRAMES_KEPT) {
recentFrames.removeFirst();
if (root == frame) {
recentRoots.addLast(frame);
while (recentRoots.size() > RECENT_ROOTS_KEPT) {
recentRoots.removeFirst();
}
}
exportDirty = true;
}
private void insertSlowestLocked(Frame frame) {
slowestSentFrames.add(frame);
Collections.sort(slowestSentFrames, (a, b) -> Long.compare(b.durationMs(), a.durationMs()));
while (slowestSentFrames.size() > SLOWEST_FRAMES_KEPT) {
slowestSentFrames.remove(slowestSentFrames.size() - 1);
private void recordLatencyLocked(Frame root, long latencyMs) {
ArrayDeque<Long> bucket = root.reason.startsWith("input:") ? inputLatencies : renderLatencies;
bucket.addLast(latencyMs);
while (bucket.size() > SENT_DURATIONS_WINDOW) {
bucket.removeFirst();
}
}
/** {p50, p90, p99, max} over the rolling sent-duration window, or null if empty. */
private long[] sentPercentilesLocked() {
if (sentDurations.isEmpty()) {
private void insertSlowestLocked(Frame root) {
if (!slowestRoots.contains(root)) {
slowestRoots.add(root);
}
// Sorted and trimmed at export time, when every subtree duration is final.
}
/** {p50, p90, p99, max} over a rolling latency window, or null if empty. */
private long[] percentilesLocked(ArrayDeque<Long> window) {
if (window.isEmpty()) {
return null;
}
List<Long> sorted = new ArrayList<>(sentDurations);
List<Long> sorted = new ArrayList<>(window);
Collections.sort(sorted);
return new long[] {
percentileOfSorted(sorted, 50),
@@ -273,11 +401,8 @@ public final class FrameTimings {
long now = SystemClock.elapsedRealtime();
List<Frame> timedOut = new ArrayList<>();
synchronized (lock) {
Iterator<Map.Entry<Integer, Frame>> iterator = activeFrames.entrySet().iterator();
while (iterator.hasNext()) {
Frame frame = iterator.next().getValue();
if (now - frame.startedAtMs >= FRAME_TIMEOUT_MS) {
iterator.remove();
for (Frame frame : new ArrayList<>(framesById.values())) {
if (frame.isOpen() && now - frame.startedAtMs >= FRAME_TIMEOUT_MS) {
finishFrameLocked(frame, now, "timeout: never reported finished");
timedOut.add(frame);
}
@@ -340,45 +465,103 @@ public final class FrameTimings {
}
private String buildExportLocked() {
long now = SystemClock.elapsedRealtime();
SimpleDateFormat wallClockFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
StringBuilder out = new StringBuilder(64 * 1024);
out.append("FrameTimings export at ").append(wallClockFormat.format(new Date())).append('\n');
out.append(statsSummaryLocked()).append('\n');
out.append('\n');
out.append("Frames are trees: an input event's own frame is the root, and the renders it\n");
out.append("caused are indented under it with offsets measured from the root's start.\n");
out.append('\n');
out.append("=== slowest sent frames ===\n");
for (Frame frame : slowestSentFrames) {
appendFrameLocked(out, frame, wallClockFormat);
Collections.sort(slowestRoots, (a, b) ->
Long.compare(b.subtreeEndAtMs(now) - b.startedAtMs, a.subtreeEndAtMs(now) - a.startedAtMs));
while (slowestRoots.size() > SLOWEST_ROOTS_KEPT) {
slowestRoots.remove(slowestRoots.size() - 1);
}
out.append("=== slowest frames that reached the glasses ===\n");
for (Frame frame : slowestRoots) {
appendTreeLocked(out, frame, frame, wallClockFormat, now);
}
out.append("=== recent frames (oldest first) ===\n");
for (Frame frame : recentFrames) {
appendFrameLocked(out, frame, wallClockFormat);
for (Frame frame : recentRoots) {
appendTreeLocked(out, frame, frame, wallClockFormat, now);
}
return out.toString();
}
private String statsSummaryLocked() {
long[] percentiles = sentPercentilesLocked();
return "frames started=" + framesStarted + " sent=" + framesSent
+ " discarded=" + framesDiscarded + " timeout=" + framesTimedOut
+ " open=" + activeFrames.size()
+ (percentiles == null
? ""
: "\nsent-frame latency (last " + sentDurations.size() + "): p50=" + percentiles[0]
+ "ms p90=" + percentiles[1] + "ms p99=" + percentiles[2]
+ "ms max=" + percentiles[3] + "ms");
long[] input = percentilesLocked(inputLatencies);
long[] render = percentilesLocked(renderLatencies);
StringBuilder out = new StringBuilder();
out.append("frames started=").append(framesStarted).append(" sent=").append(framesSent)
.append(" discarded=").append(framesDiscarded).append(" timeout=").append(framesTimedOut)
.append(" open=").append(openFrameCountLocked());
appendPercentilesLocked(out, "input-to-display latency", inputLatencies.size(), input);
appendPercentilesLocked(out, "render-to-display latency", renderLatencies.size(), render);
return out.toString();
}
private void appendFrameLocked(StringBuilder out, Frame frame, SimpleDateFormat wallClockFormat) {
out.append("frame#").append(frame.id)
.append(" [").append(frame.reason).append(']')
.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs)))
.append(" duration ").append(frame.durationMs()).append("ms")
.append(" outcome ").append(frame.outcome)
.append('\n');
for (String line : frame.lines) {
out.append(" ").append(line).append('\n');
private void appendPercentilesLocked(StringBuilder out, String label, int count, long[] percentiles) {
if (percentiles == null) {
return;
}
out.append('\n').append(label).append(" (last ").append(count).append("): p50=")
.append(percentiles[0]).append("ms p90=").append(percentiles[1])
.append("ms p99=").append(percentiles[2]).append("ms max=").append(percentiles[3]).append("ms");
}
private int openFrameCountLocked() {
int open = 0;
for (Frame frame : framesById.values()) {
if (frame.isOpen()) {
open++;
}
}
return open;
}
/** Print a frame and its descendants, all timed against root's start. */
private void appendTreeLocked(
StringBuilder out, Frame frame, Frame root, SimpleDateFormat wallClockFormat, long now) {
int depth = 0;
for (Frame walk = frame; walk != root; walk = walk.parent) {
depth++;
}
String indent = repeat(" ", depth);
long startOffsetMs = frame.startedAtMs - root.startedAtMs;
out.append(indent).append("frame#").append(frame.id)
.append(" [").append(frame.reason).append(']');
if (frame == root) {
out.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs)));
long totalMs = frame.subtreeEndAtMs(now) - frame.startedAtMs;
out.append(" duration ").append(frame.durationMs()).append("ms");
if (!frame.children.isEmpty()) {
out.append(" (tree ").append(totalMs).append("ms")
.append(frame.subtreeHasOpenFrame() ? ", still open" : "").append(')');
}
} else {
out.append(" started +").append(startOffsetMs).append("ms")
.append(" duration ").append(frame.isOpen() ? "open" : (frame.durationMs() + "ms"));
}
out.append(" outcome ").append(frame.outcome == null ? "(open)" : frame.outcome).append('\n');
for (Line line : frame.lines) {
out.append(indent).append(String.format(Locale.US, " %+7dms [%s] %s",
line.atMs - root.startedAtMs, line.thread, line.message)).append('\n');
}
for (Frame child : frame.children) {
appendTreeLocked(out, child, root, wallClockFormat, now);
}
}
private static String repeat(String unit, int times) {
StringBuilder out = new StringBuilder(unit.length() * times);
for (int i = 0; i < times; i++) {
out.append(unit);
}
return out.toString();
}
}
@@ -0,0 +1,83 @@
package com.faceclaw.app;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
/**
* Process-wide registry of the glasses' builtin 20 px font's glyph rasters,
* fed from the TS side (EvenHubFont's extracted firmware font data). Unlike
* GlyphAtlas/ImageAtlas entries, these are never uploaded: the firmware
* already owns the font, and mode 15 draws it directly. The planner needs
* the rasters purely phone-side — for the "would this draw land correctly"
* check against the composite and for punching drawn ink out of delta rects.
*
* Placement model matches the firmware's mode-15 math: a glyph draws at
* (penX + ofsX, lineY + inkTop) where inkTop already folds in the root
* font's line height and baseline. The TS side only registers glyphs whose
* on-glasses rendering it predicts exactly (native-table glyphs; baseline-
* shifted fallback symbols stay baked).
*/
public final class FwGlyphAtlas {
private FwGlyphAtlas() {}
public static final class Entry {
/** Draw x = pen x + ofsX. */
public final int ofsX;
/** First raster row relative to the run's line-top y. */
public final int inkTop;
public final int boxW;
public final int boxH;
/** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */
final byte[] nibbles;
Entry(int ofsX, int inkTop, int boxW, int boxH, byte[] nibbles) {
this.ofsX = ofsX;
this.inkTop = inkTop;
this.boxW = boxW;
this.boxH = boxH;
this.nibbles = nibbles;
}
/** The 4bpp source value at (col, row); the draw skips 0 (transparent). */
public int nibbleAt(int col, int row) {
int b = nibbles[row * ((boxW + 1) >> 1) + (col >> 1)] & 0xff;
return (col & 1) != 0 ? (b & 0x0f) : (b >> 4);
}
}
private static final Object lock = new Object();
private static final Map<Integer, Entry> entries = new HashMap<>();
/**
* Register glyph rasters. Little-endian buffer of records:
* [cp u32][ofsX s16][inkTop s16][boxW u8][boxH u8]
* [boxH x ceil(boxW/2) nibble bytes]
* Re-registration of a codepoint is ignored (the builtin font is fixed).
*/
public static void register(ByteBuffer buffer) {
if (buffer == null) return;
ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN);
synchronized (lock) {
while (in.remaining() >= 10) {
int cp = in.getInt();
int ofsX = in.getShort();
int inkTop = in.getShort();
int boxW = in.get() & 0xff;
int boxH = in.get() & 0xff;
byte[] nibbles = new byte[((boxW + 1) >> 1) * boxH];
in.get(nibbles);
if (boxW > 0 && boxH > 0 && !entries.containsKey(cp)) {
entries.put(cp, new Entry(ofsX, inkTop, boxW, boxH, nibbles));
}
}
}
}
public static Entry get(int cp) {
synchronized (lock) {
return entries.get(cp);
}
}
}
@@ -0,0 +1,267 @@
package com.faceclaw.app;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* Process-wide registry of glyph rasters, fed from the TS side so glyph
* identity survives to the BLE encoder (see notes/texture-cache-display-list-
* design.md). A frame's glyph list references entries here by (fontId,
* encoding); the texture-cache planner uses the raster three ways:
*
* - as the ink mask for the "would this draw land correctly" check against
* the composited frame,
* - to punch the ink pixels out of the baked delta rect it replaces,
* - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded
* via mode 12 and drawn via mode 14 with a top-color LUT. 1bpp (BDF)
* glyphs store ink at 15 so the LUT maps them to exactly the requested
* level; AA (TTF) glyphs store true coverage nibbles the LUT scales.
*
* Fonts are identified by a stable string key (the embedded font name), NOT a
* per-JS-context counter: worker threads register independently and must agree
* on ids. Glyph rasters are immutable once registered; re-registration of the
* same (fontId, encoding) is ignored.
*
* Cell model: a cached glyph image is bbxWidth x cellHeight (the font's line
* height), with the ink rows at inkTop — so every glyph of a line is drawn at
* the same y (the line top), which is what lets one mode-14 string carry a
* whole run. The horizontal bearing (bbxX) is applied phone-side when
* computing the draw x. Vertical padding is nearly free: it RLE-encodes to a
* couple of tokens.
*/
public final class GlyphAtlas {
private GlyphAtlas() {}
public static final class Glyph {
/** Cached image width in pixels (the glyph's tight bbox width). */
public final int width;
/** Cached image height in pixels (the font's line height). */
public final int cellHeight;
/** First row of ink within the cell. */
public final int inkTop;
/** Number of ink rows. */
public final int inkHeight;
/** Horizontal bearing: draw x = pen x + bbxX. */
public final int bbxX;
/** 1bpp ink rows (inkHeight entries), bit (rowBitWidth-1-col) = ink; null for AA glyphs. */
final int[] rows;
final int rowBitWidth;
/** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */
final byte[] coverage;
/** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */
public final byte[] cachedBytes;
Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX,
int[] rows, byte[] coverage) {
this.width = width;
this.cellHeight = cellHeight;
this.inkTop = inkTop;
this.inkHeight = inkHeight;
this.bbxX = bbxX;
this.rows = rows;
this.coverage = coverage;
this.rowBitWidth = ((width + 7) >> 3) << 3;
this.cachedBytes = encodeCachedImage();
}
/**
* The cell pixel's 4bpp source value at (col, row), row cell-relative.
* 1bpp glyphs are stored at 15 so the draw-time LUT (source*top/15)
* maps them to exactly the requested top color; AA glyphs carry their
* true coverage level.
*/
public int nibbleAt(int col, int row) {
if (col < 0 || col >= width) return 0;
int inkRow = row - inkTop;
if (inkRow < 0 || inkRow >= inkHeight) return 0;
if (coverage != null) return coverage[inkRow * width + col];
return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0 ? 15 : 0;
}
/** Whether the cell pixel at (col, row) has any ink (row is cell-relative). */
public boolean inkAt(int col, int row) {
return nibbleAt(col, row) != 0;
}
/**
* The firmware cached-image encoding: [width][height][RLE tokens]
* covering exactly width*cellHeight pixels (no row padding), each
* pixel's stored color being its nibbleAt value.
*/
private byte[] encodeCachedImage() {
int total = width * cellHeight;
// Worst case one token per pixel (1 byte each) + 2 header bytes.
byte[] out = new byte[2 + total];
out[0] = (byte) width;
out[1] = (byte) cellHeight;
int o = 2;
int i = 0;
while (i < total) {
int color = nibbleAt(i % width, i / width);
int j = i + 1;
while (j < total && nibbleAt(j % width, j / width) == color) j++;
int run = j - i;
while (run > 0) {
int c = Math.min(run, 0xffff);
if (c <= 15) {
out[o++] = (byte) ((c << 4) | color);
} else if (c <= 255) {
// Worst case can exceed the sizing above only via escape
// forms, which occur for runs >= 16 (net win); safe.
out[o++] = (byte) color;
out[o++] = (byte) c;
} else {
out[o++] = (byte) color;
out[o++] = 0;
out[o++] = (byte) (c & 0xff);
out[o++] = (byte) (c >> 8);
}
run -= c;
}
i = j;
}
byte[] trimmed = new byte[o];
System.arraycopy(out, 0, trimmed, 0, o);
return trimmed;
}
}
private static final Object lock = new Object();
private static final Map<String, Integer> fontIds = new HashMap<>();
private static final Map<Long, Glyph> glyphs = new HashMap<>();
private static int nextFontId = 1;
/** Stable id for a font key; assigns one on first use. */
public static int fontId(String key) {
synchronized (lock) {
Integer id = fontIds.get(key);
if (id == null) {
id = nextFontId++;
fontIds.put(key, id);
}
return id;
}
}
public static Glyph get(int fontId, int encoding) {
synchronized (lock) {
return glyphs.get(key(fontId, encoding));
}
}
private static long key(int fontId, int encoding) {
return ((long) fontId << 32) | (encoding & 0xffffffffL);
}
/**
* Register a batch of glyph rasters. Little-endian buffer, a sequence of
* font groups:
* [keyLen u8][key utf8][cellHeight u8][count u16]
* each followed by count glyph records:
* [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8]
* [inkHeight x rows u32] (bit (ceil(width/8)*8 - 1 - col) = ink)
* Records for an already-registered (font, encoding) are skipped (glyph
* rasters are immutable for a given font key). Malformed buffers throw:
* they indicate a phone-side marshalling bug, never device state.
*/
public static void register(ByteBuffer buffer) {
if (buffer == null) return;
ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN);
synchronized (lock) {
while (in.remaining() > 0) {
int keyLen = in.get() & 0xff;
byte[] keyBytes = new byte[keyLen];
in.get(keyBytes);
String fontKey = new String(keyBytes, StandardCharsets.UTF_8);
int cellHeight = in.get() & 0xff;
int count = in.getShort() & 0xffff;
Integer idBoxed = fontIds.get(fontKey);
int id;
if (idBoxed == null) {
id = nextFontId++;
fontIds.put(fontKey, id);
} else {
id = idBoxed;
}
for (int g = 0; g < count; g++) {
int encoding = in.getInt();
int bbxX = in.get();
int inkTop = in.get() & 0xff;
int width = in.get() & 0xff;
int inkHeight = in.get() & 0xff;
int[] rows = new int[inkHeight];
for (int r = 0; r < inkHeight; r++) {
rows[r] = in.getInt();
}
long k = key(id, encoding);
if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0
&& inkTop + inkHeight <= cellHeight) {
glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX,
rows, null));
}
}
}
}
}
/**
* Register a batch of antialiased (4bpp) glyph rasters, e.g. TTF renders.
* Same font-group framing as register(), but each glyph record carries
* packed coverage instead of 1bpp rows:
* [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8]
* [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel]
* Nibble values are the final 4bpp source levels (the draw-time LUT
* scales them by the top color). Already-registered (font, encoding)
* records are skipped, as in register().
*/
public static void registerAa(ByteBuffer buffer) {
if (buffer == null) return;
ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN);
synchronized (lock) {
while (in.remaining() > 0) {
int keyLen = in.get() & 0xff;
byte[] keyBytes = new byte[keyLen];
in.get(keyBytes);
String fontKey = new String(keyBytes, StandardCharsets.UTF_8);
int cellHeight = in.get() & 0xff;
int count = in.getShort() & 0xffff;
Integer idBoxed = fontIds.get(fontKey);
int id;
if (idBoxed == null) {
id = nextFontId++;
fontIds.put(fontKey, id);
} else {
id = idBoxed;
}
for (int g = 0; g < count; g++) {
int encoding = in.getInt();
int bbxX = in.get();
int inkTop = in.get() & 0xff;
int width = in.get() & 0xff;
int inkHeight = in.get() & 0xff;
int stride = (width + 1) >> 1;
byte[] coverage = new byte[width * inkHeight];
for (int r = 0; r < inkHeight; r++) {
for (int b = 0; b < stride; b++) {
int packed = in.get() & 0xff;
int col = b * 2;
coverage[r * width + col] = (byte) (packed >> 4);
if (col + 1 < width) {
coverage[r * width + col + 1] = (byte) (packed & 0x0f);
}
}
}
long k = key(id, encoding);
if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0
&& inkTop + inkHeight <= cellHeight) {
glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX,
null, coverage));
}
}
}
}
}
}
@@ -0,0 +1,115 @@
package com.faceclaw.app;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Process-wide registry of icon/image rasters for the texture-cache pipeline,
* the image counterpart of GlyphAtlas. Entries are content-addressed: the TS
* side keys each image by a hash of its dimensions and pixels, so the same
* icon registered from any thread or rendered at any time dedupes to one
* entry, and an icon whose content changes is simply a new entry.
*
* Unlike glyphs (1-bit ink recolored at draw time), images keep their exact
* 4bpp values: the cached bytes quantize the registered 8bpp pixels with the
* same GRAY_TO_NIBBLE table the composite is packed with, and the planner
* draws them via mode 13 with an identity LUT (top color 15) and the
* transparent bit — the draw writes exactly the nonzero-nibble pixels, which
* is what the eligibility check and hole punching are defined over.
*/
public final class ImageAtlas {
private ImageAtlas() {}
public static final class Entry {
public final int width;
public final int height;
/** One 4bpp value per pixel, row-major. */
final byte[] nibbles;
/** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */
public final byte[] cachedBytes;
Entry(int width, int height, byte[] nibbles) {
this.width = width;
this.height = height;
this.nibbles = nibbles;
this.cachedBytes = encodeCachedImage();
}
/** The 4bpp value the mode-13 draw would write at (col, row); 0 = skipped. */
public int nibbleAt(int col, int row) {
return nibbles[row * width + col] & 0xff;
}
private byte[] encodeCachedImage() {
int total = width * height;
byte[] out = new byte[2 + total];
out[0] = (byte) width;
out[1] = (byte) height;
int o = 2;
int i = 0;
while (i < total) {
int color = nibbles[i] & 0xff;
int j = i + 1;
while (j < total && (nibbles[j] & 0xff) == color) j++;
int run = j - i;
while (run > 0) {
int c = Math.min(run, 0xffff);
if (c <= 15) {
out[o++] = (byte) ((c << 4) | color);
} else if (c <= 255) {
out[o++] = (byte) color;
out[o++] = (byte) c;
} else {
out[o++] = (byte) color;
out[o++] = 0;
out[o++] = (byte) (c & 0xff);
out[o++] = (byte) (c >> 8);
}
run -= c;
}
i = j;
}
byte[] trimmed = new byte[o];
System.arraycopy(out, 0, trimmed, 0, o);
return trimmed;
}
}
private static final Object lock = new Object();
private static final Map<String, Integer> ids = new HashMap<>();
private static final List<Entry> entries = new ArrayList<>();
/**
* Register an image (8bpp grayscale pixels, width*height bytes, row-major)
* under a content key, returning its id — or the existing id when the key
* is already registered (the pixels are then ignored: same key means same
* content). Ids are positive and stable for the process lifetime.
*/
public static int ensure(String key, int width, int height, ByteBuffer pixels8bpp) {
if (key == null || width <= 0 || width > 255 || height <= 0 || height > 255
|| pixels8bpp == null || pixels8bpp.remaining() < width * height) {
throw new IllegalArgumentException("bad image registration " + width + "x" + height);
}
synchronized (lock) {
Integer existing = ids.get(key);
if (existing != null) return existing;
byte[] nibbles = new byte[width * height];
for (int i = 0; i < nibbles.length; i++) {
nibbles[i] = (byte) BmpUtil.nibbleForGray(pixels8bpp.get(i) & 0xff);
}
entries.add(new Entry(width, height, nibbles));
int id = entries.size(); // ids start at 1
ids.put(key, id);
return id;
}
}
public static Entry get(int id) {
synchronized (lock) {
return id >= 1 && id <= entries.size() ? entries.get(id - 1) : null;
}
}
}
@@ -0,0 +1,229 @@
package com.faceclaw.app;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Reader for the serialized LVGL font image in the G2's CJK flash partition.
* Faceclaw bundles that font because it is Source Han Sans SC Light under the
* SIL OFL. Proprietary fonts embedded in the main firmware never pass through
* this class and are instead extracted on the user's phone during CFW install.
*/
public final class LvglFontFile {
private static final String TAG = "LvglFontFile";
private static final int MAPPED_BASE = 0x80100000;
private static final int FONT_CACHE_SIZE = 2;
private static final Map<String, FontData> fontCache =
new LinkedHashMap<String, FontData>(FONT_CACHE_SIZE, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, FontData> eldest) {
return size() > FONT_CACHE_SIZE;
}
};
private LvglFontFile() {}
/**
* Returns [lineHeight, baseline] or an empty array when the font is invalid.
* The baseline is measured down from the top of the line.
*/
public static byte[] getMetrics(String path) {
FontData font = load(path);
if (font == null) return new byte[0];
return new byte[] { (byte) font.lineHeight, (byte) font.baseline };
}
/**
* Returns one packed glyph:
* u16 boxW, u16 boxH, i16 ofsX, i16 ofsY, then aligned 4bpp bitmap bytes.
* Returns an empty array when the code point is not mapped.
*/
public static byte[] getGlyph(String path, int codePoint) {
FontData font = load(path);
if (font == null) return new byte[0];
Integer glyphId = font.codePointToGlyph.get(codePoint);
if (glyphId == null || glyphId <= 0) return new byte[0];
try {
int descriptor = font.glyphDescriptors + glyphId * 16;
int bitmapIndex = font.u32(descriptor);
int boxWidth = font.u16(descriptor + 8);
int boxHeight = font.u16(descriptor + 10);
int offsetX = font.i16(descriptor + 12);
int offsetY = font.i16(descriptor + 14);
int stride = boxWidth / 2 + 1;
int bitmapLength = boxWidth > 0 && boxHeight > 0 ? stride * boxHeight : 0;
font.check(font.glyphBitmaps + bitmapIndex, bitmapLength);
byte[] result = new byte[8 + bitmapLength];
putU16(result, 0, boxWidth);
putU16(result, 2, boxHeight);
putU16(result, 4, offsetX);
putU16(result, 6, offsetY);
System.arraycopy(font.bytes, font.glyphBitmaps + bitmapIndex, result, 8, bitmapLength);
return result;
} catch (RuntimeException e) {
Log.w(TAG, "invalid glyph U+" + Integer.toHexString(codePoint) + " in " + path, e);
return new byte[0];
}
}
private static synchronized FontData load(String path) {
if (path == null || path.isEmpty()) return null;
FontData cached = fontCache.get(path);
if (cached != null) return cached;
try {
FontData font = FontData.read(path);
fontCache.put(path, font);
return font;
} catch (IOException | RuntimeException e) {
Log.w(TAG, "could not load LVGL font " + path, e);
return null;
}
}
private static void putU16(byte[] destination, int offset, int value) {
destination[offset] = (byte) (value & 0xff);
destination[offset + 1] = (byte) ((value >>> 8) & 0xff);
}
private static final class FontData {
final byte[] bytes;
final ByteBuffer view;
final int lineHeight;
final int baseline;
final int glyphBitmaps;
final int glyphDescriptors;
final Map<Integer, Integer> codePointToGlyph;
private FontData(byte[] bytes) {
this.bytes = bytes;
this.view = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
if (bytes.length < 0x40 || bytes[0] != 'Z' || bytes[1] != 'Z' || bytes[2] != 'Z' || bytes[3] != 'Z') {
throw new IllegalArgumentException("missing ZZZZ font header");
}
lineHeight = u16(0x38);
int baseLine = u16(0x3a);
baseline = lineHeight - baseLine;
int descriptor = pointerOffset(u32(0x34));
glyphBitmaps = pointerOffset(u32(descriptor));
glyphDescriptors = pointerOffset(u32(descriptor + 4));
int cmaps = pointerOffset(u32(descriptor + 8));
int packed = u16(descriptor + 18);
int cmapCount = packed & 0x1ff;
int bitsPerPixel = (packed >>> 9) & 0xf;
int bitmapFormat = (packed >>> 14) & 0x3;
if (lineHeight <= 0 || baseline < 0 || cmapCount <= 0 ||
bitsPerPixel != 4 || bitmapFormat != 3) {
throw new IllegalArgumentException("unsupported LVGL font metadata");
}
codePointToGlyph = new HashMap<>();
for (int index = 0; index < cmapCount; index++) {
readCmap(cmaps + index * 20);
}
}
static FontData read(String path) throws IOException {
File file = new File(path);
if (!file.isFile() || !file.canRead() || file.length() > Integer.MAX_VALUE) {
throw new IOException("font is not a readable file");
}
byte[] bytes = new byte[(int) file.length()];
try (RandomAccessFile input = new RandomAccessFile(file, "r")) {
input.readFully(bytes);
}
return new FontData(bytes);
}
void readCmap(int address) {
int rangeStart = u32(address);
int rangeLength = u16(address + 4);
int glyphIdStart = u16(address + 6);
int unicodeListPointer = u32(address + 8);
int glyphIdOffsetsPointer = u32(address + 12);
int listLength = u16(address + 16);
int type = u8(address + 18);
if (type == 2) {
for (int relative = 0; relative < rangeLength; relative++) {
codePointToGlyph.put(rangeStart + relative, glyphIdStart + relative);
}
return;
}
if (type == 0) {
int offsets = pointerOffset(glyphIdOffsetsPointer);
for (int relative = 0; relative < rangeLength; relative++) {
int offset = u8(offsets + relative);
if (relative != 0 && offset == 0) continue;
codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset);
}
return;
}
if (type == 3 || type == 1) {
int unicodeList = pointerOffset(unicodeListPointer);
int offsets = glyphIdOffsetsPointer == 0 ? -1 : pointerOffset(glyphIdOffsetsPointer);
for (int index = 0; index < listLength; index++) {
int relative = u16(unicodeList + index * 2);
int offset = index;
if (offsets >= 0) {
offset = type == 1 ? u16(offsets + index * 2) : u8(offsets + index);
if (index != 0 && offset == 0) continue;
}
codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset);
}
return;
}
throw new IllegalArgumentException("unknown cmap type " + type);
}
int u8(int offset) {
check(offset, 1);
return bytes[offset] & 0xff;
}
int u16(int offset) {
check(offset, 2);
return view.getShort(offset) & 0xffff;
}
int i16(int offset) {
check(offset, 2);
return view.getShort(offset);
}
int u32(int offset) {
check(offset, 4);
return view.getInt(offset);
}
int pointerOffset(int pointer) {
long unsignedPointer = pointer & 0xffffffffL;
long offset = unsignedPointer - (MAPPED_BASE & 0xffffffffL);
if (offset < 0 || offset > Integer.MAX_VALUE) {
throw new IllegalArgumentException("font pointer is outside the mapped partition");
}
check((int) offset, 1);
return (int) offset;
}
void check(int offset, int length) {
if (offset < 0 || length < 0 || (long) offset + length > bytes.length) {
throw new IllegalArgumentException("font range is out of bounds");
}
}
}
}
@@ -42,6 +42,71 @@ public final class SurfaceCompositor {
public static final int TRANSPARENCY_OPAQUE = 0;
public static final int TRANSPARENCY_COLOR_KEY = 1;
/**
* One deferred draw (a text glyph or an icon image) within a frame, in
* screen coordinates. The draw's pixels are already baked into the
* composited gray buffer (the TS side bakes before submitting); this
* record preserves the draw's identity so the texture-cache planner can
* replay it as an on-glasses cached draw instead of image bytes.
*
* Glyphs: x/y are the pen position and line top; the raster (and its
* bearing/cell placement) comes from GlyphAtlas under (fontId, encoding).
* Images: x/y are the blit's top-left; the raster comes from ImageAtlas
* under imageId.
*/
public static final class ScreenDraw {
public static final int KIND_GLYPH = 0;
public static final int KIND_IMAGE = 1;
/** A firmware-builtin-font text run (CFW mode 15). */
public static final int KIND_FWTEXT = 2;
public final int kind;
/** Glyph draws only. */
public final int fontId;
/** Glyph draws only. */
public final int encoding;
/** Image draws only. */
public final int imageId;
public final int x;
public final int y;
/** Glyph and fw-text draws: 8-bit brightness. */
public final int value;
/** Fw-text runs only: member codepoints, in text order. */
public final int[] fwCps;
/** Fw-text runs only: member pen offsets relative to x. */
public final int[] fwDx;
/** Fw-text runs only: whether each member has visible pixels. */
public final boolean[] fwInk;
ScreenDraw(int kind, int fontId, int encoding, int imageId, int x, int y, int value,
int[] fwCps, int[] fwDx, boolean[] fwInk) {
this.kind = kind;
this.fontId = fontId;
this.encoding = encoding;
this.imageId = imageId;
this.x = x;
this.y = y;
this.value = value;
this.fwCps = fwCps;
this.fwDx = fwDx;
this.fwInk = fwInk;
}
static ScreenDraw glyph(int fontId, int encoding, int penX, int lineY, int value) {
return new ScreenDraw(KIND_GLYPH, fontId, encoding, 0, penX, lineY, value, null, null, null);
}
static ScreenDraw image(int imageId, int x, int y) {
return new ScreenDraw(KIND_IMAGE, 0, 0, imageId, x, y, 0, null, null, null);
}
static ScreenDraw fwText(int x, int y, int value, int[] cps, int[] dx, boolean[] ink) {
return new ScreenDraw(KIND_FWTEXT, 0, 0, 0, x, y, value, cps, dx, ink);
}
}
private static final ScreenDraw[] NO_DRAWS = new ScreenDraw[0];
/** One composited full-screen frame plus the metadata the pipeline needs. */
public static final class Composite {
/** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */
@@ -56,13 +121,20 @@ public final class SurfaceCompositor {
public final String fingerprint;
/** Monotonic: a Composite with a higher seq contains strictly newer state. */
public final long seq;
/**
* Screen-space deferred draws of every visible surface, in composite
* order (surface z ascending, then each surface's draw order). Their
* pixels are already baked into gray.
*/
public final ScreenDraw[] draws;
Composite(byte[] gray, int width, int height, String fingerprint, long seq) {
Composite(byte[] gray, int width, int height, String fingerprint, long seq, ScreenDraw[] draws) {
this.gray = gray;
this.width = width;
this.height = height;
this.fingerprint = fingerprint;
this.seq = seq;
this.draws = draws == null ? NO_DRAWS : draws;
}
}
@@ -77,6 +149,8 @@ public final class SurfaceCompositor {
boolean visible = true;
byte[] pixels;
String fingerprint = "";
/** Surface-local deferred draws of the retained content (already baked into pixels). */
ScreenDraw[] draws = NO_DRAWS;
Surface(String id) {
this.id = id;
@@ -125,6 +199,7 @@ public final class SurfaceCompositor {
if (surface.pixels == null || surface.width != width || surface.height != height) {
surface.pixels = new byte[width * height];
surface.fingerprint = "";
surface.draws = NO_DRAWS;
}
surface.x = x;
surface.y = y;
@@ -183,6 +258,31 @@ public final class SurfaceCompositor {
int rectHeight,
String contentFingerprint
) {
return applyAndComposite(surfaceId, pixels, rectX, rectY, rectWidth, rectHeight,
contentFingerprint, null);
}
/**
* As above, with the frame's deferred draws: a little-endian buffer of
* tagged records
* [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph)
* [1][imageId u32][x s16][y s16] (image)
* in surface-local coordinates and draw order. The list describes the
* surface's FULL retained content and replaces the previous list, so it is
* only meaningful for full-surface updates (which is what every submitter
* sends); pass null to clear. Draw pixels must already be baked into pixels.
*/
public Composite applyAndComposite(
String surfaceId,
ByteBuffer pixels,
int rectX,
int rectY,
int rectWidth,
int rectHeight,
String contentFingerprint,
ByteBuffer glyphs
) {
ScreenDraw[] parsed = parseDraws(glyphs);
synchronized (lock) {
requireScreenConfiguredLocked();
Surface surface = surfaces.get(surfaceId);
@@ -205,10 +305,55 @@ public final class SurfaceCompositor {
pixels.get(surface.pixels, dstOffset, rectWidth);
}
surface.fingerprint = contentFingerprint == null ? "" : contentFingerprint;
surface.draws = parsed;
return compositeLocked();
}
}
private static ScreenDraw[] parseDraws(ByteBuffer draws) {
if (draws == null || draws.remaining() < 1) {
return NO_DRAWS;
}
ByteBuffer in = draws.order(java.nio.ByteOrder.LITTLE_ENDIAN);
List<ScreenDraw> out = new ArrayList<>();
while (in.remaining() >= 1) {
int kind = in.get() & 0xff;
if (kind == ScreenDraw.KIND_GLYPH && in.remaining() >= 11) {
int fontId = in.getShort() & 0xffff;
int encoding = in.getInt();
int penX = in.getShort();
int lineY = in.getShort();
int value = in.get() & 0xff;
out.add(ScreenDraw.glyph(fontId, encoding, penX, lineY, value));
} else if (kind == ScreenDraw.KIND_IMAGE && in.remaining() >= 8) {
int imageId = in.getInt();
int x = in.getShort();
int y = in.getShort();
out.add(ScreenDraw.image(imageId, x, y));
} else if (kind == ScreenDraw.KIND_FWTEXT && in.remaining() >= 6) {
int x = in.getShort();
int y = in.getShort();
int value = in.get() & 0xff;
int count = in.get() & 0xff;
if (in.remaining() < count * 7) {
break; // malformed tail: keep what parsed cleanly
}
int[] cps = new int[count];
int[] dx = new int[count];
boolean[] ink = new boolean[count];
for (int i = 0; i < count; i++) {
cps[i] = in.getInt();
dx[i] = in.getShort();
ink[i] = in.get() != 0;
}
out.add(ScreenDraw.fwText(x, y, value, cps, dx, ink));
} else {
break; // malformed tail: keep what parsed cleanly
}
}
return out.toArray(new ScreenDraw[0]);
}
/** Composite the current retained state without applying an update. */
public Composite composite() {
synchronized (lock) {
@@ -228,7 +373,7 @@ public final class SurfaceCompositor {
return null;
}
byte[] gray = buildGrayLocked();
return new Composite(gray, screenWidth, screenHeight, "preview", 0);
return new Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS);
}
}
@@ -253,7 +398,7 @@ public final class SurfaceCompositor {
byte[] gray = new byte[screenWidth * screenHeight];
if (blanked) {
return new Composite(gray, screenWidth, screenHeight,
"blanked:" + screenWidth + "x" + screenHeight, nextCompositeSeq++);
"blanked:" + screenWidth + "x" + screenHeight, nextCompositeSeq++, NO_DRAWS);
}
List<Surface> ordered = new ArrayList<>(surfaces.values());
ordered.sort(Comparator
@@ -261,9 +406,15 @@ public final class SurfaceCompositor {
.thenComparing(s -> s.id));
StringBuilder fingerprint = new StringBuilder();
fingerprint.append(screenWidth).append('x').append(screenHeight);
List<ScreenDraw> draws = new ArrayList<>();
for (Surface surface : ordered) {
if (!surface.visible) continue;
blendLocked(gray, surface);
for (ScreenDraw draw : surface.draws) {
draws.add(new ScreenDraw(draw.kind, draw.fontId, draw.encoding, draw.imageId,
draw.x + surface.x, draw.y + surface.y, draw.value,
draw.fwCps, draw.fwDx, draw.fwInk));
}
fingerprint.append('|').append(surface.id)
.append('@').append(surface.x).append(',').append(surface.y)
.append('+').append(surface.width).append('x').append(surface.height)
@@ -271,7 +422,8 @@ public final class SurfaceCompositor {
.append(':').append(surface.transparency)
.append(':').append(surface.fingerprint);
}
return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++);
return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++,
draws.toArray(new ScreenDraw[0]));
}
private void blendLocked(byte[] gray, Surface surface) {
@@ -205,15 +205,68 @@ public final class BleImageOptimizer {
return new IncrementalPlan(out, changedBytes, (boxWidth >> 1) * boxHeight, clusterCount);
}
/**
* The aligned changed bounding box between two same-size packed frames, or
* null when they are identical: pixel-space {left, top, width, height} with
* left/width multiples of 4 and top/height multiples of 2 (mode-3 header
* quantization). Exposed so the texture-cache planner can reuse the exact
* rect geometry the single-bbox path would send.
*/
public static int[] computeChangedBox(byte[] previous, byte[] next, int width, int height) {
int stride = (width + 1) >> 1;
if (width <= 0 || height <= 0 || previous == null || next == null
|| previous.length != stride * height || next.length != stride * height) {
return null;
}
int minByteX = stride;
int maxByteX = -1;
int minY = height;
int maxY = -1;
for (int y = 0; y < height; y++) {
int rowOffset = y * stride;
for (int x = 0; x < stride; x++) {
if (previous[rowOffset + x] != next[rowOffset + x]) {
if (x < minByteX) minByteX = x;
if (x > maxByteX) maxByteX = x;
if (y < minY) minY = y;
maxY = y;
}
}
}
if (maxY < 0) {
return null;
}
int left = (minByteX * 2) & ~3;
int rightExclusive = Math.min(width, (((maxByteX + 1) * 2) + 3) & ~3);
int top = minY & ~1;
int bottomExclusive = Math.min(height, (maxY + 2) & ~1);
return new int[]{left, top, rightExclusive - left, bottomExclusive - top};
}
/**
* The multi-rect split of the changed region (see computeSplitRects), or null
* when it is one region / too fragmented / the frames aren't comparable.
* Exposed for the texture-cache planner.
*/
public static List<int[]> computeChangedRects(byte[] previous, byte[] next, int width, int height, int maxRects) {
int stride = (width + 1) >> 1;
if (width <= 0 || height <= 0 || previous == null || next == null
|| previous.length != stride * height || next.length != stride * height) {
return null;
}
List<int[]> rects = computeSplitRects(previous, next, stride, width, height, maxRects);
return (rects == null || rects.size() < 2) ? null : rects;
}
/**
* Encode one CFW mode-3 rectangle delta:
* [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][zlib(rle(box pixels))]
* left/width must be multiples of 4 (=> left>>1, width>>1 are whole bytes), top/
* height multiples of 2. The box pixels are top-down rows of `next` (4bpp packed,
* width>>1 bytes/row), run-length encoded before deflate. Shared by the single-bbox
* path and each rect of a mode-8 multi-rect batch.
* path, each rect of a mode-8 multi-rect batch, and the texture-cache planner.
*/
private static byte[] encodeMode3Rect(byte[] next, int stride, int left, int top,
static byte[] encodeMode3Rect(byte[] next, int stride, int left, int top,
int boxWidth, int boxHeight, int fid) {
int regionStride = boxWidth >> 1;
byte[] region = new byte[regionStride * boxHeight];
@@ -46,6 +46,7 @@ public class BleProtocol {
public static final int FACECLAW_FB_OP_ACQUIRE = 5;
public static final int FACECLAW_FB_OP_RELEASE = 6;
public static final int FACECLAW_WEAR_OP_QUERY = 7;
public static final int CFW_IMAGE_MODE_CLEANUP = 11;
private static final int FACECLAW_WAKE_EVENT = 1;
private static final int FACECLAW_WAKE_PROTOCOL_VERSION = 1;
// UI_FOREGROUND_EVEN_AI_ID: the stock "Even AI" assistant app. It is a
@@ -59,4 +59,9 @@ public class ConnectionOptions {
// small (one tight rect); only attempt multi-rect above it or when the box
// spans multiple horizontal clusters.
public static final int MULTI_RECT_MIN_PAYLOAD = 900;
// Ship text as on-glasses cached-glyph draws (CFW modes 12/14) instead of
// pixels, punching glyph ink out of the baked deltas (see TexturePlanner).
// Also gated at runtime on the firmware advertising texcache12 + texstr14.
final boolean TEXTURE_CACHE_FRAMES = true;
}
@@ -103,6 +103,22 @@ public class MessageBuilder {
);
}
public OutboundMessage cfwCleanup(
BleProtocol.ImageTileOptions tile,
int sessionId,
boolean leftArm
) {
byte[] payload = new byte[] { (byte) BleProtocol.CFW_IMAGE_MODE_CLEANUP };
return imagePayload(
"cfw-cleanup",
tile,
sessionId,
payload,
"CFW cleanup",
leftArm
);
}
public OutboundMessage enableOrDisableMic(boolean enable) {
int magic = magicPool.allocate();
return new OutboundMessage(
@@ -0,0 +1,176 @@
package com.faceclaw.app;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Phone-side model of the CFW's flat 64 KiB texture cache (zlib_glue mode 12).
* The firmware holds only bytes; every structural decision — where font
* tables and glyph images live, what is resident, when to start over — is
* made here. Layout per font: a 96-entry uint16 offset table (chars 32..127)
* whose entries are written lazily as each glyph is uploaded; glyph images
* are the GlyphAtlas cached bytes ([w][h][4bpp RLE]).
*
* Allocation is a bump pointer with reset-on-full: the realistic working set
* (a few fonts' tables plus ~hundreds of glyphs at tens of bytes each) is far
* below 64 KiB, so eviction sophistication buys nothing. After a reset the
* firmware cache still holds stale bytes; that is safe because a draw only
* ever references (font table entry, glyph image) pairs uploaded in the
* current generation — stale table entries are never named in a mode-14
* string.
*
* Upload entries accumulate as complete [offset u16][len u16][data] records
* and drain into one or more standalone mode-12 payloads, each independently
* valid, enqueued ahead of the image message that references them (the
* transport is FIFO, so no ack round-trip is needed before use).
*
* Not thread-safe by itself: the communicator calls it under its own lock.
*/
public final class TextureCacheState {
public static final int CACHE_SIZE = 65536;
private static final int FONT_TABLE_BYTES = 96 * 2;
private int allocEnd = 0;
private final Map<Integer, Integer> fontTableOffsets = new HashMap<>();
private final Map<Long, Integer> glyphOffsets = new HashMap<>();
private final Map<Integer, Integer> imageOffsets = new HashMap<>();
private final List<byte[]> pendingEntries = new ArrayList<>();
private int pendingBytes = 0;
private int generation = 0;
/**
* Forget everything: the on-glasses cache is gone (session teardown, lease
* loss) or no longer trustworthy (upload timeout), or we ran out of space.
* Glyphs and images re-upload lazily on next use.
*/
public void reset() {
allocEnd = 0;
fontTableOffsets.clear();
glyphOffsets.clear();
imageOffsets.clear();
pendingEntries.clear();
pendingBytes = 0;
generation++;
}
/** Bumped by every reset; lets a planner detect a mid-plan reset. */
public int generation() {
return generation;
}
/**
* Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a
* sudden drop in the frame log means a reset-on-full re-upload cycle —
* the signal that the 64 KiB budget is being outgrown).
*/
public int usedBytes() {
return allocEnd;
}
/** The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit. */
public int fontTableOffset(int fontId) {
Integer existing = fontTableOffsets.get(fontId);
return existing == null ? -1 : existing;
}
/**
* Ensure a glyph image (and its font table slot) is resident, queuing the
* mode-12 upload entries for anything newly placed. Returns the glyph's
* image offset, or -1 when the cache is full (caller resets and retries)
* or the encoding is outside the mode-14 table (32..127).
*/
public int ensureGlyph(int fontId, int encoding, GlyphAtlas.Glyph glyph) {
if (glyph == null || encoding < 32 || encoding > 127) return -1;
long key = ((long) fontId << 32) | (encoding & 0xffffffffL);
Integer existing = glyphOffsets.get(key);
if (existing != null) return existing;
Integer table = fontTableOffsets.get(fontId);
int need = glyph.cachedBytes.length + (table == null ? FONT_TABLE_BYTES : 0);
if (allocEnd + need > CACHE_SIZE) return -1;
if (table == null) {
table = allocEnd;
allocEnd += FONT_TABLE_BYTES;
fontTableOffsets.put(fontId, table);
// No upload for the table itself: entries are written per glyph,
// and only uploaded entries are ever referenced by a draw.
}
int offset = allocEnd;
allocEnd += glyph.cachedBytes.length;
glyphOffsets.put(key, offset);
addEntry(offset, glyph.cachedBytes);
addEntry(table + 2 * (encoding - 32),
new byte[]{(byte) (offset & 0xff), (byte) ((offset >> 8) & 0xff)});
return offset;
}
/**
* Ensure an ImageAtlas entry is resident (mode-13 draws reference it by
* raw cache offset; no table involved). Returns the image offset, or -1
* when the cache is full (caller resets and retries).
*/
public int ensureImage(int imageId, ImageAtlas.Entry image) {
if (image == null) return -1;
Integer existing = imageOffsets.get(imageId);
if (existing != null) return existing;
if (allocEnd + image.cachedBytes.length > CACHE_SIZE) return -1;
int offset = allocEnd;
allocEnd += image.cachedBytes.length;
imageOffsets.put(imageId, offset);
addEntry(offset, image.cachedBytes);
return offset;
}
private void addEntry(int offset, byte[] data) {
byte[] entry = new byte[4 + data.length];
entry[0] = (byte) (offset & 0xff);
entry[1] = (byte) ((offset >> 8) & 0xff);
entry[2] = (byte) (data.length & 0xff);
entry[3] = (byte) ((data.length >> 8) & 0xff);
System.arraycopy(data, 0, entry, 4, data.length);
pendingEntries.add(entry);
pendingBytes += entry.length;
}
public boolean hasPendingUploads() {
return !pendingEntries.isEmpty();
}
/** Total queued upload bytes (entry framing included), for logging. */
public int pendingUploadBytes() {
return pendingBytes;
}
/**
* Drain the queued entries into complete mode-12 payloads ([12][entries]),
* each at most maxPayloadBytes so a payload fits one BLE image message
* without fragmentation. A single entry larger than the cap still gets its
* own payload (the transport can fragment it).
*/
public List<byte[]> drainUploadPayloads(int maxPayloadBytes) {
List<byte[]> payloads = new ArrayList<>();
ByteArrayOutputStream current = null;
for (byte[] entry : pendingEntries) {
if (current != null && current.size() + entry.length > maxPayloadBytes) {
payloads.add(current.toByteArray());
current = null;
}
if (current == null) {
current = new ByteArrayOutputStream(Math.min(maxPayloadBytes, 4 + entry.length));
current.write(12);
}
current.write(entry, 0, entry.length);
}
if (current != null) {
payloads.add(current.toByteArray());
}
pendingEntries.clear();
pendingBytes = 0;
return payloads;
}
}
@@ -0,0 +1,721 @@
package com.faceclaw.app;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Plans a screen update that ships text and icons as on-glasses cached draws
* instead of pixels (CFW modes 12/13/14; see g2flash/patches/zlib_glue.c and
* texture_cache.c).
*
* Approach: dirty rects come from comparing the fully composited old and new
* frames, exactly as the plain incremental path does. For each deferred draw
* (glyph or image) whose pixels intersect a sent rect, check that every pixel
* the on-glasses draw would write lands correct — i.e. the new composite's
* 4bpp value at each in-panel nonzero-source pixel equals what the draw
* produces (this one check subsumes plane occlusion, surface
* occlusion/clipping, and draw-on-draw overlap). A draw that passes and is
* (or can be made) resident in the texture cache is replayed on-glasses: its
* written pixels are punched to 0 in the delta rect content — which is what
* makes text and icons nearly free to compress — and re-emitted as a mode-14
* string or mode-13 image sub-message in the same atomic mode-8 batch, so
* the shadow after apply equals the full composite exactly. Everything else
* stays baked.
*
* Fallback shape: when nothing qualifies, plan() returns null and the caller
* uses the plain single-bbox/multi-rect/full-frame paths unchanged.
*/
public final class TexturePlanner {
private TexturePlanner() {}
/** Cap on mode-14 string sub-messages (mode-8 count is a u8). */
private static final int MAX_GLYPH_RUNS = 180;
/**
* Soft cap on cached-glyph selection per update, before run grouping.
* Sized above a full-screen 6x12 terminal repaint (~4,240 cells) so dense
* screens draw everything; the run budget is the real bound.
*/
private static final int MAX_SELECTED_GLYPHS = 4600;
/** Cap on mode-13 image sub-messages per update. */
private static final int MAX_IMAGE_DRAWS = 60;
/** Cap on mode-15 builtin-font string sub-messages per update. */
private static final int MAX_FWTEXT_RUNS = 80;
/** Mode-12 payload size that fits one BLE image message comfortably. */
private static final int UPLOAD_PAYLOAD_MAX = 3600;
/** Max x-adjust control bytes between two glyphs before starting a new run. */
private static final int MAX_ADJUST_BYTES = 4;
/** Options: identity LUT (top 15) + transparent, for mode-13 image draws. */
private static final int IMAGE_DRAW_OPTIONS = 0x1f;
public static final class Result {
/** Complete image payload: a mode-8 batch of rect deltas + cached draws. */
public final byte[] payload;
/** Mode-12 upload payloads to enqueue BEFORE the image message. */
public final List<byte[]> uploads;
/** Next mode-3 frame id (deltas consumed some). */
public final int nextFid;
public final int rectCount;
public final int drawnGlyphs;
public final int runCount;
public final int drawnImages;
public final int fwGlyphs;
public final int fwRuns;
public final int bakedCandidates;
public final int uploadBytes;
public final boolean fullFrame;
/**
* Where the planning time went, in ms. This runs on the send thread,
* directly in the input-to-display path, so the split is reported in
* the frame log: rects = finding the changed region, match = checking
* each draw against the composite, cache = residency/uploads plus run
* building, punch = clearing replayed draws out of a copy of the
* frame, encode = compressing the leftover pixels.
*/
public int rectsMs;
public int matchMs;
public int cacheMs;
public int punchMs;
public int encodeMs;
Result(byte[] payload, List<byte[]> uploads, int nextFid, int rectCount,
int drawnGlyphs, int runCount, int drawnImages, int fwGlyphs, int fwRuns,
int bakedCandidates, int uploadBytes, boolean fullFrame) {
this.payload = payload;
this.uploads = uploads;
this.nextFid = nextFid;
this.rectCount = rectCount;
this.drawnGlyphs = drawnGlyphs;
this.runCount = runCount;
this.drawnImages = drawnImages;
this.fwGlyphs = fwGlyphs;
this.fwRuns = fwRuns;
this.bakedCandidates = bakedCandidates;
this.uploadBytes = uploadBytes;
this.fullFrame = fullFrame;
}
}
/** One draw selected for on-glasses replay. */
private static final class Selected {
final SurfaceCompositor.ScreenDraw draw;
final GlyphAtlas.Glyph glyphAtlas; // glyph draws
final ImageAtlas.Entry imageAtlas; // image draws
final int gx; // screen x of the cached image (pen + bearing / blit left)
final int top; // glyph draws: 4bpp top color
Selected(SurfaceCompositor.ScreenDraw draw, GlyphAtlas.Glyph glyphAtlas,
ImageAtlas.Entry imageAtlas, int gx, int top) {
this.draw = draw;
this.glyphAtlas = glyphAtlas;
this.imageAtlas = imageAtlas;
this.gx = gx;
this.top = top;
}
}
/**
* Plan a cached-draw update. previous is the delta base (the frame the
* shadow currently holds), or null/mismatched for a full-frame keyframe.
* allowImages requires the teximg13 capability; allowFwText requires
* font15. Returns null when the plain paths should run instead (no
* replayable draws, or identical frames).
*/
public static Result plan(
byte[] previous, byte[] next, int width, int height,
SurfaceCompositor.ScreenDraw[] draws,
TextureCacheState cache, int fidStart,
boolean allowMultiRect, int maxRects, boolean allowImages, boolean allowFwText) {
if (next == null || draws == null || draws.length == 0 || width <= 0 || height <= 0) {
return null;
}
int stride = (width + 1) >> 1;
if (next.length != stride * height) {
return null;
}
long planStartedAtMs = android.os.SystemClock.elapsedRealtime();
// Rect geometry, matching what the plain paths would send.
boolean fullFrame = previous == null || previous.length != next.length;
List<int[]> rects;
if (!fullFrame) {
int[] box = BleImageOptimizer.computeChangedBox(previous, next, width, height);
if (box == null) {
return null; // identical frames; caller's dedup normally catches this
}
if (box[2] >= width && box[3] >= height) {
fullFrame = true;
rects = Collections.singletonList(new int[]{0, 0, width, height});
} else {
List<int[]> split = allowMultiRect
? BleImageOptimizer.computeChangedRects(previous, next, width, height, maxRects)
: null;
rects = split != null ? split : Collections.singletonList(box);
}
} else {
rects = Collections.singletonList(new int[]{0, 0, width, height});
}
long matchStartedAtMs = android.os.SystemClock.elapsedRealtime();
// Candidates: draws whose written pixels intersect a sent rect, are
// representable on the wire, and land correct against the composite.
List<Selected> selected = new ArrayList<>();
int bakedCandidates = 0;
int selectedImages = 0;
// Builtin-font (mode 15) runs: no cache residency, no uploads — the
// firmware owns the font. Processed first because it has no cache
// side effects; the counters feed the shared stats.
List<byte[]> fwSubs = new ArrayList<>();
List<FwPunch> fwPunches = new ArrayList<>();
int fwGlyphCount = 0;
int fwBaked = 0;
if (allowFwText) {
for (SurfaceCompositor.ScreenDraw draw : draws) {
if (draw.kind != SurfaceCompositor.ScreenDraw.KIND_FWTEXT) continue;
fwBaked += planFwRun(draw, rects, next, stride, width, height, fwSubs, fwPunches);
}
fwGlyphCount = fwPunches.size();
bakedCandidates += fwBaked;
}
for (SurfaceCompositor.ScreenDraw draw : draws) {
if (draw.kind == SurfaceCompositor.ScreenDraw.KIND_GLYPH) {
GlyphAtlas.Glyph atlas = GlyphAtlas.get(draw.fontId, draw.encoding);
if (atlas == null) continue; // unregistered: stays baked, not a "candidate"
int gx = draw.x + atlas.bbxX;
int inkTop = draw.y + atlas.inkTop;
if (!intersectsAny(rects, gx, inkTop, atlas.width, atlas.inkHeight)) {
continue;
}
if (draw.encoding < 32 || draw.encoding > 127
|| gx < 0 || draw.y < 0 || gx > 0xffff || draw.y > 0xffff) {
bakedCandidates++;
continue;
}
int top = BmpUtil.nibbleForGray(draw.value);
if (!glyphMatchesComposite(next, stride, width, height, atlas, gx, draw.y, top)) {
bakedCandidates++;
continue;
}
if (selected.size() >= MAX_SELECTED_GLYPHS) {
bakedCandidates++;
continue;
}
selected.add(new Selected(draw, atlas, null, gx, top));
} else {
if (!allowImages) continue; // firmware lacks teximg13: stays baked
ImageAtlas.Entry atlas = ImageAtlas.get(draw.imageId);
if (atlas == null) continue;
if (!intersectsAny(rects, draw.x, draw.y, atlas.width, atlas.height)) {
continue;
}
if (draw.x < 0 || draw.y < 0 || draw.x > 0xffff || draw.y > 0xffff
|| selectedImages >= MAX_IMAGE_DRAWS) {
bakedCandidates++;
continue;
}
if (!imageMatchesComposite(next, stride, width, height, atlas, draw.x, draw.y)) {
bakedCandidates++;
continue;
}
selected.add(new Selected(draw, null, atlas, draw.x, 0));
selectedImages++;
}
}
if (selected.isEmpty() && fwSubs.isEmpty()) {
return null;
}
long cacheStartedAtMs = android.os.SystemClock.elapsedRealtime();
// Residency: ensure every selected draw is in the cache, retrying once
// after a reset when the bump allocator fills. A draw that still fails
// falls back to baked. IMPORTANT: once ensure* has run, every bail-out
// to the plain path must cache.reset() — ensure* marked entries
// resident and queued uploads that only a returned Result would
// enqueue. (Fw-text needs no residency, so a fw-only plan skips this.)
List<Selected> drawable = selected.isEmpty()
? Collections.<Selected>emptyList()
: ensureResident(cache, selected);
bakedCandidates += selected.size() - drawable.size();
// Group glyphs into mode-14 runs; leftover-budget glyphs bake.
List<Selected> glyphDrawable = new ArrayList<>();
List<Selected> imageDrawable = new ArrayList<>();
for (Selected sel : drawable) {
if (sel.glyphAtlas != null) glyphDrawable.add(sel);
else imageDrawable.add(sel);
}
List<byte[]> runs = new ArrayList<>();
List<Selected> drawnGlyphs = buildRuns(cache, glyphDrawable, runs);
bakedCandidates += glyphDrawable.size() - drawnGlyphs.size();
List<Selected> drawn = new ArrayList<>(drawnGlyphs);
drawn.addAll(imageDrawable);
if ((drawn.isEmpty() && fwSubs.isEmpty())
|| rects.size() + runs.size() + imageDrawable.size() + fwSubs.size() > 255) {
if (!selected.isEmpty()) {
cache.reset();
}
return null;
}
// Punch the drawn pixels out of a copy of the composite, then encode
// the rect deltas from the punched content.
long punchStartedAtMs = android.os.SystemClock.elapsedRealtime();
byte[] punched = next.clone();
for (Selected sel : drawn) {
punch(punched, stride, width, height, sel);
}
for (FwPunch fw : fwPunches) {
punchFw(punched, stride, width, height, fw);
}
long encodeStartedAtMs = android.os.SystemClock.elapsedRealtime();
List<byte[]> subs = new ArrayList<>();
int fid = fidStart;
if (fullFrame) {
subs.add(BleImageOptimizer.maybeCompress(punched, width, height));
} else {
for (int[] r : rects) {
subs.add(BleImageOptimizer.encodeMode3Rect(punched, stride, r[0], r[1], r[2], r[3], fid));
fid = (fid >= 0xfffe) ? 1 : fid + 1;
}
}
for (Selected sel : imageDrawable) {
subs.add(encodeImageDraw(cache, sel));
}
subs.addAll(runs);
subs.addAll(fwSubs);
byte[] payload = assembleMode8(subs);
int uploadBytes = cache.pendingUploadBytes();
List<byte[]> uploads = cache.drainUploadPayloads(UPLOAD_PAYLOAD_MAX);
Result result = new Result(payload, uploads, fid, rects.size(), drawnGlyphs.size(), runs.size(),
imageDrawable.size(), fwGlyphCount, fwSubs.size(),
bakedCandidates, uploadBytes, fullFrame);
long doneAtMs = android.os.SystemClock.elapsedRealtime();
result.rectsMs = (int) (matchStartedAtMs - planStartedAtMs);
result.matchMs = (int) (cacheStartedAtMs - matchStartedAtMs);
result.cacheMs = (int) (punchStartedAtMs - cacheStartedAtMs);
result.punchMs = (int) (encodeStartedAtMs - punchStartedAtMs);
result.encodeMs = (int) (doneAtMs - encodeStartedAtMs);
return result;
}
/** One punch task for an emitted firmware-font glyph. */
private static final class FwPunch {
final FwGlyphAtlas.Entry entry;
final int gx;
final int gy;
FwPunch(FwGlyphAtlas.Entry entry, int gx, int gy) {
this.entry = entry;
this.gx = gx;
this.gy = gy;
}
}
/**
* Plan one firmware-font run (mode 15): classify every member, and when
* any correct ink member intersects a sent rect, emit sub-messages over
* the maximal correct stretches. A stretch never spans an incorrect
* member — the sub-message boundary restarts the firmware's advance walk
* at an explicit pen x, so occluded or unknown glyphs simply stay baked
* without disturbing their neighbors' positions. Emitted ink members are
* queued for punching. Returns how many ink members stay baked.
*/
private static int planFwRun(
SurfaceCompositor.ScreenDraw draw, List<int[]> rects,
byte[] next, int stride, int width, int height,
List<byte[]> outSubs, List<FwPunch> outPunches) {
int n = draw.fwCps.length;
if (n == 0 || draw.y < 0 || draw.y > 0xffff) {
return countInk(draw);
}
int top = BmpUtil.nibbleForGray(draw.value);
boolean[] ok = new boolean[n];
FwGlyphAtlas.Entry[] entries = new FwGlyphAtlas.Entry[n];
boolean relevant = false;
int baked = 0;
for (int i = 0; i < n; i++) {
if (!draw.fwInk[i]) {
ok[i] = true; // inkless connector (space): draws nothing
continue;
}
FwGlyphAtlas.Entry entry = FwGlyphAtlas.get(draw.fwCps[i]);
if (entry == null) {
baked++;
continue;
}
int gx = draw.x + draw.fwDx[i] + entry.ofsX;
int gy = draw.y + entry.inkTop;
if (!fwGlyphMatchesComposite(next, stride, width, height, entry, gx, gy, top)) {
baked++;
continue;
}
ok[i] = true;
entries[i] = entry;
if (intersectsAny(rects, gx, gy, entry.boxW, entry.boxH)) {
relevant = true;
}
}
if (!relevant) {
return 0; // nothing in a sent rect: leave the run's pixels alone
}
int i = 0;
while (i < n && outSubs.size() < MAX_FWTEXT_RUNS) {
while (i < n && !(ok[i] && draw.fwInk[i])) i++;
if (i >= n) break;
int startX = draw.x + draw.fwDx[i];
if (startX < 0 || startX > 0xffff) {
baked++;
i++;
continue;
}
// Extend across correct members while the UTF-8 payload fits,
// tracking the last ink member so trailing connectors trim away.
int j = i;
int lastInk = -1;
int bytesToLastInk = 0;
int bytesSoFar = 0;
while (j < n && ok[j]) {
int encodedLength = utf8Length(draw.fwCps[j]);
if (bytesSoFar + encodedLength > 255) break;
bytesSoFar += encodedLength;
if (draw.fwInk[j]) {
lastInk = j;
bytesToLastInk = bytesSoFar;
}
j++;
}
if (lastInk < i) { // a single over-long codepoint; not realistic
baked++;
i++;
continue;
}
byte[] sub = new byte[7 + bytesToLastInk];
sub[0] = 15;
sub[1] = (byte) (startX & 0xff);
sub[2] = (byte) ((startX >> 8) & 0xff);
sub[3] = (byte) (draw.y & 0xff);
sub[4] = (byte) ((draw.y >> 8) & 0xff);
sub[5] = (byte) (top | 0x10); // top color + transparent
sub[6] = (byte) bytesToLastInk;
int pos = 7;
for (int k = i; k <= lastInk; k++) {
byte[] encoded = new StringBuilder().appendCodePoint(draw.fwCps[k])
.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8);
System.arraycopy(encoded, 0, sub, pos, encoded.length);
pos += encoded.length;
}
outSubs.add(sub);
for (int k = i; k <= lastInk; k++) {
if (draw.fwInk[k]) {
outPunches.add(new FwPunch(entries[k],
draw.x + draw.fwDx[k] + entries[k].ofsX,
draw.y + entries[k].inkTop));
}
}
i = lastInk + 1;
}
return baked;
}
private static int utf8Length(int cp) {
if (cp < 0x80) return 1;
if (cp < 0x800) return 2;
if (cp < 0x10000) return 3;
return 4;
}
private static int countInk(SurfaceCompositor.ScreenDraw draw) {
int count = 0;
for (boolean ink : draw.fwInk) {
if (ink) count++;
}
return count;
}
/**
* Whether every in-panel nonzero pixel of the builtin-font glyph lands
* correct: the composite's 4bpp value must equal the firmware's LUT
* output src*top/15 (integer division — a general LUT check, since these
* glyphs are anti-aliased).
*/
private static boolean fwGlyphMatchesComposite(byte[] packed, int stride, int width, int height,
FwGlyphAtlas.Entry entry, int gx, int gy, int top) {
for (int row = 0; row < entry.boxH; row++) {
int y = gy + row;
if (y < 0 || y >= height) continue;
for (int col = 0; col < entry.boxW; col++) {
int source = entry.nibbleAt(col, row);
if (source == 0) continue;
int x = gx + col;
if (x < 0 || x >= width) continue;
if (nibbleAt(packed, stride, x, y) != (source * top) / 15) {
return false;
}
}
}
return true;
}
private static void punchFw(byte[] packed, int stride, int width, int height, FwPunch fw) {
for (int row = 0; row < fw.entry.boxH; row++) {
int y = fw.gy + row;
if (y < 0 || y >= height) continue;
for (int col = 0; col < fw.entry.boxW; col++) {
if (fw.entry.nibbleAt(col, row) == 0) continue;
punchPixel(packed, stride, width, fw.gx + col, y);
}
}
}
private static boolean intersectsAny(List<int[]> rects, int x, int y, int w, int h) {
if (w <= 0 || h <= 0) return false;
for (int[] r : rects) {
if (x < r[0] + r[2] && x + w > r[0] && y < r[1] + r[3] && y + h > r[1]) {
return true;
}
}
return false;
}
/**
* Whether every in-panel ink pixel of the glyph lands correct in the
* packed new frame: the composite's 4bpp value must equal the firmware's
* LUT output source*top/15 (integer). For 1bpp glyphs (source 15) this is
* exactly `== top`; AA glyphs get the general check. Ink outside the
* panel is clipped by the firmware and irrelevant.
*/
private static boolean glyphMatchesComposite(byte[] packed, int stride, int width, int height,
GlyphAtlas.Glyph atlas, int gx, int lineY, int top) {
for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) {
int y = lineY + row;
if (y < 0 || y >= height) continue;
for (int col = 0; col < atlas.width; col++) {
int source = atlas.nibbleAt(col, row);
if (source == 0) continue;
int x = gx + col;
if (x < 0 || x >= width) continue;
if (nibbleAt(packed, stride, x, y) != (source * top) / 15) {
return false;
}
}
}
return true;
}
/**
* Whether every in-panel nonzero pixel of the cached image equals the
* packed new frame — the mode-13 transparent draw writes exactly those.
*/
private static boolean imageMatchesComposite(byte[] packed, int stride, int width, int height,
ImageAtlas.Entry atlas, int left, int top) {
for (int row = 0; row < atlas.height; row++) {
int y = top + row;
if (y < 0 || y >= height) continue;
for (int col = 0; col < atlas.width; col++) {
int source = atlas.nibbleAt(col, row);
if (source == 0) continue;
int x = left + col;
if (x < 0 || x >= width) continue;
if (nibbleAt(packed, stride, x, y) != source) {
return false;
}
}
}
return true;
}
private static int nibbleAt(byte[] packed, int stride, int x, int y) {
int b = packed[y * stride + (x >> 1)] & 0xff;
return (x & 1) != 0 ? (b & 0x0f) : (b >> 4);
}
private static void punch(byte[] packed, int stride, int width, int height, Selected sel) {
if (sel.glyphAtlas != null) {
GlyphAtlas.Glyph atlas = sel.glyphAtlas;
for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) {
int y = sel.draw.y + row;
if (y < 0 || y >= height) continue;
for (int col = 0; col < atlas.width; col++) {
if (!atlas.inkAt(col, row)) continue;
punchPixel(packed, stride, width, sel.gx + col, y);
}
}
} else {
ImageAtlas.Entry atlas = sel.imageAtlas;
for (int row = 0; row < atlas.height; row++) {
int y = sel.draw.y + row;
if (y < 0 || y >= height) continue;
for (int col = 0; col < atlas.width; col++) {
if (atlas.nibbleAt(col, row) == 0) continue;
punchPixel(packed, stride, width, sel.gx + col, y);
}
}
}
}
private static void punchPixel(byte[] packed, int stride, int width, int x, int y) {
if (x < 0 || x >= width) return;
int index = y * stride + (x >> 1);
if ((x & 1) != 0) {
packed[index] = (byte) (packed[index] & 0xf0);
} else {
packed[index] = (byte) (packed[index] & 0x0f);
}
}
/**
* Ensure residency for all of `selected`, resetting the cache once if the
* allocator fills mid-plan (a reset invalidates offsets handed out earlier
* in the same pass, so the whole pass reruns). Returns the draws that are
* resident afterwards.
*/
private static List<Selected> ensureResident(TextureCacheState cache, List<Selected> selected) {
for (int attempt = 0; attempt < 2; attempt++) {
List<Selected> resident = new ArrayList<>(selected.size());
boolean full = false;
for (Selected sel : selected) {
int offset = sel.glyphAtlas != null
? cache.ensureGlyph(sel.draw.fontId, sel.draw.encoding, sel.glyphAtlas)
: cache.ensureImage(sel.draw.imageId, sel.imageAtlas);
if (offset >= 0) {
resident.add(sel);
} else {
full = true;
}
}
if (!full || attempt == 1) {
return resident;
}
cache.reset();
}
return Collections.emptyList(); // unreachable
}
/** Mode-13 cached-image draw: [13][offset u16][x u16][y u16][options u8]. */
private static byte[] encodeImageDraw(TextureCacheState cache, Selected sel) {
int offset = cache.ensureImage(sel.draw.imageId, sel.imageAtlas); // resident: returns the offset
byte[] sub = new byte[8];
sub[0] = 13;
sub[1] = (byte) (offset & 0xff);
sub[2] = (byte) ((offset >> 8) & 0xff);
sub[3] = (byte) (sel.draw.x & 0xff);
sub[4] = (byte) ((sel.draw.x >> 8) & 0xff);
sub[5] = (byte) (sel.draw.y & 0xff);
sub[6] = (byte) ((sel.draw.y >> 8) & 0xff);
sub[7] = (byte) IMAGE_DRAW_OPTIONS;
return sub;
}
/**
* Group selected glyphs into mode-14 string sub-messages:
* [14][fontTable u16][x u16][y u16][options u8][strlen u8][string]
* One run per (font, line y, top color) span; within a run, control bytes
* 1..31 adjust x by -10..+20 to hit each glyph's exact position, and each
* glyph advances x by its cached width. Order across runs is free: every
* drawn glyph's ink equals the composite, so overlaps write equal values.
* Returns the glyphs actually emitted (run budget can drop stragglers).
*/
private static List<Selected> buildRuns(TextureCacheState cache, List<Selected> drawable, List<byte[]> outRuns) {
List<Selected> sorted = new ArrayList<>(drawable);
sorted.sort(Comparator
.comparingInt((Selected s) -> s.draw.fontId)
.thenComparingInt(s -> s.draw.y)
.thenComparingInt(s -> s.top)
.thenComparingInt(s -> s.gx));
List<Selected> drawn = new ArrayList<>(sorted.size());
int i = 0;
while (i < sorted.size() && outRuns.size() < MAX_GLYPH_RUNS) {
Selected first = sorted.get(i);
int fontTable = cache.fontTableOffset(first.draw.fontId);
if (fontTable < 0) { // shouldn't happen: residency ensured above
i++;
continue;
}
java.io.ByteArrayOutputStream string = new java.io.ByteArrayOutputStream();
List<Selected> runGlyphs = new ArrayList<>();
int cursor = first.gx; // the run's wire x starts exactly at the first glyph
int j = i;
while (j < sorted.size()) {
Selected sel = sorted.get(j);
if (sel.draw.fontId != first.draw.fontId
|| sel.draw.y != first.draw.y
|| sel.top != first.top) {
break;
}
int delta = sel.gx - cursor;
int adjustBytes = adjustByteCount(delta);
if (adjustBytes > MAX_ADJUST_BYTES && j > i) {
break; // far gap: cheaper to start a fresh run
}
if (string.size() + adjustBytes + 1 > 255) {
break; // strlen is a u8
}
emitAdjust(string, delta);
string.write(sel.draw.encoding);
runGlyphs.add(sel);
cursor = sel.gx + sel.glyphAtlas.width;
j++;
}
if (!runGlyphs.isEmpty()) {
byte[] stringBytes = string.toByteArray();
byte[] run = new byte[9 + stringBytes.length];
run[0] = 14;
run[1] = (byte) (fontTable & 0xff);
run[2] = (byte) ((fontTable >> 8) & 0xff);
run[3] = (byte) (first.gx & 0xff);
run[4] = (byte) ((first.gx >> 8) & 0xff);
run[5] = (byte) (first.draw.y & 0xff);
run[6] = (byte) ((first.draw.y >> 8) & 0xff);
run[7] = (byte) (first.top | 0x10); // top color + transparent
run[8] = (byte) stringBytes.length;
System.arraycopy(stringBytes, 0, run, 9, stringBytes.length);
outRuns.add(run);
drawn.addAll(runGlyphs);
i = j;
} else {
i++;
}
}
return drawn;
}
/** How many 1..31 control bytes are needed to move the cursor by delta. */
private static int adjustByteCount(int delta) {
if (delta == 0) return 0;
return delta > 0 ? (delta + 19) / 20 : (-delta + 9) / 10;
}
private static void emitAdjust(java.io.ByteArrayOutputStream out, int delta) {
while (delta != 0) {
int step = delta > 0 ? Math.min(delta, 20) : Math.max(delta, -10);
out.write(step + 11); // byte 1..31 adjusts x by (byte - 11)
delta -= step;
}
}
/** [8][count]([len u16][sub])* — one atomic present for deltas + draws. */
private static byte[] assembleMode8(List<byte[]> subs) {
int total = 2;
for (byte[] sub : subs) {
total += 2 + sub.length;
}
byte[] out = new byte[total];
out[0] = 8;
out[1] = (byte) subs.size();
int pos = 2;
for (byte[] sub : subs) {
out[pos] = (byte) (sub.length & 0xff);
out[pos + 1] = (byte) ((sub.length >> 8) & 0xff);
pos += 2;
System.arraycopy(sub, 0, out, pos, sub.length);
pos += sub.length;
}
return out;
}
}
@@ -20,6 +20,15 @@ public class BmpUtil {
}
}
/**
* The 4bpp level an 8-bit gray value packs to. Public so the texture-cache
* planner computes a glyph draw's top color with the exact quantization
* the composited frame was packed with.
*/
public static int nibbleForGray(int gray) {
return GRAY_TO_NIBBLE[gray & 0xff] & 0xff;
}
/**
* Pack a full-resolution 8bpp grayscale buffer (row-major, top-to-bottom,
* one byte per pixel) into the headerless 4bpp wire format used by CFW
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path
name="privacy_policies"
path="privacy-policies/" />
</paths>
+12 -1
View File
@@ -1,6 +1,11 @@
# Faceclaw Privacy Policy
Faceclaw is a privacy-conscious app.
Faceclaw is a privacy-conscious app. The developers of Faceclaw don't collect
or store user data. Faceclaw does integrate with some third-party services,
which have their own privacy policies; this document describes what those
services are, what they're used for and what data they collect, and links to
their privacy policies.
## Audio
@@ -62,4 +67,10 @@ The Weather app will retrieve the user's coarse location, and will use it to
query a third-party weather information provider.
## EvenHub
The EvenHub app store is subject to Even Realities' privacy policy:
https://support.evenrealities.com/hc/en-us/articles/14127464826511-Privacy-Policy.
Apps downloaded from the EvenHub app store come with their own privacy
policies.
+18 -4
View File
@@ -38,11 +38,12 @@ To install, run
## Features
* A voice assistant that wakes up when you say "Hey Even", transcribes text
with an on-device model, OpenAI Whisper, ElevenLabs, or Soniox API, and responds
to queries and commands using an Anthropic or OpenAI model (requires an API
key), or using your own long-running OpenClaw agent (see "Connecting an
external agent" below).
with an on-device model, OpenAI Whisper, ElevenLabs, or Soniox API, and
responds to queries and commands using an onboard model (Qwen3 4B, slow)
or with an Anthropic or OpenAI model (requires an API key), or using your
own long-running OpenClaw agent (see "Connecting an external agent" below).
* Multitasking, with an app-switcher sidebar and app launcher
* Mostly-compatible with EvenHub apps
* A lock screen; glasses lock automatically when you take them off and unlock
when you unlock your phone
* Full-screen apps can use the full 640x480 display area (rather than the
@@ -88,6 +89,19 @@ OpenClaw agent with the phone plugged into its host can perform the whole
setup itself. Note that a pulled settings file contains your API keys, so
treat it as a secret (the default output path is gitignored here).
## EvenHub App Notes
Faceclaw is mostly compatible with EvenHub apps. If you are developing an
app or using an open source app, you can package it into an EHPK file, send
it to your phone, and open it in the file browser to install. Or, you can log
into EvenHub and download apps there.
Faceclaw runs EvenHub apps through an emulation layer; you may run into bugs
and differences in behavior. If you run into bugs, please try tunning them in
the stock Android app before you report them to the app's creator. If you're
developing your own app and plan to submit it to the Hub, be sure to test it in
the stock Android app before submitting.
## Additional Caveats
Some permissions are used, but may not reliably prompted for. Go to Android
+2
View File
@@ -17,6 +17,7 @@ import minesweeperApp from "./minesweeper";
import freecellApp from "./freecell";
import pinballApp from "./pinball";
import debugTestsApp from "./debug-tests";
import evenhubApp from "./evenhub";
import settingsApp from "./settings";
/**
@@ -43,5 +44,6 @@ export const ALL_APPS: readonly AppDefinition[] = [
freecellApp,
pinballApp,
debugTestsApp,
evenhubApp,
settingsApp,
];
+8 -3
View File
@@ -1,4 +1,5 @@
import { GrayImage } from "../graphics/image";
import { type GrayImage } from "../graphics/image";
import { type Plane } from "../graphics/plane";
import { type IconName } from "../graphics/icons";
import { type LayerActions } from "../ui/layers";
import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window";
@@ -18,6 +19,8 @@ export type AppDefinition = {
title: string;
/** Launcher-grid icon. */
icon: IconName;
/** App-supplied launcher icon renderer; icon remains the fallback. */
renderIcon?: (size: number) => GrayImage | null;
/** Open the app's window (or focus it if already open). */
launch: (ctx: AppContext, params?: AppLaunchParams) => Promise<void>;
/**
@@ -50,6 +53,8 @@ export type AppContext = {
actions: Omit<LayerActions, "requestRender">;
/** Launch (or focus) any app by id through the shell. */
launchApp: (appId: string, params?: AppLaunchParams) => Promise<void>;
/** Uninstall a dynamically registered app, if the app id supports it. */
uninstallApp: (appId: string) => Promise<void>;
/** Launch-or-focus a main-thread singleton window keyed by windowId. */
launchInProcessApp: (
windowId: string,
@@ -58,8 +63,8 @@ export type AppContext = {
) => Promise<void>;
/** Get this app's shared worker host, spawning the worker on first use. */
ensureWorkerHost: (createWorker: () => Worker) => WorkerAppHost;
/** Submit a painted frame for a window surface (windows created outside launchInProcessApp). */
submitWindowFrame: (surfaceId: string, image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
/** Submit a window surface's painted frame, as planes (windows created outside launchInProcessApp). */
submitWindowFrame: (surfaceId: string, planes: Plane[], paintMs: number, frameId: number) => Promise<void>;
setWindowSurfaceVisible: (surfaceId: string, visible: boolean) => void;
requestShellRender: () => void;
appendLog: (message: string) => void;
+14 -6
View File
@@ -9,7 +9,10 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
@@ -207,6 +210,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(window, message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -527,11 +533,11 @@ function updateTickTimer(window: BlocksWindow): void {
}
}
function paint(window: BlocksWindow): GrayImage {
function paint(window: BlocksWindow): Plane[] {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
return singlePlane(paintContent(window));
}
function paintContent(window: BlocksWindow): GrayImage {
@@ -632,11 +638,11 @@ function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: blocks content unchanged");
return;
@@ -646,7 +652,8 @@ function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -657,6 +664,7 @@ function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+12 -12
View File
@@ -1,5 +1,5 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage, type UiFont } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { readUpcomingEvents, type CalendarEvent } from "../../native/calendar";
@@ -7,12 +7,12 @@ import { timeFormatSetting } from "../../ui/dashboard-settings";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
import { hasCalendarPermission } from "../../g2/android-permissions";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
const PAGE_X = 12;
const PAGE_Y = 12;
const LIST_TOP = 38;
const ROW_X = 16;
const LINE_HEIGHT = 14;
const DAY_HEADER_HEIGHT = 16;
const ROW_GAP = 4;
const MAX_EVENTS = 50;
@@ -57,7 +57,7 @@ export class CalendarLayer implements Layer {
return image;
}
const rows = buildEventRows(events);
const rows = buildEventRows(font, events);
this.selectedIndex = clamp(this.selectedIndex, 0, rows.length - 1);
image.drawText(font, width - 96, PAGE_Y + 9, `${this.selectedIndex + 1}/${rows.length}`, 150);
@@ -81,7 +81,7 @@ export class CalendarLayer implements Layer {
if (event.type === "click") this.requestPermission();
return;
}
const rows = buildEventRows(readUpcomingEvents(MAX_EVENTS));
const rows = buildEventRows(getDefaultSmallFont(), readUpcomingEvents(MAX_EVENTS));
if (!rows.length) return;
if (event.type === "scroll-up") {
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
@@ -90,17 +90,17 @@ export class CalendarLayer implements Layer {
}
}
private paintPermissionPrompt(image: GrayImage, font: BdfFont, width: number, height: number): void {
private paintPermissionPrompt(image: GrayImage, font: UiFont, width: number, height: number): void {
const message = "Grant calendar permission on your phone to see your events on the glasses.";
const lines = wrapText(font, message, width - 48);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, 24, 72 + index * LINE_HEIGHT, lines[index]!, 190);
image.drawText(font, 24, 72 + index * lineStep(font), lines[index]!, 190);
}
image.drawText(font, 24, height - 36, `${GESTURE_CLICK} request ${GESTURE_DOUBLE_CLICK} back`, 110);
}
}
function buildEventRows(events: CalendarEvent[]): EventRow[] {
function buildEventRows(font: UiFont, events: CalendarEvent[]): EventRow[] {
const rows: EventRow[] = [];
let previousDayKey = "";
for (const event of events) {
@@ -119,7 +119,7 @@ function buildEventRows(events: CalendarEvent[]): EventRow[] {
event,
dayHeader,
lines,
height: (dayHeader ? DAY_HEADER_HEIGHT : 0) + 6 + lines.length * LINE_HEIGHT,
height: (dayHeader ? DAY_HEADER_HEIGHT : 0) + 6 + lines.length * lineStep(font),
});
}
return rows;
@@ -127,7 +127,7 @@ function buildEventRows(events: CalendarEvent[]): EventRow[] {
function drawEventRow(
image: GrayImage,
font: BdfFont,
font: UiFont,
row: EventRow,
x: number,
y: number,
@@ -139,7 +139,7 @@ function drawEventRow(
image.drawText(font, x, cursorY + 2, row.dayHeader, 150);
cursorY += DAY_HEADER_HEIGHT;
}
const bodyHeight = row.lines.length * LINE_HEIGHT + 4;
const bodyHeight = row.lines.length * lineStep(font) + 4;
if (selected) {
image.fillRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 15, 6);
image.drawRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 90, 6);
@@ -147,7 +147,7 @@ function drawEventRow(
const maxTextWidth = width - 2 * x;
for (let index = 0; index < row.lines.length; index++) {
const value = index === 0 ? (selected ? 235 : 205) : 160;
image.drawText(font, x, cursorY + 3 + index * LINE_HEIGHT, truncateText(font, row.lines[index]!, maxTextWidth), value);
image.drawText(font, x, cursorY + 3 + index * lineStep(font), truncateText(font, row.lines[index]!, maxTextWidth), value);
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import {
COMPASS_CALIBRATION_COMPLETE,
+1 -1
View File
@@ -1,4 +1,4 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
+6 -5
View File
@@ -1,9 +1,10 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../../ui/layers";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
import { tightRowHeight } from "../../ui/metrics";
import {
buildSoundSequencePayload,
CFW_SEQ_MAX,
@@ -14,7 +15,6 @@ import {
} from "../../ui/sound-effects";
const HEADER_HEIGHT = 30;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
@@ -42,16 +42,17 @@ export class BuzzerDemoLayer implements Layer {
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
const rowH = tightRowHeight(font);
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length);
const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < lastVisible; index++) {
const effect = SOUND_EFFECTS[index]!;
const y = HEADER_HEIGHT + (index - this.scrollRow) * ROW_HEIGHT;
const y = HEADER_HEIGHT + (index - this.scrollRow) * rowH;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
}
image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200);
image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120);
+1 -1
View File
@@ -1,4 +1,4 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
+1 -1
View File
@@ -1,4 +1,4 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
import { drawListScrollbar } from "../../ui/menu";
+114
View File
@@ -0,0 +1,114 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText } from "../../graphics/textwrap";
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawSelectionHighlight } from "../../ui/menu";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
const DIALOG_X = 8;
const DIALOG_Y = 8;
const DIALOG_WIDTH = 272;
const PADDING = 10;
const SERVICE_LABELS: Record<string, string> = {
openai: "OpenAI",
anthropic: "Anthropic",
soniox: "Soniox",
elevenlabs: "ElevenLabs",
mapbox: "Mapbox",
};
export type ApiKeyRequestItem = { id: string; configured: boolean };
/**
* Consent dialog shown when an EvenHub app calls requestApiKeyAccess: lists the
* requested API-key services (marking any the user hasn't configured) and asks
* Allow / Deny. Allow shares the configured keys' values with the app; Deny (or
* double-click) shares nothing.
*/
export class EvenHubApiKeyDialogLayer implements Layer {
/** 0 = Allow, 1 = Deny. */
private selectedIndex = 0;
private resolved = false;
constructor(
private readonly appName: string,
private readonly services: ApiKeyRequestItem[],
private readonly onConfirm: () => void,
private readonly onCancel: () => void,
) {}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const textWidth = DIALOG_WIDTH - 2 * PADDING - 4;
const left = DIALOG_X + PADDING + 2;
const headerStep = lineStep(font) + 2;
const serviceStep = lineStep(font) + 1;
const actionRowH = listRowHeight(font);
const bodyTop = PADDING + headerStep + 6 + this.services.length * serviceStep + 8;
const height = Math.min(bodyTop + 2 * actionRowH + PADDING, viewportHeight - 2 * DIALOG_Y);
image.fillRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 1);
image.drawRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 72);
let y = DIALOG_Y + PADDING;
image.drawText(font, left, y, truncateText(font, `${this.appName} wants your keys:`, textWidth), 235);
y += headerStep + 6;
for (const service of this.services) {
const label = SERVICE_LABELS[service.id] ?? service.id;
const text = service.configured ? label : `${label} (not set)`;
image.drawText(font, left + 8, y, truncateText(font, text, textWidth - 8), service.configured ? 220 : 130);
y += serviceStep;
}
y += 4;
const focused = ctx.stack.isFocused();
const actions = ["Allow", "Deny"];
for (let index = 0; index < actions.length; index++) {
const rowY = y + index * actionRowH;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
}
image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200);
}
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
switch (event.type) {
case "scroll-up":
case "scroll-down":
this.selectedIndex = (this.selectedIndex + 1) % 2;
return;
case "click":
this.resolve(ctx, this.selectedIndex === 0);
return;
case "double-click":
this.resolve(ctx, false);
return;
default:
return;
}
}
onRemoved(): void {
// Dismissed some other way (window closed): treat as deny.
if (!this.resolved) {
this.resolved = true;
this.onCancel();
}
}
private resolve(ctx: LayerContext, allowed: boolean): void {
if (this.resolved) return;
this.resolved = true;
ctx.stack.pop();
if (allowed) this.onConfirm();
else this.onCancel();
}
}
+69
View File
@@ -0,0 +1,69 @@
/**
* Arbitrates the glasses' magnetometer compass (CFW mode-10 stream) among
* running EvenHub apps, mirroring the IMU router. Only the foreground app
* receives headings, so a backgrounded app can't keep the sensor running.
*
* The compass is NOT pre-calibrated — the magnetometer is in the right arm,
* which rests on the head with a wearer-dependent curl (see compass-app memory);
* headings are relative, not true north.
*/
import { addCompassListener, setCompassEnabled, COMPASS_CHANGED, type CompassEvent } from "../../native/compass";
export type EvenHubCompassClient = {
readonly windowId: string;
isForeground(): boolean;
deliverCompass(headingDegrees: number): void;
};
class EvenHubCompassRouter {
private readonly requesting = new Set<EvenHubCompassClient>();
private active: EvenHubCompassClient | null = null;
private unsubscribe: (() => void) | null = null;
private enabled = false;
requestCompass(client: EvenHubCompassClient): void {
this.requesting.add(client);
this.evaluate();
}
releaseCompass(client: EvenHubCompassClient): void {
if (!this.requesting.delete(client)) return;
this.evaluate();
}
notifyEligibilityChanged(): void {
this.evaluate();
}
private eligible(): EvenHubCompassClient | null {
return Array.from(this.requesting).find((client) => client.isForeground()) ?? null;
}
private evaluate(): void {
const next = this.eligible();
this.active = next;
if (next) {
if (!this.unsubscribe) {
this.unsubscribe = addCompassListener((event: CompassEvent) => {
// Only heading updates carry a real heading; calibration events use -1.
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
this.active?.deliverCompass(event.headingDegrees);
}
});
}
if (!this.enabled) {
setCompassEnabled(true);
this.enabled = true;
}
} else {
if (this.enabled) {
setCompassEnabled(false);
this.enabled = false;
}
this.unsubscribe?.();
this.unsubscribe = null;
}
}
}
export const evenHubCompassRouter = new EvenHubCompassRouter();
+158
View File
@@ -0,0 +1,158 @@
/**
* Phone-side reimplementation of the on-glasses EvenHub page compositor:
* containers render into the 576x288 canvas the stock firmware gives apps.
*
* Text uses Even's extracted 20px firmware font (via EvenHubFont), measured
* and wrapped with @evenrealities/pretext, so glyph widths and line breaks
* match what apps expect.
*
* Known deviations from stock, acceptable for now:
* - List rendering is a plain vertical list; itemWidth-based horizontal
* layouts are not implemented yet.
*/
import { GrayImage } from "../../graphics/image";
import { EvenHubFont } from "../../graphics/evenhub-font";
import {
type EvenHubContainer,
type EvenHubImageContainer,
type EvenHubListContainer,
type EvenHubPage,
type EvenHubTextContainer,
} from "./containers";
export const EVENHUB_SCREEN_WIDTH = 576;
export const EVENHUB_SCREEN_HEIGHT = 288;
const TEXT_WHITE = 255;
const LIST_ROW_PADDING = 4;
/** Chars the font lacks are skipped silently on stock (no tofu); EvenHubFont
* skips unknown glyphs, matching that. */
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
paintBorder(image, container);
const inset = container.borderWidth + container.paddingLength;
EvenHubFont.get().drawTextWrapped(
image,
container.x + inset,
container.y + inset,
Math.max(1, container.width - 2 * inset),
container.content,
TEXT_WHITE,
);
}
function paintBorder(
image: GrayImage,
container: { x: number; y: number; width: number; height: number; borderWidth: number; borderRadius: number },
): void {
for (let i = 0; i < container.borderWidth; i++) {
if (container.borderRadius > 0) {
image.drawRoundedRect(
container.x + i,
container.y + i,
container.width - 2 * i,
container.height - 2 * i,
TEXT_WHITE,
Math.max(0, container.borderRadius - i),
);
} else {
image.drawRect(container.x + i, container.y + i, container.width - 2 * i, container.height - 2 * i, TEXT_WHITE);
}
}
}
function paintImageContainer(image: GrayImage, container: EvenHubImageContainer): void {
if (!container.pixels || container.pixelsWidth <= 0 || container.pixelsHeight <= 0) return;
const source = new GrayImage(container.pixelsWidth, container.pixelsHeight, 0);
source.pixels.set(container.pixels);
// Stock quirk: raw data smaller than the container tiles/repeats to fill it.
for (let ty = 0; ty < container.height; ty += container.pixelsHeight) {
for (let tx = 0; tx < container.width; tx += container.pixelsWidth) {
image.bitBlt(source, container.x + tx, container.y + ty, {
width: Math.min(container.pixelsWidth, container.width - tx),
height: Math.min(container.pixelsHeight, container.height - ty),
});
}
}
}
/** Horizontal padding between the selection outline and the item text. */
const LIST_SELECT_PADDING = 8;
const LIST_SELECT_RADIUS = 8;
function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void {
paintBorder(image, container);
const font = EvenHubFont.get();
const inset = container.borderWidth + container.paddingLength;
const rowHeight = font.lineHeight + LIST_ROW_PADDING;
const left = container.x + inset;
const top = container.y + inset;
const innerWidth = Math.max(1, container.width - 2 * inset);
// Text is indented by the selection padding so the outline sits inside the list.
const textX = left + LIST_SELECT_PADDING;
const visibleRows = Math.max(1, Math.floor((container.height - 2 * inset) / rowHeight));
// Keep the selection in view.
let firstRow = 0;
if (container.selectedIndex >= visibleRows) firstRow = container.selectedIndex - visibleRows + 1;
for (let row = 0; row < visibleRows; row++) {
const index = firstRow + row;
if (index >= container.itemNames.length) break;
const name = container.itemNames[index]!;
const y = top + row * rowHeight;
if (index === container.selectedIndex) {
// Selection: a rounded outline sized to the text (not the full row width),
// with padding on each side and no fill — matching stock's appearance.
const textWidth = font.measureLine(name);
image.drawRoundedRect(
left,
y,
Math.min(textWidth + 2 * LIST_SELECT_PADDING, innerWidth),
rowHeight - 1,
focused ? TEXT_WHITE : 130,
LIST_SELECT_RADIUS,
);
}
font.drawText(image, textX, y + Math.floor(LIST_ROW_PADDING / 2), name, TEXT_WHITE);
}
}
/** Bottom-to-top paint order for a page's containers. */
function paintOrder(containers: EvenHubContainer[]): EvenHubContainer[] {
const anyZ = containers.some((c) => c.zOrderIndex !== undefined);
if (anyZ) {
// Explicit z-order: larger zOrderIndex renders closer to the front, i.e.
// painted later. (Stock requires all-or-nothing + unique values.)
return [...containers].sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0));
}
// Default (no z-order): image containers sit below lists and text. Cross-type
// order isn't defined by the wire format (separate object arrays), so this is
// our choice, matching stock. Order within each group is preserved.
const images = containers.filter((c) => c.kind === "image");
const rest = containers.filter((c) => c.kind !== "image");
return [...images, ...rest];
}
/** Render a page into a fresh app-viewport image (positioned by the shell). */
export function compositePage(page: EvenHubPage | null, size: { width: number; height: number }, focused: boolean): GrayImage {
const image = new GrayImage(size.width, size.height, 0);
if (!page) return image;
for (const container of paintOrder(page.containers)) {
switch (container.kind) {
case "image":
// Text painted so far is deferred draws (firmware-text runs), which
// would otherwise render above this container's raster. Stock puts a
// later image container OVER earlier text, so bake the pending draws
// first; text painted after this container stays deferred (and above).
image.bakeDeferredDrawsInPlace();
paintImageContainer(image, container);
break;
case "text":
paintTextContainer(image, container);
break;
case "list":
paintListContainer(image, container, focused);
break;
}
}
return image;
}
+199
View File
@@ -0,0 +1,199 @@
/**
* EvenHub page-container model and lenient JSON parsing.
*
* Hosts and apps in the wild emit both camelCase and protobuf names
* (Container_ID), and numbers sometimes arrive as strings, so all field
* reads go through loose key matching (the SDK's pickLoose behavior).
*/
export type EvenHubTextContainer = {
kind: "text";
id: number;
name: string;
x: number;
y: number;
width: number;
height: number;
borderWidth: number;
borderRadius: number;
paddingLength: number;
isEventCapture: boolean;
zOrderIndex: number | undefined;
/** Extended-layout: inherit content from a same-named container on replace. */
preserve: boolean;
content: string;
};
export type EvenHubImageContainer = {
kind: "image";
id: number;
name: string;
x: number;
y: number;
width: number;
height: number;
zOrderIndex: number | undefined;
preserve: boolean;
/** Decoded 8bpp grayscale, set by updateImageRawData; null until first update. */
pixels: Uint8Array | null;
pixelsWidth: number;
pixelsHeight: number;
};
export type EvenHubListContainer = {
kind: "list";
id: number;
name: string;
x: number;
y: number;
width: number;
height: number;
borderWidth: number;
borderRadius: number;
paddingLength: number;
isEventCapture: boolean;
zOrderIndex: number | undefined;
preserve: boolean;
itemNames: string[];
itemWidth: number;
selectBorder: boolean;
/** Selection is host-local: scroll moves it with no app round-trip. */
selectedIndex: number;
};
export type EvenHubContainer = EvenHubTextContainer | EvenHubImageContainer | EvenHubListContainer;
export type EvenHubPage = {
/** In declaration order (lists, images, texts); z-sorted at paint time. */
containers: EvenHubContainer[];
};
function normalizeKey(key: string): string {
return key.replace(/_/g, "").toLowerCase();
}
function pickLoose(obj: Record<string, unknown>, key: string): unknown {
if (key in obj) return obj[key];
const wanted = normalizeKey(key);
for (const k of Object.keys(obj)) {
if (normalizeKey(k) === wanted) return obj[k];
}
return undefined;
}
export function readNumber(obj: Record<string, unknown>, key: string, fallback: number): number {
const value = pickLoose(obj, key);
if (typeof value === "number" && Number.isFinite(value)) return value;
if (typeof value === "string") {
const parsed = Number(value);
if (Number.isFinite(parsed)) return parsed;
}
return fallback;
}
export function readOptionalNumber(obj: Record<string, unknown>, key: string): number | undefined {
const value = readNumber(obj, key, Number.NaN);
return Number.isNaN(value) ? undefined : value;
}
export function readString(obj: Record<string, unknown>, key: string, fallback: string): string {
const value = pickLoose(obj, key);
if (typeof value === "string") return value;
if (typeof value === "number" || typeof value === "boolean") return String(value);
return fallback;
}
export function asRecord(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
}
function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer {
return {
kind: "text",
id: readNumber(json, "containerID", 0),
name: readString(json, "containerName", ""),
x: readNumber(json, "xPosition", 0),
y: readNumber(json, "yPosition", 0),
width: readNumber(json, "width", 0),
height: readNumber(json, "height", 0),
borderWidth: readNumber(json, "borderWidth", 0),
borderRadius: readNumber(json, "borderRadius", 0),
paddingLength: readNumber(json, "paddingLength", 0),
isEventCapture: readNumber(json, "isEventCapture", 0) !== 0,
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
preserve: readNumber(json, "preserve", 0) !== 0,
content: readString(json, "content", ""),
};
}
function parseImageContainer(json: Record<string, unknown>): EvenHubImageContainer {
return {
kind: "image",
id: readNumber(json, "containerID", 0),
name: readString(json, "containerName", ""),
x: readNumber(json, "xPosition", 0),
y: readNumber(json, "yPosition", 0),
width: readNumber(json, "width", 0),
height: readNumber(json, "height", 0),
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
preserve: readNumber(json, "preserve", 0) !== 0,
pixels: null,
pixelsWidth: 0,
pixelsHeight: 0,
};
}
function parseListContainer(json: Record<string, unknown>): EvenHubListContainer {
const itemContainer = asRecord(pickLoose(json, "itemContainer"));
const itemNamesRaw = pickLoose(itemContainer, "itemName");
const itemNames = Array.isArray(itemNamesRaw) ? itemNamesRaw.map(String) : [];
return {
kind: "list",
id: readNumber(json, "containerID", 0),
name: readString(json, "containerName", ""),
x: readNumber(json, "xPosition", 0),
y: readNumber(json, "yPosition", 0),
width: readNumber(json, "width", 0),
height: readNumber(json, "height", 0),
borderWidth: readNumber(json, "borderWidth", 0),
borderRadius: readNumber(json, "borderRadius", 0),
paddingLength: readNumber(json, "paddingLength", 0),
isEventCapture: readNumber(json, "isEventCapture", 0) !== 0,
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
preserve: readNumber(json, "preserve", 0) !== 0,
itemNames,
itemWidth: readNumber(itemContainer, "itemWidth", 0),
selectBorder: readNumber(itemContainer, "isItemSelectBorderEn", 0) !== 0,
selectedIndex: 0,
};
}
/**
* Parse a CreateStartUpPageContainer / RebuildPageContainer payload into a
* page. Containers keep declaration order within and across the three arrays
* (lists, then images, then texts — texts last so info panels and capture
* containers sit above image tiles when no zOrderIndex is given).
*/
export function parsePage(data: Record<string, unknown>): EvenHubPage {
const containers: EvenHubContainer[] = [];
const lists = pickLoose(data, "listObject");
if (Array.isArray(lists)) {
for (const item of lists) containers.push(parseListContainer(asRecord(item)));
}
const images = pickLoose(data, "imageObject");
if (Array.isArray(images)) {
for (const item of images) containers.push(parseImageContainer(asRecord(item)));
}
const texts = pickLoose(data, "textObject");
if (Array.isArray(texts)) {
for (const item of texts) containers.push(parseTextContainer(asRecord(item)));
}
return { containers };
}
/** The container that owns gesture events (exactly one per valid page). */
export function eventCaptureContainer(page: EvenHubPage): EvenHubContainer | undefined {
return page.containers.find(
(container) => container.kind !== "image" && container.isEventCapture,
);
}
+36
View File
@@ -0,0 +1,36 @@
import { ConfigSettingString } from "../../ui/dashboard-settings";
/**
* EvenHub owns its account configuration. The storage keys intentionally
* match the former Settings-app entries so existing users stay signed in.
*/
export const evenHubEmailSetting = new ConfigSettingString({
id: "evenhub-email",
label: "Email",
storageKey: "integrations.evenhub.email",
defaultValue: "",
editorTitle: "Email",
glassesEditTitle: "Edit Even email",
inputKind: "email",
normalize: (value) => (value ?? "").replace(/[\x00-\x1f]+/g, "").trim(),
});
export const evenHubPasswordSetting = new ConfigSettingString({
id: "evenhub-password",
label: "Password",
storageKey: "integrations.evenhub.password",
defaultValue: "",
editorTitle: "Password",
glassesEditTitle: "Edit Even password",
inputKind: "password",
normalize: (value) => (value ?? "").replace(/[\x00-\x1f]+/g, ""),
formatValue: (value) => (value ? "(set)" : "(not set)"),
});
export function hasEvenHubCredentials(): boolean {
return Boolean(evenHubEmailSetting.get().trim() && evenHubPasswordSetting.get());
}
export function clearEvenHubPassword(): void {
evenHubPasswordSetting.set("");
}
+151
View File
@@ -0,0 +1,151 @@
/**
* EvenHub .ehpk package unpacking.
*
* The .ehpk produced by `evenhub pack` (@evenrealities/evenhub-cli) is a
* custom record container holding the app's dist/ tree plus app.json, each
* entry zstd-compressed and XORed with the repeating ASCII key
* "EVEN REALITIES" (key index resets per field). Format details are in
* ../../../../ehpk-unpacker/ehpk_unpack.py, the reference implementation
* this ports. The trailing record is a SHA-512 of everything before it —
* an integrity check only, not a signature; we skip verifying it.
*/
import { decompress } from "fzstd";
import { parsePermissions, type EvenHubPermission } from "./permissions";
const XOR_KEY = "EVEN REALITIES";
const RECORD_FILE = 0xe4;
const RECORD_DIR = 0xe5;
const RECORD_FOOTER = 0xe3;
export type EhpkArchive = {
/** Relative path (e.g. "app.json", "dist/index.html") to contents. */
files: Map<string, Uint8Array>;
};
/** The app.json manifest fields Faceclaw cares about (both spec generations). */
export type EvenHubManifest = {
packageId: string;
name: string;
version: string;
/** Entry file relative to dist/ (default index.html). */
entrypoint: string;
/** Hosts the app declares it will reach; not yet enforced. */
networkWhitelist: string[];
/** Normalized declared permissions (both manifest shapes). */
permissions: EvenHubPermission[];
/** App privacy policy, when a locally supplied manifest declares one. */
privacyPolicyUrl: string;
raw: Record<string, unknown>;
};
function unxor(data: Uint8Array, start: number, length: number): Uint8Array {
const out = new Uint8Array(length);
for (let i = 0; i < length; i++) {
out[i] = data[start + i]! ^ XOR_KEY.charCodeAt(i % XOR_KEY.length);
}
return out;
}
export function utf8Decode(bytes: Uint8Array): string {
let out = "";
let i = 0;
while (i < bytes.length) {
const b = bytes[i]!;
if (b < 0x80) {
out += String.fromCharCode(b);
i += 1;
} else if (b < 0xe0) {
out += String.fromCharCode(((b & 0x1f) << 6) | (bytes[i + 1]! & 0x3f));
i += 2;
} else if (b < 0xf0) {
out += String.fromCharCode(((b & 0x0f) << 12) | ((bytes[i + 1]! & 0x3f) << 6) | (bytes[i + 2]! & 0x3f));
i += 3;
} else {
const cp =
((b & 0x07) << 18) | ((bytes[i + 1]! & 0x3f) << 12) | ((bytes[i + 2]! & 0x3f) << 6) | (bytes[i + 3]! & 0x3f);
out += String.fromCodePoint(cp);
i += 4;
}
}
return out;
}
export function parseEhpk(data: Uint8Array): EhpkArchive {
if (data.length < 20 || data[0] !== 0x45 || data[1] !== 0x48 || data[2] !== 0x50 || data[3] !== 0x4b) {
throw new Error("not an EHPK file");
}
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
const firstRecord = view.getUint32(8, true);
const files = new Map<string, Uint8Array>();
let p = firstRecord;
while (p < data.length) {
const type = data[p]!;
if (data[p + 1] !== 0xba || data[p + 2] !== 0xa9 || data[p + 3] !== 0xba) {
throw new Error(`ehpk: bad record magic at ${p}`);
}
if (type === RECORD_FILE) {
const compressedLength = view.getUint32(p + 4, true);
const uncompressedLength = view.getUint32(p + 8, true);
const nameLength = view.getUint16(p + 14, true);
const name = utf8Decode(unxor(data, p + 16, nameLength));
const blob = unxor(data, p + 16 + nameLength, compressedLength);
const content = decompress(blob, new Uint8Array(uncompressedLength));
if (content.length !== uncompressedLength) {
throw new Error(`ehpk: ${name}: expected ${uncompressedLength} bytes, got ${content.length}`);
}
files.set(name, content);
p += 16 + nameLength + compressedLength;
} else if (type === RECORD_DIR) {
const nameLength = view.getUint16(p + 6, true);
p += 8 + nameLength;
} else if (type === RECORD_FOOTER) {
break;
} else {
throw new Error(`ehpk: unknown record type 0x${type.toString(16)} at ${p}`);
}
}
return { files };
}
/**
* Parse app.json, accepting both the older manifest shape (permissions as a
* {"network": [...]} map) and the current one (array of {name, whitelist?}).
*/
export function parseManifest(appJsonText: string): EvenHubManifest {
const raw = JSON.parse(appJsonText) as Record<string, unknown>;
const packageId = String(raw.package_id ?? raw.packageId ?? "unknown.package");
const name = String(raw.name ?? packageId);
const version = String(raw.version ?? "0");
const entrypoint = String(raw.entrypoint ?? "index.html");
const permissions = parsePermissions(raw.permissions);
const privacyPolicyUrl = firstSafeHttpsUrl(
raw.privacy_link,
raw.privacyLink,
raw.privacy_policy_url,
raw.privacyPolicyUrl,
);
const networkWhitelist: string[] = [];
for (const permission of permissions) {
if (permission.name === "network" && permission.whitelist) {
networkWhitelist.push(...permission.whitelist);
}
}
return { packageId, name, version, entrypoint, networkWhitelist, permissions, privacyPolicyUrl, raw };
}
function firstSafeHttpsUrl(...values: unknown[]): string {
for (const value of values) {
if (typeof value !== "string" || value.length > 2048) continue;
try {
const url = new URL(value.trim());
if (url.protocol === "https:") return url.toString();
} catch {
// Try the next supported manifest spelling.
}
}
return "";
}
+416
View File
@@ -0,0 +1,416 @@
/**
* Client for the private storefront endpoints used by the official Even app.
*
* This is intentionally separate from the EvenHub JS compatibility bridge:
* it only discovers and downloads public .ehpk packages. Authentication is
* the same signed-header scheme used by the firmware API.
*/
import { Application } from "@nativescript/core";
import { evenHubEmailSetting, evenHubPasswordSetting, hasEvenHubCredentials } from "./credentials";
declare const android: any;
const API_HOST = "https://api.evenrealities.com";
const PUBLIC_CDN_HOST = "https://cdn-pub.evenhub.evenrealities.com";
const SIGN_KEY = "a7964f42c39200cfa25c258b7a311b106e20232173667e543c34ced91d63b404";
const REQUEST_TIMEOUT_MS = 30_000;
export type EvenHubStoreApp = {
id: number;
packageId: string;
name: string;
creatorName: string;
tagline: string;
description: string;
categories: string[];
installCount: number;
likeCount: number;
firstPublishedAt: string;
/** Public-CDN-relative SVG/raster artwork path. */
iconPath: string;
version: string;
changelog: string;
fileSize: number;
};
export type EvenHubStorePage = {
apps: EvenHubStoreApp[];
total: number;
page: number;
pageSize: number;
};
type ApiEnvelope = {
code?: number;
msg?: string;
data?: unknown;
};
type DownloadMetadata = {
url?: unknown;
size?: unknown;
public_key?: unknown;
privacy_link?: unknown;
meta?: unknown;
};
export type EvenHubAppDownload = {
bytes: Uint8Array;
privacyPolicyUrl: string;
};
export function isEvenHubStoreConfigured(): boolean {
return hasEvenHubCredentials();
}
export class EvenHubAuthenticationError extends Error {
constructor(message: string) {
super(message);
this.name = "EvenHubAuthenticationError";
}
}
export class EvenHubApiClient {
private token = "";
private loginPromise: Promise<string> | null = null;
async listApps(page = 1, category = ""): Promise<EvenHubStorePage> {
const query: Record<string, string | number> = { page, page_size: 40 };
if (category) query.category = category;
const data = await this.authenticatedRequest("GET", "/v2/evenhub/leaderboard", { query });
const root = asRecord(data);
const list = Array.isArray(root.list) ? root.list : [];
return {
apps: list.map(parseStoreApp).filter((app): app is EvenHubStoreApp => app !== null),
total: finiteNumber(root.total),
page: finiteNumber(root.page) || page,
pageSize: finiteNumber(root.page_size) || 40,
};
}
async getAppDetail(packageId: string): Promise<Record<string, unknown>> {
const data = await this.authenticatedRequest("GET", "/v2/evenhub/app/detail", {
query: { package_id: packageId, branch_name: "public" },
});
return asRecord(data);
}
async getStoreAppDetail(packageId: string): Promise<EvenHubStoreApp | null> {
return parseStoreApp(await this.getAppDetail(packageId));
}
/** Resolve the signed download URL, then fetch and sanity-check its EHPK. */
async downloadApp(packageId: string): Promise<EvenHubAppDownload> {
const data = await this.authenticatedRequest("POST", "/v2/evenhub/app/download", {
body: { package_id: packageId, branch: "public" },
});
const metadata = asRecord(data) as DownloadMetadata;
const url = typeof metadata.url === "string" ? metadata.url : "";
if (!url.startsWith("https://")) throw new Error("EvenHub returned no package download URL.");
const response = await fetchWithTimeout(url, {}, 60_000);
if (!response.ok) throw new Error(`EvenHub package download failed (HTTP ${response.status}).`);
const bytes = new Uint8Array(await response.arrayBuffer());
const expectedSize = finiteNumber(metadata.size);
if (expectedSize > 0 && bytes.length !== expectedSize) {
throw new Error(`EvenHub package was truncated (${bytes.length} of ${expectedSize} bytes).`);
}
if (bytes.length < 20 || bytes[0] !== 0x45 || bytes[1] !== 0x48 || bytes[2] !== 0x50 || bytes[3] !== 0x4b) {
throw new Error("EvenHub download is not an EHPK package.");
}
return {
bytes,
// The production API currently returns a CDN-relative `privacy_link`
// at the response root. Accept the nested serialization shape too.
privacyPolicyUrl: resolvePrivacyPolicyUrl(
stringValue(metadata.privacy_link).trim()
? metadata.privacy_link
: asRecord(metadata.meta).privacy_link,
),
};
}
/** Download one public storefront asset, constrained to Even's CDN path. */
async downloadPublicAsset(path: string): Promise<{ bytes: Uint8Array; extension: string }> {
const cleanPath = path.replace(/^\/+/, "");
if (!/^prod\/[A-Za-z0-9._~!$&'()*+,;=:@%/-]+\.(?:svg|png|webp|jpe?g)$/i.test(cleanPath) || cleanPath.includes("..")) {
throw new Error("EvenHub returned an invalid public asset path.");
}
const response = await fetchWithTimeout(`${PUBLIC_CDN_HOST}/${cleanPath}`, {}, 30_000);
if (!response.ok) throw new Error(`EvenHub icon download failed (HTTP ${response.status}).`);
const contentLength = Number(response.headers.get("Content-Length") ?? 0);
if (contentLength > 1_000_000) throw new Error("EvenHub icon is unexpectedly large.");
const bytes = new Uint8Array(await response.arrayBuffer());
if (bytes.length === 0 || bytes.length > 1_000_000) throw new Error("EvenHub icon is empty or too large.");
const extension = cleanPath.toLowerCase().match(/\.([a-z0-9]+)$/)?.[1] ?? "";
return { bytes, extension: extension === "jpeg" ? "jpg" : extension };
}
clearSession(): void {
this.token = "";
this.loginPromise = null;
}
private async authenticatedRequest(
method: "GET" | "POST",
path: string,
options: { query?: Record<string, string | number>; body?: Record<string, unknown> } = {},
): Promise<unknown> {
let token = await this.ensureLogin();
let response = await this.request(method, path, { ...options, token, commonVersion: 3 });
if (response.httpStatus === 401) {
this.clearSession();
token = await this.ensureLogin();
response = await this.request(method, path, { ...options, token, commonVersion: 3 });
}
return unwrap(response.envelope, response.httpStatus, path);
}
private ensureLogin(): Promise<string> {
if (this.token) return Promise.resolve(this.token);
if (!isEvenHubStoreConfigured()) {
return Promise.reject(new EvenHubAuthenticationError("Enter your Even account email and password."));
}
if (!this.loginPromise) {
this.loginPromise = this.login().finally(() => {
this.loginPromise = null;
});
}
return this.loginPromise;
}
private async login(): Promise<string> {
try {
const response = await this.request("POST", "/v2/g/login", {
commonVersion: 1,
body: { email: evenHubEmailSetting.get().trim(), passwd: evenHubPasswordSetting.get() },
});
const data = asRecord(unwrap(response.envelope, response.httpStatus, "/v2/g/login"));
const token = typeof data.token === "string" ? data.token : "";
if (!token) throw new Error("Even login succeeded without returning a session token.");
this.token = token;
return token;
} catch (error) {
if (error instanceof EvenHubAuthenticationError) throw error;
throw new EvenHubAuthenticationError(String((error as Error)?.message ?? error));
}
}
private async request(
method: "GET" | "POST",
path: string,
options: {
commonVersion: 1 | 3;
query?: Record<string, string | number>;
body?: Record<string, unknown>;
token?: string;
},
): Promise<{ httpStatus: number; envelope: ApiEnvelope }> {
const common = buildCommon(options.commonVersion);
const query = encodePairs(options.query ?? {});
const body = options.body ? JSON.stringify(options.body) : "";
const token = options.token ?? "";
const headers: Record<string, string> = {
common,
sign: hmacSign(signingParts(method, path, common, query, token, body)),
"Content-Type": "application/json",
};
if (token) headers.token = token;
const response = await fetchWithTimeout(`${API_HOST}${path}${query ? `?${query}` : ""}`, {
method,
headers,
body: body || undefined,
});
let envelope: ApiEnvelope = {};
try {
envelope = (await response.json()) as ApiEnvelope;
} catch {
// unwrap() below will turn this into a useful status-only error.
}
return { httpStatus: response.status, envelope };
}
}
export const evenHubApi = new EvenHubApiClient();
/** Exported for deterministic unit/probe comparison without exposing secrets. */
export function signingParts(
method: string,
path: string,
common: string,
query = "",
token = "",
body = "",
): string {
const parts = [method.toUpperCase(), path, common];
if (query) parts.push(normalizeQuery(query));
if (token) parts.push(token);
if (body) parts.push(body);
return parts.sort().join("\n");
}
function buildCommon(version: 1 | 3): string {
const fields: Record<string, string | number> = {
platform: "16",
package: "com.even.sg",
versionName: "2.2.6",
build: "114",
brand: "Google",
model: "Pixel 7a",
osVersion: "16",
carrier: "",
mcc: "310",
mnc: "260",
buildTime: "26060821",
appId: "1001",
v: version,
// This is Android's app-scoped SSAID. Even's login endpoint registers a
// previously unseen value as a terminal, so Faceclaw does not need the
// official Even app's differently-scoped ID.
openUdid: getPhoneOpenUdid(),
os: "1",
sn: "",
verL: "",
verR: "",
ringSn: "",
ringVer: "",
channel: "googlePlay",
sttLanguage: "",
sysLanguage: "US",
ts: new Date().toISOString(),
language: "en",
tzName: "America/Los_Angeles",
dateFmt: "yyyy/MM/dd",
timeFmt: "24",
unit: "metrics",
region: "US",
};
// `common` field order matches the official client; unlike queries it must
// not be sorted because the raw serialized value participates in signing.
return Object.entries(fields).map(([key, value]) => `${formEncode(key)}=${formEncode(String(value))}`).join("&");
}
function encodePairs(values: Record<string, string | number>): string {
return Object.entries(values)
.sort(([a], [b]) => a.localeCompare(b))
.map(([key, value]) => `${formEncode(key)}=${formEncode(String(value))}`)
.join("&");
}
function normalizeQuery(query: string): string {
return query
.split("&")
.filter(Boolean)
.sort((a, b) => a.split("=", 1)[0]!.localeCompare(b.split("=", 1)[0]!))
.join("&");
}
function formEncode(value: string): string {
return encodeURIComponent(value).replace(/%20/g, "+");
}
function hmacSign(message: string): string {
if (!global.isAndroid) throw new Error("The EvenHub storefront is currently available only on Android.");
const charset = java.nio.charset.StandardCharsets.UTF_8;
const key = new javax.crypto.spec.SecretKeySpec(new java.lang.String(SIGN_KEY).getBytes(charset), "HmacSHA256");
const mac = javax.crypto.Mac.getInstance("HmacSHA256");
mac.init(key);
const digest = mac.doFinal(new java.lang.String(message).getBytes(charset));
return String(android.util.Base64.encodeToString(digest, android.util.Base64.NO_WRAP));
}
async function fetchWithTimeout(url: string, init: RequestInit, timeoutMs = REQUEST_TIMEOUT_MS): Promise<Response> {
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
const timeout = new Promise<never>((_resolve, reject) => {
timeoutHandle = setTimeout(() => reject(new Error("EvenHub request timed out.")), timeoutMs);
});
try {
return await Promise.race([fetch(url, init), timeout]);
} finally {
if (timeoutHandle) clearTimeout(timeoutHandle);
}
}
function unwrap(envelope: ApiEnvelope, httpStatus: number, path: string): unknown {
if (httpStatus === 401) {
throw new EvenHubAuthenticationError("Even rejected the login session.");
}
if (httpStatus < 200 || httpStatus >= 300) {
throw new Error(`EvenHub request failed (HTTP ${httpStatus}, ${path}).`);
}
if (envelope.code !== 0) {
const detail = typeof envelope.msg === "string" && envelope.msg ? `: ${envelope.msg}` : "";
throw new Error(`EvenHub API error ${envelope.code ?? "unknown"}${detail}`);
}
return envelope.data;
}
/** Return this installation's Android ID, which Even calls `openUdid`. */
export function getPhoneOpenUdid(): string {
if (!global.isAndroid) throw new Error("The EvenHub storefront is currently available only on Android.");
const resolver = Application.android.context?.getContentResolver();
const openUdid = resolver
? String(android.provider.Settings.Secure.getString(resolver, android.provider.Settings.Secure.ANDROID_ID) ?? "")
: "";
if (!openUdid) throw new Error("Android did not provide a device ID for EvenHub authentication.");
return openUdid;
}
function parseStoreApp(value: unknown): EvenHubStoreApp | null {
const item = asRecord(value);
const version = asRecord(item.version);
const packageId = stringValue(item.package_id);
if (!packageId) return null;
return {
id: finiteNumber(item.id),
packageId,
name: stringValue(item.name) || packageId,
creatorName: stringValue(item.creator_name),
tagline: stringValue(item.tagline),
description: stringValue(item.description),
categories: Array.isArray(item.category) ? item.category.map(stringValue).filter(Boolean) : [],
installCount: finiteNumber(item.install_count),
likeCount: finiteNumber(item.like_count),
firstPublishedAt: stringValue(item.first_published_at || item.created_at),
iconPath: stringValue(item.icon),
version: stringValue(version.version || item.latest_version),
changelog: stringValue(version.changelog),
fileSize: finiteNumber(version.file_size),
};
}
function asRecord(value: unknown): Record<string, any> {
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, any>) : {};
}
function stringValue(value: unknown): string {
return typeof value === "string" ? value : value == null ? "" : String(value);
}
function finiteNumber(value: unknown): number {
const number = Number(value);
return Number.isFinite(number) ? number : 0;
}
function resolvePrivacyPolicyUrl(value: unknown): string {
if (typeof value !== "string" || value.length > 2048) return "";
const candidate = value.trim();
if (!candidate) return "";
try {
const url = new URL(candidate);
return url.protocol === "https:" ? url.toString() : "";
} catch {
// Even stores uploaded policies as public-CDN-relative paths, just like
// app icons. Keep relative resolution constrained to that CDN namespace.
const cleanPath = candidate.replace(/^\/+/, "");
if (
!/^prod\/[A-Za-z0-9._~!$&'()*+,;=:@%/-]+$/i.test(cleanPath)
|| cleanPath.includes("..")
|| candidate.startsWith("//")
|| candidate.includes("\\")
) {
return "";
}
return `${PUBLIC_CDN_HOST}/${cleanPath}`;
}
}
+94
View File
@@ -0,0 +1,94 @@
/**
* The glasses-side window for a running EvenHub app: a layer that paints the
* session's composited page and forwards gestures to it. heightMode "medium"
* makes the content area exactly the 576x288 surface EvenHub apps expect.
*/
import { GrayImage } from "../../graphics/image";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
import {
createInProcessWindow,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
import { EvenHubSession } from "./session";
import { renderInstalledEvenHubIcon } from "./installed-apps";
class EvenHubAppLayer implements Layer {
constructor(private readonly session: EvenHubSession) {}
paint(ctx: LayerContext): GrayImage {
const size = ctx.stack.getBaseSize();
return this.session.paint(size, ctx.stack.isFocused());
}
handleInput(event: DashboardInputEvent): void {
// Everything goes to the app; long-press never reaches here (the window
// menu intercepts it), which is the guaranteed way out since EvenHub
// apps own double-click.
this.session.handleGesture(event);
}
}
export function createEvenHubWindow(
windowId: string,
appId: string,
session: EvenHubSession,
options: InProcessAppOptions,
onShowPhone: () => void,
): InProcessWindow {
const fallbackIcon = windowIcon("package", session.manifest.name.charAt(0).toUpperCase() || "E");
const created = createInProcessWindow({
appId,
windowId,
title: session.manifest.name,
iconLetter: session.manifest.name.charAt(0).toUpperCase() || "E",
closeable: true,
drawIcon: makeImageWindowIcon(
(size) => renderInstalledEvenHubIcon(session.manifest.packageId, size),
fallbackIcon,
),
heightMode: "medium",
// Glasses-first: the app runs without the phone showing it; this reveals
// its phone UI (config pages, etc.) over the dashboard on demand.
menuItems: () => [{ label: "Show phone UI", onSelect: (ctx) => { ctx.stack.pop(); onShowPhone(); } }],
actions: options.actions,
baseLayer: new EvenHubAppLayer(session),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
reconfigureSurface: options.reconfigureSurface,
onClosed: () => {
options.onClosed();
session.windowClosed();
},
});
session.attachWindow({
requestRender: created.requestRender,
closeWindow: () => shell.closeWindow(windowId),
focusSwitcher: () => shell.yieldFocusToSidebar(),
pushOverlay: (layer) => created.stack.push(layer),
// The extended layout uses the full 576x452 app area ("max"); stock apps
// stay in the 576x288 band ("medium").
setTallCanvas: (tall) => created.setHeightMode(tall ? "max" : "medium"),
windowId,
});
// FOREGROUND_ENTER/EXIT for the app on shell focus changes.
const baseSetForeground = created.window.setForeground;
created.window.setForeground = (foreground) => {
baseSetForeground?.(foreground);
session.setForeground(foreground);
};
// Screen on/off gates the app's microphone (silent while the screen is off).
const baseSetScreenOn = created.window.setScreenOn;
created.window.setScreenOn = (on) => {
baseSetScreenOn?.(on);
session.setScreenOn(on);
};
return created;
}
+75
View File
@@ -0,0 +1,75 @@
/**
* Arbitrates the shared G2 accelerometer (cmd-19 IMU report stream) among
* running EvenHub apps.
*
* imuControl(true) registers an app as wanting IMU; only the foreground
* (active) window actually receives readings, so a backgrounded app can't keep
* the flaky, battery-costly BLE stream running. Unlike the mic this is NOT
* gated on screen state — head-gesture apps legitimately want IMU while the
* display is blanked. Because only one window is foreground, at most one app is
* ever the active reader, which also avoids two apps fighting over the single
* global report rate.
*
* The IMU stream is historically unreliable (the firmware may not populate it;
* see the imu-accelerometer memory) — treat it as best-effort.
*/
import { addImuListener, setImuReportEnabled, type ImuReading } from "../../native/imu";
export type EvenHubImuClient = {
readonly windowId: string;
isForeground(): boolean;
deliverImu(reading: ImuReading): void;
};
class EvenHubImuRouter {
private readonly requesting = new Map<EvenHubImuClient, number>();
private active: EvenHubImuClient | null = null;
private unsubscribe: (() => void) | null = null;
/** The report rate the stream is currently enabled at; 0 = disabled. */
private enabledFreq = 0;
/** An app enabled IMU (imuControl true) at the given report rate. */
requestImu(client: EvenHubImuClient, freq: number): void {
this.requesting.set(client, freq);
this.evaluate();
}
/** An app disabled IMU, backgrounded past use, or closed. */
releaseImu(client: EvenHubImuClient): void {
if (!this.requesting.delete(client)) return;
this.evaluate();
}
/** A requesting app's foreground state changed. */
notifyEligibilityChanged(): void {
this.evaluate();
}
private eligible(): EvenHubImuClient | null {
return Array.from(this.requesting.keys()).find((client) => client.isForeground()) ?? null;
}
private evaluate(): void {
const next = this.eligible();
this.active = next;
if (next) {
if (!this.unsubscribe) {
this.unsubscribe = addImuListener((reading) => this.active?.deliverImu(reading));
}
const freq = this.requesting.get(next) ?? 100;
if (this.enabledFreq !== freq) {
setImuReportEnabled(true, freq);
this.enabledFreq = freq;
}
} else {
if (this.enabledFreq !== 0) {
setImuReportEnabled(false, 0);
this.enabledFreq = 0;
}
this.unsubscribe?.();
this.unsubscribe = null;
}
}
}
export const evenHubImuRouter = new EvenHubImuRouter();
+71
View File
@@ -0,0 +1,71 @@
/**
* EvenHub compatibility host: runs apps built for Even Realities' EvenHub
* API (webview apps talking to the glasses through the phone) inside
* Faceclaw. Launched by picking an .ehpk file in the Files app; the app gets
* a phone WebView (its runtime + phone UI) and a 576x288 glasses window
* composited by Faceclaw instead of the stock firmware.
*
* Design notes: notes/evenhub_compatibility.txt.
*/
import { type AppContext, type AppDefinition } from "../app-definition";
import { shell } from "../../ui/shell/shell";
import { EvenHubStoreLayer } from "./store-layer";
import { launchPackage } from "./manager";
import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window";
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
const EVENHUB_STORE_WINDOW_ID = "evenhub:store";
const EVENHUB_STORE_SURFACE_ID = "window:evenhub:store";
/**
* Unpack an .ehpk and launch it as a concurrent EvenHub app (own glasses
* window + persistent WebView). The phone stays on the dashboard; see manager.
*/
export function openEvenHubPackage(ctx: AppContext, ehpkPath: string): Promise<void> {
return launchPackage(ctx, ehpkPath);
}
const evenhubApp: AppDefinition = {
appId: "evenhub",
title: "EvenHub",
icon: "package",
renderIcon: renderEvenRealitiesLogo,
launch: async (ctx) => {
const existing = shell.getWindows().find((window) => window.windowId === EVENHUB_STORE_WINDOW_ID);
if (existing) {
shell.focusWindow(existing.windowId);
ctx.requestShellRender();
return;
}
let created: InProcessWindow | null = null;
const store = new EvenHubStoreLayer({
launchApp: (appId) => ctx.launchApp(appId),
appendLog: ctx.appendLog,
});
await ctx.launchInProcessApp(EVENHUB_STORE_WINDOW_ID, EVENHUB_STORE_SURFACE_ID, (options) => {
created = createInProcessWindow({
appId: "evenhub",
windowId: EVENHUB_STORE_WINDOW_ID,
title: "EvenHub",
iconLetter: "EH",
icon: "package",
drawIcon: makeImageWindowIcon(renderEvenRealitiesLogo, windowIcon("package", "EH")),
closeable: true,
menuItems: () => (created?.stack.isAtBase() ? store.buildMenuItems() : []),
actions: options.actions,
baseLayer: store,
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
store.closeCredentialEditor(options.actions);
options.onClosed();
},
});
return created;
});
},
};
export default evenhubApp;
+315
View File
@@ -0,0 +1,315 @@
/**
* Persistent registry and package storage for user-installed EvenHub apps.
*
* The EHPK itself is the source of truth for the runnable app. A small JSON
* index in Faceclaw's settings store makes installed packages discoverable by
* the launcher without expanding the static built-in AppDefinition registry.
*/
import {
appFilesDirPath,
deletePathRecursively,
readBinaryFile,
readTextFile,
writeBinaryFile,
} from "../../native/file-access";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import { loadImageFileAsGray } from "../../native/image-files";
import { renderSvgIcon } from "../../graphics/icons";
import { type GrayImage } from "../../graphics/image";
import { parseEhpk, parseManifest, utf8Decode, type EhpkArchive, type EvenHubManifest } from "./ehpk";
const STORAGE_KEY = "evenhub.installedApps.v1";
const APP_ID_PREFIX = "evenhub-installed:";
export type InstalledEvenHubApp = {
packageId: string;
name: string;
version: string;
installedAt: string;
/** Filename beneath the installed package directory, when artwork is available. */
iconFile?: string;
};
export type EvenHubInstallIcon = {
bytes: Uint8Array;
extension: string;
};
const renderedIconCache = new Map<string, GrayImage | null>();
export function installedEvenHubAppId(packageId: string): string {
return `${APP_ID_PREFIX}${encodeURIComponent(packageId)}`;
}
export function installedEvenHubPackageId(appId: string): string | null {
if (!appId.startsWith(APP_ID_PREFIX)) return null;
try {
const packageId = decodeURIComponent(appId.slice(APP_ID_PREFIX.length));
return packageId || null;
} catch {
return null;
}
}
export function getInstalledEvenHubApps(): InstalledEvenHubApp[] {
const raw = getStringSetting(STORAGE_KEY, "");
if (!raw) return [];
try {
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
const apps: InstalledEvenHubApp[] = [];
const seen = new Set<string>();
for (const value of parsed) {
if (!value || typeof value !== "object") continue;
const item = value as Record<string, unknown>;
const packageId = stringValue(item.packageId).trim();
if (!packageId || seen.has(packageId)) continue;
seen.add(packageId);
apps.push({
packageId,
name: stringValue(item.name).trim() || packageId,
version: stringValue(item.version).trim() || "0",
installedAt: stringValue(item.installedAt),
iconFile: safeIconFilename(stringValue(item.iconFile)),
});
}
return apps.sort((a, b) => a.name.localeCompare(b.name));
} catch {
return [];
}
}
export function getInstalledEvenHubApp(packageId: string): InstalledEvenHubApp | null {
return getInstalledEvenHubApps().find((app) => app.packageId === packageId) ?? null;
}
export function getInstalledEvenHubAppById(appId: string): InstalledEvenHubApp | null {
const packageId = installedEvenHubPackageId(appId);
return packageId ? getInstalledEvenHubApp(packageId) : null;
}
export function installedEvenHubPackagePath(packageId: string): string {
return `${installedRoot(packageId)}/package.ehpk`;
}
/** Validate and copy an EHPK into Faceclaw's app-private installed store. */
export function installEvenHubPackageBytes(
bytes: Uint8Array,
options: { expectedPackageId?: string; icon?: EvenHubInstallIcon } = {},
): InstalledEvenHubApp {
const { archive, manifest } = inspectEhpk(bytes);
if (!manifest.packageId || manifest.packageId === "unknown.package") {
throw new Error("The EHPK manifest has no package ID.");
}
if (options.expectedPackageId && manifest.packageId !== options.expectedPackageId) {
throw new Error(`Downloaded package ID ${manifest.packageId} does not match ${options.expectedPackageId}.`);
}
const root = installedRoot(manifest.packageId);
const path = installedEvenHubPackagePath(manifest.packageId);
if (!writeBinaryFile(path, bytes)) {
throw new Error(`Could not store ${manifest.name}.`);
}
const previous = getInstalledEvenHubApp(manifest.packageId);
const icon = normalizeInstallIcon(options.icon) ?? findEmbeddedIcon(archive, manifest);
let iconFile = previous?.iconFile;
if (icon) {
const candidate = `icon.${icon.extension}`;
if (writeBinaryFile(`${root}/${candidate}`, icon.bytes)) {
iconFile = candidate;
clearStoredIcons(root, candidate);
} else {
console.warn(`evenhub: could not store icon for ${manifest.packageId}`);
}
}
clearRenderedIconCache(manifest.packageId);
const installed: InstalledEvenHubApp = {
packageId: manifest.packageId,
name: manifest.name,
version: manifest.version,
installedAt: new Date().toISOString(),
iconFile: iconFile && readBinaryFile(`${root}/${iconFile}`) ? iconFile : undefined,
};
const apps = getInstalledEvenHubApps().filter((app) => app.packageId !== installed.packageId);
apps.push(installed);
saveInstalledApps(apps);
return installed;
}
/** Validate and install an EHPK selected in Files. */
export function installEvenHubPackageFile(path: string): InstalledEvenHubApp {
const bytes = readBinaryFile(path);
if (!bytes) throw new Error("Could not read the EHPK file.");
return installEvenHubPackageBytes(bytes);
}
/**
* Read an .ehpk's manifest without installing it — used by the Files app to
* show the permission-confirmation dialog before install/run. Returns null if
* the file can't be read or parsed.
*/
export function readEvenHubPackageManifest(path: string): EvenHubManifest | null {
const bytes = readBinaryFile(path);
if (!bytes) return null;
try {
return inspectEhpk(bytes).manifest;
} catch {
return null;
}
}
/** Inspect already-loaded package bytes before installation/first-run approval. */
export function readEvenHubPackageManifestBytes(bytes: Uint8Array): EvenHubManifest | null {
try {
return inspectEhpk(bytes).manifest;
} catch {
return null;
}
}
/** Remove the stored EHPK, unpacked runtime files, and registry entry. */
export function uninstallEvenHubPackage(packageId: string): boolean {
const apps = getInstalledEvenHubApps();
if (!apps.some((app) => app.packageId === packageId)) return false;
saveInstalledApps(apps.filter((app) => app.packageId !== packageId));
deletePathRecursively(installedRoot(packageId));
deletePathRecursively(`${appFilesDirPath()}/evenhub-apps/${safePackageId(packageId)}`);
clearRenderedIconCache(packageId);
return true;
}
/** Add or replace artwork for an existing install (used to backfill old installs). */
export function setInstalledEvenHubIcon(packageId: string, icon: EvenHubInstallIcon): boolean {
const normalized = normalizeInstallIcon(icon);
const apps = getInstalledEvenHubApps();
const index = apps.findIndex((app) => app.packageId === packageId);
if (!normalized || index < 0) return false;
const root = installedRoot(packageId);
const iconFile = `icon.${normalized.extension}`;
if (!writeBinaryFile(`${root}/${iconFile}`, normalized.bytes)) return false;
clearStoredIcons(root, iconFile);
apps[index] = { ...apps[index]!, iconFile };
clearRenderedIconCache(packageId);
saveInstalledApps(apps);
return true;
}
/** Render an installed app's SVG/raster artwork at launcher/sidebar size. */
export function renderInstalledEvenHubIcon(
packageId: string,
size: number,
installedApp?: InstalledEvenHubApp,
): GrayImage | null {
const app = installedApp ?? getInstalledEvenHubApp(packageId);
if (!app?.iconFile) return null;
const cacheKey = `${packageId}:${app.installedAt}:${app.iconFile}:${Math.round(size)}`;
if (renderedIconCache.has(cacheKey)) return renderedIconCache.get(cacheKey) ?? null;
const path = `${installedRoot(packageId)}/${app.iconFile}`;
let image: GrayImage | null = null;
if (app.iconFile.endsWith(".svg")) {
const svg = readTextFile(path);
if (svg) image = renderSvgIcon(cacheKey, svg, size);
} else {
image = loadImageFileAsGray(path, size, size);
}
renderedIconCache.set(cacheKey, image);
return image;
}
/** Raw registry value, suitable for cheap launcher change detection. */
export function getInstalledEvenHubFingerprint(): string {
return getStringSetting(STORAGE_KEY, "");
}
function inspectEhpk(bytes: Uint8Array): { archive: EhpkArchive; manifest: EvenHubManifest } {
const archive = parseEhpk(bytes);
const appJson = archive.files.get("app.json");
if (!appJson) throw new Error("The EHPK package has no app.json manifest.");
return { archive, manifest: parseManifest(utf8Decode(appJson)) };
}
function installedRoot(packageId: string): string {
return `${appFilesDirPath()}/evenhub-installed/${safePackageId(packageId)}`;
}
function safePackageId(packageId: string): string {
return packageId.replace(/[^A-Za-z0-9._-]/g, "_");
}
function saveInstalledApps(apps: InstalledEvenHubApp[]): void {
setStringSetting(
STORAGE_KEY,
JSON.stringify(apps.slice().sort((a, b) => a.name.localeCompare(b.name))),
);
}
function findEmbeddedIcon(archive: EhpkArchive, manifest: EvenHubManifest): EvenHubInstallIcon | null {
const candidates: string[] = [];
for (const key of ["icon", "icon_path", "iconPath", "app_icon", "appIcon"]) {
const value = manifest.raw[key];
if (typeof value === "string") candidates.push(value);
}
const entrypoint = archive.files.get(`dist/${manifest.entrypoint}`);
if (entrypoint) {
const html = utf8Decode(entrypoint);
for (const tag of html.match(/<link\b[^>]*>/gi) ?? []) {
if (!/\brel\s*=\s*["'][^"']*icon/i.test(tag)) continue;
const href = tag.match(/\bhref\s*=\s*["']([^"']+)/i)?.[1];
if (href) candidates.push(href);
}
}
candidates.push(
"icon.svg", "icon.png", "favicon.svg", "favicon.png",
"dist/icon.svg", "dist/icon.png", "dist/favicon.svg", "dist/favicon.png",
);
for (const candidate of candidates) {
const normalized = normalizeArchivePath(candidate);
if (!normalized) continue;
for (const path of [normalized, `dist/${normalized}`]) {
const content = archive.files.get(path);
const extension = iconExtension(path);
if (content && extension) return { bytes: content, extension };
}
}
const fallback = Array.from(archive.files.entries()).find(([path]) =>
/(?:^|\/)(?:app-?)?(?:icon|favicon)\.(?:svg|png|webp|jpe?g)$/i.test(path),
);
const extension = fallback ? iconExtension(fallback[0]) : null;
return fallback && extension ? { bytes: fallback[1], extension } : null;
}
function normalizeInstallIcon(icon: EvenHubInstallIcon | undefined): EvenHubInstallIcon | null {
if (!icon || icon.bytes.length === 0 || icon.bytes.length > 1_000_000) return null;
const extension = iconExtension(`icon.${icon.extension}`);
return extension ? { bytes: icon.bytes, extension } : null;
}
function normalizeArchivePath(path: string): string {
const clean = path.split(/[?#]/, 1)[0]!.replace(/^\.\//, "").replace(/^\//, "");
return !clean || clean.includes("..") || /^[a-z]+:/i.test(clean) ? "" : clean;
}
function iconExtension(path: string): string | null {
const match = path.toLowerCase().match(/\.(svg|png|webp|jpe?g)$/);
return match ? (match[1] === "jpeg" ? "jpg" : match[1]!) : null;
}
function safeIconFilename(value: string): string | undefined {
return /^icon\.(?:svg|png|webp|jpg)$/i.test(value) ? value.toLowerCase() : undefined;
}
function clearStoredIcons(root: string, keep = ""): void {
for (const extension of ["svg", "png", "webp", "jpg"]) {
const filename = `icon.${extension}`;
if (filename !== keep) deletePathRecursively(`${root}/${filename}`);
}
}
function clearRenderedIconCache(packageId: string): void {
for (const key of Array.from(renderedIconCache.keys())) {
if (key.startsWith(`${packageId}:`)) renderedIconCache.delete(key);
}
}
function stringValue(value: unknown): string {
return typeof value === "string" ? value : value == null ? "" : String(value);
}
+155
View File
@@ -0,0 +1,155 @@
/**
* Registry of running EvenHub apps. Each launched package gets its own glasses
* window, session, and persistent WebView — they run concurrently and keep
* driving their windows in the background (the shell delivers FOREGROUND_ENTER/
* EXIT as focus changes; the WebView host keeps them rendering while unseen).
*
* Glasses-first: launching does NOT take over the phone screen. The phone
* stays on the Faceclaw dashboard; an app's phone UI is shown on demand
* (window menu -> "Show phone UI"), overlaying the dashboard, and the Back
* button returns to it. Apps are kept alive until explicitly closed (no memory
* eviction yet).
*/
import { Application } from "@nativescript/core";
import { type AppContext } from "../app-definition";
import {
appFilesDirPath,
deletePathRecursively,
readBinaryFile,
writeBinaryFile,
} from "../../native/file-access";
import { parseEhpk, parseManifest, utf8Decode } from "./ehpk";
import { EvenHubSession } from "./session";
import { createEvenHubWindow } from "./evenhub-window";
import { createEvenHubWebView, type EvenHubWebView } from "./webview";
import { shell } from "../../ui/shell/shell";
import {
installedEvenHubAppId,
installedEvenHubPackagePath,
type InstalledEvenHubApp,
} from "./installed-apps";
type RunningApp = {
windowId: string;
appId: string;
packageId: string;
session: EvenHubSession;
webView: EvenHubWebView;
};
const running = new Map<string, RunningApp>();
let nextSerial = 1;
let phoneShownWindowId: string | null = null;
let backHandlerRegistered = false;
/** Intercept Android Back while an app's phone UI is overlaying the dashboard. */
function ensureBackHandler(): void {
if (backHandlerRegistered) return;
backHandlerRegistered = true;
Application.android?.on("activityBackPressed", (args: { cancel: boolean }) => {
if (phoneShownWindowId) {
args.cancel = true;
hidePhone();
}
});
}
/**
* Unpack an .ehpk and launch it as a new concurrent app: a glasses window plus
* a persistent WebView. Does not disturb other running apps or the phone screen.
*/
export async function launchPackage(
ctx: AppContext,
ehpkPath: string,
options: { appId?: string; singleton?: boolean } = {},
): Promise<void> {
const data = readBinaryFile(ehpkPath);
if (!data) {
ctx.appendLog(`evenhub: could not read ${ehpkPath}`);
return;
}
const archive = parseEhpk(data);
const appJson = archive.files.get("app.json");
if (!appJson) {
ctx.appendLog("evenhub: package has no app.json");
return;
}
const manifest = parseManifest(utf8Decode(appJson));
const appId = options.appId ?? "evenhub";
if (options.singleton) {
const existing = Array.from(running.values()).find((app) => app.appId === appId);
if (existing) {
shell.focusWindow(existing.windowId);
ctx.requestShellRender();
return;
}
}
// Unpack fresh into app-private storage, keyed by package id.
const safeId = manifest.packageId.replace(/[^A-Za-z0-9._-]/g, "_");
const baseDir = `${appFilesDirPath()}/evenhub-apps/${safeId}`;
deletePathRecursively(baseDir);
for (const [name, content] of Array.from(archive.files)) {
if (!writeBinaryFile(`${baseDir}/${name}`, content)) {
ctx.appendLog(`evenhub: failed to write ${name}`);
return;
}
}
const windowId = `evenhub:app:${nextSerial++}`;
const session = new EvenHubSession(manifest, `${baseDir}/dist`, ctx.appendLog);
const webView = createEvenHubWebView(session);
session.attachWebView({
evaluateJs: webView.evaluateJs,
destroy: () => {
if (phoneShownWindowId === windowId) {
webView.hideOnPhone();
phoneShownWindowId = null;
}
webView.destroy();
running.delete(windowId);
},
});
running.set(windowId, { windowId, appId, packageId: manifest.packageId, session, webView });
ensureBackHandler();
ctx.appendLog(`evenhub: launching ${manifest.name} ${manifest.version} (${manifest.packageId})`);
await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) =>
createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)),
);
}
/** Launch an installed app as a launcher-addressable singleton. */
export function launchInstalledPackage(ctx: AppContext, app: InstalledEvenHubApp): Promise<void> {
return launchPackage(ctx, installedEvenHubPackagePath(app.packageId), {
appId: installedEvenHubAppId(app.packageId),
singleton: true,
});
}
/** Close every running instance before an installed package is removed. */
export function closeRunningPackage(packageId: string): void {
const windowIds = Array.from(running.values())
.filter((app) => app.packageId === packageId)
.map((app) => app.windowId);
for (const windowId of windowIds) shell.closeWindow(windowId);
}
/** Overlay one app's phone UI over the dashboard (from its window menu). */
export function showOnPhone(windowId: string): void {
const app = running.get(windowId);
if (!app) return;
app.webView.showOnPhone();
phoneShownWindowId = windowId;
}
/** Return the phone to the dashboard (Back, or the shown app closing). */
export function hidePhone(): void {
if (!phoneShownWindowId) return;
running.get(phoneShownWindowId)?.webView.hideOnPhone();
phoneShownWindowId = null;
}
export function runningAppCount(): number {
return running.size;
}
+93
View File
@@ -0,0 +1,93 @@
/**
* Arbitrates the single G2 microphone among running EvenHub apps.
*
* An app asks for the mic with audioControl(true); it only actually receives
* audio while it is the foreground (visible) window, the screen is on, and no
* voice-input modal (the assistant) is active. When any of those stops holding,
* the app simply stops receiving frames — it is not told, matching the stock
* contract where a backgrounded app's mic goes silent.
*
* Because only one window is foreground at a time, at most one requesting app
* is ever eligible. The router runs the shared decode-only raw-PCM tap
* (voiceControlBridge) while an eligible app exists and routes each decoded
* frame to it; STT sessions preempt the tap at the bridge level.
*/
import { voiceControlBridge } from "../../native/voice-control";
import { voiceActivity } from "../../ui/shell/voice-activity";
declare const com: any;
export type EvenHubMicClient = {
readonly windowId: string;
isForeground(): boolean;
isScreenOn(): boolean;
deliverAudioPcm(pcm: Uint8Array): void;
};
function activeCommunicator(): any {
if (!global.isAndroid) return null;
try {
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
} catch {
return null;
}
}
class EvenHubMicRouter {
private readonly requesting = new Set<EvenHubMicClient>();
private active: EvenHubMicClient | null = null;
private unsubscribeRawPcm: (() => void) | null = null;
private unsubscribeVoiceActivity: (() => void) | null = null;
/** An app enabled its microphone (audioControl true). */
requestMic(client: EvenHubMicClient): void {
this.requesting.add(client);
this.ensureSubscriptions();
this.evaluate();
}
/** An app disabled its microphone, backgrounded, or closed. */
releaseMic(client: EvenHubMicClient): void {
if (!this.requesting.delete(client)) return;
this.evaluate();
}
/** A requesting app's foreground/screen state changed. */
notifyEligibilityChanged(): void {
this.evaluate();
}
private ensureSubscriptions(): void {
if (!this.unsubscribeRawPcm) {
this.unsubscribeRawPcm = voiceControlBridge.onRawPcm((pcm) => {
this.active?.deliverAudioPcm(pcm);
});
}
if (!this.unsubscribeVoiceActivity) {
this.unsubscribeVoiceActivity = voiceActivity.subscribe(() => this.evaluate());
}
}
private eligible(): EvenHubMicClient | null {
if (voiceActivity.isActive()) return null;
return Array.from(this.requesting).find(
(client) => client.isForeground() && client.isScreenOn(),
) ?? null;
}
private evaluate(): void {
const next = this.eligible();
this.active = next;
if (next) {
// Idempotent: no-op if already running, and re-asserted on every
// eligibility change so it self-heals after an STT preemption. Returns
// false if STT currently owns the mic; a later trigger (assistant modal
// close, foreground/screen change) re-tries.
voiceControlBridge.startRawCapture({ communicator: activeCommunicator() });
} else {
voiceControlBridge.stopRawCapture();
}
}
}
export const evenHubMicRouter = new EvenHubMicRouter();
+135
View File
@@ -0,0 +1,135 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText } from "../../graphics/textwrap";
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawSelectionHighlight } from "../../ui/menu";
import { permissionDetail, permissionLabel, type EvenHubPermission } from "./permissions";
import { openPrivacyPolicyOnPhone } from "./privacy-policy";
import { lineStep, listRowHeight } from "../../ui/metrics";
const DIALOG_X = 8;
const DIALOG_Y = 8;
const DIALOG_WIDTH = 272;
const PADDING = 10;
const PERM_GAP = 4;
/**
* Confirmation dialog listing the permissions an EvenHub app declares, shown
* before installing it or running an uninstalled package. Allow proceeds;
* Cancel (or double-click) backs out. A privacy-policy-only app still reaches
* this dialog so the user can review the policy before allowing first run.
*/
export class EvenHubPermissionDialogLayer implements Layer {
/** Starts on Allow; the optional privacy-policy action is inserted after it. */
private selectedIndex = 0;
private resolved = false;
constructor(
private readonly appName: string,
private readonly permissions: EvenHubPermission[],
private readonly privacyPolicyUrl: string,
private readonly onConfirm: () => void,
private readonly onCancel: () => void,
) {}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const textWidth = DIALOG_WIDTH - 2 * PADDING - 4;
const left = DIALOG_X + PADDING + 2;
const headerStep = lineStep(font) + 2;
const permLabelStep = lineStep(font) + 1;
const permDetailStep = lineStep(font) - 1;
const actionRowH = listRowHeight(font);
const permsHeight = this.permissions.reduce(
(sum, permission) => sum + permLabelStep + (permissionDetail(permission) ? permDetailStep : 0) + PERM_GAP,
0,
);
const emptyPermissionsHeight = this.permissions.length === 0 ? permLabelStep + PERM_GAP : 0;
const bodyTop = PADDING + headerStep + 6 + permsHeight + emptyPermissionsHeight + 6;
const height = Math.min(bodyTop + this.actions().length * actionRowH + PADDING, viewportHeight - 2 * DIALOG_Y);
// Fill 1 (transparent color key is 0), outline for the dialog edge.
image.fillRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 1);
image.drawRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 72);
let y = DIALOG_Y + PADDING;
const heading = this.permissions.length ? `${this.appName} needs:` : `${this.appName} is ready to run`;
image.drawText(font, left, y, truncateText(font, heading, textWidth), 235);
y += headerStep + 6;
if (this.permissions.length === 0) {
image.drawText(font, left, y, "No special permissions requested.", 140);
y += emptyPermissionsHeight;
}
for (const permission of this.permissions) {
image.drawText(font, left, y, truncateText(font, permissionLabel(permission.name), textWidth), 220);
y += permLabelStep;
const detail = permissionDetail(permission);
if (detail) {
image.drawText(font, left + 8, y, truncateText(font, detail, textWidth - 8), 140);
y += permDetailStep;
}
y += PERM_GAP;
}
y += 2;
const focused = ctx.stack.isFocused();
const actions = this.actions();
for (let index = 0; index < actions.length; index++) {
const rowY = y + index * actionRowH;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
}
image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200);
}
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
const actions = this.actions();
switch (event.type) {
case "scroll-up":
this.selectedIndex = (this.selectedIndex + actions.length - 1) % actions.length;
return;
case "scroll-down":
this.selectedIndex = (this.selectedIndex + 1) % actions.length;
return;
case "click":
if (actions[this.selectedIndex] === "Privacy policy") {
openPrivacyPolicyOnPhone(this.privacyPolicyUrl, this.appName);
} else {
this.resolve(ctx, actions[this.selectedIndex] === "Allow");
}
return;
case "double-click":
this.resolve(ctx, false);
return;
default:
return;
}
}
onRemoved(): void {
// Dismissed some other way (window closed, back button): treat as cancel.
if (!this.resolved) {
this.resolved = true;
this.onCancel();
}
}
private resolve(ctx: LayerContext, confirmed: boolean): void {
if (this.resolved) return;
this.resolved = true;
ctx.stack.pop();
if (confirmed) this.onConfirm();
else this.onCancel();
}
private actions(): string[] {
return this.privacyPolicyUrl ? ["Allow", "Privacy policy", "Cancel"] : ["Allow", "Cancel"];
}
}
+160
View File
@@ -0,0 +1,160 @@
/**
* EvenHub app permissions: parsing the manifest's declared permissions and
* turning them into the human-readable list Faceclaw shows for confirmation.
*
* Two manifest shapes exist in the wild (see notes/evenhub_compatibility.txt):
* - Current SDK: `permissions` is an array of {name, desc?, whitelist?}, with
* names network, location, g2-microphone, phone-microphone, album, camera.
* - Older shape: `permissions` is a map {"network": [...hosts], "fs": [...]}.
*
* Permissions are confirmed by the user when an app is installed, or when an
* uninstalled .ehpk is run directly (installing implies a prior confirmation).
* Once past that gate the running app is treated as having been granted every
* permission it declared; runtime surfaces (e.g. the microphone) check the
* declared set to decide whether to honor a request.
*/
export type EvenHubPermissionName =
| "network"
| "location"
| "g2-microphone"
| "phone-microphone"
| "album"
| "camera"
| "fs"
| string;
export type EvenHubPermission = {
name: EvenHubPermissionName;
/** App-supplied description, when the manifest provides one. */
description?: string;
/** For network/fs: the declared host or path whitelist. */
whitelist?: string[];
};
/** The mic permission names an audioControl request may be gated behind. */
const MICROPHONE_PERMISSIONS = new Set(["g2-microphone", "phone-microphone", "microphone"]);
/** Short label shown as the permission's title in the confirmation dialog. */
export function permissionLabel(name: EvenHubPermissionName): string {
switch (name) {
case "network":
return "Network access";
case "location":
return "Location";
case "g2-microphone":
return "Glasses microphone";
case "phone-microphone":
return "Phone microphone";
case "microphone":
return "Microphone";
case "album":
return "Photo album";
case "camera":
return "Camera";
case "fs":
return "Bundled files";
default:
return name;
}
}
/** Secondary line under the label: the app's own description, or a default. */
export function permissionDetail(permission: EvenHubPermission): string {
if (permission.description && permission.description.trim()) {
return permission.description.trim();
}
if ((permission.name === "network" || permission.name === "fs") && permission.whitelist?.length) {
return permission.whitelist.join(", ");
}
switch (permission.name) {
case "network":
return "Reach servers on the internet";
case "location":
return "Read your location";
case "g2-microphone":
return "Listen through the glasses microphone";
case "phone-microphone":
return "Listen through the phone microphone";
case "microphone":
return "Listen through the microphone";
case "album":
return "Read photos from your album";
case "camera":
return "Capture photos from the camera";
default:
return "";
}
}
export function isMicrophonePermission(name: EvenHubPermissionName): boolean {
return MICROPHONE_PERMISSIONS.has(name);
}
/** Whether the declared set includes a permission by name. */
export function permissionsInclude(permissions: EvenHubPermission[], name: EvenHubPermissionName): boolean {
return permissions.some((permission) => permission.name === name);
}
/** Whether the declared set includes any microphone permission. */
export function permissionsIncludeMicrophone(permissions: EvenHubPermission[]): boolean {
return permissions.some((permission) => isMicrophonePermission(permission.name));
}
/**
* Parse the manifest's raw `permissions` value into a normalized list,
* accepting both the array and the older map shape. Unknown names pass through
* so the confirmation dialog still surfaces them.
*/
export function parsePermissions(raw: unknown): EvenHubPermission[] {
const out: EvenHubPermission[] = [];
const seen = new Set<string>();
const add = (permission: EvenHubPermission) => {
const name = String(permission.name).trim();
if (!name || seen.has(name)) return;
seen.add(name);
out.push({ ...permission, name });
};
if (Array.isArray(raw)) {
for (const entry of raw) {
if (typeof entry === "string") {
add({ name: entry });
} else if (entry && typeof entry === "object") {
const record = entry as Record<string, unknown>;
const name = record.name;
if (typeof name !== "string" || !name.trim()) continue;
const description = typeof record.desc === "string"
? record.desc
: typeof record.description === "string"
? record.description
: undefined;
const whitelist = Array.isArray(record.whitelist)
? record.whitelist.map(String)
: undefined;
add({ name, description, whitelist });
}
}
} else if (raw && typeof raw === "object") {
// Older map shape: {"network": ["host", ...], "fs": ["./assets"]}.
for (const [name, value] of Object.entries(raw as Record<string, unknown>)) {
const whitelist = Array.isArray(value) ? value.map(String) : undefined;
add({ name, whitelist });
}
}
return out;
}
/**
* A stable signature of a granted permission set, for comparing what the user
* confirmed against what a later version of the app declares.
*/
export function permissionsSignature(permissions: EvenHubPermission[]): string {
return permissions
.map((permission) => {
const list = permission.whitelist?.length ? `(${[...permission.whitelist].sort().join(",")})` : "";
return `${permission.name}${list}`;
})
.sort()
.join(";");
}
+150
View File
@@ -0,0 +1,150 @@
import { Application } from "@nativescript/core";
declare const android: any;
declare const androidx: any;
declare const java: any;
const MAX_POLICY_BYTES = 10_000_000;
const POLICY_DOWNLOAD_TIMEOUT_MS = 30_000;
/** Open an app privacy policy on the phone, using a native viewer for PDFs. */
export function openPrivacyPolicyOnPhone(value: string, appName: string): boolean {
const url = safeHttpsUrl(value);
const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity;
if (!url || !activity) return false;
if (isPdfUrl(url)) {
void downloadAndOpenPdf(url, appName, activity).catch((error) => {
console.warn(`evenhub: could not open privacy policy PDF: ${error}`);
if (!openExternalUrl(url, activity)) {
showOpenError(activity, appName);
}
});
return true;
}
showWebPolicy(url, appName, activity);
return true;
}
/** Show an untrusted HTML policy in an isolated, bridge-free phone WebView. */
function showWebPolicy(url: string, appName: string, activity: any): void {
const webView = new android.webkit.WebView(activity);
const settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setAllowFileAccess(false);
webView.setWebViewClient(new android.webkit.WebViewClient());
const container = new android.widget.FrameLayout(activity);
const displayHeight = activity.getResources().getDisplayMetrics().heightPixels;
container.addView(
webView,
new android.widget.FrameLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
Math.max(1, Math.round(displayHeight * 0.72)),
),
);
const dialog = new android.app.AlertDialog.Builder(activity)
.setTitle(`${appName} privacy policy`)
.setView(container)
.setNegativeButton("Close", null)
.create();
dialog.setOnDismissListener(
new android.content.DialogInterface.OnDismissListener({
onDismiss: () => webView.destroy(),
}),
);
dialog.show();
const window = dialog.getWindow();
if (window) {
window.setLayout(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
);
}
webView.loadUrl(url);
}
async function downloadAndOpenPdf(url: string, appName: string, activity: any): Promise<void> {
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
const timeout = new Promise<never>((_resolve, reject) => {
timeoutHandle = setTimeout(() => reject(new Error("Privacy policy download timed out.")), POLICY_DOWNLOAD_TIMEOUT_MS);
});
let response: Response;
try {
response = await Promise.race([fetch(url), timeout]);
} finally {
if (timeoutHandle) clearTimeout(timeoutHandle);
}
if (!response.ok) throw new Error(`Privacy policy download failed (HTTP ${response.status}).`);
const declaredSize = Number(response.headers.get("Content-Length") ?? 0);
if (declaredSize > MAX_POLICY_BYTES) throw new Error("Privacy policy PDF is unexpectedly large.");
const bytes = new Uint8Array(await response.arrayBuffer());
if (bytes.length < 5 || bytes.length > MAX_POLICY_BYTES || !hasPdfMagic(bytes)) {
throw new Error("Privacy policy response is not a valid PDF.");
}
const directory = new java.io.File(activity.getCacheDir(), "privacy-policies");
if (!directory.exists() && !directory.mkdirs()) throw new Error("Could not create the privacy policy cache.");
const hash = Number(new java.lang.String(url).hashCode()) >>> 0;
const file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`);
const stream = new java.io.FileOutputStream(file);
try {
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
stream.getChannel().write(buffer);
} finally {
stream.close();
}
const authority = `${activity.getPackageName()}.fileprovider`;
const contentUri = androidx.core.content.FileProvider.getUriForFile(activity, authority, file);
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "application/pdf");
intent.setClipData(android.content.ClipData.newRawUri("Privacy policy", contentUri));
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(android.content.Intent.createChooser(intent, `${appName} privacy policy`));
}
function openExternalUrl(url: string, activity: any): boolean {
try {
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url));
intent.addCategory(android.content.Intent.CATEGORY_BROWSABLE);
activity.startActivity(intent);
return true;
} catch (error) {
console.warn(`evenhub: no browser available for privacy policy: ${error}`);
return false;
}
}
function showOpenError(activity: any, appName: string): void {
new android.app.AlertDialog.Builder(activity)
.setTitle(`${appName} privacy policy`)
.setMessage("No app is available to display this privacy policy PDF.")
.setPositiveButton("Close", null)
.show();
}
function isPdfUrl(value: string): boolean {
try {
return new URL(value).pathname.toLowerCase().endsWith(".pdf");
} catch {
return false;
}
}
function hasPdfMagic(bytes: Uint8Array): boolean {
return bytes[0] === 0x25 && bytes[1] === 0x50 && bytes[2] === 0x44 && bytes[3] === 0x46 && bytes[4] === 0x2d;
}
function safeHttpsUrl(value: string): string {
if (!value || value.length > 2048) return "";
try {
const url = new URL(value);
return url.protocol === "https:" ? url.toString() : "";
} catch {
return "";
}
}
File diff suppressed because it is too large. Load diff
+253
View File
@@ -0,0 +1,253 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
import { drawSelectionHighlight } from "../../ui/menu";
import { TextViewerLayer } from "../files/text-viewer";
import { evenHubApi, type EvenHubStoreApp } from "./even-api";
import {
getInstalledEvenHubApp,
installEvenHubPackageBytes,
installedEvenHubAppId,
readEvenHubPackageManifestBytes,
setInstalledEvenHubIcon,
type EvenHubInstallIcon,
} from "./installed-apps";
import { EvenHubPermissionDialogLayer } from "./permission-dialog";
import { lineStep, listRowHeight } from "../../ui/metrics";
const X = 18;
export type EvenHubStoreDetailOptions = {
launchApp: (appId: string) => Promise<void> | void;
appendLog: (message: string) => void;
};
/** Storefront metadata and the Install/Launch action for one public app. */
export class EvenHubStoreDetailLayer implements Layer {
/** About, What's New, Install/Launch. */
private selectedIndex = 2;
private working = false;
private detailPromise: Promise<void> | null = null;
private status = "";
constructor(
private app: EvenHubStoreApp,
private readonly options: EvenHubStoreDetailOptions,
) {}
paint(ctx: LayerContext): GrayImage {
if (!this.detailPromise) void this.ensureDetail(ctx);
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const textWidth = width - X * 2;
const step = lineStep(font);
const actionRowH = listRowHeight(font) + 4;
let y = 8;
image.drawText(font, X, y, truncateText(font, this.app.name, textWidth), 235);
y += step + 2;
const creator = this.app.creatorName ? `by ${this.app.creatorName}` : this.app.packageId;
image.drawText(font, X, y, truncateText(font, creator, textWidth), 130);
y += step + 6;
const summary = this.app.tagline || this.app.description || "No description supplied.";
for (const line of wrapText(font, summary, textWidth).slice(0, 3)) {
image.drawText(font, X, y, line, 205);
y += step;
}
y += 4;
const metadata = [
`${formatCount(this.app.installCount)} installs · ${formatCount(this.app.likeCount)} likes`,
this.app.version ? `Version ${this.app.version}${this.app.fileSize ? ` · ${formatBytes(this.app.fileSize)}` : ""}` : "",
this.app.categories.length ? `Categories: ${this.app.categories.join(", ")}` : "",
this.app.firstPublishedAt ? `Published: ${formatDate(this.app.firstPublishedAt)}` : "",
].filter(Boolean);
for (const line of metadata) {
image.drawText(font, X, y, truncateText(font, line, textWidth), 125);
y += step;
}
const installed = getInstalledEvenHubApp(this.app.packageId);
const actions = ["About", "What's New", this.working ? "Installing..." : installed ? "Launch" : "Install"];
const actionTop = height - font.lineHeight - 10 - actions.length * actionRowH;
if (installed && !this.status) {
image.drawText(font, X, y + 2, `Installed version ${installed.version}`, 155);
}
if (this.status) {
image.drawText(font, X, Math.min(actionTop - step, y + 2), truncateText(font, this.status, textWidth), 180);
}
for (let index = 0; index < actions.length; index++) {
const actionY = actionTop + index * actionRowH;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, X - 5, actionY, textWidth + 10, actionRowH - 1, ctx.stack.isFocused(), 8);
}
image.drawText(font, X + 6, actionY + 5, actions[index]!, this.working && index === 2 ? 145 : selected ? 245 : 190);
}
image.drawText(
font,
X,
height - font.lineHeight - 4,
`${GESTURE_SCROLL} select ${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} back`,
105,
);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click") {
if (!this.working) ctx.stack.pop();
return;
}
if (this.working) return;
if (event.type === "scroll-up") {
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
return;
}
if (event.type === "scroll-down") {
this.selectedIndex = Math.min(2, this.selectedIndex + 1);
return;
}
if (event.type !== "click") return;
await this.ensureDetail(ctx);
if (this.selectedIndex === 0) {
ctx.stack.push(new TextViewerLayer(this.app.description || "No description supplied.", "About"));
return;
}
if (this.selectedIndex === 1) {
ctx.stack.push(new TextViewerLayer(this.app.changelog || "No release notes supplied.", "What's New"));
return;
}
const installed = getInstalledEvenHubApp(this.app.packageId);
if (installed) {
await this.options.launchApp(installedEvenHubAppId(installed.packageId));
return;
}
await this.prepareInstall(ctx);
}
private async prepareInstall(ctx: LayerContext): Promise<void> {
this.working = true;
this.status = `Downloading ${this.app.name}...`;
ctx.actions.requestRender();
try {
const [download, icon] = await Promise.all([
evenHubApi.downloadApp(this.app.packageId),
this.app.iconPath
? evenHubApi.downloadPublicAsset(this.app.iconPath).catch((error) => {
this.options.appendLog(`evenhub store: icon unavailable for ${this.app.packageId}: ${cleanError(error)}`);
return undefined;
})
: Promise.resolve(undefined),
]);
const manifest = readEvenHubPackageManifestBytes(download.bytes);
if (!manifest) throw new Error("The downloaded EHPK manifest could not be read.");
const privacyPolicyUrl = download.privacyPolicyUrl || manifest.privacyPolicyUrl;
this.working = false;
this.status = "";
ctx.actions.requestRender();
if (manifest.permissions.length > 0 || privacyPolicyUrl) {
ctx.stack.push(
new EvenHubPermissionDialogLayer(
manifest.name,
manifest.permissions,
privacyPolicyUrl,
() => {
void this.installAndLaunch(ctx, download.bytes, icon);
},
() => {
this.status = "Installation canceled.";
ctx.actions.requestRender();
},
),
);
return;
}
await this.installAndLaunch(ctx, download.bytes, icon);
} catch (error) {
this.working = false;
this.status = cleanError(error);
this.options.appendLog(`evenhub store: ${this.status}`);
ctx.actions.requestRender();
}
}
private async installAndLaunch(
ctx: LayerContext,
bytes: Uint8Array,
icon: EvenHubInstallIcon | undefined,
): Promise<void> {
this.working = true;
this.status = "Installing...";
ctx.actions.requestRender();
try {
const result = installEvenHubPackageBytes(bytes, {
expectedPackageId: this.app.packageId,
icon,
});
this.options.appendLog(
`evenhub store: installed ${result.packageId} ${result.version} (${bytes.length} bytes)`,
);
this.status = "Installed";
ctx.actions.requestRender();
await this.options.launchApp(installedEvenHubAppId(result.packageId));
} catch (error) {
this.status = cleanError(error);
this.options.appendLog(`evenhub store: ${this.status}`);
} finally {
this.working = false;
ctx.actions.requestRender();
}
}
private ensureDetail(ctx: LayerContext): Promise<void> {
if (this.detailPromise) return this.detailPromise;
this.detailPromise = evenHubApi
.getStoreAppDetail(this.app.packageId)
.then((detail) => {
if (detail) this.app = { ...this.app, ...detail };
const installed = getInstalledEvenHubApp(this.app.packageId);
if (installed && !installed.iconFile && this.app.iconPath) {
return evenHubApi.downloadPublicAsset(this.app.iconPath).then((icon) => {
setInstalledEvenHubIcon(this.app.packageId, icon);
});
}
return undefined;
})
.catch((error) => {
this.options.appendLog(`evenhub store: app details unavailable: ${cleanError(error)}`);
})
.finally(() => ctx.actions.requestRender());
return this.detailPromise;
}
}
function formatCount(value: number): string {
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}m`;
if (value >= 1000) return `${(value / 1000).toFixed(value >= 10_000 ? 0 : 1)}k`;
return String(value);
}
function formatDate(value: string): string {
const date = new Date(value);
return Number.isFinite(date.getTime()) ? date.toISOString().slice(0, 10) : value;
}
function formatBytes(value: number): string {
if (value < 1024) return `${value} B`;
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`;
return `${(value / (1024 * 1024)).toFixed(1)} MB`;
}
function cleanError(error: unknown): string {
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
}
+275
View File
@@ -0,0 +1,275 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { type DashboardInputEvent, type Layer, type LayerActions, type LayerContext } from "../../ui/layers";
import {
drawListScrollbar,
drawSelectionHighlight,
scrollToKeepSelectionVisible,
type MenuItem,
} from "../../ui/menu";
import { shell } from "../../ui/shell/shell";
import {
evenHubApi,
EvenHubAuthenticationError,
isEvenHubStoreConfigured,
type EvenHubStoreApp,
} from "./even-api";
import {
clearEvenHubPassword,
evenHubEmailSetting,
evenHubPasswordSetting,
} from "./credentials";
import { EvenHubStoreDetailLayer } from "./store-detail-layer";
import { lineStep } from "../../ui/metrics";
const HEADER_HEIGHT = 34;
const FOOTER_HEIGHT = 22;
const LIST_X = 18;
export type EvenHubStoreLayerOptions = {
launchApp: (appId: string) => Promise<void> | void;
appendLog: (message: string) => void;
};
/** Browse the public EvenHub leaderboard and download one package to run. */
export class EvenHubStoreLayer implements Layer {
private apps: EvenHubStoreApp[] = [];
private selectedIndex = 0;
private scrollRow = 0;
private total = 0;
private nextPage = 1;
private started = false;
private loading = false;
private showingLogin = !isEvenHubStoreConfigured();
private status = this.showingLogin ? "Sign in to browse public apps." : "";
private credentialEditorOpen = false;
constructor(private readonly options: EvenHubStoreLayerOptions) {}
paint(ctx: LayerContext): GrayImage {
if (!this.started) {
this.started = true;
if (this.showingLogin) {
this.openCredentialEditor(ctx);
} else {
void this.reload(ctx);
}
}
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, LIST_X, 8, "EvenHub", 220);
const subtitle = this.status || (this.total ? `${this.apps.length} of ${this.total} public apps` : "Public apps");
image.drawText(font, LIST_X, 22, truncateText(font, subtitle, width - LIST_X * 2), 125);
if (this.showingLogin) {
const message = this.loading
? "Signing in..."
: "Enter your Even account email and password in the phone app.";
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) {
image.drawText(font, LIST_X, HEADER_HEIGHT + 22 + index * 15, line, 190);
}
image.drawText(
font,
LIST_X,
height - 16,
truncateText(font, `${GESTURE_CLICK} edit credentials ${GESTURE_DOUBLE_CLICK} back`, width - LIST_X * 2),
105,
);
return image;
}
if (this.apps.length === 0) {
const message = this.loading ? "Loading apps..." : this.status || "No apps returned.";
image.drawText(font, LIST_X, HEADER_HEIGHT + 28, truncateText(font, message, width - LIST_X * 2), 190);
} else {
this.selectedIndex = clamp(this.selectedIndex, 0, this.apps.length - 1);
const rowH = Math.max(30, 2 * lineStep(font) + 4);
const visibleRows = Math.max(1, Math.floor((height - HEADER_HEIGHT - FOOTER_HEIGHT) / rowH));
this.scrollRow = scrollToKeepSelectionVisible(
this.scrollRow,
this.selectedIndex,
visibleRows,
this.apps.length,
);
const last = Math.min(this.apps.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < last; index++) {
const app = this.apps[index]!;
const y = HEADER_HEIGHT + (index - this.scrollRow) * rowH;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, ctx.stack.isFocused(), 5);
}
image.drawText(font, LIST_X, y + 2, truncateText(font, app.name, width - LIST_X * 2), 215);
const detail = app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`;
image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115);
}
if (this.apps.length > visibleRows) {
drawListScrollbar(image, width - 5, HEADER_HEIGHT, visibleRows * rowH - 3, this.scrollRow, visibleRows, this.apps.length);
}
}
const hint = `${GESTURE_SCROLL} select ${GESTURE_CLICK} details ${GESTURE_DOUBLE_CLICK} back`;
image.drawText(font, LIST_X, height - font.lineHeight - 4, truncateText(font, hint, width - LIST_X * 2), 105);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (this.showingLogin) {
if (event.type === "click" && !this.loading) {
this.openCredentialEditor(ctx);
} else if (event.type === "double-click") {
shell.yieldFocusToSidebar();
}
return;
}
switch (event.type) {
case "scroll-up":
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
return;
case "scroll-down":
this.selectedIndex = Math.min(Math.max(0, this.apps.length - 1), this.selectedIndex + 1);
if (this.selectedIndex >= this.apps.length - 4 && this.apps.length < this.total) {
void this.loadNextPage(ctx);
}
return;
case "click":
if (!this.loading) {
const app = this.apps[this.selectedIndex];
if (app) {
ctx.stack.push(
new EvenHubStoreDetailLayer(app, {
launchApp: this.options.launchApp,
appendLog: this.options.appendLog,
}),
);
}
}
return;
case "double-click":
shell.yieldFocusToSidebar();
return;
default:
return;
}
}
buildMenuItems(): MenuItem[] {
if (this.showingLogin) return [];
return [
{
label: "Refresh",
onSelect: (ctx) => {
ctx.stack.pop();
void this.reload(ctx);
},
},
{
label: "Log Out",
onSelect: (ctx) => {
ctx.stack.pop();
clearEvenHubPassword();
evenHubApi.clearSession();
this.enterLogin(ctx, "Signed out.");
},
},
];
}
closeCredentialEditor(actions: Pick<LayerActions, "endTextSettingEdit">): void {
if (!this.credentialEditorOpen) return;
this.credentialEditorOpen = false;
void actions.endTextSettingEdit();
}
private async reload(ctx: LayerContext): Promise<void> {
if (this.loading) return;
evenHubApi.clearSession();
this.apps = [];
this.total = 0;
this.nextPage = 1;
this.selectedIndex = 0;
this.scrollRow = 0;
this.status = "";
if (!isEvenHubStoreConfigured()) {
this.enterLogin(ctx);
return;
}
await this.loadNextPage(ctx);
}
private async loadNextPage(ctx: LayerContext): Promise<void> {
if (this.loading || (this.total > 0 && this.apps.length >= this.total)) return;
this.loading = true;
this.status = this.apps.length ? "Loading more apps..." : "Loading apps...";
ctx.actions.requestRender();
try {
const page = await evenHubApi.listApps(this.nextPage);
const seen = new Set(this.apps.map((app) => app.packageId));
this.apps.push(...page.apps.filter((app) => !seen.has(app.packageId)));
this.total = page.total;
this.nextPage = page.page + 1;
this.status = "";
} catch (error) {
const message = cleanError(error);
if (error instanceof EvenHubAuthenticationError) {
this.options.appendLog(`evenhub store login failed: ${message}`);
this.enterLogin(ctx, `Login failed: ${message}`);
return;
}
this.status = message;
this.options.appendLog(`evenhub store: ${this.status}`);
} finally {
this.loading = false;
ctx.actions.requestRender();
}
}
private enterLogin(ctx: LayerContext, status = "Sign in to browse public apps."): void {
this.showingLogin = true;
this.loading = false;
this.apps = [];
this.total = 0;
this.status = status;
this.openCredentialEditor(ctx);
ctx.actions.requestRender();
}
private openCredentialEditor(ctx: LayerContext): void {
if (this.credentialEditorOpen) return;
this.credentialEditorOpen = true;
void ctx.actions.startTextSettingsEdit(
[evenHubEmailSetting, evenHubPasswordSetting],
"Sign in to EvenHub",
() => {
this.credentialEditorOpen = false;
void this.submitCredentials(ctx);
},
);
}
private async submitCredentials(ctx: LayerContext): Promise<void> {
if (!isEvenHubStoreConfigured()) {
this.enterLogin(ctx, "Email and password are required.");
return;
}
this.showingLogin = false;
await this.reload(ctx);
}
}
function cleanError(error: unknown): string {
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
}
function formatCount(value: number): string {
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}m`;
if (value >= 1000) return `${(value / 1000).toFixed(value >= 10_000 ? 0 : 1)}k`;
return String(value);
}
+103
View File
@@ -0,0 +1,103 @@
/**
* Constructs the Android WebView that runs one EvenHub app and attaches it to
* the persistent keep-alive host (FaceclawEvenHubWebViewHost), decoupled from
* any phone Page. The WebView is created at launch and lives until the app's
* glasses window closes — it renders the app to the glasses whether or not the
* phone is currently showing it (see the host for the off-screen-render trick).
*
* A raw WebView subclass (FaceclawEvenHubWebView, not the NativeScript
* component) is used so we fully own the WebViewClient (offline asset serving
* from a fake per-app origin plus document-start injection of the
* flutter_inappwebview bridge shim) and so the page keeps running JS while the
* host app is backgrounded (see FaceclawEvenHubWebView / the host).
*/
import { Application, Utils } from "@nativescript/core";
import { EVENHUB_BRIDGE_INJECT_SCRIPT, buildFaceclawExtensionsScript, type EvenHubSession } from "./session";
import { FACECLAW_VERSION } from "../../version";
declare const android: any;
declare const com: any;
export type EvenHubWebView = {
evaluateJs: (js: string) => void;
destroy: () => void;
showOnPhone: () => void;
hideOnPhone: () => void;
/** The native android.webkit.WebView, for the host. */
native: unknown;
};
/** A distinct (fake, intercepted) https origin per app isolates web storage. */
function originHost(session: EvenHubSession): string {
const safe = session.manifest.packageId.toLowerCase().replace(/[^a-z0-9.-]/g, "-");
return `${safe}.evenhub.invalid`;
}
/**
* Build the app's WebView, wire it to the session, attach it to the keep-alive
* host, and start loading. Must run on the main thread.
*/
export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity;
const context = activity ?? Utils.android.getApplicationContext();
const webView = new com.faceclaw.app.FaceclawEvenHubWebView(context);
const settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setAllowFileAccess(false);
settings.setMediaPlaybackRequiresUserGesture(false);
const listener = new com.faceclaw.app.FaceclawEvenHubListener({
onEvenAppMessage: (handlerName: string, argsJson: string, callId: number) => {
session.handleBridgeCall(String(handlerName), String(argsJson), Number(callId));
},
onPageFinished: () => {
session.webViewLoaded();
},
});
const host = originHost(session);
// Stock bridge shim first, then the Faceclaw extensions global (both at
// document-start, before any app JS).
const injectScript = EVENHUB_BRIDGE_INJECT_SCRIPT + buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`);
webView.setWebViewClient(
new com.faceclaw.app.FaceclawEvenHubWebViewClient(session.distDir, host, injectScript, listener),
);
webView.addJavascriptInterface(new com.faceclaw.app.FaceclawEvenHubJsBridge(listener), "__faceclawEvenHub");
// Surface the app's console in logcat (tag FaceclawEvenHubConsole).
const ChromeClient = (android.webkit.WebChromeClient as any).extend({
onConsoleMessage: (message: any): boolean => {
android.util.Log.i(
"FaceclawEvenHubConsole",
`${message.message()} (${message.sourceId()}:${message.lineNumber()})`,
);
return true;
},
});
webView.setWebChromeClient(new ChromeClient());
const nativeHost = com.faceclaw.app.FaceclawEvenHubWebViewHost.getInstance();
nativeHost.attach(activity, webView);
webView.loadUrl(`https://${host}/${session.manifest.entrypoint}`);
return {
native: webView,
evaluateJs: (js: string) => {
try {
webView.evaluateJavascript(js, null);
} catch (error) {
console.warn(`evenhub evaluateJs failed: ${error}`);
}
},
destroy: () => {
try {
nativeHost.detach(webView);
} catch (error) {
console.warn(`evenhub webview destroy failed: ${error}`);
}
},
showOnPhone: () => nativeHost.showOnPhone(webView),
hideOnPhone: () => nativeHost.hideOnPhone(),
};
}
+20 -15
View File
@@ -1,10 +1,11 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText, truncateLeft } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { GrayImage, type UiFont } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
import { lineStep, tightRowHeight } from "../../ui/metrics";
import { ConfigSettingEnum } from "../../ui/dashboard-settings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import {
@@ -17,10 +18,12 @@ import {
} from "../../native/file-access";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const HEADER_HEIGHT = 34;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
/** Header: title line + path line (34px at the 12px bitmap default). */
function headerHeight(font: UiFont): number {
return 8 + lineStep(font) + font.lineHeight;
}
const GRID_COLS = 5;
const GRID_VISIBLE_ROWS = 3;
@@ -139,7 +142,7 @@ export class FileBrowserLayer implements Layer {
image.drawText(font, 20, 8, "Files", 220);
const pathLabel = this.location === null ? "Places" : this.location;
image.drawText(font, 20, 22, truncateLeft(font, pathLabel, width - 40), 130);
image.drawText(font, 20, 8 + lineStep(font), truncateLeft(font, pathLabel, width - 40), 130);
if (filesViewModeSetting.get() === "list") {
this.paintList(image, font, rows, ctx);
@@ -149,36 +152,38 @@ export class FileBrowserLayer implements Layer {
return image;
}
private paintList(image: GrayImage, font: BdfFont, rows: BrowserItem[], ctx: LayerContext): void {
private paintList(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1));
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
const rowH = tightRowHeight(font);
const headerH = headerHeight(font);
const listHeight = height - headerH - font.lineHeight - 10;
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.listIndex, visibleRows, rows.length);
const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < lastVisible; index++) {
const row = rows[index]!;
const y = HEADER_HEIGHT + (index - this.scrollRow) * ROW_HEIGHT;
const y = headerH + (index - this.scrollRow) * rowH;
const selected = index === this.listIndex;
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
}
const value = itemValue(row, selected);
image.drawText(font, LIST_X, y + 1, truncateText(font, row.label, width - 2 * LIST_X), value);
}
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} up / back`, 110);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} up / back`, 110);
}
private paintIcons(image: GrayImage, font: BdfFont, rows: BrowserItem[], ctx: LayerContext): void {
private paintIcons(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
const focused = ctx.stack.isFocused();
const iconRows = buildIconRows(rows);
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const gridTop = HEADER_HEIGHT;
const gridTop = headerHeight(font);
const gridBottom = height - GRID_BOTTOM_MARGIN;
const rowH = Math.floor((gridBottom - gridTop) / GRID_VISIBLE_ROWS);
const colW = width / GRID_COLS;
@@ -232,7 +237,7 @@ export class FileBrowserLayer implements Layer {
this.iconMode === "row"
? `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick ${GESTURE_DOUBLE_CLICK} up / back`
: `${GESTURE_SCROLL} item ${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} row`;
image.drawText(font, 20, height - 16, hint, 110);
image.drawText(font, 20, height - font.lineHeight - 4, hint, 110);
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
+11 -10
View File
@@ -1,17 +1,16 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import type { DirectoryEntry } from "../../native/file-access";
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawSelectionHighlight } from "../../ui/menu";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
const DIALOG_X = 8;
const DIALOG_Y = 8;
const DIALOG_WIDTH = 272;
const PADDING = 10;
const INFO_LINE_STEP = 14;
const ACTION_ROW_HEIGHT = 20;
const MAX_NAME_LINES = 3;
export type FileInfoAction = {
@@ -38,14 +37,16 @@ export class FileInfoDialogLayer implements Layer {
const { height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const step = lineStep(font);
const rowH = listRowHeight(font);
const textWidth = DIALOG_WIDTH - 2 * PADDING - 4;
const nameLines = wrapText(font, this.entry.name, textWidth, { breakLongWords: true }).slice(0, MAX_NAME_LINES);
const infoLines = [
`Size: ${formatSize(this.entry.sizeBytes)}`,
`Modified: ${formatDate(this.entry.modifiedMs)}`,
];
const bodyTop = PADDING + (nameLines.length + infoLines.length) * INFO_LINE_STEP + 6;
const bodyHeight = this.actions.length ? this.actions.length * ACTION_ROW_HEIGHT : INFO_LINE_STEP;
const bodyTop = PADDING + (nameLines.length + infoLines.length) * step + 6;
const bodyHeight = this.actions.length ? this.actions.length * rowH : step;
const height = Math.min(bodyTop + bodyHeight + PADDING, viewportHeight - 2 * DIALOG_Y);
// Fill 1, not 0: identical after 4bpp quantization, but 0 is the
@@ -56,11 +57,11 @@ export class FileInfoDialogLayer implements Layer {
let y = DIALOG_Y + PADDING;
for (const line of nameLines) {
image.drawText(font, DIALOG_X + PADDING + 2, y, line, 230);
y += INFO_LINE_STEP;
y += step;
}
for (const line of infoLines) {
image.drawText(font, DIALOG_X + PADDING + 2, y, line, 150);
y += INFO_LINE_STEP;
y += step;
}
y += 6;
@@ -70,12 +71,12 @@ export class FileInfoDialogLayer implements Layer {
}
const focused = ctx.stack.isFocused();
for (let index = 0; index < this.actions.length; index++) {
const rowY = y + index * ACTION_ROW_HEIGHT;
const rowY = y + index * rowH;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, ACTION_ROW_HEIGHT - 1, focused, 8);
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, rowH - 1, focused, 8);
}
image.drawText(font, DIALOG_X + 22, rowY + 3, this.actions[index]!.label, selected ? 255 : 200);
image.drawText(font, DIALOG_X + 22, rowY + LIST_ROW_TEXT_INSET, this.actions[index]!.label, selected ? 255 : 200);
}
return image;
}
+63 -1
View File
@@ -1,8 +1,12 @@
import { readTextFile, type DirectoryEntry } from "../../native/file-access";
import { isFontFile } from "../../native/font-files";
import { installFontFile, isFontInstalled } from "../../graphics/installed-fonts";
import { isDecodableImageFile } from "../../native/image-files";
import { readEvenHubPackageManifest } from "../evenhub/installed-apps";
import { EvenHubPermissionDialogLayer } from "../evenhub/permission-dialog";
import { FileBrowserLayer } from "./file-browser";
import { FileInfoDialogLayer, type FileInfoAction } from "./file-info-dialog";
import { type LayerContext } from "../../ui/layers";
import { FontPreviewerLayer } from "./font-previewer";
import { ImageViewerLayer } from "./image-viewer";
import { TextViewerLayer } from "./text-viewer";
@@ -18,12 +22,17 @@ export const FILES_WINDOW_ID = "files";
export const FILES_SURFACE_ID = "window:files";
const TEXT_FILE = /\.(txt|md|log)$/i;
const EHPK_FILE = /\.ehpk$/i;
export type FilesAppOptions = InProcessAppOptions & {
/** Open a text document as its own shell window (also used by the share intent). */
openDocumentWindow: (title: string, text: string) => void;
/** Open an image file as its own shell window. */
openImageWindow: (title: string, path: string) => void;
/** Launch an EvenHub app package (.ehpk) through the EvenHub host. */
openEhpkApp: (path: string) => void;
/** Copy an EHPK into the installed-app store, register it, and launch it. */
installEhpkApp: (path: string) => Promise<void> | void;
/** Open a font file's previewer as its own shell window. */
openFontWindow: (title: string, path: string) => void;
};
@@ -36,7 +45,7 @@ export type FilesAppOptions = InProcessAppOptions & {
export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow {
let created: InProcessWindow | null = null;
const browser = new FileBrowserLayer({
isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name) || isFontFile(name),
isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name) || EHPK_FILE.test(name) || isFontFile(name),
// The browser handles double-click itself (up a level), so it is not
// wrapped in YieldAtRootLayer; it yields explicitly from the top level.
onLeave: () => shell.yieldFocusToSidebar(),
@@ -133,6 +142,28 @@ export function createImageDocumentWindow(
});
}
/**
* Show the permission-confirmation dialog for an .ehpk before install/run,
* then run `proceed` if the user allows. Packages with neither permissions nor
* a privacy policy skip the dialog and proceed immediately.
*/
function confirmEhpkPermissions(ctx: LayerContext, entry: DirectoryEntry, proceed: () => void): void {
const manifest = readEvenHubPackageManifest(entry.path);
if (!manifest || (manifest.permissions.length === 0 && !manifest.privacyPolicyUrl)) {
proceed();
return;
}
ctx.stack.push(
new EvenHubPermissionDialogLayer(
manifest.name,
manifest.permissions,
manifest.privacyPolicyUrl,
proceed,
() => {},
),
);
}
/**
* The open actions for the picked-file dialog: View here / Open in new
* window for viewable types, empty for everything else (metadata only).
@@ -158,6 +189,26 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI
},
];
}
if (EHPK_FILE.test(entry.name)) {
return [
{
label: "Run app",
onSelect: (ctx) => {
ctx.stack.pop();
confirmEhpkPermissions(ctx, entry, () => options.openEhpkApp(entry.path));
},
},
{
label: "Install",
onSelect: (ctx) => {
ctx.stack.pop();
confirmEhpkPermissions(ctx, entry, () => {
void options.installEhpkApp(entry.path);
});
},
},
];
}
if (isDecodableImageFile(entry.name)) {
return [
{
@@ -177,6 +228,16 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI
];
}
if (isFontFile(entry.name)) {
// Install copies into the app-internal fonts directory, making the face
// available in the Settings font pickers. The row relabels itself with
// the result (the dialog re-reads labels each paint).
const install: FileInfoAction = {
label: isFontInstalled(entry.path) ? "Reinstall" : "Install",
onSelect: () => {
const error = installFontFile(entry.path);
install.label = error ?? "Installed";
},
};
return [
{
label: "Preview here",
@@ -192,6 +253,7 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI
options.openFontWindow(entry.name, entry.path);
},
},
install,
];
}
return [];
+1 -1
View File
@@ -1,4 +1,4 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText } from "../../graphics/textwrap";
import {
+2 -1
View File
@@ -1,4 +1,5 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { loadImageFileAsGray } from "../../native/image-files";
+21
View File
@@ -1,4 +1,10 @@
import { type AppContext, type AppDefinition } from "../app-definition";
import { openEvenHubPackage } from "../evenhub";
import {
installEvenHubPackageFile,
installedEvenHubAppId,
} from "../evenhub/installed-apps";
import { closeRunningPackage } from "../evenhub/manager";
import {
createFilesAppWindow,
createFontDocumentWindow,
@@ -55,6 +61,21 @@ const filesApp: AppDefinition = {
...options,
openDocumentWindow: (title, text) => openTextDocumentWindow(ctx, title, text),
openImageWindow: (title, path) => openImageDocumentWindow(ctx, title, path),
openEhpkApp: (path) => {
void openEvenHubPackage(ctx, path).catch((error) => {
ctx.appendLog(`evenhub launch failed: ${error}`);
});
},
installEhpkApp: async (path) => {
try {
const installed = installEvenHubPackageFile(path);
ctx.appendLog(`evenhub: installed ${installed.packageId} ${installed.version}`);
closeRunningPackage(installed.packageId);
await ctx.launchApp(installedEvenHubAppId(installed.packageId));
} catch (error) {
ctx.appendLog(`evenhub install failed: ${error}`);
}
},
openFontWindow: (title, path) => openFontDocumentWindow(ctx, title, path),
}),
),
+23 -9
View File
@@ -1,15 +1,27 @@
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { TtfFont } from "../../graphics/ttf-font";
import { GrayImage, type UiFont } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const MARGIN_X = 18;
const TITLE_Y = 16;
const BODY_X = 18;
const BODY_Y = 44;
const LINE_STEP = 14;
const FOOTER_MARGIN = 36;
/** Line step for a bitmap face (the 12px font plus leading, as before). */
const BDF_LINE_STEP = 14;
/** Body text follows the UI font (a TTF by default; see ui-fonts.ts). */
function getViewerFont(): UiFont {
return getDefaultSmallFont();
}
function lineStepOf(font: UiFont): number {
return font instanceof TtfFont ? font.lineHeight : BDF_LINE_STEP;
}
/**
* Paged text viewer for a document. Sized to its hosting stack (a Files
* document window, or pushed over the file browser for view-in-place).
@@ -17,6 +29,7 @@ const FOOTER_MARGIN = 36;
export class TextViewerLayer implements Layer {
private lines: string[] | null = null;
private wrappedForWidth = 0;
private wrappedWithFont = 0;
private firstLine = 0;
private bodyLineCount = 14;
@@ -26,17 +39,18 @@ export class TextViewerLayer implements Layer {
) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const font = getViewerFont();
const lineStep = lineStepOf(font);
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const footerY = height - FOOTER_MARGIN;
this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / LINE_STEP));
this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / lineStep));
image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X - 8), 220);
const lines = this.getLines(font, width);
const visibleLines = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount);
for (let index = 0; index < visibleLines.length; index++) {
image.drawText(font, BODY_X, BODY_Y + index * LINE_STEP, visibleLines[index]!, 230);
image.drawText(font, BODY_X, BODY_Y + index * lineStep, visibleLines[index]!, 230);
}
const currentPage = Math.floor(this.firstLine / this.pageStep()) + 1;
@@ -74,14 +88,15 @@ export class TextViewerLayer implements Layer {
this.firstLine = Math.max(0, Math.min(maxFirstLine, this.firstLine + delta));
}
private getLines(font: BdfFont, width: number): string[] {
if (this.lines === null || this.wrappedForWidth !== width) {
private getLines(font: UiFont, width: number): string[] {
if (this.lines === null || this.wrappedForWidth !== width || this.wrappedWithFont !== font.fingerprintId) {
const normalized = this.documentText.replace(/\t/g, " ").replace(/\r/g, "");
this.lines = wrapText(font, normalized, width - BODY_X - 12, {
preserveLeadingWhitespace: true,
breakLongWords: true,
});
this.wrappedForWidth = width;
this.wrappedWithFont = font.fingerprintId;
}
return this.lines;
}
@@ -93,4 +108,3 @@ export class TextViewerLayer implements Layer {
return Math.ceil((lineCount - this.bodyLineCount) / this.pageStep()) + 1;
}
}
+27 -8
View File
@@ -13,7 +13,10 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
@@ -162,6 +165,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -598,11 +604,20 @@ function layoutFor(window: FreecellWindow): Layout {
};
}
function paint(window: FreecellWindow): GrayImage {
function paint(window: FreecellWindow): Plane[] {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
const planes = singlePlane(paintContent(window));
if (window.phase === "won") {
// The win box goes on its own plane: card labels are deferred glyphs that
// render above their own image's raster, so an overlay drawn into the
// board image could not cover them.
const overlay = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
paintWinOverlay(overlay, window);
planes.push({ image: overlay, x: 0, y: 0 });
}
return planes;
}
function paintContent(window: FreecellWindow): GrayImage {
@@ -635,7 +650,7 @@ function paintContent(window: FreecellWindow): GrayImage {
paintCascade(image, window, layout, i);
}
paintCursor(image, window, layout);
if (window.phase === "won") paintWinOverlay(image, window);
// The win overlay is painted onto its own plane by paint(), not here.
return image;
}
@@ -702,7 +717,9 @@ function paintWinOverlay(image: GrayImage, window: FreecellWindow): void {
const height = 120;
const x = Math.round((window.viewportWidth - width) / 2);
const y = Math.round((window.viewportHeight - height) / 2);
image.fillRoundedRect(x, y, width, height, 0, 8);
// Fill 1, not 0: identical after 4bpp quantization, but 0 is transparent
// when this overlay composites as its own plane over the board.
image.fillRoundedRect(x, y, width, height, 1, 8);
image.drawRoundedRect(x, y, width, height, 200, 8);
drawCenteredIn(image, largeFont, x, width, y + 14, "You win!", 255);
drawCenteredIn(image, smallFont, x, width, y + 56, `${window.moves} moves`, 170);
@@ -726,11 +743,11 @@ function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: freecell content unchanged");
return;
@@ -740,7 +757,8 @@ function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -751,6 +769,7 @@ function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+25 -4
View File
@@ -1,6 +1,11 @@
import { shell } from "../../ui/shell/shell";
import { type AppDefinition } from "../app-definition";
import { createLauncherWindow, LAUNCHER_SURFACE_ID, LAUNCHER_WINDOW_ID } from "./launcher-app";
import {
getInstalledEvenHubApps,
installedEvenHubAppId,
renderInstalledEvenHubIcon,
} from "../evenhub/installed-apps";
const launcherApp: AppDefinition = {
appId: "launcher",
@@ -17,11 +22,27 @@ const launcherApp: AppDefinition = {
...ctx.actions,
requestRender: () => shell.foregroundWindow()?.requestRender(),
},
apps: ctx.apps
.filter((app) => app.showInLauncher !== false)
.map((app) => ({ appId: app.appId, label: app.title, icon: app.icon })),
apps: () => [
...ctx.apps
.filter((app) => app.showInLauncher !== false)
.map((app) => ({
appId: app.appId,
label: app.title,
icon: app.icon,
renderIcon: app.renderIcon,
})),
...getInstalledEvenHubApps().map((app) => ({
appId: installedEvenHubAppId(app.packageId),
label: app.name,
icon: "package" as const,
renderIcon: (size: number) => renderInstalledEvenHubIcon(app.packageId, size, app),
iconKey: `${app.installedAt}:${app.iconFile ?? ""}`,
uninstallable: true,
})),
],
launchApp: (appId) => ctx.launchApp(appId),
submitFrame: (image, paintMs, frameId) => ctx.submitWindowFrame(LAUNCHER_SURFACE_ID, image, paintMs, frameId),
uninstallApp: (appId) => ctx.uninstallApp(appId),
submitFrame: (planes, paintMs, frameId) => ctx.submitWindowFrame(LAUNCHER_SURFACE_ID, planes, paintMs, frameId),
setSurfaceVisible: (visible) => ctx.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible),
}),
);
+81 -19
View File
@@ -1,6 +1,8 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { type Plane } from "../../graphics/plane";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import {
@@ -26,14 +28,19 @@ export type LauncherAppEntry = {
appId: string;
label: string;
icon: IconName;
uninstallable?: boolean;
/** Dynamic artwork for installed apps; icon remains the fallback. */
renderIcon?: (size: number) => GrayImage | null;
iconKey?: string;
};
export type LauncherOptions = {
actions: LayerActions;
apps: LauncherAppEntry[];
apps: () => LauncherAppEntry[];
launchApp: (appId: string) => Promise<void> | void;
/** Submit a painted viewport-sized frame to this window's surface. */
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
uninstallApp: (appId: string) => Promise<void> | void;
/** Submit a painted viewport-sized frame (as planes) to this window's surface. */
submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise<void>;
/** Flip the launcher's compositor surface visibility on foreground changes. */
setSurfaceVisible: (visible: boolean) => void;
};
@@ -58,7 +65,7 @@ type LauncherMode = "row" | "item";
/** One cell of the grid: an app, or a folder holding some of the apps. */
type LauncherGridEntry =
| { kind: "app"; label: string; icon: IconName; appId: string }
| { kind: "app"; label: string; icon: IconName; appId: string; renderIcon?: (size: number) => GrayImage | null }
| { kind: "folder"; label: string; name: string };
/**
@@ -90,14 +97,21 @@ class LauncherGridLayer implements Layer {
* disbanded it), the view falls back to the top grid.
*/
private entries(): LauncherGridEntry[] {
const byAppId = new Map(this.options.apps.map((app) => [app.appId, app]));
const apps = this.options.apps();
const byAppId = new Map(apps.map((app) => [app.appId, app]));
if (this.currentFolder !== null) {
const members = (getFolders().get(this.currentFolder) ?? [])
.map((appId) => byAppId.get(appId))
.filter(Boolean) as LauncherAppEntry[];
if (members.length > 0) {
return members
.map((app): LauncherGridEntry => ({ kind: "app", label: app.label, icon: app.icon, appId: app.appId }))
.map((app): LauncherGridEntry => ({
kind: "app",
label: app.label,
icon: app.icon,
appId: app.appId,
renderIcon: app.renderIcon,
}))
.sort((a, b) => a.label.localeCompare(b.label));
}
this.currentFolder = null;
@@ -111,9 +125,15 @@ class LauncherGridLayer implements Layer {
entries.push({ kind: "folder", label: name, name });
}
}
for (const app of this.options.apps) {
for (const app of apps) {
if (!assignments[app.appId]) {
entries.push({ kind: "app", label: app.label, icon: app.icon, appId: app.appId });
entries.push({
kind: "app",
label: app.label,
icon: app.icon,
appId: app.appId,
renderIcon: app.renderIcon,
});
}
}
return entries.sort((a, b) => a.label.localeCompare(b.label));
@@ -155,6 +175,32 @@ class LauncherGridLayer implements Layer {
);
},
},
...(this.options.apps().find((app) => app.appId === entry.appId)?.uninstallable
? [{
label: "Uninstall",
onSelect: (ctx: LayerContext) => {
ctx.stack.pop();
ctx.stack.push(
new MenuLayer(
"Uninstall app?",
[
{
label: "Confirm uninstall",
onSelect: async (confirmCtx) => {
confirmCtx.stack.pop();
setAppFolder(entry.appId, null);
await this.options.uninstallApp(entry.appId);
this.mode = "row";
},
},
{ label: "Cancel", onSelect: (confirmCtx) => confirmCtx.stack.pop() },
],
WINDOW_MENU_LAYOUT,
),
);
},
} satisfies MenuItem]
: []),
];
}
@@ -237,14 +283,24 @@ class LauncherGridLayer implements Layer {
const blockTop = rowY(row) + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
if (blockTop >= gridBottom) break; // fully below the grid
const centerX = (index % COLS) * colW + colW / 2;
const icon = renderIcon(entry.kind === "app" ? entry.icon : "folder-filled", ICON_SIZE);
const icon = entry.kind === "app"
? entry.renderIcon?.(ICON_SIZE) ?? renderIcon(entry.icon, ICON_SIZE)
: renderIcon("folder-filled", ICON_SIZE);
if (icon) {
// Clip the icon at the grid bottom so a peeking row shows only its top.
const clipHeight = Math.min(icon.height, Math.floor(gridBottom - blockTop));
image.bitBlt(icon, Math.round(centerX - icon.width / 2), Math.round(blockTop), {
height: clipHeight,
transparentZero: true,
});
// A fully visible icon goes through the deferred-image path (texture
// cacheable); the clipped peeking row falls back to a raster blit,
// since cached draws are whole-image only.
// Icons narrower/shorter than ICON_SIZE (a non-square logo scaled to
// fit) are centered in the square the row reserves for them.
const iconX = Math.round(centerX - icon.width / 2);
const iconY = Math.round(blockTop + Math.max(0, (ICON_SIZE - icon.height) / 2));
const clipHeight = Math.min(icon.height, Math.floor(gridBottom - iconY));
if (clipHeight >= icon.height) {
image.drawImage(icon, iconX, iconY);
} else if (clipHeight > 0) {
image.bitBlt(icon, iconX, iconY, { height: clipHeight, transparentZero: true });
}
}
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
if (labelY + font.lineHeight <= gridBottom) {
@@ -357,12 +413,18 @@ export function createLauncherWindow(options: LauncherOptions): ShellWindow {
// keeps every unrelated setting change from repainting the launcher. The
// launcher is pinned for the app's lifetime, so the subscription never needs
// tearing down.
let lastFolderState = getFolderStateFingerprint();
let lastState = `${getFolderStateFingerprint()}\n${launcherAppsFingerprint(options.apps())}`;
onAnySettingChanged(() => {
const state = getFolderStateFingerprint();
if (state === lastFolderState) return;
lastFolderState = state;
const state = `${getFolderStateFingerprint()}\n${launcherAppsFingerprint(options.apps())}`;
if (state === lastState) return;
lastState = state;
created.requestRender();
});
return created.window;
}
function launcherAppsFingerprint(apps: LauncherAppEntry[]): string {
return apps
.map((app) => `${app.appId}:${app.label}:${app.icon}:${app.iconKey ?? ""}:${app.uninstallable ? 1 : 0}`)
.join("|");
}
+14 -6
View File
@@ -13,7 +13,10 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
@@ -172,6 +175,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(window, message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -552,11 +558,11 @@ function elapsedMs(window: MinesweeperWindow): number {
return window.elapsedMs + runningMs;
}
function paint(window: MinesweeperWindow): GrayImage {
function paint(window: MinesweeperWindow): Plane[] {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
return singlePlane(paintContent(window));
}
function paintContent(window: MinesweeperWindow): GrayImage {
@@ -696,11 +702,11 @@ function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: minesweeper content unchanged");
return;
@@ -710,7 +716,8 @@ function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -721,6 +728,7 @@ function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+26 -23
View File
@@ -1,5 +1,5 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage, type UiFont } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import {
@@ -9,6 +9,7 @@ import {
drawSubmenuIndicator,
scrollToKeepSelectionVisible,
} from "../../ui/menu";
import { lineStep, tightRowHeight } from "../../ui/metrics";
import {
mediaBrowserBridge,
type MediaBrowseItem,
@@ -16,10 +17,11 @@ import {
} from "../../native/media-browser";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const HEADER_HEIGHT = 34;
const ROW_HEIGHT = 16;
/** Header: title line + path line (34px at the 12px bitmap default). */
function headerHeight(font: UiFont): number {
return 8 + lineStep(font) + font.lineHeight;
}
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
export type MediaBrowseOptions = {
app: MediaBrowserApp;
@@ -67,49 +69,50 @@ export class MediaBrowseLayer implements Layer {
image.drawText(font, 20, 8, `Library: ${this.options.app.appName}`, 220);
if (this.phase === "connecting" || this.phase === "idle") {
image.drawText(font, 24, HEADER_HEIGHT + 8, `Starting ${this.options.app.appName}...`, 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, `Starting ${this.options.app.appName}...`, 150);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (this.phase === "failed") {
image.drawText(font, 24, HEADER_HEIGHT + 8, "Could not connect:", 180);
image.drawText(font, 24, HEADER_HEIGHT + 24, truncateRight(font, this.connectError, width - 48), 140);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, "Could not connect:", 180);
image.drawText(font, 24, headerHeight(font) + 8 + lineStep(font), truncateRight(font, this.connectError, width - 48), 140);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const level = this.currentLevel();
const path = this.levels.map((entry) => entry.title).join(" / ");
image.drawText(font, 20, 22, truncateLeft(font, path, width - 40), 130);
image.drawText(font, 20, 8 + lineStep(font), truncateLeft(font, path, width - 40), 130);
if (!level || level.items === null) {
image.drawText(font, 24, HEADER_HEIGHT + 8, level?.error ? level.error : "Loading...", level?.error ? 140 : 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, level?.error ? level.error : "Loading...", level?.error ? 140 : 150);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
const items = level.items;
if (!items.length) {
image.drawText(font, 24, HEADER_HEIGHT + 8, "(empty)", 120);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, "(empty)", 120);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
const listHeight = height - headerHeight(font) - font.lineHeight - 10;
const rowH = tightRowHeight(font);
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length);
const rowWidth = width - 2 * LIST_X;
const lastVisible = Math.min(items.length, level.scrollRow + visibleRows);
for (let index = level.scrollRow; index < lastVisible; index++) {
const item = items[index]!;
const y = HEADER_HEIGHT + (index - level.scrollRow) * ROW_HEIGHT;
const y = headerHeight(font) + (index - level.scrollRow) * rowH;
const selected = index === level.selectedIndex;
const highlightX = LIST_X - 6;
const highlightY = y - 1;
const highlightWidth = rowWidth + 12;
const highlightHeight = ROW_HEIGHT - 1;
const highlightHeight = rowH - 1;
if (selected) {
drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4);
}
@@ -131,8 +134,8 @@ export class MediaBrowseLayer implements Layer {
drawListScrollbar(
image,
width - 12,
HEADER_HEIGHT,
visibleRows * ROW_HEIGHT - 4,
headerHeight(font),
visibleRows * rowH - 4,
level.scrollRow,
visibleRows,
items.length,
@@ -285,7 +288,7 @@ export class MediaBrowseLayer implements Layer {
}
}
function truncateRight(font: BdfFont, text: string, maxWidth: number): string {
function truncateRight(font: UiFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
@@ -294,7 +297,7 @@ function truncateRight(font: BdfFont, text: string, maxWidth: number): string {
return `${out}...`;
}
function truncateLeft(font: BdfFont, text: string, maxWidth: number): string {
function truncateLeft(font: UiFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) {
+18 -16
View File
@@ -1,4 +1,4 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
@@ -10,6 +10,7 @@ import {
GESTURE_SCROLL_UP,
} from "../../ui/gestures";
import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../../ui/menu";
import { lineStep, tightRowHeight } from "../../ui/metrics";
import {
mediaControllerBridge,
type MediaControllerState,
@@ -33,7 +34,6 @@ const ART_X = 22;
const ART_Y = 8;
const META_X = ART_X + ART_SIZE + 14;
const LIST_TOP = 120;
const ROW_HEIGHT = 16;
const ACTION_X = 22;
const ACTION_WIDTH = 154;
const COLUMN_DIVIDER_X = 190;
@@ -81,7 +81,7 @@ class MusicAppLayer implements Layer {
width - 48,
);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, 24, 16 + index * 14, lines[index]!, 180);
image.drawText(font, 24, 16 + index * lineStep(font), lines[index]!, 180);
}
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
@@ -90,11 +90,11 @@ class MusicAppLayer implements Layer {
if (!media.available) {
image.drawText(font, 24, 16, "No active media session.", 180);
if (mediaBrowserBridge.listBrowsableApps().length) {
image.drawText(font, 24, 34, "Click to browse a music app's library,", 150);
image.drawText(font, 24, 48, "or start playback on the phone.", 150);
image.drawText(font, 24, 22 + lineStep(font), "Click to browse a music app's library,", 150);
image.drawText(font, 24, 22 + 2 * lineStep(font), "or start playback on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} browse ${GESTURE_DOUBLE_CLICK} back`, 110);
} else {
image.drawText(font, 24, 34, "Start playback in another app on the phone.", 150);
image.drawText(font, 24, 22 + lineStep(font), "Start playback in another app on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
}
return image;
@@ -103,22 +103,24 @@ class MusicAppLayer implements Layer {
this.drawArt(image, media);
const metaWidth = width - META_X - 24;
const metaStep = lineStep(font) + 1;
const titleLines = wrapText(font, media.title || "Unknown title", metaWidth).slice(0, 2);
for (let index = 0; index < titleLines.length; index++) {
image.drawText(font, META_X, ART_Y + 2 + index * 15, titleLines[index]!, 230);
image.drawText(font, META_X, ART_Y + 2 + index * metaStep, titleLines[index]!, 230);
}
image.drawText(font, META_X, ART_Y + 36, media.artist || "Unknown artist", 180);
image.drawText(font, META_X, ART_Y + 6 + 2 * metaStep, media.artist || "Unknown artist", 180);
if (media.album) {
image.drawText(font, META_X, ART_Y + 52, truncateText(font, media.album, metaWidth), 150);
image.drawText(font, META_X, ART_Y + 6 + 3 * metaStep, truncateText(font, media.album, metaWidth), 150);
}
image.drawText(font, META_X, ART_Y + 68, truncateText(font, media.appName || media.packageName, metaWidth), 110);
image.drawText(font, META_X, ART_Y + 6 + 4 * metaStep, truncateText(font, media.appName || media.packageName, metaWidth), 110);
this.drawProgress(image, media, metaWidth);
const queue = mediaControllerBridge.getQueue();
const actions = this.buildActions(media, queue);
this.reconcileSelection(actions, queue);
const listHeight = height - LIST_TOP - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
const rowH = tightRowHeight(font);
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
if (this.selectedQueueIndex < this.queueScrollRow) {
this.queueScrollRow = this.selectedQueueIndex;
} else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) {
@@ -128,12 +130,12 @@ class MusicAppLayer implements Layer {
for (let index = 0; index < actions.length; index++) {
const action = actions[index]!;
const y = LIST_TOP + index * ROW_HEIGHT;
const y = LIST_TOP + index * rowH;
const selected = index === this.selectedActionIndex;
const highlightX = ACTION_X - 6;
const highlightY = y - 1;
const highlightWidth = ACTION_WIDTH + 8;
const highlightHeight = ROW_HEIGHT - 1;
const highlightHeight = rowH - 1;
if (selected) {
drawSelectionHighlight(
image,
@@ -160,7 +162,7 @@ class MusicAppLayer implements Layer {
const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows);
for (let index = this.queueScrollRow; index < lastVisible; index++) {
const item = queue[index]!;
const y = LIST_TOP + (index - this.queueScrollRow) * ROW_HEIGHT;
const y = LIST_TOP + (index - this.queueScrollRow) * rowH;
const selected = index === this.selectedQueueIndex;
if (selected) {
drawSelectionHighlight(
@@ -168,7 +170,7 @@ class MusicAppLayer implements Layer {
QUEUE_X - 6,
y - 1,
queueWidth + 8,
ROW_HEIGHT - 1,
rowH - 1,
ctx.stack.isFocused() && this.focusColumn === "playlist",
4,
);
@@ -177,7 +179,7 @@ class MusicAppLayer implements Layer {
image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200);
}
if (queue.length > visibleRows) {
const trackHeight = visibleRows * ROW_HEIGHT - 4;
const trackHeight = visibleRows * rowH - 4;
const trackX = width - 12;
image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30);
const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0);
+14 -6
View File
@@ -8,7 +8,10 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
@@ -172,6 +175,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
case "text-input":
@@ -667,9 +673,9 @@ function handleInput(win: NavWindow, event: DashboardInputEvent, frameId: number
// ---------------------------------------------------------------------------
// Painting
function paint(win: NavWindow): GrayImage {
function paint(win: NavWindow): Plane[] {
if (win.menu?.isOpen()) return win.menu.paint();
return paintContent(win);
return singlePlane(paintContent(win));
}
function paintContent(win: NavWindow): GrayImage {
@@ -858,11 +864,11 @@ function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${win.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(win)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === win.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: navigate content unchanged");
return;
@@ -872,7 +878,8 @@ function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
win.surfaceId,
@@ -883,6 +890,7 @@ function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
win.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+1 -1
View File
@@ -1,4 +1,4 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import { NightscoutLayer } from "./nightscout";
+8 -8
View File
@@ -1,5 +1,5 @@
import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage, imageFromAsciiArt } from "../../graphics/image";
import { getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage, imageFromAsciiArt, type UiFont } from "../../graphics/image";
import { DashboardInputEvent, Layer, LayerContext } from "../../ui/layers";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import {
@@ -20,7 +20,7 @@ const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
function drawDirectionIndicator(
image: GrayImage,
font: BdfFont,
font: UiFont,
x: number,
y: number,
direction: string,
@@ -43,7 +43,7 @@ function drawDirectionIndicator(
}
}
function directionGlyphLabel(font: BdfFont, direction: string): string {
function directionGlyphLabel(font: UiFont, direction: string): string {
switch (direction) {
case "DoubleUp":
return font.hasGlyph("↑".codePointAt(0)!) ? "↑↑" : "";
@@ -98,7 +98,7 @@ function drawNightscoutGraph(
bounds: { x: number; y: number; width: number; height: number },
nightscout: NightscoutState,
nowMs: number,
font: BdfFont,
font: UiFont,
): void {
if (bounds.width <= 2 || bounds.height <= 2) {
return;
@@ -195,7 +195,7 @@ function drawNightscoutBasalOverlay(
function drawNightscoutCarbMarkers(
image: GrayImage,
bounds: { x: number; y: number; width: number; height: number },
font: BdfFont,
font: UiFont,
carbEvents: NightscoutState["carbs"],
nowMs: number,
): void {
@@ -216,7 +216,7 @@ function drawNightscoutCarbMarkers(
function drawNightscoutBolusMarkers(
image: GrayImage,
bounds: { x: number; y: number; width: number; height: number },
font: BdfFont,
font: UiFont,
bolusEvents: NightscoutState["boluses"],
nowMs: number,
): void {
@@ -246,7 +246,7 @@ function drawNightscoutBolusMarkers(
function drawTextCentered(
image: GrayImage,
font: BdfFont,
font: UiFont,
centerX: number,
y: number,
text: string,
+14 -6
View File
@@ -19,7 +19,10 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import type { DashboardInputEvent } from "../../ui/layers";
@@ -343,6 +346,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(window, message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -907,11 +913,11 @@ function drawFlipper(image: GrayImage, flipper: Flipper): void {
}
}
function paint(window: PinballWindow): GrayImage {
function paint(window: PinballWindow): Plane[] {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
return singlePlane(paintContent(window));
}
function paintContent(window: PinballWindow): GrayImage {
@@ -1025,11 +1031,11 @@ function renderAndSubmit(window: PinballWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: pinball content unchanged");
return;
@@ -1039,7 +1045,8 @@ function renderAndSubmit(window: PinballWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -1050,6 +1057,7 @@ function renderAndSubmit(window: PinballWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+13 -6
View File
@@ -7,7 +7,9 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText, wrapText } from "../../graphics/textwrap";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
@@ -174,6 +176,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
case "text-input":
@@ -513,9 +518,9 @@ async function handleRoamTool(name: string, args: any): Promise<ToolResult> {
// ---------------------------------------------------------------------------
// Painting
function paint(win: RoamWindow): GrayImage {
function paint(win: RoamWindow): Plane[] {
if (win.menu?.isOpen()) return win.menu.paint();
return paintContent(win);
return singlePlane(paintContent(win));
}
function paintContent(win: RoamWindow): GrayImage {
@@ -578,11 +583,11 @@ function renderAndSubmit(win: RoamWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${win.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(win)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === win.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: roam content unchanged");
return;
@@ -592,7 +597,8 @@ function renderAndSubmit(win: RoamWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
win.surfaceId,
@@ -603,6 +609,7 @@ function renderAndSubmit(win: RoamWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
win.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+2 -2
View File
@@ -1,4 +1,4 @@
import { GrayImage } from "../../graphics/image";
import { type Plane } from "../../graphics/plane";
import { LayerActions } from "../../ui/layers";
import { EditTextSettingLayer } from "../../ui/dashboard-settings";
import { createSettingsPanelLayer } from "../../ui/dashboard/settings-menus";
@@ -10,7 +10,7 @@ export const SETTINGS_SURFACE_ID = "window:settings";
export type SettingsAppOptions = {
actions: LayerActions;
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise<void>;
setSurfaceVisible: (visible: boolean) => void;
removeSurface: () => void;
onClosed: () => void;
+119 -53
View File
@@ -31,8 +31,10 @@
* re-attach snapshot resyncs contents and scrollback only once it's needed.
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getFont } from "../../graphics/bdffont";
import { GrayImage, type UiFont } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fonts";
import { TERMINAL_ICON_GLYPHS } from "../../graphics/icons";
import * as frameTimings from "../../native/frame-timings";
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
@@ -67,6 +69,7 @@ import {
scrollToKeepSelectionVisible,
type MenuItem,
} from "../../ui/menu";
import { lineStep, listRowHeight } from "../../ui/metrics";
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
import { appViewportSize } from "../../ui/shell/geometry";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
@@ -75,21 +78,26 @@ import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
declare const global: any;
declare const com: any;
// Terminus-12 has a 6x12 cell; each window derives its grid from the
// viewport in its open-window message (the hub is min-height, session views
// full-height). The websocket init handshake declares the view grid.
const terminalFont = getFont("terminus12");
const CELL_WIDTH = 6;
const CELL_HEIGHT = 12;
// Cell geometry comes from the terminal font setting (Settings > Terminal >
// Font; the default is Terminus-12's 6x12). Each window derives its grid from
// the viewport in its open-window message (the hub is min-height, session
// views full-height). Grid dimensions are baked into each session's websocket
// handshake, so windows and control connections capture the cell size at
// creation — a font change applies to ones (re)opened afterwards.
function chromeFont(): UiFont {
return getDefaultSmallFont();
}
// Grid of a session view window (full-height); also declared on the control
// connections so sessions launched from presets come up at view size.
const VIEW_VIEWPORT = appViewportSize("max");
const VIEW_GRID = {
cols: Math.floor(VIEW_VIEWPORT.width / CELL_WIDTH),
rows: Math.floor(VIEW_VIEWPORT.height / CELL_HEIGHT),
};
function viewGrid(): { cols: number; rows: number } {
const { cellWidth, cellHeight } = getTerminalFontConfig();
return {
cols: Math.floor(VIEW_VIEWPORT.width / cellWidth),
rows: Math.floor(VIEW_VIEWPORT.height / cellHeight),
};
}
const DEVICE_NAME = "Faceclaw G2";
const HUB_ROW_HEIGHT = 20;
const RENDER_COALESCE_MS = 33;
const HISTORY_PAGE = 200;
// Auto-reconnect backoff: doubles per failed attempt, resets on success.
@@ -146,6 +154,10 @@ type HubWindow = BaseWindow & {
type ViewWindow = BaseWindow & {
kind: "view";
/** Grid font and integer cell pitch, captured at window creation. */
font: UiFont;
cellWidth: number;
cellHeight: number;
connectionId: string;
socket: string;
label: string;
@@ -308,6 +320,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -425,14 +440,15 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb
renderHubWindows();
return;
}
const hubCell = getTerminalFontConfig();
windows.set(windowId, {
kind: "hub",
windowId,
surfaceId,
viewportWidth: viewport.width,
viewportHeight: viewport.height,
gridCols: Math.floor(viewport.width / CELL_WIDTH),
gridRows: Math.floor(viewport.height / CELL_HEIGHT),
gridCols: Math.floor(viewport.width / hubCell.cellWidth),
gridRows: Math.floor(viewport.height / hubCell.cellHeight),
foreground: false,
focused: false,
renderScheduled: false,
@@ -488,8 +504,7 @@ function clientOptionsFor(connection: TerminalConnection): G2MirrorClientOptions
return {
...parsed,
deviceName: DEVICE_NAME,
cols: VIEW_GRID.cols,
rows: VIEW_GRID.rows,
...viewGrid(),
};
}
@@ -684,8 +699,12 @@ function createViewWindow(
view: PendingView,
): ViewWindow {
const { connectionId, socket, label, glyph } = view;
const gridCols = Math.floor(viewport.width / CELL_WIDTH);
const gridRows = Math.floor(viewport.height / CELL_HEIGHT);
// Capture the terminal font at open time: the grid dims go into the
// websocket handshake and the emulator, so they must not change under a
// live window (a font-setting change applies to the next window).
const cell = getTerminalFontConfig();
const gridCols = Math.floor(viewport.width / cell.cellWidth);
const gridRows = Math.floor(viewport.height / cell.cellHeight);
const client = new G2MirrorClient({ ...view.options, cols: gridCols, rows: gridRows });
const window: ViewWindow = {
kind: "view",
@@ -695,6 +714,9 @@ function createViewWindow(
viewportHeight: viewport.height,
gridCols,
gridRows,
font: cell.font,
cellWidth: cell.cellWidth,
cellHeight: cell.cellHeight,
foreground: false,
focused: false,
renderScheduled: false,
@@ -1367,11 +1389,11 @@ async function launchAndOpenView(control: ControlConnection, preset: string): Pr
return socket;
}
function paint(window: TerminalWindow): GrayImage {
function paint(window: TerminalWindow): Plane[] {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
return singlePlane(paintContent(window));
}
function paintContent(window: TerminalWindow): GrayImage {
@@ -1383,64 +1405,72 @@ function paintHub(window: HubWindow): GrayImage {
return paintAddConnection(window);
}
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
const font = chromeFont();
const step = lineStep(font);
// No border box: the shell chrome (top bar + sidebar) already frames the app.
image.drawText(terminalFont, 18, 10, window.mode === "connections" ? "Terminal - Connections" : "Terminal", 220);
image.drawText(terminalFont, 24, 30, hubStatusLine(window), 170);
image.drawText(font, 18, 10, window.mode === "connections" ? "Terminal - Connections" : "Terminal", 220);
const statusY = 16 + step;
image.drawText(font, 24, statusY, hubStatusLine(window), 170);
let listTop = 52;
let listTop = statusY + step + 8;
if (window.mode === "sessions" && controls.size === 0) {
image.drawText(terminalFont, 24, 46, "Add a g2mirror:// connection to get started, see:", 150);
image.drawText(terminalFont, 24, 60, "https://github.com/jimrandomh/g2mirror", 190);
listTop += 28;
image.drawText(font, 24, statusY + step + 2, "Add a g2mirror:// connection to get started, see:", 150);
image.drawText(font, 24, statusY + 2 * step + 2, "https://github.com/jimrandomh/g2mirror", 190);
listTop += 2 * step;
}
const items = hubItems(window);
clampHubSelection(window, items);
const visibleRowCount = Math.max(1, ((window.viewportHeight - 30 - listTop) / HUB_ROW_HEIGHT) | 0);
const hubRowH = listRowHeight(chromeFont());
const visibleRowCount = Math.max(1, ((window.viewportHeight - 30 - listTop) / hubRowH) | 0);
window.scrollRow = scrollToKeepSelectionVisible(window.scrollRow, window.selectedIndex, visibleRowCount, items.length);
const lastVisibleRow = Math.min(items.length, window.scrollRow + visibleRowCount);
for (let index = window.scrollRow; index < lastVisibleRow; index++) {
const y = listTop + (index - window.scrollRow) * HUB_ROW_HEIGHT;
const y = listTop + (index - window.scrollRow) * hubRowH;
const item = items[index]!;
if (item.heading) {
image.drawText(terminalFont, 20, y + 2, item.label, 140);
image.drawText(chromeFont(), 20, y + 2, item.label, 140);
continue;
}
const selected = index === window.selectedIndex;
if (selected) {
// Match the shell convention: fill only when this window has focus, so
// an outline-only selection signals the sidebar owns input.
drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, HUB_ROW_HEIGHT - 1, window.focused, 8);
drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8);
}
image.drawText(terminalFont, 32, y + 2, item.label, selected ? 255 : 200);
image.drawText(chromeFont(), 32, y + 2, item.label, selected ? 255 : 200);
}
if (items.length > visibleRowCount) {
drawListScrollbar(
image,
window.viewportWidth - 10,
listTop,
visibleRowCount * HUB_ROW_HEIGHT - 4,
visibleRowCount * hubRowH - 4,
window.scrollRow,
visibleRowCount,
items.length,
);
}
image.drawText(terminalFont, 24, window.viewportHeight - 24, `${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 24, window.viewportHeight - font.lineHeight - 12, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
function paintAddConnection(window: HubWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
image.drawText(terminalFont, 18, 10, "Add connection", 220);
image.drawText(terminalFont, 24, 34, "Type the g2mirror://token@host string in the", 170);
image.drawText(terminalFont, 24, 48, "phone app (or use voice input from the menu).", 170);
const font = chromeFont();
const step = lineStep(font);
image.drawText(font, 18, 10, "Add connection", 220);
const instructionsY = 20 + step;
image.drawText(font, 24, instructionsY, "Type the g2mirror://token@host string in the", 170);
image.drawText(font, 24, instructionsY + step, "phone app (or use voice input from the menu).", 170);
const draftY = instructionsY + 3 * step;
const draft = terminalNewConnectionSetting.get();
image.drawText(terminalFont, 24, 76, truncateLabel(draft || "(empty)", 52), 220);
image.drawText(font, 24, draftY, truncateLabel(draft || "(empty)", 52), 220);
if (window.addError) {
image.drawText(terminalFont, 24, 96, window.addError, 150);
image.drawText(font, 24, draftY + step + 6, window.addError, 150);
}
image.drawText(terminalFont, 24, window.viewportHeight - 24, `click save ${GESTURE_DOUBLE_CLICK} cancel`, 110);
image.drawText(font, 24, window.viewportHeight - font.lineHeight - 12, `click save ${GESTURE_DOUBLE_CLICK} cancel`, 110);
return image;
}
@@ -1465,11 +1495,38 @@ function hubStatusLine(window: HubWindow): string {
return anyRetrying ? `${summary} Retrying...` : summary;
}
/**
* Draw one terminal row at a fixed integer cell pitch, so glyph columns stay
* aligned with the emulator's grid arithmetic even when a TTF face has
* fractional advances. Identical to drawText for the 6px bitmap default.
*/
function drawGridRow(
image: GrayImage,
font: UiFont,
cellWidth: number,
rowY: number,
text: string,
value: number,
): void {
let col = 0;
for (const char of text) {
const codePoint = char.codePointAt(0) ?? 32;
if (codePoint !== 32) {
const glyph = font.getGlyph(codePoint);
if (glyph && glyph.bbxWidth > 0) {
image.drawGlyph(font, glyph, col * cellWidth, rowY, value);
}
}
col++;
}
}
function paintView(window: ViewWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
if (!window.receivedData) {
image.drawText(terminalFont, 24, 110, window.status, 170);
image.drawText(terminalFont, 24, 130, `${GESTURE_DOUBLE_CLICK} back`, 110);
const font = chromeFont();
image.drawText(font, 24, 110, window.status, 170);
image.drawText(font, 24, 116 + lineStep(font), `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
@@ -1483,10 +1540,20 @@ function paintView(window: ViewWindow): GrayImage {
// Draw the cursor only if its line is within the visible window.
const cursorScreenRow = historyNext + window.emulator.cursorRow() - top;
if (cursorScreenRow >= 0 && cursorScreenRow < rows) {
image.fillRect(window.emulator.cursorCol() * CELL_WIDTH, cursorScreenRow * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT, 70);
image.fillRect(window.emulator.cursorCol() * window.cellWidth, cursorScreenRow * window.cellHeight, window.cellWidth, window.cellHeight, 70);
}
// Stale content stays visible across a disconnect, so flag it: a status
// line over the bottom row whenever the session isn't actually attached.
// Text is deferred glyphs (always on top of raster), so the covered rows
// must be suppressed rather than painted over.
const statusBannerY = window.client.state().phase !== "attached"
? window.viewportHeight - window.cellHeight
: null;
for (let row = 0; row < rows; row++) {
const rowY = row * window.cellHeight;
if (statusBannerY !== null && rowY + window.cellHeight > statusBannerY) continue;
const absolute = top + row;
let text = "";
if (absolute >= historyNext) {
@@ -1495,18 +1562,15 @@ function paintView(window: ViewWindow): GrayImage {
} else if (absolute >= window.archiveStart) {
text = window.archive[absolute - window.archiveStart] ?? "";
}
if (text.length) image.drawText(terminalFont, 0, row * CELL_HEIGHT, text, 200);
if (text.length) drawGridRow(image, window.font, window.cellWidth, rowY, text, 200);
}
if (!following) {
drawScrollIndicator(image, top, window.archiveStart, bottomTop);
}
// Stale content stays visible across a disconnect, so flag it: a status
// line over the bottom row whenever the session isn't actually attached.
if (window.client.state().phase !== "attached") {
const y = window.viewportHeight - CELL_HEIGHT;
image.fillRect(0, y, window.viewportWidth, CELL_HEIGHT, 0);
image.drawText(terminalFont, 0, y, window.status, 170);
if (statusBannerY !== null) {
image.fillRect(0, statusBannerY, window.viewportWidth, window.cellHeight, 0);
image.drawText(chromeFont(), 0, statusBannerY, window.status, 170);
}
return image;
}
@@ -1539,11 +1603,11 @@ function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: terminal content unchanged");
return;
@@ -1553,7 +1617,8 @@ function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -1564,6 +1629,7 @@ function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+33 -22
View File
@@ -4,8 +4,11 @@
* durable expiry path; worker timeouts keep the live UI prompt.
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import { type UiFont, GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import {
cancelTimerNotification,
@@ -33,7 +36,10 @@ const FOOTER_HEIGHT = 34;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
const smallFont = getDefaultSmallFont();
// Resolved per use so a font-setting change applies without a worker restart.
function smallFont(): UiFont {
return getDefaultSmallFont();
}
type TimerView = "stopwatch" | "timers" | "editor";
@@ -156,6 +162,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -498,11 +507,11 @@ function updateRenderTimer(window: TimerWindow): void {
}
}
function paint(window: TimerWindow): GrayImage {
function paint(window: TimerWindow): Plane[] {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
return singlePlane(paintContent(window));
}
function paintContent(window: TimerWindow): GrayImage {
@@ -526,7 +535,7 @@ function paintStopwatch(image: GrayImage, window: TimerWindow): void {
image.drawText(largeFont, timeX, 66, timeLabel, 245);
const state = stopwatchRunningSinceMs === null ? (elapsed > 0 ? "Paused" : "Ready") : "Running";
drawCenteredText(image, smallFont, 112, state, 145);
drawCenteredText(image, smallFont(), 112, state, 145);
const labels = [stopwatchRunningSinceMs === null ? "Start" : "Pause", "Reset", `Timers (${timers.length})`];
drawButtonRow(image, labels, window.stopwatchAction, 154);
@@ -549,7 +558,7 @@ function paintTimers(image: GrayImage, window: TimerWindow): void {
image.fillRoundedRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 34, 5);
image.drawRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 105);
}
image.drawText(index >= 2 ? mediumFont : smallFont, 32, y + (index >= 2 ? 2 : 7), items[index]!, selected ? 245 : 180);
image.drawText(index >= 2 ? mediumFont : smallFont(), 32, y + (index >= 2 ? 2 : 7), items[index]!, selected ? 245 : 180);
}
const selectedTimer = window.timerSelection >= 2 ? timers[window.timerSelection - 2] : null;
@@ -558,7 +567,7 @@ function paintTimers(image: GrayImage, window: TimerWindow): void {
}
function paintEditor(image: GrayImage, window: TimerWindow): void {
image.drawText(smallFont, 24, 14, "New timer", 190);
image.drawText(smallFont(), 24, 14, "New timer", 190);
const fields = [pad2(window.editorHours), pad2(window.editorMinutes), pad2(window.editorSeconds)];
const labels = ["hours", "minutes", "seconds"];
const fieldWidth = 118;
@@ -573,7 +582,7 @@ function paintEditor(image: GrayImage, window: TimerWindow): void {
image.drawRect(x, 57, fieldWidth, 68, 120);
}
drawCenteredIn(image, largeFont, x, fieldWidth, 65, fields[index]!, window.editorField === index ? 250 : 180);
drawCenteredIn(image, smallFont, x, fieldWidth, 105, labels[index]!, 125);
drawCenteredIn(image, smallFont(), x, fieldWidth, 105, labels[index]!, 125);
if (index < 2) image.drawText(mediumFont, x + fieldWidth + 5, 71, ":", 130);
}
@@ -584,8 +593,8 @@ function paintEditor(image: GrayImage, window: TimerWindow): void {
image.fillRoundedRect(buttonX, 151, buttonWidth, 38, 40, 6);
image.drawRect(buttonX, 151, buttonWidth, 38, 120);
}
drawCenteredIn(image, smallFont, buttonX, buttonWidth, 162, "Start timer", startSelected ? 250 : 175);
if (window.editorMessage) drawCenteredText(image, smallFont, 202, window.editorMessage, 190);
drawCenteredIn(image, smallFont(), buttonX, buttonWidth, 162, "Start timer", startSelected ? 250 : 175);
if (window.editorMessage) drawCenteredText(image, smallFont(), 202, window.editorMessage, 190);
drawFooter(image, `${GESTURE_SCROLL} adjust ${GESTURE_CLICK} next ${GESTURE_DOUBLE_CLICK} cancel`);
}
@@ -593,11 +602,11 @@ function drawTabs(image: GrayImage, active: "stopwatch" | "timers"): void {
const stopwatch = "Stopwatch";
const timerLabel = `Timers (${timers.length})`;
const left = 24;
const dividerX = left + smallFont.measureText(stopwatch) + 18;
image.drawText(smallFont, left, 15, stopwatch, active === "stopwatch" ? 245 : 105);
const dividerX = left + smallFont().measureText(stopwatch) + 18;
image.drawText(smallFont(), left, 15, stopwatch, active === "stopwatch" ? 245 : 105);
image.drawLine(left, 35, dividerX - 12, 35, active === "stopwatch" ? 200 : 30);
image.drawText(smallFont, dividerX, 15, timerLabel, active === "timers" ? 245 : 105);
image.drawLine(dividerX, 35, dividerX + smallFont.measureText(timerLabel), 35, active === "timers" ? 200 : 30);
image.drawText(smallFont(), dividerX, 15, timerLabel, active === "timers" ? 245 : 105);
image.drawLine(dividerX, 35, dividerX + smallFont().measureText(timerLabel), 35, active === "timers" ? 200 : 30);
}
function drawButtonRow(image: GrayImage, labels: string[], selected: number, y: number): void {
@@ -610,22 +619,22 @@ function drawButtonRow(image: GrayImage, labels: string[], selected: number, y:
image.fillRoundedRect(x, y, width, 40, 38, 6);
image.drawRect(x, y, width, 40, 115);
}
drawCenteredIn(image, smallFont, x, width, y + 12, labels[index]!, index === selected ? 245 : 155);
drawCenteredIn(image, smallFont(), x, width, y + 12, labels[index]!, index === selected ? 245 : 155);
}
}
function drawFooter(image: GrayImage, text: string): void {
image.drawLine(16, image.height - FOOTER_HEIGHT, image.width - 16, image.height - FOOTER_HEIGHT, 35);
drawCenteredText(image, smallFont, image.height - 22, text, 115);
drawCenteredText(image, smallFont(), image.height - 22, text, 115);
}
function drawCenteredText(image: GrayImage, font: typeof smallFont, y: number, text: string, value: number): void {
function drawCenteredText(image: GrayImage, font: UiFont, y: number, text: string, value: number): void {
image.drawText(font, Math.round((image.width - font.measureText(text)) / 2), y, text, value);
}
function drawCenteredIn(
image: GrayImage,
font: typeof smallFont,
font: UiFont,
x: number,
width: number,
y: number,
@@ -645,11 +654,11 @@ function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
const fingerprint = planesFingerprint(planes);
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: timer content unchanged");
return;
@@ -659,7 +668,8 @@ function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -670,6 +680,7 @@ function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+8 -6
View File
@@ -1,5 +1,6 @@
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage, type UiFont } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { lineStep } from "../../ui/metrics";
import { writeTextToDownloads } from "../../native/file-access";
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
@@ -41,12 +42,13 @@ export class TranscribeLayer implements Layer {
image.drawText(font, 24, 20, "Transcribe", 200);
image.drawText(font, 24, 40, this.saveNotice || this.status, 110);
const step = lineStep(font) + 2;
const bodyTop = 62;
const footerY = height - 18;
const bodyLines = Math.max(1, Math.floor((footerY - bodyTop) / 16));
const footerY = height - font.lineHeight - 6;
const bodyLines = Math.max(1, Math.floor((footerY - bodyTop) / step));
const firstLine = Math.max(0, wrapped.length - bodyLines);
for (let index = firstLine; index < wrapped.length; index++) {
const y = bodyTop + (index - firstLine) * 16;
const y = bodyTop + (index - firstLine) * step;
image.drawText(font, 32, y, wrapped[index]!, 230);
}
@@ -112,7 +114,7 @@ function transcriptTimestamp(): string {
);
}
function wrapTranscribeText(font: BdfFont, text: string, maxWidth: number): string[] {
function wrapTranscribeText(font: UiFont, text: string, maxWidth: number): string[] {
const words = text.split(/\s+/).filter(Boolean);
const lines: string[] = [];
let line = "";
+10 -7
View File
@@ -1,11 +1,12 @@
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage, type UiFont } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
import { tightRowHeight } from "../../ui/metrics";
const PAGE_X = 18;
const HEADER_Y = 8;
@@ -13,7 +14,8 @@ const CURRENT_TOP = 34;
const CURRENT_TEXT_X = 112;
const FORECAST_HEADER_Y = 105;
const FORECAST_TOP = 124;
const FORECAST_ROW_HEIGHT = 23;
/** Forecast rows hold a 20px icon; grow with the font past that. */
const FORECAST_MIN_ROW_HEIGHT = 23;
const FOOTER_HEIGHT = 18;
const FORECAST_NAME_WIDTH = 92;
const FORECAST_TEMP_X = 126;
@@ -133,7 +135,8 @@ export class WeatherLayer implements Layer {
return;
}
this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1);
const visibleRows = Math.max(1, Math.floor((height - FOOTER_HEIGHT - FORECAST_TOP) / FORECAST_ROW_HEIGHT));
const rowH = Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3);
const visibleRows = Math.max(1, Math.floor((height - FOOTER_HEIGHT - FORECAST_TOP) / rowH));
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, forecast.length);
image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150);
@@ -144,10 +147,10 @@ export class WeatherLayer implements Layer {
const last = Math.min(forecast.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < last; index++) {
const period = forecast[index]!;
const y = FORECAST_TOP + (index - this.scrollRow) * FORECAST_ROW_HEIGHT;
const y = FORECAST_TOP + (index - this.scrollRow) * rowH;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), FORECAST_ROW_HEIGHT - 2, ctx.stack.isFocused(), 5);
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), rowH - 2, ctx.stack.isFocused(), 5);
}
const shade = selected ? 245 : 190;
image.drawText(font, PAGE_X, y + 2, truncateText(font, period.name, FORECAST_NAME_WIDTH), shade);
@@ -167,7 +170,7 @@ export class WeatherLayer implements Layer {
private drawMessage(
image: GrayImage,
font: BdfFont,
font: UiFont,
width: number,
height: number,
message: string,
+2 -1
View File
@@ -1,4 +1,5 @@
import { toolRegistry, type ToolRegistry } from "./tool-registry";
import { FACECLAW_VERSION } from "../version";
/**
* Minimal MCP (Model Context Protocol) server surface over the assistant
@@ -51,7 +52,7 @@ export class AssistantMcpServer {
? msg.params.protocolVersion
: MCP_PROTOCOL_VERSION,
capabilities: { tools: { listChanged: true } },
serverInfo: { name: "faceclaw", version: "0.3.0" },
serverInfo: { name: "faceclaw", version: FACECLAW_VERSION },
});
return;
case "notifications/initialized":
+49 -10
View File
@@ -3,6 +3,13 @@
* list / focus / close shell windows, and manage the launcher's folder
* grouping. Registered from the dashboard controller (which owns launchApp);
* mirrors what the user can do from the launcher grid and the sidebar.
*
* The launchable set is the built-in launcher apps plus whatever EvenHub
* packages are installed right now, matching the launcher grid. Installs and
* uninstalls change that set at runtime, so the tools re-register (replacing
* themselves by name) whenever it changes to keep the id enums and
* descriptions current; register() fires the tools-changed broadcast that
* propagates the new specs to the assistant backends.
*/
import { shell, type ShellWindow } from "../ui/shell/shell";
import type { AppDefinition } from "../apps/app-definition";
@@ -14,10 +21,16 @@ import {
resolveFolderName,
setAppFolder,
} from "../apps/launcher/launcher-folders";
import {
getInstalledEvenHubApps,
getInstalledEvenHubFingerprint,
installedEvenHubAppId,
} from "../apps/evenhub/installed-apps";
import { onAnySettingChanged } from "../ui/dashboard-settings";
import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry";
export type WindowToolsOptions = {
/** The launcher's app list; fixes the ids apps.launch accepts. */
/** The built-in launcher apps; installed EvenHub apps are added dynamically. */
apps: readonly AppDefinition[];
/** Launch (or focus) an app exactly as the launcher grid would. */
launchApp: (appId: string) => Promise<void> | void;
@@ -33,14 +46,40 @@ export function registerWindowTools(
): void {
if (registered) return;
registered = true;
registerTools(options, registry);
let fingerprint = getInstalledEvenHubFingerprint();
onAnySettingChanged(() => {
const current = getInstalledEvenHubFingerprint();
if (current === fingerprint) return;
fingerprint = current;
registerTools(options, registry);
});
}
const appIds = options.apps.map((app) => app.appId).sort();
function registerTools(options: WindowToolsOptions, registry: ToolRegistry): void {
const installedApps = getInstalledEvenHubApps();
const apps = new Map(options.apps.map((app) => [app.appId, app.title]));
for (const app of installedApps) {
apps.set(installedEvenHubAppId(app.packageId), app.name);
}
const appIds = Array.from(apps.keys()).sort();
/** An id plus its display name, when the name isn't obvious from the id. */
const describeApp = (appId: string): string => {
const title = apps.get(appId);
return title && title.toLowerCase() !== appId.toLowerCase() ? `${appId} ("${title}")` : appId;
};
const installedNote = installedApps.length
? ` Installed EvenHub apps: ${installedApps
.map((app) => describeApp(installedEvenHubAppId(app.packageId)))
.join(", ")}.`
: "";
registry.registerSystemTool(
{
name: "apps.launch",
description:
"Open an app on the glasses and bring it to the foreground. If the app already has a window open, this focuses it instead. Wakes the display if it is off.",
"Open an app on the glasses and bring it to the foreground. If the app already has a window open, this focuses it instead. Wakes the display if it is off." +
installedNote,
inputSchema: {
type: "object",
properties: {
@@ -57,7 +96,7 @@ export function registerWindowTools(
}
shell.wake("window");
await options.launchApp(appId);
return ok(`Opened ${appId}.`);
return ok(`Opened ${describeApp(appId)}.`);
},
);
@@ -144,7 +183,7 @@ export function registerWindowTools(
{
name: "apps.list_folders",
description:
"List the app launcher's folders and the apps inside each, plus the ungrouped top-level apps.",
"List the app launcher's folders and the apps inside each, plus the ungrouped top-level apps. Includes installed EvenHub apps, shown as app_id (\"display name\").",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
proactive: true,
},
@@ -152,11 +191,11 @@ export function registerWindowTools(
const lines: string[] = [];
for (const [name, members] of getFolders()) {
const known = members.filter((appId) => appIds.indexOf(appId) >= 0).sort();
if (known.length) lines.push(`${name}: ${known.join(", ")}`);
if (known.length) lines.push(`${name}: ${known.map(describeApp).join(", ")}`);
}
const assignments = getFolderAssignments();
const ungrouped = appIds.filter((appId) => !assignments[appId]);
lines.push(`Ungrouped: ${ungrouped.length ? ungrouped.join(", ") : "(none)"}`);
lines.push(`Ungrouped: ${ungrouped.length ? ungrouped.map(describeApp).join(", ") : "(none)"}`);
return ok(lines.join("\n"));
},
);
@@ -190,7 +229,7 @@ export function registerWindowTools(
return err(`Folder names are limited to ${FOLDER_NAME_MAX_LENGTH} characters.`);
}
setAppFolder(appId, folder);
return ok(`Moved ${appId} into "${folder}".`);
return ok(`Moved ${describeApp(appId)} into "${folder}".`);
},
);
@@ -214,9 +253,9 @@ export function registerWindowTools(
return err(`Unknown app: ${appId}. Available apps: ${appIds.join(", ")}`);
}
const folder = getFolderAssignments()[appId];
if (!folder) return ok(`${appId} isn't in a folder.`);
if (!folder) return ok(`${describeApp(appId)} isn't in a folder.`);
setAppFolder(appId, null);
return ok(`Removed ${appId} from "${folder}".`);
return ok(`Removed ${describeApp(appId)} from "${folder}".`);
},
);
+96
View File
@@ -0,0 +1,96 @@
Copyright 2014-2025 Adobe (http://www.adobe.com/), with Reserved Font
Name 'Source'. Source is a trademark of Adobe in the United States
and/or other countries.
This Font Software is licensed under the SIL Open Font License,
Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font
creation efforts of academic and linguistic communities, and to
provide a free and open framework in which fonts may be shared and
improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply to
any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software
components as distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to,
deleting, or substituting -- in part or in whole -- any of the
components of the Original Version, by changing formats or by porting
the Font Software to a new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed,
modify, redistribute, and sell modified and unmodified copies of the
Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, in
Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the
corresponding Copyright Holder. This restriction only applies to the
primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created using
the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+93
View File
@@ -0,0 +1,93 @@
Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
https://openfontlicense.org
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
+93
View File
@@ -0,0 +1,93 @@
Copyright 2024 The Montserrat.Git Project Authors (https://github.com/JulietaUla/Montserrat.git)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
https://openfontlicense.org
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
+93
View File
@@ -0,0 +1,93 @@
Copyright 2011 The Roboto Project Authors (https://github.com/googlefonts/roboto-classic)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
https://openfontlicense.org
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
+93
View File
@@ -0,0 +1,93 @@
Copyright 2015 The Roboto Mono Project Authors (https://github.com/googlefonts/robotomono)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
https://openfontlicense.org
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+213 -63
View File
@@ -16,7 +16,9 @@ import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboa
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
import { voiceControlBridge } from "../native/voice-control";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
import { getDefaultMediumFont } from "../graphics/bdffont";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
import { prepareFrameDraws } from "../graphics/glyph-wire";
import { getDefaultMediumFont } from "../graphics/ui-fonts";
import { wrapText } from "../graphics/textwrap";
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
import { registerSystemTools } from "../assistant/system-tools";
@@ -34,6 +36,12 @@ import { appViewportRect, type WindowHeightMode } from "../ui/shell/geometry";
import { type LayerActions } from "../ui/layers";
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
import {
getInstalledEvenHubAppById,
installedEvenHubPackageId,
uninstallEvenHubPackage,
} from "../apps/evenhub/installed-apps";
import { closeRunningPackage, launchInstalledPackage } from "../apps/evenhub/manager";
type ConnectionPhase = "disconnected" | "connecting" | "connected" | "charging" | "disconnecting";
@@ -49,8 +57,14 @@ export type DashboardSnapshot = {
*/
displayPreviewMessage: string;
activeTextSettingId: string | null;
activeTextEditorTitle: string;
activeTextSettingTitle: string;
activeTextSettingValue: string;
activeTextSettingInputKind: "text" | "email" | "password";
secondaryTextSettingId: string | null;
secondaryTextSettingTitle: string;
secondaryTextSettingValue: string;
secondaryTextSettingInputKind: "text" | "email" | "password";
evenAppConflictMessage: string;
evenAppConflictWarningVisible: boolean;
firmwareWarningMessage: string;
@@ -134,7 +148,9 @@ class DashboardController {
private phase: ConnectionPhase = "disconnected";
private status = "Disconnected.";
private log = "";
private activeTextSetting: ConfigSettingString | null = null;
private activeTextSettings: ConfigSettingString[] = [];
private activeTextEditorTitle = "";
private activeTextEditorOnFinish: (() => void) | null = null;
private evenNotificationActive = false;
private evenAppConflictMessage = "";
private firmwareWarningMessage = "";
@@ -184,6 +200,8 @@ class DashboardController {
private lastSys = "none yet";
private shellRenderInProgress = false;
private shellRenderQueued = false;
/** Input frame that requested the next shell render; see requestShellRender. */
private pendingShellRenderCauseFrameId = 0;
private nextShellRenderWantsFreshData = false;
// One shared worker per app hosts all its windows; spawned on first launch.
private readonly appHosts = new Map<string, WorkerAppHost>();
@@ -204,6 +222,11 @@ class DashboardController {
const sharedActions = {
disconnect: () => this.disconnect(),
startTextSettingEdit: (setting: ConfigSettingString) => this.startTextSettingEdit(setting),
startTextSettingsEdit: (
settings: readonly ConfigSettingString[],
title: string,
onFinish?: () => void,
) => this.startTextSettingsEdit(settings, title, onFinish),
endTextSettingEdit: () => this.endTextSettingEdit(),
startVoiceCapture: () => this.startVoiceCapture(),
stopVoiceCapture: () => this.stopVoiceCapture(),
@@ -446,7 +469,14 @@ class DashboardController {
* visible. Concurrent display-wake, shell, and wakeword callbacks share the
* same operation.
*/
private ensureEvenHubSessionActive(): Promise<boolean> {
/**
* frameId (0 when the caller has none) attributes the barrier's four
* round trips to the input frame waiting on them; a wake barrier is the
* largest single input-to-display term there is, so knowing which step
* owns the second matters. Callers that join an in-flight barrier share
* the first caller's spans.
*/
private ensureEvenHubSessionActive(frameId = 0): Promise<boolean> {
if (this.evenHubResumePromise) return this.evenHubResumePromise;
const communicator = this.communicator;
if (!communicator || this.phase === "charging" || this.phase === "disconnected") {
@@ -454,14 +484,19 @@ class DashboardController {
}
const operation = (async () => {
await communicator.setG2ScreenOn(true);
if (!(await communicator.resumeEvenHubSession())) {
await frameTimings.spanAsync(frameId, "wake:screen-on", () => communicator.setG2ScreenOn(true));
const resumed = await frameTimings.spanAsync(frameId, "wake:resume-session", () =>
communicator.resumeEvenHubSession(),
);
if (!resumed) {
return false;
}
// Resume first: setScreenBlanked(false) then recomposites retained state
// as the desired first frame for the fresh layout.
await communicator.setScreenBlanked(false);
const ready = await communicator.awaitEvenHubSessionReady(EVENHUB_WAKE_READY_TIMEOUT_MS);
await frameTimings.spanAsync(frameId, "wake:unblank", () => communicator.setScreenBlanked(false));
const ready = await frameTimings.spanAsync(frameId, "wake:await-ready", () =>
communicator.awaitEvenHubSessionReady(EVENHUB_WAKE_READY_TIMEOUT_MS),
);
if (ready && this.communicator === communicator) {
this.evenHubSessionSuspended = false;
}
@@ -675,15 +710,23 @@ class DashboardController {
}
snapshot(): DashboardSnapshot {
const primaryTextSetting = this.activeTextSettings[0] ?? null;
const secondaryTextSetting = this.activeTextSettings[1] ?? null;
return {
phase: this.phase,
status: this.status,
log: this.log,
displayPreview: this.displayPreview,
displayPreviewMessage: this.displayPreviewMessage(),
activeTextSettingId: this.activeTextSetting?.id ?? null,
activeTextSettingTitle: this.activeTextSetting?.editorTitle ?? "",
activeTextSettingValue: this.activeTextSetting?.get() ?? "",
activeTextSettingId: primaryTextSetting?.id ?? null,
activeTextEditorTitle: this.activeTextEditorTitle,
activeTextSettingTitle: primaryTextSetting?.editorTitle ?? "",
activeTextSettingValue: primaryTextSetting?.get() ?? "",
activeTextSettingInputKind: primaryTextSetting?.inputKind ?? "text",
secondaryTextSettingId: secondaryTextSetting?.id ?? null,
secondaryTextSettingTitle: secondaryTextSetting?.editorTitle ?? "",
secondaryTextSettingValue: secondaryTextSetting?.get() ?? "",
secondaryTextSettingInputKind: secondaryTextSetting?.inputKind ?? "text",
evenAppConflictMessage: this.evenAppConflictMessage,
evenAppConflictWarningVisible: this.evenAppConflictMessage.length > 0,
firmwareWarningMessage: this.firmwareWarningMessage,
@@ -763,9 +806,11 @@ class DashboardController {
openEvenAppSettings();
}
setActiveTextSettingValue(value: string): void {
setActiveTextSettingValue(value: string, settingId?: string): void {
shell.noteUserActivity();
const setting = this.activeTextSetting;
const setting = settingId
? this.activeTextSettings.find((candidate) => candidate.id === settingId) ?? null
: this.activeTextSettings[0] ?? null;
if (!setting) return;
if (setting.get() === value) return;
setting.set(value);
@@ -1106,18 +1151,33 @@ class DashboardController {
if (leaseReleased === true) {
this.faceclawWakeLeaseState = false;
}
const shutdownAcked = await communicator?.sendShutdown(0).catch((error) => {
this.appendLog(`shutdown command failed: ${this.formatError(error)}`);
return false;
});
if (shutdownAcked === true) {
this.appendLog("Shutdown command completed.");
} else if (communicator) {
this.appendLog("Shutdown command did not complete before disconnect.");
}
// Quiesce every producer that can enqueue a glasses command before the
// firmware cleanup barrier. On cleanup-capable CFW this leaves mode 11 as
// Faceclaw's final BLE message before the transport closes.
await mediaControllerBridge.stop().catch(() => {});
await nightscoutBridge.stop().catch(() => {});
voiceControlBridge.stop();
const cleanupAcked = await communicator?.sendCfwCleanup().catch((error) => {
this.appendLog(`CFW cleanup failed: ${this.formatError(error)}`);
return false;
});
if (cleanupAcked === true) {
this.appendLog("CFW cleanup completed.");
} else if (communicator) {
// Older CFWs do not advertise cleanup11. Preserve their established
// teardown behavior; close() will also send the legacy FB lease release.
const shutdownAcked = await communicator.sendShutdown(0).catch((error) => {
this.appendLog(`shutdown command failed: ${this.formatError(error)}`);
return false;
});
if (shutdownAcked) {
this.appendLog("Shutdown command completed (legacy cleanup fallback).");
} else {
this.appendLog("Cleanup did not complete before disconnect.");
}
}
await communicator?.close().catch(() => {});
} finally {
stopForegroundNotification();
@@ -1159,7 +1219,17 @@ class DashboardController {
}
private startTextSettingEdit(setting: ConfigSettingString): void {
this.activeTextSetting = setting;
this.startTextSettingsEdit([setting], setting.editorTitle);
}
private startTextSettingsEdit(
settings: readonly ConfigSettingString[],
title: string,
onFinish?: () => void,
): void {
this.activeTextSettings = Array.from(settings.slice(0, 2));
this.activeTextEditorTitle = title;
this.activeTextEditorOnFinish = onFinish ?? null;
this.emit();
}
@@ -1213,10 +1283,12 @@ class DashboardController {
}
private endTextSettingEdit(): void {
const finishedSetting = this.activeTextSetting;
this.activeTextSetting = null;
const finishedSettings = this.activeTextSettings;
this.activeTextSettings = [];
this.activeTextEditorTitle = "";
this.activeTextEditorOnFinish = null;
this.emit();
if (finishedSetting === nightscoutSiteUrlSetting || finishedSetting === nightscoutApiTokenSetting) {
if (finishedSettings.includes(nightscoutSiteUrlSetting) || finishedSettings.includes(nightscoutApiTokenSetting)) {
void this.refreshNightscoutAfterSettingsChange();
}
}
@@ -1227,11 +1299,14 @@ class DashboardController {
* out of the edit page.
*/
finishActiveTextSettingEdit(): void {
if (!this.activeTextSetting) return;
if (!this.activeTextSettings.length) return;
const onFinish = this.activeTextEditorOnFinish;
const closesGlassesEditor = this.activeTextSettings.length === 1;
this.endTextSettingEdit();
if (this.textEditorHost?.closeTextEditor()) {
if (closesGlassesEditor && this.textEditorHost?.closeTextEditor()) {
this.textEditorHost.requestRender();
}
onFinish?.();
}
private updateTextSetting(setting: ConfigSettingString, value: string): void {
@@ -1262,6 +1337,11 @@ class DashboardController {
let frameOwned = false;
try {
const inputEvent = rawInputEventToInputEvent(event);
// The gesture, plus which app is on screen and whether input goes to it,
// the sidebar, or a shell overlay. Java only knows the raw event codes,
// and without the target the export says what was pressed but not who
// spent the time drawing the answer.
frameTimings.annotateFrame(frameId, `${inputEvent.type} ${shell.describeInputTarget()}`);
if (this.glassesLocked) {
const ringDoubleTap =
inputEvent.type === "double-click" &&
@@ -1274,7 +1354,9 @@ class DashboardController {
if (ringDoubleTap) shell.sleep();
} else {
shell.wake("sidebar");
const ready = await this.ensureEvenHubSessionActive();
const ready = await frameTimings.spanAsync(frameId, "wake-barrier", () =>
this.ensureEvenHubSessionActive(frameId),
);
if (!ready) {
this.appendLog("EvenHub wake barrier timed out for locked display");
}
@@ -1295,7 +1377,9 @@ class DashboardController {
shellPreWoke = shell.wake("sidebar");
}
if (shellPreWoke || this.evenHubSessionSuspended || this.evenHubResumePromise) {
const ready = await this.ensureEvenHubSessionActive();
const ready = await frameTimings.spanAsync(frameId, "wake-barrier", () =>
this.ensureEvenHubSessionActive(frameId),
);
if (!ready) {
this.appendLog(`EvenHub wake barrier timed out for ${event.kind}`);
}
@@ -1330,14 +1414,17 @@ class DashboardController {
}
if (outcome.shell) {
this.requestShellRender();
// Pass frameId as the cause, not the owner: the chrome render gets its
// own child frame under this one, so the export shows the input and
// every render it triggered on a single timeline.
this.requestShellRender(frameId);
}
if (outcome.window) {
// The window adapter owned frameId (render or explicit finish).
frameOwned = true;
} else if (outcome.shell) {
frameOwned = true;
frameTimings.finishFrame(frameId, "input consumed by shell; chrome render scheduled");
frameTimings.finishFrame(frameId, "handled by shell; chrome render spawned");
}
} finally {
if (!frameOwned) {
@@ -1363,9 +1450,16 @@ class DashboardController {
...this.sharedActions,
requestRender: () => {}, // rebound by createInProcessWindow
},
submitFrame: (image, paintMs, frameId) => this.submitWindowFrame(surfaceId, image, paintMs, frameId),
submitFrame: (planes, paintMs, frameId) => this.submitWindowFrame(surfaceId, planes, paintMs, frameId),
setSurfaceVisible: (visible) => this.setWindowSurfaceVisible(surfaceId, visible),
removeSurface: () => this.removeWindowSurface(surfaceId),
reconfigureSurface: (heightMode) => {
// Resize the surface rect to the new band; a foreground window stays
// visible. The shell re-renders so the chrome (top bar) follows.
const visible = shell.foregroundWindow()?.windowId === windowId;
void this.configureWindowSurface(surfaceId, visible, heightMode);
this.requestShellRender();
},
onClosed: () => {
this.inProcessApps.delete(windowId);
},
@@ -1416,10 +1510,11 @@ class DashboardController {
apps: ALL_APPS,
actions: this.sharedActions,
launchApp: (appId, params) => this.launchApp(appId, params),
uninstallApp: (appId) => this.uninstallApp(appId),
launchInProcessApp: (windowId, surfaceId, create) => this.launchInProcessApp(windowId, surfaceId, create),
ensureWorkerHost: (createWorker) => this.ensureWorkerHost(app.appId, createWorker),
submitWindowFrame: (surfaceId, image, paintMs, frameId) =>
this.submitWindowFrame(surfaceId, image, paintMs, frameId),
submitWindowFrame: (surfaceId, planes, paintMs, frameId) =>
this.submitWindowFrame(surfaceId, planes, paintMs, frameId),
setWindowSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible),
requestShellRender: () => this.requestShellRender(),
appendLog: (message) => this.appendLog(message),
@@ -1456,7 +1551,7 @@ class DashboardController {
this.suppressOpenAppsPersist = true;
try {
for (const appId of saved.open) {
if (!known.has(appId)) continue;
if (!known.has(appId) && !getInstalledEvenHubAppById(appId)) continue;
try {
await this.launchApp(appId);
} catch (error) {
@@ -1482,11 +1577,29 @@ class DashboardController {
*/
private async launchApp(appId: string, params?: AppLaunchParams): Promise<void> {
const app = ALL_APPS.find((entry) => entry.appId === appId);
if (!app) {
this.appendLog(`unknown app: ${appId}`);
if (app) {
await app.launch(this.buildAppContext(app), params);
return;
}
await app.launch(this.buildAppContext(app), params);
const installed = getInstalledEvenHubAppById(appId);
if (installed) {
const host = ALL_APPS.find((entry) => entry.appId === "evenhub")!;
await launchInstalledPackage(this.buildAppContext({ ...host, appId }), installed);
return;
}
this.appendLog(`unknown app: ${appId}`);
}
private async uninstallApp(appId: string): Promise<void> {
const packageId = installedEvenHubPackageId(appId);
if (!packageId) {
this.appendLog(`app is not uninstallable: ${appId}`);
return;
}
closeRunningPackage(packageId);
if (uninstallEvenHubPackage(packageId)) {
this.appendLog(`evenhub: uninstalled ${packageId}`);
}
}
/** Create/refresh a window surface on the compositor, if connected. */
@@ -1514,21 +1627,26 @@ class DashboardController {
}
/** Submit a painted frame for an in-process window (e.g. the launcher). */
private async submitWindowFrame(surfaceId: string, image: GrayImage, paintMs: number, frameId: number): Promise<void> {
private async submitWindowFrame(surfaceId: string, planes: Plane[], paintMs: number, frameId: number): Promise<void> {
const communicator = this.communicator;
if (!communicator || this.phase === "charging") {
frameTimings.finishFrame(frameId, "discarded: window frame with no active connection");
return;
}
const fingerprint = image.fingerprint();
const buffer = image.to8bppBuffer();
await communicator.submitSurfaceFrame(
surfaceId,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
await frameTimings.spanAsync(frameId, "submit", () =>
communicator.submitSurfaceFrame(
surfaceId,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
preparedDraws,
),
);
}
@@ -1546,7 +1664,14 @@ class DashboardController {
* overlays). Coalesces like requestRender: one render in flight, at most
* one queued.
*/
requestShellRender(): void {
requestShellRender(causeFrameId = 0): void {
// Remember the input frame that asked for this chrome repaint so
// renderShell can nest its frame under it; without that link an input
// event and the shell render it caused show up as two unrelated frames
// and the real input-to-pixels latency is never measured.
if (causeFrameId > 0 && this.pendingShellRenderCauseFrameId === 0) {
this.pendingShellRenderCauseFrameId = causeFrameId;
}
if (this.shellRenderInProgress) {
this.shellRenderQueued = true;
return;
@@ -1567,37 +1692,62 @@ class DashboardController {
}
private async renderShell(): Promise<void> {
const frameId = frameTimings.startFrame("render:shell");
const causeFrameId = this.pendingShellRenderCauseFrameId;
this.pendingShellRenderCauseFrameId = 0;
const frameId = frameTimings.startFrame("render:shell", causeFrameId);
frameTimings.annotateFrame(frameId, shell.describeInputTarget());
const wantFreshData = this.nextShellRenderWantsFreshData;
this.nextShellRenderWantsFreshData = false;
if (wantFreshData) frameTimings.logFrame(frameId, "follow-up repaint that must not use cached data");
beginRenderPass(!wantFreshData);
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => shell.paintSurface()),
);
const paintMs = Date.now() - paintStartedAtMs;
const paintUsedStaleData = endRenderPass();
if (paintUsedStaleData) {
// Repaint with fresh data (e.g. notification icons) once this frame is
// out; mirrors renderDashboard's stale-data contract.
// out; mirrors renderDashboard's stale-data contract. Linked to this
// frame so the export shows one input costing two round trips instead of
// an unexplained extra render.
this.nextShellRenderWantsFreshData = true;
this.requestShellRender();
this.requestShellRender(frameId);
}
if (!this.communicator || this.phase === "charging") {
const communicator = this.communicator;
if (!communicator || this.phase === "charging") {
frameTimings.finishFrame(frameId, "discarded: shell render with no active connection");
return;
}
const fingerprint = frameTimings.span(frameId, "fingerprint", () => image.fingerprint());
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
await this.communicator.submitSurfaceFrame(
SHELL_SURFACE_ID,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
// Spanned because the bridge serializes Java calls: a frame can sit here
// behind another surface's submission, which is otherwise an unexplained
// jump between the paint spans and the composite.
await frameTimings.spanAsync(frameId, "submit", () =>
communicator.submitSurfaceFrame(
SHELL_SURFACE_ID,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
preparedDraws,
),
);
await this.communicator.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
// Backpressure: the next shell render waits for this one to reach the
// glasses. Timing out here means the loop was blocked for the full timeout
// and any input arriving meanwhile had its chrome repaint delayed, so say
// so in the frame rather than leaving a silent stall.
const outcome = await communicator.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
if (outcome === null) {
frameTimings.logFrame(
frameId,
`still unsent after ${FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS}ms; shell render loop released`,
);
}
this.updateCompositePreview();
}
+61 -3
View File
@@ -1,17 +1,23 @@
/**
* Builds the Faceclaw custom firmware on-device: downloads the stock Even
* Realities G2 2.2.6.10 image from Even's CDN, verifies its SHA-256, applies
* the committed byte-patch set (cfw-patches.ts), verifies the patched SHA-256,
* and writes the result to app storage.
* the committed byte-patch set (cfw-patches.ts), extracts the stock EvenHub
* fonts for phone-side rendering, verifies the patched SHA-256, and writes the
* result to app storage.
*
* This mirrors g2flash/build_cfw.sh + patches/apply_patches.py exactly (same
* URL, same pinned hashes, same offset/old/new patch semantics), so a
* successful run reproduces the reviewed image byte-for-byte. It does NOT flash
* anything — producing and verifying the image is the whole job here.
*/
import { knownFolders } from "@nativescript/core";
import { File, knownFolders } from "@nativescript/core";
import { CFW_PATCH_SET, FirmwarePatchOp } from "./firmware/cfw-patches";
import {
EVENHUB_RUNTIME_FONT_FILENAME,
extractEvenHubFontAsset,
isEvenHubFontAsset,
} from "./firmware-fonts";
declare const com: any;
@@ -22,6 +28,7 @@ const STOCK_OUTPUT_FILENAME = "g2_2.2.6.10.bin";
export type FirmwareProgress =
| { phase: "downloading" }
| { phase: "verifying-base" }
| { phase: "extracting-fonts" }
| { phase: "patching"; applied: number; total: number }
| { phase: "verifying-output" }
| { phase: "writing" }
@@ -35,6 +42,36 @@ export type BuiltFirmware = {
export class FirmwareBuildError extends Error {}
/** True only when a complete, parseable runtime extraction is present. */
export function hasExtractedEvenHubFonts(): boolean {
const path = `${knownFolders.documents().path}/${EVENHUB_RUNTIME_FONT_FILENAME}`;
if (!File.exists(path)) return false;
try {
return isEvenHubFontAsset(JSON.parse(File.fromPath(path).readTextSync()));
} catch {
return false;
}
}
/**
* Download and verify the pinned stock image, then extract its EvenHub fonts.
* Used to repair an existing custom-firmware install without reflashing it.
*/
export async function downloadAndExtractEvenHubFonts(
onProgress?: (progress: FirmwareProgress) => void,
): Promise<void> {
const report = (progress: FirmwareProgress) => {
try {
onProgress?.(progress);
} catch {
// progress reporting must never break extraction
}
};
const base = await downloadAndVerifyBase(report);
report({ phase: "extracting-fonts" });
persistEvenHubFonts(base);
}
/**
* Download → verify → patch → verify → persist. Rejects with a
* FirmwareBuildError carrying a human-readable message if any hash check or the
@@ -53,6 +90,9 @@ export async function buildCustomFirmware(
const base = await downloadAndVerifyBase(report);
report({ phase: "extracting-fonts" });
persistEvenHubFonts(base);
const patched = applyPatches(new Uint8Array(base), CFW_PATCH_SET.patches, (applied, total) =>
report({ phase: "patching", applied, total }),
);
@@ -127,6 +167,24 @@ async function downloadFirmware(): Promise<ArrayBuffer> {
return response.arrayBuffer();
}
function persistEvenHubFonts(base: ArrayBuffer): void {
try {
const asset = extractEvenHubFontAsset(base);
let writeError: unknown;
knownFolders.documents().getFile(EVENHUB_RUNTIME_FONT_FILENAME).writeTextSync(
JSON.stringify(asset),
(error) => {
writeError = error;
},
);
if (writeError) throw writeError;
} catch (error) {
throw new FirmwareBuildError(
`The firmware was verified, but its EvenHub fonts could not be extracted: ${(error as Error)?.message ?? error}`,
);
}
}
/**
* Replay the committed patch set onto `base`, returning the patched bytes. Each
* op writes `new` at `offset` after checking the bytes there equal `old`; an op
Loaded 100 of 152 files, more files were not shown because too many files have changed in this diff. Show more