mode_xinput.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // mode_xinput.cpp -- Xbox 360 wired (XInput) personality (MODE_XBOX): one XInput gamepad per bonded controller.
  2. //
  3. // Real Xbox 360 pads are NOT HID: they use a vendor interface (class 0xFF / sub 0x5D / proto 0x01) carrying a
  4. // 20-byte XInput report. A custom TinyUSB class driver serves that interface plus a second boot-mouse interface
  5. // for the right trackpad. Host binds by VID/PID (045E:028E) + the FF/5D/01 interface. The OUT endpoint carries
  6. // rumble, relayed to the controller as a haptic by task().
  7. //
  8. // Multi-controller: one XInput interface per CONNECTED controller (dynamic mount -- see usb_mount.h; the device
  9. // re-enumerates without rebooting as controllers connect/disconnect). The single class driver routes xfer/open
  10. // callbacks by endpoint address into the per-bond-slot state; xi_open claims slots in connection order. The
  11. // right-pad mouse is a single shared interface (only bond slot 0's right-pad drives it).
  12. #include "mode_xinput.h"
  13. #include "triton.h"
  14. #include "gamepad_util.h"
  15. #include "config.h"
  16. #include "haptics.h"
  17. #include "bonds.h"
  18. #include "usb_mount.h"
  19. #include "usb_tx.h"
  20. #include <Adafruit_TinyUSB.h>
  21. #include <Arduino.h>
  22. #include <string.h>
  23. // custom class-driver API for the XInput interface
  24. extern "C" {
  25. #include "device/usbd_pvt.h"
  26. }
  27. XboxController g_xboxCtl;
  28. // XInput button bits
  29. enum {
  30. XB_DUP = 0x0001,
  31. XB_DDOWN = 0x0002,
  32. XB_DLEFT = 0x0004,
  33. XB_DRIGHT = 0x0008,
  34. XB_START = 0x0010,
  35. XB_BACK = 0x0020,
  36. XB_L3 = 0x0040,
  37. XB_R3 = 0x0080,
  38. XB_LB = 0x0100,
  39. XB_RB = 0x0200,
  40. XB_GUIDE = 0x0400,
  41. XB_A = 0x1000,
  42. XB_B = 0x2000,
  43. XB_X = 0x4000,
  44. XB_Y = 0x8000
  45. };
  46. // ===================== XInput custom TinyUSB class driver =====================
  47. // Custom class driver + an Adafruit_USBD_Interface subclass emitting the interface + 0x21-blob + 2 endpoints.
  48. // Per-slot state lives in g_xiSlot[]: each XInput interface gets one. The class driver dispatches open/xfer
  49. // by endpoint address; xinputSend() targets a specific slot's IN endpoint.
  50. #define XINPUT_DESC_LEN \
  51. (9 + 17 + 7 + 7) // interface(9)+vendor0x21(17)+IN ep(7)+OUT ep(7) = 40
  52. // per-slot state. `inUse` is set in xi_open from the USB ISR and read in xinputSend from the loop -- mark
  53. // volatile so the loop doesn't observe a stale 0 across a freshly-attached interface.
  54. struct XiSlot {
  55. uint8_t itf, epIn, epOut;
  56. uint8_t inBuf[32], outBuf[32];
  57. volatile uint16_t rumbleLow,
  58. rumbleHigh; // last host rumble values, x 257
  59. volatile unsigned long
  60. rumbleMs; // millis of last OUT packet (stuck-rumble watchdog)
  61. volatile bool inUse;
  62. // inBuf holds a built-but-not-yet-sent 20B XInput report; the actual usbd_edpt_xfer is issued from the
  63. // usbd task (usbTxDrainHook), never from loop(), so the cross-task blocking-defer can't stall the loop.
  64. volatile bool txPending;
  65. };
  66. static XiSlot g_xiSlot[NSLOT];
  67. // Dynamic mount: xi_open is called once per XInput interface in descriptor order; this counts them so the
  68. // u-th interface claims the u-th connected controller (g_usbToBond[u]). Reset on every (re)enumeration.
  69. static uint8_t g_xiOpenU = 0;
  70. // release a held rumble if no OUT packet refreshes it for this long (covers a lost stop)
  71. #define RUMBLE_STUCK_MS 2500u
  72. static void xi_init(void)
  73. {
  74. for (int s = 0; s < NSLOT; s++)
  75. g_xiSlot[s].inUse = false;
  76. }
  77. static bool xi_deinit(void)
  78. {
  79. return true;
  80. }
  81. static void xi_reset(uint8_t rhport)
  82. {
  83. (void)rhport;
  84. g_xiOpenU =
  85. 0; // restart per-interface claim ordering for this enumeration
  86. for (int s = 0; s < NSLOT; s++) {
  87. g_xiSlot[s].itf = 0;
  88. g_xiSlot[s].epIn = g_xiSlot[s].epOut = 0;
  89. g_xiSlot[s].inUse = false;
  90. }
  91. }
  92. // TinyUSB calls xi_open once per XInput interface in the config descriptor. Claim the first free BONDED
  93. // slot so the xiSlot index matches the bond-slot index (begin() registers them in ascending bond-slot order).
  94. // Fallback to any free slot when there are no bonds (fresh device).
  95. static uint16_t xi_open(uint8_t rhport, tusb_desc_interface_t const *itf,
  96. uint16_t max_len)
  97. {
  98. if (!(itf->bInterfaceClass == 0xFF && itf->bInterfaceSubClass == 0x5D &&
  99. itf->bInterfaceProtocol == 0x01))
  100. return 0;
  101. // The u-th XInput interface in the descriptor serves the u-th connected controller. g_xiSlot is keyed by
  102. // BOND slot so onReport45(bond)/rumble route straight through; only connected bonds get an interface.
  103. uint8_t u = g_xiOpenU++;
  104. int slot = (u < g_usbMountCount) ? g_usbToBond[u] : -1;
  105. if (slot < 0) { // fallback (e.g. map not built): first free slot
  106. for (int s = 0; s < NSLOT; s++)
  107. if (!g_xiSlot[s].inUse) {
  108. slot = s;
  109. break;
  110. }
  111. }
  112. if (slot < 0 || slot >= NSLOT || g_xiSlot[slot].inUse)
  113. return 0;
  114. XiSlot &S = g_xiSlot[slot];
  115. S.itf = itf->bInterfaceNumber;
  116. uint8_t const *p = (uint8_t const *)itf;
  117. uint8_t const *end = p + max_len;
  118. uint16_t used = itf->bLength;
  119. p += itf->bLength;
  120. uint8_t opened = 0;
  121. while (p < end && opened < itf->bNumEndpoints) {
  122. uint8_t blen = p[0], btype = p[1];
  123. if (btype == TUSB_DESC_ENDPOINT) {
  124. tusb_desc_endpoint_t const *ep =
  125. (tusb_desc_endpoint_t const *)p;
  126. usbd_edpt_open(rhport, ep);
  127. if (tu_edpt_dir(ep->bEndpointAddress) == TUSB_DIR_IN)
  128. S.epIn = ep->bEndpointAddress;
  129. else
  130. S.epOut = ep->bEndpointAddress;
  131. opened++;
  132. }
  133. used += blen;
  134. p += blen;
  135. }
  136. S.inUse = true;
  137. // arm OUT (rumble/LED)
  138. if (S.epOut)
  139. usbd_edpt_xfer(rhport, S.epOut, S.outBuf, sizeof S.outBuf);
  140. return used;
  141. }
  142. static bool xi_ctrl(uint8_t rhport, uint8_t stage,
  143. tusb_control_request_t const *req)
  144. {
  145. (void)rhport;
  146. (void)req;
  147. return stage != CONTROL_STAGE_SETUP;
  148. }
  149. // Route an endpoint xfer callback to the slot that owns that endpoint.
  150. static bool xi_xfer(uint8_t rhport, uint8_t ep, xfer_result_t res, uint32_t n)
  151. {
  152. (void)res;
  153. for (int s = 0; s < NSLOT; s++) {
  154. XiSlot &S = g_xiSlot[s];
  155. if (!S.inUse)
  156. continue;
  157. if (ep == S.epOut) {
  158. // XInput rumble packet: [00][08][00][bigMotor][smallMotor][00][00][00]; LED pkt is [01][03][led]
  159. if (n >= 5 && S.outBuf[0] == 0x00 &&
  160. S.outBuf[1] == 0x08) {
  161. uint8_t big = S.outBuf[3], sml = S.outBuf[4];
  162. uint16_t lo = (uint16_t)big * 257u;
  163. uint16_t hi = (uint16_t)sml * 257u;
  164. S.rumbleLow = lo;
  165. S.rumbleHigh = hi;
  166. S.rumbleMs = millis();
  167. hapticSteamRumble(lo, hi, (uint8_t)s);
  168. }
  169. usbd_edpt_xfer(rhport, S.epOut, S.outBuf,
  170. sizeof S.outBuf);
  171. return true;
  172. }
  173. }
  174. return true;
  175. }
  176. static const usbd_class_driver_t g_xiDriver = {
  177. #if CFG_TUSB_DEBUG >= 2
  178. .name = "XINPUT",
  179. #endif
  180. .init = xi_init,
  181. .deinit = xi_deinit,
  182. .reset = xi_reset,
  183. .open = xi_open,
  184. .control_xfer_cb = xi_ctrl,
  185. .xfer_cb = xi_xfer,
  186. .sof = NULL
  187. };
  188. extern "C" const usbd_class_driver_t *usbd_app_driver_get_cb(uint8_t *count)
  189. {
  190. *count = 1;
  191. return &g_xiDriver;
  192. }
  193. class Adafruit_USBD_XInput : public Adafruit_USBD_Interface {
  194. public:
  195. uint16_t getInterfaceDescriptor(uint8_t, uint8_t *buf,
  196. uint16_t bufsize) override
  197. {
  198. if (!buf)
  199. return XINPUT_DESC_LEN;
  200. if (bufsize < XINPUT_DESC_LEN)
  201. return 0;
  202. uint8_t itfnum = TinyUSBDevice.allocInterface(1);
  203. uint8_t epin = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN),
  204. epout = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
  205. const uint8_t t[XINPUT_DESC_LEN] = {
  206. 9, TUSB_DESC_INTERFACE, itfnum, 0x00, 0x02, 0xFF, 0x5D,
  207. 0x01, _strid, 0x11, 0x21, 0x00, 0x01, 0x01, 0x25, epin,
  208. 0x14, 0x00, 0x00, 0x00, 0x00, 0x13, epout, 0x08, 0x00,
  209. 0x00, 7, TUSB_DESC_ENDPOINT, epin, TUSB_XFER_INTERRUPT,
  210. U16_TO_U8S_LE(0x20),
  211. // bInterval 1ms (1000Hz) so the RF rate is the only limit
  212. 1, 7, TUSB_DESC_ENDPOINT, epout, TUSB_XFER_INTERRUPT,
  213. U16_TO_U8S_LE(0x20), 8
  214. };
  215. memcpy(buf, t, XINPUT_DESC_LEN);
  216. return XINPUT_DESC_LEN;
  217. }
  218. bool begin()
  219. {
  220. return TinyUSBDevice.addInterface(*this);
  221. }
  222. };
  223. // NSLOT instances: each registers its own XInput interface (own itfnum + IN/OUT ep pair) during begin().
  224. // begin() in XboxController::begin() runs in order 0..NSLOT-1, so xi_open's first-free assignment lines up.
  225. static Adafruit_USBD_XInput g_xinput[NSLOT];
  226. static void xinputSend(uint8_t slot, uint16_t buttons, uint8_t lt, uint8_t rt,
  227. int16_t lx, int16_t ly, int16_t rx, int16_t ry)
  228. {
  229. if (slot >= NSLOT || !g_xiSlot[slot].inUse)
  230. return;
  231. XiSlot &S = g_xiSlot[slot];
  232. // Only refill inBuf when the IN endpoint is idle: while a transfer is in flight (busy) the DMA is still
  233. // reading inBuf, so leave it untouched (this also coalesces -- newest state wins, like a real pad).
  234. if (!tud_mounted() || S.epIn == 0 || usbd_edpt_busy(0, S.epIn))
  235. return;
  236. uint8_t *r = S.inBuf;
  237. r[0] = 0x00;
  238. r[1] = 0x14;
  239. r[2] = buttons & 0xFF;
  240. r[3] = buttons >> 8;
  241. r[4] = lt;
  242. r[5] = rt;
  243. r[6] = lx & 0xFF;
  244. r[7] = lx >> 8;
  245. r[8] = ly & 0xFF;
  246. r[9] = ly >> 8;
  247. r[10] = rx & 0xFF;
  248. r[11] = rx >> 8;
  249. r[12] = ry & 0xFF;
  250. r[13] = ry >> 8;
  251. memset(r + 14, 0, 6);
  252. // Hand off to the usbd task: the xfer is issued from xiSofDrain() (SOF), not here, so loop() never
  253. // calls into the dcd transfer path (which can block on the device event queue under load).
  254. S.txPending = true;
  255. }
  256. // usbd-task drain (registered with usbTxRegisterDrain, called from tud_sof_cb). Issues the deferred XInput IN
  257. // transfers. Runs at higher priority than loop(), so the claim->xfer is atomic w.r.t. xinputSend() refilling
  258. // inBuf -- no torn DMA.
  259. static void xiSofDrain(void)
  260. {
  261. if (!tud_mounted())
  262. return;
  263. for (int s = 0; s < NSLOT; s++) {
  264. XiSlot &S = g_xiSlot[s];
  265. if (!S.inUse || !S.txPending || S.epIn == 0)
  266. continue;
  267. if (usbd_edpt_busy(0, S.epIn))
  268. continue;
  269. if (usbd_edpt_claim(0, S.epIn)) {
  270. if (usbd_edpt_xfer(0, S.epIn, S.inBuf, 20))
  271. S.txPending = false;
  272. else
  273. usbd_edpt_release(0, S.epIn);
  274. }
  275. }
  276. }
  277. // ===================== right-pad mouse interface =====================
  278. static const uint8_t MOUSE_HID_DESC[] = { TUD_HID_REPORT_DESC_MOUSE() };
  279. static Adafruit_USBD_HID
  280. g_mouse; // Xbox-mode mouse interface (right trackpad, slot 0 only)
  281. // ===================== report 0x45 -> XInput + mouse =====================
  282. // button code (g_back[], g_abSwap targets) -> legacy XInput bit. 0=none 1=A 2=B 3=X 4=Y 5=LB 6=RB 7=L3 8=R3 9=Back 10=Start 11=Guide 12=Dup 13=Ddown 14=Dleft 15=Dright
  283. static uint16_t codeToXB(uint8_t c)
  284. {
  285. switch (c) {
  286. case 1:
  287. return XB_A;
  288. case 2:
  289. return XB_B;
  290. case 3:
  291. return XB_X;
  292. case 4:
  293. return XB_Y;
  294. case 5:
  295. return XB_LB;
  296. case 6:
  297. return XB_RB;
  298. case 7:
  299. return XB_L3;
  300. case 8:
  301. return XB_R3;
  302. case 9:
  303. return XB_BACK;
  304. case 10:
  305. return XB_START;
  306. case 11:
  307. return XB_GUIDE;
  308. case 12:
  309. return XB_DUP;
  310. case 13:
  311. return XB_DDOWN;
  312. case 14:
  313. return XB_DLEFT;
  314. case 15:
  315. return XB_DRIGHT;
  316. default:
  317. return 0;
  318. }
  319. }
  320. static void rfXboxGamepad(uint8_t slot, const uint8_t *r)
  321. {
  322. uint32_t b = btnsOf(r);
  323. if (g_qamMap && (b & TB_QAM)) {
  324. b &= ~(uint32_t)TB_QAM;
  325. b |= tritonFromCode(g_qamMap);
  326. }
  327. uint16_t btn = 0;
  328. if (b & TB_DUP)
  329. btn |= XB_DUP;
  330. if (b & TB_DDN)
  331. btn |= XB_DDOWN;
  332. if (b & TB_DLF)
  333. btn |= XB_DLEFT;
  334. if (b & TB_DRT)
  335. btn |= XB_DRIGHT;
  336. if (b & TB_VIEW)
  337. btn |= XB_START;
  338. if (b & TB_MENU)
  339. btn |= XB_BACK;
  340. if (b & TB_STEAM)
  341. btn |= XB_GUIDE;
  342. if (b & TB_LB)
  343. btn |= XB_LB;
  344. if (b & TB_RB)
  345. btn |= XB_RB;
  346. if (b & TB_L3)
  347. btn |= XB_L3;
  348. if (b & TB_R3)
  349. btn |= XB_R3;
  350. // face buttons, with optional A/B + X/Y swap (Nintendo layout)
  351. uint16_t fA = g_abSwap ? XB_B : XB_A, fB = g_abSwap ? XB_A : XB_B,
  352. fX = g_abSwap ? XB_Y : XB_X, fY = g_abSwap ? XB_X : XB_Y;
  353. if (b & TB_A)
  354. btn |= fA;
  355. if (b & TB_B)
  356. btn |= fB;
  357. if (b & TB_X)
  358. btn |= fX;
  359. if (b & TB_Y)
  360. btn |= fY;
  361. // back paddles -> configurable mapping (default L4->LB, R4->RB, L5->L3, R5->R3)
  362. if (b & TB_L4)
  363. btn |= codeToXB(g_back[0]);
  364. if (b & TB_R4)
  365. btn |= codeToXB(g_back[1]);
  366. if (b & TB_L5)
  367. btn |= codeToXB(g_back[2]);
  368. if (b & TB_R5)
  369. btn |= codeToXB(g_back[3]);
  370. uint8_t lt = trigU8(u16off(r, 4)),
  371. rt = trigU8(u16off(
  372. r, 6)); // triggers u16 (half-scale) -> full-range u8
  373. // Trigger remaps (codes 19=LT, 20=RT): XInput triggers are analog bytes, not buttons, so a back paddle /
  374. // QAM mapped to a trigger pulls it full. QAM arrives as TB_L2/TB_R2 (folded into b via tritonFromCode);
  375. // back paddles are matched by their configured code.
  376. if (b & TB_L2)
  377. lt = 0xFF;
  378. if (b & TB_R2)
  379. rt = 0xFF;
  380. const uint8_t bc[4] = { g_back[0], g_back[1], g_back[2], g_back[3] };
  381. const uint32_t bm[4] = { TB_L4, TB_R4, TB_L5, TB_R5 };
  382. for (int i = 0; i < 4; i++) {
  383. if (!(b & bm[i]))
  384. continue;
  385. if (bc[i] == 19)
  386. lt = 0xFF;
  387. else if (bc[i] == 20)
  388. rt = 0xFF;
  389. }
  390. xinputSend(slot, btn, lt, rt, (int16_t)s16off(r, 8),
  391. (int16_t)s16off(r, 10), // L stick X/Y
  392. (int16_t)s16off(r, 12),
  393. (int16_t)s16off(r, 14)); // R stick X/Y
  394. }
  395. // Right pad -> mouse on a second HID-mouse interface alongside the XInput gamepad. Same glide model as Lizard's
  396. // right pad; RPad click = left button, LPad click = right. SINGLE shared mouse: the desktop can only consume
  397. // one mouse. Slot 0's right-pad drives it; other slots' right-pad input is intentionally ignored here.
  398. static void rfXboxMouse(const uint8_t *r)
  399. {
  400. uint32_t b = btnsOf(r);
  401. static int prx = 0, pry = 0;
  402. static bool prt = false;
  403. static float vx = 0, vy = 0, rmx = 0, rmy = 0;
  404. static uint8_t pmb = 0;
  405. bool rtouch = b & TB_RPADT;
  406. int rx = s16off(r, 22), ry = s16off(r, 24);
  407. if (rtouch) {
  408. if (prt) {
  409. vx += (rx - prx);
  410. vy += (ry - pry);
  411. }
  412. prx = rx;
  413. pry = ry;
  414. }
  415. prt = rtouch;
  416. float mxf = vx / (float)(g_mDiv * 10) + rmx,
  417. myf = -(vy / (float)(g_mDiv * 10)) +
  418. rmy; // Y inverted for screen coords
  419. int dx = (int)mxf, dy = (int)myf;
  420. rmx = mxf - dx;
  421. rmy = myf - dy; // sub-pixel carry
  422. if (dx > 127)
  423. dx = 127;
  424. if (dx < -127)
  425. dx = -127;
  426. if (dy > 127)
  427. dy = 127;
  428. if (dy < -127)
  429. dy = -127;
  430. float f = g_mFric / 100.0f;
  431. vx *= f;
  432. vy *= f;
  433. if (vx > -1 && vx < 1)
  434. vx = 0;
  435. if (vy > -1 && vy < 1)
  436. vy = 0; // friction = glide/decay
  437. uint8_t mb = ((b & TB_RPADC) ? 1 : 0) |
  438. ((b & TB_LPADC) ?
  439. 2 :
  440. 0); // RPad click = left, LPad click = right
  441. if (dx || dy || mb != pmb) {
  442. pmb = mb;
  443. hid_mouse_report_t m;
  444. m.buttons = mb;
  445. m.x = (int8_t)dx;
  446. m.y = (int8_t)dy;
  447. m.wheel = 0;
  448. m.pan = 0;
  449. if (g_mouse.ready())
  450. usbTxHid(&g_mouse, 0, &m, sizeof m);
  451. }
  452. }
  453. // ===================== IController =====================
  454. // Dynamic-mount mode: begin() is unused (setup() calls beginPool()+usbReenumerate instead).
  455. void XboxController::begin()
  456. {
  457. }
  458. // XInput slot interfaces are a custom (non-HID) class, so they don't draw on the CFG_TUD_HID budget (only the
  459. // wake mouse + right-pad mouse do). Slots are limited by NSLOT / USB endpoints, not HID instances.
  460. uint8_t XboxController::maxSlots() const
  461. {
  462. return (uint8_t)NSLOT;
  463. }
  464. void XboxController::usbIdentity()
  465. {
  466. // 045E:028E -> Windows xusb / SDL / Linux xpad all bind it
  467. USBDevice.setID(0x045E, 0x028E);
  468. USBDevice.setDeviceVersion(0x0120);
  469. USBDevice.setManufacturerDescriptor("Microsoft");
  470. USBDevice.setProductDescriptor("Controller");
  471. }
  472. void XboxController::beginPool()
  473. {
  474. // Lock the right-pad mouse HID instance (wake mouse was begun first by setup -> this is HID instance 1).
  475. g_mouse.setStringDescriptor("OpenPuck Mouse");
  476. g_mouse.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
  477. g_mouse.setReportDescriptor(MOUSE_HID_DESC, sizeof MOUSE_HID_DESC);
  478. g_mouse.setPollInterval(1);
  479. g_mouse.begin();
  480. for (int s = 0; s < NSLOT; s++)
  481. g_xinput[s].setStringDescriptor("Controller");
  482. // Drain the deferred XInput IN transfers from the usbd task every SOF (only registered in this mode).
  483. usbTxRegisterDrain(xiSofDrain);
  484. }
  485. void XboxController::mountSlots(uint8_t k)
  486. {
  487. // Right-pad mouse first (fixed HID), then one XInput interface per connected controller (claimed in
  488. // order by xi_open). The wake mouse + WebUSB are added around this by usbReenumerate.
  489. USBDevice.addInterface(g_mouse);
  490. for (uint8_t u = 0; u < k; u++)
  491. USBDevice.addInterface(g_xinput[u]);
  492. }
  493. void XboxController::onReport45(int slot, const uint8_t *rep, bool fresh,
  494. uint8_t bodyTlen)
  495. {
  496. (void)fresh;
  497. (void)bodyTlen;
  498. if (slot < 0 || slot >= NSLOT)
  499. return;
  500. // rfXboxGamepad/rfXboxMouse read report 0x45 field offsets. The new-firmware report 0x42 is VERIFIED
  501. // byte-identical over [0..45] (buttons/triggers/sticks/pads/IMU at the same offsets; it only appends 8
  502. // trailing bytes and sets always-on status bits 28/29 that no TB_ mask reads -- see the rf_link decode
  503. // note), so both ids decode here. Anything else must not be decoded as gamepad input.
  504. if (rep[0] != 0x45 && rep[0] != 0x42)
  505. return;
  506. rfXboxGamepad((uint8_t)slot, rep);
  507. // Shared desktop mouse: slot 0's right-pad only. Other slots' right-pad is still part of the
  508. // per-slot XInput (TB_RPADT in the gamepad report is unused today but kept for forward-compat).
  509. if (slot == 0)
  510. rfXboxMouse(rep);
  511. }
  512. // Lost-stop watchdog per slot: Steam/Triton rumble is latched, so force a zero report if the host stops
  513. // refreshing on a particular XInput. The haptics.cpp global watchdog fires too, but it skips while the slot
  514. // is in the post-reconnect block -- this catches a host that kept streaming while we were blocked.
  515. void XboxController::task()
  516. {
  517. for (int s = 0; s < NSLOT; s++) {
  518. if (!g_xiSlot[s].inUse)
  519. continue;
  520. if ((g_xiSlot[s].rumbleLow || g_xiSlot[s].rumbleHigh) &&
  521. millis() - g_xiSlot[s].rumbleMs > RUMBLE_STUCK_MS) {
  522. g_xiSlot[s].rumbleLow = g_xiSlot[s].rumbleHigh = 0;
  523. hapticSteamRumble(0, 0, (uint8_t)s);
  524. }
  525. }
  526. }