SDL_hidapi_switch.c 119 KB

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