From 2328c418cf80535035b8ddd525b9dbba80a36a59 Mon Sep 17 00:00:00 2001 From: Onyx Date: Fri, 20 Nov 2026 03:38:24 +0100 Subject: [PATCH] feat(bridge): ask Bridge for VIA boards by their vendor-defined usage Filters with no vendor or product id on a vendor-defined usage page (VIA's raw HID, 0xFF60:0x61) were dropped when asking Bridge for devices, so Bridge never listed a VIA keyboard. They are now sent as `usages`. Standard usage pages are still never sent, and a Bridge that does not know the field ignores it. --- src/features/bridge/bridge-hid.test.ts | 19 +++++++++++++++++++ src/features/bridge/bridge-hid.ts | 19 ++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/features/bridge/bridge-hid.test.ts b/src/features/bridge/bridge-hid.test.ts index 52e4b41e..8ed7b700 100644 --- a/src/features/bridge/bridge-hid.test.ts +++ b/src/features/bridge/bridge-hid.test.ts @@ -84,6 +84,25 @@ test("drives a device end to end over the socket", async () => { fake.transport.onClose?.(); }); +test("a vendor-less filter on a vendor-defined page is sent as a usage, never a standard one", async () => { + const fake = fakeTransport(); + const hid = bridgeHid(fake.transport); + + const scan = hid.requestDevice({ + filters: [{ usagePage: 0xff60, usage: 0x61 }, { usagePage: 0xff60, usage: 0x61 }, { usagePage: 0x01, usage: 0x06 }, { vendorId: 0x3554, usagePage: 0xff02 }], + }); + assert.deepEqual(fake.sent[0].usages, [{ usagePage: 0xff60, usage: 0x61 }]); + assert.deepEqual(fake.sent[0].vendorIds, [0x3554]); + fake.reply({ id: fake.sent[0].id, ok: true, devices: [] }); + await scan; + + const plain = hid.requestDevice({ filters: [{ vendorId: 0x3554 }] }); + assert.equal("usages" in fake.sent[1], false); + fake.reply({ id: fake.sent[1].id, ok: true, devices: [] }); + await plain; + fake.transport.onClose?.(); +}); + test("a rejected request surfaces the reason Bridge gave", async () => { const fake = fakeTransport(); const hid = bridgeHid(fake.transport); diff --git a/src/features/bridge/bridge-hid.ts b/src/features/bridge/bridge-hid.ts index 8ee7b024..d04cd02b 100644 --- a/src/features/bridge/bridge-hid.ts +++ b/src/features/bridge/bridge-hid.ts @@ -81,7 +81,7 @@ interface Reply { } type Command = - | { type: "list"; vendorIds: number[] } + | { type: "list"; vendorIds: number[]; usages?: { usagePage: number; usage?: number }[] } | { type: "open"; device: string } | { type: "close"; device: string } | { type: "listen"; device: string } @@ -141,6 +141,19 @@ function vendorIdsFor(filters: HIDDeviceFilter[]): number[] { return [...ids]; } +const VENDOR_DEFINED_USAGE_PAGE = 0xff00; + +function listCommand(filters: HIDDeviceFilter[]): Command { + const usages = new Map(); + for (const filter of filters) { + if (filter.vendorId !== undefined || filter.productId !== undefined) continue; + if (filter.usagePage === undefined || filter.usagePage < VENDOR_DEFINED_USAGE_PAGE) continue; + usages.set(`${filter.usagePage}:${filter.usage ?? ""}`, { usagePage: filter.usagePage, usage: filter.usage }); + } + const vendorIds = vendorIdsFor(filters); + return usages.size > 0 ? { type: "list", vendorIds, usages: [...usages.values()] } : { type: "list", vendorIds }; +} + // Listeners are kept in a set rather than by extending EventTarget: WebHID // declares a narrower listener type than EventTarget's, which no override can // satisfy under strict mode. Both other HID adapters in this project (Desktop's @@ -467,7 +480,7 @@ class BridgeHid implements HID { } async #listDevices(): Promise { - const reply = await this.#client.request({ type: "list", vendorIds: vendorIdsFor(HID_FILTERS) }); + const reply = await this.#client.request(listCommand(HID_FILTERS)); const { devices, added, removed } = this.#client.reconcile(reply.devices ?? []); if (this.#enumerated) { for (const device of added) this.#emit("connect", device); @@ -479,7 +492,7 @@ class BridgeHid implements HID { } async requestDevice(options: { filters: HIDDeviceFilter[] }): Promise { - const reply = await this.#client.request({ type: "list", vendorIds: vendorIdsFor(options.filters) }); + const reply = await this.#client.request(listCommand(options.filters)); const { devices } = this.#client.reconcile(reply.devices ?? []); this.#enumerated = true; this.#startPolling();