mode_xinput.cpp 17 KB

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