@openmouse/protocol 0.49.0 → 0.50.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 (83) hide show
  1. package/README.md +1 -0
  2. package/dist/drivers/atk/hid.d.ts.map +1 -1
  3. package/dist/drivers/atk/hid.js +2 -2
  4. package/dist/drivers/atk/hid.js.map +1 -1
  5. package/dist/drivers/atk/products.d.ts +7 -0
  6. package/dist/drivers/atk/products.d.ts.map +1 -1
  7. package/dist/drivers/atk/products.js +19 -2
  8. package/dist/drivers/atk/products.js.map +1 -1
  9. package/dist/drivers/gwolves/products.d.ts +5 -0
  10. package/dist/drivers/gwolves/products.d.ts.map +1 -1
  11. package/dist/drivers/gwolves/products.js +14 -10
  12. package/dist/drivers/gwolves/products.js.map +1 -1
  13. package/dist/drivers/gwolves/xvi-hid.d.ts +1 -0
  14. package/dist/drivers/gwolves/xvi-hid.d.ts.map +1 -1
  15. package/dist/drivers/gwolves/xvi-hid.js +14 -7
  16. package/dist/drivers/gwolves/xvi-hid.js.map +1 -1
  17. package/dist/drivers/hitscan/xvi-hid.d.ts +110 -0
  18. package/dist/drivers/hitscan/xvi-hid.d.ts.map +1 -0
  19. package/dist/drivers/hitscan/xvi-hid.js +803 -0
  20. package/dist/drivers/hitscan/xvi-hid.js.map +1 -0
  21. package/dist/drivers/motospeed/hid.js +1 -1
  22. package/dist/drivers/motospeed/hid.js.map +1 -1
  23. package/dist/drivers/mouse-types.d.ts +16 -0
  24. package/dist/drivers/mouse-types.d.ts.map +1 -1
  25. package/dist/drivers/razer/hid.d.ts +0 -1
  26. package/dist/drivers/razer/hid.d.ts.map +1 -1
  27. package/dist/drivers/razer/hid.js +14 -9
  28. package/dist/drivers/razer/hid.js.map +1 -1
  29. package/dist/drivers/redragon/hid.d.ts +44 -63
  30. package/dist/drivers/redragon/hid.d.ts.map +1 -1
  31. package/dist/drivers/redragon/hid.js +207 -270
  32. package/dist/drivers/redragon/hid.js.map +1 -1
  33. package/dist/drivers/registry.d.ts.map +1 -1
  34. package/dist/drivers/registry.js +6 -1
  35. package/dist/drivers/registry.js.map +1 -1
  36. package/dist/drivers/vendors.d.ts +1 -1
  37. package/dist/drivers/vendors.d.ts.map +1 -1
  38. package/dist/drivers/vendors.js +6 -4
  39. package/dist/drivers/vendors.js.map +1 -1
  40. package/dist/gearhub/index.d.ts +8 -0
  41. package/dist/gearhub/index.d.ts.map +1 -1
  42. package/dist/gearhub/index.js +18 -0
  43. package/dist/gearhub/index.js.map +1 -1
  44. package/dist/hitscan/index.d.ts +2 -0
  45. package/dist/hitscan/index.d.ts.map +1 -1
  46. package/dist/hitscan/index.js +2 -0
  47. package/dist/hitscan/index.js.map +1 -1
  48. package/dist/hitscan/xvi-inputs.d.ts +81 -0
  49. package/dist/hitscan/xvi-inputs.d.ts.map +1 -0
  50. package/dist/hitscan/xvi-inputs.js +393 -0
  51. package/dist/hitscan/xvi-inputs.js.map +1 -0
  52. package/dist/hitscan/xvi.d.ts +278 -0
  53. package/dist/hitscan/xvi.d.ts.map +1 -0
  54. package/dist/hitscan/xvi.js +773 -0
  55. package/dist/hitscan/xvi.js.map +1 -0
  56. package/dist/keychron/index.d.ts.map +1 -1
  57. package/dist/keychron/index.js +4 -0
  58. package/dist/keychron/index.js.map +1 -1
  59. package/dist/razer/codec.d.ts +3 -2
  60. package/dist/razer/codec.d.ts.map +1 -1
  61. package/dist/razer/codec.js +5 -4
  62. package/dist/razer/codec.js.map +1 -1
  63. package/dist/redragon/deprecated.d.ts +24 -0
  64. package/dist/redragon/deprecated.d.ts.map +1 -0
  65. package/dist/redragon/deprecated.js +54 -0
  66. package/dist/redragon/deprecated.js.map +1 -0
  67. package/dist/redragon/holtek.d.ts +127 -0
  68. package/dist/redragon/holtek.d.ts.map +1 -0
  69. package/dist/redragon/holtek.js +232 -0
  70. package/dist/redragon/holtek.js.map +1 -0
  71. package/dist/redragon/index.d.ts +9 -203
  72. package/dist/redragon/index.d.ts.map +1 -1
  73. package/dist/redragon/index.js +11 -430
  74. package/dist/redragon/index.js.map +1 -1
  75. package/dist/redragon/m612.d.ts +82 -0
  76. package/dist/redragon/m612.d.ts.map +1 -0
  77. package/dist/redragon/m612.js +166 -0
  78. package/dist/redragon/m612.js.map +1 -0
  79. package/dist/redragon/m724.d.ts +47 -0
  80. package/dist/redragon/m724.d.ts.map +1 -0
  81. package/dist/redragon/m724.js +107 -0
  82. package/dist/redragon/m724.js.map +1 -0
  83. package/package.json +1 -1
