SDL_hidapi_steam.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "../../SDL_hints_c.h"
  21. #include "../SDL_sysjoystick.h"
  22. #include "SDL_hidapijoystick_c.h"
  23. #ifdef SDL_JOYSTICK_HIDAPI_STEAM
  24. // Define this if you want to log all packets from the controller
  25. // #define DEBUG_STEAM_PROTOCOL
  26. #define SDL_HINT_JOYSTICK_HIDAPI_STEAM_PAIRING_ENABLED "SDL_JOYSTICK_HIDAPI_STEAM_PAIRING_ENABLED"
  27. #if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)
  28. // This requires prompting for Bluetooth permissions, so make sure the application really wants it
  29. #define SDL_HINT_JOYSTICK_HIDAPI_STEAM_DEFAULT false
  30. #else
  31. #define SDL_HINT_JOYSTICK_HIDAPI_STEAM_DEFAULT SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT)
  32. #endif
  33. #define PAIRING_STATE_DURATION_SECONDS 60
  34. /*****************************************************************************************************/
  35. #include "steam/controller_constants.h"
  36. #include "steam/controller_structs.h"
  37. enum
  38. {
  39. SDL_GAMEPAD_BUTTON_STEAM_RIGHT_PADDLE = 11,
  40. SDL_GAMEPAD_BUTTON_STEAM_LEFT_PADDLE,
  41. SDL_GAMEPAD_NUM_STEAM_BUTTONS,
  42. };
  43. typedef struct SteamControllerStateInternal_t
  44. {
  45. // Controller Type for this Controller State
  46. Uint32 eControllerType;
  47. // If packet num matches that on your prior call, then the controller state hasn't been changed since
  48. // your last call and there is no need to process it
  49. Uint32 unPacketNum;
  50. // bit flags for each of the buttons
  51. Uint64 ulButtons;
  52. // Left pad coordinates
  53. short sLeftPadX;
  54. short sLeftPadY;
  55. // Right pad coordinates
  56. short sRightPadX;
  57. short sRightPadY;
  58. // Center pad coordinates
  59. short sCenterPadX;
  60. short sCenterPadY;
  61. // Left analog stick coordinates
  62. short sLeftStickX;
  63. short sLeftStickY;
  64. // Right analog stick coordinates
  65. short sRightStickX;
  66. short sRightStickY;
  67. unsigned short sTriggerL;
  68. unsigned short sTriggerR;
  69. short sAccelX;
  70. short sAccelY;
  71. short sAccelZ;
  72. short sGyroX;
  73. short sGyroY;
  74. short sGyroZ;
  75. float sGyroQuatW;
  76. float sGyroQuatX;
  77. float sGyroQuatY;
  78. float sGyroQuatZ;
  79. short sGyroSteeringAngle;
  80. unsigned short sBatteryLevel;
  81. // Pressure sensor data.
  82. unsigned short sPressurePadLeft;
  83. unsigned short sPressurePadRight;
  84. unsigned short sPressureBumperLeft;
  85. unsigned short sPressureBumperRight;
  86. // Internal state data
  87. short sPrevLeftPad[2];
  88. short sPrevLeftStick[2];
  89. } SteamControllerStateInternal_t;
  90. // Defines for ulButtons in SteamControllerStateInternal_t
  91. #define STEAM_RIGHT_TRIGGER_MASK 0x00000001
  92. #define STEAM_LEFT_TRIGGER_MASK 0x00000002
  93. #define STEAM_RIGHT_BUMPER_MASK 0x00000004
  94. #define STEAM_LEFT_BUMPER_MASK 0x00000008
  95. #define STEAM_BUTTON_NORTH_MASK 0x00000010 // Y
  96. #define STEAM_BUTTON_EAST_MASK 0x00000020 // B
  97. #define STEAM_BUTTON_WEST_MASK 0x00000040 // X
  98. #define STEAM_BUTTON_SOUTH_MASK 0x00000080 // A
  99. #define STEAM_DPAD_UP_MASK 0x00000100 // DPAD UP
  100. #define STEAM_DPAD_RIGHT_MASK 0x00000200 // DPAD RIGHT
  101. #define STEAM_DPAD_LEFT_MASK 0x00000400 // DPAD LEFT
  102. #define STEAM_DPAD_DOWN_MASK 0x00000800 // DPAD DOWN
  103. #define STEAM_BUTTON_MENU_MASK 0x00001000 // SELECT
  104. #define STEAM_BUTTON_STEAM_MASK 0x00002000 // GUIDE
  105. #define STEAM_BUTTON_ESCAPE_MASK 0x00004000 // START
  106. #define STEAM_BUTTON_BACK_LEFT_MASK 0x00008000
  107. #define STEAM_BUTTON_BACK_RIGHT_MASK 0x00010000
  108. #define STEAM_BUTTON_LEFTPAD_CLICKED_MASK 0x00020000
  109. #define STEAM_BUTTON_RIGHTPAD_CLICKED_MASK 0x00040000
  110. #define STEAM_LEFTPAD_FINGERDOWN_MASK 0x00080000
  111. #define STEAM_RIGHTPAD_FINGERDOWN_MASK 0x00100000
  112. #define STEAM_JOYSTICK_BUTTON_MASK 0x00400000
  113. #define STEAM_LEFTPAD_AND_JOYSTICK_MASK 0x00800000
  114. // Look for report version 0x0001, type WIRELESS (3), length >= 1 byte
  115. #define D0G_IS_VALID_WIRELESS_EVENT(data, len) ((len) >= 5 && (data)[0] == 1 && (data)[1] == 0 && (data)[2] == 3 && (data)[3] >= 1)
  116. #define D0G_GET_WIRELESS_EVENT_TYPE(data) ((data)[4])
  117. #define D0G_WIRELESS_DISCONNECTED 1
  118. #define D0G_WIRELESS_ESTABLISHED 2
  119. #define D0G_WIRELESS_NEWLYPAIRED 3
  120. #define D0G_IS_WIRELESS_DISCONNECT(data, len) (D0G_IS_VALID_WIRELESS_EVENT(data, len) && D0G_GET_WIRELESS_EVENT_TYPE(data) == D0G_WIRELESS_DISCONNECTED)
  121. #define D0G_IS_WIRELESS_CONNECT(data, len) (D0G_IS_VALID_WIRELESS_EVENT(data, len) && D0G_GET_WIRELESS_EVENT_TYPE(data) != D0G_WIRELESS_DISCONNECTED)
  122. #define MAX_REPORT_SEGMENT_PAYLOAD_SIZE 18
  123. /*
  124. * SteamControllerPacketAssembler has to be used when reading output repots from controllers.
  125. */
  126. typedef struct
  127. {
  128. uint8_t uBuffer[MAX_REPORT_SEGMENT_PAYLOAD_SIZE * 8 + 1];
  129. int nExpectedSegmentNumber;
  130. bool bIsBle;
  131. } SteamControllerPacketAssembler;
  132. #undef clamp
  133. #define clamp(val, min, max) (((val) > (max)) ? (max) : (((val) < (min)) ? (min) : (val)))
  134. #undef offsetof
  135. #define offsetof(s, m) (size_t) & (((s *)0)->m)
  136. #ifdef DEBUG_STEAM_CONTROLLER
  137. #define DPRINTF(format, ...) printf(format, ##__VA_ARGS__)
  138. #define HEXDUMP(ptr, len) hexdump(ptr, len)
  139. #else
  140. #define DPRINTF(format, ...)
  141. #define HEXDUMP(ptr, len)
  142. #endif
  143. #define printf SDL_Log
  144. #define MAX_REPORT_SEGMENT_SIZE (MAX_REPORT_SEGMENT_PAYLOAD_SIZE + 2)
  145. #define CALC_REPORT_SEGMENT_NUM(index) ((index / MAX_REPORT_SEGMENT_PAYLOAD_SIZE) & 0x07)
  146. #define REPORT_SEGMENT_DATA_FLAG 0x80
  147. #define REPORT_SEGMENT_LAST_FLAG 0x40
  148. #define BLE_REPORT_NUMBER 0x03
  149. #define STEAMCONTROLLER_TRIGGER_MAX_ANALOG 26000
  150. // Enable mouse mode when using the Steam Controller locally
  151. #undef ENABLE_MOUSE_MODE
  152. // Wireless firmware quirk: the firmware intentionally signals "failure" when performing
  153. // SET_FEATURE / GET_FEATURE when it actually means "pending radio roundtrip". The only
  154. // way to make SET_FEATURE / GET_FEATURE work is to loop several times with a sleep. If
  155. // it takes more than 50ms to get the response for SET_FEATURE / GET_FEATURE, we assume
  156. // that the controller has failed.
  157. #define RADIO_WORKAROUND_SLEEP_ATTEMPTS 50
  158. #define RADIO_WORKAROUND_SLEEP_DURATION_US 500
  159. // This was defined by experimentation. 2000 seemed to work but to give that extra bit of margin, set to 3ms.
  160. #define CONTROLLER_CONFIGURATION_DELAY_US 3000
  161. static uint8_t GetSegmentHeader(int nSegmentNumber, bool bLastPacket)
  162. {
  163. uint8_t header = REPORT_SEGMENT_DATA_FLAG;
  164. header |= nSegmentNumber;
  165. if (bLastPacket) {
  166. header |= REPORT_SEGMENT_LAST_FLAG;
  167. }
  168. return header;
  169. }
  170. static void hexdump(const uint8_t *ptr, int len)
  171. {
  172. HIDAPI_DumpPacket("Data", ptr, len);
  173. }
  174. static void ResetSteamControllerPacketAssembler(SteamControllerPacketAssembler *pAssembler)
  175. {
  176. SDL_memset(pAssembler->uBuffer, 0, sizeof(pAssembler->uBuffer));
  177. pAssembler->nExpectedSegmentNumber = 0;
  178. }
  179. static void InitializeSteamControllerPacketAssembler(SteamControllerPacketAssembler *pAssembler, bool bIsBle)
  180. {
  181. pAssembler->bIsBle = bIsBle;
  182. ResetSteamControllerPacketAssembler(pAssembler);
  183. }
  184. // Returns:
  185. // <0 on error
  186. // 0 on not ready
  187. // Complete packet size on completion
  188. static int WriteSegmentToSteamControllerPacketAssembler(SteamControllerPacketAssembler *pAssembler, const uint8_t *pSegment, int nSegmentLength)
  189. {
  190. if (pAssembler->bIsBle) {
  191. uint8_t uSegmentHeader = pSegment[1];
  192. int nSegmentNumber = uSegmentHeader & 0x07;
  193. HEXDUMP(pSegment, nSegmentLength);
  194. if (pSegment[0] != BLE_REPORT_NUMBER) {
  195. // We may get keyboard/mouse input events until controller stops sending them
  196. return 0;
  197. }
  198. if (nSegmentLength != MAX_REPORT_SEGMENT_SIZE) {
  199. printf("Bad segment size! %d\n", nSegmentLength);
  200. hexdump(pSegment, nSegmentLength);
  201. ResetSteamControllerPacketAssembler(pAssembler);
  202. return -1;
  203. }
  204. DPRINTF("GOT PACKET HEADER = 0x%x\n", uSegmentHeader);
  205. if (!(uSegmentHeader & REPORT_SEGMENT_DATA_FLAG)) {
  206. // We get empty segments, just ignore them
  207. return 0;
  208. }
  209. if (nSegmentNumber != pAssembler->nExpectedSegmentNumber) {
  210. ResetSteamControllerPacketAssembler(pAssembler);
  211. if (nSegmentNumber) {
  212. // This happens occasionally
  213. DPRINTF("Bad segment number, got %d, expected %d\n",
  214. nSegmentNumber, pAssembler->nExpectedSegmentNumber);
  215. return -1;
  216. }
  217. }
  218. SDL_memcpy(pAssembler->uBuffer + nSegmentNumber * MAX_REPORT_SEGMENT_PAYLOAD_SIZE,
  219. pSegment + 2, // ignore header and report number
  220. MAX_REPORT_SEGMENT_PAYLOAD_SIZE);
  221. if (uSegmentHeader & REPORT_SEGMENT_LAST_FLAG) {
  222. pAssembler->nExpectedSegmentNumber = 0;
  223. return (nSegmentNumber + 1) * MAX_REPORT_SEGMENT_PAYLOAD_SIZE;
  224. }
  225. pAssembler->nExpectedSegmentNumber++;
  226. } else {
  227. // Just pass through
  228. SDL_memcpy(pAssembler->uBuffer,
  229. pSegment,
  230. nSegmentLength);
  231. return nSegmentLength;
  232. }
  233. return 0;
  234. }
  235. #define BLE_MAX_READ_RETRIES 8
  236. static int SetFeatureReport(SDL_HIDAPI_Device *dev, const unsigned char uBuffer[65], int nActualDataLen)
  237. {
  238. int nRet = -1;
  239. DPRINTF("SetFeatureReport %p %p %d\n", dev, uBuffer, nActualDataLen);
  240. if (dev->is_bluetooth) {
  241. int nSegmentNumber = 0;
  242. uint8_t uPacketBuffer[MAX_REPORT_SEGMENT_SIZE];
  243. const unsigned char *pBufferPtr = uBuffer + 1;
  244. if (nActualDataLen < 1) {
  245. return -1;
  246. }
  247. // Skip report number in data
  248. nActualDataLen--;
  249. while (nActualDataLen > 0) {
  250. int nBytesInPacket = nActualDataLen > MAX_REPORT_SEGMENT_PAYLOAD_SIZE ? MAX_REPORT_SEGMENT_PAYLOAD_SIZE : nActualDataLen;
  251. nActualDataLen -= nBytesInPacket;
  252. // Construct packet
  253. SDL_memset(uPacketBuffer, 0, sizeof(uPacketBuffer));
  254. uPacketBuffer[0] = BLE_REPORT_NUMBER;
  255. uPacketBuffer[1] = GetSegmentHeader(nSegmentNumber, nActualDataLen == 0);
  256. SDL_memcpy(&uPacketBuffer[2], pBufferPtr, nBytesInPacket);
  257. pBufferPtr += nBytesInPacket;
  258. nSegmentNumber++;
  259. nRet = SDL_hid_send_feature_report(dev->dev, uPacketBuffer, sizeof(uPacketBuffer));
  260. }
  261. } else {
  262. for (int nRetries = 0; nRetries < RADIO_WORKAROUND_SLEEP_ATTEMPTS; nRetries++) {
  263. nRet = SDL_hid_send_feature_report(dev->dev, uBuffer, 65);
  264. if (nRet >= 0) {
  265. break;
  266. }
  267. SDL_DelayNS(RADIO_WORKAROUND_SLEEP_DURATION_US * 1000);
  268. }
  269. }
  270. DPRINTF("SetFeatureReport() ret = %d\n", nRet);
  271. return nRet;
  272. }
  273. static int GetFeatureReport(SDL_HIDAPI_Device *dev, unsigned char uBuffer[65])
  274. {
  275. int nRet = -1;
  276. DPRINTF("GetFeatureReport( %p %p )\n", dev, uBuffer);
  277. if (dev->is_bluetooth) {
  278. int nRetries = 0;
  279. uint8_t uSegmentBuffer[MAX_REPORT_SEGMENT_SIZE + 1];
  280. uint8_t ucBytesToRead = MAX_REPORT_SEGMENT_SIZE;
  281. uint8_t ucDataStartOffset = 0;
  282. SteamControllerPacketAssembler assembler;
  283. InitializeSteamControllerPacketAssembler(&assembler, dev->is_bluetooth);
  284. // On Windows and macOS, BLE devices get 2 copies of the feature report ID, one that is removed by ReadFeatureReport,
  285. // and one that's included in the buffer we receive. We pad the bytes to read and skip over the report ID
  286. // if necessary.
  287. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_MACOS)
  288. ++ucBytesToRead;
  289. ++ucDataStartOffset;
  290. #endif
  291. while (nRetries < BLE_MAX_READ_RETRIES) {
  292. SDL_memset(uSegmentBuffer, 0, sizeof(uSegmentBuffer));
  293. uSegmentBuffer[0] = BLE_REPORT_NUMBER;
  294. nRet = SDL_hid_get_feature_report(dev->dev, uSegmentBuffer, ucBytesToRead);
  295. DPRINTF("GetFeatureReport ble ret=%d\n", nRet);
  296. HEXDUMP(uSegmentBuffer, nRet);
  297. // Zero retry counter if we got data
  298. if (nRet > 2 && (uSegmentBuffer[ucDataStartOffset + 1] & REPORT_SEGMENT_DATA_FLAG)) {
  299. nRetries = 0;
  300. } else {
  301. nRetries++;
  302. }
  303. if (nRet > 0) {
  304. int nPacketLength = WriteSegmentToSteamControllerPacketAssembler(&assembler,
  305. uSegmentBuffer + ucDataStartOffset,
  306. nRet - ucDataStartOffset);
  307. if (nPacketLength > 0 && nPacketLength < 65) {
  308. // Leave space for "report number"
  309. uBuffer[0] = 0;
  310. SDL_memcpy(uBuffer + 1, assembler.uBuffer, nPacketLength);
  311. return nPacketLength;
  312. }
  313. }
  314. }
  315. printf("Could not get a full ble packet after %d retries\n", nRetries);
  316. return -1;
  317. } else {
  318. SDL_memset(uBuffer, 0, 65);
  319. for (int nRetries = 0; nRetries < RADIO_WORKAROUND_SLEEP_ATTEMPTS; nRetries++) {
  320. nRet = SDL_hid_get_feature_report(dev->dev, uBuffer, 65);
  321. if (nRet >= 0) {
  322. break;
  323. }
  324. SDL_DelayNS(RADIO_WORKAROUND_SLEEP_DURATION_US * 1000);
  325. }
  326. DPRINTF("GetFeatureReport USB ret=%d\n", nRet);
  327. HEXDUMP(uBuffer, nRet);
  328. }
  329. return nRet;
  330. }
  331. static int ReadResponse(SDL_HIDAPI_Device *dev, uint8_t uBuffer[65], int nExpectedResponse)
  332. {
  333. for (int nRetries = 0; nRetries < 10; nRetries++) {
  334. int nRet = GetFeatureReport(dev, uBuffer);
  335. DPRINTF("ReadResponse( %p %p 0x%x )\n", dev, uBuffer, nExpectedResponse);
  336. if (nRet < 0) {
  337. continue;
  338. }
  339. DPRINTF("ReadResponse got %d bytes of data: ", nRet);
  340. HEXDUMP(uBuffer, nRet);
  341. if (uBuffer[1] != nExpectedResponse) {
  342. continue;
  343. }
  344. return nRet;
  345. }
  346. return -1;
  347. }
  348. //---------------------------------------------------------------------------
  349. // Reset steam controller (unmap buttons and pads) and re-fetch capability bits
  350. //---------------------------------------------------------------------------
  351. static bool ResetSteamController(SDL_HIDAPI_Device *dev, bool bSuppressErrorSpew, uint32_t *punUpdateRateUS)
  352. {
  353. // Firmware quirk: Set Feature and Get Feature requests always require a 65-byte buffer.
  354. unsigned char buf[65];
  355. unsigned int i;
  356. int res = -1;
  357. int nSettings = 0;
  358. int nAttributesLength;
  359. FeatureReportMsg *msg;
  360. uint32_t unUpdateRateUS = 9000; // Good default rate
  361. DPRINTF("ResetSteamController hid=%p\n", dev);
  362. SDL_zero(buf);
  363. buf[0] = 0;
  364. buf[1] = ID_GET_ATTRIBUTES_VALUES;
  365. res = SetFeatureReport(dev, buf, 2);
  366. if (res < 0) {
  367. if (!bSuppressErrorSpew) {
  368. printf("GET_ATTRIBUTES_VALUES failed for controller %p\n", dev);
  369. }
  370. return false;
  371. }
  372. // Retrieve GET_ATTRIBUTES_VALUES result
  373. // Wireless controller endpoints without a connected controller will return nAttrs == 0
  374. res = ReadResponse(dev, buf, ID_GET_ATTRIBUTES_VALUES);
  375. if (res < 0 || buf[1] != ID_GET_ATTRIBUTES_VALUES) {
  376. HEXDUMP(buf, res);
  377. if (!bSuppressErrorSpew) {
  378. printf("Bad GET_ATTRIBUTES_VALUES response for controller %p\n", dev);
  379. }
  380. return false;
  381. }
  382. nAttributesLength = buf[2];
  383. if (nAttributesLength > res) {
  384. if (!bSuppressErrorSpew) {
  385. printf("Bad GET_ATTRIBUTES_VALUES response for controller %p\n", dev);
  386. }
  387. return false;
  388. }
  389. msg = (FeatureReportMsg *)&buf[1];
  390. for (i = 0; i < (int)msg->header.length / sizeof(ControllerAttribute); ++i) {
  391. uint8_t unAttribute = msg->payload.getAttributes.attributes[i].attributeTag;
  392. uint32_t unValue = msg->payload.getAttributes.attributes[i].attributeValue;
  393. switch (unAttribute) {
  394. case ATTRIB_UNIQUE_ID:
  395. break;
  396. case ATTRIB_PRODUCT_ID:
  397. break;
  398. case ATTRIB_CAPABILITIES:
  399. break;
  400. case ATTRIB_CONNECTION_INTERVAL_IN_US:
  401. unUpdateRateUS = unValue;
  402. break;
  403. default:
  404. break;
  405. }
  406. }
  407. if (punUpdateRateUS) {
  408. *punUpdateRateUS = unUpdateRateUS;
  409. }
  410. // Clear digital button mappings
  411. SDL_zero(buf);
  412. buf[0] = 0;
  413. buf[1] = ID_CLEAR_DIGITAL_MAPPINGS;
  414. res = SetFeatureReport(dev, buf, 2);
  415. if (res < 0) {
  416. if (!bSuppressErrorSpew) {
  417. printf("CLEAR_DIGITAL_MAPPINGS failed for controller %p\n", dev);
  418. }
  419. return false;
  420. }
  421. // Reset the default settings
  422. SDL_zero(buf);
  423. buf[1] = ID_LOAD_DEFAULT_SETTINGS;
  424. buf[2] = 0;
  425. res = SetFeatureReport(dev, buf, 3);
  426. if (res < 0) {
  427. if (!bSuppressErrorSpew) {
  428. printf("LOAD_DEFAULT_SETTINGS failed for controller %p\n", dev);
  429. }
  430. return false;
  431. }
  432. // Apply custom settings - clear trackpad modes (cancel mouse emulation), etc
  433. #define ADD_SETTING(SETTING, VALUE) \
  434. buf[3 + nSettings * 3] = SETTING; \
  435. buf[3 + nSettings * 3 + 1] = ((uint16_t)VALUE) & 0xFF; \
  436. buf[3 + nSettings * 3 + 2] = ((uint16_t)VALUE) >> 8; \
  437. ++nSettings;
  438. SDL_zero(buf);
  439. buf[1] = ID_SET_SETTINGS_VALUES;
  440. ADD_SETTING(SETTING_WIRELESS_PACKET_VERSION, 2);
  441. ADD_SETTING(SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE);
  442. #ifdef ENABLE_MOUSE_MODE
  443. ADD_SETTING(SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_ABSOLUTE_MOUSE);
  444. ADD_SETTING(SETTING_SMOOTH_ABSOLUTE_MOUSE, 1);
  445. ADD_SETTING(SETTING_MOMENTUM_MAXIMUM_VELOCITY, 20000); // [0-20000] default 8000
  446. ADD_SETTING(SETTING_MOMENTUM_DECAY_AMOUNT, 50); // [0-50] default 5
  447. #else
  448. ADD_SETTING(SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE);
  449. ADD_SETTING(SETTING_SMOOTH_ABSOLUTE_MOUSE, 0);
  450. #endif
  451. buf[2] = (unsigned char)(nSettings * 3);
  452. res = SetFeatureReport(dev, buf, 3 + nSettings * 3);
  453. if (res < 0) {
  454. if (!bSuppressErrorSpew) {
  455. printf("SET_SETTINGS failed for controller %p\n", dev);
  456. }
  457. return false;
  458. }
  459. #ifdef ENABLE_MOUSE_MODE
  460. // Wait for ID_CLEAR_DIGITAL_MAPPINGS to be processed on the controller
  461. bool bMappingsCleared = false;
  462. int iRetry;
  463. for (iRetry = 0; iRetry < 2; ++iRetry) {
  464. SDL_zero(buf);
  465. buf[1] = ID_GET_DIGITAL_MAPPINGS;
  466. buf[2] = 1; // one byte - requesting from index 0
  467. buf[3] = 0;
  468. res = SetFeatureReport(dev, buf, 4);
  469. if (res < 0) {
  470. printf("GET_DIGITAL_MAPPINGS failed for controller %p\n", dev);
  471. return false;
  472. }
  473. res = ReadResponse(dev, buf, ID_GET_DIGITAL_MAPPINGS);
  474. if (res < 0 || buf[1] != ID_GET_DIGITAL_MAPPINGS) {
  475. printf("Bad GET_DIGITAL_MAPPINGS response for controller %p\n", dev);
  476. return false;
  477. }
  478. // If the length of the digital mappings result is not 1 (index byte, no mappings) then clearing hasn't executed
  479. if (buf[2] == 1 && buf[3] == 0xFF) {
  480. bMappingsCleared = true;
  481. break;
  482. }
  483. usleep(CONTROLLER_CONFIGURATION_DELAY_US);
  484. }
  485. if (!bMappingsCleared && !bSuppressErrorSpew) {
  486. printf("Warning: CLEAR_DIGITAL_MAPPINGS never completed for controller %p\n", dev);
  487. }
  488. // Set our new mappings
  489. SDL_zero(buf);
  490. buf[1] = ID_SET_DIGITAL_MAPPINGS;
  491. buf[2] = 6; // 2 settings x 3 bytes
  492. buf[3] = IO_DIGITAL_BUTTON_RIGHT_TRIGGER;
  493. buf[4] = DEVICE_MOUSE;
  494. buf[5] = MOUSE_BTN_LEFT;
  495. buf[6] = IO_DIGITAL_BUTTON_LEFT_TRIGGER;
  496. buf[7] = DEVICE_MOUSE;
  497. buf[8] = MOUSE_BTN_RIGHT;
  498. res = SetFeatureReport(dev, buf, 9);
  499. if (res < 0) {
  500. if (!bSuppressErrorSpew) {
  501. printf("SET_DIGITAL_MAPPINGS failed for controller %p\n", dev);
  502. }
  503. return false;
  504. }
  505. #endif // ENABLE_MOUSE_MODE
  506. return true;
  507. }
  508. //---------------------------------------------------------------------------
  509. // Read from a Steam Controller
  510. //---------------------------------------------------------------------------
  511. static int ReadSteamController(SDL_hid_device *dev, uint8_t *pData, int nDataSize)
  512. {
  513. SDL_memset(pData, 0, nDataSize);
  514. pData[0] = BLE_REPORT_NUMBER; // hid_read will also overwrite this with the same value, 0x03
  515. return SDL_hid_read(dev, pData, nDataSize);
  516. }
  517. //---------------------------------------------------------------------------
  518. // Set Steam Controller pairing state
  519. //---------------------------------------------------------------------------
  520. static void SetPairingState(SDL_HIDAPI_Device *dev, bool bEnablePairing)
  521. {
  522. unsigned char buf[65];
  523. SDL_zero(buf);
  524. buf[1] = ID_ENABLE_PAIRING;
  525. buf[2] = 2; // 2 payload bytes: bool + timeout
  526. buf[3] = bEnablePairing ? 1 : 0;
  527. buf[4] = bEnablePairing ? PAIRING_STATE_DURATION_SECONDS : 0;
  528. SetFeatureReport(dev, buf, 5);
  529. }
  530. //---------------------------------------------------------------------------
  531. // Commit Steam Controller pairing
  532. //---------------------------------------------------------------------------
  533. static void CommitPairing(SDL_HIDAPI_Device *dev)
  534. {
  535. unsigned char buf[65];
  536. SDL_zero(buf);
  537. buf[1] = ID_DONGLE_COMMIT_DEVICE;
  538. SetFeatureReport(dev, buf, 2);
  539. }
  540. //---------------------------------------------------------------------------
  541. // Close a Steam Controller
  542. //---------------------------------------------------------------------------
  543. static void CloseSteamController(SDL_HIDAPI_Device *dev)
  544. {
  545. // Switch the Steam Controller back to lizard mode so it works with the OS
  546. unsigned char buf[65];
  547. int nSettings = 0;
  548. // Reset digital button mappings
  549. SDL_zero(buf);
  550. buf[1] = ID_SET_DEFAULT_DIGITAL_MAPPINGS;
  551. SetFeatureReport(dev, buf, 2);
  552. // Reset the default settings
  553. SDL_zero(buf);
  554. buf[1] = ID_LOAD_DEFAULT_SETTINGS;
  555. buf[2] = 0;
  556. SetFeatureReport(dev, buf, 3);
  557. // Reset mouse mode for lizard mode
  558. SDL_zero(buf);
  559. buf[1] = ID_SET_SETTINGS_VALUES;
  560. ADD_SETTING(SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_ABSOLUTE_MOUSE);
  561. buf[2] = (unsigned char)(nSettings * 3);
  562. SetFeatureReport(dev, buf, 3 + nSettings * 3);
  563. }
  564. //---------------------------------------------------------------------------
  565. // Scale and clamp values to a range
  566. //---------------------------------------------------------------------------
  567. static float RemapValClamped(float val, float A, float B, float C, float D)
  568. {
  569. if (A == B) {
  570. return (val - B) >= 0.0f ? D : C;
  571. } else {
  572. float cVal = (val - A) / (B - A);
  573. cVal = clamp(cVal, 0.0f, 1.0f);
  574. return C + (D - C) * cVal;
  575. }
  576. }
  577. //---------------------------------------------------------------------------
  578. // Rotate the pad coordinates
  579. //---------------------------------------------------------------------------
  580. static void RotatePad(int *pX, int *pY, float flAngleInRad)
  581. {
  582. int origX = *pX, origY = *pY;
  583. *pX = (int)(SDL_cosf(flAngleInRad) * origX - SDL_sinf(flAngleInRad) * origY);
  584. *pY = (int)(SDL_sinf(flAngleInRad) * origX + SDL_cosf(flAngleInRad) * origY);
  585. }
  586. //---------------------------------------------------------------------------
  587. // Format the first part of the state packet
  588. //---------------------------------------------------------------------------
  589. static void FormatStatePacketUntilGyro(SteamControllerStateInternal_t *pState, ValveControllerStatePacket_t *pStatePacket)
  590. {
  591. int nLeftPadX;
  592. int nLeftPadY;
  593. int nRightPadX;
  594. int nRightPadY;
  595. int nPadOffset;
  596. // 15 degrees in rad
  597. const float flRotationAngle = 0.261799f;
  598. SDL_memset(pState, 0, offsetof(SteamControllerStateInternal_t, sBatteryLevel));
  599. // pState->eControllerType = m_eControllerType;
  600. pState->eControllerType = 2; // k_eControllerType_SteamController;
  601. pState->unPacketNum = pStatePacket->unPacketNum;
  602. // We have a chunk of trigger data in the packet format here, so zero it out afterwards
  603. SDL_memcpy(&pState->ulButtons, &pStatePacket->ButtonTriggerData.ulButtons, 8);
  604. pState->ulButtons &= ~0xFFFF000000LL;
  605. // The firmware uses this bit to tell us what kind of data is packed into the left two axes
  606. if (pStatePacket->ButtonTriggerData.ulButtons & STEAM_LEFTPAD_FINGERDOWN_MASK) {
  607. // Finger-down bit not set; "left pad" is actually trackpad
  608. pState->sLeftPadX = pState->sPrevLeftPad[0] = pStatePacket->sLeftPadX;
  609. pState->sLeftPadY = pState->sPrevLeftPad[1] = pStatePacket->sLeftPadY;
  610. if (pStatePacket->ButtonTriggerData.ulButtons & STEAM_LEFTPAD_AND_JOYSTICK_MASK) {
  611. // The controller is interleaving both stick and pad data, both are active
  612. pState->sLeftStickX = pState->sPrevLeftStick[0];
  613. pState->sLeftStickY = pState->sPrevLeftStick[1];
  614. } else {
  615. // The stick is not active
  616. pState->sPrevLeftStick[0] = 0;
  617. pState->sPrevLeftStick[1] = 0;
  618. }
  619. } else {
  620. // Finger-down bit not set; "left pad" is actually joystick
  621. // XXX there's a firmware bug where sometimes padX is 0 and padY is a large number (actually the battery voltage)
  622. // If that happens skip this packet and report last frames stick
  623. /*
  624. if ( m_eControllerType == k_eControllerType_SteamControllerV2 && pStatePacket->sLeftPadY > 900 ) {
  625. pState->sLeftStickX = pState->sPrevLeftStick[0];
  626. pState->sLeftStickY = pState->sPrevLeftStick[1];
  627. } else
  628. */
  629. {
  630. pState->sPrevLeftStick[0] = pState->sLeftStickX = pStatePacket->sLeftPadX;
  631. pState->sPrevLeftStick[1] = pState->sLeftStickY = pStatePacket->sLeftPadY;
  632. }
  633. /*
  634. if (m_eControllerType == k_eControllerType_SteamControllerV2) {
  635. UpdateV2JoystickCap(&state);
  636. }
  637. */
  638. if (pStatePacket->ButtonTriggerData.ulButtons & STEAM_LEFTPAD_AND_JOYSTICK_MASK) {
  639. // The controller is interleaving both stick and pad data, both are active
  640. pState->sLeftPadX = pState->sPrevLeftPad[0];
  641. pState->sLeftPadY = pState->sPrevLeftPad[1];
  642. } else {
  643. // The trackpad is not active
  644. pState->sPrevLeftPad[0] = 0;
  645. pState->sPrevLeftPad[1] = 0;
  646. // Old controllers send trackpad click for joystick button when trackpad is not active
  647. if (pState->ulButtons & STEAM_BUTTON_LEFTPAD_CLICKED_MASK) {
  648. pState->ulButtons &= ~STEAM_BUTTON_LEFTPAD_CLICKED_MASK;
  649. pState->ulButtons |= STEAM_JOYSTICK_BUTTON_MASK;
  650. }
  651. }
  652. }
  653. // Fingerdown bit indicates if the packed left axis data was joystick or pad,
  654. // but if we are interleaving both, the left finger is definitely on the pad.
  655. if (pStatePacket->ButtonTriggerData.ulButtons & STEAM_LEFTPAD_AND_JOYSTICK_MASK) {
  656. pState->ulButtons |= STEAM_LEFTPAD_FINGERDOWN_MASK;
  657. }
  658. pState->sRightPadX = pStatePacket->sRightPadX;
  659. pState->sRightPadY = pStatePacket->sRightPadY;
  660. nLeftPadX = pState->sLeftPadX;
  661. nLeftPadY = pState->sLeftPadY;
  662. nRightPadX = pState->sRightPadX;
  663. nRightPadY = pState->sRightPadY;
  664. RotatePad(&nLeftPadX, &nLeftPadY, -flRotationAngle);
  665. RotatePad(&nRightPadX, &nRightPadY, flRotationAngle);
  666. if (pState->ulButtons & STEAM_LEFTPAD_FINGERDOWN_MASK) {
  667. nPadOffset = 1000;
  668. } else {
  669. nPadOffset = 0;
  670. }
  671. pState->sLeftPadX = (short)clamp(nLeftPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  672. pState->sLeftPadY = (short)clamp(nLeftPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  673. nPadOffset = 0;
  674. if (pState->ulButtons & STEAM_RIGHTPAD_FINGERDOWN_MASK) {
  675. nPadOffset = 1000;
  676. } else {
  677. nPadOffset = 0;
  678. }
  679. pState->sRightPadX = (short)clamp(nRightPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  680. pState->sRightPadY = (short)clamp(nRightPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  681. pState->sTriggerL = (unsigned short)RemapValClamped((float)((pStatePacket->ButtonTriggerData.Triggers.nLeft << 7) | pStatePacket->ButtonTriggerData.Triggers.nLeft), 0, STEAMCONTROLLER_TRIGGER_MAX_ANALOG, 0, SDL_MAX_SINT16);
  682. pState->sTriggerR = (unsigned short)RemapValClamped((float)((pStatePacket->ButtonTriggerData.Triggers.nRight << 7) | pStatePacket->ButtonTriggerData.Triggers.nRight), 0, STEAMCONTROLLER_TRIGGER_MAX_ANALOG, 0, SDL_MAX_SINT16);
  683. }
  684. //---------------------------------------------------------------------------
  685. // Update Steam Controller state from a BLE data packet, returns true if it parsed data
  686. //---------------------------------------------------------------------------
  687. static bool UpdateBLESteamControllerState(const uint8_t *pData, int nDataSize, SteamControllerStateInternal_t *pState)
  688. {
  689. int nLeftPadX;
  690. int nLeftPadY;
  691. int nRightPadX;
  692. int nRightPadY;
  693. int nPadOffset;
  694. uint32_t ucOptionDataMask;
  695. // 15 degrees in rad
  696. const float flRotationAngle = 0.261799f;
  697. pState->unPacketNum++;
  698. ucOptionDataMask = (*pData++ & 0xF0);
  699. ucOptionDataMask |= (uint32_t)(*pData++) << 8;
  700. if (ucOptionDataMask & k_EBLEButtonChunk1) {
  701. SDL_memcpy(&pState->ulButtons, pData, 3);
  702. pData += 3;
  703. }
  704. if (ucOptionDataMask & k_EBLEButtonChunk2) {
  705. // The middle 2 bytes of the button bits over the wire are triggers when over the wire and non-SC buttons in the internal controller state packet
  706. pState->sTriggerL = (unsigned short)RemapValClamped((float)((pData[0] << 7) | pData[0]), 0, STEAMCONTROLLER_TRIGGER_MAX_ANALOG, 0, SDL_MAX_SINT16);
  707. pState->sTriggerR = (unsigned short)RemapValClamped((float)((pData[1] << 7) | pData[1]), 0, STEAMCONTROLLER_TRIGGER_MAX_ANALOG, 0, SDL_MAX_SINT16);
  708. pData += 2;
  709. }
  710. if (ucOptionDataMask & k_EBLEButtonChunk3) {
  711. uint8_t *pButtonByte = (uint8_t *)&pState->ulButtons;
  712. pButtonByte[5] = *pData++;
  713. pButtonByte[6] = *pData++;
  714. pButtonByte[7] = *pData++;
  715. }
  716. if (ucOptionDataMask & k_EBLELeftJoystickChunk) {
  717. // This doesn't handle any of the special headcrab stuff for raw joystick which is OK for now since that FW doesn't support
  718. // this protocol yet either
  719. int nLength = sizeof(pState->sLeftStickX) + sizeof(pState->sLeftStickY);
  720. SDL_memcpy(&pState->sLeftStickX, pData, nLength);
  721. pData += nLength;
  722. }
  723. if (ucOptionDataMask & k_EBLELeftTrackpadChunk) {
  724. int nLength = sizeof(pState->sLeftPadX) + sizeof(pState->sLeftPadY);
  725. SDL_memcpy(&pState->sLeftPadX, pData, nLength);
  726. if (pState->ulButtons & STEAM_LEFTPAD_FINGERDOWN_MASK) {
  727. nPadOffset = 1000;
  728. } else {
  729. nPadOffset = 0;
  730. }
  731. nLeftPadX = pState->sLeftPadX;
  732. nLeftPadY = pState->sLeftPadY;
  733. RotatePad(&nLeftPadX, &nLeftPadY, -flRotationAngle);
  734. pState->sLeftPadX = (short)clamp(nLeftPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  735. pState->sLeftPadY = (short)clamp(nLeftPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  736. pData += nLength;
  737. }
  738. if (ucOptionDataMask & k_EBLERightTrackpadChunk) {
  739. int nLength = sizeof(pState->sRightPadX) + sizeof(pState->sRightPadY);
  740. SDL_memcpy(&pState->sRightPadX, pData, nLength);
  741. if (pState->ulButtons & STEAM_RIGHTPAD_FINGERDOWN_MASK) {
  742. nPadOffset = 1000;
  743. } else {
  744. nPadOffset = 0;
  745. }
  746. nRightPadX = pState->sRightPadX;
  747. nRightPadY = pState->sRightPadY;
  748. RotatePad(&nRightPadX, &nRightPadY, flRotationAngle);
  749. pState->sRightPadX = (short)clamp(nRightPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  750. pState->sRightPadY = (short)clamp(nRightPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
  751. pData += nLength;
  752. }
  753. if (ucOptionDataMask & k_EBLEIMUAccelChunk) {
  754. int nLength = sizeof(pState->sAccelX) + sizeof(pState->sAccelY) + sizeof(pState->sAccelZ);
  755. SDL_memcpy(&pState->sAccelX, pData, nLength);
  756. pData += nLength;
  757. }
  758. if (ucOptionDataMask & k_EBLEIMUGyroChunk) {
  759. int nLength = sizeof(pState->sAccelX) + sizeof(pState->sAccelY) + sizeof(pState->sAccelZ);
  760. SDL_memcpy(&pState->sGyroX, pData, nLength);
  761. pData += nLength;
  762. }
  763. if (ucOptionDataMask & k_EBLEIMUQuatChunk) {
  764. int nLength = sizeof(pState->sGyroQuatW) + sizeof(pState->sGyroQuatX) + sizeof(pState->sGyroQuatY) + sizeof(pState->sGyroQuatZ);
  765. SDL_memcpy(&pState->sGyroQuatW, pData, nLength);
  766. pData += nLength;
  767. }
  768. return true;
  769. }
  770. //---------------------------------------------------------------------------
  771. // Update Steam Controller state from a data packet, returns true if it parsed data
  772. //---------------------------------------------------------------------------
  773. static bool UpdateSteamControllerState(const uint8_t *pData, int nDataSize, SteamControllerStateInternal_t *pState)
  774. {
  775. ValveInReport_t *pInReport = (ValveInReport_t *)pData;
  776. if (pInReport->header.unReportVersion != k_ValveInReportMsgVersion) {
  777. if ((pData[0] & 0x0F) == k_EBLEReportState) {
  778. return UpdateBLESteamControllerState(pData, nDataSize, pState);
  779. }
  780. return false;
  781. }
  782. if ((pInReport->header.ucType != ID_CONTROLLER_STATE) &&
  783. (pInReport->header.ucType != ID_CONTROLLER_BLE_STATE)) {
  784. return false;
  785. }
  786. if (pInReport->header.ucType == ID_CONTROLLER_STATE) {
  787. ValveControllerStatePacket_t *pStatePacket = &pInReport->payload.controllerState;
  788. // No new data to process; indicate that we received a state packet, but otherwise do nothing.
  789. if (pState->unPacketNum == pStatePacket->unPacketNum) {
  790. return true;
  791. }
  792. FormatStatePacketUntilGyro(pState, pStatePacket);
  793. pState->sAccelX = pStatePacket->sAccelX;
  794. pState->sAccelY = pStatePacket->sAccelY;
  795. pState->sAccelZ = pStatePacket->sAccelZ;
  796. pState->sGyroQuatW = pStatePacket->sGyroQuatW;
  797. pState->sGyroQuatX = pStatePacket->sGyroQuatX;
  798. pState->sGyroQuatY = pStatePacket->sGyroQuatY;
  799. pState->sGyroQuatZ = pStatePacket->sGyroQuatZ;
  800. pState->sGyroX = pStatePacket->sGyroX;
  801. pState->sGyroY = pStatePacket->sGyroY;
  802. pState->sGyroZ = pStatePacket->sGyroZ;
  803. } else if (pInReport->header.ucType == ID_CONTROLLER_BLE_STATE) {
  804. ValveControllerBLEStatePacket_t *pBLEStatePacket = &pInReport->payload.controllerBLEState;
  805. ValveControllerStatePacket_t *pStatePacket = &pInReport->payload.controllerState;
  806. // No new data to process; indicate that we received a state packet, but otherwise do nothing.
  807. if (pState->unPacketNum == pStatePacket->unPacketNum) {
  808. return true;
  809. }
  810. FormatStatePacketUntilGyro(pState, pStatePacket);
  811. switch (pBLEStatePacket->ucGyroDataType) {
  812. case 1:
  813. pState->sGyroQuatW = ((float)pBLEStatePacket->sGyro[0]);
  814. pState->sGyroQuatX = ((float)pBLEStatePacket->sGyro[1]);
  815. pState->sGyroQuatY = ((float)pBLEStatePacket->sGyro[2]);
  816. pState->sGyroQuatZ = ((float)pBLEStatePacket->sGyro[3]);
  817. break;
  818. case 2:
  819. pState->sAccelX = pBLEStatePacket->sGyro[0];
  820. pState->sAccelY = pBLEStatePacket->sGyro[1];
  821. pState->sAccelZ = pBLEStatePacket->sGyro[2];
  822. break;
  823. case 3:
  824. pState->sGyroX = pBLEStatePacket->sGyro[0];
  825. pState->sGyroY = pBLEStatePacket->sGyro[1];
  826. pState->sGyroZ = pBLEStatePacket->sGyro[2];
  827. break;
  828. default:
  829. break;
  830. }
  831. }
  832. return true;
  833. }
  834. /*****************************************************************************************************/
  835. typedef struct
  836. {
  837. SDL_HIDAPI_Device *device;
  838. bool connected;
  839. bool report_sensors;
  840. uint32_t update_rate_in_us;
  841. Uint64 sensor_timestamp;
  842. Uint64 pairing_time;
  843. SteamControllerPacketAssembler m_assembler;
  844. SteamControllerStateInternal_t m_state;
  845. SteamControllerStateInternal_t m_last_state;
  846. } SDL_DriverSteam_Context;
  847. static bool IsDongle(Uint16 product_id)
  848. {
  849. return (product_id == USB_PRODUCT_VALVE_STEAM_CONTROLLER_DONGLE);
  850. }
  851. static void HIDAPI_DriverSteam_RegisterHints(SDL_HintCallback callback, void *userdata)
  852. {
  853. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STEAM, callback, userdata);
  854. }
  855. static void HIDAPI_DriverSteam_UnregisterHints(SDL_HintCallback callback, void *userdata)
  856. {
  857. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STEAM, callback, userdata);
  858. }
  859. static bool HIDAPI_DriverSteam_IsEnabled(void)
  860. {
  861. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_STEAM, SDL_HINT_JOYSTICK_HIDAPI_STEAM_DEFAULT);
  862. }
  863. static bool HIDAPI_DriverSteam_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  864. {
  865. if (!SDL_IsJoystickSteamController(vendor_id, product_id)) {
  866. return false;
  867. }
  868. if (!device) {
  869. // Might be supported by this driver, enumerate and find out
  870. return true;
  871. }
  872. if (device->is_bluetooth) {
  873. return true;
  874. }
  875. if (IsDongle(product_id)) {
  876. if (interface_number >= 1 && interface_number <= 4) {
  877. // This is one of the wireless controller interfaces
  878. return true;
  879. }
  880. } else {
  881. if (interface_number == 2) {
  882. // This is the controller interface (not mouse or keyboard)
  883. return true;
  884. }
  885. }
  886. return false;
  887. }
  888. static void HIDAPI_DriverSteam_SetPairingState(SDL_DriverSteam_Context *ctx, bool enabled)
  889. {
  890. // Only have one dongle in pairing mode at a time
  891. static SDL_DriverSteam_Context *s_PairingContext = NULL;
  892. if (enabled && s_PairingContext != NULL) {
  893. return;
  894. }
  895. if (!enabled && s_PairingContext != ctx) {
  896. return;
  897. }
  898. if (ctx->connected) {
  899. return;
  900. }
  901. SetPairingState(ctx->device, enabled);
  902. if (enabled) {
  903. ctx->pairing_time = SDL_GetTicks();
  904. s_PairingContext = ctx;
  905. } else {
  906. ctx->pairing_time = 0;
  907. s_PairingContext = NULL;
  908. }
  909. }
  910. static void HIDAPI_DriverSteam_RenewPairingState(SDL_DriverSteam_Context *ctx)
  911. {
  912. Uint64 now = SDL_GetTicks();
  913. if (now >= ctx->pairing_time + PAIRING_STATE_DURATION_SECONDS * 1000) {
  914. SetPairingState(ctx->device, true);
  915. ctx->pairing_time = now;
  916. }
  917. }
  918. static void HIDAPI_DriverSteam_CommitPairing(SDL_DriverSteam_Context *ctx)
  919. {
  920. CommitPairing(ctx->device);
  921. }
  922. static void SDLCALL SDL_PairingEnabledHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  923. {
  924. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)userdata;
  925. bool enabled = SDL_GetStringBoolean(hint, false);
  926. HIDAPI_DriverSteam_SetPairingState(ctx, enabled);
  927. }
  928. static bool HIDAPI_DriverSteam_InitDevice(SDL_HIDAPI_Device *device)
  929. {
  930. SDL_DriverSteam_Context *ctx;
  931. ctx = (SDL_DriverSteam_Context *)SDL_calloc(1, sizeof(*ctx));
  932. if (!ctx) {
  933. return false;
  934. }
  935. ctx->device = device;
  936. device->context = ctx;
  937. #ifdef SDL_PLATFORM_WIN32
  938. if (device->serial) {
  939. // We get a garbage serial number on Windows
  940. SDL_free(device->serial);
  941. device->serial = NULL;
  942. }
  943. #endif // SDL_PLATFORM_WIN32
  944. HIDAPI_SetDeviceName(device, "Steam Controller");
  945. // If this is a wireless dongle, request a wireless state update
  946. if (IsDongle(device->product_id)) {
  947. unsigned char buf[65];
  948. int res;
  949. SDL_zero(buf);
  950. buf[0] = 0;
  951. buf[1] = ID_DONGLE_GET_WIRELESS_STATE;
  952. res = SetFeatureReport(device, buf, 2);
  953. if (res < 0) {
  954. return SDL_SetError("Failed to send ID_DONGLE_GET_WIRELESS_STATE request");
  955. }
  956. for (int attempt = 0; attempt < 5; ++attempt) {
  957. uint8_t data[128];
  958. res = ReadSteamController(device->dev, data, sizeof(data));
  959. if (res == 0) {
  960. SDL_Delay(1);
  961. continue;
  962. }
  963. if (res < 0) {
  964. break;
  965. }
  966. #ifdef DEBUG_STEAM_PROTOCOL
  967. HIDAPI_DumpPacket("Initial dongle packet: size = %d", data, res);
  968. #endif
  969. if (D0G_IS_WIRELESS_CONNECT(data, res)) {
  970. ctx->connected = true;
  971. break;
  972. } else if (D0G_IS_WIRELESS_DISCONNECT(data, res)) {
  973. ctx->connected = false;
  974. break;
  975. }
  976. }
  977. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STEAM_PAIRING_ENABLED,
  978. SDL_PairingEnabledHintChanged, ctx);
  979. } else {
  980. // Wired and BLE controllers are always connected if HIDAPI can see them
  981. ctx->connected = true;
  982. }
  983. if (ctx->connected) {
  984. return HIDAPI_JoystickConnected(device, NULL);
  985. } else {
  986. // We will enumerate any attached controllers in UpdateDevice()
  987. return true;
  988. }
  989. }
  990. static int HIDAPI_DriverSteam_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  991. {
  992. return -1;
  993. }
  994. static void HIDAPI_DriverSteam_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  995. {
  996. }
  997. static bool SetHomeLED(SDL_HIDAPI_Device *device, Uint8 value)
  998. {
  999. unsigned char buf[65];
  1000. int nSettings = 0;
  1001. SDL_zero(buf);
  1002. buf[1] = ID_SET_SETTINGS_VALUES;
  1003. ADD_SETTING(SETTING_LED_USER_BRIGHTNESS, value);
  1004. buf[2] = (unsigned char)(nSettings * 3);
  1005. if (SetFeatureReport(device, buf, 3 + nSettings * 3) < 0) {
  1006. return SDL_SetError("Couldn't write feature report");
  1007. }
  1008. return true;
  1009. }
  1010. static void SDLCALL SDL_HomeLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  1011. {
  1012. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)userdata;
  1013. if (hint && *hint) {
  1014. int value;
  1015. if (SDL_strchr(hint, '.') != NULL) {
  1016. value = (int)(100.0f * SDL_atof(hint));
  1017. if (value > 255) {
  1018. value = 255;
  1019. }
  1020. } else if (SDL_GetStringBoolean(hint, true)) {
  1021. value = 100;
  1022. } else {
  1023. value = 0;
  1024. }
  1025. SetHomeLED(ctx->device, (Uint8)value);
  1026. }
  1027. }
  1028. static bool HIDAPI_DriverSteam_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1029. {
  1030. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
  1031. float update_rate_in_hz = 0.0f;
  1032. SDL_AssertJoysticksLocked();
  1033. ctx->report_sensors = false;
  1034. SDL_zero(ctx->m_assembler);
  1035. SDL_zero(ctx->m_state);
  1036. SDL_zero(ctx->m_last_state);
  1037. if (!ResetSteamController(device, false, &ctx->update_rate_in_us)) {
  1038. SDL_SetError("Couldn't reset controller");
  1039. return false;
  1040. }
  1041. if (ctx->update_rate_in_us > 0) {
  1042. update_rate_in_hz = 1000000.0f / ctx->update_rate_in_us;
  1043. }
  1044. InitializeSteamControllerPacketAssembler(&ctx->m_assembler, device->is_bluetooth);
  1045. // Initialize the joystick capabilities
  1046. joystick->nbuttons = SDL_GAMEPAD_NUM_STEAM_BUTTONS;
  1047. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  1048. joystick->nhats = 1;
  1049. if (IsDongle(device->product_id)) {
  1050. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
  1051. }
  1052. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, update_rate_in_hz);
  1053. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, update_rate_in_hz);
  1054. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STEAM_HOME_LED,
  1055. SDL_HomeLEDHintChanged, ctx);
  1056. return true;
  1057. }
  1058. static bool HIDAPI_DriverSteam_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  1059. {
  1060. // You should use the full Steam Input API for rumble support
  1061. return SDL_Unsupported();
  1062. }
  1063. static bool HIDAPI_DriverSteam_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  1064. {
  1065. return SDL_Unsupported();
  1066. }
  1067. static Uint32 HIDAPI_DriverSteam_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1068. {
  1069. // You should use the full Steam Input API for extended capabilities
  1070. return 0;
  1071. }
  1072. static bool HIDAPI_DriverSteam_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1073. {
  1074. // You should use the full Steam Input API for LED support
  1075. return SDL_Unsupported();
  1076. }
  1077. static bool HIDAPI_DriverSteam_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  1078. {
  1079. if (size == 65) {
  1080. if (SetFeatureReport(device, data, size) < 0) {
  1081. return SDL_SetError("Couldn't write feature report");
  1082. }
  1083. return true;
  1084. }
  1085. return SDL_Unsupported();
  1086. }
  1087. static bool HIDAPI_DriverSteam_SetSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  1088. {
  1089. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
  1090. unsigned char buf[65];
  1091. int nSettings = 0;
  1092. SDL_zero(buf);
  1093. buf[1] = ID_SET_SETTINGS_VALUES;
  1094. if (enabled) {
  1095. ADD_SETTING(SETTING_IMU_MODE, SETTING_GYRO_MODE_SEND_RAW_ACCEL | SETTING_GYRO_MODE_SEND_RAW_GYRO);
  1096. } else {
  1097. ADD_SETTING(SETTING_IMU_MODE, SETTING_GYRO_MODE_OFF);
  1098. }
  1099. buf[2] = (unsigned char)(nSettings * 3);
  1100. if (SetFeatureReport(device, buf, 3 + nSettings * 3) < 0) {
  1101. return SDL_SetError("Couldn't write feature report");
  1102. }
  1103. ctx->report_sensors = enabled;
  1104. return true;
  1105. }
  1106. static bool ControllerConnected(SDL_HIDAPI_Device *device, SDL_Joystick **joystick)
  1107. {
  1108. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
  1109. if (!HIDAPI_JoystickConnected(device, NULL)) {
  1110. return false;
  1111. }
  1112. // We'll automatically accept this controller if we're in pairing mode
  1113. HIDAPI_DriverSteam_CommitPairing(ctx);
  1114. *joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  1115. ctx->connected = true;
  1116. return true;
  1117. }
  1118. static void ControllerDisconnected(SDL_HIDAPI_Device *device, SDL_Joystick **joystick)
  1119. {
  1120. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
  1121. if (device->joysticks) {
  1122. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  1123. }
  1124. ctx->connected = false;
  1125. *joystick = NULL;
  1126. }
  1127. static bool HIDAPI_DriverSteam_UpdateDevice(SDL_HIDAPI_Device *device)
  1128. {
  1129. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
  1130. SDL_Joystick *joystick = NULL;
  1131. if (device->num_joysticks > 0) {
  1132. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  1133. }
  1134. if (ctx->pairing_time) {
  1135. HIDAPI_DriverSteam_RenewPairingState(ctx);
  1136. }
  1137. for (;;) {
  1138. uint8_t data[128];
  1139. int r, nPacketLength;
  1140. const Uint8 *pPacket;
  1141. r = ReadSteamController(device->dev, data, sizeof(data));
  1142. if (r == 0) {
  1143. break;
  1144. }
  1145. if (r < 0) {
  1146. // Failed to read from controller
  1147. ControllerDisconnected(device, &joystick);
  1148. return false;
  1149. }
  1150. #ifdef DEBUG_STEAM_PROTOCOL
  1151. HIDAPI_DumpPacket("Steam Controller packet: size = %d", data, r);
  1152. #endif
  1153. nPacketLength = WriteSegmentToSteamControllerPacketAssembler(&ctx->m_assembler, data, r);
  1154. pPacket = ctx->m_assembler.uBuffer;
  1155. if (nPacketLength > 0 && UpdateSteamControllerState(pPacket, nPacketLength, &ctx->m_state)) {
  1156. Uint64 timestamp = SDL_GetTicksNS();
  1157. if (!ctx->connected) {
  1158. // Maybe we missed a wireless status packet?
  1159. ControllerConnected(device, &joystick);
  1160. }
  1161. if (!joystick) {
  1162. continue;
  1163. }
  1164. if (ctx->m_state.ulButtons != ctx->m_last_state.ulButtons) {
  1165. Uint8 hat = 0;
  1166. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH,
  1167. ((ctx->m_state.ulButtons & STEAM_BUTTON_SOUTH_MASK) != 0));
  1168. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST,
  1169. ((ctx->m_state.ulButtons & STEAM_BUTTON_EAST_MASK) != 0));
  1170. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST,
  1171. ((ctx->m_state.ulButtons & STEAM_BUTTON_WEST_MASK) != 0));
  1172. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH,
  1173. ((ctx->m_state.ulButtons & STEAM_BUTTON_NORTH_MASK) != 0));
  1174. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
  1175. ((ctx->m_state.ulButtons & STEAM_LEFT_BUMPER_MASK) != 0));
  1176. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER,
  1177. ((ctx->m_state.ulButtons & STEAM_RIGHT_BUMPER_MASK) != 0));
  1178. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK,
  1179. ((ctx->m_state.ulButtons & STEAM_BUTTON_MENU_MASK) != 0));
  1180. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START,
  1181. ((ctx->m_state.ulButtons & STEAM_BUTTON_ESCAPE_MASK) != 0));
  1182. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE,
  1183. ((ctx->m_state.ulButtons & STEAM_BUTTON_STEAM_MASK) != 0));
  1184. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK,
  1185. ((ctx->m_state.ulButtons & STEAM_JOYSTICK_BUTTON_MASK) != 0));
  1186. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_STEAM_LEFT_PADDLE,
  1187. ((ctx->m_state.ulButtons & STEAM_BUTTON_BACK_LEFT_MASK) != 0));
  1188. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_STEAM_RIGHT_PADDLE,
  1189. ((ctx->m_state.ulButtons & STEAM_BUTTON_BACK_RIGHT_MASK) != 0));
  1190. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK,
  1191. ((ctx->m_state.ulButtons & STEAM_BUTTON_RIGHTPAD_CLICKED_MASK) != 0));
  1192. if (ctx->m_state.ulButtons & STEAM_DPAD_UP_MASK) {
  1193. hat |= SDL_HAT_UP;
  1194. }
  1195. if (ctx->m_state.ulButtons & STEAM_DPAD_DOWN_MASK) {
  1196. hat |= SDL_HAT_DOWN;
  1197. }
  1198. if (ctx->m_state.ulButtons & STEAM_DPAD_LEFT_MASK) {
  1199. hat |= SDL_HAT_LEFT;
  1200. }
  1201. if (ctx->m_state.ulButtons & STEAM_DPAD_RIGHT_MASK) {
  1202. hat |= SDL_HAT_RIGHT;
  1203. }
  1204. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1205. }
  1206. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, (int)ctx->m_state.sTriggerL * 2 - 32768);
  1207. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, (int)ctx->m_state.sTriggerR * 2 - 32768);
  1208. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, ctx->m_state.sLeftStickX);
  1209. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, ~ctx->m_state.sLeftStickY);
  1210. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, ctx->m_state.sRightPadX);
  1211. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, ~ctx->m_state.sRightPadY);
  1212. if (ctx->report_sensors) {
  1213. float values[3];
  1214. ctx->sensor_timestamp += SDL_US_TO_NS(ctx->update_rate_in_us);
  1215. values[0] = (ctx->m_state.sGyroX / 32768.0f) * (2000.0f * (SDL_PI_F / 180.0f));
  1216. values[1] = (ctx->m_state.sGyroZ / 32768.0f) * (2000.0f * (SDL_PI_F / 180.0f));
  1217. values[2] = (ctx->m_state.sGyroY / 32768.0f) * (2000.0f * (SDL_PI_F / 180.0f));
  1218. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, ctx->sensor_timestamp, values, 3);
  1219. values[0] = (ctx->m_state.sAccelX / 32768.0f) * 2.0f * SDL_STANDARD_GRAVITY;
  1220. values[1] = (ctx->m_state.sAccelZ / 32768.0f) * 2.0f * SDL_STANDARD_GRAVITY;
  1221. values[2] = (-ctx->m_state.sAccelY / 32768.0f) * 2.0f * SDL_STANDARD_GRAVITY;
  1222. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, ctx->sensor_timestamp, values, 3);
  1223. }
  1224. ctx->m_last_state = ctx->m_state;
  1225. } else if (!ctx->connected && D0G_IS_WIRELESS_CONNECT(pPacket, nPacketLength)) {
  1226. ControllerConnected(device, &joystick);
  1227. } else if (ctx->connected && D0G_IS_WIRELESS_DISCONNECT(pPacket, nPacketLength)) {
  1228. ControllerDisconnected(device, &joystick);
  1229. }
  1230. }
  1231. return true;
  1232. }
  1233. static void HIDAPI_DriverSteam_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1234. {
  1235. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
  1236. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STEAM_HOME_LED,
  1237. SDL_HomeLEDHintChanged, ctx);
  1238. CloseSteamController(device);
  1239. }
  1240. static void HIDAPI_DriverSteam_FreeDevice(SDL_HIDAPI_Device *device)
  1241. {
  1242. SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
  1243. if (IsDongle(device->product_id)) {
  1244. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STEAM_PAIRING_ENABLED,
  1245. SDL_PairingEnabledHintChanged, ctx);
  1246. HIDAPI_DriverSteam_SetPairingState(ctx, false);
  1247. }
  1248. }
  1249. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam = {
  1250. SDL_HINT_JOYSTICK_HIDAPI_STEAM,
  1251. true,
  1252. HIDAPI_DriverSteam_RegisterHints,
  1253. HIDAPI_DriverSteam_UnregisterHints,
  1254. HIDAPI_DriverSteam_IsEnabled,
  1255. HIDAPI_DriverSteam_IsSupportedDevice,
  1256. HIDAPI_DriverSteam_InitDevice,
  1257. HIDAPI_DriverSteam_GetDevicePlayerIndex,
  1258. HIDAPI_DriverSteam_SetDevicePlayerIndex,
  1259. HIDAPI_DriverSteam_UpdateDevice,
  1260. HIDAPI_DriverSteam_OpenJoystick,
  1261. HIDAPI_DriverSteam_RumbleJoystick,
  1262. HIDAPI_DriverSteam_RumbleJoystickTriggers,
  1263. HIDAPI_DriverSteam_GetJoystickCapabilities,
  1264. HIDAPI_DriverSteam_SetJoystickLED,
  1265. HIDAPI_DriverSteam_SendJoystickEffect,
  1266. HIDAPI_DriverSteam_SetSensorsEnabled,
  1267. HIDAPI_DriverSteam_CloseJoystick,
  1268. HIDAPI_DriverSteam_FreeDevice,
  1269. };
  1270. #endif // SDL_JOYSTICK_HIDAPI_STEAM
  1271. #endif // SDL_JOYSTICK_HIDAPI