diff --git a/ACKNOWLEDGEMENTS.md b/ACKNOWLEDGEMENTS.md index 37567f19..f6e02ef6 100644 --- a/ACKNOWLEDGEMENTS.md +++ b/ACKNOWLEDGEMENTS.md @@ -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. diff --git a/App_Resources/Android/src/main/AndroidManifest.xml b/App_Resources/Android/src/main/AndroidManifest.xml index 4036788c..19659720 100644 --- a/App_Resources/Android/src/main/AndroidManifest.xml +++ b/App_Resources/Android/src/main/AndroidManifest.xml @@ -55,6 +55,17 @@ android:usesCleartextTraffic="true" android:hardwareAccelerated="true"> + + + + + pendingMessages = new ArrayDeque<>(); private final ArrayDeque 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) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java new file mode 100644 index 00000000..6e40bbef --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java @@ -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); + } + }); + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java new file mode 100644 index 00000000..56ff2d61 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java @@ -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); +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java new file mode 100644 index 00000000..5a590902 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java @@ -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); + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java new file mode 100644 index 00000000..4d89a8bf --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java @@ -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 = ""; + // After if present, else before everything. + int headIndex = indexOfIgnoreCase(html, "= 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"; + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java new file mode 100644 index 00000000..d50d00ae --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java @@ -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 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(); + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java index dc57daa8..455d5bff 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java @@ -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 validate(byte[] img) { List 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) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java index 6338fc2d..350691b7 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java @@ -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. */ diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java index a0620785..4dba5ac0 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java @@ -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//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 lines = new ArrayList<>(); - final Map openSpans = new HashMap<>(); + final Frame parent; + final List children = new ArrayList<>(0); + final List lines = new ArrayList<>(); + final Map 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 activeFrames = new HashMap<>(); - private final ArrayDeque recentFrames = new ArrayDeque<>(); - private final List slowestSentFrames = new ArrayList<>(); - private final ArrayDeque 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 framesById = + new LinkedHashMap(64, 0.75f, false) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > FRAMES_RETAINED && !eldest.getValue().isOpen(); + } + }; + private final ArrayDeque recentRoots = new ArrayDeque<>(); + private final List slowestRoots = new ArrayList<>(); + private final ArrayDeque inputLatencies = new ArrayDeque<>(); + private final ArrayDeque 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 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 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 window) { + if (window.isEmpty()) { return null; } - List sorted = new ArrayList<>(sentDurations); + List 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 timedOut = new ArrayList<>(); synchronized (lock) { - Iterator> 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(); + } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java new file mode 100644 index 00000000..c6535c0c --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java @@ -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 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); + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java new file mode 100644 index 00000000..c075c230 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java @@ -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 fontIds = new HashMap<>(); + private static final Map 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)); + } + } + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java new file mode 100644 index 00000000..206bae6b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java @@ -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 ids = new HashMap<>(); + private static final List 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; + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java b/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java new file mode 100644 index 00000000..d728cc6b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java @@ -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 fontCache = + new LinkedHashMap(FONT_CACHE_SIZE, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry 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 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"); + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java index 64249af9..e7aae1ff 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java @@ -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 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 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 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) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java index d8115ac3..ec8eac4a 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java @@ -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 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 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]; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java index a296fae2..91c89e73 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java @@ -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 diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java index b63add63..6656f9a6 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java @@ -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; } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java index 8dfdbeb3..2367178c 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java @@ -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( diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java new file mode 100644 index 00000000..0f0da3ee --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java @@ -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 fontTableOffsets = new HashMap<>(); + private final Map glyphOffsets = new HashMap<>(); + private final Map imageOffsets = new HashMap<>(); + private final List 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 drainUploadPayloads(int maxPayloadBytes) { + List 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; + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java new file mode 100644 index 00000000..4ea07987 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java @@ -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 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 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 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 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 = 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 fwSubs = new ArrayList<>(); + List 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 drawable = selected.isEmpty() + ? Collections.emptyList() + : ensureResident(cache, selected); + bakedCandidates += selected.size() - drawable.size(); + + // Group glyphs into mode-14 runs; leftover-budget glyphs bake. + List glyphDrawable = new ArrayList<>(); + List imageDrawable = new ArrayList<>(); + for (Selected sel : drawable) { + if (sel.glyphAtlas != null) glyphDrawable.add(sel); + else imageDrawable.add(sel); + } + List runs = new ArrayList<>(); + List drawnGlyphs = buildRuns(cache, glyphDrawable, runs); + bakedCandidates += glyphDrawable.size() - drawnGlyphs.size(); + List 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 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 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 rects, + byte[] next, int stride, int width, int height, + List outSubs, List 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 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 ensureResident(TextureCacheState cache, List selected) { + for (int attempt = 0; attempt < 2; attempt++) { + List 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 buildRuns(TextureCacheState cache, List drawable, List outRuns) { + List 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 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 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 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; + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java index f07f21d6..a4746c71 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java @@ -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 diff --git a/App_Resources/Android/src/main/res/xml/file_provider_paths.xml b/App_Resources/Android/src/main/res/xml/file_provider_paths.xml new file mode 100644 index 00000000..f2550b7a --- /dev/null +++ b/App_Resources/Android/src/main/res/xml/file_provider_paths.xml @@ -0,0 +1,6 @@ + + + + diff --git a/PRIVACY b/PRIVACY index e8b4089b..18e29a55 100644 --- a/PRIVACY +++ b/PRIVACY @@ -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. diff --git a/README.md b/README.md index f9ee76e2..96270b26 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/apps/all-apps.ts b/app/apps/all-apps.ts index 716f16a9..aef8d27f 100644 --- a/app/apps/all-apps.ts +++ b/app/apps/all-apps.ts @@ -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, ]; diff --git a/app/apps/app-definition.ts b/app/apps/app-definition.ts index da094ecb..eadf3f06 100644 --- a/app/apps/app-definition.ts +++ b/app/apps/app-definition.ts @@ -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; /** @@ -50,6 +53,8 @@ export type AppContext = { actions: Omit; /** Launch (or focus) any app by id through the shell. */ launchApp: (appId: string, params?: AppLaunchParams) => Promise; + /** Uninstall a dynamically registered app, if the app id supports it. */ + uninstallApp: (appId: string) => Promise; /** Launch-or-focus a main-thread singleton window keyed by windowId. */ launchInProcessApp: ( windowId: string, @@ -58,8 +63,8 @@ export type AppContext = { ) => Promise; /** 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; + /** Submit a window surface's painted frame, as planes (windows created outside launchInProcessApp). */ + submitWindowFrame: (surfaceId: string, planes: Plane[], paintMs: number, frameId: number) => Promise; setWindowSurfaceVisible: (surfaceId: string, visible: boolean) => void; requestShellRender: () => void; appendLog: (message: string) => void; diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index ebdbb8d6..241b4da3 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -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) { diff --git a/app/apps/calendar/calendar.ts b/app/apps/calendar/calendar.ts index 47d76748..2d060849 100644 --- a/app/apps/calendar/calendar.ts +++ b/app/apps/calendar/calendar.ts @@ -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); } } diff --git a/app/apps/compass/compass-app.ts b/app/apps/compass/compass-app.ts index 08f796b4..a6885d7f 100644 --- a/app/apps/compass/compass-app.ts +++ b/app/apps/compass/compass-app.ts @@ -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, diff --git a/app/apps/debug-tests/accelerometer-demo.ts b/app/apps/debug-tests/accelerometer-demo.ts index f80a4955..3d5c205d 100644 --- a/app/apps/debug-tests/accelerometer-demo.ts +++ b/app/apps/debug-tests/accelerometer-demo.ts @@ -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"; diff --git a/app/apps/debug-tests/buzzer-demo.ts b/app/apps/debug-tests/buzzer-demo.ts index 5ba6f560..9f0d9309 100644 --- a/app/apps/debug-tests/buzzer-demo.ts +++ b/app/apps/debug-tests/buzzer-demo.ts @@ -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); diff --git a/app/apps/debug-tests/resource-usage.ts b/app/apps/debug-tests/resource-usage.ts index 5150a8b1..27df5ea1 100644 --- a/app/apps/debug-tests/resource-usage.ts +++ b/app/apps/debug-tests/resource-usage.ts @@ -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"; diff --git a/app/apps/debug-tests/screen-test.ts b/app/apps/debug-tests/screen-test.ts index 37ec6e48..20ecf0f0 100644 --- a/app/apps/debug-tests/screen-test.ts +++ b/app/apps/debug-tests/screen-test.ts @@ -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"; diff --git a/app/apps/evenhub/api-key-dialog.ts b/app/apps/evenhub/api-key-dialog.ts new file mode 100644 index 00000000..c50253a9 --- /dev/null +++ b/app/apps/evenhub/api-key-dialog.ts @@ -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 = { + 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(); + } +} diff --git a/app/apps/evenhub/compass-router.ts b/app/apps/evenhub/compass-router.ts new file mode 100644 index 00000000..854e67a6 --- /dev/null +++ b/app/apps/evenhub/compass-router.ts @@ -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(); + 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(); diff --git a/app/apps/evenhub/compositor.ts b/app/apps/evenhub/compositor.ts new file mode 100644 index 00000000..60d2c706 --- /dev/null +++ b/app/apps/evenhub/compositor.ts @@ -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; +} diff --git a/app/apps/evenhub/containers.ts b/app/apps/evenhub/containers.ts new file mode 100644 index 00000000..95954fa6 --- /dev/null +++ b/app/apps/evenhub/containers.ts @@ -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, 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, 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, key: string): number | undefined { + const value = readNumber(obj, key, Number.NaN); + return Number.isNaN(value) ? undefined : value; +} + +export function readString(obj: Record, 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 { + return value && typeof value === "object" && !Array.isArray(value) ? (value as Record) : {}; +} + +function parseTextContainer(json: Record): 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): 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): 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): 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, + ); +} diff --git a/app/apps/evenhub/credentials.ts b/app/apps/evenhub/credentials.ts new file mode 100644 index 00000000..2273c750 --- /dev/null +++ b/app/apps/evenhub/credentials.ts @@ -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(""); +} diff --git a/app/apps/evenhub/ehpk.ts b/app/apps/evenhub/ehpk.ts new file mode 100644 index 00000000..f082a2be --- /dev/null +++ b/app/apps/evenhub/ehpk.ts @@ -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; +}; + +/** 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; +}; + +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(); + + 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; + 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 ""; +} diff --git a/app/apps/evenhub/even-api.ts b/app/apps/evenhub/even-api.ts new file mode 100644 index 00000000..bc548eeb --- /dev/null +++ b/app/apps/evenhub/even-api.ts @@ -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 | null = null; + + async listApps(page = 1, category = ""): Promise { + const query: Record = { 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> { + 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 { + return parseStoreApp(await this.getAppDetail(packageId)); + } + + /** Resolve the signed download URL, then fetch and sanity-check its EHPK. */ + async downloadApp(packageId: string): Promise { + 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; body?: Record } = {}, + ): Promise { + 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 { + 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 { + 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; + body?: Record; + 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 = { + 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 = { + 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 { + 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 { + let timeoutHandle: ReturnType | null = null; + const timeout = new Promise((_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 { + return value && typeof value === "object" && !Array.isArray(value) ? (value as Record) : {}; +} + +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}`; + } +} diff --git a/app/apps/evenhub/evenhub-window.ts b/app/apps/evenhub/evenhub-window.ts new file mode 100644 index 00000000..3b549980 --- /dev/null +++ b/app/apps/evenhub/evenhub-window.ts @@ -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; +} diff --git a/app/apps/evenhub/imu-router.ts b/app/apps/evenhub/imu-router.ts new file mode 100644 index 00000000..0a417ae0 --- /dev/null +++ b/app/apps/evenhub/imu-router.ts @@ -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(); + 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(); diff --git a/app/apps/evenhub/index.ts b/app/apps/evenhub/index.ts new file mode 100644 index 00000000..c7d6aeca --- /dev/null +++ b/app/apps/evenhub/index.ts @@ -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 { + 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; diff --git a/app/apps/evenhub/installed-apps.ts b/app/apps/evenhub/installed-apps.ts new file mode 100644 index 00000000..f9743cde --- /dev/null +++ b/app/apps/evenhub/installed-apps.ts @@ -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(); + +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(); + for (const value of parsed) { + if (!value || typeof value !== "object") continue; + const item = value as Record; + 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(/]*>/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); +} diff --git a/app/apps/evenhub/manager.ts b/app/apps/evenhub/manager.ts new file mode 100644 index 00000000..38ac7565 --- /dev/null +++ b/app/apps/evenhub/manager.ts @@ -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(); +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 { + 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 { + 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; +} diff --git a/app/apps/evenhub/mic-router.ts b/app/apps/evenhub/mic-router.ts new file mode 100644 index 00000000..fca3e374 --- /dev/null +++ b/app/apps/evenhub/mic-router.ts @@ -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(); + 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(); diff --git a/app/apps/evenhub/permission-dialog.ts b/app/apps/evenhub/permission-dialog.ts new file mode 100644 index 00000000..22be1f25 --- /dev/null +++ b/app/apps/evenhub/permission-dialog.ts @@ -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"]; + } +} diff --git a/app/apps/evenhub/permissions.ts b/app/apps/evenhub/permissions.ts new file mode 100644 index 00000000..31c545f0 --- /dev/null +++ b/app/apps/evenhub/permissions.ts @@ -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(); + 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; + 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)) { + 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(";"); +} diff --git a/app/apps/evenhub/privacy-policy.ts b/app/apps/evenhub/privacy-policy.ts new file mode 100644 index 00000000..e2a0592a --- /dev/null +++ b/app/apps/evenhub/privacy-policy.ts @@ -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 { + let timeoutHandle: ReturnType | null = null; + const timeout = new Promise((_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 ""; + } +} diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts new file mode 100644 index 00000000..a6ba5f8c --- /dev/null +++ b/app/apps/evenhub/session.ts @@ -0,0 +1,1449 @@ +/** + * One running EvenHub app: the host side of the SDK's + * flutter_inappwebview.callHandler('evenAppMessage', ...) contract, the page + * container state, and the glue between the phone WebView and the glasses + * window. Wire shapes were captured empirically from even_hub_sdk 0.0.12 + * (see notes/evenhub_compatibility.txt). + * + * Wire contract summary: + * - Web -> host: callHandler('evenAppMessage', '') where the JSON is + * {type: "call_even_app_method", method, data?}. The handler's return + * value resolves the app-side promise (no response postMessage). + * - Host -> web: call window._listenEvenAppMessage({type: + * "listen_even_app_data", method, data}); for method "evenHubEvent", data + * is {type: "sysEvent"|"textEvent"|"listEvent", jsonData: {...}}. + * - Return values: createStartUpPageContainer -> int 0..3; + * updateImageRawData -> int 0..3 or enum string; rebuild/textUpgrade/ + * shutDown/setLocalStorage -> boolean; getLocalStorage -> string; + * getUserInfo/getGlassesInfo -> plain JSON objects (not strings). + */ +import { ApplicationSettings } from "@nativescript/core"; +import { GrayImage } from "../../graphics/image"; +import { EvenHubFont } from "../../graphics/evenhub-font"; +import { type DashboardInputEvent, type Layer } from "../../ui/layers"; +import { + anthropicApiKeySetting, + elevenLabsApiKeySetting, + mapboxApiKeySetting, + openAiApiKeySetting, + sonioxApiKeySetting, +} from "../../ui/dashboard-settings"; +import { + buildSoundSequencePayload, + phraseDurationMs, + CFW_SEQ_MAX, + type Step, +} from "../../ui/sound-effects"; +import { EvenHubApiKeyDialogLayer } from "./api-key-dialog"; +import { + asRecord, + eventCaptureContainer, + parsePage, + readNumber, + readOptionalNumber, + readString, + type EvenHubImageContainer, + type EvenHubListContainer, + type EvenHubContainer, + type EvenHubPage, +} from "./containers"; +import { compositePage } from "./compositor"; +import { type EvenHubManifest } from "./ehpk"; +import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions"; +import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router"; +import { evenHubImuRouter, type EvenHubImuClient } from "./imu-router"; +import { evenHubCompassRouter, type EvenHubCompassClient } from "./compass-router"; +import { type ImuReading } from "../../native/imu"; +import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry"; +import { getCurrentLocation } from "../../native/location"; +import { LocationTracker, type TrackedLocation } from "../../native/location-tracker"; +import { ensureFineLocationPermission } from "../../g2/android-permissions"; + +const UPNG = require("upng-js"); + +declare const com: any; + +/** OsEventTypeList values (PB ordinals). */ +const CLICK_EVENT = 0; +const SCROLL_TOP_EVENT = 1; +const SCROLL_BOTTOM_EVENT = 2; +const DOUBLE_CLICK_EVENT = 3; +const FOREGROUND_ENTER_EVENT = 4; +const FOREGROUND_EXIT_EVENT = 5; +const SYSTEM_EXIT_EVENT = 7; +const IMU_DATA_REPORT_EVENT = 8; + +/** + * Injected into every served HTML document at document start (before any app + * JS): (1) the promise-returning flutter_inappwebview.callHandler the EvenHub + * SDK expects, backed by the FaceclawEvenHubJsBridge Java object; and (2) a + * host-driven setTimeout/setInterval/requestAnimationFrame replacement that + * survives the screen-off throttle (fired by the native ticker in + * FaceclawEvenHubWebViewHost). Kept in ES5 so it runs on any webview. + */ +export const EVENHUB_BRIDGE_INJECT_SCRIPT = ` +(function () { + if (window.flutter_inappwebview && window.flutter_inappwebview.callHandler) return; + var pending = {}; + var nextId = 1; + window.__fcResolve = function (id, ok, value) { + var entry = pending[id]; + if (!entry) return; + delete pending[id]; + (ok ? entry[0] : entry[1])(value); + }; + window.flutter_inappwebview = { + callHandler: function (name) { + var args = Array.prototype.slice.call(arguments, 1); + return new Promise(function (resolve, reject) { + var id = nextId++; + pending[id] = [resolve, reject]; + window.__faceclawEvenHub.postMessage(String(name), JSON.stringify(args), id); + }); + } + }; +})(); +(function () { + // Host-driven timers. Chromium heavily throttles a page's own + // setTimeout/setInterval when the phone screen is off, which slows + // timer-driven apps to a crawl. Replace them with a queue fired by + // window.__fcTimerTick(), which the native host calls at ~60Hz regardless of + // screen state (host-initiated evaluateJavascript escapes the throttle). + if (window.__fcTimerTick) return; + var timers = new Map(); + var nextId = 1; + window.setTimeout = function (cb, delay) { + var args = Array.prototype.slice.call(arguments, 2); + var id = nextId++; + timers.set(id, { cb: cb, args: args, due: Date.now() + (+delay || 0), interval: 0 }); + return id; + }; + window.setInterval = function (cb, delay) { + var args = Array.prototype.slice.call(arguments, 2); + var id = nextId++; + var d = +delay || 0; + timers.set(id, { cb: cb, args: args, due: Date.now() + d, interval: d }); + return id; + }; + window.clearTimeout = function (id) { timers.delete(id); }; + window.clearInterval = function (id) { timers.delete(id); }; + window.__fcTimerTick = function () { + var now = Date.now(); + var dueIds = []; + timers.forEach(function (entry, id) { if (entry.due <= now) dueIds.push(id); }); + dueIds.sort(function (a, b) { return timers.get(a).due - timers.get(b).due; }); + for (var i = 0; i < dueIds.length; i++) { + var entry = timers.get(dueIds[i]); + if (!entry) continue; // cleared by an earlier callback this tick + if (entry.interval > 0) { + // Advance one period (keeps cadence); if we've fallen behind, skip the + // missed periods rather than fast-forwarding a burst of catch-up fires. + entry.due += entry.interval; + if (entry.due <= now) entry.due = now + entry.interval; + } else { + timers.delete(dueIds[i]); + } + try { entry.cb.apply(null, entry.args); } catch (e) { if (window.console) console.error(e); } + } + }; +})(); +(function () { + // Host-driven requestAnimationFrame, same idea as the timers above: a hidden + // page's rAF is paused/throttled with the screen off, stalling canvas apps + // that render on a rAF loop. Drive it from window.__fcRafTick(), called by the + // native ticker each frame. (The glasses render via BLE, not the phone's + // display, so vsync alignment doesn't matter — a steady frame rate does.) + if (window.__fcRafTick) return; + var rafs = new Map(); + var nextRafId = 1; + window.requestAnimationFrame = function (cb) { + var id = nextRafId++; + rafs.set(id, cb); + return id; + }; + window.cancelAnimationFrame = function (id) { rafs.delete(id); }; + window.__fcRafTick = function () { + if (rafs.size === 0) return; + var ts = window.performance && performance.now ? performance.now() : Date.now(); + // Snapshot and clear: callbacks that re-request during this frame run on + // the NEXT frame, matching rAF semantics (so a loop doesn't spin forever). + var current = rafs; + rafs = new Map(); + current.forEach(function (cb) { + try { cb(ts); } catch (e) { if (window.console) console.error(e); } + }); + }; +})(); +`; + +/** + * Injected at document-start (after the stock bridge shim): defines + * `window.getFaceclawExtensions`, the single entry point for Faceclaw-only + * capabilities (see the faceclaw-extensions package). It never touches the + * standard SDK or any prototype — everything lives behind this one global, so + * apps stay compatible with the stock Even app (where the global is absent). + * + * RPC uses its own id-keyed promise map and `__fcExtResolve`, kept separate + * from the stock `__fcResolve` channel. Events arrive via `__fcExtEvent`. + * `` is interpolated by buildFaceclawExtensionsScript at load. + */ +export function buildFaceclawExtensionsScript(versionString: string): string { + return ` +(function () { + if (window.getFaceclawExtensions) return; + var VERSION = ${JSON.stringify(versionString)}; + var pending = {}; + var nextId = 1; + window.__fcExtResolve = function (id, ok, value) { + var entry = pending[id]; + if (!entry) return; + delete pending[id]; + (ok ? entry[0] : entry[1])(value); + }; + function call(method, params) { + return new Promise(function (resolve, reject) { + var id = nextId++; + pending[id] = [resolve, reject]; + window.__faceclawEvenHub.postMessage("faceclawExt", JSON.stringify([method].concat(params || [])), id); + }); + } + function send(method, params) { + // Fire-and-forget: id 0 tells the host no response is expected. + window.__faceclawEvenHub.postMessage("faceclawExt", JSON.stringify([method].concat(params || [])), 0); + } + var listeners = {}; + window.__fcExtEvent = function (name, data) { + var arr = listeners[name]; + if (!arr) return; + for (var i = 0; i < arr.slice().length; i++) { + try { arr[i](data); } catch (e) { if (window.console) console.error(e); } + } + }; + function on(name, cb) { + (listeners[name] || (listeners[name] = [])).push(cb); + return function () { + var arr = listeners[name]; + if (!arr) return; + var i = arr.indexOf(cb); + if (i >= 0) arr.splice(i, 1); + }; + } + // Compass: enable on first listener, disable when the last one leaves. + var compassListeners = []; + function addCompass(cb) { + compassListeners.push(cb); + if (compassListeners.length === 1) send("setCompass", [true]); + var off = on("compass", cb); + return function () { + off(); + var i = compassListeners.indexOf(cb); + if (i >= 0) compassListeners.splice(i, 1); + if (compassListeners.length === 0) send("setCompass", [false]); + }; + } + // Voice-assistant tools: specs go to the host; handlers stay here and run when + // the host invokes a tool via __fcExtInvokeTool. + var toolHandlers = {}; + window.__fcExtInvokeTool = function (id, name, args) { + var handler = toolHandlers[name]; + function reply(ok, value) { + window.__faceclawEvenHub.postMessage("faceclawExtToolResult", JSON.stringify([id, ok, value]), 0); + } + if (!handler) { reply(false, "unknown tool " + name); return; } + Promise.resolve().then(function () { return handler(args); }).then( + function (res) { reply(true, res == null ? "" : res); }, + function (err) { reply(false, String((err && err.message) || err)); } + ); + }; + function setAssistantTools(tools) { + toolHandlers = {}; + var specs = []; + (tools || []).forEach(function (t) { + if (!t || !t.name || typeof t.handler !== "function") return; + toolHandlers[t.name] = t.handler; + specs.push({ + name: t.name, + description: t.description || "", + parameters: t.parameters || { type: "object", properties: {} }, + availability: t.availability === "open" ? "open" : "foreground" + }); + }); + send("setAssistantTools", [specs]); + } + var extensions = { + getVersion: function () { return VERSION; }, + returnToAppSwitcher: function () { send("returnToAppSwitcher"); }, + quit: function () { send("quit"); }, + addWindowLifecycleListener: function (cb) { return on("windowLifecycle", cb); }, + getConfiguredApiKeys: function () { return call("getConfiguredApiKeys"); }, + requestApiKeyAccess: function (services) { return call("requestApiKeyAccess", [services || []]); }, + playBuzzer: function (steps) { return call("playBuzzer", [steps || []]); }, + addCompassListener: addCompass, + createLayout: function (layout) { return call("createLayout", [layout || {}]); }, + replaceLayout: function (layout) { return call("replaceLayout", [layout || {}]); }, + setAssistantTools: setAssistantTools + }; + window.getFaceclawExtensions = function () { return extensions; }; +})(); +`; +} + +/** Stock blocks a repeated createStartUpPageContainer ~2s before failing it. */ +const DUPLICATE_CREATE_DELAY_MS = 2000; + +export type EvenHubWebViewHandle = { + /** Run JS inside the app's WebView (must be called on the main thread). */ + evaluateJs: (js: string) => void; + /** Detach and destroy the WebView (the app is closing). */ + destroy: () => void; +}; + +export type EvenHubWindowHooks = { + requestRender: () => void; + closeWindow: () => void; + /** Hand focus to the app switcher (used to decline an app's quit request). */ + focusSwitcher: () => void; + /** Push a modal overlay layer onto the app window (e.g. a consent prompt). */ + pushOverlay: (layer: Layer) => void; + /** Switch the window between the stock 576x288 band and the full 576x452 canvas. */ + setTallCanvas: (tall: boolean) => void; + /** The shell window id (unique per running instance; used for tool registration). */ + windowId: string; +}; + +export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenHubCompassClient { + readonly manifest: EvenHubManifest; + readonly distDir: string; + + private page: EvenHubPage | null = null; + private pageCreated = false; + private webViewHandle: EvenHubWebViewHandle | null = null; + private windowHooks: EvenHubWindowHooks | null = null; + private closed = false; + private launchContextPushed = false; + private systemExitSent = false; + /** The one-shot launch FOREGROUND_ENTER has been delivered (page exists). */ + private launchEnterSent = false; + /** Shell focus state; true from launch (apps open focused). */ + private foreground = true; + /** Phone screen state; the mic goes silent while the screen is off. */ + private screenOn = true; + /** The app has enabled its microphone via audioControl(true). */ + private micRequested = false; + /** The app has enabled the accelerometer via imuControl(true). */ + private imuRequested = false; + /** The app has activated the compass (extension addCompassListener). */ + private compassRequested = false; + /** Non-null while the app has an active location subscription. */ + private locationTracker: LocationTracker | null = null; + /** The shell window id, for tool registration (falls back to packageId). */ + private shellWindowId = ""; + /** Whether this app has contributed assistant tools (for cleanup). */ + private hasAppTools = false; + /** Pending host→app tool invocations, keyed by call id. */ + private readonly pendingToolCalls = new Map void>(); + private nextToolCallId = 1; + // Window-lifecycle tracking for the extension API (apps launch visible+focused). + private lastVisible = true; + private lastFocused = true; + /** Shell input focus on this window, sampled during paint. */ + private shellFocused = true; + /** API-key services the user has granted this app this session. */ + private readonly grantedApiKeys = new Set(); + private log: (message: string) => void; + + constructor(manifest: EvenHubManifest, distDir: string, log: (message: string) => void) { + this.manifest = manifest; + this.distDir = distDir; + this.log = log; + } + + // ----- webview wiring ----- + + attachWebView(handle: EvenHubWebViewHandle): void { + this.webViewHandle = handle; + } + + /** The WebView finished loading the app: push the one-shot launch context. */ + webViewLoaded(): void { + if (this.launchContextPushed) return; + this.launchContextPushed = true; + // Launched to drive the glasses (from the on-glasses file browser), so + // apps that branch on this should take their glasses path. + this.pushMessage("evenAppLaunchSource", { launchSource: "glassesMenu" }); + this.pushMessage("deviceStatusChanged", { + sn: "FACECLAW-G2", + connectType: "connected", + isWearing: true, + batteryLevel: 100, + isCharging: false, + isInCase: false, + }); + } + + // ----- glasses window wiring ----- + + attachWindow(hooks: EvenHubWindowHooks): void { + this.windowHooks = hooks; + this.shellWindowId = hooks.windowId; + } + + windowClosed(): void { + this.windowHooks = null; + this.close(); + } + + paint(size: { width: number; height: number }, focused: boolean): GrayImage { + // Focus changes always trigger a repaint of the foreground window, so paint + // is a reliable place to sample shell input-focus for lifecycle events. + this.updateShellFocus(focused); + if (!this.pageCreated || !this.page) { + const image = new GrayImage(size.width, size.height, 0); + const font = EvenHubFont.get(); + font.drawText(image, 16, 16, `Loading ${this.manifest.name}...`, 255); + font.drawText(image, 16, 16 + 2 * font.lineHeight, "(waiting for the app to build its page)", 120); + return image; + } + return compositePage(this.page, size, focused); + } + + /** + * Route a gesture to the app, emulating stock hardware: clicks and + * double-clicks arrive as sysEvent (never textEvent) with zero-valued + * fields elided the way protobuf JSON elides them; list selection moves + * locally with events only for clicks and boundary scrolls. + */ + handleGesture(event: DashboardInputEvent): void { + if (!this.page) return; + const capture = this.page ? eventCaptureContainer(this.page) : undefined; + switch (event.type) { + case "click": + if (capture?.kind === "list") { + this.emitListEvent(capture, CLICK_EVENT); + } else { + this.emitSysEvent(CLICK_EVENT, gestureSource(event.source)); + } + break; + case "double-click": + this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source)); + break; + case "scroll-up": + case "scroll-down": { + const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT; + if (capture?.kind === "list") { + this.scrollList(capture, eventType); + } else { + this.emitSysEvent(eventType, 0); + } + break; + } + default: + break; + } + } + + private scrollList(list: EvenHubListContainer, eventType: number): void { + // Swipe up selects the previous item, swipe down the next (stock direction). + const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1; + const next = list.selectedIndex + delta; + if (next < 0 || next >= list.itemNames.length) { + // At a boundary the selection stays put and the app hears about it. + this.emitListEvent(list, eventType); + return; + } + list.selectedIndex = next; + this.windowHooks?.requestRender(); + } + + /** + * Shell focus changes drive foreground/background. The initial ENTER is + * deferred until the page exists (a just-launched app is focused before it + * has built its page); createStartUpPage emits it then. + */ + setForeground(foreground: boolean): void { + if (this.foreground === foreground) return; + this.foreground = foreground; + // Foreground gates mic + IMU eligibility (only the visible app hears audio / + // reads the accelerometer). + if (this.micRequested) evenHubMicRouter.notifyEligibilityChanged(); + if (this.imuRequested) evenHubImuRouter.notifyEligibilityChanged(); + if (this.compassRequested) evenHubCompassRouter.notifyEligibilityChanged(); + // Foreground gates which app tools are live; re-list on change. + if (this.hasAppTools) toolRegistry.fireToolsChanged(); + // Focus is unknown until the next paint (a re-shown window repaints and + // re-asserts it; a hidden one won't, so treat it as unfocused meanwhile). + this.shellFocused = false; + this.recomputeLifecycle(); + if (!this.pageCreated) return; + this.emitSysEvent(foreground ? FOREGROUND_ENTER_EVENT : FOREGROUND_EXIT_EVENT, 0); + } + + /** Screen turned on/off (forwarded from the shell); mutes the mic when off. */ + setScreenOn(on: boolean): void { + if (this.screenOn === on) return; + this.screenOn = on; + if (this.micRequested) evenHubMicRouter.notifyEligibilityChanged(); + // As in setForeground: let the next paint re-assert focus. + this.shellFocused = false; + this.recomputeLifecycle(); + } + + // ----- extension window lifecycle ----- + + /** Sample shell input-focus (from paint) and emit focused/blurred on change. */ + private updateShellFocus(focused: boolean): void { + if (this.shellFocused === focused) return; + this.shellFocused = focused; + this.recomputeLifecycle(); + } + + /** + * Emit visible/hidden and focused/blurred extension events on transition. + * visible = foreground && screen on; focused additionally needs shell input + * focus on this window. + */ + private recomputeLifecycle(): void { + const visible = this.foreground && this.screenOn; + const focused = visible && this.shellFocused; + if (visible !== this.lastVisible) { + this.lastVisible = visible; + this.pushExtEvent("windowLifecycle", { type: visible ? "visible" : "hidden" }); + } + if (focused !== this.lastFocused) { + this.lastFocused = focused; + this.pushExtEvent("windowLifecycle", { type: focused ? "focused" : "blurred" }); + } + } + + // ----- microphone (EvenHubMicClient) ----- + + get windowId(): string { + return this.shellWindowId || this.manifest.packageId; + } + + isForeground(): boolean { + return this.foreground; + } + + isScreenOn(): boolean { + return this.screenOn; + } + + /** A decoded mic frame from the router: hand it to the app as an audioEvent. */ + deliverAudioPcm(pcm: Uint8Array): void { + const audioPcm = Array.from(pcm); + this.pushMessage("evenHubEvent", { type: "audioEvent", jsonData: { audioPcm } }); + } + + /** + * audioControl(enable, source?): open or close the microphone for this app. + * Gated on a declared microphone permission (confirmed at install/first run). + * Enabling only registers intent — the router decides when audio actually + * flows (foreground + screen on + no assistant modal). + */ + private audioControl(data: Record): boolean { + if (!permissionsIncludeMicrophone(this.manifest.permissions)) { + this.log("evenhub: audioControl denied (app declares no microphone permission)"); + return false; + } + const enable = readEnableFlag(data); + // The 0.0.12 payload shape isn't captured; log it once per toggle so the + // real field names can be confirmed from hardware. + this.log(`evenhub: audioControl enable=${enable} data=${JSON.stringify(data)}`); + if (enable) { + if (!this.micRequested) { + this.micRequested = true; + evenHubMicRouter.requestMic(this); + } + } else if (this.micRequested) { + this.micRequested = false; + evenHubMicRouter.releaseMic(this); + } + return true; + } + + // ----- location ----- + + private declaresLocation(): boolean { + if (permissionsInclude(this.manifest.permissions, "location")) return true; + this.log("evenhub: location denied (app declares no location permission)"); + return false; + } + + /** getAppLocation(): one-shot fix, or null on denial/error (SDK contract). */ + private async getAppLocation(): Promise { + if (!this.declaresLocation()) return null; + if (!(await ensureFineLocationPermission())) return null; + try { + const location = await getCurrentLocation(); + return { + latitude: location.latitude, + longitude: location.longitude, + ...(location.accuracyMeters != null ? { accuracy: location.accuracyMeters } : {}), + timestamp: location.timestampMs, + }; + } catch (error) { + this.log(`evenhub: getAppLocation failed: ${error}`); + return null; + } + } + + /** + * startAppLocationUpdates(options?): begin a location subscription. Fixes are + * pushed to the app as `appLocationChanged`. Continues in the background (the + * subscription ends only on stopAppLocationUpdates or app close), matching the + * Navigate app's foreground-service-backed tracking. + */ + private async startLocationUpdates(data: Record): Promise { + if (!this.declaresLocation()) return false; + if (!(await ensureFineLocationPermission())) return false; + if (this.closed) return false; + if (this.locationTracker) return true; + const intervalMs = readOptionalNumber(data, "intervalMs") ?? 1000; + const tracker = new LocationTracker({ + onLocation: (location) => { + if (this.closed) return; + this.pushMessage("appLocationChanged", toAppLocation(location)); + }, + onError: (message) => this.log(`evenhub: location update error: ${message}`), + }); + this.locationTracker = tracker; + tracker.start(Math.max(200, Math.round(intervalMs))); + return true; + } + + private stopLocationUpdates(): boolean { + this.locationTracker?.stop(); + this.locationTracker = null; + return true; + } + + // ----- accelerometer / IMU (EvenHubImuClient) ----- + + /** A shared IMU reading from the router: hand it to the app as a sysEvent. */ + deliverImu(reading: ImuReading): void { + this.pushEvenHubEvent("sysEvent", { + eventType: IMU_DATA_REPORT_EVENT, + imuData: { x: reading.x, y: reading.y, z: reading.z }, + }); + } + + /** + * imuControl(isOpen, reportFrq): enable/disable the accelerometer report + * stream. There is no EvenHub IMU permission (nor an Android one), so no + * consent gate. Enabling registers intent; the router only actually streams + * to the foreground app. reportFrq is one of 100..1000 (step 100). + */ + private imuControl(data: Record): boolean { + const enable = readEnableFlag(data); + const freq = clampImuFreq(readOptionalNumber(data, "reportFrq") ?? readOptionalNumber(data, "reportFreq") ?? 100); + // Payload field spelling isn't fully captured; log once per toggle. + this.log(`evenhub: imuControl enable=${enable} freq=${freq} data=${JSON.stringify(data)}`); + if (enable) { + this.imuRequested = true; + evenHubImuRouter.requestImu(this, freq); + } else if (this.imuRequested) { + this.imuRequested = false; + evenHubImuRouter.releaseImu(this); + } + return true; + } + + // ----- bridge calls (web -> host) ----- + + /** + * Handle one callHandler('evenAppMessage', ...) invocation. argsJson is the + * JSON array of handler arguments; args[0] is the SDK's message JSON string + * (kept lenient: a bare object works too). + */ + handleBridgeCall(handlerName: string, argsJson: string, callId: number): void { + if (handlerName === "faceclawExt") { + this.handleExtensionCall(argsJson, callId); + return; + } + if (handlerName === "faceclawExtToolResult") { + this.handleToolResult(argsJson); + return; + } + void (async () => { + let result: unknown = null; + try { + if (handlerName !== "evenAppMessage") throw new Error(`unknown handler ${handlerName}`); + const args = JSON.parse(argsJson) as unknown[]; + const first = args[0]; + const message = asRecord(typeof first === "string" ? JSON.parse(first) : first); + const method = readString(message, "method", ""); + const data = asRecord(message.data); + result = await this.dispatch(method, data); + } catch (error) { + this.log(`evenhub bridge call failed: ${error}`); + this.resolveCall(callId, false, String(error)); + return; + } + this.resolveCall(callId, true, result); + })(); + } + + private async dispatch(method: string, data: Record): Promise { + switch (method) { + case "createStartUpPageContainer": + return this.createStartUpPage(data); + case "rebuildPageContainer": + return this.rebuildPage(data); + case "textContainerUpgrade": + return this.textUpgrade(data); + case "updateImageRawData": + return this.updateImage(data); + case "shutDownPageContainer": + return this.shutDown(readNumber(data, "exitMode", 0)); + case "setLocalStorage": + ApplicationSettings.setString(this.storageKey(readString(data, "key", "")), readString(data, "value", "")); + return true; + case "getLocalStorage": + return ApplicationSettings.getString(this.storageKey(readString(data, "key", "")), ""); + case "getUserInfo": + return { uid: 0, name: "Faceclaw", avatar: "", country: "" }; + case "getGlassesInfo": + return { + model: "g2", + sn: "FACECLAW-G2", + status: { + sn: "FACECLAW-G2", + connectType: "connected", + isWearing: true, + batteryLevel: 100, + isCharging: false, + isInCase: false, + }, + }; + case "audioControl": + return this.audioControl(data); + case "getAppLocation": + return this.getAppLocation(); + case "startAppLocationUpdates": + return this.startLocationUpdates(data); + case "stopAppLocationUpdates": + return this.stopLocationUpdates(); + case "imuControl": + return this.imuControl(data); + // Unsupported surface (album/camera pickers): fail politely. + case "pickImageFromAlbum": + case "captureImageFromCamera": + return null; + default: + this.log(`evenhub: unhandled method ${method}`); + return null; + } + } + + /** EhStartUpPageCreateResult: 0 success, 1 invalid, 2 oversize, 3 outOfMemory. */ + private async createStartUpPage(data: Record): Promise { + if (this.pageCreated) { + // One-shot per session on stock: a second call blocks ~2s, then fails. + await delay(DUPLICATE_CREATE_DELAY_MS); + return 1; + } + const page = parsePage(data); + this.warnOnInvalidPage(page); + this.page = page; + this.pageCreated = true; + // The launch focus arrived before the page existed; deliver the ENTER now. + if (this.foreground && !this.launchEnterSent) { + this.launchEnterSent = true; + this.emitSysEvent(FOREGROUND_ENTER_EVENT, 0); + } + this.windowHooks?.requestRender(); + return 0; + } + + private rebuildPage(data: Record): boolean { + // Also serves as create for apps that fall back to rebuild-on-error. + const page = parsePage(data); + this.warnOnInvalidPage(page); + this.page = page; + this.pageCreated = true; + this.windowHooks?.requestRender(); + return true; + } + + private warnOnInvalidPage(page: EvenHubPage): void { + // Stock rejects these outright; we accept-and-log until rejection is + // proven necessary for compatibility. + const captures = page.containers.filter((c) => c.kind !== "image" && c.isEventCapture).length; + if (captures !== 1) this.log(`evenhub: page has ${captures} event-capture containers (stock requires exactly 1)`); + const texts = page.containers.filter((c) => c.kind === "text").length; + const images = page.containers.filter((c) => c.kind === "image").length; + if (texts > 8 || images > 4) this.log(`evenhub: page exceeds stock limits (${texts} text, ${images} image)`); + } + + private textUpgrade(data: Record): boolean { + const id = readNumber(data, "containerID", -1); + // containerID is the identity; containerName is optional in the wild (e.g. + // level-even-g2 omits it). Match by id, enforcing the name only when the app + // actually supplies a non-empty one. (updateImage already matches by id.) + const name = readString(data, "containerName", ""); + const container = this.page?.containers.find( + (c) => c.kind === "text" && c.id === id && (name === "" || c.name === name), + ); + if (!container || container.kind !== "text") return false; + const content = readString(data, "content", ""); + const offset = readOptionalNumber(data, "contentOffset"); + const length = readOptionalNumber(data, "contentLength"); + if (offset !== undefined && length !== undefined) { + container.content = container.content.slice(0, offset) + content + container.content.slice(offset + length); + } else { + container.content = content; + } + this.windowHooks?.requestRender(); + return true; + } + + /** BleEhSendImageResult: 0 success, 1 imageException, 2 sizeInvalid, 3 sendFailed. */ + private updateImage(data: Record): number { + const id = readNumber(data, "containerID", -1); + const container = this.page?.containers.find((c) => c.kind === "image" && c.id === id); + if (!container || container.kind !== "image") return 3; + const bytes = imagePayloadBytes(data.imageData ?? data.rawData ?? asRecord(data).mapRawData); + if (!bytes || bytes.length === 0) return 1; + const decoded = decodeImageBytes(bytes, container); + if (!decoded) return 1; + container.pixels = decoded.pixels; + container.pixelsWidth = decoded.width; + container.pixelsHeight = decoded.height; + this.windowHooks?.requestRender(); + return 0; + } + + private shutDown(exitMode: number): boolean { + if (exitMode === 1) { + // The app asked to quit (stock would pop an on-glasses confirm). We + // decline and hand focus to the app switcher instead, leaving the app + // running — so an app's double-tap-to-quit becomes double-tap-to-defocus. + // No FOREGROUND_EXIT: focusing the sidebar doesn't background the app + // (native Faceclaw apps behave the same), and refocusing the same window + // wouldn't re-fire ENTER, which would leave a pause-on-exit app stuck. + this.windowHooks?.focusSwitcher(); + return true; + } + // Immediate exit. Resolve first (the return travels via evaluateJs, and + // close() only tears the webview down after its own delay). + setTimeout(() => this.close(), 200); + return true; + } + + private storageKey(key: string): string { + return `evenhub:${this.manifest.packageId}:ls:${key}`; + } + + // ----- pushes (host -> web) ----- + + private resolveCall(callId: number, ok: boolean, value: unknown): void { + this.webViewHandle?.evaluateJs( + `window.__fcResolve && window.__fcResolve(${callId}, ${ok ? "true" : "false"}, ${JSON.stringify(value ?? null)})`, + ); + } + + private pushMessage(method: string, data: unknown): void { + const message = { type: "listen_even_app_data", method, data }; + this.webViewHandle?.evaluateJs( + `window._listenEvenAppMessage && window._listenEvenAppMessage(${JSON.stringify(message)})`, + ); + } + + private resolveExtCall(callId: number, ok: boolean, value: unknown): void { + this.webViewHandle?.evaluateJs( + `window.__fcExtResolve && window.__fcExtResolve(${callId}, ${ok ? "true" : "false"}, ${JSON.stringify(value ?? null)})`, + ); + } + + /** Deliver an extension event to the app (e.g. windowLifecycle). */ + private pushExtEvent(name: string, data: unknown): void { + this.webViewHandle?.evaluateJs( + `window.__fcExtEvent && window.__fcExtEvent(${JSON.stringify(name)}, ${JSON.stringify(data)})`, + ); + } + + private pushEvenHubEvent(payloadType: "sysEvent" | "textEvent" | "listEvent", jsonData: Record): void { + this.pushMessage("evenHubEvent", { type: payloadType, jsonData: elideZeroFields(jsonData) }); + } + + private emitSysEvent(eventType: number, eventSource: number): void { + this.pushEvenHubEvent("sysEvent", { eventType, eventSource }); + } + + private emitListEvent(list: EvenHubListContainer, eventType: number): void { + this.pushEvenHubEvent("listEvent", { + containerID: list.id, + containerName: list.name, + currentSelectItemIndex: list.selectedIndex, + currentSelectItemName: list.itemNames[list.selectedIndex] ?? "", + eventType, + }); + } + + // ----- Faceclaw extensions (faceclawExt channel) ----- + + private handleExtensionCall(argsJson: string, callId: number): void { + void (async () => { + let ok = true; + let result: unknown = null; + try { + const args = JSON.parse(argsJson) as unknown[]; + const method = String(args[0]); + result = await this.dispatchExtension(method, args.slice(1)); + } catch (error) { + ok = false; + result = String(error); + this.log(`evenhub: extension call failed: ${error}`); + } + // id 0 is a fire-and-forget send; nothing is awaiting it. + if (callId !== 0) this.resolveExtCall(callId, ok, result); + })(); + } + + private async dispatchExtension(method: string, params: unknown[]): Promise { + switch (method) { + case "returnToAppSwitcher": + this.windowHooks?.focusSwitcher(); + return null; + case "quit": + // A real quit — bypasses the shutDown(1) → return-to-switcher behavior. + setTimeout(() => this.close(), 0); + return null; + case "getConfiguredApiKeys": + return configuredApiKeyServices(); + case "requestApiKeyAccess": + return this.requestApiKeyAccess(Array.isArray(params[0]) ? params[0].map(String) : []); + case "playBuzzer": + await this.playBuzzer(Array.isArray(params[0]) ? params[0] : []); + return null; + case "setCompass": + this.setCompass(params[0] === true); + return null; + case "createLayout": + case "replaceLayout": + return this.applyExtensionLayout(asRecord(params[0])); + case "setAssistantTools": + this.setAssistantTools(Array.isArray(params[0]) ? params[0] : []); + return null; + default: + this.log(`evenhub: unknown extension method ${method}`); + return null; + } + } + + // ----- compass (EvenHubCompassClient) ----- + + deliverCompass(headingDegrees: number): void { + this.pushExtEvent("compass", { headingDegrees }); + } + + private setCompass(enable: boolean): void { + if (enable) { + if (!this.compassRequested) { + this.compassRequested = true; + evenHubCompassRouter.requestCompass(this); + } + } else if (this.compassRequested) { + this.compassRequested = false; + evenHubCompassRouter.releaseCompass(this); + } + } + + // ----- extended layout ----- + + /** + * Build/replace the page from an extended layout: full 576x452 canvas, no + * container-count limit, and per-container `preserve` (inherit content from a + * same-named container in the outgoing layout). Returns true on success. + */ + private applyExtensionLayout(data: Record): boolean { + const previous = this.page; + const page = parsePage(data); + for (const container of page.containers) { + if (container.preserve) preserveContent(container, previous); + } + this.page = page; + this.pageCreated = true; + // The extended layout uses the taller app area. + this.windowHooks?.setTallCanvas(true); + if (this.foreground && !this.launchEnterSent) { + this.launchEnterSent = true; + this.emitSysEvent(FOREGROUND_ENTER_EVENT, 0); + } + this.windowHooks?.requestRender(); + return true; + } + + // ----- assistant tools ----- + + private setAssistantTools(rawSpecs: unknown[]): void { + const specs: ToolSpec[] = []; + for (const raw of rawSpecs) { + const record = asRecord(raw); + const name = readString(record, "name", ""); + if (!name) continue; + const availability = record.availability === "open" ? "open" : "foreground"; + specs.push({ + name, + description: readString(record, "description", ""), + inputSchema: asRecord(record.parameters), + availability, + }); + } + if (specs.length === 0 && !this.hasAppTools) return; + toolRegistry.setAppTools({ + windowId: this.windowId, + // Prefix tools with the app's package name, per the extension contract. + appId: this.manifest.packageId, + specs, + invoke: (toolName, args) => this.invokeAppTool(toolName, args), + isForeground: () => this.foreground, + }); + this.hasAppTools = specs.length > 0; + } + + /** Invoke one of the app's tools by dispatching into the webview and awaiting its reply. */ + private invokeAppTool(toolName: string, args: unknown): Promise { + return new Promise((resolve) => { + if (!this.webViewHandle || this.closed) { + resolve({ ok: false, error: "app not available" }); + return; + } + const id = this.nextToolCallId++; + this.pendingToolCalls.set(id, resolve); + this.webViewHandle.evaluateJs( + `window.__fcExtInvokeTool && window.__fcExtInvokeTool(${id}, ${JSON.stringify(toolName)}, ${JSON.stringify(args ?? {})})`, + ); + }); + } + + private handleToolResult(argsJson: string): void { + try { + const args = JSON.parse(argsJson) as unknown[]; + const id = Number(args[0]); + const ok = args[1] === true; + const value = args[2]; + const resolve = this.pendingToolCalls.get(id); + if (!resolve) return; + this.pendingToolCalls.delete(id); + if (ok) { + resolve({ ok: true, content: typeof value === "string" ? value : JSON.stringify(value ?? null) }); + } else { + resolve({ ok: false, error: typeof value === "string" ? value : "tool failed" }); + } + } catch (error) { + this.log(`evenhub: bad tool result: ${error}`); + } + } + + /** + * Prompt the user (on the glasses) to share configured API keys with the app, + * resolving with the granted values. Services already granted this session + * skip the prompt; declining resolves with an empty map. + */ + private requestApiKeyAccess(services: string[]): Promise> { + const requested = services.filter(isKnownApiKeyService); + const buildGrant = (): Record => { + const out: Record = {}; + for (const id of requested) { + if (!this.grantedApiKeys.has(id)) continue; + const value = apiKeyValue(id); + if (value) out[id] = value; + } + return out; + }; + if (requested.length === 0 || requested.every((id) => this.grantedApiKeys.has(id))) { + return Promise.resolve(buildGrant()); + } + if (!this.windowHooks) return Promise.resolve({}); + return new Promise((resolve) => { + const dialog = new EvenHubApiKeyDialogLayer( + this.manifest.name, + requested.map((id) => ({ id, configured: apiKeyValue(id).length > 0 })), + () => { + for (const id of requested) this.grantedApiKeys.add(id); + resolve(buildGrant()); + }, + () => resolve({}), + ); + this.windowHooks?.pushOverlay(dialog); + this.windowHooks?.requestRender(); + }); + } + + /** Play a piezo tone sequence, chunking/pacing past the firmware's 48-step cap. */ + private async playBuzzer(rawSteps: unknown[]): Promise { + const steps = normalizeBuzzerSteps(rawSteps); + if (steps.length === 0) return; + const communicator = activeCommunicator(); + if (!communicator) return; + for (let i = 0; i < steps.length; i += CFW_SEQ_MAX) { + const chunk = steps.slice(i, i + CFW_SEQ_MAX); + const payload = buildSoundSequencePayload(chunk); + try { + communicator.playBuzzerSequence(payload.buffer); + } catch (error) { + this.log(`evenhub: playBuzzer failed: ${error}`); + return; + } + if (i + CFW_SEQ_MAX < steps.length) await delay(phraseDurationMs(chunk)); + } + } + + // ----- teardown ----- + + /** Close everything: glasses window, WebView, session. Idempotent. */ + close(): void { + if (this.closed) return; + this.closed = true; + if (this.micRequested) { + this.micRequested = false; + evenHubMicRouter.releaseMic(this); + } + if (this.imuRequested) { + this.imuRequested = false; + evenHubImuRouter.releaseImu(this); + } + if (this.compassRequested) { + this.compassRequested = false; + evenHubCompassRouter.releaseCompass(this); + } + if (this.hasAppTools) { + this.hasAppTools = false; + toolRegistry.removeAppTools(this.windowId); + } + for (const resolve of this.pendingToolCalls.values()) resolve({ ok: false, error: "app closed" }); + this.pendingToolCalls.clear(); + this.locationTracker?.stop(); + this.locationTracker = null; + if (!this.systemExitSent) { + // Best effort: lets the app's teardown handlers run before the webview dies. + this.emitSysEvent(SYSTEM_EXIT_EVENT, 0); + this.systemExitSent = true; + } + const hooks = this.windowHooks; + this.windowHooks = null; + hooks?.closeWindow(); + const webView = this.webViewHandle; + this.webViewHandle = null; + if (webView) { + // Give in-flight resolves/pushes a beat to reach the webview first. + setTimeout(() => webView.destroy(), 100); + } + } + + isClosed(): boolean { + return this.closed; + } +} + +function delay(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +/** The SDK's AppLocation shape (even_hub_sdk 0.0.12+), all fields camelCase. */ +type AppLocation = { + latitude: number; + longitude: number; + accuracy?: number; + altitude?: number; + speed?: number; + heading?: number; + timestamp?: number; +}; + +/** Map a continuous GPS fix to the SDK's AppLocation (omitting absent fields). */ +function toAppLocation(location: TrackedLocation): AppLocation { + const out: AppLocation = { latitude: location.latitude, longitude: location.longitude }; + if (location.accuracyMeters != null) out.accuracy = location.accuracyMeters; + if (location.speedMps != null) out.speed = location.speedMps; + if (location.bearingDeg != null) out.heading = location.bearingDeg; + out.timestamp = location.timestampMs; + return out; +} + +/** + * Read the enable flag from an audioControl / imuControl payload. The exact + * wire field name for SDK 0.0.12 isn't captured, so accept the plausible + * spellings (the mock bridge reads `isOpen`; IMU's PB field is `iMUReportEn`); + * default to enabling (the common intent) when the payload is opaque, and log + * it so the real shape can be pinned from hardware. + */ +function readEnableFlag(data: Record): boolean { + for (const key of ["enable", "open", "isOpen", "on", "status", "value", "start", "iMUReportEn", "IMU_ReportEn"]) { + const value = data[key]; + if (typeof value === "boolean") return value; + if (typeof value === "number") return value !== 0; + if (typeof value === "string") return value === "true" || value === "1" || value === "on" || value === "open"; + } + return true; +} + +/** Snap a requested IMU report rate to the SDK's 100..1000 (step 100) grid. */ +function clampImuFreq(freq: number): number { + const snapped = Math.round(freq / 100) * 100; + return Math.min(1000, Math.max(100, snapped)); +} + +function activeCommunicator(): any { + if (!global.isAndroid) return null; + try { + return com.faceclaw.app.FaceclawBleCommunicator.getActive(); + } catch { + return null; + } +} + +/** + * Copy content into a `preserve` container from the same-named, same-kind + * container in the previous page (if any), so a replaceLayout keeps its pixels / + * text / list state instead of blanking. + */ +function preserveContent(target: EvenHubContainer, previous: EvenHubPage | null): void { + const source = previous?.containers.find((c) => c.kind === target.kind && c.name === target.name); + if (!source) return; + if (target.kind === "text" && source.kind === "text") { + target.content = source.content; + } else if (target.kind === "image" && source.kind === "image") { + target.pixels = source.pixels; + target.pixelsWidth = source.pixelsWidth; + target.pixelsHeight = source.pixelsHeight; + } else if (target.kind === "list" && source.kind === "list") { + target.itemNames = source.itemNames; + target.selectedIndex = Math.min(source.selectedIndex, Math.max(0, target.itemNames.length - 1)); + } +} + +/** The API-key services the extension API can surface, and their settings. */ +const API_KEY_SERVICES = [ + { id: "openai", setting: openAiApiKeySetting }, + { id: "anthropic", setting: anthropicApiKeySetting }, + { id: "soniox", setting: sonioxApiKeySetting }, + { id: "elevenlabs", setting: elevenLabsApiKeySetting }, + { id: "mapbox", setting: mapboxApiKeySetting }, +] as const; + +function isKnownApiKeyService(id: string): boolean { + return API_KEY_SERVICES.some((service) => service.id === id); +} + +function apiKeyValue(id: string): string { + return API_KEY_SERVICES.find((service) => service.id === id)?.setting.get().trim() ?? ""; +} + +/** Service ids the user has actually configured (non-empty key), names only. */ +function configuredApiKeyServices(): string[] { + return API_KEY_SERVICES.filter((service) => service.setting.get().trim()).map((service) => service.id); +} + +/** Coerce an app-supplied buzzer step list into validated {freq, ms, duty?}. */ +function normalizeBuzzerSteps(raw: unknown[]): Step[] { + const out: Step[] = []; + for (const item of raw) { + if (!item || typeof item !== "object") continue; + const record = item as Record; + const freq = Number(record.freq); + const ms = Number(record.ms); + if (!Number.isFinite(freq) || !Number.isFinite(ms)) continue; + const duty = record.duty === undefined ? undefined : Number(record.duty); + out.push({ freq, ms, duty: duty !== undefined && Number.isFinite(duty) ? duty : undefined }); + } + return out; +} + +/** + * Decode an uncompressed Windows BMP ('BM', BI_RGB) to 8bpp grayscale. Handles + * 1/4/8-bit paletted and 24/32-bit BGR(A), top-down or bottom-up. Returns null + * for compressed or unsupported variants. Apps like EvenChess send 1-bit BMPs + * (black/white palette) for full-board refreshes. + */ +function decodeBmp(bytes: Uint8Array): { pixels: Uint8Array; width: number; height: number } | null { + if (bytes.length < 54 || bytes[0] !== 0x42 || bytes[1] !== 0x4d) return null; + const u16 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8); + const u32 = (o: number) => (bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24)) >>> 0; + const i32 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24); + + const pixelOffset = u32(10); + const dibSize = u32(14); + const width = i32(18); + const heightRaw = i32(22); + const bitCount = u16(28); + const compression = u32(30); + if (compression !== 0) return null; // only BI_RGB (uncompressed) + if (width <= 0 || width > 4096) return null; + const topDown = heightRaw < 0; + const height = Math.abs(heightRaw); + if (height === 0 || height > 4096) return null; + + // Palette (grayscale per index) for <=8bpp; each entry is 4 bytes BGRA. + let palette: Uint8Array | null = null; + if (bitCount <= 8) { + let numColors = u32(46); + if (numColors === 0 || numColors > (1 << bitCount)) numColors = 1 << bitCount; + const palOffset = 14 + dibSize; + palette = new Uint8Array(numColors); + for (let i = 0; i < numColors; i++) { + const o = palOffset + i * 4; + if (o + 2 >= bytes.length) break; + palette[i] = luminance(bytes[o + 2]!, bytes[o + 1]!, bytes[o]!); + } + } + + const stride = (((width * bitCount + 31) >> 5) << 2); // rows are 4-byte aligned + const pixels = new Uint8Array(width * height); + for (let y = 0; y < height; y++) { + const srcY = topDown ? y : height - 1 - y; + const rowStart = pixelOffset + srcY * stride; + if (rowStart >= bytes.length) break; + const dstRow = y * width; + for (let x = 0; x < width; x++) { + let gray: number; + if (bitCount === 1) { + const bit = ((bytes[rowStart + (x >> 3)] ?? 0) >> (7 - (x & 7))) & 1; + gray = palette?.[bit] ?? (bit ? 255 : 0); + } else if (bitCount === 4) { + const byte = bytes[rowStart + (x >> 1)] ?? 0; + const nib = (x & 1) === 0 ? byte >> 4 : byte & 0x0f; + gray = palette?.[nib] ?? nib * 17; + } else if (bitCount === 8) { + const idx = bytes[rowStart + x] ?? 0; + gray = palette?.[idx] ?? idx; + } else if (bitCount === 24) { + const o = rowStart + x * 3; + gray = luminance(bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0); + } else if (bitCount === 32) { + const o = rowStart + x * 4; + gray = luminance(bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0); + } else { + return null; + } + pixels[dstRow + x] = gray; + } + } + return { pixels, width, height }; +} + +function luminance(r: number, g: number, b: number): number { + return Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b); +} + +/** ring/left-arm/right-arm -> PB EventSourceType. */ +function gestureSource(source: "ring" | "left-arm" | "right-arm"): number { + switch (source) { + case "right-arm": + return 1; + case "ring": + return 2; + case "left-arm": + return 3; + } +} + +/** + * Protobuf JSON elides zero-valued fields, and apps depend on it (they test + * eventType === 0 || eventType === undefined). Emit the same shape. + */ +function elideZeroFields(obj: Record): Record { + const out: Record = {}; + for (const [key, value] of Object.entries(obj)) { + if (value === undefined || value === 0 || value === "") continue; + out[key] = value; + } + return out; +} + +/** Accept the payload forms seen on the wire: number[], JSON-ified typed + * array ({"0": 1, ...}), or base64 string. */ +function imagePayloadBytes(raw: unknown): Uint8Array | null { + if (Array.isArray(raw)) { + const out = new Uint8Array(raw.length); + for (let i = 0; i < raw.length; i++) out[i] = Number(raw[i]) & 0xff; + return out; + } + if (typeof raw === "string") return base64Decode(raw); + if (raw && typeof raw === "object") { + const record = raw as Record; + const keys = Object.keys(record); + const out = new Uint8Array(keys.length); + for (let i = 0; i < keys.length; i++) { + const value = record[String(i)]; + if (typeof value !== "number") return null; + out[i] = value & 0xff; + } + return out; + } + return null; +} + +const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +function base64Decode(text: string): Uint8Array | null { + const clean = text.replace(/[\s=]+$/g, "").replace(/\s+/g, ""); + const out = new Uint8Array(Math.floor((clean.length * 3) / 4)); + let bits = 0; + let bitCount = 0; + let outIndex = 0; + for (const char of clean) { + const value = BASE64_ALPHABET.indexOf(char); + if (value < 0) return null; + bits = (bits << 6) | value; + bitCount += 6; + if (bitCount >= 8) { + bitCount -= 8; + out[outIndex++] = (bits >> bitCount) & 0xff; + } + } + return out.subarray(0, outIndex); +} + +/** + * Decode an image payload: PNG or BMP (both are sent by real apps — EvenChess + * force-refreshes as 1-bit BMP but streams live moves as PNG, so a host that + * only did PNG dropped the initial board), or raw grayscale sized to the + * container (8bpp, or 4bpp packed two pixels per byte, high nibble first). + */ +function decodeImageBytes( + bytes: Uint8Array, + container: EvenHubImageContainer, +): { pixels: Uint8Array; width: number; height: number } | null { + if (bytes.length > 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) { + const bmp = decodeBmp(bytes); + if (bmp) return bmp; + } + if (bytes.length > 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) { + try { + const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); + const decoded = UPNG.decode(buffer); + const frames = UPNG.toRGBA8(decoded) as ArrayBuffer[]; + if (!frames.length) return null; + const rgba = new Uint8Array(frames[0]!); + const pixels = new Uint8Array(decoded.width * decoded.height); + for (let i = 0; i < pixels.length; i++) { + const offset = i * 4; + const alpha = (rgba[offset + 3] ?? 0) / 255; + pixels[i] = Math.round( + (0.2126 * (rgba[offset] ?? 0) + 0.7152 * (rgba[offset + 1] ?? 0) + 0.0722 * (rgba[offset + 2] ?? 0)) * alpha, + ); + } + return { pixels, width: decoded.width, height: decoded.height }; + } catch (error) { + console.warn(`evenhub: PNG decode failed: ${error}`); + return null; + } + } + const area = container.width * container.height; + if (bytes.length === area) { + return { pixels: Uint8Array.from(bytes), width: container.width, height: container.height }; + } + if (bytes.length === Math.ceil(area / 2)) { + const pixels = new Uint8Array(area); + for (let i = 0; i < area; i++) { + const byte = bytes[i >> 1]!; + const nibble = i % 2 === 0 ? byte >> 4 : byte & 0x0f; + pixels[i] = nibble * 17; + } + return { pixels, width: container.width, height: container.height }; + } + return null; +} diff --git a/app/apps/evenhub/store-detail-layer.ts b/app/apps/evenhub/store-detail-layer.ts new file mode 100644 index 00000000..7e70e7e2 --- /dev/null +++ b/app/apps/evenhub/store-detail-layer.ts @@ -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; + 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 | 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 { + 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 { + 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 { + 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 { + 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(); +} diff --git a/app/apps/evenhub/store-layer.ts b/app/apps/evenhub/store-layer.ts new file mode 100644 index 00000000..c7fc188d --- /dev/null +++ b/app/apps/evenhub/store-layer.ts @@ -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; + 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 { + 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): void { + if (!this.credentialEditorOpen) return; + this.credentialEditorOpen = false; + void actions.endTextSettingEdit(); + } + + private async reload(ctx: LayerContext): Promise { + 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 { + 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 { + 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); +} diff --git a/app/apps/evenhub/webview.ts b/app/apps/evenhub/webview.ts new file mode 100644 index 00000000..5429e872 --- /dev/null +++ b/app/apps/evenhub/webview.ts @@ -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(), + }; +} diff --git a/app/apps/files/file-browser.ts b/app/apps/files/file-browser.ts index 88ca2f17..41b9b6c8 100644 --- a/app/apps/files/file-browser.ts +++ b/app/apps/files/file-browser.ts @@ -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 { diff --git a/app/apps/files/file-info-dialog.ts b/app/apps/files/file-info-dialog.ts index 834341af..7a7ef1f4 100644 --- a/app/apps/files/file-info-dialog.ts +++ b/app/apps/files/file-info-dialog.ts @@ -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; } diff --git a/app/apps/files/files-app.ts b/app/apps/files/files-app.ts index 5a6dd9b2..c3c3ae23 100644 --- a/app/apps/files/files-app.ts +++ b/app/apps/files/files-app.ts @@ -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; /** 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 []; diff --git a/app/apps/files/font-previewer.ts b/app/apps/files/font-previewer.ts index 08b902e4..8c2b9b4b 100644 --- a/app/apps/files/font-previewer.ts +++ b/app/apps/files/font-previewer.ts @@ -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 { diff --git a/app/apps/files/image-viewer.ts b/app/apps/files/image-viewer.ts index 6232e91b..4d13fb96 100644 --- a/app/apps/files/image-viewer.ts +++ b/app/apps/files/image-viewer.ts @@ -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"; diff --git a/app/apps/files/index.ts b/app/apps/files/index.ts index eb1f9a6e..0b744e48 100644 --- a/app/apps/files/index.ts +++ b/app/apps/files/index.ts @@ -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), }), ), diff --git a/app/apps/files/text-viewer.ts b/app/apps/files/text-viewer.ts index ce8328cc..5d0ac4c3 100644 --- a/app/apps/files/text-viewer.ts +++ b/app/apps/files/text-viewer.ts @@ -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; } } - diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index ddc587d2..a97e93c1 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -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) { diff --git a/app/apps/launcher/index.ts b/app/apps/launcher/index.ts index 3b102109..1d3786cc 100644 --- a/app/apps/launcher/index.ts +++ b/app/apps/launcher/index.ts @@ -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), }), ); diff --git a/app/apps/launcher/launcher-app.ts b/app/apps/launcher/launcher-app.ts index b7f7a873..225a28f2 100644 --- a/app/apps/launcher/launcher-app.ts +++ b/app/apps/launcher/launcher-app.ts @@ -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; - /** Submit a painted viewport-sized frame to this window's surface. */ - submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise; + uninstallApp: (appId: string) => Promise | void; + /** Submit a painted viewport-sized frame (as planes) to this window's surface. */ + submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise; /** 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("|"); +} diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index 639a4bfc..f23bb23e 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -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) { diff --git a/app/apps/music/media-browse.ts b/app/apps/music/media-browse.ts index 930e345f..22e9e041 100644 --- a/app/apps/music/media-browse.ts +++ b/app/apps/music/media-browse.ts @@ -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) { diff --git a/app/apps/music/music-app.ts b/app/apps/music/music-app.ts index 4fe5b962..7a73b57f 100644 --- a/app/apps/music/music-app.ts +++ b/app/apps/music/music-app.ts @@ -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); diff --git a/app/apps/navigate/navigate-app.worker.ts b/app/apps/navigate/navigate-app.worker.ts index 890869de..fd021ce6 100644 --- a/app/apps/navigate/navigate-app.worker.ts +++ b/app/apps/navigate/navigate-app.worker.ts @@ -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) { diff --git a/app/apps/nightscout/nightscout-app.ts b/app/apps/nightscout/nightscout-app.ts index 8677f8e2..8a35174e 100644 --- a/app/apps/nightscout/nightscout-app.ts +++ b/app/apps/nightscout/nightscout-app.ts @@ -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"; diff --git a/app/apps/nightscout/nightscout.ts b/app/apps/nightscout/nightscout.ts index 7bd1fc2e..32a23d6f 100644 --- a/app/apps/nightscout/nightscout.ts +++ b/app/apps/nightscout/nightscout.ts @@ -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, diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index 7ad62c0b..a4bef682 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -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) { diff --git a/app/apps/roam/roam-app.worker.ts b/app/apps/roam/roam-app.worker.ts index 547eddc4..aa2e8196 100644 --- a/app/apps/roam/roam-app.worker.ts +++ b/app/apps/roam/roam-app.worker.ts @@ -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 { // --------------------------------------------------------------------------- // 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) { diff --git a/app/apps/settings/settings-app.ts b/app/apps/settings/settings-app.ts index 092e0887..48a57e00 100644 --- a/app/apps/settings/settings-app.ts +++ b/app/apps/settings/settings-app.ts @@ -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; + submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise; setSurfaceVisible: (visible: boolean) => void; removeSurface: () => void; onClosed: () => void; diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index 3156c61d..8b5f2fd4 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -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) { diff --git a/app/apps/timer/timer-app.worker.ts b/app/apps/timer/timer-app.worker.ts index 650983a3..ccf6e8b9 100644 --- a/app/apps/timer/timer-app.worker.ts +++ b/app/apps/timer/timer-app.worker.ts @@ -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) { diff --git a/app/apps/transcribe/transcribe.ts b/app/apps/transcribe/transcribe.ts index 9a4d479f..74893146 100644 --- a/app/apps/transcribe/transcribe.ts +++ b/app/apps/transcribe/transcribe.ts @@ -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 = ""; diff --git a/app/apps/weather/weather.ts b/app/apps/weather/weather.ts index 50e98a0f..7a51410a 100644 --- a/app/apps/weather/weather.ts +++ b/app/apps/weather/weather.ts @@ -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, diff --git a/app/assistant/mcp-server.ts b/app/assistant/mcp-server.ts index 4057cf27..d63b9831 100644 --- a/app/assistant/mcp-server.ts +++ b/app/assistant/mcp-server.ts @@ -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": diff --git a/app/assistant/window-tools.ts b/app/assistant/window-tools.ts index 792a3c48..44350606 100644 --- a/app/assistant/window-tools.ts +++ b/app/assistant/window-tools.ts @@ -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; @@ -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}".`); }, ); diff --git a/app/fonts/source-han-sans/LICENSE.txt b/app/fonts/source-han-sans/LICENSE.txt new file mode 100644 index 00000000..3ff0ccab --- /dev/null +++ b/app/fonts/source-han-sans/LICENSE.txt @@ -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. diff --git a/app/fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin b/app/fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin new file mode 100644 index 00000000..ada45e43 Binary files /dev/null and b/app/fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin differ diff --git a/app/fonts/ttf/Inter_18pt-Bold.ttf b/app/fonts/ttf/Inter_18pt-Bold.ttf new file mode 100644 index 00000000..57704d10 Binary files /dev/null and b/app/fonts/ttf/Inter_18pt-Bold.ttf differ diff --git a/app/fonts/ttf/Inter_18pt-Light.ttf b/app/fonts/ttf/Inter_18pt-Light.ttf new file mode 100644 index 00000000..acae3612 Binary files /dev/null and b/app/fonts/ttf/Inter_18pt-Light.ttf differ diff --git a/app/fonts/ttf/Inter_18pt-Regular.ttf b/app/fonts/ttf/Inter_18pt-Regular.ttf new file mode 100644 index 00000000..ce097c82 Binary files /dev/null and b/app/fonts/ttf/Inter_18pt-Regular.ttf differ diff --git a/app/fonts/ttf/Montserrat-Bold.ttf b/app/fonts/ttf/Montserrat-Bold.ttf new file mode 100644 index 00000000..40335875 Binary files /dev/null and b/app/fonts/ttf/Montserrat-Bold.ttf differ diff --git a/app/fonts/ttf/Montserrat-Light.ttf b/app/fonts/ttf/Montserrat-Light.ttf new file mode 100644 index 00000000..881f12d0 Binary files /dev/null and b/app/fonts/ttf/Montserrat-Light.ttf differ diff --git a/app/fonts/ttf/Montserrat-Regular.ttf b/app/fonts/ttf/Montserrat-Regular.ttf new file mode 100644 index 00000000..895e220a Binary files /dev/null and b/app/fonts/ttf/Montserrat-Regular.ttf differ diff --git a/app/fonts/ttf/OFL-Inter.txt b/app/fonts/ttf/OFL-Inter.txt new file mode 100644 index 00000000..d05ec4b3 --- /dev/null +++ b/app/fonts/ttf/OFL-Inter.txt @@ -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. diff --git a/app/fonts/ttf/OFL-Montserrat.txt b/app/fonts/ttf/OFL-Montserrat.txt new file mode 100644 index 00000000..8e618c81 --- /dev/null +++ b/app/fonts/ttf/OFL-Montserrat.txt @@ -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. diff --git a/app/fonts/ttf/OFL-Roboto.txt b/app/fonts/ttf/OFL-Roboto.txt new file mode 100644 index 00000000..a417551a --- /dev/null +++ b/app/fonts/ttf/OFL-Roboto.txt @@ -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. diff --git a/app/fonts/ttf/OFL-RobotoMono.txt b/app/fonts/ttf/OFL-RobotoMono.txt new file mode 100644 index 00000000..e1651437 --- /dev/null +++ b/app/fonts/ttf/OFL-RobotoMono.txt @@ -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. diff --git a/app/fonts/ttf/Roboto-Bold.ttf b/app/fonts/ttf/Roboto-Bold.ttf new file mode 100644 index 00000000..65161856 Binary files /dev/null and b/app/fonts/ttf/Roboto-Bold.ttf differ diff --git a/app/fonts/ttf/Roboto-Light.ttf b/app/fonts/ttf/Roboto-Light.ttf new file mode 100644 index 00000000..ee7268fd Binary files /dev/null and b/app/fonts/ttf/Roboto-Light.ttf differ diff --git a/app/fonts/ttf/Roboto-Regular.ttf b/app/fonts/ttf/Roboto-Regular.ttf new file mode 100644 index 00000000..3db0d1fb Binary files /dev/null and b/app/fonts/ttf/Roboto-Regular.ttf differ diff --git a/app/fonts/ttf/RobotoMono-Bold.ttf b/app/fonts/ttf/RobotoMono-Bold.ttf new file mode 100644 index 00000000..bef439f3 Binary files /dev/null and b/app/fonts/ttf/RobotoMono-Bold.ttf differ diff --git a/app/fonts/ttf/RobotoMono-Light.ttf b/app/fonts/ttf/RobotoMono-Light.ttf new file mode 100644 index 00000000..b6fb475d Binary files /dev/null and b/app/fonts/ttf/RobotoMono-Light.ttf differ diff --git a/app/fonts/ttf/RobotoMono-Regular.ttf b/app/fonts/ttf/RobotoMono-Regular.ttf new file mode 100644 index 00000000..3806bfb1 Binary files /dev/null and b/app/fonts/ttf/RobotoMono-Regular.ttf differ diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index f0b2e26e..cf83c648 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -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(); @@ -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 { + /** + * 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 { 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 { 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 { + 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 { + private async submitWindowFrame(surfaceId: string, planes: Plane[], paintMs: number, frameId: number): Promise { 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 { - 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(); } diff --git a/app/g2/firmware-builder.ts b/app/g2/firmware-builder.ts index 495f1818..e7178142 100644 --- a/app/g2/firmware-builder.ts +++ b/app/g2/firmware-builder.ts @@ -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 { + 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 { 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 diff --git a/app/g2/firmware-fonts.ts b/app/g2/firmware-fonts.ts new file mode 100644 index 00000000..5da89d1d --- /dev/null +++ b/app/g2/firmware-fonts.ts @@ -0,0 +1,304 @@ +/** + * Extract the G2's proprietary 20 px EvenHub fallback fonts from a verified + * stock EVENOTA image. The resulting compact JSON contains only glyph boxes + * and bitmap bytes; text advances and kerning continue to come from pretext. + * + * These fonts are deliberately generated at install time rather than bundled + * with Faceclaw because their redistribution terms are unclear. + */ + +export const EVENHUB_RUNTIME_FONT_FILENAME = "evenhub-firmware-20.json"; + +const MAIN_APP_NAME = "ota/s200_firmware_ota.bin"; +const MAIN_APP_LOAD_ADDRESS = 0x00438000; +const MAIN_APP_PREAMBLE_SIZE = 0x20; +const COMPONENT_HEADER_SIZE = 128; + +type FontSpec = { + descriptor: number; + lineHeight: number; + baseLine: number; +}; + +// Priority order is the stock EvenHub fallback chain (before the CJK font). +const FONT_SPECS: FontSpec[] = [ + { descriptor: 0x0077f414, lineHeight: 27, baseLine: 5 }, + { descriptor: 0x0077f428, lineHeight: 26, baseLine: 5 }, + { descriptor: 0x0077f374, lineHeight: 20, baseLine: 2 }, +]; + +type ExtractedGlyph = { + boxW: number; + boxH: number; + ofsX: number; + ofsY: number; + bytes: Uint8Array; +}; + +export type EvenHubFontAsset = { + lineHeight: number; + baseline: number; + glyphs: Record; + bitmapBase64: string; +}; + +/** Reject missing, truncated, or otherwise unusable persisted font assets. */ +export function isEvenHubFontAsset(value: unknown): value is EvenHubFontAsset { + const asset = value as Partial | null; + return !!asset && Number.isFinite(asset.lineHeight) && Number.isFinite(asset.baseline) && + !!asset.glyphs && typeof asset.glyphs === "object" && Object.keys(asset.glyphs).length >= 700 && + typeof asset.bitmapBase64 === "string" && asset.bitmapBase64.length > 0; +} + +/** Extract and serialize the fonts used for EvenHub background text. */ +export function extractEvenHubFontAsset(firmware: ArrayBuffer): EvenHubFontAsset { + const image = new FirmwareImage(firmware); + const mainApp = image.findComponent(MAIN_APP_NAME); + if (mainApp.payloadSize <= MAIN_APP_PREAMBLE_SIZE) { + throw new Error("the main firmware component is missing its application payload"); + } + + const app = new AddressedImage( + image, + mainApp.payloadOffset + MAIN_APP_PREAMBLE_SIZE, + mainApp.payloadSize - MAIN_APP_PREAMBLE_SIZE, + MAIN_APP_LOAD_ADDRESS, + ); + const merged = new Map(); + for (const spec of FONT_SPECS) extractFont(app, spec, merged); + + if (merged.size < 700) { + throw new Error(`firmware font extraction produced only ${merged.size} glyphs`); + } + + const records: EvenHubFontAsset["glyphs"] = {}; + const chunks: Uint8Array[] = []; + let bitmapSize = 0; + for (const [codePoint, glyph] of [...merged].sort((a, b) => a[0] - b[0])) { + records[String(codePoint)] = [ + glyph.boxW, + glyph.boxH, + glyph.ofsX, + glyph.ofsY, + bitmapSize, + glyph.bytes.length, + ]; + chunks.push(glyph.bytes); + bitmapSize += glyph.bytes.length; + } + + const bitmap = new Uint8Array(bitmapSize); + let offset = 0; + for (const chunk of chunks) { + bitmap.set(chunk, offset); + offset += chunk.length; + } + + const layout = FONT_SPECS[0]!; + return { + lineHeight: layout.lineHeight, + baseline: layout.lineHeight - layout.baseLine, + glyphs: records, + bitmapBase64: base64Encode(bitmap), + }; +} + +function extractFont(app: AddressedImage, spec: FontSpec, merged: Map): void { + const bitmapAddress = app.u32(spec.descriptor); + const glyphDescriptors = app.u32(spec.descriptor + 4); + const cmapAddress = app.u32(spec.descriptor + 8); + const packed = app.u16(spec.descriptor + 18); + const cmapCount = packed & 0x1ff; + const bitsPerPixel = (packed >> 9) & 0xf; + const bitmapFormat = (packed >> 14) & 0x3; + if (bitsPerPixel !== 4 || bitmapFormat !== 3 || cmapCount === 0) { + throw new Error( + `unsupported firmware font at 0x${spec.descriptor.toString(16)} ` + + `(bpp=${bitsPerPixel}, format=${bitmapFormat}, cmaps=${cmapCount})`, + ); + } + + const mapping = new Map(); + for (let index = 0; index < cmapCount; index++) { + resolveCmap(app, cmapAddress + index * 20, mapping); + } + + for (const [codePoint, glyphId] of mapping) { + if (glyphId <= 0 || merged.has(codePoint)) continue; + const descriptor = glyphDescriptors + glyphId * 16; + const bitmapIndex = app.u32(descriptor); + const boxW = app.u16(descriptor + 8); + const boxH = app.u16(descriptor + 10); + const ofsX = app.i16(descriptor + 12); + const ofsY = app.i16(descriptor + 14); + const length = boxW > 0 && boxH > 0 ? rowStride(boxW) * boxH : 0; + merged.set(codePoint, { + boxW, + boxH, + ofsX, + ofsY, + bytes: app.bytes(bitmapAddress + bitmapIndex, length), + }); + } +} + +function resolveCmap(app: AddressedImage, address: number, mapping: Map): void { + const rangeStart = app.u32(address); + const rangeLength = app.u16(address + 4); + const glyphIdStart = app.u16(address + 6); + const unicodeList = app.u32(address + 8); + const glyphIdOffsets = app.u32(address + 12); + const listLength = app.u16(address + 16); + const type = app.u8(address + 18); + + if (type === 2) { + for (let relative = 0; relative < rangeLength; relative++) { + mapping.set(rangeStart + relative, glyphIdStart + relative); + } + return; + } + + if (type === 0) { + for (let relative = 0; relative < rangeLength; relative++) { + const offset = app.u8(glyphIdOffsets + relative); + if (relative !== 0 && offset === 0) continue; + mapping.set(rangeStart + relative, glyphIdStart + offset); + } + return; + } + + if (type === 3 || type === 1) { + for (let index = 0; index < listLength; index++) { + const relative = app.u16(unicodeList + index * 2); + let offset = index; + if (glyphIdOffsets) { + offset = type === 1 ? app.u16(glyphIdOffsets + index * 2) : app.u8(glyphIdOffsets + index); + if (index !== 0 && offset === 0) continue; + } + mapping.set(rangeStart + relative, glyphIdStart + offset); + } + return; + } + + throw new Error(`unknown LVGL font cmap type ${type} at 0x${address.toString(16)}`); +} + +function rowStride(boxWidth: number): number { + // Even's LVGL pipeline aligns every row to a whole byte and adds a padding + // byte for even widths. + return Math.floor(boxWidth / 2) + 1; +} + +class FirmwareImage { + private readonly view: DataView; + private readonly data: Uint8Array; + + constructor(buffer: ArrayBuffer) { + this.view = new DataView(buffer); + this.data = new Uint8Array(buffer); + if (this.data.length < 0x40 || ascii(this.data, 0, 8) !== "EVENOTA\0") { + throw new Error("downloaded image is not an EVENOTA firmware container"); + } + } + + findComponent(name: string): { payloadOffset: number; payloadSize: number } { + const count = this.u32(8); + if (count <= 0 || count > 64) throw new Error(`invalid firmware component count ${count}`); + for (let index = 0; index < count; index++) { + const toc = 0x40 + index * 16; + const componentOffset = this.u32(toc + 4); + this.check(componentOffset, COMPONENT_HEADER_SIZE); + const payloadSize = this.u32(componentOffset + 8); + const filename = nulTerminatedAscii(this.data, componentOffset + 48, 80); + const payloadOffset = componentOffset + COMPONENT_HEADER_SIZE; + this.check(payloadOffset, payloadSize); + if (filename === name) return { payloadOffset, payloadSize }; + } + throw new Error(`firmware component ${name} was not found`); + } + + u32(offset: number): number { + this.check(offset, 4); + return this.view.getUint32(offset, true); + } + + bytes(offset: number, length: number): Uint8Array { + this.check(offset, length); + return this.data.slice(offset, offset + length); + } + + check(offset: number, length: number): void { + if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(length) || offset < 0 || length < 0 || offset + length > this.data.length) { + throw new Error(`firmware range 0x${offset.toString(16)} + ${length} is out of bounds`); + } + } +} + +class AddressedImage { + constructor( + private readonly firmware: FirmwareImage, + private readonly fileOffset: number, + private readonly length: number, + private readonly loadAddress: number, + ) {} + + u8(address: number): number { + return this.bytes(address, 1)[0]!; + } + + u16(address: number): number { + const bytes = this.bytes(address, 2); + return bytes[0]! | (bytes[1]! << 8); + } + + i16(address: number): number { + const value = this.u16(address); + return value & 0x8000 ? value - 0x10000 : value; + } + + u32(address: number): number { + const bytes = this.bytes(address, 4); + return (bytes[0]! | (bytes[1]! << 8) | (bytes[2]! << 16) | (bytes[3]! << 24)) >>> 0; + } + + bytes(address: number, length: number): Uint8Array { + const relative = address - this.loadAddress; + if (relative < 0 || relative + length > this.length) { + throw new Error(`firmware address 0x${address.toString(16)} + ${length} is outside the main application`); + } + return this.firmware.bytes(this.fileOffset + relative, length); + } +} + +function ascii(bytes: Uint8Array, offset: number, length: number): string { + let value = ""; + for (let i = 0; i < length; i++) value += String.fromCharCode(bytes[offset + i] ?? 0); + return value; +} + +function nulTerminatedAscii(bytes: Uint8Array, offset: number, maxLength: number): string { + let value = ""; + for (let i = 0; i < maxLength; i++) { + const byte = bytes[offset + i] ?? 0; + if (byte === 0) break; + value += String.fromCharCode(byte); + } + return value; +} + +const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +function base64Encode(bytes: Uint8Array): string { + let result = ""; + for (let i = 0; i < bytes.length; i += 3) { + const a = bytes[i] ?? 0; + const b = bytes[i + 1] ?? 0; + const c = bytes[i + 2] ?? 0; + const packed = (a << 16) | (b << 8) | c; + result += BASE64_ALPHABET[(packed >> 18) & 63]; + result += BASE64_ALPHABET[(packed >> 12) & 63]; + result += i + 1 < bytes.length ? BASE64_ALPHABET[(packed >> 6) & 63] : "="; + result += i + 2 < bytes.length ? BASE64_ALPHABET[packed & 63] : "="; + } + return result; +} diff --git a/app/g2/firmware/cfw-patches.ts b/app/g2/firmware/cfw-patches.ts index c51da471..33c52b9e 100644 --- a/app/g2/firmware/cfw-patches.ts +++ b/app/g2/firmware/cfw-patches.ts @@ -19,8 +19,14 @@ export type FirmwarePatchSet = { export const CFW_PATCH_SET: FirmwarePatchSet = { "base": "g2_2.2.6.10.bin", "baseSha256": "f4dfb0b49ad3de3c2daf17f8a27a157c3dc98411d6a0d3ab2cfd0918f41b9afa", - "outputSha256": "20cba9377ea207c8c0a6fd936f32db9ecaf23da023cdf43a770b22b355107d1f", + "outputSha256": "82726bbe4d1d82f06a3d345ba3e5a4a39161077e5d26dcd7af24c4677df87fd1", "patches": [ + { + "offset": 1089903, + "old": "5ff43432", + "new": "5ff43332", + "desc": "reserve final 1 KiB of primary TLSF arena for CFW context anchor" + }, { "offset": 1449549, "old": "bdf82c10", @@ -42,133 +48,133 @@ export const CFW_PATCH_SET: FirmwarePatchSet = { { "offset": 1447919, "old": "7ef7fefd", - "new": "baf20cfb", + "new": "bcf24cfa", "desc": "bl snapshot_side @ 0x4db968" }, { "offset": 1448931, "old": "7ef704fc", - "new": "baf212f9", + "new": "bcf252f8", "desc": "bl snapshot_side @ 0x4dbd5c" }, { "offset": 1165461, "old": "45f0cefd", - "new": "fff2bdfa", + "new": "01f3fdf9", "desc": "bl image_deferred (deferred consumer -> FIFO restore + worker, both lenses)" }, { "offset": 1186287, "old": "d9f7d4ff", - "new": "fbf296f9", + "new": "fdf258f8", "desc": "bl settings_send_wrapper (append caps field 100)" }, { "offset": 1183983, "old": "f4f75aff", - "new": "fbf252fb", + "new": "fdf21afa", "desc": "bl settings_decode_wrapper (Faceclaw lease field 101)" }, { "offset": 926945, "old": "08f068fa", - "new": "3af375f8", + "new": "3bf33dff", "desc": "bl faceclaw_display_start @ 0x45c65a (fail-open double-tap takeover)" }, { "offset": 927137, "old": "08f008fa", - "new": "3af315f8", + "new": "3bf3ddfe", "desc": "bl faceclaw_display_start @ 0x45c71a (fail-open double-tap takeover)" }, { "offset": 822553, "old": "28f003f8", - "new": "54f363f8", - "desc": "bl evenhub_longpress (replaces force-quit dialog)" + "new": "55f34dff", + "desc": "bl evenhub_longpress (Faceclaw lease gates custom event vs stock dialog)" }, { "offset": 823369, "old": "1cf09bfb", - "new": "53f3e7fe", + "new": "55f3dbfd", "desc": "bl ring_release (forward ring release-long-press)" }, { "offset": 1474137, "old": "7fb50600", - "new": "b4f24fbf", + "new": "b6f211be", "desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline" }, { "offset": 1022741, "old": "f8f7c1fe", - "new": "20f3ebfb", + "new": "20f30ffc", "desc": "bl display_copy_hook @ 0x473c8e (640x480 direct framebuffer)" }, { "offset": 1022959, "old": "f8f754fe", - "new": "20f37efb", + "new": "20f3a2fb", "desc": "bl display_copy_hook @ 0x473d68 (640x480 direct framebuffer)" }, { "offset": 1198787, "old": "dff770fb", - "new": "f7f278fc", + "new": "f9f28cfb", "desc": "bl faceclaw_send_wear_event @ 0x49ec3c (outside onboarding)" }, { "offset": 1198881, "old": "dff741fb", - "new": "f7f249fc", + "new": "f9f25dfb", "desc": "bl faceclaw_send_wear_event @ 0x49ec9a (outside onboarding)" }, { "offset": 823567, "old": "1cf038fb", - "new": "51f3aef8", + "new": "51f3d2f8", "desc": "bl compass_event_forward (global IMU heading -> stock nav BLE notifier)" }, { "offset": 4301227, "old": "", - "new": "02fb01f044f6d341c0f24701084700bf084644f61751c0f24701084770b500284ed001684ef66362ccf2ff02914218bf70bd90f8081190f8095142f6c52c8d42c0f2500c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b149f29942c0f244022146bde87040104770bd00bf70b54ff6fd03c0f2450314460d46984743f6fc71c2f200010e6831466ff39601b1f1005f0ad131684ef66362ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04af26951c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd47f25d02c0f2580208469047284670bd00bf2de9f04f99b043f6fc76c2f20006d6f8009048466ff39600b0f1005f40f04d81d9f800004ef66367ccf2ff07b84240f0448199f82802002800f02d81d9f82442002c00f0288140f2b870c2f20000d0f8008004a801f006f9b8f1000f00f0fb8140f2030c45f24075cff6807ca4f1600008f160026ff0bf03c0f20205016ed4182166416ec0336166816eab42a166c16ee166016f2167416f6167816fa167c16fe167d0f88010c4f88010d0f88410c4f88410d0f88810c4f88810d0f88c10c4f88c10d0f89010c4f89010d0f89410c4f89410d0f89810c4f89810d0f89c10c4f89c10d0f8a010c4f8a010d0f8a410c4f8a410d0f8a810c4f8a810d0f8ac10c4f8ac10d0f8b010c4f8b010d0f8b410c4f8b410d0f8b810c4f8b810d0f8bc10c4f8bc1050f8c01fc4f8c0104168c4f8c4108168c4f8c810c168c4f8cc100169c4f8d0104169c4f8d4108169c4f8d810c169c4f8dc10016ac4f8e010416ac4f8e410816ac4f8e810c16ac4f8ec10016bc4f8f010416bc4f8f410816bc4f8f810c16bc4f8fc10016cc4f80011416cc4f80411816cc4f80811c16cc4f80c11016dc4f81011416dc4f81411816dc4f81811c16dc4f81c117ff462afd6f800e00eea0c00b0f1005f40f08180def80000b8427cd19ef8ce00002840f050819ef8dc00002100288df8141000f0b08105aa505c002800f0838153185878002800f059819878002800f05781d878002800f055811879002800f053815879002800f051819879002800f04f81d879002800f04d81187a002800f04b81587a002800f04981987a002800f04781d87a002800f04581187b002800f04381587b002800f04181987b002800f03f81d87b002800f03d81187c002800f03b81587c002800f03981987c002800f03781d87c002800f0358114315029abd155e199f82a0270b1d9f82c0240b144f63421c2f207010968401a002800f3fb80002089f82a024cf61520c0f2460019b0bde8f04f004744f6d341c0f247014ff40c708847002800f0cc804ff6d051cff6ff7100224318463183f8302283f8312283f8322283f8332283f8342283f8352283f8362283f8372283f8382283f8392283f83a2283f83b2283f83c2283f83d2283f83e2283f83f2283f8402283f8412283f8422283f8432283f8442283f8452283f8462283f8472283f8482283f8492283f84a2283f84b2283f84c2283f84d2283f84e2283f84f2283f8502283f8512283f8522283f8532283f8542283f8552283f8562283f8572283f8582283f8592283f85a2283f85b2283f85c2283f85d2283f85e2283f85f2283f8602283f8612283f8622283f8632283f8642283f8652283f8662283f8672283f8682283f8692283f86a2283f86b2283f86c2283f86d2283f86e2283f86f2283f8702283f8712283f8722283f8732283f8742283f875227ff470af012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e0002089f82802c9f8240289f82a02012089f829024cf61520c0f24600804714e000203060002089f82802c9f824024ff4163045f20f11069005a8c0f24701cdf814808847012089f82a0204a801f017f8c9f8d40019b0bde8f08f013122e0023120e003311ee004311ce005311ae0063118e0073116e0083114e0093112e00a3110e00b310ee00c310ce00d310ae00e3108e00f3106e0103104e0113102e0123100e013314f2921d84ff0520050541dd00a4445204e29507018d04f204d29907014d052204c29d07010d044204b2910710cd045204a29507108d052204929907104d020204829d07140f08d8600208df8630001219ef8dd00002870d0002205ab985c00284fd09f18787838b3b87838b3f87838b3387938b3787938b3b87938b3f87938b3387a38b3787a38b3b87a38b3f87a38b3387b38b3787b38b3b87b38b3f87b38b3387c38b3787c38b3b87c38b3f87c38b31432502ad2d13ce0013222e0023220e003321ee004321ce005321ae0063218e0073216e0083214e0093212e00a3210e00b320ee00c320ce00d320ae00e3208e00f3206e0103204e0113202e0123200e013324f2a15d84ff05300985411d013444b204e2a58700cd049204d2a987008d050204c2ad87004d020204b2a187140f0608100208df8630001319ef8de0000286cd0002105aa505c00284fd05318587838b3987838b3d87838b3187938b3587938b3987938b3d87938b3187a38b3587a38b3987a38b3d87a38b3187b38b3587b38b3987b38b3d87b38b3187c38b3587c38b3987c38b3d87c38b314315029d2d138e0013122e0023120e003311ee004311ce005311ae0063118e0073116e0083114e0093112e00a3110e00b310ee00c310ce00d310ae00e3108e00f3106e0103104e0113102e0123100e013314f2911d84ff0440050540dd00a4455204e29507008d050204d29907004d020204c29d07040f0ec8000208df8630001219ef8df0000284dd0002105aa505c002800f0bc8053185878002800f092809878002800f09080d878002800f08e801879002800f08c805879002800f08a809879002800f08880d879002800f08680187a002800f08480587a002800f08280987a002800f08080d87a00287ed0187b00287dd0587b00287cd0987b00287bd0d87b00287ad0187c002879d0587c002878d0987c002877d0d87c002876d014315029b4d1d0e0002940f0d180002105aa505c002800f0b88053185878002800f08e809878002800f08c80d878002800f08a801879002800f088805879002800f086809879002800f08480d879002800f08280187a002800f08080587a00287ed0987a00287dd0d87a00287cd0187b00287bd0587b00287ad0987b002879d0d87b002878d0187c002877d0587c002876d0987c002875d0d87c002874d014315029b6d181e0013122e0023120e003311ee004311ce005311ae0063118e0073116e0083114e0093112e00a3110e00b310ee00c310ce00d310ae00e3108e00f3106e0103104e0113102e0123100e013314f295ad84ff05300505456d00a444e204e29507051d041204d2990704dd050204c29d07049d04f204b29107145d046204a29507141d02020492990713dd00020d0713de00020107113e7002058719fe6013122e0023120e003311ee004311ce005311ae0063118e0073116e0083114e0093112e00a3110e00b310ee00c310ce00d310ae00e3108e00f3106e0103104e0113102e0123100e013314f290dd84ff04f00505409d00a444b204e29507004d020204d29907040f07c8400208df8630000220df1140b1bf80200002850d00beb0203587838b3987838b3d87838b3187938b3587938b3987938b3d87938b3187a38b3587a38b3987a38b3d87a38b3187b38b3587b38b3987b38b3d87b38b3187c38b3587c38b3987c38b3d87c38b31432502ad0d12be0013222e0023220e003321ee004321ce005321ae0063218e0073216e0083214e0093212e00a3210e00b320ee00c320ce00d320ae00e3208e00f3206e0103204e0113202e0123200e013324f2a04d84ff077000bf8020003d100208df8630003e00beb020000214170def8d07000261bf80600002851d00beb0602507840b3907840b3d07840b3107940b3507940b3907940b3d07940b3107a40b3507a40b3907a40b3d07a40b3107b40b3507b40b3907b40b3d07b40b3107c40b3507c40b3907c40b3d07c40b31436502ed0d1502624e0013622e0023620e003361ee004361ce005361ae0063618e0073616e0083614e0093612e00a3610e00b360ee00c360ce00d360ae00e3608e00f3606e0103604e0113602e0123600e0133601240a2f1ed364224ff47a7342f2107504eb84004000b7fbf0f10a2911d304fb02f0b7fbf0f10a290bd304fb03f0b7fbf0f10a2905d36c43b7fbf4f00928e7d800e004463046cdf804e0cdf80c804f2e98bf4f20c0f10008009018eb060e53d0b7fbf4f00a2c00f130010bf806104cf6cd41ccf6cc41a4fb012147d31ef1010242d000fb147cc9085a46bcfbf1fb97190bf13000787048f21f50c5f2eb109246642ca4fb002033d31ef1020232d00bfb11c14009b1fbf0f244f6d35302f13005c1f26203bd70b4f57a7fa4fb033523d31ef10303d34617d002fb1010a90941f25973b0fbf1f2cdf2b713a4fb033502f13003fb7002fb110742f21070043684424fea5534a9d209e0009e07e0013605e0023602e0009e00e00336d346019900270bf806701bf80700002850d00beb0704607838b3a07838b3e07838b3207938b3607938b3a07938b3e07938b3207a38b3607a38b3a07a38b3e07a38b3207b38b3607b38b3a07b38b3e07b38b3207c38b3607c38b3a07c38b3e07c38b31437502fd0d13ae0013722e0023720e003371ee004371ce005371ae0063718e0073716e0083714e0093712e00a3710e00b370ee00c370ce00d370ae00e3708e00f3706e0103704e0113702e0123700e013374f2f13d84ff075000bf807000ed00beb070273204e2f507008d020204d2f907004d070204c2fd07040f0b98200208df86300d1f8d4604ff0000a1bf80a10002967d00beb0a04617859b3a17861b3e17869b3217971b3617979b3a17981b3e17989b3217a91b3617a99b3a17aa1b3e17aa9b3217bb1b3617bb9b3a17bc1b3e17bc9b3217cd1b3617cd9b3a17ce1b3e17ce9b30af1140abaf1500fced14ff0500a37e00af1010a34e00af1020a31e00af1030a2ee00af1040a2be00af1050a28e00af1060a25e00af1070a22e00af1080a1fe00af1090a1ce00af10a0a19e00af10b0a16e00af10c0a13e00af10d0a10e00af10e0a0de00af10f0a0ae00af1100a07e00af1110a04e00af1120a01e00af1130a01240a2e1ed364224ff47a7342f2107704eb84014900b6fbf1f50a2d11d304fb02f1b6fbf1f50a2d0bd304fb03f1b6fbf1f50a2d05d37c43b6fbf4f10929e7d800e00c46d646baf14f0f98bf4ff04f0ecef1000842f2107c18eb0a074dd0b6fbf4f14cf6cd4001f13002ccf6cc400bf80a200a2ca4fb003241d37b1c3dd001fb1461d208b1fbf2f548f21f5005f130030beb0a06c5f2eb107370642ca4fb000330d3b81c29d005fb12115a09b1fbf2f5b4f57a7f05f13000b07044f6d350c1f26200a4fb000320d3f81c16d005fb1210990941f25973b0fbf1f2cdf2b713a4fb033502f13003f37002fb11060af1040a64454fea5534afd209e0f24607e00af1010a04e00af1020a01e00af1030a039c00220bf80a20cdf808901bf80200002850d00beb0200417839b3817839b3c17839b3017939b3417939b3817939b3c17939b3017a39b3417a39b3817a39b3c17a39b3017b39b3417b39b3817b39b3c17b39b3017c39b3417c39b3817c39b3c07c38b31432502ad0d132e0013222e0023220e003321ee004321ce005321ae0063218e0073216e0083214e0093212e00a3210e00b320ee00c320ce00d320ae00e3208e00f3206e0103204e0113202e0123200e013324f2a0bd84ff075000bf8020006d00beb020073214e2a417040f0538100208df863009df81490b9f1000f10d002220beb020111f8013c63b11bf8023063b14b784bb1897804320029f1d1023a04e0002105e0013a00e0013202eb4201490041eae1718a1c04f5207c01f1010e02f0030822f0030704f5a0724ff0ff3504e001350d2d02f5a07269d005eb8501bef1030f0ceb811405d24ff0ff31b8f1000f2ed1ede70021164604e000bf8f4206f1020621d04b1cdb09042b03d8337803f0f00333708b1cdb09042b03d8737803f00f037370cb1cdb09042b03d8737803f0f00373700431cb09042bdfd84b08e05c00f00f00e054d9e70139b8f1000fbfd08e1cf009042808d87008235cce076ff00f0608bf0f2633402354b8f1010fafd0ce1cf009042809d87008235cce074ff00f0608bf6ff00f0633402354b8f1020f9ed00e1df00904289ad87008c907235c6ff00f0108bf0f211940215490e7b9f1000f00f0ab80039843f2e57cc0f2000c00f2812a02220df1140efc4440f27d2b40f27a2808e01ef8019f0632b9f1000f0af1030a00f09280a9f17f0010f15f0ff1d309eb49000ceb8000a0f5c0700027d409514605e000bf02370c2f01f52071e1d0c357042c06d8b3f1ff3f03dc0e7846f0f0060e70042cdeb206d816f0400303d00b7843f00f030b70b30605d55a4503d84b7843f0f0034b70f30606d5b2f51f7f03d84b7843f00f034b7033074fea920305d59e2b03d88d7845f0f0058d70750705d5424503d88d7845f00f058d70c51995f90160042c08d8b6f1ff3f05dc91f8405145f0f00581f84051042cf6b208d816f0400505d091f8405145f00f0581f84051b50607d55a4505d891f8415145f0f00581f84151f50608d5b2f51f7f05d891f8415145f00f0581f84151350707d59e2b05d891f8423143f0f00381f8423173077ff588af42453ff685af91f8423143f00f0381f842317de7dde90298fff719b900218170ace60020107146e50020d070fff783bb00201072fff772b9f0b581b041f204054cf65561cef20005ccf2ac51c5f8ac1f4ef6fc51cef200010a6843f6fc7642f080720a6055f8041cc2f2000641f0010145f8041c316807460a466ff39602b2f1005f07d108684ef66362ccf2ff02904200f0cb8044f6d341c0f247014ff40c708847002800f0f98001464ff6d050cff6ff7000220b18463083f8302283f8312283f8322283f8332283f8342283f8352283f8362283f8372283f8382283f8392283f83a2283f83b2283f83c2283f83d2283f83e2283f83f2283f8402283f8412283f8422283f8432283f8442283f8452283f8462283f8472283f8482283f8492283f84a2283f84b2283f84c2283f84d2283f84e2283f84f2283f8502283f8512283f8522283f8532283f8542283f8552283f8562283f8572283f8582283f8592283f85a2283f85b2283f85c2283f85d2283f85e2283f85f2283f8602283f8612283f8622283f8632283f8642283f8652283f8662283f8672283f8682283f8692283f86a2283f86b2283f86c2283f86d2283f86e2283f86f2283f8702283f8712283f8722283f8732283f8742283f875227ff470af4ef66360ccf2ff000860012081f8ce004ff0ff30a1f8e000a1f8e200a1f8e400a1f8e600a1f8e800a1f8ea00a1f8ec00a1f8ee00a1f8f000a1f8f200a1f8f400a1f8f600a1f8f800a1f8fa00a1f8fc00a1f8fe003160d1f8d800b0bb44f6342245f6e464c2f20702cff6f8741368204616689e420ed116689e420bd116689e4208d116689e4203d1002800f10400efd19e421ad02b6816681068b0420ed11068b0420bd11068b04208d11068b04203d1002c04f10404efd1b04206d02868c01ac1f8d80001e0002030602868386001b0f0bd00bf2de9f04141f2040843f6fc77cef20008c2f200070146d8f8004038680d6801466ff39601b1f1005f07d101684ef66362ccf2ff02914200f0ca8044f6d341c0f247014ff40c708847002800f0d0804ff6d051cff6ff7100224318463183f8302283f8312283f8322283f8332283f8342283f8352283f8362283f8372283f8382283f8392283f83a2283f83b2283f83c2283f83d2283f83e2283f83f2283f8402283f8412283f8422283f8432283f8442283f8452283f8462283f8472283f8482283f8492283f84a2283f84b2283f84c2283f84d2283f84e2283f84f2283f8502283f8512283f8522283f8532283f8542283f8552283f8562283f8572283f8582283f8592283f85a2283f85b2283f85c2283f85d2283f85e2283f85f2283f8602283f8612283f8622283f8632283f8642283f8652283f8662283f8672283f8682283f8692283f86a2283f86b2283f86c2283f86d2283f86e2283f86f2283f8702283f8712283f8722283f8732283f8742283f875227ff470af4ef66361ccf2ff010160012180f8ce104ff0ff31a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe103860d0f8d81081b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce000203860fa2048e044f6342245f6e466c2f20702cff6f8761368314600bf17689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422ad0d8f80030176800bf1168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94214d0d8f8001044f6d357ca1ac1f26207a2fb0776c0f8d820b00901284ff0010088bfb009994208bffa2000e0fa20611bb1fbf0f0bde8f08100bf2de9f04f83b043f6fc76c2f200063568824628466ff3960044f6d347b0f1005fc0f2470707d128684ef66361ccf2ff01884200f0c8804ff40c70b8470546002800f0c0804ff6d050cff6ff70002100bf2a18463082f8301282f8311282f8321282f8331282f8341282f8351282f8361282f8371282f8381282f8391282f83a1282f83b1282f83c1282f83d1282f83e1282f83f1282f8401282f8411282f8421282f8431282f8441282f8451282f8461282f8471282f8481282f8491282f84a1282f84b1282f84c1282f84d1282f84e1282f84f1282f8501282f8511282f8521282f8531282f8541282f8551282f8561282f8571282f8581282f8591282f85a1282f85b1282f85c1282f85d1282f85e1282f85f1282f8601282f8611282f8621282f8631282f8641282f8651282f8661282f8671282f8681282f8691282f86a1282f86b1282f86c1282f86d1282f86e1282f86f1282f8701282f8711282f8721282f8731282f8741282f875127ff470af4ef66360ccf2ff002860012085f8ce004ff0ff30a5f8e000a5f8e200a5f8e400a5f8e600a5f8e800a5f8ea00a5f8ec00a5f8ee00a5f8f000a5f8f200a5f8f400a5f8f600a5f8f800a5f8fa00a5f8fc00a5f8fe003560baf1000f18bf002d07d14af26950c0f2450003b0bde8f04f0047daf80cb0bbf1000ff2d0daf82080b8f1000fedd0a94659f8040f00287ad06869002879d0686a002878d0686b002877d0686c002876d0686d002875d0686e002874d0686f002873d0d5f88400002871d0d5f8940000286fd0d5f8a40000286dd0d5f8b40000286bd0d5f8b00029690290d5f8a0002c6a0190d5f89000d5f880600090d5f870e0d5f860c02b6d286b2a6c8c4238bf21464ff0000438bf012488423cbf014602248a423cbf114603248b423cbf194604248c453cbf614605248e453cbf714606248e423cbf31460724009888423cbf01460824019888423cbf014609240298884238bf0146d5f8c00038bf0a24884238bf0b2409eb0410406807f1440126018847002049f80600012085f8df0016e0002414e0012412e0022410e003240ee004240ce005240ae0062408e0072406e0082404e0092402e00a2400e00b244046b84700283ff452af08f0030c5fea98034ff0000219d0a2eb830e431c0bf101066ff00307029400bff119cc78da19d4700c79043714714c79547189790eeb070404349171f0d1029c3a1dbcf1000f10d01bf80230bcf1010f83540ad0531c1bf80370bcf1020fc75403d0911c1bf80120425409eb04114860d5f8c400220149f802a0421cc1f80880c5f8c420c8600de738464146fff722be2de9f04f81b043f6fc79c2f20009d9f80050044628466ff3960044f6d34816460f46b0f1005fc0f2470807d128684ef66361ccf2ff01884200f0c8804ff40c70c0470546002800f01e814ff6d050cff6ff7000212a18463082f8301282f8311282f8321282f8331282f8341282f8351282f8361282f8371282f8381282f8391282f83a1282f83b1282f83c1282f83d1282f83e1282f83f1282f8401282f8411282f8421282f8431282f8441282f8451282f8461282f8471282f8481282f8491282f84a1282f84b1282f84c1282f84d1282f84e1282f84f1282f8501282f8511282f8521282f8531282f8541282f8551282f8561282f8571282f8581282f8591282f85a1282f85b1282f85c1282f85d1282f85e1282f85f1282f8601282f8611282f8621282f8631282f8641282f8651282f8661282f8671282f8681282f8691282f86a1282f86b1282f86c1282f86d1282f86e1282f86f1282f8701282f8711282f8721282f8731282f8741282f875127ff470af4ef66360ccf2ff002860012085f8ce004ff0ff30a5f8e000a5f8e200a5f8e400a5f8e600a5f8e800a5f8ea00a5f8ec00a5f8ee00a5f8f000a5f8f200a5f8f400a5f8f600a5f8f800a5f8fa00a5f8fc00a5f8fe00c9f8005005f1040b4ff0000a4ff000094ff0ff30002200234ff0ff3105e000bf6032c02a03f1060332d0af187e68a64204d13e6986423cbf194630467e69a64204d13e6a86423cbf591c30467e6aa64204d13e6b86423cbf991c30467e6ba64204d13e6c86423cbfd91c30467e6ca64204d13e6d86423cbf191d30467e6da642ced13f6e87423cbf591d3846c8e700bf002910d40beb01104768090182684bf801a02046394600f013f8814608f1440138468847ace7484601b0bde8f08f204639463246c9f8005001b0bde8f04fffe72de9f047a4b0804640f6bb4043f6fc77c0f24e0091468a46c2f2000780473c68002021466ff39601b1f1005f02908df80c0007d120684ef66361ccf2ff01884200f0cb8044f6d341c0f247014ff40c7088470446002800f0bf804ff6d050cff6ff7000212218463082f8301282f8311282f8321282f8331282f8341282f8351282f8361282f8371282f8381282f8391282f83a1282f83b1282f83c1282f83d1282f83e1282f83f1282f8401282f8411282f8421282f8431282f8441282f8451282f8461282f8471282f8481282f8491282f84a1282f84b1282f84c1282f84d1282f84e1282f84f1282f8501282f8511282f8521282f8531282f8541282f8551282f8561282f8571282f8581282f8591282f85a1282f85b1282f85c1282f85d1282f85e1282f85f1282f8601282f8611282f8621282f8631282f8641282f8651282f8661282f8671282f8681282f8691282f86a1282f86b1282f86c1282f86d1282f86e1282f86f1282f8701282f8711282f8721282f8731282f8741282f875127ff470af4ef66360ccf2ff002060012084f8ce004ff0ff30a4f8e000a4f8e200a4f8e400a4f8e600a4f8e800a4f8ea00a4f8ec00a4f8ee00a4f8f000a4f8f200a4f8f400a4f8f600a4f8f800a4f8fa00a4f8fc00a4f8fe003c60baf1000f4ff0010618bfb9f1000f20d101af3846fff75bf902ad404651464a460123009500f0ebfd05463846fff787fa002e064608d19df80c0028b943f61f00c0f247004c308047002c18bfc4f8d06022e09af8000008280ed9a0f18301052905d8012202fa01f111f0290f09d100f07f000928ccd104e00121814011f4a47febd03cb143f61f00c0f24700804794f8280228b14ff0ff35284624b0bde8f0870026b5e700bf43f6fc70c2f20000016808466ff39600b0f1005f16d108684ef66362ccf2ff0290420fd1d1f8fc0160b144f63422c2f207021268801a0028cebf01200020c1f8fc01704700207047f0b101684ef66362ccf2ff02914218bf704790f8061299b110b544f62f344ff0000cc0f24604864680f806c20120002100220023aef804c2a446bde810406047704700bf70b543f6fc76c2f200063468054621466ff39601b1f1005f07d120684ef66361ccf2ff01884200f0cc8044f6d341c0f247014ff40c708847002800f0ea8004464ff6d050cff6ff70002100bf2218463082f8301282f8311282f8321282f8331282f8341282f8351282f8361282f8371282f8381282f8391282f83a1282f83b1282f83c1282f83d1282f83e1282f83f1282f8401282f8411282f8421282f8431282f8441282f8451282f8461282f8471282f8481282f8491282f84a1282f84b1282f84c1282f84d1282f84e1282f84f1282f8501282f8511282f8521282f8531282f8541282f8551282f8561282f8571282f8581282f8591282f85a1282f85b1282f85c1282f85d1282f85e1282f85f1282f8601282f8611282f8621282f8631282f8641282f8651282f8661282f8671282f8681282f8691282f86a1282f86b1282f86c1282f86d1282f86e1282f86f1282f8701282f8711282f8721282f8731282f8741282f875127ff470af4ef66360ccf2ff002060012084f8ce004ff0ff30a4f8e000a4f8e200a4f8e400a4f8e600a4f8e800a4f8ea00a4f8ec00a4f8ee00a4f8f000a4f8f200a4f8f400a4f8f600a4f8f800a4f8fa00a4f8fc00a4f8fe00346000230820032184f81b322a2384f8180284f81912102184f81c32042384f81e02012045f61b4c04f5067284f81a1284f81d3284f81f0284f82012002d18bf0125c0f2470c0a2384f82152bde8704060470020306070bd2de9f04143f6fc77c2f200073f684ff000083c466ff39604b4f1005f07d13e684ef66365ccf2ff05ae4208bfb84601280fd11c4615460f460646fff78ffe2346044639462a463046002c18bfb8f1000f06d144f62f3cc0f2460cbde8f041604798f8060270b1d8f800020021002888f80612a8f804123ed049f2d941c0f24401884738e0b8f804020121013080b2012898bf0846a8f80402d8f8000249f2d944c0f2440488f8061268b94ff6eb40cff6ff70a4f594777844002142460023b847c8f8000240b1a047d8f80002a4f140024ff4c8719047c8b198f80602002808bfbde8f081d8f80002002188f80612a8f8041200b1a04744f62f3cc0f2460c0120002100220023bde8f04160474af26950c0f245008047012830d1082088f80802032088f80902102088f80a02002088f80b02b22088f80c02062088f80d0288f80e024620b8f8041288f80f02432088f8100201200f0a45f6153c08f5027288f8110288f8120288f81312c0f2470c09210d2388f81472bde8f0416047bde8f08170b50446d0f8000249f2b1360d46c0f2440668b94ff60540cff6ff707844002122460023b0470028c4f8000208bf70bd06f594718847d4f8000206f1e8022946bde87040104700bf2de9f04f83b04768002f00f0a2828368002b00f09e824ef6636e07eb030b4ff00008ccf2ff0e0025002414e044f63423c2f207031b6845f69075c0f201052b44c4f8fc31029b4b4400255b451f464ff0000480f07e82ba461af9013b03f07f06ae40002b44ea060410d404f00703012b16dc0bb3012b40f06c82abeb0a03082bc0f0678207f10903dee700bfda4580f06082182d05f107055746dcd959e200bf022b3ad0052b40f05482abeb0a03042bc0f04f827b1dc7e7da4580f04a8297f90130002b07f10203bed55b4580f0418297f90230b3f1ff3f07f10303b4dc5b4580f0378297f90330b3f1ff3f07f10403aadc5b4580f02d8297f90430b3f1ff3f07f10503a0dc5b4580f0238297f90530002b00f11e82bb1d96e7da4580f0198297f9013007f1020c002b03f07f091fd5dc4580f00e8297f90250fb1c05f07f06b5f1ff3f49eac61902dde2469c460fe05b4580f0fe8197f9036007f1040c06f07f05b6f1ff3f49ea853940f323819a46abeb0c03994500f2ec8124f00703b3f54a7fcdf808c07ff45dafb9f1060ffff459af029b1b78462b7ff454af9af80230432b7ff44faf9af80330012b7ff44aaf43f6fc76c2f20006346823466ff39603b3f1005f03d12368734500f0d180019144f6d34105464ff40c70c0f2470117468847002800f028814ff6d05301990446cff6ff7328463a46e718463387f8308287f8318287f8328287f8338287f8348287f8358287f8368287f8378287f8388287f8398287f83a8287f83b8287f83c8287f83d8287f83e8287f83f8287f8408287f8418287f8428287f8438287f8448287f8458287f8468287f8478287f8488287f8498287f84a8287f84b8287f84c8287f84d8287f84e8287f84f8287f8508287f8518287f8528287f8538287f8548287f8558287f8568287f8578287f8588287f8598287f85a8287f85b8287f85c8287f85d8287f85e8287f85f8287f8608287f8618287f8628287f8638287f8648287f8658287f8668287f8678287f8688287f8698287f86a8287f86b8287f86c8287f86d8287f86e8287f86f8287f8708287f8718287f8728287f8738287f8748287f875827ff470af4ef6636e0123ccf2ff0e84f8ce304ff0ff33c4f800e0a4f8e030a4f8e230a4f8e430a4f8e630a4f8e830a4f8ea30a4f8ec30a4f8ee30a4f8f030a4f8f230a4f8f430a4f8f630a4f8f830a4f8fa30a4f8fc30a4f8fe3034609af804309af805709af80660032b47ea062732dd052b61dc042b00f08180052b7ff458aed4f82c32002b00f0cb8044f63425c2f207052f68db1b002bd8bf84f82a82c1e0dc4580f0cc8097f904607b1d06f07f05b6f1ff3f49ea45593ff7bdae5b4580f0be8097f90550002d00f1b98007f1060c49ea0579c1e6012b3ff41eae022b7cd0032b7ff425aecde900208a467546fff7c7fb009a03460198ae465146002b3ff417ae94f80632002b3ff412ae204641f28831a4f80472fff7c5fddde90020ae46514605e6019928463a46c6f800804ef6636eccf2ff0efbe5062b49d0072b7ff4f7ad01904ef68f30c0f2490015460f4674468047034601985e1ea64639462a46012e3ff6e5ad981eb0fa80f04009fff7cdfba646394601982a46d9e5019015468a467646fff77cfb03460198b64651462a46002b3ff4ccad94f80632002b3ff4c7adb4f80432bb427ff4c2add4f8003284f80682002ba4f804823ff4b9ad49f2d9411846c0f244018847b6465146d2e7c4f82c8284f82a82aae594f80632c4f8fc81002b3ff4a4ad0546d4f800028a46174684f80682a4f8048220b149f2d941c0f24401884744f62f340120002100220023c0f24604a04728463a46514686e784f82a8244f63423c2f207031b6845f69075c0f201052b44c4f82c3278e540f22113c0f2490303b0bde8f04f184700bf7fb50646002806d1fff712fb002807d19de80f00064641f6d77cc0f24e0c60477fbd00bf092959d12de9f047a22602eb030cd65406268cf8016041f64068c0f200084ff0000ef84418f80e700ceb0e0508eb0e06ef707778b47896f8039096f804a02f716c7185f8069085f807a074790ef11b0e2c72b479bef1510f6c72f479ac72347aec72747a2c73b47a6c73f47aac73347bec73747b2c74b47b6c74f47bac74347cec74747c2c75b47c6c75f47cac75347dec75747d2c76b47d6c76f47dac76347eec76747e2c77b47e6c77bbd1502753338cf80270bde8f04745f6153cc0f2470c604700bf4df63040c2f234000078042818bf704780b582b04af26b1e4ff0000cc0f24d0e0020002100220923cdf804c0cdf800c0f04702b080bd00bff0b583b04df63043c2f234031b784ff6e704042bc0f2450420d144f2d043c2f207031b680546184616460f46a04703463946324628468bb11b68e02b0ed14af26b160027c0f24d060020002100220a2301970097b047012003b0f0bd04f1160303b0bde8f04018472de9f04f95b0002918bf002a04d115b0bde8f04f00f06cbe0f78ddf878b007f07f06082e5bdc052e00f00981072e00f09482082e5bd10746002b4ff0ff3000f042864af28973cff6fd73d61802339e42c0f0398691f801903846b9f1000f00f0788202274ff00108be1c964200f2c285cb19cf5d5b7857ea032300f0bb859f19974200f2b7858446885d09280dd9a0f18305062d4ff0ff3000f2158608fa05f515f0490f06d100f00ebe08fa00f515f4127fecd014460d46314460461a4600236646cdf800b0fff79bff002840f09285b9f10109294622463046c5d139e2092e00f0ad810a2e00f05983422e8fd0f51e18bf0125b41f18bf0124032afff487af25407ff484af00254c1ca2f1010c8df81c508df81d508df820508df824508df825508df826508df827508df828508df829508df82a508df82b508df82c508df82d508df82e508df82f508df830508df831508df832508df833508df834508df835508df836508df837508df838508df839508df83a508df83b508df848508df849508df84a508df84b508df84c508df84d508df84e508df84f508df850508df851508df852508df85350cde9074c4df26f14cff6ff747c440f944df27314cff6ff747c44032e1094119500f02084062e40f010850646002000211d4601f022f9b168002900f00685b6f84020b6f842005043b0f5163fc0f0fd8407a84ff4a0724ff4f07301f06afa002800f0f384dbf800000f280dd80bebc0010022ca800a814ff420724a814ff4f07201308a81cbf80000002d304640f08c8100f043bd43f6fc78c2f20008022a2cbf4d78ff25d8f8004020466ff39600b0f1005f07d120684ef66363ccf2ff03984200f0d0800f4644f6d341c0f247014ff40c7016468847002800f08a8304464ff6d050cff6ff70002132462318463083f8301283f8311283f8321283f8331283f8341283f8351283f8361283f8371283f8381283f8391283f83a1283f83b1283f83c1283f83d1283f83e1283f83f1283f8401283f8411283f8421283f8431283f8441283f8451283f8461283f8471283f8481283f8491283f84a1283f84b1283f84c1283f84d1283f84e1283f84f1283f8501283f8511283f8521283f8531283f8541283f8551283f8561283f8571283f8581283f8591283f85a1283f85b1283f85c1283f85d1283f85e1283f85f1283f8601283f8611283f8621283f8631283f8641283f8651283f8661283f8671283f8681283f8691283f86a1283f86b1283f86c1283f86d1283f86e1283f86f1283f8701283f8711283f8721283f8731283f8741283f875127ff470af4ef66360ccf2ff002060012084f8ce004ff0ff303946a4f8e000a4f8e200a4f8e400a4f8e600a4f8e800a4f8ea00a4f8ec00a4f8ee00a4f8f000a4f8f200a4f8f400a4f8f600a4f8f800a4f8fa00a4f8fc00a4f8fe00c8f8004094f8080178b1d4f8040148b149f29943c0f2440340330e46174698473a463146002084f808010123032a80f0b782c1e23f064ff0210658bf1126b242c0f0d383002f01f101050ed54af26952c0f245020f4604461e469047334601462046022918bf07f1110584462879697950ea01264ff0ff301ebfa979ea7951ea022900f01b84297b6a7b41ea02218e4240f01484a97bea7b41ea0221894540f00d842878697840ea01217018b0f5207f4ff0ff3000f20284aa78ec7842ea042209eb0204b4f5f07f00f2f8832c7a6f7a44ea072806eb0807b7f5207f00f2ee83af7aed7a47ea052509eb0507b7f5f07f00f2e483dcf80800002800f07683bcf84070bcf842406743b7f5163fc0f06d839a4643466446cde90169009500f073fddbf800000f280ad80bebc0010130a1f806800d814e81a1f80c90cbf80000baf1000f00f0ba832046594600f046fcb4e343f6fc74c2f20004236818466ff39600b0f1005f07d118684ef66367ccf2ff07b84200f0cf800e4644f6d341c0f247014ff40c70154688470346002800f0bf804ff6d050cff6ff7000211a18463082f8301282f8311282f8321282f8331282f8341282f8351282f8361282f8371282f8381282f8391282f83a1282f83b1282f83c1282f83d1282f83e1282f83f1282f8401282f8411282f8421282f8431282f8441282f8451282f8461282f8471282f8481282f8491282f84a1282f84b1282f84c1282f84d1282f84e1282f84f1282f8501282f8511282f8521282f8531282f8541282f8551282f8561282f8571282f8581282f8591282f85a1282f85b1282f85c1282f85d1282f85e1282f85f1282f8601282f8611282f8621282f8631282f8641282f8651282f8661282f8671282f8681282f8691282f86a1282f86b1282f86c1282f86d1282f86e1282f86f1282f8701282f8711282f8721282f8731282f8741282f875127ff470af4ef66360ccf2ff001860012083f8ce004ff0ff30a3f8e000a3f8e200a3f8e400a3f8e600a3f8e800a3f8ea00a3f8ec00a3f8ee00a3f8f000a3f8f200a3f8f400a3f8f600a3f8f800a3f8fa00a3f8fc00a3f8fe0031462a462360022a2cbf4978ff21002b00f0cc8202294ff0000000f0e681012900f0df81002940f0c2824ff0ff31c3f8dc00a3f8cc00c3f8c800a3f8e010a3f8e210a3f8e410a3f8e610a3f8e810a3f8ea10a3f8ec10a3f8ee10a3f8f010a3f8f210a3f8f410a3f8f610a3f8f810a3f8fa10a3f8fc10a3f8fe1083f8000115b0bde8f08f022ac0f0298243f6fc74c2f20004206802466ff39602b2f1005f07d102684ef66363ccf2ff039a4200f0cc800d4644f6d341c0f247014ff40c708847002800f097814ff6d051cff6ff7100224318463183f8302283f8312283f8322283f8332283f8342283f8352283f8362283f8372283f8382283f8392283f83a2283f83b2283f83c2283f83d2283f83e2283f83f2283f8402283f8412283f8422283f8432283f8442283f8452283f8462283f8472283f8482283f8492283f84a2283f84b2283f84c2283f84d2283f84e2283f84f2283f8502283f8512283f8522283f8532283f8542283f8552283f8562283f8572283f8582283f8592283f85a2283f85b2283f85c2283f85d2283f85e2283f85f2283f8602283f8612283f8622283f8632283f8642283f8652283f8662283f8672283f8682283f8692283f86a2283f86b2283f86c2283f86d2283f86e2283f86f2283f8702283f8712283f8722283f8732283f8742283f875227ff470af4ef66361ccf2ff010160012180f8ce104ff0ff31a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe10294620604978012900f00981002940f04381002480f807424af26950c0f24500804701462046012940f09f8145f2e550c0f25400883015b0bde8f04f00470023c8f80030002439463246032a0bd355b98878082800f2888142f6c521c0f250019631884780e1052a1ad3012d18d18d78681e06284ff0000000f27781cb78032b00f273810a79002a00f06f8142f6c520c0f2500000f59a7728461946b84763e1022d40f0888042f6c520c0f2500080475ae13f0600f1d080082ac0f0ed8081464ff0070e4ff0050c01268c19a5784ff0ff30002d00f04981e778002f00f0458111f806a04fea850808eb8a06b6f5207f00f23b8164787e0006eb4407b7f5f07f00f2338101eb0c00cdf81890894611f80c104078774641ea002081b20120cde9043200f0d1fc002840f01e8106998868002800f0b180b1f84020b1f842105143b1f5163fc0f0a88009eb0701079105996a00c91b089104eb840100ebc11000eb4a0107a8334600f00bfe002800f09480dbf8001004980f290cd84fea8a0263000bebc1070131fa803b81a7f80a80be81cbf80010002806987ff42dade4e0012083f8ce00e0e083f8ce0015b0bde8f08f0020206070e0072a73d3032d71d18878cb780a7940ea03204d798e7901284ff0010498bf204644f62061884228bf084642f6c521c0f25001642a28bf642201f5e2731146984744f20450c2f207000068002800f0b18045ea062149f29942012988bf0c46c0f2440221469047a4e00121044680f807124af26950c0f245008047012840f0998045f2e550c0f254008047014600284ff0000000f08f8084f80702084615b0bde8f08f0c2a1dd3cf78ce79b74219d10f790e7ab74215d14af26954c0f245040d46164680461f46a047324605263b462946c146022808bf01264ff00b0e4ff0090c18e74ff0ff3015b0bde8f08f032a4ff000005fd32f1fb7fa87f77f093b4059d0d01e4cf6cd42ccf6cc428b78a0fb0202b3eb920f88bf9308302b184628bf3020002b4ad000eb800303f0030c03f4fe770023ce18f578e21882f80a513579043382f80b5175799f4282f80c51b67982f80d61eed1bcf1000f10d01944ca782344bcf1010f83f80a2108d00a79bcf1020f83f80b211cbf497983f80c1184f80801d4f8040149f29945c0f24405002184f8091160b94cf28f60cff6ff70a5f1e807784422460023b847c4f8040158b10121a847002015b0bde8f08f0020a4f8080115b0bde8f08f42f6c5200025c0f2500084f808518047284615b0bde8f08ff0b583b043f6fc73c2f200031f683b466ff39603b3f1005f09d13b684ef66366ccf2ff06b34204bf002387f82a32002958d0362a56d30b78422b53d14b784d2b50d10b7f4f7f43ea0723042b4ad18b7dcf7d0e7e43ea072343ea06434f7e8e7ccd7c53ea076346ea05270e7d4d7d47ea064747ea05641d4648bf5d42b0f84070002b4ff00003c8bf0123bc422ad1b0f84270bd4226d18f7ace7a91f80cc047ea06274e7b47ea0c4747ea0667974219d2621c032606eb5202866822f0030cca19cde901c30746304621462346009500f0d5fe3846314622462b4600f069ff002003b0f0bd4cf2af53c0f24d0303b0bde8f04018472de9f04182b043f6fc77c2f200073d68884629466ff39601b1f1005f064607d128684ef66361ccf2ff01884200f0cb8044f6d341c0f247014ff40c7088470546002800f0bf804ff6d050cff6ff7000212a18463082f8301282f8311282f8321282f8331282f8341282f8351282f8361282f8371282f8381282f8391282f83a1282f83b1282f83c1282f83d1282f83e1282f83f1282f8401282f8411282f8421282f8431282f8441282f8451282f8461282f8471282f8481282f8491282f84a1282f84b1282f84c1282f84d1282f84e1282f84f1282f8501282f8511282f8521282f8531282f8541282f8551282f8561282f8571282f8581282f8591282f85a1282f85b1282f85c1282f85d1282f85e1282f85f1282f8601282f8611282f8621282f8631282f8641282f8651282f8661282f8671282f8681282f8691282f86a1282f86b1282f86c1282f86d1282f86e1282f86f1282f8701282f8711282f8721282f8731282f8741282f875127ff470af4ef66360ccf2ff002860012085f8ce004ff0ff30a5f8e000a5f8e200a5f8e400a5f8e600a5f8e800a5f8ea00a5f8ec00a5f8ee00a5f8f000a5f8f200a5f8f400a5f8f600a5f8f800a5f8fa00a5f8fc00a5f8fe003d60b068002818bf002d02d102b0bde8f081b6f84010b6f842205143b1f5163ff4d3c5f8240201264ff4f07085f8286201904ff4207044f267040090c0f2470400200021002200230027a04730b185f82872c5f8247202b0bde8f08188f8046002b0bde8f08100bf2de9f04f8ab0dde914a5089243ea010242ea0a02d207864640f0c4800eeb51004ff0ff374fea5a0c0eeb53068646012207eb5a00bcf1010f88bf4fea5a02069002f003000790caf341006ff00107059007eb5a006ff00207019007eb5a00009022f00302701c544203900ef101004ff0000b09960290cdf810e004e00bf1010bab4500f0c3810898139e5f466fea0b02864288bfaf183a183744099802eb820207eb87070eeb82198b4200eb871835d9059e6246b6b10698012e19f8002008f8002002460ed00198022e19f8002008f80020024606d0009819f8002008f80020024600bf06980328c8d300bf09eb020717f8016c08eb020000f8016c17f8026c043a00f8026c17f8036c00f8036c17f8047c00f8047ce9d1b0e700bfbaf1080f02d2002223e000bf03989601ba0102440298e64600eb060a6ff0030c0aeb0c00c67802eb0c07fe7006790cf1040c3e7146797e71807904eb0c060436b871edd10cf10402dde914a5f446ddf810e0079800283ff483af079e19f80200012e08f802003ff47baf501c079e19f80070022e08f800703ff472af901c19f8002008f800206be74ff6fc7000ea0a000af0030b0990aaf1010000240490cde905be07e0079c159d0134ddf850a0ac4200f01e81089f139ee0432246be42079488bf2a18d01900eb80000eeb801c901900eb8000baf1040f0eeb80184ff00009c0f09880ddf810a0ddf850e048468b4288bf504600eb010400eb03054fea54001cf800706ff0010080ea090009f1010288bf0eeb00026ff0020089ea000609f1020088bf0eeb06006ff0030686ea090609f1030b4feac47488bf0eeb060b07f00f06002c4fea550408bf3e0918f80470ed0707f00f0507f0f007374408bf45ea061708f80470541865081cf805501a44e40705f00f064fea520408bf2e0918f80450d20705f00f0205f0f005354408bf42ea0615421808f8045054081cf804401844d20704f00f054fea500208bf250918f80240c00704f00f0004f0f0042c4408bf40ea05140beb010008f8024042081cf802200beb0304c00702f00f054fea540008bf150918f80020e40702f00f0402f0f0022a4408bf44ea051208f80020099809f104098145aaf1040a7ff46eafdde905bebbf1000f3ff444af6fea09004a468b42149f88bf3a18501845081a44c0071cf805504fea520018f8007005f00f0408bf2c0907f00f0507f0f0072744d20708bf45ea0417bbf1010f08f800703ff420af149809f10102a0eb090a8b4288bfaaf10202541865081a44e4071cf805504fea520418f8040005f00f0708bf2f0900f00f0500f0f0003844d20708bf45ea0710bbf1020f08f804003ff4faae09f102008b4288bfaaf10300421854081844d2071cf804704fea500218f8024007f00f0508bf3d0904f00f0704f0f0042c44c00708bf47ea051408f80240d9e60ab0bde8f08f00bf70b543f6fc76c2f2000632680c4611466ff39601b1f1005f054607d110684ef66361ccf2ff01884200f0cb8044f6d341c0f247014ff40c708847002800f00b8102464ff6d050cff6ff7000211318463083f8301283f8311283f8321283f8331283f8341283f8351283f8361283f8371283f8381283f8391283f83a1283f83b1283f83c1283f83d1283f83e1283f83f1283f8401283f8411283f8421283f8431283f8441283f8451283f8461283f8471283f8481283f8491283f84a1283f84b1283f84c1283f84d1283f84e1283f84f1283f8501283f8511283f8521283f8531283f8541283f8551283f8561283f8571283f8581283f8591283f85a1283f85b1283f85c1283f85d1283f85e1283f85f1283f8601283f8611283f8621283f8631283f8641283f8651283f8661283f8671283f8681283f8691283f86a1283f86b1283f86c1283f86d1283f86e1283f86f1283f8701283f8711283f8721283f8731283f8741283f875127ff470af4ef66360ccf2ff001060012082f8ce004ff0ff30a2f8e000a2f8e200a2f8e400a2f8e600a2f8e800a2f8ea00a2f8ec00a2f8ee00a2f8f000a2f8f200a2f8f400a2f8f600a2f8f800a2f8fa00a2f8fc00a2f8fe0032600120002d82f8cc0047d0104630f8e01fa1423bd0b2f8e210a14237d0b2f8e410a14233d0b2f8e610a1422fd0b2f8e810a1422bd0b2f8ea10a14227d0b2f8ec10a14223d0b2f8ee10a1421fd0b2f8f010a1421bd0b2f8f210a14217d0b2f8f410a14213d0b2f8f610a1420fd0b2f8f810a1420bd0b2f8fa10a14207d0b2f8fc10a14203d0b2f8fe10a1420ad1012082f8de0070bd0020306070bd82f8cd00002070bd92f8cd1081b9b2f8c810611a0bb2b3f1ff3f06dd89b2022924bf012182f8dd1002e0012182f8dc10b2f8ca30002182f8cd109c42a2f8c84088bfa2f8ca4092f8001120f81140481c00f00f0082f80001002070bd2de9f04fcdb04ef687249246c0f25b044df2546298460e4604f13c07c0f278020f2138230546b84718b12846a047002423e20afb08f040000b960295059001250da94ff4807004f585724ff0000b4ff0000947464ff000080023564604930a930697cdf820a00192029ce1602061204600219047e1680daa891a07910da900f0e681ddf82ce00021002400900be000bf04940c9c002101234ff0030807980134844200f0d181002940f0e0810da80c94045db8f1020febd0b8f1010f00f09280b8f1000f40f0968004f00f000a900598210900f0a6808842c0f08880401a05900a9800eb00140001039014e01cf80b000a9a00f0f00010440cf80b000bf1010b01390125b34524bf4ff0000b09f10109002900f08c81b9454ff000084ff0000338bf012380f0028109eb8900002d0eeb801cdbd0cdf82490a946a6eb0b05b5eb510f88bf4d088db3a8080ceb0b0820d0002708f10102a7eb80006ff0030600bf02eb060e043600eb060a1af104078ef803408ef804408ef805408ef80640efd1301d069f089eddf82ce015f0030204d10ce0002015f0030208d0012a08f8004004d04044022a447018bf8470a1eb4501ab4489b1b34510d21cf80b00039a00f00f001043ddf824900cf80b00013900250029a0d12be100bf00214d46ddf8249090e700bfbcb10598a04228d201214ff0000821e0ddf810a005985aea042a16d00c9c5045cdf810a080f0938001214ff00008002342e70c9c002101234ff002083ce70c9c00214ff00108012336e7002001214ff0000804900c9c00232ee7001b05900a9800eb00110001099015e000bf1af80b200a9802f0f00202430af80b200bf1010b013c0125b34524bf4ff0000b09f10109002c00f0da80b9454ff000084ff0000338bf012350d209eb8902002d0eeb821adcd0ac46a6eb0b05b5eb540f88bf650855b3ae080aeb0b0818d0002008f10102a0eb860e6ff00306901904360eeb06070437c170017141718171f5d1069fddf82ce0321d15f0030604d10de0002215f0030609d0012e08f8021005d04244022e517018bf917000bfa4eb4504ab447cb1089eb3450ed21af80b00099a00f00f0010430af80b00013c0025002cabd184e0089e002465469de70c9c0121b4e6a0eb0a0005900a9800eb00110001099015e01cf80b000a9a00f0f00010430cf80b000bf1010baaf1010a0125b34524bf4ff0000b09f10109baf1000f65d0b9454ff000084ff0000338bf01235bd20b9a09eb8900002d02eb801cdad0ce46a946a6eb0b05b5eb5a0f88bf4fea5a0545b3a8080ceb0b0817d0002408f10102a4eb80006ff003069719043684190434f97039717971b971f6d1301d069f089e0c9c15f0030204d10ce0002015f0030208d0012a08f8001004d04044022a417018bf8170aaeb450abaf1000fab4411d0b34511d21cf80b00099a00f00f0010430cf80b00aaf1010a0025f146baf1000fa6d10be04ff0000a4d46f14697e70c9c4ff00008002131e6012102e04ff000080021ddf82ce029e600980da973b1012817d04ff0000450b90798019ab0fa80f040094ff480703ff4ffad00e0002402984ef68721c0f25b01884720464db0bde8f08f059840ea0800b0fa80f04409eee72de9f04f83b00c9e0292002e019000f08e80019823f00304471c029803f0030ac4f1000c01384ff00008009005e000bf08f10108b0450f4479d00e98c14600286fea080418bf06eb0409002bf0d0042b06d24ff0000bbaf1000f2ed1e8e700bf0d98b64609fb00f400986ff003050444607800f0f00242ea10107a19d0706078043500f00f0646ea0010107114f8020f00f0f00646ea10105071207800f00f0646ea00100ceb050604369071e0d105f1040b7646baf1000fbad002980d9a4fea5b0509fb0204019814f805e008fb01050ef00f025feacb7908bf4fea1e1242ea0212baf1010f05f80b20a1d00bf1010e4fea5e00205cb9f1000f4fea101208bf00f00f0242ea0210baf1020f05f80e008ed00bf102004208a25cb9f1000f02f00f0408bf140944ea04122a5480e703b0bde8f08f2de9f04182b003fb02f68846cde9001645f20f1107466846c0f247011c461546884740f2196047f8240f788948f2816245ea0040b86045ea044057f8204c7860c0f2490220463946fe60c7f81080904740f25761c0f244012046884702b0bde8f0814556454e4346572f3820696d6735373620696d6736343020696d677a20726c652077616b656c65617365206469726563746662206662677561726420776561726e6f7469667920636f6d7061737331300000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a8900000000000000000", + "new": "02fb01f000e000bfb0b546f27424c2f22a040146206846f23f52c5f28802b2eb500f40f2543044f281124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f2543044f29f22c2f20000c0f24802104770b500284ed001684ef66762ccf2ff02914218bf70bd90f8081190f8095142f6c52c8d42c0f2500c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b149f29942c0f244022146bde87040104770bd00bf70b54ff6fd03c0f2450314460d46984746f27021c2f22a010e6831466ff39601b1f1005f0ad131684ef66762ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04af26951c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd47f25d02c0f2580208469047284670bd00bf2de9f04f9db046f2702ac2f22a0adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66767ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f2b870c2f20000d0f8008004a802f08effb8f1000f00f0dd81032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f0348196f8dc10002000298df8140000f0ac8105a90a5c002a00f07f810a185378002b00f03d819378002b00f03b81d378002b00f039811379002b00f037815379002b00f035819379002b00f03381d379002b00f03181137a002b00f02f81537a002b00f02d81937a002b00f02b81d37a002b00f02981137b002b00f02781537b002b00f02581937b002b00f02381d37b002b00f02181137c002b00f01f81537c002b00f01d81937c002b00f01b81d37c002b00f01981137d002b00f01781537d002b00f01581937d002b00f01381d37d002b00f01181137e002b00f00f81537e002b00f00d81937e002b00f00b81d37e002b00f00981137f002b00f00781537f002b00f00581937f002b00f00381d27f002a00f00181203060287ff47baf20e19bf82a0270b1dbf82c0240b144f63421c2f207010968401a002800f3ae8000208bf82a024cf61520c0f246001db0bde8f04f004704f005fb002800f0838000216ff017020023c518c1542f338618b3f50d7f02f12f026970a970e97029716971a971e97129726972a972e97229736973a973e97329746974a974e97429756975a97586f82f1086f830106976a976e97629776977a977e97785f8201085f8211085f8221085f8231085f8241085f8251085f8261085f8271085f8281085f8291085f82a1085f82b1085f82c1085f82d1085f82e10b7d1012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf829024cf61520c0f24600804715e00020caf8000000208bf82802cbf824024ff4163045f20f11069005a8c0f24701cdf81480884701208bf82a0204a802f06efecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0168500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00a4455235e29537008d050235d29937004d020235c29d37040f08e8000218df87310013096f8df10002900f0cd80002005a90a5c002a00f0a3800a185378002b5ed09378002b5dd0d378002b5cd01379002b5bd05379002b5ad09379002b59d0d379002b58d0137a002b57d0537a002b56d0937a002b55d0d37a002b54d0137b002b53d0537b002b52d0937b002b51d0d37b002b53d0137c002b52d0537c002b51d0937c002b50d0d37c002b4fd0137d002b4ed0537d002b4dd0937d002b4cd0d37d002b4bd0137e002b4ad0537e002b49d0937e002b48d0d37e002b47d0137f002b46d0537f002b45d0937f002b44d0d27f002a43d0203060287ff49aaf5fe001303de002303be0033039e0043037e0053035e0063033e0073031e008302fe009302de00a302be00b3029e00c3027e00d3025e00e3023e00021117171e70f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f281dd84ff053020a5419d001444e225e284a7014d041225d288a7010d050225c28ca700cd04f225b280a7108d046225a284a7104d0202259288a7142f0b28200208df873000120daf8041046f23f52c5f28802b2eb510f40f08b80c90700f08e80002005a90a5c002a00f083810a185378002b00f0f7809378002b00f0f780d378002b00f0f7801379002b00f0f7805379002b00f0f7809379002b00f0f780d379002b00f0f780137a002b00f0fa80537a002b00f0fa80937a002b00f0fa80d37a002b00f0fa80137b002b00f0fa80537b002b00f0fa80937b002b00f0fa80d37b002b00f0fa80137c002b00f0fa80537c002b00f0fa80937c002b00f0fa80d37c002b00f0fa80137d002b00f0fa80537d002b00f0fa80937d002b00f0fa80d37d002b00f0fa80137e002b00f0fa80537e002b00f0fa80937e002b00f0fa80d37e002b00f0fa80137f002b00f00b81537f002b00f00981937f002b00f00781d27f002a00f00581203060287ff47baf1be14cf67e21caf21011caf80410002840f01681002005a90a5c002a00f0d9800a185378002b5ed09378002b5dd0d378002b5cd01379002b5bd05379002b5cd09379002b5dd0d379002b5ed0137a002b5fd0537a002b60d0937a002b61d0d37a002b65d0137b002b66d0537b002b67d0937b002b68d0d37b002b69d0137c002b6ad0537c002b6bd0937c002b6cd0d37c002b6dd0137d002b6ed0537d002b6fd0937d002b70d0d37d002b71d0137e002b72d0537e002b73d0937e002b74d0d37e002b75d0137f002b76d0537f002b77d0937f002b78d0d27f002a79d0203060287ff49aafa9e0013073e0023071e003306fe004306de0013084e0053069e0023080e0063065e003307ce0073061e0043078e008305de0053074e0093059e0063070e00a3055e007306ce0002151713de50b304ee0083065e00c304ae0093061e00d3046e00a305de00e3042e00b3059e00f303ee00c3055e010303ae00d3051e0113036e00e304de0123032e00f3049e013302ee0103045e014302ae0113041e0153026e012303de0163022e0133039e017301ee0143035e018301ae0153031e0193016e016302de01a3012e0173029e01b300ee0183025e01c300ae0193021e01d3006e01a301de01e3002e01b3019e01f305f2831d84ff04f020a542dd001444b225e284a7028d020225d288a7024d00020c87024e01c3004e01d3002e01e3000e01f305f2818d84ff041020a5414d001444c225e284a700fd05d288a700cd04f225c28ca7008d043225b280a7104d020225a284a7142f0018100208df8730005a80021cdf80880804618f80100002800f0a18008eb0102507800285ed0907800285dd0d07800285cd0107900285bd0507900285ad09079002859d0d079002858d0107a002857d0507a002856d0907a002855d0d07a002854d0107b002853d0507b002852d0907b002851d0d07b002850d0107c00284fd0507c00284ed0907c00284dd0d07c00284cd0107d00284bd0507d00284ad0907d002849d0d07d002848d0107e002847d0507e002846d0907e002845d0d07e002844d0107f002843d0507f002842d0907f002841d0d07f002840d0203160297ff498af43e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff0770008f8010003d100208df8730003e008eb010000214170d6f8d0704ff0000918f80900002800f0c48008eb09014878002862d08878002862d0c878002862d00879002862d04879002862d08879002862d0c879002862d0087a002862d0487a002862d0887a002862d0c87a002862d0087b002862d0487b002862d0887b002862d0c87b002862d0087c002862d0487c002862d0887c002862d0c87c002862d0087d002862d0487d002862d0887d002862d0c87d002862d0087e002862d0487e002862d0887e002862d0c87e002862d0087f002862d0487f002862d0887f002862d0c87f002862d009f12009b9f1600f7ff496af4ff060095be009f1010958e009f1020955e009f1030952e009f104094fe009f105094ce009f1060949e009f1070946e009f1080943e009f1090940e009f10a093de009f10b093ae009f10c0937e009f10d0934e009f10e0931e009f10f092ee009f110092be009f1110928e009f1120925e009f1130922e009f114091fe009f115091ce009f1160919e009f1170916e009f1180913e009f1190910e009f11a090de009f11b090ae009f11c0907e009f11d0904e009f11e0901e009f11f090a2f4ff0010103961ed364204ff47a7542f2107401eb81035b00b7fbf3f60a2e11d301fb00f3b7fbf3f60a2e0bd301fb05f3b7fbf3f60a2e05d36143b7fbf1f3092be7d800e01946cc46b9f15f0f98bf4ff05f0cccf1000e1eeb090052d0b7fbf1f34cf6cd4203f13004ccf6cc4208f809400a29a1fb025446d3451c42d003fb1173e608b3fbf6f448f21f5204f1300508eb0907c5f2eb127d706429a1fb025835d3851c36d004fb16334fea5814b3fbf4f644f6d35206f13005c1f26202bd70b1f57a7fa1fb022526d303300df1140818d006fb1430aa0941f25974b0fbf2f3cdf2b714a1fb044503f13004fc7003fb120742f2107009f1040981424fea5531aad20de0e1460be009f1010908e009f1020903e0e14601e009f103090df11408039b002108f8091018f80100002800f0a18008eb0102507800285ed0907800285dd0d07800285cd0107900285bd0507900285ad09079002859d0d079002858d0107a002857d0507a002856d0907a002855d0d07a002854d0107b002853d0507b002852d0907b002851d0d07b002850d0107c00284fd0507c00284ed0907c00284dd0d07c00284cd0107d00284bd0507d00284ad0907d002849d0d07d002848d0107e002847d0507e002846d0907e002845d0d07e002844d0107f002843d0507f002842d0907f002841d0d07f002840d0203160297ff498af52e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2913d84ff0750008f801000ed008eb010073225e29427008d020225d29827004d070225c29c27040f09e8000208df87300d3f8d4104ff0000918f80900002800f0c78008eb09035878002862d09878002862d0d878002862d01879002862d05879002862d09879002862d0d879002862d0187a002862d0587a002862d0987a002862d0d87a002862d0187b002862d0587b002862d0987b002862d0d87b002865d0187c002865d0587c002865d0987c002865d0d87c002865d0187d002865d0587d002865d0987d002865d0d87d002865d0187e002865d0587e002865d0987e002865d0d87e002865d0187f002865d0587f002865d0987f002865d0d87f002865d009f12009b9f1600f7ff496af4ff060095ee009f101095be009f1020958e009f1030955e009f1040952e009f105094fe009f106094ce009f1070949e009f1080946e009f1090943e009f10a0940e009f10b093de009f10c093ae009f10d0937e009f10e0934e00021017161e709f10f092ee009f110092be009f1110928e009f1120925e009f1130922e009f114091fe009f115091ce009f1160919e009f1170916e009f1180913e009f1190910e009f11a090de009f11b090ae009f11c0907e009f11d0904e009f11e0901e009f11f0901230a291ed364204ff47a7742f2107503eb83067600b1fbf6f40a2c11d303fb00f6b1fbf6f40a2c0bd303fb07f6b1fbf6f40a2c05d36b43b1fbf3f6092ee7d800e03346ce46b9f15f0f98bf4ff05f0ecef1000842f2107c18eb09004ed0b1fbf3f405aa04f1300502f809504cf6cd42ccf6cc420a2ba3fb025641d3451c3dd004fb1317f608b7fbf6f405a948f21f5204f130054944c5f2eb124d70642ba3fb022530d3821c29d004fb16776c09b7fbf4f6b3f57a7f06f130028a7044f6d352c1f26202a3fb022520d3033016d006fb1470aa0941f25975b0fbf2f4cdf2b715a3fb055604f13005cd7004fb120109f1040963454fea5633aed209e0f14607e009f1010904e009f1020901e009f1030900210df1140808f80910ddf8089018f80100002800f0a18008eb0102507800285ed0907800285dd0d07800285cd0107900285bd0507900285ad09079002859d0d079002858d0107a002857d0507a002856d0907a002855d0d07a002854d0107b002853d0507b002852d0907b002851d0d07b002850d0107c00284fd0507c00284ed0907c00284dd0d07c00284cd0107d00284bd0507d00284ad0907d002849d0d07d002848d0107e002847d0507e002846d0907e002845d0d07e002844d0107f002843d0507f002842d0907f002841d0d07f002840d0203160297ff498af4ae001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f290bd84ff0750008f8010006d008eb010073225e29427040f00f8100208df8730040f23c3cc2f2000cdcf81c104bf67060c2f2130048f668638142c2f2200328d1dcf82810b1f54d2f23d1dcf82c1081421fd14cf6e4240022c2f213044cf639471d1d4ff0ff3eac4213d8216831f0030000f0a683a34228bf0c290cd31e1bb04209d8c90718bf02442044013f00f10404e9d14ff0ff3e44f63420c2f20700d0f8880003f1080149f268638842c2f2270321d149f6e4250022c2f2200546f6b974181d854217d8296831f0030700f0d980ab424ff0ff36039628bf0c290ed35e1bb7420bd8c90705eb070118bf3a44013c01f10405e5d14ff0ff300390dcf8001003f1080081422bd1dcf80c10b1f5333f26d1dcf81010814222d14af2e4230021c2f2270342f6f932aaf10404aaf108004ff0ff3ca34213d81e6836f0030500f04683984228bf0c2e0cd3c71abd4209d8f7072b4418bf2944013a03f10403e9d14ff0ff3c002118f80100002800f0aa8008eb01046078002861d0a078002860d0e07800285fd0207900285ed0607900285dd0a07900285cd0e07900285bd0207a00285ad0607a002859d0a07a002858d0e07a002857d0207b002856d0607b002855d0a07b002854d0e07b002853d0207c002852d0607c002851d0a07c002856d0e07c002855d0207d002854d0607d002853d0a07d002852d0e07d002851d0207e002850d0607e00284fd0a07e00284ed0e07e00284dd0207f00284cd0607f00284bd0a07f00284ad0e07f002849d0203160297ff498af77e000218170f0e6013140e002313ee003313ce004313ae0053138e0063136e0073134e0083132e0093130e00a312ee00b312ce00c312ae00d3128e00e3126e00f3124e0103122e0113120e0854218bf4ff0ff32039236e7123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f292fd84ff0200008f801002ad008eb010366205e29587024d031205d29987020d033205c29d8701cd02f205b29187118d032205a29587114d030205929987110d02f205829d8710cd032205729187208d037205629587204d03d205529987241f0928200208df873001ef10100cdf804c000f090804fea9e214ff0000a18f80a00002800f0898108eb0a046078002800f00b81a078002800f00a81e078002800f009812079002800f008816079002800f00781a079002800f00681e079002800f00581207a002800f00481607a002800f00381a07a002800f00281e07a002800f00181207b002800f00081607b002800f0ff80a07b002800f0fe80e07b002800f0fd80207c002800f0fc80607c002800f0fb80a07c002800f0fc80e07c002800f0fd80207d002800f0fe80607d002800f0ff80a07d002800f00081e07d002800f00181207e002800f00281607e002800f00381a07e002800f00481e07e002800f00581207f002800f00681607f002800f00781a07f002800f00881e07f002800f009810af1200abaf1600f7ff477af4ff0600a01e1039b002118f80100002800f0ba8108eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04e81107c002800f04c81507c002800f04a81907c002800f04881d07c002800f04681107d002800f04481507d002800f04281907d002800f04081d07d002800f03e81107e002800f03c81507e002800f03a81907e002800f03881d07e002800f03681107f002800f03481507f002800f03281907f002800f03081d07f002800f03b81203160297ff479af3de10af1010a74e00af1020a71e00af1030a6ee00af1040a6be00af1050a68e00af1060a65e00af1070a62e00af1080a5fe00af1090a5ce00af10a0a59e00af10b0a56e00af10c0a53e00af10d0a50e00af10e0a4de00af10f0a4ae00af1100a47e00af1110a44e0013101e10af1120a3fe00231fce00af1130a3ae00331f7e00af1140a35e00431f2e00af1150a30e00531ede00af1160a2be00631e8e00af1170a26e00731e3e00af1180a21e00831dee00af1190a1ce00931d9e00af11a0a17e00a31d4e00af11b0a12e00b31cfe00af11c0a0de00c31cae00af11d0a08e00d31c5e00af11e0a03e00e31c0e00af11f0a0122bef5205f1ed364204ff47a7342f2107502eb82077f00b1fbf7f40a2c11d302fb00f7b1fbf7f40a2c0bd302fb03f7b1fbf7f40a2c05d36a43b1fbf2f7092fe7d800e03a46d146baf15f0f98bf4ff05f09c9f1000842f2107e18eb0a004ed0b1fbf2f305af03f1300407f80a404cf6cd47ccf6cc470a2aa2fb075441d3451c3dd003fb1217e308b7fbf3f405a948f21f5604f130055144c5f2eb164d70642aa2fb06c530d3861c29d004fb13736c09b3fbf4f7b2f57a7f07f130068e7044f6d356c1f26206a2fb066520d3033016d007fb1430ab0941f25976b0fbf3f7cdf2b716a2fb066507f13006ce7007fb13010af1040a72454fea5532aed209e0ca4607e00af1010a04e00af1020a01e00af1030a00200df11408dde9029308f80a003ce00f312be0103129e0113127e0123125e0133123e0143121e015311fe016311de017311be0183119e0193117e01a3115e01b3113e01c3111e01d310fe01e310de0ac4218bf4ff0ff329646fff764bca34218bf4ff0ff318c46c4e41f315f2904d84ff03f0008f8010003d100208df8730003e008eb010000214170002118f80100002800f0a18008eb0102507800285ed0907800285dd0d07800285cd0107900285bd0507900285ad09079002859d0d079002858d0107a002857d0507a002856d0907a002855d0d07a002854d0107b002853d0507b002852d0907b002851d0d07b002850d0107c00284fd0507c00284ed0907c00284dd0d07c00284cd0107d00284bd0507d00284ad0907d002849d0d07d002848d0107e002847d0507e002846d0907e002845d0d07e002844d0107f002843d0507f002842d0907f002841d0d07f002840d0203160297ff498af43e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff02f0008f8010003d100208df8730003e008eb010000214170ddf804a0581c00f08f80990a4ff0000c18f80c00002800f0888108eb0c046078002800f00a81a078002800f00981e078002800f008812079002800f007816079002800f00681a079002800f00581e079002800f00481207a002800f00381607a002800f00281a07a002800f00181e07a002800f00081207b002800f0ff80607b002800f0fe80a07b002800f0fd80e07b002800f0fc80207c002800f0fb80607c002800f0fa80a07c002800f0fb80e07c002800f0fc80207d002800f0fd80607d002800f0fe80a07d002800f0ff80e07d002800f00081207e002800f00181607e002800f00281a07e002800f00381e07e002800f00481207f002800f00581607f002800f00681a07f002800f00781e07f002800f008810cf1200cbcf1600f7ff477af4ff0600c00e1002118f80100002800f0ad8108eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04e81107c002800f04c81507c002800f04a81907c002800f04881d07c002800f04681107d002800f04481507d002800f04281907d002800f04081d07d002800f03e81107e002800f03c81507e002800f03a81907e002800f03881d07e002800f03681107f002800f03481507f002800f03281907f002800f03081d07f002800f02e81203160297ff479af30e10cf1010c74e00cf1020c71e00cf1030c6ee00cf1040c6be00cf1050c68e00cf1060c65e00cf1070c62e00cf1080c5fe00cf1090c5ce00cf10a0c59e00cf10b0c56e00cf10c0c53e00cf10d0c50e00cf10e0c4de00cf10f0c4ae00cf1100c47e00cf1110c44e00131f4e00cf1120c3fe00231efe00cf1130c3ae00331eae00cf1140c35e00431e5e00cf1150c30e00531e0e00cf1160c2be00631dbe00cf1170c26e00731d6e00cf1180c21e00831d1e00cf1190c1ce00931cce00cf11a0c17e00a31c7e00cf11b0c12e00b31c2e00cf11c0c0de00c31bde00cf11d0c08e00d31b8e00cf11e0c03e00e31b3e00cf11f0c0124b3f5205f1ed364204ff47a7342f2107704eb84067600b1fbf6f50a2d11d304fb00f6b1fbf6f50a2d0bd304fb03f6b1fbf6f50a2d05d37c43b1fbf4f6092ee7d800e03446e146bcf15f0f98bf4ff05f09c9f1000842f2107e18eb0c004ed0b1fbf4f305aa03f1300702f80c704cf6cd42ccf6cc420a2ca4fb027541d3471c3dd003fb1417eb08b7fbf3f505a948f21f5205f130066144c5f2eb124e70642ca4fb022630d3821c29d005fb13737509b3fbf5f7b4f57a7f07f130028a7044f6d352c1f26202a4fb022620d3033016d007fb1530b20941f25975b0fbf2f3cdf2b715a4fb055603f13005cd7003fb12010cf1040c74454fea5634aed209e0cc4607e00cf1010c04e00cf1020c01e00cf1030c00200df11408ddf8089008f80c002fe00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff03f0008f8010003d100208df8730003e008eb010000214170002118f80100002800f0a18008eb0102507800285ed0907800285dd0d07800285cd0107900285bd0507900285ad09079002859d0d079002858d0107a002857d0507a002856d0907a002855d0d07a002854d0107b002853d0507b002852d0907b002851d0d07b002850d0107c00284fd0507c00284ed0907c00284dd0d07c00284cd0107d00284bd0507d00284ad0907d002849d0d07d002848d0107e002847d0507e002846d0907e002845d0d07e002844d0107f002843d0507f002842d0907f002841d0d07f002840d0203160297ff498af43e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff02f0008f8010003d100208df8730003e008eb0100002141701af1010000f090804fea9a214ff0000918f80900002800f0888108eb09046078002800f00a81a078002800f00981e078002800f008812079002800f007816079002800f00681a079002800f00581e079002800f00481207a002800f00381607a002800f00281a07a002800f00181e07a002800f00081207b002800f0ff80607b002800f0fe80a07b002800f0fd80e07b002800f0fc80207c002800f0fb80607c002800f0fa80a07c002800f0fb80e07c002800f0fc80207d002800f0fd80607d002800f0fe80a07d002800f0ff80e07d002800f00081207e002800f00181607e002800f00281a07e002800f00381e07e002800f00481207f002800f00581607f002800f00681a07f002800f00781e07f002800f0088109f12009b9f1600f7ff477af4ff0600900e1002118f80100002800f0ad8108eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04e81107c002800f04c81507c002800f04a81907c002800f04881d07c002800f04681107d002800f04481507d002800f04281907d002800f04081d07d002800f03e81107e002800f03c81507e002800f03a81907e002800f03881d07e002800f03681107f002800f03481507f002800f03281907f002800f03081d07f002800f02e81203160297ff479af30e109f1010974e009f1020971e009f103096ee009f104096be009f1050968e009f1060965e009f1070962e009f108095fe009f109095ce009f10a0959e009f10b0956e009f10c0953e009f10d0950e009f10e094de009f10f094ae009f1100947e009f1110944e00131f4e009f112093fe00231efe009f113093ae00331eae009f1140935e00431e5e009f1150930e00531e0e009f116092be00631dbe009f1170926e00731d6e009f1180921e00831d1e009f119091ce00931cce009f11a0917e00a31c7e009f11b0912e00b31c2e009f11c090de00c31bde009f11d0908e00d31b8e009f11e0903e00e31b3e009f11f090123baf5205f1ed364204ff47a7742f2107603eb83056d00b1fbf5f40a2c11d303fb00f5b1fbf5f40a2c0bd303fb07f5b1fbf5f40a2c05d37343b1fbf3f5092de7d800e02b46ce46b9f15f0f98bf4ff05f0ecef1000842f2107c18eb09004ed0b1fbf3f405aa04f1300502f809504cf6cd42ccf6cc420a2ba3fb025741d3451c3dd004fb1316ff08b6fbf7f405a948f21f5204f130054944c5f2eb124d70642ba3fb022530d3821c29d004fb17676c09b7fbf4f6b3f57a7f06f130028a7044f6d352c1f26202a3fb022520d3033016d006fb1470aa0941f25975b0fbf2f4cdf2b715a3fb055604f13005cd7004fb120109f1040963454fea5633aed209e0f14607e009f1010904e009f1020901e009f1030900200df1140808f80900ddf808902fe00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff03f0008f8010003d100208df8730003e008eb010000214170002118f80100002800f0a18008eb0102507800285ed0907800285dd0d07800285cd0107900285bd0507900285ad09079002859d0d079002858d0107a002857d0507a002856d0907a002855d0d07a002854d0107b002853d0507b002852d0907b002851d0d07b002850d0107c00284fd0507c00284ed0907c00284dd0d07c00284cd0107d00284bd0507d00284ad0907d002849d0d07d002848d0107e002847d0507e002846d0907e002845d0d07e002844d0107f002843d0507f002842d0907f002841d0d07f002840d0203160297ff498af43e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff06b0008f8010003d100208df8730003e008eb0100002141709df814a0baf1000f10d0022208eb020010f8013c63b118f8023063b143784bb1807804320028f1d1023a04e0002005e0013a00e0013202eb4200400040eae070821c09f5207c00f1010e02f0030822f0030709f5a0724ff0ff3504e001350d2d02f5a07268d005eb8500bef1030f0ceb801405d24ff0ff30b8f1000f2dd1ede70020164603e0874206f1020621d0431cdb09042b03d8337803f0f0033370831cdb09042b03d8737803f00f037370c31cdb09042b03d8737803f0f00373700430c309042bdfd84308e15c01f00f01e154d9e70138b8f1000fc0d0861cf109042908d87108635cc6076ff00f0608bf0f2633406354b8f1010fb0d0c61cf109042909d87108635cc6074ff00f0608bf6ff00f0633406354b8f1020f9fd0061df10904299bd87108c007635c6ff00f0008bf0f201840605491e7baf1000f00f0ab800298022200f281280df1140e40f27d2940f27a2c08e01ef801af0632baf1000f08f1030800f09780aaf17f0010f15f0ff1d345f2e431c0f200010aeb4a00794401eb8000a0f5c0710027d409404605e000bf02370c2f00f52070dcd0cb57042c06d8b3f1ff3f03dc067846f0f0060670042cdeb206d816f0400303d0037843f00f030370b30605d54a4503d8437843f0f0034370f30606d5b2f51f7f03d8437843f00f03437033074fea920305d59e2b03d8857845f0f0058570750705d5624503d8857845f00f058570cd1995f90160042c08d8b6f1ff3f05dc90f8405145f0f00580f84051042cf6b208d816f0400505d090f8405145f00f0580f84051b50607d54a4505d890f8415145f0f00580f84151f50608d5b2f51f7f05d890f8415145f00f0580f84151350707d59e2b05d890f8423143f0f00380f8423173077ff588af62453ff685af90f8423143f00f0380f842317de7ddf80880fdf776ba0020c871fdf74dbd00208871fdf7febe00200872fdf7e9ba0020d872fef76dbd2de9f04141f204054cf65561cef20005ccf2ac51c5f8ac1f4ef6fc51cef200010a6846f2702642f080720a6055f8041cc2f22a0641f0010145f8041c316880460a466ff39602b2f1005f06d108684ef66762ccf2ff0290427dd001f0b3fc002800f0b180014600206ff01702002300bfcc18c8542f338f18b3f50d7f02f12f026070a070e07020716071a071e07120726072a072e07220736073a073e07320746074a074e07420756075a07587f82f0087f830006076a076e07620776077a077e07784f8200084f8210084f8220084f8230084f8240084f8250084f8260084f8270084f8280084f8290084f82a0084f82b0084f82c0084f82d0084f82e00b7d14ef66760ccf2ff000860012081f8ce004ff0ff30a1f8e000a1f8e200a1f8e400a1f8e600a1f8e800a1f8ea00a1f8ec00a1f8ee00a1f8f000a1f8f200a1f8f400a1f8f600a1f8f800a1f8fa00a1f8fc00a1f8fe003160d1f8d800b0bb44f6342245f6e464c2f20702cff6f8741368204617689f420ed117689f420bd117689f4208d116689e4203d1002800f10400efd19e421ad02b6816681068b0420ed11068b0420bd11068b04208d11068b04203d1002c04f10404efd1b04206d02868c01ac1f8d80001e0002030602868c8f80000bde8f0812de9f04381b041f2040846f27027cef20008c2f22a070146d8f8009038680d6801466ff39601b1f1005f06d101684ef66762ccf2ff0291427bd001f0d9fb002800f0878000216ff017020023c618c1542f338418b3f50d7f02f12f027170b170f17031717171b171f17131727172b172f17231737173b173f17331747174b174f17431757175b17584f82f1084f830107176b176f17631777177b177f17786f8201086f8211086f8221086f8231086f8241086f8251086f8261086f8271086f8281086f8291086f82a1086f82b1086f82c1086f82d1086f82e10b7d14ef66761ccf2ff010160012180f8ce104ff0ff31a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe103860d0f8d81081b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce000203860fa2048e044f6342245f6e466c2f20702cff6f8761368314600bf17689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422ad0d8f80030176800bf1168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94214d0d8f8001044f6d357ca1ac1f26207a2fb0776c0f8d820b00901284ff0010088bfb009994208bffa2000e0fa20a9eb0501b1fbf0f001b0bde8f08300bf2de9f04f85b0044618b1a069002840f0928046f27025c2f22a05286801466ff39601b1f1005f06d101684ef66762ccf2ff0291427bd001f0ddfa002876d000216ff01702002300bfc718c1542f338618b3f50d7f02f12f027970b970f97039717971b971f97139727972b972f97239737973b973f97339747974b974f97439757975b97586f82f1086f830107976b976f97639777977b977f97787f8201087f8211087f8221087f8231087f8241087f8251087f8261087f8271087f8281087f8291087f82a1087f82b1087f82c1087f82d1087f82e10b7d14ef66761ccf2ff010160012180f8ce104ff0ff31a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe102860002c18bf002807d14af26950c0f2450005b0bde8f04f0047d4f80cc0bcf1000ff2d0d4f820a0baf1000fedd0d4f844e000f104094ff0ff38002700264ff0ff3105e06037c02f06f1060600f0eb81c3195a68002a50d01a69013205d01a6942453cbfd3f8108031465a69002a06f1010241d01d6a013505d01d6a45453cbfd3f8208011465a6a002a06f1020233d01d6b013505d01d6b45453cbfd3f8308011465a6b002a06f1030225d01d6c013505d01d6c45453cbfd3f8408011465a6c002a06f1040217d01d6d013505d01d6d45453cbfd3f8508011465a6d002a06f1050209d01d6e0135acd01d6e45453cbfd3f860801146a5e71646002e019600f1a281f24500f29f810aeb0c0102910eeb0c0100220491cdf80c9000bf130159f80330a34224d109eb02135f68674507d30499b94204d304999e68cd1bae4208d9df68013714d00021196059609960d9600ee00299b9420bd9df68013708d00021196059609960d960012180f8df1000bf01320c2ad2d1049e04e000bfbef1000f00f0ba80b6eb0c02c0f0b880924500f2b5803546a6eb0a0b00224ff0000e02e06032c02aead000eb0208d8f80430a34217d1d8f80830a3b1d8f80c90002709eb03018b454ff0000138bf01219d4288bf01279e4238bf33460f421cbf4ff0010e1e46d8f81410a14216d1d8f818309bb1d8f81c10002719448b454ff0000138bf01219d4288bf01279e4238bf33460f421cbf4ff0010e1e46d8f82410a14216d1d8f828309bb1d8f82c10002719448b454ff0000138bf01219d4288bf01279e4238bf33460f421cbf4ff0010e1e46d8f83410a14216d1d8f838309bb1d8f83c10002719448b454ff0000138bf01219d4288bf01279e4238bf33460f421cbf4ff0010e1e46d8f84410a14216d1d8f848309bb1d8f84c10002719448b454ff0000138bf01219d4288bf01279e4238bf33460f421cbf4ff0010e1e46d8f85410a1427ff46eafd8f85830002b3ff469afd8f85c10002719448b454ff0000138bf01219d4288bf01279e4238bf33460f421cbf4ff0010e1e4653e7554562d1ddf80c904ff0ff36002200274ff0ff3505e000bf6032c02a07f1060744d083185968a14207d11969013104d01969a9423cbf1d693e465969a14207d1196a013104d0196aa9423cbf1d6a7e1c596aa14207d1196b013104d0196ba9423cbf1d6bbe1c596ba14207d1196c013104d0196ca9423cbf1d6cfe1c596ca14207d1196d013104d0196da9423cbf1d6d3e1d596da142bfd1196e0131bcd0196ea9423cbf1d6e7e1db6e700bf002e4ed409eb06113301002249f803204a608a60ca60012180f8df10ace6dc4543d21af00303524615d0aaf101021cf80210012b0bf802100dd0aaf102021cf80210022b0bf8021005d0aaf103021cf802100bf80210baf1040f5dd3abf10203acf10406b118cf789d186f708f789f54497805f8011cb15c043a05f8021cf1d14ae000290dd409eb01120b01002549f8035055609560d56001220e4680f8df2059e6012180f8df10dde535d90af0030e5fea9a024ff0000317d0a3eb82080bf101060cf101026ff003055119cf787319df700f7904351f714f795f71897908eb050704379971f0d12b1dbef1000f13d01cf80310bef1010f0bf803100cd0591c1cf80120bef1020f0bf8012004d0991c1cf801200bf80120039b019a03eb021112019c50d0f8c420c1f804b0531cc1f808a024bf01230022c0f8c430ca6092e538464146fff7f4bc2de9f047044648b1a06938b14cf2af53c0f24d032046bde8f047184746f27028c2f22a08d8f8005028466ff39600b0f1005f07d128684ef66763ccf2ff03984200f080800f46164600f0c8ff0546002800f00e8100206ff017010022ab18a8542f326f18b2f50d7f01f12f0158709870d870187158719871d871187258729872d872187358739873d873187458749874d87418755875987587f82f0087f8300058769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e00b7d14ef66760ccf2ff002860012085f8ce004ff0ff30a5f8e000a5f8e200a5f8e400a5f8e600a5f8e800a5f8ea00a5f8ec00a5f8ee00a5f8f000a5f8f200a5f8f400a5f8f600a5f8f800a5f8fa00a5f8fc00a5f8fe00c8f8005005f1040a4ff000094ff0ff38002015e0002948bfbde8f0870aeb01170e017968ba682046c7f80c8000f088f84af80690c7f80490c7f80890c7f80c90daf80010a14205d04ff0ff314ff0ff3205e000bf29690131f6d02a6900216b69a34204d12b6a93423cbf2a6a01216b6aa34204d12b6b93423cbf2a6b02216b6ba34204d12b6c93423cbf2a6c03216b6ca34204d12b6d93423cbf2a6d04216b6da34204d12b6e93423cbf2a6e05216b6ea34204d12b6f93423cbf2a6f06216b6fa34206d1d5f8803093423cbfd5f880200721d5f88430a34206d1d5f8903093423cbfd5f890200821d5f89430a34206d1d5f8a03093423cbfd5f8a0200921d5f8a430a34206d1d5f8b03093423cbfd5f8b0200a21d5f8b430a3427ff47bafd5f8c0309342bff476afd5f8c0000b2175e7204639463246c8f80050bde8f047ffe72de9f047a4b0804640f6bb4046f27027c0f24e0091468a46c2f22a0780473c68002021466ff39601b1f1005f02908df80c0006d120684ef66761ccf2ff0188427bd000f08bfe0446002875d000206ff017010022a318a0542f326618b2f50d7f01f12f0158709870d870187158719871d871187258729872d872187358739873d873187458749874d87418755875987586f82f0086f8300058769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e00b7d14ef66760ccf2ff002060012084f8ce004ff0ff30a4f8e000a4f8e200a4f8e400a4f8e600a4f8e800a4f8ea00a4f8ec00a4f8ee00a4f8f000a4f8f200a4f8f400a4f8f600a4f8f800a4f8fa00a4f8fc00a4f8fe003c60baf1000f4ff0010618bfb9f1000f23d101af3846fff723f902ad404651464a460123009500f01bfe05463846fff701fa002e064608d19df80c0028b943f61f00c0f247004c308047002c18bfc4f8d060284624b0bde8f0879af8001001f07f00a0f1100212f1040f09d80b2807d001f07e01082903d0032801d00628c8d13cb143f61f00c0f24700804794f8280210b14ff0ff35dce70026bae746f27020c2f22a00016808466ff39600b0f1005f16d108684ef66762ccf2ff0290420fd1d1f8fc0160b144f63422c2f207021268801a0028cebf01200020c1f8fc0170470020704746f27020c2f22a00026810466ff39600b0f1005f28d110684ef66761ccf2ff01884221d1d2f82c02f0b144f63421c2f207010968401a0028c4bf01207047d2f8301200200029c2f82c0282f82a0208bf704710b5c2f8300204460846fcf736f82046bde81040704700207047f0b101684ef66762ccf2ff02914218bf704790f8061299b110b544f62f344ff0000cc0f24604864680f806c20120002100220023aef804c2a446bde810406047704700bff0b581b046f27027c2f22a073c68054621466ff39601b1f1005f06d120684ef66761ccf2ff0188427dd000f031fd002800f0a280044600206ff01701002200bfa318a0542f326618b2f50d7f01f12f0158709870d870187158719871d871187258729872d872187358739873d873187458749874d87418755875987586f82f0086f8300058769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e00b7d14ef66760ccf2ff002060012084f8ce004ff0ff30a4f8e000a4f8e200a4f8e400a4f8e600a4f8e800a4f8ea00a4f8ec00a4f8ee00a4f8f000a4f8f200a4f8f400a4f8f600a4f8f800a4f8fa00a4f8fc00a4f8fe003c6000230820032184f81b322a2384f8180284f81912102184f81c32042384f81e02012045f61b4c04f5067284f81a1284f81d3284f81f0284f82012002d18bf0125c0f2470c0a2384f8215201b0bde8f04060470020386001b0f0bd2de9f04146f27027c2f22a073f684ff000083c466ff39604b4f1005f07d13e684ef66765ccf2ff05ae4208bfb84601280fd11c4615460f460646fff7a5fe2346044639462a463046002c18bfb8f1000f06d144f62f3cc0f2460cbde8f041604798f8060270b1d8f800020021002888f80612a8f804123ed049f2d941c0f24401884738e0b8f804020121013080b2012898bf0846a8f80402d8f8000249f2d944c0f2440488f8061268b94ff68350cff6ff70a4f594777844002142460023b847c8f8000240b1a047d8f80002a4f140024ff4c8719047c8b198f80602002808bfbde8f081d8f80002002188f80612a8f8041200b1a04744f62f3cc0f2460c0120002100220023bde8f04160474af26950c0f245008047012830d1082088f80802032088f80902102088f80a02002088f80b02b22088f80c02062088f80d0288f80e024620b8f8041288f80f02432088f8100201200f0a45f6153c08f5027288f8110288f8120288f81312c0f2470c09210d2388f81472bde8f0416047bde8f08170b50446d0f8000249f2b1360d46c0f2440668b94ff69d40cff6ff707844002122460023b0470028c4f8000208bf70bd06f594718847d4f8000206f1e8022946bde87040104700bf2de9f04f85b04768002f00f09d828368002b00f0998207eb03084ff00009002500240fe0dde90121ddf810c0c6f8009020460ceb0a03002543451f464ff0000480f08282bb461bf9013b03f07f06ae40002b44ea06040fd404f00703012b15dc03b3012b40f07082a8eb0b03082bc0f06b8207f10903dee7c34580f06582182d05f107055f46ddd95ee200bf022b3ad0052b40f05982a8eb0b03042bc0f054827b1dc8e7c34580f04f8297f90130002b07f10203bfd5434580f0468297f90230b3f1ff3f07f10303b5dc434580f03c8297f90330b3f1ff3f07f10403abdc434580f0328297f90430b3f1ff3f07f10503a1dc434580f0288297f90530002b00f12382bb1d97e7c34580f01e8297f9013007f1020c002b03f07f0a3ed5c44580f0138297f90250e34605f07f0307f1030cb5f1ff3f4aeac31601ddb2462de0c44580f0028297f9033007f1040e03f07f05b3f1ff3f46ea853a02dde346f4461ce0c64580f0f18197f9043007f1050c03f07f06b3f1ff3f4aea465a01ddf3460ce0c44580f0e18197f90530002b00f1dc8106374aea037ae346bc46a8eb0c039a4500f2d28124f00703b3f54a7f7ff441afbaf1060ffff43daf9cf80030462b7ff438af9bf80230432b7ff433af9bf80330012b7ff42eaf46f27026c2f22a063568cde903ac2b466ff39603b3f1005f07d12b684ef66767ccf2ff07bb4200f0a480cde90121044600f09ffa00283ff40aafdde90121ddf810c0054600236ff017072046b646ec1805f803902f33ee19b3f50d7f07f12f0784f8019084f8029084f8039084f8049084f8059084f8069084f8079084f8089084f8099084f80a9084f80b9084f80c9084f80d9084f80e9084f80f9084f8109084f8119084f8129084f8139084f8149084f8159084f8169086f82f9086f8309084f8199084f81a9084f81b9084f81c9084f81d9084f81e9084f81f9084f8209084f8219084f8229084f8239084f8249084f8259084f8269084f8279084f8289084f8299084f82a9084f82b9084f82c9084f82d9084f82e9099d14ef66763ccf2ff032b60012385f8ce304ff0ff33a5f8e030a5f8e230a5f8e430a5f8e630a5f8e830a5f8ea30a5f8ec30a5f8ee30a5f8f030a5f8f230a5f8f430a5f8f630a5f8f830a5f8fa30a5f8fc30a5f8fe30cef800509bf804309bf805709bf80660032b47ea062733dd052b56dcddf80ca0042b00f08680052b7ff461aed5f82c323bb144f63427c2f207073f68db1b002b11dcd5f8303285f82a9263b183461846c5f8309217460c46fbf79afcddf810c021463a46584644f63423c2f207031b6845f69077c0f201073b44c5f82c3236e6ddf80ca0012b47d0022b00f09680032b7ff42dae029014468b46fff7effbddf810c00346029859462246002b3ff41fae95f80632002b3ff41aae284641f28831a5f80472fff7d6fd5ae0ddf80ca0062b5cd0072b7ff40bae83464ef68f30c0f2490017460c4680470346ddf810c05e1eddf80ca021463a465846012e3ff6f7ad981eb0fa80f04009fff734fcddf810c021463a465846eae544f63423c2f207031b6845f69077c0f201073b44c5f8fc31dde5029014468b46fff7a0fbddf810c00346029859462246002b3ff4d0ad95f80632002b3ff4cbadb5f80432bb427ff4c6add5f8003285f80692002ba5f804923ff4bdad49f2d9411846c0f244018847ddf810c0029859462246b0e5d5f83032c5f82c92002b85f82a923ff4a8ad06461846c5f8309217460c46fbf7f1fbddf810c021463a46304699e595f80632c5f8fc91002b3ff493ad0446d5f800028b46174685f80692a5f8049220b149f2d941c0f24401884744f62f360120002100220023c0f24606b047dde903ac20463a46594674e540f22113c0f2490305b0bde8f04f18477fb50646002806d1fff72efb002807d19de80f00064641f6d77cc0f24e0c60477fbd00bf092940f08280f0b5a22602eb030cd65406268cf8016043f60a24c0f200044ff0000e7c4414f80e600ceb0e05002eee7065d004eb0e0677782f71a7b3b7786f71a7b3f778af71a7b33779ef71a7b377792f72a7b3b7796f72a7b3f779af72a7b3377aef72a7b3777a2f73a7b3b77a6f73a7b3f77aaf73a7b3377bef73a7b3777b2f74a7b3b77b6f74a7b3f67bae74a6b30ef1100ebef1800fc4d100278cf882704ff0800e2be00ef1010e28e00ef1020e25e00ef1030e22e00ef1040e1fe00ef1050e1ce00ef1060e19e00ef1070e16e00ef1080e13e00ef1090e10e00ef10a0e0de00ef10b0e0ae00ef10c0e07e00ef10d0e04e00ef10e0e01e00ef10f0e734403338cf802e0bde8f04045f6153cc0f2470c6047b0b582b00c460546fff7b6faa0b14df63040c2f23400007804280bd14af26b140025c0f24d04002000210022092301950095a04702b0b0bd4af69d62c0f246022846214602b0bde8b0401047f0b583b04ff6e70714460d460646c0f24507fff78bfaf8b14df63040c2f234000078042818d144f2d040c2f207000068b84788b10068e0280ed14af26b160027c0f24d060020002100220a2301970097b047012003b0f0bd07f1160330462946224603b0bde8f040184700bfb0b546f27424c2f22a04206846f23f51c5f28801b1eb500f44f6d3414cf67e25c0f247014ff40d70caf2101518bf25608847002804bf691c2160b0bd2de9f04f95b0002918bf002a04d115b0bde8f04f01f032b80e78ddf878b006f07f070a2f00f3b880052f00f0e180072f00f057820a2f40f0b880022ac0f0658746f27024c2f22a04206802466ff39602b2f1005f06d102684ef66763ccf2ff039a427dd00d46fff7adff002800f0858600216ff017020023c718c1542f338618b3f50d7f02f12f027970b970f97039717971b971f97139727972b972f97239737973b973f97339747974b974f97439757975b97586f82f1086f830107976b976f97639777977b977f97787f8201087f8211087f8221087f8231087f8241087f8251087f8261087f8271087f8281087f8291087f82a1087f82b1087f82c1087f82d1087f82e10b7d14ef66761ccf2ff010160012180f8ce104ff0ff31a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe10294620604978012900f04386002940f0ce86002480f807424af26950c0f24500804701462046012940f0ce8645f26d60c0f2540015b0bde8f04f00470b2f00f0dd800c2f00f07982422f3ff432afa7f10d05022d00f217848568002d00f0a786b0f84060b0f842406643b6f5163fc0f09e8601310e2fa2f101029846064628465b4600f08e860d2f40f08e8600f0c2ff00f08cbe46f27028c2f22a08022a2cbf91f801904ff0ff09d8f8004020466ff39600b0f1005f07d120684ef66763ccf2ff03984200f0838016460f46fff7ccfe002800f02b84044600206ff017010023324600bfe618e0542f336518b3f50d7f01f12f017070b070f07030717071b071f07130727072b072f07230737073b073f07330747074b074f07430757075b07585f82f0085f830007076b076f07630777077b077f07786f8200086f8210086f8220086f8230086f8240086f8250086f8260086f8270086f8280086f8290086f82a0086f82b0086f82c0086f82d0086f82e00b7d14ef66760ccf2ff002060012084f8ce004ff0ff303946a4f8e000a4f8e200a4f8e400a4f8e600a4f8e800a4f8ea00a4f8ec00a4f8ee00a4f8f000a4f8f200a4f8f400a4f8f600a4f8f800a4f8fa00a4f8fc00a4f8fe00c8f8004094f8080170b1d4f8040140b149f2d943c0f244030e46174698473a463146002084f808010123032a80f0a183ade346f27020c2f22a00046820466ff39600b0f1005f40f0d98520684ef66761ccf2ff0188424ff0000040f0d085c4f82c0284f82a0284f82802d4f83012c4f8240284f8290221b1c4f830020846fbf7d4f8d4f8040149f2d946c0f244060025a4f8085150b1b047d4f8040106f166018847002804bf0020c4f8040142f6c520c0f25000804794f8070284f8075258b14af26950c0f245008047012804d145f26d60c0f254008047d4f8001294f806520020c4f8fc0184f80602a4f8040271b107460846b047d4f8000206f16601884701463846002904bf0021c4f800120121002d6060a060e06020616061a061e06120626062a062e06220636063a063e06320646064a064e06420656065a065e06520666066a066e06620676067a067e067c4f88000c4f88400c4f88800c4f88c00c4f89000c4f89400c4f89800c4f89c00c4f8a000c4f8a400c4f8a800c4f8ac00c4f8b000c4f8b400c4f8b800c4f8bc00c4f8c000c4f8c40084f8ce1000f02f8544f62f37c0f2460701200021002200230024b847204615b0bde8f08f46f27028c2f22a08d8f8003018466ff39600b0f1005f07d118684ef66767ccf2ff07b84200f0818015460e46fff759fd0346002875d000206ff0170100229f1898542f325c18b2f50d7f01f12f017870b870f87038717871b871f87138727872b872f87238737873b873f87338747874b874f87438757875b87584f82f0084f830007876b876f87638777877b877f87787f8200087f8210087f8220087f8230087f8240087f8250087f8260087f8270087f8280087f8290087f82a0087f82b0087f82c0087f82d0087f82e00b7d14ef66760ccf2ff001860012083f8ce004ff0ff30a3f8e000a3f8e200a3f8e400a3f8e600a3f8e800a3f8ea00a3f8ec00a3f8ee00a3f8f000a3f8f200a3f8f400a3f8f600a3f8f800a3f8fa00a3f8fc00a3f8fe0031462a46c8f80030022a2cbf4978ff21002b00f0848402294ff0000000f0a683012900f09f83002940f07a844cf67e21caf21011c3f8dc00c8f804104ff0ff31a3f8cc00c3f8c800a3f8e010a3f8e210a3f8e410a3f8e610a3f8e810a3f8ea10a3f8ec10a3f8ee10a3f8f010a3f8f210a3f8f410a3f8f610a3f8f810a3f8fa10a3f8fc10a3f8fe1083f8000115b0bde8f08fb2f1010900f0428401f1010b0023002200bfa9eb03000428c0f02c840beb0306b078f7781c1d40ea0727a9eb040087424ff0ff3000f22b841bf80330767843ea06233b44b3f5803f00f22184002f07eb040318bf01224b45dbd3002a00f016848846fef7c4fe002800f0048446f27027c2f22a073c6820466ff39600b0f1005f06d120684ef66761ccf2ff0188427cd0fff74dfc002800f06d84044600206ff017010022a318a0542f326618b2f50d7f01f12f0158709870d870187158719871d871187258729872d872187358739873d873187458749874d87418755875987586f82f0086f8300058769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e00b7d14ef66760ccf2ff002060012084f8ce004ff0ff30a4f8e000a4f8e200a4f8e400a4f8e600a4f8e800a4f8ea00a4f8ec00a4f8ee00a4f8f000a4f8f200a4f8f400a4f8f600a4f8f800a4f8fa00a4f8fc00a4f8fe003c60d4f8300200286fd14ff48030faf76cfe002800f06a8300210022831881544032b2f5803f59709970d970197159719971d971197259729972d972197359739973d973197459749974d974197559759975d975197659769976d976197759779977d97783f8201083f8211083f8221083f8231083f8241083f8251083f8261083f8271083f8281083f8291083f82a1083f82b1083f82c1083f82d1083f82e1083f82f1083f8301083f8311083f8321083f8331083f8341083f8351083f8361083f8371083f8381083f8391083f83a1083f83b1083f83c1083f83d1083f83e1083f83f109ad1c4f830020021cdf80c8006e007eb0a0149454ff0000080f003830beb01039878da7801f1040a50ea0227efd012025b7802441bf80160013a1d02032a00f0030e04a8e0c001d2002224e00398a7eb0e030844a919002200bf8618d4f830c296f805800ceb010707f80280d4f83072b5790f4417447d70d4f83072f5790f441744bd70d4f83072367a0f44174404329342fe70e1d1069fdde90465bef1000fb5d0d4f830120beb0a00835c31442944bef1010f8b54aad0d4f83032511c10f801c033442b44bef1020f03f801c09ed0911cd4f83022405c32442a44505496e7092f00f08e80082f40f016810746002b4ff0ff3000f095824af28973cff6fd73d61802339e42c0f08c8291f801803846b8f1000f00f081820226b71c974200f273828b198e5d5b7856ea032300f06c82de19964200f26882cc5d04f07f0cacf1100515f1030f0ad804f07b05092d06d0bcf1030f03d0bcf1060f40f0558214460d4639441a4600230746cdf800b0fff7c6fa002840f04882b8f10108294622463846cad149e20023c8f80030002439463246032a0dd3b9f1000f0ad18878082800f23e8242f6c521c0f250019631884736e2052a1bd3b9f1010f18d18d78681e06284ff0000000f22c82cb78032b00f228820a79002a00f0248242f6c520c0f2500000f59a7728461946b84718e2b9f1020f40f0448142f6c520c0f2500080470ee237064ff0210658bf1126b242c0f0fb81002f01f101050ed54af26952c0f245020f4604461e469047334601462046022918bf07f1110584462879697950ea01264ff0ff301ebfa979ea7951ea022900f0e781297b6a7b41ea02218e4240f0e081a97bea7b41ea0221894540f0d9812878697840ea01217018b0f5207f4ff0ff3000f2ce81aa78ec7842ea042209eb0204b4f5f07f00f2c4812c7a6f7a44ea072806eb0807b7f5207f00f2ba81af7aed7a47ea052509eb0507b7f5f07f00f2b081dcf80800002800f09e81bcf84070bcf842406743b7f5163fc0f095819a4643466446cde90169009501f048ffdbf800000f280ad80bebc0010130a1f806800d814e81a1f80c90cbf80000baf1000f00f08681204680e1fd1e18bf0125bc1f18bf0124032afff4f1a925407ff4eea900254c1ca2f1010c8df81c508df81d508df820508df824508df825508df826508df827508df828508df829508df82a508df82b508df82c508df82d508df82e508df82f508df830508df831508df832508df833508df834508df835508df836508df837508df838508df839508df83a508df83b508df848508df849508df84a508df84b508df84c508df84d508df84e508df84f508df850508df851508df852508df85350cde9074c4af61b04cff6ff747c440f944af65b04cff6ff747c44032f1094119500f01881062f40f005810646002000211d4602f0c4f8b168002900f0fb80b6f84020b6f842005043b0f5163fc0f0f28007a84ff4a0724ff4f07302f0c4f9002800f0e880dbf800000f280dd80bebc0010022ca800a814ff420724a814ff4f07201308a81cbf80000002d304640f0db80dce0012083f8ce00d8e083f8ce0015b0bde8f08f00202060c4e0072a52d3b9f1030f4fd18878cb780a7940ea03204d798e7901284ff0010498bf204644f62061884228bf084642f6c521c0f25001642a28bf642201f5e2731146984744f20450c2f207000068002800f0a88045ea0621012988bf0c4649f2d941c0f24401a1f140022146904799e00121044680f807124af26950c0f245008047012840f08e8045f26d60c0f2540088388047014600284ff0000000f0838084f80702084615b0bde8f08f032a4ff0000078d3a9f10407b7fa87f77f093b4071d0d01e4cf6cd42ccf6cc428b78a0fb0202b3eb920f88bf9308302b184628bf3020002b00f0f88000eb800303f0030c03f4fe770023ce18f578e21882f80a513579043382f80b5175799f4282f80c51b67982f80d61eed1bcf1000f10d01944ca782344bcf1010f83f80a2108d00a79bcf1020f83f80b211cbf497983f80c1184f80801d4f8040149f2d945c0f24405002184f8091170b94af27f60cff6ff70a5f59477784422460023b8470028c4f8040100f0b780a5f140020121904711e000f099fc01e001f0aaf820b14ff0ff3015b0bde8f08fb8f1000f03d03046594601f0e9fc002015b0bde8f08f370670d4082aebd381464ff0070c4ff0050e0127cc1994f802a04ff0ff30baf1000fead0e678002ee7d011f807804fea8a0505eb8807b7f5207fded86478760006eb4407b7f5f07fd7d801eb0e000395cdf81890894611f80e104078674641ea002081b20120cde9043201f07eff0028c2d1069988680028b2d0b1f84020b1f842105143b1f5163faad309eb0701079105994fea4a02c91b089104eb840100ebc11000eb480107a8334602f072f8002896d0dbf800000f280cd84fea88010bebc003d9800399620001301a8159819e81cbf800000498002806987ff48aaf8be7002038607ce70c2afff47aafcf78ce79b7427ff475af0f790e7ab7427ff470af4af26954c0f245040d46164680461f46a0473b46052732462946c146022808bf01274ff00b0c4ff0090e73e70020a4f8080115b0bde8f08f42f6c5200025c0f2500084f808518047284615b0bde8f08f00bff0b583b046f27023c2f22a031f683b466ff39603b3f1005f09d13b684ef66766ccf2ff06b34204bf002387f82a32002958d0362a56d30b78422b53d14b784d2b50d10b7f4f7f43ea0723042b4ad18b7dcf7d0e7e43ea072343ea06434f7e8e7ccd7c53ea076346ea05270e7d4d7d47ea064747ea05641d4648bf5d42b0f84070002b4ff00003c8bf0123bc422ad1b0f84270bd4226d18f7ace7a91f80cc047ea06274e7b47ea0c4747ea0667974219d2621c032606eb5202866822f0030cca19cde901c30746304621462346009502f0fff93846314622462b4602f093fa002003b0f0bd4cf2af53c0f24d0303b0bde8f0401847072a1cbf4ff0ff3070472de9f04f8db00e4683461d46fef77df9002800f0228346f27028c2f22a08d8f8000001466ff39601b1f1005f06d101684ef66762ccf2ff0291427cd0fef705ff002800f0078300216ff017020023c718c1542f338418b3f50d7f02f12f027970b970f97039717971b971f97139727972b972f97239737973b973f97339747974b974f97439757975b97584f82f1084f830107976b976f97639777977b977f97787f8201087f8211087f8221087f8231087f8241087f8251087f8261087f8271087f8281087f8291087f82a1087f82b1087f82c1087f82d1087f82e10b7d14ef66761ccf2ff010160012180f8ce104ff0ff31a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe10c8f800003178727841ea02274ff6ff718f424ff0ff3200f08c82d0f830121046002900f08382cb5d4ff0ff30002b00f07d8239444a78002a00f0788202fb03f001f1020a4ff0000c4ff000010195009280b34ff6fe794ff6fd7800bfcd194d4500f062828e4651440a78140904d00125a0421bd258e200bf454500f055824c781cb10225a04211d24ee24ff6fb72aa1a042ac0f049828a78c97852ea012400f043820425a042c0f03f8200bf001b05eb0e01d3d10691b078f178b77940ea0129b8060f213279757907f00f0601eae07000fb06f189204143c90a8df824100e2142ea0525069a17f0200f08bf4ff00f0c08bf012106fb01f100fb01f14fead1218df825104ff00d0108bf022106fb01f100fb01f14fead1218df826104ff00c0108bf032106fb01f100fb01f14fead1218df827104ff00b0108bf042106fb01f100fb01f14fead1218df828104ff00a0108bf052106fb01f100fb01f14fead1218df829104ff0090108bf062106fb01f100fb01f14fead1218df82a104ff0080108bf072106fb01f100fb01f14fead1218df82b104ff0070108bf082106fb01f100fb01f14fead1218df82c104ff0060108bf092106fb01f100fb01f14fead1218df82d104ff0050108bf0a2106fb01f100fb01f14fead1218df82e104ff0040108bf0b2106fb01f100fb01f14fead1218df82f104ff0030108bf0c2106fb01f100fb01f14fead1218df830104ff0020108bf0d2106fb01f100fb01f14fead1218df831104ff0010108bf0e2171434143c90a8df832100cfb06f148434ff0010ec00a002a8df8330000f0778107f010008eea1010002604904ff00008cdf814a009e000bfdde90706069a0644ddf814a0964280f062811af806000896040901d0012113e0971b012f00f057810aeb06014c780cb1022109e0042fc0f04e818a78c97852ea012400f048810421079110f00f0109a810f801a018bf012104984fea0a1e40ea010c14f00300474600f08080b8fbf3f100224f19b7f1ff3fc8bf012202ea0c02012a1dd101fb13814944ca09042a17d87a090e2a14d807eb87020beb82124f08d65dc9076ff00f01029608bf0f210390504608bf7046029e314001430398d155012808f1010751d0b7fbf3f100264a19b2f1ff3fc8bf012606ea0c06012e1ad101fb13714944ce09042e14d856090e2e11d802eb82020beb82124e08c9070390905d6ff00f01574604bf0f2177460140039839439155022808f1020726d0b7fbf3f100224e19b6f1ff3fc8bf012202ea0c02012a18d101fb13714944ca09042a12d872090e2a0fd806eb86020beb82124e08c907905d6ff00f01574604bf0f21774601403943915508f10307611e0329a04405d244e700bf0437b8453ff440afb7fbf3f600227119b1f1ff3fc8bf012202ea0c02012a19d106fb137202eb0906f209042a12d84a090e2a0fd801eb81010beb811172088c5cf6076ff00f06504604bf0f2670463440204388547e1cb6fbf3f200205119b1f1ff3fc8bf012000ea0c00012819d102fb136000eb0906f009042812d848090e280fd801eb81000beb80107108425cf4076ff00f04564604bf0f247646224032434254be1cb6fbf3f200205119b1f1ff3fc8bf012000ea0c00012819d102fb136000eb0906f009042812d848090e280fd801eb81000beb80107108425cf4076ff00f04564604bf0f247646224032434254fe1cb6fbf3f200205119b1f1ff3fc8bf012000ea0c0001287ff47aaf02fb136000eb0906f00904283ff672af48090e283ff66eaf01eb81000beb80107108425cf4076ff00f04564604bf0f2476462240324342545ce70020c8f800004ff0ff300db0bde8f08f10460db0bde8f08f009909eb03002944b0f5207fa8bf4ff42070b1f5f07fa8bf4ff4f071019f484588bfa94203d800200db0bde8f08f3a680f2af8d8a0eb090007ebc203491b5881501ca3f806901d8199813860ebe7082a3cbf4ff0ff3070472de9f04f93b015460c4681469846fdf71afe002800f0ea8194f807b00bf1080085424ff0ff3040f0e3812178627841ea02254ff640718d4200f2da8146f2702ac2f22a0adaf80000a1790d9101466ff39601b1f1005f06d101684ef66762ccf2ff0291427dd0fef78efb002800f0bb8100216ff01702002300bfc618c1542f338718b3f50d7f02f12f027170b170f17031717171b171f17131727172b172f17231737173b173f17331747174b174f17431757175b17587f82f1087f830107176b176f17631777177b177f17786f8201086f8211086f8221086f8231086f8241086f8251086f8261086f8271086f8281086f8291086f82a1086f82b1086f82c1086f82d1086f82e10b7d14ef66761ccf2ff010160012180f8ce104ff0ff31a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe10caf80000d0f830e2bef1000f00f03f81a1780d9b0e9121799f060f260a9103f00f0106eae77707fb01f64ff0890c06fb0cf6f60a8df83c600e2694f803a0627913f0200f08bf012601fb06f606fb0cf64fead6268df83d604ff00d0608bf022601fb06f606fb0cf64fead6268df83e604ff00c0608bf032601fb06f606fb0cf64fead6268df83f604ff00b0608bf042601fb06f606fb0cf64fead6268df840604ff00a0608bf052601fb06f606fb0cf64fead6268df841604ff0090608bf062601fb06f606fb0cf64fead6268df842604ff0080608bf072601fb06f606fb0cf64fead6268df843604ff0070608bf082601fb06f606fb0cf64fead6268df844604ff0060608bf092601fb06f606fb0cf64fead6268df845604ff0050608bf0a2601fb06f606fb0cf64fead6268df846604ff0040608bf0b2601fb06f606fb0cf64fead6268df847604ff0030608bf0c2601fb06f606fb0cf64fead6268df848604ff0020608bf0d2601fb06f606fb0cf64fead6268df849604ff0010608bf0e2601fb06f606fb0cf64fead6268df84a604ff0000608bf0f26714301fb0cf10023c90abbf1000fcdf830b08df84b1000f082820e9b0eeb050143ea0a2b0a9bcdf82880ddf83080099104f1080143ea02224ff0000c4ff6fe7a0e920b9104e00cf1010c0b99c44557d011f80c104a1e1f2af5d34ab2202a4adb099a02eb410111f8402c11f83f1c42ea01274ff6ff718f423dd01ef807104ff0ff33002900f04b820eeb07025478002c00f04582ddf83080931c6143002200bfd519554527d09c182678360904d00124b14219d21fe000bf4ff6fd76b5421ad066781eb10224b1420ed214e04ff6fb76ad1b042d0fd3a578e47855ea04260ad00424b14207d300bf891b2244d8d1aae70020caf800004ff0ff3013b0bde8f08f0a990122063100910d99002601f010014ff6ff754ff6fe7882ea111106910b998a5d1f2a03d80beb02010b39f0e1d0f830124ff0ff33002900f0f181099f07eb420212f8407c12f83f2c47ea022cac4500f0e58111f80ce0bef1000f00f0df8161444f78002f00f0da81cde9017607fb0ef70e9b002f00f09d818d1c002202eb0c064645b7d0ac182178090901d0012412e04ff6fd718e42add0617809b102240ae04ff6fb71711a0429a4d3a178e67851ea06219fd004248f429cd37f1a2244ddd1002a00f0768100240027cde9042509e0dde90714049a0c44059d94424ff6fe7880f06781295d08940e090d9601d0012218e0161b012e00f05c8105eb040c9cf801200d920ab102220ce0042ec0f051819cf802609cf8032056ea02220d9200f048810422079211f00f020fa911f802c018bf012206990d9c41ea020a4fea0c1814f003013a4600f08380b7fbfef6002506fb1e725a44b2f1ff3fc8bf012505ea0a05012d1bd1f61819d4d509042d16d875090e2d13d806eb860509eb85155608d207ab5d6ff00f0208bf0f220391614608bf41461a400e9b0a430399aa55012907f1010254d0b2fbfef6002506fb1e225a44b2f1ff3fc8bf012505ea0a05012d1ad1f61818d4d509042d15d875090e2d12d806eb860609eb86135508d2070391595d6ff00f02664604bf0f2246460a40324303995a550e9b022907f1020227d0b2fbfef6002506fb1e225a44b2f1ff3fc8bf012505ea0a05012d18d1f61816d4d509042d13d875090e2d10d806eb860609eb86135508d207595d6ff00f02664604bf0f2246460a4032435a550e9bfa1c661e032e274404d23ae7043297423ff437afb2fbfef606fb1e2505eb0b040025b4f1ff3fc8bf012505ea0a05012d18d1f61816d4e509042d13d875090e2d10d806eb860609eb86166508735de4076ff00f04614604bf0f244146234019430e9b7155511cb1fbfef606fb1e1101eb0b040021b4f1ff3fc8bf012101ea0a01012918d1f61816d4e109042913d871090e2910d806eb860109eb81116308e407cd5c6ff00f04664604bf0f2446462c403443cc540e9b911cb1fbfef606fb1e1101eb0b040021b4f1ff3fc8bf012101ea0a01012918d1f61816d4e109042913d871090e2910d806eb860109eb81116308e407cd5c6ff00f04664604bf0f2446462c403443cc540e9bd11cb1fbfef606fb1e1101eb0b040021b4f1ff3fc8bf012101ea0a0101297ff471aff6183ff56eafe10904293ff66aaf71090e293ff666af06eb860109eb81116308e407cd5c6ff00f04664604bf0f2446462c403443cc540e9b53e7019a0beb0e011e461344b1f5207f0f462beaeb72a8bf4ff42077b3f5f07fa8bf4ff4f073974216ddb34214d90a9c26680f2e10d8ddf838c0bf1aa3eb0c0e009b03ebc60523f83620721ca5f802c0af80a5f806e02260029e4ff6ff750c9a0136002396428b467ff400ae184613b0bde8f08f00bf062a3cbf4ff0ff3070472de9f04fadf2944d9a46154688460f90fdf705fa002800f0d68098f80510881d854240f0298444f2dc60c2f20700d0f800b0bbf1000f00f01f849bf80c009bf80d209bf80e309bf80f7040ea022040ea034040ea0769a9f1810010f1800f4ff0ff30c0f0b2809bf810209bf811309bf8137042ea03229bf8123042ea034242ea07620692813a12f5817f40f29e800029cdf828b000f09d8008f1060b002426aa4ff4e01c00270ae000bf05f1004042f8240001378f4204f1010480f08e801bf80750681e1e28f0d968b21f2801dd012075e0a5f1c2000beb07061d28a1eb07000fd8012800f0c88396f9010010f1410f00f3c28300f03f0365f38a1302201d465de005f0f003e02b24d10328c0f0b48396f9013013f1410f00f3ae8396f9020010f1410f00f3a883e02ddeb202d1a02ec0f0a283ed2d02d19f2e00f29d832b039bb206f03f0643ea861300f03f001d18032034e004284ff0ff3036d3a5f1f50313f1050f31d396f9013013f1410f2cdc96f9020010f1410f00f37e8396f9036016f1410f00f37883f02ddbb202d1902bc0f07283f42d02d18f2b00f26d830cea854e03f03f054eea053300f03f0043ea801006f03f0326aac518042000bf074442f8245076e74ff0ff300df2944dbde8f08f0021012007915ee0002c57d0a4f1010bcdf86090cdf810a000250df1780a5f46914608e026aaab4505f1010509f10409a7f1010739d052f825100029f3d400202b4687421dd009eb80067268b2f1ff3f18dc5a1c934514d0b268b2f1ff3f11dc9a1c93450dd0f268b2f1ff3f0adcda1c934506d032690433002a00f10400e0d400e000220a9853461aae009601f00efa002800f008831a980028c3d045f265615046c0f24d018847bce7ddf810a0ddf828b0ddf860900020079404e00020ddf828b00790012098f804400f21a20604f00f0501eae271694389225143c90a8df8681001260e2198f800c098f801e098f8023098f8037014f0200f08bf0e2608bf012105fb01f102fb01f14fead1218df869104ff00d0108bf022105fb01f102fb01f14fead1218df86a104ff00c0108bf032105fb01f102fb01f14fead1218df86b104ff00b0108bf042105fb01f102fb01f14fead1218df86c104ff00a0108bf052105fb01f102fb01f14fead1218df86d104ff0090108bf062105fb01f102fb01f14fead1218df86e104ff0080108bf072105fb01f102fb01f14fead1218df86f104ff0070108bf082105fb01f102fb01f14fead1218df870104ff0060108bf092105fb01f102fb01f14fead1218df871104ff0050108bf0a2105fb01f102fb01f14fead1218df872104ff0040108bf0b2105fb01f102fb01f14fead1218df873104ff0030108bf0c2105fb01f102fb01f14fead1218df874104ff0020108bf0d2105fb01f102fb01f14fead1218df8751006fb05f102fb01f14fead1218df876104ff0000108bf0f2169435143c90a00284ff000008df877107ff4cdae43ea0720079a484403904cea0e290af10600571e00260df198080190cdf810a0029718e000bf45f265611ea8c0f24d0188479df87c009df87d10dde90ab9029fdde9072640ea01200df1980801369642814400f0ff8158f82610b1f1ff3f1edd33469f421fd008eb83004268b2f1ff3f1adc0133bb4216d08268b2f1ff3f13dc0133bb420fd0c268b2f1ff3f0cdc0133bb4208d002690133002ae2d404e001f01f000b38d2e7002258461eab19ad009501f0cdf8002800f0c781199900290d9100f0ba819df880209df881309df87e009df87f1052ea03259df8822040ea01279df8833010979df884704fea024208969df8856042ea0362069b4fea074747ea06672b4403eb2743039fcdf82c90a7eb030609eb22491aaf149500f05e810902109b014400f0030809f101004ff0010c099009f102000ceb530305900d980c934b1e0e930b9b0130121415900ceb510003eb020e9a1acde91180a1eb08001890d0074ff0000a17904feace70169013960ae014980af1010a824512981599139e0144159100f029810aeb06014b090e2befd810980028ecd00e9801eb810103280f984ff0000600eb811e80f0848011980028ded0dde90c105feac67c0afb01084fea560018f8000000f00f03c1b208bf0b09b3fa83f0400910ea141f14d106eb0901c80904280fd848081ef80020fb5c02f00f0b02f0f0021a431aafc90708bf4bea03120ef8002011980128b1d0701c400818f80000bcf1000fc1b24fea111308bf00f00f03d8b2b0fa80f0400910ea141f14d109983118c80904280fd848081ef80020fb5c02f00f0b02f0f0021a431aafc90708bf4bea03120ef80020119802283ff486afb01c400818f80000bcf1000f00f00f01c2b208bf1109b1fa81f0400910ea141f7ff474af05983318d80904283ff66eaf58081ef80020795c02f00f0602f0f0020a43db0708bf46ea01120ef800205de7ddf8548007e000bf18980436b04208f102083ff471af18f8011c0023c9b2102938bf012313ea141f09eb060c16d14feadc13042b12d84fea5c0309091ef80320795c02f00f0b02f0f0020a431aaf5feac97008bf4bea01120ef8032018f8010c00f00f01b1fa81f0400910ea141f15d10cf10103d809042810d858081ef80020795c02f00f03179d43ea011302f0f002002d08bf42ea01030ef8003098f80000c1b210294ff0000038bf012010ea141f15d10cf10203d809042810d8080959081ef80120385c02f00f0302f0f002169d0243002d08bf43ea00120ef8012098f8000000f00f01b1fa81f0400910ea141f8ad10cf10303d80904283ff685af58081ef80020795c02f00f03179d43ea011302f0f002002d08bf42ea01030ef8003072e700bf109809eb00011498b1f5207f06eb000229eae970a8bf4ff42071b2f5f07fa8bf4ff4f072ddf810a081427ff70fae26eae6739a427ff70aaedaf800700f2f3ff605ae019dd21a091a05ebc70625f83700781c7380b180f280caf80000f6e59df87c009df87d10079a40ea012001e64ff0ff300df2944dbde8f08f00200df2944dbde8f08f2de9f04182b046f27027c2f22a073d68884629466ff39601b1f1005f064606d128684ef66761ccf2ff0188427bd0fdf74ffb0546002875d000206ff017010022ab18a8542f326c18b2f50d7f01f12f0158709870d870187158719871d871187258729872d872187358739873d873187458749874d87418755875987584f82f0084f8300058769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e00b7d14ef66760ccf2ff002860012085f8ce004ff0ff30a5f8e000a5f8e200a5f8e400a5f8e600a5f8e800a5f8ea00a5f8ec00a5f8ee00a5f8f000a5f8f200a5f8f400a5f8f600a5f8f800a5f8fa00a5f8fc00a5f8fe003d60b068002818bf002d02d102b0bde8f081b6f84010b6f842205143b1f5163ff4d3c5f8240201264ff4f07085f8286201904ff4207044f267040090c0f2470400200021002200230027a04730b185f82872c5f8247202b0bde8f08188f8046002b0bde8f08100bf2de9f04f8ab0dde914a5089243ea010242ea0a02d207864640f0c4800eeb51004ff0ff374fea5a0c0eeb53068646012207eb5a00bcf1010f88bf4fea5a02069002f003000790caf341006ff00107059007eb5a006ff00207019007eb5a00009022f00302701c544203900ef101004ff0000b09960290cdf810e004e00bf1010bab4500f0c3810898139e5f466fea0b02864288bfaf183a183744099802eb820207eb87070eeb82198b4200eb871835d9059e6246b6b10698012e19f8002008f8002002460ed00198022e19f8002008f80020024606d0009819f8002008f80020024600bf06980328c8d300bf09eb020717f8016c08eb020000f8016c17f8026c043a00f8026c17f8036c00f8036c17f8047c00f8047ce9d1b0e700bfbaf1080f02d2002223e000bf03989601ba0102440298e64600eb060a6ff0030c0aeb0c00c67802eb0c07fe7006790cf1040c3e7146797e71807904eb0c060436b871edd10cf10402dde914a5f446ddf810e0079800283ff483af079e19f80200012e08f802003ff47baf501c079e19f80070022e08f800703ff472af901c19f8002008f800206be74ff6fc7000ea0a000af0030b0990aaf1010000240490cde905be07e0079c159d0134ddf850a0ac4200f01e81089f139ee0432246be42079488bf2a18d01900eb80000eeb801c901900eb8000baf1040f0eeb80184ff00009c0f09880ddf810a0ddf850e048468b4288bf504600eb010400eb03054fea54001cf800706ff0010080ea090009f1010288bf0eeb00026ff0020089ea000609f1020088bf0eeb06006ff0030686ea090609f1030b4feac47488bf0eeb060b07f00f06002c4fea550408bf3e0918f80470ed0707f00f0507f0f007374408bf45ea061708f80470541865081cf805501a44e40705f00f064fea520408bf2e0918f80450d20705f00f0205f0f005354408bf42ea0615421808f8045054081cf804401844d20704f00f054fea500208bf250918f80240c00704f00f0004f0f0042c4408bf40ea05140beb010008f8024042081cf802200beb0304c00702f00f054fea540008bf150918f80020e40702f00f0402f0f0022a4408bf44ea051208f80020099809f104098145aaf1040a7ff46eafdde905bebbf1000f3ff444af6fea09004a468b42149f88bf3a18501845081a44c0071cf805504fea520018f8007005f00f0408bf2c0907f00f0507f0f0072744d20708bf45ea0417bbf1010f08f800703ff420af149809f10102a0eb090a8b4288bfaaf10202541865081a44e4071cf805504fea520418f8040005f00f0708bf2f0900f00f0500f0f0003844d20708bf45ea0710bbf1020f08f804003ff4faae09f102008b4288bfaaf10300421854081844d2071cf804704fea500218f8024007f00f0508bf3d0904f00f0704f0f0042c44c00708bf47ea051408f80240d9e60ab0bde8f08f00bf2de9f04146f27026c2f22a063268884611466ff39601b1f1005f054606d110684ef66761ccf2ff0188427cd0fdf780f8002800f0c380024600206ff017010023d418d0542f335718b3f50d7f01f12f016070a070e07020716071a071e07120726072a072e07220736073a073e07320746074a074e07420756075a07587f82f0087f830006076a076e07620776077a077e07784f8200084f8210084f8220084f8230084f8240084f8250084f8260084f8270084f8280084f8290084f82a0084f82b0084f82c0084f82d0084f82e00b7d14ef66760ccf2ff001060012082f8ce004ff0ff30a2f8e000a2f8e200a2f8e400a2f8e600a2f8e800a2f8ea00a2f8ec00a2f8ee00a2f8f000a2f8f200a2f8f400a2f8f600a2f8f800a2f8fa00a2f8fc00a2f8fe0032600120002d82f8cc0049d0104630f8e01f41453bd0b2f8e210414537d0b2f8e410414533d0b2f8e61041452fd0b2f8e81041452bd0b2f8ea10414527d0b2f8ec10414523d0b2f8ee1041451fd0b2f8f01041451bd0b2f8f210414517d0b2f8f410414513d0b2f8f61041450fd0b2f8f81041450bd0b2f8fa10414507d0b2f8fc10414503d0b2f8fe1041450dd1012082f8de00bde8f08100203060bde8f08182f8cd000020bde8f08192f8cd1089b9b2f8c810a8eb01010bb2b3f1ff3f06dd89b2022924bf012182f8dd1002e0012182f8dc10b2f8ca30002182f8cd109845a2f8c88088bfa2f8ca8092f8001120f81180481c00f00f0082f800010020bde8f08100bf2de9f04fcdb04ef687249246c0f25b044df2546298460e4604f13c07c0f278020f2138230546b84718b12846a047002423e20afb08f040000b960295059001250da94ff4807004f585724ff0000b4ff0000947464ff000080023564604930a930697cdf820a00192029ce1602061204600219047e1680daa891a07910da900f0e681ddf82ce00021002400900be000bf04940c9c002101234ff0030807980134844200f0d181002940f0e0810da80c94045db8f1020febd0b8f1010f00f09280b8f1000f40f0968004f00f000a900598210900f0a6808842c0f08880401a05900a9800eb00140001039014e01cf80b000a9a00f0f00010440cf80b000bf1010b01390125b34524bf4ff0000b09f10109002900f08c81b9454ff000084ff0000338bf012380f0028109eb8900002d0eeb801cdbd0cdf82490a946a6eb0b05b5eb510f88bf4d088db3a8080ceb0b0820d0002708f10102a7eb80006ff0030600bf02eb060e043600eb060a1af104078ef803408ef804408ef805408ef80640efd1301d069f089eddf82ce015f0030204d10ce0002015f0030208d0012a08f8004004d04044022a447018bf8470a1eb4501ab4489b1b34510d21cf80b00039a00f00f001043ddf824900cf80b00013900250029a0d12be100bf00214d46ddf8249090e700bfbcb10598a04228d201214ff0000821e0ddf810a005985aea042a16d00c9c5045cdf810a080f0938001214ff00008002342e70c9c002101234ff002083ce70c9c00214ff00108012336e7002001214ff0000804900c9c00232ee7001b05900a9800eb00110001099015e000bf1af80b200a9802f0f00202430af80b200bf1010b013c0125b34524bf4ff0000b09f10109002c00f0da80b9454ff000084ff0000338bf012350d209eb8902002d0eeb821adcd0ac46a6eb0b05b5eb540f88bf650855b3ae080aeb0b0818d0002008f10102a0eb860e6ff00306901904360eeb06070437c170017141718171f5d1069fddf82ce0321d15f0030604d10de0002215f0030609d0012e08f8021005d04244022e517018bf917000bfa4eb4504ab447cb1089eb3450ed21af80b00099a00f00f0010430af80b00013c0025002cabd184e0089e002465469de70c9c0121b4e6a0eb0a0005900a9800eb00110001099015e01cf80b000a9a00f0f00010430cf80b000bf1010baaf1010a0125b34524bf4ff0000b09f10109baf1000f65d0b9454ff000084ff0000338bf01235bd20b9a09eb8900002d02eb801cdad0ce46a946a6eb0b05b5eb5a0f88bf4fea5a0545b3a8080ceb0b0817d0002408f10102a4eb80006ff003069719043684190434f97039717971b971f6d1301d069f089e0c9c15f0030204d10ce0002015f0030208d0012a08f8001004d04044022a417018bf8170aaeb450abaf1000fab4411d0b34511d21cf80b00099a00f00f0010430cf80b00aaf1010a0025f146baf1000fa6d10be04ff0000a4d46f14697e70c9c4ff00008002131e6012102e04ff000080021ddf82ce029e600980da973b1012817d04ff0000450b90798019ab0fa80f040094ff480703ff4ffad00e0002402984ef68721c0f25b01884720464db0bde8f08f059840ea0800b0fa80f04409eee72de9f04f83b00c9e0292002e019000f08e80019823f00304471c029803f0030ac4f1000c01384ff00008009005e000bf08f10108b0450f4479d00e98c14600286fea080418bf06eb0409002bf0d0042b06d24ff0000bbaf1000f2ed1e8e700bf0d98b64609fb00f400986ff003050444607800f0f00242ea10107a19d0706078043500f00f0646ea0010107114f8020f00f0f00646ea10105071207800f00f0646ea00100ceb050604369071e0d105f1040b7646baf1000fbad002980d9a4fea5b0509fb0204019814f805e008fb01050ef00f025feacb7908bf4fea1e1242ea0212baf1010f05f80b20a1d00bf1010e4fea5e00205cb9f1000f4fea101208bf00f00f0242ea0210baf1020f05f80e008ed00bf102004208a25cb9f1000f02f00f0408bf140944ea04122a5480e703b0bde8f08f2de9f04182b003fb02f68846cde9001645f20f1107466846c0f247011c461546884740f2196047f8240f788948f2816245ea0040b86045ea044057f8204c7860c0f2490220463946fe60c7f81080904740f25761c0f244012046884702b0bde8f08100bf2de9f04188b045f213661c46c0f24d0613460a46ddf83880002706f1ae05214627706770a770e77027716771a771e77127726772a772e77227736773a773e77327746774a774e77427756775a775e77527766776a776e77627776777a777e777a8470028c8f800705cd020786178a278e3780843104318434ff0010008d0e179a27911431ebf617a227a51ea020102d108b0bde8f081a07b14284ff00000f7d1217d8df8040041f0010121758df805008df806008df807008df808008df809008df80a008df80b008df80c008df80d008df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f0001a92046b047c8f8000018b1012008b0bde8f08106f1520120468847d8f80000002818bf012008b0bde8f0814556454e4346572f313520696d6735373620696d6736343020696d677a20726c652077616b656c65617365206469726563746662206662677561726420776561726e6f7469667920636f6d70617373313020636c65616e75703131207465786361636865313220746578696d67313320746578737472313420666f6e7431350000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a8900000000000000000", "desc": "append injected blobs to main-app payload" }, { "offset": 777711, "old": "44c33500", - "new": "e3113600", + "new": "604b3600", "desc": "main-app subheader payload size (ps)" }, { "offset": 152, "old": "c4c33500", - "new": "63123600", + "new": "e04b3600", "desc": "main-app TOC entry size (ps + 128)" }, { "offset": 777831, "old": "44c33504", - "new": "e3113604", + "new": "604b3604", "desc": "main-app preamble length (low 24 bits)" }, { "offset": 777835, "old": "df8110c9", - "new": "4c88426b", + "new": "562ec508", "desc": "[5] ota/s200_firmware_ota.bin preamble crc32" }, { "offset": 156, "old": "c1be0fb1", - "new": "79b1f28b", + "new": "ef84d21c", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)" }, { "offset": 777715, "old": "c1be0fb1", - "new": "79b1f28b", + "new": "ef84d21c", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)" } ] diff --git a/app/graphics/bdffont.ts b/app/graphics/bdffont.ts index 8f6fa9e3..8bf9dfec 100644 --- a/app/graphics/bdffont.ts +++ b/app/graphics/bdffont.ts @@ -1,5 +1,5 @@ import { knownFolders } from "@nativescript/core"; -import { getStringSetting, onSettingsStoreChanged } from "~/native/settings-store"; +import type { GrayImage } from "./image"; export type Glyph = { encoding: number; @@ -9,9 +9,42 @@ export type Glyph = { bbxX: number; bbxY: number; bitmapRows: number[]; + /** + * Antialiased coverage, bbxWidth*bbxHeight 4bpp levels (0..15) row-major. + * When present the glyph is an AA raster (e.g. a TTF render; see + * ttf-font.ts) and bitmapRows is unused/empty; when absent the glyph is + * 1bpp via bitmapRows. + */ + coverage?: Uint8Array; + /** Exact (fractional) advance in pixels; dwidthX is its integer stand-in. */ + advance?: number; }; +let nextFontFingerprintId = 1; + +/** + * Allocate a process-local font fingerprint id. Shared by every font class + * that participates in PlacedGlyph fingerprints (BdfFont, TtfFont) so ids + * never collide across font kinds. + */ +export function allocateFontFingerprintId(): number { + return nextFontFingerprintId++; +} + export class BdfFont { + /** + * Process-local identity for content fingerprints (fonts are cached + * singletons, so two draws with the same id + encoding are the same glyph + * bitmap). Only meaningful within one JS context; never persist it. + */ + readonly fingerprintId = allocateFontFingerprintId(); + /** + * Stable cross-context identity for the on-glasses glyph atlas (the + * embedded font name). Worker threads and the main thread must agree on + * it, unlike fingerprintId. Fonts without one (ad-hoc parses) simply never + * participate in texture caching — their glyphs stay baked. + */ + readonly atlasKey: string | null; readonly ascent: number; readonly descent: number; readonly lineHeight: number; @@ -32,6 +65,7 @@ export class BdfFont { defaultChar: number; glyphs: Map; fallback?: BdfFont; + atlasKey?: string; }) { this.ascent = opts.ascent; this.descent = opts.descent; @@ -39,9 +73,10 @@ export class BdfFont { this.defaultChar = opts.defaultChar; this.glyphs = opts.glyphs; this.fallback = opts.fallback; + this.atlasKey = opts.atlasKey ?? null; } - static parse(source: string, opts: { fallback?: BdfFont; minEncoding?: number } = {}): BdfFont { + static parse(source: string, opts: { fallback?: BdfFont; minEncoding?: number; atlasKey?: string } = {}): BdfFont { const minEncoding = opts.minEncoding ?? 0; const lines = source.replace(/\r/g, "").split("\n"); let ascent = 10; @@ -120,7 +155,7 @@ export class BdfFont { } } - return new BdfFont({ ascent, descent, defaultChar, glyphs, fallback: opts.fallback }); + return new BdfFont({ ascent, descent, defaultChar, glyphs, fallback: opts.fallback, atlasKey: opts.atlasKey }); } /** Exact lookup for a single code point, with no default-char substitution. */ @@ -148,6 +183,25 @@ export class BdfFont { } return width; } + + /** + * Draw text with integer advances, recording each glyph as a deferred draw + * on the image (UiFont contract; y is the top of the line). + */ + drawText(image: GrayImage, x: number, y: number, text: string, value: number): void { + let cursorX = x; + for (const char of text) { + if (char === "\n") { + cursorX = x; + y += this.lineHeight; + continue; + } + const glyph = this.getGlyph(char.codePointAt(0) ?? 32); + if (!glyph) continue; + image.drawGlyph(this, glyph, cursorX, y, value); + cursorX += glyph.dwidthX; + } + } } const embeddedFonts = { @@ -184,6 +238,7 @@ function loadEmbeddedFont(name: EmbeddedFontName): BdfFont { const font = BdfFont.parse(text, { fallback: fallbackName ? loadEmbeddedFont(fallbackName) : undefined, minEncoding: fontMinEncoding[name], + atlasKey: name, }); cachedEmbeddedFonts.set(name, font); return font; @@ -193,46 +248,6 @@ export function getFont(font: EmbeddedFontName): BdfFont { return loadEmbeddedFont(font); } -// User-selectable UI typeface. Only the small (12px) font has an alternative, -// since TerminusV ships no larger sizes; medium/large stay Terminus. The -// storage key and values are re-used by the settings UI in dashboard-settings. -export const UI_FONT_SETTING_KEY = "display.uiFont"; -export const UI_FONT_VALUES = ["terminus", "terminusv"] as const; -export type UiFontChoice = (typeof UI_FONT_VALUES)[number]; - -const SMALL_FONT_FOR_CHOICE: Record = { - terminus: "terminus12", - terminusv: "terminusv12", -}; - -// Reading the setting hits a JNI bridge, and getDefaultSmallFont() is called -// once per menu item per paint, so cache the resolved choice and invalidate it -// when the setting changes (in any isolate). -let cachedSmallFontChoice: UiFontChoice | null = null; -let smallFontListenerInstalled = false; - -function currentSmallFontName(): EmbeddedFontName { - if (!smallFontListenerInstalled) { - onSettingsStoreChanged((key) => { - if (key === UI_FONT_SETTING_KEY) cachedSmallFontChoice = null; - }); - smallFontListenerInstalled = true; - } - if (cachedSmallFontChoice === null) { - const raw = getStringSetting(UI_FONT_SETTING_KEY, "terminus"); - cachedSmallFontChoice = (UI_FONT_VALUES as readonly string[]).includes(raw) - ? (raw as UiFontChoice) - : "terminus"; - } - return SMALL_FONT_FOR_CHOICE[cachedSmallFontChoice]; -} - -export function getDefaultSmallFont(): BdfFont { - return getFont(currentSmallFontName()); -} -export function getDefaultMediumFont(): BdfFont { - return getFont("terminus16"); -} -export function getDefaultLargeFont(): BdfFont { - return getFont("terminus24"); -} +// NOTE: the default UI font getters (getDefaultSmallFont & co.) live in +// ./ui-fonts, which resolves the user's font settings to a BdfFont or +// TtfFont. This module stays a pure BDF loader. diff --git a/app/graphics/even-realities-logo.ts b/app/graphics/even-realities-logo.ts new file mode 100644 index 00000000..45f3c9dc --- /dev/null +++ b/app/graphics/even-realities-logo.ts @@ -0,0 +1,45 @@ +import { GrayImage } from "./image"; +import { loadPngAsGrayImage } from "./imagefile"; + +const LOGO_PATH = "images/EvenRealitiesLogo.png"; + +let sourceLogo: GrayImage | null | undefined; +const scaledLogos = new Map(); + +/** Render the bundled Even Realities logo at launcher/sidebar icon size. */ +export function renderEvenRealitiesLogo(size: number): GrayImage | null { + const targetSize = Math.max(1, Math.round(size)); + const cached = scaledLogos.get(targetSize); + if (cached) return cached; + + if (sourceLogo === undefined) { + try { + sourceLogo = loadPngAsGrayImage(LOGO_PATH); + } catch (error) { + console.warn(`Could not load ${LOGO_PATH}: ${error}`); + sourceLogo = null; + } + } + if (!sourceLogo) return null; + + const scaled = scaleToFit(sourceLogo, targetSize, targetSize); + scaledLogos.set(targetSize, scaled); + return scaled; +} + +function scaleToFit(source: GrayImage, maxWidth: number, maxHeight: number): GrayImage { + const scale = Math.min(maxWidth / source.width, maxHeight / source.height, 1); + const width = Math.max(1, Math.round(source.width * scale)); + const height = Math.max(1, Math.round(source.height * scale)); + if (width === source.width && height === source.height) return source; + + const output = new GrayImage(width, height, 0); + for (let y = 0; y < height; y++) { + const sourceY = Math.min(source.height - 1, Math.floor(((y + 0.5) * source.height) / height)); + for (let x = 0; x < width; x++) { + const sourceX = Math.min(source.width - 1, Math.floor(((x + 0.5) * source.width) / width)); + output.pixels[y * width + x] = source.pixels[sourceY * source.width + sourceX]!; + } + } + return output; +} diff --git a/app/graphics/evenhub-font.ts b/app/graphics/evenhub-font.ts new file mode 100644 index 00000000..fe2736db --- /dev/null +++ b/app/graphics/evenhub-font.ts @@ -0,0 +1,396 @@ +/** + * Even's on-glasses 20px UI font, for the EvenHub compatibility layer. + * + * Three pieces: + * - Latin + Greek/Cyrillic + emoji glyphs are extracted from the stock G2 + * firmware into app-private storage while Faceclaw prepares custom firmware. + * They are not distributed with Faceclaw. + * - The final fallback is the G2's serialized Source Han Sans SC Light font, + * which Faceclaw can bundle under the SIL Open Font License. + * - Text SIZING / KERNING / WRAPPING: @evenrealities/pretext, whose advance + * tables the bitmaps were validated against. Using pretext for measurement + * means our line breaks match exactly where EvenHub apps (which measure + * with the same library) expect them. + * + * Bitmaps are 4bpp, high-nibble-first, row stride floor(boxW/2)+1. The UI + * fonts are effectively 1-bit (nibbles 0 or 0xF); we keep the nibble value so + * any anti-aliasing survives. Codepoints with no glyph are skipped silently, + * matching stock firmware (no tofu boxes). + */ +import { File, knownFolders } from "@nativescript/core"; +import { getTextWidth } from "@evenrealities/pretext"; +import { EVENHUB_RUNTIME_FONT_FILENAME, isEvenHubFontAsset } from "../g2/firmware-fonts"; +import { toUint8Array } from "../util/array-util"; +import { GrayImage, grayToNibble, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image"; + +declare const com: any; + +const CJK_ASSET_PATH = "fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin"; + +/** [boxW, boxH, ofsX, ofsY, bitmapOffset, bitmapLen] */ +type GlyphRecord = [number, number, number, number, number, number]; + +type FontAsset = { + lineHeight: number; + baseline: number; + glyphs: Record; + bitmapBase64: string; +}; + +type CjkGlyph = { + record: GlyphRecord; + bitmap: Uint8Array; +}; + +const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +function base64Decode(text: string): Uint8Array { + const clean = text.replace(/[^A-Za-z0-9+/]/g, ""); + const out = new Uint8Array(Math.floor((clean.length * 3) / 4)); + let bits = 0; + let bitCount = 0; + let outIndex = 0; + for (let i = 0; i < clean.length; i++) { + bits = (bits << 6) | BASE64_ALPHABET.indexOf(clean[i]!); + bitCount += 6; + if (bitCount >= 8) { + bitCount -= 8; + out[outIndex++] = (bits >> bitCount) & 0xff; + } + } + return out.subarray(0, outIndex); +} + +/** + * Codepoint ranges whose glyphs the font asset merged from the flash CJK + * font with a baseline-shifted ofsY (see scripts/build-evenhub-font.mjs). + * The shift keeps the phone-side composite right, but the firmware's mode-15 + * draw uses the root font's baseline with the glyph's TRUE ofsY, so the two + * may disagree by the shift — keep these baked until the fallback placement + * is hardware-verified. Mirrors the build script's SYMBOL_RANGES. + */ +const MODE15_UNCERTAIN_RANGES: ReadonlyArray = [ + [0x2000, 0x206f], [0x2100, 0x214f], [0x2190, 0x21ff], [0x2200, 0x22ff], + [0x2500, 0x257f], [0x2580, 0x259f], [0x25a0, 0x25ff], [0x2600, 0x26ff], + [0x2700, 0x27bf], [0x3000, 0x303f], [0xff00, 0xffef], +]; + +function inMode15UncertainRange(cp: number): boolean { + for (const [start, end] of MODE15_UNCERTAIN_RANGES) { + if (cp >= start && cp <= end) return true; + } + return false; +} + +/** The 4bpp level an 8-bit value packs to (BmpUtil's GRAY_TO_NIBBLE). */ +const quantNibble = grayToNibble; + +export class EvenHubFont implements FwTextFont { + /** One builtin firmware font today; see FwTextFont. */ + readonly atlasTag = 1; + readonly lineHeight: number; + readonly baseline: number; + private readonly glyphs: Map; + private readonly bitmap: Uint8Array; + private readonly cjkPath: string; + private readonly cjkBaseline: number; + private readonly cjkGlyphs = new Map(); + + private constructor(asset: FontAsset) { + this.lineHeight = asset.lineHeight; + this.baseline = asset.baseline; + this.bitmap = base64Decode(asset.bitmapBase64); + this.glyphs = new Map(); + for (const key of Object.keys(asset.glyphs)) { + this.glyphs.set(Number(key), asset.glyphs[key]!); + } + this.cjkPath = knownFolders.currentApp().getFile(CJK_ASSET_PATH).path; + const metrics = toUint8Array(com.faceclaw.app.LvglFontFile.getMetrics(this.cjkPath)); + if (metrics.length < 2) { + throw new Error("The bundled Source Han Sans font could not be loaded."); + } + this.cjkBaseline = metrics[1]!; + } + + private static cached: EvenHubFont | null = null; + + static get(): EvenHubFont { + if (!EvenHubFont.cached) { + const path = `${knownFolders.documents().path}/${EVENHUB_RUNTIME_FONT_FILENAME}`; + let asset: FontAsset | null = null; + if (File.exists(path)) { + try { + const parsed: unknown = JSON.parse(File.fromPath(path).readTextSync()); + if (!isEvenHubFontAsset(parsed)) throw new Error("invalid font asset structure"); + asset = parsed; + } catch (error) { + console.warn(`Could not load the extracted G2 fonts; using Source Han Sans only: ${error}`); + } + } else { + console.warn( + "The G2 fonts have not been extracted yet; using Source Han Sans only. " + + "Reinstall Faceclaw's custom firmware to prepare the exact fallback chain.", + ); + } + EvenHubFont.cached = new EvenHubFont( + asset ?? { lineHeight: 27, baseline: 22, glyphs: {}, bitmapBase64: "" }, + ); + } + return EvenHubFont.cached; + } + + hasGlyph(codePoint: number): boolean { + return this.glyphs.has(codePoint) || this.getCjkGlyph(codePoint) !== null; + } + + /** + * Pixel advance of a codepoint, including kerning against the following one + * (pass 0/undefined for the last glyph). Derived from pretext's public + * getTextWidth so it carries pretext's kerning and missing-glyph placeholder + * exactly: width(a+b) - width(b) isolates a's kerned advance. + */ + advanceOf(codePoint: number, nextCodePoint = 0): number { + const a = String.fromCodePoint(codePoint); + if (!nextCodePoint) return getTextWidth(a); + const b = String.fromCodePoint(nextCodePoint); + return getTextWidth(a + b) - getTextWidth(b); + } + + /** Single-line pixel width including kerning (pretext-exact). */ + measureLine(text: string): number { + return getTextWidth(text); + } + + /** + * Wrap text to a pixel width, returning the actual line strings. Ports + * pretext's measureTextWrap (LVGL lv_text_get_next_line semantics) so the + * breaks land exactly where the measurement library reports them. + */ + wrap(text: string, maxWidth: number): string[] { + if (!text) return [""]; + const cps = Array.from(text).map((c) => c.codePointAt(0)!); + const lines: string[] = []; + let line: number[] = []; + let lineWidth = 0; + let lastBreak = -1; // index within `line` after which we may wrap + + for (let i = 0; i < cps.length; i++) { + const cp = cps[i]!; + if (cp === 10) { + lines.push(String.fromCodePoint(...line)); + line = []; + lineWidth = 0; + lastBreak = -1; + continue; + } + // LVGL drops leading spaces at the start of a wrapped line. + if (line.length === 0 && cp === 32) continue; + const adv = this.advanceOf(cp, cps[i + 1] ?? 0); + if (lineWidth + adv > maxWidth) { + if (cp === 32) { + // The overflowing char is a space: break here, discard it. + lines.push(String.fromCodePoint(...line)); + line = []; + lineWidth = 0; + lastBreak = -1; + } else if (lastBreak !== -1) { + // Wrap at the last break opportunity; carry the remainder forward. + const carry = line.slice(lastBreak + 1); + line = line.slice(0, lastBreak + 1); + const trimmed = line[line.length - 1] === 32 ? line.slice(0, -1) : line; + lines.push(String.fromCodePoint(...trimmed)); + line = carry; + lineWidth = carry.reduce((sum, c, j) => sum + this.advanceOf(c, carry[j + 1] ?? 0), 0); + lastBreak = -1; + line.push(cp); + lineWidth += adv; + } else { + // No break opportunity: hard break before the overflowing char. + lines.push(String.fromCodePoint(...line)); + line = [cp]; + lineWidth = adv; + lastBreak = -1; + } + } else { + line.push(cp); + lineWidth += adv; + if (isBreakable(cp)) lastBreak = line.length - 1; + } + } + lines.push(String.fromCodePoint(...line)); + return lines; + } + + /** + * Draw one line of text with its top-left at (x, y). Missing glyphs are + * skipped. + * + * Glyphs the firmware's mode-15 builtin-font draw renders identically to + * our raster (native-table entries outside MODE15_UNCERTAIN_RANGES) are + * recorded as deferred firmware-text runs: maximal stretches laid out with + * consecutive pretext advances, spaces included as inkless connectors so + * the firmware's own advance/kerning walk stays in lockstep. Anything else + * (CJK fallback, shifted symbols, unknown codepoints) rasters immediately + * and breaks the run — a break means the next run restarts at an explicit + * pen x, so no cursor prediction ever spans an uncertain glyph. + */ + drawText(image: GrayImage, x: number, y: number, text: string, value = 255): void { + const cps = Array.from(text).map((c) => c.codePointAt(0)!); + const baselineY = y + this.baseline; + let penX = x; + let run: PlacedFwTextGlyph[] = []; + let runX = 0; + const flushRun = () => { + if (run.length) { + this.emitRun(image, runX, y, value, run); + run = []; + } + }; + for (let i = 0; i < cps.length; i++) { + const cp = cps[i]!; + const glyph = this.glyphs.get(cp); + if (glyph && !inMode15UncertainRange(cp)) { + if (run.length === 0) runX = penX; + run.push({ + cp, + dx: penX - runX, + ink: glyph[0] > 0 && glyph[1] > 0 && glyph[5] > 0, + }); + } else if (glyph) { + flushRun(); + this.drawGlyph(image, glyph, this.bitmap, penX, baselineY, value); + } else { + flushRun(); + const cjk = this.getCjkGlyph(cp); + if (cjk) this.drawGlyph(image, cjk.record, cjk.bitmap, penX, y + this.cjkBaseline, value); + } + penX += this.advanceOf(cp, cps[i + 1] ?? 0); + } + flushRun(); + } + + /** Record one run, trimmed of inkless leading/trailing connectors. */ + private emitRun( + image: GrayImage, + runX: number, + lineY: number, + value: number, + members: PlacedFwTextGlyph[], + ): void { + let first = 0; + let last = members.length - 1; + while (first <= last && !members[first]!.ink) first++; + while (last >= first && !members[last]!.ink) last--; + if (first > last) return; // nothing visible + const startX = runX + members[first]!.dx; + const trimmed = members.slice(first, last + 1).map((member) => + first === 0 ? member : { ...member, dx: member.dx - members[first]!.dx }, + ); + image.drawFwText(this, startX, lineY, value, trimmed); + } + + /** FwTextFont: bake a recorded run with firmware-exact pixel math. */ + bakeFwTextRun(target: GrayImage, run: PlacedFwText, dx: number, dy: number): void { + const baselineY = run.y + dy + this.baseline; + for (const member of run.glyphs) { + if (!member.ink) continue; + const glyph = this.glyphs.get(member.cp); + if (glyph) this.drawGlyph(target, glyph, this.bitmap, run.x + member.dx + dx, baselineY, run.value); + } + } + + /** FwTextFont: raster registration data for the Java-side planner. */ + fwGlyphWireData(cp: number): { + ofsX: number; + inkTop: number; + boxW: number; + boxH: number; + nibbles: Uint8Array; + } | null { + const glyph = this.glyphs.get(cp); + if (!glyph) return null; + const [boxW, boxH, ofsX, ofsY, off, len] = glyph; + if (boxW <= 0 || boxH <= 0 || len <= 0 || boxW > 255 || boxH > 255) return null; + if (ofsX < -32768 || ofsX > 32767) return null; + const inkTop = this.baseline - boxH - ofsY; + if (inkTop < -32768 || inkTop > 32767) return null; + // Repack from the source stride (floor(boxW/2)+1, pad nibble at odd + // widths) to the wire stride ceil(boxW/2). + const srcStride = Math.floor(boxW / 2) + 1; + const outStride = (boxW + 1) >> 1; + const nibbles = new Uint8Array(outStride * boxH); + for (let row = 0; row < boxH; row++) { + for (let byte = 0; byte < outStride; byte++) { + nibbles[row * outStride + byte] = this.bitmap[off + row * srcStride + byte] ?? 0; + } + } + return { ofsX, inkTop, boxW, boxH, nibbles }; + } + + /** Draw wrapped text within a box; returns the number of lines drawn. */ + drawTextWrapped(image: GrayImage, x: number, y: number, maxWidth: number, text: string, value = 255): number { + const lines = this.wrap(text, maxWidth); + for (let i = 0; i < lines.length; i++) { + this.drawText(image, x, y + i * this.lineHeight, lines[i]!, value); + } + return lines.length; + } + + private getCjkGlyph(codePoint: number): CjkGlyph | null { + const cached = this.cjkGlyphs.get(codePoint); + if (cached !== undefined) return cached; + const bytes = toUint8Array(com.faceclaw.app.LvglFontFile.getGlyph(this.cjkPath, codePoint)); + if (bytes.length < 8) { + this.cjkGlyphs.set(codePoint, null); + return null; + } + const u16 = (offset: number) => bytes[offset]! | (bytes[offset + 1]! << 8); + const i16 = (offset: number) => { + const value = u16(offset); + return value & 0x8000 ? value - 0x10000 : value; + }; + const bitmap = bytes.subarray(8); + const glyph: CjkGlyph = { + record: [u16(0), u16(2), i16(4), i16(6), 0, bitmap.length], + bitmap, + }; + this.cjkGlyphs.set(codePoint, glyph); + return glyph; + } + + private drawGlyph( + image: GrayImage, + glyph: GlyphRecord, + bitmap: Uint8Array, + penX: number, + baselineY: number, + value: number, + ): void { + const [boxW, boxH, ofsX, ofsY, off, len] = glyph; + if (boxW <= 0 || boxH <= 0 || len <= 0) return; + const stride = Math.floor(boxW / 2) + 1; + const left = penX + ofsX; + const top = baselineY - boxH - ofsY; + // Firmware-exact shading: the CFW draws through a LUT mapping source + // nibble n to n*top/15 (integer), skipping only n == 0. Writing 16*level + // makes the composite quantize (min(15, (v+8)>>4)) back to exactly that + // level, so the texture-cache planner's pixel checks — and the shadow + // after an on-glasses mode-15 draw — match this bake bit-for-bit. + const top4 = quantNibble(value); + for (let row = 0; row < boxH; row++) { + const rowStart = off + row * stride; + for (let col = 0; col < boxW; col++) { + const byte = bitmap[rowStart + (col >> 1)] ?? 0; + const nibble = col % 2 === 0 ? byte >> 4 : byte & 0x0f; + if (nibble === 0) continue; + image.setPixel(left + col, top + row, Math.floor((nibble * top4) / 15) * 16); + } + } + } +} + +function isBreakable(cp: number): boolean { + if (cp === 32 || cp === 45) return true; // space, hyphen + // CJK ranges (pretext parity) — breakable between any two ideographs. + return (cp >= 0x2e80 && cp <= 0x9fff) || (cp >= 0xf900 && cp <= 0xfaff) || (cp >= 0xac00 && cp <= 0xd7af); +} diff --git a/app/graphics/glyph-wire.ts b/app/graphics/glyph-wire.ts new file mode 100644 index 00000000..3842c499 --- /dev/null +++ b/app/graphics/glyph-wire.ts @@ -0,0 +1,375 @@ +/** + * Marshals deferred-draw identity (text glyphs and icon images) to the Java + * side for the texture-cache pipeline (CFW modes 12/13/14; see + * notes/texture-cache-display-list-design.md). + * + * Channels, all cheap ByteBuffer crossings: + * - glyph raster registration into the process-wide Java GlyphAtlas, once + * per (font, glyph) per JS context (main thread and each worker register + * independently; the atlas dedupes by the font's stable atlasKey); + * - image registration into the Java ImageAtlas, once per distinct image + * content per JS context (images are content-addressed: the key is a hash + * of dimensions + pixels, so the same icon rendered anywhere dedupes and + * a changed icon is simply a new image); + * - a per-frame draw list accompanying each submitted surface frame, which + * the Java planner may replay as on-glasses cached draws. + * + * Draws that can't participate (no atlasKey, outside the mode-14 ASCII + * table, ink outside the line cell, oversized images) are simply left out — + * their pixels are baked into the submitted frame either way, so exclusion + * only means "no wire savings for this draw". + */ +import { Glyph } from "./bdffont"; +import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedImage } from "./image"; + +declare const com: any; + +const GLYPH_RECORD_BYTES = 12; // [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] +const IMAGE_RECORD_BYTES = 9; // [1][imageId u32][x s16][y s16] +const FWTEXT_HEADER_BYTES = 7; // [2][x s16][y s16][value u8][count u8] +const FWTEXT_MEMBER_BYTES = 7; // [cp u32][dx s16][ink u8] + +type FontWireState = { + fontId: number; + cellHeight: number; + /** Encodings already registered with the Java atlas from this JS context. */ + registered: Set; +}; + +const fontStates = new Map(); + +/** + * Java-side image id per source image object, or null for images that can't + * participate. Sources handed to drawImage are immutable by contract, so + * memoizing by object identity is safe; a re-rendered icon is a new object + * and re-resolves (usually to the same content-addressed id). + */ +const imageIds = new WeakMap(); + +function fontWireState(font: GlyphFont): FontWireState | null { + if (!global.isAndroid || !font.atlasKey) return null; + if (font.lineHeight <= 0 || font.lineHeight > 255) return null; + let state = fontStates.get(font); + if (!state) { + state = { + fontId: com.faceclaw.app.GlyphAtlas.fontId(font.atlasKey), + cellHeight: font.lineHeight, + registered: new Set(), + }; + fontStates.set(font, state); + } + return state; +} + +/** + * Whether this glyph can be expressed as an on-glasses cached draw: within + * the mode-14 char table (32..127), a nonempty raster that fits the font's + * line cell (the cached image is bbxWidth x lineHeight with the ink placed at + * inkTop), and metrics that fit the wire fields. + */ +function representableGlyph(font: GlyphFont, glyph: Glyph): boolean { + if (glyph.encoding < 32 || glyph.encoding > 127) return false; + if (glyph.bbxWidth <= 0 || glyph.bbxWidth > 255) return false; + if (glyph.bbxHeight <= 0 || glyph.bbxHeight > 255) return false; + if (glyph.bbxX < -128 || glyph.bbxX > 127) return false; + const inkTop = font.ascent - (glyph.bbxHeight + glyph.bbxY); + return inkTop >= 0 && inkTop + glyph.bbxHeight <= font.lineHeight; +} + +/** Firmware-font codepoints already registered with the Java FwGlyphAtlas. */ +const fwRegistered = new Set(); + +/** + * Whether a firmware-text run is expressible on the wire, registering any + * unseen ink members' rasters with the Java FwGlyphAtlas as a side effect. + */ +function prepareFwTextRun(placed: PlacedFwText): boolean { + if (!inRange16(placed.x) || !inRange16(placed.y)) return false; + if (placed.glyphs.length === 0 || placed.glyphs.length > 255) return false; + let registration: Array<{ cp: number; data: NonNullable> }> | null = null; + for (const member of placed.glyphs) { + if (member.cp < 0 || member.cp > 0x10ffff || !inRange16(member.dx)) return false; + if (!member.ink) continue; + const key = `${placed.font.atlasTag}:${member.cp}`; + if (fwRegistered.has(key)) continue; + const data = placed.font.fwGlyphWireData(member.cp); + if (!data) return false; // ink without raster data: keep the run baked + fwRegistered.add(key); + registration ??= []; + registration.push({ cp: member.cp, data }); + } + if (registration) { + let total = 0; + for (const entry of registration) { + total += 10 + entry.data.nibbles.length; + } + const buffer = new ArrayBuffer(total); + const view = new DataView(buffer); + const bytes = new Uint8Array(buffer); + let offset = 0; + for (const entry of registration) { + view.setUint32(offset, entry.cp, true); + view.setInt16(offset + 4, entry.data.ofsX, true); + view.setInt16(offset + 6, entry.data.inkTop, true); + view.setUint8(offset + 8, entry.data.boxW); + view.setUint8(offset + 9, entry.data.boxH); + bytes.set(entry.data.nibbles, offset + 10); + offset += 10 + entry.data.nibbles.length; + } + com.faceclaw.app.FwGlyphAtlas.register(buffer); + } + return true; +} + +/** Resolve (registering on first sight) the Java image id for a drawImage source. */ +function imageId(placed: PlacedImage): number | null { + if (!global.isAndroid) return null; + const source = placed.source; + const memo = imageIds.get(source); + if (memo !== undefined) return memo; + let id: number | null = null; + if (source.width > 0 && source.width <= 255 && source.height > 0 && source.height <= 255) { + const key = `img:${source.width}x${source.height}:${source.contentHash32().toString(16)}`; + id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, source.pixels.buffer); + if (!(typeof id === "number") || id <= 0) id = null; + } + imageIds.set(source, id); + return id; +} + +function inRange16(v: number): boolean { + return v >= -32768 && v <= 32767; +} + +/** + * Register any not-yet-registered glyph rasters and image contents among + * `draws` with the Java atlases, then build the per-frame draw buffer to pass + * alongside the frame's pixels: little-endian tagged records + * [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph) + * [1][imageId u32][x s16][y s16] (image) + * [2][x s16][y s16][value u8][count u8] (fw text run) + * then count x [cp u32][dx s16][ink u8] + * in draw order. Returns null when nothing is expressible (or off-Android). + */ +export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null { + if (!global.isAndroid || draws.length === 0) return null; + + // Pass 1: resolve ids, register unseen rasters, size the buffer. + let registration: Map | null = null; + const fwRunOk = new Map(); + let bytes = 0; + for (const placed of draws) { + if (placed.kind === "glyph") { + const state = fontWireState(placed.font); + if (!state || !representableGlyph(placed.font, placed.glyph)) continue; + if (!inRange16(placed.x) || !inRange16(placed.y)) continue; + bytes += GLYPH_RECORD_BYTES; + if (state.registered.has(placed.glyph.encoding)) continue; + state.registered.add(placed.glyph.encoding); + registration ??= new Map(); + let group = registration.get(state); + if (!group) { + group = { font: placed.font, glyphs: [], aaGlyphs: [] }; + registration.set(state, group); + } + (placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph); + } else if (placed.kind === "image") { + if (!inRange16(placed.x) || !inRange16(placed.y)) continue; + if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES; + } else { + const ok = prepareFwTextRun(placed); + fwRunOk.set(placed, ok); + if (ok) bytes += FWTEXT_HEADER_BYTES + placed.glyphs.length * FWTEXT_MEMBER_BYTES; + } + } + if (registration) { + const plainBuffer = buildRegistrationBuffer(registration); + if (plainBuffer) com.faceclaw.app.GlyphAtlas.register(plainBuffer); + const aaBuffer = buildAaRegistrationBuffer(registration); + if (aaBuffer) com.faceclaw.app.GlyphAtlas.registerAa(aaBuffer); + } + if (bytes === 0) return null; + + // Pass 2: the frame's draw records. + const out = new DataView(new ArrayBuffer(bytes)); + let offset = 0; + for (const placed of draws) { + if (placed.kind === "glyph") { + if (!inRange16(placed.x) || !inRange16(placed.y)) continue; + const state = fontWireState(placed.font); + if (!state || !representableGlyph(placed.font, placed.glyph)) continue; + out.setUint8(offset, 0); + out.setUint16(offset + 1, state.fontId, true); + out.setUint32(offset + 3, placed.glyph.encoding, true); + out.setInt16(offset + 7, placed.x, true); + out.setInt16(offset + 9, placed.y, true); + out.setUint8(offset + 11, placed.value); + offset += GLYPH_RECORD_BYTES; + } else if (placed.kind === "image") { + if (!inRange16(placed.x) || !inRange16(placed.y)) continue; + const id = imageId(placed); + if (id === null) continue; + out.setUint8(offset, 1); + out.setUint32(offset + 1, id, true); + out.setInt16(offset + 5, placed.x, true); + out.setInt16(offset + 7, placed.y, true); + offset += IMAGE_RECORD_BYTES; + } else { + if (!fwRunOk.get(placed)) continue; + out.setUint8(offset, 2); + out.setInt16(offset + 1, placed.x, true); + out.setInt16(offset + 3, placed.y, true); + out.setUint8(offset + 5, placed.value); + out.setUint8(offset + 6, placed.glyphs.length); + offset += FWTEXT_HEADER_BYTES; + for (const member of placed.glyphs) { + out.setUint32(offset, member.cp, true); + out.setInt16(offset + 4, member.dx, true); + out.setUint8(offset + 6, member.ink ? 1 : 0); + offset += FWTEXT_MEMBER_BYTES; + } + } + } + return out.buffer; +} + +type RegistrationGroup = { + font: GlyphFont; + /** 1bpp (BDF) glyphs for GlyphAtlas.register. */ + glyphs: Glyph[]; + /** Antialiased (coverage) glyphs for GlyphAtlas.registerAa. */ + aaGlyphs: Glyph[]; +}; + +/** + * 1bpp registration buffer: per font group + * [keyLen u8][key utf8][cellHeight u8][count u16] + * then per glyph + * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] + * [inkHeight x row u32] (bit (ceil(width/8)*8 - 1 - col) = ink) + * mirroring GlyphAtlas.register on the Java side. Null when the groups hold + * no 1bpp glyphs. + */ +function buildRegistrationBuffer( + registration: Map, +): ArrayBuffer | null { + return buildGroupedBuffer( + registration, + (group) => group.glyphs, + (glyph) => 8 + 4 * glyph.bbxHeight, + (view, _bytes, offset, group, glyph) => { + writeGlyphHeader(view, offset, group, glyph); + offset += 8; + for (let row = 0; row < glyph.bbxHeight; row++) { + view.setUint32(offset, (glyph.bitmapRows[row] ?? 0) >>> 0, true); + offset += 4; + } + return offset; + }, + ); +} + +/** + * AA registration buffer: same group framing, but each glyph record carries + * packed 4bpp 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] + * mirroring GlyphAtlas.registerAa. Null when the groups hold no AA glyphs. + */ +function buildAaRegistrationBuffer( + registration: Map, +): ArrayBuffer | null { + return buildGroupedBuffer( + registration, + (group) => group.aaGlyphs, + (glyph) => 8 + ((glyph.bbxWidth + 1) >> 1) * glyph.bbxHeight, + (view, bytes, offset, group, glyph) => { + writeGlyphHeader(view, offset, group, glyph); + offset += 8; + const coverage = glyph.coverage!; + const stride = (glyph.bbxWidth + 1) >> 1; + for (let row = 0; row < glyph.bbxHeight; row++) { + const rowStart = row * glyph.bbxWidth; + for (let b = 0; b < stride; b++) { + const col = b * 2; + const high = coverage[rowStart + col]! & 0x0f; + const low = col + 1 < glyph.bbxWidth ? coverage[rowStart + col + 1]! & 0x0f : 0; + bytes[offset++] = (high << 4) | low; + } + } + return offset; + }, + ); +} + +/** [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8], shared by both formats. */ +function writeGlyphHeader(view: DataView, offset: number, group: RegistrationGroup, glyph: Glyph): void { + const inkTop = group.font.ascent - (glyph.bbxHeight + glyph.bbxY); + view.setUint32(offset, glyph.encoding, true); + view.setInt8(offset + 4, glyph.bbxX); + view.setUint8(offset + 5, inkTop); + view.setUint8(offset + 6, glyph.bbxWidth); + view.setUint8(offset + 7, glyph.bbxHeight); +} + +/** Shared group-framing walk for the two registration formats. */ +function buildGroupedBuffer( + registration: Map, + glyphsOf: (group: RegistrationGroup) => Glyph[], + glyphBytes: (glyph: Glyph) => number, + writeGlyph: ( + view: DataView, + bytes: Uint8Array, + offset: number, + group: RegistrationGroup, + glyph: Glyph, + ) => number, +): ArrayBuffer | null { + let total = 0; + let glyphCount = 0; + const encodedKeys = new Map(); + registration.forEach((group, state) => { + const glyphs = glyphsOf(group); + if (glyphs.length === 0) return; + const keyBytes = utf8Encode(group.font.atlasKey ?? ""); + encodedKeys.set(state, keyBytes); + total += 1 + keyBytes.length + 1 + 2; + glyphCount += glyphs.length; + for (const glyph of glyphs) { + total += glyphBytes(glyph); + } + }); + if (glyphCount === 0) return null; + const buffer = new ArrayBuffer(total); + const view = new DataView(buffer); + const bytes = new Uint8Array(buffer); + let offset = 0; + registration.forEach((group, state) => { + const glyphs = glyphsOf(group); + if (glyphs.length === 0) return; + const keyBytes = encodedKeys.get(state)!; + view.setUint8(offset, keyBytes.length); + bytes.set(keyBytes, offset + 1); + offset += 1 + keyBytes.length; + view.setUint8(offset, state.cellHeight); + view.setUint16(offset + 1, glyphs.length, true); + offset += 3; + for (const glyph of glyphs) { + offset = writeGlyph(view, bytes, offset, group, glyph); + } + }); + return buffer; +} + +function utf8Encode(text: string): Uint8Array { + // TextEncoder exists in NativeScript's JS runtime; fall back to code units + // (atlas keys are ASCII font names, so both paths agree). + if (typeof TextEncoder !== "undefined") { + return new TextEncoder().encode(text); + } + const out = new Uint8Array(text.length); + for (let i = 0; i < text.length; i++) { + out[i] = text.charCodeAt(i) & 0x7f; + } + return out; +} diff --git a/app/graphics/icons.ts b/app/graphics/icons.ts index 93408e54..6deba979 100644 --- a/app/graphics/icons.ts +++ b/app/graphics/icons.ts @@ -54,6 +54,8 @@ export const ICON_SVGS = { '', music: '', + package: + '', activity: '', bell: @@ -141,6 +143,11 @@ export function renderIcon(name: IconName, size: number): GrayImage | null { return renderSvgCached(name, ICON_SVGS[name], size); } +/** Render an app-provided SVG using the same small renderer and cache. */ +export function renderSvgIcon(cacheName: string, svg: string, size: number): GrayImage | null { + return renderSvgCached(`custom:${cacheName}`, svg, size); +} + /** * Render an icon with a glyph character substituted in — currently only the * terminal icon, whose "_" becomes the glyph (">3" instead of ">_"). Falls diff --git a/app/graphics/image.ts b/app/graphics/image.ts index f234f4be..4a42271d 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -1,4 +1,4 @@ -import { BdfFont, Glyph } from "./bdffont"; +import { Glyph } from "./bdffont"; import { wrapText } from "./textwrap"; export const G2_LENS_WIDTH = 640; @@ -21,10 +21,128 @@ export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayIm return image; } +/** + * The font behind a deferred glyph draw: what rasterizeGlyph and the wire + * encoder (glyph-wire.ts) need from it. BdfFont satisfies this directly; + * TtfFont (ttf-font.ts) implements it for antialiased glyphs. Structural so + * image.ts needs no imports beyond BdfFont's Glyph shape. + */ +export interface GlyphFont { + /** Process-local identity for content fingerprints (see BdfFont). */ + readonly fingerprintId: number; + /** Stable cross-context identity for the on-glasses glyph atlas, or null. */ + readonly atlasKey: string | null; + readonly ascent: number; + readonly lineHeight: number; +} + +/** + * A general text-drawing font: what GrayImage.drawText and the shared list + * UI helpers need. BdfFont (bitmap) and TtfFont (antialiased) both implement + * it. The font owns text layout (drawText) because layout rules differ by + * kind — BDF advances are integers, TTF advances are fractional and kerned. + */ +export interface UiFont extends GlyphFont { + readonly descent: number; + measureText(text: string): number; + getGlyph(codePoint: number): Glyph | undefined; + hasGlyph(codePoint: number): boolean; + drawText(image: GrayImage, x: number, y: number, text: string, value: number): void; +} + +/** + * One deferred glyph draw. Text drawn through drawGlyph/drawText/ + * drawTextWrapped is retained here rather than baked into the pixel buffer, + * so the compositor can ship glyphs as on-glasses cached draws instead of + * pixels. (x, y) and value are the drawGlyph arguments (y is the top of + * the line; the baseline is y + font.ascent). + */ +export type PlacedGlyph = { + kind: "glyph"; + font: GlyphFont; + glyph: Glyph; + x: number; + y: number; + value: number; +}; + +/** + * One deferred image draw (drawImage): a color-key blit of `source` at + * (x, y), retained so icons can ship as on-glasses cached-image draws. + * `source` is flat (no deferred draws of its own) and treated as immutable + * from the moment it is placed. + */ +export type PlacedImage = { + kind: "image"; + source: GrayImage; + x: number; + y: number; +}; + +/** One member of a firmware-font text run: codepoint at run.x + dx; ink = + * whether it has visible pixels (spaces are inkless connectors kept for the + * firmware's own advance/kerning continuity). */ +export type PlacedFwTextGlyph = { + cp: number; + dx: number; + ink: boolean; +}; + +/** + * The provider behind firmware-font text runs (implemented by EvenHubFont). + * Structural interface so image.ts needs no import: the font both bakes runs + * phone-side (with firmware-exact pixel math) and serves per-glyph raster + * data for the Java-side eligibility checks. + */ +export interface FwTextFont { + /** Stable tag for fingerprints/registration (one builtin font today). */ + readonly atlasTag: number; + bakeFwTextRun(target: GrayImage, run: PlacedFwText, dx: number, dy: number): void; + /** Wire-registration raster for a run member, or null when unavailable. */ + fwGlyphWireData(cp: number): { + ofsX: number; + /** First ink row relative to the run's line-top y. */ + inkTop: number; + boxW: number; + boxH: number; + /** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */ + nibbles: Uint8Array; + } | null; +} + +/** + * One deferred firmware-font text run (CFW mode 15): a maximal stretch of + * text that EvenHubFont laid out with consecutive pretext advances, drawn by + * the glasses' builtin 20px font chain. Members' dx are relative to x so + * translation only touches x/y. Runs must only be built over glyphs whose + * on-glasses rendering the phone predicts exactly (the font enforces this). + */ +export type PlacedFwText = { + kind: "fwtext"; + font: FwTextFont; + /** Pen x of the first member; the wire draw starts exactly here. */ + x: number; + /** Line top y (the mode-15 y argument). */ + y: number; + value: number; + glyphs: readonly PlacedFwTextGlyph[]; +}; + +/** + * A retained draw, always rendered on top of the raster pixels regardless of + * draw order (deferred draws keep their order among themselves). An overlay + * that must cover deferred draws below it needs its own plane (see plane.ts) + * or must suppress the covered draws. + */ +export type DeferredDraw = PlacedGlyph | PlacedImage | PlacedFwText; + export class GrayImage { readonly width: number; readonly height: number; readonly pixels: Uint8Array; + private readonly drawList: DeferredDraw[] = []; + /** See sourceContentHash32; null until this image is hashed as a draw source. */ + private memoizedSourceHash: number | null = null; constructor(width: number, height: number, fill = 0) { this.width = width; @@ -33,14 +151,24 @@ export class GrayImage { this.clear(fill); } + get draws(): readonly DeferredDraw[] { + return this.drawList; + } + + hasDeferredDraws(): boolean { + return this.drawList.length > 0; + } + clone(): GrayImage { const clone = new GrayImage(this.width, this.height, 0); clone.pixels.set(this.pixels); + clone.drawList.push(...this.drawList); return clone; } clear(value = 0): void { this.pixels.fill(clampByte(value)); + this.drawList.length = 0; } setPixel(x: number, y: number, value: number): void { @@ -105,24 +233,12 @@ export class GrayImage { } } - drawText(font: BdfFont, x: number, y: number, text: string, value: number): void { - let cursorX = x; - const fill = clampByte(value); - for (const char of text) { - if (char === "\n") { - cursorX = x; - y += font.lineHeight; - continue; - } - const glyph = font.getGlyph(char.codePointAt(0) ?? 32); - if (!glyph) continue; - this.drawGlyph(font, glyph, cursorX, y, fill); - cursorX += glyph.dwidthX; - } + drawText(font: UiFont, x: number, y: number, text: string, value: number): void { + font.drawText(this, x, y, text, value); } drawTextWrapped({font, x, y, width, text, value }: { - font: BdfFont; + font: UiFont; x: number; y: number; width: number; @@ -131,7 +247,7 @@ export class GrayImage { }): void { const lines = wrapText(font, text, width); for (let i = 0; i < lines.length; i++) { - this.drawText(font, x, y + i * font.lineHeight, lines[i]!, value); + font.drawText(this, x, y + i * font.lineHeight, lines[i]!, value); } } @@ -168,6 +284,23 @@ export class GrayImage { this.pixels[ty * this.width + tx] = value; } } + + // Carry the source's deferred draws along, translated into destination + // space. Membership in the copied rect is judged by the draw's anchor + // (fine for the whole-image blits this is used for; a sub-rect blit can + // clip a draw's overhang imperfectly). + if (source.drawList.length) { + const offsetX = destX - srcX; + const offsetY = destY - srcY; + for (const placed of source.drawList) { + if ( + placed.x >= srcX && placed.x < srcX + copyWidth && + placed.y >= srcY && placed.y < srcY + copyHeight + ) { + this.drawList.push({ ...placed, x: placed.x + offsetX, y: placed.y + offsetY }); + } + } + } } fillRoundedRect( @@ -245,37 +378,221 @@ export class GrayImage { } } - fingerprint(): string { + /** 32-bit content hash over pixels and deferred draws (fingerprint's core). */ + contentHash32(): number { + let hash = this.pixelsHash32(); + for (const placed of this.drawList) { + if (placed.kind === "glyph") { + hash = mixInt(hash, placed.font.fingerprintId); + hash = mixInt(hash, placed.glyph.encoding); + hash = mixInt(hash, placed.value); + } else if (placed.kind === "image") { + hash = mixInt(hash, 0x1c0e5); + hash = mixInt(hash, placed.source.width); + hash = mixInt(hash, placed.source.height); + hash = mixInt(hash, placed.source.sourceContentHash32()); + } else { + hash = mixInt(hash, 0xf17e); + hash = mixInt(hash, placed.font.atlasTag); + hash = mixInt(hash, placed.value); + for (const member of placed.glyphs) { + hash = mixInt(hash, member.cp); + hash = mixInt(hash, member.dx); + } + } + hash = mixInt(hash, placed.x); + hash = mixInt(hash, placed.y); + } + return hash >>> 0; + } + + /** + * FNV-1a over the raster pixels. Four bytes at a time where alignment + * allows: this runs on every frame's fingerprint, over the whole screen, on + * the thread that is about to hand the frame to the transport. + */ + private pixelsHash32(): number { + const pixels = this.pixels; let hash = 2166136261; - for (let i = 0; i < this.pixels.length; i++) { - hash ^= this.pixels[i]!; + let i = 0; + if ((pixels.byteOffset & 3) === 0) { + const words = new Uint32Array(pixels.buffer, pixels.byteOffset, pixels.length >>> 2); + for (let w = 0; w < words.length; w++) { + hash ^= words[w]!; + hash = Math.imul(hash, 16777619); + } + i = words.length << 2; + } + for (; i < pixels.length; i++) { + hash ^= pixels[i]!; hash = Math.imul(hash, 16777619); } - return `fnv:${(hash >>> 0).toString(16)}`; + return hash >>> 0; + } + + /** + * contentHash32 for an image being used as another image's deferred-draw + * source, memoized. drawImage/drawFwText document their source as immutable + * from the call onwards (glyph-wire's ImageAtlas id cache already depends on + * that), so the hash of a long-lived icon asset can be computed once instead + * of on every frame that draws it. + */ + sourceContentHash32(): number { + if (this.memoizedSourceHash === null) { + this.memoizedSourceHash = this.contentHash32(); + } + return this.memoizedSourceHash; + } + + fingerprint(): string { + return `fnv:${this.contentHash32().toString(16)}`; } /** * Snapshot of the full image as a single 8-bit-per-pixel grayscale buffer, - * row-major and top-to-bottom. The Java side turns this into whatever wire - * format the firmware currently expects (today: a 4bpp BMP), so all framing - * concerns stay on one side of the bridge. + * row-major and top-to-bottom, with any deferred draws baked in. The Java + * side turns this into whatever wire format the firmware currently expects + * (today: a 4bpp BMP), so all framing concerns stay on one side of the + * bridge. */ to8bppBuffer(): Uint8Array { - return Uint8Array.from(this.pixels); + if (!this.drawList.length) { + // slice() is a memcpy; Uint8Array.from walks the buffer element by element. + return this.pixels.slice(); + } + // withDrawsBaked's buffer is a private temporary, safe to hand out. + return this.withDrawsBaked().pixels; } - private drawGlyph(font: BdfFont, glyph: Glyph, x: number, y: number, value: number): void { - const baselineY = y + font.ascent; - const top = baselineY - (glyph.bbxHeight + glyph.bbxY); - const left = x + glyph.bbxX; - const rowBitWidth = ((glyph.bbxWidth + 7) >> 3) << 3; + /** + * Record a glyph draw. Deferred: the glyph joins the image's draw list + * (rendered above all raster pixels) instead of being baked into the + * buffer. + */ + drawGlyph(font: GlyphFont, glyph: Glyph, x: number, y: number, value: number): void { + this.drawList.push({ kind: "glyph", font, glyph, x, y, value: clampByte(value) }); + } + + /** + * Record a color-key image blit (source value 0 = transparent), deferred + * like glyph draws so icons keep their identity to the wire encoder. The + * source is snapshot-flattened (its own deferred draws baked) and must not + * be mutated afterwards — pass long-lived rendered assets (icons), not + * scratch buffers. For a clipped or mutable blit, use bitBlt instead. + */ + drawImage(source: GrayImage, x: number, y: number): void { + if (source.width <= 0 || source.height <= 0) return; + this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y }); + } + + /** + * Record a firmware-font text run (see PlacedFwText). Called by + * EvenHubFont, which owns run construction; the members array must be + * immutable from here on. + */ + drawFwText( + font: FwTextFont, + x: number, + y: number, + value: number, + glyphs: readonly PlacedFwTextGlyph[], + ): void { + if (glyphs.length === 0) return; + this.drawList.push({ kind: "fwtext", font, x, y, value: clampByte(value), glyphs }); + } + + /** + * Bake this image's deferred draws into its own pixels and clear the list. + * Used where later raster must be able to cover earlier draws within the + * same image (e.g. the EvenHub compositor's image-over-text z-order). + */ + bakeDeferredDrawsInPlace(): void { + if (!this.drawList.length) return; + const draws = this.drawList.splice(0, this.drawList.length); + for (const placed of draws) { + if (placed.kind === "glyph") { + rasterizeGlyph(this, placed.font, placed.glyph, placed.x, placed.y, placed.value); + } else if (placed.kind === "image") { + this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true }); + } else { + placed.font.bakeFwTextRun(this, placed, 0, 0); + } + } + } + + /** + * This image with its deferred draws rasterized into the pixel buffer. + * Returns this image unchanged when there are none, otherwise a baked copy + * with an empty draw list. + */ + withDrawsBaked(): GrayImage { + if (!this.drawList.length) return this; + const baked = new GrayImage(this.width, this.height, 0); + baked.pixels.set(this.pixels); + this.bakeDrawsInto(baked, 0, 0); + return baked; + } + + /** Rasterize this image's deferred draws into target, translated by (dx, dy). */ + bakeDrawsInto(target: GrayImage, dx: number, dy: number): void { + for (const placed of this.drawList) { + if (placed.kind === "glyph") { + rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value); + } else if (placed.kind === "image") { + target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true }); + } else { + placed.font.bakeFwTextRun(target, placed, dx, dy); + } + } + } +} + +/** + * The 4bpp level an 8-bit gray value packs to (BmpUtil's GRAY_TO_NIBBLE). + * Shared by every phone-side bake that must match the firmware's draw-time + * LUT math bit-for-bit (AA glyphs here, EvenHubFont's mode-15 bakes). + */ +export function grayToNibble(value: number): number { + return value <= 0 ? 0 : Math.min(15, (value + 8) >> 4); +} + +/** Bake one glyph into an image's pixel buffer (y is the top of the line). */ +function rasterizeGlyph( + target: GrayImage, + font: GlyphFont, + glyph: Glyph, + x: number, + y: number, + value: number, +): void { + const baselineY = y + font.ascent; + const top = baselineY - (glyph.bbxHeight + glyph.bbxY); + const left = x + glyph.bbxX; + if (glyph.coverage) { + // Firmware-exact AA shading (see EvenHubFont.drawGlyph): the CFW draws + // cached glyphs through a LUT mapping source nibble n to n*top/15 + // (integer), skipping only n == 0. Writing 16*level makes the composite + // quantize (grayToNibble) back to exactly that level, so the + // texture-cache planner's pixel checks — and the shadow after an + // on-glasses mode-14 draw — match this bake bit-for-bit. + const top4 = grayToNibble(value); for (let row = 0; row < glyph.bbxHeight; row++) { - const bits = glyph.bitmapRows[row] ?? 0; + const rowStart = row * glyph.bbxWidth; for (let col = 0; col < glyph.bbxWidth; col++) { - const shift = rowBitWidth - 1 - col; - if (((bits >> shift) & 1) !== 0) { - this.setPixelUnchecked(left + col, top + row, value); - } + const nibble = glyph.coverage[rowStart + col]!; + if (nibble === 0) continue; + target.setPixel(left + col, top + row, Math.floor((nibble * top4) / 15) * 16); + } + } + return; + } + const rowBitWidth = ((glyph.bbxWidth + 7) >> 3) << 3; + for (let row = 0; row < glyph.bbxHeight; row++) { + const bits = glyph.bitmapRows[row] ?? 0; + for (let col = 0; col < glyph.bbxWidth; col++) { + const shift = rowBitWidth - 1 - col; + if (((bits >> shift) & 1) !== 0) { + target.setPixel(left + col, top + row, value); } } } @@ -285,6 +602,12 @@ function clampByte(value: number): number { return Math.max(0, Math.min(255, Math.round(value))); } +/** Fold a 32-bit int into an FNV-style running hash. */ +function mixInt(hash: number, value: number): number { + hash ^= value | 0; + return Math.imul(hash, 16777619); +} + function roundedRectRowSpan( row: number, x: number, diff --git a/app/graphics/installed-fonts.ts b/app/graphics/installed-fonts.ts new file mode 100644 index 00000000..14d7259a --- /dev/null +++ b/app/graphics/installed-fonts.ts @@ -0,0 +1,237 @@ +/** + * The app-internal installed-fonts directory: the set of TTF/OTF faces the + * font picker offers. Fonts arrive two ways — bundled faces copied out of the + * app assets on first listing (so the directory is the single source of + * truth), and user installs from the Files app ("Install" on a font file). + * + * Listing parses each file's name table for family/style (via the Java + * FontFileRenderer) and probes monospace-ness by comparing advances, both + * memoized per path for the session. + */ +import { File, Folder, knownFolders, path as nsPath } from "@nativescript/core"; +import { canLoadFontFile, fontFileDisplayName, isFontFile } from "../native/font-files"; + +declare const com: any; +declare const java: any; +declare const global: any; + +/** Bundled faces (under app assets fonts/ttf/) preinstalled on first listing. */ +const PREINSTALLED_FONT_FILES = [ + "Inter_18pt-Light.ttf", + "Inter_18pt-Regular.ttf", + "Inter_18pt-Bold.ttf", + "Montserrat-Light.ttf", + "Montserrat-Regular.ttf", + "Montserrat-Bold.ttf", + "Roboto-Light.ttf", + "Roboto-Regular.ttf", + "Roboto-Bold.ttf", + "RobotoMono-Light.ttf", + "RobotoMono-Regular.ttf", + "RobotoMono-Bold.ttf", +] as const; + +export type InstalledFont = { + /** Absolute path, loadable by TtfFont / FontFileRenderer. */ + path: string; + fileName: string; + /** Family name from the font's name table (filename stem as fallback). */ + family: string; + /** Style name from the name table ("Light", "Bold", ... ; "" = regular). */ + style: string; + /** Sort key for weight pickers (400 = regular; +1000 marks italics last). */ + weightOrder: number; + monospace: boolean; +}; + +/** The app-internal fonts directory (created on first use). */ +export function installedFontsDir(): string { + return knownFolders.documents().getFolder("fonts").path; +} + +/** Absolute path of an installed font by filename (existence not checked). */ +export function installedFontPath(fileName: string): string { + return nsPath.join(installedFontsDir(), fileName); +} + +let preinstalledEnsured = false; + +/** + * Copy any missing bundled faces into the installed-fonts directory. Cheap + * after the first run (existence checks only), so callers can invoke it + * before every listing. + */ +export function ensurePreinstalledFonts(): void { + if (preinstalledEnsured || !global.isAndroid) return; + const assetsDir = nsPath.join(knownFolders.currentApp().path, "fonts/ttf"); + for (const fileName of PREINSTALLED_FONT_FILES) { + const target = installedFontPath(fileName); + if (File.exists(target)) continue; + const source = nsPath.join(assetsDir, fileName); + if (!File.exists(source)) continue; + try { + copyFileBytes(source, target); + } catch (error) { + console.warn(`Could not preinstall font ${fileName}: ${error}`); + } + } + preinstalledEnsured = true; +} + +/** + * Install a font file into the installed-fonts directory (overwriting a + * same-named file). Returns an error message for the UI, or null on success. + */ +export function installFontFile(sourcePath: string): string | null { + if (!global.isAndroid) return "Not available on this platform."; + if (!isFontFile(sourcePath)) return "Not a font file."; + if (!canLoadFontFile(sourcePath)) return "This file could not be loaded as a font."; + const fileName = sourcePath.slice(sourcePath.lastIndexOf("/") + 1); + try { + copyFileBytes(sourcePath, installedFontPath(fileName)); + metadataCache.delete(installedFontPath(fileName)); + return null; + } catch (error) { + console.warn(`installFontFile failed for ${sourcePath}: ${error}`); + return "Could not copy the font file."; + } +} + +/** Whether a same-named font is already installed. */ +export function isFontInstalled(sourcePath: string): boolean { + const fileName = sourcePath.slice(sourcePath.lastIndexOf("/") + 1); + return File.exists(installedFontPath(fileName)); +} + +/** + * All installed fonts (preinstalling bundled faces first), sorted by family + * then weight. Unloadable files are skipped. + */ +export function listInstalledFonts(): InstalledFont[] { + if (!global.isAndroid) return []; + ensurePreinstalledFonts(); + const fonts: InstalledFont[] = []; + try { + for (const entity of Folder.fromPath(installedFontsDir()).getEntitiesSync()) { + if (!isFontFile(entity.name)) continue; + const meta = fontMetadata(entity.path, entity.name); + if (meta) fonts.push(meta); + } + } catch (error) { + console.warn(`listInstalledFonts failed: ${error}`); + } + fonts.sort((a, b) => + a.family.localeCompare(b.family) || a.weightOrder - b.weightOrder || a.style.localeCompare(b.style), + ); + return fonts; +} + +/** The installed font with this filename, or null. */ +export function getInstalledFont(fileName: string): InstalledFont | null { + const path = installedFontPath(fileName); + if (!File.exists(path)) return null; + return fontMetadata(path, fileName); +} + +const metadataCache = new Map(); + +function fontMetadata(path: string, fileName: string): InstalledFont | null { + const cached = metadataCache.get(path); + if (cached !== undefined) return cached; + let meta: InstalledFont | null = null; + if (canLoadFontFile(path)) { + const name = fontFileDisplayName(path); + const family = name?.family || fileName.replace(/\.[^.]+$/, ""); + const style = normalizeStyle(name?.style ?? ""); + meta = { + path, + fileName, + family, + style, + weightOrder: styleWeightOrder(style), + monospace: isMonospaceFont(path), + }; + } + metadataCache.set(path, meta); + return meta; +} + +function normalizeStyle(style: string): string { + const trimmed = style.trim(); + return trimmed === "Regular" ? "" : trimmed; +} + +/** OS/2-style weight for a style name; italics sort after all uprights. */ +function styleWeightOrder(style: string): number { + const withoutItalic = style.replace(/\bitalic\b/i, "").trim().toLowerCase(); + const weights: Record = { + "thin": 100, + "extralight": 200, + "extra light": 200, + "ultralight": 200, + "light": 300, + "": 400, + "regular": 400, + "normal": 400, + "medium": 500, + "semibold": 600, + "semi bold": 600, + "demibold": 600, + "bold": 700, + "extrabold": 800, + "extra bold": 800, + "black": 900, + "heavy": 900, + }; + const weight = weights[withoutItalic] ?? 400; + return /\bitalic\b/i.test(style) ? weight + 1000 : weight; +} + +/** + * Whether the face is monospace: narrow, wide, and punctuation glyphs all + * measure the same advance. Measured at a large size so fractional advance + * differences show up clearly. + */ +function isMonospaceFont(path: string): boolean { + try { + const measure = (text: string) => + Number(com.faceclaw.app.FontFileRenderer.measureTextExact(path, text, 48)); + const narrow = measure("i"); + if (!(narrow > 0)) return false; + return ( + Math.abs(measure("W") - narrow) < 0.5 && + Math.abs(measure("m") - narrow) < 0.5 && + Math.abs(measure(".") - narrow) < 0.5 + ); + } catch { + return false; + } +} + +// Plain stream/channel copy: java.nio.file.Paths.get is a varargs overload +// set that NativeScript's JNI overload resolution picks wrong (it tries the +// (URI) form for a JS string), so java.nio.file is avoided here. +// +// Written to a temp name and renamed into place (atomic on the same +// filesystem): preinstallation can be triggered concurrently from several JS +// isolates on first launch (each resolving the default UI font), and readers +// must never see a half-written font file. +function copyFileBytes(source: string, target: string): void { + const temp = `${target}.tmp-${Math.floor(Math.random() * 0x7fffffff).toString(36)}`; + const input = new java.io.FileInputStream(source); + try { + const output = new java.io.FileOutputStream(temp); + try { + const inChannel = input.getChannel(); + output.getChannel().transferFrom(inChannel, 0, inChannel.size()); + } finally { + output.close(); + } + } finally { + input.close(); + } + if (!new java.io.File(temp).renameTo(new java.io.File(target))) { + new java.io.File(temp).delete(); + throw new Error(`rename to ${target} failed`); + } +} diff --git a/app/graphics/plane.ts b/app/graphics/plane.ts new file mode 100644 index 00000000..e29ff88f --- /dev/null +++ b/app/graphics/plane.ts @@ -0,0 +1,100 @@ +import { GrayImage, type DeferredDraw } from "./image"; + +/** + * The unit apps (and the shell) submit to the compositor: instead of one + * flattened image, a frame is an ordered array of planes, each an image plus + * placement metadata. Planes composite in array order, first at the bottom, + * with the shell's transparency convention: an 8-bit value of 0 is + * transparent, 1 is black (the two quantize to the same 4bpp shade, so + * reserving 0 costs nothing visible). + * + * This representation exists to carry structure the wire pipeline will later + * exploit: a plane's glyph list can become on-glasses display-list references + * (texture cache), and the offset can diverge per lens for stereo depth. For + * now the compositor boundary just flattens (flattenPlanes) and feeds the + * existing incremental-update and screen-preview pipeline. + */ +export type Plane = { + image: GrayImage; + /** + * Offset of the plane's top-left within the submitted frame. Applied + * identically to both lenses for now; a per-lens divergence (stereo depth + * for floating overlays) is the planned extension. + */ + x: number; + y: number; +}; + +/** Wrap a single full-frame image as a one-plane submission. */ +export function singlePlane(image: GrayImage): Plane[] { + return [{ image, x: 0, y: 0 }]; +} + +/** + * Flatten planes into one image: for each plane in order, blend its raster + * (0 = transparent) and then its glyphs on top. A later plane's raster covers + * an earlier plane's glyphs — this is what lets an overlay plane occlude text + * beneath it, which raster drawn into the *same* image cannot do (glyphs + * always render above their own image's raster). + * + * The output size defaults to the planes' joint extent (in practice the base + * plane's size, since overlays share its dimensions). + */ +export function flattenPlanes( + planes: readonly Plane[], + size?: { width: number; height: number }, +): GrayImage { + const width = size?.width ?? Math.max(1, ...planes.map((plane) => plane.x + plane.image.width)); + const height = size?.height ?? Math.max(1, ...planes.map((plane) => plane.y + plane.image.height)); + + const only = planes.length === 1 ? planes[0]! : undefined; + if (only && only.x === 0 && only.y === 0 && only.image.width === width && only.image.height === height) { + return only.image.withDrawsBaked(); + } + + const target = new GrayImage(width, height, 0); + for (const plane of planes) { + // Baking first (rather than bitBlt's glyph carry-over) is what enforces + // the plane ordering: the next plane's raster must be able to cover this + // plane's glyphs. + target.bitBlt(plane.image.withDrawsBaked(), plane.x, plane.y, { transparentZero: true }); + } + return target; +} + +/** + * Flatten planes and also return the frame's deferred draws (glyphs and + * images), translated into frame coordinates and in bake order (plane order, + * then each image's draw order). The flattened image has every draw baked in, + * exactly as flattenPlanes produces; the list preserves the draws' identity + * so the texture-cache pipeline can replay them as on-glasses cached draws + * (see graphics/glyph-wire.ts). + */ +export function flattenPlanesWithDraws( + planes: readonly Plane[], + size?: { width: number; height: number }, +): { image: GrayImage; draws: DeferredDraw[] } { + const image = flattenPlanes(planes, size); + const draws: DeferredDraw[] = []; + for (const plane of planes) { + for (const placed of plane.image.draws) { + draws.push( + plane.x === 0 && plane.y === 0 + ? placed + : { ...placed, x: placed.x + plane.x, y: placed.y + plane.y }, + ); + } + } + return { image, draws }; +} + +/** + * Stable identifier for a plane stack's composited content: equal + * fingerprints mean an equal flattened frame. Feeds the same dedupe paths + * that single-image fingerprints did. + */ +export function planesFingerprint(planes: readonly Plane[]): string { + return planes + .map((plane) => `${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`) + .join("|"); +} diff --git a/app/graphics/textwrap.ts b/app/graphics/textwrap.ts index 8a9df153..3d2d4ff5 100644 --- a/app/graphics/textwrap.ts +++ b/app/graphics/textwrap.ts @@ -1,4 +1,13 @@ -import { BdfFont } from "./bdffont"; +/** + * What wrapping and truncation need from a font. BdfFont and TtfFont both + * satisfy it structurally: getGlyph feeds per-codepoint advances for wrap + * measurement (integer and kerning-free, so slightly conservative for kerned + * faces), measureText feeds truncation. + */ +export interface WrapFont { + measureText(text: string): number; + getGlyph(codePoint: number): { dwidthX: number } | undefined; +} export type WrapTextOptions = { preserveLeadingWhitespace?: boolean; @@ -34,7 +43,7 @@ export function findWrapOpportunities(text: string): number[] { return offsets; } -export function wrapText(font: BdfFont, text: string, targetWidth: number, opts: WrapTextOptions = {}): string[] { +export function wrapText(font: WrapFont, text: string, targetWidth: number, opts: WrapTextOptions = {}): string[] { if (targetWidth <= 0) { return text.length ? text.split("\n") : [""]; } @@ -47,7 +56,7 @@ export function wrapText(font: BdfFont, text: string, targetWidth: number, opts: return lines.length ? lines : [""]; } -export function truncateText(font: BdfFont, text: string, maxWidth: number): string { +export function truncateText(font: WrapFont, text: string, maxWidth: number): string { if (font.measureText(text) <= maxWidth) return text; let out = text; while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) { @@ -56,7 +65,7 @@ export function truncateText(font: BdfFont, text: string, maxWidth: number): str return `${out}...`; } -export function truncateLeft(font: BdfFont, text: string, maxWidth: number): string { +export function truncateLeft(font: WrapFont, text: string, maxWidth: number): string { if (font.measureText(text) <= maxWidth) return text; let out = text; while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) { @@ -65,7 +74,7 @@ export function truncateLeft(font: BdfFont, text: string, maxWidth: number): str return `...${out}`; } -function wrapParagraph(font: BdfFont, paragraph: string, targetWidth: number, opts: WrapTextOptions): string[] { +function wrapParagraph(font: WrapFont, paragraph: string, targetWidth: number, opts: WrapTextOptions): string[] { if (paragraph.length === 0) { return [""]; } @@ -109,7 +118,7 @@ function wrapParagraph(font: BdfFont, paragraph: string, targetWidth: number, op return lines.length ? lines : [""]; } -function measureOffsets(font: BdfFont, text: string): Map { +function measureOffsets(font: WrapFont, text: string): Map { const widths = new Map(); let total = 0; widths.set(0, 0); diff --git a/app/graphics/ttf-font.ts b/app/graphics/ttf-font.ts new file mode 100644 index 00000000..3118235e --- /dev/null +++ b/app/graphics/ttf-font.ts @@ -0,0 +1,272 @@ +/** + * TTF/OTF fonts for on-glasses UI text, rendered per-codepoint through the + * Java FontFileRenderer (Android minikin/HarfBuzz: real shaping, kerning, + * antialiasing) and cached as 4bpp coverage rasters that ride the + * texture-cache glyph pipeline (CFW mode 14; see + * notes/texture-cache-display-list-design.md and graphics/glyph-wire.ts). + * + * Layout model: the font owns drawText (like EvenHubFont) because TTF + * advances are fractional and kerned, which GrayImage.drawText's integer + * cursor walk can't express. Each glyph is recorded as a deferred + * PlacedGlyph at an integer pen x, so the wire path and the phone-side bake + * (rasterizeGlyph's coverage branch, firmware-LUT-exact) see identical + * positions; the fractional pen accumulates across the line so rounding + * never drifts. + * + * Kerning uses memoized per-pair measurements (measure("ab") - a - b); + * ligatures are disabled Java-side so per-codepoint rasters plus pairwise + * kerning fully describe a line. Glyph rasters and advances come from one + * JNI render per (font, size, codepoint) per JS context, memoized forever. + * + * Cross-context determinism: worker threads and the main thread each build + * their own TtfFont, but the Java GlyphAtlas dedupes by atlasKey and treats + * rasters as immutable — safe because renderGlyphCell is deterministic for a + * given (path, size, gamma), all of which are baked into the atlasKey. + */ +import { allocateFontFingerprintId, type Glyph } from "./bdffont"; +import { GrayImage, grayToNibble, type GlyphFont } from "./image"; +import { toUint8Array } from "../util/array-util"; + +declare const com: any; +declare const global: any; + +/** + * Coverage-to-shade gamma (out = coverage^gamma). The G2 display response + * looks roughly linear, so 1.0; part of the atlas identity, so changing it + * per-font is safe but changing it per-draw is not possible. + */ +const DEFAULT_TTF_GAMMA = 1.0; + +const loadedFonts = new Map(); + +export class TtfFont implements GlyphFont { + readonly fingerprintId = allocateFontFingerprintId(); + /** Stable cross-context atlas identity: face + size + gamma. */ + readonly atlasKey: string; + readonly ascent: number; + readonly descent: number; + readonly lineHeight: number; + private readonly glyphCache = new Map(); + private readonly kernCache = new Map(); + + private constructor( + readonly path: string, + readonly sizePx: number, + readonly gamma: number, + ascent: number, + descent: number, + ) { + this.ascent = ascent; + this.descent = descent; + this.lineHeight = ascent + descent; + const basename = path.slice(path.lastIndexOf("/") + 1).replace(/\.[^.]+$/, ""); + this.atlasKey = `ttf:${basename}:${fnv32(path).toString(36)}@${sizePx}g${gamma}`; + } + + /** + * Load (or return the memoized) font at a pixel size. Null when the font + * file is missing/unloadable or off-Android — callers fall back to a BDF + * face. + */ + static load(path: string, sizePx: number, gamma = DEFAULT_TTF_GAMMA): TtfFont | null { + const key = `${path}@${sizePx}g${gamma}`; + const cached = loadedFonts.get(key); + if (cached !== undefined) return cached; + let font: TtfFont | null = null; + if (global.isAndroid && sizePx > 0) { + try { + const raw = String(com.faceclaw.app.FontFileRenderer.getFontMetrics(path, sizePx) ?? ""); + const parts = raw.split(" ").map((part) => parseInt(part, 10)); + if (parts.length === 3 && parts.every((part) => Number.isFinite(part))) { + const [ascent, descent] = parts as [number, number, number]; + if (ascent + descent > 0 && ascent + descent <= 255) { + font = new TtfFont(path, sizePx, gamma, ascent, descent); + } + } + } catch (error) { + console.warn(`TtfFont.load failed for ${path}: ${error}`); + } + } + loadedFonts.set(key, font); + return font; + } + + /** + * The codepoint's glyph (rendered and memoized on first use), or undefined + * when the render fails outright. Android renders SOMETHING for any + * codepoint the face covers or tofu otherwise, so undefined is rare. + * Ink-free glyphs (space) have bbxWidth 0 and no coverage. + */ + getGlyph(codePoint: number): Glyph | undefined { + let glyph = this.glyphCache.get(codePoint); + if (glyph === undefined) { + glyph = this.loadGlyph(codePoint); + this.glyphCache.set(codePoint, glyph); + } + return glyph ?? undefined; + } + + hasGlyph(codePoint: number): boolean { + return this.getGlyph(codePoint) !== undefined; + } + + /** + * Pixel width of the widest line in `text` (fractional; kerning included), + * for wrapping and truncation. + */ + measureText(text: string): number { + const cps = toCodePoints(text); + let width = 0; + let maxWidth = 0; + for (let i = 0; i < cps.length; i++) { + const cp = cps[i]!; + if (cp === 10) { + maxWidth = Math.max(maxWidth, width); + width = 0; + continue; + } + width += this.advanceOf(cp); + const next = cps[i + 1]; + if (next !== undefined && next !== 10) { + width += this.kernBetween(cp, next); + } + } + return Math.max(maxWidth, width); + } + + /** + * Draw text with the font's own (fractional, kerned) layout, recording + * each inked glyph as a deferred draw on the image. y is the top of the + * line (baseline at y + ascent), matching GrayImage.drawText. + */ + drawText(image: GrayImage, x: number, y: number, text: string, value: number): void { + const cps = toCodePoints(text); + let penX = x; + let lineY = y; + for (let i = 0; i < cps.length; i++) { + const cp = cps[i]!; + if (cp === 10) { + penX = x; + lineY += this.lineHeight; + continue; + } + const glyph = this.getGlyph(cp); + if (!glyph) continue; + if (glyph.coverage && glyph.bbxWidth > 0) { + image.drawGlyph(this, glyph, Math.round(penX), lineY, value); + } + penX += glyph.advance ?? glyph.dwidthX; + const next = cps[i + 1]; + if (next !== undefined && next !== 10) { + penX += this.kernBetween(cp, next); + } + } + } + + private advanceOf(codePoint: number): number { + const glyph = this.getGlyph(codePoint); + return glyph ? (glyph.advance ?? glyph.dwidthX) : 0; + } + + /** + * Kerning between two adjacent codepoints: measure(ab) - a - b, memoized. + * One JNI measurement per distinct pair per JS context. + */ + private kernBetween(a: number, b: number): number { + const key = a * 0x200000 + b; + const cached = this.kernCache.get(key); + if (cached !== undefined) return cached; + let kern = 0; + try { + const pair = Number( + com.faceclaw.app.FontFileRenderer.measureTextExact( + this.path, + String.fromCodePoint(a) + String.fromCodePoint(b), + this.sizePx, + ), + ); + if (pair > 0) { + kern = pair - this.advanceOf(a) - this.advanceOf(b); + // A kern larger than the em size means the decomposition failed + // (e.g. the pair shaped into something we can't represent). + if (!Number.isFinite(kern) || Math.abs(kern) > this.sizePx) kern = 0; + } + } catch { + kern = 0; + } + this.kernCache.set(key, kern); + return kern; + } + + /** Render + quantize one glyph via renderGlyphCell (see FontFileRenderer). */ + private loadGlyph(codePoint: number): Glyph | null { + let bytes: Uint8Array; + try { + bytes = toUint8Array( + com.faceclaw.app.FontFileRenderer.renderGlyphCell(this.path, this.sizePx, codePoint, this.gamma), + ); + } catch (error) { + console.warn(`renderGlyphCell failed for U+${codePoint.toString(16)}: ${error}`); + return null; + } + if (bytes.length < 10) return null; + const u16 = (offset: number) => bytes[offset]! | (bytes[offset + 1]! << 8); + const s16 = (offset: number) => { + const value = u16(offset); + return value & 0x8000 ? value - 0x10000 : value; + }; + const advance = u16(0) / 64; + const bearingX = s16(2); + const inkTop = s16(4); + const width = u16(6); + const height = u16(8); + const base: Glyph = { + encoding: codePoint, + dwidthX: Math.round(advance), + bbxWidth: 0, + bbxHeight: 0, + bbxX: 0, + bbxY: 0, + bitmapRows: [], + advance, + }; + if (width <= 0 || height <= 0 || bytes.length < 10 + width * height) { + return base; // ink-free (space) or malformed: advance-only + } + // Quantize 8-bit coverage to the 4bpp levels the composite packs to; + // faint fringe (< 8) quantizes to 0 = transparent. + const coverage = new Uint8Array(width * height); + let hasInk = false; + for (let i = 0; i < coverage.length; i++) { + const nibble = grayToNibble(bytes[10 + i]!); + coverage[i] = nibble; + if (nibble !== 0) hasInk = true; + } + if (!hasInk) return base; + return { + ...base, + bbxWidth: width, + bbxHeight: height, + bbxX: bearingX, + bbxY: this.ascent - inkTop - height, + coverage, + }; + } +} + +function toCodePoints(text: string): number[] { + const cps: number[] = []; + for (const char of text) { + cps.push(char.codePointAt(0)!); + } + return cps; +} + +function fnv32(text: string): number { + let hash = 2166136261; + for (let i = 0; i < text.length; i++) { + hash ^= text.charCodeAt(i); + hash = Math.imul(hash, 16777619); + } + return hash >>> 0; +} diff --git a/app/graphics/ui-fonts.ts b/app/graphics/ui-fonts.ts new file mode 100644 index 00000000..bcca0c53 --- /dev/null +++ b/app/graphics/ui-fonts.ts @@ -0,0 +1,228 @@ +/** + * The user-selectable UI fonts: resolves the font settings (chosen in the + * Settings app's font picker) to concrete BdfFont/TtfFont instances for the + * three UI size roles, plus the terminal's separate monospace font. + * + * Selections are stored as one JSON value per setting so a font/size change + * is atomic. Resolution is cached per isolate and invalidated by the settings + * broadcast, matching the old getDefaultSmallFont pattern (these getters are + * called per menu row per paint). + */ +import { getFont, type BdfFont } from "./bdffont"; +import type { UiFont } from "./image"; +import { TtfFont } from "./ttf-font"; +import { ensurePreinstalledFonts, getInstalledFont, installedFontPath } from "./installed-fonts"; +import { getStringSetting, onSettingsStoreChanged, setStringSetting } from "../native/settings-store"; + +export type BitmapFace = "terminus" | "terminusv"; + +export type UiFontSelection = + | { kind: "bitmap"; face: BitmapFace } + | { kind: "ttf"; file: string; size: number }; + +export const UI_FONT_SELECTION_KEY = "display.uiFont2"; +export const TERMINAL_FONT_SELECTION_KEY = "terminal.font"; +/** Pre-picker setting ("terminus" | "terminusv"), read as a migration default. */ +const LEGACY_UI_FONT_KEY = "display.uiFont"; + +/** Line-height floors for the medium/large UI font roles (bitmap: 16/24px faces). */ +const MEDIUM_MIN_LINE_HEIGHT = 20; +const LARGE_MIN_LINE_HEIGHT = 26; +/** How far size can grow hunting a line-height floor before giving up. */ +const MAX_SIZE_GROWTH = 48; + +/** + * Guaranteed line-height range of getDefaultSmallFont, which UI layouts may + * rely on (getDefaultMediumFont's range follows: 16..29). The font picker + * disables sizes outside it, and resolution falls back to the bitmap face if + * a stored selection somehow lands outside (e.g. the font file changed). + */ +export const SMALL_FONT_MIN_LINE_HEIGHT = 12; +export const SMALL_FONT_MAX_LINE_HEIGHT = 21; + +/** Whether a small-font selection at this path+size satisfies the UI bound. */ +export function uiFontSizeAllowed(path: string, size: number): boolean { + ensurePreinstalledFonts(); // the default selection may precede any picker use + const font = TtfFont.load(path, size); + return ( + font !== null && + font.lineHeight >= SMALL_FONT_MIN_LINE_HEIGHT && + font.lineHeight <= SMALL_FONT_MAX_LINE_HEIGHT + ); +} + +export function parseFontSelection(raw: string): UiFontSelection | null { + try { + const parsed: unknown = JSON.parse(raw); + if (typeof parsed !== "object" || parsed === null) return null; + const sel = parsed as Record; + if (sel.kind === "bitmap" && (sel.face === "terminus" || sel.face === "terminusv")) { + return { kind: "bitmap", face: sel.face }; + } + if ( + sel.kind === "ttf" && + typeof sel.file === "string" && sel.file.length > 0 && + typeof sel.size === "number" && Number.isFinite(sel.size) && sel.size >= 6 && sel.size <= 64 + ) { + return { kind: "ttf", file: sel.file, size: Math.round(sel.size) }; + } + } catch { + // fall through + } + return null; +} + +/** Default UI font when the user has never picked one. */ +const DEFAULT_UI_FONT: UiFontSelection = { kind: "ttf", file: "Roboto-Light.ttf", size: 14 }; + +export function getUiFontSelection(): UiFontSelection { + const parsed = parseFontSelection(getStringSetting(UI_FONT_SELECTION_KEY, "")); + if (parsed) return parsed; + // A pre-picker-era explicit bitmap choice is honored; fresh states get the + // bundled default. + const legacy = getStringSetting(LEGACY_UI_FONT_KEY, ""); + if (legacy === "terminus" || legacy === "terminusv") { + return { kind: "bitmap", face: legacy }; + } + return DEFAULT_UI_FONT; +} + +export function setUiFontSelection(selection: UiFontSelection): void { + setStringSetting(UI_FONT_SELECTION_KEY, JSON.stringify(selection)); +} + +export function getTerminalFontSelection(): UiFontSelection { + return parseFontSelection(getStringSetting(TERMINAL_FONT_SELECTION_KEY, "")) ?? { kind: "bitmap", face: "terminus" }; +} + +export function setTerminalFontSelection(selection: UiFontSelection): void { + setStringSetting(TERMINAL_FONT_SELECTION_KEY, JSON.stringify(selection)); +} + +/** Human label for a selection, e.g. "Inter Light 16" or "Terminus". */ +export function fontSelectionLabel(selection: UiFontSelection): string { + if (selection.kind === "bitmap") { + return selection.face === "terminusv" ? "TerminusV" : "Terminus"; + } + const installed = getInstalledFont(selection.file); + const face = installed ? `${installed.family}${installed.style ? ` ${installed.style}` : ""}` : selection.file; + return `${face} ${selection.size}`; +} + +// --- Resolution, cached per isolate --- + +const resolved = new Map(); +let invalidationInstalled = false; + +function cacheKey(role: string): string { + return role; +} + +function installInvalidation(): void { + if (invalidationInstalled) return; + invalidationInstalled = true; + onSettingsStoreChanged((key) => { + if (key === UI_FONT_SELECTION_KEY || key === LEGACY_UI_FONT_KEY || key === TERMINAL_FONT_SELECTION_KEY) { + resolved.clear(); + cachedTerminalConfig = null; + } + }); +} + +/** + * The selection's TTF at a size grown until lineHeight reaches minLineHeight + * (never shrunk below the picked size), or null when the file can't load. + */ +function resolveTtf(selection: UiFontSelection, minLineHeight = 0): TtfFont | null { + if (selection.kind !== "ttf") return null; + // The default selection can be resolved before the picker (whose listing + // preinstalls bundled faces) has ever been opened, so make sure the + // bundled files exist first. Cheap after the first call per isolate. + ensurePreinstalledFonts(); + const path = installedFontPath(selection.file); + for (let size = selection.size; size <= selection.size + MAX_SIZE_GROWTH; size++) { + const font = TtfFont.load(path, size); + if (!font) return null; + if (font.lineHeight >= minLineHeight) return font; + } + return null; +} + +function resolveRole(role: "small" | "medium" | "large"): UiFont { + installInvalidation(); + const key = cacheKey(role); + const cached = resolved.get(key); + if (cached) return cached; + const selection = getUiFontSelection(); + let font: UiFont | null = null; + // Enforce the guaranteed small-font range even against a stale or + // hand-edited selection: an out-of-range selection falls back to bitmap for + // every role (medium/large derive from small, so their documented ranges + // follow and the roles never mix faces). + if (selection.kind === "ttf" && uiFontSizeAllowed(installedFontPath(selection.file), selection.size)) { + font = + role === "small" + ? resolveTtf(selection) + : resolveTtf(selection, role === "medium" ? MEDIUM_MIN_LINE_HEIGHT : LARGE_MIN_LINE_HEIGHT); + } + if (!font) { + font = bitmapForRole(selection.kind === "bitmap" ? selection.face : "terminus", role); + } + resolved.set(key, font); + return font; +} + +// Only the small (12px) bitmap font has a TerminusV alternative (it ships no +// larger sizes); bitmap medium/large stay Terminus, as before the picker. +function bitmapForRole(face: BitmapFace, role: "small" | "medium" | "large"): BdfFont { + if (role === "medium") return getFont("terminus16"); + if (role === "large") return getFont("terminus24"); + return getFont(face === "terminusv" ? "terminusv12" : "terminus12"); +} + +export function getDefaultSmallFont(): UiFont { + return resolveRole("small"); +} +export function getDefaultMediumFont(): UiFont { + return resolveRole("medium"); +} +export function getDefaultLargeFont(): UiFont { + return resolveRole("large"); +} + +// --- Terminal font --- + +export type TerminalFontConfig = { + font: UiFont; + /** Integer cell pitch; TTF glyphs are drawn per-cell at col*cellWidth. */ + cellWidth: number; + cellHeight: number; +}; + +let cachedTerminalConfig: TerminalFontConfig | null = null; + +/** + * The terminal's font and cell geometry. Note grid dimensions are baked into + * each session's handshake, so callers capture this per window at open time; + * a settings change applies to windows opened afterwards. + */ +export function getTerminalFontConfig(): TerminalFontConfig { + installInvalidation(); + if (cachedTerminalConfig) return cachedTerminalConfig; + const selection = getTerminalFontSelection(); + let config: TerminalFontConfig | null = null; + if (selection.kind === "ttf") { + const font = resolveTtf(selection); + // Cell pitch from a digit's advance (all advances are equal in the + // monospace faces the picker offers), rounded to an integer grid. + const advance = font?.getGlyph(48)?.advance ?? 0; + if (font && advance > 0) { + config = { font, cellWidth: Math.max(1, Math.round(advance)), cellHeight: font.lineHeight }; + } + } + if (!config) { + config = { font: getFont("terminus12"), cellWidth: 6, cellHeight: 12 }; + } + cachedTerminalConfig = config; + return config; +} diff --git a/app/images/EvenRealitiesLogo.png b/app/images/EvenRealitiesLogo.png new file mode 100644 index 00000000..ff953cf2 Binary files /dev/null and b/app/images/EvenRealitiesLogo.png differ diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index c6605ae0..b4327160 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -111,6 +111,8 @@ export class FaceclawCommunicatorBridge { private readonly communicator: any; private readonly listenerProxy: any; private javaCallQueue: Promise = Promise.resolve(); + /** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */ + private queuedJavaCalls = 0; private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); // Recent frame-finished outcomes from the Java side, so waitForFrameFinished // does not race against finishes that land before the wait starts. @@ -227,10 +229,30 @@ export class FaceclawCommunicatorBridge { }, 0); } - private enqueueJavaCall(operation: () => T): Promise { + /** + * Run a Java call, ordered against every other call made through here. + * + * inlineWhenIdle runs the call on the spot when nothing is queued ahead of + * it. Ordering still holds — the fast path only fires when no earlier call + * is waiting — and it skips a task-queue hop that measured ~18ms on device + * (the gap between "span submit start" and the Java side's own log line in + * the frame timings), which is pure latency on a call the caller is already + * awaiting. Off by default: a call that might block for a while should keep + * yielding to the main looper first. + */ + private enqueueJavaCall(operation: () => T, inlineWhenIdle = false): Promise { + if (inlineWhenIdle && this.queuedJavaCalls === 0) { + try { + return Promise.resolve(operation()); + } catch (error) { + return Promise.reject(error); + } + } + this.queuedJavaCalls++; const run = () => new Promise((resolve, reject) => { setTimeout(() => { + this.queuedJavaCalls--; try { resolve(operation()); } catch (error) { @@ -515,11 +537,19 @@ export class FaceclawCommunicatorBridge { fingerprint: string, paintMs = -1, frameId = 0, + /** + * The frame's glyph draws in surface coordinates (see + * graphics/glyph-wire.ts prepareFrameGlyphs); lets the texture-cache + * pipeline ship text as on-glasses cached draws instead of pixels. + */ + glyphs: ArrayBuffer | null = null, ): Promise { // Snapshot because the Java call is deferred; the buffer is passed as an // ArrayBuffer, which NativeScript marshals to a ByteBuffer without the // ~150ms per-element copy a byte[] parameter would need. const snapshot = new Uint8Array(pixels8bpp); + // inlineWhenIdle: this is the frame path, the Java side of it measures + // ~5ms (composite + pack), and the caller awaits it either way. await this.enqueueJavaCall(() => { this.communicator.submitSurfaceFrame( snapshot.buffer, @@ -531,8 +561,9 @@ export class FaceclawCommunicatorBridge { fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), + glyphs, ); - }); + }, true); } async disconnect(): Promise { @@ -543,6 +574,15 @@ export class FaceclawCommunicatorBridge { return this.enqueueJavaCall(() => Boolean(this.communicator.sendShutdown(exitMode))); } + /** + * Ask CFW mode 11 to release all custom-session state. On success this must + * remain the final Faceclaw message before close(); false requests the legacy + * shutdown/release fallback for older firmware. + */ + async sendCfwCleanup(): Promise { + return this.enqueueJavaCall(() => Boolean(this.communicator.sendCfwCleanup())); + } + /** * End only the EvenHub page/session, leaving both arm BLE connections and * their async notification subscriptions alive. diff --git a/app/native/file-access.ts b/app/native/file-access.ts index 4f0dfd0f..b79f3d5f 100644 --- a/app/native/file-access.ts +++ b/app/native/file-access.ts @@ -118,6 +118,77 @@ export function writeTextToDownloads(filename: string, text: string): string | n } } +/** Read a whole file as bytes; null when unreadable. */ +export function readBinaryFile(path: string): Uint8Array | null { + try { + const file = new java.io.File(path); + if (!file.isFile()) return null; + const bytes = java.nio.file.Files.readAllBytes(file.toPath()); + try { + // The V8 runtime can wrap a ByteBuffer's memory directly; copy out of it + // so the result outlives the Java array. + const arrayBuffer = (ArrayBuffer as any).from(java.nio.ByteBuffer.wrap(bytes)); + return new Uint8Array(arrayBuffer).slice(); + } catch { + const out = new Uint8Array(bytes.length); + for (let i = 0; i < bytes.length; i++) { + out[i] = bytes[i] & 0xff; + } + return out; + } + } catch (error) { + console.warn(`readBinaryFile failed for ${path}: ${error}`); + return null; + } +} + +/** Write bytes to a file, creating parent directories; false on failure. */ +export function writeBinaryFile(path: string, bytes: Uint8Array): boolean { + try { + const file = new java.io.File(path); + const parent = file.getParentFile(); + if (parent) parent.mkdirs(); + const stream = new java.io.FileOutputStream(file); + try { + // An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion + // submitSurfaceFrame relies on), so write through the channel. + const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); + stream.getChannel().write(buffer); + } finally { + stream.close(); + } + return true; + } catch (error) { + console.warn(`writeBinaryFile failed for ${path}: ${error}`); + return false; + } +} + +/** Recursively delete a file or directory tree (best effort). */ +export function deletePathRecursively(path: string): void { + try { + const root = new java.io.File(path); + if (!root.exists()) return; + if (root.isDirectory()) { + const children = root.listFiles(); + if (children) { + for (let i = 0; i < children.length; i++) { + deletePathRecursively(String(children[i].getAbsolutePath())); + } + } + } + root.delete(); + } catch (error) { + console.warn(`deletePathRecursively failed for ${path}: ${error}`); + } +} + +/** The app-private files directory (no permissions needed). */ +export function appFilesDirPath(): string { + const context = Utils.android.getApplicationContext(); + return String(context.getFilesDir().getAbsolutePath()); +} + /** Read a UTF-8 text file (size-capped); null when unreadable or too large. */ export function readTextFile(path: string): string | null { try { diff --git a/app/native/frame-timings.ts b/app/native/frame-timings.ts index 0b6408b3..56e2a5e1 100644 --- a/app/native/frame-timings.ts +++ b/app/native/frame-timings.ts @@ -3,13 +3,17 @@ * instrumentation; see FrameTimings.java). Frame ID 0 means "no frame" and is * ignored by every method, so callers never need to null-check. * - * Because the JS side is single-threaded, this module also tracks a "current - * frame" so that leaf drawing/IO code can record spans without threading a - * frame ID through every call signature. + * Frames form a tree: pass the causing frame's ID as `parentFrameId` when one + * frame fans out into several (an input event that repaints both the focused + * app and the shell chrome), and the export nests them under one timeline + * instead of showing unrelated frames. + * + * Because each JS context is single-threaded, this module also tracks a + * "current frame" so that leaf drawing/IO code can record spans without + * threading a frame ID through every call signature. */ declare const com: any; -declare const global: any; let javaInstance: any = null; let javaUnavailable = false; @@ -20,12 +24,11 @@ let currentFrameId = 0; function getJava(): any { if (javaInstance === null && !javaUnavailable) { + // Deliberately not gated on global.isAndroid: this runs in app workers + // too, and a worker that had not yet imported the core globals would + // silently drop every frame it renders. try { - if (typeof com !== "undefined" && global.isAndroid) { - javaInstance = com.faceclaw.app.FrameTimings.getInstance(); - } else { - javaUnavailable = true; - } + javaInstance = com.faceclaw.app.FrameTimings.getInstance(); } catch { javaUnavailable = true; } @@ -33,10 +36,23 @@ function getJava(): any { return javaInstance; } -export function startFrame(reason: string): number { +/** Begin a frame; parentFrameId links it under the frame that caused it. */ +export function startFrame(reason: string, parentFrameId = 0): number { const java = getJava(); if (!java) return fallbackNextFrameId--; - return Number(java.startFrame(reason)); + // Guarded: requestRender-style callbacks can be invoked with an unrelated + // argument, and a NaN would blow up the int marshalling. + const parent = Number.isFinite(parentFrameId) ? Math.max(0, Math.round(parentFrameId)) : 0; + return Number(java.startFrame(reason, parent)); +} + +/** + * Append a fact to the frame's label, e.g. which app was in the foreground + * when the input arrived. Shows in the frame's header line in the export. + */ +export function annotateFrame(frameId: number, text: string): void { + if (frameId <= 0 || !text) return; + getJava()?.annotate(frameId, text); } export function logFrame(frameId: number, message: string): void { @@ -68,6 +84,20 @@ export function span(frameId: number, name: string, fn: () => T): T { } } +/** + * Span around an awaited step. Most multi-second gaps in the timing export are + * awaits (an app's async input handler, a worker round trip, the Java call + * queue), so instrument those with this rather than leaving a bare jump. + */ +export async function spanAsync(frameId: number, name: string, fn: () => Promise): Promise { + spanStart(frameId, name); + try { + return await fn(); + } finally { + spanEnd(frameId, name); + } +} + /** Set the current frame while running fn, for leaf code using the *Current helpers. */ export function runWithFrame(frameId: number, fn: () => T): T { const previous = currentFrameId; diff --git a/app/native/gray-image-preview.ts b/app/native/gray-image-preview.ts index 06eb95f0..690c1e4b 100644 --- a/app/native/gray-image-preview.ts +++ b/app/native/gray-image-preview.ts @@ -17,10 +17,11 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { return null; } + const baked = image.withDrawsBaked(); const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray( - image.pixels.buffer, - image.width, - image.height, + baked.pixels.buffer, + baked.width, + baked.height, PREVIEW_BRIGHTEN_GAMMA, ); return new ImageSource(bitmap); diff --git a/app/native/voice-control.ts b/app/native/voice-control.ts index da5f10c9..a5686680 100644 --- a/app/native/voice-control.ts +++ b/app/native/voice-control.ts @@ -41,6 +41,8 @@ export type PushToTalkOptions = { /** Who currently wants the mic running. */ type CaptureHolder = "ptt" | "continuous"; +export type RawPcmListener = (pcm: Uint8Array) => void; + export class FaceclawVoiceControlBridge { private readonly statusListeners = new Set<(state: VoiceControlState) => void>(); private readonly wakeWordListeners = new Set<(keyword: string) => void>(); @@ -57,6 +59,12 @@ export class FaceclawVoiceControlBridge { private readonly captureHolders = new Set(); // Non-null while a cloud provider owns the transcript; Java only decodes PCM. private cloudClient: CloudSttClient | null = null; + // Raw-PCM tap (EvenHub mic apps): when active, the controller runs in the + // decode-only "cloud" mode and every decoded PCM frame is broadcast to these + // listeners. STT capture preempts it — a live assistant/transcribe session + // owns the single G2 mic and the raw tap is stopped for its duration. + private readonly rawPcmListeners = new Set(); + private rawActive = false; onStatus(listener: (state: VoiceControlState) => void): () => void { this.statusListeners.add(listener); @@ -102,8 +110,46 @@ export class FaceclawVoiceControlBridge { this.releaseCapture("continuous", false); } + /** Subscribe to decoded raw mic PCM (16 kHz mono S16LE). */ + onRawPcm(listener: RawPcmListener): () => void { + this.rawPcmListeners.add(listener); + return () => this.rawPcmListeners.delete(listener); + } + + /** + * Start the decode-only raw-PCM tap for an EvenHub mic app. Fails (returns + * false) if an STT capture already owns the mic; the caller re-tries when its + * eligibility is re-evaluated. Idempotent while already running. + */ + startRawCapture(options: { communicator: any }): boolean { + if (!global.isAndroid) return false; + if (this.rawActive) return true; + if (this.captureHolders.size > 0 || this.started) return false; + this.ensureController(); + this.controller?.setCommunicator(options.communicator); + this.controller?.setSaveRecordings(false); + this.controller?.setEndpointing(false); + this.cloudClient = null; + this.rawActive = true; + this.started = true; + // "cloud" mode decodes LC3 to PCM and emits it via onPcm without loading + // any recognizer; with no cloudClient the frames reach only the raw tap. + this.controller?.start("cloud"); + return true; + } + + stopRawCapture(): void { + if (!this.rawActive) return; + this.rawActive = false; + this.started = false; + if (global.isAndroid) this.controller?.stop(); + } + private acquireCapture(holder: CaptureHolder, options: PushToTalkOptions): void { if (!global.isAndroid) return; + // STT preempts the raw-PCM tap: a live transcription session owns the mic, + // and an EvenHub app gets no audio for its duration. + if (this.rawActive) this.stopRawCapture(); const wasIdle = this.captureHolders.size === 0; this.captureHolders.add(holder); if (!wasIdle) { @@ -196,6 +242,7 @@ export class FaceclawVoiceControlBridge { stop(): void { this.captureHolders.clear(); + this.rawActive = false; if (global.isAndroid) { this.controller?.stop(); } @@ -214,6 +261,9 @@ export class FaceclawVoiceControlBridge { this.controller = new com.faceclaw.app.FaceclawVoiceController(context); this.listenerProxy = new com.faceclaw.app.FaceclawVoiceControllerListener({ onStatus: (status: string) => { + // The raw tap runs silently; its "Listening (cloud)..." chatter must + // not surface on the glasses voice status. + if (this.rawActive) return; this.setStatus(String(status)); }, onWakeWord: (keyword: string) => { @@ -225,7 +275,11 @@ export class FaceclawVoiceControlBridge { this.emitTranscript(String(text), Boolean(isFinal)); }, onPcm: (pcm: any) => { - this.cloudClient?.acceptPcm(toUint8Array(pcm)); + const bytes = toUint8Array(pcm); + this.cloudClient?.acceptPcm(bytes); + if (this.rawPcmListeners.size > 0) { + this.rawPcmListeners.forEach((listener) => listener(bytes)); + } }, onSpeechEnd: () => { for (const listener of this.speechEndListeners) { diff --git a/app/phone-ui/main-page.ts b/app/phone-ui/main-page.ts index 6356fd23..5429381e 100644 --- a/app/phone-ui/main-page.ts +++ b/app/phone-ui/main-page.ts @@ -79,12 +79,16 @@ function applySettingsTextFieldContrast(textField: TextField): void { nativeTextField.setSelectAllOnFocus(true) } -function focusSystemNameField(page: Page): void { +function focusTextEditor(page: Page): void { setTimeout(() => { const textField = page.getViewById('settingsTextField') + const secondaryTextField = page.getViewById('secondarySettingsTextField') if (textField) { applySettingsTextFieldContrast(textField) } + if (secondaryTextField) { + applySettingsTextFieldContrast(secondaryTextField) + } textField?.focus() }, 0) } @@ -139,7 +143,7 @@ export function loaded(args: EventData) { } if (propertyArgs.propertyName === 'activeTextSettingId') { if (model.isTextSettingEditorActive) { - focusSystemNameField(page) + focusTextEditor(page) } return } @@ -160,7 +164,7 @@ export function loaded(args: EventData) { setPageState(page, state) scrollLogsToBottom(scrollViews) if (model.isTextSettingEditorActive) { - focusSystemNameField(page) + focusTextEditor(page) } } diff --git a/app/phone-ui/main-page.xml b/app/phone-ui/main-page.xml index bf7bc295..98f51b4a 100644 --- a/app/phone-ui/main-page.xml +++ b/app/phone-ui/main-page.xml @@ -105,15 +105,31 @@ visibility="{{ textSettingEditorVisibility }}" horizontalAlignment="stretch" verticalAlignment="top"> +