@@ -0,0 +1,773 @@
1
+ /**
2
+ * Hitscan's web control protocol (control.hitscan.com, release
3
+ * `1.0.15-fe600431`), recovered from the shipped `hitscan-device.js`,
4
+ * `hitscan-input-codec.js`, `hitscan-inputs.js` and `vendor/xvi-20260924.min.js`.
5
+ *
6
+ * This is a different generation from the native `Hitscan Utility 1.0.2`
7
+ * protocol in `./index.ts`: the web app talks to vendor id `0x3770` over
8
+ * 64-byte feature report 0 with an `0xa1` status byte, while the native utility
9
+ * uses report 8 output/input transfers and checksummed flash records. The two
10
+ * share only the vendor id.
11
+ *
12
+ * Request layout (64 bytes, feature report id 0):
13
+ * `[0, 0, device, length, class, command, data...]`
14
+ * Reply layout after `hitscanXviNormalize` (some platforms prepend report id 0):
15
+ * `[0xa1, 0, 0, length, class, command, payload...]`
16
+ * `0xa1` is success, `0x02` means the mouse is asleep, `0x00` is not a status.
17
+ *
18
+ * The payload starts at byte 6 and usually echoes the request's first data
19
+ * byte (the profile) before the value. Each decoder below documents the exact
20
+ * offsets established by the vendor reply parsers. Nothing here has been
21
+ * exercised on hardware; ranges and encodings are the vendor app's own.
22
+ */
23
+ import { hitscanXviDecodeGbkName } from "./xvi-inputs.js";
24
+ /** Hit scan devices that speak the web protocol. `0x0300` is the shared dongle. */
25
+ export const HITSCAN_XVI_VENDOR_ID = 0x3770;
26
+ export const HITSCAN_XVI_PRODUCTS = new Map([
27
+ [0x0136, { model: "Hyperlight X", transport: "wired" }],
28
+ [0x0194, { model: "Hyperlight N", transport: "wired" }],
29
+ // The receiver answers for whichever mouse is paired; the mouse id comes
30
+ // from the `GetDevicePID` reply.
31
+ [0x0300, { model: null, transport: "receiver" }],
32
+ ]);
33
+ export const HITSCAN_XVI_REPORT_ID = 0;
34
+ export const HITSCAN_XVI_PACKET_LENGTH = 64;
35
+ /** Mouse device id used in request byte 2 (`sdk.wiredMouseDeviceID`). */
36
+ export const HITSCAN_XVI_DEVICE_ID = 2;
37
+ /** The dongle answers its own firmware query with device id 0. */
38
+ export const HITSCAN_XVI_DONGLE_DEVICE_ID = 0;
39
+ export const HITSCAN_XVI_STATUS_OK = 0xa1;
40
+ export const HITSCAN_XVI_STATUS_ASLEEP = 0x02;
41
+ export const HITSCAN_XVI_NOTIFICATION_REPORT_ID = 4;
42
+ export const HITSCAN_XVI_MAX_PROFILES = 10;
43
+ export const HITSCAN_XVI_MAX_DPI_STAGES = 4;
44
+ /** The vendor reads and writes six RGB slots even on four-stage mice. */
45
+ export const HITSCAN_XVI_MAX_DPI_COLORS = 6;
46
+ export const HITSCAN_XVI_DPI_MIN = 50;
47
+ export const HITSCAN_XVI_DPI_MAX = 30_000;
48
+ export const HITSCAN_XVI_DPI_STEP = 50;
49
+ export const HITSCAN_XVI_MAX_MACROS = 20;
50
+ export const HITSCAN_XVI_MAX_MACRO_BYTES = 4096;
51
+ export const HITSCAN_XVI_TIMING_MAX_MS = 50;
52
+ export const HITSCAN_XVI_ANGLE_LIMIT = 30;
53
+ /** `RATE_IDS` in hitscan-device.js. 16 is also read back as 1 kHz. */
54
+ export const HITSCAN_XVI_POLLING_CODES = new Map([
55
+ [125, 8], [250, 4], [500, 2], [1000, 1], [2000, 32], [4000, 64], [8000, 128],
56
+ ]);
57
+ /** The app's polling picker offers these on both the cable and the receiver. */
58
+ export const HITSCAN_XVI_POLLING_RATES = [500, 1000, 2000, 4000, 8000];
59
+ export const HITSCAN_XVI_LIFT_OFF_MILLIMETRES = [0.7, 1, 2];
60
+ /** Auto-sleep picker in the web app, seconds. 0/0xff00/0xffff read as "never". */
61
+ export const HITSCAN_XVI_SLEEP_OPTIONS = [10, 30, 60, 300, 600];
62
+ export const HITSCAN_XVI_SLEEP_NEVER = 0xffff;
63
+ /** Request classes (`request[4]`). */
64
+ export const HITSCAN_XVI_CLASS = {
65
+ config: 0,
66
+ sensor: 1,
67
+ color: 2,
68
+ button: 3,
69
+ macro: 4,
70
+ };
71
+ /** Command bytes (`request[5]`). A value may repeat across classes. */
72
+ export const HITSCAN_XVI_COMMAND = {
73
+ profileSet: 0x05,
74
+ profileGet: 0x85,
75
+ profileList: 0x86,
76
+ profileNameSet: 0x0e,
77
+ profileNameGet: 0x8e,
78
+ resetProfile: 0x0d,
79
+ firmware: 0x81,
80
+ battery: 0x83,
81
+ signal: 0xa7,
82
+ devicePid: 0x8b,
83
+ pollingSet: 0x00,
84
+ pollingGet: 0x80,
85
+ sleepSet: 0x07,
86
+ sleepGet: 0x87,
87
+ liftOffSet: 0x08,
88
+ liftOffGet: 0x88,
89
+ dpiStagesSet: 0x01,
90
+ dpiStagesGet: 0x81,
91
+ dpiStageSet: 0x02,
92
+ dpiStageGet: 0x82,
93
+ dpiMax: 0x8c,
94
+ colorsSet: 0x01,
95
+ colorsGet: 0x81,
96
+ splitDpiSet: 0x0d,
97
+ splitDpiGet: 0x8d,
98
+ angleSet: 0x14,
99
+ angleGet: 0x94,
100
+ motionSyncSet: 0x09,
101
+ motionSyncGet: 0x89,
102
+ hyperModeSet: 0x0b,
103
+ hyperModeGet: 0x8b,
104
+ trackingModeSet: 0x13,
105
+ trackingModeGet: 0x93,
106
+ spdtSet: 0x03,
107
+ spdtGet: 0x83,
108
+ scrollHighPerformanceSet: 0x19,
109
+ scrollHighPerformanceGet: 0x99,
110
+ timingsSet: 0x08,
111
+ timingsGet: 0x88,
112
+ buttonSet: 0x00,
113
+ buttonGet: 0x80,
114
+ macroAllocate: 0x01,
115
+ macroSize: 0x81,
116
+ macroDataSet: 0x03,
117
+ macroDataGet: 0x83,
118
+ macroNameSet: 0x06,
119
+ macroNameGet: 0x86,
120
+ macroList: 0x85,
121
+ macroDelete: 0x02,
122
+ };
123
+ function integer(value, min, max, label) {
124
+ if (!Number.isInteger(value) || value < min || value > max)
125
+ throw new RangeError(`Invalid Hitscan ${label}: ${value}.`);
126
+ }
127
+ function checkedBytes(data) {
128
+ const bytes = Uint8Array.from(data);
129
+ for (const byte of bytes)
130
+ integer(byte, 0, 255, "byte");
131
+ return bytes;
132
+ }
133
+ function profile(value) {
134
+ integer(value, 1, HITSCAN_XVI_MAX_PROFILES, "profile");
135
+ }
136
+ function rate(value) {
137
+ integer(value, 0, 255, "rate code");
138
+ }
139
+ /** `[0, 0, device, length, class, command, data...]`, zero padded to 64 bytes. */
140
+ export function hitscanXviFrame(length, commandClass, command, data = [], deviceId = HITSCAN_XVI_DEVICE_ID) {
141
+ integer(length, 0, HITSCAN_XVI_PACKET_LENGTH - 2, "length");
142
+ integer(commandClass, 0, 255, "class");
143
+ integer(command, 0, 255, "command");
144
+ integer(deviceId, 0, 255, "device id");
145
+ const payload = checkedBytes(data);
146
+ if (payload.length > HITSCAN_XVI_PACKET_LENGTH - 6)
147
+ throw new RangeError("Hitscan request payload exceeds 58 bytes.");
148
+ const packet = new Uint8Array(HITSCAN_XVI_PACKET_LENGTH);
149
+ packet.set([deviceId, length, commandClass, command, ...payload], 2);
150
+ return packet;
151
+ }
152
+ /** Drops a platform-prepended report-id byte so reply[0] is the status. */
153
+ export function hitscanXviNormalize(reply) {
154
+ const bytes = checkedBytes(reply);
155
+ return bytes.length > HITSCAN_XVI_PACKET_LENGTH || bytes[0] === HITSCAN_XVI_REPORT_ID
156
+ ? bytes.slice(1)
157
+ : bytes;
158
+ }
159
+ /**
160
+ * Normalizes and validates a reply against the request's class and command.
161
+ * Returns null for stale, malformed or mismatched replies; the caller decides
162
+ * whether an `0x02` status means "asleep".
163
+ */
164
+ export function hitscanXviReply(reply, request) {
165
+ const bytes = hitscanXviNormalize(reply);
166
+ if (bytes.length < 6)
167
+ return null;
168
+ if (bytes[0] !== HITSCAN_XVI_STATUS_OK && bytes[0] !== HITSCAN_XVI_STATUS_ASLEEP)
169
+ return null;
170
+ if (request) {
171
+ const sent = checkedBytes(request);
172
+ if (bytes[4] !== sent[4] || bytes[5] !== sent[5])
173
+ return null;
174
+ }
175
+ return bytes;
176
+ }
177
+ /** Valid `0xa1` reply for `class`/`command`, or null. */
178
+ export function hitscanXviDecode(reply, commandClass, command) {
179
+ const bytes = hitscanXviReply(reply);
180
+ if (!bytes || bytes[0] !== HITSCAN_XVI_STATUS_OK)
181
+ return null;
182
+ if (bytes[4] !== commandClass || bytes[5] !== command)
183
+ return null;
184
+ return bytes.subarray(6);
185
+ }
186
+ /** Signed angle byte to degrees. */
187
+ export function hitscanXviDecodeAngleByte(value) {
188
+ integer(value, 0, 255, "angle byte");
189
+ return value > 127 ? value - 256 : value;
190
+ }
191
+ export function hitscanXviEncodeAngle(degrees) {
192
+ integer(degrees, -HITSCAN_XVI_ANGLE_LIMIT, HITSCAN_XVI_ANGLE_LIMIT, "angle");
193
+ return degrees < 0 ? 256 + degrees : degrees;
194
+ }
195
+ /** Lift-off distance: fractional millimetres use `value | 0x80` with tenths. */
196
+ export function hitscanXviEncodeLiftOff(millimetres) {
197
+ if (!HITSCAN_XVI_LIFT_OFF_MILLIMETRES.includes(millimetres)) {
198
+ throw new RangeError("Hitscan lift-off distance must be 0.7, 1 or 2 mm.");
199
+ }
200
+ return millimetres >= 1 ? millimetres : Math.round(millimetres * 10) | 0x80;
201
+ }
202
+ export function hitscanXviDecodeLiftOffByte(value) {
203
+ if (!Number.isInteger(value) || value < 0 || value > 255)
204
+ return null;
205
+ const millimetres = (value & 0x80) !== 0 ? (value & 0x7f) / 10 : value;
206
+ return HITSCAN_XVI_LIFT_OFF_MILLIMETRES.includes(millimetres) ? millimetres : null;
207
+ }
208
+ export function hitscanXviEncodePollingRate(hz) {
209
+ const code = HITSCAN_XVI_POLLING_CODES.get(hz);
210
+ if (code === undefined)
211
+ throw new RangeError(`Unsupported Hitscan polling rate: ${hz} Hz.`);
212
+ return code;
213
+ }
214
+ /** `16` is the receiver's alternate 1 kHz code, exactly as the vendor app maps it. */
215
+ export function hitscanXviDecodePollingRateCode(code) {
216
+ if (code === 16)
217
+ return 1000;
218
+ return [...HITSCAN_XVI_POLLING_CODES].find(([, value]) => value === code)?.[0] ?? null;
219
+ }
220
+ function word(high, low) {
221
+ return (high << 8) | low;
222
+ }
223
+ function splitWord(value) {
224
+ return [value >> 8, value & 0xff];
225
+ }
226
+ function offsetWord(bytes, at) {
227
+ return word(bytes[at] ?? 0, bytes[at + 1] ?? 0);
228
+ }
229
+ /* ------------------------------------------------------------------ reads */
230
+ export function hitscanXviFirmwareRequest(deviceId = HITSCAN_XVI_DEVICE_ID) {
231
+ return hitscanXviFrame(16, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.firmware, [], deviceId);
232
+ }
233
+ export function hitscanXviBatteryRequest() {
234
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.battery);
235
+ }
236
+ export function hitscanXviSignalRequest() {
237
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.signal);
238
+ }
239
+ /** Device id 1 and a trailing `2`: the vendor's `GetDevicePID` constructor. */
240
+ export function hitscanXviDevicePidRequest() {
241
+ return hitscanXviFrame(6, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.devicePid, [2], 1);
242
+ }
243
+ export function hitscanXviProfileListRequest() {
244
+ return hitscanXviFrame(1, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileList);
245
+ }
246
+ export function hitscanXviProfileGetRequest() {
247
+ return hitscanXviFrame(1, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileGet);
248
+ }
249
+ export function hitscanXviProfileSetRequest(profileId) {
250
+ profile(profileId);
251
+ return hitscanXviFrame(1, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileSet, [profileId]);
252
+ }
253
+ export function hitscanXviProfileNameGetRequest(profileId) {
254
+ profile(profileId);
255
+ return hitscanXviFrame(58, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileNameGet, [profileId]);
256
+ }
257
+ export function hitscanXviProfileNameSetRequest(profileId, nameBytes) {
258
+ profile(profileId);
259
+ const bytes = checkedBytes(nameBytes);
260
+ if (bytes.length > 56)
261
+ throw new RangeError("Hitscan profile names are at most 56 encoded bytes.");
262
+ return hitscanXviFrame(bytes.length + 2, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileNameSet, [
263
+ profileId, bytes.length, ...bytes,
264
+ ]);
265
+ }
266
+ export function hitscanXviResetProfileRequest(profileId) {
267
+ profile(profileId);
268
+ return hitscanXviFrame(1, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.resetProfile, [profileId]);
269
+ }
270
+ export function hitscanXviPollingRateGetRequest(profileId) {
271
+ profile(profileId);
272
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.pollingGet, [profileId]);
273
+ }
274
+ export function hitscanXviPollingRateSetRequest(profileId, hz) {
275
+ profile(profileId);
276
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.pollingSet, [
277
+ profileId, hitscanXviEncodePollingRate(hz),
278
+ ]);
279
+ }
280
+ export function hitscanXviSleepGetRequest(profileId) {
281
+ profile(profileId);
282
+ return hitscanXviFrame(3, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.sleepGet, [profileId]);
283
+ }
284
+ /** `seconds === null` writes the "never" sentinel. */
285
+ export function hitscanXviSleepSetRequest(profileId, seconds) {
286
+ profile(profileId);
287
+ if (seconds !== null)
288
+ integer(seconds, 0, 0xfffe, "sleep seconds");
289
+ const value = seconds === null ? HITSCAN_XVI_SLEEP_NEVER : seconds;
290
+ return hitscanXviFrame(3, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.sleepSet, [
291
+ profileId, ...splitWord(value),
292
+ ]);
293
+ }
294
+ export function hitscanXviLiftOffGetRequest(profileId) {
295
+ profile(profileId);
296
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.liftOffGet, [profileId]);
297
+ }
298
+ export function hitscanXviLiftOffSetRequest(profileId, millimetres) {
299
+ profile(profileId);
300
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.liftOffSet, [
301
+ profileId, hitscanXviEncodeLiftOff(millimetres),
302
+ ]);
303
+ }
304
+ function dpiValue(value) {
305
+ integer(value, HITSCAN_XVI_DPI_MIN, HITSCAN_XVI_DPI_MAX, "DPI");
306
+ if (value % HITSCAN_XVI_DPI_STEP !== 0)
307
+ throw new RangeError("Hitscan DPI must use 50-DPI steps.");
308
+ }
309
+ export function hitscanXviDpiStagesGetRequest(profileId, capacity = 6) {
310
+ profile(profileId);
311
+ integer(capacity, 1, 6, "DPI stage capacity");
312
+ return hitscanXviFrame(10, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.dpiStagesGet, [profileId, capacity]);
313
+ }
314
+ /** `[2, 26, 1, 1, profile, count, x/y pairs...]`; the length byte stays 26. */
315
+ export function hitscanXviDpiStagesSetRequest(profileId, stages) {
316
+ profile(profileId);
317
+ if (!Array.isArray(stages) || stages.length < 1 || stages.length > HITSCAN_XVI_MAX_DPI_STAGES) {
318
+ throw new RangeError(`Hitscan takes 1-${HITSCAN_XVI_MAX_DPI_STAGES} DPI stages.`);
319
+ }
320
+ const data = [profileId, stages.length];
321
+ for (const stage of stages) {
322
+ dpiValue(stage.x);
323
+ dpiValue(stage.y);
324
+ data.push(...splitWord(stage.x), ...splitWord(stage.y));
325
+ }
326
+ return hitscanXviFrame(26, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.dpiStagesSet, data);
327
+ }
328
+ export function hitscanXviActiveStageGetRequest(profileId) {
329
+ profile(profileId);
330
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.dpiStageGet, [profileId]);
331
+ }
332
+ /** `stage` is 1-based, matching the firmware. */
333
+ export function hitscanXviActiveStageSetRequest(profileId, stage) {
334
+ profile(profileId);
335
+ integer(stage, 1, HITSCAN_XVI_MAX_DPI_STAGES, "DPI stage");
336
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.dpiStageSet, [profileId, stage]);
337
+ }
338
+ export function hitscanXviDpiMaxRequest() {
339
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.dpiMax);
340
+ }
341
+ export function hitscanXviDpiColorsGetRequest(profileId) {
342
+ profile(profileId);
343
+ return hitscanXviFrame(19, HITSCAN_XVI_CLASS.color, HITSCAN_XVI_COMMAND.colorsGet, [profileId]);
344
+ }
345
+ /** `rgb` is 18 bytes: six RGB triplets, one per stage slot. */
346
+ export function hitscanXviDpiColorsSetRequest(profileId, rgb) {
347
+ profile(profileId);
348
+ const bytes = checkedBytes(rgb);
349
+ if (bytes.length !== HITSCAN_XVI_MAX_DPI_COLORS * 3) {
350
+ throw new RangeError("Hitscan stage colours are six RGB triplets (18 bytes).");
351
+ }
352
+ return hitscanXviFrame(19, HITSCAN_XVI_CLASS.color, HITSCAN_XVI_COMMAND.colorsSet, [profileId, ...bytes]);
353
+ }
354
+ export function hitscanXviSplitDpiGetRequest(profileId) {
355
+ profile(profileId);
356
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.splitDpiGet, [profileId]);
357
+ }
358
+ export function hitscanXviSplitDpiSetRequest(profileId, enabled) {
359
+ profile(profileId);
360
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.splitDpiSet, [profileId, enabled ? 1 : 0]);
361
+ }
362
+ export function hitscanXviAngleGetRequest(profileId) {
363
+ profile(profileId);
364
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.angleGet, [profileId]);
365
+ }
366
+ export function hitscanXviAngleSetRequest(profileId, degrees) {
367
+ profile(profileId);
368
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.angleSet, [
369
+ profileId, hitscanXviEncodeAngle(degrees),
370
+ ]);
371
+ }
372
+ export function hitscanXviFlagGetRequest(profileId, command) {
373
+ profile(profileId);
374
+ rate(command);
375
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, command, [profileId]);
376
+ }
377
+ export function hitscanXviFlagSetRequest(profileId, command, enabled) {
378
+ profile(profileId);
379
+ rate(command);
380
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.sensor, command, [profileId, enabled ? 1 : 0]);
381
+ }
382
+ export function hitscanXviSpdtGetRequest(profileId) {
383
+ profile(profileId);
384
+ return hitscanXviFrame(3, HITSCAN_XVI_CLASS.button, HITSCAN_XVI_COMMAND.spdtGet, [profileId]);
385
+ }
386
+ /** The vendor SDK duplicates one flag; Hitscan's own adapter writes both. */
387
+ export function hitscanXviSpdtSetRequest(profileId, left, right) {
388
+ profile(profileId);
389
+ return hitscanXviFrame(3, HITSCAN_XVI_CLASS.button, HITSCAN_XVI_COMMAND.spdtSet, [
390
+ profileId, left ? 1 : 0, right ? 1 : 0,
391
+ ]);
392
+ }
393
+ export function hitscanXviScrollHighPerformanceGetRequest(profileId) {
394
+ profile(profileId);
395
+ return hitscanXviFrame(4, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.scrollHighPerformanceGet, [profileId]);
396
+ }
397
+ /** `timeoutMs` is the vendor UI's 300 ms constant. */
398
+ export function hitscanXviScrollHighPerformanceSetRequest(profileId, enabled, timeoutMs = 300) {
399
+ profile(profileId);
400
+ integer(timeoutMs, 0, 0xffff, "scroll timeout");
401
+ return hitscanXviFrame(4, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.scrollHighPerformanceSet, [
402
+ profileId, enabled ? 1 : 0, ...splitWord(timeoutMs),
403
+ ]);
404
+ }
405
+ const TIMING_KEYS = ["beforePress", "beforeRelease", "afterPress", "afterRelease"];
406
+ export function hitscanXviTimingsGetRequest(profileId) {
407
+ profile(profileId);
408
+ return hitscanXviFrame(6, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.timingsGet, [profileId, 0, 0, 0, 0, 0]);
409
+ }
410
+ /** Five payload bytes: four timings plus the vendor's trailing `3`. */
411
+ export function hitscanXviTimingsSetRequest(profileId, timings) {
412
+ profile(profileId);
413
+ const values = TIMING_KEYS.map((key) => {
414
+ const value = timings[key];
415
+ integer(value, 0, HITSCAN_XVI_TIMING_MAX_MS, key);
416
+ return value;
417
+ });
418
+ return hitscanXviFrame(6, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.timingsSet, [profileId, ...values, 3]);
419
+ }
420
+ /** Physical button record ids: Left, Right, Middle, Forward, Back, DPI. */
421
+ export const HITSCAN_XVI_BUTTON_IDS = [1, 2, 3, 5, 4, 0x14];
422
+ export const HITSCAN_XVI_BUTTON_NAMES = ["Left", "Right", "Middle", "Forward", "Back", "DPI"];
423
+ export function hitscanXviButtonGetRequest(profileId, buttonId, length = 10) {
424
+ profile(profileId);
425
+ integer(buttonId, 0, 0x14, "button id");
426
+ integer(length, 0, 53, "button data length");
427
+ return hitscanXviFrame(5 + length, HITSCAN_XVI_CLASS.button, HITSCAN_XVI_COMMAND.buttonGet, [
428
+ profileId, buttonId, 0, 0xff, length,
429
+ ]);
430
+ }
431
+ export function hitscanXviButtonSetRequest(profileId, buttonId, functionId, data) {
432
+ profile(profileId);
433
+ integer(buttonId, 0, 0x14, "button id");
434
+ integer(functionId, 0, 255, "button function");
435
+ const payload = checkedBytes(data);
436
+ if (payload.length > 53)
437
+ throw new RangeError("Hitscan button data exceeds 53 bytes.");
438
+ return hitscanXviFrame(5 + payload.length, HITSCAN_XVI_CLASS.button, HITSCAN_XVI_COMMAND.buttonSet, [
439
+ profileId, buttonId, 0, functionId, payload.length, ...payload,
440
+ ]);
441
+ }
442
+ /* ----------------------------------------------------------------- macros */
443
+ export function hitscanXviMacroListRequest(capacity = HITSCAN_XVI_MAX_MACROS) {
444
+ integer(capacity, 0, HITSCAN_XVI_MAX_MACROS, "macro capacity");
445
+ return hitscanXviFrame(8, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroList, splitWord(capacity));
446
+ }
447
+ export function hitscanXviMacroSizeRequest(macroId) {
448
+ integer(macroId, 1, 0xffff, "macro id");
449
+ return hitscanXviFrame(6, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroSize, splitWord(macroId));
450
+ }
451
+ export function hitscanXviMacroAllocateRequest(macroId, size) {
452
+ integer(macroId, 1, 0xffff, "macro id");
453
+ integer(size, 0, 0xffffffff, "macro size");
454
+ return hitscanXviFrame(6, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroAllocate, [
455
+ ...splitWord(macroId), (size >>> 24) & 0xff, (size >>> 16) & 0xff, (size >>> 8) & 0xff, size & 0xff,
456
+ ]);
457
+ }
458
+ export function hitscanXviMacroDataSetRequest(macroId, offset, data) {
459
+ integer(macroId, 1, 0xffff, "macro id");
460
+ integer(offset, 0, 0xffffffff, "macro offset");
461
+ const bytes = checkedBytes(data);
462
+ if (bytes.length > 51)
463
+ throw new RangeError("Hitscan macro chunks are at most 51 bytes.");
464
+ return hitscanXviFrame(7 + bytes.length, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroDataSet, [
465
+ ...splitWord(macroId),
466
+ (offset >>> 24) & 0xff, (offset >>> 16) & 0xff, (offset >>> 8) & 0xff, offset & 0xff,
467
+ bytes.length, ...bytes,
468
+ ]);
469
+ }
470
+ export function hitscanXviMacroDataRequest(macroId, offset, length) {
471
+ integer(macroId, 1, 0xffff, "macro id");
472
+ integer(offset, 0, 0xffffffff, "macro offset");
473
+ integer(length, 1, 50, "macro chunk length");
474
+ return hitscanXviFrame(length + 7, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroDataGet, [
475
+ ...splitWord(macroId),
476
+ (offset >>> 24) & 0xff, (offset >>> 16) & 0xff, (offset >>> 8) & 0xff, offset & 0xff,
477
+ length,
478
+ ]);
479
+ }
480
+ export function hitscanXviMacroNameRequest(macroId) {
481
+ integer(macroId, 1, 0xffff, "macro id");
482
+ return hitscanXviFrame(6, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroNameGet, [macroId, 56]);
483
+ }
484
+ export function hitscanXviMacroNameSetRequest(macroId, nameBytes) {
485
+ integer(macroId, 1, 0xffff, "macro id");
486
+ const bytes = checkedBytes(nameBytes);
487
+ if (bytes.length > 56)
488
+ throw new RangeError("Hitscan macro names are at most 56 encoded bytes.");
489
+ return hitscanXviFrame(bytes.length + 2, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroNameSet, [
490
+ macroId, bytes.length, ...bytes,
491
+ ]);
492
+ }
493
+ export function hitscanXviMacroDeleteRequest(macroId) {
494
+ integer(macroId, 1, HITSCAN_XVI_MAX_MACROS, "macro id");
495
+ return hitscanXviFrame(2, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroDelete, splitWord(macroId));
496
+ }
497
+ /* ---------------------------------------------------------------- decoders */
498
+ export function hitscanXviDecodeFirmware(reply) {
499
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.firmware);
500
+ if (!payload || payload.length < 4)
501
+ return null;
502
+ const version = `${payload[0]}.${payload[1]}.${payload[2]}.${payload[3]}`;
503
+ return version === "0.0.0.0" ? null : version;
504
+ }
505
+ /** Payload byte 0 is the charging flag, byte 1 the percentage. */
506
+ export function hitscanXviDecodeBattery(reply) {
507
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.battery);
508
+ if (!payload || payload.length < 2)
509
+ return null;
510
+ const charging = payload[0];
511
+ const percent = payload[1];
512
+ if ((charging !== 0 && charging !== 1) || percent > 100)
513
+ return null;
514
+ return { percent, charging: charging === 1 };
515
+ }
516
+ /** Supplier levels are 1 = strong, 2 = medium, 3 = weak, 0 = unavailable. */
517
+ export function hitscanXviDecodeSignal(reply) {
518
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.signal);
519
+ if (!payload || payload.length < 2)
520
+ return null;
521
+ const level = payload[0];
522
+ if (level === undefined || level > 3)
523
+ return null;
524
+ return { level: level, rssi: payload[1] };
525
+ }
526
+ /** PID at payload[4..5], VID at payload[2..3]. */
527
+ export function hitscanXviDecodeDevicePid(reply) {
528
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.devicePid);
529
+ if (!payload || payload.length < 6)
530
+ return null;
531
+ const pid = offsetWord(payload, 4);
532
+ return pid === 0 ? null : pid;
533
+ }
534
+ export function hitscanXviDecodeProfileCount(reply) {
535
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileList);
536
+ const count = payload?.[0];
537
+ return count !== undefined && count >= 1 && count <= HITSCAN_XVI_MAX_PROFILES ? count : null;
538
+ }
539
+ export function hitscanXviDecodeProfileId(reply) {
540
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileGet);
541
+ const id = payload?.[0];
542
+ return id !== undefined && id >= 1 && id <= HITSCAN_XVI_MAX_PROFILES ? id : null;
543
+ }
544
+ /** Payload[0] is the profile echo, payload[1] the name length, then GBK bytes. */
545
+ export function hitscanXviDecodeProfileName(reply) {
546
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.profileNameGet);
547
+ if (!payload || payload.length < 2)
548
+ return null;
549
+ const id = payload[0];
550
+ const length = payload[1];
551
+ if (id === undefined || id < 1 || id > HITSCAN_XVI_MAX_PROFILES)
552
+ return null;
553
+ if (length === undefined || length > 56 || payload.length < 2 + length)
554
+ return null;
555
+ return { id, name: hitscanXviDecodeGbkName(payload.subarray(2, 2 + length)) };
556
+ }
557
+ export function hitscanXviDecodePollingRate(reply) {
558
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.pollingGet);
559
+ const code = payload?.[1];
560
+ return code === undefined ? null : hitscanXviDecodePollingRateCode(code);
561
+ }
562
+ export function hitscanXviDecodeSleep(reply) {
563
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.sleepGet);
564
+ if (!payload || payload.length < 3)
565
+ return null;
566
+ const seconds = offsetWord(payload, 1);
567
+ return seconds === 0 || seconds === 0xff00 || seconds === 0xffff ? null : seconds;
568
+ }
569
+ export function hitscanXviDecodeLiftOff(reply) {
570
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.liftOffGet);
571
+ const value = payload?.[1];
572
+ return value === undefined ? null : hitscanXviDecodeLiftOffByte(value);
573
+ }
574
+ /**
575
+ * Payload[1] is the stage count, then four bytes per stage from payload[2].
576
+ * The vendor reads and writes X and Y independently.
577
+ */
578
+ export function hitscanXviDecodeDpiStages(reply) {
579
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.dpiStagesGet);
580
+ if (!payload || payload.length < 2)
581
+ return null;
582
+ const count = payload[1];
583
+ if (count === undefined || count < 1 || count > 6 || payload.length < 2 + count * 4)
584
+ return null;
585
+ const stages = [];
586
+ for (let index = 0; index < count; index++) {
587
+ const x = offsetWord(payload, 2 + index * 4);
588
+ const y = offsetWord(payload, 4 + index * 4);
589
+ if (x < HITSCAN_XVI_DPI_MIN || x > HITSCAN_XVI_DPI_MAX || y < HITSCAN_XVI_DPI_MIN || y > HITSCAN_XVI_DPI_MAX)
590
+ return null;
591
+ stages.push({ x, y });
592
+ }
593
+ return { stages };
594
+ }
595
+ export function hitscanXviDecodeActiveStage(reply) {
596
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.dpiStageGet);
597
+ const stage = payload?.[1];
598
+ return stage !== undefined && stage >= 1 && stage <= 6 ? stage : null;
599
+ }
600
+ export function hitscanXviDecodeDpiColors(reply) {
601
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.color, HITSCAN_XVI_COMMAND.colorsGet);
602
+ if (!payload || payload.length < 1 + HITSCAN_XVI_MAX_DPI_COLORS * 3)
603
+ return null;
604
+ return Array.from({ length: HITSCAN_XVI_MAX_DPI_COLORS }, (_, index) => {
605
+ const at = 1 + index * 3;
606
+ return `#${[0, 1, 2].map((channel) => (payload[at + channel] ?? 0).toString(16).padStart(2, "0")).join("")}`;
607
+ });
608
+ }
609
+ export function hitscanXviDecodeSplitDpi(reply) {
610
+ return hitscanXviDecodeFlag(reply, HITSCAN_XVI_COMMAND.splitDpiGet);
611
+ }
612
+ export function hitscanXviDecodeAngle(reply) {
613
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.sensor, HITSCAN_XVI_COMMAND.angleGet);
614
+ const value = payload?.[1];
615
+ if (value === undefined)
616
+ return null;
617
+ const degrees = hitscanXviDecodeAngleByte(value);
618
+ return Math.abs(degrees) <= HITSCAN_XVI_ANGLE_LIMIT ? degrees : null;
619
+ }
620
+ export function hitscanXviDecodeFlag(reply, command) {
621
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.sensor, command);
622
+ const value = payload?.[1];
623
+ return value === 0 || value === 1 ? value === 1 : null;
624
+ }
625
+ export function hitscanXviDecodeSpdt(reply) {
626
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.button, HITSCAN_XVI_COMMAND.spdtGet);
627
+ if (!payload || payload.length < 3)
628
+ return null;
629
+ const left = payload[1];
630
+ const right = payload[2];
631
+ return left === 0 || left === 1 ? (right === 0 || right === 1 ? { left: left === 1, right: right === 1 } : null) : null;
632
+ }
633
+ export function hitscanXviDecodeScrollHighPerformance(reply) {
634
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.scrollHighPerformanceGet);
635
+ const value = payload?.[1];
636
+ return value === 0 || value === 1 ? value === 1 : null;
637
+ }
638
+ export function hitscanXviDecodeTimings(reply) {
639
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.config, HITSCAN_XVI_COMMAND.timingsGet);
640
+ if (!payload || payload.length < 5)
641
+ return null;
642
+ const values = TIMING_KEYS.map((_key, index) => payload[1 + index]);
643
+ if (values.some((value) => value === undefined || value > HITSCAN_XVI_TIMING_MAX_MS))
644
+ return null;
645
+ return {
646
+ beforePress: values[0],
647
+ beforeRelease: values[1],
648
+ afterPress: values[2],
649
+ afterRelease: values[3],
650
+ };
651
+ }
652
+ /** Payload: `[profile, button, 0, function, length, data...]`. */
653
+ export function hitscanXviDecodeButton(reply) {
654
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.button, HITSCAN_XVI_COMMAND.buttonGet);
655
+ if (!payload || payload.length < 5)
656
+ return null;
657
+ const profileId = payload[0];
658
+ const buttonId = payload[1];
659
+ const functionId = payload[3];
660
+ const length = payload[4];
661
+ if (profileId === undefined || profileId < 1 || profileId > HITSCAN_XVI_MAX_PROFILES)
662
+ return null;
663
+ if (buttonId === undefined || functionId === undefined || length === undefined)
664
+ return null;
665
+ if (length > 53 || payload.length < 5 + length)
666
+ return null;
667
+ return { profileId, buttonId, functionId, data: Array.from(payload.subarray(5, 5 + length)) };
668
+ }
669
+ /** Payload[0..1] is the returned count, payload[4..] the ids. */
670
+ export function hitscanXviDecodeMacroIds(reply) {
671
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroList);
672
+ if (!payload || payload.length < 4)
673
+ return null;
674
+ const count = offsetWord(payload, 0);
675
+ if (count > HITSCAN_XVI_MAX_MACROS || payload.length < 4 + count * 2)
676
+ return null;
677
+ const ids = [];
678
+ for (let index = 0; index < count; index++) {
679
+ const id = offsetWord(payload, 4 + index * 2);
680
+ if (id > HITSCAN_XVI_MAX_MACROS)
681
+ return null;
682
+ if (id !== 0 && !ids.includes(id))
683
+ ids.push(id);
684
+ }
685
+ return ids;
686
+ }
687
+ /** Payload[2..5] is a big-endian 32-bit byte count. */
688
+ export function hitscanXviDecodeMacroSize(reply) {
689
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroSize);
690
+ if (!payload || payload.length < 6)
691
+ return null;
692
+ const size = (payload[2] * 0x1000000) + (payload[3] << 16) + (payload[4] << 8) + payload[5];
693
+ return size <= 0x100000 ? size : null;
694
+ }
695
+ /** Payload: `[macro, length, GBK bytes...]`. */
696
+ export function hitscanXviDecodeMacroName(reply) {
697
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroNameGet);
698
+ if (!payload || payload.length < 2)
699
+ return null;
700
+ const id = payload[0];
701
+ const length = payload[1];
702
+ if (id === undefined || id < 1 || id > HITSCAN_XVI_MAX_MACROS)
703
+ return null;
704
+ if (length === undefined || length > 56 || payload.length < 2 + length)
705
+ return null;
706
+ return { id, bytes: Array.from(payload.subarray(2, 2 + length)) };
707
+ }
708
+ /** Validates the echoed id, offset and length before returning chunk bytes. */
709
+ export function hitscanXviDecodeMacroData(reply, macroId, offset, length) {
710
+ const payload = hitscanXviDecode(reply, HITSCAN_XVI_CLASS.macro, HITSCAN_XVI_COMMAND.macroDataGet);
711
+ if (!payload || payload.length < 7 + length)
712
+ return null;
713
+ const echoedId = offsetWord(payload, 0);
714
+ const echoedOffset = (payload[2] * 0x1000000) + (payload[3] << 16) + (payload[4] << 8) + payload[5];
715
+ if (echoedId !== macroId || echoedOffset !== offset || payload[6] !== length)
716
+ return null;
717
+ return Array.from(payload.subarray(7, 7 + length));
718
+ }
719
+ /**
720
+ * Input report 4 carries unsolicited state changes. WebHID strips the report id
721
+ * on some platforms, so a leading `4` followed by a known type is dropped too.
722
+ */
723
+ export function hitscanXviDecodeNotification(bytes) {
724
+ let data = checkedBytes(bytes);
725
+ if (data[0] === HITSCAN_XVI_NOTIFICATION_REPORT_ID && data.length > 1
726
+ && [0x01, 0x02, 0x03, 0x06, 0x07, 0x08, 0x19].includes(data[1])) {
727
+ data = data.slice(1);
728
+ }
729
+ const type = data[0];
730
+ if (type === 0x19) {
731
+ const rssi = data[1];
732
+ const level = data[2];
733
+ if (rssi === undefined || level === undefined || level > 3)
734
+ return null;
735
+ return { type: "signal", level: level, rssi };
736
+ }
737
+ if (type === 0x06) {
738
+ if (data[1] === 0)
739
+ return { type: "sleep" };
740
+ if (data[1] === 1)
741
+ return { type: "wake" };
742
+ return null;
743
+ }
744
+ if (type === 0x03) {
745
+ const percent = data[1];
746
+ const charging = data[2];
747
+ if (percent === undefined || charging === undefined || percent > 100 || (charging !== 0 && charging !== 1))
748
+ return null;
749
+ return { type: "battery", percent, charging: charging === 1 };
750
+ }
751
+ if (type === 0x02)
752
+ return data.length >= 2 ? { type: "profile" } : null;
753
+ if (type === 0x01) {
754
+ if (data.length < 6)
755
+ return null;
756
+ const stage = data[1] - 1;
757
+ const x = word(data[2], data[3]);
758
+ const y = word(data[4], data[5]);
759
+ if (stage < 0 || x < HITSCAN_XVI_DPI_MIN || x > HITSCAN_XVI_DPI_MAX || y < HITSCAN_XVI_DPI_MIN || y > HITSCAN_XVI_DPI_MAX)
760
+ return null;
761
+ return { type: "dpi", stage, x, y };
762
+ }
763
+ if (type === 0x07) {
764
+ const millimetres = data[1] === undefined ? null : hitscanXviDecodeLiftOffByte(data[1]);
765
+ return millimetres === null ? null : { type: "liftOff", millimetres };
766
+ }
767
+ if (type === 0x08) {
768
+ const hz = data[1] === undefined ? null : hitscanXviDecodePollingRateCode(data[1]);
769
+ return hz === null ? null : { type: "polling", hz };
770
+ }
771
+ return { type: "unknown", bytes: Array.from(data) };
772
+ }
773
+ //# sourceMappingURL=xvi.js.map