forked from Blendman974/faceclaw
Add bluetooth keyboard support
G2 only: You can pair the glasses with a bluetooth keyboard. It will relay key events, and you can use it to type in some apps and to navigate the UI.
This commit is contained in:
1 parent
9372b2742c
commit
fd3c26333f
46 files changed
+3867
-41
No files matched your search
@@ -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,7 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
|
||||
},
|
||||
'../font-picker': { uiFontPickerMenuItem: () => ({ label: 'Font' }) },
|
||||
'../notification-filter': { NotificationFilterLayer: class {} },
|
||||
'../keyboard-settings': { keyboardSettingsMenuItem: () => ({ label: 'Bluetooth keyboard' }) },
|
||||
'../openai-compatible-model-picker': {
|
||||
compatibleModelDiscoveryMenuItem: () => ({ label: 'Discover models' }),
|
||||
},
|
||||
@@ -83,6 +85,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 +151,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');
|
||||
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);
|
||||
});
|
||||
@@ -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 },
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -0,0 +1,509 @@
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const { HidKeys, parseReportMap, reportKeys } = 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) }
|
||||
}
|
||||
})
|
||||
|
||||
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} still types`, async () => {
|
||||
const events = [], 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), log: line => logs.push(line) })
|
||||
await client.enable(); await client.connect(1, new Uint8Array(6)); const config = await client.configure()
|
||||
// Keys and media keys are subscribed; the pointer and the battery are not.
|
||||
assert.deepEqual(config.watched, [27,31]); assert.deepEqual(g.watches, [27,31,3]); assert.deepEqual(g.writes, [25,4,28,32])
|
||||
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]])
|
||||
// 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\) carries no keys/.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: [], 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),
|
||||
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: 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()
|
||||
})
|
||||
@@ -0,0 +1,157 @@
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const { KeyTranslator, translateUsage, navigationGestureForKey } = 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('navigation keys stand in for ring gestures; everything else does nothing', () => {
|
||||
const gesture = (k, extra = {}) => navigationGestureForKey(key({ key: k, text: undefined, ...extra }))
|
||||
assert.equal(gesture('ArrowUp'), 'scroll-up'); assert.equal(gesture('ArrowDown'), 'scroll-down')
|
||||
assert.equal(gesture('Enter'), 'click'); assert.equal(gesture('ArrowRight'), 'click')
|
||||
assert.equal(gesture('Escape'), 'double-click'); assert.equal(gesture('ArrowLeft'), 'double-click')
|
||||
// Holding an arrow keeps scrolling; holding Enter or Escape acts once.
|
||||
assert.equal(gesture('ArrowDown', { repeat: true }), 'scroll-down')
|
||||
assert.equal(gesture('Enter', { repeat: true }), null); assert.equal(gesture('Escape', { repeat: true }), null)
|
||||
assert.equal(gesture('Enter', { phase: 'up' }), null)
|
||||
assert.equal(gesture('Enter', { modifiers: { ctrl: true } }), null)
|
||||
assert.equal(gesture('a'), null); assert.equal(gesture(' '), null); assert.equal(gesture('Backspace'), null)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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']);
|
||||
});
|
||||
@@ -16,6 +16,12 @@
|
||||
"../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/apps/terminal/terminal-keys.ts",
|
||||
"../app/phone-ui/ble-bandwidth-meter.ts",
|
||||
"../app/apps/terminal/hub-header.ts",
|
||||
"../app/g2/ios-peripheral-identity.ts",
|
||||
|
||||
Reference in new issue
Block a user