@gamepadly/sdk 0.1.1 → 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-GMU2WEFE.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;