@gamepadly/sdk 0.1.0 → 0.2.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.
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  TRIGGER_PRESETS,
3
3
  applyDeadzone
4
- } from "../chunks/chunk-BEBDNL5B.js";
4
+ } from "../chunks/chunk-75I5NTQZ.js";
5
5
 
6
6
  // src/bridge/profiles.ts
7
7
  var BUILTIN_BRIDGE_PROFILES = {
package/dist/browser.js CHANGED
@@ -46,6 +46,9 @@ function applyDeadzone(value, threshold = 0.06) {
46
46
  function parseGamepadVendorId(lower) {
47
47
  return (lower.match(/vendor:\s*([0-9a-f]{4})/) ?? lower.match(/^([0-9a-f]{1,4})-[0-9a-f]{1,4}-/))?.[1]?.padStart(4, "0");
48
48
  }
49
+ function parseGamepadProductId(lower) {
50
+ return (lower.match(/product:\s*([0-9a-f]{4})/) ?? lower.match(/^[0-9a-f]{1,4}-([0-9a-f]{1,4})-/))?.[1]?.padStart(4, "0");
51
+ }
49
52
  function detectGamepadBrand(lower) {
50
53
  const vendor = parseGamepadVendorId(lower);
51
54
  if (vendor === "054c") return "sony";
@@ -59,39 +62,50 @@ function detectGamepadBrand(lower) {
59
62
  }
60
63
  function identifyGamepadFromId(rawId) {
61
64
  const lower = (rawId || "").toLowerCase();
65
+ const parsedVidHex = parseGamepadVendorId(lower);
66
+ const parsedPidHex = parseGamepadProductId(lower);
67
+ const parsedVid = parsedVidHex ? Number.parseInt(parsedVidHex, 16) : void 0;
68
+ const parsedPid = parsedPidHex ? Number.parseInt(parsedPidHex, 16) : void 0;
62
69
  const brand = detectGamepadBrand(lower);
63
70
  if (brand === "sony") {
64
71
  if (/0df2|edge/.test(lower)) {
65
- return { brand: "sony", model: "dualsense_edge", displayName: "DualSense Edge", rawId, vendorId: 1356, productId: 3570 };
72
+ return { brand: "sony", model: "dualsense_edge", displayName: "DualSense Edge", rawId, vendorId: 1356, productId: parsedPid ?? 3570 };
66
73
  }
67
74
  if (/0ce6|dualsense/.test(lower)) {
68
- return { brand: "sony", model: "dualsense", displayName: "PlayStation DualSense", rawId, vendorId: 1356, productId: 3302 };
75
+ return { brand: "sony", model: "dualsense", displayName: "PlayStation DualSense", rawId, vendorId: 1356, productId: parsedPid ?? 3302 };
69
76
  }
70
77
  if (/05c4|09cc|dualshock/.test(lower)) {
71
- return { brand: "sony", model: "dualshock4", displayName: "DualShock 4", rawId, vendorId: 1356 };
78
+ return { brand: "sony", model: "dualshock4", displayName: "DualShock 4", rawId, vendorId: 1356, productId: parsedPid };
72
79
  }
73
- return { brand: "sony", model: "dualsense", displayName: "PlayStation Controller", rawId, vendorId: 1356 };
80
+ return { brand: "sony", model: "dualsense", displayName: "PlayStation Controller", rawId, vendorId: 1356, productId: parsedPid };
74
81
  }
75
82
  if (brand === "xbox") {
76
83
  if (/0b12|0b13|0b20|0b21|0b22|series/.test(lower)) {
77
- return { brand: "xbox", model: "xbox_series", displayName: "Xbox Wireless Controller", rawId, vendorId: 1118 };
84
+ return { brand: "xbox", model: "xbox_series", displayName: "Xbox Wireless Controller", rawId, vendorId: 1118, productId: parsedPid };
78
85
  }
79
86
  if (/028e|360/.test(lower)) {
80
- return { brand: "xbox", model: "xbox_360", displayName: "Xbox 360 Controller", rawId, vendorId: 1118 };
87
+ return { brand: "xbox", model: "xbox_360", displayName: "Xbox 360 Controller", rawId, vendorId: 1118, productId: parsedPid };
81
88
  }
82
- return { brand: "xbox", model: "xbox_series", displayName: "Xbox Controller", rawId, vendorId: 1118 };
89
+ return { brand: "xbox", model: "xbox_series", displayName: "Xbox Controller", rawId, vendorId: 1118, productId: parsedPid };
83
90
  }
84
91
  if (brand === "nintendo") {
85
92
  if (/2069|pro 2|switch 2/.test(lower)) {
86
- return { brand: "nintendo", model: "switch2_pro", displayName: "Nintendo Switch 2 Pro Controller", rawId, vendorId: 1406, productId: 8297 };
93
+ return { brand: "nintendo", model: "switch2_pro", displayName: "Nintendo Switch 2 Pro Controller", rawId, vendorId: 1406, productId: parsedPid ?? 8297 };
87
94
  }
88
95
  if (/joy-con|2006|2007/.test(lower)) {
89
- return { brand: "nintendo", model: "joycon", displayName: "Nintendo Joy-Con", rawId, vendorId: 1406 };
96
+ return { brand: "nintendo", model: "joycon", displayName: "Nintendo Joy-Con", rawId, vendorId: 1406, productId: parsedPid };
90
97
  }
91
- return { brand: "nintendo", model: "switch_pro", displayName: "Nintendo Switch Pro Controller", rawId, vendorId: 1406, productId: 8201 };
98
+ return { brand: "nintendo", model: "switch_pro", displayName: "Nintendo Switch Pro Controller", rawId, vendorId: 1406, productId: parsedPid ?? 8201 };
92
99
  }
93
100
  if (rawId) {
94
- return { brand: "generic", model: "generic", displayName: rawId.split("(")[0].trim() || rawId, rawId };
101
+ return {
102
+ brand: "generic",
103
+ model: "generic",
104
+ displayName: rawId.split("(")[0].trim() || rawId,
105
+ rawId,
106
+ vendorId: parsedVid,
107
+ productId: parsedPid
108
+ };
95
109
  }
96
110
  return { brand: "none", model: "none", displayName: "No Controller Detected", rawId: "" };
97
111
  }
@@ -224,7 +238,12 @@ function createEmptyInput() {
224
238
  create: false,
225
239
  options: false,
226
240
  ps: false,
227
- touchpadClick: false
241
+ touchpadClick: false,
242
+ mute: false,
243
+ fnL: false,
244
+ fnR: false,
245
+ paddleL: false,
246
+ paddleR: false
228
247
  },
229
248
  touch: { active: false, x: 0.5, y: 0.5 },
230
249
  touch2: { active: false, x: 0.5, y: 0.5 },
@@ -251,6 +270,7 @@ function createDefaultOutState(playerIndex = 0) {
251
270
  speakerPreamp: 7,
252
271
  lightbar: [23, 23, 23],
253
272
  playerLeds: PS5_PLAYER_LED_PATTERNS[playerIndex % PS5_PLAYER_LED_PATTERNS.length],
273
+ muteLed: "off",
254
274
  leftTrigger: encodeTriggerEffect({ type: "off" }),
255
275
  rightTrigger: encodeTriggerEffect({ type: "off" }),
256
276
  legacyRumbleActive: false,
@@ -838,9 +858,27 @@ var DualSenseControllerManager = class _DualSenseControllerManager {
838
858
  await sleepMs(20);
839
859
  await this.sendSwitchSubcommand(session, 16, [61, 96, 0, 0, 18]).catch(() => {
840
860
  });
861
+ await sleepMs(20);
862
+ await this.sendSwitchSubcommand(session, 2).catch(() => {
863
+ });
841
864
  } catch {
842
865
  }
843
866
  }
867
+ parseSwitchDeviceInfo(session, data) {
868
+ if (data.byteLength < 17) return;
869
+ if (data.getUint8(13) !== 2) return;
870
+ const major = data.getUint8(14);
871
+ const minor = data.getUint8(15);
872
+ if (major === 0 && minor === 0) return;
873
+ const fw = {
874
+ buildDate: "",
875
+ buildTime: "",
876
+ hwInfoHex: "",
877
+ fwVersionHex: `v${major}.${minor}`
878
+ };
879
+ session.firmwareInfo = fw;
880
+ session.input.identity.firmwareInfo = fw;
881
+ }
844
882
  parseSwitchStickCalibration(session, data) {
845
883
  if (data.byteLength < 37) return;
846
884
  if (data.getUint8(13) !== 16) return;
@@ -872,6 +910,7 @@ var DualSenseControllerManager = class _DualSenseControllerManager {
872
910
  const { reportId, data } = event;
873
911
  if (reportId === 33) {
874
912
  this.parseSwitchStickCalibration(session, data);
913
+ this.parseSwitchDeviceInfo(session, data);
875
914
  }
876
915
  if (reportId !== 48 && reportId !== 33 || data.byteLength < 11) {
877
916
  return;
@@ -1087,7 +1126,12 @@ var DualSenseControllerManager = class _DualSenseControllerManager {
1087
1126
  input.buttons.r3 = Boolean(btn1 & 128);
1088
1127
  input.buttons.ps = Boolean(btn2 & 1);
1089
1128
  input.buttons.touchpadClick = Boolean(btn2 & 2);
1090
- const btnMask = btn0 & 240 | (dpad < 8 ? 1 : 0) | btn1 << 8 | (btn2 & 3) << 16;
1129
+ input.buttons.mute = Boolean(btn2 & 4);
1130
+ input.buttons.fnL = Boolean(btn2 & 16);
1131
+ input.buttons.fnR = Boolean(btn2 & 32);
1132
+ input.buttons.paddleL = Boolean(btn2 & 64);
1133
+ input.buttons.paddleR = Boolean(btn2 & 128);
1134
+ const btnMask = btn0 & 240 | (dpad < 8 ? 1 : 0) | btn1 << 8 | (btn2 & 247) << 16;
1091
1135
  if ((btnMask & ~session.prevBtnMask) !== 0) {
1092
1136
  session.lastActivityAt = now;
1093
1137
  }
@@ -1121,9 +1165,9 @@ var DualSenseControllerManager = class _DualSenseControllerManager {
1121
1165
  const battByte = data.getUint8(offset + 52);
1122
1166
  const level = battByte & 15;
1123
1167
  const state = battByte >> 4 & 15;
1124
- input.battery.percent = Math.min(100, Math.round((level + 0.5) / 10 * 100));
1125
1168
  input.battery.charging = state === 1;
1126
1169
  input.battery.full = state === 2;
1170
+ input.battery.percent = state === 2 ? 100 : Math.min(100, Math.round((level + 0.5) / 10 * 100));
1127
1171
  input.battery.known = true;
1128
1172
  }
1129
1173
  }
@@ -1560,7 +1604,8 @@ var DualSenseControllerManager = class _DualSenseControllerManager {
1560
1604
  common[37] = outState.speakerPreamp & 7;
1561
1605
  }
1562
1606
  if (includeLeds) {
1563
- flag1 |= 4 | 16;
1607
+ flag1 |= 1 | 4 | 16;
1608
+ common[8] = outState.muteLed === "on" ? 1 : outState.muteLed === "pulse" ? 2 : 0;
1564
1609
  common[38] = 2;
1565
1610
  common[41] = 2;
1566
1611
  common[42] = 0;
@@ -1668,6 +1713,7 @@ var DualSenseControllerManager = class _DualSenseControllerManager {
1668
1713
  session.outState.legacyLeft = 0;
1669
1714
  session.outState.legacyRight = 0;
1670
1715
  session.outState.lightbar = [23, 23, 23];
1716
+ session.outState.muteLed = "off";
1671
1717
  void this.flushSessionOutput(session, { includeTriggers: true, includeLeds: true }).catch(() => {
1672
1718
  });
1673
1719
  }
@@ -1678,6 +1724,30 @@ var DualSenseControllerManager = class _DualSenseControllerManager {
1678
1724
  session.outState.lightbar = [r & 255, g & 255, b & 255];
1679
1725
  return this.flushSessionOutput(session, { includeLeds: true });
1680
1726
  }
1727
+ /**
1728
+ * Sets the player indicator LED bitmask over WebHID:
1729
+ * - DualSense / DualSense Edge: 5 white LEDs below touchpad (`0x00..0x1f`, e.g. `0x04` = center, `0x1f` = all 5).
1730
+ * - Switch Pro: 4 green LEDs on bottom edge (`0x00..0x0f`, e.g. `0x01`, `0x03`, `0x07`, `0x0f`).
1731
+ */
1732
+ async setPlayerLeds(mask, slotId) {
1733
+ const session = this.getHidSessionForSlot(slotId);
1734
+ if (!session || !session.device.opened) return false;
1735
+ if (session.kind === "switch_pro") {
1736
+ await this.sendSwitchSubcommand(session, 48, [mask & 15]);
1737
+ return true;
1738
+ }
1739
+ session.outState.playerLeds = mask & 31;
1740
+ return this.flushSessionOutput(session, { includeLeds: true });
1741
+ }
1742
+ /**
1743
+ * Controls the DualSense / DualSense Edge orange microphone mute button LED (`'off' | 'on' | 'pulse'`).
1744
+ */
1745
+ async setMuteLed(mode, slotId) {
1746
+ const session = this.getHidSessionForSlot(slotId);
1747
+ if (!session || session.kind !== "dualsense") return false;
1748
+ session.outState.muteLed = mode;
1749
+ return this.flushSessionOutput(session, { includeLeds: true });
1750
+ }
1681
1751
  async pulseLegacyRumble(left = 200, right = 200, durationMs = 250, slotId) {
1682
1752
  const session = this.getHidSessionForSlot(slotId);
1683
1753
  if (!session || session.kind !== "dualsense" || !session.device.opened) return false;
@@ -1994,19 +2064,35 @@ var BUTTON_NAMES = /* @__PURE__ */ new Set([
1994
2064
  "ps",
1995
2065
  "touchpadClick"
1996
2066
  ]);
2067
+ var SCROLL_NAV_KEYS = /* @__PURE__ */ new Set([
2068
+ "Space",
2069
+ "ArrowUp",
2070
+ "ArrowDown",
2071
+ "ArrowLeft",
2072
+ "ArrowRight",
2073
+ "Tab"
2074
+ ]);
2075
+ function isEditableTarget(target) {
2076
+ if (!target || typeof target !== "object") return false;
2077
+ const el = target;
2078
+ if (el.isContentEditable) return true;
2079
+ const tag = (el.tagName || "").toUpperCase();
2080
+ return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT";
2081
+ }
1997
2082
  function evalBindingValue(binding, input, keysDown, mouseDown, threshold) {
1998
- if (binding === "l2") return input.l2;
1999
- if (binding === "r2") return input.r2;
2000
- if (binding === "lx+") return Math.max(0, input.lx);
2001
- if (binding === "lx-") return Math.max(0, -input.lx);
2002
- if (binding === "ly+") return Math.max(0, input.ly);
2003
- if (binding === "ly-") return Math.max(0, -input.ly);
2004
- if (binding === "rx+") return Math.max(0, input.rx);
2005
- if (binding === "rx-") return Math.max(0, -input.rx);
2006
- if (binding === "ry+") return Math.max(0, input.ry);
2007
- if (binding === "ry-") return Math.max(0, -input.ry);
2083
+ const useControllerInput = input.source !== "keyboard_mouse";
2084
+ if (binding === "l2") return useControllerInput ? input.l2 : 0;
2085
+ if (binding === "r2") return useControllerInput ? input.r2 : 0;
2086
+ if (binding === "lx+") return useControllerInput ? Math.max(0, input.lx) : 0;
2087
+ if (binding === "lx-") return useControllerInput ? Math.max(0, -input.lx) : 0;
2088
+ if (binding === "ly+") return useControllerInput ? Math.max(0, input.ly) : 0;
2089
+ if (binding === "ly-") return useControllerInput ? Math.max(0, -input.ly) : 0;
2090
+ if (binding === "rx+") return useControllerInput ? Math.max(0, input.rx) : 0;
2091
+ if (binding === "rx-") return useControllerInput ? Math.max(0, -input.rx) : 0;
2092
+ if (binding === "ry+") return useControllerInput ? Math.max(0, input.ry) : 0;
2093
+ if (binding === "ry-") return useControllerInput ? Math.max(0, -input.ry) : 0;
2008
2094
  if (BUTTON_NAMES.has(binding)) {
2009
- return input.buttons[binding] ? 1 : 0;
2095
+ return useControllerInput && input.buttons[binding] ? 1 : 0;
2010
2096
  }
2011
2097
  if (binding === "MouseLeft" || binding === "MouseRight" || binding === "MouseMiddle") {
2012
2098
  return mouseDown.has(binding) ? 1 : 0;
@@ -2020,8 +2106,25 @@ function createActionTracker(spec) {
2020
2106
  const threshold = spec.analogThreshold ?? 0.45;
2021
2107
  const actionsDef = spec.actions ?? {};
2022
2108
  const axesDef = spec.axes ?? {};
2109
+ const preventDefaultKeys = spec.preventDefaultKeys !== false;
2110
+ const boundPreventKeys = /* @__PURE__ */ new Set();
2111
+ for (const bindings of Object.values(actionsDef)) {
2112
+ for (const b of bindings) {
2113
+ if (SCROLL_NAV_KEYS.has(b)) boundPreventKeys.add(b);
2114
+ }
2115
+ }
2116
+ for (const def of Object.values(axesDef)) {
2117
+ for (const k of def.negativeKeys ?? []) {
2118
+ if (SCROLL_NAV_KEYS.has(k)) boundPreventKeys.add(k);
2119
+ }
2120
+ for (const k of def.positiveKeys ?? []) {
2121
+ if (SCROLL_NAV_KEYS.has(k)) boundPreventKeys.add(k);
2122
+ }
2123
+ }
2023
2124
  const keysDown = /* @__PURE__ */ new Set();
2125
+ const latchedKeysDown = /* @__PURE__ */ new Set();
2024
2126
  const mouseDown = /* @__PURE__ */ new Set();
2127
+ const latchedMouseDown = /* @__PURE__ */ new Set();
2025
2128
  const states = /* @__PURE__ */ new Map();
2026
2129
  const pressedAt = /* @__PURE__ */ new Map();
2027
2130
  const axisValues = /* @__PURE__ */ new Map();
@@ -2039,7 +2142,14 @@ function createActionTracker(spec) {
2039
2142
  };
2040
2143
  const onKeyDown = (e) => {
2041
2144
  const ke = e;
2042
- if (ke.code) keysDown.add(ke.code);
2145
+ if (!ke.code) return;
2146
+ if (preventDefaultKeys && boundPreventKeys.has(ke.code) && !ke.ctrlKey && !ke.metaKey && !ke.altKey && !isEditableTarget(ke.target)) {
2147
+ ke.preventDefault();
2148
+ }
2149
+ if (!keysDown.has(ke.code)) {
2150
+ latchedKeysDown.add(ke.code);
2151
+ }
2152
+ keysDown.add(ke.code);
2043
2153
  };
2044
2154
  const onKeyUp = (e) => {
2045
2155
  const ke = e;
@@ -2048,7 +2158,10 @@ function createActionTracker(spec) {
2048
2158
  const onPointerDown = (e) => {
2049
2159
  const pe = e;
2050
2160
  const mapped = mapMouseButton(pe.button);
2051
- if (mapped) mouseDown.add(mapped);
2161
+ if (mapped) {
2162
+ if (!mouseDown.has(mapped)) latchedMouseDown.add(mapped);
2163
+ mouseDown.add(mapped);
2164
+ }
2052
2165
  };
2053
2166
  const onPointerUp = (e) => {
2054
2167
  const pe = e;
@@ -2057,7 +2170,9 @@ function createActionTracker(spec) {
2057
2170
  };
2058
2171
  const onBlur = () => {
2059
2172
  keysDown.clear();
2173
+ latchedKeysDown.clear();
2060
2174
  mouseDown.clear();
2175
+ latchedMouseDown.clear();
2061
2176
  };
2062
2177
  const shouldAttach = spec.attachWindowListeners !== false && typeof window !== "undefined";
2063
2178
  const mouseTarget = spec.mouseTarget ?? (typeof window !== "undefined" ? window : void 0);
@@ -2077,10 +2192,14 @@ function createActionTracker(spec) {
2077
2192
  };
2078
2193
  return {
2079
2194
  update(input, nowMs = typeof performance !== "undefined" ? performance.now() : Date.now()) {
2195
+ const effectiveKeys = latchedKeysDown.size > 0 ? /* @__PURE__ */ new Set([...keysDown, ...latchedKeysDown]) : keysDown;
2196
+ const effectiveMouse = latchedMouseDown.size > 0 ? /* @__PURE__ */ new Set([...mouseDown, ...latchedMouseDown]) : mouseDown;
2197
+ latchedKeysDown.clear();
2198
+ latchedMouseDown.clear();
2080
2199
  for (const [name, bindings] of Object.entries(actionsDef)) {
2081
2200
  let maxVal = 0;
2082
2201
  for (const b of bindings) {
2083
- const v = evalBindingValue(b, input, keysDown, mouseDown, threshold);
2202
+ const v = evalBindingValue(b, input, effectiveKeys, effectiveMouse, threshold);
2084
2203
  if (v > maxVal) maxVal = v;
2085
2204
  }
2086
2205
  const pressed = maxVal >= threshold;
@@ -2099,18 +2218,19 @@ function createActionTracker(spec) {
2099
2218
  prev.heldMs = pressed && startAt !== void 0 ? Math.max(0, nowMs - startAt) : 0;
2100
2219
  prev.value = Math.max(0, Math.min(1, maxVal));
2101
2220
  }
2221
+ const useControllerInput = input.source !== "keyboard_mouse";
2102
2222
  for (const [name, def] of Object.entries(axesDef)) {
2103
2223
  let value = 0;
2104
- if (def.stick) {
2224
+ if (def.stick && useControllerInput) {
2105
2225
  const raw = input[def.stick];
2106
2226
  const dz = def.deadzone ?? 0.08;
2107
2227
  value = applyDeadzone(raw, dz);
2108
2228
  }
2109
2229
  let digital = 0;
2110
- if (def.negativeButtons?.some((b) => input.buttons[b])) digital -= 1;
2111
- if (def.positiveButtons?.some((b) => input.buttons[b])) digital += 1;
2112
- if (def.negativeKeys?.some((k) => keysDown.has(k))) digital -= 1;
2113
- if (def.positiveKeys?.some((k) => keysDown.has(k))) digital += 1;
2230
+ if (useControllerInput && def.negativeButtons?.some((b) => input.buttons[b])) digital -= 1;
2231
+ if (useControllerInput && def.positiveButtons?.some((b) => input.buttons[b])) digital += 1;
2232
+ if (def.negativeKeys?.some((k) => effectiveKeys.has(k))) digital -= 1;
2233
+ if (def.positiveKeys?.some((k) => effectiveKeys.has(k))) digital += 1;
2114
2234
  const combined = Math.abs(digital) > Math.abs(value) ? digital : value;
2115
2235
  const clamped = Math.max(-1, Math.min(1, combined));
2116
2236
  axisValues.set(name, def.invert ? -clamped : clamped);
@@ -2132,12 +2252,20 @@ function createActionTracker(spec) {
2132
2252
  return axisValues.get(name) ?? 0;
2133
2253
  },
2134
2254
  setKeyDown(code, down) {
2135
- if (down) keysDown.add(code);
2136
- else keysDown.delete(code);
2255
+ if (down) {
2256
+ if (!keysDown.has(code)) latchedKeysDown.add(code);
2257
+ keysDown.add(code);
2258
+ } else {
2259
+ keysDown.delete(code);
2260
+ }
2137
2261
  },
2138
2262
  setMouseButton(button, down) {
2139
- if (down) mouseDown.add(button);
2140
- else mouseDown.delete(button);
2263
+ if (down) {
2264
+ if (!mouseDown.has(button)) latchedMouseDown.add(button);
2265
+ mouseDown.add(button);
2266
+ } else {
2267
+ mouseDown.delete(button);
2268
+ }
2141
2269
  },
2142
2270
  dispose() {
2143
2271
  if (shouldAttach) {
@@ -2148,7 +2276,9 @@ function createActionTracker(spec) {
2148
2276
  window.removeEventListener("pointerup", onPointerUp);
2149
2277
  }
2150
2278
  keysDown.clear();
2279
+ latchedKeysDown.clear();
2151
2280
  mouseDown.clear();
2281
+ latchedMouseDown.clear();
2152
2282
  pressedAt.clear();
2153
2283
  }
2154
2284
  };
@@ -2263,6 +2393,7 @@ function createGamepadly(options = {}) {
2263
2393
  const createPlayerHandle = (target) => {
2264
2394
  let lastLightbarColor = [23, 23, 23];
2265
2395
  let lastLightbarSentAt = 0;
2396
+ let activeFlashTimer = null;
2266
2397
  const resolveTarget = () => {
2267
2398
  if (target === "active") {
2268
2399
  return { slotId: void 0, connected: true, isPrimary: true };
@@ -2433,7 +2564,11 @@ function createGamepadly(options = {}) {
2433
2564
  if (lastLightbarColor[0] === rCol && lastLightbarColor[1] === gCol && lastLightbarColor[2] === bCol) {
2434
2565
  return;
2435
2566
  }
2436
- if (now - lastLightbarSentAt < 45) return;
2567
+ if (activeFlashTimer !== null) {
2568
+ lastLightbarColor = [rCol, gCol, bCol];
2569
+ return;
2570
+ }
2571
+ if (now - lastLightbarSentAt < 30) return;
2437
2572
  lastLightbarSentAt = now;
2438
2573
  lastLightbarColor = [rCol, gCol, bCol];
2439
2574
  const r = resolveTarget();
@@ -2442,17 +2577,24 @@ function createGamepadly(options = {}) {
2442
2577
  },
2443
2578
  flash: (rCol, gCol, bCol, durationMs = 240) => {
2444
2579
  if (disposed) return;
2445
- const prev = [...lastLightbarColor];
2580
+ if (activeFlashTimer !== null) {
2581
+ clearTimeout(activeFlashTimer);
2582
+ timers.delete(activeFlashTimer);
2583
+ activeFlashTimer = null;
2584
+ }
2446
2585
  const r = resolveTarget();
2447
2586
  hid.setLightbar(rCol, gCol, bCol, r.slotId).catch(() => {
2448
2587
  });
2449
2588
  const t = setTimeout(() => {
2450
2589
  timers.delete(t);
2590
+ if (activeFlashTimer === t) activeFlashTimer = null;
2451
2591
  if (!disposed) {
2452
- hid.setLightbar(prev[0], prev[1], prev[2], r.slotId).catch(() => {
2592
+ const latest = lastLightbarColor;
2593
+ hid.setLightbar(latest[0], latest[1], latest[2], r.slotId).catch(() => {
2453
2594
  });
2454
2595
  }
2455
2596
  }, durationMs);
2597
+ activeFlashTimer = t;
2456
2598
  timers.add(t);
2457
2599
  }
2458
2600
  },
@@ -3269,77 +3411,110 @@ var ThreeLayerAudioEngine = class _ThreeLayerAudioEngine {
3269
3411
  createContinuous(preset) {
3270
3412
  let stopped = false;
3271
3413
  let dsNodes = null;
3272
- if (this.dsCtx && this.dsChGains.length >= 4) {
3273
- const ctx = this.dsCtx;
3274
- const osc = ctx.createOscillator();
3275
- const filter = ctx.createBiquadFilter();
3276
- const master = ctx.createGain();
3277
- const gL = ctx.createGain();
3278
- const gR = ctx.createGain();
3279
- const spkGain = ctx.createGain();
3280
- master.gain.value = 1e-4;
3281
- gL.gain.value = 0.7;
3282
- gR.gain.value = 0.7;
3283
- spkGain.gain.value = 1e-4;
3284
- const baseCfg = {
3285
- ice: { wave: "triangle", f: 240, filtF: 310, spk: 0.25 },
3286
- metal: { wave: "sine", f: 150, filtF: 180, spk: 0.35 },
3287
- gravel: { wave: "sawtooth", f: 82, filtF: 150, spk: 0.2 },
3288
- grass: { wave: "sine", f: 65, filtF: 110, spk: 0.1 },
3289
- water: { wave: "sine", f: 48, filtF: 85, spk: 0.18 },
3290
- engine: { wave: "sawtooth", f: 42, filtF: 160, spk: 0.05 },
3291
- tension: { wave: "sawtooth", f: 78, filtF: 210, spk: 0.4 }
3292
- };
3293
- const c = baseCfg[preset];
3294
- osc.type = c.wave;
3295
- osc.frequency.value = c.f;
3296
- filter.type = "lowpass";
3297
- filter.frequency.value = c.filtF;
3298
- let noise;
3299
- if (this.dsNoiseBuffer && (preset === "water" || preset === "gravel" || preset === "ice")) {
3300
- noise = ctx.createBufferSource();
3301
- noise.buffer = this.dsNoiseBuffer;
3302
- noise.loop = true;
3303
- const nGain = ctx.createGain();
3304
- nGain.gain.value = 0.45;
3305
- noise.connect(nGain);
3306
- nGain.connect(filter);
3307
- noise.start();
3308
- }
3309
- osc.connect(filter);
3310
- filter.connect(master);
3311
- master.connect(gL);
3312
- master.connect(gR);
3313
- gL.connect(this.dsChGains[2]);
3314
- gR.connect(this.dsChGains[3]);
3315
- master.connect(spkGain);
3316
- spkGain.connect(this.dsChGains[0]);
3317
- spkGain.connect(this.dsChGains[1]);
3318
- spkGain.gain.value = c.spk;
3319
- osc.start();
3320
- dsNodes = { osc, noise, filter, master, gL, gR, spkGain };
3321
- }
3322
3414
  let pcNodes = null;
3323
- if (this.pcCtx && this.pcMasterGain && (preset === "engine" || preset === "tension" || preset === "water")) {
3324
- const ctx = this.pcCtx;
3325
- const osc = ctx.createOscillator();
3326
- osc.type = preset === "water" ? "sine" : "triangle";
3327
- osc.frequency.value = preset === "engine" ? 75 : preset === "tension" ? 210 : 120;
3328
- const gain = ctx.createGain();
3329
- gain.gain.value = 1e-4;
3330
- const panner = ctx.createStereoPanner();
3331
- osc.connect(gain);
3332
- gain.connect(panner);
3333
- panner.connect(this.pcMasterGain);
3334
- osc.start();
3335
- pcNodes = { osc, gain, panner };
3336
- }
3415
+ const cleanupDsNodes = () => {
3416
+ if (!dsNodes) return;
3417
+ try {
3418
+ dsNodes.osc.stop();
3419
+ dsNodes.noise?.stop();
3420
+ dsNodes.master.disconnect();
3421
+ dsNodes.spkGain.disconnect();
3422
+ } catch {
3423
+ }
3424
+ dsNodes = null;
3425
+ };
3426
+ const cleanupPcNodes = () => {
3427
+ if (!pcNodes) return;
3428
+ try {
3429
+ pcNodes.osc.stop();
3430
+ pcNodes.gain.disconnect();
3431
+ } catch {
3432
+ }
3433
+ pcNodes = null;
3434
+ };
3435
+ const ensureNodes = () => {
3436
+ if (stopped) return;
3437
+ if (dsNodes && dsNodes.ctx !== this.dsCtx) {
3438
+ cleanupDsNodes();
3439
+ }
3440
+ if (!dsNodes && this.dsCtx && this.dsChGains.length >= 4) {
3441
+ const ctx = this.dsCtx;
3442
+ const osc = ctx.createOscillator();
3443
+ const filter = ctx.createBiquadFilter();
3444
+ const master = ctx.createGain();
3445
+ const gL = ctx.createGain();
3446
+ const gR = ctx.createGain();
3447
+ const spkGain = ctx.createGain();
3448
+ master.gain.value = 1e-4;
3449
+ gL.gain.value = 0.7;
3450
+ gR.gain.value = 0.7;
3451
+ spkGain.gain.value = 1e-4;
3452
+ const baseCfg = {
3453
+ ice: { wave: "triangle", f: 240, filtF: 310, spk: 0.25 },
3454
+ metal: { wave: "sine", f: 150, filtF: 180, spk: 0.35 },
3455
+ gravel: { wave: "sawtooth", f: 82, filtF: 150, spk: 0.2 },
3456
+ grass: { wave: "sine", f: 65, filtF: 110, spk: 0.1 },
3457
+ water: { wave: "sine", f: 48, filtF: 85, spk: 0.18 },
3458
+ engine: { wave: "sawtooth", f: 42, filtF: 160, spk: 0.05 },
3459
+ tension: { wave: "sawtooth", f: 78, filtF: 210, spk: 0.4 }
3460
+ };
3461
+ const c = baseCfg[preset];
3462
+ osc.type = c.wave;
3463
+ osc.frequency.value = c.f;
3464
+ filter.type = "lowpass";
3465
+ filter.frequency.value = c.filtF;
3466
+ let noise;
3467
+ if (this.dsNoiseBuffer && (preset === "water" || preset === "gravel" || preset === "ice")) {
3468
+ noise = ctx.createBufferSource();
3469
+ noise.buffer = this.dsNoiseBuffer;
3470
+ noise.loop = true;
3471
+ const nGain = ctx.createGain();
3472
+ nGain.gain.value = 0.45;
3473
+ noise.connect(nGain);
3474
+ nGain.connect(filter);
3475
+ noise.start();
3476
+ }
3477
+ osc.connect(filter);
3478
+ filter.connect(master);
3479
+ master.connect(gL);
3480
+ master.connect(gR);
3481
+ gL.connect(this.dsChGains[2]);
3482
+ gR.connect(this.dsChGains[3]);
3483
+ master.connect(spkGain);
3484
+ spkGain.connect(this.dsChGains[0]);
3485
+ spkGain.connect(this.dsChGains[1]);
3486
+ spkGain.gain.value = c.spk;
3487
+ osc.start();
3488
+ dsNodes = { ctx, osc, noise, filter, master, gL, gR, spkGain };
3489
+ }
3490
+ if (pcNodes && pcNodes.ctx !== this.pcCtx) {
3491
+ cleanupPcNodes();
3492
+ }
3493
+ if (!pcNodes && this.pcCtx && this.pcMasterGain && (preset === "engine" || preset === "tension" || preset === "water")) {
3494
+ const ctx = this.pcCtx;
3495
+ const osc = ctx.createOscillator();
3496
+ osc.type = preset === "water" ? "sine" : "triangle";
3497
+ osc.frequency.value = preset === "engine" ? 75 : preset === "tension" ? 210 : 120;
3498
+ const gain = ctx.createGain();
3499
+ gain.gain.value = 1e-4;
3500
+ const panner = ctx.createStereoPanner();
3501
+ osc.connect(gain);
3502
+ gain.connect(panner);
3503
+ panner.connect(this.pcMasterGain);
3504
+ osc.start();
3505
+ pcNodes = { ctx, osc, gain, panner };
3506
+ }
3507
+ };
3508
+ ensureNodes();
3337
3509
  const handle = {
3338
3510
  update: ({ intensity = 0, speed = 0.5, pan = 0, freqScale = 1 } = {}) => {
3339
3511
  if (stopped) return;
3512
+ if (intensity > 5e-3) {
3513
+ ensureNodes();
3514
+ }
3340
3515
  const amp = Math.max(1e-4, Math.min(1, intensity));
3341
3516
  const p = Math.max(-1, Math.min(1, pan));
3342
- if (dsNodes && this.dsCtx) {
3517
+ if (dsNodes && this.dsCtx && dsNodes.ctx === this.dsCtx) {
3343
3518
  const now = this.dsCtx.currentTime;
3344
3519
  dsNodes.master.gain.setTargetAtTime(amp * 0.85, now, 0.025);
3345
3520
  dsNodes.gL.gain.setTargetAtTime(Math.cos((p + 1) * Math.PI / 4), now, 0.025);
@@ -3347,7 +3522,7 @@ var ThreeLayerAudioEngine = class _ThreeLayerAudioEngine {
3347
3522
  const baseF = preset === "tension" ? 68 + speed * 150 : preset === "engine" ? 35 + speed * 95 : 85 + speed * 140;
3348
3523
  dsNodes.osc.frequency.setTargetAtTime(Math.max(25, baseF * freqScale), now, 0.03);
3349
3524
  }
3350
- if (pcNodes && this.pcCtx) {
3525
+ if (pcNodes && this.pcCtx && pcNodes.ctx === this.pcCtx) {
3351
3526
  const now = this.pcCtx.currentTime;
3352
3527
  pcNodes.gain.gain.setTargetAtTime(amp * 0.22, now, 0.03);
3353
3528
  pcNodes.panner.pan.setTargetAtTime(p * 0.7, now, 0.03);
@@ -3359,19 +3534,8 @@ var ThreeLayerAudioEngine = class _ThreeLayerAudioEngine {
3359
3534
  if (stopped) return;
3360
3535
  stopped = true;
3361
3536
  this.activeContinuous.delete(handle);
3362
- try {
3363
- if (dsNodes) {
3364
- dsNodes.osc.stop();
3365
- dsNodes.noise?.stop();
3366
- dsNodes.master.disconnect();
3367
- dsNodes.spkGain.disconnect();
3368
- }
3369
- if (pcNodes) {
3370
- pcNodes.osc.stop();
3371
- pcNodes.gain.disconnect();
3372
- }
3373
- } catch {
3374
- }
3537
+ cleanupDsNodes();
3538
+ cleanupPcNodes();
3375
3539
  }
3376
3540
  };
3377
3541
  this.activeContinuous.add(handle);