@gamepadly/sdk 0.2.0 → 0.4.1

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