mode_xinput.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. // skip tracking+relay when rumble is disabled for this type
  163. if (g_rumble) {
  164. uint16_t lo = (uint16_t)big * 257u;
  165. uint16_t hi = (uint16_t)sml * 257u;
  166. S.rumbleLow = lo;
  167. S.rumbleHigh = hi;
  168. S.rumbleMs = millis();
  169. hapticSteamRumble(lo, hi, (uint8_t)s);
  170. }
  171. }
  172. usbd_edpt_xfer(rhport, S.epOut, S.outBuf,
  173. sizeof S.outBuf);
  174. return true;
  175. }
  176. }
  177. return true;
  178. }
  179. static const usbd_class_driver_t g_xiDriver = {
  180. #if CFG_TUSB_DEBUG >= 2
  181. .name = "XINPUT",
  182. #endif
  183. .init = xi_init,
  184. .deinit = xi_deinit,
  185. .reset = xi_reset,
  186. .open = xi_open,
  187. .control_xfer_cb = xi_ctrl,
  188. .xfer_cb = xi_xfer,
  189. .sof = NULL
  190. };
  191. extern "C" const usbd_class_driver_t *usbd_app_driver_get_cb(uint8_t *count)
  192. {
  193. *count = 1;
  194. return &g_xiDriver;
  195. }
  196. class Adafruit_USBD_XInput : public Adafruit_USBD_Interface {
  197. public:
  198. uint16_t getInterfaceDescriptor(uint8_t, uint8_t *buf,
  199. uint16_t bufsize) override
  200. {
  201. if (!buf)
  202. return XINPUT_DESC_LEN;
  203. if (bufsize < XINPUT_DESC_LEN)
  204. return 0;
  205. uint8_t itfnum = TinyUSBDevice.allocInterface(1);
  206. uint8_t epin = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN),
  207. epout = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
  208. const uint8_t t[XINPUT_DESC_LEN] = {
  209. 9, TUSB_DESC_INTERFACE, itfnum, 0x00, 0x02, 0xFF, 0x5D,
  210. 0x01, _strid, 0x11, 0x21, 0x00, 0x01, 0x01, 0x25, epin,
  211. 0x14, 0x00, 0x00, 0x00, 0x00, 0x13, epout, 0x08, 0x00,
  212. 0x00, 7, TUSB_DESC_ENDPOINT, epin, TUSB_XFER_INTERRUPT,
  213. U16_TO_U8S_LE(0x20),
  214. // bInterval 1ms (1000Hz) so the RF rate is the only limit
  215. 1, 7, TUSB_DESC_ENDPOINT, epout, TUSB_XFER_INTERRUPT,
  216. U16_TO_U8S_LE(0x20), 8
  217. };
  218. memcpy(buf, t, XINPUT_DESC_LEN);
  219. return XINPUT_DESC_LEN;
  220. }
  221. bool begin()
  222. {
  223. return TinyUSBDevice.addInterface(*this);
  224. }
  225. };
  226. // NSLOT instances: each registers its own XInput interface (own itfnum + IN/OUT ep pair) during begin().
  227. // begin() in XboxController::begin() runs in order 0..NSLOT-1, so xi_open's first-free assignment lines up.
  228. static Adafruit_USBD_XInput g_xinput[NSLOT];
  229. static void xinputSend(uint8_t slot, uint16_t buttons, uint8_t lt, uint8_t rt,
  230. int16_t lx, int16_t ly, int16_t rx, int16_t ry)
  231. {
  232. if (slot >= NSLOT || !g_xiSlot[slot].inUse)
  233. return;
  234. XiSlot &S = g_xiSlot[slot];
  235. // Only refill inBuf when the IN endpoint is idle: while a transfer is in flight (busy) the DMA is still
  236. // reading inBuf, so leave it untouched (this also coalesces -- newest state wins, like a real pad).
  237. if (!tud_mounted() || S.epIn == 0 || usbd_edpt_busy(0, S.epIn))
  238. return;
  239. uint8_t *r = S.inBuf;
  240. r[0] = 0x00;
  241. r[1] = 0x14;
  242. r[2] = buttons & 0xFF;
  243. r[3] = buttons >> 8;
  244. r[4] = lt;
  245. r[5] = rt;
  246. r[6] = lx & 0xFF;
  247. r[7] = lx >> 8;
  248. r[8] = ly & 0xFF;
  249. r[9] = ly >> 8;
  250. r[10] = rx & 0xFF;
  251. r[11] = rx >> 8;
  252. r[12] = ry & 0xFF;
  253. r[13] = ry >> 8;
  254. memset(r + 14, 0, 6);
  255. // Hand off to the usbd task: the xfer is issued from xiSofDrain() (SOF), not here, so loop() never
  256. // calls into the dcd transfer path (which can block on the device event queue under load).
  257. S.txPending = true;
  258. }
  259. // usbd-task drain (registered with usbTxRegisterDrain, called from tud_sof_cb). Issues the deferred XInput IN
  260. // transfers. Runs at higher priority than loop(), so the claim->xfer is atomic w.r.t. xinputSend() refilling
  261. // inBuf -- no torn DMA.
  262. static void xiSofDrain(void)
  263. {
  264. if (!tud_mounted())
  265. return;
  266. for (int s = 0; s < NSLOT; s++) {
  267. XiSlot &S = g_xiSlot[s];
  268. if (!S.inUse || !S.txPending || S.epIn == 0)
  269. continue;
  270. if (usbd_edpt_busy(0, S.epIn))
  271. continue;
  272. if (usbd_edpt_claim(0, S.epIn)) {
  273. if (usbd_edpt_xfer(0, S.epIn, S.inBuf, 20))
  274. S.txPending = false;
  275. else
  276. usbd_edpt_release(0, S.epIn);
  277. }
  278. }
  279. }
  280. // ===================== right-pad mouse interface =====================
  281. static const uint8_t MOUSE_HID_DESC[] = { TUD_HID_REPORT_DESC_MOUSE() };
  282. static Adafruit_USBD_HID
  283. g_mouse; // Xbox-mode mouse interface (right trackpad, slot 0 only)
  284. // ===================== report 0x45 -> XInput + mouse =====================
  285. // 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
  286. static uint16_t codeToXB(uint8_t c)
  287. {
  288. switch (c) {
  289. case 1:
  290. return XB_A;
  291. case 2:
  292. return XB_B;
  293. case 3:
  294. return XB_X;
  295. case 4:
  296. return XB_Y;
  297. case 5:
  298. return XB_LB;
  299. case 6:
  300. return XB_RB;
  301. case 7:
  302. return XB_L3;
  303. case 8:
  304. return XB_R3;
  305. case 9:
  306. return XB_BACK;
  307. case 10:
  308. return XB_START;
  309. case 11:
  310. return XB_GUIDE;
  311. case 12:
  312. return XB_DUP;
  313. case 13:
  314. return XB_DDOWN;
  315. case 14:
  316. return XB_DLEFT;
  317. case 15:
  318. return XB_DRIGHT;
  319. default:
  320. return 0;
  321. }
  322. }
  323. static void rfXboxGamepad(uint8_t slot, const uint8_t *r)
  324. {
  325. uint32_t b = btnsOf(r);
  326. if (g_qamMap && (b & TB_QAM)) {
  327. b &= ~(uint32_t)TB_QAM;
  328. b |= tritonFromCode(g_qamMap);
  329. }
  330. uint16_t btn = 0;
  331. if (b & TB_DUP)
  332. btn |= XB_DUP;
  333. if (b & TB_DDN)
  334. btn |= XB_DDOWN;
  335. if (b & TB_DLF)
  336. btn |= XB_DLEFT;
  337. if (b & TB_DRT)
  338. btn |= XB_DRIGHT;
  339. if (b & TB_VIEW)
  340. btn |= XB_START;
  341. if (b & TB_MENU)
  342. btn |= XB_BACK;
  343. if (b & TB_STEAM)
  344. btn |= XB_GUIDE;
  345. if (b & TB_LB)
  346. btn |= XB_LB;
  347. if (b & TB_RB)
  348. btn |= XB_RB;
  349. if (b & TB_L3)
  350. btn |= XB_L3;
  351. if (b & TB_R3)
  352. btn |= XB_R3;
  353. // face buttons, with optional A/B + X/Y swap (Nintendo layout)
  354. uint16_t fA = g_abSwap ? XB_B : XB_A, fB = g_abSwap ? XB_A : XB_B,
  355. fX = g_abSwap ? XB_Y : XB_X, fY = g_abSwap ? XB_X : XB_Y;
  356. if (b & TB_A)
  357. btn |= fA;
  358. if (b & TB_B)
  359. btn |= fB;
  360. if (b & TB_X)
  361. btn |= fX;
  362. if (b & TB_Y)
  363. btn |= fY;
  364. // back paddles -> configurable mapping (default L4->LB, R4->RB, L5->L3, R5->R3)
  365. if (b & TB_L4)
  366. btn |= codeToXB(g_back[0]);
  367. if (b & TB_R4)
  368. btn |= codeToXB(g_back[1]);
  369. if (b & TB_L5)
  370. btn |= codeToXB(g_back[2]);
  371. if (b & TB_R5)
  372. btn |= codeToXB(g_back[3]);
  373. uint8_t lt = trigU8(u16off(r, 4)),
  374. rt = trigU8(u16off(
  375. r, 6)); // triggers u16 (half-scale) -> full-range u8
  376. // Trigger remaps (codes 19=LT, 20=RT): XInput triggers are analog bytes, not buttons, so a back paddle /
  377. // QAM mapped to a trigger pulls it full. QAM arrives as TB_L2/TB_R2 (folded into b via tritonFromCode);
  378. // back paddles are matched by their configured code.
  379. if (b & TB_L2)
  380. lt = 0xFF;
  381. if (b & TB_R2)
  382. rt = 0xFF;
  383. const uint8_t bc[4] = { g_back[0], g_back[1], g_back[2], g_back[3] };
  384. const uint32_t bm[4] = { TB_L4, TB_R4, TB_L5, TB_R5 };
  385. for (int i = 0; i < 4; i++) {
  386. if (!(b & bm[i]))
  387. continue;
  388. if (bc[i] == 19)
  389. lt = 0xFF;
  390. else if (bc[i] == 20)
  391. rt = 0xFF;
  392. }
  393. xinputSend(slot, btn, lt, rt, (int16_t)s16off(r, 8),
  394. (int16_t)s16off(r, 10), // L stick X/Y
  395. (int16_t)s16off(r, 12),
  396. (int16_t)s16off(r, 14)); // R stick X/Y
  397. }
  398. // Right pad -> mouse on a second HID-mouse interface alongside the XInput gamepad. Same glide model as Lizard's
  399. // right pad; RPad click = left button, LPad click = right. SINGLE shared mouse: the desktop can only consume
  400. // one mouse. Slot 0's right-pad drives it; other slots' right-pad input is intentionally ignored here.
  401. static void rfXboxMouse(const uint8_t *r)
  402. {
  403. uint32_t b = btnsOf(r);
  404. static int prx = 0, pry = 0;
  405. static bool prt = false;
  406. static float vx = 0, vy = 0, rmx = 0, rmy = 0;
  407. static uint8_t pmb = 0;
  408. bool rtouch = b & TB_RPADT;
  409. int rx = s16off(r, 22), ry = s16off(r, 24);
  410. if (rtouch) {
  411. if (prt) {
  412. vx += (rx - prx);
  413. vy += (ry - pry);
  414. }
  415. prx = rx;
  416. pry = ry;
  417. }
  418. prt = rtouch;
  419. float mxf = vx / (float)(g_mDiv * 10) + rmx,
  420. myf = -(vy / (float)(g_mDiv * 10)) +
  421. rmy; // Y inverted for screen coords
  422. int dx = (int)mxf, dy = (int)myf;
  423. rmx = mxf - dx;
  424. rmy = myf - dy; // sub-pixel carry
  425. if (dx > 127)
  426. dx = 127;
  427. if (dx < -127)
  428. dx = -127;
  429. if (dy > 127)
  430. dy = 127;
  431. if (dy < -127)
  432. dy = -127;
  433. float f = g_mFric / 100.0f;
  434. vx *= f;
  435. vy *= f;
  436. if (vx > -1 && vx < 1)
  437. vx = 0;
  438. if (vy > -1 && vy < 1)
  439. vy = 0; // friction = glide/decay
  440. uint8_t mb = ((b & TB_RPADC) ? 1 : 0) |
  441. ((b & TB_LPADC) ?
  442. 2 :
  443. 0); // RPad click = left, LPad click = right
  444. if (dx || dy || mb != pmb) {
  445. pmb = mb;
  446. hid_mouse_report_t m;
  447. m.buttons = mb;
  448. m.x = (int8_t)dx;
  449. m.y = (int8_t)dy;
  450. m.wheel = 0;
  451. m.pan = 0;
  452. if (g_mouse.ready())
  453. usbTxHid(&g_mouse, 0, &m, sizeof m);
  454. }
  455. }
  456. // ===================== IController =====================
  457. // Dynamic-mount mode: begin() is unused (setup() calls beginPool()+usbReenumerate instead).
  458. void XboxController::begin()
  459. {
  460. }
  461. // XInput slot interfaces are a custom (non-HID) class, so they don't draw on the CFG_TUD_HID budget (only the
  462. // wake mouse + right-pad mouse do). Slots are limited by NSLOT / USB endpoints, not HID instances.
  463. uint8_t XboxController::maxSlots() const
  464. {
  465. return (uint8_t)NSLOT;
  466. }
  467. void XboxController::usbIdentity()
  468. {
  469. // 045E:028E -> Windows xusb / SDL / Linux xpad all bind it
  470. USBDevice.setID(0x045E, 0x028E);
  471. USBDevice.setDeviceVersion(0x0120);
  472. USBDevice.setManufacturerDescriptor("Microsoft");
  473. USBDevice.setProductDescriptor("Controller");
  474. }
  475. void XboxController::beginPool()
  476. {
  477. // Lock the right-pad mouse HID instance (wake mouse was begun first by setup -> this is HID instance 1).
  478. g_mouse.setStringDescriptor("OpenPuck Mouse");
  479. g_mouse.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
  480. g_mouse.setReportDescriptor(MOUSE_HID_DESC, sizeof MOUSE_HID_DESC);
  481. g_mouse.setPollInterval(1);
  482. g_mouse.begin();
  483. for (int s = 0; s < NSLOT; s++)
  484. g_xinput[s].setStringDescriptor("Controller");
  485. // Drain the deferred XInput IN transfers from the usbd task every SOF (only registered in this mode).
  486. usbTxRegisterDrain(xiSofDrain);
  487. }
  488. void XboxController::mountSlots(uint8_t k)
  489. {
  490. // Right-pad mouse first (fixed HID), then one XInput interface per connected controller (claimed in
  491. // order by xi_open). The wake mouse + WebUSB are added around this by usbReenumerate.
  492. USBDevice.addInterface(g_mouse);
  493. for (uint8_t u = 0; u < k; u++)
  494. USBDevice.addInterface(g_xinput[u]);
  495. }
  496. void XboxController::onReport45(int slot, const uint8_t *rep, bool fresh,
  497. uint8_t bodyTlen)
  498. {
  499. (void)fresh;
  500. (void)bodyTlen;
  501. if (slot < 0 || slot >= NSLOT)
  502. return;
  503. // rfXboxGamepad/rfXboxMouse read report 0x45 field offsets. The new-firmware report 0x42 is VERIFIED
  504. // byte-identical over [0..45] (buttons/triggers/sticks/pads/IMU at the same offsets; it only appends 8
  505. // trailing bytes and sets always-on status bits 28/29 that no TB_ mask reads -- see the rf_link decode
  506. // note), so both ids decode here. Anything else must not be decoded as gamepad input.
  507. if (rep[0] != 0x45 && rep[0] != 0x42)
  508. return;
  509. rfXboxGamepad((uint8_t)slot, rep);
  510. // Shared desktop mouse: slot 0's right-pad only. Other slots' right-pad is still part of the
  511. // per-slot XInput (TB_RPADT in the gamepad report is unused today but kept for forward-compat).
  512. if (slot == 0)
  513. rfXboxMouse(rep);
  514. }
  515. // Lost-stop watchdog per slot: Steam/Triton rumble is latched, so force a zero report if the host stops
  516. // refreshing on a particular XInput. The haptics.cpp global watchdog fires too, but it skips while the slot
  517. // is in the post-reconnect block -- this catches a host that kept streaming while we were blocked.
  518. void XboxController::task()
  519. {
  520. for (int s = 0; s < NSLOT; s++) {
  521. if (!g_xiSlot[s].inUse)
  522. continue;
  523. if ((g_xiSlot[s].rumbleLow || g_xiSlot[s].rumbleHigh) &&
  524. millis() - g_xiSlot[s].rumbleMs > RUMBLE_STUCK_MS) {
  525. g_xiSlot[s].rumbleLow = g_xiSlot[s].rumbleHigh = 0;
  526. hapticSteamRumble(0, 0, (uint8_t)s);
  527. }
  528. }
  529. }