@openmouse/protocol 0.49.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/README.md +2 -0
  2. package/dist/amkette/index.d.ts +2 -0
  3. package/dist/amkette/index.d.ts.map +1 -0
  4. package/dist/amkette/index.js +2 -0
  5. package/dist/amkette/index.js.map +1 -0
  6. package/dist/amkette/xs.d.ts +63 -0
  7. package/dist/amkette/xs.d.ts.map +1 -0
  8. package/dist/amkette/xs.js +79 -0
  9. package/dist/amkette/xs.js.map +1 -0
  10. package/dist/corsair/bragi.d.ts +70 -12
  11. package/dist/corsair/bragi.d.ts.map +1 -1
  12. package/dist/corsair/bragi.js +85 -16
  13. package/dist/corsair/bragi.js.map +1 -1
  14. package/dist/drivers/amkette/xs-hid.d.ts +142 -0
  15. package/dist/drivers/amkette/xs-hid.d.ts.map +1 -0
  16. package/dist/drivers/amkette/xs-hid.js +683 -0
  17. package/dist/drivers/amkette/xs-hid.js.map +1 -0
  18. package/dist/drivers/atk/hid.d.ts.map +1 -1
  19. package/dist/drivers/atk/hid.js +2 -2
  20. package/dist/drivers/atk/hid.js.map +1 -1
  21. package/dist/drivers/atk/products.d.ts +7 -0
  22. package/dist/drivers/atk/products.d.ts.map +1 -1
  23. package/dist/drivers/atk/products.js +19 -2
  24. package/dist/drivers/atk/products.js.map +1 -1
  25. package/dist/drivers/corsair/bragi-hid.d.ts +17 -12
  26. package/dist/drivers/corsair/bragi-hid.d.ts.map +1 -1
  27. package/dist/drivers/corsair/bragi-hid.js +35 -15
  28. package/dist/drivers/corsair/bragi-hid.js.map +1 -1
  29. package/dist/drivers/gwolves/products.d.ts +5 -0
  30. package/dist/drivers/gwolves/products.d.ts.map +1 -1
  31. package/dist/drivers/gwolves/products.js +17 -10
  32. package/dist/drivers/gwolves/products.js.map +1 -1
  33. package/dist/drivers/gwolves/xvi-hid.d.ts +1 -0
  34. package/dist/drivers/gwolves/xvi-hid.d.ts.map +1 -1
  35. package/dist/drivers/gwolves/xvi-hid.js +14 -7
  36. package/dist/drivers/gwolves/xvi-hid.js.map +1 -1
  37. package/dist/drivers/hitscan/xvi-hid.d.ts +110 -0
  38. package/dist/drivers/hitscan/xvi-hid.d.ts.map +1 -0
  39. package/dist/drivers/hitscan/xvi-hid.js +803 -0
  40. package/dist/drivers/hitscan/xvi-hid.js.map +1 -0
  41. package/dist/drivers/incott/hid.d.ts +5 -0
  42. package/dist/drivers/incott/hid.d.ts.map +1 -1
  43. package/dist/drivers/incott/hid.js +18 -7
  44. package/dist/drivers/incott/hid.js.map +1 -1
  45. package/dist/drivers/lamzu/hid.d.ts +4 -0
  46. package/dist/drivers/lamzu/hid.d.ts.map +1 -1
  47. package/dist/drivers/lamzu/hid.js +41 -2
  48. package/dist/drivers/lamzu/hid.js.map +1 -1
  49. package/dist/drivers/lamzu-atlantis/hid.d.ts.map +1 -1
  50. package/dist/drivers/lamzu-atlantis/hid.js +2 -3
  51. package/dist/drivers/lamzu-atlantis/hid.js.map +1 -1
  52. package/dist/drivers/logitech/hidpp.d.ts +2 -0
  53. package/dist/drivers/logitech/hidpp.d.ts.map +1 -1
  54. package/dist/drivers/logitech/hidpp.js +28 -13
  55. package/dist/drivers/logitech/hidpp.js.map +1 -1
  56. package/dist/drivers/mchose/hid.d.ts +9 -0
  57. package/dist/drivers/mchose/hid.d.ts.map +1 -1
  58. package/dist/drivers/mchose/hid.js +36 -5
  59. package/dist/drivers/mchose/hid.js.map +1 -1
  60. package/dist/drivers/motospeed/hid.js +1 -1
  61. package/dist/drivers/motospeed/hid.js.map +1 -1
  62. package/dist/drivers/mouse-types.d.ts +17 -1
  63. package/dist/drivers/mouse-types.d.ts.map +1 -1
  64. package/dist/drivers/ninjutso/hid.d.ts +1 -0
  65. package/dist/drivers/ninjutso/hid.d.ts.map +1 -1
  66. package/dist/drivers/ninjutso/hid.js +18 -11
  67. package/dist/drivers/ninjutso/hid.js.map +1 -1
  68. package/dist/drivers/pulsar/pulsar-hid.js +1 -1
  69. package/dist/drivers/pulsar/pulsar-hid.js.map +1 -1
  70. package/dist/drivers/razer/hid.d.ts +0 -1
  71. package/dist/drivers/razer/hid.d.ts.map +1 -1
  72. package/dist/drivers/razer/hid.js +14 -9
  73. package/dist/drivers/razer/hid.js.map +1 -1
  74. package/dist/drivers/redragon/hid.d.ts +44 -63
  75. package/dist/drivers/redragon/hid.d.ts.map +1 -1
  76. package/dist/drivers/redragon/hid.js +207 -270
  77. package/dist/drivers/redragon/hid.js.map +1 -1
  78. package/dist/drivers/registry.d.ts +2 -1
  79. package/dist/drivers/registry.d.ts.map +1 -1
  80. package/dist/drivers/registry.js +12 -1
  81. package/dist/drivers/registry.js.map +1 -1
  82. package/dist/drivers/ryunix/kyu-pro-mx1-hid.d.ts.map +1 -1
  83. package/dist/drivers/ryunix/kyu-pro-mx1-hid.js +4 -1
  84. package/dist/drivers/ryunix/kyu-pro-mx1-hid.js.map +1 -1
  85. package/dist/drivers/scyrox/hid.d.ts +5 -0
  86. package/dist/drivers/scyrox/hid.d.ts.map +1 -1
  87. package/dist/drivers/scyrox/hid.js +5 -0
  88. package/dist/drivers/scyrox/hid.js.map +1 -1
  89. package/dist/drivers/vendors.d.ts +8 -3
  90. package/dist/drivers/vendors.d.ts.map +1 -1
  91. package/dist/drivers/vendors.js +41 -13
  92. package/dist/drivers/vendors.js.map +1 -1
  93. package/dist/gearhub/index.d.ts +8 -0
  94. package/dist/gearhub/index.d.ts.map +1 -1
  95. package/dist/gearhub/index.js +18 -0
  96. package/dist/gearhub/index.js.map +1 -1
  97. package/dist/hitscan/index.d.ts +2 -0
  98. package/dist/hitscan/index.d.ts.map +1 -1
  99. package/dist/hitscan/index.js +2 -0
  100. package/dist/hitscan/index.js.map +1 -1
  101. package/dist/hitscan/xvi-inputs.d.ts +81 -0
  102. package/dist/hitscan/xvi-inputs.d.ts.map +1 -0
  103. package/dist/hitscan/xvi-inputs.js +393 -0
  104. package/dist/hitscan/xvi-inputs.js.map +1 -0
  105. package/dist/hitscan/xvi.d.ts +278 -0
  106. package/dist/hitscan/xvi.d.ts.map +1 -0
  107. package/dist/hitscan/xvi.js +773 -0
  108. package/dist/hitscan/xvi.js.map +1 -0
  109. package/dist/incott/index.d.ts +33 -8
  110. package/dist/incott/index.d.ts.map +1 -1
  111. package/dist/incott/index.js +44 -11
  112. package/dist/incott/index.js.map +1 -1
  113. package/dist/index.d.ts +1 -0
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +1 -0
  116. package/dist/index.js.map +1 -1
  117. package/dist/keychron/index.d.ts.map +1 -1
  118. package/dist/keychron/index.js +4 -0
  119. package/dist/keychron/index.js.map +1 -1
  120. package/dist/lamzu/atlantis.d.ts +18 -9
  121. package/dist/lamzu/atlantis.d.ts.map +1 -1
  122. package/dist/lamzu/atlantis.js +21 -6
  123. package/dist/lamzu/atlantis.js.map +1 -1
  124. package/dist/lamzu/index.d.ts +8 -5
  125. package/dist/lamzu/index.d.ts.map +1 -1
  126. package/dist/lamzu/index.js +10 -5
  127. package/dist/lamzu/index.js.map +1 -1
  128. package/dist/mchose/index.d.ts +14 -0
  129. package/dist/mchose/index.d.ts.map +1 -1
  130. package/dist/mchose/index.js +25 -2
  131. package/dist/mchose/index.js.map +1 -1
  132. package/dist/ninjutso/index.d.ts.map +1 -1
  133. package/dist/ninjutso/index.js +5 -2
  134. package/dist/ninjutso/index.js.map +1 -1
  135. package/dist/razer/codec.d.ts +3 -2
  136. package/dist/razer/codec.d.ts.map +1 -1
  137. package/dist/razer/codec.js +5 -4
  138. package/dist/razer/codec.js.map +1 -1
  139. package/dist/redragon/deprecated.d.ts +24 -0
  140. package/dist/redragon/deprecated.d.ts.map +1 -0
  141. package/dist/redragon/deprecated.js +54 -0
  142. package/dist/redragon/deprecated.js.map +1 -0
  143. package/dist/redragon/holtek.d.ts +127 -0
  144. package/dist/redragon/holtek.d.ts.map +1 -0
  145. package/dist/redragon/holtek.js +232 -0
  146. package/dist/redragon/holtek.js.map +1 -0
  147. package/dist/redragon/index.d.ts +9 -203
  148. package/dist/redragon/index.d.ts.map +1 -1
  149. package/dist/redragon/index.js +11 -430
  150. package/dist/redragon/index.js.map +1 -1
  151. package/dist/redragon/m612.d.ts +82 -0
  152. package/dist/redragon/m612.d.ts.map +1 -0
  153. package/dist/redragon/m612.js +166 -0
  154. package/dist/redragon/m612.js.map +1 -0
  155. package/dist/redragon/m724.d.ts +47 -0
  156. package/dist/redragon/m724.d.ts.map +1 -0
  157. package/dist/redragon/m724.js +107 -0
  158. package/dist/redragon/m724.js.map +1 -0
  159. package/dist/ryunix/index.d.ts +1 -0
  160. package/dist/ryunix/index.d.ts.map +1 -1
  161. package/dist/ryunix/index.js +1 -0
  162. package/dist/ryunix/index.js.map +1 -1
  163. package/dist/ryunix/kyu-pro-mx1.d.ts +10 -1
  164. package/dist/ryunix/kyu-pro-mx1.d.ts.map +1 -1
  165. package/dist/ryunix/kyu-pro-mx1.js +10 -12
  166. package/dist/ryunix/kyu-pro-mx1.js.map +1 -1
  167. package/dist/ryunix/protocol.d.ts +204 -0
  168. package/dist/ryunix/protocol.d.ts.map +1 -0
  169. package/dist/ryunix/protocol.js +366 -0
  170. package/dist/ryunix/protocol.js.map +1 -0
  171. package/dist/scyrox/index.d.ts +5 -0
  172. package/dist/scyrox/index.d.ts.map +1 -1
  173. package/dist/scyrox/index.js +37 -1
  174. package/dist/scyrox/index.js.map +1 -1
  175. package/package.json +6 -1
