SDL_hidapi_sinput.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 2025 Mitchell Cairns <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "../../SDL_hints_c.h"
  21. #include "../SDL_sysjoystick.h"
  22. #include "SDL_hidapijoystick_c.h"
  23. #include "SDL_hidapi_rumble.h"
  24. #ifdef SDL_JOYSTICK_HIDAPI_SINPUT
  25. /*****************************************************************************************************/
  26. // This protocol is documented at:
  27. // https://docs.handheldlegend.com/s/sinput/doc/sinput-hid-protocol-TkPYWlDMAg
  28. /*****************************************************************************************************/
  29. // Define this if you want to log all packets from the controller
  30. #if 0
  31. #define DEBUG_SINPUT_PROTOCOL
  32. #endif
  33. #if 0
  34. #define DEBUG_SINPUT_INIT
  35. #endif
  36. #define SINPUT_DEVICE_REPORT_SIZE 64 // Size of input reports (And CMD Input reports)
  37. #define SINPUT_DEVICE_REPORT_COMMAND_SIZE 48 // Size of command OUTPUT reports
  38. #define SINPUT_DEVICE_REPORT_ID_JOYSTICK_INPUT 0x01
  39. #define SINPUT_DEVICE_REPORT_ID_INPUT_CMDDAT 0x02
  40. #define SINPUT_DEVICE_REPORT_ID_OUTPUT_CMDDAT 0x03
  41. #define SINPUT_DEVICE_COMMAND_HAPTIC 0x01
  42. #define SINPUT_DEVICE_COMMAND_FEATURES 0x02
  43. #define SINPUT_DEVICE_COMMAND_PLAYERLED 0x03
  44. #define SINPUT_DEVICE_COMMAND_JOYSTICKRGB 0x04
  45. #define SINPUT_HAPTIC_TYPE_PRECISE 0x01
  46. #define SINPUT_HAPTIC_TYPE_ERMSIMULATION 0x02
  47. #define SINPUT_DEFAULT_GYRO_SENS 2000
  48. #define SINPUT_DEFAULT_ACCEL_SENS 8
  49. #define SINPUT_REPORT_IDX_BUTTONS_0 3
  50. #define SINPUT_REPORT_IDX_BUTTONS_1 4
  51. #define SINPUT_REPORT_IDX_BUTTONS_2 5
  52. #define SINPUT_REPORT_IDX_BUTTONS_3 6
  53. #define SINPUT_REPORT_IDX_LEFT_X 7
  54. #define SINPUT_REPORT_IDX_LEFT_Y 9
  55. #define SINPUT_REPORT_IDX_RIGHT_X 11
  56. #define SINPUT_REPORT_IDX_RIGHT_Y 13
  57. #define SINPUT_REPORT_IDX_LEFT_TRIGGER 15
  58. #define SINPUT_REPORT_IDX_RIGHT_TRIGGER 17
  59. #define SINPUT_REPORT_IDX_IMU_TIMESTAMP 19
  60. #define SINPUT_REPORT_IDX_IMU_ACCEL_X 23
  61. #define SINPUT_REPORT_IDX_IMU_ACCEL_Y 25
  62. #define SINPUT_REPORT_IDX_IMU_ACCEL_Z 27
  63. #define SINPUT_REPORT_IDX_IMU_GYRO_X 29
  64. #define SINPUT_REPORT_IDX_IMU_GYRO_Y 31
  65. #define SINPUT_REPORT_IDX_IMU_GYRO_Z 33
  66. #define SINPUT_REPORT_IDX_TOUCH1_X 35
  67. #define SINPUT_REPORT_IDX_TOUCH1_Y 37
  68. #define SINPUT_REPORT_IDX_TOUCH1_P 39
  69. #define SINPUT_REPORT_IDX_TOUCH2_X 41
  70. #define SINPUT_REPORT_IDX_TOUCH2_Y 43
  71. #define SINPUT_REPORT_IDX_TOUCH2_P 45
  72. #define SINPUT_BUTTON_IDX_SOUTH 0
  73. #define SINPUT_BUTTON_IDX_EAST 1
  74. #define SINPUT_BUTTON_IDX_WEST 2
  75. #define SINPUT_BUTTON_IDX_NORTH 3
  76. #define SINPUT_BUTTON_IDX_DPAD_UP 4
  77. #define SINPUT_BUTTON_IDX_DPAD_DOWN 5
  78. #define SINPUT_BUTTON_IDX_DPAD_LEFT 6
  79. #define SINPUT_BUTTON_IDX_DPAD_RIGHT 7
  80. #define SINPUT_BUTTON_IDX_LEFT_STICK 8
  81. #define SINPUT_BUTTON_IDX_RIGHT_STICK 9
  82. #define SINPUT_BUTTON_IDX_LEFT_BUMPER 10
  83. #define SINPUT_BUTTON_IDX_RIGHT_BUMPER 11
  84. #define SINPUT_BUTTON_IDX_LEFT_TRIGGER 12
  85. #define SINPUT_BUTTON_IDX_RIGHT_TRIGGER 13
  86. #define SINPUT_BUTTON_IDX_LEFT_PADDLE1 14
  87. #define SINPUT_BUTTON_IDX_RIGHT_PADDLE1 15
  88. #define SINPUT_BUTTON_IDX_START 16
  89. #define SINPUT_BUTTON_IDX_BACK 17
  90. #define SINPUT_BUTTON_IDX_GUIDE 18
  91. #define SINPUT_BUTTON_IDX_CAPTURE 19
  92. #define SINPUT_BUTTON_IDX_LEFT_PADDLE2 20
  93. #define SINPUT_BUTTON_IDX_RIGHT_PADDLE2 21
  94. #define SINPUT_BUTTON_IDX_TOUCHPAD1 22
  95. #define SINPUT_BUTTON_IDX_TOUCHPAD2 23
  96. #define SINPUT_BUTTON_IDX_POWER 24
  97. #define SINPUT_BUTTON_IDX_MISC4 25
  98. #define SINPUT_BUTTON_IDX_MISC5 26
  99. #define SINPUT_BUTTON_IDX_MISC6 27
  100. #define SINPUT_BUTTON_IDX_MISC7 28
  101. #define SINPUT_BUTTON_IDX_MISC8 29
  102. #define SINPUT_BUTTON_IDX_MISC9 30
  103. #define SINPUT_BUTTON_IDX_MISC10 31
  104. #define SINPUT_REPORT_IDX_COMMAND_RESPONSE_ID 1
  105. #define SINPUT_REPORT_IDX_COMMAND_RESPONSE_BULK 2
  106. #define SINPUT_REPORT_IDX_PLUG_STATUS 1
  107. #define SINPUT_REPORT_IDX_CHARGE_LEVEL 2
  108. #define SINPUT_MAX_ALLOWED_TOUCHPADS 2
  109. #ifndef EXTRACTSINT16
  110. #define EXTRACTSINT16(data, idx) ((Sint16)((data)[(idx)] | ((data)[(idx) + 1] << 8)))
  111. #endif
  112. #ifndef EXTRACTUINT16
  113. #define EXTRACTUINT16(data, idx) ((Uint16)((data)[(idx)] | ((data)[(idx) + 1] << 8)))
  114. #endif
  115. #ifndef EXTRACTUINT32
  116. #define EXTRACTUINT32(data, idx) ((Uint32)((data)[(idx)] | ((data)[(idx) + 1] << 8) | ((data)[(idx) + 2] << 16) | ((data)[(idx) + 3] << 24)))
  117. #endif
  118. typedef struct
  119. {
  120. uint8_t type;
  121. union {
  122. // Frequency Amplitude pairs
  123. struct {
  124. struct {
  125. uint16_t frequency_1;
  126. uint16_t amplitude_1;
  127. uint16_t frequency_2;
  128. uint16_t amplitude_2;
  129. } left;
  130. struct {
  131. uint16_t frequency_1;
  132. uint16_t amplitude_1;
  133. uint16_t frequency_2;
  134. uint16_t amplitude_2;
  135. } right;
  136. } type_1;
  137. // Basic ERM simulation model
  138. struct {
  139. struct {
  140. uint8_t amplitude;
  141. bool brake;
  142. } left;
  143. struct {
  144. uint8_t amplitude;
  145. bool brake;
  146. } right;
  147. } type_2;
  148. };
  149. } SINPUT_HAPTIC_S;
  150. typedef struct
  151. {
  152. SDL_HIDAPI_Device *device;
  153. Uint16 protocol_version;
  154. bool sensors_enabled;
  155. Uint8 player_idx;
  156. bool player_leds_supported;
  157. bool joystick_rgb_supported;
  158. bool rumble_supported;
  159. bool accelerometer_supported;
  160. bool gyroscope_supported;
  161. bool left_analog_stick_supported;
  162. bool right_analog_stick_supported;
  163. bool left_analog_trigger_supported;
  164. bool right_analog_trigger_supported;
  165. bool dpad_supported;
  166. bool touchpad_supported;
  167. Uint8 touchpad_count; // 2 touchpads maximum
  168. Uint8 touchpad_finger_count; // 2 fingers for one touchpad, or 1 per touchpad (2 max)
  169. Uint8 polling_rate_ms;
  170. Uint8 sub_type; // Subtype of the device, 0 in most cases
  171. Uint16 accelRange; // Example would be 2,4,8,16 +/- (g-force)
  172. Uint16 gyroRange; // Example would be 1000,2000,4000 +/- (degrees per second)
  173. float accelScale; // Scale factor for accelerometer values
  174. float gyroScale; // Scale factor for gyroscope values
  175. Uint8 last_state[USB_PACKET_LENGTH];
  176. Uint8 buttons_count;
  177. Uint8 usage_masks[4];
  178. Uint32 last_imu_timestamp_us;
  179. Uint64 imu_timestamp_ns; // Nanoseconds. We accumulate with received deltas
  180. } SDL_DriverSInput_Context;
  181. // Converts raw int16_t gyro scale setting
  182. static inline float CalculateGyroScale(uint16_t dps_range)
  183. {
  184. return SDL_PI_F / 180.0f / (32768.0f / (float)dps_range);
  185. }
  186. // Converts raw int16_t accel scale setting
  187. static inline float CalculateAccelScale(uint16_t g_range)
  188. {
  189. return SDL_STANDARD_GRAVITY / (32768.0f / (float)g_range);
  190. }
  191. static void ProcessSDLFeaturesResponse(SDL_HIDAPI_Device *device, Uint8 *data)
  192. {
  193. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  194. // Obtain protocol version
  195. ctx->protocol_version = EXTRACTUINT16(data, 0);
  196. // Bitfields are not portable, so we unpack them into a struct value
  197. ctx->rumble_supported = (data[2] & 0x01) != 0;
  198. ctx->player_leds_supported = (data[2] & 0x02) != 0;
  199. ctx->accelerometer_supported = (data[2] & 0x04) != 0;
  200. ctx->gyroscope_supported = (data[2] & 0x08) != 0;
  201. ctx->left_analog_stick_supported = (data[2] & 0x10) != 0;
  202. ctx->right_analog_stick_supported = (data[2] & 0x20) != 0;
  203. ctx->left_analog_trigger_supported = (data[2] & 0x40) != 0;
  204. ctx->right_analog_trigger_supported = (data[2] & 0x80) != 0;
  205. ctx->touchpad_supported = (data[3] & 0x01) != 0;
  206. ctx->joystick_rgb_supported = (data[3] & 0x02) != 0;
  207. SDL_GamepadType type = SDL_GAMEPAD_TYPE_UNKNOWN;
  208. type = (SDL_GamepadType)SDL_clamp(data[4], SDL_GAMEPAD_TYPE_UNKNOWN, SDL_GAMEPAD_TYPE_COUNT);
  209. device->type = type;
  210. // The 4 MSB represent a button layout style SDL_GamepadFaceStyle
  211. // The 4 LSB represent a device sub-type
  212. device->guid.data[15] = data[5];
  213. #if defined(DEBUG_SINPUT_INIT)
  214. SDL_Log("SInput Face Style: %d", (data[5] & 0xF0) >> 4);
  215. SDL_Log("SInput Sub-type: %d", (data[5] & 0xF));
  216. #endif
  217. ctx->polling_rate_ms = data[6];
  218. ctx->accelRange = EXTRACTUINT16(data, 8);
  219. ctx->gyroRange = EXTRACTUINT16(data, 10);
  220. // Masks in LSB to MSB
  221. // South, East, West, North, DUp, DDown, DLeft, DRight
  222. ctx->usage_masks[0] = data[12];
  223. // Stick Left, Stick Right, L Shoulder, R Shoulder,
  224. // L Trigger, R Trigger, L Paddle 1, R Paddle 1
  225. ctx->usage_masks[1] = data[13];
  226. // Start, Back, Guide, Capture, L Paddle 2, R Paddle 2, Touchpad L, Touchpad R
  227. ctx->usage_masks[2] = data[14];
  228. // Power, Misc 4 to 10
  229. ctx->usage_masks[3] = data[15];
  230. // Derive button count from mask
  231. for (Uint8 byte = 0; byte < 4; ++byte) {
  232. for (Uint8 bit = 0; bit < 8; ++bit) {
  233. if ((ctx->usage_masks[byte] & (1 << bit)) != 0) {
  234. ++ctx->buttons_count;
  235. }
  236. }
  237. }
  238. // Convert DPAD to hat
  239. const int DPAD_MASK = (1 << SINPUT_BUTTON_IDX_DPAD_UP) |
  240. (1 << SINPUT_BUTTON_IDX_DPAD_DOWN) |
  241. (1 << SINPUT_BUTTON_IDX_DPAD_LEFT) |
  242. (1 << SINPUT_BUTTON_IDX_DPAD_RIGHT);
  243. if ((ctx->usage_masks[0] & DPAD_MASK) == DPAD_MASK) {
  244. ctx->dpad_supported = true;
  245. ctx->usage_masks[0] &= ~DPAD_MASK;
  246. ctx->buttons_count -= 4;
  247. }
  248. #if defined(DEBUG_SINPUT_INIT)
  249. SDL_Log("Buttons count: %d", ctx->buttons_count);
  250. #endif
  251. // Get and validate touchpad parameters
  252. ctx->touchpad_count = data[16];
  253. ctx->touchpad_finger_count = data[17];
  254. #if defined(DEBUG_SINPUT_INIT)
  255. SDL_Log("Accelerometer Range: %d", ctx->accelRange);
  256. #endif
  257. #if defined(DEBUG_SINPUT_INIT)
  258. SDL_Log("Gyro Range: %d", ctx->gyroRange);
  259. #endif
  260. ctx->accelScale = CalculateAccelScale(ctx->accelRange);
  261. ctx->gyroScale = CalculateGyroScale(ctx->gyroRange);
  262. }
  263. static bool RetrieveSDLFeatures(SDL_HIDAPI_Device *device)
  264. {
  265. int written = 0;
  266. // Attempt to send the SDL features get command.
  267. for (int attempt = 0; attempt < 8; ++attempt) {
  268. const Uint8 featuresGetCommand[SINPUT_DEVICE_REPORT_COMMAND_SIZE] = { SINPUT_DEVICE_REPORT_ID_OUTPUT_CMDDAT, SINPUT_DEVICE_COMMAND_FEATURES };
  269. // This write will occasionally return -1, so ignore failure here and try again
  270. written = SDL_hid_write(device->dev, featuresGetCommand, sizeof(featuresGetCommand));
  271. if (written == SINPUT_DEVICE_REPORT_COMMAND_SIZE) {
  272. break;
  273. }
  274. }
  275. if (written < SINPUT_DEVICE_REPORT_COMMAND_SIZE) {
  276. SDL_SetError("SInput device SDL Features GET command could not write");
  277. return false;
  278. }
  279. int read = 0;
  280. // Read the reply
  281. for (int i = 0; i < 100; ++i) {
  282. SDL_Delay(1);
  283. Uint8 data[USB_PACKET_LENGTH];
  284. read = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0);
  285. if (read < 0) {
  286. SDL_SetError("SInput device SDL Features GET command could not read");
  287. return false;
  288. }
  289. if (read == 0) {
  290. continue;
  291. }
  292. #ifdef DEBUG_SINPUT_PROTOCOL
  293. HIDAPI_DumpPacket("SInput packet: size = %d", data, size);
  294. #endif
  295. if ((read == USB_PACKET_LENGTH) && (data[0] == SINPUT_DEVICE_REPORT_ID_INPUT_CMDDAT) && (data[1] == SINPUT_DEVICE_COMMAND_FEATURES)) {
  296. ProcessSDLFeaturesResponse(device, &(data[SINPUT_REPORT_IDX_COMMAND_RESPONSE_BULK]));
  297. #if defined(DEBUG_SINPUT_INIT)
  298. SDL_Log("Received SInput SDL Features command response");
  299. #endif
  300. return true;
  301. }
  302. }
  303. return false;
  304. }
  305. // Type 2 haptics are for more traditional rumble such as
  306. // ERM motors or simulated ERM motors
  307. static inline void HapticsType2Pack(SINPUT_HAPTIC_S *in, Uint8 *out)
  308. {
  309. // Type of haptics
  310. out[0] = 2;
  311. out[1] = in->type_2.left.amplitude;
  312. out[2] = in->type_2.left.brake;
  313. out[3] = in->type_2.right.amplitude;
  314. out[4] = in->type_2.right.brake;
  315. }
  316. static void HIDAPI_DriverSInput_RegisterHints(SDL_HintCallback callback, void *userdata)
  317. {
  318. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SINPUT, callback, userdata);
  319. }
  320. static void HIDAPI_DriverSInput_UnregisterHints(SDL_HintCallback callback, void *userdata)
  321. {
  322. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SINPUT, callback, userdata);
  323. }
  324. static bool HIDAPI_DriverSInput_IsEnabled(void)
  325. {
  326. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_SINPUT, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  327. }
  328. static bool HIDAPI_DriverSInput_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)
  329. {
  330. return SDL_IsJoystickSInputController(vendor_id, product_id);
  331. }
  332. static bool HIDAPI_DriverSInput_InitDevice(SDL_HIDAPI_Device *device)
  333. {
  334. #if defined(DEBUG_SINPUT_INIT)
  335. SDL_Log("SInput device Init");
  336. #endif
  337. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)SDL_calloc(1, sizeof(*ctx));
  338. if (!ctx) {
  339. return false;
  340. }
  341. ctx->device = device;
  342. device->context = ctx;
  343. if (!RetrieveSDLFeatures(device)) {
  344. return false;
  345. }
  346. return HIDAPI_JoystickConnected(device, NULL);
  347. }
  348. static int HIDAPI_DriverSInput_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  349. {
  350. return -1;
  351. }
  352. static void HIDAPI_DriverSInput_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  353. {
  354. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  355. if (ctx->player_leds_supported) {
  356. player_index = SDL_clamp(player_index + 1, 0, 255);
  357. Uint8 player_num = (Uint8)player_index;
  358. ctx->player_idx = player_num;
  359. // Set player number, finalizing the setup
  360. Uint8 playerLedCommand[SINPUT_DEVICE_REPORT_COMMAND_SIZE] = { SINPUT_DEVICE_REPORT_ID_OUTPUT_CMDDAT, SINPUT_DEVICE_COMMAND_PLAYERLED, ctx->player_idx };
  361. int playerNumBytesWritten = SDL_hid_write(device->dev, playerLedCommand, SINPUT_DEVICE_REPORT_COMMAND_SIZE);
  362. if (playerNumBytesWritten < 0) {
  363. SDL_SetError("SInput device player led command could not write");
  364. }
  365. }
  366. }
  367. #ifndef DEG2RAD
  368. #define DEG2RAD(x) ((float)(x) * (float)(SDL_PI_F / 180.f))
  369. #endif
  370. static bool HIDAPI_DriverSInput_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  371. {
  372. #if defined(DEBUG_SINPUT_INIT)
  373. SDL_Log("SInput device Open");
  374. #endif
  375. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  376. SDL_AssertJoysticksLocked();
  377. joystick->nbuttons = ctx->buttons_count;
  378. SDL_zeroa(ctx->last_state);
  379. int axes = 0;
  380. if (ctx->left_analog_stick_supported) {
  381. axes += 2;
  382. }
  383. if (ctx->right_analog_stick_supported) {
  384. axes += 2;
  385. }
  386. if (ctx->left_analog_trigger_supported) {
  387. ++axes;
  388. }
  389. if (ctx->right_analog_trigger_supported) {
  390. ++axes;
  391. }
  392. joystick->naxes = axes;
  393. if (ctx->dpad_supported) {
  394. joystick->nhats = 1;
  395. }
  396. if (ctx->accelerometer_supported) {
  397. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 1000.0f / ctx->polling_rate_ms);
  398. }
  399. if (ctx->gyroscope_supported) {
  400. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 1000.0f / ctx->polling_rate_ms);
  401. }
  402. if (ctx->touchpad_supported) {
  403. // If touchpad is supported, minimum 1, max is capped
  404. ctx->touchpad_count = SDL_clamp(ctx->touchpad_count, 1, SINPUT_MAX_ALLOWED_TOUCHPADS);
  405. if (ctx->touchpad_count > 1) {
  406. // Support two separate touchpads with 1 finger each
  407. // or support one touchpad with 2 fingers max
  408. ctx->touchpad_finger_count = 1;
  409. }
  410. if (ctx->touchpad_count > 0) {
  411. SDL_PrivateJoystickAddTouchpad(joystick, ctx->touchpad_finger_count);
  412. }
  413. if (ctx->touchpad_count > 1) {
  414. SDL_PrivateJoystickAddTouchpad(joystick, ctx->touchpad_finger_count);
  415. }
  416. }
  417. return true;
  418. }
  419. static bool HIDAPI_DriverSInput_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  420. {
  421. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  422. if (ctx->rumble_supported) {
  423. SINPUT_HAPTIC_S hapticData = { 0 };
  424. Uint8 hapticReport[SINPUT_DEVICE_REPORT_COMMAND_SIZE] = { SINPUT_DEVICE_REPORT_ID_OUTPUT_CMDDAT, SINPUT_DEVICE_COMMAND_HAPTIC };
  425. // Low Frequency = Left
  426. // High Frequency = Right
  427. hapticData.type_2.left.amplitude = (Uint8) (low_frequency_rumble >> 8);
  428. hapticData.type_2.right.amplitude = (Uint8)(high_frequency_rumble >> 8);
  429. HapticsType2Pack(&hapticData, &(hapticReport[2]));
  430. SDL_HIDAPI_SendRumble(device, hapticReport, SINPUT_DEVICE_REPORT_COMMAND_SIZE);
  431. return true;
  432. }
  433. return SDL_Unsupported();
  434. }
  435. static bool HIDAPI_DriverSInput_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  436. {
  437. return SDL_Unsupported();
  438. }
  439. static Uint32 HIDAPI_DriverSInput_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  440. {
  441. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  442. Uint32 caps = 0;
  443. if (ctx->rumble_supported) {
  444. caps |= SDL_JOYSTICK_CAP_RUMBLE;
  445. }
  446. if (ctx->player_leds_supported) {
  447. caps |= SDL_JOYSTICK_CAP_PLAYER_LED;
  448. }
  449. if (ctx->joystick_rgb_supported) {
  450. caps |= SDL_JOYSTICK_CAP_RGB_LED;
  451. }
  452. return caps;
  453. }
  454. static bool HIDAPI_DriverSInput_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  455. {
  456. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  457. if (ctx->player_leds_supported) {
  458. // Set player number, finalizing the setup
  459. Uint8 joystickRGBCommand[SINPUT_DEVICE_REPORT_COMMAND_SIZE] = { SINPUT_DEVICE_REPORT_ID_OUTPUT_CMDDAT, SINPUT_DEVICE_COMMAND_JOYSTICKRGB, red, green, blue };
  460. int joystickRGBBytesWritten = SDL_hid_write(device->dev, joystickRGBCommand, SINPUT_DEVICE_REPORT_COMMAND_SIZE);
  461. if (joystickRGBBytesWritten < 0) {
  462. SDL_SetError("SInput device joystick rgb command could not write");
  463. return false;
  464. }
  465. return true;
  466. }
  467. return SDL_Unsupported();
  468. }
  469. static bool HIDAPI_DriverSInput_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  470. {
  471. return SDL_Unsupported();
  472. }
  473. static bool HIDAPI_DriverSInput_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  474. {
  475. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  476. if (ctx->accelerometer_supported || ctx->gyroscope_supported) {
  477. ctx->sensors_enabled = enabled;
  478. return true;
  479. }
  480. return SDL_Unsupported();
  481. }
  482. static void HIDAPI_DriverSInput_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverSInput_Context *ctx, Uint8 *data, int size)
  483. {
  484. Sint16 axis = 0;
  485. Sint16 accel = 0;
  486. Sint16 gyro = 0;
  487. Uint64 timestamp = SDL_GetTicksNS();
  488. float imu_values[3] = { 0 };
  489. Uint8 output_idx = 0;
  490. // Process digital buttons according to the supplied
  491. // button mask to create a contiguous button input set
  492. for (Uint8 processes = 0; processes < 4; ++processes) {
  493. Uint8 button_idx = SINPUT_REPORT_IDX_BUTTONS_0 + processes;
  494. for (Uint8 buttons = 0; buttons < 8; ++buttons) {
  495. // If a button is enabled by our usage mask
  496. const Uint8 mask = (0x01 << buttons);
  497. if ((ctx->usage_masks[processes] & mask) != 0) {
  498. bool down = (data[button_idx] & mask) != 0;
  499. if ( (output_idx < SDL_GAMEPAD_BUTTON_COUNT) && (ctx->last_state[button_idx] != data[button_idx]) ) {
  500. SDL_SendJoystickButton(timestamp, joystick, output_idx, down);
  501. }
  502. ++output_idx;
  503. }
  504. }
  505. }
  506. if (ctx->dpad_supported) {
  507. Uint8 hat = SDL_HAT_CENTERED;
  508. if (data[SINPUT_REPORT_IDX_BUTTONS_0] & (1 << SINPUT_BUTTON_IDX_DPAD_UP)) {
  509. hat |= SDL_HAT_UP;
  510. }
  511. if (data[SINPUT_REPORT_IDX_BUTTONS_0] & (1 << SINPUT_BUTTON_IDX_DPAD_DOWN)) {
  512. hat |= SDL_HAT_DOWN;
  513. }
  514. if (data[SINPUT_REPORT_IDX_BUTTONS_0] & (1 << SINPUT_BUTTON_IDX_DPAD_LEFT)) {
  515. hat |= SDL_HAT_LEFT;
  516. }
  517. if (data[SINPUT_REPORT_IDX_BUTTONS_0] & (1 << SINPUT_BUTTON_IDX_DPAD_RIGHT)) {
  518. hat |= SDL_HAT_RIGHT;
  519. }
  520. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  521. }
  522. // Analog inputs map to a signed Sint16 range of -32768 to 32767 from the device.
  523. // Use an axis index because not all gamepads will have the same axis inputs.
  524. Uint8 axis_idx = 0;
  525. // Left Analog Stick
  526. axis = 0; // Reset axis value for joystick
  527. if (ctx->left_analog_stick_supported) {
  528. axis = EXTRACTSINT16(data, SINPUT_REPORT_IDX_LEFT_X);
  529. SDL_SendJoystickAxis(timestamp, joystick, axis_idx, axis);
  530. ++axis_idx;
  531. axis = EXTRACTSINT16(data, SINPUT_REPORT_IDX_LEFT_Y);
  532. SDL_SendJoystickAxis(timestamp, joystick, axis_idx, axis);
  533. ++axis_idx;
  534. }
  535. // Right Analog Stick
  536. axis = 0; // Reset axis value for joystick
  537. if (ctx->right_analog_stick_supported) {
  538. axis = EXTRACTSINT16(data, SINPUT_REPORT_IDX_RIGHT_X);
  539. SDL_SendJoystickAxis(timestamp, joystick, axis_idx, axis);
  540. ++axis_idx;
  541. axis = EXTRACTSINT16(data, SINPUT_REPORT_IDX_RIGHT_Y);
  542. SDL_SendJoystickAxis(timestamp, joystick, axis_idx, axis);
  543. ++axis_idx;
  544. }
  545. // Left Analog Trigger
  546. axis = SDL_MIN_SINT16; // Reset axis value for trigger
  547. if (ctx->left_analog_trigger_supported) {
  548. axis = EXTRACTSINT16(data, SINPUT_REPORT_IDX_LEFT_TRIGGER);
  549. SDL_SendJoystickAxis(timestamp, joystick, axis_idx, axis);
  550. ++axis_idx;
  551. }
  552. // Right Analog Trigger
  553. axis = SDL_MIN_SINT16; // Reset axis value for trigger
  554. if (ctx->right_analog_trigger_supported) {
  555. axis = EXTRACTSINT16(data, SINPUT_REPORT_IDX_RIGHT_TRIGGER);
  556. SDL_SendJoystickAxis(timestamp, joystick, axis_idx, axis);
  557. }
  558. // Battery/Power state handling
  559. if (ctx->last_state[SINPUT_REPORT_IDX_PLUG_STATUS] != data[SINPUT_REPORT_IDX_PLUG_STATUS] ||
  560. ctx->last_state[SINPUT_REPORT_IDX_CHARGE_LEVEL] != data[SINPUT_REPORT_IDX_CHARGE_LEVEL]) {
  561. SDL_PowerState state = SDL_POWERSTATE_UNKNOWN;
  562. Uint8 status = data[SINPUT_REPORT_IDX_PLUG_STATUS];
  563. int percent = data[SINPUT_REPORT_IDX_CHARGE_LEVEL];
  564. percent = SDL_clamp(percent, 0, 100); // Ensure percent is within valid range
  565. switch (status) {
  566. case 1:
  567. state = SDL_POWERSTATE_NO_BATTERY;
  568. percent = 0;
  569. break;
  570. case 2:
  571. state = SDL_POWERSTATE_CHARGING;
  572. break;
  573. case 3:
  574. state = SDL_POWERSTATE_CHARGED;
  575. percent = 100;
  576. break;
  577. case 4:
  578. state = SDL_POWERSTATE_ON_BATTERY;
  579. break;
  580. default:
  581. break;
  582. }
  583. if (state != SDL_POWERSTATE_UNKNOWN) {
  584. SDL_SendJoystickPowerInfo(joystick, state, percent);
  585. }
  586. }
  587. // Extract the IMU timestamp delta (in microseconds)
  588. Uint32 imu_timestamp_us = EXTRACTUINT32(data, SINPUT_REPORT_IDX_IMU_TIMESTAMP);
  589. Uint32 imu_time_delta_us = 0;
  590. // Check if we should process IMU data and if sensors are enabled
  591. if (ctx->sensors_enabled) {
  592. if (imu_timestamp_us >= ctx->last_imu_timestamp_us) {
  593. imu_time_delta_us = (imu_timestamp_us - ctx->last_imu_timestamp_us);
  594. } else {
  595. // Handle rollover case
  596. imu_time_delta_us = (UINT32_MAX - ctx->last_imu_timestamp_us) + imu_timestamp_us + 1;
  597. }
  598. // Convert delta to nanoseconds and update running timestamp
  599. ctx->imu_timestamp_ns += (Uint64)imu_time_delta_us * 1000;
  600. // Update last timestamp
  601. ctx->last_imu_timestamp_us = imu_timestamp_us;
  602. // Process Accelerometer
  603. if (ctx->accelerometer_supported) {
  604. accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_Y);
  605. imu_values[2] = -(float)accel * ctx->accelScale; // Y-axis acceleration
  606. accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_Z);
  607. imu_values[1] = (float)accel * ctx->accelScale; // Z-axis acceleration
  608. accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_X);
  609. imu_values[0] = -(float)accel * ctx->accelScale; // X-axis acceleration
  610. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, ctx->imu_timestamp_ns, imu_values, 3);
  611. }
  612. // Process Gyroscope
  613. if (ctx->gyroscope_supported) {
  614. gyro = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_GYRO_Y);
  615. imu_values[2] = -(float)gyro * ctx->gyroScale; // Y-axis rotation
  616. gyro = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_GYRO_Z);
  617. imu_values[1] = (float)gyro * ctx->gyroScale; // Z-axis rotation
  618. gyro = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_GYRO_X);
  619. imu_values[0] = -(float)gyro * ctx->gyroScale; // X-axis rotation
  620. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, ctx->imu_timestamp_ns, imu_values, 3);
  621. }
  622. }
  623. // Check if we should process touchpad
  624. if (ctx->touchpad_supported && ctx->touchpad_count > 0) {
  625. Uint8 touchpad = 0;
  626. Uint8 finger = 0;
  627. Sint16 touch1X = EXTRACTSINT16(data, SINPUT_REPORT_IDX_TOUCH1_X);
  628. Sint16 touch1Y = EXTRACTSINT16(data, SINPUT_REPORT_IDX_TOUCH1_Y);
  629. Uint16 touch1P = EXTRACTUINT16(data, SINPUT_REPORT_IDX_TOUCH1_P);
  630. Sint16 touch2X = EXTRACTSINT16(data, SINPUT_REPORT_IDX_TOUCH2_X);
  631. Sint16 touch2Y = EXTRACTSINT16(data, SINPUT_REPORT_IDX_TOUCH2_Y);
  632. Uint16 touch2P = EXTRACTUINT16(data, SINPUT_REPORT_IDX_TOUCH2_P);
  633. SDL_SendJoystickTouchpad(timestamp, joystick, touchpad, finger,
  634. touch1P > 0,
  635. touch1X / 65536.0f + 0.5f,
  636. touch1Y / 65536.0f + 0.5f,
  637. touch1P / 32768.0f);
  638. if (ctx->touchpad_count > 1) {
  639. ++touchpad;
  640. } else if (ctx->touchpad_finger_count > 1) {
  641. ++finger;
  642. }
  643. if ((touchpad > 0) || (finger > 0)) {
  644. SDL_SendJoystickTouchpad(timestamp, joystick, touchpad, finger,
  645. touch2P > 0,
  646. touch2X / 65536.0f + 0.5f,
  647. touch2Y / 65536.0f + 0.5f,
  648. touch2P / 32768.0f);
  649. }
  650. }
  651. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  652. }
  653. static bool HIDAPI_DriverSInput_UpdateDevice(SDL_HIDAPI_Device *device)
  654. {
  655. SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
  656. SDL_Joystick *joystick = NULL;
  657. Uint8 data[USB_PACKET_LENGTH];
  658. int size = 0;
  659. if (device->num_joysticks > 0) {
  660. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  661. } else {
  662. return false;
  663. }
  664. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  665. #ifdef DEBUG_SINPUT_PROTOCOL
  666. HIDAPI_DumpPacket("SInput packet: size = %d", data, size);
  667. #endif
  668. if (!joystick) {
  669. continue;
  670. }
  671. // Handle command response information
  672. if (data[0] == SINPUT_DEVICE_REPORT_ID_JOYSTICK_INPUT) {
  673. HIDAPI_DriverSInput_HandleStatePacket(joystick, ctx, data, size);
  674. }
  675. }
  676. if (size < 0) {
  677. // Read error, device is disconnected
  678. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  679. }
  680. return (size >= 0);
  681. }
  682. static void HIDAPI_DriverSInput_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  683. {
  684. }
  685. static void HIDAPI_DriverSInput_FreeDevice(SDL_HIDAPI_Device *device)
  686. {
  687. }
  688. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSInput = {
  689. SDL_HINT_JOYSTICK_HIDAPI_SINPUT,
  690. true,
  691. HIDAPI_DriverSInput_RegisterHints,
  692. HIDAPI_DriverSInput_UnregisterHints,
  693. HIDAPI_DriverSInput_IsEnabled,
  694. HIDAPI_DriverSInput_IsSupportedDevice,
  695. HIDAPI_DriverSInput_InitDevice,
  696. HIDAPI_DriverSInput_GetDevicePlayerIndex,
  697. HIDAPI_DriverSInput_SetDevicePlayerIndex,
  698. HIDAPI_DriverSInput_UpdateDevice,
  699. HIDAPI_DriverSInput_OpenJoystick,
  700. HIDAPI_DriverSInput_RumbleJoystick,
  701. HIDAPI_DriverSInput_RumbleJoystickTriggers,
  702. HIDAPI_DriverSInput_GetJoystickCapabilities,
  703. HIDAPI_DriverSInput_SetJoystickLED,
  704. HIDAPI_DriverSInput_SendJoystickEffect,
  705. HIDAPI_DriverSInput_SetJoystickSensorsEnabled,
  706. HIDAPI_DriverSInput_CloseJoystick,
  707. HIDAPI_DriverSInput_FreeDevice,
  708. };
  709. #endif // SDL_JOYSTICK_HIDAPI_SINPUT
  710. #endif // SDL_JOYSTICK_HIDAPI