@gamepadly/sdk 0.0.0-stage → 0.1.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +137 -2
  3. package/SECURITY.md +19 -0
  4. package/dist/bridge/index.d.ts +50 -0
  5. package/dist/bridge/index.d.ts.map +1 -0
  6. package/dist/bridge/index.js +422 -0
  7. package/dist/bridge/index.js.map +7 -0
  8. package/dist/bridge/profiles.d.ts +51 -0
  9. package/dist/bridge/profiles.d.ts.map +1 -0
  10. package/dist/browser.d.ts +15 -0
  11. package/dist/browser.d.ts.map +1 -0
  12. package/dist/browser.js +4140 -0
  13. package/dist/browser.js.map +7 -0
  14. package/dist/browser.min.js +1 -0
  15. package/dist/chunks/chunk-BEBDNL5B.js +1850 -0
  16. package/dist/chunks/chunk-BEBDNL5B.js.map +7 -0
  17. package/dist/haptics/presets.d.ts +35 -0
  18. package/dist/haptics/presets.d.ts.map +1 -0
  19. package/dist/haptics/router.d.ts +102 -0
  20. package/dist/haptics/router.d.ts.map +1 -0
  21. package/dist/hid/index.d.ts +323 -0
  22. package/dist/hid/index.d.ts.map +1 -0
  23. package/dist/hub.d.ts +112 -0
  24. package/dist/hub.d.ts.map +1 -0
  25. package/dist/index.d.ts +15 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +747 -0
  28. package/dist/index.js.map +7 -0
  29. package/dist/input/actions.d.ts +59 -0
  30. package/dist/input/actions.d.ts.map +1 -0
  31. package/dist/link/engine.d.ts +213 -0
  32. package/dist/link/engine.d.ts.map +1 -0
  33. package/dist/link/index.d.ts +8 -0
  34. package/dist/link/index.d.ts.map +1 -0
  35. package/dist/link/index.js +944 -0
  36. package/dist/link/index.js.map +7 -0
  37. package/dist/loop/index.d.ts +69 -0
  38. package/dist/loop/index.d.ts.map +1 -0
  39. package/dist/loop/index.js +214 -0
  40. package/dist/loop/index.js.map +7 -0
  41. package/package.json +79 -4