@@ -0,0 +1,803 @@
1
+ import { HITSCAN_XVI_BUTTON_IDS, HITSCAN_XVI_BUTTON_NAMES, HITSCAN_XVI_DPI_MAX, HITSCAN_XVI_DPI_MIN, HITSCAN_XVI_DPI_STEP, HITSCAN_XVI_LIFT_OFF_MILLIMETRES, HITSCAN_XVI_MAX_DPI_STAGES, HITSCAN_XVI_MAX_MACROS, HITSCAN_XVI_NOTIFICATION_REPORT_ID, HITSCAN_XVI_PACKET_LENGTH, HITSCAN_XVI_POLLING_RATES, HITSCAN_XVI_PRODUCTS, HITSCAN_XVI_REPORT_ID, HITSCAN_XVI_SLEEP_OPTIONS, HITSCAN_XVI_STATUS_ASLEEP, HITSCAN_XVI_TIMING_MAX_MS, HITSCAN_XVI_VENDOR_ID, hitscanXviActiveStageGetRequest, hitscanXviActiveStageSetRequest, hitscanXviAngleGetRequest, hitscanXviAngleSetRequest, hitscanXviBatteryRequest, hitscanXviButtonGetRequest, hitscanXviButtonSetRequest, hitscanXviDecodeActiveStage, hitscanXviDecodeAngle, hitscanXviDecodeBattery, hitscanXviDecodeButton, hitscanXviDecodeDevicePid, hitscanXviDecodeDpiColors, hitscanXviDecodeDpiStages, hitscanXviDecodeFirmware, hitscanXviDecodeLiftOff, hitscanXviDecodeMacroData, hitscanXviDecodeMacroIds, hitscanXviDecodeMacroName, hitscanXviDecodeMacroSize, hitscanXviDecodeNotification, hitscanXviDecodePollingRate, hitscanXviDecodeProfileCount, hitscanXviDecodeProfileId, hitscanXviDecodeProfileName, hitscanXviDecodeScrollHighPerformance, hitscanXviDecodeSignal, hitscanXviDecodeSleep, hitscanXviDecodeSpdt, hitscanXviDecodeSplitDpi, hitscanXviDecodeTimings, hitscanXviDevicePidRequest, hitscanXviDpiColorsGetRequest, hitscanXviDpiColorsSetRequest, hitscanXviDpiStagesGetRequest, hitscanXviDpiStagesSetRequest, hitscanXviFirmwareRequest, hitscanXviFlagGetRequest, hitscanXviFlagSetRequest, hitscanXviLiftOffGetRequest, hitscanXviLiftOffSetRequest, hitscanXviMacroAllocateRequest, hitscanXviMacroDataRequest, hitscanXviMacroDataSetRequest, hitscanXviMacroDeleteRequest, hitscanXviMacroListRequest, hitscanXviMacroNameRequest, hitscanXviMacroNameSetRequest, hitscanXviMacroSizeRequest, hitscanXviPollingRateGetRequest, hitscanXviPollingRateSetRequest, hitscanXviProfileGetRequest, hitscanXviProfileListRequest, hitscanXviProfileNameGetRequest, hitscanXviProfileNameSetRequest, hitscanXviProfileSetRequest, hitscanXviReply, hitscanXviResetProfileRequest, hitscanXviScrollHighPerformanceGetRequest, hitscanXviScrollHighPerformanceSetRequest, hitscanXviSleepGetRequest, hitscanXviSignalRequest, hitscanXviSleepSetRequest, hitscanXviSpdtGetRequest, hitscanXviSpdtSetRequest, hitscanXviSplitDpiGetRequest, hitscanXviSplitDpiSetRequest, hitscanXviTimingsGetRequest, hitscanXviTimingsSetRequest, HITSCAN_XVI_COMMAND, } from "../../hitscan/xvi.js";
2
+ import { hitscanXviButtonActionForLabel, hitscanXviButtonLabel, hitscanXviDecodeButtonAction, hitscanXviDecodeGbkName, hitscanXviDecodeMacroEvents, hitscanXviEncodeButtonAction, hitscanXviEncodeGbkName, hitscanXviEncodeMacroEvents, hitscanXviIsLeftClick, HITSCAN_XVI_BUTTON_LABELS, } from "../../hitscan/xvi-inputs.js";
3
+ const POLL_INTERVAL_MS = 20;
4
+ const POLLS_PER_SEND = 30;
5
+ const SENDS = 5;
6
+ const CHUNK_BYTES = 50;
7
+ const LIFT_OFF_STOPS = { 0.7: "Low", 1: "Medium", 2: "High" };
8
+ /** Vendor signal levels (1 strong, 2 medium, 3 weak) as the shared 0-4 bars. */
9
+ const SIGNAL_BARS = { 1: 4, 2: 2, 3: 1 };
10
+ const DEFAULT_STAGES = [400, 800, 1600, 3200];
11
+ /**
12
+ * Hitscan's current generation, as driven by control.hitscan.com release
13
+ * `1.0.15-fe600431`. Vendor `0x3770`, 64-byte feature report 0, mouse device
14
+ * id 2. This is separate from the native Hitscan Utility protocol in
15
+ * `./hid.ts`: that one uses report 8 and checksummed flash records.
16
+ *
17
+ * Not tested on hardware. Every setter verifies by reading the value back and
18
+ * throws on mismatch, so a wrong assumption fails loudly.
19
+ */
20
+ export class HitscanXviHidClient {
21
+ device;
22
+ queue = Promise.resolve();
23
+ opening = null;
24
+ disposed = false;
25
+ listening = false;
26
+ lastStatus = null;
27
+ notify;
28
+ onInput = (event) => {
29
+ if (event.reportId !== HITSCAN_XVI_NOTIFICATION_REPORT_ID)
30
+ return;
31
+ const bytes = new Uint8Array(event.data.buffer, event.data.byteOffset, event.data.byteLength);
32
+ const notification = hitscanXviDecodeNotification(bytes);
33
+ if (notification)
34
+ (this.notifications ?? this.notify)?.(notification);
35
+ };
36
+ constructor(device, options = {}) {
37
+ this.device = device;
38
+ this.notify = options.onNotification;
39
+ }
40
+ static isSupported(device) {
41
+ if (device.vendorId !== HITSCAN_XVI_VENDOR_ID || !HITSCAN_XVI_PRODUCTS.has(device.productId))
42
+ return false;
43
+ const control = (collections) => collections.some((collection) => collection.featureReports.some((report) => report.reportId === HITSCAN_XVI_REPORT_ID
44
+ && report.items.reduce((bits, item) => bits + item.reportSize * item.reportCount, 0) === HITSCAN_XVI_PACKET_LENGTH * 8)
45
+ || control(collection.children ?? []));
46
+ return control(device.collections);
47
+ }
48
+ get pollIntervalMs() {
49
+ return this.isWireless() ? 10_000 : 30_000;
50
+ }
51
+ displayName() {
52
+ const product = HITSCAN_XVI_PRODUCTS.get(this.device.productId);
53
+ if (product?.model)
54
+ return `Hitscan ${product.model}`;
55
+ return this.device.productName?.trim() || "Hitscan mouse";
56
+ }
57
+ deviceBrand() {
58
+ return "Hitscan";
59
+ }
60
+ isWireless() {
61
+ return HITSCAN_XVI_PRODUCTS.get(this.device.productId)?.transport === "receiver";
62
+ }
63
+ maxDpi() {
64
+ return HITSCAN_XVI_DPI_MAX;
65
+ }
66
+ getDpiOptions() {
67
+ const values = [];
68
+ for (let dpi = HITSCAN_XVI_DPI_MIN; dpi <= HITSCAN_XVI_DPI_MAX; dpi += HITSCAN_XVI_DPI_STEP)
69
+ values.push(dpi);
70
+ return values;
71
+ }
72
+ getSupportedPollingRates() {
73
+ return [...HITSCAN_XVI_POLLING_RATES];
74
+ }
75
+ getSleepOptions() {
76
+ return [...HITSCAN_XVI_SLEEP_OPTIONS];
77
+ }
78
+ getDebounceMaxMs() {
79
+ return HITSCAN_XVI_TIMING_MAX_MS;
80
+ }
81
+ /** True when the interface carries input report 4 for unsolicited updates. */
82
+ startNotifications(listener) {
83
+ if (listener !== undefined)
84
+ this.notifications = listener;
85
+ const carries = (collections) => collections.some((collection) => collection.inputReports.some((report) => report.reportId === HITSCAN_XVI_NOTIFICATION_REPORT_ID)
86
+ || carries(collection.children ?? []));
87
+ if (!carries(this.device.collections))
88
+ return false;
89
+ if (!this.listening) {
90
+ this.device.addEventListener("inputreport", this.onInput);
91
+ this.listening = true;
92
+ }
93
+ return true;
94
+ }
95
+ notifications;
96
+ open() {
97
+ if (this.disposed)
98
+ return Promise.reject(new Error("The Hitscan client is closed."));
99
+ this.opening ??= (async () => {
100
+ if (!this.device.opened)
101
+ await this.device.open();
102
+ if (this.disposed)
103
+ throw new Error("The Hitscan client is closed.");
104
+ })().catch((error) => {
105
+ this.opening = null;
106
+ throw error;
107
+ });
108
+ return this.opening;
109
+ }
110
+ async close() {
111
+ this.disposed = true;
112
+ this.lastStatus = null;
113
+ await this.opening?.catch(() => undefined);
114
+ this.opening = null;
115
+ await this.queue;
116
+ if (this.listening)
117
+ this.device.removeEventListener("inputreport", this.onInput);
118
+ this.listening = false;
119
+ if (this.device.opened)
120
+ await this.device.close();
121
+ }
122
+ /** Serializes complete command round trips. */
123
+ run(task) {
124
+ const operation = this.queue.then(task);
125
+ this.queue = operation.catch(() => undefined);
126
+ return operation;
127
+ }
128
+ async transact(request) {
129
+ await this.open();
130
+ const label = `0x${(request[5] ?? 0).toString(16)}`;
131
+ let lastError = null;
132
+ for (let send = 0; send < SENDS; send++) {
133
+ if (this.disposed)
134
+ throw new Error("The Hitscan client is closed.");
135
+ try {
136
+ await this.device.sendFeatureReport(HITSCAN_XVI_REPORT_ID, request);
137
+ }
138
+ catch (error) {
139
+ throw error instanceof Error ? error : new Error(String(error));
140
+ }
141
+ for (let poll = 0; poll < POLLS_PER_SEND; poll++) {
142
+ await hitscanXviDelay(POLL_INTERVAL_MS);
143
+ let view;
144
+ try {
145
+ view = await this.device.receiveFeatureReport(HITSCAN_XVI_REPORT_ID);
146
+ }
147
+ catch (error) {
148
+ // A native read timeout is retryable; a closed device is not.
149
+ lastError = error;
150
+ if (this.disposed || !this.device.opened)
151
+ throw error instanceof Error ? error : new Error(String(error));
152
+ continue;
153
+ }
154
+ const reply = hitscanXviReply(new Uint8Array(view.buffer, view.byteOffset, view.byteLength), request);
155
+ if (!reply)
156
+ continue;
157
+ if (reply[0] === HITSCAN_XVI_STATUS_ASLEEP) {
158
+ throw new Error(`${this.displayName()} is asleep or out of range. Move it, then retry.`);
159
+ }
160
+ return reply;
161
+ }
162
+ }
163
+ throw lastError instanceof Error
164
+ ? lastError
165
+ : new Error(`${this.displayName()} did not answer command ${label}.`);
166
+ }
167
+ patch(changes) {
168
+ if (this.lastStatus)
169
+ this.lastStatus = { ...this.lastStatus, ...changes };
170
+ }
171
+ async optional(read) {
172
+ try {
173
+ return await read();
174
+ }
175
+ catch {
176
+ return null;
177
+ }
178
+ }
179
+ async readStatus(live = false) {
180
+ return this.run(async () => {
181
+ if (live && this.lastStatus)
182
+ return await this.readLiveStatus(this.lastStatus);
183
+ const firmware = hitscanXviDecodeFirmware(await this.transact(hitscanXviFirmwareRequest()));
184
+ if (!firmware)
185
+ throw new Error("The Hitscan mouse did not report valid firmware.");
186
+ const wireless = this.isWireless();
187
+ const receiverFirmware = wireless
188
+ ? hitscanXviDecodeFirmware(await this.transact(hitscanXviFirmwareRequest(0)))
189
+ : null;
190
+ const pairedPid = wireless
191
+ ? await this.optional(() => this.transact(hitscanXviDevicePidRequest()).then(hitscanXviDecodeDevicePid))
192
+ : this.device.productId;
193
+ const model = HITSCAN_XVI_PRODUCTS.get(pairedPid ?? -1)?.model;
194
+ const name = model ? `Hitscan ${model}` : this.displayName();
195
+ const profileCount = hitscanXviDecodeProfileCount(await this.transact(hitscanXviProfileListRequest()));
196
+ const profileId = hitscanXviDecodeProfileId(await this.transact(hitscanXviProfileGetRequest()));
197
+ if (!profileCount || !profileId || profileId > profileCount)
198
+ throw new Error("The Hitscan profile table is invalid.");
199
+ const names = [];
200
+ for (let id = 1; id <= profileCount; id++) {
201
+ const decoded = await this.optional(() => this.transact(hitscanXviProfileNameGetRequest(id)).then(hitscanXviDecodeProfileName));
202
+ names.push(decoded?.name || `Profile ${id}`);
203
+ }
204
+ const settings = await this.readSettings(profileId);
205
+ const battery = await this.optional(() => this.transact(hitscanXviBatteryRequest()).then(hitscanXviDecodeBattery));
206
+ const signal = wireless
207
+ ? await this.optional(() => this.transact(hitscanXviSignalRequest()).then(hitscanXviDecodeSignal))
208
+ : null;
209
+ const stages = settings.stages;
210
+ const active = settings.activeStage;
211
+ const dpi = stages[active]?.x ?? HITSCAN_XVI_DPI_MIN;
212
+ const firmwareList = [`Mouse ${firmware}`];
213
+ if (receiverFirmware)
214
+ firmwareList.push(`Dongle ${receiverFirmware}`);
215
+ this.lastStatus = {
216
+ brand: "Hitscan",
217
+ name,
218
+ batteryPercent: battery?.percent ?? null,
219
+ batteryState: battery ? (battery.charging ? (battery.percent >= 100 ? "Full" : "Charging") : "Discharging") : "Unknown",
220
+ dpi,
221
+ ...(settings.splitDpi ? { dpiY: stages[active]?.y ?? dpi } : {}),
222
+ supportsSeparateDpiAxes: settings.splitDpi,
223
+ dpiStages: stages.slice(0, settings.stageCount).map((stage) => stage.x),
224
+ dpiStageColors: settings.colors,
225
+ activeDpiStage: active,
226
+ pollingRateHz: settings.pollingRateHz,
227
+ supportedPollingRates: this.getSupportedPollingRates(),
228
+ activeProfile: profileId,
229
+ profileCount,
230
+ profileNames: names,
231
+ connectionType: wireless ? "Wireless" : "Wired",
232
+ connectionDetail: wireless ? "2.4 GHz receiver · Hitscan web protocol" : "USB · Hitscan web protocol",
233
+ motionSync: settings.motionSync,
234
+ hyperMode: settings.hyperMode,
235
+ hitscanCompetitiveMode: settings.competitiveMode,
236
+ angleTuning: settings.angle,
237
+ sleepTimeout: settings.sleep,
238
+ liftOffDistance: LIFT_OFF_STOPS[settings.liftOff] ?? null,
239
+ liftOffScale: this.liftOffScale(settings.liftOff),
240
+ debounceMs: settings.timings?.beforePress ?? null,
241
+ hitscanClickTimings: settings.timings,
242
+ hitscanSpdt: settings.spdt,
243
+ hitscanTournamentScroll: settings.scrollHighPerformance,
244
+ signalStrength: signal ? (SIGNAL_BARS[signal.level] ?? null) : null,
245
+ buttonMappings: settings.buttonMappings,
246
+ buttonOptions: HITSCAN_XVI_BUTTON_LABELS.map(([label]) => label),
247
+ fixedButtons: settings.fixedButtons,
248
+ firmware: firmwareList,
249
+ ui: {
250
+ family: "hitscan-xvi",
251
+ hideUnsupportedPollingRates: true,
252
+ hideRippleControl: true,
253
+ hideAngleSnapping: true,
254
+ showAdvancedSection: true,
255
+ ...(wireless ? { forceShowBattery: true } : {}),
256
+ defaultDisplayName: name,
257
+ statusNote: "Hitscan's control.hitscan.com protocol; not tested on hardware yet.",
258
+ dpiStageEditor: {
259
+ maxStages: HITSCAN_XVI_MAX_DPI_STAGES,
260
+ countEditable: true,
261
+ minDpi: HITSCAN_XVI_DPI_MIN,
262
+ maxDpi: HITSCAN_XVI_DPI_MAX,
263
+ stepDpi: HITSCAN_XVI_DPI_STEP,
264
+ },
265
+ },
266
+ };
267
+ return this.lastStatus;
268
+ });
269
+ }
270
+ async readLiveStatus(previous) {
271
+ const battery = await this.optional(() => this.transact(hitscanXviBatteryRequest()).then(hitscanXviDecodeBattery));
272
+ const profileId = hitscanXviDecodeProfileId(await this.transact(hitscanXviProfileGetRequest()));
273
+ const signal = this.isWireless()
274
+ ? await this.optional(() => this.transact(hitscanXviSignalRequest()).then(hitscanXviDecodeSignal))
275
+ : null;
276
+ let next = {
277
+ ...previous,
278
+ batteryPercent: battery?.percent ?? null,
279
+ batteryState: battery ? (battery.charging ? (battery.percent >= 100 ? "Full" : "Charging") : "Discharging") : "Unknown",
280
+ signalStrength: signal ? (SIGNAL_BARS[signal.level] ?? null) : previous.signalStrength,
281
+ };
282
+ if (profileId && profileId !== previous.activeProfile) {
283
+ next = { ...next, activeProfile: profileId };
284
+ }
285
+ this.lastStatus = next;
286
+ return next;
287
+ }
288
+ liftOffScale(millimetres) {
289
+ return {
290
+ value: millimetres,
291
+ min: HITSCAN_XVI_LIFT_OFF_MILLIMETRES[0] ?? 0.7,
292
+ max: HITSCAN_XVI_LIFT_OFF_MILLIMETRES[HITSCAN_XVI_LIFT_OFF_MILLIMETRES.length - 1] ?? 2,
293
+ millimetres,
294
+ minMillimetres: HITSCAN_XVI_LIFT_OFF_MILLIMETRES[0] ?? 0.7,
295
+ maxMillimetres: HITSCAN_XVI_LIFT_OFF_MILLIMETRES[HITSCAN_XVI_LIFT_OFF_MILLIMETRES.length - 1] ?? 2,
296
+ millimetresByCode: [...HITSCAN_XVI_LIFT_OFF_MILLIMETRES],
297
+ };
298
+ }
299
+ async readSettings(profileId) {
300
+ // The vendor requests six slots even though every shipped model has four;
301
+ // a reply above four is a firmware generation this driver does not model.
302
+ const stages = hitscanXviDecodeDpiStages(await this.transact(hitscanXviDpiStagesGetRequest(profileId)));
303
+ if (!stages || stages.stages.length > HITSCAN_XVI_MAX_DPI_STAGES) {
304
+ throw new Error("The Hitscan mouse returned an unsupported DPI stage table.");
305
+ }
306
+ const active = hitscanXviDecodeActiveStage(await this.transact(hitscanXviActiveStageGetRequest(profileId)));
307
+ if (!active || active > stages.stages.length)
308
+ throw new Error("The Hitscan mouse returned an invalid active DPI stage.");
309
+ const pollingRateHz = hitscanXviDecodePollingRate(await this.transact(hitscanXviPollingRateGetRequest(profileId)));
310
+ const liftOff = hitscanXviDecodeLiftOff(await this.transact(hitscanXviLiftOffGetRequest(profileId)));
311
+ if (pollingRateHz === null || liftOff === null)
312
+ throw new Error("The Hitscan mouse returned invalid core settings.");
313
+ const colors = await this.optional(() => this.transact(hitscanXviDpiColorsGetRequest(profileId)).then(hitscanXviDecodeDpiColors));
314
+ const splitDpi = hitscanXviDecodeSplitDpi(await this.transact(hitscanXviSplitDpiGetRequest(profileId)));
315
+ const sleep = await this.optional(() => this.transact(hitscanXviSleepGetRequest(profileId)).then(hitscanXviDecodeSleep));
316
+ const angle = hitscanXviDecodeAngle(await this.transact(hitscanXviAngleGetRequest(profileId)));
317
+ const motionSync = hitscanXviDecodeFlagPayload(profileId, await this.transact(hitscanXviFlagGetRequest(profileId, HITSCAN_XVI_COMMAND.motionSyncGet)));
318
+ const hyperMode = hitscanXviDecodeFlagPayload(profileId, await this.transact(hitscanXviFlagGetRequest(profileId, HITSCAN_XVI_COMMAND.hyperModeGet)));
319
+ const competitiveMode = await this.optional(async () => {
320
+ const reply = await this.transact(hitscanXviFlagGetRequest(profileId, HITSCAN_XVI_COMMAND.trackingModeGet));
321
+ return hitscanXviDecodeFlagPayload(profileId, reply);
322
+ });
323
+ const spdt = await this.optional(() => this.transact(hitscanXviSpdtGetRequest(profileId)).then(hitscanXviDecodeSpdt));
324
+ const scrollHighPerformance = await this.optional(() => this.transact(hitscanXviScrollHighPerformanceGetRequest(profileId)).then(hitscanXviDecodeScrollHighPerformance));
325
+ const timings = await this.optional(() => this.transact(hitscanXviTimingsGetRequest(profileId)).then(hitscanXviDecodeTimings));
326
+ const buttons = await this.optional(() => this.readButtonSet(profileId));
327
+ if (splitDpi === null || angle === null || motionSync === null || hyperMode === null) {
328
+ throw new Error("The Hitscan mouse returned invalid sensor settings.");
329
+ }
330
+ return {
331
+ stageCount: stages.stages.length,
332
+ activeStage: active - 1,
333
+ stages: stages.stages,
334
+ splitDpi,
335
+ colors: colors ?? [],
336
+ pollingRateHz,
337
+ liftOff,
338
+ sleep,
339
+ angle,
340
+ motionSync,
341
+ hyperMode,
342
+ competitiveMode,
343
+ spdt,
344
+ scrollHighPerformance,
345
+ timings,
346
+ buttonMappings: buttons?.buttonMappings ?? {},
347
+ fixedButtons: buttons?.fixedButtons ?? [],
348
+ };
349
+ }
350
+ async readButtonSet(profileId) {
351
+ const mappings = {};
352
+ const fixed = [];
353
+ for (const [index, name] of HITSCAN_XVI_BUTTON_NAMES.entries()) {
354
+ const binding = await this.readButton(profileId, HITSCAN_XVI_BUTTON_IDS[index]);
355
+ const action = hitscanXviDecodeButtonAction({ functionId: binding.functionId, data: binding.data });
356
+ mappings[name] = hitscanXviButtonLabel(action);
357
+ if (name !== "Left" && hitscanXviButtonActionForLabel(mappings[name]) === null)
358
+ fixed.push(name);
359
+ }
360
+ return { buttonMappings: mappings, fixedButtons: fixed };
361
+ }
362
+ async readButton(profileId, buttonId) {
363
+ let decoded = hitscanXviDecodeButton(await this.transact(hitscanXviButtonGetRequest(profileId, buttonId, 10)));
364
+ if (!decoded || decoded.profileId !== profileId || decoded.buttonId !== buttonId) {
365
+ throw new Error("The Hitscan mouse returned an invalid button reply.");
366
+ }
367
+ if (decoded.data.length > 10) {
368
+ decoded = hitscanXviDecodeButton(await this.transact(hitscanXviButtonGetRequest(profileId, buttonId, decoded.data.length)));
369
+ if (!decoded || decoded.profileId !== profileId || decoded.buttonId !== buttonId) {
370
+ throw new Error("The Hitscan mouse returned an invalid button reply.");
371
+ }
372
+ }
373
+ return { functionId: decoded.functionId, data: decoded.data };
374
+ }
375
+ async writeFlag(profileId, command, enabled, label) {
376
+ const getCommand = command | 0x80;
377
+ await this.transact(hitscanXviFlagSetRequest(profileId, command, enabled));
378
+ const reply = await this.transact(hitscanXviFlagGetRequest(profileId, getCommand));
379
+ const confirmed = hitscanXviDecodeFlagPayload(profileId, reply);
380
+ if (confirmed !== enabled)
381
+ throw new Error(`The Hitscan mouse kept ${label} ${confirmed ? "on" : "off"}.`);
382
+ return confirmed;
383
+ }
384
+ async writeStages(profileId, stages) {
385
+ await this.transact(hitscanXviDpiStagesSetRequest(profileId, stages));
386
+ const confirmed = hitscanXviDecodeDpiStages(await this.transact(hitscanXviDpiStagesGetRequest(profileId)));
387
+ if (!confirmed)
388
+ throw new Error("The Hitscan mouse did not return its DPI table.");
389
+ if (confirmed.stages.length !== stages.length)
390
+ throw new Error("The Hitscan mouse kept a different DPI stage count.");
391
+ const expected = stages.slice(0, confirmed.stages.length);
392
+ const equal = expected.every((stage, index) => stage.x === confirmed.stages[index]?.x && stage.y === confirmed.stages[index]?.y);
393
+ if (!equal)
394
+ throw new Error("The Hitscan mouse did not retain the requested DPI stages.");
395
+ this.patch({
396
+ dpiStages: confirmed.stages.map((stage) => stage.x),
397
+ ...(this.lastStatus?.activeDpiStage === undefined
398
+ ? {}
399
+ : { dpi: confirmed.stages[this.lastStatus.activeDpiStage]?.x ?? this.lastStatus.dpi }),
400
+ });
401
+ return confirmed.stages;
402
+ }
403
+ setDpi(dpi) {
404
+ return this.run(async () => {
405
+ const profileId = await this.currentProfile();
406
+ const settings = await this.readSettings(profileId);
407
+ if (!settings.stages[settings.activeStage])
408
+ throw new Error("The Hitscan mouse reported no active DPI stage.");
409
+ const stages = settings.stages.map((entry, index) => index === settings.activeStage
410
+ ? { x: dpi, y: settings.splitDpi ? entry.y : dpi }
411
+ : entry);
412
+ await this.writeStages(profileId, stages);
413
+ this.patch({ dpi, ...(settings.splitDpi ? { dpiY: dpi } : {}) });
414
+ return dpi;
415
+ });
416
+ }
417
+ setDpiStageValue(stage, dpi) {
418
+ return this.run(async () => {
419
+ const profileId = await this.currentProfile();
420
+ const settings = await this.readSettings(profileId);
421
+ if (!Number.isInteger(stage) || stage < 0 || stage >= settings.stages.length) {
422
+ throw new Error(`This mouse has no DPI stage ${stage + 1}.`);
423
+ }
424
+ const entry = settings.stages[stage];
425
+ const updated = { x: dpi, y: settings.splitDpi ? entry.y : dpi };
426
+ const stages = settings.stages.map((value, index) => index === stage ? updated : value);
427
+ await this.writeStages(profileId, stages);
428
+ return dpi;
429
+ });
430
+ }
431
+ setDpiStageCount(count) {
432
+ return this.run(async () => {
433
+ if (!Number.isInteger(count) || count < 1 || count > HITSCAN_XVI_MAX_DPI_STAGES) {
434
+ throw new Error(`This mouse supports between 1 and ${HITSCAN_XVI_MAX_DPI_STAGES} DPI stages.`);
435
+ }
436
+ const profileId = await this.currentProfile();
437
+ const settings = await this.readSettings(profileId);
438
+ if (settings.activeStage >= count) {
439
+ await this.transact(hitscanXviActiveStageSetRequest(profileId, count));
440
+ }
441
+ const stages = Array.from({ length: count }, (_, index) => settings.stages[index] ?? { x: DEFAULT_STAGES[index] ?? DEFAULT_STAGES[DEFAULT_STAGES.length - 1], y: DEFAULT_STAGES[index] ?? DEFAULT_STAGES[DEFAULT_STAGES.length - 1] });
442
+ await this.writeStages(profileId, stages);
443
+ return count;
444
+ });
445
+ }
446
+ setActiveDpiStage(stage) {
447
+ return this.run(async () => {
448
+ const profileId = await this.currentProfile();
449
+ const settings = await this.readSettings(profileId);
450
+ if (!Number.isInteger(stage) || stage < 0 || stage >= settings.stages.length) {
451
+ throw new Error(`This mouse has no DPI stage ${stage + 1}.`);
452
+ }
453
+ await this.transact(hitscanXviActiveStageSetRequest(profileId, stage + 1));
454
+ const confirmed = hitscanXviDecodeActiveStage(await this.transact(hitscanXviActiveStageGetRequest(profileId)));
455
+ if (confirmed !== stage + 1)
456
+ throw new Error("The Hitscan mouse kept a different DPI stage.");
457
+ this.patch({ activeDpiStage: stage, dpi: settings.stages[stage]?.x ?? this.lastStatus?.dpi ?? HITSCAN_XVI_DPI_MIN });
458
+ return stage;
459
+ });
460
+ }
461
+ setDpiStageColor(stage, color) {
462
+ return this.run(async () => {
463
+ const match = /^#?([0-9a-f]{6})$/i.exec(color.trim());
464
+ if (!match)
465
+ throw new Error(`${color} is not a #rrggbb colour.`);
466
+ if (!Number.isInteger(stage) || stage < 0 || stage >= HITSCAN_XVI_MAX_DPI_STAGES) {
467
+ throw new Error(`This mouse has no DPI stage ${stage + 1}.`);
468
+ }
469
+ const profileId = await this.currentProfile();
470
+ const current = hitscanXviDecodeDpiColors(await this.transact(hitscanXviDpiColorsGetRequest(profileId)));
471
+ if (!current)
472
+ throw new Error("The Hitscan mouse did not return its stage colours.");
473
+ const hex = match[1].toLowerCase();
474
+ current[stage] = `#${hex}`;
475
+ const rgb = current.flatMap((value) => [1, 3, 5].map((at) => Number.parseInt(value.slice(at, at + 2), 16)));
476
+ await this.transact(hitscanXviDpiColorsSetRequest(profileId, rgb));
477
+ const confirmed = hitscanXviDecodeDpiColors(await this.transact(hitscanXviDpiColorsGetRequest(profileId)));
478
+ if (confirmed?.[stage] !== `#${hex}`)
479
+ throw new Error("The Hitscan mouse did not retain the stage colour.");
480
+ this.patch({ dpiStageColors: confirmed });
481
+ return `#${hex}`;
482
+ });
483
+ }
484
+ setSeparateDpiAxes(enabled) {
485
+ return this.run(async () => {
486
+ const profileId = await this.currentProfile();
487
+ await this.transact(hitscanXviSplitDpiSetRequest(profileId, enabled));
488
+ const confirmed = hitscanXviDecodeSplitDpi(await this.transact(hitscanXviSplitDpiGetRequest(profileId)));
489
+ if (confirmed !== enabled)
490
+ throw new Error(`The Hitscan mouse kept separate X/Y ${confirmed ? "on" : "off"}.`);
491
+ this.patch({ supportsSeparateDpiAxes: confirmed });
492
+ return confirmed;
493
+ });
494
+ }
495
+ setPollingRate(hz) {
496
+ return this.run(async () => {
497
+ const profileId = await this.currentProfile();
498
+ await this.transact(hitscanXviPollingRateSetRequest(profileId, hz));
499
+ const confirmed = hitscanXviDecodePollingRate(await this.transact(hitscanXviPollingRateGetRequest(profileId)));
500
+ if (confirmed !== hz)
501
+ throw new Error(`The Hitscan mouse kept ${confirmed ?? "an unknown"} Hz instead of ${hz} Hz.`);
502
+ this.patch({ pollingRateHz: confirmed });
503
+ return confirmed;
504
+ });
505
+ }
506
+ setLiftOffDistance(value) {
507
+ const millimetres = Object.entries(LIFT_OFF_STOPS).find(([, stop]) => stop === value)?.[0];
508
+ if (millimetres === undefined)
509
+ return Promise.reject(new Error(`This mouse does not support a ${value.toLowerCase()} lift-off distance.`));
510
+ return this.run(async () => {
511
+ const profileId = await this.currentProfile();
512
+ await this.transact(hitscanXviLiftOffSetRequest(profileId, Number(millimetres)));
513
+ const confirmed = hitscanXviDecodeLiftOff(await this.transact(hitscanXviLiftOffGetRequest(profileId)));
514
+ if (confirmed !== Number(millimetres))
515
+ throw new Error("The Hitscan mouse kept a different lift-off distance.");
516
+ this.patch({ liftOffDistance: value, liftOffScale: this.liftOffScale(confirmed) });
517
+ return value;
518
+ });
519
+ }
520
+ setSleepTimeout(seconds) {
521
+ if (seconds !== null && !HITSCAN_XVI_SLEEP_OPTIONS.includes(seconds)) {
522
+ return Promise.reject(new Error(`Hitscan auto-sleep offers ${HITSCAN_XVI_SLEEP_OPTIONS.join(", ")} seconds.`));
523
+ }
524
+ return this.run(async () => {
525
+ const profileId = await this.currentProfile();
526
+ await this.transact(hitscanXviSleepSetRequest(profileId, seconds));
527
+ const confirmed = hitscanXviDecodeSleep(await this.transact(hitscanXviSleepGetRequest(profileId)));
528
+ if (confirmed !== seconds)
529
+ throw new Error(`The Hitscan mouse kept ${confirmed ?? "no"} auto-sleep instead of ${seconds ?? "no"} s.`);
530
+ this.patch({ sleepTimeout: confirmed });
531
+ return confirmed;
532
+ });
533
+ }
534
+ setAngleTuning(degrees) {
535
+ return this.run(async () => {
536
+ const profileId = await this.currentProfile();
537
+ await this.transact(hitscanXviAngleSetRequest(profileId, degrees));
538
+ const confirmed = hitscanXviDecodeAngle(await this.transact(hitscanXviAngleGetRequest(profileId)));
539
+ if (confirmed !== degrees)
540
+ throw new Error("The Hitscan mouse kept a different sensor angle.");
541
+ this.patch({ angleTuning: confirmed });
542
+ return confirmed;
543
+ });
544
+ }
545
+ setMotionSync(enabled) {
546
+ return this.run(async () => {
547
+ const profileId = await this.currentProfile();
548
+ const confirmed = await this.writeFlag(profileId, HITSCAN_XVI_COMMAND.motionSyncSet, enabled, "Motion Sync");
549
+ this.patch({ motionSync: confirmed });
550
+ return confirmed;
551
+ });
552
+ }
553
+ setHyperMode(enabled) {
554
+ return this.run(async () => {
555
+ const profileId = await this.currentProfile();
556
+ const confirmed = await this.writeFlag(profileId, HITSCAN_XVI_COMMAND.hyperModeSet, enabled, "Performance mode");
557
+ this.patch({ hyperMode: confirmed });
558
+ return confirmed;
559
+ });
560
+ }
561
+ setTrackingMode(enabled) {
562
+ return this.run(async () => {
563
+ const profileId = await this.currentProfile();
564
+ const confirmed = await this.writeFlag(profileId, HITSCAN_XVI_COMMAND.trackingModeSet, enabled, "Competitive mode");
565
+ this.patch({ hitscanCompetitiveMode: confirmed });
566
+ return confirmed;
567
+ });
568
+ }
569
+ setScrollHighPerformance(enabled, timeoutMs = 300) {
570
+ return this.run(async () => {
571
+ const profileId = await this.currentProfile();
572
+ await this.transact(hitscanXviScrollHighPerformanceSetRequest(profileId, enabled, timeoutMs));
573
+ const confirmed = hitscanXviDecodeScrollHighPerformance(await this.transact(hitscanXviScrollHighPerformanceGetRequest(profileId)));
574
+ if (confirmed !== enabled)
575
+ throw new Error(`The Hitscan mouse kept tournament scroll ${confirmed ? "on" : "off"}.`);
576
+ this.patch({ hitscanTournamentScroll: confirmed });
577
+ return confirmed;
578
+ });
579
+ }
580
+ setSpdt(left, right) {
581
+ return this.run(async () => {
582
+ const profileId = await this.currentProfile();
583
+ await this.transact(hitscanXviSpdtSetRequest(profileId, left, right));
584
+ const confirmed = hitscanXviDecodeSpdt(await this.transact(hitscanXviSpdtGetRequest(profileId)));
585
+ if (!confirmed || confirmed.left !== left || confirmed.right !== right) {
586
+ throw new Error("The Hitscan mouse kept different SPDT flags.");
587
+ }
588
+ this.patch({ hitscanSpdt: confirmed });
589
+ return confirmed;
590
+ });
591
+ }
592
+ setClickTimings(timings) {
593
+ return this.run(async () => {
594
+ const profileId = await this.currentProfile();
595
+ await this.transact(hitscanXviTimingsSetRequest(profileId, timings));
596
+ const confirmed = hitscanXviDecodeTimings(await this.transact(hitscanXviTimingsGetRequest(profileId)));
597
+ if (!confirmed || Object.keys(timings).some((key) => timings[key] !== confirmed[key])) {
598
+ throw new Error("The Hitscan mouse kept different click timings.");
599
+ }
600
+ this.patch({ debounceMs: confirmed.beforePress, hitscanClickTimings: confirmed });
601
+ return confirmed;
602
+ });
603
+ }
604
+ readButtonBindings() {
605
+ return this.run(async () => {
606
+ const profileId = await this.currentProfile();
607
+ const { buttonMappings, fixedButtons } = await this.readButtonSet(profileId);
608
+ return { mappings: buttonMappings, options: HITSCAN_XVI_BUTTON_LABELS.map(([label]) => label), fixed: fixedButtons };
609
+ });
610
+ }
611
+ setButtonMapping(button, label) {
612
+ return this.run(async () => {
613
+ const index = HITSCAN_XVI_BUTTON_NAMES.indexOf(button);
614
+ if (index < 0)
615
+ throw new Error(`This mouse has no ${button} button.`);
616
+ if (index === 0)
617
+ throw new Error("The left button cannot be remapped.");
618
+ const action = hitscanXviButtonActionForLabel(label);
619
+ if (!action)
620
+ throw new Error(`${label} is not an action this mouse accepts.`);
621
+ const profileId = await this.currentProfile();
622
+ const current = await this.readButton(profileId, HITSCAN_XVI_BUTTON_IDS[index]);
623
+ const currentAction = hitscanXviDecodeButtonAction(current);
624
+ if (hitscanXviIsLeftClick(currentAction)) {
625
+ throw new Error("Keep at least one button as Left Click.");
626
+ }
627
+ const binding = hitscanXviEncodeButtonAction(action);
628
+ await this.transact(hitscanXviButtonSetRequest(profileId, HITSCAN_XVI_BUTTON_IDS[index], binding.functionId, binding.data));
629
+ const confirmed = await this.readButton(profileId, HITSCAN_XVI_BUTTON_IDS[index]);
630
+ const encoded = hitscanXviEncodeButtonAction(action);
631
+ if (confirmed.functionId !== encoded.functionId
632
+ || confirmed.data.length !== encoded.data.length
633
+ || confirmed.data.some((byte, at) => byte !== encoded.data[at])) {
634
+ throw new Error(`The Hitscan mouse kept a different ${button} action.`);
635
+ }
636
+ const mappings = this.lastStatus?.buttonMappings;
637
+ if (mappings)
638
+ this.patch({ buttonMappings: { ...mappings, [button]: label } });
639
+ });
640
+ }
641
+ setActiveProfile(profileId) {
642
+ return this.run(async () => {
643
+ const count = this.lastStatus?.profileCount ?? hitscanXviDecodeProfileCount(await this.transact(hitscanXviProfileListRequest()));
644
+ if (!count || !Number.isInteger(profileId) || profileId < 1 || profileId > count) {
645
+ throw new Error(`This mouse has no profile ${profileId}.`);
646
+ }
647
+ await this.transact(hitscanXviProfileSetRequest(profileId));
648
+ const confirmed = hitscanXviDecodeProfileId(await this.transact(hitscanXviProfileGetRequest()));
649
+ if (confirmed !== profileId)
650
+ throw new Error("The Hitscan mouse kept a different profile selected.");
651
+ this.patch({ activeProfile: profileId });
652
+ return confirmed;
653
+ });
654
+ }
655
+ renameProfile(profileId, name) {
656
+ return this.run(async () => {
657
+ const bytes = hitscanXviEncodeGbkName(name);
658
+ await this.transact(hitscanXviProfileNameSetRequest(profileId, bytes));
659
+ const decoded = hitscanXviDecodeProfileName(await this.transact(hitscanXviProfileNameGetRequest(profileId)));
660
+ if (decoded?.name !== name)
661
+ throw new Error("The Hitscan mouse kept a different profile name.");
662
+ const names = this.lastStatus?.profileNames;
663
+ if (names && profileId <= names.length) {
664
+ const updated = [...names];
665
+ updated[profileId - 1] = name;
666
+ this.patch({ profileNames: updated });
667
+ }
668
+ return decoded.name;
669
+ });
670
+ }
671
+ /** Restores the profile's stored defaults; the caller should reread the status. */
672
+ resetProfile(profileId) {
673
+ return this.run(async () => {
674
+ const id = profileId ?? await this.currentProfile();
675
+ await this.transact(hitscanXviResetProfileRequest(id));
676
+ this.lastStatus = null;
677
+ });
678
+ }
679
+ async readBattery() {
680
+ return this.optional(() => this.transact(hitscanXviBatteryRequest()).then(hitscanXviDecodeBattery));
681
+ }
682
+ /* ---------------------------------------------------------------- macros */
683
+ async readMacros() {
684
+ return this.run(async () => {
685
+ const ids = hitscanXviDecodeMacroIds(await this.transact(hitscanXviMacroListRequest()));
686
+ if (!ids)
687
+ throw new Error("The Hitscan mouse returned a malformed macro list.");
688
+ const macros = [];
689
+ for (const id of ids) {
690
+ const macro = await this.readMacroNow(id);
691
+ if (!macro)
692
+ throw new Error(`Hitscan macro ${id} is listed but unreadable.`);
693
+ macros.push(macro);
694
+ }
695
+ return macros;
696
+ });
697
+ }
698
+ async readMacroNow(id) {
699
+ const size = hitscanXviDecodeMacroSize(await this.transact(hitscanXviMacroSizeRequest(id)));
700
+ if (size === null)
701
+ throw new Error(`Invalid Hitscan macro size for macro ${id}.`);
702
+ if (size === 0)
703
+ return null;
704
+ const nameReply = hitscanXviDecodeMacroName(await this.transact(hitscanXviMacroNameRequest(id)));
705
+ if (!nameReply || nameReply.id !== id)
706
+ throw new Error(`Invalid Hitscan macro name for macro ${id}.`);
707
+ const bytes = [];
708
+ const readOnlyLimit = 4096;
709
+ for (let offset = 0; offset < size && offset < readOnlyLimit; offset += CHUNK_BYTES) {
710
+ const length = Math.min(CHUNK_BYTES, size - offset);
711
+ const chunk = hitscanXviDecodeMacroData(await this.transact(hitscanXviMacroDataRequest(id, offset, length)), id, offset, length);
712
+ if (!chunk)
713
+ throw new Error(`Invalid Hitscan macro data for macro ${id} at ${offset}.`);
714
+ bytes.push(...chunk);
715
+ }
716
+ const stored = Uint8Array.from(bytes);
717
+ const events = size > readOnlyLimit ? null : hitscanXviDecodeMacroEvents(stored);
718
+ return {
719
+ id,
720
+ name: hitscanXviDecodeGbkName(nameReply.bytes),
721
+ events,
722
+ bytes: stored,
723
+ readOnly: size > readOnlyLimit || events === null,
724
+ };
725
+ }
726
+ /** Writes or creates a macro, then verifies bytes and name by reading them back. */
727
+ writeMacro(input) {
728
+ return this.run(async () => {
729
+ const bytes = hitscanXviEncodeMacroEvents(input.events);
730
+ const nameBytes = hitscanXviEncodeGbkName(input.name);
731
+ const ids = hitscanXviDecodeMacroIds(await this.transact(hitscanXviMacroListRequest()));
732
+ if (!ids)
733
+ throw new Error("The Hitscan mouse returned a malformed macro list.");
734
+ let id;
735
+ if (input.id !== undefined) {
736
+ if (!ids.includes(input.id))
737
+ throw new Error(`Hitscan macro ${input.id} does not exist.`);
738
+ id = input.id;
739
+ }
740
+ else {
741
+ const free = Array.from({ length: HITSCAN_XVI_MAX_MACROS }, (_, index) => index + 1)
742
+ .find((slot) => !ids.includes(slot));
743
+ if (free === undefined)
744
+ throw new Error("The Hitscan macro library is full.");
745
+ id = free;
746
+ }
747
+ await this.transact(hitscanXviMacroAllocateRequest(id, bytes.length));
748
+ const size = hitscanXviDecodeMacroSize(await this.transact(hitscanXviMacroSizeRequest(id)));
749
+ if (size !== bytes.length)
750
+ throw new Error("The Hitscan mouse did not allocate the macro size.");
751
+ for (let offset = 0; offset < bytes.length; offset += 51) {
752
+ const chunk = bytes.subarray(offset, offset + 51);
753
+ await this.transact(hitscanXviMacroDataSetRequest(id, offset, chunk));
754
+ }
755
+ await this.transact(hitscanXviMacroNameSetRequest(id, nameBytes));
756
+ const actual = await this.readMacroNow(id);
757
+ if (!actual || actual.bytes.length !== bytes.length || actual.bytes.some((byte, at) => byte !== bytes[at])) {
758
+ throw new Error("The Hitscan mouse did not retain the macro stream.");
759
+ }
760
+ const expectedName = hitscanXviDecodeGbkName(nameBytes);
761
+ if (actual.name !== expectedName)
762
+ throw new Error("The Hitscan mouse did not retain the macro name.");
763
+ return actual;
764
+ });
765
+ }
766
+ deleteMacro(id) {
767
+ return this.run(async () => {
768
+ const profileCount = this.lastStatus?.profileCount ?? hitscanXviDecodeProfileCount(await this.transact(hitscanXviProfileListRequest()));
769
+ if (!profileCount)
770
+ throw new Error("The Hitscan mouse returned no profile list.");
771
+ for (let profileId = 1; profileId <= profileCount; profileId++) {
772
+ const mappings = await this.readButtonSet(profileId);
773
+ if (Object.values(mappings.buttonMappings).some((label) => label === `Macro ${id}`)) {
774
+ throw new Error(`Hitscan macro ${id} is still assigned in profile ${profileId}.`);
775
+ }
776
+ }
777
+ await this.transact(hitscanXviMacroDeleteRequest(id));
778
+ const ids = hitscanXviDecodeMacroIds(await this.transact(hitscanXviMacroListRequest()));
779
+ if (!ids || ids.includes(id))
780
+ throw new Error("The Hitscan mouse did not delete the macro.");
781
+ });
782
+ }
783
+ /* --------------------------------------------------------------- helpers */
784
+ async currentProfile() {
785
+ const profileId = hitscanXviDecodeProfileId(await this.transact(hitscanXviProfileGetRequest()));
786
+ if (!profileId)
787
+ throw new Error("The Hitscan mouse returned an invalid active profile.");
788
+ return profileId;
789
+ }
790
+ }
791
+ /** Shared by every boolean feature: payload[0] echoes the profile, payload[1] is the flag. */
792
+ function hitscanXviDecodeFlagPayload(profileId, reply) {
793
+ const payload = reply.subarray(6);
794
+ if (payload[0] !== profileId)
795
+ return null;
796
+ const value = payload[1];
797
+ return value === 0 || value === 1 ? value === 1 : null;
798
+ }
799
+ /** The package targets ES2022, whose lib has no `Promise.withResolvers`. */
800
+ function hitscanXviDelay(milliseconds) {
801
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
802
+ }
803
+ //# sourceMappingURL=xvi-hid.js.map