Refactor Java to shared kotlin

This commit is contained in:
James Babcock committed 2026-09-18 22:48:20 -07:00
1 parent bc8e80c533
commit 68810197a8
173 files changed
+9855 -6252

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
/** Bulk binary bridge; slices are normalized without per-byte native calls. */
export function toData(bytes: Uint8Array): NSData {
const buffer = bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength
? bytes.buffer : bytes.slice().buffer
return NSData.dataWithBytesLength(interop.handleof(buffer), bytes.byteLength)
}
export function fromData(data: NSData): Uint8Array {
return new Uint8Array(interop.bufferFromData(data)).slice()
}
export function nativeArray<T, R>(items: NSArray<T>, convert: (item: T) => R): R[] {
return Array.from({ length: items.count }, (_, i) => convert(items.objectAtIndex(i)))
}
+2
View File
@@ -1,3 +1,4 @@
import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke';
import type { KotlinBridge } from './kotlin-platform';
// Handwritten declarations for the small native surface, checked against the
@@ -17,6 +18,7 @@ declare namespace com.faceclaw.shared {
}
export function createKotlinBridge(): KotlinBridge {
runKotlinProtocolSmokeTest();
const bridge = new com.faceclaw.shared.KotlinBridge();
return {
platform: () => bridge.platform(),
+2
View File
@@ -1,3 +1,4 @@
import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke.ios';
import type { KotlinBridge } from './kotlin-platform';
// Handwritten declarations matching FaceclawKit's Objective-C header.
@@ -26,6 +27,7 @@ class KotlinCallback extends NSObject implements FaceclawKitKotlinBridgeListener
}
export function createKotlinBridge(): KotlinBridge {
runKotlinProtocolSmokeTest();
const bridge = FaceclawKitKotlinBridge.alloc().init();
return {
platform: () => bridge.platform(),
+36
View File
@@ -0,0 +1,36 @@
import * as protocol from '../g2/ble-protocol.ios'
import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios'
import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios'
import { lvglMetrics } from './lvgl-font.ios'
/** Development-only checks of real NativeScript selectors and bulk data crossings. */
export function runKotlinProtocolSmokeTest(): void {
const hex = (data: Uint8Array) => Array.from(data, byte => byte.toString(16).padStart(2, '0')).join('')
const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) }
const auth = protocol.authentication(101)
assert(hex(auth) === '080410651a0408011004', 'authentication')
assert(protocol.crc16(new Uint8Array([1, 2, 3])) === 0xadad, 'CRC')
const receiver = new protocol.MessageReceiver()
const frame = protocol.frameMessage(auth, 128, 0, 64)[0]
const received = receiver.receive('test', frame)
assert(received.length === 1 && received[0].magic === 101 && hex(received[0].payload) === hex(auth), 'reassembly')
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2))))
const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 })
assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event')
const transport = new CfwTransport()
const packets = transport.encode(new Uint8Array(128).fill(9), 255, 3, 185)
assert(packets.length > 0 && packets.every(p => p.length <= 185), 'compression')
transport.reset()
assert(parseCfwAcks(new Uint8Array()) === null, 'invalid ACK')
const c = new SurfaceCompositor(3, 2)
c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' })
c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 })
assert(hex(c.composite()) === '0010ff203040', 'composition')
assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing')
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
const changed = new Uint8Array(16); changed[0] = 255
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS iOS')
}
+20
View File
@@ -0,0 +1,20 @@
declare const com: any
/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */
export function runKotlinProtocolSmokeTest(): void {
const native = com.faceclaw.app
const hex = (data: any) => Array.from({ length: data.length }, (_, i) => (data[i] & 255).toString(16).padStart(2, '0')).join('')
const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) }
const auth = native.BleProtocol.buildAuthenticationRequest(101)
assert(hex(auth) === '080410651a0408011004', 'authentication')
const compositor = new native.SurfaceCompositor()
compositor.configureScreen(3, 2)
compositor.configureSurface('test', 0, 0, 3, 2, 0, 0)
const pixels = new Uint8Array([0, 16, 255, 32, 48, 64])
compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1')
assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer')
assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing')
const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer))
assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android')
}
+5 -16
View File
@@ -1,16 +1,5 @@
import { LvglFont } from '../graphics/lvgl-font'
import { readBinaryFile } from './file-access'
const cache = new Map<string, LvglFont>()
function load(path: string): LvglFont {
let font = cache.get(path)
if (!font) {
const bytes = readBinaryFile(path)
if (!bytes) throw new Error('Cannot read LVGL font')
font = new LvglFont(bytes)
if (cache.size >= 2) cache.delete(cache.keys().next().value!)
cache.set(path, font)
}
return font
}
export function lvglMetrics(path: string): Uint8Array { return load(path).metrics }
export function lvglGlyph(path: string, codePoint: number): Uint8Array { return load(path).glyph(codePoint) }
import { fromData } from './kotlin-data'
declare const FaceclawKitIosProtocol: any
const protocol = FaceclawKitIosProtocol.new()
export function lvglMetrics(path: string): Uint8Array { return fromData(protocol.fontMetricsPath(path)) }
export function lvglGlyph(path: string, codePoint: number): Uint8Array { return fromData(protocol.fontGlyphPathCodePoint(path, codePoint)) }