Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/protocol/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
serialize as kcSerialize,
setCustomKeycodes,
setKeycodeVersion,
setMacroCount,
setTapDanceCount,
type CustomKeycode,
} from "./keycodes.ts";
Expand Down Expand Up @@ -908,6 +909,7 @@ export class Keyboard {
private async reloadMacros(): Promise<void> {
const countData = await this.transport.send(new Uint8Array([C.CMD_VIA_MACRO_GET_COUNT]), 20);
this.macroCount = countData[1];
setMacroCount(this.macroCount);

const sizeData = await this.transport.send(new Uint8Array([C.CMD_VIA_MACRO_GET_BUFFER_SIZE]), 20);
this.macroMemory = new DataView(sizeData.buffer, sizeData.byteOffset, sizeData.byteLength).getUint16(1, false);
Expand Down
30 changes: 21 additions & 9 deletions src/protocol/keycodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1072,15 +1072,14 @@ export const KEYCODES_QUANTUM: KeycodeDef[] = [
]),
];

function macroKeycodes(): KeycodeDef[] {
const defs: KeycodeDef[] = [];
for (let x = 0; x < 16; x++) {
defs.push({ qmkId: `M${x}`, label: `M${x}` });
}
return defs;
}

export const KEYCODES_MACRO: KeycodeDef[] = macroKeycodes();
/**
* The connected device's macro slots, M0..M(macroCount-1) — populated by
* `setMacroCount` once `Keyboard.reload()` knows the device's macro count
* (see `reloadMacros`), same live-binding pattern as `setTapDanceCount`
* below. Empty (and the "Macros" category empty) until then, rather than a
* fixed 16 that would drift from whatever the device actually reports.
*/
export const KEYCODES_MACRO: KeycodeDef[] = [];

export const KEYCODES_LIGHTING: KeycodeDef[] = table([
["BL_TOGG", "BL\nToggle"],
Expand Down Expand Up @@ -1708,6 +1707,19 @@ export function setTapDanceCount(count: number): void {
}
}

/**
* Registers how many macro slots the device exposes, as M0..M(count-1).
* Mutates {@link KEYCODES_MACRO} in place (rather than reassigning it) so the
* "Macros" entry already captured into `KEYCODE_CATEGORIES` at module load
* keeps seeing the live list.
*/
export function setMacroCount(count: number): void {
KEYCODES_MACRO.length = 0;
for (let x = 0; x < count; x++) {
KEYCODES_MACRO.push({ qmkId: `M${x}`, label: `M${x}` });
}
}

/** The connected device's custom keycodes, for building a picker tab. */
export function customKeycodeDefs(): readonly KeycodeDef[] {
return customKeycodeDefsCache;
Expand Down
Loading