mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Merge main into ios-port and update the iOS CFW transport
This commit is contained in:
commit
e8e144f3f2
156 files changed
+10528
-1679
No files matched your search
+456
-62
@@ -27,15 +27,14 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private static final String TAG = "FaceclawComm";
|
||||
|
||||
// The EvenHub image container is a memory carrier only. Its 576x288 geometry
|
||||
// gives the firmware separate 165888-byte display and reconstruction
|
||||
// allocations: CFW reuses the former for its 640x480 packed-4bpp shadow and
|
||||
// leaves the latter wholly available for compressed incoming messages.
|
||||
// Local metadata for custom-command bookkeeping, not an EvenHub container.
|
||||
// Submitted frames supply pixel geometry; the stock layout only captures input.
|
||||
private static final BleProtocol.ImageTileOptions DASHBOARD_TILE =
|
||||
new BleProtocol.ImageTileOptions("img00", 10, 0, 0, 576, 288);
|
||||
|
||||
@@ -77,7 +76,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
private final java.util.List<FaceclawMicStatusListener> micStatusListeners =
|
||||
new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
private volatile String lastFirmwareCapabilities = "";
|
||||
private volatile Thread workerThread;
|
||||
private volatile boolean running;
|
||||
private volatile boolean userDisconnectRequested;
|
||||
@@ -130,7 +128,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private int faceclawFramebufferControlGeneration;
|
||||
private int faceclawFramebufferControlSentCount;
|
||||
private int faceclawWakePendingNonce = -1;
|
||||
private boolean cfwCleanupSupported;
|
||||
/**
|
||||
* The last firmware-info read said the glasses run Faceclaw's custom
|
||||
* firmware. Gates the private modes (cleanup, texture cache, ...) so stock
|
||||
* or third-party firmware never sees them; the TS side checks the actual
|
||||
* revision and disconnects on a mismatch, so no per-feature gating is
|
||||
* needed here.
|
||||
*/
|
||||
private boolean customFirmwareDetected;
|
||||
private boolean cfwCleanupDelivered;
|
||||
private int lastCfwCleanupAckMagic;
|
||||
|
||||
@@ -162,6 +167,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private long lastShutdownExitAtMs = 0;
|
||||
private int headsetBattery = -1;
|
||||
private int headsetCharging = -1;
|
||||
private int ringBattery = -1;
|
||||
private int ringCharging = -1;
|
||||
// Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked.
|
||||
private int silentMode = -1;
|
||||
private int wearState = -1;
|
||||
@@ -176,6 +183,38 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private volatile FaceclawAudioPacketListener audioPacketListener;
|
||||
private PowerManager.WakeLock g2ScreenWakeLock;
|
||||
|
||||
// BLE bandwidth benchmark (Developer app). Streams no-op image payloads
|
||||
// (CFW mode 7 with an unused sub-op: parsed, acked, and discarded — stock
|
||||
// firmware likewise ignores unknown image modes) for a fixed duration with
|
||||
// a selectable message size and pipeline window, then reports throughput.
|
||||
// While active, desired-frame sends are held back and heartbeats are
|
||||
// satisfied by the benchmark's own acks, so the stream is the only image
|
||||
// traffic. All state below is guarded by `lock`; results are read with
|
||||
// getBandwidthBenchmarkStatus() and survive until the next run starts.
|
||||
private boolean benchmarkActive;
|
||||
private boolean benchmarkAborted;
|
||||
private final Random benchmarkRandom = new Random();
|
||||
private int benchmarkMessageSize;
|
||||
private int benchmarkWindowSize;
|
||||
private int benchmarkDurationMs;
|
||||
private int benchmarkLinkMode;
|
||||
private boolean benchmarkLinkPending;
|
||||
private long benchmarkReadyAtMs;
|
||||
private long benchmarkStartAtMs; // first benchmark write; 0 until then
|
||||
private long benchmarkDeadlineAtMs; // start + duration; MAX_VALUE until first write
|
||||
private long benchmarkLastAckAtMs;
|
||||
private long benchmarkEndAtMs; // 0 while running; set when the run drains
|
||||
private int benchmarkMessagesSent;
|
||||
private int benchmarkMessagesAcked;
|
||||
private int benchmarkTimeouts;
|
||||
private long benchmarkPayloadBytesAcked;
|
||||
private long benchmarkWireBytesAcked;
|
||||
// A timed-out benchmark message aborts the run, but its already-in-flight
|
||||
// peers still time out one by one; keep the window comfortably below
|
||||
// MAX_CONSECUTIVE_ACK_TIMEOUTS so a dead run can't escalate into a
|
||||
// transport-failure reconnect all by itself.
|
||||
private static final int BENCHMARK_MAX_WINDOW = 6;
|
||||
|
||||
private final BroadcastReceiver phoneLockReceiver = new BroadcastReceiver() {
|
||||
@Override public void onReceive(Context context, Intent intent) {
|
||||
emitPhoneLockStateIfChanged(true);
|
||||
@@ -226,11 +265,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// firmware frees the cache with the fb lease, and after any resync the
|
||||
// cheap safe assumption is an empty cache (glyphs re-upload lazily).
|
||||
private final TextureCacheState textureCache = new TextureCacheState();
|
||||
private boolean textureCacheSupported;
|
||||
private boolean textureImagesSupported;
|
||||
private boolean fwTextSupported;
|
||||
|
||||
private final ArrayDeque<OutboundMessage> pendingMessages = new ArrayDeque<>();
|
||||
private final CfwTransport[] cfwTransports = { new CfwTransport(), new CfwTransport() };
|
||||
private final ArrayDeque<OutboundMessage> inFlightMessages = new ArrayDeque<>();
|
||||
private OutboundMessage prewrittenMessage;
|
||||
private List<byte[]> prewrittenFrames = Collections.emptyList();
|
||||
@@ -580,6 +617,200 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the BLE bandwidth benchmark: stream messageSize-byte no-op image
|
||||
* payloads for durationMs, keeping up to windowSize messages awaiting ack
|
||||
* at once, then leave the results for getBandwidthBenchmarkStatus().
|
||||
* Returns false when a run is already active or the image path is not
|
||||
* ready. The duration clock starts at the first benchmark write, so
|
||||
* traffic already queued ahead of the run doesn't count against it.
|
||||
*/
|
||||
public boolean startBandwidthBenchmark(int messageSize, int windowSize, int durationMs) {
|
||||
return startBandwidthBenchmarkWithLinkMode(messageSize, windowSize, durationMs, 0);
|
||||
}
|
||||
|
||||
// 0: current link; 1: re-request HIGH; 2: request 2M; 3: both.
|
||||
public boolean startBandwidthBenchmarkWithLinkMode(int messageSize, int windowSize,
|
||||
int durationMs, int linkMode) {
|
||||
synchronized (lock) {
|
||||
if (benchmarkActive || !running || !sessionReady || !fixedLayoutCreated
|
||||
|| shutdownRequested || chargingMode) {
|
||||
logLine("skip bandwidth benchmark; already running or image path not ready");
|
||||
return false;
|
||||
}
|
||||
benchmarkMessageSize = Math.max(2, Math.min(messageSize, ConnectionOptions.IMAGE_FRAGMENT_SIZE));
|
||||
benchmarkWindowSize = Math.max(1, Math.min(windowSize, BENCHMARK_MAX_WINDOW));
|
||||
benchmarkDurationMs = Math.max(1_000, durationMs);
|
||||
benchmarkLinkMode = linkMode & 3;
|
||||
benchmarkLinkPending = true;
|
||||
benchmarkReadyAtMs = Long.MAX_VALUE;
|
||||
benchmarkStartAtMs = 0;
|
||||
benchmarkDeadlineAtMs = Long.MAX_VALUE;
|
||||
benchmarkLastAckAtMs = 0;
|
||||
benchmarkEndAtMs = 0;
|
||||
benchmarkMessagesSent = 0;
|
||||
benchmarkMessagesAcked = 0;
|
||||
benchmarkTimeouts = 0;
|
||||
benchmarkPayloadBytesAcked = 0;
|
||||
benchmarkWireBytesAcked = 0;
|
||||
benchmarkAborted = false;
|
||||
benchmarkActive = true;
|
||||
logLine("bandwidth benchmark start: size=" + benchmarkMessageSize
|
||||
+ "B window=" + benchmarkWindowSize + " duration=" + benchmarkDurationMs
|
||||
+ "ms linkMode=" + benchmarkLinkMode);
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel an in-progress benchmark (benchmark page closed). Queued no-op
|
||||
* messages are dropped; in-flight ones drain through their normal acks.
|
||||
*/
|
||||
public void cancelBandwidthBenchmark() {
|
||||
synchronized (lock) {
|
||||
if (!benchmarkActive) {
|
||||
return;
|
||||
}
|
||||
clearMessagesOfKindLocked("bandwidth");
|
||||
finishBenchmarkLocked(true, "cancelled");
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
/** Status/results of the current or most recent benchmark run, as JSON. */
|
||||
public String getBandwidthBenchmarkStatus() {
|
||||
synchronized (lock) {
|
||||
String state = benchmarkActive
|
||||
? (benchmarkStartAtMs == 0 ? "starting" : "running")
|
||||
: (benchmarkEndAtMs != 0 ? "done" : "idle");
|
||||
long end = benchmarkActive ? SystemClock.elapsedRealtime() : benchmarkEndAtMs;
|
||||
long elapsed = benchmarkStartAtMs == 0 ? 0 : Math.max(0, end - benchmarkStartAtMs);
|
||||
try {
|
||||
org.json.JSONObject status = new org.json.JSONObject();
|
||||
status.put("state", state);
|
||||
status.put("messageSize", benchmarkMessageSize);
|
||||
status.put("windowSize", benchmarkWindowSize);
|
||||
status.put("linkMode", benchmarkLinkMode);
|
||||
status.put("elapsedMs", elapsed);
|
||||
status.put("messagesSent", benchmarkMessagesSent);
|
||||
status.put("messagesAcked", benchmarkMessagesAcked);
|
||||
status.put("timeouts", benchmarkTimeouts);
|
||||
status.put("payloadBytesAcked", benchmarkPayloadBytesAcked);
|
||||
status.put("wireBytesAcked", benchmarkWireBytesAcked);
|
||||
status.put("aborted", benchmarkAborted);
|
||||
return status.toString();
|
||||
} catch (org.json.JSONException e) {
|
||||
return "{\"state\":\"idle\"}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Pending + in-flight benchmark no-op messages. */
|
||||
private int benchmarkOutstandingLocked() {
|
||||
int count = 0;
|
||||
for (OutboundMessage message : pendingMessages) {
|
||||
if ("bandwidth".equals(message.kind)) count++;
|
||||
}
|
||||
for (OutboundMessage message : inFlightMessages) {
|
||||
if ("bandwidth".equals(message.kind)) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the benchmark stream fed: top the pending queue up so the send
|
||||
* window never starves, stop enqueueing once the run expires (or a message
|
||||
* times out), and finish the run when the last outstanding message drains.
|
||||
*/
|
||||
private void maintainBenchmarkLocked(long now) {
|
||||
if (!sessionReady || !fixedLayoutCreated || shutdownRequested) {
|
||||
finishBenchmarkLocked(true, "session no longer ready");
|
||||
return;
|
||||
}
|
||||
if (benchmarkLinkPending || now < benchmarkReadyAtMs) {
|
||||
return;
|
||||
}
|
||||
if (benchmarkAborted || now >= benchmarkDeadlineAtMs) {
|
||||
// The run is over: drop queued-but-unsent no-ops (sending them
|
||||
// would stretch the run past its deadline) and finish once the
|
||||
// in-flight tail has acked or timed out.
|
||||
Iterator<OutboundMessage> pendingIterator = pendingMessages.iterator();
|
||||
while (pendingIterator.hasNext()) {
|
||||
OutboundMessage queued = pendingIterator.next();
|
||||
if ("bandwidth".equals(queued.kind)) {
|
||||
pendingIterator.remove();
|
||||
magicPool.release(queued.sid, queued.magic, queued.label, "benchmark over");
|
||||
}
|
||||
}
|
||||
if (benchmarkOutstandingLocked() == 0) {
|
||||
finishBenchmarkLocked(false, "complete");
|
||||
}
|
||||
return;
|
||||
}
|
||||
// One more than the window so a fresh message is always ready to write
|
||||
// the moment an ack frees a slot.
|
||||
for (int outstanding = benchmarkOutstandingLocked(); outstanding <= benchmarkWindowSize; outstanding++) {
|
||||
enqueueBenchmarkMessageLocked();
|
||||
}
|
||||
}
|
||||
|
||||
private void finishBenchmarkLocked(boolean aborted, String reason) {
|
||||
if (!benchmarkActive) {
|
||||
return;
|
||||
}
|
||||
benchmarkActive = false;
|
||||
benchmarkAborted |= aborted;
|
||||
// Prefer the last ack as the end time so drain lag after the deadline
|
||||
// doesn't dilute the throughput figure.
|
||||
benchmarkEndAtMs = benchmarkLastAckAtMs != 0 ? benchmarkLastAckAtMs : SystemClock.elapsedRealtime();
|
||||
logLine("bandwidth benchmark " + (benchmarkAborted ? "aborted" : "finished") + " (" + reason + "): "
|
||||
+ benchmarkMessagesAcked + "/" + benchmarkMessagesSent + " acked, "
|
||||
+ benchmarkPayloadBytesAcked + "B payload, " + benchmarkTimeouts + " timeouts");
|
||||
}
|
||||
|
||||
private void enqueueBenchmarkMessageLocked() {
|
||||
// Fresh bytes per message: the transport's persistent compression history
|
||||
// would compress even a random payload if we reused it across the run.
|
||||
byte[] payload = new byte[benchmarkMessageSize];
|
||||
benchmarkRandom.nextBytes(payload);
|
||||
payload[0] = 7; // CFW diagnostic-control mode...
|
||||
payload[1] = (byte) 0x7f; // ...with an unused sub-op: acked, no effect
|
||||
OutboundMessage message = messageBuilder.imagePayload(
|
||||
"bandwidth",
|
||||
DASHBOARD_TILE,
|
||||
nextMapSessionId(),
|
||||
payload,
|
||||
"bandwidth no-op " + payload.length + "B",
|
||||
connectionOptions.sendImagesToLeft);
|
||||
final int payloadBytes = payload.length;
|
||||
// Logical custom-message bytes; excludes length tag and packet framing.
|
||||
final int wireBytes = message.message.length;
|
||||
message.onSent = () -> {
|
||||
benchmarkMessagesSent++;
|
||||
if (benchmarkStartAtMs == 0) {
|
||||
benchmarkStartAtMs = SystemClock.elapsedRealtime();
|
||||
benchmarkDeadlineAtMs = benchmarkStartAtMs + benchmarkDurationMs;
|
||||
}
|
||||
};
|
||||
message.onAck = () -> {
|
||||
long ackedAtMs = SystemClock.elapsedRealtime();
|
||||
benchmarkMessagesAcked++;
|
||||
benchmarkPayloadBytesAcked += payloadBytes;
|
||||
benchmarkWireBytesAcked += wireBytes;
|
||||
benchmarkLastAckAtMs = ackedAtMs;
|
||||
// No-op payloads ride the image path, so the firmware resets its
|
||||
// heartbeat timer on them just like real image messages.
|
||||
lastHeartbeatAckedAtMs = ackedAtMs;
|
||||
};
|
||||
message.onTimeout = () -> {
|
||||
benchmarkTimeouts++;
|
||||
benchmarkAborted = true;
|
||||
logLine("bandwidth benchmark ack timeout");
|
||||
};
|
||||
pendingMessages.addLast(message);
|
||||
}
|
||||
|
||||
public void addImuListener(FaceclawImuListener listener) {
|
||||
if (listener != null) {
|
||||
imuListeners.add(listener);
|
||||
@@ -592,11 +823,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
/** The CFW capability token string from the last firmware-info read ("" before one arrives). */
|
||||
public String getFirmwareCapabilities() {
|
||||
return lastFirmwareCapabilities;
|
||||
}
|
||||
|
||||
public void addAmbientLightListener(FaceclawAmbientLightListener listener) {
|
||||
if (listener != null) {
|
||||
ambientLightListeners.add(listener);
|
||||
@@ -1100,7 +1326,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (cfwCleanupDelivered) {
|
||||
return true;
|
||||
}
|
||||
if (!cfwCleanupSupported || !running || !sessionReady
|
||||
if (!customFirmwareDetected || !running || !sessionReady
|
||||
|| shutdownRequested || !fixedLayoutCreated) {
|
||||
logLine("skip CFW cleanup; mode 11 unavailable or image path not ready");
|
||||
return false;
|
||||
@@ -1432,6 +1658,33 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (!BleProtocol.NOTIFY_CHAR_UUID.equals(uuid)) {
|
||||
return;
|
||||
}
|
||||
if (data.length >= 7 && (data[6] & 255) == CfwTransport.SID) {
|
||||
CfwTransport.Ack[] acks = CfwTransport.parseAcks(data);
|
||||
if (acks == null) return;
|
||||
synchronized (lock) {
|
||||
lastIncomingAtMs = SystemClock.elapsedRealtime();
|
||||
for (CfwTransport.Ack ack : acks) {
|
||||
for (OutboundMessage message : inFlightMessages) {
|
||||
String ingress = message.isLeftArmMessage ? leftAddress : rightAddress;
|
||||
if (message.sid == CfwTransport.SID && address.equalsIgnoreCase(ingress) && message.magic == ack.streamId) {
|
||||
message.acceptCfwAck(ack);
|
||||
Log.i(TAG, "CFW " + (ack.nack ? "NACK" : "ACK") + " id=" + ack.streamId
|
||||
+ " ordinal=" + ack.messageId + " lens=" + ack.lens + " txseq=" + (data[2] & 255)
|
||||
+ " redundant=" + (ack != acks[0])
|
||||
+ " size=" + ack.size + " crc=" + ack.checksum
|
||||
+ " expected=" + message.message.length + "/" + message.cfwChecksum
|
||||
+ " ackedLenses=" + message.cfwAckLenses
|
||||
+ " ageMs=" + (lastIncomingAtMs - message.sentAtMs));
|
||||
message.ackPayload = Arrays.copyOf(data, data.length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
drainCfwAcknowledgementsLocked();
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
return;
|
||||
}
|
||||
Log.d(TAG, "onNotification: address=" + address + " characteristicUuid=" + characteristicUuid + " data.length=" + data.length);
|
||||
BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data);
|
||||
int decodedWearState = BleProtocol.parseWearState(frame);
|
||||
@@ -1461,17 +1714,44 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
);
|
||||
faceclawWakeNotification = true;
|
||||
lastConnectionOrInputAtMs = lastIncomingAtMs;
|
||||
// The event type says which gesture woke the glasses: TS
|
||||
// gives a head-up the Glanceboard and a double tap the
|
||||
// regular UI. The claim handshake is the same for both.
|
||||
boolean headUp = BleProtocol.parseFaceclawWakeEventCode(frame.pb)
|
||||
== BleProtocol.FACECLAW_WAKE_EVENT_HEAD_UP;
|
||||
event = new G2Event(
|
||||
"display-wake",
|
||||
"",
|
||||
BleProtocol.EVENT_DOUBLE_CLICK,
|
||||
headUp ? BleProtocol.EVENT_HEAD_UP : BleProtocol.EVENT_DOUBLE_CLICK,
|
||||
0,
|
||||
0
|
||||
);
|
||||
logLine("claimed deferred dashboard wake nonce=" + wakeNonce);
|
||||
logLine("claimed deferred dashboard wake nonce=" + wakeNonce
|
||||
+ (headUp ? " (head-up)" : ""));
|
||||
}
|
||||
}
|
||||
if (!faceclawWakeNotification
|
||||
&& shutdownRequested
|
||||
&& frame.ok
|
||||
&& frame.sid == BleProtocol.SID_UI_SETTING
|
||||
&& address.equalsIgnoreCase(rightAddress)) {
|
||||
// CFW idle-gesture forwarding (firmware revision 2+): with no
|
||||
// EvenHub page the stock display thread drops taps, long
|
||||
// presses and releases; the CFW reports them on the settings
|
||||
// sid while our wake lease is held. Deliver them as the
|
||||
// sys-events a live page would have produced, so TS treats a
|
||||
// sleep-time tap or hold exactly like one during soft sleep
|
||||
// (the Glanceboard).
|
||||
BleProtocol.FaceclawGestureEvent gesture = BleProtocol.parseFaceclawGestureEvent(frame.pb);
|
||||
if (gesture != null) {
|
||||
lastConnectionOrInputAtMs = lastIncomingAtMs;
|
||||
event = new G2Event("sys-event", "", gesture.eventType, gesture.eventSource, 0);
|
||||
logLine("idle gesture forwarded by CFW: type=" + gesture.eventType
|
||||
+ " source=" + gesture.eventSource);
|
||||
}
|
||||
}
|
||||
if (!faceclawWakeNotification
|
||||
&& event == null
|
||||
&& shutdownRequested
|
||||
&& address.equalsIgnoreCase(rightAddress)
|
||||
&& BleProtocol.isDisplayWakeStateChange(frame)) {
|
||||
@@ -1490,6 +1770,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (!faceclawWakeNotification
|
||||
&& frame.ok
|
||||
&& frame.sid == BleProtocol.SID_UI_SETTING) {
|
||||
// Mode-17 query notifications; settings READs are handled by
|
||||
// createBatteryQueryMessageLocked so headset/ring update together.
|
||||
if (address.equalsIgnoreCase(rightAddress)
|
||||
&& (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) {
|
||||
BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(frame.pb);
|
||||
if (ring != null) {
|
||||
ringBattery = ring.battery;
|
||||
ringCharging = ring.charging;
|
||||
emitBatteryState(headsetBattery, headsetCharging);
|
||||
}
|
||||
}
|
||||
// CFW mic status (field 104) rides both standalone pushes and
|
||||
// settings read acks, from each temple on its own link.
|
||||
byte[] micStatus = BleProtocol.parseFaceclawMicStatus(frame.pb);
|
||||
@@ -1534,6 +1825,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
&& address.equalsIgnoreCase(rightAddress)
|
||||
&& (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) {
|
||||
event = G2Event.decode(frame);
|
||||
if (event != null
|
||||
&& "sys-event".equals(event.kind)
|
||||
&& event.eventType == BleProtocol.EVENT_HEAD_UP) {
|
||||
// CFW forwards the IMU head-up while our page is on screen
|
||||
// (soft sleep). Surface it as the same wake-only input the
|
||||
// deferred head-up wake produces from a dark display.
|
||||
event = new G2Event("display-wake", "", BleProtocol.EVENT_HEAD_UP, event.eventSource, 0);
|
||||
logLine("head-up forwarded by CFW while page on screen");
|
||||
}
|
||||
if (event != null) {
|
||||
// Pure IMU samples arrive continuously; don't let them count
|
||||
// as user input (which would starve battery polling).
|
||||
@@ -1703,6 +2003,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
audioCaptureActive = false;
|
||||
faceclawWakePendingNonce = -1;
|
||||
cfwCleanupDelivered = false;
|
||||
for (CfwTransport transport : cfwTransports) transport.reset();
|
||||
lastCfwCleanupAckMagic = 0;
|
||||
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
||||
lastFaceclawFramebufferLeaseQueuedAtMs = 0;
|
||||
@@ -1722,7 +2023,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
logLine("session ready");
|
||||
synchronized (lock) {
|
||||
// Query settings promptly on the first session so firmware
|
||||
// version/capabilities (and battery) arrive without waiting for
|
||||
// version/extension (and battery) arrive without waiting for
|
||||
// the input-quiet battery poll. The settings response doubles as
|
||||
// the firmware-compatibility check surfaced during onboarding.
|
||||
if (!firmwareInfoQueried) {
|
||||
@@ -1954,6 +2255,33 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private long driveSession() {
|
||||
//Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")");
|
||||
while (true) {
|
||||
// Run on the sender thread, outside `lock`: Bluetooth API calls can
|
||||
// wait on the global GATT lock, and callbacks need the session lock.
|
||||
// Keep negotiation outside the timed stream. A fixed settling period
|
||||
// is only an experiment boundary; HCI must confirm actual parameters.
|
||||
int linkMode = -1;
|
||||
String[] benchmarkAddresses = null;
|
||||
synchronized (lock) {
|
||||
if (benchmarkActive && benchmarkLinkPending) {
|
||||
benchmarkLinkPending = false;
|
||||
linkMode = benchmarkLinkMode;
|
||||
benchmarkAddresses = new String[] {leftAddress, rightAddress};
|
||||
}
|
||||
}
|
||||
if (benchmarkAddresses != null) {
|
||||
for (String address : benchmarkAddresses) {
|
||||
try {
|
||||
bleManager.prepareBenchmarkLink(address, linkMode);
|
||||
} catch (RuntimeException error) {
|
||||
logLine("benchmark link request failed: " + safeMessage(error));
|
||||
}
|
||||
}
|
||||
synchronized (lock) {
|
||||
// Cancellation/new-run races leave the new run pending; its
|
||||
// own preparation will replace this deadline before sending.
|
||||
benchmarkReadyAtMs = SystemClock.elapsedRealtime() + 1_000;
|
||||
}
|
||||
}
|
||||
OutboundMessage messageToWrite = null;
|
||||
OutboundMessage messageToPrewrite = null;
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
@@ -1961,6 +2289,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
maybeFinishNoChangeDesiredFrame();
|
||||
|
||||
synchronized (lock) {
|
||||
drainCfwAcknowledgementsLocked();
|
||||
if (cfwCleanupDelivered) {
|
||||
/* Successful mode 11 must be the last Faceclaw write. Drop
|
||||
* anything a late external producer attempted to enqueue
|
||||
@@ -1988,11 +2317,36 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
if (!inFlightMessages.isEmpty()) {
|
||||
OutboundMessage oldest = inFlightMessages.peekFirst();
|
||||
List<OutboundMessage> replay = CfwMessageWindow.replayWindow(inFlightMessages, now);
|
||||
if (!replay.isEmpty()) {
|
||||
logLine("CFW recovery: replay " + replay.size()
|
||||
+ " unresolved message(s) from id=" + replay.get(0).magic);
|
||||
for (OutboundMessage candidate : replay) {
|
||||
if (candidate.cfwRetries >= CfwMessageWindow.MAX_RETRIES) {
|
||||
handleTransportFailure("CFW recovery retry limit");
|
||||
return 0;
|
||||
}
|
||||
logLine("CFW replay id=" + candidate.magic + " label=" + candidate.label
|
||||
+ " cause=" + (candidate.cfwRetryPending ? "NACK"
|
||||
: candidate.cfwAckLenses == CfwTransport.BOTH ? "ordered tail" : "missing ACK")
|
||||
+ " ackedLenses=" + candidate.cfwAckLenses
|
||||
+ " ageMs=" + (now - candidate.sentAtMs));
|
||||
inFlightMessages.remove(candidate);
|
||||
magicPool.release(candidate.sid, candidate.magic, candidate.label, "CFW replay");
|
||||
candidate.prepareCfwReplay(magicPool.allocate());
|
||||
}
|
||||
for (int i = replay.size() - 1; i >= 0; --i) pendingMessages.addFirst(replay.get(i));
|
||||
return 0;
|
||||
}
|
||||
if (oldest != null && oldest.ackDeadlineAtMs <= now) {
|
||||
Log.i(TAG, "message timed out: " + oldest.label);
|
||||
inFlightMessages.removeFirst();
|
||||
logLine("message timed out: " + oldest.label);
|
||||
logLine("message timed out: " + oldest.label + " sid=" + oldest.sid
|
||||
+ " id=" + oldest.magic + " ackedLenses=" + oldest.cfwAckLenses
|
||||
+ " ageMs=" + (now - oldest.sentAtMs));
|
||||
magicPool.release(oldest.sid, oldest.magic, oldest.label, "timeout");
|
||||
if (oldest.sid == CfwTransport.SID)
|
||||
cfwTransports[oldest.isLeftArmMessage ? 0 : 1].reset();
|
||||
handleAckTimeoutLocked(oldest);
|
||||
return 0;
|
||||
}
|
||||
@@ -2050,9 +2404,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
enqueueCompassControlLocked(false, wanted);
|
||||
}
|
||||
|
||||
if (benchmarkActive) {
|
||||
maintainBenchmarkLocked(now);
|
||||
}
|
||||
|
||||
// Up to WINDOW_SIZE messages may be in flight at once (full
|
||||
// pipelining); a slot frees when an ack arrives.
|
||||
boolean windowHasRoom = inFlightMessages.size() < Math.max(1, connectionOptions.WINDOW_SIZE);
|
||||
// pipelining); a slot frees when an ack arrives. An active
|
||||
// bandwidth benchmark measures its own selected window instead.
|
||||
boolean windowHasRoom = inFlightMessages.size()
|
||||
< Math.max(1, benchmarkActive ? benchmarkWindowSize : connectionOptions.WINDOW_SIZE);
|
||||
// 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).
|
||||
@@ -2063,11 +2423,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// 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).
|
||||
// A benchmark run counts as image traffic here for the same
|
||||
// reason: its acks reset the firmware heartbeat timer, so a
|
||||
// fresh heartbeat in front of it is redundant.
|
||||
boolean imageWaiting = !shutdownRequested && fixedLayoutCreated
|
||||
&& !hasPendingOrInflightKindLocked("heartbeat")
|
||||
&& now >= imageRetryAfterMs
|
||||
&& (hasPendingImageLocked()
|
||||
|| !getDesiredFingerprint().equals(lastEnqueuedFingerprint));
|
||||
&& (benchmarkActive
|
||||
|| (now >= imageRetryAfterMs
|
||||
&& (hasPendingImageLocked()
|
||||
|| !getDesiredFingerprint().equals(lastEnqueuedFingerprint))));
|
||||
noteImageStallLocked(now, windowHasRoom);
|
||||
if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) {
|
||||
return ConnectionOptions.IDLE_SLEEP_MS;
|
||||
@@ -2076,7 +2440,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (messageToPrewrite == null && sessionReady && windowHasRoom && !pendingMessages.isEmpty()) {
|
||||
messageToWrite = pendingMessages.removeFirst();
|
||||
Log.i(TAG, "sending pending message: " + messageToWrite.label);
|
||||
} else if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated
|
||||
} else if (messageToPrewrite == null && !shutdownRequested && !benchmarkActive
|
||||
&& fixedLayoutCreated
|
||||
&& windowHasRoom && !hasPendingImageLocked()
|
||||
&& now >= imageRetryAfterMs
|
||||
&& !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) {
|
||||
@@ -2145,7 +2510,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private boolean canPrewriteCandidate(OutboundMessage message) {
|
||||
if (message == null || !message.isLeftArmMessage) {
|
||||
if (message == null || message.sid == CfwTransport.SID || !message.isLeftArmMessage) {
|
||||
return false;
|
||||
}
|
||||
if (!"image".equals(message.kind)) {
|
||||
@@ -2240,6 +2605,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
frames = Collections.singletonList(prewrittenFrames.get(prewrittenFrames.size() - 1));
|
||||
prewrittenMessage = null;
|
||||
prewrittenFrames = Collections.emptyList();
|
||||
} else if (message.sid == CfwTransport.SID) {
|
||||
if (message.cfwRetries > 0) cfwTransports[message.isLeftArmMessage ? 0 : 1].reset();
|
||||
frames = cfwTransports[message.isLeftArmMessage ? 0 : 1].encode(
|
||||
message.message, message.magic, CfwTransport.BOTH,
|
||||
bleManager.getNegotiatedMtu(writeAddress));
|
||||
} else {
|
||||
frames = BleProtocol.framePb(
|
||||
message.message,
|
||||
@@ -2267,6 +2637,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
if (!result && message.sid == CfwTransport.SID)
|
||||
cfwTransports[message.isLeftArmMessage ? 0 : 1].reset();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -2354,6 +2726,18 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
|
||||
/** Also drain on the worker: a clear/timeout may have removed a blocking
|
||||
* head since the last notification. Fully ACKed controls must never reach
|
||||
* the generic timeout path just because no further BLE reply arrived. */
|
||||
private void drainCfwAcknowledgementsLocked() {
|
||||
while (true) {
|
||||
OutboundMessage first = CfwMessageWindow.acknowledgedHead(inFlightMessages);
|
||||
if (first == null) return;
|
||||
lastAckAtMs = SystemClock.elapsedRealtime();
|
||||
resolveAckLocked(first, first.ackPayload);
|
||||
}
|
||||
}
|
||||
|
||||
private void resolveAckLocked(int sid, int magic, byte[] pb) {
|
||||
Iterator<OutboundMessage> iterator = inFlightMessages.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
@@ -2375,6 +2759,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
message.onAck.run();
|
||||
}
|
||||
consecutiveAckTimeouts = 0;
|
||||
// onAck may have just satisfied a waiter blocked on lock.wait() (e.g.
|
||||
// awaitEvenHubSessionReady polling fixedLayoutCreated/displayedFingerprint
|
||||
// after a create-layout or image ack). Without this, that waiter only
|
||||
// notices on its own up-to-100ms poll tick, adding avoidable latency to
|
||||
// every EvenHub wake. Always called with lock held (see call site).
|
||||
lock.notifyAll();
|
||||
}
|
||||
|
||||
private void logImageUpdateSendLandmarkLocked(OutboundMessage message) {
|
||||
@@ -2432,10 +2822,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private void enqueueCreateLayoutLocked() {
|
||||
// New session/container: re-assert the firmware-debug-flags overlay once
|
||||
// New session: re-assert the firmware-debug-flags overlay once
|
||||
// the layout is ready (the mode-7 send is gated on this having reset).
|
||||
firmwareDebugFlagsLastSent = -1;
|
||||
OutboundMessage message = messageBuilder.createLayout(DASHBOARD_TILE);
|
||||
OutboundMessage message = messageBuilder.createLayout();
|
||||
message.onAck = () -> {
|
||||
startupProbePending = false;
|
||||
clearMessagesOfKindLocked("startup-text-probe");
|
||||
@@ -2480,10 +2870,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the CFW mode-7 diagnostic-flag control op to the dashboard container:
|
||||
* Send the CFW mode-7 diagnostic-flag control op through the private stream:
|
||||
* [7][2] to show the on-glasses debug-flag overlay, [7][1] to hide it. Uses the
|
||||
* arbitrary-payload image path (no bmp/dedup/frame-timing interaction) and does
|
||||
* nothing on stock firmware (which ignores unknown image modes).
|
||||
* arbitrary-payload custom path (no bmp/dedup/frame-timing interaction).
|
||||
*/
|
||||
private void enqueueFirmwareDebugFlagsLocked() {
|
||||
boolean show = firmwareDebugFlagsEnabled;
|
||||
@@ -2567,7 +2956,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// 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
|
||||
if (customFirmwareDetected && connectionOptions.TEXTURE_CACHE_FRAMES
|
||||
&& draws != null && draws.length > 0 && packed.length > 0) {
|
||||
byte[] deltaBase = (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0
|
||||
&& lastEnqueuedWidth == width && lastEnqueuedHeight == height)
|
||||
@@ -2576,8 +2965,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
TexturePlanner.Result tex = TexturePlanner.plan(
|
||||
deltaBase, packed, width, height, draws, textureCache,
|
||||
nextImageFrameId,
|
||||
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS,
|
||||
textureImagesSupported, fwTextSupported);
|
||||
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS);
|
||||
if (tex != null) {
|
||||
nextImageFrameId = tex.nextFid;
|
||||
for (byte[] upload : tex.uploads) {
|
||||
@@ -2585,7 +2973,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
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
|
||||
@@ -2648,7 +3035,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
BleImageOptimizer.TileImagePlan plan = incrementalPayload != null
|
||||
? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), incrementalPayload)
|
||||
: new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId());
|
||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
||||
FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan");
|
||||
if (incrementalLog != null) {
|
||||
FrameTimings.getInstance().log(frameId, incrementalLog);
|
||||
@@ -2656,15 +3042,26 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
|
||||
}
|
||||
|
||||
/** Shared tail of enqueueDesiredImageLocked: enqueue fragments, advance the delta base, log. */
|
||||
/** Queue complete private commands, advance the delta base, and retain frame/ACK bookkeeping. */
|
||||
private void finishEnqueueDesiredImageLocked(
|
||||
BleImageOptimizer.TileImagePlan plan, String fingerprint, int paintMs, int frameId) {
|
||||
int updateId = nextImageUpdateId++;
|
||||
int messageCount = plan.fragments.size();
|
||||
List<byte[]> commands = new ArrayList<>();
|
||||
if (plan.payload.length <= CfwTransport.MAX_MESSAGE) {
|
||||
commands.add(plan.payload);
|
||||
} else {
|
||||
// A noisy full frame can exceed the stream record limit. Repaint it
|
||||
// with independently decodable RLE bands below the decoded-message limit.
|
||||
commands.addAll(BleImageOptimizer.encodeFullFrameBands(
|
||||
plan.packed, plan.width, plan.height, nextImageFrameId));
|
||||
for (int i = 0; i < commands.size(); i++) {
|
||||
nextImageFrameId = nextImageFrameId >= 0xfffe ? 1 : nextImageFrameId + 1;
|
||||
}
|
||||
}
|
||||
int messageCount = commands.size();
|
||||
imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId));
|
||||
for (int i = 0; i < plan.fragments.size(); i++) {
|
||||
BleProtocol.ImageFragment fragment = plan.fragments.get(i);
|
||||
enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, i + 1, messageCount, true);
|
||||
for (int i = 0; i < commands.size(); i++) {
|
||||
enqueueCustomImageLocked(plan, commands.get(i), fingerprint, updateId, i + 1, messageCount);
|
||||
}
|
||||
// This frame is now the base for the next delta (it will be the firmware
|
||||
// shadow once applied), even though it hasn't been acked yet — that is what
|
||||
@@ -2704,16 +3101,16 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
logLine("queue " + message.label);
|
||||
}
|
||||
|
||||
private void enqueueImageFragmentLocked(
|
||||
private void enqueueCustomImageLocked(
|
||||
BleImageOptimizer.TileImagePlan plan,
|
||||
BleProtocol.ImageFragment fragment,
|
||||
byte[] payload,
|
||||
String fingerprint,
|
||||
int updateId,
|
||||
int messageNumber,
|
||||
int messageCount,
|
||||
boolean requestAck
|
||||
int messageCount
|
||||
) {
|
||||
OutboundMessage message = messageBuilder.imageFragment(fragment, plan, requestAck, connectionOptions.sendImagesToLeft);
|
||||
OutboundMessage message = messageBuilder.customMessage("image", payload,
|
||||
"image " + plan.tile.name + "#" + messageNumber, plan.tileIndex, connectionOptions.sendImagesToLeft);
|
||||
message.setImageUpdatePosition(updateId, messageNumber, messageCount);
|
||||
message.onAck = () -> {
|
||||
imageRetryAfterMs = 0;
|
||||
@@ -2777,6 +3174,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private OutboundMessage createBatteryQueryMessageLocked() {
|
||||
OutboundMessage message = messageBuilder.batteryQuery();
|
||||
message.onAck = () -> {
|
||||
BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(message.ackPayload);
|
||||
// Old firmware and missing/malformed extensions must clear any prior reading.
|
||||
ringBattery = ring == null ? -1 : ring.battery;
|
||||
ringCharging = ring == null ? -1 : ring.charging;
|
||||
BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(message.ackPayload);
|
||||
if (snapshot != null) {
|
||||
headsetBattery = snapshot.battery;
|
||||
@@ -2792,12 +3193,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload);
|
||||
if (firmwareInfo != null) {
|
||||
lastFirmwareCapabilities = firmwareInfo.capabilities == null ? "" : firmwareInfo.capabilities;
|
||||
cfwCleanupSupported = hasCapability(firmwareInfo.capabilities, "cleanup11");
|
||||
textureCacheSupported = hasCapability(firmwareInfo.capabilities, "texcache12")
|
||||
&& hasCapability(firmwareInfo.capabilities, "texstr14");
|
||||
textureImagesSupported = hasCapability(firmwareInfo.capabilities, "teximg13");
|
||||
fwTextSupported = hasCapability(firmwareInfo.capabilities, "font15");
|
||||
customFirmwareDetected = firmwareInfo.isFaceclawFirmware();
|
||||
emitFirmwareInfo(firmwareInfo);
|
||||
}
|
||||
};
|
||||
@@ -3339,6 +3735,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
Log.e(TAG, "Transport failure: "+reason);
|
||||
synchronized (lock) {
|
||||
maybeEmitEvenAppConflictLocked(reason);
|
||||
finishBenchmarkLocked(true, "transport failure");
|
||||
sessionReady = false;
|
||||
fixedLayoutCreated = false;
|
||||
startupProbePending = false;
|
||||
@@ -3361,6 +3758,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private void resetSessionStateLocked() {
|
||||
ringBattery = -1;
|
||||
ringCharging = -1;
|
||||
sessionReady = false;
|
||||
shutdownRequested = false;
|
||||
fixedLayoutCreated = false;
|
||||
@@ -3388,6 +3787,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
||||
faceclawWakeControlSentCount = 0;
|
||||
cfwCleanupDelivered = false;
|
||||
for (CfwTransport transport : cfwTransports) transport.reset();
|
||||
lastCfwCleanupAckMagic = 0;
|
||||
wearState = -1;
|
||||
displayedFingerprint = "";
|
||||
@@ -3396,14 +3796,6 @@ 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) {
|
||||
@@ -3528,13 +3920,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private void emitBatteryState(int headsetBattery, int headsetCharging) {
|
||||
final int reportedRingBattery = ringBattery;
|
||||
final int reportedRingCharging = ringCharging;
|
||||
final FaceclawBleCommunicatorListener current = listener;
|
||||
if (current == null) {
|
||||
return;
|
||||
}
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
current.onBatteryState(headsetBattery, headsetCharging);
|
||||
current.onBatteryState(headsetBattery, headsetCharging, reportedRingBattery, reportedRingCharging);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onBatteryState failed", t);
|
||||
}
|
||||
@@ -3548,7 +3942,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
current.onFirmwareInfo(info.leftVersion, info.rightVersion, info.capabilities);
|
||||
current.onFirmwareInfo(info.leftVersion, info.rightVersion, info.extension);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onFirmwareInfo failed", t);
|
||||
}
|
||||
|
||||
+2
-2
@@ -4,12 +4,12 @@ public interface FaceclawBleCommunicatorListener {
|
||||
void onLog(String line);
|
||||
void onStateChange(String phase, String status);
|
||||
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId);
|
||||
void onBatteryState(int headsetBattery, int headsetCharging);
|
||||
void onBatteryState(int headsetBattery, int headsetCharging, int ringBattery, int ringCharging);
|
||||
void onSilentMode(boolean silent);
|
||||
void onWearState(boolean wearing);
|
||||
void onPhoneLockState(boolean locked);
|
||||
void onEvenAppConflict(String message);
|
||||
void onFrameMetrics(int paintMs, int transmitMs, int tileCount);
|
||||
void onFrameFinished(int frameId, String outcome);
|
||||
void onFirmwareInfo(String leftVersion, String rightVersion, String capabilities);
|
||||
void onFirmwareInfo(String leftVersion, String rightVersion, String extension);
|
||||
}
|
||||
@@ -42,6 +42,7 @@ public class FaceclawBleManager {
|
||||
private final ConcurrentHashMap<String, Integer> servicesStatuses = new ConcurrentHashMap<>();
|
||||
|
||||
private final ConcurrentHashMap<String, CountDownLatch> mtuLatches = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Integer> negotiatedMtus = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Integer> mtuStatuses = new ConcurrentHashMap<>();
|
||||
|
||||
private final ConcurrentHashMap<String, CountDownLatch> descriptorLatches = new ConcurrentHashMap<>();
|
||||
@@ -99,6 +100,29 @@ public class FaceclawBleManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Android's bond state for this address (BluetoothDevice.BOND_NONE /
|
||||
* BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined.
|
||||
* BOND_BONDING covers the whole OS pairing flow, including a pairing
|
||||
* dialog that is still waiting for the user.
|
||||
*/
|
||||
public int getBondState(String address) {
|
||||
if (address == null || address.trim().isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
|
||||
return device == null ? -1 : device.getBondState();
|
||||
} catch (Throwable t) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a GATT client for this address is open, i.e. no disconnect callback has arrived for it. */
|
||||
public boolean isConnected(String address) {
|
||||
return address != null && gattClients.containsKey(address);
|
||||
}
|
||||
|
||||
public boolean connect(String address, int timeoutMs) {
|
||||
if (address == null || address.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("address is required");
|
||||
@@ -142,6 +166,7 @@ public class FaceclawBleManager {
|
||||
connectLatches.remove(address);
|
||||
connectResults.remove(address);
|
||||
gattClients.remove(address, gatt);
|
||||
negotiatedMtus.remove(address);
|
||||
gatt.disconnect();
|
||||
gatt.close();
|
||||
return false;
|
||||
@@ -160,6 +185,28 @@ public class FaceclawBleManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */
|
||||
public void prepareBenchmarkLink(String address, int mode) {
|
||||
synchronized (gattLock(address)) {
|
||||
BluetoothGatt gatt = requireGatt(address);
|
||||
if ((mode & 1) != 0) {
|
||||
boolean accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
|
||||
Log.i(TAG, "benchmark HIGH address=" + address + " submitted=" + accepted);
|
||||
}
|
||||
if ((mode & 2) != 0) {
|
||||
Log.i(TAG, "benchmark request 2M address=" + address);
|
||||
gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK,
|
||||
BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED);
|
||||
}
|
||||
gatt.readPhy();
|
||||
}
|
||||
}
|
||||
|
||||
public int getNegotiatedMtu(String address) {
|
||||
Integer mtu = negotiatedMtus.get(address);
|
||||
return mtu == null ? 23 : mtu;
|
||||
}
|
||||
|
||||
public boolean requestMtu(String address, int mtu, int timeoutMs) {
|
||||
synchronized (gattLock(address)) {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
@@ -350,6 +397,7 @@ public class FaceclawBleManager {
|
||||
Object gattLock = gattLock(address);
|
||||
synchronized (gattLock) {
|
||||
BluetoothGatt gatt = gattClients.remove(address);
|
||||
negotiatedMtus.remove(address);
|
||||
if (gatt == null) {
|
||||
return;
|
||||
}
|
||||
@@ -423,6 +471,7 @@ public class FaceclawBleManager {
|
||||
Object gattLock = gattLock(address);
|
||||
synchronized (gattLock) {
|
||||
gattClients.remove(address, gatt);
|
||||
negotiatedMtus.remove(address);
|
||||
gatt.close();
|
||||
}
|
||||
dispatchConnectionState(address, false);
|
||||
@@ -442,6 +491,7 @@ public class FaceclawBleManager {
|
||||
@Override
|
||||
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
|
||||
String address = gatt.getDevice().getAddress();
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) negotiatedMtus.put(address, mtu);
|
||||
mtuStatuses.put(address, status);
|
||||
CountDownLatch latch = mtuLatches.remove(address);
|
||||
if (latch != null) {
|
||||
@@ -451,7 +501,14 @@ public class FaceclawBleManager {
|
||||
|
||||
@Override
|
||||
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
|
||||
Log.i(TAG, "onPhyRead: txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
|
||||
Log.i(TAG, "onPhyRead: address=" + gatt.getDevice().getAddress()
|
||||
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
|
||||
Log.i(TAG, "onPhyUpdate: address=" + gatt.getDevice().getAddress()
|
||||
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+312
-43
@@ -1,25 +1,41 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Connects to the glasses (stock-firmware compatible), reads the device-info /
|
||||
* settings response, and reports the firmware versions plus the CFW capability
|
||||
* string (empty on stock firmware). Used by onboarding to decide whether to
|
||||
* settings response, and reports the firmware versions plus the firmware-
|
||||
* extension string (empty on stock firmware). Used by onboarding to decide whether to
|
||||
* flash. Reuses FaceclawBleManager + BleProtocol; owns its own connection and
|
||||
* runs on a single worker thread. Shows nothing on the lens.
|
||||
*/
|
||||
public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
private static final String TAG = "FaceclawDeviceInfo";
|
||||
private static final int QUERY_TIMEOUT_MS = 4_000;
|
||||
/** Connect + auth attempts per arm; a link that drops mid-pairing is retried once the bond settles. */
|
||||
private static final int ARM_ATTEMPTS = 3;
|
||||
private static final int ARM_RETRY_DELAY_MS = 1_000;
|
||||
/**
|
||||
* Upper bound on waiting for the auth success while Android reports the arm
|
||||
* as BOND_BONDING — i.e. an OS pairing dialog may be sitting there waiting
|
||||
* for the user, which can take a lot longer than the normal 30 s window.
|
||||
*/
|
||||
private static final int PAIRING_WAIT_CAP_MS = 90_000;
|
||||
/** After a fresh bond, how long the firmware gets to notify success on its own before the request is re-sent. */
|
||||
private static final int POST_BOND_GRACE_MS = 2_000;
|
||||
private static final int POLL_MS = 250;
|
||||
|
||||
private final Context context;
|
||||
private final String rightAddress;
|
||||
@@ -36,6 +52,17 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
private byte[] awaitPb = null;
|
||||
private CountDownLatch awaitLatch = null;
|
||||
|
||||
/**
|
||||
* Security-auth wait, kept separate from the generic ack wait: only the
|
||||
* firmware's SUCCESS result (empty result message) may satisfy it. The
|
||||
* firmware answers the request as soon as it arrives, but with a
|
||||
* non-success result until the link is encrypted; accepting that first
|
||||
* reply as "the ack" is what let the probe move on to the other arm while
|
||||
* Android's pairing dialog for this one was still up.
|
||||
*/
|
||||
private final Set<Integer> authMagics = new HashSet<>();
|
||||
private CountDownLatch authLatch = null;
|
||||
|
||||
/**
|
||||
* A sid-0x09 frame that carried firmware versions without matching the
|
||||
* awaited magic — some firmware pushes the settings snapshot on a magic it
|
||||
@@ -92,16 +119,52 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] ack = probeArm(rightAddress, "right");
|
||||
boolean haveLeft = !leftAddress.trim().isEmpty() && !leftAddress.equalsIgnoreCase(rightAddress);
|
||||
|
||||
// Bring up and authenticate BOTH arms before asking for versions.
|
||||
// Each arm is its own peripheral with its own bond, so on a fresh
|
||||
// phone each raises its own pairing prompt; completing both here
|
||||
// means the flash step (and the app proper) find both bonds in
|
||||
// place. Strictly one arm at a time: Android pairs with one device
|
||||
// at a time, and a second arm connected while the first was still
|
||||
// pairing has been seen to drop within a second and never pair
|
||||
// (2026-09-11 logcat).
|
||||
boolean rightAuthenticated = bringUpArm(rightAddress, "right");
|
||||
boolean leftAuthenticated = false;
|
||||
if (haveLeft && !cancelled) {
|
||||
leftAuthenticated = bringUpArm(leftAddress, "left");
|
||||
}
|
||||
if (cancelled) {
|
||||
emitError("Cancelled.");
|
||||
return;
|
||||
}
|
||||
if (!bleManager.isConnected(rightAddress)) {
|
||||
// The right lens may have dropped while the left one paired;
|
||||
// it is bonded by now, so this comes back quickly.
|
||||
emitLog("right lens disconnected while the left lens was set up; reconnecting");
|
||||
rightAuthenticated = bringUpArm(rightAddress, "right");
|
||||
if (cancelled) {
|
||||
emitError("Cancelled.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
byte[] ack = null;
|
||||
String rightFailure = null;
|
||||
try {
|
||||
ack = queryArm(rightAddress, "right", rightAuthenticated);
|
||||
} catch (IllegalStateException e) {
|
||||
rightFailure = e.getMessage() == null ? e.toString() : e.getMessage();
|
||||
emitLog("right-lens query failed: " + rightFailure);
|
||||
}
|
||||
// The right lens is the documented control endpoint, but a silent
|
||||
// right lens has been observed on stock 2.2.9 even after a
|
||||
// successful security auth — run the same probe against the left
|
||||
// lens rather than giving up, and log which lens answered.
|
||||
if (ack == null && !cancelled && !leftAddress.trim().isEmpty()
|
||||
&& !leftAddress.equalsIgnoreCase(rightAddress)) {
|
||||
if (ack == null && !cancelled && haveLeft) {
|
||||
emitLog("right lens did not answer the settings query; probing the left lens");
|
||||
try {
|
||||
ack = probeArm(leftAddress, "left");
|
||||
ack = queryArm(leftAddress, "left", leftAuthenticated);
|
||||
} catch (Exception e) {
|
||||
emitLog("left-lens probe failed: " + (e.getMessage() == null ? e.toString() : e.getMessage()));
|
||||
}
|
||||
@@ -111,17 +174,18 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
return;
|
||||
}
|
||||
if (ack == null) {
|
||||
throw new IllegalStateException(
|
||||
"no response to the device-info query on either lens — check `adb logcat -s "
|
||||
throw new IllegalStateException(rightFailure != null
|
||||
? rightFailure
|
||||
: "no response to the device-info query on either lens — check `adb logcat -s "
|
||||
+ TAG + "` for the frame trace");
|
||||
}
|
||||
|
||||
BleProtocol.FirmwareInfo info = BleProtocol.parseSettingsFirmwareInfo(ack);
|
||||
String left = info == null ? "" : info.leftVersion;
|
||||
String right = info == null ? "" : info.rightVersion;
|
||||
String caps = info == null ? "" : info.capabilities;
|
||||
emitLog("device-info: L=" + left + " R=" + right + " caps=[" + caps + "]");
|
||||
emitResult(left, right, caps);
|
||||
String extension = info == null ? "" : info.extension;
|
||||
emitLog("device-info: L=" + left + " R=" + right + " ext=[" + extension + "]");
|
||||
emitResult(left, right, extension);
|
||||
} catch (Exception e) {
|
||||
String message = cancelled ? "Cancelled." : (e.getMessage() == null ? e.toString() : e.getMessage());
|
||||
emitError(message);
|
||||
@@ -134,34 +198,223 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect + authenticate + prelude + settings read on one lens. Returns the
|
||||
* settings ack protobuf, an unsolicited settings push that carried firmware
|
||||
* versions, or null when the lens never answered the read. Throws on
|
||||
* connect or prelude failure; the caller wraps the fallback lens's attempt
|
||||
* Connect to one arm and complete its security-auth exchange, retrying
|
||||
* when the link drops mid-pairing. Returns whether the auth success was
|
||||
* seen (false = connected but unconfirmed, tolerated because the custom
|
||||
* firmware's response to the exchange is not yet hardware-verified);
|
||||
* throws when the arm cannot be brought up at all.
|
||||
*/
|
||||
private boolean bringUpArm(String address, String label) throws InterruptedException {
|
||||
String lastFailure = null;
|
||||
for (int attempt = 1; attempt <= ARM_ATTEMPTS && !cancelled; attempt++) {
|
||||
if (attempt > 1) {
|
||||
// A link Android dropped while it was still pairing comes back
|
||||
// once the bond settles; reconnecting sooner just fails again.
|
||||
waitForBondToSettle(address, label);
|
||||
if (cancelled) {
|
||||
break;
|
||||
}
|
||||
emitLog("retrying the " + label + " lens (attempt " + attempt + "/" + ARM_ATTEMPTS + "): " + lastFailure);
|
||||
Thread.sleep(ARM_RETRY_DELAY_MS);
|
||||
}
|
||||
emitState("connecting", label);
|
||||
try {
|
||||
connectArm(address);
|
||||
} catch (IllegalStateException e) {
|
||||
lastFailure = e.getMessage() == null ? e.toString() : e.getMessage();
|
||||
emitLog(label + " lens: " + lastFailure);
|
||||
continue;
|
||||
}
|
||||
if (cancelled) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Firmware 2.2.9 answers no queries until the security-auth
|
||||
// exchange completes over an encrypted link; on a phone with no
|
||||
// existing bond this is also what triggers SMP pairing (and its OS
|
||||
// prompt), so it must come before the prelude and query.
|
||||
emitState("authenticating", label);
|
||||
AuthResult result = authenticate(address, label);
|
||||
if (result == AuthResult.SUCCESS) {
|
||||
return true;
|
||||
}
|
||||
if (result == AuthResult.UNCONFIRMED) {
|
||||
return false;
|
||||
}
|
||||
lastFailure = "link dropped during authentication";
|
||||
}
|
||||
if (cancelled) {
|
||||
return false;
|
||||
}
|
||||
throw new IllegalStateException("could not connect to the " + label + " lens (" + address + "): "
|
||||
+ lastFailure + " — if Android showed a Bluetooth pairing request, accept it and try again");
|
||||
}
|
||||
|
||||
private enum AuthResult { SUCCESS, UNCONFIRMED, LINK_DROPPED }
|
||||
|
||||
/**
|
||||
* Send the sid-0x80 authentication request and wait for the firmware's
|
||||
* SUCCESS notification, which only arrives once the link is encrypted.
|
||||
* On an unbonded phone that means waiting through Android's pairing flow
|
||||
* (BOND_BONDING, possibly with a dialog up), so the wait is extended while
|
||||
* the OS reports pairing in progress and the request is re-sent once
|
||||
* after a fresh bond in case the pre-pairing write was dropped. Bond
|
||||
* state is polled rather than awaited via broadcast; 250 ms granularity
|
||||
* is plenty here.
|
||||
*/
|
||||
private AuthResult authenticate(String address, String label) throws InterruptedException {
|
||||
long start = SystemClock.elapsedRealtime();
|
||||
long deadline = start + ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS;
|
||||
long hardDeadline = start + PAIRING_WAIT_CAP_MS;
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
synchronized (lock) {
|
||||
authMagics.clear();
|
||||
authLatch = latch;
|
||||
}
|
||||
int initialBond = bleManager.getBondState(address);
|
||||
emitLog("security auth: " + label + " lens, Android bond state " + bondStateName(initialBond));
|
||||
try {
|
||||
int sends = 0;
|
||||
boolean lastSendFailed = false;
|
||||
long nextSendAt = start;
|
||||
long bondedAt = -1;
|
||||
boolean resentAfterBond = false;
|
||||
boolean loggedBonding = false;
|
||||
while (!cancelled) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
int bond = bleManager.getBondState(address);
|
||||
|
||||
if (bond == BluetoothDevice.BOND_BONDING) {
|
||||
// OS pairing in progress — the firmware will answer once the
|
||||
// link is encrypted. Don't write, don't give up, don't move
|
||||
// on to the other arm.
|
||||
if (!loggedBonding) {
|
||||
emitLog("Android is pairing with the " + label + " lens; waiting for the user to accept");
|
||||
loggedBonding = true;
|
||||
}
|
||||
if (now >= hardDeadline) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (bond == BluetoothDevice.BOND_BONDED && initialBond != BluetoothDevice.BOND_BONDED
|
||||
&& bondedAt < 0) {
|
||||
bondedAt = now;
|
||||
emitLog("Android bond established with the " + label + " lens; waiting for the auth success");
|
||||
deadline = Math.max(deadline, now + ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS);
|
||||
}
|
||||
boolean wantSend = sends == 0
|
||||
|| (lastSendFailed && sends < 3 && now >= nextSendAt)
|
||||
|| (bondedAt >= 0 && !resentAfterBond && now - bondedAt >= POST_BOND_GRACE_MS);
|
||||
if (wantSend) {
|
||||
if (bondedAt >= 0) {
|
||||
resentAfterBond = true;
|
||||
if (sends > 0) {
|
||||
emitLog("re-sending the auth request after pairing (" + label + " lens)");
|
||||
}
|
||||
}
|
||||
lastSendFailed = !sendAuthRequest(address);
|
||||
sends++;
|
||||
nextSendAt = SystemClock.elapsedRealtime() + ARM_RETRY_DELAY_MS;
|
||||
if (lastSendFailed && !bleManager.isConnected(address)) {
|
||||
return AuthResult.LINK_DROPPED;
|
||||
}
|
||||
}
|
||||
if (now >= deadline) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (latch.await(POLL_MS, TimeUnit.MILLISECONDS)) {
|
||||
emitLog("security auth complete: " + label + " lens (" + address + ")");
|
||||
return AuthResult.SUCCESS;
|
||||
}
|
||||
if (!bleManager.isConnected(address)) {
|
||||
emitLog("security auth: " + label + " lens disconnected while waiting"
|
||||
+ (bond == BluetoothDevice.BOND_BONDING ? " (Android was still pairing)" : ""));
|
||||
return AuthResult.LINK_DROPPED;
|
||||
}
|
||||
}
|
||||
if (cancelled) {
|
||||
return AuthResult.UNCONFIRMED;
|
||||
}
|
||||
emitLog("security auth unconfirmed: " + label + " lens (" + address + "), Android bond state "
|
||||
+ bondStateName(bleManager.getBondState(address)));
|
||||
return AuthResult.UNCONFIRMED;
|
||||
} finally {
|
||||
synchronized (lock) {
|
||||
authLatch = null;
|
||||
authMagics.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean sendAuthRequest(String address) {
|
||||
int magic = allocMagic();
|
||||
int seq;
|
||||
synchronized (lock) {
|
||||
authMagics.add(magic);
|
||||
seq = nextSeq++ & 0xff;
|
||||
}
|
||||
byte[] payload = BleProtocol.buildAuthenticationRequest(magic);
|
||||
emitLog(String.format("tx %s sid=0x%02x flag=0x%02x magic=%d seq=0x%02x len=%d",
|
||||
address, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, magic, seq, payload.length));
|
||||
List<byte[]> frames = BleProtocol.framePb(payload, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, seq);
|
||||
boolean written;
|
||||
try {
|
||||
written = bleManager.writeFrames(
|
||||
address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
} catch (IllegalStateException e) {
|
||||
// "Not connected" — the link went away underneath us.
|
||||
written = false;
|
||||
}
|
||||
if (!written) {
|
||||
emitLog("tx write FAILED sid=0x80 (" + address + ")");
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
/** Block while Android reports this arm as BOND_BONDING (pairing dialog up / SMP in flight). */
|
||||
private void waitForBondToSettle(String address, String label) throws InterruptedException {
|
||||
long cap = SystemClock.elapsedRealtime() + PAIRING_WAIT_CAP_MS;
|
||||
boolean logged = false;
|
||||
while (!cancelled && bleManager.getBondState(address) == BluetoothDevice.BOND_BONDING
|
||||
&& SystemClock.elapsedRealtime() < cap) {
|
||||
if (!logged) {
|
||||
emitLog("waiting for Android to finish pairing with the " + label + " lens before reconnecting");
|
||||
logged = true;
|
||||
}
|
||||
Thread.sleep(POLL_MS);
|
||||
}
|
||||
}
|
||||
|
||||
private static String bondStateName(int state) {
|
||||
switch (state) {
|
||||
case BluetoothDevice.BOND_NONE:
|
||||
return "none";
|
||||
case BluetoothDevice.BOND_BONDING:
|
||||
return "bonding";
|
||||
case BluetoothDevice.BOND_BONDED:
|
||||
return "bonded";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prelude + settings read on an already connected/authenticated lens.
|
||||
* Returns the settings ack protobuf, an unsolicited settings push that
|
||||
* carried firmware versions, or null when the lens never answered the read.
|
||||
* Throws on prelude failure; the caller wraps the fallback lens's attempt
|
||||
* so its failure cannot mask the primary lens's outcome.
|
||||
*/
|
||||
private byte[] probeArm(String address, String label) throws InterruptedException {
|
||||
emitState("connecting", label);
|
||||
connectArm(address);
|
||||
if (cancelled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Firmware 2.2.9 answers no queries until the security-auth exchange
|
||||
// completes over an encrypted link; on a phone with no existing bond
|
||||
// this is also what triggers SMP pairing (and its OS prompt), so it
|
||||
// must come before the prelude and query. Soft: this probe also runs
|
||||
// against the custom firmware, whose response to the exchange is not
|
||||
// yet hardware-verified, so an unconfirmed auth falls through.
|
||||
emitState("authenticating", label);
|
||||
boolean authenticated = authenticate(address);
|
||||
private byte[] queryArm(String address, String label, boolean authenticated) throws InterruptedException {
|
||||
if (cancelled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
emitState("querying", label);
|
||||
// Session prelude, then a settings/device-info read (both arms'
|
||||
// versions and the CFW capability string ride back in one response).
|
||||
// versions and the firmware-extension string ride back in one response).
|
||||
if (writeAndAwaitAck(address, BleProtocol.PRELUDE_ACK_SID, BleProtocol.FLAG_REQUEST,
|
||||
BleProtocol.PRELUDE_ACK_MAGIC, BleProtocol.PRELUDE_F5872_PAYLOAD,
|
||||
ConnectionOptions.PRELUDE_TIMEOUT_MS) == null) {
|
||||
@@ -191,15 +444,6 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean authenticate(String address) throws InterruptedException {
|
||||
int magic = allocMagic();
|
||||
byte[] ack = writeAndAwaitAck(address, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH,
|
||||
magic, BleProtocol.buildAuthenticationRequest(magic), ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS);
|
||||
boolean success = ack != null && BleProtocol.isAuthenticationSuccess(ack, magic);
|
||||
emitLog(success ? "security auth complete: " + address : "security auth unconfirmed: " + address);
|
||||
return success;
|
||||
}
|
||||
|
||||
private void connectArm(String address) {
|
||||
if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) {
|
||||
throw new IllegalStateException("connect failed");
|
||||
@@ -284,7 +528,32 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
unsolicitedSettingsPb = frame.pb;
|
||||
}
|
||||
if (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT) {
|
||||
return; // async event, not an ack
|
||||
// Async event, not an ack. This includes the lens's own periodic
|
||||
// sid-0x80 notifies (device-chosen magic, non-empty result).
|
||||
return;
|
||||
}
|
||||
if (frame.sid == BleProtocol.SID_SECURITY_AUTH) {
|
||||
boolean success = false;
|
||||
boolean ours = false;
|
||||
synchronized (lock) {
|
||||
if (authLatch != null) {
|
||||
ours = authMagics.contains(frame.msgSeq);
|
||||
for (int magic : authMagics) {
|
||||
if (BleProtocol.isAuthenticationSuccess(frame.pb, magic)) {
|
||||
success = true;
|
||||
authLatch.countDown();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ours && !success) {
|
||||
// Expected before the link is encrypted: the firmware answers
|
||||
// the request straight away with a non-success result and
|
||||
// sends the real success once pairing/encryption completes.
|
||||
emitLog("auth reply for magic " + frame.msgSeq + " is not the success result; still waiting");
|
||||
}
|
||||
return;
|
||||
}
|
||||
synchronized (lock) {
|
||||
if (awaitLatch != null && frame.sid == awaitSid && frame.msgSeq == awaitMagic) {
|
||||
@@ -329,11 +598,11 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
});
|
||||
}
|
||||
|
||||
private void emitResult(String left, String right, String caps) {
|
||||
private void emitResult(String left, String right, String extension) {
|
||||
mainHandler.post(() -> {
|
||||
FaceclawDeviceInfoProbeListener current = listener;
|
||||
if (current != null) {
|
||||
current.onResult(left, right, caps);
|
||||
current.onResult(left, right, extension);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+3
-3
@@ -4,11 +4,11 @@ package com.faceclaw.app;
|
||||
public interface FaceclawDeviceInfoProbeListener {
|
||||
void onLog(String line);
|
||||
|
||||
/** Lifecycle: connecting, querying. */
|
||||
/** Lifecycle: connecting/authenticating for each arm, then querying. */
|
||||
void onState(String state, String detail);
|
||||
|
||||
/** Terminal success: firmware versions and the CFW capability string (may be empty). */
|
||||
void onResult(String leftVersion, String rightVersion, String capabilities);
|
||||
/** Terminal success: firmware versions and the firmware-extension string (empty on stock). */
|
||||
void onResult(String leftVersion, String rightVersion, String extension);
|
||||
|
||||
/** Terminal failure (couldn't connect or read the version). */
|
||||
void onError(String message);
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.faceclaw.app;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
@@ -47,19 +48,26 @@ public final class FaceclawEvenAppDetector {
|
||||
return hasNotfication;
|
||||
}
|
||||
|
||||
public static void openEvenAppSettings(Context context) {
|
||||
/** Returns "opened", "not-installed", or "failed" for the phone UI. */
|
||||
public static String openEvenAppSettings(Context context) {
|
||||
if (context == null) {
|
||||
return;
|
||||
return "failed";
|
||||
}
|
||||
Context appContext = context.getApplicationContext();
|
||||
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
intent.setData(Uri.parse("package:" + EVEN_PACKAGE_NAME));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
try {
|
||||
// Settings may launch successfully and then immediately close for a
|
||||
// missing package, so check before starting the activity.
|
||||
appContext.getPackageManager().getApplicationInfo(EVEN_PACKAGE_NAME, 0);
|
||||
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
intent.setData(Uri.parse("package:" + EVEN_PACKAGE_NAME));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
appContext.startActivity(intent);
|
||||
} catch (Throwable t) {
|
||||
return "opened";
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return "not-installed";
|
||||
} catch (Exception t) {
|
||||
Log.w(TAG, "failed to open Even app settings", t);
|
||||
openNotificationAccessSettings(appContext);
|
||||
return "failed";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-5
@@ -22,7 +22,12 @@ public class FaceclawForegroundService extends Service {
|
||||
public static final String ACTION_STOP = "com.faceclaw.app.action.STOP";
|
||||
public static final String EXTRA_TEXT = "text";
|
||||
|
||||
private static final String CHANNEL_ID = "faceclaw-dashboard";
|
||||
// Channel id was bumped from "faceclaw-dashboard" when the badge setting
|
||||
// changed: Android freezes a channel's showBadge flag at creation, so the
|
||||
// old channel (which let Samsung's launcher count the pinned notification
|
||||
// as a red "1" badge) is deleted on upgrade rather than reused.
|
||||
private static final String LEGACY_CHANNEL_ID = "faceclaw-dashboard";
|
||||
private static final String CHANNEL_ID = "faceclaw-connection";
|
||||
private static final int NOTIFICATION_ID = 4201;
|
||||
|
||||
@Override
|
||||
@@ -43,7 +48,7 @@ public class FaceclawForegroundService extends Service {
|
||||
|
||||
ensureNotificationChannel();
|
||||
Notification notification = buildNotification(
|
||||
text != null && !text.trim().isEmpty() ? text : "Keeping the dashboard connected"
|
||||
text != null && !text.trim().isEmpty() ? text : "Connected to glasses"
|
||||
);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
@@ -69,14 +74,18 @@ public class FaceclawForegroundService extends Service {
|
||||
|
||||
NotificationChannel channel = new NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"Faceclaw dashboard",
|
||||
"Glasses connection",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
);
|
||||
channel.setDescription("Keeps the Faceclaw dashboard connected to the glasses.");
|
||||
channel.setDescription("Keeps Faceclaw connected to the glasses.");
|
||||
// The pinned status notification must not count toward the launcher
|
||||
// icon's notification badge.
|
||||
channel.setShowBadge(false);
|
||||
|
||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||
if (manager != null) {
|
||||
manager.createNotificationChannel(channel);
|
||||
manager.deleteNotificationChannel(LEGACY_CHANNEL_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +105,7 @@ public class FaceclawForegroundService extends Service {
|
||||
: new Notification.Builder(this);
|
||||
|
||||
return builder
|
||||
.setContentTitle("faceclaw dashboard")
|
||||
.setContentTitle("Faceclaw")
|
||||
.setContentText(text)
|
||||
.setSmallIcon(getApplicationInfo().icon)
|
||||
.setContentIntent(contentIntent)
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.ContentObserver;
|
||||
import android.graphics.Bitmap;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaDescription;
|
||||
@@ -27,6 +28,8 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class FaceclawMediaController {
|
||||
private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
|
||||
|
||||
private final Context appContext;
|
||||
private final Object lock = new Object();
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
@@ -70,9 +73,27 @@ public class FaceclawMediaController {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Fires when the user grants or revokes notification-listener access in
|
||||
* system settings. Session access depends on that grant, so the sessions
|
||||
* listener is (re)registered and the state re-emitted whenever it changes;
|
||||
* otherwise a grant made after start() would never be noticed.
|
||||
*/
|
||||
private final ContentObserver notificationAccessObserver = new ContentObserver(mainHandler) {
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
synchronized (lock) {
|
||||
if (!started) return;
|
||||
syncSessionsListenerLocked();
|
||||
refreshActiveControllerLocked(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private volatile FaceclawMediaControllerListener listener;
|
||||
private MediaController activeController;
|
||||
private boolean started;
|
||||
private boolean sessionsListenerRegistered;
|
||||
private Set<String> ignoredPackages = new HashSet<>();
|
||||
|
||||
public FaceclawMediaController(Context context) {
|
||||
@@ -96,26 +117,48 @@ public class FaceclawMediaController {
|
||||
return;
|
||||
}
|
||||
started = true;
|
||||
if (!isNotificationAccessEnabled()) {
|
||||
emitStateLocked();
|
||||
return;
|
||||
}
|
||||
if (sessionManager != null) {
|
||||
try {
|
||||
sessionManager.addOnActiveSessionsChangedListener(
|
||||
sessionsChangedListener,
|
||||
listenerComponent,
|
||||
mainHandler
|
||||
);
|
||||
} catch (SecurityException ignored) {
|
||||
emitStateLocked();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
appContext.getContentResolver().registerContentObserver(
|
||||
Settings.Secure.getUriFor(ENABLED_NOTIFICATION_LISTENERS),
|
||||
false,
|
||||
notificationAccessObserver
|
||||
);
|
||||
} catch (Exception e) {
|
||||
Log.w("FaceclawMedia", "notification access observer registration failed", e);
|
||||
}
|
||||
syncSessionsListenerLocked();
|
||||
refreshActiveControllerLocked(null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the active-sessions listener registered exactly while notification
|
||||
* access is granted. Registration throws SecurityException without the
|
||||
* grant, so this is retried from the settings observer rather than only
|
||||
* attempted once at start().
|
||||
*/
|
||||
private void syncSessionsListenerLocked() {
|
||||
if (sessionManager == null) return;
|
||||
boolean accessEnabled = started && isNotificationAccessEnabled();
|
||||
if (accessEnabled && !sessionsListenerRegistered) {
|
||||
try {
|
||||
sessionManager.addOnActiveSessionsChangedListener(
|
||||
sessionsChangedListener,
|
||||
listenerComponent,
|
||||
mainHandler
|
||||
);
|
||||
sessionsListenerRegistered = true;
|
||||
} catch (SecurityException ignored) {
|
||||
}
|
||||
} else if (!accessEnabled && sessionsListenerRegistered) {
|
||||
try {
|
||||
sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener);
|
||||
} catch (SecurityException ignored) {
|
||||
}
|
||||
sessionsListenerRegistered = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Apply the complete preference set and immediately choose an allowed session. */
|
||||
public void setIgnoredPackagesJson(String packagesJson) {
|
||||
Set<String> packages = new HashSet<>();
|
||||
@@ -139,12 +182,11 @@ public class FaceclawMediaController {
|
||||
return;
|
||||
}
|
||||
started = false;
|
||||
if (sessionManager != null) {
|
||||
try {
|
||||
sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener);
|
||||
} catch (SecurityException ignored) {
|
||||
}
|
||||
try {
|
||||
appContext.getContentResolver().unregisterContentObserver(notificationAccessObserver);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
syncSessionsListenerLocked();
|
||||
setActiveControllerLocked(null);
|
||||
emitStateLocked();
|
||||
}
|
||||
@@ -377,7 +419,7 @@ public class FaceclawMediaController {
|
||||
private boolean isNotificationAccessEnabled() {
|
||||
String enabledListeners = Settings.Secure.getString(
|
||||
appContext.getContentResolver(),
|
||||
"enabled_notification_listeners"
|
||||
ENABLED_NOTIFICATION_LISTENERS
|
||||
);
|
||||
if (enabledListeners == null || enabledListeners.isEmpty()) {
|
||||
return false;
|
||||
|
||||
+120
-22
@@ -13,6 +13,7 @@ import com.k2fsa.sherpa.onnx.OfflineRecognizer;
|
||||
import com.k2fsa.sherpa.onnx.OfflineRecognizerConfig;
|
||||
import com.k2fsa.sherpa.onnx.OfflineRecognizerResult;
|
||||
import com.k2fsa.sherpa.onnx.OfflineStream;
|
||||
import com.k2fsa.sherpa.onnx.OfflineWhisperModelConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -61,12 +62,43 @@ public class FaceclawVoiceController {
|
||||
"decoder_model_merged.ort",
|
||||
"tokens.txt"
|
||||
};
|
||||
// Second on-device model: sherpa-onnx's offline Whisper backend (base.en,
|
||||
// int8-quantized -- see the model-choice note in asr-model.ts). Directory
|
||||
// shared with the TS-side download flow, same convention as ASR_MODEL_DIR.
|
||||
//
|
||||
// On every call, sherpa-onnx (offline-recognizer-whisper-impl.h, v1.13.0)
|
||||
// runs Whisper's encoder over the whole buffer plus 1000 frames (~10s) of
|
||||
// zero tail padding, capped at 30s. Re-decoding on Moonshine's
|
||||
// TRANSCRIPT_DECODE_INTERVAL_MS live-partial cadence would repeat that
|
||||
// encode, growing with the utterance, roughly 1.4x/second while the user is
|
||||
// still speaking, so onboardModelKind gates that loop off for Whisper --
|
||||
// see processRecognizer(). Whisper is also known to hallucinate text on
|
||||
// near-silent input; recognizeTranscriptSegment() gates that too.
|
||||
private static final String ASR_WHISPER_MODEL_DIR = "sherpa-onnx-whisper-base-en-int8";
|
||||
private static final String[] ASR_WHISPER_MODEL_FILES = {
|
||||
"base.en-encoder.int8.onnx",
|
||||
"base.en-decoder.int8.onnx",
|
||||
"base.en-tokens.txt"
|
||||
};
|
||||
// Below this peak amplitude (pre-normalization, of a full-scale +/-1.0f
|
||||
// buffer) a segment is treated as silence and never reaches the Whisper
|
||||
// recognizer at all, rather than risking a hallucinated non-answer. Picked
|
||||
// conservatively low (well under typical mic noise floor already seen in
|
||||
// this pipeline's normalization target) -- UNTESTED on real hardware, tune
|
||||
// against real glasses captures rather than trusting this number.
|
||||
private static final float WHISPER_SILENCE_PEAK_THRESHOLD = 0.01f;
|
||||
|
||||
private enum VoiceInputMode {
|
||||
ONBOARD, // on-phone Moonshine transcription
|
||||
ONBOARD, // on-phone transcription (Moonshine or Whisper; see onboardModelKind)
|
||||
CLOUD // decode locally, emit PCM for a cloud recognizer on the TS side
|
||||
}
|
||||
|
||||
/** Which on-device model ONBOARD mode uses. Set via setOnboardModelKind() before start(). */
|
||||
private enum OnboardModelKind {
|
||||
MOONSHINE,
|
||||
WHISPER
|
||||
}
|
||||
|
||||
private final Context appContext;
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private final Object lock = new Object();
|
||||
@@ -86,6 +118,7 @@ public class FaceclawVoiceController {
|
||||
private volatile boolean usePhoneMic;
|
||||
private boolean activePhoneMic;
|
||||
private VoiceInputMode mode = VoiceInputMode.CLOUD;
|
||||
private volatile OnboardModelKind onboardModelKind = OnboardModelKind.MOONSHINE;
|
||||
private OfflineRecognizer recognizer;
|
||||
private FaceclawLc3Decoder lc3Decoder;
|
||||
private final float[] transcriptSamples = new float[TRANSCRIPT_SEGMENT_MAX_SAMPLES];
|
||||
@@ -150,6 +183,17 @@ public class FaceclawVoiceController {
|
||||
this.saveRecordings = saveRecordings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Which on-device model {@link #start}("onboard") should load: "whisper"
|
||||
* selects the second on-device model (sherpa-onnx offline Whisper); any
|
||||
* other value (including null/absent) keeps the existing Moonshine model,
|
||||
* so callers that never call this see unchanged behavior. Must be set
|
||||
* before {@link #start}; has no effect in CLOUD mode.
|
||||
*/
|
||||
public void setOnboardModelKind(String kind) {
|
||||
this.onboardModelKind = "whisper".equals(kind) ? OnboardModelKind.WHISPER : OnboardModelKind.MOONSHINE;
|
||||
}
|
||||
|
||||
/**
|
||||
* When true, watch the decoded PCM and fire {@code onSpeechEnd} once the
|
||||
* speaker stops. Used by hands-free ("Hey Even") capture, which has no
|
||||
@@ -222,20 +266,26 @@ public class FaceclawVoiceController {
|
||||
}
|
||||
|
||||
public void start(String requestedMode) {
|
||||
start(requestedMode, 0);
|
||||
}
|
||||
|
||||
public void start(String requestedMode, int captureId) {
|
||||
synchronized (lock) {
|
||||
if (started) {
|
||||
emitStatus("Voice control is already listening.");
|
||||
emitStopped(captureId);
|
||||
return;
|
||||
}
|
||||
if (!usePhoneMic && (communicator == null || !communicator.isSessionReady())) {
|
||||
emitStatus("Voice control needs an active G2 connection.");
|
||||
emitStopped(captureId);
|
||||
return;
|
||||
}
|
||||
mode = parseMode(requestedMode);
|
||||
activePhoneMic = usePhoneMic;
|
||||
started = true;
|
||||
audioStarted = false;
|
||||
workerThread = new Thread(this::runLoop, "FaceclawVoiceController");
|
||||
workerThread = new Thread(() -> runLoop(captureId), "FaceclawVoiceController");
|
||||
workerThread.start();
|
||||
}
|
||||
}
|
||||
@@ -306,18 +356,19 @@ public class FaceclawVoiceController {
|
||||
return VoiceInputMode.ONBOARD;
|
||||
}
|
||||
|
||||
private void runLoop() {
|
||||
private void runLoop(int captureId) {
|
||||
try {
|
||||
deleteLegacyKwsFiles();
|
||||
VoiceInputMode currentMode = mode;
|
||||
OnboardModelKind currentOnboardKind = onboardModelKind;
|
||||
if (currentMode == VoiceInputMode.ONBOARD) {
|
||||
File modelDir = findAsrModelDir();
|
||||
File modelDir = findAsrModelDir(currentOnboardKind);
|
||||
if (modelDir == null) {
|
||||
emitStatus("Voice model not downloaded (see Settings > Voice).");
|
||||
return;
|
||||
}
|
||||
emitStatus("Loading transcription model...");
|
||||
recognizer = new OfflineRecognizer(buildRecognizerConfig(modelDir));
|
||||
recognizer = new OfflineRecognizer(buildRecognizerConfig(modelDir, currentOnboardKind));
|
||||
resetTranscriptState();
|
||||
lastTranscript = "";
|
||||
}
|
||||
@@ -387,6 +438,7 @@ public class FaceclawVoiceController {
|
||||
audioStarted = false;
|
||||
workerThread = null;
|
||||
}
|
||||
emitStopped(captureId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -453,32 +505,48 @@ public class FaceclawVoiceController {
|
||||
return b.array();
|
||||
}
|
||||
|
||||
private OfflineRecognizerConfig buildRecognizerConfig(File modelDir) {
|
||||
private OfflineRecognizerConfig buildRecognizerConfig(File modelDir, OnboardModelKind kind) {
|
||||
OfflineModelConfig.Builder modelConfig = OfflineModelConfig.builder()
|
||||
.setNumThreads(1);
|
||||
if (kind == OnboardModelKind.WHISPER) {
|
||||
modelConfig
|
||||
.setWhisper(OfflineWhisperModelConfig.builder()
|
||||
.setEncoder(new File(modelDir, "base.en-encoder.int8.onnx").getAbsolutePath())
|
||||
.setDecoder(new File(modelDir, "base.en-decoder.int8.onnx").getAbsolutePath())
|
||||
.setLanguage("en")
|
||||
.setTask("transcribe")
|
||||
.build())
|
||||
.setTokens(new File(modelDir, "base.en-tokens.txt").getAbsolutePath());
|
||||
} else {
|
||||
modelConfig
|
||||
.setMoonshine(OfflineMoonshineModelConfig.builder()
|
||||
.setEncoder(new File(modelDir, "encoder_model.ort").getAbsolutePath())
|
||||
.setMergedDecoder(new File(modelDir, "decoder_model_merged.ort").getAbsolutePath())
|
||||
.build())
|
||||
.setTokens(new File(modelDir, "tokens.txt").getAbsolutePath());
|
||||
}
|
||||
return OfflineRecognizerConfig.builder()
|
||||
.setFeatureConfig(FeatureConfig.builder()
|
||||
.setSampleRate(SAMPLE_RATE)
|
||||
.setFeatureDim(FEATURE_DIM)
|
||||
.build())
|
||||
.setModelConfig(OfflineModelConfig.builder()
|
||||
.setMoonshine(OfflineMoonshineModelConfig.builder()
|
||||
.setEncoder(new File(modelDir, "encoder_model.ort").getAbsolutePath())
|
||||
.setMergedDecoder(new File(modelDir, "decoder_model_merged.ort").getAbsolutePath())
|
||||
.build())
|
||||
.setTokens(new File(modelDir, "tokens.txt").getAbsolutePath())
|
||||
.setNumThreads(1)
|
||||
.build())
|
||||
.setModelConfig(modelConfig.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* The Moonshine model directory, populated by the download flow in
|
||||
* asr-model.ts (releases before 0.5.0 copied the same files out of the
|
||||
* APK, so upgraded installs are already complete). Null when any file is
|
||||
* The on-device model directory for the given kind, populated by the
|
||||
* download flow in asr-model.ts (releases before 0.5.0 copied the
|
||||
* Moonshine files out of the APK directly, so upgraded installs are
|
||||
* already complete for that model). Null when any expected file is
|
||||
* missing, i.e. the model still needs to be downloaded.
|
||||
*/
|
||||
private File findAsrModelDir() {
|
||||
File modelDir = new File(appContext.getFilesDir(), ASR_ROOT + File.separator + ASR_MODEL_DIR);
|
||||
for (String fileName : ASR_MODEL_FILES) {
|
||||
private File findAsrModelDir(OnboardModelKind kind) {
|
||||
boolean whisper = kind == OnboardModelKind.WHISPER;
|
||||
String dirName = whisper ? ASR_WHISPER_MODEL_DIR : ASR_MODEL_DIR;
|
||||
String[] fileNames = whisper ? ASR_WHISPER_MODEL_FILES : ASR_MODEL_FILES;
|
||||
File modelDir = new File(appContext.getFilesDir(), ASR_ROOT + File.separator + dirName);
|
||||
for (String fileName : fileNames) {
|
||||
File file = new File(modelDir, fileName);
|
||||
if (!file.exists() || file.length() == 0) {
|
||||
return null;
|
||||
@@ -658,6 +726,16 @@ public class FaceclawVoiceController {
|
||||
|
||||
private void processRecognizer(float[] samples) {
|
||||
appendTranscriptSamples(samples);
|
||||
// Each Whisper call re-encodes the whole buffer plus ~10s of tail
|
||||
// padding (see the ASR_WHISPER_* comment above),
|
||||
// so it skips the live-partial redecode Moonshine does on this
|
||||
// interval and only decodes when a segment commits (8s buffer fill)
|
||||
// or the utterance ends (decodeTranscript(true) in runLoop()). This
|
||||
// means no live preview text while speaking in Whisper mode -- status
|
||||
// stays "Listening..." until release. This is a deliberate tradeoff.
|
||||
if (onboardModelKind == OnboardModelKind.WHISPER) {
|
||||
return;
|
||||
}
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
if (transcriptSampleCount >= TRANSCRIPT_MIN_SAMPLES
|
||||
&& now - lastTranscriptDecodeAtMs >= TRANSCRIPT_DECODE_INTERVAL_MS) {
|
||||
@@ -767,6 +845,14 @@ public class FaceclawVoiceController {
|
||||
return "";
|
||||
}
|
||||
float[] segment = Arrays.copyOf(transcriptSamples, sampleCount);
|
||||
// Whisper hallucinates text on near-silent input (a known quirk of the
|
||||
// model, not this pipeline); gate it on the PRE-normalization peak, since
|
||||
// normalizePeak() below would otherwise amplify true silence right up to
|
||||
// the target level and hide the very thing being checked for. Moonshine
|
||||
// does not share this failure mode in practice, so it is left unchanged.
|
||||
if (onboardModelKind == OnboardModelKind.WHISPER && peakAmplitude(segment) < WHISPER_SILENCE_PEAK_THRESHOLD) {
|
||||
return "";
|
||||
}
|
||||
normalizePeak(segment);
|
||||
OfflineStream offlineStream = currentRecognizer.createStream();
|
||||
try {
|
||||
@@ -780,7 +866,7 @@ public class FaceclawVoiceController {
|
||||
}
|
||||
}
|
||||
|
||||
private static void normalizePeak(float[] samples) {
|
||||
private static float peakAmplitude(float[] samples) {
|
||||
float peak = 0f;
|
||||
for (float s : samples) {
|
||||
float a = Math.abs(s);
|
||||
@@ -788,6 +874,11 @@ public class FaceclawVoiceController {
|
||||
peak = a;
|
||||
}
|
||||
}
|
||||
return peak;
|
||||
}
|
||||
|
||||
private static void normalizePeak(float[] samples) {
|
||||
float peak = peakAmplitude(samples);
|
||||
if (peak <= 0f) {
|
||||
return;
|
||||
}
|
||||
@@ -805,7 +896,7 @@ public class FaceclawVoiceController {
|
||||
(committedTranscriptSampleCount + transcriptSampleCount) / (double) SAMPLE_RATE;
|
||||
String preview = text.length() <= TRANSCRIPT_LOG_PREVIEW_CHARS
|
||||
? text : text.substring(0, TRANSCRIPT_LOG_PREVIEW_CHARS) + "...";
|
||||
Log.i(TAG, "Moonshine decode final=" + isFinal
|
||||
Log.i(TAG, (onboardModelKind == OnboardModelKind.WHISPER ? "Whisper" : "Moonshine") + " decode final=" + isFinal
|
||||
+ " audioSec=" + String.format(java.util.Locale.US, "%.2f", totalAudioSec)
|
||||
+ " segmentAudioSec=" + String.format(java.util.Locale.US, "%.2f", segmentSampleCount / (double) SAMPLE_RATE)
|
||||
+ " textLen=" + text.length() + " text=\"" + preview + "\"");
|
||||
@@ -930,6 +1021,13 @@ public class FaceclawVoiceController {
|
||||
mainHandler.post(currentListener::onSpeechEnd);
|
||||
}
|
||||
|
||||
private void emitStopped(int captureId) {
|
||||
FaceclawVoiceControllerListener currentListener = listener;
|
||||
if (currentListener != null) {
|
||||
mainHandler.post(() -> currentListener.onStopped(captureId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides when a hands-free utterance is over, so "Hey Even" capture can
|
||||
* stop without a button release.
|
||||
|
||||
+3
@@ -10,6 +10,9 @@ public interface FaceclawVoiceControllerListener {
|
||||
*/
|
||||
void onTranscript(String text, boolean isFinal);
|
||||
|
||||
/** Capture has finished, after the final transcript or a startup/recognition error. */
|
||||
void onStopped(int captureId);
|
||||
|
||||
/**
|
||||
* Decoded microphone audio: 16 kHz mono signed 16-bit little-endian PCM.
|
||||
* Emitted in every mode so raw-PCM consumers (levels, recording, captions)
|
||||
|
||||
@@ -60,6 +60,10 @@ public final class FaceclawWearBridge {
|
||||
public static final String PATH_PREFIX = "/faceclaw";
|
||||
public static final String PATH_STATE = "/faceclaw/state";
|
||||
public static final String PATH_STATE_REQUEST = "/faceclaw/state/request";
|
||||
/** Watch -> phone: the watch's own battery; carries no seq and is never acked. */
|
||||
public static final String PATH_WATCH_BATTERY = "/faceclaw/battery";
|
||||
/** Phone -> watch: answered with PATH_WATCH_BATTERY even while the watch app is closed. */
|
||||
public static final String PATH_WATCH_BATTERY_REQUEST = "/faceclaw/battery/request";
|
||||
public static final String PATH_ACK = "/faceclaw/ack";
|
||||
public static final String PATH_EVENT = "/faceclaw/event";
|
||||
|
||||
@@ -284,6 +288,10 @@ public final class FaceclawWearBridge {
|
||||
|
||||
boolean delivered = dispatchMessage(path, json, nodeId);
|
||||
if (delivered) return;
|
||||
// A battery report answers a poll the dashboard sent; with no
|
||||
// dashboard to receive it, it is simply dropped (the dashboard polls
|
||||
// again when it starts), and the watch does not expect an ack.
|
||||
if (PATH_WATCH_BATTERY.equals(path)) return;
|
||||
|
||||
// No JS listener yet: tell the watch so it can explain why nothing is
|
||||
// happening. Only state requests are kept for replay (see class doc);
|
||||
|
||||
+25
-41
@@ -1,11 +1,9 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
import android.util.Log;
|
||||
|
||||
public final class BleImageOptimizer {
|
||||
@@ -79,7 +77,7 @@ public final class BleImageOptimizer {
|
||||
public final byte[] packed;
|
||||
public final int width;
|
||||
public final int height;
|
||||
final byte[] payload; // bytes actually streamed: mode-6 zlib(rle(4bpp))
|
||||
final byte[] payload; // bytes actually streamed: mode-6 rle(4bpp)
|
||||
final int sessionId;
|
||||
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
||||
|
||||
@@ -113,7 +111,7 @@ public final class BleImageOptimizer {
|
||||
* Build a mode-3 bounding-box incremental payload, or null when a full
|
||||
* update should be sent instead (frames not comparable, or the changed
|
||||
* region spans the whole screen). Wire format:
|
||||
* [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][zlib(rle(headerless 4bpp region))]
|
||||
* [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][rle(headerless 4bpp region)]
|
||||
* The region is top-down rows of the NEW frame, stride width/2 bytes. The
|
||||
* box is aligned so left/width are multiples of 4 pixels and top/height
|
||||
* multiples of 2 rows, letting each coordinate fit one byte and avoiding
|
||||
@@ -260,7 +258,7 @@ public final class BleImageOptimizer {
|
||||
|
||||
/**
|
||||
* Encode one CFW mode-3 rectangle delta:
|
||||
* [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][zlib(rle(box pixels))]
|
||||
* [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][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
|
||||
@@ -273,7 +271,7 @@ public final class BleImageOptimizer {
|
||||
for (int y = 0; y < boxHeight; y++) {
|
||||
System.arraycopy(next, (top + y) * stride + (left >> 1), region, y * regionStride, regionStride);
|
||||
}
|
||||
byte[] compressed = deflate(rleEncode(region));
|
||||
byte[] compressed = rleEncode(region);
|
||||
byte[] out = new byte[7 + compressed.length];
|
||||
out[0] = 3;
|
||||
out[1] = (byte) (left / 4);
|
||||
@@ -286,6 +284,22 @@ public final class BleImageOptimizer {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** A full repaint in independently decodable commands below the uint16 record limit. */
|
||||
static List<byte[]> encodeFullFrameBands(byte[] packed, int width, int height, int firstFid) {
|
||||
if (width <= 0 || width > 640 || (width & 3) != 0 || height <= 0 || height > 480
|
||||
|| (height & 1) != 0 || packed.length != width * height / 2) {
|
||||
throw new IllegalArgumentException("unsupported custom framebuffer dimensions");
|
||||
}
|
||||
List<byte[]> commands = new ArrayList<>();
|
||||
int fid = firstFid;
|
||||
for (int top = 0; top < height; top += 64) {
|
||||
commands.add(encodeMode3Rect(packed, width / 2, 0, top, width,
|
||||
Math.min(64, height - top), fid));
|
||||
fid = fid >= 0xfffe ? 1 : fid + 1;
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* A CFW mode-8 multi-segment payload (`[8][count]` then per-rect
|
||||
* `[len16][mode-3 submsg]`) carrying several tight rectangle deltas instead of
|
||||
@@ -310,8 +324,7 @@ public final class BleImageOptimizer {
|
||||
}
|
||||
|
||||
// Break-even tuning for the rect splitter. Splitting adds ~15 fixed bytes per
|
||||
// rect (seglen + mode-3 header + fid + zlib framing) and loses cross-rect
|
||||
// dictionary sharing, so only split across gaps big enough to pay for that.
|
||||
// rect (seglen + mode-3 header + fid), so only split across gaps big enough to pay for that.
|
||||
// Gaps are in the changed-mask's native units: whole 4bpp bytes (2px) for
|
||||
// columns, rows for the vertical bands. Kept above the 4px/2px box alignment so
|
||||
// aligned rects never overlap.
|
||||
@@ -461,8 +474,8 @@ public final class BleImageOptimizer {
|
||||
}
|
||||
|
||||
/**
|
||||
* RLE- then zlib-compress headerless 4bpp pixels for CFW load_image_z mode 6.
|
||||
* Wire format: [6][zlib(rle(4bpp pixels))]. Always use mode 6: the logical
|
||||
* Run-length encode headerless 4bpp pixels for CFW load_image_z mode 6.
|
||||
* Wire format: [6][rle(4bpp pixels)]. Always use mode 6: the logical
|
||||
* image can be larger than its EvenHub carrier, so a raw BMP fallback would
|
||||
* carry dimensions the legacy container loader cannot accept.
|
||||
*/
|
||||
@@ -470,7 +483,7 @@ public final class BleImageOptimizer {
|
||||
if (packed == null || packed.length == 0) {
|
||||
return packed;
|
||||
}
|
||||
byte[] z = deflate(rleEncode(packed));
|
||||
byte[] z = rleEncode(packed);
|
||||
byte[] out = new byte[z.length + 1];
|
||||
out[0] = 6;
|
||||
System.arraycopy(z, 0, out, 1, z.length);
|
||||
@@ -479,7 +492,7 @@ public final class BleImageOptimizer {
|
||||
|
||||
/**
|
||||
* Run-length encode packed 4bpp pixels for CFW modes 3 and 6, whose payload is
|
||||
* zlib(rle(pixels)) rather than zlib(pixels). Runs are over the pixel NIBBLES of
|
||||
* RLE tokens; deflate belongs to the transport. Runs are over the pixel NIBBLES of
|
||||
* {@code pix} in wire order (high nibble = left pixel), including the pad nibble
|
||||
* that ends each row at odd widths — i.e. {@code pix} read as 2*length nibbles.
|
||||
* One token is:
|
||||
@@ -527,35 +540,6 @@ public final class BleImageOptimizer {
|
||||
return (i & 1) != 0 ? (b & 0x0f) : (b >> 4);
|
||||
}
|
||||
|
||||
// Level 6: BEST_COMPRESSION cost 18-109ms per frame on the BLE worker,
|
||||
// but BEST_SPEED inflated typical payloads from ~2.7KB past the 3800-byte
|
||||
// fragment boundary, adding a whole extra ack round trip (~350ms). The
|
||||
// default level keeps payloads under one fragment at about half the CPU.
|
||||
// One long-lived Deflater per thread: constructing one allocates and
|
||||
// initializes a native zlib stream, which is measurable at per-frame rates.
|
||||
private static final ThreadLocal<Deflater> DEFLATER =
|
||||
new ThreadLocal<Deflater>() {
|
||||
@Override protected Deflater initialValue() {
|
||||
return new Deflater(Deflater.DEFAULT_COMPRESSION);
|
||||
}
|
||||
};
|
||||
|
||||
private static byte[] deflate(byte[] data) {
|
||||
Deflater deflater = DEFLATER.get();
|
||||
try {
|
||||
deflater.setInput(data);
|
||||
deflater.finish();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 3));
|
||||
byte[] buf = new byte[4096];
|
||||
while (!deflater.finished()) {
|
||||
out.write(buf, 0, deflater.deflate(buf));
|
||||
}
|
||||
return out.toByteArray();
|
||||
} finally {
|
||||
deflater.reset();
|
||||
}
|
||||
}
|
||||
|
||||
public static final class ImageUpdateStats {
|
||||
final int paintMs;
|
||||
final int tileCount;
|
||||
|
||||
@@ -50,16 +50,15 @@ public class BleProtocol {
|
||||
public static final int FACECLAW_WAKE_CONTROL_FIELD = 101;
|
||||
public static final int FACECLAW_WAKE_EVENT_FIELD = 102;
|
||||
/**
|
||||
* CFW mic_control (EVENCFW/16, caps tokens micctl/micmc/micraw): host
|
||||
* writes an ['M','C',ver,op,...] record as settings field 103; each temple
|
||||
* CFW mic_control: host writes an ['M','C',ver,op,...] record as settings
|
||||
* field 103; each temple
|
||||
* reports a 21-byte ['M','C',ver,...] status as field 104, both appended
|
||||
* to settings read responses and as standalone commandId=3 pushes.
|
||||
*/
|
||||
public static final int FACECLAW_MIC_CONTROL_FIELD = 103;
|
||||
public static final int FACECLAW_MIC_STATUS_FIELD = 104;
|
||||
/**
|
||||
* CFW als_sensor (EVENCFW/18, caps token als16): ambient-light reports on
|
||||
* settings-channel field 105, 24-byte ['A','L',ver,reason,...] records from
|
||||
* CFW als_sensor: ambient-light reports on settings-channel field 105, 24-byte ['A','L',ver,reason,...] records from
|
||||
* the master temple (decoded in app/native/ambient-light.ts).
|
||||
*/
|
||||
public static final int FACECLAW_ALS_REPORT_FIELD = 105;
|
||||
@@ -72,6 +71,19 @@ public class BleProtocol {
|
||||
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;
|
||||
// Field-102 deferred wake from the IMU head-up (Faceclaw firmware revision
|
||||
// 3+); same nonce handshake as the double-tap wake event 1.
|
||||
public static final int FACECLAW_WAKE_EVENT_HEAD_UP = 5;
|
||||
// Field-102 idle-gesture events (Faceclaw firmware revision 2+): gestures the
|
||||
// stock display thread drops while no app is on screen, forwarded while the
|
||||
// wake lease is held. Their last two bytes are the raw touch source and 0.
|
||||
public static final int FACECLAW_GESTURE_EVENT_TAP = 2;
|
||||
public static final int FACECLAW_GESTURE_EVENT_LONG_PRESS = 3;
|
||||
public static final int FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE = 4;
|
||||
// The display thread's raw touch sources, as the firmware reports them.
|
||||
private static final int FACECLAW_RAW_SOURCE_LEFT_TEMPLE = 0;
|
||||
private static final int FACECLAW_RAW_SOURCE_RIGHT_TEMPLE = 1;
|
||||
private static final int FACECLAW_RAW_SOURCE_RING = 4;
|
||||
private static final int FACECLAW_WAKE_PROTOCOL_VERSION = 1;
|
||||
// UI_FOREGROUND_EVEN_AI_ID: the stock "Even AI" assistant app. It is a
|
||||
// FOREGROUND app, whereas EvenHub (and therefore faceclaw) is the
|
||||
@@ -112,6 +124,9 @@ public class BleProtocol {
|
||||
public static final int EVENT_RING_LONG_PRESS_RELEASE = 10;
|
||||
// CFW extension: G2 2.2.9 tap-then-hold gesture forwarded to the phone.
|
||||
public static final int EVENT_SHORT_THEN_LONG_PRESS = 11;
|
||||
// CFW extension (firmware revision 3+): the IMU head-up, forwarded as a
|
||||
// sys-event while an EvenHub page is on screen (soft sleep).
|
||||
public static final int EVENT_HEAD_UP = 12;
|
||||
|
||||
public static final int EVENT_SOURCE_GLASSES_R = 1;
|
||||
public static final int EVENT_SOURCE_RING = 2;
|
||||
@@ -170,13 +185,11 @@ public class BleProtocol {
|
||||
}
|
||||
|
||||
|
||||
public static byte[] buildCreateMixedImagePage(int magic, ImageTileOptions[] tiles) {
|
||||
/** Retain the text container for forwarded inputs, without an image container. */
|
||||
public static byte[] buildCreateInputPage(int magic) {
|
||||
List<byte[]> innerParts = new ArrayList<>();
|
||||
innerParts.add(encodeVarintField(1, 1 + tiles.length));
|
||||
innerParts.add(encodeVarintField(1, 1));
|
||||
innerParts.add(encodeMessageField(3, encodeTextObject("dashboard", 1, 0, 0, 576, 288, " ", true)));
|
||||
for (ImageTileOptions tile : tiles) {
|
||||
innerParts.add(encodeMessageField(4, encodeImageObject(tile)));
|
||||
}
|
||||
innerParts.add(encodeVarintField(5, 10000));
|
||||
byte[] inner = concat(innerParts);
|
||||
return wrapEvenHub(0, magic, 3, inner);
|
||||
@@ -475,24 +488,127 @@ public class BleProtocol {
|
||||
return body;
|
||||
}
|
||||
|
||||
/** Field 106: RB/version 1/flags/percentage, from the CFW ring-battery cache. */
|
||||
public static RingBatterySnapshot parseRingBattery(byte[] pb) {
|
||||
if (pb == null) return null;
|
||||
byte[] body = readFieldBytes(stripTrailingCrc(pb), 106);
|
||||
if (body == null || body.length != 5 || body[0] != 'R' || body[1] != 'B'
|
||||
|| body[2] != 1) return null;
|
||||
int flags = body[3] & 0xff;
|
||||
int level = body[4] & 0xff;
|
||||
if ((flags & ~7) != 0) return null;
|
||||
if ((flags & 2) != 0) {
|
||||
if ((flags & 1) == 0 || level > 100) return null;
|
||||
return new RingBatterySnapshot(level, (flags & 4) != 0 ? 1 : 0);
|
||||
}
|
||||
if (level != 255 || (flags & 4) != 0) return null;
|
||||
return new RingBatterySnapshot(-1, -1);
|
||||
}
|
||||
|
||||
public static final class RingBatterySnapshot {
|
||||
final int battery;
|
||||
final int charging;
|
||||
RingBatterySnapshot(int battery, int charging) {
|
||||
this.battery = battery;
|
||||
this.charging = charging;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the uint16 wake nonce from a CFW field-102 notification, or -1
|
||||
* when this is an ordinary settings frame.
|
||||
* Return the uint16 wake nonce from a CFW field-102 deferred-wake
|
||||
* notification (double tap or head-up), or -1 when this is an ordinary
|
||||
* settings frame (idle-gesture events included).
|
||||
*/
|
||||
public static int parseFaceclawWakeEvent(byte[] pb) {
|
||||
byte[] event = faceclawWakeEventBytes(pb);
|
||||
return event == null ? -1 : (event[4] & 0xff) | ((event[5] & 0xff) << 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Which gesture a CFW deferred-wake notification reports: 1 (double tap)
|
||||
* or FACECLAW_WAKE_EVENT_HEAD_UP; -1 when the frame is not a wake event.
|
||||
*/
|
||||
public static int parseFaceclawWakeEventCode(byte[] pb) {
|
||||
byte[] event = faceclawWakeEventBytes(pb);
|
||||
return event == null ? -1 : (event[3] & 0xff);
|
||||
}
|
||||
|
||||
private static byte[] faceclawWakeEventBytes(byte[] pb) {
|
||||
if (pb == null) {
|
||||
return -1;
|
||||
return null;
|
||||
}
|
||||
byte[] event = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD);
|
||||
if (event == null
|
||||
|| event.length != 6
|
||||
|| event[0] != 'F'
|
||||
|| event[1] != 'C'
|
||||
|| (event[2] & 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION
|
||||
|| (event[3] & 0xff) != FACECLAW_WAKE_EVENT) {
|
||||
return -1;
|
||||
|| (event[2] & 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION) {
|
||||
return null;
|
||||
}
|
||||
return (event[4] & 0xff) | ((event[5] & 0xff) << 8);
|
||||
int code = event[3] & 0xff;
|
||||
return code == FACECLAW_WAKE_EVENT || code == FACECLAW_WAKE_EVENT_HEAD_UP ? event : null;
|
||||
}
|
||||
|
||||
/** An idle gesture from a CFW field-102 notification, in EvenHub sys-event terms. */
|
||||
public static final class FaceclawGestureEvent {
|
||||
public final int eventType;
|
||||
public final int eventSource;
|
||||
|
||||
FaceclawGestureEvent(int eventType, int eventSource) {
|
||||
this.eventType = eventType;
|
||||
this.eventSource = eventSource;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a CFW field-102 idle-gesture event (tap, long press, release
|
||||
* while no app is on screen) into the EvenHub sys-event type and source
|
||||
* the same gesture would carry from a live page, or null when the frame
|
||||
* is not one (ordinary settings frames and the double-tap wake event
|
||||
* included).
|
||||
*/
|
||||
public static FaceclawGestureEvent parseFaceclawGestureEvent(byte[] pb) {
|
||||
if (pb == null) {
|
||||
return null;
|
||||
}
|
||||
byte[] event = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD);
|
||||
if (event == null
|
||||
|| event.length != 6
|
||||
|| event[0] != 'F'
|
||||
|| event[1] != 'C'
|
||||
|| (event[2] & 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION) {
|
||||
return null;
|
||||
}
|
||||
int eventType;
|
||||
switch (event[3] & 0xff) {
|
||||
case FACECLAW_GESTURE_EVENT_TAP:
|
||||
eventType = EVENT_CLICK;
|
||||
break;
|
||||
case FACECLAW_GESTURE_EVENT_LONG_PRESS:
|
||||
eventType = EVENT_RING_LONG_PRESS;
|
||||
break;
|
||||
case FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE:
|
||||
eventType = EVENT_RING_LONG_PRESS_RELEASE;
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
int eventSource;
|
||||
switch (event[4] & 0xff) {
|
||||
case FACECLAW_RAW_SOURCE_LEFT_TEMPLE:
|
||||
eventSource = EVENT_SOURCE_GLASSES_L;
|
||||
break;
|
||||
case FACECLAW_RAW_SOURCE_RIGHT_TEMPLE:
|
||||
eventSource = EVENT_SOURCE_GLASSES_R;
|
||||
break;
|
||||
case FACECLAW_RAW_SOURCE_RING:
|
||||
eventSource = EVENT_SOURCE_RING;
|
||||
break;
|
||||
default:
|
||||
eventSource = 0;
|
||||
break;
|
||||
}
|
||||
return new FaceclawGestureEvent(eventType, eventSource);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -559,11 +675,14 @@ public class BleProtocol {
|
||||
}
|
||||
|
||||
/**
|
||||
* Firmware versions and the CFW capability advertisement from a settings
|
||||
* Firmware versions and the firmware-extension string from a settings
|
||||
* READ ack. Versions are fields 5/6 of the deviceReceiveRequestFromApp
|
||||
* submessage (field 4); the custom firmware additionally appends top-level
|
||||
* field 100, a string like "EVENCFW/6 img576 img640 ... directfb fbguard", which
|
||||
* stock firmware never sends. Returns null when the ack carries none of it.
|
||||
* submessage (field 4); Faceclaw's custom firmware additionally appends
|
||||
* top-level field 100 with its revision ("Faceclaw/<n>"; older builds sent
|
||||
* "EVENCFW/<ver> <tokens>"), which stock firmware never sends. Returns null
|
||||
* when the ack carries none of it. Compatibility is judged on the TS side
|
||||
* (app/g2/firmware-compat.ts); Java only needs to know whether the firmware
|
||||
* is ours at all.
|
||||
*/
|
||||
public static FirmwareInfo parseSettingsFirmwareInfo(byte[] pb) {
|
||||
byte[] root = stripTrailingCrc(pb);
|
||||
@@ -573,11 +692,11 @@ public class BleProtocol {
|
||||
}
|
||||
String leftVersion = readStringFieldValue(request, 5);
|
||||
String rightVersion = readStringFieldValue(request, 6);
|
||||
String capabilities = readStringFieldValue(root, 100);
|
||||
if (leftVersion.isEmpty() && rightVersion.isEmpty() && capabilities.isEmpty()) {
|
||||
String extension = readStringFieldValue(root, 100);
|
||||
if (leftVersion.isEmpty() && rightVersion.isEmpty() && extension.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return new FirmwareInfo(leftVersion, rightVersion, capabilities);
|
||||
return new FirmwareInfo(leftVersion, rightVersion, extension);
|
||||
}
|
||||
|
||||
public static byte[] wrapEvenHub(int cmd, int magic, int innerFieldNumber, byte[] inner) {
|
||||
@@ -1094,14 +1213,28 @@ public class BleProtocol {
|
||||
}
|
||||
|
||||
public static final class FirmwareInfo {
|
||||
/** Prefix of the firmware-extension string on Faceclaw's custom firmware. */
|
||||
public static final String FACECLAW_EXTENSION_PREFIX = "Faceclaw/";
|
||||
|
||||
final String leftVersion;
|
||||
final String rightVersion;
|
||||
final String capabilities;
|
||||
/** Raw field-100 string ("" on stock firmware). */
|
||||
final String extension;
|
||||
|
||||
FirmwareInfo(String leftVersion, String rightVersion, String capabilities) {
|
||||
FirmwareInfo(String leftVersion, String rightVersion, String extension) {
|
||||
this.leftVersion = leftVersion;
|
||||
this.rightVersion = rightVersion;
|
||||
this.capabilities = capabilities;
|
||||
this.extension = extension == null ? "" : extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the glasses run Faceclaw's custom firmware (any revision).
|
||||
* Whether the revision is the one this app needs is decided on the TS
|
||||
* side, which disconnects on a mismatch; this only guards the private
|
||||
* modes against stock or third-party firmware in the meantime.
|
||||
*/
|
||||
public boolean isFaceclawFirmware() {
|
||||
return extension.trim().startsWith(FACECLAW_EXTENSION_PREFIX);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Ordered completion and go-back-N recovery for the custom-message window. */
|
||||
final class CfwMessageWindow {
|
||||
// Flappy capture: p99 ACK 63ms, max 77ms. Leave room for full-frame
|
||||
// processing while recovering a lost reply well before the stock timeout.
|
||||
static final int ACK_TIMEOUT_MS = 500;
|
||||
static final int MAX_RETRIES = 3;
|
||||
|
||||
private CfwMessageWindow() {}
|
||||
|
||||
static OutboundMessage acknowledgedHead(Iterable<OutboundMessage> messages) {
|
||||
for (OutboundMessage message : messages) {
|
||||
if (message.sid != CfwTransport.SID) continue;
|
||||
return !message.cfwRetryPending && message.cfwAckLenses == CfwTransport.BOTH ? message : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static List<OutboundMessage> replayWindow(Iterable<OutboundMessage> messages, long nowMs) {
|
||||
List<OutboundMessage> replay = new ArrayList<>();
|
||||
boolean failed = false;
|
||||
for (OutboundMessage message : messages) {
|
||||
if (message.sid != CfwTransport.SID) continue;
|
||||
failed |= message.cfwRetryPending || (message.cfwAckLenses != CfwTransport.BOTH
|
||||
&& message.ackDeadlineAtMs > 0 && message.ackDeadlineAtMs <= nowMs);
|
||||
replay.add(message);
|
||||
}
|
||||
// A lost ACK, first fragment, or NACK can leave an older attempt
|
||||
// unresolved. Replaying only the named failure strands that older
|
||||
// attempt at the head until its timeout. Rewind the unresolved window.
|
||||
if (!failed) replay.clear();
|
||||
return replay;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Faceclaw/10 records: clear flags, wire length and decoded CRC; persistent zlib body. */
|
||||
public final class CfwTransport {
|
||||
public static final int SID = 0xf0;
|
||||
public static final int BOTH = 3;
|
||||
public static final int MAX_MESSAGE = 65535;
|
||||
public static final int COMPRESSED = 4, RESET_CONTEXT = 8;
|
||||
private final Deflater deflater = new Deflater();
|
||||
private boolean resetPending = true;
|
||||
private int previousLenses;
|
||||
|
||||
public CfwTransport() {}
|
||||
|
||||
public synchronized void reset() {
|
||||
deflater.reset();
|
||||
resetPending = true;
|
||||
previousLenses = 0;
|
||||
}
|
||||
|
||||
/** Compress only when writing, in exactly the order seen by the receiver. */
|
||||
public synchronized List<byte[]> encode(byte[] message, int streamId, int lenses, int mtu) {
|
||||
validate(message, streamId, lenses, mtu);
|
||||
if (previousLenses != lenses) reset();
|
||||
boolean reset = resetPending;
|
||||
deflater.setInput(message);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] chunk = new byte[4096];
|
||||
int count;
|
||||
do {
|
||||
count = deflater.deflate(chunk, 0, chunk.length, Deflater.SYNC_FLUSH);
|
||||
out.write(chunk, 0, count);
|
||||
} while (count == chunk.length);
|
||||
byte[] body = out.toByteArray();
|
||||
int flags = lenses | COMPRESSED | (reset ? RESET_CONTEXT : 0);
|
||||
if (body.length == 0) flags &= ~COMPRESSED; // repeated empty sync flush
|
||||
if (body.length > MAX_MESSAGE) {
|
||||
// Incompressible maximum-size records still fit as raw records.
|
||||
// The attempted deflate advanced history: reset both ends before reuse.
|
||||
reset();
|
||||
body = message;
|
||||
flags = lenses | RESET_CONTEXT;
|
||||
} else {
|
||||
resetPending = false;
|
||||
previousLenses = lenses;
|
||||
}
|
||||
return frameRecord(body, crc(message), streamId, flags, mtu);
|
||||
}
|
||||
|
||||
private static void validate(byte[] message, int streamId, int lenses, int mtu) {
|
||||
if (message == null || message.length > MAX_MESSAGE || streamId < 0 || streamId > 255
|
||||
|| lenses < 1 || lenses > BOTH || mtu < 23 || mtu > 517)
|
||||
throw new IllegalArgumentException("invalid CFW stream parameters");
|
||||
}
|
||||
|
||||
/** One logical command per stream for now. Packets are bounded by actual ATT MTU. */
|
||||
public static List<byte[]> frame(byte[] message, int streamId, int lenses, int mtu) {
|
||||
validate(message, streamId, lenses, mtu);
|
||||
return frameRecord(message, crc(message), streamId, lenses | RESET_CONTEXT, mtu);
|
||||
}
|
||||
|
||||
private static List<byte[]> frameRecord(byte[] body, int checksum, int streamId, int flags, int mtu) {
|
||||
int lenses = flags & BOTH;
|
||||
int capacity = Math.min(252, mtu - 14);
|
||||
byte[] stream = new byte[body.length + 5];
|
||||
stream[0] = (byte) flags;
|
||||
stream[1] = (byte) body.length;
|
||||
stream[2] = (byte) (body.length >>> 8);
|
||||
stream[3] = (byte) checksum;
|
||||
stream[4] = (byte) (checksum >>> 8);
|
||||
System.arraycopy(body, 0, stream, 5, body.length);
|
||||
List<byte[]> packets = new ArrayList<>();
|
||||
for (int offset = 0, index = 0; offset < stream.length; offset += capacity, index++) {
|
||||
int count = Math.min(capacity, stream.length - offset);
|
||||
byte[] packet = new byte[count + 11];
|
||||
packet[0] = (byte) 0xaa;
|
||||
packet[1] = 0x21;
|
||||
packet[2] = (byte) (streamId + index);
|
||||
packet[3] = (byte) (count + 3);
|
||||
packet[4] = packet[5] = 1;
|
||||
packet[6] = (byte) SID;
|
||||
packet[8] = (byte) (lenses | (offset == 0 ? 0x80 : 0)
|
||||
| (offset + count == stream.length ? 0x40 : 0));
|
||||
System.arraycopy(stream, offset, packet, 9, count);
|
||||
int crc = crc(packet, 8, count + 1);
|
||||
packet[packet.length - 2] = (byte) crc;
|
||||
packet[packet.length - 1] = (byte) (crc >>> 8);
|
||||
packets.add(packet);
|
||||
}
|
||||
return packets;
|
||||
}
|
||||
|
||||
public static int crc(byte[] data) { return crc(data, 0, data.length); }
|
||||
private static int crc(byte[] data, int offset, int count) {
|
||||
int crc = 0xffff;
|
||||
for (int i = offset; i < offset + count; i++) {
|
||||
crc ^= (data[i] & 255) << 8;
|
||||
for (int bit = 0; bit < 8; bit++)
|
||||
crc = ((crc << 1) ^ ((crc & 0x8000) != 0 ? 0x1021 : 0)) & 65535;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
private static int u16(byte[] data, int offset) {
|
||||
return (data[offset] & 255) | ((data[offset + 1] & 255) << 8);
|
||||
}
|
||||
public static Ack parseAck(byte[] packet) {
|
||||
Ack[] acks = parseAcks(packet);
|
||||
return acks == null ? null : acks[0];
|
||||
}
|
||||
/** Primary ACK followed by up to three explicit preceding successes from
|
||||
* the same processing lens. Validate the entire packet before applying any. */
|
||||
public static Ack[] parseAcks(byte[] packet) {
|
||||
if (packet == null || packet.length < 19 || packet.length > 40
|
||||
|| (packet.length - 19) % 7 != 0 || (packet[0] & 255) != 0xaa
|
||||
|| packet[1] != 0x12 || (packet[3] & 255) != packet.length - 8 || packet[4] != 1 || packet[5] != 1
|
||||
|| (packet[6] & 255) != SID || packet[7] != 0 || (packet[8] != 1 && packet[8] != 3)
|
||||
|| (packet[12] != 1 && packet[12] != 2)
|
||||
|| (packet[8] == 3 && packet.length != 19)
|
||||
|| crc(packet, 8, packet.length - 10) != u16(packet, packet.length - 2)) return null;
|
||||
Ack[] acks = new Ack[1 + (packet.length - 19) / 7];
|
||||
acks[0] = new Ack(packet[8] == 3, packet[9] & 255, u16(packet, 10), packet[12], u16(packet, 13), u16(packet, 15));
|
||||
for (int i = 1, offset = 17; i < acks.length; i++, offset += 7)
|
||||
acks[i] = new Ack(false, packet[offset] & 255, u16(packet, offset + 1),
|
||||
packet[12], u16(packet, offset + 3), u16(packet, offset + 5));
|
||||
return acks;
|
||||
}
|
||||
public static final class Ack {
|
||||
public final boolean nack;
|
||||
public final int streamId, messageId, lens, size, checksum;
|
||||
Ack(boolean nack, int streamId, int messageId, int lens, int size, int checksum) {
|
||||
this.nack = nack;
|
||||
this.streamId = streamId; this.messageId = messageId; this.lens = lens;
|
||||
this.size = size; this.checksum = checksum;
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-29
@@ -72,35 +72,18 @@ public class MessageBuilder {
|
||||
String label,
|
||||
boolean leftArm
|
||||
) {
|
||||
BleProtocol.ImageFragment fragment =
|
||||
new BleProtocol.ImageFragment(0, payload, payload.length);
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
kind,
|
||||
label,
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildImageRawData(tile, sessionId, payload.length, fragment, magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
// Keep these arguments for source compatibility with existing callers;
|
||||
// the private command no longer carries a tile, session, or image header.
|
||||
return customMessage(kind, payload, label, -1, leftArm);
|
||||
}
|
||||
|
||||
public OutboundMessage imageFragment(BleProtocol.ImageFragment fragment, BleImageOptimizer.TileImagePlan plan, boolean requestAck, boolean leftArm) {
|
||||
int magic = requestAck ? magicPool.allocate() : 0;
|
||||
return new OutboundMessage(
|
||||
"image",
|
||||
"image " + plan.tile.name + "#" + fragment.index,
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildImageRawData(plan.tile, plan.sessionId, plan.payload.length, fragment, magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
plan.tileIndex,
|
||||
leftArm
|
||||
);
|
||||
public OutboundMessage customMessage(String kind, byte[] payload, String label,
|
||||
int tileIndex, boolean leftArm) {
|
||||
if (payload == null || payload.length > CfwTransport.MAX_MESSAGE)
|
||||
throw new IllegalArgumentException("CFW message exceeds uint16 length");
|
||||
int streamId = magicPool.allocate();
|
||||
return new OutboundMessage(kind, label, CfwTransport.SID, 0, streamId, payload,
|
||||
CfwMessageWindow.ACK_TIMEOUT_MS, tileIndex, leftArm);
|
||||
}
|
||||
|
||||
public OutboundMessage cfwCleanup(
|
||||
@@ -179,7 +162,7 @@ public class MessageBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage createLayout(BleProtocol.ImageTileOptions... tiles) {
|
||||
public OutboundMessage createLayout() {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"create-layout",
|
||||
@@ -187,7 +170,7 @@ public class MessageBuilder {
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildCreateMixedImagePage(magic, tiles),
|
||||
BleProtocol.buildCreateInputPage(magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
|
||||
+29
-1
@@ -6,12 +6,16 @@ public final class OutboundMessage {
|
||||
final String label;
|
||||
final int sid;
|
||||
final int flag;
|
||||
final int magic;
|
||||
int magic;
|
||||
final byte[] message;
|
||||
final int ackTimeoutMs;
|
||||
final int tileIndex;
|
||||
final boolean isLeftArmMessage;
|
||||
|
||||
int cfwAckLenses, cfwRetries;
|
||||
boolean cfwRetryPending;
|
||||
final int cfwChecksum;
|
||||
|
||||
int imageUpdateId;
|
||||
int imageMessageNumber;
|
||||
int imageMessageCount;
|
||||
@@ -30,11 +34,35 @@ public final class OutboundMessage {
|
||||
this.flag = flag;
|
||||
this.magic = magic;
|
||||
this.message = message == null ? new byte[0] : Arrays.copyOf(message, message.length);
|
||||
this.cfwChecksum = sid == CfwTransport.SID ? CfwTransport.crc(this.message) : 0;
|
||||
this.ackTimeoutMs = ackTimeoutMs;
|
||||
this.tileIndex = tileIndex;
|
||||
this.isLeftArmMessage = isLeftArmMessage;
|
||||
}
|
||||
|
||||
/** NACK names this attempt's stream/message sequence, even without a decoded size. */
|
||||
boolean acceptCfwAck(CfwTransport.Ack ack) {
|
||||
if (ack == null || sid != CfwTransport.SID || magic != ack.streamId
|
||||
|| ack.messageId != 0 || (ack.lens != 1 && ack.lens != 2)) return false;
|
||||
if (ack.nack) {
|
||||
cfwRetryPending = true;
|
||||
return false;
|
||||
}
|
||||
if (message.length != ack.size || cfwChecksum != ack.checksum) return false;
|
||||
cfwAckLenses |= ack.lens;
|
||||
return !cfwRetryPending && cfwAckLenses == CfwTransport.BOTH;
|
||||
}
|
||||
|
||||
/** Replace an attempt atomically under the communicator lock. */
|
||||
void prepareCfwReplay(int streamId) {
|
||||
magic = streamId;
|
||||
++cfwRetries;
|
||||
cfwAckLenses = 0;
|
||||
cfwRetryPending = false;
|
||||
ackDeadlineAtMs = 0;
|
||||
ackPayload = new byte[0];
|
||||
}
|
||||
|
||||
void setImageUpdatePosition(int updateId, int messageNumber, int messageCount) {
|
||||
this.imageUpdateId = updateId;
|
||||
this.imageMessageNumber = messageNumber;
|
||||
|
||||
+23
-20
@@ -7,31 +7,31 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Phone-side model of the CFW's flat 64 KiB texture cache (zlib_glue mode 12).
|
||||
* Phone-side model of the CFW's flat 256 KiB texture cache (zlib_glue mode 18).
|
||||
* 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)
|
||||
* made here. Layout per font: a 96-entry uint32 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
|
||||
* below 256 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
|
||||
* current generation — stale table entries are never named in a mode-20
|
||||
* string.
|
||||
*
|
||||
* Upload entries accumulate as complete [offset u16][len u16][data] records
|
||||
* and drain into one or more standalone mode-12 payloads, each independently
|
||||
* Upload entries accumulate as complete [offset u32][len u16][data] records
|
||||
* and drain into one or more standalone mode-18 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;
|
||||
public static final int CACHE_SIZE = 262144;
|
||||
private static final int FONT_TABLE_BYTES = 96 * 4;
|
||||
|
||||
private int allocEnd = 0;
|
||||
private final Map<Integer, Integer> fontTableOffsets = new HashMap<>();
|
||||
@@ -64,7 +64,7 @@ public final class TextureCacheState {
|
||||
/**
|
||||
* 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).
|
||||
* the signal that the 256 KiB budget is being outgrown).
|
||||
*/
|
||||
public int usedBytes() {
|
||||
return allocEnd;
|
||||
@@ -78,9 +78,9 @@ public final class TextureCacheState {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* mode-18 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).
|
||||
* or the encoding is outside the mode-20 table (32..127).
|
||||
*/
|
||||
public int ensureGlyph(int fontId, int encoding, GlyphAtlas.Glyph glyph) {
|
||||
if (glyph == null || encoding < 32 || encoding > 127) return -1;
|
||||
@@ -104,13 +104,14 @@ public final class TextureCacheState {
|
||||
glyphOffsets.put(key, offset);
|
||||
|
||||
addEntry(offset, glyph.cachedBytes);
|
||||
addEntry(table + 2 * (encoding - 32),
|
||||
new byte[]{(byte) (offset & 0xff), (byte) ((offset >> 8) & 0xff)});
|
||||
addEntry(table + 4 * (encoding - 32),
|
||||
new byte[]{(byte) offset, (byte) (offset >> 8),
|
||||
(byte) (offset >> 16), (byte) (offset >> 24)});
|
||||
return offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure an ImageAtlas entry is resident (mode-13 draws reference it by
|
||||
* Ensure an ImageAtlas entry is resident (mode-19 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).
|
||||
*/
|
||||
@@ -127,12 +128,14 @@ public final class TextureCacheState {
|
||||
}
|
||||
|
||||
private void addEntry(int offset, byte[] data) {
|
||||
byte[] entry = new byte[4 + data.length];
|
||||
byte[] entry = new byte[6 + 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);
|
||||
entry[2] = (byte) ((offset >> 16) & 0xff);
|
||||
entry[3] = (byte) ((offset >> 24) & 0xff);
|
||||
entry[4] = (byte) (data.length & 0xff);
|
||||
entry[5] = (byte) ((data.length >> 8) & 0xff);
|
||||
System.arraycopy(data, 0, entry, 6, data.length);
|
||||
pendingEntries.add(entry);
|
||||
pendingBytes += entry.length;
|
||||
}
|
||||
@@ -147,7 +150,7 @@ public final class TextureCacheState {
|
||||
}
|
||||
|
||||
/**
|
||||
* Drain the queued entries into complete mode-12 payloads ([12][entries]),
|
||||
* Drain the queued entries into complete mode-18 payloads ([18][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).
|
||||
@@ -162,7 +165,7 @@ public final class TextureCacheState {
|
||||
}
|
||||
if (current == null) {
|
||||
current = new ByteArrayOutputStream(Math.min(maxPayloadBytes, 4 + entry.length));
|
||||
current.write(12);
|
||||
current.write(18);
|
||||
}
|
||||
current.write(entry, 0, entry.length);
|
||||
}
|
||||
|
||||
+41
-41
@@ -7,7 +7,7 @@ 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
|
||||
* instead of pixels (CFW modes 18/19/20; see g2flash/patches/zlib_glue.c and
|
||||
* texture_cache.c).
|
||||
*
|
||||
* Approach: dirty rects come from comparing the fully composited old and new
|
||||
@@ -19,8 +19,8 @@ import java.util.List;
|
||||
* 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
|
||||
* makes text and icons nearly free to compress — and re-emitted as a mode-20
|
||||
* string or mode-19 image sub-message in the same atomic mode-8 batch, so
|
||||
* the shadow after apply equals the full composite exactly. Everything else
|
||||
* stays baked.
|
||||
*
|
||||
@@ -30,7 +30,7 @@ import java.util.List;
|
||||
public final class TexturePlanner {
|
||||
private TexturePlanner() {}
|
||||
|
||||
/** Cap on mode-14 string sub-messages (mode-8 count is a u8). */
|
||||
/** Cap on mode-20 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.
|
||||
@@ -38,21 +38,21 @@ public final class TexturePlanner {
|
||||
* 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. */
|
||||
/** Cap on mode-19 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. */
|
||||
/** Mode-18 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. */
|
||||
/** Options: identity LUT (top 15) + transparent, for mode-19 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. */
|
||||
/** Mode-18 upload payloads to enqueue BEFORE the image message. */
|
||||
public final List<byte[]> uploads;
|
||||
/** Next mode-3 frame id (deltas consumed some). */
|
||||
public final int nextFid;
|
||||
@@ -118,15 +118,14 @@ public final class TexturePlanner {
|
||||
/**
|
||||
* 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).
|
||||
* 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) {
|
||||
boolean allowMultiRect, int maxRects) {
|
||||
if (next == null || draws == null || draws.length == 0 || width <= 0 || height <= 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -171,14 +170,12 @@ public final class TexturePlanner {
|
||||
List<FwPunch> fwPunches = new ArrayList<>();
|
||||
int fwGlyphCount = 0;
|
||||
int fwBaked = 0;
|
||||
if (allowFwText) {
|
||||
for (SurfaceCompositor.ScreenDraw draw : draws) {
|
||||
if (draw.kind != SurfaceCompositor.ScreenDraw.KIND_FWTEXT) continue;
|
||||
fwBaked += planFwRun(draw, rects, next, stride, width, height, fwSubs, fwPunches);
|
||||
}
|
||||
fwGlyphCount = fwPunches.size();
|
||||
bakedCandidates += fwBaked;
|
||||
for (SurfaceCompositor.ScreenDraw draw : draws) {
|
||||
if (draw.kind != SurfaceCompositor.ScreenDraw.KIND_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);
|
||||
@@ -204,7 +201,6 @@ public final class TexturePlanner {
|
||||
}
|
||||
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)) {
|
||||
@@ -239,7 +235,7 @@ public final class TexturePlanner {
|
||||
: ensureResident(cache, selected);
|
||||
bakedCandidates += selected.size() - drawable.size();
|
||||
|
||||
// Group glyphs into mode-14 runs; leftover-budget glyphs bake.
|
||||
// Group glyphs into mode-20 runs; leftover-budget glyphs bake.
|
||||
List<Selected> glyphDrawable = new ArrayList<>();
|
||||
List<Selected> imageDrawable = new ArrayList<>();
|
||||
for (Selected sel : drawable) {
|
||||
@@ -509,7 +505,7 @@ public final class TexturePlanner {
|
||||
|
||||
/**
|
||||
* Whether every in-panel nonzero pixel of the cached image equals the
|
||||
* packed new frame — the mode-13 transparent draw writes exactly those.
|
||||
* packed new frame — the mode-19 transparent draw writes exactly those.
|
||||
*/
|
||||
private static boolean imageMatchesComposite(byte[] packed, int stride, int width, int height,
|
||||
ImageAtlas.Entry atlas, int left, int top) {
|
||||
@@ -596,24 +592,26 @@ public final class TexturePlanner {
|
||||
return Collections.emptyList(); // unreachable
|
||||
}
|
||||
|
||||
/** Mode-13 cached-image draw: [13][offset u16][x u16][y u16][options u8]. */
|
||||
/** Mode-19 cached-image draw: [19][offset u32][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;
|
||||
byte[] sub = new byte[10];
|
||||
sub[0] = 19;
|
||||
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;
|
||||
sub[3] = (byte) ((offset >> 16) & 0xff);
|
||||
sub[4] = (byte) ((offset >> 24) & 0xff);
|
||||
sub[5] = (byte) (sel.draw.x & 0xff);
|
||||
sub[6] = (byte) ((sel.draw.x >> 8) & 0xff);
|
||||
sub[7] = (byte) (sel.draw.y & 0xff);
|
||||
sub[8] = (byte) ((sel.draw.y >> 8) & 0xff);
|
||||
sub[9] = (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]
|
||||
* Group selected glyphs into mode-20 string sub-messages:
|
||||
* [20][fontTable u32][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
|
||||
@@ -664,17 +662,19 @@ public final class TexturePlanner {
|
||||
}
|
||||
if (!runGlyphs.isEmpty()) {
|
||||
byte[] stringBytes = string.toByteArray();
|
||||
byte[] run = new byte[9 + stringBytes.length];
|
||||
run[0] = 14;
|
||||
byte[] run = new byte[11 + stringBytes.length];
|
||||
run[0] = 20;
|
||||
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);
|
||||
run[3] = (byte) ((fontTable >> 16) & 0xff);
|
||||
run[4] = (byte) ((fontTable >> 24) & 0xff);
|
||||
run[5] = (byte) (first.gx & 0xff);
|
||||
run[6] = (byte) ((first.gx >> 8) & 0xff);
|
||||
run[7] = (byte) (first.draw.y & 0xff);
|
||||
run[8] = (byte) ((first.draw.y >> 8) & 0xff);
|
||||
run[9] = (byte) (first.top | 0x10); // top color + transparent
|
||||
run[10] = (byte) stringBytes.length;
|
||||
System.arraycopy(stringBytes, 0, run, 11, stringBytes.length);
|
||||
outRuns.add(run);
|
||||
drawn.addAll(runGlyphs);
|
||||
i = j;
|
||||
|
||||
@@ -162,7 +162,7 @@ public class OfflineModelConfig {
|
||||
public static class Builder {
|
||||
private OfflineTransducerModelConfig transducer = new OfflineTransducerModelConfig();
|
||||
private OfflineParaformerModelConfig paraformer = new OfflineParaformerModelConfig();
|
||||
private OfflineWhisperModelConfig whisper = new OfflineWhisperModelConfig();
|
||||
private OfflineWhisperModelConfig whisper = OfflineWhisperModelConfig.builder().build();
|
||||
private OfflineFireRedAsrModelConfig fireRedAsr = new OfflineFireRedAsrModelConfig();
|
||||
private OfflineMoonshineModelConfig moonshine = OfflineMoonshineModelConfig.builder().build();
|
||||
private OfflineNemoEncDecCtcModelConfig nemo = new OfflineNemoEncDecCtcModelConfig();
|
||||
@@ -191,6 +191,11 @@ public class OfflineModelConfig {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setWhisper(OfflineWhisperModelConfig whisper) {
|
||||
this.whisper = whisper;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTokens(String tokens) {
|
||||
this.tokens = tokens;
|
||||
return this;
|
||||
@@ -228,16 +233,6 @@ class OfflineParaformerModelConfig {
|
||||
private final QnnConfig qnnConfig = new QnnConfig();
|
||||
}
|
||||
|
||||
class OfflineWhisperModelConfig {
|
||||
private final String encoder = "";
|
||||
private final String decoder = "";
|
||||
private final String language = "en";
|
||||
private final String task = "transcribe";
|
||||
private final int tailPaddings = 1000;
|
||||
private final boolean enableTokenTimestamps = false;
|
||||
private final boolean enableSegmentTimestamps = false;
|
||||
}
|
||||
|
||||
class OfflineFireRedAsrModelConfig {
|
||||
private final String encoder = "";
|
||||
private final String decoder = "";
|
||||
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
/**
|
||||
* Config for sherpa-onnx's offline (non-streaming) Whisper backend.
|
||||
*
|
||||
* Field names and types here are load-bearing: the native JNI glue reads
|
||||
* them by name via reflection (GetFieldID), not through the getters below,
|
||||
* so they must match sherpa-onnx's own OfflineWhisperModelConfig exactly
|
||||
* (verified against sherpa-onnx/jni/offline-recognizer.cc at the pinned
|
||||
* release tag: it unconditionally reads encoder, decoder, language, task,
|
||||
* tailPaddings, enableTokenTimestamps and enableSegmentTimestamps off the
|
||||
* OfflineModelConfig's "whisper" field). This mirrors the
|
||||
* OfflineMoonshineModelConfig.java pattern already used in this package.
|
||||
*/
|
||||
public class OfflineWhisperModelConfig {
|
||||
private final String encoder;
|
||||
private final String decoder;
|
||||
private final String language;
|
||||
private final String task;
|
||||
private final int tailPaddings;
|
||||
private final boolean enableTokenTimestamps;
|
||||
private final boolean enableSegmentTimestamps;
|
||||
|
||||
private OfflineWhisperModelConfig(Builder builder) {
|
||||
this.encoder = builder.encoder;
|
||||
this.decoder = builder.decoder;
|
||||
this.language = builder.language;
|
||||
this.task = builder.task;
|
||||
this.tailPaddings = builder.tailPaddings;
|
||||
this.enableTokenTimestamps = builder.enableTokenTimestamps;
|
||||
this.enableSegmentTimestamps = builder.enableSegmentTimestamps;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getEncoder() {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
public String getDecoder() {
|
||||
return decoder;
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
public String getTask() {
|
||||
return task;
|
||||
}
|
||||
|
||||
public int getTailPaddings() {
|
||||
return tailPaddings;
|
||||
}
|
||||
|
||||
public boolean getEnableTokenTimestamps() {
|
||||
return enableTokenTimestamps;
|
||||
}
|
||||
|
||||
public boolean getEnableSegmentTimestamps() {
|
||||
return enableSegmentTimestamps;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String encoder = "";
|
||||
private String decoder = "";
|
||||
private String language = "en";
|
||||
private String task = "transcribe";
|
||||
private int tailPaddings = 1000;
|
||||
private boolean enableTokenTimestamps = false;
|
||||
private boolean enableSegmentTimestamps = false;
|
||||
|
||||
public Builder setEncoder(String encoder) {
|
||||
this.encoder = encoder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDecoder(String decoder) {
|
||||
this.decoder = decoder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setLanguage(String language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTask(String task) {
|
||||
this.task = task;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTailPaddings(int tailPaddings) {
|
||||
this.tailPaddings = tailPaddings;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setEnableTokenTimestamps(boolean enableTokenTimestamps) {
|
||||
this.enableTokenTimestamps = enableTokenTimestamps;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setEnableSegmentTimestamps(boolean enableSegmentTimestamps) {
|
||||
this.enableSegmentTimestamps = enableSegmentTimestamps;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OfflineWhisperModelConfig build() {
|
||||
return new OfflineWhisperModelConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<!-- Overrides the CLI-generated app_name (derived from package.json's
|
||||
lowercase "faceclaw") so the launcher shows a capitalized label. -->
|
||||
<string name="app_name">Faceclaw</string>
|
||||
<string name="title_activity_kimera">Faceclaw</string>
|
||||
</resources>
|
||||
@@ -1,10 +1,58 @@
|
||||
# Changelog
|
||||
|
||||
## 0.6.6 - Future
|
||||
* Navigate and terminal app: App-specific screen size/position setting
|
||||
## 0.7.1 - Unreleased
|
||||
|
||||
* Add Wear OS watch battery indicator to the top bar and Glanceboard
|
||||
* Glanceboard tap setting includes a Disable option
|
||||
* Refinements to builtin games
|
||||
* Optimize bluetooth message transport
|
||||
* Short-the-long gesture from screen off activates the glanceboard
|
||||
* Glanceboard has a 2x3 layout option
|
||||
|
||||
## 0.7.0 - 2026-09-12
|
||||
|
||||
### Major Features
|
||||
|
||||
* Add Glanceboard
|
||||
|
||||
The Glanceboard is a customizable dashboard, which opens when you single-tap
|
||||
or long-press-and-hold while the screen is off, then closes automatically. The
|
||||
idea is that this is where you put things that you want to keep accessible
|
||||
while you're not actively using the glasses. The glanceboard has four slots
|
||||
for widgets, and has a system card widget (time, notifications, etc) and
|
||||
widgets for music, calendar, terminals, and more.
|
||||
|
||||
* Add "AI Chat" app
|
||||
|
||||
This is a conversational-style way to use the voice assistant that was already
|
||||
built in. Rather than say the wake word each time, while in the AI Chat app,
|
||||
you press-and-hold the ring to start speaking and release to send.
|
||||
|
||||
* Significantly increase speed
|
||||
|
||||
This version reconfigures the glasses' Bluetooth radio link, increasing
|
||||
transfer speed by 3-10x. This makes the user interface a lot snappier overall,
|
||||
and will enable better animations and graphics in future versions. There is a
|
||||
new Flappy game, to demonstrate that we can now reasonably handle 30fps
|
||||
full-screen animation. This does increase battery usage, but not by very much;
|
||||
based on the specs of the bluetooth chip used, this is estiamted to consume an
|
||||
extra 5% battery per day.
|
||||
|
||||
### Smaller Features and Bug Fixes
|
||||
|
||||
* After starting in Preview mode, connecting glasses walks you through the custom firmware flashing process in the same way as regular onboarding
|
||||
* Messaging in the flasher distinguishes between downgrading from stock firmwares where that's been validated and veresions where it hasn't, and considers 2.2.10 validated
|
||||
* Navigate and Terminal apps have their own config setting for screen-size
|
||||
* Transcribe app: Scrolling; automatic reconnection; insert newlines
|
||||
* Notifications: Add notification filters
|
||||
* Phone screen preview: Add rotation option; improve landscape layout
|
||||
* Add brightness control to the system context menu
|
||||
* Add R1 ring battery to the top bar
|
||||
* Battery indicators have new styling and more customization options
|
||||
* Add magnetometer calibration debug info to the Compass app
|
||||
* Nightscout app has alert thresholds for canula age, cartridge volume, pump battery, time since last loop, shown in the top bar
|
||||
* Terminal app: Icons in the app-switcher sidebar show terminal activity
|
||||
* Speed up wake wake from sleep (https://github.com/jimrandomh/faceclaw/pull/24)
|
||||
|
||||
## 0.6.5 - 2026-09-04
|
||||
* Changed context menu gestures: — for system, ●— for app context menu.
|
||||
@@ -62,4 +110,3 @@
|
||||
|
||||
## 0.6 - 2026-08-29
|
||||
- Start keeping a changelog and making signed binaries via GitHub Releases
|
||||
|
||||
@@ -66,6 +66,8 @@ import it into the new version.
|
||||
slow) or with an Anthropic or OpenAI model (requires an API key), or using
|
||||
your own long-running OpenClaw agent.
|
||||
* **Multitasking**, with an app-switcher sidebar and app launcher.
|
||||
* **Glanceboard**, a dashboard with widgets that you can quickly view with a
|
||||
tap or tap-and-hold from the screen-off state.
|
||||
* **Mostly-compatible with EvenHub apps.**
|
||||
* **A lock screen**; glasses lock automatically when you take them off and
|
||||
unlock when you unlock your phone.
|
||||
@@ -86,23 +88,14 @@ import it into the new version.
|
||||
* **Power management**: the glasses go to sleep properly when the screen is
|
||||
off, and wake when you double-tap the ring or speak the wakeword, allowing
|
||||
battery life similar to the stock Even app.
|
||||
* **Connection management** with auto-reconnect, and autodetection of
|
||||
conflict with the official Even Realities app.
|
||||
* **A Wear OS watch app** that replaces (and outdoes) the R1 ring: tap,
|
||||
* **A Wear OS watch app** that complements (or replaces) the R1 ring: tap,
|
||||
swipe, hold and crown gestures, side buttons, app launching and window
|
||||
switching, voice or keyboard queries to the assistant with the reply on
|
||||
your wrist, typing into apps, and glasses status/lock/display control.
|
||||
* **On-phone screen mirroring with touch control**: tap what you see on the
|
||||
mirror (sidebar icons, launcher cells), or use the phone's own touchpad,
|
||||
d-pad and Back/Menu buttons — the same spatial scheme as the watch — plus a
|
||||
compact ring simulator. A display-mode picker (576×288 band, 576×480 tall,
|
||||
or the full 640×480 panel with an auto-hiding sidebar) and a brightness
|
||||
slider with an Auto toggle sit beside the mirror.
|
||||
* **Bluetooth pairing** that scans for nearby glasses and identifies each
|
||||
pair before connecting: model, frame shape, and colour decoded from the
|
||||
advertised serial (with product photos), left and right arms matched to
|
||||
each other by that serial, an estimated distance so the pair in your hand
|
||||
sorts first, and the optional R1 ring.
|
||||
* **On-phone screen mirroring**: Shows the glasses screen contents and
|
||||
provides a phone-screen touch area equivalent to the R1 ring or the Wear OS
|
||||
watch app. This can also be used to try out the app without pairing or
|
||||
flashing a real pair of glasses.
|
||||
* **Dual-language NativeScript architecture**, with Java for the
|
||||
multithreaded Android API and bluetooth stack bits, Typescript for the bits
|
||||
you want to hack on.
|
||||
@@ -175,7 +168,8 @@ it getting paused, throttled to low CPU usage, etc.
|
||||
Be bold. Modify Faceclaw into the app that you want it to be for yourself,
|
||||
without worrying about whether other people will like your version. Then if you
|
||||
think your changes might be useful to others, make a pull request at
|
||||
https://github.com/jimrandomh/faceclaw.
|
||||
https://github.com/jimrandomh/faceclaw. Consider joining the Discord server
|
||||
to talk to other developer: https://discord.gg/XVFDFSwek3
|
||||
|
||||
The Typescript and Java code in this repository runs on your phone, not on the
|
||||
glasses themselves, and (with the narrow exception of the firmware-updating
|
||||
|
||||
+10
-1
@@ -52,7 +52,8 @@ TextField {
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
.system-name-modal TextField {
|
||||
.system-name-modal TextField,
|
||||
.system-name-modal TextView {
|
||||
background-color: #ffffff;
|
||||
color: #222222;
|
||||
}
|
||||
@@ -132,6 +133,14 @@ Button.-primary {
|
||||
border-radius: 8;
|
||||
}
|
||||
|
||||
/* The modal stays light in dark mode (setting editor and keyboard-input
|
||||
* panel); the .ns-dark pairing outguns the theme's dark-mode recolour of
|
||||
* Label/.h2 so the title doesn't go white-on-light-gray. */
|
||||
.system-name-modal Label,
|
||||
.ns-dark .system-name-modal Label {
|
||||
color: #222222;
|
||||
}
|
||||
|
||||
/* Modal behind the action bar's warning triangle: the problem-status warnings
|
||||
* that used to sit inline on the main page, each with its fix-it button.
|
||||
* The width is set from the view model (near-full-width on phones, capped on
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { ASSISTANT_MODEL_VALUES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
|
||||
import type { AssistantTranscriptEntry } from "../../assistant/session";
|
||||
import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations";
|
||||
import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, gestureHints, type InputEvent } from "../../ui/gestures";
|
||||
import type { Layer, LayerContext } from "../../ui/layers";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import { WindowMenuLayer } from "../../ui/window-menu";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { VoiceDraft } from "./voice-draft";
|
||||
|
||||
class AiChatLayer implements Layer {
|
||||
readonly acceptsDirectional = true;
|
||||
private firstLine: number | null = null; // null follows the streaming tail
|
||||
private maxFirstLine = 0;
|
||||
private selectedId = "";
|
||||
private requestRender = () => {};
|
||||
private readonly wrappedEntries = new WeakMap<AssistantTranscriptEntry, {
|
||||
font: UiFont; width: number; text: string; lines: string[];
|
||||
}>();
|
||||
readonly draft: VoiceDraft;
|
||||
|
||||
constructor(private readonly conversations: AssistantConversations, options: InProcessAppOptions) {
|
||||
this.draft = new VoiceDraft(options.actions, () => shell.prepareChatVoiceCapture(),
|
||||
() => this.requestRender(), (text) => this.send(text));
|
||||
}
|
||||
start(render: () => void): () => void {
|
||||
this.requestRender = render;
|
||||
return this.conversations.onChanged(() => render());
|
||||
}
|
||||
send(text: string): void {
|
||||
this.firstLine = null;
|
||||
shell.sendToAssistant(text, false);
|
||||
}
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const size = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(size.width, size.height);
|
||||
const font = getDefaultSmallFont();
|
||||
const step = font.lineHeight + 3;
|
||||
const left = 12;
|
||||
const width = size.width - 28;
|
||||
const record = this.conversations.current();
|
||||
if (this.selectedId !== record.id) { this.selectedId = record.id; this.firstLine = null; }
|
||||
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
|
||||
const title = this.conversations.title();
|
||||
image.drawText(font, left, 6, truncateText(font, title, width), 180);
|
||||
const label = assistantBackendSetting.get() === "external" ? "External agent" :
|
||||
`${assistantModelLabel(record.model)} · ${record.reasoning === "default" ? "default reasoning" : record.reasoning}`;
|
||||
const footer = wrapText(font, `${label} ${gestureHints([
|
||||
[GESTURE_CLICK, "menu"], [GESTURE_DOUBLE_CLICK, "back"], [GESTURE_LONG_PRESS, "hold to talk"],
|
||||
])}`, width);
|
||||
const footerY = size.height - step * footer.length;
|
||||
const footerTop = footerY - step * 3 - 12;
|
||||
const historyTop = step + 16;
|
||||
const visible = Math.max(1, Math.floor((footerTop - historyTop - 8) / step));
|
||||
const lines: { text: string; color: number }[] = [];
|
||||
for (const entry of transcript) {
|
||||
if (!entry.text) continue;
|
||||
let wrapped = this.wrappedEntries.get(entry);
|
||||
if (!wrapped || wrapped.font !== font || wrapped.width !== width || wrapped.text !== entry.text) {
|
||||
wrapped = { font, width, text: entry.text,
|
||||
lines: wrapText(font, `${entry.role === "user" ? "You" : "AI"}: ${entry.text}`, width - 8) };
|
||||
this.wrappedEntries.set(entry, wrapped);
|
||||
}
|
||||
for (const text of wrapped.lines) {
|
||||
lines.push({ text, color: entry.role === "user" ? 175 : 245 });
|
||||
}
|
||||
lines.push({ text: "", color: 0 });
|
||||
}
|
||||
if (!lines.length) lines.push({ text: "Hold to talk to your assistant.", color: 175 },
|
||||
{ text: "Voice and chat share this conversation.", color: 150 });
|
||||
this.maxFirstLine = Math.max(0, lines.length - visible);
|
||||
const first = this.firstLine === null ? this.maxFirstLine : Math.min(this.firstLine, this.maxFirstLine);
|
||||
for (let row = 0; row < visible; row++) {
|
||||
const line = lines[first + row];
|
||||
if (line) image.drawText(font, left, historyTop + row * step, line.text, line.color);
|
||||
}
|
||||
if (this.maxFirstLine > 0) {
|
||||
const track = visible * step;
|
||||
const thumb = Math.max(8, Math.floor(track * visible / lines.length));
|
||||
image.fillRect(size.width - 5, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), 2, thumb, 110);
|
||||
}
|
||||
image.fillRect(left, footerTop, width, 1, 65);
|
||||
const state = this.draft.active ? this.draft.status : record.session?.status || this.draft.status;
|
||||
if (state) image.drawText(font, left, footerTop + 5, truncateText(font, state, width), 160);
|
||||
const pending = wrapText(font, this.draft.text || (this.draft.active ? "Listening..." : "Hold to speak; release to send"), width);
|
||||
pending.slice(-2).forEach((text, i) => image.drawText(font, left, footerTop + step * (i + 1) + 5, text, 220));
|
||||
footer.forEach((text, i) => image.drawText(font, left + width - font.measureText(text), footerY + i * step, text, 120));
|
||||
return image;
|
||||
}
|
||||
handleInput(event: InputEvent): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up": case "swipe-up":
|
||||
this.firstLine = Math.max(0, (this.firstLine ?? this.maxFirstLine) - 3); return;
|
||||
case "scroll-down": case "swipe-down": {
|
||||
const next = (this.firstLine ?? this.maxFirstLine) + 3;
|
||||
this.firstLine = next >= this.maxFirstLine ? null : next; return;
|
||||
}
|
||||
case "long-press":
|
||||
if (!this.conversations.current().session?.isTurnActive()) {
|
||||
if (!shell.isAssistantAvailable()) { shell.showAlert("Set an API key or download the on-phone model in Settings."); return; }
|
||||
void this.draft.start();
|
||||
}
|
||||
return;
|
||||
case "long-press-release": this.draft.release(); return;
|
||||
case "double-click": this.draft.cancel(); shell.yieldFocusToSidebar(); return;
|
||||
}
|
||||
}
|
||||
menuItems(): MenuItem[] {
|
||||
const record = this.conversations.current();
|
||||
const busy = () => this.draft.active || Boolean(record.session?.isTurnActive());
|
||||
const external = assistantBackendSetting.get() === "external";
|
||||
const submenu = (ctx: LayerContext, title: string, items: MenuItem[]) => ctx.stack.push(new WindowMenuLayer(title, items, true));
|
||||
return [
|
||||
...(external ? [{ label: "Sessions and model managed by bridge", disabled: true, onSelect: () => {} }] : []),
|
||||
{ label: "New session", disabled: () => external || busy(), onSelect: (ctx) => {
|
||||
if (this.conversations.create()) ctx.stack.clearToBase();
|
||||
} },
|
||||
{ label: "Switch session", disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Sessions",
|
||||
[...this.conversations.list()].reverse().map((item) => ({
|
||||
label: `${item.id === record.id ? "✓ " : ""}${this.conversations.title(item)}`,
|
||||
onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model",
|
||||
ASSISTANT_MODEL_VALUES.map((model) => ({
|
||||
label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`,
|
||||
disabled: () => !this.conversations.available(model, record.reasoning),
|
||||
onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Reasoning: ${record.reasoning}`, disabled: () => {
|
||||
const llm = resolveAssistantModel(record.model, { anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get() });
|
||||
return external || busy() || !llm?.effort;
|
||||
}, onSelect: (ctx) => submenu(ctx, "Reasoning", (["default", "low", "medium", "high"] as ReasoningLevel[]).map((reasoning) => ({
|
||||
label: `${reasoning === record.reasoning ? "✓ " : ""}${reasoning === "default" ? "Model default" : reasoning}`,
|
||||
onSelect: (ctx) => { if (this.conversations.configure(record.model, reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
...(record.session?.isTurnActive() ? [{ label: "Cancel response", onSelect: (ctx: LayerContext) => {
|
||||
record.session?.cancel(); ctx.stack.clearToBase();
|
||||
} }] : []),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export function createAiChatWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const layer = new AiChatLayer(shell.getAssistantConversations(), options);
|
||||
let unsubscribe = () => {};
|
||||
const app = createInProcessWindow({
|
||||
...options,
|
||||
appId: "ai-chat", windowId: "ai-chat", title: "AI Chat", iconLetter: "AI", icon: "message-circle", closeable: true,
|
||||
baseLayer: layer, holdToTalk: true,
|
||||
isVoiceCapturing: () => layer.draft.active,
|
||||
onSystemMenuOpened: () => layer.draft.cancel(),
|
||||
onAppMenuOpened: () => layer.draft.cancel(),
|
||||
setScreenOn: (on) => { if (!on) layer.draft.cancel(); },
|
||||
setSurfaceVisible: (visible) => { if (!visible) layer.draft.cancel(); options.setSurfaceVisible(visible); },
|
||||
menuItems: () => layer.menuItems(),
|
||||
receiveTextInput: (text) => { if (!layer.draft.active) layer.send(text); },
|
||||
onClosed: () => { unsubscribe(); layer.draft.cancel(); options.onClosed(); },
|
||||
});
|
||||
unsubscribe = layer.start(app.requestRender);
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { AppDefinition } from "../app-definition";
|
||||
import { createAiChatWindow } from "./ai-chat-app";
|
||||
|
||||
const aiChatApp: AppDefinition = {
|
||||
appId: "ai-chat",
|
||||
title: "AI Chat",
|
||||
icon: "message-circle",
|
||||
launch: (ctx) => ctx.launchInProcessApp("ai-chat", "window:ai-chat", createAiChatWindow),
|
||||
};
|
||||
export default aiChatApp;
|
||||
@@ -0,0 +1,136 @@
|
||||
import { voiceControlBridge } from "../../native/voice-control";
|
||||
import { voiceActivity } from "../../ui/shell/voice-activity";
|
||||
import type { LayerActions } from "../../ui/layers";
|
||||
|
||||
/** One hold/release cycle. Async startup and trailing cloud finals belong to this cycle only. */
|
||||
export class VoiceDraft {
|
||||
phase: "idle" | "preparing" | "listening" | "finishing" = "idle";
|
||||
text = "";
|
||||
status = "";
|
||||
private committed = "";
|
||||
private live = "";
|
||||
private generation = 0;
|
||||
private started = false;
|
||||
private ownsVoiceActivity = false;
|
||||
private starting = false;
|
||||
private unsubscribe: (() => void)[] = [];
|
||||
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly actions: Pick<LayerActions, "startVoiceCapture" | "stopVoiceCapture">,
|
||||
private readonly prepare: () => Promise<boolean>,
|
||||
private readonly changed: () => void,
|
||||
private readonly send: (text: string) => void,
|
||||
) {}
|
||||
|
||||
get active(): boolean { return this.phase !== "idle" || this.starting; }
|
||||
|
||||
async start(): Promise<void> {
|
||||
if (this.active) return;
|
||||
const generation = ++this.generation;
|
||||
this.text = this.committed = this.live = "";
|
||||
this.phase = "preparing";
|
||||
this.status = "Preparing microphone...";
|
||||
this.changed();
|
||||
try {
|
||||
const ready = await this.prepare();
|
||||
if (generation !== this.generation) return;
|
||||
if (!ready) { this.cancel("Microphone unavailable"); return; }
|
||||
this.phase = "listening";
|
||||
this.status = "Listening... release to send";
|
||||
this.ownsVoiceActivity = true;
|
||||
voiceActivity.setActive(true);
|
||||
this.unsubscribe.push(voiceControlBridge.onTranscript((event) => {
|
||||
if (!this.active || generation !== this.generation) return;
|
||||
if (event.isFinal) {
|
||||
const text = event.text.trim();
|
||||
if (text) this.committed = [this.committed, text].filter(Boolean).join(" ");
|
||||
this.live = "";
|
||||
} else this.live = event.text.trim();
|
||||
this.text = [this.committed, this.live].filter(Boolean).join(" ");
|
||||
if (event.isFinal && this.phase === "finishing") this.finish();
|
||||
else this.changed();
|
||||
}));
|
||||
// onStatus immediately replays the previous capture's status.
|
||||
let subscribed = false;
|
||||
this.unsubscribe.push(voiceControlBridge.onStatus(({ status }) => {
|
||||
if (!subscribed) return;
|
||||
if (generation !== this.generation || !this.active) return;
|
||||
if (/ignored|failed|error|denied/i.test(status)) this.cancel(status);
|
||||
}));
|
||||
subscribed = true;
|
||||
if (generation !== this.generation) return;
|
||||
this.starting = true;
|
||||
this.changed();
|
||||
await this.actions.startVoiceCapture();
|
||||
this.starting = false;
|
||||
if (generation !== this.generation) {
|
||||
await this.actions.stopVoiceCapture();
|
||||
this.changed();
|
||||
return;
|
||||
}
|
||||
this.started = true;
|
||||
if (this.isFinishing()) this.stopAndFinalize();
|
||||
} catch (error) {
|
||||
this.starting = false;
|
||||
if (generation === this.generation) this.cancel(`Microphone: ${String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
private isFinishing(): boolean { return this.phase === "finishing"; }
|
||||
|
||||
release(): void {
|
||||
if (this.phase === "preparing") { this.cancel(); return; }
|
||||
if (this.phase !== "listening") return;
|
||||
this.phase = "finishing";
|
||||
this.status = "Finishing transcription...";
|
||||
this.changed();
|
||||
if (!this.starting) this.stopAndFinalize();
|
||||
}
|
||||
|
||||
private stopAndFinalize(): void {
|
||||
const generation = this.generation;
|
||||
const afterStop = () => {
|
||||
if (generation !== this.generation || !this.isFinishing()) return;
|
||||
this.timer = setTimeout(() => this.finish(), 1500);
|
||||
};
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
// Native recognition can take longer than the synchronous stop join.
|
||||
// Its final callback may finish the draft before this promise resolves.
|
||||
const stopped = this.actions.stopVoiceCapture();
|
||||
if (!stopped) { afterStop(); return; }
|
||||
void stopped.then(afterStop, (error) => {
|
||||
if (generation === this.generation) this.cancel(String(error));
|
||||
});
|
||||
} else {
|
||||
afterStop();
|
||||
}
|
||||
}
|
||||
|
||||
private finish(): void {
|
||||
if (this.phase !== "finishing") return;
|
||||
const text = this.text.trim();
|
||||
this.cancel(text ? "" : "No speech detected");
|
||||
if (text) this.send(text);
|
||||
}
|
||||
|
||||
cancel(status = ""): void {
|
||||
++this.generation;
|
||||
this.phase = "idle";
|
||||
this.status = status;
|
||||
this.text = this.live = this.committed = "";
|
||||
if (this.timer !== null) clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
for (const unsubscribe of this.unsubscribe.splice(0)) unsubscribe();
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
void Promise.resolve(this.actions.stopVoiceCapture()).catch(() => {});
|
||||
}
|
||||
if (this.ownsVoiceActivity) {
|
||||
this.ownsVoiceActivity = false;
|
||||
voiceActivity.setActive(false);
|
||||
}
|
||||
this.changed();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type AppDefinition } from "./app-definition";
|
||||
import aiChatApp from "./ai-chat";
|
||||
import launcherApp from "./launcher";
|
||||
import timerApp from "./timer";
|
||||
import calculatorApp from "./calculator";
|
||||
@@ -19,8 +20,10 @@ import blocksApp from "./blocks";
|
||||
import minesweeperApp from "./minesweeper";
|
||||
import freecellApp from "./freecell";
|
||||
import pinballApp from "./pinball";
|
||||
import flappyApp from "./flappy";
|
||||
import developerApp from "./developer";
|
||||
import evenhubApp from "./evenhub";
|
||||
import glanceboardApp from "./glanceboard";
|
||||
import settingsApp from "./settings";
|
||||
|
||||
/**
|
||||
@@ -30,6 +33,7 @@ import settingsApp from "./settings";
|
||||
*/
|
||||
export const ALL_APPS: readonly AppDefinition[] = [
|
||||
launcherApp,
|
||||
aiChatApp,
|
||||
timerApp,
|
||||
calculatorApp,
|
||||
terminalApp,
|
||||
@@ -49,7 +53,9 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
||||
minesweeperApp,
|
||||
freecellApp,
|
||||
pinballApp,
|
||||
flappyApp,
|
||||
developerApp,
|
||||
evenhubApp,
|
||||
glanceboardApp,
|
||||
settingsApp,
|
||||
];
|
||||
@@ -32,6 +32,39 @@ export type AppDefinition = {
|
||||
showInLauncher?: boolean;
|
||||
/** Present on the app that handles text shared via the Android share intent. */
|
||||
openSharedText?: (ctx: AppContext, title: string, text: string) => void;
|
||||
/**
|
||||
* Present on the app that supplies the Glanceboard: the sleep-time display
|
||||
* the controller shows on a tap or hold while the shell is asleep (see
|
||||
* app/g2/glance-host.ts). At most one app provides it.
|
||||
*/
|
||||
glanceboard?: GlanceboardProvider;
|
||||
};
|
||||
|
||||
/** A board the glance host can show while the shell sleeps. */
|
||||
export type GlanceboardProvider = {
|
||||
/** Board pixel size; the host centres it in the standard 576x288 band. */
|
||||
size: { width: number; height: number };
|
||||
/** Whether sleep-time gestures should show the board at all (user setting). */
|
||||
isEnabled: () => boolean;
|
||||
/** Whether a single tap shows the board (for tapTimeoutMs). */
|
||||
showOnTap: () => boolean;
|
||||
/** How long a tap or head-tilt keeps the board up. */
|
||||
tapTimeoutMs: () => number;
|
||||
/** Whether a long-press holds the board up until released. */
|
||||
showOnLongPress: () => boolean;
|
||||
/** Whether the head-tilt wake goes to the board rather than the regular UI. */
|
||||
showOnHeadTilt: () => boolean;
|
||||
/** A fresh board; requestRender asks the host to repaint it. */
|
||||
createBoard: (requestRender: () => void) => GlanceBoardInstance;
|
||||
};
|
||||
|
||||
export type GlanceBoardInstance = {
|
||||
/** The board is about to show: start widgets (subscriptions, hardware). */
|
||||
start: () => void;
|
||||
/** The board was hidden: stop everything start began. */
|
||||
stop: () => void;
|
||||
/** Paint the board at its size. */
|
||||
paint: () => GrayImage;
|
||||
};
|
||||
|
||||
export type AppLaunchParams = {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
/**
|
||||
* Blocks app worker: a falling-blocks game. One singleton window holds the
|
||||
* whole game; a gravity interval drives piece descent while the window is
|
||||
* foreground and the screen is on (losing either auto-pauses).
|
||||
* foreground, the screen is on, and the window holds input focus (losing any
|
||||
* of them auto-pauses: backgrounding, screen-off, the system menu or a
|
||||
* notification modal opening over the game, focus going to the sidebar).
|
||||
*
|
||||
* Controls (in play): scroll moves the piece, click rotates, long-press hard
|
||||
* drops, double-click pauses. Watch swipes are spatial: left/right move,
|
||||
@@ -17,6 +19,7 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -43,11 +46,16 @@ const BOARD_X = 60;
|
||||
const BOARD_Y = 10;
|
||||
const PANEL_X = 230;
|
||||
|
||||
/** Gravity starts here and speeds up with score (see dropIntervalMs). */
|
||||
const BASE_DROP_MS = 1200;
|
||||
const MIN_DROP_MS = 200;
|
||||
/** Every this many points, gravity gets one 12% step faster. */
|
||||
const SPEED_STEP_SCORE = 300;
|
||||
/**
|
||||
* Gravity starts here and speeds up with score (see dropIntervalMs). Slow
|
||||
* by desktop standards: the glasses round trip is ~250 ms, so a piece must
|
||||
* hang around long enough to be steered through it.
|
||||
*/
|
||||
const BASE_DROP_MS = 1500;
|
||||
const MIN_DROP_MS = 350;
|
||||
/** Every this many points, gravity gets one SPEED_STEP_FACTOR step faster. */
|
||||
const SPEED_STEP_SCORE = 500;
|
||||
const SPEED_STEP_FACTOR = 0.92;
|
||||
/** Points per cleared-line count (index = simultaneous lines), times level. */
|
||||
const LINE_SCORES = [0, 100, 300, 500, 800];
|
||||
const HARD_DROP_POINTS_PER_ROW = 2;
|
||||
@@ -201,7 +209,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
lines: 0,
|
||||
tickTimer: null,
|
||||
tickIntervalMs: BASE_DROP_MS,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("blocks"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
windows.set(message.windowId, window);
|
||||
@@ -248,6 +256,20 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away from the game (the system menu, a
|
||||
// notification modal, the voice dialog, focus back to the sidebar)
|
||||
// pauses it: the player can't steer a piece they aren't in control of.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
updateTickTimer(window);
|
||||
// Still on screen under a shell overlay, so show the pause.
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
@@ -296,6 +318,7 @@ function windowMenuItems(window: BlocksWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("blocks", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
@@ -547,7 +570,7 @@ function level(window: BlocksWindow): number {
|
||||
|
||||
function dropIntervalMs(window: BlocksWindow): number {
|
||||
const steps = Math.floor(window.score / SPEED_STEP_SCORE);
|
||||
return Math.max(MIN_DROP_MS, Math.round(BASE_DROP_MS * Math.pow(0.88, steps)));
|
||||
return Math.max(MIN_DROP_MS, Math.round(BASE_DROP_MS * Math.pow(SPEED_STEP_FACTOR, steps)));
|
||||
}
|
||||
|
||||
/** Keep the gravity interval running exactly when the game is live and visible. */
|
||||
|
||||
@@ -178,7 +178,7 @@ function dayKeyOf(timestampMs: number): string {
|
||||
return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
||||
}
|
||||
|
||||
function dayHeaderLabel(timestampMs: number): string {
|
||||
export function dayHeaderLabel(timestampMs: number): string {
|
||||
const date = new Date(timestampMs);
|
||||
const now = new Date();
|
||||
const midnight = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
@@ -189,7 +189,7 @@ function dayHeaderLabel(timestampMs: number): string {
|
||||
return base;
|
||||
}
|
||||
|
||||
function formatEventTime(timestampMs: number): string {
|
||||
export function formatEventTime(timestampMs: number): string {
|
||||
const date = new Date(timestampMs);
|
||||
const minutes = String(date.getMinutes()).padStart(2, "0");
|
||||
const hour24 = date.getHours();
|
||||
|
||||
+10
-322
@@ -23,6 +23,7 @@ import {
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
|
||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
|
||||
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
|
||||
@@ -41,59 +42,6 @@ const EDGE_PAD = 8;
|
||||
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
|
||||
const MAX_ROSE_RADIUS = 98;
|
||||
|
||||
/**
|
||||
* The rose is drawn as a flat disc lying in front of the wearer, tilted away
|
||||
* from vertical: `TILT_SQUASH` is how much that tilt shortens the depth axis,
|
||||
* and `TILT_PERSPECTIVE` how much nearer parts of the disc are magnified
|
||||
* relative to farther ones. Together they turn the ring into an egg-shaped
|
||||
* oval — wider and taller at the bottom — and give the rose's points their
|
||||
* foreshortening for free.
|
||||
*/
|
||||
const TILT_SQUASH = 0.42;
|
||||
const TILT_PERSPECTIVE = 0.22;
|
||||
/** Screen height of the projected ring, per unit of radius. */
|
||||
const RING_HEIGHT_PER_RADIUS =
|
||||
TILT_SQUASH * (1 / (1 + TILT_PERSPECTIVE) + 1 / (1 - TILT_PERSPECTIVE));
|
||||
/** Screen half-width of the projected ring, per unit of radius. */
|
||||
const RING_HALF_WIDTH_PER_RADIUS = 1 / Math.sqrt(1 - TILT_PERSPECTIVE * TILT_PERSPECTIVE);
|
||||
|
||||
/** Tip radii of the rose's long and short points, as a fraction of the ring. */
|
||||
const CARDINAL_TIP = 0.9;
|
||||
const INTERCARDINAL_TIP = 0.54;
|
||||
/** Radius the point bases sit on: how fat the rose is at its waist. */
|
||||
const POINT_BASE_RADIUS = 0.17;
|
||||
/** Half-angle of a point's base. 22.5° makes neighbouring bases just meet. */
|
||||
const POINT_BASE_HALF_ANGLE = 22.5;
|
||||
/**
|
||||
* A polar grid lying in the same plane as the disc, to give it somewhere to
|
||||
* sit and to fill the empty flanks either side of it. Rings are multiples of
|
||||
* the disc's radius; spokes are offset by half a step so none runs up the
|
||||
* middle into the heading tick.
|
||||
*/
|
||||
const PLANE_RINGS = [1.35, 1.8, 2.5, 3.6, 5.6];
|
||||
const PLANE_SPOKE_STEP = 30;
|
||||
/** Grid brightness at the disc's rim, and how fast it falls off with distance. */
|
||||
const PLANE_NEAR_VALUE = 78;
|
||||
const PLANE_FALLOFF = 0.55;
|
||||
/** Dimmer than this is left unpainted, so the grid ends by vanishing. */
|
||||
const PLANE_CUTOFF = 22;
|
||||
/** Height over which the grid fades out as it approaches the readout. */
|
||||
const PLANE_FADE_SPAN = 45;
|
||||
|
||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||
const TICK_HEIGHT = 7;
|
||||
const TICK_HALF_WIDTH = 4;
|
||||
/** Depth of the disc's side wall, as a fraction of the ring's radius. */
|
||||
const DISC_DEPTH = 0.1;
|
||||
/**
|
||||
* Rim angle at which the silhouette turns vertical. Between this and its
|
||||
* mirror the wall faces the wearer; outside it the rim curves back away, so a
|
||||
* wall drawn there would hook inward instead of ending on the vertical edge.
|
||||
*/
|
||||
const WALL_START_ANGLE = (Math.acos(-TILT_PERSPECTIVE) * 180) / Math.PI;
|
||||
/** Side-wall shading, from the edges of the silhouette to the nearest point. */
|
||||
const WALL_DARK = 20;
|
||||
const WALL_LIT = 62;
|
||||
|
||||
class CompassLayer implements Layer {
|
||||
private rawHeading: number | null = null;
|
||||
@@ -233,21 +181,15 @@ class CompassLayer implements Layer {
|
||||
// The rose is sized and placed first — it hangs off the bottom edge — and
|
||||
// the readout then floats in whatever room is left above it.
|
||||
const textHeight = readoutHeight + 4 + statusLines.length * smallStep;
|
||||
const radius = Math.max(
|
||||
24,
|
||||
Math.min(
|
||||
MAX_ROSE_RADIUS,
|
||||
Math.floor(
|
||||
(height - BOTTOM_PAD - TOP_PAD - textHeight - STACK_GAP - TICK_HEIGHT)
|
||||
/ (RING_HEIGHT_PER_RADIUS + DISC_DEPTH),
|
||||
),
|
||||
Math.floor((Math.min(cx, width - cx) - EDGE_PAD) / RING_HALF_WIDTH_PER_RADIUS),
|
||||
),
|
||||
);
|
||||
// The wall hangs below the rim, so the near rim sits a wall's depth up
|
||||
// from the bottom padding rather than on it.
|
||||
const cy = height - BOTTOM_PAD - discDepth(radius) - (radius * TILT_SQUASH) / (1 - TILT_PERSPECTIVE);
|
||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||
const { cy, radius, ringTop } = layoutCompassRose({
|
||||
width,
|
||||
height,
|
||||
cx,
|
||||
topClearance: TOP_PAD + textHeight + STACK_GAP,
|
||||
bottomPad: BOTTOM_PAD,
|
||||
edgePad: EDGE_PAD,
|
||||
maxRadius: MAX_ROSE_RADIUS,
|
||||
});
|
||||
|
||||
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
@@ -356,257 +298,3 @@ function frameStatusText(): string {
|
||||
return "Magnetic heading - waiting for location";
|
||||
}
|
||||
}
|
||||
|
||||
function cardinalDirection(heading: number): string {
|
||||
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||
}
|
||||
|
||||
interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Project a point of the rose's plane onto the screen. `angle` is degrees
|
||||
* clockwise from the far side of the disc — the direction the wearer faces —
|
||||
* and `radius` is a fraction of the rose's outer ring.
|
||||
*/
|
||||
function projectRose(cx: number, cy: number, scale: number, radius: number, angle: number): Point {
|
||||
const radians = (angle * Math.PI) / 180;
|
||||
const across = radius * Math.sin(radians);
|
||||
const away = radius * Math.cos(radians);
|
||||
const depth = 1 + TILT_PERSPECTIVE * away;
|
||||
return {
|
||||
x: cx + (across * scale) / depth,
|
||||
y: cy - (away * scale * TILT_SQUASH) / depth,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Bounds of every orientation of the rotating points. The longest tip sweeps
|
||||
* a circle in the disc's plane; perspective projects it to an offset ellipse.
|
||||
* Round outward so all rasterized triangle pixels fit inside the rectangle.
|
||||
*/
|
||||
function rotatingRoseBounds(cx: number, cy: number, radius: number): {
|
||||
x: number; y: number; width: number; height: number;
|
||||
} {
|
||||
const perspective = TILT_PERSPECTIVE * CARDINAL_TIP;
|
||||
const halfWidth = radius * CARDINAL_TIP / Math.sqrt(1 - perspective * perspective);
|
||||
const depth = radius * CARDINAL_TIP * TILT_SQUASH;
|
||||
const x = Math.floor(cx - halfWidth);
|
||||
const y = Math.floor(cy - depth / (1 + perspective));
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
width: Math.ceil(cx + halfWidth) - x,
|
||||
height: Math.ceil(cy + depth / (1 - perspective)) - y,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the fixed pixels inside the rotating rose's dirty rectangle as one
|
||||
* immutable texture. The wire planner can omit them from each raster delta
|
||||
* and restore them with a cached image draw. Outside that rectangle the
|
||||
* background stays raster, since heading updates do not change it.
|
||||
*
|
||||
* The texture uses transparent zero for the empty disc interior, so it can
|
||||
* overlay the rotating points. Its only ink over the points is the fixed
|
||||
* heading tick, which belongs on top of them.
|
||||
*/
|
||||
function createCompassBackground(
|
||||
width: number, height: number, cx: number, cy: number, radius: number, clipY: number, fade: number,
|
||||
): GrayImage {
|
||||
const image = new GrayImage(width, height, 0);
|
||||
drawPlaneGrid(image, cx, cy, radius, clipY);
|
||||
drawDiscWall(image, cx, cy, radius, fade);
|
||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||
|
||||
const bounds = rotatingRoseBounds(cx, cy, radius);
|
||||
const left = Math.max(0, bounds.x);
|
||||
const top = Math.max(0, bounds.y);
|
||||
const right = Math.min(width, bounds.x + bounds.width);
|
||||
const bottom = Math.min(height, bounds.y + bounds.height);
|
||||
if (right > left && bottom > top) {
|
||||
const texture = new GrayImage(right - left, bottom - top, 0);
|
||||
texture.bitBlt(image, 0, 0, { sx: left, sy: top, width: texture.width, height: texture.height });
|
||||
image.fillRect(left, top, texture.width, texture.height, 0);
|
||||
image.drawImage(texture, left, top);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Draw only the rotating points, with the brightest one aimed at north. */
|
||||
function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: number, heading: number | null): void {
|
||||
// Without a reading there is nothing to aim, so show the rose in its resting
|
||||
// orientation, dimmed, rather than an empty ring.
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const bearing = heading ?? 0;
|
||||
// Short points first, so the long ones sit on top of them at the waist. Only
|
||||
// north is drawn at full brightness: the other seven are there to make the
|
||||
// rose read as a rose, and competing with north would defeat the point.
|
||||
for (const degrees of [45, 135, 225, 315]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, INTERCARDINAL_TIP, 85 * fade, 40 * fade);
|
||||
}
|
||||
for (const degrees of [90, 180, 270]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, CARDINAL_TIP, 120 * fade, 55 * fade);
|
||||
}
|
||||
drawRosePoint(image, cx, cy, radius, -bearing, CARDINAL_TIP, 255 * fade, 140 * fade);
|
||||
}
|
||||
|
||||
/**
|
||||
* One point of the rose: a tapered spike split down its axis into a lit and a
|
||||
* shaded facet, which is what keeps the eight points readable once perspective
|
||||
* has squashed the ones lying across the wearer's line of sight.
|
||||
*/
|
||||
function drawRosePoint(
|
||||
image: GrayImage,
|
||||
cx: number,
|
||||
cy: number,
|
||||
radius: number,
|
||||
angle: number,
|
||||
tipRadius: number,
|
||||
litValue: number,
|
||||
shadedValue: number,
|
||||
): void {
|
||||
const centre = { x: cx, y: cy };
|
||||
const tip = projectRose(cx, cy, radius, tipRadius, angle);
|
||||
const clockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle + POINT_BASE_HALF_ANGLE);
|
||||
const anticlockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle - POINT_BASE_HALF_ANGLE);
|
||||
fillTriangle(image, tip, centre, clockwise, litValue);
|
||||
fillTriangle(image, tip, centre, anticlockwise, shadedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* The side wall below the wearer-facing arc of the rim, which turns the tilted
|
||||
* disc into a shallow cylinder. The arc runs between the two points where the
|
||||
* silhouette turns vertical, so the wall ends flush with that vertical edge,
|
||||
* and it is shaded from dark there to lit where it faces the wearer head-on.
|
||||
*/
|
||||
function drawDiscWall(image: GrayImage, cx: number, cy: number, radius: number, fade: number): void {
|
||||
const depth = discDepth(radius);
|
||||
const segments = 48;
|
||||
const sweep = 360 - WALL_START_ANGLE * 2;
|
||||
const rimValue = 105 * fade;
|
||||
let previous = projectRose(cx, cy, radius, 1, WALL_START_ANGLE);
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const angle = WALL_START_ANGLE + (i * sweep) / segments;
|
||||
const point = projectRose(cx, cy, radius, 1, angle);
|
||||
// Quads rather than per-column lines, so no gap opens up where the rim
|
||||
// runs nearly horizontal across the bottom of the oval.
|
||||
const value = (WALL_DARK + (WALL_LIT - WALL_DARK) * wallFacing(angle)) * fade;
|
||||
const top = { x: previous.x, y: previous.y };
|
||||
const next = { x: point.x, y: point.y };
|
||||
fillTriangle(image, top, next, { x: next.x, y: next.y + depth }, value);
|
||||
fillTriangle(image, top, { x: top.x, y: top.y + depth }, { x: next.x, y: next.y + depth }, value);
|
||||
image.drawLine(top.x, top.y + depth, next.x, next.y + depth, rimValue);
|
||||
previous = point;
|
||||
}
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
}
|
||||
|
||||
/** 1 where the wall faces the wearer head-on, 0 at the silhouette's edges. */
|
||||
function wallFacing(angle: number): number {
|
||||
const towardWearer = -Math.cos((angle * Math.PI) / 180);
|
||||
return (towardWearer - TILT_PERSPECTIVE) / (1 - TILT_PERSPECTIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The grid the disc sits on, drawn before the disc so the disc occludes it.
|
||||
* Brightness falls off with distance and fades to nothing as the plane climbs
|
||||
* towards `clipY`, so the grid dissolves into the black under the readout
|
||||
* rather than stopping at a hard edge.
|
||||
*/
|
||||
function drawPlaneGrid(image: GrayImage, cx: number, cy: number, radius: number, clipY: number): void {
|
||||
const shade = (point: Point, ringRadius: number): number => {
|
||||
if (point.y > image.height || Math.abs(point.x - cx) > image.width) return 0;
|
||||
const fade = Math.max(0, Math.min(1, (point.y - clipY) / PLANE_FADE_SPAN));
|
||||
const value = (PLANE_NEAR_VALUE / (1 + PLANE_FALLOFF * (ringRadius - 1))) * fade;
|
||||
return value >= PLANE_CUTOFF ? value : 0;
|
||||
};
|
||||
const trace = (
|
||||
steps: number,
|
||||
at: (step: number) => { point: Point; ringRadius: number },
|
||||
): void => {
|
||||
let previous: Point | null = null;
|
||||
let previousValue = 0;
|
||||
for (let i = 0; i <= steps; i++) {
|
||||
const { point, ringRadius } = at(i);
|
||||
const value = shade(point, ringRadius);
|
||||
if (previous !== null && previousValue > 0 && value > 0) {
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, (previousValue + value) / 2);
|
||||
}
|
||||
previous = point;
|
||||
previousValue = value;
|
||||
}
|
||||
};
|
||||
|
||||
for (const ringRadius of PLANE_RINGS) {
|
||||
trace(96, (step) => ({ point: projectRose(cx, cy, radius, ringRadius, (step * 360) / 96), ringRadius }));
|
||||
}
|
||||
// Spokes run from just outside the disc to the outermost ring, so the grid
|
||||
// closes on itself instead of trailing off into loose ends.
|
||||
const outer = PLANE_RINGS[PLANE_RINGS.length - 1]!;
|
||||
for (let angle = PLANE_SPOKE_STEP / 2; angle < 360; angle += PLANE_SPOKE_STEP) {
|
||||
trace(24, (step) => {
|
||||
const ringRadius = 1.05 + (step * (outer - 1.05)) / 24;
|
||||
return { point: projectRose(cx, cy, radius, ringRadius, angle), ringRadius };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Screen depth of the disc's side wall at the given ring radius. */
|
||||
function discDepth(radius: number): number {
|
||||
return Math.max(3, Math.round(radius * DISC_DEPTH));
|
||||
}
|
||||
|
||||
/** The ring bounding the rose: the tilted disc's rim, drawn as a polygon. */
|
||||
function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const segments = 72;
|
||||
let previous = projectRose(cx, cy, radius, 1, 0);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const point = projectRose(cx, cy, radius, 1, (i * 360) / segments);
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, value);
|
||||
previous = point;
|
||||
}
|
||||
}
|
||||
|
||||
/** A fixed mark just inside the top rim: the heading the wearer is facing. */
|
||||
function drawHeadingTick(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const top = projectRose(cx, cy, radius, 1, 0);
|
||||
const tickTop = top.y + 1;
|
||||
fillTriangle(
|
||||
image,
|
||||
{ x: cx, y: tickTop + TICK_HEIGHT - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
function fillTriangle(image: GrayImage, a: Point, b: Point, c: Point, value: number): void {
|
||||
const left = Math.max(0, Math.floor(Math.min(a.x, b.x, c.x)));
|
||||
const right = Math.min(image.width - 1, Math.ceil(Math.max(a.x, b.x, c.x)));
|
||||
const top = Math.max(0, Math.floor(Math.min(a.y, b.y, c.y)));
|
||||
const bottom = Math.min(image.height - 1, Math.ceil(Math.max(a.y, b.y, c.y)));
|
||||
const shade = Math.round(value);
|
||||
for (let y = top; y <= bottom; y++) {
|
||||
for (let x = left; x <= right; x++) {
|
||||
const point = { x: x + 0.5, y: y + 0.5 };
|
||||
const ab = edge(a, b, point);
|
||||
const bc = edge(b, c, point);
|
||||
const ca = edge(c, a, point);
|
||||
// Accept either winding, so callers don't have to order their vertices.
|
||||
if ((ab >= 0 && bc >= 0 && ca >= 0) || (ab <= 0 && bc <= 0 && ca <= 0)) {
|
||||
image.setPixel(x, y, shade);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Twice the signed area of the triangle a-b-p; its sign is p's side of a-b. */
|
||||
function edge(a: Point, b: Point, p: Point): number {
|
||||
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
/**
|
||||
* The compass rose renderer: a tilted disc with eight rotating points, drawn
|
||||
* from a heading into any GrayImage. Pure drawing code shared by the Compass
|
||||
* app's full-window view and the Glanceboard's compass widget; the callers
|
||||
* own the magnetometer lifecycle and the readout text around it.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { normalizeHeading } from "./calibration";
|
||||
|
||||
/**
|
||||
* The rose is drawn as a flat disc lying in front of the wearer, tilted away
|
||||
* from vertical: `TILT_SQUASH` is how much that tilt shortens the depth axis,
|
||||
* and `TILT_PERSPECTIVE` how much nearer parts of the disc are magnified
|
||||
* relative to farther ones. Together they turn the ring into an egg-shaped
|
||||
* oval — wider and taller at the bottom — and give the rose's points their
|
||||
* foreshortening for free.
|
||||
*/
|
||||
const TILT_SQUASH = 0.42;
|
||||
const TILT_PERSPECTIVE = 0.22;
|
||||
/** Screen height of the projected ring, per unit of radius. */
|
||||
const RING_HEIGHT_PER_RADIUS =
|
||||
TILT_SQUASH * (1 / (1 + TILT_PERSPECTIVE) + 1 / (1 - TILT_PERSPECTIVE));
|
||||
/** Screen half-width of the projected ring, per unit of radius. */
|
||||
const RING_HALF_WIDTH_PER_RADIUS = 1 / Math.sqrt(1 - TILT_PERSPECTIVE * TILT_PERSPECTIVE);
|
||||
|
||||
/** Tip radii of the rose's long and short points, as a fraction of the ring. */
|
||||
const CARDINAL_TIP = 0.9;
|
||||
const INTERCARDINAL_TIP = 0.54;
|
||||
/** Radius the point bases sit on: how fat the rose is at its waist. */
|
||||
const POINT_BASE_RADIUS = 0.17;
|
||||
/** Half-angle of a point's base. 22.5° makes neighbouring bases just meet. */
|
||||
const POINT_BASE_HALF_ANGLE = 22.5;
|
||||
/**
|
||||
* A polar grid lying in the same plane as the disc, to give it somewhere to
|
||||
* sit and to fill the empty flanks either side of it. Rings are multiples of
|
||||
* the disc's radius; spokes are offset by half a step so none runs up the
|
||||
* middle into the heading tick.
|
||||
*/
|
||||
const PLANE_RINGS = [1.35, 1.8, 2.5, 3.6, 5.6];
|
||||
const PLANE_SPOKE_STEP = 30;
|
||||
/** Grid brightness at the disc's rim, and how fast it falls off with distance. */
|
||||
const PLANE_NEAR_VALUE = 78;
|
||||
const PLANE_FALLOFF = 0.55;
|
||||
/** Dimmer than this is left unpainted, so the grid ends by vanishing. */
|
||||
const PLANE_CUTOFF = 22;
|
||||
/** Height over which the grid fades out as it approaches the readout. */
|
||||
const PLANE_FADE_SPAN = 45;
|
||||
|
||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||
const TICK_HEIGHT = 7;
|
||||
const TICK_HALF_WIDTH = 4;
|
||||
/** Depth of the disc's side wall, as a fraction of the ring's radius. */
|
||||
const DISC_DEPTH = 0.1;
|
||||
/**
|
||||
* Rim angle at which the silhouette turns vertical. Between this and its
|
||||
* mirror the wall faces the wearer; outside it the rim curves back away, so a
|
||||
* wall drawn there would hook inward instead of ending on the vertical edge.
|
||||
*/
|
||||
const WALL_START_ANGLE = (Math.acos(-TILT_PERSPECTIVE) * 180) / Math.PI;
|
||||
/** Side-wall shading, from the edges of the silhouette to the nearest point. */
|
||||
const WALL_DARK = 20;
|
||||
const WALL_LIT = 62;
|
||||
|
||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||
export { TICK_HEIGHT };
|
||||
|
||||
/**
|
||||
* Fit a rose into a box: the largest radius whose projected ring plus disc
|
||||
* wall fits between `topClearance` (the bottom of whatever text sits above)
|
||||
* and the box's bottom padding, and whose width clears the side padding
|
||||
* around `cx`. Returns the centre and radius to hand to drawCompassRose.
|
||||
*/
|
||||
export function layoutCompassRose(options: {
|
||||
width: number;
|
||||
height: number;
|
||||
cx: number;
|
||||
/** Lowest y already used above the rose (readout text, its gap). */
|
||||
topClearance: number;
|
||||
bottomPad: number;
|
||||
edgePad: number;
|
||||
maxRadius: number;
|
||||
minRadius?: number;
|
||||
}): { cx: number; cy: number; radius: number; ringTop: number } {
|
||||
const { width, height, cx } = options;
|
||||
const radius = Math.max(
|
||||
options.minRadius ?? 24,
|
||||
Math.min(
|
||||
options.maxRadius,
|
||||
Math.floor((height - options.bottomPad - options.topClearance - TICK_HEIGHT) / (RING_HEIGHT_PER_RADIUS + DISC_DEPTH)),
|
||||
Math.floor((Math.min(cx, width - cx) - options.edgePad) / RING_HALF_WIDTH_PER_RADIUS),
|
||||
),
|
||||
);
|
||||
// The wall hangs below the rim, so the near rim sits a wall's depth up
|
||||
// from the bottom padding rather than on it.
|
||||
const cy = height - options.bottomPad - discDepth(radius) - (radius * TILT_SQUASH) / (1 - TILT_PERSPECTIVE);
|
||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||
return { cx, cy, radius, ringTop };
|
||||
}
|
||||
|
||||
export function cardinalDirection(heading: number): string {
|
||||
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||
}
|
||||
|
||||
interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Project a point of the rose's plane onto the screen. `angle` is degrees
|
||||
* clockwise from the far side of the disc — the direction the wearer faces —
|
||||
* and `radius` is a fraction of the rose's outer ring.
|
||||
*/
|
||||
function projectRose(cx: number, cy: number, scale: number, radius: number, angle: number): Point {
|
||||
const radians = (angle * Math.PI) / 180;
|
||||
const across = radius * Math.sin(radians);
|
||||
const away = radius * Math.cos(radians);
|
||||
const depth = 1 + TILT_PERSPECTIVE * away;
|
||||
return {
|
||||
x: cx + (across * scale) / depth,
|
||||
y: cy - (away * scale * TILT_SQUASH) / depth,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Bounds of every orientation of the rotating points. The longest tip sweeps
|
||||
* a circle in the disc's plane; perspective projects it to an offset ellipse.
|
||||
* Round outward so all rasterized triangle pixels fit inside the rectangle.
|
||||
*/
|
||||
function rotatingRoseBounds(cx: number, cy: number, radius: number): {
|
||||
x: number; y: number; width: number; height: number;
|
||||
} {
|
||||
const perspective = TILT_PERSPECTIVE * CARDINAL_TIP;
|
||||
const halfWidth = radius * CARDINAL_TIP / Math.sqrt(1 - perspective * perspective);
|
||||
const depth = radius * CARDINAL_TIP * TILT_SQUASH;
|
||||
const x = Math.floor(cx - halfWidth);
|
||||
const y = Math.floor(cy - depth / (1 + perspective));
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
width: Math.ceil(cx + halfWidth) - x,
|
||||
height: Math.ceil(cy + depth / (1 - perspective)) - y,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the fixed pixels inside the rotating rose's dirty rectangle as one
|
||||
* immutable texture. The wire planner can omit them from each raster delta
|
||||
* and restore them with a cached image draw. Outside that rectangle the
|
||||
* background stays raster, since heading updates do not change it.
|
||||
*
|
||||
* The texture uses transparent zero for the empty disc interior, so it can
|
||||
* overlay the rotating points. Its only ink over the points is the fixed
|
||||
* heading tick, which belongs on top of them.
|
||||
*/
|
||||
export function createCompassBackground(
|
||||
width: number, height: number, cx: number, cy: number, radius: number, clipY: number, fade: number,
|
||||
): GrayImage {
|
||||
const image = new GrayImage(width, height, 0);
|
||||
drawPlaneGrid(image, cx, cy, radius, clipY);
|
||||
drawDiscWall(image, cx, cy, radius, fade);
|
||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||
|
||||
const bounds = rotatingRoseBounds(cx, cy, radius);
|
||||
const left = Math.max(0, bounds.x);
|
||||
const top = Math.max(0, bounds.y);
|
||||
const right = Math.min(width, bounds.x + bounds.width);
|
||||
const bottom = Math.min(height, bounds.y + bounds.height);
|
||||
if (right > left && bottom > top) {
|
||||
const texture = new GrayImage(right - left, bottom - top, 0);
|
||||
texture.bitBlt(image, 0, 0, { sx: left, sy: top, width: texture.width, height: texture.height });
|
||||
image.fillRect(left, top, texture.width, texture.height, 0);
|
||||
image.drawImage(texture, left, top);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Draw only the rotating points, with the brightest one aimed at north. */
|
||||
export function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: number, heading: number | null): void {
|
||||
// Without a reading there is nothing to aim, so show the rose in its resting
|
||||
// orientation, dimmed, rather than an empty ring.
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const bearing = heading ?? 0;
|
||||
// Short points first, so the long ones sit on top of them at the waist. Only
|
||||
// north is drawn at full brightness: the other seven are there to make the
|
||||
// rose read as a rose, and competing with north would defeat the point.
|
||||
for (const degrees of [45, 135, 225, 315]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, INTERCARDINAL_TIP, 85 * fade, 40 * fade);
|
||||
}
|
||||
for (const degrees of [90, 180, 270]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, CARDINAL_TIP, 120 * fade, 55 * fade);
|
||||
}
|
||||
drawRosePoint(image, cx, cy, radius, -bearing, CARDINAL_TIP, 255 * fade, 140 * fade);
|
||||
}
|
||||
|
||||
/**
|
||||
* One point of the rose: a tapered spike split down its axis into a lit and a
|
||||
* shaded facet, which is what keeps the eight points readable once perspective
|
||||
* has squashed the ones lying across the wearer's line of sight.
|
||||
*/
|
||||
function drawRosePoint(
|
||||
image: GrayImage,
|
||||
cx: number,
|
||||
cy: number,
|
||||
radius: number,
|
||||
angle: number,
|
||||
tipRadius: number,
|
||||
litValue: number,
|
||||
shadedValue: number,
|
||||
): void {
|
||||
const centre = { x: cx, y: cy };
|
||||
const tip = projectRose(cx, cy, radius, tipRadius, angle);
|
||||
const clockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle + POINT_BASE_HALF_ANGLE);
|
||||
const anticlockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle - POINT_BASE_HALF_ANGLE);
|
||||
fillTriangle(image, tip, centre, clockwise, litValue);
|
||||
fillTriangle(image, tip, centre, anticlockwise, shadedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* The side wall below the wearer-facing arc of the rim, which turns the tilted
|
||||
* disc into a shallow cylinder. The arc runs between the two points where the
|
||||
* silhouette turns vertical, so the wall ends flush with that vertical edge,
|
||||
* and it is shaded from dark there to lit where it faces the wearer head-on.
|
||||
*/
|
||||
function drawDiscWall(image: GrayImage, cx: number, cy: number, radius: number, fade: number): void {
|
||||
const depth = discDepth(radius);
|
||||
const segments = 48;
|
||||
const sweep = 360 - WALL_START_ANGLE * 2;
|
||||
const rimValue = 105 * fade;
|
||||
let previous = projectRose(cx, cy, radius, 1, WALL_START_ANGLE);
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const angle = WALL_START_ANGLE + (i * sweep) / segments;
|
||||
const point = projectRose(cx, cy, radius, 1, angle);
|
||||
// Quads rather than per-column lines, so no gap opens up where the rim
|
||||
// runs nearly horizontal across the bottom of the oval.
|
||||
const value = (WALL_DARK + (WALL_LIT - WALL_DARK) * wallFacing(angle)) * fade;
|
||||
const top = { x: previous.x, y: previous.y };
|
||||
const next = { x: point.x, y: point.y };
|
||||
fillTriangle(image, top, next, { x: next.x, y: next.y + depth }, value);
|
||||
fillTriangle(image, top, { x: top.x, y: top.y + depth }, { x: next.x, y: next.y + depth }, value);
|
||||
image.drawLine(top.x, top.y + depth, next.x, next.y + depth, rimValue);
|
||||
previous = point;
|
||||
}
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
}
|
||||
|
||||
/** 1 where the wall faces the wearer head-on, 0 at the silhouette's edges. */
|
||||
function wallFacing(angle: number): number {
|
||||
const towardWearer = -Math.cos((angle * Math.PI) / 180);
|
||||
return (towardWearer - TILT_PERSPECTIVE) / (1 - TILT_PERSPECTIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The grid the disc sits on, drawn before the disc so the disc occludes it.
|
||||
* Brightness falls off with distance and fades to nothing as the plane climbs
|
||||
* towards `clipY`, so the grid dissolves into the black under the readout
|
||||
* rather than stopping at a hard edge.
|
||||
*/
|
||||
function drawPlaneGrid(image: GrayImage, cx: number, cy: number, radius: number, clipY: number): void {
|
||||
const shade = (point: Point, ringRadius: number): number => {
|
||||
if (point.y > image.height || Math.abs(point.x - cx) > image.width) return 0;
|
||||
const fade = Math.max(0, Math.min(1, (point.y - clipY) / PLANE_FADE_SPAN));
|
||||
const value = (PLANE_NEAR_VALUE / (1 + PLANE_FALLOFF * (ringRadius - 1))) * fade;
|
||||
return value >= PLANE_CUTOFF ? value : 0;
|
||||
};
|
||||
const trace = (
|
||||
steps: number,
|
||||
at: (step: number) => { point: Point; ringRadius: number },
|
||||
): void => {
|
||||
let previous: Point | null = null;
|
||||
let previousValue = 0;
|
||||
for (let i = 0; i <= steps; i++) {
|
||||
const { point, ringRadius } = at(i);
|
||||
const value = shade(point, ringRadius);
|
||||
if (previous !== null && previousValue > 0 && value > 0) {
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, (previousValue + value) / 2);
|
||||
}
|
||||
previous = point;
|
||||
previousValue = value;
|
||||
}
|
||||
};
|
||||
|
||||
for (const ringRadius of PLANE_RINGS) {
|
||||
trace(96, (step) => ({ point: projectRose(cx, cy, radius, ringRadius, (step * 360) / 96), ringRadius }));
|
||||
}
|
||||
// Spokes run from just outside the disc to the outermost ring, so the grid
|
||||
// closes on itself instead of trailing off into loose ends.
|
||||
const outer = PLANE_RINGS[PLANE_RINGS.length - 1]!;
|
||||
for (let angle = PLANE_SPOKE_STEP / 2; angle < 360; angle += PLANE_SPOKE_STEP) {
|
||||
trace(24, (step) => {
|
||||
const ringRadius = 1.05 + (step * (outer - 1.05)) / 24;
|
||||
return { point: projectRose(cx, cy, radius, ringRadius, angle), ringRadius };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Screen depth of the disc's side wall at the given ring radius. */
|
||||
function discDepth(radius: number): number {
|
||||
return Math.max(3, Math.round(radius * DISC_DEPTH));
|
||||
}
|
||||
|
||||
/** The ring bounding the rose: the tilted disc's rim, drawn as a polygon. */
|
||||
function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const segments = 72;
|
||||
let previous = projectRose(cx, cy, radius, 1, 0);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const point = projectRose(cx, cy, radius, 1, (i * 360) / segments);
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, value);
|
||||
previous = point;
|
||||
}
|
||||
}
|
||||
|
||||
/** A fixed mark just inside the top rim: the heading the wearer is facing. */
|
||||
function drawHeadingTick(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const top = projectRose(cx, cy, radius, 1, 0);
|
||||
const tickTop = top.y + 1;
|
||||
fillTriangle(
|
||||
image,
|
||||
{ x: cx, y: tickTop + TICK_HEIGHT - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
function fillTriangle(image: GrayImage, a: Point, b: Point, c: Point, value: number): void {
|
||||
const left = Math.max(0, Math.floor(Math.min(a.x, b.x, c.x)));
|
||||
const right = Math.min(image.width - 1, Math.ceil(Math.max(a.x, b.x, c.x)));
|
||||
const top = Math.max(0, Math.floor(Math.min(a.y, b.y, c.y)));
|
||||
const bottom = Math.min(image.height - 1, Math.ceil(Math.max(a.y, b.y, c.y)));
|
||||
const shade = Math.round(value);
|
||||
for (let y = top; y <= bottom; y++) {
|
||||
for (let x = left; x <= right; x++) {
|
||||
const point = { x: x + 0.5, y: y + 0.5 };
|
||||
const ab = edge(a, b, point);
|
||||
const bc = edge(b, c, point);
|
||||
const ca = edge(c, a, point);
|
||||
// Accept either winding, so callers don't have to order their vertices.
|
||||
if ((ab >= 0 && bc >= 0 && ca >= 0) || (ab <= 0 && bc <= 0 && ca <= 0)) {
|
||||
image.setPixel(x, y, shade);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Twice the signed area of the triangle a-b-p; its sign is p's side of a-b. */
|
||||
function edge(a: Point, b: Point, p: Point): number {
|
||||
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
cancelBandwidthBenchmark,
|
||||
getBandwidthBenchmarkStatus,
|
||||
startBandwidthBenchmark,
|
||||
type BandwidthBenchmarkStatus,
|
||||
} from "../../native/bandwidth-benchmark";
|
||||
|
||||
const MESSAGE_SIZES = [250, 500, 1000, 2000, 3800];
|
||||
const WINDOW_SIZES = [1, 2, 3, 4, 6];
|
||||
const LINK_MODES = ["Current", "Request HIGH", "Request 2M", "HIGH + 2M"];
|
||||
const DURATION_MS = 15_000;
|
||||
// After painting the "running" screen, wait for that frame to reach the
|
||||
// glasses before the benchmark blocks normal image sends — otherwise the
|
||||
// status frame would sit queued behind the whole 15-second run.
|
||||
const ARM_DELAY_MS = 800;
|
||||
const POLL_INTERVAL_MS = 500;
|
||||
// Give up polling if the run somehow never reports done (duration + ack
|
||||
// timeout drain + slack).
|
||||
const POLL_DEADLINE_MS = DURATION_MS + 15_000;
|
||||
|
||||
const HEADER_HEIGHT = 30;
|
||||
const LIST_X = 20;
|
||||
const VALUE_X = 150;
|
||||
|
||||
type Phase = "config" | "arming" | "running" | "done";
|
||||
|
||||
/**
|
||||
* BLE bandwidth benchmark: pick a message size and pipeline window, stream
|
||||
* no-op image payloads for 15 seconds, and report throughput. The glasses
|
||||
* screen is intentionally static while the run is active — the benchmark
|
||||
* stream is the only image traffic, so the numbers are clean.
|
||||
*/
|
||||
export class BandwidthBenchmarkLayer implements Layer {
|
||||
private phase: Phase = "config";
|
||||
private selectedRow = 0; // size, window, link preference, start
|
||||
private sizeIndex = 2;
|
||||
private windowIndex = 2;
|
||||
private linkModeIndex = 0;
|
||||
private result: BandwidthBenchmarkStatus | null = null;
|
||||
private error: string | null = null;
|
||||
private armTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private pollStartedAtMs = 0;
|
||||
|
||||
constructor(private readonly requestRender: () => void) {}
|
||||
|
||||
onRemoved(): void {
|
||||
this.clearTimers();
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
cancelBandwidthBenchmark();
|
||||
}
|
||||
}
|
||||
|
||||
private clearTimers(): void {
|
||||
if (this.armTimer !== null) {
|
||||
clearTimeout(this.armTimer);
|
||||
this.armTimer = null;
|
||||
}
|
||||
if (this.pollTimer !== null) {
|
||||
clearInterval(this.pollTimer);
|
||||
this.pollTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private startRun(): void {
|
||||
this.result = null;
|
||||
this.error = null;
|
||||
this.phase = "arming";
|
||||
this.requestRender();
|
||||
this.armTimer = setTimeout(() => {
|
||||
this.armTimer = null;
|
||||
const started = startBandwidthBenchmark(
|
||||
MESSAGE_SIZES[this.sizeIndex]!,
|
||||
WINDOW_SIZES[this.windowIndex]!,
|
||||
DURATION_MS,
|
||||
this.linkModeIndex,
|
||||
);
|
||||
if (!started) {
|
||||
this.phase = "config";
|
||||
this.error = "Could not start (not connected?)";
|
||||
this.requestRender();
|
||||
return;
|
||||
}
|
||||
this.phase = "running";
|
||||
this.pollStartedAtMs = Date.now();
|
||||
// No renders while polling: a repaint would queue an image update that
|
||||
// stays blocked behind the benchmark anyway. Render once on completion.
|
||||
this.pollTimer = setInterval(() => this.poll(), POLL_INTERVAL_MS);
|
||||
}, ARM_DELAY_MS);
|
||||
}
|
||||
|
||||
private poll(): void {
|
||||
const status = getBandwidthBenchmarkStatus();
|
||||
if (status && (status.state === "starting" || status.state === "running")) {
|
||||
if (Date.now() - this.pollStartedAtMs < POLL_DEADLINE_MS) return;
|
||||
cancelBandwidthBenchmark();
|
||||
return; // next poll picks up the cancelled run's "done"
|
||||
}
|
||||
this.clearTimers();
|
||||
if (status && status.state === "done") {
|
||||
this.result = status;
|
||||
this.phase = "done";
|
||||
} else {
|
||||
this.phase = "config";
|
||||
this.error = "Benchmark lost (disconnected?)";
|
||||
}
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
|
||||
image.drawText(font, 20, 8, "BLE bandwidth", 220);
|
||||
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
image.drawText(font, LIST_X, HEADER_HEIGHT + 8, "Running: streaming no-op packets for 15 s...", 200);
|
||||
image.drawText(
|
||||
font,
|
||||
LIST_X,
|
||||
HEADER_HEIGHT + 30,
|
||||
`${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}, ${LINK_MODES[this.linkModeIndex]}`,
|
||||
150,
|
||||
);
|
||||
image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
const rowH = tightRowHeight(font) + 4;
|
||||
const rows: Array<[string, string]> = [
|
||||
["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`],
|
||||
["Window size", `${WINDOW_SIZES[this.windowIndex]}`],
|
||||
["Link request", LINK_MODES[this.linkModeIndex]!],
|
||||
[this.phase === "done" ? "Run again" : "Start", ""],
|
||||
];
|
||||
for (let index = 0; index < rows.length; index++) {
|
||||
const [label, value] = rows[index]!;
|
||||
const y = HEADER_HEIGHT + index * rowH;
|
||||
const selected = index === this.selectedRow;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 2, label, selected ? 255 : 200);
|
||||
if (value) {
|
||||
image.drawText(font, VALUE_X, y + 2, value, selected ? 235 : 160);
|
||||
}
|
||||
}
|
||||
|
||||
let y = HEADER_HEIGHT + rows.length * rowH + 10;
|
||||
if (this.error) {
|
||||
image.drawText(font, LIST_X, y, this.error, 180);
|
||||
} else if (this.result) {
|
||||
const r = this.result;
|
||||
const seconds = r.elapsedMs / 1000;
|
||||
const payloadRate = seconds > 0 ? r.payloadBytesAcked / seconds : 0;
|
||||
const wireRate = seconds > 0 ? r.wireBytesAcked / seconds : 0;
|
||||
const lines = [
|
||||
`Throughput: ${(payloadRate / 1024).toFixed(2)} KB/s payload (${(wireRate / 1024).toFixed(2)} KB/s wire)`,
|
||||
`Messages: ${r.messagesAcked}/${r.messagesSent} acked, ${r.timeouts} timeouts`,
|
||||
`Elapsed: ${seconds.toFixed(1)} s` + (r.aborted ? " (aborted early)" : ""),
|
||||
`Link request: ${LINK_MODES[r.linkMode] ?? "Current"}`,
|
||||
];
|
||||
for (const line of lines) {
|
||||
image.drawText(font, LIST_X, y, line, 190);
|
||||
y += 20;
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
// A static screen during the run: only allow bailing out.
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedRow = Math.max(0, this.selectedRow - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedRow = Math.min(3, this.selectedRow + 1);
|
||||
return;
|
||||
case "click":
|
||||
if (this.selectedRow === 0) {
|
||||
this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length;
|
||||
} else if (this.selectedRow === 1) {
|
||||
this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length;
|
||||
} else if (this.selectedRow === 2) {
|
||||
this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length;
|
||||
} else {
|
||||
this.startRun();
|
||||
}
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { MenuLayer, drawSubmenuIndicator, type MenuItem } from "../../ui/menu";
|
||||
import { ScreenTestLayer } from "./screen-test";
|
||||
import { BuzzerDemoLayer } from "./buzzer-demo";
|
||||
import { AccelerometerDemoLayer } from "./accelerometer-demo";
|
||||
import { BandwidthBenchmarkLayer } from "./bandwidth-benchmark";
|
||||
import { LightSensorDemoLayer } from "./light-sensor-demo";
|
||||
import { ResourceUsageLayer } from "./resource-usage";
|
||||
import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app";
|
||||
@@ -80,6 +81,12 @@ function debugTestsMenu(): MenuLayer {
|
||||
ctx.stack.push(new LightSensorDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "BLE bandwidth",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new BandwidthBenchmarkLayer(ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
],
|
||||
MENU_LAYOUT,
|
||||
);
|
||||
|
||||
@@ -113,8 +113,7 @@ export class LightSensorDemoLayer implements Layer {
|
||||
if (!report) {
|
||||
const message = this.enabled ? "Waiting for a report…" : "Paused (page not visible).";
|
||||
image.drawText(small, 22, 44, message, 190);
|
||||
image.drawText(small, 22, 66, "Needs custom firmware with the als16", 120);
|
||||
image.drawText(small, 22, 82, "extension (EVENCFW/18 or newer).", 120);
|
||||
image.drawText(small, 22, 66, "Needs Faceclaw's custom firmware.", 120);
|
||||
image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,777 @@
|
||||
/**
|
||||
* Flappy app worker: a Flappy Bird-style side scroller. One singleton
|
||||
* window; the bird sits a third of the way across, pipes scroll in from the
|
||||
* right, a flap counters gravity, and the score is one point per pipe pair
|
||||
* cleared.
|
||||
*
|
||||
* The BLE pipeline runs ~250 ms input-to-pixels and a handful of fps, so the
|
||||
* game is tuned latency-tolerant: slow scroll, gentle gravity, a wide gap,
|
||||
* and a forgiving hitbox. Physics runs at a 60 Hz fixed step inside a ~12 fps
|
||||
* render tick.
|
||||
*
|
||||
* Bandwidth: everything that moves is a sprite placed with drawImage, so the
|
||||
* wire pipeline can ship it as an on-glasses cached-image draw (a 9-byte
|
||||
* record) rather than pixels. The bird is pre-rendered at a few tilt angles
|
||||
* and wing positions; pipes are a cap plus vertically uniform body tiles;
|
||||
* the ground is one tile repeated. Sprites hanging off the top or left edge
|
||||
* fall back to raster (the planner needs a non-negative origin), which is
|
||||
* small and rare.
|
||||
*
|
||||
* Controls (a watch swipe in either direction is the primary flap):
|
||||
* swipe-up, swipe-down, click, or scroll-up flaps; double-click pauses;
|
||||
* tap-then-hold opens the window menu. Ready / paused / game over: click or
|
||||
* a swipe starts or resumes, double-click yields focus. Losing input focus
|
||||
* mid-flight (a shell overlay such as the system menu or a notification,
|
||||
* focus to the sidebar) pauses, as does backgrounding or screen-off.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, 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 { getActiveDisplay } from "../../native/active-display";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
type InputEvent,
|
||||
} from "../../ui/gestures";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import {
|
||||
BIRD_ANGLES,
|
||||
BIRD_SPRITE_H,
|
||||
BIRD_SPRITE_W,
|
||||
BIRD_WING_FRAMES,
|
||||
createBirdSprite,
|
||||
createGroundTile,
|
||||
createPipeBodyTile,
|
||||
createPipeCap,
|
||||
GROUND_H,
|
||||
GROUND_TILE_W,
|
||||
PIPE_BODY_TILE_H,
|
||||
PIPE_CAP_H,
|
||||
PIPE_CAP_OVERHANG,
|
||||
PIPE_CAP_W,
|
||||
PIPE_W,
|
||||
} from "./flappy-sprites";
|
||||
|
||||
declare const global: any;
|
||||
declare const com: any;
|
||||
|
||||
const largeFont = getFont("terminus32");
|
||||
const mediumFont = getFont("terminus24");
|
||||
const smallFont = getDefaultSmallFont();
|
||||
|
||||
const HIGH_SCORE_KEY = "flappy.highScore";
|
||||
|
||||
// --- Tuning (pixels, seconds) ------------------------------------------------
|
||||
/** Bird center x; fixed, the world scrolls past it. */
|
||||
const BIRD_X = 150;
|
||||
/** Gentle on purpose: a flap-to-flap cycle is well over the glasses round trip. */
|
||||
const GRAVITY = 520;
|
||||
const FLAP_VY = -215;
|
||||
const MAX_FALL_SPEED = 330;
|
||||
const SCROLL_SPEED = 76;
|
||||
const GAP_H = 104;
|
||||
const PIPE_SPACING = 236;
|
||||
/** Least body height either pipe of a pair keeps, so gaps never hug an edge. */
|
||||
const MIN_PIPE_BODY = 30;
|
||||
/** Forgiving hitbox half-extents around the bird center. */
|
||||
const HITBOX_HALF_W = 9;
|
||||
const HITBOX_HALF_H = 7;
|
||||
const PHYSICS_DT = 1 / 60;
|
||||
const RENDER_TICK_MS = 30;
|
||||
/** Physics steps per wing frame while flying. */
|
||||
const WING_STEPS_PER_FRAME = 6;
|
||||
|
||||
const SFX_FLAP: Step[] = [{ freq: 880, duty: 40, ms: 20 }, { freq: 1175, duty: 40, ms: 25 }];
|
||||
const SFX_SCORE: Step[] = [{ freq: 1568, duty: 40, ms: 40 }, { freq: 2093, duty: 40, ms: 60 }];
|
||||
const SFX_HIT: Step[] = [{ freq: 220, duty: 55, ms: 80 }, { freq: 165, duty: 55, ms: 120 }];
|
||||
const SFX_GAME_OVER: Step[] = [
|
||||
{ freq: 494, duty: 45, ms: 150 },
|
||||
{ freq: 440, duty: 45, ms: 150 },
|
||||
{ freq: 392, duty: 45, ms: 150 },
|
||||
{ freq: 294, duty: 50, ms: 320 },
|
||||
];
|
||||
const SFX_PAUSE: Step[] = [
|
||||
{ freq: 1319, duty: 40, ms: 60 },
|
||||
{ freq: 880, duty: 40, ms: 110 },
|
||||
];
|
||||
const SFX_RESUME: Step[] = [
|
||||
{ freq: 880, duty: 40, ms: 60 },
|
||||
{ freq: 1319, duty: 40, ms: 110 },
|
||||
];
|
||||
const MINOR_SFX_MIN_GAP_MS = 120;
|
||||
|
||||
/**
|
||||
* ready: bird hovering, waiting for the first flap. playing: scrolling.
|
||||
* dying: hit something, bird tumbling to the ground with the world frozen.
|
||||
* game-over / paused: static screens with a dialog.
|
||||
*/
|
||||
type GamePhase = "ready" | "playing" | "dying" | "game-over" | "paused";
|
||||
|
||||
type Pipe = {
|
||||
/** Body left edge in viewport coordinates. */
|
||||
x: number;
|
||||
/** Bottom of the top pipe's cap; the gap runs from here to gapTop + GAP_H. */
|
||||
gapTop: number;
|
||||
scored: boolean;
|
||||
};
|
||||
|
||||
type FlappyWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
/** Whether this window is the shell's input target (pushed with each message). */
|
||||
focused: boolean;
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
birdY: number;
|
||||
birdVy: number;
|
||||
/** Physics steps since the game started; drives the wing animation. */
|
||||
stepCount: number;
|
||||
/** World scroll distance so far, for the ground tiling phase. */
|
||||
scrollX: number;
|
||||
pipes: Pipe[];
|
||||
score: number;
|
||||
highScore: number;
|
||||
tickTimer: ReturnType<typeof setInterval> | null;
|
||||
lastTickAtMs: number;
|
||||
lastMinorSfxAtMs: number;
|
||||
soundOn: boolean;
|
||||
lastSubmittedFingerprint: string;
|
||||
};
|
||||
|
||||
const windows = new Map<string, FlappyWindow>();
|
||||
let screenOn = true;
|
||||
|
||||
// --- Sprites (immutable, built once per worker) -----------------------------
|
||||
|
||||
const pipeBodyTile = createPipeBodyTile();
|
||||
const pipeCap = createPipeCap();
|
||||
const groundTile = createGroundTile();
|
||||
/** [angleIndex][wingFrame] */
|
||||
const birdSprites: GrayImage[][] = BIRD_ANGLES.map((angle) =>
|
||||
Array.from({ length: BIRD_WING_FRAMES }, (_, frame) => createBirdSprite(angle, frame)),
|
||||
);
|
||||
|
||||
function post(message: WorkerAppReply): void {
|
||||
global.postMessage(message);
|
||||
}
|
||||
|
||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||
// evaluation is synchronous, so the handler below is installed before any
|
||||
// queued message can be delivered.
|
||||
post({ type: "worker-ready" });
|
||||
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "open-window": {
|
||||
const window: FlappyWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
focused: false,
|
||||
menu: null,
|
||||
phase: "ready",
|
||||
birdY: 0,
|
||||
birdVy: 0,
|
||||
stepCount: 0,
|
||||
scrollX: 0,
|
||||
pipes: [],
|
||||
score: 0,
|
||||
highScore: loadHighScore(),
|
||||
tickTimer: null,
|
||||
lastTickAtMs: 0,
|
||||
lastMinorSfxAtMs: 0,
|
||||
soundOn: loadSoundEnabled("flappy"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
resetGame(window);
|
||||
windows.set(message.windowId, window);
|
||||
break;
|
||||
}
|
||||
case "resize-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.viewportWidth = message.viewport.width;
|
||||
window.viewportHeight = message.viewport.height;
|
||||
window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight });
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "close-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (window?.tickTimer) clearInterval(window.tickTimer);
|
||||
windows.delete(message.windowId);
|
||||
break;
|
||||
}
|
||||
case "input": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) {
|
||||
frameTimings.finishFrame(message.frameId, "discarded: unknown flappy window");
|
||||
break;
|
||||
}
|
||||
window.focused = message.focused;
|
||||
inferForeground(window, message.focused);
|
||||
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
|
||||
handleInput(window, message.event as InputEvent, message.frameId);
|
||||
break;
|
||||
}
|
||||
case "render": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.focused = message.focused;
|
||||
inferForeground(window, message.focused);
|
||||
renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "foreground": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.foreground = message.foreground;
|
||||
window.focused = message.focused;
|
||||
// The player can't see the bird while backgrounded, so don't let it
|
||||
// keep falling.
|
||||
if (!window.foreground && window.phase === "playing") window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away (the system menu, a notification
|
||||
// modal, the voice dialog, focus back to the sidebar) pauses the
|
||||
// flight; a death tumble is allowed to finish.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
if (!screenOn && window.phase === "playing") window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Input and render messages only ever target the shell's foreground window,
|
||||
* so a focused message proves this window is foreground. This backstops the
|
||||
* "foreground" message itself, which can be lost while a freshly spawned
|
||||
* worker is still evaluating its bundle (see the Blocks worker).
|
||||
*/
|
||||
function inferForeground(window: FlappyWindow, focused: boolean): void {
|
||||
if (!focused || window.foreground) return;
|
||||
window.foreground = true;
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
function loadHighScore(): number {
|
||||
try {
|
||||
const parsed = parseInt(getStringSetting(HIGH_SCORE_KEY, "0"), 10);
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function saveHighScore(window: FlappyWindow): void {
|
||||
if (window.score <= window.highScore) return;
|
||||
window.highScore = window.score;
|
||||
try {
|
||||
setStringSetting(HIGH_SCORE_KEY, String(window.highScore));
|
||||
} catch (error) {
|
||||
console.warn(`flappy high-score save failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
|
||||
* steps on its own timer, and the Java call is safe from the worker thread.
|
||||
* Minor (frequent) effects are dropped when they'd arrive within the
|
||||
* rate-limit gap; newsworthy ones always play.
|
||||
*/
|
||||
function playSfx(window: FlappyWindow, steps: Step[], minor = false): void {
|
||||
if (!window.soundOn || steps.length === 0) return;
|
||||
const now = Date.now();
|
||||
if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return;
|
||||
if (minor) window.lastMinorSfxAtMs = now;
|
||||
try {
|
||||
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
if (!communicator) return;
|
||||
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
|
||||
} catch (error) {
|
||||
console.warn(`flappy sfx failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
function windowMenuItems(window: FlappyWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: "New game",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
resetGame(window);
|
||||
playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: window.soundOn ? "Sound: on" : "Sound: off",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("flappy", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: FlappyWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => windowMenuItems(window),
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
});
|
||||
}
|
||||
return window.menu;
|
||||
}
|
||||
|
||||
// --- Input ------------------------------------------------------------------
|
||||
|
||||
function handleInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
|
||||
// An open window menu owns all input (it closes itself via pop); menus are
|
||||
// list UIs, so watch swipes take their standard fallback meanings there.
|
||||
if (window.menu?.isOpen()) {
|
||||
window.menu
|
||||
.handleInput(directionalFallback(event))
|
||||
.catch((error) => console.error(`flappy menu input failed: ${error}`))
|
||||
.then(() => renderAndSubmit(window, frameId));
|
||||
return;
|
||||
}
|
||||
|
||||
// Opening the menu mid-flight would leave the bird falling under it.
|
||||
if (event.type === "short-then-long-press") {
|
||||
if (window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
}
|
||||
windowMenu(window).open();
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (window.phase) {
|
||||
case "ready":
|
||||
case "playing":
|
||||
handleFlightInput(window, event, frameId);
|
||||
break;
|
||||
case "dying":
|
||||
// Let the tumble finish; a flap mashed during it must not restart.
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy ignored input while dying");
|
||||
break;
|
||||
default:
|
||||
handleIdleInput(window, event, frameId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Input while ready or flying: the flap gestures, plus pause. */
|
||||
function handleFlightInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
|
||||
switch (event.type) {
|
||||
// Either swipe direction flaps: on the watch the flap is a reflex, and
|
||||
// which way the thumb went shouldn't matter.
|
||||
case "swipe-up":
|
||||
case "swipe-down":
|
||||
case "click":
|
||||
case "scroll-up":
|
||||
flap(window);
|
||||
break;
|
||||
case "double-click":
|
||||
if (window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
playSfx(window, SFX_PAUSE);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy ignored input");
|
||||
return;
|
||||
}
|
||||
syncTickTimer(window);
|
||||
renderAndSubmit(window, frameId);
|
||||
}
|
||||
|
||||
/** Input while paused or game over. Swipes take their standard fallback meanings. */
|
||||
function handleIdleInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
|
||||
switch (directionalFallback(event).type) {
|
||||
case "click":
|
||||
if (window.phase === "game-over") {
|
||||
resetGame(window);
|
||||
} else {
|
||||
window.phase = "playing";
|
||||
}
|
||||
syncTickTimer(window);
|
||||
playSfx(window, SFX_RESUME);
|
||||
break;
|
||||
case "double-click":
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy ignored input");
|
||||
return;
|
||||
}
|
||||
renderAndSubmit(window, frameId);
|
||||
}
|
||||
|
||||
function groundTop(window: FlappyWindow): number {
|
||||
return window.viewportHeight - GROUND_H;
|
||||
}
|
||||
|
||||
function resetGame(window: FlappyWindow): void {
|
||||
window.phase = "ready";
|
||||
window.birdY = Math.round(groundTop(window) * 0.42);
|
||||
window.birdVy = 0;
|
||||
window.stepCount = 0;
|
||||
window.scrollX = 0;
|
||||
window.pipes = [];
|
||||
window.score = 0;
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
function flap(window: FlappyWindow): void {
|
||||
if (window.phase === "ready") {
|
||||
window.phase = "playing";
|
||||
window.lastTickAtMs = Date.now();
|
||||
}
|
||||
window.birdVy = FLAP_VY;
|
||||
playSfx(window, SFX_FLAP, true);
|
||||
}
|
||||
|
||||
// --- Simulation -------------------------------------------------------------
|
||||
|
||||
function isAnimating(window: FlappyWindow): boolean {
|
||||
return window.phase === "playing" || window.phase === "dying";
|
||||
}
|
||||
|
||||
/** Keep the render tick running exactly while something is visibly moving. */
|
||||
function syncTickTimer(window: FlappyWindow): void {
|
||||
const shouldRun = window.foreground && screenOn && isAnimating(window);
|
||||
if (!shouldRun && window.tickTimer !== null) {
|
||||
clearInterval(window.tickTimer);
|
||||
window.tickTimer = null;
|
||||
}
|
||||
if (shouldRun && window.tickTimer === null) {
|
||||
window.lastTickAtMs = Date.now();
|
||||
window.tickTimer = setInterval(() => tick(window), RENDER_TICK_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/** One render tick: catch the fixed-step physics up to real time, repaint. */
|
||||
function tick(window: FlappyWindow): void {
|
||||
if (!isAnimating(window)) {
|
||||
syncTickTimer(window);
|
||||
return;
|
||||
}
|
||||
const now = Date.now();
|
||||
const elapsedMs = Math.min(now - window.lastTickAtMs, 300);
|
||||
window.lastTickAtMs = now;
|
||||
const steps = clamp(Math.round(elapsedMs / (PHYSICS_DT * 1000)), 1, 20);
|
||||
for (let i = 0; i < steps && isAnimating(window); i++) {
|
||||
stepPhysics(window);
|
||||
}
|
||||
renderAndSubmit(window, 0);
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
function stepPhysics(window: FlappyWindow): void {
|
||||
const dt = PHYSICS_DT;
|
||||
window.stepCount++;
|
||||
window.birdVy = Math.min(window.birdVy + GRAVITY * dt, MAX_FALL_SPEED);
|
||||
window.birdY += window.birdVy * dt;
|
||||
const floor = groundTop(window) - HITBOX_HALF_H;
|
||||
|
||||
if (window.phase === "dying") {
|
||||
if (window.birdY >= floor) {
|
||||
window.birdY = floor;
|
||||
finishGame(window);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Ceiling: stop rather than die, like the original.
|
||||
if (window.birdY < HITBOX_HALF_H) {
|
||||
window.birdY = HITBOX_HALF_H;
|
||||
window.birdVy = Math.max(0, window.birdVy);
|
||||
}
|
||||
|
||||
const advance = SCROLL_SPEED * dt;
|
||||
window.scrollX += advance;
|
||||
for (const pipe of window.pipes) pipe.x -= advance;
|
||||
if (window.pipes.length && window.pipes[0]!.x + PIPE_W + PIPE_CAP_OVERHANG < 0) {
|
||||
window.pipes.shift();
|
||||
}
|
||||
const last = window.pipes[window.pipes.length - 1];
|
||||
if (!last || last.x <= window.viewportWidth - PIPE_SPACING) {
|
||||
spawnPipe(window, last ? last.x + PIPE_SPACING : window.viewportWidth + 40);
|
||||
}
|
||||
|
||||
for (const pipe of window.pipes) {
|
||||
if (!pipe.scored && pipe.x + PIPE_W < BIRD_X - HITBOX_HALF_W) {
|
||||
pipe.scored = true;
|
||||
window.score++;
|
||||
playSfx(window, SFX_SCORE);
|
||||
}
|
||||
}
|
||||
|
||||
if (window.birdY >= floor) {
|
||||
window.birdY = floor;
|
||||
playSfx(window, SFX_HIT);
|
||||
finishGame(window);
|
||||
return;
|
||||
}
|
||||
if (window.pipes.some((pipe) => hitsPipe(window, pipe))) {
|
||||
window.phase = "dying";
|
||||
window.birdVy = Math.max(window.birdVy, 40);
|
||||
playSfx(window, SFX_HIT);
|
||||
}
|
||||
}
|
||||
|
||||
function spawnPipe(window: FlappyWindow, x: number): void {
|
||||
const minGapTop = PIPE_CAP_H + MIN_PIPE_BODY;
|
||||
const maxGapTop = groundTop(window) - GAP_H - PIPE_CAP_H - MIN_PIPE_BODY;
|
||||
const gapTop = Math.round(minGapTop + Math.random() * Math.max(0, maxGapTop - minGapTop));
|
||||
window.pipes.push({ x, gapTop, scored: false });
|
||||
}
|
||||
|
||||
function hitsPipe(window: FlappyWindow, pipe: Pipe): boolean {
|
||||
const left = BIRD_X - HITBOX_HALF_W;
|
||||
const right = BIRD_X + HITBOX_HALF_W;
|
||||
const top = window.birdY - HITBOX_HALF_H;
|
||||
const bottom = window.birdY + HITBOX_HALF_H;
|
||||
const capLeft = pipe.x - PIPE_CAP_OVERHANG;
|
||||
const capRight = capLeft + PIPE_CAP_W;
|
||||
const gapBottom = pipe.gapTop + GAP_H;
|
||||
// The cap is wider than the body, so test the whole pipe at cap width for
|
||||
// the cap rows and at body width elsewhere.
|
||||
const withinBody = right > pipe.x && left < pipe.x + PIPE_W;
|
||||
const withinCap = right > capLeft && left < capRight;
|
||||
if (withinCap) {
|
||||
if (bottom > pipe.gapTop - PIPE_CAP_H && top < pipe.gapTop) return true;
|
||||
if (bottom > gapBottom && top < gapBottom + PIPE_CAP_H) return true;
|
||||
}
|
||||
if (withinBody) {
|
||||
if (top < pipe.gapTop - PIPE_CAP_H) return true;
|
||||
if (bottom > gapBottom + PIPE_CAP_H) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function finishGame(window: FlappyWindow): void {
|
||||
window.phase = "game-over";
|
||||
saveHighScore(window);
|
||||
playSfx(window, SFX_GAME_OVER);
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
// --- Painting ---------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Place a sprite. A whole sprite with a non-negative origin goes through the
|
||||
* deferred-image path (texture cacheable); anything hanging off the top or
|
||||
* left edge, or vertically clipped, falls back to a raster blit, since
|
||||
* cached draws are whole-image only with an unsigned origin.
|
||||
*/
|
||||
function stamp(image: GrayImage, sprite: GrayImage, x: number, y: number, clipHeight?: number): void {
|
||||
const ix = Math.round(x);
|
||||
const iy = Math.round(y);
|
||||
if (ix >= 0 && iy >= 0 && (clipHeight === undefined || clipHeight >= sprite.height)) {
|
||||
image.drawImage(sprite, ix, iy);
|
||||
} else {
|
||||
image.bitBlt(sprite, ix, iy, { height: clipHeight, transparentZero: true });
|
||||
}
|
||||
}
|
||||
|
||||
function paint(window: FlappyWindow): Plane[] {
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: FlappyWindow): GrayImage {
|
||||
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
const ground = groundTop(window);
|
||||
|
||||
for (const pipe of window.pipes) paintPipe(image, pipe, ground);
|
||||
|
||||
// Ground: tiles phased by the scroll distance so the hatching moves.
|
||||
const groundPhase = -(Math.round(window.scrollX) % GROUND_TILE_W);
|
||||
for (let x = groundPhase; x < window.viewportWidth; x += GROUND_TILE_W) {
|
||||
stamp(image, groundTile, x, ground);
|
||||
}
|
||||
|
||||
// Bird last, so it sits over the pipes when they meet.
|
||||
const sprite = birdSprites[birdAngleIndex(window)]![birdWingFrame(window)]!;
|
||||
stamp(image, sprite, BIRD_X - BIRD_SPRITE_W / 2, window.birdY - BIRD_SPRITE_H / 2);
|
||||
|
||||
paintHud(image, window);
|
||||
return image;
|
||||
}
|
||||
|
||||
function paintPipe(image: GrayImage, pipe: Pipe, ground: number): void {
|
||||
const capX = pipe.x - PIPE_CAP_OVERHANG;
|
||||
// Top pipe: cap just above the gap, body tiles stacked upward from it. The
|
||||
// tile that would start above the top edge is clipped to raster.
|
||||
const topBodyBottom = pipe.gapTop - PIPE_CAP_H;
|
||||
for (let y = topBodyBottom - PIPE_BODY_TILE_H; y > -PIPE_BODY_TILE_H; y -= PIPE_BODY_TILE_H) {
|
||||
stamp(image, pipeBodyTile, pipe.x, y);
|
||||
}
|
||||
stamp(image, pipeCap, capX, topBodyBottom);
|
||||
|
||||
// Bottom pipe: cap just below the gap, body tiles stacked down to the
|
||||
// ground; the final partial tile is clipped to raster.
|
||||
const gapBottom = pipe.gapTop + GAP_H;
|
||||
stamp(image, pipeCap, capX, gapBottom);
|
||||
for (let y = gapBottom + PIPE_CAP_H; y < ground; y += PIPE_BODY_TILE_H) {
|
||||
stamp(image, pipeBodyTile, pipe.x, y, Math.min(PIPE_BODY_TILE_H, ground - y));
|
||||
}
|
||||
}
|
||||
|
||||
/** Nose-up while rising, nose-down the faster the fall; steepest during the death tumble. */
|
||||
function birdAngleIndex(window: FlappyWindow): number {
|
||||
if (window.phase === "ready") return 1;
|
||||
if (window.phase === "dying") return BIRD_ANGLES.length - 1;
|
||||
const target = window.birdVy < 0 ? -25 : (window.birdVy / MAX_FALL_SPEED) * 55;
|
||||
let best = 0;
|
||||
for (let i = 1; i < BIRD_ANGLES.length - 1; i++) {
|
||||
if (Math.abs(BIRD_ANGLES[i]! - target) < Math.abs(BIRD_ANGLES[best]! - target)) best = i;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
function birdWingFrame(window: FlappyWindow): number {
|
||||
if (window.phase !== "playing") return 1;
|
||||
// Up, mid, down, mid, ... so the wing beats rather than snaps.
|
||||
const cycle = [0, 1, 2, 1];
|
||||
return cycle[Math.floor(window.stepCount / WING_STEPS_PER_FRAME) % cycle.length]!;
|
||||
}
|
||||
|
||||
function paintHud(image: GrayImage, window: FlappyWindow): void {
|
||||
const width = window.viewportWidth;
|
||||
if (window.phase !== "ready") {
|
||||
drawCenteredIn(image, largeFont, 0, width, 10, String(window.score), 245);
|
||||
}
|
||||
switch (window.phase) {
|
||||
case "ready":
|
||||
drawCenteredIn(image, mediumFont, 0, width, 14, "FLAPPY", 245);
|
||||
if (window.highScore > 0) {
|
||||
drawCenteredIn(image, smallFont, 0, width, 44, `best ${window.highScore}`, 150);
|
||||
}
|
||||
drawCenteredIn(image, smallFont, 0, width, groundTop(window) - 26, `swipe / ${GESTURE_CLICK} flap`, 150);
|
||||
break;
|
||||
case "paused":
|
||||
paintDialog(image, window, "PAUSED", [`${GESTURE_CLICK} resume`, `${GESTURE_DOUBLE_CLICK} leave`]);
|
||||
break;
|
||||
case "game-over":
|
||||
paintDialog(image, window, "GAME OVER", [
|
||||
`score ${window.score} best ${window.highScore}`,
|
||||
`${GESTURE_CLICK} new game ${GESTURE_DOUBLE_CLICK} leave`,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function paintDialog(image: GrayImage, window: FlappyWindow, title: string, lines: string[]): void {
|
||||
const width = 260;
|
||||
const height = 46 + lines.length * 20;
|
||||
const x = Math.round((window.viewportWidth - width) / 2);
|
||||
const y = Math.round((groundTop(window) - height) / 2);
|
||||
image.fillRect(x, y, width, height, 0);
|
||||
image.drawRect(x + 3, y + 3, width - 6, height - 6, 150);
|
||||
drawCenteredIn(image, mediumFont, x, width, y + 12, title, 245);
|
||||
lines.forEach((line, i) => {
|
||||
drawCenteredIn(image, smallFont, x, width, y + 40 + i * 20, line, 150);
|
||||
});
|
||||
}
|
||||
|
||||
function drawCenteredIn(
|
||||
image: GrayImage,
|
||||
font: typeof smallFont,
|
||||
x: number,
|
||||
width: number,
|
||||
y: number,
|
||||
text: string,
|
||||
value: number,
|
||||
): void {
|
||||
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
|
||||
}
|
||||
|
||||
function renderAndSubmit(window: FlappyWindow, inputFrameId: number): void {
|
||||
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
|
||||
try {
|
||||
const paintStartedAtMs = Date.now();
|
||||
const planes = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => paint(window)),
|
||||
);
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const fingerprint = planesFingerprint(planes);
|
||||
if (fingerprint === window.lastSubmittedFingerprint) {
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy content unchanged");
|
||||
return;
|
||||
}
|
||||
const communicator = getActiveDisplay();
|
||||
if (!communicator) {
|
||||
frameTimings.finishFrame(frameId, "discarded: no active display");
|
||||
return;
|
||||
}
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
||||
);
|
||||
window.lastSubmittedFingerprint = fingerprint;
|
||||
} catch (error) {
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy render failed");
|
||||
console.error(`flappy worker render failed: ${error}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* Sprite assets for the Flappy game, built once per worker and never mutated
|
||||
* afterwards, so every frame can place them with GrayImage.drawImage and the
|
||||
* wire pipeline can ship them as on-glasses cached-image draws (see
|
||||
* graphics/glyph-wire.ts). A frame then costs a handful of 9-byte draw
|
||||
* records plus whatever raster is left, instead of re-sending the sprite
|
||||
* pixels every tick.
|
||||
*
|
||||
* Constraints the pipeline puts on the art (kept here so the worker doesn't
|
||||
* have to know):
|
||||
* - a cached image is at most 255x255, so pipes are built from short body
|
||||
* tiles under a cap rather than one tall image;
|
||||
* - the body tile is vertically uniform (stripes only), so overlapping two
|
||||
* tiles at any vertical offset writes identical pixels and both still
|
||||
* match the composite the planner checks against;
|
||||
* - value 0 is transparent (color key), so sprites use 1 for opaque black.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
|
||||
export const PIPE_W = 44;
|
||||
export const PIPE_CAP_W = 52;
|
||||
export const PIPE_CAP_H = 16;
|
||||
/** How far the cap overhangs the body on each side. */
|
||||
export const PIPE_CAP_OVERHANG = (PIPE_CAP_W - PIPE_W) / 2;
|
||||
export const PIPE_BODY_TILE_H = 32;
|
||||
|
||||
export const GROUND_TILE_W = 96;
|
||||
export const GROUND_H = 18;
|
||||
|
||||
export const BIRD_SPRITE_W = 30;
|
||||
export const BIRD_SPRITE_H = 26;
|
||||
/** Bird rotation angles the sprites are pre-rendered at, in degrees (positive = nose down). */
|
||||
export const BIRD_ANGLES = [-25, 0, 25, 50, 80] as const;
|
||||
export const BIRD_WING_FRAMES = 3;
|
||||
|
||||
const OPAQUE_BLACK = 1;
|
||||
|
||||
/** Vertical stripe shading across the pipe body: highlight left, shadow right. */
|
||||
function pipeColumnShade(col: number, width: number): number {
|
||||
if (col === 0) return 90;
|
||||
if (col <= 3) return 210;
|
||||
if (col >= width - 1) return 60;
|
||||
if (col >= width - 4) return 95;
|
||||
return 150;
|
||||
}
|
||||
|
||||
export function createPipeBodyTile(): GrayImage {
|
||||
const image = new GrayImage(PIPE_W, PIPE_BODY_TILE_H, 0);
|
||||
for (let col = 0; col < PIPE_W; col++) {
|
||||
image.fillRect(col, 0, 1, PIPE_BODY_TILE_H, pipeColumnShade(col, PIPE_W));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Vertically symmetric, so one tile serves as both the top and the bottom pipe's cap. */
|
||||
export function createPipeCap(): GrayImage {
|
||||
const image = new GrayImage(PIPE_CAP_W, PIPE_CAP_H, 0);
|
||||
for (let col = 0; col < PIPE_CAP_W; col++) {
|
||||
image.fillRect(col, 0, 1, PIPE_CAP_H, pipeColumnShade(col, PIPE_CAP_W));
|
||||
}
|
||||
image.fillRect(0, 0, PIPE_CAP_W, 2, 220);
|
||||
image.fillRect(0, PIPE_CAP_H - 2, PIPE_CAP_W, 2, 220);
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Ground strip: a bright surface line over diagonal hatching that tiles at GROUND_TILE_W. */
|
||||
export function createGroundTile(): GrayImage {
|
||||
const image = new GrayImage(GROUND_TILE_W, GROUND_H, 0);
|
||||
image.fillRect(0, 0, GROUND_TILE_W, 2, 200);
|
||||
const period = 24; // divides GROUND_TILE_W, so the hatching wraps seamlessly
|
||||
for (let y = 3; y < GROUND_H; y++) {
|
||||
for (let x = 0; x < GROUND_TILE_W; x++) {
|
||||
const phase = ((x + y) % period + period) % period;
|
||||
image.setPixel(x, y, phase < 10 ? 110 : 45);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* The bird, rendered by sampling shapes in bird-local space so any rotation
|
||||
* and wing position comes out of the same geometry. Local x points along the
|
||||
* beak, y down; the sprite is centered on the body center.
|
||||
*/
|
||||
export function createBirdSprite(angleDeg: number, wingFrame: number): GrayImage {
|
||||
const image = new GrayImage(BIRD_SPRITE_W, BIRD_SPRITE_H, 0);
|
||||
const angle = (angleDeg * Math.PI) / 180;
|
||||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
const cx = BIRD_SPRITE_W / 2;
|
||||
const cy = BIRD_SPRITE_H / 2;
|
||||
// Wing pivot sits behind the body center; the tip swings up/mid/down.
|
||||
const wingTipY = [-5, 0, 4][wingFrame % BIRD_WING_FRAMES]!;
|
||||
for (let py = 0; py < BIRD_SPRITE_H; py++) {
|
||||
for (let px = 0; px < BIRD_SPRITE_W; px++) {
|
||||
const sx = px + 0.5 - cx;
|
||||
const sy = py + 0.5 - cy;
|
||||
// Sprite space -> bird-local space (inverse rotation).
|
||||
const x = sx * cos + sy * sin;
|
||||
const y = -sx * sin + sy * cos;
|
||||
const value = birdShade(x, y, wingTipY);
|
||||
if (value > 0) image.setPixel(px, py, value);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Gray value of the bird at a bird-local point, or 0 outside it. */
|
||||
function birdShade(x: number, y: number, wingTipY: number): number {
|
||||
// Eye: white with a pupil, drawn first so it sits on top of the body.
|
||||
const eyeX = 4.5;
|
||||
const eyeY = -2.5;
|
||||
if (inEllipse(x - eyeX - 1.1, y - eyeY, 1.3, 1.3)) return OPAQUE_BLACK;
|
||||
if (inEllipse(x - eyeX, y - eyeY, 3.2, 3.0)) return 255;
|
||||
// Beak: a wedge off the front of the body.
|
||||
if (x >= 7 && x <= 14 && Math.abs(y - 1.5) <= 2.6 * (1 - (x - 7) / 8.5)) {
|
||||
return y < 1.5 ? 235 : 190;
|
||||
}
|
||||
// Wing: an angled lobe behind the center, swinging with the frame.
|
||||
const wx = x + 3;
|
||||
const wy = y - wingTipY * 0.5 - wingTipY * 0.35 * (wx / 6);
|
||||
if (inEllipse(wx, wy, 6.5, 3.2)) {
|
||||
return inEllipse(wx, wy, 5.0, 2.0) ? 120 : 175;
|
||||
}
|
||||
// Body: lighter belly, darker back, dark outline ring.
|
||||
if (inEllipse(x, y, 11, 8.5)) {
|
||||
if (!inEllipse(x, y, 10, 7.5)) return 110;
|
||||
return y > 2 ? 255 : 205;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function inEllipse(x: number, y: number, rx: number, ry: number): boolean {
|
||||
return (x * x) / (rx * rx) + (y * y) / (ry * ry) <= 1;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { launchWorkerAppWindow, type AppDefinition } from "../app-definition";
|
||||
|
||||
const flappyApp: AppDefinition = {
|
||||
appId: "flappy",
|
||||
title: "Flappy",
|
||||
icon: "bird",
|
||||
launch: (ctx) =>
|
||||
launchWorkerAppWindow(ctx, {
|
||||
createWorker: () => new Worker("./flappy-app.worker"),
|
||||
windowId: "flappy:main",
|
||||
title: "Flappy",
|
||||
iconLetter: "B",
|
||||
icon: "bird",
|
||||
acceptsDirectional: true,
|
||||
}),
|
||||
};
|
||||
|
||||
export default flappyApp;
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Freecell solitaire app worker. One singleton window holds a standard
|
||||
* 52-card Freecell game: 8 cascades, 4 free cells, 4 foundations. Red suits
|
||||
* render dim and black suits bright, since the display has no color.
|
||||
* have a bright marker beside their label, since the display has no color.
|
||||
*
|
||||
* Controls: scroll moves a cursor through the 16 locations (free cells,
|
||||
* foundations, then cascades, wrapping). Click selects a source, then click
|
||||
@@ -11,6 +11,10 @@
|
||||
* selection. Tap-then-hold opens the window menu (undo, new game, restart).
|
||||
* Watch swipes move the cursor spatially: left/right within the row, up/down
|
||||
* between the top row (cells + foundations) and the cascades.
|
||||
* The cursor skips locations that can't take part in the move being built:
|
||||
* with nothing selected, empty spots and the foundations (never a source);
|
||||
* with a source selected, filled free cells and foundations that can't
|
||||
* accept the card (see isCursorEligible).
|
||||
* Safe cards auto-play to the foundations after every move.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
@@ -18,10 +22,12 @@ import { 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 { ensurePreinstalledFonts, installedFontPath } from "../../graphics/installed-fonts";
|
||||
import { TtfFont } from "../../graphics/ttf-font";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -30,10 +36,13 @@ import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_
|
||||
declare const global: any;
|
||||
declare const com: any;
|
||||
|
||||
const largeFont = getFont("terminus32");
|
||||
const mediumFont = getFont("terminus24");
|
||||
const labelFont = getFont("terminus16");
|
||||
const smallFont = getDefaultSmallFont();
|
||||
ensurePreinstalledFonts();
|
||||
const fontPath = installedFontPath("Roboto-Regular.ttf");
|
||||
const largeFont = TtfFont.load(fontPath, 28) ?? getFont("terminus32");
|
||||
const mediumFont = TtfFont.load(fontPath, 18) ?? getFont("terminus24");
|
||||
const labelFont = TtfFont.load(fontPath, 14) ?? getFont("terminus16");
|
||||
const suitFont = TtfFont.load(fontPath, 10) ?? getFont("terminus16");
|
||||
const smallFont = TtfFont.load(fontPath, 12) ?? getFont("terminus12");
|
||||
|
||||
/** Cards are 0..51: suit = card % 4 (♠♥♣♦, alternating colors), rank 1..13. */
|
||||
const SUIT_CHARS = ["♠", "♥", "♣", "♦"] as const;
|
||||
@@ -52,10 +61,6 @@ function cardLabel(card: number): string {
|
||||
return RANK_CHARS[rankOf(card)]! + SUIT_CHARS[suitOf(card)]!;
|
||||
}
|
||||
|
||||
/** Red suits render dimmer than black so card color survives grayscale. */
|
||||
const BLACK_SHADE = 255;
|
||||
const RED_SHADE = 150;
|
||||
|
||||
const CARD_W = 60;
|
||||
const CARD_H = 36;
|
||||
const TOP_Y = 4;
|
||||
@@ -158,7 +163,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
moves: 0,
|
||||
cursor: LOC_CASCADE0,
|
||||
selected: null,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("freecell"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
newGame(window, false);
|
||||
@@ -248,6 +253,7 @@ function windowMenuItems(window: FreecellWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("freecell", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_NEW_GAME);
|
||||
},
|
||||
},
|
||||
@@ -291,25 +297,25 @@ function handleInput(window: FreecellWindow, event: InputEvent, frameId: number)
|
||||
function handlePlayingInput(window: FreecellWindow, event: InputEvent, frameId: number): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
window.cursor = (window.cursor + LOC_COUNT - 1) % LOC_COUNT;
|
||||
moveCursor(window, -1);
|
||||
break;
|
||||
case "scroll-down":
|
||||
window.cursor = (window.cursor + 1) % LOC_COUNT;
|
||||
moveCursor(window, 1);
|
||||
break;
|
||||
// Watch swipes are spatial over the two rows of eight columns: up/down
|
||||
// switch between the top row (free cells + foundations) and the cascades
|
||||
// keeping the column, left/right move within the row.
|
||||
// landing on the nearest usable column, left/right move within the row.
|
||||
case "swipe-up":
|
||||
if (window.cursor >= LOC_CASCADE0) window.cursor -= LOC_CASCADE0;
|
||||
if (window.cursor >= LOC_CASCADE0) moveCursorToRow(window, LOC_CELL0);
|
||||
break;
|
||||
case "swipe-down":
|
||||
if (window.cursor < LOC_CASCADE0) window.cursor += LOC_CASCADE0;
|
||||
if (window.cursor < LOC_CASCADE0) moveCursorToRow(window, LOC_CASCADE0);
|
||||
break;
|
||||
case "swipe-left":
|
||||
if (window.cursor % LOC_CASCADE0 > 0) window.cursor--;
|
||||
moveCursorInRow(window, -1);
|
||||
break;
|
||||
case "swipe-right":
|
||||
if (window.cursor % LOC_CASCADE0 < LOC_CASCADE0 - 1) window.cursor++;
|
||||
moveCursorInRow(window, 1);
|
||||
break;
|
||||
case "click":
|
||||
if (window.selected === null) {
|
||||
@@ -410,6 +416,72 @@ function locationHasCard(window: FreecellWindow, location: number): boolean {
|
||||
return window.cascades[location - LOC_CASCADE0]!.length > 0;
|
||||
}
|
||||
|
||||
function isFoundation(location: number): boolean {
|
||||
return location >= LOC_FOUNDATION0 && location < LOC_CASCADE0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the cursor should stop at a location, given the move being built.
|
||||
* Picking a source: it must hold a card, and foundations are never a source.
|
||||
* Picking a destination: an empty free cell, a foundation that accepts the
|
||||
* selected card, any cascade (a full legality check would hide the
|
||||
* cascades the player wants to compare), or the source itself (clicking it
|
||||
* again cancels the selection).
|
||||
*/
|
||||
function isCursorEligible(window: FreecellWindow, location: number): boolean {
|
||||
if (window.selected === null) {
|
||||
return !isFoundation(location) && locationHasCard(window, location);
|
||||
}
|
||||
if (location === window.selected) return true;
|
||||
if (location < LOC_FOUNDATION0) return window.cells[location] === null;
|
||||
if (isFoundation(location)) {
|
||||
const card = topCardAt(window, window.selected);
|
||||
return card !== null && foundationCanAccept(window.foundations[location - LOC_FOUNDATION0]!, card);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Step the cursor through the location list (wrapping), skipping ineligible spots. */
|
||||
function moveCursor(window: FreecellWindow, step: 1 | -1): void {
|
||||
let location = window.cursor;
|
||||
for (let i = 0; i < LOC_COUNT; i++) {
|
||||
location = (location + step + LOC_COUNT) % LOC_COUNT;
|
||||
if (isCursorEligible(window, location)) {
|
||||
window.cursor = location;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Step the cursor within its row (no wrapping), skipping ineligible spots. */
|
||||
function moveCursorInRow(window: FreecellWindow, step: 1 | -1): void {
|
||||
const rowStart = window.cursor < LOC_CASCADE0 ? LOC_CELL0 : LOC_CASCADE0;
|
||||
for (let location = window.cursor + step; location >= rowStart && location < rowStart + 8; location += step) {
|
||||
if (isCursorEligible(window, location)) {
|
||||
window.cursor = location;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Jump to the other row, keeping the column when it is eligible and
|
||||
* otherwise taking the nearest eligible column; stays put when the row has
|
||||
* none.
|
||||
*/
|
||||
function moveCursorToRow(window: FreecellWindow, rowStart: number): void {
|
||||
const column = window.cursor % 8;
|
||||
for (let distance = 0; distance < 8; distance++) {
|
||||
for (const candidate of [column - distance, column + distance]) {
|
||||
if (candidate < 0 || candidate >= 8) continue;
|
||||
if (isCursorEligible(window, rowStart + candidate)) {
|
||||
window.cursor = rowStart + candidate;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** True if `card` can go on `onto` in a cascade (descending, alternating color). */
|
||||
function canStack(card: number, onto: number): boolean {
|
||||
return rankOf(onto) === rankOf(card) + 1 && isRed(onto) !== isRed(card);
|
||||
@@ -710,13 +782,27 @@ function paintCard(
|
||||
selected: boolean,
|
||||
fullyVisible: boolean,
|
||||
): void {
|
||||
const shade = isRed(card) ? RED_SHADE : BLACK_SHADE;
|
||||
image.fillRoundedRect(x, y, CARD_W, CARD_H, selected ? 60 : 25, 4);
|
||||
image.fillRoundedRect(x, y, CARD_W, CARD_H, 0, 4);
|
||||
image.drawRoundedRect(x, y, CARD_W, CARD_H, selected ? 255 : 160, 4);
|
||||
if (selected) image.drawRoundedRect(x + 1, y + 1, CARD_W - 2, CARD_H - 2, 255, 3);
|
||||
image.drawText(labelFont, x + 5, y + 2, cardLabel(card), shade);
|
||||
const rankText: string = RANK_CHARS[rankOf(card)]!
|
||||
const suitText: string = SUIT_CHARS[suitOf(card)]!;
|
||||
const labelText = rankText + suitText;
|
||||
image.drawText(labelFont, x + 5, y, rankText, 255);
|
||||
image.drawText(suitFont, x + 5 + labelFont.measureText(rankText), y + 3, suitText, 150);
|
||||
if (isRed(card)) {
|
||||
// Keep the marker within the top strip, clear of even a two-digit rank.
|
||||
const markerX = x + 28;
|
||||
image.fillRect(markerX, y + 1, Math.max(0, x + CARD_W - 4 - markerX), 13, 10);
|
||||
}
|
||||
if (fullyVisible) {
|
||||
image.drawText(mediumFont, x + CARD_W - 18, y + CARD_H - 29, SUIT_CHARS[suitOf(card)]!, shade);
|
||||
image.drawText(
|
||||
mediumFont,
|
||||
x + CARD_W - 4 - Math.ceil(mediumFont.measureText(suitText)),
|
||||
y + CARD_H - mediumFont.lineHeight - 2,
|
||||
suitText,
|
||||
100,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { onAnySettingChanged } from "../../ui/dashboard-settings";
|
||||
import { glanceLayout, glanceShowLinesSetting, glanceSlotSettings } from "./glanceboard-settings";
|
||||
import { resolveGlanceRegions, slotDividers, type GlanceLayout, type GlanceRegion } from "./layout";
|
||||
import { type GlanceWidget } from "./widget";
|
||||
import { findGlanceWidget, glanceWidgetSpans } from "./widgets";
|
||||
|
||||
/** Brightness of the hairlines between regions: the dimmest shade that survives 4bpp quantization. */
|
||||
const DIVIDER_VALUE = 20;
|
||||
|
||||
type LiveRegion = { region: GlanceRegion; key: string; widget: GlanceWidget | null };
|
||||
|
||||
/** Identity of a region for diffing: which widget, in which slots. */
|
||||
function regionKey(region: GlanceRegion): string {
|
||||
return `${region.choice}@${region.slots.join(",")}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* A board: the layout's slots filled with the widgets the settings choose,
|
||||
* painted as one image. A widget chosen for two vertically adjacent slots
|
||||
* (when it supports that) gets one double-height region with no divider
|
||||
* between the slots. Widgets are created and started when the board starts
|
||||
* and stopped when it stops; a settings change meanwhile restarts only the
|
||||
* regions that changed.
|
||||
*/
|
||||
export class GlanceBoard {
|
||||
private regions: LiveRegion[] = [];
|
||||
private started = false;
|
||||
private unsubscribeSettings: (() => void) | null = null;
|
||||
private currentLayout: GlanceLayout;
|
||||
|
||||
constructor(
|
||||
private readonly requestRender: () => void,
|
||||
private readonly fixedLayout?: GlanceLayout,
|
||||
) {
|
||||
this.currentLayout = fixedLayout ?? glanceLayout();
|
||||
}
|
||||
|
||||
get layout(): GlanceLayout {
|
||||
return this.currentLayout;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
return this.layout.width;
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return this.layout.height;
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (this.started) return;
|
||||
this.started = true;
|
||||
this.applySlotSettings();
|
||||
this.unsubscribeSettings = onAnySettingChanged(() => this.applySlotSettings());
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (!this.started) return;
|
||||
this.started = false;
|
||||
this.unsubscribeSettings?.();
|
||||
this.unsubscribeSettings = null;
|
||||
for (const live of this.regions) live.widget?.stop();
|
||||
this.regions = [];
|
||||
}
|
||||
|
||||
private currentRegions(): GlanceRegion[] {
|
||||
const choices = glanceSlotSettings(this.layout).map((setting) => setting.get());
|
||||
return resolveGlanceRegions(this.layout, choices, glanceWidgetSpans);
|
||||
}
|
||||
|
||||
/** Rebuild the live regions from the settings, keeping widgets whose region is unchanged. */
|
||||
private applySlotSettings(): void {
|
||||
if (!this.started) return;
|
||||
const layout = this.fixedLayout ?? glanceLayout();
|
||||
const layoutChanged = layout !== this.currentLayout;
|
||||
this.currentLayout = layout;
|
||||
const previous = new Map(this.regions.map((live) => [live.key, live]));
|
||||
const next: LiveRegion[] = [];
|
||||
let changed = layoutChanged;
|
||||
for (const region of this.currentRegions()) {
|
||||
const key = regionKey(region);
|
||||
const kept = previous.get(key);
|
||||
if (kept) {
|
||||
previous.delete(key);
|
||||
next.push(kept);
|
||||
continue;
|
||||
}
|
||||
const widget = findGlanceWidget(region.choice)?.create() ?? null;
|
||||
widget?.start(this.requestRender);
|
||||
next.push({ region, key, widget });
|
||||
changed = true;
|
||||
}
|
||||
for (const removed of previous.values()) {
|
||||
removed.widget?.stop();
|
||||
changed = true;
|
||||
}
|
||||
this.regions = next;
|
||||
if (changed) this.requestRender();
|
||||
}
|
||||
|
||||
/** Paint the whole board (layout-sized). Widgets each get a fresh canvas of their region's size. */
|
||||
paint(): GrayImage {
|
||||
const image = new GrayImage(this.layout.width, this.layout.height, 0);
|
||||
for (const live of this.regions) {
|
||||
if (!live.widget) continue;
|
||||
const { rect } = live.region;
|
||||
const canvas = new GrayImage(rect.width, rect.height, 0);
|
||||
try {
|
||||
live.widget.paint(canvas);
|
||||
} catch (error) {
|
||||
console.warn(`glanceboard widget ${live.region.choice} paint failed`, error);
|
||||
}
|
||||
canvas.composeInto(image, rect.x, rect.y);
|
||||
}
|
||||
if (glanceShowLinesSetting.get()) {
|
||||
drawDividers(image, this.layout, this.regions.map((live) => live.region));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
function drawDividers(image: GrayImage, layout: GlanceLayout, regions: readonly GlanceRegion[]): void {
|
||||
for (const line of slotDividers(layout, regions)) {
|
||||
image.drawLine(line.x0, line.y0, line.x1, line.y1, DIVIDER_VALUE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { GlanceBoard } from "./board";
|
||||
import {
|
||||
clearConflictingSlots,
|
||||
glanceboardEnabledSetting,
|
||||
glanceLayout,
|
||||
glanceLayoutSetting,
|
||||
glanceShowLinesSetting,
|
||||
glanceShowOnHeadTiltSetting,
|
||||
glanceShowOnLongPressSetting,
|
||||
glanceSlotChoiceLabel,
|
||||
glanceSlotSettings,
|
||||
glanceTapDurationSetting,
|
||||
type GlanceSlotChoice,
|
||||
} from "./glanceboard-settings";
|
||||
import { resolveGlanceRegions, slotDividers } from "./layout";
|
||||
import { glanceWidgetSpans } from "./widgets";
|
||||
|
||||
export const GLANCEBOARD_WINDOW_ID = "glanceboard";
|
||||
export const GLANCEBOARD_SURFACE_ID = "window:glanceboard";
|
||||
|
||||
const TITLE_X = 18;
|
||||
const TITLE_Y = 10;
|
||||
const PARAGRAPH_X = 24;
|
||||
/** The entry page splits at the middle: menu left, slot preview right. */
|
||||
const PREVIEW_MARGIN = 8;
|
||||
const PREVIEW_MIN_LINE_VALUE = 20;
|
||||
const PREVIEW_LINE_VALUE = 100;
|
||||
|
||||
const INTRO =
|
||||
"Your Glanceboard is a display for things you want to look at quickly. Starting " +
|
||||
"from the screen being off, you can show the glanceboard with a tap or " +
|
||||
"long-press-and-hold. It will close automatically.";
|
||||
|
||||
/** Full-page list layout shared by the sub-screens. */
|
||||
function pageMenuLayout(width: number) {
|
||||
return { x: 8, y: 8, width: width - 16, opaque: true, showBorder: false } as const;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Glanceboard app's entry page. Left half: title, a short description
|
||||
* and the menu (enable toggle, live preview, slot contents, settings). Right
|
||||
* half: a scaled preview of the board's slots with their dividers and the
|
||||
* name of the widget in each. "Select Contents" moves focus into that
|
||||
* preview, where scroll picks a slot and a tap opens the picker for it;
|
||||
* a double-tap returns focus to the menu.
|
||||
*/
|
||||
class GlanceboardHomeLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private menuTop = 0;
|
||||
private menuHeight = 0;
|
||||
private focus: "menu" | "preview" = "menu";
|
||||
private selectedSlot = 0;
|
||||
|
||||
constructor(private readonly setPreviewVisible: (visible: boolean) => void) {}
|
||||
|
||||
private currentMenu(ctx: LayerContext, menuTop: number): MenuLayer {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
if (!this.menu || this.menuTop !== menuTop || this.menuHeight !== height) {
|
||||
const half = (width / 2) | 0;
|
||||
const items: MenuItem[] = [
|
||||
toggleSettingMenuItem(glanceboardEnabledSetting),
|
||||
{
|
||||
label: "Preview",
|
||||
onSelect: (menuCtx) => {
|
||||
menuCtx.stack.push(new GlancePreviewLayer(
|
||||
() => menuCtx.actions.requestRender(),
|
||||
() => this.setPreviewVisible(false),
|
||||
));
|
||||
this.setPreviewVisible(true);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Select Contents",
|
||||
onSelect: () => {
|
||||
this.focus = "preview";
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Settings",
|
||||
onSelect: (menuCtx) => menuCtx.stack.push(new GlanceSettingsLayer()),
|
||||
},
|
||||
];
|
||||
this.menuTop = menuTop;
|
||||
this.menuHeight = height;
|
||||
this.menu = new MenuLayer(null, items, {
|
||||
x: 8, y: menuTop, width: half - 16,
|
||||
showBorder: false, minHeight: 0, maxHeight: height - menuTop,
|
||||
});
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
/** Where the scaled board sits: the right half, board aspect, vertically centred. */
|
||||
private previewRect(width: number, height: number): { x: number; y: number; width: number; height: number } {
|
||||
const half = (width / 2) | 0;
|
||||
const availableWidth = width - half - 2 * PREVIEW_MARGIN;
|
||||
const layout = glanceLayout();
|
||||
const scale = Math.min(availableWidth / layout.width, (height - 2 * PREVIEW_MARGIN) / layout.height);
|
||||
const previewWidth = Math.round(layout.width * scale);
|
||||
const previewHeight = Math.round(layout.height * scale);
|
||||
return {
|
||||
x: half + PREVIEW_MARGIN + (((availableWidth - previewWidth) / 2) | 0),
|
||||
y: ((height - previewHeight) / 2) | 0,
|
||||
width: previewWidth,
|
||||
height: previewHeight,
|
||||
};
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const half = (width / 2) | 0;
|
||||
image.drawText(font, TITLE_X, TITLE_Y, "Glanceboard", 220);
|
||||
const step = lineStep(font);
|
||||
let y = TITLE_Y + step + 6;
|
||||
for (const line of wrapText(font, INTRO, half - PARAGRAPH_X - 12)) {
|
||||
image.drawText(font, PARAGRAPH_X, y, line, 160);
|
||||
y += step;
|
||||
}
|
||||
// The menu paints onto this page below the paragraph (its fill is raster,
|
||||
// so the deferred paragraph glyphs above stay untouched either way).
|
||||
// While the preview has focus the menu keeps its outline-only selection.
|
||||
const menu = this.currentMenu(ctx, y + 4);
|
||||
const menuFocused = ctx.stack.isFocused() && this.focus === "menu";
|
||||
const menuCtx: LayerContext = { ...ctx, stack: Object.assign(Object.create(ctx.stack), { isFocused: () => menuFocused }) };
|
||||
menu.paint(menuCtx, () => image);
|
||||
|
||||
this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1);
|
||||
const preview = this.previewRect(width, height);
|
||||
drawSlotPreview(image, preview, {
|
||||
selectedSlot: this.selectedSlot,
|
||||
selectionFocused: ctx.stack.isFocused() && this.focus === "preview",
|
||||
});
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.focus === "preview") {
|
||||
const slotCount = glanceLayout().slots.length;
|
||||
this.selectedSlot = Math.min(this.selectedSlot, slotCount - 1);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedSlot = (this.selectedSlot + slotCount - 1) % slotCount;
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedSlot = (this.selectedSlot + 1) % slotCount;
|
||||
return;
|
||||
case "click":
|
||||
openSlotPicker(ctx, this.selectedSlot);
|
||||
return;
|
||||
case "double-click":
|
||||
// Back from the preview is back to the menu, not out of the app;
|
||||
// this layer is the window's root and handles the yield itself
|
||||
// (see createGlanceboardAppWindow), so the gesture arrives here.
|
||||
this.focus = "menu";
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (event.type === "double-click") {
|
||||
shell.yieldFocusToSidebar();
|
||||
return;
|
||||
}
|
||||
await this.currentMenu(ctx, this.menuTop).handleInput(event, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The board's slots scaled into `rect`: dividers (full brightness, or the
|
||||
* dimmest dotted line when "Show lines" is off), the widget name centred in
|
||||
* each region (a merged double-height region has one name and no line
|
||||
* across it), and the selected slot highlighted.
|
||||
*/
|
||||
function drawSlotPreview(
|
||||
image: GrayImage,
|
||||
rect: { x: number; y: number; width: number; height: number },
|
||||
options: { selectedSlot: number; selectionFocused: boolean },
|
||||
): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const layout = glanceLayout();
|
||||
const settings = glanceSlotSettings(layout);
|
||||
const choices = settings.map((setting) => setting.get());
|
||||
const regions = resolveGlanceRegions(layout, choices, glanceWidgetSpans);
|
||||
const sx = rect.width / layout.width;
|
||||
const sy = rect.height / layout.height;
|
||||
const scaled = (r: { x: number; y: number; width: number; height: number }) => ({
|
||||
x: rect.x + Math.round(r.x * sx),
|
||||
y: rect.y + Math.round(r.y * sy),
|
||||
width: Math.round(r.width * sx),
|
||||
height: Math.round(r.height * sy),
|
||||
});
|
||||
|
||||
image.drawRect(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2, 72);
|
||||
const selected = scaled(layout.slots[options.selectedSlot]!.rect);
|
||||
drawSelectionHighlight(image, selected.x + 1, selected.y + 1, selected.width - 2, selected.height - 2, options.selectionFocused, 4);
|
||||
|
||||
const showLines = glanceShowLinesSetting.get();
|
||||
for (const line of slotDividers(layout, regions)) {
|
||||
const x0 = rect.x + Math.round(line.x0 * sx);
|
||||
const y0 = rect.y + Math.round(line.y0 * sy);
|
||||
const x1 = rect.x + Math.round(line.x1 * sx);
|
||||
const y1 = rect.y + Math.round(line.y1 * sy);
|
||||
if (showLines) {
|
||||
image.drawLine(x0, y0, x1, y1, PREVIEW_LINE_VALUE);
|
||||
} else {
|
||||
// Every other pixel at the dimmest visible shade: the line is there to
|
||||
// read the layout by, but says "off".
|
||||
drawDottedLine(image, x0, y0, x1, y1, PREVIEW_MIN_LINE_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
// Names: one per region, "Empty" in slots no region covers.
|
||||
const covered = new Set<number>();
|
||||
const label = (r: { x: number; y: number; width: number; height: number }, text: string, value: number) => {
|
||||
const clipped = truncateText(font, text, Math.max(0, r.width - 8));
|
||||
image.drawText(font, r.x + (((r.width - font.measureText(clipped)) / 2) | 0), r.y + (((r.height - font.lineHeight) / 2) | 0), clipped, value);
|
||||
};
|
||||
for (const region of regions) {
|
||||
for (const index of region.slots) covered.add(index);
|
||||
label(scaled(region.rect), glanceSlotChoiceLabel(region.choice as GlanceSlotChoice), 210);
|
||||
}
|
||||
layout.slots.forEach((slot, index) => {
|
||||
if (!covered.has(index)) label(scaled(slot.rect), "Empty", 90);
|
||||
});
|
||||
}
|
||||
|
||||
/** A horizontal or vertical line lit on every other pixel. */
|
||||
function drawDottedLine(image: GrayImage, x0: number, y0: number, x1: number, y1: number, value: number): void {
|
||||
if (y0 === y1) {
|
||||
for (let x = Math.min(x0, x1); x <= Math.max(x0, x1); x += 2) image.setPixel(x, y0, value);
|
||||
} else {
|
||||
for (let y = Math.min(y0, y1); y <= Math.max(y0, y1); y += 2) image.setPixel(x0, y, value);
|
||||
}
|
||||
}
|
||||
|
||||
/** The picker for one slot: every widget, the current one marked; choosing applies the one-per-board rule. */
|
||||
function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
|
||||
const layout = glanceLayout();
|
||||
const setting = glanceSlotSettings(layout)[slotIndex];
|
||||
if (!setting) return;
|
||||
const current = setting.get();
|
||||
const items: MenuItem[] = setting.values.map((value) => ({
|
||||
label: setting.displayValue(value),
|
||||
onSelect: (menuCtx) => {
|
||||
setting.set(value);
|
||||
clearConflictingSlots(layout, slotIndex, value);
|
||||
menuCtx.stack.pop();
|
||||
},
|
||||
render: ({ image, x, y }) => {
|
||||
image.drawText(getDefaultSmallFont(), x, y + LIST_ROW_TEXT_INSET, `${setting.displayValue(value)}${setting.get() === value ? " *" : ""}`, 200);
|
||||
},
|
||||
}));
|
||||
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
|
||||
}
|
||||
|
||||
/** Settings: layout, sleep gestures, duration, and slot lines. */
|
||||
class GlanceSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
if (!this.menu) {
|
||||
const { width } = ctx.stack.getBaseSize();
|
||||
const items: MenuItem[] = [
|
||||
enumSettingMenuItem(glanceLayoutSetting),
|
||||
enumSettingMenuItem(glanceTapDurationSetting),
|
||||
toggleSettingMenuItem(glanceShowOnLongPressSetting),
|
||||
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
|
||||
toggleSettingMenuItem(glanceShowLinesSetting),
|
||||
];
|
||||
this.menu = new MenuLayer("Glanceboard settings", items, pageMenuLayout(width));
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
return this.currentMenu(ctx).paint(ctx, paintBelow);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
await this.currentMenu(ctx).handleInput(event, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/** The live board in the window; any click or double-click returns to the list. */
|
||||
class GlancePreviewLayer implements Layer {
|
||||
private readonly board: GlanceBoard;
|
||||
|
||||
constructor(requestRender: () => void, private readonly onClose: () => void) {
|
||||
this.board = new GlanceBoard(requestRender);
|
||||
this.board.start();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const board = this.board.paint();
|
||||
if (board.width === width && board.height === height) return board;
|
||||
// A viewport of another size (a per-app display override) shows the board
|
||||
// top-left, cropped or padded, rather than scaled.
|
||||
const image = new GrayImage(width, height, 0);
|
||||
board.composeInto(image, 0, 0);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "click" || event.type === "double-click") ctx.stack.pop();
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.board.stop();
|
||||
this.onClose();
|
||||
}
|
||||
}
|
||||
|
||||
export function createGlanceboardAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let previewVisible = false;
|
||||
const heightMode = () => previewVisible && glanceLayoutSetting.get() === "2x3" ? "max" as const : "medium" as const;
|
||||
let unsubscribeSettings: (() => void) | undefined;
|
||||
const app = createInProcessWindow({
|
||||
appId: "glanceboard",
|
||||
windowId: GLANCEBOARD_WINDOW_ID,
|
||||
title: "Glanceboard",
|
||||
iconLetter: "Gb",
|
||||
icon: "eye",
|
||||
closeable: true,
|
||||
// Settings and the contents picker stay in the default 576x288 viewport;
|
||||
// only the live six-slot preview needs the taller window.
|
||||
heightMode: heightMode(),
|
||||
actions: options.actions,
|
||||
// Not wrapped in YieldAtRootLayer: the home page routes double-click
|
||||
// itself (preview focus -> menu focus, menu focus -> sidebar).
|
||||
baseLayer: new GlanceboardHomeLayer((visible) => {
|
||||
previewVisible = visible;
|
||||
app.setHeightMode(heightMode());
|
||||
}),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
reconfigureSurface: options.reconfigureSurface,
|
||||
onClosed: () => {
|
||||
unsubscribeSettings?.();
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
unsubscribeSettings = onAnySettingChanged(() => app.setHeightMode(heightMode()));
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
|
||||
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT, slotsVerticallyAdjacent, type GlanceLayout } from "./layout";
|
||||
import { type GlanceWidgetId } from "./widget";
|
||||
import { glanceWidgetSpans } from "./widgets";
|
||||
|
||||
export type GlanceSlotChoice = GlanceWidgetId | "none";
|
||||
|
||||
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "calendar", "terminal", "nightscout", "compass", "music"];
|
||||
|
||||
const SLOT_CHOICE_LABELS: Record<GlanceSlotChoice, string> = {
|
||||
none: "Empty",
|
||||
"system-card": "System card",
|
||||
calendar: "Calendar",
|
||||
terminal: "Terminal",
|
||||
nightscout: "Nightscout",
|
||||
compass: "Compass",
|
||||
music: "Music",
|
||||
};
|
||||
|
||||
export function glanceSlotChoiceLabel(choice: GlanceSlotChoice): string {
|
||||
return SLOT_CHOICE_LABELS[choice] ?? choice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Slot order follows QUADRANT_LAYOUT: top left, top right, bottom left, bottom
|
||||
* right. Calendar in both right slots merges into one double-height region.
|
||||
*/
|
||||
const DEFAULT_QUADRANT_CHOICES: readonly GlanceSlotChoice[] = ["system-card", "calendar", "music", "calendar"];
|
||||
|
||||
export const glanceLayoutSetting = new ConfigSettingEnum<"2x2" | "2x3">({
|
||||
id: "glanceboard-layout",
|
||||
label: "Layout",
|
||||
storageKey: "glanceboard.layout",
|
||||
defaultValue: "2x2",
|
||||
values: ["2x2", "2x3"],
|
||||
description: "Choose two columns with two or three rows of widgets.",
|
||||
});
|
||||
|
||||
export function glanceLayout(): GlanceLayout {
|
||||
return glanceLayoutSetting.get() === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether sleep-time gestures show the board at all. Off restores the plain
|
||||
* behaviour: a tap, hold or head-tilt while asleep does nothing (a head-tilt
|
||||
* wakes the regular UI), double-tap wakes.
|
||||
*/
|
||||
export const glanceboardEnabledSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-enabled",
|
||||
label: "Enable Glanceboard",
|
||||
storageKey: "glanceboard.enabled",
|
||||
defaultValue: false,
|
||||
description:
|
||||
"While the display is asleep, a tap, a long-press or a head-tilt shows the Glanceboard instead of the regular UI. Double-tap still wakes the regular UI.",
|
||||
});
|
||||
|
||||
export type GlanceTapDuration = "off" | "3s" | "5s" | "7s" | "10s";
|
||||
|
||||
const DEFAULT_TAP_DURATION: GlanceTapDuration = "5s";
|
||||
|
||||
/**
|
||||
* How long a tap (or head-tilt) keeps the board up; each further tap restarts
|
||||
* it. "off" means a single tap does not show the board at all; a head-tilt
|
||||
* then uses the default duration.
|
||||
*/
|
||||
export const glanceTapDurationSetting = new ConfigSettingEnum<GlanceTapDuration>({
|
||||
id: "glanceboard-tap-duration",
|
||||
label: "Show on tap",
|
||||
storageKey: "glanceboard.tapDuration",
|
||||
defaultValue: DEFAULT_TAP_DURATION,
|
||||
values: ["off", "3s", "5s", "7s", "10s"],
|
||||
formatValue: (value) => (value === "off" ? "Disabled" : value.replace("s", " s")),
|
||||
description:
|
||||
"How long a single tap (or a head-tilt) keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.",
|
||||
});
|
||||
|
||||
/** Whether a single tap while asleep shows the board. */
|
||||
export function glanceShowOnTap(): boolean {
|
||||
return glanceTapDurationSetting.get() !== "off";
|
||||
}
|
||||
|
||||
export function glanceTapTimeoutMs(): number {
|
||||
const duration = glanceTapDurationSetting.get();
|
||||
const seconds = duration === "off" ? DEFAULT_TAP_DURATION : duration;
|
||||
return Number.parseInt(seconds, 10) * 1000;
|
||||
}
|
||||
|
||||
/** A long-press holds the board up until the press is released. */
|
||||
export const glanceShowOnLongPressSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-show-on-long-press",
|
||||
label: "Show on long press",
|
||||
storageKey: "glanceboard.showOnLongPress",
|
||||
defaultValue: true,
|
||||
description: "A long-press or short-then-long-press while asleep shows the Glanceboard until the press is released.",
|
||||
});
|
||||
|
||||
/** The head-tilt wake shows the board (for the tap duration) instead of the regular UI. */
|
||||
export const glanceShowOnHeadTiltSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-show-on-head-tilt",
|
||||
label: "Show on head tilt",
|
||||
storageKey: "glanceboard.showOnHeadTilt",
|
||||
defaultValue: true,
|
||||
description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI.",
|
||||
});
|
||||
|
||||
/** Hairlines between the board's slots. */
|
||||
export const glanceShowLinesSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-show-lines",
|
||||
label: "Show lines",
|
||||
storageKey: "glanceboard.showLines",
|
||||
defaultValue: true,
|
||||
description: "Draw the separator lines between the Glanceboard's slots.",
|
||||
});
|
||||
|
||||
/**
|
||||
* Both grid sizes share the original storage keys so changing row count
|
||||
* preserves the first four choices and remembers the hidden bottom row.
|
||||
*/
|
||||
export function glanceSlotSettings(layout: GlanceLayout = glanceLayout()): ConfigSettingEnum<GlanceSlotChoice>[] {
|
||||
const grid = layout === QUADRANT_LAYOUT || layout === SIX_SLOT_LAYOUT;
|
||||
const storageId = grid ? QUADRANT_LAYOUT.id : layout.id;
|
||||
return layout.slots.map(
|
||||
(slot, index) =>
|
||||
new ConfigSettingEnum<GlanceSlotChoice>({
|
||||
id: `glanceboard-${layout.id}-slot-${index}`,
|
||||
label: slot.label,
|
||||
storageKey: `glanceboard.${storageId}.slot.${index}`,
|
||||
defaultValue: (grid ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none",
|
||||
values: SLOT_CHOICES,
|
||||
formatValue: glanceSlotChoiceLabel,
|
||||
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A slot was just set to `choice`: each widget appears on the board once, so
|
||||
* clear every other slot holding the same choice, except a vertically
|
||||
* adjacent one when the widget can span two slots (it then renders at
|
||||
* double height). Returns the indices cleared.
|
||||
*/
|
||||
export function clearConflictingSlots(layout: GlanceLayout, changedIndex: number, choice: GlanceSlotChoice): number[] {
|
||||
if (choice === "none") return [];
|
||||
const settings = glanceSlotSettings(layout);
|
||||
const spans = glanceWidgetSpans(choice);
|
||||
const cleared: number[] = [];
|
||||
let keptPartner = false;
|
||||
settings.forEach((setting, index) => {
|
||||
if (index === changedIndex || setting.get() !== choice) return;
|
||||
if (spans && !keptPartner && slotsVerticallyAdjacent(layout, changedIndex, index)) {
|
||||
keptPartner = true;
|
||||
return;
|
||||
}
|
||||
setting.set("none");
|
||||
cleared.push(index);
|
||||
});
|
||||
return cleared;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { GlanceBoard } from "./board";
|
||||
import { createGlanceboardAppWindow, GLANCEBOARD_SURFACE_ID, GLANCEBOARD_WINDOW_ID } from "./glanceboard-app";
|
||||
import {
|
||||
glanceboardEnabledSetting,
|
||||
glanceLayout,
|
||||
glanceShowOnHeadTiltSetting,
|
||||
glanceShowOnLongPressSetting,
|
||||
glanceShowOnTap,
|
||||
glanceTapTimeoutMs,
|
||||
} from "./glanceboard-settings";
|
||||
|
||||
const glanceboardApp: AppDefinition = {
|
||||
appId: "glanceboard",
|
||||
title: "Glanceboard",
|
||||
icon: "eye",
|
||||
launch: (ctx) => ctx.launchInProcessApp(GLANCEBOARD_WINDOW_ID, GLANCEBOARD_SURFACE_ID, createGlanceboardAppWindow),
|
||||
glanceboard: {
|
||||
get size() {
|
||||
const layout = glanceLayout();
|
||||
return { width: layout.width, height: layout.height };
|
||||
},
|
||||
isEnabled: () => glanceboardEnabledSetting.get(),
|
||||
showOnTap: glanceShowOnTap,
|
||||
tapTimeoutMs: glanceTapTimeoutMs,
|
||||
showOnLongPress: () => glanceShowOnLongPressSetting.get(),
|
||||
showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(),
|
||||
createBoard: (requestRender) => new GlanceBoard(requestRender),
|
||||
},
|
||||
};
|
||||
|
||||
export default glanceboardApp;
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Board geometry. A layout names its slots and places them on the board;
|
||||
* the Glanceboard app's slot pickers and the board renderer are both driven
|
||||
* by it, so a replacement layout (different slot count, sizes, positions) is
|
||||
* a matter of swapping this object out and reusing the same widgets.
|
||||
*/
|
||||
export const GLANCEBOARD_WIDTH = 576;
|
||||
export const GLANCEBOARD_HEIGHT = 288;
|
||||
|
||||
export type GlanceSlotRect = { x: number; y: number; width: number; height: number };
|
||||
|
||||
export type GlanceLayout = {
|
||||
id: string;
|
||||
width: number;
|
||||
height: number;
|
||||
slots: readonly { label: string; rect: GlanceSlotRect }[];
|
||||
};
|
||||
|
||||
/** The first layout: four equal quadrants, each 288x144. */
|
||||
export const QUADRANT_LAYOUT: GlanceLayout = {
|
||||
id: "quadrants",
|
||||
width: GLANCEBOARD_WIDTH,
|
||||
height: GLANCEBOARD_HEIGHT,
|
||||
slots: [
|
||||
{ label: "Top left", rect: { x: 0, y: 0, width: 288, height: 144 } },
|
||||
{ label: "Top right", rect: { x: 288, y: 0, width: 288, height: 144 } },
|
||||
{ label: "Bottom left", rect: { x: 0, y: 144, width: 288, height: 144 } },
|
||||
{ label: "Bottom right", rect: { x: 288, y: 144, width: 288, height: 144 } },
|
||||
],
|
||||
};
|
||||
|
||||
/** Six 288x144 slots: the same first two rows, plus a third at the bottom. */
|
||||
export const SIX_SLOT_LAYOUT: GlanceLayout = {
|
||||
id: "six-slots",
|
||||
width: GLANCEBOARD_WIDTH,
|
||||
height: 432,
|
||||
slots: [
|
||||
...QUADRANT_LAYOUT.slots.slice(0, 2),
|
||||
{ label: "Middle left", rect: { x: 0, y: 144, width: 288, height: 144 } },
|
||||
{ label: "Middle right", rect: { x: 288, y: 144, width: 288, height: 144 } },
|
||||
{ label: "Bottom left", rect: { x: 0, y: 288, width: 288, height: 144 } },
|
||||
{ label: "Bottom right", rect: { x: 288, y: 288, width: 288, height: 144 } },
|
||||
],
|
||||
};
|
||||
|
||||
/** Whether two slots stack vertically with matching width (one region if merged). */
|
||||
export function slotsVerticallyAdjacent(layout: GlanceLayout, a: number, b: number): boolean {
|
||||
const first = layout.slots[a]?.rect;
|
||||
const second = layout.slots[b]?.rect;
|
||||
if (!first || !second || a === b) return false;
|
||||
if (first.x !== second.x || first.width !== second.width) return false;
|
||||
return first.y + first.height === second.y || second.y + second.height === first.y;
|
||||
}
|
||||
|
||||
/** A painted area of the board: one slot, or two vertically adjacent slots merged. */
|
||||
export type GlanceRegion = {
|
||||
/** The slot choice (widget id) filling the region. */
|
||||
choice: string;
|
||||
rect: GlanceSlotRect;
|
||||
/** Indices into layout.slots, ascending. */
|
||||
slots: number[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Turn per-slot choices into regions. A widget chosen for two vertically
|
||||
* adjacent slots, when `canSpan` says it supports it, becomes one
|
||||
* double-height region; every other non-empty slot is a region of its own
|
||||
* (a duplicate that cannot merge simply paints twice). Regions come out in
|
||||
* slot order.
|
||||
*/
|
||||
export function resolveGlanceRegions(
|
||||
layout: GlanceLayout,
|
||||
choices: readonly string[],
|
||||
canSpan: (choice: string) => boolean,
|
||||
): GlanceRegion[] {
|
||||
const regions: GlanceRegion[] = [];
|
||||
const consumed = new Set<number>();
|
||||
layout.slots.forEach((slot, index) => {
|
||||
if (consumed.has(index)) return;
|
||||
const choice = choices[index] ?? "none";
|
||||
if (choice === "none") return;
|
||||
consumed.add(index);
|
||||
let rect = { ...slot.rect };
|
||||
const slots = [index];
|
||||
if (canSpan(choice)) {
|
||||
const partner = layout.slots.findIndex(
|
||||
(_, other) => !consumed.has(other) && choices[other] === choice && slotsVerticallyAdjacent(layout, index, other),
|
||||
);
|
||||
if (partner >= 0) {
|
||||
consumed.add(partner);
|
||||
slots.push(partner);
|
||||
const other = layout.slots[partner]!.rect;
|
||||
const top = Math.min(rect.y, other.y);
|
||||
rect = { x: rect.x, y: top, width: rect.width, height: Math.max(rect.y + rect.height, other.y + other.height) - top };
|
||||
}
|
||||
}
|
||||
regions.push({ choice, rect, slots });
|
||||
});
|
||||
return regions;
|
||||
}
|
||||
|
||||
/** A divider line between slots, in board coordinates (inclusive endpoints). */
|
||||
export type GlanceDivider = { x0: number; y0: number; x1: number; y1: number };
|
||||
|
||||
/**
|
||||
* The hairlines between slots: every slot's left edge (when not at the
|
||||
* board's left) and top edge (when not at the top), except the top edge of
|
||||
* the lower slot of a merged double-height region, which is that region's
|
||||
* interior. Shared by the board painter and the config app's preview.
|
||||
*/
|
||||
export function slotDividers(layout: GlanceLayout, regions: readonly GlanceRegion[]): GlanceDivider[] {
|
||||
const dividers: GlanceDivider[] = [];
|
||||
layout.slots.forEach(({ rect }, index) => {
|
||||
if (rect.x > 0) dividers.push({ x0: rect.x, y0: rect.y, x1: rect.x, y1: rect.y + rect.height - 1 });
|
||||
if (rect.y === 0) return;
|
||||
const interior = regions.some(
|
||||
(region) => region.slots.length > 1 && region.slots.includes(index) && region.rect.y < rect.y,
|
||||
);
|
||||
if (!interior) dividers.push({ x0: rect.x, y0: rect.y, x1: rect.x + rect.width - 1, y1: rect.y });
|
||||
});
|
||||
return dividers;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { type GrayImage } from "../../graphics/image";
|
||||
|
||||
/**
|
||||
* A Glanceboard widget: a small self-contained view that paints into a slot
|
||||
* of the board. Widgets are sized by the layout (the first layout is four
|
||||
* 288x144 quadrants) and own their data subscriptions and any hardware they
|
||||
* need between start and stop, so a board that is not showing costs nothing.
|
||||
*
|
||||
* Layouts are expected to be swapped out and iterated on; widgets are the
|
||||
* stable part, so keep them independent of any particular board geometry
|
||||
* beyond the slot size they are handed.
|
||||
*/
|
||||
export interface GlanceWidget {
|
||||
/**
|
||||
* The board became visible (or this widget was added to a visible board):
|
||||
* subscribe to data, start timers, enable hardware. `requestRender` asks
|
||||
* the board to repaint when something changed.
|
||||
*/
|
||||
start(requestRender: () => void): void;
|
||||
/** Undo start. Called once for every start; must be safe to call twice. */
|
||||
stop(): void;
|
||||
/**
|
||||
* Paint the widget into `image`, a fresh 0-filled canvas of the slot's
|
||||
* size. Deferred draws (text, drawImage) are welcome and keep the cheap
|
||||
* texture-cache wire path.
|
||||
*/
|
||||
paint(image: GrayImage): void;
|
||||
}
|
||||
|
||||
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal";
|
||||
|
||||
export type GlanceWidgetDefinition = {
|
||||
id: GlanceWidgetId;
|
||||
/** Picker label in the Glanceboard app. */
|
||||
label: string;
|
||||
/**
|
||||
* The widget can fill two vertically adjacent slots as one double-height
|
||||
* region (chosen for both left or both right quadrants). Its paint then
|
||||
* gets the taller canvas; the divider between the slots is not drawn.
|
||||
*/
|
||||
tall?: boolean;
|
||||
create: () => GlanceWidget;
|
||||
};
|
||||
@@ -0,0 +1,137 @@
|
||||
import { type GrayImage, type UiFont } from "../../../graphics/image";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { hasCalendarPermission } from "../../../g2/android-permissions";
|
||||
import { readUpcomingEvents, type CalendarEvent } from "../../../native/calendar";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { dayHeaderLabel, formatEventTime } from "../../calendar/calendar";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
/** A row fits while its glyph line clears this margin above the bottom edge. */
|
||||
const BOTTOM_MARGIN = 2;
|
||||
const MINUTE_MS = 60_000;
|
||||
const HOUR_MS = 60 * MINUTE_MS;
|
||||
|
||||
/**
|
||||
* What is next on the calendar: the current or next event large, with how
|
||||
* far off it is (or when it ends), then the following events grouped under
|
||||
* day headers, as many as fit. Reads the same cached provider query as the
|
||||
* Calendar app; a minute tick keeps the countdown honest and lets the
|
||||
* 30-second cache refresh on its own.
|
||||
*/
|
||||
export class CalendarWidget implements GlanceWidget {
|
||||
private tick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.tick = setInterval(requestRender, MINUTE_MS);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (this.tick !== null) clearInterval(this.tick);
|
||||
this.tick = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const small = getDefaultSmallFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const step = lineStep(small);
|
||||
const textWidth = image.width - 2 * PAD;
|
||||
|
||||
if (!hasCalendarPermission()) {
|
||||
image.drawText(small, PAD, PAD, "Calendar", 150);
|
||||
image.drawText(small, PAD, PAD + step + 4, "Calendar permission needed", 140);
|
||||
image.drawText(small, PAD, PAD + 2 * step + 4, "Open the Calendar app to grant it", 100);
|
||||
return;
|
||||
}
|
||||
const nowMs = Date.now();
|
||||
// Events still in progress count; the provider's window starts at "now",
|
||||
// so the list can include an event that began earlier and is still on.
|
||||
const events = readUpcomingEvents().filter((event) => event.endMs > nowMs);
|
||||
if (!events.length) {
|
||||
image.drawText(small, PAD, PAD, "Calendar", 150);
|
||||
image.drawText(small, PAD, PAD + step + 4, "No upcoming events", 140);
|
||||
return;
|
||||
}
|
||||
|
||||
// Headline: the current or next event, with its day when it is not today.
|
||||
const next = events[0]!;
|
||||
let y = PAD;
|
||||
const headline = truncateText(medium, next.title || "(untitled)", textWidth);
|
||||
image.drawText(medium, PAD, y, headline, 230);
|
||||
y += medium.lineHeight + 2;
|
||||
image.drawText(small, PAD, y, truncateText(small, headlineDetail(next, nowMs), textWidth), 170);
|
||||
y += step + 6;
|
||||
|
||||
// The rest, grouped by day. A day header only where the day changes from
|
||||
// the previous listed event (the headline's day included).
|
||||
let previousDayKey = dayKeyOf(next.startMs);
|
||||
for (let index = 1; index < events.length; index++) {
|
||||
const event = events[index]!;
|
||||
const dayKey = dayKeyOf(event.startMs);
|
||||
const needsHeader = dayKey !== previousDayKey;
|
||||
// The header and its row are placed a step apart; the row only needs
|
||||
// its line height to fit above the bottom margin.
|
||||
if (y + (needsHeader ? step : 0) + small.lineHeight > image.height - BOTTOM_MARGIN) break;
|
||||
if (needsHeader) {
|
||||
image.drawText(small, PAD, y, truncateText(small, dayHeaderLabel(event.startMs), textWidth), 120);
|
||||
y += step;
|
||||
previousDayKey = dayKey;
|
||||
}
|
||||
drawEventLine(image, small, PAD, y, textWidth, event, 190);
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** "Now, ends 14:30", "In 25 min", "Tomorrow 09:00", "All day"... for the headline. */
|
||||
function headlineDetail(event: CalendarEvent, nowMs: number): string {
|
||||
const dayDelta = dayDeltaFromToday(event.startMs);
|
||||
const when = event.allDay
|
||||
? "All day"
|
||||
: `${formatEventTime(event.startMs)} - ${formatEventTime(event.endMs)}`;
|
||||
if (event.startMs <= nowMs) {
|
||||
if (event.allDay) return dayDelta === 0 ? "All day today" : `All day, ${dayHeaderLabel(event.startMs)}`;
|
||||
return `Now, ends ${formatEventTime(event.endMs)}`;
|
||||
}
|
||||
const untilMs = event.startMs - nowMs;
|
||||
if (dayDelta === 0 && untilMs < 12 * HOUR_MS) {
|
||||
return `In ${formatDuration(untilMs)}, ${when}`;
|
||||
}
|
||||
const day = dayDelta === 0 ? "Today" : dayDelta === 1 ? "Tomorrow" : dayHeaderLabel(event.startMs);
|
||||
return `${day}, ${when}`;
|
||||
}
|
||||
|
||||
function drawEventLine(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
event: CalendarEvent,
|
||||
value: number,
|
||||
): void {
|
||||
const time = event.allDay ? "All day" : formatEventTime(event.startMs);
|
||||
const timeWidth = font.measureText(time);
|
||||
image.drawText(font, x, y, time, value - 50);
|
||||
const titleX = x + timeWidth + 8;
|
||||
image.drawText(font, titleX, y, truncateText(font, event.title || "(untitled)", Math.max(0, x + width - titleX)), value);
|
||||
}
|
||||
|
||||
function formatDuration(ms: number): string {
|
||||
const minutes = Math.max(1, Math.round(ms / MINUTE_MS));
|
||||
if (minutes < 60) return `${minutes} min`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const rest = minutes % 60;
|
||||
return rest ? `${hours} h ${rest} min` : `${hours} h`;
|
||||
}
|
||||
|
||||
function dayKeyOf(timestampMs: number): string {
|
||||
const date = new Date(timestampMs);
|
||||
return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
||||
}
|
||||
|
||||
function dayDeltaFromToday(timestampMs: number): number {
|
||||
const midnight = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
return Math.round((midnight(new Date(timestampMs)) - midnight(new Date())) / (24 * HOUR_MS));
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { hasLocationPermission } from "../../../g2/android-permissions";
|
||||
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
|
||||
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "../../compass/declination";
|
||||
import { getNorthReference, resolveHeading } from "../../compass/heading";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
/** Owner token for the refcounted magnetometer, distinct from the Compass app's. */
|
||||
const COMPASS_OWNER = "glanceboard";
|
||||
const TOP_PAD = 4;
|
||||
const BOTTOM_PAD = 4;
|
||||
const EDGE_PAD = 8;
|
||||
const MAX_ROSE_RADIUS = 60;
|
||||
|
||||
/**
|
||||
* The Compass app's rose at card size: heading readout above, tilted rose
|
||||
* below. Turns the magnetometer on for as long as the board shows it (the
|
||||
* Compass app keeps its own hold, so the two never switch each other off).
|
||||
* Never prompts for location: true north is used when a declination is
|
||||
* already known, otherwise the readout says it is magnetic.
|
||||
*/
|
||||
export class CompassWidget implements GlanceWidget {
|
||||
private rawHeading: number | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private unsubscribeDeclination: (() => void) | null = null;
|
||||
private enabled = false;
|
||||
private background: { key: string; image: GrayImage } | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = addCompassListener((event) => {
|
||||
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||
this.rawHeading = normalizeHeading(event.headingDegrees);
|
||||
requestRender();
|
||||
}
|
||||
});
|
||||
this.unsubscribeDeclination = onDeclinationChanged(requestRender);
|
||||
if (!this.enabled) {
|
||||
this.enabled = true;
|
||||
setCompassEnabled(true, COMPASS_OWNER);
|
||||
}
|
||||
if (getNorthReference() === "true" && hasLocationPermission()) refreshDeclination();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.unsubscribeDeclination?.();
|
||||
this.unsubscribeDeclination = null;
|
||||
if (this.enabled) {
|
||||
this.enabled = false;
|
||||
setCompassEnabled(false, COMPASS_OWNER);
|
||||
}
|
||||
}
|
||||
|
||||
private statusText(): string | null {
|
||||
if (this.rawHeading === null) return "Waiting for compass";
|
||||
if (!isCompassCalibrated()) return "Uncalibrated";
|
||||
if (getNorthReference() === "true" && getDeclinationAvailability() !== "available") return "Magnetic";
|
||||
return null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const { width, height } = image;
|
||||
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
|
||||
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
|
||||
const status = this.statusText();
|
||||
const textHeight = medium.lineHeight + (status ? small.lineHeight + 2 : 0);
|
||||
const cx = width / 2;
|
||||
const { cy, radius, ringTop } = layoutCompassRose({
|
||||
width,
|
||||
height,
|
||||
cx,
|
||||
topClearance: TOP_PAD + textHeight + 4,
|
||||
bottomPad: BOTTOM_PAD,
|
||||
edgePad: EDGE_PAD,
|
||||
maxRadius: MAX_ROSE_RADIUS,
|
||||
minRadius: 16,
|
||||
});
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||
const clipY = y + textHeight + 4;
|
||||
const key = [width, height, cx, cy, radius, clipY, fade].join(",");
|
||||
if (this.background?.key !== key) {
|
||||
this.background = { key, image: createCompassBackground(width, height, cx, cy, radius, clipY, fade) };
|
||||
}
|
||||
// composeInto keeps the background's cached interior texture as a
|
||||
// deferred image draw, so the wire planner can restore it from cache.
|
||||
this.background.image.composeInto(image, 0, 0);
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
image.drawText(medium, Math.round(cx - medium.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
|
||||
if (status) {
|
||||
image.drawText(small, Math.round(cx - small.measureText(status) / 2), y + medium.lineHeight + 2, status, 125);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { type GlanceWidgetDefinition, type GlanceWidgetId } from "../widget";
|
||||
import { CalendarWidget } from "./calendar-widget";
|
||||
import { CompassWidget } from "./compass-widget";
|
||||
import { MusicWidget } from "./music-widget";
|
||||
import { NightscoutWidget } from "./nightscout-widget";
|
||||
import { SystemCardWidget } from "./system-card";
|
||||
import { TerminalWidget } from "./terminal-widget";
|
||||
|
||||
/** Every widget a board slot can hold. */
|
||||
export const GLANCE_WIDGETS: readonly GlanceWidgetDefinition[] = [
|
||||
{ id: "system-card", label: "System card", create: () => new SystemCardWidget() },
|
||||
{ id: "calendar", label: "Calendar", tall: true, create: () => new CalendarWidget() },
|
||||
{ id: "terminal", label: "Terminal", tall: true, create: () => new TerminalWidget() },
|
||||
{ id: "nightscout", label: "Nightscout", create: () => new NightscoutWidget() },
|
||||
{ id: "compass", label: "Compass", create: () => new CompassWidget() },
|
||||
{ id: "music", label: "Music", create: () => new MusicWidget() },
|
||||
];
|
||||
|
||||
export function findGlanceWidget(id: GlanceWidgetId | string): GlanceWidgetDefinition | null {
|
||||
return GLANCE_WIDGETS.find((widget) => widget.id === id) ?? null;
|
||||
}
|
||||
|
||||
/** Whether a slot choice may span two vertically adjacent slots (see GlanceWidgetDefinition.tall). */
|
||||
export function glanceWidgetSpans(choice: string): boolean {
|
||||
return Boolean(findGlanceWidget(choice)?.tall);
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import { GrayImage } from "../../../graphics/image";
|
||||
import { renderIcon } from "../../../graphics/icons";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
|
||||
import { clamp } from "../../../util/numeric-util";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
const ART_SIZE = 96;
|
||||
/** The idle note: the Music app's icon, at this size and the dimmest shade. */
|
||||
const IDLE_NOTE_SIZE = 64;
|
||||
const IDLE_NOTE_VALUE = 20;
|
||||
let idleNote: GrayImage | null | undefined;
|
||||
|
||||
/**
|
||||
* The music icon reduced to a single dim shade: every pixel the renderer
|
||||
* lit at least half becomes IDLE_NOTE_VALUE, the rest stay transparent
|
||||
* (scaling the antialiased original down would quantize its edges away).
|
||||
* Built once and kept, so drawImage's content-addressed cache holds.
|
||||
*/
|
||||
function idleNoteImage(): GrayImage | null {
|
||||
if (idleNote !== undefined) return idleNote;
|
||||
const icon = renderIcon("music", IDLE_NOTE_SIZE);
|
||||
if (!icon) {
|
||||
idleNote = null;
|
||||
return null;
|
||||
}
|
||||
const note = new GrayImage(icon.width, icon.height, 0);
|
||||
for (let y = 0; y < icon.height; y++) {
|
||||
for (let x = 0; x < icon.width; x++) {
|
||||
if (icon.getPixel(x, y) >= 128) note.setPixel(x, y, IDLE_NOTE_VALUE);
|
||||
}
|
||||
}
|
||||
idleNote = note;
|
||||
return note;
|
||||
}
|
||||
const ART_X = PAD;
|
||||
const PROGRESS_BAR_HEIGHT = 5;
|
||||
|
||||
/**
|
||||
* Now playing: album art, title/artist/album and a progress bar for the
|
||||
* active Android media session, the Music app's header at card size. The
|
||||
* media bridge runs for the whole session; the widget subscribes and ticks
|
||||
* once a second only while something is playing.
|
||||
*/
|
||||
export class MusicWidget implements GlanceWidget {
|
||||
private media: MediaControllerState = mediaControllerBridge.snapshot();
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private progressTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = mediaControllerBridge.onStateChange((state) => {
|
||||
this.media = state;
|
||||
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
|
||||
this.progressTimer ??= setInterval(requestRender, 1_000);
|
||||
} else {
|
||||
this.stopProgressTimer();
|
||||
}
|
||||
requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.stopProgressTimer();
|
||||
}
|
||||
|
||||
private stopProgressTimer(): void {
|
||||
if (this.progressTimer !== null) clearInterval(this.progressTimer);
|
||||
this.progressTimer = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
this.media = media;
|
||||
if (!media.available || (!media.title && !media.artist)) {
|
||||
if (!media.accessEnabled) {
|
||||
// Actionable, so it stays as text.
|
||||
image.drawText(small, PAD, PAD, "Music", 150);
|
||||
image.drawText(small, PAD, PAD + small.lineHeight + 6, "Notification access needed", 140);
|
||||
return;
|
||||
}
|
||||
// Nothing playing: a dim note, centred, and nothing else.
|
||||
const note = idleNoteImage();
|
||||
if (note) image.drawImage(note, ((image.width - note.width) / 2) | 0, ((image.height - note.height) / 2) | 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const artY = Math.max(PAD, ((image.height - ART_SIZE) / 2) | 0);
|
||||
this.drawArt(image, media, artY);
|
||||
|
||||
const textX = ART_X + ART_SIZE + 12;
|
||||
const textWidth = image.width - textX - PAD;
|
||||
let y = artY;
|
||||
image.drawText(medium, textX, y, truncateText(medium, media.title || "Unknown title", textWidth), 230);
|
||||
y += medium.lineHeight + 2;
|
||||
if (media.artist) {
|
||||
image.drawText(small, textX, y, truncateText(small, media.artist, textWidth), 170);
|
||||
y += small.lineHeight + 2;
|
||||
}
|
||||
if (media.album) {
|
||||
image.drawText(small, textX, y, truncateText(small, media.album, textWidth), 120);
|
||||
}
|
||||
|
||||
// Progress pinned to the art's bottom edge; the state label sits beside it.
|
||||
const barY = artY + ART_SIZE - PROGRESS_BAR_HEIGHT;
|
||||
const timeY = barY - small.lineHeight - 3;
|
||||
const stateLabel = media.playbackState === "playing" ? "" : media.playbackState === "paused" ? "Paused" : media.playbackState;
|
||||
if (media.durationMs > 0 && media.positionMs >= 0) {
|
||||
const elapsed = formatMediaTime(media.positionMs);
|
||||
const duration = formatMediaTime(media.durationMs);
|
||||
image.drawText(small, textX, timeY, elapsed, 140);
|
||||
image.drawText(small, textX + textWidth - small.measureText(duration), timeY, duration, 140);
|
||||
if (stateLabel) {
|
||||
image.drawText(small, textX + (((textWidth - small.measureText(stateLabel)) / 2) | 0), timeY, stateLabel, 140);
|
||||
}
|
||||
image.drawRect(textX, barY, textWidth, PROGRESS_BAR_HEIGHT, 55);
|
||||
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
|
||||
image.fillRect(textX + 1, barY + 1, Math.round((textWidth - 2) * progress), PROGRESS_BAR_HEIGHT - 2, 170);
|
||||
} else if (stateLabel) {
|
||||
image.drawText(small, textX, timeY, stateLabel, 140);
|
||||
}
|
||||
}
|
||||
|
||||
private drawArt(image: GrayImage, media: MediaControllerState, artY: number): void {
|
||||
const key = `${media.packageName}|${media.title}|${media.album}`;
|
||||
// Retry while null: players often publish metadata before the bitmap.
|
||||
if (key !== this.artKey || this.art === null) {
|
||||
this.artKey = key;
|
||||
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
|
||||
}
|
||||
if (this.art) {
|
||||
const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0);
|
||||
const dy = artY + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0);
|
||||
image.bitBlt(this.art, dx, dy);
|
||||
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
|
||||
} else {
|
||||
image.drawRect(ART_X, artY, ART_SIZE, ART_SIZE, 60);
|
||||
const font = getDefaultSmallFont();
|
||||
image.drawText(font, ART_X + Math.round((ART_SIZE - font.measureText("no art")) / 2),
|
||||
artY + Math.round((ART_SIZE - font.lineHeight) / 2), "no art", 90);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function formatMediaTime(milliseconds: number): string {
|
||||
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
return hours > 0
|
||||
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
|
||||
: `${minutes}:${String(seconds).padStart(2, "0")}`;
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge";
|
||||
import { isNightscoutSettingsConfigured, loadNightscoutThresholds } from "../../../ui/dashboard-settings";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { formatAgeShortFromTimestamp } from "~/util/date-util";
|
||||
import { evaluateNightscoutAlerts } from "../../nightscout/nightscout-alerts";
|
||||
import {
|
||||
drawDirectionIndicator,
|
||||
drawNightscoutGraph,
|
||||
drawNightscoutValueStrikeThrough,
|
||||
formatDelta,
|
||||
isNightscoutPointStale,
|
||||
} from "../../nightscout/nightscout";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
/** Width of the readout column left of the graph. */
|
||||
const READOUT_WIDTH = 96;
|
||||
|
||||
/**
|
||||
* Current glucose (struck through once older than the app's staleness
|
||||
* limit) with delta, trend, age and IOB beside the Nightscout app's 2-hour
|
||||
* graph, plus any of cannula age, reservoir, pump battery and loop age that
|
||||
* breach the limits in Nightscout settings, inverted as the app shows them. The bridge polls on its own for the whole session, so the
|
||||
* widget only subscribes; a minute tick keeps the age and staleness moving
|
||||
* during a stalled fetch.
|
||||
*/
|
||||
export class NightscoutWidget implements GlanceWidget {
|
||||
private state: NightscoutState = nightscoutBridge.snapshot();
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private tick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = nightscoutBridge.onStateChange((state) => {
|
||||
this.state = state;
|
||||
requestRender();
|
||||
});
|
||||
this.tick = setInterval(requestRender, 60_000);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
if (this.tick !== null) clearInterval(this.tick);
|
||||
this.tick = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const small = getDefaultSmallFont();
|
||||
const large = getDefaultLargeFont();
|
||||
const state = this.state;
|
||||
const nowMs = Date.now();
|
||||
const step = lineStep(small);
|
||||
image.drawText(small, PAD, PAD, "Nightscout", 150);
|
||||
|
||||
if (!isNightscoutSettingsConfigured() || state.configurationMissing) {
|
||||
image.drawText(small, PAD, PAD + step + 4, "Not configured", 140);
|
||||
image.drawText(small, PAD, PAD + 2 * step + 4, "Set the site URL in the Nightscout app", 100);
|
||||
return;
|
||||
}
|
||||
const latest = state.latest;
|
||||
if (!state.available || !latest) {
|
||||
image.drawText(small, PAD, PAD + step + 4, "No data", 140);
|
||||
image.drawText(small, PAD, PAD + 2 * step + 4, truncateText(small, state.status, image.width - 2 * PAD), 100);
|
||||
return;
|
||||
}
|
||||
|
||||
// Readout column: big value, units, then delta + trend, age and IOB.
|
||||
const glucoseText = `${latest.sgv}`;
|
||||
const glucoseY = PAD + step;
|
||||
const glucoseWidth = large.measureText(glucoseText);
|
||||
image.drawText(large, PAD, glucoseY, glucoseText, 230);
|
||||
if (isNightscoutPointStale(latest, nowMs)) {
|
||||
// Same rule as the app: older than NIGHTSCOUT_STALE_MS (15 min) gets a
|
||||
// line through the digits at mid-height.
|
||||
drawNightscoutValueStrikeThrough(image, PAD, glucoseY + (large.lineHeight >> 1), glucoseWidth);
|
||||
}
|
||||
const unitsX = PAD + glucoseWidth + 4;
|
||||
if (unitsX + small.measureText(state.units) <= PAD + READOUT_WIDTH) {
|
||||
image.drawText(small, unitsX, glucoseY + large.lineHeight - small.lineHeight, state.units, 140);
|
||||
}
|
||||
let y = glucoseY + large.lineHeight + 2;
|
||||
const deltaText = `${formatDelta(state.delta)} `;
|
||||
image.drawText(small, PAD, y, deltaText, 180);
|
||||
drawDirectionIndicator(image, small, PAD + small.measureText(deltaText), y, state.direction, 180);
|
||||
y += step;
|
||||
image.drawText(small, PAD, y, `${formatAgeShortFromTimestamp(latest.timestampMs, nowMs)} ago`, 130);
|
||||
y += step;
|
||||
image.drawText(small, PAD, y, `IOB ${state.iob === null ? "--" : state.iob.toFixed(2)}`, 160);
|
||||
y += step;
|
||||
|
||||
// Breached limits only, one per line while they fit, inverted like the
|
||||
// app's warning fields. Nothing here means nothing is out of range.
|
||||
const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs);
|
||||
const warnings: string[] = [];
|
||||
if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`);
|
||||
if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`);
|
||||
if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`);
|
||||
if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`);
|
||||
for (const warning of warnings) {
|
||||
if (y + small.lineHeight > image.height - 2) break;
|
||||
const text = truncateText(small, warning, READOUT_WIDTH - 4);
|
||||
image.fillRect(PAD - 2, y - 1, small.measureText(text) + 4, small.lineHeight + 2, 230);
|
||||
image.drawText(small, PAD, y, text, 1);
|
||||
y += step;
|
||||
}
|
||||
|
||||
const graphX = PAD + READOUT_WIDTH + PAD;
|
||||
drawNightscoutGraph(
|
||||
image,
|
||||
{ x: graphX, y: PAD, width: image.width - graphX - PAD, height: image.height - 2 * PAD },
|
||||
state,
|
||||
nowMs,
|
||||
small,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
import { BATTERY_ICON_WIDTH, drawBattery } from "../../../graphics/battery";
|
||||
import { type GrayImage, type UiFont } from "../../../graphics/image";
|
||||
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { onAndroidNotificationPosted, readActiveNotificationIcons } from "../../../native/notification-icons";
|
||||
import { readPhoneBatteryState } from "../../../native/phone-battery";
|
||||
import { formatClockDate, formatClockTime } from "../../../ui/clock-format";
|
||||
import {
|
||||
batteryDisplayModeSetting,
|
||||
batteryIndicatorVisible,
|
||||
glassesBatteryVisibilitySetting,
|
||||
onAnySettingChanged,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
watchBatteryVisibilitySetting,
|
||||
type BatteryIndicatorVisibility,
|
||||
} from "../../../ui/dashboard-settings";
|
||||
import { shell } from "../../../ui/shell/shell";
|
||||
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../../util/render-freshness";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { timerEngine } from "../../timer/timer-engine";
|
||||
import { formatCountdown, sortTimers, timerDisplayName, timerPhase, timerRemainingMs } from "../../timer/timer-model";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
const NOTIFICATION_ICON_SIZE = 24;
|
||||
const NOTIFICATION_ICON_GAP = 4;
|
||||
const BATTERY_ITEM_GAP = 12;
|
||||
const BATTERY_LABEL_VALUE = 150;
|
||||
|
||||
type BatteryItem = { label: string; percent: number; charging: boolean };
|
||||
|
||||
/**
|
||||
* Date and time, the phone's notification icons, the phone/watch/G2/R1
|
||||
* battery indicators (the Wear OS watch's only while one is reachable), and
|
||||
* the Timers app's countdowns: the top bar's contents plus timers, laid out
|
||||
* for a card. Battery style and per-device visibility
|
||||
* follow Settings > Display > Battery indicators, so the card agrees with
|
||||
* the bar. Countdowns tick once a second only while one is running.
|
||||
*/
|
||||
export class SystemCardWidget implements GlanceWidget {
|
||||
private requestRender: (() => void) | null = null;
|
||||
private minuteTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private secondTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private unsubscribeNotifications: (() => void) | null = null;
|
||||
private unsubscribeSettings: (() => void) | null = null;
|
||||
private unsubscribeTimers: (() => void) | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.requestRender = requestRender;
|
||||
this.scheduleMinuteTick();
|
||||
this.unsubscribeNotifications = onAndroidNotificationPosted(() => this.requestRender?.());
|
||||
this.unsubscribeSettings = onAnySettingChanged(() => this.requestRender?.());
|
||||
this.unsubscribeTimers = timerEngine.onChange(() => {
|
||||
this.syncSecondTick();
|
||||
this.requestRender?.();
|
||||
});
|
||||
this.syncSecondTick();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.requestRender = null;
|
||||
if (this.minuteTimer !== null) clearTimeout(this.minuteTimer);
|
||||
this.minuteTimer = null;
|
||||
if (this.secondTimer !== null) clearInterval(this.secondTimer);
|
||||
this.secondTimer = null;
|
||||
this.unsubscribeNotifications?.();
|
||||
this.unsubscribeNotifications = null;
|
||||
this.unsubscribeSettings?.();
|
||||
this.unsubscribeSettings = null;
|
||||
this.unsubscribeTimers?.();
|
||||
this.unsubscribeTimers = null;
|
||||
}
|
||||
|
||||
/** Countdowns change every second; run that clock only while one is running. */
|
||||
private syncSecondTick(): void {
|
||||
const running = timerEngine.timers().some((timer) => timerPhase(timer) === "running");
|
||||
if (running && this.secondTimer === null) {
|
||||
this.secondTimer = setInterval(() => this.requestRender?.(), 1_000);
|
||||
} else if (!running && this.secondTimer !== null) {
|
||||
clearInterval(this.secondTimer);
|
||||
this.secondTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Repaint on the minute boundary, so the clock never shows a stale minute. */
|
||||
private scheduleMinuteTick(): void {
|
||||
if (this.minuteTimer !== null) clearTimeout(this.minuteTimer);
|
||||
const now = Date.now();
|
||||
const untilNextMinute = 60_000 - (now % 60_000) + 50;
|
||||
this.minuteTimer = setTimeout(() => {
|
||||
this.minuteTimer = null;
|
||||
if (!this.requestRender) return;
|
||||
this.requestRender();
|
||||
this.scheduleMinuteTick();
|
||||
}, untilNextMinute);
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const large = getDefaultLargeFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const now = new Date();
|
||||
|
||||
// Clock and date down the left; batteries take the right edge.
|
||||
const batteryLeft = drawBatteryBlock(image, small, image.width - PAD, PAD);
|
||||
const textWidth = Math.max(0, batteryLeft - BATTERY_ITEM_GAP - PAD);
|
||||
const timeText = formatClockTime(now);
|
||||
const timeFont = large.measureText(timeText) <= textWidth ? large : medium;
|
||||
let y = PAD;
|
||||
image.drawText(timeFont, PAD, y, timeText, 230);
|
||||
y += timeFont.lineHeight + 2;
|
||||
image.drawText(medium, PAD, y, formatClockDate(now), 170);
|
||||
y += medium.lineHeight + 4;
|
||||
|
||||
// Notification icons along the bottom, as many as fit; the Timers app's
|
||||
// countdowns take the band between the date and the icons.
|
||||
const iconY = image.height - PAD - NOTIFICATION_ICON_SIZE;
|
||||
drawTimers(image, small, PAD, y, textWidth, iconY - 4, now.getTime());
|
||||
const maxIcons = Math.max(0, ((image.width - 2 * PAD + NOTIFICATION_ICON_GAP) / (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP)) | 0);
|
||||
if (maxIcons > 0) {
|
||||
const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData());
|
||||
if (stale) noteStaleDataUsed();
|
||||
for (let index = 0; index < icons.length; index++) {
|
||||
image.drawImage(icons[index]!, PAD + index * (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP), iconY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function collectBatteryItems(): BatteryItem[] {
|
||||
const items: BatteryItem[] = [];
|
||||
const push = (visibility: BatteryIndicatorVisibility, label: string, percent: number | null, charging: boolean | null) => {
|
||||
if (percent === null || !Number.isFinite(percent)) return;
|
||||
const clamped = Math.max(0, Math.min(100, Math.round(percent)));
|
||||
if (!batteryIndicatorVisible(visibility, clamped)) return;
|
||||
items.push({ label, percent: clamped, charging: Boolean(charging) });
|
||||
};
|
||||
const phone = readPhoneBatteryState();
|
||||
push(phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, phone.charging);
|
||||
const levels = shell.getBatteryLevels();
|
||||
push(watchBatteryVisibilitySetting.get(), "Watch", levels.watch, levels.watchCharging);
|
||||
push(glassesBatteryVisibilitySetting.get(), "G2", levels.headset, levels.headsetCharging);
|
||||
if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) {
|
||||
push(ringBatteryVisibilitySetting.get(), "R1", levels.ring, levels.ringCharging);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* The battery indicators right-aligned at `right`, in the configured style.
|
||||
* Side-by-side styles put the label beside the gauge (or percentage) on one
|
||||
* line; stacked styles centre the label above it. Returns the block's left
|
||||
* edge (or `right` when nothing is shown).
|
||||
*/
|
||||
function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: number): number {
|
||||
const items = collectBatteryItems();
|
||||
if (!items.length) return right;
|
||||
const mode = batteryDisplayModeSetting.get();
|
||||
const percentage = mode === "percentage" || mode === "stacked-percentage";
|
||||
const stacked = mode === "stacked" || mode === "stacked-percentage";
|
||||
const gaugeHeight = drawBattery(0, false).height;
|
||||
let x = right;
|
||||
for (let index = items.length - 1; index >= 0; index--) {
|
||||
const item = items[index]!;
|
||||
const percentText = `${item.percent}%`;
|
||||
const labelWidth = font.measureText(item.label);
|
||||
const valueWidth = percentage ? font.measureText(percentText) : BATTERY_ICON_WIDTH;
|
||||
if (stacked) {
|
||||
const itemWidth = Math.max(labelWidth, valueWidth);
|
||||
x -= itemWidth;
|
||||
image.drawText(font, x + (((itemWidth - labelWidth) / 2) | 0), top, item.label, BATTERY_LABEL_VALUE);
|
||||
drawBatteryValue(image, font, x + (((itemWidth - valueWidth) / 2) | 0), top + font.lineHeight + 2, item, percentage, gaugeHeight);
|
||||
} else {
|
||||
x -= labelWidth + 5 + valueWidth;
|
||||
image.drawText(font, x, top, item.label, BATTERY_LABEL_VALUE);
|
||||
drawBatteryValue(image, font, x + labelWidth + 5, top, item, percentage, gaugeHeight);
|
||||
}
|
||||
x -= BATTERY_ITEM_GAP;
|
||||
}
|
||||
return x + BATTERY_ITEM_GAP;
|
||||
}
|
||||
|
||||
function drawBatteryValue(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
x: number,
|
||||
lineTop: number,
|
||||
item: BatteryItem,
|
||||
percentage: boolean,
|
||||
gaugeHeight: number,
|
||||
): void {
|
||||
if (percentage) {
|
||||
const percentText = `${item.percent}%`;
|
||||
if (item.charging) {
|
||||
// Inverted text marks charging, as in the top bar.
|
||||
image.fillRect(x - 2, lineTop - 1, font.measureText(percentText) + 4, font.lineHeight + 2, 255);
|
||||
image.drawText(font, x, lineTop, percentText, 1);
|
||||
} else {
|
||||
image.drawText(font, x, lineTop, percentText, 200);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const icon = drawBattery(item.percent, item.charging);
|
||||
image.bitBlt(icon, x, lineTop + Math.max(0, ((font.lineHeight - gaugeHeight) / 2) | 0), { transparentZero: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* The Timers app's countdowns, soonest first: remaining time (or "Done" once
|
||||
* rung, inverted so it is noticed; "paused" for a paused one) and the
|
||||
* timer's name, one per line while they fit between `top` and `bottom`.
|
||||
*/
|
||||
function drawTimers(image: GrayImage, font: UiFont, x: number, top: number, width: number, bottom: number, nowMs: number): void {
|
||||
const timers = sortTimers(timerEngine.timers(), nowMs);
|
||||
const step = lineStep(font);
|
||||
let y = top;
|
||||
for (const timer of timers) {
|
||||
if (y + font.lineHeight > bottom) break;
|
||||
const phase = timerPhase(timer);
|
||||
const clock = phase === "rung" ? "Done" : formatCountdown(timerRemainingMs(timer, nowMs));
|
||||
const clockWidth = font.measureText(clock);
|
||||
if (phase === "rung") {
|
||||
image.fillRect(x - 2, y - 1, clockWidth + 4, font.lineHeight + 2, 230);
|
||||
image.drawText(font, x, y, clock, 1);
|
||||
} else {
|
||||
image.drawText(font, x, y, clock, phase === "paused" ? 120 : 200);
|
||||
}
|
||||
const nameX = x + clockWidth + 8;
|
||||
const name = phase === "paused" ? `${timerDisplayName(timer)} (paused)` : timerDisplayName(timer);
|
||||
image.drawText(font, nameX, y, truncateText(font, name, Math.max(0, x + width - nameX)), 160);
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { onWorkerStateChanged, readWorkerState } from "../../../ui/shell/worker-state";
|
||||
import {
|
||||
drawSessionRow,
|
||||
isSessionRowActive,
|
||||
SESSION_ACTIVITY_STEP_MS,
|
||||
TERMINAL_SESSIONS_STATE_KEY,
|
||||
type TerminalSessionsSnapshot,
|
||||
} from "../../terminal/session-list";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
const GUTTER_X = PAD;
|
||||
/** A row fits while its glyph line clears this margin above the bottom edge. */
|
||||
const BOTTOM_MARGIN = 2;
|
||||
|
||||
/**
|
||||
* The Terminal app's session list at card size: the same hosts, sessions,
|
||||
* activity indicators and open-window numbers as the hub window, painted
|
||||
* with the same row painter from the snapshot the Terminal worker publishes.
|
||||
* The indicator animates on the hub's cadence while any session is active;
|
||||
* each row carries its own activity deadline, so nothing needs the worker
|
||||
* once the snapshot is in.
|
||||
*/
|
||||
export class TerminalWidget implements GlanceWidget {
|
||||
private snapshot: TerminalSessionsSnapshot | undefined = readWorkerState(TERMINAL_SESSIONS_STATE_KEY);
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private requestRender: (() => void) | null = null;
|
||||
private phase = 0;
|
||||
private animationTimer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.requestRender = requestRender;
|
||||
this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY);
|
||||
this.unsubscribe = onWorkerStateChanged<TerminalSessionsSnapshot>(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => {
|
||||
this.snapshot = snapshot;
|
||||
this.syncAnimation();
|
||||
requestRender();
|
||||
});
|
||||
this.syncAnimation();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.requestRender = null;
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
if (this.animationTimer !== null) clearInterval(this.animationTimer);
|
||||
this.animationTimer = null;
|
||||
}
|
||||
|
||||
private anyActive(nowMs = Date.now()): boolean {
|
||||
return Boolean(this.snapshot?.hosts.some((host) => host.sessions.some((row) => isSessionRowActive(row, nowMs))));
|
||||
}
|
||||
|
||||
/** Run the indicator clock only while something is active; the last tick clears expired ones. */
|
||||
private syncAnimation(): void {
|
||||
const shouldRun = this.anyActive();
|
||||
if (shouldRun && this.animationTimer === null) {
|
||||
this.phase = 0;
|
||||
this.animationTimer = setInterval(() => {
|
||||
this.phase = (this.phase + 1) % 2;
|
||||
this.requestRender?.();
|
||||
if (!this.anyActive()) this.syncAnimation();
|
||||
}, SESSION_ACTIVITY_STEP_MS);
|
||||
} else if (!shouldRun && this.animationTimer !== null) {
|
||||
clearInterval(this.animationTimer);
|
||||
this.animationTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const step = lineStep(font);
|
||||
const textWidth = image.width - 2 * PAD;
|
||||
const snapshot = this.snapshot;
|
||||
const nowMs = Date.now();
|
||||
let y = PAD;
|
||||
image.drawText(font, PAD, y, "Terminal", 150);
|
||||
y += step;
|
||||
|
||||
if (!snapshot) {
|
||||
image.drawText(font, PAD, y, "Open the Terminal app to connect", 120);
|
||||
return;
|
||||
}
|
||||
const hosts = snapshot.hosts;
|
||||
const sessionCount = hosts.reduce((sum, host) => sum + host.sessions.length, 0);
|
||||
if (!hosts.length || !sessionCount) {
|
||||
const text = !snapshot.configured
|
||||
? "No connections configured"
|
||||
: !hosts.length
|
||||
? snapshot.status
|
||||
: "No live sessions";
|
||||
image.drawText(font, PAD, y, truncateText(font, text, textWidth), 120);
|
||||
return;
|
||||
}
|
||||
|
||||
// Rows are placed a line step apart but only need their line height to
|
||||
// fit, so the last one may sit right against the bottom margin.
|
||||
const fits = (top: number) => top + font.lineHeight <= image.height - BOTTOM_MARGIN;
|
||||
const multiHost = hosts.length > 1;
|
||||
for (const host of hosts) {
|
||||
if (multiHost) {
|
||||
if (!fits(y)) return;
|
||||
image.drawText(font, PAD, y, truncateText(font, host.name, textWidth), 110);
|
||||
y += step;
|
||||
}
|
||||
for (const row of host.sessions) {
|
||||
if (!fits(y)) return;
|
||||
drawSessionRow(image, font, {
|
||||
x: GUTTER_X,
|
||||
y,
|
||||
width: image.width - GUTTER_X - PAD,
|
||||
label: row.label,
|
||||
openGlyph: row.openGlyph,
|
||||
active: isSessionRowActive(row, nowMs),
|
||||
phase: this.phase,
|
||||
value: 200,
|
||||
});
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ const DEFAULT_ASSIGNMENTS: Record<string, string> = {
|
||||
freecell: "Games",
|
||||
minesweeper: "Games",
|
||||
pinball: "Games",
|
||||
flappy: "Games",
|
||||
calculator: "Prototypes",
|
||||
microphones: "Prototypes",
|
||||
nightscout: "Prototypes",
|
||||
|
||||
@@ -20,8 +20,9 @@ declare const com: any;
|
||||
* Host-side controller for the CFW mic_control array: sends the same config
|
||||
* to both temples (each temple is its own endpoint and its own 2-mic array),
|
||||
* tracks per-temple field-104 status, and keeps the 90 s streaming lease
|
||||
* renewed while capture is armed. Degrades cleanly on firmware without the
|
||||
* feature: micControlSupported() is false and apply() refuses to arm.
|
||||
* renewed while capture is armed. The feature is part of the required
|
||||
* firmware revision, so the only gate is the developer setting:
|
||||
* micControlSupported() is false and apply() refuses to arm when it is off.
|
||||
*/
|
||||
|
||||
const CONFIG_SETTING_KEY = "microphones.array-config";
|
||||
@@ -38,26 +39,13 @@ function activeCommunicator(): any {
|
||||
}
|
||||
}
|
||||
|
||||
/** True when the connected glasses advertise the CFW mic_control feature. */
|
||||
export function micControlAdvertised(): boolean {
|
||||
const communicator = activeCommunicator();
|
||||
if (!communicator) return false;
|
||||
try {
|
||||
const caps = String(communicator.getFirmwareCapabilities() ?? "");
|
||||
return caps.trim().split(/\s+/).includes("micctl");
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the connected glasses advertise the CFW mic_control feature AND
|
||||
* the "Use microphone control" developer setting is on. With the setting off
|
||||
* (the default), Faceclaw behaves exactly as it would on firmware without the
|
||||
* micctl caps token.
|
||||
* True when the "Use microphone control" developer setting is on. With the
|
||||
* setting off, Faceclaw behaves exactly as it would on firmware without the
|
||||
* mic-control channel (one mixed stream).
|
||||
*/
|
||||
export function micControlSupported(): boolean {
|
||||
return useMicControlSetting.get() && micControlAdvertised();
|
||||
return useMicControlSetting.get();
|
||||
}
|
||||
|
||||
export function loadMicConfig(): MicConfig {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
/**
|
||||
* Wire codec for the CFW mic_control feature (g2flash `microphone-configurations`
|
||||
* branch, EVENCFW/16, caps tokens micctl/micmc/micraw). Mirrors the contract
|
||||
* header in g2flash patches/mic_control.c:
|
||||
* Wire codec for the CFW mic_control feature. Mirrors the contract header in
|
||||
* g2flash patches/mic_control.c:
|
||||
*
|
||||
* field 103 (write) ['M','C', ver, op, <9 config bytes for CONFIGURE>]
|
||||
* field 104 (read) 21-byte ['M','C', ver, active, src, chanMask, codec,
|
||||
|
||||
@@ -730,7 +730,10 @@ class MicSession {
|
||||
},
|
||||
});
|
||||
engine.setListener(this.captionListenerProxy);
|
||||
engine.setAsrModelDir(isAsrModelReady() ? moonshineModelDir() : null);
|
||||
// Live captions are a separate sherpa-onnx pipeline (FaceclawCaptionEngine)
|
||||
// that only ever uses Moonshine; unaffected by the two-model on-device
|
||||
// Whisper addition in native/voice-control.ts's push-to-talk path.
|
||||
engine.setAsrModelDir(isAsrModelReady("moonshine") ? moonshineModelDir() : null);
|
||||
engine.setSpeakerModelPath(micModelPath("speaker-embedding"));
|
||||
engine.start();
|
||||
this.captionEngine = engine;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { isAsrModelReady, startAsrModelDownload, asrModelState } from "../../nat
|
||||
import { CaptionsLayer } from "./captions-layer";
|
||||
import { LevelsLayer } from "./levels-layer";
|
||||
import { RadarLayer } from "./radar-layer";
|
||||
import { loadMicConfig, micArrayController, micControlAdvertised, micControlSupported, saveMicConfig } from "./mic-control";
|
||||
import { loadMicConfig, micArrayController, micControlSupported, saveMicConfig } from "./mic-control";
|
||||
import { isMicModelReady, micModelState, startMicModelDownload } from "./mic-models";
|
||||
import { micChannelLabel, type MicChannelKey, type MicConfig } from "./mic-protocol";
|
||||
import { micSession } from "./mic-session";
|
||||
@@ -166,7 +166,7 @@ function micSetupMenu(): MenuLayer {
|
||||
{
|
||||
label: "Array status",
|
||||
description:
|
||||
"Per-temple mic-control status. Requires the mic-control custom firmware (caps token micctl) and the Use microphone control developer setting.",
|
||||
"Per-temple mic-control status. Requires the Use microphone control developer setting.",
|
||||
onSelect: (ctx) => {
|
||||
micArrayController.query();
|
||||
const { left, right } = micArrayController.getStatuses();
|
||||
@@ -186,9 +186,7 @@ function micSetupMenu(): MenuLayer {
|
||||
"Array status",
|
||||
micControlSupported()
|
||||
? `${describe("Left", left)}\n${describe("Right", right)}\n${consistency}`
|
||||
: micControlAdvertised()
|
||||
? "Mic-control is available but disabled. Turn on Settings > Developer > Use microphone control to control the four mics individually."
|
||||
: "The connected firmware does not advertise mic-control. Flash the microphone-configurations CFW build to control the four mics individually; on stock firmware the glasses stream one mixed channel with a firmware-computed direction of arrival.",
|
||||
: "Mic-control is disabled. Turn on Settings > Developer > Use microphone control to control the four mics individually; while it is off the glasses stream one mixed channel with a firmware-computed direction of arrival.",
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -330,17 +328,20 @@ function voiceSpeakersMenu(): MenuLayer {
|
||||
() => startMicModelDownload("speaker-embedding"),
|
||||
),
|
||||
modelItem(
|
||||
// Live captions always use Moonshine (see the matching note in
|
||||
// mic-session.ts); the second on-device model (Whisper) is a
|
||||
// push-to-talk dictation option only, managed in Settings > Voice.
|
||||
"Caption ASR model",
|
||||
isAsrModelReady,
|
||||
() => isAsrModelReady("moonshine"),
|
||||
() => {
|
||||
const state = asrModelState();
|
||||
const state = asrModelState("moonshine");
|
||||
if (state.status === "ready") return "ready";
|
||||
if (state.status === "downloading") {
|
||||
return `${Math.round((state.bytesDownloaded / state.totalBytes) * 100)}%`;
|
||||
}
|
||||
return "download (141 MB)";
|
||||
},
|
||||
startAsrModelDownload,
|
||||
() => startAsrModelDownload("moonshine"),
|
||||
),
|
||||
modelItem(
|
||||
"Re-diarization model",
|
||||
|
||||
@@ -9,9 +9,13 @@
|
||||
* scroll moves along the row, click reveals (or chords a satisfied number),
|
||||
* long-press toggles a flag, double-click returns to row-select.
|
||||
* Watch swipes skip the two-layer scheme and move the cell cursor in four
|
||||
* directions; a watch double-click pauses directly.
|
||||
* directions; a watch double-click pauses directly. Tap-then-hold opens the
|
||||
* window menu in any phase, pausing a live game first.
|
||||
* Paused/won/lost: click resumes or starts a new game, double-click yields
|
||||
* focus, long-press opens the window menu.
|
||||
* focus. Losing input focus mid-game (a shell overlay such as the system
|
||||
* menu or a notification, focus to the sidebar) pauses, as does
|
||||
* backgrounding or screen-off. The difficulty setting persists across
|
||||
* launches.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
@@ -19,9 +23,11 @@ import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../gra
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -55,6 +61,8 @@ const DIFFICULTIES = [
|
||||
{ name: "Medium", mines: 20 },
|
||||
{ name: "Hard", mines: 26 },
|
||||
] as const;
|
||||
/** Persisted difficulty, stored by name so a reordering can't misfile it. */
|
||||
const DIFFICULTY_KEY = "minesweeper.difficulty";
|
||||
|
||||
/** Number glyph shades by adjacent-mine count; higher counts read brighter. */
|
||||
const COUNT_SHADES = [0, 140, 170, 200, 220, 235, 245, 250, 250];
|
||||
@@ -155,7 +163,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
focused: false,
|
||||
menu: null,
|
||||
phase: "playing",
|
||||
difficultyIndex: 0,
|
||||
difficultyIndex: loadDifficultyIndex(),
|
||||
mines: new Uint8Array(COLS * ROWS),
|
||||
counts: new Uint8Array(COLS * ROWS),
|
||||
cellState: new Uint8Array(COLS * ROWS),
|
||||
@@ -169,7 +177,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
elapsedMs: 0,
|
||||
runningSinceMs: null,
|
||||
tickTimer: null,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("minesweeper"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
windows.set(message.windowId, window);
|
||||
@@ -215,6 +223,19 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away (the system menu, a notification
|
||||
// modal, the voice dialog, focus back to the sidebar) pauses, so the
|
||||
// clock stops and the board is hidden while the player can't act.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncClock(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
@@ -237,6 +258,24 @@ function inferForeground(window: MinesweeperWindow, focused: boolean): void {
|
||||
syncClock(window);
|
||||
}
|
||||
|
||||
function loadDifficultyIndex(): number {
|
||||
try {
|
||||
const name = getStringSetting(DIFFICULTY_KEY, "");
|
||||
const index = DIFFICULTIES.findIndex((difficulty) => difficulty.name === name);
|
||||
return index >= 0 ? index : 0;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function saveDifficultyIndex(window: MinesweeperWindow): void {
|
||||
try {
|
||||
setStringSetting(DIFFICULTY_KEY, DIFFICULTIES[window.difficultyIndex]!.name);
|
||||
} catch (error) {
|
||||
console.warn(`minesweeper difficulty save failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
|
||||
* steps on its own timer, and the Java call is safe from the worker thread.
|
||||
@@ -252,7 +291,7 @@ function playSfx(window: MinesweeperWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for flagging. */
|
||||
/** The window's context menu (game actions). Tap-then-hold reaches it in any phase; playing keeps long-press for flagging. */
|
||||
function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
const nextDifficulty = DIFFICULTIES[(window.difficultyIndex + 1) % DIFFICULTIES.length]!;
|
||||
return [
|
||||
@@ -269,6 +308,7 @@ function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.difficultyIndex = (window.difficultyIndex + 1) % DIFFICULTIES.length;
|
||||
saveDifficultyIndex(window);
|
||||
resetGame(window);
|
||||
playSfx(window, SFX_RESUME);
|
||||
},
|
||||
@@ -278,6 +318,7 @@ function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("minesweeper", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
@@ -290,7 +331,7 @@ function windowMenu(window: MinesweeperWindow): WindowMenu {
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
items: () => windowMenuItems(window),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
@@ -367,7 +408,11 @@ function handlePlayingInput(window: MinesweeperWindow, event: InputEvent, frameI
|
||||
toggleFlag(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
// The menu's actions (new game, difficulty) all discard the board, and
|
||||
// the clock must not run under it, so pause first; the board is hidden
|
||||
// while paused, so the menu can't be used to study it off the clock.
|
||||
window.phase = "paused";
|
||||
syncClock(window);
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
|
||||
@@ -68,7 +68,7 @@ class MusicAppLayer implements Layer {
|
||||
if (!media.accessEnabled) {
|
||||
const lines = wrapText(
|
||||
font,
|
||||
"Notification access is required before Android exposes media sessions. Click to open settings.",
|
||||
"Notification access is required to control Android media sessions. Tap to open settings.",
|
||||
width - 48,
|
||||
);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
|
||||
@@ -28,12 +28,14 @@ import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
|
||||
import type { LayerContext } from "../../ui/layers";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||
import { openUrlOnPhone } from "../../native/open-url";
|
||||
import {
|
||||
navigateDestinationAddressDraftSetting,
|
||||
navigateDestinationNameDraftSetting,
|
||||
navigateRememberRecentSetting,
|
||||
navigateDisplayModeSetting,
|
||||
navigateVerticalPositionSetting,
|
||||
mapboxApiKeySetting,
|
||||
enumSettingMenuItem,
|
||||
type ConfigSettingString,
|
||||
} from "../../ui/dashboard-settings";
|
||||
@@ -68,6 +70,7 @@ import {
|
||||
fetchStaticMapGray,
|
||||
geocodeForward,
|
||||
isMapboxConfigured,
|
||||
MAPBOX_TOKEN_SETTING_KEY,
|
||||
type GeocodeCandidate,
|
||||
type Route,
|
||||
type RouteProfile,
|
||||
@@ -190,10 +193,14 @@ type NavTarget =
|
||||
| { kind: "query"; query: string; label?: string }
|
||||
| { kind: "place"; name: string; place: string; longitude: number; latitude: number };
|
||||
|
||||
/** An entry on the idle page's destination list. */
|
||||
/**
|
||||
* An entry on the idle page's list: a destination, or (while no Mapbox
|
||||
* token is set) one of the setup actions offered in its place.
|
||||
*/
|
||||
type IdleEntry =
|
||||
| { kind: "saved"; destination: SavedDestination }
|
||||
| { kind: "recent"; destination: RecentDestination };
|
||||
| { kind: "recent"; destination: RecentDestination }
|
||||
| { kind: "action"; label: string; detail: string; run: () => void };
|
||||
|
||||
/** Idle-page list selection (index into idleEntries()). */
|
||||
let idleSelection = 0;
|
||||
@@ -208,6 +215,8 @@ type DestinationEdit = {
|
||||
setting: ConfigSettingString;
|
||||
title: string;
|
||||
onDone: (value: string) => void;
|
||||
/** Runs on double-click (cancel); edits of a live setting restore it here. */
|
||||
onCancel?: () => void;
|
||||
};
|
||||
let editing: DestinationEdit | null = null;
|
||||
|
||||
@@ -251,7 +260,9 @@ function post(message: WorkerAppReply): void {
|
||||
// Destinations edited on the phone (the text editor, or the Settings app's
|
||||
// Home/Work rows) repaint the idle list / the edit screen live.
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key.startsWith("navigate.")) render();
|
||||
// The token key repaints too: the phone editor writes it live during
|
||||
// Edit token, and the idle page changes shape once one is set.
|
||||
if (key.startsWith("navigate.") || key === MAPBOX_TOKEN_SETTING_KEY) render();
|
||||
});
|
||||
|
||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||
@@ -374,7 +385,7 @@ function startNavigation(query: string, requestedProfile: RouteProfile): Promise
|
||||
async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfile): Promise<string> {
|
||||
const query = target.kind === "query" ? target.query : target.name;
|
||||
if (!isMapboxConfigured()) {
|
||||
statusMessage = "Set a Mapbox token in Settings > API Keys.";
|
||||
statusMessage = "Navigation needs a Mapbox token. This is free (up to a usage limit); open Mapbox in a browser on your phone or pick Edit token below.";
|
||||
phase = "idle";
|
||||
render();
|
||||
throw new Error(statusMessage);
|
||||
@@ -1036,8 +1047,13 @@ function closeMenusAnd(ctx: LayerContext, action: () => void): void {
|
||||
* Open the phone text editor on a staging setting. The editor writes the
|
||||
* draft live (repainted via the settings listener); click here confirms.
|
||||
*/
|
||||
function beginEdit(setting: ConfigSettingString, title: string, onDone: (value: string) => void): void {
|
||||
editing = { setting, title, onDone };
|
||||
function beginEdit(
|
||||
setting: ConfigSettingString,
|
||||
title: string,
|
||||
onDone: (value: string) => void,
|
||||
onCancel?: () => void,
|
||||
): void {
|
||||
editing = { setting, title, onDone, onCancel };
|
||||
post({ type: "start-text-setting-edit", settingId: setting.id });
|
||||
render();
|
||||
}
|
||||
@@ -1047,6 +1063,7 @@ function finishEdit(confirmed: boolean): void {
|
||||
editing = null;
|
||||
post({ type: "end-text-setting-edit" });
|
||||
if (current && confirmed) current.onDone(current.setting.get());
|
||||
else if (current) current.onCancel?.();
|
||||
// onDone may have started the next step; only fully leave when it didn't.
|
||||
if (!editing) {
|
||||
navigateDestinationNameDraftSetting.set("");
|
||||
@@ -1075,10 +1092,52 @@ function beginAddDestination(): void {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit the Mapbox token in place: the phone editor writes the real setting
|
||||
* live, so cancelling puts back whatever was there before.
|
||||
*/
|
||||
function beginTokenEdit(): void {
|
||||
const previous = mapboxApiKeySetting.get();
|
||||
beginEdit(
|
||||
mapboxApiKeySetting,
|
||||
mapboxApiKeySetting.glassesEditTitle,
|
||||
() => {
|
||||
statusMessage = "";
|
||||
},
|
||||
() => mapboxApiKeySetting.set(previous),
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Idle page destination list
|
||||
|
||||
/** Where a user gets a token: Mapbox's access-tokens page (sign-up/login first when needed). */
|
||||
const MAPBOX_TOKENS_URL = "https://account.mapbox.com/access-tokens/";
|
||||
|
||||
/** Offered in place of the destination list until a Mapbox token is set. */
|
||||
function tokenSetupEntries(): IdleEntry[] {
|
||||
return [
|
||||
{
|
||||
kind: "action",
|
||||
label: "Open mapbox.com",
|
||||
detail: "Get a free token in the phone browser",
|
||||
run: () => {
|
||||
statusMessage = openUrlOnPhone(MAPBOX_TOKENS_URL)
|
||||
? "Opened mapbox.com on your phone. Copy your public token (pk...) and pick Edit token."
|
||||
: "Could not open a browser on the phone. Visit account.mapbox.com/access-tokens to get a token.";
|
||||
},
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Edit token",
|
||||
detail: "Type or paste it in the phone app",
|
||||
run: () => beginTokenEdit(),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function idleEntries(): IdleEntry[] {
|
||||
if (!isMapboxConfigured()) return tokenSetupEntries();
|
||||
const entries: IdleEntry[] = loadSavedDestinations()
|
||||
.filter((destination) => destination.address)
|
||||
.map((destination): IdleEntry => ({ kind: "saved", destination }));
|
||||
@@ -1094,6 +1153,10 @@ function clampIdleSelection(): void {
|
||||
}
|
||||
|
||||
function navigateToIdleEntry(entry: IdleEntry): void {
|
||||
if (entry.kind === "action") {
|
||||
entry.run();
|
||||
return;
|
||||
}
|
||||
const target: NavTarget =
|
||||
entry.kind === "saved"
|
||||
? { kind: "query", query: entry.destination.address, label: entry.destination.name }
|
||||
@@ -1233,8 +1296,9 @@ function paintIdle(image: GrayImage, win: NavWindow): void {
|
||||
image.drawText(mediumFont, 24, 16, "Navigate", 245);
|
||||
const busy = phase !== "idle";
|
||||
const entries = busy ? [] : idleEntries();
|
||||
const hint = !isMapboxConfigured()
|
||||
? "Set a Mapbox token in Settings > API Keys to enable navigation."
|
||||
const configured = isMapboxConfigured();
|
||||
const hint = !configured
|
||||
? "Navigation needs a Mapbox public token (free at mapbox.com). Get one, then enter it here or in Settings > API Keys."
|
||||
: entries.length
|
||||
? "Pick a destination below, or say one via Voice input (system menu, long-press)."
|
||||
: "Ask the voice assistant to navigate somewhere, or pick Voice input from the system menu (long-press) to say a destination. Save Home, Work and other places from the app menu.";
|
||||
@@ -1255,7 +1319,7 @@ function paintIdle(image: GrayImage, win: NavWindow): void {
|
||||
paintIdleList(image, win, entries, y);
|
||||
drawFooter(
|
||||
image,
|
||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} go ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`,
|
||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} ${configured ? "go" : "choose"} ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`,
|
||||
);
|
||||
} else {
|
||||
drawFooter(image, `${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`);
|
||||
@@ -1281,7 +1345,18 @@ function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], t
|
||||
if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused);
|
||||
const textX = rowX + 6;
|
||||
const textY = y + LIST_ROW_TEXT_INSET;
|
||||
if (entry.kind === "saved") {
|
||||
if (entry.kind === "action") {
|
||||
const label = truncateText(smallFont, entry.label, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, entry.detail, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
} else if (entry.kind === "saved") {
|
||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
|
||||
@@ -24,11 +24,11 @@ import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
|
||||
const nightscoutLargeFont = getDefaultLargeFont();
|
||||
const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
|
||||
export const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
|
||||
const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
|
||||
const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
|
||||
|
||||
function drawDirectionIndicator(
|
||||
export function drawDirectionIndicator(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
x: number,
|
||||
@@ -85,7 +85,7 @@ function truncateLine(text: string, maxChars: number): string {
|
||||
return `${text.slice(0, Math.max(0, maxChars - 3))}...`;
|
||||
}
|
||||
|
||||
function formatDelta(delta: number | null): string {
|
||||
export function formatDelta(delta: number | null): string {
|
||||
if (delta === null) return "--";
|
||||
return `${delta >= 0 ? "+" : ""}${Math.round(delta)}`;
|
||||
}
|
||||
@@ -98,7 +98,7 @@ function formatBolusLabel(value: number): string {
|
||||
return Number.isInteger(value) ? `${value}` : value.toFixed(1);
|
||||
}
|
||||
|
||||
function drawNightscoutGraph(
|
||||
export function drawNightscoutGraph(
|
||||
image: GrayImage,
|
||||
bounds: { x: number; y: number; width: number; height: number },
|
||||
nightscout: NightscoutState,
|
||||
@@ -431,10 +431,10 @@ export function nightscoutMenuItems(): MenuItem[] {
|
||||
];
|
||||
}
|
||||
|
||||
function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
|
||||
export function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
|
||||
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
|
||||
}
|
||||
|
||||
function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
|
||||
export function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
|
||||
image.drawLine(x, y, x + width, y, 180);
|
||||
}
|
||||
@@ -13,11 +13,12 @@
|
||||
*
|
||||
* Controls (ball ready): scroll sets launch power, click launches. In play:
|
||||
* click flips both flippers, scroll-up/down flips left/right individually,
|
||||
* long-press nudges the table (three quick nudges tilt), double-click pauses.
|
||||
* Watch swipes: left/right work the matching flipper, up nudges (or raises
|
||||
* launch power at the plunger), down lowers launch power.
|
||||
* double-click pauses. Watch swipes: left/right work the matching flipper,
|
||||
* up/down raise and lower launch power at the plunger.
|
||||
* Paused/game over: click resumes or starts a new game, double-click yields
|
||||
* focus, long-press opens the window menu.
|
||||
* focus, long-press opens the window menu. Losing input focus mid-game (a
|
||||
* shell overlay such as the system menu or a notification, focus to the
|
||||
* sidebar) pauses, as does backgrounding or screen-off.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
@@ -29,6 +30,7 @@ import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -36,7 +38,6 @@ import {
|
||||
directionalFallback,
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
GESTURE_LONG_PRESS,
|
||||
GESTURE_SCROLL,
|
||||
type InputEvent,
|
||||
} from "../../ui/gestures";
|
||||
@@ -77,11 +78,6 @@ const RENDER_TICK_MS = 50;
|
||||
const LAUNCH_SPEEDS = [320, 380, 440, 500, 560] as const;
|
||||
const BUMPER_KICK = 340;
|
||||
const SLING_KICK = 300;
|
||||
const NUDGE_KICK_UP = 70;
|
||||
const NUDGE_KICK_SIDE = 45;
|
||||
/** Nudges add 1 heat each and decay slowly; reaching 3 tilts the ball away. */
|
||||
const TILT_LIMIT = 3;
|
||||
const TILT_HEAT_DECAY_PER_S = 0.4;
|
||||
|
||||
const SCORE_BUMPER = 100;
|
||||
const SCORE_SLING = 25;
|
||||
@@ -208,7 +204,6 @@ const SFX_DRAIN: Step[] = [
|
||||
{ freq: 370, duty: 45, ms: 100 },
|
||||
{ freq: 247, duty: 50, ms: 200 },
|
||||
];
|
||||
const SFX_TILT: Step[] = [{ freq: 110, duty: 60, ms: 300 }];
|
||||
const SFX_GAME_OVER: Step[] = [
|
||||
{ freq: 494, duty: 45, ms: 150 },
|
||||
{ freq: 440, duty: 45, ms: 150 },
|
||||
@@ -269,10 +264,6 @@ type PinballWindow = {
|
||||
/** Debounce: a sensor re-arms only after the ball leaves its zone. */
|
||||
rolloverInside: boolean[];
|
||||
bumperFlashUntilMs: number[];
|
||||
tiltHeat: number;
|
||||
tilted: boolean;
|
||||
/** Alternates nudge direction so mashing doesn't push one way forever. */
|
||||
nudgeSign: 1 | -1;
|
||||
/** Transient center-table message ("BALL LOST", "+500", ...). */
|
||||
toast: string;
|
||||
toastUntilMs: number;
|
||||
@@ -331,15 +322,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
rolloverLit: ROLLOVERS.map(() => false),
|
||||
rolloverInside: ROLLOVERS.map(() => false),
|
||||
bumperFlashUntilMs: BUMPERS.map(() => 0),
|
||||
tiltHeat: 0,
|
||||
tilted: false,
|
||||
nudgeSign: 1,
|
||||
toast: "",
|
||||
toastUntilMs: 0,
|
||||
tickTimer: null,
|
||||
lastTickAtMs: 0,
|
||||
lastMinorSfxAtMs: 0,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("pinball"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
windows.set(message.windowId, window);
|
||||
@@ -385,6 +373,19 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away (the system menu, a notification
|
||||
// modal, the voice dialog, focus back to the sidebar) pauses: the
|
||||
// ball must not drain under a menu the player can't flip through.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
@@ -446,7 +447,7 @@ function playSfx(window: PinballWindow, steps: Step[], minor = false): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for the nudge. */
|
||||
/** The window's context menu (game actions), offered while paused or over. */
|
||||
function windowMenuItems(window: PinballWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
@@ -462,6 +463,7 @@ function windowMenuItems(window: PinballWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("pinball", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
@@ -475,7 +477,6 @@ function windowMenu(window: PinballWindow): WindowMenu {
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -519,8 +520,8 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
flip(window, window.flippers[1]!);
|
||||
}
|
||||
break;
|
||||
// Watch swipes: left/right work the matching flipper; up nudges the
|
||||
// table (or raises launch power at the plunger), down lowers it.
|
||||
// Watch swipes: left/right work the matching flipper; up/down set the
|
||||
// launch power at the plunger.
|
||||
case "swipe-left":
|
||||
if (!ready) flip(window, window.flippers[0]!);
|
||||
break;
|
||||
@@ -530,8 +531,6 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
case "swipe-up":
|
||||
if (ready) {
|
||||
window.launchPower = clamp(window.launchPower + 1, 1, LAUNCH_SPEEDS.length);
|
||||
} else {
|
||||
nudge(window);
|
||||
}
|
||||
break;
|
||||
case "swipe-down":
|
||||
@@ -547,13 +546,6 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
flip(window, window.flippers[1]!);
|
||||
}
|
||||
break;
|
||||
case "long-press":
|
||||
if (ready) {
|
||||
frameTimings.finishFrame(frameId, "discarded: pinball ignored input");
|
||||
return;
|
||||
}
|
||||
nudge(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
@@ -606,8 +598,6 @@ function resetGame(window: PinballWindow): void {
|
||||
window.score = 0;
|
||||
window.rolloverLit = ROLLOVERS.map(() => false);
|
||||
window.rolloverInside = ROLLOVERS.map(() => false);
|
||||
window.tiltHeat = 0;
|
||||
window.tilted = false;
|
||||
window.toast = "";
|
||||
window.toastUntilMs = 0;
|
||||
for (const flipper of window.flippers) {
|
||||
@@ -628,7 +618,6 @@ function launchBall(window: PinballWindow): void {
|
||||
}
|
||||
|
||||
function flip(window: PinballWindow, flipper: Flipper): void {
|
||||
if (window.tilted) return;
|
||||
if (flipper.state === "hold") {
|
||||
flipper.holdUntilMs = Date.now() + FLIPPER_HOLD_MS;
|
||||
return;
|
||||
@@ -637,25 +626,8 @@ function flip(window: PinballWindow, flipper: Flipper): void {
|
||||
playSfx(window, SFX_FLIPPER, true);
|
||||
}
|
||||
|
||||
function nudge(window: PinballWindow): void {
|
||||
if (window.tilted) return;
|
||||
window.ballVy -= NUDGE_KICK_UP;
|
||||
window.ballVx += window.nudgeSign * NUDGE_KICK_SIDE;
|
||||
window.nudgeSign = window.nudgeSign === 1 ? -1 : 1;
|
||||
window.tiltHeat += 1;
|
||||
if (window.tiltHeat >= TILT_LIMIT) {
|
||||
window.tilted = true;
|
||||
showToast(window, "TILT");
|
||||
// A tilt drops the flippers dead until the ball drains.
|
||||
for (const flipper of window.flippers) {
|
||||
if (flipper.state !== "rest") flipper.state = "falling";
|
||||
}
|
||||
playSfx(window, SFX_TILT);
|
||||
}
|
||||
}
|
||||
|
||||
function addScore(window: PinballWindow, points: number): void {
|
||||
if (!window.tilted) window.score += points;
|
||||
window.score += points;
|
||||
}
|
||||
|
||||
function showToast(window: PinballWindow, text: string, ms = 1100): void {
|
||||
@@ -710,7 +682,6 @@ function tick(window: PinballWindow): void {
|
||||
function stepPhysics(window: PinballWindow): void {
|
||||
const dt = PHYSICS_DT;
|
||||
stepFlippers(window, dt);
|
||||
window.tiltHeat = Math.max(0, window.tiltHeat - TILT_HEAT_DECAY_PER_S * dt);
|
||||
if (window.ballState !== "live") return;
|
||||
|
||||
window.ballVy += GRAVITY * dt;
|
||||
@@ -894,8 +865,6 @@ function ballDrained(window: PinballWindow): void {
|
||||
window.ballVx = 0;
|
||||
window.ballVy = 0;
|
||||
window.trail = [];
|
||||
window.tilted = false;
|
||||
window.tiltHeat = 0;
|
||||
window.ballsLeft--;
|
||||
if (window.ballsLeft <= 0) {
|
||||
window.phase = "game-over";
|
||||
@@ -1007,9 +976,6 @@ function paintContent(window: PinballWindow): GrayImage {
|
||||
if (window.toastUntilMs > now && window.toast) {
|
||||
drawCenteredIn(image, mediumFont, LEFT, LANE_X - LEFT, 150, window.toast, 245);
|
||||
}
|
||||
if (window.tilted) {
|
||||
drawCenteredIn(image, mediumFont, LEFT, LANE_X - LEFT, 60, "TILT", 245);
|
||||
}
|
||||
if (window.phase === "paused") {
|
||||
paintDialog(image, "PAUSED", [`${GESTURE_CLICK} resume`, `${GESTURE_DOUBLE_CLICK} leave`]);
|
||||
} else if (window.phase === "game-over") {
|
||||
@@ -1050,7 +1016,7 @@ function paintPanel(image: GrayImage, window: PinballWindow): void {
|
||||
image.drawText(smallFont, PANEL_X, 236, `${GESTURE_DOUBLE_CLICK} pause`, 115);
|
||||
} else {
|
||||
image.drawText(smallFont, PANEL_X, 216, `${GESTURE_CLICK} flip ${GESTURE_SCROLL} L/R flip`, 115);
|
||||
image.drawText(smallFont, PANEL_X, 236, `${GESTURE_LONG_PRESS} nudge ${GESTURE_DOUBLE_CLICK} pause`, 115);
|
||||
image.drawText(smallFont, PANEL_X, 236, `${GESTURE_DOUBLE_CLICK} pause`, 115);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* The terminal session list as it is drawn in two places: the Terminal app's
|
||||
* hub window (in the worker) and the Glanceboard's Terminal widget (on the
|
||||
* main thread). The worker publishes a snapshot of the list through the
|
||||
* host's worker-state channel; both painters use drawSessionRow so a row
|
||||
* looks the same in each.
|
||||
*
|
||||
* No NativeScript imports: this module is bundled into the worker as well.
|
||||
*/
|
||||
import { type GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
|
||||
/** Worker-state key the Terminal worker publishes its snapshot under. */
|
||||
export const TERMINAL_SESSIONS_STATE_KEY = "terminal:sessions";
|
||||
|
||||
/** Alternation period of the activity indicator, shared by hub rows, the widget and sidebar cursors. */
|
||||
export const SESSION_ACTIVITY_STEP_MS = 800;
|
||||
|
||||
export type TerminalSessionRow = {
|
||||
/** Connection-scoped identity (connection id + socket). */
|
||||
key: string;
|
||||
label: string;
|
||||
/** Local-clock time (Date.now() domain) until which the session counts as active; 0 = idle. */
|
||||
activeUntilMs: number;
|
||||
/** Sidebar glyph ("1", "2", ...) of the view window showing this session, or null when none is open. */
|
||||
openGlyph: string | null;
|
||||
};
|
||||
|
||||
export type TerminalHostGroup = {
|
||||
/** Host name, shown as a heading when more than one host is connected. */
|
||||
name: string;
|
||||
sessions: TerminalSessionRow[];
|
||||
};
|
||||
|
||||
export type TerminalSessionsSnapshot = {
|
||||
/** Connected hosts, each with its live sessions most-recently-updated first. */
|
||||
hosts: TerminalHostGroup[];
|
||||
/** The hub's status line ("Connecting...", "No connections configured.", ...). */
|
||||
status: string;
|
||||
/** Whether any g2mirror connection is configured at all. */
|
||||
configured: boolean;
|
||||
};
|
||||
|
||||
/** Whether a row's activity indicator should show right now. */
|
||||
export function isSessionRowActive(row: TerminalSessionRow, nowMs = Date.now()): boolean {
|
||||
return row.activeUntilMs > nowMs;
|
||||
}
|
||||
|
||||
/** Width of the window-number column (one glyph plus breathing room). */
|
||||
export function sessionGlyphColumnWidth(font: UiFont): number {
|
||||
return font.measureText("W") + 6;
|
||||
}
|
||||
|
||||
/** Left edge of the label in a row whose activity gutter starts at `x`. */
|
||||
export function sessionLabelX(font: UiFont, x: number): number {
|
||||
return x + SESSION_INDICATOR_SIZE + 4 + sessionGlyphColumnWidth(font);
|
||||
}
|
||||
|
||||
const SESSION_INDICATOR_SIZE = 6;
|
||||
|
||||
/**
|
||||
* One session row: the activity indicator in the gutter at `x` (a small
|
||||
* square, filled on even animation phases and outlined on odd ones, drawn as
|
||||
* shapes rather than a font glyph so both states read in every UI font),
|
||||
* then the open view window's glyph in its own column, then the label,
|
||||
* truncated (never wrapped) to fit `width`.
|
||||
*/
|
||||
export function drawSessionRow(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
options: {
|
||||
x: number;
|
||||
/** Top of the text line. */
|
||||
y: number;
|
||||
/** Horizontal room from `x`, label included. */
|
||||
width: number;
|
||||
label: string;
|
||||
openGlyph: string | null;
|
||||
active: boolean;
|
||||
/** Animation phase (0 = filled indicator). */
|
||||
phase: number;
|
||||
/** Text brightness for the label (the glyph draws a little dimmer). */
|
||||
value: number;
|
||||
},
|
||||
): void {
|
||||
const { x, y, width, label, openGlyph, active, phase, value } = options;
|
||||
if (active) {
|
||||
const iy = y + Math.max(0, ((font.lineHeight - SESSION_INDICATOR_SIZE) / 2) | 0);
|
||||
if (phase % 2 === 0) {
|
||||
image.fillRect(x, iy, SESSION_INDICATOR_SIZE, SESSION_INDICATOR_SIZE, value);
|
||||
} else {
|
||||
image.drawRect(x, iy, SESSION_INDICATOR_SIZE, SESSION_INDICATOR_SIZE, value);
|
||||
}
|
||||
}
|
||||
if (openGlyph) {
|
||||
image.drawText(font, x + SESSION_INDICATOR_SIZE + 4, y, openGlyph, Math.max(0, value - 60));
|
||||
}
|
||||
const labelX = sessionLabelX(font, x);
|
||||
image.drawText(font, labelX, y, truncateText(font, label, Math.max(0, x + width - labelX)), value);
|
||||
}
|
||||
@@ -38,6 +38,11 @@ import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fo
|
||||
import { layoutHubHeader } from "./hub-header";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons";
|
||||
import {
|
||||
drawSessionRow,
|
||||
TERMINAL_SESSIONS_STATE_KEY,
|
||||
type TerminalSessionsSnapshot,
|
||||
} from "./session-list";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
@@ -723,6 +728,48 @@ function renderHubWindows(): void {
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "hub") scheduleRender(window);
|
||||
}
|
||||
// Every change the hub repaints for is one the published list may reflect.
|
||||
publishSessionsSnapshot();
|
||||
}
|
||||
|
||||
let lastPublishedSnapshot = "";
|
||||
|
||||
/**
|
||||
* Publish the session list for main-thread consumers (the Glanceboard's
|
||||
* Terminal widget): connected hosts with their sessions most-recently-updated
|
||||
* first, each with its activity deadline (local clock, so the consumer can
|
||||
* expire and animate it itself) and the glyph of its open view window. Posted
|
||||
* only when the JSON actually changed.
|
||||
*/
|
||||
function publishSessionsSnapshot(): void {
|
||||
const connected = connectedControls();
|
||||
const snapshot: TerminalSessionsSnapshot = {
|
||||
hosts: connected.map((control) => {
|
||||
const connectionId = control.config.id;
|
||||
const sessions = (control.state?.sessions ?? [])
|
||||
.slice()
|
||||
.sort(
|
||||
(a, b) =>
|
||||
(sessionRecency.get(recencyKey(connectionId, b.socket)) ?? 0) -
|
||||
(sessionRecency.get(recencyKey(connectionId, a.socket)) ?? 0),
|
||||
);
|
||||
return {
|
||||
name: connectionDisplayName(control.config),
|
||||
sessions: sessions.map((session) => ({
|
||||
key: recencyKey(connectionId, session.socket),
|
||||
label: sessionLabel(session),
|
||||
activeUntilMs: (sessionActivity.get(recencyKey(connectionId, session.socket)) ?? -ACTIVITY_ACTIVE_MS) + ACTIVITY_ACTIVE_MS,
|
||||
openGlyph: viewGlyphForSocket(connectionId, session.socket),
|
||||
})),
|
||||
};
|
||||
}),
|
||||
status: sessionsStatusLine(),
|
||||
configured: controls.size > 0,
|
||||
};
|
||||
const encoded = JSON.stringify(snapshot);
|
||||
if (encoded === lastPublishedSnapshot) return;
|
||||
lastPublishedSnapshot = encoded;
|
||||
post({ type: "publish-state", key: TERMINAL_SESSIONS_STATE_KEY, state: snapshot });
|
||||
}
|
||||
|
||||
// One animation clock for foreground hub rows and every terminal sidebar icon.
|
||||
@@ -1118,6 +1165,8 @@ type HubItem = {
|
||||
heading?: boolean;
|
||||
/** Session with recent output: an animated indicator marks the row. */
|
||||
active?: boolean;
|
||||
/** Glyph of the view window showing this session (the number column). */
|
||||
openGlyph?: string | null;
|
||||
onSelect?: () => void;
|
||||
};
|
||||
|
||||
@@ -1183,10 +1232,10 @@ function hubSessionItems(window: HubWindow): HubItem[] {
|
||||
}
|
||||
const sessions = orderedSessions(window, control);
|
||||
for (const session of sessions) {
|
||||
const openWindowId = viewWindowIdForSocket(control.config.id, session.socket);
|
||||
items.push({
|
||||
label: openWindowId ? `${sessionLabel(session)} [open]` : sessionLabel(session),
|
||||
label: sessionLabel(session),
|
||||
active: isSessionActive(control.config.id, session.socket),
|
||||
openGlyph: viewGlyphForSocket(control.config.id, session.socket),
|
||||
onSelect: () => {
|
||||
const windowId = viewWindowIdForSocket(control.config.id, session.socket);
|
||||
if (windowId) {
|
||||
@@ -1421,6 +1470,19 @@ function handleHubInput(window: HubWindow, event: InputEvent, frameId: number):
|
||||
* Includes views that were requested but whose surface hasn't opened yet, so
|
||||
* a quick double-select can't spawn two windows for one session.
|
||||
*/
|
||||
/** Sidebar glyph of the (open or opening) view window on a session, or null. */
|
||||
function viewGlyphForSocket(connectionId: string, socket: string): string | null {
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "view" && window.connectionId === connectionId && window.socket === socket) {
|
||||
return window.glyph || null;
|
||||
}
|
||||
}
|
||||
for (const pending of pendingViews.values()) {
|
||||
if (pending.connectionId === connectionId && pending.socket === socket) return pending.glyph || null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function viewWindowIdForSocket(connectionId: string, socket: string): string | null {
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "view" && window.connectionId === connectionId && window.socket === socket) {
|
||||
@@ -1539,20 +1601,18 @@ function paintHub(window: HubWindow): GrayImage {
|
||||
// an outline-only selection signals the sidebar owns input.
|
||||
drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8);
|
||||
}
|
||||
image.drawText(chromeFont(), 32, y + 2, item.label, selected ? 255 : 200);
|
||||
if (item.active) {
|
||||
// Activity indicator in the gutter left of the label, alternating
|
||||
// filled/outline each animation step (drawn shapes, not a font glyph,
|
||||
// so the two states render distinctly in every UI font).
|
||||
const size = 6;
|
||||
const iy = y + 2 + Math.max(0, ((font.lineHeight - size) / 2) | 0);
|
||||
const value = selected ? 255 : 200;
|
||||
if (hubAnimationPhase === 0) {
|
||||
image.fillRect(22, iy, size, size, value);
|
||||
} else {
|
||||
image.drawRect(22, iy, size, size, value);
|
||||
}
|
||||
}
|
||||
// Activity gutter, open-window number column, then the label, truncated
|
||||
// to the row (shared with the Glanceboard's Terminal widget).
|
||||
drawSessionRow(image, chromeFont(), {
|
||||
x: 22,
|
||||
y: y + 2,
|
||||
width: window.viewportWidth - 22 - 26,
|
||||
label: item.label,
|
||||
openGlyph: item.openGlyph ?? null,
|
||||
active: Boolean(item.active),
|
||||
phase: hubAnimationPhase,
|
||||
value: selected ? 255 : 200,
|
||||
});
|
||||
}
|
||||
if (items.length > visibleRowCount) {
|
||||
drawListScrollbar(
|
||||
@@ -1595,6 +1655,11 @@ function hubStatusLine(window: HubWindow): string {
|
||||
if (window.mode === "connections") {
|
||||
return "Select a connection to connect, disconnect, or remove.";
|
||||
}
|
||||
return sessionsStatusLine();
|
||||
}
|
||||
|
||||
/** The sessions view's status line (also published with the session snapshot). */
|
||||
function sessionsStatusLine(): string {
|
||||
if (controls.size === 0) {
|
||||
return "No connections configured.";
|
||||
}
|
||||
@@ -1830,7 +1895,8 @@ function toolListSessions(): ToolResult {
|
||||
lines.push(`${connectionDisplayName(control.config)}:`);
|
||||
}
|
||||
for (const session of sessions) {
|
||||
const open = viewWindowIdForSocket(control.config.id, session.socket) ? " [open]" : "";
|
||||
const glyph = viewGlyphForSocket(control.config.id, session.socket);
|
||||
const open = glyph ? ` [open in window ${glyph}]` : viewWindowIdForSocket(control.config.id, session.socket) ? " [open]" : "";
|
||||
lines.push(`- ${sessionLabel(session)}${open}`);
|
||||
}
|
||||
if (!sessions.length && multiHost) {
|
||||
|
||||
@@ -266,6 +266,11 @@ export class TimerEngine {
|
||||
);
|
||||
}
|
||||
|
||||
/** Every countdown timer (running, paused or rung), unsorted. */
|
||||
timers(): readonly CountdownTimer[] {
|
||||
return this.state.timers;
|
||||
}
|
||||
|
||||
ringingItems(): RingingItem[] {
|
||||
const items: RingingItem[] = [];
|
||||
for (const timer of this.state.timers) {
|
||||
|
||||
@@ -13,7 +13,9 @@ import { TranscriptModel, TranscriptScroll, wrapTranscribeText } from "./transcr
|
||||
* it, and saves it to Downloads from the app menu.
|
||||
*/
|
||||
export class TranscribeLayer implements Layer {
|
||||
private status = "Listening...";
|
||||
private status = "Starting microphone...";
|
||||
private listening = false;
|
||||
private detail = "";
|
||||
private readonly transcript = new TranscriptModel();
|
||||
private readonly scroll = new TranscriptScroll();
|
||||
private saveNotice = "";
|
||||
@@ -29,6 +31,8 @@ export class TranscribeLayer implements Layer {
|
||||
this.unsubscribePause = voiceControlBridge.onSpeechPause(() => this.transcript.pause());
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
this.listening = state.listening;
|
||||
this.detail = state.detail;
|
||||
this.saveNotice = "";
|
||||
requestRender();
|
||||
});
|
||||
@@ -38,7 +42,7 @@ export class TranscribeLayer implements Layer {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const text = this.transcript.text || "Listening...";
|
||||
const text = this.transcript.text || (this.listening ? "Listening..." : this.detail);
|
||||
const wrapped = wrapTranscribeText((text) => font.measureText(text), text, width - 64);
|
||||
|
||||
image.drawText(font, 24, 20, "Transcribe", 200);
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
import { AssistantSession, type AssistantBackendConfig, type AssistantSessionHistory } from "./session";
|
||||
import { ASSISTANT_MODEL_VALUES, type AssistantModel } from "./models";
|
||||
|
||||
export type ReasoningLevel = "default" | "low" | "medium" | "high";
|
||||
export type Conversation = {
|
||||
id: string;
|
||||
model: AssistantModel;
|
||||
reasoning: ReasoningLevel;
|
||||
session: AssistantSession | null;
|
||||
history?: AssistantSessionHistory;
|
||||
};
|
||||
type SavedConversation = Omit<Conversation, "session">;
|
||||
type SavedConversations = { selectedId: string; conversations: SavedConversation[] };
|
||||
|
||||
/** Shared selection and history for every assistant entry point. No credentials are saved. */
|
||||
export class AssistantConversations {
|
||||
private records: Conversation[] = [];
|
||||
private selectedId = "";
|
||||
private readonly listeners = new Set<() => void>();
|
||||
|
||||
constructor(
|
||||
private readonly resolve: (model: AssistantModel, reasoning: ReasoningLevel) => AssistantBackendConfig | null,
|
||||
private readonly defaultModel: () => AssistantModel,
|
||||
private readonly persist: (value: string) => void,
|
||||
saved = "",
|
||||
) {
|
||||
try {
|
||||
const data: SavedConversations = JSON.parse(saved);
|
||||
if (Array.isArray(data.conversations)) {
|
||||
this.records = data.conversations.filter((record) =>
|
||||
typeof record.id === "string" && ASSISTANT_MODEL_VALUES.includes(record.model) &&
|
||||
["default", "low", "medium", "high"].includes(record.reasoning) &&
|
||||
Array.isArray(record.history?.messages) && Array.isArray(record.history?.transcript),
|
||||
).map((record) => ({ ...record, session: null }));
|
||||
this.selectedId = data.selectedId;
|
||||
}
|
||||
} catch { /* First launch or invalid saved state. */ }
|
||||
if (!this.records.length) this.records.push(this.makeConversation());
|
||||
if (!this.records.some((record) => record.id === this.selectedId)) this.selectedId = this.records[0]!.id;
|
||||
}
|
||||
|
||||
current(): Conversation { return this.records.find((record) => record.id === this.selectedId)!; }
|
||||
list(): readonly Conversation[] { return this.records; }
|
||||
title(record = this.current()): string {
|
||||
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
|
||||
return transcript.find((entry) => entry.role === "user")?.text.replace(/\s+/g, " ").slice(0, 60) || "New session";
|
||||
}
|
||||
onChanged(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
private changed(save = true): void {
|
||||
if (save) this.persist(JSON.stringify({ selectedId: this.selectedId, conversations: this.records.map(
|
||||
({ session, ...record }) => ({ ...record, history: session?.history() ?? record.history ?? { messages: [], transcript: [] } }),
|
||||
) }));
|
||||
for (const listener of this.listeners) listener();
|
||||
}
|
||||
private makeConversation(): Conversation {
|
||||
return { id: `${Date.now()}-${Math.random().toString(36).slice(2)}`, model: this.defaultModel(), reasoning: "default", session: null };
|
||||
}
|
||||
create(): boolean {
|
||||
if (this.current().session?.isTurnActive()) return false;
|
||||
const record = this.makeConversation();
|
||||
this.records.push(record);
|
||||
this.selectedId = record.id;
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
select(id: string): boolean {
|
||||
if (this.current().session?.isTurnActive() || !this.records.some((record) => record.id === id)) return false;
|
||||
this.selectedId = id;
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
configure(model: AssistantModel, reasoning: ReasoningLevel): boolean {
|
||||
const record = this.current();
|
||||
if (record.session?.isTurnActive() || !this.resolve(model, reasoning)) return false;
|
||||
record.model = model;
|
||||
record.reasoning = reasoning;
|
||||
this.ensureSession();
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
available(model: AssistantModel, reasoning: ReasoningLevel): boolean { return this.resolve(model, reasoning) !== null; }
|
||||
ensureSession(): AssistantSession | null {
|
||||
const record = this.current();
|
||||
const config = this.resolve(record.model, record.reasoning);
|
||||
if (!config) return null;
|
||||
if (!record.session) {
|
||||
record.session = new AssistantSession(config, undefined, record.history);
|
||||
record.session.onChanged(() => this.changed(!record.session!.isTurnActive()));
|
||||
} else record.session.configure(config);
|
||||
return record.session;
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ export class DirectAssistantBackend {
|
||||
const startMs = Date.now();
|
||||
let cancelled = false;
|
||||
let streamHandle: LlmStreamHandle | null = null;
|
||||
let priorText = "";
|
||||
|
||||
const finishError = (message: string) => {
|
||||
if (cancelled) return;
|
||||
@@ -78,7 +79,7 @@ export class DirectAssistantBackend {
|
||||
tools: options.buildTools(),
|
||||
onTextDelta: (delta, textSoFar) => {
|
||||
if (cancelled) return;
|
||||
options.callbacks.onTextDelta(delta, textSoFar);
|
||||
options.callbacks.onTextDelta(delta, priorText + textSoFar);
|
||||
},
|
||||
onDone: (result) => {
|
||||
streamHandle = null;
|
||||
@@ -87,6 +88,9 @@ export class DirectAssistantBackend {
|
||||
finishError("The assistant declined this request");
|
||||
return;
|
||||
}
|
||||
const fullText = priorText + result.text;
|
||||
options.callbacks.onTextDelta("", fullText);
|
||||
priorText = fullText ? `${fullText}\n\n` : "";
|
||||
// Record what the model produced this iteration.
|
||||
const assistantContent: LlmContentBlock[] = result.content.length
|
||||
? result.content
|
||||
|
||||
+113
-47
@@ -15,26 +15,20 @@ import type {
|
||||
AssistantTurnHandle,
|
||||
} from "./types";
|
||||
|
||||
/**
|
||||
* One assistant conversation. Owns the message history and turn state and
|
||||
* drives either the on-phone provider loop (direct) or the external agent
|
||||
* bridge. The UI talks only to this surface, so AssistantLayer is
|
||||
* backend-agnostic.
|
||||
*
|
||||
* Sessions are cheap and idle-expire (see isExpired): the shell starts a fresh
|
||||
* one for a new wakeword and reuses the current one for a follow-up. In
|
||||
* external mode the conversation history lives on the agent's machine (one
|
||||
* long-lived session there), so expiry here only affects the overlay UI.
|
||||
*/
|
||||
/** One shared conversation, independent of the voice overlay or chat window. */
|
||||
export type AssistantTranscriptEntry = { role: "user" | "assistant"; text: string };
|
||||
export type AssistantSessionHistory = {
|
||||
messages: LlmMessage[];
|
||||
transcript: AssistantTranscriptEntry[];
|
||||
/** Model identity for safe replay after Auto resolves differently on restart. */
|
||||
engine?: string;
|
||||
};
|
||||
|
||||
/** Which engine answers utterances, plus what it needs to do so. */
|
||||
export type AssistantBackendConfig =
|
||||
| { kind: "direct"; llm: ResolvedAssistantModel }
|
||||
| { kind: "external"; bridge: AssistantBridgeConfig };
|
||||
|
||||
/** A new PTT after this much idle starts a fresh conversation. */
|
||||
const SESSION_IDLE_MS = 10 * 60 * 1000;
|
||||
|
||||
/** Trim history from the head once it grows past this many messages. */
|
||||
const MAX_HISTORY_MESSAGES = 40;
|
||||
|
||||
@@ -42,38 +36,61 @@ export class AssistantSession {
|
||||
private readonly directBackend = new DirectAssistantBackend();
|
||||
private readonly messages: LlmMessage[] = [];
|
||||
private turnHandle: AssistantTurnHandle | null = null;
|
||||
private lastActivityMs = Date.now();
|
||||
readonly transcript: AssistantTranscriptEntry[] = [];
|
||||
status = "";
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private turnGeneration = 0;
|
||||
// API-safe tool name -> canonical registry name. Provider APIs restrict tool
|
||||
// names, so dotted registry names are sanitized and mapped back on calls.
|
||||
private readonly toolNameMap = new Map<string, string>();
|
||||
|
||||
constructor(
|
||||
private readonly config: AssistantBackendConfig,
|
||||
private config: AssistantBackendConfig,
|
||||
private readonly registry: ToolRegistry = toolRegistry,
|
||||
) {}
|
||||
|
||||
matchesConfiguration(config: AssistantBackendConfig): boolean {
|
||||
if (this.config.kind !== config.kind) return false;
|
||||
if (this.config.kind === "direct" && config.kind === "direct") {
|
||||
return (
|
||||
this.config.llm.provider === config.llm.provider &&
|
||||
this.config.llm.model === config.llm.model &&
|
||||
this.config.llm.apiKey === config.llm.apiKey
|
||||
);
|
||||
history?: AssistantSessionHistory,
|
||||
) {
|
||||
if (history) {
|
||||
this.messages.push(...history.messages);
|
||||
this.transcript.push(...history.transcript);
|
||||
if (history.engine !== this.engine()) this.rebuildTextHistory();
|
||||
}
|
||||
if (this.config.kind === "external" && config.kind === "external") {
|
||||
return (
|
||||
this.config.bridge.host === config.bridge.host &&
|
||||
this.config.bridge.port === config.bridge.port &&
|
||||
this.config.bridge.token === config.bridge.token
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Whether this session has been idle long enough to retire. */
|
||||
isExpired(nowMs = Date.now()): boolean {
|
||||
return nowMs - this.lastActivityMs > SESSION_IDLE_MS;
|
||||
onChanged(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
private changed(): void {
|
||||
for (const listener of this.listeners) {
|
||||
try { listener(); } catch (error) { console.warn("assistant history listener failed", error); }
|
||||
}
|
||||
}
|
||||
|
||||
history(): AssistantSessionHistory {
|
||||
return { messages: this.messages, transcript: this.transcript, engine: this.engine() };
|
||||
}
|
||||
|
||||
configure(config: AssistantBackendConfig): void {
|
||||
if (this.isTurnActive()) return;
|
||||
// Opaque reasoning/tool provider items are only replayable on their
|
||||
// original model. Preserve visible conversation when switching engines.
|
||||
if (this.config.kind !== config.kind ||
|
||||
(this.config.kind === "direct" && config.kind === "direct" &&
|
||||
(this.config.llm.model !== config.llm.model || this.config.llm.provider !== config.llm.provider))) {
|
||||
this.rebuildTextHistory();
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
private engine(): string {
|
||||
return this.config.kind === "direct" ? `${this.config.llm.provider}:${this.config.llm.model}` : "external";
|
||||
}
|
||||
|
||||
private rebuildTextHistory(): void {
|
||||
this.messages.splice(0, this.messages.length, ...this.transcript
|
||||
.filter((entry) => entry.text.trim())
|
||||
.map((entry) => ({ role: entry.role, content: entry.text })));
|
||||
}
|
||||
|
||||
isTurnActive(): boolean {
|
||||
@@ -86,20 +103,44 @@ export class AssistantSession {
|
||||
callbacks.onError("The assistant is still working on the previous request");
|
||||
return;
|
||||
}
|
||||
this.lastActivityMs = Date.now();
|
||||
text = text.trim();
|
||||
if (!text) return;
|
||||
const generation = ++this.turnGeneration;
|
||||
const reply: AssistantTranscriptEntry = { role: "assistant", text: "" };
|
||||
this.transcript.push({ role: "user", text }, reply);
|
||||
this.status = "Thinking...";
|
||||
// Install a sentinel before calling providers: some failures are synchronous.
|
||||
this.turnHandle = { cancel: () => {} };
|
||||
this.changed();
|
||||
|
||||
const finish = () => {
|
||||
++this.turnGeneration;
|
||||
this.turnHandle = null;
|
||||
this.lastActivityMs = Date.now();
|
||||
this.changed();
|
||||
};
|
||||
const wrappedCallbacks: AssistantTurnCallbacks = {
|
||||
onTextDelta: callbacks.onTextDelta,
|
||||
onToolActivity: callbacks.onToolActivity,
|
||||
onTextDelta: (delta, full) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
reply.text = full;
|
||||
this.status = "Thinking...";
|
||||
this.changed();
|
||||
callbacks.onTextDelta(delta, full);
|
||||
},
|
||||
onToolActivity: (label) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = `→ ${label}`;
|
||||
this.changed();
|
||||
callbacks.onToolActivity(label);
|
||||
},
|
||||
onTurnDone: (result) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = "";
|
||||
finish();
|
||||
callbacks.onTurnDone(result);
|
||||
},
|
||||
onError: (message) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = message;
|
||||
finish();
|
||||
callbacks.onError(message);
|
||||
},
|
||||
@@ -108,7 +149,8 @@ export class AssistantSession {
|
||||
if (this.config.kind === "external") {
|
||||
// History and the agent loop live on the agent's machine; the phone just
|
||||
// streams this turn. The overlay keeps its own display state.
|
||||
this.turnHandle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
|
||||
const handle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
|
||||
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -123,24 +165,48 @@ export class AssistantSession {
|
||||
content: isLocal ? `${text}\n\n(${describeAssistantContext(ctx)})` : text,
|
||||
});
|
||||
this.trimHistory();
|
||||
this.turnHandle = this.directBackend.runTurn({
|
||||
// Work on a turn-local array: cancelled tools may finish later, and must
|
||||
// never append orphan results into a subsequent turn's context.
|
||||
const turnMessages = this.messages.slice();
|
||||
const handle = this.directBackend.runTurn({
|
||||
provider: llm.provider,
|
||||
apiKey: llm.apiKey,
|
||||
model: llm.model,
|
||||
effort: llm.effort,
|
||||
system: isLocal ? ASSISTANT_SYSTEM_PROMPT_BASE : buildAssistantSystemPrompt(ctx),
|
||||
messages: this.messages,
|
||||
messages: turnMessages,
|
||||
buildTools: () => this.buildToolDefinitions(),
|
||||
registry: this.registry,
|
||||
resolveToolName: (apiName) => this.toolNameMap.get(apiName) ?? apiName,
|
||||
callbacks: wrappedCallbacks,
|
||||
callbacks: {
|
||||
...wrappedCallbacks,
|
||||
onTurnDone: (result) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.messages.splice(0, this.messages.length, ...turnMessages);
|
||||
wrappedCallbacks.onTurnDone(result);
|
||||
},
|
||||
onError: (message) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
// Retain the visible partial answer, without incomplete tool pairs.
|
||||
if (reply.text) this.messages.push({ role: "assistant", content: reply.text });
|
||||
wrappedCallbacks.onError(message);
|
||||
},
|
||||
},
|
||||
});
|
||||
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.turnHandle?.cancel();
|
||||
if (!this.turnHandle) return;
|
||||
++this.turnGeneration;
|
||||
this.turnHandle.cancel();
|
||||
this.turnHandle = null;
|
||||
this.lastActivityMs = Date.now();
|
||||
const reply = this.transcript[this.transcript.length - 1];
|
||||
if (reply?.role === "assistant" && reply.text) {
|
||||
this.messages.push({ role: "assistant", content: reply.text });
|
||||
}
|
||||
this.status = "Cancelled";
|
||||
this.changed();
|
||||
}
|
||||
|
||||
private buildToolDefinitions(): LlmToolDefinition[] {
|
||||
|
||||
@@ -2,10 +2,10 @@ import { concat, rle4 } from './ble-protocol'
|
||||
|
||||
/** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in
|
||||
* units of 4 columns / 2 rows; pixels are top-down packed 4bpp, high nibble first.
|
||||
* null means identical, incomparable, or a full-screen box: use dedup/mode 6.
|
||||
* null means identical, incomparable, or a full-screen box: use dedup/mode 6. Compression belongs to the CFW transport.
|
||||
*/
|
||||
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array,
|
||||
width: number, height: number, frameId: number, deflate: (data: Uint8Array) => Uint8Array): Uint8Array | null {
|
||||
width: number, height: number, frameId: number): Uint8Array | null {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
|
||||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
|
||||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
|
||||
@@ -30,5 +30,20 @@ export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8
|
||||
region.set(next.subarray(start, start + regionStride), y * regionStride)
|
||||
}
|
||||
return concat(new Uint8Array([3, left / 4, top / 2, boxWidth / 4, boxHeight / 2,
|
||||
frameId & 255, (frameId >>> 8) & 255]), deflate(rle4(region)))
|
||||
frameId & 255, (frameId >>> 8) & 255]), rle4(region))
|
||||
}
|
||||
|
||||
/** Full repaint as independent 64-row mode-3 bands when RLE exceeds uint16. */
|
||||
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
|
||||
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
|
||||
throw new Error('Invalid framebuffer dimensions')
|
||||
const bands: Uint8Array[] = []
|
||||
let id = firstId
|
||||
for (let top = 0; top < height; top += 64) {
|
||||
const rows = Math.min(64, height - top)
|
||||
bands.push(concat(new Uint8Array([3, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]),
|
||||
rle4(packed.subarray(top * width / 2, (top + rows) * width / 2))))
|
||||
id = id >= 0xfffe ? 1 : id + 1
|
||||
}
|
||||
return bands
|
||||
}
|
||||
@@ -3,7 +3,7 @@ export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
|
||||
export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402'
|
||||
export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402'
|
||||
export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f']
|
||||
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9 } as const
|
||||
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const
|
||||
export function concat(...parts: Uint8Array[]): Uint8Array {
|
||||
const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
|
||||
let offset = 0; for (const part of parts) { output.set(part, offset); offset += part.length }; return output
|
||||
@@ -28,13 +28,7 @@ export const framebufferLease = (acquire: boolean) => concat(integer(1, 1), inte
|
||||
export function createLayout(magic: number): Uint8Array {
|
||||
const geometry = concat(integer(1, 0), integer(2, 0), integer(3, 576), integer(4, 288))
|
||||
const text = concat(geometry, integer(9, 1), string(10, 'dashboard'), integer(11, 1), string(12, ' '))
|
||||
const image = concat(geometry, integer(5, 10), string(6, 'img00'))
|
||||
return wrap(0, magic, 3, concat(integer(1, 2), bytes(3, text), bytes(4, image), integer(5, 10000)))
|
||||
}
|
||||
export function imageFragment(magic: number, payload: Uint8Array, offset: number, size = 3800): Uint8Array {
|
||||
const fragment = payload.subarray(offset, offset + size)
|
||||
return wrap(3, magic, 5, concat(integer(1, 10), string(2, 'img00'), integer(3, 10), integer(4, payload.length),
|
||||
integer(5, 0), integer(6, Math.floor(offset / size)), integer(7, fragment.length), bytes(8, fragment)))
|
||||
return wrap(0, magic, 3, concat(integer(1, 1), bytes(3, text), integer(5, 10000)))
|
||||
}
|
||||
export function crc16(data: Uint8Array): number {
|
||||
let crc = 0xffff
|
||||
@@ -55,7 +49,7 @@ export function frameMessage(payload: Uint8Array, sid: number, flag: number, seq
|
||||
return concat(new Uint8Array([0xaa, 0x21, sequence & 255, chunk.length, count, i + 1, sid, flag]), chunk)
|
||||
})
|
||||
}
|
||||
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number }
|
||||
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array }
|
||||
/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */
|
||||
export class MessageReceiver {
|
||||
private streams = new Map<string, Uint8Array>()
|
||||
@@ -70,7 +64,12 @@ export class MessageReceiver {
|
||||
const length = stream[3], count = stream[4], index = stream[5]
|
||||
if (stream.length < length + 8) break
|
||||
const sid = stream[6], flag = stream[7], key = `${link}:${stream[2]}:${sid}:${flag}`
|
||||
const packet = stream.slice(0, length + 8)
|
||||
const chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8)
|
||||
if (sid === SID.cfw) {
|
||||
output.push({ sid, flag, payload: chunk, command: -1, magic: -1, packet })
|
||||
continue
|
||||
}
|
||||
if (!count || !index || index > count) { this.messages.delete(key); continue }
|
||||
if (index === 1) this.messages.set(key, { chunks: [], total: count, seen: now, size: 0 })
|
||||
const message = this.messages.get(key)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { concat, crc16, SID } from './ble-protocol'
|
||||
|
||||
declare const require: (name: string) => any
|
||||
export const CFW_MAX_MESSAGE = 65535
|
||||
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
|
||||
const u16 = (data: Uint8Array, offset: number) => data[offset] | (data[offset + 1] << 8)
|
||||
|
||||
/** Matches Android CfwTransport. maxWrite is the ATT value limit (MTU - 3). */
|
||||
export class CfwTransport {
|
||||
private compressor: any
|
||||
private resetPending = true
|
||||
private lenses = 0
|
||||
reset(): void { this.compressor = null; this.resetPending = true; this.lenses = 0 }
|
||||
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
|
||||
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
|
||||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
|
||||
throw new Error('Invalid CFW stream parameters')
|
||||
if (this.lenses !== lenses) this.reset()
|
||||
if (!this.compressor) this.compressor = new (require('pako').Deflate)()
|
||||
const chunks: Uint8Array[] = []
|
||||
// SYNC_FLUSH emits this record while retaining history for the next one.
|
||||
this.compressor.onData = (chunk: Uint8Array) => chunks.push(chunk.slice())
|
||||
if (!this.compressor.push(message, 2)) throw new Error(this.compressor.msg || 'CFW compression failed')
|
||||
let body = concat(...chunks), flags = lenses | 4 | (this.resetPending ? 8 : 0)
|
||||
if (!body.length) flags &= ~4
|
||||
if (body.length > CFW_MAX_MESSAGE) {
|
||||
this.reset(); body = message; flags = lenses | 8
|
||||
} else { this.resetPending = false; this.lenses = lenses }
|
||||
const checksum = crc16(message)
|
||||
const record = concat(new Uint8Array([flags, body.length & 255, body.length >>> 8, checksum & 255, checksum >>> 8]), body)
|
||||
const capacity = Math.min(252, maxWrite - 11), packets: Uint8Array[] = []
|
||||
for (let offset = 0, index = 0; offset < record.length; offset += capacity, index++) {
|
||||
const body = concat(new Uint8Array([lenses | (offset === 0 ? 0x80 : 0) |
|
||||
(offset + capacity >= record.length ? 0x40 : 0)]), record.subarray(offset, offset + capacity))
|
||||
const crc = crc16(body)
|
||||
packets.push(concat(new Uint8Array([0xaa, 0x21, (streamId + index) & 255, body.length + 2, 1, 1, SID.cfw, 0]),
|
||||
body, new Uint8Array([crc & 255, crc >>> 8])))
|
||||
}
|
||||
return packets
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate the entire primary + redundant-ACK packet before applying any ACK. */
|
||||
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
|
||||
const n = packet.length
|
||||
if (n < 19 || n > 40 || (n - 19) % 7 || packet[0] !== 0xaa || packet[1] !== 0x12 ||
|
||||
packet[3] !== n - 8 || packet[4] !== 1 || packet[5] !== 1 || packet[6] !== SID.cfw || packet[7] !== 0 ||
|
||||
![1, 3].includes(packet[8]) || ![1, 2].includes(packet[12]) || (packet[8] === 3 && n !== 19) ||
|
||||
crc16(packet.subarray(8, -2)) !== u16(packet, n - 2)) return null
|
||||
const acks = [{ nack: packet[8] === 3, streamId: packet[9], messageId: u16(packet, 10),
|
||||
lens: packet[12], size: u16(packet, 13), checksum: u16(packet, 15) }]
|
||||
for (let offset = 17; offset < n - 2; offset += 7)
|
||||
acks.push({ nack: false, streamId: packet[offset], messageId: u16(packet, offset + 1), lens: packet[12],
|
||||
size: u16(packet, offset + 3), checksum: u16(packet, offset + 5) })
|
||||
return acks
|
||||
}
|
||||
+109
-38
@@ -16,7 +16,7 @@ import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, readActiveNotifications
|
||||
import { shouldShowNotificationOnGlasses } from "../native/notification-sources";
|
||||
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
||||
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
||||
import { firmwareIncompatibilityMessage } from "./firmware-compat";
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat";
|
||||
import { hasExtractedEvenHubFonts } from "./firmware-builder";
|
||||
import { resumeAutoReconnect, suppressAutoReconnect } from "./reconnect-policy";
|
||||
import { WearRemote, type WearRemoteInputKind } from "./wear-remote";
|
||||
@@ -43,6 +43,8 @@ const MIRROR_TOUCH_GESTURES: Record<Exclude<MirrorTouchKind, "tap">, WearRemoteI
|
||||
"swipe-right": "swipe-right",
|
||||
};
|
||||
import { findSoundEffect, playSoundEffect } from "../ui/sound-effects";
|
||||
import { GlanceHost } from "./glance-host";
|
||||
import { type GlanceEvent } from "./glance-state";
|
||||
import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboarding-state";
|
||||
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||
import { voiceControlBridge } from "../native/voice-control";
|
||||
@@ -52,6 +54,7 @@ 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 { type InputEvent } from "../ui/gestures";
|
||||
import { registerSystemTools } from "../assistant/system-tools";
|
||||
import { updateGlassesPresence } from "./glasses-presence";
|
||||
import { timerEngine } from "../apps/timer/timer-engine";
|
||||
@@ -252,10 +255,30 @@ class DashboardController {
|
||||
private screenTimeoutTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private evenHubSuspendTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private evenHubSessionSuspended = false;
|
||||
/**
|
||||
* The Glanceboard: the alternate sleep-time display, on its own surface
|
||||
* above the shell. Fed sleep-time input from handleInputEvent; it uses the
|
||||
* same wake barrier as the shell and hands the compositor back to the
|
||||
* screen-off path when it hides.
|
||||
*/
|
||||
private readonly glance = new GlanceHost({
|
||||
getDisplay: () => this.display,
|
||||
getProvider: () => ALL_APPS.find((app) => app.glanceboard)?.glanceboard ?? null,
|
||||
canShow: () => (this.phase === "connected" || this.isPreviewDisplayActive()) && !this.glassesLocked,
|
||||
ensureSessionActive: (frameId) => this.ensureEvenHubSessionActive(frameId),
|
||||
onHiddenWhileAsleep: () => {
|
||||
if (!shell.isScreenOn()) this.handleScreenStateChanged(false);
|
||||
},
|
||||
onVisibilityChanged: () => {
|
||||
this.schedulePreviewUpdate();
|
||||
this.emit();
|
||||
},
|
||||
appendLog: (message) => this.appendLog(message),
|
||||
});
|
||||
private evenHubResumePromise: Promise<boolean> | null = null;
|
||||
private faceclawWakeLeaseSupported = false;
|
||||
/** Faceclaw's firmware at the required revision reported in; its extensions can be used. */
|
||||
private customFirmwareConfirmed = false;
|
||||
private faceclawWakeLeaseState: boolean | null = null;
|
||||
private wearNotifySupported = false;
|
||||
private glassesWorn: boolean | null = null;
|
||||
private phoneLocked = false;
|
||||
private glassesLocked = false;
|
||||
@@ -319,7 +342,7 @@ class DashboardController {
|
||||
toggle?: TextSettingsEditToggle,
|
||||
) => this.startTextSettingsEdit(settings, title, onFinish, toggle),
|
||||
endTextSettingEdit: () => this.endTextSettingEdit(),
|
||||
startVoiceCapture: () => this.startVoiceCapture(),
|
||||
startVoiceCapture: (endpointing = false) => this.startVoiceCapture(endpointing),
|
||||
stopVoiceCapture: () => this.stopVoiceCapture(),
|
||||
startContinuousVoiceCapture: () => this.startContinuousVoiceCapture(),
|
||||
stopContinuousVoiceCapture: () => this.stopContinuousVoiceCapture(),
|
||||
@@ -359,6 +382,8 @@ class DashboardController {
|
||||
this.emit();
|
||||
},
|
||||
onScreenStateChanged: (on) => {
|
||||
// Any wake of the regular UI replaces a showing Glanceboard.
|
||||
if (on) this.glance.dismiss();
|
||||
this.handleScreenStateChanged(on);
|
||||
if (on) this.requestShellRender();
|
||||
this.emit();
|
||||
@@ -631,7 +656,7 @@ class DashboardController {
|
||||
const communicator = this.communicator;
|
||||
if (
|
||||
!lockScreenEnabledSetting.get() ||
|
||||
!this.wearNotifySupported ||
|
||||
!this.customFirmwareConfirmed ||
|
||||
this.phase !== "connected" ||
|
||||
!communicator
|
||||
) {
|
||||
@@ -736,7 +761,7 @@ class DashboardController {
|
||||
// foreground app when Faceclaw handles the glasses wakeword. Without the
|
||||
// latter, Even AI displaces EvenHub before our first wake frame can ACK.
|
||||
return (
|
||||
this.faceclawWakeLeaseSupported &&
|
||||
this.customFirmwareConfirmed &&
|
||||
(suspendEvenHubWhenScreenOffSetting.get() || wakeWordActionSetting.get() !== "off")
|
||||
);
|
||||
}
|
||||
@@ -788,6 +813,7 @@ class DashboardController {
|
||||
if (
|
||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||
shell.isScreenOn() ||
|
||||
this.glance.isVisible() ||
|
||||
this.phase !== "connected" ||
|
||||
!this.communicator ||
|
||||
this.evenHubSessionSuspended
|
||||
@@ -801,9 +827,11 @@ class DashboardController {
|
||||
if (
|
||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||
shell.isScreenOn() ||
|
||||
this.glance.isVisible() ||
|
||||
this.phase !== "connected" ||
|
||||
this.communicator !== communicator
|
||||
) {
|
||||
// A showing Glanceboard re-arms this when it hides.
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -816,7 +844,7 @@ class DashboardController {
|
||||
}
|
||||
|
||||
void (async () => {
|
||||
if (this.faceclawWakeLeaseSupported) {
|
||||
if (this.customFirmwareConfirmed) {
|
||||
// Refresh immediately before teardown so the first suspended wake
|
||||
// cannot race an old lease's expiry.
|
||||
const leaseReady = await this.syncFaceclawWakeLease(communicator, true);
|
||||
@@ -829,6 +857,7 @@ class DashboardController {
|
||||
if (
|
||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||
shell.isScreenOn() ||
|
||||
this.glance.isVisible() ||
|
||||
this.phase !== "connected" ||
|
||||
this.communicator !== communicator
|
||||
) {
|
||||
@@ -1250,6 +1279,7 @@ class DashboardController {
|
||||
const target = this.previewTarget;
|
||||
if (!target) return;
|
||||
this.previewTarget = null;
|
||||
this.glance.reset();
|
||||
target.release();
|
||||
this.clearDashboardTimer();
|
||||
this.appendLog("Preview-only display released.");
|
||||
@@ -1291,10 +1321,10 @@ class DashboardController {
|
||||
);
|
||||
|
||||
let communicator: FaceclawCommunicatorBridge | null = null;
|
||||
this.faceclawWakeLeaseSupported = false;
|
||||
this.customFirmwareConfirmed = false;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
this.wearNotifySupported = false;
|
||||
this.lockSurfaceConfigured = false;
|
||||
this.glance.reset();
|
||||
this.evenHubResumePromise = null;
|
||||
this.connectRunning = true;
|
||||
|
||||
@@ -1390,9 +1420,14 @@ class DashboardController {
|
||||
// especially while the glasses remain reachable in their case.
|
||||
const hasBatteryLevel = Number.isInteger(state.battery) && state.battery >= 0 && state.battery <= 100;
|
||||
if (hasBatteryLevel) this.lastHeadsetBattery = state.battery;
|
||||
const ringBattery = state.ringBattery;
|
||||
const hasRingBattery = typeof ringBattery === "number" && Number.isInteger(ringBattery)
|
||||
&& ringBattery >= 0 && ringBattery <= 100;
|
||||
shell.setBatteryLevels({
|
||||
headset: hasBatteryLevel ? state.battery : this.lastHeadsetBattery,
|
||||
headsetCharging: state.chargingStatus > 0,
|
||||
ring: hasRingBattery ? ringBattery : null,
|
||||
ringCharging: hasRingBattery ? state.ringChargingStatus === 1 : null,
|
||||
});
|
||||
updateGlassesPresence({ charging: state.chargingStatus > 0 || this.phase === "charging" });
|
||||
if ((this.phase === "connected" || this.phase === "charging") && this.communicator) {
|
||||
@@ -1436,26 +1471,18 @@ class DashboardController {
|
||||
this.offFirmwareInfo = communicator.onFirmwareInfo((info) => {
|
||||
this.appendLog(
|
||||
`firmware: L=${info.leftVersion || "?"} R=${info.rightVersion || "?"}` +
|
||||
(info.capabilities ? ` caps="${info.capabilities}"` : " (no CFW capability string)"),
|
||||
(info.extension ? ` ext="${info.extension}"` : " (no firmware extension string)"),
|
||||
);
|
||||
const warning = firmwareIncompatibilityMessage(info) ?? "";
|
||||
const wakeLeaseSupported = info.capabilities
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.includes("wakelease");
|
||||
const wearNotifySupported = info.capabilities
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.includes("wearnotify");
|
||||
if (wakeLeaseSupported !== this.faceclawWakeLeaseSupported) {
|
||||
this.faceclawWakeLeaseSupported = wakeLeaseSupported;
|
||||
// The firmware contract is all-or-nothing: a compatible revision has
|
||||
// every extension (wake lease, wear notifications, ...) we use.
|
||||
const customFirmwareConfirmed = !warning && hasCompatibleFirmware(info);
|
||||
if (customFirmwareConfirmed !== this.customFirmwareConfirmed) {
|
||||
this.customFirmwareConfirmed = customFirmwareConfirmed;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
void this.syncFaceclawWakeLease().catch((error) => {
|
||||
this.appendLog(`wake takeover lease sync failed: ${this.formatError(error)}`);
|
||||
});
|
||||
}
|
||||
if (wearNotifySupported !== this.wearNotifySupported) {
|
||||
this.wearNotifySupported = wearNotifySupported;
|
||||
this.ensureWearStateTracking();
|
||||
}
|
||||
if (warning !== this.firmwareWarningMessage) {
|
||||
@@ -1553,8 +1580,8 @@ class DashboardController {
|
||||
}
|
||||
this.communicator = null;
|
||||
this.lockSurfaceConfigured = false;
|
||||
this.wearNotifySupported = false;
|
||||
this.faceclawWakeLeaseSupported = false;
|
||||
this.glance.reset();
|
||||
this.customFirmwareConfirmed = false;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
this.evenHubResumePromise = null;
|
||||
this.clearDashboardTimer();
|
||||
@@ -1688,6 +1715,7 @@ class DashboardController {
|
||||
const communicator = this.communicator;
|
||||
this.communicator = null;
|
||||
this.lockSurfaceConfigured = false;
|
||||
this.glance.reset();
|
||||
this.evenHubSessionSuspended = false;
|
||||
this.evenHubResumePromise = null;
|
||||
|
||||
@@ -1746,9 +1774,8 @@ class DashboardController {
|
||||
await communicator?.close().catch(() => {});
|
||||
} finally {
|
||||
stopForegroundNotification();
|
||||
this.faceclawWakeLeaseSupported = false;
|
||||
this.customFirmwareConfirmed = false;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
this.wearNotifySupported = false;
|
||||
this.setPhase("disconnected");
|
||||
this.setStatus("Disconnected.");
|
||||
this.appendLog("Disconnected from the glasses.");
|
||||
@@ -1762,14 +1789,14 @@ class DashboardController {
|
||||
* finger-down/finger-up (the watch) hold for as long as the user does.
|
||||
*/
|
||||
async injectSyntheticRingInput(kind: WearRemoteInputKind, origin: SyntheticInputOrigin = "ring"): Promise<void> {
|
||||
// Match the ring while the display is dark: only a double-click wakes it,
|
||||
// and that wake is handled by Shell.receiveInput. This check also protects
|
||||
// against a watch acting on a stale state snapshot.
|
||||
// Match the ring while the display is dark: a double-click wakes it
|
||||
// (handled by Shell.receiveInput) and a tap or hold shows the
|
||||
// Glanceboard; scrolls and the menu gesture mean nothing. This check also
|
||||
// protects against a watch acting on a stale state snapshot.
|
||||
if (
|
||||
origin === "watch" &&
|
||||
!shell.isScreenOn() &&
|
||||
kind !== "double-click" &&
|
||||
kind !== "long-press-release"
|
||||
(kind === "scroll-up" || kind === "scroll-down" || kind === "short-then-long-press")
|
||||
) {
|
||||
this.appendLog(`${kind} (watch scheme) ignored while display is off`);
|
||||
return;
|
||||
@@ -1800,7 +1827,12 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
if (!shell.isScreenOn()) {
|
||||
// Asleep, the mirror offers the sleep gestures: double-tap wakes, a
|
||||
// tap shows the Glanceboard for its timeout. The mirror's hold has no
|
||||
// real release (the synthetic long-press releases at once, which would
|
||||
// only flash the board), so it counts as a tap here too.
|
||||
if (kind === "double-tap") await this.injectSyntheticRingInput("double-click", "watch");
|
||||
else if (kind === "tap" || kind === "long-press") await this.injectSyntheticRingInput("click", "watch");
|
||||
return;
|
||||
}
|
||||
if (kind !== "tap") {
|
||||
@@ -1874,12 +1906,15 @@ class DashboardController {
|
||||
* push-to-talk and the Transcribe app. Android mic permission is the consent
|
||||
* gate even though the audio source is the G2 mic over BLE.
|
||||
*/
|
||||
private startVoiceCapture(endpointing = false): void {
|
||||
this.beginVoiceCapture("ptt", endpointing);
|
||||
private pttCaptureGeneration = 0;
|
||||
|
||||
private startVoiceCapture(endpointing = false): Promise<void> {
|
||||
return this.beginVoiceCapture("ptt", endpointing);
|
||||
}
|
||||
|
||||
private stopVoiceCapture(): void {
|
||||
voiceControlBridge.stopPushToTalk();
|
||||
private stopVoiceCapture(): Promise<void> | void {
|
||||
++this.pttCaptureGeneration;
|
||||
return voiceControlBridge.stopPushToTalk();
|
||||
}
|
||||
|
||||
private startContinuousVoiceCapture(): void {
|
||||
@@ -1944,7 +1979,8 @@ class DashboardController {
|
||||
return granted;
|
||||
}
|
||||
|
||||
private beginVoiceCapture(kind: "ptt" | "continuous", endpointing = false): void {
|
||||
private async beginVoiceCapture(kind: "ptt" | "continuous", endpointing = false): Promise<void> {
|
||||
const pttGeneration = kind === "ptt" ? ++this.pttCaptureGeneration : 0;
|
||||
// Preview mode captures from the phone mic (voiceCaptureOptions with a
|
||||
// null communicator); otherwise a live glasses session must be the source.
|
||||
const previewCapture = this.isPreviewDisplayActive();
|
||||
@@ -1952,8 +1988,12 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
const communicator = this.communicator;
|
||||
void ensureVoicePermissions()
|
||||
// The system prompt appears on the phone; tell the glasses dialog so it
|
||||
// sends the user there rather than claiming to listen.
|
||||
if (!hasMicrophonePermission()) voiceControlBridge.reportPermissionPrompt();
|
||||
await ensureVoicePermissions()
|
||||
.then(() => {
|
||||
if (kind === "ptt" && pttGeneration !== this.pttCaptureGeneration) return;
|
||||
if (previewCapture) {
|
||||
if (!this.isPreviewDisplayActive()) return;
|
||||
} else if (this.phase !== "connected" || this.communicator !== communicator) {
|
||||
@@ -1968,6 +2008,7 @@ class DashboardController {
|
||||
})
|
||||
.catch((error) => {
|
||||
this.appendLog(`voice permission failed: ${this.formatError(error)}`);
|
||||
if (!hasMicrophonePermission()) voiceControlBridge.reportPermissionDenied();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2057,6 +2098,20 @@ class DashboardController {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!shell.isScreenOn()) {
|
||||
// Sleep-time gestures belong to the Glanceboard: a tap or a hold
|
||||
// shows it (the shell would ignore them), and a double-tap while it
|
||||
// is up dismisses it and falls through to the normal wake below.
|
||||
const glanceEvent = this.glanceEventFor(inputEvent, event);
|
||||
if (glanceEvent?.type === "dismiss") {
|
||||
this.glance.dismiss();
|
||||
} else if (glanceEvent) {
|
||||
frameTimings.annotateFrame(frameId, `glanceboard ${glanceEvent.type}`);
|
||||
await this.glance.handleEvent(glanceEvent, frameId);
|
||||
frameOwned = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
const wakewordShouldWake =
|
||||
event.kind === "even-ai" &&
|
||||
event.eventType === EvenAIStatus.EVEN_AI_WAKE_UP &&
|
||||
@@ -2130,6 +2185,19 @@ class DashboardController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Glanceboard event a sleep-time input means, or null when the shell
|
||||
* should see the input as usual. A display-wake carries the head-tilt
|
||||
* gesture (HEAD_UP_EVENT) as a press; the double-tap display-wake stays the
|
||||
* regular UI's wake.
|
||||
*/
|
||||
private glanceEventFor(inputEvent: InputEvent, event: RawInputEvent): GlanceEvent | null {
|
||||
if (inputEvent.type === "display-wake") {
|
||||
return event.eventType === OsEventTypeList.HEAD_UP_EVENT ? this.glance.eventForGesture("head-tilt") : null;
|
||||
}
|
||||
return this.glance.eventForGesture(inputEvent.type);
|
||||
}
|
||||
|
||||
/** Launch or focus an in-process singleton app (notifications, debug tests). */
|
||||
private async launchInProcessApp(
|
||||
windowId: string,
|
||||
@@ -2527,6 +2595,9 @@ class DashboardController {
|
||||
private setPhase(phase: ConnectionPhase): void {
|
||||
if (this.phase === phase) return;
|
||||
this.phase = phase;
|
||||
if (phase !== "connected" && phase !== "charging") {
|
||||
shell.setBatteryLevels({ ring: null, ringCharging: null });
|
||||
}
|
||||
// "charging" is a live BLE link with the glasses in their case.
|
||||
updateGlassesPresence({
|
||||
connected: phase === "connected" || phase === "charging",
|
||||
|
||||
@@ -71,6 +71,12 @@ export const OsEventTypeList = {
|
||||
RING_LONG_PRESS_RELEASE_EVENT: 10,
|
||||
/** CFW extension: the 2.2.9 tap-then-hold Menu gesture. */
|
||||
SHORT_THEN_LONG_PRESS_EVENT: 11,
|
||||
/**
|
||||
* CFW extension (head-tilt wake branch): the IMU head-up gesture, forwarded
|
||||
* while the display sleeps. Reaches the phone as a display-wake carrying
|
||||
* this eventType; the glance host treats it as a press.
|
||||
*/
|
||||
HEAD_UP_EVENT: 12,
|
||||
} as const;
|
||||
|
||||
export const OsEventTypeName: Record<number, string> = {
|
||||
@@ -86,4 +92,5 @@ export const OsEventTypeName: Record<number, string> = {
|
||||
9: "RING_LONG_PRESS_EVENT",
|
||||
10: "RING_LONG_PRESS_RELEASE_EVENT",
|
||||
11: "SHORT_THEN_LONG_PRESS_EVENT",
|
||||
12: "HEAD_UP_EVENT",
|
||||
};
|
||||
+146
-68
@@ -1,20 +1,79 @@
|
||||
/**
|
||||
* Compatibility check for the glasses firmware. Faceclaw requires the custom
|
||||
* firmware: version >= 2.2.4.34 with the required direct-framebuffer and wear
|
||||
* notification tokens in the CFW capability string. Stock firmware sends no
|
||||
* capability string at all.
|
||||
* Compatibility check for the glasses firmware. Faceclaw requires its own
|
||||
* custom firmware at a specific revision.
|
||||
*
|
||||
* The firmware identifies itself through one extra string in the sid-0x09
|
||||
* settings READ response (protobuf field 100, the "firmware extension" slot):
|
||||
*
|
||||
* "Faceclaw/<n>" -- Faceclaw's custom firmware, revision <n>. The revision
|
||||
* goes up whenever the firmware contract changes and is
|
||||
* not kept in sync with Faceclaw app versions.
|
||||
* "EVENCFW/..." -- older Faceclaw firmware, from before revisions existed
|
||||
* (it advertised feature tokens instead). Always outdated.
|
||||
* (empty) -- stock firmware; it never sends the field.
|
||||
* anything else -- custom firmware from a different source.
|
||||
*
|
||||
* Because an exact revision is required, nothing else in the app gates on the
|
||||
* extension string: once the firmware is compatible, every feature it provides
|
||||
* is assumed present.
|
||||
*/
|
||||
|
||||
import { type FirmwareInfo } from "../native/faceclaw-communicator";
|
||||
export type FirmwareInfo = {
|
||||
leftVersion: string;
|
||||
rightVersion: string;
|
||||
/** Raw contents of the firmware-extension slot ("" on stock firmware). */
|
||||
extension: string;
|
||||
};
|
||||
|
||||
const MIN_FIRMWARE_VERSION = [2, 2, 4, 34];
|
||||
const REQUIRED_FIRMWARE_EXTENSIONS = ["img640", "fbguard", "wearnotify"] as const;
|
||||
/** The Faceclaw firmware revision this build of the app needs. */
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 13;
|
||||
|
||||
const FACECLAW_PREFIX = "Faceclaw/";
|
||||
const LEGACY_PREFIX = "EVENCFW";
|
||||
|
||||
// The stock firmware release Faceclaw's custom image is built from. Stock at or
|
||||
// below this can be flashed with our patched image; a newer stock version is
|
||||
// unrecognized (its layout may differ from what our patch set targets).
|
||||
export const FLASHABLE_STOCK_VERSION = [2, 2, 9, 22];
|
||||
export const FLASHABLE_STOCK_VERSION_TEXT = FLASHABLE_STOCK_VERSION.join(".");
|
||||
export const BASE_STOCK_VERSION = [2, 2, 9, 22];
|
||||
export const BASE_STOCK_VERSION_TEXT = BASE_STOCK_VERSION.join(".");
|
||||
export const VALIDATED_STOCK_VERSION = [2, 2, 10, 10];
|
||||
export const VALIDATED_STOCK_VERSION_TEXT = VALIDATED_STOCK_VERSION.join(".");
|
||||
|
||||
export type FirmwareExtension =
|
||||
/** Nothing in the slot: stock firmware. */
|
||||
| { kind: "none" }
|
||||
/** Faceclaw's firmware at the given revision. */
|
||||
| { kind: "faceclaw"; version: number }
|
||||
/** Pre-revision Faceclaw firmware ("EVENCFW/<ver> <tokens>"). */
|
||||
| { kind: "legacy-faceclaw"; text: string }
|
||||
/** Custom firmware from some other project. */
|
||||
| { kind: "other"; text: string };
|
||||
|
||||
export function parseFirmwareExtension(extension: string): FirmwareExtension {
|
||||
const text = extension.trim();
|
||||
if (!text) return { kind: "none" };
|
||||
if (text.startsWith(FACECLAW_PREFIX)) {
|
||||
const version = parseInt(text.slice(FACECLAW_PREFIX.length), 10);
|
||||
if (Number.isFinite(version) && version >= 0) return { kind: "faceclaw", version };
|
||||
return { kind: "other", text };
|
||||
}
|
||||
if (text.startsWith(LEGACY_PREFIX)) return { kind: "legacy-faceclaw", text };
|
||||
return { kind: "other", text };
|
||||
}
|
||||
|
||||
/** Short user-facing description of what the extension slot reported. */
|
||||
export function describeFirmwareExtension(extension: FirmwareExtension): string {
|
||||
switch (extension.kind) {
|
||||
case "faceclaw":
|
||||
return `Faceclaw firmware revision ${extension.version}`;
|
||||
case "legacy-faceclaw":
|
||||
return `an older Faceclaw firmware (reported "${extension.text}")`;
|
||||
case "other":
|
||||
return `custom firmware from a different source (reported "${extension.text}")`;
|
||||
default:
|
||||
return "stock firmware";
|
||||
}
|
||||
}
|
||||
|
||||
function parseDottedVersion(version: string): number[] {
|
||||
return version
|
||||
@@ -36,66 +95,53 @@ function compareVersions(a: number[], b: number[]): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the glasses run Faceclaw's firmware at the required revision or
|
||||
* newer. (A newer revision is accepted: revisions only add to the contract.)
|
||||
*/
|
||||
export function hasCompatibleFirmware(info: FirmwareInfo): boolean {
|
||||
const extension = parseFirmwareExtension(info.extension);
|
||||
return extension.kind === "faceclaw" && extension.version >= REQUIRED_FACECLAW_FIRMWARE_VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Human-readable explanation of why this firmware cannot run Faceclaw, or
|
||||
* null if it is compatible. Returns null when no version was reported at all
|
||||
* null if it is compatible. Returns null when nothing was reported at all
|
||||
* (no data is not evidence of incompatibility).
|
||||
*/
|
||||
export function firmwareIncompatibilityMessage(info: FirmwareInfo): string | null {
|
||||
const reportedVersions = [info.leftVersion, info.rightVersion].filter((v) => v.trim().length > 0);
|
||||
if (reportedVersions.length === 0) return null;
|
||||
const extension = parseFirmwareExtension(info.extension);
|
||||
if (reportedVersions.length === 0 && extension.kind === "none") return null;
|
||||
|
||||
const minVersionText = MIN_FIRMWARE_VERSION.join(".");
|
||||
const versionsText = `L=${info.leftVersion || "unknown"} R=${info.rightVersion || "unknown"}`;
|
||||
const required = `Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}`;
|
||||
|
||||
if (reportedVersions.some((v) => compareVersions(parseDottedVersion(v), MIN_FIRMWARE_VERSION) < 0)) {
|
||||
return (
|
||||
`The glasses report firmware ${versionsText}, but Faceclaw requires the modified firmware, ` +
|
||||
`version ${minVersionText} or newer. Displaying images will not work until the glasses firmware is updated.`
|
||||
);
|
||||
switch (extension.kind) {
|
||||
case "faceclaw":
|
||||
if (extension.version >= REQUIRED_FACECLAW_FIRMWARE_VERSION) return null;
|
||||
return (
|
||||
`The glasses run Faceclaw firmware revision ${extension.version} (base ${versionsText}), ` +
|
||||
`but this version of Faceclaw requires revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}. ` +
|
||||
"Install the updated custom firmware to continue."
|
||||
);
|
||||
case "legacy-faceclaw":
|
||||
return (
|
||||
`The glasses run an older version of Faceclaw's custom firmware (reported "${extension.text}", ` +
|
||||
`base ${versionsText}). This version of Faceclaw requires ${required}. ` +
|
||||
"Install the updated custom firmware to continue."
|
||||
);
|
||||
case "other":
|
||||
return (
|
||||
`The glasses run custom firmware from a different source (reported "${extension.text}", ` +
|
||||
`base ${versionsText}). Faceclaw requires ${required}; installing it will replace the current firmware.`
|
||||
);
|
||||
default:
|
||||
return (
|
||||
`The glasses report stock firmware ${versionsText}. Faceclaw requires ${required}. ` +
|
||||
"Displaying images will not work until the custom firmware is installed."
|
||||
);
|
||||
}
|
||||
|
||||
const tokens = info.capabilities.trim().split(/\s+/);
|
||||
const missingExtensions = REQUIRED_FIRMWARE_EXTENSIONS.filter(
|
||||
(extension) => !tokens.includes(extension),
|
||||
);
|
||||
if (missingExtensions.length) {
|
||||
return (
|
||||
`The glasses firmware (${versionsText}) does not advertise the required ` +
|
||||
`${missingExtensions.map((extension) => `"${extension}"`).join(" and ")} extension` +
|
||||
`${missingExtensions.length === 1 ? "" : "s"}` +
|
||||
`${info.capabilities.trim() ? ` (reported: ${info.capabilities.trim()})` : ", which suggests stock firmware"}. ` +
|
||||
`Faceclaw requires the modified firmware with the guarded 640x480 direct-framebuffer path and wear notifications.`
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** True when the glasses advertise every custom-firmware extension Faceclaw needs. */
|
||||
export function hasCustomFirmware(info: FirmwareInfo): boolean {
|
||||
const tokens = info.capabilities.trim().split(/\s+/);
|
||||
return REQUIRED_FIRMWARE_EXTENSIONS.every((extension) => tokens.includes(extension));
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the firmware advertises the optional mic-control extension: the
|
||||
* private field-103/104 channel for per-temple multi-microphone capture that
|
||||
* the Microphones app's 4-mic array mode and beamforming need. Absent on the
|
||||
* stock single-mono-stream path, which still works (firmware-computed
|
||||
* direction of arrival, one mixed channel).
|
||||
*/
|
||||
export function hasMicControl(info: FirmwareInfo): boolean {
|
||||
return info.capabilities.trim().split(/\s+/).includes("micctl");
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the firmware advertises the optional ambient-light extension:
|
||||
* image-handler mode 16 (query / passive polling of the OPT3001 light sensor)
|
||||
* with field-105 reports, used to drive a phone-side brightness policy.
|
||||
*/
|
||||
export function hasAmbientLightSensor(info: FirmwareInfo): boolean {
|
||||
return info.capabilities.trim().split(/\s+/).includes("als16");
|
||||
}
|
||||
|
||||
/** The higher of the two arms' reported versions, or "" if none reported. */
|
||||
@@ -109,24 +155,56 @@ export function reportedFirmwareVersion(info: FirmwareInfo): string {
|
||||
|
||||
/**
|
||||
* How the pre-flash firmware check should treat the connected glasses:
|
||||
* - "custom": Faceclaw's firmware is already installed — nothing to flash.
|
||||
* - "custom": Faceclaw's firmware at the required revision (or newer) is
|
||||
* already installed — nothing to flash.
|
||||
* - "older-faceclaw": Faceclaw's firmware at an older revision (including
|
||||
* the pre-revision "EVENCFW" builds) — flash to update it.
|
||||
* - "other-custom": custom firmware from a different source — flash to
|
||||
* replace it.
|
||||
* - "flashable-stock": stock firmware at or below the version we build from.
|
||||
* - "newer-stock": stock firmware newer than we recognize — flash only on override.
|
||||
* - "newer-stock-validated": stock firmware newer than the version we build
|
||||
* from, on which the downgrade has been tested successfully
|
||||
* - "newer-stock-unvalidated": stock firmware newer than we recognize — flash only on override.
|
||||
* - "unknown": no version could be read (treated as a probe/connection failure).
|
||||
*/
|
||||
export type OnboardingFirmwareKind = "custom" | "flashable-stock" | "newer-stock" | "unknown";
|
||||
export type OnboardingFirmwareKind =
|
||||
| "custom"
|
||||
| "older-faceclaw"
|
||||
| "other-custom"
|
||||
| "flashable-stock"
|
||||
| "newer-stock-validated"
|
||||
| "newer-stock-unvalidated"
|
||||
| "unknown";
|
||||
|
||||
export function classifyOnboardingFirmware(info: FirmwareInfo): {
|
||||
kind: OnboardingFirmwareKind;
|
||||
version: string;
|
||||
extension: FirmwareExtension;
|
||||
} {
|
||||
if (hasCustomFirmware(info)) {
|
||||
return { kind: "custom", version: reportedFirmwareVersion(info) };
|
||||
}
|
||||
const extension = parseFirmwareExtension(info.extension);
|
||||
const version = reportedFirmwareVersion(info);
|
||||
switch (extension.kind) {
|
||||
case "faceclaw":
|
||||
return {
|
||||
kind: extension.version >= REQUIRED_FACECLAW_FIRMWARE_VERSION ? "custom" : "older-faceclaw",
|
||||
version,
|
||||
extension,
|
||||
};
|
||||
case "legacy-faceclaw":
|
||||
return { kind: "older-faceclaw", version, extension };
|
||||
case "other":
|
||||
return { kind: "other-custom", version, extension };
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!version) {
|
||||
return { kind: "unknown", version: "" };
|
||||
return { kind: "unknown", version: "", extension };
|
||||
}
|
||||
if (compareVersions(parseDottedVersion(version), BASE_STOCK_VERSION) <= 0) {
|
||||
return { kind: "flashable-stock", version, extension };
|
||||
} else if (compareVersions(parseDottedVersion(version), VALIDATED_STOCK_VERSION) <= 0) {
|
||||
return { kind: "newer-stock-validated", version, extension };
|
||||
} else {
|
||||
return { kind: "newer-stock-unvalidated", version, extension };
|
||||
}
|
||||
const comparison = compareVersions(parseDottedVersion(version), FLASHABLE_STOCK_VERSION);
|
||||
return { kind: comparison <= 0 ? "flashable-stock" : "newer-stock", version };
|
||||
}
|
||||
@@ -19,168 +19,192 @@ export type FirmwarePatchSet = {
|
||||
export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
"base": "g2_2.2.9.22.bin",
|
||||
"baseSha256": "a03fbea9f68a9de6bc271daabb9f3a41c59053d1086622c76a4e990f829cc561",
|
||||
"outputSha256": "920f5e45129453c155d7fb004b93049985594744be973e793cf41e8b6e07005a",
|
||||
"outputSha256": "32cea69474252a2b2f9c0fdc5f89bf0d5d6f745b4eec10aea8e9c34e3f106b3f",
|
||||
"patches": [
|
||||
{
|
||||
"offset": 935280,
|
||||
"old": "fff717f8",
|
||||
"new": "64f395fb",
|
||||
"desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 935628,
|
||||
"old": "fef769ff",
|
||||
"new": "64f3e7fa",
|
||||
"desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 943818,
|
||||
"old": "fcf76aff",
|
||||
"new": "62f3e8fa",
|
||||
"desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 1415004,
|
||||
"old": "fcf745f8",
|
||||
"new": "eef23dfb",
|
||||
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
|
||||
},
|
||||
{
|
||||
"offset": 1363762,
|
||||
"old": "7c20",
|
||||
"new": "7d20",
|
||||
"desc": "Set Local Feature: enable LE 2M bit 8"
|
||||
},
|
||||
{
|
||||
"offset": 4410298,
|
||||
"old": "0c001800",
|
||||
"new": "06000600",
|
||||
"desc": "fast connection interval min=max=7.5 ms; latency remains 0"
|
||||
},
|
||||
{
|
||||
"offset": 1053266,
|
||||
"old": "0500",
|
||||
"new": "a325",
|
||||
"desc": "_connectParamReq_impl: force requested mode to fast (0xa3); disables idle slow requests"
|
||||
},
|
||||
{
|
||||
"offset": 1124178,
|
||||
"old": "5ff43432",
|
||||
"new": "5ff43332",
|
||||
"desc": "reserve final 1 KiB of primary TLSF arena for CFW context anchor"
|
||||
},
|
||||
{
|
||||
"offset": 1524180,
|
||||
"old": "bdf82c10",
|
||||
"new": "40f24120",
|
||||
"desc": "container width <= 576"
|
||||
},
|
||||
{
|
||||
"offset": 1524380,
|
||||
"old": "bdf82e00",
|
||||
"new": "40f22111",
|
||||
"desc": "container height movw #0x121"
|
||||
},
|
||||
{
|
||||
"offset": 1524384,
|
||||
"old": "9128",
|
||||
"new": "8842",
|
||||
"desc": "container height cmp r0,r1"
|
||||
},
|
||||
{
|
||||
"offset": 1516784,
|
||||
"old": "70f775ff",
|
||||
"new": "d6f295fa",
|
||||
"desc": "bl snapshot_side @ 0x4ec0ee (shared image-complete helper)"
|
||||
},
|
||||
{
|
||||
"offset": 1516816,
|
||||
"old": "30d0",
|
||||
"new": "1cd0",
|
||||
"desc": "image-complete success -> immediate ACK (pipelining-safe)"
|
||||
},
|
||||
{
|
||||
"offset": 1222660,
|
||||
"old": "49f0daff",
|
||||
"new": "1ef30ff9",
|
||||
"desc": "bl image_deferred (deferred consumer -> FIFO restore + worker, both lenses)"
|
||||
},
|
||||
{
|
||||
"offset": 1242342,
|
||||
"old": "d4f790fb",
|
||||
"new": "1af3faf9",
|
||||
"new": "1af33cfc",
|
||||
"desc": "bl settings_send_wrapper (append caps field 100)"
|
||||
},
|
||||
{
|
||||
"offset": 1240038,
|
||||
"old": "f5f710f9",
|
||||
"new": "1af3bafb",
|
||||
"new": "1af3dafd",
|
||||
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
|
||||
},
|
||||
{
|
||||
"offset": 939336,
|
||||
"old": "0bf02af9",
|
||||
"new": "63f325fe",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f146 (fail-open double-tap takeover)"
|
||||
"new": "64f351f8",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f146 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 939528,
|
||||
"old": "0bf0caf8",
|
||||
"new": "63f3c5fd",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f206 (fail-open double-tap takeover)"
|
||||
"new": "64f3bff8",
|
||||
"desc": "bl faceclaw_display_start_headup @ 0x45f206 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 939016,
|
||||
"old": "10f096f8",
|
||||
"new": "65f397fd",
|
||||
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
|
||||
},
|
||||
{
|
||||
"offset": 939036,
|
||||
"old": "fff765fb",
|
||||
"new": "63f3ddff",
|
||||
"desc": "bl faceclaw_idle_input_gate (idle tap/long/release -> field-102 events 2/3/4)"
|
||||
},
|
||||
{
|
||||
"offset": 831036,
|
||||
"old": "1df057fc",
|
||||
"new": "7ff3b3fd",
|
||||
"new": "7ff373ff",
|
||||
"desc": "bl gesture_press (Faceclaw lease gates event 9 vs stock UI event 8)"
|
||||
},
|
||||
{
|
||||
"offset": 831042,
|
||||
"old": "24f023f9",
|
||||
"new": "7ff354fe",
|
||||
"new": "80f314f8",
|
||||
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
|
||||
},
|
||||
{
|
||||
"offset": 831800,
|
||||
"old": "1df0d9fa",
|
||||
"new": "7ff339fc",
|
||||
"new": "7ff3f9fd",
|
||||
"desc": "bl gesture_release (source-qualified event 10 vs stock UI event 0x4a)"
|
||||
},
|
||||
{
|
||||
"offset": 1553752,
|
||||
"old": "7fb50600",
|
||||
"new": "cef2afb9",
|
||||
"new": "cef2f1bb",
|
||||
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
|
||||
},
|
||||
{
|
||||
"offset": 1047796,
|
||||
"old": "f6f7edff",
|
||||
"new": "45f37df9",
|
||||
"new": "45f32ffd",
|
||||
"desc": "bl display_copy_hook @ 0x4798f2 (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1048112,
|
||||
"old": "f6f74fff",
|
||||
"new": "45f3dff8",
|
||||
"new": "45f391fc",
|
||||
"desc": "bl display_copy_hook @ 0x479a2e (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1255404,
|
||||
"old": "d9f758ff",
|
||||
"new": "16f3fbfb",
|
||||
"new": "16f3f9fd",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ac3ea (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1255504,
|
||||
"old": "d9f726ff",
|
||||
"new": "16f3c9fb",
|
||||
"new": "16f3c7fd",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ac44e (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 831998,
|
||||
"old": "1df076fa",
|
||||
"new": "79f3b8fe",
|
||||
"desc": "bl compass_event_forward (global IMU heading -> stock nav BLE notifier)"
|
||||
"offset": 1297700,
|
||||
"old": "65f0affd",
|
||||
"new": "0cf351f8",
|
||||
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
|
||||
},
|
||||
{
|
||||
"offset": 1296176,
|
||||
"old": "fff759fc",
|
||||
"new": "0cf3c7fb",
|
||||
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
|
||||
},
|
||||
{
|
||||
"offset": 4476518,
|
||||
"old": "",
|
||||
"new": "02fb01f000e000bfb0b54ff2ac44c2f229040146206846f23f52c5f28802b2eb500f40f258304cf2e9124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f258304cf20732c2f20000c0f24802104770b500284ed001684ef66962ccf2ff02914218bf70bd90f8081190f8095146f6756c8d42c0f2510c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b142f64d42c0f244022146bde87040104770bd00bf70b542f2ed23c0f2460314460d4698474ff2a841c2f229010e6831466ff39601b1f1005f0ad131684ef66962ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04cf6dd71c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd4ff27d42c0f2590208469047284670bd00bf2de9f04f9db04ff2a84ac2f2290adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66967ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f6b400c2f20000d0f8008004a802f0aaffb8f1000f00f0f881032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f04f8196f8dc10002000298df8140000f0c78105a90a5c002a00f09a810a185378002b00f058819378002b00f05681d378002b00f054811379002b00f052815379002b00f050819379002b00f04e81d379002b00f04c81137a002b00f04a81537a002b00f04881937a002b00f04681d37a002b00f04481137b002b00f04281537b002b00f04081937b002b00f03e81d37b002b00f03c81137c002b00f03a81537c002b00f03881937c002b00f03681d37c002b00f03481137d002b00f03281537d002b00f03081937d002b00f02e81d37d002b00f02c81137e002b00f02a81537e002b00f02881937e002b00f02681d37e002b00f02481137f002b00f02281537f002b00f02081937f002b00f01e81d27f002a00f01c81203060287ff47baf3be19bf82a0270b1dbf82c0240b146f68051c2f207010968401a002800f3c98000208bf82a0240f6d100c0f247001db0bde8f04f00474ff4217004f071fd002800f09c804ff67c51cff6ff71002200bf43182e3183f8842283f8852283f8862283f8872283f8882283f8892283f88a2283f88b2283f88c2283f88d2283f88e2283f88f2283f8902283f8912283f8922283f8932283f8942283f8952283f8962283f8972283f8982283f8992283f89a2283f89b2283f89c2283f89d2283f89e2283f89f2283f8a02283f8a12283f8a22283f8a32283f8a42283f8a52283f8a62283f8a72283f8a82283f8a92283f8aa2283f8ab2283f8ac2283f8ad2283f8ae2283f8af2283f8b02283f8b122a0d1012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf8290240f6d100c0f24700804715e00020caf8000000208bf82802cbf824024ff416304cf60361069005a8c0f24701cdf81480884701208bf82a0204a802f08bfecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0178500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e01Line truncated
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6b4ff2a84416460d46ccf2ff0bc2f229044ff6ff7900274ff00108019006e048f2c731c0f24501bff32f8f8847d4f800a0bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0bb804ff4377005f039f8002800f0a1824ff62451cff6ff7142183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d1c0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001f51457ff436afbff35f8f44e8000121b154e8001f5145f8d02be7aa4645ea064100260127c0f8701206e048f2c731c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0bc804ff4377004f05eff002800f0c6814ff62451cff6ff7100bf42183d3182f8dc6282f8dd6282f8de6282f8df6282f8e06282f8e16282f8e26282f8e36282f8e46282f8e56282f8e66282f8e76282f8e86282f8e96282f8ea6282f8eb6282f8ec6282f8ed6282f8ee6282f8ef6282f8f06282f8f16282f8f26282f8f36282f8f46282f8f56282f8f66282f8f76282f8f86282f8f96282f8fa6282f8fb6282f8fc6282f8fd6282f8fe6282f8ff6282f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382d1c0f800b08772a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff437afbff35f8f00bf44e8000121b154e8001fa942f8d02be7d0f8746242f6f767c0f24407002ebff35f8f00f0eb8007f19a0230464ff0ff314ff0ff379047002840f0418143f67750c0f24f00804700274ff001080397adf8107006e048f2c731c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0e1804ff4377004f068fe002800f0d9804ff62451cff6ff7100bf42183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d1c0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff436afbff35f8f44e8000149b354e8001fa942f8d02be705460020b847b0b105f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b10646f9e64ff0ff3742e007f5a9718847f2e60121f1e70020019d42f6f764002d4ff0010bc0f2440418bfbaf1000f33d10df108098046484602f07df903ab28465146012205f0abfc9df8111007464846002918bf4ff0ff3702f0a3fabbf1000f044608d19df8100028b949f28340c0f247004c308047b8f1000f18bfc8f80c4042f6f764c0f2440404f5807130468847384625b0bde8f08f2a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629b7d140b149f28341c0f247010746884797f8641111b14ff0ff37d6e738464ff0000ba6e770b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f8455046f6756c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04f9fb04ff2a840c2f229000668bff35f8f30466ff39600b0f1005f42d130684ef66c65ccf2ff05a8423bd196f8640130b3d6f860411cb340f6b400c2f20000076804a802f0a5f8002f00f06a8140f2030a384621464ff416320296cff6807a4ff6ff760397fff752fb4ff6245b48f2c738cff6ff7b00270124c0f245081ce096f8660170b1d6f8680140b146f68051c2f207010968401a002802f37080002086f8660140f6d100c0f247001fb0bde8f04f0047bff32f8fc0474ff2a840c2f22900d0f80090bff35f8f0Line truncated
|
||||
"desc": "append injected blobs to main-app payload"
|
||||
},
|
||||
{
|
||||
"offset": 779114,
|
||||
"old": "846a3800",
|
||||
"new": "0e073900",
|
||||
"new": "7c1b3900",
|
||||
"desc": "main-app subheader payload size (ps)"
|
||||
},
|
||||
{
|
||||
"offset": 152,
|
||||
"old": "046b3800",
|
||||
"new": "8e073900",
|
||||
"new": "fc1b3900",
|
||||
"desc": "main-app TOC entry size (ps + 128)"
|
||||
},
|
||||
{
|
||||
"offset": 779234,
|
||||
"old": "846a3804",
|
||||
"new": "0e073904",
|
||||
"new": "7c1b3904",
|
||||
"desc": "main-app preamble length (low 24 bits)"
|
||||
},
|
||||
{
|
||||
"offset": 779238,
|
||||
"old": "85c355a2",
|
||||
"new": "21ab3673",
|
||||
"new": "a1afb7e4",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
|
||||
},
|
||||
{
|
||||
"offset": 156,
|
||||
"old": "ed292ee4",
|
||||
"new": "9e600c9c",
|
||||
"new": "b1adc0d0",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
|
||||
},
|
||||
{
|
||||
"offset": 779118,
|
||||
"old": "ed292ee4",
|
||||
"new": "9e600c9c",
|
||||
"new": "b1adc0d0",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
/**
|
||||
* Shows the Glanceboard while the shell sleeps. The board is not a window:
|
||||
* it lives on its own full-screen opaque compositor surface above the shell
|
||||
* (below only the lock screen), so it never composites with the sidebar,
|
||||
* top bar, or the sleeping app's retained frame. The host owns that surface,
|
||||
* the visibility state machine (glance-state.ts), its auto-hide timer, and
|
||||
* the board's render loop; the controller feeds it sleep-time input and the
|
||||
* wake barrier, and takes the compositor back to blank when the board hides.
|
||||
*/
|
||||
import { GrayImage, G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
|
||||
import { prepareFrameDraws } from "../graphics/glyph-wire";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
|
||||
import * as frameTimings from "../native/frame-timings";
|
||||
import { type DisplayTarget } from "../native/preview-display";
|
||||
import { minWindowTop } from "../ui/shell/geometry";
|
||||
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||
import { type GlanceBoardInstance, type GlanceboardProvider } from "../apps/app-definition";
|
||||
import { GLANCE_HIDDEN, glanceEventForGesture, reduceGlance, type GlanceEvent, type GlanceState } from "./glance-state";
|
||||
|
||||
export const GLANCE_SURFACE_ID = "glance";
|
||||
/** Above the shell (1) and every window (0); below the lock screen (1000). */
|
||||
export const GLANCE_SURFACE_Z_ORDER = 900;
|
||||
|
||||
export type GlanceHostOptions = {
|
||||
getDisplay: () => DisplayTarget | null;
|
||||
getProvider: () => GlanceboardProvider | null;
|
||||
/** Whether a board may show right now (connected or preview, not locked). */
|
||||
canShow: () => boolean;
|
||||
/**
|
||||
* The controller's wake barrier: restore the EvenHub session if it was
|
||||
* suspended and unblank the compositor. Resolves once the frame is visible.
|
||||
*/
|
||||
ensureSessionActive: (frameId: number) => Promise<boolean>;
|
||||
/**
|
||||
* The board hid on its own (timer or release) with the shell still
|
||||
* asleep: the controller re-blanks and re-arms the screen-off power save.
|
||||
*/
|
||||
onHiddenWhileAsleep: () => void;
|
||||
/** Visibility changed either way (phone mirror refresh). */
|
||||
onVisibilityChanged: () => void;
|
||||
appendLog: (message: string) => void;
|
||||
};
|
||||
|
||||
export class GlanceHost {
|
||||
private state: GlanceState = GLANCE_HIDDEN;
|
||||
private board: GlanceBoardInstance | null = null;
|
||||
/** Whether the surface is currently visible on the compositor. */
|
||||
private shown = false;
|
||||
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private configuredFor: DisplayTarget | null = null;
|
||||
/** Serializes show/hide against each other and against in-flight renders. */
|
||||
private queue: Promise<void> = Promise.resolve();
|
||||
private rendering = false;
|
||||
private renderPending = false;
|
||||
private nextRenderWantsFreshData = false;
|
||||
|
||||
constructor(private readonly options: GlanceHostOptions) {}
|
||||
|
||||
isVisible(): boolean {
|
||||
return this.state.visible;
|
||||
}
|
||||
|
||||
/** Whether sleep-time gestures should reach the board at all. */
|
||||
isEnabled(): boolean {
|
||||
return this.options.getProvider()?.isEnabled() ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The glance event a sleep-time gesture means under the board's settings,
|
||||
* or null when the shell should see the input as usual: nothing while the
|
||||
* board is disabled, no press from a tap when "Show on tap" is disabled,
|
||||
* no hold when "Show on long press" is off, no press from a head-tilt when
|
||||
* "Show on head tilt" is off (it then wakes the regular UI). A release
|
||||
* always passes so a hold in progress can end.
|
||||
*/
|
||||
eventForGesture(gesture: "head-tilt" | string): GlanceEvent | null {
|
||||
const provider = this.options.getProvider();
|
||||
if (!provider?.isEnabled()) return null;
|
||||
if (gesture === "click" && !provider.showOnTap()) return null;
|
||||
if ((gesture === "long-press" || gesture === "short-then-long-press") && !provider.showOnLongPress()) return null;
|
||||
if (gesture === "head-tilt" && !provider.showOnHeadTilt()) return null;
|
||||
return glanceEventForGesture(gesture, this.isVisible());
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a glance event from sleep-time input. The frame is owned from here:
|
||||
* a show submits it with the board's first frame, anything else finishes it.
|
||||
*/
|
||||
async handleEvent(event: GlanceEvent, frameId: number): Promise<void> {
|
||||
const before = this.state;
|
||||
const timeoutMs = this.options.getProvider()?.tapTimeoutMs();
|
||||
this.state = reduceGlance(before, event, Date.now(), timeoutMs);
|
||||
this.armHideTimer();
|
||||
if (this.state.visible === before.visible) {
|
||||
frameTimings.finishFrame(frameId, this.state.visible ? "glanceboard stays up" : "glance event while hidden");
|
||||
return;
|
||||
}
|
||||
if (this.state.visible) {
|
||||
await this.enqueue(() => this.show(frameId));
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "glanceboard hidden");
|
||||
await this.enqueue(() => this.hide(true));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide because the regular UI is waking (double-tap, a notification, ...):
|
||||
* the shell's own wake path handles the compositor from here, so no re-blank.
|
||||
*/
|
||||
dismiss(): void {
|
||||
if (!this.state.visible && !this.shown && !this.board) return;
|
||||
this.state = GLANCE_HIDDEN;
|
||||
this.armHideTimer();
|
||||
void this.enqueue(() => this.hide(false));
|
||||
}
|
||||
|
||||
/** The display target went away (disconnect, preview teardown): forget everything. */
|
||||
reset(): void {
|
||||
this.state = GLANCE_HIDDEN;
|
||||
this.armHideTimer();
|
||||
this.board?.stop();
|
||||
this.board = null;
|
||||
this.shown = false;
|
||||
this.configuredFor = null;
|
||||
this.renderPending = false;
|
||||
}
|
||||
|
||||
private enqueue(operation: () => Promise<void>): Promise<void> {
|
||||
const run = this.queue.then(operation, operation).catch((error) => {
|
||||
this.options.appendLog(`glanceboard operation failed: ${String(error)}`);
|
||||
});
|
||||
this.queue = run;
|
||||
return run;
|
||||
}
|
||||
|
||||
private armHideTimer(): void {
|
||||
if (this.hideTimer !== null) clearTimeout(this.hideTimer);
|
||||
this.hideTimer = null;
|
||||
const hideAtMs = this.state.hideAtMs;
|
||||
if (!this.state.visible || hideAtMs === null) return;
|
||||
this.hideTimer = setTimeout(() => {
|
||||
this.hideTimer = null;
|
||||
void this.handleEvent({ type: "timeout" }, frameTimings.startFrame("glanceboard timeout"));
|
||||
}, Math.max(0, hideAtMs - Date.now()));
|
||||
}
|
||||
|
||||
private async configureSurface(display: DisplayTarget): Promise<void> {
|
||||
await display.configureSurface(GLANCE_SURFACE_ID, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: G2_LENS_WIDTH,
|
||||
height: G2_LENS_HEIGHT,
|
||||
zOrder: GLANCE_SURFACE_Z_ORDER,
|
||||
transparency: "opaque",
|
||||
});
|
||||
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
|
||||
this.configuredFor = display;
|
||||
}
|
||||
|
||||
private async show(frameId: number): Promise<void> {
|
||||
if (!this.state.visible) {
|
||||
// Hidden again before this ran (a quick release); nothing to do.
|
||||
frameTimings.finishFrame(frameId, "glanceboard show superseded");
|
||||
return;
|
||||
}
|
||||
const display = this.options.getDisplay();
|
||||
const provider = this.options.getProvider();
|
||||
if (!display || !provider || !this.options.canShow()) {
|
||||
this.state = GLANCE_HIDDEN;
|
||||
this.armHideTimer();
|
||||
frameTimings.finishFrame(frameId, "glanceboard unavailable");
|
||||
return;
|
||||
}
|
||||
if (this.configuredFor !== display) await this.configureSurface(display);
|
||||
if (!this.board) {
|
||||
this.board = provider.createBoard(() => this.requestRender());
|
||||
this.board.start();
|
||||
}
|
||||
// Submit the first frame and reveal the surface while the compositor is
|
||||
// still blanked, so the unblank below composites the board on top of the
|
||||
// sleeping UI in one step rather than flashing the app first.
|
||||
await this.render(frameId);
|
||||
await display.setSurfaceVisible(GLANCE_SURFACE_ID, true);
|
||||
this.shown = true;
|
||||
const ready = await this.options.ensureSessionActive(frameId);
|
||||
if (!ready) this.options.appendLog("glanceboard: wake barrier did not complete");
|
||||
this.options.onVisibilityChanged();
|
||||
}
|
||||
|
||||
private async hide(stillAsleep: boolean): Promise<void> {
|
||||
const wasShown = this.shown;
|
||||
this.shown = false;
|
||||
const display = this.options.getDisplay();
|
||||
if (wasShown && display && this.configuredFor === display) {
|
||||
// Blank before revealing what is under the board: hiding the surface
|
||||
// first recomposites the sleeping UI's retained frames and transmits
|
||||
// them, a visible flash of the main UI before the screen goes dark.
|
||||
// Blanked, the surface change composites to the same black frame. (The
|
||||
// controller's screen-off path below blanks again, harmlessly.)
|
||||
if (stillAsleep) {
|
||||
await display.setScreenBlanked(true);
|
||||
}
|
||||
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
|
||||
}
|
||||
this.board?.stop();
|
||||
this.board = null;
|
||||
this.renderPending = false;
|
||||
if (wasShown && stillAsleep) this.options.onHiddenWhileAsleep();
|
||||
if (wasShown) this.options.onVisibilityChanged();
|
||||
}
|
||||
|
||||
private requestRender(): void {
|
||||
if (!this.board) return;
|
||||
if (this.rendering) {
|
||||
this.renderPending = true;
|
||||
return;
|
||||
}
|
||||
void this.enqueue(() => this.render(frameTimings.startFrame("render:glanceboard")));
|
||||
}
|
||||
|
||||
/** Paint the board into a full-screen frame and submit it to the surface. */
|
||||
private async render(frameId: number): Promise<void> {
|
||||
const board = this.board;
|
||||
const display = this.options.getDisplay();
|
||||
if (!board || !display || this.configuredFor !== display) {
|
||||
frameTimings.finishFrame(frameId, "discarded: glanceboard not showing");
|
||||
return;
|
||||
}
|
||||
this.rendering = true;
|
||||
try {
|
||||
frameTimings.annotateFrame(frameId, "glanceboard");
|
||||
const wantFreshData = this.nextRenderWantsFreshData;
|
||||
this.nextRenderWantsFreshData = false;
|
||||
beginRenderPass(!wantFreshData);
|
||||
const paintStartedAtMs = Date.now();
|
||||
const frame = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => {
|
||||
const boardImage = board.paint();
|
||||
const full = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||
// Centred horizontally in the panel, in the standard window band
|
||||
// vertically (Display > Vertical position).
|
||||
const x = Math.max(0, ((G2_LENS_WIDTH - boardImage.width) / 2) | 0);
|
||||
const y = Math.max(0, Math.min(G2_LENS_HEIGHT - boardImage.height, minWindowTop()));
|
||||
boardImage.composeInto(full, x, y);
|
||||
return full;
|
||||
}),
|
||||
);
|
||||
const paintUsedStaleData = endRenderPass();
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const planes: Plane[] = [{ image: frame, x: 0, y: 0 }];
|
||||
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", () =>
|
||||
display.submitSurfaceFrame(
|
||||
GLANCE_SURFACE_ID,
|
||||
buffer,
|
||||
{ x: 0, y: 0, width: image.width, height: image.height },
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
preparedDraws,
|
||||
),
|
||||
);
|
||||
if (paintUsedStaleData) {
|
||||
// Same contract as the windows: a paint that used cached data (the
|
||||
// notification icons) gets one follow-up with fresh data.
|
||||
this.nextRenderWantsFreshData = true;
|
||||
this.renderPending = true;
|
||||
}
|
||||
} finally {
|
||||
this.rendering = false;
|
||||
if (this.renderPending && this.board) {
|
||||
this.renderPending = false;
|
||||
this.requestRender();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* The Glanceboard's visibility rules, as a pure reducer so they can be tested
|
||||
* without the display plumbing. The board is an alternate sleep-time display:
|
||||
* while the shell is asleep a press (single tap, or a head-tilt wake) shows it
|
||||
* for a short while, a hold (long-press or short-then-long-press) shows it until release,
|
||||
* and a double-tap dismisses it in favour of the regular UI. GlanceHost feeds
|
||||
* these events in and applies the resulting visibility to the compositor.
|
||||
*
|
||||
* No NativeScript imports: this module is compiled and run under plain node
|
||||
* by tests/glance-state.test.cjs.
|
||||
*/
|
||||
|
||||
/** Fallback press timeout when the host supplies none; each further press restarts it. */
|
||||
export const GLANCE_TIMEOUT_MS = 3000;
|
||||
|
||||
export type GlanceState = {
|
||||
visible: boolean;
|
||||
/** A long-press is holding the board up; the timer does not run meanwhile. */
|
||||
holding: boolean;
|
||||
/** When the board hides on its own (epoch ms), or null while held / hidden. */
|
||||
hideAtMs: number | null;
|
||||
};
|
||||
|
||||
export type GlanceEvent =
|
||||
/** Single tap or head-tilt wake: show, and (re)start the auto-hide timer. */
|
||||
| { type: "press" }
|
||||
/** Long-press: show and keep showing until the release. */
|
||||
| { type: "hold" }
|
||||
/** The long-press ended. */
|
||||
| { type: "release" }
|
||||
/** The auto-hide timer fired. */
|
||||
| { type: "timeout" }
|
||||
/** Double-tap (or any other wake of the regular UI): hide now. */
|
||||
| { type: "dismiss" };
|
||||
|
||||
export const GLANCE_HIDDEN: GlanceState = Object.freeze({ visible: false, holding: false, hideAtMs: null });
|
||||
|
||||
export function reduceGlance(
|
||||
state: GlanceState,
|
||||
event: GlanceEvent,
|
||||
nowMs: number,
|
||||
timeoutMs: number = GLANCE_TIMEOUT_MS,
|
||||
): GlanceState {
|
||||
switch (event.type) {
|
||||
case "press":
|
||||
// A tap during a hold changes nothing: the hold decides when it ends.
|
||||
if (state.holding) return state;
|
||||
return { visible: true, holding: false, hideAtMs: nowMs + timeoutMs };
|
||||
case "hold":
|
||||
return { visible: true, holding: true, hideAtMs: null };
|
||||
case "release":
|
||||
// Releases delivered late or after a disabled hold gesture mean nothing here.
|
||||
return state.holding ? GLANCE_HIDDEN : state;
|
||||
case "timeout":
|
||||
if (!state.visible || state.holding || state.hideAtMs === null || nowMs < state.hideAtMs) return state;
|
||||
return GLANCE_HIDDEN;
|
||||
case "dismiss":
|
||||
return GLANCE_HIDDEN;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Which glance event, if any, a shell input gesture means while the screen is
|
||||
* off. A double-tap only concerns the board while it is showing (it then
|
||||
* dismisses it before waking the regular UI); everything else the shell
|
||||
* already handles or ignores while asleep.
|
||||
*/
|
||||
export function glanceEventForGesture(
|
||||
gesture: "click" | "double-click" | "long-press" | "short-then-long-press" | "long-press-release" | "head-tilt" | string,
|
||||
boardVisible: boolean,
|
||||
): GlanceEvent | null {
|
||||
switch (gesture) {
|
||||
case "click":
|
||||
case "head-tilt":
|
||||
return { type: "press" };
|
||||
case "long-press":
|
||||
case "short-then-long-press":
|
||||
return { type: "hold" };
|
||||
case "long-press-release":
|
||||
return { type: "release" };
|
||||
case "double-click":
|
||||
return boardVisible ? { type: "dismiss" } : null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+115
-31
@@ -1,6 +1,8 @@
|
||||
import * as protocol from './ble-protocol'
|
||||
import { iosBleTraffic } from './ble-traffic-counters'
|
||||
import { buildBoundingBoxPayload } from './ble-image-optimizer'
|
||||
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
|
||||
import { CfwTransport, CFW_MAX_MESSAGE, parseCfwAcks } from './cfw-transport'
|
||||
import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLAW_FIRMWARE_VERSION } from './firmware-compat'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
|
||||
@@ -25,7 +27,9 @@ class AckTimeout extends Error {}
|
||||
// Match Android ConnectionOptions.WINDOW_SIZE. This bounds complete image
|
||||
// messages queued for writing or awaiting ACK, not individual BLE packets.
|
||||
const DISPLAY_WINDOW_SIZE = 3
|
||||
type DisplayFrame = { packed: Uint8Array; payload: Uint8Array; offset: number; pending: number }
|
||||
type CfwPending = { payload: Uint8Array; checksum: number; role: string; magic: number; ackLenses: number; retries: number;
|
||||
retryPending: boolean; deadline: number; resolve: () => void; reject: (error: Error) => void }
|
||||
type DisplayFrame = { packed: Uint8Array; commands: Uint8Array[]; offset: number; pending: number }
|
||||
|
||||
/** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */
|
||||
export class GlassesSession {
|
||||
@@ -34,6 +38,9 @@ export class GlassesSession {
|
||||
private generation = 0
|
||||
private ids: Record<string, string> = {}
|
||||
private limits: Record<string, number> = {}
|
||||
private cfwPending: CfwPending[] = []
|
||||
private cfwTransports = new Map<string, CfwTransport>()
|
||||
private cfwTimer: number | null = null
|
||||
private pending = new Map<string, PendingAck>()
|
||||
private receiver = new protocol.MessageReceiver()
|
||||
private writes = new Map<string, Promise<void>>()
|
||||
@@ -68,7 +75,6 @@ export class GlassesSession {
|
||||
private microphoneWork: Promise<void> = Promise.resolve()
|
||||
private microphoneEnabled = false
|
||||
constructor(private readonly transport: SessionTransport,
|
||||
private readonly deflate: (data: Uint8Array) => Uint8Array,
|
||||
private readonly onState: (state: SessionState) => void,
|
||||
private readonly onInput: (input: protocol.GlassesInput) => void,
|
||||
private readonly log: (message: string) => void = () => {},
|
||||
@@ -114,9 +120,9 @@ export class GlassesSession {
|
||||
this.update('connecting', 'Checking glasses firmware…')
|
||||
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Settings query')
|
||||
this.applySettings(settings)
|
||||
const caps = this.state.capabilities.split(/\s+/)
|
||||
if (!['img640', 'fbguard', 'wearnotify'].every(cap => caps.includes(cap)))
|
||||
throw new Error('The glasses do not report compatible Faceclaw firmware (img640, fbguard, wearnotify). Bluetooth is working, but this display requires the modified firmware. Flashing remains available on Android.')
|
||||
const firmware = { leftVersion: this.state.leftVersion, rightVersion: this.state.rightVersion, extension: this.state.capabilities }
|
||||
if (!hasCompatibleFirmware(firmware))
|
||||
throw new Error((firmwareIncompatibilityMessage(firmware) || `The glasses must report Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION} or newer.`) + ' Flashing remains available on Android.')
|
||||
this.update('connecting', 'Starting glasses display…')
|
||||
await this.request('right', protocol.SID.launch, () => protocol.prelude(), 'App launch', 3500, 0x20, 156)
|
||||
await this.lease(true)
|
||||
@@ -176,7 +182,7 @@ export class GlassesSession {
|
||||
private nextMagic(): number {
|
||||
for (let i = 0; i < 156; i++) {
|
||||
const magic = this.magic; this.magic = this.magic === 255 ? 100 : this.magic + 1
|
||||
if (magic !== 156 && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
|
||||
if (magic !== 156 && !this.cfwPending.some(item => item.magic === magic) && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
|
||||
}
|
||||
throw new Error('All message identifiers are in use')
|
||||
}
|
||||
@@ -200,15 +206,20 @@ export class GlassesSession {
|
||||
})
|
||||
}
|
||||
private send(role: string, sid: number, flag: number, payload: Uint8Array): Promise<void> {
|
||||
return this.writePackets(role, () => protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role]))
|
||||
}
|
||||
private writePackets(role: string, build: () => Uint8Array[], current = () => true): Promise<void> {
|
||||
const identifier = this.ids[role], generation = this.generation
|
||||
if (!identifier) return Promise.reject(new Error(`No ${role} device connected`))
|
||||
const frames = protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role])
|
||||
const previous = this.writes.get(identifier) ?? Promise.resolve()
|
||||
// A failed write poisons this link's queue until reset: do not transmit
|
||||
// later fragments after a hole in the image stream.
|
||||
const work = previous.then(async () => {
|
||||
for (const frame of frames) {
|
||||
this.check(generation)
|
||||
if (!current()) return
|
||||
for (const frame of build()) {
|
||||
this.check(generation)
|
||||
if (!current()) return
|
||||
await this.transport.write(identifier, protocol.G2_WRITE, frame)
|
||||
iosBleTraffic.recordWrite(frame.length)
|
||||
}
|
||||
@@ -221,6 +232,75 @@ export class GlassesSession {
|
||||
private lease(acquire: boolean): Promise<void[]> {
|
||||
return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire))))
|
||||
}
|
||||
private requestCfw(role: string, payload: Uint8Array): Promise<void> {
|
||||
if (payload.length > CFW_MAX_MESSAGE) return Promise.reject(new Error('CFW message too large'))
|
||||
return new Promise((resolve, reject) => {
|
||||
const item: CfwPending = { role, payload, checksum: protocol.crc16(payload), magic: this.nextMagic(), ackLenses: 0, retries: 0,
|
||||
retryPending: false, deadline: 0, resolve, reject }
|
||||
this.cfwPending.push(item)
|
||||
this.sendCfw(item)
|
||||
})
|
||||
}
|
||||
private sendCfw(item: CfwPending): void {
|
||||
const generation = this.generation, magic = item.magic
|
||||
const current = () => generation === this.generation && item.magic === magic && this.cfwPending.includes(item)
|
||||
void this.writePackets(item.role, () => {
|
||||
let transport = this.cfwTransports.get(item.role)
|
||||
if (!transport) { transport = new CfwTransport(); this.cfwTransports.set(item.role, transport) }
|
||||
if (item.retries) transport.reset()
|
||||
return transport.encode(item.payload, magic, 3, this.limits[item.role])
|
||||
}, current).then(() => {
|
||||
if (!current()) return
|
||||
item.deadline = Date.now() + 500
|
||||
this.scheduleCfwRecovery()
|
||||
}).catch(error => { if (current()) this.fail(error, true) })
|
||||
}
|
||||
private receiveCfw(identifier: string, packet: Uint8Array): void {
|
||||
const acks = parseCfwAcks(packet)
|
||||
if (!acks) return
|
||||
for (const ack of acks) {
|
||||
// The ingress temple relays the other lens's result on the SAME link.
|
||||
const item = this.cfwPending.find(p => this.ids[p.role] === identifier && p.magic === ack.streamId)
|
||||
if (!item || ack.messageId !== 0) continue
|
||||
if (ack.nack) item.retryPending = true
|
||||
else if (ack.size === item.payload.length && ack.checksum === item.checksum) item.ackLenses |= ack.lens
|
||||
this.log(`CFW ${ack.nack ? 'NACK' : 'ACK'} stream=${ack.streamId} lens=${ack.lens} size=${ack.size} crc=${ack.checksum} confirmed=${item.ackLenses}`)
|
||||
}
|
||||
while (this.cfwPending.length) {
|
||||
const head = this.cfwPending[0]
|
||||
if (head.retryPending || head.ackLenses !== 3) break
|
||||
this.cfwPending.shift(); head.resolve()
|
||||
}
|
||||
this.scheduleCfwRecovery()
|
||||
}
|
||||
private scheduleCfwRecovery(): void {
|
||||
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
|
||||
this.cfwTimer = null
|
||||
let deadline = Infinity
|
||||
for (const item of this.cfwPending) {
|
||||
if (item.retryPending) deadline = 0
|
||||
else if (item.ackLenses !== 3 && item.deadline) deadline = Math.min(deadline, item.deadline)
|
||||
}
|
||||
if (deadline === Infinity) return
|
||||
this.cfwTimer = setTimeout(() => {
|
||||
this.cfwTimer = null
|
||||
this.recoverCfw()
|
||||
}, Math.max(0, deadline - Date.now()))
|
||||
}
|
||||
private recoverCfw(): void {
|
||||
if (!this.cfwPending.some(p => p.retryPending || (p.ackLenses !== 3 && p.deadline > 0 && p.deadline <= Date.now()))) {
|
||||
this.scheduleCfwRecovery(); return
|
||||
}
|
||||
if (this.cfwPending.some(p => p.retries >= 3)) { this.fail(new Error('CFW recovery retry limit'), true); return }
|
||||
this.log(`CFW recovery: replay ${this.cfwPending.length} unresolved messages`)
|
||||
// Go back to the oldest unresolved command, including any ACKed tail.
|
||||
// Fresh stream IDs reject stale ACKs; each replay resets compression history.
|
||||
for (const item of this.cfwPending) {
|
||||
item.magic = this.nextMagic(); item.retries++; item.ackLenses = 0
|
||||
item.deadline = 0; item.retryPending = false
|
||||
}
|
||||
for (const item of this.cfwPending) this.sendCfw(item)
|
||||
}
|
||||
private receive(event: TransportEvent): void {
|
||||
if (!Object.values(this.ids).includes(event.identifier ?? '')) return
|
||||
if (event.kind === 'disconnected') {
|
||||
@@ -252,6 +332,10 @@ export class GlassesSession {
|
||||
}
|
||||
for (const message of this.receiver.receive(event.identifier!, data)) {
|
||||
this.log(`RX ${event.identifier === this.ids.left ? 'L' : 'R'} sid=${message.sid.toString(16)} flag=${message.flag.toString(16)} cmd=${message.command} magic=${message.magic}`)
|
||||
if (message.sid === protocol.SID.cfw) {
|
||||
this.receiveCfw(event.identifier!, message.packet!)
|
||||
continue
|
||||
}
|
||||
if (![1, 6].includes(message.flag)) {
|
||||
// A master arm can relay the other arm's ACK; magic is allocated
|
||||
// across BOTH links, matching Android's communicator.
|
||||
@@ -343,32 +427,32 @@ export class GlassesSession {
|
||||
// Compare with the last enqueued image, not the last ACKed one. In an
|
||||
// A -> B -> A sequence, B may still be in flight when A is requested.
|
||||
if (this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
|
||||
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId, this.deflate)
|
||||
const payload = delta ?? protocol.concat(new Uint8Array([6]), this.deflate(protocol.rle4(packed)))
|
||||
if (payload.length > 165888) throw new Error('Compressed frame exceeds the glasses image buffer')
|
||||
if (delta) {
|
||||
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
|
||||
const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed))
|
||||
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
const commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
|
||||
if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands)
|
||||
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
this.log(`Display bbox ${delta[3] * 4}x${delta[4] * 2}+${delta[1] * 4}+${delta[2] * 2} fid=${delta[5] | (delta[6] << 8)} (${payload.length} bytes)`)
|
||||
} else this.log(`Display full 640x480 (${payload.length} bytes)`)
|
||||
this.displaySending = { packed, payload, offset: 0, pending: 0 }
|
||||
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
|
||||
this.displaySending = { packed, commands, offset: 0, pending: 0 }
|
||||
this.displayFrames.push(this.displaySending); this.lastEnqueued = packed
|
||||
}
|
||||
const frame = this.displaySending, offset = frame.offset
|
||||
frame.offset += 3800; frame.pending++; this.displayInFlight++
|
||||
if (frame.offset >= frame.payload.length) this.displaySending = null
|
||||
// send() serializes whole messages on L, but request() waits for each
|
||||
// magic's ACK independently. Fill the other slots without awaiting it.
|
||||
void this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, frame.payload, offset), 'Display frame').then(() => {
|
||||
const frame = this.displaySending, payload = frame.commands[frame.offset++]
|
||||
frame.pending++; this.displayInFlight++
|
||||
if (frame.offset >= frame.commands.length) this.displaySending = null
|
||||
// Writes serialize on L while up to three complete commands await their
|
||||
// two-lens ACKs. Retire commands in submission order.
|
||||
void this.requestCfw('left', payload).then(() => {
|
||||
if (generation !== this.generation) return
|
||||
frame.pending--; this.displayInFlight--; this.lastHeartbeat = Date.now()
|
||||
// Retire in submission order even if relayed ACKs arrive out of order.
|
||||
// A frame counts only after every fragment has been acknowledged.
|
||||
// A frame counts only after every command has been acknowledged.
|
||||
while (this.displayFrames.length) {
|
||||
const completed = this.displayFrames[0]
|
||||
if (completed.offset < completed.payload.length || completed.pending) break
|
||||
if (completed.offset < completed.commands.length || completed.pending) break
|
||||
this.displayFrames.shift(); this.displayed = completed.packed
|
||||
this.state.frames++; iosBleTraffic.recordDisplayFrame(); this.onState({ ...this.state })
|
||||
this.log(`Display frame ${this.state.frames} acknowledged (${completed.payload.length} bytes)`)
|
||||
this.log(`Display frame ${this.state.frames} acknowledged (${completed.commands.length} commands)`)
|
||||
}
|
||||
this.wake()
|
||||
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
|
||||
@@ -404,6 +488,11 @@ export class GlassesSession {
|
||||
this.timer = this.retryTimer = null; this.pumping = false
|
||||
const pending = [...this.pending.values()]; this.pending.clear()
|
||||
for (const request of pending) { if (request.timer) clearTimeout(request.timer); request.reject(new Error('Connection ended')) }
|
||||
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
|
||||
this.cfwTimer = null
|
||||
const custom = this.cfwPending; this.cfwPending = []
|
||||
for (const item of custom) item.reject(new Error('Connection ended'))
|
||||
this.cfwTransports.clear()
|
||||
this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null
|
||||
this.lastEnqueued = null; this.displayFrames = []; this.displaySending = null; this.displayInFlight = 0
|
||||
}
|
||||
@@ -436,12 +525,7 @@ export class GlassesSession {
|
||||
this.update('disconnecting', 'Disconnecting…'); this.reset()
|
||||
try {
|
||||
if (cleanup) {
|
||||
if (this.state.capabilities.split(/\s+/).includes('cleanup11'))
|
||||
await this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, new Uint8Array([11]), 0), 'Display cleanup', 1200)
|
||||
else {
|
||||
await this.request('right', protocol.SID.hub, protocol.shutdown, 'Close display', 1200)
|
||||
await this.lease(false)
|
||||
}
|
||||
await this.requestCfw('left', new Uint8Array([11]))
|
||||
}
|
||||
} catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) }
|
||||
finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') }
|
||||
|
||||
@@ -308,8 +308,7 @@ export class IosPreviewController {
|
||||
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
|
||||
if (error) { this.onError(error); return }
|
||||
if (!this.session) {
|
||||
const { deflate } = require('pako') as { deflate: (data: Uint8Array) => Uint8Array }
|
||||
this.session = new GlassesSession(iosBluetooth(), deflate, state => {
|
||||
this.session = new GlassesSession(iosBluetooth(), state => {
|
||||
if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
|
||||
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging })
|
||||
this.syncRuntime()
|
||||
|
||||
+71
-2
@@ -14,6 +14,12 @@
|
||||
* stream to the watch as events so a query made from the wrist can be read
|
||||
* from the wrist.
|
||||
*
|
||||
* The watch's own battery flows the other way for the glasses' top-bar and
|
||||
* Glanceboard indicators: the phone polls it (the watch answers from a
|
||||
* listener service even while its app is closed) and the open watch app
|
||||
* pushes changes unprompted. The level lives in shell.getBatteryLevels()
|
||||
* beside the G2 and R1 ones and is cleared when no watch is reachable.
|
||||
*
|
||||
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge); the
|
||||
* message shapes are documented in wear/PROTOCOL.md.
|
||||
*/
|
||||
@@ -88,6 +94,14 @@ const MAX_TEXT_LENGTH = 2000;
|
||||
const STATE_PUBLISH_DEBOUNCE_MS = 150;
|
||||
const STATE_REFRESH_INTERVAL_MS = 30_000;
|
||||
const ASSISTANT_STREAM_MIN_INTERVAL_MS = 250;
|
||||
// Each poll wakes the watch app's process for a moment; the level changes
|
||||
// slowly, and a charger being plugged in is pushed at once whenever the
|
||||
// watch app is open, so a leisurely cadence is enough.
|
||||
const WATCH_BATTERY_POLL_INTERVAL_MS = 5 * 60_000;
|
||||
// Floor between polls that state changes (a watch coming into range, the
|
||||
// glasses connecting) would otherwise trigger back to back.
|
||||
const WATCH_BATTERY_POLL_MIN_GAP_MS = 20_000;
|
||||
const WATCH_BATTERY_URGENT_POLL_LIMIT = 3;
|
||||
|
||||
/** What the dashboard controller lends the watch remote. */
|
||||
export type WearRemoteHost = {
|
||||
@@ -120,6 +134,11 @@ export class WearRemote {
|
||||
private assistantStreamTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private assistantPendingText: string | null = null;
|
||||
private lastAssistantSentAtMs = 0;
|
||||
private lastWatchBatteryPollAtMs = 0;
|
||||
private watchBatteryKnown = false;
|
||||
// Urgent polls that got no answer; a watch app too old to answer them
|
||||
// must not be poked every publish, so after a few the cadence drops back.
|
||||
private watchBatteryPollsUnanswered = 0;
|
||||
|
||||
constructor(private readonly host: WearRemoteHost) {
|
||||
this.active = wearBridge.isAvailable();
|
||||
@@ -141,8 +160,16 @@ export class WearRemote {
|
||||
connection.reachable ? `watch: ${connection.watchName || "watch"} reachable` : "watch: no watch reachable",
|
||||
);
|
||||
}
|
||||
// A watch that just came into range gets the current state at once.
|
||||
if (connection.reachable) this.publishNow(true);
|
||||
// A watch that just came into range gets the current state at once,
|
||||
// and is asked for its battery; one that left takes its indicator
|
||||
// with it (no placeholder).
|
||||
if (changed) this.watchBatteryPollsUnanswered = 0;
|
||||
if (connection.reachable) {
|
||||
this.publishNow(true);
|
||||
this.pollWatchBattery(true);
|
||||
} else if (changed) {
|
||||
this.setWatchBattery(null, null);
|
||||
}
|
||||
});
|
||||
shell.onAssistantActivity((event) => this.forwardAssistantActivity(event));
|
||||
shell.onAlertShown((text) => {
|
||||
@@ -155,9 +182,38 @@ export class WearRemote {
|
||||
// Catch-all refresh for state the controller does not announce (cheap:
|
||||
// identical states are dropped on the Java side).
|
||||
setInterval(() => this.schedulePublish(), STATE_REFRESH_INTERVAL_MS);
|
||||
setInterval(() => this.pollWatchBattery(false), WATCH_BATTERY_POLL_INTERVAL_MS);
|
||||
this.schedulePublish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask the reachable watch for its battery. `urgent` skips the poll gap for
|
||||
* a level that is not known yet (the glasses just connected, the watch
|
||||
* just appeared); a level already on screen waits for the regular cadence.
|
||||
*/
|
||||
private pollWatchBattery(urgent: boolean): void {
|
||||
if (!this.active || !this.watchReachable) return;
|
||||
const now = Date.now();
|
||||
const hurry = urgent && !this.watchBatteryKnown && this.watchBatteryPollsUnanswered < WATCH_BATTERY_URGENT_POLL_LIMIT;
|
||||
const gap = hurry ? WATCH_BATTERY_POLL_MIN_GAP_MS : WATCH_BATTERY_POLL_INTERVAL_MS;
|
||||
if (now - this.lastWatchBatteryPollAtMs < gap) return;
|
||||
this.lastWatchBatteryPollAtMs = now;
|
||||
this.watchBatteryPollsUnanswered++;
|
||||
wearBridge.sendToWatch(WEAR_PATHS.batteryRequest, {});
|
||||
}
|
||||
|
||||
/** Record a watch battery report (or its absence) and repaint the indicators. */
|
||||
private setWatchBattery(battery: number | null, charging: boolean | null): void {
|
||||
const current = shell.getBatteryLevels();
|
||||
if (current.watch === battery && current.watchCharging === charging) return;
|
||||
this.watchBatteryKnown = battery !== null;
|
||||
shell.setBatteryLevels({ watch: battery, watchCharging: charging });
|
||||
// Repaint the top bar, as the controller does for G2/R1 reports.
|
||||
const phase = this.host.getState().phase;
|
||||
if (phase === "connected" || phase === "charging") this.host.requestShellRender();
|
||||
this.host.appendLog(battery === null ? "watch: battery unknown" : `watch: battery ${battery}%${charging ? " (charging)" : ""}`);
|
||||
}
|
||||
|
||||
/** Whether a watch running the Faceclaw watch app is reachable right now. */
|
||||
isWatchReachable(): boolean {
|
||||
return this.watchReachable;
|
||||
@@ -183,6 +239,10 @@ export class WearRemote {
|
||||
} catch (error) {
|
||||
this.host.appendLog(`watch: state publish failed: ${formatError(error)}`);
|
||||
}
|
||||
// The indicator is only ever drawn while the glasses are connected, so
|
||||
// a still-unknown level is worth asking for as soon as they are.
|
||||
const phase = this.host.getState().phase;
|
||||
if (phase === "connected" || phase === "charging") this.pollWatchBattery(true);
|
||||
}
|
||||
|
||||
private buildState(): Record<string, unknown> {
|
||||
@@ -240,6 +300,15 @@ export class WearRemote {
|
||||
ack(true);
|
||||
return;
|
||||
}
|
||||
if (message.path === WEAR_PATHS.battery) {
|
||||
// Status, not control: not gated by the remote setting and never
|
||||
// acked (the watch sends it fire-and-forget, without a seq).
|
||||
this.watchBatteryPollsUnanswered = 0;
|
||||
const battery = readNumber(message.payload.battery, -1);
|
||||
const valid = Number.isInteger(battery) && battery >= 0 && battery <= 100;
|
||||
this.setWatchBattery(valid ? battery : null, valid ? message.payload.charging === true : null);
|
||||
return;
|
||||
}
|
||||
if (!watchRemoteEnabledSetting.get()) {
|
||||
ack(false, "Watch control is turned off in Faceclaw's Settings > Watch.");
|
||||
return;
|
||||
|
||||
+62
-23
@@ -1,20 +1,36 @@
|
||||
import { GrayImage, imageFromAsciiArt } from "./image";
|
||||
|
||||
const EMPTY_BATTERY_ICON = imageFromAsciiArt(
|
||||
[
|
||||
" ################## ",
|
||||
" #................# ",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................# ",
|
||||
" ################## ",
|
||||
],
|
||||
120,
|
||||
);
|
||||
/**
|
||||
* Top-bar battery gauge: a 10px-tall outlined body with a nub on the right,
|
||||
* filled left-to-right in proportion to the charge. The fill is drawn as
|
||||
* BATTERY_BAR_COUNT bars of BATTERY_BAR_WIDTH px separated by dimmed
|
||||
* BATTERY_BAR_GAP-px columns; the body outline is sized from those
|
||||
* constants so they can be tuned freely. The fill level is continuous (not
|
||||
* quantized to whole bars): the rightmost lit bar is cut to a partial width.
|
||||
*/
|
||||
export const BATTERY_BAR_COUNT = 4;
|
||||
export const BATTERY_BAR_WIDTH = 3;
|
||||
export const BATTERY_BAR_GAP = 1;
|
||||
|
||||
const OUTLINE_VALUE = 120;
|
||||
const BAR_VALUE = 190;
|
||||
/** The separator columns inside the lit portion of the fill. */
|
||||
const BAR_GAP_VALUE = 70;
|
||||
|
||||
const ICON_HEIGHT = 10;
|
||||
/** Outline stroke plus one clear pixel between it and the fill, per side. */
|
||||
const BODY_INSET = 2;
|
||||
const NUB_WIDTH = 2;
|
||||
const NUB_TOP = 2;
|
||||
const NUB_HEIGHT = 6;
|
||||
|
||||
const FILL_X = BODY_INSET;
|
||||
const FILL_Y = BODY_INSET;
|
||||
const FILL_WIDTH = BATTERY_BAR_COUNT * BATTERY_BAR_WIDTH + (BATTERY_BAR_COUNT - 1) * BATTERY_BAR_GAP;
|
||||
const FILL_HEIGHT = ICON_HEIGHT - 2 * BODY_INSET;
|
||||
const BODY_WIDTH = FILL_WIDTH + 2 * BODY_INSET;
|
||||
|
||||
export const BATTERY_ICON_WIDTH = BODY_WIDTH + NUB_WIDTH;
|
||||
|
||||
const BATTERY_BOLT_ICON = imageFromAsciiArt(
|
||||
[
|
||||
@@ -29,25 +45,48 @@ const BATTERY_BOLT_ICON = imageFromAsciiArt(
|
||||
255,
|
||||
);
|
||||
|
||||
export const BATTERY_ICON_WIDTH = EMPTY_BATTERY_ICON.width;
|
||||
/** The empty outline, built once from the geometry constants. */
|
||||
const EMPTY_BATTERY_ICON = buildEmptyBattery();
|
||||
|
||||
const BATTERY_FILL_X = 3;
|
||||
const BATTERY_FILL_Y = 2;
|
||||
const BATTERY_FILL_WIDTH = 14;
|
||||
const BATTERY_FILL_HEIGHT = 6;
|
||||
function buildEmptyBattery(): GrayImage {
|
||||
const icon = new GrayImage(BATTERY_ICON_WIDTH, ICON_HEIGHT, 0);
|
||||
icon.fillRect(0, 0, BODY_WIDTH, 1, OUTLINE_VALUE);
|
||||
icon.fillRect(0, ICON_HEIGHT - 1, BODY_WIDTH, 1, OUTLINE_VALUE);
|
||||
icon.fillRect(0, 0, 1, ICON_HEIGHT, OUTLINE_VALUE);
|
||||
icon.fillRect(BODY_WIDTH - 1, 0, 1, ICON_HEIGHT, OUTLINE_VALUE);
|
||||
icon.fillRect(BODY_WIDTH, NUB_TOP, NUB_WIDTH, NUB_HEIGHT, OUTLINE_VALUE);
|
||||
return icon;
|
||||
}
|
||||
|
||||
export function drawBattery(percentCharge: number, isCharging: boolean): GrayImage {
|
||||
const icon = new GrayImage(EMPTY_BATTERY_ICON.width, EMPTY_BATTERY_ICON.height, 0);
|
||||
icon.bitBlt(EMPTY_BATTERY_ICON, 0, 0);
|
||||
const clamped = Math.max(0, Math.min(100, percentCharge));
|
||||
const fillWidth = Math.round((BATTERY_FILL_WIDTH * clamped) / 100);
|
||||
icon.fillRect(BATTERY_FILL_X, BATTERY_FILL_Y, fillWidth, BATTERY_FILL_HEIGHT, 190);
|
||||
const litWidth = Math.round((FILL_WIDTH * clamped) / 100);
|
||||
drawSegmentedFill(icon, litWidth);
|
||||
if (isCharging) {
|
||||
overlayImage(icon, BATTERY_BOLT_ICON, 7, 1, clamped > 50 ? 0 : 255);
|
||||
const boltX = FILL_X + (((FILL_WIDTH - BATTERY_BOLT_ICON.width) / 2) | 0);
|
||||
overlayImage(icon, BATTERY_BOLT_ICON, boltX, 1, clamped > 50 ? 0 : 255);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Light the leftmost `litWidth` columns of the fill area as bars with dim
|
||||
* separator columns. Columns past the lit width stay clear, so a partial
|
||||
* charge ends mid-bar rather than snapping to a bar boundary. A separator is
|
||||
* only drawn between two lit bars: when the lit width ends exactly on one it
|
||||
* is left clear rather than dangling past the last bar.
|
||||
*/
|
||||
function drawSegmentedFill(icon: GrayImage, litWidth: number): void {
|
||||
const pitch = BATTERY_BAR_WIDTH + BATTERY_BAR_GAP;
|
||||
for (let column = 0; column < litWidth; column++) {
|
||||
const isBar = column % pitch < BATTERY_BAR_WIDTH;
|
||||
if (!isBar && column + BATTERY_BAR_GAP >= litWidth) continue;
|
||||
icon.fillRect(FILL_X + column, FILL_Y, 1, FILL_HEIGHT, isBar ? BAR_VALUE : BAR_GAP_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
function overlayImage(target: GrayImage, source: GrayImage, dx: number, dy: number, value: number): void {
|
||||
for (let y = 0; y < source.height; y++) {
|
||||
for (let x = 0; x < source.width; x++) {
|
||||
|
||||
@@ -16,6 +16,10 @@ const ICON_STROKE_WIDTH = 2;
|
||||
// Lucide icons (MIT/ISC licensed). Kept verbatim so they can be diffed
|
||||
// against upstream if an icon needs updating.
|
||||
export const ICON_SVGS = {
|
||||
"message-circle":
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M21 11.5a8.4 8.4 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.4 8.4 0 0 1-3.8-.9L3 21l1.9-5.7a8.4 8.4 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.4 8.4 0 0 1 3.8-.9h.5a8.5 8.5 0 0 1 8 8z"/></svg>',
|
||||
eye:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0"/><circle cx="12" cy="12" r="3"/></svg>',
|
||||
"layout-grid":
|
||||
'<svg viewBox="0 0 24 24" fill="none"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></svg>',
|
||||
timer:
|
||||
@@ -30,6 +34,8 @@ export const ICON_SVGS = {
|
||||
// Not a Lucide icon: a ball above two angled flippers, for Pinball.
|
||||
pinball:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><circle cx="12" cy="6" r="3"/><path d="M4 14l7 5"/><path d="M20 14l-7 5"/><circle cx="4" cy="14" r="1"/><circle cx="20" cy="14" r="1"/></svg>',
|
||||
bird:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M16 7h.01"/><path d="M3.4 18H12a8 8 0 0 0 8-8V7a4 4 0 0 0-7.28-2.3L2 20"/><path d="m20 7 2 .5-2 .5"/><path d="M10 18v3"/><path d="M14 17.75V21"/><path d="M7 18a6 6 0 0 0 3.84-10.61"/></svg>',
|
||||
spade:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 18v4"/><path d="M2 14.499a5.5 5.5 0 0 0 9.591 3.675.6.6 0 0 1 .818.001A5.5 5.5 0 0 0 22 14.5c0-2.29-1.5-4-3-5.5l-5.492-5.312a2 2 0 0 0-3-.02L5 8.999c-1.5 1.5-3 3.2-3 5.5"/></svg>',
|
||||
terminal:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Ambient light sensor access through the custom firmware (CFW image-handler
|
||||
* mode 16, capability token "als16"; see g2flash/patches/als_sensor.c).
|
||||
* mode 16; see g2flash/patches/als_sensor.c).
|
||||
*
|
||||
* The stock firmware only reads its OPT3001 light sensor while auto-brightness
|
||||
* is on, and then steps the panel brightness itself (visible flicker). The CFW
|
||||
|
||||
+172
-95
@@ -4,50 +4,107 @@ declare const com: any;
|
||||
declare const java: any;
|
||||
|
||||
/**
|
||||
* Download management for the on-device transcription model (Moonshine, via
|
||||
* sherpa-onnx). The model is no longer bundled in the APK; it is fetched on
|
||||
* demand into the same filesDir location that earlier releases copied the
|
||||
* bundled files to, so upgraded installs that already used on-device
|
||||
* transcription need no download. FaceclawVoiceController reads the files
|
||||
* from this directory.
|
||||
* Download management for the on-device transcription models (sherpa-onnx
|
||||
* offline recognizers). Models are fetched on demand into filesDir rather
|
||||
* than bundled in the APK; FaceclawVoiceController reads the files from the
|
||||
* directory each model's `dirName` names.
|
||||
*
|
||||
* Mirrors the on-phone assistant model flow in llama.ts, except the model is
|
||||
* three files rather than one; they download sequentially through
|
||||
* Two models, same shape:
|
||||
* - "moonshine": the original on-device option (three files). The model is
|
||||
* no longer bundled in the APK; it is fetched into the same filesDir
|
||||
* location that earlier releases copied the bundled files to, so
|
||||
* upgraded installs that already used on-device transcription need no
|
||||
* download.
|
||||
* - "whisper-base-en": sherpa-onnx's offline Whisper backend, base.en,
|
||||
* int8-quantized. Picked over tiny.en for materially better accuracy
|
||||
* (Whisper's own tiny/base WER gap is real and well documented) on the
|
||||
* reasoning that FaceclawVoiceController.java no longer re-decodes a
|
||||
* Whisper segment on every live-partial tick — see its ASR_WHISPER_*
|
||||
* comment — so base.en's extra compute is a one-time cost per utterance,
|
||||
* not a cost paid ~1.4x/second while the user is speaking. tiny.en is an
|
||||
* easy swap (this file's numbers only) if base.en proves too slow on a
|
||||
* real phone; nobody has measured that yet. Files are csukuangfj's own
|
||||
* (the sherpa-onnx maintainer's) HF mirror of the upstream sherpa-onnx
|
||||
* release asset, matching the mirror-not-the-GH-tarball approach already
|
||||
* used for Moonshine below. Every hash here was computed locally from a
|
||||
* freshly downloaded copy of the file, not copied from a doc page.
|
||||
*
|
||||
* Mirrors the on-phone assistant model flow in llama.ts, except each model
|
||||
* here is multiple files rather than one; they download sequentially through
|
||||
* FaceclawModelDownloader (resume + pinned sha256 per file).
|
||||
*/
|
||||
|
||||
export type AsrModelId = "moonshine" | "whisper-base-en";
|
||||
|
||||
type AsrModelFile = {
|
||||
name: string;
|
||||
sha256: string;
|
||||
sizeBytes: number;
|
||||
};
|
||||
|
||||
export const ASR_MODEL = {
|
||||
label: "Moonshine (English)",
|
||||
dirName: "sherpa-onnx-moonshine-base-en-quantized-2026-02-27",
|
||||
// Hugging Face mirror of the sherpa-onnx release asset of the same name.
|
||||
// The GitHub release only offers a tar.bz2, which the phone can't unpack;
|
||||
// this mirror serves the same files (sha256-verified) individually.
|
||||
baseUrl:
|
||||
"https://huggingface.co/csukuangfj2/sherpa-onnx-moonshine-base-en-quantized-2026-02-27/resolve/main/",
|
||||
files: [
|
||||
{
|
||||
name: "decoder_model_merged.ort",
|
||||
sha256: "d9d7b333af34bc552580576ddcf248a1c6c839e0d3b43b09afb9376ed009899d",
|
||||
sizeBytes: 109424400,
|
||||
},
|
||||
{
|
||||
name: "encoder_model.ort",
|
||||
sha256: "7c66495948d0d08ec1af454cd4b5514862ae6511e94712a60e6d83eaec8dc8cf",
|
||||
sizeBytes: 31326816,
|
||||
},
|
||||
{
|
||||
name: "tokens.txt",
|
||||
sha256: "2870d843e14c1e187bf1913a521562a63b53933814bd7f2145120468f494a049",
|
||||
sizeBytes: 549350,
|
||||
},
|
||||
] as AsrModelFile[],
|
||||
totalBytes: 141300566,
|
||||
type AsrModelDef = {
|
||||
label: string;
|
||||
dirName: string;
|
||||
baseUrl: string;
|
||||
files: AsrModelFile[];
|
||||
totalBytes: number;
|
||||
};
|
||||
|
||||
export const ASR_MODELS: Record<AsrModelId, AsrModelDef> = {
|
||||
moonshine: {
|
||||
label: "Moonshine (English)",
|
||||
dirName: "sherpa-onnx-moonshine-base-en-quantized-2026-02-27",
|
||||
// Hugging Face mirror of the sherpa-onnx release asset of the same name.
|
||||
// The GitHub release only offers a tar.bz2, which the phone can't unpack;
|
||||
// this mirror serves the same files (sha256-verified) individually.
|
||||
baseUrl:
|
||||
"https://huggingface.co/csukuangfj2/sherpa-onnx-moonshine-base-en-quantized-2026-02-27/resolve/main/",
|
||||
files: [
|
||||
{
|
||||
name: "decoder_model_merged.ort",
|
||||
sha256: "d9d7b333af34bc552580576ddcf248a1c6c839e0d3b43b09afb9376ed009899d",
|
||||
sizeBytes: 109424400,
|
||||
},
|
||||
{
|
||||
name: "encoder_model.ort",
|
||||
sha256: "7c66495948d0d08ec1af454cd4b5514862ae6511e94712a60e6d83eaec8dc8cf",
|
||||
sizeBytes: 31326816,
|
||||
},
|
||||
{
|
||||
name: "tokens.txt",
|
||||
sha256: "2870d843e14c1e187bf1913a521562a63b53933814bd7f2145120468f494a049",
|
||||
sizeBytes: 549350,
|
||||
},
|
||||
],
|
||||
totalBytes: 141300566,
|
||||
},
|
||||
"whisper-base-en": {
|
||||
label: "Whisper (English, base)",
|
||||
dirName: "sherpa-onnx-whisper-base-en-int8",
|
||||
// csukuangfj/sherpa-onnx-whisper-base.en on Hugging Face: the sherpa-onnx
|
||||
// maintainer's own mirror of the project's whisper export, individual
|
||||
// files (no tar.bz2-unpack problem to begin with, but kept as the same
|
||||
// per-file-with-pinned-hash shape as Moonshine above for consistency).
|
||||
baseUrl: "https://huggingface.co/csukuangfj/sherpa-onnx-whisper-base.en/resolve/main/",
|
||||
files: [
|
||||
{
|
||||
name: "base.en-encoder.int8.onnx",
|
||||
sha256: "ef6b936f4c9b1d90a3b68634b60c4ed8576b26172b33c2535ec0e933c9edb823",
|
||||
sizeBytes: 29120534,
|
||||
},
|
||||
{
|
||||
name: "base.en-decoder.int8.onnx",
|
||||
sha256: "f7162ad6db2dbef16cfaeaa7f945b9d7dd9c1b8d472f6aca82f2273d185e4d41",
|
||||
sizeBytes: 130669978,
|
||||
},
|
||||
{
|
||||
name: "base.en-tokens.txt",
|
||||
sha256: "306cd27f03c1a714eca7108e03d66b7dc042abe8c258b44c199a7ed9838dd930",
|
||||
sizeBytes: 835554,
|
||||
},
|
||||
],
|
||||
totalBytes: 160626066,
|
||||
},
|
||||
};
|
||||
|
||||
export type AsrModelState = {
|
||||
@@ -56,121 +113,141 @@ export type AsrModelState = {
|
||||
totalBytes: number;
|
||||
};
|
||||
|
||||
let downloader: any = null;
|
||||
// Bytes of files already fully downloaded in this run, plus progress within
|
||||
// the file currently downloading; drives the aggregate percentage.
|
||||
let completedBytes = 0;
|
||||
let currentFileBytes = 0;
|
||||
const stateListeners = new Set<(state: AsrModelState) => void>();
|
||||
type ModelRuntime = {
|
||||
downloader: any;
|
||||
// Bytes of files already fully downloaded in this run, plus progress within
|
||||
// the file currently downloading; drives the aggregate percentage.
|
||||
completedBytes: number;
|
||||
currentFileBytes: number;
|
||||
stateListeners: Set<(state: AsrModelState) => void>;
|
||||
};
|
||||
|
||||
function modelDirPath(): string {
|
||||
function freshRuntime(): ModelRuntime {
|
||||
return { downloader: null, completedBytes: 0, currentFileBytes: 0, stateListeners: new Set() };
|
||||
}
|
||||
|
||||
const runtimes: Record<AsrModelId, ModelRuntime> = {
|
||||
moonshine: freshRuntime(),
|
||||
"whisper-base-en": freshRuntime(),
|
||||
};
|
||||
|
||||
function modelDirPath(id: AsrModelId): string {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
return `${context.getFilesDir().getAbsolutePath()}/faceclaw-voice-asr/${ASR_MODEL.dirName}`;
|
||||
return `${context.getFilesDir().getAbsolutePath()}/faceclaw-voice-asr/${ASR_MODELS[id].dirName}`;
|
||||
}
|
||||
|
||||
function filePath(file: AsrModelFile): string {
|
||||
return `${modelDirPath()}/${file.name}`;
|
||||
function filePath(id: AsrModelId, file: AsrModelFile): string {
|
||||
return `${modelDirPath(id)}/${file.name}`;
|
||||
}
|
||||
|
||||
function isFilePresent(file: AsrModelFile): boolean {
|
||||
function isFilePresent(id: AsrModelId, file: AsrModelFile): boolean {
|
||||
try {
|
||||
const javaFile = new java.io.File(filePath(file));
|
||||
const javaFile = new java.io.File(filePath(id, file));
|
||||
return javaFile.exists() && javaFile.length() > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isAsrModelReady(): boolean {
|
||||
export function isAsrModelReady(id: AsrModelId): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
return ASR_MODEL.files.every(isFilePresent);
|
||||
return ASR_MODELS[id].files.every((file) => isFilePresent(id, file));
|
||||
}
|
||||
|
||||
export function asrModelState(): AsrModelState {
|
||||
if (downloader) {
|
||||
export function asrModelState(id: AsrModelId): AsrModelState {
|
||||
const runtime = runtimes[id];
|
||||
const totalBytes = ASR_MODELS[id].totalBytes;
|
||||
if (runtime.downloader) {
|
||||
return {
|
||||
status: "downloading",
|
||||
bytesDownloaded: completedBytes + currentFileBytes,
|
||||
totalBytes: ASR_MODEL.totalBytes,
|
||||
bytesDownloaded: runtime.completedBytes + runtime.currentFileBytes,
|
||||
totalBytes,
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: isAsrModelReady() ? "ready" : "absent",
|
||||
status: isAsrModelReady(id) ? "ready" : "absent",
|
||||
bytesDownloaded: 0,
|
||||
totalBytes: ASR_MODEL.totalBytes,
|
||||
totalBytes,
|
||||
};
|
||||
}
|
||||
|
||||
export function onAsrModelStateChanged(listener: (state: AsrModelState) => void): () => void {
|
||||
stateListeners.add(listener);
|
||||
return () => stateListeners.delete(listener);
|
||||
export function onAsrModelStateChanged(id: AsrModelId, listener: (state: AsrModelState) => void): () => void {
|
||||
const runtime = runtimes[id];
|
||||
runtime.stateListeners.add(listener);
|
||||
return () => runtime.stateListeners.delete(listener);
|
||||
}
|
||||
|
||||
function notifyStateChanged(): void {
|
||||
const state = asrModelState();
|
||||
stateListeners.forEach((listener) => listener(state));
|
||||
function notifyStateChanged(id: AsrModelId): void {
|
||||
const state = asrModelState(id);
|
||||
runtimes[id].stateListeners.forEach((listener) => listener(state));
|
||||
}
|
||||
|
||||
export function startAsrModelDownload(): void {
|
||||
if (!global.isAndroid || downloader || isAsrModelReady()) return;
|
||||
completedBytes = ASR_MODEL.files.filter(isFilePresent).reduce((sum, f) => sum + f.sizeBytes, 0);
|
||||
downloadNextFile();
|
||||
notifyStateChanged();
|
||||
export function startAsrModelDownload(id: AsrModelId): void {
|
||||
const runtime = runtimes[id];
|
||||
if (!global.isAndroid || runtime.downloader || isAsrModelReady(id)) return;
|
||||
runtime.completedBytes = ASR_MODELS[id].files
|
||||
.filter((file) => isFilePresent(id, file))
|
||||
.reduce((sum, f) => sum + f.sizeBytes, 0);
|
||||
downloadNextFile(id);
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
|
||||
function downloadNextFile(): void {
|
||||
const nextFile = ASR_MODEL.files.find((file) => !isFilePresent(file));
|
||||
function downloadNextFile(id: AsrModelId): void {
|
||||
const runtime = runtimes[id];
|
||||
const def = ASR_MODELS[id];
|
||||
const nextFile = def.files.find((file) => !isFilePresent(id, file));
|
||||
if (!nextFile) {
|
||||
downloader = null;
|
||||
notifyStateChanged();
|
||||
runtime.downloader = null;
|
||||
notifyStateChanged(id);
|
||||
return;
|
||||
}
|
||||
currentFileBytes = 0;
|
||||
runtime.currentFileBytes = 0;
|
||||
const listener = new com.faceclaw.app.FaceclawModelDownloaderListener({
|
||||
onProgress: (bytes: number, _total: number) => {
|
||||
currentFileBytes = Number(bytes);
|
||||
notifyStateChanged();
|
||||
runtime.currentFileBytes = Number(bytes);
|
||||
notifyStateChanged(id);
|
||||
},
|
||||
onDone: () => {
|
||||
completedBytes += nextFile.sizeBytes;
|
||||
currentFileBytes = 0;
|
||||
downloadNextFile();
|
||||
runtime.completedBytes += nextFile.sizeBytes;
|
||||
runtime.currentFileBytes = 0;
|
||||
downloadNextFile(id);
|
||||
},
|
||||
onError: (message: string) => {
|
||||
console.error(`Voice model download failed (${nextFile.name}): ${message}`);
|
||||
downloader = null;
|
||||
notifyStateChanged();
|
||||
console.error(`Voice model download failed (${id}/${nextFile.name}): ${message}`);
|
||||
runtime.downloader = null;
|
||||
notifyStateChanged(id);
|
||||
},
|
||||
});
|
||||
downloader = new com.faceclaw.app.FaceclawModelDownloader(
|
||||
`${ASR_MODEL.baseUrl}${nextFile.name}`,
|
||||
filePath(nextFile),
|
||||
runtime.downloader = new com.faceclaw.app.FaceclawModelDownloader(
|
||||
`${def.baseUrl}${nextFile.name}`,
|
||||
filePath(id, nextFile),
|
||||
nextFile.sha256,
|
||||
nextFile.sizeBytes,
|
||||
listener,
|
||||
);
|
||||
downloader.start();
|
||||
runtime.downloader.start();
|
||||
}
|
||||
|
||||
/** Stops the download; already-fetched bytes are kept and resumed next time. */
|
||||
export function cancelAsrModelDownload(): void {
|
||||
if (!downloader) return;
|
||||
downloader.cancel();
|
||||
downloader = null;
|
||||
notifyStateChanged();
|
||||
export function cancelAsrModelDownload(id: AsrModelId): void {
|
||||
const runtime = runtimes[id];
|
||||
if (!runtime.downloader) return;
|
||||
runtime.downloader.cancel();
|
||||
runtime.downloader = null;
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
|
||||
export function deleteAsrModel(): void {
|
||||
export function deleteAsrModel(id: AsrModelId): void {
|
||||
if (!global.isAndroid) return;
|
||||
cancelAsrModelDownload();
|
||||
cancelAsrModelDownload(id);
|
||||
try {
|
||||
for (const file of ASR_MODEL.files) {
|
||||
new java.io.File(filePath(file)).delete();
|
||||
new java.io.File(`${filePath(file)}.part`).delete();
|
||||
for (const file of ASR_MODELS[id].files) {
|
||||
new java.io.File(filePath(id, file)).delete();
|
||||
new java.io.File(`${filePath(id, file)}.part`).delete();
|
||||
}
|
||||
new java.io.File(modelDirPath()).delete();
|
||||
new java.io.File(modelDirPath(id)).delete();
|
||||
} catch (error) {
|
||||
console.error(`Voice model delete failed: ${String(error)}`);
|
||||
console.error(`Voice model delete failed (${id}): ${String(error)}`);
|
||||
}
|
||||
notifyStateChanged();
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* BLE bandwidth benchmark controls (Developer app). The Java communicator
|
||||
* streams no-op image payloads for a fixed duration with a selectable message
|
||||
* size and pipeline window, then reports throughput. Progress and results are
|
||||
* polled with getBandwidthBenchmarkStatus.
|
||||
*
|
||||
* Talks to the active communicator via the FaceclawBleCommunicator.getActive()
|
||||
* static, so it works from any isolate without threading the bridge through.
|
||||
*/
|
||||
|
||||
export type BandwidthBenchmarkStatus = {
|
||||
state: "idle" | "starting" | "running" | "done";
|
||||
messageSize: number;
|
||||
windowSize: number;
|
||||
linkMode: number;
|
||||
elapsedMs: number;
|
||||
messagesSent: number;
|
||||
messagesAcked: number;
|
||||
timeouts: number;
|
||||
payloadBytesAcked: number;
|
||||
wireBytesAcked: number;
|
||||
aborted: boolean;
|
||||
};
|
||||
|
||||
function activeCommunicator(): any {
|
||||
if (!global.isAndroid) return null;
|
||||
try {
|
||||
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Start a run. False if not connected or a run is already active. */
|
||||
export function startBandwidthBenchmark(messageSize: number, windowSize: number, durationMs: number, linkMode = 0): boolean {
|
||||
const active = activeCommunicator();
|
||||
if (!active) return false;
|
||||
try {
|
||||
return Boolean(
|
||||
active.startBandwidthBenchmarkWithLinkMode(
|
||||
Math.round(messageSize), Math.round(windowSize), Math.round(durationMs), Math.round(linkMode),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
console.warn(`startBandwidthBenchmark failed: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Cancel an in-progress run. Safe to call when idle. */
|
||||
export function cancelBandwidthBenchmark(): void {
|
||||
try {
|
||||
activeCommunicator()?.cancelBandwidthBenchmark();
|
||||
} catch (error) {
|
||||
console.warn(`cancelBandwidthBenchmark failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Status of the current or most recent run; null when not connected. */
|
||||
export function getBandwidthBenchmarkStatus(): BandwidthBenchmarkStatus | null {
|
||||
const active = activeCommunicator();
|
||||
if (!active) return null;
|
||||
try {
|
||||
return JSON.parse(String(active.getBandwidthBenchmarkStatus())) as BandwidthBenchmarkStatus;
|
||||
} catch (error) {
|
||||
console.warn(`getBandwidthBenchmarkStatus failed: ${error}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,17 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
import { type FirmwareInfo } from "../g2/firmware-compat";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
export type DeviceInfo = {
|
||||
leftVersion: string;
|
||||
rightVersion: string;
|
||||
capabilities: string;
|
||||
};
|
||||
export type DeviceInfo = FirmwareInfo;
|
||||
|
||||
export type DeviceInfoState = "connecting" | "authenticating" | "querying";
|
||||
|
||||
/**
|
||||
* TS wrapper around the native FaceclawDeviceInfoProbe — a one-shot,
|
||||
* stock-compatible connect + firmware/device-info read. `run()` resolves with
|
||||
* the firmware versions and CFW capability string, or rejects on failure.
|
||||
* the firmware versions and the firmware-extension string, or rejects on failure.
|
||||
*/
|
||||
export class DeviceInfoProbe {
|
||||
private readonly probe: any;
|
||||
@@ -34,11 +32,11 @@ export class DeviceInfoProbe {
|
||||
onLog: (line: string) => this.emit(this.logListeners, String(line)),
|
||||
onState: (state: string, detail: string) =>
|
||||
this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? "")),
|
||||
onResult: (leftVersion: string, rightVersion: string, capabilities: string) =>
|
||||
onResult: (leftVersion: string, rightVersion: string, extension: string) =>
|
||||
this.settle(null, {
|
||||
leftVersion: String(leftVersion),
|
||||
rightVersion: String(rightVersion),
|
||||
capabilities: String(capabilities),
|
||||
extension: String(extension),
|
||||
}),
|
||||
onError: (message: string) => this.settle(new Error(String(message)), null),
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { Dialogs, Utils } from "@nativescript/core";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
@@ -23,11 +23,19 @@ export function readEvenAppNotificationState(): EvenAppNotificationState {
|
||||
}
|
||||
|
||||
export function openEvenAppSettings(): void {
|
||||
if (!global.isAndroid) return;
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (context) {
|
||||
com.faceclaw.app.FaceclawEvenAppDetector.openEvenAppSettings(context);
|
||||
}
|
||||
const context = global.isAndroid ? Utils.android.getApplicationContext() : null;
|
||||
const result = context
|
||||
? com.faceclaw.app.FaceclawEvenAppDetector.openEvenAppSettings(context)
|
||||
: "failed";
|
||||
if (result === "opened") return;
|
||||
const notInstalled = result === "not-installed";
|
||||
void Dialogs.alert({
|
||||
title: notInstalled ? "Even app not installed" : "Unable to open Even app settings",
|
||||
message: notInstalled
|
||||
? "The official Even Realities app isn't installed on this phone. If your glasses are connected to another app or phone, disconnect them there. Otherwise, you can continue."
|
||||
: "Open your phone's settings manually to manage the Even app, or disconnect your glasses from within the app that's connected to them.",
|
||||
okButtonText: "OK",
|
||||
});
|
||||
}
|
||||
|
||||
export function openNotificationAccessSettings(): void {
|
||||
|
||||
@@ -22,6 +22,8 @@ export type CommunicatorState = {
|
||||
export type HeadsetBatteryState = {
|
||||
battery: number;
|
||||
chargingStatus: number;
|
||||
ringBattery?: number;
|
||||
ringChargingStatus?: number;
|
||||
};
|
||||
|
||||
export type FrameMetrics = {
|
||||
@@ -30,11 +32,9 @@ export type FrameMetrics = {
|
||||
tileCount: number;
|
||||
};
|
||||
|
||||
export type FirmwareInfo = {
|
||||
leftVersion: string;
|
||||
rightVersion: string;
|
||||
capabilities: string;
|
||||
};
|
||||
import { type FirmwareInfo } from "../g2/firmware-compat";
|
||||
|
||||
export type { FirmwareInfo };
|
||||
|
||||
/**
|
||||
* Compositor surface configuration. Position/size are in screen pixels;
|
||||
@@ -189,10 +189,12 @@ export class FaceclawCommunicatorBridge {
|
||||
frameTimings.logFrame(event.frameId, "input event received on JS side");
|
||||
this.emitAsync(this.ringListeners, event);
|
||||
},
|
||||
onBatteryState: (headsetBattery: number, headsetCharging: number) => {
|
||||
onBatteryState: (headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number) => {
|
||||
const state = {
|
||||
battery: Number(headsetBattery),
|
||||
chargingStatus: Number(headsetCharging),
|
||||
ringBattery: Number(ringBattery),
|
||||
ringChargingStatus: Number(ringCharging),
|
||||
};
|
||||
this.emitAsync(this.batteryListeners, state);
|
||||
},
|
||||
@@ -226,11 +228,11 @@ export class FaceclawCommunicatorBridge {
|
||||
onFrameFinished: (frameId: number, outcome: string) => {
|
||||
this.recordFrameFinished(Number(frameId), String(outcome));
|
||||
},
|
||||
onFirmwareInfo: (leftVersion: string, rightVersion: string, capabilities: string) => {
|
||||
const info = {
|
||||
onFirmwareInfo: (leftVersion: string, rightVersion: string, extension: string) => {
|
||||
const info: FirmwareInfo = {
|
||||
leftVersion: String(leftVersion),
|
||||
rightVersion: String(rightVersion),
|
||||
capabilities: String(capabilities),
|
||||
extension: String(extension),
|
||||
};
|
||||
this.emitAsync(this.firmwareInfoListeners, info);
|
||||
},
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Open a URL in the phone's browser. Uses the application context with
|
||||
* NEW_TASK so it works from worker threads (no Activity in hand). Returns
|
||||
* false when no browser is available or on a non-Android platform.
|
||||
*/
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const android: any;
|
||||
declare const global: any;
|
||||
|
||||
export function openUrlOnPhone(url: string): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
try {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return false;
|
||||
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url));
|
||||
intent.addCategory(android.content.Intent.CATEGORY_BROWSABLE);
|
||||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn(`open-url: could not open ${url}: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -23,14 +23,14 @@ export class IosVoiceControlBridge {
|
||||
private log: (message: string) => void = () => {}
|
||||
|
||||
onStatus(listener: (state: VoiceControlState) => void): () => void {
|
||||
this.statuses.add(listener); listener({ status: this.status }); return () => { this.statuses.delete(listener) }
|
||||
this.statuses.add(listener); listener({ status: this.status, listening: this.capturing, detail: '' }); return () => { this.statuses.delete(listener) }
|
||||
}
|
||||
onTranscript(listener: (event: VoiceTranscriptEvent) => void): () => void {
|
||||
this.transcripts.add(listener); return () => { this.transcripts.delete(listener) }
|
||||
}
|
||||
onSpeechEnd(listener: () => void): () => void { this.ends.add(listener); return () => { this.ends.delete(listener) } }
|
||||
onSpeechPause(_listener: () => void): () => void { return () => {} }
|
||||
private setStatus(status: string): void { this.status = status; for (const fn of [...this.statuses]) fn({ status }) }
|
||||
private setStatus(status: string): void { this.status = status; for (const fn of [...this.statuses]) fn({ status, listening: this.capturing, detail: '' }) }
|
||||
private ensureNative(): any {
|
||||
if (!this.native) {
|
||||
this.native = FaceclawSpeech.new()
|
||||
|
||||
@@ -11,10 +11,27 @@ import { toUint8Array } from "../util/array-util";
|
||||
declare const com: any;
|
||||
|
||||
export type VoiceControlState = {
|
||||
/** One-line state of the capture: progress, "Listening...", or an error. */
|
||||
status: string;
|
||||
/**
|
||||
* Whether audio is actually being captured and transcribed right now. False
|
||||
* while the mic is starting, while the phone is showing the permission
|
||||
* prompt, and after an error, so a dialog must not claim to be listening.
|
||||
*/
|
||||
listening: boolean;
|
||||
/**
|
||||
* Longer guidance to show in place of the (still empty) transcript, e.g.
|
||||
* what the user has to do on the phone. Empty when there is nothing to say.
|
||||
*/
|
||||
detail: string;
|
||||
};
|
||||
|
||||
export type VoiceProviderKind = "onboard" | "elevenlabs" | "whisper" | "soniox";
|
||||
// "whisper" (no "onboard-" prefix) is the OpenAI CLOUD provider (gpt-realtime-whisper,
|
||||
// see openai-stt.ts) -- an unfortunate pre-existing name collision with the model
|
||||
// family, not with "onboard-whisper" below, which is the on-device sherpa-onnx
|
||||
// Whisper backend added alongside "onboard" (Moonshine). Kept as-is rather than
|
||||
// renamed, since it's a persisted settings value on real installs already.
|
||||
export type VoiceProviderKind = "onboard" | "onboard-whisper" | "elevenlabs" | "whisper" | "soniox";
|
||||
|
||||
export type VoiceTranscriptEvent = CloudSttTranscriptEvent & {
|
||||
/**
|
||||
@@ -88,7 +105,12 @@ export class FaceclawVoiceControlBridge {
|
||||
private controller: any | null = null;
|
||||
private listenerProxy: any | null = null;
|
||||
private status = "Voice control stopped.";
|
||||
private listening = false;
|
||||
private detail = "";
|
||||
private started = false;
|
||||
private nativeCaptureFinished: Promise<void> = Promise.resolve();
|
||||
private nextNativeCaptureId = 1;
|
||||
private readonly nativeCaptureResolvers = new Map<number, () => void>();
|
||||
// The mic is a single shared stream; these are the reasons it is running.
|
||||
// The first holder starts capture (choosing the provider); the mic stops
|
||||
// when the last one releases. Transcript events broadcast to every
|
||||
@@ -118,7 +140,7 @@ export class FaceclawVoiceControlBridge {
|
||||
|
||||
onStatus(listener: (state: VoiceControlState) => void): () => void {
|
||||
this.statusListeners.add(listener);
|
||||
listener({ status: this.status });
|
||||
listener(this.state());
|
||||
return () => this.statusListeners.delete(listener);
|
||||
}
|
||||
|
||||
@@ -142,14 +164,41 @@ export class FaceclawVoiceControlBridge {
|
||||
return () => this.speechPauseListeners.delete(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* The capture is blocked on the phone's system microphone-permission
|
||||
* prompt. Reported by the controller before it asks Android, so the dialog
|
||||
* on the glasses can send the user to the phone instead of claiming to
|
||||
* listen; the next status (from the capture start, or reportPermissionDenied)
|
||||
* replaces it.
|
||||
*/
|
||||
reportPermissionPrompt(): void {
|
||||
this.setStatus(
|
||||
"Waiting for microphone permission...",
|
||||
"Allow microphone access in the prompt on your phone.",
|
||||
);
|
||||
}
|
||||
|
||||
/** The user declined the microphone permission (or the prompt could not be shown). */
|
||||
reportPermissionDenied(): void {
|
||||
this.setStatus(
|
||||
"Microphone access denied.",
|
||||
"Enable the microphone for Faceclaw in the phone's app settings, then try again.",
|
||||
);
|
||||
}
|
||||
|
||||
/** Begin push-to-talk capture (momentary; released with stopPushToTalk). */
|
||||
startPushToTalk(options: PushToTalkOptions): void {
|
||||
this.acquireCapture("ptt", options);
|
||||
}
|
||||
|
||||
/** End push-to-talk: for cloud, commit for a final result if it was the last holder. */
|
||||
stopPushToTalk(): void {
|
||||
stopPushToTalk(): Promise<void> | void {
|
||||
// Shared continuous capture keeps running; cloud providers finalize via
|
||||
// their own callbacks. Only a stopped native recognizer must be awaited.
|
||||
const finished = !this.cloudClient && this.captureHolders.has("ptt") && this.captureHolders.size === 1
|
||||
? this.nativeCaptureFinished : undefined;
|
||||
this.releaseCapture("ptt", true);
|
||||
return finished;
|
||||
}
|
||||
|
||||
/** Begin continuous capture (Transcribe): the mic stays on until released. */
|
||||
@@ -262,6 +311,9 @@ export class FaceclawVoiceControlBridge {
|
||||
}
|
||||
|
||||
this.speechPause.reset();
|
||||
// Replaces whatever the last capture left behind ("Listening...", an
|
||||
// error, the permission prompt) until the controller reports progress.
|
||||
this.setStatus("Starting microphone...");
|
||||
// A previous push-to-talk commit may still be awaiting its final result.
|
||||
this.cloudClient?.stop();
|
||||
const cloudClient = this.createCloudClient(options);
|
||||
@@ -275,7 +327,12 @@ export class FaceclawVoiceControlBridge {
|
||||
|
||||
this.cloudClient = null;
|
||||
this.started = true;
|
||||
this.controller?.start("onboard");
|
||||
// Which on-device model to load; a no-op setter for every provider except
|
||||
// "onboard-whisper" (FaceclawVoiceController defaults to Moonshine).
|
||||
this.controller?.setOnboardModelKind(options.provider === "onboard-whisper" ? "whisper" : "moonshine");
|
||||
const captureId = this.nextNativeCaptureId++;
|
||||
this.nativeCaptureFinished = new Promise((resolve) => { this.nativeCaptureResolvers.set(captureId, resolve); });
|
||||
this.controller?.start("onboard", captureId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -284,7 +341,7 @@ export class FaceclawVoiceControlBridge {
|
||||
* than failing the capture outright.
|
||||
*/
|
||||
private createCloudClient(options: PushToTalkOptions): CloudSttClient | null {
|
||||
if (options.provider === "onboard") return null;
|
||||
if (options.provider === "onboard" || options.provider === "onboard-whisper") return null;
|
||||
const sttOptions = {
|
||||
apiKey: "",
|
||||
onTranscript: (event: CloudSttTranscriptEvent) =>
|
||||
@@ -444,6 +501,10 @@ export class FaceclawVoiceControlBridge {
|
||||
onTranscript: (text: string, isFinal: boolean) => {
|
||||
this.emitTranscript(String(text), Boolean(isFinal));
|
||||
},
|
||||
onStopped: (captureId: number) => {
|
||||
this.nativeCaptureResolvers.get(captureId)?.();
|
||||
this.nativeCaptureResolvers.delete(captureId);
|
||||
},
|
||||
onPcm: (pcm: any) => {
|
||||
const bytes = toUint8Array(pcm);
|
||||
this.cloudClient?.acceptPcm(bytes);
|
||||
@@ -492,12 +553,21 @@ export class FaceclawVoiceControlBridge {
|
||||
}
|
||||
}
|
||||
|
||||
private setStatus(status: string): void {
|
||||
private setStatus(status: string, detail = ""): void {
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
// Every "audio is flowing" report — from the Java controller and from the
|
||||
// cloud providers — is phrased "Listening..." / "Listening (X)...".
|
||||
this.listening = status.startsWith("Listening");
|
||||
const state = this.state();
|
||||
for (const listener of this.statusListeners) {
|
||||
listener({ status });
|
||||
listener(state);
|
||||
}
|
||||
}
|
||||
|
||||
private state(): VoiceControlState {
|
||||
return { status: this.status, listening: this.listening, detail: this.detail };
|
||||
}
|
||||
}
|
||||
|
||||
function toJavaFloats(values: Float32Array): any {
|
||||
|
||||
@@ -24,6 +24,10 @@ export const WEAR_PATHS = {
|
||||
text: "/faceclaw/text",
|
||||
/** Watch -> phone: please re-publish the state item. */
|
||||
stateRequest: "/faceclaw/state/request",
|
||||
/** Watch -> phone: the watch's own battery ({battery, charging}); never acked. */
|
||||
battery: "/faceclaw/battery",
|
||||
/** Phone -> watch: please report the watch battery (answered even while the watch app is closed). */
|
||||
batteryRequest: "/faceclaw/battery/request",
|
||||
/** Phone -> watch: reply to any of the above. */
|
||||
ack: "/faceclaw/ack",
|
||||
/** Phone -> watch: assistant activity, alerts, voice-dialog transcripts. */
|
||||
@@ -157,7 +161,12 @@ class WearBridge {
|
||||
|
||||
/** Send an event to every reachable watch. */
|
||||
sendEvent(payload: Record<string, unknown>): void {
|
||||
this.getJava()?.sendToWatch(WEAR_PATHS.event, JSON.stringify(payload));
|
||||
this.sendToWatch(WEAR_PATHS.event, payload);
|
||||
}
|
||||
|
||||
/** Send a message on any phone -> watch path to every reachable watch. */
|
||||
sendToWatch(path: string, payload: Record<string, unknown>): void {
|
||||
this.getJava()?.sendToWatch(path, JSON.stringify(payload));
|
||||
}
|
||||
|
||||
/** Acknowledge a watch message; `seq` echoes the watch's sequence number. */
|
||||
|
||||
@@ -7,14 +7,13 @@
|
||||
<Label col="2" text="{{ connectionStatusLabel }}" tap="{{ onConnectionStatusTap }}" class="action-bar-status" />
|
||||
<Label col="3" text="⚠️" tap="{{ onWarningIconTap }}" visibility="{{ warningIconVisibility }}" class="action-bar-warning" />
|
||||
</GridLayout>
|
||||
<ActionItem text="{{ buttonLabel }}" tap="{{ onTap }}" isEnabled="{{ connectItemEnabled }}" android.position="popup" />
|
||||
<ActionItem text="{{ buttonLabel }}" tap="{{ onTap }}" isEnabled="{{ connectItemEnabled }}" visibility="{{ glassesMenuItemsVisibility }}" android.position="popup" />
|
||||
<ActionItem text="Pair glasses" tap="{{ onPairGlassesTap }}" isEnabled="{{ canRun }}" android.position="popup" />
|
||||
<ActionItem text="Configure devices" tap="{{ onConfigureTap }}" isEnabled="{{ canRun }}" android.position="popup" />
|
||||
<ActionItem text="Permissions" tap="{{ onPermissionsTap }}" android.position="popup" />
|
||||
<ActionItem text="Conversations" tap="{{ onConversationsTap }}" android.position="popup" />
|
||||
<ActionItem text="Take screenshot" tap="{{ onTakeScreenshotTap }}" android.position="popup" />
|
||||
<ActionItem text="Record screen" tap="{{ onRecordScreenTap }}" android.position="popup" />
|
||||
<ActionItem text="Uninstall custom firmware" tap="{{ onUninstallFirmwareTap }}" isEnabled="{{ canRun }}" android.position="popup" />
|
||||
<ActionItem text="Uninstall custom firmware" tap="{{ onUninstallFirmwareTap }}" isEnabled="{{ canRun }}" visibility="{{ glassesMenuItemsVisibility }}" android.position="popup" />
|
||||
</ActionBar>
|
||||
|
||||
<GridLayout>
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
import { sampleBleTraffic } from "../native/ble-traffic";
|
||||
import { isValidMacAddress, loadDeviceAddresses } from "../g2/device-addresses";
|
||||
import { isAutoReconnectSuppressed, resumeAutoReconnect } from "../g2/reconnect-policy";
|
||||
import { isPreviewOnlyMode } from "./onboarding-state";
|
||||
import { formatErrorMessage } from "../util/format-error";
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
|
||||
|
||||
@@ -887,11 +888,6 @@ export class MainViewModel extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
onConfigureTap(): void {
|
||||
if (!this.canRun) return;
|
||||
Frame.topmost()?.navigate("phone-ui/config-page");
|
||||
}
|
||||
|
||||
onPermissionsTap(): void {
|
||||
Frame.topmost()?.navigate({
|
||||
moduleName: "phone-ui/permissions-page",
|
||||
@@ -904,6 +900,16 @@ export class MainViewModel extends Observable {
|
||||
Frame.topmost()?.navigate("phone-ui/conversations-page");
|
||||
}
|
||||
|
||||
/**
|
||||
* Preview-only users have no glasses paired, so the Connect and Uninstall
|
||||
* menu items have nothing to act on; hide them until pairing completes.
|
||||
* The view model is rebuilt on every visit to the main page, so this picks
|
||||
* up the mode change when pairing/flashing returns here.
|
||||
*/
|
||||
get glassesMenuItemsVisibility(): "visible" | "collapse" {
|
||||
return isPreviewOnlyMode() ? "collapse" : "visible";
|
||||
}
|
||||
|
||||
/**
|
||||
* Live scan that names each nearby pair by model, colour, and serial and
|
||||
* checks both arms belong together. A connected arm stops advertising, so
|
||||
@@ -911,9 +917,18 @@ export class MainViewModel extends Observable {
|
||||
* state; pairing is a detour, not a Disconnect, so lift the suppression
|
||||
* right away — nothing dials the glasses until the main page's autoConnect
|
||||
* runs again on the way back.
|
||||
*
|
||||
* In preview-only mode there are no glasses yet, so pairing is really the
|
||||
* rest of onboarding: re-enter that chain at its "Disconnect Other Apps"
|
||||
* step, which leads to the scan, the firmware check, and flashing. The
|
||||
* chain's Back buttons pop history, so its first page returns here.
|
||||
*/
|
||||
async onPairGlassesTap(): Promise<void> {
|
||||
if (!this.canRun) return;
|
||||
if (isPreviewOnlyMode()) {
|
||||
Frame.topmost()?.navigate({ moduleName: "phone-ui/onboarding-unpair-page" });
|
||||
return;
|
||||
}
|
||||
if (this.phase === "connected" || this.phase === "charging" || this.phase === "connecting") {
|
||||
try {
|
||||
await dashboardController.disconnect();
|
||||
@@ -1002,8 +1017,15 @@ export class MainViewModel extends Observable {
|
||||
// Each pad therefore defers its double-click until the finger-up (the touch
|
||||
// handler) and converts the deferred pair into the G2 tap-then-hold gesture
|
||||
// when a longPress lands first.
|
||||
//
|
||||
// A hold is two events, like the hardware's: longPress sends the press
|
||||
// (long-press-start, which the controller delivers as a plain long-press)
|
||||
// and the finger-up sends the release, so a hold really holds (the
|
||||
// Glanceboard stays up until the finger lifts). The firmware sends the
|
||||
// same release after a tap-then-hold, so that pair gets one too.
|
||||
|
||||
private ringPadDoubleTapPending = false;
|
||||
private ringPadHeld = false;
|
||||
|
||||
async onRingPadTap(): Promise<void> {
|
||||
await dashboardController.injectSyntheticRingInput("click");
|
||||
@@ -1014,8 +1036,9 @@ export class MainViewModel extends Observable {
|
||||
}
|
||||
|
||||
async onRingPadLongPress(): Promise<void> {
|
||||
const kind = this.ringPadDoubleTapPending ? "short-then-long-press" : "long-press";
|
||||
const kind = this.ringPadDoubleTapPending ? "short-then-long-press" : "long-press-start";
|
||||
this.ringPadDoubleTapPending = false;
|
||||
this.ringPadHeld = true;
|
||||
await dashboardController.injectSyntheticRingInput(kind);
|
||||
}
|
||||
|
||||
@@ -1023,6 +1046,11 @@ export class MainViewModel extends Observable {
|
||||
if (args.action !== "up" && args.action !== "cancel") return;
|
||||
const pending = this.ringPadDoubleTapPending;
|
||||
this.ringPadDoubleTapPending = false;
|
||||
if (this.ringPadHeld) {
|
||||
this.ringPadHeld = false;
|
||||
await dashboardController.injectSyntheticRingInput("long-press-release");
|
||||
return;
|
||||
}
|
||||
if (args.action === "up" && pending) {
|
||||
await dashboardController.injectSyntheticRingInput("double-click");
|
||||
}
|
||||
@@ -1049,8 +1077,9 @@ export class MainViewModel extends Observable {
|
||||
|
||||
private padTwoFingerDown = false;
|
||||
// See the ring pad above: defers the double-click so a longPress can turn
|
||||
// the pair into tap-then-hold.
|
||||
// the pair into tap-then-hold, and pairs every hold with a release.
|
||||
private padDoubleTapPending = false;
|
||||
private padHeld = false;
|
||||
|
||||
/** What the next gesture lands on, as the watch pad shows it. */
|
||||
get padFocusLine(): string {
|
||||
@@ -1072,8 +1101,10 @@ export class MainViewModel extends Observable {
|
||||
}
|
||||
|
||||
async onPadLongPress(): Promise<void> {
|
||||
const kind = this.padDoubleTapPending ? "short-then-long-press" : "long-press";
|
||||
if (this.padTwoFingerDown) return;
|
||||
const kind = this.padDoubleTapPending ? "short-then-long-press" : "long-press-start";
|
||||
this.padDoubleTapPending = false;
|
||||
this.padHeld = true;
|
||||
await dashboardController.injectSyntheticRingInput(kind, "watch");
|
||||
this.refreshPadFocusLine();
|
||||
}
|
||||
@@ -1093,6 +1124,12 @@ export class MainViewModel extends Observable {
|
||||
if (args.action === "up" || args.action === "cancel") {
|
||||
const pendingDouble = this.padDoubleTapPending;
|
||||
this.padDoubleTapPending = false;
|
||||
if (this.padHeld) {
|
||||
this.padHeld = false;
|
||||
await dashboardController.injectSyntheticRingInput("long-press-release", "watch");
|
||||
this.refreshPadFocusLine();
|
||||
return;
|
||||
}
|
||||
const twoFinger = this.padTwoFingerDown;
|
||||
if (twoFinger) {
|
||||
// Let the single-tap recognizer's delayed tap see the flag first.
|
||||
|
||||
@@ -7,12 +7,19 @@ import {
|
||||
hasExtractedEvenHubFonts,
|
||||
type FirmwareProgress,
|
||||
} from "../g2/firmware-builder";
|
||||
import { classifyOnboardingFirmware, FLASHABLE_STOCK_VERSION_TEXT, type OnboardingFirmwareKind } from "../g2/firmware-compat";
|
||||
import {
|
||||
BASE_STOCK_VERSION_TEXT,
|
||||
REQUIRED_FACECLAW_FIRMWARE_VERSION,
|
||||
classifyOnboardingFirmware,
|
||||
describeFirmwareExtension,
|
||||
type FirmwareExtension,
|
||||
type OnboardingFirmwareKind,
|
||||
} from "../g2/firmware-compat";
|
||||
import { DeviceInfoProbe, DeviceInfoState } from "../native/device-info-probe";
|
||||
import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state";
|
||||
import { formatErrorMessage } from "../util/format-error";
|
||||
|
||||
type CheckPhase = "checking" | "fonts" | "custom" | "flashable" | "newer" | "error";
|
||||
type CheckPhase = "checking" | "fonts" | "custom" | "flashable" | "newer-validated" | "newer" | "error";
|
||||
|
||||
export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
private _phase: CheckPhase = "checking";
|
||||
@@ -72,6 +79,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
case "custom":
|
||||
return "Finish";
|
||||
case "flashable":
|
||||
case "newer-validated":
|
||||
return "Install Firmware";
|
||||
case "newer":
|
||||
return "Proceed Anyway";
|
||||
@@ -107,6 +115,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
this.finish();
|
||||
return;
|
||||
case "flashable":
|
||||
case "newer-validated":
|
||||
case "newer":
|
||||
this.goToFlashing();
|
||||
return;
|
||||
@@ -143,7 +152,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
if (this._phase !== "error") return;
|
||||
// Same path as a real "custom" classification, so the phone-side G2 fonts
|
||||
// still get extracted when they're missing.
|
||||
this.applyClassification("custom", "", "");
|
||||
this.applyClassification("custom", "", { kind: "none" });
|
||||
}
|
||||
|
||||
// --- probe flow ------------------------------------------------------------
|
||||
@@ -169,35 +178,39 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
this.disposeProbe();
|
||||
const probe = new DeviceInfoProbe(stored.right, stored.left);
|
||||
this.probeInstance = probe;
|
||||
probe.onStateChange((state) => this.reportProbeState(state));
|
||||
probe.onStateChange((state, detail) => this.reportProbeState(state, detail));
|
||||
|
||||
const info = await probe.run();
|
||||
this.probeInstance = null;
|
||||
|
||||
const { kind, version } = classifyOnboardingFirmware(info);
|
||||
this.applyClassification(kind, version, info.capabilities.trim());
|
||||
const { kind, version, extension } = classifyOnboardingFirmware(info);
|
||||
this.applyClassification(kind, version, extension);
|
||||
} catch (error) {
|
||||
this.probeInstance = null;
|
||||
this.toError(this.formatError(error));
|
||||
}
|
||||
}
|
||||
|
||||
private reportProbeState(state: DeviceInfoState): void {
|
||||
private reportProbeState(state: DeviceInfoState, detail: string): void {
|
||||
// The probe brings up the right lens, then the left, and pairs with each
|
||||
// in turn (they are separate Bluetooth devices with separate bonds).
|
||||
const lens = detail === "left" || detail === "right" ? `the ${detail} lens` : "your glasses";
|
||||
if (state === "connecting") {
|
||||
this.status = "Connecting to your glasses...";
|
||||
this.status = `Connecting to ${lens}...`;
|
||||
} else if (state === "authenticating") {
|
||||
// First-time connections pair here; the OS may show a Bluetooth dialog.
|
||||
this.status = "Authenticating with your glasses... If Android asks to pair, tap Pair.";
|
||||
this.status = `Pairing with ${lens}... Each lens pairs separately; if Android asks to pair, tap Pair.`;
|
||||
} else if (state === "querying") {
|
||||
this.status = "Reading the firmware version...";
|
||||
}
|
||||
}
|
||||
|
||||
private applyClassification(kind: OnboardingFirmwareKind, version: string, capabilities: string): void {
|
||||
private applyClassification(kind: OnboardingFirmwareKind, version: string, extension: FirmwareExtension): void {
|
||||
this.busy = false;
|
||||
// `kind` is classifyOnboardingFirmware's OnboardingFirmwareKind ("custom" |
|
||||
// "flashable-stock" | "newer-stock" | "unknown") -- NOT CheckPhase's shorter
|
||||
// "flashable"/"newer" (used below via setPhase for _phase/primaryLabel/etc).
|
||||
// "older-faceclaw" | "other-custom" | "flashable-stock" | "newer-stock-unvalidated" |
|
||||
// "newer-stock-validated" | "unknown") -- NOT CheckPhase's shorter
|
||||
// "flashable"/"newer-validated"/"newer" (used below via setPhase for _phase/primaryLabel/etc).
|
||||
// The two types share two of four names, so a typo'd case label here type-checks
|
||||
// fine as long as `kind` stays plain `string`, but silently falls through to
|
||||
// `default` (the hard "couldn't read a firmware version" error) for every real
|
||||
@@ -206,10 +219,26 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
switch (kind) {
|
||||
case "custom":
|
||||
if (!hasExtractedEvenHubFonts()) {
|
||||
void this.extractFontsForCustomFirmware(version, capabilities);
|
||||
void this.extractFontsForCustomFirmware(version, extension);
|
||||
break;
|
||||
}
|
||||
this.showCustomReady(version, capabilities, false);
|
||||
this.showCustomReady(version, extension, false);
|
||||
break;
|
||||
case "older-faceclaw":
|
||||
this.setPhase("flashable");
|
||||
this.headline = "Firmware Update Available";
|
||||
this.status =
|
||||
`Your glasses run ${describeFirmwareExtension(extension)}${version ? `, based on stock ${version}` : ""}. ` +
|
||||
`This version of Faceclaw needs revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}. ` +
|
||||
"Tap Install Firmware to update it.";
|
||||
break;
|
||||
case "other-custom":
|
||||
this.setPhase("flashable");
|
||||
this.headline = "Other Custom Firmware";
|
||||
this.status =
|
||||
`Your glasses run ${describeFirmwareExtension(extension)}${version ? `, based on stock ${version}` : ""}. ` +
|
||||
`Faceclaw needs its own custom firmware (revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}). ` +
|
||||
"Tap Install Firmware to replace the current firmware with it.";
|
||||
break;
|
||||
case "flashable-stock":
|
||||
this.setPhase("flashable");
|
||||
@@ -218,11 +247,21 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
`Your glasses run stock firmware ${version}. This is compatible — tap Install Firmware to flash ` +
|
||||
"Faceclaw's custom firmware.";
|
||||
break;
|
||||
case "newer-stock":
|
||||
case "newer-stock-validated":
|
||||
this.setPhase("newer-validated");
|
||||
this.headline = "Ready to Install";
|
||||
this.status =
|
||||
`Your glasses run stock firmware ${version}, which is compatible. This version is newer than the ` +
|
||||
`${BASE_STOCK_VERSION_TEXT} release Faceclaw's custom image is built from. You may not be able to ` +
|
||||
`use the official Even app without upgrading back to ${version} first (the official Even app will ` +
|
||||
`perform that upgrade for you if you re-pair it).`;
|
||||
break;
|
||||
|
||||
case "newer-stock-unvalidated":
|
||||
this.setPhase("newer");
|
||||
this.headline = "Unrecognized Firmware";
|
||||
this.status =
|
||||
`Your glasses run stock firmware ${version}, which is newer than the ${FLASHABLE_STOCK_VERSION_TEXT} ` +
|
||||
`Your glasses run stock firmware ${version}, which is newer than the ${BASE_STOCK_VERSION_TEXT} ` +
|
||||
"release Faceclaw's custom image is built from. Flashing may not work correctly and carries extra risk. " +
|
||||
"You can proceed anyway, or go back.";
|
||||
break;
|
||||
@@ -236,7 +275,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
private async extractFontsForCustomFirmware(version: string, capabilities: string): Promise<void> {
|
||||
private async extractFontsForCustomFirmware(version: string, extension: FirmwareExtension): Promise<void> {
|
||||
this.setPhase("fonts");
|
||||
this.headline = "Preparing G2 Fonts";
|
||||
this.busy = true;
|
||||
@@ -245,7 +284,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
"Downloading the official firmware to extract them; your glasses will not be reflashed.";
|
||||
try {
|
||||
await downloadAndExtractEvenHubFonts((progress) => this.reportFontProgress(progress));
|
||||
this.showCustomReady(version, capabilities, true);
|
||||
this.showCustomReady(version, extension, true);
|
||||
} catch (error) {
|
||||
this.toError(
|
||||
`Custom firmware is installed, but the G2 fonts could not be prepared: ${this.formatError(error)}`,
|
||||
@@ -270,13 +309,16 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
private showCustomReady(version: string, capabilities: string, extractedFonts: boolean): void {
|
||||
private showCustomReady(version: string, extension: FirmwareExtension, extractedFonts: boolean): void {
|
||||
this.busy = false;
|
||||
this.setPhase("custom");
|
||||
this.headline = extractedFonts ? "Fonts Ready" : "Custom Firmware Detected";
|
||||
const details = [
|
||||
extension.kind === "faceclaw" ? `revision ${extension.version}` : "",
|
||||
version ? `based on stock ${version}` : "",
|
||||
].filter(Boolean);
|
||||
this.status =
|
||||
`Your glasses already run Faceclaw's custom firmware${version ? ` (version ${version})` : ""}` +
|
||||
`${capabilities ? `, extensions: ${capabilities}` : ""}. ` +
|
||||
`Your glasses already run Faceclaw's custom firmware${details.length ? ` (${details.join(", ")})` : ""}. ` +
|
||||
(extractedFonts
|
||||
? "The phone-side G2 fonts were extracted successfully. No flashing was needed — you're all set."
|
||||
: "The phone-side G2 fonts are present. No flashing needed — you're all set.");
|
||||
@@ -288,7 +330,9 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
this.disposeProbe();
|
||||
Frame.topmost()?.navigate({
|
||||
moduleName: "phone-ui/onboarding-flash-page",
|
||||
context: { mode: "install", fromOnboarding: true },
|
||||
// This page already described the install; skip the flash page's
|
||||
// intro/"Connect & Confirm" step and connect immediately.
|
||||
context: { mode: "install", fromOnboarding: true, autoStart: true },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,12 @@ import { FlashMode, OnboardingFlashViewModel } from "./onboarding-flash-view-mod
|
||||
export function navigatingTo(args: NavigatedData): void {
|
||||
const page = args.object as Page;
|
||||
if (!page.bindingContext) {
|
||||
const context = (args.context as { mode?: FlashMode; fromOnboarding?: boolean } | undefined) ?? undefined;
|
||||
const context =
|
||||
(args.context as { mode?: FlashMode; fromOnboarding?: boolean; autoStart?: boolean } | undefined) ?? undefined;
|
||||
page.bindingContext = new OnboardingFlashViewModel({
|
||||
mode: context?.mode ?? "install",
|
||||
fromOnboarding: context?.fromOnboarding ?? true,
|
||||
autoStart: context?.autoStart ?? false,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ import { resumeAutoReconnect, suppressAutoReconnect } from "../g2/reconnect-poli
|
||||
import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state";
|
||||
import { formatErrorMessage } from "../util/format-error";
|
||||
|
||||
type FlashPhase = "intro" | "prompt" | "building" | "ready" | "flashing" | "flashed" | "error";
|
||||
type FlashPhase = "intro" | "prompt" | "building" | "flashing" | "flashed" | "error";
|
||||
|
||||
export type FlashMode = "install" | "uninstall";
|
||||
|
||||
@@ -53,7 +53,7 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
private flasherUnsubscribers: Array<() => void> = [];
|
||||
private retryAction: () => void = () => this.beginPrompt();
|
||||
|
||||
constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean }) {
|
||||
constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean; autoStart?: boolean }) {
|
||||
super();
|
||||
// The flash flow needs the glasses to itself: from here on the main
|
||||
// page must not auto-reconnect, until an install succeeds (below) or
|
||||
@@ -68,13 +68,19 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
"and reflashes the official firmware — removing Faceclaw's custom features."
|
||||
: "This connects to your glasses, asks for confirmation on the lens, checks the battery, then downloads, " +
|
||||
"verifies, and flashes Faceclaw's custom firmware.";
|
||||
if (options?.autoStart) {
|
||||
// Entered from a page that already explained what's about to happen
|
||||
// (the onboarding firmware check), so the intro/"Connect & Confirm"
|
||||
// step would be redundant: go straight to connecting.
|
||||
void this.beginPrompt();
|
||||
}
|
||||
}
|
||||
|
||||
// Kept short to fit the glasses' ~50-column text grid.
|
||||
private get glassesWarning(): string {
|
||||
return this.mode === "uninstall"
|
||||
? "Reinstalling the official firmware removes Faceclaw's custom features. Continue?"
|
||||
: "Flashing custom firmware will void your warranty and carries some risk of bricking the glasses. Continue?";
|
||||
: "Flashing custom firmware will void your warranty. Continue?";
|
||||
}
|
||||
|
||||
private get noun(): string {
|
||||
@@ -161,8 +167,6 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
switch (this._phase) {
|
||||
case "intro":
|
||||
return "Connect & Confirm";
|
||||
case "ready":
|
||||
return "Flash Now";
|
||||
case "flashed":
|
||||
return "Finish";
|
||||
case "error":
|
||||
@@ -176,8 +180,6 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
switch (this._phase) {
|
||||
case "prompt":
|
||||
return "Cancel";
|
||||
case "ready":
|
||||
return "Not Now";
|
||||
default:
|
||||
return "Back";
|
||||
}
|
||||
@@ -201,9 +203,6 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
case "intro":
|
||||
void this.beginPrompt();
|
||||
return;
|
||||
case "ready":
|
||||
this.startFlashing();
|
||||
return;
|
||||
case "flashed":
|
||||
this.finish();
|
||||
return;
|
||||
@@ -330,7 +329,7 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
this.disposePrompt();
|
||||
if (!approved) {
|
||||
this.busy = false;
|
||||
this.toError("You declined on the glasses. No firmware was written.", () => this.beginPrompt());
|
||||
this.toError("You declined on the glasses. Firmware was not installed.", () => this.beginPrompt());
|
||||
return;
|
||||
}
|
||||
const lowBattery = this.describeLowBattery(this.promptBattery);
|
||||
@@ -392,15 +391,9 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
const build = this.mode === "uninstall" ? buildStockFirmware : buildCustomFirmware;
|
||||
const result = await build((progress) => this.reportBuildProgress(progress));
|
||||
this.firmwarePath = result.path;
|
||||
this.busy = false;
|
||||
this.setPhase("ready");
|
||||
this.headline = "Firmware Ready";
|
||||
this.status =
|
||||
`The ${this.noun} is prepared and verified (${result.bytes.toLocaleString()} bytes).\n\n` +
|
||||
"Tap Flash Now to write it to your glasses. Keep both lenses powered on and nearby — " +
|
||||
"each lens takes a few minutes, and the glasses will reboot when each lens finishes. " +
|
||||
"Do not close the app during flashing.";
|
||||
this.appendLog(`saved to ${result.path}`);
|
||||
this.appendLog(`${this.noun} prepared and verified (${result.bytes.toLocaleString()} bytes), saved to ${result.path}`);
|
||||
// The user already said Yes on the lens; no second confirmation here.
|
||||
this.startFlashing();
|
||||
} catch (error) {
|
||||
this.busy = false;
|
||||
const message =
|
||||
@@ -445,7 +438,9 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
this.headline = "Flashing Firmware";
|
||||
this.busy = true;
|
||||
this.progress = 0;
|
||||
this.status = "Starting. Do not close the app or power off the glasses.";
|
||||
this.status =
|
||||
"Starting. Keep both lenses powered on and nearby — each lens takes a few minutes, " +
|
||||
"and the glasses will reboot when each lens finishes. Do not close the app during flashing.";
|
||||
|
||||
this.disposeFlasher();
|
||||
const flasher = new FirmwareFlasher(this.addresses, this.firmwarePath);
|
||||
|
||||
Loaded 100 of 156 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user