diff --git a/src/device/controller.ts b/src/device/controller.ts index 688ea24c..c7c45a9c 100644 --- a/src/device/controller.ts +++ b/src/device/controller.ts @@ -7156,7 +7156,9 @@ export function applyLowPowerThreshold(percent: number): void { * Buttons card rendered from `razerButtonMappings` while every dropdown in it * staged nothing — the row looked editable and did nothing. */ -type ButtonMappingClient = Pick; +type ButtonMappingClient = { + setButtonMapping(control: RazerButtonControl, mapping: RazerButtonMapping, layer?: number): Promise; +}; type ToggleControlClient = Pick; function razerClientWith(method: string): Record | null { @@ -7176,7 +7178,7 @@ function razerToggleClient(): ToggleControlClient | null { /** The Viper Mini's client carries the keyboard-mapping write; others do not. */ type KeyboardMappingClient = { - setKeyboardMapping(control: RazerButtonControl, key: RazerKeyAction): Promise; + setKeyboardMapping(control: RazerButtonControl, key: RazerKeyAction, layer?: number): Promise; }; function razerKeyClient(): KeyboardMappingClient | null { @@ -7188,20 +7190,27 @@ export function supportsRazerKeyMapping(): boolean { return razerKeyClient() !== null; } -export function applyRazerButtonMapping(control: RazerButtonControl, mapping: RazerButtonMapping): void { +export function applyRazerButtonMapping( + control: RazerButtonControl, + mapping: RazerButtonMapping, + layer: number = 0, +): void { if (!razerButtonClient()) return; + const base = C().RAZER_BUTTON_CONTROL_LABEL[control]; + const name = layer === 1 ? `${base} (Hypershift)` : base; stageChange({ - key: `razer-button-${control}`, - label: `${C().RAZER_BUTTON_CONTROL_LABEL[control]} → ${mapping}`, - command: `Change ${C().RAZER_BUTTON_CONTROL_LABEL[control]} mapping`, - progress: `Changing ${C().RAZER_BUTTON_CONTROL_LABEL[control]} mapping…`, + key: `razer-button-${control}${layer === 1 ? "-hypershift" : ""}`, + label: `${name} → ${mapping}`, + command: `Change ${name} mapping`, + progress: `Changing ${name} mapping…`, preview: (status) => { - if (status.razerButtonMappings) status.razerButtonMappings[control] = mapping; + if (layer === 1) status.razerHypershiftMappings = { ...status.razerHypershiftMappings, [control]: mapping }; + else if (status.razerButtonMappings) status.razerButtonMappings[control] = mapping; }, apply: async () => { const client = razerButtonClient(); if (!client) throw new Error(st("ctl.gone")); - await client.setButtonMapping(control, mapping); + await client.setButtonMapping(control, mapping, layer); }, }); } @@ -7211,22 +7220,28 @@ export function applyRazerButtonMapping(control: RazerButtonControl, mapping: Ra * been checked for the encoding get here: the Viper Mini stores * `02 02 ` and physically sends it, confirmed with `wev`. */ -export function applyRazerKeyMapping(control: RazerButtonControl, key: RazerKeyAction): void { +export function applyRazerKeyMapping( + control: RazerButtonControl, + key: RazerKeyAction, + layer: number = 0, +): void { if (!razerKeyClient()) return; - const name = C().RAZER_BUTTON_CONTROL_LABEL[control]; + const base = C().RAZER_BUTTON_CONTROL_LABEL[control]; + const name = layer === 1 ? `${base} (Hypershift)` : base; const label = C().razerKeyLabel(key); stageChange({ - key: `razer-button-${control}`, + key: `razer-button-${control}${layer === 1 ? "-hypershift" : ""}`, label: `${name} → ${label}`, command: `Change ${name} mapping`, progress: `Changing ${name} mapping…`, preview: (status) => { - if (status.razerButtonMappings) status.razerButtonMappings[control] = label; + if (layer === 1) status.razerHypershiftMappings = { ...status.razerHypershiftMappings, [control]: label }; + else if (status.razerButtonMappings) status.razerButtonMappings[control] = label; }, apply: async () => { const client = razerKeyClient(); if (!client) throw new Error(st("ctl.gone")); - await client.setKeyboardMapping(control, key); + await client.setKeyboardMapping(control, key, layer); }, }); } diff --git a/src/features/devices/cards/AdvancedCards.tsx b/src/features/devices/cards/AdvancedCards.tsx index f4a2a5cf..b8fc9a3f 100644 --- a/src/features/devices/cards/AdvancedCards.tsx +++ b/src/features/devices/cards/AdvancedCards.tsx @@ -1616,13 +1616,22 @@ const RAZER_KEY_OPTION = "__razer_key__"; export function RazerButtonCard({ snapshot }: { snapshot: ControlSnapshot }): ReactNode { const locale = snapshot.preferences.locale; - const mappings = snapshot.status?.razerButtonMappings; + const standard = snapshot.status?.razerButtonMappings; + // Written values only: this firmware answers a class `0x02` read with + // whichever layer was written last, so a "read" of Hypershift is the Standard + // value and must not be shown as if it were the layer's contents. + const hypershift = snapshot.status?.razerHypershiftMappings; + const layerWritable = snapshot.status?.razerHypershiftWritable === true; // Only the models whose firmware was checked for the key encoding offer it — // the Viper Mini stores `02 02 ` where the older clients have // no keyboard write at all. const keyCapable = control.supportsRazerKeyMapping(); - const [capturing, setCapturing] = useState(null); - useEffect(() => setCapturing(null), [snapshot.activeDeviceKey]); + const [layer, setLayer] = useState(0); + const [capturing, setCapturing] = useState<{ control: RazerButtonControl; layer: number } | null>(null); + useEffect(() => { + setCapturing(null); + setLayer(0); + }, [snapshot.activeDeviceKey]); useEffect(() => { if (!capturing) return undefined; const onKeyDown = (event: KeyboardEvent): void => { @@ -1641,13 +1650,14 @@ export function RazerButtonCard({ snapshot }: { snapshot: ControlSnapshot }): Re | (event.shiftKey ? RAZER_KEY_MODIFIERS.shift ?? 0 : 0) | (event.altKey ? RAZER_KEY_MODIFIERS.alt ?? 0 : 0) | (event.metaKey ? RAZER_KEY_MODIFIERS.meta ?? 0 : 0); - control.applyRazerKeyMapping(capturing, { mods, usage }); + control.applyRazerKeyMapping(capturing.control, { mods, usage }, capturing.layer); setCapturing(null); }; window.addEventListener("keydown", onKeyDown, true); return () => window.removeEventListener("keydown", onKeyDown, true); }, [capturing]); - if (!mappings) return null; + if (!standard) return null; + const mappings = layer === 1 ? (hypershift ?? {}) : standard; const busy = snapshot.settingInProgress; const rows: Array<{ key: string; name: string; current: string; options: readonly string[]; locked: boolean }> = []; // The wheel press and the bottom button are model-specific: only a model @@ -1656,18 +1666,21 @@ export function RazerButtonCard({ snapshot }: { snapshot: ControlSnapshot }): Re const options = (snapshot.status?.razerButtonOptions ?? RAZER_BUTTON_MAPPINGS) as readonly string[]; for (const con of order) { const current = mappings[con]; - if (!current) continue; + // On Hypershift a control nobody has written to yet is unknown, not absent: + // keep the row so it can be set, and show it as unread. + if (!current && layer === 0) continue; + if (layer === 1 && con === RAZER_LOCKED_BUTTON_CONTROL) continue; rows.push({ key: `razer-button-${con}`, name: RAZER_BUTTON_CONTROL_LABEL[con], - current, + current: current ?? "", options, // Left Click is fixed on the Standard layer — Synapse enforces the same // restriction, and the driver throws rather than send it. locked: con === RAZER_LOCKED_BUTTON_CONTROL, }); } - for (const con of RAZER_TOGGLE_CONTROLS) { + for (const con of layer === 1 ? [] : RAZER_TOGGLE_CONTROLS) { const current = mappings[con]; if (!current) continue; const info = RAZER_TOGGLE_CONTROL_INFO[con]; @@ -1693,22 +1706,35 @@ export function RazerButtonCard({ snapshot }: { snapshot: ControlSnapshot }): Re

{t(locale, "adv.mapping")}

+ {layerWritable ? ( + + ) : null}
{rows.map((row) => { const staged = snapshot.pending.keys.includes(row.key); - const known = row.options.includes(row.current); + const known = row.current !== "" && row.options.includes(row.current); const rowControl = row.key.startsWith("razer-button-") ? row.key.slice("razer-button-".length) as RazerButtonControl : null; const applyRazerRow = (value: string): void => { if (value === RAZER_KEY_OPTION && rowControl) { - setCapturing(rowControl); + setCapturing({ control: rowControl, layer }); return; } if (row.key.startsWith("razer-button-")) { control.applyRazerButtonMapping( row.key.slice("razer-button-".length) as RazerButtonControl, value as RazerButtonMapping, + layer, ); } else { control.applyRazerToggleControl( @@ -1724,7 +1750,7 @@ export function RazerButtonCard({ snapshot }: { snapshot: ControlSnapshot }): Re data-pending-key={row.key} > {displayText(locale, row.name)} - {row.locked ? {displayText(locale, row.current)} : capturing !== null && capturing === rowControl ? ( + {row.locked ? {displayText(locale, row.current)} : capturing !== null && capturing?.control === rowControl ? ( {t(locale, "adv.pressKey")}