Repository navigation
feat(xiaomi): add Mi Gaming Mouse support #1527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,8 @@ import { ensureDriverModules, driverModules, type DriverModules } from "./driver | |
| import type { MagneticButtonStatus, MouseLighting, MouseStatus } from "@openmouse/protocol/drivers/mouse-types"; | ||
| import type { KsnakeMacroProfile } from "@openmouse/protocol/ksnake"; | ||
| import type { WallhackCurvePoint, WallhackMacroStep } from "@openmouse/protocol/wallhack"; | ||
| import type { XiaomiBinding, XiaomiMacroEvent } from "@openmouse/protocol/xiaomi"; | ||
| import { XIAOMI_BUTTON_NAMES, XIAOMI_KEY_COMBOS, XIAOMI_MACRO_EVENT_SIZE } from "@openmouse/protocol/xiaomi"; | ||
| import { | ||
| cloneM2NexProfile, | ||
| loadM2NexProfiles, | ||
|
|
@@ -143,6 +145,7 @@ import type { WLMouseHidClient } from "@openmouse/protocol/drivers/wlmouse/hid"; | |
| import type { WLMouseBeastX4kHidClient } from "@openmouse/protocol/drivers/wlmouse/beast-x-4k-hid"; | ||
| import type { DareuHidClient } from "@openmouse/protocol/drivers/dareu/hid"; | ||
| import type { IncottHidClient } from "@openmouse/protocol/drivers/incott/hid"; | ||
| import type { XiaomiHidClient } from "@openmouse/protocol/drivers/xiaomi/hid"; | ||
| import type { IncottMacro } from "@openmouse/protocol/incott"; | ||
| import { BASILISK_V3_PRO_BLUETOOTH, HID_FILTERS } from "./hid-filters"; | ||
| import { VENDOR_ID } from "@openmouse/protocol/drivers/vendors"; | ||
|
|
@@ -272,7 +275,7 @@ const needsOpenClasses = (): Array<ClientClass<SupportedClient> | undefined> => | |
| CM()?.MchoseHidClient, CM()?.MchoseDockHidClient, CM()?.MchoseA5ProMaxHidClient, CM()?.MchoseV3HidClient, | ||
| CM()?.MicrosoftHidClient, CM()?.DareuHidClient, CM()?.IncottHidClient, CM()?.BytechHidClient, CM()?.RapooHidClient, | ||
| CM()?.FaterHidClient, CM()?.CoolerMasterHidClient, CM()?.RedragonM690ProHidClient, CM()?.AjazzHidClient, | ||
| CM()?.HitscanHidClient, | ||
| CM()?.HitscanHidClient, CM()?.XiaomiHidClient, | ||
| ]; | ||
| const pulsarClasses = (): Array<ClientClass<PulsarClient> | undefined> => | ||
| [CM()?.PulsarHidClient, CM()?.PulsarProHidClient, CM()?.PulsarXs1HidClient]; | ||
|
|
@@ -300,6 +303,7 @@ const keychron4kClient = (): Keychron4kHidClient | null => activeAs(CM()?.Keychr | |
| const wlmouse4kClient = (): WLMouseBeastX4kHidClient | null => activeAs(CM()?.WLMouseBeastX4kHidClient); | ||
| const wallhackMouseClient = (): WallhackMouseHidClient | null => activeAs(CM()?.WallhackMouseHidClient); | ||
| const incottClient = (): IncottHidClient | null => activeAs(CM()?.IncottHidClient); | ||
| const xiaomiMacroClient = (): XiaomiHidClient | null => activeAs(CM()?.XiaomiHidClient); | ||
| /** Pulsar is the only family with the collection-explorer onboarding path. */ | ||
| const pulsarClient = (): PulsarClient | null => | ||
| active !== null ? activeAs<PulsarClient>(...pulsarClasses()) : null; | ||
|
|
@@ -329,6 +333,10 @@ let ksnakeMacrosError: string | null = null; | |
| let wallhackMacros: (WallhackMacroStep[] | null)[] | null = null; | ||
| let wallhackMacrosLoading = false; | ||
| let wallhackMacrosError: string | null = null; | ||
| let xiaomiMacros: (XiaomiMacroEvent[] | null)[] | null = null; | ||
| let xiaomiBindings: (XiaomiBinding | null)[] | null = null; | ||
| let xiaomiMacrosLoading = false; | ||
| let xiaomiMacrosError: string | null = null; | ||
| let wallhackCurves: Record<"classic" | "natural" | "jump" | "custom", WallhackCurvePoint[]> | null = null; | ||
| let wallhackCurvesLoading = false; | ||
| let wallhackCurvesError: string | null = null; | ||
|
|
@@ -588,6 +596,14 @@ const wallhackMacrosCache: SnapshotCloneCache<typeof wallhackMacros, typeof wall | |
| source: undefined, | ||
| output: undefined, | ||
| }; | ||
| const xiaomiMacrosCache: SnapshotCloneCache<typeof xiaomiMacros, typeof xiaomiMacros> = { | ||
| source: undefined, | ||
| output: undefined, | ||
| }; | ||
| const xiaomiBindingsCache: SnapshotCloneCache<typeof xiaomiBindings, typeof xiaomiBindings> = { | ||
| source: undefined, | ||
| output: undefined, | ||
| }; | ||
| const wallhackCurvesCache: SnapshotCloneCache<typeof wallhackCurves, typeof wallhackCurves> = { | ||
| source: undefined, | ||
| output: undefined, | ||
|
|
@@ -603,6 +619,18 @@ function cloneWallhackMacrosSnapshot( | |
| return source?.map((steps) => steps?.map((step) => ({ ...step, event: { ...step.event } })) ?? null) ?? null; | ||
| } | ||
|
|
||
| function cloneXiaomiMacrosSnapshot( | ||
| source: typeof xiaomiMacros, | ||
| ): typeof xiaomiMacros { | ||
| return source?.map((events) => events?.map((event) => ({ ...event })) ?? null) ?? null; | ||
| } | ||
|
|
||
| function cloneXiaomiBindingsSnapshot( | ||
| source: typeof xiaomiBindings, | ||
| ): typeof xiaomiBindings { | ||
| return source?.map((binding) => (binding ? { ...binding } : null)) ?? null; | ||
| } | ||
|
|
||
| function cloneWallhackCurvesSnapshot( | ||
| source: typeof wallhackCurves, | ||
| ): typeof wallhackCurves { | ||
|
|
@@ -675,6 +703,10 @@ function buildSnapshot(): ControlSnapshot { | |
| wallhackMacros: cachedSnapshotClone(wallhackMacrosCache, wallhackMacros, cloneWallhackMacrosSnapshot), | ||
| wallhackMacrosLoading, | ||
| wallhackMacrosError, | ||
| xiaomiMacros: cachedSnapshotClone(xiaomiMacrosCache, xiaomiMacros, cloneXiaomiMacrosSnapshot), | ||
| xiaomiBindings: cachedSnapshotClone(xiaomiBindingsCache, xiaomiBindings, cloneXiaomiBindingsSnapshot), | ||
| xiaomiMacrosLoading, | ||
| xiaomiMacrosError, | ||
| wallhackCurves: cachedSnapshotClone(wallhackCurvesCache, wallhackCurves, cloneWallhackCurvesSnapshot), | ||
| wallhackCurvesLoading, | ||
| wallhackCurvesError, | ||
|
|
@@ -1466,6 +1498,132 @@ export function clearWallhackMacroSlot(slot: number): void { | |
| }); | ||
| } | ||
|
|
||
| /** Read the eight Xiaomi Mi Gaming Mouse button bindings and macro bodies. */ | ||
| export function loadXiaomiMacros(): void { | ||
| if (xiaomiMacrosLoading || xiaomiMacros !== null) return; | ||
| const client = xiaomiMacroClient(); | ||
| if (!client) { | ||
| xiaomiMacrosError = st("macro.notReady"); | ||
| emit(); | ||
| return; | ||
| } | ||
| xiaomiMacrosLoading = true; | ||
| xiaomiMacrosError = null; | ||
| emit(); | ||
| void (async () => { | ||
| try { | ||
| const macros: (XiaomiMacroEvent[] | null)[] = []; | ||
| const bindings: (XiaomiBinding | null)[] = []; | ||
| for (let button = 0; button < 8; button++) { | ||
| const binding = await client.readBinding(button); | ||
| bindings.push({ ...binding }); | ||
| if (binding.mode === XIAOMI_KEY_COMBOS.MACROS_NO_DELAY || binding.mode === XIAOMI_KEY_COMBOS.MACROS_WITH_DELAY) { | ||
| macros.push((await client.readMacro(button)).map((event) => ({ ...event }))); | ||
| } else { | ||
| macros.push(null); | ||
| } | ||
| } | ||
| if (xiaomiMacroClient() !== client) return; | ||
| xiaomiMacros = macros; | ||
| xiaomiBindings = bindings; | ||
| xiaomiMacrosError = null; | ||
| } catch (error) { | ||
| xiaomiMacrosError = error instanceof Error ? error.message : String(error); | ||
| } finally { | ||
| xiaomiMacrosLoading = false; | ||
|
Comment on lines
+1530
to
+1533
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignore errors from a departed Xiaomi client. If a macro read rejects after a device switch, this catch writes the departed client's error into the current snapshot. The identity check at Line 1526 protects only successful reads. The error can block automatic loading for the next Xiaomi device. Check the read's client or generation before publishing its error or clearing its loading state. Keep retry available for failures that still belong to the active read.
|
||
| emit(); | ||
| } | ||
| })(); | ||
| } | ||
|
|
||
| function setXiaomiBinding(button: number, binding: XiaomiBinding): void { | ||
| xiaomiBindings = (xiaomiBindings ?? [null, null, null, null, null, null, null, null]).map( | ||
| (entry, index) => (index === button ? { ...binding } : entry), | ||
| ); | ||
| } | ||
|
|
||
| /** Stage a custom key-combo write for one Xiaomi button (0-7). */ | ||
| export function applyXiaomiCustomCombo(button: number, modifiers: number, key: number): void { | ||
| const client = xiaomiMacroClient(); | ||
| if (!client) { | ||
| setReadStatus(st("macro.notReady")); | ||
| emit(); | ||
| return; | ||
| } | ||
| stageChange({ | ||
| key: `xiaomi-combo-${button}`, | ||
| label: `Button ${button + 1} combo`, | ||
| command: `Save Xiaomi button ${button + 1} combo`, | ||
| progress: `Saving Xiaomi button ${button + 1} combo…`, | ||
| apply: async () => { | ||
| const live = xiaomiMacroClient(); | ||
| if (!live) throw new Error(st("ctl.gone")); | ||
| await live.setBinding(button, XIAOMI_KEY_COMBOS.CUSTOM_KEY_COMBO, modifiers, key); | ||
| if (xiaomiMacroClient() === live) { | ||
| setXiaomiBinding(button, { mode: XIAOMI_KEY_COMBOS.CUSTOM_KEY_COMBO, modifiers, key }); | ||
| emit(); | ||
| } | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** Stage a macro write for one Xiaomi button (0-7). */ | ||
| export function applyXiaomiMacro(button: number, mode: number, events: readonly XiaomiMacroEvent[]): void { | ||
| const client = xiaomiMacroClient(); | ||
| if (!client) { | ||
| setReadStatus(st("macro.notReady")); | ||
| emit(); | ||
| return; | ||
| } | ||
| const snapshot = events.map((event) => ({ ...event })); | ||
| stageChange({ | ||
| key: `xiaomi-macro-${button}`, | ||
| label: `Button ${button + 1} macro`, | ||
| command: `Save Xiaomi button ${button + 1} macro`, | ||
| progress: `Saving Xiaomi button ${button + 1} macro…`, | ||
| apply: async () => { | ||
| const live = xiaomiMacroClient(); | ||
| if (!live) throw new Error(st("ctl.gone")); | ||
| await live.setMacro(button, mode, snapshot); | ||
| if (xiaomiMacroClient() === live) { | ||
| xiaomiMacros = (xiaomiMacros ?? [null, null, null, null, null, null, null, null]).map((entry, index) => | ||
| index === button ? snapshot.map((event) => ({ ...event })) : entry, | ||
| ); | ||
| setXiaomiBinding(button, { mode, macroSize: snapshot.length * XIAOMI_MACRO_EVENT_SIZE }); | ||
| emit(); | ||
| } | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** Stage clearing the macro bound to one Xiaomi button (0-7). */ | ||
| export function clearXiaomiMacro(button: number): void { | ||
| const client = xiaomiMacroClient(); | ||
| if (!client) { | ||
| setReadStatus(st("macro.notReady")); | ||
| emit(); | ||
| return; | ||
| } | ||
| stageChange({ | ||
| key: `xiaomi-macro-${button}`, | ||
| label: `Clear button ${button + 1} macro`, | ||
| command: `Clear Xiaomi button ${button + 1} macro`, | ||
| progress: `Clearing Xiaomi button ${button + 1} macro…`, | ||
| apply: async () => { | ||
| const live = xiaomiMacroClient(); | ||
| if (!live) throw new Error(st("ctl.gone")); | ||
| await live.setMacro(button, XIAOMI_KEY_COMBOS.MACROS_NO_DELAY, []); | ||
| if (xiaomiMacroClient() === live) { | ||
| xiaomiMacros = (xiaomiMacros ?? [null, null, null, null, null, null, null, null]).map((entry, index) => | ||
| index === button ? null : entry, | ||
| ); | ||
| setXiaomiBinding(button, { mode: XIAOMI_KEY_COMBOS.MACROS_NO_DELAY, macroSize: 0 }); | ||
| emit(); | ||
| } | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function readCapabilities(): DeviceCapabilities { | ||
| const razer = activeAs<RazerHidClient>(CM()?.RazerHidClient); | ||
| const dm = dmClient(); | ||
|
|
@@ -6797,6 +6955,13 @@ export function applyDeviceButtonMapping(button: string, action: string): void { | |
| // remap to anything else supersedes it, and must do so even when stageChange | ||
| // drops the remap as "already matches", or Apply would still bind Macro. | ||
| if (action !== "Macro") dropPendingChange(`incott-macro-${button}`); | ||
| // A staged Xiaomi combo/macro write is superseded by any preset remap. The | ||
| // combo/macro keys use the wire button index, not the display name. | ||
| const xiaomiButtonIndex = XIAOMI_BUTTON_NAMES.indexOf(button as (typeof XIAOMI_BUTTON_NAMES)[number]); | ||
| if (xiaomiButtonIndex !== -1) { | ||
| dropPendingChange(`xiaomi-combo-${xiaomiButtonIndex}`); | ||
| dropPendingChange(`xiaomi-macro-${xiaomiButtonIndex}`); | ||
| } | ||
| stageChange({ | ||
| key: `button-${button}`, | ||
| label: `${button}: ${action}`, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,8 @@ export type DriverModules = typeof import("@openmouse/protocol/drivers/endgame/e | |
| & typeof import("@openmouse/protocol/drivers/coolermaster/hid") | ||
| & typeof import("@openmouse/protocol/drivers/ajazz/hid") | ||
| & typeof import("@openmouse/protocol/drivers/redragon/m690-pro-hid") | ||
| & typeof import("@openmouse/protocol/drivers/hitscan/hid"); | ||
| & typeof import("@openmouse/protocol/drivers/hitscan/hid") | ||
| & typeof import("@openmouse/protocol/drivers/xiaomi/hid"); | ||
|
|
||
| let loaded: DriverModules | null = null; | ||
| let loading: Promise<DriverModules> | null = null; | ||
|
|
@@ -143,6 +144,7 @@ export function ensureDriverModules(): Promise<DriverModules> { | |
| import("@openmouse/protocol/drivers/ajazz/hid"), | ||
| import("@openmouse/protocol/drivers/redragon/m690-pro-hid"), | ||
| import("@openmouse/protocol/drivers/hitscan/hid"), | ||
| import("@openmouse/protocol/drivers/xiaomi/hid"), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the protocol dependency with the new import. The manifest still pins Update the manifest and lockfile to a published release that contains the Xiaomi entrypoints. The future dependency-update workflow does not make this revision buildable.
|
||
| ]).then((mods) => (loaded = Object.assign({}, ...mods) as DriverModules)); | ||
| return loading; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset Xiaomi state when the active device changes.
Neither
activateClientNownorshowDisconnectedStateresets these fields. After Xiaomi A loads its bindings, Xiaomi B inherits A's arrays.loadXiaomiMacrosthen returns at Line 1503 becausexiaomiMacrosis already non-null.Reset the Xiaomi arrays, loading flag and error with the other per-device macro state. This must also allow a reconnect to read the device again.