diff --git a/.gitignore b/.gitignore index 326fbf1f..6ecde3ea 100644 --- a/.gitignore +++ b/.gitignore @@ -58,6 +58,8 @@ native/kotlin/.kotlin/ native/kotlin/build/ native/kotlin/shared/build/ native/kotlin/local.properties +# Go SSH transport build outputs (scripts/go-ssh-build.cjs) +native/ssh/build/ native/kotlin/plugin/platforms/android/*.aar native/kotlin/plugin/platforms/ios/ !native/kotlin/plugin/platforms/ diff --git a/ACKNOWLEDGEMENTS.md b/ACKNOWLEDGEMENTS.md index 7e9ee19f..4b7709b0 100644 --- a/ACKNOWLEDGEMENTS.md +++ b/ACKNOWLEDGEMENTS.md @@ -23,6 +23,12 @@ and Mentra Labs, Inc. (MIT License): https://github.com/tetramo-labs/glassly and https://github.com/tetramo-labs/glassly-rayneo-cfw. The iO wire codecs in native/kotlin/shared/.../rayneo/RayNeoIoProtocol.kt follow their protocol work. +Bluetooth keyboard support on the G2 uses the keyboard bridge Tetramo, Inc. wrote +for their Glassly custom firmware (https://github.com/tetramo-labs/g2flash). +Faceclaw's client for it, in app/g2/keyboard/ and app/apps/terminal/terminal-keys.ts, +follows the reference client in that repository and the keyboard handling in the +Glassly app (MIT License): https://github.com/tetramo-labs/glassly. + iPhone notification icons are Pebble's notification and timeline icons, from https://github.com/pebble-dev/iconography: the original Pebble Technology icons and Rebble community additions. They are licensed under the Apache License 2.0; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt index 9f0e47e5..531244ce 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt @@ -290,6 +290,13 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre /** See [GlassesSessionCore.getRingStatus]. */ fun getRingStatus(): String = core.getRingStatus() + /** See [GlassesSessionCore.setHeadUpTrigger]. */ + fun setHeadUpTrigger(degrees: Int, originalSwitch: Int, originalAngle: Int) = + core.setHeadUpTrigger(degrees, originalSwitch, originalAngle) + + /** See [GlassesSessionCore.getHeadUpStatus]. */ + fun getHeadUpStatus(): String = core.getHeadUpStatus() + /** See [GlassesSessionCore.setGlassesRingLink]. */ fun setGlassesRingLink(action: String, fallbackAddress: String?, fallbackName: String?): Boolean = core.setGlassesRingLink(action, fallbackAddress, fallbackName) @@ -322,6 +329,13 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre fun sendFaceclawMicControl(record: ByteArray?, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean = core.sendFaceclawMicControl(record, label, rightTemple, leftTemple) + fun addKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.addKeyboardEventListener(listener) + + fun removeKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.removeKeyboardEventListener(listener) + + fun sendFaceclawKeyboardControl(record: ByteArray?, label: String): Boolean = + core.sendFaceclawKeyboardControl(record, label) + fun startG2AudioForwarding(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioForwarding(listener) fun stopG2AudioForwarding() = core.stopG2AudioForwarding() diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSsh.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSsh.kt new file mode 100644 index 00000000..4efc7c9d --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSsh.kt @@ -0,0 +1,212 @@ +package com.faceclaw.app + +import android.content.ClipData +import android.content.ClipboardManager +import android.content.Context +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.security.keystore.KeyGenParameterSpec +import android.security.keystore.KeyProperties +import android.security.keystore.StrongBoxUnavailableException +import android.util.Base64 +import android.util.Log +import com.faceclaw.ssh.faceclawssh.Channel +import com.faceclaw.ssh.faceclawssh.Faceclawssh +import com.faceclaw.ssh.faceclawssh.Listener +import com.faceclaw.ssh.faceclawssh.Signer +import java.security.KeyPairGenerator +import java.security.KeyStore +import java.security.PrivateKey +import java.security.Signature +import java.security.spec.ECGenParameterSpec +import javax.crypto.Cipher +import javax.crypto.KeyGenerator +import javax.crypto.SecretKey +import javax.crypto.spec.GCMParameterSpec + +/** + * One SSH channel for the TypeScript side (app/native/ssh.ts), backed by the + * gomobile-bound Go transport in native/ssh. `spec` is the Go openSpec JSON. + * Events arrive from Go goroutines and are posted to the Looper of the + * thread that constructed this object (a JS isolate's own thread), like + * FaceclawWebSocket. + */ +class FaceclawSshChannel(spec: String, useKey: Boolean, listener: FaceclawSshListener) { + private val handler = Handler(Looper.myLooper() ?: Looper.getMainLooper()) + private val channel: Channel + + init { + val signer = if (useKey) FaceclawSshKeys.signer() else null + channel = Faceclawssh.open(spec, signer, object : Listener { + override fun onEvent(kind: String?, payload: String?) { + handler.post { + try { + listener.onEvent(kind, payload) + } catch (t: Throwable) { + Log.w(TAG, "listener onEvent($kind) failed", t) + } + } + } + }) + } + + fun send(text: String): Boolean = channel.send(text) + + fun close() = channel.close() + + private companion object { + const val TAG = "FaceclawSsh" + } +} + +/** + * The device's SSH client key and the Terminal app's saved SSH passwords. + * + * The client key is an ECDSA P-256 key generated inside Android Keystore + * (StrongBox when the device has one) and never exportable: only its public + * half leaves the keystore, as an authorized_keys line. Passwords are + * encrypted with an AES-GCM key that also lives in Keystore, and stored in a + * private SharedPreferences file kept apart from the settings store (which is + * exported by the settings port). + */ +object FaceclawSshKeys { + private const val TAG = "FaceclawSsh" + private const val CLIENT_KEY_ALIAS = "faceclaw-ssh-client" + private const val SECRET_KEY_ALIAS = "faceclaw-ssh-secrets" + private const val SECRETS_PREFS = "faceclaw_ssh_secrets" + private const val GCM_TAG_BITS = 128 + + private fun keyStore(): KeyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) } + + @JvmStatic + @Synchronized + fun hasClientKey(): Boolean = keyStore().containsAlias(CLIENT_KEY_ALIAS) + + /** Generate the client key if it doesn't exist yet. */ + @JvmStatic + @Synchronized + fun ensureClientKey() { + if (keyStore().containsAlias(CLIENT_KEY_ALIAS)) return + fun spec(strongBox: Boolean): KeyGenParameterSpec { + val builder = KeyGenParameterSpec.Builder(CLIENT_KEY_ALIAS, KeyProperties.PURPOSE_SIGN) + .setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1")) + .setDigests(KeyProperties.DIGEST_SHA256) + if (strongBox && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) builder.setIsStrongBoxBacked(true) + return builder.build() + } + val generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore") + try { + generator.initialize(spec(strongBox = true)) + generator.generateKeyPair() + } catch (e: Exception) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && e is StrongBoxUnavailableException) { + Log.i(TAG, "StrongBox unavailable; generating the SSH key in the TEE") + } else { + Log.w(TAG, "StrongBox key generation failed; retrying in the TEE", e) + } + generator.initialize(spec(strongBox = false)) + generator.generateKeyPair() + } + Log.i(TAG, "generated SSH client key") + } + + /** X.509 SubjectPublicKeyInfo DER of the client key, or null if none. */ + @JvmStatic + fun publicKey(): ByteArray? = keyStore().getCertificate(CLIENT_KEY_ALIAS)?.publicKey?.encoded + + /** The client key's authorized_keys line (generating the key if needed). */ + @JvmStatic + fun authorizedKey(comment: String): String { + ensureClientKey() + return Faceclawssh.authorizedKey(publicKey(), comment) + } + + /** The client key's SHA256 fingerprint (generating the key if needed). */ + @JvmStatic + fun fingerprint(): String { + ensureClientKey() + return Faceclawssh.fingerprint(publicKey()) + } + + /** A Go Signer over the client key, generating the key if needed. */ + @JvmStatic + fun signer(): Signer? = try { + ensureClientKey() + val publicKey = publicKey() ?: throw IllegalStateException("client key has no certificate") + object : Signer { + override fun publicKey(): ByteArray = publicKey + override fun sign(data: ByteArray?): ByteArray { + val privateKey = keyStore().getKey(CLIENT_KEY_ALIAS, null) as PrivateKey + return Signature.getInstance("SHA256withECDSA").run { + initSign(privateKey) + update(data ?: ByteArray(0)) + sign() + } + } + } + } catch (t: Throwable) { + Log.w(TAG, "SSH client key unavailable", t) + null + } + + private fun secretKey(): SecretKey { + val store = keyStore() + (store.getKey(SECRET_KEY_ALIAS, null) as? SecretKey)?.let { return it } + val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore") + generator.init( + KeyGenParameterSpec.Builder(SECRET_KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT) + .setBlockModes(KeyProperties.BLOCK_MODE_GCM) + .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) + .setKeySize(256) + .build() + ) + return generator.generateKey() + } + + private fun prefs(context: Context) = + context.applicationContext.getSharedPreferences(SECRETS_PREFS, Context.MODE_PRIVATE) + + @JvmStatic + @Synchronized + fun setSecret(context: Context, id: String, value: String) { + val cipher = Cipher.getInstance("AES/GCM/NoPadding") + cipher.init(Cipher.ENCRYPT_MODE, secretKey()) + val sealed = cipher.iv + cipher.doFinal(value.toByteArray(Charsets.UTF_8)) + prefs(context).edit().putString(id, Base64.encodeToString(sealed, Base64.NO_WRAP)).apply() + } + + /** The saved secret, or "" if none (or it can no longer be decrypted). */ + @JvmStatic + @Synchronized + fun getSecret(context: Context, id: String): String { + val encoded = prefs(context).getString(id, null) ?: return "" + return try { + val sealed = Base64.decode(encoded, Base64.NO_WRAP) + val cipher = Cipher.getInstance("AES/GCM/NoPadding") + val ivLength = 12 + cipher.init(Cipher.DECRYPT_MODE, secretKey(), GCMParameterSpec(GCM_TAG_BITS, sealed, 0, ivLength)) + String(cipher.doFinal(sealed, ivLength, sealed.size - ivLength), Charsets.UTF_8) + } catch (t: Throwable) { + Log.w(TAG, "saved SSH secret unreadable; dropping it", t) + prefs(context).edit().remove(id).apply() + "" + } + } + + @JvmStatic + @Synchronized + fun removeSecret(context: Context, id: String) { + prefs(context).edit().remove(id).apply() + } + + /** Put text on the phone's clipboard (from any thread). */ + @JvmStatic + fun copyToClipboard(context: Context, label: String, text: String) { + val appContext = context.applicationContext + Handler(Looper.getMainLooper()).post { + val clipboard = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + clipboard.setPrimaryClip(ClipData.newPlainText(label, text)) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt index eb6068a6..239f08cc 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt @@ -114,6 +114,21 @@ class FaceclawVoiceController(context: Context) { } } + override fun micArrayLink(): MicArrayLink? { + val currentCommunicator = communicator ?: return null + return object : MicArrayLink { + override fun startForwarding(listener: FaceclawAudioPacketListener): Boolean = + currentCommunicator.startG2AudioForwarding(listener) + + override fun stopForwarding() = currentCommunicator.stopG2AudioForwarding() + + override fun isForwarding(): Boolean = currentCommunicator.isAudioCaptureActive() + + override fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean = + currentCommunicator.sendFaceclawMicControl(record, label, rightTemple, leftTemple) + } + } + override fun createPacketDecoder(): AudioPacketDecoder = if (communicator?.isRayNeoIo() == true) AndroidSpeechEngines.OpusDecoderAdapter() else AndroidSpeechEngines.Lc3DecoderAdapter() @@ -140,6 +155,11 @@ class FaceclawVoiceController(context: Context) { private val session = VoiceCaptureSession(host) + // The recording of this controller's live captures (see startRecording). It outlives any one + // capture, and is only handed to the session while recording is enabled. + private var recording: MicRecordingWriter? = null + private var recordingEnabled = true + fun setListener(listener: FaceclawVoiceControllerListener?) { session.setListener(listener) } @@ -201,6 +221,88 @@ class FaceclawVoiceController(context: Context) { session.setBeamFilter(enabled, centerDeg, halfWidthDeg) } + /** + * Capture glasses sessions from the temples' own microphones (the custom firmware's + * mic_control stream) instead of the stock mixed stream. [configureRecord] is the CONFIGURE + * record to arm both temples with; null goes back to the mixed stream. Must be set before + * {@link #start}. + */ + fun setMicArray(configureRecord: ByteArray?) { + session.setMicArray(configureRecord) + } + + /** + * Have the next {@link #start} play the recording at [path] (a .mka file) from [startMs] in + * place of any microphone; null goes back to the microphones. Progress arrives through the + * listener's onReplay. + */ + fun setReplay(path: String?, startMs: Long) { + session.setReplay(path, startMs) + } + + /** Continue the running replay from [positionMs] into its recording. */ + fun seekReplay(positionMs: Long) { + session.seekReplay(positionMs) + } + + fun setReplayPaused(paused: Boolean) { + session.setReplayPaused(paused) + } + + /** + * Record live captures to a new Matroska audio file at [path], from now until + * {@link #stopRecording}, across however many captures run in between: every source's audio + * as it arrives, before any processing. [configJson] describes the capture and is stored in + * the file. Returns false when the file cannot be created. + */ + @Synchronized + fun startRecording(path: String, configJson: String?): Boolean { + stopRecording() + return try { + val parent = File(path).parentFile + if (parent != null && !parent.exists()) parent.mkdirs() + recording = MicRecordingWriter(openFileOutput(path), configJson, System.currentTimeMillis()) + session.setRecorder(if (recordingEnabled) recording else null) + true + } catch (t: Throwable) { + Log.w(TAG, "could not create recording $path", t) + recording = null + false + } + } + + /** + * Whether captures are written to the recording right now: off while the capture running + * isn't one that is to be recorded. + */ + @Synchronized + fun setRecordingEnabled(enabled: Boolean) { + recordingEnabled = enabled + session.setRecorder(if (enabled) recording else null) + } + + /** Whether a recording is open and still being written (false once writing to it failed). */ + @Synchronized + fun isRecording(): Boolean = recording?.failed == false + + /** + * Note a change in what is being captured (a JSON object) at the recording's current + * position; the audio after it is placed afresh on the recording's timeline. + */ + @Synchronized + fun noteRecordingEvent(json: String) { + recording?.event(json) + } + + /** Complete the recording's file. Returns its length in ms, or -1 when none was open. */ + @Synchronized + fun stopRecording(): Long { + val current = recording ?: return -1 + recording = null + session.setRecorder(null) + return current.finish() + } + fun start(requestedMode: String?) { session.start(requestedMode, 0) } diff --git a/App_Resources/iOS/src/FaceclawSsh.h b/App_Resources/iOS/src/FaceclawSsh.h new file mode 100644 index 00000000..cf8a479a --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSsh.h @@ -0,0 +1,31 @@ +#import + +/** + * One SSH channel for the TypeScript side (app/native/ssh.ios.ts), backed by + * the gomobile-bound Go transport in native/ssh. `spec` is the Go openSpec + * JSON. Events arrive on Go threads and queue here; the owning JS isolate + * drains them with takeEvents (a JSON array of {kind, payload}). + */ +@interface FaceclawSshChannel : NSObject +- (instancetype)initWithSpec:(NSString *)spec useKey:(BOOL)useKey; +- (NSString *)takeEvents; +- (BOOL)send:(NSString *)text; +- (void)close; +@end + +/** + * The device's SSH client key (a non-exportable Secure Enclave P-256 key; a + * plain keychain key on the simulator, which has no Secure Enclave) and the + * Terminal app's saved SSH passwords (keychain generic passwords). + */ +@interface FaceclawSshKeys : NSObject +/** "ecdsa-sha2-nistp256 AAAA... comment"; generates the key if needed. nil on failure. */ ++ (NSString *)authorizedKeyWithComment:(NSString *)comment; +/** "SHA256:..." fingerprint; generates the key if needed. nil on failure. */ ++ (NSString *)fingerprint; ++ (void)setSecret:(NSString *)value forId:(NSString *)secretId; +/** The saved secret, or "" if none. */ ++ (NSString *)secretForId:(NSString *)secretId; ++ (void)removeSecretForId:(NSString *)secretId; ++ (void)copyToClipboard:(NSString *)text; +@end diff --git a/App_Resources/iOS/src/FaceclawSsh.m b/App_Resources/iOS/src/FaceclawSsh.m new file mode 100644 index 00000000..492707f5 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSsh.m @@ -0,0 +1,234 @@ +#import "FaceclawSsh.h" +#import +#import +#import + +static NSString *const kClientKeyTag = @"com.faceclaw.ssh.client"; +static NSString *const kSecretService = @"com.faceclaw.ssh.secret"; + +#pragma mark - Client key + +static NSData *ClientKeyTagData(void) { + return [kClientKeyTag dataUsingEncoding:NSUTF8StringEncoding]; +} + +static SecKeyRef CopyClientKey(void) { + NSDictionary *query = @{ + (id)kSecClass: (id)kSecClassKey, + (id)kSecAttrApplicationTag: ClientKeyTagData(), + (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, + (id)kSecReturnRef: @YES, + }; + CFTypeRef key = NULL; + if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &key) != errSecSuccess) return NULL; + return (SecKeyRef)key; +} + +static SecKeyRef CreateClientKey(BOOL secureEnclave) { + SecAccessControlRef access = SecAccessControlCreateWithFlags( + kCFAllocatorDefault, kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, + secureEnclave ? kSecAccessControlPrivateKeyUsage : 0, NULL); + if (!access) return NULL; + NSMutableDictionary *attributes = [@{ + (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, + (id)kSecAttrKeySizeInBits: @256, + (id)kSecPrivateKeyAttrs: @{ + (id)kSecAttrIsPermanent: @YES, + (id)kSecAttrApplicationTag: ClientKeyTagData(), + (id)kSecAttrAccessControl: (__bridge id)access, + }, + } mutableCopy]; + if (secureEnclave) attributes[(id)kSecAttrTokenID] = (id)kSecAttrTokenIDSecureEnclave; + CFErrorRef error = NULL; + SecKeyRef key = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes, &error); + CFRelease(access); + if (!key && error) { + NSLog(@"FaceclawSsh: key generation failed (secure enclave %d): %@", secureEnclave, (__bridge NSError *)error); + CFRelease(error); + } + return key; +} + +/** The client key, generating it on first use. Caller releases. */ +static SecKeyRef CopyOrCreateClientKey(void) { + @synchronized([FaceclawSshKeys class]) { + SecKeyRef key = CopyClientKey(); + if (key) return key; + key = CreateClientKey(YES); +#if TARGET_OS_SIMULATOR + if (!key) key = CreateClientKey(NO); +#endif + return key; + } +} + +/** Uncompressed X9.62 public point (0x04 || X || Y). */ +static NSData *PublicKeyData(SecKeyRef key) { + SecKeyRef publicKey = SecKeyCopyPublicKey(key); + if (!publicKey) return nil; + NSData *data = (__bridge_transfer NSData *)SecKeyCopyExternalRepresentation(publicKey, NULL); + CFRelease(publicKey); + return data; +} + +@interface FaceclawSshSigner : NSObject +@end + +@implementation FaceclawSshSigner { + SecKeyRef _key; + NSData *_publicKey; +} +- (instancetype)initWithKey:(SecKeyRef)key { + if ((self = [super init])) { + _key = (SecKeyRef)CFRetain(key); + _publicKey = PublicKeyData(key); + } + return self; +} +- (void)dealloc { + if (_key) CFRelease(_key); +} +- (NSData *)publicKey { + return _publicKey; +} +- (NSData *)sign:(NSData *)data error:(NSError **)error { + CFErrorRef cfError = NULL; + NSData *signature = (__bridge_transfer NSData *)SecKeyCreateSignature( + _key, kSecKeyAlgorithmECDSASignatureMessageX962SHA256, (__bridge CFDataRef)(data ?: [NSData data]), &cfError); + if (!signature) { + if (error) *error = (__bridge_transfer NSError *)cfError; + else if (cfError) CFRelease(cfError); + } + return signature; +} +@end + +#pragma mark - Channel + +@interface FaceclawSshEventQueue : NSObject +@property(nonatomic, strong) NSMutableArray *events; +@property(nonatomic) BOOL ended; +@end + +@implementation FaceclawSshEventQueue +- (instancetype)init { + if ((self = [super init])) _events = [NSMutableArray array]; + return self; +} +- (void)onEvent:(NSString *)kind payload:(NSString *)payload { + @synchronized(self) { + if (_ended) return; + [_events addObject:@{@"kind": kind ?: @"", @"payload": payload ?: @""}]; + } +} +@end + +@implementation FaceclawSshChannel { + FaceclawsshChannel *_channel; + FaceclawSshEventQueue *_queue; +} + +- (instancetype)initWithSpec:(NSString *)spec useKey:(BOOL)useKey { + if ((self = [super init])) { + _queue = [FaceclawSshEventQueue new]; + FaceclawSshSigner *signer = nil; + if (useKey) { + SecKeyRef key = CopyOrCreateClientKey(); + if (key) { + signer = [[FaceclawSshSigner alloc] initWithKey:key]; + CFRelease(key); + } + } + _channel = FaceclawsshOpen(spec, signer, _queue); + } + return self; +} + +- (NSString *)takeEvents { + NSArray *events; + @synchronized(_queue) { + if (_queue.events.count == 0) return @"[]"; + events = [_queue.events copy]; + [_queue.events removeAllObjects]; + } + NSData *data = [NSJSONSerialization dataWithJSONObject:events options:0 error:nil]; + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]"; +} + +- (BOOL)send:(NSString *)text { + return [_channel send:text]; +} + +- (void)close { + @synchronized(_queue) { + _queue.ended = YES; + [_queue.events removeAllObjects]; + } + [_channel close]; +} +@end + +#pragma mark - Keys and secrets + +@implementation FaceclawSshKeys + ++ (NSString *)authorizedKeyWithComment:(NSString *)comment { + SecKeyRef key = CopyOrCreateClientKey(); + if (!key) return nil; + NSData *publicKey = PublicKeyData(key); + CFRelease(key); + NSError *error = nil; + NSString *line = FaceclawsshAuthorizedKey(publicKey, comment ?: @"", &error); + if (error) NSLog(@"FaceclawSsh: authorizedKey failed: %@", error); + return line; +} + ++ (NSString *)fingerprint { + SecKeyRef key = CopyOrCreateClientKey(); + if (!key) return nil; + NSData *publicKey = PublicKeyData(key); + CFRelease(key); + NSError *error = nil; + NSString *fingerprint = FaceclawsshFingerprint(publicKey, &error); + if (error) NSLog(@"FaceclawSsh: fingerprint failed: %@", error); + return fingerprint; +} + ++ (NSDictionary *)secretQuery:(NSString *)secretId { + return @{ + (id)kSecClass: (id)kSecClassGenericPassword, + (id)kSecAttrService: kSecretService, + (id)kSecAttrAccount: secretId ?: @"", + }; +} + ++ (void)setSecret:(NSString *)value forId:(NSString *)secretId { + NSDictionary *query = [self secretQuery:secretId]; + SecItemDelete((__bridge CFDictionaryRef)query); + NSMutableDictionary *item = [query mutableCopy]; + item[(id)kSecValueData] = [(value ?: @"") dataUsingEncoding:NSUTF8StringEncoding]; + item[(id)kSecAttrAccessible] = (id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly; + OSStatus status = SecItemAdd((__bridge CFDictionaryRef)item, NULL); + if (status != errSecSuccess) NSLog(@"FaceclawSsh: saving secret failed: %d", (int)status); +} + ++ (NSString *)secretForId:(NSString *)secretId { + NSMutableDictionary *query = [[self secretQuery:secretId] mutableCopy]; + query[(id)kSecReturnData] = @YES; + query[(id)kSecMatchLimit] = (id)kSecMatchLimitOne; + CFTypeRef result = NULL; + if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &result) != errSecSuccess) return @""; + NSData *data = (__bridge_transfer NSData *)result; + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @""; +} + ++ (void)removeSecretForId:(NSString *)secretId { + SecItemDelete((__bridge CFDictionaryRef)[self secretQuery:secretId]); +} + ++ (void)copyToClipboard:(NSString *)text { + dispatch_async(dispatch_get_main_queue(), ^{ + UIPasteboard.generalPasteboard.string = text ?: @""; + }); +} +@end diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap index 9bc01ad2..41e90570 100644 --- a/App_Resources/iOS/src/module.modulemap +++ b/App_Resources/iOS/src/module.modulemap @@ -2,6 +2,7 @@ module FaceclawGraphics { header "FaceclawGraphics.h" header "FaceclawFontRenderer.h" header "FaceclawSocket.h" + header "FaceclawSsh.h" header "FaceclawLc3Decoder.h" header "FaceclawSpeech.h" header "FaceclawLocation.h" diff --git a/README.md b/README.md index fc17edc5..fd27e4de 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,10 @@ import it into the new version (see [config transfer](scripts/config-transfer.md * **Integration with Android notifications**: shows the same notification icons your phone does, and popups when notifications arrive. * **Terminal mirroring.** Mirror terminal apps such as Claude Code or Codex - CLI with [g2mirror](https://github.com/jimrandomh/g2mirror), view them on - the glasses, and send them inputs with the voice assistant. + CLI with [g2mirror](https://github.com/jimrandomh/g2mirror), or attach to + tmux sessions over SSH (no server-side setup beyond tmux 3.2+), view them + on the glasses, and send them inputs with the voice assistant. g2mirror + can also be tunnelled over SSH instead of a VPN. * **Media player controls** including playlist and media library navigation, compatible with most Android media players. * **Turn-by-turn directions** (requires a Mapbox API token). diff --git a/app/apps/app-definition.ts b/app/apps/app-definition.ts index a51adf4f..88c8a9c8 100644 --- a/app/apps/app-definition.ts +++ b/app/apps/app-definition.ts @@ -55,8 +55,10 @@ export type GlanceboardProvider = { isEnabled: () => boolean; /** Whether a single tap shows the board (for tapTimeoutMs). */ showOnTap: () => boolean; - /** How long a tap or head-tilt keeps the board up. */ + /** How long a tap keeps the board up (and a head-tilt, when its head-down is unlikely to come). */ tapTimeoutMs: () => number; + /** How long a head-tilt showing may last without a head-down (null = no limit). */ + tiltTimeoutMs: () => number | null; /** 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. */ @@ -187,6 +189,8 @@ export async function launchWorkerAppWindow( matchExistingBy?: "appId" | "windowId"; /** Deliver watch swipes raw instead of the fallback (see WorkerWindowSpec). */ acceptsDirectional?: boolean; + /** Deliver hardware-keyboard keys as keys (see WorkerWindowSpec). */ + acceptsKeys?: boolean; }, ): Promise { const existing = shell @@ -207,6 +211,7 @@ export async function launchWorkerAppWindow( icon: options.icon, focus: true, acceptsDirectional: options.acceptsDirectional, + acceptsKeys: options.acceptsKeys, }); ctx.appendLog(`launched ${options.windowId}`); } diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index 3580bee9..0ada183f 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -312,6 +312,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { } case "navigation-sensors": case "text-input": + case "key": case "tool-call": break; } diff --git a/app/apps/calendar/calendar.ts b/app/apps/calendar/calendar.ts index 5d112ab4..c2a5e976 100644 --- a/app/apps/calendar/calendar.ts +++ b/app/apps/calendar/calendar.ts @@ -9,6 +9,7 @@ import { hasCalendarPermission } from "../../native/calendar-permissions"; import { type Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; import type { Viewport } from "../../ui/viewport"; +import { listingDayMs, spanLabel, spansDays } from "./event-days"; /** How far a row's text sits inside its selection box; the title lines up with it. */ const ROW_TEXT_INSET = 10; @@ -143,14 +144,20 @@ export class CalendarLayer implements Layer { function buildEventRows(font: UiFont, events: CalendarEvent[]): EventRow[] { const rows: EventRow[] = []; let previousDayKey = ""; + const nowMs = Date.now(); for (const event of events) { - const dayKey = dayKeyOf(event.startMs); - const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(event.startMs); + // An event still going since an earlier day is listed under today, not its first day. + const listedMs = listingDayMs(event, nowMs); + const dayKey = dayKeyOf(listedMs); + const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(listedMs); previousDayKey = dayKey; const lines: string[] = []; const timeLabel = event.allDay ? "All day" : formatEventTime(event.startMs); lines.push(`${timeLabel} ${event.title || "(untitled)"}`); + if (spansDays(event)) { + lines.push(spanLabel(event)); + } if (event.location) { lines.push(event.location); } diff --git a/app/apps/calendar/event-days.ts b/app/apps/calendar/event-days.ts new file mode 100644 index 00000000..68c8ef66 --- /dev/null +++ b/app/apps/calendar/event-days.ts @@ -0,0 +1,43 @@ +/** + * Which day an event is listed under, and how a multi-day span reads. Pure (no + * native or UI imports) so it can be unit-tested. + */ + +const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; +const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + +export type DayEvent = { startMs: number; endMs: number; allDay: boolean }; + +function midnight(timestampMs: number): number { + const date = new Date(timestampMs); + return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime(); +} + +/** The last moment the event covers: all-day ends are exclusive (the midnight after its last day). */ +function lastMomentMs(event: DayEvent): number { + return event.allDay ? Math.max(event.startMs, event.endMs - 1) : Math.max(event.startMs, event.endMs); +} + +/** + * The day to list an event under: its start, or today for one that began on an + * earlier day and is still going. Without this, the third day of a three-day + * event shows under its first day and never under today. + */ +export function listingDayMs(event: DayEvent, nowMs: number): number { + return Math.max(midnight(event.startMs), Math.min(midnight(nowMs), midnight(lastMomentMs(event)))); +} + +/** Whether the event covers more than one calendar day. */ +export function spansDays(event: DayEvent): boolean { + return midnight(lastMomentMs(event)) > midnight(event.startMs); +} + +/** "Mon 16 – Wed 18 Nov", or with both months when they differ ("Mon 30 Nov – Tue 1 Dec"). */ +export function spanLabel(event: DayEvent): string { + const start = new Date(event.startMs); + const last = new Date(lastMomentMs(event)); + const day = (date: Date) => `${WEEKDAYS[date.getDay()]} ${date.getDate()}`; + return start.getMonth() === last.getMonth() && start.getFullYear() === last.getFullYear() + ? `${day(start)} – ${day(last)} ${MONTHS[last.getMonth()]}` + : `${day(start)} ${MONTHS[start.getMonth()]} – ${day(last)} ${MONTHS[last.getMonth()]}`; +} diff --git a/app/apps/developer/accelerometer-demo.ts b/app/apps/developer/accelerometer-demo.ts index 2e39c14d..9100dfef 100644 --- a/app/apps/developer/accelerometer-demo.ts +++ b/app/apps/developer/accelerometer-demo.ts @@ -1,7 +1,7 @@ import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { clamp } from "../../util/numeric-util"; -import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu"; +import { addImuListener, imuPitchDegrees, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu"; import { type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; import { shell } from "../../ui/shell/shell"; @@ -23,7 +23,8 @@ const BAR_RIGHT_INSET = 4; /** * Accelerometer demo: turns the IMU stream on while this page is visible (the * Developer window is foregrounded and the screen is on), off otherwise, and - * shows the latest x/y/z reading with a simple per-axis bar. + * shows the latest x/y/z reading (Faceclaw's frame, imu-types.ts) with a + * simple per-axis bar, and the head's pitch. * * The IMU stream is experimental — the firmware may never populate it — so the * page also reports how many samples have arrived. @@ -92,7 +93,8 @@ export class AccelerometerDemoLayer implements Layer { const bodyX = padding.left + BODY_INDENT; image.drawText(small, padding.left, top, "Accelerometer", 220); - const status = this.enabled ? "IMU on" : "IMU off"; + const pitch = this.reading ? `pitch ${Math.round(imuPitchDegrees(this.reading))}° ` : ""; + const status = pitch + (this.enabled ? "IMU on" : "IMU off"); image.drawText(small, right - small.measureText(status), top, status, 150); if (this.reading) { diff --git a/app/apps/developer/developer-app.ts b/app/apps/developer/developer-app.ts index 276990b3..02615a0a 100644 --- a/app/apps/developer/developer-app.ts +++ b/app/apps/developer/developer-app.ts @@ -6,6 +6,7 @@ import { BuzzerDemoLayer } from "./buzzer-demo"; import { AccelerometerDemoLayer } from "./accelerometer-demo"; import { BandwidthBenchmarkLayer } from "./bandwidth-benchmark"; import { LightSensorDemoLayer } from "./light-sensor-demo"; +import { MouseTestLayer } from "./mouse-test"; import { MicrophonesMonitorLayer } from "./microphones-monitor"; import { ResourceUsageLayer } from "./resource-usage"; import { RingStatusLayer } from "./ring-status"; @@ -70,6 +71,13 @@ function debugTestsMenu(openInputEvents: OpenPage, openMicrophones: OpenPage): M label: "Microphones", onSelect: openMicrophones, }, + { + // The touchpad or mouse of a keyboard paired through the glasses. + label: "Mouse cursor", + onSelect: (ctx) => { + ctx.stack.push(new MouseTestLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); + }, + }, { label: "BLE bandwidth", onSelect: (ctx) => { diff --git a/app/apps/developer/mouse-test.ts b/app/apps/developer/mouse-test.ts new file mode 100644 index 00000000..a2233036 --- /dev/null +++ b/app/apps/developer/mouse-test.ts @@ -0,0 +1,206 @@ +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage } from "../../graphics/image"; +import type { HidPointerSample } from "../../g2/keyboard/hid"; +import { keyboardService } from "../../g2/keyboard/service"; +import { type InputEvent } from "../../ui/gestures"; +import { Layer, type LayerContext } from "../../ui/layers"; +import { shell } from "../../ui/shell/shell"; +import { clamp } from "../../util/numeric-util"; + +// How often to reconcile holding the pointer with window visibility. +const RECONCILE_INTERVAL_MS = 400; +/** A touchpad reports a hundred times a second; the page repaints at most this often. */ +const RENDER_INTERVAL_MS = 33; +/** Cursor pixels per count the touchpad reports. */ +const POINTER_GAIN = 1; +/** Left-click marks kept on the page. */ +const MAX_MARKS = 12; + +/** The arrow, tip at its top left: '#' is the outline, '.' the fill. */ +const CURSOR = [ + "#", + "##", + "#.#", + "#..#", + "#...#", + "#....#", + "#.....#", + "#......#", + "#.......#", + "#........#", + "#.....#####", + "#..#..#", + "#.# #..#", + "## #..#", + "# #..#", + " #..#", + " ##", +]; + +/** + * Mouse cursor test: moves an arrow with the touchpad or mouse of the + * keyboard paired through the glasses (app/g2/keyboard/), and shows its + * buttons and wheel. The main UI has no cursor; this is where one is tried + * out. + * + * While the page is visible it takes the pointer, so the buttons and wheel + * show here instead of acting as select, back and scroll. A left click + * leaves a mark where the arrow is and a right click clears them. Leave + * with Esc, or a double tap on the ring or a temple. + */ +export class MouseTestLayer implements Layer { + private x = -1; + private y = -1; + private size = { width: 0, height: 0 }; + private buttons = 0; + private wheel = 0; + private marks: Array<{ x: number; y: number }> = []; + private samples = 0; + private rate = 0; + private rateWindowStart = Date.now(); + private removed = false; + private release: (() => void) | null = null; + private reconcileTimer: ReturnType | null = null; + private renderTimer: ReturnType | null = null; + + constructor( + private readonly windowId: string, + private readonly requestRender: () => void, + ) { + this.reconcileTimer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS); + this.reconcile(); + } + + /** Hold the pointer only while the page can be seen; elsewhere its buttons go back to being keys. */ + private reconcile(): void { + if (this.removed) return; + const visible = shell.isWindowVisible(this.windowId); + if (visible && !this.release) { + this.release = keyboardService.takePointer((sample) => this.onSample(sample)); + } else if (!visible && this.release) { + this.release(); + this.release = null; + this.buttons = 0; + } + // The rate reads zero once the pointer has been still for a second. + const now = Date.now(); + if (now - this.rateWindowStart >= 1000) { + const rate = this.samples; + this.samples = 0; + this.rateWindowStart = now; + if (rate !== this.rate) { + this.rate = rate; + this.scheduleRender(); + } + } + } + + private onSample(sample: HidPointerSample): void { + if (this.removed) return; + this.samples++; + const { width, height } = this.size; + if (width > 0 && height > 0) { + if (this.x < 0) { + this.x = width / 2; + this.y = height / 2; + } + this.x = clamp(this.x + sample.dx * POINTER_GAIN, 0, width - 1); + this.y = clamp(this.y + sample.dy * POINTER_GAIN, 0, height - 1); + } + const pressed = sample.buttons & ~this.buttons; + this.buttons = sample.buttons; + this.wheel += sample.wheel; + if (pressed & 1 && this.x >= 0) { + this.marks.push({ x: Math.round(this.x), y: Math.round(this.y) }); + if (this.marks.length > MAX_MARKS) this.marks.shift(); + } + if (pressed & 2) this.marks = []; + this.scheduleRender(); + } + + private scheduleRender(): void { + if (this.renderTimer !== null || this.removed) return; + this.renderTimer = setTimeout(() => { + this.renderTimer = null; + if (!this.removed) this.requestRender(); + }, RENDER_INTERVAL_MS); + } + + onRemoved(): void { + this.removed = true; + if (this.reconcileTimer !== null) clearInterval(this.reconcileTimer); + this.reconcileTimer = null; + if (this.renderTimer !== null) clearTimeout(this.renderTimer); + this.renderTimer = null; + this.release?.(); + this.release = null; + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const { width, height, padding } = ctx.stack.getBaseSize(); + this.size = { width, height }; + const image = new GrayImage(width, height, 0); + const left = padding.left; + const right = width - padding.right; + const top = padding.top; + const line = font.lineHeight + 4; + + image.drawText(font, left, top, "Mouse cursor", 220); + const snapshot = keyboardService.snapshot(); + const problem = snapshot.state !== "connected" + ? "No keyboard is connected." + : !snapshot.pointer ? "This keyboard has no touchpad or mouse buttons." : null; + if (problem) { + image.drawText(font, left, top + 2 * line, problem, 190); + image.drawText(font, left, top + 3 * line, "Pair one under Settings > Keyboard.", 120); + return image; + } + + const position = this.x < 0 ? "move the pointer" : `${Math.round(this.x)}, ${Math.round(this.y)}`; + image.drawText(font, right - font.measureText(position), top, position, 150); + // Buttons: a box each, filled while held. + let x = left; + const boxY = top + line; + for (const [index, label] of ["L", "M", "R"].entries()) { + // HID numbers them left, right, middle; they sit left, middle, right. + const held = !!(this.buttons & [1, 4, 2][index]!); + const boxW = font.measureText(label) + 10; + if (held) image.fillRect(x, boxY, boxW, font.lineHeight + 2, 200); + else image.drawRect(x, boxY, boxW, font.lineHeight + 2, 90); + image.drawText(font, x + 5, boxY + 1, label, held ? 1 : 170); + x += boxW + 4; + } + image.drawText(font, x + 8, boxY + 1, `wheel ${this.wheel}`, 150); + const rate = `${this.rate}/s`; + image.drawText(font, right - font.measureText(rate), boxY + 1, rate, 110); + const hint = "Left click marks, right click clears. Esc or double tap: back"; + image.drawText(font, left, height - padding.bottom - font.lineHeight, hint, 90); + + for (const mark of this.marks) { + image.drawLine(mark.x - 4, mark.y, mark.x + 4, mark.y, 140); + image.drawLine(mark.x, mark.y - 4, mark.x, mark.y + 4, 140); + } + if (this.x >= 0) drawCursor(image, Math.round(this.x), Math.round(this.y)); + return image; + } + + async handleInput(event: InputEvent, ctx: LayerContext): Promise { + if (event.type === "double-click") { + ctx.stack.pop(); + } + } +} + +function drawCursor(image: GrayImage, x: number, y: number): void { + for (const [row, pixels] of CURSOR.entries()) { + for (let col = 0; col < pixels.length; col++) { + const pixel = pixels[col]; + if (pixel === " ") continue; + const px = x + col; + const py = y + row; + if (px < 0 || py < 0 || px >= image.width || py >= image.height) continue; + image.setPixel(px, py, pixel === "#" ? 255 : 70); + } + } +} diff --git a/app/apps/files/files-app.ts b/app/apps/files/files-app.ts index d8872e58..83abb779 100644 --- a/app/apps/files/files-app.ts +++ b/app/apps/files/files-app.ts @@ -2,6 +2,7 @@ import { readTextFile, type DirectoryEntry } from "../../native/file-access"; import { isFontFile } from "../../native/font-files"; import { installFontFile, isFontInstalled } from "../../graphics/installed-fonts"; import { isDecodableImageFile } from "../../native/image-files"; +import { isMicRecordingFile } from "../../native/mic-recording"; import { isPlayableVideoFile } from "../../native/video-player"; import { readEvenHubPackageManifest } from "../evenhub/installed-apps"; import { EvenHubPermissionDialogLayer } from "../evenhub/permission-dialog"; @@ -38,6 +39,8 @@ export type FilesAppOptions = InProcessAppOptions & { openFontWindow: (title: string, path: string) => void; /** Open a video file's player (settings panel first) as its own shell window. */ openVideoWindow: (title: string, path: string) => void; + /** Replay a recording (.mka) through Transcribe, in a window of its own. */ + openRecordingInTranscribe: (title: string, path: string) => void; }; /** @@ -50,7 +53,7 @@ export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow const browser = new FileBrowserLayer({ isSupportedFile: (name) => TEXT_FILE.test(name) || isFontFile(name) || EHPK_FILE.test(name) || - (!global.isIOS && (isDecodableImageFile(name) || isPlayableVideoFile(name))), + (!global.isIOS && (isDecodableImageFile(name) || isPlayableVideoFile(name) || isMicRecordingFile(name))), // The browser handles double-click itself (up a level), so it is not // wrapped in YieldAtRootLayer; it yields explicitly from the top level. onLeave: () => shell.yieldFocusToSidebar(), @@ -235,6 +238,17 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI }, ]; } + if (isMicRecordingFile(entry.name)) { + return [ + { + label: "Open in Transcribe", + onSelect: (ctx) => { + ctx.stack.pop(); + options.openRecordingInTranscribe(entry.name, entry.path); + }, + }, + ]; + } if (isPlayableVideoFile(entry.name)) { return [ { diff --git a/app/apps/files/index.ts b/app/apps/files/index.ts index 65f9430f..984b51cc 100644 --- a/app/apps/files/index.ts +++ b/app/apps/files/index.ts @@ -14,6 +14,7 @@ import { FILES_WINDOW_ID, } from "./files-app"; import { createVideoPlayerWindow } from "./video-window"; +import { openRecordingInTranscribe } from "../transcribe"; // Document windows are closeable and non-singleton; the serial keeps their // windowIds unique. @@ -91,6 +92,9 @@ const filesApp: AppDefinition = { }, openFontWindow: (title, path) => openFontDocumentWindow(ctx, title, path), openVideoWindow: (title, path) => openVideoWindow(ctx, title, path), + openRecordingInTranscribe: (title, path) => { + openRecordingInTranscribe(ctx, path, title); + }, }), ), // A document arriving via Android's Share intent opens as its own window. diff --git a/app/apps/flappy/flappy-app.worker.ts b/app/apps/flappy/flappy-app.worker.ts index 5f95d777..6dd2f7d6 100644 --- a/app/apps/flappy/flappy-app.worker.ts +++ b/app/apps/flappy/flappy-app.worker.ts @@ -305,6 +305,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { break; case "navigation-sensors": case "text-input": + case "key": case "tool-call": break; } diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index 1bdee5a8..5805254d 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -236,6 +236,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { case "navigation-sensors": case "screen": case "text-input": + case "key": case "tool-call": break; } diff --git a/app/apps/glanceboard/glanceboard-app.ts b/app/apps/glanceboard/glanceboard-app.ts index 9151d416..40322724 100644 --- a/app/apps/glanceboard/glanceboard-app.ts +++ b/app/apps/glanceboard/glanceboard-app.ts @@ -5,7 +5,9 @@ import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from import { uiFontOverridePickerMenuItem } from "../../ui/font-picker"; import { type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; -import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu"; +import { drawSelectionHighlight, isMenuItemDisabled, menuHeadingItem, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu"; +import { liveHeadPitchFooter } from "../../g2/head-angle"; +import { headUpControlsAvailable } from "../../native/glasses-controls"; import { Menu } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; @@ -20,7 +22,9 @@ import { glanceLayout, glanceLayoutSetting, glanceShowLinesSetting, + glanceAngleSetting, glanceShowOnHeadTiltSetting, + glanceTiltTimeoutSetting, glanceShowOnLongPressSetting, glanceSlotChoiceLabel, glanceSlotSettings, @@ -319,25 +323,45 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void { } /** - * Settings: layout, sleep gestures, duration, slot lines, widget font, and - * stereo depth, then a row per widget on the board for its own settings. + * Settings, in four sections: Layout (the slot grid), Trigger (the sleep + * gestures that show the board, and for how long), Display (slot lines, + * widget font, stereo depth) and Cards (a row per widget on the board, for + * its own settings; left out while the board is empty). */ function openGlanceSettings(ctx: LayerContext): void { - const items = (): MenuItem[] => [ - // The layout decides which slots (and so which widgets) are on the board. - enumSettingMenuItem(glanceLayoutSetting, { onChange: () => layer.setItems(items()) }), - enumSettingMenuItem(glanceTapDurationSetting), - toggleSettingMenuItem(glanceShowOnLongPressSetting), - toggleSettingMenuItem(glanceShowOnHeadTiltSetting), - toggleSettingMenuItem(glanceShowLinesSetting), - uiFontOverridePickerMenuItem(glanceFont, { - title: "Glanceboard font", - description: - "Typeface for the Glanceboard's widgets. Same as UI font follows the Font setting in Settings > Customization.", - }), - enumSettingMenuItem(glanceDepthSetting), - ...widgetSettingsMenuItems(), - ]; + const items = (): MenuItem[] => { + const cards = widgetSettingsMenuItems(); + return [ + menuHeadingItem("Layout"), + // The layout decides which slots (and so which widgets) are on the board. + enumSettingMenuItem(glanceLayoutSetting, { onChange: () => layer.setItems(items()) }), + + menuHeadingItem("Trigger"), + enumSettingMenuItem(glanceTapDurationSetting), + toggleSettingMenuItem(glanceShowOnLongPressSetting), + toggleSettingMenuItem(glanceShowOnHeadTiltSetting), + enumSettingMenuItem(glanceTiltTimeoutSetting), + // Only Android can change the glasses' head-up setting (glasses-controls.ts). + // The controller's settings listener sends a new angle to the glasses; the + // picker shows the head's current angle to choose by. + ...(headUpControlsAvailable() + ? [enumSettingMenuItem(glanceAngleSetting, { + pickerFooter: liveHeadPitchFooter(() => shell.isWindowVisible(GLANCEBOARD_WINDOW_ID)), + })] + : []), + + menuHeadingItem("Display"), + toggleSettingMenuItem(glanceShowLinesSetting), + uiFontOverridePickerMenuItem(glanceFont, { + title: "Glanceboard font", + description: + "Typeface for the Glanceboard's widgets. Same as UI font follows the Font setting in Settings > Customization.", + }), + enumSettingMenuItem(glanceDepthSetting), + + ...(cards.length ? [menuHeadingItem("Cards"), ...cards] : []), + ]; + }; const layer = new MenuLayer("Glanceboard settings", items(), PAGE_MENU_LAYOUT); ctx.stack.push(layer); } diff --git a/app/apps/glanceboard/glanceboard-settings.ts b/app/apps/glanceboard/glanceboard-settings.ts index 4e6ba8e6..be567851 100644 --- a/app/apps/glanceboard/glanceboard-settings.ts +++ b/app/apps/glanceboard/glanceboard-settings.ts @@ -49,8 +49,9 @@ export function glanceLayout(): GlanceLayout { /** * 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. + * behaviour: a tap or hold while asleep does nothing, a head-tilt wakes the + * regular UI, double-tap wakes; and the head-up angle goes back to the glasses' + * own. */ export const glanceboardEnabledSetting = new ConfigSettingBoolean({ id: "glanceboard-enabled", @@ -85,9 +86,10 @@ function formatTapDuration(value: GlanceTapDuration): string { } /** - * 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. + * How long a tap keeps the board up; each further tap restarts it. "off" + * means a single tap does not show the board at all. A head-tilt showing + * lasts until the head comes back down, falling back to this duration (the + * default one when "off") where the head-down is unlikely to arrive. */ export const glanceTapDurationSetting = new ConfigSettingEnum({ id: "glanceboard-tap-duration", @@ -97,7 +99,7 @@ export const glanceTapDurationSetting = new ConfigSettingEnum values: TAP_DURATION_VALUES, formatValue: formatTapDuration, description: - "How long a single tap (or a head-tilt) keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.", + "How long a single tap keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.", }); /** Whether a single tap while asleep shows the board. */ @@ -119,15 +121,71 @@ export const glanceShowOnLongPressSetting = new ConfigSettingBoolean({ 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. */ +/** The head-tilt wake shows the board (until the head comes back down) 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.", + description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI. It stays up until you look back down.", }); +export type GlanceTiltTimeout = "60s" | "30s" | "15s" | "10s" | "never"; + +/** + * How long a head-tilt showing may last if the head-down never comes (the + * board normally hides when the head comes back down). "never" removes the + * limit; a double-tap still dismisses the board. + */ +export const glanceTiltTimeoutSetting = new ConfigSettingEnum({ + id: "glanceboard-head-tilt-timeout", + label: "Head tilt timeout", + storageKey: "glanceboard.headTiltTimeout", + defaultValue: "60s", + values: ["60s", "30s", "15s", "10s", "never"], + formatValue: (value) => (value === "never" ? "Never" : value.replace("s", " s")), + description: + "The longest a head-tilt keeps the Glanceboard up when looking back down is not detected. Never: it stays until you look down or double-tap. " + + "When looking down is unlikely to register (a low Head tilt angle, or the display was suspended), the tap duration applies if shorter.", +}); + +/** The Head tilt timeout in ms, or null for none. */ +export function glanceTiltTimeoutMs(): number | null { + const value = glanceTiltTimeoutSetting.get(); + return value === "never" ? null : Number.parseInt(value, 10) * 1000; +} + +const GLANCE_ANGLE_VALUES = ["glasses", "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60"] as const; +export type GlanceAngle = (typeof GLANCE_ANGLE_VALUES)[number]; + +/** + * How far the head must tilt up for a head-tilt wake, as the glasses' own + * pitch reading (degrees). "glasses" keeps the glasses' own setting (from the + * Even app), writing back their original values if Faceclaw changed them. A + * number is written to the glasses' head-up setting on every connection while + * the Glanceboard is enabled (head-angle.ts). Android only. + */ +export const glanceAngleSetting = new ConfigSettingEnum({ + id: "glanceboard-head-tilt-angle", + label: "Head tilt angle", + storageKey: "glanceboard.headTiltAngle", + defaultValue: "glasses", + values: GLANCE_ANGLE_VALUES, + formatValue: (value) => (value === "glasses" ? "Glasses default" : `${value}°`), + description: + "How far you tilt your head up to wake the display; the list shows your head's angle now. Glasses default uses the glasses' own setting from the Even app. " + + "The board hides when your head drops 20° below this angle; under 25°, looking straight ahead may not count, so it hides after the tap duration instead.", +}); + +/** + * The trigger Faceclaw should maintain, in degrees: the Head tilt angle while + * the Glanceboard is enabled, else 0 (the glasses' own setting). + */ +export function glanceAngleTriggerDegrees(): number { + const value = glanceAngleSetting.get(); + return value === "glasses" || !glanceboardEnabledSetting.get() ? 0 : Number(value); +} + const DEPTH_VALUES = ["-64", "-48", "-32", "-16", "0", "16", "32", "48", "64"] as const; export type GlanceDepth = (typeof DEPTH_VALUES)[number]; diff --git a/app/apps/glanceboard/index.ts b/app/apps/glanceboard/index.ts index 65affdfa..2042cce1 100644 --- a/app/apps/glanceboard/index.ts +++ b/app/apps/glanceboard/index.ts @@ -9,6 +9,7 @@ import { glanceShowOnLongPressSetting, glanceShowOnTap, glanceTapTimeoutMs, + glanceTiltTimeoutMs, } from "./glanceboard-settings"; const glanceboardApp: AppDefinition = { @@ -24,6 +25,7 @@ const glanceboardApp: AppDefinition = { isEnabled: () => glanceboardEnabledSetting.get(), showOnTap: glanceShowOnTap, tapTimeoutMs: glanceTapTimeoutMs, + tiltTimeoutMs: glanceTiltTimeoutMs, showOnLongPress: () => glanceShowOnLongPressSetting.get(), showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(), depth: glanceDepth, diff --git a/app/apps/glanceboard/widgets/music-widget.ts b/app/apps/glanceboard/widgets/music-widget.ts index ecb23a0c..3b53b15d 100644 --- a/app/apps/glanceboard/widgets/music-widget.ts +++ b/app/apps/glanceboard/widgets/music-widget.ts @@ -50,16 +50,26 @@ export const musicShowAlbumArtSetting = new ConfigSettingBoolean({ "Show the album art beside the track details. When off, the title, artist, album and progress bar use the full width of the card.", }); +export const musicShowIdleNoteSetting = new ConfigSettingBoolean({ + id: "glanceboard-music-show-idle-note", + label: "Show note when idle", + storageKey: "glanceboard.music.showIdleNote", + defaultValue: true, + description: "Show a dim music note on the card while nothing is playing. When off, the card is left blank.", +}); + /** The music card's submenu in Glanceboard > Settings. */ export function musicSettingsMenuItems(): MenuItem[] { - return [toggleSettingMenuItem(musicShowAlbumArtSetting)]; + return [toggleSettingMenuItem(musicShowAlbumArtSetting), toggleSettingMenuItem(musicShowIdleNoteSetting)]; } /** * Now playing: album art (unless turned off), title/artist/album and a * progress bar for the active media session (Apple Music on iOS), 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. + * app's header at card size. With nothing playing the card holds a dim note, + * or nothing with that turned off. 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(); @@ -108,8 +118,8 @@ export class MusicWidget implements GlanceWidget { image.drawText(small, PAD, PAD + small.lineHeight + 6, mediaControllerBridge.accessLabel, 140); return; } - // Nothing playing: a dim note, centred, and nothing else. - const note = idleNoteImage(); + // Nothing playing: a dim note, centred, and nothing else; or a blank card. + const note = musicShowIdleNoteSetting.get() ? idleNoteImage() : null; if (note) image.drawImage(note, ((image.width - note.width) / 2) | 0, ((image.height - note.height) / 2) | 0); return; } diff --git a/app/apps/microphones/mic-control.ts b/app/apps/microphones/mic-control.ts index 6f62b3ad..ddc4a75d 100644 --- a/app/apps/microphones/mic-control.ts +++ b/app/apps/microphones/mic-control.ts @@ -56,6 +56,27 @@ export function saveMicConfig(config: MicConfig): void { setStringSetting(CONFIG_SETTING_KEY, JSON.stringify(config)); } +/** + * The configuration a capture of the temples' own microphones arms them + * with: `mics` of 2 is one microphone per temple (the front one), 4 is both + * of each. The front end and direction detection follow Microphone setup + * (the Microphones app), except that both of a temple's microphones only + * come through the codec front end. Always raw PCM16 at 16 kHz, which is + * what the firmware streams and transcription takes. + */ +export function micArrayCaptureConfig(mics: 2 | 4): MicConfig { + const base = loadMicConfig(); + return { + ...base, + source: mics === 4 ? "codec" : base.source, + channelMask: mics === 4 ? 0b11 : 0b01, + codec: "raw", + format: "pcm16", + sampleRateHz: 16_000, + armHardware: true, + }; +} + class MicArrayController { private readonly statusListeners = new Set<(statuses: MicArrayStatuses) => void>(); private statuses: MicArrayStatuses = { left: null, right: null }; diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index 739b71a1..89629bfe 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -272,6 +272,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { } case "navigation-sensors": case "text-input": + case "key": case "tool-call": break; } diff --git a/app/apps/navigate/navigate-app.worker.ts b/app/apps/navigate/navigate-app.worker.ts index 9bd37f2d..9adbc82a 100644 --- a/app/apps/navigate/navigate-app.worker.ts +++ b/app/apps/navigate/navigate-app.worker.ts @@ -370,6 +370,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`); handleInput(window, message.event as InputEvent, message.frameId); break; + case "key": + // Only windows opened with acceptsKeys are sent keys. + break; case "text-input": if (editing) { // Voice input as an alternative to the phone keyboard. diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index 3a844d17..aba49b78 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -433,6 +433,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { } case "navigation-sensors": case "text-input": + case "key": case "tool-call": break; } diff --git a/app/apps/roam/roam-app.worker.ts b/app/apps/roam/roam-app.worker.ts index b348fba6..b3564c7d 100644 --- a/app/apps/roam/roam-app.worker.ts +++ b/app/apps/roam/roam-app.worker.ts @@ -227,6 +227,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`); handleInput(window, message.event as InputEvent, message.frameId); break; + case "key": + // Only windows opened with acceptsKeys are sent keys. + break; case "text-input": if (editingSetting) { // Voice input as an alternative to the phone keyboard. diff --git a/app/apps/t3code/t3code-app.worker.ts b/app/apps/t3code/t3code-app.worker.ts index a0f998d3..305c0332 100644 --- a/app/apps/t3code/t3code-app.worker.ts +++ b/app/apps/t3code/t3code-app.worker.ts @@ -424,6 +424,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`); handleInput(window, message.event as InputEvent, message.frameId); break; + case "key": + // Only windows opened with acceptsKeys are sent keys. + break; case "text-input": if (window && window.windowId === message.windowId) handleTextInput(message.text.trim()); break; diff --git a/app/apps/terminal/connections.ts b/app/apps/terminal/connections.ts index aeb1fec8..f53cb650 100644 --- a/app/apps/terminal/connections.ts +++ b/app/apps/terminal/connections.ts @@ -1,13 +1,21 @@ import { getStringSetting, setStringSetting } from "../../native/settings-store"; /** - * Terminal-app connection registry: the g2mirror hosts the app knows about, - * stored as JSON under one settings key so every isolate sees the same list - * and change notifications ride the ordinary settings-store channel. + * Terminal-app connection registry: the hosts the app knows about, stored as + * JSON under one settings key so every isolate sees the same list and change + * notifications ride the ordinary settings-store channel. * - * A connection is entered as a g2mirror:// connection string (the same form - * g2mirror-view accepts): g2mirror://@[:port] for plain - * websockets (default port 8737), g2mirrors://... for TLS (default 443). + * A connection is entered as a connection string: + * - g2mirror://@[:port]: a g2mirror server reached directly + * (the same form g2mirror-view accepts) over a plain websocket (default + * port 8737); g2mirrors://... for TLS (default 443). + * - ssh://@[:port]: tmux on an SSH host (default port 22). + * - ssh://@[:port]?g2mirror=[@[:port]]: a g2mirror + * server tunnelled over SSH; the address after the token is where the + * server listens as seen from the SSH host (default 127.0.0.1:8737). + * SSH connections pin the host's key (hostKeys, trusted on first connection + * after the user checks its fingerprint); their passwords live in the + * platform keystore (app/native/ssh), not here. */ export const TERMINAL_CONNECTIONS_KEY = "terminal.connections"; @@ -25,6 +33,8 @@ export type TerminalConnection = { serverName: string | null; /** Auto-connect while the Terminal app is open. Toggled by Connect/Disconnect. */ enabled: boolean; + /** SSH connections: trusted host keys (SSH wire format, base64). */ + hostKeys: string[]; }; export type ParsedConnectionString = { @@ -54,8 +64,79 @@ export function parseConnectionString(url: string): ParsedConnectionString | nul return { secure, host, port, authToken }; } -/** The host[:port] part of a connection string; "" if it doesn't parse. */ +export type ParsedSshConnection = { + user: string; + host: string; + port: number; +} & ( + | { mode: "tmux" } + | { + mode: "g2mirror"; + authToken: string; + /** Where g2mirror-server listens, from the SSH host's point of view. */ + targetHost: string; + targetPort: number; + } +); + +function decodeComponent(text: string): string { + try { + return decodeURIComponent(text); + } catch { + return text; + } +} + +/** Parse an ssh://user@host[:port][?g2mirror=token[@host[:port]]] string; null if malformed. */ +export function parseSshConnectionString(url: string): ParsedSshConnection | null { + const match = /^ssh:\/\/([^@\s/?]+)@(\[[^\]\s]+\]|[^:/@\s?]+)(?::(\d+))?\/?(?:\?(.*))?$/.exec(url.trim()); + if (!match) return null; + const user = decodeComponent(match[1]!); + const host = match[2]!.replace(/^\[(.*)\]$/, "$1"); + const port = match[3] ? parseInt(match[3], 10) : 22; + if (!user || !(port >= 1 && port <= 65535)) return null; + const params = new Map(); + for (const part of (match[4] ?? "").split("&").filter(Boolean)) { + const equals = part.indexOf("="); + if (equals < 0) return null; + params.set(part.slice(0, equals), decodeComponent(part.slice(equals + 1))); + } + for (const key of params.keys()) { + if (key !== "g2mirror") return null; + } + const g2mirror = params.get("g2mirror"); + if (g2mirror === undefined) return { user, host, port, mode: "tmux" }; + // token[@host[:port]], split at the last "@" so the token may contain one. + const at = g2mirror.lastIndexOf("@"); + const authToken = at < 0 ? g2mirror : g2mirror.slice(0, at); + const target = at < 0 ? "" : g2mirror.slice(at + 1); + const targetMatch = /^(\[[^\]\s]+\]|[^:\s]*)(?::(\d+))?$/.exec(target); + if (!authToken || !targetMatch) return null; + const targetHost = targetMatch[1] || "127.0.0.1"; + const targetPort = targetMatch[2] ? parseInt(targetMatch[2], 10) : 8737; + if (!(targetPort >= 1 && targetPort <= 65535)) return null; + return { user, host, port, mode: "g2mirror", authToken, targetHost, targetPort }; +} + +export type ParsedAnyConnection = + | ({ kind: "g2mirror" } & ParsedConnectionString) + | ({ kind: "ssh" } & ParsedSshConnection); + +/** Parse any supported connection string; null if it's none of them. */ +export function parseAnyConnectionString(url: string): ParsedAnyConnection | null { + const direct = parseConnectionString(url); + if (direct) return { kind: "g2mirror", ...direct }; + const ssh = parseSshConnectionString(url); + return ssh ? { kind: "ssh", ...ssh } : null; +} + +/** The [user@]host[:port] part of a connection string (never a token); "" if it doesn't parse. */ export function connectionStringHostLabel(url: string): string { + const ssh = parseSshConnectionString(url); + if (ssh) { + const host = ssh.host.includes(":") ? `[${ssh.host}]` : ssh.host; + return `${ssh.user}@${host}${ssh.port === 22 ? "" : `:${ssh.port}`}`; + } const parsed = parseConnectionString(url); if (!parsed) return ""; const defaultPort = parsed.secure ? 443 : 8737; @@ -79,6 +160,7 @@ export function loadConnections(): TerminalConnection[] { url: String(item?.url ?? ""), serverName: typeof item?.serverName === "string" && item.serverName ? item.serverName : null, enabled: item?.enabled !== false, + hostKeys: Array.isArray(item?.hostKeys) ? item.hostKeys.filter((key: unknown) => typeof key === "string") : [], })) .filter((connection) => connection.id.length > 0 && connection.url.length > 0); } catch { diff --git a/app/apps/terminal/index.ts b/app/apps/terminal/index.ts index 9de1e7e9..e305c21e 100644 --- a/app/apps/terminal/index.ts +++ b/app/apps/terminal/index.ts @@ -14,6 +14,8 @@ const terminalApp: AppDefinition = { // Session windows stay independent: launching reopens the hub even // while terminal sessions are open. matchExistingBy: "windowId", + // The hub window can show a session too, so it takes keys like one. + acceptsKeys: true, }), }; diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index 622c3aea..051b1a0f 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -16,11 +16,17 @@ * Each view has its own websocket connection (the protocol allows one attached * session per connection) and its own xterm emulator. * - * Connections are g2mirror://@[:port] strings (g2mirrors:// for - * TLS), stored as JSON in the settings store (see connections.ts). Each - * enabled connection gets a control websocket that backs the session list and - * carries unsolicited bell/title notifications; control connections live as - * long as the worker so bells keep flowing even if the hub window is closed. + * Connections are connection strings stored as JSON in the settings store + * (see connections.ts): g2mirror://@[:port] for a g2mirror + * server (g2mirrors:// for TLS), ssh://@ for tmux over SSH + * (TmuxClient), or ssh://@?g2mirror= for a g2mirror server + * tunnelled over SSH. Each enabled connection gets a control client that + * backs the session list and carries bell/title/activity notifications; + * control connections live as long as the worker so bells keep flowing even + * if the hub window is closed. SSH connections that need the user (an + * unverified or changed host key, a password, an unauthorized key) record + * the failure on the control (sshIssue) and offer a page that resolves it + * instead of retrying. * The host's server_name (from the init reply) is cached per connection and * used as its display name, falling back to the connection string's * host[:port] before the first successful handshake. @@ -55,7 +61,24 @@ import { import * as frameTimings from "../../native/frame-timings"; import { getActiveDisplay } from "../../native/active-display"; import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures"; -import { G2MirrorClient, type G2MirrorClientOptions, type G2MirrorSession, type G2MirrorState } from "../../native/g2mirror-client"; +import type { KeyEvent } from "../../g2/keyboard/keys"; +import { encodeTerminalKey, terminalScrollbackKey } from "./terminal-keys"; +import { G2MirrorClient, type G2MirrorSession, type G2MirrorState } from "../../native/g2mirror-client"; +import type { TerminalClient } from "./terminal-client"; +import { TmuxClient, type OpenLineChannel } from "./tmux-client"; +import { + copyToPhoneClipboard, + describeSshFailure, + getSshSecret, + openSshChannel, + removeSshSecret, + setSshSecret, + sshClientKey, + type SshEndpoint, + type SshFailure, + type SshHostKey, +} from "../../native/ssh"; +import { wrapText } from "../../graphics/textwrap"; import { onSettingsStoreChanged } from "../../native/settings-store"; import { clamp } from "../../util/numeric-util"; import { @@ -64,12 +87,22 @@ import { terminalDisplayLayoutSetting, terminalLaunchPresetsSetting, terminalNewConnectionSetting, + terminalSshPasswordSetting, terminalWakeOnBellSetting, textSettingMenuItem, toggleSettingMenuItem, } from "../../ui/dashboard-settings"; import { terminalFontPickerMenuItem } from "../../ui/font-picker"; -import { connectionDisplayName, loadConnections, parseConnectionString, saveConnections, TERMINAL_CONNECTIONS_KEY, updateConnection, type TerminalConnection } from "./connections"; +import { + connectionDisplayName, + connectionStringHostLabel, + loadConnections, + parseAnyConnectionString, + saveConnections, + TERMINAL_CONNECTIONS_KEY, + updateConnection, + type TerminalConnection, +} from "./connections"; import { TerminalEmulator } from "./terminal-emulator"; import { drawSubmenuIndicator, submenuItem, type MenuItem } from "../../ui/menu"; import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; @@ -112,6 +145,10 @@ const RECONNECT_MAX_DELAY_MS = 60_000; const NEW_CONNECTION_DRAFT_KEY = "terminal.newConnectionDraft"; // Storage key of terminalLaunchPresetsSetting, edited from the window menu. const LAUNCH_PRESETS_KEY = "terminal.launchPresets"; +// Storage key of terminalSshPasswordSetting (the password page's draft). +const SSH_PASSWORD_DRAFT_KEY = "terminal.sshPasswordDraft"; +// Comment on this device's SSH public key in authorized_keys. +const SSH_KEY_COMMENT = "faceclaw"; type BaseWindow = { windowId: string; @@ -148,10 +185,24 @@ type BaseWindow = { /** * What the hub window is showing: the session list, the Manage Connections - * section, or the Add-connection screen (type the g2mirror:// string on the - * phone, click to save). + * section, the Add-connection screen (type the connection string on the + * phone, click to save), or a page of text with a click action (verify a + * host key, enter a password, show this device's SSH key). */ -type HubMode = "sessions" | "connections" | "add"; +type HubMode = "sessions" | "connections" | "add" | "page"; + +type HubPage = { + title: string; + /** Body text, wrapped to the window; re-read on every paint. */ + body: () => string; + /** Bottom hint line. */ + hint: string; + onClick?: () => void; + /** Double-click. */ + onBack: () => void; + /** Runs whenever the page goes away (back, click, window closed). */ + onLeave?: () => void; +}; type HubWindow = BaseWindow & { kind: "hub"; @@ -168,6 +219,8 @@ type HubWindow = BaseWindow & { sessionOrder: string[]; /** Parse-failure message shown on the Add-connection screen. */ addError: string; + /** The page shown in mode "page". */ + page: HubPage | null; }; type ViewWindow = BaseWindow & { @@ -186,7 +239,7 @@ type ViewWindow = BaseWindow & { label: string; /** Sidebar-icon character (">3"); "" if every glyph was taken at open time. */ glyph: string; - client: G2MirrorClient; + client: TerminalClient; emulator: TerminalEmulator; receivedData: boolean; attachRequested: boolean; @@ -220,8 +273,8 @@ type PendingView = { socket: string; label: string; glyph: string; - /** Client options captured when the view was requested from a live control. */ - options: G2MirrorClientOptions; + /** How to make the view's client, captured when it was requested from a live control. */ + options: ClientRecipe; }; const windows = new Map(); @@ -302,7 +355,13 @@ let screenOn = true; */ type ControlConnection = { config: TerminalConnection; - client: G2MirrorClient | null; + client: TerminalClient | null; + /** + * An SSH failure only the user can fix (host key to verify, password or + * key needed). Shown on the host's heading with a row that opens the fix; + * reconnects wait for it. Cleared on a successful connection. + */ + sshIssue: SshFailure | null; state: G2MirrorState | null; unsubscribers: Array<() => void>; reconnectTimer: ReturnType | null; @@ -435,6 +494,13 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { } break; } + case "key": { + const window = windows.get(message.windowId); + if (!window) break; + window.focused = message.focused; + handleKey(window, message.event as KeyEvent); + break; + } case "render": { const window = windows.get(message.windowId); if (!window) break; @@ -504,7 +570,8 @@ onSettingsStoreChanged((key) => { reportIdle(); return; case NEW_CONNECTION_DRAFT_KEY: - // Live keystrokes from the phone editor; repaint the add screen. + case SSH_PASSWORD_DRAFT_KEY: + // Live keystrokes from the phone editor; repaint the add/password screen. renderHubWindows(); return; case "terminal.autoReconnect": @@ -578,6 +645,7 @@ function openWindow(windowId: string, surfaceId: string, title: string, viewport list: null, sessionOrder: [], addError: "", + page: null, }); // The hub carries the app's assistant tools; declare them once it exists. post({ type: "set-tools", windowId, tools: TERMINAL_TOOLS }); @@ -610,6 +678,10 @@ function closeWindow(windowId: string): void { // Window closed mid-add: shut the phone editor down. endAddConnection(window); } + if (window.kind === "hub" && window.mode === "page") { + // Likewise for a page's phone editor (the password draft). + closeHubPage(window, "sessions"); + } // Likewise for a setting being edited from the window menu. window.menu?.close(); windows.delete(windowId); @@ -627,17 +699,120 @@ function closeWindow(windowId: string): void { } } -/** Client options for a connection string; null if the string doesn't parse. */ -function clientOptionsFor(connection: TerminalConnection): G2MirrorClientOptions | null { - const parsed = parseConnectionString(connection.url); +/** + * How to make clients for a connection: "g2mirror" dials a g2mirror server + * directly, "ssh-g2mirror" reaches one through an SSH tunnel, "tmux" runs + * tmux control mode over SSH. + */ +type ClientRecipe = { + kind: "g2mirror" | "ssh-g2mirror" | "tmux"; + /** + * `role` "control" watches bells (tmux keeps the bell bookkeeping for that + * client only). `onSshFailure` hears why an SSH channel failed. + */ + create(cols: number, rows: number, role: "control" | "view", onSshFailure?: (failure: SshFailure) => void): TerminalClient; +}; + +/** Client recipe for a connection string; null if the string doesn't parse. */ +function clientOptionsFor(connection: TerminalConnection): ClientRecipe | null { + const parsed = parseAnyConnectionString(connection.url); if (!parsed) return null; + if (parsed.kind === "g2mirror") { + const { secure, host, port, authToken } = parsed; + return { + kind: "g2mirror", + create: (cols, rows) => new G2MirrorClient({ secure, host, port, authToken, deviceName: DEVICE_NAME, cols, rows }), + }; + } + const connectionId = connection.id; + const label = connectionStringHostLabel(connection.url); + // Read at every channel open, so a newly trusted key or saved password + // applies to the next (re)connect, views included. + const endpoint = (): SshEndpoint => ({ + host: parsed.host, + port: parsed.port, + user: parsed.user, + hostKeys: loadConnections().find((candidate) => candidate.id === connectionId)?.hostKeys ?? [], + password: getSshSecret(sshPasswordSecretId(connectionId)), + useKey: true, + }); + if (parsed.mode === "g2mirror") { + const { authToken, targetHost, targetPort } = parsed; + const urlHost = targetHost.includes(":") && !targetHost.startsWith("[") ? `[${targetHost}]` : targetHost; + const url = `ws://${urlHost}:${targetPort}/`; + return { + kind: "ssh-g2mirror", + create: (cols, rows, _role, onSshFailure) => + new G2MirrorClient({ + secure: false, + host: targetHost, + port: targetPort, + authToken, + deviceName: DEVICE_NAME, + cols, + rows, + transport: { + label: `${label} over SSH`, + open: (listener) => { + const channel = openSshChannel(endpoint(), { type: "websocket", url }, { + onOpen: () => listener.onOpen(), + onText: (text) => listener.onTextMessage(text), + onClosed: (info) => listener.onClosed(info.code ?? 1000, info.reason ?? ""), + onFailure: (failure) => { + onSshFailure?.(failure); + listener.onFailure(describeSshFailure(failure)); + }, + }); + return { sendText: (text) => channel.send(text), sendBinary: () => false, close: () => channel.close() }; + }, + }, + }), + }; + } return { - ...parsed, - deviceName: DEVICE_NAME, - ...viewGrid(), + kind: "tmux", + create: (cols, rows, role, onSshFailure) => { + const openChannel: OpenLineChannel = (command, listener) => + openSshChannel(endpoint(), { type: "exec", command }, { + onOpen: () => listener.onOpen(), + onText: (line) => listener.onLine(line), + onStderr: (line) => listener.onStderr(line), + onClosed: (info) => listener.onClosed(info.exitStatus ?? null), + onFailure: (failure) => { + onSshFailure?.(failure); + listener.onFailure(describeSshFailure(failure)); + }, + }); + return new TmuxClient({ openChannel, label, cols, rows, stateKey: role === "control" ? connectionId : "" }); + }, }; } +function sshPasswordSecretId(connectionId: string): string { + return `terminal.password.${connectionId}`; +} + +/** Failures that retrying can't fix: the user has to verify a key or supply credentials. */ +function sshFailureNeedsUser(failure: SshFailure): boolean { + return failure.kind === "hostkey-unknown" || failure.kind === "hostkey-mismatch" || failure.kind === "auth"; +} + +/** + * A control's SSH channel failed. If the user has to act, remember why, and + * if they're looking at the hub's lists, open the page that fixes it. + */ +function noteSshFailure(control: ControlConnection, failure: SshFailure): void { + if (!sshFailureNeedsUser(failure) || controls.get(control.config.id) !== control || !control.client) return; + control.sshIssue = failure; + renderHubWindows(); + for (const window of windows.values()) { + if (window.kind === "hub" && window.foreground && (window.mode === "sessions" || window.mode === "connections")) { + openSshIssuePage(window, control); + break; + } + } +} + /** * Reconcile the live control clients with the stored connection list: start * enabled connections, stop disabled or removed ones, restart ones whose @@ -663,6 +838,7 @@ function syncControlsFromSettings(): void { control = { config: connection, client: null, + sshIssue: null, state: null, unsubscribers: [], reconnectTimer: null, @@ -684,12 +860,13 @@ function syncControlsFromSettings(): void { function startControl(control: ControlConnection): void { stopControl(control); - const options = clientOptionsFor(control.config); - if (!options) { + const recipe = clientOptionsFor(control.config); + if (!recipe) { control.state = null; return; } - const client = new G2MirrorClient(options); + const grid = viewGrid(); + const client = recipe.create(grid.cols, grid.rows, "control", (failure) => noteSshFailure(control, failure)); const connectionId = control.config.id; control.client = client; control.state = client.state(); @@ -703,6 +880,7 @@ function startControl(control: ControlConnection): void { control.state = state; if (state.phase === "connected") { control.reconnectDelayMs = RECONNECT_MIN_DELAY_MS; + control.sshIssue = null; maybeCacheServerName(control, state.serverName); } if (state.phase === "failed") scheduleControlReconnect(control); @@ -777,6 +955,8 @@ function scheduleControlReconnect(control: ControlConnection): void { if (!terminalAutoReconnectSetting.get()) return; if (windows.size === 0 && !hasTerminalBackgroundWork()) return; if (!control.config.enabled) return; + // Waiting on the user (host key, password) or on the host (no usable tmux). + if (control.sshIssue || control.state?.retryable === false) return; const delayMs = control.reconnectDelayMs; control.reconnectDelayMs = Math.min(control.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS); control.reconnectTimer = setTimeout(() => { @@ -943,7 +1123,7 @@ function createViewWindow( const cell = getTerminalFontConfig(); const gridCols = Math.floor(viewport.width / cell.cellWidth); const gridRows = Math.floor(viewport.height / cell.cellHeight); - const client = new G2MirrorClient({ ...view.options, cols: gridCols, rows: gridRows }); + const client = view.options.create(gridCols, gridRows, "view"); const window: ViewWindow = { kind: "view", hub: null, @@ -1238,6 +1418,11 @@ function handleInput(window: TerminalWindow, event: InputEvent, frameId: number) renderAndSubmit(window, frameId); return; } + if (window.kind === "hub" && window.mode === "page") { + window.page?.onBack(); + renderAndSubmit(window, frameId); + return; + } if (window.kind === "hub" && window.mode === "connections") { setHubMode(window, "sessions"); renderAndSubmit(window, frameId); @@ -1260,6 +1445,35 @@ function handleInput(window: TerminalWindow, event: InputEvent, frameId: number) frameTimings.finishFrame(frameId, "discarded: terminal view ignored input"); } +/** + * A key from a hardware keyboard (the glasses' keyboard bridge) that no + * keybinding claimed. A session with nothing over it gets the key as its + * terminal would: Escape and the arrows belong to the program, and the way + * out is Escape with Shift, Ctrl or Alt, which never arrives here (it is a + * built-in keybinding for "back"). Anywhere else (the hub's lists, an open + * menu) the key is handed back, to act as navigation. + */ +function handleKey(window: TerminalWindow, event: KeyEvent): void { + if (window.kind === "view" && !window.menu?.isOpen()) { + activeViewId = window.windowId; + const page = terminalScrollbackKey(event); + if (page !== null) { + handleViewScroll(window, page, 0); + return; + } + const bytes = encodeTerminalKey(event, window.emulator.applicationCursorKeys()); + if (bytes === null) return; + // Typing follows the live screen, as in any terminal emulator. + if (window.scrollTop !== null) { + window.scrollTop = null; + scheduleRender(window); + } + window.client.sendInput(bytes); + return; + } + post({ type: "key-unhandled", windowId: window.windowId, event }); +} + /** Top visible absolute line index when following the live bottom. */ function followTop(window: ViewWindow): number { return window.historyNext + window.emulator.bufferLength() - window.gridRows; @@ -1386,6 +1600,154 @@ function setHubMode(window: HubWindow, mode: HubMode): void { window.addError = ""; } +function openHubPage(window: HubWindow, page: HubPage): void { + if (window.mode === "add") endAddConnection(window); + if (window.mode === "page") window.page?.onLeave?.(); + window.page = page; + window.mode = "page"; + scheduleRender(window); +} + +/** Leave the current page (if any) for `mode`. */ +function closeHubPage(window: HubWindow, mode: HubMode): void { + const page = window.page; + window.page = null; + page?.onLeave?.(); + setHubMode(window, mode); + scheduleRender(window); +} + +/** The list a page returns to: the one it was opened from. */ +function hubListMode(window: HubWindow): HubMode { + return window.mode === "connections" ? "connections" : "sessions"; +} + +function openSshIssuePage(window: HubWindow, control: ControlConnection): void { + const issue = control.sshIssue; + if (!issue) return; + if (issue.kind === "hostkey-unknown" && issue.hostKey) { + openHostKeyPage(window, control, issue.hostKey); + } else if (issue.kind === "hostkey-mismatch" && issue.hostKey) { + openHostKeyChangedPage(window, control, issue.hostKey); + } else if (issue.kind === "auth" && issue.passwordAllowed) { + openPasswordPage(window, control, Boolean(issue.passwordTried)); + } else if (issue.kind === "auth") { + openClientKeyPage(window, `${connectionDisplayName(control.config)} didn't accept this device's key (and doesn't take passwords).\n`); + } +} + +function hostKeyTypeName(type: string): string { + if (type === "ssh-ed25519") return "ED25519"; + if (type.startsWith("ecdsa-")) return "ECDSA"; + if (type === "ssh-rsa") return "RSA"; + return type; +} + +function hostKeyFile(type: string): string { + if (type === "ssh-ed25519") return "/etc/ssh/ssh_host_ed25519_key.pub"; + if (type.startsWith("ecdsa-")) return "/etc/ssh/ssh_host_ecdsa_key.pub"; + return "/etc/ssh/ssh_host_rsa_key.pub"; +} + +/** First connection to an SSH host: show the key's fingerprint; click trusts it. */ +function openHostKeyPage(window: HubWindow, control: ControlConnection, hostKey: SshHostKey): void { + const returnMode = hubListMode(window); + openHubPage(window, { + title: "Verify host key", + body: () => + `First connection to ${connectionStringHostLabel(control.config.url)}. Its ${hostKeyTypeName(hostKey.type)} host key fingerprint is:\n` + + `${hostKey.fingerprint}\n\n` + + `To check it, run this on the host and compare:\nssh-keygen -lf ${hostKeyFile(hostKey.type)}`, + hint: `click trust and connect ${GESTURE_DOUBLE_CLICK} cancel`, + onClick: () => { + const hostKeys = [...control.config.hostKeys.filter((key) => key !== hostKey.key), hostKey.key]; + control.config.hostKeys = hostKeys; + updateConnection(control.config.id, { hostKeys }); + control.sshIssue = null; + closeHubPage(window, returnMode); + connectControl(control); + }, + onBack: () => closeHubPage(window, returnMode), + }); +} + +function openHostKeyChangedPage(window: HubWindow, control: ControlConnection, hostKey: SshHostKey): void { + const returnMode = hubListMode(window); + openHubPage(window, { + title: "HOST KEY CHANGED", + body: () => + `${connectionStringHostLabel(control.config.url)} presented a different host key than the one trusted before: ` + + `${hostKeyTypeName(hostKey.type)} ${hostKey.fingerprint}\n\n` + + "Someone may be intercepting the connection, so Faceclaw won't connect. If the host was reinstalled or its keys " + + 'were regenerated, use "Forget host key" on this connection in Manage Connections, then verify the new fingerprint.', + hint: `${GESTURE_DOUBLE_CLICK} back`, + onBack: () => closeHubPage(window, returnMode), + }); +} + +/** Ask for the host's password on the phone (masked editor); click saves it and connects. */ +function openPasswordPage(window: HubWindow, control: ControlConnection, rejected: boolean): void { + const returnMode = hubListMode(window); + terminalSshPasswordSetting.set(""); + post({ type: "start-text-setting-edit", settingId: terminalSshPasswordSetting.id }); + openHubPage(window, { + title: "SSH password", + body: () => { + const typed = terminalSshPasswordSetting.get(); + return ( + (rejected ? "The host refused the saved password.\n" : "") + + `Type the password for ${connectionStringHostLabel(control.config.url)} in the phone app, then click to connect. ` + + "It is saved encrypted on this phone.\n\n" + + (typed ? "*".repeat(Math.min(typed.length, 32)) : "(nothing typed yet)") + ); + }, + hint: `click connect ${GESTURE_DOUBLE_CLICK} cancel`, + onClick: () => { + const password = terminalSshPasswordSetting.get(); + if (!password) return; + setSshSecret(sshPasswordSecretId(control.config.id), password); + control.sshIssue = null; + closeHubPage(window, returnMode); + connectControl(control); + }, + onBack: () => closeHubPage(window, returnMode), + onLeave: () => { + terminalSshPasswordSetting.set(""); + post({ type: "end-text-setting-edit" }); + }, + }); +} + +/** This device's SSH public key (the private half never leaves the keystore); click copies it. */ +function openClientKeyPage(window: HubWindow, intro = ""): void { + const returnMode = hubListMode(window); + let key: { authorizedKey: string; fingerprint: string } | null = null; + let error = ""; + try { + key = sshClientKey(SSH_KEY_COMMENT); + } catch (caught) { + error = String((caught as Error)?.message ?? caught); + } + let copied = false; + openHubPage(window, { + title: "This device's SSH key", + body: () => + key + ? `${intro}Add this line to ~/.ssh/authorized_keys on each host to log in with this phone's key:\n` + + `${key.authorizedKey}\n${key.fingerprint}` + + (copied ? "\n\nCopied to the phone's clipboard." : "") + : `Couldn't create this device's key: ${error}`, + hint: key ? `click copy to phone clipboard ${GESTURE_DOUBLE_CLICK} back` : `${GESTURE_DOUBLE_CLICK} back`, + onClick: key + ? () => { + copyToPhoneClipboard("SSH public key", key!.authorizedKey); + copied = true; + } + : undefined, + onBack: () => closeHubPage(window, returnMode), + }); +} + /** * One control connection's sessions in this hub window's display order: * most-recently-updated first as of when the window became visible, with @@ -1441,6 +1803,7 @@ function hubSessionItems(window: HubWindow): HubItem[] { ]; const multiHost = controls.size > 1; const canLaunch = launchPresetNames().length > 0; + const isTmux = (control: ControlConnection) => clientOptionsFor(control.config)?.kind === "tmux"; // Every configured host is listed under its own heading, in stored order, // whatever its connection state: a connected host lists its sessions, any // other shows its state on the heading plus a Connect row (unless it is @@ -1450,10 +1813,17 @@ function hubSessionItems(window: HubWindow): HubItem[] { const name = connectionDisplayName(control.config); if (!isControlConnected(control)) { items.push({ label: `${name} (${controlStatusWord(control)})`, heading: true }); + if (control.sshIssue && control.config.enabled) { + items.push({ label: sshIssueAction(control.sshIssue), onSelect: () => openSshIssuePage(window, control) }); + continue; + } const connecting = control.config.enabled && control.state?.phase === "connecting"; if (!connecting && clientOptionsFor(control.config)) { + // A failure retrying can't fix (tmux missing or too old) says why; + // selecting it retries once the host is fixed. + const unfixable = control.config.enabled && control.state?.retryable === false; items.push({ - label: control.reconnectTimer ? "Connect now" : "Connect", + label: unfixable ? control.state!.status : control.reconnectTimer ? "Connect now" : "Connect", onSelect: () => connectControl(control), }); } @@ -1462,7 +1832,8 @@ function hubSessionItems(window: HubWindow): HubItem[] { items.push({ label: name, heading: true, - onSelect: canLaunch ? () => launchOnHost(window, control) : undefined, + // tmux hosts can always start a shell, presets or not. + onSelect: canLaunch || isTmux(control) ? () => launchOnHost(window, control) : undefined, }); const sessions = orderedSessions(window, control); for (const session of sessions) { @@ -1478,7 +1849,11 @@ function hubSessionItems(window: HubWindow): HubItem[] { } if (!sessions.length) { items.push({ - label: multiHost ? "(no live sessions)" : "(no live sessions; run g2mirror )", + label: isTmux(control) + ? "(no tmux sessions; select the host to start one)" + : multiHost + ? "(no live sessions)" + : "(no live sessions; run g2mirror )", onSelect: () => control.client?.listSessions(), }); } @@ -1499,6 +1874,10 @@ function hubConnectionItems(window: HubWindow): HubItem[] { label: "Add connection", onSelect: () => beginAddConnection(window), }, + { + label: "This device's SSH key", + onSelect: () => openClientKeyPage(window), + }, { label: "Done", onSelect: () => setHubMode(window, "sessions"), @@ -1509,6 +1888,13 @@ function hubConnectionItems(window: HubWindow): HubItem[] { function controlStatusWord(control: ControlConnection): string { if (!control.config.enabled) return "off"; + const issue = control.sshIssue; + if (issue) { + if (issue.kind === "hostkey-unknown") return "verify host key"; + if (issue.kind === "hostkey-mismatch") return "HOST KEY CHANGED"; + if (issue.passwordTried) return "password rejected"; + return issue.passwordAllowed ? "password needed" : "key not authorized"; + } switch (control.state?.phase) { case "connected": case "attached": @@ -1541,6 +1927,32 @@ function openConnectionActions(window: HubWindow, control: ControlConnection): v connectControl(control); }, }, + ...(control.config.hostKeys.length + ? [ + { + label: "Forget host key", + onSelect: (ctx: { stack: { pop(): unknown } }) => { + ctx.stack.pop(); + control.config.hostKeys = []; + updateConnection(control.config.id, { hostKeys: [] }); + control.sshIssue = null; + if (control.config.enabled) startControl(control); + renderHubWindows(); + }, + }, + ] + : []), + ...(isSshConnection(control) && getSshSecret(sshPasswordSecretId(control.config.id)) + ? [ + { + label: "Forget saved password", + onSelect: (ctx: { stack: { pop(): unknown } }) => { + ctx.stack.pop(); + removeSshSecret(sshPasswordSecretId(control.config.id)); + }, + }, + ] + : []), { label: `Remove ${name}`, onSelect: (ctx) => { @@ -1558,6 +1970,11 @@ function openConnectionActions(window: HubWindow, control: ControlConnection): v windowMenu(window).open(items); } +function isSshConnection(control: ControlConnection): boolean { + const kind = clientOptionsFor(control.config)?.kind; + return kind === "tmux" || kind === "ssh-g2mirror"; +} + /** Enable and (re)connect now; also the manual retry path, so reset backoff. */ function connectControl(control: ControlConnection): void { control.reconnectDelayMs = RECONNECT_MIN_DELAY_MS; @@ -1578,6 +1995,7 @@ function disconnectControl(control: ControlConnection): void { function removeControl(control: ControlConnection): void { stopControl(control); + removeSshSecret(sshPasswordSecretId(control.config.id)); controls.delete(control.config.id); saveConnections(loadConnections().filter((connection) => connection.id !== control.config.id)); renderHubWindows(); @@ -1586,7 +2004,7 @@ function removeControl(control: ControlConnection): void { /** * Enter the Add-connection screen: clear the draft and ask the shell to open * the phone text editor on it. The user types (or voice-inputs) the - * g2mirror:// string, then clicks to save or double-clicks to cancel. + * connection string, then clicks to save or double-clicks to cancel. */ function beginAddConnection(window: HubWindow): void { terminalNewConnectionSetting.set(""); @@ -1596,15 +2014,15 @@ function beginAddConnection(window: HubWindow): void { function saveAddConnection(window: HubWindow): void { const url = terminalNewConnectionSetting.get(); - if (!parseConnectionString(url)) { + if (!parseAnyConnectionString(url)) { window.addError = url - ? "Not a g2mirror://token@host connection string." - : "Enter a g2mirror:// connection string first."; + ? "Not an ssh://user@host or g2mirror://token@host string." + : "Enter a connection string first."; scheduleRender(window); return; } const connections = loadConnections(); - connections.push({ id: `c${Date.now()}`, url, serverName: null, enabled: true }); + connections.push({ id: `c${Date.now()}`, url, serverName: null, enabled: true, hostKeys: [] }); saveConnections(connections); endAddConnection(window); setHubMode(window, "connections"); @@ -1634,6 +2052,15 @@ function handleHubInput(window: HubWindow, event: InputEvent, frameId: number): frameTimings.finishFrame(frameId, "discarded: terminal add-connection ignored input"); return; } + if (window.mode === "page") { + if (event.type === "click" && window.page?.onClick) { + window.page.onClick(); + renderAndSubmit(window, frameId); + return; + } + frameTimings.finishFrame(frameId, "discarded: terminal page ignored input"); + return; + } const list = hubMenu(window); list.setItems(hubItems(window)); switch (event.type) { @@ -1800,6 +2227,7 @@ function openViewWindow(control: ControlConnection, socket: string, label: strin focus: true, // Default to tall sessions; the app display setting can override this. heightMode: "max", + acceptsKeys: true, }); } @@ -1820,6 +2248,11 @@ function launchPresetNames(): string[] { */ function launchOnHost(window: HubWindow, control: ControlConnection): void { const presets = launchPresetNames(); + if (presets.length === 0) { + // A tmux host with no presets configured: a new session running the login shell. + launchFromUi(control, "shell"); + return; + } if (presets.length === 1) { launchFromUi(control, presets[0]!); return; @@ -1864,6 +2297,9 @@ function paintHub(window: HubWindow): GrayImage { if (window.mode === "add") { return paintAddConnection(window); } + if (window.mode === "page" && window.page) { + return paintHubPage(window, window.page); + } const { viewportWidth: width, viewportHeight: height, padding } = window; const image = new GrayImage(width, height, 0); const font = chromeFont(); @@ -1878,7 +2314,7 @@ function paintHub(window: HubWindow): GrayImage { let listTop = header.listTop; if (window.mode === "sessions" && controls.size === 0) { - image.drawText(font, header.titleX, listTop, "Add a g2mirror:// connection to get started, see:", 150); + image.drawText(font, header.titleX, listTop, "Add an ssh:// (tmux) or g2mirror:// connection to get started, see:", 150); image.drawText(font, header.titleX, listTop + step, "https://github.com/jimrandomh/g2mirror", 190); listTop += 2 * step + 6; } @@ -1920,8 +2356,8 @@ function paintAddConnection(window: HubWindow): GrayImage { image.drawText(font, padding.left, padding.top, "Add connection", 220); const bodyX = padding.left + PAGE_BODY_INDENT; const instructionsY = padding.top + 10 + step; - image.drawText(font, bodyX, instructionsY, "Type the g2mirror://token@host string in the", 170); - image.drawText(font, bodyX, instructionsY + step, "phone app (or use voice input from the menu).", 170); + image.drawText(font, bodyX, instructionsY, "Type ssh://user@host (tmux) or g2mirror://token@host in", 170); + image.drawText(font, bodyX, instructionsY + step, "the phone app (or use voice input from the menu).", 170); const draftY = instructionsY + 3 * step; const draft = terminalNewConnectionSetting.get(); image.drawText(font, bodyX, draftY, truncateLabel(draft || "(empty)", 52), 220); @@ -1932,6 +2368,34 @@ function paintAddConnection(window: HubWindow): GrayImage { return image; } +/** A page of text: title at the top padding, wrapped body, hint on the bottom padding. */ +function paintHubPage(window: HubWindow, page: HubPage): GrayImage { + const { viewportWidth: width, viewportHeight: height, padding } = window; + const image = new GrayImage(width, height, 0); + const font = chromeFont(); + const step = lineStep(font); + image.drawText(font, padding.left, padding.top, page.title, 220); + const bodyX = padding.left + PAGE_BODY_INDENT; + const hintY = height - padding.bottom - font.lineHeight; + const lines = wrapText(font, page.body(), Math.max(1, width - padding.right - bodyX), { breakLongWords: true }); + let y = padding.top + 10 + step; + for (const line of lines) { + if (y + step > hintY) break; + image.drawText(font, bodyX, y, line, 190); + y += step; + } + image.drawText(font, bodyX, hintY, page.hint, 110); + return image; +} + +/** The action row under a host whose SSH connection needs the user. */ +function sshIssueAction(issue: SshFailure): string { + if (issue.kind === "hostkey-unknown") return "Verify host key..."; + if (issue.kind === "hostkey-mismatch") return "Host key changed!"; + if (issue.passwordAllowed) return issue.passwordTried ? "Re-enter password..." : "Enter password..."; + return "Show this device's SSH key..."; +} + function truncateLabel(text: string, maxLength: number): string { return text.length <= maxLength ? text : `${text.slice(0, Math.max(0, maxLength - 3))}...`; } diff --git a/app/apps/terminal/terminal-client.ts b/app/apps/terminal/terminal-client.ts new file mode 100644 index 00000000..529850f7 --- /dev/null +++ b/app/apps/terminal/terminal-client.ts @@ -0,0 +1,44 @@ +import type { + G2MirrorHistoryReply, + G2MirrorPhase, + G2MirrorSession, + G2MirrorState, +} from "../../native/terminal-session-types"; + +/** + * What the Terminal worker needs from a session backend. G2MirrorClient (a + * g2mirror server, directly or tunnelled over SSH) and TmuxClient (tmux + * control mode over SSH) both provide it. The model is g2mirror's: a client + * starts "connecting", reaches "connected" (it can list and launch sessions, + * and reports bells/activity/titles for them), and can then attach to one + * session, view it (snapshot + live output), page its scrollback, and type + * into it. + */ +export interface TerminalClient { + state(): G2MirrorState; + onStateChange(listener: (state: G2MirrorState) => void): () => void; + /** Raw VT100/xterm bytes to feed the emulator. Snapshot implies reset-first. */ + onTerminalData(listener: (data: Uint8Array, kind: "snapshot" | "output") => void): () => void; + onSessionAttached(listener: (command: string) => void): () => void; + onSessionDetached(listener: (reason: string) => void): () => void; + onBell(listener: (socket: string, lastBellAtMs: number) => void): () => void; + onActivity(listener: (socket: string, lastOutputAtMs: number) => void): () => void; + onTitle(listener: (socket: string, title: string) => void): () => void; + /** Fired after a snapshot's terminal data, with its scrollback splice point. */ + onSnapshot(listener: (historyNext: number, historyOldest: number) => void): () => void; + onHistoryLines(listener: (reply: G2MirrorHistoryReply) => void): () => void; + setViewport(cols: number, rows: number): void; + start(): void; + stop(): void; + listSessions(): void; + launchSession(preset: string): Promise; + connectSession(socket: string): void; + disconnectSession(): void; + view(): void; + unview(): void; + requestHistory(before: number, limit?: number): void; + sendInput(text: string): void; + submitInput(text: string): void; +} + +export type { G2MirrorHistoryReply, G2MirrorPhase, G2MirrorSession, G2MirrorState }; diff --git a/app/apps/terminal/terminal-emulator.ts b/app/apps/terminal/terminal-emulator.ts index 914ff50f..aaa4aec6 100644 --- a/app/apps/terminal/terminal-emulator.ts +++ b/app/apps/terminal/terminal-emulator.ts @@ -82,4 +82,9 @@ export class TerminalEmulator { cursorCol(): number { return this.term.buffer.active.cursorX; } + + /** DECCKM: the session asked for application cursor keys (ESC O A rather than ESC [ A). */ + applicationCursorKeys(): boolean { + return this.term.modes.applicationCursorKeysMode; + } } diff --git a/app/apps/terminal/terminal-keys.ts b/app/apps/terminal/terminal-keys.ts new file mode 100644 index 00000000..a99cd107 --- /dev/null +++ b/app/apps/terminal/terminal-keys.ts @@ -0,0 +1,118 @@ +/** + * What a terminal emulator sends for a key from a hardware keyboard, by the + * classic xterm conventions: Ctrl chords become control characters, Alt + * prefixes ESC, and modified cursor and function keys carry the xterm + * modifier parameter (1 + shift + 2*alt + 4*ctrl). Follows the Glassly + * Terminal miniapp's encoder (terminal/keys.ts, MIT). + * No platform imports: this runs under node in the tests. + */ +import type { KeyEvent } from "../../g2/keyboard/keys"; + +const ESC = "\x1b"; + +/** Keys xterm encodes as `ESC [ ~`, with a modifier as `;m`. */ +const TILDE_KEYS: Record = { + Insert: 2, + Delete: 3, + PageUp: 5, + PageDown: 6, + F5: 15, + F6: 17, + F7: 18, + F8: 19, + F9: 20, + F10: 21, + F11: 23, + F12: 24, +}; + +/** Keys with a single-letter final byte: SS3 in application cursor mode, CSI otherwise. */ +const LETTER_KEYS: Record = { + ArrowUp: "A", + ArrowDown: "B", + ArrowRight: "C", + ArrowLeft: "D", + Home: "H", + End: "F", +}; + +/** F1-F4 are always SS3 (`ESC O P`), or CSI with a modifier. */ +const SS3_KEYS: Record = { F1: "P", F2: "Q", F3: "R", F4: "S" }; + +/** Ctrl with a punctuation or digit key, per xterm (^[ ^\ ^] ^^ ^_ ^? ^@). */ +const CONTROL_PUNCTUATION: Record = { + "[": "\x1b", + "\\": "\x1c", + "]": "\x1d", + "^": "\x1e", + "6": "\x1e", + "_": "\x1f", + "-": "\x1f", + "?": "\x7f", + "/": "\x7f", + "8": "\x7f", + "@": "\x00", + "2": "\x00", +}; + +/** + * The text to write to the terminal for one key event, or null when the key + * sends nothing: releases, bare modifiers, Meta chords, and keys a terminal + * has no bytes for (volume, media). `appCursor` is DECCKM, which the emulator + * tracks from the session's output. + */ +export function encodeTerminalKey(event: KeyEvent, appCursor: boolean): string | null { + if (event.phase !== "down") return null; + const { key, modifiers } = event; + if (modifiers.meta) return null; + const modifierParam = 1 + (modifiers.shift ? 1 : 0) + (modifiers.alt ? 2 : 0) + (modifiers.ctrl ? 4 : 0); + const modified = (prefix: string, final: string): string | null => + modifierParam > 1 ? `${ESC}[${prefix};${modifierParam}${final}` : null; + + switch (key) { + case "Enter": + return modifiers.alt ? `${ESC}\r` : "\r"; + case "Escape": + return ESC; + case "Tab": + return modifiers.shift ? `${ESC}[Z` : "\t"; + case "Backspace": + if (modifiers.alt) return `${ESC}\x7f`; + return modifiers.ctrl ? "\x08" : "\x7f"; + case " ": + return modifiers.ctrl ? "\x00" : modifiers.alt ? `${ESC} ` : " "; + } + + const letter = LETTER_KEYS[key]; + if (letter) return modified("1", letter) ?? (appCursor ? `${ESC}O${letter}` : `${ESC}[${letter}`); + const tilde = TILDE_KEYS[key]; + if (tilde) return modified(String(tilde), "~") ?? `${ESC}[${tilde}~`; + const ss3 = SS3_KEYS[key]; + if (ss3) return modified("1", ss3) ?? `${ESC}O${ss3}`; + + if (modifiers.ctrl) { + // From the unshifted key: ^A..^Z, then the punctuation set. + const base = key.length === 1 ? key.toLowerCase() : ""; + let char: string | undefined; + if (base >= "a" && base <= "z") char = String.fromCharCode(base.charCodeAt(0) - 96); + else char = CONTROL_PUNCTUATION[key]; + if (char === undefined) return null; + return modifiers.alt ? `${ESC}${char}` : char; + } + + // With Alt held the translator leaves `text` out (it is a chord, not + // typing); a terminal still wants ESC and the character. + const text = event.text ?? (modifiers.alt && key.length === 1 ? key : undefined); + if (text === undefined) return null; + return modifiers.alt ? `${ESC}${text}` : text; +} + +/** Shift+PageUp / Shift+PageDown page the emulator's own scrollback, as in xterm. */ +export function terminalScrollbackKey(event: KeyEvent): -1 | 1 | null { + if (event.phase !== "down") return null; + const { shift, ctrl, alt, meta } = event.modifiers; + if (!shift || ctrl || alt || meta) return null; + if (event.key === "PageUp") return -1; + if (event.key === "PageDown") return 1; + return null; +} diff --git a/app/apps/terminal/tmux-client.ts b/app/apps/terminal/tmux-client.ts new file mode 100644 index 00000000..b2a0abd0 --- /dev/null +++ b/app/apps/terminal/tmux-client.ts @@ -0,0 +1,1094 @@ +/** + * tmux backend for the Terminal app: lists, watches, views and types into + * tmux panes on an SSH host, presenting them through the same TerminalClient + * model as g2mirror sessions (each pane is one "session"; its pane id, e.g. + * "%12", plays the part of g2mirror's session socket). + * + * Two kinds of channel run on the host, both opened through `openChannel` + * (an SSH exec channel in the app, a local process in tests): + * + * - The poller: a /bin/sh loop that prints the tmux version and host name, + * then every 2 s lists every pane of every session (only when the listing + * changed). It also (re)installs an alert-bell hook that counts bells per + * window in the @faceclaw_bells window option: tmux's own bell flag misses + * bells in a session's current window, and this way bells that ring while + * we're disconnected still show up as a higher count. Bell, activity and + * title notifications come from diffing successive listings. + * + * - The viewer (after connectSession): a tmux control-mode client + * (`tmux -C attach-session`) on the pane's session. It never changes the + * session's current window or active pane, so a desktop client attached to + * the same session is undisturbed. Viewing sets a per-window size for this + * client (`refresh-client -C @W:WxH`, which resizes the window to the + * glasses while we look, and restores it when cleared), snapshots the pane + * with capture-pane, and then applies its %output stream. Unviewing pauses + * the pane's output to this client (`refresh-client -A %N:pause`; "off" + * would make tmux stop reading the pane altogether when no other client + * wants it, stalling the program). Output from the session's other panes is + * paused as soon as it shows up. + * + * Requires tmux 3.2+ (control-mode pause/continue and per-window sizes); + * older versions get a message asking for an upgrade. 3.8's history_added / + * history_collected make scrollback paging exact; before that it assumes + * the history hasn't hit its limit since the snapshot. + */ +import type { G2MirrorHistoryReply, G2MirrorPhase, G2MirrorSession, G2MirrorState } from "./terminal-client"; + +export const TMUX_MIN_VERSION: [number, number] = [3, 2]; + +export type LineChannelListener = { + onOpen(): void; + /** One stdout line (see native/ssh escapeLine for non-UTF-8 lines). */ + onLine(line: string): void; + onStderr(line: string): void; + onClosed(exitStatus: number | null): void; + onFailure(message: string): void; +}; + +export type LineChannel = { + send(line: string): boolean; + close(): void; +}; + +/** Run `command` on the host (through the user's login shell, like ssh does). */ +export type OpenLineChannel = (command: string, listener: LineChannelListener) => LineChannel; + +export type TmuxClientOptions = { + openChannel: OpenLineChannel; + /** How status lines name the host, e.g. "jim@laptop". */ + label: string; + /** Grid the glasses view sessions at. */ + cols: number; + rows: number; + /** + * Remembers per-window bell counts across client instances (the worker + * makes a new client per reconnect), so a bell that rang while we were + * disconnected is still reported. The connection id for the client that + * watches bells; "" (counts kept per instance) for view clients, which + * mustn't consume bells the watching client hasn't seen yet. + */ + stateKey: string; +}; + +const POLL_INTERVAL_S = 2; +const BELL_HOOK_INDEX = 7091; +const LAUNCH_TIMEOUT_MS = 15_000; +const SUBMIT_DELAY_MS = 150; +// send-keys -H takes one argument per byte; keep command lines modest. +const SEND_KEYS_CHUNK_BYTES = 256; +// How many listings connectSession waits for a just-launched pane to appear. +const CONNECT_WAIT_POLLS = 4; + +// ---- host-side scripts ---- + +const TMUX_CANDIDATES = [ + "/opt/homebrew/bin/tmux", + "/usr/local/bin/tmux", + '"$HOME/.local/bin/tmux"', + '"$HOME/bin/tmux"', + "/home/linuxbrew/.linuxbrew/bin/tmux", + '"$HOME/.nix-profile/bin/tmux"', + "/run/current-system/sw/bin/tmux", + "/snap/bin/tmux", +]; + +/** + * Wrap a /bin/sh script as an exec command. The command runs under the + * user's login shell, which may be bash, zsh, fish or csh, so it stays a + * single line whose only quoting is one pair of single quotes, and the + * script may contain neither single quotes nor "\\" (fish treats \\ and \' + * specially inside single quotes; any other backslash is literal in all of + * them). + */ +export function hostCommand(script: string): string { + if (script.includes("'") || script.includes("\\\\") || /[\r\n]/.test(script)) { + throw new Error("host script contains characters that can't be quoted portably"); + } + return `exec /bin/sh -c '${script}'`; +} + +/** + * Sets T to the tmux binary: PATH as the exec shell sees it, then PATH as a + * login shell sees it (Homebrew/Nix/~/.local installs are often only on the + * login PATH), then common install locations. + */ +function locateTmux(): string { + return [ + "T=$(command -v tmux 2>/dev/null)", + '[ -x "$T" ] || T=$("${SHELL:-/bin/sh}" -lc "command -v tmux" 2>/dev/null &1)"', + 'echo "FACECLAW-HOST $(hostname 2>/dev/null)"', + 'echo "FACECLAW-HOME $HOME"', + `F="${format}"`, + `H="${hook}"`, + "P=-", + // Print the listing only when it changed. No server running: the + // listing is empty. + `while :; do O=$("$T" set-hook -g "alert-bell[${BELL_HOOK_INDEX}]" "$H" \\; list-panes -a -F "$F" 2>/dev/null); ` + + `if [ "$O" != "$P" ]; then echo FACECLAW-BEGIN; printf "%s\\n" "$O"; echo FACECLAW-END; P=$O; fi; sleep ${POLL_INTERVAL_S}; done`, + ].join("; "), + ); +} + +export function viewerCommand(sessionId: string): string { + if (!/^\$\d+$/.test(sessionId)) throw new Error(`bad tmux session id ${sessionId}`); + // "\$" keeps /bin/sh from expanding the session id ($3) as a variable. + return hostCommand(`${locateTmux()}; exec "$T" -C attach-session -t "\\${sessionId}"`); +} + +const SAFE_LAUNCH_COMMAND = /^[A-Za-z0-9 ._/+:=@%,-]+$/; + +/** New detached session running `preset` ("shell" or "": the default shell). */ +export function launchCommand(preset: string): string { + const command = preset === "shell" ? "" : preset.trim(); + if (command && !SAFE_LAUNCH_COMMAND.test(command)) { + throw new Error(`Launch preset "${preset}" has characters that can't be passed to tmux.`); + } + return hostCommand( + `${locateTmux()}; exec "$T" new-session -d -P -F "FACECLAW-LAUNCHED #{pane_id} #{session_id}"${command ? ` "${command}"` : ""}`, + ); +} + +// ---- parsing helpers ---- + +/** Parse `tmux -V` output; null for builds without a numeric version (master), treated as new. */ +export function parseTmuxVersion(text: string): [number, number] | null { + const match = /(\d+)\.(\d+)/.exec(text); + return match ? [parseInt(match[1]!, 10), parseInt(match[2]!, 10)] : null; +} + +export function tmuxVersionSupported(text: string): boolean { + const version = parseTmuxVersion(text); + if (!version) return true; + const [major, minor] = version; + return major > TMUX_MIN_VERSION[0] || (major === TMUX_MIN_VERSION[0] && minor >= TMUX_MIN_VERSION[1]); +} + +export type PaneRow = { + paneId: string; + sessionId: string; + sessionName: string; + windowId: string; + windowIndex: string; + windowName: string; + windowPanes: number; + paneIndex: string; + pid: number; + dead: boolean; + /** Last output (or window activity), seconds, host clock. */ + activity: number; + bells: number; + command: string; + path: string; + title: string; +}; + +export function parsePaneRow(line: string): PaneRow | null { + const fields = line.split("\t"); + if (fields.length < PANE_FIELDS.length || !fields[0]!.startsWith("%")) return null; + const title = fields.slice(PANE_FIELDS.length - 1).join("\t"); + return { + paneId: fields[0]!, + sessionId: fields[1]!, + sessionName: fields[2]!, + windowId: fields[3]!, + windowIndex: fields[4]!, + windowName: fields[5]!, + windowPanes: parseInt(fields[6]!, 10) || 1, + paneIndex: fields[7]!, + pid: parseInt(fields[8]!, 10) || 0, + dead: fields[9] === "1", + activity: parseInt(fields[10]!, 10) || 0, + bells: parseInt(fields[11]!, 10) || 0, + command: fields[12]!, + path: fields[13]!, + title, + }; +} + +/** + * A row's label in the session list: "session:window[.pane] ", where + * the title is what the program set (OSC 0/2) or, when it hasn't set one + * (tmux's default title is the host name), the running command and its + * directory. + */ +export function paneLabel(row: PaneRow, hostName: string, home: string): string { + const where = `${row.sessionName}:${row.windowIndex}${row.windowPanes > 1 ? `.${row.paneIndex}` : ""}`; + const shortHost = hostName.split(".")[0] ?? ""; + const meaningfulTitle = row.title && row.title !== hostName && row.title !== shortHost ? row.title : ""; + if (meaningfulTitle) return `${where} ${meaningfulTitle}`; + const path = home && (row.path === home || row.path.startsWith(`${home}/`)) ? `~${row.path.slice(home.length)}` : row.path; + return `${where} ${row.command}${path ? ` ${path}` : ""}`; +} + +/** + * Decode a tmux control-mode %output value to bytes: "\ooo" is an octal + * byte (tmux escapes control characters and backslashes this way, and the + * SSH transport escapes bytes of lines that aren't valid UTF-8 the same + * way); any other character is UTF-8 encoded. + */ +export function decodeTmuxOutput(value: string): Uint8Array { + const bytes: number[] = []; + for (let i = 0; i < value.length; i++) { + const code = value.charCodeAt(i); + if (code === 0x5c && i + 3 < value.length && /^[0-7]{3}$/.test(value.slice(i + 1, i + 4))) { + bytes.push(parseInt(value.slice(i + 1, i + 4), 8) & 0xff); + i += 3; + continue; + } + pushUtf8(bytes, value, i); + if (code >= 0xd800 && code <= 0xdbff && i + 1 < value.length) i++; + } + return Uint8Array.from(bytes); +} + +function pushUtf8(bytes: number[], text: string, index: number): void { + const point = text.codePointAt(index)!; + if (point < 0x80) bytes.push(point); + else if (point < 0x800) bytes.push(0xc0 | (point >> 6), 0x80 | (point & 0x3f)); + else if (point < 0x10000) bytes.push(0xe0 | (point >> 12), 0x80 | ((point >> 6) & 0x3f), 0x80 | (point & 0x3f)); + else + bytes.push( + 0xf0 | (point >> 18), + 0x80 | ((point >> 12) & 0x3f), + 0x80 | ((point >> 6) & 0x3f), + 0x80 | (point & 0x3f), + ); +} + +export function encodeUtf8(text: string): Uint8Array { + const bytes: number[] = []; + for (let i = 0; i < text.length; i++) { + const code = text.charCodeAt(i); + pushUtf8(bytes, text, i); + if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) i++; + } + return Uint8Array.from(bytes); +} + +// Pane state read for a snapshot, '|'-separated (all numeric but the id). +const SNAPSHOT_FIELDS = [ + "#{pane_id}", + "#{cursor_x}", + "#{cursor_y}", + "#{cursor_flag}", + "#{keypad_cursor_flag}", + "#{keypad_flag}", + "#{wrap_flag}", + "#{insert_flag}", + "#{alternate_on}", + "#{scroll_region_upper}", + "#{scroll_region_lower}", + "#{pane_width}", + "#{pane_height}", + "#{history_size}", + "#{history_added}", + "#{history_collected}", + "#{history_generation}", +]; + +export type PaneState = { + paneId: string; + cursorX: number; + cursorY: number; + cursorVisible: boolean; + appCursorKeys: boolean; + appKeypad: boolean; + wrap: boolean; + insert: boolean; + alternate: boolean; + scrollUpper: number; + scrollLower: number; + width: number; + height: number; + /** Absolute index of the next line to enter the history (history_added, else history_size). */ + historyNext: number; + /** Oldest retained absolute index (history_collected, else 0). */ + historyOldest: number; + generation: string; +}; + +export function parsePaneState(line: string): PaneState | null { + const f = line.split("|"); + if (f.length < SNAPSHOT_FIELDS.length || !f[0]!.startsWith("%")) return null; + const n = (index: number) => parseInt(f[index]!, 10) || 0; + const exact = f[14] !== ""; + return { + paneId: f[0]!, + cursorX: n(1), + cursorY: n(2), + cursorVisible: f[3] !== "0", + appCursorKeys: f[4] === "1", + appKeypad: f[5] === "1", + wrap: f[6] !== "0", + insert: f[7] === "1", + alternate: f[8] === "1", + scrollUpper: n(9), + scrollLower: n(10), + width: n(11), + height: n(12), + historyNext: exact ? n(14) : n(13), + historyOldest: exact ? n(15) : 0, + generation: f[16] ?? "", + }; +} + +/** + * Terminal bytes that recreate a captured pane in a freshly reset emulator: + * the normal screen (when the pane is in the alternate screen), the visible + * lines, then cursor position and the modes that change how keys encode. + */ +export function snapshotBytes(state: PaneState, visible: string[], normalScreen: string[]): Uint8Array { + let out = ""; + const rows = (lines: string[]) => lines.slice(0, Math.max(1, state.height)).join("\x1b[0m\r\n"); + if (state.alternate) { + out += rows(normalScreen) + "\x1b[0m\x1b[?1049h\x1b[H"; + } + out += rows(visible) + "\x1b[0m"; + if (state.scrollUpper !== 0 || state.scrollLower !== state.height - 1) { + out += `\x1b[${state.scrollUpper + 1};${state.scrollLower + 1}r`; + } + out += `\x1b[${state.cursorY + 1};${state.cursorX + 1}H`; + if (!state.cursorVisible) out += "\x1b[?25l"; + if (state.appCursorKeys) out += "\x1b[?1h"; + if (state.appKeypad) out += "\x1b="; + if (!state.wrap) out += "\x1b[?7l"; + if (state.insert) out += "\x1b[4h"; + return encodeUtf8(out); +} + +/** send-keys -H commands typing `bytes` into pane `paneId`. */ +export function sendKeysCommands(paneId: string, bytes: Uint8Array): string[] { + const commands: string[] = []; + for (let start = 0; start < bytes.length; start += SEND_KEYS_CHUNK_BYTES) { + const hex = Array.from(bytes.subarray(start, start + SEND_KEYS_CHUNK_BYTES), (byte) => byte.toString(16).padStart(2, "0")); + commands.push(`send-keys -t ${paneId} -H ${hex.join(" ")}`); + } + return commands; +} + +// ---- client ---- + +type Block = { ok: boolean; lines: string[] }; +type PendingCommands = { count: number; blocks: Block[]; done: (blocks: Block[]) => void }; +type BellMemory = Map<string, { count: number; lastBellAt: number | null }>; + +const bellMemoryByKey = new Map<string, BellMemory>(); + +export class TmuxClient { + private phase: G2MirrorPhase = "idle"; + private status = "Not connected."; + private retryable = true; + private stopped = false; + private generation = 0; + + private poller: LineChannel | null = null; + private pollerLines: string[] | null = null; + private versionText = ""; + private hostName = ""; + private home = ""; + private rows = new Map<string, PaneRow>(); + private sessions: G2MirrorSession[] = []; + private listings = 0; + private pendingConnect: { paneId: string; deadline: number } | null = null; + + private viewer: LineChannel | null = null; + private attachedPane: PaneRow | null = null; + private viewing = false; + private sizeDirty = false; + private snapshotInFlight = false; + private snapshotGeneration = ""; + private pausedPanes = new Set<string>(); + private pending: PendingCommands[] = []; + private block: { time: string; number: string; ours: boolean; lines: string[] } | null = null; + private liveCheckInFlight = false; + private readonly localBellMemory: BellMemory = new Map(); + + private readonly stateListeners = new Set<(state: G2MirrorState) => void>(); + private readonly terminalDataListeners = new Set<(data: Uint8Array, kind: "snapshot" | "output") => void>(); + private readonly snapshotListeners = new Set<(historyNext: number, historyOldest: number) => void>(); + private readonly historyListeners = new Set<(reply: G2MirrorHistoryReply) => void>(); + private readonly attachedListeners = new Set<(command: string) => void>(); + private readonly detachedListeners = new Set<(reason: string) => void>(); + private readonly bellListeners = new Set<(socket: string, lastBellAtMs: number) => void>(); + private readonly activityListeners = new Set<(socket: string, lastOutputAtMs: number) => void>(); + private readonly titleListeners = new Set<(socket: string, title: string) => void>(); + + constructor(private readonly options: TmuxClientOptions) {} + + state(): G2MirrorState & { retryable: boolean } { + return { + phase: this.phase, + status: this.status, + sessions: this.sessions.slice(), + attachedCommand: this.attachedPane ? this.labelFor(this.attachedPane) : "", + serverName: this.hostName, + retryable: this.retryable, + }; + } + + onStateChange(listener: (state: G2MirrorState) => void): () => void { + return subscribe(this.stateListeners, listener); + } + onTerminalData(listener: (data: Uint8Array, kind: "snapshot" | "output") => void): () => void { + return subscribe(this.terminalDataListeners, listener); + } + onSnapshot(listener: (historyNext: number, historyOldest: number) => void): () => void { + return subscribe(this.snapshotListeners, listener); + } + onHistoryLines(listener: (reply: G2MirrorHistoryReply) => void): () => void { + return subscribe(this.historyListeners, listener); + } + onSessionAttached(listener: (command: string) => void): () => void { + return subscribe(this.attachedListeners, listener); + } + onSessionDetached(listener: (reason: string) => void): () => void { + return subscribe(this.detachedListeners, listener); + } + onBell(listener: (socket: string, lastBellAtMs: number) => void): () => void { + return subscribe(this.bellListeners, listener); + } + onActivity(listener: (socket: string, lastOutputAtMs: number) => void): () => void { + return subscribe(this.activityListeners, listener); + } + onTitle(listener: (socket: string, title: string) => void): () => void { + return subscribe(this.titleListeners, listener); + } + + setViewport(cols: number, rows: number): void { + if (cols === this.options.cols && rows === this.options.rows) return; + this.options.cols = cols; + this.options.rows = rows; + if (this.viewing) this.snapshot(); + } + + start(): void { + if (this.poller || this.viewer) return; + this.stopped = false; + this.retryable = true; + this.resetSessionState(); + this.setState("connecting", `Connecting to ${this.options.label}...`); + this.openPoller(); + } + + stop(): void { + this.stopped = true; + this.generation++; + if (this.viewer && this.attachedPane && this.viewing) { + // Best effort: give the window back its own size before detaching. + this.viewer.send(`refresh-client -C ${this.attachedPane.windowId}:`); + } + this.closeChannels(); + this.resetSessionState(); + this.setState("idle", "Not connected."); + } + + listSessions(): void { + // The poller pushes every change; nothing to ask for. + } + + launchSession(preset: string): Promise<string> { + return new Promise((resolve, reject) => { + if (this.phase !== "connected" && this.phase !== "attached") { + reject(new Error("Not connected to tmux.")); + return; + } + let command: string; + try { + command = launchCommand(preset); + } catch (error) { + reject(error); + return; + } + let settled = false; + let channel: LineChannel | null = null; + const errors: string[] = []; + const finish = (error: Error | null, paneId?: string) => { + if (settled) return; + settled = true; + clearTimeout(timer); + channel?.close(); + if (error) reject(error); + else resolve(paneId!); + }; + const timer = setTimeout(() => finish(new Error(`Launch of "${preset}" timed out.`)), LAUNCH_TIMEOUT_MS); + channel = this.options.openChannel(command, { + onOpen: () => {}, + onLine: (line) => { + const match = /^FACECLAW-LAUNCHED (%\d+) (\$\d+)$/.exec(line); + if (match) finish(null, match[1]); + else if (line === "FACECLAW-NO-TMUX") finish(new Error("tmux is not installed on the host.")); + }, + onStderr: (line) => errors.push(line), + onClosed: () => finish(new Error(errors.join(" ") || `Launch of "${preset}" failed.`)), + onFailure: (message) => finish(new Error(message)), + }); + if (settled) channel.close(); + }); + } + + connectSession(paneId: string): void { + if (this.phase !== "connected") return; + const row = this.rows.get(paneId); + if (!row) { + // Just launched, perhaps: wait a few listings for it to show up. + this.pendingConnect = { paneId, deadline: this.listings + CONNECT_WAIT_POLLS }; + this.setState(this.phase, "Waiting for the session..."); + return; + } + this.pendingConnect = null; + this.openViewer(row); + } + + disconnectSession(): void { + if (this.phase !== "attached") return; + this.unview(); + this.viewer?.close(); + this.viewer = null; + this.attachedPane = null; + this.setState("idle", "Detached."); + } + + view(): void { + if (this.phase !== "attached" || !this.attachedPane) return; + this.viewing = true; + this.snapshot(); + } + + unview(): void { + if (this.phase !== "attached" || !this.attachedPane || !this.viewing) return; + this.viewing = false; + const pane = this.attachedPane; + this.command([`refresh-client -A '${pane.paneId}:pause'`, `refresh-client -C ${pane.windowId}:`]); + } + + requestHistory(before: number, limit = 200): void { + if (this.phase !== "attached" || !this.attachedPane) return; + void this.fetchHistory(this.attachedPane, before, limit); + } + + sendInput(text: string): void { + if (this.phase !== "attached" || !this.attachedPane || !text) return; + if (this.sizeDirty && this.viewing) { + // Someone else resized the window since we sized it (window-size + // "latest" follows the most recently active client). Typing here makes + // us the active one again: take the size back and resync. + this.snapshot(); + } + this.command(sendKeysCommands(this.attachedPane.paneId, encodeUtf8(text))); + } + + submitInput(text: string): void { + if (this.phase !== "attached") return; + // Like g2mirror's `delays`: a pause before Enter so paste-detecting apps + // (Claude Code) read it as a keypress, not the tail of a paste. + if (text) this.sendInput(text); + const generation = this.generation; + setTimeout(() => { + if (generation === this.generation) this.sendInput("\r"); + }, text ? SUBMIT_DELAY_MS : 0); + } + + // ---- poller ---- + + private openPoller(): void { + const generation = this.generation; + const stderr: string[] = []; + this.poller = this.options.openChannel(pollerCommand(), { + onOpen: () => {}, + onLine: (line) => { + if (generation === this.generation) this.handlePollerLine(line); + }, + onStderr: (line) => { + stderr.push(line); + if (stderr.length > 5) stderr.shift(); + }, + onClosed: (exitStatus) => { + if (generation !== this.generation || this.phase === "failed") return; + this.poller = null; + if (this.phase === "attached") return; // the viewer carries on alone + const detail = stderr.length ? `: ${stderr[stderr.length - 1]}` : exitStatus !== null ? ` (exit ${exitStatus})` : ""; + this.fail(`Session listing on ${this.options.label} ended${detail}`); + }, + onFailure: (message) => { + if (generation !== this.generation) return; + this.poller = null; + this.fail(message); + }, + }); + } + + private handlePollerLine(line: string): void { + if (this.pollerLines) { + if (line === "FACECLAW-END") { + this.applyListing(this.pollerLines); + this.pollerLines = null; + } else { + this.pollerLines.push(line); + } + return; + } + if (line === "FACECLAW-BEGIN") { + this.pollerLines = []; + } else if (line === "FACECLAW-NO-TMUX") { + this.retryable = false; + this.fail(`tmux is not installed on ${this.options.label} (or not on the login shell's PATH).`); + } else if (line.startsWith("FACECLAW-TMUX ")) { + this.versionText = line.slice("FACECLAW-TMUX ".length).trim(); + if (!tmuxVersionSupported(this.versionText)) { + this.retryable = false; + this.fail( + `${this.versionText || "tmux"} on ${this.options.label} is too old; Faceclaw needs tmux ${TMUX_MIN_VERSION.join(".")} or newer. Please upgrade it.`, + ); + } + } else if (line.startsWith("FACECLAW-HOST ")) { + this.hostName = line.slice("FACECLAW-HOST ".length).trim(); + } else if (line.startsWith("FACECLAW-HOME ")) { + this.home = line.slice("FACECLAW-HOME ".length).trim(); + } + } + + private applyListing(lines: string[]): void { + const now = Date.now(); + const previous = this.rows; + const firstListing = this.listings === 0; + this.listings++; + const memory = this.options.stateKey ? bellMemoryFor(this.options.stateKey) : this.localBellMemory; + const rows = new Map<string, PaneRow>(); + const bellsByWindow = new Map<string, number>(); + for (const line of lines) { + const row = parsePaneRow(line); + if (!row || rows.has(row.paneId)) continue; + rows.set(row.paneId, row); + bellsByWindow.set(row.windowId, row.bells); + } + // Bells are counted per window. A count above the remembered one is a + // bell (possibly one that rang while we were away); a lower one means the + // tmux server restarted and reused the id. + const belledWindows = new Set<string>(); + for (const [windowId, count] of bellsByWindow) { + const known = memory.get(windowId); + if (!known) { + memory.set(windowId, { count, lastBellAt: null }); + } else if (count > known.count) { + memory.set(windowId, { count, lastBellAt: now }); + belledWindows.add(windowId); + } else if (count < known.count) { + memory.set(windowId, { count, lastBellAt: known.lastBellAt }); + } + } + this.rows = rows; + this.sessions = [...rows.values()] + .filter((row) => !row.dead) + .map((row) => ({ + socket: row.paneId, + pid: row.pid, + cwdHint: row.path.replace(/\//g, "_"), + lastBellAt: memory.get(row.windowId)?.lastBellAt ?? null, + lastOutputAt: row.activity ? row.activity * 1000 : null, + title: this.labelFor(row), + })); + if (this.phase === "connecting") { + this.setState("connected", `Connected to ${this.options.label} (${this.versionText || "tmux"}).`); + } else { + this.emitState(); + } + if (!firstListing) { + for (const row of rows.values()) { + const before = previous.get(row.paneId); + if (belledWindows.has(row.windowId)) { + const at = memory.get(row.windowId)?.lastBellAt ?? now; + for (const listener of this.bellListeners) listener(row.paneId, at); + } + if (before && row.activity > before.activity) { + for (const listener of this.activityListeners) listener(row.paneId, row.activity * 1000); + } + if (before && row.title !== before.title) { + for (const listener of this.titleListeners) listener(row.paneId, this.labelFor(row)); + } + } + } + if (this.pendingConnect) { + const { paneId, deadline } = this.pendingConnect; + const row = rows.get(paneId); + if (row) { + this.pendingConnect = null; + this.openViewer(row); + } else if (this.listings >= deadline) { + this.pendingConnect = null; + this.setState("connected", "That session no longer exists."); + for (const listener of this.detachedListeners) listener("session not found"); + } + } + } + + private labelFor(row: PaneRow): string { + return paneLabel(row, this.hostName, this.home); + } + + // ---- viewer ---- + + private openViewer(row: PaneRow): void { + const generation = this.generation; + let command: string; + try { + command = viewerCommand(row.sessionId); + } catch (error) { + this.fail(String((error as Error).message ?? error)); + return; + } + this.attachedPane = row; + this.pausedPanes.clear(); + this.pending = []; + this.block = null; + this.setState("connected", "Attaching to session..."); + const stderr: string[] = []; + let attached = false; + let attachError = ""; + this.viewer = this.options.openChannel(command, { + onOpen: () => {}, + onLine: (line) => { + if (generation !== this.generation) return; + if (!attached) { + // The attach-session command's own reply block: %end means tmux + // attached us, %error (then %exit) that the session is gone. + if (line.startsWith("%end ")) { + attached = true; + // The view needs no listings from here on. + this.poller?.close(); + this.poller = null; + this.setState("attached", `Attached to ${this.labelFor(row)}.`); + for (const listener of this.attachedListeners) listener(this.labelFor(row)); + } else if (!line.startsWith("%") && !attachError) { + attachError = line; + } + return; + } + this.handleControlLine(line); + }, + onStderr: (line) => { + stderr.push(line); + if (stderr.length > 5) stderr.shift(); + }, + onClosed: () => { + if (generation !== this.generation) return; + this.viewer = null; + if (!attached && attachError) { + // E.g. "can't find session": it ended between listing and attaching. + this.generation++; + this.closeChannels(); + this.attachedPane = null; + this.setState("connected", `That session no longer exists (${attachError}).`); + for (const listener of this.detachedListeners) listener("session not found"); + return; + } + if (!attached) { + this.fail(stderr.length ? `tmux: ${stderr[stderr.length - 1]}` : "tmux refused to attach."); + return; + } + this.sessionEnded("tmux client exited"); + }, + onFailure: (message) => { + if (generation !== this.generation) return; + this.viewer = null; + this.fail(message); + }, + }); + } + + private handleControlLine(line: string): void { + const block = this.block; + if (block) { + if (line.startsWith("%end ") || line.startsWith("%error ")) { + const [, time, number] = line.split(" "); + if (time === block.time && number === block.number) { + this.block = null; + if (block.ours) this.finishBlock({ ok: line.startsWith("%end "), lines: block.lines }); + return; + } + } + block.lines.push(line); + return; + } + const space = line.indexOf(" "); + const kind = space < 0 ? line : line.slice(0, space); + switch (kind) { + case "%begin": { + const [, time, number, flags] = line.split(" "); + // flags 1: a command this client sent (0: e.g. the initial attach). + this.block = { time: time ?? "", number: number ?? "", ours: flags === "1", lines: [] }; + return; + } + case "%output": { + const second = line.indexOf(" ", space + 1); + if (second < 0) return; + this.handleOutput(line.slice(space + 1, second), line.slice(second + 1)); + return; + } + case "%exit": + this.sessionEnded(line.length > 6 ? line.slice(6) : "tmux session ended"); + return; + case "%layout-change": { + const [, windowId, layout] = line.split(" "); + const pane = this.attachedPane; + if (pane && windowId === pane.windowId) { + const size = /^[0-9a-f]{4},(\d+)x(\d+),/.exec(layout ?? ""); + if (size && this.viewing && (+size[1]! !== this.options.cols || +size[2]! !== this.options.rows)) { + this.sizeDirty = true; + } + this.checkPaneAlive(); + } + return; + } + case "%window-close": + case "%unlinked-window-close": + case "%window-pane-changed": + case "%session-changed": + this.checkPaneAlive(); + return; + default: + // %session-renamed, %window-renamed, %pause, %continue, ... + return; + } + } + + private handleOutput(paneId: string, value: string): void { + const pane = this.attachedPane; + if (!pane || paneId !== pane.paneId) { + // Another pane in the attached session: we never show it, so stop tmux + // sending it to us. + if (!this.pausedPanes.has(paneId) && /^%\d+$/.test(paneId)) { + this.pausedPanes.add(paneId); + this.command([`refresh-client -A '${paneId}:pause'`]); + } + return; + } + // Output that arrives before a snapshot's capture is already in it. + if (!this.viewing || this.snapshotInFlight) return; + const bytes = decodeTmuxOutput(value); + for (const listener of this.terminalDataListeners) listener(bytes, "output"); + } + + private finishBlock(block: Block): void { + const pending = this.pending[0]; + if (!pending) return; + pending.blocks.push(block); + if (pending.blocks.length >= pending.count) { + this.pending.shift(); + pending.done(pending.blocks); + } + } + + /** + * Send commands as one line (tmux runs them back to back, so e.g. a + * capture and the state read with it are consistent); `done` gets one + * block per command, synchronously as the last one arrives — before any + * later %output line is handled — or [] if the viewer went away. + */ + private command(commands: string[], done: (blocks: Block[]) => void = () => {}): void { + if (!this.viewer || !commands.length) { + done([]); + return; + } + this.pending.push({ count: commands.length, blocks: [], done }); + if (!this.viewer.send(commands.join(" ; "))) { + this.pending.pop(); + done([]); + } + } + + private commandAsync(commands: string[]): Promise<Block[]> { + return new Promise((resolve) => this.command(commands, resolve)); + } + + private snapshot(): void { + const pane = this.attachedPane; + if (!pane || this.snapshotInFlight) return; + this.snapshotInFlight = true; + this.sizeDirty = false; + const generation = this.generation; + const { cols, rows } = this.options; + this.command([ + `refresh-client -C ${pane.windowId}:${cols}x${rows}`, + `refresh-client -A '${pane.paneId}:continue'`, + `display -p -t ${pane.paneId} '${SNAPSHOT_FIELDS.join("|")}'`, + `capture-pane -p -e -t ${pane.paneId}`, + `capture-pane -p -e -a -q -t ${pane.paneId}`, + ], (blocks) => { + if (generation !== this.generation) return; + this.snapshotInFlight = false; + if (blocks.length < 5 || !this.viewing) return; + const state = parsePaneState(blocks[2]!.lines[0] ?? ""); + if (!state || state.paneId !== pane.paneId) { + this.sessionEnded("session closed"); + return; + } + this.snapshotGeneration = state.generation; + const data = snapshotBytes(state, blocks[3]!.lines, blocks[4]!.ok ? blocks[4]!.lines : []); + for (const listener of this.terminalDataListeners) listener(data, "snapshot"); + for (const listener of this.snapshotListeners) listener(state.historyNext, state.historyOldest); + }); + } + + private async fetchHistory(pane: PaneRow, before: number, limit: number): Promise<void> { + const generation = this.generation; + const stateCommand = `display -p -t ${pane.paneId} '${SNAPSHOT_FIELDS.join("|")}'`; + let state = parsePaneState((await this.commandAsync([stateCommand]))[0]?.lines[0] ?? ""); + // Absolute index a is at capture-pane line (a - historyNext). If more + // lines scroll into the history between our two requests, the offsets go + // stale; the second request re-reads the state with the capture, so + // retry until they agree. + for (let attempt = 0; attempt < 3 && state && generation === this.generation; attempt++) { + if (state.generation !== this.snapshotGeneration) { + // History was cleared or reflowed: our indices mean nothing now. + this.snapshot(); + return; + } + const start = Math.max(state.historyOldest, before - limit); + const end = Math.min(before, state.historyNext); + if (end <= start) { + this.emitHistory({ start, oldest: state.historyOldest, next: state.historyNext, lines: [] }); + return; + } + const blocks = await this.commandAsync([ + stateCommand, + `capture-pane -p -t ${pane.paneId} -S ${start - state.historyNext} -E ${end - 1 - state.historyNext}`, + ]); + if (generation !== this.generation || blocks.length < 2) return; + const after = parsePaneState(blocks[0]!.lines[0] ?? ""); + if (after && after.historyNext === state.historyNext && after.generation === state.generation) { + this.emitHistory({ + start, + oldest: state.historyOldest, + next: state.historyNext, + lines: blocks[1]!.ok ? blocks[1]!.lines.slice(0, end - start) : [], + }); + return; + } + state = after; + } + } + + private emitHistory(reply: G2MirrorHistoryReply): void { + for (const listener of this.historyListeners) listener(reply); + } + + /** Something structural changed: make sure our pane still exists. */ + private checkPaneAlive(): void { + const pane = this.attachedPane; + if (!pane || this.liveCheckInFlight) return; + this.liveCheckInFlight = true; + const generation = this.generation; + this.command([`display -p -t ${pane.paneId} '#{pane_id} #{window_id}'`], (blocks) => { + this.liveCheckInFlight = false; + if (generation !== this.generation || !blocks.length) return; + const [paneId, windowId] = (blocks[0]!.lines[0] ?? "").split(" "); + if (paneId !== pane.paneId) { + this.sessionEnded("session closed"); + } else if (windowId && windowId !== pane.windowId) { + // The pane moved to another window (break-pane, move-pane). + this.attachedPane = { ...pane, windowId }; + if (this.viewing) this.snapshot(); + } + }); + } + + private sessionEnded(reason: string): void { + if (this.phase !== "attached") return; + this.generation++; + this.closeChannels(); + this.attachedPane = null; + this.viewing = false; + this.setState("connected", `Session ended (${reason}).`); + for (const listener of this.detachedListeners) listener(reason); + } + + // ---- shared ---- + + private fail(message: string): void { + if (this.stopped) return; + this.generation++; + this.closeChannels(); + this.attachedPane = null; + this.viewing = false; + this.setState("failed", message); + } + + private closeChannels(): void { + this.poller?.close(); + this.poller = null; + this.viewer?.close(); + this.viewer = null; + for (const pending of this.pending.splice(0)) pending.done([]); + this.block = null; + this.snapshotInFlight = false; + this.liveCheckInFlight = false; + } + + private resetSessionState(): void { + this.pollerLines = null; + this.rows = new Map(); + this.sessions = []; + this.listings = 0; + this.pendingConnect = null; + this.attachedPane = null; + this.viewing = false; + this.sizeDirty = false; + } + + private setState(phase: G2MirrorPhase, status: string): void { + this.phase = phase; + this.status = status; + this.emitState(); + } + + private emitState(): void { + const state = this.state(); + for (const listener of this.stateListeners) listener(state); + } +} + +function bellMemoryFor(key: string): BellMemory { + let memory = bellMemoryByKey.get(key); + if (!memory) { + memory = new Map(); + bellMemoryByKey.set(key, memory); + } + return memory; +} + +function subscribe<T>(set: Set<T>, listener: T): () => void { + set.add(listener); + return () => set.delete(listener); +} diff --git a/app/apps/transcribe/index.ts b/app/apps/transcribe/index.ts index d5e504c6..ccdcddf9 100644 --- a/app/apps/transcribe/index.ts +++ b/app/apps/transcribe/index.ts @@ -1,5 +1,32 @@ -import { type AppDefinition } from "../app-definition"; -import { createTranscribeAppWindow, TRANSCRIBE_SURFACE_ID, TRANSCRIBE_WINDOW_ID } from "./transcribe-app"; +import { type AppContext, type AppDefinition } from "../app-definition"; +import { + createTranscribeAppWindow, + createTranscribeReplayWindow, + TRANSCRIBE_SURFACE_ID, + TRANSCRIBE_WINDOW_ID, +} from "./transcribe-app"; + +// Replay windows are closeable and non-singleton; the serial keeps their windowIds unique. +let nextReplaySerial = 1; + +/** + * Open a recording (.mka) in Transcribe's replay mode, as a window of its + * own. Returns false where recordings can't be replayed. + */ +export function openRecordingInTranscribe(ctx: AppContext, path: string, name: string): boolean { + if (!ctx.actions.openVoiceReplay) return false; + const windowId = `transcribe:replay:${nextReplaySerial++}`; + void ctx + .launchInProcessApp(windowId, `window:${windowId}`, (options) => { + const window = createTranscribeReplayWindow(windowId, path, name, options); + if (!window) throw new Error("recordings can't be replayed here"); + return window; + }) + .catch((error) => { + ctx.appendLog(`transcribe replay window failed: ${error}`); + }); + return true; +} const transcribeApp: AppDefinition = { appId: "transcribe", diff --git a/app/apps/transcribe/replay-time.ts b/app/apps/transcribe/replay-time.ts new file mode 100644 index 00000000..29c6268c --- /dev/null +++ b/app/apps/transcribe/replay-time.ts @@ -0,0 +1,23 @@ +/** A position in a recording as m:ss, or h:mm:ss from an hour on (or when `withHours`). */ +export function formatReplayTime(ms: number, withHours = false): string { + const total = Math.max(0, Math.floor(ms / 1000)); + const hours = Math.floor(total / 3600); + const minutes = Math.floor((total % 3600) / 60); + const seconds = String(total % 60).padStart(2, "0"); + if (hours === 0 && !withHours) return `${minutes}:${seconds}`; + return `${hours}:${String(minutes).padStart(2, "0")}:${seconds}`; +} + +/** The time picker's fields, in display and click order. */ +export const TIME_FIELDS = ["hours", "minutes", "seconds"] as const; +export type TimeField = (typeof TIME_FIELDS)[number]; + +const FIELD_SECONDS: Record<TimeField, number> = { hours: 3600, minutes: 60, seconds: 1 }; + +/** + * Step a position (whole seconds) by `delta` of a field's unit, carrying + * into the neighboring fields, within [0, maxSeconds]. + */ +export function stepReplaySeconds(seconds: number, field: TimeField, delta: number, maxSeconds: number): number { + return Math.max(0, Math.min(maxSeconds, seconds + delta * FIELD_SECONDS[field])); +} diff --git a/app/apps/transcribe/time-picker.ts b/app/apps/transcribe/time-picker.ts new file mode 100644 index 00000000..3bb281e6 --- /dev/null +++ b/app/apps/transcribe/time-picker.ts @@ -0,0 +1,147 @@ +import { GrayImage } from "../../graphics/image"; +import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, gestureHints, type InputEvent } from "../../ui/gestures"; +import type { Layer, LayerContext, PaintBelow } from "../../ui/layers"; +import { CONTEXT_MENU_DIM } from "../../ui/menu"; +import { centeredTextY } from "../../ui/metrics"; +import { formatReplayTime, stepReplaySeconds, TIME_FIELDS, type TimeField } from "./replay-time"; + +const PADDING = 12; +const FIELD_GAP = 6; +const FIELD_PAD_X = 6; +/** Fill and outline of the active field (the Timers and date pickers' field style). */ +const ACTIVE_FILL = 35; +const ACTIVE_STROKE = 130; + +export type TimePickerOptions = { + title: string; + /** Where the picker starts, and the furthest it goes, in ms. */ + initialMs: number; + maxMs: number; + onPick: (positionMs: number) => void; +}; + +/** + * A modal position picker for a recording: hours (when the recording has + * any), minutes and seconds, over the dimmed content beneath. Input follows + * the date picker's: scroll adjusts the active field (up is later, carrying + * into the fields beside it), click moves to the next field and on the last + * one picks, double-click steps back a field and on the first one cancels. + * A watch's swipes move between fields and adjust. + */ +export class TimePickerLayer implements Layer { + readonly acceptsDirectional = true; + readonly dimUnderneath = CONTEXT_MENU_DIM; + private readonly fields: readonly TimeField[]; + private readonly maxSeconds: number; + private seconds: number; + private field = 0; + + constructor(private readonly options: TimePickerOptions) { + this.maxSeconds = Math.max(0, Math.floor(options.maxMs / 1000)); + this.seconds = Math.max(0, Math.min(this.maxSeconds, Math.floor(options.initialMs / 1000))); + this.fields = this.maxSeconds >= 3600 ? TIME_FIELDS : TIME_FIELDS.slice(1); + } + + handleInput(event: InputEvent, ctx: LayerContext): void { + switch (event.type) { + case "scroll-up": + case "swipe-up": + this.adjust(1); + return; + case "scroll-down": + case "swipe-down": + this.adjust(-1); + return; + case "swipe-right": + this.field = Math.min(this.fields.length - 1, this.field + 1); + return; + case "swipe-left": + this.field = Math.max(0, this.field - 1); + return; + case "click": + if (this.field < this.fields.length - 1) { + this.field++; + } else { + ctx.stack.pop(); + this.options.onPick(this.seconds * 1000); + } + return; + case "double-click": + if (this.field > 0) this.field--; + else ctx.stack.pop(); + return; + default: + return; + } + } + + private adjust(delta: number): void { + this.seconds = stepReplaySeconds(this.seconds, this.fields[this.field]!, delta, this.maxSeconds); + } + + private hints(field: number): string { + return gestureHints([ + [GESTURE_SCROLL, "adjust"], + [GESTURE_CLICK, field < this.fields.length - 1 ? "next" : "jump"], + [GESTURE_DOUBLE_CLICK, field > 0 ? "back" : "cancel"], + ]); + } + + paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { + const image = paintBelow(); + const small = getDefaultSmallFont(); + const medium = getDefaultMediumFont(); + const focused = ctx.stack.isFocused(); + const base = ctx.stack.getBaseSize(); + + const values = [ + String(Math.floor(this.seconds / 3600)), + String(Math.floor((this.seconds % 3600) / 60)).padStart(this.fields.length === 3 ? 2 : 1, "0"), + String(this.seconds % 60).padStart(2, "0"), + ].slice(TIME_FIELDS.length - this.fields.length); + // Sized for the widest value a field can show, so the dialog holds still while the time changes. + const fieldWidths = this.fields.map( + (field) => Math.max(medium.measureText("000"), small.measureText(field)) + 2 * FIELD_PAD_X, + ); + const fieldsWidth = fieldWidths.reduce((sum, width) => sum + width, 0) + FIELD_GAP * (fieldWidths.length - 1); + const boxHeight = medium.lineHeight + 6; + const length = `of ${formatReplayTime(this.maxSeconds * 1000)}`; + const hintsWidth = Math.max(...this.fields.map((_, index) => small.measureText(this.hints(index)))); + const width = Math.min( + base.width, + Math.max(fieldsWidth, hintsWidth, small.measureText(this.options.title), small.measureText(length)) + 2 * PADDING, + ); + const titleHeight = small.lineHeight + 12; + const bodyHeight = boxHeight + 2 + small.lineHeight + 10 + small.lineHeight; + const height = Math.min(base.height, 8 + titleHeight + bodyHeight + 12 + small.lineHeight + 10); + const x = Math.max(0, Math.floor((base.width - width) / 2)); + const y = Math.max(0, Math.floor((base.height - height) / 2)); + + // Fill 1, not 0: 0 is the transparent color key on the shell surface. + image.fillRoundedRect(x, y, width, height, 1); + image.drawRoundedRect(x, y, width, height, 72); + image.drawText(small, x + PADDING, y + 8, this.options.title, 220); + + const top = y + 8 + titleHeight; + let fieldX = x + PADDING; + for (let index = 0; index < this.fields.length; index++) { + const fieldWidth = fieldWidths[index]!; + const active = index === this.field; + if (active) { + if (focused) image.fillRoundedRect(fieldX, top, fieldWidth, boxHeight, ACTIVE_FILL, 6); + image.drawRoundedRect(fieldX, top, fieldWidth, boxHeight, ACTIVE_STROKE, 6); + } + const value = values[index]!; + image.drawText(medium, fieldX + Math.floor((fieldWidth - medium.measureText(value)) / 2), + centeredTextY(medium, top, boxHeight), value, active ? 250 : 170); + const caption = this.fields[index]!; + image.drawText(small, fieldX + Math.floor((fieldWidth - small.measureText(caption)) / 2), + top + boxHeight + 2, caption, active ? 190 : 110); + fieldX += fieldWidth + FIELD_GAP; + } + image.drawText(small, x + PADDING, top + bodyHeight - small.lineHeight, length, 190); + image.drawText(small, x + PADDING, y + height - 10 - small.lineHeight, this.hints(this.field), 110); + return image; + } +} diff --git a/app/apps/transcribe/transcribe-app.ts b/app/apps/transcribe/transcribe-app.ts index f175e818..1bd34659 100644 --- a/app/apps/transcribe/transcribe-app.ts +++ b/app/apps/transcribe/transcribe-app.ts @@ -1,8 +1,11 @@ import { imageFromAsciiArt, type GrayImage } from "../../graphics/image"; import { voiceControlBridge } from "../../native/voice-control"; -import { saveRecordingSetting, toggleSettingMenuItem } from "../../ui/dashboard-settings"; +import { enumSettingMenuItem, saveRecordingSetting, toggleSettingMenuItem } from "../../ui/dashboard-settings"; +import { type ContinuousCaptureOptions } from "../../ui/layers"; import { withRecordingIcon } from "../../ui/listening-header"; +import { TimePickerLayer } from "./time-picker"; import { TranscribeLayer, transcriptTimestamp } from "./transcribe"; +import { transcribeMicCount, transcribeMicsSetting } from "./transcribe-settings"; import { createInProcessWindow, YieldAtRootLayer, @@ -45,8 +48,8 @@ export function micTrayIcon(recording: boolean): GrayImage { } export type TranscribeAppOptions = InProcessAppOptions & { - /** Begin continuous mic capture (kept on while the window is open). */ - startContinuousVoiceCapture: (options?: { recordAs?: string }) => void; + /** Begin (or retarget) continuous mic capture, kept on while the window is open. */ + startContinuousVoiceCapture: (options?: ContinuousCaptureOptions) => void; stopContinuousVoiceCapture: () => void; }; @@ -59,6 +62,10 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc // One timestamp per session names both the audio recording and a saved transcript. const stamp = transcriptTimestamp(); const layer = new TranscribeLayer(stamp); + // At launch, and again when the Microphones setting changes: a running + // capture moves over to the microphones now asked for. + const startCapture = () => + options.startContinuousVoiceCapture({ recordAs: `transcribe-${stamp}`, mics: transcribeMicCount() }); const app = createInProcessWindow({ appId: "transcribe", windowId: TRANSCRIBE_WINDOW_ID, @@ -78,6 +85,7 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc }, // The controller starts and stops the recording as the setting changes. toggleSettingMenuItem(saveRecordingSetting), + enumSettingMenuItem(transcribeMicsSetting, { onChange: startCapture }), ], submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, @@ -97,7 +105,96 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc shell.setTrayIcon(TRAY_ICON_ID, micTrayIcon(voiceControlBridge.recording)); app.requestRender(); }); - options.startContinuousVoiceCapture({ recordAs: `transcribe-${stamp}` }); + startCapture(); shell.setTrayIcon(TRAY_ICON_ID, micTrayIcon(voiceControlBridge.recording)); return app; } + +/** + * Transcribe in replay mode: a window of its own that transcribes a + * recording (a .mka file from Save Recording) in place of the microphones, + * in real time and through the same pipeline. It neither uses the mic nor + * shows the mic tray icon, so it runs beside a live Transcribe window. The + * header shows the position in the recording; a tap pauses and resumes; the + * menu jumps to a time. + */ +export function createTranscribeReplayWindow( + windowId: string, + path: string, + name: string, + options: InProcessAppOptions, +): InProcessWindow | null { + const replay = options.actions.openVoiceReplay?.(path) ?? null; + if (!replay) return null; + // Names a saved transcript after the recording it came from. + const layer = new TranscribeLayer(name.replace(/\.[^.]+$/, ""), replay, name); + const app = createInProcessWindow({ + appId: "transcribe", + windowId, + title: name, + iconLetter: "Tr", + icon: "mic", + closeable: true, + // Relaunching Transcribe after a restart would open the microphone, not this recording. + restoresApp: false, + // Like a playing video: something to watch, not to touch. + keepsScreenOn: () => replay.state === "playing", + actions: options.actions, + baseLayer: new YieldAtRootLayer(layer), + menuItems: () => [ + { + label: "Jump to time", + // The length isn't known until the recording has been opened. + disabled: () => replay.durationMs <= 0, + onSelect: (ctx) => { + ctx.stack.pop(); + ctx.stack.push( + new TimePickerLayer({ + title: "Jump to time", + initialMs: replay.positionMs, + maxMs: replay.durationMs, + onPick: (positionMs) => { + replay.jumpTo(positionMs); + app.requestRender(); + }, + }), + ); + }, + }, + { + label: replay.state === "paused" ? "Resume" : "Pause", + disabled: () => replay.state !== "playing" && replay.state !== "paused", + onSelect: (ctx) => { + ctx.stack.pop(); + replay.setPaused(replay.state === "playing"); + }, + }, + { + label: "Restart from 0:00", + onSelect: (ctx) => { + ctx.stack.pop(); + replay.jumpTo(0); + }, + }, + { + label: "Save", + onSelect: (ctx) => { + ctx.stack.pop(); + layer.saveTranscript(); + }, + }, + ], + submitFrame: options.submitFrame, + setSurfaceVisible: options.setSurfaceVisible, + removeSurface: options.removeSurface, + onClosed: () => { + layer.onRemoved(); + replay.close(); + options.onClosed(); + }, + }); + // Started first, so the layer picks its opening status up as it subscribes. + replay.start(); + layer.start(app.requestRender, () => shell.isWindowVisible(windowId)); + return app; +} diff --git a/app/apps/transcribe/transcribe-settings.ts b/app/apps/transcribe/transcribe-settings.ts new file mode 100644 index 00000000..f9cc68f6 --- /dev/null +++ b/app/apps/transcribe/transcribe-settings.ts @@ -0,0 +1,40 @@ +import { ConfigSettingEnum, glassesTypeSetting } from "../../ui/dashboard-settings"; +import { type MicCount } from "../../ui/layers"; +import { micControlSupported } from "../microphones/mic-control"; + +export type TranscribeMics = "1" | "2" | "4"; + +const MICS_LABELS: Record<TranscribeMics, string> = { + "1": "1 (mixed)", + "2": "2 (one per temple)", + "4": "4 (two per temple)", +}; + +/** Whether the paired glasses can stream their microphones separately (G2, with the microphone channel in use). */ +export function separateMicsSupported(): boolean { + return glassesTypeSetting.get() === "g2" && micControlSupported(); +} + +/** + * How many of the glasses' microphones Transcribe captures. With more than + * one, the temples stream their own microphones (each temple on its own + * clock) instead of the glasses' mixed stream; a recording keeps every one, + * and what is transcribed is a mix made on the phone. + */ +export const transcribeMicsSetting = new ConfigSettingEnum<TranscribeMics>({ + id: "transcribe-mics", + label: "Microphones", + storageKey: "transcribe.mics", + defaultValue: "1", + values: ["1", "2", "4"], + formatValue: (value) => MICS_LABELS[value], + isDisabled: (value) => value !== "1" && !separateMicsSupported(), + description: + "Which of the glasses' microphones to capture. 1 is the glasses' own mixed stream. 2 and 4 stream each temple's microphones separately (custom firmware), so a saved recording has every microphone on its own channel; the transcript then comes from a mix made on the phone.", +}); + +/** The setting as a capture option; 1 where the glasses can't stream their microphones separately. */ +export function transcribeMicCount(): MicCount { + if (!separateMicsSupported()) return 1; + return Number(transcribeMicsSetting.get()) as MicCount; +} diff --git a/app/apps/transcribe/transcribe.ts b/app/apps/transcribe/transcribe.ts index 05e1fef7..78797709 100644 --- a/app/apps/transcribe/transcribe.ts +++ b/app/apps/transcribe/transcribe.ts @@ -3,15 +3,20 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { lineStep } from "../../ui/metrics"; import { MicLevelMeter, paintListeningHeader, RECORDING_FAILED_NOTICE } from "../../ui/listening-header"; import { writeTextToDownloads } from "../../native/file-access"; -import { voiceControlBridge } from "../../native/voice-control"; +import { voiceControlBridge, type VoiceEventSource, type VoiceReplay } from "../../native/voice-control"; import { Layer, type LayerContext } from "../../ui/layers"; import { type InputEvent } from "../../ui/gestures"; +import { formatReplayTime } from "./replay-time"; import { TranscriptModel, TranscriptScroll, transcriptLayout, wrapTranscribeText } from "./transcript-model"; /** * Live transcription view. Continuous mic capture is owned by the app wrapper * (createTranscribeAppWindow); this layer accumulates the transcript, shows * it, and saves it to Downloads from the app menu. + * + * Given a replay, it transcribes that recording instead of the microphone: + * the header shows the position in the recording, a tap pauses and resumes, + * and each jump leaves its time as a line in the transcript. */ export class TranscribeLayer implements Layer { private status = "Starting microphone..."; @@ -24,25 +29,67 @@ export class TranscribeLayer implements Layer { private unsubscribeTranscript: (() => void) | null = null; private unsubscribePause: (() => void) | null = null; private unsubscribeStatus: (() => void) | null = null; + private unsubscribeReplay: (() => void) | null = null; + private readonly source: VoiceEventSource; - /** `stamp` names the saved transcript, matching the session's audio recording. */ - constructor(private readonly stamp: string = transcriptTimestamp()) {} + /** + * `stamp` names the saved transcript, matching the session's audio + * recording. `replay` is the recording to transcribe in place of the + * microphone; `replayName` is what to call it. + */ + constructor( + private readonly stamp: string = transcriptTimestamp(), + private readonly replay: VoiceReplay | null = null, + private readonly replayName = "", + ) { + this.source = replay ? replay.events : voiceControlBridge; + if (replay) this.status = "Opening the recording..."; + } - /** `isVisible` says whether the window is on screen (the level meter repaints only then). */ + /** `isVisible` says whether the window is on screen (the level meter and replay clock repaint only then). */ start(requestRender: () => void, isVisible: () => boolean): void { - this.meter.start(requestRender, isVisible); - this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => { + this.meter.start(requestRender, isVisible, this.source); + this.unsubscribeTranscript = this.source.onTranscript((event) => { this.transcript.accept(event); requestRender(); }); - this.unsubscribePause = voiceControlBridge.onSpeechPause(() => this.transcript.pause()); - this.unsubscribeStatus = voiceControlBridge.onStatus((state) => { + this.unsubscribePause = this.source.onSpeechPause(() => this.transcript.pause()); + // The current status arrives while subscribing, before the window has a + // surface to render to; the window paints it when it is first shown. + let subscribing = true; + this.unsubscribeStatus = this.source.onStatus((state) => { this.status = state.status; this.listening = state.listening; this.detail = state.detail; this.saveNotice = ""; - requestRender(); + if (!subscribing) requestRender(); }); + subscribing = false; + const replay = this.replay; + if (replay) { + let shownSecond = -1; + let shownState = replay.state; + this.unsubscribeReplay = replay.onChange((jumpedToMs) => { + if (jumpedToMs !== undefined) this.transcript.mark(`[${formatReplayTime(jumpedToMs)}]`); + // The clock ticks in whole seconds; repaint when what it shows changed. + const second = Math.floor(replay.positionMs / 1000); + if (jumpedToMs === undefined && second === shownSecond && replay.state === shownState) return; + shownSecond = second; + shownState = replay.state; + if (isVisible()) requestRender(); + }); + } + } + + /** The header's status for a replay: where it is in the recording, and anything worth saying beside that. */ + private replayStatus(replay: VoiceReplay): string { + if (replay.state === "failed" || (replay.state === "opening" && replay.durationMs === 0)) return this.status; + const withHours = replay.durationMs >= 3_600_000; + const clock = `${formatReplayTime(replay.positionMs, withHours)} / ${formatReplayTime(replay.durationMs, withHours)}`; + if (replay.state === "paused") return `${clock} · Paused`; + if (replay.state === "ended") return `${clock} · Finished`; + // Anything but plain listening is worth showing: the model loading, a provider reconnecting. + return this.listening ? clock : `${clock} · ${this.status}`; } paint(ctx: LayerContext): GrayImage { @@ -50,14 +97,19 @@ export class TranscribeLayer implements Layer { const viewport = ctx.stack.getBaseSize(); const image = new GrayImage(viewport.width, viewport.height, 0); - const status = voiceControlBridge.recordingFailed ? RECORDING_FAILED_NOTICE : this.status; + const replay = this.replay; + const status = replay + ? this.replayStatus(replay) + : this.source.recordingFailed ? RECORDING_FAILED_NOTICE : this.status; + const playing = !replay || replay.state === "playing"; const bodyTop = paintListeningHeader( - image, font, "Transcribe", this.saveNotice || status, this.listening ? this.meter : null, viewport.padding, - voiceControlBridge.recording, + image, font, "Transcribe", this.saveNotice || status, this.listening && playing ? this.meter : null, viewport.padding, + this.source.recording, ); const step = lineStep(font) + 2; const layout = transcriptLayout(viewport, bodyTop, font.lineHeight, step); - const text = this.transcript.text || (this.listening ? "Listening..." : this.detail); + const placeholder = replay ? `Replaying ${this.replayName}` : "Listening..."; + const text = this.transcript.text || (this.listening ? placeholder : this.detail); const wrapped = wrapTranscribeText((line) => font.measureText(line), text, layout.textWidth); const firstLine = this.scroll.layout(wrapped.length, layout.pageLines); @@ -82,6 +134,11 @@ export class TranscribeLayer implements Layer { ctx.actions.requestRender(); return; } + if (event.type === "click" && this.replay) { + this.replay.setPaused(this.replay.state === "playing"); + ctx.actions.requestRender(); + return; + } if (event.type === "double-click") { ctx.stack.pop(); } @@ -89,6 +146,8 @@ export class TranscribeLayer implements Layer { onRemoved(): void { this.meter.stop(); + this.unsubscribeReplay?.(); + this.unsubscribeReplay = null; this.unsubscribePause?.(); this.unsubscribePause = null; this.unsubscribeTranscript?.(); diff --git a/app/apps/transcribe/transcript-model.ts b/app/apps/transcribe/transcript-model.ts index af950a70..05befc46 100644 --- a/app/apps/transcribe/transcript-model.ts +++ b/app/apps/transcribe/transcript-model.ts @@ -36,6 +36,18 @@ export class TranscriptModel { if (this.live) this.breakAfterLive = true; else if (this.finalized) this.separator = "\n"; } + + /** + * A line of its own (where a replay jumped to), ending whatever was in + * progress: what follows it starts a new paragraph. + */ + mark(label: string): void { + const before = this.text; + this.finalized = before ? `${before}\n${label}` : label; + this.live = ""; + this.separator = "\n"; + this.breakAfterLive = false; + } } /** An absolute history position, with an explicit follow-tail mode. */ diff --git a/app/assistant/date-format.ts b/app/assistant/date-format.ts new file mode 100644 index 00000000..e7a810fb --- /dev/null +++ b/app/assistant/date-format.ts @@ -0,0 +1,62 @@ +/** + * Date and calendar-event text for the assistant's tool results. Pure (no + * native imports) so it can be unit-tested. + */ + +const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; +const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + +export type FormattableEvent = { + title: string; + startMs: number; + endMs: number; + allDay: boolean; + location?: string; +}; + +export function formatEvent(event: FormattableEvent): string { + const location = event.location ? ` @ ${event.location}` : ""; + return `- ${event.title || "(untitled)"} — ${formatEventSpan(event)}${location}`; +} + +/** + * When an event happens, end included: a multi-day all-day event names its last + * day (all-day ends are exclusive, at the midnight after it), a timed event its + * end time. Without the end, an event that started yesterday and runs through + * tomorrow reads as yesterday's. + */ +export function formatEventSpan(event: Pick<FormattableEvent, "startMs" | "endMs" | "allDay">): string { + const start = new Date(event.startMs); + if (event.allDay) { + const lastDay = new Date(Math.max(event.startMs, event.endMs - 1)); + return sameDay(start, lastDay) + ? `${formatDate(start)} (all day)` + : `${formatDate(start)} – ${formatDate(lastDay)} (all day)`; + } + if (!(event.endMs > event.startMs)) return formatDateTime(start); + const end = new Date(event.endMs); + return sameDay(start, end) + ? `${formatDateTime(start)} – ${formatTime(end)}` + : `${formatDateTime(start)} – ${formatDateTime(end)}`; +} + +export function formatDateTime(date: Date): string { + return `${formatDate(date)}, ${formatTime(date)}`; +} + +export function formatDate(date: Date): string { + return `${WEEKDAYS[date.getDay()]} ${MONTHS[date.getMonth()]} ${date.getDate()}`; +} + +export function formatTime(date: Date): string { + let hours = date.getHours(); + const minutes = date.getMinutes().toString().padStart(2, "0"); + const meridiem = hours >= 12 ? "PM" : "AM"; + hours = hours % 12; + if (hours === 0) hours = 12; + return `${hours}:${minutes} ${meridiem}`; +} + +function sameDay(a: Date, b: Date): boolean { + return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate(); +} diff --git a/app/assistant/system-tools.ts b/app/assistant/system-tools.ts index ccff0c25..4d41bc8a 100644 --- a/app/assistant/system-tools.ts +++ b/app/assistant/system-tools.ts @@ -1,10 +1,11 @@ -import { readUpcomingEventsAsync, type CalendarEvent } from "../native/calendar"; +import { readUpcomingEventsAsync } from "../native/calendar"; import { hasCalendarPermission } from "../native/calendar-permissions"; import { mediaControllerBridge } from "../native/media-controller"; import { spotifyService } from "../native/spotify"; import { dismissNotification, readActiveNotifications } from "../native/notification-icons"; import { shell } from "../ui/shell/shell"; import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry"; +import { formatDateTime, formatEvent } from "./date-format"; /** * Registers the always-available system tools into the registry. Called once at @@ -12,9 +13,6 @@ import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registr * media, notifications); nothing here needs an app window to be open. */ -const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; -const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; - let registered = false; export function registerSystemTools(registry: ToolRegistry = toolRegistry): void { @@ -244,35 +242,12 @@ function describeNowPlaying(): string { return "Nothing is currently playing."; } -function formatEvent(event: CalendarEvent): string { - const when = event.allDay ? `${formatDate(new Date(event.startMs))} (all day)` : formatDateTime(new Date(event.startMs)); - const location = event.location ? ` @ ${event.location}` : ""; - return `- ${event.title || "(untitled)"} — ${when}${location}`; -} - function formatNotification(notification: { key: string; appName: string; title: string; text: string; customText?: string[] }): string { const title = notification.title || notification.customText?.join(" ") || "(no title)"; const body = notification.text ? `: ${notification.text}` : ""; return `- [${notification.appName}] ${title}${body} (key: ${notification.key})`; } -function formatDateTime(date: Date): string { - return `${formatDate(date)}, ${formatTime(date)}`; -} - -function formatDate(date: Date): string { - return `${WEEKDAYS[date.getDay()]} ${MONTHS[date.getMonth()]} ${date.getDate()}`; -} - -function formatTime(date: Date): string { - let hours = date.getHours(); - const minutes = date.getMinutes().toString().padStart(2, "0"); - const meridiem = hours >= 12 ? "PM" : "AM"; - hours = hours % 12; - if (hours === 0) hours = 12; - return `${hours}:${minutes} ${meridiem}`; -} - function clampNumber(value: unknown, min: number, max: number, fallback: number): number { const num = typeof value === "number" ? value : Number(value); if (!Number.isFinite(num)) return fallback; diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 8732bc73..e2acb67f 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -19,9 +19,14 @@ import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, onAndroidNotificationRe import { watchNotificationListenerBinding } from "../native/notification-access"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { defaultRingName, setGlassesRingLink } from "../native/ring-status"; +import { applyGlanceAngle, glassesHeadUpTrigger, syncGlanceAngleOriginal } from "./head-angle"; +import { glanceAngleTriggerDegrees, glanceTapTimeoutMs } from "../apps/glanceboard/glanceboard-settings"; +import { GLANCE_TILT_MAX_MS, glanceTiltMaxMs } from "./glance-state"; import { getBooleanSetting, setBooleanSetting } from "../native/settings-store"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat"; +import { keyboardService } from "./keyboard/service"; +import type { KeyEvent } from "./keyboard/keys"; import { hasExtractedEvenHubFonts } from "./firmware-builder"; import { isHaltedSessionPhase, resumeAutoReconnect, suppressAutoReconnect } from "./reconnect-policy"; import { WearRemote, type WearRemoteInputKind } from "./wear-remote"; @@ -53,7 +58,7 @@ import { type GlanceEvent } from "./glance-state"; import { isPreviewOnlyMode } from "../phone-ui/onboarding-state"; import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from "../apps/onboarding/onboarding-progress"; import { beginRenderPass, endRenderPass } from "../util/render-freshness"; -import { voiceControlBridge } from "../native/voice-control"; +import { VoiceReplay, voiceControlBridge } from "../native/voice-control"; import { voiceActivity } from "../ui/shell/voice-activity"; import { GrayImage } from "../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane"; @@ -76,7 +81,7 @@ import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-p import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence"; import { appViewportRect, isOnSwitcherEdge, screenHeight, screenWidth, sidebarStripVisible, windowEdges, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry"; import { sameEdges } from "../ui/viewport"; -import { type ContinuousCaptureOptions, type LayerActions, type TextSettingsEditToggle, type TranscriptionChoice } from "../ui/layers"; +import { type ContinuousCaptureOptions, type LayerActions, type MicCount, type TextSettingsEditToggle, type TranscriptionChoice } from "../ui/layers"; import { type KeyboardInputSession } from "../ui/shell/keyboard-input"; import { appSwitcherPositionSetting, glassesModelName, glassesTypeSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, effectiveRingConnectionMode, type RingConnectionMode, saveRecordingSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, shouldPopUpNewNotification, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; @@ -98,6 +103,8 @@ import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_W import { isInstalledPackagePresent } from "../apps/evenhub/updates"; import { wearerVerificationOptions } from "../apps/microphones/speakers"; import { micSession } from "../apps/microphones/mic-session"; +import { micArrayCaptureConfig, micControlSupported } from "../apps/microphones/mic-control"; +import { encodeMicControl } from "../apps/microphones/mic-protocol"; import { chargingPreviewMessage, glassesDisplayAsleep, @@ -333,6 +340,7 @@ class DashboardController { private evenHubResumePromise: Promise<boolean> | null = null; /** Faceclaw's firmware at the required revision reported in; its extensions can be used. */ private customFirmwareConfirmed = false; + private lastGlanceAngleDegrees = glanceAngleTriggerDegrees(); private faceclawWakeLeaseState: boolean | null = null; /** Ring connection mode the current session was started with (it applies per connection). */ private ringModeAtConnect: RingConnectionMode = "glasses"; @@ -410,6 +418,7 @@ class DashboardController { startContinuousVoiceCapture: (client: string, options?: ContinuousCaptureOptions) => this.startContinuousVoiceCapture(client, options), stopContinuousVoiceCapture: (client: string) => this.stopContinuousVoiceCapture(client), + openVoiceReplay: (path: string) => this.openVoiceReplay(path), playBuzzerSequence: (payload: Uint8Array) => this.playBuzzerSequence(payload), }; this.sharedActions = sharedActions; @@ -457,6 +466,7 @@ class DashboardController { if (on) this.glance.dismiss(); this.handleScreenStateChanged(on); if (on) this.requestShellRender(); + if (on) keyboardService.retryNow(); this.emit(); }, }); @@ -488,6 +498,7 @@ class DashboardController { if (!resized && !appsResized) this.applyWindowEdgesIfChanged(); this.syncAssistantBridgeIfChanged(); this.syncLockScreenSettingIfChanged(); + this.applyGlanceAngleIfChanged(); this.syncContinuousRecording(); // A Preview color or scaling change should show on the mirror at once, // not at the next composited frame (cheap: floor-limited, dedup'd on @@ -509,6 +520,29 @@ class DashboardController { assistantAvailable: () => shell.isAssistantAvailable(), assistant: text => shell.sendToAssistant(text), }); + // A keyboard paired through the glasses (CFW keyboard bridge). The + // service decides what each key means (app/g2/keyboard/key-router.ts); + // this is how it reaches the display and the input pipeline. + keyboardService.setHost({ + ready: () => this.phase === "connected", + locked: () => this.glassesLocked, + wake: () => { + void this.handleInputEvent({ + kind: "display-wake", + containerName: "", + eventType: OsEventTypeList.DOUBLE_CLICK_EVENT, + eventSource: EventSourceType.TOUCH_EVENT_FORM_DUMMY_NULL, + systemExitReasonCode: 0, + frameId: 0, + }).catch((error) => this.appendLog(`keyboard wake failed: ${this.formatError(error)}`)); + }, + gesture: (gesture, origin) => this.injectSyntheticRingInput(gesture, origin), + log: (line) => this.appendLog(line), + // A keyboard that is switched off would otherwise keep the glasses' + // radio looking for it around the clock. One still connected when the + // display sleeps stays connected, and its next key wakes the display. + shouldReconnect: () => this.phase === "connected" && shell.isScreenOn(), + }); this.wearRemote = new WearRemote({ apps: launchableApps, injectInput: (kind) => this.injectSyntheticRingInput(kind, "watch"), @@ -1655,6 +1689,8 @@ class DashboardController { if (mappedPhase === "connected") { this.syncEvenHubScreenOffSetting(); this.ensureWearStateTracking(); + // The session reapplies it on reconnects; restate it for a new session. + applyGlanceAngle(); this.maybeWakeAfterChargingReconnect(); } else if (mappedPhase !== "charging") { this.cancelEvenHubSuspendTimer(); @@ -1763,6 +1799,7 @@ class DashboardController { this.appendLog(`wake takeover lease sync failed: ${this.formatError(error)}`); }); this.ensureWearStateTracking(); + this.syncKeyboardLink(); } // The shared session halts on incompatible firmware by itself // ("incompatible-firmware" phase); this only explains why. @@ -1816,6 +1853,7 @@ class DashboardController { this.requestShellRender(); this.updateCompositePreview(); this.updateConnectedForegroundNotification(); + syncGlanceAngleOriginal(); }, SHELL_REFRESH_INTERVAL_MS); this.previewTimer = setInterval(() => this.updateCompositePreview(), PREVIEW_INTERVAL_MS); this.screenTimeoutTimer = setInterval(() => { @@ -2253,6 +2291,30 @@ class DashboardController { return undefined; } + /** + * How many of the glasses' microphones the continuous-capture windows call + * for (the most any asks for), where the glasses can stream them + * separately: G2 with the microphone channel in use. Otherwise 1, the + * mixed stream. + */ + private continuousMics(): MicCount { + if (glassesTypeSetting.get() !== "g2" || !micControlSupported()) return 1; + let mics: MicCount = 1; + for (const options of this.continuousClients.values()) { + if ((options.mics ?? 1) > mics) mics = options.mics!; + } + return mics; + } + + /** + * A recording as a capture of its own (see VoiceReplay), transcribed with + * the provider and processing settings a microphone capture would get. + * "My voice only" is left out: a recording is of whoever spoke. + */ + private openVoiceReplay(path: string): VoiceReplay { + return new VoiceReplay(path, () => ({ ...this.voiceCaptureOptions(null), speakerVerification: undefined })); + } + /** * Continuous capture's claim on the mic (the Microphones app and EvenHub * mic apps stand down while it is held) follows whether the capture really @@ -2278,9 +2340,26 @@ class DashboardController { // Otherwise only Whisper server is told a language (its own setting). const choice = this.transcriptionChoice(); const provider = choice?.provider ?? voiceProviderSetting.get(); + // Separate microphones only come from connected glasses. + const mics = communicator ? this.continuousMics() : 1; + const micConfig = mics === 1 ? null : micArrayCaptureConfig(mics); return { communicator: communicator?.getNativeCommunicator() ?? null, usePhoneMic: communicator === null, + micArray: micConfig ? { configure: encodeMicControl("configure", micConfig) } : null, + // Stored in a recording of the capture (see Save Recording). + captureInfo: { + glasses: communicator ? glassesModelName() : null, + mics, + micConfig: micConfig && { + source: micConfig.source, + channelMask: micConfig.channelMask, + codec: micConfig.codec, + format: micConfig.format, + sampleRateHz: micConfig.sampleRateHz, + directionDetection: micConfig.beamform, + }, + }, provider, language: choice ? choice.language : provider === "whisper-server" ? whisperServerLanguageSetting.get() : "", elevenLabsApiKey: elevenLabsApiKeySetting.get(), @@ -2463,6 +2542,16 @@ class DashboardController { frameTimings.logFrame(frameId, `TS input handler start: ${event.kind} ${eventLabel(event.kind, event.eventType)}`); let frameOwned = false; try { + if (event.kind === "sys-event" && event.eventType === OsEventTypeList.HEAD_DOWN_EVENT) { + // Only the Glanceboard cares (it ends a head-tilt showing); never + // shell input, so it can't count as activity or wake anything. + const glanceEvent = this.glance.eventForGesture("head-down"); + if (glanceEvent) { + await this.glance.handleEvent(glanceEvent, frameId); + frameOwned = true; + } + return; + } const inputEvent = rawInputEventToInputEvent(event); if (!acceptInput(inputEvent)) return; // The gesture, plus which app is on screen and whether input goes to it, @@ -2581,15 +2670,49 @@ class DashboardController { } } + /** Re-send the head-tilt angle when it changed (the setting, from either settings UI, or the Glanceboard toggle). */ + private applyGlanceAngleIfChanged(): void { + const degrees = glanceAngleTriggerDegrees(); + if (degrees === this.lastGlanceAngleDegrees) return; + this.lastGlanceAngleDegrees = degrees; + applyGlanceAngle(); + } + + /** + * Tell the keyboard bridge whether the session can host a keyboard: a G2 + * on firmware with the bridge, connected and in use. While the glasses + * charge in their case the link stays up but nobody is typing, so the + * firmware is told to let the keyboard go. + */ + private syncKeyboardLink(): void { + const up = + this.phase === "connected" && + this.customFirmwareConfirmed && + glassesTypeSetting.get() === "g2"; + // Charging, or a disconnect the user asked for: the link is still up, so + // the DISABLE gets out ahead of everything the teardown sends. + if (up) keyboardService.linkReady(); + else keyboardService.linkLost(this.phase === "charging" || this.phase === "disconnecting"); + } + /** * The Glanceboard event a sleep-time input means, or null when the shell * should see the input as usual. A display-wake carries the head-tilt - * gesture (HEAD_UP_EVENT) as a press; the double-tap display-wake stays the - * regular UI's wake. + * gesture (HEAD_UP_EVENT) as a tilt, which lasts until the head-down; 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; + if (event.eventType !== OsEventTypeList.HEAD_UP_EVENT) return null; + const glanceEvent = this.glance.eventForGesture("head-tilt"); + if (glanceEvent?.type !== "tilt") return glanceEvent; + const maxMs = glanceTiltMaxMs({ + capMs: glanceEvent.maxMs === undefined ? GLANCE_TILT_MAX_MS : glanceEvent.maxMs, + resumedSession: this.evenHubSessionSuspended || this.evenHubResumePromise !== null, + triggerDegrees: glassesHeadUpTrigger() ?? (glanceAngleTriggerDegrees() || null), + tapTimeoutMs: glanceTapTimeoutMs(), + }); + return { type: "tilt", maxMs }; } return this.glance.eventForGesture(inputEvent.type); } @@ -2688,6 +2811,7 @@ class DashboardController { } }, endTextSettingEdit: () => this.endTextSettingEdit(), + unhandledKey: (event) => keyboardService.handleUnhandledKey(event as KeyEvent), }); this.appHosts.set(appId, host); return host; @@ -2721,10 +2845,11 @@ class DashboardController { if (!this.openAppsRestored || this.suppressOpenAppsPersist) return; const open: string[] = []; for (const window of shell.getWindows()) { - if (window.appId === "launcher") continue; + if (window.appId === "launcher" || window.restoresApp === false) continue; if (!open.includes(window.appId)) open.push(window.appId); } - savePersistedOpenApps({ open, foreground: shell.foregroundWindow()?.appId ?? null }); + const foreground = shell.foregroundWindow(); + savePersistedOpenApps({ open, foreground: foreground && foreground.restoresApp !== false ? foreground.appId : null }); } /** @@ -3052,6 +3177,7 @@ class DashboardController { private setPhase(phase: ConnectionPhase): void { if (this.phase === phase) return; this.phase = phase; + this.syncKeyboardLink(); if (phase !== "connected" && phase !== "charging") { shell.setBatteryLevels({ ring: null, ringCharging: null }); } diff --git a/app/g2/even-advertisement.ts b/app/g2/even-advertisement.ts index 341cd286..75f0397e 100644 --- a/app/g2/even-advertisement.ts +++ b/app/g2/even-advertisement.ts @@ -68,6 +68,23 @@ export function extractG2Mac(mfg: Uint8Array): string | null { return humanReadableAddressFromWireBytes(mfg.subarray(16, 22)); } +/** + * G2 manufacturer data (hex) carrying `serial` and `address`, for an arm known + * only from a saved pairing: what `extractG2Serial` and `extractG2Mac` read + * back, with the serial zero-padded and no flag byte. Null when the serial is + * not up to 14 printable ASCII characters or the address is not a MAC. + */ +export function g2ManufacturerData(serial: string, address: string): string | null { + const wire = wireBytesFromHumanReadableAddress(address); + if (!wire || !/^[\x20-\x7e]{1,14}$/.test(serial)) return null; + const bytes = new Uint8Array(22); + bytes[0] = 0x45; + bytes[1] = 0x52; + for (let i = 0; i < serial.length; i++) bytes[2 + i] = serial.charCodeAt(i); + bytes.set(wire, 16); + return bytesToHex(bytes); +} + /** Preserve the final G2 manufacturer-data byte without guessing its meaning. */ export function extractG2AdvertisementFlag(mfg: Uint8Array): number | null { if (mfg.length < 23 || !hasEvenManufacturerSignature(mfg)) return null; diff --git a/app/g2/events.ts b/app/g2/events.ts index 1b49436c..a8f7b5ed 100644 --- a/app/g2/events.ts +++ b/app/g2/events.ts @@ -79,6 +79,12 @@ export const OsEventTypeList = { HEAD_UP_EVENT: 12, /** Faceclaw/18: R1 touch-down, before click/hold/slide recognition. */ RING_PRESS_EVENT: 14, + /** + * Faceclaw/38: the IMU head-down (the head back below the head-up + * threshold minus 20 degrees), forwarded while a page is up. Ends a + * head-tilt Glanceboard; nothing else uses it. + */ + HEAD_DOWN_EVENT: 15, } as const; export const OsEventTypeName: Record<number, string> = { @@ -96,4 +102,5 @@ export const OsEventTypeName: Record<number, string> = { 11: "SHORT_THEN_LONG_PRESS_EVENT", 12: "HEAD_UP_EVENT", 14: "RING_PRESS_EVENT", + 15: "HEAD_DOWN_EVENT", }; diff --git a/app/g2/firmware-compat.ts b/app/g2/firmware-compat.ts index 3d8508e2..31eac92b 100644 --- a/app/g2/firmware-compat.ts +++ b/app/g2/firmware-compat.ts @@ -26,7 +26,7 @@ export type FirmwareInfo = { }; /** The Faceclaw firmware revision this build of the app needs. */ -export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 38; +export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 40; const FACECLAW_PREFIX = "Faceclaw/"; const LEGACY_PREFIX = "EVENCFW"; diff --git a/app/g2/firmware/cfw-patches.ts b/app/g2/firmware/cfw-patches.ts index 86f17ae6..d3c00de1 100644 --- a/app/g2/firmware/cfw-patches.ts +++ b/app/g2/firmware/cfw-patches.ts @@ -19,12 +19,42 @@ export type FirmwarePatchSet = { export const CFW_PATCH_SET: FirmwarePatchSet = { "base": "g2_2.3.0.24.bin", "baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979", - "outputSha256": "4aac023f49ec6965186d9106e328a516f20a2915ab400d561f662f984023a432", + "outputSha256": "2a31417f04cec16ecd4e4c3e575d89ce77fb3c5386b47cdb49b9753aaefb1c32", "patches": [ + { + "offset": 1379947, + "old": "0ef009fa", + "new": "08f3a6fc", + "desc": "DM callback allocation: retain keyboard lifecycle on queue exhaustion" + }, + { + "offset": 1380027, + "old": "0ef0e1f9", + "new": "08f304fc", + "desc": "ATT callback allocation: expose lost keyboard events as relay gaps" + }, + { + "offset": 2649417, + "old": "0a681279", + "new": "d2f155fc", + "desc": "SMP Pairing I/O capability: KeyboardDisplay only for owned keyboard" + }, + { + "offset": 2651811, + "old": "0a681279", + "new": "d1f1a8ff", + "desc": "SMP Pairing I/O capability: KeyboardDisplay only for owned keyboard" + }, + { + "offset": 1385975, + "old": "38b584b0", + "new": "06f3c2bf", + "desc": "BLE app dispatcher: isolate explicitly enabled keyboard connection" + }, { "offset": 1045797, "old": "38b584b0", - "new": "5cf371bb", + "new": "5bf309ba", "desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease" }, { @@ -36,49 +66,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = { { "offset": 1437923, "old": "fef788ff", - "new": "fdf2a2f8", + "new": "fbf23aff", "desc": "cfw_ancs_event" }, { "offset": 1437951, "old": "fef7e8fb", - "new": "fcf2d0fd", + "new": "fbf268fc", "desc": "cfw_ancs_open" }, { "offset": 1437969, "old": "fef7e7fb", - "new": "fcf2ebfd", + "new": "fbf283fc", "desc": "cfw_ancs_close" }, { "offset": 1388387, "old": "39694d00", - "new": "25597d00", + "new": "55467d00", "desc": "connection-bound ANCS EUS write callback" }, { "offset": 971497, "old": "fff717f8", - "new": "6ef3a1fc", + "new": "6df339fb", "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { "offset": 971845, "old": "fef769ff", - "new": "6ef3f3fb", + "new": "6df38bfa", "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { "offset": 979579, "old": "fdf74ef8", - "new": "6cf3d8fc", + "new": "6bf370fb", "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { "offset": 1428895, "old": "f8f76cfd", - "new": "fbf250fe", + "new": "f9f2d0fb", "desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)" }, { @@ -102,7 +132,7 @@ export const CFW_PATCH_SET: FirmwarePatchSet = { { "offset": 1058287, "old": "fff754fb", - "new": "59f342fb", + "new": "58f3daf9", "desc": "bl ble_classify_link (only a <= 11.25 ms / latency-0 link is 'already fast')" }, { @@ -111,148 +141,184 @@ export const CFW_PATCH_SET: FirmwarePatchSet = { "new": "5ff43332", "desc": "reserve final 1 KiB of primary TLSF arena for CFW context anchor" }, + { + "offset": 1130241, + "old": "5ff44d22", + "new": "5ff40622", + "desc": "LVGL arena size 0xcd000 -> 0x86000 (fixed CFW buffers follow it)" + }, + { + "offset": 2697223, + "old": "9c212020", + "new": "9c192320", + "desc": "EvenHub arena base 0x2020219c -> 0x2023199c" + }, + { + "offset": 2697133, + "old": "5ff4e121", + "new": "5ff48221", + "desc": "EvenHub arena clear length 0x70800 -> 0x41000" + }, + { + "offset": 2697149, + "old": "5ff4e121", + "new": "5ff48221", + "desc": "EvenHub TLSF pool size 0x70800 -> 0x41000" + }, + { + "offset": 2245731, + "old": "5ff4e125", + "new": "5ff48225", + "desc": "EvenHub heap total in the stock usage log 0x70800 -> 0x41000" + }, + { + "offset": 4394495, + "old": "9b212020", + "new": "9bb11b20", + "desc": "MPU region 6 (non-cacheable) ends with the shrunk LVGL arena" + }, { "offset": 1248487, "old": "d4f712fb", - "new": "29f344fd", + "new": "27f3a0f9", "desc": "bl settings_send_wrapper (append caps field 100)" }, { "offset": 1246183, "old": "f5f7e2f8", - "new": "29f3e8fe", + "new": "27f3befb", "desc": "bl settings_decode_wrapper (Faceclaw lease field 101)" }, { "offset": 975219, "old": "02f083fc", - "new": "6bf34aff", + "new": "69f320fc", "desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)" }, { "offset": 975411, "old": "02f023fc", - "new": "6bf3b8ff", + "new": "69f38efc", "desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)" }, { "offset": 975103, "old": "07f085fb", - "new": "6df3a6fc", + "new": "6cf33efb", "desc": "bl headup_gate (idle input gate: tap/long/release -> field-102 events, stock result kept)" }, { "offset": 830549, "old": "19f03efb", - "new": "90f37bfe", + "new": "8ff313fd", "desc": "bl gesture_ring_press_mode (ring touch-down 14, head up 12 / down 15; stock dispatch preserved)" }, { "offset": 830795, "old": "1ff071ff", - "new": "90f334fe", + "new": "8ff3ccfc", "desc": "bl gesture_press (owned event 9 before stock menu gate)" }, { "offset": 830887, "old": "1ff043ff", - "new": "90f32efe", + "new": "8ff3c6fc", "desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)" }, { "offset": 831763, "old": "1ff08dfd", - "new": "90f364fc", + "new": "8ff3fcfa", "desc": "bl gesture_release (owned event 10 before stock menu gate)" }, { "offset": 1570209, "old": "7fb50600", - "new": "dbf2d5b8", + "new": "d8f231bd", "desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline" }, { "offset": 1051545, "old": "f6f787f9", - "new": "55f3c9f8", + "new": "54f359fe", "desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)" }, { "offset": 1051861, "old": "f6f7e9f8", - "new": "55f32bf8", + "new": "54f3bbfd", "desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)" }, { "offset": 1262237, "old": "d9f7d0fd", - "new": "25f3bffd", + "new": "23f3edfa", "desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)" }, { "offset": 1262337, "old": "d9f79efd", - "new": "25f38dfd", + "new": "23f3bbfa", "desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)" }, { "offset": 1309405, "old": "67f02dff", - "new": "19f35bfe", + "new": "17f397fb", "desc": "bl compass_decode_capture (sample-matched GAF diagnostics)" }, { "offset": 1307865, "old": "fff7c9fb", - "new": "1af3ddf9", + "new": "17f319ff", "desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)" }, { "offset": 1300937, "old": "00210170", - "new": "1bf3bbfe", + "new": "19f3f7fb", "desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)" }, { "offset": 4537963, "old": "", - "new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc7b04ff29c5bc2f2290b4ff6ff754ff47d7400274ff0010847f67f7a7f261191cde90f3007e000bf48f2a351c0f24501bff32f8f8847dbf80090bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14068b0f57d7f00f096804ff47d7006f004fd002800f0cb814ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff010160446080f80e80a0f8c4a180f8c361058445848584c584058545858585c585058645868586c586058745878587c5875be8001f49457ff452afbff35f8f4be8000121b15be8001f4945f8d047e74ff47d7800274ff0010a47f67f797f2607e000bf48f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f098804ff47d7006f042fc002800f009814ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff010160c0f8048080f80ea0a0f8c49180f8c361058445848584c584058545858585c585058645868586c586058745878587c5875be8001fa1427ff451afbff35f8f00bf4be8000121b15be8001fa142f8d045e7d0f8948242f6f76ac0f2440ab8f1000fbff35f8f4ad00af19a0240464ff0ff314ff0ff379047002840f090811198002861d010980778a7f1170002285bd811980c2840f07d81109c6078a178084300f07781e0780138022800f27281a279e379667a42ea0322237a207942ea034242ea0663a27ae67a6179192f42ea062200f0de81172f40f0f481002a40f05981fe2800f0b086812800f0a386002840f0b68651ea030040f04c8100f000bf04460020d047e8b104f5257151e8008fb8f1000f0cd1bff35f8f00bf41e80002002a00f0e58051e8008fb8f1000ff5d00021bff32f8f0029bff35f8f00f0d480804692e74ff0ff372ae147f6df40c0f24f00cdf83880804700274ff47d784ff0010947f67f744ff07f0a1397adf8507007e000bf48f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f09f804ff47d7006f0f8fa002800f097804ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff010160c0f8048080f80e90a0f8c44180f8c3a1058445848584c584058545858585c585058645868586c586058745878587c5875be8001fb1427ff451afbff35f8f00bf4be8000159b15be8001fb142f8d045e70af5a9718847bce6012120e70020109f42f6f76addf83880c0f2440a9fb1119989b1397801f07f02b2f10b0418bf0124b2f11c0618bf01268a2924dc0b2926d01c2924d03ce00f9012a803f045ff4ff0ff3701249df85100002812a818bf4ff0ff3704f05ff8054644b99df8500028b94af21f30c0f247004c3080470f98002818bf056114e09c2901d08b2918d168b14af21f31c0f247010746884797f8681121b997f824133846109f49b14ff0ff370af5807140468847384647b0bde8f08f0f9012a8344003f007ff387800f07f061e2e05d11199384607f094fe0746b9e71d2e00f07e0018bf1a2819d1dbf800100322cff680720a40b2f1005fbff35f8f12d10a684ef66c63ccf2ff039a420bd14a68b2f57d7f04bf002281f8a12303e0a6f115010129e1d90f2e2cdd142e40f31082152e00f05d83162e00f069831f2e40f02c841198012840f0fc8438781f2840f0f88445f64d55c0f24605a847012841f0828140f24c71c0f2010105f5e2777944e02029220023b8470746002818bf4ff0ff3766e7092e00f3fe80052e00f03e82072e40f0028400274ff0010947f67f764ff07f080d943ae000294ff0ff3708bf002b7ff47caf1328bff479af102a4ff0000138bf01211228b2fa82f218bf510901297ff46caf00f01ebd182f4ff0ff3708bf00297ff463af002b7ff460af352800f30884002800f0f684012800f00285312840f0f484082abff451af00f003bd48f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f05a864ff47d7006f035f9002800f052864ff60c41cff6ff7142182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff0101604ff47d71416080f80e90a0f8c46180f8c381058445848584c584058545858585c585058645868586c586058745878587c5875be8001fa1427ff451afbff35f8f4be80001002900f0c6855be8001fa142f6d044e70a2e00f00a820b2e40f00483dbf800500320cff680702840b0f1005fbff35f8f41f05e8028684ef66c61ccf2ff01884241f0568068680027b0f57d7f7ff446aed5f8900342f68d49c0f244090028a5f8a07318bfc8470021a5f8d013c5f8cc13c5f86c1185f86a1185f86811d5f87001c5f8641185f86911c5f87011c5f82013a5f88c1320b148f2a351c0f245018847d5f89802002818bf03f0deff0026c5f89862d5f8880320b148f2a351c0f245018847c5f88863a5f88c63686ca5f8486040b1c847686c09f166018847002804bf002068644bf63940c0f251008047dbf800700320cff680703840b0f1005fbff35f8f48d138684ef66c61ccf2ff01884241d17868b0f57d7f3dd197f87b014ff00008c7f8848100b387f87b8197f8750149f259620138b0fa80f04109c0f2590240f20b1090474ef6e37610f1800fd8bf6ff07f00c0f25a067e28a8bf7e2087f8c401b04706f1bc008047d7f8880187f8748142f6f76addf83880c0f2440a50b1c847d7f8880109f166018847002804bf0020c7f88801dbf800000321cff680710140b1f1005fbff35f8f40f03b8701684ef66c62ccf2ff02914240f033874168b1f57d7f40f02e8790f8cc11002900f0238706f0cbfb00f025bf102e00f05581112e40f01f821198022840f0ef823878112840f0eb827878002840f0e78245f64d50c0f24600804701284ff000077ff465ad40f20830c0f210003790ff208df8d80044f25220c0f20100359040f2c960c0f24800cde93877804798b148f27e30c2f2070000880122c1b264298df8d72008d80721b0f5807f38bf03218df8d7108df8d80040f2d260adf8e000052037ae8df8e200f01d35a90522fff713f94ff22507c0f247070120092132460c23b847074600f0b9be11980d944ff47d7800244ff0010a47f67f797f2602282cbf7878ff200c9006e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f09f814ff47d7005f027ff002800f084844ff60c41cff6ff7142182e3182f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f8194482f81a4482f81b4482f81c4482f81d4482f81e4482f81f4482f8204482f82144a0d14ef66c61ccf2ff010160c0f8048080f80ea0a0f8c49180f8c361058445848584c584058545858585c585058645868586c586058745878587c5875be8001fb9427ff452afbff35f8f4be80001002900f00c815be8001fb942f6d045e711980228c0f0cd8100f06ffe002800f0c8817e78002e00f08d83022e00f2c18140f0b585014611980628c0f03185109fb878fa783b797f7940ea022043ea072232283790389280f09a85322000f09dbd1199781c013907f05afb0746fff729bc1199384606f07ffb0746fff722bc119801380228c0f095817878b9784ff0ff3740ea0120b0f5007f3ff613ac0321119a01eb400191427ff40cac002700288df8dc708df8dd708df8de708df8df708df8e0708df8e1708df8e2708df8e3708df8e4708df8e5708df8e6708df8e7708df8e8708df8e9708df8ea708df8eb708df8ec708df8ed708df8ee708df8ef708df8f0708df8f1708df8f2708df8f3708df8f4708df8f5708df8f6708df8f7708df8f8708df8f9708df8fa708df8fb708df8fc708df8fd708df8fe708df8ff708df800718df801718df802718df803718df804718df805718df806718df807718df808718df809718df80a718df80b718df80c718df80d718df80e718df80f718df810718df811718df812718df813718df814718df815718df816718df817718df818718df819718df81a718df81b713ff488ab109937aa03314ff0010c4b78012b00f2f7800f7847ea0323db08d65c07f007070cfa07f7374240f0eb80d65c013847ea060701f10201d754e7d10d9404f04afe002800f06d844ff47d7800244ff0010a47f67f797f2608e31d2e60d11199781c013907f071ff0746fff752bb90f8481042f6f76addf83880109fdde90c64c0f2440a61b1416c39b142f68d42c0f244020546084690472846002180f848100121119a032a1ad3ceb9b878082800f23a854bf63941c0f25101963188470027fff727bb362800f00681462800f00081572840f0ec80002e7ff449abfbe0119a052ac0f0d580012e40f0d280b8783e46411e06294ff000073ff60babf17803293ff607ab3279002a3ff403ab4bf63943c0f2510303f59a7398470027fff7f9ba1a2818bf1c2e6cd104f0d5fd002868d000f00bfd814608f0fef9b9f1000f4ff0ff3718bf00283ff4e4aa834647f63400c2f20700d0f81806d9f894131b2ea0eb0100c9f89c0300f0a2831a2e40f0c383109900254f1c11994ff4f072a1f1010a4ff4a071cde93d5037a84ff420764ff48058cde90950cde901214846394652465b46cde937853f95cde90755cde90555cde90355009608f0fefc002840f09883d9f89c033946cde93e0537a80a90012009904ff4a07002904ff4f0700190484652465b464ff42077cde937853d95cde90755cde90555cde90355009708f0dbfc61e54ff0ff37fff784ba0c294ff0ff377ff4adaab3f5005f5dd3fff7a8ba481e1f284ff0ff373ff6a2aac1f51630834251d9fff79cba002b7ff497aa421f18bf0122b0f1350318bf01231a40b0f1590318bf01231a40b0f1470318bf0123134208bf012937d0b0f1370218bf0122b0f1c10318bf01231a4208bf02292bd09f284ff0ff377ff473aa042924d0fff76fba022e40f044814bf63940c0f2510080470027fff736ba022abff461aa13e0002a7ff45daaa0f1fd0102290cd397287ff456aa08e06ff0780002427ff450aa02e0402abff44caa45f64d50c0f24600804710990128c9784ff0010008bf022008424ff000073ff43baacdf838804ff47d7847f67f797f2406e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f096804ff47d7005f019fc002800f030814ff60c41cff6ff7142182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ef66c61ccf2ff0101600121c0f804808173a0f8c49180f8c341058445848584c584058545858585c585058645868586c586058745878587c5875be8001fb1427ff452afbff35f8f4be8000121b15be8001fb142f8d047e74af21f35c0f247050446a84794f86801ddf83880109f00287ff46ba994f82403214600287ff465a93878172812d0d1f8840320b904f05ff9c4f8840360b342f68d41c0f24401a1f140024ff47a719047214608bb01f226300e4639460c22fef700fd0f98002186f825030120bff35f8f86f824034ff4f07001904ff4207005f5106600900020002200230027b04700283ff431a984f8247305f14c0080474ff0ff37fff728b9119a072ac0f05581032e40f05281b878fa783e4640ea022039797f79b57901284ff0010698bf304644f62062904228bf10464bf63942c0f2510202f5e272642928bf6421904747f63400c2f207000068002800f0d98247ea0521012988bf0e4642f68d41c0f24401a1f14002314690470027fff7bfb8002780f8477145f64d50c0f24600804701287ff4b4a843f2a310c0f2560080470746fff7acb8ddf838804ff0ff37fff7d4b800201199dde90d48022910992cbf4978ff21002800f0a48202294ff000072cd0012926d000297ff491a84cf67e21caf2101187778783cbf8041087818760058445848584c584058545858585c585058645868586c586058745878587c58780f8407080f8d17380f8d073c0f8cc73fff76db801218173fff769b88773fff766b842f6f76addf83880109fdde90c6400210020c0f2440a21e548f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66c62ccf2ff02914204d14168b1f57d7f00f0e0804ff47d7005f06efa002800f039814ff60c41cff6ff7142182e3182f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f8194482f81a4482f81b4482f81c4482f81d4482f81e4482f81f4482f8204482f82144a0d14ef66c61ccf2ff010160c0f8048080f80ea0a0f8c49180f8c361058445848584c584058545858585c585058645868586c586058745878587c5875be8001fb9427ff452afbff35f8f4be8000100294dd05be8001fb942f7d046e780461198a14603284ff0000738d3301fb0fa80f04009084032d01198109a4cf6cd410338ccf6cc419578a0fb0101b5eb910f88bf8d082e46302d28bf302608f14a00d11c06eb8602fef75ffb002788f8486088f849709db1d8f8440038b9404600f0a7fa0028c8f8440000f07c8142f68d41c0f24401a1f1400201219047002742f6f76addf83880c0f2440a4c46fef758bfd0f870110027002900f0b0804ff400624ff0ff3c0df1dc0e04e033681a44b2f5403f50d28e18b568b7eb552ff5d1ec081ef8044005f0070324fa03f3db071ebf41f82570c6f808c0f760e6e71198032840f0c48010994ff4f077487889785a4640ea012600200890cde90600cde90400cde902004ff4a0700190484631464ff42073009707f06aff002800f01e814ff0ff37a3e01198012840f0a08009f57970bff35f8f50e8001f013140e80012002af8d113a94846012207f08afffff7d2b94ff0ff3752e04ff0000c4ff400634ff0000ec0f820c305e077463344b3f5403fbe4642d2cf18ba68ce580132f4d1f418220c022a2fd80a19956801352bd115682e443e6012681444220c022a23d80a19956801351fd115682e443e6012681444220c022a17d80a199568013513d115682e443e6012681444220c022a0bd80a199568013507d115682e443e6012681444b4f5403fcfd3bef1000fc7f804c00cbfc0f82033cef80430b7e7002742f6f76adde90d48c0f2440afef79abeb0f5fa6f084603d94ff4fa60379008460121054680f8471145f64d50c0f24600804701281bd143f2a318c0f25608a8f188008047022e074608bf002f01d057b90de04bf2110237a9c0f24b020220904728b10746c047002085f8470100e0002742f6f76addf83880c0f2440afef762be90f8cd11002918bf05f0a4fcdbf800000321cff680710140b1f1005fbff35f8f24d101684ef66c62ccf2ff0291421dd14168b1f57d7f19d1d0f8d813b1b14ff00041bff35f8fc0f8d4134af21f30c0f2470000f510674ff4f07001904ff4207600200021002200230096b84795f84701002785f8477158b145f64d50c0f246008047012804d143f2a310c0f256008047d5f8400195f84661c5f83c7185f84671a5f8447150b1c847d5f8400109f166018847002804bf0020c5f840010120a87356b14af27366c0f246060021002200230027b047fef7f6bd0027fef7f3bd701ca9f88c03002783e74bf639400027c0f2510088f84870804783e600bf2de9f04f81b04ef66c644ff29c55ccf2ff04c2f229054ff6ff7600274ff0010a47f67f7b4ff07f0807e000bf48f2a351c0f24501bff32f8f8847d5f80090bff35f8f2868bff35f8f01466ff39601b1f1005f07d10168a14204d14168b1f57d7f00f095804ff47d7004f0e8ff002800f08d804ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ff47d710460416080f80ea0a0f8c4b180f8c381068446848684c684068546858685c685068646868686c686068746878687c68755e8001f49457ff45aafbff35f8f45e8000129b155e8001f4945f8d04fe7002001b0bde8f08f70b500284dd001684ef66c62ccf2ff02914218bf70bd90f8481090f849504bf6394c8d42c0f2510c2ad205eb8501014491f84d2091f84e4091f84ae091f84b3091f84c1042ea0424012c4ff0010298bf14466e1c054680f84960b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84810bde870406047e047686c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04fcdb04ff29c57c2f229073c6840f2030b20464ef66c696ff39600cff6807bccf2ff09b0f1005fbff35f8f40f0e9812068484540f0e5816068b0f57d7f40f0e08194f8710347f64c65c2f2070504f5497628b347f61800c2f20700016d4bf67c52c0f2720291421ad1406cc0b1d4f87c032968401a012802db03f05cff78b93046b8460df02bf950b142f69570c0f25b0080474746002084f8720384f8710394f8700368b1d4f880032968401a012802db03f040ff20b9002084f8700384f86a013078bff35f8f70b194f86fa3ac46baf1000f00961cd094f8250394f83e13091a18bf012115e094f87103002840f0888194f87003002840f08381d4f88403002800f07e8142f68d41c0f24401884777e1012194f8270394f83133059094f8280394f83d23069094f8265394f82a0394f82b9394f82ce394f82db394f82e8394f82f63934235d194f8302394f83c339a422fd194f83b2396422bd194f83a23904527d194f83923934523d194f8382396451fd194f8372391451bd194f83623904217d194f8292394f835339a4211d194f83423069b93420cd194f83323059b934207d194f83223954203d111b994f82583a9e00023012284f8403384f8433384f8443384f8453384f8463384f8473384f8483384f8493384f84a3384f84b3384f84c3384f84d3384f84e3384f84f3384f8503384f8513384f8523384f8533384f8543384f8553384f8563384f8573384f8583384f8593384f85a3384f85b3384f85c3384f85d3384f85e3384f85f3384f8603384f8613384f8623384f8633384f8643384f8653384f8663384f8673384f8683384f8693384f86a3384f86b3384f86c3384f86d3384f86e3384f83f2384f8415384f84203dcf8002047f61803170a84f8432384f84473170c120e84f846230822c2f2070384f8457384f86f231a6d4bf67c57c0f2720704f22631ba4204f2323a1bd15a6ccab140f6b803c2f200031a689ab1182d00f02984172d40f03584fe2804f2473c0d4600f05684002840f0608400980df0acf8294602e0022084f8400350460c22fdf71fff94f8258394f86fa384f83e8345f64d50c0f246008047811eb1fa81f14fea511b012808bf4ff0020bbaf1000f5ad004f23f3007a9049040f261400631c0f2470003910aa9023100254ff00309a0f5b84000240291019015e000bf029807a91a468df828908df82980fdf7e9fe47f00802019ff0200aa90023b8470334e0b25045b1462fd2aaeb0400c7b20420032f28bf4f468df81c0005983a468df81d0006988df81fb08df81e0004988df8204050fa84f103988df821a08df822508df823508df82450fdf7bbfeb81dc345c3b24ff00306c2d14ef60b670120f02107aac0f24707c9e7bff35f8f00994ff29c574ef66c6940f2030b0020c2f22907ccf2ff09cff6807b08703d68bff35f8f05ea0b00b0f1005f59d12868484556d16868b0f57d7f52d105f575700021bff35f8f50e8006f40e80012002af9d147f64c60bff35f8fc2f207000468b6f1004f0fd0b84603f073fd58b1002e00f04d8195f8ec03320a002800f0e38195f8ed13ece195f8ec03e8b147f61800c2f207000068b8b145f60960c2f207000478002040f261430a9007900aa907aac0f247032046984707990a9a43f69b53c0f24e0320469847002085f8ee0385f8ec03d5f8d80320b142f68d41c0f244018847d7f800a0bff35f8f0aea0b00b0f1005f40f00481daf80000484540f0ff80daf80400b0f57d7f40f0f9809af8700338b100208af86801caf864014db0bde8f08f9af86801002800f0d780daf86441002c00f0d28040f6b800c2f20000056807a802f066f9002d00f0d781284621464ff416324e464ff6ff770695fdf7fbfd002547f67f744ff07f0806e048f2a351c0f24501bff32f8f88474ff29c50c2f22900d0f80090bff35f8f0068bff35f8f00ea0b01b1f1005f07d10168b14204d14168b1f57d7f00f0b8804ff47d7004f028fc002802f00c814ff60c41cff6ff7100bf42182e3182f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f8195482f81a5482f81b5482f81c5482f81d5482f81e5482f81f5482f8205482f82154a0d14ff47d71416001214ff29c5206608173a0f8c44180f8c381078447848784c784078547858785c785078647868786c786078747878787c787c2f2290252e8001f49457ff453afbff35f8f42e8000121b352e8001f4945f8d048e79af86a0170b1daf86c0140b147f64c61c2f207010968401a002802f3848000208af86a0140f6a120c0f2470080474db0bde8f08f95f8ec03002840f0db80e3e6817b002942f05780027f0021002a8df8281000f028824ff29c550aaac2f22905535c002b00f0928253185f78002f00f050829f78002f00f04e82df78002f00f04c821f79002f00f04a825f79002f00f048829f79002f00f04682df79002f00f044821f7a002f00f042825f7a002f00f040829f7a002f00f03e82df7a002f00f03c821f7b002f00f03a825f7b002f00f038829f7b002f00f03682df7b002f00f034821f7c002f00f032825f7c002f00f030829f7c002f00f02e82df7c002f00f02c821f7d002f00f02a825f7d002f00f028829f7d002f00f02682df7d002f00f024821f7e002f00f022825f7e002f00f020829f7e002f00f01e82df7e002f00f01c821f7f002f00f01a825f7f002f00f018829f7f002f00f01682db7f002b00f01482203160297ff47baf33e20120a5f8ec0340f20220a5f8f0030220002185f8ef03c5f8e01302f00103c6f300208b42c6f34b2206d123b395f8f233f7b2bb421fd00ce05bb995f8ef13a5f8ea2385f8f013022185f8f113c5f8dc4311e095f8ef3385f8f03385f8f163a5f8ea23c5f8dc4331b9012185f8ee13c6f34951a5f8e813002804bf002185f8ee1385f8ed0385f8f26395f8ee1341b395f8680160b1d5f8e403bff35f8fb5f8e8136ff39f20884216d195f8ee13c1b195f8ef6340e000208af86801caf864018af86a0101208af8690140f6a120c0f24700804701f06dbf002085f8ee03c5f8dc43d5f8dc13b5f8ea0395f8f163611a814220d28902b1fbf0f04ff4406395f8f01300fb00f2a3eb40005043000d721a00fb02f34ff60c47002b4ff40073cff6ff7748bf07f5fa7310fb0230c21700eb925001eb9026002147f61800c2f207000068e0b1d5f8e003f4b2a0421ad0002040f261430a9007900aa907aac0f247032046984707990a9a43f69b53c0f24e0320469847c5f8e04395f8ee1302e00020c5f8e003d5f8d80385f8ef6311b14ff47a7108e095f8f113f2b28a424ff0280108bf4ff47a7142f68d42c0f24402403a904795f8ee034746c8b13868bff35f8f00ea0b01b1f1005f41f0f8860168494541f0f4864168b1f57d7f41f0ef86002180f86811c0f864114db0bde8f08f95f8ed0300287ff453adf0b20228ddd140f6b800c2f2000001680029d6d001684ff416322139002301f8403f403a01f81f3c01f81e3c01f81d3c01f81c3c01f81b3c01f81a3c01f8193c01f8183c01f8173c01f8163c01f8153c01f8143c01f8133c01f8123c01f8113c01f8103c01f80f3c01f80e3c01f80d3c01f80c3c01f80b3c01f80a3c01f8093c01f8083c01f8073c01f8063c01f8053c01f8043c01f8033c01f8023c01f8013c4b708b70cb700b714b718b71cb710b724b728b72cb720b734b738b73cb730b744b748b74cb740b754b758b75cb750b764b768b76cb760b774b778b77cb7781f820309cd100684ef217310a904ff416300b900aa8c0f247018847474661e700980d460cf0f2fd10b302304ff0030008bf022084f840032946fff7dcbb84f87003dcf800204ef660231a44c4f88023b8b100980d460cf0b7ff0cf08bff294660b9fff7c8bb4ff29c55c2f2290595e00cf080ff002829463ff4bdab0320fff7b8bb002084f86a01fff7b5bb18684a4600eb0b20704400eb084000eb06616046fdf7ccfa0ee04eea0b2141ea084141ea066361464a460cf07ffd00281cbf032084f8400309f1080084f86f032946fff792bb01313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04f235d29937014d052235c29d37010d044235b2913710cd045235a29537108d052235929937104d020235829d37141f0c48200218df887100121427f002a00f0c28000220aab9f5c002f00f0a0809f187e78002e5ed0be78002e5dd0fe78002e5cd03e79002e5bd07e79002e5ad0be79002e59d0fe79002e58d03e7a002e57d07e7a002e56d0be7a002e55d0fe7a002e54d03e7b002e53d07e7b002e52d0be7b002e51d0fe7b002e50d03e7c002e4fd07e7c002e4ed0be7c002e4dd0fe7c002e4cd03e7d002e4bd07e7d002e4ad0be7d002e49d0fe7d002e48d03e7e002e47d07e7e002e46d0be7e002e45d0fe7e002e44d03e7f002e43d07e7f002e42d0be7f002e41d0ff7f002f40d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff053079f5411d013444b275e2a5f700cd049275d2a9f7008d050275c2adf7004d020275b2a1f7140f0f08100228df887200131827f002a00f0be8000210aaa535c002b00f0a08053185f78002f5ed09f78002f5dd0df78002f5cd01f79002f5bd05f79002f5ad09f79002f59d0df79002f58d01f7a002f57d05f7a002f56d09f7a002f55d0df7a002f54d01f7b002f53d05f7b002f52d09f7b002f51d0df7b002f50d01f7c002f4fd05f7c002f4ed09f7c002f4dd0df7c002f4cd01f7d002f4bd05f7d002f4ad09f7d002f49d0df7d002f48d01f7e002f47d05f7e002f46d09f7e002f45d0df7e002f44d01f7f002f43d05f7f002f42d09f7f002f41d0db7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00a4455235e29537008d050235d29937004d020235c29d37040f0098100218df8871001216a6846f23f53c5f28803b3eb520f40f08b80d20700f08d8000210aaa535c002b00f0858153185f78002f00f0f9809f78002f00f0f980df78002f00f0f9801f79002f00f0f9805f79002f00f0f9809f79002f00f0f980df79002f00f0f9801f7a002f00f0fc805f7a002f00f0fc809f7a002f00f0fc80df7a002f00f0fc801f7b002f00f0fc805f7b002f00f0fc809f7b002f00f0fc80df7b002f00f0fc801f7c002f00f0fc805f7c002f00f0fc809f7c002f00f0fc80df7c002f00f0fc801f7d002f00f0fc805f7d002f00f0fc809f7d002f00f0fc80df7d002f00f0fc801f7e002f00f0fc805f7e002f00f0fc809f7e002f00f0fc80df7e002f00f0fc801f7f002f00f00d815f7f002f00f00b819f7f002f00f00981db7f002b00f00781203160297ff47baf1de14cf67e22caf210126a60002940f0198100210aaa535c002b00f0dc8053185f78002f61d09f78002f60d0df78002f5fd01f79002f5ed05f79002f5fd09f79002f60d0df79002f61d01f7a002f62d05f7a002f63d09f7a002f64d0df7a002f68d01f7b002f69d05f7b002f6ad09f7b002f6bd0df7b002f6cd01f7c002f6dd05f7c002f6ed09f7c002f6fd0df7c002f70d01f7d002f71d05f7d002f72d09f7d002f73d0df7d002f74d01f7e002f75d05f7e002f76d09f7e002f77d0df7e002f78d01f7f002f79d05f7f002f7ad09f7f002f7bd0db7f002b7cd0203160297ff49aaface000211171f6e6013173e0023171e003316fe004316de0013184e0053169e0023180e0063165e003317ce0073161e0043178e008315de0053174e0093159e0063170e00a3155e007316ce000225a710fe60b314ee0083165e00c314ae0093161e00d3146e00a315de00e3142e00b3159e00f313ee00c3155e010313ae00d3151e0113136e00e314de0123132e00f3149e013312ee0103145e014312ae0113141e0153126e012313de0163122e0133139e017311ee0143135e018311ae0153131e0193116e016312de01a3112e0173129e01b310ee0183125e01c310ae0193121e01d3106e01a311de01e3102e01b3119e01f315f2931d84ff04f0353542dd00a444b235e29537028d020235d29937024d00021d17024e01c3104e01d3102e01e3100e01f315f2918d84ff04103535414d00a444c235e2953700fd05d2993700cd04f235c29d37008d043235b29137104d020235a29537140f0828700218df8871000220aafb95c002900f0a080bb18597800295ed0997800295dd0d97800295cd0197900295bd0597900295ad09979002959d0d979002958d0197a002957d0597a002956d0997a002955d0d97a002954d0197b002953d0597b002952d0997b002951d0d97b002950d0197c00294fd0597c00294ed0997c00294dd0d97c00294cd0197d00294bd0597d00294ad0997d002949d0d97d002948d0197e002947d0597e002946d0997e002945d0d97e002944d0197f002943d0597f002942d0997f002941d0d97f002940d02032602a7ff49aaf42e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a03d84ff07701b95403d100218df8871002e0b91800224a7004694ff0000917f80910002900f0c48007eb09025178002962d09178002962d0d178002962d01179002962d05179002962d09179002962d0d179002962d0117a002962d0517a002962d0917a002962d0d17a002962d0117b002962d0517b002962d0917b002962d0d17b002962d0117c002962d0517c002962d0917c002962d0d17c002962d0117d002962d0517d002962d0917d002962d0d17d002962d0117e002962d0517e002962d0917e002962d0d17e002962d0117f002962d0517f002962d0917f002962d0d17f002962d009f12009b9f1600f7ff496af4ff060095be009f1010958e009f1020955e009f1030952e009f104094fe009f105094ce009f1060949e009f1070946e009f1080943e009f1090940e009f10a093de009f10b093ae009f10c0937e009f10d0934e009f10e0931e009f10f092ee009f110092be009f1110928e009f1120925e009f1130922e009f114091fe009f115091ce009f1160919e009f1170916e009f1180913e009f1190910e009f11a090de009f11b090ae009f11c0907e009f11d0904e009f11e0901e009f11f0901250a2c1ed364214ff47a7242f2107305eb85077f00b4fbf7f60a2e11d305fb01f7b4fbf7f60a2e0bd305fb02f7b4fbf7f60a2e05d35d43b4fbf5f7092fe7d800e03d464946b9f15f0f98bf5f21c1f1000e05911eeb090b54d0b4fbf5f30aa903f1300201f809204cf6cd41ccf6cc410a2da5fb017248d31bf1010742d003fb154cd608bcfbf6f80aa901eb090448f21f5108f13002c5f2eb116270642da5fb013235d31bf102032cd008fb16c65309b6fbf3f244f6d35102f13007c1f26201a770b5f57a7fa5fb011724d31bf1030118d002fb1361ba0941f25977b1fbf2f3cdf2b717a5fb077603f13007e77003fb121442f2107109f104098d424fea5635a8d20ae0ddf8149007e009f1010904e009f1020901e009f1030900240aab03f80940195d002900f0a0801f19797800295ed0b97800295dd0f97800295cd0397900295bd0797900295ad0b979002959d0f979002958d0397a002957d0797a002956d0b97a002955d0f97a002954d0397b002953d0797b002952d0b97b002951d0f97b002950d0397c00294fd0797c00294ed0b97c00294dd0f97c00294cd0397d00294bd0797d00294ad0b97d002949d0f97d002948d0397e002947d0797e002946d0b97e002945d0f97e002944d0397f002943d0797f002942d0b97f002941d0f97f002940d02034602c7ff49aaf50e001343ae0023438e0033436e0043434e0053432e0063430e007342ee008342ce009342ae00a3428e00b3426e00c3424e00d3422e00e3420e00f341ee010341ce011341ae0123418e0133416e0143414e0153412e0163410e017340ee018340ce019340ae01a3408e01b3406e01c3404e01d3402e01e3400e01f345f2c11d84ff0750119550dd0191973225e2c4a7008d020225d2c8a7004d070225c2cca7040f09d8000218df8871044694ff0000e13f80e10002900f0c78003eb0e056978002962d0a978002962d0e978002962d02979002962d06979002962d0a979002962d0e979002962d0297a002962d0697a002962d0a97a002962d0e97a002962d0297b002962d0697b002962d0a97b002962d0e97b002965d0297c002965d0697c002965d0a97c002965d0e97c002965d0297d002965d0697d002965d0a97d002965d0e97d002965d0297e002965d0697e002965d0a97e002965d0e97e002965d0297f002965d0697f002965d0a97f002965d0e97f002965d00ef1200ebef1600f7ff496af4ff0600e5ee00ef1010e5be00ef1020e58e00ef1030e55e00ef1040e52e00ef1050e4fe00ef1060e4ce00ef1070e49e00ef1080e46e00ef1090e43e00ef10a0e40e00ef10b0e3de00ef10c0e3ae00ef10d0e37e00ef10e0e34e000220a7162e70ef10f0e2ee00ef1100e2be00ef1110e28e00ef1120e25e00ef1130e22e00ef1140e1fe00ef1150e1ce00ef1160e19e00ef1170e16e00ef1180e13e00ef1190e10e00ef11a0e0de00ef11b0e0ae00ef11c0e07e00ef11d0e04e00ef11e0e01e00ef11f0e01250a2c1ed364214ff47a7242f2107305eb85077f00b4fbf7f60a2e11d305fb01f7b4fbf7f60a2e0bd305fb02f7b4fbf7f60a2e05d35d43b4fbf5f7092fe7d800e03d46f446bef15f0f98bf4ff05f0cccf1000842f2107b18eb0e0952d0b4fbf5f20aa902f1300301f80e304cf6cd41ccf6cc410a2da5fb016345d319f1010640d002fb1546db08b6fbf3f20aa901eb0e0448f21f5102f13007c5f2eb116770642da5fb011732d319f102012ad002fb13637a09b3fbf2f6b5f57a7f06f13001a17044f6d351c1f26201a5fb011721d319f1030116d006fb1231ba0941f25976b1fbf2f3cdf2b716a5fb066703f13006e67003fb12140ef1040e5d454fea5735aad209e0e64607e00ef1010e04e00ef1020e01e00ef1030e00240df1280b0bf80e401bf80410002900f0a1800beb0405697800295ed0a97800295dd0e97800295cd0297900295bd0697900295ad0a979002959d0e979002958d0297a002957d0697a002956d0a97a002955d0e97a002954d0297b002953d0697b002952d0a97b002951d0e97b002950d0297c00294fd0697c00294ed0a97c00294dd0e97c00294cd0297d00294bd0697d00294ad0a97d002949d0e97d002948d0297e002947d0697e002946d0a97e002945d0e97e002944d0297f002943d0697f002942d0a97f002941d0e97f002940d02034602c7ff498af5ee001343ae0023438e0033436e0043434e0053432e0063430e007342ee008342ce009342ae00a3428e00b3426e00c3424e00d3422e00e3420e00f341ee010341ce011341ae0123418e0133416e0143414e0153412e0163410e017340ee018340ce019340ae01a3408e01b3406e01c3404e01d3402e01e3400e01f345f2c1fd84ff075010bf804101ad00beb040173225e2c4a7014d020225d2c8a7010d074225c2cca700cd031225b2c0a7108d030225a2c4a7104d02022592c8a7140f0898200218df88710d0f8cc734ff000091bf80910002900f0c4800beb09046178002962d0a178002962d0e178002962d02179002962d06179002962d0a179002962d0e179002962d0217a002962d0617a002962d0a17a002962d0e17a002962d0217b002962d0617b002962d0a17b002962d0e17b002962d0217c002962d0617c002962d0a17c002962d0e17c002962d0217d002962d0617d002962d0a17d002962d0e17d002962d0217e002962d0617e002962d0a17e002962d0e17e002962d0217f002962d0617f002962d0a17f002962d0e17f002962d009f12009b9f1600f7ff496af4ff060095be009f1010958e009f1020955e009f1030952e009f104094fe009f105094ce009f1060949e009f1070946e009f1080943e009f1090940e009f10a093de009f10b093ae009f10c0937e009f10d0934e009f10e0931e009f10f092ee009f110092be009f1110928e009f1120925e009f1130922e009f114091fe009f115091ce009f1160919e009f1170916e009f1180913e009f1190910e009f11a090de009f11b090ae009f11c0907e009f11d0904e009f11e0901e009f11f0901240a2f1ed364214ff47a7242f2107304eb84067600b7fbf6f50a2d11d304fb01f6b7fbf6f50a2d0bd304fb02f6b7fbf6f50a2d05d35c43b7fbf4f6092ee7d800e03446c846b9f15f0f98bf4ff05f08c8f1000e42f2107c1eeb09054dd0b7fbf4f14cf6cd4001f13002ccf6cc400bf809200a2ca4fb002341d36a1c3dd001fb1476d908b6fbf1f348f21f5003f130020beb0907c5f2eb107a70642ca4fb000230d3a81c29d003fb11615309b1fbf3f6b4f57a7f06f13000b87044f6d350c1f26200a4fb000220d3e81c16d006fb1310910941f25973b0fbf1f2cdf2b713a4fb033502f13003fb7002fb110709f1040964454fea5534afd209e0c14607e009f1010904e009f1020901e009f10309069d40f6b8060022c2f200060bf809201bf80200002800f0a1800beb0200417800295ed0817800295dd0c17800295cd0017900295bd0417900295ad08179002959d0c179002958d0017a002957d0417a002956d0817a002955d0c17a002954d0017b002953d0417b002952d0817b002951d0c17b002950d0017c00294fd0417c00294ed0817c00294dd0c17c00294cd0017d00294bd0417d00294ad0817d002949d0c17d002948d0017e002947d0417e002946d0817e002945d0c17e002944d0017f002943d0417f002942d0817f002941d0c07f002840d02032602a7ff498af49e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a0ad84ff075000bf8020005d00beb020073215e2a41705dd100208df887000aaf42f2941845f29c14284602213a46c2f22008c2f213040bf0a3fd00208df8280038460bf01bff28460e213a460bf098fd4ff6a420cff6ff703058a0426ad14ff6b020cff6ff703058b0f54d2f62d14ff6b420cff6ff703058a0425bd145f610634ff0000ec2f213034cf6394408f1040c00274ff0ff3263454cd8186830f003051bd0984528bf0c2846d3a8eb03018d424ff0ff3142d8bd42394688bf2946c00703eb05001cbf0f46ae44013c00f10403e1d12fe000218170a1e70421b7f5807f21d300223d46b5f5007f17d3b5f5806f16d3b5f5006f15d30901b2eb153f4fea1515f0d10fe00022ca7176e500219171fff77db800211172fef73bbd490002e0890000e0c900634504d1484207ea0002714603e04ff0ff324ff0ff3147f64c60c2f20700d0f8840008f1080342f6941c9842c2f2270c4dd142f610674ff0000bc2f2200746f6b9700cf1040e00244ff0ff384ff0ff3977453cd83e6836f0030515d0bc4528bf0c2e36d3aceb07039d4234d82346a54288bf2b46f6071cbf1c46ab447b19013803f10407e4d122e00420b4f5807f17d34ff000082546b5f5007f0cd3b5f5806f0bd3b5f5006f0ad30001b8eb153f4fea1515f0d104e0400002e0800000e0c000774504d1404204ea0008d94603e04ff0ff384ff0ff394ff6882040f6b807cff6ff70c2f200073b580cf10800834264d14ff69423cff6ff73fb58b3f5333f5cd14ff69823cff6ff73fb58834255d14ff29c5043f21063c2f229004ff0000bc2f2270342f6f935a0f1040ca0f1080e0024634542d81e6836f003071ad09e454ff0ff30059028bf0c2e3ad3aeeb030087424ff0ff3034d82046a74288bf3846f6071cbf0446bb44d819013d00f10403dfd123e00427b4f5807f17d34ff0000e2546b5f5007f0cd3b5f5806f0bd3b5f5006f0ad33f01beeb153f4fea1515f0d104e07f0002e0bf0000e0ff00634505d17b4204ea03000590584602e04ff0ff300590064666208df8280072208df8290065208df82a008df82b002f208df82c006d204b238df82d0061208df8313069238df82e0078208df83230422356278df82f0020208df833303a238df8377047270aac8df830008df834308df835004c238df838708df83a00002720468df836308df839308df83b700bf0dafe069de05d002800f0a080e019417800295ed0817800295dd0c17800295cd0017900295bd0417900295ad08179002959d0c179002958d0017a002957d0417a002956d0817a002955d0c17a002954d0017b002953d0417b002952d0817b002951d0c17b002950d0017c00294fd0417c00294ed0817c00294dd0c17c00294cd0017d00294bd0417d00294ad0817d002949d0c17d002948d0017e002947d0417e002946d0817e002945d0c17e002944d0017f002943d0417f002942d0817f002941d0c07f002840d02037602f7ff49aaf64e001373ae0023738e0033736e0043734e0053732e0063730e007372ee008372ce009372ae00a3728e00b3726e00c3724e00d3722e00e3720e00f371ee010371ce011371ae0123718e0133716e0143714e0153712e0163710e017370ee018370ce019370ae01a3708e01b3706e01c3704e01d3702e01e3700e01f375f2f25d84ff02000e05521d0e01945215e2f41701cd076215d2f817018d065215c2fc17014d06e215b2f017110d048215a2f41710cd07521592f817108d06221582fc17104d02021572f017240f0f88000208df887002046494642460bf005fe0020215c002900f0a08021184a78002a5ed08a78002a5dd0ca78002a5cd00a79002a5bd04a79002a5ad08a79002a59d0ca79002a58d00a7a002a57d04a7a002a56d08a7a002a55d0ca7a002a54d00a7b002a53d04a7b002a52d08a7b002a51d0ca7b002a50d00a7c002a4fd04a7c002a4ed08a7c002a4dd0ca7c002a4cd00a7d002a4bd04a7d002a4ad08a7d002a49d0ca7d002a48d00a7e002a47d04a7e002a46d08a7e002a45d0ca7e002a44d00a7f002a43d04a7f002a42d08a7f002a41d0c97f002940d0203060287ff49aaf5be001303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f281cd84ff02001215418d021184f225e284a7013d074225d288a700fd068225c28ca700bd065225b280a7107d072225a284a7103d0202259288a7128d100208df887000aac059a204631460bf038fd28461a2122460bf069fa00208af86801caf8640106984ef217310a904ff416300b900aa8c0f24701884701208af86a0107a800f031f9caf814004db0bde8f08f0020c871d6e70021417207e72de9f04f81b0009041f204024cf65560cef20002ccf2ac50c2f8ac0f4ef6fc50cef2000001684ef66c6641f08071016052f8040c4ff29c57ccf2ff06c2f229074ff6ff7440f0010000254ff0010847f67f794ff07f0a42f8040c06e048f2a351c0f24501bff32f8f8847d7f800b0bff35f8f3868bff35f8f01466ff39601b1f1005f07d10168b14204d14168b1f57d7f00f094804ff47d7002f0aafa002800f0ca804ff60c41cff6ff7100bf42182e3182f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f8195482f81a5482f81b5482f81c5482f81d5482f81e5482f81f5482f8205482f82154a0d14ff47d710660416080f80e80a0f8c49180f8c3a1048444848484c484048544858485c485048644868486c486048744878487c48757e8001f59457ff45aafbff35f8f47e8000121b157e8001f5945f8d04fe78169d1bb47f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f421ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94206d041f20401cef200010968c91a816141f20400cef2000000680099086001b0bde8f08f00bf2de9f04f83b041f20401cef200014ef66c674ff29c5609680068ccf2ff07c2f229064ff6ff744ff000084ff0010947f67f7a4ff07f0b0291019006e048f2a351c0f24501bff32f8f88473568bff35f8f3068bff35f8f01466ff39601b1f1005f07d10168b94204d14168b1f57d7f00f093804ff47d7002f095f9002800f0e7804ff60c41cff6ff7142182e3182f8f48382f8f58382f8f68382f8f78382f8f88382f8f98382f8fa8382f8fb8382f8fc8382f8fd8382f8fe8382f8ff8382f8008482f8018482f8028482f8038482f8048482f8058482f8068482f8078482f8088482f8098482f80a8482f80b8482f80c8482f80d8482f80e8482f80f8482f8108482f8118482f8128482f8138482f8148482f8158482f8168482f8178482f8188482f8198482f81a8482f81b8482f81c8482f81d8482f81e8482f81f8482f8208482f82184a0d14ff47d710760416080f80e90a0f8c4a180f8c3b1048444848484c484048544858485c485048644868486c486048744878487c48756e8001fa9427ff45cafbff35f8f46e8000121b156e8001fa942f8d051e7816961b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce047f64c6245f6e466c2f20702cff6f8761368314600bf17689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94216d041f20401cef20001096844f6d357ca1ac1f26207a2fb07768261b00901284ff0010088bfb009994208bffa2000e0fa20dde90121891ab1fbf0f003b0bde8f08f2ab1a2fb0103002b1cbf0020704702fb01f0ffe7b0b54ff2a054c2f229040146206846f23f52c5f28802b2eb500f40f258304df681124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f258304df69f22c2f20000c0f248021047002855d1002953d0082a51d30b78aa2b4ed18b79f02b4bd12de9f04182b045f64d550b2ac0f2460547d3c878a2f10803834242d14878212841d1087901283ed1487901283bd1c879c8bb0c4614f8080f10f03c0f33d18818884610f8021c10f8010c164641ea0027a2f10a0081b220460af032f9874222d1a847811eb1fa81f14f09012808bf0227002f4ad0207887f00301084247d0012039464246334600f07ff90446002898f8080018bf06243be04ff27143c0f24c0318470b2400e00a24a847811eb1fa81f14e09012808bf02261eb3304600f090f8f8b10546407ce0b10027284631463246032300970af0acf9686840b14bf69b71c0f25d0188476868fff774ff6f602868af75002818bffff76dff00202860a8602882204602b0bde8f0810624f9e700243842f6d0404631463a463b4600f056f9002818bf0446ece70b291dd3c278a1f10803934218d10278aa2a13d14278212a10d10279012a0dd14279012a0ad18279f02a07d1c2792ab9024612f8083f13f03c0f03d00a2070470b20704710b5084410f8023c10f8010c43ea0024a1f10a0081b210460af09cf8201a18bf0a2010bd002808bf7047b0b582b00446407cd0b100252046032300950af042f9606840b14bf69b71c0f25d0188476068fff70aff65602068a575002818bffff703ff00202060a060208202b0bde8b040704700bf2de9f04f81b04ef66c654ff29c56ccf2ff05c2f229064ff6ff7700244ff0010b47f67f784ff07f09009006e048f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f07d10168a94204d14168b1f57d7f00f095804ff47d7001f038ff002800f08d804ff60c41cff6ff7100bf42182e3182f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f8194482f81a4482f81b4482f81c4482f81d4482f81e4482f81f4482f8204482f82144a0d14ff47d710560416080f80eb0a0f8c48180f8c391078447848784c784078547858785c785078647868786c786078747878787c78756e8001f51457ff45aafbff35f8f46e8000129b156e8001f5145f8d04fe700200099002801eb410100eb011118bf01f51b7001b0bde8f08f00bfb0b5c4b0dc08202c03d94ff0ff3044b0b0bd0df107048df807008df80810a01c11461a461d46fbf72ff8a81c4af6610582b2c0f24605f02021460023a84744b0b0bd00bf2de9f04f89b0064618469a4617468946fff700ff002800f09983044696f908000796b0f1ff3f00f003050add264616f8110f00283fd12068002818bffff7e4fd2de3207c10b3607c00b34ff000082046394652460323cdf800800af003f8606848b14bf69b71c0f25d0188476068fff7cbfdc4f80480206884f81680002818bffff7c2fd00202060a06020822068002818bffff7b9fd4ff480710020218207992060a0608978a081092004f11106e174a174257560760799a07c8978884240f0d382217da94240f0cf82411ca9f10b00627e01961e2828bf1e209042a17488bf6076a9f10200039787b20a2f4ff00005c0f0db82092241f2210809200497cdf808a017e02068049f002818bffff77cfda089002d00f10100069d26602674a660a08118bf0625059e06eb0b0082b2ba4280f0bb82217c80b20429064600f28080079b461c9a5c4b1c0129237405dd022906d0032968d1e28106e021b9627503e0238943ea02222281be4280f09e82042966d0079a831c925d8f1c0129277406dd049fff290bd003290bd11e464ce000291cbf278947ea0222049f228102e0627500e0e281bb4280f08082022935d8079ac61cd25ccb1c0129237405dd022933d0fe2905d1627506e021b9238943ea0222228100e0e281be4280f0678202292fd0079a031d925d41f00407fc2927740add049ffd290cd0fe2903d0278947ea0222049f228105e0049f0029bed1e28100e06275bb4280f0498209b11e4610e0461d0798c25c05202074e189208941ea0221e18128b1fff7c4fc0028206000f06682b4f808906089a9eb00011ffa81fbb1b27a1a5a45b8bf93465fea0b420ad022681044079a11441ffa8bf2faf7ebfe6089b4f80890584481b2494560817ff454afd4f800a0607dbaf1000f05964ff000060df1230108bf8a460f288df8236006d8217d00f00302511ab1fa81f14e0900074ff0000706d4217e607e814238bf20760fe000bf606840b14bf69b71c0f25d0188476068fff798fc6760607e20760120e0820695a6b1607da67d400715d4002e4ff0000500f089811ffa89f100294ff6ff7300f00a8101295cd10020cfe000bf607d400750d4002577e14ff48030fff74ffc002e824600f06f81a07d002800f06b81baf1000f00f067816668002e00f0fb8020890428c0f05f8121680a1812f8043c002b40f0588112f8033c002b40f0538112f8023cff2b40f04e8112f8012cff2a40f049814ff4803531607060c6f80ca03561002800f0648107464bf69b70c0f25d0000f5857230460021904740b9316931b170688d420d46ebd187420d46e8d15546002728e14ff48030fff700fc054622e14ff6fe7000ea0900ac4642426ff001000aeb0007be78ff7883ea06231eb25d002db208ead63686ea430308ead5355e0085ea430336b25d002db208ead63686ea430308ead5355e0085ea430336b25d002db208ead63686ea430308ead5355e0085ea430336b25d002db208ead63686ea430308ead53686ea430383ea07231fb25e0036b208ead73787ea430308ead6365f0086ea43033fb25e0036b208ead73787ea430308ead6365f0086ea43033fb25e0036b208ead73787ea430308ead6365f0086ea43033fb25e0036b208ead737023087ea430308ead6371618023687ea430395d10230002765465feac97232d01af8000083ea002002b243001bb208ead23282ea400008ead333420083ea400012b243001bb208ead23282ea400008ead333420083ea400012b243001bb208ead23282ea400008ead333420083ea400012b243001bb208ead23282ea400008ead33282ea4003e0899ab2824273d15046ddf808a05346faf7a1fe00286bd10026012378e03820fff741fb002862d00646077047708770c770077147718771c771077247728772c772077347738773c773077447748774c774077547758775c775077647768776c776077747778777c77780f8287080f8297080f82a7080f82b7080f82c7080f82d7080f82e7080f82f7080f8307080f8317080f8327080f8337080f8347080f8357080f8367080f837704ff2dd50cff6ff70784430624ff22960cff6ff70784470624bf69b70c0f25d0045f6e46200f13c0730460f21c0f27c0238230025b84738b33046fff703fb2f46554600e05546606840b14bf69b71c0f25d0188476068fff7f5fa6760ddf808a000260323a77503991ffa89f720465246009709f015fd002d294605463ff460ad0846fff7dffa5be50027666077e6c5f580390027b5f5803f55463760f7607ff649aed0e700252046394652460323009509f0f6fc606840b14bf69b71c0f25d0188476068fff7befa65602068a575002818bffff7b7fa0020307000202060a06020820a2551e00798007a40064dd5207c00b301980078e0b103990025204652460323009509f0ccfc606840b14bf69b71c0f25d0188476068fff794fa65602068a575002818bffff78dfa00202060a06020820b252068002818bffff783fa00202060a060208220e001980078e0b103990025204652460323009509f0a1fc606840b14bf69b71c0f25d0188476068fff769fa65602068a575002818bffff762fa00202060a06020820625284609b0bde8f08f00bf2de9f041f0284bd100294ff00b0000f07f80022a7cd345f64d50c0f246000e4617468047811eb1fa81f14c09012808bf0224002c4ff00a006ad07578e91ec9b2fe2938bfbde8f081307833463946032860d002282dd001281cbf0a20bde8f081ac4204bf0020bde8f0814808842884bf0b20bde8f081881e03f1020887b240463946fff7bffa002869d006462846fff715fb21462a46fff7e9fa3046bde8f08145f64d57c0f2460707f5e27cbde8f0416047ac421cbf0020bde8f081a1f1210010f1160f48d36ff0480048432330c0b2242841d81a4612f8020f40f00207032f4bd19b796340032b47d10b2904d0032804bf0b20bde8f081881e4ef60b6783b2c0f247070120f021b847002818bf0620bde8f081ac422dd1a1f10c0010f1030f1ad31a4612f8020f042815d158796040032811d1d879a0f1310717f1300f0bd39b79984208d9a1f10807c01a874284bf0b20bde8f081cfe70b20bde8f081b07a204207d04046394622462b46bde8f041fff79abb0020bde8f0810a20bde8f08140f20d00c0f2000042f6653c7844c0f2440c012100220023604700bf80b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd4ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f19d10a684ef66c63ccf2ff039a4212d14a68b2f57d7f0ed1d1f86c1159b147f64c62c2f207021268891a012903db0178042938bf7047002101707047b0b541f23154c0f252041d46a0474ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f0cd10a684ef66c63ccf2ff039a4205d14a68b2f57d7f18bf002100e00021421c18bf002900d1b0bd95f84d20002afad091f84721002a08bfb0bd46f25863c2f206031a68132aeed8c2ebc20403eb04135b6801ebc2014ff40042c1f8e431a1f8ea212a7a01f5f271002a0fbf802248f20112ca808122ab7c33b1ab7d23b1022342f002028b71ca7195f820303bb195f8243023b1032342f004028b71ca7195f846304bb195f8443042f008020b7195f84530ca714b71b0bd48f27342c2f207021378002b14bf9378ff230b711378002b14bf5278ff224a71b0bdf0b589b048f66502c0f24b020c46054690474ff29c51c2f229010e68bff35f8f31466ff39601b1f1005f0cd131684ef66c62ccf2ff02914205d17168b1f57d7f08bf092d01d009b0f0bde1082c29fad896f847110029f6d045f64d51c0f2460107468847014638460129ecd1ff22079244f6435246f258610020cff60172c2f20601089006920b685a1e132a4fd8c3ebc30501eb05111a1fa1f5de7191f85451ad0618d4d51c42d091f8e450ad0616d4951c3cd091f87450ad0613d4551c36d00d79ad0610d4043aa1f5e071151da3f10403e3d12be05a1e01f5a87104e09a1ee03101e0da1e703106ebc203096893f9eb21b2f1ff3f1adc03f5f2731e688e4215d11e798df81b605e798df81c609b798df81e200a0a8df820108df821200a0c090e8df81d308df822208df823100f228df8012010228df80220522208218df80420b4f5804f4ff080028df800108df8061038bf4ff48042b4f5001f6ff07f0604f07f0138bf0026802c28bf80318df807109df809509df80a108df8030004d29df808200223002415e0c4f3c610b4f5804f024403d20323012400200be0b4f5001f03d204230224012003e0052303240120002135468df8053004f108038df808208df809508df80a1070b96846a222c118c254062048700c208870c81c06a90c22faf7f7f904f117034ff225066a46c0f2470601200821b047384609b0f0bd70b582b00546ff208df8040044f25220c0f20100009040f2c960c0f2480016460c46804798b148f27e30c2f2070000880122c1b264298df8032008d80721b0f5807f38bf03218df803108df80400301b28bf08280cd3d221281929550621417005218170033069460522faf7b3f90834204602b070bd00bf024600284ff0ff3009d0022918bf70471078112801d1507810b14ff0ff30704710b586b045f64d50c0f24600804701284ff000003ed1cde9020040f20830c0f210000190ff208df8140044f25220c0f20100049040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8132008d80721b0f5807f38bf03218df813108df8140040f2d260adf80800052001ac8df80a00e01d04a90522faf75df94ff2250cc0f2470c0120092122460c23e04706b010bd024600284ff0ff3021d0012918bf704710781f2819d110b545f64d54c0f24604a04701281cbf002010bd4bf20e31c0f2000104f5e2747944e02029220023a047002818bf4ff0ff3010bd4ff0ff3070474ff29c50c2f22900016808466ff39600b0f1005f1ad108684ef66c62ccf2ff02904213d14868b0f57d7f0fd1d1f83c0160b147f64c62c2f207021268801a0028cebf01200020c1f83c017047002070474ff29c50c2f22900026810466ff39600b0f1005f32d110684ef66c61ccf2ff0188422bd15068b0f57d7f27d1d2f86c0120b347f64c61c2f207010968401a0028c4bf01207047d2f8701100200029c2f86c0182f86a01c2f87001c2f82003a2f88c0308bf704780b548f2a352c0f24502084690470020bde88040704700207047f0b101684ef66c62ccf2ff02914218bf704790f8461199b110b54af273644ff0000cc0f24604864680f846c10120002100220023aef844c1a446bde810406047704700bf02464ff6ab70cff6ff7042f6653c7844c0f2440c00210023604700bf70b5002866d0044600796179a279e37940ea012040ea024050ea036007d00e2803d0032856d1032502e0042500e00225fff74eff00284dd04ff29c50c2f22900066830466ff39600b0f1005f42d130684ef66c61ccf2ff0188423bd17068b0f57d7f18bf70bd45f64d50a478c0f24600804701282ed1082086f88402032086f88502102086f88602b22086f88802062086f8890286f88a02462086f88b0243204ff0000e86f88c0201204ef6057c06f5217286f887e286f88d0286f88e5286f88f42c0f2470c09210d2386f890e2bde87040604770bd00bf2de9f04f81b04ef66c6a4ff29c560446ccf2ff0ac2f229064ff6ff77002547f67f784ff07f0906e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f07d10168514504d14168b1f57d7f00f096804ff47d7000f048fe002800f0bb804ff60c41cff6ff7100bf42182e3182f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f8195482f81a5482f81b5482f81c5482f81d5482f81e5482f81f5482f8205482f82154a0d14ff47d7141600121c0f800a08173a0f8c48180f8c391078447848784c784078547858785c785078647868786c786078747878787c78756e8001f59457ff459afbff35f8f00bf46e8000121b156e8001f5945f8d04de7002780f85b712a270821032380f85c71042780f8581180f85931102380f85d7180f85e1101214ff2250c074600f5ac7280f85a3180f85f1180f86031002c18bf0124c0f2470c012010210a2387f8614101b0bde8f04f604701b0bde8f08f00bf80b582b04ff0010ccdf800c000f002f802b080bd2de9f04381b04ff29c57c2f229073f68ddf820803c466ff39604b4f1005f10d13e684ef66c65ccf2ff05ae424ff0000904d17e68b6f57d7f08bfb946012804d013e04ff0000901280fd11c4615460e460746fff7d1fd2346044631462a463846002c18bfb9f1000f07d14af2736cc0f2460c01b0bde8f043604799f8460180b1b8f1010f7cd1d9f84001002189f84611a9f84411a0b342f68d41c0f2440188472ee0b9f844010121013080b2012898bf0846a9f84401d9f8400142f68d44c0f2440489f8461128b94846fff71ffec9f8400140b1a047d9f84001a4f140024ff4c8719047a8b199f84601002848d0d9f84001002189f84611a9f8441100b1a0474af2736cc0f2460c0120002100220023afe745f64d50c0f24600804701282fd1082089f84801032089f84901102089f84a01002089f84b01b22089f84c01062089f84d0189f84e01462089f84f01432089f85001012089f85101b9f844014ef6057c070a09f5a47289f8528189f85301c0f2470c012009210d2389f8547178e701b0bde8f08300bf80b582b04ff0050ccdf800c0fff734ff02b080bd2de9f04f83b04468002c00f0ba828368002b00f0b68204eb030b4ff000090027002614e047f64c63c2f207031b6845f69077c0f201073b44c6f83c310ceb0a0300275b451c464ff0000680f09a82a04618f9013b03f07f05bd40002b46ea050610d406f00703012b16dc0bb3012b40f08882abeb0803082bc0f0838204f10903dee700bfd84580f07c82182f07f107074446dcd975e200bf022b3ad0052b40f07082abeb0803042bc0f06b82631dc7e7d84580f0668294f90130002b04f10203bed55b4580f05d8294f90230b3f1ff3f04f10303b4dc5b4580f0538294f90330b3f1ff3f04f10403aadc5b4580f0498294f90430b3f1ff3f04f10503a0dc5b4580f03f8294f90530002b00f13a82a31d96e7d84580f0358294f9013004f1020c002b03f07f0a1fd5dc4580f02a8294f90270e31c07f07f05b7f1ff3f4aeac51a02dde0469c460fe05b4580f01a8294f9037004f1040c07f07f05b7f1ff3f4aea853a40f3ff809846abeb0c039a4500f20882f308672b1dd0652b7ff45fafbaf1060ffff45baf9cf80030462b7ff456af98f80230432b7ff451af98f80330012b7ff44caf4ff29c54c2f229040290cde9002118e0044660460e4651461746654600f036fcac4631463a46204636e748f2a3513046c0f24501bff32f8f8847dde900210298ac462768bff35f8f2668bff35f8f33466ff39603b3f1005f0bd133684ef66c65ccf2ff05ab4204d17368b3f57d7f00f0c2804ff47d70654600f09efbdde9002106460298ac46002e3ff406af4ff60c43cff6ff73f4182e3384f8f49384f8f59384f8f69384f8f79384f8f89384f8f99384f8fa9384f8fb9384f8fc9384f8fd9384f8fe9384f8ff9384f8009484f8019484f8029484f8039484f8049484f8059484f8069484f8079484f8089484f8099484f80a9484f80b9484f80c9484f80d9484f80e9484f80f9484f8109484f8119484f8129484f8139484f8149484f8159484f8169484f8179484f8189484f8199484f81a9484f81b9484f81c9484f81d9484f81e9484f81f9484f8209484f82194a0d14ef66c63ccf2ff0333604ff47d7373600123b37347f67f73a6f8c4317f2386f8c3314ff6ff734ff29c5433847384b384f38433857385b385f38533867386b386f38633877387b387f387c2f2290454e8003fbb427ff440afbff35f8f00bf44e80063fbb154e8003fbb42f8d034e7dc4580f00c8194f90470631d07f07f05b7f1ff3f4aea455a3ff7e1ae5b4580f0fe8094f90570002f00f1f98004f1060c4aea077ae5e698f8043098f8057098f80650032b47ea052837dd052b58dc042b78d0052b7ff440aed6f86c313bb147f64c67c2f207073f68db1b002b18dcd6f8703186f86a91c6f87091c6f82093a6f88c936bb10c4648f2a35180461846c0f24501174665468847ac4621463a46404647f64c63c2f207031b6845f69077c0f201073b44c6f86c310ee6012b3ff400ae022b00f08780032b7ff406ae17460c466546fff711fb03460298ac4621463a46002b3ff4f9ad96f84631002b3ff4f4ad304641f28831a6f8448109f0b1f947e0062b4ad0072b7ff4e7ad80464df6db50c0f24a0016460f466446804703465d1ea446394632464046012d3ff6d5ad981eb0fa80f04009fff7e3fba446394632464046c9e517460c466546fff7d5fa03460298ac4621463a46002b3ff4bdad96f84631002b3ff4b8adb6f8443143457ff4b3add6f8403186f84691002ba6f844913ff4aaad42f68d411846c0f2440188470298ac4621463a469ee5d6f87031c6f86c91002b86f86a91c6f87091c6f82093a6f88c933ff490ad0f4648f2a35104461846c0f24501164665468847ac463946324648e696f84631c6f83c91002b3ff47bad0446d6f8400165468846174686f84691a6f8449120b142f68d41c0f2440188474af273660120002100220023c0f24606b04720463a464146ac465ce54ff2a513c0f2490303b0bde8f04f184700bf7fb50646002806d1fff75efa002807d19de80f00064649f29b1cc0f24f0c60477fbd00bff0b583b01d46144609294ed106460f46f22d10d8a22160196155062141700b2181704af29571c0f20001033079440b22f9f762fb0e35204629464ff4807200f03df80546ff208df8080044f25220c0f20100019040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8072008d80721b0f5807f38bf03218df807108df80800f82d0cd8d221601961550621417005218170033001a90522f9f72afb0835394630464ef60577c0f2470722462b46b84703b0f0bd70b58ab00c464ff29c51c2f229010b68bff35f8f19466ff39601b1f1005f16d119684ef66c66ccf2ff06b1420fd15968b1f57d7f0bd1694605461846164600f039fc324628462825a24231d244e04d218df8001043218df8011001218df80210002115258df803108df804108df805108df806108df807108df808108df809108df80a108df80b108df80c108df80d108df80e108df80f108df810108df811108df812108df813108df81410a24213d3111b022910d30fd003398d420cd8c22201190255062048708d70c81c69462a46f9f7b4fa6019c41c20460ab070bd00bf46f65450c2f20700016840f2230cc0f2000cfc44c26861454ff001034ff0040008bf0223624508bf0820002a08bf1046002918bf184370472de9f04f81b04ff29c53c2f229031c68bff35f8f23466ff39603b3f1005f40f0cc8023684ef66c67ccf2ff07bb4240f0c4806368b3f57d7f40f0bf80d4f8bc310133c4f8bc314ff6ff739a4238bf134694f87471a4f8c03184f8c2011fb1002a18bf002907d1b4f8c6010130a4f8c60101b0bde8f08f94f87b010028f3d0d4f8840170b147f64c69c2f20709d9f80030c01a002805dd94f87501012809d101270ce0b4f8c8010130a4f8c80101b0bde8f08f94f87771012f18bf022794f87a510020eb0793468846adf80200adf8000012d0022f10d194f8790168b90df1020641f281556b46c0f25b05404611463246a84794f87a515a461646b2f5c86f28bf4ff4c86606f1150a504600f061f8002856d0bbf5c86f88bf45f08005d9f80010d4f8802153230271120a42710a0a8171c2710a0c090e03704d234172a02143700123c17200218370c57002728772017394f87911bdf802204173bdf800108373c173090a0174110a8174310a017500f115014274c6740746084641463246f9f7d1f94ff27d12c0f2470238465146904710f1800fd8bf6ff07f007e28a8bf7e20d4f8801184f8c501481c48f2a351c4f88001c0f245013846884701b0bde8f08fb4f8ca010130a4f8ca0101b0bde8f08fb0b54ff2a054c2f22904216846f23f52c5f28802b2eb510f48f25f514cf67e25c0f24501caf2101518bf25608847002804bf691c2160b0bd10b301684ef66c62ccf2ff0291421bd190f87b11c1b1d0f88411b1b147f64c62c2f207021368cb1a012b0edb1268d0f88801002808bf7047891a42f64d42c0f2440208bf01211047704710b590f875210021c0f8841180f87b11511eb1fa81f149f259624909c0f25902044640f20b10904710f1800fd8bf6ff07f007e28a8bf7e2084f8c4014ef6e374c0f25a04a04704f1bc00bde81040004700bf2de9f04f81b00429c0f0e980834600784d2840f0e4809bf80100432840f0df809bf80200012840f0da804ef66c684ff29c570d46ccf2ff08c2f229074ff6ff7600244ff07f0906e048f2a351c0f24501bff32f8f8847d7f800a0bff35f8f3868bff35f8f01466ff39601b1f1005f07d10168414504d14168b1f57d7f00f098804ff47d70fff752ff002800f0a8804ff60c41cff6ff7100bf42182e3182f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f8194482f81a4482f81b4482f81c4482f81d4482f81e4482f81f4482f8204482f82144a0d14ff47d7141600121817347f67f71c0f80080a0f8c41180f8c391068446848684c684068546858685c685068646868686c686068746878687c68757e8001f51457ff457afbff35f8f00bf47e8000121b157e8001f5145f8d04be79bf80310022905dc01290ed0022900f09e800ce003290dd0042908d190f8741129b101b0bde8f04fdce00d2d06d201b0bde8f08f044600f031f987e090f87b1119b390f87521002180f87b11511eb1fa81f149f259624909c0f25902044640f20b1090474ef6e37510f1800fd8bf6ff07f00c0f25a057e28a8bf7e2084f8c401a84705f1bc00804720469bf804100446012988bf002180f875119bf8051001f00302012a4ff0010198bf0a4680f877219bf80630012b88bf002380f878319bf80730022b88bf002380f879319bf808309bf8097043ea0723502b98bf5023b3f5f07f28bf4ff4f073a0f87c319bf80a309bf80b7043ea072341f28837bb4238bf1f46a0f87e719bf80c3080f8741103f0030380f87a3102f00103002103eb5202c0f88011c0f8bc11a0f8c01180f8c211c0f8c811a0f8c6117f2180f8762180f8c51100f009f8204600f04cf8204601b0bde8f04f78e000bfb0b5044690f87a01800758bfb0bd94f875014ef633750128c0f25a0507d105f1b000804705f58b71002088470ee005f5b670804794f87501012801d1002004e094f87701013818bf0120a84794f875014ff6ed120138b0fa80f0cff6ff7249f2015341097a44c0f2590340f20b10984710f1800fd8bf6ff07f007e28a8bf7e2084f8c301012084f87b01b0bd70b5044647f64c60c2f20700006894f87b1145f69075c0f2010500292844c4f8840108bf70bdd4f8880142f66536c0f2440658b94ff69130cff6ff707844002122460023b047c4f8880150b106f594718847d4f8880106f1e8022946bde87040104770bdb0b5044600f5c675082084f88c01032084f88d01102084f88e01002084f88f01c22084f89001062084f8910104f29311204600f065f84ff0280e4ef6057cc0f2470c012009212a462f2384f892e1bde8b040604770b54ff29c50c2f229000468bff35f8f20466ff39600b0f1005f46d120684ef66c61ccf2ff0188423fd16068b0f57d7f18bf70bd94f87b010025c4f8845100b394f8750149f259620138b0fa80f04109c0f2590240f20b1084f87b5190474ef6e37610f1800fd8bf6ff07f00c0f25a067e28a8bf7e2084f8c401b04706f1bc008047d4f8880184f8745170b142f68d45c0f24405a847d4f8880105f166018847002804bf0020c4f8880170bd70b505464d200870432048700120887095f874010c46c87095f87501087195f87701487195f87801887195f87901c87195f87c01087295f87d01487295f87e01887295f87f01c87295f87a01087395f87b01487345f64d50c0f246008047a073d5f880010026e073d5f88001000a2074b5f88201607495f883012675a074a020e074d5f8bc016075d5f8bc01000aa075b5f8be01e07595f8bf01207695f8c001607695f8c101a07695f8c201e07695f8c601207795f8c701607795f8c801a07795f8c901e07795f8ca0184f8200095f8cb0184f8210095f8c30184f8220095f8c40184f8230095f8c50184f82400fff79dfb84f82500d5f88401a0b147f64c61c2f207010a68821a012a0bdb0968401a44f6d35100f2e730c1f26201a0fb01018e0900e00026300a84f8266084f8270070bd00bf2de9f04182b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f79d121684ef66c62ccf2ff02914272d16168b1f57d7f6ed194f8cc1179b394f8ce01c0070ad0d4f86c0178b347f64c61c2f207010968401a002827dd47f24c45c2f2070555f8040c002854d042f29707c0f24c074af6e908002607f5437101a8c0f24b0801968847a8b1032084f8cf612860b4f8d001c04702b0bde8f081d4f8d811c1b302b0bde8f0410847204602b0bde8f04133e0019841f60d56c0f24c06a861b04706f5af708047e861b847012084f8cf0103202860b4f8d001c047d4f8e001a0b10199d4f8dc21b4f8d271531a38bf8b1abb420ad2b4f8d41159b147f64c62c2f207021268101a884203d32046022100f078f802b0bde8f08100bfb0b590f8cc41002c4cd044f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114614d14a68b2f5870f10d36ff35602012a0cd14a684ff65363cff6ff737b449a4204d1d0f8d821002a18bf4a60002180f8cc1190f8cd11b1b1002180f8cd1145f60a61c2f20701097869b947f24841c2f20701096839b14af68171c0f24b010546042088472846002c08bfb0bd0321bde8b04000e000bfb0b588b0044645f64d50c0f246000d468047012840f08f8008208df8010003208df8020010208df8030000208df80400ca208df8050006208df8060018208df8070047f26440c2f2070050f81c2c94f8cc11002a18bf0122002945f60961c2f207014b7818bf0232002b94f8cf3118bf0432002b4ff0410318bf08328df808304c238df8093001238df80a308df80b508df80c2050f8182c4ef605758df80d200268c0f24705130a8df80e208df80f30130c120e8df810308df811204268130a8df812208df81330130c120e8df814308df8152002698df81620120a8df8172009788df8181081688df819106821c2f2000109688df81a10090a8df81b1047f64c61c2f207010a68130a8df81c208df81d30130c120e8df81e308df81f2000680df10102c4f8dc0108680921c4f8e00101201f23a84708b0b0bd00bf2de9f04f83b0022902d24ff0ff35ece0cde901100120cff680704ef66c644ff29c56ccf2ff04c2f229064ff6ff77851c4ff0000947f67f7b4ff07f0807e000bf48f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f00ea0501b1f1005f07d10168a14204d14168b1f57d7f00f093804ff47d70fff7a1fa0028c3d04ff60c41cff6ff7100bf42182e3182f8f49382f8f59382f8f69382f8f79382f8f89382f8f99382f8fa9382f8fb9382f8fc9382f8fd9382f8fe9382f8ff9382f8009482f8019482f8029482f8039482f8049482f8059482f8069482f8079482f8089482f8099482f80a9482f80b9482f80c9482f80d9482f80e9482f80f9482f8109482f8119482f8129482f8139482f8149482f8159482f8169482f8179482f8189482f8199482f81a9482f81b9482f81c9482f81d9482f81e9482f81f9482f8209482f82194a0d14ff47d714160012104608173a0f8c4b180f8c381078447848784c784078547858785c785078647868786c786078747878787c78756e8001f51457ff45cafbff35f8f46e8000121b156e8001f5145f8d051e7044645f64d50c0f24600804701281dd1029d6978022916d00129204607d000297ff425af00210025fff77efe0fe0019f41f28831022f0ed14ff4fa730022012741f2883624e02046fff7fafd0025284603b0bde8f08faa78052f11d3eb782e79072f43ea062310d395f805c0ae79092f4cea0627e4d3ee792d7a46ea052607e041f288364ff4fa7301e041f288360127642b98bf64238b4228bf0b4690f8cc1180f8ce21a0f8d031a0f8d271a0f8d46171b147f24841c2f20701096800295ed001210446fff730fe0025c4f8e051bee744f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114632d14a686ff00105b2f5870ffff483af0123cff680731a40012a7ff47caf4b684ff6c112cff6ff727a44934214bfc0f8d831d0f8d831002b3ff46daf4a60012180f8cc1199e74af60771c0f24b010420884720b105462046fff756fd5be70121204684f8cd118fe76ff0010553e700bf70b582b00c464df6cb61c0f245018847002c18bf002801d102b070bd0168e029fad10546fef7d2f9014628460029f3d0a278e3782679217842ea0322637942ea0642042942ea036604d16178002908bf0d2e1fd026f001010629ddd147f29120c0f246008047014628460029d4d04ff65914062e4ff00f034ff00006c0f24e044ff000004ff000014ff0000208bf0c2301960ae004204ff6591401900026c0f24e040020002100220e230096a047284602b070bd3146fff7a3bf00bfb0b582b00c460546fef782f90146002d4ff0000018bf002901d102b0b0bd47f2fc70c2f2070000684df6cb61c0f24501884790b10068e0284ff00000edd129784ff6591c0191c0f24e0c0021002223460090e047012002b0b0bd002002b0b0bd08b530460921fff7cbff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460a21fff7b7ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460b21fff7a3ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf01280bd1002908bf704710b5896804460846fef781f92046bde81040704700bf10b5059c4ff20340c0f2460080472146fff7e6ff10bd00bfb0b5c4b0044600284ff0000000f0c78089b20b2940f0c380207838b96078092804d1a078612801d1e07810b1002044b0b0bdfef7e5f80028f8d045f64d50c0f24600804701281cd00228efd103208df8070001200df107058df80800a81c21460b22f8f7cff94af66104c0f24604f02029460d220023a047b0fa80f0400944b0b0bd4ff29c50c2f22900006801466ff39601b1f1005fc9d101684ef66c62ccf2ff029142c2d14168b1f57d7fbed122797f21042a06dc012a0cdcbab1012a08bf00211ae0082a0bdc052a11d0082a08bf0a2112e0022a0dd0042a08bf01210ce0092a09d00a2a08bf0e2106e0092104e0022102e0032100e00b2180f80b1310216a2580f80c13a22180f80653152580f80e13062180f807531a2580f80f130c2180f80853132580f8101352210822022380f8095380f811134921012580f8042380f8053380f80a2380f80d3380f8121380f8135380f81453217900f5417280f815136179192380f81613a17980f81713002180f81813e17980f81913217a80f81a13617a80f81b13a17a4ff2250480f81c13c0f247040120e021a047284644b0b0bd00bf1fb5fff72bff002803d0bde81f4000207047bde81f4038b584b049f21f13c0f24703184710b5f0281bd1d1b1cab10c78032c16d10d2a1cbf0b2010bd487801281cbf0a2010bd45f64d50c0f246000c468047012803d1a01c0b21fff7fffe002010bdbde81040fdf785bc00bf002804bfa3207047017e427e837ec07e41ea0221184300064ff0a40008bfa320a4220a2928bf1046704700bf4ff29c52c2f229021268bff35f8f13466ff39603b3f1005f10d1d2f800c04ef66c63ccf2ff039c4508d15368b3f57d7f02bf0023c2f8fc32c2f8003347f2c942c0f24d02104700bf4ff29c50c2f229000068bff35f8f01466ff39601b1f1005f0fd101684ef66c62ccf2ff02914208d14168b1f57d7f02bf0021c0f8fc12c0f8001347f2d940c0f24d0000472de9f04184b0dde90a76022f16d340f64705a94212d08eb13578412d0ed175784e2d0bd1014602f0bf0012284ff00d0008bf002b0ed004b0bde8f0810c9d0097cde9016546f63916c0f24d06b04704b0bde8f081082feed3b078012802d1f078022803d90d2004b0bde8f081002953d045f6f444c2f2060414f8040c81424bd12068002848d048f22732c0f254020d4608460221904700283ed024682088d8b36088c8b3a088b8b3e088a8b330797179b279f37940ea012040ea024050ea0368d0d04ff29c50c2f229000068bff35f8f01466ff39601b1f1005f1bd101684ef66c62ccf2ff02914214d14168b1f57d7f10d1f178a1b190f8fc22aa420cd1d0f80023424508d101295bd1082faad100f09bf875e0f07818b1082004b0bde8f081082f9fd1f9f742fec8b10646d0f86c010028f1d047f64c61c2f207010968401a0128e9db96f8fc0268b196f8fd02032809d00628e0d1304600f076f8dce7112004b0bde8f08146f63917c0f24d0707f53a60804707f539622846214690470120002786f8fd020df10f02304600210123a6f8fe7286f8fc52c6f800838df80f70adf80c7000f07ef861884af2bd6603abc0f24c0628460222b047384604b0bde8f081a7f1510189b24ff6b87291424ff00d0119d390f8fd02032808467ff42baf334613f8080fa7f108018ab280b9052a0ed9a1884af2bd67c0f24c072846b847002004b0bde8f081084604b0bde8f08101280dd1042a0bd3981810f8021c00297ff420af10f8010c00287ff41bafe0e702284ff00d0008bf062a7ff4fcae707b01283ff60fafd4e790f8fd120a1f022a38bf7047022905d0012907d1042180f8fd127047052180f8fd12704780b582b0052180f8fd120121adf8061045f6f441c2f2060190f8fc0209684af2bd6c49880df10603c0f24c0c0222e04702b0bde8804070472de9f04fc7b0054690f8fc0203920591d8b145f6f041c2f206010978884214d148f22732c0f2540202211c46904760b195f8fc0240f66502c0f255020021904718b100690088172802d247b0bde8f08f033880b2f42828bff42007af0b3807f101084ff000094ff0000a01900495029414e000bf0699cde94509adf810112121adf8121143f23d5144a8c0f255018847049d029c1ffa8af0a042d6d2a4eb0a01019a1ffa81fbb5f8fe1241235a45b8bf93468df81c304e234a1c8df81d300123a5f8fe22d5f800238df81e30059b1ffa8af08df81f30130a8df82410090a8df820208df82130130c120e8df82510b0fa80f18df823204a091ffa8bf111fa8afaa2458df8223008bf0232bbf1000f8df826202fd001390329039901eb000c01d2002115e04ff6fc700bea00020cf10103002100bf5e1816f8014c7d187078ec725c5cb67804318a422c736873ae73f1d1049d1bf003020dd01cf80160cb19012ade7207d00ceb01004178022a19731cbf8078587395f8fc020bf10b0406900b2010fa84f649f6cd713046c0f24c01884700283ff47eaf26f003031ffa84fc06f003025e426ff00303c1180433f518043581f8049081f8059081f8069081f80790f2d152b10344012a83f8049005d0022a83f8059018bf83f806900bf10e0202701b22442108230272220480f801908170c370417283723ff43dafbcf1040f01d2002214e047f6fc710cea0103002200bfb95c8618f17218f8021008eb020531736978ad78043293427173b573f0d11cf003033ff41fafbe5c00f10b01012b8e543ff418af561cbd5d022b8d553ff412af0232bb5c8b540de7f0b583b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f4bd121684ef66c62ccf2ff02914244d16168b1f57d7f40d194f8fc12e9b3002840d002888a423dd145f6f446c2f2060616f8041c8a4235d1316899b3356894f8fd12032919d8d4f86c1139b147f64c62c2f207021268891a00290edc02218df80b100df10b020746204600210123fff78dfe2046fff75cfe38468278092a1bd141896b8894f8fd72994217d1032f3adc012f6ad0022f3dd004e018b181780939022901d203b0f0bd47f6ed31c0f24d0103b0bde8f040084794f8fd72032ff1d8a2f10d0102290cd2c178d1b302208df8040001aa204600210123fff757fe03b0f0bd092a08bf032fdcd14189aa889142d8d1c078002800f0818002200df105028df80500e6e7052f04d0042fcad1c07805262fe0c078002848d0062084f8fd0202208df807000df10702204600210123fff72cfe052fb5d02046fff7f9fd03b0f0bd4268002aadd00389a3f2012189b2b1f57e4fa6d34089082b48d12988884245d12046012108234ce0c0780526002808bf022650b102208df80a000df10a02204600210123fff701fe6988012294f8fc024af2bd67adf8082002abc0f24c07022284f8fd62b84703b0f0bd052f15d1002094f8fc523668c4f8fc02c4f8000347f2c944c0f24d0404f11000804728463146224603b0bde8f0401047032084f8fd0201208df806000df1060272e7e98888427ff457af2046022103e0204603210022002303b0bde8f040bfe506290ad14178012907d18178653911f1630f02d3c178012902d94ff0ff307047f0b581b00179427941ea022540f6b8318d421fd80646fdf7edfbd8b1f9f724fbc0b10446d0f8d80342f66537c0f2440758b941f26f60c0f200007844012100220023b847c4f8d80320b107f1e8022821904718b14ff0ff3001b0f0bdb078f178d4f8e4230023c7f6e07340ea452003ea425240ea0120bff35f8f1043bff35f8fc4f8d403002001b0f0bd00bf02293cbf4ff0ff3070472de9f04f9bb00278437842ea0325b5f5007f04d94ff0ff301bb0bde8f08f4ef66c6a4ff29c56ccf2ff0ac2f229064ff6ff774ff0000b4ff07f090791099008e000bf48f2a3512046c0f24501bff32f8f8847d6f80080bff35f8f3468bff35f8f20466ff39600b0f1005f07d12068504504d16068b0f57d7f00f09a804ff47d70fef7c5fa002800f0928004464ff60c40cff6ff7000bf21182e3081f8f4b381f8f5b381f8f6b381f8f7b381f8f8b381f8f9b381f8fab381f8fbb381f8fcb381f8fdb381f8feb381f8ffb381f800b481f801b481f802b481f803b481f804b481f805b481f806b481f807b481f808b481f809b481f80ab481f80bb481f80cb481f80db481f80eb481f80fb481f810b481f811b481f812b481f813b481f814b481f815b481f816b481f817b481f818b481f819b481f81ab481f81bb481f81cb481f81db481f81eb481f81fb481f820b481f821b4a0d14ff47d7060600120a07347f67f70c4f800a0a4f8c40184f8c39127846784a784e78427856785a785e78527866786a786e78627876787a787e78756e8000f40457ff455afbff35f8f00bf46e8004028b156e8000f4045f8d049e70024ddf824805db1fdf7d2fa0146002c4ff0ff3018bf002902d11bb0bde8f08fb5fa85f040094ff000098df82c908df82d908df82e908df82f908df830908df831908df832908df833908df834908df835908df836908df837908df838908df839908df83a908df83b908df83c908df83d908df83e908df83f908df840908df841908df842908df843908df844908df845908df846908df847908df848908df849908df84a908df84b908df84c908df84d908df84e908df84f908df850908df851908df852908df853908df854908df855908df856908df857908df858908df859908df85a908df85b908df85c908df85d908df85e908df85f908df860908df861908df862908df863908df864908df865908df866908df867908df868908df869908df86a908df86b90bcb1d4f87021a2b14ff4006304e03f683b44b3f5403f0cd2d718be680136f6d07e68033626f0030609eb060101f11009ede7002d4ff0020a039500f00581ddf81cc0acf102020a2ac0f00081002608eb0a0b9bf801504ff0ff30012d3ff648af9bf802209bf803309bf8047042ea03229bf8053042ea074747ea0361a1f1011212f5803f0a91fff433af9bf808109bf809e0029141ea0e210af10a020497aceb02034f1e9f42bff423afcde905269bf806209bf807300a9e42ea032cb6eb0c07fff416afb9423ff613af18f80a70089147ea052a4feada000ba90e5c07f00705012707fa05f5354240f001830e5c45ea06050d5407994ff6ff75c0f20205b4b3d4f8700198b317461a0201924fea0e264ff4006200eb0208d8f8083053454bd0d8f800301a44aa4221d800eb0208d8f80830534540d0d8f800301a44aa4216d800eb0208d8f80830534535d0d8f800301a44aa420bd800eb0208d8f8083053452ad0d8f800301a44aa42d3d900bfbcf1000f40f0bc8204980a464ff6fc710330c0f2030108404844944600f1100908980599069e00eb010a0398013686424ff0000028bf0120ddf824804fd2aceb0a02092a3ff64caf49e0d8f804000a9a904240f09582d8f80c006045c0f0908236d9089aa0eb0c00824200f289827ab3029831180198c1f1000cc2199044a1f1010e002208eb02050beb0203287c9e7ab04240f0758296451cd06e7cdf7abe4240f06e820ceb0207be1c13d0ae7c187b864240f06582079ef81c07d0e87c5b7b984240f05d8204329142dbd1b446a7e78c46a5e7ddf81cc0a2e7ddf81cc0002800f04e82baeb0c004ff0000118bf0120b9f53e3f88bf0121014303980028c1f100003ff43eae00297ff43baed4f870b1bbf1000f40f08b804ff44030fef754f8002800f02d82834600206ff0200100220beb02030bf8020040320beb0107b2f5006f01f1400158709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d87618775877987787f8400087f8410087f8420083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f0093d14ff43e30cbf800080020cbf804084ff0ff31cbf80c084ff40060c4f870b1cbf80818c4f820034ff6ff760221c0f202064ff000090ae00aeb080109f10109ddf8248099454ff000003ff497ad08eb010518f801006f78cdf81c900a9195f802c0eb7895f804906a7995f806a0e97995f808e06d7a40ea072808955cb3012f29d84ff400670beb0705a868404500f0a28028680744b7421dd80beb0705a868404500f0988028680744b74213d80beb0705a868404500f08e8028680744b74209d80beb0705a868404500f0848028680744b742d7d94cea032040ea094040ea0267f81c20f00300d4f82093cdf818a000f1100a0020cde904e10297b9f1000f26d00beb09035bf809105a68514580f09b80eab10beb02005bf80210d0f80490514580f08c80b9f1000f11d00beb09035bf809105a68514580f0868042b10beb02005bf80210d0f804905145d6d376e000204ff400694ff4006504e000bf3d44b5f5403f23d20beb05018a685bf805700132f4d04868ca68033020f0030000f110060beb09001032f7f711f94168c2684bf809608368d4f870b18a424ff00001b14408bf00f110014bf82310d7e7c9f540310f294ff00002c4f8202310d90beb09004bf809104ff0ff3142608160c260c4f820930ae0e868039b0a9a089f60e010b10268114401604ff000090020b9f1000f7ad00beb09035bf809105a68514522d2002a71d00beb02005bf80210d0f80490514513d2b9f1000f66d00beb09035bf809105a6851450ed2002a5dd00beb02005bf80210d0f804905145d7d30546cc469146184601e01d469446a1eb0a0110290fd309eb0a020beb02034bf802104ff0ff31c3f804c0996000219446d960c5f800a0039b0a9addf818a0dde904e1089f00280cbfc4f820c3c0f804c002984ff6ff7668600020c0f20206c5f80880e8604aea0121ddf81c904eea072a814202f10a087ff4c0ae05f1100778180999524641449b46f7f726f8e9685b466868aa68d4f870b15144e96081424ff0000018bf07464bf82270a6e600204ff400684ff4006703e02f44b7f5403f23d20beb07018a685bf807500132f4d04868ca68033020f0030000f110060beb08001032f7f745f84168c2684bf808608368d4f870b18a424ff00001b04408bf00f110014bf82310d7e7c8f5403200210f2ac4f820130dd90beb08034ff0ff304bf8082059609860d960c4f820831bb0bde8f08f00281ebf0168114401604ff0ff301bb0bde8f08f00bf02293cbf4ff0ff3070472de9f04f8bb00278437842ea0325b5f5007f00f2f08005eb4502022303eb4202914240f0e8800590fcf7abfe002800f0e2804ef66c694ff29c56ccf2ff09c2f229064ff6ff77002447f67f784ff07f0b06e048f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f07d10168494504d14168b1f57d7f00f096804ff47d70fdf7d0fd002800f0af804ff60c41cff6ff7100bf42182e3182f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f8194482f81a4482f81b4482f81c4482f81d4482f81e4482f81f4482f8204482f82144a0d14ff47d7141600121c0f800908173a0f8c48180f8c3b1078447848784c784078547858785c785078647868786c786078747878787c78756e8001f51457ff459afbff35f8f00bf46e8000121b156e8001f5145f8d04de7d0f870c10495bcf1000f20d000234ff4006104e03f683944b1f5403f0bd20ceb0107be680136f5d07e68033626f0030633441033eee775b9b3f53e3f40f2e0804ff0ff310bb0bde8f04f08467047002d00f0d680002305994ff0000901f1020a153103914ff0ff31059109eb49060aeb46056c784ff0ff31012ce3d8af78ea7857ea022e1ebf2a796d7952ea0528d9d0bef5207fd6d8b8f5f07fd3d80ef10102520802fb08fb4ff6fb724ff6ff779345c0f20207c6d81af81610059e41ea0421039cb9f1000f28d014f8132c14f8125c42ea05228a42b3d0feb114f80d2c14f80c5c42ea05228a42aad0012e15d014f8072c14f8065c42ea05228a42a0d0022e0bd014f8012c257842ea05228a4297d0043e721c04f11804d6d15646bcf1000f2fd00bf105054ff400640ceb040adaf808208a4230d0daf800201444bc4220d80ceb040adaf808208a4225d0daf800201444bc4215d80ceb040adaf808208a421ad0daf800201444bc420ad80ceb040adaf808208a420fd0daf800201444bc42d3d94ff6fc720bf10801c0f201021140194401f1100320e0daf80410a9427ff44fafdaf80c10a9427ff44aaf9af8101004297ff445af9af811109af8122041ea022171457ff43caf9af813109af8142041ea022141457ff433af049909f1010989450599b24601f1010105917ff43dafb3f53e3f3ff624af00240025019019e000211fe7d0f870c14a688b684ff6ff7793454ff00002c0f20207c1f80cb008bf01f110024cf82320013575454ff000013ff408af05eb45000aeb40011af8100091f8018091f802e091f803b091f804904979bcf1000f40ea082605912cd0b8f1010f29d84ff400610ceb01039a68b24200f0a8801a681144b9421dd80ceb01039a68b24200f09e801a681144b94213d80ceb01039a68b24200f094801a681144b94209d80ceb01039a68b24200f08a801a681144b942d7d94eea0b21059b013149ea0322490801fb02f75a4607f1050b8df81d004fea1b208df820004fea1b408df8210005208df82500042001218df8270048468df81b1003908df82a000df11b0011218df81c408df81e808df81fb08df822408df824408df823408df82640cdf808e08df828e091468df829208df82b30fff797f800287ff482ae01980021d0f870c1bcf1000f3fd0ddf810e0b8f1010f3cd84ff400630ceb03018a68b24235d009680b444ff6ff71c0f202018b4226d80ceb03018a68b24228d009680b444ff6ff71c0f202018b4219d80ceb03018a68b2421bd009680b444ff6ff71c0f202018b420cd80ceb03018a68b2420ed009680b444ff6ff71c0f202018b42cbd9002104e0ddf810e02ee7ddf810e0002f3ff41aaf4fea0922029f059b42f001023a44039f1b0252083b4353435a1e032a01d200220ae023f0030700228e18043297427475b475f4753476f7d113f003033ff4f8ae01f11507012bbc543ff4f2ae3a44022b547018bf9470ebe62de9f04f81b04ef66c644ff29c55ccf2ff04c2f229054ff6ff7600274ff0010a47f67f7b4ff07f0807e000bf48f2a351c0f24501bff32f8f8847d5f80090bff35f8f2868bff35f8f01466ff39601b1f1005f07d10168a14204d14168b1f57d7f00f094804ff47d70fdf7f2fa002800f005814ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ff47d710460416080f80ea0a0f8c4b180f8c381068446848684c684068546858685c685068646868686c686068746878687c68755e8001f49457ff45aafbff35f8f45e8000121b155e8001f4945f8d04fe7d0f88813002974d104464ff41630fdf759fa014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8881300e00021084601b0bde8f08f00bf2de9f04f93b04ff6ff75a9424ff0000c2cd0dde91c874ff4805ecde90aecd0f89ce3cde90087229fdde920a9dde91e6b249c0697239f0aadcdf840c0cde911eccde90845cde9026bcde904a90546079704f0b8fe84464cb1bcf1000f06d19df8480095f8a013084385f8a003604613b0bde8f08f2de9f04f93b00446002047f64c65a4f8a003c2f207052868002a89461abfc4f894030146d4f89413401ac4f89c03fcf739fa002800f05a81d4f88803002800f0558104f0e7ff002800f050810646b4f88c03002853d0a0f10108d4f89c0300254ff4a0714ff4f0774ff4805acde911050df1280bcde901152046414632464ff42073cde90aa51095cdf824b0cde90755cde90555cde90355009704f059fe002840f02481d4f89c034146cde91105012008904ff4a0700190204632464ff420734ff4f077cde90aa51095cdf824b0cde90655cde90455cde90255009704f038fe002840f003819df8480094f8a01347f64c65084384f8a003c2f2070506e0d4f8881330464ff41632f6f7cefa4ef66c684ff29c57ccf2ff08c2f229074ff6ff764ff0000a07e000bf48f2a351c0f24501bff32f8f8847d7f800b0bff35f8f3868bff35f8f01466ff39601b1f1005f07d10168414504d14168b1f57d7f00f098804ff47d70fdf7f6f8002800f0c4804ff60c41cff6ff7100bf42182e3182f8f4a382f8f5a382f8f6a382f8f7a382f8f8a382f8f9a382f8faa382f8fba382f8fca382f8fda382f8fea382f8ffa382f800a482f801a482f802a482f803a482f804a482f805a482f806a482f807a482f808a482f809a482f80aa482f80ba482f80ca482f80da482f80ea482f80fa482f810a482f811a482f812a482f813a482f814a482f815a482f816a482f817a482f818a482f819a482f81aa482f81ba482f81ca482f81da482f81ea482f81fa482f820a482f821a4a0d14ff47d7141600121817347f67f71a0f8c4117f21c0f8008080f8c311068446848684c684068546858685c685068646868686c686068746878687c68757e8001f59457ff456afbff35f8f47e8000121b157e8001f5945f8d04be7064604f0bffe002e18bf002803d1012089f8050022e0c6f864014ff0010a4ff4f07086f868a101904ff420704af61f4c0090c0f2470c00200021002200230027e04730b186f86871c6f8647189f805a004e099f8050089f804a030b14ff0ff3013b0bde8f08f0026cbe794f8a00384f8a10338b3d4f8900342f66536c0f2440620b129682d31c4f8981310e044f6dd70c0f200007844002122460023b047c4f89003296801f12d01c4f8981320b106f1e8022d21904728b1002084f8a10313b0bde8f08f002013b0bde8f08f80b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd022a3cbf4ff0ff3070472de9f04fadf2345d239008784f780a4450ea072001f10207249002f04483259300232292974202f03883781c904202f0348338787978be1c40ea0121901b81422c962f9102f229832598ddf880253690ddf85805ddf878153790ddf85c053f913890ddf8600527933990ddf864053a90ddf868053b90ddf86c053c90ddf870053d90ddf874053e901068411e0028116002f003832f98002802f0ff822f98841c032c02f0fa82fe78072e02f2f6822c9890f80090f00702d107f1040e27e02f9820f00100022802f0e88297f804a07d7923984aea052b594636aa02f0ecfa002842f0db82ddf8640507f1060e012d3a900dd8ddf88005806948b14feadb020af00701835c012505fa01f119438154b00707eb0408cdf8a08005d44ff0000a042e29d2a1e000bfc64505d01ef9014b4ff0000af34603e000244ff0010ac34645f64d55c0f24605a847012808bf0134601e81b2670800ebd130002c48bf4710a84701283a9808bf7f42ddf8a0803844de463a90042e78d3baf1000f42d1c64540d0724612f8011b3a9842453dd09ef801200ef10203120642ea0141434500eb214c3ed00ef103029ef80210424534d09ef803200ef10403120642ea014143454fea214129d00ef105039ef80420434500f0ac869ef805700ef10603434542ea072200f0a3860ef107079ef80630474501f0dc829ef807700ef1080e3f024ff0000a15e03a98002100e0c64600eb010c4ff0010a0022002108e04ff0010a002203e04ff0010a00220021c646002300273b433b9f62440b447fb1dde93c76dde93e40bc45d8bfbc46b142d8bf3146a242a8bf22468342a8bf0346ddf8a0800120cde93cc1cde93e233b90baf1000f42f01582b9f1040f40f38380b9f1060f40f3f380b9f1070f00f0b982b9f1080f00f06c81b9f1090f42f00182c64502f0fe811ef801abc64542f0f981baf10f0f02f2f581ddf87c05002801f078833b993698dde93829002900f029863799dde93d733f9e994227eae775b8bf0b46b242b8bf1646b542359681f261833c994fea630c21eae17b4fea0a11339135990bf10104491b4fea5b0e4aea0a1211f003084fea54013494329100f0f7869b4581f2638205fb09045feacb7171465e4609d014f80e10349e01f0f00141ea0a0104f80e10329961452144319681f23f8204eb0c060c4604f8012bb44281f039828f1cb7424a7081f03582cf1cb7428a7081f030820f1dca70b7423946ead301f029ba00bfb9f1020f37dcb9f1010f00f06f81b9f1020f42f083813698dde937763490ddf88005dde939a1dde93b45dde93d32ddf8fcb0c0694ff00009c645cde946e8cdf820914290cdf80c91cde92a32cde928152e9400f07c820ef1010046909ef80090404500f074820ef1020046909ef80100b84604026fe2b9f1030f00f09681b9f1040f42f04b81dde936c0dde938633490ddf88005dde93aa2dde93c74dde93e59c0694ff0000bc645cde942e8cdf810b14090cdf804b1cde9323c3596cde92d74cde9305200f0af820ef1010042909ef800b0404500f0a7820ef1020042909ef801004fea0028a2e2b9f1050f00f0fa81b9f1060f42f01281c64502f00f810ef10100404502f00a810ef10200404502f005810ef10300404502f000810ef10400404502f0fb800ef10500404502f0f6800ef10600404502f0f1800ef10700404502f0ec800ef1080ca8eb0c001028c2f0e5800ef11800404542f0e0809ef804009ef8051050ea01261ebf9ef806009ef8071050ea012502f0d1809ef800009ef80120379940ea0224081bc2f0c780864202f2c4809ef802009ef80330389a40ea0327d01bc2f0ba80854202f2b780ddf87c05002801f03a8239980df1e80e369b34909ee8014fcdf8d4e0cde906478de8010f1846dde93437cdf828c0cde90865059704f0d7f801f021ba36983999339037983591349038983a9931904046cde94ee0ddf880053291c06900214eac46aedde93b7bdde93da5ddf8fc805091cde946012046314602f03ffb81462046314602f03afb5099002940f0ac82dde94ec2cdf8c080944500f0a5820cf10101914200f0a0820cf10201914200f09b820cf10301914200f096820cf10401914200f091820cf10501914200f08c820cf10601914200f087820cf10701914200f082822d909cf800009cf804102b909cf805003b4628900cf108005e46d646cdf8a8909cf801b09cf802709cf8039029919cf806a09cf80710a2eb0008b8f1010f2e9141f0fe80904200f05d829cf808100f2900f258820cf1090001f0f3b8c64502f01580724612f8010b012802f20f80369900283091dde93751359139993191dde93ac134913c992d913d992b913e992a913f99299100f0d982424501f0f7870ef10202424501f0f2870ef10302424501f0ed870ef10402424501f0e8870ef10502424501f0e3870ef10602424501f0de870ef10702424501f0d9870ef10802424501f0d4879ef801209ef802309ef8046042ea03239ef803209ef805409ef80610774642ea062e44ea012af9793a7a07f109041a9441ea0221b8e2a8eb0e06052ec1f0b4870ef10505454501f0af870ef1060b9ef80540a8eb0b00a042c1f0a6870beb0400404541f0a187369820903798359038982a9039981f90dde93a9034903c9831903d983290dde93e803390ecb14ff0000a04e000bf0af1010aa24515d21bf80a0001381e28f6d90beb0a00a4eb0a014eaa46ab774602f07cfe002801f075874e98be468244a245e9d3ddf87c05002801f0f3803498cdf8c090f146002800f0b283319820eae0703190329820eae070219035984045a8bf404635902a9833998842a8bf08462a90a0e3c64501f04d870ef10100404501f048870ef10200404541f04387704601784078dde93b5441ea0021ddf880050df1d80c0990ddf87c059ce80c10089004a8dde93976ddf8f490ddf8f8e0ddf8fc8080e810422398cde900c7cde90265cdf81c8004f0b2f8074659e1ddf880050df1f00adde936c1dde93853dde93a629ae89004ddf8fc90c0694ff0000bc645cde946e8cdf820b14290cdf80cb1cde9323ccde93451cde92d47cde93062cdf8ac9000f022810ef1010046909ef800b0404500f01a810ef1020046909ef801004fea002815e10120b8464890002446ad42af2846394602f0a3f935902846394602f09ef9489907460029cdf8b4b0cde9324a00f0148144460025002031900120bb464890002046af0df5847a30903846514602f085f980463846514602f080f94899002940f0f2800346dde946014ff0ff37884240f0ec80ddf8d4e0bef1000f00f1e680bbf1000f00f1e280ddf880254398116a329f08431062349847ea09074e904ff6fe7087423398dc46a246b3464f945096519000f0e5834ff6ff70874240f0c1832398d0f888934ff420704f904ff4f07050904ff4a0705190d4e3012044904ff0000842ac40ad2046294602f037f906462046294602f032f94499002940f0a480dde94212914200f09f8011f8014b914240f09a80ddf8803541991a6a1143239a1962002a00f09080d2f870114ff0ff37002900f08b8048ea0b030022b2eb532f40f0848051f82320002a00f07f80a2eb010e5fea8e7279d16ff4403240f60f057244cff6fd75aa4270d301eb0e0c5cf8081c99426ad15cf80c1ca1f1011212f5803f63d3bcf1000f60d05cf8042c8a425cd1cef54032914258d82b90ddf880051d46806950b1eb080bf0070e10f80380012202fa0ef242ea0802c2546046002246ab01f061fe00284ff0000708bf4ff0ff373bd0ddf87c05c0b3a6f1011010f1021f4ff0000731d92b98a0f1011010f1021f2bd93198002300284e9301f07f822d98349920eae0722e9820eae07330988142a8bf0146359834914845a8bf4846359001f06dba012048904ff0000846af42ac3846214602f091f881463846214602f08cf8489951b34ff0ff37229a279b002f00f0478701f0bbbddde9460188423ff4e7ae024612f8015b8a42469200f0bf82821c469243788a4245ea032500f0b782c21c469283788a42319300f03486011d4691c078bb4644460002d1e6dde946218a42d0d0134613f8014b8b42cbd0951c56784a1bb2424ff0ff37c6d3aa198a42c3d1ddf8803543991a6a1143239a1962002abad0d2f87021002ab6d048ea0b0e0021b1eb5e2fb0d152f82e100029acd0891a8b07a9d16ff44033bc4640f60f070b44cff6fd77bb4267469ed3531853f8082c724597d153f80cccacf1011212f5803f90d328aa002b4ff0ff3782e8604026903ff48aaf53f8040c60457ff485afc1f540302a9984453ff67fafddf880051d468069664640b1ca080bf0070c835c012101fa0cf119438154c12efff46daf287801287ff469af2998002801f02380dde928b8a5f13f0700f0bcbf424501f01e850ef10202424501f019850ef10302424501f014850ef10402424501f00f859ef801209ef80260714693004fea460eca780e7905314fea820a1a917100baf1000f1b9101f0fb841b99002901f0f784ea1ac1f0f484924501f2f1843599b1eb0e02c1f0ec841b99914201f2e884349903eb0c06084301d000230be05feacc704ff0000306d1002e04d40aeb0600a842d8bf01231a9f1b9aa8eb07001890ddf87c0502fb0af9b0fa80f0400918430024c0074ff0000b19931f902e95cdf8709022e0dde9303002fb003c4a081cf80230339803f00f0803f0f0030344c907329808bf48ea0003ddf8a0800cf8023026988344dde9200181424ff0000088bf01200443e00708bf0f44e10704d0d94507d801f095bc00bf474518bfd94540f2a281b8eb070001f08b8439780b0901d0012211e0012801f083847b780bb102220ae00428c1f07c84ba78fb7852ea032301f07684042200bf2192a9eb0b02934201f26e8420901f9826930028c0d101f00f003390269809010128cde91d7432912dd14ff0000c2698ddf8a0801e9cdde91c97c007acd00ceb0b00b0fbfaf101fb1a0001eb0e02811952ea0100a0d4a9429ed2359882429bd2349800283ff482af2d98814294db2b98824291db2a988142bff68eaf29988242fff674af88e720f001014ff0000c16e0dde93030339f04fb00305308c45cd20704f00f0504f0f0043c44329a08bf45ea02042e9dc4540cf1020c6145b7d00beb0c02b2fbfaf404fb1a2704eb0e0907eb060859ea080427d4a84525d23598814522d2349858b12d9880451ddb2b9881451adb2a98804517da2998814514dadde930304fea580709fb00343398e35d03f00f0503f0f00303445feac870329808bf45ea00032e9de355501cb0fbfaf202fb1a0002eb0e04821954ea0200bbd4aa42b9d235988442b6d234980028a0d02d988242b0db2b988442addb2a988242aada2998844294dba6e70aeb0a1109fb02f2f5f7cefa00f040bd00234ff0010ac646fff77bb9002021903190052e01f0ac832098002801f0a883fbf7e4f8002801f0a38395f800a00af10600864241f09c8347f65420c2f207000768002f01f09483387b797bba7bfb7b40ea012040ea024040ea0365a5f1810010f1800fc1f08483387c797cfa7c40ea0120b97c40ea014040ea02601490813810f5817f41f27483baf1000f4ff000001b97339077d04ff0000e00270ae000bf00f100404ea941f826000137574506f1010e17d21bf807007646411e1e29efd90beb0700aaeb070146aa42ab02f054fa00283ff489ad469942984eaa0f4442f82600e3e7bef1000f329500f0df80aef1010b4ff0000a4ead09e0be46d3450af1010aa6f1010605f1040500f0cd804ea850f82a100029f1d40020534686421dd005eb80077a68b2f1ff3f18dc5a1c934514d0ba68b2f1ff3f11dc9a1c93450dd0fa68b2f1ff3f0adcda1c934506d03a690433002a00f10400e0d400e000221b9846ab42ac7746009402f092fa00283ff43bad42980028c0d04af20d3146a8c0f24e018847b9e7329501204ff0000e90e04446fff720bc474508bfd94541f0e882ddf87c05002818bf012019990840012840f06684dde9300318990efb03004fea5a0700eb56021b98009701901a9801f09ffb002840f0548401f0caba2f4600f04abc239839464eaa1e46644601f0cefa00287ff4f9acddf88005ddf8d4e08069a446334660b10021b1eb572f08d1fa0809f00701875c012606fa01f139438154ddf838912a46b9f1000f3ff4ddac002a4ff0ff371cbfdde9301050ea01003ff4d5ac4f997145fff4d1aca1eb0e018a423ff6ccac50996145fff4c8aca1eb0c0188423ff6c3acddf87c15002700293ff4bdaca8f101111d4611f1021f7ff6b6ac2e99002900f02b85299921eae1732a9921eae1742b998a45a8bf8a462d998b45a8bf8b4600f01dbd002002e04ff0000e012099f804200f21950602f00f0601eae575754389214d43ed0a99f800c08df808510e2599f801a099f8023099f80370349212f0200f08bf012506fb05f501fb05f54fead5258df809514ff00d0508bf022506fb05f501fb05f54fead5258df80a514ff00c0508bf032506fb05f501fb05f54fead5258df80b514ff00b0508bf042506fb05f501fb05f54fead5258df80c514ff00a0508bf052506fb05f501fb05f54fead5258df80d514ff0090508bf062506fb05f501fb05f54fead5258df80e514ff0080508bf072506fb05f501fb05f54fead5258df80f514ff0070508bf082506fb05f501fb05f54fead5258df810514ff0060508bf092506fb05f501fb05f54fead5258df811514ff0050508bf0a2506fb05f501fb05f54fead5258df812514ff0040508bf0b2506fb05f501fb05f54fead5258df813514ff0030508bf0c2506fb05f501fb05f54fead5258df814514ff0020508bf0d2506fb05f501fb05f54fead5258df815514ff0010508bf0e2506fb05f501fb05f54fead5258df816514ff0000508bf0f256e434e43f60a00288df8176140f024824fea0a6242ea0c41309a380602eb214b329940ea0340ddf88480ddf86ca0aef1010501eb204000261390cdf85ce0159507e001f01f000b3801367645834400f0f8814ea850f82610b1f1ff3ff1dd33469d421cd04ea800eb83004268b2f1ff3f16dc0133ab4212d08268b2f1ff3f0fdc0133ab420bd0c268b2f1ff3f08dc0133ab4204d002690133002ae1d400e00022504646ab40af009702f0d5f8002800f0d081409900291d9100f0ba819df820019df82111189650ea0125cdf868b000f09f819df824719df825613f0447ea0667149e9df81f019df81e112e444fea002c41ea00229df8220106eb2747139e9df823310004f71b1a9e40ea03604ff0010e06eb204b28920eeb520203141c920ceb010201f003000bf1010119910bf1020116911d992d97571e1e97f718f31a01312990101a2e910eeb5201319a3390d8074ff000093290f8072b913090269509e000bf2e982b9909f10109a94508442e9000f04e812d9809eb00034345f1db2a988342eeda28980028ebd0dde91f104ff0000a03fb01071e98032880f09c802998ddf88480269d0028dbd0dde91c10012309fb010c34984fea5a011cf8011000f0100083ea1014c8b201f00f035feaca7e08bf03091846002b18bf01202043012815d10aeb0b06964211db359886420eda7008395c42adeb5c01f00f0501f0f0011943f60708bf45ea0311269d395429980128a6d00af1010040081cf80000bef1000fc1b24fea111808bf00f00f085ffa88f0002818bf01202043012817d119980aeb0003934212db359883420fda5808395c42ae16f8086001f00f0501f0f0013143db0708bf45ea0611269d39542998ddf8848002283ff474af0af1020040081cf80000bef1000f00f00f03c1b208bf0b091846002b18bf0120204301287ff460af16980aeb00069642fff65aaf35988642bff656af7008395c42adeb5c01f00f0501f0f0011943f60708bf45ea0311269d395445e7ddf8b8c008e000bf33980af1040a50450cf1020c3ff458af34981cf8016c00f010030120f6b2002580ea13130f2e88bf01251d43012d0beb0a0815d1904513db3598804510da4fea58053609795d42a8865d01f00f0401f0f00131435feacb7008bf44ea061179551cf8010c10f00f0e704618bf01201843012818d108f10106964214db3598864211da7008395c42aa12f80e6001f00f05329c319a45ea061501f0f001002c08bf41ea06053d549cf80000c6b200200f2e88bf01201843012816d108f10205954212db359885420fda690830097e5c42ad285c06f00f0506f0f006309c0643002c08bf45ea00167e549cf8000010f00f06304618bf0120184301287ff481af08f103039342fff67caf35988342bff678af5808395c42ab9b5d01f00f06329d46ea031601f0f001002d08bf41ea03063e5466e74af20d3146a8c0f24e0188479df81c019df81d11dde91aba159ddde917e605e09df81c019df81d11ddf85ce040ea012002e6002007e000274ff0010ac646fef73cbd4ff0ff303390339ffff7aab94446fff79eb80f4600e04f1cd90708d03199994205da3978339c01f00f0121433970b8f1010f05f1010700f0d8809b4580f2968007fb09045feacb7171465e4609d014f80e10349e01f0f00141ea0a0104f80e10329961452144319614da04eb0c060c4604f8012bb4426dd28f1cb7424a706ad2cf1cb7428a7066d20f1dca70b7423946edd360e00f465ee0102190427ff45ea92b9ab84652ea0b2c4ff0ff373ff458a958ea092226923ff453a9bcf5207f3ff64fa9269ab2f5f07f3ff64aa9baf10f0f2191cdf87ca03ff643a92e9910293ff63fa9ddf88005479a016a1143ddf87c250162002a00f08a8332992a9a614449428a42c0f283833499329a0027891a2a9a8a42bff625a926992d9a49428a42fff61fa92d9a31988242bff61aa9002b00f0858326eae671209134992eeaee7ea942a8bf2946dde9303034919842a8bf1846319077e34f1cd90708d03199994205da3978339c01f00f0121433970b8f1020f05f1020739d09b4536da07fb09045feacb717146d8460ad014f80e10ddf8d08001f0f00141ea0a0104f80e1032996145214414da04eb0c060c4604f8012bb4420fd28f1cb7424a700cd2cf1cb7428a7008d20f1dca70b7423946edd302e00f4600e04f1cd90707d0984505da3978339c01f00f0121433970ef1c3599691a11f1040f11d9229a279b2498013383422c982f9900eb01077ef432ab00f071be00bf359904378f42edd09b4539da07fb09045feacb7171465d4609d014f80e10349d01f0f00141ea0a0104f80e1032996145214418da04eb0c060c4604f8012bb44280f0cd8001f10208b0454a700cd201f10308b0458a7007d201f10408ca70b0454146e9d300e08846d90709d09d4207da98f80010339c01f00f01214388f800109b4536da791c01fb09015feacb7474465d4609d011f80e40349d04f0f00444ea0a0401f80e40329c644501eb040615da01eb0c04314601f8012ba14280f09580b11ca14272700ad2f11ca142b27006d2311df270a1420e46ecd300e03146dc0707d09d4205da0c78339d04f00f042c430c709b4536dab91c01fb09015feacb7474465d4609d011f80e40349d04f0f00444ea0a0401f80e40329c644501eb040615da01eb0c0400bf314601f8012ba14260d2b11ca14272700ad2f11ca142b27006d2311df270a1420e46edd300e03146dc0707d09d4205da0c78339d04f00f042c430c709b45bff64baff91c01fb09015feacb7474465d4609d011f80e40349d04f0f00444ea0a0401f80e40329c644501eb040615da01eb0c0400bf314601f8012ba1422ad2b11ca14272700ad2f11ca142b27006d2311df270a1420e46edd300e03146dc073ff41baf9d42bff618af0c78339d04f00f042c430c7010e701f10108d9077ff442af4ae7711cdc077ff478af7ee7711cdc07acd1b3e7711cdc07e3d1fde6b8f1010860d01bf8010b411e1f29f7d341b22029fef698af07eb400117f81000497840ea0122c12afef48eaf284631464eab00f0a9fd0028e2d1fef785bf002400235345b8bf5c45bef680af289908eb01065645bef67aaf5d45bef677af991a8e427ef773af211a8d427ef76faf9f1b1946b8bf3146b342e0464ff0000cd8bf6746be44d31b671bd8bf6746c61ba542aaeb0100c8bf2c46834238bf1846abeb04039e422e9328bf1e46349b2b96994507eb080329932d9442d1299b2d9f9f422fdd4ff0010c3be0ddf87c05002800f09381a9f1011010f1021f4ff000077ef635af2698a0f1011010f1021f7ef62eaf3198002300284e9300f0cf842d9820eae0722e9820eae07335985045a8bf5046359034982b998842a8bf0846349000f0bdbc299b2d9f7145a7eb0303b3fa83f34fea53134ff00007c8bf012703ea070c2b9b002b00f058814ff0040a2aea8e071a970eeb000743184fea5e06013f01f0010203f001041b967e0819962aea8706171918963294c61bdde930745018274435920eeb02048eea01024908179159082d9b169179192e9dc91a4fea560ba94238bf0d4601212e95bbf1010f88bf710801f0030321f00301129161082691410821913499159301eb5000411c03381490d00720904ff0ff3000eb56002a906ff0010009eb540300eb56002890981e13906ff00200ddf844e11c9600eb5600339e4ff00008109111931f90cde91dec08e02e9808f10108339e80454ff000073ef495ae6fea08014046bcf1000f2b9a18bf50182999359b421802fb0e910bb9002305e01b9b1a9ccb5ce34003f00f033193329b0bb9002304e0199b189ccb5ce3401b0130932d9b209f03443498002f03fb060a2198269f504439441cd15a46f4f7b3fadde91dec359840b11799319a1af8010000f0f00010440af8010032980028b6d01699309a1af8010000f00f0010430af80100ace703fb06f4bcf1000f02fb0ef23ed01c980228ddd31398149910442144002200eb0b039c785f782a9dc4f3031444ea071701eb0b049542a770cad05f7810f80b50c7f3031747ea0517289d67709542bfd010f80b7013f8015cc7f3031747ea05171f9d01f80b709542b2d013f8017c13f8023cc7f30317043247ea031304389345a1f1040104f8013cc9d1a1e71c9b022b9ed31c9b082b01d200222be0119bddf848e003eb020c109a144400220ceb02077d781cf80260c5f3031545ea0616a51805f8016cbe787b78c6f3031646ea0313a354fb78be78c3f3031343ea06136b703b79ff780432c3f3031343ea07139645ab70dbd1159bdde91dec002b3ff468af531ccf5c8e5c159cc7f3031747ea0617012c87543ff45caf941c0f5dce5c159dc7f3031747ea0617022dc7543ff450af0a44d278095dc2f3031242ea0111015546e70027fef7a7bd00224ea8cde905402b9806eb0a0104903598cde902510190dde93210349f009701f089fb0746fef792bd00214ff0000e2091dde9280141ea0021b1eb5c0f28bf4fea5c01269ab1eb520f28bf51081c91013938bf394610912d99beeb0104d8bf3c463198411a9142a8bf11468c421e91bef66dad28984fea092a4fea002932982a994fea0b230f181c9920984a004943890028921b918742c8bf3846349d0ceb0702aa424ff000012a92b8bf1546bcf1030f38bf0121269e0022032e38bf012211431a91410819912b990aeb080219444b08b3eb520f219b1d9000f1010c4fea031015904fea520243ea031038bf4a08149029986910484429951891824228bf0246109b4ff0000a03fb03f080000d906ff0080000eb420016902d987045c8bf8646339835992b9701fb0e002e99309000203290701e1790b01e139058001290781c119008010f9041ea011026460e904fea5c00cdf830c00b9010e001780f9a01f00f0111430170359c319e30980136204430901e9800278642bef6e8ac2699f04308448642b8bf304628996fea400008440128319638db1b994ff0ff3200fb10101c99a1eb0a01012902eb41013bdb01fb01f2824237dd1699a1eb4a0202f10803032b02f1060126db01fb01f7874222dd052b02f1040121db01fb01f787421ddd072b02f102011cdb01fb01f7874218dd0af1040a092ba2f1080103db524382420a46dbdc08310ee02d983399359c219a4ff0000a39e00af1010a04e00af1020a01e00af1030abaf1010f29db08318a1f5243824224dcbaf1020f16db0a1f5243824215dcbaf1020f0fd08a1e5243824211dcbaf1040f08db01fb01f282420ddcbaf1040a01f10801e1d14ff0000a07e0aaf1010a04e0aaf1020a01e0aaf1030a2d983399359c219a319e30442b9b00fb04192a985344102aa0eb0a0800f08b8034988342b8bf18461d99814239da1d9f199af90739460ad0199f219a19f8071001f0f001114309f80710dde90b21b2eb600f09eb020715da149d09eb60063a4602f8015bb2420fd2ba1cb2427d700cd2fa1cb242bd7008d23a1dfd70b2421746edd302e03a4600e07a1c359c319ec70707d0814205da1078159900f00f0008431070ddf880c0e045c8bfc4462998844541da5feacc700bd04fea5c0019f80010219a01f0f00111430cf1010c09f800101898b0eb5c0f1add1898309e149d4fea5c0209eb0007b018044604f8015bbc4210d2811cb94225700fd2c41cbc420d700ad20436b118b9422570ecd305e009eb5c0102e0411c00e021462998359c319ec00708d02998844505da0878159a00f00f00104308702e9810280bd1012002901f982099349a01904846cdf8008002f0c7fbcae6b6fa86f01a99400908434ad11798864247d01399701e891b8842b8bf01466fea4100129908440128c0f29f800d99329a00fb101010998a1a4ff0ff31012a01eb42016ddb01fb01f7874269dd5100a1f1090202f10807032f02f1060157db01fb01f6864253dd052f02f1040152db01fb01f686424edd072f02f102014ddb01fb01f6864249dd3299092f01f104013291a2f1080103db524382420a46d9dc08313ee020988342d8bf034634988045a8bf80464345bff66daed80709d0580819f800102e9a01f0f0011143013309f800105908b1eb680f17da309f0e9d09eb68027c18264606f8015b96427ad2a01c904235707bd2e61c9642057076d20437781890423570ecd371e009eb53006ee0329a013204e0329a023201e0329a03323292329a012a23db08318a1f524382421edc329a022a19db0a1f5243824200f3ba80329a022a11d08a1e5243824200f3b580329a042a09db01fb01f2824200f3b080329a0831043a3292ded1002032901198329900eb010c20988342d8bf0346ddf8d0e0f445b8bfe646734543dad80709d0580819f800102e9a01f0f0011143013309f800105a08b2eb6e0f17da309e0e9d09eb6e07b018044604f8015bbc4212d2811cb94225701ad2c41cbc420d7015d20436b118b9422570ecd310e009eb53010de0601c02e0411c09e030465feac8713ff4cfad4345fff6c6adcae521465feace7007d0734505da08780f9a00f00f001043087032982a99c0430e18012002901f98209f349c01904846394622466346009602f0abfabe42d8bf3e46a045a8bfa0464645bff6a5adf00709d0700819f800102e9a01f0f0011143013609f800107108b1eb680f17da309b0e9d09eb68025c18274607f8015b974210d2a01c90423d700fd2e71c974205700ad20433581890423d70ecd305e009eb560002e0601c00e038465feac8713ff473ad4645fff66aad6ee53298013858e73298023855e73298033852e700223098359f09eb00014ea808902998009707902898cde905402698cde903102a98029034980190dde9321001f0c1fd0746fef73eba4ff0ff300df2345dbde8f08fb81a18bf4ff0ff300df2345dbde8f08fb0b586b008b3d0f87001f0b10023b3eb512f1ad150f82130bbb11b1a9d0714d16ff4403540f60f041d44cff6fd74a5420bd3184450f8085c8d4206d150f80c1ca1f1011515f5803f03d24ff0ff3006b0b0bd50f8045c8d42f7d1c3f540339942f3d80028f1d0037813f00b0fedd1144601ab0022002500f011f80028e5d00198dde903122060481c40086160a260e06028462561656106b0b0bd00bf91429cbf002070472de9f041855c4ff0050c6e0758bf4ff0030c15f0f30f02d1891a614502d20020bde8f0818418627894f802e0002e05d5e078267942ea0e2240ea062e002a4ff0000018bfbef1000fead0b2f5207fe7d8bef5f07f88bfbde8f081082d38bf0120072d18610ad8501c400800fb0ef0a1eb0c018842d3d804eb0c0828e004eb0c080efb02f5002000bf00eb0c04a142c6d008eb00073e78360903d00127b54213d2bde70c1b012cbad07e781eb10227b5420ad2b4e7042cb2d3bc78fe7854ea0626add00427b542aad3ad1b3844dcd158600120c3f800809a60c3f80ce0bde8f0812de9f04f89b0dde91257069005fb07f000294fea4007029100f0d48100200390089002984ff0010e00264ff0000b4ff000084ff0000c4ff000090497079205930be000bf4ff0000c4ff00308cdf80ca009f10109814500f0ba81bcf1000f40f0b4810699b8f1020f11f809a0ead0b8f1010f15d0ddf81cc0b8f1000f40f086805fea1a100af00f01089100f092800499814280f09a804ff0010c4ff0000882e1079abaf1000f00f07f800499514576d3a1eb0a010491089901eb01100901019114e000bfa15d089f01f0f0013943a1550136aaf1010a4ff0010eae4224bf00260bf1010bbaf1000f00f0598113998b45d1d20bfb0324bef1000fe3d0a91bb1eb5a0f88bf4fea5a0141b38d0804eb060817d0002208f10107a2eb850c6ff003057b1904350ceb05020432d870187158719871f5d1059b079a2f1d11f0030504d10ce0002711f0030508d0012d08f8070004d04744022d787018bfb870aaeb410abaf1000f0e440dd0129dae42b5d2a15d019f01f00f013943aaf1010a4ff0000ea155afe7129d4ff0000aa6e7039a52ea0a2213d004990392914280f08f804ff0010c4ff0000853e74ff0000c4ff002084ee74ff0000c4ff00108f4e000214ff0010c4ff00008039142e7089a091a049102eb02111201cdf80090019214e000bf1af80670089a07f0f00717440af80670013601384ff0010eae4224bf00260bf1010b002800f0d680139a934580f0cd800bfb03cabef1000fe2d0ad1bb5eb500ff14688bf45087db3ac080aeb06081ed0002308f10107a3eb840c6ff0030400bf07eb040e04340ceb040304338ef803108ef804108ef805108ef80610f0d1059bddf81cc0271d15f0030404d10ce0002715f0030408d0012c08f8071004d04744022c797018bfb970a0eb45002e4488b1129dae4210d21af80620019f02f00f023a430af8062001384ff0000e0028abd180e000bf129d0020ce469de7891a0491089901eb011009010191114613e000bf1af80620089c02f0f00222430af80620013601394ff0010eae4224bf00260bf1010b002953d0139a9345bff4ccae0bfb03cabef1000fe3d0ad1bb5eb510f88bf4d085db3ac080aeb060819d0002208f10107a2eb840c6ff0030400bf3a1904340ceb04030433d070107150719071f5d1059bddf81cc0271d15f0030404d10de0002715f0030409d0012c08f8070005d04744022c787018bfb87000bfa1eb45012e4489b1129dae42b6d21af80620019f02f00f023a4301394ff0000e0af806200029b0d103e000bf129d0021a4e74ff000084ff0000c029852e64ff0010c4ff0000803e04ff000084ff0000c0298ddf8009045e60120012107e04ff0010cb8fa88f0049f8cf001014009b7fa87f252091140084009b0bde8f08f00bf2de9f041826882b10121816007220020511e20fa01f1c90708bfbde8f0814ff0ff3191400843bde8f0818c46056841688d42e9d06b1c03602c78ff2c11d18b4200f0dc80ac1c046095f90120b2f1ff3fd6b200f3d080bf2e22d806f03f0602238fe062b2b2f1ff3f02dd07222046cfe7bf2c09d804f03f070e224ff0010e4ff000084ff0000c2ee0df2c0fd804f01f0715224ff0000c4ff001084ff0010e22e0df2e0fd806f01f0603236ae0ef2c0fd804f00f071c224ff0010c4ff000084ff0010e10e0ef2e58d806f00f06042358e0f02c40f093804ff0000820224ff0000c4ff0000e00278b424fea072603d0af1c07606d7803e00125856000251f46c02c45ea06046cd35b1a7d1ab3fa83f3b5fa85f55b096d092b434fea042403d001258560002503e07e1c06603d783746b8f1000f44ea050453d17d1ab5fa85f56d092b434fea042403d001258560002503e07e1c06603d783746bcf1000f44ea05043ed1791ab1fa81f1490919434fea042131d001238360002030e0f02e3ed1002605234ff0010e00250127ea074fea062617d18c4215d0621c0260247801379f4246ea040623d000bf8a424fea062609d0541c0460127801371643bb422246f3d116e02246013701254ff0000e9f4214468560dad112e07b1c0360387840ea0104bef1000f20467ff413afbde8f08114465feace7202d0091b8e4204d9012181600020bde8f081a11907462046002263463960bde8f041ffe78446081ab0f5806f01d90020704761454ff00000fad0002a18bf70472de9f04fabb09fed251a9fed250a9fed253a9e4600264ff0ff380df12c090df10c0ab7ee002ab0ee084ab6ee005ab1ee006ab9ee007a00206746019009e000bf49f826500af8064001368f42bc4600f0cb8217f8012b501e01280fd8012a23d18f4274d09cf901000cf10207b0f1ff3fc5b240f38f8000220724f5e1052a37d0042a30d0032a49d1002e00f0d982701e59f820501af8004093e200bf0000004f000000cf000000008f4254d00cf102079cf801008f4247d09cf802200cf103078f4240ea02203fd09cf803200cf104078f4240ea024037d09cf804200cf10507130600223ae0002e00f0aa82013eace7022ec0f0a582b21e59f82230701e59f8205049f820301af802301af800400af8003049f822500af8024096e702f0f80010281fd1002e00f08b82701e1af80030002b40f0858259f82050152a6fd16d420022002406463de20023012206e001220724002588e101220020002343ea000501242fe2a2f12000062827d8002e00f06782731e1af80300002800f0618259f82300242a40f0858080f00045012400221e4617e2bf2d5ad801220292002205f03f000e244ff0000b00928f4200f019819cf802300cf10308002215e1322a51d002f0fe0030284dd0402a40f0a380032ec0f03682701e1af80020002a00f03082b21e1af80230002b00f02a82033e1af80630002b00f0248209eb8202d2ed000a09eb860209eb8000d2ed001ad0ed002a70eee10a62eea00a71eea00acee1002800f00e82023e1af80600002840f0088259f82600122a58dc102a00f03081112a00f03381122a40f02c81454354e1df2d44d8002205f01f00152400924ff0010b0122bae0002e00f0eb81a2f13100013eb0fa80f01af806304009834240f0e08159f82600302a7bd100ee900af8eee00a94e1002b00f0d481023e1af80630002b00f0ce8159f82630212a40f3ed80222a00f02581232a00f02981252a40f03a8100ee903a01ee900af4ee610af1ee10fa48bff0ee601a11ee905a71e1ef2d72d805f00f001c24012274e0a2f11303022b80f0c480002d00f0a381b0f1004f08bf15f1010300f0eb80132a40f0f18090fbf5f5f1e0a2f1480010f1070f4ff00000c0f05f81002e00f05c81013e1af80600002800f0858109eb8600d0ed000af4ee420af1ee10faf5ee400ac8bff0ee420af1ee10fa48bff0ee430a432a72ee601a00f3b480412a00f0f380422a00f00f81432a40f0008161eea10a72ee600a1de1312a40f0948000ee900a20f00040b0f1ff4f4ff00002f4ee410aa8bf0122f1ee10fa4ff0000080f21c81f4ee400af1ee10fa00f11681002a40f01381fdeee00a10ee905a98e0f02d4ff0000040f00981002220244ff0000b009202928f427ff4e7ae00230122b8460002c02d43ea000535d3781aa8eb0103b0fa80f0b3fa83f340095b0918434fea05270ebf18f8013b00230122bbf1000f43ea07051fd1a8eb0103b3fa83f35b0918430ebf18f8013b002301222f02009d002d43ea07050ed1a8eb0103b3fa83f35b0918434fea05230ebf18f8010b0020012240ea0305474602984ff0ff3838b1601e25fa00f0c0071cbf08fa04f005430024a3e0162a10d1a842b8bf054637e0202a4cd0212a51d100ee903a01ee900a70eee10a8ee005442ae0a842c8bf054626e0451b24e0002800f1c680def800200546824238bf15469042019888bf0120019015e0442a48d0452a5fd0462a4ed161eea10a61e00025132a4ff0000208bf4ff00045002469e090fbf5f202fb15050022002462e000ee903a01ee900a61eea00a57e000ee900af5ee400af1ee10fa00f0928001ee903ac1eea00a4ae000ee903a01ee900a71eea00a43e000ee903a01ee900af4ee610af1ee10fac8bff0ee601a11ee905a37e060eea01a70eea00a74ee600a61eea00a2ce0f4ee450af1ee10fa18d570eea01a60eea10a22e0f4ee450af1ee10fa15d560ee861a60eea11a60eea10a16e060eea00a13e060eea01a60eea10a0ee071eea10a61eea00a72ee600a07e061ee870a61eea00a61eea00a70ee820a10ee905a012400224ff000005ab9202e09d0002c3ff436ad25f00040b0f1ff4ffff630ad00202bb0bde8f08f56b3701e1af8001059f82000d9b102ee100a20f00040b0f1ff4f4ff00001b4ee412aa8bf0121f1ee10fa4ff00000e4dab4ee402af1ee10fadfd40029ddd1bdeec20a10ee100a019adef804101143cef804102bb0bde8f08f00202bb0bde8f08f80b5002970d090f900c0bcf1200f5ffa8cfc07db012010604ff0010ec3f800c0704680bdacf1c20ebef11d0f0fd801295ad090f9011011f1410f55dc02211160407800f03f006cf38a108446e4e70cf0f00ebef1e00f23d1032945d390f901e01ef1410f40dc90f9021011f1410f3bdcbcf1e00f5ffa8ef101d1a02934d3bcf1ed0f01d19f292fd80321116042784fea0c3189b2807802f03f0241ea821136e004294ff0000ebbd3acf1f50111f1050fb6d390f901e01ef1410f15dc90f9021011f1410f10dc90f9031011f1410f0bdcbcf1f00f5ffa8ef101d1902904d3bcf1f40f03d18f2901d9002080bd042111604ff4e011427801ea8c4c817802f03f02c0784cea023201f03f0142ea811100f03f0001eb000c7fe72de9f04188b04af2bb261c46c0f24e0613460a46ddf83880002706f1ae05214627706770a770e77027716771a771e77127726772a772e77227736773a773e77327746774a774e77427756775a775e77527766776a776e77627776777a777e777a8470028c8f800705cd020786178a278e3780843104318434ff0010008d0e179a27911431ebf617a227a51ea020102d108b0bde8f081a07b14284ff00000f7d1217d8df8040041f0010121758df805008df806008df807008df808008df809008df80a008df80b008df80c008df80d008df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f0001a92046b047c8f8000018b1012008b0bde8f08106f1520120468847d8f80000002818bf012008b0bde8f081002804bf4ff0ff3070472de9f04f95b00df1200c8ce80f00f8f7fefc002800f004824ef66c6a4ff29c5bccf2ff0ac2f2290b4ff6ff760024012547f67f797f2707e000bf48f2a351c0f24501bff32f8f8847dbf80080bff35f8fdbf80000bff35f8f01466ff39601b1f1005f07d10168514504d14168b1f57d7f00f093804ff47d70f9f721fc002800f0cf814ff60c41cff6ff7142182e3182f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f8194482f81a4482f81b4482f81c4482f81d4482f81e4482f81f4482f8204482f82144a0d14ff47d71c0f800a041608573a0f8c49180f8c371068446848684c684068546858685c685068646868686c686068746878687c6875be8001f41457ff45aafbff35f8f4be8000121b15be8001f4145f8d04fe7d0f87011209e4ff0ff3029b30022b2eb562f21d151f82600002800f03481431a98074ff0ff3017d16ff4403240f60f071a44cff6fd77ba420ed3ca1852f8080cb04240f0208152f80c1ca1f1011010f5803f04d24ff0ff3015b0bde8f08f002a4ff0ff30f8d052f8047c8f42f4d1c3f540339942f0d810ad104600222b460024fef716ff002800f0fe80239b0f22990603f00f0002eae17101fb00f289214a43d20a8df830200e2213f0200f08bf0f2408bf012200fb02f201fb02f24fead2228df831204ff00d0208bf022200fb02f201fb02f24fead2228df832204ff00c0208bf032200fb02f201fb02f24fead2228df833204ff00b0208bf042200fb02f201fb02f24fead2228df834204ff00a0208bf052200fb02f201fb02f24fead2228df835204ff0090208bf062200fb02f201fb02f24fead2228df836204ff0080208bf072200fb02f201fb02f24fead2228df837204ff0070208bf082200fb02f201fb02f24fead2228df838204ff0060208bf092200fb02f201fb02f24fead2228df839204ff0050208bf0a2200fb02f201fb02f24fead2228df83a204ff0040208bf0b2200fb02f201fb02f24fead2228df83b204ff0030208bf0c2200fb02f201fb02f24fead2228df83c204ff0020208bf0d2200fb02f201fb02f24fead2228df83d204ff0010208bf0e22424360434843c00a8df83f00c3f3001006900ca84a43cde9045008ac0b9dd20addf88890ddf884a0ddf87c8013cc1e9e8df83e2022462b46cdf80c90cdf808a0cdf80480009600f02df8dde91201a2455044c8bf5446a9454944c8bf4d46b042b8bf06464145b8bf8846a64211dd4146a8450edd249800680f280ad8249f491b321b07ebc0030130dc801d815a8199813860002015b0bde8f08f4ff0ff3015b0bde8f08f2de9f04f8fb0dde91c5a0e9129691e9eddf868c0002900f05c812f68002e40f05381874200f050819af80f100f2940f04b819af80e100e2940f046819af80d100d2940f041819af80c100c2940f03c819af80b100b2940f037819af80a100a2940f032819af80910092940f02d819af80810082940f028819af80710072940f023819af80610062940f01e819af80510052940f019819af80410042940f014819af80310032940f00f819af80210022940f00a819af80110012940f005819af80010002940f000811b9fb2eb0c044ff00001d8bf0c46db1bd8bf0b46d5f808e0ea681d46e919189fa444aeeb0403a7eb0c070694bb42b8bf1f46199b521b0b91591a8a42b8bf1146012f0c910797c0f2e5820c990129c0f2e1820ef101014c08069900eb6c000a9048100990042020ea81000590079801310138cde903014810029004fb05f000f102094ff0000acde9005c089407e000bf0c980af1010a8245a14400f0bb821c980e9b01680aeb050000fb04120b980a9f504400fb03705feacc7303d1099fdde9063810e0099b2e46d35c059d0778eb4007f0f00703f00f033b4400f8013b049bdde902783546d619da074fea580b0d9208d131465a466446f2f712fba446089c68e000bfb8f1020f64d3abf10102032a04d200211bf0030e35d15be06ff0030202ea58034f44b646c3f1000c3944471c8e18f578b478c5f3031545ea0415bc18e5703579f378c5f3031545ea0313237173793579c3f3031343ea05136371b3797679c3f30313043243ea06130ceb02060436a371dcd1dde9005c089c111d76461bf0030e26d04b1cf75c3246765cc7f30317ddf804c047ea06171646bef1010f475417d08f1cd65dd55cc6f3031646ea0516009dddf804c0bef1020fc654164608d03144c978f25dc1f3031141ea0211c15500bf5feac8713ff45aaf0d9a16f80b10002a4ff0000208bf042210f80b30d14003f00f0242ea011100f80b1047e7874200f095810026a1e16d68002d00f0018200244ff000090ae000bfdde90c14d8f804500c44ddf874a0ac4280f0f281ddf870800d94d8f800100f5d3e0903d001210c910ce000bf2e1b012e00f0e28121444d78002d00f0628102210c912e4617f00f011e9f1af801a0b7fa87f74fea571718bf012147ea010e1b9f4fea0a1b16f003014c4600f08980cde90a16d8f808100026b9fbf1f404fb119161449142a8bf012606ea0e06012e18d13c449c4215db189da94212da199dac420fda0e9d4e0804fb0504c907a75d6ff00f01554604bf0f215d4639401b9f2943a1550a990b9e012909f1010458d0d8f80810b4fbf1f606fb1141002461449142a8bf012404ea0e04012c18d1f4199c4215db189da94212da199dac420fda0e9d574604fb05044d08c907665d6ff00f0104bf0f215f46314039431b9f61550a990b9e022909f102042ad0d8f80810b4fbf1f505fb1141002461449142a8bf012404ea0e04012c19d1ec199c4216db189da94213da199dac4210da0e9d574604fb05044d08c907665d6ff00f0104bf0f215f4631400b9e39431b9f615509f10304711e0329b14405d23ae700bf0434a1453ff436afd8f808100026b4fbf1f505fb114161449142a8bf012606ea0e06012e1bd13d449d4218db189eb14215da199eb54212da0e9ed04605fb06054e08c907af5d6ff00f0104bf0f21d84639401b9f41ea0801ddf87080a955d8f80810661cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e1cd13d449d4219db189eb14216da199eb54213da0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa955d8f80810a61cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e1cd13d449d4219db189eb14216da199eb54213da0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa955d8f80810e61cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e7ff466af3d449d42fff662af189eb142bff65eaf199eb542bff65aaf0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa95544e7042e78d38e78c97856ea012673d0042188e61b99002901dd012609e0b1fa81f149090027bcf1000fc8bf012707ea0106d5f808e0d5f80cb00bfb0ef9b9f1000f59d01e990024b1fa81f149094ff0ff380d91f1070c9107e000bf01340bfb0ef94c45a8f1010846d20c991c9e0029214618bf09eb0801b1fbfef536680ef1010705fb1e117f0805fb07664f08f65d042727ea8107fe4016f00f094f4618bf01270d9e3743012fd8d161449142d5db1b9f3d449d42d1db189fb942ceda199fbd42cbda0e9f1af8096005fb070e4d081ef80570c90707f00f0907f0f00747ea06071c9908bf49ea06170ef80570d1f808e0d1f80cb0b1e70fb0bde8f08f00bf002804bf4ff0ff3070472de9f04f99b01c461546cde90e01f7f772ff002800f0fb814ef66c6b4ff29c59cde90c54ccf2ff0bc2f229094ff6ff7a0024012547f67f787f2607e000bf48f2a351c0f24501bff32f8f8847d9f80070bff35f8fd9f80000bff35f8f01466ff39601b1f1005f07d10168594504d14168b1f57d7f00f0a3804ff47d70f8f793fe002800f0c4814ff60c41cff6ff7142182e3182f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f8194482f81a4482f81b4482f81c4482f81d4482f81e4482f81f4482f8204482f82144a0d14ff47d71c0f800b041608573a0f8c48180f8c361a0f820a0a0f822a0a0f824a0a0f826a0a0f828a0a0f82aa0a0f82ca0a0f82ea0a0f830a0a0f832a0a0f834a0a0f836a0a0f838a0a0f83aa0a0f83ca0a0f83ea059e8001fb9427ff44aafbff35f8f49e8000121b159e8001fb942f8d03fe7d0f87021279e249d4ff0ff302ab30021b1eb552f21d152f82500002800f01881811a88074ff0ff3017d16ff4403340f60f070b44cff6fd77bb420ed3571857f8080ca84240f0048157f80cacaaf1011010f5803f04d24ff0ff3019b0bde8f08f57f8040c50454ff0ff30f6d1c1f540318a45f2d8002ff0d0baf1c10fedd33878012840f0e580b0060f2206f00f0102eae070484389225043c00a8df854000e2016f0200f08bf012001fb00f002fb00f04fead0208df855004ff00d0008bf022001fb00f002fb00f04fead0208df856004ff00c0008bf032001fb00f002fb00f04fead0208df857004ff00b0008bf042001fb00f002fb00f04fead0208df858004ff00a0008bf052001fb00f002fb00f04fead0208df859004ff0090008bf062001fb00f002fb00f04fead0208df85a004ff0080008bf072001fb00f002fb00f04fead0208df85b004ff0070008bf082001fb00f002fb00f04fead0208df85c004ff0060008bf092001fb00f002fb00f04fead0208df85d004ff0050008bf0a2001fb00f002fb00f04fead0208df85e004ff0040008bf0b2001fb00f002fb00f04fead0208df85f004ff0030008bf0c2001fb00f002fb00f04fead0208df860004ff0020008bf0d2001fb00f002fb00f04fead0208df861004ff0010008bf0e2001fb00f002fb00f04fead0204ff000038df8620008bf0f235943299d51430020c90a002d8df863103ff427af289cc6f30010a7f13f0810ae0b9001e0013d1cd014f8010b411e1f29f8d341b220290fdb08eb400118f81000497840ea0122c12a06d3384651463346fef794f80028e5d14ff0ff3019b0bde8f08f0d9c2698ddf894b0a042c8bf04462a980894dde9289406300790cde909a704e058440b38013c834653d019f8010b1f28f6d908eb400118f81000497840ea0122c12ad8d3384651463346fef766f80028d1d00b980c9f069015a8cde90460ddf898a0229ddde90e010d9b239e3a46cde902ba00950196fff740fadde91201bb4558445144d8bfbb46a8422a46b8bf0246b142a8bf31465a4517dd089b994214dd2a9b1b680f2b10d8089ea2eb0b02a1eb060c079901ebc307ba802a9a21f833b0591c7e80a7f806c01160dde909a710aea8e7002019b0bde8f08f2de9f04fd3b0dde95ce5dde9648cdde9629a0d9075b1dde96050dde95e47a94224eae47b27eae774a8bf29468242a8bf024602e000244ff0000b09eb0e0008eb00055845c8bf83468d420ceb0a07b8bf2946a24588bf54469742b8bf3a468b45b8bf944202db53b0bde8f08f669804f0010701974fea5b07033809970bf101070090580008974e080c90002701200df14c0e0df10c0c02948ce80e00cde90a6b06e000bf0798012700284ff00000dad0c00707906698069708bf0830ddf800906ff007086ff004044ff0000b4ff0000a0f9018bf09f10809cdf838900f994fea5a00085c102300f0f002cc46a14610901194cdf8488000bf1cf8041f0eeb08001cf8014c9cf8015042ea141704f00f041443447242ea111401f00f01847211430eeb0904217242ea15119cf802600772617205f00f011143a17242ea1611817306f00f01114308f10808083b09f10809c173d1d11098ddf83890000110224b465d4600bf13f8041f0eeb050713f8014c5e7840ea141804f00f0404437c7440ea111401f00f010143f97440ea161193f802c0397506f00f010143797540ea1c11b9750cf00f010143083a05f1080587f81080bc74f975d5d1ddf84880119c0af1020a08f120082034baf1100f0bf1200b84d101980699049a48400299059b00eb01090399dde90a6b9145bff65baf0d9803fb090807e000bf0c9809f1020991458044bff64faf0d98dc4609fb03045feacb700bd0099dddf820c0605d1ef8007000f0f00007f00f0738446055b6eb5c0f28d94fea5c0a04eb060b464616f80a0006eb0a041ef80050204600f8015b58451dd205781ef80570a51c5d45077017d228781ef800002870e51c5d4510d2287804361ef80000287006eb0a055d45ddd306e000bf04eb5c05c80705d1b4e7651cdde90a6bc807afd08c45adda28781ef8007000f00f0007f0f00738442870a3e72de9f04f8bb01d9d6c68072c00f286802cb12f89b94200f08180012c7bd100287cd0d0f87061002e78d00027b7eb512f74d156f82170002f70d0a7eb060c5fea8c776bd16ff440370ceb070e40f60f07cff6fd77be4561d306eb0c0e5ef8086c8e425bd15ef80c6ca6f1011717f5803f54d35ef8047cb74250d1ccf54037be424cd8bef1000f49d0032e47d39ef80070022f43d19446aa69ddf86ca0dde91987dde917b9b2b14fead10801f00704da464ff0010b4f4612f808900bfa04f444ea090402f80840d346ddf86ca0b946dde919876c6805eb44021181721e1c9e01346c600996169e0ef101010396159e0a950296149ecde904b9cde900366346cde90687cdf820a0fbf795fa6968013969600bb0bde8f08f6f89b94204d14ff0ff300bb0bde8f08f022c3ff479afaf89b942f4d0032c3ff473afef89b942eed0042c3ff46daf2f8ab942e8d0052c3ff467af6f8ab942e2d0062c3ff461afaf8ab9427ff45dafdae700bf70b5049d8b42c8bf19469542b8bf2a46914205dadde905e3bef1000f01d103b170bdbef10f0c18bf9c46cb070ed04b10c45cbcf1000f03d004f00f05ae4504d904f0f00444ea0e04c45401314c1000eb61035510bcf1000f00eb620049d0ac425fda00bf1c78764604f00f05beeb141f38bf2609754598bf754645ea06141d4605f8014b854265d25c78764604f00f05beeb141f38bf2609754598bf75469c1c45ea061584425d7040d29c78764604f00f05beeb141f38bf2609754598bf7546dc1c45ea061584429d702fd2dc78764604f00f05beeb141f38bf2609754598bf754645ea06151c1ddd7084422346b9d31ce0ac4219da4eea0e1500bf1c4604f8015b844226d29c1c84425d700ed2dc1c84429d700ad21c1ddd7084422346edd304e01c46d00704d17ae71c46d0073ff477af9142bff674af2078bcf1000f03d0beeb101f98bf70bd00f00f0040ea0e10207070bd5c1cd007ebd161e72de9f04f81b04ef66c644ff29c55ccf2ff04c2f229054ff6ff7600274ff0010a47f67f7b4ff07f0807e000bf48f2a351c0f24501bff32f8f8847d5f80090bff35f8f2868bff35f8f01466ff39601b1f1005f07d10168a14204d14168b1f57d7f00f094804ff47d70f8f76af9002800f005814ff60c41cff6ff7100bf42182e3182f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f8197482f81a7482f81b7482f81c7482f81d7482f81e7482f81f7482f8207482f82174a0d14ff47d710460416080f80ea0a0f8c4b180f8c381068446848684c684068546858685c685068646868686c686068746878687c68755e8001f49457ff45aafbff35f8f45e8000121b155e8001f4945f8d04fe7d0f89812002974d104464ff41630f6f739f8014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8981200e00021084601b0bde8f08f00bf70b5044690f8a00384f8a10350b3d4f8900347f64c6642f66535c2f20706c0f2440520b131682d31c4f8981310e040f22f00c0f200007844002122460023a847c4f89003316801f12d01c4f8981330b105f1e8022d219047002808bf70bd002084f8a10370bd00bf2de9f047a4b0d0f89442bff35f8fa4b142f691760546c0f2440620460021b04770b1d5f8900340b142f64d42c0f244022d2124b0bde8f047104724b0bde8f08747f64c69c2f20709d9f8007095f8a10300283fd0f7f7a4f8e0b3d5f89813781a17d495f8680110b995f82403e0b395f8a00385f8a10380b3d5f8900342f64d48c0f2440878b1d9f800102d31c5f898131de0d5f8900300b342f64d42c91bc0f24402904719e04ff64b70cff6ff70a8f1e807784400212a460023b847c5f89003d9f8001001f12d01c5f8981310b12d21c04710b1002085f8a10306f16601204624b0bde8f04708474af21f37c0f2470700200290adf80c00b84795f8680110b995f8240318b12846fff746ffe5e70df104084046f5f7a4fc02a9284600224ff0000afaf7ebfd81464046f5f7bffdb9f1000f02d085f8a1a303e00146284600f009f89df80c000028c7d107f14c008047c3e700bf2de9f04f90f8d13300f5697c4cf82310591c41f69a134b4390f8d0b31b0c03eb8303a1eb4301bbf1090f80f8d1139cbf0bf1010b80f8d0b3bbf1040f0bf0030e05d200244ff000094ff0000a2ee00bf0fc0800244ff000094ff0000a00eb8406d6f8a473d6f8a853b7fbfbf3d6f8ac1303eb090203fb1b73b5fbfbf7d6f8b06307fb1b5553443a44b1fbfbf72b44b6fbfbf53a4407fb1b1105eb020905fb1b6219440434a04502eb010ad7d1bef1000f1dd05cf82450bef1010fb5fbfbf606fb1b55b144aa4412d00ceb84056e68bef1020fb6fbfbf404fb1b66a144b24406d0ae68b6fbfbf505fb1b66a944b244bafbfbf202eb0901c0f8cc13bde8f08f00bff0b500294ff6ff7e00f0af80012903d10022c90775d1a8e021f00102c2f1000c6ff0010241f221038418a578e7788eea05252cb26e0036b203ead43484ea450503ead6366c0086ea450624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead53585ea460686ea07273eb27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead636023286ea470703ead5360ceb0205023586ea470e95d10232c90733d0805c41f221038eea002001b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead23181ea400e1ffa8ef0f0bd2de9f04fcbb00646c07c8b468df80600b08954998df80700000a8df808008df80a10080af1898df80b00080a17468df80d000020012b8df805308df809b08df80c108df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f008df820008df821008df822004cd1307e96f81780a0f1090144f62510c2f29240a1fb0002891a02eb510100970df10500b8eb910f88bf4fea9108c8ebc80706f11a0400f1010a00f10509093021463a46f0f781ff06f1210506f1280029460722f0f779ff284621460722f0f774ff20465146032207f10905f0f76dff06f11d0049460422f0f767fff07d02289cbf0130f075009fbb450cd0b8f1240f13d94ff0ff304bb0bde8f08fbb454ff00905f07509d14ef60b67abb20df10502c0f247070120f02115e002200df12304aab28df82300a01c0df105018df82470f0f73bffa81c4af6610782b2c0f24607f02021460023b8474bb0bde8f08f2de9f04184b0044690f84d0000284ff0000000f09a8094f84f1042f28b0701f078018df8071094f85010c0f25b0701f007018df80610cde9020002a906200022b847054670b9002002adcde902000df1070128460122f0f701ff012029460122b847054694f84e00a8b1002602a906200022cde90266b84760b9cde9026602ae04f1540130460122f0f7e8fe572031460122b8470543002602a906200022cde90266b84768b90df108080df1060140460122cde90266f0f7d1fe312041460122b847054302a906200022cde90266b84768b9002002ae04f15301cde9020030460122f0f7bbfe462031460122b8470543002602a906200022cde90266b84768b90df1080804f1510140460222cde90266f0f7a4fe362041460222b847054302a9a3200022cde90266b847054302a997200022cde90266b84742f62b310543c0f2440102208847284615b104b0bde8f081002184f84e1084f84d10a16504b0bde8f08100bf2de9f04f8bb00190233002254ff000080df10c0a4ff001094ff0000b002402900be000bf09fa0bf0002f0bf1010b08bf0443bbf1070f354426d042f2ff10c0f20000784410f80b0042f2f911c0f20001794411f80b6042f2fb0351463246c0f25b03cde90988cde90788cde90588cde90388984707460028d4d10298514628443246f0f739fecde742f2fb00c0f25b004ff60073002600f1340703a9c0f21f0381200c22cde90966cde90766cde90566cde90366b84730b9029803a928440c22f0f71afe8026ddf804c040f6b80146ea0400220ac2f200018cf823008cf824200a68002a00f0ad8045f2fd774ff6ff766ff0020241f22103c0f202070d681544ed7886ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4342d7984ea460686ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4346d7984ea460686ea052635b2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead4350332ba4285ea46067ff460afb1b2090a01e0ff26ff218cf83e109cf84c109cf84d208cf83f1026218cf83d608cf84020ff288cf84b101cbf04208cf81c000bb0bde8f08f70b588b042f2fb0514460e46c0f25b05002181280791cde90511cde90311cde90111009107d11b023435694681202246a84720b905e069462246a84708b108b070bd6946054630462246f0f71bfd284608b070bd2de9f0478cb08579972d17d00446fe2d0fd0fd2d2ad142f69570c0f25b008047002084f84e0084f84d00a0650cb0bde8f08720460cb0bde8f047cbe50020cde9040042f2fb00c0f25b00a0f1700304a9972000229847044642f62b31c0f244010220884720460cb0bde8f08742f2fb08002094f80c90677bc0f25b08cde90a00cde90800cde90600cde9040004a99f200422c047064620b901a804a90422f0f7c7fc002e6ff00100ddd19df80410bd29d9d19df805104029d5d19df806101029d1d194f84d00002852d047f64c60c2f20700006843f698210844a065002604a937200222cde90a66cde90866cde90666cde90466c047002840f0898002a804a90222f0f795fc04a947200122cde90a66cde90866cde90666cde90466c047002875d10df1030004a90122f0f781fc0020cde90a00cde90800cde90600cde9040004a905200122c047002860d10df1020004a9012249ea0726f0f76afc352d5bdc002d00f08180012d70d19df8020082e0002604a9052001228df80e60adf80c600296cde90a66cde90866cde90666cde90466c047d8bb0df1080a04a950460122f0f746fc04a935200122cde90a66cde90866cde90666cde90466c04738bb0af1010004a90122f0f733fc002604a937200222cde90a66cde90866cde90666cde90466c04798b90af1020004a90222f0f71ffc04a947200122cde90a66cde90866cde90666cde904660126c04778b14ff0ff300cb0bde8f087362d6ed0572d15d194f84e00002800f08580572039e00af1040004a90122f0f7fbfb04f14f0051460722f0f7f5fb84f84d603ee700208df801006946284601228df8009000f04ef9312d044627d036e09df80200002e00f0380618bf4036704010f0380f10d09df808009df8091041f6f6430e22c0f2000302ea96027b449a5a41ea0020904239d8012000218df80110694601228df8006000f024f904460df103014620012200f01df9054602a93620022200f017f945ea040141ea00040025a8f1700604a9a3200022cde90455b047044304a997200022cde90455b0470443aee69df802000e2101ea900041f67e41c0f200017944085a864204d96ff001000cb0bde8f0876946362002228df800708df8019000f0e6f80446d0e70020cde90a00cde90800cde90600cde9040004a9592001220125c04700287ff45caf04f1540004a90122f0f767fb84f84e5060e740f6b800c2f200000168002904bf4ff0ff30704780b584b000684ef2173102904ff41630039002a8c0f2470188474ff4f07042f29f7e01904ff4207cc0f25b0e0020002100220023cdf800c0f04704b080bd00bf2de9f04340f6b801c2f2000109684ff0000e01f1020c4cf6cd4340f2762808e00ef1010ebef5f07f0cf5a07c08bfbde8f083aef1a0096ff00906644607e000bf07eb0711a170053445452e46e8d290f84c1006f10a054f1e112905fb03f208bf570d122908d1a6f1e6019f294ff000079cbfb9f19f0f077b07eb071104f8021c90f84c1006f10c024f1e112903fb02f208bf570d122908d1a6f1e4019f294ff000079cbfb9f19f0f077b07eb071104f8011c90f84c1006f10e024f1e112903fb02f208bf570d122908d1a6f1e2019f294ff000079cbfb9f19f0f077b07eb0711217090f84c1006f110024f1e112903fb02f208bf570d122908d1a6f1e0019f294ff000079cbfb9f19f0f077b07eb0711617090f84c1006f112024f1e112903fb02f208bf570d12298ad1a6f1de019f294ff000079cbfb9f19f0f077b80e700bf2de9f04383b042f28b0914460e460546c0f25b090027694606200022cde90077c84710b103b0bde8f083e846404631462246cde90077f0f77dfa284641462246c84703b0bde8f08370b50446d0f840010d4638b92046f6f7cdf90028c4f8400108bf70bd42f64d46c0f2440606f140018847d4f8400129463246bde8704010472de9f04f81b0934612788ab1022300bf0beb030717f8012c62b11bf8032062b17a784ab1ba780433002af1d1023b04e0002205e0013b00e0013303eb4302520042eae272931c02f1010c01eb810203f0030e23f0030a00eb8213a3f5a0784ff0ff3906e009f10109b9f10d0f08f5a0786bd009eb010202eb8202bcf1030f00eb821605d24ff0ff34bef1000f2ed1e9e70024474604e000bfa24507f1020721d0621cd209042a03d83a7802f0f0023a70a21cd209042a03d87a7802f00f027a70e21cd209042a03d87a7802f0f0027a700434e209042adfd86208b55c05f00f05b554d9e7013cbef1000fbbd0a21cd509042d08d85208b55ce7076ff00f0708bf0f273d40b554bef1010fabd0e21cd509042d09d85208b55ce7074ff00f0708bf6ff00f073d40b554bef1020f9ad0221dd509042d96d85208e407b55c6ff00f0408bf0f242c40b4548ce79bf80010cdf800b0002900f0ce80022041f2410a41f2420b41f2430808e0009a063012f8011f03330029009200f0bd80a1f17f0717f15f0ff1d341f2f932c0f2000201eb41017a4402eb81014ff20014a1f5c079cff6ff7402e014f52074ded0494619f9027bc509042d09d8b7f1ff3f06dc1a1992f8016f46f0f00682f8016f5ffa87fe5fea4e6208d5042d06d81a1992f8017f47f00f0782f8017f5fea8e620ad540f27d22904206d81a1992f8027f47f0f00782f8027f5feace6209d5b0f51f7f06d81a1992f8027f47f00f0782f8027f5fea0e724fea900c09d5bcf19e0f06d81a1992f8036f46f0f00682f8036f5fea4e720ad540f27a22904206d81a1992f8037f47f00f0782f8037f91f90110042d09d8b1f1ff3f06dc1a1912f80a7047f0f00702f80a70c9b24a0608d5042d06d81a1912f80a7047f00f0702f80a708a060ad540f27d22904206d81a1912f80b7047f0f00702f80b70ca0609d5b0f51f7f06d81a1912f80b7047f00f0702f80b700a0709d5bcf19e0f06d81a1912f8087047f0f00702f8087049077ff565af40f27a2188423ff660af191911f8082042f00f0201f8082057e701b0bde8f08f00bf2de9f04381b04ff29c51c2f229010f68044638466ff39600b0f1005fbff35f8f0cd138684ef66c61ccf2ff01884205d17868b0f57d7f18bf002700e0002745f64d50c0f24600804741f2d161c0f2000141f2d662c0f2000279447a44022808bf0a4641f2b861c0f2000179440128204618bf114600f074fe47b197f87b0180b141f2b661c0f20001794419e041f2a361c0f200017944204601b0bde8f04300f05fbe97f8740141f29062c0f2000241f28d61c0f2000179447a44002808bf1146204600f04dfe97f8750141f26d62c0f2000241f27761c0f200017944012820467a4408bf114600f03bfe41f26561c0f200017944204600f033fe97f9c311204600f022ff41f25161c0f200017944204600f026fef6f7a2fd41f23368c0f2000841f23f66c0f200067e44054600f003003146f844012808bf414641f21d69c0f2000902282046f94408bf494600f008fea808012808bf4646022808bf4e462046314600f0fdfd41f2fb51c0f200017944204600f0f5fdd7f8840198b147f64c61c2f207010a68821a012a0bdb0968401a44f6d35100f2e730c1f26201a0fb0101890900e00021204600f0eafe41f2bd51c0f200017944204600f0d2fdd7f8bc11204600f0ddfe41f2aa51c0f200017944204600f0c5fdb7f8c011204600f0d0fe41f29251c0f200017944204600f0b8fd97f8c211204600f0c3fe41f27b51c0f200017944204600f0abfdb7f8c611204600f0b6fe41f26855c0f200057d442046294600f09dfdb7f8c811204600f0a8fe2046294600f094fdb7f8ca11204600f09ffe41f23c51c0f200017944204600f087fdd7f88011204600f092fe41f22751c0f200017944204600f07afd97f9c511204600f069fe41f21351c0f200017944204600f06dfd97f9c411204601b0bde8f04300f059be00bf2de9f04f81b0134681464a1c00f08f808e0a4ff0000b19f80b20002a00f0888109eb0b077a78002a00f00a81ba78002a00f00981fa78002a00f008813a79002a00f007817a79002a00f00681ba79002a00f00581fa79002a00f004813a7a002a00f003817a7a002a00f00281ba7a002a00f00181fa7a002a00f000813a7b002a00f0ff807a7b002a00f0fe80ba7b002a00f0fd80fa7b002a00f0fc803a7c002a00f0fb807a7c002a00f0fa80ba7c002a00f0fb80fa7c002a00f0fc803a7d002a00f0fd807a7d002a00f0fe80ba7d002a00f0ff80fa7d002a00f000813a7e002a00f001817a7e002a00f00281ba7e002a00f00381fa7e002a00f004813a7f002a00f005817a7f002a00f00681ba7f002a00f00781fa7f002a00f008810bf1200bbbf1600f7ff477af4ff0600b00e1002119f80120002a00f0af8109eb01056a78002a00f0b080aa78002a00f0b180ea78002a00f0b2802a79002a00f0b3806a79002a00f0b480aa79002a00f0b580ea79002a00f0b6802a7a002a00f0b7806a7a002a00f0b880aa7a002a00f0b980ea7a002a00f0ba802a7b002a00f0bb806a7b002a00f0bc80aa7b002a00f0bd80ea7b002a00f050812a7c002a00f04e816a7c002a00f04c81aa7c002a00f04a81ea7c002a00f048812a7d002a00f046816a7d002a00f04481aa7d002a00f04281ea7d002a00f040812a7e002a00f03e816a7e002a00f03c81aa7e002a00f03a81ea7e002a00f038812a7f002a00f036816a7f002a00f03481aa7f002a00f03281ea7f002a00f03081203160297ff479af32e10bf1010b74e00bf1020b71e00bf1030b6ee00bf1040b6be00bf1050b68e00bf1060b65e00bf1070b62e00bf1080b5fe00bf1090b5ce00bf10a0b59e00bf10b0b56e00bf10c0b53e00bf10d0b50e00bf10e0b4de00bf10f0b4ae00bf1100b47e00bf1110b44e00131f6e00bf1120b3fe00231f1e00bf1130b3ae00331ece00bf1140b35e00431e7e00bf1150b30e00531e2e00bf1160b2be00631dde00bf1170b26e00731d8e00bf1180b21e00831d3e00bf1190b1ce00931cee00bf11a0b17e00a31c9e00bf11b0b12e00b31c4e00bf11c0b0de00c31bfe00bf11d0b08e00d31bae00bf11e0b03e00e31b5e00bf11f0bb1f5205f4ff0010100931fd364234ff47a7442f2107700bf01eb81025200b6fbf2f50a2d11d301fb03f2b6fbf2f50a2d0bd301fb04f2b6fbf2f50a2d05d37943b6fbf1f2092ae7d800e011465846bbf15f0f98bf5f20c0f1000c1ceb0b0a53d0b6fbf1f74cf6cd4207f13003ccf6cc4209f80b300a29a1fb023547d31af1010342d007fb11674fead508b7fbf8fe48f21f520ef1300309eb0b06c5f2eb1273706429a1fb023434d31af102032cd00efb18776409b7fbf4f544f6d35205f13003c1f26202b370b1f57a7fa1fb022323d31af1030218d005fb14729b0941f25975b2fbf3f7cdf2b715a1fb055407f13005f57007fb132642f210720bf1040b91424fea5431a9d209e0834607e00bf1010b04e00bf1020b01e00bf1030b0020009b09f80b002de00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f020a7003d1002189f85f1001e000204870002119f80100002800f0a18009eb0105687800285ed0a87800285dd0e87800285cd0287900285bd0687900285ad0a879002859d0e879002858d0287a002857d0687a002856d0a87a002855d0e87a002854d0287b002853d0687b002852d0a87b002851d0e87b002850d0287c00284fd0687c00284ed0a87c00284dd0e87c00284cd0287d00284bd0687d00284ad0a87d002849d0e87d002848d0287e002847d0687e002846d0a87e002845d0e87e002844d0287f002843d0687f002842d0a87f002841d0e87f002840d0203160297ff498af44e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2905d849444ff02f00087040f09580002089f85f00581c00f094809d0a4ff0000e19f80e00002800f08d8109eb0e067078002800f00f81b078002800f00e81f078002800f00d813079002800f00c817079002800f00b81b079002800f00a81f079002800f00981307a002800f00881707a002800f00781b07a002800f00681f07a002800f00581307b002800f00481707b002800f00381b07b002800f00281f07b002800f00181307c002800f00081707c002800f0ff80b07c002800f00081f07c002800f00181307d002800f00281707d002800f00381b07d002800f00481f07d002800f00581307e002800f00681707e002800f00781b07e002800f00881f07e002800f00981307f002800f00a81707f002800f00b81b07f002800f00c81f07f002800f00d810ef1200ebef1600f7ff477af4ff0600e05e100204870581c7ff46caf002119f80100002800f0aa8109eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04b81107c002800f04981507c002800f04781907c002800f04581d07c002800f04381107d002800f04181507d002800f03f81907d002800f03d81d07d002800f03b81107e002800f03981507e002800f03781907e002800f03581d07e002800f03381107f002800f03181507f002800f02f81907f002800f02d81d07f002800f02b81203160297ff479af2de10ef1010e74e00ef1020e71e00ef1030e6ee00ef1040e6be00ef1050e68e00ef1060e65e00ef1070e62e00ef1080e5fe00ef1090e5ce00ef10a0e59e00ef10b0e56e00ef10c0e53e00ef10d0e50e00ef10e0e4de00ef10f0e4ae00ef1100e47e00ef1110e44e00131f1e00ef1120e3fe00231ece00ef1130e3ae00331e7e00ef1140e35e00431e2e00ef1150e30e00531dde00ef1160e2be00631d8e00ef1170e26e00731d3e00ef1180e21e00831cee00ef1190e1ce00931c9e00ef11a0e17e00a31c4e00ef11b0e12e00b31bfe00ef11c0e0de00c31bae00ef11d0e08e00d31b5e00ef11e0e03e00e31b0e00ef11f0e0122b3f5205f1ed364234ff47a7442f2107602eb82004100b5fbf1f00a2811d302fb03f1b5fbf1f00a280bd302fb04f1b5fbf1f00a2805d37243b5fbf2f00928e7d800e00a46f046bef15f0f98bf4ff05f08c8f1000c42f2107a1ceb0e074dd0b5fbf2f30a2a03f1300009f80e004cf6cd40ccf6cc40a2fb000641d3781c3dd003fb1253f608b3fbf6f409eb0e0504f13000687048f21f50c5f2eb10642aa2fb000130d3b81c29d004fb16334e09b3fbf6f4b2f57a7f04f13000a87044f6d350c1f26200a2fb000120d3f81c16d004fb1630890941f25974b0fbf1f3cdf2b714a2fb044603f13004ec7003fb11050ef1040e52454fea5632afd209e0c64607e00ef1010e04e00ef1020e01e00ef1030e002009f80e0001b0bde8f08f0f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f00087005d1002089f85f0001b0bde8f08f0020487001b0bde8f08f2de9f0411f22831813f81f7c002f00f0808013f81e7c002f7dd013f81d7c002f7bd013f81c7c002f79d013f81b7c002f77d013f81a7c002f75d013f8197c002f73d013f8187c002f71d013f8177c002f6fd013f8167c002f6dd013f8157c002f6bd013f8147c002f69d013f8137c002f67d013f8127c002f65d013f8117c002f63d013f8107c002f61d013f80f7c002f5fd013f80e7c002f5dd013f80d7c002f5bd013f80c7c002f59d013f80b7c002f57d013f80a7c002f55d013f8097c002f53d013f8087c002f51d013f8077c002f4fd013f8067c002f4dd013f8057c002f4bd013f8047c002f49d013f8037c002f47d013f8027c002f45d013f8013c002b43d0835c002b41d020327f2a7ff47baf64e01f3a3ae01e3a38e01d3a36e01c3a34e01b3a32e01a3a30e0193a2ee0183a2ce0173a2ae0163a28e0153a26e0143a24e0133a22e0123a20e0113a1ee0103a1ce00f3a1ae00e3a18e00d3a16e00c3a14e00b3a12e00a3a10e0093a0ee0083a0ce0073a0ae0063a08e0053a06e0043a04e0033a02e0023a00e0013a5f2a25d8c2f15d0cc2f15e0ec2f15f03c2f160080244002509e000bffc78002cf47008bfbde8f0810435a84510d04e5d565586b1ab420bd04f197c78561974704cb1ae4504d0bc78b47024b1ac45e5d1002180f85f10bde8f08100bf7f2905d140f61301c0f20001794403e7b1f1ff3fc8bf0fe0b0b540f2fd72c0f200027a440d4611460446fff7f5fe20466942bde8b040ffe72de9f04f4ff0000c10f80c20002a00f0c48000eb0c035a78002a62d09a78002a62d0da78002a62d01a79002a62d05a79002a62d09a79002a62d0da79002a62d01a7a002a62d05a7a002a62d09a7a002a62d0da7a002a62d01a7b002a62d05a7b002a62d09a7b002a62d0da7b002a62d01a7c002a62d05a7c002a62d09a7c002a62d0da7c002a62d01a7d002a62d05a7d002a62d09a7d002a62d0da7d002a62d01a7e002a62d05a7e002a62d09a7e002a62d0da7e002a62d01a7f002a62d05a7f002a62d09a7f002a62d0da7f002a62d00cf1200cbcf1600f7ff496af4ff0600c5be00cf1010c58e00cf1020c55e00cf1030c52e00cf1040c4fe00cf1050c4ce00cf1060c49e00cf1070c46e00cf1080c43e00cf1090c40e00cf10a0c3de00cf10b0c3ae00cf10c0c37e00cf10d0c34e00cf10e0c31e00cf10f0c2ee00cf1100c2be00cf1110c28e00cf1120c25e00cf1130c22e00cf1140c1fe00cf1150c1ce00cf1160c19e00cf1170c16e00cf1180c13e00cf1190c10e00cf11a0c0de00cf11b0c0ae00cf11c0c07e00cf11d0c04e00cf11e0c01e00cf11f0c01230a291fd364274ff47a7642f2107500bf03eb83025200b1fbf2f40a2c11d303fb07f2b1fbf2f40a2c0bd303fb06f2b1fbf2f40a2c05d36b43b1fbf3f2092ae7d800e01346e2464cf6cd49bcf15f0f98bf4ff05f0acaf1000eccf6cc491eeb0c0b4ed0b1fbf3f50a2b05f1300200f80c20a3fb092446d31bf1010241d005fb1316e508b6fbf5f800eb0c0108f130024a7048f21f52c5f2eb12642ba3fb022434d31bf102022cd008fb15656609b5fbf6f4b3f57a7f04f130028a7044f6d352c1f26202a3fb027223d31bf1030718d004fb1654920941f25976b4fbf2f5cdf2b716a3fb066705f13006ce7005fb124142f210720cf1040c93424fea5733aed209e0d44607e00cf1010c04e00cf1020c01e00cf1030c002100f80c10bde8f08f0000434d01ffff0000000000000052420100ff000000080310000000000000000000080010631a2308011a1c0800100018c00420a0024801520964617368626f617264580162012028904e46616365636c61772f33380000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a89000000000000000009f0535593747c181040101010201020c005353a829c51bd314a810260d5b0bfe096d6963205220006d6963204c20006d6963203f20006e6f206374780061726d200063666720006f6666200070646d00636f646563002072656720002074626c2000540053002d00206c65617365200073207461702000200042400020736b697020002f002066722000206e7466200020756e7265672000000000", + "new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc7b04ff29c5bc2f2290b4ff6ff784ff47e740027012647f67f754ff07f0a1191cde90f3007e000bf48f2a351c0f24501bff32f8f8847dbf80090bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0ad101684ef66d62ccf2ff02914203d14068b0f57e7f4ed04ff47e7002f0f9f9002800f027816ff0070142180831b1f57c7f177257729772d772177357739773d773f2d14ef66d61ccf2ff01016044608673a0f8c85180f8c7a1a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e805be8001f494599d1bff35f8f4be8000121b15be8001f4945f8d08fe74ff47e7a00254ff0010947f67f777f2407e000bf48f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0ad101684ef66d62ccf2ff02914203d14168b1f57e7f50d04ff47e7002f07ff9002800f0ad806ff0070142180831b1f57c7f157255729572d572157355739573d573f2d14ef66d61ccf2ff010160c0f804a080f80e90a0f8c87180f8c741a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e805be8001fb14297d1bff35f8f4be8000121b15be8001fb142f8d08de7d0f8984242f6f76ac0f2440abff35f8fdcb30af19a0220464ff0ff314ff0ff379047002840f02e81119800284ed010980778a7f11700022848d811980c2821d1109d6878a97808431cd0e8780138022818d8aa79eb796e7a42ea03222b7a287942ea034242ea0663aa7aee7a6979192f42ea062200f07f81172f40f09581002a00f04e834ff0ff37f8e004460020d047c8b104f5267151e8004f54b9bff35f8f41e80002002a00f0998051e8004f002cf6d00021bff32f8f0029bff35f8f00f089800446a5e74ff0ff37dbe047f6df40c0f24f000e94804700274ff47e79012647f67f744ff07f0a13978df8507006e048f2a351c0f24501bff32f8f8847dbf80050bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0ad101684ef66d62ccf2ff02914203d14168b1f57e7f57d04ff47e7002f095f8002850d06ff0070100bf42180831b1f57c7f177257729772d772177357739773d773f2d14ef66d61ccf2ff010160c0f804908673a0f8c84180f8c7a1a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e805be8001fa94298d1bff35f8f00bf4be8000159b15be8001fa942f8d08de70af5a97188471ae701216be70020109f42f6f76ac0f2440a9fb1119989b1397801f07f02b2f10b0418bf0124b2f11c0518bf01258a291edc0b2920d01c291ed03ae00f9012a801f06dfe4ff0ff37012612a801f037ff054646b99df8500028b94af21f30c0f247004c3080470f98002818bf056117e09c2901d08b291cd180b14af21f31c0f247010646884796f86811bff35f8f29b996f824133046bff35f8f51b14ff0ff370e9c0af5807120468847384647b0bde8f08f0f9012a804ea050601f030fe387800f07f041e2c05d11199384606f01ffe0746bae71d2c00f07e0018bf1a2819d1dbf800100322cff680720a40b2f1005fbff35f8f12d10a684ef66d63ccf2ff039a420bd14a68b2f57e7f04bf002281f8a12303e0a4f115010129e1d90f2c2cdd142c40f3ad81152c00f0c882162c00f0d2821f2c40f095831198012840f0648438781f2840f0608445f64d54c0f24604a047012841f05e8041f2de01c0f2010104f5e2777944e02029220023b8470746002818bf4ff0ff3767e7092c00f3b880052c00f0ee81072c40f06b830d9600264ff0010947f67f777f243ae000294ff0ff3708bf002b7ff47caf1328bff479af102a4ff0000138bf01211228b2fa82f218bf510901297ff46caf00f087bc182f4ff0ff3708bf00297ff463af002b7ff460af352800f37083002800f05f84012800f06b84312840f05d84082abff451af00f06cbc48f2a351c0f24501bff32f8f8847dbf80050bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66d62ccf2ff02914204d14168b1f57e7f00f077854ff47e7001f01dff002800f06f856ff0070142180831b1f57c7f167256729672d672167356739673d673f2d14ef66d61ccf2ff0101604ff47e71416080f80e90a0f8c87180f8c741a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e805be8001fa94295d1bff35f8f4be80001002900f028855be8001fa942f6d089e70a2c00f0bb810b2c40f0b382dbf800500320cff680702840b0f1005fbff35f8f40f0808728684ef66d61ccf2ff01884240f0788768680027b0f57e7f7ff48daed5f8900342f68d49c0f244090028a5f8a07318bfc8470021a5f8d013c5f8cc13c5f8701185f86c11bff35f8f85f86811686c85f86b11c5f87411c5f82013c5f88813a5f88c13a5f8481040b1c847686c09f166018847002804bf002068644bf63940c0f251008047dbf800400320cff680702040b0f1005fbff35f8f46d120684ef66d61ccf2ff0188423fd16068b0f57e7f3bd194f87f014ff00008c4f8888100b384f87f8194f8790149f259620138b0fa80f04109c0f2590240f20b1090474ef6e37710f1800fd8bf6ff07f00c0f25a077e28a8bf7e2084f8c801b84707f1bc008047d4f88c0142f6f76ac0f2440a84f8788150b1c847d4f88c0109f166018847002804bf0020c4f88c01dbf800000321cff680710140b1f1005fbff35f8f40f07a8601684ef66d62ccf2ff02914240f072864168b1f57e7f40f06d8690f8d011002900f0628605f007fc00f064be102c00f02281112c40f0eb811198022840f0ba823878112840f0b6827878002840f0b28245f64d50c0f24600804701284ff000077ff4c9ad40f20830c0f210003790ff208df8d80044f25220c0f20100359040f2c960c0f248003446cde93877804798b148f27e30c2f2070000880122c1b264298df8d72008d80721b0f5807f38bf03218df8d7108df8d80040f2d260adf8e000052037ae8df8e200f01d35a90522fff768fa4ff22507c0f247070120092132460c23b84742f6f76a0746c0f2440a264681e5fe2800f06c82812800f05f82002840f0728251ea03007ff4a5acbce211980d964ff47e7a00254ff0010947f67f747f2602282cbf7878ff200c9006e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66d62ccf2ff02914204d14168b1f57e7f00f058814ff47e7001f05efd002800f0ff836ff0070142180831b1f57c7f157255729572d572157355739573d573f2d14ef66d61ccf2ff010160c0f804a080f80e90a0f8c84180f8c761a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e805be8001fb94296d1bff35f8f4be80001002900f00a815be8001fb942f6d08ae711980228c0f0ca8100f0dffd002800f0c5817c78002c00f04483022c00f2be8140f02885014611980628c0f0a584109fb878fa783b797f7940ea022043ea072232283790389280f00d85322000f010bd1199781c013906f07afb0746bfe41199384605f0eefb0746b9e4119801380228c0f094817878b9784ff0ff3740ea0120b0f5007f3ff6abac0321119a01eb400191427ff4a4ac002700288df8dc708df8dd708df8de708df8df708df8e0708df8e1708df8e2708df8e3708df8e4708df8e5708df8e6708df8e7708df8e8708df8e9708df8ea708df8eb708df8ec708df8ed708df8ee708df8ef708df8f0708df8f1708df8f2708df8f3708df8f4708df8f5708df8f6708df8f7708df8f8708df8f9708df8fa708df8fb708df8fc708df8fd708df8fe708df8ff708df800718df801718df802718df803718df804718df805718df806718df807718df808718df809718df80a718df80b718df80c718df80d718df80e718df80f718df810718df811718df812718df813718df814718df815718df816718df817718df818718df819718df81a718df81b713ff420ac10990df1dc0a033101224b78012b00f2e4830f7847ea0323db081af8035007f0070702fa07f72f4240f0d7831af80350013847ea050701f102010af80370e4d10d9602f00dfd002800f0e0830025012647f67f797f27c8e21d2c5ed11199781c013906f0ddfe0746fff7eabb90f8481042f6f76a109fdde90c56c0f2440a61b1416c39b142f68d42c0f244020446084690472046002180f848100121119a032a1ad3cdb9b878082800f2af844bf63941c0f25101963188470027fff7c1bb362800f00781462800f00181572840f0ed80002e7ff4e1abfce0119a052ac0f0d680012d40f0d380b8783d46411e06294ff000073ff6a5abe97803293ff6a1ab2a79002a3ff49dab4bf63943c0f2510303f59a7398470027fff793bb1a2818bf1c2c6dd102f09dfc002869d000f07ffc814607f014f9b9f1000f4ff0ff3718bf00283ff47eab834647f63400c2f20700d0f81806d9f894131b2ca0eb0100c9f89c0300f01a831a2c40f03b83109900254f1c11994ff4f072a1f1010a4ff4a071cde93d5037a84ff420744ff48058cde90950cde901214846394652465b46cde937853f95cde90755cde90555cde90355009407f024fb002840f01083d9f89c033946cde93e0537a80a90012009904ff4a07002904ff4f0700190484652465b464ff42077cde937853d95cde90755cde90555cde90355009707f001fb074693e34ff0ff37fff71dbb0c294ff0ff377ff444abb3f5005f5dd3fff73fbb481e1f284ff0ff373ff639abc1f51630834251d9fff733bb002b7ff434aa421f18bf0122b0f1350318bf01231a40b0f1590318bf01231a40b0f1470318bf0123134208bf012937d0b0f1370218bf0122b0f1c10318bf01231a4208bf02292bd09f284ff0ff377ff40aab042924d0fff706bb022d40f0ff804bf63940c0f2510080470027fff7cfba022abff4f8aa13e0002a7ff4f4aaa0f1fd0102290cd397287ff4edaa08e06ff0780002427ff4e7aa02e0402abff4e3aa45f64d50c0f24600804710990128c9784ff001004ff0010608bf022008424ff000073ff4d0aa0e9447f67f797f2406e048f2a351c0f24501bff32f8f8847dbf80050bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0ad101684ef66d62ccf2ff02914203d14168b1f57e7f50d04ff47e7001f09afa00283ff4a0aa6ff0070142180831b1f57c7f177257729772d772177357739773d773f2d14ef66d61ccf2ff0101604ff47e7141608673a0f8c89180f8c741a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e805be8001fa94297d1bff35f8f4be8000121b15be8001fa942f8d08de74af21f35c0f247050446a84794f86801bff35f8f109f00287ff448aa94f8240321460028bff35f8f7ff440aa3878172812d0d1f8840320b902f06bf8c4f8840360b342f68d41c0f24401a1f140024ff47a719047214608bb01f226300e4639460c22fef7d0fe0f98002186f825030120bff35f8f86f824034ff4f07001904ff4207005f5106600900020002200230027b04700283ff40caa84f8247305f14c0080474ff0ff37fff703ba119a072ac0f01681032d40f01381b878fa78397940ea02207c79bd7901284ff0010798bf384644f62062904228bf10464bf63942c0f2510202f5e272642928bf6421904747f63400c2f207000068002800f0938244ea0521012988bf0f4642f68d41c0f24401a1f14002394690470027fff79eb9002780f8477145f64d50c0f24600804701287ff493a943f2a310c0f2560080470746fff78bb9002011990d9e022910992cbf4978ff21002800f0658202294ff000073cd0012936d000297ff477a94cf67e21caf2101187778783cbf8041087818760a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e8080f8407080f8d17380f8d073c0f8cc73fff743b901218173fff73fb98773fff73cb942f6f76a109fdde90c5600210020c0f2440a5fe548f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f0bd101684ef66d62ccf2ff02914204d14168b1f57e7f00f099804ff47e7001f02cf9002800f0ef806ff0070142180831b1f57c7f157255729572d572157355739573d573f2d14ef66d61ccf2ff0101604ff47e7141608673a0f8c89180f8c771a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a80a0f83c80a0f83e805be8001fa14296d1bff35f8f4be8000100294bd05be8001fa142f7d08be780461198b14603284ff0000738d3281fb0fa80f04009084032d01198109a4cf6cd410338ccf6cc419578a0fb0101b5eb910f88bf8d082e46302d28bf302608f14a00d11c06eb8602fef76efd002788f8486088f849709db1d8f8440038b9404600f046fa0028c8f8440000f0748142f68d41c0f24401a1f1400201219047002742f6f76ac0f2440a4e46fff777b8d0f874110027002900f0ad804ff400624ff0ff3c04e02b681a44b2f5a12f4fd28d18ac68b7eb542ff5d1e3081af8033004f00706f340db071ebf41f82470c5f808c0ef60e7e71198032840f0c38010994ff4f077487889785a4640ea012400200890cde90600cde90400cde902004ff4a0700190484621464ff42073009706f06cfe002800f01b814ff0ff37a2e01198012840f09f8009f57970bff35f8f50e8001f013140e80012002af8d113a94846012206f08cfec7178ce04ff0ff3752e04ff0000c4ff400634ff0000ec0f820c305e077463344b3f5a12fbe4642d2cf18ba68ce580132f4d1f418e20aa02a2fd80a19956801352bd115682e443e6012681444e20aa02a23d80a19956801351fd115682e443e6012681444e20aa02a17d80a199568013513d115682e443e6012681444e20aa02a0bd80a199568013507d115682e443e6012681444b4f5a12fcfd3bef1000fc7f804c00cbfc0f82033cef80430b7e7002742f6f76a0d9ec0f2440afef7bdbfb0f5fa6f084603d94ff4fa60379008460121054680f8471145f64d50c0f24600804701281bd143f2a318c0f25608a8f188008047022c074608bf002f01d057b90de04bf2110237a9c0f24b020220904728b10746c047002085f8470100e0002742f6f76ac0f2440afef787bf90f8d111002918bf04f0a1fddbf800000321cff680710140b1f1005fbff35f8f24d101684ef66d62ccf2ff0291421dd14168b1f57e7f19d1d0f8d813b1b14ff00041bff35f8fc0f8d4134af21f30c0f2470000f510674ff4f07001904ff4207400200021002200230094b84795f84701002785f8477158b145f64d50c0f246008047012804d143f2a310c0f256008047d5f8400195f84641c5f83c7185f84671a5f8447150b1c847d5f8400109f166018847002804bf0020c5f840010120a87354b14af27365c0f246050021002200230027a847fef71bbf0027fef718bf601ca9f88c03002785e74bf639400027c0f2510088f8487080478be62de9f04f81b04ef66d644ff29c55ccf2ff04c2f229054ff6ff7600274ff0010947f67f7a4ff07f0b07e000bf48f2a351c0f24501bff32f8f8847d5f80080bff35f8f2868bff35f8f01466ff39601b1f1005f06d10168a14203d14168b1f57e7f3dd04ff47e7000f0f5feb8b36ff0070142180831b1f57c7f177257729772d772177357739773d773f2d14ff47e710460416080f80e90a0f8c8a180f8c7b1068446848684c684068546858685c685068646868686c686068746878687c68755e8001f4145b2d1bff35f8f00bf45e8000129b155e8001f4145f8d0a7e7002001b0bde8f08f70b500284dd001684ef66d62ccf2ff02914218bf70bd90f8481090f849504bf6394c8d42c0f2510c2ad205eb8501014491f84d2091f84e4091f84ae091f84b3091f84c1042ea0424012c4ff0010298bf14466e1c054680f84960b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84810bde870406047e047686c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04fcdb04ff29c5ac2f2290adaf8004040f2030920464ef66d686ff39600cff68079ccf2ff08b0f1005fbff35f8f40f0e6812068404540f0e2816068b0f57e7f40f0dd8194f8710347f64c67c2f2070704f5497518b347f61800c2f20700016d4bf67c52c0f27202914218d1406cb0b1d4f87c033968401a012802db01f007ff68b928460ef067ff48b142f69570c0f25b008047002084f8720384f8710394f8700368b1d4f880033968401a012802db01f0edfe20b9002084f8700384f86c012878bff35f8f68b194f86f830095b8f1000f1cd094f8250394f83e13091a18bf012115e094f87103002840f0888194f87003002840f08381d4f88403002800f07e8142f68d41c0f24401884777e1012194f8270394f83133059094f8280394f83d23069094f8265394f82a0394f82b9394f82ce394f82db394f82ea394f82f63934235d194f8302394f83c339a422fd194f83b2396422bd194f83a23924527d194f83923934523d194f8382396451fd194f8372391451bd194f83623904217d194f8292394f835339a4211d194f83423069b93420cd194f83323059b934207d194f83223954203d111b994f825a3a9e00023012284f8403384f8433384f8443384f8453384f8463384f8473384f8483384f8493384f84a3384f84b3384f84c3384f84d3384f84e3384f84f3384f8503384f8513384f8523384f8533384f8543384f8553384f8563384f8573384f8583384f8593384f85a3384f85b3384f85c3384f85d3384f85e3384f85f3384f8603384f8613384f8623384f8633384f8643384f8653384f8663384f8673384f8683384f8693384f86a3384f86b3384f86c3384f86d3384f86e3384f83f2384f8415384f842033a68bc46170a84f8432384f84473170c120e47f6180384f846230822c2f2070384f8457384f86f231a6d4bf67c57c0f2720704f22631ba4204f232381bd15a6ccab140f6b803c2f200031a689ab1182d00f0ed82172d40f0f882fe2804f2473c0d4600f01183002840f01b8300980ef0eafe294602e0022084f8400340460c22fef791f994f825a394f86f8384f83ea345f64d50c0f246008047811eb1fa81f14fea511b012808bf4ff0020bb8f1000f5ad004f23f3007a9049040f261400631c0f2470003910aa9023100254ff00309a0f5b84000240291019015e000bf029807a91a468df828908df829a0fef75bf947f00802019ff0200aa90023b8470334e0b24045b1462fd2a8eb0400c7b20420032f28bf4f468df81c0005983a468df81d0006988df81fb08df81e0004988df8204050fa84f103988df821808df822508df823508df82450fef72df9b81dd345c3b24ff00306c2d14ef60b670120f02107aac0f24707c9e7bff35f8f00994ef66d6840f203094ff29c5a0020ccf2ff08cff68079c2f2290a0870daf80050bff35f8f05ea0900b0f1005f59d12868404556d16868b0f57e7f52d105f575700021bff35f8f00bf50e8006f40e80012002af9d147f64c60bff35f8fc2f207000468b6f1004f0ed001f020fd58b1002e00f0a58095f8ec03320a002800f0a48095f8ed13ade095f8ec03e8b147f61800c2f207000068b8b145f60960c2f207000478002040f261430a9007900aa907aac0f247032046984707990a9a43f69b53c0f24e0320469847002085f8ee0385f8ec03d5f8d80320b142f68d41c0f2440188470120daf80050bff35f8f05ea0901b1f1005f1ed1296841451bd16968b1f57e7f17d110b195f87003f0b195f86801bff35f8f002800f0d0810020bff35f8f85f868010220bff35f8f85f869014db0bde8f08f002800f0c08140f6a120c0f2470080474db0bde8f08f95f86801bff35f8f00b307a800f003fad5f86441bff35f8f0120bff35f8f85f8690164b142f68d40c0f2440000f5417220464ff47a719047002800f07181002085f86a01bff35f8f85f8680195f86c010028cdd0d5f8700140b147f64c61c2f207010968401a002800f38281002085f86c01bde795f8ec03002844d18ce70120a5f8ec0340f20220a5f8f0030220002185f8ef03c5f8e01302f00103c6f300208b42c6f34b2206d123b395f8f233f7b2bb421fd00ce05bb995f8ef13a5f8ea2385f8f013022185f8f113c5f8dc4311e095f8ef3385f8f03385f8f163a5f8ea23c5f8dc4331b9012185f8ee13c6f34951a5f8e813002804bf002185f8ee1385f8ed0385f8f26395f8ee03c8b195f86801bff35f8f48b1d5f8e403bff35f8fb5f8e8136ff39f20884205d195f8ee1339b195f8ef632de0002085f8ee03c5f8dc43d5f8dc13b5f8ea0395f8f163611a81421ed28902b1fbf0f04ff4406395f8f01300fb00f2a3eb40005043000d721a00fb02f3002b4ff4007344bf4ff60063cff6ff7310fb0230c21700eb925001eb9026002147f61800c2f207000068e0b1d5f8e003f4b2a0421ad0002040f261430a9007900aa907aac0f247032046984707990a9a43f69b53c0f24e0320469847c5f8e04395f8ee1302e00020c5f8e003d5f8d80385f8ef6311b14ff47a7108e095f8f113f2b28a424ff0280108bf4ff47a7142f68d42c0f24402403a904795f8ee0308b10020c1e695f8ed0300287ff4bcaef0b20228f5d140f6b801c2f20001086800284ff000003ff4b0ae0a684ff41633213a02f8400f403b02f81f0c02f81e0c02f81d0c02f81c0c02f81b0c02f81a0c02f8190c02f8180c02f8170c02f8160c02f8150c02f8140c02f8130c02f8120c02f8110c02f8100c02f80f0c02f80e0c02f80d0c02f80c0c02f80b0c02f80a0c02f8090c02f8080c02f8070c02f8060c02f8050c02f8040c02f8030c02f8020c02f8010c50709070d070107150719071d071107250729072d072107350739073d073107450749074d074107550759075d075107650769076d076107750779077d07782f820009cd108684ef217310a904ff416300b900aa8c0f24701884778e7002095f86a6185f86a0142f6f771bff35f8f85f86801c0f2440120468847002e3ff484ae40f6b800c2f2000000684ef217310a904ff416300b900aa8c0f247018847012085f86c0107a800f024f968614db0bde8f08f00980d460ef06cfdd8b102304ff0030008bf022084f84003294618e584f87003dcf800204ef660231a44c4f8802380b100980d460ef032ff0ef006ff294630b905e50ef001ff002829463ff400ad0320fbe4002084f86c01f9e418684a4600eb0b20704400eb0a4000eb06616046fdf783fe0ee04eea0b2141ea0a4141ea066361464a460ef002fd00281cbf032084f8400309f1080084f86f032946d7e400bf2de9f04f81b0009041f204024cf65560cef20002ccf2ac50c2f8ac0f4ef6fc50cef2000001684ef66d6641f08071016052f8040c4ff29c57ccf2ff06c2f229074ff6ff7440f0010000254ff0010b47f67f784ff07f0942f8040c06e048f2a351c0f24501bff32f8f8847d7f800a0bff35f8f3868bff35f8f01466ff39601b1f1005f06d10168b14203d14168b1f57e7f3ed04ff47e7000f069f9002875d06ff0070100bf42180831b1f57c7f157255729572d572157355739573d573f2d14ff47e710660416080f80eb0a0f8c88180f8c791048444848484c484048544858485c485048644868486c486048744878487c48757e8001f5145b0d1bff35f8f00bf47e8000121b157e8001f5145f8d0a5e78169d1bb47f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f421ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94206d041f20401cef200010968c91a816141f20400cef2000000680099086001b0bde8f08f00bf2de9f04f83b041f20401cef200014ef66d674ff29c5609680068ccf2ff07c2f229064ff6ff7400254ff0010847f67f794ff07f0a0291019007e000bf48f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f06d10168b94203d14168b1f57e7f3ed04ff47e7000f0a9f8002800f093806ff0070142180831b1f57c7f157255729572d572157355739573d573f2d14ff47e710760416080f80e80a0f8c89180f8c7a1048444848484c484048544858485c485048644868486c486048744878487c48756e8001f5945b0d1bff35f8f00bf46e8000121b156e8001f5945f8d0a5e7816961b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce047f64c6245f6e466c2f20702cff6f8761368314600bf17689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94216d041f20401cef20001096844f6d357ca1ac1f26207a2fb07768261b00901284ff0010088bfb009994208bffa2000e0fa20dde90121891ab1fbf0f003b0bde8f08f2ab1a2fb0103002b1cbf0020704702fb01f0ffe7b0b54ff2a054c2f22904216846f23f52c5f28802b2eb510f48f25f514cf67e25c0f24501caf2101518bf25608847002804bf691c2160b0bd084648f2a351c0f245010847002855d1002953d0082a51d30b78aa2b4ed18b79f02b4bd12de9f04182b045f64d550b2ac0f2460547d3c878a2f10803834242d14878212841d1087901283ed1487901283bd1c879c8bb0c4614f8080f10f03c0f33d18818884610f8021c10f8010c164641ea0027a2f10a0081b220460df07eff874222d1a847811eb1fa81f14f09012808bf0227002f4cd0207887f00301084249d0012039464246334600f02bf90446002898f8080018bf06243de04ff27143c0f24c0318470b2400e00a24a847811eb1fa81f14e09012808bf02262eb3304600f094f808b30546407cf0b148f2a35800272846314632460323c0f2450800970df0f4ff686838b14bf69b71c0f25d0188476868c0476f602868af75002818bfc04700202860a8602882204602b0bde8f0810624f9e700243842f6d0404631463a463b4600f000f9002818bf0446ece70b291dd3c278a1f10803934218d10278aa2a13d14278212a10d10279012a0dd14279012a0ad18279f02a07d1c2792ab9024612f8083f13f03c0f03d00a2070470b20704710b5084410f8023c10f8010c43ea0024a1f10a0081b210460df0e6fe201a18bf0a2010bd002808bf704770b582b00446407ce0b148f2a355002620460323c0f2450500960df088ff606838b14bf69b71c0f25d0188476068a84766602068a675002818bfa84700202060a060208202b0bde87040704700bf2de9f04f81b04ef66d654ff29c56ccf2ff05c2f229064ff6ff7700244ff0010a47f67f7b4ff07f08009006e048f2a351c0f24501bff32f8f8847d6f80090bff35f8f3068bff35f8f01466ff39601b1f1005f06d10168a94203d14168b1f57e7f3dd04ff47e70fff7a9feb8b36ff0070142180831b1f57c7f147254729472d472147354739473d473f2d14ff47e710560416080f80ea0a0f8c8b180f8c781078447848784c784078547858785c785078647868786c786078747878787c78756e8001f4945b2d1bff35f8f00bf46e8000129b156e8001f4945f8d0a7e700200099002801eb410100eb011118bf01f51b7001b0bde8f08f00bfb0b5c4b0dc08202c03d94ff0ff3044b0b0bd0df107048df807008df80810a01c11461a461d46fdf703fba81c4af6610582b2c0f24605f02021460023a84744b0b0bd00bf2de9f04f8bb0054618461f4616468946fff758ff002800f0aa83044695f9080048f2a35ac0f2450ab0f1ff3f00f0030b089508dd254615f8110fe0bb2068002818bfd04738e3207c00b3607cf0b131464ff0000820463a460323cdf800800df0a1fe606840b14bf69b71c0f25d0188476068d047c4f80480206884f81680002818bfd04700202060a06020822068002818bfd0474ff480710020218208992060a0608978a081092004f11105e174a17484f814b060760899a07c8978884240f0de82217d594540f0da82411ca9f10b00627e02951e2828bf1e209042a17488bf6076a9f10200cde9036787b2089d0a2f4ff000000990c0f0e782092241f22108092000210991059718e000bf2068059f002818bfd047a08926600130a08109982674a660002d18bf0620dde90765099006eb0b0082b2ba4280f0c682217c80b2042906467ad8aa5c461c4b1c0129237405dd022906d0032964d1e28106e021b9627503e0238943ea02222281be4280f0ab82042962d0aa5d831c8f1c0129277406dd059fff290bd003290bd11e4649e000291cbf278947ea0222059f228102e0627500e0e281bb4280f08e82022933d8ea5cc61ccb1c0129237405dd022931d0fe2905d1627506e021b9238943ea0222228100e0e281be4280f0768202292dd0aa5d031d41f00407fc2927740add059ffd290cd0fe2903d0278947ea0222059f228105e0059f0029c0d1e28100e06275bb4280f0598209b11e460fe0ea5c461d05202074e189208941ea0221e18128b1fff726fd0028206000f07b82b4f808906089a9eb00011ffa81fbb1b27a1a5a45b8bf93465fea0b4209d02268294410441ffa8bf2fdf7c2f96089b4f80890584481b2494560817ff45bafd4f800a0607dbaf1000f07964ff000060df12b0108bf8a460f288df82b6006d8217d00f00302511ab1fa81f14e0900074ff000050bd4217e607e814238bf2076ceb9607d40072cd400202ee000bf606858b14bf69b71c0f25d018847606848f2a351c0f2450188476560607e20760120e082002ee5d0607da67d400718d4002e099e4ff00000069000f08c811ffa89f100294ff6ff7300f0098101295bd10020cee04ff48030fff7aefc099e069079e14ff48030fff7a7fc002e824600f06f81a07d099e002800f06b81baf1000f00f067816668002e00f0fb8020890428c0f05e8121680a1812f8043c002b40f0578112f8033c002b40f0528112f8023cff2b40f04d8112f8012cff2a40f048814ff4803731607060c6f80ca037610028069000f064814bf69b70c0f25d0000f58572304600219047002840f031813169002900f02d8170688f420f46e7d1069a0f468242e3d123e14ff6fe7000ea090042426ff0010000bf0aeb0007be78ff7883ea06231eb25d002db208ead63686ea430308ead5355e0085ea430336b25d002db208ead63686ea430308ead5355e0085ea430336b25d002db208ead63686ea430308ead5355e0085ea430336b25d002db208ead63686ea430308ead53686ea430383ea07231fb25e0036b208ead73787ea430308ead6365f0086ea43033fb25e0036b208ead73787ea430308ead6365f0086ea43033fb25e0036b208ead73787ea430308ead6365f0086ea43033fb25e0036b208ead737023087ea430308ead6371618023687ea430395d1099e023000255feac97232d01af8000083ea002002b243001bb208ead23282ea400008ead333420083ea400012b243001bb208ead23282ea400008ead333420083ea400012b243001bb208ead23282ea400008ead333420083ea400012b243001bb208ead23282ea400008ead33282ea4003e0899ab2824277d1049b5046fdf771f9002871d148f2a35a099600260123c0f2450a7be03820fff798fb002861d00646057045708570c570057145718571c571057245728572c572057345738573c573057445748574c574057545758575c575057645768576c576057745778577c57780f8285080f8295080f82a5080f82b5080f82c5080f82d5080f82e5080f82f5080f8305080f8315080f8325080f8335080f8345080f8355080f8365080f837504ff28b60cff6ff70784430624ff2cb60cff6ff70784470624bf69b70c0f25d0045f6e46200f13c0730460f21c0f27c023823b84750b348f2a3513046c0f245018847099ecdf818a0606848f2a35ac0f2450a099638b14bf69b71c0f25d0188476068d047656000260323a575dde903121ffa89f7204600970df0a9fb0699054600293ff458ad0846d04754e5666076e63560f560099ec7f58039b7f5803fcdf818a07ff645aecfe74ff00008204631463a460323cdf800800df089fb606840b14bf69b71c0f25d0188476068d047c4f80480206884f81680002818bfd0470020287000202060a06020820a200bb0bde8f08f287a400603d409980bb0bde8f08f207c099df0b102980078d0b1dde9031200252046032300950df059fb606838b14bf69b71c0f25d0188476068d04765602068a575002818bfd04700202060a06020820b252068002818bfd04700202060a060208228460bb0bde8f08f02980078d0b1dde9031200252046032300950df02efb606838b14bf69b71c0f25d0188476068d04765602068a575002818bfd04700202060a060208206200bb0bde8f08f00bf2de9f041f0284bd100294ff00b0000f07f80022a7cd345f64d50c0f246000e4617468047811eb1fa81f14c09012808bf0224002c4ff00a006ad07578e91ec9b2fe2938bfbde8f081307833463946032860d002282dd001281cbf0a20bde8f081ac4204bf0020bde8f0814808842884bf0b20bde8f081881e03f1020887b240463946fff705fb002869d006462846fff75dfb21462a46fff72ffb3046bde8f08145f64d57c0f2460707f5e27cbde8f0416047ac421cbf0020bde8f081a1f1210010f1160f48d36ff0480048432330c0b2242841d81a4612f8020f40f00207032f4bd19b796340032b47d10b2904d0032804bf0b20bde8f081881e4ef60b6783b2c0f247070120f021b847002818bf0620bde8f081ac422dd1a1f10c0010f1030f1ad31a4612f8020f042815d158796040032811d1d879a0f1310717f1300f0bd39b79984208d9a1f10807c01a874284bf0b20bde8f081cfe70b20bde8f081b07a204207d04046394622462b46bde8f041fff78abb0020bde8f0810a20bde8f08140f20d00c0f2000042f6653c7844c0f2440c012100220023604700bf80b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd4ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f19d10a684ef66d63ccf2ff039a4212d14a68b2f57e7f0ed1d1f8701159b147f64c62c2f207021268891a012903db0178042938bf7047002101707047b0b541f23154c0f252041d46a0474ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f0cd10a684ef66d63ccf2ff039a4205d14a68b2f57e7f18bf002100e00021421c18bf002900d1b0bd95f84d20002afad091f84721002a08bfb0bd46f25863c2f206031a68132aeed8c2ebc20403eb04135b6801ebc2014ff40042c1f8e831a1f8ee212a7a01f5f471002a0fbf802248f20112ca808122ab7c33b1ab7d23b1022342f002028b71ca7195f820303bb195f8243023b1032342f004028b71ca7195f846304bb195f8443042f008020b7195f84530ca714b71b0bd48f27342c2f207021378002b14bf9378ff230b711378002b14bf5278ff224a71b0bdf0b589b048f66502c0f24b020c46054690474ff29c51c2f229010e68bff35f8f31466ff39601b1f1005f0cd131684ef66d62ccf2ff02914205d17168b1f57e7f08bf092d01d009b0f0bde1082c29fad896f847110029f6d045f64d51c0f2460107468847014638460129ecd1ff22079244f6435246f258610020cff60172c2f20601089006920b685a1e132a4fd8c3ebc30501eb05111a1fa1f5de7191f85451ad0618d4d51c42d091f8e450ad0616d4951c3cd091f87450ad0613d4551c36d00d79ad0610d4043aa1f5e071151da3f10403e3d12be05a1e01f5a87104e09a1ee03101e0da1e703106ebc203096893f9ef21b2f1ff3f1adc03f5f4731e688e4215d11e798df81b605e798df81c609b798df81e200a0a8df820108df821200a0c090e8df81d308df822208df823100f228df8012010228df80220522208218df80420b4f5804f4ff080028df800108df8061038bf4ff48042b4f5001f6ff07f0604f07f0138bf0026802c28bf80318df807109df809509df80a108df8030004d29df808200223002415e0c4f3c610b4f5804f024403d20323012400200be0b4f5001f03d204230224012003e0052303240120002135468df8053004f108038df808208df809508df80a1070b96846a222c118c254062048700c208870c81c06a90c22fcf7bbfc04f117034ff225066a46c0f2470601200821b047384609b0f0bd70b582b00546ff208df8040044f25220c0f20100009040f2c960c0f2480016460c46804798b148f27e30c2f2070000880122c1b264298df8032008d80721b0f5807f38bf03218df803108df80400301b28bf08280cd3d221281929550621417005218170033069460522fcf777fc0834204602b070bd00bf024600284ff0ff3009d0022918bf70471078112801d1507810b14ff0ff30704710b586b045f64d50c0f24600804701284ff000003ed1cde9020040f20830c0f210000190ff208df8140044f25220c0f20100049040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8132008d80721b0f5807f38bf03218df813108df8140040f2d260adf80800052001ac8df80a00e01d04a90522fcf721fc4ff2250cc0f2470c0120092122460c23e04706b010bd024600284ff0ff3021d0012918bf704710781f2819d110b545f64d54c0f24604a04701281cbf002010bd4ef24201c0f2000104f5e2747944e02029220023a047002818bf4ff0ff3010bd4ff0ff3070474ff29c50c2f22900016808466ff39600b0f1005f1ad108684ef66d62ccf2ff02904213d14868b0f57e7f0fd1d1f83c0160b147f64c62c2f207021268801a0028cebf01200020c1f83c017047002070474ff29c50c2f22900016808466ff39600b0f1005f23d108684ef66d62ccf2ff0290421cd14868b0f57e7f18d1d1f87001a8b147f64c62c2f207021268801a0028c4bf012070470020c1f8700181f86c01c1f87401c1f82003a1f88c0370470020704700bff0b101684ef66d62ccf2ff02914218bf704790f8461199b110b54af273644ff0000cc0f24604864680f846c10120002100220023aef844c1a446bde810406047704700bf02464ff6ab70cff6ff7042f6653c7844c0f2440c00210023604700bf70b5002866d0044600796179a279e37940ea012040ea024050ea036007d00e2803d0032856d1032502e0042500e00225fff75cff00284dd04ff29c50c2f22900066830466ff39600b0f1005f42d130684ef66d61ccf2ff0188423bd17068b0f57e7f18bf70bd45f64d50a478c0f24600804701282ed1082086f88802032086f88902102086f88a02b22086f88c02062086f88d0286f88e02462086f88f0243204ff0000e86f8900201204ef6057c06f5227286f88be286f8910286f8925286f89342c0f2470c09210d2386f894e2bde87040604770bd00bf2de9f04f81b04ef66d694ff29c560446ccf2ff09c2f229064ff6ff77002547f67f7b4ff07f0806e048f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f06d10168494503d14168b1f57e7f3ed04ff47e70fef70ffe002864d06ff0070100bf42180831b1f57c7f157255729572d572157355739573d573f2d14ff47e7141600121c0f800908173a0f8c8b180f8c781078447848784c784078547858785c785078647868786c786078747878787c78756e8001f5145afd1bff35f8f46e8000121b156e8001f5145f8d0a5e7002780f85b712a270821032380f85c71042780f8581180f85931102380f85d7180f85e1101214ff2250c074600f5ac7280f85a3180f85f1180f86031002c18bf0124c0f2470c012010210a2387f8614101b0bde8f04f604701b0bde8f08f00bf80b582b04ff0010ccdf800c000f002f802b080bd2de9f04381b04ff29c57c2f229073f68ddf820803c466ff39604b4f1005f10d13e684ef66d65ccf2ff05ae424ff0000904d17e68b6f57e7f08bfb946012804d013e04ff0000901280fd11c4615460e460746fff737fe2346044631462a463846002c18bfb9f1000f07d14af2736cc0f2460c01b0bde8f043604799f8460180b1b8f1010f7cd1d9f84001002189f84611a9f84411a0b342f68d41c0f2440188472ee0b9f844010121013080b2012898bf0846a9f84401d9f8400142f68d44c0f2440489f8461128b94846fff777fec9f8400140b1a047d9f84001a4f140024ff4c8719047a8b199f84601002848d0d9f84001002189f84611a9f8441100b1a0474af2736cc0f2460c0120002100220023afe745f64d50c0f24600804701282fd1082089f84801032089f84901102089f84a01002089f84b01b22089f84c01062089f84d0189f84e01462089f84f01432089f85001012089f85101b9f844014ef6057c070a09f5a47289f8528189f85301c0f2470c012009210d2389f8547178e701b0bde8f08300bf80b582b04ff0050ccdf800c0fff734ff02b080bd2de9f04f83b04568002d00f041828368002b00f03d8205eb03080024002612e0054650460e465946144601f069ff3146224628460aeb0b03002443451d464ff0000680f02582a94619f9013b03f07f07a740002b46ea070610d406f00703012b16dc0bb3012b40f01382a8eb0903082bc0f00e8205f10903dee700bfc14580f00782182c04f107044d46dcd900e200bf022b3ad0052b40f0fb81a8eb0903042bc0f0f6816b1dc7e7c14580f0f18195f90130002b05f10203bed5434580f0e88195f90230b3f1ff3f05f10303b4dc434580f0de8195f90330b3f1ff3f05f10403aadc434580f0d48195f90430b3f1ff3f05f10503a0dc434580f0ca8195f90530002b00f1c581ab1d96e7c14580f0c08195f9013005f1020a002b03f07f0b3dd5c24580f0b58195f90270eb1c07f07f04b7f1ff3f4beac41b02ddd1469a462de0434580f0a58195f9037005f1040a07f07f04b7f1ff3f4bea843b01dd99461de0c24580f0958195f9043005f1050c03f07f07b3f1ff3f4bea475b02ddd146e2460ce0c44580f0848195f90530002b00f17f8105f1060a4bea037be146a8eb0a039b4500f27581f308822b1cd0672b3ff437af652b7ff43eafbbf1060ffff43aaf9af80030462b7ff435af99f80230432b7ff430af99f80330012b7ff42baf0290cde9002112e0054650460e465946144600f0f4fa1be748f2a3513046c0f24501bff32f8f8847dde9002102984ff29c53c2f229031c68bff35f8f1e68bff35f8f33466ff39603b3f1005f0ad133684ef66d67ccf2ff07bb4203d17368b3f57e7f50d04ff47e70fef7a7fbdde9002106460298002e3ff4eeae6ff00703002500bff7180833b3f57c7f3d727d72bd72fd723d737d73bd73fd73f2d14ef66d63ccf2ff0333604ff47e7373600123b37347f67f73a6f8c8317f2386f8c7314ff6ff734ff29c5733847384b384f38433857385b385f38533867386b386f38633877387b387f387c2f2290757e8003fa34292d1bff35f8f47e800632bb157e8003fa342f8d088e7002599f8043099f8057099f80640032b47ea042727dd052b44dc042b6ed0052b7ff496aed6f870313bb147f64c67c2f207073f68db1b002b08dc002386f86c31c6f87431c6f82033a6f88c3347f64c63c2f207031b6845f69077c0f201073b44c6f8703174e6012b3bd0022b7ad0032b7ff46eae14468946fff7e4fb0346029849462246002b3ff463ae96f84631002b3ff45eae304641f28831a6f844710df015f950e0062b52d0072b7ff451ae81464df6db50c0f24a0016460d46804703465f1e294632464846012f3ff641ae981eb0fa80f04009fff7abfc29463246484636e647f64c63c2f207031b6845f69077c0f201073b44c6f83c3129e614468946fff7a0fb0346029849462246002b3ff41fae96f84631002b3ff41aaeb6f84431bb427ff415aed6f840310027002b86f84671a6f844713ff40bae42f68d411846c0f24401884702984946224600e6c6f8705186f86c51c6f87451c6f82053a6f88c53f5e596f84631c6f83c51002b3ff4efad0546d6f8400189461446002186f84611a6f8441120b142f68d41c0f2440188474af273670120002100220023c0f24607b847284622464946d1e54ff2a513c0f2490303b0bde8f04f18477fb50646002806d1fff73efb002807d19de80f00064649f29b1cc0f24f0c60477fbd00bff0b583b01d46144609294ed106460f46f22d10d8a22160196155062141700b2181704df28961c0f20001033079440b22fbf706ff0e35204629464ff4807200f03df80546ff208df8080044f25220c0f20100019040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8072008d80721b0f5807f38bf03218df807108df80800f82d0cd8d221601961550621417005218170033001a90522fbf7cefe0835394630464ef60577c0f2470722462b46b84703b0f0bd70b58ab00c464ff29c51c2f229010b68bff35f8f19466ff39601b1f1005f16d119684ef66d66ccf2ff06b1420fd15968b1f57e7f0bd1694605461846164601f0c3fe324628462825a24231d244e04d218df8001043218df8011001218df80210002115258df803108df804108df805108df806108df807108df808108df809108df80a108df80b108df80c108df80d108df80e108df80f108df810108df811108df812108df813108df81410a24213d3111b022910d30fd003398d420cd8c22201190255062048708d70c81c69462a46fbf758fe6019c41c20460ab070bd00bf2de9f04f81b0044600f1e20900f16005663004274ff0080b00900fe004234ff22506012009212a46c0f24706b0470af10100013fbff35f8f60603dd0d4f804a02068bff35f8f824536d00af01f00792110fb0191032011f8016b84f86100102084f8620070b24ff0010884f860b0002848bf4ff00208c8f17c02b2424ff0000284f86320cad39a22b0f1ff3f84f8642084f865b084f8660001dd012003e0f00984f867000220009a10443246fbf7fafd06eb0800831db2e704f124050121bff35f8f00bf55e8000f45e80012002af9d1bff35f8f00bb48f67766c0f24d060820b047a8b10146422008704b204870fe2088700020c87008714871c871887143f6ba60c2f20700007806f11c02904703e00020bff35f8f28600020bff35f8fa062e06abff35f8f38b92068bff35f8f6168bff35f8f88420ed004f128050121bff35f8f00bf55e8000f45e80012002af9d1bff35f8f10b101b0bde8f08f206a42f64d42c0f244021421904700281ebfbff35f8f0020286001b0bde8f08f2de9f04f81b00d46804600f06bf9002800f02b8145f64d50c0f246008047012840f023814ef66d6a4ff29c57ccf2ff0ac2f229074ff6ff7400264ff07f0b06e048f2a351c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f06d10168514503d14168b1f57e7f40d04ff47e70fef79ff8002800f0f3806ff0070142180831b1f57c7f167256729672d672167356739673d673f2d14ff47e7141600121817347f67f71c0f800a0a0f8c81180f8c7b1048444848484c484048544858485c485048644868486c486048744878487c48757e8001f4945add1bff35f8f47e8000121b157e8001f4945f8d0a3e7d0f8f413bff35f8f11b348f6776905f10800c0f24d0980b2c847002800f0a9800146422008704b204870fe20887000202a0a002dc8700d714a71c871887100f0c180042d05f0030080f096800022a7e098f80310022900f28c80074641f2040041f20404fef72ef8002800f08280064607f57d7900206ff019010022b318b05432327718a24201f1320158709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d87587f8320087f8330087f83400d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f83100b1d14ff6614042f66534cff6ff70c0f244047844002132460023a0470028306250d059e8000f50b9bff35f8f49e8006000283ff45faf59e8000f0028f6d0bff32f8fbff35f8f306a04f5c771884748f2a3513046c0f2450188474be701b0bde8f08f25f0030308f10107002200bfbe1816f8015c8c182572bd5c0432657275789342a572b678e672f1d180b118f8027001f1080301289f5409d0571c18f807600228de5503d0901c18f800201a5443f6ba60c2f20700007809f11c0201b0bde8f04f104748f2a3513046c0f2450101b0bde8f04f0847024600284ff0000000f08c80a1f1510313f1490fc0f0868010784b2808d15078422805d19078012802d1d3780c2b01d900207047082ba1f1080019dd0a2b26dc092b3bd00a2b57d104281cbf00207047107a517a08434ff0000008bf7047917ad27a41ea0221b1f5007f98bf01207047042b1ad0072b43d0082b3dd101284ff000004fd1117a022938bf012070470b2b25d00c2b30d1002804bf00207047117a1029c5d84900013125e007284ff0000038d1117a042938bf012070470428b7d1107a517a50ea01214ff000002ad0937ad27a43ea02228a4228bf01207047a1f10c001228a4d8117a537a1943a0d0917a01299dd8d17a401ab0fa80f04009704703284ff000000dd1117a537a927a41ea032141ea024144f24022c0f20f02914238bf0120704700bf2de9f04f87b04ff29c50c2f229000068bff35f8f02466ff39602b2f1005f17d102684ef66d63ccf2ff039a4210d14268b2f57e7f0cd1d0f8f44300294ff00000bff35f8f18bf002c06d107b0bde8f08f002007b0bde8f08f8878fe2826d1088844f64232904221d108794a7940ea0225aab2002a4ed0502a00f2988101f108000e461146fff728ff002800f08f81f17a307b737b3246012940ea032900f04f81002940f0b08194f856004be1204600f003fc94f8561039b994f8571021b994f85a10002900f0f2800121bff35f8fe16204f128050122bff35f8f00bf55e8001f45e80023002bf9d10029bff35f8facd10646206a42f64d42c0f244021421904700281ebfbff35f8f00202860304607b0bde8f08f0020bff35f8f606247f64c60c2f207000568606900284bd0281a49d494f85600e6688df8120094f8570094f858108df8130094f859008df814108df8150094f85c0094f85a108df81700300a8df81900300c8df81a00300e8df81b000a20628e02900df112008df81610019000272046012108238df8186000970cf063fce068b04208bf26616761e765678684f8597094f8571061b194f85b0048b901204ef2f53384f85b00c0f24c0303201322984794f85a00002860d0e069281a5dd446f6cc10c2f207000078002856d143f63c40c2f20700817a29b10178ff2902d14178ff294ad090f83a1039b190f83010ff2903d190f83110ff293fd090f86a1039b190f86010ff2903d190f86100ff2834d0002784f85a7094f85600e6688df8120094f8580094f857108df8140094f85c008df813108df81700300a8df81900300c8df81a00300e94f859108df81b000a2002900df112008df81510019020460121002208238df816708df8186000970cf0e9fbe068b04208bf2661a069281ab0f57a7fc0f28980a56194f8560094f85720b8b194f85a101ee0e16822698b1a18bf01239142bff35f8fe3627ff407af2168bff35f8f6268bff35f8f91423ff4bbaefce694f85a1032b921b9e1682269914262d0002100228df8120094f85800e5688df8140094f85c008df813208df81700280a8df81900280c8df81a00280e94f859208df81b000a2002900df112008df815208df816108df81850019000272046012100220023009730e0012084f856008df8120094f85700e5688df8130094f8590094f858108df8150094f85c008df814108df81700280a8df81900280c8df81a00280e94f85a108df81b000a2002900df1120000268df816108df8185001901ffa89f220460121002300960cf05afbe068a84208bf256194f8560030b994f8570018b994f85a0000284ad00120bff35f8fe06204f128050121bff35f8f00bf55e8000f45e80012002af9d1bff35f8f58b9206a42f64d42c0f244021421904718b10020bff35f8f2860012007b0bde8f08f907bd37b278e40ea0320b84233d1022961d1002384f8563094f85710a4f85e30002984f8593084f85d3000f0a88094f85b00002840f0a38001204ef2f53384f85b00c0f24c03032013229847002396e0e0682169421a18bf01228842bff35f8fe262aed12068bff35f8f6168bff35f8f8842a6d1c0e794f85600e5688df8120094f8570094f858108df8130094f859008df814108df8150094f85c0094f85a108df81700280a8df81900280c8df81a00280e8df81b000a2002900df112008df816108df81850019000271ffa89f22046012104232ee794f8560078b3052938d0042902f110082bd0032941d194f85a0068bb94f8570050bb0cf07ffa38b3012047f64c6184f85a00c2f20701096841f6583211448df8120041f2883049f61347e161002500900df11202c0f257070120002101230195b8472b4620e002231ee094f8570018b994f85a00002859d0032315e094f85710002000294ff00003a4f85e0084f8590084f85d007ff460af06e094f8580010b194f85b0078b3062394f85600e5688df8120094f8570094f858108df8130094f859008df814108df8150094f85c0094f85a108df81700280a8df81900280c8df81a00280e8df81b000a2002900df112008df81610019000271ffa89f2204601218df81850abe608292bd0072916d006293ad194f857000cf0affb68b947f64c60c2f2070000684ef660216ee0307c113631460cf045fbe8b30346b5e794f85e00002800f0cd80002584f85e5094f8570046f28563c0f24e030321424698472b46a2e794f85f00002800f0ba80002584f85f5094f8570098f8001040f23922c0f2550290472b468fe794f85900002840d00c2940d198f80080b8f1000f55d006f11100b8f1040f08f0030e039051d20023002577e098f8005044f2a932c0f248022846314690474df249270346c0f251070120294632460125b847011f11f1030f01d2052360e784f85700208e47f23051013080b2012888bf054647f64c602586c2f207000068a4f832900844606100234ae7072348e794f85c0000287ff42caf327c737cb77cf07c42ea032247ea00230929a4f83290a2865bd00a295bd118044ff0060008bf05205be0002384f85d302ae708f0fc0a0023002506eb450797f811b0ba7cf87c397d42ea0b0241ea000b797db87d97f817c03f7e120640ea010008bf01235fea0b6247ea0c0108bf0123000608bf0123002905f1040508bf0123aa45dad1bef1000f18d0039901eb450011f8157045782f4308bf0123bef1010f0cd08778c5782f4308bf0123bef1020f04d007794079384308bf0123180401d00123e1e6b8f1040f84f85d802bd2002247e0022005e0b07c00284ff00a0008bf092084f85c0047f64c60c2f20700006847f230573844606194f8570006f11407a5f10c06b6b292b29bb2019600970cf0d5fa00287ff4ccad0020092384f85c0060866061b0e608f0fc00002206eb42014b7c8f7c43ea072304eb4207fb86cb7c0d7d043243ea05233b874b7d8d7d904243ea05237b87cb7d097e43ea0121b987e4d1bef1000f27d00399bef1010f01eb420011f81210437804f1360041ea032120f8121018d0039f511c07eb410317f811705b78bef1020f47ea032320f811300ad0039b911c03eb410213f81130527843ea022220f81120002365e6002904bf002070472de9f04790b08a780446d5b2202d35d1208ee56801302086280a8df81e00280c8df81f00280e00278df820000a20a765e76567866761a4f8567002900df11700019020460121002200238df817708df818708df819708df81a708df81b708df81c708df81d5000970cf096f8e068a84208bf2561002010b0bde8f087a2f12400c0b2022843d894f85a0000283fd0262d63d0252d01d0ca7812b1002084f85a008df81b0094f85c00e56894f856208df81c00280a8df8172094f857208df81e00280c94f858308df81f00280e8df8182094f859208df820000a208df81930cb7802900df1170000278df81a208df81d50019020460121002200970cf04df8e068a84208bf2561012010b0bde8f08794f857000028aed00b888342abd1901ec0b2162855d8a2f10d00c0b2012800f2a28094f8560038b194f8590020b194f85b00002800f03d810e2dddd194f857004af25971c0f24c018847d5e794f85620002a9ed091f80890b9f11f0fccd8cb7a032bc9d8d1f804e0bef1000fc4d00e7b4d7b8df818608e7b8df817304b7a8f7a8df81950cd7b8df81a600e7c497cb9f1000f0df117088df81b508df81c608df81d108df81e308df81f708df8209000f05781b9f1040f09f0030c80f02781002738e1a2f12700c0b20d2812d36ff0380050fa82f01e2807d8012202fa00f08122c4f20002104204d1a5f135000228bff43aaf4df2c307c0f2510708468846b84798f802104046322901d027291dd1017843f6c40201eb0111c2f2070252f8211099b943f62d4ac0f2480a0120d047064600204ff00009d047042e00f29f80014631444046092900f2998007f1a00188472b2d21dc272d40463ed0282d00f083802b2d26d055e094f85c00a8427ff444af0020628e84f85c0060866061c8784f894e68098945ea0023cde901612046022100970bf07aff2fe72c2d404630d02d2d07d02e2d35d10179002900f00681002201e00022626194f85710a4f85e20002984f8592084f85d2000f0fa8094f85b20002a40f0f580e4e0c278b2fa82f1490984f858100021002a616100f0c88084f85b10a4f8581084f85710e1656186dfe0c178b1fa81f1490984f8591000216161d6e0352d40f0d480012184f85f10427c017c837c1204c77c42ea016141ea03214df683623944c4f21b32a1fb02239a0c43f609535a43a1eb82118df813100a0a090c8df8151000218df814208df816100df11301ade0002184f85b10a4f8581084f85710e16561866161a1e094f85710a4f85e90002984f8599084f85d903ff45faf94f85b0000287ff45aaf01204ef2f53384f85b00c0f24c030320132298474ee794f85d80b8f1000f3ff4bdae4f89c8f1000ca8f1010e002604eb4602d08eb84200f0b980b6453ff4aeae138fbb4200f0b2800ceb0603981c3ff4a5ae508fb84200f0a980d81c3ff49eae908fb84200f0a2800436b045dfd195e609f01c060ef101050027eb1913f8011c08eb070258789b789172e95d0437be42d17210735373f0d1bcf1000f12d01ef8070008f10a06bcf1010ff0550ad0781c1ef80010bcf1020f315403d0b81c1ef80010315409f10a0000210091c7b22046052100220023cdf8048002970bf087fe3ce694f8561011b194f85b10d1b194f85710002284f85b20a4f85e2084f8592084f85d2071b101204ef2f53384f85b00c0f24c03032013229847404602e0012184f85e10011dc0780027352d4ff0000608bf042745ea00232e2d08bf0227cde9006120460421002202970bf050fe94f85600e5688df8170094f8570094f858108df8180094f859008df819108df81a0094f85c0094f85a108df81c00280a8df81e00280c8df81f00280e8df820000a2002900df117008df81b10019020460121002200238df81d500096d1e5486809890022cde90101204603212b4600970bf016feebe500bf07b5fff7e5f80028bde8074006d138b584b04cf2f11cc0f24c0c6047704700bf4ff29c52c2f2290212688c4613466ff39603b3f1005fbff35f8f1bd113684ef66d61ccf2ff018b4214d15168b1f57e7f10d1d2f8f423bff35f8f5ab192f8561041b192f857202ab190f83d00904208bf4ff0040c604670472de91b50eff30084284609680979fff7cbff024684f30088bde81b90b0b50c4648f6776180b2c0f24d01884700b1b0bd4ff29c51c2f229010968bff35f8f0a466ff39602b2f1005ff1d10a684ef66d63ccf2ff039a42ead14a68b2f57e7fe6d1d1f8f413bff35f8f0029e0d091f85720002adcd02388934218bfb0bd8a68cb6801328a605a1cca600122bff35f8fca6201f12804bff35f8f54e8003f44e80025002df9d1002bbff35f8fc0d1096a42f64d42c0f244020546084614219047014600291ebfbff35f8f002121602846b0bd2946fff7a3bf00bf70b50d4648f6776180b2c0f24d01884700b170bd4ff29c51c2f229010968bff35f8f0a466ff39602b2f1005ff1d10a684ef66d63ccf2ff039a42ead14a68b2f57e7fe6d1d1f8f443bff35f8f002c08bf70bd0646a068e1680130a060481ce06020462946fff768fc94f8560028b994f8570010b994f85a0020b30120bff35f8fe062304604f128050122bff35f8f00bf55e8001f45e80023002bf9d10029bff35f8fb6d1206a42f64d42c0f2440214219047014600291ebfbff35f8f00212960304670bde0682169421a18bf012288423046bff35f8fe262d4d12168bff35f8f6268bff35f8f914293d0cbe73146fff787bf00bf46f65450c2f20700016840f2230cc0f2000cfc44c26861454ff001034ff0040008bf0223624508bf0820002a08bf1046002918bf184370472de9f04f81b04ff29c53c2f229031c68bff35f8f23466ff39603b3f1005f40f0cc8023684ef66d67ccf2ff07bb4240f0c4806368b3f57e7f40f0bf80d4f8c0310133c4f8c0314ff6ff739a4238bf134694f87871a4f8c43184f8c6011fb1002a18bf002907d1b4f8ca010130a4f8ca0101b0bde8f08f94f87f010028f3d0d4f8880170b147f64c69c2f20709d9f80030c01a002805dd94f87901012809d101270ce0b4f8cc010130a4f8cc0101b0bde8f08f94f87b71012f18bf022794f87e510020eb0793468846adf80200adf8000012d0022f10d194f87d0168b90df1020641f281556b46c0f25b05404611463246a84794f87e515a461646b2f5c86f28bf4ff4c86606f1150a5046fcf7fbfd002856d0bbf5c86f88bf45f08005d9f80010d4f8842153230271120a42710a0a8171c2710a0c090e03704d234172a02143700123c17200218370c57002728772017394f87d11bdf802204173bdf800108373c173090a0174110a8174310a017500f115014274c6740746084641463246faf777fa4ff27d12c0f2470238465146904710f1800fd8bf6ff07f007e28a8bf7e20d4f8841184f8c901481c48f2a351c4f88401c0f245013846884701b0bde8f08fb4f8ce010130a4f8ce0101b0bde8f08f10b301684ef66d62ccf2ff0291421bd190f87f11c1b1d0f88811b1b147f64c62c2f207021368cb1a012b0edb1268d0f88c01002808bf7047891a42f64d42c0f2440208bf01211047704710b590f879210021c0f8881180f87f11511eb1fa81f149f259624909c0f25902044640f20b10904710f1800fd8bf6ff07f007e28a8bf7e2084f8c8014ef6e374c0f25a04a04704f1bc00bde81040004700bf2de9f04f81b00429c0f09180824600784d2840f08c809af80100432840f087809af80200012840f082804ef66d684ff29c570d46ccf2ff08c2f229074ff6ff7600244ff07f0b06e048f2a351c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f06d10168414503d14168b1f57e7f40d04ff47e70fcf709fd002851d06ff0070100bf42180831b1f57c7f147254729472d472147354739473d473f2d14ff47e7141600121817347f67f71c0f80080a0f8c81180f8c7b1068446848684c684068546858685c685068646868686c686068746878687c68757e8001f4945add1bff35f8f47e8000121b157e8001f4945f8d0a3e79af80310022905dc01290ed0022900f09e800ce003290dd0042908d190f8781129b101b0bde8f04fdce00d2d06d201b0bde8f08f044600f031f987e090f87f1119b390f87921002180f87f11511eb1fa81f149f259624909c0f25902044640f20b1090474ef6e37510f1800fd8bf6ff07f00c0f25a057e28a8bf7e2084f8c801a84705f1bc00804720469af804100446012988bf002180f879119af8051001f00302012a4ff0010198bf0a4680f87b219af80630012b88bf002380f87c319af80730022b88bf002380f87d319af808309af8097043ea0723502b98bf5023b3f5f07f28bf4ff4f073a0f880319af80a309af80b7043ea072341f28837bb4238bf1f46a0f882719af80c3080f8781103f0030380f87e3102f00103002103eb5202c0f88411c0f8c011a0f8c41180f8c611c0f8cc11a0f8ca117f2180f87a2180f8c91100f009f8204600f04cf8204601b0bde8f04f78e000bfb0b5044690f87e01800758bfb0bd94f879014ef633750128c0f25a0507d105f1b000804705f58b71002088470ee005f5b670804794f87901012801d1002004e094f87b01013818bf0120a84794f879014ff6d5220138b0fa80f0cff6ff7249f2015341097a44c0f2590340f20b10984710f1800fd8bf6ff07f007e28a8bf7e2084f8c701012084f87f01b0bd70b5044647f64c60c2f20700006894f87f1145f69075c0f2010500292844c4f8880108bf70bdd4f88c0142f66536c0f2440658b94ff64140cff6ff707844002122460023b047c4f88c0150b106f594718847d4f88c0106f1e8022946bde87040104770bdb0b5044600f5c875082084f89001032084f89101102084f89201002084f89301c22084f89401062084f8950104f29711204600f065f84ff0280e4ef6057cc0f2470c012009212a462f2384f896e1bde8b040604770b54ff29c50c2f229000468bff35f8f20466ff39600b0f1005f46d120684ef66d61ccf2ff0188423fd16068b0f57e7f18bf70bd94f87f010025c4f8885100b394f8790149f259620138b0fa80f04109c0f2590240f20b1084f87f5190474ef6e37610f1800fd8bf6ff07f00c0f25a067e28a8bf7e2084f8c801b04706f1bc008047d4f88c0184f8785170b142f68d45c0f24405a847d4f88c0105f166018847002804bf0020c4f88c0170bd70b505464d200870432048700120887095f878010c46c87095f87901087195f87b01487195f87c01887195f87d01c87195f88001087295f88101487295f88201887295f88301c87295f87e01087395f87f01487345f64d50c0f246008047a073d5f884010026e073d5f88401000a2074b5f88601607495f887012675a074a020e074d5f8c0016075d5f8c001000aa075b5f8c201e07595f8c301207695f8c401607695f8c501a07695f8c601e07695f8ca01207795f8cb01607795f8cc01a07795f8cd01e07795f8ce0184f8200095f8cf0184f8210095f8c70184f8220095f8c80184f8230095f8c90184f82400fff711fc84f82500d5f88801a0b147f64c61c2f207010a68821a012a0bdb0968401a44f6d35100f2e730c1f26201a0fb01018e0900e00026300a84f8266084f8270070bd00bf2de9f04182b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f79d121684ef66d62ccf2ff02914272d16168b1f57e7f6ed194f8d01179b394f8d201c0070ad0d4f8700178b347f64c61c2f207010968401a002827dd47f24c45c2f2070555f8040c002854d042f29707c0f24c074af6e908002607f5437101a8c0f24b0801968847a8b1032084f8d3612860b4f8d401c04702b0bde8f081d4f8dc11c1b302b0bde8f0410847204602b0bde8f04133e0019841f60d56c0f24c06a861b04706f5af708047e861b847012084f8d30103202860b4f8d401c047d4f8e401a0b10199d4f8e021b4f8d671531a38bf8b1abb420ad2b4f8d81159b147f64c62c2f207021268101a884203d32046022100f078f802b0bde8f08100bfb0b590f8d041002c4cd044f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114614d14a68b2f5870f10d36ff35602012a0cd14a684ff65363cff6ff737b449a4204d1d0f8dc21002a18bf4a60002180f8d01190f8d111b1b1002180f8d11145f60a61c2f20701097869b947f24841c2f20701096839b14af68171c0f24b010546042088472846002c08bfb0bd0321bde8b04000e000bfb0b588b0044645f64d50c0f246000d468047012840f08f8008208df8010003208df8020010208df8030000208df80400ca208df8050006208df8060018208df8070047f26440c2f2070050f81c2c94f8d011002a18bf0122002945f60961c2f207014b7818bf0232002b94f8d33118bf0432002b4ff0410318bf08328df808304c238df8093001238df80a308df80b508df80c2050f8182c4ef605758df80d200268c0f24705130a8df80e208df80f30130c120e8df810308df811204268130a8df812208df81330130c120e8df814308df8152002698df81620120a8df8172009788df8181081688df819106821c2f2000109688df81a10090a8df81b1047f64c61c2f207010a68130a8df81c208df81d30130c120e8df81e308df81f2000680df10102c4f8e00108680921c4f8e40101201f23a84708b0b0bd00bf2de9f04f83b0022902d24ff0ff359ee0cde901100120cff680704ef66d644ff29c56ccf2ff04c2f229064ff6ff77851c4ff0000847f67f7a4ff07f0b07e000bf48f2a351c0f24501bff32f8f8847d6f80090bff35f8f3068bff35f8f00ea0501b1f1005f06d10168a14203d14168b1f57e7f45d04ff47e70fcf7b0f80028c4d06ff0070142180831b1f57c7f82f8088082f8098082f80a8082f80b8082f80c8082f80d8082f80e8082f80f80ead14ff47e714160012104608173a0f8c8a180f8c7b1078447848784c784078547858785c785078647868786c786078747878787c78756e8001f4945aad1bff35f8f00bf46e8000121b156e8001f4945f8d09fe7044645f64d50c0f24600804701281dd1029d6978022916d00129204607d000297ff473af00210025fff7ccfe0fe0019f41f28831022f0ed14ff4fa730022012741f2883624e02046fff748fe0025284603b0bde8f08faa78052f11d3eb782e79072f43ea062310d395f805c0ae79092f4cea0627e4d3ee792d7a46ea052607e041f288364ff4fa7301e041f288360127642b98bf64238b4228bf0b4690f8d01180f8d221a0f8d431a0f8d671a0f8d86171b147f24841c2f20701096800295ed001210446fff77efe0025c4f8e451bee744f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114632d14a686ff00105b2f5870ffff483af0123cff680731a40012a7ff47caf4b684ff65d22cff6ff727a44934214bfc0f8dc31d0f8dc31002b3ff46daf4a60012180f8d01199e74af60771c0f24b010420884720b105462046fff7a4fd5be70121204684f8d1118fe76ff0010553e700bf70b582b00c464df6cb61c0f245018847002c18bf002801d102b070bd0168e029fad10546fdf776f8014628460029f3d0a278e3782679217842ea0322637942ea0642042942ea036604d16178002908bf0d2e1fd026f001010629ddd147f29120c0f246008047014628460029d4d04ff65914062e4ff00f034ff00006c0f24e044ff000004ff000014ff0000208bf0c2301960ae004204ff6591401900026c0f24e040020002100220e230096a047284602b070bd3146fff7a3bf00bfb0b582b00c460546fdf726f80146002d4ff0000018bf002901d102b0b0bd47f2fc70c2f2070000684df6cb61c0f24501884790b10068e0284ff00000edd129784ff6591c0191c0f24e0c0021002223460090e047012002b0b0bd002002b0b0bd08b530460921fff7cbff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460a21fff7b7ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460b21fff7a3ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf01280bd1002908bf704710b5896804460846fdf717f82046bde81040704700bf10b5059c4ff20340c0f2460080472146fff7e6ff10bd00bfb0b5c4b0044600284ff0000000f0c78089b20b2940f0c380207838b96078092804d1a078612801d1e07810b1002044b0b0bdfcf789ff0028f8d045f64d50c0f24600804701281cd00228efd103208df8070001200df107058df80800a81c21460b22f9f737fb4af66104c0f24604f02029460d220023a047b0fa80f0400944b0b0bd4ff29c50c2f22900006801466ff39601b1f1005fc9d101684ef66d62ccf2ff029142c2d14168b1f57e7fbed122797f21042a06dc012a0cdcbab1012a08bf00211ae0082a0bdc052a11d0082a08bf0a2112e0022a0dd0042a08bf01210ce0092a09d00a2a08bf0e2106e0092104e0022102e0032100e00b2180f80b1310216a2580f80c13a22180f80653152580f80e13062180f807531a2580f80f130c2180f80853132580f8101352210822022380f8095380f811134921012580f8042380f8053380f80a2380f80d3380f8121380f8135380f81453217900f5417280f815136179192380f81613a17980f81713002180f81813e17980f81913217a80f81a13617a80f81b13a17a4ff2250480f81c13c0f247040120e021a047284644b0b0bd00bf1fb5fff72bff002803d0bde81f4000207047bde81f4038b584b049f21f13c0f24703184710b5f0281bd1d1b1cab10c78032c16d10d2a1cbf0b2010bd487801281cbf0a2010bd45f64d50c0f246000c468047012803d1a01c0b21fff7fffe002010bdbde81040fcf729bb00bf002804bfa3207047017e427e837ec07e41ea0221184300064ff0a40008bfa320a4220a2928bf1046704700bf4ff29c52c2f229021268bff35f8f13466ff39603b3f1005f10d1d2f800c04ef66d63ccf2ff039c4508d15368b3f57e7f02bf0023c2f8fc32c2f8003347f2c942c0f24d02104700bf4ff29c50c2f229000068bff35f8f01466ff39601b1f1005f0fd101684ef66d62ccf2ff02914208d14168b1f57e7f02bf0021c0f8fc12c0f8001347f2d940c0f24d0000472de9f04184b0dde90a76022f16d340f64705a94212d08eb13578412d0ed175784e2d0bd1014602f0bf0012284ff00d0008bf002b0ed004b0bde8f0810c9d0097cde9016546f63916c0f24d06b04704b0bde8f081082feed3b078012802d1f078022803d90d2004b0bde8f081002953d045f6f444c2f2060414f8040c81424bd12068002848d048f22732c0f254020d4608460221904700283ed024682088d8b36088c8b3a088b8b3e088a8b330797179b279f37940ea012040ea024050ea0368d0d04ff29c50c2f229000068bff35f8f01466ff39601b1f1005f1bd101684ef66d62ccf2ff02914214d14168b1f57e7f10d1f178a1b190f8fc22aa420cd1d0f80023424508d101295bd1082faad100f09bf875e0f07818b1082004b0bde8f081082f9fd1faf792fdc8b10646d0f870010028f1d047f64c61c2f207010968401a0128e9db96f8fc0268b196f8fd02032809d00628e0d1304600f076f8dce7112004b0bde8f08146f63917c0f24d0707f53a60804707f539622846214690470120002786f8fd020df10f02304600210123a6f8fe7286f8fc52c6f800838df80f70adf80c7000f07ef861884af2bd6603abc0f24c0628460222b047384604b0bde8f081a7f1510189b24ff6b87291424ff00d0119d390f8fd02032808467ff42baf334613f8080fa7f108018ab280b9052a0ed9a1884af2bd67c0f24c072846b847002004b0bde8f081084604b0bde8f08101280dd1042a0bd3981810f8021c00297ff420af10f8010c00287ff41bafe0e702284ff00d0008bf062a7ff4fcae707b01283ff60fafd4e790f8fd120a1f022a38bf7047022905d0012907d1042180f8fd127047052180f8fd12704780b582b0052180f8fd120121adf8061045f6f441c2f2060190f8fc0209684af2bd6c49880df10603c0f24c0c0222e04702b0bde8804070472de9f04fc7b0054690f8fc0203920591d8b145f6f041c2f206010978884214d148f22732c0f2540202211c46904760b195f8fc0240f66502c0f255020021904718b100690088172802d247b0bde8f08f033880b2f42828bff42007af0b3807f101084ff000094ff0000a01900495029414e000bf0699cde94509adf810112121adf8121143f23d5144a8c0f255018847049d029c1ffa8af0a042d6d2a4eb0a01019a1ffa81fbb5f8fe1241235a45b8bf93468df81c304e234a1c8df81d300123a5f8fe22d5f800238df81e30059b1ffa8af08df81f30130a8df82410090a8df820208df82130130c120e8df82510b0fa80f18df823204a091ffa8bf111fa8afaa2458df8223008bf0232bbf1000f8df826202fd001390329039901eb000c01d2002115e04ff6fc700bea00020cf10103002100bf5e1816f8014c7d187078ec725c5cb67804318a422c736873ae73f1d1049d1bf003020dd01cf80160cb19012ade7207d00ceb01004178022a19731cbf8078587395f8fc020bf10b0406900b2010fa84f649f6cd713046c0f24c01884700283ff47eaf26f003031ffa84fc06f003025e426ff00303c1180433f518043581f8049081f8059081f8069081f80790f2d152b10344012a83f8049005d0022a83f8059018bf83f806900bf10e0202701b22442108230272220480f801908170c370417283723ff43dafbcf1040f01d2002214e047f6fc710cea0103002200bfb95c8618f17218f8021008eb020531736978ad78043293427173b573f0d11cf003033ff41fafbe5c00f10b01012b8e543ff418af561cbd5d022b8d553ff412af0232bb5c8b540de7f0b583b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f4bd121684ef66d62ccf2ff02914244d16168b1f57e7f40d194f8fc12e9b3002840d002888a423dd145f6f446c2f2060616f8041c8a4235d1316899b3356894f8fd12032919d8d4f8701139b147f64c62c2f207021268891a00290edc02218df80b100df10b020746204600210123fff78dfe2046fff75cfe38468278092a1bd141896b8894f8fd72994217d1032f3adc012f6ad0022f3dd004e018b181780939022901d203b0f0bd47f6ed31c0f24d0103b0bde8f040084794f8fd72032ff1d8a2f10d0102290cd2c178d1b302208df8040001aa204600210123fff757fe03b0f0bd092a08bf032fdcd14189aa889142d8d1c078002800f0818002200df105028df80500e6e7052f04d0042fcad1c07805262fe0c078002848d0062084f8fd0202208df807000df10702204600210123fff72cfe052fb5d02046fff7f9fd03b0f0bd4268002aadd00389a3f2012189b2b1f57e4fa6d34089082b48d12988884245d12046012108234ce0c0780526002808bf022650b102208df80a000df10a02204600210123fff701fe6988012294f8fc024af2bd67adf8082002abc0f24c07022284f8fd62b84703b0f0bd052f15d1002094f8fc523668c4f8fc02c4f8000347f2c944c0f24d0404f11000804728463146224603b0bde8f0401047032084f8fd0201208df806000df1060272e7e98888427ff457af2046022103e0204603210022002303b0bde8f040bfe506290ad14178012907d18178653911f1630f02d3c178012902d94ff0ff307047f0b581b00179427941ea022540f6b8318d421fd80646fcf791fad8b1faf774fac0b10446d0f8d80342f66537c0f2440758b941f27320c0f200007844012100220023b847c4f8d80320b107f1e8022821904718b14ff0ff3001b0f0bdb078f178d4f8e4230023c7f6e07340ea452003ea425240ea0120bff35f8f1043bff35f8fc4f8d403002001b0f0bd00bf02293cbf4ff0ff3070472de9f04f9db00278437842ea0322b2f5007f0c9204d94ff0ff301db0bde8f08f4ef66d694ff29c57ccf2ff09c2f229074ff6ff76002547f67f7b4ff07f0805910b9008e000bf48f2a3512046c0f24501bff32f8f8847d7f800a0bff35f8f3c68bff35f8f20466ff39600b0f1005f06d12068484503d16068b0f57e7f3ed04ff47e70fbf720f9c0b304466ff0070021180830b0f57c7f0d724d728d72cd720d734d738d73cd73f2d14ff47e7060600120c4f80090a073a4f8c8b184f8c78126846684a684e68426856685a685e68526866686a686e68626876687a687e68757e8000f5045afd1bff35f8f47e8004028b157e8000f5045f8d0a5e700240c98ddf814b058b1fcf7cff90146002c4ff0ff3018bf002902d11db0bde8f08f0c984ff0000ab0fa80f040098df834a08df835a08df836a08df837a08df838a08df839a08df83aa08df83ba08df83ca08df83da08df83ea08df83fa08df840a08df841a08df842a08df843a08df844a08df845a08df846a08df847a08df848a08df849a08df84aa08df84ba08df84ca08df84da08df84ea08df84fa08df850a08df851a08df852a08df853a08df854a08df855a08df856a08df857a08df858a08df859a08df85aa08df85ba08df85ca08df85da08df85ea08df85fa08df860a08df861a08df862a08df863a08df864a08df865a08df866a08df867a08df868a08df869a08df86aa08df86ba08df86ca08df86da08df86ea08df86fa08df870a08df871a08df872a08df873a0bcb1d4f87421a2b14ff4006304e03f683b44b3f5a12f0cd2d718be680136f6d07e68033626f003060aeb060101f1100aede70c9a4ff0020e002a00f0f780abf102020a2ac0f0f28000260b9800eb0e0292f801904ff0ff30b9f1010f3ff647af9378d778157943ea0723577943ea054545ea07610027a1f10113cff6ff77bb420a91fff434af137a92f809800ef10a0743ea08210495abeb070c4d1e6545bff426af0796967992f807b00a9d039246ea0b22ad1a0892fff41aafa9423ff617af0b98cde9016310f80e50069745ea09200991c2080df1340c1cf802e005f007050126ae4016ea0e0f40f0a3821cf8025046ea05060cf8026040f2ff75ddf824c0c0f205054cb3d4f8746136b34fea0b294fea082e4ff40062b718bb6883423ed03b681a44aa4218d8b718bb68834236d03b681a44aa4210d8b718bb6883422ed03b681a44aa4208d8b718bb68834226d03b681a44aa42dfd900bf0898002840f0688204984ff6fc720330c0f203021040504400f1100addf814b00698079e0ceb000e0c98013686424ff0000028bf01204cd2abeb0e02092a3ff657af46e078680a998842089940f04482f8688842c0f04082e0d9401a844500f23b82bcf1000fd9d00298ddf814b00eeb00010198c1f1000e09eb000207eb0208a1f1010c0026039b08eb06023344107c9d7aa84240f02082b44517d0557cdf7abd4240f019820eeb0607bd1c0ed0957c187b854240f01082f81c07d0d07c5a7b904240f009820436b142dcd1ddf824c0a6e7002800f00082beeb0b004ff0000118bf0120baf5a02f88bf012101430c980028c1f100003ff44dae00297ff44aaed4f874b1bbf1000f43d1002140f6bf10cff6ff71c2f21e0001f57841002200bf43184031c3f8e127c3f8e527c3f8e927c3f8ed27c3f8f127c3f8f527c3f8f927c3f8fd27c3f80128c3f80528c3f80928c3f80d28c3f81128c3f81528c3f81928c3f81d28dcd14ff4a021a0f11f0bc0f8e11741f2a410c2f21e0000214ff0ff320160426081604ff40060c4f874b1c4f8200340f2ff790221c0f205094ff0000a09e00c980af1010a06eb080182454ff000003ff4efad0b980a914518af79405c0997ef796e7808972f7a95f802c0e9782a796b7907976f7a002c40ea0628069718bfbbf1000f7dd14cea012040ea024040ea0367f81c20f00300d4f82093cdf814a000f1100a00200497b9f1000f26d00beb09035bf809105a68514580f0c780eab10beb02005bf80210d0f80490514580f0b880b9f1000f11d00beb09035bf809105a68514580f0b28042b10beb02005bf80210d0f804905145d6d3a2e000204ff400694ff4006504e000bf3544b5f5a12f23d20beb05018a685bf805600132f4d04868ca68033020f0030000f110070beb09001032f8f757fb4168c2684bf809708368d4f874b18a424ff00001b94408bf00f110014bf82310d7e7c9f5a1210f294ff00002c4f820233cd90beb09004bf809104ff0ff3142608160c260c4f8209336e0012e3ff67faf4ff400660beb0605a868404500f0a580286806444e453ff672af0beb0605a868404500f09a80286806444e453ff667af0beb0605a868404500f08f80286806444e453ff65caf0beb0605a868404500f08480286806444e45d4d950e710b10268114401604ff000090020b9f1000f76d00beb09035bf809105a68514522d2002a6dd00beb02005bf80210d0f80490514513d2b9f1000f62d00beb09035bf809105a6851450ed2002a59d00beb02005bf80210d0f804905145d7d305464e469146184601e01d461646a1eb0a0110290ed309eb0a020beb02034bf802104ff0ff315e60996000211646d960c5f800a0ddf814a000280cbfc4f820634660049840f2ff7968600020c0f20509c5f80880e860dde9082141ea0221dde9063281420a9842ea032600f10a087ff4c6ae05f1100778180b9932464144f8f745fae9686868aa68d4f874b13144e96081424ff0000018bf07464bf82270aee6e868d8e700204ff400684ff4006603e02e44b6f5a12f23d20beb06018a685bf806500132f4d04868ca68033020f0030000f110070beb08001032f8f763fa4168c2684bf808708368d4f874b18a424ff00001b84408bf00f110014bf82310d7e7c8f5a12200210f2ac4f820130dd90beb08034ff0ff304bf8082059609860d960c4f820831db0bde8f08f00281ebf0168114401604ff0ff301db0bde8f08f00bf02293cbf4ff0ff3070472de9f04f8bb00278437842ea0325b5f5007f00f2988005eb4502022303eb4202914240f090800590fbf705fe002800f08a804ef66d694ff29c56ccf2ff09c2f229064ff6ff77002447f67f7a4ff07f0806e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f06d10168494503d14168b1f57e7f3ed04ff47e70faf7e3fc002858d06ff0070100bf42180831b1f57c7f147254729472d472147354739473d473f2d14ff47e7141600121c0f800908173a0f8c8a180f8c781078447848784c784078547858785c785078647868786c786078747878787c78756e8001f5945afd1bff35f8f46e8000121b156e8001f5945f8d0a5e7d0f874c10495bcf1000f20d000234ff4006104e03f683944b1f5a12f0bd20ceb0107be680136f5d07e68033626f0030633441033eee775b9b3f5a02f40f2e0804ff0ff310bb0bde8f04f08467047002d00f0d680002305994ff0000901f1020a153103914ff0ff31059109eb49060aeb46056c784ff0ff31012ce3d8af78ea7857ea022e1ebf2a796d7952ea0528d9d0bef5207fd6d8b8f5f07fd3d80ef10102520802fb08fb4ff6fb7240f2ff779345c0f20507c6d81af81610059e41ea0421039cb9f1000f28d014f8132c14f8125c42ea05228a42b3d0feb114f80d2c14f80c5c42ea05228a42aad0012e15d014f8072c14f8065c42ea05228a42a0d0022e0bd014f8012c257842ea05228a4297d0043e721c04f11804d6d15646bcf1000f2fd00bf105054ff400640ceb040adaf808208a4230d0daf800201444bc4220d80ceb040adaf808208a4225d0daf800201444bc4215d80ceb040adaf808208a421ad0daf800201444bc420ad80ceb040adaf808208a420fd0daf800201444bc42d3d94ff6fc720bf10801c0f201021140194401f1100320e0daf80410a9427ff44fafdaf80c10a9427ff44aaf9af8101004297ff445af9af811109af8122041ea022171457ff43caf9af813109af8142041ea022141457ff433af049909f1010989450599b24601f1010105917ff43dafb3f5a02f3ff624af00240025019019e000211fe7d0f874c14a688b6840f2ff7793454ff00002c0f20507c1f80cb008bf01f110024cf82320013575454ff000013ff408af05eb45000aeb40011af8100091f8018091f802e091f803b091f804904979bcf1000f40ea082605912cd0b8f1010f29d84ff400610ceb01039a68b24200f0a8801a681144b9421dd80ceb01039a68b24200f09e801a681144b94213d80ceb01039a68b24200f094801a681144b94209d80ceb01039a68b24200f08a801a681144b942d7d94eea0b21059b013149ea0322490801fb02f75a4607f1050b8df81d004fea1b208df820004fea1b408df8210005208df82500042001218df8270048468df81b1003908df82a000df11b0011218df81c408df81e808df81fb08df822408df824408df823408df82640cdf808e08df828e091468df829208df82b30fff7a5f900287ff482ae01980021d0f874c1bcf1000f3fd0ddf810e0b8f1010f3cd84ff400630ceb03018a68b24235d009680b4440f2ff71c0f205018b4226d80ceb03018a68b24228d009680b4440f2ff71c0f205018b4219d80ceb03018a68b2421bd009680b4440f2ff71c0f205018b420cd80ceb03018a68b2420ed009680b4440f2ff71c0f205018b42cbd9002104e0ddf810e02ee7ddf810e0002f3ff41aaf4fea0922029f059b42f001023a44039f1b0252083b4353435a1e032a01d200220ae023f0030700228e18043297427475b475f4753476f7d113f003033ff4f8ae01f11507012bbc543ff4f2ae3a44022b547018bf9470ebe62de9f04f81b04ef66d644ff29c55ccf2ff04c2f229054ff6ff7600274ff0010947f67f7a4ff07f0b07e000bf48f2a351c0f24501bff32f8f8847d5f80080bff35f8f2868bff35f8f01466ff39601b1f1005f06d10168a14203d14168b1f57e7f3ed04ff47e70faf75dfa00286ad06ff0070100bf42180831b1f57c7f177257729772d772177357739773d773f2d14ff47e710460416080f80e90a0f8c8a180f8c7b1068446848684c684068546858685c685068646868686c686068746878687c68755e8001f4145b0d1bff35f8f00bf45e8000121b155e8001f4145f8d0a5e7d0f8881379bb4bf2a011c2f21b01a1f121074ff416320023c7f82130c7f82530c7f82930c7f82d30c7f83130c7f83530c7f83930c7f83d30c7f84130c7f84530c7f84930c7f84d30c7f85130c7f85530c7f85930c7f85d304037403adcd1c0f8881300e00021084601b0bde8f08f00bf2de9f04f93b04ff6ff75a9424ff0000c2cd0dde91c874ff4805ecde90aecd0f89ce3cde90087229fdde920a9dde91e6b249c0697239f0aadcdf840c0cde911eccde90845cde9026bcde904a90546079704f0fcfd84464cb1bcf1000f06d19df8480095f8a013084385f8a003604613b0bde8f08f2de9f04f93b00446002047f64c69a4f8a003c2f20709d9f80000002a0f461abfc4f894030146d4f89413401ac4f89c03fbf786fa002857d0d4f88803002853d040f6b80ab4f88c03c2f2000adaf80020461e002808bf4ff6ff76002a44d04ff6ff7086421fd04ff48050d4f89c130a900020cde911100aa90b901090cde90801cde90600cde90400cde902004ff4a07001904ff4f075204631464ff42073009504f09afd00bbd4f86481b8f1000f0dd142f62b30c0f2440000f57371002088478046bff35f8fc4f8640150b142f6f770c0f24400a0f1660240464ff47a71904738b1012084f86b014ff0ff3013b0bde8f08f4ff0000bbff35f8f84f869b184f86ab10120bff35f8f84f868014ff4f0704af61f4501904ff4207cc0f247050020002100220023cdf800c0a84790b142f6f775bff35f8f84f868b1c0f24405012084f86b014ff0ff344046a847204613b0bde8f08f0120387194f86901bff35f8f88b942f62b3b40f2e737c0f2440b00bf0120d84794f86901bff35f8f1fb10028a7f10107f4d0022815d042f6f7750128c0f24405d3d1daf80070002fcfd04ff6ff7086420dd1d4f8881338464ff41632f7f78cfd3ee042f6f7750124c0f24405c2e74ff48050d4f89c130a9000200b901090cde91110cde90600cde90400cde902004ff4a0700aa901904ff4f07009914ff0010a0090204631463a464ff42073cdf820a004f0e8fc68b1d4f8881338464ff41632f7f75afd04f072febff35f8f84f86aa18be79df8480094f8a013084384f8a003384604f063fe0120bff35f8f84f86a014046a84794f8a00384f8a10358b3d4f8900342f62b36c0f2440628b1d9f800102d31c4f8981313e047f63560c0f2000006f13a077844002122460023b847c4f89003d9f8001001f12d01c4f8981320b106f591722d21904728b1002084f8a10313b0bde8f08f002013b0bde8f08f00bf80b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd022a3cbf4ff0ff3070472de9f04fadf2345d239008784f780a4450ea072001f10206249002f07583002725932292964202f06983701c904202f0658330787178b31c40ea0121d01a81422c932e9102f25a832598ddf880253690ddf85805ddf878153790ddf85c053f913890ddf8600527973990ddf864053a90ddf868053b90ddf86c053c90ddf870053d90ddf874053e901068411e0028116002f034832e98002802f030832e98841c032c02f02b83f778072f02f227832c9890f80090f80702d106f1040e27e02e9820f00100022802f0198396f804a0757923984aea052b594636aa02f01efb002842f00c83ddf8640506f1060e012d3a900dd8ddf88005806948b14feadb020af00701835c012505fa01f119438154b80706eb04002a904cd44ff0000a042f71d32a9fbaf1000f4cd1be454ad0724612f8011b3a98ba4268d09ef801200ef10203120642ea0141bb4200eb214c69d00ef103029ef80210ba425fd09ef803200ef10403120642ea0141bb424fea214154d00ef105039ef80420bb4200f09b8738469ef805700ef10603834242ea072200f0ba870ef107079ef80630874201f031839ef807700ef1080e3f024ff0000a3fe000bf864508d01ef9014b4ff0000af04606e03a98002121e000244ff0010a804645f64d55c0f24605a847012808bf0134601e81b2660800ebd130002c48bf4610a84701283a9808bf76423044c6463a90042f8dd2ddf8a8802fe0be4600eb010c4ff0010a0022002108e04ff0010a002203e04ff0010a00220021be46002300273b433b9f62440b447fb1dde93c76dde93e40bc45d8bfbc46b142d8bf3146a242a8bf22468342a8bf0346ddf8a8800120cde93cc1cde93e233b90baf1000f42f04582b9f1040f40f38280b9f1060f40f3f280b9f1070f00f05d82b9f1080f00f06b81b9f1090f42f03182c64502f02e821ef801abc64542f02982baf10f0f02f22582ddf87c05002801f073833b993698dde93829002900f02c863799dde93d733f9e994227eae775b8bf0b46b242b8bf1646b542359681f25c833c994fea630c21eae17b4fea0a11339135990bf10104491b4fea5b0e4aea0a1211f003084fea54013494329100f0fc869b4581f2858205fb09045feacb7171465e4609d014f80e10349e01f0f00141ea0a0104f80e10329961452144319681f2618204eb0c060c4604f8012bb44281f05b828f1cb7424a7081f05782cf1cb7428a7081f052820f1dca70b7423946ead301f04bbab9f1020f37dcb9f1010f00f01c81b9f1020f42f0b48138983aae33903998dde9367b3490ddf8800572cec069dde93e324ff000094290c64528a8cde946e8cdf82091cdf80c91cdf8c8b02d922b9362c02f94359700f020820ef1010046909ef80090404500f018820ef1020046909ef80100060213e2b9f1030f00f04381b9f1040f42f07c81dde93a70dde936c232903c98dde938543090ddf88005dde93d3addf8fc90c0694ff0000bc64542aecde942e8cdf810b14090cdf804b13592cde9334c319300f04f820ef1010042909ef800b0404500f047820ef1020042909ef801004fea002842e2b9f1050f00f09f81b9f1060f42f04381c64502f040810ef10100404502f03b810ef10200404502f036810ef10300404502f031810ef10400404502f02c810ef10500404502f027810ef10600404502f022810ef10700404502f01d810ef1080ca8eb0c001028c2f016810ef11800404542f011819ef804009ef8051050ea01261ebf9ef806009ef8071050ea012502f002819ef800009ef80120379940ea0224081bc2f0f880864202f2f5809ef802009ef80330389a40ea0327d01bc2f0eb80854202f2e880ddf87c05002801f0368239980df1e80e369b34909ee8014fcdf8d4e0cde906478de8010f1846dde93437cdf828c0cde90865059704f06ff801f01dba36983999339037983591349038983a9931904046cde94ee0ddf880053291c06900214eac46aedde93b7bdde93da5ddf8fc805091cde946012046314602f06ffb81462046314602f06afb5099002900f07a824ff0ff33279f1c464ee2c64502f09980724612f8010b012802f29380369900283091dde93751359139993191dde93ac134913c992d913d992b913e9929913f99289100f02f83424502f07b800ef10202424502f076800ef10302424502f071800ef10402424502f06c800ef10502424502f067800ef10602424502f062800ef10702424502f05d800ef10802424502f058809ef801209ef802309ef8046042ea03239ef803209ef805409ef80610774642ea062e44ea012af9793a7a07f109041a9441ea02210ee3a8eb0e06052ec2f038800ef10509c14502f033800ef106059ef80540a8eb0500a042c2f02a802819404542f0268036981f90dde93780cdf8d4e0289039981e90dde93aa034903c9832903d983190dde93eb03390b4b1002703e000bf0137a74210d2e85d01381e28f8d9e819e11b4eaa46ab02f005ff002801f0ff874e980744a742eed3ddf87c05002801f049813498cdf8c0a0002800f00b843298444620eae07032903198d84520eae0702090a8bf5c462898ddf8cc804045a8bf40462890fae3c64501f0d9870ef10100404501f0d4870ef10200404541f0cf87704601784078dde93b5441ea0021ddf880050df1d80c0990ddf87c059ce80c10089004a8dde93976ddf8f490ddf8f8e0ddf8fc8080e810422398cde900c7cde90265cdf81c8004f0a5f8044659e1ddf880050df1f00ac069dde936c1dde93853dde93a629ae89004ddf8fc904ff0000b4290c64531a8cde946e8cdf820b1cdf80cb1cde934512d9480e80c10cde92f76cdf8ac9000f020810ef1010046909ef800b0404500f018810ef1020046909ef801004fea002813e101204890002646ad42af2846394602f02ffa83462846394602f02afa48998046002900f0198100240020319001204890002046af0df5847a30903846514602f017fa05463846514602f012fa4899002940f0f9808446dde94601279fa24688424ff0ff3440f0f280bbf1000f00f1ee80b8f1000f00f1ea80ddf880254398116a46ea0906084310623598ddf8c8e04e9033985b4650904ff6fe7086423498cdf83ce1519000f04c844ff6ff70864240f028842398d0f888934ff420704f904ff4f07050904ff4a070519000f03bbc012044904ff0000840ac3046214602f0c8f9314606460846214602f0c2f94499002940f0a980dde94212914200f0a48011f801eb914240f09f80ddf8803541991a6a1143239a1962002a00f09580d2f874114ff0ff34002900f0908048ea0b030022b2eb532f40f0898051f82320002a00f08480a2eb01085fea88727ed16ff4a122a4460f244244cff6fb74a242644674d301eb080c5cf8081c99426cd15cf80c1ca1f1011212f5803f65d3bcf1000f4ff0ff3462d05cf8042c8a425ed1c8f5a12291425ad82d90ddf880052f938069cdf8ace058b12f9a0bf0070ed30810f80380012202fa0ef242ea0802c2546046002246ab01f0ebfe00284ff0000408bf4ff0ff343ad0ddf87c05b8b3a6f1011010f1021f4ff0000430d92d98a0f1011010f1021f2ad93298002300284e9301f00a83309820eae072319820eae07335985045a8bf504635904d45a8bf4d4601f0fbba012048904ff0000846af42ac3846214602f01ef981463846214602f019f94899002900f080804ff0ff34279f229a002c00f09e8701f047bedde9460188423ff4e2ae024612f8014b8a4246923ff4dcae821c469243788a4244ea03243ff4d4aec21c469283788a4231933ff4cfae011d4691c0780002cce6dde94ec2cdf8c08094453ff47fad0cf1010191423ff47aad0cf1020191423ff475ad0cf1030191423ff470ad0cf1040191423ff46bad0cf1050191423ff466ad0cf1060191423ff461ad0cf1070191423ff45cad2d909cf800009cf8041056460cf1080abe465c46cdf8a8902b909cf801709cf802309cf8030029919cf805909cf806b09cf80710a2eb0a08b8f1010f2f9140f0a78692453ff438ad9cf808100f293ff633ad0cf1090a00f09cbedde946218a423ff47baf134613f801cb8b423ff475af951c56784a1bb2424ff0ff34fff46fafaa198a427ff46bafddf8803543991a6a1143239a1962002a3ff461afd2f87421002a3ff45caf48ea0b0e0021b1eb5e2f7ff455af52f82e1000293ff450af891a8b077ff44caf6ff4a1230f270b44cff6fb77bb42fff443af531853f8082c72457ff43daf53f80c7ca7f1011212f5803ffff435af28aa002b82e86040cdf884c026903ff42caf53f8040cb8427ff427afc1f5a1202a9987423ff621afddf880051e4680693d4640b1ca080bf0070c835c012101fa0cf119438154c12dfff40faf307801287ff40baf2998002801f0bf81dde928b8a6f13f0400f0b6bf424501f04c850ef10202424501f047850ef10302424501f042850ef10402424501f03d859ef801209ef80260714693004fea460eca780e7905314fea820a1a917100baf1000f1b9101f029851b99002901f02585ea1ac1f02285924501f21f853599b1eb0e02c1f01a851b99914201f21685349903eb0c06084301d000230be05feacc704ff0000306d1002e04d40aeb0600a842d8bf01231a9c1b9aa8eb04001890ddf87c0502fb0af9b0fa80f0400918430027c0074ff0000b19931f902f95cdf8709023e0dde9303002fb003c4a081cf80230339803f00f0803f0f0030344c907329808bf48ea0003ddf8a8800cf8023000bf26988344dde9200181424ff0000088bf01200743f80708bf0c44f90704d0d94507d801f0c2bc00bf444518bfd94540f29e81b8eb040001f0b88421780b0901d0012211e0012801f0b08463780bb102220ae00428c1f0a984a278e37852ea032301f0a384042200bf2192a9eb0b02934201f29b8420901f9826930028c0d101f00f003390269809010128cde91d4732912dd14ff0000c2698ddf8a8801e9fdde91c94c007acd00ceb0b00b0fbfaf101fb1a0001eb0e02811952ea0100a0d4a9429ed2359882429bd2349800283ff481af2d98814294db2b98824291db29988142bff68eaf28988242fff673af88e720f001014ff0000c16e0dde93030339f04fb00305308c45cd20704f00f0504f0f0043c44329a08bf45ea02042f9dc4540cf1020c6145b7d00beb0c02b2fbfaf404fb1a2704eb0e0907eb060859ea080427d4a84525d23598814522d2349858b12d9880451ddb2b9881451adb2998804517da2898814514dadde930304fea580709fb00343398e35d03f00f0503f0f00303445feac870329808bf45ea00032f9de355501cb0fbfaf202fb1a0002eb0e04821954ea0200bbd4aa42b9d235988442b6d234980028a0d02d988242b0db2b988442addb29988242aada2898844294dba6e70aeb0a1109fb02f2f6f730fe00f038bd0020209032904446052e01f0de831f98002801f0da83faf787f9002801f0d58399f8009009f10600864241f0ce8347f65420c2f207000768002f01f0c683387b797bba7bfb7b40ea012040ea024040ea0363a3f1810010f1800fc1f0b683387c797cfa7c40ea0120b97c40ea014040ea02601090813810f5817f41f2a683b9f1000f31931897349477d0359a002300270ae000bf00f100404ea941f8260001374f4506f1010316d2e85d1e46411e1e29f0d9e819a9eb070146aa42ab02f088fa00283ff432ad429846994eaa42f82600359a0f44e4e7002b00f0ed80a3f1010a77424ff000094eac0ce0359a5b46ca4509f10109a6f1010604f1040407f1010700f0d7804ea850f829100029efd49b46ca4513d10022189846ab42ad009502f0e3fa00283ff401ad42980028ddd04af20d3146a8c0f24e018847d6e7a6f1010c002304eb83004268002ae4d59c45e1d08268b2f1ff3fdedcfd18aa1cdad0c268b2f1ff3fd7dcea1cd4d00269b2f1ff3fd1dc04339e42e4d1cce7359a012000239be000234ff0010abe46fff7b7b8444508bfd94541f01983ddf87c05002818bf012019990840012840f06284dde9300318990efb03004fea5a0700eb56021b98009701901a9801f0cffb002840f0508401f0fbba00234ff0010a8646fff78eb82f4600f040bc239831464eaacdf898c0744601f0f9fa002857d1ddf88005ddf898c08069a6465b4660b10021b1eb562f08d1f20809f00701845c012606fa01f121438154ddf83891b9f1000f3dd0baf1000f4ff0ff341cbfdde9301050ea01003ff476ac4f999942fff472acc91a8a453ff66eac50994145fff46aaca1eb080188423ff665acddf87c15002400293ff45faca5f1011111f1021f7ff659ac2f99002900f02585299921eae1722a9921eae1762b998e45a8bf8e4633992d9ca142a8bf2146339100f015bd4ff0ff34fff73fbc002001e00023012092f800c0144692f801e091781279e77895060f2402f00f0604eae575754389246543ed0a8df808510e25339212f0200f08bf012506fb05f504fb05f54fead5258df809514ff00d0508bf022506fb05f504fb05f54fead5258df80a514ff00c0508bf032506fb05f504fb05f54fead5258df80b514ff00b0508bf042506fb05f504fb05f54fead5258df80c514ff00a0508bf052506fb05f504fb05f54fead5258df80d514ff0090508bf062506fb05f504fb05f54fead5258df80e514ff0080508bf072506fb05f504fb05f54fead5258df80f514ff0070508bf082506fb05f504fb05f54fead5258df810514ff0060508bf092506fb05f504fb05f54fead5258df811514ff0050508bf0a2506fb05f504fb05f54fead5258df812514ff0040508bf0b2506fb05f504fb05f54fead5258df813514ff0030508bf0c2506fb05f504fb05f54fead5258df814514ff0020508bf0d2506fb05f504fb05f54fead5258df815514ff0010508bf0e2506fb05f504fb05f54fead5258df816514ff0000508bf0f256e436643f60a00284ff000048df817617ff46eab38064fea0e6240ea014042ea0c41309a349c02eb2149591e13913199ddf860a001eb2040c3f1000b4ff000084ead1a460f9012930ce001f01f000b3808f101088144013a043598450bf1010b00f028864ea850f82810b1f1ff3fecdd13981992804540f0dc810022504646ab40af1a95009702f010f900283ff42eab409900291c9100f0f2819df820019df82111ddf880c050ea0120cde9148bcdf85c902d9000f0ad819df824719df825613f0447ea0667109e2d9d9df81f019df81e112e440f9d4fea002e41ea00229df8220106eb27479df82331ed1b4ff0010800042b95179d219208eb520240ea03601b920eeb0102031405eb2046571e1d97ef1801f00300e91a731c1693b31c11931c9b2690101addf8c8b03190c8070df5847a5d1c08eb52034ff000093090f80735962a932f9008e000bf2d9809f1010981452a98054400f05c812b9809eb00026245f2db28988242efda21980028ecd0dde91e10002702fb010e1d982995032880f0a5802698ddf880c0299d0028dcd0dde91b10012209fb01083398790818f8011000f0100082ea1013c8b201f00f02fe0708bf02091046002a18bf0120184301281dd135983d185d4519dba54217da68081ef800101af802209b46334601f00f0601f0f0011143ed0708bf46ea02111e465b46ddf8c8b00df5847a0ef800102698299d0128a0d0781c400818f80000002ec1b24fea111c08bf00f00f0c5ffa8cf0002818bf0120184301281ed116983a185a451adba24218da50081ef8001042acb34614f80c6001f00f0501f0f0013143349cd20708bf45ea0611299d5e46ddf8c8b00df5847a0ef800102698ddf880c002283ff469afb81c400818f80000002e00f00f02c1b208bf0a091046002a18bf0120184301287ff457af11983b185b45fff652afa342bff64faf58081ef800101af8022001f00f0701f0f0011143db0708bf47ea02110ef800103de7ac4606e031980437b8420cf1020c3ff452af33981cf8012c00f01001012080ea1113d5b200210f2d88bf012135981943012900eb07021ad15a4518dba24216da290955081ef805401af8011004f00f0004f0f004359e0c43f607ddf8c8b008bf40ea01140ef80540349c0df5847a1cf8010c10f00f08404618bf01201843012819d1551c5d4516dba54214da68081ef800101af8084001f00f05309e45ea041501f0f001002eddf8c8b008bf41ea0405349c0ef800509cf80000c5b200200f2d88bf01201843012817d1911c594514dba14212da490828091ef801401af8000004f00f0504f0f0042f9e0443002e08bf45ea00140ef80140349c9cf8000010f00f05284618bf0120184301287ff47aafd11c5945fff676afa142bff673af48081ef800101af8052001f00f03309d43ea021301f0f001002d08bf41ea02030ef800305fe74af20d3146a8c0f24e0188479df81c019df81d11129bdde9179adde9148b2ae0501e002305eb83077a68002a7ff51dae0beb0306b21c3ff417aeba68b2f1ff3f3ff713aef21c3ff40faefa68b2f1ff3f3ff70bae321d3ff407ae3a69b2f1ff3f3ff703ae04339842dcd1fde59df81c019df81d11129b40ea0120dde91925dee500274ff0010a8646fef711bd0f4600e04f1cd90708d03199994205da3978339c01f00f0121433970b8f1010f05f1010700f0b1809b456fda07fb09045feacb7171465e4609d014f80e10349e01f0f00141ea0a0104f80e10329961452144319614da04eb0c060c4604f8012bb44247d28f1cb7424a7044d2cf1cb7428a7040d20f1dca70b7423946edd33ae00f4638e0102192457ef490ae2b9a984652ea072c4ff0ff333ef48aae58ea002228923ef485aebcf5207f3ef681ae289ab2f5f07f3ef67caebbf10f0f2691cdf880b03ef675ae2f9910293ef671aeddf88035479a196a1143ddf87c25196232b132992a9a614449428a4280f2d0810023fef75ebe4f1cd90708d03199994205da3978339c01f00f0121433970b8f1020f05f1020739d09b4536da07fb09045feacb717146d8460ad014f80e10ddf8d08001f0f00141ea0a0104f80e1032996145214414da04eb0c060c4604f8012bb4420fd28f1cb7424a700cd2cf1cb7428a7008d20f1dca70b7423946edd302e00f4600e04f1cd90707d0984505da3978339c01f00f0121433970ef1c3599691a11f1040f11d9229a279f2498013787422c982e9900eb01067ef436ab00f0a6be00bf359904378f42edd09b4539da07fb09045feacb7171465d4609d014f80e10349d01f0f00141ea0a0104f80e1032996145214418da04eb0c060c4604f8012bb44280f0cd8001f10208b0454a700cd201f10308b0458a7007d201f10408ca70b0454146e9d300e08846d90709d09d4207da98f80010339c01f00f01214388f800109b4536da791c01fb09015feacb7474465d4609d011f80e40349d04f0f00444ea0a0401f80e40329c644501eb040615da01eb0c04314601f8012ba14280f09580b11ca14272700ad2f11ca142b27006d2311df270a1420e46ecd300e03146dc0707d09d4205da0c78339d04f00f042c430c709b4536dab91c01fb09015feacb7474465d4609d011f80e40349d04f0f00444ea0a0401f80e40329c644501eb040615da01eb0c0400bf314601f8012ba14260d2b11ca14272700ad2f11ca142b27006d2311df270a1420e46edd300e03146dc0707d09d4205da0c78339d04f00f042c430c709b45bff64baff91c01fb09015feacb7474465d4609d011f80e40349d04f0f00444ea0a0401f80e40329c644501eb040615da01eb0c0400bf314601f8012ba1422ad2b11ca14272700ad2f11ca142b27006d2311df270a1420e46edd300e03146dc073ff41baf9d42bff618af0c78339d04f00f042c430c7010e701f10108d9077ff442af4ae7711cdc077ff478af7ee7711cdc07acd1b3e7711cdc07e3d1fde6b8f1010800f003821bf8010b411e1f29f6d341b22029fef640af04eb400114f81000497840ea0122c12afef436af304629464eab00f0dcfd0028e1d1fef72dbf00260022724545da33998e4242da289900240d447545bef623af33998c45bef61fafa2eb0a018d427ef71aaf311a8c457ef716af571b1146b8bf2946aa4262464ff0000cd8bf6746c34607eb0308aaeb0704b71ad8bf6746c51bb242aeeb0100c8bf1646844238bf2046339b92469b1b9d422f9328bf1d46359b2d95994507eb0b03cde92a363fd12a9b9e422ddd4ff0010c39e00024fef7e3be3499329a0023891a2a9a8a42bef689ac28992d9a49428a42fef683ac2d9a31998a42bef67eacbef1000f00f0c58124eae4712191349926eae67aa942a8bf29463491dde93041a142a8bf21463191b7e1dde92a734145a3eb0703b3fa83f34fea53134ff00007c8bf012703ea070c2d9b002b00f08981042222ea88071a9708eb000743184fea5805013f01f0010603f001041b957d08199522ea8705371918952546c21bdde930477018274408eb060488ea01084908179159082b9b169107eb0a01ddf8bce0c91a4fea520b714538bf8e460121cdf8bce0bbf1010f88bf510801f00303159343082693359b21f0030103eb5000431c033814904feac87021904ff0ff3000eb52001291610831906ff00100289109eb540100eb52003090881eddf844e113906ff0020000eb52004ff00008109311911c922090cdf87cc02995cde91de608e02f9808f10108279f80454ff000043ef437ae6fea08014046bcf1000f2d9a18bf50182a9900eb010202fb0e910eb9002305e01b9b1a9ccb5ce34003f00f03359f33930db9002304e0199b189ccb5ce3401b0132932b9b349c034403fb0475219f2698002f289faa46284439441dd15a46f5f714fedde91de6ddf87cc046b11799339a1af8010000f0f00010440af80100299d002db4d01699329a1af8010000f00f0010430af80100aae75c43bcf1000f02fb0ef23ed01c980228dfd31398149910442144002200eb0b039c785f78319dc4f3031444ea071701eb0b049542a770ccd05f7810f80b50c7f3031747ea0517309d67709542c1d010f80b7013f8015cc7f3031747ea0517209d01f80b709542b4d013f8017c13f8023cc7f30317043247ea031304389345a1f1040104f8013cc9d1a3e71c9b022ba0d31c9b082b01d200222be0119bddf848e003eb020c109a144400220ceb02077d781cf80260c5f3031545ea0616a51805f8016cbe787b78c6f3031646ea0313a354fb78be78c3f3031343ea06136b703b79ff780432c3f3031343ea07139645ab70dbd1159bddf87cc0dde91de6002b3ff468af531ccf5c8d5c159cc7f3031747ea0517012c87543ff45caf941c0f5dcd5cc7f3031747ea0517159dc754022d3ff450af0a44d278095dc2f3031242ea0111015546e7ddf87c0530b3a9f1011010f1021f4ff000047ef63dad2698a0f1011010f1021f7ef636ad3198002300284e9300f062832d9820eae0722f9820eae07335985045a8bf5046359034982b998842a8bf0846349050e30024fef71bbd00224ea806902b98f11905902d98359fcde903102f980097cde90150dde9331001f088fb0446fef706bd00214ff0000a219129994ff0000e41ea0921b1eb5c0f28bf4fea5c01289ab1eb520f28bf51081c91013938bf714610912d99baeb0104d8bf7446319e711a9142a8bf11468c421f91bef68faa4fea002e32982a993b020f181c9921984a00494389001e921b918742c8bf3846349d0ceb0702aa424ff000012a92b8bf1546bcf1030f38bf0121289e0022032e38bf012211431a91410819912b990eeb080219444b08b3eb520f269b1d9000f1010c4fea031015904fea520243ea031038bf4a08149029984fea09294844691029951891824228bf0246109b03fb03f080000d906ff0080000eb420016902d985045c8bf8246339835992b9701fb0a002f99309000203290701e1790b01e139058001290781c119008010f9041ea011026460e904fea5c004ff0000acdf830c00b9010e001780f9a01f00f0111430170359c319e30980136204430901f9800238642bef60caa2899f04308448642b8bf30461e996fea400008440128319638db1b994ff0ff3200fb10101c99a1eb0a01012902eb41013bdb01fb01f2824237dd1699a1eb4a0202f10803032b02f1060126db01fb01f7874222dd052b02f1040121db01fb01f787421ddd072b02f102011cdb01fb01f7874218dd0af1040a092ba2f1080103db524382420a46dbdc08310ee02d983399359c269a4ff0000a39e00af1010a04e00af1020a01e00af1030abaf1010f29db08318a1f5243824224dcbaf1020f16db0a1f5243824215dcbaf1020f0fd08a1e5243824211dcbaf1040f08db01fb01f282420ddcbaf1040a01f10801e1d14ff0000a07e0aaf1010a04e0aaf1020a01e0aaf1030a2d983399359c269a319e30442b9b00fb04192a985344102aa0eb0a0800f08b8034988342b8bf18461d99814239da1d9f199af90739460ad0199f269a19f8071001f0f001114309f80710dde90b21b2eb600f09eb020715da149d09eb60063a4602f8015bb2420fd2ba1cb2427d700cd2fa1cb242bd7008d23a1dfd70b2421746edd302e03a4600e07a1c359c319ec70707d0814205da1078159900f00f0008431070ddf884c0e045c8bfc4462998844541da5feacc700bd04fea5c0019f80010269a01f0f00111430cf1010c09f800101898b0eb5c0f1add1898309e149d4fea5c0209eb0007b018044604f8015bbc4210d2811cb94225700fd2c41cbc420d700ad20436b118b9422570ecd305e009eb5c0102e0411c00e021462998359c319ec00708d02998844505da0878159a00f00f00104308702f9810280bd10120029020982199349a01904846cdf8008002f02dfbcae6b6fa86f01a99400908434ad11798864247d01399701e891b8842b8bf01466fea4100129908440128c0f29f800d99329a00fb101010998a1a4ff0ff31012a01eb42016ddb01fb01f7874269dd5100a1f1090202f10807032f02f1060157db01fb01f6864253dd052f02f1040152db01fb01f686424edd072f02f102014ddb01fb01f6864249dd3299092f01f104013291a2f1080103db524382420a46d9dc08313ee021988342d8bf034634988045a8bf80464345bff66daed80709d0580819f800102f9a01f0f0011143013309f800105908b1eb680f17da309f0e9d09eb68027c18264606f8015b96427ad2a01c904235707bd2e61c9642057076d20437781890423570ecd371e009eb53006ee0329a013204e0329a023201e0329a03323292329a012a23db08318a1f524382421edc329a022a19db0a1f5243824200f3ba80329a022a11d08a1e5243824200f3b580329a042a09db01fb01f2824200f3b080329a0831043a3292ded1002032901198329900eb010c21988342d8bf0346ddf8d0e0f445b8bfe646734543dad80709d0580819f800102f9a01f0f0011143013309f800105a08b2eb6e0f17da309e0e9d09eb6e07b018044604f8015bbc4212d2811cb94225701ad2c41cbc420d7015d20436b118b9422570ecd310e009eb53010de0601c02e0411c09e030465feac8713ff4cfad4345fff6c6adcae521465feace7007d0734505da08780f9a00f00f001043087032982a99c0430e18012002902098219f349c01904846394622466346009602f011fabe42d8bf3e46a045a8bfa0464645bff6a5adf00709d0700819f800102f9a01f0f0011143013609f800107108b1eb680f17da309b0e9d09eb68025c18274607f8015b974210d2a01c90423d700fd2e71c974205700ad20433581890423d70ecd305e009eb560002e0601c00e038465feac8713ff473ad4645fff66aad6ee53298013858e73298023855e73298033852e700223098359f09eb00014ea8089029980097079028980690219805902698cde903102a98029034980190dde9321001f076fd0446fef7b2b94ff0ff300df2345dbde8f08fb01a18bf4ff0ff300df2345dbde8f08f00bfb0b586b000b3d0f87401e8b10023b3eb512f19d150f82130b3b11b1a9d0713d16ff4a1250f241d44cff6fb74a5420bd3184450f8085c8d4206d150f80c1ca1f1011515f5803f03d24ff0ff3006b0b0bd50f8045c8d42f7d1c3f5a1239942f3d80028f1d0037813f00b0fedd1144601ab0022002500f010f80028e5d00198dde903122060481c40086160a260e06028462561656106b0b0bd91429cbf002070472de9f041855c4ff0050c6e0758bf4ff0030c15f0f30f02d1891a614502d20020bde8f0818418627894f802e0002e05d5e078267942ea0e2240ea062e002a4ff0000018bfbef1000fead0b2f5207fe7d8bef5f07f88bfbde8f081082d38bf0120072d18610ad8501c400800fb0ef0a1eb0c018842d3d804eb0c0828e004eb0c080efb02f5002000bf00eb0c04a142c6d008eb00073e78360903d00127b54213d2bde70c1b012cbad07e781eb10227b5420ad2b4e7042cb2d3bc78fe7854ea0626add00427b542aad3ad1b3844dcd158600120c3f800809a60c3f80ce0bde8f0812de9f04f89b0dde91257069005fb07f000294fea4007029100f0d48100200390089002984ff0010e00264ff0000b4ff000084ff0000c4ff000090497079205930be000bf4ff0000c4ff00308cdf80ca009f10109814500f0ba81bcf1000f40f0b4810699b8f1020f11f809a0ead0b8f1010f15d0ddf81cc0b8f1000f40f086805fea1a100af00f01089100f092800499814280f09a804ff0010c4ff0000882e1079abaf1000f00f07f800499514576d3a1eb0a010491089901eb01100901019114e000bfa15d089f01f0f0013943a1550136aaf1010a4ff0010eae4224bf00260bf1010bbaf1000f00f0598113998b45d1d20bfb0324bef1000fe3d0a91bb1eb5a0f88bf4fea5a0141b38d0804eb060817d0002208f10107a2eb850c6ff003057b1904350ceb05020432d870187158719871f5d1059b079a2f1d11f0030504d10ce0002711f0030508d0012d08f8070004d04744022d787018bfb870aaeb410abaf1000f0e440dd0129dae42b5d2a15d019f01f00f013943aaf1010a4ff0000ea155afe7129d4ff0000aa6e7039a52ea0a2213d004990392914280f08f804ff0010c4ff0000853e74ff0000c4ff002084ee74ff0000c4ff00108f4e000214ff0010c4ff00008039142e7089a091a049102eb02111201cdf80090019214e000bf1af80670089a07f0f00717440af80670013601384ff0010eae4224bf00260bf1010b002800f0d680139a934580f0cd800bfb03cabef1000fe2d0ad1bb5eb500ff14688bf45087db3ac080aeb06081ed0002308f10107a3eb840c6ff0030400bf07eb040e04340ceb040304338ef803108ef804108ef805108ef80610f0d1059bddf81cc0271d15f0030404d10ce0002715f0030408d0012c08f8071004d04744022c797018bfb970a0eb45002e4488b1129dae4210d21af80620019f02f00f023a430af8062001384ff0000e0028abd180e000bf129d0020ce469de7891a0491089901eb011009010191114613e000bf1af80620089c02f0f00222430af80620013601394ff0010eae4224bf00260bf1010b002953d0139a9345bff4ccae0bfb03cabef1000fe3d0ad1bb5eb510f88bf4d085db3ac080aeb060819d0002208f10107a2eb840c6ff0030400bf3a1904340ceb04030433d070107150719071f5d1059bddf81cc0271d15f0030404d10de0002715f0030409d0012c08f8070005d04744022c787018bfb87000bfa1eb45012e4489b1129dae42b6d21af80620019f02f00f023a4301394ff0000e0af806200029b0d103e000bf129d0021a4e74ff000084ff0000c029852e64ff0010c4ff0000803e04ff000084ff0000c0298ddf8009045e60120012107e04ff0010cb8fa88f0049f8cf001014009b7fa87f252091140084009b0bde8f08f00bf2de9f041826882b10121816007220020511e20fa01f1c90708bfbde8f0814ff0ff3191400843bde8f0818c46056841688d42e9d06b1c03602c78ff2c11d18b4200f0dc80ac1c046095f90120b2f1ff3fd6b200f3d080bf2e22d806f03f0602238fe062b2b2f1ff3f02dd07222046cfe7bf2c09d804f03f070e224ff0010e4ff000084ff0000c2ee0df2c0fd804f01f0715224ff0000c4ff001084ff0010e22e0df2e0fd806f01f0603236ae0ef2c0fd804f00f071c224ff0010c4ff000084ff0010e10e0ef2e58d806f00f06042358e0f02c40f093804ff0000820224ff0000c4ff0000e00278b424fea072603d0af1c07606d7803e00125856000251f46c02c45ea06046cd35b1a7d1ab3fa83f3b5fa85f55b096d092b434fea042403d001258560002503e07e1c06603d783746b8f1000f44ea050453d17d1ab5fa85f56d092b434fea042403d001258560002503e07e1c06603d783746bcf1000f44ea05043ed1791ab1fa81f1490919434fea042131d001238360002030e0f02e3ed1002605234ff0010e00250127ea074fea062617d18c4215d0621c0260247801379f4246ea040623d000bf8a424fea062609d0541c0460127801371643bb422246f3d116e02246013701254ff0000e9f4214468560dad112e07b1c0360387840ea0104bef1000f20467ff413afbde8f08114465feace7202d0091b8e4204d9012181600020bde8f081a11907462046002263463960bde8f041ffe78446081ab0f5806f01d90020704761454ff00000fad0002a18bf70472de9f04fabb09fed251a9fed250a9fed253a9e4600264ff0ff380df12c090df10c0ab7ee002ab0ee084ab6ee005ab1ee006ab9ee007a00206746019009e000bf49f826500af8064001368f42bc4600f0cb8217f8012b501e01280fd8012a23d18f4274d09cf901000cf10207b0f1ff3fc5b240f38f8000220724f5e1052a37d0042a30d0032a49d1002e00f0d982701e59f820501af8004093e200bf0000004f000000cf000000008f4254d00cf102079cf801008f4247d09cf802200cf103078f4240ea02203fd09cf803200cf104078f4240ea024037d09cf804200cf10507130600223ae0002e00f0aa82013eace7022ec0f0a582b21e59f82230701e59f8205049f820301af802301af800400af8003049f822500af8024096e702f0f80010281fd1002e00f08b82701e1af80030002b40f0858259f82050152a6fd16d420022002406463de20023012206e001220724002588e101220020002343ea000501242fe2a2f12000062827d8002e00f06782731e1af80300002800f0618259f82300242a40f0858080f00045012400221e4617e2bf2d5ad801220292002205f03f000e244ff0000b00928f4200f019819cf802300cf10308002215e1322a51d002f0fe0030284dd0402a40f0a380032ec0f03682701e1af80020002a00f03082b21e1af80230002b00f02a82033e1af80630002b00f0248209eb8202d2ed000a09eb860209eb8000d2ed001ad0ed002a70eee10a62eea00a71eea00acee1002800f00e82023e1af80600002840f0088259f82600122a58dc102a00f03081112a00f03381122a40f02c81454354e1df2d44d8002205f01f00152400924ff0010b0122bae0002e00f0eb81a2f13100013eb0fa80f01af806304009834240f0e08159f82600302a7bd100ee900af8eee00a94e1002b00f0d481023e1af80630002b00f0ce8159f82630212a40f3ed80222a00f02581232a00f02981252a40f03a8100ee903a01ee900af4ee610af1ee10fa48bff0ee601a11ee905a71e1ef2d72d805f00f001c24012274e0a2f11303022b80f0c480002d00f0a381b0f1004f08bf15f1010300f0eb80132a40f0f18090fbf5f5f1e0a2f1480010f1070f4ff00000c0f05f81002e00f05c81013e1af80600002800f0858109eb8600d0ed000af4ee420af1ee10faf5ee400ac8bff0ee420af1ee10fa48bff0ee430a432a72ee601a00f3b480412a00f0f380422a00f00f81432a40f0008161eea10a72ee600a1de1312a40f0948000ee900a20f00040b0f1ff4f4ff00002f4ee410aa8bf0122f1ee10fa4ff0000080f21c81f4ee400af1ee10fa00f11681002a40f01381fdeee00a10ee905a98e0f02d4ff0000040f00981002220244ff0000b009202928f427ff4e7ae00230122b8460002c02d43ea000535d3781aa8eb0103b0fa80f0b3fa83f340095b0918434fea05270ebf18f8013b00230122bbf1000f43ea07051fd1a8eb0103b3fa83f35b0918430ebf18f8013b002301222f02009d002d43ea07050ed1a8eb0103b3fa83f35b0918434fea05230ebf18f8010b0020012240ea0305474602984ff0ff3838b1601e25fa00f0c0071cbf08fa04f005430024a3e0162a10d1a842b8bf054637e0202a4cd0212a51d100ee903a01ee900a70eee10a8ee005442ae0a842c8bf054626e0451b24e0002800f1c680def800200546824238bf15469042019888bf0120019015e0442a48d0452a5fd0462a4ed161eea10a61e00025132a4ff0000208bf4ff00045002469e090fbf5f202fb15050022002462e000ee903a01ee900a61eea00a57e000ee900af5ee400af1ee10fa00f0928001ee903ac1eea00a4ae000ee903a01ee900a71eea00a43e000ee903a01ee900af4ee610af1ee10fac8bff0ee601a11ee905a37e060eea01a70eea00a74ee600a61eea00a2ce0f4ee450af1ee10fa18d570eea01a60eea10a22e0f4ee450af1ee10fa15d560ee861a60eea11a60eea10a16e060eea00a13e060eea01a60eea10a0ee071eea10a61eea00a72ee600a07e061ee870a61eea00a61eea00a70ee820a10ee905a012400224ff000005ab9202e09d0002c3ff436ad25f00040b0f1ff4ffff630ad00202bb0bde8f08f56b3701e1af8001059f82000d9b102ee100a20f00040b0f1ff4f4ff00001b4ee412aa8bf0121f1ee10fa4ff00000e4dab4ee402af1ee10fadfd40029ddd1bdeec20a10ee100a019adef804101143cef804102bb0bde8f08f00202bb0bde8f08f80b5002970d090f900c0bcf1200f5ffa8cfc07db012010604ff0010ec3f800c0704680bdacf1c20ebef11d0f0fd801295ad090f9011011f1410f55dc02211160407800f03f006cf38a108446e4e70cf0f00ebef1e00f23d1032945d390f901e01ef1410f40dc90f9021011f1410f3bdcbcf1e00f5ffa8ef101d1a02934d3bcf1ed0f01d19f292fd80321116042784fea0c3189b2807802f03f0241ea821136e004294ff0000ebbd3acf1f50111f1050fb6d390f901e01ef1410f15dc90f9021011f1410f10dc90f9031011f1410f0bdcbcf1f00f5ffa8ef101d1902904d3bcf1f40f03d18f2901d9002080bd042111604ff4e011427801ea8c4c817802f03f02c0784cea023201f03f0142ea811100f03f0001eb000c7fe72de9f04188b04af2bb261c46c0f24e0613460a46ddf83880002706f1ae05214627706770a770e77027716771a771e77127726772a772e77227736773a773e77327746774a774e77427756775a775e77527766776a776e77627776777a777e777a8470028c8f800705cd020786178a278e3780843104318434ff0010008d0e179a27911431ebf617a227a51ea020102d108b0bde8f081a07b14284ff00000f7d1217d8df8040041f0010121758df805008df806008df807008df808008df809008df80a008df80b008df80c008df80d008df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f0001a92046b047c8f8000018b1012008b0bde8f08106f1520120468847d8f80000002818bf012008b0bde8f081002804bf4ff0ff3070472de9f04f95b00df1200c8ce80f00f7f770fd002800f0aa814ef66d6a4ff29c5bccf2ff0ac2f2290b4ff6ff764ff00008012447f67f754ff07f0907e000bf48f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf80000bff35f8f01466ff39601b1f1005f06d10168514503d14168b1f57e7f47d04ff47e70f6f74afc002800f074816ff0070100bf42180831b1f57c7f82f8088082f8098082f80a8082f80b8082f80c8082f80d8082f80e8082f80f80ead14ff47e71c0f800a041608473a0f8c85180f8c791068446848684c684068546858685c685068646868686c686068746878687c6875be8001fb942a6d1bff35f8f00bf4be8000121b15be8001fb942f8d09be7d0f87411209e4ff0ff3021b30022b2eb562f20d151f82600002800f02481431a98074ff0ff3016d16ff4a1220f271a44cff6fb77ba420ed3ca1852f8080cb04240f0118152f80c1ca1f1011010f5803f04d24ff0ff3015b0bde8f08f002a4ff0ff30f8d052f8047c8f42f4d1c3f5a1239942f0d810ac1046002223464ff00009fef760ff002800f0ee80239b0f22990603f00f0002eae17101fb00f289214a43d20a8df830200e2213f0200f08bf4ff00f0908bf012200fb02f201fb02f24fead2228df831204ff00d0208bf022200fb02f201fb02f24fead2228df832204ff00c0208bf032200fb02f201fb02f24fead2228df833204ff00b0208bf042200fb02f201fb02f24fead2228df834204ff00a0208bf052200fb02f201fb02f24fead2228df835204ff0090208bf062200fb02f201fb02f24fead2228df836204ff0080208bf072200fb02f201fb02f24fead2228df837204ff0070208bf082200fb02f201fb02f24fead2228df838204ff0060208bf092200fb02f201fb02f24fead2228df839204ff0050208bf0a2200fb02f201fb02f24fead2228df83a204ff0040208bf0b2200fb02f201fb02f24fead2228df83b204ff0030208bf0c2200fb02f201fb02f24fead2228df83c204ff0020208bf0d2200fb02f201fb02f24fead2228df83d204ff0010208bf0e22424309fb00f04843c00a8df83f00c3f3001006900ca84a43cde9044008ac0b9dd20addf88880ddf884901f9f13cc1e9e8df83e2022462b46cdf80c80cdf808900197009600f02df8dde91201a1454844c8bf4c46a8454144c8bf4546b042b8bf0646b942b8bf0f46a642c8bfaf4208dc002015b0bde8f08f4ff0ff3015b0bde8f08f249800680f28f2d8791b249f321b07ebc0030130dc801d815a8199813860e6e72de9f04f8fb0dde91c5a0e9129691e9eddf868c0002900f05c812f68002e40f05381874200f050819af80f100f2940f04b819af80e100e2940f046819af80d100d2940f041819af80c100c2940f03c819af80b100b2940f037819af80a100a2940f032819af80910092940f02d819af80810082940f028819af80710072940f023819af80610062940f01e819af80510052940f019819af80410042940f014819af80310032940f00f819af80210022940f00a819af80110012940f005819af80010002940f000811b9fb2eb0c044ff00001d8bf0c46db1bd8bf0b46d5f808e0ea681d46e919189fa444aeeb0403a7eb0c070694bb42b8bf1f46199b521b0b91591a8a42b8bf1146012f0c910797c0f2e5820c990129c0f2e1820ef101014c08069900eb6c000a9048100990042020ea81000590079801310138cde903014810029004fb05f000f102094ff0000acde9005c089407e000bf0c980af1010a8245a14400f0bb821c980e9b01680aeb050000fb04120b980a9f504400fb03705feacc7303d1099fdde9063810e0099b2e46d35c059d0778eb4007f0f00703f00f033b4400f8013b049bdde902783546d619da074fea580b0d9208d131465a466446f3f792fea446089c68e000bfb8f1020f64d3abf10102032a04d200211bf0030e35d15be06ff0030202ea58034f44b646c3f1000c3944471c8e18f578b478c5f3031545ea0415bc18e5703579f378c5f3031545ea0313237173793579c3f3031343ea05136371b3797679c3f30313043243ea06130ceb02060436a371dcd1dde9005c089c111d76461bf0030e26d04b1cf75c3246765cc7f30317ddf804c047ea06171646bef1010f475417d08f1cd65dd55cc6f3031646ea0516009dddf804c0bef1020fc654164608d03144c978f25dc1f3031141ea0211c15500bf5feac8713ff45aaf0d9a16f80b10002a4ff0000208bf042210f80b30d14003f00f0242ea011100f80b1047e7874200f095810026a1e16d68002d00f0018200244ff000090ae000bfdde90c14d8f804500c44ddf874a0ac4280f0f281ddf870800d94d8f800100f5d3e0903d001210c910ce000bf2e1b012e00f0e28121444d78002d00f0628102210c912e4617f00f011e9f1af801a0b7fa87f74fea571718bf012147ea010e1b9f4fea0a1b16f003014c4600f08980cde90a16d8f808100026b9fbf1f404fb119161449142a8bf012606ea0e06012e18d13c449c4215db189da94212da199dac420fda0e9d4e0804fb0504c907a75d6ff00f01554604bf0f215d4639401b9f2943a1550a990b9e012909f1010458d0d8f80810b4fbf1f606fb1141002461449142a8bf012404ea0e04012c18d1f4199c4215db189da94212da199dac420fda0e9d574604fb05044d08c907665d6ff00f0104bf0f215f46314039431b9f61550a990b9e022909f102042ad0d8f80810b4fbf1f505fb1141002461449142a8bf012404ea0e04012c19d1ec199c4216db189da94213da199dac4210da0e9d574604fb05044d08c907665d6ff00f0104bf0f215f4631400b9e39431b9f615509f10304711e0329b14405d23ae700bf0434a1453ff436afd8f808100026b4fbf1f505fb114161449142a8bf012606ea0e06012e1bd13d449d4218db189eb14215da199eb54212da0e9ed04605fb06054e08c907af5d6ff00f0104bf0f21d84639401b9f41ea0801ddf87080a955d8f80810661cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e1cd13d449d4219db189eb14216da199eb54213da0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa955d8f80810a61cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e1cd13d449d4219db189eb14216da199eb54213da0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa955d8f80810e61cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e7ff466af3d449d42fff662af189eb142bff65eaf199eb542bff65aaf0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa95544e7042e78d38e78c97856ea012673d0042188e61b99002901dd012609e0b1fa81f149090027bcf1000fc8bf012707ea0106d5f808e0d5f80cb00bfb0ef9b9f1000f59d01e990024b1fa81f149094ff0ff380d91f1070c9107e000bf01340bfb0ef94c45a8f1010846d20c991c9e0029214618bf09eb0801b1fbfef536680ef1010705fb1e117f0805fb07664f08f65d042727ea8107fe4016f00f094f4618bf01270d9e3743012fd8d161449142d5db1b9f3d449d42d1db189fb942ceda199fbd42cbda0e9f1af8096005fb070e4d081ef80570c90707f00f0907f0f00747ea06071c9908bf49ea06170ef80570d1f808e0d1f80cb0b1e70fb0bde8f08f00bf002804bf4ff0ff3070472de9f04f9bb01c461546cde91001f7f72ef8002800f0a6814ef66d6b4ff29c59cde90e54ccf2ff0bc2f229094ff6ff7a0027012447f67f754ff07f0806e048f2a351c0f24501bff32f8f8847d9f80060bff35f8fd9f80000bff35f8f01466ff39601b1f1005f06d10168594503d14168b1f57e7f4fd04ff47e70f5f708ff002800f070816ff0070100bf42180831b1f57c7f177257729772d772177357739773d773f2d14ff47e71c0f800b041608473a0f8c85180f8c781a0f820a0a0f822a0a0f824a0a0f826a0a0f828a0a0f82aa0a0f82ca0a0f82ea0a0f830a0a0f832a0a0f834a0a0f836a0a0f838a0a0f83aa0a0f83ca0a0f83ea059e8001fb1429ed1bff35f8f00bf49e8000121b159e8001fb142f8d093e7d0f87421299c269d4ff0ff3022b30021b1eb552f20d152f82500002800f01781811a88074ff0ff3016d16ff4a1230f270b44cff6fb77bb420ed3561856f8080ca84240f0048156f80c7ca7f1011010f5803f04d24ff0ff301bb0bde8f08f56f8040cb8424ff0ff30f6d1c1f5a1218f42f2d8002ef0d0c12feed33078012840f0e680a0060f2204f00f0102eae070484389225043c00a8df85c000e2014f0200f08bf012001fb00f002fb00f04fead0208df85d004ff00d0008bf022001fb00f002fb00f04fead0208df85e004ff00c0008bf032001fb00f002fb00f04fead0208df85f004ff00b0008bf042001fb00f002fb00f04fead0208df860004ff00a0008bf052001fb00f002fb00f04fead0208df861004ff0090008bf062001fb00f002fb00f04fead0208df862004ff0080008bf072001fb00f002fb00f04fead0208df863004ff0070008bf082001fb00f002fb00f04fead0208df864004ff0060008bf092001fb00f002fb00f04fead0208df865004ff0050008bf0a2001fb00f002fb00f04fead0208df866004ff0040008bf0b2001fb00f002fb00f04fead0208df867004ff0030008bf0c2001fb00f002fb00f04fead0208df868004ff0020008bf0d2001fb00f002fb00f04fead0208df869004ff0010008bf0e2001fb00f002fb00f04fead0204ff000038df86a0008bf0f2359432b9d51430020c90a002d8df86b103ff428afc4f300102a9ca6f13f080df148090d9001e0013d1cd014f8010b411e1f29f8d341b220290fdb08eb400118f81000497840ea0122c12a06d3304639464b46fef733f90028e5d14ff0ff301bb0bde8f08f0f992898dde92ab58842c8bf01462c98ddf89ca006300a910890cde90b76cdf8248004e050440b38013d824657d01bf8010b1f28f6d908eb400118f81000497840ea0122c12ad6d3304639464b46fef703f90028cfd00d98ddf83880069017a8cde90490ddf8a090249cdde910010f9b259e4246cde902a900940196fff792fadde91401c24550444944d8bfc246a0422246b8bf0246b142a8bf3146524517dd0a9b994214dd2c9b1b680f2b10d80a9ea2eb0a02a1eb060c089901ebc307ba802c9a21f833a0591c7e80a7f806c01160dde90b76ddf824800df14809a4e700201bb0bde8f08f00bf2de9f04fd3b0dde95ce5dde9648cdde9629a0d9075b1dde96050dde95e47a94224eae47b27eae774a8bf29468242a8bf024602e000244ff0000b09eb0e0008eb00055845c8bf83468d420ceb0a07b8bf2946a24588bf54469742b8bf3a468b45b8bf944202db53b0bde8f08f669804f0010701974fea5b07033809970bf101070090580008974e080c90002701200df14c0e0df10c0c02948ce80e00cde90a6b06e000bf0798012700284ff00000dad0c00707906698069708bf0830ddf800906ff007086ff004044ff0000b4ff0000a0f9018bf09f10809cdf838900f994fea5a00085c102300f0f002cc46a14610901194cdf8488000bf1cf8041f0eeb08001cf8014c9cf8015042ea141704f00f041443447242ea111401f00f01847211430eeb0904217242ea15119cf802600772617205f00f011143a17242ea1611817306f00f01114308f10808083b09f10809c173d1d11098ddf83890000110224b465d4600bf13f8041f0eeb050713f8014c5e7840ea141804f00f0404437c7440ea111401f00f010143f97440ea161193f802c0397506f00f010143797540ea1c11b9750cf00f010143083a05f1080587f81080bc74f975d5d1ddf84880119c0af1020a08f120082034baf1100f0bf1200b84d101980699049a48400299059b00eb01090399dde90a6b9145bff65baf0d9803fb090807e000bf0c9809f1020991458044bff64faf0d98dc4609fb03045feacb700bd0099dddf820c0605d1ef8007000f0f00007f00f0738446055b6eb5c0f28d94fea5c0a04eb060b464616f80a0006eb0a041ef80050204600f8015b58451dd205781ef80570a51c5d45077017d228781ef800002870e51c5d4510d2287804361ef80000287006eb0a055d45ddd306e000bf04eb5c05c80705d1b4e7651cdde90a6bc807afd08c45adda28781ef8007000f00f0007f0f00738442870a3e72de9f04f8bb01d9d6c68072c00f285802cb12f89b94200f08080012c7ad100287bd0d0f87461002e77d00027b7eb512f73d156f82170002f6fd0a7eb060c5fea8c776ad16ff4a1270ceb070e0f27cff6fb77be4561d306eb0c0e5ef8086c8e425bd15ef80c6ca6f1011717f5803f54d35ef8047cb74250d1ccf5a127be424cd8bef1000f49d0032e47d39ef80070022f43d19446aa69ddf86ca0dde91987dde917b9b2b14fead10801f00704da464ff0010b4f4612f808900bfa04f444ea090402f80840d346ddf86ca0b946dde919876c6805eb44021181721e1c9e01346c600996169e0ef101010396159e0a950296149ecde904b9cde900366346cde90687cdf820a0fbf7fefa6968013969600bb0bde8f08f6f89b94204d14ff0ff300bb0bde8f08f022c3ff47aafaf89b942f4d0032c3ff474afef89b942eed0042c3ff46eaf2f8ab942e8d0052c3ff468af6f8ab942e2d0062c3ff462afaf8ab9427ff45eafdae770b5049d8b42c8bf19469542b8bf2a46914205dadde905e3bef1000f01d103b170bdbef10f0c18bf9c46cb070ed04b10c45cbcf1000f03d004f00f05ae4504d904f0f00444ea0e04c45401314c1000eb61035510bcf1000f00eb620049d0ac425fda00bf1c78764604f00f05beeb141f38bf2609754598bf754645ea06141d4605f8014b854265d25c78764604f00f05beeb141f38bf2609754598bf75469c1c45ea061584425d7040d29c78764604f00f05beeb141f38bf2609754598bf7546dc1c45ea061584429d702fd2dc78764604f00f05beeb141f38bf2609754598bf754645ea06151c1ddd7084422346b9d31ce0ac4219da4eea0e1500bf1c4604f8015b844226d29c1c84425d700ed2dc1c84429d700ad21c1ddd7084422346edd304e01c46d00704d17ae71c46d0073ff477af9142bff674af2078bcf1000f03d0beeb101f98bf70bd00f00f0040ea0e10207070bd5c1cd007ebd161e770b5044690f8a00384f8a10350b3d4f8900347f64c6642f66535c2f20706c0f2440520b131682d31c4f8981310e043f2df10c0f200007844002122460023a847c4f89003316801f12d01c4f8981330b105f1e8022d219047002808bf70bd002084f8a10370bd00bf2de9f04f99b000904ef66d644ff294504ff29c5accf2ff04c2f229004ff6ff7500274ff0010947f67f7b7f26c2f2290a0be000bf48f2a351c0f24501bff32f8f88474ff29450c2f22900d0f80880bff35f8f8068bff35f8f01466ff39601b1f1005f06d10168a14203d14168b1f57e7f3cd04ff47e70f5f7f3f9c8b36ff0070142180831b1f57c7f177257729772d772177357739773d773f2d14ff47e710460416080f80e90a0f8c8b180f8c761058445848584c584058545858585c585058645868586c586058745878587c5875ae8001f4145aed1bff35f8f00bf4ae8000121b15ae8001f4145f8d0a3e7817b11b119b0bde8f08f027f0021002a8df8041000f0cd8001aa535c002b00f0a08053185f78002f5ed09f78002f5dd0df78002f5cd01f79002f5bd05f79002f5ad09f79002f59d0df79002f58d01f7a002f57d05f7a002f56d09f7a002f55d0df7a002f54d01f7b002f53d05f7b002f52d09f7b002f51d0df7b002f50d01f7c002f4fd05f7c002f4ed09f7c002f4dd0df7c002f4cd01f7d002f4bd05f7d002f4ad09f7d002f49d0df7d002f48d01f7e002f47d05f7e002f46d09f7e002f45d0df7e002f44d01f7f002f43d05f7f002f42d09f7f002f41d0db7f002b40d0203160297ff49aaf60e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04f235d29937014d052235c29d37010d044235b2913710cd045235a29537108d052235929937104d020235829d37141f0778200218df863100121427f002a00f0c280002201ab9f5c002f00f0a0809f187e78002e5ed0be78002e5dd0fe78002e5cd03e79002e5bd07e79002e5ad0be79002e59d0fe79002e58d03e7a002e57d07e7a002e56d0be7a002e55d0fe7a002e54d03e7b002e53d07e7b002e52d0be7b002e51d0fe7b002e50d03e7c002e4fd07e7c002e4ed0be7c002e4dd0fe7c002e4cd03e7d002e4bd07e7d002e4ad0be7d002e49d0fe7d002e48d03e7e002e47d07e7e002e46d0be7e002e45d0fe7e002e44d03e7f002e43d07e7f002e42d0be7f002e41d0ff7f002f40d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff053079f5411d013444b275e2a5f700cd049275d2a9f7008d050275c2adf7004d020275b2a1f7140f0f48100228df863200131827f002a00f0be80002101aa535c002b00f0a08053185f78002f5ed09f78002f5dd0df78002f5cd01f79002f5bd05f79002f5ad09f79002f59d0df79002f58d01f7a002f57d05f7a002f56d09f7a002f55d0df7a002f54d01f7b002f53d05f7b002f52d09f7b002f51d0df7b002f50d01f7c002f4fd05f7c002f4ed09f7c002f4dd0df7c002f4cd01f7d002f4bd05f7d002f4ad09f7d002f49d0df7d002f48d01f7e002f47d05f7e002f46d09f7e002f45d0df7e002f44d01f7f002f43d05f7f002f42d09f7f002f41d0db7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00a4455235e29537008d050235d29937004d020235c29d37040f00d8100218df8631001214ff29457c2f22907fa6846f23f53c5f28803b3eb520f40f08b80d20700f08d80002101aa535c002b00f0858153185f78002f00f0f9809f78002f00f0f980df78002f00f0f9801f79002f00f0f9805f79002f00f0f9809f79002f00f0f980df79002f00f0f9801f7a002f00f0fc805f7a002f00f0fc809f7a002f00f0fc80df7a002f00f0fc801f7b002f00f0fc805f7b002f00f0fc809f7b002f00f0fc80df7b002f00f0fc801f7c002f00f0fc805f7c002f00f0fc809f7c002f00f0fc80df7c002f00f0fc801f7d002f00f0fc805f7d002f00f0fc809f7d002f00f0fc80df7d002f00f0fc801f7e002f00f0fc805f7e002f00f0fc809f7e002f00f0fc80df7e002f00f0fc801f7f002f00f00d815f7f002f00f00b819f7f002f00f00981db7f002b00f00781203160297ff47baf1de14cf67e22caf21012fa60002940f01981002101aa535c002b00f0dc8053185f78002f61d09f78002f60d0df78002f5fd01f79002f5ed05f79002f5fd09f79002f60d0df79002f61d01f7a002f62d05f7a002f63d09f7a002f64d0df7a002f68d01f7b002f69d05f7b002f6ad09f7b002f6bd0df7b002f6cd01f7c002f6dd05f7c002f6ed09f7c002f6fd0df7c002f70d01f7d002f71d05f7d002f72d09f7d002f73d0df7d002f74d01f7e002f75d05f7e002f76d09f7e002f77d0df7e002f78d01f7f002f79d05f7f002f7ad09f7f002f7bd0db7f002b7cd0203160297ff49aaface000211171f2e6013173e0023171e003316fe004316de0013184e0053169e0023180e0063165e003317ce0073161e0043178e008315de0053174e0093159e0063170e00a3155e007316ce000225a710be60b314ee0083165e00c314ae0093161e00d3146e00a315de00e3142e00b3159e00f313ee00c3155e010313ae00d3151e0113136e00e314de0123132e00f3149e013312ee0103145e014312ae0113141e0153126e012313de0163122e0133139e017311ee0143135e018311ae0153131e0193116e016312de01a3112e0173129e01b310ee0183125e01c310ae0193121e01d3106e01a311de01e3102e01b3119e01f315f2931d84ff04f0353542dd00a444b235e29537028d020235d29937024d00021d17024e01c3104e01d3102e01e3100e01f315f2918d84ff04103535414d00a444c235e2953700fd05d2993700cd04f235c29d37008d043235b29137104d020235a29537140f0318700218df8631000220df1040919f80210002900f0a18009eb0203597800295ed0997800295dd0d97800295cd0197900295bd0597900295ad09979002959d0d979002958d0197a002957d0597a002956d0997a002955d0d97a002954d0197b002953d0597b002952d0997b002951d0d97b002950d0197c00294fd0597c00294ed0997c00294dd0d97c00294cd0197d00294bd0597d00294ad0997d002949d0d97d002948d0197e002947d0597e002946d0997e002945d0d97e002944d0197f002943d0597f002942d0997f002941d0d97f002940d02032602a7ff498af43e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a04d84ff0770109f8021003d100218df8631003e009eb020100224a700269002319f80310002900f0a28009eb0307797800295fd0b97800295ed0f97800295dd0397900295cd0797900295bd0b97900295ad0f979002959d0397a002958d0797a002957d0b97a002956d0f97a002955d0397b002954d0797b002953d0b97b002952d0f97b002951d0397c002950d0797c00294fd0b97c00294ed0f97c00294dd0397d00294cd0797d00294bd0b97d00294ad0f97d002949d0397e002948d0797e002947d0b97e002946d0f97e002945d0397f002944d0797f002943d0b97f002942d0f97f002941d02033602b7ff498af60233ce001333ae0023338e0033336e0043334e0053332e0063330e007332ee008332ce009332ae00a3328e00b3326e00c3324e00d3322e00e3320e00f331ee010331ce011331ae0123318e0133316e0143314e0153312e0163310e017330ee018330ce019330ae01a3308e01b3306e01c3304e01d3302e01e3300e01f334ff0010e0a2a1fd364214ff47a7642f210750eeb8e077f00b2fbf7f40a2c12d30efb01f7b2fbf7f40a2c0cd30efb06f7b2fbf7f40a2c06d30efb05feb2fbfef7092fe6d800e0be465f2b9a4698bf4ff05f0a5e2b6ad84cf6cd4bcaf1000803f10409ccf6cc4bb2fbfefc01a90cf130034944bef1090f01f8043c4bd908eb0904032caefb0b654fd00cfb1e22ee08b2fbf6f5bef1640f05f1300701f8037c3cd348f21f53c5f2eb13022caefb03373bd005fb16227e09b2fbf6f5bef57a7f05f1300301f8023c2bd344f6d353c1f26203012caefb033427d005fb1622a609b2fbf6f542f2107705f13003be4501f8013c19d341f25971cdf2b711aefb011309f1040905fb162208eb090104294fea533ea9d109e0a9f1030a06e0a9f1020a03e0a9f1010a00e0ca460df10409002209f80a2019f80210002900f0a18009eb0203597800295ed0997800295dd0d97800295cd0197900295bd0597900295ad09979002959d0d979002958d0197a002957d0597a002956d0997a002955d0d97a002954d0197b002953d0597b002952d0997b002951d0d97b002950d0197c00294fd0597c00294ed0997c00294dd0d97c00294cd0197d00294bd0597d00294ad0997d002949d0d97d002948d0197e002947d0597e002946d0997e002945d0d97e002944d0197f002943d0597f002942d0997f002941d0d97f002940d02032602a7ff498af52e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a13d84ff0750109f802100ed009eb020173235e2a4b7008d020235d2a8b7004d070235c2acb7040f08b8000218df863104269002319f80310002900f0a58009eb0307797800295fd0b97800295ed0f97800295dd0397900295cd0797900295bd0b97900295ad0f979002959d0397a002958d0797a002957d0b97a002956d0f97a002955d0397b002954d0797b002953d0b97b002952d0f97b002954d0397c002953d0797c002952d0b97c002951d0f97c002950d0397d00294fd0797d00294ed0b97d00294dd0f97d00294cd0397e00294bd0797e00294ad0b97e002949d0f97e002948d0397f002947d0797f002946d0b97f002945d0f97f002944d02033602b7ff498af60233fe001333de002333be0033339e0043337e0053335e0063333e0073331e008332fe009332de00a332be00b3329e00c3327e00d3325e00e3323e000220a7174e70f331ee010331ce011331ae0123318e0133316e0143314e0153312e0163310e017330ee018330ce019330ae01a3308e01b3306e01c3304e01d3302e01e3300e01f334ff0010e0a2a1fd364214ff47a7642f210750eeb8e077f00b2fbf7f40a2c12d30efb01f7b2fbf7f40a2c0cd30efb06f7b2fbf7f40a2c06d30efb05feb2fbfef7092fe6d800e0be465f2b9a4698bf4ff05f0a5e2b6cd84cf6cd4bcaf1000803f1040cccf6cc4bb2fbfef609eb0c0106f13003bef1090f01f8043c4dd908eb0c04032caefb0b7553d006fb1e22ee08b2fbf6f5bef1640f05f1300701f8037c3ed348f21f53c5f2eb13022caefb03373fd005fb16227e09b2fbf6f9bef57a7f09f1300301f8023c2dd344f6d353c1f26203012caefb033429d009fb1622a409b2fbf4f542f2107605f13003b64501f8013c1bd341f25971cdf2b711aefb01130cf1040c05fb142208eb0c0104294fea533e0df10409a7d10be0acf1030a08e0acf1020a05e0acf1010a00e0e2460df10409002209f80a2019f80210002900f0a18009eb0203597800295ed0997800295dd0d97800295cd0197900295bd0597900295ad09979002959d0d979002958d0197a002957d0597a002956d0997a002955d0d97a002954d0197b002953d0597b002952d0997b002951d0d97b002950d0197c00294fd0597c00294ed0997c00294dd0d97c00294cd0197d00294bd0597d00294ad0997d002949d0d97d002948d0197e002947d0597e002946d0997e002945d0d97e002944d0197f002943d0597f002942d0997f002941d0d97f002940d02032602a7ff498af5ee001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a1fd84ff0750109f802101ad009eb020173235e2a4b7014d020235d2a8b7010d074235c2acb700cd031235b2a0b7108d030235a2a4b7104d02023592a8b7140f0638200218df86310d0f8cc03002219f80210002900f0a28009eb0203597800295fd0997800295ed0d97800295dd0197900295cd0597900295bd0997900295ad0d979002959d0197a002958d0597a002957d0997a002956d0d97a002955d0197b002954d0597b002953d0997b002952d0d97b002951d0197c002950d0597c00294fd0997c00294ed0d97c00294dd0197d00294cd0597d00294bd0997d00294ad0d97d002949d0197e002948d0597e002947d0997e002946d0d97e002945d0197f002944d0597f002943d0997f002942d0d97f002941d02032602a7ff498af60223ce001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f324ff001080a281fd364214ff47a7742f2107608eb88035b00b0fbf3f50a2d12d308fb01f3b0fbf3f50a2d0cd308fb07f3b0fbf3f50a2d06d308fb06f8b0fbf8f3092be6d800e098465f2a934698bf4ff05f0b5e2a66d84cf6cd4c48f21f5a44f6d359cbf100060432ccf6cc4cc5f2eb1ac1f26209b0fbf8f501ab05f130019c18b8f1090f04f8041c40d9b1180329a8fb0c3745d005fb1800fd08b0fbf5f7b8f1640f07f1300304f8033c32d30229a8fb0ae335d007fb15005d09b0fbf5f7b8f57a7f07f1300304f8023c25d30129a8fb093125d007fb15008909b0fbf1f542f2107705f13003b84504f8013c17d341f25973cdf2b7130432a8fb033705fb1100b11804294fea5738b4d109e0a2f1030b06e0a2f1020b03e0a2f1010b00e093460df10409002009f80b0019f80010002900f0a18009eb0002517800295ed0917800295dd0d17800295cd0117900295bd0517900295ad09179002959d0d179002958d0117a002957d0517a002956d0917a002955d0d17a002954d0117b002953d0517b002952d0917b002951d0d17b002950d0117c00294fd0517c00294ed0917c00294dd0d17c00294cd0117d00294bd0517d00294ad0917d002949d0d17d002948d0117e002947d0517e002946d0917e002945d0d17e002944d0117f002943d0517f002942d0917f002941d0d17f002940d0203060287ff498af49e001303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f280ad84ff0750109f8001005d009eb000173225e284a7054d100208df86300009d01ae45f29c1440f24037284602213246c2f21304c2f2000700f02dfb00208df80400304600f0a5fc28460e21324600f022fbf869a04264d1b86ab0f5062f60d1f86aa0425dd14bf2941e45f61063c2f21b0e0020c2f2130348f239550ef1040c002763454dd81c6834f003061cd09e454ff0ff3228bf0c2c45d3aeeb03018e424ff0ff3141d83946be4288bf3146e2071cbf0f4630449919013d01f10403e0d12fe000208870aae70421b7f5807f21d300223e46b6f5007f17d3b6f5806f16d3b6f5006f15d30901b2eb163f4fea1616f0d10fe00022ca719ce500219171fff7ceb800211172fef788bd490002e0890000e0c900634504d1494207ea0102014603e04ff0ff324ff0ff3147f6d060c2f20700006841f69c13c2f2230342f6941c9842c2f2270c4dd142f210674ff0000ac2f2230744f239050cf1040900264ff0ff384ff0ff3b4f453cd83c6834f0030015d0bc4528bf0c2c36d3aceb0703984234d8b042334688bf0346e4071cbf1e4682443844013d00f10407e4d122e00425b6f5807f17d34ff0000e3046b0f5007f0cd3b0f5806f0bd3b0f5006f0ad32d01beeb103f4fea1010f0d104e06d0002e0ad0000e0ed004f4504d1684206ea0008d34603e04ff0ff384ff0ff3b40f24037c2f200073b680cf1080083425ad1fb68b3f5333f56d13b69834253d14ff2945043f21063c2f229004ff00009c2f2270342f6f93500f1040e4ff0000c4ff0ff364ff0ff3a73453dd81c6834f0030717d0984228bf0c2c37d3c01a874236d86046674588bf3846e4071cbf8446b944d819031d4ff29450013dc2f22900e2d121e00427bcf5807f16d300206546b5f5007f0cd3b5f5806f0bd3b5f5006f0ad33f01b0eb153f4fea1515f0d104e07f0002e0bf0000e0ff00734504d17b420cea0306ca4603e04ff0ff364ff0ff3a66208df8040072208df8050065208df806008df807002f208df808006d204b238df8090061208df80d3069238df80a0078208df80e3042238df80b0020208df80f303a23562701ac35468df80c008df810308df811004c238df8137047278df81600002620468df812308df814708df815308df8176000f07afca05d002800f0a080a019417800295ed0817800295dd0c17800295cd0017900295bd0417900295ad08179002959d0c179002958d0017a002957d0417a002956d0817a002955d0c17a002954d0017b002953d0417b002952d0817b002951d0c17b002950d0017c00294fd0417c00294ed0817c00294dd0c17c00294cd0017d00294bd0417d00294ad0817d002949d0c17d002948d0017e002947d0417e002946d0817e002945d0c17e002944d0017f002943d0417f002942d0817f002941d0c07f002840d02036602e7ff49aaf64e001363ae0023638e0033636e0043634e0053632e0063630e007362ee008362ce009362ae00a3628e00b3626e00c3624e00d3622e00e3620e00f361ee010361ce011361ae0123618e0133616e0143614e0153612e0163610e017360ee018360ce019360ae01a3608e01b3606e01c3604e01d3602e01e3600e01f365f2e25d84ff02000a05521d0a01945215e2e41701cd076215d2e817018d065215c2ec17014d06e215b2e017110d048215a2e41710cd07521592e817108d06221582ec17104d02021572e017240f0e18000208df8630020465946424600f0a6fb00202b46215c002900f0a08021184a78002a5ed08a78002a5dd0ca78002a5cd00a79002a5bd04a79002a5ad08a79002a59d0ca79002a58d00a7a002a57d04a7a002a56d08a7a002a55d0ca7a002a54d00a7b002a53d04a7b002a52d08a7b002a51d0ca7b002a50d00a7c002a4fd04a7c002a4ed08a7c002a4dd0ca7c002a4cd00a7d002a4bd04a7d002a4ad08a7d002a49d0ca7d002a48d00a7e002a47d04a7e002a46d08a7e002a45d0ca7e002a44d00a7f002a43d04a7f002a42d08a7f002a41d0c97f002940d0203060287ff49aaf5be001303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f281cd84ff02001215418d021184f225e284a7013d074225d288a700fd068225c28ca700bd065225b280a7107d072225a284a7103d0202259288a7110d100208df8630001ac204651461a4600f0d8fa00981a21224600f009f819b0bde8f08f0020c871eee7002141721ee72de9f04f81b0934612788ab1022300bf0beb030717f8012c62b11bf8032062b17a784ab1ba780433002af1d1023b04e0002205e0013b00e0013303eb4302520042eae272931c02f1010c01eb810203f0030e23f0030a00eb8213a3f5a0784ff0ff3906e009f10109b9f10d0f08f5a0786bd009eb010202eb8202bcf1030f00eb821605d24ff0ff34bef1000f2ed1e9e70024474604e000bfa24507f1020721d0621cd209042a03d83a7802f0f0023a70a21cd209042a03d87a7802f00f027a70e21cd209042a03d87a7802f0f0027a700434e209042adfd86208b55c05f00f05b554d9e7013cbef1000fbbd0a21cd509042d08d85208b55ce7076ff00f0708bf0f273d40b554bef1010fabd0e21cd509042d09d85208b55ce7074ff00f0708bf6ff00f073d40b554bef1020f9ad0221dd509042d96d85208e407b55c6ff00f0408bf0f242c40b4548ce79bf80010cdf800b0002900f0ce80022041f2410a41f2420b41f2430808e0009a063012f8011f03330029009200f0bd80a1f17f0717f15f0ff1d342f2a572c0f2000201eb41017a4402eb81014ff20014a1f5c079cff6ff7402e014f52074ded0494619f9027bc509042d09d8b7f1ff3f06dc1a1992f8016f46f0f00682f8016f5ffa87fe5fea4e6208d5042d06d81a1992f8017f47f00f0782f8017f5fea8e620ad540f27d22904206d81a1992f8027f47f0f00782f8027f5feace6209d5b0f51f7f06d81a1992f8027f47f00f0782f8027f5fea0e724fea900c09d5bcf19e0f06d81a1992f8036f46f0f00682f8036f5fea4e720ad540f27a22904206d81a1992f8037f47f00f0782f8037f91f90110042d09d8b1f1ff3f06dc1a1912f80a7047f0f00702f80a70c9b24a0608d5042d06d81a1912f80a7047f00f0702f80a708a060ad540f27d22904206d81a1912f80b7047f0f00702f80b70ca0609d5b0f51f7f06d81a1912f80b7047f00f0702f80b700a0709d5bcf19e0f06d81a1912f8087047f0f00702f8087049077ff565af40f27a2188423ff660af191911f8082042f00f0201f8082057e701b0bde8f08f00bf2de9f04381b04ff29c51c2f229010f68044638466ff39600b0f1005fbff35f8f0cd138684ef66d61ccf2ff01884205d17868b0f57e7f18bf002700e0002745f64d50c0f24600804742f65c21c0f2000142f66122c0f2000279447a44022808bf0a4642f64321c0f2000179440128204618bf114600f03efe47b197f87f0180b142f64121c0f20001794419e042f62e21c0f200017944204601b0bde8f04300f029be97f8780142f61b22c0f2000242f61821c0f2000179447a44002808bf1146204600f017fe97f8790142f6f812c0f2000242f60221c0f200017944012820467a4408bf114600f005fe42f6f011c0f200017944204600f0fdfd97f9c711204600f0ecfe42f6dc11c0f200017944204600f0f0fdf6f7fcfa42f6be18c0f2000842f6ca16c0f200067e44054600f003003146f844012808bf414642f6a819c0f2000902282046f94408bf494600f0d2fda808012808bf4646022808bf4e462046314600f0c7fd42f68611c0f200017944204600f0bffdd7f8880198b147f64c61c2f207010a68821a012a0bdb0968401a44f6d35100f2e730c1f26201a0fb0101890900e00021204600f0b4fe42f64811c0f200017944204600f09cfdd7f8c011204600f0a7fe42f63511c0f200017944204600f08ffdb7f8c411204600f09afe42f61d11c0f200017944204600f082fd97f8c611204600f08dfe42f60611c0f200017944204600f075fdb7f8ca11204600f080fe42f6f305c0f200057d442046294600f067fdb7f8cc11204600f072fe2046294600f05efdb7f8ce11204600f069fe42f6c701c0f200017944204600f051fdd7f88411204600f05cfe42f6b201c0f200017944204600f044fd97f9c911204600f033fe42f69e01c0f200017944204600f037fd97f9c811204601b0bde8f04300f023be00bf2de9f04f81b094464a1c00f08a804fea912e0026825d002a00f0638183195a78002a00f005819a78002a00f00381da78002a00f001811a79002a00f0ff805a79002a00f0fd809a79002a00f0fb80da79002a00f0f9801a7a002a00f0f7805a7a002a00f0f5809a7a002a00f0f380da7a002a00f0f1801a7b002a00f0ef805a7b002a00f0ed809a7b002a00f0eb80da7b002a00f0e9801a7c002a00f0e7805a7c002a00f0e5809a7c002a00f0e580da7c002a00f0e5801a7d002a00f0e5805a7d002a00f0e5809a7d002a00f0e580da7d002a00f0e5801a7e002a00f0e5805a7e002a00f0e5809a7e002a00f0e580da7e002a00f0e5801a7f002a00f0e5805a7f002a00f0e5809a7f002a00f0e580da7f002a00f0e5802036602e7ff47baf6026dfe00021425c002a00f0978143185a78002a00f09f809a78002a00f09f80da78002a00f09f801a79002a00f09f805a79002a00f09f809a79002a00f09f80da79002a00f09f801a7a002a00f09f805a7a002a00f09f809a7a002a00f09f80da7a002a00f09f801a7b002a00f09f805a7b002a00f09f809a7b002a00f09f80da7b002a00f039811a7c002a00f037815a7c002a00f035819a7c002a00f03381da7c002a00f031811a7d002a00f02f815a7d002a00f02d819a7d002a00f02b81da7d002a00f029811a7e002a00f027815a7e002a00f025819a7e002a00f02381da7e002a00f021811a7f002a00f01f815a7f002a00f01d819a7f002a00f01b81da7f002a00f01981203160297ff47baf1be1013656e0023654e0033652e0043650e005364ee006364ce007364ae0083648e0093646e00a3644e00b3642e00c3640e00d363ee00e363ce00f363ae0103638e0113636e00131f0e0123632e00231ece013362ee00331e8e014362ae00431e4e0153626e00531e0e0163622e00631dce017361ee00731d8e018361ae00831d4e0193616e00931d0e01a3612e00a31cce01b360ee00b31c8e01c360ae00c31c4e01d3606e00d31c0e01e3602e00e31bce01f36b1f5205f1ed3012164234ff47a7442f2107501eb81025200befbf2f70a2f13d301fb03f2befbf2f70a2f0dd301fb04f2befbf2f70a2f07d36943befbf1f2092ae7d802e0012100e011465f2eb24698bf4ff05f0a5e2e6ad84cf6cd49cdf800c0caf1000806f1040cccf6cc49befbf1f400eb0c0b04f1300209290bf8042c4ad908eb0c06032ea1fb09324ed004fb11e4d208b4fbf2f5642905f130030bf8033c3cd348f21f53c5f2eb13022ea1fb03733bd005fb12445a09b4fbf2f5b1f57a7f05f130030bf8023c2bd344f6d353c1f26203012ea1fb033627d005fb1242b609b2fbf6f542f2107705f13003b9420bf8013c19d341f25973cdf2b713a1fb03130cf1040c05fb162e08eb0c0104294fea5331abd109e0acf1030a06e0acf1020a03e0acf1010a00e0e246ddf800c0002100f80a102de00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d801444ff03f020a7003d1002180f85f1001e000224a700021425c002a00f0a08043185a78002a5ed09a78002a5dd0da78002a5cd01a79002a5bd05a79002a5ad09a79002a59d0da79002a58d01a7a002a57d05a7a002a56d09a7a002a55d0da7a002a54d01a7b002a53d05a7b002a52d09a7b002a51d0da7b002a50d01a7c002a4fd05a7c002a4ed09a7c002a4dd0da7c002a4cd01a7d002a4bd05a7d002a4ad09a7d002a49d0da7d002a48d01a7e002a47d05a7e002a46d09a7e002a45d0da7e002a44d01a7f002a43d05a7f002a42d09a7f002a41d0da7f002a40d0203160297ff49aaf44e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2905d801444ff02f020a7040f09180002180f85f101cf1010100f090804fea9c210024025d002a00f0698103195a78002a00f00b819a78002a00f00981da78002a00f007811a79002a00f005815a79002a00f003819a79002a00f00181da79002a00f0ff801a7a002a00f0fd805a7a002a00f0fb809a7a002a00f0f980da7a002a00f0f7801a7b002a00f0f5805a7b002a00f0f3809a7b002a00f0f180da7b002a00f0ef801a7c002a00f0ed805a7c002a00f0eb809a7c002a00f0eb80da7c002a00f0eb801a7d002a00f0eb805a7d002a00f0eb809a7d002a00f0eb80da7d002a00f0eb801a7e002a00f0eb805a7e002a00f0eb809a7e002a00f0eb80da7e002a00f0eb801a7f002a00f0eb805a7f002a00f0eb809a7f002a00f0eb80da7f002a00f0eb802034602c7ff47baf6024e5e000224a701cf101017ff470af0021425c002a00f0988142185378002b00f09f809378002b00f09f80d378002b00f09f801379002b00f09f805379002b00f09f809379002b00f09f80d379002b00f09f80137a002b00f09f80537a002b00f09f80937a002b00f09f80d37a002b00f09f80137b002b00f09f80537b002b00f09f80937b002b00f09f80d37b002b00f03a81137c002b00f03881537c002b00f03681937c002b00f03481d37c002b00f03281137d002b00f03081537d002b00f02e81937d002b00f02c81d37d002b00f02a81137e002b00f02881537e002b00f02681937e002b00f02481d37e002b00f02281137f002b00f02081537f002b00f01e81937f002b00f01c81d27f002a00f01a81203160297ff47baf1ce1013456e0023454e0033452e0043450e005344ee006344ce007344ae0083448e0093446e00a3444e00b3442e00c3440e00d343ee00e343ce00f343ae0103438e0113436e00131f1e0123432e00231ede013342ee00331e9e014342ae00431e5e0153426e00531e1e0163422e00631dde017341ee00731d9e018341ae00831d5e0193416e00931d1e01a3412e00a31cde01b340ee00b31c9e01c340ae00c31c5e01d3406e00d31c1e01e3402e00e31bde01f344ff00109bcf5205f1fd364234ff47a7742f2107609eb89025200b1fbf2f50a2d12d309fb03f2b1fbf2f50a2d0cd309fb07f2b1fbf2f50a2d06d309fb06f9b1fbf9f2092ae6d800e091465f2ca44698bf4ff05f0c5e2c05d9002100f80c1001b0bde8f08f4cf6cd4accf1000e04f1040bccf6cc4ab1fbf9f400eb0b0704f13002b9f1090f07f8042c4bd90eeb0b06032ea9fb0a25e0d004fb1911ec08b1fbf4f5b9f1640f05f1300207f8032c3cd348f21f52c5f2eb12022ea9fb0232ccd005fb14115409b1fbf4f8b9f57a7f08f1300207f8022c2bd344f6d352c1f26202012ea9fb0225b8d008fb1411ac09b1fbf4f542f2107305f13002994507f8012c19d341f25972cdf2b712a9fb02230bf1040b05fb14110eeb0b02042a4fea5339a9d19ae7abf1030c97e7abf1020c94e7abf1010c91e7dc468fe70f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d801444ff03f020a7005d1002180f85f1001b0bde8f08f0020487001b0bde8f08f00bf2de9f0411f22831813f81f7c002f00f0808013f81e7c002f7dd013f81d7c002f7bd013f81c7c002f79d013f81b7c002f77d013f81a7c002f75d013f8197c002f73d013f8187c002f71d013f8177c002f6fd013f8167c002f6dd013f8157c002f6bd013f8147c002f69d013f8137c002f67d013f8127c002f65d013f8117c002f63d013f8107c002f61d013f80f7c002f5fd013f80e7c002f5dd013f80d7c002f5bd013f80c7c002f59d013f80b7c002f57d013f80a7c002f55d013f8097c002f53d013f8087c002f51d013f8077c002f4fd013f8067c002f4dd013f8057c002f4bd013f8047c002f49d013f8037c002f47d013f8027c002f45d013f8013c002b43d0835c002b41d020327f2a7ff47baf64e01f3a3ae01e3a38e01d3a36e01c3a34e01b3a32e01a3a30e0193a2ee0183a2ce0173a2ae0163a28e0153a26e0143a24e0133a22e0123a20e0113a1ee0103a1ce00f3a1ae00e3a18e00d3a16e00c3a14e00b3a12e00a3a10e0093a0ee0083a0ce0073a0ae0063a08e0053a06e0043a04e0033a02e0023a00e0013a5f2a25d8c2f15d0cc2f15e0ec2f15f03c2f160080244002509e000bffc78002cf47008bfbde8f0810435a84510d04e5d565586b1ab420bd04f197c78561974704cb1ae4504d0bc78b47024b1ac45e5d1002180f85f10bde8f08100bf7f2905d141f60a41c0f20001794403e7b1f1ff3fc8bf0fe0b0b541f6f432c0f200027a440d4611460446fff7f5fe20466942bde8b040ffe72de9f04f0024025d002a00f0a18003195a78002a5fd09a78002a5ed0da78002a5dd01a79002a5cd05a79002a5bd09a79002a5ad0da79002a59d01a7a002a58d05a7a002a57d09a7a002a56d0da7a002a55d01a7b002a54d05a7b002a53d09a7b002a52d0da7b002a51d01a7c002a50d05a7c002a4fd09a7c002a4ed0da7c002a4dd01a7d002a4cd05a7d002a4bd09a7d002a4ad0da7d002a49d01a7e002a48d05a7e002a47d09a7e002a46d0da7e002a45d01a7f002a44d05a7f002a43d09a7f002a42d0da7f002a41d02034602c7ff49aaf60243ce001343ae0023438e0033436e0043434e0053432e0063430e007342ee008342ce009342ae00a3428e00b3426e00c3424e00d3422e00e3420e00f341ee010341ce011341ae0123418e0133416e0143414e0153412e0163410e017340ee018340ce019340ae01a3408e01b3406e01c3404e01d3402e01e3400e01f344ff0010a0a2920d364224ff47a7742f2107600bf0aeb8a035b00b1fbf3f50a2d12d30afb02f3b1fbf3f50a2d0cd30afb07f3b1fbf3f50a2d06d30afb06fab1fbfaf3092be6d800e09a465f2ca44698bf4ff05f0c5e2c04d9002100f80c10bde8f08f4cf6cd4bccf1000904f1040eccf6cc4bb1fbfaf500eb0e0705f13002baf1090f07f8042c4bd909eb0e06032eaafb0b34e1d005fb1a11e508b1fbf5f4baf1640f04f1300307f8033c3cd348f21f52c5f2eb12022eaafb0223cdd004fb15115d09b1fbf5f8baf57a7f08f1300207f8022c2bd344f6d352c1f26202012eaafb0224b9d008fb1511a409b1fbf4f542f2107305f130029a4507f8012c19d341f25972cdf2b712aafb02230ef1040e05fb141109eb0e02042a4fea533aa9d19be7aef1030c98e7aef1020c95e7aef1010c92e7f44690e700bf2de9f047a4b0d0f89842bff35f8fa4b142f691760546c0f2440620460021b04770b1d5f8900340b142f64d42c0f244022d2124b0bde8f047104724b0bde8f08747f64c69c2f20709d9f8007095f8a103002844d0f3f71cfa002840d0d5f89813781a1bd495f86801bff35f8f20b995f82403bff35f8fe0b395f8a00385f8a10380b3d5f8900342f64d48c0f2440878b1d9f800102d31c5f898131de0d5f8900300b342f64d42c91bc0f24402904719e04ff64170cff6ff70a8f1e807784400212a460023b847c5f89003d9f8001001f12d01c5f8981310b12d21c04710b1002085f8a10306f16601204624b0bde8f04708474af21f37c0f24707002002908df80c00b84795f86801bff35f8f20b995f82403bff35f8f18b12846fcf765fee1e70df104084046f1f70dff02a9284600224ff0000af7f70cff81464046f1f7d2ffb9f1000f02d085f8a1a303e00146284600f008f89df80c000028c3d107f14c008047bfe72de9f04f90f8d13300f5697c4cf82310591c41f69a134b4390f8d0b31b0c03eb8303a1eb4301bbf1090f80f8d1139cbf0bf1010b80f8d0b3bbf1040f0bf0030e05d200244ff000094ff0000a2ee00bf0fc0800244ff000094ff0000a00eb8406d6f8a473d6f8a853b7fbfbf3d6f8ac1303eb090203fb1b73b5fbfbf7d6f8b06307fb1b5553443a44b1fbfbf72b44b6fbfbf53a4407fb1b1105eb020905fb1b6219440434a04502eb010ad7d1bef1000f1dd05cf82450bef1010fb5fbfbf606fb1b55b144aa4412d00ceb84056e68bef1020fb6fbfbf404fb1b66a144b24406d0ae68b6fbfbf505fb1b66a944b244bafbfbf202eb0901c0f8cc13bde8f08f00bff0b500294ff6ff7e00f0af80012903d10022c90775d1a8e021f00102c2f1000c6ff0010241f221038418a578e7788eea05252cb26e0036b203ead43484ea450503ead6366c0086ea450624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead53585ea460686ea07273eb27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead636023286ea470703ead5360ceb0205023586ea470e95d10232c90733d0805c41f221038eea002001b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead23181ea400e1ffa8ef0f0bd2de9f04fcbb00646c07c8b468df80600b08954998df80700000a8df808008df80a10080af1898df80b00080a17468df80d000020012b8df805308df809b08df80c108df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f008df820008df821008df822004cd1307e96f81780a0f1090144f62510c2f29240a1fb0002891a02eb510100970df10500b8eb910f88bf4fea9108c8ebc80706f11a0400f1010a00f10509093021463a46eff7b5fb06f1210506f1280029460722eff7adfb284621460722eff7a8fb20465146032207f10905eff7a1fb06f11d0049460422eff79bfbf07d02289cbf0130f075009fbb450cd0b8f1240f13d94ff0ff304bb0bde8f08fbb454ff00905f07509d14ef60b67abb20df10502c0f247070120f02115e002200df12304aab28df82300a01c0df105018df82470eff76ffba81c4af6610782b2c0f24607f02021460023b8474bb0bde8f08f2de9f04184b0044690f84d0000284ff0000000f09a8094f84f1042f28b0701f078018df8071094f85010c0f25b0701f007018df80610cde9020002a906200022b847054670b9002002adcde902000df1070128460122eff735fb012029460122b847054694f84e00a8b1002602a906200022cde90266b84760b9cde9026602ae04f1540130460122eff71cfb572031460122b8470543002602a906200022cde90266b84768b90df108080df1060140460122cde90266eff705fb312041460122b847054302a906200022cde90266b84768b9002002ae04f15301cde9020030460122eff7effa462031460122b8470543002602a906200022cde90266b84768b90df1080804f1510140460222cde90266eff7d8fa362041460222b847054302a9a3200022cde90266b847054302a997200022cde90266b84742f62b310543c0f2440102208847284615b104b0bde8f081002184f84e1084f84d10a16504b0bde8f08100bf2de9f04f8bb00190233002254ff000080df10c0a4ff001094ff0000b002402900be000bf09fa0bf0002f0bf1010b08bf0443bbf1070f354426d041f28a20c0f20000784410f80b0041f28421c0f20001794411f80b6042f2fb0351463246c0f25b03cde90988cde90788cde90588cde90388984707460028d4d10298514628443246eff76dfacde742f2fb00c0f25b004ff60073002600f1340703a9c0f21f0381200c22cde90966cde90766cde90566cde90366b84730b9029803a928440c22eff74efa8026ddf804c040f6b80146ea0400220ac2f200018cf823008cf824200a68002a00f0ad8045f2fd774ff6ff766ff0020241f22103c0f202070d681544ed7886ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4342d7984ea460686ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4346d7984ea460686ea052635b2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead4350332ba4285ea46067ff460afb1b2090a01e0ff26ff218cf83e109cf84c109cf84d208cf83f1026218cf83d608cf84020ff288cf84b101cbf04208cf81c000bb0bde8f08f70b588b042f2fb0514460e46c0f25b05002181280791cde90511cde90311cde90111009107d11b023435694681202246a84720b905e069462246a84708b108b070bd6946054630462246eff74ff9284608b070bd2de9f0478cb08579972d17d00446fe2d0fd0fd2d2ad142f69570c0f25b008047002084f84e0084f84d00a0650cb0bde8f08720460cb0bde8f047cbe50020cde9040042f2fb00c0f25b00a0f1700304a9972000229847044642f62b31c0f244010220884720460cb0bde8f08742f2fb08002094f80c90677bc0f25b08cde90a00cde90800cde90600cde9040004a99f200422c047064620b901a804a90422eff7fbf8002e6ff00100ddd19df80410bd29d9d19df805104029d5d19df806101029d1d194f84d00002852d047f64c60c2f20700006843f698210844a065002604a937200222cde90a66cde90866cde90666cde90466c047002840f0898002a804a90222eff7c9f804a947200122cde90a66cde90866cde90666cde90466c047002875d10df1030004a90122eff7b5f80020cde90a00cde90800cde90600cde9040004a905200122c047002860d10df1020004a9012249ea0726eff79ef8352d5bdc002d00f08180012d70d19df8020082e0002604a9052001228df80e60adf80c600296cde90a66cde90866cde90666cde90466c047d8bb0df1080a04a950460122eff77af804a935200122cde90a66cde90866cde90666cde90466c04738bb0af1010004a90122eff767f8002604a937200222cde90a66cde90866cde90666cde90466c04798b90af1020004a90222eff753f804a947200122cde90a66cde90866cde90666cde904660126c04778b14ff0ff300cb0bde8f087362d6ed0572d15d194f84e00002800f08580572039e00af1040004a90122eff72ff804f14f0051460722eff729f884f84d603ee700208df801006946284601228df8009000f04ef9312d044627d036e09df80200002e00f0380618bf4036704010f0380f10d09df808009df8091040f680530e22c0f2000302ea96027b449a5a41ea0020904239d8012000218df80110694601228df8006000f024f904460df103014620012200f01df9054602a93620022200f017f945ea040141ea00040025a8f1700604a9a3200022cde90455b047044304a997200022cde90455b0470443aee69df802000e2101ea900040f60851c0f200017944085a864204d96ff001000cb0bde8f0876946362002228df800708df8019000f0e6f80446d0e70020cde90a00cde90800cde90600cde9040004a9592001220125c04700287ff45caf04f1540004a90122eef79bff84f84e5060e740f6b800c2f200000168002904bf4ff0ff30704780b584b000684ef2173102904ff41630039002a8c0f2470188474ff4f07042f29f7e01904ff4207cc0f25b0e0020002100220023cdf800c0f04704b080bd00bf2de9f04340f6b801c2f2000109684ff0000e01f1020c4cf6cd4340f2762808e00ef1010ebef5f07f0cf5a07c08bfbde8f083aef1a0096ff00906644607e000bf07eb0711a170053445452e46e8d290f84c1006f10a054f1e112905fb03f208bf570d122908d1a6f1e6019f294ff000079cbfb9f19f0f077b07eb071104f8021c90f84c1006f10c024f1e112903fb02f208bf570d122908d1a6f1e4019f294ff000079cbfb9f19f0f077b07eb071104f8011c90f84c1006f10e024f1e112903fb02f208bf570d122908d1a6f1e2019f294ff000079cbfb9f19f0f077b07eb0711217090f84c1006f110024f1e112903fb02f208bf570d122908d1a6f1e0019f294ff000079cbfb9f19f0f077b07eb0711617090f84c1006f112024f1e112903fb02f208bf570d12298ad1a6f1de019f294ff000079cbfb9f19f0f077b80e700bf2de9f04383b042f28b0914460e460546c0f25b090027694606200022cde90077c84710b103b0bde8f083e846404631462246cde90077eef7b1fe284641462246c84703b0bde8f08370b50446d0f840010d4638b92046f2f72ffb0028c4f8400108bf70bd42f64d46c0f2440606f140018847d4f8400129463246bde87040104746f6cc10c2f20700007808b10020704743f63c40c2f20700817a29b10178ff2902d14178ff29f1d090f83a1039b190f83010ff2903d190f83110ff29e6d090f86a1039b190f86010ff2903d190f86100ff28dbd0012070472de9f04f97b0229d4af6ab261692826805f15f07c0f2aa26a7fb064e1491511c15918160002d08bf4ff0010ed0f800904168b5f5007fbff35f8f05d9c1680131c16017b0bde8f08f219f002db7fa87f72a464fea571718bf01221742eed1a1eb090120318e45e9d847f64c610290c2f207010868bef1000f139000f0bc800298209a00f1e20112911599008e0f0a11970f0c090ecde90e10000a0d9016984ff0000c000a0c90180a0b90100a0a90280a09901398ca46010a0891010c000e069021984ff000080130109707910190cde9033ecdf814900ae008f101080bf11801dc44f0450af1010a017000f0848008eb0901129801f01f017924a5eb0c0211fb04004fea1c21017593464b21602a28bf4ff0600b4170422181700121c170149965450171159943734171119945748171109980f813c0c1710e9901720f9941720d9981721699c1720c9901730b9981732099c1730a9901740999817413994175089981750799c17506990176b4d02b46042a01d200261ee0029a0af01f01792711fb0721019a0bf07c0402eb0c050026aa1912f8019c01eb060e8ef8fb90af5d04368ef8fc705778b4428ef8fd7092788ef8fe20ecd11d46dde9033eddf814901bf0030788d0219a00f1190102eb0c04a25d012f8a553ff47faf721c14f80290022f01f80290ddf814903ff475afb21ca75c8f5470e7bff35f8f029809eb0e01016000f128040122bff35f8f54e8001f44e80023002bf9d1bff35f8f11b117b0bde8f08f006a42f64d42c0f244021421904700281ebfbff35f8f0020206017b0bde8f08fb0b546f6cc12c2f2070212780ab10320b0bd43f64642c2f2070213783bb112f80a3cff2b03d112f8093cff2befd092f830303bb192f82630ff2b03d192f82730ff2be4d092f860303bb192f85630ff2b03d192f85730ff2bd9d0137892f8304092f86020b4fa84f46409002b08bf0134002a08bf44f001043cb144f2a932c0f24802904718b10020b0bd0520b0bd43f62d45c0f248050120a84704460020a8470146042c4ff0050088bfb0bd2144092998bf0020b0bd00bff0b581b043f6cc0200eb0011c2f2070212f821200ab1032121e043f6c402c2f2070202eb8104216889b943f62d470646c0f248070120b84705460020b847042d4ff005010bd828440928304607d84cf2e573c0f251030121224698470021084601b0f0bd2de9f04f81b088460b999b469246b8f1090f064605d0b8f10a0f05d1bbf1000f02d08946052505e003258946b8f10b0f08bfcd1c05f1080449f6cd71a0b2c0f24c018847034600284ff0000056d0ddf828e024f00301b44605f003024c426ff0030100bf5e1804316718043730717071b071f071f6d142b158180021012a017103d0022a417118bf8171280ab8f1090f1d7058700dd14fea1a2083f802a0d87083f804b04fea1b20022204210827052618e0b8f10a0f24d10c20bbf1000f08bf0a2018724fea1a2083f809a0987233d05ffa8bf04fea1b2106220c270b2683f802b0d9709855d9554af261560027c0f24c06604651460097b047012001b0bde8f08f5fea0b604ff0520008bf12204ff00a02187208bf09224fea1a20b9f1000f83f809a09872dfd0f046b9f1040f09f0030e03d2002715e00522d5e729f0030108f10106002700bff51915f8014cd819c472f45d043704736c78b9424473ac788473f1d1bef1000fbed018f8070003f10b01bef1010fc855b6d0781c18f80040bef1020f0c54afd0b81c18f800700f54aae70000434d01ffff0000000000000052420100ff000000080310000000000000000000080010631a2308011a1c0800100018c00420a0024801520964617368626f617264580162012028904e46616365636c61772f34300000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a89000000000000000006d6963205220006d6963204c20006d6963203f20006e6f206374780061726d200063666720006f6666200070646d00636f646563002072656720002074626c2000540053002d00206c65617365200073207461702000200042400020736b697020002f002066722000206e7466200020756e72656720009f0535593747c181040101010201020c5353a829c51bd314a810260d5b0bfe09", "desc": "append injected blobs to main-app payload" }, { "offset": 779123, "old": "805a3900", - "new": "68703a00", + "new": "10783a00", "desc": "main-app subheader payload size (ps)" }, { "offset": 152, "old": "005b3900", - "new": "e8703a00", + "new": "90783a00", "desc": "main-app TOC entry size (ps + 128)" }, { "offset": 779243, "old": "805a3904", - "new": "68703a04", + "new": "10783a04", "desc": "main-app preamble length (low 24 bits)" }, { "offset": 779247, "old": "effdbcea", - "new": "0b0c48b0", + "new": "adc5378e", "desc": "[5] ota/s200_firmware_ota.bin preamble crc32" }, { "offset": 156, "old": "c0c6322a", - "new": "52937d21", + "new": "0c2e1c3e", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)" }, { "offset": 779127, "old": "c0c6322a", - "new": "52937d21", + "new": "0c2e1c3e", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)" } ] diff --git a/app/g2/glance-host.ts b/app/g2/glance-host.ts index cf5d05ff..9a21c5f4 100644 --- a/app/g2/glance-host.ts +++ b/app/g2/glance-host.ts @@ -82,7 +82,7 @@ export class GlanceHost { * 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 + * no hold when "Show on long press" is off, no tilt 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. */ @@ -92,7 +92,8 @@ export class GlanceHost { 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()); + const event = glanceEventForGesture(gesture, this.isVisible()); + return event?.type === "tilt" ? { type: "tilt", maxMs: provider.tiltTimeoutMs() } : event; } /** diff --git a/app/g2/glance-state.ts b/app/g2/glance-state.ts index 9848d284..c63ad269 100644 --- a/app/g2/glance-state.ts +++ b/app/g2/glance-state.ts @@ -1,9 +1,10 @@ /** * 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 + * while the shell is asleep a press (single tap) shows it for a short while, a + * head-tilt wake shows it until the head comes back down, 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 @@ -13,17 +14,67 @@ /** Fallback press timeout when the host supplies none; each further press restarts it. */ export const GLANCE_TIMEOUT_MS = 3000; +/** + * Default cap for a head-tilt showing: the board normally stays up until the + * head comes back down (as the stock dashboard does), but a lost head-down + * event should not leave it on indefinitely. The user's Head tilt timeout + * setting replaces it (and may remove it). + */ +export const GLANCE_TILT_MAX_MS = 60_000; + +/** + * The firmware reports head-down once the pitch falls 20° below the head-up + * trigger (a fixed hysteresis in DRV_IMUCheckHeadUpEvent). Under this trigger, + * looking level again does not get below that, so the head-down may never come. + */ +export const GLANCE_LOW_TILT_TRIGGER_DEGREES = 25; + +/** + * How long a head-tilt showing may last without a head-down: capMs (the Head + * tilt timeout; null = no limit), except when the head-down is unlikely to + * arrive, where the tap duration is used if shorter: a tilt that woke a + * suspended EvenHub session (the head-down is only forwarded once the page is + * back, so a quick glance can end before that), or a trigger too low for a + * level gaze to count as looking down. triggerDegrees is the glasses' + * trigger, or null when not known. + */ +export function glanceTiltMaxMs(options: { + capMs: number | null; + resumedSession: boolean; + triggerDegrees: number | null; + tapTimeoutMs: number; +}): number | null { + const lowTrigger = + options.triggerDegrees !== null && options.triggerDegrees > 0 && options.triggerDegrees < GLANCE_LOW_TILT_TRIGGER_DEGREES; + if (!options.resumedSession && !lowTrigger) return options.capMs; + return options.capMs === null ? options.tapTimeoutMs : Math.min(options.capMs, options.tapTimeoutMs); +} + export type GlanceState = { visible: boolean; /** A long-press is holding the board up; the timer does not run meanwhile. */ holding: boolean; + /** + * A head-tilt is holding the board up: it stays until the firmware reports + * the head back down (or tiltMaxMs passes). + */ + tilted: boolean; + /** The current head-tilt's cap (0 when not tilted, null for none); a hold ending mid-tilt re-arms it. */ + tiltMaxMs: number | null; /** 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. */ + /** Single tap: show, and (re)start the auto-hide timer. */ | { type: "press" } + /** + * Head-tilt (IMU head-up): show until the head comes back down, or until + * maxMs passes (default GLANCE_TILT_MAX_MS; null = no limit). + */ + | { type: "tilt"; maxMs?: number | null } + /** The IMU reported the head back down: ends a head-tilt showing. */ + | { type: "head-down" } /** Long-press: show and keep showing until the release. */ | { type: "hold" } /** The long-press ended. */ @@ -33,7 +84,7 @@ export type GlanceEvent = /** 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 const GLANCE_HIDDEN: GlanceState = Object.freeze({ visible: false, holding: false, tilted: false, tiltMaxMs: 0, hideAtMs: null }); export function reduceGlance( state: GlanceState, @@ -43,14 +94,26 @@ export function reduceGlance( ): 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 }; + // A tap during a hold or a head-tilt changes nothing: those decide when it ends. + if (state.holding || state.tilted) return state; + return { visible: true, holding: false, tilted: false, tiltMaxMs: 0, hideAtMs: nowMs + timeoutMs }; + case "tilt": { + const maxMs = event.maxMs === undefined ? GLANCE_TILT_MAX_MS : event.maxMs; + return { visible: true, holding: state.holding, tilted: true, tiltMaxMs: maxMs, hideAtMs: state.holding ? null : tiltHideAt(nowMs, maxMs) }; + } + case "head-down": + // Only a head-tilt showing ends with the head; a tap's timer or a hold keeps running. + if (!state.tilted) return state; + return state.holding ? { ...state, tilted: false, tiltMaxMs: 0 } : GLANCE_HIDDEN; case "hold": - return { visible: true, holding: true, hideAtMs: null }; + return { visible: true, holding: true, tilted: state.tilted, tiltMaxMs: state.tiltMaxMs, hideAtMs: null }; case "release": // Releases delivered late or after a disabled hold gesture mean nothing here. - return state.holding ? GLANCE_HIDDEN : state; + if (!state.holding) return state; + // Head still up: the board stays, as if the tilt had just shown it. + return state.tilted + ? { visible: true, holding: false, tilted: true, tiltMaxMs: state.tiltMaxMs, hideAtMs: tiltHideAt(nowMs, state.tiltMaxMs) } + : GLANCE_HIDDEN; case "timeout": if (!state.visible || state.holding || state.hideAtMs === null || nowMs < state.hideAtMs) return state; return GLANCE_HIDDEN; @@ -59,6 +122,10 @@ export function reduceGlance( } } +function tiltHideAt(nowMs: number, maxMs: number | null): number | null { + return maxMs === null ? null : nowMs + maxMs; +} + /** * 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 @@ -66,13 +133,16 @@ export function reduceGlance( * 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 & {}), + gesture: "click" | "double-click" | "long-press" | "short-then-long-press" | "long-press-release" | "head-tilt" | "head-down" | (string & {}), boardVisible: boolean, ): GlanceEvent | null { switch (gesture) { case "click": - case "head-tilt": return { type: "press" }; + case "head-tilt": + return { type: "tilt" }; + case "head-down": + return boardVisible ? { type: "head-down" } : null; case "long-press": case "short-then-long-press": return { type: "hold" }; diff --git a/app/g2/head-angle.ts b/app/g2/head-angle.ts new file mode 100644 index 00000000..796e6426 --- /dev/null +++ b/app/g2/head-angle.ts @@ -0,0 +1,151 @@ +import { addImuListener, imuPitchDegrees, setImuReportEnabled, type ImuReading } from "../native/imu"; +import { getHeadUpStatus, setHeadUpTrigger, type HeadUpStatus } from "../native/glasses-controls"; +import { getStringSetting, setStringSetting } from "../native/settings-store"; +import { glanceAngleTriggerDegrees } from "../apps/glanceboard/glanceboard-settings"; + +// Same ImuReportPace code the accelerometer demo uses (not literal Hz). +const IMU_REPORT_PACE = 200; +/** How often the live reading checks visibility and repaints a changed value. */ +const LIVE_TICK_MS = 250; +/** Weight of each new sample in the smoothed pitch. */ +const LIVE_SMOOTHING = 0.3; +/** A reading older than this (or none this long after the stream started) is shown as missing. */ +const LIVE_STALE_MS = 3_000; + +/** + * The head's pitch, live, for the Head tilt angle picker. The IMU report + * stream runs while `visible()` holds (checked every tick, so it stops while + * the screen is off or the window is in the background) and until stop(). + * text() is the footer line; a changed value repaints at most once a tick. + */ +class LiveHeadPitch { + private pitch: number | null = null; + private lastSampleAt = 0; + private streaming = false; + private streamingSince = 0; + private shown = ""; + private readonly unsubscribe: () => void; + private readonly timer: ReturnType<typeof setInterval>; + + constructor( + private readonly visible: () => boolean, + private readonly repaint: () => void, + ) { + this.unsubscribe = addImuListener((reading) => this.onReading(reading)); + this.timer = setInterval(() => this.tick(), LIVE_TICK_MS); + this.tick(); + } + + text(): string { + return this.shown; + } + + stop(): void { + clearInterval(this.timer); + this.unsubscribe(); + if (this.streaming) setImuReportEnabled(false, IMU_REPORT_PACE); + this.streaming = false; + } + + private onReading(reading: ImuReading): void { + // Glasses temples only (1 right, 3 left, 0 unspecified); the ring is 2. + if (!this.streaming || reading.source === 2) return; + if (!(Math.hypot(reading.x, reading.y, reading.z) > 1e-6)) return; + const pitch = imuPitchDegrees(reading); + this.pitch = this.pitch === null ? pitch : this.pitch + LIVE_SMOOTHING * (pitch - this.pitch); + this.lastSampleAt = Date.now(); + } + + private tick(): void { + const now = Date.now(); + const visible = this.visible(); + if (visible !== this.streaming) { + setImuReportEnabled(visible, IMU_REPORT_PACE); + this.streaming = visible; + this.streamingSince = now; + this.pitch = null; + } + const text = this.format(now); + if (text !== this.shown) { + this.shown = text; + this.repaint(); + } + } + + private format(now: number): string { + if (this.pitch !== null && now - this.lastSampleAt < LIVE_STALE_MS) return `Head now: ${Math.round(this.pitch)}°`; + return now - Math.max(this.streamingSince, this.lastSampleAt) < LIVE_STALE_MS ? "Head now: …" : "Head now: no data"; + } +} + +/** + * The Head tilt angle picker's footer: the head's current pitch, read while + * the picker is open and `visible()` holds (enumSettingMenuItem pickerFooter). + */ +export function liveHeadPitchFooter(visible: () => boolean): { text: () => string; open: (repaint: () => void) => () => void } { + let reader: LiveHeadPitch | null = null; + return { + text: () => reader?.text() ?? "", + open: (repaint) => { + const opened = new LiveHeadPitch(visible, repaint); + reader = opened; + return () => { + opened.stop(); + if (reader === opened) reader = null; + }; + }, + }; +} + +// The glasses' own head-up switch and angle from before Faceclaw first changed +// them, as "switch,angle", or "" when none is held. Kept here (not only in the +// session) so "Glasses default" can still restore them after an app restart. +const ORIGINAL_KEY = "glanceboard.headTiltOriginal"; + +function savedOriginal(): { switch: number; angle: number } | null { + const [sw, angle] = getStringSetting(ORIGINAL_KEY, "").split(",").map(Number); + return (sw === 0 || sw === 1) && Number.isInteger(angle) ? { switch: sw, angle } : null; +} + +/** + * Push the wanted trigger to the session (on change and on each connect): + * the Head tilt angle while the Glanceboard is enabled, else none, which + * writes back the glasses' own values if Faceclaw changed them. + */ +export function applyGlanceAngle(): boolean { + const sent = setHeadUpTrigger(glanceAngleTriggerDegrees(), savedOriginal()); + syncGlanceAngleOriginal(); + return sent; +} + +/** + * Keep the persisted original in step with the session: save the values the + * session captured before its first write, and drop them once a read shows + * them restored. Called on apply, from the controller's periodic refresh, and + * when the settings row draws. + */ +export function syncGlanceAngleOriginal(status: HeadUpStatus | null = getHeadUpStatus()): void { + if (!status) return; + const saved = savedOriginal(); + if (!saved) { + if (status.originalSwitch === 0 || status.originalSwitch === 1) { + setStringSetting(ORIGINAL_KEY, `${status.originalSwitch},${status.originalAngle}`); + } + return; + } + if ( + glanceAngleTriggerDegrees() === 0 && + status.desired < 0 && + !status.restorePending && + status.switch === saved.switch && + status.angle === saved.angle + ) { + setStringSetting(ORIGINAL_KEY, ""); + } +} + +/** The glasses' current head-up trigger (angle + calibration), 0 when off, or null when not read yet. */ +export function glassesHeadUpTrigger(status: HeadUpStatus | null = getHeadUpStatus()): number | null { + if (!status || status.switch < 0) return null; + return status.switch === 1 ? status.angle + status.calibration : 0; +} diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index 9842a207..6d9d617b 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -29,6 +29,8 @@ import { OsEventTypeList } from './events' import { loadDeviceAddresses } from './device-addresses' import { updateGlassesPresence } from './glasses-presence' import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from './firmware-compat' +import { keyboardService } from './keyboard/service' +import type { KeyEvent } from './keyboard/keys' import { isHaltedSessionPhase, resumeAutoReconnect, suppressAutoReconnect } from './reconnect-policy' import { WearRemote, type WearRemoteInputKind } from './wear-remote' import { deviceAddressError } from './ios-peripheral-identity' @@ -232,6 +234,7 @@ export class IosPreviewController { if (!on) void this.display?.setScreenBlanked(true).catch(error => this.fail(error)) this.requestShellRender() this.wearRemote?.schedulePublish() + if (on) keyboardService.retryNow() }, }) const launcher = createLauncherWindow({ @@ -270,6 +273,28 @@ export class IosPreviewController { assistantAvailable: () => shell.isAssistantAvailable(), assistant: text => shell.sendToAssistant(text), }) + // A keyboard paired through the glasses (the CFW keyboard bridge). The service decides what + // each key means (app/g2/keyboard/key-router.ts); its gestures share the controller queue + // with physical input, like the watch's. + const queueKeyboardInput = (run: () => Promise<void>): Promise<void> => { + const task = this.inputQueue.then(() => { this.onActivity(); return run() }) + this.inputQueue = task.catch(error => this.fail(error)) + return task + } + keyboardService.setHost({ + ready: () => this.state.phase === 'connected', + locked: () => this.glassesLocked, + wake: () => { void queueKeyboardInput(() => this.receiveInput(makeInputEvent({ type: 'display-wake' }))).catch(() => {}) }, + gesture: (gesture, origin) => queueKeyboardInput(async () => { + await this.receiveInput(makeInputEvent({ type: gesture, source: origin } as InputEventPayload)) + // A discrete hold includes its release. + if (gesture === 'long-press') await this.receiveInput(makeInputEvent({ type: 'long-press-release', source: origin })) + }), + log: line => this.logBluetooth(line), + // Only while the display is on: a keyboard that is switched off would otherwise keep the + // glasses' radio looking for it around the clock. + shouldReconnect: () => this.state.phase === 'connected' && shell.isScreenOn(), + }) // The header's EvenHub phone-UI button hides while that UI is showing. onPhoneShownChanged(() => this.syncEvenHubPhoneUi()) registerSystemTools() @@ -809,6 +834,7 @@ export class IosPreviewController { }, setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible), removeSurface: id => this.removeWindowSurface(id), + unhandledKey: event => keyboardService.handleUnhandledKey(event as KeyEvent), submitPixels: (id, pixels, width, height, draws) => { void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error)) }, @@ -830,6 +856,18 @@ export class IosPreviewController { this.state = { ...this.state, phase, status } this.logBluetooth(status) this.emitState() + this.syncKeyboardLink() + } + /** + * Tell the keyboard bridge whether the session can host a keyboard: a G2 on firmware with + * the bridge, connected. On a disconnect the user asked for, the link is still up when the + * phase turns, so the firmware is told to let the keyboard go first. + */ + private syncKeyboardLink(): void { + const up = !!this.communicator && this.state.phase === 'connected' && this.firmwareCompatible + && glassesTypeSetting.get() === 'g2' + if (up) keyboardService.linkReady() + else keyboardService.linkLost(this.state.phase === 'disconnecting') } /** Mirror the session into glasses-presence (read by EvenHub's device status and Timers). */ private syncPresence(): void { @@ -949,6 +987,7 @@ export class IosPreviewController { const firmwareWarning = firmwareIncompatibilityMessage(info) ?? '' if (firmwareWarning && firmwareWarning !== this.state.firmwareWarning) this.logBluetooth(`Firmware compatibility warning: ${firmwareWarning}`) this.firmwareCompatible = !firmwareWarning && hasCompatibleFirmware(info) + this.syncKeyboardLink() this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion, rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities, firmwareWarning } @@ -1013,6 +1052,13 @@ export class IosPreviewController { private handleRingEvent(input: RawInputEvent): void { this.inputQueue = this.inputQueue.then(async () => { if (this.state.phase !== 'connected') return + if (input.kind === 'sys-event' && input.eventType === OsEventTypeList.HEAD_DOWN_EVENT) { + // As on Android: only the Glanceboard uses it (it ends a head-tilt + // showing); it never reaches the shell or counts as activity. + const glanceEvent = this.glance.eventForGesture('head-down') + if (glanceEvent) await this.glance.handleEvent(glanceEvent, 0) + return + } this.onActivity() if (input.ringInput) this.state = { ...this.state, ring: true } await this.receiveInput(rawInputEventToInputEvent(input), @@ -1111,8 +1157,8 @@ export class IosPreviewController { /** * Continuous capture on behalf of `client` (one id per window: Transcribe, * Translate, Teleprompter, Calculator); it runs while any client wants it, as - * on Android. `recordAs` names a WAV of the audio in Downloads when Save - * Recording is on. + * on Android. `recordAs` names a recording (.mka) of the audio in Downloads + * when Save Recording is on. */ private startContinuousVoiceCapture(client: string, options: ContinuousCaptureOptions = {}): void { this.continuousClients.set(client, options) diff --git a/app/g2/keyboard/bindings.ts b/app/g2/keyboard/bindings.ts new file mode 100644 index 00000000..85606bfe --- /dev/null +++ b/app/g2/keyboard/bindings.ts @@ -0,0 +1,291 @@ +/** + * What keys from a hardware keyboard do, apart from typing: the built-in + * defaults, the user's custom keybindings, and how the two combine. + * + * A key becomes one of the gestures the ring and the watch already have, so + * nothing downstream knows about keyboards. Three layers, first match wins: + * + * 1. Custom keybindings (Settings > Keyboard > Keybindings). These apply + * everywhere, including where the focused window takes keys for itself + * (a terminal session), since that is the point of binding a remote's + * few buttons. + * 2. Built-in bindings that also apply there: Escape with Shift, Ctrl or + * Alt goes back, which is the way out of a terminal session; and a + * mouse's buttons and wheel, which are never typed. + * 3. Built-in navigation, only for keys no window took: the arrows are the + * watch's four swipes, Enter selects, Escape goes back. + * + * A keyboard's touchpad or mouse arrives here as keys too: "MouseButton1" + * (left), "MouseButton2" (right), "MouseButton3" (middle) and so on, and + * "WheelUp" / "WheelDown" / "WheelLeft" / "WheelRight" (see pointer.ts). + * + * No platform imports: this runs under node in the tests. + */ +import type { KeyEvent } from "./keys"; + +/** A gesture a key can stand for, named as the input pipeline names it. */ +export type KeyGesture = + | "swipe-up" + | "swipe-down" + | "swipe-left" + | "swipe-right" + | "scroll-up" + | "scroll-down" + | "click" + | "double-click" + | "long-press" + | "short-then-long-press"; + +/** + * What a custom keybinding does. "none" turns off the key's built-in + * navigation meaning; it does not stop the key being typed into a window + * that takes keys. + */ +export type KeyAction = KeyGesture | "none"; + +/** The actions, in the order the picker lists them. */ +export const KEY_ACTIONS: readonly { action: KeyAction; label: string }[] = [ + { action: "click", label: "Select" }, + { action: "double-click", label: "Back" }, + { action: "swipe-up", label: "Swipe up" }, + { action: "swipe-down", label: "Swipe down" }, + { action: "swipe-left", label: "Swipe left" }, + { action: "swipe-right", label: "Swipe right" }, + { action: "scroll-up", label: "Scroll up" }, + { action: "scroll-down", label: "Scroll down" }, + { action: "long-press", label: "System menu" }, + { action: "short-then-long-press", label: "App menu" }, + { action: "none", label: "Nothing" }, +]; + +export function keyActionLabel(action: KeyAction): string { + return KEY_ACTIONS.find((entry) => entry.action === action)?.label ?? action; +} + +function isKeyAction(value: unknown): value is KeyAction { + return KEY_ACTIONS.some((entry) => entry.action === value); +} + +/** A physical key with the modifiers held: what a keybinding is keyed on. */ +export type KeyChord = { code: string; shift: boolean; ctrl: boolean; alt: boolean; meta: boolean }; +export type KeyBinding = { chord: KeyChord; action: KeyAction }; + +const MODIFIER_CODES = new Set([ + "ControlLeft", "ControlRight", "ShiftLeft", "ShiftRight", "AltLeft", "AltRight", "MetaLeft", "MetaRight", +]); + +/** Shift, Ctrl, Alt and Meta themselves: they qualify a chord rather than being one. */ +export function isModifierCode(code: string): boolean { + return MODIFIER_CODES.has(code); +} + +export function chordOf(event: KeyEvent): KeyChord { + const { shift, ctrl, alt, meta } = event.modifiers; + return { code: event.code, shift, ctrl, alt, meta }; +} + +/** Canonical text of a chord ("Ctrl+Shift+Escape"): the stored form, and the lookup key. */ +export function chordId(chord: KeyChord): string { + return [chord.ctrl && "Ctrl", chord.alt && "Alt", chord.shift && "Shift", chord.meta && "Meta", chord.code] + .filter(Boolean).join("+"); +} + +/** The inverse of chordId, or null for anything it could not have produced. */ +export function parseChordId(text: string): KeyChord | null { + const parts = text.split("+"); + const code = parts.pop() ?? ""; + if (!/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(code) || isModifierCode(code)) return null; + const chord: KeyChord = { code, shift: false, ctrl: false, alt: false, meta: false }; + for (const part of parts) { + if (part === "Ctrl" && !chord.ctrl) chord.ctrl = true; + else if (part === "Alt" && !chord.alt) chord.alt = true; + else if (part === "Shift" && !chord.shift) chord.shift = true; + else if (part === "Meta" && !chord.meta) chord.meta = true; + else return null; + } + return chord; +} + +const CODE_LABELS: Record<string, string> = { + ArrowUp: "Up", ArrowDown: "Down", ArrowLeft: "Left", ArrowRight: "Right", + Escape: "Esc", PageUp: "Page Up", PageDown: "Page Down", CapsLock: "Caps Lock", + PrintScreen: "Print Screen", ScrollLock: "Scroll Lock", NumLock: "Num Lock", ContextMenu: "Menu", + Minus: "-", Equal: "=", BracketLeft: "[", BracketRight: "]", Backslash: "\\", IntlBackslash: "\\", + IntlHash: "#", Semicolon: ";", Quote: "'", Backquote: "`", Comma: ",", Period: ".", Slash: "/", + AudioVolumeUp: "Volume Up", AudioVolumeDown: "Volume Down", AudioVolumeMute: "Mute", + MediaPlayPause: "Play/Pause", MediaPlay: "Play", MediaPause: "Pause", MediaStop: "Stop", + MediaTrackNext: "Next Track", MediaTrackPrevious: "Previous Track", + BrightnessUp: "Brightness Up", BrightnessDown: "Brightness Down", + BrowserSearch: "Search", BrowserHome: "Browser Home", BrowserBack: "Browser Back", BrowserForward: "Browser Forward", + LaunchMail: "Mail", LaunchApplication1: "App 1", LaunchApplication2: "App 2", + MouseButton1: "Left button", MouseButton2: "Right button", MouseButton3: "Middle button", + WheelUp: "Wheel up", WheelDown: "Wheel down", WheelLeft: "Wheel left", WheelRight: "Wheel right", +}; + +/** A key as a person would name it. Unknown keys keep their HID usage number, which is all there is. */ +function codeLabel(code: string): string { + const known = CODE_LABELS[code]; + if (known) return known; + let match = /^Key([A-Z])$/.exec(code); + if (match) return match[1]!; + match = /^Digit([0-9])$/.exec(code); + if (match) return match[1]!; + match = /^Numpad(.+)$/.exec(code); + if (match) return `Numpad ${match[1]}`; + match = /^MouseButton([0-9]+)$/.exec(code); + if (match) return `Mouse button ${match[1]}`; + match = /^Usage([0-9a-f]+)$/.exec(code); + if (match) return `Key 0x${match[1]}`; + match = /^Consumer([0-9a-f]+)$/.exec(code); + if (match) return `Media key 0x${match[1]}`; + return code; +} + +/** A chord for display: "Shift+Esc", "Left", "Volume Up". */ +export function describeChord(chord: KeyChord): string { + return [chord.ctrl && "Ctrl", chord.alt && "Alt", chord.shift && "Shift", chord.meta && "Meta", codeLabel(chord.code)] + .filter(Boolean).join("+"); +} + +/** The most custom keybindings kept; far more than a keyboard has uses for. */ +export const MAX_KEY_BINDINGS = 64; + +/** + * Custom keybindings from their stored JSON. Lenient: whatever doesn't make + * sense is dropped, and a later entry for a chord replaces an earlier one. + */ +export function parseKeyBindings(raw: string): KeyBinding[] { + let parsed: unknown; + try { + parsed = raw ? JSON.parse(raw) : []; + } catch { + return []; + } + if (!Array.isArray(parsed)) return []; + const bindings = new Map<string, KeyBinding>(); + for (const entry of parsed) { + if (!entry || typeof entry !== "object") continue; + const { key, action } = entry as { key?: unknown; action?: unknown }; + const chord = typeof key === "string" ? parseChordId(key) : null; + if (!chord || !isKeyAction(action)) continue; + bindings.set(chordId(chord), { chord, action }); + if (bindings.size >= MAX_KEY_BINDINGS) break; + } + return [...bindings.values()]; +} + +export function serializeKeyBindings(bindings: readonly KeyBinding[]): string { + return JSON.stringify(bindings.map((binding) => ({ key: chordId(binding.chord), action: binding.action }))); +} + +/** Custom keybindings as the lookup resolveKey takes. */ +export function keyBindingMap(bindings: readonly KeyBinding[]): Map<string, KeyAction> { + return new Map(bindings.map((binding) => [chordId(binding.chord), binding.action])); +} + +/** What a mouse does unless bound otherwise: the ring's tap, double tap, hold and scroll. */ +const MOUSE_DEFAULTS: Record<string, KeyGesture | undefined> = { + MouseButton1: "click", + MouseButton2: "double-click", + MouseButton3: "long-press", + WheelUp: "scroll-up", + WheelDown: "scroll-down", +}; + +/** + * The built-in meaning of a key press, or null when it has none. + * `overridesTyping`: it holds even where the focused window takes keys. + * Built-ins go by the logical key, so both Enter keys select. + */ +export function defaultKeyAction(event: KeyEvent): { action: KeyGesture; overridesTyping: boolean } | null { + // A mouse's buttons and wheel are the ring's gestures: tap, double tap, + // hold, and scroll. Nothing types them, so they hold everywhere, whatever + // is held on the keyboard. + const mouse = MOUSE_DEFAULTS[event.key]; + if (mouse) return { action: mouse, overridesTyping: true }; + const { shift, ctrl, alt, meta } = event.modifiers; + if (meta) return null; + if (event.key === "Escape") { + // Plain Escape belongs to whatever takes keys (it is a keystroke in a + // terminal); with a modifier it always means "back out of here". + return { action: "double-click", overridesTyping: shift || ctrl || alt }; + } + if (ctrl || alt) return null; + switch (event.key) { + case "ArrowUp": + return { action: "swipe-up", overridesTyping: false }; + case "ArrowDown": + return { action: "swipe-down", overridesTyping: false }; + case "ArrowLeft": + return { action: "swipe-left", overridesTyping: false }; + case "ArrowRight": + return { action: "swipe-right", overridesTyping: false }; + case "Enter": + return { action: "click", overridesTyping: false }; + default: + return null; + } +} + +/** The built-ins, for showing to the user. Keep in step with defaultKeyAction. */ +export const DEFAULT_KEYS_HELP = + "Arrow keys: swipe up, down, left and right, as on the watch. Enter: select. Esc: back.\n\n" + + "In a terminal session those keys are typed instead, and Shift+Esc (or Ctrl+Esc, Alt+Esc) goes back.\n\n" + + "Mouse or touchpad: the left button selects, the right button goes back, the middle button opens the system menu, " + + "and the wheel scrolls. These work in a terminal session too.\n\n" + + "A custom keybinding replaces these for its key and works everywhere, terminals included. " + + "\"Nothing\" turns a key's meaning here off without stopping it being typed."; + +/** + * Whether holding the key repeats the gesture. Moving up and down a list + * does. Sideways swipes don't: depending on the layout they enter and leave + * things, and a held key would flap between the two. Selecting, backing out + * and menus act once per press. + */ +export function gestureRepeats(gesture: KeyGesture): boolean { + return gesture === "scroll-up" || gesture === "scroll-down" || gesture === "swipe-up" || gesture === "swipe-down"; +} + +export type KeyResolution = { + /** The key is spoken for: don't offer it to the focused window. */ + consume: boolean; + /** + * The gesture to perform: at once when `consume`, otherwise only if no + * window takes the key. Null for none (including a held key's repeats of + * a gesture that doesn't repeat). + */ + gesture: KeyGesture | null; +}; + +/** What one key press (or auto-repeat) means, given the custom keybindings. */ +export function resolveKey(event: KeyEvent, custom: ReadonlyMap<string, KeyAction>): KeyResolution { + const repeatable = (gesture: KeyGesture): KeyGesture | null => + event.repeat && !gestureRepeats(gesture) ? null : gesture; + const bound = custom.get(chordId(chordOf(event))); + if (bound !== undefined && bound !== "none") return { consume: true, gesture: repeatable(bound) }; + // "none" takes the built-in meaning away and leaves the key to be typed. + if (bound === "none") return { consume: false, gesture: null }; + const builtIn = defaultKeyAction(event); + if (!builtIn) return { consume: false, gesture: null }; + return { consume: builtIn.overridesTyping, gesture: repeatable(builtIn.action) }; +} + +export type GestureOrigin = "ring" | "watch"; + +/** + * Which device a keyboard's gesture is presented as. Swipes are the watch's + * and scrolls the ring's; the two schemes differ in places (an icon grid + * moves by cell for the watch, by row and then cell for the ring). A tap, a + * double tap or a hold exists in both, so it goes with whichever the + * keyboard last moved with: arrow keys make the keyboard a watch, a remote + * bound to scroll makes it a ring. Before any movement, the watch. + */ +export class GestureScheme { + private scheme: GestureOrigin = "watch"; + + originFor(gesture: KeyGesture): GestureOrigin { + if (gesture.startsWith("swipe-")) this.scheme = "watch"; + else if (gesture.startsWith("scroll-")) this.scheme = "ring"; + return this.scheme; + } +} diff --git a/app/g2/keyboard/bridge.ts b/app/g2/keyboard/bridge.ts new file mode 100644 index 00000000..bfe53593 --- /dev/null +++ b/app/g2/keyboard/bridge.ts @@ -0,0 +1,566 @@ +/** + * The phone side of the CFW keyboard bridge for one glasses session: the + * client, the remembered keyboard, reconnecting it, and what the UI shows. + * The session hands it field-131 records and a way to send controls; the + * rest stays in here. Modelled on the Glassly app's G2KeyboardBridge (MIT). + * + * The firmware keeps nothing across a reboot (it comes back with the bridge + * off), and holds the keyboard's connection only while told to. So the + * remembered keyboard is brought back whenever the glasses link comes up and + * whenever the keyboard drops, backing off while it stays away. The + * discovered handle mapping is good for one connection and is never stored. + * No platform imports: this runs under node in the tests. + */ +import { KeyboardClient, KeyboardClientError, type KeyboardStatus } from "./client"; +import type { HidKeyEvent, HidPointerSample } from "./hid"; +import { + KeyboardDm, + KeyboardError, + mergeKeyboardAdverts, + parseKeyboardAddress, + type KeyboardAdvert, +} from "./protocol"; + +export type SavedKeyboard = { address: string; addressType: number; name: string }; + +export type KeyboardBridgeState = + /** No glasses link that can host a keyboard. */ + | "unavailable" + | "idle" + | "scanning" + | "connecting" + | "pairing" + | "configuring" + | "connected" + | "error"; + +/** A pairing step waiting on the user. */ +export type KeyboardPairingPrompt = + /** Type this on the keyboard, then Enter. */ + | { kind: "display"; pin: number } + /** Confirm both sides show this value (answerCompare). */ + | { kind: "compare"; value: number } + /** The keyboard shows a code to enter here (answerPasskey). */ + | { kind: "entry" }; + +export type KeyboardBridgeSnapshot = { + state: KeyboardBridgeState; + saved: SavedKeyboard | null; + /** Name of the keyboard being connected or in use. */ + activeName: string; + error: string; + pairing: KeyboardPairingPrompt | null; + /** Advertisers from the last scan, HID devices first, strongest first. */ + scanResults: KeyboardAdvert[]; + /** A user-started scan or connect is running. */ + busy: boolean; + /** The remembered keyboard is away and the bridge keeps trying to bring it back. */ + waiting: boolean; + /** The connected keyboard has a touchpad or mouse buttons. */ + pointer: boolean; +}; + +export type KeyboardBridgeHost = { + /** Queue a control record on the live session; false when there is none. */ + send(record: Uint8Array): boolean; + loadSaved(): SavedKeyboard | null; + storeSaved(saved: SavedKeyboard | null): void; + /** Key transitions from the keyboard (including forced releases). */ + onKeys(events: HidKeyEvent[]): void; + /** A report from the keyboard's touchpad or mouse (including a forced release of its buttons). */ + onPointer(sample: HidPointerSample): void; + /** The keyboard stopped being usable; drop held-key state without releases. */ + onKeyboardGone(): void; + /** Something in snapshot() changed. */ + onChanged(): void; + /** + * A line for the log. `routine` marks the ones that repeat for as long as + * a remembered keyboard stays away, which only a verbose log wants. + */ + log(line: string, routine?: boolean): void; + /** A fresh six-digit pairing passkey from a good random source. */ + randomPasskey?(): number; + /** + * Whether looking for an absent keyboard is worthwhile now. Each attempt + * has the glasses' radio listening for the keyboard for up to half a + * minute, so the owner limits it to when the glasses are in use, and calls + * retryNow() when that starts. Default: always. + */ + shouldReconnect?(): boolean; +}; + +/** Delay before each automatic attempt after a failure; the last one repeats. */ +const RECONNECT_BACKOFF_MS = [2_000, 5_000, 10_000, 20_000, 30_000]; +/** After the glasses link comes up, before the first attempt. */ +const RESUME_DELAY_MS = 1_000; +/** After the keyboard drops (it may just be going to sleep). */ +const DROP_RETRY_DELAY_MS = 2_000; +const RELEASE_POLLS = 20; +const RELEASE_POLL_MS = 150; + +export class KeyboardBridge { + private client: KeyboardClient | null = null; + private available = false; + private state: KeyboardBridgeState = "unavailable"; + private saved: SavedKeyboard | null; + private activeName = ""; + private error = ""; + private pairing: KeyboardPairingPrompt | null = null; + private scanResults: KeyboardAdvert[] = []; + /** A user-started scan/connect holds the bridge. */ + private busy = false; + /** The keyboard is connected, encrypted and subscribed. */ + private ready = false; + private userDisconnected = false; + /** The keyboard was let go while the glasses were out of reach; tell the firmware when they are back. */ + private releaseOnLink = false; + private reconnectTimer: ReturnType<typeof setTimeout> | null = null; + private reconnectAttempt = 0; + /** Bumped whenever the client is replaced; work started for an older one stops quietly. */ + private generation = 0; + + constructor(private readonly host: KeyboardBridgeHost) { + this.saved = host.loadSaved(); + } + + snapshot(): KeyboardBridgeSnapshot { + return { + state: this.state, + saved: this.saved, + activeName: this.activeName, + error: this.error, + pairing: this.pairing, + scanResults: sortAdverts(this.scanResults), + busy: this.busy, + waiting: this.available && !!this.saved && !this.ready && !this.userDisconnected && !this.busy, + pointer: this.ready && this.client?.configuration?.pointer === true, + }; + } + + /** True while key events can arrive. */ + isConnected(): boolean { + return this.ready; + } + + // ---------- Link lifecycle (driven by the session owner) ---------- + + /** The glasses link is up on firmware with the bridge: bring the remembered keyboard back. */ + linkReady(): void { + if (this.available) return; + this.available = true; + this.userDisconnected = false; + this.reconnectAttempt = 0; + this.error = ""; + const client = this.replaceClient(); + this.setState("idle"); + if (this.saved && !this.releaseOnLink) { + this.scheduleReconnect(RESUME_DELAY_MS); + } else if (this.releaseOnLink && client) { + this.userDisconnected = !!this.saved; + client.query().then((status) => { + if (status.enabled || status.conn) return client.disable(); + }).catch(() => {}); + } + this.releaseOnLink = false; + } + + /** The glasses link is gone. Only the remembered keyboard survives. */ + linkLost(): void { + if (!this.available && !this.client) return; + this.available = false; + this.cancelReconnect(); + this.generation++; + this.client?.close(); + this.client = null; + this.busy = false; + this.pairing = null; + this.error = ""; + this.dropKeyboard(); + this.setState("unavailable"); + } + + /** + * The app is about to close the glasses link on purpose: tell the firmware + * to let the keyboard go, so it isn't left attached to glasses nobody is + * listening to. Best effort; nothing waits for the answer. + */ + linkClosing(): void { + const client = this.client; + if (!client || !this.available || !client.status.enabled) return; + client.disable().catch(() => {}); + } + + /** One field-131 payload from the session. */ + accept(payload: Uint8Array): void { + this.client?.accept(payload); + } + + /** + * The keyboard may be reachable now, or is wanted now (the display woke): + * if it is away and the bridge is waiting out a backoff, try at once. + */ + retryNow(): void { + if (!this.available || !this.saved || this.ready || this.userDisconnected || this.busy) return; + // No timer means an attempt is already in flight. + if (this.reconnectTimer === null) return; + this.reconnectAttempt = 0; + this.scheduleReconnect(0); + } + + // ---------- Commands (the settings UI) ---------- + + /** Scan for about five seconds; results accumulate in the snapshot as they arrive. */ + async scan(): Promise<KeyboardAdvert[]> { + const client = this.beginUserOperation(); + const generation = this.generation; + this.scanResults = []; + this.error = ""; + this.setState("scanning"); + try { + await this.releaseLink(client); + await client.enable(); + await client.scan(); + if (generation === this.generation) this.setState("idle"); + } catch (e) { + if (generation === this.generation) this.fail(e); + throw friendly(e); + } finally { + if (generation === this.generation) this.endUserOperation(); + } + return sortAdverts(this.scanResults); + } + + /** Connect, pair and subscribe a keyboard, then remember it. */ + async connect(keyboard: SavedKeyboard): Promise<void> { + if (!parseKeyboardAddress(keyboard.address) || keyboard.addressType < 0 || keyboard.addressType > 3) { + throw new KeyboardClientError("Invalid keyboard address"); + } + const client = this.beginUserOperation(); + const generation = this.generation; + this.userDisconnected = false; + try { + await this.connectWith(client, generation, keyboard, false); + } finally { + if (generation === this.generation) this.endUserOperation(); + } + } + + /** Try the remembered keyboard again now. */ + async reconnect(): Promise<void> { + if (!this.saved) throw new KeyboardClientError("No remembered keyboard"); + await this.connect(this.saved); + } + + /** Drop the keyboard but keep it remembered. No automatic reconnect until the next explicit connect or glasses session. */ + async disconnect(): Promise<void> { + this.userDisconnected = true; + this.cancelReconnect(); + const client = this.replaceClient(); + // A scan or connect this interrupts no longer owns the bridge. + this.busy = false; + this.dropKeyboard(); + this.pairing = null; + this.error = ""; + if (!client) { + this.releaseOnLink = true; + this.setState("unavailable"); + return; + } + this.setState("idle"); + try { + await client.query(); + await client.disable(); + } catch (e) { + this.host.log(`keyboard disconnect: ${messageOf(e)}`); + } + } + + /** Forget the remembered keyboard. The glasses keep their bond with it. */ + async forget(): Promise<void> { + this.saved = null; + this.host.storeSaved(null); + await this.disconnect(); + this.scanResults = []; + this.activeName = ""; + this.host.onChanged(); + } + + /** Answer a "compare" pairing prompt. */ + async answerCompare(accept: boolean): Promise<void> { + if (!this.client || this.pairing?.kind !== "compare") throw new KeyboardClientError("No pairing comparison is pending"); + this.pairing = null; + this.host.onChanged(); + await this.client.confirm(accept); + } + + /** Answer an "entry" pairing prompt with the code the keyboard shows. */ + async answerPasskey(pin: number): Promise<void> { + if (!this.client || this.pairing?.kind !== "entry") throw new KeyboardClientError("No pairing code is pending"); + this.pairing = null; + this.host.onChanged(); + await this.client.authenticate(pin); + } + + // ---------- Internals ---------- + + private beginUserOperation(): KeyboardClient { + if (!this.available) throw new KeyboardClientError("Connect to G2 glasses running Faceclaw firmware first"); + if (this.busy) throw new KeyboardClientError("Another keyboard operation is in progress"); + // An automatic attempt may be mid-flight (a sleeping keyboard holds one + // for up to 30 s): a fresh client fails it at once and takes over. + this.cancelReconnect(); + const client = this.replaceClient()!; + this.dropKeyboard(); + this.pairing = null; + this.busy = true; + return client; + } + + private endUserOperation(): void { + this.busy = false; + this.host.onChanged(); + } + + /** Close the current client and, while the link is up, start a fresh one. */ + private replaceClient(): KeyboardClient | null { + this.generation++; + this.client?.close(); + this.client = null; + if (!this.available) return null; + const generation = this.generation; + const live = () => generation === this.generation; + const client = new KeyboardClient( + (record) => live() && this.host.send(record), + { + keys: (events) => { if (live() && this.ready) this.host.onKeys(events); }, + pointer: (sample) => { if (live() && this.ready) this.host.onPointer(sample); }, + advert: (advert) => { if (live()) this.addAdvert(advert); }, + status: (status) => { if (live()) this.onStatus(status); }, + dm: (event, status) => { if (live()) this.onDm(event, status); }, + passkeyDisplay: (pin) => { if (live()) this.prompt({ kind: "display", pin }); }, + passkeyRequest: () => { if (live()) this.prompt({ kind: "entry" }); }, + compareRequest: (value) => { if (live()) this.prompt({ kind: "compare", value }); }, + servicesChanged: () => { if (live()) this.onServicesChanged(); }, + error: (message) => { if (live()) this.host.log(`keyboard: ${message}`); }, + log: (line) => { if (live()) this.host.log(`keyboard setup: ${line}`); }, + }, + { randomPasskey: this.host.randomPasskey ? () => this.host.randomPasskey!() : undefined }, + ); + this.client = client; + return client; + } + + private async connectWith( + client: KeyboardClient, + generation: number, + keyboard: SavedKeyboard, + resume: boolean, + ): Promise<void> { + const live = () => generation === this.generation; + this.error = ""; + this.activeName = keyboard.name; + this.setState("connecting"); + try { + const status = await client.query(); + if (resume && status.enabled && status.connected && status.encrypted) { + // The glasses kept the keyboard through a phone-side drop (or an app + // restart): only the handle mapping is missing. + this.host.log("keyboard still connected to the glasses; rediscovering"); + } else { + await this.releaseLink(client); + await client.enable(); + await client.connect(keyboard.addressType, parseKeyboardAddress(keyboard.address)!); + } + if (!live()) return; + this.pairing = null; + this.setState("configuring"); + const config = await client.configure(); + if (!live()) return; + this.ready = true; + this.reconnectAttempt = 0; + // The list was for choosing; it is stale from here on. + this.scanResults = []; + if (!sameKeyboard(this.saved, keyboard)) { + this.saved = { ...keyboard }; + this.host.storeSaved(this.saved); + } + this.host.log( + `keyboard ready: ${keyboard.name || keyboard.address}, ${config.watched.length} of ` + + `${config.reports.filter((r) => r.type === 1).length} input reports subscribed` + + `${config.pointer ? ", with a pointer" : ""}, epoch ${config.epoch}`); + this.setState("connected"); + } catch (e) { + if (!live()) return; + this.ready = false; + this.pairing = null; + if (resume) { + // An automatic attempt: a keyboard that is asleep or out of range is + // the ordinary case, not an error to put in front of the user. + this.host.log(`keyboard reconnect: ${messageOf(friendly(e))}`, true); + this.setState("idle"); + } else { + this.fail(e); + } + throw friendly(e); + } + } + + /** + * The firmware refuses SCAN and CONNECT while it still owns a keyboard + * slot, and an interrupted connect keeps initiating until the close lands. + * DISABLE is epoch-checked and a fresh client hasn't seen the epoch yet, + * so QUERY first (it isn't, and its answer carries the epoch), then wait + * for the slot to clear. + */ + private async releaseLink(client: KeyboardClient): Promise<void> { + const status = await client.query(); + if (!status.enabled && status.conn === 0) return; + await client.disable(); + for (let i = 0; i < RELEASE_POLLS; i++) { + if ((await client.query()).conn === 0) return; + await delay(RELEASE_POLL_MS); + } + throw new KeyboardClientError("The glasses are still closing the previous keyboard link; try again in a moment"); + } + + private cancelReconnect(): void { + if (this.reconnectTimer !== null) clearTimeout(this.reconnectTimer); + this.reconnectTimer = null; + } + + private scheduleReconnect(delayMs: number): void { + this.cancelReconnect(); + this.reconnectTimer = setTimeout(() => { + this.reconnectTimer = null; + void this.attemptReconnect(); + }, delayMs); + } + + private async attemptReconnect(): Promise<void> { + const saved = this.saved; + const client = this.client; + if (!this.available || !client || !saved || this.userDisconnected || this.ready || this.busy) return; + if (this.host.shouldReconnect && !this.host.shouldReconnect()) { + this.parkFirmware(client); + this.scheduleReconnect(RECONNECT_BACKOFF_MS[RECONNECT_BACKOFF_MS.length - 1]!); + return; + } + const generation = this.generation; + try { + await this.connectWith(client, generation, saved, true); + } catch { + if (generation !== this.generation || !this.available || this.userDisconnected) return; + // Keyboards sleep, and the ring's own scan can hold the controller + // (BUSY): keep trying, less often, while the glasses stay up. + const backoff = RECONNECT_BACKOFF_MS[Math.min(this.reconnectAttempt, RECONNECT_BACKOFF_MS.length - 1)]!; + this.reconnectAttempt++; + this.scheduleReconnect(backoff); + } + } + + /** + * While it is enabled the firmware bridge runs a 20 ms timer and reports + * its status every second, keyboard or no keyboard. When nothing will be + * tried for a while, switch it off; the next attempt enables it again. + */ + private parkFirmware(client: KeyboardClient): void { + if (!client.status.enabled) return; + client.disable().catch(() => {}); + } + + private onStatus(status: KeyboardStatus): void { + if (this.ready && (!status.connected || !status.encrypted)) this.keyboardDropped("status"); + else if (this.state === "connecting" && status.connected && !status.encrypted) this.setState("pairing"); + } + + private onDm(event: number, status: number): void { + if (event === KeyboardDm.connClose || event === KeyboardDm.pairFailed || event === KeyboardDm.encryptFailed) { + this.host.log(`keyboard DM event 0x${event.toString(16)} status ${status}`); + if (this.ready) this.keyboardDropped("dm"); + } + } + + private onServicesChanged(): void { + this.host.log("keyboard services changed; rediscovering"); + this.dropKeyboard(); + this.setState("idle"); + if (this.saved && !this.userDisconnected && !this.busy) this.scheduleReconnect(0); + } + + private keyboardDropped(reason: string): void { + this.host.log(`keyboard disconnected (${reason})`); + this.dropKeyboard(); + this.setState("idle"); + if (this.saved && !this.userDisconnected && !this.busy) this.scheduleReconnect(DROP_RETRY_DELAY_MS); + } + + /** The keyboard is no longer usable: tell the host once. */ + private dropKeyboard(): void { + if (!this.ready) return; + this.ready = false; + this.host.onKeyboardGone(); + } + + private addAdvert(advert: KeyboardAdvert): void { + const index = this.scanResults.findIndex( + (a) => a.address === advert.address && a.addressType === advert.addressType); + if (index < 0) { + this.scanResults.push(advert); + } else { + const merged = mergeKeyboardAdverts(this.scanResults[index]!, advert); + const old = this.scanResults[index]!; + if (merged.name === old.name && merged.hid === old.hid && merged.rssi === old.rssi) return; + this.scanResults[index] = merged; + } + this.host.onChanged(); + } + + private prompt(pairing: KeyboardPairingPrompt): void { + this.pairing = pairing; + this.setState("pairing"); + } + + private setState(next: KeyboardBridgeState): void { + this.state = next; + if (next !== "pairing" && next !== "connecting") this.pairing = null; + this.host.onChanged(); + } + + private fail(error: unknown): void { + this.error = messageOf(friendly(error)); + this.host.log(`keyboard error: ${this.error}`); + this.setState("error"); + } +} + +function sameKeyboard(a: SavedKeyboard | null, b: SavedKeyboard): boolean { + return !!a && a.address === b.address && a.addressType === b.addressType && a.name === b.name; +} + +function sortAdverts(adverts: readonly KeyboardAdvert[]): KeyboardAdvert[] { + return [...adverts].sort((a, b) => Number(b.hid) - Number(a.hid) || b.rssi - a.rssi); +} + +/** + * Firmware BUSY nearly always means the ring's own scan or reconnect holds + * the radio; say that rather than the raw control error. + */ +function friendly(error: unknown): Error { + if (error instanceof KeyboardClientError && error.code === KeyboardError.busy) { + return new KeyboardClientError( + "The glasses' radio is busy (usually the ring reconnecting). Wait a few seconds and try again.", error.code); + } + if (error instanceof KeyboardClientError && error.code === KeyboardError.noSpace) { + return new KeyboardClientError( + "The glasses have no room for another connection or pairing.", error.code); + } + return error instanceof Error ? error : new Error(String(error)); +} + +function messageOf(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +function delay(ms: number): Promise<void> { + return new Promise<void>((resolve) => setTimeout(resolve, ms)); +} diff --git a/app/g2/keyboard/client.ts b/app/g2/keyboard/client.ts new file mode 100644 index 00000000..627fc14c --- /dev/null +++ b/app/g2/keyboard/client.ts @@ -0,0 +1,754 @@ +/** + * Transport-independent client for the CFW keyboard bridge: enable, scan, + * connect and pair a keyboard through the glasses, discover its HID service + * over the relayed ATT operations, subscribe its input reports, and decode + * them into key transitions. From the reference client in g2flash demos/ + * (keyboard-client.ts), with the fixes its use in the Glassly app turned up. + * + * The firmware keeps almost no policy: it owns the connection and the bond, + * and forwards one ATT request at a time. Everything HID lives here. Handle + * numbers found by configure() are only good for one connection epoch. + * No platform imports: this runs under node in the tests. + */ +import { + HidKeys, + parseReportMap, + reportHasKeys, + reportHasPointer, + reportPointer, + type HidKeyEvent, + type HidPointerSample, + type HidReport, +} from "./hid"; +import { + KeyboardDm, + KeyboardError, + KeyboardKind, + KeyboardOp, + KeyboardRecords, + keyboardControl, + keyboardErrorName, + le16, + parseKeyboardAdvert, + u16, + u32, + type KeyboardAdvert, + type KeyboardRecord, +} from "./protocol"; + +/** Queue one field-130 control record; false when the glasses link can't take it now. */ +export type KeyboardTransport = (record: Uint8Array) => boolean; + +export type KeyboardStatus = { + enabled: boolean; + /** The firmware's connection id for the keyboard, 0 when it owns none. */ + conn: number; + connected: boolean; + encrypted: boolean; + scanning: boolean; +}; + +export type KeyboardClientEvents = { + /** Key transitions from one input report (or the releases a gap forces). */ + keys?(events: HidKeyEvent[]): void; + /** + * One report from the keyboard's touchpad or mouse: the buttons now held + * (across all its pointer reports) and the movement and wheel since the + * last one. Also sent with everything zero when a gap forces a release. + */ + pointer?(sample: HidPointerSample): void; + advert?(advert: KeyboardAdvert): void; + status?(status: KeyboardStatus): void; + /** The stream may have lost records; held keys were released. */ + gap?(): void; + /** Device-manager event from the glasses' BLE host (KeyboardDm). */ + dm?(event: number, status: number): void; + /** The keyboard shows a six-digit passkey; answer with authenticate(). */ + passkeyRequest?(): void; + /** We chose a passkey and answered with it; the user types it on the keyboard, then Enter. */ + passkeyDisplay?(pin: number): void; + /** Numeric comparison: answer with confirm() once the user has compared. */ + compareRequest?(value: number): void; + /** The keyboard's GATT table changed; the mapping is void until configure() runs again. */ + servicesChanged?(): void; + error?(message: string): void; + /** Setup diagnostics: the keyboard's attribute table and what was made of it. Never key contents. */ + log?(line: string): void; +}; + +export type KeyboardReport = { + /** Handle of the HID service declaration the report belongs to. */ + service: number; + handle: number; + id: number; + /** 1 input, 2 output, 3 feature. */ + type: number; + ccc?: number; + /** From the characteristic's declaration; undefined when the keyboard wouldn't say (see declaredProperties). */ + properties?: number; + map: Uint8Array; +}; + +export type KeyboardConfiguration = { + epoch: number; + /** Every report found, input or not. */ + reports: KeyboardReport[]; + /** Handles of the input reports subscribed: those that carry keys, or a pointer's buttons and movement. */ + watched: number[]; + /** Whether any of them is a pointer's (a touchpad, mouse buttons, a wheel). */ + pointer: boolean; + serviceChanged?: number; +}; + +export type KeyboardClientOptions = { + /** A fresh six-digit passkey for display pairing. Default: Math.random (pass a stronger source). */ + randomPasskey?: () => number; +}; + +export class KeyboardClientError extends Error { + /** The firmware's control error code (KeyboardError), when that is what failed. */ + readonly code: number | undefined; + /** The keyboard's ATT error code, when it refused a read or write. */ + readonly attStatus: number | undefined; + constructor(message: string, code?: number, attStatus?: number) { + super(message); + this.name = "KeyboardClientError"; + this.code = code; + this.attStatus = attStatus; + } +} + +type Attribute = { handle: number; uuid: number | string }; +type Pending = { + op: number; + resolve: (r: KeyboardRecord) => void; + reject: (e: Error) => void; + timer: ReturnType<typeof setTimeout>; +}; +type Waiter = { test: () => boolean; resolve: () => void; reject: (e: Error) => void; timer: ReturnType<typeof setTimeout> }; +type Input = { report: HidReport; id: number; service: number; keys: boolean; pointer: boolean }; + +const COMMAND_TIMEOUT_MS = 32_000; +const MAX_PENDING = 8; +const MAX_ATTRIBUTES = 512; +const MAX_VALUE = 512; +const MAX_WATCHES = 16; + +const UUID_PRIMARY_SERVICE = 0x2800; +const UUID_SECONDARY_SERVICE = 0x2801; +const UUID_CHARACTERISTIC = 0x2803; +const UUID_CCC = 0x2902; +const UUID_EXTERNAL_REPORT_REFERENCE = 0x2907; +const UUID_REPORT_REFERENCE = 0x2908; +const UUID_HID_SERVICE = 0x1812; +const UUID_SERVICE_CHANGED = 0x2a05; +const UUID_REPORT_MAP = 0x2a4b; +const UUID_REPORT = 0x2a4d; +const UUID_PROTOCOL_MODE = 0x2a4e; + +/** ATT error codes that end a paged read rather than failing it. */ +const ATT_INVALID_OFFSET = 7; +const ATT_NOT_LONG = 11; +const ATT_NOT_FOUND = 10; + +/** + * ATT UUIDs are little endian. A 128-bit UUID on the Bluetooth base collapses + * to its 16-bit form; vendor UUIDs stay as hex strings. + */ +export function attributeUuid(bytes: Uint8Array): number | string { + if (bytes.length === 2) return u16(bytes); + if (bytes.length !== 16) throw new KeyboardClientError("Invalid ATT UUID length"); + const base = [0xfb, 0x34, 0x9b, 0x5f, 0x80, 0, 0, 0x80, 0, 0x10, 0, 0]; + if (base.every((v, i) => bytes[i] === v) && bytes[14] === 0 && bytes[15] === 0) return u16(bytes, 12); + return Array.from(bytes, (x) => x.toString(16).padStart(2, "0")).join(""); +} + +export class KeyboardClient { + /** The firmware's connection generation; every control but QUERY/ENABLE must carry the live one. */ + epoch = 0; + status: KeyboardStatus = { enabled: false, conn: 0, connected: false, encrypted: false, scanning: false }; + configuration: KeyboardConfiguration | undefined; + + private serial = 0; + private closed = false; + /** The link under connect() went away (close, failed open, failed pairing). */ + private linkLost = false; + private readonly pending = new Map<number, Pending>(); + private readonly waiters = new Set<Waiter>(); + private readonly keys = new HidKeys(); + /** Pointer buttons held, by the report that says so (as HidKeys does for keys). */ + private readonly pointerButtons = new Map<number, number>(); + private inputs = new Map<number, Input>(); + private readonly records = new KeyboardRecords(() => this.gap()); + private readonly randomPasskey: () => number; + + constructor( + private readonly transport: KeyboardTransport, + private readonly events: KeyboardClientEvents = {}, + options: KeyboardClientOptions = {}, + ) { + this.randomPasskey = options.randomPasskey ?? (() => Math.floor(Math.random() * 1_000_000)); + } + + // ---------- Inbound ---------- + + /** Feed one field-131 payload, in arrival order. */ + accept(data: Uint8Array): void { + if (this.closed) return; + const r = this.records.push(data); + if (!r) return; + this.epoch = r.epoch; + if (r.kind === KeyboardKind.status && r.data.length === 10) { + this.status = { + enabled: !!r.data[0], conn: r.data[1]!, connected: !!r.data[2], encrypted: !!r.data[3], scanning: !!r.data[4], + }; + if (!this.status.enabled || !this.status.connected || !this.status.encrypted) this.release(); + this.events.status?.(this.status); + } + const pending = this.pending.get(r.request); + if (pending && (r.kind === KeyboardKind.status || r.kind === KeyboardKind.att)) { + this.pending.delete(r.request); + clearTimeout(pending.timer); + if (r.kind === KeyboardKind.status && r.code) { + pending.reject(new KeyboardClientError( + `Keyboard command ${opName(pending.op)}: firmware ${keyboardErrorName(r.code)}`, r.code)); + } else { + pending.resolve(r); + } + } + if (r.kind === KeyboardKind.dm) this.acceptDm(r); + else if (r.kind === KeyboardKind.input) this.acceptInput(r); + else if (r.kind === KeyboardKind.advert) { + const advert = parseKeyboardAdvert(r.data); + if (advert) this.events.advert?.(advert); + } + this.checkWaiters(); + } + + private acceptDm(r: KeyboardRecord): void { + const event = r.code & 255; + const status = r.code >>> 8; + if (event === KeyboardDm.connClose || event === KeyboardDm.pairFailed || event === KeyboardDm.encryptFailed || + (event === KeyboardDm.connOpen && status !== 0)) { + this.linkLost = true; + this.gap(); + this.inputs.clear(); + this.configuration = undefined; + } + if (event === KeyboardDm.authRequest && r.data.length === 2) { + if (r.data[0]) { + this.command(KeyboardOp.disconnect).catch(() => {}); + this.events.error?.("This keyboard only pairs out of band, which isn't supported."); + } else if (r.data[1]) { + // KeyboardDisplay on our side: choose the passkey, show it, and + // answer at once; the user types it on the keyboard. + const pin = Math.min(999_999, Math.max(0, Math.floor(this.randomPasskey()))); + this.events.passkeyDisplay?.(pin); + this.authenticate(pin).catch((e) => this.events.error?.(messageOf(e))); + } else { + this.events.passkeyRequest?.(); + } + } + if (event === KeyboardDm.compare && r.data.length === 4) this.events.compareRequest?.(u32(r.data)); + this.events.dm?.(event, status); + } + + private acceptInput(r: KeyboardRecord): void { + const changed = this.configuration?.serviceChanged; + if (changed !== undefined && r.handle === changed) { + this.gap(); + this.inputs.clear(); + this.configuration = undefined; + this.events.servicesChanged?.(); + return; + } + const input = this.inputs.get(r.handle); + if (!input || this.configuration?.epoch !== r.epoch) { + this.gap(); + return; + } + if (input.keys) { + try { + const events = this.keys.update(r.handle, input.report, r.data); + if (events.length) this.events.keys?.(events); + } catch (e) { + this.release(); + this.events.error?.(messageOf(e)); + } + } + if (input.pointer) { + const sample = reportPointer(input.report, r.data); + this.pointerButtons.set(r.handle, sample.buttons); + this.events.pointer?.({ ...sample, buttons: this.heldButtons() }); + } + } + + private heldButtons(): number { + let buttons = 0; + for (const held of this.pointerButtons.values()) buttons |= held; + return buttons; + } + + /** Nothing is held any more, as far as anyone can tell: say so. */ + private release(): void { + const events = this.keys.reset(); + if (events.length) this.events.keys?.(events); + const buttons = this.heldButtons(); + this.pointerButtons.clear(); + if (buttons) this.events.pointer?.({ buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }); + } + + private gap(): void { + this.release(); + this.events.gap?.(); + for (const [id, p] of this.pending) { + if (p.op < KeyboardOp.find) continue; + clearTimeout(p.timer); + this.pending.delete(id); + p.reject(new KeyboardClientError("Keyboard stream gap during ATT transaction")); + } + } + + /** The phone's link to the glasses dropped: release held keys and fail everything in flight. */ + lostPhoneLink(): void { + this.records.reset(); + this.cancel("Glasses link lost"); + } + + /** Stop for good; later accept() calls are ignored. */ + close(): void { + if (this.closed) return; + this.lostPhoneLink(); + this.closed = true; + } + + private cancel(reason: string): void { + for (const p of this.pending.values()) { + clearTimeout(p.timer); + p.reject(new KeyboardClientError(reason)); + } + this.pending.clear(); + for (const w of this.waiters) { + clearTimeout(w.timer); + w.reject(new KeyboardClientError(reason)); + } + this.waiters.clear(); + } + + private checkWaiters(): void { + for (const w of [...this.waiters]) { + let done = false; + try { + done = w.test(); + } catch (e) { + this.waiters.delete(w); + clearTimeout(w.timer); + w.reject(e instanceof Error ? e : new KeyboardClientError(String(e))); + continue; + } + if (!done) continue; + this.waiters.delete(w); + clearTimeout(w.timer); + w.resolve(); + } + } + + /** Resolve once `test` holds; it is re-evaluated after every accepted record. */ + private waitFor(test: () => boolean, timeoutMs: number, what: string): Promise<void> { + if (this.closed) return Promise.reject(new KeyboardClientError("Keyboard client closed")); + if (test()) return Promise.resolve(); + return new Promise<void>((resolve, reject) => { + const waiter: Waiter = { + test, resolve, reject, + timer: setTimeout(() => { + this.waiters.delete(waiter); + reject(new KeyboardClientError(`Timed out waiting for ${what}`)); + }, timeoutMs), + }; + this.waiters.add(waiter); + }); + } + + // ---------- Commands ---------- + + /** Send one control and await the STATUS or ATT record that answers it. */ + command(op: number, body: readonly number[] = []): Promise<KeyboardRecord> { + if (this.closed) return Promise.reject(new KeyboardClientError("Keyboard client closed")); + if (this.pending.size >= MAX_PENDING) return Promise.reject(new KeyboardClientError("Too many keyboard commands")); + const request = this.serial = this.serial % 65535 + 1; + return new Promise<KeyboardRecord>((resolve, reject) => { + const timer = setTimeout(() => { + this.pending.delete(request); + reject(new KeyboardClientError(`Keyboard command ${opName(op)} timed out`)); + }, COMMAND_TIMEOUT_MS); + this.pending.set(request, { op, resolve, reject, timer }); + let queued = false; + try { + queued = this.transport(keyboardControl(op, request, this.epoch, body)); + } catch { + queued = false; + } + if (!queued) { + clearTimeout(timer); + this.pending.delete(request); + reject(new KeyboardClientError("The glasses aren't ready for keyboard commands")); + } + }); + } + + async enable(): Promise<void> { + await this.command(KeyboardOp.enable); + } + + /** Disconnect the keyboard and stop the bridge; the bond stays. */ + async disable(): Promise<void> { + await this.command(KeyboardOp.disable); + this.release(); + } + + /** Ask for the firmware's state. Also the way to learn the live epoch: QUERY isn't epoch-checked. */ + async query(): Promise<KeyboardStatus> { + await this.command(KeyboardOp.query); + return this.status; + } + + /** + * Active scan for about five seconds; adverts arrive through events.advert. + * The firmware only clears `scanning` once the stock scanner is idle again, + * and a ring looking for its lens can hold that well past the window, so a + * lingering flag is not an error: the adverts already delivered stand. + */ + async scan(): Promise<void> { + await this.command(KeyboardOp.scan); + try { + await this.waitFor(() => !this.status.scanning, 8000, "the scan to finish"); + } catch (e) { + if (this.closed) throw e; + } + } + + /** Connect, then pair unless the link comes up encrypted. `address` is LSB first. */ + async connect(addressType: number, address: Uint8Array): Promise<void> { + if (address.length !== 6 || !Number.isInteger(addressType) || addressType < 0 || addressType > 3) { + throw new KeyboardClientError("Invalid BLE address"); + } + this.release(); + this.inputs.clear(); + this.configuration = undefined; + this.linkLost = false; + await this.command(KeyboardOp.connect, [addressType, ...address]); + await this.waitFor(() => this.status.connected || this.linkLost, 32_000, "the keyboard to connect"); + if (!this.status.connected) throw new KeyboardClientError("The keyboard didn't answer"); + if (!this.status.encrypted) { + // The keyboard's own security request may have started pairing already. + // BUSY is not success: the encrypted status below still decides. + try { + await this.command(KeyboardOp.pair); + } catch (e) { + if (!(e instanceof KeyboardClientError) || e.code !== KeyboardError.busy) throw e; + } + await this.waitFor(() => this.status.encrypted || this.linkLost, 65_000, "pairing to finish"); + if (!this.status.encrypted) throw new KeyboardClientError("Pairing with the keyboard failed"); + } + } + + async disconnect(): Promise<void> { + await this.command(KeyboardOp.disconnect); + this.release(); + } + + /** Answer a pending passkey prompt. */ + async authenticate(pin: number): Promise<void> { + if (!Number.isInteger(pin) || pin < 0 || pin > 999_999) throw new KeyboardClientError("Invalid pairing passkey"); + await this.command(KeyboardOp.auth, [pin & 255, (pin >>> 8) & 255, pin >>> 16]); + } + + /** Answer a pending numeric comparison. */ + async confirm(accept: boolean): Promise<void> { + await this.command(KeyboardOp.compare, [accept ? 1 : 0]); + } + + private async att(op: number, body: readonly number[]): Promise<KeyboardRecord> { + const r = await this.command(op, body); + if (r.kind !== KeyboardKind.att) throw new KeyboardClientError("Expected keyboard ATT response"); + return r; + } + + /** Read a whole attribute: Read, then Read Blob until a short or empty reply. Never assumes the MTU. */ + async read(handle: number): Promise<Uint8Array> { + const parts: number[] = []; + for (;;) { + const r = await this.att(KeyboardOp.read, [...le16(handle), ...le16(parts.length)]); + const status = r.code >>> 8; + if (parts.length && (status === ATT_INVALID_OFFSET || status === ATT_NOT_LONG)) break; + if (status) throw new KeyboardClientError(`ATT read ${handle}: ${status}`, undefined, status); + if (!r.data.length) break; + for (const b of r.data) parts.push(b); + if (parts.length > MAX_VALUE) throw new KeyboardClientError("Attribute exceeds 512 bytes"); + if (parts.length === MAX_VALUE) break; + } + return Uint8Array.from(parts); + } + + /** Read a short attribute once (a Report Reference, a declaration, a service UUID). */ + private async shortRead(handle: number): Promise<Uint8Array> { + const r = await this.att(KeyboardOp.read, [...le16(handle), 0, 0]); + if (r.code >>> 8) throw new KeyboardClientError(`ATT read ${handle}: ${r.code >>> 8}`, undefined, r.code >>> 8); + return r.data; + } + + /** Write 1..18 bytes (an output report such as the LEDs, a CCC, the protocol mode). */ + async write(handle: number, value: Uint8Array, withoutResponse = false): Promise<void> { + if (!value.length || value.length > 18) throw new KeyboardClientError("Keyboard write supports 1..18 bytes"); + const r = await this.att(KeyboardOp.write, [...le16(handle), withoutResponse ? 1 : 0, value.length, ...value]); + if (r.code >>> 8) throw new KeyboardClientError(`ATT write ${handle}: ${r.code >>> 8}`, undefined, r.code >>> 8); + } + + private async attributes(): Promise<Attribute[]> { + const attrs: Attribute[] = []; + let start = 1; + while (start <= 65535) { + const r = await this.att(KeyboardOp.find, [...le16(start), 255, 255]); + const status = r.code >>> 8; + if (status === ATT_NOT_FOUND) break; // the normal end of the table + if (status) throw new KeyboardClientError(`ATT Find Information: ${status}`); + const width = r.data[0] === 1 ? 4 : r.data[0] === 2 ? 18 : 0; + if (!width || r.data.length <= 1 || (r.data.length - 1) % width) { + throw new KeyboardClientError("Malformed Find Information response"); + } + for (let i = 1; i < r.data.length; i += width) { + const handle = u16(r.data, i); + if (handle < start || attrs.length >= MAX_ATTRIBUTES) throw new KeyboardClientError("Invalid or excessive GATT handles"); + attrs.push({ handle, uuid: attributeUuid(r.data.subarray(i + 2, i + width)) }); + start = handle + 1; + } + } + return attrs; + } + + /** + * The properties byte of a characteristic, from its declaration, or + * undefined when the keyboard won't say. A GATT client normally learns + * declarations through Read By Type, which the bridge doesn't relay, so + * this reads the declaration attribute itself. Most stacks answer that + * with the full declaration. Some keep only the properties byte there and + * build the rest for Read By Type alone (older Telink SDKs, common in + * small keyboards), and some refuse the read. None of that is a reason to + * turn a keyboard away: the report's handle and UUID are already known. + */ + private async declaredProperties(attrs: readonly Attribute[], a: Attribute): Promise<number | undefined> { + const decl = attrs.filter((d) => d.handle < a.handle && d.uuid === UUID_CHARACTERISTIC).pop(); + if (!decl) return undefined; + let declaration: Uint8Array; + try { + declaration = await this.shortRead(decl.handle); + } catch (e) { + const status = attRefusal(e); + this.events.log?.(`declaration ${decl.handle} of ${a.handle}: read refused (ATT ${status})`); + return undefined; + } + if (declaration.length === 5 || declaration.length === 19) { + if (u16(declaration, 1) === a.handle && attributeUuid(declaration.subarray(3)) === a.uuid) return declaration[0]!; + this.events.log?.(`declaration ${decl.handle} of ${a.handle}: describes something else (${hex(declaration)})`); + return undefined; + } + if (declaration.length === 1) return declaration[0]!; + this.events.log?.(`declaration ${decl.handle} of ${a.handle}: unexpected value (${hex(declaration)})`); + return undefined; + } + + /** + * Discover every HID service, its Report Map and each report's Report + * Reference; select Report Protocol and subscribe the input reports that + * carry keys or a pointer's buttons and movement. Needs an encrypted link. + * No handle is assumed. + * + * Only what input needs is required of the device: a Report Map that + * parses, and at least one such input report with a Report Reference and + * a CCC. A report that can't be placed is skipped and logged, as is + * anything wrong with an external report (the battery level, usually). + */ + async configure(): Promise<KeyboardConfiguration> { + const epoch = this.epoch; + const log = (line: string): void => this.events.log?.(line); + // A new discovery replaces any earlier mapping, whether or not it succeeds. + this.release(); + this.inputs.clear(); + this.configuration = undefined; + const attrs = await this.attributes(); + log(`gatt ${attrs.map((a) => `${a.handle}=${typeof a.uuid === "number" ? a.uuid.toString(16) : a.uuid}`).join(" ")}`); + const reports: KeyboardReport[] = []; + const watched: KeyboardReport[] = []; + let pointerFound = false; + const skipped: string[] = []; + const descriptorsFor = (handle: number): Attribute[] => { + const end = attrs.find((a) => a.handle > handle && + (a.uuid === UUID_PRIMARY_SERVICE || a.uuid === UUID_SECONDARY_SERVICE || a.uuid === UUID_CHARACTERISTIC))?.handle ?? 65536; + return attrs.filter((a) => a.handle > handle && a.handle < end); + }; + /** Why a report was left out; the link going away is not a reason to carry on. */ + const skip = (a: Attribute, reason: string): void => { + skipped.push(`${a.handle}: ${reason}`); + log(`report ${a.handle} skipped: ${reason}`); + }; + const addReport = async (service: number, a: Attribute, map: Uint8Array, parsed: Map<number, HidReport>) => { + if (reports.some((r) => r.handle === a.handle)) return skip(a, "already listed by another HID service"); + const descriptors = descriptorsFor(a.handle); + const ref = descriptors.find((d) => d.uuid === UUID_REPORT_REFERENCE); + if (!ref) return skip(a, "no Report Reference descriptor"); + let reference: Uint8Array; + try { + reference = await this.shortRead(ref.handle); + } catch (e) { + return skip(a, `Report Reference ${ref.handle} unreadable (ATT ${attRefusal(e)})`); + } + if (reference.length !== 2) return skip(a, `Report Reference ${ref.handle} is ${hex(reference) || "empty"}`); + const id = reference[0]!; + const type = reference[1]!; + if (type < 1 || type > 3) return skip(a, `Report Reference type ${type}`); + const ccc = descriptors.find((d) => d.uuid === UUID_CCC)?.handle; + const properties = await this.declaredProperties(attrs, a); + const report: KeyboardReport = { service, handle: a.handle, id, type, ccc, properties, map }; + if (type === 1) { + const layout = parsed.get(id); + if (!layout) return skip(a, `input report id ${id} is not in the Report Map`); + if (ccc === undefined || (properties !== undefined && !(properties & 0x10))) return skip(a, "input report can't notify"); + const keys = reportHasKeys(layout); + const pointer = reportHasPointer(layout); + if (keys || pointer) watched.push(report); + else log(`report ${a.handle} (id ${id}) carries no keys or pointer; not subscribed`); + if (pointer) { + pointerFound = true; + log(`report ${a.handle} (id ${id}) is a pointer's${keys ? ", with keys" : ""}`); + } + } + reports.push(report); + log(`report ${a.handle}: id ${id} type ${type} ccc ${ccc ?? "-"} properties ${properties ?? "?"}`); + }; + for (let i = 0; i < attrs.length; i++) { + const service = attrs[i]!; + if (service.uuid !== UUID_PRIMARY_SERVICE) continue; + if (attributeUuid(await this.shortRead(service.handle)) !== UUID_HID_SERVICE) continue; + const end = attrs.slice(i + 1).find((a) => a.uuid === UUID_PRIMARY_SERVICE || a.uuid === UUID_SECONDARY_SERVICE)?.handle ?? 65536; + const members = attrs.filter((a) => a.handle > service.handle && a.handle < end); + const mapAttr = members.find((a) => a.uuid === UUID_REPORT_MAP); + if (!mapAttr) throw new KeyboardClientError(`HID service ${service.handle} has no Report Map`); + const map = await this.read(mapAttr.handle); + log(`report map ${mapAttr.handle}: ${hex(map)}`); + const parsed = parseReportMap(map); + for (const mode of members.filter((a) => a.uuid === UUID_PROTOCOL_MODE)) { + try { + await this.write(mode.handle, Uint8Array.of(1), true); + } catch (e) { + // Report Protocol is the default after a connection; a keyboard + // that won't take the write is almost certainly in it already. + log(`protocol mode ${mode.handle}: write refused (ATT ${attRefusal(e)})`); + } + } + for (const a of members.filter((m) => m.uuid === UUID_REPORT)) await addReport(service.handle, a, map, parsed); + // HOGP lets a Report Map refer to reports in other services (commonly Battery). + for (const ext of descriptorsFor(mapAttr.handle).filter((a) => a.uuid === UUID_EXTERNAL_REPORT_REFERENCE)) { + let value: Uint8Array; + try { + value = await this.shortRead(ext.handle); + } catch (e) { + log(`external report reference ${ext.handle} skipped: unreadable (ATT ${attRefusal(e)})`); + continue; + } + if (value.length !== 2 && value.length !== 16) { + log(`external report reference ${ext.handle} skipped: value ${hex(value) || "empty"}`); + continue; + } + const uuid = attributeUuid(value); + for (const a of attrs.filter((m) => m.uuid === uuid && (m.handle < service.handle || m.handle >= end))) { + await addReport(service.handle, a, map, parsed); + } + } + } + if (this.closed || this.epoch !== epoch) throw new KeyboardClientError("The keyboard link changed during setup"); + if (!watched.length) { + throw new KeyboardClientError(reports.length || skipped.length + ? `No usable keyboard input report (${skipped.join("; ") || "none carries keys or a pointer"})` + : "This device has no HID service"); + } + if (watched.length > MAX_WATCHES) throw new KeyboardClientError("Keyboard has more than 16 input reports"); + const changed = attrs.find((a) => a.uuid === UUID_SERVICE_CHANGED); + const changedCcc = changed && descriptorsFor(changed.handle).find((a) => a.uuid === UUID_CCC); + const watches = watched.map((r) => r.handle); + if (changed && changedCcc && watches.length < MAX_WATCHES) watches.push(changed.handle); + const serviceChanged = changed && changedCcc && watches.includes(changed.handle) ? changed.handle : undefined; + const config: KeyboardConfiguration = { + epoch: this.epoch, reports, watched: watched.map((r) => r.handle), pointer: pointerFound, serviceChanged, + }; + this.restore(config); + try { + await this.command(KeyboardOp.watch, [watches.length, ...watches.flatMap(le16)]); + if (serviceChanged !== undefined) { + try { + await this.write(changedCcc!.handle, Uint8Array.of(2, 0)); + } catch (e) { + log(`service changed ${changedCcc!.handle}: subscription refused (ATT ${attRefusal(e)})`); + } + } + for (const r of watched) await this.write(r.ccc!, Uint8Array.of(1, 0)); + } catch (e) { + this.inputs.clear(); + this.configuration = undefined; + this.release(); + throw e; + } + return config; + } + + /** + * Install a mapping found by configure(). It is only good while the glasses + * keep the same connection (the same epoch): after a keyboard reconnect or + * a firmware boot, discover again. + */ + restore(config: KeyboardConfiguration): void { + if (!config.reports.length || config.reports.length > 64) throw new KeyboardClientError("Invalid keyboard configuration"); + const inputs = new Map<number, Input>(); + for (const r of config.reports) { + if (r.type !== 1 || !config.watched.includes(r.handle)) continue; + if (!Number.isInteger(r.handle) || r.handle < 1 || r.handle > 65535 || inputs.has(r.handle)) { + throw new KeyboardClientError("Invalid restored Report handle"); + } + if (!r.map.length || r.map.length > MAX_VALUE) throw new KeyboardClientError("Invalid restored Report Map"); + const report = parseReportMap(r.map).get(r.id); + if (!report) throw new KeyboardClientError("Restored Report ID missing"); + inputs.set(r.handle, { report, id: r.id, service: r.service, keys: reportHasKeys(report), pointer: reportHasPointer(report) }); + } + if (!inputs.size || inputs.size > MAX_WATCHES) throw new KeyboardClientError("Invalid restored input count"); + this.release(); + this.inputs = inputs; + this.configuration = config; + } + + /** Output reports of the configured keyboard (the LED report is the usual one). */ + outputReports(): KeyboardReport[] { + return this.configuration?.reports.filter((r) => r.type === 2) ?? []; + } +} + +/** + * The ATT error code when `error` is the keyboard refusing a read or write. + * Anything else (the link dropping, a timeout, a stream gap) is rethrown: + * those end the setup, where a refusal only costs the one attribute. + */ +function attRefusal(error: unknown): number { + if (error instanceof KeyboardClientError && error.attStatus !== undefined) return error.attStatus; + throw error; +} + +function hex(bytes: Uint8Array): string { + return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""); +} + +function opName(op: number): string { + for (const [name, value] of Object.entries(KeyboardOp)) if (value === op) return name; + return String(op); +} + +function messageOf(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} diff --git a/app/g2/keyboard/hid.ts b/app/g2/keyboard/hid.ts new file mode 100644 index 00000000..809f4ca5 --- /dev/null +++ b/app/g2/keyboard/hid.ts @@ -0,0 +1,268 @@ +/** + * Bounded HID report-descriptor decoder for the keyboard bridge: turns a + * keyboard's Report Map into the layout of each input report, and input + * reports into the set of keys held. From the reference client in g2flash + * demos/ (keyboard-hid.ts). + * + * HID over GATT carries the Report ID out of band (in each characteristic's + * Report Reference descriptor), so byte zero of a report value is data: it is + * never stripped as USB HID would. The keyboard/keypad page (7) and the + * consumer page (12) produce keys. A mouse's or touchpad's report (buttons, + * relative X/Y, wheel) is read by reportPointer. + * No platform imports: this runs under node in the tests. + */ + +export type HidUsage = { page: number; usage: number }; +export type HidKeyEvent = HidUsage & { down: boolean }; + +type Global = { page: number; min: number; max: number; size: number; count: number; id: number }; +type Field = { + bit: number; + size: number; + count: number; + variable: boolean; + /** Reports a change since the last report (a mouse's axes and wheel), not a state. */ + relative: boolean; + min: number; + max: number; + usages: HidUsage[]; + range?: [HidUsage, HidUsage]; +}; +export type HidReport = { id: number; bits: number; fields: Field[] }; + +const HID_PAGE_KEYBOARD = 7; +const HID_PAGE_CONSUMER = 12; +const REPORT_MAP_MAX = 512; + +function fail(reason: string): never { + throw new Error(`Unsupported or malformed HID descriptor: ${reason}`); +} + +const usageKey = (u: HidUsage): string => `${u.page}:${u.usage}`; + +/** Input reports by Report ID (0 when the descriptor uses none). Throws on anything out of bounds. */ +export function parseReportMap(bytes: Uint8Array): Map<number, HidReport> { + if (!bytes.length || bytes.length > REPORT_MAP_MAX) fail("report map must contain 1..512 bytes"); + const reports = new Map<number, HidReport>(); + let g: Global = { page: 0, min: 0, max: 0, size: 0, count: 0, id: 0 }; + const stack: Global[] = []; + let usages: HidUsage[] = []; + let lo: HidUsage | undefined; + let hi: HidUsage | undefined; + let depth = 0; + const usage = (v: number, n: number): HidUsage => + n === 4 ? { page: v >>> 16, usage: v & 65535 } : { page: g.page, usage: v }; + for (let p = 0; p < bytes.length;) { + const prefix = bytes[p++]!; + if (prefix === 0xfe) fail("long items"); + const n = [0, 1, 2, 4][prefix & 3]!; + if (p + n > bytes.length) fail("truncated item"); + let v = 0; + for (let j = 0; j < n; j++) v += bytes[p++]! * 2 ** (8 * j); + const signed = n && v >= 2 ** (n * 8 - 1) ? v - 2 ** (n * 8) : v; + const type = (prefix >> 2) & 3; + const tag = prefix >> 4; + if (type === 1) { + switch (tag) { + case 0: g.page = v; break; + case 1: g.min = signed; break; + case 2: g.max = g.min < 0 ? signed : v; break; + case 7: g.size = v; break; + case 8: if (!v || v > 255) fail("Report ID"); g.id = v; break; + case 9: g.count = v; break; + case 10: if (stack.length >= 8) fail("global stack overflow"); stack.push({ ...g }); break; + case 11: if (!stack.length) fail("global stack underflow"); g = stack.pop()!; break; + case 3: case 4: case 5: case 6: break; // physical range, unit and exponent + default: fail("unknown global item"); + } + } else if (type === 2) { + if (tag === 0) { if (usages.length >= 512) fail("too many usages"); usages.push(usage(v, n)); } + else if (tag === 1) lo = usage(v, n); + else if (tag === 2) hi = usage(v, n); + else if (tag === 10) fail("usage delimiters"); + else if (tag > 9) fail("unknown local item"); + } else if (type === 0) { + if (tag === 10) { if (++depth > 32) fail("collection depth"); } + else if (tag === 12) { if (--depth < 0) fail("collection underflow"); } + else if (tag === 8) { + if (!g.size || g.size > 32 || !g.count || g.count > 512) fail("field dimensions"); + let r = reports.get(g.id); + if (!r) { + if (reports.size >= 16) fail("too many input reports"); + r = { id: g.id, bits: 0, fields: [] }; + reports.set(g.id, r); + } + const field: Field = { + bit: r.bits, size: g.size, count: g.count, variable: !!(v & 2), relative: !!(v & 4), + min: g.min, max: g.max, usages: [...usages], + }; + if (lo || hi) { + if (!lo || !hi || lo.page !== hi.page || hi.usage < lo.usage) fail("usage range"); + field.range = [lo!, hi!]; + } + r.bits += g.size * g.count; + if (r.bits > 4096) fail("report exceeds 512 bytes"); + // Constant fields are padding. + if (!(v & 1)) r.fields.push(field); + } else if (tag !== 9 && tag !== 11) fail("unknown main item"); + usages = []; + lo = hi = undefined; + } else fail("reserved item type"); + } + if (depth || stack.length) fail("unbalanced collection/global stack"); + if (reports.has(0) && reports.size > 1) fail("mixed numbered and unnumbered input reports"); + if (!reports.size) fail("no input reports"); + return reports; +} + +function usageAt(f: Field, index: number): HidUsage | undefined { + if (index < 0) return undefined; + if (index < f.usages.length) return f.usages[index]; + const rangeIndex = index - f.usages.length; + if (f.range && rangeIndex <= f.range[1].usage - f.range[0].usage) { + return { page: f.range[0].page, usage: f.range[0].usage + rangeIndex }; + } + // Variable items reuse their last usage when Report Count exceeds the usages. + if (f.variable) return f.range?.[1] ?? f.usages[f.usages.length - 1]; + return undefined; +} + +/** + * Whether an input report can ever say a key is held. False for a pointer's + * report (a trackpad or mouse on a combined device), whose movement would + * otherwise be relayed, and thrown away, a hundred times a second. + */ +export function reportHasKeys(report: HidReport): boolean { + const isKeyPage = (u: HidUsage): boolean => u.page === HID_PAGE_KEYBOARD || u.page === HID_PAGE_CONSUMER; + return report.fields.some((f) => !f.relative && (f.usages.some(isKeyPage) || (f.range !== undefined && isKeyPage(f.range[0])))); +} + +/** One field's `index`th value, sign-extended when the field is signed. Bits past the data read as zero. */ +function fieldValue(f: Field, index: number, data: Uint8Array): number { + let value = 0; + for (let j = 0; j < f.size; j++) { + const bit = f.bit + index * f.size + j; + if ((data[bit >>> 3] ?? 0) & (1 << (bit & 7))) value += 2 ** j; + } + if (f.min < 0 && value >= 2 ** (f.size - 1)) value -= 2 ** f.size; + return value; +} + +/** What one report from a mouse or touchpad says. */ +export type HidPointerSample = { + /** Buttons held, bit 0 for button 1 (left), bit 1 for button 2 (right), bit 2 for button 3 (middle). */ + buttons: number; + /** Movement since the last report, in the device's counts; +x right, +y down. */ + dx: number; + dy: number; + /** Wheel detents since the last report; positive is away from the user ("up"). */ + wheel: number; + /** Horizontal wheel (AC Pan); positive is right. */ + pan: number; +}; + +const HID_PAGE_GENERIC_DESKTOP = 1; +const HID_PAGE_BUTTON = 9; +const USAGE_X = 0x30; +const USAGE_Y = 0x31; +const USAGE_WHEEL = 0x38; +const USAGE_AC_PAN = 0x238; +/** Buttons past this are not tracked; a mouse with more is rare and has nothing to bind them to anyway. */ +const MAX_POINTER_BUTTONS = 8; + +/** Which part of a pointer sample a field's usage feeds, if any. */ +function pointerRole(f: Field, u: HidUsage | undefined): "button" | "dx" | "dy" | "wheel" | "pan" | null { + if (!u || !f.variable) return null; + if (u.page === HID_PAGE_BUTTON) return !f.relative && u.usage >= 1 && u.usage <= MAX_POINTER_BUTTONS ? "button" : null; + if (!f.relative) return null; // an absolute X/Y is a touchscreen or a digitizer, not a mouse + if (u.page === HID_PAGE_GENERIC_DESKTOP) { + if (u.usage === USAGE_X) return "dx"; + if (u.usage === USAGE_Y) return "dy"; + if (u.usage === USAGE_WHEEL) return "wheel"; + } + if (u.page === HID_PAGE_CONSUMER && u.usage === USAGE_AC_PAN) return "pan"; + return null; +} + +/** Whether an input report carries a mouse's buttons, movement or wheel. */ +export function reportHasPointer(report: HidReport): boolean { + return report.fields.some((f) => { + for (let i = 0; i < f.count; i++) if (pointerRole(f, usageAt(f, i))) return true; + return false; + }); +} + +/** + * Read a mouse or touchpad report. Lenient about length, unlike reportKeys: + * some devices leave the wheel bytes off when there is nothing to say, and a + * missing byte is zero either way. + */ +export function reportPointer(report: HidReport, data: Uint8Array): HidPointerSample { + const sample: HidPointerSample = { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }; + for (const f of report.fields) { + for (let i = 0; i < f.count; i++) { + const u = usageAt(f, i); + const role = pointerRole(f, u); + if (!role) continue; + const value = fieldValue(f, i, data); + if (value < f.min || value > f.max) continue; // out of range: no reading + if (role === "button") { + if (value) sample.buttons |= 1 << (u!.usage - 1); + } else { + sample[role] += value; + } + } + } + return sample; +} + +/** The keys one input report says are held. Throws on a length mismatch or a rollover report. */ +export function reportKeys(report: HidReport, data: Uint8Array): HidUsage[] { + if (data.length !== Math.ceil(report.bits / 8)) throw new Error("HID input length does not match Report Map"); + const keys = new Map<string, HidUsage>(); + for (const f of report.fields) { + // A relative field is a movement, not a key that is down. + if (f.relative) continue; + for (let i = 0; i < f.count; i++) { + const value = fieldValue(f, i, data); + if (value < f.min || value > f.max) continue; // null state + const u = usageAt(f, f.variable ? i : value - f.min); + if (!u || (f.variable && value === 0) || !u.usage) continue; + if (u.page === HID_PAGE_KEYBOARD && u.usage <= 3) throw new Error("HID keyboard rollover/error report"); + if (u.page === HID_PAGE_KEYBOARD || u.page === HID_PAGE_CONSUMER) keys.set(usageKey(u), u); + } + } + return [...keys.values()]; +} + +/** + * Held keys across a keyboard's input reports. Ownership is per report + * characteristic, then unioned: releasing a key in one report cannot release + * a modifier another report still holds. + */ +export class HidKeys { + private reports = new Map<number, HidUsage[]>(); + private held = new Map<string, HidUsage>(); + + /** Apply one report from characteristic `handle`; returns the transitions it caused. */ + update(handle: number, report: HidReport, data: Uint8Array): HidKeyEvent[] { + this.reports.set(handle, reportKeys(report, data)); + return this.diff(); + } + + /** Release everything (a stream gap, a dropped link). */ + reset(): HidKeyEvent[] { + this.reports.clear(); + return this.diff(); + } + + private diff(): HidKeyEvent[] { + const next = new Map<string, HidUsage>(); + for (const values of this.reports.values()) for (const u of values) next.set(usageKey(u), u); + const events: HidKeyEvent[] = []; + for (const [k, u] of this.held) if (!next.has(k)) events.push({ ...u, down: false }); + for (const [k, u] of next) if (!this.held.has(k)) events.push({ ...u, down: true }); + this.held = next; + return events; + } +} diff --git a/app/g2/keyboard/key-router.ts b/app/g2/keyboard/key-router.ts new file mode 100644 index 00000000..385f0a04 --- /dev/null +++ b/app/g2/keyboard/key-router.ts @@ -0,0 +1,146 @@ +/** + * Where a key from the hardware keyboard goes: to a keybinding being set up, + * to a gesture it is bound to, to the window with input focus, or to the + * built-in navigation. The meanings are in bindings.ts; this is the order of + * asking, and the bookkeeping around it. Shared by the Android and iOS + * controllers through KeyRouterHost. + * No platform imports: this runs under node in the tests. + */ +import { + GestureScheme, + chordOf, + isModifierCode, + resolveKey, + type GestureOrigin, + type KeyAction, + type KeyChord, + type KeyGesture, +} from "./bindings"; +import type { KeyEvent } from "./keys"; + +export type KeyRouterHost = { + /** Connected to glasses that are in use. */ + ready(): boolean; + locked(): boolean; + screenOn(): boolean; + /** Turn a dark display on, and nothing else. */ + wake(): void; + /** Offer a key to the window with input focus; true when that window takes keys. */ + offerToWindow(event: KeyEvent): boolean; + /** Perform a gesture as if it came from the ring or the watch. */ + gesture(gesture: KeyGesture, origin: GestureOrigin): Promise<void>; + /** The custom keybindings, by chord id. */ + bindings(): ReadonlyMap<string, KeyAction>; + /** A key was used for something other than a gesture or typing (count it against the screen timeout). */ + noteActivity(): void; + log(line: string): void; + now?(): number; +}; + +/** + * A held key repeats twenty times a second, and a wheel or a two-finger + * scroll reports faster still: more than a list can repaint over the + * glasses' link. Those are dropped while a gesture is still being handled, + * and closer together than this. + */ +const REPEAT_MIN_INTERVAL_MS = 100; + +export class KeyRouter { + private capture: ((chord: KeyChord) => void) | null = null; + /** Keys whose press was spoken for, so their release is too. */ + private readonly consumed = new Set<string>(); + private readonly scheme = new GestureScheme(); + private gesturesInFlight = 0; + private lastGestureAtMs = 0; + + constructor(private readonly host: KeyRouterHost) {} + + /** One key transition from the keyboard. */ + handleKey(event: KeyEvent): void { + const host = this.host; + if (!host.ready()) return; + if (host.locked() || !host.screenOn()) { + // A dark display takes no typing: the first press only wakes it, as + // the ring's double tap would, and a locked one stays locked. + if (event.phase === "down" && !event.repeat && !host.screenOn()) host.wake(); + return; + } + if (this.capture) { + this.captureKey(event); + return; + } + if (event.phase === "up") { + if (!this.consumed.delete(event.code)) host.offerToWindow(event); + return; + } + const resolution = resolveKey(event, host.bindings()); + const droppable = event.repeat || event.burst === true; + if (resolution.consume) { + this.consumed.add(event.code); + if (resolution.gesture) this.perform(resolution.gesture, droppable); + return; + } + if (host.offerToWindow(event)) return; + if (resolution.gesture) this.perform(resolution.gesture, droppable); + } + + /** + * A key the focused window was offered and had no use for (a terminal + * showing its session list rather than a session): it gets the built-in + * navigation meaning after all. + */ + handleUnhandledKey(event: KeyEvent): void { + const host = this.host; + if (event.phase !== "down" || this.capture || !host.ready() || host.locked() || !host.screenOn()) return; + const resolution = resolveKey(event, host.bindings()); + if (!resolution.consume && resolution.gesture) this.perform(resolution.gesture, event.repeat || event.burst === true); + } + + /** + * Take the next key press instead of acting on it (setting up a + * keybinding). Modifiers held with it are part of the chord; pressed alone + * they are waited through. Returns a function that calls the capture off. + */ + captureNextKey(onChord: (chord: KeyChord) => void): () => void { + this.capture = onChord; + return () => { + if (this.capture === onChord) this.capture = null; + }; + } + + /** The keyboard went away: nothing is held any more. */ + reset(): void { + this.consumed.clear(); + } + + private captureKey(event: KeyEvent): void { + this.host.noteActivity(); + if (event.phase === "up") { + this.consumed.delete(event.code); + return; + } + if (event.repeat || isModifierCode(event.code)) return; + const capture = this.capture!; + this.capture = null; + this.consumed.add(event.code); + capture(chordOf(event)); + } + + private perform(gesture: KeyGesture, droppable: boolean): void { + const now = this.host.now ? this.host.now() : Date.now(); + if (droppable && (this.gesturesInFlight > 0 || now - this.lastGestureAtMs < REPEAT_MIN_INTERVAL_MS)) return; + this.lastGestureAtMs = now; + void this.run(gesture, this.scheme.originFor(gesture)); + } + + private async run(gesture: KeyGesture, origin: GestureOrigin): Promise<void> { + this.gesturesInFlight++; + try { + await this.host.gesture(gesture, origin); + } catch (error) { + this.host.log(`keyboard gesture ${gesture} failed: ${error instanceof Error ? error.message : String(error)}`); + } finally { + this.gesturesInFlight--; + } + } +} diff --git a/app/g2/keyboard/keys.ts b/app/g2/keyboard/keys.ts new file mode 100644 index 00000000..1ab9f146 --- /dev/null +++ b/app/g2/keyboard/keys.ts @@ -0,0 +1,306 @@ +/** + * Key events as the rest of Faceclaw sees them, and the translation from the + * keyboard bridge's raw HID transitions: layout (US only for now), modifier + * and Caps Lock state, and typematic repeat, which a BLE keyboard never sends + * (its report just stays asserted). The translation tables follow the Glassly + * app's (GlassesKeyboardInput.ts, MIT). + * No platform imports: this runs under node in the tests, and in app workers. + */ +import type { HidKeyEvent } from "./hid"; + +export type KeyModifiers = { shift: boolean; ctrl: boolean; alt: boolean; meta: boolean }; + +/** + * One key transition, shaped like a DOM KeyboardEvent: `key` is the logical + * key with the layout applied ("a", "A", "Enter", "ArrowUp"), `code` the + * physical key ("KeyA"), `text` what a press types (absent for releases, for + * keys that type nothing, and for Ctrl/Alt/Meta chords, which are commands). + */ +export type KeyEvent = { + phase: "down" | "up"; + key: string; + code: string; + text?: string; + modifiers: KeyModifiers; + /** A synthesized auto-repeat of a held key. */ + repeat: boolean; + /** + * One of a stream that can arrive faster than it can be acted on (a wheel + * turning): whoever acts on it may drop some rather than fall behind. + */ + burst?: boolean; + /** Date.now() when the phone saw the transition. */ + timestampMs: number; +}; + +const HID_PAGE_KEYBOARD = 7; +const HID_PAGE_CONSUMER = 12; +/** A mouse's buttons, which arrive here as keys: usage 1 is the left button, 2 the right, 3 the middle. */ +export const HID_PAGE_BUTTON = 9; +const USAGE_CAPS_LOCK = 57; + +/** [unshifted, shifted, code] for US-layout punctuation on the keyboard page. */ +const PUNCTUATION: Record<number, [string, string, string]> = { + 45: ["-", "_", "Minus"], + 46: ["=", "+", "Equal"], + 47: ["[", "{", "BracketLeft"], + 48: ["]", "}", "BracketRight"], + 49: ["\\", "|", "Backslash"], + 50: ["#", "~", "IntlHash"], + 51: [";", ":", "Semicolon"], + 52: ["'", '"', "Quote"], + 53: ["`", "~", "Backquote"], + 54: [",", "<", "Comma"], + 55: [".", ">", "Period"], + 56: ["/", "?", "Slash"], + 100: ["\\", "|", "IntlBackslash"], +}; + +const DIGITS = "1234567890"; +const SHIFTED_DIGITS = "!@#$%^&*()"; + +/** [key, code] for keys that type nothing. */ +const NAMED: Record<number, [string, string]> = { + 40: ["Enter", "Enter"], + 41: ["Escape", "Escape"], + 42: ["Backspace", "Backspace"], + 43: ["Tab", "Tab"], + 57: ["CapsLock", "CapsLock"], + 70: ["PrintScreen", "PrintScreen"], + 71: ["ScrollLock", "ScrollLock"], + 72: ["Pause", "Pause"], + 73: ["Insert", "Insert"], + 74: ["Home", "Home"], + 75: ["PageUp", "PageUp"], + 76: ["Delete", "Delete"], + 77: ["End", "End"], + 78: ["PageDown", "PageDown"], + 79: ["ArrowRight", "ArrowRight"], + 80: ["ArrowLeft", "ArrowLeft"], + 81: ["ArrowDown", "ArrowDown"], + 82: ["ArrowUp", "ArrowUp"], + 83: ["NumLock", "NumLock"], + 88: ["Enter", "NumpadEnter"], + 101: ["ContextMenu", "ContextMenu"], + 102: ["Power", "Power"], + 224: ["Control", "ControlLeft"], + 225: ["Shift", "ShiftLeft"], + 226: ["Alt", "AltLeft"], + 227: ["Meta", "MetaLeft"], + 228: ["Control", "ControlRight"], + 229: ["Shift", "ShiftRight"], + 230: ["Alt", "AltRight"], + 231: ["Meta", "MetaRight"], +}; + +/** [key and text, code] for the numeric keypad's operators. */ +const NUMPAD: Record<number, [string, string]> = { + 84: ["/", "NumpadDivide"], + 85: ["*", "NumpadMultiply"], + 86: ["-", "NumpadSubtract"], + 87: ["+", "NumpadAdd"], + 99: [".", "NumpadDecimal"], + 103: ["=", "NumpadEqual"], +}; + +const CONSUMER: Record<number, string> = { + 0xe9: "AudioVolumeUp", + 0xea: "AudioVolumeDown", + 0xe2: "AudioVolumeMute", + 0xcd: "MediaPlayPause", + 0xb0: "MediaPlay", + 0xb1: "MediaPause", + 0xb5: "MediaTrackNext", + 0xb6: "MediaTrackPrevious", + 0xb7: "MediaStop", + 0x6f: "BrightnessUp", + 0x70: "BrightnessDown", + 0x221: "BrowserSearch", + 0x223: "BrowserHome", + 0x224: "BrowserBack", + 0x225: "BrowserForward", + 0x18a: "LaunchMail", + 0x192: "LaunchApplication2", + 0x194: "LaunchApplication1", +}; + +const MODIFIER_USAGES: Record<number, keyof KeyModifiers> = { + 224: "ctrl", 228: "ctrl", 225: "shift", 229: "shift", 226: "alt", 230: "alt", 227: "meta", 231: "meta", +}; + +type Translated = { key: string; code: string; text?: string }; + +/** One HID usage under the US layout. Caps Lock only affects letters. */ +export function translateUsage(page: number, usage: number, shift: boolean, capsLock: boolean): Translated { + if (page === HID_PAGE_KEYBOARD) { + if (usage >= 4 && usage <= 29) { + const lower = String.fromCharCode(97 + usage - 4); + const key = shift !== capsLock ? lower.toUpperCase() : lower; + return { key, code: `Key${lower.toUpperCase()}`, text: key }; + } + if (usage >= 30 && usage <= 39) { + const index = usage - 30; + const key = shift ? SHIFTED_DIGITS[index]! : DIGITS[index]!; + return { key, code: `Digit${DIGITS[index]}`, text: key }; + } + if (usage === 44) return { key: " ", code: "Space", text: " " }; + const punctuation = PUNCTUATION[usage]; + if (punctuation) { + const key = shift ? punctuation[1] : punctuation[0]; + return { key, code: punctuation[2], text: key }; + } + if (usage >= 58 && usage <= 69) return { key: `F${usage - 57}`, code: `F${usage - 57}` }; + if (usage >= 104 && usage <= 115) return { key: `F${usage - 91}`, code: `F${usage - 91}` }; + if (usage >= 89 && usage <= 97) { + const digit = String(usage - 88); + return { key: digit, code: `Numpad${digit}`, text: digit }; + } + if (usage === 98) return { key: "0", code: "Numpad0", text: "0" }; + const numpad = NUMPAD[usage]; + if (numpad) return { key: numpad[0], code: numpad[1], text: numpad[0] }; + const named = NAMED[usage]; + if (named) return { key: named[0], code: named[1] }; + return { key: "Unidentified", code: `Usage${usage.toString(16).padStart(2, "0")}` }; + } + if (page === HID_PAGE_CONSUMER) { + const name = CONSUMER[usage]; + return name ? { key: name, code: name } : { key: "Unidentified", code: `Consumer${usage.toString(16)}` }; + } + if (page === HID_PAGE_BUTTON) return { key: `MouseButton${usage}`, code: `MouseButton${usage}` }; + return { key: "Unidentified", code: `Page${page.toString(16)}Usage${usage.toString(16)}` }; +} + +export type KeyTranslatorOptions = { + emit: (event: KeyEvent) => void; + now?: () => number; + setTimer?: (callback: () => void, delayMs: number) => unknown; + clearTimer?: (handle: unknown) => void; + /** Hold time before the first repeat, and the gap between repeats. */ + repeatDelayMs?: number; + repeatIntervalMs?: number; +}; + +const REPEAT_DELAY_MS = 400; +const REPEAT_INTERVAL_MS = 50; + +/** + * Raw HID transitions in, KeyEvents out. Modifier state comes from the same + * stream, and the bridge reports a release for every key when the stream may + * have lost something, so a missed key-up cannot leave a modifier stuck or a + * key repeating forever. + */ +export class KeyTranslator { + private readonly emitEvent: (event: KeyEvent) => void; + private readonly now: () => number; + private readonly setTimer: (callback: () => void, delayMs: number) => unknown; + private readonly clearTimer: (handle: unknown) => void; + private readonly repeatDelayMs: number; + private readonly repeatIntervalMs: number; + private readonly held = new Set<string>(); + private modifiers: KeyModifiers = { shift: false, ctrl: false, alt: false, meta: false }; + private capsLock = false; + private repeatTimer: unknown = null; + private repeating: { page: number; usage: number } | null = null; + + constructor(options: KeyTranslatorOptions) { + this.emitEvent = options.emit; + this.now = options.now ?? (() => Date.now()); + this.setTimer = options.setTimer ?? ((callback, delayMs) => setTimeout(callback, delayMs)); + this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle as ReturnType<typeof setTimeout>)); + this.repeatDelayMs = options.repeatDelayMs ?? REPEAT_DELAY_MS; + this.repeatIntervalMs = options.repeatIntervalMs ?? REPEAT_INTERVAL_MS; + } + + /** Apply the transitions one input report produced, in order. */ + handle(events: readonly HidKeyEvent[]): void { + for (const event of events) this.handleOne(event); + } + + private handleOne(event: HidKeyEvent): void { + const id = `${event.page}:${event.usage}`; + const modifier = event.page === HID_PAGE_KEYBOARD ? MODIFIER_USAGES[event.usage] : undefined; + if (event.down) { + if (this.held.has(id)) return; + this.held.add(id); + if (modifier) this.modifiers[modifier] = true; + if (event.page === HID_PAGE_KEYBOARD && event.usage === USAGE_CAPS_LOCK) this.capsLock = !this.capsLock; + this.emit("down", event.page, event.usage, false); + // The newest non-modifier key is the one that repeats, as on a PC. A + // mouse button neither repeats nor takes the repeat from a held key. + if (!modifier && event.page !== HID_PAGE_BUTTON) this.startRepeat(event.page, event.usage); + } else { + if (!this.held.delete(id)) return; + if (modifier) this.modifiers[modifier] = this.otherHeld(event.usage, modifier); + if (this.repeating?.page === event.page && this.repeating.usage === event.usage) this.stopRepeat(); + this.emit("up", event.page, event.usage, false); + } + } + + /** + * A press and release in one, for input that has no "held" (a wheel's + * detent): `name` is both the key and the code, e.g. "WheelUp". Carries + * the modifiers held on the keyboard, like any key. + */ + pulse(name: string): void { + const timestampMs = this.now(); + for (const phase of ["down", "up"] as const) { + this.emitEvent({ phase, key: name, code: name, modifiers: { ...this.modifiers }, repeat: false, burst: true, timestampMs }); + } + } + + /** The keyboard went away: forget everything without emitting releases. */ + reset(): void { + this.stopRepeat(); + this.held.clear(); + this.modifiers = { shift: false, ctrl: false, alt: false, meta: false }; + this.capsLock = false; + } + + /** Whether the same modifier's key on the other side is still down. */ + private otherHeld(released: number, modifier: keyof KeyModifiers): boolean { + for (const [usage, name] of Object.entries(MODIFIER_USAGES)) { + if (name === modifier && Number(usage) !== released && this.held.has(`${HID_PAGE_KEYBOARD}:${usage}`)) return true; + } + return false; + } + + private emit(phase: "down" | "up", page: number, usage: number, repeat: boolean): void { + const translated = translateUsage(page, usage, this.modifiers.shift, this.capsLock); + const event: KeyEvent = { + phase, + key: translated.key, + code: translated.code, + modifiers: { ...this.modifiers }, + repeat, + timestampMs: this.now(), + }; + if (phase === "down" && translated.text !== undefined && + !this.modifiers.ctrl && !this.modifiers.alt && !this.modifiers.meta) { + event.text = translated.text; + } + this.emitEvent(event); + } + + private startRepeat(page: number, usage: number): void { + this.stopRepeat(); + this.repeating = { page, usage }; + this.repeatTimer = this.setTimer(() => this.repeatTick(), this.repeatDelayMs); + } + + private repeatTick(): void { + const key = this.repeating; + this.repeatTimer = null; + if (!key || !this.held.has(`${key.page}:${key.usage}`)) { + this.repeating = null; + return; + } + this.emit("down", key.page, key.usage, true); + this.repeatTimer = this.setTimer(() => this.repeatTick(), this.repeatIntervalMs); + } + + private stopRepeat(): void { + if (this.repeatTimer !== null) this.clearTimer(this.repeatTimer); + this.repeatTimer = null; + this.repeating = null; + } +} diff --git a/app/g2/keyboard/pointer.ts b/app/g2/keyboard/pointer.ts new file mode 100644 index 00000000..e1d13ede --- /dev/null +++ b/app/g2/keyboard/pointer.ts @@ -0,0 +1,83 @@ +/** + * A keyboard's touchpad or mouse, as the rest of the keyboard support sees + * it. Its buttons and wheel become keys ("MouseButton1", "WheelUp"), so + * they get default meanings and custom keybindings like any key + * (bindings.ts). Its movement goes nowhere unless something has taken the + * pointer (the cursor test page): there is no cursor in the main UI. + * No platform imports: this runs under node in the tests. + */ +import type { HidKeyEvent, HidPointerSample } from "./hid"; +import { HID_PAGE_BUTTON } from "./keys"; + +/** The names a wheel's detents arrive under, as keys. */ +export type WheelKey = "WheelUp" | "WheelDown" | "WheelLeft" | "WheelRight"; + +export type PointerInputHost = { + /** Button transitions, as HID usages on the Button page. */ + buttons(events: HidKeyEvent[]): void; + /** One wheel report in a direction, however many detents it claimed. */ + wheel(key: WheelKey): void; +}; + +export class PointerInput { + /** The buttons the host has been told are down. */ + private reported = 0; + /** The buttons actually down, as of the last sample. */ + private held = 0; + /** Buttons that went down while the pointer was taken: not keys until they have come up. */ + private ignored = 0; + private grab: ((sample: HidPointerSample) => void) | null = null; + + constructor(private readonly host: PointerInputHost) {} + + /** One report from the pointer (see KeyboardClientEvents.pointer). */ + handle(sample: HidPointerSample): void { + this.held = sample.buttons; + if (this.grab) { + this.grab(sample); + return; + } + this.ignored &= sample.buttons; + this.report(sample.buttons & ~this.ignored); + // One step per report: a touchpad's two-finger scroll sends many small + // ones, and the gesture they turn into is rate-limited downstream. + if (sample.wheel) this.host.wheel(sample.wheel > 0 ? "WheelUp" : "WheelDown"); + if (sample.pan) this.host.wheel(sample.pan > 0 ? "WheelRight" : "WheelLeft"); + } + + /** + * Take the pointer: every sample goes to `listener`, movement included, + * and its buttons and wheel stop acting as keys. Buttons held at that + * moment are released first, so nothing is left down. Returns a function + * that gives the pointer back. + */ + take(listener: (sample: HidPointerSample) => void): () => void { + this.report(0); + this.grab = listener; + return () => { + if (this.grab !== listener) return; + this.grab = null; + // A button still down was pressed for the listener, not as a key: its + // release is nobody's business either. + this.ignored = this.held; + }; + } + + /** The keyboard went away: nothing is held, and nobody is told. */ + reset(): void { + this.reported = 0; + this.held = 0; + this.ignored = 0; + } + + private report(buttons: number): void { + const changed = buttons ^ this.reported; + if (!changed) return; + const events: HidKeyEvent[] = []; + for (let bit = 0; bit < 8; bit++) { + if (changed & (1 << bit)) events.push({ page: HID_PAGE_BUTTON, usage: bit + 1, down: !!(buttons & (1 << bit)) }); + } + this.reported = buttons; + this.host.buttons(events); + } +} diff --git a/app/g2/keyboard/protocol.ts b/app/g2/keyboard/protocol.ts new file mode 100644 index 00000000..d70e661a --- /dev/null +++ b/app/g2/keyboard/protocol.ts @@ -0,0 +1,297 @@ +/** + * Wire format of the CFW keyboard bridge (Faceclaw/39). The master temple acts + * as a BLE central for one HID-over-GATT keyboard; the phone drives that link + * over private settings fields and gets the keyboard's raw input reports back. + * The firmware side is g2flash patches/keyboard_core.h, documented in its + * docs/keyboard.md. This follows the reference client in g2flash demos/ + * (keyboard-protocol.ts), which came with the firmware from tetramo-labs. + * + * Phone -> glasses, settings field 130 (wrapped by the native session): + * ['K','B',1,op, request LE16, epoch LE16, body...] at most 80 bytes + * Glasses -> phone, settings field 131: + * ['K','B',1,kind, sequence LE32, epoch LE16, request LE16, code LE16, + * handle LE16, total LE16, offset LE16, timestamp LE32, fragment...] + * + * All integers are little endian. No platform imports: this runs under node + * in the tests. + */ + +export const KEYBOARD_PROTOCOL_VERSION = 1; + +/** Fixed part of a field-131 record, before its fragment. */ +export const KEYBOARD_RECORD_HEADER = 24; +/** Most body bytes one record carries; longer bodies are fragmented. */ +export const KEYBOARD_RECORD_CHUNK = 96; +/** Largest reassembled body (an ATT value or a HID report). */ +export const KEYBOARD_BODY_MAX = 512; +/** Largest control body after the 8-byte prefix. */ +export const KEYBOARD_CONTROL_BODY_MAX = 72; + +export const KeyboardOp = { + query: 0, + enable: 1, + disable: 2, + scan: 3, + connect: 4, + disconnect: 5, + pair: 6, + auth: 7, + compare: 8, + find: 9, + read: 10, + write: 11, + watch: 12, +} as const; +export type KeyboardOpCode = (typeof KeyboardOp)[keyof typeof KeyboardOp]; + +export const KeyboardKind = { + status: 1, + att: 2, + input: 3, + dm: 4, + advert: 5, +} as const; + +/** Control error codes, carried in a STATUS record's `code`. */ +export const KeyboardError = { + ok: 0, + badRequest: 1, + disabled: 2, + busy: 3, + staleEpoch: 4, + noSpace: 5, + notConnected: 6, + notEncrypted: 7, + timeout: 8, + noMemory: 9, +} as const; + +export function keyboardErrorName(code: number): string { + switch (code) { + case KeyboardError.ok: return "ok"; + case KeyboardError.badRequest: return "bad request"; + case KeyboardError.disabled: return "keyboard bridge disabled"; + case KeyboardError.busy: return "busy"; + case KeyboardError.staleEpoch: return "stale epoch"; + case KeyboardError.noSpace: return "no connection or bond space"; + case KeyboardError.notConnected: return "not connected"; + case KeyboardError.notEncrypted: return "not encrypted"; + case KeyboardError.timeout: return "timeout"; + case KeyboardError.noMemory: return "allocation failure"; + default: return `error ${code}`; + } +} + +/** Cordio device-manager events the bridge relays (a DM record's `code & 0xff`). */ +export const KeyboardDm = { + connOpen: 0x27, + connClose: 0x28, + pairComplete: 0x2a, + pairFailed: 0x2b, + encrypted: 0x2c, + encryptFailed: 0x2d, + authRequest: 0x2e, + compare: 0x35, +} as const; + +/** ATT client events (an ATT record's `code & 0xff`; the status is `code >> 8`). */ +export const KeyboardAtt = { + notification: 13, + indication: 14, +} as const; + +export type KeyboardRecord = { + kind: number; + sequence: number; + epoch: number; + request: number; + code: number; + handle: number; + /** Glasses uptime in ms when the record was queued. */ + timestamp: number; + data: Uint8Array; +}; + +export const u16 = (p: Uint8Array, off = 0): number => p[off]! | (p[off + 1]! << 8); +export const u32 = (p: Uint8Array, off = 0): number => + (p[off]! | (p[off + 1]! << 8) | (p[off + 2]! << 16) | (p[off + 3]! << 24)) >>> 0; +export const le16 = (v: number): number[] => [v & 255, (v >>> 8) & 255]; + +/** A field-130 control record. The session wraps it in the settings package. */ +export function keyboardControl(op: number, request: number, epoch: number, body: readonly number[] = []): Uint8Array { + if (!Number.isInteger(op) || op < 0 || op > KeyboardOp.watch || body.length > KEYBOARD_CONTROL_BODY_MAX) { + throw new Error("Keyboard control bounds"); + } + return Uint8Array.from([75, 66, KEYBOARD_PROTOCOL_VERSION, op, ...le16(request), ...le16(epoch), ...body]); +} + +/** + * Bounded, ordered reassembly of field-131 records. `onGap` fires whenever + * the stream may have lost something (a skipped sequence, an epoch change, a + * malformed or orphaned fragment): the caller then releases every held key + * and fails any setup transaction in flight. The firmware's once-a-second + * status record makes a lost final release visible even when no further keys + * arrive. + */ +export class KeyboardRecords { + private epoch: number | undefined; + private last: number | undefined; + private fragment: { record: KeyboardRecord; total: number; used: number } | undefined; + + constructor(private readonly onGap: () => void) {} + + /** Forget the stream position (the phone link dropped); counts as a gap. */ + reset(): void { + this.epoch = undefined; + this.last = undefined; + this.fragment = undefined; + this.onGap(); + } + + /** Feed one field-131 payload; returns a complete record, or undefined. */ + push(p: Uint8Array): KeyboardRecord | undefined { + const invalid = (): undefined => { + this.fragment = undefined; + this.onGap(); + return undefined; + }; + if ( + p.length < KEYBOARD_RECORD_HEADER || p.length > KEYBOARD_RECORD_HEADER + KEYBOARD_RECORD_CHUNK || + p[0] !== 75 || p[1] !== 66 || p[2] !== KEYBOARD_PROTOCOL_VERSION + ) { + return invalid(); + } + const r: KeyboardRecord = { + kind: p[3]!, + sequence: u32(p, 4), + epoch: u16(p, 8), + request: u16(p, 10), + code: u16(p, 12), + handle: u16(p, 14), + timestamp: u32(p, 20), + data: new Uint8Array(0), + }; + const total = u16(p, 16); + const offset = u16(p, 18); + const n = p.length - KEYBOARD_RECORD_HEADER; + if (r.kind < KeyboardKind.status || r.kind > KeyboardKind.advert || total > KEYBOARD_BODY_MAX || + offset + n > total || (total > 0 && n === 0)) { + return invalid(); + } + if (this.epoch !== r.epoch) { + this.onGap(); + this.fragment = undefined; + this.last = undefined; + this.epoch = r.epoch; + } + if (offset === 0) { + if (this.last !== undefined) { + const distance = (r.sequence - this.last) >>> 0; + if (distance === 0 || distance >= 0x80000000) return undefined; // duplicate or old delivery + } + if (this.fragment || (this.last !== undefined && r.sequence !== ((this.last + 1) >>> 0))) this.onGap(); + this.last = r.sequence; + this.fragment = { record: { ...r, data: new Uint8Array(total) }, total, used: 0 }; + } + const f = this.fragment; + if (!f || f.used !== offset || f.total !== total || f.record.sequence !== r.sequence || + f.record.kind !== r.kind || f.record.request !== r.request || f.record.code !== r.code || + f.record.handle !== r.handle || f.record.timestamp !== r.timestamp) { + return invalid(); + } + f.record.data.set(p.subarray(KEYBOARD_RECORD_HEADER), offset); + f.used += n; + if (f.used !== total) return undefined; + this.fragment = undefined; + return f.record; + } +} + +/** BLE address types as the controller reports them. */ +export const KEYBOARD_ADDRESS_TYPES = ["public", "random", "public-identity", "random-identity"] as const; + +/** One advertiser seen during a scan (an ADVERT record's body, parsed). */ +export type KeyboardAdvert = { + addressType: number; + /** "aa:bb:cc:dd:ee:ff", most significant byte first. */ + address: string; + rssi: number; + name: string; + /** Advertises the HID service, or a HID appearance. */ + hid: boolean; +}; + +/** ADVERT body: addrType, addr[6] (LSB first), RSSI, event type, AD length, AD structures. */ +export function parseKeyboardAdvert(body: Uint8Array): KeyboardAdvert | null { + if (body.length < 10 || body[0]! > 3) return null; + let name = ""; + let hid = false; + for (let i = 10; i < body.length;) { + const n = body[i++]!; + if (n === 0 || i + n > body.length) break; + const type = body[i]!; + if (type === 8 || type === 9) name = decodeUtf8(body.subarray(i + 1, i + n)); + // 16-bit service lists (incomplete / complete): 0x1812 is HID. + if (type === 2 || type === 3) { + for (let j = i + 1; j + 1 < i + n; j += 2) if (body[j] === 0x12 && body[j + 1] === 0x18) hid = true; + } + // Appearance 0x03C0..0x03FF is the HID category (0x03C1 keyboard); some + // keyboards advertise that without the service list. + if (type === 0x19 && n >= 3 && body[i + 2] === 0x03 && body[i + 1]! >= 0xc0) hid = true; + i += n; + } + return { + addressType: body[0]!, + address: formatKeyboardAddress(body.subarray(1, 7)), + rssi: (body[7]! << 24) >> 24, + name, + hid, + }; +} + +/** + * Fold a later record for the same advertiser into an earlier one. An active + * scan reports the advertisement and its scan response separately and the + * name usually only rides in the latter: keep a name once seen, HID once + * seen, and the best signal. + */ +export function mergeKeyboardAdverts(a: KeyboardAdvert, b: KeyboardAdvert): KeyboardAdvert { + return { ...a, rssi: Math.max(a.rssi, b.rssi), name: a.name || b.name, hid: a.hid || b.hid }; +} + +/** Controller byte order (LSB first) to the usual "aa:bb:cc:dd:ee:ff". */ +export function formatKeyboardAddress(lsbFirst: Uint8Array): string { + return Array.from(lsbFirst).reverse().map((b) => b.toString(16).padStart(2, "0")).join(":"); +} + +/** "aa:bb:cc:dd:ee:ff" to the controller's LSB-first bytes, or null if malformed. */ +export function parseKeyboardAddress(text: string): Uint8Array | null { + if (!/^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i.test(text)) return null; + return Uint8Array.from(text.split(":").reverse().map((part) => parseInt(part, 16))); +} + +/** Lenient UTF-8 decode (advertised names); no TextDecoder in every runtime this loads in. */ +function decodeUtf8(bytes: Uint8Array): string { + let out = ""; + for (let i = 0; i < bytes.length;) { + const b = bytes[i++]!; + let code = b; + let extra = 0; + if (b >= 0xf0 && b < 0xf8) { code = b & 0x07; extra = 3; } + else if (b >= 0xe0) { code = b & 0x0f; extra = 2; } + else if (b >= 0xc0) { code = b & 0x1f; extra = 1; } + else if (b >= 0x80) { out += "�"; continue; } + if (i + extra > bytes.length) { out += "�"; break; } + let valid = true; + for (let k = 0; k < extra; k++) { + const c = bytes[i + k]!; + if ((c & 0xc0) !== 0x80) { valid = false; break; } + code = (code << 6) | (c & 0x3f); + } + if (!valid || code > 0x10ffff) { out += "�"; continue; } + i += extra; + if (code === 0) continue; + out += String.fromCodePoint(code); + } + return out; +} diff --git a/app/g2/keyboard/service.ts b/app/g2/keyboard/service.ts new file mode 100644 index 00000000..84dfdcea --- /dev/null +++ b/app/g2/keyboard/service.ts @@ -0,0 +1,235 @@ +/** + * The keyboard bridge as the app uses it: one KeyboardBridge for the life of + * the app, fed by whichever glasses session is live, its remembered keyboard + * and the custom keybindings kept in the settings store, and its key + * transitions turned into KeyEvents and routed. The platform's controller + * reports the link coming and going and performs the gestures keys stand + * for; the Settings app drives pairing through `bridge` and edits the + * keybindings. + */ +import { addKeyboardEventListener, keyboardLinkSession, sendKeyboardControlRecord } from "../../native/keyboard-link"; +import { getStringSetting, setStringSetting } from "../../native/settings-store"; +import { shell } from "../../ui/shell/shell"; +import { + MAX_KEY_BINDINGS, + chordId, + keyBindingMap, + parseKeyBindings, + serializeKeyBindings, + type GestureOrigin, + type KeyAction, + type KeyBinding, + type KeyChord, + type KeyGesture, +} from "./bindings"; +import { KeyboardBridge, type KeyboardBridgeSnapshot, type SavedKeyboard } from "./bridge"; +import type { HidPointerSample } from "./hid"; +import { KeyRouter } from "./key-router"; +import { KeyTranslator, type KeyEvent } from "./keys"; +import { PointerInput } from "./pointer"; + +declare const java: any; +declare const arc4random_uniform: ((bound: number) => number) | undefined; + +/** JSON of the remembered keyboard ({ address, addressType, name }), or "" for none. */ +const SAVED_KEYBOARD_KEY = "keyboard.saved"; +/** JSON list of the custom keybindings ({ key, action }); see bindings.ts. */ +const KEY_BINDINGS_KEY = "keyboard.bindings"; + +/** What the platform's controller does for the keyboard. */ +export type KeyboardServiceHost = { + /** Connected to glasses that are in use (not charging in their case). */ + ready(): boolean; + locked(): boolean; + /** Turn a dark display on, and nothing else (a locked display stays locked). */ + wake(): void; + /** Perform a gesture as if it came from the ring or the watch. */ + gesture(gesture: KeyGesture, origin: GestureOrigin): Promise<void>; + /** Routine lines only (failed background reconnects); the rest go to the device log directly. */ + log(line: string): void; + /** Whether an absent keyboard is worth looking for now (see KeyboardBridgeHost). */ + shouldReconnect(): boolean; +}; + +function loadSavedKeyboard(): SavedKeyboard | null { + try { + const raw = getStringSetting(SAVED_KEYBOARD_KEY, ""); + if (!raw) return null; + const value = JSON.parse(raw); + if (typeof value?.address !== "string" || typeof value?.addressType !== "number") return null; + return { address: value.address, addressType: value.addressType, name: typeof value.name === "string" ? value.name : "" }; + } catch { + return null; + } +} + +/** Passkey entry only protects the pairing if the passkey can't be guessed. */ +function randomPasskey(): number { + try { + if (global.isAndroid) return new java.security.SecureRandom().nextInt(1_000_000); + if (typeof arc4random_uniform === "function") return arc4random_uniform(1_000_000); + } catch { + // Fall through to the weaker source rather than failing the pairing. + } + return Math.floor(Math.random() * 1_000_000); +} + +class KeyboardService { + private host: KeyboardServiceHost | null = null; + private session: unknown = null; + private offEvents: (() => void) | null = null; + private readonly listeners = new Set<() => void>(); + private bindings: KeyBinding[] = parseKeyBindings(getStringSetting(KEY_BINDINGS_KEY, "")); + private bindingMap = keyBindingMap(this.bindings); + + private readonly router = new KeyRouter({ + ready: () => this.host?.ready() ?? false, + locked: () => this.host?.locked() ?? false, + screenOn: () => shell.isScreenOn(), + wake: () => this.host?.wake(), + offerToWindow: (event) => shell.receiveKeyEvent(event), + gesture: (gesture, origin) => this.host?.gesture(gesture, origin) ?? Promise.resolve(), + bindings: () => this.bindingMap, + noteActivity: () => shell.noteUserActivity(), + log: (line) => console.log(`[keyboard] ${line}`), + }); + private readonly translator = new KeyTranslator({ emit: (event) => this.router.handleKey(event) }); + /** The touchpad's buttons and wheel go in as keys; see pointer.ts. */ + private readonly pointer = new PointerInput({ + buttons: (events) => this.translator.handle(events), + wheel: (key) => this.translator.pulse(key), + }); + + readonly bridge = new KeyboardBridge({ + send: (record) => { + const session = this.session; + if (!session) return false; + try { + return sendKeyboardControlRecord(session, record, `op=${record[3]}`); + } catch { + return false; + } + }, + loadSaved: loadSavedKeyboard, + storeSaved: (saved) => setStringSetting(SAVED_KEYBOARD_KEY, saved ? JSON.stringify(saved) : ""), + onKeys: (events) => this.translator.handle(events), + onPointer: (sample) => this.pointer.handle(sample), + onKeyboardGone: () => { + this.translator.reset(); + this.pointer.reset(); + this.router.reset(); + }, + onChanged: () => { + for (const listener of [...this.listeners]) listener(); + }, + // Pairing and setup are rare and are what a failure report needs, so + // they always reach the device log. Nothing here ever includes a key. + log: (line, routine) => { + if (routine) this.host?.log(line); + else console.log(`[keyboard] ${line}`); + }, + randomPasskey, + shouldReconnect: () => this.host?.shouldReconnect() ?? true, + }); + + setHost(host: KeyboardServiceHost): void { + this.host = host; + } + + snapshot(): KeyboardBridgeSnapshot { + return this.bridge.snapshot(); + } + + /** Called on every bridge state change. Returns an unsubscribe function. */ + subscribe(listener: () => void): () => void { + this.listeners.add(listener); + return () => this.listeners.delete(listener); + } + + /** The glasses came into use (the display woke): look for an absent keyboard now. */ + retryNow(): void { + this.bridge.retryNow(); + } + + /** A G2 session on firmware with the bridge is up. Idempotent. */ + linkReady(): void { + const session = keyboardLinkSession(); + if (!session) return; + if (session !== this.session) { + // The native session object is rebuilt across reconnects. + this.offEvents?.(); + this.session = session; + this.offEvents = addKeyboardEventListener(session, (body) => this.bridge.accept(body)); + } + this.bridge.linkReady(); + } + + /** + * The session can't host the keyboard any more. `release` asks the firmware + * to let the keyboard go first, for when the link itself stays up (the + * glasses went into their case, or the app is disconnecting on purpose). + * Idempotent. + */ + linkLost(release = false): void { + if (release) this.bridge.linkClosing(); + this.bridge.linkLost(); + this.offEvents?.(); + this.offEvents = null; + this.session = null; + } + + /** A key a worker's window was sent and had no use for (the "key-unhandled" reply). */ + handleUnhandledKey(event: KeyEvent): void { + this.router.handleUnhandledKey(event); + } + + // ---------- Custom keybindings ---------- + + /** The custom keybindings, in the order they were added. */ + keyBindings(): readonly KeyBinding[] { + return this.bindings; + } + + /** Bind a chord, replacing what it was bound to. False when the list is full. */ + setKeyBinding(chord: KeyChord, action: KeyAction): boolean { + const id = chordId(chord); + const index = this.bindings.findIndex((binding) => chordId(binding.chord) === id); + if (index < 0 && this.bindings.length >= MAX_KEY_BINDINGS) return false; + const next = [...this.bindings]; + if (index < 0) next.push({ chord, action }); + else next[index] = { chord, action }; + this.storeBindings(next); + return true; + } + + removeKeyBinding(chord: KeyChord): void { + const id = chordId(chord); + this.storeBindings(this.bindings.filter((binding) => chordId(binding.chord) !== id)); + } + + /** + * Take the next key pressed on the keyboard instead of acting on it. Returns + * a function that calls the capture off. + */ + captureNextKey(onChord: (chord: KeyChord) => void): () => void { + return this.router.captureNextKey(onChord); + } + + /** + * Take the keyboard's touchpad or mouse: every report goes to `listener`, + * movement included, and its buttons and wheel stop acting as keys until + * the returned function is called. For the cursor test page; the main UI + * has no cursor. + */ + takePointer(listener: (sample: HidPointerSample) => void): () => void { + return this.pointer.take(listener); + } + + private storeBindings(bindings: KeyBinding[]): void { + this.bindings = bindings; + this.bindingMap = keyBindingMap(bindings); + setStringSetting(KEY_BINDINGS_KEY, serializeKeyBindings(bindings)); + } +} + +export const keyboardService = new KeyboardService(); diff --git a/app/native/device-discovery.ios.ts b/app/native/device-discovery.ios.ts index 519cf51d..d1ea9651 100644 --- a/app/native/device-discovery.ios.ts +++ b/app/native/device-discovery.ios.ts @@ -3,11 +3,18 @@ import type { DiscoveryEvents } from './device-discovery' import type { RawAdvertisement } from '../g2/even-advertisement' export { buildAddressSet } from './device-discovery-common' +/** + * How often a scan re-reads which arms the system is connected to. Each read refreshes + * their rows, which age out like advertisements once the link drops and they go quiet. + */ +const CONNECTED_ARMS_POLL_MS = 2000 + export class DeviceDiscoveryBridge { private ble = iosBluetooth() private off: (() => void) | null = null private generation = 0 private cancelCollection: (() => void) | null = null + private connectedArmsTimer: ReturnType<typeof setInterval> | null = null get bluetoothEnabled(): boolean { return this.ble.state === 5 } get scanning(): boolean { return !!this.off && this.ble.scanning } startScan(events: DiscoveryEvents): boolean { @@ -21,16 +28,27 @@ export class DeviceDiscoveryBridge { void this.ble.startScan(24 * 60 * 60 * 1000).catch(error => { if (generation === this.generation) events.onScanFailed?.(-1, String(error.message ?? error)) }) + // A connected arm doesn't advertise (iOS holds the right arm's ANCS link open after a + // session ends), so the scan alone would show those glasses one-armed. + const reportConnectedArms = () => { for (const device of this.ble.systemConnectedDevices()) events.onAdvertisement(device) } + reportConnectedArms() + this.connectedArmsTimer = setInterval(reportConnectedArms, CONNECTED_ARMS_POLL_MS) return true } stopScan(): void { ++this.generation; this.cancelCollection?.() + if (this.connectedArmsTimer) { clearInterval(this.connectedArmsTimer); this.connectedArmsTimer = null } if (this.off) { this.off(); this.off = null; this.ble.stopScan() } } // CoreBluetooth does not expose the phone's bond list. Fresh advertisements - // supply both identity and the UUID mapping that iOS needs for connections. + // supply both identity and the UUID mapping that iOS needs for connections; + // startScan() reports the arms the system is connected to. emitBondedDevices(): void {} - async getBondedCandidates(): Promise<RawAdvertisement[]> { return [...this.ble.devices.values()] } + async getBondedCandidates(): Promise<RawAdvertisement[]> { + const devices = new Map(this.ble.devices) + for (const device of this.ble.systemConnectedDevices()) if (!devices.has(device.address)) devices.set(device.address, device) + return [...devices.values()] + } async scanCandidates(timeoutMs = 6000): Promise<RawAdvertisement[]> { const generation = this.generation await this.ble.ensureReady() diff --git a/app/native/faceclaw-communicator.ios.ts b/app/native/faceclaw-communicator.ios.ts index 563c6eb0..6baba51a 100644 --- a/app/native/faceclaw-communicator.ios.ts +++ b/app/native/faceclaw-communicator.ios.ts @@ -19,6 +19,7 @@ declare const FaceclawKitFaceclawCompassListener: any; declare const FaceclawKitFaceclawImuListener: any; declare const FaceclawKitIosAmbientLightListener: any; declare const FaceclawKitIosMicStatusListener: any; +declare const FaceclawKitIosKeyboardEventListener: any; export type CommunicatorPhase = @@ -198,6 +199,13 @@ class MicStatusListener extends NSObject { onNsDataMicStatusBodyArm(body: NSData, arm: string): void { this.handler(fromData(body), String(arm ?? "")); } } +@NativeClass() +class KeyboardEventListener extends NSObject { + static ObjCProtocols = [FaceclawKitIosKeyboardEventListener]; + handler!: (body: Uint8Array) => void; + onNsDataKeyboardEventBody(body: NSData): void { this.handler(fromData(body)); } +} + /** * The iOS twin of the Android communicator bridge: the same public API over * the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth @@ -803,6 +811,19 @@ export class FaceclawCommunicatorBridge { toData(new Uint8Array(record)), label, Boolean(rightTemple), Boolean(leftTemple))); } + /** Raw field-131 keyboard-bridge records, in arrival order, on the main queue. */ + addKeyboardEventListener(listener: (body: Uint8Array) => void): () => void { + const proxy = KeyboardEventListener.new() as KeyboardEventListener; + proxy.handler = listener; + const handle = this.communicator.addKeyboardEventListenerNsDataListener(proxy); + return () => { this.communicator.removeKeyboardEventListenerListener(handle); }; + } + + /** Queue a CFW keyboard-bridge control for the right temple; false when nothing was queued. */ + sendFaceclawKeyboardControl(record: Uint8Array, label: string): boolean { + return Boolean(this.communicator.sendFaceclawKeyboardControlRecordLabel(toData(new Uint8Array(record)), label)); + } + /** * Every audio-characteristic packet, whatever enabled the mics, on the * main queue. Passive: sends nothing and doesn't affect capture. diff --git a/app/native/g2mirror-client.ts b/app/native/g2mirror-client.ts index b93f18c7..16956df8 100644 --- a/app/native/g2mirror-client.ts +++ b/app/native/g2mirror-client.ts @@ -1,5 +1,6 @@ -import { openSocket } from "./socket"; +import { openSocket, type SocketConnection, type SocketListener } from "./socket"; import { toUint8Array } from "../util/array-util"; +import type { G2MirrorHistoryReply, G2MirrorPhase, G2MirrorSession, G2MirrorState } from "./terminal-session-types"; declare const android: any; declare const java: any; @@ -19,54 +20,7 @@ const LAUNCH_REPLY_TIMEOUT_MS = 10_000; // otherwise treat the "\r" as a pasted newline instead of the Enter key. const SUBMIT_DELAY_MS = 150; -export type G2MirrorSession = { - socket: string; - pid: number; - cwdHint: string; - /** Unix epoch ms of the terminal's last bell, or null if none observed. */ - lastBellAt: number | null; - /** - * Unix epoch ms (host clock) of the app's last output, or null if none - * observed. Fresh to within the wrapper's 2s activity reporting interval - * while we're connected; compare against other host timestamps, not the - * phone clock (see the worker's local-receive-time activity tracking). - */ - lastOutputAt: number | null; - /** Window title the app last set (xterm OSC 0/2), or null if none observed. */ - title: string | null; -}; - -export type G2MirrorPhase = - | "idle" - | "connecting" - | "connected" // handshake accepted; can list/attach - | "attached" // relaying to one session - | "failed"; - -export type G2MirrorState = { - phase: G2MirrorPhase; - status: string; - sessions: G2MirrorSession[]; - attachedCommand: string; - /** - * server_name from the init success reply: a human-readable name for the - * machine the server runs on. "" until the handshake succeeds (or when an - * older server omits the field). - */ - serverName: string; -}; - -/** A page of archived scrollback lines (see PROTOCOL.md "Scrollback history"). */ -export type G2MirrorHistoryReply = { - /** Absolute index of lines[0]; the page covers [start, start + lines.length). */ - start: number; - /** Oldest index still retained by the wrapper (older requests return nothing). */ - oldest: number; - /** Current splice point (next line to be archived). */ - next: number; - /** Plain text, ANSI-stripped, oldest-to-newest. */ - lines: string[]; -}; +export type { G2MirrorHistoryReply, G2MirrorPhase, G2MirrorSession, G2MirrorState }; export type G2MirrorClientOptions = { /** TLS (g2mirrors:// → wss) vs plain (g2mirror:// → ws). */ @@ -77,6 +31,12 @@ export type G2MirrorClientOptions = { deviceName: string; cols: number; rows: number; + /** + * Opens the websocket some other way than dialing host:port directly + * (e.g. through an SSH tunnel); `label` then names the endpoint in status + * lines instead of host:port. + */ + transport?: { open: (listener: SocketListener) => SocketConnection; label: string }; }; type TerminalDataKind = "snapshot" | "output"; @@ -180,7 +140,8 @@ export class G2MirrorClient { this.stopped = false; const generation = ++this.connectionGeneration; const url = `${this.options.secure ? "wss" : "ws"}://${this.options.host}:${this.options.port}`; - this.setState("connecting", `Connecting to ${this.options.host}:${this.options.port}...`); + const transport = this.options.transport; + this.setState("connecting", `Connecting to ${transport ? transport.label : `${this.options.host}:${this.options.port}`}...`); this.listenerProxy = { onOpen: () => { if (this.stopped || generation !== this.connectionGeneration) return; @@ -208,7 +169,7 @@ export class G2MirrorClient { }, }; try { - this.ws = openSocket(url, this.listenerProxy); + this.ws = transport ? transport.open(this.listenerProxy!) : openSocket(url, this.listenerProxy); } catch (error) { this.ws = null; this.handleConnectionLost(`Connection failed: ${shortenError(String((error as Error)?.message ?? error))}`); diff --git a/app/native/glasses-controls.ts b/app/native/glasses-controls.ts new file mode 100644 index 00000000..f5d3cc30 --- /dev/null +++ b/app/native/glasses-controls.ts @@ -0,0 +1,65 @@ +declare const com: any; + +/** + * Head-up (look-up wake) trigger on the active glasses session + * (GlassesSessionCore.setHeadUpTrigger / getHeadUpStatus). Android only for + * now (headUpControlsAvailable): on iOS, or with no session, the setter does + * nothing and the getter returns null. + */ + +/** Whether this platform can change the glasses' head-up setting at all. */ +export function headUpControlsAvailable(): boolean { + return Boolean(global.isAndroid); +} + +/** GlassesSessionCore.getHeadUpStatus. switch -1 = not read on this connection. */ +export type HeadUpStatus = { + switch: number; + angle: number; + calibration: number; + /** The trigger Faceclaw is maintaining, or -1 when it leaves the glasses' own setting. */ + desired: number; + readAgeMs: number; + /** The glasses' own switch and angle from before Faceclaw changed them (switch -1 = none held). */ + originalSwitch: number; + originalAngle: number; + /** Those originals are being written back. */ + restorePending: boolean; +}; + +function activeCommunicator(): any { + if (!global.isAndroid) return null; + try { + return com.faceclaw.app.FaceclawBleCommunicator.getActive(); + } catch { + return null; + } +} + +/** + * Ask the session to keep the head-up trigger at `degrees` (1..90); 0 stops + * managing it and writes back `original` (or what the session saved itself) + * if Faceclaw changed the glasses' values. + */ +export function setHeadUpTrigger(degrees: number, original: { switch: number; angle: number } | null = null): boolean { + const active = activeCommunicator(); + if (!active) return false; + try { + active.setHeadUpTrigger(Math.round(degrees), original ? original.switch : -1, original ? Math.round(original.angle) : 0); + return true; + } catch (error) { + console.warn(`setHeadUpTrigger failed: ${error}`); + return false; + } +} + +export function getHeadUpStatus(): HeadUpStatus | null { + const active = activeCommunicator(); + if (!active) return null; + try { + return JSON.parse(String(active.getHeadUpStatus())) as HeadUpStatus; + } catch (error) { + console.warn(`getHeadUpStatus failed: ${error}`); + return null; + } +} diff --git a/app/native/imu-types.ts b/app/native/imu-types.ts index bc0eb693..4194d7bc 100644 --- a/app/native/imu-types.ts +++ b/app/native/imu-types.ts @@ -1,7 +1,23 @@ /** Platform-neutral IMU types, shared by imu.ts and imu.ios.ts. */ +/** + * An accelerometer sample in g, in Faceclaw's frame for every glasses model + * (the G2's own; the session rotates other models' samples into it, see + * ImuOrientation.kt): +x forward, +y to the wearer's left, +z up through the + * top of the head. At rest it is the "up" direction: head level reads + * (0, 0, 1), looking 45° up (0.71, 0, 0.71), the head tilted 45° to the right + * (0, 0.71, 0.71). + */ export type ImuReading = { x: number; y: number; z: number; source: number }; +/** + * How far the head is tilted up from level, in degrees (negative: down), from + * a reading taken at rest. Rolling the head to the side does not change it. + */ +export function imuPitchDegrees(reading: { x: number; y: number; z: number }): number { + return (Math.atan2(reading.x, Math.hypot(reading.y, reading.z)) * 180) / Math.PI; +} + /** Map an IMU sample's EventSource to a short label. */ export function imuSourceLabel(source: number): string { switch (source) { diff --git a/app/native/ios-bluetooth.ts b/app/native/ios-bluetooth.ts index aac0b385..b19217f1 100644 --- a/app/native/ios-bluetooth.ts +++ b/app/native/ios-bluetooth.ts @@ -1,4 +1,7 @@ import { ApplicationSettings } from '@nativescript/core' +import { loadPairedGlassesIdentity } from '../g2/device-addresses' +import { g2ManufacturerData } from '../g2/even-advertisement' +import { serialKey } from '../g2/glasses-hardware-identity' import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity' import { hexToBytes } from '../util/hex-util' import { kotlinListener } from './kotlin-listener.ios' @@ -10,6 +13,13 @@ export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_PO /** CBManagerAuthorization.allowedAlways. */ const BLE_AUTHORIZATION_ALLOWED = 3 +/** + * Per address, the peripheral identifier and side last heard there and (since 2026-11-16) + * the advertisement itself, from which an arm that has stopped advertising is rebuilt. + */ +const PERIPHERAL_KEY_PREFIX = 'ios.ble.peripheral.' +type RememberedPeripheral = { identifier: string; role: string; name?: string; manufacturerData?: string } + export type BluetoothEvent = | { kind: 'state'; state: number; authorization: number } | { kind: 'advertisement'; identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean } @@ -28,6 +38,8 @@ export class IosBluetooth { private readonly native = FaceclawKitIosBleCentral.alloc().initWithPlatform(FaceclawKitIosProtocolPlatform.shared) private readonly listeners = new Set<(event: BluetoothEvent) => void>() readonly devices = new Map<string, IosDevice>() + /** The record last written per settings key, so repeated advertisements don't rewrite it. */ + private readonly rememberedValues = new Map<string, string>() private scanTimer: ReturnType<typeof setTimeout> | null = null private scanGeneration = 0 private readonly scanListener: object @@ -47,12 +59,68 @@ export class IosBluetooth { const device = identifyIosPeripheral(raw) if (device) { this.devices.set(device.address, device) - ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role })) + this.remember(device) this.emit({ kind: 'device', device }) } }, }) } + /** Persists what `device` advertised; skips the write when nothing changed (scans report duplicates). */ + private remember(device: IosDevice): void { + const key = `${PERIPHERAL_KEY_PREFIX}${device.address}` + const record: RememberedPeripheral = { identifier: device.identifier, role: device.role, name: device.name, manufacturerData: device.manufacturerData } + const value = JSON.stringify(record) + if (this.rememberedValues.get(key) === value) return + this.rememberedValues.set(key, value) + ApplicationSettings.setString(key, value) + } + /** + * Even arms the system is connected to, rebuilt from what they last advertised here. A + * connected arm doesn't advertise, and iOS keeps the right arm's ANCS link open after a + * session ends (even after pairing other glasses), so a scan alone hears only the left + * arm. An arm this phone has never heard advertise can't be rebuilt: its MAC and the + * pair's serial come only from the advertisement. The device has no signal strength. + */ + systemConnectedDevices(): IosDevice[] { + if (this.state !== BLE_STATE_POWERED_ON) return [] + let connected: any + try { + connected = this.native.systemConnectedG2Arms() + } catch (error) { + // Scanning still works without this; don't let it take the pairing page down. + console.log(`[IosBluetooth] systemConnectedG2Arms failed: ${error}`) + return [] + } + if (!connected?.count) return [] + const records = new Map<string, { address: string; record: RememberedPeripheral }>() + for (const key of ApplicationSettings.getAllKeys()) { + if (!key.startsWith(PERIPHERAL_KEY_PREFIX)) continue + try { + const record = JSON.parse(ApplicationSettings.getString(key, '{}')) as RememberedPeripheral + if (typeof record.identifier === 'string') records.set(record.identifier.toUpperCase(), { address: key.slice(PERIPHERAL_KEY_PREFIX.length), record }) + } catch { /* Malformed: a fresh advertisement rewrites it. */ } + } + const identity = loadPairedGlassesIdentity() + const devices: IosDevice[] = [] + for (let i = 0; i < connected.count; i++) { + const peripheral = connected.objectAtIndex(i) + const identifier = String(peripheral.identifier) + const remembered = records.get(identifier) + if (!remembered) continue + const { address, record } = remembered + let name = record.name ?? '' + let manufacturerData = record.manufacturerData ?? '' + if (!manufacturerData && identity && (address === identity.leftAddress || address === identity.rightAddress)) { + // Remembered before advertisements were: the saved pairing supplies the serial. + const side = address === identity.leftAddress ? 'left' : 'right' + name ||= side === 'left' ? identity.leftName : identity.rightName + manufacturerData = g2ManufacturerData(serialKey(identity.serial) ?? '', address) ?? '' + } + const device = identifyIosPeripheral({ identifier, name: name || String(peripheral.name ?? ''), manufacturerData, rssi: 0, connectable: true }) + if (device?.address === address && device.role === record.role) devices.push({ ...device, rssi: null, source: 'paired' }) + } + return devices + } onEvent(listener: (event: BluetoothEvent) => void): () => void { this.listeners.add(listener); return () => { this.listeners.delete(listener) } } @@ -111,7 +179,7 @@ export class IosBluetooth { const live = this.devices.get(address) if (live && live.role !== role) throw new Error(`${address} advertises as ${live.role}, not ${role}. Check Configure devices.`) try { - const saved = JSON.parse(ApplicationSettings.getString(`ios.ble.peripheral.${address}`, '{}')) + const saved = JSON.parse(ApplicationSettings.getString(`${PERIPHERAL_KEY_PREFIX}${address}`, '{}')) if (saved.role === role && typeof saved.identifier === 'string') result[role] = saved.identifier } catch { /* A fresh scan can recover malformed settings. */ } } @@ -138,13 +206,15 @@ export class IosBluetooth { /** The peripheral last heard at this address, if it was on this side. */ peripheralIdentifier(address: string, role: string): string | null { try { - const saved = JSON.parse(ApplicationSettings.getString(`ios.ble.peripheral.${address}`, '{}')) + const saved = JSON.parse(ApplicationSettings.getString(`${PERIPHERAL_KEY_PREFIX}${address}`, '{}')) return saved.role === role && typeof saved.identifier === 'string' ? saved.identifier : null } catch { return null } } - forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) } + forget(address: string): void { + ApplicationSettings.remove(`${PERIPHERAL_KEY_PREFIX}${address}`); this.rememberedValues.delete(`${PERIPHERAL_KEY_PREFIX}${address}`); this.devices.delete(address) + } } let instance: IosBluetooth | null = null export function iosBluetooth(): IosBluetooth { return instance ??= new IosBluetooth() } diff --git a/app/native/keyboard-link.ios.ts b/app/native/keyboard-link.ios.ts new file mode 100644 index 00000000..7dcc52b2 --- /dev/null +++ b/app/native/keyboard-link.ios.ts @@ -0,0 +1,24 @@ +import { FaceclawCommunicatorBridge } from "./faceclaw-communicator.ios"; + +/** The iOS twin of keyboard-link.ts, over the running shared-Kotlin session. */ + +export function keyboardLinkSession(): unknown { + return FaceclawCommunicatorBridge.getActive(); +} + +/** Queue a control record for the right temple; false when the session wasn't ready and nothing was queued. */ +export function sendKeyboardControlRecord(session: unknown, record: Uint8Array, label: string): boolean { + return (session as FaceclawCommunicatorBridge).sendFaceclawKeyboardControl(record, label); +} + +/** Raw keyboard-bridge records (undecoded, possibly fragments), in arrival order. Returns an unsubscribe function. */ +export function addKeyboardEventListener(session: unknown, listener: (body: Uint8Array) => void): () => void { + const unsubscribe = (session as FaceclawCommunicatorBridge).addKeyboardEventListener(listener); + return () => { + try { + unsubscribe(); + } catch { + // The session may have been closed during a reconnect. + } + }; +} diff --git a/app/native/keyboard-link.ts b/app/native/keyboard-link.ts new file mode 100644 index 00000000..954cd24f --- /dev/null +++ b/app/native/keyboard-link.ts @@ -0,0 +1,40 @@ +import { toJavaBytes } from "./cloud-stt"; +import { toUint8Array } from "../util/array-util"; + +declare const com: any; + +/** + * Platform plumbing for the CFW keyboard bridge (app/g2/keyboard/): the running + * session to send field-130 controls on and to hear field-131 records from. + * The session is opaque and is a different object after a reconnect. + */ + +export function keyboardLinkSession(): unknown { + if (!global.isAndroid) return null; + try { + return com.faceclaw.app.FaceclawBleCommunicator.getActive(); + } catch { + return null; + } +} + +/** Queue a control record for the right temple; false when the session wasn't ready and nothing was queued. */ +export function sendKeyboardControlRecord(session: unknown, record: Uint8Array, label: string): boolean { + return (session as any).sendFaceclawKeyboardControl(toJavaBytes(record), label) !== false; +} + +/** Raw keyboard-bridge records (undecoded, possibly fragments), in arrival order. Returns an unsubscribe function. */ +export function addKeyboardEventListener(session: unknown, listener: (body: Uint8Array) => void): () => void { + const communicator = session as any; + const proxy = new com.faceclaw.app.FaceclawKeyboardEventListener({ + onKeyboardEvent: (body: any) => listener(toUint8Array(body)), + }); + communicator.addKeyboardEventListener(proxy); + return () => { + try { + communicator.removeKeyboardEventListener(proxy); + } catch { + // The old communicator instance may be gone after a reconnect. + } + }; +} diff --git a/app/native/mic-recorder.ios.ts b/app/native/mic-recorder.ios.ts new file mode 100644 index 00000000..e5f76a3b --- /dev/null +++ b/app/native/mic-recorder.ios.ts @@ -0,0 +1,50 @@ +import { knownFolders, path as paths } from '@nativescript/core' + +// Kotlin/Native facade exported by FaceclawKit (native/kotlin/shared/src/iosMain/.../IosMicRecorder.kt). +declare const FaceclawKitIosMicRecordings: any + +/** + * Records 16 kHz mono PCM16 to a Matroska audio file (.mka) in + * Documents/Downloads while it is captured, through the shared Kotlin writer + * that Android's captures are recorded with. The file is written about a + * second at a time, so a crash leaves everything up to the last second. + */ +export class MicRecorder { + private constructor(private recorder: any, readonly path: string) {} + + /** + * A recorder for Downloads/<name>.mka (a -2, -3... suffix if taken), or null. + * `info` describes the capture and is stored in the file. + */ + static open(name: string, info: Record<string, unknown> = {}): MicRecorder | null { + const fm = NSFileManager.defaultManager + const dir = paths.join(knownFolders.documents().path, 'Downloads') + try { + fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(dir, true, null) + let path = paths.join(dir, `${name}.mka`) + for (let n = 2; fm.fileExistsAtPath(path); n++) path = paths.join(dir, `${name}-${n}.mka`) + const recorder = FaceclawKitIosMicRecordings.shared.openPathConfigJson(path, JSON.stringify(info)) + return recorder ? new MicRecorder(recorder, path) : null + } catch { + return null + } + } + + append(pcm: Uint8Array): void { + if (!this.recorder || !pcm.length) return + // The handle is the buffer's start: a view into a larger buffer is copied out first. + const bytes = pcm.byteOffset === 0 && pcm.byteLength === pcm.buffer.byteLength ? pcm : pcm.slice() + try { + // Copied by the recorder before this returns. + this.recorder.appendPcm16le(NSData.dataWithBytesLength(interop.handleof(bytes.buffer), bytes.byteLength)) + } catch { + // Stop recording rather than retry. + this.close() + } + } + + close(): void { + try { this.recorder?.finish() } catch { /* already closed */ } + this.recorder = null + } +} diff --git a/app/native/mic-recording.ts b/app/native/mic-recording.ts new file mode 100644 index 00000000..0c8663f1 --- /dev/null +++ b/app/native/mic-recording.ts @@ -0,0 +1,29 @@ +declare const android: any; +declare const java: any; + +/** Extension of the recordings Save Recording writes, and Transcribe replays (Matroska audio). */ +export const RECORDING_EXTENSION = ".mka"; + +export function isMicRecordingFile(name: string): boolean { + return name.toLowerCase().endsWith(RECORDING_EXTENSION); +} + +/** + * The path for a new recording in the public Downloads directory: + * Downloads/<name>.mka, with a -2, -3... suffix if that is taken. Null when + * Downloads isn't available. The file itself is created by whoever records. + */ +export function newRecordingPath(name: string): string | null { + try { + const downloads = android.os.Environment.getExternalStoragePublicDirectory( + android.os.Environment.DIRECTORY_DOWNLOADS, + ); + downloads.mkdirs(); + let file = new java.io.File(downloads, `${name}${RECORDING_EXTENSION}`); + for (let n = 2; file.exists(); n++) file = new java.io.File(downloads, `${name}-${n}${RECORDING_EXTENSION}`); + return String(file.getAbsolutePath()); + } catch (error) { + console.warn(`Voice: no Downloads directory for ${name}${RECORDING_EXTENSION}: ${error}`); + return null; + } +} diff --git a/app/native/ssh-common.ts b/app/native/ssh-common.ts new file mode 100644 index 00000000..6d60cc80 --- /dev/null +++ b/app/native/ssh-common.ts @@ -0,0 +1,153 @@ +/** + * Platform-independent half of the SSH transport bindings (ssh.ts / + * ssh.ios.ts): the types the app uses and the decoding of the Go + * transport's events (native/ssh/faceclawssh.go, Listener). + */ + +/** Where and as whom to connect. */ +export type SshEndpoint = { + host: string; + port: number; + user: string; + /** Pinned host keys (SSH wire format, base64). Empty: first connection reports hostkey-unknown. */ + hostKeys: string[]; + /** Saved password, or "" to try only the device key. */ + password: string; + /** Offer the device's client key. */ + useKey: boolean; +}; + +export type SshChannelSpec = + | { + /** Run a command on the host; text is exchanged a line at a time. */ + type: "exec"; + command: string; + } + | { + /** WebSocket to a URL resolved on the host's side (e.g. ws://127.0.0.1:8737/). */ + type: "websocket"; + url: string; + }; + +export type SshHostKey = { + /** SSH key type, e.g. "ssh-ed25519". */ + type: string; + /** OpenSSH-style "SHA256:..." fingerprint. */ + fingerprint: string; + /** SSH wire format, base64: the value to pin in SshEndpoint.hostKeys. */ + key: string; +}; + +export type SshFailureKind = + | "hostkey-unknown" + | "hostkey-mismatch" + | "auth" + | "network" + | "channel" + | "config" + | "internal"; + +export type SshFailure = { + kind: SshFailureKind; + message: string; + /** For hostkey-*: the key the host presented. */ + hostKey?: SshHostKey; + /** For auth: the host takes passwords / we sent one / we offered our key. */ + passwordAllowed?: boolean; + passwordTried?: boolean; + keyTried?: boolean; +}; + +export type SshClosed = { + /** Exec channels: the command's exit status (and signal, if killed). */ + exitStatus?: number; + signal?: string; + /** WebSocket channels: the close frame's code and reason. */ + code?: number; + reason?: string; +}; + +export type SshChannelListener = { + onOpen(): void; + onText(text: string): void; + onStderr?(line: string): void; + /** Terminal: the channel ended normally. */ + onClosed(info: SshClosed): void; + /** Terminal: the channel couldn't open or was lost. */ + onFailure(failure: SshFailure): void; +}; + +export type SshChannel = { + /** Queue a websocket text frame / an exec stdin line. False once closed. */ + send(text: string): boolean; + /** Flush queued sends, then end the channel. No events follow. */ + close(): void; +}; + +export type SshClientKey = { + /** "ecdsa-sha2-nistp256 AAAA... comment", for ~/.ssh/authorized_keys. */ + authorizedKey: string; + fingerprint: string; +}; + +/** Build the Go transport's openSpec JSON. */ +export function encodeSshSpec(endpoint: SshEndpoint, channel: SshChannelSpec): string { + return JSON.stringify({ + host: endpoint.host, + port: endpoint.port, + user: endpoint.user, + password: endpoint.password, + hostKeys: endpoint.hostKeys, + useKey: endpoint.useKey, + channel, + }); +} + +/** Route one transport event to the listener. Unknown kinds are ignored. */ +export function dispatchSshEvent(listener: SshChannelListener, kind: string, payload: string): void { + switch (kind) { + case "open": + listener.onOpen(); + return; + case "text": + listener.onText(payload); + return; + case "stderr": + listener.onStderr?.(payload); + return; + case "closed": + listener.onClosed(parseJson(payload) ?? {}); + return; + case "failure": { + const failure = parseJson(payload); + listener.onFailure( + failure && typeof failure.kind === "string" + ? failure + : { kind: "internal", message: `bad failure payload: ${payload}` }, + ); + return; + } + } +} + +function parseJson(text: string): any { + try { + return JSON.parse(text); + } catch { + return null; + } +} + +/** Human summary of a failure for status lines. */ +export function describeSshFailure(failure: SshFailure): string { + switch (failure.kind) { + case "hostkey-unknown": + return "Host key not yet trusted."; + case "hostkey-mismatch": + return "HOST KEY CHANGED; refusing to connect."; + case "auth": + return failure.message || "Authentication failed."; + default: + return failure.message || failure.kind; + } +} diff --git a/app/native/ssh.ios.ts b/app/native/ssh.ios.ts new file mode 100644 index 00000000..87cf7f3f --- /dev/null +++ b/app/native/ssh.ios.ts @@ -0,0 +1,69 @@ +import { + dispatchSshEvent, + encodeSshSpec, + type SshChannel, + type SshChannelListener, + type SshChannelSpec, + type SshClientKey, + type SshEndpoint, +} from "./ssh-common"; + +export * from "./ssh-common"; + +declare const FaceclawSshChannel: any; +declare const FaceclawSshKeys: any; + +// Like socket.ios.ts: Go delivers events on its own threads, so the native +// side queues them and this isolate drains the queue on a timer. +const POLL_INTERVAL_MS = 25; + +/** iOS SSH transport: FaceclawSshChannel (ObjC) over the gomobile-bound Go package in native/ssh. */ +export function openSshChannel(endpoint: SshEndpoint, channel: SshChannelSpec, listener: SshChannelListener): SshChannel { + const native = FaceclawSshChannel.alloc().initWithSpecUseKey(encodeSshSpec(endpoint, channel), endpoint.useKey); + let ended = false; + const stop = () => { + if (ended) return; + ended = true; + clearInterval(timer); + native.close(); + }; + const timer = setInterval(() => { + const events = JSON.parse(String(native.takeEvents())) as Array<{ kind: string; payload: string }>; + for (const event of events) { + if (ended) break; + if (event.kind === "closed" || event.kind === "failure") { + // Terminal events: stop polling, then deliver. + ended = true; + clearInterval(timer); + } + dispatchSshEvent(listener, event.kind, event.payload); + } + }, POLL_INTERVAL_MS); + return { + send: (text) => !ended && native.send(text) === true, + close: stop, + }; +} + +export function sshClientKey(comment: string): SshClientKey { + const authorizedKey = FaceclawSshKeys.authorizedKeyWithComment(comment); + const fingerprint = FaceclawSshKeys.fingerprint(); + if (!authorizedKey || !fingerprint) throw new Error("Could not create the SSH client key."); + return { authorizedKey: String(authorizedKey), fingerprint: String(fingerprint) }; +} + +export function getSshSecret(id: string): string { + return String(FaceclawSshKeys.secretForId(id) ?? ""); +} + +export function setSshSecret(id: string, value: string): void { + FaceclawSshKeys.setSecretForId(value, id); +} + +export function removeSshSecret(id: string): void { + FaceclawSshKeys.removeSecretForId(id); +} + +export function copyToPhoneClipboard(_label: string, text: string): void { + FaceclawSshKeys.copyToClipboard(text); +} diff --git a/app/native/ssh.ts b/app/native/ssh.ts new file mode 100644 index 00000000..dd7a2978 --- /dev/null +++ b/app/native/ssh.ts @@ -0,0 +1,66 @@ +import { Utils } from "@nativescript/core"; +import { + dispatchSshEvent, + encodeSshSpec, + type SshChannel, + type SshChannelListener, + type SshChannelSpec, + type SshClientKey, + type SshEndpoint, +} from "./ssh-common"; + +export * from "./ssh-common"; + +declare const com: any; + +/** + * Android SSH transport: FaceclawSshChannel (Kotlin) over the gomobile-bound + * Go package in native/ssh. Callable from the main isolate or a worker; + * events arrive on the calling isolate's thread. + */ +export function openSshChannel(endpoint: SshEndpoint, channel: SshChannelSpec, listener: SshChannelListener): SshChannel { + let closed = false; + const proxy = new com.faceclaw.app.FaceclawSshListener({ + onEvent: (kind: string, payload: string) => { + if (closed) return; + dispatchSshEvent(listener, String(kind), payload == null ? "" : String(payload)); + }, + }); + const native = new com.faceclaw.app.FaceclawSshChannel(encodeSshSpec(endpoint, channel), endpoint.useKey, proxy); + return { + send: (text) => !closed && native.send(text) === true, + close: () => { + if (closed) return; + closed = true; + native.close(); + }, + }; +} + +function context(): any { + return Utils.android.getApplicationContext(); +} + +/** The device's SSH client key (generated on first use). */ +export function sshClientKey(comment: string): SshClientKey { + return { + authorizedKey: String(com.faceclaw.app.FaceclawSshKeys.authorizedKey(comment)), + fingerprint: String(com.faceclaw.app.FaceclawSshKeys.fingerprint()), + }; +} + +export function getSshSecret(id: string): string { + return String(com.faceclaw.app.FaceclawSshKeys.getSecret(context(), id) ?? ""); +} + +export function setSshSecret(id: string, value: string): void { + com.faceclaw.app.FaceclawSshKeys.setSecret(context(), id, value); +} + +export function removeSshSecret(id: string): void { + com.faceclaw.app.FaceclawSshKeys.removeSecret(context(), id); +} + +export function copyToPhoneClipboard(label: string, text: string): void { + com.faceclaw.app.FaceclawSshKeys.copyToClipboard(context(), label, text); +} diff --git a/app/native/terminal-session-types.ts b/app/native/terminal-session-types.ts new file mode 100644 index 00000000..637d9dd3 --- /dev/null +++ b/app/native/terminal-session-types.ts @@ -0,0 +1,59 @@ +/** + * Session-list and connection-state types shared by the Terminal app's + * backends (g2mirror-client.ts, apps/terminal/tmux-client.ts). Kept free of + * platform imports so the tmux client can be unit-tested under node. + */ + +export type G2MirrorSession = { + socket: string; + pid: number; + cwdHint: string; + /** Unix epoch ms of the terminal's last bell, or null if none observed. */ + lastBellAt: number | null; + /** + * Unix epoch ms (host clock) of the app's last output, or null if none + * observed. Fresh to within the wrapper's 2s activity reporting interval + * while we're connected; compare against other host timestamps, not the + * phone clock (see the worker's local-receive-time activity tracking). + */ + lastOutputAt: number | null; + /** Window title the app last set (xterm OSC 0/2), or null if none observed. */ + title: string | null; +}; + +export type G2MirrorPhase = + | "idle" + | "connecting" + | "connected" // handshake accepted; can list/attach + | "attached" // relaying to one session + | "failed"; + +export type G2MirrorState = { + phase: G2MirrorPhase; + status: string; + sessions: G2MirrorSession[]; + attachedCommand: string; + /** + * server_name from the init success reply: a human-readable name for the + * machine the server runs on. "" until the handshake succeeds (or when an + * older server omits the field). + */ + serverName: string; + /** + * False when reconnecting can't help until the user changes something on + * the host (e.g. tmux missing or too old). Absent means retryable. + */ + retryable?: boolean; +}; + +/** A page of archived scrollback lines (see PROTOCOL.md "Scrollback history"). */ +export type G2MirrorHistoryReply = { + /** Absolute index of lines[0]; the page covers [start, start + lines.length). */ + start: number; + /** Oldest index still retained by the wrapper (older requests return nothing). */ + oldest: number; + /** Current splice point (next line to be archived). */ + next: number; + /** Plain text, ANSI-stripped, oldest-to-newest. */ + lines: string[]; +}; diff --git a/app/native/voice-control.ios.ts b/app/native/voice-control.ios.ts index 9d8f3864..aa0740a2 100644 --- a/app/native/voice-control.ios.ts +++ b/app/native/voice-control.ios.ts @@ -3,7 +3,7 @@ import type { CloudSttTranscriptEvent } from './cloud-stt' import type { ReconnectingSttClient } from './reconnecting-stt' import { createCloudSttClient, usesCloudStt, type VoiceProviderSelection } from './cloud-stt-provider' import { createSpeechAnalyzerClient, speechAnalyzerSupported } from './speech-analyzer-stt.ios' -import { WavRecorder } from './wav-recorder.ios' +import { MicRecorder } from './mic-recorder.ios' import { pcmLevel } from '../util/audio-level' declare const FaceclawSpeech: any @@ -57,11 +57,11 @@ export class IosVoiceControlBridge { private continuous = false // Shown instead of the native "ended" status once a drain the bridge started completes. private drainStatus: string | null = null - // Continuous captures' audio goes to Downloads/<recordingName>.wav while it is + // Continuous captures' audio goes to Downloads/<recordingName>.mka while it is // set. The file outlives a capture, so a restarted one keeps appending to it, // and it is created by the first audio, so a failed start leaves no file. private recordingName = '' - private recorder: WavRecorder | null = null + private recorder: MicRecorder | null = null // The file could not be created: one attempt per recording, not one per packet. private recordingFailure = false private readonly recordingListeners = new Set<() => void>() @@ -209,7 +209,7 @@ export class IosVoiceControlBridge { /** Where a listening continuous capture gets its audio; null when none is listening. */ get continuousSource(): 'glasses' | 'phone' | null { return this.continuous && this.capturing ? this.source : null } /** - * Record continuous captures' audio to Downloads/<name>.wav, across restarts, + * Record continuous captures' audio to Downloads/<name>.mka, across restarts, * until called with another name or ''. Only cloud captures supply PCM. */ recordTo(name: string): void { @@ -232,7 +232,10 @@ export class IosVoiceControlBridge { private record(pcm: Uint8Array): void { if (!this.recording) return if (!this.recorder) { - this.recorder = WavRecorder.open(this.recordingName) + this.recorder = MicRecorder.open(this.recordingName, { + recording: this.recordingName, startedAt: new Date().toISOString(), platform: 'ios', + source: this.source === 'phone' ? 'phone' : 'glasses', mics: 1, + }) this.log(this.recorder ? `Voice: recording audio to ${this.recorder.path}` : 'Voice: could not create the audio recording') if (!this.recorder) { this.setRecording(this.recordingName, true); return } } diff --git a/app/native/voice-control.ts b/app/native/voice-control.ts index eaae562d..96243f7d 100644 --- a/app/native/voice-control.ts +++ b/app/native/voice-control.ts @@ -1,9 +1,9 @@ import { Utils } from "@nativescript/core"; -import { type CloudSttTranscriptEvent, CloudSttClient } from "./cloud-stt"; +import { type CloudSttTranscriptEvent, CloudSttClient, toJavaBytes } from "./cloud-stt"; import { createCloudSttClient, transcriptionKey } from "./cloud-stt-provider"; +import { newRecordingPath } from "./mic-recording"; import { SpeechPauseDetector } from "./speech-pause"; -import { WavRecorder } from "./wav-recorder"; import { toUint8Array } from "../util/array-util"; import { pcmLevel } from "../util/audio-level"; @@ -84,6 +84,49 @@ export type PushToTalkOptions = { * frame). Null/absent leaves the mic omnidirectional. */ beamFilter?: { centerDeg: number; halfWidthDeg: number } | null; + /** + * Capture from the temples' own microphones (the custom firmware's + * mic_control stream) instead of the glasses' mixed stream. Each temple + * streams its microphones on its own link and clock; the phone mixes them + * into the mono feed that is transcribed. Null/absent keeps the mixed + * stream; ignored with usePhoneMic. + */ + micArray?: MicArrayCapture | null; + /** + * Play this recording (a .mka file from Save Recording) in real time in + * place of any microphone. The capture needs no glasses, and ends by + * itself when the recording does. + */ + replay?: { path: string; startMs: number } | null; + /** What the caller knows of the capture, for the description stored in a recording of it. */ + captureInfo?: Record<string, unknown>; +}; + +export type MicArrayCapture = { + /** The mic_control CONFIGURE record both temples are armed with (see mic-protocol.ts). */ + configure: Uint8Array; +}; + +/** Progress of a capture that replays a recording; positions are ms into it. */ +export type VoiceReplayEvent = { + /** + * `start` once the file is open, `position` about once per second played, + * `seek` when a jump took effect, `end` when the recording ran out, and + * `error` when it could not be opened (the status says why). + */ + kind: "start" | "position" | "seek" | "end" | "error"; + positionMs: number; + durationMs: number; +}; + +/** What a listening view (Transcribe, Translate) takes from a capture: the microphone's, or a replay's. */ +export type VoiceEventSource = { + onStatus(listener: (state: VoiceControlState) => void): () => void; + onTranscript(listener: (event: VoiceTranscriptEvent) => void): () => void; + onSpeechPause(listener: () => void): () => void; + onMicLevel(listener: (level: number) => void): () => void; + readonly recording: boolean; + readonly recordingFailed: boolean; }; /** Who currently wants the mic running. */ @@ -130,9 +173,13 @@ export class FaceclawVoiceControlBridge { private captureEndpointing = false; // Non-null while a cloud provider owns the transcript; Java only decodes PCM. private cloudClient: CloudSttClient | null = null; - // What the running STT stream transcribes with (transcriptionKey); a holder - // asking for something else restarts the stream rather than sharing it. + // What the running STT stream captures from and transcribes with + // (streamKey); a holder asking for something else restarts the stream + // rather than sharing it. private streamProvider: string | null = null; + // The options of the running stream, for the description of a recording of it. + private streamOptions: PushToTalkOptions | null = null; + private readonly replayListeners = new Set<(event: VoiceReplayEvent) => void>(); // Raw-PCM tap (EvenHub mic apps, the Microphones app): when active, the // controller runs in the decode-only "cloud" mode and every decoded PCM // frame is broadcast to these listeners. STT capture preempts it — a live @@ -150,11 +197,14 @@ export class FaceclawVoiceControlBridge { // the bridge swallow it. private verificationActive = false; private verificationRejected = false; - // Continuous captures' audio goes to Downloads/<recordingName>.wav while it is - // set. The file outlives a capture, so a restarted one keeps appending to it, - // and it is created by the first audio, so a failed start leaves no file. + // Continuous captures' audio goes to Downloads/<recordingName>.mka while it is + // set: every source's chunks as they arrive, before any processing, written + // by the controller. The file outlives a capture, so a restarted one keeps + // adding to it, and it is created by the first audio, so a failed start + // leaves no file. private recordingName = ""; - private recorder: WavRecorder | null = null; + // The file is open in the controller. + private recordingOpen = false; // The file could not be created: one attempt per recording, not one per packet. private recordingFailure = false; private readonly recordingListeners = new Set<() => void>(); @@ -251,16 +301,55 @@ export class FaceclawVoiceControlBridge { } /** - * Record continuous captures' audio to Downloads/<name>.wav, across restarts, + * End the continuous capture as an utterance: stop it and have the + * transcript of its last audio come as a final. For a capture whose audio + * ran out by itself (a replay). + */ + finishContinuousCapture(): void { + this.releaseCapture("continuous", true); + } + + /** Progress of a capture that replays a recording (PushToTalkOptions.replay). */ + onReplay(listener: (event: VoiceReplayEvent) => void): () => void { + this.replayListeners.add(listener); + return () => this.replayListeners.delete(listener); + } + + /** Continue the running replay from `positionMs` into its recording. */ + seekReplay(positionMs: number): void { + this.controller?.seekReplay(Math.max(0, Math.round(positionMs))); + } + + /** Hold the running replay where it is, or let it go on. */ + setReplayPaused(paused: boolean): void { + this.controller?.setReplayPaused(paused); + } + + /** Whether a capture is running and still has audio coming (a replay stops being so at its end). */ + isCaptureLive(): boolean { + return this.micIsLive(); + } + + /** + * Record continuous captures' audio to Downloads/<name>.mka, across restarts, * until called with another name or "". */ recordTo(name: string): void { if (name === this.recordingName) return; - this.recorder?.close(); - this.recorder = null; + this.closeRecording(); this.setRecording(name, false); } + private closeRecording(): void { + if (!this.recordingOpen) return; + this.recordingOpen = false; + try { + this.controller?.stopRecording(); + } catch (error) { + console.warn(`Voice: closing the recording failed: ${error}`); + } + } + /** Whether continuous captures' audio is being saved (see recordTo). */ get recording(): boolean { return this.recordingName !== "" && !this.recordingFailure; @@ -286,18 +375,48 @@ export class FaceclawVoiceControlBridge { for (const listener of Array.from(this.recordingListeners)) listener(); } - /** `pcm` is the controller's Java byte[], handed to the recorder as it came. */ - private record(pcm: any): void { - if (!this.recording) return; - if (!this.recorder) { - this.recorder = WavRecorder.open(this.recordingName); - if (!this.recorder) { - this.setRecording(this.recordingName, true); - return; - } - console.log(`Voice: recording audio to ${this.recorder.path}`); + /** + * Audio arrived for a continuous capture: open the recording asked for, if + * it isn't open yet. The controller writes it from then on, as the audio + * arrives on its own thread. + */ + private ensureRecording(): void { + if (!this.recording || this.recordingOpen) return; + const path = newRecordingPath(this.recordingName); + let opened = false; + try { + opened = path !== null && Boolean(this.controller?.startRecording(path, JSON.stringify(this.describeCapture()))); + } catch (error) { + console.warn(`Voice: could not create the recording: ${error}`); } - this.recorder.append(pcm); + if (!opened) { + this.setRecording(this.recordingName, true); + return; + } + this.recordingOpen = true; + console.log(`Voice: recording audio to ${path}`); + } + + /** Only captures a continuous holder has a part in are recorded: push-to-talk on its own is not. */ + private syncRecordingEnabled(): void { + this.controller?.setRecordingEnabled(this.captureHolders.has("continuous")); + } + + /** The running capture, as the JSON stored in a recording of it. */ + private describeCapture(): Record<string, unknown> { + const options = this.streamOptions; + return { + recording: this.recordingName, + startedAt: new Date().toISOString(), + source: options?.usePhoneMic ? "phone" : "glasses", + micArray: Boolean(options?.micArray) && !options?.usePhoneMic, + transcription: { provider: options?.provider ?? "", language: options?.language ?? "" }, + processing: { + noiseSuppression: Boolean(options?.noiseSuppression), + beamFilter: options?.beamFilter ?? null, + }, + ...options?.captureInfo, + }; } /** Subscribe to decoded raw mic PCM (16 kHz mono S16LE). */ @@ -364,6 +483,8 @@ export class FaceclawVoiceControlBridge { // preview-mode capture may have left on the shared controller. this.controller?.setUsePhoneMic(false); this.controller?.setEndpointing(false); + this.controller?.setMicArray(null); + this.controller?.setReplay(null, 0); // Raw means raw: the Microphones session (and EvenHub apps) get the // unprocessed stream and run their own beam/suppression processing. this.controller?.setNoiseSuppression(false); @@ -399,18 +520,33 @@ export class FaceclawVoiceControlBridge { const micLive = this.micIsLive(); this.suspendedHolders.delete(holder); this.captureHolders.add(holder); - if (micLive && this.streamProvider === transcriptionKey(options)) { + if (micLive && this.streamProvider === streamKey(options)) { // Mic already running; the new holder just shares the existing stream // (transcripts are already broadcast to its listeners). + this.syncRecordingEnabled(); return; } - // Any leftover capture is dead, or transcribes with another provider than + // Any leftover capture is dead, or captures or transcribes otherwise than // this holder needs; drop it before starting the new one. if (this.started) this.teardownCapture(); - this.streamProvider = transcriptionKey(options); + this.streamProvider = streamKey(options); + this.streamOptions = options; this.ensureController(); + this.syncRecordingEnabled(); + // A recording under way carries on into this capture: note what changed. + if (this.recordingOpen) { + try { + this.controller?.noteRecordingEvent(JSON.stringify({ event: "capture", ...this.describeCapture() })); + } catch (error) { + console.warn(`Voice: recording event failed: ${error}`); + } + } this.controller?.setCommunicator(options.communicator); this.controller?.setUsePhoneMic(Boolean(options.usePhoneMic)); + this.controller?.setMicArray( + options.micArray && !options.usePhoneMic && !options.replay ? toJavaBytes(options.micArray.configure) : null, + ); + this.controller?.setReplay(options.replay?.path ?? null, Math.max(0, Math.round(options.replay?.startMs ?? 0))); this.controller?.setEndpointing(Boolean(options.endpointing)); this.captureEndpointing = Boolean(options.endpointing); this.controller?.setNoiseSuppression(Boolean(options.noiseSuppression)); @@ -435,7 +571,7 @@ 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..."); + this.setStatus(options.replay ? "Opening the recording..." : "Starting microphone..."); // A previous push-to-talk commit may still be awaiting its final result. this.cloudClient?.stop(); const cloudClient = this.createCloudClient(options); @@ -480,6 +616,7 @@ export class FaceclawVoiceControlBridge { if (commit) this.cloudClient?.finish(); return; } + this.syncRecordingEnabled(); if (this.captureHolders.size > 0) { // Another holder still wants the mic; keep it running. return; @@ -504,6 +641,7 @@ export class FaceclawVoiceControlBridge { this.captureHolders.clear(); this.suspendedHolders.clear(); this.suspendedRaw = false; + this.syncRecordingEnabled(); this.teardownCapture(); this.setStatus("Voice control stopped."); } @@ -521,6 +659,7 @@ export class FaceclawVoiceControlBridge { this.suspendedHolders.add(holder); } this.captureHolders.clear(); + this.syncRecordingEnabled(); this.suspendedRaw = this.rawActive; this.teardownCapture(); if (this.suspendedHolders.size > 0) { @@ -578,6 +717,7 @@ export class FaceclawVoiceControlBridge { private teardownCapture(): void { this.rawActive = false; this.streamProvider = null; + this.streamOptions = null; if (global.isAndroid) { this.controller?.stop(); } @@ -611,7 +751,7 @@ export class FaceclawVoiceControlBridge { const bytes = toUint8Array(pcm); this.cloudClient?.acceptPcm(bytes); // Push-to-talk on a capture of its own is not recorded. - if (this.captureHolders.has("continuous")) this.record(pcm); + if (this.captureHolders.has("continuous")) this.ensureRecording(); if (!this.cloudClient && this.captureHolders.has("continuous") && this.speechPause.accept(bytes)) { for (const listener of this.speechPauseListeners) listener(); } @@ -642,6 +782,10 @@ export class FaceclawVoiceControlBridge { ); } }, + onReplay: (kind: string, positionMs: number, durationMs: number) => { + const event = { kind: String(kind) as VoiceReplayEvent["kind"], positionMs: Number(positionMs), durationMs: Number(durationMs) }; + for (const listener of Array.from(this.replayListeners)) listener(event); + }, }); this.controller.setListener(this.listenerProxy); } @@ -678,6 +822,21 @@ export class FaceclawVoiceControlBridge { } } +/** + * What a capture reads and transcribes with. A running capture is shared only + * by holders asking for the same: the provider and language, the microphones + * (the mixed stream, or the temples' own under a given configuration), or the + * recording it replays. + */ +function streamKey(options: PushToTalkOptions): string { + const source = options.replay + ? `replay:${options.replay.path}` + : options.micArray && !options.usePhoneMic + ? `array:${Array.from(options.micArray.configure).join(",")}` + : "mixed"; + return `${transcriptionKey(options)}|${source}`; +} + function toJavaFloats(values: Float32Array): any { const javaFloats = Array.create("float", values.length); for (let i = 0; i < values.length; i++) { @@ -687,3 +846,136 @@ function toJavaFloats(values: Float32Array): any { } export const voiceControlBridge = new FaceclawVoiceControlBridge(); + +export type VoiceReplayState = "opening" | "playing" | "paused" | "ended" | "failed"; + +/** + * A recording played back as a capture of its own: transcribed by the same + * pipeline as the microphone, with the same settings, on a bridge and native + * controller separate from the microphone's. Nothing here touches the mic, + * so voice input and a live Transcribe window carry on beside a replay. + */ +export class VoiceReplay { + private readonly bridge = new FaceclawVoiceControlBridge(); + private readonly listeners = new Set<(jumpedToMs?: number) => void>(); + private readonly offReplay: () => void; + private position = 0; + private duration = 0; + private current: VoiceReplayState = "opening"; + private closed = false; + + /** + * `options` gives the capture options (provider, keys, processing) each + * time the replay starts, so a restart picks up changed settings. + */ + constructor(readonly path: string, private readonly options: () => PushToTalkOptions) { + this.offReplay = this.bridge.onReplay((event) => this.handle(event)); + } + + /** The replay's status, transcript and level events. */ + get events(): VoiceEventSource { + return this.bridge; + } + + /** Milliseconds into the recording. */ + get positionMs(): number { + return this.position; + } + + /** The recording's length in ms; 0 until it has been opened. */ + get durationMs(): number { + return this.duration; + } + + get state(): VoiceReplayState { + return this.current; + } + + /** + * The position, duration or state changed. `jumpedToMs` is given when + * playback moved there other than by playing on: a jump, or a start + * part-way in. It comes after the transcript of what played before. + */ + onChange(listener: (jumpedToMs?: number) => void): () => void { + this.listeners.add(listener); + return () => this.listeners.delete(listener); + } + + /** Play from `positionMs` into the recording. */ + start(positionMs = 0): void { + if (this.closed) return; + this.position = Math.max(0, positionMs); + this.set("opening"); + this.bridge.startContinuousCapture({ + ...this.options(), + communicator: null, + usePhoneMic: false, + micArray: null, + replay: { path: this.path, startMs: this.position }, + }); + } + + /** Continue from `positionMs`: a jump while playing, a fresh start once the replay has ended. */ + jumpTo(positionMs: number): void { + if (this.closed) return; + const target = Math.max(0, this.duration > 0 ? Math.min(positionMs, this.duration) : positionMs); + if ((this.current === "playing" || this.current === "paused") && this.bridge.isCaptureLive()) { + this.bridge.seekReplay(target); + this.position = target; + this.notify(); + return; + } + this.start(target); + } + + setPaused(paused: boolean): void { + if (this.closed || (this.current !== "playing" && this.current !== "paused")) return; + this.bridge.setReplayPaused(paused); + this.set(paused ? "paused" : "playing"); + } + + /** Stop for good and release the capture. */ + close(): void { + if (this.closed) return; + this.closed = true; + this.offReplay(); + this.listeners.clear(); + this.bridge.stop(); + } + + private handle(event: VoiceReplayEvent): void { + this.duration = event.durationMs; + switch (event.kind) { + case "start": + this.position = event.positionMs; + this.current = "playing"; + this.notify(event.positionMs > 0 ? event.positionMs : undefined); + return; + case "seek": + this.position = event.positionMs; + this.notify(event.positionMs); + return; + case "end": + this.position = event.durationMs; + // Its last audio is still to be transcribed (a cloud provider's final). + this.bridge.finishContinuousCapture(); + this.set("ended"); + return; + case "error": + this.set("failed"); + return; + default: + this.position = event.positionMs; + this.notify(); + } + } + + private set(state: VoiceReplayState): void { + this.current = state; + this.notify(); + } + + private notify(jumpedToMs?: number): void { + for (const listener of Array.from(this.listeners)) listener(jumpedToMs); + } +} diff --git a/app/native/wav-header.ts b/app/native/wav-header.ts deleted file mode 100644 index a85ba2e0..00000000 --- a/app/native/wav-header.ts +++ /dev/null @@ -1,22 +0,0 @@ -/** The 44-byte header of a 16 kHz mono PCM16 WAV file holding `dataBytes` of samples. */ -export function wavHeader(dataBytes: number): Uint8Array { - const header = new Uint8Array(44); - const view = new DataView(header.buffer); - const ascii = (offset: number, text: string) => { - for (let i = 0; i < text.length; i++) header[offset + i] = text.charCodeAt(i); - }; - ascii(0, "RIFF"); - view.setUint32(4, 36 + dataBytes, true); - ascii(8, "WAVE"); - ascii(12, "fmt "); - view.setUint32(16, 16, true); - view.setUint16(20, 1, true); // PCM - view.setUint16(22, 1, true); // mono - view.setUint32(24, 16000, true); - view.setUint32(28, 32000, true); // byte rate - view.setUint16(32, 2, true); // block align - view.setUint16(34, 16, true); // bits per sample - ascii(36, "data"); - view.setUint32(40, dataBytes, true); - return header; -} diff --git a/app/native/wav-recorder.ios.ts b/app/native/wav-recorder.ios.ts deleted file mode 100644 index ff5e815a..00000000 --- a/app/native/wav-recorder.ios.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { knownFolders, path as paths } from '@nativescript/core' -import { wavHeader } from './wav-header' - -// About a second of 16 kHz mono PCM16: written in batches, not per 50 ms packet. -const FLUSH_BYTES = 32000 - -/** - * Appends 16 kHz mono PCM16 to a WAV file in Documents/Downloads while it is - * captured. The header's sizes are rewritten on every flush, so a crash leaves a - * playable file of everything up to the last second. - */ -export class WavRecorder { - private pending: Uint8Array[] = [] - private pendingBytes = 0 - private dataBytes = 0 - - private constructor(private handle: NSFileHandle | null, readonly path: string) {} - - /** A recorder for Downloads/<name>.wav (a -2, -3... suffix if taken), or null. */ - static open(name: string): WavRecorder | null { - const fm = NSFileManager.defaultManager - const dir = paths.join(knownFolders.documents().path, 'Downloads') - try { - fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(dir, true, null) - let path = paths.join(dir, `${name}.wav`) - for (let n = 2; fm.fileExistsAtPath(path); n++) path = paths.join(dir, `${name}-${n}.wav`) - if (!fm.createFileAtPathContentsAttributes(path, toData(wavHeader(0)), null)) return null - const handle = NSFileHandle.fileHandleForWritingAtPath(path) - if (!handle || !handle.seekToEndReturningOffsetError(null)) return null - return new WavRecorder(handle, path) - } catch { - return null - } - } - - append(pcm: Uint8Array): void { - if (!this.handle || !pcm.length) return - this.pending.push(pcm.slice()) - this.pendingBytes += pcm.length - if (this.pendingBytes >= FLUSH_BYTES) this.flush() - } - - close(): void { - this.flush() - try { this.handle?.closeAndReturnError() } catch { /* already closed */ } - this.handle = null - } - - private flush(): void { - const handle = this.handle - if (!handle || !this.pendingBytes) return - const chunk = new Uint8Array(this.pendingBytes) - let offset = 0 - for (const part of this.pending) { chunk.set(part, offset); offset += part.length } - this.pending = [] - this.pendingBytes = 0 - try { - // NativeScript throws a failed call's NSError (storage full, file removed). - handle.writeDataError(toData(chunk)) - handle.seekToOffsetError(0) - handle.writeDataError(toData(wavHeader(this.dataBytes + chunk.length))) - handle.seekToEndReturningOffsetError(null) - this.dataBytes += chunk.length - return - } catch { - // Stop recording rather than retry. - } - try { handle.closeAndReturnError() } catch { /* ignore */ } - this.handle = null - } -} - -function toData(bytes: Uint8Array): NSData { - return NSData.dataWithBytesLength(interop.handleof(bytes.buffer), bytes.byteLength) -} diff --git a/app/native/wav-recorder.ts b/app/native/wav-recorder.ts deleted file mode 100644 index cec96f3f..00000000 --- a/app/native/wav-recorder.ts +++ /dev/null @@ -1,42 +0,0 @@ -declare const android: any; -declare const java: any; -declare const com: any; - -const SAMPLE_RATE = 16000; - -/** - * Appends 16 kHz mono PCM16 to a WAV file in the public Downloads directory - * while it is captured (Android; wav-recorder.ios.ts is the iOS one). The - * native recorder keeps the header's sizes current, so a crash leaves a - * playable file of everything up to the last second. - */ -export class WavRecorder { - private constructor(private recorder: any, readonly path: string) {} - - /** A recorder for Downloads/<name>.wav (a -2, -3... suffix if taken), or null. */ - static open(name: string): WavRecorder | null { - try { - const downloads = android.os.Environment.getExternalStoragePublicDirectory( - android.os.Environment.DIRECTORY_DOWNLOADS, - ); - downloads.mkdirs(); - let file = new java.io.File(downloads, `${name}.wav`); - for (let n = 2; file.exists(); n++) file = new java.io.File(downloads, `${name}-${n}.wav`); - const path = String(file.getAbsolutePath()); - return new WavRecorder(new com.faceclaw.app.FaceclawWavRecorder(path, SAMPLE_RATE, 1), path); - } catch (error) { - console.warn(`WavRecorder: could not create ${name}.wav in Downloads: ${error}`); - return null; - } - } - - /** `pcm` is the Java byte[] the voice controller delivered, passed back as it came. */ - append(pcm: any): void { - this.recorder?.append(pcm); - } - - close(): void { - this.recorder?.finish(); - this.recorder = null; - } -} diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index d47a981b..5e69dd07 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -947,7 +947,7 @@ export const useMicControlSetting = new ConfigSettingBoolean({ storageKey: "developer.useMicControl", defaultValue: true, description: - "Use the custom firmware's per-temple mic-control channel for the Microphones app's array capture. When off, use the standard single mixed stream.", + "Use the custom firmware's per-temple mic-control channel for capturing the microphones separately (the Microphones app's array capture, and Transcribe's Microphones option). When off, use the standard single mixed stream.", }); export const showBleBandwidthSetting = new ConfigSettingBoolean({ @@ -1054,7 +1054,9 @@ export const saveRecordingSetting = new ConfigSettingBoolean({ label: "Save Recording", storageKey: "voice.saveRecording", defaultValue: false, - description: "Record the audio of Transcribe and Translate sessions as WAV files in Downloads (about 2 MB per minute), named after the app and the session's start time. Recordings are kept until you delete them.", + description: global.isIOS + ? "Record the audio of Transcribe and Translate sessions as Matroska audio (.mka) files in Downloads (about 2 MB per minute), named after the app and the session's start time. Recordings are kept until you delete them." + : "Record the audio of Transcribe and Translate sessions as Matroska audio (.mka) files in Downloads: about 2 MB per minute, or 10 MB with all four microphones. Named after the app and the session's start time, and kept until you delete them. Transcribe can replay one: pick it in the Files app.", }); export const assistantSkipConfirmationSetting = new ConfigSettingBoolean({ @@ -1338,11 +1340,28 @@ export const terminalNewConnectionSetting = new ConfigSettingString({ label: "New connection", storageKey: "terminal.newConnectionDraft", defaultValue: "", - editorTitle: "g2mirror connection string (g2mirror://token@host)", + editorTitle: "Connection (ssh://user@host or g2mirror://token@host)", glassesEditTitle: "Add connection", normalize: (value) => (value ?? "").replace(/[\x00-\x1f]+/g, "").trim(), }); +/** + * Staging buffer for the Terminal app's SSH password prompt: the worker opens + * the phone's (masked) text editor on it, then moves what was typed into the + * platform keystore (app/native/ssh) and clears it as soon as the user + * confirms or cancels on the glasses. Not listed in the Settings app. + */ +export const terminalSshPasswordSetting = new ConfigSettingString({ + id: "terminal-ssh-password", + label: "SSH password", + storageKey: "terminal.sshPasswordDraft", + defaultValue: "", + inputKind: "password", + editorTitle: "SSH password", + glassesEditTitle: "SSH password", + normalize: (value) => (value ?? "").replace(/[\r\n]+/g, ""), +}); + /** * Staging buffer for the T3 Code app's Pair-environment screen: the phone * text editor types the `t3 pair` link into it, and the app reads it back @@ -1722,9 +1741,18 @@ type SettingsMenuOptions<T> = { label?: string } +type EnumSettingMenuOptions<T> = SettingsMenuOptions<T> & { + /** + * A live line under the picker's values, such as a sensor reading to pick + * by. open() runs as the picker opens, with a function that repaints it, + * and returns what to run when the picker closes. + */ + pickerFooter?: { text: () => string; open: (repaint: () => void) => () => void } +} + export function enumSettingMenuItem<TValue extends string, TId extends string = string>( setting: ConfigSettingEnum<TValue, TId>, - opts?: SettingsMenuOptions<TValue> + opts?: EnumSettingMenuOptions<TValue> ): MenuItem { const label = opts?.label ?? setting.label; return { @@ -1752,7 +1780,11 @@ export function enumSettingMenuItem<TValue extends string, TId extends string = ); }, })); - openModalMenu(ctx, label, items, Math.max(0, setting.values.indexOf(current))); + const footer = opts?.pickerFooter; + openModalMenu(ctx, label, items, Math.max(0, setting.values.indexOf(current)), { + footer: footer?.text, + onClosed: footer?.open(() => ctx.actions.requestRender()), + }); }, render: ({ image, x, y, width }) => { drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, label, setting.displayValue(setting.get())); diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index 8785caad..567ef335 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -103,6 +103,8 @@ import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from ". import { uiFontPickerMenuItem } from "../font-picker"; import { NotificationFilterLayer } from "../notification-filter"; import { compatibleModelDiscoveryMenuItem } from "../openai-compatible-model-picker"; +import { keyboardSettingsMenuItem } from "../keyboard-settings"; +import { keybindingsMenuItem } from "../keybinding-settings"; import { isVeniceApiBaseUrl } from "../../native/openai-chat"; /** The Settings section the Notifications app's "Settings" menu item opens. */ @@ -242,6 +244,14 @@ function settingsSections(): SettingsSection[] { ], renderDetail: renderWatchStatus, }, + // A Bluetooth keyboard paired with the glasses themselves (the G2's + // custom firmware hosts it; app/g2/keyboard/). Hidden rather than left + // out: the Settings window can be open across a change of glasses. + { + label: "Keyboard", + items: [keyboardSettingsMenuItem(), keybindingsMenuItem()], + hidden: () => glassesTypeSetting.get() !== "g2", + }, { label: "Developer", items: [ diff --git a/app/ui/dashboard/settings-panel.ts b/app/ui/dashboard/settings-panel.ts index cb113433..d7b9b3b0 100644 --- a/app/ui/dashboard/settings-panel.ts +++ b/app/ui/dashboard/settings-panel.ts @@ -28,6 +28,13 @@ export type SettingsSection = { height: number; ctx: LayerContext; }) => number | void; + /** + * Leave the section out of the list while this returns true. Re-read on + * every paint and input, for a section that only applies with some other + * setting (the paired glasses' type): the Settings window outlives changes + * to those. + */ + hidden?: () => boolean; }; const LEFT_W = 150; @@ -66,7 +73,7 @@ export class SettingsPanelLayer implements Layer { constructor(private readonly sections: SettingsSection[]) { this.leftMenu = new Menu<SettingsSection>({ - items: sections, + items: sections.filter((section) => !section.hidden?.()), wrap: true, rowGap: ROW_GAP, highlight: { radius: 6 }, @@ -108,9 +115,28 @@ export class SettingsPanelLayer implements Layer { return this.leftMenu.selectedItem ?? this.sections[0]!; } + /** + * Bring the left column in line with which sections are hidden now. The + * highlight stays on its section; if that one just went away it moves to + * the list's start, with focus back in the left column. + */ + private syncSections(): void { + const visible = this.sections.filter((section) => !section.hidden?.()); + const shown = this.leftMenu.items; + if (visible.length === shown.length && visible.every((section, index) => section === shown[index])) return; + const selected = this.leftMenu.selectedItem; + const index = selected ? visible.indexOf(selected) : -1; + this.leftMenu.setItems(visible, Math.max(0, index)); + if (index < 0) { + this.focus = "left"; + this.resetRight(); + } + } + /** Select a left-column section by label (deep link, e.g. from an app's menu). */ focusSection(label: string): void { - const index = this.sections.findIndex((section) => section.label === label); + this.syncSections(); + const index = this.leftMenu.items.findIndex((section) => section.label === label); if (index < 0) return; this.leftMenu.select(index); this.focus = "left"; @@ -123,6 +149,7 @@ export class SettingsPanelLayer implements Layer { const { width, height, padding } = viewport; const image = new GrayImage(width, height, 0); const appFocused = ctx.stack.isFocused(); + this.syncSections(); const section = this.section(); const rightItems = section.items; this.rightMenu.setItems(rightItems); @@ -228,6 +255,7 @@ export class SettingsPanelLayer implements Layer { } async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> { + this.syncSections(); const scroll = scrollEventFor(event); if (this.focus === "left") { if (scroll) { diff --git a/app/ui/keybinding-settings.ts b/app/ui/keybinding-settings.ts new file mode 100644 index 00000000..37717f17 --- /dev/null +++ b/app/ui/keybinding-settings.ts @@ -0,0 +1,147 @@ +import { getDefaultSmallFont } from "../graphics/ui-fonts"; +import { + DEFAULT_KEYS_HELP, + KEY_ACTIONS, + chordId, + describeChord, + keyActionLabel, + type KeyChord, +} from "../g2/keyboard/bindings"; +import { keyboardService } from "../g2/keyboard/service"; +import { TextViewerLayer } from "../apps/files/text-viewer"; +import type { LayerContext } from "./layers"; +import { drawRightValueMenuItem, MenuLayer, openModalMenu, submenuItem, TextPageLayer, type MenuItem } from "./menu"; + +/** + * Settings > Keyboard > Keybindings: the custom keybindings (a key, with + * any modifiers held, to a gesture), which come before the built-in + * meanings in app/g2/keyboard/bindings.ts. Adding one asks for the key on + * the keyboard itself, so it works for a remote whose buttons send who + * knows what. + */ + +/** Waits for the key to bind. Double tap backs out, as from any page. */ +class KeyCaptureLayer extends TextPageLayer { + private cancel: (() => void) | null = null; + + constructor(private readonly onChord: (chord: KeyChord, ctx: LayerContext) => void) { + super( + "Add keybinding", + "Press the key to bind, on the keyboard: a key, a mouse button, or a turn of the wheel. Hold Shift, Ctrl or Alt with it to bind the combination.", + ); + } + + open(ctx: LayerContext): void { + ctx.stack.push(this); + this.cancel = keyboardService.captureNextKey((chord) => { + this.cancel = null; + ctx.stack.popThrough(this); + this.onChord(chord, ctx); + ctx.actions.requestRender(); + }); + } + + onRemoved(): void { + this.cancel?.(); + this.cancel = null; + } +} + +class KeybindingsLayer extends MenuLayer { + constructor(viewport: { width: number; height: number }) { + super("Keybindings", [], { + x: "center", + y: 8, + width: Math.min(320, viewport.width - 40), + minHeight: Math.min(160, viewport.height - 40), + maxHeight: Math.min(240, viewport.height - 40), + depth: 4, + }); + } + + open(ctx: LayerContext): void { + this.updateRows(); + ctx.stack.push(this); + } + + /** Rebuild the rows, with the highlight on `selectId`'s keybinding when given. */ + private updateRows(selectId?: string): void { + const bindings = keyboardService.keyBindings(); + const rows: MenuItem[] = bindings.map((binding) => { + const label = describeChord(binding.chord); + return { + label, + onSelect: (ctx: LayerContext) => this.pickAction(ctx, binding.chord), + render: ({ image, x, y, width }) => { + drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, label, keyActionLabel(binding.action)); + }, + }; + }); + rows.push( + { label: "Add keybinding", onSelect: (ctx) => this.add(ctx) }, + // Scrolls, where a page would cut the text off in a short window. + submenuItem("Default keys", (ctx) => ctx.stack.push(new TextViewerLayer(DEFAULT_KEYS_HELP, "Default keys"))), + ); + const selected = selectId === undefined ? -1 : bindings.findIndex((binding) => chordId(binding.chord) === selectId); + if (selected >= 0) this.setItems(rows, selected); + else this.setItems(rows); + } + + private add(ctx: LayerContext): void { + if (!keyboardService.bridge.isConnected()) { + ctx.stack.push(new TextPageLayer( + "Add keybinding", + "No keyboard is connected. Pair or reconnect one under Bluetooth keyboard, then come back: the key to bind is read from the keyboard.", + )); + return; + } + new KeyCaptureLayer((chord, captureCtx) => this.pickAction(captureCtx, chord)).open(ctx); + } + + /** Choose what `chord` does: for a new keybinding, or to change or remove one. */ + private pickAction(ctx: LayerContext, chord: KeyChord): void { + const id = chordId(chord); + const current = keyboardService.keyBindings().find((binding) => chordId(binding.chord) === id)?.action; + const done = (inner: LayerContext): void => { + inner.stack.pop(); + this.updateRows(id); + inner.actions.requestRender(); + }; + const rows: MenuItem[] = KEY_ACTIONS.map((entry) => ({ + label: entry.action === current ? `${entry.label} *` : entry.label, + onSelect: (inner: LayerContext) => { + if (!keyboardService.setKeyBinding(chord, entry.action)) { + inner.stack.pop(); + inner.stack.push(new TextPageLayer("Keybindings", "There is no room for another keybinding. Remove one first.")); + return; + } + done(inner); + }, + })); + if (current !== undefined) { + rows.push({ + label: "Remove keybinding", + onSelect: (inner) => { + keyboardService.removeKeyBinding(chord); + done(inner); + }, + }); + } + const selected = Math.max(0, KEY_ACTIONS.findIndex((entry) => entry.action === current)); + openModalMenu(ctx, describeChord(chord), rows, selected); + } +} + +/** The Settings row that opens the keybindings menu. */ +export function keybindingsMenuItem(): MenuItem { + return { + label: "Keybindings", + description: + "Choose what keys do: arrows, Enter, Esc and a mouse's buttons and wheel navigate by default, and any key (or a remote's button) can be bound to select, back, a swipe, a scroll or a menu.", + onSelect: (ctx) => new KeybindingsLayer(ctx.stack.getBaseSize()).open(ctx), + render: ({ image, x, y, width }) => { + const count = keyboardService.keyBindings().length; + drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, "Keybindings", count ? `${count} custom` : "Default"); + }, + }; +} diff --git a/app/ui/keyboard-settings.ts b/app/ui/keyboard-settings.ts new file mode 100644 index 00000000..8aeaf9e9 --- /dev/null +++ b/app/ui/keyboard-settings.ts @@ -0,0 +1,232 @@ +import { getDefaultSmallFont } from "../graphics/ui-fonts"; +import type { KeyboardBridgeSnapshot } from "../g2/keyboard/bridge"; +import type { KeyboardAdvert } from "../g2/keyboard/protocol"; +import { keyboardService } from "../g2/keyboard/service"; +import { ConfigSettingString, textSettingMenuItem } from "./dashboard-settings"; +import type { LayerContext } from "./layers"; +import { drawRightValueMenuItem, MenuLayer, type MenuItem } from "./menu"; + +/** + * Settings > Keyboard: pairing a Bluetooth keyboard with the glasses (the + * CFW keyboard bridge, app/g2/keyboard/). The keyboard pairs with the + * glasses, not the phone, so all of it happens here, on the glasses: scan, + * pick, and for a keyboard that wants one, the passkey to type on it. + */ + +/** The code a keyboard with a display shows, typed on the phone. Scratch, not a preference. */ +const pairingCodeSetting = new ConfigSettingString({ + id: "keyboardPairingCode", + storageKey: "keyboard.pairingCode", + label: "Code on keyboard", + defaultValue: "", + normalize: (value) => (value ?? "").replace(/\D/g, "").slice(0, 6), +}); + +const sixDigits = (value: number): string => { + const text = String(value).padStart(6, "0"); + return `${text.slice(0, 3)} ${text.slice(3)}`; +}; + +function advertLabel(advert: KeyboardAdvert): string { + const name = advert.name.trim() || advert.address; + return name.length > 30 ? `${name.slice(0, 27)}...` : name; +} + +/** One line saying where the keyboard stands, for the Settings row and the menu's first row. */ +export function keyboardStatusText(snapshot: KeyboardBridgeSnapshot): string { + const name = snapshot.activeName || snapshot.saved?.name || "keyboard"; + switch (snapshot.state) { + case "unavailable": + return snapshot.saved ? "Glasses not connected" : "Not set up"; + case "scanning": + return "Scanning..."; + case "connecting": + return `Connecting to ${name}...`; + case "pairing": + return `Pairing with ${name}...`; + case "configuring": + return `Setting up ${name}...`; + case "connected": + return `Connected: ${name}`; + case "error": + return snapshot.error || "Failed"; + default: + if (snapshot.waiting) return `Waiting for ${name}`; + return snapshot.saved ? `${name} (disconnected)` : "Not set up"; + } +} + +/** The short form for the right edge of the Settings row. */ +function keyboardStatusBrief(snapshot: KeyboardBridgeSnapshot): string { + switch (snapshot.state) { + case "connected": + return "Connected"; + case "scanning": + return "Scanning"; + case "connecting": + case "pairing": + case "configuring": + return "Connecting"; + case "error": + return "Failed"; + case "unavailable": + return snapshot.saved ? "Offline" : "Off"; + default: + return snapshot.waiting ? "Waiting" : snapshot.saved ? "Disconnected" : "Off"; + } +} + +const info = (label: string): MenuItem => ({ label, disabled: true, onSelect: () => {} }); + +class KeyboardSettingsLayer extends MenuLayer { + private unsubscribe: (() => void) | null = null; + private showOthers = false; + /** What kind of rows are showing; a change moves the highlight to the first row that does something. */ + private shape = ""; + + constructor(viewport: { width: number; height: number }) { + super("Bluetooth keyboard", [], { + x: "center", + y: 8, + width: Math.min(320, viewport.width - 40), + minHeight: Math.min(160, viewport.height - 40), + maxHeight: Math.min(240, viewport.height - 40), + depth: 4, + }); + } + + open(ctx: LayerContext): void { + ctx.stack.push(this); + this.updateRows(); + this.unsubscribe = keyboardService.subscribe(() => { + this.updateRows(); + ctx.actions.requestRender(); + }); + } + + onRemoved(): void { + this.unsubscribe?.(); + this.unsubscribe = null; + } + + /** Run a bridge command; its failure shows up in the snapshot, which repaints the rows. */ + private run(action: () => Promise<unknown>): void { + void action().catch(() => {}); + } + + private updateRows(): void { + const snapshot = keyboardService.snapshot(); + const bridge = keyboardService.bridge; + const rows: MenuItem[] = [info(keyboardStatusText(snapshot))]; + let shape: string = snapshot.state; + + const scanRow = (label: string): MenuItem => ({ + label, + disabled: snapshot.busy, + onSelect: () => { + this.showOthers = false; + this.run(() => bridge.scan()); + }, + }); + + if (snapshot.state === "unavailable") { + rows.push(info("Connect G2 glasses to pair one.")); + if (snapshot.saved) rows.push({ label: `Forget ${snapshot.saved.name || "keyboard"}`, onSelect: () => this.run(() => bridge.forget()) }); + } else if (snapshot.pairing) { + const pairing = snapshot.pairing; + shape = `pairing:${pairing.kind}`; + if (pairing.kind === "display") { + rows.push(info(`Type ${sixDigits(pairing.pin)} on the keyboard,`), info("then press Enter.")); + } else if (pairing.kind === "compare") { + rows.push( + info(`Does the keyboard show ${sixDigits(pairing.value)}?`), + { label: "Yes, pair", onSelect: () => this.run(() => bridge.answerCompare(true)) }, + { label: "No", onSelect: () => this.run(() => bridge.answerCompare(false)) }, + ); + } else { + rows.push( + info("Enter the code the keyboard shows."), + textSettingMenuItem(pairingCodeSetting), + { + label: "Pair with this code", + disabled: () => pairingCodeSetting.get().length !== 6, + onSelect: () => { + const code = pairingCodeSetting.get(); + pairingCodeSetting.set(""); + this.run(() => bridge.answerPasskey(parseInt(code, 10))); + }, + }, + ); + } + rows.push({ label: "Cancel pairing", onSelect: () => this.run(() => bridge.disconnect()) }); + } else if (snapshot.state === "connecting" || snapshot.state === "pairing" || snapshot.state === "configuring") { + rows.push({ label: "Cancel", onSelect: () => this.run(() => bridge.disconnect()) }); + } else if (snapshot.state === "scanning" || (snapshot.scanResults.length > 0 && snapshot.state !== "connected")) { + shape = "results"; + const hid = snapshot.scanResults.filter((advert) => advert.hid); + const others = snapshot.scanResults.filter((advert) => !advert.hid); + const pick = (advert: KeyboardAdvert): MenuItem => ({ + label: advertLabel(advert), + disabled: snapshot.busy, + onSelect: () => { + this.run(() => bridge.connect({ address: advert.address, addressType: advert.addressType, name: advert.name.trim() || advert.address })); + }, + }); + rows.push(...hid.map(pick)); + if (!hid.length && snapshot.state !== "scanning") rows.push(info("No keyboards found."), info("Put it in pairing mode.")); + if (others.length && this.showOthers) rows.push(...others.map(pick)); + else if (others.length) { + rows.push({ + label: `Show ${others.length} other device${others.length === 1 ? "" : "s"}`, + onSelect: (ctx) => { + this.showOthers = true; + this.updateRows(); + ctx.actions.requestRender(); + }, + }); + } + if (snapshot.state !== "scanning") rows.push(scanRow("Scan again")); + if (snapshot.saved && snapshot.state !== "scanning") { + rows.push({ label: `Reconnect ${snapshot.saved.name || "keyboard"}`, disabled: snapshot.busy, onSelect: () => this.run(() => bridge.reconnect()) }); + } + } else if (snapshot.state === "connected") { + rows.push( + { label: "Disconnect", onSelect: () => this.run(() => bridge.disconnect()) }, + { label: "Forget this keyboard", onSelect: () => this.run(() => bridge.forget()) }, + scanRow("Pair a different keyboard"), + ); + } else if (snapshot.saved) { + shape = snapshot.waiting ? "waiting" : "saved"; + rows.push({ label: "Reconnect now", disabled: snapshot.busy, onSelect: () => this.run(() => bridge.reconnect()) }); + if (snapshot.waiting) rows.push({ label: "Stop reconnecting", onSelect: () => this.run(() => bridge.disconnect()) }); + rows.push( + { label: "Forget this keyboard", onSelect: () => this.run(() => bridge.forget()) }, + scanRow("Pair a different keyboard"), + ); + } else { + shape = "empty"; + rows.push(info("Put the keyboard in pairing mode,"), info("then scan."), scanRow("Scan for keyboards")); + } + + const firstAction = rows.findIndex((row) => row.disabled !== true); + if (shape !== this.shape) { + this.shape = shape; + this.setItems(rows, Math.max(0, firstAction)); + } else { + this.setItems(rows); + } + } +} + +/** The Settings row that opens the keyboard menu. */ +export function keyboardSettingsMenuItem(): MenuItem { + return { + label: "Bluetooth keyboard", + description: + "Pair a Bluetooth keyboard with the glasses. It types into Terminal sessions, and its arrow keys, Enter and Escape work everywhere as scroll, select and back.", + onSelect: (ctx) => new KeyboardSettingsLayer(ctx.stack.getBaseSize()).open(ctx), + render: ({ image, x, y, width }) => { + drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, "Bluetooth keyboard", keyboardStatusBrief(keyboardService.snapshot())); + }, + }; +} diff --git a/app/ui/layers.ts b/app/ui/layers.ts index f41f3acf..90eb70de 100644 --- a/app/ui/layers.ts +++ b/app/ui/layers.ts @@ -3,6 +3,7 @@ import { screenHeight, screenWidth } from "../graphics/screen-size"; import { dimPlanes, type Plane } from "../graphics/plane"; import { spanCurrent } from "../native/frame-timings"; import { type ConfigSettingBoolean, type ConfigSettingString, type VoiceProvider } from "./dashboard-settings"; +import { type VoiceReplay } from "../native/voice-control"; import { directionalFallback, isDirectionalInput, type InputEvent } from "./gestures"; import { toViewport, type Viewport, type ViewportSize } from "./viewport"; @@ -13,8 +14,17 @@ export type TextSettingsEditToggle = { /** A continuous-capture client's needs (see LayerActions.startContinuousVoiceCapture). */ export type ContinuousCaptureOptions = { - /** Names a WAV of the captured audio in Downloads, written while Save Recording is on. */ + /** Names a recording (.mka) of the captured audio in Downloads, written while Save Recording is on. */ recordAs?: string; + /** + * How many of the glasses' microphones to capture: 1 is their own mixed + * stream; 2 (one per temple) and 4 (both of each temple) stream the + * microphones separately through the custom firmware, for recording and + * array processing. While any client asks for more than one, every capture + * uses the most asked for. Glasses without the microphone channel, and the + * phone's microphone, give 1 whatever is asked. + */ + mics?: MicCount; /** * Transcribe with this provider, expecting this spoken language (ISO 639-1, * or "" for the provider to detect it), instead of the Transcription @@ -26,6 +36,8 @@ export type ContinuousCaptureOptions = { export type TranscriptionChoice = { provider: VoiceProvider; language: string }; +export type MicCount = 1 | 2 | 4; + export type LayerActions = { /** Ask for a dashboard repaint+transmit, e.g. when async data arrives. */ requestRender: () => void; @@ -55,6 +67,12 @@ export type LayerActions = { */ startContinuousVoiceCapture: (client: string, options?: ContinuousCaptureOptions) => Promise<void> | void; stopContinuousVoiceCapture: (client: string) => Promise<void> | void; + /** + * Open a recording (.mka) for playing back through transcription in place + * of the microphone; the caller starts and closes it. Absent, or null, + * where recordings can't be replayed. + */ + openVoiceReplay?: (path: string) => VoiceReplay | null; /** Play a CFW tone-sequencer payload (see sound-effects.ts). */ playBuzzerSequence: (payload: Uint8Array) => Promise<void> | void; }; diff --git a/app/ui/listening-header.ts b/app/ui/listening-header.ts index 4c334d19..762fcaaa 100644 --- a/app/ui/listening-header.ts +++ b/app/ui/listening-header.ts @@ -1,6 +1,6 @@ import { GrayImage, imageFromAsciiArt, type UiFont } from "../graphics/image"; import { truncateText } from "../graphics/textwrap"; -import { voiceControlBridge } from "../native/voice-control"; +import { voiceControlBridge, type VoiceEventSource } from "../native/voice-control"; import { lineStep } from "./metrics"; import type { Edges } from "./viewport"; @@ -68,8 +68,9 @@ export class MicLevelMeter { private timer: ReturnType<typeof setInterval> | null = null; private unsubscribe: (() => void) | null = null; - start(requestRender: () => void, isVisible: () => boolean): void { - this.unsubscribe = voiceControlBridge.onMicLevel((level) => { + /** `source` is the capture whose level to show: the microphone's unless given. */ + start(requestRender: () => void, isVisible: () => boolean, source: VoiceEventSource = voiceControlBridge): void { + this.unsubscribe = source.onMicLevel((level) => { this.peak = Math.max(this.peak, level); }); this.timer = setInterval(() => { diff --git a/app/ui/menu.ts b/app/ui/menu.ts index 067a7577..217de19d 100644 --- a/app/ui/menu.ts +++ b/app/ui/menu.ts @@ -39,6 +39,8 @@ const PAGE_TITLE_OFFSET_Y = 2; const PAGE_TITLE_GAP = 4; /** Room a page menu keeps right of its rows for the scrollbar. */ const PAGE_SCROLLBAR_GUTTER = 6; +/** A heading row's text: dimmer than a row's, brighter than a disabled row's. */ +const MENU_HEADING_VALUE = 90; export type MenuLayout = { /** Stereo depth of the menu surface, including the selected row. */ @@ -57,7 +59,7 @@ export type MenuLayout = { * Dim hint line pinned to the bottom edge inside the box, below the items * (gesture help such as "●— system menu"). Takes a line off the item area. */ - footer?: string; + footer?: MenuFooter; /** Brightness factor for everything beneath the menu (see Layer.dimUnderneath). Default: none. */ dimUnderneath?: number; /** @@ -83,9 +85,16 @@ export type MenuPageLayout = { /** Width of the rows. Default (and at most): the window's, inside its side padding. */ width?: number; /** Dim hint line pinned above the window's bottom padding, below the rows. */ - footer?: string; + footer?: MenuFooter; }; +/** A menu's footer line: fixed text, or read on every paint (a live value). */ +export type MenuFooter = string | (() => string); + +function footerText(footer: MenuFooter): string { + return typeof footer === "function" ? footer() : footer; +} + export type MenuItemRenderArgs = { image: GrayImage; x: number; @@ -104,6 +113,12 @@ export type MenuItem = { description?: string; /** Disabled rows stay visible but render dimly and ignore clicks. */ disabled?: boolean | (() => boolean); + /** + * A label for the group of rows after it (see menuHeadingItem). MenuLayer's + * highlight skips it; a list that doesn't know headings shows it as a + * disabled row. + */ + heading?: boolean; onSelect: (ctx: LayerContext, menu: MenuLayer) => Promise<void> | void; render?: (args: MenuItemRenderArgs) => void; }; @@ -207,6 +222,22 @@ export function submenuItem( }; } +/** + * A heading for the rows that follow it, splitting a long menu into sections: + * dim capitals in the rows' text column, never selected. + */ +export function menuHeadingItem(label: string): MenuItem { + return { + label, + heading: true, + disabled: true, + onSelect: () => {}, + render: ({ image, x, y, text }) => { + image.drawText(getDefaultSmallFont(), x, y + LIST_ROW_TEXT_INSET, text.toUpperCase(), MENU_HEADING_VALUE); + }, + }; +} + export function drawToggleMenuItem( image: GrayImage, font: UiFont, @@ -274,6 +305,7 @@ export class MenuLayer implements Layer { wrap: true, rowGap: 1, getHeight: () => listRowHeight(getDefaultSmallFont()), + isSelectable: (item) => !item.heading, draw: ({ image, item, x, y, width, height, selected }) => { const font = getDefaultSmallFont(); const disabled = isMenuItemDisabled(item); @@ -360,7 +392,7 @@ export class MenuLayer implements Layer { image.drawText(font, x + 12, y + 8, this.title, 220); } if (layout.footer) { - image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, layout.footer, 110); + image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, footerText(layout.footer), 110); } const bodyY = y + chromeTop; @@ -397,7 +429,7 @@ export class MenuLayer implements Layer { let fixedBottom = height - padding.bottom; if (layout.footer) { const footerY = height - padding.bottom - font.lineHeight; - image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, layout.footer, 110); + image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, footerText(layout.footer), 110); bottom = fixedBottom = footerY - MENU_FOOTER_GAP; } if (bottom <= top) return image; @@ -431,17 +463,26 @@ export class MenuLayer implements Layer { } } +export type ModalMenuOptions = { + /** A dim line under the items (see MenuLayout.footer). */ + footer?: MenuFooter; + /** Runs when the menu leaves the stack, however it closes. */ + onClosed?: () => void; +}; + /** Open a centered, bordered menu over the current layer. */ export function openModalMenu( ctx: LayerContext, title: string, items: MenuItem[], initialSelectedIndex = 0, + options: ModalMenuOptions = {}, ): void { const { width, height } = ctx.stack.getBaseSize(); const font = getDefaultSmallFont(); const menuWidth = Math.min(320, width - 40); - const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font); + const footerHeight = options.footer ? MENU_FOOTER_GAP + lineStep(font) : 0; + const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font) + footerHeight; // 20px clear above and below, or 8 in a short window (the RayNeo iO's), // where those rows are worth more than the margin. const marginY = height < SHORT_WINDOW_HEIGHT ? 8 : 20; @@ -454,8 +495,19 @@ export function openModalMenu( maxHeight: menuHeight, // Floats nearer than the content it opens over, like the window and system menus. depth: 4, + footer: options.footer, }; - ctx.stack.push(new MenuLayer(title, items, layout).selectItem(initialSelectedIndex)); + ctx.stack.push(new ModalMenuLayer(title, items, layout, options.onClosed).selectItem(initialSelectedIndex)); +} + +class ModalMenuLayer extends MenuLayer { + constructor(title: string, items: MenuItem[], layout: MenuLayout, private readonly onClosed?: () => void) { + super(title, items, layout); + } + + onRemoved(): void { + this.onClosed?.(); + } } export function isMenuItemDisabled(item: MenuItem): boolean { diff --git a/app/ui/shell/in-process-window.ts b/app/ui/shell/in-process-window.ts index d0b7990c..7061d509 100644 --- a/app/ui/shell/in-process-window.ts +++ b/app/ui/shell/in-process-window.ts @@ -48,6 +48,8 @@ export type InProcessWindowOptions = { setScreenOn?: ShellWindow["setScreenOn"]; /** See ShellWindow.keepsScreenOn. */ keepsScreenOn?: ShellWindow["keepsScreenOn"]; + /** See ShellWindow.restoresApp. */ + restoresApp?: boolean; /** The window came to or left the foreground (after its surface visibility follows). */ onForegroundChanged?: (foreground: boolean) => void; /** Shared actions; requestRender is rebound to this window's render. */ @@ -177,6 +179,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces holdToTalk: options.holdToTalk, isVoiceCapturing: options.isVoiceCapturing, keepsScreenOn: options.keepsScreenOn, + restoresApp: options.restoresApp, setScreenOn: options.setScreenOn, heightMode, hasAppMenu: () => appMenuItems().length > 0, diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index 683bc97c..e8792750 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -17,6 +17,7 @@ import { isWatchInput, makeInputEvent, } from "../gestures"; +import type { KeyEvent } from "../../g2/keyboard/keys"; import { Layer, LayerActions, LayerContext, LayerStack, noopLayerActions } from "../layers"; import { CONTEXT_MENU_DIM, MenuLayer, type MenuItem } from "../menu"; import { VoiceInputLayer, type VoiceSendTarget } from "./voice-input"; @@ -151,6 +152,13 @@ export type ShellWindow = { * draws this window's top bar. */ heightMode: WindowHeightMode; + /** + * False for a window that must not bring its app back when Faceclaw + * restarts (the controller's open-apps restore relaunches apps, not + * windows): one that shows something a plain launch of its app would not, + * where the relaunch would do something else in its place. + */ + restoresApp?: boolean; /** App-side cleanup when the shell closes the window (worker notification, surface removal). */ close?: () => void; drawIcon: ShellChromeWindow["drawIcon"]; @@ -185,6 +193,15 @@ export type ShellWindow = { * only windows that consume typed text (the terminal) implement it. */ receiveTextInput?: (text: string, options?: { submit?: boolean }) => void; + /** + * Take a key from a hardware keyboard (app/g2/keyboard) as a key. Optional: + * only windows that give keys a meaning of their own (the terminal) + * implement it. They get the keys no keybinding claimed while they have + * input focus, presses and releases alike; the rest of the time, and for + * every other window, the navigation keys act as gestures (see + * app/g2/keyboard/bindings.ts). + */ + handleKeyEvent?: (event: KeyEvent) => void; /** Foreground state changed: this window's surface is (not) the visible one. */ setForeground?: (foreground: boolean) => void; /** @@ -1599,6 +1616,21 @@ class Shell { this.foregroundWindow()?.receiveTextInput?.(text, options); } + /** + * Offer a hardware-keyboard key to the window that has input focus. True + * when that window takes keys (ShellWindow.handleKeyEvent) and got this + * one; false when the key is the caller's to interpret: the screen is off, + * the sidebar or a shell overlay has focus, or the window has no use for + * keys. Typing counts as activity for the screen timeout. + */ + receiveKeyEvent(event: KeyEvent): boolean { + const window = this.inputTargetWindow(); + if (!window?.handleKeyEvent) return false; + if (event.phase === "down") this.lastInputAtMs = Date.now(); + window.handleKeyEvent(event); + return true; + } + /** * Open the voice dialog aimed at the foreground window. Called when the * user picks Voice input from the system menu (or an app asks via a diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts index 2e38e1dc..b6b3e9ad 100644 --- a/app/ui/shell/worker-window.ts +++ b/app/ui/shell/worker-window.ts @@ -28,6 +28,8 @@ export type WorkerAppMessage = | { type: "close-window"; windowId: string } | { type: "input"; windowId: string; event: unknown; frameId: number; focused: boolean } | { type: "text-input"; windowId: string; text: string; submit?: boolean } + /** A hardware-keyboard key (a KeyEvent), for windows opened with acceptsKeys. */ + | { type: "key"; windowId: string; event: unknown; focused: boolean } | { type: "render"; windowId: string; focused: boolean } | { type: "foreground"; windowId: string; foreground: boolean; focused: boolean } /** @@ -56,6 +58,16 @@ export type WorkerAppReply = type: "worker-ready"; } | { type: "yield-focus"; windowId: string } + | { + /** + * A "key" message the window had no use for as a key (a terminal + * showing its session list rather than a session). The host gives it + * the keyboard's navigation meaning instead. + */ + type: "key-unhandled"; + windowId: string; + event: unknown; + } | { /** Foreground and focus one of the app's existing windows. */ type: "focus-window"; @@ -81,6 +93,8 @@ export type WorkerAppReply = focus?: boolean; /** Window height: the standard 288px band ("min", default) or full screen ("max"). */ heightMode?: WindowHeightMode; + /** Send the window hardware-keyboard keys (see WorkerWindowSpec). */ + acceptsKeys?: boolean; } | { /** @@ -196,6 +210,13 @@ export type WorkerWindowSpec = { * of all four directions in every window state (see ShellWindow). */ acceptsDirectional?: boolean; + /** + * Deliver hardware-keyboard keys to the worker as "key" messages while the + * window has input focus (see ShellWindow.handleKeyEvent). The worker + * answers "key-unhandled" for a key it doesn't want as a key just then, + * which gets the keyboard's navigation meaning instead. + */ + acceptsKeys?: boolean; }; export type WorkerAppHostOptions = { @@ -223,6 +244,8 @@ export type WorkerAppHostOptions = { startTextSettingEdit: (settingId: string) => void; /** Close the phone text editor opened by startTextSettingEdit. */ endTextSettingEdit: () => void; + /** A key a window was sent and handed back (the "key-unhandled" reply); a KeyEvent. */ + unhandledKey?: (event: unknown) => void; }; /** @@ -312,6 +335,10 @@ export class WorkerAppHost { shell.yieldFocusToSidebar(); } break; + case "key-unhandled": + // Stale once focus has moved on: the key was meant for that window. + if (shell.isWindowFocused(message.windowId)) this.options.unhandledKey?.(message.event); + break; case "focus-window": if (this.openWindows.has(message.windowId)) { shell.focusWindow(message.windowId); @@ -335,6 +362,7 @@ export class WorkerAppHost { iconGlyph: message.iconGlyph, focus: message.focus, heightMode: message.heightMode, + acceptsKeys: message.acceptsKeys, }); break; case "set-height-mode": @@ -522,6 +550,13 @@ export class WorkerAppHost { receiveTextInput: (text, options) => { this.post({ type: "text-input", windowId: spec.windowId, text, ...(options?.submit === undefined ? {} : { submit: options.submit }) }); }, + ...(spec.acceptsKeys + ? { + handleKeyEvent: (event) => { + this.post({ type: "key", windowId: spec.windowId, event, focused: shell.isWindowFocused(spec.windowId) }); + }, + } + : {}), setForeground: (foreground) => { this.options.setSurfaceVisible(surfaceId, foreground); if (foreground) { diff --git a/hooks/before-prepare/faceclaw-kotlin.js b/hooks/before-prepare/faceclaw-kotlin.js index 435b315f..03dc7ecb 100644 --- a/hooks/before-prepare/faceclaw-kotlin.js +++ b/hooks/before-prepare/faceclaw-kotlin.js @@ -1,8 +1,11 @@ const { buildKotlin, checkAndroidRuntime } = require('../../scripts/kotlin-build.cjs'); +const { buildGoSsh } = require('../../scripts/go-ssh-build.cjs'); module.exports = function (hookArgs) { // NativeScript parses hooks with an older JavaScript parser; avoid optional chaining. const { platform, release } = hookArgs.prepareData; if (platform === 'android') checkAndroidRuntime(); - return buildKotlin(platform, release ? 'release' : 'debug'); + return buildKotlin(platform, release ? 'release' : 'debug').then(function () { + return buildGoSsh(platform); + }); }; diff --git a/native/kotlin/README.md b/native/kotlin/README.md index 4ad08a49..40e73b9e 100644 --- a/native/kotlin/README.md +++ b/native/kotlin/README.md @@ -11,7 +11,8 @@ local `@faceclaw/kotlin` plugin. Generated binaries and Gradle caches are ignore callback interfaces. Since 2026-09-22 also the platform-neutral cores behind the Android adapters: `util/` (Json codec, FrameTimingsCore, concurrency primitives, sha256/clock/log/file expects), `g2protocol/FirmwareImage`, - `audio/` (Lc3PacketFramer, AudioSegmentation, VoicePrint, WavPcmReader), + `audio/` (Lc3PacketFramer, AudioSegmentation, VoicePrint, WavPcmReader, the Matroska + writer/reader and the MicRecording capture format built on it, MicArrayProtocol), `graphics/` (GrayPacket, PreviewPalette, StatusIconArt, NotificationIconArt, ColorGlyphArt, GlyphPacket, OpenTypeNames, SvgIconSource), `net/` (HttpIdentity, SseStream, ResumableDownload, RemoteInputSession), `evenhub/EvenHubAssetServer`, diff --git a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformUtil.android.kt b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformUtil.android.kt index 7e1e63f9..ff0bcdaf 100644 --- a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformUtil.android.kt +++ b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformUtil.android.kt @@ -69,3 +69,41 @@ actual fun fileLength(path: String): Long = java.io.File(path).let { if (it.exis actual fun deleteFile(path: String): Boolean = java.io.File(path).delete() actual fun renameFile(from: String, to: String): Boolean = java.io.File(from).renameTo(java.io.File(to)) + +actual fun openFileOutput(path: String): SeekableOutput = object : SeekableOutput { + private val file = java.io.RandomAccessFile(path, "rw").also { it.setLength(0) } + override var size = 0L + private set + + override fun write(bytes: ByteArray, offset: Int, length: Int) { + file.write(bytes, offset, length) + size += length + } + + override fun writeAt(position: Long, bytes: ByteArray) { + require(position >= 0 && position + bytes.size <= size) + file.seek(position) + file.write(bytes) + file.seek(size) + } + + override fun close() = file.close() +} + +actual fun openFileInput(path: String): SeekableInput = object : SeekableInput { + private val file = java.io.RandomAccessFile(path, "r") + override val length = file.length() + + override fun readAt(position: Long, buffer: ByteArray, offset: Int, length: Int): Int { + file.seek(position) + var total = 0 + while (total < length) { + val read = file.read(buffer, offset + total, length - total) + if (read <= 0) break + total += read + } + return total + } + + override fun close() = file.close() +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt index 739c23e9..e1c717f7 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt @@ -20,3 +20,33 @@ expect fun fileLength(path: String): Long expect fun deleteFile(path: String): Boolean expect fun renameFile(from: String, to: String): Boolean + +/** A file being written front to back whose earlier bytes can still be patched (container headers). */ +interface SeekableOutput { + /** Bytes appended so far. */ + val size: Long + + /** Appends [length] bytes of [bytes] from [offset]. */ + fun write(bytes: ByteArray, offset: Int, length: Int) + + /** Overwrites bytes already written, at [position]; later appends still go to the end. */ + fun writeAt(position: Long, bytes: ByteArray) + + fun close() +} + +/** A file read at arbitrary positions without loading it whole. */ +interface SeekableInput { + val length: Long + + /** Reads up to [length] bytes at [position] into [buffer]; returns the count read (short only at the end). */ + fun readAt(position: Long, buffer: ByteArray, offset: Int, length: Int): Int + + fun close() +} + +/** Creates (or truncates) [path] for writing; parent directories must exist. Throws when it cannot. */ +expect fun openFileOutput(path: String): SeekableOutput + +/** Opens [path] for reading. Throws when it cannot. */ +expect fun openFileInput(path: String): SeekableInput diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/Matroska.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/Matroska.kt new file mode 100644 index 00000000..897b700c --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/Matroska.kt @@ -0,0 +1,764 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField + +/** + * A small Matroska (EBML) muxer and demuxer for audio captures (.mka): PCM16 audio tracks that + * each have their own sample rate and channel count, UTF-8 text tracks for timed metadata, and + * file attachments. Every block carries its own timestamp, so tracks on independent clocks and + * gaps in a track survive in the file. See notes/mic-recording-format.md for how captures use it. + * + * The writer produces files standard players open (VLC, mpv, ffmpeg): known-size clusters of + * about a second each, cues and the segment size written when the recording finishes. A + * recording cut short by a crash is still a valid live-style stream of everything up to its last + * complete cluster, and the reader takes it as such. + */ +internal object MatroskaIds { + const val EBML = 0x1A45DFA3 + const val EBML_VERSION = 0x4286 + const val EBML_READ_VERSION = 0x42F7 + const val EBML_MAX_ID_LENGTH = 0x42F2 + const val EBML_MAX_SIZE_LENGTH = 0x42F3 + const val DOC_TYPE = 0x4282 + const val DOC_TYPE_VERSION = 0x4287 + const val DOC_TYPE_READ_VERSION = 0x4285 + const val VOID = 0xEC + const val SEGMENT = 0x18538067 + const val SEEK_HEAD = 0x114D9B74 + const val SEEK = 0x4DBB + const val SEEK_ID = 0x53AB + const val SEEK_POSITION = 0x53AC + const val INFO = 0x1549A966 + const val TIMESTAMP_SCALE = 0x2AD7B1 + const val DURATION = 0x4489 + const val DATE_UTC = 0x4461 + const val TITLE = 0x7BA9 + const val MUXING_APP = 0x4D80 + const val WRITING_APP = 0x5741 + const val TRACKS = 0x1654AE6B + const val TRACK_ENTRY = 0xAE + const val TRACK_NUMBER = 0xD7 + const val TRACK_UID = 0x73C5 + const val TRACK_TYPE = 0x83 + const val FLAG_DEFAULT = 0x88 + const val FLAG_LACING = 0x9C + const val NAME = 0x536E + const val LANGUAGE = 0x22B59C + const val CODEC_ID = 0x86 + const val AUDIO = 0xE1 + const val SAMPLING_FREQUENCY = 0xB5 + const val CHANNELS = 0x9F + const val BIT_DEPTH = 0x6264 + const val ATTACHMENTS = 0x1941A469 + const val ATTACHED_FILE = 0x61A7 + const val FILE_NAME = 0x466E + const val FILE_MIME_TYPE = 0x4660 + const val FILE_DATA = 0x465C + const val FILE_UID = 0x46AE + const val CLUSTER = 0x1F43B675 + const val TIMESTAMP = 0xE7 + const val SIMPLE_BLOCK = 0xA3 + const val BLOCK_GROUP = 0xA0 + const val BLOCK = 0xA1 + const val BLOCK_DURATION = 0x9B + const val CUES = 0x1C53BB6B + const val CUE_POINT = 0xBB + const val CUE_TIME = 0xB3 + const val CUE_TRACK_POSITIONS = 0xB7 + const val CUE_TRACK = 0xF7 + const val CUE_CLUSTER_POSITION = 0xF1 +} + +/** Builds EBML elements into a byte buffer. */ +internal class EbmlWriter(capacity: Int = 256) { + private val sink = ByteSink(capacity) + + fun size(): Int = sink.size() + + fun bytes(): ByteArray = sink.toByteArray() + + /** An element ID, written as given: its length marker is part of the value. */ + fun id(id: Int) { + if (id > 0xFFFFFF) sink.write(id ushr 24) + if (id > 0xFFFF) sink.write(id ushr 16) + if (id > 0xFF) sink.write(id ushr 8) + sink.write(id) + } + + /** A data size in [width] bytes, or in the fewest that hold it when [width] is 0. */ + fun dataSize(size: Long, width: Int = 0) { + var bytes = width + if (bytes == 0) { + bytes = 1 + // The all-ones value of each width means "unknown size". + while (bytes < 8 && size >= (1L shl (7 * bytes)) - 1) bytes++ + } + val marked = size or (1L shl (7 * bytes)) + for (index in bytes - 1 downTo 0) sink.write((marked ushr (8 * index)).toInt()) + } + + fun uint(id: Int, value: Long) { + var bytes = 1 + while (bytes < 8 && (value ushr (8 * bytes)) != 0L) bytes++ + id(id) + dataSize(bytes.toLong()) + for (index in bytes - 1 downTo 0) sink.write((value ushr (8 * index)).toInt()) + } + + /** A fixed eight-byte integer (dates). */ + fun int64(id: Int, value: Long) { + id(id) + dataSize(8) + for (index in 7 downTo 0) sink.write((value ushr (8 * index)).toInt()) + } + + fun float64(id: Int, value: Double) = int64(id, value.toRawBits()) + + fun string(id: Int, text: String) { + val bytes = text.encodeToByteArray() + binary(id, bytes, 0, bytes.size) + } + + fun binary(id: Int, bytes: ByteArray, offset: Int, length: Int) { + id(id) + dataSize(length.toLong()) + sink.write(bytes, offset, length) + } + + fun raw(bytes: ByteArray) = sink.write(bytes) + + fun raw(value: Int) = sink.write(value) + + fun master(id: Int, build: EbmlWriter.() -> Unit) { + val child = EbmlWriter() + child.build() + id(id) + dataSize(child.size().toLong()) + sink.write(child.bytes()) + } + + companion object { + /** [element] followed by a Void element filling the rest of [reserved] bytes, or null when it doesn't fit. */ + fun padded(element: ByteArray, reserved: Int): ByteArray? { + val filler = reserved - element.size + // A Void element is at least two bytes. + if (filler < 0 || filler == 1) return null + val out = element.copyOf(reserved) + if (filler == 0) return out + val at = element.size + out[at] = MatroskaIds.VOID.toByte() + if (filler < 9) { + out[at + 1] = (0x80 or (filler - 2)).toByte() + } else { + val size = (filler - 9).toLong() + out[at + 1] = 0x01 + for (index in 0 until 7) out[at + 2 + index] = (size ushr (8 * (6 - index))).toByte() + } + return out + } + } +} + +class MkaAttachment(@JvmField val name: String, @JvmField val mimeType: String, @JvmField val data: ByteArray) + +/** + * Writes one Matroska audio file. Tracks can be added at any time before the first block that + * uses them: the track list sits in a reserved region that is rewritten in place. Timestamps are + * milliseconds from the start of the file. Not thread-safe. + */ +class MkaWriter( + private val output: SeekableOutput, + title: String?, + /** When the recording began (Unix epoch ms), for the file's date. */ + dateUtcMs: Long, + attachments: List<MkaAttachment>, +) { + companion object { + const val CODEC_PCM = "A_PCM/INT/LIT" + const val CODEC_TEXT = "S_TEXT/UTF8" + const val TRACK_AUDIO = 2 + const val TRACK_SUBTITLE = 0x11 + const val APP_NAME = "Faceclaw" + private const val SEEK_RESERVED = 128 + private const val TRACKS_RESERVED = 4096 + private const val CLUSTER_SPAN_MS = 1000L + private const val CLUSTER_MAX_BYTES = 1 shl 20 + // Block timestamps are signed 16-bit offsets from their cluster's. + private const val CLUSTER_MAX_BACKSTEP_MS = 30_000L + private const val DURATION_PATCH_CLUSTERS = 5 + // Matroska dates count nanoseconds from 2001-01-01T00:00:00 UTC. + private const val DATE_EPOCH_MS = 978_307_200_000L + } + + private class Track( + val number: Int, + val type: Int, + val name: String, + val sampleRate: Int, + val channels: Int, + val preferred: Boolean, + ) + + private class Cue(val clusterPosition: Long, val timeMs: Long) + + private val tracks = ArrayList<Track>() + private val cues = ArrayList<Cue>() + private val segmentSizePosition: Long + private val segmentStart: Long + private val seekPosition: Long + private val infoPosition: Long + private val durationPosition: Long + private val tracksPosition: Long + private val attachmentsPosition: Long + private var cluster: EbmlWriter? = null + private var clusterBaseMs = 0L + private var clustersSincePatch = 0 + private var finished = false + private val hasAttachments = attachments.isNotEmpty() + + /** The end of the latest audio or text written, in ms from the start of the file. */ + var durationMs = 0L + private set + + init { + val head = EbmlWriter() + head.master(MatroskaIds.EBML) { + uint(MatroskaIds.EBML_VERSION, 1) + uint(MatroskaIds.EBML_READ_VERSION, 1) + uint(MatroskaIds.EBML_MAX_ID_LENGTH, 4) + uint(MatroskaIds.EBML_MAX_SIZE_LENGTH, 8) + string(MatroskaIds.DOC_TYPE, "matroska") + uint(MatroskaIds.DOC_TYPE_VERSION, 4) + uint(MatroskaIds.DOC_TYPE_READ_VERSION, 2) + } + head.id(MatroskaIds.SEGMENT) + segmentSizePosition = head.size().toLong() + // Unknown size (a live stream) until finish() knows it. + head.raw(0x01) + repeat(7) { head.raw(0xFF) } + segmentStart = head.size().toLong() + append(head.bytes()) + + seekPosition = output.size + infoPosition = seekPosition + SEEK_RESERVED + val info = EbmlWriter() + info.master(MatroskaIds.INFO) { + uint(MatroskaIds.TIMESTAMP_SCALE, 1_000_000) + string(MatroskaIds.MUXING_APP, APP_NAME) + string(MatroskaIds.WRITING_APP, APP_NAME) + int64(MatroskaIds.DATE_UTC, (dateUtcMs - DATE_EPOCH_MS) * 1_000_000) + if (!title.isNullOrEmpty()) string(MatroskaIds.TITLE, title) + // Last, so its eight bytes are the element's final ones. + float64(MatroskaIds.DURATION, 0.0) + } + durationPosition = infoPosition + info.size() - 8 + tracksPosition = infoPosition + info.size() + attachmentsPosition = tracksPosition + TRACKS_RESERVED + + append(seekHead(-1)) + append(info.bytes()) + append(EbmlWriter.padded(ByteArray(0), TRACKS_RESERVED)!!) + if (attachments.isNotEmpty()) { + val files = EbmlWriter() + files.master(MatroskaIds.ATTACHMENTS) { + attachments.forEachIndexed { index, file -> + master(MatroskaIds.ATTACHED_FILE) { + string(MatroskaIds.FILE_NAME, file.name) + string(MatroskaIds.FILE_MIME_TYPE, file.mimeType) + binary(MatroskaIds.FILE_DATA, file.data, 0, file.data.size) + uint(MatroskaIds.FILE_UID, index + 1L) + } + } + } + append(files.bytes()) + } + } + + private fun append(bytes: ByteArray) = output.write(bytes, 0, bytes.size) + + private fun seekHead(cuesPosition: Long): ByteArray { + val head = EbmlWriter() + head.master(MatroskaIds.SEEK_HEAD) { + fun entry(id: Int, position: Long) = master(MatroskaIds.SEEK) { + val idBytes = EbmlWriter() + idBytes.id(id) + binary(MatroskaIds.SEEK_ID, idBytes.bytes(), 0, idBytes.size()) + uint(MatroskaIds.SEEK_POSITION, position - segmentStart) + } + entry(MatroskaIds.INFO, infoPosition) + entry(MatroskaIds.TRACKS, tracksPosition) + if (hasAttachments) entry(MatroskaIds.ATTACHMENTS, attachmentsPosition) + if (cuesPosition >= 0) entry(MatroskaIds.CUES, cuesPosition) + } + return EbmlWriter.padded(head.bytes(), SEEK_RESERVED) ?: error("seek head exceeds its reserved space") + } + + /** + * Adds a PCM16 little-endian audio track and returns its number, or -1 when the track list + * is full. A [preferred] track is listed ahead of the others, and the first listed track is + * the one players pick by default. + */ + fun addAudioTrack(name: String, sampleRate: Int, channels: Int, preferred: Boolean): Int = + addTrack(Track(tracks.size + 1, TRACK_AUDIO, name, sampleRate, channels, preferred)) + + /** Adds a UTF-8 text track (one string per block) and returns its number, or -1 when the track list is full. */ + fun addTextTrack(name: String): Int = addTrack(Track(tracks.size + 1, TRACK_SUBTITLE, name, 0, 0, false)) + + private fun addTrack(track: Track): Int { + check(!finished) { "recording already finished" } + // A block's track number is written in one byte. + if (track.number > 126) return -1 + tracks.add(track) + val region = EbmlWriter.padded(tracksElement(), TRACKS_RESERVED) + if (region == null) { + tracks.remove(track) + return -1 + } + output.writeAt(tracksPosition, region) + return track.number + } + + private fun listedTracks(): List<Track> = + tracks.filter { it.type == TRACK_AUDIO && it.preferred } + + tracks.filter { it.type == TRACK_AUDIO && !it.preferred } + + tracks.filter { it.type != TRACK_AUDIO } + + private fun tracksElement(): ByteArray { + val listed = listedTracks() + val defaultTrack = listed.firstOrNull { it.type == TRACK_AUDIO } + val out = EbmlWriter(1024) + out.master(MatroskaIds.TRACKS) { + for (track in listed) { + master(MatroskaIds.TRACK_ENTRY) { + uint(MatroskaIds.TRACK_NUMBER, track.number.toLong()) + uint(MatroskaIds.TRACK_UID, track.number.toLong()) + uint(MatroskaIds.TRACK_TYPE, track.type.toLong()) + uint(MatroskaIds.FLAG_DEFAULT, if (track === defaultTrack) 1 else 0) + uint(MatroskaIds.FLAG_LACING, 0) + string(MatroskaIds.NAME, track.name) + string(MatroskaIds.LANGUAGE, "und") + if (track.type == TRACK_AUDIO) { + string(MatroskaIds.CODEC_ID, CODEC_PCM) + master(MatroskaIds.AUDIO) { + float64(MatroskaIds.SAMPLING_FREQUENCY, track.sampleRate.toDouble()) + uint(MatroskaIds.CHANNELS, track.channels.toLong()) + uint(MatroskaIds.BIT_DEPTH, 16) + } + } else { + string(MatroskaIds.CODEC_ID, CODEC_TEXT) + } + } + } + } + return out.bytes() + } + + private fun track(number: Int): Track = tracks.getOrNull(number - 1) ?: error("no track $number") + + /** Appends interleaved PCM16 little-endian samples to an audio track, starting at [timeMs]. */ + fun writeAudio(trackNumber: Int, timeMs: Long, pcm16le: ByteArray, offset: Int, length: Int) { + val track = track(trackNumber) + require(track.type == TRACK_AUDIO) { "track $trackNumber is not audio" } + if (length <= 0) return + val block = openCluster(timeMs) + block.id(MatroskaIds.SIMPLE_BLOCK) + block.dataSize(4L + length) + blockHeader(block, trackNumber, timeMs, 0x80) + block.raw(if (offset == 0 && length == pcm16le.size) pcm16le else pcm16le.copyOfRange(offset, offset + length)) + val frames = length / (2 * track.channels) + noteEnd(timeMs + frames * 1000L / track.sampleRate) + } + + /** Appends one string to a text track, shown from [timeMs] for [durationMs]. */ + fun writeText(trackNumber: Int, timeMs: Long, durationMs: Long, text: String) { + require(track(trackNumber).type == TRACK_SUBTITLE) { "track $trackNumber is not text" } + val bytes = text.encodeToByteArray() + val block = openCluster(timeMs) + block.master(MatroskaIds.BLOCK_GROUP) { + id(MatroskaIds.BLOCK) + dataSize(4L + bytes.size) + blockHeader(this, trackNumber, timeMs, 0) + raw(bytes) + uint(MatroskaIds.BLOCK_DURATION, maxOf(1L, durationMs)) + } + noteEnd(timeMs + maxOf(1L, durationMs)) + } + + private fun blockHeader(block: EbmlWriter, trackNumber: Int, timeMs: Long, flags: Int) { + val relative = (maxOf(0L, timeMs) - clusterBaseMs).toInt() + block.raw(0x80 or trackNumber) + block.raw(relative shr 8) + block.raw(relative) + block.raw(flags) + } + + private fun noteEnd(endMs: Long) { + if (endMs > durationMs) durationMs = endMs + } + + /** The cluster a block at [timeMs] goes in: the open one, or a new one once that is full or too far away in time. */ + private fun openCluster(timeMs: Long): EbmlWriter { + check(!finished) { "recording already finished" } + val time = maxOf(0L, timeMs) + val open = cluster + if (open != null) { + val relative = time - clusterBaseMs + if (relative < CLUSTER_SPAN_MS && relative > -CLUSTER_MAX_BACKSTEP_MS && open.size() < CLUSTER_MAX_BYTES) return open + flushCluster() + } + clusterBaseMs = time + return EbmlWriter(64 * 1024).also { cluster = it } + } + + private fun flushCluster() { + val body = cluster ?: return + cluster = null + if (body.size() == 0) return + val timestamp = EbmlWriter(16) + timestamp.uint(MatroskaIds.TIMESTAMP, clusterBaseMs) + val header = EbmlWriter(32) + header.id(MatroskaIds.CLUSTER) + header.dataSize(timestamp.size().toLong() + body.size()) + header.raw(timestamp.bytes()) + cues.add(Cue(output.size - segmentStart, clusterBaseMs)) + append(header.bytes()) + append(body.bytes()) + if (++clustersSincePatch >= DURATION_PATCH_CLUSTERS) patchDuration() + } + + private fun patchDuration() { + clustersSincePatch = 0 + val value = durationMs.toDouble().toRawBits() + output.writeAt(durationPosition, ByteArray(8) { (value ushr (8 * (7 - it))).toByte() }) + } + + /** Writes what is buffered, then the cues, duration and segment size, and closes the file. */ + fun finish() { + if (finished) return + try { + flushCluster() + finished = true + val cuesPosition = output.size + val cueTrack = listedTracks().firstOrNull()?.number ?: 1 + if (cues.isNotEmpty()) { + val index = EbmlWriter(cues.size * 16 + 16) + index.master(MatroskaIds.CUES) { + for (cue in cues) { + master(MatroskaIds.CUE_POINT) { + uint(MatroskaIds.CUE_TIME, cue.timeMs) + master(MatroskaIds.CUE_TRACK_POSITIONS) { + uint(MatroskaIds.CUE_TRACK, cueTrack.toLong()) + uint(MatroskaIds.CUE_CLUSTER_POSITION, cue.clusterPosition) + } + } + } + } + append(index.bytes()) + output.writeAt(seekPosition, seekHead(cuesPosition)) + } + patchDuration() + val size = EbmlWriter(8) + size.dataSize(output.size - segmentStart, 8) + output.writeAt(segmentSizePosition, size.bytes()) + } finally { + finished = true + output.close() + } + } +} + +class MkaTrackInfo( + @JvmField val number: Int, + /** [MkaWriter.TRACK_AUDIO], [MkaWriter.TRACK_SUBTITLE] or another Matroska track type. */ + @JvmField val type: Int, + @JvmField val codecId: String, + @JvmField val name: String, + @JvmField val sampleRate: Int, + @JvmField val channels: Int, + @JvmField val bitDepth: Int, +) { + val isPcm16: Boolean get() = type == MkaWriter.TRACK_AUDIO && codecId == MkaWriter.CODEC_PCM && bitDepth == 16 +} + +class MkaBlock( + @JvmField val track: Int, + @JvmField val timeMs: Long, + @JvmField val data: ByteArray, + /** From the block's group, or -1 when the file doesn't say (audio blocks). */ + @JvmField val durationMs: Long, +) + +/** + * Reads the blocks of a Matroska file in file order, a cluster at a time. Handles what + * [MkaWriter] and ffmpeg write for PCM audio: known-size clusters, unlaced blocks. Throws + * IllegalArgumentException for anything that is not such a file. + */ +class MkaReader(private val input: SeekableInput) { + private class ClusterRef(val position: Long, val size: Int, val rawTime: Long) + + private val clusters = ArrayList<ClusterRef>() + private val trackList = ArrayList<MkaTrackInfo>() + private val attachmentList = ArrayList<MkaAttachment>() + private var timestampScale = 1_000_000L + private var infoDurationMs = 0L + private var nextCluster = 0 + private var blocks: List<MkaBlock> = emptyList() + private var blockIndex = 0 + private var skipBeforeMs = 0L + + val tracks: List<MkaTrackInfo> get() = trackList + val attachments: List<MkaAttachment> get() = attachmentList + + /** The end of the last audio or text in the file, in ms. */ + val durationMs: Long + + init { + scan() + var end = infoDurationMs + if (clusters.isNotEmpty()) { + for (block in readCluster(clusters.last())) end = maxOf(end, block.timeMs + blockSpanMs(block)) + } + durationMs = end + } + + fun track(number: Int): MkaTrackInfo? = trackList.firstOrNull { it.number == number } + + /** Continue from the first block at or after [timeMs]. */ + fun seekTo(timeMs: Long) { + val index = clusters.indexOfLast { toMs(it.rawTime) <= timeMs } + nextCluster = maxOf(0, index) + blocks = emptyList() + blockIndex = 0 + skipBeforeMs = timeMs + } + + /** The next block, or null at the end of the file. */ + fun next(): MkaBlock? { + while (true) { + while (blockIndex >= blocks.size) { + if (nextCluster >= clusters.size) return null + blocks = readCluster(clusters[nextCluster++]) + blockIndex = 0 + } + val block = blocks[blockIndex++] + if (block.timeMs >= skipBeforeMs) return block + } + } + + fun close() = input.close() + + private fun toMs(raw: Long): Long = raw * timestampScale / 1_000_000L + + private fun blockSpanMs(block: MkaBlock): Long { + if (block.durationMs >= 0) return block.durationMs + val track = track(block.track) ?: return 0 + if (!track.isPcm16 || track.channels <= 0 || track.sampleRate <= 0) return 0 + return block.data.size / (2L * track.channels) * 1000L / track.sampleRate + } + + /** An element header at [at] in [bytes]: its ID, payload size (-1 unknown) and header length; null when cut off. */ + private class Header(val id: Int, val size: Long, val length: Int) + + private fun header(bytes: ByteArray, at: Int, end: Int): Header? { + if (at >= end) return null + val first = bytes[at].toInt() and 0xff + val idLength = first.countLeadingZeroBits() - 24 + 1 + if (idLength > 4 || at + idLength >= end) return null + var id = 0 + for (index in 0 until idLength) id = (id shl 8) or (bytes[at + index].toInt() and 0xff) + val sizeAt = at + idLength + val sizeFirst = bytes[sizeAt].toInt() and 0xff + val sizeLength = sizeFirst.countLeadingZeroBits() - 24 + 1 + if (sizeLength > 8 || sizeAt + sizeLength > end) return null + var size = (sizeFirst and (0xff shr sizeLength)).toLong() + var allOnes = size == (0xff shr sizeLength).toLong() + for (index in 1 until sizeLength) { + val byte = bytes[sizeAt + index].toInt() and 0xff + if (byte != 0xff) allOnes = false + size = (size shl 8) or byte.toLong() + } + return Header(id, if (allOnes) -1 else size, idLength + sizeLength) + } + + private fun readAt(position: Long, length: Int): ByteArray { + val buffer = ByteArray(length) + val read = input.readAt(position, buffer, 0, length) + return if (read == length) buffer else buffer.copyOf(maxOf(0, read)) + } + + private fun uint(bytes: ByteArray, at: Int, size: Int): Long { + var value = 0L + for (index in 0 until size) value = (value shl 8) or (bytes[at + index].toLong() and 0xff) + return value + } + + private fun float(bytes: ByteArray, at: Int, size: Int): Double = when (size) { + 4 -> Float.fromBits(uint(bytes, at, 4).toInt()).toDouble() + 8 -> Double.fromBits(uint(bytes, at, 8)) + else -> 0.0 + } + + /** Calls [visit] with the ID, payload offset and payload size of each complete child element. */ + private inline fun children(bytes: ByteArray, start: Int, end: Int, visit: (id: Int, at: Int, size: Int) -> Unit) { + var at = start + while (true) { + val child = header(bytes, at, end) ?: return + val payload = at + child.length + if (child.size < 0 || child.size > end - payload) return + visit(child.id, payload, child.size.toInt()) + at = payload + child.size.toInt() + } + } + + private fun scan() { + val length = input.length + val start = readAt(0, 64) + val ebml = header(start, 0, start.size) + require(ebml != null && ebml.id == MatroskaIds.EBML && ebml.size >= 0) { "not a Matroska file" } + var position = ebml.length + ebml.size + val segment = readAt(position, 12).let { header(it, 0, it.size) } + require(segment != null && segment.id == MatroskaIds.SEGMENT) { "not a Matroska file" } + position += segment.length + val end = if (segment.size < 0) length else minOf(length, position + segment.size) + while (position < end) { + val head = readAt(position, 12) + val element = header(head, 0, head.size) ?: break + val payload = position + element.length + if (element.id == MatroskaIds.CLUSTER) { + require(element.size >= 0) { "unsupported Matroska file (clusters of unknown size)" } + // A recording cut short ends in a partial cluster; its complete blocks still count. + val available = minOf(element.size, end - payload) + require(available <= Int.MAX_VALUE) { "unsupported Matroska file (oversized cluster)" } + clusters.add(ClusterRef(payload, available.toInt(), clusterTime(payload, available.toInt()))) + } else if (element.size < 0) { + break + } else if (element.size <= end - payload && element.size <= 64L * 1024 * 1024) { + when (element.id) { + MatroskaIds.INFO -> parseInfo(readAt(payload, element.size.toInt())) + MatroskaIds.TRACKS -> parseTracks(readAt(payload, element.size.toInt())) + MatroskaIds.ATTACHMENTS -> parseAttachments(readAt(payload, element.size.toInt())) + } + } + if (element.size < 0) break + position = payload + element.size + } + } + + private fun clusterTime(position: Long, size: Int): Long { + val head = readAt(position, minOf(size, 16)) + val first = header(head, 0, head.size) + if (first != null && first.id == MatroskaIds.TIMESTAMP && first.size in 0..8 && first.length + first.size <= head.size) { + return uint(head, first.length, first.size.toInt()) + } + val whole = readAt(position, size) + var time = 0L + children(whole, 0, whole.size) { id, at, length -> if (id == MatroskaIds.TIMESTAMP) time = uint(whole, at, length) } + return time + } + + private fun parseInfo(bytes: ByteArray) { + var duration = 0.0 + children(bytes, 0, bytes.size) { id, at, size -> + when (id) { + MatroskaIds.TIMESTAMP_SCALE -> timestampScale = maxOf(1L, uint(bytes, at, size)) + MatroskaIds.DURATION -> duration = float(bytes, at, size) + } + } + infoDurationMs = (duration * timestampScale / 1_000_000.0).toLong() + } + + private fun parseTracks(bytes: ByteArray) { + trackList.clear() + children(bytes, 0, bytes.size) { id, at, size -> + if (id == MatroskaIds.TRACK_ENTRY) { + var number = 0 + var type = 0 + var codec = "" + var name = "" + var rate = 8000.0 + var channels = 1 + var depth = 0 + children(bytes, at, at + size) { field, fieldAt, fieldSize -> + when (field) { + MatroskaIds.TRACK_NUMBER -> number = uint(bytes, fieldAt, fieldSize).toInt() + MatroskaIds.TRACK_TYPE -> type = uint(bytes, fieldAt, fieldSize).toInt() + MatroskaIds.CODEC_ID -> codec = bytes.decodeToString(fieldAt, fieldAt + fieldSize).trimEnd('\u0000') + MatroskaIds.NAME -> name = bytes.decodeToString(fieldAt, fieldAt + fieldSize).trimEnd('\u0000') + MatroskaIds.AUDIO -> children(bytes, fieldAt, fieldAt + fieldSize) { audio, audioAt, audioSize -> + when (audio) { + MatroskaIds.SAMPLING_FREQUENCY -> rate = float(bytes, audioAt, audioSize) + MatroskaIds.CHANNELS -> channels = uint(bytes, audioAt, audioSize).toInt() + MatroskaIds.BIT_DEPTH -> depth = uint(bytes, audioAt, audioSize).toInt() + } + } + } + } + val audio = type == MkaWriter.TRACK_AUDIO + trackList.add( + MkaTrackInfo(number, type, codec, name, if (audio) rate.toInt() else 0, if (audio) channels else 0, if (audio) depth else 0), + ) + } + } + } + + private fun parseAttachments(bytes: ByteArray) { + children(bytes, 0, bytes.size) { id, at, size -> + if (id == MatroskaIds.ATTACHED_FILE) { + var name = "" + var mime = "" + var data = ByteArray(0) + children(bytes, at, at + size) { field, fieldAt, fieldSize -> + when (field) { + MatroskaIds.FILE_NAME -> name = bytes.decodeToString(fieldAt, fieldAt + fieldSize) + MatroskaIds.FILE_MIME_TYPE -> mime = bytes.decodeToString(fieldAt, fieldAt + fieldSize) + MatroskaIds.FILE_DATA -> data = bytes.copyOfRange(fieldAt, fieldAt + fieldSize) + } + } + attachmentList.add(MkaAttachment(name, mime, data)) + } + } + } + + private fun readCluster(cluster: ClusterRef): List<MkaBlock> { + val bytes = readAt(cluster.position, cluster.size) + val out = ArrayList<MkaBlock>() + var clusterRaw = cluster.rawTime + children(bytes, 0, bytes.size) { id, at, size -> + when (id) { + MatroskaIds.TIMESTAMP -> clusterRaw = uint(bytes, at, size) + MatroskaIds.SIMPLE_BLOCK -> block(bytes, at, size, clusterRaw, -1)?.let { out.add(it) } + MatroskaIds.BLOCK_GROUP -> { + var blockAt = -1 + var blockSize = 0 + var duration = -1L + children(bytes, at, at + size) { field, fieldAt, fieldSize -> + when (field) { + MatroskaIds.BLOCK -> { + blockAt = fieldAt + blockSize = fieldSize + } + MatroskaIds.BLOCK_DURATION -> duration = toMs(uint(bytes, fieldAt, fieldSize)) + } + } + if (blockAt >= 0) block(bytes, blockAt, blockSize, clusterRaw, duration)?.let { out.add(it) } + } + } + } + return out + } + + /** A block's payload: track number, 16-bit timestamp offset, flags, then the frame. Laced blocks are skipped. */ + private fun block(bytes: ByteArray, at: Int, size: Int, clusterRaw: Long, durationMs: Long): MkaBlock? { + if (size < 4) return null + val first = bytes[at].toInt() and 0xff + val trackLength = first.countLeadingZeroBits() - 24 + 1 + if (trackLength > 2 || size < trackLength + 3) return null + var track = first and (0xff shr trackLength) + if (trackLength == 2) track = (track shl 8) or (bytes[at + 1].toInt() and 0xff) + val head = at + trackLength + val relative = ((bytes[head].toInt() shl 8) or (bytes[head + 1].toInt() and 0xff)).toShort().toLong() + val flags = bytes[head + 2].toInt() and 0xff + if (flags and 0x06 != 0) return null + return MkaBlock(track, toMs(clusterRaw + relative), bytes.copyOfRange(head + 3, at + size), durationMs) + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/MicArrayProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/MicArrayProtocol.kt new file mode 100644 index 00000000..2014532b --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/MicArrayProtocol.kt @@ -0,0 +1,93 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * The parts of the CFW mic_control wire format the capture session speaks itself: the 'SM' + * multi-channel stream frame each temple sends, and the fixed STOP/RENEW control records. The + * CONFIGURE record is built by the caller (app/apps/microphones/mic-protocol.ts encodes it; the + * contract is the header of g2flash patches/mic_control.c). + */ +object MicArrayProtocol { + const val VERSION = 1 + const val HEADER_BYTES = 21 + const val OP_CONFIGURE = 1 + const val OP_STOP = 3 + const val OP_RENEW = 4 + /** How long an armed temple streams without a CONFIGURE or RENEW. */ + const val LEASE_MS = 90_000L + const val FLAG_TRUNCATED = 0x80 + const val CODEC_RAW = 1 + const val FORMAT_PCM16 = 0 + + /** One decoded 'SM' frame header; the samples are [payloadLength] bytes at [payloadOffset] of its packet. */ + class Frame( + /** Low 16 bits of the temple's frame counter. */ + @JvmField val sequence: Int, + /** The temple's own 1 ms tick when it packed the frame (unsigned 32-bit). */ + @JvmField val deviceTickMs: Long, + @JvmField val channels: Int, + @JvmField val sampleRate: Int, + @JvmField val format: Int, + @JvmField val codec: Int, + /** The temple's own bearing estimate, in signed degrees (0 unless it was asked for one). */ + @JvmField val angleDegrees: Int, + @JvmField val ssr: Int, + /** The firmware cut the payload short of the samples it captured. */ + @JvmField val truncated: Boolean, + @JvmField val payloadOffset: Int, + @JvmField val payloadLength: Int, + ) + + /** A control record with no payload: ['M','C', version, op]. */ + @JvmStatic + fun control(op: Int): ByteArray = byteArrayOf('M'.code.toByte(), 'C'.code.toByte(), VERSION.toByte(), op.toByte()) + + @JvmStatic + fun isConfigure(record: ByteArray?): Boolean = + record != null && record.size >= 13 && record[0] == 'M'.code.toByte() && record[1] == 'C'.code.toByte() && + record[2] == VERSION.toByte() && record[3] == OP_CONFIGURE.toByte() + + /** Decodes a stream frame's header, or returns null when [data] isn't one. */ + @JvmStatic + fun decodeFrame(data: ByteArray): Frame? { + if (data.size < HEADER_BYTES || data[0] != 'S'.code.toByte() || data[1] != 'M'.code.toByte() || data[2] != VERSION.toByte()) { + return null + } + val length = minOf(u16(data, 19), data.size - HEADER_BYTES) + return Frame( + sequence = u16(data, 4), + deviceTickMs = u16(data, 6).toLong() or (u16(data, 8).toLong() shl 16), + channels = data[10].toInt() and 0xff, + sampleRate = u16(data, 11) * 100, + format = data[13].toInt() and 0xff, + codec = data[14].toInt() and 0xff, + angleDegrees = u16(data, 15).toShort().toInt(), + ssr = u16(data, 17).toShort().toInt(), + truncated = (data[3].toInt() and FLAG_TRUNCATED) != 0, + payloadOffset = HEADER_BYTES, + payloadLength = length, + ) + } + + /** + * The frame's samples as interleaved PCM16 (whole sample frames only), or null when the + * payload isn't raw 16-bit PCM or holds none. + */ + @JvmStatic + fun pcm16(data: ByteArray, frame: Frame): ShortArray? { + if (frame.codec != CODEC_RAW || frame.format != FORMAT_PCM16 || frame.channels < 1) return null + val frames = frame.payloadLength / (2 * frame.channels) + if (frames <= 0) return null + val samples = ShortArray(frames * frame.channels) + var at = frame.payloadOffset + for (index in samples.indices) { + samples[index] = AudioSegmentation.pcm16le(data[at], data[at + 1]) + at += 2 + } + return samples + } + + private fun u16(data: ByteArray, at: Int): Int = (data[at].toInt() and 0xff) or ((data[at + 1].toInt() and 0xff) shl 8) +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/MicRecording.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/MicRecording.kt new file mode 100644 index 00000000..4488b007 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/MicRecording.kt @@ -0,0 +1,484 @@ +package com.faceclaw.app + +import kotlin.concurrent.Volatile +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic +import kotlin.math.abs + +/** Which microphones a chunk of capture audio came from. */ +object MicStream { + /** One mixed channel: the glasses' stock stream, or the phone's own microphone. */ + const val MONO = 0 + /** A temple's own microphones, on that temple's clock (channel 0 front, channel 1 rear). */ + const val LEFT = 1 + const val RIGHT = 2 + /** The mono feed derived from the temples for speech recognition: recorded, never replayed. */ + const val MIX = 3 + const val COUNT = 4 + + /** The stream's name in frame metadata. */ + @JvmStatic + fun key(stream: Int): String = when (stream) { + MONO -> "mic" + LEFT -> "left" + RIGHT -> "right" + else -> "mix" + } + + /** The name of the stream's audio tracks in a recording. */ + @JvmStatic + fun trackName(stream: Int): String = when (stream) { + MONO -> "mic" + LEFT -> "left temple" + RIGHT -> "right temple" + else -> "mix" + } + + /** The stream a recording's track or metadata names, or -1. */ + @JvmStatic + fun named(name: String): Int = (0 until COUNT).firstOrNull { trackName(it) == name || key(it) == name } ?: -1 +} + +/** + * One chunk of microphone audio as a capture source delivered it (about 50 ms), before any + * processing. Live captures, recordings and replays all trade in these. + */ +class MicChunk( + @JvmField val stream: Int, + @JvmField val channels: Int, + @JvmField val sampleRate: Int, + /** Interleaved PCM16; the first [frames] sample frames are the chunk. */ + @JvmField val pcm: ShortArray, + @JvmField val frames: Int, + /** When the chunk arrived: the phone's monotonic clock for a live capture, the offset into the file for a replay. */ + @JvmField val timeMs: Long, +) { + /** Whether the source reported a direction of arrival with the chunk. */ + @JvmField var hasDirection = false + @JvmField var angleDegrees = 0 + @JvmField var ssr = 0 + /** The source's packet counter, or -1 when it has none. */ + @JvmField var sequence = -1 + /** The source's own clock when it captured the chunk (unsigned 32-bit ms), or -1 when it has none. */ + @JvmField var deviceTickMs = -1L + /** Whose clock [deviceTickMs] is on: the stream the samples were captured by. */ + @JvmField var clockStream = stream + /** The source cut the chunk short of what it captured. */ + @JvmField var truncated = false + /** Sample frames known to be missing between the previous chunk of this stream and this one. */ + @JvmField var lostFramesBefore = 0 + + val durationMs: Long get() = frames * 1000L / sampleRate +} + +/** + * Derives the mono feed speech recognition hears from the temples' chunks. This is the seam for + * array processing (beamforming, noise cancellation): replays feed it the recorded chunks with + * the times they first arrived at, so an implementation that decides from the chunks alone + * behaves the same on a replay as it did live. + */ +interface MicArrayMixer { + /** The mono chunk [chunk] contributes to the feed, or null when it contributes nothing. */ + fun mix(chunk: MicChunk): MicChunk? + + fun reset() +} + +/** + * The feed is one temple at a time, its microphones averaged (the firmware's own stock downmix). + * The temples sample on independent clocks, so their audio is never summed. The left temple + * feeds whenever it is streaming; the right one stands in while it is not. + */ +class SingleTempleMixer : MicArrayMixer { + companion object { + /** A temple that has delivered nothing for this long no longer counts as streaming. */ + const val SILENT_MS = 300L + } + + private var leftAtMs = Long.MIN_VALUE + + override fun reset() { + leftAtMs = Long.MIN_VALUE + } + + override fun mix(chunk: MicChunk): MicChunk? { + if (chunk.stream == MicStream.LEFT) { + leftAtMs = chunk.timeMs + } else if (chunk.stream != MicStream.RIGHT || (leftAtMs != Long.MIN_VALUE && chunk.timeMs - leftAtMs < SILENT_MS)) { + return null + } + val channels = chunk.channels + val mono = ShortArray(chunk.frames) + for (frame in 0 until chunk.frames) { + var sum = 0 + for (channel in 0 until channels) sum += chunk.pcm[frame * channels + channel] + mono[frame] = (sum / channels).toShort() + } + val out = MicChunk(MicStream.MIX, 1, chunk.sampleRate, mono, chunk.frames, chunk.timeMs) + out.hasDirection = chunk.hasDirection + out.angleDegrees = chunk.angleDegrees + out.ssr = chunk.ssr + out.deviceTickMs = chunk.deviceTickMs + out.clockStream = chunk.stream + out.truncated = chunk.truncated + return out + } +} + +/** + * Records a capture's chunks to a Matroska audio file (.mka), one PCM16 track per stream and + * layout as each first appears, plus a "frames" text track with one JSON object per chunk that + * has metadata, written just ahead of the chunk's audio. Format: notes/mic-recording-format.md. + * + * Block timestamps are capture times, not arrival times. A stream's own clock (a temple's tick, + * or the count of samples from a source without one) says how far apart its chunks were + * captured; the least-delayed recent arrival places that clock on the file's timeline. So a + * chunk held up over Bluetooth still lands where it was captured, and lost audio leaves a gap. + * + * Safe to share between the capture thread and whoever finishes the recording. + */ +class MicRecordingWriter( + output: SeekableOutput, + /** A JSON document describing the capture, stored as the file's "capture.json" attachment. */ + configJson: String?, + /** When the recording began (Unix epoch ms). */ + startedAtMs: Long, + platform: ProtocolPlatform = protocolPlatform(), + private val logWarn: (String) -> Unit = { PlatformLog.w("FaceclawMicRecording", it) }, +) { + companion object { + const val CONFIG_ATTACHMENT = "capture.json" + const val META_TRACK = "frames" + /** How far a chunk's arrival may sit from where its stream's clock puts it before the clock is re-placed. */ + const val DISCONTINUITY_MS = 5000L + private const val WINDOW_BUCKETS = 8 + private const val BUCKET_MS = 1000L + } + + /** + * Places one track's chunks on the file's timeline. `source` time is the stream's own clock + * since this clock was anchored; the offset from it to arrival time is taken as the least + * seen over the last few seconds, which is the least-delayed delivery. + */ + private class TrackClock(val number: Int, val sampleRate: Int) { + private var anchored = false + private var clockStream = -1 + private var tickBase = -1L + private var lastTick = -1L + private var frames = 0L + private val minima = LongArray(WINDOW_BUCKETS) + private var bucket = 0 + private var bucketStartMs = 0L + var endMs = Long.MIN_VALUE + private set + + fun reset() { + anchored = false + } + + private fun offset(): Long = minima.min() + + fun stamp(chunk: MicChunk): Long { + val arrival = chunk.timeMs + val ticked = chunk.deviceTickMs >= 0 + if (anchored) { + // Another clock (the feed moved to the other temple), or this one restarted. + if (chunk.clockStream != clockStream || ticked != (tickBase >= 0)) anchored = false + else if (ticked && (chunk.deviceTickMs - lastTick).toInt() < 0) anchored = false + } + var anchorNow = !anchored + var source = 0L + if (anchored) { + frames += chunk.lostFramesBefore + source = sourceTime(chunk) + if (abs(arrival - source - offset()) > DISCONTINUITY_MS) anchorNow = true + } + if (anchorNow) { + anchored = true + clockStream = chunk.clockStream + tickBase = if (ticked) chunk.deviceTickMs else -1L + frames = 0 + minima.fill(Long.MAX_VALUE) + bucket = 0 + bucketStartMs = arrival + source = 0 + } + if (arrival - bucketStartMs >= BUCKET_MS) { + bucket = (bucket + 1) % WINDOW_BUCKETS + minima[bucket] = Long.MAX_VALUE + bucketStartMs = arrival + } + if (arrival - source < minima[bucket]) minima[bucket] = arrival - source + // Never before the end of the track's previous block. + val time = maxOf(source + offset(), endMs) + frames += chunk.frames + lastTick = chunk.deviceTickMs + endMs = time + chunk.durationMs + return time + } + + private fun sourceTime(chunk: MicChunk): Long = + if (tickBase >= 0) (chunk.deviceTickMs - tickBase) and 0xffffffffL else frames * 1000L / sampleRate + } + + private val lock = platform.createLock() + private val writer: MkaWriter + private val clocks = HashMap<Long, TrackClock>() + private var metaTrack = 0 + private var originMs = Long.MIN_VALUE + private var lastTimeMs = 0L + private var closed = false + + /** The recording stopped early: the file could not be written. */ + @Volatile + var failed = false + private set + + init { + val attachments = if (configJson.isNullOrEmpty()) emptyList() + else listOf(MkaAttachment(CONFIG_ATTACHMENT, "application/json", configJson.encodeToByteArray())) + writer = MkaWriter(output, null, startedAtMs, attachments) + } + + /** Length of the recording so far, in ms. */ + val durationMs: Long get() = lock.withLock { writer.durationMs } + + /** Appends [chunk] to its stream's track, adding the track on its first chunk. */ + fun write(chunk: MicChunk) { + if (chunk.frames <= 0) return + lock.withLock { + if (closed) return + try { + val clock = clockFor(chunk) ?: return + if (originMs == Long.MIN_VALUE) originMs = chunk.timeMs + val time = clock.stamp(chunk) - originMs + val meta = metadata(chunk) + if (meta != null && metaTrack > 0) writer.writeText(metaTrack, time, maxOf(1L, chunk.durationMs), meta) + val bytes = AudioSegmentation.pcm16ToLittleEndian(chunk.pcm, 0, chunk.frames * chunk.channels) + writer.writeAudio(clock.number, time, bytes, 0, bytes.size) + lastTimeMs = maxOf(lastTimeMs, time) + } catch (error: Throwable) { + fail(error) + } + } + } + + /** + * Records a JSON object as an event at the recording's current position (a capture starting + * over with another configuration, say), and has every stream's clock placed afresh by its + * next chunk: what follows an event is not continuous with what came before. + */ + fun event(json: String) { + lock.withLock { + if (closed) return + try { + for (clock in clocks.values) clock.reset() + if (ensureMetaTrack()) writer.writeText(metaTrack, maxOf(lastTimeMs, writer.durationMs), 1, json) + } catch (error: Throwable) { + fail(error) + } + } + } + + /** Completes the file. Returns the recording's length in ms. */ + fun finish(): Long { + lock.withLock { + if (!closed) { + closed = true + try { + writer.finish() + } catch (error: Throwable) { + failed = true + logWarn("finishing the recording failed: ${error.message}") + } + } + return writer.durationMs + } + } + + private fun fail(error: Throwable) { + failed = true + closed = true + logWarn("recording stopped: ${error.message}") + try { + writer.finish() + } catch (ignored: Throwable) { + // The file is whatever made it to disk. + } + } + + private fun ensureMetaTrack(): Boolean { + if (metaTrack == 0) metaTrack = writer.addTextTrack(META_TRACK) + return metaTrack > 0 + } + + private fun clockFor(chunk: MicChunk): TrackClock? { + val key = (chunk.stream.toLong() shl 48) or (chunk.channels.toLong() shl 32) or chunk.sampleRate.toLong() + clocks[key]?.let { return it } + // The frames track goes in with the first audio track, so it is there for that track's first chunk. + ensureMetaTrack() + // The mixed streams are what a player should pick; a temple's raw pair is for analysis. + val preferred = chunk.stream == MicStream.MONO || chunk.stream == MicStream.MIX + val number = writer.addAudioTrack(MicStream.trackName(chunk.stream), chunk.sampleRate, chunk.channels, preferred) + if (number <= 0) { + logWarn("recording has no room for another track; dropping ${MicStream.trackName(chunk.stream)} audio") + return null + } + return TrackClock(number, chunk.sampleRate).also { clocks[key] = it } + } + + /** The chunk's frame-metadata JSON, or null when it has nothing to say beyond its audio. */ + private fun metadata(chunk: MicChunk): String? { + if (chunk.stream == MicStream.MIX) return null + if (!chunk.hasDirection && chunk.sequence < 0 && chunk.deviceTickMs < 0 && !chunk.truncated && chunk.lostFramesBefore == 0) return null + val text = StringBuilder(96) + text.append("{\"s\":\"").append(MicStream.key(chunk.stream)).append("\",\"rx\":").append(chunk.timeMs - originMs) + if (chunk.sequence >= 0) text.append(",\"seq\":").append(chunk.sequence) + if (chunk.deviceTickMs >= 0) text.append(",\"tick\":").append(chunk.deviceTickMs) + if (chunk.hasDirection) text.append(",\"angle\":").append(chunk.angleDegrees).append(",\"ssr\":").append(chunk.ssr) + if (chunk.truncated) text.append(",\"trunc\":true") + if (chunk.lostFramesBefore > 0) text.append(",\"lost\":").append(chunk.lostFramesBefore) + return text.append('}').toString() + } +} + +/** + * Reads a recording back as the chunks its capture delivered: the "mic" track as [MicStream.MONO] + * chunks and the temple tracks as [MicStream.LEFT] / [MicStream.RIGHT] ones, each with the + * metadata its frame carried. The "mix" track is skipped: a replay derives the feed again. A + * Matroska file from elsewhere plays its first PCM16 track as the mono microphone. + * + * Throws IllegalArgumentException when the file has no audio a replay can use. + */ +class MicRecordingReader(input: SeekableInput) { + companion object { + const val SAMPLE_RATE = 16000 + /** Blocks longer than this many frames are handed out in pieces, like a live capture's chunks. */ + const val MAX_CHUNK_FRAMES = SAMPLE_RATE / 20 + } + + private class Meta(val timeMs: Long, val fields: Map<String, Any?>) + + private val reader = MkaReader(input) + private val streams = HashMap<Int, Int>() + private var metaTrack = -1 + private val pendingMeta = arrayOfNulls<Meta>(MicStream.COUNT) + private val queued = ArrayDeque<MicChunk>() + + val durationMs: Long get() = reader.durationMs + + /** The capture's "capture.json" attachment, or null. */ + val configJson: String? = + reader.attachments.firstOrNull { it.name == MicRecordingWriter.CONFIG_ATTACHMENT }?.data?.decodeToString() + + /** What the replay plays, for a status line: "left temple 2ch + right temple 2ch". */ + val description: String + + init { + try { + val audio = reader.tracks.filter { it.isPcm16 && it.channels in 1..8 } + for (track in audio) { + val stream = MicStream.named(track.name) + if (stream == MicStream.MONO || stream == MicStream.LEFT || stream == MicStream.RIGHT) streams[track.number] = stream + } + if (streams.isEmpty()) { + // Not one of ours: its first usable track stands in for the microphone. + val first = audio.firstOrNull { MicStream.named(it.name) != MicStream.MIX } ?: audio.firstOrNull() + require(first != null) { "no 16-bit PCM audio track" } + streams[first.number] = MicStream.MONO + } + for (number in streams.keys) { + val rate = reader.track(number)!!.sampleRate + require(rate == SAMPLE_RATE) { "unsupported sample rate ($rate Hz; replay needs ${SAMPLE_RATE / 1000} kHz)" } + } + metaTrack = reader.tracks.firstOrNull { + it.type == MkaWriter.TRACK_SUBTITLE && it.name == MicRecordingWriter.META_TRACK + }?.number ?: -1 + description = streams.entries.sortedBy { it.value }.joinToString(" + ") { (number, stream) -> + val channels = reader.track(number)!!.channels + MicStream.trackName(stream) + if (channels > 1 || stream != MicStream.MONO) " ${channels}ch" else "" + } + } catch (error: Throwable) { + reader.close() + throw error + } + } + + /** Continue from the first chunk at or after [timeMs]. */ + fun seekTo(timeMs: Long) { + queued.clear() + pendingMeta.fill(null) + reader.seekTo(timeMs) + } + + /** The next chunk in file order, or null at the end. */ + fun next(): MicChunk? { + while (queued.isEmpty()) { + val block = reader.next() ?: return null + if (block.track == metaTrack) { + noteMeta(block) + continue + } + val stream = streams[block.track] ?: continue + val track = reader.track(block.track) ?: continue + queueChunks(block, stream, track) + } + return queued.removeFirst() + } + + fun close() = reader.close() + + private fun noteMeta(block: MkaBlock) { + try { + val fields = Json.parseObject(block.data.decodeToString()) + val stream = MicStream.named(fields["s"].asString() ?: return) + if (stream >= 0) pendingMeta[stream] = Meta(block.timeMs, fields) + } catch (ignored: Throwable) { + // An event or a frame we can't read: the audio plays without it. + } + } + + private fun queueChunks(block: MkaBlock, stream: Int, track: MkaTrackInfo) { + val sourceChannels = track.channels + val totalFrames = block.data.size / (2 * sourceChannels) + if (totalFrames <= 0) return + // The mixed microphone is one channel however a foreign file stored it. + val channels = if (stream == MicStream.MONO) 1 else sourceChannels + val meta = pendingMeta[stream]?.takeIf { it.timeMs == block.timeMs } + pendingMeta[stream] = null + var start = 0 + while (start < totalFrames) { + val frames = minOf(MAX_CHUNK_FRAMES, totalFrames - start) + val pcm = ShortArray(frames * channels) + for (frame in 0 until frames) { + val at = (start + frame) * sourceChannels * 2 + if (channels == sourceChannels) { + for (channel in 0 until channels) { + pcm[frame * channels + channel] = AudioSegmentation.pcm16le(block.data[at + channel * 2], block.data[at + channel * 2 + 1]) + } + } else { + var sum = 0 + for (channel in 0 until sourceChannels) sum += AudioSegmentation.pcm16le(block.data[at + channel * 2], block.data[at + channel * 2 + 1]) + pcm[frame] = (sum / sourceChannels).toShort() + } + } + val chunk = MicChunk(stream, channels, track.sampleRate, pcm, frames, block.timeMs + start * 1000L / track.sampleRate) + if (meta != null && start == 0) applyMeta(chunk, meta.fields) + queued.addLast(chunk) + start += frames + } + } + + private fun applyMeta(chunk: MicChunk, fields: Map<String, Any?>) { + chunk.sequence = fields["seq"].asInt() ?: -1 + chunk.deviceTickMs = fields["tick"].asLong() ?: -1L + val angle = fields["angle"].asInt() + if (angle != null) { + chunk.hasDirection = true + chunk.angleDegrees = angle + chunk.ssr = fields["ssr"].asInt() ?: 0 + } + chunk.truncated = fields["trunc"].asBoolean() ?: false + chunk.lostFramesBefore = fields["lost"].asInt() ?: 0 + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoiceCaptureSession.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoiceCaptureSession.kt index 8ad954f0..57b6ee8e 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoiceCaptureSession.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoiceCaptureSession.kt @@ -35,6 +35,12 @@ interface VoiceCaptureHost { /** The speaker-embedding model at [modelPath] (may be shared/cached across sessions). */ fun speakerEmbedder(modelPath: String): SpeakerEmbedder + + /** The glasses' per-temple microphone channel, or null when there are no glasses that have one. */ + fun micArrayLink(): MicArrayLink? = null + + /** Opens a recording to replay; throws when it cannot be read. */ + fun openRecording(path: String): SeekableInput = openFileInput(path) } /** @@ -44,6 +50,12 @@ interface VoiceCaptureHost { * REPLACE-semantics partials, and speaker verification at session end. The session owns its * worker thread (started through the shared [startThread]); listener callbacks go through the * host's [CallbackDispatcher] so the adapter decides the delivery thread. + * + * Audio comes from one of four sources, chosen at start(): the glasses' stock mixed stream, the + * phone's microphone, the glasses' per-temple microphones ([setMicArray]), or a recording + * played back in real time ([setReplay]). Each delivers [MicChunk]s; a live capture's chunks go + * to the recorder ([setRecorder]) as they came, before any processing, which is what lets a + * replay of the recording run the same processing again. */ class VoiceCaptureSession( private val host: VoiceCaptureHost, @@ -95,6 +107,19 @@ class VoiceCaptureSession( const val DEFAULT_VERIFY_THRESHOLD = 0.8f /** Samples per G2 packet; the decode buffer the worker reuses. */ const val PACKET_SAMPLES = Lc3PacketFramer.SAMPLES_PER_PACKET + // Mic array upkeep: the temples stop streaming unless their lease is renewed, and a + // temple that goes quiet (a lost CONFIGURE, or it restarted) is configured again. + const val ARRAY_CHECK_MS = 1000L + const val ARRAY_RENEW_MS = MicArrayProtocol.LEASE_MS / 3 + const val ARRAY_TEMPLE_SILENT_MS = 5000L + const val ARRAY_WAITING_MS = 2000L + const val ARRAY_WAITING_STATUS = "Waiting for the glasses microphones..." + // A replay skips stretches of the recording with no audio longer than this + // (a recording made across a disconnect) instead of sitting through them. + const val REPLAY_MAX_GAP_MS = 2000L + const val REPLAY_POSITION_INTERVAL_MS = 1000L + const val REPLAY_WAIT_SLICE_MS = 20L + const val REPLAY_FINISHED_STATUS = "Replay finished." /** Joins transcript pieces, attaching leading punctuation to the previous text. */ @JvmStatic @@ -120,6 +145,8 @@ class VoiceCaptureSession( CLOUD, // decode locally, emit PCM for a cloud recognizer on the TS side } + private enum class Source { G2, PHONE, ARRAY, REPLAY } + private class AudioPacket(val data: ByteArray, arm: String?, val arrivalMs: Long) { val arm: String = arm ?: "?" } @@ -137,11 +164,33 @@ class VoiceCaptureSession( @Volatile private var workerThreadName: String? = null // Capture from the phone's own microphone instead of the G2 over BLE - // (preview-only mode). Latched into activePhoneMic at start() (under `lock`) + // (preview-only mode). Latched into activeSource at start() (under `lock`) // so a mid-session setter call can't switch pipelines underneath the worker. @Volatile private var usePhoneMic = false - private var activePhoneMic = false + // What the next capture reads (see start()); latched into activeSource and its + // companions under `lock`, like usePhoneMic. + @Volatile + private var micArrayConfigure: ByteArray? = null + @Volatile + private var replayPath: String? = null + @Volatile + private var replayStartMs = 0L + private var activeSource = Source.G2 + private var activeConfigure: ByteArray? = null + private var activeReplayPath: String? = null + private var activeReplayStartMs = 0L + // Replay controls, picked up by the worker between chunks. + @Volatile + private var replaySeekMs = -1L + @Volatile + private var replayPaused = false + @Volatile + private var recorder: MicRecordingWriter? = null + @Volatile + private var mixer: MicArrayMixer = SingleTempleMixer() + private var lastMissingPackets = 0L + private var arrayRateWarned = false private var mode = Mode.CLOUD @Volatile private var onboardModelKind = VoiceModelKind.MOONSHINE @@ -193,6 +242,47 @@ class VoiceCaptureSession( this.usePhoneMic = usePhoneMic } + /** + * Capture the next glasses session from the temples' own microphones instead of the stock + * mixed stream: [configureRecord] is the mic_control CONFIGURE record sent to both temples + * (it decides how many microphones each one streams). Null goes back to the mixed stream. + */ + fun setMicArray(configureRecord: ByteArray?) { + this.micArrayConfigure = if (MicArrayProtocol.isConfigure(configureRecord)) configureRecord else null + } + + /** How the temples' chunks become the mono feed that is transcribed. */ + fun setMicArrayMixer(mixer: MicArrayMixer) { + this.mixer = mixer + } + + /** + * Have the next capture play the recording at [path] from [startMs] in real time, in place + * of any microphone; null goes back to the microphones. A replay needs no glasses. + */ + fun setReplay(path: String?, startMs: Long) { + this.replayPath = path + this.replayStartMs = maxOf(0L, startMs) + } + + /** Continue the running replay from [positionMs] into its recording. */ + fun seekReplay(positionMs: Long) { + this.replaySeekMs = maxOf(0L, positionMs) + } + + /** Hold the running replay where it is, or let it go on. A new capture starts unpaused. */ + fun setReplayPaused(paused: Boolean) { + this.replayPaused = paused + } + + /** + * Where live captures' chunks are recorded as they arrive, or null. Takes effect at once, + * also mid-capture; the caller finishes the recording. A replay is never recorded. + */ + fun setRecorder(recorder: MicRecordingWriter?) { + this.recorder = recorder + } + fun setOnboardModelKind(kind: String?) { this.onboardModelKind = parseModelKind(kind) } @@ -258,13 +348,26 @@ class VoiceCaptureSession( emitStopped(captureId) return } - if (!usePhoneMic && !host.isG2SessionReady()) { + val path = replayPath + val configure = micArrayConfigure + val source = when { + path != null -> Source.REPLAY + usePhoneMic -> Source.PHONE + configure != null -> Source.ARRAY + else -> Source.G2 + } + if ((source == Source.G2 || source == Source.ARRAY) && !host.isG2SessionReady()) { emitStatus("Voice control needs an active G2 connection.") emitStopped(captureId) return } mode = parseMode(requestedMode) - activePhoneMic = usePhoneMic + activeSource = source + activeConfigure = configure + activeReplayPath = path + activeReplayStartMs = replayStartMs + replaySeekMs = -1L + replayPaused = false started = true audioStarted = false val finished = Latch(1, platform) @@ -281,21 +384,22 @@ class VoiceCaptureSession( */ fun isCapturing(): Boolean { var audioUp = false - var phoneMic = false + var source = Source.G2 lock.withLock { if (!started) { return false } audioUp = audioStarted - phoneMic = activePhoneMic + source = activeSource } if (!audioUp) { // The worker is still bringing the mic up; report it as running so // a concurrent request shares it instead of restarting it. return true } - if (phoneMic) { - // The phone mic has no session to lose the enable to; it runs until stop(). + if (source == Source.PHONE || source == Source.REPLAY) { + // Neither has a glasses session to lose the enable to: the phone mic runs + // until stop(), a replay until its recording ends. return true } return host.isG2AudioCaptureActive() @@ -317,7 +421,7 @@ class VoiceCaptureSession( finished = workerFinished workerName = workerThreadName } - stopG2Audio() + stopSource() audioQueue.wake() if (finished != null && (workerName == null || currentThreadName() != workerName)) { finished.await(1500) @@ -356,28 +460,62 @@ class VoiceCaptureSession( val verifying = verifySpeakerModelPath != null && verifyWearerEmbedding != null verifyBuffer = if (verifying) ShortArray(VERIFY_MAX_SAMPLES) else null verifyCount = 0 - if (activePhoneMic) { - val record = host.openPhoneMic() - if (record == null) { - emitStatus("Could not start the phone microphone.") - return + mixer.reset() + lastMissingPackets = 0 + val listening = if (currentMode == Mode.CLOUD) "Listening (cloud)..." else "Listening..." + when (activeSource) { + Source.PHONE -> { + val record = host.openPhoneMic() + if (record == null) { + emitStatus("Could not start the phone microphone.") + return + } + lock.withLock { audioStarted = true } + emitStatus(listening) + try { + processPhoneAudio(record) + } finally { + record.close() + } } - lock.withLock { audioStarted = true } - emitStatus(if (currentMode == Mode.CLOUD) "Listening (cloud)..." else "Listening...") - try { - processPhoneAudio(record) - } finally { - record.close() + Source.REPLAY -> { + val reader = try { + MicRecordingReader(host.openRecording(activeReplayPath!!)) + } catch (error: Throwable) { + emitStatus("Could not open the recording: " + error.message) + emitReplay("error", 0, 0) + return + } + try { + lock.withLock { audioStarted = true } + logInfo("replaying " + reader.description + ", " + formatFixed(reader.durationMs / 1000.0, 1) + " s") + emitStatus(listening) + processReplay(reader) + } finally { + reader.close() + } } - } else { - packetDecoder = host.createPacketDecoder() - if (!startG2Audio()) { - emitStatus("Could not start G2 microphone input.") - return + Source.ARRAY -> { + val link = host.micArrayLink() + val configure = activeConfigure + if (link == null || configure == null || !startArray(link, configure)) { + emitStatus("Could not start the glasses microphone array.") + return + } + lock.withLock { audioStarted = true } + emitStatus(listening) + processArray(link, configure, listening) + } + Source.G2 -> { + packetDecoder = host.createPacketDecoder() + if (!startG2Audio()) { + emitStatus("Could not start G2 microphone input.") + return + } + lock.withLock { audioStarted = true } + emitStatus(listening) + processG2Audio() } - lock.withLock { audioStarted = true } - emitStatus(if (currentMode == Mode.CLOUD) "Listening (cloud)..." else "Listening...") - processG2Audio() } // Verification result must precede the final transcript so the // TS bridge can suppress a non-wearer command before it is acted @@ -392,7 +530,7 @@ class VoiceCaptureSession( logError("Voice control failed", error) emitStatus("Voice control failed: " + error.message) } finally { - stopG2Audio() + stopSource() releaseRecognizer() releaseDecoder() lock.withLock { @@ -432,23 +570,214 @@ class VoiceCaptureSession( continue } decodedSamples += count.toLong() - // The beam filter drops packets whose firmware direction-of-arrival - // falls outside the listening wedge (isolating the aimed talker for - // every consumer, recognition included); the spectral noise - // suppressor then cleans what remains before it reaches the - // recognizer, cloud PCM, endpointing, or speaker verification. - val angleDegrees = currentDecoder.lastAngleDegrees - val ssr = currentDecoder.lastSsr - if (beamFilterEnabled && ssr > 0 && !withinBeam(angleDegrees)) { - emitFrameMeta(angleDegrees, ssr) - maybeEmitAudioStats(false) - continue - } - processPcmChunk(pcm, count, angleDegrees, ssr, currentDecoder.hasFrameMeta) + val chunk = MicChunk(MicStream.MONO, 1, SAMPLE_RATE, pcm, count, packet.arrivalMs) + chunk.hasDirection = currentDecoder.hasFrameMeta + chunk.angleDegrees = currentDecoder.lastAngleDegrees + chunk.ssr = currentDecoder.lastSsr + // Packets the decoder found missing ahead of this one: a gap in a recording. + val missing = currentDecoder.missingPackets + chunk.lostFramesBefore = ((missing - lastMissingPackets) * currentDecoder.maxSamplesPerPacket).toInt() + lastMissingPackets = missing + handleMonoChunk(chunk) maybeEmitAudioStats(false) } } + /** + * A mixed-microphone chunk from any source: recorded as it came, then on to processing + * unless the beam filter drops it. The filter drops chunks whose firmware + * direction-of-arrival falls outside the listening wedge (isolating the aimed talker for + * every consumer, recognition included); the spectral noise suppressor then cleans what + * remains before it reaches the recognizer, cloud PCM, endpointing, or speaker verification. + */ + private fun handleMonoChunk(chunk: MicChunk) { + recordChunk(chunk) + if (beamFilterEnabled && chunk.ssr > 0 && !withinBeam(chunk.angleDegrees)) { + emitFrameMeta(chunk.angleDegrees, chunk.ssr) + return + } + processPcmChunk(chunk.pcm, chunk.frames, chunk.angleDegrees, chunk.ssr, chunk.hasDirection) + } + + /** + * A chunk of one temple's own microphones: recorded as it came, then mixed into the mono + * feed, which is recorded too (as what was transcribed) and processed like a mixed chunk. + * The beam filter does not apply: a temple's bearing is along its own pair, not the + * head-frame direction the filter is aimed in. + */ + private fun handleArrayChunk(chunk: MicChunk) { + recordChunk(chunk) + val feed = mixer.mix(chunk) ?: return + recordChunk(feed) + if (feed.sampleRate != SAMPLE_RATE || feed.channels != 1) { + if (!arrayRateWarned) logWarn("mic array feed is ${feed.channels}ch ${feed.sampleRate} Hz; recognition needs mono $SAMPLE_RATE Hz") + arrayRateWarned = true + return + } + processPcmChunk(feed.pcm, feed.frames, feed.angleDegrees, feed.ssr, feed.hasDirection) + } + + private fun recordChunk(chunk: MicChunk) { + if (activeSource != Source.REPLAY) recorder?.write(chunk) + } + + private fun handleChunk(chunk: MicChunk) { + if (chunk.stream == MicStream.MONO) handleMonoChunk(chunk) else handleArrayChunk(chunk) + } + + private fun startArray(link: MicArrayLink, configure: ByteArray): Boolean { + resetAudioStats() + audioQueue.clear() + if (!startArrayForwarding(link)) return false + return link.sendControl(configure, "configure", true, true) + } + + private fun startArrayForwarding(link: MicArrayLink): Boolean = + link.startForwarding(object : FaceclawAudioPacketListener { + override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) { + queueAudioPacket(data, arm, arrivalMs) + } + }) + + /** + * Mic array capture loop: each temple's 'SM' frames become chunks of that temple's stream. + * Between frames it keeps the streams alive: renews the temples' lease, configures a temple + * again when it has gone quiet, and takes forwarding back when the glasses session dropped it. + */ + private fun processArray(link: MicArrayLink, configure: ByteArray, listening: String) { + val startedAt = platform.elapsedRealtimeMs() + var checkedAt = startedAt + var renewedAt = startedAt + // Left, right. + val armedAt = longArrayOf(startedAt, startedAt) + val frameAt = longArrayOf(startedAt, startedAt) + var waiting = false + while (started) { + val packet = takeAudioPacket() + val now = platform.elapsedRealtimeMs() + if (packet != null) { + val side = if (packet.arm == "L") 0 else if (packet.arm == "R") 1 else -1 + val frame = if (side >= 0) MicArrayProtocol.decodeFrame(packet.data) else null + val pcm = if (frame != null) MicArrayProtocol.pcm16(packet.data, frame) else null + // Anything else is a stock-format packet from before arming took effect. + if (frame != null && pcm != null) { + frameAt[side] = now + val frames = pcm.size / frame.channels + decodedSamples += frames.toLong() + val stream = if (side == 0) MicStream.LEFT else MicStream.RIGHT + val chunk = MicChunk(stream, frame.channels, frame.sampleRate, pcm, frames, packet.arrivalMs) + chunk.hasDirection = frame.angleDegrees != 0 || frame.ssr != 0 + chunk.angleDegrees = frame.angleDegrees + chunk.ssr = frame.ssr + chunk.sequence = frame.sequence + chunk.deviceTickMs = frame.deviceTickMs + chunk.truncated = frame.truncated + handleArrayChunk(chunk) + } + } + if (now - checkedAt >= ARRAY_CHECK_MS) { + checkedAt = now + if (!link.isForwarding()) { + if (host.isG2SessionReady() && startArrayForwarding(link) && link.sendControl(configure, "configure", true, true)) { + logInfo("mic array forwarding re-established") + armedAt.fill(now) + renewedAt = now + } + } else { + if (now - renewedAt >= ARRAY_RENEW_MS) { + link.sendControl(MicArrayProtocol.control(MicArrayProtocol.OP_RENEW), "renew", true, true) + renewedAt = now + } + for (side in 0..1) { + if (now - maxOf(frameAt[side], armedAt[side]) < ARRAY_TEMPLE_SILENT_MS) continue + logWarn("mic array: " + (if (side == 0) "left" else "right") + " temple silent; configuring it again") + link.sendControl(configure, "configure-" + (if (side == 0) "left" else "right"), side == 1, side == 0) + armedAt[side] = now + } + } + val silent = now - maxOf(frameAt[0], frameAt[1]) >= ARRAY_WAITING_MS + if (silent != waiting) { + waiting = silent + emitStatus(if (silent) ARRAY_WAITING_STATUS else listening) + } + } + maybeEmitAudioStats(false) + } + } + + /** + * Replay loop: hands the recording's chunks to the pipeline at the pace they were captured, + * so recognition (its partials, a cloud provider's stream) sees what a live capture would. + * Jumps and pauses asked for from outside take effect between chunks. + */ + private fun processReplay(reader: MicRecordingReader) { + val duration = reader.durationMs + var position = minOf(activeReplayStartMs, duration) + reader.seekTo(position) + emitReplay("start", position, duration) + var reportedAt = position + // Wall-clock time at which file time `anchorFile` plays; unset after a jump, pause or gap. + var anchorWall = -1L + var anchorFile = 0L + var pending: MicChunk? = null + while (started) { + val seek = replaySeekMs + if (seek >= 0) { + replaySeekMs = -1L + // What played so far is a finished utterance; the rest starts clean. + finishReplayedUtterance() + position = minOf(seek, duration) + reader.seekTo(position) + pending = null + anchorWall = -1L + reportedAt = position + emitReplay("seek", position, duration) + } + if (replayPaused) { + anchorWall = -1L + sleepMs(REPLAY_WAIT_SLICE_MS) + continue + } + val chunk = pending ?: reader.next() + if (chunk == null) { + emitReplay("end", duration, duration) + emitStatus(REPLAY_FINISHED_STATUS) + return + } + pending = chunk + val now = platform.elapsedRealtimeMs() + if (anchorWall < 0 || chunk.timeMs - position > REPLAY_MAX_GAP_MS) { + anchorWall = now + anchorFile = chunk.timeMs + } + val wait = anchorWall + (chunk.timeMs - anchorFile) - now + if (wait > 0) { + // Short slices, so a stop, jump or pause is noticed promptly. + sleepMs(minOf(wait, REPLAY_WAIT_SLICE_MS)) + continue + } + pending = null + position = chunk.timeMs + decodedSamples += chunk.frames.toLong() + handleChunk(chunk) + if (position - reportedAt >= REPLAY_POSITION_INTERVAL_MS) { + reportedAt = position + emitReplay("position", position, duration) + } + } + } + + /** Ends the utterance a replay was in the middle of when it jumped, so the audio on both sides isn't joined. */ + private fun finishReplayedUtterance() { + mixer.reset() + endpointDetector.reset() + suppressor?.reset() + if (mode != Mode.ONBOARD) return + decodeTranscript(true) + resetTranscriptState() + lastTranscript = "" + } + /** * Per-chunk processing shared by the G2 and phone-mic paths, downstream of * decode and the beam filter. hasFrameMeta is false for the phone mic, @@ -495,7 +824,7 @@ class VoiceCaptureSession( continue } decodedSamples += read.toLong() - processPcmChunk(pcm, read, 0, 0, false) + handleMonoChunk(MicChunk(MicStream.MONO, 1, SAMPLE_RATE, pcm, read, platform.elapsedRealtimeMs())) } } @@ -681,6 +1010,11 @@ class VoiceCaptureSession( host.dispatcher.post { currentListener.onFrameMeta(angleDegrees, ssr) } } + private fun emitReplay(event: String, positionMs: Long, durationMs: Long) { + val currentListener = listener ?: return + host.dispatcher.post { currentListener.onReplay(event, positionMs, durationMs) } + } + private fun emitSpeechEnd() { val currentListener = listener ?: return host.dispatcher.post { currentListener.onSpeechEnd() } @@ -691,8 +1025,21 @@ class VoiceCaptureSession( host.dispatcher.post { currentListener.onStopped(captureId) } } - private fun stopG2Audio() { - host.g2AudioSource()?.stop() + /** + * Release what the running capture's source holds on the glasses. Runs on stop() and again + * as the worker winds down (it may have started the source after stop() looked), so + * everything here is safe to repeat. + */ + private fun stopSource() { + val source = lock.withLock { activeSource } + when (source) { + Source.G2 -> host.g2AudioSource()?.stop() + Source.ARRAY -> host.micArrayLink()?.let { link -> + link.sendControl(MicArrayProtocol.control(MicArrayProtocol.OP_STOP), "stop", true, true) + link.stopForwarding() + } + Source.PHONE, Source.REPLAY -> {} + } maybeEmitAudioStats(true) } @@ -711,7 +1058,7 @@ class VoiceCaptureSession( if (!started || data == null) { return } - if ("L" != arm) { + if ("L" != arm && activeSource != Source.ARRAY) { wrongArmPackets++ } audioQueue.put(AudioPacket(data, arm, arrivalMs)) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoicePorts.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoicePorts.kt index 2c0bdd9a..fe2dba95 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoicePorts.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/VoicePorts.kt @@ -67,6 +67,24 @@ interface PacketAudioSource { fun isActive(): Boolean } +/** + * The custom firmware's mic_control channel on a glasses session: each temple streams its own + * microphones as 'SM' frames (see [MicArrayProtocol]) on the characteristic the stock mixed + * stream uses, while it holds a configuration that the phone keeps renewing. + */ +interface MicArrayLink { + /** Deliver the audio characteristic's packets to [listener]; false when no session is up. */ + fun startForwarding(listener: FaceclawAudioPacketListener): Boolean + + fun stopForwarding() + + /** Whether forwarding still holds: it lives in the glasses session, which can drop it. */ + fun isForwarding(): Boolean + + /** Queue a control record for the chosen temples; false when nothing was queued. */ + fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean +} + /** The phone's own microphone delivering 16 kHz mono PCM16 in blocking reads. */ interface PcmAudioSource { /** Blocking read into [pcm]; returns samples read, 0 to retry, or a negative error. */ diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt index 3dd17667..f7c10d4f 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt @@ -3,6 +3,8 @@ package com.faceclaw.app /** * Receives IMU (accelerometer) readings decoded from the glasses' sys-event stream. Registered on * FaceclawBleCommunicator via addImuListener; callbacks are delivered on the Android main thread. + * x, y, z are in g, in the canonical frame of every glasses model (+x forward, +y left, +z up; + * see ImuOrientation). */ interface FaceclawImuListener { fun onImuData(x: Double, y: Double, z: Double, eventSource: Int): Unit diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawKeyboardEventListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawKeyboardEventListener.kt new file mode 100644 index 00000000..c5491179 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawKeyboardEventListener.kt @@ -0,0 +1,10 @@ +package com.faceclaw.app + +interface FaceclawKeyboardEventListener { + /** + * One CFW keyboard-bridge record (settings field 131) from the glasses: the raw + * ['K','B',ver,kind,...] bytes, possibly one fragment of a longer record. Decoded and + * reassembled on the TS side (app/g2/keyboard/protocol.ts). Delivered in arrival order. + */ + fun onKeyboardEvent(body: ByteArray?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSshListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSshListener.kt new file mode 100644 index 00000000..f513f3bb --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSshListener.kt @@ -0,0 +1,9 @@ +package com.faceclaw.app + +/** + * Events from FaceclawSshChannel, delivered on the thread that opened the + * channel. See native/ssh/faceclawssh.go (Listener) for the kinds and payloads. + */ +interface FaceclawSshListener { + fun onEvent(kind: String?, payload: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt index e3b4fd75..80736519 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt @@ -41,4 +41,13 @@ interface FaceclawVoiceControllerListener { * speaker model could not be loaded. */ fun onSpeakerVerified(isWearer: Boolean, similarity: Float): Unit + + /** + * Progress of a capture that replays a recording (see VoiceCaptureSession.setReplay): + * "start" once the file is open, "position" about once per second of it played, "seek" after + * a jump took effect (the transcript of what played before it has been emitted as final), + * "end" when the file ran out, and "error" when it could not be opened (the status says + * why). Positions are milliseconds into the file. + */ + fun onReplay(event: String?, positionMs: Long, durationMs: Long): Unit } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt index 16fa3610..9bc4f045 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt @@ -12,6 +12,12 @@ import kotlin.jvm.JvmStatic */ class BleProtocol { companion object { + /** The service holding [WRITE_CHAR_UUID] and [NOTIFY_CHAR_UUID]. */ + const val PROTOCOL_SERVICE_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5450" + + /** The service holding [RENDER_NOTIFY_UUID]. */ + const val RENDER_SERVICE_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e6450" + const val WRITE_CHAR_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5401" const val NOTIFY_CHAR_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5402" @@ -119,6 +125,19 @@ class BleProtocol { */ const val FACECLAW_ALS_REPORT_FIELD: Int = 105 + /** + * CFW keyboard bridge (Faceclaw/39): the master temple hosts one BLE HID keyboard. The + * host writes ['K','B',ver,op,request,epoch,...] controls as settings field 130; results, + * pairing prompts and the keyboard's raw input reports come back as commandId=3 pushes + * carrying field 131 (['K','B',ver,kind,...], decoded in app/g2/keyboard/protocol.ts). + */ + const val FACECLAW_KEYBOARD_CONTROL_FIELD: Int = 130 + + const val FACECLAW_KEYBOARD_EVENT_FIELD: Int = 131 + + /** Fixed header of a field-131 record; fragments follow it. */ + const val FACECLAW_KEYBOARD_EVENT_HEADER: Int = 24 + const val FACECLAW_WAKE_OP_ACQUIRE: Int = 1 const val FACECLAW_WAKE_OP_RELEASE: Int = 2 @@ -497,6 +516,70 @@ class BleProtocol { ) } + // Field numbers from g2_setting.proto (package g2.g2_setting; see g2-kit-unofficial + // ble/gen/g2_setting_pb.ts), named after the schema fields. + + /** G2SettingPackage.commandId */ + private const val G2_SETTING_PACKAGE_COMMAND_ID: Int = 1 + + /** G2SettingPackage.magicRandom */ + private const val G2_SETTING_PACKAGE_MAGIC_RANDOM: Int = 2 + + /** G2SettingPackage.deviceReceiveInfoFromApp */ + private const val G2_SETTING_PACKAGE_DEVICE_RECEIVE_INFO_FROM_APP: Int = 3 + + /** G2SettingPackage.deviceReceiveRequestFromApp (the settings-read reply) */ + private const val G2_SETTING_PACKAGE_DEVICE_RECEIVE_REQUEST_FROM_APP: Int = 4 + + /** g2_settingCommandId.DeviceReceiveInfo */ + private const val G2_SETTING_COMMAND_DEVICE_RECEIVE_INFO: Int = 1 + + /** DeviceReceiveInfoFromAPP.deviceReceiveHeadUpSetting */ + private const val DEVICE_RECEIVE_INFO_HEAD_UP_SETTING: Int = 4 + + /** DeviceReceiveRequestFromAPP.headUpSwitchRestored */ + private const val DEVICE_RECEIVE_REQUEST_HEAD_UP_SWITCH_RESTORED: Int = 7 + + /** DeviceReceiveRequestFromAPP.headUpAngleRestored */ + private const val DEVICE_RECEIVE_REQUEST_HEAD_UP_ANGLE_RESTORED: Int = 8 + + /** DeviceReceiveRequestFromAPP.headUpAngleCalibrationRestored */ + private const val DEVICE_RECEIVE_REQUEST_HEAD_UP_CALIBRATION_RESTORED: Int = 9 + + /** + * One head-up (look-up wake) setting: DeviceReceiveInfoFromAPP.deviceReceiveHeadUpSetting, + * a DeviceReceive_Head_UP_Setting carrying one of headUpSwitch, headUpAngle, + * headUpCalibrationSwitch or headUpCalibration ([field] is one of the HEAD_UP_FIELD_*). + * The firmware treats that submessage as a oneof: setting_handle_head_up_setting + * (2.3.0.24, 0x46c39a) receives a single {field, value} and applies it, so the switch and + * the angle go out as separate packages. headUpCalibration is not stored from the + * phone; it is the pitch the glasses measured on their own calibration screen. The + * effective trigger pitch is angle + calibration, clamped to -90..90 and accepted only in + * 1..90 (SVC_Settings_HeadUpConfig 0x470580, DRV_IMUHUDParameterConfig). The angle is a + * uint32 on the wire but used as a signed int, so a negative offset is sent as its + * 32-bit two's complement. + */ + @JvmStatic + fun buildSetHeadUp(magic: Int, field: Int, value: Int): ByteArray { + var headUp: ByteArray = concat(CollectionUtils.listOf(encodeKey(field, 0), encodeUint32(value))) + return concat( + CollectionUtils.listOf( + encodeVarintField(G2_SETTING_PACKAGE_COMMAND_ID, G2_SETTING_COMMAND_DEVICE_RECEIVE_INFO), + encodeVarintField(G2_SETTING_PACKAGE_MAGIC_RANDOM, magic), + encodeMessageField( + G2_SETTING_PACKAGE_DEVICE_RECEIVE_INFO_FROM_APP, + encodeMessageField(DEVICE_RECEIVE_INFO_HEAD_UP_SETTING, headUp), + ), + ) + ) + } + + /** DeviceReceive_Head_UP_Setting.headUpSwitch */ + const val HEAD_UP_FIELD_SWITCH: Int = 1 + + /** DeviceReceive_Head_UP_Setting.headUpAngle */ + const val HEAD_UP_FIELD_ANGLE: Int = 2 + /** Enable the firmware's wear detector (DeviceReceiveInfoFromAPP field 5). */ @JvmStatic fun buildSetWearDetection(magic: Int, enabled: Boolean): ByteArray { @@ -724,6 +807,42 @@ class BleProtocol { return body } + /** + * Wrap an already-encoded ['K','B',ver,op,...] keyboard control as a settings write + * carrying CFW field 130. Magic zero, like the other private controls: the answer is a + * field-131 record naming the control's request id, not the settings ACK. + */ + @JvmStatic + fun buildFaceclawKeyboardControl(record: ByteArray): ByteArray { + return concat( + CollectionUtils.listOf( + encodeVarintField(1, 1), + encodeVarintField(2, 0), + encodeBytesField(FACECLAW_KEYBOARD_CONTROL_FIELD, record), + ) + ) + } + + /** + * Extract a CFW keyboard record (field 131) from a settings frame, or null when the frame + * carries none. Only the ['K','B'] prelude and the fixed header's length are checked + * here; the TS side validates the version and decodes the rest. + */ + @JvmStatic + fun parseFaceclawKeyboardEvent(pb: ByteArray?): ByteArray? { + if (pb == null) { + return null + } + val body = readFieldBytes(stripTrailingCrc(pb), FACECLAW_KEYBOARD_EVENT_FIELD) + if ( + body == null || body.size < FACECLAW_KEYBOARD_EVENT_HEADER || + body[0].toInt() != 75 || body[1].toInt() != 66 + ) { + return null + } + return body + } + /** * Return the raw CFW ambient-light report from a sid-0x09 frame, or null when this is an * ordinary settings frame. Only the 65.toByte(),76.toByte(),version prelude is validated @@ -905,6 +1024,26 @@ class BleProtocol { return BatterySnapshot(battery, charging, silentMode) } + /** + * The head-up settings in a settings READ ack: deviceReceiveRequestFromApp (field 4) + * headUpSwitchRestored=7, headUpAngleRestored=8, headUpAngleCalibrationRestored=9. + * Returns null when the ack has no request submessage. Absent fields (proto3 drops + * zeros) read as 0; angle and calibration are signed ints carried as uint32. + */ + @JvmStatic + fun parseSettingsHeadUp(pb: ByteArray): IntArray? { + var root: ByteArray = stripTrailingCrc(pb) + var request: ByteArray? = readFieldBytes(root, G2_SETTING_PACKAGE_DEVICE_RECEIVE_REQUEST_FROM_APP) + if ((request == null)) { + return null + } + return intArrayOf( + readVarintFieldValue(request, DEVICE_RECEIVE_REQUEST_HEAD_UP_SWITCH_RESTORED, 0), + readVarintFieldValue(request, DEVICE_RECEIVE_REQUEST_HEAD_UP_ANGLE_RESTORED, 0), + readVarintFieldValue(request, DEVICE_RECEIVE_REQUEST_HEAD_UP_CALIBRATION_RESTORED, 0), + ) + } + /** GlassesCaseDataPackage{commandId=CASE_INFO, magicRandom, caseInfo={}}, as the Even app sends it. */ @JvmStatic fun buildCaseInfoQuery(magic: Int): ByteArray { @@ -1089,6 +1228,18 @@ class BleProtocol { return out.toByteArray() } + /** A varint of the value's 32-bit two's complement (encodeVarint stops short on negatives). */ + private fun encodeUint32(value: Int): ByteArray { + var out: ByteSink = ByteSink() + var v: Long = (value.toLong() and 0xffffffffL) + while ((v >= 0x80L)) { + out.write((((v and 0x7fL).toInt()) or 0x80)) + v = (v ushr 7) + } + out.write(v.toInt()) + return out.toByteArray() + } + private fun crcBytesLe(data: ByteArray): ByteArray { var crc: Int = 0xffff for (b in data) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt index 485565c9..7e59ac0a 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt @@ -188,6 +188,22 @@ class MessageBuilder { ) } + /** One head-up setting field (see BleProtocol.buildSetHeadUp). */ + fun setHeadUp(field: Int, value: Int): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "head-up-control", + ("head-up field " + field + "=" + value), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildSetHeadUp(magic, field, value), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + fun createLayout(): OutboundMessage { var magic: Int = magicPool.allocate() return OutboundMessage( @@ -321,6 +337,24 @@ class MessageBuilder { ) } + /** + * CFW keyboard control (settings field 130). Always the right arm: only the master temple + * acts on it, and a copy sent down each arm would run the control twice. + */ + fun faceclawKeyboardControl(record: ByteArray, label: String): OutboundMessage { + return OutboundMessage( + "keyboard-control", + "keyboard control $label", + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + 0, + BleProtocol.buildFaceclawKeyboardControl(record), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + fun faceclawWearQuery(leftArm: Boolean): OutboundMessage { return OutboundMessage( "wear-query-control", diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt index c9d4e670..e1d05e89 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt @@ -20,11 +20,18 @@ class CachedResource @JvmOverloads constructor(bytes: ByteArray, val owner: Stri */ class ResourceCacheState { companion object { - /** Mirrors g2flash/patches/resource_cache.h. */ - const val CACHE_SIZE = 196608 + /** + * Mirrors g2flash/patches/resource_cache.h: the id table, then 320 KiB shared by the + * resources and their block headers. Firmware before Faceclaw/40 had 192 KiB in all, + * so this must not grow past what REQUIRED_FACECLAW_FIRMWARE_VERSION guarantees. + * RayNeo iO sessions use the same budget: that firmware's own copy of the header + * (faceclaw-rayneo-cfw patches/fc/resource_cache.h) needs at least this much before + * it reports the required revision. + */ const val RESOURCE_COUNT = 512 const val TABLE_BYTES = RESOURCE_COUNT * 4 - const val ARENA_BYTES = CACHE_SIZE - TABLE_BYTES + const val ARENA_BYTES = 320 * 1024 + const val CACHE_SIZE = TABLE_BYTES + ARENA_BYTES const val MAX_RESOURCE_SIZE = 65536 const val BLOCK_HEADER_BYTES = 16 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt index badd9509..88cf6d87 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt @@ -506,6 +506,9 @@ internal fun GlassesSessionCore.scheduleReconnectLocked(sessionWasReady: Boolean internal fun GlassesSessionCore.resetSessionStateLocked() { ringBattery = -1 ringCharging = -1 + headUpSwitch = -1 + headUpReadAtMs = 0L + headUpVerifyPending = false directRingBattery = -1 directRingCharging = -1 sessionReady = false @@ -583,6 +586,22 @@ internal fun GlassesSessionCore.emitMicStatus(body: ByteArray, address: String) } } +internal fun GlassesSessionCore.emitKeyboardEvent(body: ByteArray) { + if (keyboardEventListeners.isEmpty()) { + return + } + val copy = body.copyOf() + host.postToMain { + for (keyboardListener in keyboardEventListeners) { + try { + keyboardListener.onKeyboardEvent(copy) + } catch (t: Throwable) { + logWarn("keyboard event listener failed", t) + } + } + } +} + internal fun GlassesSessionCore.emitAudioMonitorPacket(data: ByteArray, arm: String, arrivalMs: Long) { if (audioMonitorListeners.isEmpty()) { return @@ -634,14 +653,16 @@ internal fun GlassesSessionCore.finishFrame(frameId: Int, outcome: String) { } } +/** [x], [y], [z] as the hardware reports them; listeners get them in [ImuOrientation]'s frame. */ internal fun GlassesSessionCore.emitImuData(x: Double, y: Double, z: Double, eventSource: Int) { if (imuListeners.isEmpty()) { return } + val (cx, cy, cz) = ImuOrientation.toCanonical(glassesType, x, y, z) host.postToMain { for (imuListener in imuListeners) { try { - imuListener.onImuData(x, y, z, eventSource) + imuListener.onImuData(cx, cy, cz, eventSource) } catch (t: Throwable) { logWarn("listener onImuData failed", t) } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt index f64886f8..9dab554f 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt @@ -131,6 +131,7 @@ class GlassesSessionCore( internal val compassSubscriptions = CopyOnWriteList<CompassSubscription>(platform) internal val ambientLightListeners = CopyOnWriteList<FaceclawAmbientLightListener>(platform) internal val micStatusListeners = CopyOnWriteList<FaceclawMicStatusListener>(platform) + internal val keyboardEventListeners = CopyOnWriteList<FaceclawKeyboardEventListener>(platform) internal val audioMonitorListeners = CopyOnWriteList<FaceclawAudioPacketListener>(platform) @Volatile internal var running = false @Volatile internal var userDisconnectRequested = false @@ -357,6 +358,26 @@ class GlassesSessionCore( internal var lastGlassesRingInputAtMs = 0L internal var lastGlassesRingInputLabel = "" internal var lastGlassesRingBatteryAtMs = 0L + // Head-up (look-up wake) state from settings reads: -1 switch = not read yet. + internal var headUpSwitch = -1 + internal var headUpAngle = 0 + internal var headUpCalibration = 0 + internal var headUpReadAtMs = 0L + // The trigger pitch TS asked for (degrees, 1..90), or -1 to leave the glasses' + // own setting alone. Reapplied after every read that disagrees with it. + internal var desiredHeadUpTrigger = -1 + // The glasses' own switch and angle from before Faceclaw first changed them + // (switch -1 = unknown): captured before the first write, or handed back by TS, + // which persists them across app restarts. Written back while + // headUpRestorePending (TS stopped managing the trigger), then forgotten once a + // read confirms them. + internal var headUpOriginalSwitch = -1 + internal var headUpOriginalAngle = 0 + internal var headUpRestorePending = false + // Set after queueing a correction so the next settings read comes right away to + // confirm it; cleared by that read, so a firmware that refuses the value is + // retried on the normal poll cadence, not in a loop. + internal var headUpVerifyPending = false internal var ringConnectedChangedAtMs = 0L internal var ringConnectCount = 0 internal var lastDirectRingError = "" @@ -486,7 +507,7 @@ class GlassesSessionCore( // Preview/screenshot/recording callers render those explicitly on demand. internal val compositor = SurfaceCompositor(false) - // Phone-side model of the CFW's 192 KiB resource cache. + // Phone-side model of the CFW's resource cache (ResourceCacheState.CACHE_SIZE). // Reset whenever the image pipeline / EvenHub session is torn down: the // firmware frees the cache with the fb lease, and after any resync the // cheap safe assumption is an empty cache (glyphs re-upload lazily). @@ -1015,6 +1036,61 @@ class GlassesSessionCore( interruptibleSleep.interrupt() } + /** + * Set the pitch (degrees, 1..90) at which looking up wakes the display, or 0 to stop + * managing it. Kept for the life of this object and reapplied on every connection: the + * next settings read supplies the glasses' calibration, then the switch and angle are + * written (see reconcileHeadUpLocked) and a follow-up read confirms them. + * + * [originalSwitch] (0/1, or -1 when unknown) and [originalAngle] are the glasses' own + * values from before Faceclaw changed them, as TS saved them from getHeadUpStatus; they + * take precedence over what this object captured. With degrees 0 they are written back. + */ + fun setHeadUpTrigger(degrees: Int, originalSwitch: Int, originalAngle: Int) { + monitor.withLock { + if (originalSwitch in 0..1) { + headUpOriginalSwitch = originalSwitch + headUpOriginalAngle = originalAngle + } + // A change of mind drops writes still queued for the previous value. + clearMessagesOfKindLocked("head-up-control") + headUpVerifyPending = false + desiredHeadUpTrigger = if (degrees in 1..90) degrees else -1 + headUpRestorePending = desiredHeadUpTrigger < 0 && headUpOriginalSwitch >= 0 + logLine(when { + desiredHeadUpTrigger > 0 -> "head-up trigger requested: ${desiredHeadUpTrigger}°" + headUpRestorePending -> "head-up trigger: restore the glasses' own " + + "(switch=$headUpOriginalSwitch angle=$headUpOriginalAngle)" + else -> "head-up trigger: left to the glasses" + }) + if (!running || !sessionReady) return + if (desiredHeadUpTrigger <= 0 && !headUpRestorePending) return + if (headUpSwitch >= 0) { + applyHeadUpLocked(verifying = false) + } else { + // Calibration unknown: read the settings first; the ack applies the trigger. + lastBatteryRefreshAtMs = 0L + } + } + interruptibleSleep.interrupt() + } + + /** Head-up settings as last read, as JSON (switch -1 = not read on this connection). */ + fun getHeadUpStatus(): String { + monitor.withLock { + val status = linkedMapOf<String, Any?>() + status["switch"] = headUpSwitch + status["angle"] = headUpAngle + status["calibration"] = headUpCalibration + status["desired"] = desiredHeadUpTrigger + status["originalSwitch"] = headUpOriginalSwitch + status["originalAngle"] = headUpOriginalAngle + status["restorePending"] = headUpRestorePending + status["readAgeMs"] = if (headUpReadAtMs == 0L) -1L else maxOf(0L, now() - headUpReadAtMs) + return try { Json.write(status) } catch (e: Exception) { "{}" } + } + } + // --------------------------------------------------------------------- // Bandwidth benchmark @@ -1388,6 +1464,39 @@ class GlassesSessionCore( return true } + fun addKeyboardEventListener(listener: FaceclawKeyboardEventListener?) { + if (listener != null) { + keyboardEventListeners.add(listener) + } + } + + fun removeKeyboardEventListener(listener: FaceclawKeyboardEventListener?) { + if (listener != null) { + keyboardEventListeners.remove(listener) + } + } + + /** + * Queue a CFW keyboard-bridge control (['K','B',ver,op,request,epoch,...]) as a settings + * field-130 write to the right temple. Fire-and-forget: the firmware answers with field-131 + * records, which arrive through addKeyboardEventListener. Returns false when nothing was + * queued (not a G2, no session, or a malformed record). + */ + fun sendFaceclawKeyboardControl(record: ByteArray?, label: String): Boolean { + if (!isG2 || record == null || record.size < 8) { + return false + } + monitor.withLock { + if (!running || !sessionReady) { + logLine("skip keyboard control ($label); session not ready") + return false + } + pendingMessages.addLast(messageBuilder.faceclawKeyboardControl(record, label)) + } + interruptibleSleep.interrupt() + return true + } + /** * Forward render-characteristic audio packets to the listener WITHOUT * sending the stock EvenHub audio-control enable. Used for the CFW @@ -1892,8 +2001,21 @@ class GlassesSessionCore( tap(data.copyOf()) return } - logDebug("onNotification: address=" + address + " characteristicUuid=" + characteristicUuid + " data.length=" + data.size) val frame = BleProtocol.parseFrame(data) + if (frame.ok && frame.sid == BleProtocol.SID_UI_SETTING) { + // CFW keyboard-bridge records (field 131) come from whichever temple hosts the + // keyboard. They are their own stream: neither an ACK nor glasses input, and the + // once-a-second status record among them must not count as user activity (which + // would hold off battery polling). A touchpad sends a hundred of them a second + // while a finger is on it, so they are not logged one by one either. + val keyboardEvent = BleProtocol.parseFaceclawKeyboardEvent(frame.pb) + if (keyboardEvent != null) { + monitor.withLock { lastIncomingAtMs = now() } + emitKeyboardEvent(keyboardEvent) + return + } + } + logDebug("onNotification: address=" + address + " characteristicUuid=" + characteristicUuid + " data.length=" + data.size) val decodedWearState = BleProtocol.parseWearState(frame) val compassEvent = if (address.equals(rightAddress, ignoreCase = true)) BleProtocol.parseCompassEvent(frame) @@ -2061,11 +2183,10 @@ class GlassesSessionCore( } else if (decoded != null && "sys-event" == decoded.kind && decoded.eventType == BleProtocol.EVENT_HEAD_DOWN) { - // Faceclaw/38 also forwards the head coming back down. Nothing - // uses it yet; as an unknown input it would count as activity - // and keep the screen awake, so drop it here. - logLine("head-down forwarded by CFW (ignored)") - decoded = null + // Faceclaw/38 also forwards the head coming back down. TS uses + // it to end a head-tilt Glanceboard and otherwise drops it + // before the shell, so it never counts as activity. + logLine("head-down forwarded by CFW") } event = decoded if (decoded != null) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt index 856ca6e2..a2b781bb 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt @@ -299,7 +299,8 @@ internal fun GlassesSessionCore.g1StopAudioCapture() { } /** - * There is no magnetometer on a G1, only the accelerometer and gyroscope: x, y, z in g, head frame, from the right arm. + * There is no magnetometer on a G1, only the accelerometer and gyroscope: x, y, z in g from the right arm, in the + * G1's own axes, which [emitImuData] rotates into the G2's ([ImuOrientation]). * [enable] raises the sample rate and polls it; [g1MaxSleepMs] follows. */ internal fun GlassesSessionCore.g1SetImuReport(enable: Boolean) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt index 01580acb..1ff2024c 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt @@ -1078,6 +1078,85 @@ internal fun GlassesSessionCore.shouldPollBatteryLocked(now: Long): Boolean { && (lastBatteryRefreshAtMs == 0L || now - lastBatteryRefreshAtMs >= ConnectionOptions.BATTERY_REFRESH_INTERVAL_MS) } +/** + * Bring the glasses' head-up trigger to [GlassesSessionCore.desiredHeadUpTrigger] after a settings + * read: switch on, then angle = trigger - calibration (the firmware adds its stored calibration). + */ +internal fun GlassesSessionCore.reconcileHeadUpLocked(ackPayload: ByteArray?) { + val verifying = headUpVerifyPending + headUpVerifyPending = false + val headUp = if (ackPayload == null) null else BleProtocol.parseSettingsHeadUp(ackPayload) + if (headUp != null) { + headUpSwitch = headUp[0] + headUpAngle = headUp[1] + headUpCalibration = headUp[2] + headUpReadAtMs = now() + } + if (headUpSwitch < 0 || !sessionReady) return + applyHeadUpLocked(verifying) +} + +/** + * Compare the last read with what is wanted (the trigger, or the glasses' original values + * while a restore is pending) and queue writes when they differ. After a verifying read + * that still disagrees nothing is queued: the next regular poll retries, so a firmware + * that clamps or refuses the value costs one attempt per poll interval, not a write loop. + */ +internal fun GlassesSessionCore.applyHeadUpLocked(verifying: Boolean) { + val desired = desiredHeadUpTrigger + val wantSwitch: Int + val wantAngle: Int + if (desired > 0) { + wantSwitch = 1 + wantAngle = desired - headUpCalibration + } else if (headUpRestorePending) { + wantSwitch = headUpOriginalSwitch + wantAngle = headUpOriginalAngle + } else { + return + } + if (headUpSwitch == wantSwitch && headUpAngle == wantAngle) { + if (desired > 0) { + if (verifying) logLine("head-up trigger confirmed: ${desired}° (angle $wantAngle + calibration $headUpCalibration)") + } else { + logLine("head-up setting restored (switch=$wantSwitch angle=$wantAngle)") + headUpRestorePending = false + headUpOriginalSwitch = -1 + headUpOriginalAngle = 0 + } + return + } + if (verifying) { + logLine("head-up setting not applied yet (switch=$headUpSwitch angle=$headUpAngle); retrying on next poll") + return + } + if (desired > 0 && headUpOriginalSwitch < 0) { + headUpOriginalSwitch = headUpSwitch + headUpOriginalAngle = headUpAngle + logLine("head-up original saved (switch=$headUpSwitch angle=$headUpAngle)") + } + queueHeadUpWritesLocked(wantSwitch, wantAngle) +} + +internal fun GlassesSessionCore.queueHeadUpWritesLocked(switch: Int, angle: Int) { + clearMessagesOfKindLocked("head-up-control") + val sw = messageBuilder.setHeadUp(BleProtocol.HEAD_UP_FIELD_SWITCH, switch) + sw.onTimeout = MessageCallback { logLine("head-up switch ack timeout") } + val set = messageBuilder.setHeadUp(BleProtocol.HEAD_UP_FIELD_ANGLE, angle) + set.onTimeout = MessageCallback { logLine("head-up angle ack timeout") } + // Switch on before the angle; when switching off, the angle goes first. + if (switch == 1) { + pendingMessages.addLast(sw) + pendingMessages.addLast(set) + } else { + pendingMessages.addLast(set) + pendingMessages.addLast(sw) + } + headUpVerifyPending = true + lastBatteryRefreshAtMs = 0L + logLine("queue head-up switch $switch + angle $angle (trigger ${desiredHeadUpTrigger}°, calibration $headUpCalibration)") +} + internal fun GlassesSessionCore.createBatteryQueryMessageLocked(): OutboundMessage { val message = messageBuilder.batteryQuery() message.onAck = MessageCallback { @@ -1086,6 +1165,7 @@ internal fun GlassesSessionCore.createBatteryQueryMessageLocked(): OutboundMessa ringBattery = if (ring == null) -1 else ring.battery ringCharging = if (ring == null) -1 else ring.charging if (ringBattery >= 0) releaseRingFromGlassesLocked("glasses report the ring connected") + reconcileHeadUpLocked(message.ackPayload) val snapshot = BleProtocol.parseSettingsBattery(message.ackPayload) if (snapshot != null) { headsetBattery = snapshot.battery diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/ImuOrientation.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/ImuOrientation.kt new file mode 100644 index 00000000..1735fe8a --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/ImuOrientation.kt @@ -0,0 +1,29 @@ +package com.faceclaw.app + +/** + * Faceclaw's accelerometer frame. Every model's samples are rotated into it before they reach a + * FaceclawImuListener ([GlassesSessionCore.emitImuData]), so Faceclaw apps and EvenHub pages see + * the same frame whichever glasses are worn. It is the G2's own frame, in g, glasses worn: + * + * +x forward (the way the wearer faces), +y to the wearer's left, +z up through the top of the head. + * + * At rest a reading is the "up" direction (the reaction to gravity): head level reads (0, 0, 1), + * looking 45° up (0.71, 0, 0.71), and the head tilted 45° to the right (right ear down) + * (0, 0.71, 0.71). Pitch above level is atan2(x, hypot(y, z)). + */ +object ImuOrientation { + /** + * A sample as [type]'s hardware reports it, in the canonical frame. Axes measured on hardware + * with the Developer app's accelerometer page: + * - G2: already canonical. + * - G1 (g1v0): +x right, +y up, +z back. Level reads (0, 1, 0), 45° up (0, 0.71, -0.71), + * 45° to the right (-0.71, 0.71, 0). + * - RayNeo iO: no accelerometer stream yet, so nothing to rotate; measure its axes when one + * is added. + */ + fun toCanonical(type: GlassesType, x: Double, y: Double, z: Double): DoubleArray = when (type) { + GlassesType.G2 -> doubleArrayOf(x, y, z) + GlassesType.G1 -> doubleArrayOf(-z, -x, y) + GlassesType.RAYNEO_IO -> doubleArrayOf(x, y, z) + } +} diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosMicRecorder.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosMicRecorder.kt new file mode 100644 index 00000000..4dd5aa05 --- /dev/null +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosMicRecorder.kt @@ -0,0 +1,37 @@ +package com.faceclaw.app + +import platform.Foundation.NSData + +/** + * Records the iOS voice bridge's audio to a Matroska audio file (.mka), the format Android's + * captures are recorded in (see [MicRecordingWriter]). The bridge has one stream to record: + * 16 kHz mono PCM16, as its recognizer hands it to TypeScript. + */ +class IosMicRecorder internal constructor(private val writer: MicRecordingWriter) { + /** The recording stopped early: the file could not be written. */ + val failed: Boolean get() = writer.failed + + /** Appends a chunk of 16 kHz mono PCM16 (little-endian), as having arrived now. */ + fun append(pcm16le: NSData) { + val bytes = pcm16le.byteArray() + val samples = ShortArray(bytes.size / 2) { AudioSegmentation.pcm16le(bytes[it * 2], bytes[it * 2 + 1]) } + writer.write(MicChunk(MicStream.MONO, 1, 16000, samples, samples.size, IosProtocolPlatform.elapsedRealtimeMs())) + } + + /** Completes the file. Returns the recording's length in ms. */ + fun finish(): Long = writer.finish() +} + +object IosMicRecordings { + /** + * A recorder writing a new file at [path], or null when the file cannot be created. + * [configJson] describes the capture and is stored in the file. + */ + fun open(path: String, configJson: String?): IosMicRecorder? = + try { + IosMicRecorder(MicRecordingWriter(openFileOutput(path), configJson, currentTimeMillis(), IosProtocolPlatform)) + } catch (error: Throwable) { + PlatformLog.w("FaceclawMicRecording", "could not create $path: ${error.message}") + null + } +} diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformUtil.ios.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformUtil.ios.kt index b89c02ff..7dadbbf4 100644 --- a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformUtil.ios.kt +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformUtil.ios.kt @@ -114,3 +114,54 @@ actual fun fileLength(path: String): Long = memScoped { actual fun deleteFile(path: String): Boolean = unlink(path) == 0 actual fun renameFile(from: String, to: String): Boolean = rename(from, to) == 0 + +actual fun openFileOutput(path: String): SeekableOutput { + val file = fopen(path, "wb+") ?: error("Cannot write $path") + return object : SeekableOutput { + override var size = 0L + private set + + private fun put(bytes: ByteArray, offset: Int, length: Int) { + if (length == 0) return + bytes.usePinned { + check(fwrite(it.addressOf(offset), 1u, length.convert(), file) == length.convert<platform.posix.size_t>()) { "Short write to $path" } + } + } + + override fun write(bytes: ByteArray, offset: Int, length: Int) { + put(bytes, offset, length) + size += length + // Each write is a complete element; hand it to the OS so a crash keeps it. + platform.posix.fflush(file) + } + + override fun writeAt(position: Long, bytes: ByteArray) { + require(position >= 0 && position + bytes.size <= size) + check(platform.posix.fseek(file, position, platform.posix.SEEK_SET) == 0) + put(bytes, 0, bytes.size) + check(platform.posix.fseek(file, size, platform.posix.SEEK_SET) == 0) + platform.posix.fflush(file) + } + + override fun close() { + fclose(file) + } + } +} + +actual fun openFileInput(path: String): SeekableInput { + val file = fopen(path, "rb") ?: error("Cannot read $path") + val fileSize = fileLength(path) + return object : SeekableInput { + override val length = fileSize + + override fun readAt(position: Long, buffer: ByteArray, offset: Int, length: Int): Int { + if (length == 0 || platform.posix.fseek(file, position, platform.posix.SEEK_SET) != 0) return 0 + return buffer.usePinned { platform.posix.fread(it.addressOf(offset), 1u, length.convert(), file).toInt() } + } + + override fun close() { + fclose(file) + } + } +} diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosBleCentral.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosBleCentral.kt index 68c31f7e..2a66e045 100644 --- a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosBleCentral.kt +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosBleCentral.kt @@ -29,6 +29,7 @@ import platform.CoreBluetooth.CBPeripheralDelegateProtocol import platform.CoreBluetooth.CBPeripheralStateConnected import platform.CoreBluetooth.CBPeripheralStateDisconnected import platform.CoreBluetooth.CBService +import platform.CoreBluetooth.CBUUID import platform.Foundation.NSData import platform.Foundation.NSError import platform.Foundation.NSNumber @@ -47,6 +48,9 @@ interface IosBleScanListener { fun onAdvertisement(identifier: String, name: String, manufacturerDataHex: String, rssi: Int, connectable: Boolean) } +/** A peripheral the system holds a connection to; see [IosBleCentral.systemConnectedG2Arms]. */ +class IosConnectedPeripheral(val identifier: String, val name: String) + /** ANCS authorization changes for a connected peripheral; delivered on the BLE queue. */ fun interface IosAncsAuthorizationListener { fun onAncsAuthorization(identifier: String, authorized: Boolean) @@ -214,6 +218,25 @@ class IosBleCentral(private val platform: ProtocolPlatform = IosProtocolPlatform manager.scanForPeripheralsWithServices(null, mapOf(CBCentralManagerScanOptionAllowDuplicatesKey to true)) } + /** + * G2 arms the system is connected to, through this app, another app or iOS itself. A + * connected arm stops advertising, and iOS keeps the right arm's ANCS link open after a + * session ends, so a scan alone never hears it. Empty until Bluetooth is powered on. + */ + fun systemConnectedG2Arms(): List<IosConnectedPeripheral> { + val found = ArrayList<IosConnectedPeripheral>() + dispatch_sync(queue) { + val manager = central ?: return@dispatch_sync + if (manager.state != CBManagerStatePoweredOn) return@dispatch_sync + val services = listOf(BleProtocol.PROTOCOL_SERVICE_UUID, BleProtocol.RENDER_SERVICE_UUID).map { CBUUID.UUIDWithString(it) } + for (item in manager.retrieveConnectedPeripheralsWithServices(services)) { + val peripheral = item as? CBPeripheral ?: continue + found.add(IosConnectedPeripheral(peripheral.identifier.UUIDString.uppercase(), peripheral.name ?: "")) + } + } + return found + } + /** Known peripheral identifiers from this process's scans/retrievals (uppercase). */ fun knownIdentifiers(): List<String> = cond.withLock { peers.keys.toList() } diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosGlassesSession.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosGlassesSession.kt index 4ad21dda..4e34d8b5 100644 --- a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosGlassesSession.kt +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/ble/IosGlassesSession.kt @@ -34,6 +34,11 @@ interface IosMicStatusListener { fun onNsDataMicStatus(body: NSData, arm: String) } +/** CFW keyboard-bridge records (the raw field-131 record) as NSData. Main queue. */ +interface IosKeyboardEventListener { + fun onNsDataKeyboardEvent(body: NSData) +} + /** * ObjC-facing facade over the shared [GlassesSessionCore] for the NativeScript app: the iOS * counterpart of Android's FaceclawBleCommunicator with the same method names. GATT goes @@ -230,6 +235,20 @@ class IosGlassesSession internal constructor( return adapter } + fun addKeyboardEventListenerNsData(listener: IosKeyboardEventListener): FaceclawKeyboardEventListener { + val adapter = object : FaceclawKeyboardEventListener { + override fun onKeyboardEvent(body: ByteArray?) = + listener.onNsDataKeyboardEvent((body ?: ByteArray(0)).data()) + } + core.addKeyboardEventListener(adapter) + return adapter + } + + fun removeKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.removeKeyboardEventListener(listener) + + fun sendFaceclawKeyboardControl(record: NSData?, label: String): Boolean = + core.sendFaceclawKeyboardControl(record?.byteArray(), label) + /** Every audio-characteristic packet, whether or not a capture is active (passive; sends nothing). */ fun addAudioMonitorListenerNsData(listener: IosAudioPacketListener): FaceclawAudioPacketListener { val adapter = object : FaceclawAudioPacketListener { diff --git a/native/ssh/README.md b/native/ssh/README.md new file mode 100644 index 00000000..90a77de9 --- /dev/null +++ b/native/ssh/README.md @@ -0,0 +1,43 @@ +# Faceclaw SSH transport + +The Terminal app's SSH client: a small Go package over +[golang.org/x/crypto/ssh](https://pkg.go.dev/golang.org/x/crypto/ssh), bound +into the apps with gomobile. It provides text-message channels over SSH: + +- **exec**: run a command on the host, exchanging newline-delimited text + (used for tmux control mode and the tmux session poller; see + `app/apps/terminal/tmux-client.ts`). +- **websocket**: a WebSocket to an address as seen from the host, through a + `direct-tcpip` channel (used to tunnel g2mirror-server's protocol). + +Channels to the same host and credentials share one SSH connection, with a +keepalive that fails them promptly when the peer silently disappears. Host +keys are pinned by the app (first connection reports the presented key so +the user can verify its fingerprint). Authentication uses a non-exportable +ECDSA P-256 key held by the platform keystore (Android Keystore/StrongBox, +iOS Secure Enclave; see `FaceclawSsh.kt` / `FaceclawSsh.m`) and, if the +server allows it, a password saved encrypted on the phone. + +## Building + +`scripts/go-ssh-build.cjs` runs `gomobile bind` (pinned in `go.mod`'s tool +block) from the NativeScript before-prepare hook and stages +`faceclaw-ssh.aar` / `Faceclawssh.xcframework` into the local +`@faceclaw/kotlin` plugin. It needs Go 1.26+ on PATH, plus the Android NDK +(Android) or Xcode (iOS); a content stamp skips rebuilds when the Go sources +haven't changed. + +## Testing + +`go test -race ./...` runs the transport against an in-process SSH server +(host-key pinning, password and keyboard-interactive auth, key auth through +a fake platform signer, exec framing, the websocket tunnel, connection +sharing and loss). + +## Keeping it current + +x/crypto/ssh is the security boundary against a malicious host or a +man in the middle; update it promptly when advisories appear +(`go get golang.org/x/crypto@latest && go mod tidy`). A Go panic in any +goroutine terminates the app: the package recovers in the goroutines it +owns, but not inside the library's own. diff --git a/native/ssh/channels.go b/native/ssh/channels.go new file mode 100644 index 00000000..cfc03dc7 --- /dev/null +++ b/native/ssh/channels.go @@ -0,0 +1,254 @@ +package faceclawssh + +import ( + "bufio" + "context" + "errors" + "fmt" + "io" + "net" + "net/url" + "strings" + "time" + "unicode/utf8" + + "github.com/gorilla/websocket" + "golang.org/x/crypto/ssh" +) + +type closedPayload struct { + ExitStatus *int `json:"exitStatus,omitempty"` + Signal string `json:"signal,omitempty"` + Code *int `json:"code,omitempty"` + Reason string `json:"reason,omitempty"` +} + +// runExec runs command on the host and relays newline-delimited text both +// ways until the command exits, the connection drops, or Close. +func (c *Channel) runExec(conn *pooledConn, command string) { + session, err := conn.client.NewSession() + if err != nil { + c.fail(channelOpenFailure(conn, err)) + return + } + defer session.Close() + stdin, err1 := session.StdinPipe() + stdout, err2 := session.StdoutPipe() + stderr, err3 := session.StderrPipe() + if err := errors.Join(err1, err2, err3); err != nil { + c.fail(&failure{Kind: "channel", Message: err.Error()}) + return + } + if err := session.Start(command); err != nil { + c.fail(&failure{Kind: "channel", Message: "could not run command: " + err.Error()}) + return + } + go func() { + <-c.ctx.Done() + session.Close() + }() + c.markOpen() + go func() { + defer c.guard() + for text := range c.sendQ { + if _, err := io.WriteString(stdin, text+"\n"); err != nil { + return + } + } + // Close was called and the queue has drained: EOF tells the command + // we're done (tmux control mode detaches on it). + stdin.Close() + }() + stderrDone := make(chan struct{}) + go func() { + defer close(stderrDone) + defer c.guard() + readLines(stderr, func(line string) { c.emit("stderr", line) }) + }() + readErr := readLines(stdout, func(line string) { c.emit("text", line) }) + // Deliver the command's last words (e.g. "tmux: command not found") + // before the close event, but don't hang on a stderr that never ends. + select { + case <-stderrDone: + case <-time.After(500 * time.Millisecond): + } + if c.ctx.Err() != nil { + return + } + waitErr := session.Wait() + var exitErr *ssh.ExitError + var missing *ssh.ExitMissingError + switch { + case waitErr == nil: + status := 0 + c.finish("closed", closedPayload{ExitStatus: &status}) + case errors.As(waitErr, &exitErr): + status := exitErr.ExitStatus() + c.finish("closed", closedPayload{ExitStatus: &status, Signal: exitErr.Signal()}) + case errors.As(waitErr, &missing): + // The channel closed without an exit status: usually the connection + // went away under it. + c.fail(lostFailure(conn, readErr)) + default: + c.fail(lostFailure(conn, waitErr)) + } +} + +// readLines calls emit for each line of r (without its newline) and returns +// the read error that ended it (nil at EOF). Lines may be arbitrarily long. +func readLines(r io.Reader, emit func(string)) error { + reader := bufio.NewReaderSize(r, 64*1024) + for { + line, err := reader.ReadBytes('\n') + if len(line) > 0 { + if line[len(line)-1] == '\n' { + line = line[:len(line)-1] + } + emit(escapeLine(line)) + } + if err == io.EOF { + return nil + } + if err != nil { + return err + } + } +} + +// escapeLine makes a line safe to hand across the gomobile boundary as a +// string (which must be valid UTF-8). Valid UTF-8 passes through. Otherwise +// every byte >= 0x80 becomes "\ooo" (backslash, three octal digits): the same +// escape tmux control mode uses for control characters and backslashes in +// %output, so a %output line split mid-character still decodes back to its +// exact bytes. (Outside %output a literal backslash is ambiguous with this +// escape; such lines are only ever displayed.) +func escapeLine(line []byte) string { + if utf8.Valid(line) { + return string(line) + } + var b strings.Builder + for _, c := range line { + if c >= 0x80 { + fmt.Fprintf(&b, "\\%03o", c) + } else { + b.WriteByte(c) + } + } + return b.String() +} + +// runWebSocket opens rawURL (ws://host:port/path) through the SSH +// connection — host:port is resolved on the SSH server's side — and relays +// text frames. +func (c *Channel) runWebSocket(conn *pooledConn, rawURL string) { + parsed, err := url.Parse(rawURL) + if err != nil || parsed.Scheme != "ws" || parsed.Host == "" { + c.fail(&failure{Kind: "config", Message: fmt.Sprintf("bad websocket URL %q", rawURL)}) + return + } + target := parsed.Host + if parsed.Port() == "" { + target = net.JoinHostPort(parsed.Hostname(), "80") + } + dialer := websocket.Dialer{ + NetDialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { + forwarded, err := conn.client.DialContext(ctx, "tcp", target) + if err != nil { + return nil, err + } + return noDeadlineConn{forwarded}, nil + }, + } + // The handshake timeout rides the context: SSH channels can't do deadlines. + handshakeCtx, cancelHandshake := context.WithTimeout(c.ctx, defaultConnectTimeout) + ws, _, err := dialer.DialContext(handshakeCtx, rawURL, nil) + cancelHandshake() + if err != nil { + if c.ctx.Err() != nil { + return + } + c.fail(channelOpenFailure(conn, fmt.Errorf("could not reach %s on the host: %w", target, err))) + return + } + defer ws.Close() + go func() { + <-c.ctx.Done() + ws.Close() + }() + c.markOpen() + go func() { + defer c.guard() + for text := range c.sendQ { + if err := ws.WriteMessage(websocket.TextMessage, []byte(text)); err != nil { + return + } + } + ws.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "bye"), + time.Now().Add(time.Second)) + }() + for { + kind, data, err := ws.ReadMessage() + if err != nil { + if c.ctx.Err() != nil { + return + } + var closeErr *websocket.CloseError + if errors.As(err, &closeErr) { + code := closeErr.Code + c.finish("closed", closedPayload{Code: &code, Reason: closeErr.Text}) + return + } + c.fail(lostFailure(conn, err)) + return + } + if kind == websocket.TextMessage { + c.emit("text", string(data)) + } + } +} + +// noDeadlineConn adapts an SSH direct-tcpip channel for the websocket +// library, which sets deadlines the channel doesn't support. Timeouts come +// from contexts and the SSH keepalive instead. +type noDeadlineConn struct{ net.Conn } + +func (noDeadlineConn) SetDeadline(time.Time) error { return nil } +func (noDeadlineConn) SetReadDeadline(time.Time) error { return nil } +func (noDeadlineConn) SetWriteDeadline(time.Time) error { return nil } + +// channelOpenFailure classifies a failure to open a channel: if the SSH +// connection itself is gone it's a network failure (reconnect), otherwise +// the host refused this particular channel. +func channelOpenFailure(conn *pooledConn, err error) *failure { + if isConnDead(conn) { + return &failure{Kind: "network", Message: "connection lost: " + err.Error()} + } + return &failure{Kind: "channel", Message: err.Error()} +} + +func lostFailure(conn *pooledConn, err error) *failure { + message := "connection lost" + if err != nil && !errors.Is(err, io.EOF) { + message += ": " + err.Error() + } + if !isConnDead(conn) { + // The connection is fine; only this channel ended abruptly. + return &failure{Kind: "channel", Message: "channel closed unexpectedly"} + } + return &failure{Kind: "network", Message: message} +} + +func isConnDead(conn *pooledConn) bool { + // client.Wait's goroutine marks the connection dead slightly after the + // channels see their streams end; give it a moment to catch up. + deadline := time.Now().Add(200 * time.Millisecond) + for { + poolMu.Lock() + dead := conn.dead + poolMu.Unlock() + if dead || time.Now().After(deadline) { + return dead + } + time.Sleep(10 * time.Millisecond) + } +} diff --git a/native/ssh/faceclawssh.go b/native/ssh/faceclawssh.go new file mode 100644 index 00000000..6ca260a2 --- /dev/null +++ b/native/ssh/faceclawssh.go @@ -0,0 +1,304 @@ +// Package faceclawssh is Faceclaw's SSH transport, bound into the Android and +// iOS apps with gomobile. It exposes text-message channels over SSH: +// +// - "exec": run a command on the host and exchange newline-delimited text +// with it (stdin lines in, stdout lines out). Used for tmux control mode +// and the tmux session poller. +// - "websocket": open a direct-tcpip channel to an address as seen from the +// host (normally g2mirror-server on 127.0.0.1:8737) and speak WebSocket +// text frames over it. +// +// Channels to the same host, user and credentials share one SSH connection +// (see pool.go). Host keys are pinned by the caller: a connection whose host +// presents a key that isn't in the spec's hostKeys fails with a +// "hostkey-unknown" or "hostkey-mismatch" failure carrying the presented key, +// so the app can show the fingerprint and retry once the user has trusted it. +// Authentication uses a platform-held, non-exportable ECDSA P-256 key (the +// Signer interface; Android Keystore / iOS Secure Enclave) and, if the server +// allows it, a password. +// +// Everything crossing the gomobile boundary is a string, []byte or one of the +// interfaces below; structured values are JSON strings. +package faceclawssh + +import ( + "context" + "encoding/json" + "fmt" + "runtime/debug" + "sync" + "time" +) + +// Signer is implemented by the platform: an ECDSA P-256 key whose private +// half never leaves the device's keystore. +type Signer interface { + // PublicKey returns the key's public half, either as X.509 + // SubjectPublicKeyInfo DER (Android's PublicKey.getEncoded()) or as an + // uncompressed X9.62 point, 0x04 || X || Y (iOS's + // SecKeyCopyExternalRepresentation). + PublicKey() []byte + // Sign returns the ASN.1 DER ECDSA signature of SHA-256(data). + Sign(data []byte) ([]byte, error) +} + +// Listener receives a channel's events. Calls arrive on arbitrary goroutines +// (never concurrently for one channel); the platform wrapper hops them onto +// the thread that owns the channel. +// +// Event kinds and payloads: +// - "open": the channel is ready; payload empty. Sends made before this are +// queued and delivered once open. +// - "text": one message (websocket text frame, or one stdout line without +// its newline). Exec lines that aren't valid UTF-8 have every byte >= 0x80 +// escaped as a backslash and three octal digits, matching tmux control +// mode's escaping of control characters (see escapeLine). +// - "stderr": one stderr line of an exec channel, escaped the same way. +// - "closed": the channel ended normally; payload is JSON +// {"exitStatus": n, "signal": "...", "code": n, "reason": "..."} with the +// fields that apply (exec exit status/signal, websocket close code/reason). +// Terminal: no events follow. +// - "failure": the channel could not be opened or was lost; payload is a +// JSON Failure. Terminal: no events follow. +type Listener interface { + OnEvent(kind string, payload string) +} + +// Failure describes why a channel failed, as the JSON payload of a "failure" +// event. +type failure struct { + // "hostkey-unknown": no host key is pinned yet; HostKey is what the host + // presented. "hostkey-mismatch": the host presented a key that isn't + // pinned. "auth": the host refused every credential we have. + // "network": couldn't reach the host, or the connection dropped. + // "channel": the SSH connection is fine but the channel couldn't be + // opened (exec refused, nothing listening at the forwarded address...). + // "config": the spec was malformed. "internal": a bug. + Kind string `json:"kind"` + Message string `json:"message"` + HostKey *hostKeyInfo `json:"hostKey,omitempty"` + // For "auth": the host accepts password (or keyboard-interactive) + // authentication, whether a password was sent, and whether our key was + // offered. + PasswordAllowed bool `json:"passwordAllowed,omitempty"` + PasswordTried bool `json:"passwordTried,omitempty"` + KeyTried bool `json:"keyTried,omitempty"` +} + +func (f *failure) Error() string { return f.Message } + +type hostKeyInfo struct { + // SSH key type, e.g. "ssh-ed25519". + Type string `json:"type"` + // OpenSSH-style SHA-256 fingerprint, "SHA256:<base64>". + Fingerprint string `json:"fingerprint"` + // The key in SSH wire format, base64: what goes in a spec's hostKeys. + Key string `json:"key"` +} + +type channelSpec struct { + // "exec" or "websocket". + Type string `json:"type"` + // exec: the command line, run by the user's login shell. + Command string `json:"command"` + // websocket: ws:// URL, resolved on the host's side of the connection. + URL string `json:"url"` +} + +type openSpec struct { + Host string `json:"host"` + Port int `json:"port"` + User string `json:"user"` + Password string `json:"password"` + // Pinned host keys, SSH wire format, base64. Empty: the first connection + // fails with "hostkey-unknown". + HostKeys []string `json:"hostKeys"` + // Offer the platform key (the Signer passed to Open). + UseKey bool `json:"useKey"` + ConnectTimeoutMs int `json:"connectTimeoutMs"` + KeepaliveIntervalMs int `json:"keepaliveIntervalMs"` + Channel channelSpec `json:"channel"` +} + +const ( + defaultConnectTimeout = 15 * time.Second + defaultKeepaliveInterval = 20 * time.Second + // Messages queued on a channel before Send starts refusing them. + sendQueueCapacity = 4096 + // How long Close waits for queued messages to flush and the far end to + // finish before tearing the channel down. + closeGracePeriod = 2 * time.Second +) + +// Channel is one open (or opening) channel. Methods are safe to call from +// any thread. +type Channel struct { + listener Listener + ctx context.Context + cancel context.CancelFunc + + mu sync.Mutex + closed bool // Close was called: no more sends, no more events + opened bool + sendQ chan string + + finishOnce sync.Once +} + +// Open starts opening a channel described by spec (a JSON openSpec) and +// returns immediately; progress arrives as listener events. signer may be +// nil (no key authentication). +func Open(spec string, signer Signer, listener Listener) *Channel { + ctx, cancel := context.WithCancel(context.Background()) + c := &Channel{ + listener: listener, + ctx: ctx, + cancel: cancel, + sendQ: make(chan string, sendQueueCapacity), + } + go c.run(spec, signer) + return c +} + +// Send queues one message: a websocket text frame, or a line for an exec +// channel's stdin (the newline is added). Returns false if the channel is +// closed or its queue is full. +func (c *Channel) Send(text string) bool { + c.mu.Lock() + defer c.mu.Unlock() + if c.closed { + return false + } + select { + case c.sendQ <- text: + return true + default: + return false + } +} + +// Close ends the channel. Messages already queued are still sent (so a +// parting "unview" reaches the host), then the channel's write side is +// closed and, after a short grace period, the channel is torn down. No +// events are delivered after Close. +func (c *Channel) Close() { + c.mu.Lock() + if c.closed { + c.mu.Unlock() + return + } + c.closed = true + opened := c.opened + close(c.sendQ) + c.mu.Unlock() + if !opened { + c.cancel() + return + } + time.AfterFunc(closeGracePeriod, c.cancel) +} + +func (c *Channel) isClosed() bool { + c.mu.Lock() + defer c.mu.Unlock() + return c.closed +} + +func (c *Channel) emit(kind, payload string) { + if c.isClosed() { + return + } + c.listener.OnEvent(kind, payload) +} + +func (c *Channel) markOpen() { + c.mu.Lock() + c.opened = true + c.mu.Unlock() + c.emit("open", "") +} + +// finish delivers the channel's one terminal event ("closed" or "failure"). +func (c *Channel) finish(kind string, payload any) { + c.finishOnce.Do(func() { + encoded, err := json.Marshal(payload) + if err != nil { + encoded = []byte(`{"kind":"internal","message":"unencodable event"}`) + } + c.emit(kind, string(encoded)) + c.cancel() + }) +} + +func (c *Channel) fail(f *failure) { c.finish("failure", f) } + +// guard turns a panic in one of our goroutines into a failure event instead +// of crashing the app (an unrecovered Go panic kills the whole process). +func (c *Channel) guard() { + if r := recover(); r != nil { + c.fail(&failure{Kind: "internal", Message: fmt.Sprintf("internal error: %v\n%s", r, debug.Stack())}) + } +} + +func (c *Channel) run(specJSON string, signer Signer) { + defer c.guard() + var spec openSpec + if err := json.Unmarshal([]byte(specJSON), &spec); err != nil { + c.fail(&failure{Kind: "config", Message: "bad channel spec: " + err.Error()}) + return + } + if spec.Host == "" || spec.User == "" { + c.fail(&failure{Kind: "config", Message: "host and user are required"}) + return + } + if spec.Port == 0 { + spec.Port = 22 + } + if !spec.UseKey { + signer = nil + } + conn, err := acquire(c.ctx, &spec, signer) + if err != nil { + if c.ctx.Err() != nil { + return + } + c.fail(asFailure(err)) + return + } + defer conn.release() + switch spec.Channel.Type { + case "exec": + c.runExec(conn, spec.Channel.Command) + case "websocket": + c.runWebSocket(conn, spec.Channel.URL) + default: + c.fail(&failure{Kind: "config", Message: fmt.Sprintf("unknown channel type %q", spec.Channel.Type)}) + } +} + +func asFailure(err error) *failure { + if f, ok := err.(*failure); ok { + return f + } + return &failure{Kind: "network", Message: err.Error()} +} + +// AuthorizedKey formats a platform public key (see Signer.PublicKey) as an +// authorized_keys line: "ecdsa-sha2-nistp256 AAAA... comment". +func AuthorizedKey(publicKey []byte, comment string) (string, error) { + key, err := sshPublicKey(publicKey) + if err != nil { + return "", err + } + return authorizedKeyLine(key, comment), nil +} + +// Fingerprint returns the OpenSSH SHA-256 fingerprint ("SHA256:...") of a +// platform public key. +func Fingerprint(publicKey []byte) (string, error) { + key, err := sshPublicKey(publicKey) + if err != nil { + return "", err + } + return fingerprint(key), nil +} diff --git a/native/ssh/faceclawssh_test.go b/native/ssh/faceclawssh_test.go new file mode 100644 index 00000000..cb6da45c --- /dev/null +++ b/native/ssh/faceclawssh_test.go @@ -0,0 +1,526 @@ +package faceclawssh + +import ( + "bufio" + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/sha256" + "crypto/x509" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net" + "net/http" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/gorilla/websocket" + "golang.org/x/crypto/ssh" +) + +// ---- test SSH server ---- + +type testServer struct { + addr string + hostKey ssh.Signer + accepts atomic.Int32 + listener net.Listener +} + +type serverOptions struct { + password string // "" = password auth disabled + keyboardInteractive bool // offer the password via keyboard-interactive instead + authorizedKey ssh.PublicKey +} + +func newTestServer(t *testing.T, opts serverOptions) *testServer { + t.Helper() + _, priv, _ := ed25519.GenerateKey(rand.Reader) + hostKey, err := ssh.NewSignerFromKey(priv) + if err != nil { + t.Fatal(err) + } + config := &ssh.ServerConfig{} + if opts.password != "" && !opts.keyboardInteractive { + config.PasswordCallback = func(_ ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) { + if string(password) == opts.password { + return nil, nil + } + return nil, fmt.Errorf("wrong password") + } + } + if opts.password != "" && opts.keyboardInteractive { + config.KeyboardInteractiveCallback = func(_ ssh.ConnMetadata, challenge ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error) { + answers, err := challenge("", "", []string{"Password: "}, []bool{false}) + if err != nil { + return nil, err + } + if len(answers) == 1 && answers[0] == opts.password { + return nil, nil + } + return nil, fmt.Errorf("wrong password") + } + } + if opts.authorizedKey != nil { + config.PublicKeyCallback = func(_ ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { + if string(key.Marshal()) == string(opts.authorizedKey.Marshal()) { + return nil, nil + } + return nil, fmt.Errorf("unknown key") + } + } + config.AddHostKey(hostKey) + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + server := &testServer{addr: listener.Addr().String(), hostKey: hostKey, listener: listener} + t.Cleanup(func() { listener.Close() }) + go func() { + for { + tcp, err := listener.Accept() + if err != nil { + return + } + server.accepts.Add(1) + go server.serve(tcp, config) + } + }() + return server +} + +func (s *testServer) serve(tcp net.Conn, config *ssh.ServerConfig) { + conn, chans, reqs, err := ssh.NewServerConn(tcp, config) + if err != nil { + tcp.Close() + return + } + defer conn.Close() + go ssh.DiscardRequests(reqs) // replies false to keepalives, which still counts as alive + for newChannel := range chans { + switch newChannel.ChannelType() { + case "session": + go serveSession(newChannel) + case "direct-tcpip": + go serveDirectTCPIP(newChannel) + default: + newChannel.Reject(ssh.UnknownChannelType, "nope") + } + } +} + +// serveSession implements a few fake commands: +// - "echo": echo stdin lines back with a "> " prefix until EOF, exit 0 +// - "bytes": print an invalid-UTF-8 line and a stderr line, exit 3 +func serveSession(newChannel ssh.NewChannel) { + channel, requests, err := newChannel.Accept() + if err != nil { + return + } + defer channel.Close() + for req := range requests { + if req.Type != "exec" { + req.Reply(false, nil) + continue + } + var payload struct{ Command string } + ssh.Unmarshal(req.Payload, &payload) + req.Reply(true, nil) + status := 0 + switch payload.Command { + case "echo": + scanner := bufio.NewScanner(channel) + for scanner.Scan() { + fmt.Fprintf(channel, "> %s\n", scanner.Text()) + } + case "bytes": + channel.Write([]byte("%output %1 caf\xc3\n%output %1 \xa9 ok\nplain ✓\n")) + channel.Stderr().Write([]byte("oops\n")) + status = 3 + default: + channel.Stderr().Write([]byte("unknown command\n")) + status = 127 + } + channel.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{uint32(status)})) + return + } +} + +func serveDirectTCPIP(newChannel ssh.NewChannel) { + var target struct { + Host string + Port uint32 + OriginHost string + OriginPort uint32 + } + if err := ssh.Unmarshal(newChannel.ExtraData(), &target); err != nil { + newChannel.Reject(ssh.ConnectionFailed, "bad payload") + return + } + upstream, err := net.Dial("tcp", net.JoinHostPort(target.Host, fmt.Sprint(target.Port))) + if err != nil { + newChannel.Reject(ssh.ConnectionFailed, err.Error()) + return + } + channel, requests, err := newChannel.Accept() + if err != nil { + upstream.Close() + return + } + go ssh.DiscardRequests(requests) + go func() { io.Copy(upstream, channel); upstream.Close() }() + io.Copy(channel, upstream) + channel.Close() +} + +// ---- test client plumbing ---- + +type event struct{ kind, payload string } + +type recorder struct { + events chan event +} + +func newRecorder() *recorder { return &recorder{events: make(chan event, 256)} } + +func (r *recorder) OnEvent(kind, payload string) { r.events <- event{kind, payload} } + +func (r *recorder) next(t *testing.T) event { + t.Helper() + select { + case e := <-r.events: + return e + case <-time.After(10 * time.Second): + t.Fatal("timed out waiting for event") + return event{} + } +} + +func (r *recorder) expect(t *testing.T, kind string) event { + t.Helper() + e := r.next(t) + if e.kind != kind { + t.Fatalf("got event %s(%s), want %s", e.kind, e.payload, kind) + } + return e +} + +func (r *recorder) expectFailure(t *testing.T) failure { + t.Helper() + e := r.expect(t, "failure") + var f failure + if err := json.Unmarshal([]byte(e.payload), &f); err != nil { + t.Fatal(err) + } + return f +} + +type softwareSigner struct{ key *ecdsa.PrivateKey } + +func newSoftwareSigner(t *testing.T) *softwareSigner { + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + return &softwareSigner{key: key} +} + +func (s *softwareSigner) PublicKey() []byte { + der, _ := x509.MarshalPKIXPublicKey(&s.key.PublicKey) + return der +} + +func (s *softwareSigner) Sign(data []byte) ([]byte, error) { + digest := sha256.Sum256(data) + return ecdsa.SignASN1(rand.Reader, s.key, digest[:]) +} + +func (s *softwareSigner) sshKey(t *testing.T) ssh.PublicKey { + key, err := ssh.NewPublicKey(&s.key.PublicKey) + if err != nil { + t.Fatal(err) + } + return key +} + +func specFor(server *testServer, mutate func(*openSpec)) string { + host, port, _ := net.SplitHostPort(server.addr) + var portNumber int + fmt.Sscan(port, &portNumber) + spec := openSpec{ + Host: host, + Port: portNumber, + User: "jim", + HostKeys: []string{base64.StdEncoding.EncodeToString(server.hostKey.PublicKey().Marshal())}, + Channel: channelSpec{Type: "exec", Command: "echo"}, + } + if mutate != nil { + mutate(&spec) + } + encoded, _ := json.Marshal(spec) + return string(encoded) +} + +func resetPool() { + poolMu.Lock() + defer poolMu.Unlock() + for key, conn := range pool { + conn.client.Close() + delete(pool, key) + } +} + +// ---- tests ---- + +func TestHostKeyPinning(t *testing.T) { + defer resetPool() + server := newTestServer(t, serverOptions{password: "pw"}) + wantKey := base64.StdEncoding.EncodeToString(server.hostKey.PublicKey().Marshal()) + + rec := newRecorder() + Open(specFor(server, func(s *openSpec) { s.HostKeys = nil; s.Password = "pw" }), nil, rec) + f := rec.expectFailure(t) + if f.Kind != "hostkey-unknown" || f.HostKey == nil || f.HostKey.Key != wantKey || + f.HostKey.Type != ssh.KeyAlgoED25519 || !strings.HasPrefix(f.HostKey.Fingerprint, "SHA256:") { + t.Fatalf("unexpected failure %+v", f) + } + + _, otherPriv, _ := ed25519.GenerateKey(rand.Reader) + other, _ := ssh.NewSignerFromKey(otherPriv) + rec = newRecorder() + Open(specFor(server, func(s *openSpec) { + s.HostKeys = []string{base64.StdEncoding.EncodeToString(other.PublicKey().Marshal())} + s.Password = "pw" + }), nil, rec) + f = rec.expectFailure(t) + if f.Kind != "hostkey-mismatch" || f.HostKey.Key != wantKey { + t.Fatalf("unexpected failure %+v", f) + } + + rec = newRecorder() + ch := Open(specFor(server, func(s *openSpec) { s.Password = "pw" }), nil, rec) + rec.expect(t, "open") + ch.Close() +} + +func TestPasswordAuth(t *testing.T) { + for _, interactive := range []bool{false, true} { + t.Run(fmt.Sprintf("keyboardInteractive=%v", interactive), func(t *testing.T) { + defer resetPool() + server := newTestServer(t, serverOptions{password: "secret", keyboardInteractive: interactive}) + + rec := newRecorder() + Open(specFor(server, nil), nil, rec) + f := rec.expectFailure(t) + if f.Kind != "auth" || !f.PasswordAllowed || f.PasswordTried { + t.Fatalf("no password: %+v", f) + } + + rec = newRecorder() + Open(specFor(server, func(s *openSpec) { s.Password = "wrong" }), nil, rec) + f = rec.expectFailure(t) + if f.Kind != "auth" || !f.PasswordTried { + t.Fatalf("wrong password: %+v", f) + } + + rec = newRecorder() + ch := Open(specFor(server, func(s *openSpec) { s.Password = "secret" }), nil, rec) + rec.expect(t, "open") + ch.Send("hi") + if e := rec.expect(t, "text"); e.payload != "> hi" { + t.Fatalf("got %q", e.payload) + } + ch.Close() + }) + } +} + +func TestKeyAuth(t *testing.T) { + defer resetPool() + signer := newSoftwareSigner(t) + server := newTestServer(t, serverOptions{authorizedKey: signer.sshKey(t)}) + + rec := newRecorder() + ch := Open(specFor(server, func(s *openSpec) { s.UseKey = true }), signer, rec) + rec.expect(t, "open") + ch.Close() + + // A key the server doesn't know, and no password support. + resetPool() + rec = newRecorder() + Open(specFor(server, func(s *openSpec) { s.UseKey = true }), newSoftwareSigner(t), rec) + f := rec.expectFailure(t) + if f.Kind != "auth" || !f.KeyTried || f.PasswordAllowed { + t.Fatalf("unexpected failure %+v", f) + } + + line, err := AuthorizedKey(signer.PublicKey(), "Faceclaw on my phone") + if err != nil || !strings.HasPrefix(line, "ecdsa-sha2-nistp256 AAAA") || !strings.HasSuffix(line, " Faceclaw-on-my-phone") { + t.Fatalf("AuthorizedKey = %q, %v", line, err) + } + parsed, _, _, _, err := ssh.ParseAuthorizedKey([]byte(line)) + if err != nil || string(parsed.Marshal()) != string(signer.sshKey(t).Marshal()) { + t.Fatalf("authorized key does not round-trip: %v", err) + } + // The uncompressed-point form iOS hands us parses to the same key. + point := elliptic.Marshal(elliptic.P256(), signer.key.X, signer.key.Y) //nolint:staticcheck + fromPoint, err := Fingerprint(point) + fromDER, _ := Fingerprint(signer.PublicKey()) + if err != nil || fromPoint != fromDER { + t.Fatalf("fingerprints differ: %s vs %s (%v)", fromPoint, fromDER, err) + } +} + +func TestExecFraming(t *testing.T) { + defer resetPool() + server := newTestServer(t, serverOptions{password: "pw"}) + rec := newRecorder() + Open(specFor(server, func(s *openSpec) { s.Password = "pw"; s.Channel.Command = "bytes" }), nil, rec) + rec.expect(t, "open") + var texts, stderr []string + var closed closedPayload + for done := false; !done; { + e := rec.next(t) + switch e.kind { + case "text": + texts = append(texts, e.payload) + case "stderr": + stderr = append(stderr, e.payload) + case "closed": + json.Unmarshal([]byte(e.payload), &closed) + done = true + default: + t.Fatalf("unexpected %s(%s)", e.kind, e.payload) + } + } + want := []string{`%output %1 caf\303`, `%output %1 \251 ok`, "plain ✓"} + if strings.Join(texts, "|") != strings.Join(want, "|") { + t.Fatalf("texts = %q, want %q", texts, want) + } + if len(stderr) != 1 || stderr[0] != "oops" { + t.Fatalf("stderr = %q", stderr) + } + if closed.ExitStatus == nil || *closed.ExitStatus != 3 { + t.Fatalf("closed = %+v", closed) + } +} + +func TestWebSocketTunnelAndSharing(t *testing.T) { + defer resetPool() + upgrader := websocket.Upgrader{} + var serverConns sync.WaitGroup + wsServer := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + serverConns.Add(1) + defer serverConns.Done() + defer conn.Close() + for { + kind, data, err := conn.ReadMessage() + if err != nil { + return + } + if string(data) == "quit" { + conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(4000, "asked to quit")) + return + } + conn.WriteMessage(kind, append([]byte("echo:"), data...)) + } + })} + wsListener, _ := net.Listen("tcp", "127.0.0.1:0") + go wsServer.Serve(wsListener) + defer wsServer.Close() + + server := newTestServer(t, serverOptions{password: "pw"}) + wsSpec := specFor(server, func(s *openSpec) { + s.Password = "pw" + s.Channel = channelSpec{Type: "websocket", URL: "ws://" + wsListener.Addr().String() + "/"} + }) + recA, recB := newRecorder(), newRecorder() + a := Open(wsSpec, nil, recA) + b := Open(wsSpec, nil, recB) + // Sent before open: queued until the channel is up. + a.Send(`{"type":"init"}`) + recA.expect(t, "open") + recB.expect(t, "open") + if e := recA.expect(t, "text"); e.payload != `echo:{"type":"init"}` { + t.Fatalf("got %q", e.payload) + } + b.Send("quit") + e := recB.expect(t, "closed") + var closed closedPayload + json.Unmarshal([]byte(e.payload), &closed) + if closed.Code == nil || *closed.Code != 4000 || closed.Reason != "asked to quit" { + t.Fatalf("closed = %+v", closed) + } + if n := server.accepts.Load(); n != 1 { + t.Fatalf("expected both channels on one SSH connection, got %d connections", n) + } + + // Close flushes queued messages before tearing down. + a.Send("last words") + a.Close() + time.Sleep(300 * time.Millisecond) + select { + case e := <-recA.events: + t.Fatalf("event after Close: %s(%s)", e.kind, e.payload) + default: + } + + // Nothing listening at the forwarded address: a channel failure, not a + // network one (the SSH connection is fine). + recC := newRecorder() + Open(specFor(server, func(s *openSpec) { + s.Password = "pw" + s.Channel = channelSpec{Type: "websocket", URL: "ws://127.0.0.1:1/"} + }), nil, recC) + if f := recC.expectFailure(t); f.Kind != "channel" { + t.Fatalf("unexpected failure %+v", f) + } +} + +func TestConnectionLoss(t *testing.T) { + defer resetPool() + server := newTestServer(t, serverOptions{password: "pw"}) + rec := newRecorder() + Open(specFor(server, func(s *openSpec) { s.Password = "pw" }), nil, rec) + rec.expect(t, "open") + // Kill the SSH connection under the channel. + poolMu.Lock() + for _, conn := range pool { + conn.client.Close() + } + poolMu.Unlock() + if f := rec.expectFailure(t); f.Kind != "network" { + t.Fatalf("unexpected failure %+v", f) + } + // The next channel dials a fresh connection. + rec = newRecorder() + ch := Open(specFor(server, func(s *openSpec) { s.Password = "pw" }), nil, rec) + rec.expect(t, "open") + ch.Close() + if n := server.accepts.Load(); n != 2 { + t.Fatalf("expected a second connection, got %d", n) + } +} + +func TestNetworkFailure(t *testing.T) { + rec := newRecorder() + Open(`{"host":"127.0.0.1","port":1,"user":"jim","channel":{"type":"exec","command":"x"}}`, nil, rec) + if f := rec.expectFailure(t); f.Kind != "network" { + t.Fatalf("unexpected failure %+v", f) + } + rec = newRecorder() + Open(`not json`, nil, rec) + if f := rec.expectFailure(t); f.Kind != "config" { + t.Fatalf("unexpected failure %+v", f) + } +} diff --git a/native/ssh/go.mod b/native/ssh/go.mod new file mode 100644 index 00000000..b2d34a18 --- /dev/null +++ b/native/ssh/go.mod @@ -0,0 +1,21 @@ +module github.com/jimrandomh/faceclaw/native/ssh + +go 1.26.0 + +require ( + github.com/gorilla/websocket v1.5.3 + golang.org/x/crypto v0.59.0 +) + +require ( + golang.org/x/mobile v0.0.0-20261011034528-197770444867 // indirect + golang.org/x/mod v0.42.0 // indirect + golang.org/x/sync v0.24.0 // indirect + golang.org/x/sys v0.50.0 // indirect + golang.org/x/tools v0.52.0 // indirect +) + +tool ( + golang.org/x/mobile/cmd/gobind + golang.org/x/mobile/cmd/gomobile +) diff --git a/native/ssh/go.sum b/native/ssh/go.sum new file mode 100644 index 00000000..02707999 --- /dev/null +++ b/native/ssh/go.sum @@ -0,0 +1,16 @@ +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +golang.org/x/crypto v0.59.0 h1:xS2jhqYbzNrpvkWP2YTDf4TJwBy3ykR03i8vFDxEc7c= +golang.org/x/crypto v0.59.0/go.mod h1:veDyUwATGs3c+vN3VfTbzViMGXeTBk3WyhBOkn+F/4U= +golang.org/x/mobile v0.0.0-20261011034528-197770444867 h1:rav73cdd7OQUPgt1aIFbZQTiPNnjfueKXvfOiLCY0Ss= +golang.org/x/mobile v0.0.0-20261011034528-197770444867/go.mod h1:HR/iBzuQmVTl/us733QPwrqHp43a8m9ZLgyKXD3ST6U= +golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= +golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs= +golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/term v0.48.0 h1:G6UU1eiSB8nl3DzeUGt7Mg+2SctI8tPS6Z5/mBt6cOk= +golang.org/x/term v0.48.0/go.mod h1:BRDLHCZr2SUjIeBHHpRyQeC9xzSAVbzY2rvKgafnHf4= +golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= +golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= diff --git a/native/ssh/interop_test.go b/native/ssh/interop_test.go new file mode 100644 index 00000000..1e5167b1 --- /dev/null +++ b/native/ssh/interop_test.go @@ -0,0 +1,58 @@ +package faceclawssh + +import ( + "encoding/json" + "net" + "os" + "strconv" + "testing" +) + +// TestOpenSSHInterop checks the parts of the handshake that need no +// credentials against a real sshd: key exchange, host key reporting and +// pinning, and how an authentication refusal is classified. Opt-in: +// +// FACECLAW_SSH_INTEROP_ADDR=127.0.0.1:22 go test -run Interop -v +// +// Compare the logged fingerprint with `ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub`. +// It makes one failed login attempt (no password, an unknown key) as +// FACECLAW_SSH_INTEROP_USER (default: $USER). +func TestOpenSSHInterop(t *testing.T) { + addr := os.Getenv("FACECLAW_SSH_INTEROP_ADDR") + if addr == "" { + t.Skip("FACECLAW_SSH_INTEROP_ADDR not set") + } + defer resetPool() + user := os.Getenv("FACECLAW_SSH_INTEROP_USER") + if user == "" { + user = os.Getenv("USER") + } + host, portText, err := net.SplitHostPort(addr) + if err != nil { + t.Fatal(err) + } + port, _ := strconv.Atoi(portText) + spec := func(hostKeys []string) string { + encoded, _ := json.Marshal(openSpec{ + Host: host, Port: port, User: user, HostKeys: hostKeys, UseKey: true, + Channel: channelSpec{Type: "exec", Command: "true"}, + }) + return string(encoded) + } + + rec := newRecorder() + Open(spec(nil), newSoftwareSigner(t), rec) + f := rec.expectFailure(t) + if f.Kind != "hostkey-unknown" || f.HostKey == nil { + t.Fatalf("first contact: %+v", f) + } + t.Logf("host key: %s %s", f.HostKey.Type, f.HostKey.Fingerprint) + + rec = newRecorder() + Open(spec([]string{f.HostKey.Key}), newSoftwareSigner(t), rec) + auth := rec.expectFailure(t) + if auth.Kind != "auth" || !auth.KeyTried { + t.Fatalf("pinned, unknown key: %+v", auth) + } + t.Logf("auth refusal: %+v", auth) +} diff --git a/native/ssh/keys.go b/native/ssh/keys.go new file mode 100644 index 00000000..3d876086 --- /dev/null +++ b/native/ssh/keys.go @@ -0,0 +1,91 @@ +package faceclawssh + +import ( + "crypto/ecdsa" + "crypto/elliptic" + "crypto/sha256" + "crypto/x509" + "encoding/asn1" + "encoding/base64" + "errors" + "fmt" + "io" + "math/big" + "strings" + + "golang.org/x/crypto/ssh" +) + +// platformSigner adapts the platform's keystore-backed Signer to ssh.Signer. +// The platform signs SHA-256(data) with ECDSA P-256 and returns ASN.1 DER; +// SSH wants the signature as two mpints (RFC 5656 §3.1.2). +type platformSigner struct { + signer Signer + pub ssh.PublicKey +} + +func newPlatformSigner(signer Signer) (*platformSigner, error) { + pub, err := sshPublicKey(signer.PublicKey()) + if err != nil { + return nil, err + } + return &platformSigner{signer: signer, pub: pub}, nil +} + +func (p *platformSigner) PublicKey() ssh.PublicKey { return p.pub } + +func (p *platformSigner) Sign(_ io.Reader, data []byte) (*ssh.Signature, error) { + der, err := p.signer.Sign(data) + if err != nil { + return nil, fmt.Errorf("platform key signing failed: %w", err) + } + var sig struct{ R, S *big.Int } + rest, err := asn1.Unmarshal(der, &sig) + if err != nil || len(rest) != 0 || sig.R == nil || sig.S == nil || sig.R.Sign() <= 0 || sig.S.Sign() <= 0 { + return nil, errors.New("platform key returned a malformed signature") + } + blob := ssh.Marshal(struct{ R, S *big.Int }{sig.R, sig.S}) + return &ssh.Signature{Format: p.pub.Type(), Blob: blob}, nil +} + +// sshPublicKey parses a platform public key (SubjectPublicKeyInfo DER or an +// uncompressed P-256 point) into an ecdsa-sha2-nistp256 SSH key. +func sshPublicKey(encoded []byte) (ssh.PublicKey, error) { + var pub *ecdsa.PublicKey + if len(encoded) == 65 && encoded[0] == 4 { + parsed, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), encoded) + if err != nil { + return nil, fmt.Errorf("bad P-256 point: %v", err) + } + pub = parsed + } else { + parsed, err := x509.ParsePKIXPublicKey(encoded) + if err != nil { + return nil, fmt.Errorf("bad public key: %v", err) + } + ecdsaKey, ok := parsed.(*ecdsa.PublicKey) + if !ok { + return nil, fmt.Errorf("unsupported public key type %T", parsed) + } + pub = ecdsaKey + } + if pub.Curve != elliptic.P256() { + return nil, errors.New("only P-256 keys are supported") + } + return ssh.NewPublicKey(pub) +} + +func authorizedKeyLine(key ssh.PublicKey, comment string) string { + line := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))) + comment = strings.Join(strings.Fields(comment), "-") + if comment != "" { + line += " " + comment + } + return line +} + +// fingerprint is OpenSSH's SHA256 fingerprint format (unpadded base64). +func fingerprint(key ssh.PublicKey) string { + sum := sha256.Sum256(key.Marshal()) + return "SHA256:" + base64.RawStdEncoding.EncodeToString(sum[:]) +} diff --git a/native/ssh/pool.go b/native/ssh/pool.go new file mode 100644 index 00000000..8720c439 --- /dev/null +++ b/native/ssh/pool.go @@ -0,0 +1,416 @@ +package faceclawssh + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "errors" + "fmt" + "log" + "net" + "strconv" + "strings" + "sync" + "time" + + "golang.org/x/crypto/ssh" +) + +// SSH connections are shared between channels with the same host, port, +// user, credentials and pinned keys: the Terminal app's session poller and +// each of its view windows ride one TCP connection. A connection with no +// channels left lingers briefly (a view window closing and reopening shouldn't +// pay a fresh handshake) and is then closed. A keepalive probe closes +// connections whose peer has silently gone away, which fails every channel on +// them so the app's reconnect logic takes over. + +const idleLinger = 15 * time.Second + +type pooledConn struct { + key string + client *ssh.Client + + // Guarded by poolMu. + refs int + dead bool + linger *time.Timer +} + +type dialCall struct { + done chan struct{} + err error +} + +var ( + poolMu sync.Mutex + pool = map[string]*pooledConn{} + dialing = map[string]*dialCall{} +) + +func poolKey(spec *openSpec, signer *platformSigner) string { + h := sha256.New() + fmt.Fprintf(h, "%s\x00%d\x00%s\x00%s\x00", spec.Host, spec.Port, spec.User, spec.Password) + for _, key := range spec.HostKeys { + fmt.Fprintf(h, "%s\x00", key) + } + if signer != nil { + h.Write(signer.pub.Marshal()) + } + fmt.Fprintf(h, "\x00%d", spec.KeepaliveIntervalMs) + return hex.EncodeToString(h.Sum(nil)) +} + +// acquire returns a live pooled connection for spec, dialing one if needed +// (concurrent callers for the same key share a single dial). The caller must +// release() it. +func acquire(ctx context.Context, spec *openSpec, signer Signer) (*pooledConn, error) { + var ps *platformSigner + if signer != nil { + var err error + if ps, err = newPlatformSigner(signer); err != nil { + return nil, &failure{Kind: "config", Message: "client key unusable: " + err.Error()} + } + } + key := poolKey(spec, ps) + for { + poolMu.Lock() + if conn := pool[key]; conn != nil && !conn.dead { + conn.refs++ + if conn.linger != nil { + conn.linger.Stop() + conn.linger = nil + } + poolMu.Unlock() + return conn, nil + } + if call := dialing[key]; call != nil { + poolMu.Unlock() + select { + case <-call.done: + case <-ctx.Done(): + return nil, ctx.Err() + } + if call.err != nil { + return nil, call.err + } + continue + } + call := &dialCall{done: make(chan struct{})} + dialing[key] = call + poolMu.Unlock() + + conn, err := dial(spec, ps, key) + poolMu.Lock() + delete(dialing, key) + if err == nil { + pool[key] = conn + } + call.err = err + close(call.done) + poolMu.Unlock() + if err != nil { + return nil, err + } + // Loop around to take a reference like any other caller. + } +} + +func (conn *pooledConn) release() { + poolMu.Lock() + defer poolMu.Unlock() + conn.refs-- + if conn.refs > 0 || conn.dead { + return + } + conn.linger = time.AfterFunc(idleLinger, func() { + poolMu.Lock() + idle := conn.refs == 0 && !conn.dead + if idle { + conn.dead = true + if pool[conn.key] == conn { + delete(pool, conn.key) + } + } + poolMu.Unlock() + if idle { + conn.client.Close() + } + }) +} + +// markDead removes a connection from the pool once its transport has ended, +// so the next acquire dials afresh. +func (conn *pooledConn) markDead() { + poolMu.Lock() + defer poolMu.Unlock() + conn.dead = true + if conn.linger != nil { + conn.linger.Stop() + conn.linger = nil + } + if pool[conn.key] == conn { + delete(pool, conn.key) + } +} + +// hostKeyChecker pins the host key: only keys listed in the spec are +// accepted, and whatever the host presented is kept for the failure report. +type hostKeyChecker struct { + known [][]byte + presented ssh.PublicKey + rejected bool +} + +func newHostKeyChecker(encoded []string) (*hostKeyChecker, error) { + checker := &hostKeyChecker{} + for _, entry := range encoded { + wire, err := base64.StdEncoding.DecodeString(strings.TrimSpace(entry)) + if err != nil { + return nil, fmt.Errorf("bad pinned host key: %v", err) + } + if _, err := ssh.ParsePublicKey(wire); err != nil { + return nil, fmt.Errorf("bad pinned host key: %v", err) + } + checker.known = append(checker.known, wire) + } + return checker, nil +} + +func (h *hostKeyChecker) check(_ string, _ net.Addr, key ssh.PublicKey) error { + h.presented = key + wire := key.Marshal() + for _, known := range h.known { + if bytes.Equal(known, wire) { + return nil + } + } + h.rejected = true + return errors.New("host key not trusted") +} + +// algorithms lists host key algorithms with the pinned keys' types first, so +// a host with several host keys presents the one we pinned rather than +// whichever the default preference order favors. +func (h *hostKeyChecker) algorithms() []string { + var preferred []string + seen := map[string]bool{} + add := func(algorithm string) { + if !seen[algorithm] { + seen[algorithm] = true + preferred = append(preferred, algorithm) + } + } + for _, wire := range h.known { + key, err := ssh.ParsePublicKey(wire) + if err != nil { + continue + } + if key.Type() == ssh.KeyAlgoRSA { + add(ssh.KeyAlgoRSASHA512) + add(ssh.KeyAlgoRSASHA256) + continue + } + add(key.Type()) + } + if len(preferred) == 0 { + return nil // the library default + } + for _, algorithm := range ssh.SupportedAlgorithms().HostKeys { + add(algorithm) + } + return preferred +} + +func (h *hostKeyChecker) failure() *failure { + if h.presented == nil { + return nil + } + info := &hostKeyInfo{ + Type: h.presented.Type(), + Fingerprint: fingerprint(h.presented), + Key: base64.StdEncoding.EncodeToString(h.presented.Marshal()), + } + if len(h.known) == 0 { + return &failure{Kind: "hostkey-unknown", Message: "Unknown host key " + info.Fingerprint, HostKey: info} + } + return &failure{Kind: "hostkey-mismatch", Message: "Host key changed: now " + info.Fingerprint, HostKey: info} +} + +// errNoPassword aborts authentication when the host would accept a password +// but we don't have one; the "auth" failure then asks the app to prompt. +var errNoPassword = errors.New("password required") + +type authState struct { + mu sync.Mutex + passwordAllowed bool + passwordTried bool + keyTried bool +} + +func (a *authState) set(update func(*authState)) { + a.mu.Lock() + update(a) + a.mu.Unlock() +} + +func authMethods(spec *openSpec, signer *platformSigner, state *authState) []ssh.AuthMethod { + var methods []ssh.AuthMethod + if signer != nil { + methods = append(methods, ssh.PublicKeysCallback(func() ([]ssh.Signer, error) { + state.set(func(a *authState) { a.keyTried = true }) + return []ssh.Signer{signer}, nil + })) + } + password := spec.Password + methods = append(methods, + ssh.PasswordCallback(func() (string, error) { + state.set(func(a *authState) { a.passwordAllowed = true }) + if password == "" { + return "", errNoPassword + } + state.set(func(a *authState) { a.passwordTried = true }) + return password, nil + }), + // Many hosts (PAM-backed sshd, e.g. macOS) take passwords through + // keyboard-interactive. Answer hidden prompts with the password and + // visible ones with nothing. + ssh.KeyboardInteractive(func(_, _ string, questions []string, echos []bool) ([]string, error) { + if len(questions) == 0 { + return nil, nil + } + state.set(func(a *authState) { a.passwordAllowed = true }) + if password == "" { + return nil, errNoPassword + } + state.set(func(a *authState) { a.passwordTried = true }) + answers := make([]string, len(questions)) + for i := range questions { + if !echos[i] { + answers[i] = password + } + } + return answers, nil + }), + ) + return methods +} + +func dial(spec *openSpec, signer *platformSigner, key string) (*pooledConn, error) { + timeout := defaultConnectTimeout + if spec.ConnectTimeoutMs > 0 { + timeout = time.Duration(spec.ConnectTimeoutMs) * time.Millisecond + } + checker, err := newHostKeyChecker(spec.HostKeys) + if err != nil { + return nil, &failure{Kind: "config", Message: err.Error()} + } + addr := net.JoinHostPort(spec.Host, strconv.Itoa(spec.Port)) + dialer := net.Dialer{Timeout: timeout, KeepAlive: 30 * time.Second} + tcp, err := dialer.Dial("tcp", addr) + if err != nil { + return nil, &failure{Kind: "network", Message: describeNetError(err)} + } + // Bound the whole handshake, not just the TCP connect. + tcp.SetDeadline(time.Now().Add(timeout)) + state := &authState{} + config := &ssh.ClientConfig{ + User: spec.User, + Auth: authMethods(spec, signer, state), + HostKeyCallback: checker.check, + HostKeyAlgorithms: checker.algorithms(), + ClientVersion: "SSH-2.0-Faceclaw", + Timeout: timeout, + } + sshConn, chans, reqs, err := ssh.NewClientConn(tcp, addr, config) + if err != nil { + tcp.Close() + if checker.rejected { + return nil, checker.failure() + } + state.mu.Lock() + defer state.mu.Unlock() + if errors.Is(err, errNoPassword) || strings.Contains(err.Error(), "unable to authenticate") { + message := "The host refused this device's key." + switch { + case state.passwordTried: + message = "The host refused the password." + case state.passwordAllowed: + message = "The host wants a password." + case signer == nil: + message = "The host accepts neither passwords nor keys we have." + } + return nil, &failure{ + Kind: "auth", + Message: message, + PasswordAllowed: state.passwordAllowed, + PasswordTried: state.passwordTried, + KeyTried: state.keyTried, + } + } + return nil, &failure{Kind: "network", Message: "SSH handshake failed: " + describeNetError(err)} + } + tcp.SetDeadline(time.Time{}) + client := ssh.NewClient(sshConn, chans, reqs) + conn := &pooledConn{key: key, client: client} + log.Printf("faceclawssh: connected to %s@%s", spec.User, addr) + go func() { + err := client.Wait() + log.Printf("faceclawssh: connection to %s ended: %v", addr, err) + conn.markDead() + }() + interval := defaultKeepaliveInterval + if spec.KeepaliveIntervalMs > 0 { + interval = time.Duration(spec.KeepaliveIntervalMs) * time.Millisecond + } + go keepalive(conn, interval) + return conn, nil +} + +// keepalive probes the host every interval and closes the connection if a +// probe goes unanswered for a whole interval, so a dead peer (phone switched +// networks, laptop asleep) fails the channels promptly instead of hanging. +func keepalive(conn *pooledConn, interval time.Duration) { + defer func() { + if r := recover(); r != nil { + log.Printf("faceclawssh: keepalive panic: %v", r) + conn.client.Close() + } + }() + ticker := time.NewTicker(interval) + defer ticker.Stop() + for range ticker.C { + poolMu.Lock() + dead := conn.dead + poolMu.Unlock() + if dead { + return + } + reply := make(chan error, 1) + go func() { + _, _, err := conn.client.SendRequest("keepalive@openssh.com", true, nil) + reply <- err + }() + select { + case err := <-reply: + if err != nil { + conn.client.Close() + return + } + case <-time.After(interval): + log.Printf("faceclawssh: keepalive timed out; closing") + conn.client.Close() + return + } + } +} + +func describeNetError(err error) string { + message := err.Error() + // "dial tcp 1.2.3.4:22: connect: connection refused" → keep the tail. + if index := strings.LastIndex(message, ": "); index >= 0 && strings.HasPrefix(message, "dial ") { + return message[index+2:] + } + return message +} diff --git a/scripts/go-ssh-build.cjs b/scripts/go-ssh-build.cjs new file mode 100644 index 00000000..b947914e --- /dev/null +++ b/scripts/go-ssh-build.cjs @@ -0,0 +1,129 @@ +// Builds the Go SSH transport (native/ssh) with gomobile and stages it into +// the local @faceclaw/kotlin NativeScript plugin next to the shared Kotlin +// artifacts: platforms/android/faceclaw-ssh.aar and +// platforms/ios/Faceclawssh.xcframework. Run from the before-prepare hook; +// a content stamp skips the (~10 s) gomobile build when nothing changed. +const { spawn, spawnSync } = require('node:child_process'); +const crypto = require('node:crypto'); +const { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } = require('node:fs'); +const path = require('node:path'); + +const root = path.resolve(__dirname, '..'); +const source = path.join(root, 'native/ssh'); +const buildDir = path.join(source, 'build'); +const plugin = path.join(root, 'native/kotlin/plugin/platforms'); + +// Bump to force a rebuild when the gomobile invocation below changes. +const BUILD_RECIPE = 2; + +function sourceHash(platform) { + const hash = crypto.createHash('sha256'); + hash.update(`${BUILD_RECIPE}\n${platform}\n`); + const files = readdirSync(source) + .filter(name => (name.endsWith('.go') && !name.endsWith('_test.go')) || name === 'go.mod' || name === 'go.sum') + .sort(); + for (const name of files) { + hash.update(`${name}\n`); + hash.update(readFileSync(path.join(source, name))); + } + return hash.digest('hex'); +} + +function findGo() { + const probe = spawnSync('go', ['version'], { encoding: 'utf8' }); + if (probe.status !== 0) { + throw new Error('Building the SSH transport needs Go 1.26 or newer on PATH (e.g. "brew install go").'); + } +} + +function newestNdk() { + if (process.env.ANDROID_NDK_HOME && existsSync(process.env.ANDROID_NDK_HOME)) return process.env.ANDROID_NDK_HOME; + const sdk = process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT; + if (!sdk) throw new Error('ANDROID_HOME must be set to build the SSH transport for Android.'); + const ndkRoot = path.join(sdk, 'ndk'); + const versions = existsSync(ndkRoot) ? readdirSync(ndkRoot).sort((a, b) => b.localeCompare(a, undefined, { numeric: true })) : []; + if (!versions.length) throw new Error(`No Android NDK under ${ndkRoot}; install one with the SDK manager.`); + return path.join(ndkRoot, versions[0]); +} + +// gomobile's ObjC headers say "@import Foundation;", which fails wherever +// Clang modules are off (the NativeScript app target's compile, and its +// metadata generator). A plain #import means the same thing everywhere. +function patchGomobileHeaders(xcframework) { + const walk = (dir) => { + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const file = path.join(dir, entry.name); + if (entry.isDirectory()) walk(file); + else if (entry.name.endsWith('.h')) { + const text = readFileSync(file, 'utf8'); + const patched = text.replace(/^@import Foundation;$/gm, '#import <Foundation/Foundation.h>'); + if (patched !== text) writeFileSync(file, patched); + } + } + }; + walk(xcframework); +} + +function run(command, args, env) { + return new Promise((resolve, reject) => { + const child = spawn(command, args, { cwd: source, stdio: 'inherit', env }); + child.on('error', reject); + child.on('exit', (code, signal) => { + if (code === 0) resolve(); + else reject(new Error(`${command} ${args[0]} failed (${signal || code})`)); + }); + }); +} + +async function buildGoSsh(platform) { + if (!['android', 'ios'].includes(platform)) throw new Error(`Unsupported platform: ${platform}`); + const stampFile = path.join(buildDir, `.stamp-${platform}`); + const output = platform === 'android' + ? path.join(buildDir, 'faceclaw-ssh.aar') + : path.join(buildDir, 'Faceclawssh.xcframework'); + const staged = platform === 'android' + ? path.join(plugin, 'android/faceclaw-ssh.aar') + : path.join(plugin, 'ios/Faceclawssh.xcframework'); + const hash = sourceHash(platform); + const fresh = existsSync(output) && existsSync(stampFile) && readFileSync(stampFile, 'utf8') === hash; + if (!fresh) { + findGo(); + console.log(`[go-ssh] Building ${platform}`); + mkdirSync(path.join(buildDir, 'bin'), { recursive: true }); + const env = { ...process.env, PATH: `${path.join(buildDir, 'bin')}${path.delimiter}${process.env.PATH}` }; + // gomobile bind finds gobind on PATH; build both from the versions pinned + // in go.mod's tool block. + await run('go', ['build', '-o', path.join(buildDir, 'bin') + path.sep, 'golang.org/x/mobile/cmd/gomobile', 'golang.org/x/mobile/cmd/gobind'], env); + const common = ['-ldflags=-s -w', '-trimpath']; + rmSync(output, { recursive: true, force: true }); + if (platform === 'android') { + env.ANDROID_NDK_HOME = newestNdk(); + await run('gomobile', ['bind', '-target=android/arm64', '-androidapi', '24', '-javapkg', 'com.faceclaw.ssh', ...common, '-o', output, '.'], env); + } else { + await run('gomobile', ['bind', '-target=ios,iossimulator', '-iosversion', '15.0', ...common, '-o', output, '.'], env); + patchGomobileHeaders(output); + } + writeFileSync(stampFile, hash); + } + // Stage only when the staged copy is missing or stale; preserve mtimes on + // no-op builds (NativeScript uses them to detect changes). + const stagedStamp = path.join(buildDir, `.staged-${platform}`); + if (existsSync(staged) && existsSync(stagedStamp) && readFileSync(stagedStamp, 'utf8') === hash) return; + mkdirSync(path.dirname(staged), { recursive: true }); + rmSync(staged, { recursive: true, force: true }); + cpSync(output, staged, { recursive: true }); + writeFileSync(stagedStamp, hash); + if (platform === 'android') { + // As in kotlin-build.cjs: NativeScript's metadata task tracks jars by + // path, so force it to regenerate for the new classes. + rmSync(path.join(root, 'platforms/android/app/src/main/assets/metadata'), { recursive: true, force: true }); + } +} + +module.exports = { buildGoSsh }; +if (require.main === module) { + buildGoSsh(process.argv[2]).catch(error => { + console.error(error); + process.exitCode = 1; + }); +} diff --git a/scripts/hooks/faceclaw-kotlin.before-prepare.js b/scripts/hooks/faceclaw-kotlin.before-prepare.js index 435b315f..03dc7ecb 100644 --- a/scripts/hooks/faceclaw-kotlin.before-prepare.js +++ b/scripts/hooks/faceclaw-kotlin.before-prepare.js @@ -1,8 +1,11 @@ const { buildKotlin, checkAndroidRuntime } = require('../../scripts/kotlin-build.cjs'); +const { buildGoSsh } = require('../../scripts/go-ssh-build.cjs'); module.exports = function (hookArgs) { // NativeScript parses hooks with an older JavaScript parser; avoid optional chaining. const { platform, release } = hookArgs.prepareData; if (platform === 'android') checkAndroidRuntime(); - return buildKotlin(platform, release ? 'release' : 'debug'); + return buildKotlin(platform, release ? 'release' : 'debug').then(function () { + return buildGoSsh(platform); + }); }; diff --git a/tests/assistant-settings-visibility.test.cjs b/tests/assistant-settings-visibility.test.cjs index e7a8846c..1e8ed8e3 100644 --- a/tests/assistant-settings-visibility.test.cjs +++ b/tests/assistant-settings-visibility.test.cjs @@ -2,7 +2,7 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); -function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') { +function settingsEnv(initialUrl = '', initialModel = 'openai-compatible', glassesType = 'g2') { let url = initialUrl; let model = initialModel; let webSearch = false; @@ -31,6 +31,7 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') { assistantHermesUrlSetting: { ...dummySetting, id: 'assistant-hermes-url', label: 'Hermes URL' }, assistantHermesKeySetting: { ...dummySetting, id: 'assistant-hermes-key', label: 'Hermes key' }, veniceWebSearchSetting: searchSetting, + glassesTypeSetting: { ...dummySetting, id: 'glasses-type', get: () => (typeof glassesType === 'function' ? glassesType() : glassesType) }, enumSettingMenuItem: row, textSettingMenuItem: row, toggleSettingMenuItem: row, @@ -73,6 +74,8 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') { }, '../font-picker': { uiFontPickerMenuItem: () => ({ label: 'Font' }) }, '../notification-filter': { NotificationFilterLayer: class {} }, + '../keyboard-settings': { keyboardSettingsMenuItem: () => ({ label: 'Bluetooth keyboard' }) }, + '../keybinding-settings': { keybindingsMenuItem: () => ({ label: 'Keybindings' }) }, '../openai-compatible-model-picker': { compatibleModelDiscoveryMenuItem: () => ({ label: 'Discover models' }), }, @@ -83,6 +86,7 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') { assert.ok(section); return { section, + sections: panel.sections, labels: () => section.items.map(item => item.label), setUrl: value => { url = value; }, setModel: value => { model = value; }, @@ -148,3 +152,21 @@ test('OpenAI-compatible rows appear only while OpenAI-compatible is the selected const labels = env.labels(); assert.equal(labels.indexOf('Compatible API URL'), labels.indexOf('Assistant model') + 1, 'compatible rows follow the model row'); }); + +test('the Keyboard section, with its pairing row, follows the glasses type while Settings stays open', () => { + let glassesType = 'g1'; + const sections = settingsEnv('', 'openai-compatible', () => glassesType).sections; + const labels = sections.map(section => section.label); + // Always in the list, between the other input devices and the developer + // options; whether it shows is asked again on every paint. + assert.equal(labels.indexOf('Keyboard'), labels.indexOf('Watch') + 1); + const keyboard = sections.find(section => section.label === 'Keyboard'); + assert.equal(keyboard.items.map(item => item.label).join(), 'Bluetooth keyboard,Keybindings'); + assert.equal(keyboard.hidden(), true); + glassesType = 'rayneo-io'; + assert.equal(keyboard.hidden(), true); + glassesType = 'g2'; + assert.equal(keyboard.hidden(), false); + // Nothing else is conditional this way. + assert.equal(sections.filter(section => section.hidden).length, 1); +}); diff --git a/tests/calendar-event-days.test.cjs b/tests/calendar-event-days.test.cjs new file mode 100644 index 00000000..e0d216b2 --- /dev/null +++ b/tests/calendar-event-days.test.cjs @@ -0,0 +1,47 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +// Local-time day arithmetic: pin the zone so the expectations hold on any machine. +process.env.TZ = 'Europe/Brussels'; + +function load(file) { + const sandbox = { exports: {}, require: id => { throw new Error(`Unstubbed import: ${id}`); } }; + vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, sandbox); + return sandbox.exports; +} + +const { listingDayMs, spansDays, spanLabel } = load('app/apps/calendar/event-days.ts'); +const local = (y, m, d, h = 0, min = 0) => new Date(y, m - 1, d, h, min).getTime(); +// Mon Nov 16 – Wed Nov 18, all day, as the provider stores it (end = midnight after the last day). +const threeDays = { startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true }; + +test('an all-day event still going is listed under today, not its first day', () => { + assert.equal(listingDayMs(threeDays, local(2026, 11, 18, 0, 24)), local(2026, 11, 18)); + assert.equal(listingDayMs(threeDays, local(2026, 11, 17, 9)), local(2026, 11, 17)); +}); + +test('events that have not started are listed under their start day', () => { + assert.equal(listingDayMs(threeDays, local(2026, 11, 14, 12)), local(2026, 11, 16)); + const meeting = { startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }; + assert.equal(listingDayMs(meeting, local(2026, 11, 18, 0, 24)), local(2026, 11, 19)); +}); + +test('a timed event running past midnight is listed under today the next morning', () => { + const lateShift = { startMs: local(2026, 11, 17, 22), endMs: local(2026, 11, 18, 2), allDay: false }; + assert.equal(listingDayMs(lateShift, local(2026, 11, 18, 0, 30)), local(2026, 11, 18)); + assert.equal(spansDays(lateShift), true); +}); + +test('only events covering more than one day get a span, and it reads naturally', () => { + assert.equal(spansDays(threeDays), true); + assert.equal(spanLabel(threeDays), 'Mon 16 – Wed 18 Nov'); + assert.equal(spansDays({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true }), false); + assert.equal(spansDays({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }), false); + assert.equal(spanLabel({ startMs: local(2026, 11, 30), endMs: local(2026, 12, 2), allDay: true }), 'Mon 30 Nov – Tue 1 Dec'); +}); diff --git a/tests/date-format.test.cjs b/tests/date-format.test.cjs new file mode 100644 index 00000000..5b847e22 --- /dev/null +++ b/tests/date-format.test.cjs @@ -0,0 +1,45 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +// Local-time formatting: pin the zone so the expectations hold on any machine. +process.env.TZ = 'Europe/Brussels'; + +function load(file) { + const sandbox = { exports: {}, require: id => { throw new Error(`Unstubbed import: ${id}`); } }; + vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, sandbox); + return sandbox.exports; +} + +const { formatEvent, formatEventSpan } = load('app/assistant/date-format.ts'); +const local = (y, m, d, h = 0, min = 0) => new Date(y, m - 1, d, h, min).getTime(); + +test('a multi-day all-day event names its last day (all-day ends are exclusive)', () => { + // Mon Nov 16 – Wed Nov 18, as the calendar provider stores it: end = midnight after the last day. + const span = formatEventSpan({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true }); + assert.equal(span, 'Mon Nov 16 – Wed Nov 18 (all day)'); +}); + +test('a one-day all-day event stays a single date', () => { + assert.equal(formatEventSpan({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true }), 'Mon Nov 16 (all day)'); +}); + +test('a timed event shows its end time, and the end date when it crosses midnight', () => { + assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }), 'Thu Nov 19, 9:00 AM – 12:00 PM'); + assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 22), endMs: local(2026, 11, 20, 2), allDay: false }), 'Thu Nov 19, 10:00 PM – Fri Nov 20, 2:00 AM'); +}); + +test('a timed event without a usable end keeps the old start-only form', () => { + assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 9), allDay: false }), 'Thu Nov 19, 9:00 AM'); +}); + +test('formatEvent keeps the title and location around the span', () => { + const line = formatEvent({ title: 'Office', startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true, location: 'HQ' }); + assert.equal(line, '- Office — Mon Nov 16 – Wed Nov 18 (all day) @ HQ'); + assert.equal(formatEvent({ title: '', startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true, location: '' }), '- (untitled) — Mon Nov 16 (all day)'); +}); diff --git a/tests/even-advertisement.test.cjs b/tests/even-advertisement.test.cjs index 99a9befa..f156e2c4 100644 --- a/tests/even-advertisement.test.cjs +++ b/tests/even-advertisement.test.cjs @@ -275,3 +275,27 @@ test("names that only look like a G1 are not admitted", () => { assert.equal(classifyAdvertisement(g1Raw(name, "")), null, name); } }); + +test("an iOS arm rebuilt from a saved pairing joins its advertising other arm", () => { + const { g2ManufacturerData } = require("../.test-build/app/g2/even-advertisement.js"); + const { identifyIosPeripheral } = require("../.test-build/app/g2/ios-peripheral-identity.js"); + const { DiscoveryAggregator } = require("../.test-build/app/g2/pairing-candidates.js"); + const mfg = g2ManufacturerData("S211GBBC180304", "E0:12:14:B6:EC:E1"); + assert.equal(extractG2Serial(hexToBytes(mfg)), "S211GBBC180304"); + assert.equal(extractG2Mac(hexToBytes(mfg)), "E0:12:14:B6:EC:E1"); + const right = identifyIosPeripheral({ identifier: "14b99254-0798-d1a8-3eec-b141bdabfae8", name: "Even G2_32_R_B6ECE1", + manufacturerData: mfg, rssi: 0, connectable: true }); + assert.equal(right.address, "E0:12:14:B6:EC:E1"); + assert.equal(right.role, "right"); + assert.equal(right.note, null); + const aggregator = new DiscoveryAggregator(); + aggregator.ingest(raw({})); + aggregator.ingest({ ...right, rssi: null, source: "paired" }); + const [pair] = aggregator.pairs(); + assert.equal(pair.completeness, "complete"); + assert.equal(pair.rssi, -55); + + assert.equal(g2ManufacturerData("S211GBBC180304XX", "E0:12:14:B6:EC:E1"), null); + assert.equal(g2ManufacturerData("", "E0:12:14:B6:EC:E1"), null); + assert.equal(g2ManufacturerData("S211GBBC180304", "not a mac"), null); +}); diff --git a/tests/glance-state.test.cjs b/tests/glance-state.test.cjs index 0bc7d415..af3c229e 100644 --- a/tests/glance-state.test.cjs +++ b/tests/glance-state.test.cjs @@ -4,7 +4,9 @@ const assert = require("node:assert/strict"); const { GLANCE_HIDDEN, GLANCE_TIMEOUT_MS, + GLANCE_TILT_MAX_MS, glanceEventForGesture, + glanceTiltMaxMs, reduceGlance, } = require("../.test-build/app/g2/glance-state.js"); @@ -12,7 +14,7 @@ const T0 = 1_000_000; test("a press shows the board and arms the auto-hide timer", () => { const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0); - assert.deepEqual(shown, { visible: true, holding: false, hideAtMs: T0 + GLANCE_TIMEOUT_MS }); + assert.deepEqual(shown, { visible: true, holding: false, tilted: false, tiltMaxMs: 0, hideAtMs: T0 + GLANCE_TIMEOUT_MS }); // A timer firing early is ignored; on time it hides. assert.equal(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS - 1), shown); assert.deepEqual(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), GLANCE_HIDDEN); @@ -27,7 +29,7 @@ test("a further press renews the timer from the new press", () => { test("a hold shows the board without a timer and the release hides it", () => { const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0); - assert.deepEqual(held, { visible: true, holding: true, hideAtMs: null }); + assert.deepEqual(held, { visible: true, holding: true, tilted: false, tiltMaxMs: 0, hideAtMs: null }); assert.equal(reduceGlance(held, { type: "timeout" }, T0 + 60_000), held); // A tap mid-hold neither hides nor starts a timer. assert.equal(reduceGlance(held, { type: "press" }, T0 + 500), held); @@ -47,9 +49,11 @@ test("dismiss hides from any state", () => { assert.deepEqual(reduceGlance(shown, { type: "dismiss" }, T0), GLANCE_HIDDEN); }); -test("gesture mapping: taps and head-tilt press, both long-press gestures hold, double-tap dismisses only a visible board", () => { +test("gesture mapping: taps press, head-tilt tilts, both long-press gestures hold, double-tap dismisses only a visible board", () => { assert.deepEqual(glanceEventForGesture("click", false), { type: "press" }); - assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "press" }); + assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "tilt" }); + assert.deepEqual(glanceEventForGesture("head-down", true), { type: "head-down" }); + assert.equal(glanceEventForGesture("head-down", false), null); assert.deepEqual(glanceEventForGesture("long-press", false), { type: "hold" }); assert.deepEqual(glanceEventForGesture("short-then-long-press", false), { type: "hold" }); assert.deepEqual(glanceEventForGesture("short-then-long-press", true), { type: "hold" }); @@ -65,3 +69,61 @@ test("the press timeout is the host's when supplied", () => { assert.equal(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), shown); assert.deepEqual(reduceGlance(shown, { type: "timeout" }, T0 + 10_000), GLANCE_HIDDEN); }); + +test("a head-tilt shows the board until the head comes back down", () => { + const up = reduceGlance(GLANCE_HIDDEN, { type: "tilt" }, T0); + assert.deepEqual(up, { visible: true, holding: false, tilted: true, tiltMaxMs: GLANCE_TILT_MAX_MS, hideAtMs: T0 + GLANCE_TILT_MAX_MS }); + // The tap duration no longer applies, and taps change nothing. + assert.equal(reduceGlance(up, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), up); + assert.equal(reduceGlance(up, { type: "press" }, T0 + 500), up); + assert.deepEqual(reduceGlance(up, { type: "head-down" }, T0 + 9000), GLANCE_HIDDEN); + // The safety cap still hides it if the head-down never arrives. + assert.deepEqual(reduceGlance(up, { type: "timeout" }, T0 + GLANCE_TILT_MAX_MS), GLANCE_HIDDEN); +}); + +test("head-down leaves a tap's or a hold's showing alone", () => { + const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0); + assert.equal(reduceGlance(shown, { type: "head-down" }, T0 + 100), shown); + const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0); + assert.equal(reduceGlance(held, { type: "head-down" }, T0 + 100), held); +}); + +test("a hold and a head-tilt together: the board stays until both end", () => { + const both = reduceGlance(reduceGlance(GLANCE_HIDDEN, { type: "tilt" }, T0), { type: "hold" }, T0 + 100); + assert.deepEqual(both, { visible: true, holding: true, tilted: true, tiltMaxMs: GLANCE_TILT_MAX_MS, hideAtMs: null }); + const released = reduceGlance(both, { type: "release" }, T0 + 200); + assert.deepEqual(released, { visible: true, holding: false, tilted: true, tiltMaxMs: GLANCE_TILT_MAX_MS, hideAtMs: T0 + 200 + GLANCE_TILT_MAX_MS }); + const downWhileHeld = reduceGlance(both, { type: "head-down" }, T0 + 200); + assert.deepEqual(downWhileHeld, { visible: true, holding: true, tilted: false, tiltMaxMs: 0, hideAtMs: null }); + assert.deepEqual(reduceGlance(downWhileHeld, { type: "release" }, T0 + 300), GLANCE_HIDDEN); +}); + +test("a tilt with a shorter cap keeps it, also across a hold", () => { + const up = reduceGlance(GLANCE_HIDDEN, { type: "tilt", maxMs: 5000 }, T0); + assert.equal(up.hideAtMs, T0 + 5000); + assert.deepEqual(reduceGlance(up, { type: "timeout" }, T0 + 5000), GLANCE_HIDDEN); + const held = reduceGlance(up, { type: "hold" }, T0 + 100); + assert.equal(reduceGlance(held, { type: "release" }, T0 + 1000).hideAtMs, T0 + 1000 + 5000); +}); + +test("the tilt cap is the Head tilt timeout, or the tap duration if shorter where the head-down is unlikely", () => { + const tapTimeoutMs = 5000; + const cap = (capMs, resumedSession, triggerDegrees) => glanceTiltMaxMs({ capMs, resumedSession, triggerDegrees, tapTimeoutMs }); + assert.equal(cap(60_000, false, 30), 60_000); + assert.equal(cap(15_000, false, null), 15_000); + assert.equal(cap(null, false, 30), null); + assert.equal(cap(60_000, true, 30), tapTimeoutMs); + assert.equal(cap(60_000, false, 20), tapTimeoutMs); + assert.equal(cap(null, false, 20), tapTimeoutMs); + assert.equal(cap(3000, true, 30), 3000); + assert.equal(cap(60_000, false, 25), 60_000); +}); + +test("a tilt with no timeout stays until the head-down, also across a hold", () => { + const up = reduceGlance(GLANCE_HIDDEN, { type: "tilt", maxMs: null }, T0); + assert.equal(up.hideAtMs, null); + assert.equal(reduceGlance(up, { type: "timeout" }, T0 + 3_600_000), up); + const held = reduceGlance(up, { type: "hold" }, T0 + 100); + assert.equal(reduceGlance(held, { type: "release" }, T0 + 1000).hideAtMs, null); + assert.deepEqual(reduceGlance(up, { type: "head-down" }, T0 + 9000), GLANCE_HIDDEN); +}); diff --git a/tests/imu-types.test.cjs b/tests/imu-types.test.cjs new file mode 100644 index 00000000..ad52f25b --- /dev/null +++ b/tests/imu-types.test.cjs @@ -0,0 +1,30 @@ +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { imuPitchDegrees } = require("../.test-build/app/native/imu-types.js"); + +const rad = (deg) => (deg * Math.PI) / 180; + +/** + * A reading at rest in the canonical frame (+x forward, +y left, +z up), in + * m/s^2 to show the scale does not matter: the head pitched up by pitchDeg, + * then rolled by rollDeg. + */ +function reading(pitchDeg, rollDeg = 0) { + const p = rad(pitchDeg); + const r = rad(rollDeg); + return { x: 9.8 * Math.sin(p), y: 9.8 * Math.cos(p) * Math.sin(r), z: 9.8 * Math.cos(p) * Math.cos(r) }; +} + +test("imuPitchDegrees reads the head's pitch from one reading, past 45° and with the head rolled", () => { + for (const pitch of [-30, 0, 5, 20, 45, 50, 60, 75]) { + for (const roll of [0, 30, -40]) { + assert.ok(Math.abs(imuPitchDegrees(reading(pitch, roll)) - pitch) < 1e-6, `pitch ${pitch} roll ${roll}`); + } + } + // The poses measured on a G2 (in g). + const s = Math.SQRT1_2; + assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: 0, z: 1 })) < 1e-9); + assert.ok(Math.abs(imuPitchDegrees({ x: s, y: 0, z: s }) - 45) < 1e-9); + assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: s, z: s })) < 1e-9); +}); diff --git a/tests/ios-calendar.test.cjs b/tests/ios-calendar.test.cjs index 88acbd03..73b4b1fd 100644 --- a/tests/ios-calendar.test.cjs +++ b/tests/ios-calendar.test.cjs @@ -163,7 +163,7 @@ function screenFixture() { 'util/numeric-util': { clamp: (n, min, max) => Math.max(min, Math.min(max, n)) }, }; const modules = depth => Object.fromEntries(Object.entries(common).map(([key, value]) => [depth + key, value])); - const calendar = load('app/apps/calendar/calendar.ts', modules('../../')); + const calendar = load('app/apps/calendar/calendar.ts', { ...modules('../../'), './event-days': load('app/apps/calendar/event-days.ts') }); const widget = load('app/apps/glanceboard/widgets/calendar-widget.ts', { ...modules('../../../'), '../../calendar/calendar': calendar, '../glance-font': { glanceFont: { small: () => font, medium: () => font } }, @@ -243,6 +243,7 @@ test('iOS assistant calendar tool awaits events, reports denied access and failu '../native/media-controller': { mediaControllerBridge: { start: async () => {} } }, '../native/spotify': { spotifyService: () => null }, '../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {}, + './date-format': load('app/assistant/date-format.ts'), }, { global: { isIOS: true } }); registerSystemTools({ registerSystemTool: (definition, handler) => registered.set(definition.name, handler) }); assert.deepEqual([...registered.keys()], ['glasses.get_state', 'glasses.show_alert', 'calendar.list_events', diff --git a/tests/ios-communicator-surfaces.test.cjs b/tests/ios-communicator-surfaces.test.cjs index 6e1a6cd9..b9d30751 100644 --- a/tests/ios-communicator-surfaces.test.cjs +++ b/tests/ios-communicator-surfaces.test.cjs @@ -33,7 +33,7 @@ function harness(refused = new Set()) { FaceclawKitIosGlassesSession: { alloc: () => ({ initWithRightAddressLeftAddressRingAddress: () => session }) }, FaceclawKitFaceclawBleCommunicatorListener: {}, FaceclawKitIosAncsListener: {}, FaceclawKitIosAudioPacketListener: {}, FaceclawKitFaceclawCompassListener: {}, FaceclawKitFaceclawImuListener: {}, FaceclawKitIosAmbientLightListener: {}, - FaceclawKitIosMicStatusListener: {}, console: { log() {}, warn() {} }, + FaceclawKitIosMicStatusListener: {}, FaceclawKitIosKeyboardEventListener: {}, console: { log() {}, warn() {} }, }, { '@nativescript/core': { File: {}, knownFolders: {}, path: {} }, '../g2/firmware-compat': { REQUIRED_FACECLAW_FIRMWARE_VERSION: 37 }, diff --git a/tests/ios-glanceboard.test.cjs b/tests/ios-glanceboard.test.cjs index b9d55157..949ac03f 100644 --- a/tests/ios-glanceboard.test.cjs +++ b/tests/ios-glanceboard.test.cjs @@ -30,6 +30,9 @@ function fixture() { let now = 1000, nextTask = 0, screenOn = true, shellOptions; const battery = {}; const tasks = new Map(), sent = [], previews = [], received = [], errors = []; + // The keyboard bridge's service, as the controller sees it: what it was told, and the host it was given. + const keyboard = { host: null, calls: [], setHost(host) { this.host = host; }, linkReady() { this.calls.push('ready'); }, + linkLost(release) { this.calls.push(release ? 'lost+release' : 'lost'); }, retryNow() { this.calls.push('retry'); }, handleUnhandledKey() {} }; const boardStats = { starts: 0, stops: 0, paints: 0 }; const registered = [], inProcessApps = []; const settings = { lock: true, enabled: true, tap: true, hold: true, tilt: true, duration: 3000, depth: 0 }; @@ -165,6 +168,7 @@ function fixture() { './device-addresses': { loadDeviceAddresses: () => ({ right: 'AA', left: 'BB', ring: '' }) }, './ios-peripheral-identity': { deviceAddressError: () => null }, './wear-remote': { WearRemote: class { schedulePublish() {} } }, './firmware-compat': load('app/g2/firmware-compat.ts', {}), './reconnect-policy': load('app/g2/reconnect-policy.ts', {}), + './keyboard/service': { keyboardService: keyboard }, '../apps/launcher': { launcherEntries: () => [] }, '../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': { onPhoneShownChanged: () => () => {}, phoneUiButton: () => null }, '../apps/evenhub/updates': {}, '../apps/evenhub': {}, @@ -223,7 +227,7 @@ function fixture() { } async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); } async function connect() { await controller.connect(); await render(); } - return { relay, controller, shell, battery, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers, + return { relay, controller, shell, battery, remoteHost, keyboard, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers, get shellOptions() { return shellOptions; }, registered, inProcessApps, get bridge() { return bridge; }, connect, wear: wearing => bridge.emitWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render }; } @@ -348,3 +352,53 @@ test('the switcher can open the Notifications window in the background, once, wi assert.equal(f.shellOptions.openNotificationsWindow().opened, true); assert.equal(f.inProcessApps.length, 2); }); + +test('a paired keyboard: the bridge follows the G2 link, and its keys\' gestures go in with physical input', async () => { + const f = fixture(); + await f.connect(); + // Connected is not enough: the firmware has to be one with the bridge. + f.bridge.emitState('connected'); + assert.ok(!f.keyboard.calls.includes('ready')); + f.bridge.listeners.firmware({ leftVersion: '2.3.0.24', rightVersion: '2.3.0.24', extension: 'Faceclaw/39' }); + assert.ok(!f.keyboard.calls.includes('ready')); + f.bridge.listeners.firmware({ leftVersion: '2.3.0.24', rightVersion: '2.3.0.24', extension: 'Faceclaw/40' }); + assert.equal(f.keyboard.calls.at(-1), 'ready'); + assert.equal(f.keyboard.host.ready(), true); + // The link dropping on its own ends it without a word to the firmware; coming back restores it. + f.bridge.emitState('retrying', 'Reconnecting'); + assert.equal(f.keyboard.calls.at(-1), 'lost'); assert.equal(f.keyboard.host.ready(), false); + f.bridge.emitState('connected'); + assert.equal(f.keyboard.calls.at(-1), 'ready'); + + // A key's gesture is ordinary input from there on, tagged with the device it stands in for. + f.received.length = 0; + await f.keyboard.host.gesture('swipe-left', 'watch'); await f.keyboard.host.gesture('click', 'ring'); + await f.keyboard.host.gesture('long-press', 'watch'); + assert.deepEqual(f.received.map(input => `${input.type}/${input.source}`), + ['swipe-left/watch', 'click/ring', 'long-press/watch', 'long-press-release/watch']); + // A dark display: the bridge stops looking for an absent keyboard, a key wakes it, and it looks again. + f.shell.sleep(); + assert.equal(f.keyboard.host.shouldReconnect(), false); + f.keyboard.calls.length = 0; f.received.length = 0; + f.keyboard.host.wake(); await f.render(); + assert.deepEqual(f.received.map(input => input.type), ['display-wake']); + assert.equal(f.shell.isScreenOn(), true); assert.ok(f.keyboard.calls.includes('retry')); + assert.equal(f.keyboard.host.shouldReconnect(), true); + + // Disconnecting on purpose lets the keyboard go while the link is still up. + f.keyboard.calls.length = 0; + await f.controller.disconnect(); + assert.equal(f.keyboard.calls[0], 'lost+release'); + assert.equal(f.keyboard.host.ready(), false); +}); + +test('a keyboard needs a G2: other glasses never bring the bridge up', async () => { + glassesType = 'g1'; + try { + const f = fixture(); + await f.connect(); + f.bridge.emitState('connected'); + f.bridge.listeners.firmware({ leftVersion: 'g1v0 0.5.66', rightVersion: 'g1v0 0.5.66', extension: 'Faceclaw/40' }); + assert.ok(!f.keyboard.calls.includes('ready'), f.keyboard.calls.join()); + } finally { glassesType = 'g2'; } +}); diff --git a/tests/ios-sensors.test.cjs b/tests/ios-sensors.test.cjs index d84dffd2..1118c01c 100644 --- a/tests/ios-sensors.test.cjs +++ b/tests/ios-sensors.test.cjs @@ -37,7 +37,7 @@ function harness() { FaceclawKitIosGlassesSession: { alloc: () => ({ initWithRightAddressLeftAddressRingAddress: () => session }) }, FaceclawKitFaceclawBleCommunicatorListener: {}, FaceclawKitIosAncsListener: {}, FaceclawKitIosAudioPacketListener: {}, FaceclawKitFaceclawCompassListener: {}, FaceclawKitFaceclawImuListener: {}, FaceclawKitIosAmbientLightListener: {}, - FaceclawKitIosMicStatusListener: {}, console: { log() {}, warn() {} }, + FaceclawKitIosMicStatusListener: {}, FaceclawKitIosKeyboardEventListener: {}, console: { log() {}, warn() {} }, }, { '@nativescript/core': { File: {}, knownFolders: {}, path: {} }, '../g2/firmware-compat': { REQUIRED_FACECLAW_FIRMWARE_VERSION: 37 }, diff --git a/tests/ios-spotify.test.cjs b/tests/ios-spotify.test.cjs index 4a7f941c..0a7428a0 100644 --- a/tests/ios-spotify.test.cjs +++ b/tests/ios-spotify.test.cjs @@ -511,6 +511,7 @@ test('the assistant offers Spotify search-and-play once signed in and refreshes snapshot: () => ({ accessEnabled: true, playbackState: 'playing', title: 'Song', artist: 'Band', appName: 'Spotify' }) } }, '../native/spotify': { spotifyService: () => spotify }, '../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {}, + './date-format': load('app/assistant/date-format.ts'), }, { global: { isIOS: true } }); registerSystemTools({ registerSystemTool: (definition, handler) => systemTools.set(definition.name, handler), diff --git a/tests/ios-voice.test.cjs b/tests/ios-voice.test.cjs index 2561c249..d0c533de 100644 --- a/tests/ios-voice.test.cjs +++ b/tests/ios-voice.test.cjs @@ -36,7 +36,7 @@ function harness() { static open(name) { recordingAttempts++; if (failRecordings) return null; - const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r; + const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.mka`; r.pcm = []; r.closed = false; recordings.push(r); return r; } append(bytes) { this.pcm.push(...bytes); } close() { this.closed = true; } @@ -49,7 +49,7 @@ function harness() { setTimeout: fn => { timers.set(fn, fn); return fn; }, clearTimeout: fn => timers.delete(fn), NSData: { dataWithBytesLength: (b, n) => Buffer.from(b, 0, n) }, interop: { handleof: b => b, bufferFromData: d => d }, require: name => name === './cloud-stt-provider' ? provider : name === './speech-analyzer-stt.ios' ? analyzer - : name === './wav-recorder.ios' ? { WavRecorder: FakeRecorder } : {} }; + : name === './mic-recorder.ios' ? { MicRecorder: FakeRecorder } : {} }; vm.runInNewContext(ts.transpileModule(fs.readFileSync('app/native/voice-control.ios.ts', 'utf8'), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox); const bridge = new sandbox.exports.IosVoiceControlBridge(); diff --git a/tests/keybinding-settings.test.cjs b/tests/keybinding-settings.test.cjs new file mode 100644 index 00000000..ebd8a8cc --- /dev/null +++ b/tests/keybinding-settings.test.cjs @@ -0,0 +1,160 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +function load(file, modules) { + const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); + const context = { exports: {}, console, require(name) { assert.ok(name in modules, name); return modules[name]; } }; + vm.runInNewContext(ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, context); + return context.exports; +} + +const graphics = require('../.test-build/app/graphics/image.js'); +const metrics = require('../.test-build/app/ui/metrics.js'); +const menuCore = require('../.test-build/app/ui/menu-core.js'); +const bindings = require('../.test-build/app/g2/keyboard/bindings.js'); +const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} }); +const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u12n.bdf'), 'utf8')); +const textwrap = load('app/graphics/textwrap.ts', {}); +const fonts = { getDefaultSmallFont: () => font }; +const menu = load('app/ui/menu.ts', { + './menu-animation-pref': require('../.test-build/app/ui/menu-animation-pref.js'), './animation-speed': require('../.test-build/app/ui/animation-speed.js'), '../native/settings-store': { getStringSetting: (_key, fallback) => fallback }, '../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 }, + '../graphics/image': graphics, '../graphics/textwrap': textwrap, '../graphics/ui-fonts': fonts, + '../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) }, './gestures': {}, + './metrics': metrics, './menu-core': menuCore, +}); + +/** The settings menu over a fake keyboard service and layer stack. */ +function fixture({ connected = true, stored = [], room = true } = {}) { + const service = { + list: [...stored], capture: null, cancelled: 0, + bridge: { isConnected: () => connected }, + keyBindings() { return this.list; }, + setKeyBinding(chord, action) { + if (!room) return false; + const id = bindings.chordId(chord); + const index = this.list.findIndex(b => bindings.chordId(b.chord) === id); + const next = [...this.list]; + if (index < 0) next.push({ chord, action }); else next[index] = { chord, action }; + this.list = next; + return true; + }, + removeKeyBinding(chord) { this.list = this.list.filter(b => bindings.chordId(b.chord) !== bindings.chordId(chord)); }, + captureNextKey(onChord) { this.capture = onChord; return () => { if (this.capture === onChord) { this.capture = null; this.cancelled++; } }; }, + }; + class TextViewerLayer { constructor(text, title) { this.text = text; this.title = title; } handleInput(event, ctx) { if (event.type === 'double-click') ctx.stack.pop(); } } + const { keybindingsMenuItem } = load('app/ui/keybinding-settings.ts', { + '../graphics/ui-fonts': fonts, '../g2/keyboard/bindings': bindings, '../g2/keyboard/service': { keyboardService: service }, + '../apps/files/text-viewer': { TextViewerLayer }, './menu': menu, + }); + const layers = [{ base: true }]; + let renders = 0; + const removed = layer => layer.onRemoved?.(); + const ctx = { + actions: { requestRender: () => { renders++; } }, + stack: { + getBaseSize: () => ({ width: 576, height: 260, padding: { top: 0, right: 0, bottom: 0, left: 0 } }), isFocused: () => true, + push: layer => { layers.push(layer); }, + pop: () => { if (layers.length > 1) removed(layers.pop()); }, + popThrough: layer => { + const index = layers.indexOf(layer); + if (index <= 0) return false; + for (const gone of layers.splice(index).reverse()) removed(gone); + return true; + }, + }, + }; + const top = () => layers[layers.length - 1]; + const input = type => top().handleInput({ type, timestampMs: 0 }, ctx); + const labels = () => top().items.map(item => item.label); + /** Move the highlight of the menu on top to the row with this label, and select it. */ + async function choose(label) { + const layer = top(); + for (let i = 0; i <= layer.items.length && layer.selectedItem?.label !== label; i++) await input('scroll-down'); + assert.equal(layer.selectedItem?.label, label, `${label} in ${labels().join(' | ')}`); + await input('click'); + } + const item = keybindingsMenuItem(); + return { service, item, ctx, layers, top, input, labels, choose, renders: () => renders, + open: () => item.onSelect(ctx), chord: (code, mods = {}) => ({ code, shift: false, ctrl: false, alt: false, meta: false, ...mods }) }; +} + +test('adding a keybinding: the key is read from the keyboard, then an action is picked', async () => { + const f = fixture(); + f.open(); + assert.deepEqual(f.labels(), ['Add keybinding', 'Default keys']); + await f.choose('Add keybinding'); + assert.ok(f.service.capture, 'waiting for a key'); + assert.match(f.top().body ?? f.top().constructor.name, /Press the key|KeyCaptureLayer/); + f.service.capture(f.chord('AudioVolumeUp')); + // The waiting page is gone and the picker for that key is up, with every action in it. + assert.equal(f.layers.length, 3); assert.equal(f.top().title, 'Volume Up'); + assert.deepEqual(f.labels(), ['Select', 'Back', 'Swipe up', 'Swipe down', 'Swipe left', 'Swipe right', + 'Scroll up', 'Scroll down', 'System menu', 'App menu', 'Nothing']); + await f.choose('Scroll up'); + assert.deepEqual(f.service.list, [{ chord: f.chord('AudioVolumeUp'), action: 'scroll-up' }]); + // Back on the list, on the new row. + assert.equal(f.layers.length, 2); + assert.deepEqual(f.labels(), ['Volume Up', 'Add keybinding', 'Default keys']); + assert.equal(f.top().selectedItem.label, 'Volume Up'); + assert.ok(f.renders() > 0); +}); + +test('a keybinding can be changed or removed, and a chord is one keybinding however often it is added', async () => { + const f = fixture(); + f.service.list = [{ chord: f.chord('Escape', { shift: true }), action: 'none' }, { chord: f.chord('KeyQ', { ctrl: true }), action: 'long-press' }]; + f.open(); + assert.deepEqual(f.labels(), ['Shift+Esc', 'Ctrl+Q', 'Add keybinding', 'Default keys']); + await f.choose('Ctrl+Q'); + // The picker opens on the current action, marks it, and offers removal. + assert.equal(f.top().selectedItem.label, 'System menu *'); assert.equal(f.labels().at(-1), 'Remove keybinding'); + await f.choose('App menu'); + assert.equal(f.service.list[1].action, 'short-then-long-press'); assert.equal(f.service.list.length, 2); + // Adding the same chord again edits it rather than making a second one. + await f.choose('Add keybinding'); + f.service.capture(f.chord('KeyQ', { ctrl: true })); + assert.equal(f.top().selectedItem.label, 'App menu *'); + await f.choose('Back'); + assert.deepEqual(f.service.list.map(b => b.action), ['none', 'double-click']); + await f.choose('Shift+Esc'); + await f.choose('Remove keybinding'); + assert.deepEqual(f.labels(), ['Ctrl+Q', 'Add keybinding', 'Default keys']); +}); + +test('backing out of the wait calls the capture off; with no keyboard there is nothing to wait for', async () => { + const f = fixture(); + f.open(); + await f.choose('Add keybinding'); + await f.input('double-click'); + assert.equal(f.service.capture, null); assert.equal(f.service.cancelled, 1); assert.equal(f.layers.length, 2); + // A key pressed after that is not taken for a keybinding. + assert.deepEqual(f.service.list, []); + + const away = fixture({ connected: false }); + away.open(); + await away.choose('Add keybinding'); + assert.equal(away.service.capture, null); + assert.match(away.top().body, /No keyboard is connected/); + await away.input('double-click'); + assert.equal(away.layers.length, 2); +}); + +test('the defaults are a page of their own, and a full list says so', async () => { + const f = fixture(); + f.open(); + await f.choose('Default keys'); + assert.equal(f.top().title, 'Default keys'); + for (const phrase of ['Arrow keys', 'Enter', 'Shift+Esc', 'terminal']) assert.ok(f.top().text.includes(phrase), phrase); + + const full = fixture({ room: false }); + full.open(); + await full.choose('Add keybinding'); + full.service.capture(full.chord('F5')); + await full.choose('Select'); + assert.match(full.top().body, /no room/); assert.deepEqual(full.service.list, []); +}); diff --git a/tests/keyboard-bindings.test.cjs b/tests/keyboard-bindings.test.cjs new file mode 100644 index 00000000..8c5359db --- /dev/null +++ b/tests/keyboard-bindings.test.cjs @@ -0,0 +1,302 @@ +const test = require('node:test') +const assert = require('node:assert/strict') +const { + KEY_ACTIONS, MAX_KEY_BINDINGS, GestureScheme, chordId, chordOf, defaultKeyAction, describeChord, gestureRepeats, + isModifierCode, keyActionLabel, keyBindingMap, parseChordId, parseKeyBindings, resolveKey, serializeKeyBindings, +} = require('../.test-build/app/g2/keyboard/bindings.js') +const { KeyRouter } = require('../.test-build/app/g2/keyboard/key-router.js') + +const key = (k, overrides = {}) => ({ + phase: 'down', key: k, code: overrides.code ?? k, repeat: false, timestampMs: 0, + ...overrides, modifiers: { shift: false, ctrl: false, alt: false, meta: false, ...overrides.modifiers }, +}) +const chord = (code, mods = {}) => ({ code, shift: false, ctrl: false, alt: false, meta: false, ...mods }) +const none = new Map() + +test('built-in keys: arrows are the watch\'s swipes, Enter selects, Esc backs out', () => { + const action = (k, overrides) => defaultKeyAction(key(k, overrides)) + assert.deepEqual(action('ArrowLeft'), { action: 'swipe-left', overridesTyping: false }) + assert.deepEqual(action('ArrowRight'), { action: 'swipe-right', overridesTyping: false }) + assert.deepEqual(action('ArrowUp'), { action: 'swipe-up', overridesTyping: false }) + assert.deepEqual(action('ArrowDown'), { action: 'swipe-down', overridesTyping: false }) + assert.deepEqual(action('Enter'), { action: 'click', overridesTyping: false }) + assert.deepEqual(action('Enter', { code: 'NumpadEnter' }), { action: 'click', overridesTyping: false }) + assert.deepEqual(action('Escape'), { action: 'double-click', overridesTyping: false }) + // With a modifier, Escape means "back" even where keys are being typed. + for (const modifier of ['shift', 'ctrl', 'alt']) { + assert.deepEqual(action('Escape', { modifiers: { [modifier]: true } }), { action: 'double-click', overridesTyping: true }) + } + assert.equal(action('Escape', { modifiers: { meta: true } }), null) + assert.equal(action('ArrowLeft', { modifiers: { ctrl: true } }), null) + assert.equal(action('Enter', { modifiers: { alt: true } }), null) + for (const k of ['a', ' ', 'Backspace', 'Tab', 'F1', 'AudioVolumeUp']) assert.equal(action(k), null) +}) + +test('chords: canonical ids round-trip, and read naturally', () => { + const c = chord('Escape', { shift: true, ctrl: true }) + assert.equal(chordId(c), 'Ctrl+Shift+Escape'); assert.deepEqual(parseChordId('Ctrl+Shift+Escape'), c) + assert.deepEqual(parseChordId('ArrowLeft'), chord('ArrowLeft')) + assert.deepEqual(chordOf(key('A', { code: 'KeyA', modifiers: { shift: true } })), chord('KeyA', { shift: true })) + for (const bad of ['', 'Shift+', 'Shift+Shift+KeyA', 'Hyper+KeyA', 'ShiftLeft', 'Ctrl+ControlRight', 'Key A', '+'.repeat(3)]) { + assert.equal(parseChordId(bad), null, bad) + } + assert.equal(isModifierCode('AltRight'), true); assert.equal(isModifierCode('CapsLock'), false) + const said = (code, mods) => describeChord(chord(code, mods)) + assert.equal(said('Escape', { shift: true }), 'Shift+Esc'); assert.equal(said('ArrowLeft'), 'Left') + assert.equal(said('KeyA', { ctrl: true, alt: true }), 'Ctrl+Alt+A'); assert.equal(said('Digit7'), '7') + assert.equal(said('AudioVolumeUp'), 'Volume Up'); assert.equal(said('MediaPlayPause'), 'Play/Pause') + assert.equal(said('NumpadEnter'), 'Numpad Enter'); assert.equal(said('F5'), 'F5'); assert.equal(said('Slash'), '/') + // A remote's unnamed buttons keep the only name they have. + assert.equal(said('Consumer1cb'), 'Media key 0x1cb'); assert.equal(said('Usagea5'), 'Key 0xa5') +}) + +test('custom keybindings survive storage; junk in storage is dropped', () => { + const bindings = [ + { chord: chord('AudioVolumeUp'), action: 'scroll-up' }, + { chord: chord('KeyQ', { ctrl: true }), action: 'long-press' }, + { chord: chord('ArrowLeft'), action: 'none' }, + ] + assert.deepEqual(parseKeyBindings(serializeKeyBindings(bindings)), bindings) + assert.deepEqual([...keyBindingMap(bindings)], [['AudioVolumeUp', 'scroll-up'], ['Ctrl+KeyQ', 'long-press'], ['ArrowLeft', 'none']]) + assert.deepEqual(parseKeyBindings(''), []); assert.deepEqual(parseKeyBindings('{'), []); assert.deepEqual(parseKeyBindings('{"a":1}'), []) + assert.deepEqual(parseKeyBindings(JSON.stringify([ + null, 7, { key: 'KeyA' }, { key: 'KeyA', action: 'explode' }, { key: 'Shift+ShiftLeft', action: 'click' }, + { key: 'KeyB', action: 'click' }, { key: 'KeyB', action: 'double-click' }, + ])), [{ chord: chord('KeyB'), action: 'double-click' }]) + const many = Array.from({ length: 200 }, (_, i) => ({ key: `F${i + 1}`, action: 'click' })) + assert.equal(parseKeyBindings(JSON.stringify(many)).length, MAX_KEY_BINDINGS) + // Every action has a label for the picker, and the list has no duplicates. + assert.equal(new Set(KEY_ACTIONS.map(a => a.action)).size, KEY_ACTIONS.length) + assert.equal(keyActionLabel('double-click'), 'Back'); assert.equal(keyActionLabel('none'), 'Nothing') +}) + +test('resolution: custom first and everywhere, then the built-ins; "Nothing" only removes the built-in', () => { + const custom = keyBindingMap([ + { chord: chord('AudioVolumeUp'), action: 'scroll-up' }, + { chord: chord('ArrowLeft'), action: 'double-click' }, + { chord: chord('ArrowRight'), action: 'none' }, + { chord: chord('Escape', { shift: true }), action: 'none' }, + ]) + // Unbound keys: typed where something takes keys, otherwise the built-in. + assert.deepEqual(resolveKey(key('a', { code: 'KeyA' }), custom), { consume: false, gesture: null }) + assert.deepEqual(resolveKey(key('ArrowUp'), custom), { consume: false, gesture: 'swipe-up' }) + assert.deepEqual(resolveKey(key('Escape'), custom), { consume: false, gesture: 'double-click' }) + assert.deepEqual(resolveKey(key('Escape', { modifiers: { ctrl: true } }), custom), { consume: true, gesture: 'double-click' }) + // Custom: spoken for even in a terminal. + assert.deepEqual(resolveKey(key('AudioVolumeUp'), custom), { consume: true, gesture: 'scroll-up' }) + assert.deepEqual(resolveKey(key('ArrowLeft'), custom), { consume: true, gesture: 'double-click' }) + // The chord is exact: Shift+Left is not the Left keybinding. + assert.deepEqual(resolveKey(key('ArrowLeft', { modifiers: { shift: true } }), custom), { consume: false, gesture: 'swipe-left' }) + // "Nothing": no navigation, still typed; and it can take a built-in override away. + assert.deepEqual(resolveKey(key('ArrowRight'), custom), { consume: false, gesture: null }) + assert.deepEqual(resolveKey(key('Escape', { modifiers: { shift: true } }), custom), { consume: false, gesture: null }) + // Held keys: moving repeats, the rest acts once but stays spoken for. + assert.deepEqual(resolveKey(key('AudioVolumeUp', { repeat: true }), custom), { consume: true, gesture: 'scroll-up' }) + assert.deepEqual(resolveKey(key('ArrowLeft', { repeat: true }), custom), { consume: true, gesture: null }) + assert.deepEqual(resolveKey(key('ArrowDown', { repeat: true }), none), { consume: false, gesture: 'swipe-down' }) + assert.deepEqual(resolveKey(key('ArrowRight', { repeat: true }), none), { consume: false, gesture: null }) + assert.deepEqual(resolveKey(key('Enter', { repeat: true }), none), { consume: false, gesture: null }) + assert.deepEqual(['swipe-left', 'swipe-right', 'click', 'double-click', 'long-press'].filter(gestureRepeats), []) +}) + +test('taps go with whichever scheme the keyboard last moved by', () => { + const scheme = new GestureScheme() + assert.equal(scheme.originFor('click'), 'watch') + assert.equal(scheme.originFor('scroll-down'), 'ring'); assert.equal(scheme.originFor('click'), 'ring') + assert.equal(scheme.originFor('double-click'), 'ring'); assert.equal(scheme.originFor('long-press'), 'ring') + assert.equal(scheme.originFor('swipe-left'), 'watch'); assert.equal(scheme.originFor('click'), 'watch') + assert.equal(scheme.originFor('short-then-long-press'), 'watch') +}) + +function router(options = {}) { + const state = { + ready: true, locked: false, screenOn: true, windowTakesKeys: false, now: 1000, + bindings: keyBindingMap(options.bindings ?? []), gestures: [], offered: [], wakes: 0, activity: 0, pending: [], + hold: false, + } + const r = new KeyRouter({ + ready: () => state.ready, locked: () => state.locked, screenOn: () => state.screenOn, + wake: () => { state.wakes++ }, + offerToWindow: event => { state.offered.push(`${event.phase}:${event.key}`); return state.windowTakesKeys }, + gesture: (gesture, origin) => { + state.gestures.push(`${gesture}/${origin}`) + return state.hold ? new Promise(resolve => state.pending.push(resolve)) : Promise.resolve() + }, + bindings: () => state.bindings, noteActivity: () => { state.activity++ }, log: () => {}, now: () => state.now, + }) + return { r, state } +} +const up = (k, overrides) => key(k, { ...overrides, phase: 'up' }) +const flush = () => new Promise(resolve => setImmediate(resolve)) + +test('router: navigation where nothing takes keys, typing where something does, Shift+Esc out of either', () => { + const { r, state } = router() + r.handleKey(key('ArrowLeft')); r.handleKey(up('ArrowLeft')); r.handleKey(key('Enter')); r.handleKey(key('Escape')) + assert.deepEqual(state.gestures, ['swipe-left/watch', 'click/watch', 'double-click/watch']) + // A terminal session: the same keys are its own, Escape included. + state.windowTakesKeys = true; state.gestures.length = 0; state.offered.length = 0 + for (const k of ['ArrowLeft', 'Enter', 'Escape', 'a']) { r.handleKey(key(k)); r.handleKey(up(k)) } + assert.deepEqual(state.gestures, []) + assert.deepEqual(state.offered, ['down:ArrowLeft', 'up:ArrowLeft', 'down:Enter', 'up:Enter', 'down:Escape', 'up:Escape', 'down:a', 'up:a']) + // Shift+Esc (or Ctrl, or Alt) backs out, and neither its press nor its release is typed. + state.offered.length = 0 + for (const modifier of ['shift', 'ctrl', 'alt']) { + r.handleKey(key('Escape', { modifiers: { [modifier]: true } })); r.handleKey(up('Escape')) + } + assert.deepEqual(state.gestures, ['double-click/watch', 'double-click/watch', 'double-click/watch']) + assert.deepEqual(state.offered, []) +}) + +test('router: a key the window hands back gets its navigation meaning; one it kept does not', () => { + const { r, state } = router() + state.windowTakesKeys = true + r.handleKey(key('ArrowDown')) + assert.deepEqual(state.gestures, []) + // The terminal was showing its session list: the key comes back. + r.handleUnhandledKey(key('ArrowDown')) + assert.deepEqual(state.gestures, ['swipe-down/watch']) + r.handleUnhandledKey(key('a')); r.handleUnhandledKey(up('ArrowDown')) + assert.equal(state.gestures.length, 1) + // Stale by the time it comes back: the display went dark, or locked. + state.screenOn = false; r.handleUnhandledKey(key('ArrowDown')) + state.screenOn = true; state.locked = true; r.handleUnhandledKey(key('ArrowDown')) + assert.equal(state.gestures.length, 1); assert.equal(state.wakes, 0) +}) + +test('router: custom keybindings work in a terminal too, and "Nothing" leaves the key to be typed', () => { + const { r, state } = router({ bindings: [ + { chord: chord('AudioVolumeUp'), action: 'scroll-up' }, + { chord: chord('AudioVolumeDown'), action: 'scroll-down' }, + { chord: chord('MediaPlayPause'), action: 'click' }, + { chord: chord('ArrowLeft'), action: 'none' }, + ] }) + state.windowTakesKeys = true + r.handleKey(key('AudioVolumeDown')); r.handleKey(up('AudioVolumeDown')); r.handleKey(key('MediaPlayPause')); r.handleKey(up('MediaPlayPause')) + // A remote bound to scroll is a ring: its select is a ring's tap. + assert.deepEqual(state.gestures, ['scroll-down/ring', 'click/ring']); assert.deepEqual(state.offered, []) + r.handleKey(key('ArrowLeft')) + assert.deepEqual(state.offered, ['down:ArrowLeft']) + // And where nothing takes keys, "Nothing" is nothing. + state.windowTakesKeys = false; state.gestures.length = 0 + r.handleKey(key('ArrowLeft')); r.handleUnhandledKey(key('ArrowLeft')) + assert.deepEqual(state.gestures, []) +}) + +test('router: a dark display only wakes, a locked one stays locked, and nothing happens off the glasses', () => { + const { r, state } = router() + state.screenOn = false + r.handleKey(key('a')); r.handleKey(key('a', { repeat: true })); r.handleKey(up('a')) + assert.equal(state.wakes, 1); assert.deepEqual(state.gestures, []); assert.deepEqual(state.offered, []) + state.locked = true + r.handleKey(key('Enter')) + assert.equal(state.wakes, 2) + state.screenOn = true + r.handleKey(key('Enter')); r.handleKey(key('Escape', { modifiers: { shift: true } })) + assert.equal(state.wakes, 2); assert.deepEqual(state.gestures, []) + state.locked = false; state.ready = false + r.handleKey(key('Enter')) + assert.deepEqual(state.gestures, []) +}) + +test('router: a held key repeats movement at a rate the display can follow, and never queues behind itself', async () => { + const { r, state } = router() + r.handleKey(key('ArrowDown')) + for (let i = 1; i <= 20; i++) { await flush(); state.now = 1400 + i * 50; r.handleKey(key('ArrowDown', { repeat: true })) } + // Twenty repeats a second arrive; every other one is acted on. + assert.equal(state.gestures.length, 1 + 10) + // While a gesture is still being handled, repeats are dropped rather than queued. + state.gestures.length = 0; state.hold = true; state.now += 1000 + r.handleKey(key('ArrowUp')) + for (let i = 1; i <= 5; i++) { state.now += 200; r.handleKey(key('ArrowUp', { repeat: true })) } + assert.equal(state.gestures.length, 1) + state.pending.splice(0).forEach(resolve => resolve()); await flush() + state.now += 200; r.handleKey(key('ArrowUp', { repeat: true })) + assert.equal(state.gestures.length, 2) + // A fresh press is never dropped, even on the heels of another gesture. + r.handleKey(up('ArrowUp')); r.handleKey(key('Enter')) + assert.equal(state.gestures.at(-1), 'click/watch') +}) + +test('router: capturing a key for a new keybinding takes the chord and nothing else happens', () => { + const { r, state } = router() + const captured = [] + r.captureNextKey(c => captured.push(c)) + // Modifiers alone are waited through; the key pressed with them is the chord. + r.handleKey(key('Shift', { code: 'ShiftLeft', modifiers: { shift: true } })) + assert.deepEqual(captured, []) + r.handleKey(key('Escape', { modifiers: { shift: true } })) + assert.deepEqual(captured, [chord('Escape', { shift: true })]) + // Its release, and the modifier's, do nothing; the next key is ordinary again. + r.handleKey(up('Escape')); r.handleKey(up('Shift', { code: 'ShiftLeft' })) + assert.deepEqual(state.gestures, []); assert.ok(state.activity >= 2) + r.handleKey(key('Enter')) + assert.deepEqual(state.gestures, ['click/watch']) + // Called off: keys go back to meaning what they mean. + const cancel = r.captureNextKey(c => captured.push(c)) + cancel() + r.handleKey(key('ArrowUp')) + assert.equal(captured.length, 1); assert.equal(state.gestures.at(-1), 'swipe-up/watch') + // A second capture replaces the first, and the first one's cancel can't undo it. + const first = r.captureNextKey(() => captured.push('first')) + r.captureNextKey(c => captured.push(c)) + first() + r.handleKey(key('x', { code: 'KeyX' })) + assert.deepEqual(captured.at(-1), chord('KeyX')) +}) + +test('a mouse by default: left taps, right double taps, middle holds, the wheel is the ring\'s scroll', () => { + const action = (k, overrides) => defaultKeyAction(key(k, overrides)) + // Nothing types a mouse button, so these hold in a terminal session too. + assert.deepEqual(action('MouseButton1'), { action: 'click', overridesTyping: true }) + assert.deepEqual(action('MouseButton2'), { action: 'double-click', overridesTyping: true }) + assert.deepEqual(action('MouseButton3'), { action: 'long-press', overridesTyping: true }) + assert.deepEqual(action('WheelUp'), { action: 'scroll-up', overridesTyping: true }) + assert.deepEqual(action('WheelDown'), { action: 'scroll-down', overridesTyping: true }) + // Whatever is held on the keyboard. + assert.deepEqual(action('MouseButton1', { modifiers: { ctrl: true, shift: true } }), { action: 'click', overridesTyping: true }) + // Extra buttons and the horizontal wheel mean nothing until bound. + for (const k of ['MouseButton4', 'MouseButton5', 'WheelLeft', 'WheelRight']) assert.equal(action(k), null) + const said = code => describeChord(chord(code)) + assert.equal(said('MouseButton1'), 'Left button'); assert.equal(said('MouseButton2'), 'Right button') + assert.equal(said('MouseButton3'), 'Middle button'); assert.equal(said('MouseButton4'), 'Mouse button 4') + assert.equal(said('WheelUp'), 'Wheel up'); assert.equal(describeChord(chord('WheelDown', { ctrl: true })), 'Ctrl+Wheel down') + assert.deepEqual(parseChordId('Shift+MouseButton2'), chord('MouseButton2', { shift: true })) +}) + +test('router: the mouse works where keys are being typed, its wheel is paced, and its buttons can be rebound', async () => { + const { r, state } = router({ bindings: [{ chord: chord('MouseButton4'), action: 'short-then-long-press' }, { chord: chord('MouseButton2'), action: 'none' }] }) + const wheel = k => { r.handleKey(key(k, { burst: true })); r.handleKey(up(k, { burst: true })) } + state.windowTakesKeys = true + r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1')) + r.handleKey(key('MouseButton3')); r.handleKey(up('MouseButton3')) + await flush(); state.now += 500 + wheel('WheelDown') + // The wheel is the ring's scroll, and what follows it is the ring's too. + await flush(); state.now += 500 + r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1')) + assert.deepEqual(state.gestures, ['click/watch', 'long-press/watch', 'scroll-down/ring', 'click/ring']) + assert.deepEqual(state.offered, [], 'never typed') + // A custom keybinding for an extra button; "Nothing" on the right button leaves it inert. + state.gestures.length = 0 + r.handleKey(key('MouseButton4')); r.handleKey(up('MouseButton4')); r.handleKey(key('MouseButton2')); r.handleKey(up('MouseButton2')) + assert.deepEqual(state.gestures, ['short-then-long-press/ring']) + // A two-finger scroll reports far faster than the display can follow: every 100 ms at most. + state.gestures.length = 0; state.offered.length = 0 + for (let i = 0; i < 40; i++) { await flush(); state.now += 25; wheel('WheelUp') } + assert.equal(state.gestures.length, 10); assert.ok(state.gestures.every(g => g === 'scroll-up/ring')) + // On a dark display a click wakes it and does nothing else. + state.screenOn = false; state.gestures.length = 0 + r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1')) + assert.equal(state.wakes, 1); assert.deepEqual(state.gestures, []) +}) + +test('router: a mouse button or a turn of the wheel can be captured for a keybinding', () => { + const { r, state } = router() + const captured = [] + r.captureNextKey(c => captured.push(c)) + r.handleKey(key('MouseButton2')); r.handleKey(up('MouseButton2')) + r.captureNextKey(c => captured.push(c)) + r.handleKey(key('WheelUp', { burst: true, modifiers: { shift: true } })); r.handleKey(up('WheelUp', { burst: true })) + assert.deepEqual(captured, [chord('MouseButton2'), chord('WheelUp', { shift: true })]) + assert.deepEqual(state.gestures, []) +}) diff --git a/tests/keyboard-bridge.test.cjs b/tests/keyboard-bridge.test.cjs new file mode 100644 index 00000000..427f97ef --- /dev/null +++ b/tests/keyboard-bridge.test.cjs @@ -0,0 +1,583 @@ +const test = require('node:test') +const assert = require('node:assert/strict') +const { HidKeys, parseReportMap, reportHasKeys, reportHasPointer, reportKeys, reportPointer } = require('../.test-build/app/g2/keyboard/hid.js') +const { + KeyboardRecords, keyboardControl, le16, u16, parseKeyboardAdvert, mergeKeyboardAdverts, + parseKeyboardAddress, formatKeyboardAddress, +} = require('../.test-build/app/g2/keyboard/protocol.js') +const { KeyboardClient, KeyboardClientError, attributeUuid } = require('../.test-build/app/g2/keyboard/client.js') +const { KeyboardBridge } = require('../.test-build/app/g2/keyboard/bridge.js') + +// The HID vectors and the simulated HOGP keyboard follow g2flash demos/keyboard.test.ts. +const boot = Uint8Array.from([ + 0x05,1,0x09,6,0xa1,1,0x05,7,0x19,0xe0,0x29,0xe7,0x15,0,0x25,1,0x75,1,0x95,8,0x81,2, + 0x75,8,0x95,1,0x81,1,0x95,6,0x75,8,0x15,0,0x25,0x65,0x05,7,0x19,0,0x29,0x65,0x81,0,0xc0, +]) +const numbered = Uint8Array.from([ + ...boot.slice(0, 6), 0x85,1,...boot.slice(6, -1), + 0x85,2,0x05,0x0c,0x15,0,0x25,1,0x75,1,0x95,3,0x09,0xe9,0x09,0xea,0x09,0xe2,0x81,2, + 0x75,5,0x95,1,0x81,1,0xc0, +]) +function frame(kind, seq, epoch, request, code, handle, data, offset = 0, total = data.length) { + const p = new Uint8Array(24 + data.length), v = new DataView(p.buffer) + p.set([75,66,1,kind]); v.setUint32(4,seq,true); v.setUint16(8,epoch,true); v.setUint16(10,request,true) + v.setUint16(12,code,true); v.setUint16(14,handle,true); v.setUint16(16,total,true); v.setUint16(18,offset,true) + v.setUint32(20,1234,true); p.set(data,24); return p +} +const flush = () => new Promise(resolve => setImmediate(resolve)) +async function settle(rounds = 20) { for (let i = 0; i < rounds; i++) await flush() } + +test('boot array, modifiers, release, repeats and rollover', () => { + const r = parseReportMap(boot).get(0), keys = new HidKeys() + assert.deepEqual(keys.update(12,r,Uint8Array.of(2,0,4,0,0,0,0,0)), [ + {page:7,usage:0xe1,down:true},{page:7,usage:4,down:true}, + ]) + assert.deepEqual(keys.update(12,r,Uint8Array.of(2,0,4,0,0,0,0,0)), []) + assert.throws(() => keys.update(12,r,Uint8Array.of(0,0,1,1,1,1,1,1)), /rollover/) + assert.deepEqual(keys.reset().map(k => k.down), [false,false]) + assert.throws(() => reportKeys(r,Uint8Array.of(0)), /length/) +}) +test('HOGP report IDs are out of band, with consumer keys', () => { + const maps = parseReportMap(numbered) + assert.deepEqual(reportKeys(maps.get(1),Uint8Array.of(0,0,5,0,0,0,0,0)), [{page:7,usage:5}]) + assert.deepEqual(reportKeys(maps.get(2),Uint8Array.of(5)), [{page:12,usage:0xe9},{page:12,usage:0xe2}]) + assert.throws(() => reportKeys(maps.get(2),Uint8Array.of(2,5)), /length/) +}) +test('NKRO bitmap with non-byte-aligned fields and report union', () => { + const map = Uint8Array.of(5,7,0x19,4,0x29,19,0x15,0,0x25,1,0x75,1,0x95,16,0x81,2) + const r = parseReportMap(map).get(0), keys = new HidKeys() + assert.deepEqual(reportKeys(r,Uint8Array.of(0x81,0x80)), [{page:7,usage:4},{page:7,usage:11},{page:7,usage:19}]) + assert.equal(keys.update(10,r,Uint8Array.of(1,0)).length, 1) + assert.deepEqual(keys.update(11,r,Uint8Array.of(1,0)), []) + assert.deepEqual(keys.update(10,r,Uint8Array.of(0,0)), []) + assert.deepEqual(keys.update(11,r,Uint8Array.of(0,0)), [{page:7,usage:4,down:false}]) +}) +test('array values index usages relative to logical minimum', () => { + const r = parseReportMap(Uint8Array.of(5,7,0x19,4,0x29,6,0x15,1,0x25,3,0x75,8,0x95,1,0x81,0)).get(0) + assert.deepEqual(reportKeys(r,Uint8Array.of(2)), [{page:7,usage:5}]) + assert.deepEqual(reportKeys(r,Uint8Array.of(0)), []) +}) +test('descriptor resource bounds and malformed items', () => { + for (const data of [new Uint8Array(),new Uint8Array(513),Uint8Array.of(0xfe),Uint8Array.of(0x75), + Uint8Array.of(0xb4),Uint8Array.of(0xc0),Uint8Array.of(0x85,0),Uint8Array.of(0x75,33,0x95,1,0x81,2), + Uint8Array.of(0x75,32,0x96,0,2,0x81,2),Uint8Array.of(0xa1,1)]) assert.throws(() => parseReportMap(data)) + // Deterministic malformed corpus; parsing must be bounded and never hang. + let seed = 1 + for (let i = 0; i < 1000; i++) { + const data = Uint8Array.from({length:i%128+1}, () => { seed=(Math.imul(seed,1664525)+1013904223)>>>0; return seed>>>24 }) + try { parseReportMap(data) } catch (e) { assert.ok(e instanceof Error) } + } +}) + +// A wheel mouse as most are described: three buttons, 8-bit X/Y and wheel. +const mouseMap = Uint8Array.from([0x05,1,0x09,2,0xa1,1,0x09,1,0xa1,0,0x05,9,0x19,1,0x29,3,0x15,0,0x25,1,0x75,1,0x95,3,0x81,2, + 0x75,5,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x09,0x38,0x15,0x81,0x25,0x7f,0x75,8,0x95,3,0x81,6,0xc0,0xc0]) +test('a mouse report: buttons, movement and wheel; not keys', () => { + const r = parseReportMap(mouseMap).get(0) + assert.equal(reportHasPointer(r), true); assert.equal(reportHasKeys(r), false) + assert.deepEqual(reportPointer(r, Uint8Array.of(0, 0, 0, 0)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }) + assert.deepEqual(reportPointer(r, Uint8Array.of(0b101, 10, 0xf6, 1)), { buttons: 5, dx: 10, dy: -10, wheel: 1, pan: 0 }) + assert.deepEqual(reportPointer(r, Uint8Array.of(0b010, 0, 0, 0xff)), { buttons: 2, dx: 0, dy: 0, wheel: -1, pan: 0 }) + // Some devices leave trailing bytes off; what is missing is zero. Extra bytes are ignored. + assert.deepEqual(reportPointer(r, Uint8Array.of(1, 3)), { buttons: 1, dx: 3, dy: 0, wheel: 0, pan: 0 }) + assert.deepEqual(reportPointer(r, Uint8Array.of(0, 1, 1, 1, 9, 9)), { buttons: 0, dx: 1, dy: 1, wheel: 1, pan: 0 }) + // -128 is outside the declared range: no reading, rather than a jump. + assert.deepEqual(reportPointer(r, Uint8Array.of(0, 0x80, 0, 0)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }) + assert.deepEqual(reportKeys(r, Uint8Array.of(7, 1, 1, 1)), []) + // A keyboard's report is not a pointer's. + assert.equal(reportHasPointer(parseReportMap(boot).get(0)), false) +}) +test('a touchpad with 12-bit axes and a horizontal wheel; an absolute digitizer is not a mouse', () => { + // Five buttons, 3 bits of padding, X/Y packed 12 bits each, wheel, then AC Pan from the consumer page. + const map = Uint8Array.from([0x05,1,0x09,2,0xa1,1,0x05,9,0x19,1,0x29,5,0x15,0,0x25,1,0x75,1,0x95,5,0x81,2, + 0x75,3,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x16,0x01,0xf8,0x26,0xff,0x07,0x75,12,0x95,2,0x81,6, + 0x09,0x38,0x15,0x81,0x25,0x7f,0x75,8,0x95,1,0x81,6,0x05,0x0c,0x0a,0x38,0x02,0x81,6,0xc0]) + const r = parseReportMap(map).get(0) + assert.equal(reportHasPointer(r), true) + // X = 0x123 (291), Y = 0xfff (-1): packed little endian, 12 bits each. + assert.deepEqual(reportPointer(r, Uint8Array.of(0b10000, 0x23, 0xf1, 0xff, 2, 0xfe)), { buttons: 16, dx: 291, dy: -1, wheel: 2, pan: -2 }) + // The pan field is on the consumer page, but it is a movement, not a key going down. + assert.equal(reportHasKeys(r), false); assert.deepEqual(reportKeys(r, Uint8Array.of(0, 0, 0, 0, 0, 5)), []) + // Absolute X/Y (a touchscreen, a pen) is something else, and is left alone. + const absolute = parseReportMap(Uint8Array.of(0x05,1,0x09,0x30,0x09,0x31,0x15,0,0x26,0xff,0x0f,0x75,16,0x95,2,0x81,2)).get(0) + assert.equal(reportHasPointer(absolute), false) + assert.deepEqual(reportPointer(absolute, Uint8Array.of(1, 2, 3, 4)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }) +}) + +test('control records, UUIDs and addresses', () => { + assert.deepEqual([...keyboardControl(4,0x1234,0x5678,[1,2,3,4,5,6,7])], [75,66,1,4,0x34,0x12,0x78,0x56,1,2,3,4,5,6,7]) + assert.throws(() => keyboardControl(13,1,1)) + assert.throws(() => keyboardControl(11,1,1,new Array(73).fill(0))) + assert.equal(attributeUuid(Uint8Array.of(0xfb,0x34,0x9b,0x5f,0x80,0,0,0x80,0,0x10,0,0,0x12,0x18,0,0)), 0x1812) + const address = parseKeyboardAddress('AA:bb:cc:dd:ee:01') + assert.deepEqual([...address], [1,0xee,0xdd,0xcc,0xbb,0xaa]) + assert.equal(formatKeyboardAddress(address), 'aa:bb:cc:dd:ee:01') + assert.equal(parseKeyboardAddress('aa:bb:cc:dd:ee'), null) + assert.equal(parseKeyboardAddress('aa:bb:cc:dd:ee:gg'), null) +}) +test('fragments, duplicate delivery, gaps, epochs and sequence wrap', () => { + let gaps = 0; const stream = new KeyboardRecords(() => gaps++) + const data = Uint8Array.from({length:200}, (_,i) => i) + assert.equal(stream.push(frame(3,0xffffffff,1,0,13,33,data.slice(0,96),0,200)), undefined) + assert.equal(stream.push(frame(3,0xffffffff,1,0,13,33,data.slice(96,192),96,200)), undefined) + assert.deepEqual(stream.push(frame(3,0xffffffff,1,0,13,33,data.slice(192),192,200)).data, data) + const status = frame(1,0,1,0,0,0,new Uint8Array(10)) + assert.equal(stream.push(status).sequence, 0) + assert.equal(stream.push(status), undefined); assert.equal(gaps, 1) + stream.push(frame(1,2,1,0,0,0,new Uint8Array(10))); assert.equal(gaps, 2) + assert.equal(stream.push(status), undefined); assert.equal(gaps, 2) + stream.push(frame(1,3,2,0,0,0,new Uint8Array(10))); assert.equal(gaps, 3) +}) +test('missing or mismatched fragments never emit partial input', () => { + let gaps = 0; const stream = new KeyboardRecords(() => gaps++) + stream.push(frame(3,1,1,0,13,10,new Uint8Array(96),0,100)) + assert.equal(stream.push(frame(3,1,1,0,13,11,new Uint8Array(4),96,100)), undefined) + assert.equal(gaps, 2) + assert.equal(stream.push(frame(3,1,1,0,13,10,new Uint8Array(4),96,100)), undefined) + assert.equal(stream.push(new Uint8Array(513)), undefined) + // Another protocol version is not ours to decode. + const future = frame(1,5,1,0,0,0,new Uint8Array(10)); future[2] = 2 + assert.equal(stream.push(future), undefined) +}) +test('adverts: name from the scan response, HID from the service list or the appearance', () => { + const body = (ad, rssi = -60) => Uint8Array.from([1, 6,5,4,3,2,1, rssi & 255, 0, ad.length, ...ad]) + const services = parseKeyboardAdvert(body([2,1,6, 3,3,0x12,0x18])) + assert.deepEqual(services, { addressType: 1, address: '01:02:03:04:05:06', rssi: -60, name: '', hid: true }) + const named = parseKeyboardAdvert(body([5,9,0x4b,0x33,0x38,0x30], -48)) + assert.equal(named.name, 'K380'); assert.equal(named.hid, false) + assert.deepEqual(mergeKeyboardAdverts(services, named), { ...services, name: 'K380', rssi: -48 }) + assert.equal(parseKeyboardAdvert(body([3,0x19,0xc1,0x03])).hid, true) + assert.equal(parseKeyboardAdvert(body([3,0x19,0x41,0x03])).hid, false) + assert.equal(parseKeyboardAdvert(Uint8Array.of(1,2,3)), null) + // A truncated AD structure ends the parse without reading past the body. + assert.equal(parseKeyboardAdvert(body([9,9,0x41])).name, '') +}) + +/** + * The firmware bridge and a HOGP keyboard behind it, as far as the phone can + * tell: answers control records with the records the real one sends. + */ +function fakeGlasses(deliver) { + const attrs = new Map() + const add = (h, uuid, bytes) => attrs.set(h, { uuid, value: Uint8Array.from(bytes) }) + const mockMap = Uint8Array.from([...numbered.slice(0,-1),0x85,3,0x05,6,0x09,0x20,0x15,0,0x25,100,0x75,8,0x95,1,0x81,2,0xc0]) + add(1,0x2800,[1,0x18]); add(2,0x2803,[0x20,3,0,5,0x2a]); add(3,0x2a05,[]); add(4,0x2902,[0,0]) + add(20,0x2800,[0x12,0x18]) + add(21,0x2803,[2,22,0,0x4b,0x2a]); add(22,0x2a4b,mockMap); add(23,0x2907,[0x19,0x2a]) + add(24,0x2803,[6,25,0,0x4e,0x2a]); add(25,0x2a4e,[1]) + add(26,0x2803,[0x12,27,0,0x4d,0x2a]); add(27,0x2a4d,[]); add(28,0x2902,[0,0]); add(29,0x2908,[1,1]) + add(30,0x2803,[0x12,31,0,0x4d,0x2a]); add(31,0x2a4d,[]); add(32,0x2902,[0,0]); add(33,0x2908,[2,1]) + add(40,0x2800,[0x0f,0x18]); add(41,0x2803,[0x12,42,0,0x19,0x2a]); add(42,0x2a19,[100]); add(43,0x2902,[0,0]); add(44,0x2908,[3,1]) + const g = { + seq: 0, epoch: 0, enabled: 0, conn: 0, connected: 0, encrypted: 0, scanning: 0, + ops: [], writes: [], watches: [], adverts: [], pins: [], compares: [], + reachable: true, bonded: false, pairing: 'just-works', up: true, + // The keyboard's attribute table, and how its stack answers a direct read + // of a characteristic declaration: 'full', 'short' (the properties byte + // alone) or 'refused'. + attrs, add, declarations: 'full', + } + g.emit = (kind, request, code, handle, data = []) => + deliver(frame(kind, ++g.seq, g.epoch, request, code, handle, Uint8Array.from(data))) + g.status = (request = 0, code = 0) => + g.emit(1, request, code, 0, [g.enabled, g.conn, g.connected, g.encrypted, g.scanning, 0, 0,0,0,0]) + g.dm = (event, status = 0, data = []) => g.emit(4, 0, event | (status << 8), 0, data) + g.close = () => { if (!g.conn) return; g.conn = g.connected = g.encrypted = 0; g.watches = []; g.dm(0x28); g.status() } + g.encrypt = () => { g.encrypted = 1; g.dm(0x2c); g.status() } + g.input = (handle, bytes) => g.emit(3, 0, 13, handle, bytes) + g.send = (record) => { + if (!g.up) return false + const op = record[3], req = u16(record,4), epoch = u16(record,6), h = record.length >= 10 ? u16(record,8) : 0 + g.ops.push(op) + if (op !== 0 && op !== 1 && epoch !== g.epoch) { g.status(req, 4); return true } + if (op === 0) g.status(req) + else if (op === 1) { g.enabled = 1; g.status(req) } + else if (op === 2) { g.enabled = 0; g.status(req); g.close() } + else if (op === 3) { + if (g.conn) { g.status(req, 3); return true } + g.scanning = 1; g.status(req) + for (const advert of g.adverts) g.emit(5, 0, 0, 0, advert) + g.scanning = 0; g.status() + } else if (op === 4) { + if (g.conn) { g.status(req, 3); return true } + g.epoch = (g.epoch + 1) & 0xffff; g.conn = 3; g.status(req) + if (!g.reachable) return true + g.connected = 1; g.dm(0x27); g.status() + if (g.bonded) g.encrypt() + } else if (op === 5) { g.status(req); g.close() } + else if (op === 6) { + if (!g.connected) { g.status(req, 6); return true } + g.status(req) + if (g.pairing === 'just-works') { g.dm(0x2a); g.encrypt() } + else if (g.pairing === 'display') g.dm(0x2e, 0, [0, 1]) + else if (g.pairing === 'entry') g.dm(0x2e, 0, [0, 0]) + else if (g.pairing === 'compare') g.dm(0x35, 0, [0x40, 0xe2, 0x01, 0]) + else if (g.pairing === 'fail') { g.dm(0x2b, 5); g.close() } + } else if (op === 7) { + g.pins.push(record[8] | (record[9] << 8) | (record[10] << 16)); g.status(req); g.dm(0x2a); g.encrypt() + } else if (op === 8) { + g.compares.push(record[8]); g.status(req) + if (record[8]) { g.dm(0x2a); g.encrypt() } else { g.dm(0x2b, 4); g.close() } + } else if (op === 9) { + const batch = [...attrs].filter(([handle]) => handle >= h).slice(0,4) + g.emit(2, req, batch.length ? 2 : (10<<8)|2, h, batch.length ? [1, ...batch.flatMap(([handle,a]) => [...le16(handle), ...le16(a.uuid)])] : []) + } else if (op === 10) { + const attr = attrs.get(h), offset = u16(record,10) + if (attr.uuid === 0x2803 && g.declarations === 'refused') { g.emit(2, req, (2<<8)|5, h); return true } + const data = attr.uuid === 0x2803 && g.declarations === 'short' ? attr.value.slice(0,1) : attr.value + g.emit(2, req, offset >= data.length ? (7<<8)|6 : offset ? 6 : 5, h, data.slice(offset, offset+20)) + } else if (op === 11) { g.writes.push(h); g.emit(2, req, record[10] ? 10 : 9, h) } + else if (op === 12) { g.watches = Array.from({length:record[8]}, (_,i) => u16(record,9+2*i)); g.status(req) } + else throw new Error(`Unexpected op ${op}`) + return true + } + return g +} + +test('client: HOGP discovery, long Report Map, subscriptions, typing and loss recovery', async () => { + const events = [] + let client + const g = fakeGlasses(payload => client.accept(payload)) + client = new KeyboardClient(g.send, { keys: e => events.push(...e) }) + await client.enable(); await client.connect(1, new Uint8Array(6)); const config = await client.configure() + // The battery level (an external report) is found, but it can't carry a + // key, so it isn't subscribed: only the two key reports and Service Changed. + assert.deepEqual(g.watches, [27,31,3]); assert.deepEqual(g.writes, [25,4,28,32]) + assert.deepEqual(config.reports.map(r => r.id), [1,2,3]); assert.deepEqual(config.watched, [27,31]) + assert.deepEqual(config.reports.map(r => r.properties), [0x12,0x12,0x12]) + g.input(27, [0,0,4,0,0,0,0,0]); g.input(31, [1]) + assert.deepEqual(events.map(e => [e.usage,e.down]), [[4,true],[0xe9,true]]) + g.seq++ // a release lost in the firmware queue or on the way to the phone + g.status() + assert.deepEqual(events.slice(-2).map(e => e.down), [false,false]) + // Another client on the same connection can take over with the mapping. + const restored = new KeyboardClient(() => true, { keys: e => events.push(...e) }) + restored.restore(config); restored.accept(frame(3, g.seq + 1, g.epoch, 0, 13, 27, Uint8Array.of(0,0,5,0,0,0,0,0))) + assert.deepEqual([events.at(-1).usage, events.at(-1).down], [5, true]); restored.lostPhoneLink() + assert.deepEqual([events.at(-1).usage, events.at(-1).down], [5, false]) + // Service Changed voids the handles: no more keys until configure() runs again. + g.emit(3, 0, 14, 3, [1,0,255,255]) + const count = events.length; g.input(27, [0,0,6,0,0,0,0,0]) + assert.equal(events.length, count) + client.close(); restored.close() +}) +// A pointer's report, as a keyboard with a built-in trackpad adds: buttons and relative X/Y. +const mouseCollection = [0x05,1,0x09,2,0xa1,1,0x85,4,0x09,1,0xa1,0,0x05,9,0x19,1,0x29,3,0x15,0,0x25,1,0x75,1,0x95,3,0x81,2, + 0x75,5,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x15,0x81,0x25,0x7f,0x75,8,0x95,2,0x81,6,0xc0,0xc0] +/** Turn the fake into a keyboard with a trackpad, whose battery level has no Report Reference. */ +function makeCombo(g) { + g.add(22, 0x2a4b, [...g.attrs.get(22).value, ...mouseCollection]) + g.add(34,0x2803,[0x12,35,0,0x4d,0x2a]); g.add(35,0x2a4d,[]); g.add(36,0x2902,[0,0]); g.add(37,0x2908,[4,1]) + g.attrs.delete(44) + const sorted = [...g.attrs].sort((a, b) => a[0] - b[0]) + g.attrs.clear(); for (const [handle, attr] of sorted) g.attrs.set(handle, attr) +} +for (const declarations of ['short', 'refused']) { + test(`client: a keyboard with a trackpad whose stack answers declaration reads ${declarations} types and points`, async () => { + const events = [], pointer = [], logs = [] + let client + const g = fakeGlasses(payload => client.accept(payload)) + makeCombo(g); g.declarations = declarations + client = new KeyboardClient(g.send, { keys: e => events.push(...e), pointer: p => pointer.push(p), log: line => logs.push(line) }) + await client.enable(); await client.connect(1, new Uint8Array(6)); const config = await client.configure() + // Keys, media keys and the trackpad are subscribed; the battery level is not. + assert.deepEqual(config.watched, [27,31,35]); assert.equal(config.pointer, true) + assert.deepEqual(g.watches, [27,31,35,3]); assert.deepEqual(g.writes, [25,4,28,32,36]) + assert.deepEqual(config.reports.map(r => [r.handle, r.id]), [[27,1],[31,2],[35,4]]) + assert.deepEqual(config.reports.map(r => r.properties), declarations === 'short' ? [0x12,0x12,0x12] : [undefined,undefined,undefined]) + g.input(27, [0,0,4,0,0,0,0,0]) + assert.deepEqual(events.map(e => [e.usage,e.down]), [[4,true]]) + // The trackpad: movement and buttons, and never a key. + g.input(35, [0, 5, 0xfd]); g.input(35, [1, 0, 0]); g.input(35, [5, 0x81, 0x7f]) + assert.deepEqual(pointer, [ + { buttons: 0, dx: 5, dy: -3, wheel: 0, pan: 0 }, + { buttons: 1, dx: 0, dy: 0, wheel: 0, pan: 0 }, + { buttons: 5, dx: -127, dy: 127, wheel: 0, pan: 0 }, + ]) + assert.equal(events.length, 1) + // A record lost on the way: the held buttons are let go along with the held keys. + g.seq++; g.status() + assert.deepEqual(pointer.at(-1), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }) + assert.deepEqual([events.at(-1).usage, events.at(-1).down], [4, false]) + // With nothing held, another gap has nothing to say. + const count = pointer.length; g.seq++; g.status() + assert.equal(pointer.length, count) + // What was skipped, and why, is in the log; so is the table it came from. + assert.ok(logs.some(line => /^gatt 1=2800 2=2803 /.test(line)), logs.join('\n')) + assert.ok(logs.some(line => /report 42 skipped: no Report Reference/.test(line)), logs.join('\n')) + assert.ok(logs.some(line => /report 35 \(id 4\) is a pointer's/.test(line)), logs.join('\n')) + assert.ok(logs.some(line => line.startsWith('report map 22: 0501'))) + client.close() + }) +} +test('client: one unusable report is skipped, and a device with no key report is refused with the reasons', async () => { + let client + const g = fakeGlasses(payload => client.accept(payload)) + client = new KeyboardClient(g.send) + g.add(29, 0x2908, [1]) // a Report Reference of the wrong length + await client.enable(); await client.connect(1, new Uint8Array(6)) + assert.deepEqual((await client.configure()).watched, [31]) + g.add(33, 0x2908, [9,1]) // and one naming a report the map doesn't have + await assert.rejects(client.configure(), /No usable keyboard input report \(27: Report Reference 29 is 01; 31: input report id 9 is not in the Report Map\)/) + assert.equal(client.configuration, undefined) + // Not a HID device at all. + g.add(20, 0x2800, [0x0f,0x18]) + await assert.rejects(client.configure(), /no HID service/) + client.close() +}) +test('client: the link dropping during setup ends it, rather than being skipped over', async () => { + let client + const g = fakeGlasses(payload => client.accept(payload)) + client = new KeyboardClient(g.send) + await client.enable(); await client.connect(1, new Uint8Array(6)) + const send = g.send + g.send = (record) => { + // The keyboard goes away as the first Report Reference is read. + if (record[3] === 10 && u16(record, 8) === 29) { g.close(); return true } + return send(record) + } + client = new KeyboardClient(record => g.send(record)) + await client.query() + await assert.rejects(client.configure(), /stream gap|link changed/) + client.close() +}) +test('client: an unreachable keyboard fails the connect when the glasses give up, not by timeout', async () => { + let client + const g = fakeGlasses(payload => client.accept(payload)); g.reachable = false + client = new KeyboardClient(g.send) + await client.enable() + const pending = client.connect(1, new Uint8Array(6)) + await settle(); g.close() + await assert.rejects(pending, /didn't answer/) + client.close() +}) +test('client: firmware errors carry their code, and a dead link rejects at once', async () => { + let client + const g = fakeGlasses(payload => client.accept(payload)) + client = new KeyboardClient(g.send) + await client.enable(); await client.connect(1, new Uint8Array(6)) + await assert.rejects(client.command(4, [1,0,0,0,0,0,0]), e => e instanceof KeyboardClientError && e.code === 3) + g.up = false + await assert.rejects(client.query(), /aren't ready/) + client.close() + await assert.rejects(client.query(), /closed/) +}) + +function bridgeSetup(saved = null) { + const host = { keys: [], pointer: [], gone: 0, changes: 0, logs: [], prompts: [], saved, reconnect: true } + let bridge + const g = fakeGlasses(payload => bridge.accept(payload)) + bridge = new KeyboardBridge({ + send: g.send, + loadSaved: () => host.saved, + storeSaved: value => { host.saved = value }, + onKeys: events => host.keys.push(...events), + onPointer: sample => host.pointer.push(sample), + onKeyboardGone: () => { host.gone++ }, + onChanged: () => { + host.changes++ + const pairing = bridge.snapshot().pairing + if (pairing && host.prompts.at(-1) !== pairing) host.prompts.push(pairing) + }, + log: line => host.logs.push(line), + randomPasskey: () => 123456, + shouldReconnect: () => host.reconnect, + }) + return { bridge, g, host } +} +const K380 = { address: 'aa:bb:cc:dd:ee:01', addressType: 1, name: 'K380' } +const advertOf = (lastByte, ad) => [1, lastByte,0xee,0xdd,0xcc,0xbb,0xaa, 0xc4, 0, ad.length, ...ad] + +test('bridge: scan, pick, pair by typing the displayed passkey, type, forget', async () => { + const { bridge, g, host } = bridgeSetup() + assert.equal(bridge.snapshot().state, 'unavailable') + await assert.rejects(bridge.scan(), /Connect to G2 glasses/) + bridge.linkReady() + g.adverts = [ + advertOf(2, [2,1,6]), // some other device + advertOf(1, [2,1,6, 3,3,0x12,0x18]), // the keyboard's advertisement + advertOf(1, [5,9,0x4b,0x33,0x38,0x30]), // and its scan response, with the name + ] + const found = await bridge.scan() + assert.deepEqual(found.map(a => [a.address, a.name, a.hid]), [[K380.address,'K380',true],['aa:bb:cc:dd:ee:02','',false]]) + assert.equal(bridge.snapshot().state, 'idle') + + g.pairing = 'display' + await bridge.connect(K380) + // The passkey went up for the user to type, and to the glasses as the answer. + assert.deepEqual(host.prompts, [{ kind: 'display', pin: 123456 }]) + assert.deepEqual(g.pins, [123456]) + assert.equal(bridge.snapshot().state, 'connected'); assert.equal(bridge.snapshot().pairing, null) + assert.deepEqual(host.saved, K380); assert.ok(bridge.isConnected()) + + g.input(27, [2,0,4,0,0,0,0,0]) + assert.deepEqual(host.keys.map(k => [k.usage, k.down]), [[0xe1,true],[4,true]]) + await bridge.forget() + assert.equal(host.saved, null); assert.equal(host.gone, 1); assert.equal(g.conn, 0); assert.equal(g.enabled, 0) + assert.equal(bridge.snapshot().state, 'idle') + bridge.linkLost() +}) +test('bridge: numeric comparison waits for the answer; declining fails the pairing', async () => { + const { bridge, g } = bridgeSetup() + bridge.linkReady(); g.pairing = 'compare' + const pending = bridge.connect(K380) + await settle() + assert.deepEqual(bridge.snapshot().pairing, { kind: 'compare', value: 123456 }) + assert.equal(bridge.snapshot().state, 'pairing') + await bridge.answerCompare(false) + await assert.rejects(pending, /Pairing with the keyboard failed/) + assert.equal(bridge.snapshot().state, 'error'); assert.deepEqual(g.compares, [0]) + await assert.rejects(bridge.answerCompare(true), /No pairing comparison/) + bridge.linkLost() +}) +test('bridge: a keyboard that shows a code is paired with the code typed in', async () => { + const { bridge, g } = bridgeSetup() + bridge.linkReady(); g.pairing = 'entry' + const pending = bridge.connect(K380) + await settle() + assert.deepEqual(bridge.snapshot().pairing, { kind: 'entry' }) + await bridge.answerPasskey(42) + await pending + assert.deepEqual(g.pins, [42]); assert.equal(bridge.snapshot().state, 'connected') + bridge.linkLost() +}) +test('bridge: the remembered keyboard comes back with the link, and again after it drops', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }) + const { bridge, g, host } = bridgeSetup(K380) + g.bonded = true + bridge.linkReady() + assert.equal(bridge.snapshot().waiting, true) + t.mock.timers.tick(1000); await settle() + assert.equal(bridge.snapshot().state, 'connected'); assert.equal(g.connected, 1) + + // The keyboard goes to sleep: held keys are dropped, and it is looked for again. + g.input(27, [0,0,4,0,0,0,0,0]) + g.close(); await settle() + assert.equal(host.gone, 1); assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().waiting, true) + g.reachable = false + t.mock.timers.tick(2000); await settle() + assert.equal(bridge.snapshot().state, 'connecting') + g.close(); await settle() + // Asleep is the ordinary case, not an error in front of the user. + assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().error, '') + g.reachable = true + t.mock.timers.tick(2000); await settle() + assert.equal(bridge.snapshot().state, 'connected') + + // Disconnecting by hand stops the retries until asked again. + await bridge.disconnect() + assert.equal(bridge.snapshot().waiting, false); assert.equal(g.enabled, 0) + t.mock.timers.tick(60000); await settle() + assert.equal(bridge.snapshot().state, 'idle'); assert.equal(g.connected, 0) + bridge.linkLost() +}) +test('bridge: the glasses kept the keyboard through a phone-side drop, so only discovery is redone', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }) + const { bridge, g, host } = bridgeSetup(K380) + g.bonded = true + bridge.linkReady(); t.mock.timers.tick(1000); await settle() + assert.equal(bridge.snapshot().state, 'connected') + const epoch = g.epoch + bridge.linkLost() + assert.equal(bridge.snapshot().state, 'unavailable'); assert.equal(host.gone, 1) + g.ops.length = 0 + bridge.linkReady(); t.mock.timers.tick(1000); await settle() + assert.equal(bridge.snapshot().state, 'connected') + assert.equal(g.epoch, epoch, 'no reconnect of the keyboard itself') + assert.ok(!g.ops.includes(4) && !g.ops.includes(2)) + g.input(27, [0,0,7,0,0,0,0,0]) + assert.deepEqual([host.keys.at(-1).usage, host.keys.at(-1).down], [7, true]) + bridge.linkLost() +}) +test('bridge: a scan started by hand takes over from an attempt in flight, and nothing retries while charging', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }) + const { bridge, g, host } = bridgeSetup(K380) + g.reachable = false + bridge.linkReady(); t.mock.timers.tick(1000); await settle() + assert.equal(bridge.snapshot().state, 'connecting'); assert.equal(g.conn, 3) + g.adverts = [advertOf(1, [3,3,0x12,0x18])] + const scan = bridge.scan() + await settle(); t.mock.timers.tick(200); await settle() + assert.equal((await scan).length, 1) + assert.equal(g.conn, 0, 'the pending connection was released first') + assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().busy, false) + + // Reconnecting waits for the glasses to be in use. + host.reconnect = false + bridge.linkLost(); bridge.linkReady(); g.ops.length = 0 + t.mock.timers.tick(1000); await settle() + t.mock.timers.tick(120000); await settle() + assert.deepEqual(g.ops, []) + // A bridge left enabled by an attempt is switched off for the wait, so the + // firmware's timer and its once-a-second status stop. + host.reconnect = true; g.reachable = false + bridge.retryNow(); t.mock.timers.tick(0); await settle() + assert.equal(g.enabled, 1); assert.equal(bridge.snapshot().state, 'connecting') + g.close(); await settle() + host.reconnect = false + t.mock.timers.tick(2000); await settle() + assert.equal(g.enabled, 0) + g.ops.length = 0 + t.mock.timers.tick(120000); await settle() + assert.deepEqual(g.ops, []) + // The display woke: no waiting out the rest of the backoff. + host.reconnect = true; g.reachable = true; g.bonded = true + bridge.retryNow() + t.mock.timers.tick(0); await settle() + assert.equal(bridge.snapshot().state, 'connected') + // With the keyboard in use, or none remembered, there is nothing to retry. + g.ops.length = 0 + bridge.retryNow(); t.mock.timers.tick(0); await settle() + assert.deepEqual(g.ops, []) + bridge.linkLost() +}) +test('bridge: a keyboard\'s touchpad is passed on while it is in use, and the snapshot says it has one', async () => { + const plain = bridgeSetup() + plain.bridge.linkReady(); await plain.bridge.connect(K380) + assert.equal(plain.bridge.snapshot().pointer, false) + plain.bridge.linkLost() + + const { bridge, g, host } = bridgeSetup() + makeCombo(g) + bridge.linkReady() + assert.equal(bridge.snapshot().pointer, false) + await bridge.connect(K380) + assert.equal(bridge.snapshot().pointer, true) + assert.ok(host.logs.some(line => /keyboard ready: K380, 3 of 3 input reports subscribed, with a pointer/.test(line)), host.logs.join('\n')) + g.input(35, [2, 1, 1]) + assert.deepEqual(host.pointer, [{ buttons: 2, dx: 1, dy: 1, wheel: 0, pan: 0 }]) + // The keyboard drops with a button down: the button is let go, as held keys + // are, before the host hears the keyboard is gone; and the snapshot follows. + g.close(); await settle() + assert.deepEqual(host.pointer.at(-1), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }); assert.equal(host.pointer.length, 2) + assert.equal(host.gone, 1); assert.equal(bridge.snapshot().pointer, false) + bridge.linkLost() +}) +test('bridge: leaving on purpose tells the glasses to let the keyboard go', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }) + const { bridge, g } = bridgeSetup(K380) + g.bonded = true + bridge.linkReady(); t.mock.timers.tick(1000); await settle() + assert.equal(g.connected, 1) + bridge.linkClosing(); bridge.linkLost() + assert.equal(g.enabled, 0); assert.equal(g.conn, 0) + // With no keyboard in use there is nothing to say. + const idle = bridgeSetup() + idle.bridge.linkReady(); idle.bridge.linkClosing(); idle.bridge.linkLost() + assert.deepEqual(idle.g.ops, []) +}) +test('bridge: a keyboard forgotten while the glasses were away is released when they return', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }) + const { bridge, g, host } = bridgeSetup(K380) + g.bonded = true + bridge.linkReady(); t.mock.timers.tick(1000); await settle() + assert.equal(g.connected, 1) + bridge.linkLost() + await bridge.forget() + assert.equal(host.saved, null); assert.equal(g.connected, 1, 'out of reach, so still attached') + bridge.linkReady(); await settle() + assert.equal(g.enabled, 0); assert.equal(g.conn, 0) + t.mock.timers.tick(60000); await settle() + assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().waiting, false) + bridge.linkLost() +}) diff --git a/tests/keyboard-keys.test.cjs b/tests/keyboard-keys.test.cjs new file mode 100644 index 00000000..d6faa494 --- /dev/null +++ b/tests/keyboard-keys.test.cjs @@ -0,0 +1,165 @@ +const test = require('node:test') +const assert = require('node:assert/strict') +const { KeyTranslator, translateUsage } = require('../.test-build/app/g2/keyboard/keys.js') +const { encodeTerminalKey, terminalScrollbackKey } = require('../.test-build/app/apps/terminal/terminal-keys.js') + +const K = 7 +const down = (usage, page = K) => ({ page, usage, down: true }) +const up = (usage, page = K) => ({ page, usage, down: false }) +const SHIFT = 225, RSHIFT = 229, CTRL = 224, ALT = 226, META = 227, CAPS = 57 +const A = 4, ONE = 30, ENTER = 40, SLASH = 56 + +function translator() { + const events = [], timers = new Map() + let nextTimer = 1, now = 1000 + const t = new KeyTranslator({ + emit: e => events.push(e), + now: () => now, + setTimer: (callback, delayMs) => { timers.set(nextTimer, { callback, at: now + delayMs }); return nextTimer++ }, + clearTimer: handle => timers.delete(handle), + }) + const advance = ms => { + const end = now + ms + for (;;) { + const due = [...timers].filter(([, timer]) => timer.at <= end).sort((a, b) => a[1].at - b[1].at)[0] + if (!due) break + timers.delete(due[0]); now = due[1].at; due[1].callback() + } + now = end + } + return { t, events, timers, advance } +} +const key = (overrides = {}) => ({ + phase: 'down', key: 'a', code: 'KeyA', text: 'a', repeat: false, timestampMs: 0, + ...overrides, modifiers: { shift: false, ctrl: false, alt: false, meta: false, ...overrides.modifiers }, +}) + +test('US layout: letters, digits, punctuation, keypad, function and consumer keys', () => { + assert.deepEqual(translateUsage(K, A, false, false), { key: 'a', code: 'KeyA', text: 'a' }) + assert.deepEqual(translateUsage(K, A, true, false), { key: 'A', code: 'KeyA', text: 'A' }) + assert.deepEqual(translateUsage(K, A, true, true), { key: 'a', code: 'KeyA', text: 'a' }) + assert.deepEqual(translateUsage(K, ONE, true, true), { key: '!', code: 'Digit1', text: '!' }) + assert.deepEqual(translateUsage(K, SLASH, true, false), { key: '?', code: 'Slash', text: '?' }) + assert.deepEqual(translateUsage(K, 44, false, false), { key: ' ', code: 'Space', text: ' ' }) + assert.deepEqual(translateUsage(K, 89, false, false), { key: '1', code: 'Numpad1', text: '1' }) + assert.deepEqual(translateUsage(K, 88, false, false), { key: 'Enter', code: 'NumpadEnter' }) + assert.deepEqual(translateUsage(K, 69, false, false), { key: 'F12', code: 'F12' }) + assert.deepEqual(translateUsage(K, 104, false, false), { key: 'F13', code: 'F13' }) + assert.deepEqual(translateUsage(12, 0xe9, false, false), { key: 'AudioVolumeUp', code: 'AudioVolumeUp' }) + assert.equal(translateUsage(K, 200, false, false).key, 'Unidentified') +}) +test('modifiers follow the stream, on either side, and chords carry no text', () => { + const { t, events } = translator() + t.handle([down(SHIFT), down(A)]) + assert.deepEqual(events.map(e => [e.phase, e.key, e.text]), [['down', 'Shift', undefined], ['down', 'A', 'A']]) + assert.equal(events[1].modifiers.shift, true) + // The other Shift takes over; releasing the first leaves it held. + t.handle([down(RSHIFT), up(SHIFT), up(A), down(A)]) + assert.equal(events.at(-1).key, 'A') + t.handle([up(RSHIFT), up(A), down(CTRL), down(A)]) + assert.deepEqual([events.at(-1).key, events.at(-1).text, events.at(-1).modifiers.ctrl], ['a', undefined, true]) + t.handle([up(CTRL), up(A), down(ALT), down(A)]) + assert.equal(events.at(-1).text, undefined) + t.handle([up(ALT), up(A), down(META), down(A)]) + assert.equal(events.at(-1).text, undefined) + // A duplicate report changes nothing. + const count = events.length; t.handle([down(A)]); assert.equal(events.length, count) + t.handle([up(200)]); assert.equal(events.length, count) +}) +test('Caps Lock toggles letters only', () => { + const { t, events } = translator() + t.handle([down(CAPS), up(CAPS), down(A), up(A), down(ONE), up(ONE), down(SHIFT), down(A)]) + const typed = events.filter(e => e.phase === 'down' && e.text).map(e => e.text) + assert.deepEqual(typed, ['A', '1', 'a']) +}) +test('a held key repeats until it is released; the newest key is the one that repeats', () => { + const { t, events, timers, advance } = translator() + t.handle([down(A)]) + advance(399); assert.equal(events.length, 1) + advance(1); assert.deepEqual([events.at(-1).key, events.at(-1).repeat], ['a', true]) + advance(100); assert.equal(events.filter(e => e.repeat).length, 3) + t.handle([down(ONE)]); advance(400) + assert.deepEqual([events.at(-1).key, events.at(-1).repeat], ['1', true]) + // Releasing the older key leaves the newer one repeating. + t.handle([up(A)]); const before = events.length; advance(50) + assert.equal(events.length, before + 1) + t.handle([up(ONE)]); const after = events.length; advance(1000) + assert.equal(events.length, after); assert.equal(timers.size, 0) + // Modifiers never repeat. + t.handle([down(SHIFT)]); advance(2000); assert.equal(events.filter(e => e.key === 'Shift').length, 1) +}) +test('reset forgets held keys and modifiers without emitting, and stops the repeat', () => { + const { t, events, timers, advance } = translator() + t.handle([down(SHIFT), down(CAPS), down(A)]) + const count = events.length + t.reset(); advance(5000) + assert.equal(events.length, count); assert.equal(timers.size, 0) + t.handle([down(A)]) + assert.deepEqual([events.at(-1).key, events.at(-1).modifiers.shift], ['a', false]) +}) + +test('terminal: printable keys, Enter, Tab, Backspace, Escape', () => { + const enc = (overrides, appCursor = false) => encodeTerminalKey(key(overrides), appCursor) + assert.equal(enc({}), 'a') + assert.equal(enc({ key: 'A', text: 'A', modifiers: { shift: true } }), 'A') + assert.equal(enc({ key: 'Enter', text: undefined }), '\r') + assert.equal(enc({ key: 'Tab', text: undefined }), '\t') + assert.equal(enc({ key: 'Tab', text: undefined, modifiers: { shift: true } }), '\x1b[Z') + assert.equal(enc({ key: 'Backspace', text: undefined }), '\x7f') + assert.equal(enc({ key: 'Backspace', text: undefined, modifiers: { ctrl: true } }), '\x08') + assert.equal(enc({ key: 'Escape', text: undefined }), '\x1b') + assert.equal(enc({ key: ' ', text: ' ' }), ' ') + assert.equal(enc({ phase: 'up' }), null) + assert.equal(enc({ key: 'Shift', code: 'ShiftLeft', text: undefined, modifiers: { shift: true } }), null) + assert.equal(enc({ key: 'AudioVolumeUp', text: undefined }), null) + assert.equal(enc({ modifiers: { meta: true }, text: undefined }), null) + // A repeat types again. + assert.equal(enc({ repeat: true }), 'a') +}) +test('terminal: Ctrl chords are control characters, Alt prefixes ESC', () => { + const enc = (k, modifiers) => encodeTerminalKey(key({ key: k, text: undefined, modifiers }), false) + assert.equal(enc('c', { ctrl: true }), '\x03'); assert.equal(enc('C', { ctrl: true, shift: true }), '\x03') + assert.equal(enc('z', { ctrl: true }), '\x1a'); assert.equal(enc('[', { ctrl: true }), '\x1b') + assert.equal(enc(' ', { ctrl: true }), '\x00'); assert.equal(enc('/', { ctrl: true }), '\x7f') + assert.equal(enc('=', { ctrl: true }), null) + assert.equal(enc('f', { alt: true }), '\x1bf'); assert.equal(enc('F', { alt: true, shift: true }), '\x1bF') + assert.equal(enc('c', { ctrl: true, alt: true }), '\x1b\x03') + assert.equal(enc('Enter', { alt: true }), '\x1b\r'); assert.equal(enc('Backspace', { alt: true }), '\x1b\x7f') +}) +test('terminal: cursor keys follow DECCKM; modified ones and function keys use the xterm forms', () => { + const enc = (k, modifiers, appCursor = false) => encodeTerminalKey(key({ key: k, text: undefined, modifiers }), appCursor) + assert.equal(enc('ArrowUp'), '\x1b[A'); assert.equal(enc('ArrowUp', {}, true), '\x1bOA') + assert.equal(enc('ArrowLeft', { ctrl: true }), '\x1b[1;5D'); assert.equal(enc('ArrowLeft', { ctrl: true }, true), '\x1b[1;5D') + assert.equal(enc('ArrowRight', { shift: true, alt: true }), '\x1b[1;4C') + assert.equal(enc('Home'), '\x1b[H'); assert.equal(enc('End', {}, true), '\x1bOF') + assert.equal(enc('PageUp'), '\x1b[5~'); assert.equal(enc('Delete'), '\x1b[3~'); assert.equal(enc('Delete', { ctrl: true }), '\x1b[3;5~') + assert.equal(enc('F1'), '\x1bOP'); assert.equal(enc('F4', { shift: true }), '\x1b[1;2S') + assert.equal(enc('F5'), '\x1b[15~'); assert.equal(enc('F12'), '\x1b[24~'); assert.equal(enc('F13'), null) +}) +test('terminal: Shift+PageUp and Shift+PageDown page the scrollback instead', () => { + const page = (k, modifiers, phase = 'down') => terminalScrollbackKey(key({ key: k, text: undefined, modifiers, phase })) + assert.equal(page('PageUp', { shift: true }), -1); assert.equal(page('PageDown', { shift: true }), 1) + assert.equal(page('PageUp', {}), null); assert.equal(page('PageUp', { shift: true, ctrl: true }), null) + assert.equal(page('PageUp', { shift: true }, 'up'), null); assert.equal(page('ArrowUp', { shift: true }), null) +}) + +test('mouse buttons are keys that neither repeat nor take the repeat from a held key; a wheel detent is a press and release', () => { + const { t, events, advance } = translator() + t.handle([down(A)]) + t.handle([{ page: 9, usage: 1, down: true }]) + assert.deepEqual([events.at(-1).key, events.at(-1).code, events.at(-1).text], ['MouseButton1', 'MouseButton1', undefined]) + // The held key goes on repeating; the held button never does. + advance(500) + assert.ok(events.filter(e => e.repeat).every(e => e.key === 'a')); assert.ok(events.some(e => e.repeat)) + t.handle([up(A), { page: 9, usage: 1, down: false }]) + assert.deepEqual([events.at(-1).phase, events.at(-1).key], ['up', 'MouseButton1']) + assert.equal(translateUsage(9, 3, false, false).code, 'MouseButton3') + + // The wheel: down and up at once, marked as droppable, with the keyboard's modifiers. + events.length = 0 + t.handle([down(SHIFT)]); t.pulse('WheelUp') + assert.deepEqual(events.slice(1).map(e => [e.phase, e.key, e.code, e.burst, e.repeat, e.modifiers.shift]), + [['down', 'WheelUp', 'WheelUp', true, false, true], ['up', 'WheelUp', 'WheelUp', true, false, true]]) + const count = events.length; advance(2000) + assert.equal(events.length, count) +}) diff --git a/tests/keyboard-pointer.test.cjs b/tests/keyboard-pointer.test.cjs new file mode 100644 index 00000000..e71a36e4 --- /dev/null +++ b/tests/keyboard-pointer.test.cjs @@ -0,0 +1,58 @@ +const test = require('node:test') +const assert = require('node:assert/strict') +const { PointerInput } = require('../.test-build/app/g2/keyboard/pointer.js') + +const sample = (buttons, extra = {}) => ({ buttons, dx: 0, dy: 0, wheel: 0, pan: 0, ...extra }) +function pointer() { + const out = [] + const input = new PointerInput({ + buttons: events => out.push(...events.map(e => `${e.down ? 'down' : 'up'}:${e.page}:${e.usage}`)), + wheel: key => out.push(key), + }) + return { input, out } +} + +test('buttons become key transitions; the wheel a key per report; movement nothing', () => { + const { input, out } = pointer() + input.handle(sample(0, { dx: 40, dy: -3 })) + assert.deepEqual(out, []) + input.handle(sample(0b001)); input.handle(sample(0b001, { dx: 2 })); input.handle(sample(0b110)); input.handle(sample(0)) + assert.deepEqual(out, ['down:9:1', 'up:9:1', 'down:9:2', 'down:9:3', 'up:9:2', 'up:9:3']) + out.length = 0 + // However many detents a report claims, it is one step; a two-finger scroll sends many. + input.handle(sample(0, { wheel: 1 })); input.handle(sample(0, { wheel: -3 })); input.handle(sample(0, { pan: 2 })); input.handle(sample(0, { pan: -1 })) + assert.deepEqual(out, ['WheelUp', 'WheelDown', 'WheelRight', 'WheelLeft']) +}) + +test('taking the pointer: samples go to the taker, held buttons are let go first, and its clicks are nobody else\'s', () => { + const { input, out } = pointer() + const taken = [] + input.handle(sample(0b001)) + const release = input.take(s => taken.push(s)) + assert.deepEqual(out, ['down:9:1', 'up:9:1']) + out.length = 0 + input.handle(sample(0b001, { dx: 5 })); input.handle(sample(0b011, { wheel: 1 })); input.handle(sample(0b010)) + assert.deepEqual(taken, [sample(0b001, { dx: 5 }), sample(0b011, { wheel: 1 }), sample(0b010)]) + assert.deepEqual(out, []) + // Given back with the right button still down: its release is not a key-up for anyone. + release() + input.handle(sample(0)); input.handle(sample(0b100)); input.handle(sample(0)) + assert.deepEqual(out, ['down:9:3', 'up:9:3']) + // A stale release function can't take the pointer away from a later taker. + const second = [] + const releaseSecond = input.take(s => second.push(s)) + release() + input.handle(sample(0, { dx: 1 })) + assert.equal(second.length, 1) + releaseSecond() +}) + +test('the keyboard going away forgets held buttons without reporting releases', () => { + const { input, out } = pointer() + input.handle(sample(0b001)) + input.reset() + assert.deepEqual(out, ['down:9:1']) + // The next keyboard starts clean: a press is a press. + input.handle(sample(0b001)) + assert.deepEqual(out, ['down:9:1', 'down:9:1']) +}) diff --git a/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/MicRecordingFileTest.kt b/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/MicRecordingFileTest.kt new file mode 100644 index 00000000..ea454599 --- /dev/null +++ b/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/MicRecordingFileTest.kt @@ -0,0 +1,63 @@ +package com.faceclaw.app + +import java.io.File +import kotlin.math.PI +import kotlin.math.sin +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class MicRecordingFileTest { + /** + * A capture recorded through the real file adapters and read back. Set FACECLAW_MKA_SAMPLE + * to a path to keep the file, for checking it against other Matroska tools (ffprobe, mpv). + */ + @Test + fun recordsACaptureToAFileThatReadsBack() { + val kept = System.getenv("FACECLAW_MKA_SAMPLE") + val file = if (kept.isNullOrEmpty()) File.createTempFile("capture", ".mka") else File(kept) + try { + val writer = MicRecordingWriter(openFileOutput(file.path), "{\"mics\":4,\"app\":\"test\"}", 1_700_000_000_000) + val mixer = SingleTempleMixer() + // Five seconds: each temple streams a stereo pair of tones on its own tick. + for (index in 0 until 100) { + for (side in 0..1) { + val pcm = ShortArray(1600) { + val frame = index * 800 + it / 2 + val hz = if (side == 0) 440.0 else 660.0 + (sin(2 * PI * hz * frame / 16000) * (if (it % 2 == 0) 8000 else 4000)).toInt().toShort() + } + val chunk = MicChunk(if (side == 0) MicStream.LEFT else MicStream.RIGHT, 2, 16000, pcm, 800, 10_000 + index * 50L + side * 7) + chunk.sequence = index + chunk.deviceTickMs = (if (side == 0) 500_000L else 123_456L) + index * 50L + chunk.hasDirection = true + chunk.angleDegrees = if (side == 0) -30 else 25 + chunk.ssr = 12 + writer.write(chunk) + mixer.mix(chunk)?.let { writer.write(it) } + } + } + // The right temple's chunks arrive 7 ms behind the left's. + assertEquals(5007L, writer.finish()) + + val reader = MicRecordingReader(openFileInput(file.path)) + assertEquals(5007L, reader.durationMs) + assertEquals("left temple 2ch + right temple 2ch", reader.description) + assertEquals("{\"mics\":4,\"app\":\"test\"}", reader.configJson) + var left = 0 + var right = 0 + while (true) { + val chunk = reader.next() ?: break + if (chunk.stream == MicStream.LEFT) left++ else right++ + assertEquals(12, chunk.ssr) + } + assertEquals(100, left) + assertEquals(100, right) + reader.close() + // Audio (three tracks) plus the frame metadata. + assertTrue(file.length() > 100 * 2 * 3200 + 100 * 1600) + } finally { + if (kept.isNullOrEmpty()) file.delete() + } + } +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt index bed1f4c0..f261e550 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt @@ -347,9 +347,9 @@ class DisplayListTest { val gray = ByteArray(640 * 480) { 48 }; val app = BmpUtil.pack4bppFromGray8(gray, 640, 480) fun layer(key: Int, y: Int, h: Int, nibble: Int, depth: Int = 0) = ShellScene.Layer(key, 14, y, 612, h, 256, ByteArray(306 * h) { (nibble * 17).toByte() }, depth = depth) - // Each 612x214 layer is just under the 64 KiB resource cap; three exceed the arena on their own. + // Each 612x200 layer takes 60 KiB of the 320 KiB arena: five fit and six exceed it on their own. fun chrome(depth: Int) = layer(1, 444, 36, 7, depth) - val modals = listOf(layer(2, 10, 214, 9), layer(3, 120, 214, 11), layer(4, 230, 214, 13, depth = 4)) + val modals = (0..5).map { layer(2 + it, 10 + 50 * it, 200, 8 + it, depth = if (it == 5) 4 else 0) } val overflow = ShellScene(listOf(chrome(6)) + modals) val plan = planner.plan(app, 640, 480, null, overflow, 1) assertEquals(2, plan.bakedLayers); assertFalse(plan.bakedSelections) @@ -363,7 +363,7 @@ class DisplayListTest { assertContentEquals(BmpUtil.pack4bppFromGray8(expected.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition) } // Once the shell fits again, the next frame is fully live and the screen is the app's again. - val fits = ShellScene(listOf(chrome(0), modals[2])) + val fits = ShellScene(listOf(chrome(0), modals.last())) val next = planner.plan(app, 640, 480, null, fits, plan.nextFid) assertEquals(0, next.bakedLayers) for (glasses in lenses) { @@ -372,13 +372,13 @@ class DisplayListTest { assertContentEquals(BmpUtil.pack4bppFromGray8(fits.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition) } } - @Test fun noisyFramesRemainBoundedAndResourceBudgetIs192KiB() { + @Test fun noisyFramesRemainBoundedAndResourceBudgetIs322KiB() { val cache=ResourceCacheState();val planner=ScenePlanner(cache);val glasses=Glasses() val app=ByteArray(640*480/2){(it*37).toByte()} val plan=planner.plan(app,640,480,null,ShellScene.EMPTY,1) assertTrue(plan.commands.all { it.size<=65535 });glasses.apply(plan.commands) assertContentEquals(app,glasses.screen);assertContentEquals(app,glasses.composition) - assertEquals(196608,ResourceCacheState.CACHE_SIZE) + assertEquals(2048+320*1024,ResourceCacheState.CACHE_SIZE) } /** Revision 35. Same calls and expectations as ClipTest in g2flash/tests/test_display_list.py. */ @Test fun clipRectsNarrowEveryOpNestThroughListsAndMoveWithDepth() { diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt index 26b5f5c9..b9a9ee29 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt @@ -1130,7 +1130,8 @@ class G1FeaturesTest { } s.core.setImuReportEnabled(true, 200) assertTrue(awaitTrue(3_000) { lock.withLock { readings.size >= 5 } }, "samples: " + lock.withLock { readings.size } + "\n" + s.tail()) - assertEquals(listOf(0.1, -1.0, 0.25, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() }) + // The G1's (x, y, z) arrive in the G2's frame as (-z, -x, y) (ImuOrientation). + assertEquals(listOf(-0.25, -0.1, -1.0, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() }) assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_STREAM } }) s.core.setImuReportEnabled(false, 0) assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_IDLE } }) diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt index b457440d..ef7f96b8 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt @@ -855,4 +855,62 @@ class GlassesSessionCoreTest { s.core.close() } } + + @Test + fun keyboardControlsGoToTheRightArmAndKeyboardRecordsReachTheirListenerOnly() { + val s = Session() + val records = ArrayList<ByteArray>() + val lock = s.platform.createLock() + val listener = object : FaceclawKeyboardEventListener { + override fun onKeyboardEvent(body: ByteArray?) { lock.withLock { records.add(body!!) } } + } + s.core.addKeyboardEventListener(listener) + // ['K','B',1,QUERY, request 7, epoch 0] + val control = byteArrayOf(75, 66, 1, 0, 7, 0, 0, 0) + try { + assertFalse(s.core.sendFaceclawKeyboardControl(control, "query"), "refused before the session is up") + s.startAndAwaitLayout() + assertFalse(s.core.sendFaceclawKeyboardControl(null, "none")) + assertFalse(s.core.sendFaceclawKeyboardControl(byteArrayOf(75, 66, 1), "short")) + assertTrue(s.core.sendFaceclawKeyboardControl(control, "query")) + fun controls() = s.link.writes.filter { it.sid == BleProtocol.SID_UI_SETTING && it.pb.containsSequence(control) } + assertTrue(waitUntil(5_000) { controls().isNotEmpty() }) + // One copy, down the right arm: a second one would run the control twice. + assertEquals(listOf(RIGHT), controls().map { it.address }) + assertContentEquals( + BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, 0) + + BleProtocol.encodeBytesField(BleProtocol.FACECLAW_KEYBOARD_CONTROL_FIELD, control), + controls().single().pb, + ) + + // A field-131 record: ['K','B',1,STATUS, seq 1, epoch 0, request 7, ...] with a 10-byte body. + val record = byteArrayOf(75, 66, 1, 1, 1, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 10, 0, 0, 0, 1, 2, 3, 4) + ByteArray(10) + fun push(address: String, body: ByteArray, flag: Int) = s.core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID, + BleProtocol.framePb( + BleProtocol.encodeVarintField(1, 3) + BleProtocol.encodeVarintField(2, 0) + + BleProtocol.encodeBytesField(BleProtocol.FACECLAW_KEYBOARD_EVENT_FIELD, body), + BleProtocol.SID_UI_SETTING, flag, 33)[0]) + val eventsBefore = s.listener.events.size + val activityBefore = s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs } + s.platform.offsetMs += 5_000 + push(RIGHT, record, BleProtocol.FLAG_NOTIFY) + // Whichever temple hosts the keyboard reports it, and the flag doesn't matter. + push(LEFT, record, BleProtocol.FLAG_REQUEST) + assertEquals(2, lock.withLock { records.size }) + assertContentEquals(record, lock.withLock { records[0] }) + // Not glasses input, not user activity, and not an ACK for anything. + assertEquals(eventsBefore, s.listener.events.size) + assertEquals(activityBefore, s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs }) + assertFalse(s.host.hasLog("unexpected ack"), s.host.logs().takeLast(5).toString()) + // Too short to hold the fixed header, or not a keyboard record: left to the normal path. + push(RIGHT, record.copyOf(23), BleProtocol.FLAG_NOTIFY) + push(RIGHT, byteArrayOf(77, 67) + ByteArray(30), BleProtocol.FLAG_NOTIFY) + assertEquals(2, lock.withLock { records.size }) + s.core.removeKeyboardEventListener(listener) + push(RIGHT, record, BleProtocol.FLAG_NOTIFY) + assertEquals(2, lock.withLock { records.size }) + } finally { + s.core.close() + } + } } diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ImuOrientationTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ImuOrientationTest.kt new file mode 100644 index 00000000..6162db29 --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ImuOrientationTest.kt @@ -0,0 +1,25 @@ +package com.faceclaw.app + +import kotlin.math.abs +import kotlin.math.sqrt +import kotlin.test.* + +class ImuOrientationTest { + private val s = sqrt(0.5) + + private fun assertVec(expected: DoubleArray, actual: DoubleArray, what: String) { + for (i in 0..2) assertTrue(abs(expected[i] - actual[i]) < 1e-9, "$what: expected ${expected.toList()}, got ${actual.toList()}") + } + + /** The poses measured on hardware (level, 45° up, 45° to the right) read the G2's values on every model. */ + @Test fun measuredPosesMatchTheG2Frame() { + val level = doubleArrayOf(0.0, 0.0, 1.0) + val up = doubleArrayOf(s, 0.0, s) + val right = doubleArrayOf(0.0, s, s) + val g1 = listOf(doubleArrayOf(0.0, 1.0, 0.0), doubleArrayOf(0.0, s, -s), doubleArrayOf(-s, s, 0.0)) + for ((name, canonical, raw) in listOf(Triple("level", level, g1[0]), Triple("up", up, g1[1]), Triple("right", right, g1[2]))) { + assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G1, raw[0], raw[1], raw[2]), "G1 $name") + assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G2, canonical[0], canonical[1], canonical[2]), "G2 $name") + } + } +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/MicRecordingTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/MicRecordingTest.kt new file mode 100644 index 00000000..10e6c50d --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/MicRecordingTest.kt @@ -0,0 +1,572 @@ +package com.faceclaw.app + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +/** A file held in memory: written through [SeekableOutput], read back through [input]. */ +internal class MemoryFile : SeekableOutput { + var bytes = ByteArray(0) + var closed = false + override val size: Long get() = bytes.size.toLong() + + override fun write(bytes: ByteArray, offset: Int, length: Int) { + this.bytes += bytes.copyOfRange(offset, offset + length) + } + + override fun writeAt(position: Long, bytes: ByteArray) { + bytes.copyInto(this.bytes, position.toInt()) + } + + override fun close() { + closed = true + } + + /** The file as it stands, or its first [length] bytes. */ + fun input(length: Int = bytes.size): SeekableInput { + val snapshot = bytes.copyOf(length) + return object : SeekableInput { + override val length = snapshot.size.toLong() + + override fun readAt(position: Long, buffer: ByteArray, offset: Int, length: Int): Int { + val count = minOf(length.toLong(), maxOf(0L, snapshot.size - position)).toInt() + snapshot.copyInto(buffer, offset, position.toInt(), position.toInt() + count) + return count + } + + override fun close() {} + } + } +} + +private fun pcmBytes(frames: Int, channels: Int, value: Int): ByteArray = + AudioSegmentation.pcm16ToLittleEndian(ShortArray(frames * channels) { value.toShort() }, 0, frames * channels) + +private fun chunk(stream: Int, timeMs: Long, value: Int, channels: Int = 1, frames: Int = 800): MicChunk = + MicChunk(stream, channels, 16000, ShortArray(frames * channels) { (value + it % channels).toShort() }, frames, timeMs) + +private fun drain(reader: MicRecordingReader): List<MicChunk> { + val out = ArrayList<MicChunk>() + while (true) out.add(reader.next() ?: break) + return out +} + +class MatroskaTest { + @Test + fun roundTripsTracksBlocksAndAttachments() { + val file = MemoryFile() + val writer = MkaWriter(file, "session", 1_700_000_000_000, listOf(MkaAttachment("capture.json", "application/json", "{\"a\":1}".encodeToByteArray()))) + val temple = writer.addAudioTrack("left temple", 16000, 2, false) + val text = writer.addTextTrack("frames") + for (index in 0 until 30) { + writer.writeText(text, index * 50L, 50, "{\"n\":$index}") + writer.writeAudio(temple, index * 50L, pcmBytes(800, 2, index), 0, 3200) + } + // A track that first appears mid-file, and is the one players should pick. + val mix = writer.addAudioTrack("mix", 16000, 1, true) + for (index in 30 until 60) { + writer.writeAudio(temple, index * 50L, pcmBytes(800, 2, index), 0, 3200) + writer.writeAudio(mix, index * 50L, pcmBytes(800, 1, -index), 0, 1600) + } + assertEquals(3000L, writer.durationMs) + writer.finish() + assertTrue(file.closed) + + val reader = MkaReader(file.input()) + // Listed with the preferred audio track first, then the others, then text; numbers are as added. + assertEquals(listOf("mix", "left temple", "frames"), reader.tracks.map { it.name }) + assertEquals(listOf(mix, temple, text), reader.tracks.map { it.number }) + val templeInfo = reader.track(temple)!! + assertTrue(templeInfo.isPcm16) + assertEquals(2, templeInfo.channels) + assertEquals(16000, templeInfo.sampleRate) + assertFalse(reader.track(text)!!.isPcm16) + assertEquals("{\"a\":1}", reader.attachments.single().data.decodeToString()) + assertEquals("application/json", reader.attachments.single().mimeType) + assertEquals(3000L, reader.durationMs) + + val blocks = ArrayList<MkaBlock>() + while (true) blocks.add(reader.next() ?: break) + assertEquals(30 + 60 + 30, blocks.size) + assertEquals(text, blocks[0].track) + assertEquals("{\"n\":0}", blocks[0].data.decodeToString()) + assertEquals(50L, blocks[0].durationMs) + val templeBlocks = blocks.filter { it.track == temple } + assertEquals((0 until 60).map { it * 50L }, templeBlocks.map { it.timeMs }) + assertEquals(pcmBytes(800, 2, 59).toList(), templeBlocks.last().data.toList()) + assertEquals(1500L, blocks.first { it.track == mix }.timeMs) + + reader.seekTo(2000) + val afterSeek = ArrayList<MkaBlock>() + while (true) afterSeek.add(reader.next() ?: break) + assertEquals(2000L, afterSeek.first().timeMs) + assertEquals(40, afterSeek.size) + reader.seekTo(99_000) + assertNull(reader.next()) + } + + @Test + fun recordingCutShortIsReadableUpToItsLastCompleteBlock() { + val file = MemoryFile() + val writer = MkaWriter(file, null, 0, emptyList()) + val track = writer.addAudioTrack("mic", 16000, 1, true) + // 2.5 s: clusters of a second each reach the file as they fill; the last half-second is still buffered. + for (index in 0 until 50) writer.writeAudio(track, index * 50L, pcmBytes(800, 1, index), 0, 1600) + val whole = MkaReader(file.input()) + assertEquals("mic", whole.tracks.single().name) + var count = 0 + while (whole.next() != null) count++ + assertEquals(40, count) + assertEquals(2000L, whole.durationMs) + // A crash mid-write tears the last cluster: its complete blocks still read. + val torn = MkaReader(file.input(file.bytes.size - 1000)) + count = 0 + while (torn.next() != null) count++ + assertEquals(39, count) + assertEquals(1950L, torn.durationMs) + } + + @Test + fun rejectsOtherFiles() { + val wav = MemoryFile() + wav.write(BinaryEncoding.wavHeader(0, 16000, 1), 0, 44) + assertFailsWith<IllegalArgumentException> { MkaReader(wav.input()) } + assertFailsWith<IllegalArgumentException> { MkaReader(MemoryFile().input()) } + } + + @Test + fun paddedRegionsFillTheirReservedSpaceWithAVoidElement() { + assertNull(EbmlWriter.padded(ByteArray(10), 9)) + assertNull(EbmlWriter.padded(ByteArray(10), 11)) + assertEquals(listOf(1, 2, 0xEC, 0x80), EbmlWriter.padded(byteArrayOf(1, 2), 4)!!.map { it.toInt() and 0xff }) + val long = EbmlWriter.padded(ByteArray(0), 300)!! + assertEquals(300, long.size) + // An eight-byte size field: 300 - 9 = 0x123. + assertEquals(listOf(0xEC, 0x01, 0, 0, 0, 0, 0, 0x01, 0x23), long.take(9).map { it.toInt() and 0xff }) + } +} + +class MicRecordingTest { + private val warnings = mutableListOf<String>() + + private fun recorder(file: MemoryFile, config: String? = "{\"mics\":4}") = + MicRecordingWriter(file, config, 1_700_000_000_000, testPlatform()) { warnings.add(it) } + + @Test + fun recordsEveryStreamWithItsMetadataAndReplaysTheSources() { + val file = MemoryFile() + val writer = recorder(file) + for (index in 0 until 4) { + val left = chunk(MicStream.LEFT, 5000 + index * 50L, 100 + index, channels = 2) + left.sequence = 7 + index + left.deviceTickMs = 900_000 + index * 50L + left.hasDirection = true + left.angleDegrees = -12 + left.ssr = 40 + left.truncated = index == 3 + writer.write(left) + val feed = SingleTempleMixer().mix(left)!! + writer.write(feed) + val right = chunk(MicStream.RIGHT, 5010 + index * 50L, 200 + index, channels = 2) + right.sequence = 3 + index + right.deviceTickMs = 40_000 + index * 50L + writer.write(right) + } + // The capture went back to the mixed stream part-way through. + writer.event("{\"event\":\"capture\",\"mics\":1}") + val mono = chunk(MicStream.MONO, 5200, 55) + mono.hasDirection = true + mono.angleDegrees = 30 + mono.ssr = 9 + writer.write(mono) + assertEquals(250L, writer.finish()) + assertFalse(writer.failed) + assertTrue(warnings.isEmpty(), warnings.toString()) + + val mka = MkaReader(file.input()) + // What a player picks by default is a mixed track. + assertEquals(listOf("mix", "mic", "left temple", "right temple", "frames"), mka.tracks.map { it.name }) + + val reader = MicRecordingReader(file.input()) + assertEquals("{\"mics\":4}", reader.configJson) + assertEquals("mic + left temple 2ch + right temple 2ch", reader.description) + assertEquals(250L, reader.durationMs) + val chunks = drain(reader) + // The mix is derived again on a replay, so only the sources come back. + assertEquals(4, chunks.count { it.stream == MicStream.LEFT }) + assertEquals(4, chunks.count { it.stream == MicStream.RIGHT }) + assertEquals(1, chunks.count { it.stream == MicStream.MONO }) + val left = chunks.filter { it.stream == MicStream.LEFT } + assertEquals(listOf(0L, 50L, 100L, 150L), left.map { it.timeMs }) + assertEquals(listOf(7, 8, 9, 10), left.map { it.sequence }) + assertEquals(900_050L, left[1].deviceTickMs) + assertEquals(2, left[0].channels) + assertEquals(listOf<Short>(100, 101, 100, 101), left[0].pcm.take(4)) + assertTrue(left.all { it.hasDirection && it.angleDegrees == -12 && it.ssr == 40 }) + assertEquals(listOf(false, false, false, true), left.map { it.truncated }) + val right = chunks.filter { it.stream == MicStream.RIGHT } + assertEquals(listOf(10L, 60L, 110L, 160L), right.map { it.timeMs }) + assertFalse(right[0].hasDirection) + val replayedMono = chunks.single { it.stream == MicStream.MONO } + assertEquals(200L, replayedMono.timeMs) + assertEquals(30, replayedMono.angleDegrees) + assertEquals(800, replayedMono.frames) + + reader.seekTo(100) + assertEquals(100L, reader.next()!!.timeMs) + } + + @Test + fun chunksHeldUpInDeliveryLandWhereTheyWereCaptured() { + val file = MemoryFile() + val writer = recorder(file, null) + // A temple's ticks say when each chunk was captured. The first ten arrive as captured; + // the link then stalls for a second and delivers the next twenty at once. + for (index in 0 until 30) { + val arrival = if (index < 10) 1000 + index * 50L else maxOf(2500L, 1000 + index * 50L) + val chunk = chunk(MicStream.LEFT, arrival, index) + chunk.deviceTickMs = 77_000 + index * 50L + writer.write(chunk) + } + // Two chunks were lost on the way (their ticks are skipped): a gap, not a splice. + val late = chunk(MicStream.LEFT, 2650, 99) + late.deviceTickMs = 77_000 + 32 * 50L + writer.write(late) + writer.finish() + val times = drain(MicRecordingReader(file.input())).map { it.timeMs } + assertEquals((0 until 30).map { it * 50L } + 1600L, times) + assertNull(MicRecordingReader(file.input()).configJson) + } + + @Test + fun aSourceWithoutAClockIsPlacedBySampleCount() { + val file = MemoryFile() + val writer = recorder(file) + for (index in 0 until 10) { + // Arrivals jitter around the capture cadence; the stream's samples are contiguous. + val jitter = if (index % 3 == 1) 30L else 0L + writer.write(chunk(MicStream.MONO, 400 + index * 50L + jitter, index)) + } + // The decoder reported one packet missing before this chunk. + val afterLoss = chunk(MicStream.MONO, 400 + 11 * 50L, 11) + afterLoss.lostFramesBefore = 800 + writer.write(afterLoss) + // A capture that resumes much later is placed by its arrival, not spliced on. + writer.write(chunk(MicStream.MONO, 60_000, 12)) + writer.finish() + val chunks = drain(MicRecordingReader(file.input())) + assertEquals((0 until 10).map { it * 50L } + 550L + 59_600L, chunks.map { it.timeMs }) + assertEquals(800, chunks[10].lostFramesBefore) + } + + @Test + fun aForeignFilePlaysItsFirstTrackAsTheMicrophone() { + val file = MemoryFile() + val mka = MkaWriter(file, null, 0, emptyList()) + val track = mka.addAudioTrack("Stereo", 16000, 2, false) + // One long block of stereo frames [100, 300]. + val samples = ShortArray(2000 * 2) { if (it % 2 == 0) 100 else 300 } + mka.writeAudio(track, 0, AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size), 0, samples.size * 2) + mka.finish() + val reader = MicRecordingReader(file.input()) + assertEquals("mic 2ch", reader.description) + val chunks = drain(reader) + assertEquals(listOf(800, 800, 400), chunks.map { it.frames }) + assertEquals(listOf(0L, 50L, 100L), chunks.map { it.timeMs }) + assertTrue(chunks.all { it.stream == MicStream.MONO && it.channels == 1 && it.pcm.all { sample -> sample == 200.toShort() } }) + + val fast = MemoryFile() + val other = MkaWriter(fast, null, 0, emptyList()) + other.writeAudio(other.addAudioTrack("mic", 48000, 1, true), 0, pcmBytes(480, 1, 1), 0, 960) + other.finish() + val error = assertFailsWith<IllegalArgumentException> { MicRecordingReader(fast.input()) } + assertTrue(error.message!!.contains("48000 Hz"), error.message) + } + + @Test + fun theFeedFollowsTheLeftTempleAndFallsBackToTheRight() { + val mixer = SingleTempleMixer() + // Until the left temple is heard from, the right one feeds. + assertNotNull(mixer.mix(chunk(MicStream.RIGHT, 0, 10, channels = 2))) + val left = chunk(MicStream.LEFT, 20, 100, channels = 2) + left.deviceTickMs = 5 + val feed = mixer.mix(left)!! + assertEquals(MicStream.MIX, feed.stream) + assertEquals(1, feed.channels) + assertEquals(800, feed.frames) + // Channels [100, 101] average to 100. + assertTrue(feed.pcm.all { it == 100.toShort() }) + assertEquals(MicStream.LEFT, feed.clockStream) + assertEquals(5L, feed.deviceTickMs) + assertNull(mixer.mix(chunk(MicStream.RIGHT, 60, 10))) + assertNull(mixer.mix(chunk(MicStream.RIGHT, 300, 10))) + // The left temple went quiet: the right stands in until it is back. + assertNotNull(mixer.mix(chunk(MicStream.RIGHT, 320, 10))) + assertNotNull(mixer.mix(chunk(MicStream.LEFT, 330, 100))) + assertNull(mixer.mix(chunk(MicStream.RIGHT, 340, 10))) + assertNull(mixer.mix(chunk(MicStream.MONO, 350, 10))) + } + + @Test + fun decodesTheTemplesStreamFrames() { + val frame = micStreamFrame(sequence = 0x1234, tick = 0x89ABCDEFL, channels = 2, samples = shortArrayOf(1, -2, 3, -4), angle = -45, ssr = 300, truncated = true) + val decoded = MicArrayProtocol.decodeFrame(frame)!! + assertEquals(0x1234, decoded.sequence) + assertEquals(0x89ABCDEFL, decoded.deviceTickMs) + assertEquals(2, decoded.channels) + assertEquals(16000, decoded.sampleRate) + assertEquals(-45, decoded.angleDegrees) + assertEquals(300, decoded.ssr) + assertTrue(decoded.truncated) + assertEquals(listOf<Short>(1, -2, 3, -4), MicArrayProtocol.pcm16(frame, decoded)!!.toList()) + assertNull(MicArrayProtocol.decodeFrame(ByteArray(205))) + // A payload length beyond the packet is cut to what arrived. + val short = frame.copyOf(frame.size - 4) + assertEquals(listOf<Short>(1, -2), MicArrayProtocol.pcm16(short, MicArrayProtocol.decodeFrame(short)!!)!!.toList()) + assertEquals(listOf(0x4d, 0x43, 1, 4), MicArrayProtocol.control(MicArrayProtocol.OP_RENEW).map { it.toInt() }) + assertTrue(MicArrayProtocol.isConfigure(CONFIGURE_RECORD)) + assertFalse(MicArrayProtocol.isConfigure(MicArrayProtocol.control(MicArrayProtocol.OP_STOP))) + } +} + +/** A mic_control CONFIGURE record: codec front end, both mics, raw PCM16 at 16 kHz, hardware armed. */ +internal val CONFIGURE_RECORD = byteArrayOf(0x4d, 0x43, 1, 1, 0, 3, 1, 0, 160.toByte(), 0, 0, 0, 2) + +/** An 'SM' stream frame as a temple sends it. */ +internal fun micStreamFrame( + sequence: Int, + tick: Long, + channels: Int, + samples: ShortArray, + angle: Int = 0, + ssr: Int = 0, + truncated: Boolean = false, +): ByteArray { + val payload = AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size) + val header = ByteArray(MicArrayProtocol.HEADER_BYTES) + fun le16(at: Int, value: Int) { + header[at] = value.toByte() + header[at + 1] = (value shr 8).toByte() + } + header[0] = 'S'.code.toByte() + header[1] = 'M'.code.toByte() + header[2] = 1 + header[3] = (if (truncated) 0x82 else 0x02).toByte() + le16(4, sequence) + le16(6, (tick and 0xffff).toInt()) + le16(8, ((tick shr 16) and 0xffff).toInt()) + header[10] = channels.toByte() + le16(11, 160) + header[13] = 0 + header[14] = 1 + le16(15, angle) + le16(17, ssr) + le16(19, payload.size) + return header + payload +} + +class VoiceCaptureSourcesTest { + private class Events : FaceclawVoiceControllerListener { + val statuses = mutableListOf<String?>() + val transcripts = mutableListOf<Pair<String?, Boolean>>() + val pcmChunks = mutableListOf<ByteArray>() + val replay = mutableListOf<Pair<String?, Long>>() + val stoppedLatch = Latch(1, testPlatform()) + override fun onStatus(status: String?) { statuses.add(status) } + override fun onTranscript(text: String?, isFinal: Boolean) { transcripts.add(text to isFinal) } + override fun onStopped(captureId: Int) { stoppedLatch.countDown() } + override fun onPcm(pcm16le: ByteArray?) { pcmChunks.add(pcm16le!!) } + override fun onFrameMeta(angleDegrees: Int, ssr: Int) {} + override fun onSpeechEnd() {} + override fun onSpeakerVerified(isWearer: Boolean, similarity: Float) {} + override fun onReplay(event: String?, positionMs: Long, durationMs: Long) { replay.add(event to positionMs) } + } + + private class FakeLink : MicArrayLink { + var listener: FaceclawAudioPacketListener? = null + val controls = mutableListOf<String>() + override fun startForwarding(listener: FaceclawAudioPacketListener): Boolean { this.listener = listener; return true } + override fun stopForwarding() { listener = null } + override fun isForwarding(): Boolean = listener != null + override fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean { + controls.add(label + ":" + record[3] + (if (leftTemple) "L" else "") + (if (rightTemple) "R" else "")) + return true + } + } + + private class FakeHost(val link: FakeLink? = null, val recording: MemoryFile? = null) : VoiceCaptureHost { + override val dispatcher = CallbackDispatcher { it() } + override fun onSessionStarting() {} + override fun isG2SessionReady(): Boolean = link != null + override fun isG2AudioCaptureActive(): Boolean = link?.isForwarding() == true + override fun g2AudioSource(): PacketAudioSource? = null + override fun createPacketDecoder(): AudioPacketDecoder = error("no stock stream in these tests") + override fun openPhoneMic(): PcmAudioSource? = null + override fun hasTranscriberModel(kind: VoiceModelKind): Boolean = true + override fun loadTranscriber(kind: VoiceModelKind): OfflineTranscriber = object : OfflineTranscriber { + override fun recognize(samples: FloatArray, count: Int): String = "x" + override fun release() {} + } + override fun speakerEmbedder(modelPath: String): SpeakerEmbedder = error("no verification in these tests") + override fun micArrayLink(): MicArrayLink? = link + override fun openRecording(path: String): SeekableInput = if (path == "missing") error("no such file") else recording!!.input() + } + + private val logs = mutableListOf<String>() + + private fun session(host: VoiceCaptureHost, name: String) = + VoiceCaptureSession(host, testPlatform(), name, { logs.add("I $it") }, { logs.add("W $it") }, { m, e -> logs.add("E $m ${e.message}") }) + + private fun waitFor(timeoutMs: Long, condition: () -> Boolean): Boolean { + val deadline = testPlatform().elapsedRealtimeMs() + timeoutMs + while (!condition()) { + if (testPlatform().elapsedRealtimeMs() > deadline) return false + sleepMs(5) + } + return true + } + + private fun monoRecording(chunks: Int): MemoryFile { + val file = MemoryFile() + val writer = MicRecordingWriter(file, null, 0, testPlatform()) + for (index in 0 until chunks) writer.write(chunk(MicStream.MONO, index * 50L, 1000 + index)) + writer.finish() + return file + } + + @Test + fun micArrayCaptureArmsTheTemplesRecordsThemAndTranscribesTheFeed() { + val link = FakeLink() + val host = FakeHost(link) + val events = Events() + val session = session(host, "voice-test-array") + val file = MemoryFile() + val recorder = MicRecordingWriter(file, "{}", 0, testPlatform()) + session.setListener(events) + session.setMicArray(CONFIGURE_RECORD) + session.setRecorder(recorder) + session.start("cloud", 1) + assertTrue(waitFor(3000) { link.controls.isNotEmpty() }) + assertTrue(session.isCapturing()) + val listener = link.listener!! + for (index in 0 until 5) { + val left = ShortArray(1600) { if (it % 2 == 0) 100 else 300 } + listener.onAudioPacket(micStreamFrame(index, 1000 + index * 50L, 2, left, angle = 20, ssr = 7), "L", 500 + index * 50L) + listener.onAudioPacket(micStreamFrame(index, 9000 + index * 50L, 2, ShortArray(1600) { 50 }), "R", 505 + index * 50L) + } + // A stock-format packet from before arming took effect is not audio. + listener.onAudioPacket(ByteArray(205), "L", 760) + assertTrue(waitFor(3000) { events.pcmChunks.size >= 5 }) + session.stop() + assertTrue(events.stoppedLatch.await(3000)) + assertEquals(listOf("configure:1LR"), link.controls.take(1)) + assertEquals("stop:3LR", link.controls.last()) + assertFalse(link.isForwarding()) + assertFalse(session.isCapturing()) + // The feed is the left temple's microphones averaged. + assertEquals(5, events.pcmChunks.size) + assertEquals(1600, events.pcmChunks[0].size) + assertTrue(AudioSegmentation.pcm16leToFloat(events.pcmChunks[0]).all { it == 200 / 32768f }) + assertTrue(events.statuses.contains("Listening (cloud)...")) + + recorder.finish() + assertEquals(listOf("mix", "left temple", "right temple", "frames"), MkaReader(file.input()).tracks.map { it.name }) + val recorded = drain(MicRecordingReader(file.input())) + assertEquals(5, recorded.count { it.stream == MicStream.LEFT }) + assertEquals(5, recorded.count { it.stream == MicStream.RIGHT }) + val left = recorded.first { it.stream == MicStream.LEFT } + assertEquals(2, left.channels) + assertEquals(1000L, left.deviceTickMs) + assertEquals(20, left.angleDegrees) + } + + @Test + fun micArrayCaptureNeedsGlassesThatHaveTheChannel() { + val events = Events() + val session = session(FakeHost(), "voice-test-array-none") + session.setListener(events) + session.setMicArray(CONFIGURE_RECORD) + session.start("cloud", 1) + assertEquals<List<String?>>(listOf("Voice control needs an active G2 connection."), events.statuses) + // A record that is not a CONFIGURE leaves the capture on the mixed stream. + val mixed = Events() + session.setListener(mixed) + session.setMicArray(MicArrayProtocol.control(MicArrayProtocol.OP_STOP)) + session.setUsePhoneMic(true) + session.start("cloud", 2) + assertTrue(mixed.stoppedLatch.await(3000)) + assertEquals<List<String?>>(listOf("Could not start the phone microphone."), mixed.statuses) + } + + @Test + fun replayPlaysARecordingThroughThePipelineInRealTime() { + val host = FakeHost(recording = monoRecording(30)) + val events = Events() + val session = session(host, "voice-test-replay") + session.setListener(events) + // A replay is never recorded again. + val copy = MemoryFile() + val recorder = MicRecordingWriter(copy, null, 0, testPlatform()) + session.setRecorder(recorder) + session.setReplay("recording.mka", 1000) + val startedAt = testPlatform().elapsedRealtimeMs() + session.start("onboard", 5) + assertTrue(events.stoppedLatch.await(5000)) + val elapsed = testPlatform().elapsedRealtimeMs() - startedAt + // Half a second of audio, paced as it was captured. + assertTrue(elapsed >= 400, "replay took $elapsed ms") + assertEquals(10, events.pcmChunks.size) + assertEquals(1020.toShort(), AudioSegmentation.pcm16le(events.pcmChunks[0][0], events.pcmChunks[0][1])) + assertEquals("start" to 1000L, events.replay.first()) + assertEquals("end" to 1500L, events.replay.last()) + assertEquals(VoiceCaptureSession.REPLAY_FINISHED_STATUS, events.statuses.last()) + assertEquals("x" to true, events.transcripts.last()) + assertFalse(session.isCapturing()) + recorder.finish() + assertTrue(MkaReader(copy.input()).tracks.isEmpty()) + } + + @Test + fun replayJumpsAndPauses() { + val host = FakeHost(recording = monoRecording(200)) + val events = Events() + val session = session(host, "voice-test-replay-seek") + session.setListener(events) + session.setReplay("recording.mka", 0) + session.start("onboard", 6) + assertTrue(waitFor(3000) { events.pcmChunks.size >= 2 }) + session.setReplayPaused(true) + sleepMs(100) + val heldAt = events.pcmChunks.size + sleepMs(200) + assertEquals(heldAt, events.pcmChunks.size) + assertTrue(session.isCapturing()) + // Jump to the last quarter-second while paused, then go on. + session.seekReplay(9750) + session.setReplayPaused(false) + assertTrue(events.stoppedLatch.await(5000)) + assertTrue(events.replay.contains("seek" to 9750L), events.replay.toString()) + assertEquals("end" to 10_000L, events.replay.last()) + assertEquals(heldAt + 5, events.pcmChunks.size) + // What played before the jump ended as its own utterance. + assertEquals(2, events.transcripts.count { it.second }) + val last = events.pcmChunks.last() + assertEquals(1199.toShort(), AudioSegmentation.pcm16le(last[0], last[1])) + } + + @Test + fun replayOfAnUnreadableFileReportsAndStops() { + val events = Events() + val session = session(FakeHost(), "voice-test-replay-missing") + session.setListener(events) + session.setReplay("missing", 0) + session.start("cloud", 7) + assertTrue(events.stoppedLatch.await(3000)) + assertEquals<List<String?>>(listOf("Could not open the recording: no such file"), events.statuses) + assertEquals(listOf<Pair<String?, Long>>("error" to 0L), events.replay) + } +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ResourceCacheTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ResourceCacheTest.kt index 44c84e9c..b90372e8 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ResourceCacheTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ResourceCacheTest.kt @@ -64,10 +64,12 @@ class ResourceCacheTest { @Test fun lruEvictsByBytesAndProtectsAllCurrentFrameHits() { val cache = ResourceCacheState() + // Five of these fill the 320 KiB arena; a sixth needs an eviction. val a = resource(60000, 1); val b = resource(60000, 2); val c = resource(60000, 3); val d = resource(60000, 4) - assertContentEquals(intArrayOf(0, 1, 2), cache.prepare(listOf(a, b, c))) + val e = resource(60000, 5); val f = resource(60000, 6) + assertContentEquals(intArrayOf(0, 1, 2, 3, 4), cache.prepare(listOf(a, b, c, e, f))) cache.drainCommands(3600) - cache.prepare(listOf(a)); cache.drainCommands(3600) // b is oldest now. + cache.prepare(listOf(a, e, f)); cache.drainCommands(3600) // b is oldest now. assertContentEquals(intArrayOf(1, 2), cache.prepare(listOf(d, c))) // protect later hit c before d. val commands = cache.drainCommands(3600) assertContentEquals(byteArrayOf(22, 1, 0, 1, 0), commands.first()) @@ -75,10 +77,10 @@ class ResourceCacheTest { assertEquals(0, cache.resourceId(a)); assertEquals(2, cache.resourceId(c)) assertEquals(1, cache.resourceId(d)) assertContentEquals(d.bytes, uploaded(commands).getValue(1)) - val tooLarge = cache.prepare(listOf(a, c, d, b)) + val tooLarge = cache.prepare(listOf(a, c, d, e, f, b)) assertEquals(-1, tooLarge.last()) assertTrue(cache.drainCommands(3600).isEmpty()) - assertEquals(3, cache.residentCount()) + assertEquals(5, cache.residentCount()) } @Test fun idPressureUsesLruEvenWhenBytesAreAvailable() { @@ -96,15 +98,15 @@ class ResourceCacheTest { @Test fun rejectedPlansRestoreResidencyAndLru() { val cache = ResourceCacheState() - val resources = (0..3).map { resource(65536, it) } - cache.prepare(resources.take(3)); cache.drainCommands(3600) + val resources = (0..5).map { resource(60000, it) } // five fit, the sixth evicts + assertContentEquals(intArrayOf(0, 1, 2, 3, 4), cache.prepare(resources.take(5))); cache.drainCommands(3600) val rollback = cache.checkpoint() - cache.prepare(listOf(resources[3])) + cache.prepare(listOf(resources[5])) assertEquals(-1, cache.resourceId(resources[0])) rollback() - assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[3])) + assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[5])) assertTrue(cache.drainCommands(3600).isEmpty()) - cache.prepare(listOf(resources[3])) + cache.prepare(listOf(resources[5])) assertContentEquals(byteArrayOf(22, 1, 0, 0, 0), cache.drainCommands(3600).first()) } diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/VoiceCaptureSessionTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/VoiceCaptureSessionTest.kt index 630d9bbc..c7b072e4 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/VoiceCaptureSessionTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/VoiceCaptureSessionTest.kt @@ -29,6 +29,8 @@ class VoiceCaptureSessionTest { override fun onFrameMeta(angleDegrees: Int, ssr: Int) { frameMeta.add(angleDegrees to ssr) } override fun onSpeechEnd() { speechEnds++ } override fun onSpeakerVerified(isWearer: Boolean, similarity: Float) { verified = isWearer to similarity } + val replay = mutableListOf<Pair<String?, Long>>() + override fun onReplay(event: String?, positionMs: Long, durationMs: Long) { replay.add(event to positionMs) } } /** Scripted phone mic: each entry is a chunk amplitude; after the script the read fails (-1). */ diff --git a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosMicRecorderTest.kt b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosMicRecorderTest.kt new file mode 100644 index 00000000..3b0a2d65 --- /dev/null +++ b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosMicRecorderTest.kt @@ -0,0 +1,46 @@ +package com.faceclaw.app + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import platform.Foundation.NSTemporaryDirectory +import platform.Foundation.NSUUID + +class IosMicRecorderTest { + @Test + fun recordsPcmToAMatroskaFileThroughThePosixFileAdapters() { + val path = NSTemporaryDirectory() + NSUUID().UUIDString + ".mka" + try { + val recorder = assertNotNull(IosMicRecordings.open(path, "{\"platform\":\"ios\"}")) + // 2.5 s in 50 ms chunks: enough for whole clusters to reach the file before finish(). + for (index in 0 until 50) { + val samples = ShortArray(800) { (index * 10 + it % 7).toShort() } + recorder.append(AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size).data()) + } + assertFalse(recorder.failed) + val length = recorder.finish() + assertTrue(length >= 2500, "recorded $length ms") + assertEquals(fileLength(path), openFileInput(path).length) + + val reader = MicRecordingReader(openFileInput(path)) + assertEquals("{\"platform\":\"ios\"}", reader.configJson) + assertEquals("mic", reader.description) + var chunks = 0 + var last: MicChunk? = null + while (true) { + last = reader.next() ?: break + assertEquals(800, last.frames) + assertEquals((chunks * 10).toShort(), last.pcm[0]) + chunks++ + } + assertEquals(50, chunks) + reader.close() + } finally { + deleteFile(path) + } + assertNull(IosMicRecordings.open("/nonexistent-directory/x.mka", null)) + } +} diff --git a/tests/menu-display-list.test.cjs b/tests/menu-display-list.test.cjs index 8fb9f0d5..df31cb63 100644 --- a/tests/menu-display-list.test.cjs +++ b/tests/menu-display-list.test.cjs @@ -17,7 +17,7 @@ const font={lineHeight:12,ascent:10,descent:2,measureText:text=>text.length*4, const menuCore=load('app/ui/menu-core.ts',{ '../graphics/image':graphics,'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'), }); -const {MenuLayer,TextPageLayer}=load('app/ui/menu.ts',{ +const {MenuLayer,TextPageLayer,menuHeadingItem}=load('app/ui/menu.ts',{ './menu-animation-pref': require('../.test-build/app/ui/menu-animation-pref.js'), './animation-speed': require('../.test-build/app/ui/animation-speed.js'), '../native/settings-store': { getStringSetting: (_key, fallback) => fallback }, '../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 }, '../graphics/image':graphics,'../graphics/textwrap':{wrapText:(_font,text)=>text.split('\n')},'../graphics/ui-fonts':{getDefaultSmallFont:()=>font}, '../util/numeric-util':{clamp:(n,a,b)=>Math.max(a,Math.min(b,n))},'./gestures':{}, @@ -81,6 +81,31 @@ test('a submenu that replaces its parent menu skips the parent plane and dims th stack.pop();stack.pop(); assert.equal(stack.paint().length,2); }); +test('heading rows label a section and are never selected',async()=>{ + const picked=[]; + const row=label=>({label,onSelect(){picked.push(label);}}); + const layer=new MenuLayer(null,[menuHeadingItem('First'),row('A'),row('B'),menuHeadingItem('Second'),row('C')], + {x:0,y:0,width:120,minHeight:150,maxHeight:150,squareCorners:true,showBorder:false}); + const context={stack:{getBaseSize:()=>({width:120,height:150}),isFocused:()=>true}}; + const paint=()=>layer.paint(context,()=>new graphics.GrayImage(120,150)); + const move=async type=>{await layer.handleInput({type},context);paint();return layer.selectedIndex;}; + // The highlight starts below the first heading and steps over the second, both ways and when wrapping. + paint();assert.equal(layer.selectedIndex,1); + assert.equal(await move('scroll-down'),2); + assert.equal(await move('scroll-down'),4); + assert.equal(await move('scroll-down'),1); + assert.equal(await move('scroll-up'),4); + assert.equal(await move('scroll-up'),2); + await layer.handleInput({type:'click'},context);assert.deepEqual(picked,['B']); + // A heading asked for by index lands on the row under it. + layer.selectItem(3);assert.equal(layer.selectedIndex,4); + // Headings draw dim, in capitals (4px per character in this font), in the rows' text column. + const image=paint().withDrawsBaked(false),inkAt=y=>{const xs=[];for(let x=0;x<120;x++) if(image.pixels[y*120+x]>=8) xs.push([x,image.pixels[y*120+x]]);return xs;}; + const heading=inkAt(8+4+2),unselected=inkAt(8+20+4+2); + assert.equal(heading[0][0],unselected[0][0]); + assert.ok(heading.every(([,value])=>value===90));assert.ok(unselected.every(([,value])=>value===200)); + assert.equal(heading.length,'FIRST'.length*2); +}); test('text page paints at the stack size, not the lens size',()=>{ // An app window's stack is its viewport; a lens-sized plane is rejected by // the compositor as larger than the window surface. diff --git a/tests/mouse-test-page.test.cjs b/tests/mouse-test-page.test.cjs new file mode 100644 index 00000000..f1804c46 --- /dev/null +++ b/tests/mouse-test-page.test.cjs @@ -0,0 +1,136 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +function load(file, modules, globals = {}) { + const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); + const context = { exports: {}, console, ...globals, require(name) { assert.ok(name in modules, name); return modules[name]; } }; + vm.runInNewContext(ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, context); + return context.exports; +} + +const graphics = require('../.test-build/app/graphics/image.js'); +const { bordered } = require('./helpers/viewport.cjs'); +const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} }); +const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u12n.bdf'), 'utf8')); + +/** The page over a fake keyboard service, shell and clock. */ +function fixture({ state = 'connected', pointer = true } = {}) { + let now = 10_000, visible = true, renders = 0, nextTimer = 1; + const timers = new Map(); + const service = { + listener: null, takes: 0, releases: 0, + snapshot: () => ({ state, pointer }), + takePointer(listener) { + this.takes++; this.listener = listener; + return () => { if (this.listener === listener) this.listener = null; this.releases++; }; + }, + }; + const globals = { + Date: { now: () => now }, + setInterval: (fn, ms) => { timers.set(nextTimer, { fn, ms, at: now + ms, repeat: true }); return nextTimer++; }, + setTimeout: (fn, ms) => { timers.set(nextTimer, { fn, ms, at: now + ms, repeat: false }); return nextTimer++; }, + clearInterval: id => timers.delete(id), clearTimeout: id => timers.delete(id), + }; + const { MouseTestLayer } = load('app/apps/developer/mouse-test.ts', { + '../../graphics/ui-fonts': { getDefaultSmallFont: () => font }, '../../graphics/image': graphics, + '../../g2/keyboard/service': { keyboardService: service }, + '../../ui/shell/shell': { shell: { isWindowVisible: () => visible } }, + '../../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) }, + '../../ui/gestures': {}, '../../ui/layers': {}, + }, globals); + const layer = new MouseTestLayer('developer', () => { renders++; }); + const viewport = bordered(576, 260); + const popped = []; + const ctx = { stack: { getBaseSize: () => viewport, pop: () => popped.push(1) } }; + function advance(ms) { + const end = now + ms; + for (;;) { + const due = [...timers].filter(([, t]) => t.at <= end).sort((a, b) => a[1].at - b[1].at)[0]; + if (!due) break; + const [id, timer] = due; + now = timer.at; + if (timer.repeat) timer.at += timer.ms; else timers.delete(id); + timer.fn(); + } + now = end; + } + const sample = (buttons, extra = {}) => service.listener({ buttons, dx: 0, dy: 0, wheel: 0, pan: 0, ...extra }); + return { layer, service, ctx, viewport, timers, popped, advance, sample, paint: () => layer.paint(ctx), + renders: () => renders, setVisible: value => { visible = value; } }; +} +/** Whether the arrow's tip (its top-left pixel, full white) is at (x, y) and nowhere it shouldn't be. */ +const tipAt = (image, x, y) => image.getPixel(x, y) === 255 && image.getPixel(x, y + 1) === 255 && image.getPixel(x + 1, y + 2) === 70; + +test('the arrow follows the touchpad, stays on the page, and repaints at a rate the link can carry', () => { + const f = fixture(); + assert.equal(f.service.takes, 1, 'takes the pointer while the page shows'); + f.paint(); + const { width, height } = f.viewport; + // The first movement starts from the middle of the page. + f.sample(0, { dx: 10, dy: -20 }); + assert.ok(tipAt(f.paint(), width / 2 + 10, height / 2 - 20)); + // A hundred reports in a second are one repaint per 33 ms, not a hundred. + const before = f.renders(); + for (let i = 0; i < 100; i++) { f.sample(0, { dx: 1 }); f.advance(10); } + assert.ok(f.renders() - before >= 20 && f.renders() - before <= 31, String(f.renders() - before)); + assert.ok(tipAt(f.paint(), width / 2 + 110, height / 2 - 20)); + // Pushed past an edge it stops there. + f.sample(0, { dx: 5000, dy: 5000 }); + let image = f.paint(); + assert.equal(image.getPixel(width - 1, height - 1), 255); + f.sample(0, { dx: -9000, dy: -9000 }); + assert.ok(tipAt(f.paint(), 0, 0)); +}); + +test('buttons and wheel show on the page; a left click leaves a mark and a right click clears them', () => { + const f = fixture(); + f.paint(); + f.sample(0, { dx: 100, dy: 40 }); + const { width, height } = f.viewport; + const at = { x: width / 2 + 100, y: height / 2 + 40 }; + f.sample(1); + f.sample(0, { dx: -60, wheel: 2 }); + let image = f.paint(); + // The mark: a small cross where the click was, after the arrow has moved off it. + assert.equal(image.getPixel(at.x - 4, at.y), 140); assert.equal(image.getPixel(at.x, at.y + 4), 140); + assert.ok(tipAt(image, at.x - 60, at.y)); + f.sample(2); + image = f.paint(); + assert.equal(image.getPixel(at.x - 4, at.y), 0); + f.sample(0); + assert.doesNotThrow(() => f.paint()); +}); + +test('the pointer is only held while the page can be seen, and given back when the page goes', () => { + const f = fixture(); + f.paint(); + f.sample(1); + // Another window comes to the front: the buttons go back to being keys. + f.setVisible(false); f.advance(400); + assert.equal(f.service.releases, 1); assert.equal(f.service.listener, null); + f.setVisible(true); f.advance(400); + assert.equal(f.service.takes, 2); + // Double tap (or Esc, which is one) leaves; leaving gives the pointer back and stops the timers. + f.layer.handleInput({ type: 'double-click' }, f.ctx); + assert.equal(f.popped.length, 1); + f.layer.onRemoved(); + assert.equal(f.service.releases, 2); assert.equal(f.timers.size, 0); + const renders = f.renders(); + f.advance(5000); + assert.equal(f.renders(), renders); +}); + +test('without a keyboard, or with one that has no pointer, the page says so', () => { + for (const options of [{ state: 'idle' }, { state: 'connected', pointer: false }]) { + const f = fixture(options); + const image = f.paint(); + assert.ok(image instanceof graphics.GrayImage); + f.layer.onRemoved(); + } +}); diff --git a/tests/settings-panel.test.cjs b/tests/settings-panel.test.cjs index 34dc7c9e..e6dc1552 100644 --- a/tests/settings-panel.test.cjs +++ b/tests/settings-panel.test.cjs @@ -157,3 +157,39 @@ test('the columns keep the padding suggested for each window edge, and scroll ri const lit = (y) => { for (let x = 180; x < 560; x++) if (image.getPixel(x, y) > 40) return true; return false; }; assert.ok([0, 1, 2, 3, 4, 5, 6, 7].some(lit), 'rows show in the top padding once scrolled'); }); + +test('a hidden section comes and goes while the panel stays open', async () => { + let hidden = true; + const selected = []; + const item = (label) => ({ label, onSelect: () => selected.push(label) }); + const layer = new SettingsPanelLayer([ + { label: 'Display', items: [item('Brightness')] }, + { label: 'Keyboard', items: [item('Pair')], hidden: () => hidden }, + { label: 'About', items: [item('License')] }, + ]); + const ctx = { stack: { getBaseSize: () => bordered(576, 260), isFocused: () => true } }; + const input = (type) => layer.handleInput({ type, timestampMs: 0 }, ctx); + const enter = async () => { await input('click'); await input('click'); await input('double-click'); }; + + // Hidden: the row below Display is About. + await input('scroll-down'); await enter(); + assert.deepEqual(selected, ['License']); + layer.focusSection('Keyboard'); + await enter(); + assert.deepEqual(selected, ['License', 'License'], 'a hidden section is no deep-link target either'); + + // Shown, with no new panel: the highlight stays on About, and Keyboard is the row above it. + hidden = false; + layer.paint(ctx); + await enter(); + assert.deepEqual(selected.slice(2), ['License']); + await input('scroll-up'); await enter(); + assert.deepEqual(selected.slice(3), ['Pair']); + + // Hidden again from inside it: focus comes back out to the top of the list. + await input('click'); + hidden = true; + layer.paint(ctx); + await enter(); + assert.deepEqual(selected.slice(4), ['Brightness']); +}); diff --git a/tests/terminal-connections.test.cjs b/tests/terminal-connections.test.cjs new file mode 100644 index 00000000..19c46fbc --- /dev/null +++ b/tests/terminal-connections.test.cjs @@ -0,0 +1,56 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +const settings = new Map(); +const connections = loader({}, { + '../../native/settings-store': { + getStringSetting: (key, fallback) => (settings.has(key) ? settings.get(key) : fallback), + setStringSetting: (key, value) => settings.set(key, value), + }, +})('app/apps/terminal/connections.ts'); + +// vm-realm objects: compare as plain JSON. +const plain = (value) => JSON.parse(JSON.stringify(value)); + +test('ssh:// strings select tmux by default', () => { + assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@laptop')), { user: 'jim', host: 'laptop', port: 22, mode: 'tmux' }); + assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@laptop:2222/')), { user: 'jim', host: 'laptop', port: 2222, mode: 'tmux' }); + assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@[fd7a::1]:22')), { user: 'jim', host: 'fd7a::1', port: 22, mode: 'tmux' }); + assert.equal(connections.parseSshConnectionString('ssh://jim%40corp@laptop').user, 'jim@corp'); +}); + +test('?g2mirror= tunnels a g2mirror server over ssh', () => { + assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@laptop?g2mirror=tok')), { + user: 'jim', host: 'laptop', port: 22, mode: 'g2mirror', authToken: 'tok', targetHost: '127.0.0.1', targetPort: 8737, + }); + const custom = connections.parseSshConnectionString('ssh://jim@laptop?g2mirror=tok@en@localhost:9000'); + assert.equal(custom.authToken, 'tok@en'); + assert.equal(custom.targetHost, 'localhost'); + assert.equal(custom.targetPort, 9000); +}); + +test('malformed ssh:// strings are rejected', () => { + for (const url of ['ssh://laptop', 'ssh://jim@', 'ssh://jim@laptop:0', 'ssh://jim@laptop?mode=tmux', 'ssh://jim@laptop?g2mirror=', 'ssh://jim@laptop?g2mirror=tok@host:99999', 'ssh://jim@laptop?g2mirror']) { + assert.equal(connections.parseSshConnectionString(url), null, url); + } +}); + +test('any-kind parsing and host labels', () => { + assert.equal(connections.parseAnyConnectionString('g2mirror://tok@laptop').kind, 'g2mirror'); + assert.equal(connections.parseAnyConnectionString('ssh://jim@laptop').kind, 'ssh'); + assert.equal(connections.parseAnyConnectionString('http://laptop'), null); + assert.equal(connections.connectionStringHostLabel('ssh://jim@laptop?g2mirror=secret'), 'jim@laptop'); + assert.equal(connections.connectionStringHostLabel('ssh://jim@laptop:2222'), 'jim@laptop:2222'); + assert.equal(connections.connectionStringHostLabel('ssh://jim@[fd7a::1]'), 'jim@[fd7a::1]'); + assert.equal(connections.connectionStringHostLabel('g2mirror://tok@laptop'), 'laptop'); +}); + +test('stored connections keep pinned host keys', () => { + connections.saveConnections([{ id: 'c1', url: 'ssh://jim@laptop', serverName: null, enabled: true, hostKeys: ['AAAA'] }]); + connections.updateConnection('c1', { hostKeys: ['AAAA', 'BBBB'] }); + assert.deepEqual(plain(connections.loadConnections()[0].hostKeys), ['AAAA', 'BBBB']); + // Entries saved before SSH support have no hostKeys. + settings.set(connections.TERMINAL_CONNECTIONS_KEY, JSON.stringify([{ id: 'c2', url: 'g2mirror://t@h', enabled: true }])); + assert.deepEqual(plain(connections.loadConnections()[0].hostKeys), []); +}); diff --git a/tests/terminal-in-place-view.test.cjs b/tests/terminal-in-place-view.test.cjs index b0550b07..0c582c89 100644 --- a/tests/terminal-in-place-view.test.cjs +++ b/tests/terminal-in-place-view.test.cjs @@ -117,6 +117,12 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = }, '../../ui/gestures': { GESTURE_DOUBLE_CLICK: 'double-tap' }, '../../native/g2mirror-client': { G2MirrorClient: FakeClient }, + '../../native/ssh': { + copyToPhoneClipboard() {}, describeSshFailure: (failure) => failure.message, getSshSecret: () => '', + openSshChannel() { throw new Error('no SSH in this test'); }, removeSshSecret() {}, setSshSecret() {}, + sshClientKey: () => ({ authorizedKey: 'ecdsa-sha2-nistp256 AAAA test', fingerprint: 'SHA256:test' }), + }, + './tmux-client': { TmuxClient: class {} }, '../../native/settings-store': { onSettingsStoreChanged() {} }, '../../ui/dashboard-settings': { enumSettingMenuItem() {}, textSettingMenuItem() {}, toggleSettingMenuItem() {}, @@ -124,6 +130,7 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = terminalDisplayLayoutSetting: setting('default'), terminalLaunchPresetsSetting: setting(presets), terminalNewConnectionSetting: setting(''), + terminalSshPasswordSetting: setting(''), terminalWakeOnBellSetting: setting(bellWake), }, '../../ui/font-picker': { terminalFontPickerMenuItem() {} }, @@ -132,6 +139,8 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = connectionDisplayName: (config) => config.id, loadConnections: () => [{ id: 'c1', url: 'ok', serverName: 'host', enabled: true }], parseConnectionString: () => ({ host: 'host' }), + parseAnyConnectionString: () => ({ kind: 'g2mirror', secure: false, host: 'host', port: 8737, authToken: 'token' }), + connectionStringHostLabel: () => 'host', saveConnections() {}, updateConnection() {}, }, './terminal-emulator': { TerminalEmulator: FakeEmulator }, diff --git a/tests/tmux-client.test.cjs b/tests/tmux-client.test.cjs new file mode 100644 index 00000000..b32f1958 --- /dev/null +++ b/tests/tmux-client.test.cjs @@ -0,0 +1,202 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { spawn, spawnSync } = require('node:child_process'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); + +const tmux = require('../.test-build/app/apps/terminal/tmux-client.js'); + +// ---- pure helpers ---- + +test('tmux version gate', () => { + assert.deepEqual(tmux.parseTmuxVersion('tmux 3.2a'), [3, 2]); + assert.equal(tmux.tmuxVersionSupported('tmux 3.2a'), true); + assert.equal(tmux.tmuxVersionSupported('tmux 3.8'), true); + assert.equal(tmux.tmuxVersionSupported('tmux 3.1c'), false); + assert.equal(tmux.tmuxVersionSupported('tmux 2.7'), false); + assert.equal(tmux.tmuxVersionSupported('tmux next-3.9'), true); + assert.equal(tmux.tmuxVersionSupported('tmux master'), true); +}); + +test('decodeTmuxOutput undoes tmux and transport escaping', () => { + const bytes = (text) => Array.from(tmux.decodeTmuxOutput(text)); + assert.deepEqual(bytes('a\\015\\012b'), [0x61, 0x0d, 0x0a, 0x62]); + assert.deepEqual(bytes('back\\134slash'), [...Buffer.from('back\\slash')]); + assert.deepEqual(bytes('héllo ✓'), [...Buffer.from('héllo ✓')]); + // A UTF-8 character split across two %output lines arrives escaped. + assert.deepEqual([...bytes('caf\\303'), ...bytes('\\251')], [...Buffer.from('café')]); + // A lone backslash without three octal digits is literal. + assert.deepEqual(bytes('\\9'), [0x5c, 0x39]); +}); + +test('hostCommand keeps scripts portable across login shells', () => { + assert.equal(tmux.hostCommand('echo hi'), "exec /bin/sh -c 'echo hi'"); + assert.throws(() => tmux.hostCommand("echo 'hi'")); + assert.throws(() => tmux.hostCommand('echo \\\\')); + for (const command of [tmux.pollerCommand(), tmux.viewerCommand('$3'), tmux.launchCommand('shell'), tmux.launchCommand('htop -d 5')]) { + assert.match(command, /^exec \/bin\/sh -c '[^']*'$/); + } + assert.throws(() => tmux.viewerCommand('work')); + assert.throws(() => tmux.launchCommand('rm -rf ~; echo $HOME')); +}); + +test('pane rows and labels', () => { + const fields = ['%3', '$1', 'work', '@2', '1', 'zsh', '2', '0', '4242', '0', '1700000000', '3', 'zsh', '/home/jim/src', 'build\twatcher']; + const row = tmux.parsePaneRow(fields.join('\t')); + assert.equal(row.title, 'build\twatcher'); + assert.equal(row.bells, 3); + assert.equal(row.windowPanes, 2); + assert.equal(tmux.paneLabel(row, 'laptop.local', '/home/jim'), 'work:1.0 build\twatcher'); + assert.equal(tmux.paneLabel({ ...row, title: 'laptop.local' }, 'laptop.local', '/home/jim'), 'work:1.0 zsh ~/src'); + assert.equal(tmux.paneLabel({ ...row, title: 'laptop', windowPanes: 1 }, 'laptop.local', '/home/jim'), 'work:1 zsh ~/src'); + assert.equal(tmux.parsePaneRow('garbage'), null); +}); + +test('snapshot bytes restore cursor and key modes', () => { + const state = tmux.parsePaneState('%1|4|1|1|1|0|1|0|0|0|1|80|2|10|57|5|0'); + assert.equal(state.historyNext, 57); + assert.equal(state.historyOldest, 5); + const text = Buffer.from(tmux.snapshotBytes(state, ['$ ls', 'a b'], [])).toString(); + assert.equal(text, '$ ls\x1b[0m\r\na b\x1b[0m\x1b[2;5H\x1b[?1h'); + // Without history_added (tmux < 3.8), history_size stands in. + assert.equal(tmux.parsePaneState('%1|0|0|1|0|0|1|0|0|0|23|80|24|10||||').historyNext, 10); +}); + +test('send-keys commands are hex and chunked', () => { + const commands = tmux.sendKeysCommands('%4', new Uint8Array(300).fill(0x41)); + assert.equal(commands.length, 2); + assert.match(commands[0], /^send-keys -t %4 -H 41 41 /); +}); + +// ---- against a real tmux server ---- + +const tmuxVersion = spawnSync('tmux', ['-V'], { encoding: 'utf8' }); +const haveTmux = tmuxVersion.status === 0 && tmux.tmuxVersionSupported(tmuxVersion.stdout); + +/** Mimic the SSH transport: run through a shell, split stdout/stderr into lines, escape non-UTF-8 lines. */ +function localChannel(env) { + return (command, listener) => { + const child = spawn('/bin/sh', ['-c', command], { env, stdio: ['pipe', 'pipe', 'pipe'] }); + let closed = false; + // Like the SSH transport, a write to a channel whose command just exited is dropped, not thrown. + child.stdin.on('error', () => {}); + const lines = (stream, emit) => { + let buffered = Buffer.alloc(0); + stream.on('data', (chunk) => { + buffered = Buffer.concat([buffered, chunk]); + let index; + while ((index = buffered.indexOf(0x0a)) >= 0) { + emit(escapeLine(buffered.subarray(0, index))); + buffered = buffered.subarray(index + 1); + } + }); + }; + lines(child.stdout, (line) => !closed && listener.onLine(line)); + lines(child.stderr, (line) => !closed && listener.onStderr(line)); + child.on('spawn', () => listener.onOpen()); + child.on('exit', (status) => { if (!closed) { closed = true; listener.onClosed(status); } }); + return { + send: (line) => { if (closed) return false; child.stdin.write(`${line}\n`); return true; }, + close: () => { if (closed) return; closed = true; child.stdin.end(); setTimeout(() => child.kill(), 500); }, + }; + }; +} + +function escapeLine(bytes) { + const text = bytes.toString('utf8'); + if (Buffer.from(text, 'utf8').equals(bytes)) return text; + return Array.from(bytes, (byte) => (byte >= 0x80 ? `\\${byte.toString(8).padStart(3, '0')}` : String.fromCharCode(byte))).join(''); +} + +function waitFor(predicate, what, timeoutMs = 10_000) { + return new Promise((resolve, reject) => { + const started = Date.now(); + const timer = setInterval(() => { + let value; + try { value = predicate(); } catch (error) { clearInterval(timer); reject(error); return; } + if (value) { clearInterval(timer); resolve(value); } + else if (Date.now() - started > timeoutMs) { clearInterval(timer); reject(new Error(`timed out waiting for ${what}`)); } + }, 50); + }); +} + +test('TmuxClient against a local tmux server', { skip: !haveTmux && 'tmux >= 3.2 not installed' }, async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fc-tmux-')); + const env = { ...process.env, TMUX_TMPDIR: dir, SHELL: '/bin/sh', PS1: '$ ' }; + delete env.TMUX; + const tm = (...args) => spawnSync('tmux', args, { env, encoding: 'utf8' }); + const openChannel = localChannel(env); + const clients = []; + try { + tm('new-session', '-d', '-s', 'work', '-x', '120', '-y', '40', '/bin/sh'); + const control = new tmux.TmuxClient({ openChannel, label: 'test', cols: 80, rows: 20, stateKey: 'test-connection' }); + clients.push(control); + const bells = []; + control.onBell((socket) => bells.push(socket)); + control.start(); + await waitFor(() => control.state().phase === 'connected' && control.state().sessions.length === 1, 'listing'); + assert.equal(control.state().sessions[0].socket, '%0'); + assert.match(control.state().sessions[0].title, /^work:0 /); + + // A bell in the session's current window (tmux's own bell flag misses these). + tm('send-keys', '-t', '%0', '-l', "printf '\\a'"); + tm('send-keys', '-t', '%0', 'Enter'); + await waitFor(() => bells.includes('%0'), 'bell'); + + const launched = await control.launchSession('shell'); + assert.match(launched, /^%\d+$/); + await waitFor(() => control.state().sessions.some((s) => s.socket === launched), 'launched pane in listing'); + + // View the original pane. + const view = new tmux.TmuxClient({ openChannel, label: 'test', cols: 80, rows: 20, stateKey: '' }); + clients.push(view); + let screen = ''; + const snapshots = []; + const histories = []; + view.onTerminalData((data, kind) => { + if (kind === 'snapshot') { screen = ''; snapshots.push(Buffer.from(data).toString()); } + screen += Buffer.from(data).toString(); + }); + view.onSnapshot((next, oldest) => snapshots.push({ next, oldest })); + view.onHistoryLines((reply) => histories.push(reply)); + view.onStateChange((state) => { + if (state.phase === 'connected' && !view.requested) { view.requested = true; view.connectSession('%0'); } + }); + view.onSessionAttached(() => view.view()); + view.start(); + await waitFor(() => snapshots.length >= 2, 'snapshot'); + assert.equal(view.state().phase, 'attached'); + assert.equal(tm('display', '-p', '-t', '%0', '#{window_width}x#{window_height}').stdout.trim(), '80x20'); + + view.sendInput('for i in $(seq 1 60); do echo row$i; done\r'); + await waitFor(() => screen.includes('row60'), 'typed command output'); + + // Re-snapshot: rows have scrolled into the history; page some back. + view.unview(); + view.view(); + await waitFor(() => snapshots.length >= 4, 'second snapshot'); + const { next } = snapshots[snapshots.length - 1]; + assert.ok(next > 20, `history should have grown, next=${next}`); + view.requestHistory(next, 5); + await waitFor(() => histories.length === 1, 'history page'); + assert.equal(histories[0].lines.length, 5); + assert.equal(histories[0].start, next - 5); + assert.ok(histories[0].lines.some((line) => /^row\d+$/.test(line)), JSON.stringify(histories[0].lines)); + + // Unviewing gives the window back its own size. + view.unview(); + await waitFor(() => tm('display', '-p', '-t', '%0', '#{window_width}').stdout.trim() !== '80', 'size restored', 3000).catch(() => {}); + + // The pane going away ends the view. + let detached = null; + view.onSessionDetached((reason) => { detached = reason; }); + view.view(); + tm('kill-pane', '-t', '%0'); + await waitFor(() => detached !== null, 'detach on pane exit'); + } finally { + for (const client of clients) client.stop(); + tm('kill-server'); + fs.rmSync(dir, { recursive: true, force: true }); + } +}); diff --git a/tests/transcribe.test.cjs b/tests/transcribe.test.cjs index 1eef452c..7ee55802 100644 --- a/tests/transcribe.test.cjs +++ b/tests/transcribe.test.cjs @@ -4,6 +4,7 @@ const { loader } = require('./helpers/load-typescript.cjs'); const load = loader(); const { TranscriptModel, TranscriptScroll, transcriptLayout, wrapTranscribeText } = load('app/apps/transcribe/transcript-model.ts'); const { SpeechPauseDetector } = load('app/native/speech-pause.ts'); +const { formatReplayTime, stepReplaySeconds } = load('app/apps/transcribe/replay-time.ts'); const { TimedTranscript } = load('app/native/transcript-format.ts'); const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); @@ -128,3 +129,33 @@ test('pause detector uses 1.5 seconds of PCM, fires once per pause, and ignores d.accept(pcm(3000)); assert.equal(d.accept(pcm(0, 1500)), true); }); + +test('a replay jump leaves its time on a line of its own and starts a new paragraph', () => { + const m = new TranscriptModel(); + m.mark('[0:30]'); + assert.equal(m.text, '[0:30]'); + m.accept({ text: 'After the jump', isFinal: false }); + assert.equal(m.text, '[0:30]\nAfter the jump'); + // What was still being spoken when the replay jumped is kept, ahead of the mark. + m.mark('[1:15]'); + m.accept({ text: 'Later.', isFinal: true }); + m.accept({ text: 'More.', isFinal: true }); + assert.equal(m.text, '[0:30]\nAfter the jump\n[1:15]\nLater. More.'); +}); + +test('replay times read as m:ss, with hours from an hour on', () => { + assert.equal(formatReplayTime(0), '0:00'); + assert.equal(formatReplayTime(83_999), '1:23'); + assert.equal(formatReplayTime(3_600_000), '1:00:00'); + assert.equal(formatReplayTime(83_000, true), '0:01:23'); + assert.equal(formatReplayTime(-5), '0:00'); +}); + +test('the time picker steps carry between fields and stay inside the recording', () => { + assert.equal(stepReplaySeconds(59, 'seconds', 1, 600), 60); + assert.equal(stepReplaySeconds(60, 'seconds', -1, 600), 59); + assert.equal(stepReplaySeconds(30, 'minutes', 2, 600), 150); + assert.equal(stepReplaySeconds(590, 'minutes', 1, 600), 600); + assert.equal(stepReplaySeconds(30, 'minutes', -1, 600), 0); + assert.equal(stepReplaySeconds(100, 'hours', 1, 7300), 3700); +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 7f1d06f5..dc704cf4 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -16,6 +16,18 @@ "../app/remote/protocol.ts", "../app/remote/listeners.ts", "../app/g2/ancs-client.ts", + "../app/g2/keyboard/protocol.ts", + "../app/g2/keyboard/hid.ts", + "../app/g2/keyboard/client.ts", + "../app/g2/keyboard/bridge.ts", + "../app/g2/keyboard/keys.ts", + "../app/g2/keyboard/bindings.ts", + "../app/g2/keyboard/key-router.ts", + "../app/g2/keyboard/pointer.ts", + "../app/apps/terminal/terminal-keys.ts", + "../app/native/terminal-session-types.ts", + "../app/apps/terminal/terminal-client.ts", + "../app/apps/terminal/tmux-client.ts", "../app/phone-ui/ble-bandwidth-meter.ts", "../app/apps/terminal/hub-header.ts", "../app/g2/ios-peripheral-identity.ts", @@ -38,6 +50,7 @@ "../app/g2/glasses-artwork.ts", "../app/g2/glasses-display-state.ts", "../app/g2/glance-state.ts", + "../app/native/imu-types.ts", "../app/ui/shell/notification-modal-queue.ts", "../app/ui/shell/notification-popup-delay.ts", "../app/ui/shell/notification-popup-timeout.ts", diff --git a/tests/voice-finalization.test.cjs b/tests/voice-finalization.test.cjs index 3050d7c8..9c104cd5 100644 --- a/tests/voice-finalization.test.cjs +++ b/tests/voice-finalization.test.cjs @@ -164,6 +164,8 @@ function androidBridge(modules = {}) { setListener(listener) { this.listener = listener; }, setCommunicator() {}, setUsePhoneMic() {}, setEndpointing() {}, setNoiseSuppression() {}, setBeamFilter() {}, clearSpeakerVerification() {}, setOnboardModelKind() {}, + setMicArray() {}, setReplay() {}, setRecordingEnabled(enabled) { this.recordingEnabled = enabled; }, + noteRecordingEvent(json) { (this.recordingEvents ??= []).push(JSON.parse(json)); }, start(_mode, id) { this.captureId = id; this.active = true; this.starts++; }, stop() { this.active = false; }, isCapturing() { return this.active; }, @@ -217,24 +219,23 @@ test('a shared capture restarts for a holder that needs another provider, and is assert.equal(bridge.holdsCapture('ptt'), true); }); -test('Android continuous captures record to the requested WAV, and report one that cannot be created', () => { +test('Android continuous captures record to the requested file, and report one that cannot be created', () => { const recordings = []; let fail = false, attempts = 0; - class FakeRecorder { - static open(name) { - attempts++; - if (fail) return null; - const r = new FakeRecorder(); r.name = name; r.path = `/Download/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r; - } - append(pcm) { this.pcm.push(...pcm); } - close() { this.closed = true; } - } const { bridge, controller } = androidBridge({ - './wav-recorder': { WavRecorder: FakeRecorder }, + './mic-recording': { newRecordingPath: name => `/Download/${name}.mka` }, './speech-pause': { SpeechPauseDetector: class { reset() {} accept() { return false; } } }, '../util/array-util': { toUint8Array: pcm => Uint8Array.from(pcm) }, '../util/audio-level': { pcmLevel: () => 0 }, }); + // The controller writes the file itself, from the capture's own thread. + controller.startRecording = (path, json) => { + attempts++; + if (fail) return false; + recordings.push({ path, info: JSON.parse(json), closed: false }); + return true; + }; + controller.stopRecording = () => { recordings.at(-1).closed = true; return 0; }; const options = { provider: 'onboard' }; let changes = 0; bridge.onRecordingChange(() => changes++); bridge.recordTo('transcribe-1'); @@ -242,20 +243,24 @@ test('Android continuous captures record to the requested WAV, and report one th // Push-to-talk on a capture of its own is not recorded, and the file waits for audio to save. bridge.startPushToTalk(options); controller.listener.onPcm([9, 9]); - assert.equal(attempts, 0); + assert.deepEqual([attempts, controller.recordingEnabled], [0, false]); bridge.startContinuousCapture(options); controller.listener.onPcm([1, 2]); controller.listener.onPcm([3, 4]); - assert.deepEqual(recordings.map(r => [r.name, r.pcm, r.closed]), [['transcribe-1', [1, 2, 3, 4], false]]); - // The file outlives a capture: one restarted after the glasses reconnect keeps appending to it. + assert.deepEqual(recordings.map(r => [r.path, r.info.recording, r.closed]), [['/Download/transcribe-1.mka', 'transcribe-1', false]]); + assert.deepEqual([attempts, controller.recordingEnabled], [1, true]); + // The file outlives a capture: one restarted after the glasses reconnect goes on in it, + // with the restart noted, and nothing is recorded while no capture holds the mic. bridge.handleSessionEnded(); + assert.equal(controller.recordingEnabled, false); bridge.resumeCapture(options); controller.listener.onPcm([5]); - assert.deepEqual(recordings.map(r => [r.pcm, r.closed]), [[[1, 2, 3, 4, 5], false]]); + assert.deepEqual([attempts, recordings[0].closed, controller.recordingEnabled], [1, false, true]); + assert.deepEqual(controller.recordingEvents.map(event => [event.event, event.recording]), [['capture', 'transcribe-1']]); bridge.recordTo(''); assert.deepEqual([recordings[0].closed, bridge.recording, changes], [true, false, 2]); controller.listener.onPcm([6]); - assert.equal(recordings[0].pcm.length, 5); + assert.equal(attempts, 1); // One attempt per recording, not one per packet; another recording is a fresh attempt. fail = true; attempts = 0; @@ -269,5 +274,5 @@ test('Android continuous captures record to the requested WAV, and report one th bridge.recordTo(''); bridge.recordTo('transcribe-2'); controller.listener.onPcm([10]); - assert.deepEqual([bridge.recording, bridge.recordingFailed, recordings.at(-1).pcm], [true, false, [10]]); + assert.deepEqual([bridge.recording, bridge.recordingFailed, recordings.at(-1).path], [true, false, '/Download/transcribe-2.mka']); }); diff --git a/tests/voice-recording.test.cjs b/tests/voice-recording.test.cjs new file mode 100644 index 00000000..5255726a --- /dev/null +++ b/tests/voice-recording.test.cjs @@ -0,0 +1,209 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +/** The Android voice bridge over a fake native controller that records what it is told. */ +function harness({ recordingOk = true, downloads = '/downloads' } = {}) { + const controllers = []; + class FakeController { + constructor() { + this.calls = []; this.events = []; this.capturing = false; this.recording = null; + this.micArray = null; this.replay = null; this.recordingEnabled = null; + controllers.push(this); + // Setters the tests don't look at are only logged. + return new Proxy(this, { get: (target, name) => name in target ? target[name] : (...args) => { target.calls.push([name, ...args]); } }); + } + setListener(listener) { this.listener = listener; } + setMicArray(record) { this.micArray = record; } + setReplay(path, startMs) { this.replay = path === null ? null : { path, startMs }; } + start(mode) { this.calls.push(['start', mode]); this.capturing = true; } + stop() { this.calls.push(['stop']); this.capturing = false; } + isCapturing() { return this.capturing; } + startRecording(path, json) { if (recordingOk) this.recording = { path, info: JSON.parse(json) }; return recordingOk; } + setRecordingEnabled(enabled) { this.recordingEnabled = enabled; } + noteRecordingEvent(json) { this.events.push(JSON.parse(json)); } + stopRecording() { this.calls.push(['stopRecording']); this.recording = null; return 0; } + seekReplay(ms) { this.calls.push(['seekReplay', ms]); } + setReplayPaused(paused) { this.calls.push(['setReplayPaused', paused]); } + } + const module = loader({ + global: { isAndroid: true }, + com: { faceclaw: { app: { FaceclawVoiceController: FakeController, FaceclawVoiceControllerListener: function (impl) { return impl; } } } }, + }, { + '@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } }, + './cloud-stt': { toJavaBytes: bytes => Array.from(bytes) }, + './cloud-stt-provider': { + createCloudSttClient: () => null, + transcriptionKey: options => `${options.provider}:${options.language ?? ''}`, + }, + './mic-recording': { newRecordingPath: name => downloads === null ? null : `${downloads}/${name}.mka` }, + '../util/array-util': { toUint8Array: value => value }, + })('app/native/voice-control.ts'); + const options = (extra = {}) => ({ + communicator: {}, provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: '', sonioxApiKey: '', ...extra, + }); + return { ...module, controllers, options, pcm: controller => controller.listener.onPcm(new Uint8Array(1600)) }; +} + +const CONFIGURE_2 = Uint8Array.from([0x4d, 0x43, 1, 1, 1, 1, 1, 0, 160, 0, 0, 0, 2]); +const CONFIGURE_4 = Uint8Array.from([0x4d, 0x43, 1, 1, 0, 3, 1, 0, 160, 0, 0, 0, 2]); + +test('a capture of the temples\' microphones arms them, and another microphone count restarts the stream', () => { + const h = harness(); + const bridge = h.voiceControlBridge; + bridge.startContinuousCapture(h.options({ micArray: { configure: CONFIGURE_2 } })); + const controller = h.controllers[0]; + assert.deepEqual(controller.micArray, Array.from(CONFIGURE_2)); + assert.equal(controller.replay, null); + assert.deepEqual(controller.calls.filter(call => call[0] === 'start'), [['start', 'onboard']]); + // Push-to-talk asks for the same thing while the window is open: it shares the stream. + bridge.startPushToTalk(h.options({ micArray: { configure: CONFIGURE_2 } })); + assert.equal(controller.calls.filter(call => call[0] === 'start').length, 1); + bridge.stopPushToTalk(); + // The Microphones setting changed. + bridge.retargetContinuousCapture(h.options({ micArray: { configure: CONFIGURE_4 } })); + assert.deepEqual(controller.micArray, Array.from(CONFIGURE_4)); + assert.deepEqual(controller.calls.filter(call => call[0] === 'start' || call[0] === 'stop').map(call => call[0]), ['start', 'stop', 'start']); + // Back to the mixed stream; the phone's microphone never gets an array. + bridge.retargetContinuousCapture(h.options()); + assert.equal(controller.micArray, null); + bridge.retargetContinuousCapture(h.options({ communicator: null, usePhoneMic: true, micArray: { configure: CONFIGURE_4 } })); + assert.equal(controller.micArray, null); +}); + +test('a recording opens with the first audio, describes its capture, and follows the capture across restarts', () => { + const h = harness(); + const bridge = h.voiceControlBridge; + const changes = []; + bridge.onRecordingChange(() => changes.push(bridge.recording)); + bridge.recordTo('transcribe-1'); + assert.equal(bridge.recording, true); + bridge.startContinuousCapture(h.options({ micArray: { configure: CONFIGURE_4 }, noiseSuppression: true, captureInfo: { mics: 4, glasses: 'G2' } })); + const controller = h.controllers[0]; + // Nothing on disk until audio arrives: a capture that never starts leaves no file. + assert.equal(controller.recording, null); + assert.equal(controller.recordingEnabled, true); + h.pcm(controller); + assert.equal(controller.recording.path, '/downloads/transcribe-1.mka'); + const info = controller.recording.info; + assert.equal(info.recording, 'transcribe-1'); + assert.equal(info.source, 'glasses'); + assert.equal(info.micArray, true); + assert.equal(info.mics, 4); + assert.equal(info.glasses, 'G2'); + assert.deepEqual(info.transcription, { provider: 'onboard', language: '' }); + assert.deepEqual(info.processing, { noiseSuppression: true, beamFilter: null }); + h.pcm(controller); + assert.equal(controller.calls.filter(call => call[0] === 'stopRecording').length, 0); + + // The capture restarts with other microphones: the same file goes on, with the change noted in it. + bridge.retargetContinuousCapture(h.options({ captureInfo: { mics: 1, glasses: 'G2' } })); + assert.equal(controller.events.length, 1); + assert.equal(controller.events[0].event, 'capture'); + assert.equal(controller.events[0].mics, 1); + assert.equal(controller.events[0].micArray, false); + assert.equal(controller.recording.path, '/downloads/transcribe-1.mka'); + + // Save Recording was turned off. + bridge.recordTo(''); + assert.equal(bridge.recording, false); + assert.equal(controller.recording, null); + assert.deepEqual(changes, [true, false]); +}); + +test('push-to-talk on a capture of its own is not recorded', () => { + const h = harness(); + const bridge = h.voiceControlBridge; + bridge.recordTo('transcribe-1'); + bridge.startPushToTalk(h.options()); + const controller = h.controllers[0]; + assert.equal(controller.recordingEnabled, false); + h.pcm(controller); + assert.equal(controller.recording, null); + // The Transcribe window joins the running stream: from here it is recorded. + bridge.startContinuousCapture(h.options()); + assert.equal(controller.recordingEnabled, true); + h.pcm(controller); + assert.equal(controller.recording.path, '/downloads/transcribe-1.mka'); + bridge.stopContinuousCapture(); + assert.equal(controller.recordingEnabled, false); +}); + +test('a recording that cannot be created is reported once', () => { + for (const h of [harness({ recordingOk: false }), harness({ downloads: null })]) { + const bridge = h.voiceControlBridge; + bridge.recordTo('transcribe-1'); + bridge.startContinuousCapture(h.options()); + h.pcm(h.controllers[0]); + assert.equal(bridge.recording, false); + assert.equal(bridge.recordingFailed, true); + } +}); + +test('a replay runs on its own controller, follows its events, jumps while playing and restarts after the end', () => { + const h = harness(); + const replay = new h.VoiceReplay('/downloads/talk.mka', () => h.options({ communicator: {}, micArray: { configure: CONFIGURE_4 } })); + const seen = []; + replay.onChange(jumpedToMs => seen.push([replay.state, replay.positionMs, jumpedToMs])); + assert.equal(h.controllers.length, 0); + replay.start(); + // The microphone's bridge has not been touched. + assert.equal(h.controllers.length, 1); + const controller = h.controllers[0]; + assert.deepEqual(controller.replay, { path: '/downloads/talk.mka', startMs: 0 }); + assert.equal(controller.micArray, null); + assert.equal(replay.state, 'opening'); + const statuses = []; + replay.events.onStatus(state => statuses.push(state.status)); + assert.equal(statuses.at(-1), 'Opening the recording...'); + + controller.listener.onReplay('start', 0, 60_000); + assert.equal(replay.state, 'playing'); + assert.equal(replay.durationMs, 60_000); + controller.listener.onReplay('position', 1000, 60_000); + assert.equal(replay.positionMs, 1000); + // A plain start and the ticking position are not jumps. + assert.deepEqual(seen.map(entry => entry[2]), [undefined, undefined, undefined]); + + replay.setPaused(true); + assert.equal(replay.state, 'paused'); + replay.setPaused(false); + replay.jumpTo(90_000); // past the end: held to the recording's length + assert.deepEqual(controller.calls.filter(call => call[0] === 'seekReplay' || call[0] === 'setReplayPaused'), + [['setReplayPaused', true], ['setReplayPaused', false], ['seekReplay', 60_000]]); + replay.jumpTo(30_000); + controller.listener.onReplay('seek', 30_000, 60_000); + assert.deepEqual(seen.at(-1), ['playing', 30_000, 30_000]); + + controller.listener.onReplay('end', 60_000, 60_000); + controller.capturing = false; + assert.equal(replay.state, 'ended'); + assert.equal(replay.positionMs, 60_000); + replay.setPaused(true); // nothing to pause + assert.equal(replay.state, 'ended'); + + // A jump once it has ended plays again from there. + replay.jumpTo(45_000); + assert.deepEqual(controller.replay, { path: '/downloads/talk.mka', startMs: 45_000 }); + assert.equal(replay.state, 'opening'); + controller.listener.onReplay('start', 45_000, 60_000); + assert.deepEqual(seen.at(-1), ['playing', 45_000, 45_000]); + + replay.close(); + assert.equal(controller.calls.at(-1)[0], 'stop'); + replay.start(); + assert.equal(controller.calls.at(-1)[0], 'stop'); +}); + +test('a recording that cannot be opened fails the replay', () => { + const h = harness(); + const replay = new h.VoiceReplay('/downloads/missing.mka', () => h.options()); + replay.start(); + const controller = h.controllers[0]; + controller.listener.onStatus('Could not open the recording: no such file'); + controller.listener.onReplay('error', 0, 0); + assert.equal(replay.state, 'failed'); + const statuses = []; + replay.events.onStatus(state => statuses.push(state.status)); + assert.deepEqual(statuses, ['Could not open the recording: no such file']); +}); diff --git a/tests/wav-header.test.cjs b/tests/wav-header.test.cjs deleted file mode 100644 index ce2744a6..00000000 --- a/tests/wav-header.test.cjs +++ /dev/null @@ -1,19 +0,0 @@ -const test = require('node:test'); -const assert = require('node:assert/strict'); -const { loader } = require('./helpers/load-typescript.cjs'); - -test('the WAV header describes 16 kHz mono PCM16 and its data size', () => { - const { wavHeader } = loader({ DataView }, {})('app/native/wav-header.ts'); - const header = Buffer.from(wavHeader(32000)); - assert.equal(header.length, 44); - assert.equal(header.toString('ascii', 0, 4), 'RIFF'); - assert.equal(header.readUInt32LE(4), 36 + 32000); - assert.equal(header.toString('ascii', 8, 16), 'WAVEfmt '); - assert.equal(header.readUInt16LE(20), 1); - assert.equal(header.readUInt16LE(22), 1); - assert.equal(header.readUInt32LE(24), 16000); - assert.equal(header.readUInt32LE(28), 32000); - assert.equal(header.readUInt16LE(34), 16); - assert.equal(header.toString('ascii', 36, 40), 'data'); - assert.equal(header.readUInt32LE(40), 32000); -});