Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8f7fc57
feat(razer): button remapping on the Viper Mini via class 0x02 under …
Dec 4, 2026
a1d2a95
fix(razer): retry a Viper Mini exchange answered by a stale reply
Dec 4, 2026
fad5972
feat(razer): keyboard-key button mappings, verified on the Viper Mini
Dec 4, 2026
694b176
fix(razer): treat a lost write ack on the Viper Mini as unconfirmed, …
Dec 4, 2026
1def63b
feat(razer): the Viper Mini's bottom button is a remappable control
Dec 4, 2026
2e0936d
feat(razer): middle click as a target and the wheel press as a contro…
Dec 4, 2026
880d151
fix(razer): review findings on the Viper Mini button mapping
onyxhq-dev Dec 4, 2026
fd5aed4
Merge remote-tracking branch 'origin/main' into pr-348
onyxhq-dev Dec 4, 2026
c439263
fix(razer): re-read the Viper Mini mappings after an unconfirmed write
onyxhq-dev Dec 4, 2026
383ba36
feat(razer): read and write the Viper Mini's Hypershift layer
Dec 4, 2026
3ca167f
fix(razer): probe the Viper Mini's controls one at a time so none go …
Dec 4, 2026
04b8468
fix(razer): keep reading a control's own reply instead of dropping it
Dec 4, 2026
3cc67f1
perf(razer): read the Viper Mini's keymap at the speed it actually an…
Dec 4, 2026
47313a2
fix(razer): fill missing controls a couple at a time instead of re-pr…
Dec 4, 2026
d3fea07
fix(razer): only one Viper Mini probe at a time
Dec 4, 2026
481d067
fix(razer): re-send instead of re-polling when the Viper Mini answers…
Dec 4, 2026
beb5335
fix(razer): name the bottom button's factory DPI action on the Viper …
Dec 4, 2026
f340806
fix(razer): call the wraparound DPI steps what they are
Dec 4, 2026
bb0f476
feat(razer): read the Viper Mini keymap in one table read
Dec 4, 2026
9dc87d1
fix(razer): anchor the keymap table where this driver's own request p…
Dec 4, 2026
01cde94
fix(razer): never present a Hypershift read as the layer's contents
Dec 4, 2026
e022228
Merge origin/main into feat/viper-mini-hypershift-layer
onyxhq-dev Dec 5, 2026
df4b791
fix(razer): a Hypershift write keeps the Standard mapping cache
onyxhq-dev Dec 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/drivers/mouse-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,16 @@ export interface MouseStatus {
* Hypershift because that write was verified on it.
*/
razerButtonOptions?: string[];
/**
* What this session has written to the Hypershift layer, keyed like
* `razerButtonMappings`. It is deliberately not a read: this firmware answers
* a class `0x02` read with whichever layer was written last, ignoring the
* layer argument, so a "read" of Hypershift returns the Standard value. Only
* a value written here is known.
*/
razerHypershiftMappings?: Record<string, string>;
/** True when the driver accepts writes to the Hypershift layer. */
razerHypershiftWritable?: boolean;
/** Keychron Nape Pro user layer currently running on the device (1–8).
* Matches firmware get/set. Undefined when the layer commands were not answered.
*/
Expand Down
72 changes: 72 additions & 0 deletions src/drivers/razer/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
RAZER_TOGGLE_CONTROLS,
RAZER_TOGGLE_CONTROL_INFO,
razerDecodeButtonMapping,
razerDecodeKeymapTable,
razerDecodeKeyboardMapping,
razerKeyLabel,
razerDecodeToggleControl,
Expand Down Expand Up @@ -975,3 +976,74 @@ test("key labels name what they can and hex what they cannot", () => {
test("Left Click cannot take a keyboard key either", () => {
assert.throws(() => razerSetKeyboardMappingCommand("leftClick", { mods: 0x00, usage: 0x68 }), RazerProtocolError);
});

test("the keymap table decodes a captured Viper Mini reply", () => {
// Captured after every slot was set to a distinct action, then re-read after
// only `back` changed — one byte moved, which is what fixed the record shape.
const bytes = Uint8Array.from([
0x01,0x04,0x00, 0x01,0x01,0x00,0x00,0x00,0x00,
0x02,0x01,0x02,0x02,0x00,0x00,0x00,0x00,
0x03,0x01,0x03,0x03,0x00,0x00,0x00,0x00,
0x04,0x01,0x04,0x04,0x00,0x00,0x00,0x00,
0x05,0x01,0x05,0x05,0x00,0x00,0x00,0x00,
0x60,0x0c,0x60,0x01,0x00,0x00,0x00,0x00,
0x09,0x01,0x09,0x09,0x00,0x00,0x00,0x00,
0x0a,0x01,0x0a,0x0a,0x00,0x00,0x00,0x00,
0xa2,
]);

const table = razerDecodeKeymapTable(bytes, 0x01, 0x00, 9);

assert.ok(table);
assert.equal(table.get(0x02), "Right Click");
assert.equal(table.get(0x03), "Middle Click");
assert.equal(table.get(0x04), "Mouse Button 4");
assert.equal(table.get(0x05), "Mouse Button 5");
assert.equal(table.get(0x60), "Hypershift");
});

test("the keymap table carries keyboard keys and refuses records it cannot place", () => {
const keys = Uint8Array.from([
0x01,0x04,0x00, 0x01,0x01,0x00,0x00,0x00,0x00,
0x02,0x02,0x02,0x00,0x68,0x00,0x00,0x00,
0x03,0x02,0x03,0x00,0x69,0x00,0x00,0x00,
0xc1,
]);
const table = razerDecodeKeymapTable(keys, 0x01, 0x00, 9);
assert.equal(table?.get(0x02), "F13");
assert.equal(table?.get(0x03), "F14");

// Controls that are not on this mouse mean this is not its keymap table.
const foreign = Uint8Array.from([
0x01,0x04,0x00, 0x01,0x01,0x00,0x00,0x00,0x00,
0x77,0x01,0x77,0x02,0x00,0x00,0x00,0x00,
0x78,0x01,0x78,0x02,0x00,0x00,0x00,0x00,
0x79,0x01,0x79,0x02,0x00,0x00,0x00,0x00,
0xc1,
]);
assert.equal(razerDecodeKeymapTable(foreign, 0x01, 0x00, 9), null);
});

test("the keymap table decodes the reply the panel actually receives", () => {
// Captured from the panel's own request: the preamble is eleven bytes here
// versus nine for a probe with different arguments, and the third field of
// each record is a constant rather than the control.
const bytes = Uint8Array.from([
0x01,0x00,0x00, 0x01,0x01,0x01,0x01, 0x00,0x00,0x00,0x00,
0x02,0x01,0x01,0x02,0x00,0x00,0x00,0x00,
0x03,0x01,0x01,0x03,0x00,0x00,0x00,0x00,
0x04,0x01,0x01,0x04,0x00,0x00,0x00,0x00,
0x05,0x01,0x01,0x05,0x00,0x00,0x00,0x00,
0x60,0x0c,0x01,0x01,0x00,0x00,0x00,0x00,
0x09,0x01,0x01,0x09,0x00,0x00,0x00,0x00,
0x0a,0x01,0x01,0x0a,0x00,0x00,0x00,0x00,
0x00,0x00,
]);

const table = razerDecodeKeymapTable(bytes, 0x01, 0x00);

assert.ok(table);
assert.equal(table.get(0x02), "Right Click");
assert.equal(table.get(0x04), "Mouse Button 4");
assert.equal(table.get(0x60), "Hypershift");
});
149 changes: 129 additions & 20 deletions src/drivers/razer/viper-mini-hid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,35 @@ function replyPacket(commandClass: number, commandId: number, dataSize: number,
* that acknowledges a write and keeps the old action — the case read-back
* confirmation exists to catch.
*/
function fakeViperMini(options: { ignoreButtonWrites?: boolean; staleReplies?: number; staleWriteAcks?: number; wrongControlReplies?: number } = {}) {
function fakeViperMini(options: {
ignoreButtonWrites?: boolean;
staleReplies?: number;
staleWriteAcks?: number;
wrongControlReplies?: number;
droppedReads?: number;
} = {}) {
const sent: Uint8Array[] = [];
let pending = new Uint8Array(RAZER_PACKET_LENGTH);
let staleLeft = options.staleReplies ?? 0;
let staleAcks = options.staleWriteAcks ?? 0;
let wrongControls = options.wrongControlReplies ?? 0;
const actions = new Map<number, number[]>([
[0x01, [0x01, 0x01, 0x01]],
[0x03, [0x01, 0x01, 0x03]],
[0x02, [0x01, 0x01, 0x02]],
[0x04, [0x01, 0x01, 0x04]],
[0x05, [0x01, 0x01, 0x05]],
// The real mouse's bottom button ships as F24, not as a mouse action.
[0x60, [0x02, 0x02, 0x00, 0x73]],
// A reply the mouse is still holding from an earlier exchange: it comes out
// of the buffer first, then the answer to the command just sent.
let stalePending: Uint8Array | null = null;
// Reads answered with "unsupported", the way a busy mouse drops a request.
let droppedReads = options.droppedReads ?? 0;
// Keyed by layer and control: the two layers store independent actions.
const actions = new Map<string, number[]>([
["0:1", [0x01, 0x01, 0x01]],
["0:3", [0x01, 0x01, 0x03]],
["0:2", [0x01, 0x01, 0x02]],
["0:4", [0x01, 0x01, 0x04]],
["0:5", [0x01, 0x01, 0x05]],
// A factory bottom button steps the sensitivity, not a mouse action.
["0:96", [0x06, 0x01, 0x06]],
// Hypershift layer starts empty on a factory mouse.
["1:4", [0x00, 0x00, 0x00]],
["1:5", [0x00, 0x00, 0x00]],
]);
const device = {
vendorId: 0x1532,
Expand Down Expand Up @@ -165,17 +180,18 @@ function fakeViperMini(options: { ignoreButtonWrites?: boolean; staleReplies?: n
return;
}
const control = data[9];
const slot = `${data[10]}:${data[9]}`;
const read = commandClass === 0x02 && commandId === 0x8c;
const write = commandClass === 0x02 && commandId === 0x0c;
if (read || write) {
if (write && !options.ignoreButtonWrites) {
// A mouse action is three bytes, a keyboard key is four (type, len,
// mods, usage) — the reply echoes whichever the write carried.
actions.set(control, data[11] === 0x02
actions.set(slot, data[11] === 0x02
? [data[11], data[12], data[13], data[14]]
: [data[11], data[12], data[13]]);
}
const action = actions.get(control) ?? [0x00, 0x00, 0x00];
const action = actions.get(slot) ?? [0x00, 0x00, 0x00];
// A write the mouse applied but acknowledged with an older reply, which
// is what the reporter's log showed as "answered by a different
// command" on a write that had in fact stored.
Expand All @@ -184,17 +200,25 @@ function fakeViperMini(options: { ignoreButtonWrites?: boolean; staleReplies?: n
pending = replyPacket(0x05, 0x81, 0x02, [0x01, 0x01], RAZER_STATUS.ok);
return;
}
if (read && droppedReads > 0) {
droppedReads -= 1;
pending = replyPacket(commandClass, commandId, data[5], [], RAZER_STATUS.unsupported);
return;
}
if (read && wrongControls > 0) {
wrongControls -= 1;
pending = replyPacket(commandClass, commandId, data[5], [data[8], 0x05, data[10], ...actions.get(0x05)!], RAZER_STATUS.ok);
return;
stalePending = replyPacket(commandClass, commandId, data[5], [data[8], 0x05, data[10], ...(actions.get(`${data[10]}:5`) ?? [0x00, 0x00, 0x00])], RAZER_STATUS.ok);
}
pending = replyPacket(commandClass, commandId, data[5], [data[8], control, data[10], ...action], RAZER_STATUS.ok);
return;
}
pending = replyPacket(commandClass, commandId, data[5], [], RAZER_STATUS.unsupported);
},
receiveFeatureReport: async () => new DataView(pending.buffer.slice(0)),
receiveFeatureReport: async () => {
const out = stalePending ?? pending;
stalePending = null;
return new DataView(out.buffer.slice(0));
},
} as unknown as HIDDevice;
return { client: new RazerViperMiniHidClient(device), sent, actions };
}
Expand Down Expand Up @@ -223,7 +247,7 @@ test("Viper Mini reports the four remappable controls in its status", async () =
rightClick: "Right Click",
mouse4: "Mouse Button 4",
mouse5: "Mouse Button 5",
dpiButton: "F24",
dpiButton: "DPI Up Wrap",
});
assert.ok(status.razerButtonOptions?.includes("Hypershift"));
assert.ok(status.razerButtonOptions?.includes("Middle Click"));
Expand Down Expand Up @@ -256,7 +280,7 @@ test("Viper Mini writes a mapping on 0x1f and confirms it by read-back", async (
assert.equal(await client.setButtonMapping("mouse4", "Right Click"), "Right Click");
assert.deepEqual([...sent.at(-2)!.slice(8, 18)], [0x01, 0x04, 0x00, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00]);
assert.equal(sent.at(-2)![1], 0x1f);
assert.deepEqual(actions.get(0x04), [0x01, 0x01, 0x02]);
assert.deepEqual(actions.get("0:4"), [0x01, 0x01, 0x02]);
assert.equal((await client.readStatus()).razerButtonMappings?.mouse4, "Right Click");
});

Expand Down Expand Up @@ -304,7 +328,7 @@ test("Viper Mini reports a key already stored on the mouse instead of dropping t
// as a mouse action returns null, so without the keyboard read the row would
// vanish from the card.
const { client, actions } = fakeViperMini();
actions.set(0x05, [0x02, 0x02, 0x01, 0x68]);
actions.set("0:5", [0x02, 0x02, 0x01, 0x68]);

const status = await client.readStatus();

Expand All @@ -330,7 +354,7 @@ const ALL_DEFAULT_MAPPINGS = {
rightClick: "Right Click",
mouse4: "Mouse Button 4",
mouse5: "Mouse Button 5",
dpiButton: "F24",
dpiButton: "DPI Up Wrap",
};

test("Viper Mini ignores a mapping reply that echoes another control", async () => {
Expand All @@ -350,7 +374,7 @@ test("Viper Mini reads the mappings again after it is closed", async () => {
const { client, actions } = fakeViperMini();

await client.readStatus();
actions.set(0x04, [0x01, 0x01, 0x02]);
actions.set("0:4", [0x01, 0x01, 0x02]);
await client.close();
assert.equal((await client.readStatus()).razerButtonMappings?.mouse4, "Right Click");
});
Expand All @@ -369,6 +393,91 @@ test("Viper Mini reads the mappings again when a write's read-back is refused",

await client.readStatus();
await assert.rejects(() => client.setButtonMapping("mouse4", "Right Click"));
actions.set(0x04, [0x01, 0x01, 0x02]);
actions.set("0:4", [0x01, 0x01, 0x02]);
assert.equal((await client.readStatus()).razerButtonMappings?.mouse4, "Right Click");
});

test("Viper Mini writes and reads the Hypershift layer separately", async () => {
// The layer byte is the third argument: 0 Standard, 1 Hypershift. A binding
// on Hypershift only does something if that layer holds its own action.
const { client, sent } = fakeViperMini();

assert.equal(await client.setButtonMapping("mouse4", "Middle Click", 1), "Middle Click");
assert.deepEqual([...sent.at(-2)!.slice(8, 18)], [0x01, 0x04, 0x01, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00]);
assert.equal((await client.readStatus()).razerHypershiftMappings?.mouse4, "Middle Click");
});

test("Viper Mini reports only what it wrote to the Hypershift layer", async () => {
// The class `0x02` read ignores the layer argument and answers with whichever
// layer was written last, so a read must never be presented as the Hypershift
// layer's contents.
const { client } = fakeViperMini();

const untouched = await client.readStatus();
assert.equal(untouched.razerHypershiftMappings, undefined);
assert.equal(untouched.razerHypershiftWritable, true);

await client.setButtonMapping("mouse4", "Middle Click", 1);
const written = await client.readStatus();
assert.equal(written.razerHypershiftMappings?.mouse4, "Middle Click");
assert.equal(written.razerHypershiftMappings?.mouse5, undefined);
assert.equal(written.razerButtonMappings?.mouse4, "Mouse Button 4");
});

test("Viper Mini keeps the Standard cache through a Hypershift write", async () => {
const { client, sent } = fakeViperMini();

await client.readStatus();
await client.setButtonMapping("mouse4", "Middle Click", 1);
const before = sent.length;
const status = await client.readStatus();

const keymapReads = sent.slice(before).filter((packet) => packet[6] === 0x02);
assert.equal(keymapReads.length, 0);
assert.equal(status.razerButtonMappings?.mouse4, "Mouse Button 4");
assert.equal(status.razerHypershiftMappings?.mouse4, "Middle Click");
});

test("Viper Mini re-probes the controls a read missed", async () => {
// The card showed two rows on hardware because three reads were dropped and
// the short map was cached. An incomplete probe must not be cached.
const { client } = fakeViperMini({ droppedReads: 3 });

const first = await client.readStatus();
assert.ok(Object.keys(first.razerButtonMappings ?? {}).length < 6);

const second = await client.readStatus();
assert.deepEqual(second.razerButtonMappings, ALL_DEFAULT_MAPPINGS);
});

test("Viper Mini reports every control when status is read twice at once", async () => {
// Two status reads used to interleave their probes and answer each other's
// reads, leaving one layer with the first few controls and the other with the
// rest. Only one probe may run at a time.
const { client } = fakeViperMini();

const [first, second] = await Promise.all([client.readStatus(), client.readStatus()]);

assert.deepEqual(first.razerButtonMappings, ALL_DEFAULT_MAPPINGS);
assert.deepEqual(second.razerButtonMappings, ALL_DEFAULT_MAPPINGS);
});

test("Viper Mini names the bottom button's factory DPI action", async () => {
// A mouse nobody has remapped reads its bottom button as a DPI step, which is
// a type the mouse-action decoder reports as unknown. Without this the row
// would vanish on exactly the mice that have never been touched.
const { client } = fakeViperMini();

const status = await client.readStatus();

assert.equal(status.razerButtonMappings?.dpiButton, "DPI Up Wrap");
});

test("Viper Mini still reports a keyboard key left on the bottom button", async () => {
const { client, actions } = fakeViperMini();
actions.set("0:96", [0x02, 0x02, 0x00, 0x73]);

const status = await client.readStatus();

assert.equal(status.razerButtonMappings?.dpiButton, "F24");
});
Loading