From 8174cb004db056260c493fca92d0d575c212f6e4 Mon Sep 17 00:00:00 2001 From: snekxs <26660858+snekxs@users.noreply.github.com> Date: Fri, 4 Dec 2026 20:18:29 -0600 Subject: [PATCH] fix(wlmouse): support merged 512B LCD device, 0x02 status ACKs, and resilient dongle screen reads --- src/drivers/wlmouse/beast-x-v2-hid.test.ts | 62 ++++++++++++++++++++++ src/drivers/wlmouse/hid.ts | 28 +++++++--- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/drivers/wlmouse/beast-x-v2-hid.test.ts b/src/drivers/wlmouse/beast-x-v2-hid.test.ts index e12107c8..9084add1 100644 --- a/src/drivers/wlmouse/beast-x-v2-hid.test.ts +++ b/src/drivers/wlmouse/beast-x-v2-hid.test.ts @@ -626,3 +626,65 @@ test("review edge cases: 1.3mm LOD maps to Medium, UTF-8 names round-trip, over- await client.close(); }); + +test("merged 64B+512B HIDDevice (Desktop host), 0x02 status byte, and partial LCD read resilience", async () => { + stubHidDevices([]); + let lastReq = new Uint8Array(64); + const sentLengths: number[] = []; + const merged = { + vendorId: 0x36a7, + productId: WLMOUSE_BEAST_X_V2_WIRELESS_PID, + productName: "WLmouse Beast X V2 8K", + opened: false, + collections: [ + { + usagePage: 0xff00, + usage: 1, + featureReports: [{ reportId: 0, items: [{ reportSize: 8, reportCount: 64 }] }], + inputReports: [], + outputReports: [], + children: [], + }, + { + usagePage: 0xff01, + usage: 1, + featureReports: [{ reportId: 0, items: [{ reportSize: 8, reportCount: 512 }] }], + inputReports: [], + outputReports: [], + children: [], + }, + ], + open: async function () { this.opened = true; }, + close: async function () { this.opened = false; }, + sendFeatureReport: async (_reportId: number, data: Uint8Array) => { + lastReq = new Uint8Array(data); + sentLengths.push(lastReq.length); + }, + receiveFeatureReport: async () => { + const reply = new Uint8Array(lastReq.length); + const page = lastReq[4]!; + const cmd = lastReq[5]!; + reply[4] = page; + reply[5] = cmd; + if (page === 0x05 && cmd === 0x8c) { + // Simulate double-tap unsupported while other LCD getters answer with 0x02 OK + reply[0] = 0xa2; + } else { + reply[0] = 0x02; + reply[3] = 1; + reply[6] = cmd === 0x86 ? 1 : 2; // enabled=true, defaultScreen=time + } + return new DataView(reply.buffer); + }, + } as unknown as HIDDevice; + + assert.equal(WLMouseHidClient.isSupported(merged), true); + const client = new WLMouseHidClient(merged); + const screen = await client.readDongleScreen(); + assert.equal(screen.hasHighSpeedChannel, true); + assert.equal(screen.enabled, true); + assert.equal(screen.defaultScreen, "usb-monitor"); + assert.equal(screen.doubleTapEnabled, false); + assert.ok(sentLengths.every((len) => len === 512)); + await client.close(); +}); diff --git a/src/drivers/wlmouse/hid.ts b/src/drivers/wlmouse/hid.ts index 059b745d..4fd510c0 100644 --- a/src/drivers/wlmouse/hid.ts +++ b/src/drivers/wlmouse/hid.ts @@ -310,7 +310,7 @@ export class WLMouseHidClient { this.notifier = null; this.notifyListener = null; if (this.lcdDevice) { - if (this.lcdDevice.opened) await this.lcdDevice.close(); + if (this.lcdDevice !== this.device && this.lcdDevice.opened) await this.lcdDevice.close(); this.lcdDevice = null; } this.lcd512Probed = false; @@ -1057,13 +1057,16 @@ export class WLMouseHidClient { async readDongleScreen(): Promise { const has512 = await this.ensureLcd512Device(); - const stateReply = await this.lcdRequest(LCD_CMD.getLcdState, 1, []); - const screenReply = await this.lcdRequest(LCD_CMD.getDefaultScreen, 1, []); - const doubleTapReply = await this.lcdRequest(LCD_CMD.getDoubleTap, 3, [0x00, 0x01]); + const stateReply = await this.lcdRequest(LCD_CMD.getLcdState, 1, []).catch(() => null); + const screenReply = await this.lcdRequest(LCD_CMD.getDefaultScreen, 1, []).catch(() => null); + const doubleTapReply = await this.lcdRequest(LCD_CMD.getDoubleTap, 3, [0x00, 0x01]).catch(() => null); + if (!stateReply && !screenReply && !doubleTapReply) { + throw new Error("The dongle did not answer any LCD screen status commands."); + } const status: WLMouseDongleScreenStatus = { - enabled: (stateReply[0] ?? 0) !== 0, - defaultScreen: wlmouseDecodeDongleDefaultScreen(screenReply[0] ?? 1), - doubleTapEnabled: (doubleTapReply[0] ?? 0) === 1, + enabled: stateReply ? (stateReply[0] ?? 0) !== 0 : true, + defaultScreen: wlmouseDecodeDongleDefaultScreen(screenReply?.[0] ?? 1), + doubleTapEnabled: doubleTapReply ? (doubleTapReply[0] ?? 0) === 1 : false, width: WLMOUSE_BEAST_X_V2_SCREEN.width, height: WLMOUSE_BEAST_X_V2_SCREEN.height, hasHighSpeedChannel: has512, @@ -1395,6 +1398,13 @@ export class WLMouseHidClient { } if (this.lcd512Probed) return false; this.lcd512Probed = true; + // Native HID hosts (e.g. OpenMouse Desktop) merge all interfaces of a + // composite USB device into one HIDDevice whose collections declare both + // the 64-byte config report and the 512-byte LCD report. + if (WLMouseHidClient.hasFeatureReportSize(this.device, LCD_REPORT_LENGTH)) { + this.lcdDevice = this.device; + return true; + } const devices = await navigator.hid?.getDevices() ?? []; const siblings = devices.filter((candidate) => candidate !== this.device @@ -1463,7 +1473,9 @@ export class WLMouseHidClient { if (reply[offset] === STATUS.unsupported) { throw new Error(this.describe(page, command, "is not supported by this mouse")); } - if (reply[offset] !== STATUS.ok) continue; + // WLMouse firmware (including Beast X V2 dongle LCD replies) uses both + // 0xa1 (STATUS.ok) and 0x02 as successful response status bytes. + if (reply[offset] !== STATUS.ok && reply[offset] !== 0x02) continue; const start = HEADER_LENGTH + offset; const reportedLength = reply[3 + offset]; const sliceLength = reportLimit > PACKET_LENGTH && reportedLength === 0xff