package/dist/index.js ADDED
@@ -0,0 +1,747 @@
1
+ import {
2
+ DualSenseControllerManager,
3
+ TACTILE_PRESETS,
4
+ TRIGGER_PRESETS,
5
+ applyDeadzone,
6
+ buildSwitchHdFromPulse,
7
+ crc32WithSeed,
8
+ createEmptyInput,
9
+ encodeSwitchHdRumbleSide,
10
+ encodeTriggerEffect,
11
+ identifyGamepadFromId
12
+ } from "./chunks/chunk-BEBDNL5B.js";
13
+
14
+ // src/haptics/router.ts
15
+ function canUse4Ch(ctx) {
16
+ if (ctx.allow4ChAudio === false) return false;
17
+ return Boolean(ctx.audio && ctx.audio.dsMode !== "none" && ctx.audio.dsMaxChannelCount >= 4);
18
+ }
19
+ function routeHapticPulse(ctx, spec) {
20
+ if (canUse4Ch(ctx)) {
21
+ ctx.audio.playHapticPulse(spec);
22
+ return;
23
+ }
24
+ const matrix = ctx.hid.getCapabilityMatrixForSlot(ctx.slotId);
25
+ const intensity = Math.max(0, Math.min(1, spec.intensity));
26
+ const durationMs = Math.max(25, Math.min(2500, Math.round(spec.durationMs)));
27
+ const pan = Math.max(-1, Math.min(1, spec.pan ?? 0));
28
+ if (matrix.hd_rumble?.availability === "ready") {
29
+ const hdSpec = buildSwitchHdFromPulse({
30
+ pan,
31
+ freqHz: spec.freqHz,
32
+ intensity,
33
+ durationMs
34
+ });
35
+ void ctx.hid.playSwitchHdRumble(hdSpec, ctx.slotId);
36
+ return;
37
+ }
38
+ if (matrix.impulse_triggers?.availability === "ready") {
39
+ const leftGain = Math.min(1, 1 - Math.max(0, pan));
40
+ const rightGain = Math.min(1, 1 + Math.min(0, pan));
41
+ const crispBias = spec.freqHz >= 120 ? 0.85 : 0.45;
42
+ const gripBias = spec.freqHz < 120 ? 0.78 : 0.35;
43
+ void ctx.hid.playTriggerRumble(
44
+ intensity * leftGain * crispBias,
45
+ intensity * rightGain * crispBias,
46
+ durationMs,
47
+ intensity * (spec.freqHz >= 120 ? 0.65 : 0.35),
48
+ intensity * gripBias,
49
+ ctx.slotId
50
+ );
51
+ return;
52
+ }
53
+ const strong = spec.freqHz < 120 ? intensity * 0.8 : intensity * 0.3;
54
+ const weak = spec.freqHz >= 120 ? intensity * 0.85 : intensity * 0.4;
55
+ ctx.hid.triggerGamepadRumbleFallback(weak, strong, durationMs, ctx.slotId);
56
+ }
57
+ function routeHapticSweep(ctx, spec) {
58
+ if (canUse4Ch(ctx)) {
59
+ ctx.audio.playHapticSweep(spec);
60
+ return;
61
+ }
62
+ const matrix = ctx.hid.getCapabilityMatrixForSlot(ctx.slotId);
63
+ const intensity = Math.max(0, Math.min(1, spec.intensity));
64
+ const durationMs = Math.max(40, Math.min(2500, Math.round(spec.durationMs)));
65
+ if (matrix.hd_rumble?.availability === "ready") {
66
+ const stepsCount = Math.max(2, Math.min(6, Math.round(durationMs / 55)));
67
+ const stepDur = Math.max(30, Math.round(durationMs / stepsCount));
68
+ const steps = [];
69
+ for (let i = 0; i < stepsCount; i++) {
70
+ const t = stepsCount === 1 ? 0 : i / (stepsCount - 1);
71
+ const pan = spec.fromPan + (spec.toPan - spec.fromPan) * t;
72
+ const freqHz = spec.fromHz + (spec.toHz - spec.fromHz) * t;
73
+ steps.push({
74
+ ...buildSwitchHdFromPulse({ pan, freqHz, intensity, durationMs: stepDur }),
75
+ delayMs: i * stepDur
76
+ });
77
+ }
78
+ void ctx.hid.playSwitchHdRumbleSequence(steps, ctx.slotId);
79
+ return;
80
+ }
81
+ if (matrix.impulse_triggers?.availability === "ready") {
82
+ const midPan = (spec.fromPan + spec.toPan) * 0.5;
83
+ const leftGain = Math.min(1, 1 - Math.max(0, midPan));
84
+ const rightGain = Math.min(1, 1 + Math.min(0, midPan));
85
+ void ctx.hid.playTriggerRumble(
86
+ intensity * leftGain * 0.7,
87
+ intensity * rightGain * 0.7,
88
+ durationMs,
89
+ intensity * 0.5,
90
+ intensity * 0.6,
91
+ ctx.slotId
92
+ );
93
+ return;
94
+ }
95
+ ctx.hid.triggerGamepadRumbleFallback(intensity * 0.55, intensity * 0.65, durationMs, ctx.slotId);
96
+ }
97
+ function routeTactilePreset(ctx, presetId, opts = {}) {
98
+ const preset = TACTILE_PRESETS[presetId];
99
+ if (!preset) return;
100
+ const pan = Math.max(-1, Math.min(1, opts.pan ?? 0));
101
+ const intensity = Math.max(0, Math.min(1, (opts.intensity ?? 1) * preset.intensity));
102
+ if (canUse4Ch(ctx)) {
103
+ ctx.audio.playHapticPulse({
104
+ pan,
105
+ freqHz: preset.freqHz,
106
+ endFreqHz: preset.endFreqHz,
107
+ durationMs: preset.durationMs,
108
+ intensity,
109
+ wave: preset.wave
110
+ });
111
+ return;
112
+ }
113
+ const matrix = ctx.hid.getCapabilityMatrixForSlot(ctx.slotId);
114
+ if (matrix.hd_rumble?.availability === "ready" && preset.switchHd) {
115
+ const hdSpec = buildSwitchHdFromPulse({
116
+ pan,
117
+ freqHz: preset.freqHz,
118
+ intensity,
119
+ durationMs: preset.durationMs,
120
+ highFreqHz: preset.switchHd.highFreqHz,
121
+ lowFreqHz: preset.switchHd.lowFreqHz,
122
+ highWeight: preset.switchHd.highWeight,
123
+ lowWeight: preset.switchHd.lowWeight
124
+ });
125
+ void ctx.hid.playSwitchHdRumble(hdSpec, ctx.slotId);
126
+ return;
127
+ }
128
+ if (matrix.impulse_triggers?.availability === "ready" && preset.triggerEmphasis) {
129
+ const leftGain = Math.min(1, 1 - Math.max(0, pan));
130
+ const rightGain = Math.min(1, 1 + Math.min(0, pan));
131
+ const trig = intensity * preset.triggerEmphasis;
132
+ void ctx.hid.playTriggerRumble(
133
+ trig * leftGain,
134
+ trig * rightGain,
135
+ Math.max(30, preset.durationMs),
136
+ intensity * 0.45,
137
+ intensity * 0.55,
138
+ ctx.slotId
139
+ );
140
+ return;
141
+ }
142
+ routeHapticPulse(ctx, {
143
+ pan,
144
+ freqHz: preset.freqHz,
145
+ endFreqHz: preset.endFreqHz,
146
+ durationMs: preset.durationMs,
147
+ intensity,
148
+ wave: preset.wave
149
+ });
150
+ }
151
+
152
+ // src/input/actions.ts
153
+ var BUTTON_NAMES = /* @__PURE__ */ new Set([
154
+ "cross",
155
+ "circle",
156
+ "square",
157
+ "triangle",
158
+ "l1",
159
+ "r1",
160
+ "l2Btn",
161
+ "r2Btn",
162
+ "l3",
163
+ "r3",
164
+ "up",
165
+ "down",
166
+ "left",
167
+ "right",
168
+ "create",
169
+ "options",
170
+ "ps",
171
+ "touchpadClick"
172
+ ]);
173
+ function evalBindingValue(binding, input, keysDown, mouseDown, threshold) {
174
+ if (binding === "l2") return input.l2;
175
+ if (binding === "r2") return input.r2;
176
+ if (binding === "lx+") return Math.max(0, input.lx);
177
+ if (binding === "lx-") return Math.max(0, -input.lx);
178
+ if (binding === "ly+") return Math.max(0, input.ly);
179
+ if (binding === "ly-") return Math.max(0, -input.ly);
180
+ if (binding === "rx+") return Math.max(0, input.rx);
181
+ if (binding === "rx-") return Math.max(0, -input.rx);
182
+ if (binding === "ry+") return Math.max(0, input.ry);
183
+ if (binding === "ry-") return Math.max(0, -input.ry);
184
+ if (BUTTON_NAMES.has(binding)) {
185
+ return input.buttons[binding] ? 1 : 0;
186
+ }
187
+ if (binding === "MouseLeft" || binding === "MouseRight" || binding === "MouseMiddle") {
188
+ return mouseDown.has(binding) ? 1 : 0;
189
+ }
190
+ if (keysDown.has(binding)) {
191
+ return Math.max(1, threshold);
192
+ }
193
+ return 0;
194
+ }
195
+ function createActionTracker(spec) {
196
+ const threshold = spec.analogThreshold ?? 0.45;
197
+ const actionsDef = spec.actions ?? {};
198
+ const axesDef = spec.axes ?? {};
199
+ const keysDown = /* @__PURE__ */ new Set();
200
+ const mouseDown = /* @__PURE__ */ new Set();
201
+ const states = /* @__PURE__ */ new Map();
202
+ const pressedAt = /* @__PURE__ */ new Map();
203
+ const axisValues = /* @__PURE__ */ new Map();
204
+ for (const name of Object.keys(actionsDef)) {
205
+ states.set(name, { pressed: false, justPressed: false, justReleased: false, heldMs: 0, value: 0 });
206
+ }
207
+ for (const name of Object.keys(axesDef)) {
208
+ axisValues.set(name, 0);
209
+ }
210
+ const mapMouseButton = (button) => {
211
+ if (button === 0) return "MouseLeft";
212
+ if (button === 1) return "MouseMiddle";
213
+ if (button === 2) return "MouseRight";
214
+ return null;
215
+ };
216
+ const onKeyDown = (e) => {
217
+ const ke = e;
218
+ if (ke.code) keysDown.add(ke.code);
219
+ };
220
+ const onKeyUp = (e) => {
221
+ const ke = e;
222
+ if (ke.code) keysDown.delete(ke.code);
223
+ };
224
+ const onPointerDown = (e) => {
225
+ const pe = e;
226
+ const mapped = mapMouseButton(pe.button);
227
+ if (mapped) mouseDown.add(mapped);
228
+ };
229
+ const onPointerUp = (e) => {
230
+ const pe = e;
231
+ const mapped = mapMouseButton(pe.button);
232
+ if (mapped) mouseDown.delete(mapped);
233
+ };
234
+ const onBlur = () => {
235
+ keysDown.clear();
236
+ mouseDown.clear();
237
+ };
238
+ const shouldAttach = spec.attachWindowListeners !== false && typeof window !== "undefined";
239
+ const mouseTarget = spec.mouseTarget ?? (typeof window !== "undefined" ? window : void 0);
240
+ if (shouldAttach) {
241
+ window.addEventListener("keydown", onKeyDown);
242
+ window.addEventListener("keyup", onKeyUp);
243
+ window.addEventListener("blur", onBlur);
244
+ mouseTarget?.addEventListener("pointerdown", onPointerDown);
245
+ window.addEventListener("pointerup", onPointerUp);
246
+ }
247
+ const EMPTY_STATE = {
248
+ pressed: false,
249
+ justPressed: false,
250
+ justReleased: false,
251
+ heldMs: 0,
252
+ value: 0
253
+ };
254
+ return {
255
+ update(input, nowMs = typeof performance !== "undefined" ? performance.now() : Date.now()) {
256
+ for (const [name, bindings] of Object.entries(actionsDef)) {
257
+ let maxVal = 0;
258
+ for (const b of bindings) {
259
+ const v = evalBindingValue(b, input, keysDown, mouseDown, threshold);
260
+ if (v > maxVal) maxVal = v;
261
+ }
262
+ const pressed = maxVal >= threshold;
263
+ const prev = states.get(name);
264
+ const justPressed = pressed && !prev.pressed;
265
+ const justReleased = !pressed && prev.pressed;
266
+ if (justPressed) {
267
+ pressedAt.set(name, nowMs);
268
+ } else if (!pressed) {
269
+ pressedAt.delete(name);
270
+ }
271
+ const startAt = pressedAt.get(name);
272
+ prev.pressed = pressed;
273
+ prev.justPressed = justPressed;
274
+ prev.justReleased = justReleased;
275
+ prev.heldMs = pressed && startAt !== void 0 ? Math.max(0, nowMs - startAt) : 0;
276
+ prev.value = Math.max(0, Math.min(1, maxVal));
277
+ }
278
+ for (const [name, def] of Object.entries(axesDef)) {
279
+ let value = 0;
280
+ if (def.stick) {
281
+ const raw = input[def.stick];
282
+ const dz = def.deadzone ?? 0.08;
283
+ value = applyDeadzone(raw, dz);
284
+ }
285
+ let digital = 0;
286
+ if (def.negativeButtons?.some((b) => input.buttons[b])) digital -= 1;
287
+ if (def.positiveButtons?.some((b) => input.buttons[b])) digital += 1;
288
+ if (def.negativeKeys?.some((k) => keysDown.has(k))) digital -= 1;
289
+ if (def.positiveKeys?.some((k) => keysDown.has(k))) digital += 1;
290
+ const combined = Math.abs(digital) > Math.abs(value) ? digital : value;
291
+ const clamped = Math.max(-1, Math.min(1, combined));
292
+ axisValues.set(name, def.invert ? -clamped : clamped);
293
+ }
294
+ },
295
+ action(name) {
296
+ return states.get(name) ?? EMPTY_STATE;
297
+ },
298
+ isPressed(name) {
299
+ return states.get(name)?.pressed ?? false;
300
+ },
301
+ justPressed(name) {
302
+ return states.get(name)?.justPressed ?? false;
303
+ },
304
+ justReleased(name) {
305
+ return states.get(name)?.justReleased ?? false;
306
+ },
307
+ axis(name) {
308
+ return axisValues.get(name) ?? 0;
309
+ },
310
+ setKeyDown(code, down) {
311
+ if (down) keysDown.add(code);
312
+ else keysDown.delete(code);
313
+ },
314
+ setMouseButton(button, down) {
315
+ if (down) mouseDown.add(button);
316
+ else mouseDown.delete(button);
317
+ },
318
+ dispose() {
319
+ if (shouldAttach) {
320
+ window.removeEventListener("keydown", onKeyDown);
321
+ window.removeEventListener("keyup", onKeyUp);
322
+ window.removeEventListener("blur", onBlur);
323
+ mouseTarget?.removeEventListener("pointerdown", onPointerDown);
324
+ window.removeEventListener("pointerup", onPointerUp);
325
+ }
326
+ keysDown.clear();
327
+ mouseDown.clear();
328
+ pressedAt.clear();
329
+ }
330
+ };
331
+ }
332
+
333
+ // src/hub.ts
334
+ var NOOP_CONTINUOUS = {
335
+ update() {
336
+ },
337
+ stop() {
338
+ }
339
+ };
340
+ function computeCapabilities(hid, audio, slotId, allow4ChAudio) {
341
+ const has4Ch = allow4ChAudio && Boolean(audio && audio.dsMode !== "none" && audio.dsMaxChannelCount >= 4);
342
+ const hasSpk = allow4ChAudio && Boolean(audio && audio.dsMode !== "none");
343
+ const input = hid.getInputState(slotId);
344
+ const matrix = hid.getCapabilityMatrixForSlot(slotId, {
345
+ has4ChAudio: has4Ch,
346
+ hasControllerSpeaker: hasSpk
347
+ });
348
+ const has = (cap) => matrix[cap]?.availability === "ready";
349
+ let tier = "keyboard_mouse";
350
+ if (has("adaptive_triggers") && has4Ch) tier = "dualsense_full_4ch";
351
+ else if (has("adaptive_triggers")) tier = "dualsense_hid_only";
352
+ else if (has("hd_rumble")) tier = "switch_pro_hid";
353
+ else if (has("impulse_triggers")) tier = "xbox_impulse";
354
+ else if (input.source === "gamepad") tier = "standard_gamepad";
355
+ return {
356
+ slotId: slotId ?? hid.getActiveSlotId(),
357
+ tier,
358
+ identity: input.identity,
359
+ matrix,
360
+ has,
361
+ hasAdaptiveTriggers: has("adaptive_triggers"),
362
+ hasImpulseTriggers: has("impulse_triggers"),
363
+ hasHdRumble: has("hd_rumble"),
364
+ hasDualRumble: has("dual_motor_rumble"),
365
+ has4ChHaptics: has4Ch,
366
+ hasControllerSpeaker: hasSpk,
367
+ hasGyro: has("gyro"),
368
+ hasTouchpad: has("touchpad")
369
+ };
370
+ }
371
+ function buildCapabilitySignature(slots, active) {
372
+ const slotSig = slots.map((s) => `${s.id}:${s.source}:${s.identity.model}:${s.identity.transport}`).join(",");
373
+ return [
374
+ slotSig,
375
+ active.slotId ?? "",
376
+ active.tier,
377
+ active.hasAdaptiveTriggers ? 1 : 0,
378
+ active.hasImpulseTriggers ? 1 : 0,
379
+ active.hasHdRumble ? 1 : 0,
380
+ active.has4ChHaptics ? 1 : 0,
381
+ active.hasControllerSpeaker ? 1 : 0,
382
+ active.hasGyro ? 1 : 0
383
+ ].join("|");
384
+ }
385
+ function createGamepadly(options = {}) {
386
+ const ownsHid = !options.hid;
387
+ const hid = options.hid ?? new DualSenseControllerManager();
388
+ let audio = options.audio ?? null;
389
+ let disposed = false;
390
+ const continuousHandles = /* @__PURE__ */ new Set();
391
+ const timers = /* @__PURE__ */ new Set();
392
+ const trackers = /* @__PURE__ */ new Set();
393
+ const capListeners = /* @__PURE__ */ new Set();
394
+ if (options.autoHotplug !== false) {
395
+ hid.attachHidHotplug();
396
+ }
397
+ if (options.attachKeyboardFallback && ownsHid) {
398
+ hid.attachKeyboardFallback();
399
+ }
400
+ let lastCapSig = "";
401
+ const syncCapabilities = () => {
402
+ if (disposed) return;
403
+ const slots = hid.getConnectedControllers();
404
+ const active = computeCapabilities(hid, audio, void 0, true);
405
+ const nextSig = buildCapabilitySignature(slots, active);
406
+ if (nextSig === lastCapSig) return;
407
+ lastCapSig = nextSig;
408
+ for (const fn of capListeners) {
409
+ try {
410
+ fn({ slots, active });
411
+ } catch {
412
+ }
413
+ }
414
+ };
415
+ lastCapSig = buildCapabilitySignature(
416
+ hid.getConnectedControllers(),
417
+ computeCapabilities(hid, audio, void 0, true)
418
+ );
419
+ const unsubHidStatus = hid.onStatusChange(() => {
420
+ syncCapabilities();
421
+ });
422
+ const onGamepadEvent = () => syncCapabilities();
423
+ const onPageHide = () => hid.emergencyReset();
424
+ if (typeof window !== "undefined") {
425
+ if (options.autoHotplug !== false) {
426
+ window.addEventListener("gamepadconnected", onGamepadEvent);
427
+ window.addEventListener("gamepaddisconnected", onGamepadEvent);
428
+ }
429
+ if (options.autoEmergencyReset !== false) {
430
+ window.addEventListener("pagehide", onPageHide);
431
+ window.addEventListener("beforeunload", onPageHide);
432
+ }
433
+ }
434
+ let pollInterval = null;
435
+ const pollMs = options.capabilityPollMs ?? 250;
436
+ if (pollMs > 0 && typeof setInterval !== "undefined") {
437
+ pollInterval = setInterval(syncCapabilities, pollMs);
438
+ }
439
+ const createPlayerHandle = (target) => {
440
+ let lastLightbarColor = [23, 23, 23];
441
+ let lastLightbarSentAt = 0;
442
+ const resolveTarget = () => {
443
+ if (target === "active") {
444
+ return { slotId: void 0, connected: true, isPrimary: true };
445
+ }
446
+ if (typeof target === "number") {
447
+ const slots = hid.getConnectedControllers();
448
+ const slot = slots[target];
449
+ if (!slot) return { slotId: `__unassigned_${target}`, connected: false, isPrimary: target === 0 };
450
+ const activeId2 = hid.getActiveSlotId();
451
+ return { slotId: slot.id, connected: true, isPrimary: slot.id === activeId2 || target === 0 };
452
+ }
453
+ const activeId = hid.getActiveSlotId();
454
+ return { slotId: target, connected: true, isPrimary: target === activeId };
455
+ };
456
+ const getRouteCtx = () => {
457
+ const r = resolveTarget();
458
+ return {
459
+ hid,
460
+ audio,
461
+ slotId: r.slotId,
462
+ allow4ChAudio: r.isPrimary
463
+ };
464
+ };
465
+ return {
466
+ getSlotId() {
467
+ const r = resolveTarget();
468
+ if (r.slotId === void 0) return hid.getActiveSlotId();
469
+ return r.connected ? r.slotId : null;
470
+ },
471
+ getCapabilities() {
472
+ const r = resolveTarget();
473
+ return computeCapabilities(hid, audio, r.slotId, r.isPrimary);
474
+ },
475
+ input: {
476
+ getState: () => {
477
+ const r = resolveTarget();
478
+ return hid.getInputState(r.slotId);
479
+ },
480
+ applyDeadzone
481
+ },
482
+ triggers: {
483
+ reset: () => {
484
+ if (disposed) return;
485
+ const r = resolveTarget();
486
+ hid.resetTriggers(r.slotId).catch(() => {
487
+ });
488
+ },
489
+ setLeft: (effect) => {
490
+ if (disposed) return;
491
+ const r = resolveTarget();
492
+ hid.setTriggers(effect, void 0, r.slotId).catch(() => {
493
+ });
494
+ },
495
+ setRight: (effect) => {
496
+ if (disposed) return;
497
+ const r = resolveTarget();
498
+ hid.setTriggers(void 0, effect, r.slotId).catch(() => {
499
+ });
500
+ },
501
+ setBoth: (left, right) => {
502
+ if (disposed) return;
503
+ const r = resolveTarget();
504
+ hid.setTriggers(left, right, r.slotId).catch(() => {
505
+ });
506
+ },
507
+ applyPreset: (presetId, side = "both") => {
508
+ if (disposed) return;
509
+ const effect = TRIGGER_PRESETS[presetId];
510
+ if (!effect) return;
511
+ const r = resolveTarget();
512
+ if (side === "left") hid.setTriggers(effect, void 0, r.slotId).catch(() => {
513
+ });
514
+ else if (side === "right") hid.setTriggers(void 0, effect, r.slotId).catch(() => {
515
+ });
516
+ else hid.setTriggers(effect, effect, r.slotId).catch(() => {
517
+ });
518
+ },
519
+ pulseImpulse: (spec) => {
520
+ if (disposed) return;
521
+ const r = resolveTarget();
522
+ void hid.playTriggerRumble(
523
+ spec.left ?? 0,
524
+ spec.right ?? 0,
525
+ spec.durationMs ?? 80,
526
+ spec.weakGrip ?? 0,
527
+ spec.strongGrip ?? 0,
528
+ r.slotId
529
+ );
530
+ }
531
+ },
532
+ haptics: {
533
+ pulse: (spec) => {
534
+ if (disposed) return;
535
+ routeHapticPulse(getRouteCtx(), spec);
536
+ },
537
+ sweep: (spec) => {
538
+ if (disposed) return;
539
+ routeHapticSweep(getRouteCtx(), spec);
540
+ },
541
+ rumble: (opts) => {
542
+ if (disposed) return;
543
+ const r = resolveTarget();
544
+ hid.triggerGamepadRumbleFallback(opts.weak, opts.strong, opts.durationMs, r.slotId);
545
+ },
546
+ hdRumble: (spec) => {
547
+ if (disposed) return;
548
+ const r = resolveTarget();
549
+ void hid.playSwitchHdRumble(spec, r.slotId);
550
+ },
551
+ hdSequence: (steps) => {
552
+ if (disposed) return;
553
+ const r = resolveTarget();
554
+ void hid.playSwitchHdRumbleSequence(steps, r.slotId);
555
+ },
556
+ playPreset: (presetId, opts) => {
557
+ if (disposed) return;
558
+ routeTactilePreset(getRouteCtx(), presetId, opts);
559
+ },
560
+ createContinuous: (preset) => {
561
+ if (disposed || !audio) return NOOP_CONTINUOUS;
562
+ const handle = audio.createContinuous(preset);
563
+ continuousHandles.add(handle);
564
+ return {
565
+ update: (p) => {
566
+ if (!disposed) handle.update(p);
567
+ },
568
+ stop: () => {
569
+ continuousHandles.delete(handle);
570
+ handle.stop();
571
+ }
572
+ };
573
+ },
574
+ playComboPreset: (presetId, pan = 0) => {
575
+ if (disposed) return;
576
+ const r = resolveTarget();
577
+ audio?.playComboPreset(presetId, pan);
578
+ if (!audio || !r.isPrimary || audio.dsMode === "none" || audio.dsMaxChannelCount < 4) {
579
+ const strong = presetId === "shotgun" || presetId === "thunder" ? 0.85 : 0.4;
580
+ const dur = presetId === "ratchet_tick" ? 30 : 120;
581
+ routeHapticPulse(getRouteCtx(), {
582
+ pan,
583
+ freqHz: presetId === "ratchet_tick" ? 190 : 95,
584
+ durationMs: dur,
585
+ intensity: strong
586
+ });
587
+ }
588
+ },
589
+ stop: () => {
590
+ const r = resolveTarget();
591
+ hid.stopGamepadRumble(r.slotId);
592
+ }
593
+ },
594
+ audio: {
595
+ playPcTone: (spec) => {
596
+ if (!disposed) audio?.playPcTone(spec);
597
+ },
598
+ playPcNoise: (noiseOpts) => {
599
+ if (!disposed) audio?.playPcNoiseBurst(noiseOpts);
600
+ },
601
+ playControllerSpeakerTone: (spec) => {
602
+ if (!disposed) audio?.playControllerSpeakerTone(spec);
603
+ }
604
+ },
605
+ lightbar: {
606
+ setColor: (rCol, gCol, bCol) => {
607
+ if (disposed) return;
608
+ const now = typeof performance !== "undefined" ? performance.now() : Date.now();
609
+ if (lastLightbarColor[0] === rCol && lastLightbarColor[1] === gCol && lastLightbarColor[2] === bCol) {
610
+ return;
611
+ }
612
+ if (now - lastLightbarSentAt < 45) return;
613
+ lastLightbarSentAt = now;
614
+ lastLightbarColor = [rCol, gCol, bCol];
615
+ const r = resolveTarget();
616
+ hid.setLightbar(rCol, gCol, bCol, r.slotId).catch(() => {
617
+ });
618
+ },
619
+ flash: (rCol, gCol, bCol, durationMs = 240) => {
620
+ if (disposed) return;
621
+ const prev = [...lastLightbarColor];
622
+ const r = resolveTarget();
623
+ hid.setLightbar(rCol, gCol, bCol, r.slotId).catch(() => {
624
+ });
625
+ const t = setTimeout(() => {
626
+ timers.delete(t);
627
+ if (!disposed) {
628
+ hid.setLightbar(prev[0], prev[1], prev[2], r.slotId).catch(() => {
629
+ });
630
+ }
631
+ }, durationMs);
632
+ timers.add(t);
633
+ }
634
+ },
635
+ createActions(spec) {
636
+ const tracker = createActionTracker(spec);
637
+ trackers.add(tracker);
638
+ return {
639
+ ...tracker,
640
+ dispose() {
641
+ trackers.delete(tracker);
642
+ tracker.dispose();
643
+ }
644
+ };
645
+ }
646
+ };
647
+ };
648
+ const activePlayer = createPlayerHandle("active");
649
+ return {
650
+ hid,
651
+ async connectWebHID() {
652
+ const ok = await hid.connect();
653
+ syncCapabilities();
654
+ return ok;
655
+ },
656
+ async reconnectAuthorized() {
657
+ const ok = await hid.reconnectAuthorized();
658
+ syncCapabilities();
659
+ return ok;
660
+ },
661
+ attachAudio(nextAudio) {
662
+ audio = nextAudio;
663
+ syncCapabilities();
664
+ },
665
+ getSlots() {
666
+ return hid.getConnectedControllers();
667
+ },
668
+ get active() {
669
+ return activePlayer;
670
+ },
671
+ player(target) {
672
+ if (target === void 0 || target === "active") return activePlayer;
673
+ return createPlayerHandle(target);
674
+ },
675
+ onCapabilitiesChange(listener) {
676
+ capListeners.add(listener);
677
+ return () => {
678
+ capListeners.delete(listener);
679
+ };
680
+ },
681
+ syncCapabilities,
682
+ dispose() {
683
+ if (disposed) return;
684
+ disposed = true;
685
+ if (pollInterval !== null) {
686
+ clearInterval(pollInterval);
687
+ pollInterval = null;
688
+ }
689
+ unsubHidStatus();
690
+ capListeners.clear();
691
+ if (typeof window !== "undefined") {
692
+ if (options.autoHotplug !== false) {
693
+ window.removeEventListener("gamepadconnected", onGamepadEvent);
694
+ window.removeEventListener("gamepaddisconnected", onGamepadEvent);
695
+ }
696
+ if (options.autoEmergencyReset !== false) {
697
+ window.removeEventListener("pagehide", onPageHide);
698
+ window.removeEventListener("beforeunload", onPageHide);
699
+ }
700
+ }
701
+ for (const t of timers) clearTimeout(t);
702
+ timers.clear();
703
+ for (const tr of trackers) {
704
+ try {
705
+ tr.dispose();
706
+ } catch {
707
+ }
708
+ }
709
+ trackers.clear();
710
+ for (const h of continuousHandles) {
711
+ try {
712
+ h.stop();
713
+ } catch {
714
+ }
715
+ }
716
+ continuousHandles.clear();
717
+ hid.stopGamepadRumble();
718
+ hid.resetTriggers().catch(() => {
719
+ });
720
+ hid.setLightbar(23, 23, 23).catch(() => {
721
+ });
722
+ if (ownsHid) {
723
+ if (options.autoHotplug !== false) hid.detachHidHotplug();
724
+ if (options.attachKeyboardFallback) hid.detachKeyboardFallback();
725
+ }
726
+ }
727
+ };
728
+ }
729
+ export {
730
+ DualSenseControllerManager as ControllerManager,
731
+ DualSenseControllerManager,
732
+ TACTILE_PRESETS,
733
+ TRIGGER_PRESETS,
734
+ applyDeadzone,
735
+ buildSwitchHdFromPulse,
736
+ crc32WithSeed,
737
+ createActionTracker,
738
+ createEmptyInput,
739
+ createGamepadly,
740
+ encodeSwitchHdRumbleSide,
741
+ encodeTriggerEffect,
742
+ identifyGamepadFromId,
743
+ routeHapticPulse,
744
+ routeHapticSweep,
745
+ routeTactilePreset
746
+ };
747
+ //# sourceMappingURL=index.js.map