SDL_hidapi_switch.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837
  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. /* This driver supports the Nintendo Switch Pro controller.
  19. Code and logic contributed by Valve Corporation under the SDL zlib license.
  20. */
  21. #include "SDL_internal.h"
  22. #ifdef SDL_JOYSTICK_HIDAPI
  23. #include "../../SDL_hints_c.h"
  24. #include "../SDL_sysjoystick.h"
  25. #include "SDL_hidapijoystick_c.h"
  26. #include "SDL_hidapi_rumble.h"
  27. #include "SDL_hidapi_nintendo.h"
  28. #ifdef SDL_JOYSTICK_HIDAPI_SWITCH
  29. // Define this if you want to log all packets from the controller
  30. // #define DEBUG_SWITCH_PROTOCOL
  31. // Define this to get log output for rumble logic
  32. // #define DEBUG_RUMBLE
  33. /* The initialization sequence doesn't appear to work correctly on Windows unless
  34. the reads and writes are on the same thread.
  35. ... and now I can't reproduce this, so I'm leaving it in, but disabled for now.
  36. */
  37. // #define SWITCH_SYNCHRONOUS_WRITES
  38. /* How often you can write rumble commands to the controller.
  39. If you send commands more frequently than this, you can turn off the controller
  40. in Bluetooth mode, or the motors can miss the command in USB mode.
  41. */
  42. #define RUMBLE_WRITE_FREQUENCY_MS 30
  43. // How often you have to refresh a long duration rumble to keep the motors running
  44. #define RUMBLE_REFRESH_FREQUENCY_MS 50
  45. #define SWITCH_GYRO_SCALE 14.2842f
  46. #define SWITCH_ACCEL_SCALE 4096.f
  47. #define SWITCH_GYRO_SCALE_OFFSET 13371.0f
  48. #define SWITCH_GYRO_SCALE_MULT 936.0f
  49. #define SWITCH_ACCEL_SCALE_OFFSET 16384.0f
  50. #define SWITCH_ACCEL_SCALE_MULT 4.0f
  51. enum
  52. {
  53. SDL_GAMEPAD_BUTTON_SWITCH_SHARE = 11,
  54. SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE1,
  55. SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE1,
  56. SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE2,
  57. SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE2,
  58. SDL_GAMEPAD_NUM_SWITCH_BUTTONS,
  59. };
  60. typedef enum
  61. {
  62. k_eSwitchInputReportIDs_SubcommandReply = 0x21,
  63. k_eSwitchInputReportIDs_FullControllerState = 0x30,
  64. k_eSwitchInputReportIDs_FullControllerAndMcuState = 0x31,
  65. k_eSwitchInputReportIDs_SimpleControllerState = 0x3F,
  66. k_eSwitchInputReportIDs_CommandAck = 0x81,
  67. } ESwitchInputReportIDs;
  68. typedef enum
  69. {
  70. k_eSwitchOutputReportIDs_RumbleAndSubcommand = 0x01,
  71. k_eSwitchOutputReportIDs_Rumble = 0x10,
  72. k_eSwitchOutputReportIDs_Proprietary = 0x80,
  73. } ESwitchOutputReportIDs;
  74. typedef enum
  75. {
  76. k_eSwitchSubcommandIDs_BluetoothManualPair = 0x01,
  77. k_eSwitchSubcommandIDs_RequestDeviceInfo = 0x02,
  78. k_eSwitchSubcommandIDs_SetInputReportMode = 0x03,
  79. k_eSwitchSubcommandIDs_SetHCIState = 0x06,
  80. k_eSwitchSubcommandIDs_SPIFlashRead = 0x10,
  81. k_eSwitchSubcommandIDs_SetPlayerLights = 0x30,
  82. k_eSwitchSubcommandIDs_SetHomeLight = 0x38,
  83. k_eSwitchSubcommandIDs_EnableIMU = 0x40,
  84. k_eSwitchSubcommandIDs_SetIMUSensitivity = 0x41,
  85. k_eSwitchSubcommandIDs_EnableVibration = 0x48,
  86. } ESwitchSubcommandIDs;
  87. typedef enum
  88. {
  89. k_eSwitchProprietaryCommandIDs_Status = 0x01,
  90. k_eSwitchProprietaryCommandIDs_Handshake = 0x02,
  91. k_eSwitchProprietaryCommandIDs_HighSpeed = 0x03,
  92. k_eSwitchProprietaryCommandIDs_ForceUSB = 0x04,
  93. k_eSwitchProprietaryCommandIDs_ClearUSB = 0x05,
  94. k_eSwitchProprietaryCommandIDs_ResetMCU = 0x06,
  95. } ESwitchProprietaryCommandIDs;
  96. #define k_unSwitchOutputPacketDataLength 49
  97. #define k_unSwitchMaxOutputPacketLength 64
  98. #define k_unSwitchBluetoothPacketLength k_unSwitchOutputPacketDataLength
  99. #define k_unSwitchUSBPacketLength k_unSwitchMaxOutputPacketLength
  100. #define k_unSPIStickFactoryCalibrationStartOffset 0x603D
  101. #define k_unSPIStickFactoryCalibrationEndOffset 0x604E
  102. #define k_unSPIStickFactoryCalibrationLength (k_unSPIStickFactoryCalibrationEndOffset - k_unSPIStickFactoryCalibrationStartOffset + 1)
  103. #define k_unSPIStickUserCalibrationStartOffset 0x8010
  104. #define k_unSPIStickUserCalibrationEndOffset 0x8025
  105. #define k_unSPIStickUserCalibrationLength (k_unSPIStickUserCalibrationEndOffset - k_unSPIStickUserCalibrationStartOffset + 1)
  106. #define k_unSPIIMUScaleStartOffset 0x6020
  107. #define k_unSPIIMUScaleEndOffset 0x6037
  108. #define k_unSPIIMUScaleLength (k_unSPIIMUScaleEndOffset - k_unSPIIMUScaleStartOffset + 1)
  109. #define k_unSPIIMUUserScaleStartOffset 0x8026
  110. #define k_unSPIIMUUserScaleEndOffset 0x8039
  111. #define k_unSPIIMUUserScaleLength (k_unSPIIMUUserScaleEndOffset - k_unSPIIMUUserScaleStartOffset + 1)
  112. #pragma pack(1)
  113. typedef struct
  114. {
  115. Uint8 rgucButtons[2];
  116. Uint8 ucStickHat;
  117. Uint8 rgucJoystickLeft[2];
  118. Uint8 rgucJoystickRight[2];
  119. } SwitchInputOnlyControllerStatePacket_t;
  120. typedef struct
  121. {
  122. Uint8 rgucButtons[2];
  123. Uint8 ucStickHat;
  124. Sint16 sJoystickLeft[2];
  125. Sint16 sJoystickRight[2];
  126. } SwitchSimpleStatePacket_t;
  127. typedef struct
  128. {
  129. Uint8 ucCounter;
  130. Uint8 ucBatteryAndConnection;
  131. Uint8 rgucButtons[3];
  132. Uint8 rgucJoystickLeft[3];
  133. Uint8 rgucJoystickRight[3];
  134. Uint8 ucVibrationCode;
  135. } SwitchControllerStatePacket_t;
  136. typedef struct
  137. {
  138. SwitchControllerStatePacket_t controllerState;
  139. struct
  140. {
  141. Sint16 sAccelX;
  142. Sint16 sAccelY;
  143. Sint16 sAccelZ;
  144. Sint16 sGyroX;
  145. Sint16 sGyroY;
  146. Sint16 sGyroZ;
  147. } imuState[3];
  148. } SwitchStatePacket_t;
  149. typedef struct
  150. {
  151. Uint32 unAddress;
  152. Uint8 ucLength;
  153. } SwitchSPIOpData_t;
  154. typedef struct
  155. {
  156. SwitchControllerStatePacket_t m_controllerState;
  157. Uint8 ucSubcommandAck;
  158. Uint8 ucSubcommandID;
  159. #define k_unSubcommandDataBytes 35
  160. union
  161. {
  162. Uint8 rgucSubcommandData[k_unSubcommandDataBytes];
  163. struct
  164. {
  165. SwitchSPIOpData_t opData;
  166. Uint8 rgucReadData[k_unSubcommandDataBytes - sizeof(SwitchSPIOpData_t)];
  167. } spiReadData;
  168. struct
  169. {
  170. Uint8 rgucFirmwareVersion[2];
  171. Uint8 ucDeviceType;
  172. Uint8 ucFiller1;
  173. Uint8 rgucMACAddress[6];
  174. Uint8 ucFiller2;
  175. Uint8 ucColorLocation;
  176. } deviceInfo;
  177. struct
  178. {
  179. SwitchSPIOpData_t opData;
  180. Uint8 rgucLeftCalibration[9];
  181. Uint8 rgucRightCalibration[9];
  182. } stickFactoryCalibration;
  183. struct
  184. {
  185. SwitchSPIOpData_t opData;
  186. Uint8 rgucLeftMagic[2];
  187. Uint8 rgucLeftCalibration[9];
  188. Uint8 rgucRightMagic[2];
  189. Uint8 rgucRightCalibration[9];
  190. } stickUserCalibration;
  191. };
  192. } SwitchSubcommandInputPacket_t;
  193. typedef struct
  194. {
  195. Uint8 ucPacketType;
  196. Uint8 ucCommandID;
  197. Uint8 ucFiller;
  198. Uint8 ucDeviceType;
  199. Uint8 rgucMACAddress[6];
  200. } SwitchProprietaryStatusPacket_t;
  201. typedef struct
  202. {
  203. Uint8 rgucData[4];
  204. } SwitchRumbleData_t;
  205. typedef struct
  206. {
  207. Uint8 ucPacketType;
  208. Uint8 ucPacketNumber;
  209. SwitchRumbleData_t rumbleData[2];
  210. } SwitchCommonOutputPacket_t;
  211. typedef struct
  212. {
  213. SwitchCommonOutputPacket_t commonData;
  214. Uint8 ucSubcommandID;
  215. Uint8 rgucSubcommandData[k_unSwitchOutputPacketDataLength - sizeof(SwitchCommonOutputPacket_t) - 1];
  216. } SwitchSubcommandOutputPacket_t;
  217. typedef struct
  218. {
  219. Uint8 ucPacketType;
  220. Uint8 ucProprietaryID;
  221. Uint8 rgucProprietaryData[k_unSwitchOutputPacketDataLength - 1 - 1];
  222. } SwitchProprietaryOutputPacket_t;
  223. #pragma pack()
  224. /* Enhanced report hint mode:
  225. * "0": enhanced features are never used
  226. * "1": enhanced features are always used
  227. * "auto": enhanced features are advertised to the application, but SDL doesn't touch the controller state unless the application explicitly requests it.
  228. */
  229. typedef enum
  230. {
  231. SWITCH_ENHANCED_REPORT_HINT_OFF,
  232. SWITCH_ENHANCED_REPORT_HINT_ON,
  233. SWITCH_ENHANCED_REPORT_HINT_AUTO
  234. } HIDAPI_Switch_EnhancedReportHint;
  235. typedef struct
  236. {
  237. SDL_HIDAPI_Device *device;
  238. SDL_Joystick *joystick;
  239. bool m_bInputOnly;
  240. bool m_bUseButtonLabels;
  241. bool m_bPlayerLights;
  242. int m_nPlayerIndex;
  243. bool m_bSyncWrite;
  244. int m_nMaxWriteAttempts;
  245. ESwitchDeviceInfoControllerType m_eControllerType;
  246. Uint8 m_nInitialInputMode;
  247. Uint8 m_nCurrentInputMode;
  248. Uint8 m_rgucMACAddress[6];
  249. Uint8 m_nCommandNumber;
  250. HIDAPI_Switch_EnhancedReportHint m_eEnhancedReportHint;
  251. bool m_bEnhancedMode;
  252. bool m_bEnhancedModeAvailable;
  253. SwitchCommonOutputPacket_t m_RumblePacket;
  254. Uint8 m_rgucReadBuffer[k_unSwitchMaxOutputPacketLength];
  255. bool m_bRumbleActive;
  256. Uint64 m_ulRumbleSent;
  257. bool m_bRumblePending;
  258. bool m_bRumbleZeroPending;
  259. Uint32 m_unRumblePending;
  260. bool m_bSensorsSupported;
  261. bool m_bReportSensors;
  262. bool m_bHasSensorData;
  263. Uint64 m_ulLastInput;
  264. Uint64 m_ulLastIMUReset;
  265. Uint64 m_ulIMUSampleTimestampNS;
  266. Uint32 m_unIMUSamples;
  267. Uint64 m_ulIMUUpdateIntervalNS;
  268. Uint64 m_ulTimestampNS;
  269. bool m_bVerticalMode;
  270. SwitchInputOnlyControllerStatePacket_t m_lastInputOnlyState;
  271. SwitchSimpleStatePacket_t m_lastSimpleState;
  272. SwitchStatePacket_t m_lastFullState;
  273. struct StickCalibrationData
  274. {
  275. struct
  276. {
  277. Sint16 sCenter;
  278. Sint16 sMin;
  279. Sint16 sMax;
  280. } axis[2];
  281. } m_StickCalData[2];
  282. struct StickExtents
  283. {
  284. struct
  285. {
  286. Sint16 sMin;
  287. Sint16 sMax;
  288. } axis[2];
  289. } m_StickExtents[2], m_SimpleStickExtents[2];
  290. struct IMUScaleData
  291. {
  292. float fAccelScaleX;
  293. float fAccelScaleY;
  294. float fAccelScaleZ;
  295. float fGyroScaleX;
  296. float fGyroScaleY;
  297. float fGyroScaleZ;
  298. } m_IMUScaleData;
  299. } SDL_DriverSwitch_Context;
  300. static int ReadInput(SDL_DriverSwitch_Context *ctx)
  301. {
  302. int result;
  303. // Make sure we don't try to read at the same time a write is happening
  304. if (SDL_GetAtomicInt(&ctx->device->rumble_pending) > 0) {
  305. return 0;
  306. }
  307. result = SDL_hid_read_timeout(ctx->device->dev, ctx->m_rgucReadBuffer, sizeof(ctx->m_rgucReadBuffer), 0);
  308. // See if we can guess the initial input mode
  309. if (result > 0 && !ctx->m_bInputOnly && !ctx->m_nInitialInputMode) {
  310. switch (ctx->m_rgucReadBuffer[0]) {
  311. case k_eSwitchInputReportIDs_FullControllerState:
  312. case k_eSwitchInputReportIDs_FullControllerAndMcuState:
  313. case k_eSwitchInputReportIDs_SimpleControllerState:
  314. ctx->m_nInitialInputMode = ctx->m_rgucReadBuffer[0];
  315. break;
  316. default:
  317. break;
  318. }
  319. }
  320. return result;
  321. }
  322. static int WriteOutput(SDL_DriverSwitch_Context *ctx, const Uint8 *data, int size)
  323. {
  324. #ifdef SWITCH_SYNCHRONOUS_WRITES
  325. return SDL_hid_write(ctx->device->dev, data, size);
  326. #else
  327. // Use the rumble thread for general asynchronous writes
  328. if (!SDL_HIDAPI_LockRumble()) {
  329. return -1;
  330. }
  331. return SDL_HIDAPI_SendRumbleAndUnlock(ctx->device, data, size);
  332. #endif // SWITCH_SYNCHRONOUS_WRITES
  333. }
  334. static SwitchSubcommandInputPacket_t *ReadSubcommandReply(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs expectedID)
  335. {
  336. // Average response time for messages is ~30ms
  337. Uint64 endTicks = SDL_GetTicks() + 100;
  338. int nRead = 0;
  339. while ((nRead = ReadInput(ctx)) != -1) {
  340. if (nRead > 0) {
  341. if (ctx->m_rgucReadBuffer[0] == k_eSwitchInputReportIDs_SubcommandReply) {
  342. SwitchSubcommandInputPacket_t *reply = (SwitchSubcommandInputPacket_t *)&ctx->m_rgucReadBuffer[1];
  343. if (reply->ucSubcommandID == expectedID && (reply->ucSubcommandAck & 0x80)) {
  344. return reply;
  345. }
  346. }
  347. } else {
  348. SDL_Delay(1);
  349. }
  350. if (SDL_GetTicks() >= endTicks) {
  351. break;
  352. }
  353. }
  354. return NULL;
  355. }
  356. static bool ReadProprietaryReply(SDL_DriverSwitch_Context *ctx, ESwitchProprietaryCommandIDs expectedID)
  357. {
  358. // Average response time for messages is ~30ms
  359. Uint64 endTicks = SDL_GetTicks() + 100;
  360. int nRead = 0;
  361. while ((nRead = ReadInput(ctx)) != -1) {
  362. if (nRead > 0) {
  363. if (ctx->m_rgucReadBuffer[0] == k_eSwitchInputReportIDs_CommandAck && ctx->m_rgucReadBuffer[1] == expectedID) {
  364. return true;
  365. }
  366. } else {
  367. SDL_Delay(1);
  368. }
  369. if (SDL_GetTicks() >= endTicks) {
  370. break;
  371. }
  372. }
  373. return false;
  374. }
  375. static void ConstructSubcommand(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs ucCommandID, const Uint8 *pBuf, Uint8 ucLen, SwitchSubcommandOutputPacket_t *outPacket)
  376. {
  377. SDL_memset(outPacket, 0, sizeof(*outPacket));
  378. outPacket->commonData.ucPacketType = k_eSwitchOutputReportIDs_RumbleAndSubcommand;
  379. outPacket->commonData.ucPacketNumber = ctx->m_nCommandNumber;
  380. SDL_memcpy(outPacket->commonData.rumbleData, ctx->m_RumblePacket.rumbleData, sizeof(ctx->m_RumblePacket.rumbleData));
  381. outPacket->ucSubcommandID = ucCommandID;
  382. if (pBuf) {
  383. SDL_memcpy(outPacket->rgucSubcommandData, pBuf, ucLen);
  384. }
  385. ctx->m_nCommandNumber = (ctx->m_nCommandNumber + 1) & 0xF;
  386. }
  387. static bool WritePacket(SDL_DriverSwitch_Context *ctx, void *pBuf, Uint8 ucLen)
  388. {
  389. Uint8 rgucBuf[k_unSwitchMaxOutputPacketLength];
  390. const size_t unWriteSize = ctx->device->is_bluetooth ? k_unSwitchBluetoothPacketLength : k_unSwitchUSBPacketLength;
  391. if (ucLen > k_unSwitchOutputPacketDataLength) {
  392. return false;
  393. }
  394. if (ucLen < unWriteSize) {
  395. SDL_memcpy(rgucBuf, pBuf, ucLen);
  396. SDL_memset(rgucBuf + ucLen, 0, unWriteSize - ucLen);
  397. pBuf = rgucBuf;
  398. ucLen = (Uint8)unWriteSize;
  399. }
  400. if (ctx->m_bSyncWrite) {
  401. return SDL_hid_write(ctx->device->dev, (Uint8 *)pBuf, ucLen) >= 0;
  402. } else {
  403. return WriteOutput(ctx, (Uint8 *)pBuf, ucLen) >= 0;
  404. }
  405. }
  406. static bool WriteSubcommand(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs ucCommandID, const Uint8 *pBuf, Uint8 ucLen, SwitchSubcommandInputPacket_t **ppReply)
  407. {
  408. SwitchSubcommandInputPacket_t *reply = NULL;
  409. int nTries;
  410. for (nTries = 1; !reply && nTries <= ctx->m_nMaxWriteAttempts; ++nTries) {
  411. SwitchSubcommandOutputPacket_t commandPacket;
  412. ConstructSubcommand(ctx, ucCommandID, pBuf, ucLen, &commandPacket);
  413. if (!WritePacket(ctx, &commandPacket, sizeof(commandPacket))) {
  414. continue;
  415. }
  416. reply = ReadSubcommandReply(ctx, ucCommandID);
  417. }
  418. if (ppReply) {
  419. *ppReply = reply;
  420. }
  421. return reply != NULL;
  422. }
  423. static bool WriteProprietary(SDL_DriverSwitch_Context *ctx, ESwitchProprietaryCommandIDs ucCommand, Uint8 *pBuf, Uint8 ucLen, bool waitForReply)
  424. {
  425. int nTries;
  426. for (nTries = 1; nTries <= ctx->m_nMaxWriteAttempts; ++nTries) {
  427. SwitchProprietaryOutputPacket_t packet;
  428. if ((!pBuf && ucLen > 0) || ucLen > sizeof(packet.rgucProprietaryData)) {
  429. return false;
  430. }
  431. SDL_zero(packet);
  432. packet.ucPacketType = k_eSwitchOutputReportIDs_Proprietary;
  433. packet.ucProprietaryID = ucCommand;
  434. if (pBuf) {
  435. SDL_memcpy(packet.rgucProprietaryData, pBuf, ucLen);
  436. }
  437. if (!WritePacket(ctx, &packet, sizeof(packet))) {
  438. continue;
  439. }
  440. if (!waitForReply || ReadProprietaryReply(ctx, ucCommand)) {
  441. // SDL_Log("Succeeded%s after %d tries", ctx->m_bSyncWrite ? " (sync)" : "", nTries);
  442. return true;
  443. }
  444. }
  445. // SDL_Log("Failed%s after %d tries", ctx->m_bSyncWrite ? " (sync)" : "", nTries);
  446. return false;
  447. }
  448. static Uint8 EncodeRumbleHighAmplitude(Uint16 amplitude)
  449. {
  450. /* More information about these values can be found here:
  451. * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
  452. */
  453. Uint16 hfa[101][2] = { { 0, 0x0 }, { 514, 0x2 }, { 775, 0x4 }, { 921, 0x6 }, { 1096, 0x8 }, { 1303, 0x0a }, { 1550, 0x0c }, { 1843, 0x0e }, { 2192, 0x10 }, { 2606, 0x12 }, { 3100, 0x14 }, { 3686, 0x16 }, { 4383, 0x18 }, { 5213, 0x1a }, { 6199, 0x1c }, { 7372, 0x1e }, { 7698, 0x20 }, { 8039, 0x22 }, { 8395, 0x24 }, { 8767, 0x26 }, { 9155, 0x28 }, { 9560, 0x2a }, { 9984, 0x2c }, { 10426, 0x2e }, { 10887, 0x30 }, { 11369, 0x32 }, { 11873, 0x34 }, { 12398, 0x36 }, { 12947, 0x38 }, { 13520, 0x3a }, { 14119, 0x3c }, { 14744, 0x3e }, { 15067, 0x40 }, { 15397, 0x42 }, { 15734, 0x44 }, { 16079, 0x46 }, { 16431, 0x48 }, { 16790, 0x4a }, { 17158, 0x4c }, { 17534, 0x4e }, { 17918, 0x50 }, { 18310, 0x52 }, { 18711, 0x54 }, { 19121, 0x56 }, { 19540, 0x58 }, { 19967, 0x5a }, { 20405, 0x5c }, { 20851, 0x5e }, { 21308, 0x60 }, { 21775, 0x62 }, { 22251, 0x64 }, { 22739, 0x66 }, { 23236, 0x68 }, { 23745, 0x6a }, { 24265, 0x6c }, { 24797, 0x6e }, { 25340, 0x70 }, { 25894, 0x72 }, { 26462, 0x74 }, { 27041, 0x76 }, { 27633, 0x78 }, { 28238, 0x7a }, { 28856, 0x7c }, { 29488, 0x7e }, { 30134, 0x80 }, { 30794, 0x82 }, { 31468, 0x84 }, { 32157, 0x86 }, { 32861, 0x88 }, { 33581, 0x8a }, { 34316, 0x8c }, { 35068, 0x8e }, { 35836, 0x90 }, { 36620, 0x92 }, { 37422, 0x94 }, { 38242, 0x96 }, { 39079, 0x98 }, { 39935, 0x9a }, { 40809, 0x9c }, { 41703, 0x9e }, { 42616, 0xa0 }, { 43549, 0xa2 }, { 44503, 0xa4 }, { 45477, 0xa6 }, { 46473, 0xa8 }, { 47491, 0xaa }, { 48531, 0xac }, { 49593, 0xae }, { 50679, 0xb0 }, { 51789, 0xb2 }, { 52923, 0xb4 }, { 54082, 0xb6 }, { 55266, 0xb8 }, { 56476, 0xba }, { 57713, 0xbc }, { 58977, 0xbe }, { 60268, 0xc0 }, { 61588, 0xc2 }, { 62936, 0xc4 }, { 64315, 0xc6 }, { 65535, 0xc8 } };
  454. int index = 0;
  455. for (; index < 101; index++) {
  456. if (amplitude <= hfa[index][0]) {
  457. return (Uint8)hfa[index][1];
  458. }
  459. }
  460. return (Uint8)hfa[100][1];
  461. }
  462. static Uint16 EncodeRumbleLowAmplitude(Uint16 amplitude)
  463. {
  464. /* More information about these values can be found here:
  465. * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
  466. */
  467. Uint16 lfa[101][2] = { { 0, 0x0040 }, { 514, 0x8040 }, { 775, 0x0041 }, { 921, 0x8041 }, { 1096, 0x0042 }, { 1303, 0x8042 }, { 1550, 0x0043 }, { 1843, 0x8043 }, { 2192, 0x0044 }, { 2606, 0x8044 }, { 3100, 0x0045 }, { 3686, 0x8045 }, { 4383, 0x0046 }, { 5213, 0x8046 }, { 6199, 0x0047 }, { 7372, 0x8047 }, { 7698, 0x0048 }, { 8039, 0x8048 }, { 8395, 0x0049 }, { 8767, 0x8049 }, { 9155, 0x004a }, { 9560, 0x804a }, { 9984, 0x004b }, { 10426, 0x804b }, { 10887, 0x004c }, { 11369, 0x804c }, { 11873, 0x004d }, { 12398, 0x804d }, { 12947, 0x004e }, { 13520, 0x804e }, { 14119, 0x004f }, { 14744, 0x804f }, { 15067, 0x0050 }, { 15397, 0x8050 }, { 15734, 0x0051 }, { 16079, 0x8051 }, { 16431, 0x0052 }, { 16790, 0x8052 }, { 17158, 0x0053 }, { 17534, 0x8053 }, { 17918, 0x0054 }, { 18310, 0x8054 }, { 18711, 0x0055 }, { 19121, 0x8055 }, { 19540, 0x0056 }, { 19967, 0x8056 }, { 20405, 0x0057 }, { 20851, 0x8057 }, { 21308, 0x0058 }, { 21775, 0x8058 }, { 22251, 0x0059 }, { 22739, 0x8059 }, { 23236, 0x005a }, { 23745, 0x805a }, { 24265, 0x005b }, { 24797, 0x805b }, { 25340, 0x005c }, { 25894, 0x805c }, { 26462, 0x005d }, { 27041, 0x805d }, { 27633, 0x005e }, { 28238, 0x805e }, { 28856, 0x005f }, { 29488, 0x805f }, { 30134, 0x0060 }, { 30794, 0x8060 }, { 31468, 0x0061 }, { 32157, 0x8061 }, { 32861, 0x0062 }, { 33581, 0x8062 }, { 34316, 0x0063 }, { 35068, 0x8063 }, { 35836, 0x0064 }, { 36620, 0x8064 }, { 37422, 0x0065 }, { 38242, 0x8065 }, { 39079, 0x0066 }, { 39935, 0x8066 }, { 40809, 0x0067 }, { 41703, 0x8067 }, { 42616, 0x0068 }, { 43549, 0x8068 }, { 44503, 0x0069 }, { 45477, 0x8069 }, { 46473, 0x006a }, { 47491, 0x806a }, { 48531, 0x006b }, { 49593, 0x806b }, { 50679, 0x006c }, { 51789, 0x806c }, { 52923, 0x006d }, { 54082, 0x806d }, { 55266, 0x006e }, { 56476, 0x806e }, { 57713, 0x006f }, { 58977, 0x806f }, { 60268, 0x0070 }, { 61588, 0x8070 }, { 62936, 0x0071 }, { 64315, 0x8071 }, { 65535, 0x0072 } };
  468. int index = 0;
  469. for (; index < 101; index++) {
  470. if (amplitude <= lfa[index][0]) {
  471. return lfa[index][1];
  472. }
  473. }
  474. return lfa[100][1];
  475. }
  476. static void SetNeutralRumble(SwitchRumbleData_t *pRumble)
  477. {
  478. pRumble->rgucData[0] = 0x00;
  479. pRumble->rgucData[1] = 0x01;
  480. pRumble->rgucData[2] = 0x40;
  481. pRumble->rgucData[3] = 0x40;
  482. }
  483. static void EncodeRumble(SwitchRumbleData_t *pRumble, Uint16 usHighFreq, Uint8 ucHighFreqAmp, Uint8 ucLowFreq, Uint16 usLowFreqAmp)
  484. {
  485. if (ucHighFreqAmp > 0 || usLowFreqAmp > 0) {
  486. // High-band frequency and low-band amplitude are actually nine-bits each so they
  487. // take a bit from the high-band amplitude and low-band frequency bytes respectively
  488. pRumble->rgucData[0] = usHighFreq & 0xFF;
  489. pRumble->rgucData[1] = ucHighFreqAmp | ((usHighFreq >> 8) & 0x01);
  490. pRumble->rgucData[2] = ucLowFreq | ((usLowFreqAmp >> 8) & 0x80);
  491. pRumble->rgucData[3] = usLowFreqAmp & 0xFF;
  492. #ifdef DEBUG_RUMBLE
  493. SDL_Log("Freq: %.2X %.2X %.2X, Amp: %.2X %.2X %.2X",
  494. usHighFreq & 0xFF, ((usHighFreq >> 8) & 0x01), ucLowFreq,
  495. ucHighFreqAmp, ((usLowFreqAmp >> 8) & 0x80), usLowFreqAmp & 0xFF);
  496. #endif
  497. } else {
  498. SetNeutralRumble(pRumble);
  499. }
  500. }
  501. static bool WriteRumble(SDL_DriverSwitch_Context *ctx)
  502. {
  503. /* Write into m_RumblePacket rather than a temporary buffer to allow the current rumble state
  504. * to be retained for subsequent rumble or subcommand packets sent to the controller
  505. */
  506. ctx->m_RumblePacket.ucPacketType = k_eSwitchOutputReportIDs_Rumble;
  507. ctx->m_RumblePacket.ucPacketNumber = ctx->m_nCommandNumber;
  508. ctx->m_nCommandNumber = (ctx->m_nCommandNumber + 1) & 0xF;
  509. // Refresh the rumble state periodically
  510. ctx->m_ulRumbleSent = SDL_GetTicks();
  511. return WritePacket(ctx, (Uint8 *)&ctx->m_RumblePacket, sizeof(ctx->m_RumblePacket));
  512. }
  513. static ESwitchDeviceInfoControllerType CalculateControllerType(SDL_DriverSwitch_Context *ctx, ESwitchDeviceInfoControllerType eControllerType)
  514. {
  515. SDL_HIDAPI_Device *device = ctx->device;
  516. // The N64 controller reports as a Pro controller over USB
  517. if (eControllerType == k_eSwitchDeviceInfoControllerType_ProController &&
  518. device->product_id == USB_PRODUCT_NINTENDO_N64_CONTROLLER) {
  519. eControllerType = k_eSwitchDeviceInfoControllerType_N64;
  520. }
  521. if (eControllerType == k_eSwitchDeviceInfoControllerType_Unknown) {
  522. // This might be a Joy-Con that's missing from a charging grip slot
  523. if (device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
  524. if (device->interface_number == 1) {
  525. eControllerType = k_eSwitchDeviceInfoControllerType_JoyConLeft;
  526. } else {
  527. eControllerType = k_eSwitchDeviceInfoControllerType_JoyConRight;
  528. }
  529. }
  530. }
  531. return eControllerType;
  532. }
  533. static bool BReadDeviceInfo(SDL_DriverSwitch_Context *ctx)
  534. {
  535. SwitchSubcommandInputPacket_t *reply = NULL;
  536. if (ctx->device->is_bluetooth) {
  537. if (WriteSubcommand(ctx, k_eSwitchSubcommandIDs_RequestDeviceInfo, NULL, 0, &reply)) {
  538. // Byte 2: Controller ID (1=LJC, 2=RJC, 3=Pro)
  539. ctx->m_eControllerType = CalculateControllerType(ctx, (ESwitchDeviceInfoControllerType)reply->deviceInfo.ucDeviceType);
  540. // Bytes 4-9: MAC address (big-endian)
  541. SDL_memcpy(ctx->m_rgucMACAddress, reply->deviceInfo.rgucMACAddress, sizeof(ctx->m_rgucMACAddress));
  542. return true;
  543. }
  544. } else {
  545. if (WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Status, NULL, 0, true)) {
  546. SwitchProprietaryStatusPacket_t *status = (SwitchProprietaryStatusPacket_t *)&ctx->m_rgucReadBuffer[0];
  547. size_t i;
  548. ctx->m_eControllerType = CalculateControllerType(ctx, (ESwitchDeviceInfoControllerType)status->ucDeviceType);
  549. for (i = 0; i < sizeof(ctx->m_rgucMACAddress); ++i) {
  550. ctx->m_rgucMACAddress[i] = status->rgucMACAddress[sizeof(ctx->m_rgucMACAddress) - i - 1];
  551. }
  552. return true;
  553. }
  554. }
  555. return false;
  556. }
  557. static bool BTrySetupUSB(SDL_DriverSwitch_Context *ctx)
  558. {
  559. /* We have to send a connection handshake to the controller when communicating over USB
  560. * before we're able to send it other commands. Luckily this command is not supported
  561. * over Bluetooth, so we can use the controller's lack of response as a way to
  562. * determine if the connection is over USB or Bluetooth
  563. */
  564. if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Handshake, NULL, 0, true)) {
  565. return false;
  566. }
  567. if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_HighSpeed, NULL, 0, true)) {
  568. // The 8BitDo M30 and SF30 Pro don't respond to this command, but otherwise work correctly
  569. // return false;
  570. }
  571. if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Handshake, NULL, 0, true)) {
  572. // This fails on the right Joy-Con when plugged into the charging grip
  573. // return false;
  574. }
  575. if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, false)) {
  576. return false;
  577. }
  578. return true;
  579. }
  580. static bool SetVibrationEnabled(SDL_DriverSwitch_Context *ctx, Uint8 enabled)
  581. {
  582. return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_EnableVibration, &enabled, sizeof(enabled), NULL);
  583. }
  584. static bool SetInputMode(SDL_DriverSwitch_Context *ctx, Uint8 input_mode)
  585. {
  586. #ifdef FORCE_SIMPLE_REPORTS
  587. input_mode = k_eSwitchInputReportIDs_SimpleControllerState;
  588. #endif
  589. #ifdef FORCE_FULL_REPORTS
  590. input_mode = k_eSwitchInputReportIDs_FullControllerState;
  591. #endif
  592. if (input_mode == ctx->m_nCurrentInputMode) {
  593. return true;
  594. } else {
  595. ctx->m_nCurrentInputMode = input_mode;
  596. return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetInputReportMode, &input_mode, sizeof(input_mode), NULL);
  597. }
  598. }
  599. static bool SetHomeLED(SDL_DriverSwitch_Context *ctx, Uint8 brightness)
  600. {
  601. Uint8 ucLedIntensity = 0;
  602. Uint8 rgucBuffer[4];
  603. if (brightness > 0) {
  604. if (brightness < 65) {
  605. ucLedIntensity = (brightness + 5) / 10;
  606. } else {
  607. ucLedIntensity = (Uint8)SDL_ceilf(0xF * SDL_powf((float)brightness / 100.f, 2.13f));
  608. }
  609. }
  610. rgucBuffer[0] = (0x0 << 4) | 0x1; // 0 mini cycles (besides first), cycle duration 8ms
  611. rgucBuffer[1] = ((ucLedIntensity & 0xF) << 4) | 0x0; // LED start intensity (0x0-0xF), 0 cycles (LED stays on at start intensity after first cycle)
  612. rgucBuffer[2] = ((ucLedIntensity & 0xF) << 4) | 0x0; // First cycle LED intensity, 0x0 intensity for second cycle
  613. rgucBuffer[3] = (0x0 << 4) | 0x0; // 8ms fade transition to first cycle, 8ms first cycle LED duration
  614. return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetHomeLight, rgucBuffer, sizeof(rgucBuffer), NULL);
  615. }
  616. static void SDLCALL SDL_HomeLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  617. {
  618. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)userdata;
  619. if (hint && *hint) {
  620. int value;
  621. if (SDL_strchr(hint, '.') != NULL) {
  622. value = (int)(100.0f * SDL_atof(hint));
  623. if (value > 255) {
  624. value = 255;
  625. }
  626. } else if (SDL_GetStringBoolean(hint, true)) {
  627. value = 100;
  628. } else {
  629. value = 0;
  630. }
  631. SetHomeLED(ctx, (Uint8)value);
  632. }
  633. }
  634. static void UpdateSlotLED(SDL_DriverSwitch_Context *ctx)
  635. {
  636. if (!ctx->m_bInputOnly) {
  637. Uint8 led_data = 0;
  638. if (ctx->m_bPlayerLights && ctx->m_nPlayerIndex >= 0) {
  639. led_data = (1 << (ctx->m_nPlayerIndex % 4));
  640. }
  641. WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetPlayerLights, &led_data, sizeof(led_data), NULL);
  642. }
  643. }
  644. static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  645. {
  646. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)userdata;
  647. bool bPlayerLights = SDL_GetStringBoolean(hint, true);
  648. if (bPlayerLights != ctx->m_bPlayerLights) {
  649. ctx->m_bPlayerLights = bPlayerLights;
  650. UpdateSlotLED(ctx);
  651. HIDAPI_UpdateDeviceProperties(ctx->device);
  652. }
  653. }
  654. static void GetInitialInputMode(SDL_DriverSwitch_Context *ctx)
  655. {
  656. if (!ctx->m_nInitialInputMode) {
  657. // This will set the initial input mode if it can
  658. ReadInput(ctx);
  659. }
  660. }
  661. static Uint8 GetDefaultInputMode(SDL_DriverSwitch_Context *ctx)
  662. {
  663. Uint8 input_mode;
  664. // Determine the desired input mode
  665. if (ctx->m_nInitialInputMode) {
  666. input_mode = ctx->m_nInitialInputMode;
  667. } else {
  668. if (ctx->device->is_bluetooth) {
  669. input_mode = k_eSwitchInputReportIDs_SimpleControllerState;
  670. } else {
  671. input_mode = k_eSwitchInputReportIDs_FullControllerState;
  672. }
  673. }
  674. switch (ctx->m_eEnhancedReportHint) {
  675. case SWITCH_ENHANCED_REPORT_HINT_OFF:
  676. input_mode = k_eSwitchInputReportIDs_SimpleControllerState;
  677. break;
  678. case SWITCH_ENHANCED_REPORT_HINT_ON:
  679. if (input_mode == k_eSwitchInputReportIDs_SimpleControllerState) {
  680. input_mode = k_eSwitchInputReportIDs_FullControllerState;
  681. }
  682. break;
  683. case SWITCH_ENHANCED_REPORT_HINT_AUTO:
  684. /* Joy-Con controllers switch their thumbsticks into D-pad mode in simple mode,
  685. * so let's enable full controller state for them.
  686. */
  687. if (ctx->device->vendor_id == USB_VENDOR_NINTENDO &&
  688. (ctx->device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT ||
  689. ctx->device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT)) {
  690. input_mode = k_eSwitchInputReportIDs_FullControllerState;
  691. }
  692. break;
  693. }
  694. return input_mode;
  695. }
  696. static Uint8 GetSensorInputMode(SDL_DriverSwitch_Context *ctx)
  697. {
  698. Uint8 input_mode;
  699. // Determine the desired input mode
  700. if (!ctx->m_nInitialInputMode ||
  701. ctx->m_nInitialInputMode == k_eSwitchInputReportIDs_SimpleControllerState) {
  702. input_mode = k_eSwitchInputReportIDs_FullControllerState;
  703. } else {
  704. input_mode = ctx->m_nInitialInputMode;
  705. }
  706. return input_mode;
  707. }
  708. static void UpdateInputMode(SDL_DriverSwitch_Context *ctx)
  709. {
  710. Uint8 input_mode;
  711. if (ctx->m_bReportSensors) {
  712. input_mode = GetSensorInputMode(ctx);
  713. } else {
  714. input_mode = GetDefaultInputMode(ctx);
  715. }
  716. SetInputMode(ctx, input_mode);
  717. }
  718. static void SetEnhancedModeAvailable(SDL_DriverSwitch_Context *ctx)
  719. {
  720. if (ctx->m_bEnhancedModeAvailable) {
  721. return;
  722. }
  723. ctx->m_bEnhancedModeAvailable = true;
  724. if (ctx->m_bSensorsSupported) {
  725. // Use the right sensor in the combined Joy-Con pair
  726. if (!ctx->device->parent ||
  727. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  728. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_GYRO, 200.0f);
  729. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL, 200.0f);
  730. }
  731. if (ctx->device->parent &&
  732. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  733. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_GYRO_L, 200.0f);
  734. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL_L, 200.0f);
  735. }
  736. if (ctx->device->parent &&
  737. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  738. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_GYRO_R, 200.0f);
  739. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL_R, 200.0f);
  740. }
  741. }
  742. }
  743. static void SetEnhancedReportHint(SDL_DriverSwitch_Context *ctx, HIDAPI_Switch_EnhancedReportHint eEnhancedReportHint)
  744. {
  745. ctx->m_eEnhancedReportHint = eEnhancedReportHint;
  746. switch (eEnhancedReportHint) {
  747. case SWITCH_ENHANCED_REPORT_HINT_OFF:
  748. ctx->m_bEnhancedMode = false;
  749. break;
  750. case SWITCH_ENHANCED_REPORT_HINT_ON:
  751. SetEnhancedModeAvailable(ctx);
  752. ctx->m_bEnhancedMode = true;
  753. break;
  754. case SWITCH_ENHANCED_REPORT_HINT_AUTO:
  755. SetEnhancedModeAvailable(ctx);
  756. break;
  757. }
  758. UpdateInputMode(ctx);
  759. }
  760. static void UpdateEnhancedModeOnEnhancedReport(SDL_DriverSwitch_Context *ctx)
  761. {
  762. if (ctx->m_eEnhancedReportHint == SWITCH_ENHANCED_REPORT_HINT_AUTO) {
  763. SetEnhancedReportHint(ctx, SWITCH_ENHANCED_REPORT_HINT_ON);
  764. }
  765. }
  766. static void UpdateEnhancedModeOnApplicationUsage(SDL_DriverSwitch_Context *ctx)
  767. {
  768. if (ctx->m_eEnhancedReportHint == SWITCH_ENHANCED_REPORT_HINT_AUTO) {
  769. SetEnhancedReportHint(ctx, SWITCH_ENHANCED_REPORT_HINT_ON);
  770. }
  771. }
  772. static void SDLCALL SDL_EnhancedReportsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  773. {
  774. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)userdata;
  775. if (hint && SDL_strcasecmp(hint, "auto") == 0) {
  776. SetEnhancedReportHint(ctx, SWITCH_ENHANCED_REPORT_HINT_AUTO);
  777. } else if (SDL_GetStringBoolean(hint, true)) {
  778. SetEnhancedReportHint(ctx, SWITCH_ENHANCED_REPORT_HINT_ON);
  779. } else {
  780. SetEnhancedReportHint(ctx, SWITCH_ENHANCED_REPORT_HINT_OFF);
  781. }
  782. }
  783. static bool SetIMUEnabled(SDL_DriverSwitch_Context *ctx, bool enabled)
  784. {
  785. Uint8 imu_data = enabled ? 1 : 0;
  786. return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_EnableIMU, &imu_data, sizeof(imu_data), NULL);
  787. }
  788. static bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx)
  789. {
  790. Uint8 *pLeftStickCal;
  791. Uint8 *pRightStickCal;
  792. size_t stick, axis;
  793. SwitchSubcommandInputPacket_t *user_reply = NULL;
  794. SwitchSubcommandInputPacket_t *factory_reply = NULL;
  795. SwitchSPIOpData_t readUserParams;
  796. SwitchSPIOpData_t readFactoryParams;
  797. // Read User Calibration Info
  798. readUserParams.unAddress = k_unSPIStickUserCalibrationStartOffset;
  799. readUserParams.ucLength = k_unSPIStickUserCalibrationLength;
  800. // This isn't readable on all controllers, so ignore failure
  801. WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readUserParams, sizeof(readUserParams), &user_reply);
  802. // Read Factory Calibration Info
  803. readFactoryParams.unAddress = k_unSPIStickFactoryCalibrationStartOffset;
  804. readFactoryParams.ucLength = k_unSPIStickFactoryCalibrationLength;
  805. if (!WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readFactoryParams, sizeof(readFactoryParams), &factory_reply)) {
  806. return false;
  807. }
  808. // Automatically select the user calibration if magic bytes are set
  809. if (user_reply && user_reply->stickUserCalibration.rgucLeftMagic[0] == 0xB2 && user_reply->stickUserCalibration.rgucLeftMagic[1] == 0xA1) {
  810. pLeftStickCal = user_reply->stickUserCalibration.rgucLeftCalibration;
  811. } else {
  812. pLeftStickCal = factory_reply->stickFactoryCalibration.rgucLeftCalibration;
  813. }
  814. if (user_reply && user_reply->stickUserCalibration.rgucRightMagic[0] == 0xB2 && user_reply->stickUserCalibration.rgucRightMagic[1] == 0xA1) {
  815. pRightStickCal = user_reply->stickUserCalibration.rgucRightCalibration;
  816. } else {
  817. pRightStickCal = factory_reply->stickFactoryCalibration.rgucRightCalibration;
  818. }
  819. /* Stick calibration values are 12-bits each and are packed by bit
  820. * For whatever reason the fields are in a different order for each stick
  821. * Left: X-Max, Y-Max, X-Center, Y-Center, X-Min, Y-Min
  822. * Right: X-Center, Y-Center, X-Min, Y-Min, X-Max, Y-Max
  823. */
  824. // Left stick
  825. ctx->m_StickCalData[0].axis[0].sMax = ((pLeftStickCal[1] << 8) & 0xF00) | pLeftStickCal[0]; // X Axis max above center
  826. ctx->m_StickCalData[0].axis[1].sMax = (pLeftStickCal[2] << 4) | (pLeftStickCal[1] >> 4); // Y Axis max above center
  827. ctx->m_StickCalData[0].axis[0].sCenter = ((pLeftStickCal[4] << 8) & 0xF00) | pLeftStickCal[3]; // X Axis center
  828. ctx->m_StickCalData[0].axis[1].sCenter = (pLeftStickCal[5] << 4) | (pLeftStickCal[4] >> 4); // Y Axis center
  829. ctx->m_StickCalData[0].axis[0].sMin = ((pLeftStickCal[7] << 8) & 0xF00) | pLeftStickCal[6]; // X Axis min below center
  830. ctx->m_StickCalData[0].axis[1].sMin = (pLeftStickCal[8] << 4) | (pLeftStickCal[7] >> 4); // Y Axis min below center
  831. // Right stick
  832. ctx->m_StickCalData[1].axis[0].sCenter = ((pRightStickCal[1] << 8) & 0xF00) | pRightStickCal[0]; // X Axis center
  833. ctx->m_StickCalData[1].axis[1].sCenter = (pRightStickCal[2] << 4) | (pRightStickCal[1] >> 4); // Y Axis center
  834. ctx->m_StickCalData[1].axis[0].sMin = ((pRightStickCal[4] << 8) & 0xF00) | pRightStickCal[3]; // X Axis min below center
  835. ctx->m_StickCalData[1].axis[1].sMin = (pRightStickCal[5] << 4) | (pRightStickCal[4] >> 4); // Y Axis min below center
  836. ctx->m_StickCalData[1].axis[0].sMax = ((pRightStickCal[7] << 8) & 0xF00) | pRightStickCal[6]; // X Axis max above center
  837. ctx->m_StickCalData[1].axis[1].sMax = (pRightStickCal[8] << 4) | (pRightStickCal[7] >> 4); // Y Axis max above center
  838. // Filter out any values that were uninitialized (0xFFF) in the SPI read
  839. for (stick = 0; stick < 2; ++stick) {
  840. for (axis = 0; axis < 2; ++axis) {
  841. if (ctx->m_StickCalData[stick].axis[axis].sCenter == 0xFFF) {
  842. ctx->m_StickCalData[stick].axis[axis].sCenter = 2048;
  843. }
  844. if (ctx->m_StickCalData[stick].axis[axis].sMax == 0xFFF) {
  845. ctx->m_StickCalData[stick].axis[axis].sMax = (Sint16)(ctx->m_StickCalData[stick].axis[axis].sCenter * 0.7f);
  846. }
  847. if (ctx->m_StickCalData[stick].axis[axis].sMin == 0xFFF) {
  848. ctx->m_StickCalData[stick].axis[axis].sMin = (Sint16)(ctx->m_StickCalData[stick].axis[axis].sCenter * 0.7f);
  849. }
  850. }
  851. }
  852. for (stick = 0; stick < 2; ++stick) {
  853. for (axis = 0; axis < 2; ++axis) {
  854. ctx->m_StickExtents[stick].axis[axis].sMin = -(Sint16)(ctx->m_StickCalData[stick].axis[axis].sMin * 0.7f);
  855. ctx->m_StickExtents[stick].axis[axis].sMax = (Sint16)(ctx->m_StickCalData[stick].axis[axis].sMax * 0.7f);
  856. }
  857. }
  858. for (stick = 0; stick < 2; ++stick) {
  859. for (axis = 0; axis < 2; ++axis) {
  860. ctx->m_SimpleStickExtents[stick].axis[axis].sMin = (Sint16)(SDL_MIN_SINT16 * 0.5f);
  861. ctx->m_SimpleStickExtents[stick].axis[axis].sMax = (Sint16)(SDL_MAX_SINT16 * 0.5f);
  862. }
  863. }
  864. return true;
  865. }
  866. static bool LoadIMUCalibration(SDL_DriverSwitch_Context *ctx)
  867. {
  868. SwitchSubcommandInputPacket_t *reply = NULL;
  869. // Read Calibration Info
  870. SwitchSPIOpData_t readParams;
  871. readParams.unAddress = k_unSPIIMUScaleStartOffset;
  872. readParams.ucLength = k_unSPIIMUScaleLength;
  873. if (WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readParams, sizeof(readParams), &reply)) {
  874. Uint8 *pIMUScale;
  875. Sint16 sAccelRawX, sAccelRawY, sAccelRawZ, sGyroRawX, sGyroRawY, sGyroRawZ;
  876. // IMU scale gives us multipliers for converting raw values to real world values
  877. pIMUScale = reply->spiReadData.rgucReadData;
  878. sAccelRawX = (pIMUScale[1] << 8) | pIMUScale[0];
  879. sAccelRawY = (pIMUScale[3] << 8) | pIMUScale[2];
  880. sAccelRawZ = (pIMUScale[5] << 8) | pIMUScale[4];
  881. sGyroRawX = (pIMUScale[13] << 8) | pIMUScale[12];
  882. sGyroRawY = (pIMUScale[15] << 8) | pIMUScale[14];
  883. sGyroRawZ = (pIMUScale[17] << 8) | pIMUScale[16];
  884. // Check for user calibration data. If it's present and set, it'll override the factory settings
  885. readParams.unAddress = k_unSPIIMUUserScaleStartOffset;
  886. readParams.ucLength = k_unSPIIMUUserScaleLength;
  887. if (WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readParams, sizeof(readParams), &reply) && (pIMUScale[0] | pIMUScale[1] << 8) == 0xA1B2) {
  888. pIMUScale = reply->spiReadData.rgucReadData;
  889. sAccelRawX = (pIMUScale[3] << 8) | pIMUScale[2];
  890. sAccelRawY = (pIMUScale[5] << 8) | pIMUScale[4];
  891. sAccelRawZ = (pIMUScale[7] << 8) | pIMUScale[6];
  892. sGyroRawX = (pIMUScale[15] << 8) | pIMUScale[14];
  893. sGyroRawY = (pIMUScale[17] << 8) | pIMUScale[16];
  894. sGyroRawZ = (pIMUScale[19] << 8) | pIMUScale[18];
  895. }
  896. // Accelerometer scale
  897. ctx->m_IMUScaleData.fAccelScaleX = SWITCH_ACCEL_SCALE_MULT / (SWITCH_ACCEL_SCALE_OFFSET - (float)sAccelRawX) * SDL_STANDARD_GRAVITY;
  898. ctx->m_IMUScaleData.fAccelScaleY = SWITCH_ACCEL_SCALE_MULT / (SWITCH_ACCEL_SCALE_OFFSET - (float)sAccelRawY) * SDL_STANDARD_GRAVITY;
  899. ctx->m_IMUScaleData.fAccelScaleZ = SWITCH_ACCEL_SCALE_MULT / (SWITCH_ACCEL_SCALE_OFFSET - (float)sAccelRawZ) * SDL_STANDARD_GRAVITY;
  900. // Gyro scale
  901. ctx->m_IMUScaleData.fGyroScaleX = SWITCH_GYRO_SCALE_MULT / (SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawX) * SDL_PI_F / 180.0f;
  902. ctx->m_IMUScaleData.fGyroScaleY = SWITCH_GYRO_SCALE_MULT / (SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawY) * SDL_PI_F / 180.0f;
  903. ctx->m_IMUScaleData.fGyroScaleZ = SWITCH_GYRO_SCALE_MULT / (SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawZ) * SDL_PI_F / 180.0f;
  904. } else {
  905. // Use default values
  906. const float accelScale = SDL_STANDARD_GRAVITY / SWITCH_ACCEL_SCALE;
  907. const float gyroScale = SDL_PI_F / 180.0f / SWITCH_GYRO_SCALE;
  908. ctx->m_IMUScaleData.fAccelScaleX = accelScale;
  909. ctx->m_IMUScaleData.fAccelScaleY = accelScale;
  910. ctx->m_IMUScaleData.fAccelScaleZ = accelScale;
  911. ctx->m_IMUScaleData.fGyroScaleX = gyroScale;
  912. ctx->m_IMUScaleData.fGyroScaleY = gyroScale;
  913. ctx->m_IMUScaleData.fGyroScaleZ = gyroScale;
  914. }
  915. return true;
  916. }
  917. static Sint16 ApplyStickCalibration(SDL_DriverSwitch_Context *ctx, int nStick, int nAxis, Sint16 sRawValue)
  918. {
  919. sRawValue -= ctx->m_StickCalData[nStick].axis[nAxis].sCenter;
  920. if (sRawValue > ctx->m_StickExtents[nStick].axis[nAxis].sMax) {
  921. ctx->m_StickExtents[nStick].axis[nAxis].sMax = sRawValue;
  922. }
  923. if (sRawValue < ctx->m_StickExtents[nStick].axis[nAxis].sMin) {
  924. ctx->m_StickExtents[nStick].axis[nAxis].sMin = sRawValue;
  925. }
  926. return (Sint16)HIDAPI_RemapVal(sRawValue, ctx->m_StickExtents[nStick].axis[nAxis].sMin, ctx->m_StickExtents[nStick].axis[nAxis].sMax, SDL_MIN_SINT16, SDL_MAX_SINT16);
  927. }
  928. static Sint16 ApplySimpleStickCalibration(SDL_DriverSwitch_Context *ctx, int nStick, int nAxis, Sint16 sRawValue)
  929. {
  930. // 0x8000 is the neutral value for all joystick axes
  931. const Uint16 usJoystickCenter = 0x8000;
  932. sRawValue -= usJoystickCenter;
  933. if (sRawValue > ctx->m_SimpleStickExtents[nStick].axis[nAxis].sMax) {
  934. ctx->m_SimpleStickExtents[nStick].axis[nAxis].sMax = sRawValue;
  935. }
  936. if (sRawValue < ctx->m_SimpleStickExtents[nStick].axis[nAxis].sMin) {
  937. ctx->m_SimpleStickExtents[nStick].axis[nAxis].sMin = sRawValue;
  938. }
  939. return (Sint16)HIDAPI_RemapVal(sRawValue, ctx->m_SimpleStickExtents[nStick].axis[nAxis].sMin, ctx->m_SimpleStickExtents[nStick].axis[nAxis].sMax, SDL_MIN_SINT16, SDL_MAX_SINT16);
  940. }
  941. static Uint8 RemapButton(SDL_DriverSwitch_Context *ctx, Uint8 button)
  942. {
  943. if (ctx->m_bUseButtonLabels) {
  944. // Use button labels instead of positions, e.g. Nintendo Online Classic controllers
  945. switch (button) {
  946. case SDL_GAMEPAD_BUTTON_SOUTH:
  947. return SDL_GAMEPAD_BUTTON_EAST;
  948. case SDL_GAMEPAD_BUTTON_EAST:
  949. return SDL_GAMEPAD_BUTTON_SOUTH;
  950. case SDL_GAMEPAD_BUTTON_WEST:
  951. return SDL_GAMEPAD_BUTTON_NORTH;
  952. case SDL_GAMEPAD_BUTTON_NORTH:
  953. return SDL_GAMEPAD_BUTTON_WEST;
  954. default:
  955. break;
  956. }
  957. }
  958. return button;
  959. }
  960. static int GetMaxWriteAttempts(SDL_HIDAPI_Device *device)
  961. {
  962. if (device->vendor_id == USB_VENDOR_NINTENDO &&
  963. device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
  964. // This device is a little slow and we know we're always on USB
  965. return 20;
  966. } else {
  967. return 5;
  968. }
  969. }
  970. static ESwitchDeviceInfoControllerType ReadJoyConControllerType(SDL_HIDAPI_Device *device)
  971. {
  972. ESwitchDeviceInfoControllerType eControllerType = k_eSwitchDeviceInfoControllerType_Unknown;
  973. const int MAX_ATTEMPTS = 1; // Don't try too long, in case this is a zombie Bluetooth controller
  974. int attempts = 0;
  975. // Create enough of a context to read the controller type from the device
  976. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)SDL_calloc(1, sizeof(*ctx));
  977. if (ctx) {
  978. ctx->device = device;
  979. ctx->m_bSyncWrite = true;
  980. ctx->m_nMaxWriteAttempts = GetMaxWriteAttempts(device);
  981. for ( ; ; ) {
  982. ++attempts;
  983. if (device->is_bluetooth) {
  984. SwitchSubcommandInputPacket_t *reply = NULL;
  985. if (WriteSubcommand(ctx, k_eSwitchSubcommandIDs_RequestDeviceInfo, NULL, 0, &reply)) {
  986. eControllerType = CalculateControllerType(ctx, (ESwitchDeviceInfoControllerType)reply->deviceInfo.ucDeviceType);
  987. }
  988. } else {
  989. if (WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Status, NULL, 0, true)) {
  990. SwitchProprietaryStatusPacket_t *status = (SwitchProprietaryStatusPacket_t *)&ctx->m_rgucReadBuffer[0];
  991. eControllerType = CalculateControllerType(ctx, (ESwitchDeviceInfoControllerType)status->ucDeviceType);
  992. }
  993. }
  994. if (eControllerType == k_eSwitchDeviceInfoControllerType_Unknown && attempts < MAX_ATTEMPTS) {
  995. // Wait a bit and try again
  996. SDL_Delay(100);
  997. continue;
  998. }
  999. break;
  1000. }
  1001. SDL_free(ctx);
  1002. }
  1003. return eControllerType;
  1004. }
  1005. static bool HasHomeLED(SDL_DriverSwitch_Context *ctx)
  1006. {
  1007. Uint16 vendor_id = ctx->device->vendor_id;
  1008. Uint16 product_id = ctx->device->product_id;
  1009. // The Power A Nintendo Switch Pro controllers don't have a Home LED
  1010. if (vendor_id == 0 && product_id == 0) {
  1011. return false;
  1012. }
  1013. // HORI Wireless Switch Pad
  1014. if (vendor_id == 0x0f0d && product_id == 0x00f6) {
  1015. return false;
  1016. }
  1017. // Third party controllers don't have a home LED and will shut off if we try to set it
  1018. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_Unknown ||
  1019. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_LicProController) {
  1020. return false;
  1021. }
  1022. // The Nintendo Online classic controllers don't have a Home LED
  1023. if (vendor_id == USB_VENDOR_NINTENDO &&
  1024. ctx->m_eControllerType > k_eSwitchDeviceInfoControllerType_ProController) {
  1025. return false;
  1026. }
  1027. return true;
  1028. }
  1029. static bool AlwaysUsesLabels(Uint16 vendor_id, Uint16 product_id, ESwitchDeviceInfoControllerType eControllerType)
  1030. {
  1031. // Some controllers don't have a diamond button configuration, so should always use labels
  1032. if (SDL_IsJoystickGameCube(vendor_id, product_id)) {
  1033. return true;
  1034. }
  1035. switch (eControllerType) {
  1036. case k_eSwitchDeviceInfoControllerType_HVCLeft:
  1037. case k_eSwitchDeviceInfoControllerType_HVCRight:
  1038. case k_eSwitchDeviceInfoControllerType_NESLeft:
  1039. case k_eSwitchDeviceInfoControllerType_NESRight:
  1040. case k_eSwitchDeviceInfoControllerType_N64:
  1041. case k_eSwitchDeviceInfoControllerType_SEGA_Genesis:
  1042. return true;
  1043. default:
  1044. return false;
  1045. }
  1046. }
  1047. static void HIDAPI_DriverNintendoClassic_RegisterHints(SDL_HintCallback callback, void *userdata)
  1048. {
  1049. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_NINTENDO_CLASSIC, callback, userdata);
  1050. }
  1051. static void HIDAPI_DriverNintendoClassic_UnregisterHints(SDL_HintCallback callback, void *userdata)
  1052. {
  1053. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_NINTENDO_CLASSIC, callback, userdata);
  1054. }
  1055. static bool HIDAPI_DriverNintendoClassic_IsEnabled(void)
  1056. {
  1057. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_NINTENDO_CLASSIC, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  1058. }
  1059. static bool HIDAPI_DriverNintendoClassic_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)
  1060. {
  1061. if (vendor_id == USB_VENDOR_NINTENDO) {
  1062. if (product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT) {
  1063. if (SDL_strncmp(name, "NES Controller", 14) == 0 ||
  1064. SDL_strncmp(name, "HVC Controller", 14) == 0) {
  1065. return true;
  1066. }
  1067. }
  1068. if (product_id == USB_PRODUCT_NINTENDO_N64_CONTROLLER) {
  1069. return true;
  1070. }
  1071. if (product_id == USB_PRODUCT_NINTENDO_SEGA_GENESIS_CONTROLLER) {
  1072. return true;
  1073. }
  1074. if (product_id == USB_PRODUCT_NINTENDO_SNES_CONTROLLER) {
  1075. return true;
  1076. }
  1077. }
  1078. return false;
  1079. }
  1080. static void HIDAPI_DriverJoyCons_RegisterHints(SDL_HintCallback callback, void *userdata)
  1081. {
  1082. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, callback, userdata);
  1083. }
  1084. static void HIDAPI_DriverJoyCons_UnregisterHints(SDL_HintCallback callback, void *userdata)
  1085. {
  1086. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, callback, userdata);
  1087. }
  1088. static bool HIDAPI_DriverJoyCons_IsEnabled(void)
  1089. {
  1090. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  1091. }
  1092. static bool HIDAPI_DriverJoyCons_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)
  1093. {
  1094. if (vendor_id == USB_VENDOR_NINTENDO) {
  1095. if (product_id == USB_PRODUCT_NINTENDO_SWITCH_PRO && device && device->dev) {
  1096. // This might be a Kinvoca Joy-Con that reports VID/PID as a Switch Pro controller
  1097. ESwitchDeviceInfoControllerType eControllerType = ReadJoyConControllerType(device);
  1098. if (eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
  1099. eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  1100. return true;
  1101. }
  1102. }
  1103. if (product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT ||
  1104. product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT ||
  1105. product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
  1106. return true;
  1107. }
  1108. }
  1109. return false;
  1110. }
  1111. static void HIDAPI_DriverSwitch_RegisterHints(SDL_HintCallback callback, void *userdata)
  1112. {
  1113. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH, callback, userdata);
  1114. }
  1115. static void HIDAPI_DriverSwitch_UnregisterHints(SDL_HintCallback callback, void *userdata)
  1116. {
  1117. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH, callback, userdata);
  1118. }
  1119. static bool HIDAPI_DriverSwitch_IsEnabled(void)
  1120. {
  1121. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_SWITCH, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  1122. }
  1123. static bool HIDAPI_DriverSwitch_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)
  1124. {
  1125. /* The HORI Wireless Switch Pad enumerates as a HID device when connected via USB
  1126. with the same VID/PID as when connected over Bluetooth but doesn't actually
  1127. support communication over USB. The most reliable way to block this without allowing the
  1128. controller to continually attempt to reconnect is to filter it out by manufacturer/product string.
  1129. Note that the controller does have a different product string when connected over Bluetooth.
  1130. */
  1131. if (SDL_strcmp(name, "HORI Wireless Switch Pad") == 0) {
  1132. return false;
  1133. }
  1134. // If it's handled by another driver, it's not handled here
  1135. if (HIDAPI_DriverNintendoClassic_IsSupportedDevice(device, name, type, vendor_id, product_id, version, interface_number, interface_class, interface_subclass, interface_protocol) ||
  1136. HIDAPI_DriverJoyCons_IsSupportedDevice(device, name, type, vendor_id, product_id, version, interface_number, interface_class, interface_subclass, interface_protocol)) {
  1137. return false;
  1138. }
  1139. return (type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO);
  1140. }
  1141. static void UpdateDeviceIdentity(SDL_HIDAPI_Device *device)
  1142. {
  1143. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  1144. if (ctx->m_bInputOnly) {
  1145. if (SDL_IsJoystickGameCube(device->vendor_id, device->product_id)) {
  1146. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1147. }
  1148. } else {
  1149. char serial[18];
  1150. switch (ctx->m_eControllerType) {
  1151. case k_eSwitchDeviceInfoControllerType_JoyConLeft:
  1152. HIDAPI_SetDeviceName(device, "Nintendo Switch Joy-Con (L)");
  1153. HIDAPI_SetDeviceProduct(device, USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT);
  1154. device->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT;
  1155. break;
  1156. case k_eSwitchDeviceInfoControllerType_JoyConRight:
  1157. HIDAPI_SetDeviceName(device, "Nintendo Switch Joy-Con (R)");
  1158. HIDAPI_SetDeviceProduct(device, USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT);
  1159. device->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT;
  1160. break;
  1161. case k_eSwitchDeviceInfoControllerType_ProController:
  1162. case k_eSwitchDeviceInfoControllerType_LicProController:
  1163. HIDAPI_SetDeviceName(device, "Nintendo Switch Pro Controller");
  1164. HIDAPI_SetDeviceProduct(device, USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH_PRO);
  1165. device->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
  1166. break;
  1167. case k_eSwitchDeviceInfoControllerType_HVCLeft:
  1168. HIDAPI_SetDeviceName(device, "Nintendo HVC Controller (1)");
  1169. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1170. break;
  1171. case k_eSwitchDeviceInfoControllerType_HVCRight:
  1172. HIDAPI_SetDeviceName(device, "Nintendo HVC Controller (2)");
  1173. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1174. break;
  1175. case k_eSwitchDeviceInfoControllerType_NESLeft:
  1176. HIDAPI_SetDeviceName(device, "Nintendo NES Controller (L)");
  1177. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1178. break;
  1179. case k_eSwitchDeviceInfoControllerType_NESRight:
  1180. HIDAPI_SetDeviceName(device, "Nintendo NES Controller (R)");
  1181. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1182. break;
  1183. case k_eSwitchDeviceInfoControllerType_SNES:
  1184. HIDAPI_SetDeviceName(device, "Nintendo SNES Controller");
  1185. HIDAPI_SetDeviceProduct(device, USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SNES_CONTROLLER);
  1186. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1187. break;
  1188. case k_eSwitchDeviceInfoControllerType_N64:
  1189. HIDAPI_SetDeviceName(device, "Nintendo N64 Controller");
  1190. HIDAPI_SetDeviceProduct(device, USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_N64_CONTROLLER);
  1191. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1192. break;
  1193. case k_eSwitchDeviceInfoControllerType_SEGA_Genesis:
  1194. HIDAPI_SetDeviceName(device, "Nintendo SEGA Genesis Controller");
  1195. HIDAPI_SetDeviceProduct(device, USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SEGA_GENESIS_CONTROLLER);
  1196. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1197. break;
  1198. case k_eSwitchDeviceInfoControllerType_Unknown:
  1199. // We couldn't read the device info for this controller, might not be fully compliant
  1200. if (device->vendor_id == USB_VENDOR_NINTENDO) {
  1201. switch (device->product_id) {
  1202. case USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT:
  1203. ctx->m_eControllerType = k_eSwitchDeviceInfoControllerType_JoyConLeft;
  1204. HIDAPI_SetDeviceName(device, "Nintendo Switch Joy-Con (L)");
  1205. device->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT;
  1206. break;
  1207. case USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT:
  1208. ctx->m_eControllerType = k_eSwitchDeviceInfoControllerType_JoyConRight;
  1209. HIDAPI_SetDeviceName(device, "Nintendo Switch Joy-Con (R)");
  1210. device->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT;
  1211. break;
  1212. case USB_PRODUCT_NINTENDO_SWITCH_PRO:
  1213. ctx->m_eControllerType = k_eSwitchDeviceInfoControllerType_ProController;
  1214. HIDAPI_SetDeviceName(device, "Nintendo Switch Pro Controller");
  1215. device->type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
  1216. break;
  1217. default:
  1218. break;
  1219. }
  1220. }
  1221. return;
  1222. default:
  1223. device->type = SDL_GAMEPAD_TYPE_STANDARD;
  1224. break;
  1225. }
  1226. device->guid.data[15] = ctx->m_eControllerType;
  1227. (void)SDL_snprintf(serial, sizeof(serial), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
  1228. ctx->m_rgucMACAddress[0],
  1229. ctx->m_rgucMACAddress[1],
  1230. ctx->m_rgucMACAddress[2],
  1231. ctx->m_rgucMACAddress[3],
  1232. ctx->m_rgucMACAddress[4],
  1233. ctx->m_rgucMACAddress[5]);
  1234. HIDAPI_SetDeviceSerial(device, serial);
  1235. }
  1236. }
  1237. static bool HIDAPI_DriverSwitch_InitDevice(SDL_HIDAPI_Device *device)
  1238. {
  1239. SDL_DriverSwitch_Context *ctx;
  1240. ctx = (SDL_DriverSwitch_Context *)SDL_calloc(1, sizeof(*ctx));
  1241. if (!ctx) {
  1242. return false;
  1243. }
  1244. ctx->device = device;
  1245. device->context = ctx;
  1246. ctx->m_nMaxWriteAttempts = GetMaxWriteAttempts(device);
  1247. ctx->m_bSyncWrite = true;
  1248. // Find out whether or not we can send output reports
  1249. ctx->m_bInputOnly = SDL_IsJoystickNintendoSwitchProInputOnly(device->vendor_id, device->product_id);
  1250. if (!ctx->m_bInputOnly) {
  1251. // Initialize rumble data, important for reading device info on the MOBAPAD M073
  1252. SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]);
  1253. SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[1]);
  1254. BReadDeviceInfo(ctx);
  1255. }
  1256. UpdateDeviceIdentity(device);
  1257. // Prefer the USB device over the Bluetooth device
  1258. if (device->is_bluetooth) {
  1259. if (HIDAPI_HasConnectedUSBDevice(device->serial)) {
  1260. return true;
  1261. }
  1262. } else {
  1263. HIDAPI_DisconnectBluetoothDevice(device->serial);
  1264. }
  1265. return HIDAPI_JoystickConnected(device, NULL);
  1266. }
  1267. static int HIDAPI_DriverSwitch_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  1268. {
  1269. return -1;
  1270. }
  1271. static void HIDAPI_DriverSwitch_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  1272. {
  1273. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  1274. if (!ctx->joystick) {
  1275. return;
  1276. }
  1277. ctx->m_nPlayerIndex = player_index;
  1278. UpdateSlotLED(ctx);
  1279. }
  1280. static bool HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1281. {
  1282. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  1283. SDL_AssertJoysticksLocked();
  1284. ctx->joystick = joystick;
  1285. ctx->m_bSyncWrite = true;
  1286. if (!ctx->m_bInputOnly) {
  1287. GetInitialInputMode(ctx);
  1288. ctx->m_nCurrentInputMode = ctx->m_nInitialInputMode;
  1289. // Initialize rumble data
  1290. SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]);
  1291. SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[1]);
  1292. if (!device->is_bluetooth) {
  1293. if (!BTrySetupUSB(ctx)) {
  1294. SDL_SetError("Couldn't setup USB mode");
  1295. return false;
  1296. }
  1297. }
  1298. if (!LoadStickCalibration(ctx)) {
  1299. SDL_SetError("Couldn't load stick calibration");
  1300. return false;
  1301. }
  1302. if (ctx->m_eControllerType != k_eSwitchDeviceInfoControllerType_HVCLeft &&
  1303. ctx->m_eControllerType != k_eSwitchDeviceInfoControllerType_HVCRight &&
  1304. ctx->m_eControllerType != k_eSwitchDeviceInfoControllerType_NESLeft &&
  1305. ctx->m_eControllerType != k_eSwitchDeviceInfoControllerType_NESRight &&
  1306. ctx->m_eControllerType != k_eSwitchDeviceInfoControllerType_SNES &&
  1307. ctx->m_eControllerType != k_eSwitchDeviceInfoControllerType_N64 &&
  1308. ctx->m_eControllerType != k_eSwitchDeviceInfoControllerType_SEGA_Genesis) {
  1309. if (LoadIMUCalibration(ctx)) {
  1310. ctx->m_bSensorsSupported = true;
  1311. }
  1312. }
  1313. // Enable vibration
  1314. SetVibrationEnabled(ctx, 1);
  1315. // Set desired input mode
  1316. SDL_AddHintCallback(SDL_HINT_JOYSTICK_ENHANCED_REPORTS,
  1317. SDL_EnhancedReportsChanged, ctx);
  1318. // Start sending USB reports
  1319. if (!device->is_bluetooth) {
  1320. // ForceUSB doesn't generate an ACK, so don't wait for a reply
  1321. if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, false)) {
  1322. SDL_SetError("Couldn't start USB reports");
  1323. return false;
  1324. }
  1325. }
  1326. // Set the LED state
  1327. if (HasHomeLED(ctx)) {
  1328. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
  1329. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  1330. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_JOYCON_HOME_LED,
  1331. SDL_HomeLEDHintChanged, ctx);
  1332. } else {
  1333. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED,
  1334. SDL_HomeLEDHintChanged, ctx);
  1335. }
  1336. }
  1337. }
  1338. if (AlwaysUsesLabels(device->vendor_id, device->product_id, ctx->m_eControllerType)) {
  1339. ctx->m_bUseButtonLabels = true;
  1340. }
  1341. // Initialize player index (needed for setting LEDs)
  1342. ctx->m_nPlayerIndex = SDL_GetJoystickPlayerIndex(joystick);
  1343. ctx->m_bPlayerLights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED, true);
  1344. UpdateSlotLED(ctx);
  1345. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED,
  1346. SDL_PlayerLEDHintChanged, ctx);
  1347. // Initialize the joystick capabilities
  1348. joystick->nbuttons = SDL_GAMEPAD_NUM_SWITCH_BUTTONS;
  1349. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  1350. joystick->nhats = 1;
  1351. // Set up for input
  1352. ctx->m_bSyncWrite = false;
  1353. ctx->m_ulLastIMUReset = ctx->m_ulLastInput = SDL_GetTicks();
  1354. ctx->m_ulIMUUpdateIntervalNS = SDL_MS_TO_NS(5); // Start off at 5 ms update rate
  1355. // Set up for vertical mode
  1356. ctx->m_bVerticalMode = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS, false);
  1357. return true;
  1358. }
  1359. static bool HIDAPI_DriverSwitch_ActuallyRumbleJoystick(SDL_DriverSwitch_Context *ctx, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  1360. {
  1361. /* Experimentally determined rumble values. These will only matter on some controllers as tested ones
  1362. * seem to disregard these and just use any non-zero rumble values as a binary flag for constant rumble
  1363. *
  1364. * More information about these values can be found here:
  1365. * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
  1366. */
  1367. const Uint16 k_usHighFreq = 0x0074;
  1368. const Uint8 k_ucHighFreqAmp = EncodeRumbleHighAmplitude(high_frequency_rumble);
  1369. const Uint8 k_ucLowFreq = 0x3D;
  1370. const Uint16 k_usLowFreqAmp = EncodeRumbleLowAmplitude(low_frequency_rumble);
  1371. if (low_frequency_rumble || high_frequency_rumble) {
  1372. EncodeRumble(&ctx->m_RumblePacket.rumbleData[0], k_usHighFreq, k_ucHighFreqAmp, k_ucLowFreq, k_usLowFreqAmp);
  1373. EncodeRumble(&ctx->m_RumblePacket.rumbleData[1], k_usHighFreq, k_ucHighFreqAmp, k_ucLowFreq, k_usLowFreqAmp);
  1374. } else {
  1375. SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]);
  1376. SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[1]);
  1377. }
  1378. ctx->m_bRumbleActive = (low_frequency_rumble || high_frequency_rumble);
  1379. if (!WriteRumble(ctx)) {
  1380. return SDL_SetError("Couldn't send rumble packet");
  1381. }
  1382. return true;
  1383. }
  1384. static bool HIDAPI_DriverSwitch_SendPendingRumble(SDL_DriverSwitch_Context *ctx)
  1385. {
  1386. if (SDL_GetTicks() < (ctx->m_ulRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
  1387. return true;
  1388. }
  1389. if (ctx->m_bRumblePending) {
  1390. Uint16 low_frequency_rumble = (Uint16)(ctx->m_unRumblePending >> 16);
  1391. Uint16 high_frequency_rumble = (Uint16)ctx->m_unRumblePending;
  1392. #ifdef DEBUG_RUMBLE
  1393. SDL_Log("Sent pending rumble %d/%d, %d ms after previous rumble", low_frequency_rumble, high_frequency_rumble, SDL_GetTicks() - ctx->m_ulRumbleSent);
  1394. #endif
  1395. ctx->m_bRumblePending = false;
  1396. ctx->m_unRumblePending = 0;
  1397. return HIDAPI_DriverSwitch_ActuallyRumbleJoystick(ctx, low_frequency_rumble, high_frequency_rumble);
  1398. }
  1399. if (ctx->m_bRumbleZeroPending) {
  1400. ctx->m_bRumbleZeroPending = false;
  1401. #ifdef DEBUG_RUMBLE
  1402. SDL_Log("Sent pending zero rumble, %d ms after previous rumble", SDL_GetTicks() - ctx->m_ulRumbleSent);
  1403. #endif
  1404. return HIDAPI_DriverSwitch_ActuallyRumbleJoystick(ctx, 0, 0);
  1405. }
  1406. return true;
  1407. }
  1408. static bool HIDAPI_DriverSwitch_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  1409. {
  1410. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  1411. if (ctx->m_bInputOnly) {
  1412. return SDL_Unsupported();
  1413. }
  1414. if (device->parent) {
  1415. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  1416. // Just handle low frequency rumble
  1417. high_frequency_rumble = 0;
  1418. } else if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  1419. // Just handle high frequency rumble
  1420. low_frequency_rumble = 0;
  1421. }
  1422. }
  1423. if (ctx->m_bRumblePending) {
  1424. if (!HIDAPI_DriverSwitch_SendPendingRumble(ctx)) {
  1425. return false;
  1426. }
  1427. }
  1428. if (SDL_GetTicks() < (ctx->m_ulRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
  1429. if (low_frequency_rumble || high_frequency_rumble) {
  1430. Uint32 unRumblePending = ((Uint32)low_frequency_rumble << 16) | high_frequency_rumble;
  1431. // Keep the highest rumble intensity in the given interval
  1432. if (unRumblePending > ctx->m_unRumblePending) {
  1433. ctx->m_unRumblePending = unRumblePending;
  1434. }
  1435. ctx->m_bRumblePending = true;
  1436. ctx->m_bRumbleZeroPending = false;
  1437. } else {
  1438. // When rumble is complete, turn it off
  1439. ctx->m_bRumbleZeroPending = true;
  1440. }
  1441. return true;
  1442. }
  1443. #ifdef DEBUG_RUMBLE
  1444. SDL_Log("Sent rumble %d/%d", low_frequency_rumble, high_frequency_rumble);
  1445. #endif
  1446. return HIDAPI_DriverSwitch_ActuallyRumbleJoystick(ctx, low_frequency_rumble, high_frequency_rumble);
  1447. }
  1448. static bool HIDAPI_DriverSwitch_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  1449. {
  1450. return SDL_Unsupported();
  1451. }
  1452. static Uint32 HIDAPI_DriverSwitch_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1453. {
  1454. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  1455. Uint32 result = 0;
  1456. if (ctx->m_bPlayerLights && !ctx->m_bInputOnly) {
  1457. result |= SDL_JOYSTICK_CAP_PLAYER_LED;
  1458. }
  1459. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_ProController && !ctx->m_bInputOnly) {
  1460. // Doesn't have an RGB LED, so don't return SDL_JOYSTICK_CAP_RGB_LED here
  1461. result |= SDL_JOYSTICK_CAP_RUMBLE;
  1462. } else if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
  1463. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  1464. result |= SDL_JOYSTICK_CAP_RUMBLE;
  1465. }
  1466. return result;
  1467. }
  1468. static bool HIDAPI_DriverSwitch_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1469. {
  1470. return SDL_Unsupported();
  1471. }
  1472. static bool HIDAPI_DriverSwitch_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  1473. {
  1474. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  1475. if (size == sizeof(SwitchCommonOutputPacket_t)) {
  1476. const SwitchCommonOutputPacket_t *packet = (SwitchCommonOutputPacket_t *)data;
  1477. if (packet->ucPacketType != k_eSwitchOutputReportIDs_Rumble) {
  1478. return SDL_SetError("Unknown Nintendo Switch Pro effect type");
  1479. }
  1480. SDL_copyp(&ctx->m_RumblePacket.rumbleData[0], &packet->rumbleData[0]);
  1481. SDL_copyp(&ctx->m_RumblePacket.rumbleData[1], &packet->rumbleData[1]);
  1482. if (!WriteRumble(ctx)) {
  1483. return false;
  1484. }
  1485. // This overwrites any internal rumble
  1486. ctx->m_bRumblePending = false;
  1487. ctx->m_bRumbleZeroPending = false;
  1488. return true;
  1489. } else if (size >= 2 && size <= 256) {
  1490. const Uint8 *payload = (const Uint8 *)data;
  1491. ESwitchSubcommandIDs cmd = (ESwitchSubcommandIDs)payload[0];
  1492. if (cmd == k_eSwitchSubcommandIDs_SetInputReportMode && !device->is_bluetooth) {
  1493. // Going into simple mode over USB disables input reports, so don't do that
  1494. return true;
  1495. }
  1496. if (cmd == k_eSwitchSubcommandIDs_SetHomeLight && !HasHomeLED(ctx)) {
  1497. // Setting the home LED when it's not supported can cause the controller to reset
  1498. return true;
  1499. }
  1500. if (!WriteSubcommand(ctx, cmd, &payload[1], (Uint8)(size - 1), NULL)) {
  1501. return false;
  1502. }
  1503. return true;
  1504. }
  1505. return SDL_Unsupported();
  1506. }
  1507. static bool HIDAPI_DriverSwitch_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  1508. {
  1509. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  1510. UpdateEnhancedModeOnApplicationUsage(ctx);
  1511. if (!ctx->m_bSensorsSupported || (enabled && !ctx->m_bEnhancedMode)) {
  1512. return SDL_Unsupported();
  1513. }
  1514. ctx->m_bReportSensors = enabled;
  1515. ctx->m_unIMUSamples = 0;
  1516. ctx->m_ulIMUSampleTimestampNS = SDL_GetTicksNS();
  1517. UpdateInputMode(ctx);
  1518. SetIMUEnabled(ctx, enabled);
  1519. return true;
  1520. }
  1521. static void HandleInputOnlyControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchInputOnlyControllerStatePacket_t *packet)
  1522. {
  1523. Sint16 axis;
  1524. Uint64 timestamp = SDL_GetTicksNS();
  1525. if (packet->rgucButtons[0] != ctx->m_lastInputOnlyState.rgucButtons[0]) {
  1526. Uint8 data = packet->rgucButtons[0];
  1527. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x02) != 0));
  1528. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x04) != 0));
  1529. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x01) != 0));
  1530. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x08) != 0));
  1531. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x10) != 0));
  1532. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x20) != 0));
  1533. }
  1534. if (packet->rgucButtons[1] != ctx->m_lastInputOnlyState.rgucButtons[1]) {
  1535. Uint8 data = packet->rgucButtons[1];
  1536. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data & 0x01) != 0));
  1537. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x02) != 0));
  1538. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x04) != 0));
  1539. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data & 0x08) != 0));
  1540. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x10) != 0));
  1541. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_SHARE, ((data & 0x20) != 0));
  1542. }
  1543. if (packet->ucStickHat != ctx->m_lastInputOnlyState.ucStickHat) {
  1544. Uint8 hat;
  1545. switch (packet->ucStickHat) {
  1546. case 0:
  1547. hat = SDL_HAT_UP;
  1548. break;
  1549. case 1:
  1550. hat = SDL_HAT_RIGHTUP;
  1551. break;
  1552. case 2:
  1553. hat = SDL_HAT_RIGHT;
  1554. break;
  1555. case 3:
  1556. hat = SDL_HAT_RIGHTDOWN;
  1557. break;
  1558. case 4:
  1559. hat = SDL_HAT_DOWN;
  1560. break;
  1561. case 5:
  1562. hat = SDL_HAT_LEFTDOWN;
  1563. break;
  1564. case 6:
  1565. hat = SDL_HAT_LEFT;
  1566. break;
  1567. case 7:
  1568. hat = SDL_HAT_LEFTUP;
  1569. break;
  1570. default:
  1571. hat = SDL_HAT_CENTERED;
  1572. break;
  1573. }
  1574. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1575. }
  1576. axis = (packet->rgucButtons[0] & 0x40) ? 32767 : -32768;
  1577. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  1578. axis = (packet->rgucButtons[0] & 0x80) ? 32767 : -32768;
  1579. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  1580. if (packet->rgucJoystickLeft[0] != ctx->m_lastInputOnlyState.rgucJoystickLeft[0]) {
  1581. axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickLeft[0], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
  1582. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  1583. }
  1584. if (packet->rgucJoystickLeft[1] != ctx->m_lastInputOnlyState.rgucJoystickLeft[1]) {
  1585. axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickLeft[1], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
  1586. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  1587. }
  1588. if (packet->rgucJoystickRight[0] != ctx->m_lastInputOnlyState.rgucJoystickRight[0]) {
  1589. axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickRight[0], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
  1590. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  1591. }
  1592. if (packet->rgucJoystickRight[1] != ctx->m_lastInputOnlyState.rgucJoystickRight[1]) {
  1593. axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickRight[1], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
  1594. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  1595. }
  1596. ctx->m_lastInputOnlyState = *packet;
  1597. }
  1598. static void HandleCombinedSimpleControllerStateL(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchSimpleStatePacket_t *packet)
  1599. {
  1600. if (packet->rgucButtons[0] != ctx->m_lastSimpleState.rgucButtons[0]) {
  1601. Uint8 data = packet->rgucButtons[0];
  1602. Uint8 hat = 0;
  1603. if (data & 0x01) {
  1604. hat |= SDL_HAT_LEFT;
  1605. }
  1606. if (data & 0x02) {
  1607. hat |= SDL_HAT_DOWN;
  1608. }
  1609. if (data & 0x04) {
  1610. hat |= SDL_HAT_UP;
  1611. }
  1612. if (data & 0x08) {
  1613. hat |= SDL_HAT_RIGHT;
  1614. }
  1615. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1616. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE1, ((data & 0x10) != 0));
  1617. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE2, ((data & 0x20) != 0));
  1618. }
  1619. if (packet->rgucButtons[1] != ctx->m_lastSimpleState.rgucButtons[1]) {
  1620. Uint8 data = packet->rgucButtons[1];
  1621. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data & 0x01) != 0));
  1622. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x04) != 0));
  1623. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_SHARE, ((data & 0x20) != 0));
  1624. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x40) != 0));
  1625. }
  1626. Sint16 axis = (packet->rgucButtons[1] & 0x80) ? 32767 : -32768;
  1627. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  1628. if (packet->ucStickHat != ctx->m_lastSimpleState.ucStickHat) {
  1629. switch (packet->ucStickHat) {
  1630. case 0:
  1631. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1632. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1633. break;
  1634. case 1:
  1635. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1636. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1637. break;
  1638. case 2:
  1639. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1640. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1641. break;
  1642. case 3:
  1643. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1644. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1645. break;
  1646. case 4:
  1647. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1648. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1649. break;
  1650. case 5:
  1651. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1652. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1653. break;
  1654. case 6:
  1655. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1656. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1657. break;
  1658. case 7:
  1659. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1660. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1661. break;
  1662. default:
  1663. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1664. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1665. break;
  1666. }
  1667. }
  1668. }
  1669. static void HandleCombinedSimpleControllerStateR(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchSimpleStatePacket_t *packet)
  1670. {
  1671. if (packet->rgucButtons[0] != ctx->m_lastSimpleState.rgucButtons[0]) {
  1672. Uint8 data = packet->rgucButtons[0];
  1673. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x01) != 0));
  1674. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x02) != 0));
  1675. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x04) != 0));
  1676. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x08) != 0));
  1677. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE2, ((data & 0x10) != 0));
  1678. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE1, ((data & 0x20) != 0));
  1679. }
  1680. if (packet->rgucButtons[1] != ctx->m_lastSimpleState.rgucButtons[1]) {
  1681. Uint8 data = packet->rgucButtons[1];
  1682. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x02) != 0));
  1683. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data & 0x08) != 0));
  1684. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x10) != 0));
  1685. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x40) != 0));
  1686. }
  1687. Sint16 axis = (packet->rgucButtons[1] & 0x80) ? 32767 : -32768;
  1688. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  1689. if (packet->ucStickHat != ctx->m_lastSimpleState.ucStickHat) {
  1690. switch (packet->ucStickHat) {
  1691. case 0:
  1692. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, SDL_JOYSTICK_AXIS_MIN);
  1693. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, 0);
  1694. break;
  1695. case 1:
  1696. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, SDL_JOYSTICK_AXIS_MIN);
  1697. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, SDL_JOYSTICK_AXIS_MIN);
  1698. break;
  1699. case 2:
  1700. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, 0);
  1701. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, SDL_JOYSTICK_AXIS_MIN);
  1702. break;
  1703. case 3:
  1704. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, SDL_JOYSTICK_AXIS_MAX);
  1705. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, SDL_JOYSTICK_AXIS_MIN);
  1706. break;
  1707. case 4:
  1708. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, SDL_JOYSTICK_AXIS_MAX);
  1709. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, 0);
  1710. break;
  1711. case 5:
  1712. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, SDL_JOYSTICK_AXIS_MAX);
  1713. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, SDL_JOYSTICK_AXIS_MAX);
  1714. break;
  1715. case 6:
  1716. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, 0);
  1717. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, SDL_JOYSTICK_AXIS_MAX);
  1718. break;
  1719. case 7:
  1720. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, SDL_JOYSTICK_AXIS_MIN);
  1721. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, SDL_JOYSTICK_AXIS_MAX);
  1722. break;
  1723. default:
  1724. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, 0);
  1725. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, 0);
  1726. break;
  1727. }
  1728. }
  1729. }
  1730. static void HandleMiniSimpleControllerStateL(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchSimpleStatePacket_t *packet)
  1731. {
  1732. if (packet->rgucButtons[0] != ctx->m_lastSimpleState.rgucButtons[0]) {
  1733. Uint8 data = packet->rgucButtons[0];
  1734. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x01) != 0));
  1735. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x02) != 0));
  1736. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x04) != 0));
  1737. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x08) != 0));
  1738. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x10) != 0));
  1739. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x20) != 0));
  1740. }
  1741. if (packet->rgucButtons[1] != ctx->m_lastSimpleState.rgucButtons[1]) {
  1742. Uint8 data = packet->rgucButtons[1];
  1743. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x01) != 0));
  1744. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x04) != 0));
  1745. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x20) != 0));
  1746. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE1, ((data & 0x40) != 0));
  1747. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE2, ((data & 0x80) != 0));
  1748. }
  1749. if (packet->ucStickHat != ctx->m_lastSimpleState.ucStickHat) {
  1750. switch (packet->ucStickHat) {
  1751. case 0:
  1752. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1753. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1754. break;
  1755. case 1:
  1756. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1757. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1758. break;
  1759. case 2:
  1760. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1761. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1762. break;
  1763. case 3:
  1764. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1765. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1766. break;
  1767. case 4:
  1768. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1769. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1770. break;
  1771. case 5:
  1772. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1773. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1774. break;
  1775. case 6:
  1776. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1777. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1778. break;
  1779. case 7:
  1780. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1781. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1782. break;
  1783. default:
  1784. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1785. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1786. break;
  1787. }
  1788. }
  1789. }
  1790. static void HandleMiniSimpleControllerStateR(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchSimpleStatePacket_t *packet)
  1791. {
  1792. if (packet->rgucButtons[0] != ctx->m_lastSimpleState.rgucButtons[0]) {
  1793. Uint8 data = packet->rgucButtons[0];
  1794. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x01) != 0));
  1795. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x02) != 0));
  1796. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x04) != 0));
  1797. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x08) != 0));
  1798. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x10) != 0));
  1799. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x20) != 0));
  1800. }
  1801. if (packet->rgucButtons[1] != ctx->m_lastSimpleState.rgucButtons[1]) {
  1802. Uint8 data = packet->rgucButtons[1];
  1803. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x02) != 0));
  1804. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x08) != 0));
  1805. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x10) != 0));
  1806. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_SHARE, ((data & 0x20) != 0));
  1807. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE1, ((data & 0x40) != 0));
  1808. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE2, ((data & 0x80) != 0));
  1809. }
  1810. if (packet->ucStickHat != ctx->m_lastSimpleState.ucStickHat) {
  1811. switch (packet->ucStickHat) {
  1812. case 0:
  1813. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1814. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1815. break;
  1816. case 1:
  1817. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1818. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1819. break;
  1820. case 2:
  1821. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1822. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1823. break;
  1824. case 3:
  1825. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MAX);
  1826. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1827. break;
  1828. case 4:
  1829. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1830. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1831. break;
  1832. case 5:
  1833. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1834. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MAX);
  1835. break;
  1836. case 6:
  1837. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1838. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1839. break;
  1840. case 7:
  1841. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, SDL_JOYSTICK_AXIS_MIN);
  1842. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, SDL_JOYSTICK_AXIS_MIN);
  1843. break;
  1844. default:
  1845. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, 0);
  1846. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, 0);
  1847. break;
  1848. }
  1849. }
  1850. }
  1851. static void HandleSimpleControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchSimpleStatePacket_t *packet)
  1852. {
  1853. Uint64 timestamp = SDL_GetTicksNS();
  1854. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  1855. if (ctx->device->parent || ctx->m_bVerticalMode) {
  1856. HandleCombinedSimpleControllerStateL(timestamp, joystick, ctx, packet);
  1857. } else {
  1858. HandleMiniSimpleControllerStateL(timestamp, joystick, ctx, packet);
  1859. }
  1860. } else if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  1861. if (ctx->device->parent || ctx->m_bVerticalMode) {
  1862. HandleCombinedSimpleControllerStateR(timestamp, joystick, ctx, packet);
  1863. } else {
  1864. HandleMiniSimpleControllerStateR(timestamp, joystick, ctx, packet);
  1865. }
  1866. } else {
  1867. Sint16 axis;
  1868. if (packet->rgucButtons[0] != ctx->m_lastSimpleState.rgucButtons[0]) {
  1869. Uint8 data = packet->rgucButtons[0];
  1870. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x01) != 0));
  1871. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x02) != 0));
  1872. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x04) != 0));
  1873. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x08) != 0));
  1874. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x10) != 0));
  1875. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x20) != 0));
  1876. }
  1877. if (packet->rgucButtons[1] != ctx->m_lastSimpleState.rgucButtons[1]) {
  1878. Uint8 data = packet->rgucButtons[1];
  1879. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data & 0x01) != 0));
  1880. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x02) != 0));
  1881. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x04) != 0));
  1882. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data & 0x08) != 0));
  1883. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x10) != 0));
  1884. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_SHARE, ((data & 0x20) != 0));
  1885. }
  1886. if (packet->ucStickHat != ctx->m_lastSimpleState.ucStickHat) {
  1887. Uint8 hat;
  1888. switch (packet->ucStickHat) {
  1889. case 0:
  1890. hat = SDL_HAT_UP;
  1891. break;
  1892. case 1:
  1893. hat = SDL_HAT_RIGHTUP;
  1894. break;
  1895. case 2:
  1896. hat = SDL_HAT_RIGHT;
  1897. break;
  1898. case 3:
  1899. hat = SDL_HAT_RIGHTDOWN;
  1900. break;
  1901. case 4:
  1902. hat = SDL_HAT_DOWN;
  1903. break;
  1904. case 5:
  1905. hat = SDL_HAT_LEFTDOWN;
  1906. break;
  1907. case 6:
  1908. hat = SDL_HAT_LEFT;
  1909. break;
  1910. case 7:
  1911. hat = SDL_HAT_LEFTUP;
  1912. break;
  1913. default:
  1914. hat = SDL_HAT_CENTERED;
  1915. break;
  1916. }
  1917. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1918. }
  1919. axis = (packet->rgucButtons[0] & 0x40) ? 32767 : -32768;
  1920. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  1921. axis = ((packet->rgucButtons[0] & 0x80) || (packet->rgucButtons[1] & 0x80)) ? 32767 : -32768;
  1922. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  1923. axis = ApplySimpleStickCalibration(ctx, 0, 0, packet->sJoystickLeft[0]);
  1924. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  1925. axis = ApplySimpleStickCalibration(ctx, 0, 1, packet->sJoystickLeft[1]);
  1926. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  1927. axis = ApplySimpleStickCalibration(ctx, 1, 0, packet->sJoystickRight[0]);
  1928. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  1929. axis = ApplySimpleStickCalibration(ctx, 1, 1, packet->sJoystickRight[1]);
  1930. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  1931. }
  1932. ctx->m_lastSimpleState = *packet;
  1933. }
  1934. static void SendSensorUpdate(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SDL_SensorType type, Uint64 sensor_timestamp, const Sint16 *values)
  1935. {
  1936. float data[3];
  1937. /* Note the order of components has been shuffled to match PlayStation controllers,
  1938. * since that's our de facto standard from already supporting those controllers, and
  1939. * users will want consistent axis mappings across devices.
  1940. */
  1941. if (type == SDL_SENSOR_GYRO || type == SDL_SENSOR_GYRO_L || type == SDL_SENSOR_GYRO_R) {
  1942. data[0] = -(ctx->m_IMUScaleData.fGyroScaleY * (float)values[1]);
  1943. data[1] = ctx->m_IMUScaleData.fGyroScaleZ * (float)values[2];
  1944. data[2] = -(ctx->m_IMUScaleData.fGyroScaleX * (float)values[0]);
  1945. } else {
  1946. data[0] = -(ctx->m_IMUScaleData.fAccelScaleY * (float)values[1]);
  1947. data[1] = ctx->m_IMUScaleData.fAccelScaleZ * (float)values[2];
  1948. data[2] = -(ctx->m_IMUScaleData.fAccelScaleX * (float)values[0]);
  1949. }
  1950. // Right Joy-Con flips some axes, so let's flip them back for consistency
  1951. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  1952. data[0] = -data[0];
  1953. data[1] = -data[1];
  1954. }
  1955. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft &&
  1956. !ctx->device->parent && !ctx->m_bVerticalMode) {
  1957. // Mini-gamepad mode, swap some axes around
  1958. float tmp = data[2];
  1959. data[2] = -data[0];
  1960. data[0] = tmp;
  1961. }
  1962. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight &&
  1963. !ctx->device->parent && !ctx->m_bVerticalMode) {
  1964. // Mini-gamepad mode, swap some axes around
  1965. float tmp = data[2];
  1966. data[2] = data[0];
  1967. data[0] = -tmp;
  1968. }
  1969. SDL_SendJoystickSensor(timestamp, joystick, type, sensor_timestamp, data, 3);
  1970. }
  1971. static void HandleCombinedControllerStateL(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchStatePacket_t *packet)
  1972. {
  1973. Sint16 axis;
  1974. if (packet->controllerState.rgucButtons[1] != ctx->m_lastFullState.controllerState.rgucButtons[1]) {
  1975. Uint8 data = packet->controllerState.rgucButtons[1];
  1976. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data & 0x01) != 0));
  1977. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x08) != 0));
  1978. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_SHARE, ((data & 0x20) != 0));
  1979. }
  1980. if (packet->controllerState.rgucButtons[2] != ctx->m_lastFullState.controllerState.rgucButtons[2]) {
  1981. Uint8 data = packet->controllerState.rgucButtons[2];
  1982. Uint8 hat = 0;
  1983. if (data & 0x01) {
  1984. hat |= SDL_HAT_DOWN;
  1985. }
  1986. if (data & 0x02) {
  1987. hat |= SDL_HAT_UP;
  1988. }
  1989. if (data & 0x04) {
  1990. hat |= SDL_HAT_RIGHT;
  1991. }
  1992. if (data & 0x08) {
  1993. hat |= SDL_HAT_LEFT;
  1994. }
  1995. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1996. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE2, ((data & 0x10) != 0));
  1997. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE1, ((data & 0x20) != 0));
  1998. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x40) != 0));
  1999. axis = (data & 0x80) ? 32767 : -32768;
  2000. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  2001. }
  2002. axis = packet->controllerState.rgucJoystickLeft[0] | ((packet->controllerState.rgucJoystickLeft[1] & 0xF) << 8);
  2003. axis = ApplyStickCalibration(ctx, 0, 0, axis);
  2004. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  2005. axis = ((packet->controllerState.rgucJoystickLeft[1] & 0xF0) >> 4) | (packet->controllerState.rgucJoystickLeft[2] << 4);
  2006. axis = ApplyStickCalibration(ctx, 0, 1, axis);
  2007. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, ~axis);
  2008. }
  2009. static void HandleCombinedControllerStateR(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchStatePacket_t *packet)
  2010. {
  2011. Sint16 axis;
  2012. if (packet->controllerState.rgucButtons[0] != ctx->m_lastFullState.controllerState.rgucButtons[0]) {
  2013. Uint8 data = packet->controllerState.rgucButtons[0];
  2014. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x04) != 0));
  2015. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x08) != 0));
  2016. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x01) != 0));
  2017. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x02) != 0));
  2018. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE1, ((data & 0x10) != 0));
  2019. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE2, ((data & 0x20) != 0));
  2020. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x40) != 0));
  2021. axis = (data & 0x80) ? 32767 : -32768;
  2022. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  2023. }
  2024. if (packet->controllerState.rgucButtons[1] != ctx->m_lastFullState.controllerState.rgucButtons[1]) {
  2025. Uint8 data = packet->controllerState.rgucButtons[1];
  2026. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x02) != 0));
  2027. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data & 0x04) != 0));
  2028. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x10) != 0));
  2029. }
  2030. axis = packet->controllerState.rgucJoystickRight[0] | ((packet->controllerState.rgucJoystickRight[1] & 0xF) << 8);
  2031. axis = ApplyStickCalibration(ctx, 1, 0, axis);
  2032. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  2033. axis = ((packet->controllerState.rgucJoystickRight[1] & 0xF0) >> 4) | (packet->controllerState.rgucJoystickRight[2] << 4);
  2034. axis = ApplyStickCalibration(ctx, 1, 1, axis);
  2035. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, ~axis);
  2036. }
  2037. static void HandleMiniControllerStateL(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchStatePacket_t *packet)
  2038. {
  2039. Sint16 axis;
  2040. if (packet->controllerState.rgucButtons[1] != ctx->m_lastFullState.controllerState.rgucButtons[1]) {
  2041. Uint8 data = packet->controllerState.rgucButtons[1];
  2042. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x01) != 0));
  2043. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x08) != 0));
  2044. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x20) != 0));
  2045. }
  2046. if (packet->controllerState.rgucButtons[2] != ctx->m_lastFullState.controllerState.rgucButtons[2]) {
  2047. Uint8 data = packet->controllerState.rgucButtons[2];
  2048. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x08) != 0));
  2049. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x01) != 0));
  2050. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x02) != 0));
  2051. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x04) != 0));
  2052. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x10) != 0));
  2053. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x20) != 0));
  2054. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE1, ((data & 0x40) != 0));
  2055. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_LEFT_PADDLE2, ((data & 0x80) != 0));
  2056. }
  2057. axis = packet->controllerState.rgucJoystickLeft[0] | ((packet->controllerState.rgucJoystickLeft[1] & 0xF) << 8);
  2058. axis = ApplyStickCalibration(ctx, 0, 0, axis);
  2059. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, ~axis);
  2060. axis = ((packet->controllerState.rgucJoystickLeft[1] & 0xF0) >> 4) | (packet->controllerState.rgucJoystickLeft[2] << 4);
  2061. axis = ApplyStickCalibration(ctx, 0, 1, axis);
  2062. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, ~axis);
  2063. }
  2064. static void HandleMiniControllerStateR(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchStatePacket_t *packet)
  2065. {
  2066. Sint16 axis;
  2067. if (packet->controllerState.rgucButtons[0] != ctx->m_lastFullState.controllerState.rgucButtons[0]) {
  2068. Uint8 data = packet->controllerState.rgucButtons[0];
  2069. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x08) != 0));
  2070. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x02) != 0));
  2071. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x04) != 0));
  2072. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x01) != 0));
  2073. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x10) != 0));
  2074. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x20) != 0));
  2075. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE1, ((data & 0x40) != 0));
  2076. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_RIGHT_PADDLE2, ((data & 0x80) != 0));
  2077. }
  2078. if (packet->controllerState.rgucButtons[1] != ctx->m_lastFullState.controllerState.rgucButtons[1]) {
  2079. Uint8 data = packet->controllerState.rgucButtons[1];
  2080. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x02) != 0));
  2081. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x04) != 0));
  2082. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x10) != 0));
  2083. }
  2084. axis = packet->controllerState.rgucJoystickRight[0] | ((packet->controllerState.rgucJoystickRight[1] & 0xF) << 8);
  2085. axis = ApplyStickCalibration(ctx, 1, 0, axis);
  2086. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  2087. axis = ((packet->controllerState.rgucJoystickRight[1] & 0xF0) >> 4) | (packet->controllerState.rgucJoystickRight[2] << 4);
  2088. axis = ApplyStickCalibration(ctx, 1, 1, axis);
  2089. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  2090. }
  2091. static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchStatePacket_t *packet) SDL_NO_THREAD_SAFETY_ANALYSIS // We unlock and lock the device lock to be able to change IMU state
  2092. {
  2093. Uint64 timestamp = SDL_GetTicksNS();
  2094. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  2095. if (ctx->device->parent || ctx->m_bVerticalMode) {
  2096. HandleCombinedControllerStateL(timestamp, joystick, ctx, packet);
  2097. } else {
  2098. HandleMiniControllerStateL(timestamp, joystick, ctx, packet);
  2099. }
  2100. } else if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  2101. if (ctx->device->parent || ctx->m_bVerticalMode) {
  2102. HandleCombinedControllerStateR(timestamp, joystick, ctx, packet);
  2103. } else {
  2104. HandleMiniControllerStateR(timestamp, joystick, ctx, packet);
  2105. }
  2106. } else {
  2107. Sint16 axis;
  2108. if (packet->controllerState.rgucButtons[0] != ctx->m_lastFullState.controllerState.rgucButtons[0]) {
  2109. Uint8 data = packet->controllerState.rgucButtons[0];
  2110. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_SOUTH), ((data & 0x04) != 0));
  2111. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_EAST), ((data & 0x08) != 0));
  2112. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_WEST), ((data & 0x01) != 0));
  2113. SDL_SendJoystickButton(timestamp, joystick, RemapButton(ctx, SDL_GAMEPAD_BUTTON_NORTH), ((data & 0x02) != 0));
  2114. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x40) != 0));
  2115. }
  2116. if (packet->controllerState.rgucButtons[1] != ctx->m_lastFullState.controllerState.rgucButtons[1]) {
  2117. Uint8 data = packet->controllerState.rgucButtons[1];
  2118. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data & 0x01) != 0));
  2119. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x02) != 0));
  2120. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data & 0x04) != 0));
  2121. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x08) != 0));
  2122. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x10) != 0));
  2123. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SWITCH_SHARE, ((data & 0x20) != 0));
  2124. }
  2125. if (packet->controllerState.rgucButtons[2] != ctx->m_lastFullState.controllerState.rgucButtons[2]) {
  2126. Uint8 data = packet->controllerState.rgucButtons[2];
  2127. Uint8 hat = 0;
  2128. if (data & 0x01) {
  2129. hat |= SDL_HAT_DOWN;
  2130. }
  2131. if (data & 0x02) {
  2132. hat |= SDL_HAT_UP;
  2133. }
  2134. if (data & 0x04) {
  2135. hat |= SDL_HAT_RIGHT;
  2136. }
  2137. if (data & 0x08) {
  2138. hat |= SDL_HAT_LEFT;
  2139. }
  2140. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  2141. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x40) != 0));
  2142. }
  2143. axis = (packet->controllerState.rgucButtons[0] & 0x80) ? 32767 : -32768;
  2144. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  2145. axis = (packet->controllerState.rgucButtons[2] & 0x80) ? 32767 : -32768;
  2146. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  2147. axis = packet->controllerState.rgucJoystickLeft[0] | ((packet->controllerState.rgucJoystickLeft[1] & 0xF) << 8);
  2148. axis = ApplyStickCalibration(ctx, 0, 0, axis);
  2149. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  2150. axis = ((packet->controllerState.rgucJoystickLeft[1] & 0xF0) >> 4) | (packet->controllerState.rgucJoystickLeft[2] << 4);
  2151. axis = ApplyStickCalibration(ctx, 0, 1, axis);
  2152. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, ~axis);
  2153. axis = packet->controllerState.rgucJoystickRight[0] | ((packet->controllerState.rgucJoystickRight[1] & 0xF) << 8);
  2154. axis = ApplyStickCalibration(ctx, 1, 0, axis);
  2155. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  2156. axis = ((packet->controllerState.rgucJoystickRight[1] & 0xF0) >> 4) | (packet->controllerState.rgucJoystickRight[2] << 4);
  2157. axis = ApplyStickCalibration(ctx, 1, 1, axis);
  2158. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, ~axis);
  2159. }
  2160. /* High nibble of battery/connection byte is battery level, low nibble is connection status (always 0 on 8BitDo Pro 2)
  2161. * LSB of connection nibble is USB/Switch connection status
  2162. * LSB of the battery nibble is used to report charging.
  2163. * The battery level is reported from 0(empty)-8(full)
  2164. */
  2165. SDL_PowerState state;
  2166. int charging = (packet->controllerState.ucBatteryAndConnection & 0x10);
  2167. int level = (packet->controllerState.ucBatteryAndConnection & 0xE0) >> 4;
  2168. int percent = (int)SDL_roundf((level / 8.0f) * 100.0f);
  2169. if (charging) {
  2170. if (level == 8) {
  2171. state = SDL_POWERSTATE_CHARGED;
  2172. } else {
  2173. state = SDL_POWERSTATE_CHARGING;
  2174. }
  2175. } else {
  2176. state = SDL_POWERSTATE_ON_BATTERY;
  2177. }
  2178. SDL_SendJoystickPowerInfo(joystick, state, percent);
  2179. if (ctx->m_bReportSensors) {
  2180. bool bHasSensorData = (packet->imuState[0].sAccelZ != 0 ||
  2181. packet->imuState[0].sAccelY != 0 ||
  2182. packet->imuState[0].sAccelX != 0);
  2183. if (bHasSensorData) {
  2184. const Uint32 IMU_UPDATE_RATE_SAMPLE_FREQUENCY = 1000;
  2185. Uint64 sensor_timestamp[3];
  2186. ctx->m_bHasSensorData = true;
  2187. // We got three IMU samples, calculate the IMU update rate and timestamps
  2188. ctx->m_unIMUSamples += 3;
  2189. if (ctx->m_unIMUSamples >= IMU_UPDATE_RATE_SAMPLE_FREQUENCY) {
  2190. Uint64 now = SDL_GetTicksNS();
  2191. Uint64 elapsed = (now - ctx->m_ulIMUSampleTimestampNS);
  2192. if (elapsed > 0) {
  2193. ctx->m_ulIMUUpdateIntervalNS = elapsed / ctx->m_unIMUSamples;
  2194. }
  2195. ctx->m_unIMUSamples = 0;
  2196. ctx->m_ulIMUSampleTimestampNS = now;
  2197. }
  2198. ctx->m_ulTimestampNS += ctx->m_ulIMUUpdateIntervalNS;
  2199. sensor_timestamp[0] = ctx->m_ulTimestampNS;
  2200. ctx->m_ulTimestampNS += ctx->m_ulIMUUpdateIntervalNS;
  2201. sensor_timestamp[1] = ctx->m_ulTimestampNS;
  2202. ctx->m_ulTimestampNS += ctx->m_ulIMUUpdateIntervalNS;
  2203. sensor_timestamp[2] = ctx->m_ulTimestampNS;
  2204. if (!ctx->device->parent ||
  2205. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  2206. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO, sensor_timestamp[0], &packet->imuState[2].sGyroX);
  2207. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL, sensor_timestamp[0], &packet->imuState[2].sAccelX);
  2208. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO, sensor_timestamp[1], &packet->imuState[1].sGyroX);
  2209. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL, sensor_timestamp[1], &packet->imuState[1].sAccelX);
  2210. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO, sensor_timestamp[2], &packet->imuState[0].sGyroX);
  2211. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL, sensor_timestamp[2], &packet->imuState[0].sAccelX);
  2212. }
  2213. if (ctx->device->parent &&
  2214. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  2215. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO_L, sensor_timestamp[0], &packet->imuState[2].sGyroX);
  2216. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL_L, sensor_timestamp[0], &packet->imuState[2].sAccelX);
  2217. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO_L, sensor_timestamp[1], &packet->imuState[1].sGyroX);
  2218. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL_L, sensor_timestamp[1], &packet->imuState[1].sAccelX);
  2219. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO_L, sensor_timestamp[2], &packet->imuState[0].sGyroX);
  2220. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL_L, sensor_timestamp[2], &packet->imuState[0].sAccelX);
  2221. }
  2222. if (ctx->device->parent &&
  2223. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  2224. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO_R, sensor_timestamp[0], &packet->imuState[2].sGyroX);
  2225. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL_R, sensor_timestamp[0], &packet->imuState[2].sAccelX);
  2226. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO_R, sensor_timestamp[1], &packet->imuState[1].sGyroX);
  2227. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL_R, sensor_timestamp[1], &packet->imuState[1].sAccelX);
  2228. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_GYRO_R, sensor_timestamp[2], &packet->imuState[0].sGyroX);
  2229. SendSensorUpdate(timestamp, joystick, ctx, SDL_SENSOR_ACCEL_R, sensor_timestamp[2], &packet->imuState[0].sAccelX);
  2230. }
  2231. } else if (ctx->m_bHasSensorData) {
  2232. // Uh oh, someone turned off the IMU?
  2233. const int IMU_RESET_DELAY_MS = 3000;
  2234. Uint64 now = SDL_GetTicks();
  2235. if (now >= (ctx->m_ulLastIMUReset + IMU_RESET_DELAY_MS)) {
  2236. SDL_HIDAPI_Device *device = ctx->device;
  2237. if (device->updating) {
  2238. SDL_UnlockMutex(device->dev_lock);
  2239. }
  2240. SetIMUEnabled(ctx, true);
  2241. if (device->updating) {
  2242. SDL_LockMutex(device->dev_lock);
  2243. }
  2244. ctx->m_ulLastIMUReset = now;
  2245. }
  2246. } else {
  2247. // We have never gotten IMU data, probably not supported on this device
  2248. }
  2249. }
  2250. ctx->m_lastFullState = *packet;
  2251. }
  2252. static bool HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
  2253. {
  2254. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  2255. SDL_Joystick *joystick = NULL;
  2256. int size;
  2257. int packet_count = 0;
  2258. Uint64 now = SDL_GetTicks();
  2259. if (device->num_joysticks > 0) {
  2260. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  2261. }
  2262. while ((size = ReadInput(ctx)) > 0) {
  2263. #ifdef DEBUG_SWITCH_PROTOCOL
  2264. HIDAPI_DumpPacket("Nintendo Switch packet: size = %d", ctx->m_rgucReadBuffer, size);
  2265. #endif
  2266. ++packet_count;
  2267. ctx->m_ulLastInput = now;
  2268. if (!joystick) {
  2269. continue;
  2270. }
  2271. if (ctx->m_bInputOnly) {
  2272. HandleInputOnlyControllerState(joystick, ctx, (SwitchInputOnlyControllerStatePacket_t *)&ctx->m_rgucReadBuffer[0]);
  2273. } else {
  2274. if (ctx->m_rgucReadBuffer[0] == k_eSwitchInputReportIDs_SubcommandReply) {
  2275. continue;
  2276. }
  2277. ctx->m_nCurrentInputMode = ctx->m_rgucReadBuffer[0];
  2278. switch (ctx->m_rgucReadBuffer[0]) {
  2279. case k_eSwitchInputReportIDs_SimpleControllerState:
  2280. HandleSimpleControllerState(joystick, ctx, (SwitchSimpleStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
  2281. break;
  2282. case k_eSwitchInputReportIDs_FullControllerState:
  2283. case k_eSwitchInputReportIDs_FullControllerAndMcuState:
  2284. // This is the extended report, we can enable sensors now in auto mode
  2285. UpdateEnhancedModeOnEnhancedReport(ctx);
  2286. HandleFullControllerState(joystick, ctx, (SwitchStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
  2287. break;
  2288. default:
  2289. break;
  2290. }
  2291. }
  2292. }
  2293. if (joystick) {
  2294. if (packet_count == 0) {
  2295. if (!ctx->m_bInputOnly && !device->is_bluetooth &&
  2296. ctx->device->product_id != USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
  2297. const int INPUT_WAIT_TIMEOUT_MS = 100;
  2298. if (now >= (ctx->m_ulLastInput + INPUT_WAIT_TIMEOUT_MS)) {
  2299. // Steam may have put the controller back into non-reporting mode
  2300. bool wasSyncWrite = ctx->m_bSyncWrite;
  2301. ctx->m_bSyncWrite = true;
  2302. WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, false);
  2303. ctx->m_bSyncWrite = wasSyncWrite;
  2304. }
  2305. } else if (device->is_bluetooth &&
  2306. ctx->m_nCurrentInputMode != k_eSwitchInputReportIDs_SimpleControllerState) {
  2307. const int INPUT_WAIT_TIMEOUT_MS = 3000;
  2308. if (now >= (ctx->m_ulLastInput + INPUT_WAIT_TIMEOUT_MS)) {
  2309. // Bluetooth may have disconnected, try reopening the controller
  2310. size = -1;
  2311. }
  2312. }
  2313. }
  2314. if (ctx->m_bRumblePending || ctx->m_bRumbleZeroPending) {
  2315. HIDAPI_DriverSwitch_SendPendingRumble(ctx);
  2316. } else if (ctx->m_bRumbleActive &&
  2317. now >= (ctx->m_ulRumbleSent + RUMBLE_REFRESH_FREQUENCY_MS)) {
  2318. #ifdef DEBUG_RUMBLE
  2319. SDL_Log("Sent continuing rumble, %d ms after previous rumble", now - ctx->m_ulRumbleSent);
  2320. #endif
  2321. WriteRumble(ctx);
  2322. }
  2323. }
  2324. // Reconnect the Bluetooth device once the USB device is gone
  2325. if (device->num_joysticks == 0 && device->is_bluetooth && packet_count > 0 &&
  2326. !device->parent &&
  2327. !HIDAPI_HasConnectedUSBDevice(device->serial)) {
  2328. HIDAPI_JoystickConnected(device, NULL);
  2329. }
  2330. if (size < 0 && device->num_joysticks > 0) {
  2331. // Read error, device is disconnected
  2332. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  2333. }
  2334. return (size >= 0);
  2335. }
  2336. static void HIDAPI_DriverSwitch_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  2337. {
  2338. SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
  2339. if (!ctx->m_bInputOnly) {
  2340. // Restore simple input mode for other applications
  2341. if (!ctx->m_nInitialInputMode ||
  2342. ctx->m_nInitialInputMode == k_eSwitchInputReportIDs_SimpleControllerState) {
  2343. SetInputMode(ctx, k_eSwitchInputReportIDs_SimpleControllerState);
  2344. }
  2345. }
  2346. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_ENHANCED_REPORTS,
  2347. SDL_EnhancedReportsChanged, ctx);
  2348. if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
  2349. ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
  2350. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_JOYCON_HOME_LED,
  2351. SDL_HomeLEDHintChanged, ctx);
  2352. } else {
  2353. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED,
  2354. SDL_HomeLEDHintChanged, ctx);
  2355. }
  2356. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED,
  2357. SDL_PlayerLEDHintChanged, ctx);
  2358. ctx->joystick = NULL;
  2359. ctx->m_bReportSensors = false;
  2360. ctx->m_bEnhancedMode = false;
  2361. ctx->m_bEnhancedModeAvailable = false;
  2362. }
  2363. static void HIDAPI_DriverSwitch_FreeDevice(SDL_HIDAPI_Device *device)
  2364. {
  2365. }
  2366. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverNintendoClassic = {
  2367. SDL_HINT_JOYSTICK_HIDAPI_NINTENDO_CLASSIC,
  2368. true,
  2369. HIDAPI_DriverNintendoClassic_RegisterHints,
  2370. HIDAPI_DriverNintendoClassic_UnregisterHints,
  2371. HIDAPI_DriverNintendoClassic_IsEnabled,
  2372. HIDAPI_DriverNintendoClassic_IsSupportedDevice,
  2373. HIDAPI_DriverSwitch_InitDevice,
  2374. HIDAPI_DriverSwitch_GetDevicePlayerIndex,
  2375. HIDAPI_DriverSwitch_SetDevicePlayerIndex,
  2376. HIDAPI_DriverSwitch_UpdateDevice,
  2377. HIDAPI_DriverSwitch_OpenJoystick,
  2378. HIDAPI_DriverSwitch_RumbleJoystick,
  2379. HIDAPI_DriverSwitch_RumbleJoystickTriggers,
  2380. HIDAPI_DriverSwitch_GetJoystickCapabilities,
  2381. HIDAPI_DriverSwitch_SetJoystickLED,
  2382. HIDAPI_DriverSwitch_SendJoystickEffect,
  2383. HIDAPI_DriverSwitch_SetJoystickSensorsEnabled,
  2384. HIDAPI_DriverSwitch_CloseJoystick,
  2385. HIDAPI_DriverSwitch_FreeDevice,
  2386. };
  2387. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverJoyCons = {
  2388. SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS,
  2389. true,
  2390. HIDAPI_DriverJoyCons_RegisterHints,
  2391. HIDAPI_DriverJoyCons_UnregisterHints,
  2392. HIDAPI_DriverJoyCons_IsEnabled,
  2393. HIDAPI_DriverJoyCons_IsSupportedDevice,
  2394. HIDAPI_DriverSwitch_InitDevice,
  2395. HIDAPI_DriverSwitch_GetDevicePlayerIndex,
  2396. HIDAPI_DriverSwitch_SetDevicePlayerIndex,
  2397. HIDAPI_DriverSwitch_UpdateDevice,
  2398. HIDAPI_DriverSwitch_OpenJoystick,
  2399. HIDAPI_DriverSwitch_RumbleJoystick,
  2400. HIDAPI_DriverSwitch_RumbleJoystickTriggers,
  2401. HIDAPI_DriverSwitch_GetJoystickCapabilities,
  2402. HIDAPI_DriverSwitch_SetJoystickLED,
  2403. HIDAPI_DriverSwitch_SendJoystickEffect,
  2404. HIDAPI_DriverSwitch_SetJoystickSensorsEnabled,
  2405. HIDAPI_DriverSwitch_CloseJoystick,
  2406. HIDAPI_DriverSwitch_FreeDevice,
  2407. };
  2408. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch = {
  2409. SDL_HINT_JOYSTICK_HIDAPI_SWITCH,
  2410. true,
  2411. HIDAPI_DriverSwitch_RegisterHints,
  2412. HIDAPI_DriverSwitch_UnregisterHints,
  2413. HIDAPI_DriverSwitch_IsEnabled,
  2414. HIDAPI_DriverSwitch_IsSupportedDevice,
  2415. HIDAPI_DriverSwitch_InitDevice,
  2416. HIDAPI_DriverSwitch_GetDevicePlayerIndex,
  2417. HIDAPI_DriverSwitch_SetDevicePlayerIndex,
  2418. HIDAPI_DriverSwitch_UpdateDevice,
  2419. HIDAPI_DriverSwitch_OpenJoystick,
  2420. HIDAPI_DriverSwitch_RumbleJoystick,
  2421. HIDAPI_DriverSwitch_RumbleJoystickTriggers,
  2422. HIDAPI_DriverSwitch_GetJoystickCapabilities,
  2423. HIDAPI_DriverSwitch_SetJoystickLED,
  2424. HIDAPI_DriverSwitch_SendJoystickEffect,
  2425. HIDAPI_DriverSwitch_SetJoystickSensorsEnabled,
  2426. HIDAPI_DriverSwitch_CloseJoystick,
  2427. HIDAPI_DriverSwitch_FreeDevice,
  2428. };
  2429. #endif // SDL_JOYSTICK_HIDAPI_SWITCH
  2430. #endif // SDL_JOYSTICK_HIDAPI