From 68f3e869139b2a846e2be4209f5201f7e4893494 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Wed, 1 Nov 2023 15:26:25 +0000 Subject: [PATCH 1/3] fix: filter for correct usage/usagePage when finding xkeys devices --- packages/webhid/src/methods.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/webhid/src/methods.ts b/packages/webhid/src/methods.ts index 4156db9..45818f8 100644 --- a/packages/webhid/src/methods.ts +++ b/packages/webhid/src/methods.ts @@ -3,25 +3,32 @@ import { WebHIDDevice } from './web-hid-wrapper' /** Prompts the user for which X-keys panel to select */ export async function requestXkeysPanels(): Promise { - return navigator.hid.requestDevice({ + const allDevices = await navigator.hid.requestDevice({ filters: [ { vendorId: XKEYS_VENDOR_ID, }, ], }) + return allDevices.filter(isValidXkeysUsage) } /** * Reopen previously selected devices. * The browser remembers what the user previously allowed your site to access, and this will open those without the request dialog */ export async function getOpenedXKeysPanels(): Promise { - return await navigator.hid.getDevices() + const allDevices = await navigator.hid.getDevices() + return allDevices.filter(isValidXkeysUsage) +} + +function isValidXkeysUsage(device: HIDDevice): boolean { + return device.vendorId === XKEYS_VENDOR_ID && !!device.collections.find((collection) => collection.usagePage === 12) } /** Sets up a connection to a HID device (the X-keys panel) */ export async function setupXkeysPanel(browserDevice: HIDDevice): Promise { if (!browserDevice?.collections?.length) throw Error(`device collections is empty`) + if (!isValidXkeysUsage(browserDevice)) throw new Error(`Device has incorrect usage/interface`) if (!browserDevice.productId) throw Error(`Device has no productId!`) const productId = browserDevice.productId @@ -43,7 +50,12 @@ export async function setupXkeysPanel(browserDevice: HIDDevice): Promise ) // Wait for the device to initialize: - await xkeys.init() + try { + await xkeys.init() - return xkeys + return xkeys + } catch (e) { + await deviceWrap.close() + throw new Error('Failed to initialise device') + } } From 8b9fdd1eb69abf03cfbc67f5b503bd01a8623bc5 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Wed, 1 Nov 2023 16:20:52 +0000 Subject: [PATCH 2/3] fix: filter for correct usage/usagePage when finding xkeys devices --- packages/webhid/src/methods.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/webhid/src/methods.ts b/packages/webhid/src/methods.ts index 45818f8..1cf211a 100644 --- a/packages/webhid/src/methods.ts +++ b/packages/webhid/src/methods.ts @@ -22,7 +22,14 @@ export async function getOpenedXKeysPanels(): Promise { } function isValidXkeysUsage(device: HIDDevice): boolean { - return device.vendorId === XKEYS_VENDOR_ID && !!device.collections.find((collection) => collection.usagePage === 12) + if (device.vendorId !== XKEYS_VENDOR_ID) return false + + return !!device.collections.find((collection) => { + if (collection.usagePage !== 12) return false + + // Check the write-length of the device is > 20 + return !!collection.outputReports?.find((report) => !!report.items?.find((item) => item.reportCount ?? 0 > 20)) + }) } /** Sets up a connection to a HID device (the X-keys panel) */ From 1147671a865035b8893a0e40e3088a7af4763ab4 Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Thu, 2 Nov 2023 09:20:47 +0100 Subject: [PATCH 3/3] chore: throw original error --- packages/webhid/src/methods.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webhid/src/methods.ts b/packages/webhid/src/methods.ts index 1cf211a..96b57df 100644 --- a/packages/webhid/src/methods.ts +++ b/packages/webhid/src/methods.ts @@ -63,6 +63,6 @@ export async function setupXkeysPanel(browserDevice: HIDDevice): Promise return xkeys } catch (e) { await deviceWrap.close() - throw new Error('Failed to initialise device') + throw e } }