Skip to content
Open
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
32 changes: 25 additions & 7 deletions src/device/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
retrySharedLightspeedDevice,
SharedLightspeedDevice,
} from "./shared-lightspeed.ts";
import { closestDpiOption, dpiOptionsForStage } from "./dpi-presets";
import { closestDpiOption, dpiOptionsForStage, storedDpiStages } from "./dpi-presets";
import {
RIVAL650_FACTORY_MAPPING,
editRival650ButtonDraft,
Expand Down Expand Up @@ -1698,7 +1698,7 @@ export function clearXiaomiMacro(button: number): void {
});
}

function readCapabilities(): DeviceCapabilities {
function readCapabilities(status?: MouseStatus): DeviceCapabilities {
const razer = activeAs<RazerHidClient>(CM()?.RazerHidClient);
const dm = dmClient();
const keychron = keychronNapeClient();
Expand All @@ -1716,7 +1716,8 @@ function readCapabilities(): DeviceCapabilities {
// numeric `setDpiStageValue` mirrors X into Y everywhere it appears. Lamzu
// Luna spells it `setLunaFuryDpiStageAxes`, the SteelSeries Gen 2 families
// take the generic name.
dpiStageAxesWritable: dpiStageAxesWriterName() !== null,
dpiStageAxesWritable: dpiStageAxesWriterName() !== null
&& (status?.ui?.family !== "mchose" || status.supportsSeparateDpiAxes === true),
dpiStageColorWritable: clientHasMethod("setDpiStageColor"),
// Any client may publish these; the two named drivers are just the ones
// that predate the generic lookup below.
Expand Down Expand Up @@ -4419,7 +4420,7 @@ async function activateClientNow(
// Optional setters/getters are runtime capabilities of the selected
// client. Populate them before rendering so controls such as RAWM angle
// tuning are editable on the first status snapshot.
capabilities = readCapabilities();
capabilities = readCapabilities(status);
applyStatus(status);
if (isNoirProfileDevice(status) && m2nexProfiles === null) seedM2NexProfiles(status);
if (!isNoirProfileDevice(status)) {
Expand Down Expand Up @@ -5132,9 +5133,16 @@ export function applyDpiStageCount(count: number): void {
progress: `Setting ${count} DPI stages…`,
preview: (status) => {
const current = status.dpiStages?.slice() ?? [];
const stored = storedDpiStages(status);
status.dpiStages = count <= current.length
? current.slice(0, count)
: [...current, ...Array.from({ length: count - current.length }, () => current.at(-1) ?? status.dpi)];
: [...current, ...Array.from({ length: count - current.length }, (_, index) => stored.x?.[current.length + index] ?? current.at(-1) ?? status.dpi)];
if (status.dpiStagesY) {
const currentY = status.dpiStagesY;
status.dpiStagesY = count <= currentY.length
? currentY.slice(0, count)
: [...currentY, ...Array.from({ length: count - currentY.length }, (_, index) => stored.y?.[currentY.length + index] ?? currentY.at(-1) ?? status.dpiY ?? status.dpi)];
}
if (status.dpiStageColors) {
status.dpiStageColors = count <= status.dpiStageColors.length
? status.dpiStageColors.slice(0, count)
Expand All @@ -5146,6 +5154,7 @@ export function applyDpiStageCount(count: number): void {
if ((status.activeDpiStage ?? 0) >= count) {
status.activeDpiStage = count - 1;
status.dpi = status.dpiStages[count - 1] ?? status.dpi;
if (status.dpiStagesY) status.dpiY = status.dpiStagesY[count - 1] ?? status.dpiY;
}
},
apply: async () => {
Expand Down Expand Up @@ -5198,7 +5207,8 @@ export function applyDpiStageValue(stage: number, rawDpi: number): void {
if (!hasActiveClient()) return;
// A client that can address the axes separately must not take the numeric
// route: that one mirrors X into Y and would discard the stage's Y.
if (dpiStageAxesWriterName() !== null) {
if (dpiStageAxesWriterName() !== null
&& (latestDeviceStatus?.ui?.family !== "mchose" || latestDeviceStatus.supportsSeparateDpiAxes === true)) {
applyDpiStageAxes(stage, { x: rawDpi });
return;
}
Expand Down Expand Up @@ -8476,7 +8486,15 @@ async function refreshStatus(): Promise<void> {
const currentClient = activeSettingsClient();
if (client !== currentClient || client.device !== activeDevice) return;
const key = JSON.stringify(status);
if (key !== lastRenderedStatusKey) applyStatus(status, key);
if (key !== lastRenderedStatusKey) {
if (isA(client, CM()?.MchoseHidClient)
&& (status.name !== latestDeviceStatus?.name
|| status.supportsSeparateDpiAxes !== latestDeviceStatus?.supportsSeparateDpiAxes)) {
capabilities = readCapabilities(status);
dpiOptions = client.getDpiOptions();
}
applyStatus(status, key);
}
} catch (error) {
lastRenderedStatusKey = null;
deviceStatusText = st("ctl.waitingRefresh");
Expand Down
8 changes: 8 additions & 0 deletions src/device/dpi-presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export function dpiStageEditor(status: Pick<MouseStatus, "ui"> | null | undefine
return status?.ui?.dpiStageEditor;
}

// Protocol packages without L7 Ultra support omit the stored tables.
export function storedDpiStages(status: (MouseStatus & {
dpiStoredStages?: readonly number[];
dpiStoredStagesY?: readonly number[];
}) | null | undefined): { x?: readonly number[]; y?: readonly number[] } {
return { x: status?.dpiStoredStages, y: status?.dpiStoredStagesY };
}

export function closestDpiOption(options: readonly number[], target: number): number | null {
let closest: number | null = null;
for (const option of options) {
Expand Down
29 changes: 29 additions & 0 deletions src/device/dpi-stage-writable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ function harness() {
const mocks = new Map();
const source = readFileSync(new URL("./controller.ts", import.meta.url), "utf8").replaceAll("import.meta.env", "({ DEV: false })") + `
export function testSelect(client, status, options) { clearActiveClients(); clearPendingChanges(); active = client; activeDevice = client.device; latestDeviceStatus = status ?? { name: "Fixed stages", dpi: 800, dpiStages: [400, 800, 1600], pollingRateHz: 1000, ui: { dpiStageEditor: { maxStages: 3 } } }; dpiOptions = options ?? [400, 800, 1200, 1600, 3200]; }
export function testAxisCapability(status) { return readCapabilities(status).dpiStageAxesWritable; }
`;
const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText;
const localStorage = { getItem: () => null };
const context = { exports, structuredClone, __BUILD_CHANNEL__: "stable", __BUILD_ID__: "test", __APP_VERSION__: "test", localStorage, window: { localStorage, setTimeout: () => 1, clearTimeout() {} }, setTimeout, clearTimeout, console, require: (name: string) => {
if (name.endsWith("pending-changes")) return pending;
if (name.endsWith("game-profile-snapshot")) return snapshots;
if (name.endsWith("dpi-presets")) return dpiPresets;
if (name.endsWith("driver-classes")) return { driverModules: () => new Proxy({}, { get: () => function () {} }), ensureDriverModules: async () => ({}) };
if (name.endsWith("interface-preferences")) return { loadInterfacePreferences: () => ({ locale: "en", reducedMotion: false }) };
if (name.endsWith("i18n/index")) return { t: (_locale: string, key: string) => key, tp: (_locale: string, key: string) => key };
if (!mocks.has(name)) mocks.set(name, new Proxy({}, { get: (target: any, key) => target[key] ??= function () {} }));
Expand Down Expand Up @@ -62,3 +64,30 @@ test("DPI values use the range of the pending active stage", () => {
assert.equal(control.applyDpiValue(20000), true);
assert.deepEqual(pending.pendingChanges().map((change) => change.key), ["dpi-active-stage", "dpi"]);
});

test("enabling a DPI stage previews its stored X/Y values", () => {
const control = harness();
const status = {
name: "L7 Ultra", dpi: 4000, dpiY: 4000, dpiStages: [4000], dpiStagesY: [4000],
dpiStoredStages: [4000, 40000, 2000, 3200, 6400, 42000],
dpiStoredStagesY: [4000, 24000, 2000, 3200, 6400, 42000],
activeDpiStage: 0, pollingRateHz: 4000,
ui: { dpiStageEditor: { maxStages: 6, countEditable: true } },
};
control.testSelect({ device: {}, async setDpiStageCount() {} }, status);
control.applyDpiStageCount(2);
const preview = pending.withPendingChanges(status as any);
assert.deepEqual(Array.from(preview.dpiStages ?? []), [4000, 40000]);
assert.deepEqual(Array.from(preview.dpiStagesY ?? []), [4000, 24000]);
});

test("axis editing is exposed only when the device reports that capability", () => {
const control = harness();
control.testSelect({ device: {}, async setDpiStageAxes() {} });
assert.equal(control.testAxisCapability({ ui: { family: "mchose" }, supportsSeparateDpiAxes: false }), false);
assert.equal(control.testAxisCapability({ ui: { family: "mchose" }, supportsSeparateDpiAxes: true }), true);
assert.equal(control.testAxisCapability({ ui: { family: "mchose" }, supportsSeparateDpiAxes: true, dpiStages: undefined }), true,
"a temporary config read failure does not erase the model capability");
assert.equal(control.testAxisCapability({ supportsSeparateDpiAxes: false }), true,
"other manufacturers retain the existing method-based capability");
});
26 changes: 20 additions & 6 deletions src/features/devices/cards/DpiCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import * as control from "../../../device/controller";
import type { ControlSnapshot, LiftOffLevel } from "../../../device/types";
import { isNoirE1Status, isNoirKsnakeStatus, isNoirS1Status, NOIR_DPI_STAGE_COLORS } from "../../../device/noir.ts";
import { closestDpiOption, dpiOptionsForStage, dpiPresetValues, dpiStageEditor } from "../../../device/dpi-presets";
import { closestDpiOption, dpiOptionsForStage, dpiPresetValues, dpiStageEditor, storedDpiStages } from "../../../device/dpi-presets";
import { t, tp } from "../../../i18n/index";
import { LiftOffDistance, hasLiftOff } from "./PerformanceCards";

Expand Down Expand Up @@ -42,6 +42,7 @@ function DpiStageEditor({
editorView?: "stage" | "single";
}): ReactNode {
const status = snapshot.status;
const stored = storedDpiStages(status);
const locale = snapshot.preferences.locale;
const options = dpiOptionsForStage(snapshot.dpiOptions, status);

Expand All @@ -63,9 +64,10 @@ function DpiStageEditor({
const stageAxesEditable = mode === "stage"
&& snapshot.capabilities?.dpiStageAxesWritable === true
&& Array.isArray(status?.dpiStagesY);
const stageAxesLinkable = stageAxesEditable && status?.ui?.family === "mchose";
const slotAxesEditable = mode === "logitech" && status?.supportsSeparateDpiAxes === true;
const hostAxesEditable = mode === "generic" && snapshot.traits.logitech && status?.supportsSeparateDpiAxes === true;
const axesLinkable = slotAxesEditable || hostAxesEditable;
const axesLinkable = stageAxesLinkable || slotAxesEditable || hostAxesEditable;
const showAxes = stageAxesEditable || slotAxesEditable || hostAxesEditable;
const genericRows = hostAxesEditable ? MAX_HOST_ROWS : MAX_EDITOR_ROWS;
const stageColors = mode === "stage"
Expand Down Expand Up @@ -102,8 +104,14 @@ function DpiStageEditor({
value: String(value),
valueY: String(stagesY?.[index] ?? value),
lod: DEFAULT_LOD,
linked: (stagesY?.[index] ?? value) === value,
}));
while (rows.length < rowCap) rows.push({ enabled: false, value: String(last), valueY: String(last), lod: DEFAULT_LOD });
while (rows.length < rowCap) {
const index = rows.length;
const x = stored.x?.[index] ?? last;
const y = stored.y?.[index] ?? x;
rows.push({ enabled: false, value: String(x), valueY: String(y), lod: DEFAULT_LOD, linked: x === y });
}
return rows;
}
const savedSlots = hostAxesEditable ? control.loadHostDpiSlots() : null;
Expand Down Expand Up @@ -289,16 +297,22 @@ function DpiStageEditor({
enabled.forEach((row, position) => {
const value = parseRow(row.value);
const valueY = parseRow(row.valueY);
const storedX = current[position] ?? stored.x?.[position];
const storedY = currentY[position] ?? stored.y?.[position];
if (stageAxesEditable) {
if (stageAxesLinkable && row.linked !== false) {
if (value !== null && (storedX !== value || storedY !== value)) control.applyDpiStageAxes(position, { x: value, y: value });
return;
}
// Send only the axes the user actually moved, so the untouched one
// stays whatever the mouse holds.
const edit: { x?: number; y?: number } = {};
if (value !== null && current[position] !== value) edit.x = value;
if (valueY !== null && currentY[position] !== valueY) edit.y = valueY;
if (value !== null && storedX !== value) edit.x = value;
if (valueY !== null && storedY !== valueY) edit.y = valueY;
if (edit.x !== undefined || edit.y !== undefined) control.applyDpiStageAxes(position, edit);
return;
}
if (value === null || current[position] === value) return;
if (value === null || storedX === value) return;
control.applyDpiStageValue(position, value);
});
return;
Expand Down